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
114 /* RAID map configuration infos. */
116 __u32 pba_of_lba0
; /* start address of partition */
117 __u32 blocks_per_member
;/* blocks per member */
118 __u32 num_data_stripes
; /* number of data stripes */
119 __u16 blocks_per_strip
;
120 __u8 map_state
; /* Normal, Uninitialized, Degraded, Failed */
121 #define IMSM_T_STATE_NORMAL 0
122 #define IMSM_T_STATE_UNINITIALIZED 1
123 #define IMSM_T_STATE_DEGRADED 2
124 #define IMSM_T_STATE_FAILED 3
126 #define IMSM_T_RAID0 0
127 #define IMSM_T_RAID1 1
128 #define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */
129 __u8 num_members
; /* number of member disks */
130 __u8 num_domains
; /* number of parity domains */
131 __u8 failed_disk_num
; /* valid only when state is degraded */
133 __u32 filler
[7]; /* expansion area */
134 #define IMSM_ORD_REBUILD (1 << 24)
135 __u32 disk_ord_tbl
[1]; /* disk_ord_tbl[num_members],
136 * top byte contains some flags
138 } __attribute__ ((packed
));
141 __u32 curr_migr_unit
;
142 __u32 checkpoint_id
; /* id to access curr_migr_unit */
143 __u8 migr_state
; /* Normal or Migrating */
145 #define MIGR_REBUILD 1
146 #define MIGR_VERIFY 2 /* analagous to echo check > sync_action */
147 #define MIGR_GEN_MIGR 3
148 #define MIGR_STATE_CHANGE 4
149 #define MIGR_REPAIR 5
150 __u8 migr_type
; /* Initializing, Rebuilding, ... */
152 __u8 fs_state
; /* fast-sync state for CnG (0xff == disabled) */
153 __u16 verify_errors
; /* number of mismatches */
154 __u16 bad_blocks
; /* number of bad blocks during verify */
156 struct imsm_map map
[1];
157 /* here comes another one if migr_state */
158 } __attribute__ ((packed
));
161 __u8 volume
[MAX_RAID_SERIAL_LEN
];
164 #define DEV_BOOTABLE __cpu_to_le32(0x01)
165 #define DEV_BOOT_DEVICE __cpu_to_le32(0x02)
166 #define DEV_READ_COALESCING __cpu_to_le32(0x04)
167 #define DEV_WRITE_COALESCING __cpu_to_le32(0x08)
168 #define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10)
169 #define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20)
170 #define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40)
171 #define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80)
172 #define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100)
173 #define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200)
174 #define DEV_CLONE_N_GO __cpu_to_le32(0x400)
175 #define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800)
176 #define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000)
177 __u32 status
; /* Persistent RaidDev status */
178 __u32 reserved_blocks
; /* Reserved blocks at beginning of volume */
182 __u8 cng_master_disk
;
186 #define IMSM_DEV_FILLERS 10
187 __u32 filler
[IMSM_DEV_FILLERS
];
189 } __attribute__ ((packed
));
192 __u8 sig
[MAX_SIGNATURE_LENGTH
]; /* 0x00 - 0x1F */
193 __u32 check_sum
; /* 0x20 - 0x23 MPB Checksum */
194 __u32 mpb_size
; /* 0x24 - 0x27 Size of MPB */
195 __u32 family_num
; /* 0x28 - 0x2B Checksum from first time this config was written */
196 __u32 generation_num
; /* 0x2C - 0x2F Incremented each time this array's MPB is written */
197 __u32 error_log_size
; /* 0x30 - 0x33 in bytes */
198 __u32 attributes
; /* 0x34 - 0x37 */
199 __u8 num_disks
; /* 0x38 Number of configured disks */
200 __u8 num_raid_devs
; /* 0x39 Number of configured volumes */
201 __u8 error_log_pos
; /* 0x3A */
202 __u8 fill
[1]; /* 0x3B */
203 __u32 cache_size
; /* 0x3c - 0x40 in mb */
204 __u32 orig_family_num
; /* 0x40 - 0x43 original family num */
205 __u32 pwr_cycle_count
; /* 0x44 - 0x47 simulated power cycle count for array */
206 __u32 bbm_log_size
; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */
207 #define IMSM_FILLERS 35
208 __u32 filler
[IMSM_FILLERS
]; /* 0x4C - 0xD7 RAID_MPB_FILLERS */
209 struct imsm_disk disk
[1]; /* 0xD8 diskTbl[numDisks] */
210 /* here comes imsm_dev[num_raid_devs] */
211 /* here comes BBM logs */
212 } __attribute__ ((packed
));
214 #define BBM_LOG_MAX_ENTRIES 254
216 struct bbm_log_entry
{
217 __u64 defective_block_start
;
218 #define UNREADABLE 0xFFFFFFFF
219 __u32 spare_block_offset
;
220 __u16 remapped_marked_count
;
222 } __attribute__ ((__packed__
));
225 __u32 signature
; /* 0xABADB10C */
227 __u32 reserved_spare_block_count
; /* 0 */
228 __u32 reserved
; /* 0xFFFF */
229 __u64 first_spare_lba
;
230 struct bbm_log_entry mapped_block_entries
[BBM_LOG_MAX_ENTRIES
];
231 } __attribute__ ((__packed__
));
235 static char *map_state_str
[] = { "normal", "uninitialized", "degraded", "failed" };
238 #define RAID_DISK_RESERVED_BLOCKS_IMSM_HI 2209
240 #define GEN_MIGR_AREA_SIZE 2048 /* General Migration Copy Area size in blocks */
242 #define UNIT_SRC_NORMAL 0 /* Source data for curr_migr_unit must
243 * be recovered using srcMap */
244 #define UNIT_SRC_IN_CP_AREA 1 /* Source data for curr_migr_unit has
245 * already been migrated and must
246 * be recovered from checkpoint area */
248 __u32 rec_status
; /* Status used to determine how to restart
249 * migration in case it aborts
251 __u32 curr_migr_unit
; /* 0..numMigrUnits-1 */
252 __u32 family_num
; /* Family number of MPB
253 * containing the RaidDev
254 * that is migrating */
255 __u32 ascending_migr
; /* True if migrating in increasing
257 __u32 blocks_per_unit
; /* Num disk blocks per unit of operation */
258 __u32 dest_depth_per_unit
; /* Num member blocks each destMap
260 * advances per unit-of-operation */
261 __u32 ckpt_area_pba
; /* Pba of first block of ckpt copy area */
262 __u32 dest_1st_member_lba
; /* First member lba on first
263 * stripe of destination */
264 __u32 num_migr_units
; /* Total num migration units-of-op */
265 __u32 post_migr_vol_cap
; /* Size of volume after
266 * migration completes */
267 __u32 post_migr_vol_cap_hi
; /* Expansion space for LBA64 */
268 __u32 ckpt_read_disk_num
; /* Which member disk in destSubMap[0] the
269 * migration ckpt record was read from
270 * (for recovered migrations) */
271 } __attribute__ ((__packed__
));
273 static __u8
migr_type(struct imsm_dev
*dev
)
275 if (dev
->vol
.migr_type
== MIGR_VERIFY
&&
276 dev
->status
& DEV_VERIFY_AND_FIX
)
279 return dev
->vol
.migr_type
;
282 static void set_migr_type(struct imsm_dev
*dev
, __u8 migr_type
)
284 /* for compatibility with older oroms convert MIGR_REPAIR, into
285 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
287 if (migr_type
== MIGR_REPAIR
) {
288 dev
->vol
.migr_type
= MIGR_VERIFY
;
289 dev
->status
|= DEV_VERIFY_AND_FIX
;
291 dev
->vol
.migr_type
= migr_type
;
292 dev
->status
&= ~DEV_VERIFY_AND_FIX
;
296 static unsigned int sector_count(__u32 bytes
)
298 return ((bytes
+ (512-1)) & (~(512-1))) / 512;
301 static unsigned int mpb_sectors(struct imsm_super
*mpb
)
303 return sector_count(__le32_to_cpu(mpb
->mpb_size
));
307 struct imsm_dev
*dev
;
308 struct intel_dev
*next
;
313 enum sys_dev_type type
;
316 struct intel_hba
*next
;
323 /* internal representation of IMSM metadata */
326 void *buf
; /* O_DIRECT buffer for reading/writing metadata */
327 struct imsm_super
*anchor
; /* immovable parameters */
330 void *migr_rec_buf
; /* buffer for I/O operations */
331 struct migr_record
*migr_rec
; /* migration record */
333 size_t len
; /* size of the 'buf' allocation */
334 void *next_buf
; /* for realloc'ing buf from the manager */
336 int updates_pending
; /* count of pending updates for mdmon */
337 int current_vol
; /* index of raid device undergoing creation */
338 __u32 create_offset
; /* common start for 'current_vol' */
339 __u32 random
; /* random data for seeding new family numbers */
340 struct intel_dev
*devlist
;
344 __u8 serial
[MAX_RAID_SERIAL_LEN
];
347 struct imsm_disk disk
;
350 struct extent
*e
; /* for determining freespace @ create */
351 int raiddisk
; /* slot to fill in autolayout */
353 } *disks
, *current_disk
;
354 struct dl
*disk_mgmt_list
; /* list of disks to add/remove while mdmon
356 struct dl
*missing
; /* disks removed while we weren't looking */
357 struct bbm_log
*bbm_log
;
358 struct intel_hba
*hba
; /* device path of the raid controller for this metadata */
359 const struct imsm_orom
*orom
; /* platform firmware support */
360 struct intel_super
*next
; /* (temp) list for disambiguating family_num */
364 struct imsm_disk disk
;
365 #define IMSM_UNKNOWN_OWNER (-1)
367 struct intel_disk
*next
;
371 unsigned long long start
, size
;
374 /* definitions of reshape process types */
375 enum imsm_reshape_type
{
380 /* definition of messages passed to imsm_process_update */
381 enum imsm_update_type
{
382 update_activate_spare
,
386 update_add_remove_disk
,
387 update_reshape_container_disks
,
388 update_reshape_migration
,
390 update_general_migration_checkpoint
,
393 struct imsm_update_activate_spare
{
394 enum imsm_update_type type
;
398 struct imsm_update_activate_spare
*next
;
411 enum takeover_direction
{
415 struct imsm_update_takeover
{
416 enum imsm_update_type type
;
418 enum takeover_direction direction
;
421 struct imsm_update_reshape
{
422 enum imsm_update_type type
;
426 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
429 struct imsm_update_reshape_migration
{
430 enum imsm_update_type type
;
433 /* fields for array migration changes
440 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
443 struct imsm_update_general_migration_checkpoint
{
444 enum imsm_update_type type
;
445 __u32 curr_migr_unit
;
449 __u8 serial
[MAX_RAID_SERIAL_LEN
];
452 struct imsm_update_create_array
{
453 enum imsm_update_type type
;
458 struct imsm_update_kill_array
{
459 enum imsm_update_type type
;
463 struct imsm_update_rename_array
{
464 enum imsm_update_type type
;
465 __u8 name
[MAX_RAID_SERIAL_LEN
];
469 struct imsm_update_add_remove_disk
{
470 enum imsm_update_type type
;
474 static const char *_sys_dev_type
[] = {
475 [SYS_DEV_UNKNOWN
] = "Unknown",
476 [SYS_DEV_SAS
] = "SAS",
477 [SYS_DEV_SATA
] = "SATA"
480 const char *get_sys_dev_type(enum sys_dev_type type
)
482 if (type
>= SYS_DEV_MAX
)
483 type
= SYS_DEV_UNKNOWN
;
485 return _sys_dev_type
[type
];
488 static struct intel_hba
* alloc_intel_hba(struct sys_dev
*device
)
490 struct intel_hba
*result
= malloc(sizeof(*result
));
492 result
->type
= device
->type
;
493 result
->path
= strdup(device
->path
);
495 if (result
->path
&& (result
->pci_id
= strrchr(result
->path
, '/')) != NULL
)
501 static struct intel_hba
* find_intel_hba(struct intel_hba
*hba
, struct sys_dev
*device
)
503 struct intel_hba
*result
=NULL
;
504 for (result
= hba
; result
; result
= result
->next
) {
505 if (result
->type
== device
->type
&& strcmp(result
->path
, device
->path
) == 0)
511 static int attach_hba_to_super(struct intel_super
*super
, struct sys_dev
*device
)
513 struct intel_hba
*hba
;
515 /* check if disk attached to Intel HBA */
516 hba
= find_intel_hba(super
->hba
, device
);
519 /* Check if HBA is already attached to super */
520 if (super
->hba
== NULL
) {
521 super
->hba
= alloc_intel_hba(device
);
526 /* Intel metadata allows for all disks attached to the same type HBA.
527 * Do not sypport odf HBA types mixing
529 if (device
->type
!= hba
->type
)
535 hba
->next
= alloc_intel_hba(device
);
539 static struct sys_dev
* find_disk_attached_hba(int fd
, const char *devname
)
541 struct sys_dev
*list
, *elem
, *prev
;
544 if ((list
= find_intel_devices()) == NULL
)
548 disk_path
= (char *) devname
;
550 disk_path
= diskfd_to_devpath(fd
);
557 for (prev
= NULL
, elem
= list
; elem
; prev
= elem
, elem
= elem
->next
) {
558 if (path_attached_to_hba(disk_path
, elem
->path
)) {
562 prev
->next
= elem
->next
;
564 if (disk_path
!= devname
)
570 if (disk_path
!= devname
)
578 static int find_intel_hba_capability(int fd
, struct intel_super
*super
,
581 static struct supertype
*match_metadata_desc_imsm(char *arg
)
583 struct supertype
*st
;
585 if (strcmp(arg
, "imsm") != 0 &&
586 strcmp(arg
, "default") != 0
590 st
= malloc(sizeof(*st
));
593 memset(st
, 0, sizeof(*st
));
594 st
->container_dev
= NoMdDev
;
595 st
->ss
= &super_imsm
;
596 st
->max_devs
= IMSM_MAX_DEVICES
;
597 st
->minor_version
= 0;
603 static __u8
*get_imsm_version(struct imsm_super
*mpb
)
605 return &mpb
->sig
[MPB_SIG_LEN
];
609 /* retrieve a disk directly from the anchor when the anchor is known to be
610 * up-to-date, currently only at load time
612 static struct imsm_disk
*__get_imsm_disk(struct imsm_super
*mpb
, __u8 index
)
614 if (index
>= mpb
->num_disks
)
616 return &mpb
->disk
[index
];
619 /* retrieve the disk description based on a index of the disk
622 static struct dl
*get_imsm_dl_disk(struct intel_super
*super
, __u8 index
)
626 for (d
= super
->disks
; d
; d
= d
->next
)
627 if (d
->index
== index
)
632 /* retrieve a disk from the parsed metadata */
633 static struct imsm_disk
*get_imsm_disk(struct intel_super
*super
, __u8 index
)
637 dl
= get_imsm_dl_disk(super
, index
);
644 /* generate a checksum directly from the anchor when the anchor is known to be
645 * up-to-date, currently only at load or write_super after coalescing
647 static __u32
__gen_imsm_checksum(struct imsm_super
*mpb
)
649 __u32 end
= mpb
->mpb_size
/ sizeof(end
);
650 __u32
*p
= (__u32
*) mpb
;
654 sum
+= __le32_to_cpu(*p
);
658 return sum
- __le32_to_cpu(mpb
->check_sum
);
661 static size_t sizeof_imsm_map(struct imsm_map
*map
)
663 return sizeof(struct imsm_map
) + sizeof(__u32
) * (map
->num_members
- 1);
666 struct imsm_map
*get_imsm_map(struct imsm_dev
*dev
, int second_map
)
668 /* A device can have 2 maps if it is in the middle of a migration.
671 * 0 - we return the first map
673 * 1 - we return the second map if it exists, else NULL
674 * -1 - we return the second map if it exists, else the first
675 * -2 - we return longer map /excluding uninitialized state/
677 struct imsm_map
*map
= &dev
->vol
.map
[0];
678 struct imsm_map
*map2
= NULL
;
680 if (dev
->vol
.migr_state
)
681 map2
= (void *)map
+ sizeof_imsm_map(map
);
683 switch (second_map
) {
697 && map2
->map_state
!= IMSM_T_STATE_UNINITIALIZED
698 && map2
->num_members
> map
->num_members
)
708 /* return the size of the device.
709 * migr_state increases the returned size if map[0] were to be duplicated
711 static size_t sizeof_imsm_dev(struct imsm_dev
*dev
, int migr_state
)
713 size_t size
= sizeof(*dev
) - sizeof(struct imsm_map
) +
714 sizeof_imsm_map(get_imsm_map(dev
, 0));
716 /* migrating means an additional map */
717 if (dev
->vol
.migr_state
)
718 size
+= sizeof_imsm_map(get_imsm_map(dev
, 1));
720 size
+= sizeof_imsm_map(get_imsm_map(dev
, 0));
726 /* retrieve disk serial number list from a metadata update */
727 static struct disk_info
*get_disk_info(struct imsm_update_create_array
*update
)
730 struct disk_info
*inf
;
732 inf
= u
+ sizeof(*update
) - sizeof(struct imsm_dev
) +
733 sizeof_imsm_dev(&update
->dev
, 0);
739 static struct imsm_dev
*__get_imsm_dev(struct imsm_super
*mpb
, __u8 index
)
745 if (index
>= mpb
->num_raid_devs
)
748 /* devices start after all disks */
749 offset
= ((void *) &mpb
->disk
[mpb
->num_disks
]) - _mpb
;
751 for (i
= 0; i
<= index
; i
++)
753 return _mpb
+ offset
;
755 offset
+= sizeof_imsm_dev(_mpb
+ offset
, 0);
760 static struct imsm_dev
*get_imsm_dev(struct intel_super
*super
, __u8 index
)
762 struct intel_dev
*dv
;
764 if (index
>= super
->anchor
->num_raid_devs
)
766 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
767 if (dv
->index
== index
)
775 * == 1 get second map
776 * == -1 than get map according to the current migr_state
778 static __u32
get_imsm_ord_tbl_ent(struct imsm_dev
*dev
,
782 struct imsm_map
*map
;
784 map
= get_imsm_map(dev
, second_map
);
786 /* top byte identifies disk under rebuild */
787 return __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
790 #define ord_to_idx(ord) (((ord) << 8) >> 8)
791 static __u32
get_imsm_disk_idx(struct imsm_dev
*dev
, int slot
, int second_map
)
793 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, second_map
);
795 return ord_to_idx(ord
);
798 static void set_imsm_ord_tbl_ent(struct imsm_map
*map
, int slot
, __u32 ord
)
800 map
->disk_ord_tbl
[slot
] = __cpu_to_le32(ord
);
803 static int get_imsm_disk_slot(struct imsm_map
*map
, unsigned idx
)
808 for (slot
= 0; slot
< map
->num_members
; slot
++) {
809 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
810 if (ord_to_idx(ord
) == idx
)
817 static int get_imsm_raid_level(struct imsm_map
*map
)
819 if (map
->raid_level
== 1) {
820 if (map
->num_members
== 2)
826 return map
->raid_level
;
829 static int cmp_extent(const void *av
, const void *bv
)
831 const struct extent
*a
= av
;
832 const struct extent
*b
= bv
;
833 if (a
->start
< b
->start
)
835 if (a
->start
> b
->start
)
840 static int count_memberships(struct dl
*dl
, struct intel_super
*super
)
845 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
846 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
847 struct imsm_map
*map
= get_imsm_map(dev
, 0);
849 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
856 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
);
858 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
)
860 /* find a list of used extents on the given physical device */
861 struct extent
*rv
, *e
;
863 int memberships
= count_memberships(dl
, super
);
866 /* trim the reserved area for spares, so they can join any array
867 * regardless of whether the OROM has assigned sectors from the
868 * IMSM_RESERVED_SECTORS region
871 reservation
= imsm_min_reserved_sectors(super
);
873 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
875 rv
= malloc(sizeof(struct extent
) * (memberships
+ 1));
880 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
881 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
882 struct imsm_map
*map
= get_imsm_map(dev
, 0);
884 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
885 e
->start
= __le32_to_cpu(map
->pba_of_lba0
);
886 e
->size
= __le32_to_cpu(map
->blocks_per_member
);
890 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
892 /* determine the start of the metadata
893 * when no raid devices are defined use the default
894 * ...otherwise allow the metadata to truncate the value
895 * as is the case with older versions of imsm
898 struct extent
*last
= &rv
[memberships
- 1];
901 remainder
= __le32_to_cpu(dl
->disk
.total_blocks
) -
902 (last
->start
+ last
->size
);
903 /* round down to 1k block to satisfy precision of the kernel
907 /* make sure remainder is still sane */
908 if (remainder
< (unsigned)ROUND_UP(super
->len
, 512) >> 9)
909 remainder
= ROUND_UP(super
->len
, 512) >> 9;
910 if (reservation
> remainder
)
911 reservation
= remainder
;
913 e
->start
= __le32_to_cpu(dl
->disk
.total_blocks
) - reservation
;
918 /* try to determine how much space is reserved for metadata from
919 * the last get_extents() entry, otherwise fallback to the
922 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
928 /* for spares just return a minimal reservation which will grow
929 * once the spare is picked up by an array
932 return MPB_SECTOR_CNT
;
934 e
= get_extents(super
, dl
);
936 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
938 /* scroll to last entry */
939 for (i
= 0; e
[i
].size
; i
++)
942 rv
= __le32_to_cpu(dl
->disk
.total_blocks
) - e
[i
].start
;
949 static int is_spare(struct imsm_disk
*disk
)
951 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
954 static int is_configured(struct imsm_disk
*disk
)
956 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
959 static int is_failed(struct imsm_disk
*disk
)
961 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
964 /* try to determine how much space is reserved for metadata from
965 * the last get_extents() entry on the smallest active disk,
966 * otherwise fallback to the default
968 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
)
972 __u32 min_active
, remainder
;
973 __u32 rv
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
974 struct dl
*dl
, *dl_min
= NULL
;
980 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
983 if (dl
->disk
.total_blocks
< min_active
|| min_active
== 0) {
985 min_active
= dl
->disk
.total_blocks
;
991 /* find last lba used by subarrays on the smallest active disk */
992 e
= get_extents(super
, dl_min
);
995 for (i
= 0; e
[i
].size
; i
++)
998 remainder
= min_active
- e
[i
].start
;
1001 /* to give priority to recovery we should not require full
1002 IMSM_RESERVED_SECTORS from the spare */
1003 rv
= MPB_SECTOR_CNT
+ NUM_BLOCKS_DIRTY_STRIPE_REGION
;
1005 /* if real reservation is smaller use that value */
1006 return (remainder
< rv
) ? remainder
: rv
;
1009 /* Return minimum size of a spare that can be used in this array*/
1010 static unsigned long long min_acceptable_spare_size_imsm(struct supertype
*st
)
1012 struct intel_super
*super
= st
->sb
;
1016 unsigned long long rv
= 0;
1020 /* find first active disk in array */
1022 while (dl
&& (is_failed(&dl
->disk
) || dl
->index
== -1))
1026 /* find last lba used by subarrays */
1027 e
= get_extents(super
, dl
);
1030 for (i
= 0; e
[i
].size
; i
++)
1033 rv
= e
[i
-1].start
+ e
[i
-1].size
;
1036 /* add the amount of space needed for metadata */
1037 rv
= rv
+ imsm_min_reserved_sectors(super
);
1043 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
1044 struct imsm_dev
*dev
);
1046 static int is_gen_migration(struct imsm_dev
*dev
);
1048 static void print_imsm_dev(struct intel_super
*super
,
1049 struct imsm_dev
*dev
,
1055 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1056 struct imsm_map
*map2
= get_imsm_map(dev
, 1);
1060 printf("[%.16s]:\n", dev
->volume
);
1061 printf(" UUID : %s\n", uuid
);
1062 printf(" RAID Level : %d", get_imsm_raid_level(map
));
1064 printf(" <-- %d", get_imsm_raid_level(map2
));
1066 printf(" Members : %d", map
->num_members
);
1068 printf(" <-- %d", map2
->num_members
);
1070 printf(" Slots : [");
1071 for (i
= 0; i
< map
->num_members
; i
++) {
1072 ord
= get_imsm_ord_tbl_ent(dev
, i
, 0);
1073 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1078 for (i
= 0; i
< map2
->num_members
; i
++) {
1079 ord
= get_imsm_ord_tbl_ent(dev
, i
, 1);
1080 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1085 printf(" Failed disk : ");
1086 if (map
->failed_disk_num
== 0xff)
1089 printf("%i", map
->failed_disk_num
);
1091 slot
= get_imsm_disk_slot(map
, disk_idx
);
1093 ord
= get_imsm_ord_tbl_ent(dev
, slot
, -1);
1094 printf(" This Slot : %d%s\n", slot
,
1095 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
1097 printf(" This Slot : ?\n");
1098 sz
= __le32_to_cpu(dev
->size_high
);
1100 sz
+= __le32_to_cpu(dev
->size_low
);
1101 printf(" Array Size : %llu%s\n", (unsigned long long)sz
,
1102 human_size(sz
* 512));
1103 sz
= __le32_to_cpu(map
->blocks_per_member
);
1104 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz
,
1105 human_size(sz
* 512));
1106 printf(" Sector Offset : %u\n",
1107 __le32_to_cpu(map
->pba_of_lba0
));
1108 printf(" Num Stripes : %u\n",
1109 __le32_to_cpu(map
->num_data_stripes
));
1110 printf(" Chunk Size : %u KiB",
1111 __le16_to_cpu(map
->blocks_per_strip
) / 2);
1113 printf(" <-- %u KiB",
1114 __le16_to_cpu(map2
->blocks_per_strip
) / 2);
1116 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
1117 printf(" Migrate State : ");
1118 if (dev
->vol
.migr_state
) {
1119 if (migr_type(dev
) == MIGR_INIT
)
1120 printf("initialize\n");
1121 else if (migr_type(dev
) == MIGR_REBUILD
)
1122 printf("rebuild\n");
1123 else if (migr_type(dev
) == MIGR_VERIFY
)
1125 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
1126 printf("general migration\n");
1127 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
1128 printf("state change\n");
1129 else if (migr_type(dev
) == MIGR_REPAIR
)
1132 printf("<unknown:%d>\n", migr_type(dev
));
1135 printf(" Map State : %s", map_state_str
[map
->map_state
]);
1136 if (dev
->vol
.migr_state
) {
1137 struct imsm_map
*map
= get_imsm_map(dev
, 1);
1139 printf(" <-- %s", map_state_str
[map
->map_state
]);
1140 printf("\n Checkpoint : %u ",
1141 __le32_to_cpu(dev
->vol
.curr_migr_unit
));
1142 if ((is_gen_migration(dev
)) && (super
->disks
->index
> 1))
1145 printf("(%llu)", (unsigned long long)
1146 blocks_per_migr_unit(super
, dev
));
1149 printf(" Dirty State : %s\n", dev
->vol
.dirty
? "dirty" : "clean");
1152 static void print_imsm_disk(struct imsm_disk
*disk
, int index
, __u32 reserved
)
1154 char str
[MAX_RAID_SERIAL_LEN
+ 1];
1157 if (index
< -1 || !disk
)
1161 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
1163 printf(" Disk%02d Serial : %s\n", index
, str
);
1165 printf(" Disk Serial : %s\n", str
);
1166 printf(" State :%s%s%s\n", is_spare(disk
) ? " spare" : "",
1167 is_configured(disk
) ? " active" : "",
1168 is_failed(disk
) ? " failed" : "");
1169 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
1170 sz
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
1171 printf(" Usable Size : %llu%s\n", (unsigned long long)sz
,
1172 human_size(sz
* 512));
1175 void examine_migr_rec_imsm(struct intel_super
*super
)
1177 struct migr_record
*migr_rec
= super
->migr_rec
;
1178 struct imsm_super
*mpb
= super
->anchor
;
1181 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1182 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1183 if (is_gen_migration(dev
) == 0)
1186 printf("\nMigration Record Information:");
1187 if (super
->disks
->index
> 1) {
1188 printf(" Empty\n ");
1189 printf("Examine one of first two disks in array\n");
1192 printf("\n Status : ");
1193 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
1196 printf("Contains Data\n");
1197 printf(" Current Unit : %u\n",
1198 __le32_to_cpu(migr_rec
->curr_migr_unit
));
1199 printf(" Family : %u\n",
1200 __le32_to_cpu(migr_rec
->family_num
));
1201 printf(" Ascending : %u\n",
1202 __le32_to_cpu(migr_rec
->ascending_migr
));
1203 printf(" Blocks Per Unit : %u\n",
1204 __le32_to_cpu(migr_rec
->blocks_per_unit
));
1205 printf(" Dest. Depth Per Unit : %u\n",
1206 __le32_to_cpu(migr_rec
->dest_depth_per_unit
));
1207 printf(" Checkpoint Area pba : %u\n",
1208 __le32_to_cpu(migr_rec
->ckpt_area_pba
));
1209 printf(" First member lba : %u\n",
1210 __le32_to_cpu(migr_rec
->dest_1st_member_lba
));
1211 printf(" Total Number of Units : %u\n",
1212 __le32_to_cpu(migr_rec
->num_migr_units
));
1213 printf(" Size of volume : %u\n",
1214 __le32_to_cpu(migr_rec
->post_migr_vol_cap
));
1215 printf(" Expansion space for LBA64 : %u\n",
1216 __le32_to_cpu(migr_rec
->post_migr_vol_cap_hi
));
1217 printf(" Record was read from : %u\n",
1218 __le32_to_cpu(migr_rec
->ckpt_read_disk_num
));
1223 #endif /* MDASSEMBLE */
1224 /*******************************************************************************
1225 * function: imsm_check_attributes
1226 * Description: Function checks if features represented by attributes flags
1227 * are supported by mdadm.
1229 * attributes - Attributes read from metadata
1231 * 0 - passed attributes contains unsupported features flags
1232 * 1 - all features are supported
1233 ******************************************************************************/
1234 static int imsm_check_attributes(__u32 attributes
)
1237 __u32 not_supported
= MPB_ATTRIB_SUPPORTED
^0xffffffff;
1239 not_supported
&= ~MPB_ATTRIB_IGNORED
;
1241 not_supported
&= attributes
;
1242 if (not_supported
) {
1243 fprintf(stderr
, Name
"(IMSM): Unsupported attributes : %x\n",
1244 (unsigned)__le32_to_cpu(not_supported
));
1245 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1246 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
1247 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1249 if (not_supported
& MPB_ATTRIB_2TB
) {
1250 dprintf("\t\tMPB_ATTRIB_2TB\n");
1251 not_supported
^= MPB_ATTRIB_2TB
;
1253 if (not_supported
& MPB_ATTRIB_RAID0
) {
1254 dprintf("\t\tMPB_ATTRIB_RAID0\n");
1255 not_supported
^= MPB_ATTRIB_RAID0
;
1257 if (not_supported
& MPB_ATTRIB_RAID1
) {
1258 dprintf("\t\tMPB_ATTRIB_RAID1\n");
1259 not_supported
^= MPB_ATTRIB_RAID1
;
1261 if (not_supported
& MPB_ATTRIB_RAID10
) {
1262 dprintf("\t\tMPB_ATTRIB_RAID10\n");
1263 not_supported
^= MPB_ATTRIB_RAID10
;
1265 if (not_supported
& MPB_ATTRIB_RAID1E
) {
1266 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
1267 not_supported
^= MPB_ATTRIB_RAID1E
;
1269 if (not_supported
& MPB_ATTRIB_RAID5
) {
1270 dprintf("\t\tMPB_ATTRIB_RAID5\n");
1271 not_supported
^= MPB_ATTRIB_RAID5
;
1273 if (not_supported
& MPB_ATTRIB_RAIDCNG
) {
1274 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
1275 not_supported
^= MPB_ATTRIB_RAIDCNG
;
1277 if (not_supported
& MPB_ATTRIB_BBM
) {
1278 dprintf("\t\tMPB_ATTRIB_BBM\n");
1279 not_supported
^= MPB_ATTRIB_BBM
;
1281 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1282 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
1283 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1285 if (not_supported
& MPB_ATTRIB_EXP_STRIPE_SIZE
) {
1286 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
1287 not_supported
^= MPB_ATTRIB_EXP_STRIPE_SIZE
;
1289 if (not_supported
& MPB_ATTRIB_2TB_DISK
) {
1290 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
1291 not_supported
^= MPB_ATTRIB_2TB_DISK
;
1293 if (not_supported
& MPB_ATTRIB_NEVER_USE2
) {
1294 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
1295 not_supported
^= MPB_ATTRIB_NEVER_USE2
;
1297 if (not_supported
& MPB_ATTRIB_NEVER_USE
) {
1298 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
1299 not_supported
^= MPB_ATTRIB_NEVER_USE
;
1303 dprintf(Name
"(IMSM): Unknown attributes : %x\n", not_supported
);
1312 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
1314 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
1316 struct intel_super
*super
= st
->sb
;
1317 struct imsm_super
*mpb
= super
->anchor
;
1318 char str
[MAX_SIGNATURE_LENGTH
];
1323 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
1326 snprintf(str
, MPB_SIG_LEN
, "%s", mpb
->sig
);
1327 printf(" Magic : %s\n", str
);
1328 snprintf(str
, strlen(MPB_VERSION_RAID0
), "%s", get_imsm_version(mpb
));
1329 printf(" Version : %s\n", get_imsm_version(mpb
));
1330 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
1331 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
1332 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
1333 printf(" Attributes : ");
1334 if (imsm_check_attributes(mpb
->attributes
))
1335 printf("All supported\n");
1337 printf("not supported\n");
1338 getinfo_super_imsm(st
, &info
, NULL
);
1339 fname_from_uuid(st
, &info
, nbuf
, ':');
1340 printf(" UUID : %s\n", nbuf
+ 5);
1341 sum
= __le32_to_cpu(mpb
->check_sum
);
1342 printf(" Checksum : %08x %s\n", sum
,
1343 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
1344 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
));
1345 printf(" Disks : %d\n", mpb
->num_disks
);
1346 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
1347 print_imsm_disk(__get_imsm_disk(mpb
, super
->disks
->index
), super
->disks
->index
, reserved
);
1348 if (super
->bbm_log
) {
1349 struct bbm_log
*log
= super
->bbm_log
;
1352 printf("Bad Block Management Log:\n");
1353 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
1354 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
1355 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
1356 printf(" Spare Blocks : %d\n", __le32_to_cpu(log
->reserved_spare_block_count
));
1357 printf(" First Spare : %llx\n",
1358 (unsigned long long) __le64_to_cpu(log
->first_spare_lba
));
1360 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1362 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1364 super
->current_vol
= i
;
1365 getinfo_super_imsm(st
, &info
, NULL
);
1366 fname_from_uuid(st
, &info
, nbuf
, ':');
1367 print_imsm_dev(super
, dev
, nbuf
+ 5, super
->disks
->index
);
1369 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1370 if (i
== super
->disks
->index
)
1372 print_imsm_disk(__get_imsm_disk(mpb
, i
), i
, reserved
);
1375 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
1376 if (dl
->index
== -1)
1377 print_imsm_disk(&dl
->disk
, -1, reserved
);
1379 examine_migr_rec_imsm(super
);
1382 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
1384 /* We just write a generic IMSM ARRAY entry */
1387 struct intel_super
*super
= st
->sb
;
1389 if (!super
->anchor
->num_raid_devs
) {
1390 printf("ARRAY metadata=imsm\n");
1394 getinfo_super_imsm(st
, &info
, NULL
);
1395 fname_from_uuid(st
, &info
, nbuf
, ':');
1396 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
1399 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
1401 /* We just write a generic IMSM ARRAY entry */
1405 struct intel_super
*super
= st
->sb
;
1408 if (!super
->anchor
->num_raid_devs
)
1411 getinfo_super_imsm(st
, &info
, NULL
);
1412 fname_from_uuid(st
, &info
, nbuf
, ':');
1413 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1414 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1416 super
->current_vol
= i
;
1417 getinfo_super_imsm(st
, &info
, NULL
);
1418 fname_from_uuid(st
, &info
, nbuf1
, ':');
1419 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
1420 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
1424 static void export_examine_super_imsm(struct supertype
*st
)
1426 struct intel_super
*super
= st
->sb
;
1427 struct imsm_super
*mpb
= super
->anchor
;
1431 getinfo_super_imsm(st
, &info
, NULL
);
1432 fname_from_uuid(st
, &info
, nbuf
, ':');
1433 printf("MD_METADATA=imsm\n");
1434 printf("MD_LEVEL=container\n");
1435 printf("MD_UUID=%s\n", nbuf
+5);
1436 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
1439 static void detail_super_imsm(struct supertype
*st
, char *homehost
)
1444 getinfo_super_imsm(st
, &info
, NULL
);
1445 fname_from_uuid(st
, &info
, nbuf
, ':');
1446 printf("\n UUID : %s\n", nbuf
+ 5);
1449 static void brief_detail_super_imsm(struct supertype
*st
)
1453 getinfo_super_imsm(st
, &info
, NULL
);
1454 fname_from_uuid(st
, &info
, nbuf
, ':');
1455 printf(" UUID=%s", nbuf
+ 5);
1458 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
);
1459 static void fd2devname(int fd
, char *name
);
1461 static int ahci_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
1463 /* dump an unsorted list of devices attached to AHCI Intel storage
1464 * controller, as well as non-connected ports
1466 int hba_len
= strlen(hba_path
) + 1;
1471 unsigned long port_mask
= (1 << port_count
) - 1;
1473 if (port_count
> (int)sizeof(port_mask
) * 8) {
1475 fprintf(stderr
, Name
": port_count %d out of range\n", port_count
);
1479 /* scroll through /sys/dev/block looking for devices attached to
1482 dir
= opendir("/sys/dev/block");
1483 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
1494 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
1496 path
= devt_to_devpath(makedev(major
, minor
));
1499 if (!path_attached_to_hba(path
, hba_path
)) {
1505 /* retrieve the scsi device type */
1506 if (asprintf(&device
, "/sys/dev/block/%d:%d/device/xxxxxxx", major
, minor
) < 0) {
1508 fprintf(stderr
, Name
": failed to allocate 'device'\n");
1512 sprintf(device
, "/sys/dev/block/%d:%d/device/type", major
, minor
);
1513 if (load_sys(device
, buf
) != 0) {
1515 fprintf(stderr
, Name
": failed to read device type for %s\n",
1521 type
= strtoul(buf
, NULL
, 10);
1523 /* if it's not a disk print the vendor and model */
1524 if (!(type
== 0 || type
== 7 || type
== 14)) {
1527 sprintf(device
, "/sys/dev/block/%d:%d/device/vendor", major
, minor
);
1528 if (load_sys(device
, buf
) == 0) {
1529 strncpy(vendor
, buf
, sizeof(vendor
));
1530 vendor
[sizeof(vendor
) - 1] = '\0';
1531 c
= (char *) &vendor
[sizeof(vendor
) - 1];
1532 while (isspace(*c
) || *c
== '\0')
1536 sprintf(device
, "/sys/dev/block/%d:%d/device/model", major
, minor
);
1537 if (load_sys(device
, buf
) == 0) {
1538 strncpy(model
, buf
, sizeof(model
));
1539 model
[sizeof(model
) - 1] = '\0';
1540 c
= (char *) &model
[sizeof(model
) - 1];
1541 while (isspace(*c
) || *c
== '\0')
1545 if (vendor
[0] && model
[0])
1546 sprintf(buf
, "%.64s %.64s", vendor
, model
);
1548 switch (type
) { /* numbers from hald/linux/device.c */
1549 case 1: sprintf(buf
, "tape"); break;
1550 case 2: sprintf(buf
, "printer"); break;
1551 case 3: sprintf(buf
, "processor"); break;
1553 case 5: sprintf(buf
, "cdrom"); break;
1554 case 6: sprintf(buf
, "scanner"); break;
1555 case 8: sprintf(buf
, "media_changer"); break;
1556 case 9: sprintf(buf
, "comm"); break;
1557 case 12: sprintf(buf
, "raid"); break;
1558 default: sprintf(buf
, "unknown");
1564 /* chop device path to 'host%d' and calculate the port number */
1565 c
= strchr(&path
[hba_len
], '/');
1568 fprintf(stderr
, Name
": %s - invalid path name\n", path
+ hba_len
);
1573 if (sscanf(&path
[hba_len
], "host%d", &port
) == 1)
1577 *c
= '/'; /* repair the full string */
1578 fprintf(stderr
, Name
": failed to determine port number for %s\n",
1585 /* mark this port as used */
1586 port_mask
&= ~(1 << port
);
1588 /* print out the device information */
1590 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
1594 fd
= dev_open(ent
->d_name
, O_RDONLY
);
1596 printf(" Port%d : - disk info unavailable -\n", port
);
1598 fd2devname(fd
, buf
);
1599 printf(" Port%d : %s", port
, buf
);
1600 if (imsm_read_serial(fd
, NULL
, (__u8
*) buf
) == 0)
1601 printf(" (%.*s)\n", MAX_RAID_SERIAL_LEN
, buf
);
1616 for (i
= 0; i
< port_count
; i
++)
1617 if (port_mask
& (1 << i
))
1618 printf(" Port%d : - no device attached -\n", i
);
1624 static void print_found_intel_controllers(struct sys_dev
*elem
)
1626 for (; elem
; elem
= elem
->next
) {
1627 fprintf(stderr
, Name
": found Intel(R) ");
1628 if (elem
->type
== SYS_DEV_SATA
)
1629 fprintf(stderr
, "SATA ");
1630 else if (elem
->type
== SYS_DEV_SAS
)
1631 fprintf(stderr
, "SAS ");
1632 fprintf(stderr
, "RAID controller");
1634 fprintf(stderr
, " at %s", elem
->pci_id
);
1635 fprintf(stderr
, ".\n");
1640 static int ahci_get_port_count(const char *hba_path
, int *port_count
)
1647 if ((dir
= opendir(hba_path
)) == NULL
)
1650 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
1653 if (sscanf(ent
->d_name
, "host%d", &host
) != 1)
1655 if (*port_count
== 0)
1657 else if (host
< host_base
)
1660 if (host
+ 1 > *port_count
+ host_base
)
1661 *port_count
= host
+ 1 - host_base
;
1667 static void print_imsm_capability(const struct imsm_orom
*orom
)
1669 printf(" Platform : Intel(R) Matrix Storage Manager\n");
1670 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
1671 orom
->hotfix_ver
, orom
->build
);
1672 printf(" RAID Levels :%s%s%s%s%s\n",
1673 imsm_orom_has_raid0(orom
) ? " raid0" : "",
1674 imsm_orom_has_raid1(orom
) ? " raid1" : "",
1675 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
1676 imsm_orom_has_raid10(orom
) ? " raid10" : "",
1677 imsm_orom_has_raid5(orom
) ? " raid5" : "");
1678 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1679 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
1680 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
1681 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
1682 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
1683 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
1684 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
1685 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
1686 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
1687 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
1688 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
1689 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
1690 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
1691 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
1692 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
1693 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
1694 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
1695 printf(" Max Disks : %d\n", orom
->tds
);
1696 printf(" Max Volumes : %d\n", orom
->vpa
);
1700 static int detail_platform_imsm(int verbose
, int enumerate_only
)
1702 /* There are two components to imsm platform support, the ahci SATA
1703 * controller and the option-rom. To find the SATA controller we
1704 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
1705 * controller with the Intel vendor id is present. This approach
1706 * allows mdadm to leverage the kernel's ahci detection logic, with the
1707 * caveat that if ahci.ko is not loaded mdadm will not be able to
1708 * detect platform raid capabilities. The option-rom resides in a
1709 * platform "Adapter ROM". We scan for its signature to retrieve the
1710 * platform capabilities. If raid support is disabled in the BIOS the
1711 * option-rom capability structure will not be available.
1713 const struct imsm_orom
*orom
;
1714 struct sys_dev
*list
, *hba
;
1719 if (enumerate_only
) {
1720 if (check_env("IMSM_NO_PLATFORM"))
1722 list
= find_intel_devices();
1725 for (hba
= list
; hba
; hba
= hba
->next
) {
1726 orom
= find_imsm_capability(hba
->type
);
1732 free_sys_dev(&list
);
1736 list
= find_intel_devices();
1739 fprintf(stderr
, Name
": no active Intel(R) RAID "
1740 "controller found.\n");
1741 free_sys_dev(&list
);
1744 print_found_intel_controllers(list
);
1746 for (hba
= list
; hba
; hba
= hba
->next
) {
1747 orom
= find_imsm_capability(hba
->type
);
1749 fprintf(stderr
, Name
": imsm capabilities not found for controller: %s (type %s)\n",
1750 hba
->path
, get_sys_dev_type(hba
->type
));
1752 print_imsm_capability(orom
);
1755 for (hba
= list
; hba
; hba
= hba
->next
) {
1756 printf(" I/O Controller : %s (%s)\n",
1757 hba
->path
, get_sys_dev_type(hba
->type
));
1759 if (hba
->type
== SYS_DEV_SATA
) {
1760 host_base
= ahci_get_port_count(hba
->path
, &port_count
);
1761 if (ahci_enumerate_ports(hba
->path
, port_count
, host_base
, verbose
)) {
1763 fprintf(stderr
, Name
": failed to enumerate "
1764 "ports on SATA controller at %s.", hba
->pci_id
);
1770 free_sys_dev(&list
);
1775 static int match_home_imsm(struct supertype
*st
, char *homehost
)
1777 /* the imsm metadata format does not specify any host
1778 * identification information. We return -1 since we can never
1779 * confirm nor deny whether a given array is "meant" for this
1780 * host. We rely on compare_super and the 'family_num' fields to
1781 * exclude member disks that do not belong, and we rely on
1782 * mdadm.conf to specify the arrays that should be assembled.
1783 * Auto-assembly may still pick up "foreign" arrays.
1789 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
1791 /* The uuid returned here is used for:
1792 * uuid to put into bitmap file (Create, Grow)
1793 * uuid for backup header when saving critical section (Grow)
1794 * comparing uuids when re-adding a device into an array
1795 * In these cases the uuid required is that of the data-array,
1796 * not the device-set.
1797 * uuid to recognise same set when adding a missing device back
1798 * to an array. This is a uuid for the device-set.
1800 * For each of these we can make do with a truncated
1801 * or hashed uuid rather than the original, as long as
1803 * In each case the uuid required is that of the data-array,
1804 * not the device-set.
1806 /* imsm does not track uuid's so we synthesis one using sha1 on
1807 * - The signature (Which is constant for all imsm array, but no matter)
1808 * - the orig_family_num of the container
1809 * - the index number of the volume
1810 * - the 'serial' number of the volume.
1811 * Hopefully these are all constant.
1813 struct intel_super
*super
= st
->sb
;
1816 struct sha1_ctx ctx
;
1817 struct imsm_dev
*dev
= NULL
;
1820 /* some mdadm versions failed to set ->orig_family_num, in which
1821 * case fall back to ->family_num. orig_family_num will be
1822 * fixed up with the first metadata update.
1824 family_num
= super
->anchor
->orig_family_num
;
1825 if (family_num
== 0)
1826 family_num
= super
->anchor
->family_num
;
1827 sha1_init_ctx(&ctx
);
1828 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
1829 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
1830 if (super
->current_vol
>= 0)
1831 dev
= get_imsm_dev(super
, super
->current_vol
);
1833 __u32 vol
= super
->current_vol
;
1834 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
1835 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
1837 sha1_finish_ctx(&ctx
, buf
);
1838 memcpy(uuid
, buf
, 4*4);
1843 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
1845 __u8
*v
= get_imsm_version(mpb
);
1846 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
1847 char major
[] = { 0, 0, 0 };
1848 char minor
[] = { 0 ,0, 0 };
1849 char patch
[] = { 0, 0, 0 };
1850 char *ver_parse
[] = { major
, minor
, patch
};
1854 while (*v
!= '\0' && v
< end
) {
1855 if (*v
!= '.' && j
< 2)
1856 ver_parse
[i
][j
++] = *v
;
1864 *m
= strtol(minor
, NULL
, 0);
1865 *p
= strtol(patch
, NULL
, 0);
1869 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
1871 /* migr_strip_size when repairing or initializing parity */
1872 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1873 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1875 switch (get_imsm_raid_level(map
)) {
1880 return 128*1024 >> 9;
1884 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
1886 /* migr_strip_size when rebuilding a degraded disk, no idea why
1887 * this is different than migr_strip_size_resync(), but it's good
1890 struct imsm_map
*map
= get_imsm_map(dev
, 1);
1891 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1893 switch (get_imsm_raid_level(map
)) {
1896 if (map
->num_members
% map
->num_domains
== 0)
1897 return 128*1024 >> 9;
1901 return max((__u32
) 64*1024 >> 9, chunk
);
1903 return 128*1024 >> 9;
1907 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
1909 struct imsm_map
*lo
= get_imsm_map(dev
, 0);
1910 struct imsm_map
*hi
= get_imsm_map(dev
, 1);
1911 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
1912 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
1914 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
1917 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
1919 struct imsm_map
*lo
= get_imsm_map(dev
, 0);
1920 int level
= get_imsm_raid_level(lo
);
1922 if (level
== 1 || level
== 10) {
1923 struct imsm_map
*hi
= get_imsm_map(dev
, 1);
1925 return hi
->num_domains
;
1927 return num_stripes_per_unit_resync(dev
);
1930 static __u8
imsm_num_data_members(struct imsm_dev
*dev
, int second_map
)
1932 /* named 'imsm_' because raid0, raid1 and raid10
1933 * counter-intuitively have the same number of data disks
1935 struct imsm_map
*map
= get_imsm_map(dev
, second_map
);
1937 switch (get_imsm_raid_level(map
)) {
1941 return map
->num_members
;
1943 return map
->num_members
- 1;
1945 dprintf("%s: unsupported raid level\n", __func__
);
1950 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
1952 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1953 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1955 switch(get_imsm_raid_level(map
)) {
1958 return chunk
* map
->num_domains
;
1960 return chunk
* map
->num_members
;
1966 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
1968 struct imsm_map
*map
= get_imsm_map(dev
, 1);
1969 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1970 __u32 strip
= block
/ chunk
;
1972 switch (get_imsm_raid_level(map
)) {
1975 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
1976 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
1978 return vol_stripe
* chunk
+ block
% chunk
;
1980 __u32 stripe
= strip
/ (map
->num_members
- 1);
1982 return stripe
* chunk
+ block
% chunk
;
1989 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
1990 struct imsm_dev
*dev
)
1992 /* calculate the conversion factor between per member 'blocks'
1993 * (md/{resync,rebuild}_start) and imsm migration units, return
1994 * 0 for the 'not migrating' and 'unsupported migration' cases
1996 if (!dev
->vol
.migr_state
)
1999 switch (migr_type(dev
)) {
2000 case MIGR_GEN_MIGR
: {
2001 struct migr_record
*migr_rec
= super
->migr_rec
;
2002 return __le32_to_cpu(migr_rec
->blocks_per_unit
);
2007 struct imsm_map
*map
= get_imsm_map(dev
, 0);
2008 __u32 stripes_per_unit
;
2009 __u32 blocks_per_unit
;
2018 /* yes, this is really the translation of migr_units to
2019 * per-member blocks in the 'resync' case
2021 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
2022 migr_chunk
= migr_strip_blocks_resync(dev
);
2023 disks
= imsm_num_data_members(dev
, 0);
2024 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
2025 stripe
= __le16_to_cpu(map
->blocks_per_strip
) * disks
;
2026 segment
= blocks_per_unit
/ stripe
;
2027 block_rel
= blocks_per_unit
- segment
* stripe
;
2028 parity_depth
= parity_segment_depth(dev
);
2029 block_map
= map_migr_block(dev
, block_rel
);
2030 return block_map
+ parity_depth
* segment
;
2032 case MIGR_REBUILD
: {
2033 __u32 stripes_per_unit
;
2036 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
2037 migr_chunk
= migr_strip_blocks_rebuild(dev
);
2038 return migr_chunk
* stripes_per_unit
;
2040 case MIGR_STATE_CHANGE
:
2046 static int imsm_level_to_layout(int level
)
2054 return ALGORITHM_LEFT_ASYMMETRIC
;
2061 /*******************************************************************************
2062 * Function: read_imsm_migr_rec
2063 * Description: Function reads imsm migration record from last sector of disk
2065 * fd : disk descriptor
2066 * super : metadata info
2070 ******************************************************************************/
2071 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
)
2074 unsigned long long dsize
;
2076 get_dev_size(fd
, NULL
, &dsize
);
2077 if (lseek64(fd
, dsize
- 512, SEEK_SET
) < 0) {
2079 Name
": Cannot seek to anchor block: %s\n",
2083 if (read(fd
, super
->migr_rec_buf
, 512) != 512) {
2085 Name
": Cannot read migr record block: %s\n",
2095 /*******************************************************************************
2096 * Function: load_imsm_migr_rec
2097 * Description: Function reads imsm migration record (it is stored at the last
2100 * super : imsm internal array info
2101 * info : general array info
2105 ******************************************************************************/
2106 static int load_imsm_migr_rec(struct intel_super
*super
, struct mdinfo
*info
)
2109 struct dl
*dl
= NULL
;
2115 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
2116 /* read only from one of the first two slots */
2117 if ((sd
->disk
.raid_disk
> 1) ||
2118 (sd
->disk
.raid_disk
< 0))
2120 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
2121 fd
= dev_open(nm
, O_RDONLY
);
2127 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
2128 /* read only from one of the first two slots */
2131 sprintf(nm
, "%d:%d", dl
->major
, dl
->minor
);
2132 fd
= dev_open(nm
, O_RDONLY
);
2139 retval
= read_imsm_migr_rec(fd
, super
);
2148 /*******************************************************************************
2149 * function: imsm_create_metadata_checkpoint_update
2150 * Description: It creates update for checkpoint change.
2152 * super : imsm internal array info
2153 * u : pointer to prepared update
2156 * If length is equal to 0, input pointer u contains no update
2157 ******************************************************************************/
2158 static int imsm_create_metadata_checkpoint_update(
2159 struct intel_super
*super
,
2160 struct imsm_update_general_migration_checkpoint
**u
)
2163 int update_memory_size
= 0;
2165 dprintf("imsm_create_metadata_checkpoint_update(enter)\n");
2171 /* size of all update data without anchor */
2172 update_memory_size
=
2173 sizeof(struct imsm_update_general_migration_checkpoint
);
2175 *u
= calloc(1, update_memory_size
);
2177 dprintf("error: cannot get memory for "
2178 "imsm_create_metadata_checkpoint_update update\n");
2181 (*u
)->type
= update_general_migration_checkpoint
;
2182 (*u
)->curr_migr_unit
= __le32_to_cpu(super
->migr_rec
->curr_migr_unit
);
2183 dprintf("imsm_create_metadata_checkpoint_update: prepared for %u\n",
2184 (*u
)->curr_migr_unit
);
2186 return update_memory_size
;
2190 static void imsm_update_metadata_locally(struct supertype
*st
,
2191 void *buf
, int len
);
2193 /*******************************************************************************
2194 * Function: write_imsm_migr_rec
2195 * Description: Function writes imsm migration record
2196 * (at the last sector of disk)
2198 * super : imsm internal array info
2202 ******************************************************************************/
2203 static int write_imsm_migr_rec(struct supertype
*st
)
2205 struct intel_super
*super
= st
->sb
;
2206 unsigned long long dsize
;
2212 struct imsm_update_general_migration_checkpoint
*u
;
2214 for (sd
= super
->disks
; sd
; sd
= sd
->next
) {
2215 /* write to 2 first slots only */
2216 if ((sd
->index
< 0) || (sd
->index
> 1))
2218 sprintf(nm
, "%d:%d", sd
->major
, sd
->minor
);
2219 fd
= dev_open(nm
, O_RDWR
);
2222 get_dev_size(fd
, NULL
, &dsize
);
2223 if (lseek64(fd
, dsize
- 512, SEEK_SET
) < 0) {
2225 Name
": Cannot seek to anchor block: %s\n",
2229 if (write(fd
, super
->migr_rec_buf
, 512) != 512) {
2231 Name
": Cannot write migr record block: %s\n",
2238 /* update checkpoint information in metadata */
2239 len
= imsm_create_metadata_checkpoint_update(super
, &u
);
2242 dprintf("imsm: Cannot prepare update\n");
2245 /* update metadata locally */
2246 imsm_update_metadata_locally(st
, u
, len
);
2247 /* and possibly remotely */
2248 if (st
->update_tail
) {
2249 append_metadata_update(st
, u
, len
);
2250 /* during reshape we do all work inside metadata handler
2251 * manage_reshape(), so metadata update has to be triggered
2254 flush_metadata_updates(st
);
2255 st
->update_tail
= &st
->updates
;
2265 #endif /* MDASSEMBLE */
2267 /* spare/missing disks activations are not allowe when
2268 * array/container performs reshape operation, because
2269 * all arrays in container works on the same disks set
2271 int imsm_reshape_blocks_arrays_changes(struct intel_super
*super
)
2274 struct intel_dev
*i_dev
;
2275 struct imsm_dev
*dev
;
2277 /* check whole container
2279 for (i_dev
= super
->devlist
; i_dev
; i_dev
= i_dev
->next
) {
2281 if (is_gen_migration(dev
)) {
2282 /* No repair during any migration in container
2291 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
2293 struct intel_super
*super
= st
->sb
;
2294 struct migr_record
*migr_rec
= super
->migr_rec
;
2295 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
2296 struct imsm_map
*map
= get_imsm_map(dev
, 0);
2297 struct imsm_map
*prev_map
= get_imsm_map(dev
, 1);
2298 struct imsm_map
*map_to_analyse
= map
;
2301 unsigned int component_size_alligment
;
2302 int map_disks
= info
->array
.raid_disks
;
2304 memset(info
, 0, sizeof(*info
));
2306 map_to_analyse
= prev_map
;
2308 dl
= super
->current_disk
;
2310 info
->container_member
= super
->current_vol
;
2311 info
->array
.raid_disks
= map
->num_members
;
2312 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
2313 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
2314 info
->array
.md_minor
= -1;
2315 info
->array
.ctime
= 0;
2316 info
->array
.utime
= 0;
2317 info
->array
.chunk_size
=
2318 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
2319 info
->array
.state
= !dev
->vol
.dirty
;
2320 info
->custom_array_size
= __le32_to_cpu(dev
->size_high
);
2321 info
->custom_array_size
<<= 32;
2322 info
->custom_array_size
|= __le32_to_cpu(dev
->size_low
);
2323 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
2325 if (is_gen_migration(dev
)) {
2326 info
->reshape_active
= 1;
2327 info
->new_level
= get_imsm_raid_level(map
);
2328 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
2329 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
2330 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
2331 if (info
->delta_disks
) {
2332 /* this needs to be applied to every array
2335 info
->reshape_active
= CONTAINER_RESHAPE
;
2337 /* We shape information that we give to md might have to be
2338 * modify to cope with md's requirement for reshaping arrays.
2339 * For example, when reshaping a RAID0, md requires it to be
2340 * presented as a degraded RAID4.
2341 * Also if a RAID0 is migrating to a RAID5 we need to specify
2342 * the array as already being RAID5, but the 'before' layout
2343 * is a RAID4-like layout.
2345 switch (info
->array
.level
) {
2347 switch(info
->new_level
) {
2349 /* conversion is happening as RAID4 */
2350 info
->array
.level
= 4;
2351 info
->array
.raid_disks
+= 1;
2354 /* conversion is happening as RAID5 */
2355 info
->array
.level
= 5;
2356 info
->array
.layout
= ALGORITHM_PARITY_N
;
2357 info
->delta_disks
-= 1;
2360 /* FIXME error message */
2361 info
->array
.level
= UnSet
;
2367 info
->new_level
= UnSet
;
2368 info
->new_layout
= UnSet
;
2369 info
->new_chunk
= info
->array
.chunk_size
;
2370 info
->delta_disks
= 0;
2374 info
->disk
.major
= dl
->major
;
2375 info
->disk
.minor
= dl
->minor
;
2376 info
->disk
.number
= dl
->index
;
2377 info
->disk
.raid_disk
= get_imsm_disk_slot(map_to_analyse
,
2381 info
->data_offset
= __le32_to_cpu(map_to_analyse
->pba_of_lba0
);
2382 info
->component_size
=
2383 __le32_to_cpu(map_to_analyse
->blocks_per_member
);
2385 /* check component size aligment
2387 component_size_alligment
=
2388 info
->component_size
% (info
->array
.chunk_size
/512);
2390 if (component_size_alligment
&&
2391 (info
->array
.level
!= 1) && (info
->array
.level
!= UnSet
)) {
2392 dprintf("imsm: reported component size alligned from %llu ",
2393 info
->component_size
);
2394 info
->component_size
-= component_size_alligment
;
2395 dprintf("to %llu (%i).\n",
2396 info
->component_size
, component_size_alligment
);
2399 memset(info
->uuid
, 0, sizeof(info
->uuid
));
2400 info
->recovery_start
= MaxSector
;
2402 info
->reshape_progress
= 0;
2403 info
->resync_start
= MaxSector
;
2404 if ((map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
2406 imsm_reshape_blocks_arrays_changes(super
) == 0) {
2407 info
->resync_start
= 0;
2409 if (dev
->vol
.migr_state
) {
2410 switch (migr_type(dev
)) {
2413 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2415 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
2417 info
->resync_start
= blocks_per_unit
* units
;
2420 case MIGR_GEN_MIGR
: {
2421 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2423 __u64 units
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
2424 unsigned long long array_blocks
;
2427 if (__le32_to_cpu(migr_rec
->ascending_migr
) &&
2429 (__le32_to_cpu(migr_rec
->num_migr_units
)-1)) &&
2430 (super
->migr_rec
->rec_status
==
2431 __cpu_to_le32(UNIT_SRC_IN_CP_AREA
)))
2434 info
->reshape_progress
= blocks_per_unit
* units
;
2436 dprintf("IMSM: General Migration checkpoint : %llu "
2437 "(%llu) -> read reshape progress : %llu\n",
2438 (unsigned long long)units
,
2439 (unsigned long long)blocks_per_unit
,
2440 info
->reshape_progress
);
2442 used_disks
= imsm_num_data_members(dev
, 1);
2443 if (used_disks
> 0) {
2444 array_blocks
= map
->blocks_per_member
*
2446 /* round array size down to closest MB
2448 info
->custom_array_size
= (array_blocks
2449 >> SECT_PER_MB_SHIFT
)
2450 << SECT_PER_MB_SHIFT
;
2454 /* we could emulate the checkpointing of
2455 * 'sync_action=check' migrations, but for now
2456 * we just immediately complete them
2459 /* this is handled by container_content_imsm() */
2460 case MIGR_STATE_CHANGE
:
2461 /* FIXME handle other migrations */
2463 /* we are not dirty, so... */
2464 info
->resync_start
= MaxSector
;
2468 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
2469 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
2471 info
->array
.major_version
= -1;
2472 info
->array
.minor_version
= -2;
2473 devname
= devnum2devname(st
->container_dev
);
2474 *info
->text_version
= '\0';
2476 sprintf(info
->text_version
, "/%s/%d", devname
, info
->container_member
);
2478 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
2479 uuid_from_super_imsm(st
, info
->uuid
);
2483 for (i
=0; i
<map_disks
; i
++) {
2485 if (i
< info
->array
.raid_disks
) {
2486 struct imsm_disk
*dsk
;
2487 j
= get_imsm_disk_idx(dev
, i
, -1);
2488 dsk
= get_imsm_disk(super
, j
);
2489 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
2496 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
2497 int failed
, int look_in_map
);
2499 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
2502 static void manage_second_map(struct intel_super
*super
, struct imsm_dev
*dev
)
2504 if (is_gen_migration(dev
)) {
2507 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
2509 failed
= imsm_count_failed(super
, dev
, MAP_1
);
2510 map_state
= imsm_check_degraded(super
, dev
, failed
,
2512 if (map2
->map_state
!= map_state
) {
2513 map2
->map_state
= map_state
;
2514 super
->updates_pending
++;
2519 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
2523 for (d
= super
->missing
; d
; d
= d
->next
)
2524 if (d
->index
== index
)
2529 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
2531 struct intel_super
*super
= st
->sb
;
2532 struct imsm_disk
*disk
;
2533 int map_disks
= info
->array
.raid_disks
;
2534 int max_enough
= -1;
2536 struct imsm_super
*mpb
;
2538 if (super
->current_vol
>= 0) {
2539 getinfo_super_imsm_volume(st
, info
, map
);
2542 memset(info
, 0, sizeof(*info
));
2544 /* Set raid_disks to zero so that Assemble will always pull in valid
2547 info
->array
.raid_disks
= 0;
2548 info
->array
.level
= LEVEL_CONTAINER
;
2549 info
->array
.layout
= 0;
2550 info
->array
.md_minor
= -1;
2551 info
->array
.ctime
= 0; /* N/A for imsm */
2552 info
->array
.utime
= 0;
2553 info
->array
.chunk_size
= 0;
2555 info
->disk
.major
= 0;
2556 info
->disk
.minor
= 0;
2557 info
->disk
.raid_disk
= -1;
2558 info
->reshape_active
= 0;
2559 info
->array
.major_version
= -1;
2560 info
->array
.minor_version
= -2;
2561 strcpy(info
->text_version
, "imsm");
2562 info
->safe_mode_delay
= 0;
2563 info
->disk
.number
= -1;
2564 info
->disk
.state
= 0;
2566 info
->recovery_start
= MaxSector
;
2567 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
2569 /* do we have the all the insync disks that we expect? */
2570 mpb
= super
->anchor
;
2572 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
2573 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
2574 int failed
, enough
, j
, missing
= 0;
2575 struct imsm_map
*map
;
2578 failed
= imsm_count_failed(super
, dev
, MAP_0
);
2579 state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
2580 map
= get_imsm_map(dev
, 0);
2582 /* any newly missing disks?
2583 * (catches single-degraded vs double-degraded)
2585 for (j
= 0; j
< map
->num_members
; j
++) {
2586 __u32 ord
= get_imsm_ord_tbl_ent(dev
, j
, 0);
2587 __u32 idx
= ord_to_idx(ord
);
2589 if (!(ord
& IMSM_ORD_REBUILD
) &&
2590 get_imsm_missing(super
, idx
)) {
2596 if (state
== IMSM_T_STATE_FAILED
)
2598 else if (state
== IMSM_T_STATE_DEGRADED
&&
2599 (state
!= map
->map_state
|| missing
))
2601 else /* we're normal, or already degraded */
2604 /* in the missing/failed disk case check to see
2605 * if at least one array is runnable
2607 max_enough
= max(max_enough
, enough
);
2609 dprintf("%s: enough: %d\n", __func__
, max_enough
);
2610 info
->container_enough
= max_enough
;
2613 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
2615 disk
= &super
->disks
->disk
;
2616 info
->data_offset
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
2617 info
->component_size
= reserved
;
2618 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
2619 /* we don't change info->disk.raid_disk here because
2620 * this state will be finalized in mdmon after we have
2621 * found the 'most fresh' version of the metadata
2623 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
2624 info
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
2627 /* only call uuid_from_super_imsm when this disk is part of a populated container,
2628 * ->compare_super may have updated the 'num_raid_devs' field for spares
2630 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
2631 uuid_from_super_imsm(st
, info
->uuid
);
2633 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
2635 /* I don't know how to compute 'map' on imsm, so use safe default */
2638 for (i
= 0; i
< map_disks
; i
++)
2644 /* allocates memory and fills disk in mdinfo structure
2645 * for each disk in array */
2646 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
2648 struct mdinfo
*mddev
= NULL
;
2649 struct intel_super
*super
= st
->sb
;
2650 struct imsm_disk
*disk
;
2653 if (!super
|| !super
->disks
)
2656 mddev
= malloc(sizeof(*mddev
));
2658 fprintf(stderr
, Name
": Failed to allocate memory.\n");
2661 memset(mddev
, 0, sizeof(*mddev
));
2665 tmp
= malloc(sizeof(*tmp
));
2667 fprintf(stderr
, Name
": Failed to allocate memory.\n");
2672 memset(tmp
, 0, sizeof(*tmp
));
2674 tmp
->next
= mddev
->devs
;
2676 tmp
->disk
.number
= count
++;
2677 tmp
->disk
.major
= dl
->major
;
2678 tmp
->disk
.minor
= dl
->minor
;
2679 tmp
->disk
.state
= is_configured(disk
) ?
2680 (1 << MD_DISK_ACTIVE
) : 0;
2681 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
2682 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
2683 tmp
->disk
.raid_disk
= -1;
2689 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
2690 char *update
, char *devname
, int verbose
,
2691 int uuid_set
, char *homehost
)
2693 /* For 'assemble' and 'force' we need to return non-zero if any
2694 * change was made. For others, the return value is ignored.
2695 * Update options are:
2696 * force-one : This device looks a bit old but needs to be included,
2697 * update age info appropriately.
2698 * assemble: clear any 'faulty' flag to allow this device to
2700 * force-array: Array is degraded but being forced, mark it clean
2701 * if that will be needed to assemble it.
2703 * newdev: not used ????
2704 * grow: Array has gained a new device - this is currently for
2706 * resync: mark as dirty so a resync will happen.
2707 * name: update the name - preserving the homehost
2708 * uuid: Change the uuid of the array to match watch is given
2710 * Following are not relevant for this imsm:
2711 * sparc2.2 : update from old dodgey metadata
2712 * super-minor: change the preferred_minor number
2713 * summaries: update redundant counters.
2714 * homehost: update the recorded homehost
2715 * _reshape_progress: record new reshape_progress position.
2718 struct intel_super
*super
= st
->sb
;
2719 struct imsm_super
*mpb
;
2721 /* we can only update container info */
2722 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
2725 mpb
= super
->anchor
;
2727 if (strcmp(update
, "uuid") == 0 && uuid_set
&& !info
->update_private
)
2729 else if (strcmp(update
, "uuid") == 0 && uuid_set
&& info
->update_private
) {
2730 mpb
->orig_family_num
= *((__u32
*) info
->update_private
);
2732 } else if (strcmp(update
, "uuid") == 0) {
2733 __u32
*new_family
= malloc(sizeof(*new_family
));
2735 /* update orig_family_number with the incoming random
2736 * data, report the new effective uuid, and store the
2737 * new orig_family_num for future updates.
2740 memcpy(&mpb
->orig_family_num
, info
->uuid
, sizeof(__u32
));
2741 uuid_from_super_imsm(st
, info
->uuid
);
2742 *new_family
= mpb
->orig_family_num
;
2743 info
->update_private
= new_family
;
2746 } else if (strcmp(update
, "assemble") == 0)
2751 /* successful update? recompute checksum */
2753 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
2758 static size_t disks_to_mpb_size(int disks
)
2762 size
= sizeof(struct imsm_super
);
2763 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
2764 size
+= 2 * sizeof(struct imsm_dev
);
2765 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
2766 size
+= (4 - 2) * sizeof(struct imsm_map
);
2767 /* 4 possible disk_ord_tbl's */
2768 size
+= 4 * (disks
- 1) * sizeof(__u32
);
2773 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
)
2775 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
2778 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
2781 static void free_devlist(struct intel_super
*super
)
2783 struct intel_dev
*dv
;
2785 while (super
->devlist
) {
2786 dv
= super
->devlist
->next
;
2787 free(super
->devlist
->dev
);
2788 free(super
->devlist
);
2789 super
->devlist
= dv
;
2793 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
2795 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
2798 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
2802 * 0 same, or first was empty, and second was copied
2803 * 1 second had wrong number
2805 * 3 wrong other info
2807 struct intel_super
*first
= st
->sb
;
2808 struct intel_super
*sec
= tst
->sb
;
2815 /* in platform dependent environment test if the disks
2816 * use the same Intel hba
2818 if (!check_env("IMSM_NO_PLATFORM")) {
2819 if (!first
->hba
|| !sec
->hba
||
2820 (first
->hba
->type
!= sec
->hba
->type
)) {
2822 "HBAs of devices does not match %s != %s\n",
2823 first
->hba
? get_sys_dev_type(first
->hba
->type
) : NULL
,
2824 sec
->hba
? get_sys_dev_type(sec
->hba
->type
) : NULL
);
2829 /* if an anchor does not have num_raid_devs set then it is a free
2832 if (first
->anchor
->num_raid_devs
> 0 &&
2833 sec
->anchor
->num_raid_devs
> 0) {
2834 /* Determine if these disks might ever have been
2835 * related. Further disambiguation can only take place
2836 * in load_super_imsm_all
2838 __u32 first_family
= first
->anchor
->orig_family_num
;
2839 __u32 sec_family
= sec
->anchor
->orig_family_num
;
2841 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
2842 MAX_SIGNATURE_LENGTH
) != 0)
2845 if (first_family
== 0)
2846 first_family
= first
->anchor
->family_num
;
2847 if (sec_family
== 0)
2848 sec_family
= sec
->anchor
->family_num
;
2850 if (first_family
!= sec_family
)
2856 /* if 'first' is a spare promote it to a populated mpb with sec's
2859 if (first
->anchor
->num_raid_devs
== 0 &&
2860 sec
->anchor
->num_raid_devs
> 0) {
2862 struct intel_dev
*dv
;
2863 struct imsm_dev
*dev
;
2865 /* we need to copy raid device info from sec if an allocation
2866 * fails here we don't associate the spare
2868 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
2869 dv
= malloc(sizeof(*dv
));
2872 dev
= malloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
2879 dv
->next
= first
->devlist
;
2880 first
->devlist
= dv
;
2882 if (i
< sec
->anchor
->num_raid_devs
) {
2883 /* allocation failure */
2884 free_devlist(first
);
2885 fprintf(stderr
, "imsm: failed to associate spare\n");
2888 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
2889 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
2890 first
->anchor
->family_num
= sec
->anchor
->family_num
;
2891 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
2892 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
2893 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
2899 static void fd2devname(int fd
, char *name
)
2903 char dname
[PATH_MAX
];
2908 if (fstat(fd
, &st
) != 0)
2910 sprintf(path
, "/sys/dev/block/%d:%d",
2911 major(st
.st_rdev
), minor(st
.st_rdev
));
2913 rv
= readlink(path
, dname
, sizeof(dname
)-1);
2918 nm
= strrchr(dname
, '/');
2921 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
2925 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
2927 static int imsm_read_serial(int fd
, char *devname
,
2928 __u8 serial
[MAX_RAID_SERIAL_LEN
])
2930 unsigned char scsi_serial
[255];
2939 memset(scsi_serial
, 0, sizeof(scsi_serial
));
2941 rv
= scsi_get_serial(fd
, scsi_serial
, sizeof(scsi_serial
));
2943 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
2944 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
2945 fd2devname(fd
, (char *) serial
);
2952 Name
": Failed to retrieve serial for %s\n",
2957 rsp_len
= scsi_serial
[3];
2961 Name
": Failed to retrieve serial for %s\n",
2965 rsp_buf
= (char *) &scsi_serial
[4];
2967 /* trim all whitespace and non-printable characters and convert
2970 for (i
= 0, dest
= rsp_buf
; i
< rsp_len
; i
++) {
2973 /* ':' is reserved for use in placeholder serial
2974 * numbers for missing disks
2982 len
= dest
- rsp_buf
;
2985 /* truncate leading characters */
2986 if (len
> MAX_RAID_SERIAL_LEN
) {
2987 dest
+= len
- MAX_RAID_SERIAL_LEN
;
2988 len
= MAX_RAID_SERIAL_LEN
;
2991 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
2992 memcpy(serial
, dest
, len
);
2997 static int serialcmp(__u8
*s1
, __u8
*s2
)
2999 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
3002 static void serialcpy(__u8
*dest
, __u8
*src
)
3004 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
3007 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
3011 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3012 if (serialcmp(dl
->serial
, serial
) == 0)
3018 static struct imsm_disk
*
3019 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
3023 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3024 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3026 if (serialcmp(disk
->serial
, serial
) == 0) {
3037 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3039 struct imsm_disk
*disk
;
3044 __u8 serial
[MAX_RAID_SERIAL_LEN
];
3046 rv
= imsm_read_serial(fd
, devname
, serial
);
3051 dl
= calloc(1, sizeof(*dl
));
3055 Name
": failed to allocate disk buffer for %s\n",
3061 dl
->major
= major(stb
.st_rdev
);
3062 dl
->minor
= minor(stb
.st_rdev
);
3063 dl
->next
= super
->disks
;
3064 dl
->fd
= keep_fd
? fd
: -1;
3065 assert(super
->disks
== NULL
);
3067 serialcpy(dl
->serial
, serial
);
3070 fd2devname(fd
, name
);
3072 dl
->devname
= strdup(devname
);
3074 dl
->devname
= strdup(name
);
3076 /* look up this disk's index in the current anchor */
3077 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
3080 /* only set index on disks that are a member of a
3081 * populated contianer, i.e. one with raid_devs
3083 if (is_failed(&dl
->disk
))
3085 else if (is_spare(&dl
->disk
))
3093 /* When migrating map0 contains the 'destination' state while map1
3094 * contains the current state. When not migrating map0 contains the
3095 * current state. This routine assumes that map[0].map_state is set to
3096 * the current array state before being called.
3098 * Migration is indicated by one of the following states
3099 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
3100 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
3101 * map1state=unitialized)
3102 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
3104 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
3105 * map1state=degraded)
3106 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
3109 static void migrate(struct imsm_dev
*dev
, struct intel_super
*super
,
3110 __u8 to_state
, int migr_type
)
3112 struct imsm_map
*dest
;
3113 struct imsm_map
*src
= get_imsm_map(dev
, 0);
3115 dev
->vol
.migr_state
= 1;
3116 set_migr_type(dev
, migr_type
);
3117 dev
->vol
.curr_migr_unit
= 0;
3118 dest
= get_imsm_map(dev
, 1);
3120 /* duplicate and then set the target end state in map[0] */
3121 memcpy(dest
, src
, sizeof_imsm_map(src
));
3122 if ((migr_type
== MIGR_REBUILD
) ||
3123 (migr_type
== MIGR_GEN_MIGR
)) {
3127 for (i
= 0; i
< src
->num_members
; i
++) {
3128 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
3129 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
3133 if (migr_type
== MIGR_GEN_MIGR
)
3134 /* Clear migration record */
3135 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
3137 src
->map_state
= to_state
;
3140 static void end_migration(struct imsm_dev
*dev
, __u8 map_state
)
3142 struct imsm_map
*map
= get_imsm_map(dev
, 0);
3143 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
);
3146 /* merge any IMSM_ORD_REBUILD bits that were not successfully
3147 * completed in the last migration.
3149 * FIXME add support for raid-level-migration
3151 for (i
= 0; i
< prev
->num_members
; i
++)
3152 for (j
= 0; j
< map
->num_members
; j
++)
3153 /* during online capacity expansion
3154 * disks position can be changed if takeover is used
3156 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
3157 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
3158 map
->disk_ord_tbl
[j
] |= prev
->disk_ord_tbl
[i
];
3162 dev
->vol
.migr_state
= 0;
3163 set_migr_type(dev
, 0);
3164 dev
->vol
.curr_migr_unit
= 0;
3165 map
->map_state
= map_state
;
3169 static int parse_raid_devices(struct intel_super
*super
)
3172 struct imsm_dev
*dev_new
;
3173 size_t len
, len_migr
;
3175 size_t space_needed
= 0;
3176 struct imsm_super
*mpb
= super
->anchor
;
3178 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3179 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3180 struct intel_dev
*dv
;
3182 len
= sizeof_imsm_dev(dev_iter
, 0);
3183 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
3185 space_needed
+= len_migr
- len
;
3187 dv
= malloc(sizeof(*dv
));
3190 if (max_len
< len_migr
)
3192 if (max_len
> len_migr
)
3193 space_needed
+= max_len
- len_migr
;
3194 dev_new
= malloc(max_len
);
3199 imsm_copy_dev(dev_new
, dev_iter
);
3202 dv
->next
= super
->devlist
;
3203 super
->devlist
= dv
;
3206 /* ensure that super->buf is large enough when all raid devices
3209 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
3212 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
, 512);
3213 if (posix_memalign(&buf
, 512, len
) != 0)
3216 memcpy(buf
, super
->buf
, super
->len
);
3217 memset(buf
+ super
->len
, 0, len
- super
->len
);
3226 /* retrieve a pointer to the bbm log which starts after all raid devices */
3227 struct bbm_log
*__get_imsm_bbm_log(struct imsm_super
*mpb
)
3231 if (__le32_to_cpu(mpb
->bbm_log_size
)) {
3233 ptr
+= mpb
->mpb_size
- __le32_to_cpu(mpb
->bbm_log_size
);
3239 /*******************************************************************************
3240 * Function: check_mpb_migr_compatibility
3241 * Description: Function checks for unsupported migration features:
3242 * - migration optimization area (pba_of_lba0)
3243 * - descending reshape (ascending_migr)
3245 * super : imsm metadata information
3247 * 0 : migration is compatible
3248 * -1 : migration is not compatible
3249 ******************************************************************************/
3250 int check_mpb_migr_compatibility(struct intel_super
*super
)
3252 struct imsm_map
*map0
, *map1
;
3253 struct migr_record
*migr_rec
= super
->migr_rec
;
3256 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3257 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3260 dev_iter
->vol
.migr_state
== 1 &&
3261 dev_iter
->vol
.migr_type
== MIGR_GEN_MIGR
) {
3262 /* This device is migrating */
3263 map0
= get_imsm_map(dev_iter
, 0);
3264 map1
= get_imsm_map(dev_iter
, 1);
3265 if (map0
->pba_of_lba0
!= map1
->pba_of_lba0
)
3266 /* migration optimization area was used */
3268 if (migr_rec
->ascending_migr
== 0
3269 && migr_rec
->dest_depth_per_unit
> 0)
3270 /* descending reshape not supported yet */
3277 static void __free_imsm(struct intel_super
*super
, int free_disks
);
3279 /* load_imsm_mpb - read matrix metadata
3280 * allocates super->mpb to be freed by free_imsm
3282 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
3284 unsigned long long dsize
;
3285 unsigned long long sectors
;
3287 struct imsm_super
*anchor
;
3290 get_dev_size(fd
, NULL
, &dsize
);
3294 Name
": %s: device to small for imsm\n",
3299 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0) {
3301 fprintf(stderr
, Name
3302 ": Cannot seek to anchor block on %s: %s\n",
3303 devname
, strerror(errno
));
3307 if (posix_memalign((void**)&anchor
, 512, 512) != 0) {
3310 Name
": Failed to allocate imsm anchor buffer"
3311 " on %s\n", devname
);
3314 if (read(fd
, anchor
, 512) != 512) {
3317 Name
": Cannot read anchor block on %s: %s\n",
3318 devname
, strerror(errno
));
3323 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
3326 Name
": no IMSM anchor on %s\n", devname
);
3331 __free_imsm(super
, 0);
3332 /* reload capability and hba */
3334 /* capability and hba must be updated with new super allocation */
3335 find_intel_hba_capability(fd
, super
, devname
);
3336 super
->len
= ROUND_UP(anchor
->mpb_size
, 512);
3337 if (posix_memalign(&super
->buf
, 512, super
->len
) != 0) {
3340 Name
": unable to allocate %zu byte mpb buffer\n",
3345 memcpy(super
->buf
, anchor
, 512);
3347 sectors
= mpb_sectors(anchor
) - 1;
3350 if (posix_memalign(&super
->migr_rec_buf
, 512, 512) != 0) {
3351 fprintf(stderr
, Name
3352 ": %s could not allocate migr_rec buffer\n", __func__
);
3358 check_sum
= __gen_imsm_checksum(super
->anchor
);
3359 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3362 Name
": IMSM checksum %x != %x on %s\n",
3364 __le32_to_cpu(super
->anchor
->check_sum
),
3372 /* read the extended mpb */
3373 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0) {
3376 Name
": Cannot seek to extended mpb on %s: %s\n",
3377 devname
, strerror(errno
));
3381 if ((unsigned)read(fd
, super
->buf
+ 512, super
->len
- 512) != super
->len
- 512) {
3384 Name
": Cannot read extended mpb on %s: %s\n",
3385 devname
, strerror(errno
));
3389 check_sum
= __gen_imsm_checksum(super
->anchor
);
3390 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3393 Name
": IMSM checksum %x != %x on %s\n",
3394 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
3399 /* FIXME the BBM log is disk specific so we cannot use this global
3400 * buffer for all disks. Ok for now since we only look at the global
3401 * bbm_log_size parameter to gate assembly
3403 super
->bbm_log
= __get_imsm_bbm_log(super
->anchor
);
3408 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
);
3411 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3415 err
= load_imsm_mpb(fd
, super
, devname
);
3418 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
3421 err
= parse_raid_devices(super
);
3426 static void __free_imsm_disk(struct dl
*d
)
3438 static void free_imsm_disks(struct intel_super
*super
)
3442 while (super
->disks
) {
3444 super
->disks
= d
->next
;
3445 __free_imsm_disk(d
);
3447 while (super
->disk_mgmt_list
) {
3448 d
= super
->disk_mgmt_list
;
3449 super
->disk_mgmt_list
= d
->next
;
3450 __free_imsm_disk(d
);
3452 while (super
->missing
) {
3454 super
->missing
= d
->next
;
3455 __free_imsm_disk(d
);
3460 /* free all the pieces hanging off of a super pointer */
3461 static void __free_imsm(struct intel_super
*super
, int free_disks
)
3463 struct intel_hba
*elem
, *next
;
3469 /* unlink capability description */
3471 if (super
->migr_rec_buf
) {
3472 free(super
->migr_rec_buf
);
3473 super
->migr_rec_buf
= NULL
;
3476 free_imsm_disks(super
);
3477 free_devlist(super
);
3481 free((void *)elem
->path
);
3489 static void free_imsm(struct intel_super
*super
)
3491 __free_imsm(super
, 1);
3495 static void free_super_imsm(struct supertype
*st
)
3497 struct intel_super
*super
= st
->sb
;
3506 static struct intel_super
*alloc_super(void)
3508 struct intel_super
*super
= malloc(sizeof(*super
));
3511 memset(super
, 0, sizeof(*super
));
3512 super
->current_vol
= -1;
3513 super
->create_offset
= ~((__u32
) 0);
3519 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
3521 static int find_intel_hba_capability(int fd
, struct intel_super
*super
, char *devname
)
3523 struct sys_dev
*hba_name
;
3526 if ((fd
< 0) || check_env("IMSM_NO_PLATFORM")) {
3531 hba_name
= find_disk_attached_hba(fd
, NULL
);
3535 Name
": %s is not attached to Intel(R) RAID controller.\n",
3539 rv
= attach_hba_to_super(super
, hba_name
);
3542 struct intel_hba
*hba
= super
->hba
;
3544 fprintf(stderr
, Name
": %s is attached to Intel(R) %s RAID "
3545 "controller (%s),\n"
3546 " but the container is assigned to Intel(R) "
3547 "%s RAID controller (",
3550 hba_name
->pci_id
? : "Err!",
3551 get_sys_dev_type(hba_name
->type
));
3554 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
3556 fprintf(stderr
, ", ");
3560 fprintf(stderr
, ").\n"
3561 " Mixing devices attached to different controllers "
3562 "is not allowed.\n");
3564 free_sys_dev(&hba_name
);
3567 super
->orom
= find_imsm_capability(hba_name
->type
);
3568 free_sys_dev(&hba_name
);
3574 /* find_missing - helper routine for load_super_imsm_all that identifies
3575 * disks that have disappeared from the system. This routine relies on
3576 * the mpb being uptodate, which it is at load time.
3578 static int find_missing(struct intel_super
*super
)
3581 struct imsm_super
*mpb
= super
->anchor
;
3583 struct imsm_disk
*disk
;
3585 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3586 disk
= __get_imsm_disk(mpb
, i
);
3587 dl
= serial_to_dl(disk
->serial
, super
);
3591 dl
= malloc(sizeof(*dl
));
3597 dl
->devname
= strdup("missing");
3599 serialcpy(dl
->serial
, disk
->serial
);
3602 dl
->next
= super
->missing
;
3603 super
->missing
= dl
;
3610 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
3612 struct intel_disk
*idisk
= disk_list
;
3615 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
3617 idisk
= idisk
->next
;
3623 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
3624 struct intel_super
*super
,
3625 struct intel_disk
**disk_list
)
3627 struct imsm_disk
*d
= &super
->disks
->disk
;
3628 struct imsm_super
*mpb
= super
->anchor
;
3631 for (i
= 0; i
< tbl_size
; i
++) {
3632 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
3633 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
3635 if (tbl_mpb
->family_num
== mpb
->family_num
) {
3636 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
3637 dprintf("%s: mpb from %d:%d matches %d:%d\n",
3638 __func__
, super
->disks
->major
,
3639 super
->disks
->minor
,
3640 table
[i
]->disks
->major
,
3641 table
[i
]->disks
->minor
);
3645 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
3646 is_configured(d
) == is_configured(tbl_d
)) &&
3647 tbl_mpb
->generation_num
< mpb
->generation_num
) {
3648 /* current version of the mpb is a
3649 * better candidate than the one in
3650 * super_table, but copy over "cross
3651 * generational" status
3653 struct intel_disk
*idisk
;
3655 dprintf("%s: mpb from %d:%d replaces %d:%d\n",
3656 __func__
, super
->disks
->major
,
3657 super
->disks
->minor
,
3658 table
[i
]->disks
->major
,
3659 table
[i
]->disks
->minor
);
3661 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
3662 if (idisk
&& is_failed(&idisk
->disk
))
3663 tbl_d
->status
|= FAILED_DISK
;
3666 struct intel_disk
*idisk
;
3667 struct imsm_disk
*disk
;
3669 /* tbl_mpb is more up to date, but copy
3670 * over cross generational status before
3673 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
3674 if (disk
&& is_failed(disk
))
3675 d
->status
|= FAILED_DISK
;
3677 idisk
= disk_list_get(d
->serial
, *disk_list
);
3680 if (disk
&& is_configured(disk
))
3681 idisk
->disk
.status
|= CONFIGURED_DISK
;
3684 dprintf("%s: mpb from %d:%d prefer %d:%d\n",
3685 __func__
, super
->disks
->major
,
3686 super
->disks
->minor
,
3687 table
[i
]->disks
->major
,
3688 table
[i
]->disks
->minor
);
3696 table
[tbl_size
++] = super
;
3700 /* update/extend the merged list of imsm_disk records */
3701 for (j
= 0; j
< mpb
->num_disks
; j
++) {
3702 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
3703 struct intel_disk
*idisk
;
3705 idisk
= disk_list_get(disk
->serial
, *disk_list
);
3707 idisk
->disk
.status
|= disk
->status
;
3708 if (is_configured(&idisk
->disk
) ||
3709 is_failed(&idisk
->disk
))
3710 idisk
->disk
.status
&= ~(SPARE_DISK
);
3712 idisk
= calloc(1, sizeof(*idisk
));
3715 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
3716 idisk
->disk
= *disk
;
3717 idisk
->next
= *disk_list
;
3721 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
3728 static struct intel_super
*
3729 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
3732 struct imsm_super
*mpb
= super
->anchor
;
3736 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3737 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3738 struct intel_disk
*idisk
;
3740 idisk
= disk_list_get(disk
->serial
, disk_list
);
3742 if (idisk
->owner
== owner
||
3743 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
3746 dprintf("%s: '%.16s' owner %d != %d\n",
3747 __func__
, disk
->serial
, idisk
->owner
,
3750 dprintf("%s: unknown disk %x [%d]: %.16s\n",
3751 __func__
, __le32_to_cpu(mpb
->family_num
), i
,
3757 if (ok_count
== mpb
->num_disks
)
3762 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
3764 struct intel_super
*s
;
3766 for (s
= super_list
; s
; s
= s
->next
) {
3767 if (family_num
!= s
->anchor
->family_num
)
3769 fprintf(stderr
, "Conflict, offlining family %#x on '%s'\n",
3770 __le32_to_cpu(family_num
), s
->disks
->devname
);
3774 static struct intel_super
*
3775 imsm_thunderdome(struct intel_super
**super_list
, int len
)
3777 struct intel_super
*super_table
[len
];
3778 struct intel_disk
*disk_list
= NULL
;
3779 struct intel_super
*champion
, *spare
;
3780 struct intel_super
*s
, **del
;
3785 memset(super_table
, 0, sizeof(super_table
));
3786 for (s
= *super_list
; s
; s
= s
->next
)
3787 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
3789 for (i
= 0; i
< tbl_size
; i
++) {
3790 struct imsm_disk
*d
;
3791 struct intel_disk
*idisk
;
3792 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
3795 d
= &s
->disks
->disk
;
3797 /* 'd' must appear in merged disk list for its
3798 * configuration to be valid
3800 idisk
= disk_list_get(d
->serial
, disk_list
);
3801 if (idisk
&& idisk
->owner
== i
)
3802 s
= validate_members(s
, disk_list
, i
);
3807 dprintf("%s: marking family: %#x from %d:%d offline\n",
3808 __func__
, mpb
->family_num
,
3809 super_table
[i
]->disks
->major
,
3810 super_table
[i
]->disks
->minor
);
3814 /* This is where the mdadm implementation differs from the Windows
3815 * driver which has no strict concept of a container. We can only
3816 * assemble one family from a container, so when returning a prodigal
3817 * array member to this system the code will not be able to disambiguate
3818 * the container contents that should be assembled ("foreign" versus
3819 * "local"). It requires user intervention to set the orig_family_num
3820 * to a new value to establish a new container. The Windows driver in
3821 * this situation fixes up the volume name in place and manages the
3822 * foreign array as an independent entity.
3827 for (i
= 0; i
< tbl_size
; i
++) {
3828 struct intel_super
*tbl_ent
= super_table
[i
];
3834 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
3839 if (s
&& !is_spare
) {
3840 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
3842 } else if (!s
&& !is_spare
)
3855 fprintf(stderr
, "Chose family %#x on '%s', "
3856 "assemble conflicts to new container with '--update=uuid'\n",
3857 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
3859 /* collect all dl's onto 'champion', and update them to
3860 * champion's version of the status
3862 for (s
= *super_list
; s
; s
= s
->next
) {
3863 struct imsm_super
*mpb
= champion
->anchor
;
3864 struct dl
*dl
= s
->disks
;
3869 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3870 struct imsm_disk
*disk
;
3872 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
3875 /* only set index on disks that are a member of
3876 * a populated contianer, i.e. one with
3879 if (is_failed(&dl
->disk
))
3881 else if (is_spare(&dl
->disk
))
3887 if (i
>= mpb
->num_disks
) {
3888 struct intel_disk
*idisk
;
3890 idisk
= disk_list_get(dl
->serial
, disk_list
);
3891 if (idisk
&& is_spare(&idisk
->disk
) &&
3892 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
3900 dl
->next
= champion
->disks
;
3901 champion
->disks
= dl
;
3905 /* delete 'champion' from super_list */
3906 for (del
= super_list
; *del
; ) {
3907 if (*del
== champion
) {
3908 *del
= (*del
)->next
;
3911 del
= &(*del
)->next
;
3913 champion
->next
= NULL
;
3917 struct intel_disk
*idisk
= disk_list
;
3919 disk_list
= disk_list
->next
;
3926 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
3930 struct intel_super
*super_list
= NULL
;
3931 struct intel_super
*super
= NULL
;
3932 int devnum
= fd2devnum(fd
);
3938 /* check if 'fd' an opened container */
3939 sra
= sysfs_read(fd
, 0, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
3943 if (sra
->array
.major_version
!= -1 ||
3944 sra
->array
.minor_version
!= -2 ||
3945 strcmp(sra
->text_version
, "imsm") != 0) {
3950 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
3951 struct intel_super
*s
= alloc_super();
3959 s
->next
= super_list
;
3963 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
3964 dfd
= dev_open(nm
, O_RDWR
);
3968 rv
= find_intel_hba_capability(dfd
, s
, devname
);
3969 /* no orom/efi or non-intel hba of the disk */
3973 err
= load_and_parse_mpb(dfd
, s
, NULL
, 1);
3975 /* retry the load if we might have raced against mdmon */
3976 if (err
== 3 && mdmon_running(devnum
))
3977 for (retry
= 0; retry
< 3; retry
++) {
3979 err
= load_and_parse_mpb(dfd
, s
, NULL
, 1);
3987 /* all mpbs enter, maybe one leaves */
3988 super
= imsm_thunderdome(&super_list
, i
);
3994 if (find_missing(super
) != 0) {
4000 /* load migration record */
4001 err
= load_imsm_migr_rec(super
, NULL
);
4007 /* Check migration compatibility */
4008 if (check_mpb_migr_compatibility(super
) != 0) {
4009 fprintf(stderr
, Name
": Unsupported migration detected");
4011 fprintf(stderr
, " on %s\n", devname
);
4013 fprintf(stderr
, " (IMSM).\n");
4022 while (super_list
) {
4023 struct intel_super
*s
= super_list
;
4025 super_list
= super_list
->next
;
4034 st
->container_dev
= devnum
;
4035 if (err
== 0 && st
->ss
== NULL
) {
4036 st
->ss
= &super_imsm
;
4037 st
->minor_version
= 0;
4038 st
->max_devs
= IMSM_MAX_DEVICES
;
4043 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
4045 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
);
4049 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
4051 struct intel_super
*super
;
4054 if (test_partition(fd
))
4055 /* IMSM not allowed on partitions */
4058 free_super_imsm(st
);
4060 super
= alloc_super();
4063 Name
": malloc of %zu failed.\n",
4067 /* Load hba and capabilities if they exist.
4068 * But do not preclude loading metadata in case capabilities or hba are
4069 * non-compliant and ignore_hw_compat is set.
4071 rv
= find_intel_hba_capability(fd
, super
, devname
);
4072 /* no orom/efi or non-intel hba of the disk */
4073 if ((rv
!= 0) && (st
->ignore_hw_compat
== 0)) {
4076 Name
": No OROM/EFI properties for %s\n", devname
);
4080 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
4085 Name
": Failed to load all information "
4086 "sections on %s\n", devname
);
4092 if (st
->ss
== NULL
) {
4093 st
->ss
= &super_imsm
;
4094 st
->minor_version
= 0;
4095 st
->max_devs
= IMSM_MAX_DEVICES
;
4098 /* load migration record */
4099 if (load_imsm_migr_rec(super
, NULL
) == 0) {
4100 /* Check for unsupported migration features */
4101 if (check_mpb_migr_compatibility(super
) != 0) {
4103 Name
": Unsupported migration detected");
4105 fprintf(stderr
, " on %s\n", devname
);
4107 fprintf(stderr
, " (IMSM).\n");
4115 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
4117 if (info
->level
== 1)
4119 return info
->chunk_size
>> 9;
4122 static __u32
info_to_num_data_stripes(mdu_array_info_t
*info
, int num_domains
)
4126 num_stripes
= (info
->size
* 2) / info_to_blocks_per_strip(info
);
4127 num_stripes
/= num_domains
;
4132 static __u32
info_to_blocks_per_member(mdu_array_info_t
*info
)
4134 if (info
->level
== 1)
4135 return info
->size
* 2;
4137 return (info
->size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
4140 static void imsm_update_version_info(struct intel_super
*super
)
4142 /* update the version and attributes */
4143 struct imsm_super
*mpb
= super
->anchor
;
4145 struct imsm_dev
*dev
;
4146 struct imsm_map
*map
;
4149 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4150 dev
= get_imsm_dev(super
, i
);
4151 map
= get_imsm_map(dev
, 0);
4152 if (__le32_to_cpu(dev
->size_high
) > 0)
4153 mpb
->attributes
|= MPB_ATTRIB_2TB
;
4155 /* FIXME detect when an array spans a port multiplier */
4157 mpb
->attributes
|= MPB_ATTRIB_PM
;
4160 if (mpb
->num_raid_devs
> 1 ||
4161 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
4162 version
= MPB_VERSION_ATTRIBS
;
4163 switch (get_imsm_raid_level(map
)) {
4164 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
4165 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
4166 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
4167 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
4170 if (map
->num_members
>= 5)
4171 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
4172 else if (dev
->status
== DEV_CLONE_N_GO
)
4173 version
= MPB_VERSION_CNG
;
4174 else if (get_imsm_raid_level(map
) == 5)
4175 version
= MPB_VERSION_RAID5
;
4176 else if (map
->num_members
>= 3)
4177 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
4178 else if (get_imsm_raid_level(map
) == 1)
4179 version
= MPB_VERSION_RAID1
;
4181 version
= MPB_VERSION_RAID0
;
4183 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
4187 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
4189 struct imsm_super
*mpb
= super
->anchor
;
4190 char *reason
= NULL
;
4193 if (strlen(name
) > MAX_RAID_SERIAL_LEN
)
4194 reason
= "must be 16 characters or less";
4196 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4197 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4199 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
4200 reason
= "already exists";
4205 if (reason
&& !quiet
)
4206 fprintf(stderr
, Name
": imsm volume name %s\n", reason
);
4211 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
4212 unsigned long long size
, char *name
,
4213 char *homehost
, int *uuid
)
4215 /* We are creating a volume inside a pre-existing container.
4216 * so st->sb is already set.
4218 struct intel_super
*super
= st
->sb
;
4219 struct imsm_super
*mpb
= super
->anchor
;
4220 struct intel_dev
*dv
;
4221 struct imsm_dev
*dev
;
4222 struct imsm_vol
*vol
;
4223 struct imsm_map
*map
;
4224 int idx
= mpb
->num_raid_devs
;
4226 unsigned long long array_blocks
;
4227 size_t size_old
, size_new
;
4228 __u32 num_data_stripes
;
4230 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
4231 fprintf(stderr
, Name
": This imsm-container already has the "
4232 "maximum of %d volumes\n", super
->orom
->vpa
);
4236 /* ensure the mpb is large enough for the new data */
4237 size_old
= __le32_to_cpu(mpb
->mpb_size
);
4238 size_new
= disks_to_mpb_size(info
->nr_disks
);
4239 if (size_new
> size_old
) {
4241 size_t size_round
= ROUND_UP(size_new
, 512);
4243 if (posix_memalign(&mpb_new
, 512, size_round
) != 0) {
4244 fprintf(stderr
, Name
": could not allocate new mpb\n");
4247 if (posix_memalign(&super
->migr_rec_buf
, 512, 512) != 0) {
4248 fprintf(stderr
, Name
4249 ": %s could not allocate migr_rec buffer\n",
4256 memcpy(mpb_new
, mpb
, size_old
);
4259 super
->anchor
= mpb_new
;
4260 mpb
->mpb_size
= __cpu_to_le32(size_new
);
4261 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
4263 super
->current_vol
= idx
;
4265 /* handle 'failed_disks' by either:
4266 * a) create dummy disk entries in the table if this the first
4267 * volume in the array. We add them here as this is the only
4268 * opportunity to add them. add_to_super_imsm_volume()
4269 * handles the non-failed disks and continues incrementing
4271 * b) validate that 'failed_disks' matches the current number
4272 * of missing disks if the container is populated
4274 if (super
->current_vol
== 0) {
4276 for (i
= 0; i
< info
->failed_disks
; i
++) {
4277 struct imsm_disk
*disk
;
4280 disk
= __get_imsm_disk(mpb
, i
);
4281 disk
->status
= CONFIGURED_DISK
| FAILED_DISK
;
4282 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
4283 snprintf((char *) disk
->serial
, MAX_RAID_SERIAL_LEN
,
4286 find_missing(super
);
4291 for (d
= super
->missing
; d
; d
= d
->next
)
4293 if (info
->failed_disks
> missing
) {
4294 fprintf(stderr
, Name
": unable to add 'missing' disk to container\n");
4299 if (!check_name(super
, name
, 0))
4301 dv
= malloc(sizeof(*dv
));
4303 fprintf(stderr
, Name
": failed to allocate device list entry\n");
4306 dev
= calloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
4309 fprintf(stderr
, Name
": could not allocate raid device\n");
4313 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
4314 if (info
->level
== 1)
4315 array_blocks
= info_to_blocks_per_member(info
);
4317 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
4318 info
->layout
, info
->chunk_size
,
4320 /* round array size down to closest MB */
4321 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
4323 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
4324 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
4325 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
4327 vol
->migr_state
= 0;
4328 set_migr_type(dev
, MIGR_INIT
);
4329 vol
->dirty
= !info
->state
;
4330 vol
->curr_migr_unit
= 0;
4331 map
= get_imsm_map(dev
, 0);
4332 map
->pba_of_lba0
= __cpu_to_le32(super
->create_offset
);
4333 map
->blocks_per_member
= __cpu_to_le32(info_to_blocks_per_member(info
));
4334 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
4335 map
->failed_disk_num
= ~0;
4336 map
->map_state
= info
->failed_disks
? IMSM_T_STATE_DEGRADED
: IMSM_T_STATE_NORMAL
;
4339 if (info
->level
== 1 && info
->raid_disks
> 2) {
4342 fprintf(stderr
, Name
": imsm does not support more than 2 disks"
4343 "in a raid1 volume\n");
4347 map
->raid_level
= info
->level
;
4348 if (info
->level
== 10) {
4349 map
->raid_level
= 1;
4350 map
->num_domains
= info
->raid_disks
/ 2;
4351 } else if (info
->level
== 1)
4352 map
->num_domains
= info
->raid_disks
;
4354 map
->num_domains
= 1;
4356 num_data_stripes
= info_to_num_data_stripes(info
, map
->num_domains
);
4357 map
->num_data_stripes
= __cpu_to_le32(num_data_stripes
);
4359 map
->num_members
= info
->raid_disks
;
4360 for (i
= 0; i
< map
->num_members
; i
++) {
4361 /* initialized in add_to_super */
4362 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
4364 mpb
->num_raid_devs
++;
4367 dv
->index
= super
->current_vol
;
4368 dv
->next
= super
->devlist
;
4369 super
->devlist
= dv
;
4371 imsm_update_version_info(super
);
4376 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
4377 unsigned long long size
, char *name
,
4378 char *homehost
, int *uuid
)
4380 /* This is primarily called by Create when creating a new array.
4381 * We will then get add_to_super called for each component, and then
4382 * write_init_super called to write it out to each device.
4383 * For IMSM, Create can create on fresh devices or on a pre-existing
4385 * To create on a pre-existing array a different method will be called.
4386 * This one is just for fresh drives.
4388 struct intel_super
*super
;
4389 struct imsm_super
*mpb
;
4394 return init_super_imsm_volume(st
, info
, size
, name
, homehost
, uuid
);
4397 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
4401 super
= alloc_super();
4402 if (super
&& posix_memalign(&super
->buf
, 512, mpb_size
) != 0) {
4407 fprintf(stderr
, Name
4408 ": %s could not allocate superblock\n", __func__
);
4411 if (posix_memalign(&super
->migr_rec_buf
, 512, 512) != 0) {
4412 fprintf(stderr
, Name
4413 ": %s could not allocate migr_rec buffer\n", __func__
);
4418 memset(super
->buf
, 0, mpb_size
);
4420 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
4424 /* zeroing superblock */
4428 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
4430 version
= (char *) mpb
->sig
;
4431 strcpy(version
, MPB_SIGNATURE
);
4432 version
+= strlen(MPB_SIGNATURE
);
4433 strcpy(version
, MPB_VERSION_RAID0
);
4439 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
4440 int fd
, char *devname
)
4442 struct intel_super
*super
= st
->sb
;
4443 struct imsm_super
*mpb
= super
->anchor
;
4444 struct imsm_disk
*_disk
;
4445 struct imsm_dev
*dev
;
4446 struct imsm_map
*map
;
4450 dev
= get_imsm_dev(super
, super
->current_vol
);
4451 map
= get_imsm_map(dev
, 0);
4453 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
4454 fprintf(stderr
, Name
": %s: Cannot add spare devices to IMSM volume\n",
4460 /* we're doing autolayout so grab the pre-marked (in
4461 * validate_geometry) raid_disk
4463 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4464 if (dl
->raiddisk
== dk
->raid_disk
)
4467 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4468 if (dl
->major
== dk
->major
&&
4469 dl
->minor
== dk
->minor
)
4474 fprintf(stderr
, Name
": %s is not a member of the same container\n", devname
);
4478 /* add a pristine spare to the metadata */
4479 if (dl
->index
< 0) {
4480 dl
->index
= super
->anchor
->num_disks
;
4481 super
->anchor
->num_disks
++;
4483 /* Check the device has not already been added */
4484 slot
= get_imsm_disk_slot(map
, dl
->index
);
4486 (get_imsm_ord_tbl_ent(dev
, slot
, -1) & IMSM_ORD_REBUILD
) == 0) {
4487 fprintf(stderr
, Name
": %s has been included in this array twice\n",
4491 set_imsm_ord_tbl_ent(map
, dk
->raid_disk
, dl
->index
);
4492 dl
->disk
.status
= CONFIGURED_DISK
;
4494 /* update size of 'missing' disks to be at least as large as the
4495 * largest acitve member (we only have dummy missing disks when
4496 * creating the first volume)
4498 if (super
->current_vol
== 0) {
4499 for (df
= super
->missing
; df
; df
= df
->next
) {
4500 if (dl
->disk
.total_blocks
> df
->disk
.total_blocks
)
4501 df
->disk
.total_blocks
= dl
->disk
.total_blocks
;
4502 _disk
= __get_imsm_disk(mpb
, df
->index
);
4507 /* refresh unset/failed slots to point to valid 'missing' entries */
4508 for (df
= super
->missing
; df
; df
= df
->next
)
4509 for (slot
= 0; slot
< mpb
->num_disks
; slot
++) {
4510 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, -1);
4512 if ((ord
& IMSM_ORD_REBUILD
) == 0)
4514 set_imsm_ord_tbl_ent(map
, slot
, df
->index
| IMSM_ORD_REBUILD
);
4515 if (is_gen_migration(dev
)) {
4516 struct imsm_map
*map2
= get_imsm_map(dev
, 1);
4517 if (slot
< map2
->num_members
) {
4518 __u32 ord2
= get_imsm_ord_tbl_ent(dev
,
4521 if ((unsigned)df
->index
==
4523 set_imsm_ord_tbl_ent(map2
,
4529 dprintf("set slot:%d to missing disk:%d\n", slot
, df
->index
);
4533 /* if we are creating the first raid device update the family number */
4534 if (super
->current_vol
== 0) {
4536 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
4538 _disk
= __get_imsm_disk(mpb
, dl
->index
);
4539 if (!_dev
|| !_disk
) {
4540 fprintf(stderr
, Name
": BUG mpb setup error\n");
4546 sum
+= __gen_imsm_checksum(mpb
);
4547 mpb
->family_num
= __cpu_to_le32(sum
);
4548 mpb
->orig_family_num
= mpb
->family_num
;
4550 super
->current_disk
= dl
;
4555 * Function marks disk as spare and restores disk serial
4556 * in case it was previously marked as failed by takeover operation
4558 * -1 : critical error
4559 * 0 : disk is marked as spare but serial is not set
4562 int mark_spare(struct dl
*disk
)
4564 __u8 serial
[MAX_RAID_SERIAL_LEN
];
4571 if (!imsm_read_serial(disk
->fd
, NULL
, serial
)) {
4572 /* Restore disk serial number, because takeover marks disk
4573 * as failed and adds to serial ':0' before it becomes
4576 serialcpy(disk
->serial
, serial
);
4577 serialcpy(disk
->disk
.serial
, serial
);
4580 disk
->disk
.status
= SPARE_DISK
;
4586 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
4587 int fd
, char *devname
)
4589 struct intel_super
*super
= st
->sb
;
4591 unsigned long long size
;
4596 /* If we are on an RAID enabled platform check that the disk is
4597 * attached to the raid controller.
4598 * We do not need to test disks attachment for container based additions,
4599 * they shall be already tested when container was created/assembled.
4601 rv
= find_intel_hba_capability(fd
, super
, devname
);
4602 /* no orom/efi or non-intel hba of the disk */
4604 dprintf("capability: %p fd: %d ret: %d\n",
4605 super
->orom
, fd
, rv
);
4609 if (super
->current_vol
>= 0)
4610 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
4613 dd
= malloc(sizeof(*dd
));
4616 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
4619 memset(dd
, 0, sizeof(*dd
));
4620 dd
->major
= major(stb
.st_rdev
);
4621 dd
->minor
= minor(stb
.st_rdev
);
4622 dd
->devname
= devname
? strdup(devname
) : NULL
;
4625 dd
->action
= DISK_ADD
;
4626 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
4629 Name
": failed to retrieve scsi serial, aborting\n");
4634 get_dev_size(fd
, NULL
, &size
);
4636 serialcpy(dd
->disk
.serial
, dd
->serial
);
4637 dd
->disk
.total_blocks
= __cpu_to_le32(size
);
4639 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
4640 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
4642 dd
->disk
.scsi_id
= __cpu_to_le32(0);
4644 if (st
->update_tail
) {
4645 dd
->next
= super
->disk_mgmt_list
;
4646 super
->disk_mgmt_list
= dd
;
4648 dd
->next
= super
->disks
;
4650 super
->updates_pending
++;
4657 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
4659 struct intel_super
*super
= st
->sb
;
4662 /* remove from super works only in mdmon - for communication
4663 * manager - monitor. Check if communication memory buffer
4666 if (!st
->update_tail
) {
4668 Name
": %s shall be used in mdmon context only"
4669 "(line %d).\n", __func__
, __LINE__
);
4672 dd
= malloc(sizeof(*dd
));
4675 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
4678 memset(dd
, 0, sizeof(*dd
));
4679 dd
->major
= dk
->major
;
4680 dd
->minor
= dk
->minor
;
4683 dd
->action
= DISK_REMOVE
;
4685 dd
->next
= super
->disk_mgmt_list
;
4686 super
->disk_mgmt_list
= dd
;
4692 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
4696 struct imsm_super anchor
;
4697 } spare_record
__attribute__ ((aligned(512)));
4699 /* spare records have their own family number and do not have any defined raid
4702 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
4704 struct imsm_super
*mpb
= super
->anchor
;
4705 struct imsm_super
*spare
= &spare_record
.anchor
;
4709 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
)),
4710 spare
->generation_num
= __cpu_to_le32(1UL),
4711 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
4712 spare
->num_disks
= 1,
4713 spare
->num_raid_devs
= 0,
4714 spare
->cache_size
= mpb
->cache_size
,
4715 spare
->pwr_cycle_count
= __cpu_to_le32(1),
4717 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
4718 MPB_SIGNATURE MPB_VERSION_RAID0
);
4720 for (d
= super
->disks
; d
; d
= d
->next
) {
4724 spare
->disk
[0] = d
->disk
;
4725 sum
= __gen_imsm_checksum(spare
);
4726 spare
->family_num
= __cpu_to_le32(sum
);
4727 spare
->orig_family_num
= 0;
4728 sum
= __gen_imsm_checksum(spare
);
4729 spare
->check_sum
= __cpu_to_le32(sum
);
4731 if (store_imsm_mpb(d
->fd
, spare
)) {
4732 fprintf(stderr
, "%s: failed for device %d:%d %s\n",
4733 __func__
, d
->major
, d
->minor
, strerror(errno
));
4745 static int write_super_imsm(struct supertype
*st
, int doclose
)
4747 struct intel_super
*super
= st
->sb
;
4748 struct imsm_super
*mpb
= super
->anchor
;
4754 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
4756 int clear_migration_record
= 1;
4758 /* 'generation' is incremented everytime the metadata is written */
4759 generation
= __le32_to_cpu(mpb
->generation_num
);
4761 mpb
->generation_num
= __cpu_to_le32(generation
);
4763 /* fix up cases where previous mdadm releases failed to set
4766 if (mpb
->orig_family_num
== 0)
4767 mpb
->orig_family_num
= mpb
->family_num
;
4769 for (d
= super
->disks
; d
; d
= d
->next
) {
4773 mpb
->disk
[d
->index
] = d
->disk
;
4777 for (d
= super
->missing
; d
; d
= d
->next
) {
4778 mpb
->disk
[d
->index
] = d
->disk
;
4781 mpb
->num_disks
= num_disks
;
4782 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
4784 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4785 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
4786 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
4788 imsm_copy_dev(dev
, dev2
);
4789 mpb_size
+= sizeof_imsm_dev(dev
, 0);
4791 if (is_gen_migration(dev2
))
4792 clear_migration_record
= 0;
4794 mpb_size
+= __le32_to_cpu(mpb
->bbm_log_size
);
4795 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
4797 /* recalculate checksum */
4798 sum
= __gen_imsm_checksum(mpb
);
4799 mpb
->check_sum
= __cpu_to_le32(sum
);
4801 if (clear_migration_record
)
4802 memset(super
->migr_rec_buf
, 0, 512);
4804 /* write the mpb for disks that compose raid devices */
4805 for (d
= super
->disks
; d
; d
= d
->next
) {
4806 if (d
->index
< 0 || is_failed(&d
->disk
))
4808 if (store_imsm_mpb(d
->fd
, mpb
))
4809 fprintf(stderr
, "%s: failed for device %d:%d %s\n",
4810 __func__
, d
->major
, d
->minor
, strerror(errno
));
4811 if (clear_migration_record
) {
4812 unsigned long long dsize
;
4814 get_dev_size(d
->fd
, NULL
, &dsize
);
4815 if (lseek64(d
->fd
, dsize
- 512, SEEK_SET
) >= 0) {
4816 if (write(d
->fd
, super
->migr_rec_buf
, 512) != 512)
4817 perror("Write migr_rec failed");
4827 return write_super_imsm_spares(super
, doclose
);
4833 static int create_array(struct supertype
*st
, int dev_idx
)
4836 struct imsm_update_create_array
*u
;
4837 struct intel_super
*super
= st
->sb
;
4838 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
4839 struct imsm_map
*map
= get_imsm_map(dev
, 0);
4840 struct disk_info
*inf
;
4841 struct imsm_disk
*disk
;
4844 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
4845 sizeof(*inf
) * map
->num_members
;
4848 fprintf(stderr
, "%s: failed to allocate update buffer\n",
4853 u
->type
= update_create_array
;
4854 u
->dev_idx
= dev_idx
;
4855 imsm_copy_dev(&u
->dev
, dev
);
4856 inf
= get_disk_info(u
);
4857 for (i
= 0; i
< map
->num_members
; i
++) {
4858 int idx
= get_imsm_disk_idx(dev
, i
, -1);
4860 disk
= get_imsm_disk(super
, idx
);
4861 serialcpy(inf
[i
].serial
, disk
->serial
);
4863 append_metadata_update(st
, u
, len
);
4868 static int mgmt_disk(struct supertype
*st
)
4870 struct intel_super
*super
= st
->sb
;
4872 struct imsm_update_add_remove_disk
*u
;
4874 if (!super
->disk_mgmt_list
)
4880 fprintf(stderr
, "%s: failed to allocate update buffer\n",
4885 u
->type
= update_add_remove_disk
;
4886 append_metadata_update(st
, u
, len
);
4891 static int write_init_super_imsm(struct supertype
*st
)
4893 struct intel_super
*super
= st
->sb
;
4894 int current_vol
= super
->current_vol
;
4896 /* we are done with current_vol reset it to point st at the container */
4897 super
->current_vol
= -1;
4899 if (st
->update_tail
) {
4900 /* queue the recently created array / added disk
4901 * as a metadata update */
4904 /* determine if we are creating a volume or adding a disk */
4905 if (current_vol
< 0) {
4906 /* in the mgmt (add/remove) disk case we are running
4907 * in mdmon context, so don't close fd's
4909 return mgmt_disk(st
);
4911 rv
= create_array(st
, current_vol
);
4916 for (d
= super
->disks
; d
; d
= d
->next
)
4917 Kill(d
->devname
, NULL
, 0, 1, 1);
4918 return write_super_imsm(st
, 1);
4923 static int store_super_imsm(struct supertype
*st
, int fd
)
4925 struct intel_super
*super
= st
->sb
;
4926 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
4932 return store_imsm_mpb(fd
, mpb
);
4938 static int imsm_bbm_log_size(struct imsm_super
*mpb
)
4940 return __le32_to_cpu(mpb
->bbm_log_size
);
4944 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
4945 int layout
, int raiddisks
, int chunk
,
4946 unsigned long long size
, char *dev
,
4947 unsigned long long *freesize
,
4951 unsigned long long ldsize
;
4952 struct intel_super
*super
=NULL
;
4955 if (level
!= LEVEL_CONTAINER
)
4960 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
4963 fprintf(stderr
, Name
": imsm: Cannot open %s: %s\n",
4964 dev
, strerror(errno
));
4967 if (!get_dev_size(fd
, dev
, &ldsize
)) {
4972 /* capabilities retrieve could be possible
4973 * note that there is no fd for the disks in array.
4975 super
= alloc_super();
4978 Name
": malloc of %zu failed.\n",
4984 rv
= find_intel_hba_capability(fd
, super
, verbose
? dev
: NULL
);
4988 fd2devname(fd
, str
);
4989 dprintf("validate_geometry_imsm_container: fd: %d %s orom: %p rv: %d raiddisk: %d\n",
4990 fd
, str
, super
->orom
, rv
, raiddisks
);
4992 /* no orom/efi or non-intel hba of the disk */
4998 if (super
->orom
&& raiddisks
> super
->orom
->tds
) {
5000 fprintf(stderr
, Name
": %d exceeds maximum number of"
5001 " platform supported disks: %d\n",
5002 raiddisks
, super
->orom
->tds
);
5008 *freesize
= avail_size_imsm(st
, ldsize
>> 9);
5014 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
5016 const unsigned long long base_start
= e
[*idx
].start
;
5017 unsigned long long end
= base_start
+ e
[*idx
].size
;
5020 if (base_start
== end
)
5024 for (i
= *idx
; i
< num_extents
; i
++) {
5025 /* extend overlapping extents */
5026 if (e
[i
].start
>= base_start
&&
5027 e
[i
].start
<= end
) {
5030 if (e
[i
].start
+ e
[i
].size
> end
)
5031 end
= e
[i
].start
+ e
[i
].size
;
5032 } else if (e
[i
].start
> end
) {
5038 return end
- base_start
;
5041 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
5043 /* build a composite disk with all known extents and generate a new
5044 * 'maxsize' given the "all disks in an array must share a common start
5045 * offset" constraint
5047 struct extent
*e
= calloc(sum_extents
, sizeof(*e
));
5051 unsigned long long pos
;
5052 unsigned long long start
= 0;
5053 unsigned long long maxsize
;
5054 unsigned long reserve
;
5059 /* coalesce and sort all extents. also, check to see if we need to
5060 * reserve space between member arrays
5063 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5066 for (i
= 0; i
< dl
->extent_cnt
; i
++)
5069 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
5074 while (i
< sum_extents
) {
5075 e
[j
].start
= e
[i
].start
;
5076 e
[j
].size
= find_size(e
, &i
, sum_extents
);
5078 if (e
[j
-1].size
== 0)
5087 unsigned long long esize
;
5089 esize
= e
[i
].start
- pos
;
5090 if (esize
>= maxsize
) {
5095 pos
= e
[i
].start
+ e
[i
].size
;
5097 } while (e
[i
-1].size
);
5103 /* FIXME assumes volume at offset 0 is the first volume in a
5106 if (start_extent
> 0)
5107 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
5111 if (maxsize
< reserve
)
5114 super
->create_offset
= ~((__u32
) 0);
5115 if (start
+ reserve
> super
->create_offset
)
5116 return 0; /* start overflows create_offset */
5117 super
->create_offset
= start
+ reserve
;
5119 return maxsize
- reserve
;
5122 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
5124 if (level
< 0 || level
== 6 || level
== 4)
5127 /* if we have an orom prevent invalid raid levels */
5130 case 0: return imsm_orom_has_raid0(orom
);
5133 return imsm_orom_has_raid1e(orom
);
5134 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
5135 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
5136 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
5139 return 1; /* not on an Intel RAID platform so anything goes */
5144 static int imsm_default_chunk(const struct imsm_orom
*orom
)
5146 /* up to 512 if the plaform supports it, otherwise the platform max.
5147 * 128 if no platform detected
5149 int fs
= max(7, orom
? fls(orom
->sss
) : 0);
5151 return min(512, (1 << fs
));
5154 #define pr_vrb(fmt, arg...) (void) (verbose && fprintf(stderr, Name fmt, ##arg))
5156 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
5157 int raiddisks
, int *chunk
, int verbose
)
5159 /* check/set platform and metadata limits/defaults */
5160 if (super
->orom
&& raiddisks
> super
->orom
->dpa
) {
5161 pr_vrb(": platform supports a maximum of %d disks per array\n",
5166 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
5167 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
5168 pr_vrb(": platform does not support raid%d with %d disk%s\n",
5169 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
5173 if (chunk
&& (*chunk
== 0 || *chunk
== UnSet
))
5174 *chunk
= imsm_default_chunk(super
->orom
);
5176 if (super
->orom
&& chunk
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
5177 pr_vrb(": platform does not support a chunk size of: "
5182 if (layout
!= imsm_level_to_layout(level
)) {
5184 pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
5185 else if (level
== 10)
5186 pr_vrb(": imsm raid 10 only supports the n2 layout\n");
5188 pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
5195 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
5196 * FIX ME add ahci details
5198 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
5199 int layout
, int raiddisks
, int *chunk
,
5200 unsigned long long size
, char *dev
,
5201 unsigned long long *freesize
,
5205 struct intel_super
*super
= st
->sb
;
5206 struct imsm_super
*mpb
;
5208 unsigned long long pos
= 0;
5209 unsigned long long maxsize
;
5213 /* We must have the container info already read in. */
5217 mpb
= super
->anchor
;
5219 if (mpb
->num_raid_devs
> 0 && mpb
->num_disks
!= raiddisks
) {
5220 fprintf(stderr
, Name
": the option-rom requires all "
5221 "member disks to be a member of all volumes.\n");
5225 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, verbose
)) {
5226 fprintf(stderr
, Name
": RAID gemetry validation failed. "
5227 "Cannot proceed with the action(s).\n");
5231 /* General test: make sure there is space for
5232 * 'raiddisks' device extents of size 'size' at a given
5235 unsigned long long minsize
= size
;
5236 unsigned long long start_offset
= MaxSector
;
5239 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
5240 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5245 e
= get_extents(super
, dl
);
5248 unsigned long long esize
;
5249 esize
= e
[i
].start
- pos
;
5250 if (esize
>= minsize
)
5252 if (found
&& start_offset
== MaxSector
) {
5255 } else if (found
&& pos
!= start_offset
) {
5259 pos
= e
[i
].start
+ e
[i
].size
;
5261 } while (e
[i
-1].size
);
5266 if (dcnt
< raiddisks
) {
5268 fprintf(stderr
, Name
": imsm: Not enough "
5269 "devices with space for this array "
5277 /* This device must be a member of the set */
5278 if (stat(dev
, &stb
) < 0)
5280 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
5282 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5283 if (dl
->major
== (int)major(stb
.st_rdev
) &&
5284 dl
->minor
== (int)minor(stb
.st_rdev
))
5289 fprintf(stderr
, Name
": %s is not in the "
5290 "same imsm set\n", dev
);
5292 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
5293 /* If a volume is present then the current creation attempt
5294 * cannot incorporate new spares because the orom may not
5295 * understand this configuration (all member disks must be
5296 * members of each array in the container).
5298 fprintf(stderr
, Name
": %s is a spare and a volume"
5299 " is already defined for this container\n", dev
);
5300 fprintf(stderr
, Name
": The option-rom requires all member"
5301 " disks to be a member of all volumes\n");
5305 /* retrieve the largest free space block */
5306 e
= get_extents(super
, dl
);
5311 unsigned long long esize
;
5313 esize
= e
[i
].start
- pos
;
5314 if (esize
>= maxsize
)
5316 pos
= e
[i
].start
+ e
[i
].size
;
5318 } while (e
[i
-1].size
);
5323 fprintf(stderr
, Name
": unable to determine free space for: %s\n",
5327 if (maxsize
< size
) {
5329 fprintf(stderr
, Name
": %s not enough space (%llu < %llu)\n",
5330 dev
, maxsize
, size
);
5334 /* count total number of extents for merge */
5336 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5338 i
+= dl
->extent_cnt
;
5340 maxsize
= merge_extents(super
, i
);
5342 if (!check_env("IMSM_NO_PLATFORM") &&
5343 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
5344 fprintf(stderr
, Name
": attempting to create a second "
5345 "volume with size less then remaining space. "
5350 if (maxsize
< size
|| maxsize
== 0) {
5352 fprintf(stderr
, Name
": not enough space after merge (%llu < %llu)\n",
5357 *freesize
= maxsize
;
5362 static int reserve_space(struct supertype
*st
, int raiddisks
,
5363 unsigned long long size
, int chunk
,
5364 unsigned long long *freesize
)
5366 struct intel_super
*super
= st
->sb
;
5367 struct imsm_super
*mpb
= super
->anchor
;
5372 unsigned long long maxsize
;
5373 unsigned long long minsize
;
5377 /* find the largest common start free region of the possible disks */
5381 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5387 /* don't activate new spares if we are orom constrained
5388 * and there is already a volume active in the container
5390 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
5393 e
= get_extents(super
, dl
);
5396 for (i
= 1; e
[i
-1].size
; i
++)
5404 maxsize
= merge_extents(super
, extent_cnt
);
5408 minsize
= chunk
* 2;
5410 if (cnt
< raiddisks
||
5411 (super
->orom
&& used
&& used
!= raiddisks
) ||
5412 maxsize
< minsize
||
5414 fprintf(stderr
, Name
": not enough devices with space to create array.\n");
5415 return 0; /* No enough free spaces large enough */
5427 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5429 dl
->raiddisk
= cnt
++;
5436 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
5437 int raiddisks
, int *chunk
, unsigned long long size
,
5438 char *dev
, unsigned long long *freesize
,
5446 * if given unused devices create a container
5447 * if given given devices in a container create a member volume
5449 if (level
== LEVEL_CONTAINER
) {
5450 /* Must be a fresh device to add to a container */
5451 return validate_geometry_imsm_container(st
, level
, layout
,
5453 chunk
?*chunk
:0, size
,
5460 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
5464 /* we are being asked to automatically layout a
5465 * new volume based on the current contents of
5466 * the container. If the the parameters can be
5467 * satisfied reserve_space will record the disks,
5468 * start offset, and size of the volume to be
5469 * created. add_to_super and getinfo_super
5470 * detect when autolayout is in progress.
5473 return reserve_space(st
, raiddisks
, size
,
5474 chunk
?*chunk
:0, freesize
);
5479 /* creating in a given container */
5480 return validate_geometry_imsm_volume(st
, level
, layout
,
5481 raiddisks
, chunk
, size
,
5482 dev
, freesize
, verbose
);
5485 /* This device needs to be a device in an 'imsm' container */
5486 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
5490 Name
": Cannot create this array on device %s\n",
5495 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
5497 fprintf(stderr
, Name
": Cannot open %s: %s\n",
5498 dev
, strerror(errno
));
5501 /* Well, it is in use by someone, maybe an 'imsm' container. */
5502 cfd
= open_container(fd
);
5506 fprintf(stderr
, Name
": Cannot use %s: It is busy\n",
5510 sra
= sysfs_read(cfd
, 0, GET_VERSION
);
5511 if (sra
&& sra
->array
.major_version
== -1 &&
5512 strcmp(sra
->text_version
, "imsm") == 0)
5516 /* This is a member of a imsm container. Load the container
5517 * and try to create a volume
5519 struct intel_super
*super
;
5521 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
) == 0) {
5523 st
->container_dev
= fd2devnum(cfd
);
5525 return validate_geometry_imsm_volume(st
, level
, layout
,
5534 fprintf(stderr
, Name
": failed container membership check\n");
5540 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
5542 struct intel_super
*super
= st
->sb
;
5544 if (level
&& *level
== UnSet
)
5545 *level
= LEVEL_CONTAINER
;
5547 if (level
&& layout
&& *layout
== UnSet
)
5548 *layout
= imsm_level_to_layout(*level
);
5550 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0))
5551 *chunk
= imsm_default_chunk(super
->orom
);
5554 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
5556 static int kill_subarray_imsm(struct supertype
*st
)
5558 /* remove the subarray currently referenced by ->current_vol */
5560 struct intel_dev
**dp
;
5561 struct intel_super
*super
= st
->sb
;
5562 __u8 current_vol
= super
->current_vol
;
5563 struct imsm_super
*mpb
= super
->anchor
;
5565 if (super
->current_vol
< 0)
5567 super
->current_vol
= -1; /* invalidate subarray cursor */
5569 /* block deletions that would change the uuid of active subarrays
5571 * FIXME when immutable ids are available, but note that we'll
5572 * also need to fixup the invalidated/active subarray indexes in
5575 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5578 if (i
< current_vol
)
5580 sprintf(subarray
, "%u", i
);
5581 if (is_subarray_active(subarray
, st
->devname
)) {
5583 Name
": deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
5590 if (st
->update_tail
) {
5591 struct imsm_update_kill_array
*u
= malloc(sizeof(*u
));
5595 u
->type
= update_kill_array
;
5596 u
->dev_idx
= current_vol
;
5597 append_metadata_update(st
, u
, sizeof(*u
));
5602 for (dp
= &super
->devlist
; *dp
;)
5603 if ((*dp
)->index
== current_vol
) {
5606 handle_missing(super
, (*dp
)->dev
);
5607 if ((*dp
)->index
> current_vol
)
5612 /* no more raid devices, all active components are now spares,
5613 * but of course failed are still failed
5615 if (--mpb
->num_raid_devs
== 0) {
5618 for (d
= super
->disks
; d
; d
= d
->next
)
5623 super
->updates_pending
++;
5628 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
5629 char *update
, struct mddev_ident
*ident
)
5631 /* update the subarray currently referenced by ->current_vol */
5632 struct intel_super
*super
= st
->sb
;
5633 struct imsm_super
*mpb
= super
->anchor
;
5635 if (strcmp(update
, "name") == 0) {
5636 char *name
= ident
->name
;
5640 if (is_subarray_active(subarray
, st
->devname
)) {
5642 Name
": Unable to update name of active subarray\n");
5646 if (!check_name(super
, name
, 0))
5649 vol
= strtoul(subarray
, &ep
, 10);
5650 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
5653 if (st
->update_tail
) {
5654 struct imsm_update_rename_array
*u
= malloc(sizeof(*u
));
5658 u
->type
= update_rename_array
;
5660 snprintf((char *) u
->name
, MAX_RAID_SERIAL_LEN
, "%s", name
);
5661 append_metadata_update(st
, u
, sizeof(*u
));
5663 struct imsm_dev
*dev
;
5666 dev
= get_imsm_dev(super
, vol
);
5667 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
5668 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5669 dev
= get_imsm_dev(super
, i
);
5670 handle_missing(super
, dev
);
5672 super
->updates_pending
++;
5680 static int is_gen_migration(struct imsm_dev
*dev
)
5685 if (!dev
->vol
.migr_state
)
5688 if (migr_type(dev
) == MIGR_GEN_MIGR
)
5693 #endif /* MDASSEMBLE */
5695 static int is_rebuilding(struct imsm_dev
*dev
)
5697 struct imsm_map
*migr_map
;
5699 if (!dev
->vol
.migr_state
)
5702 if (migr_type(dev
) != MIGR_REBUILD
)
5705 migr_map
= get_imsm_map(dev
, 1);
5707 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
5713 static void update_recovery_start(struct intel_super
*super
,
5714 struct imsm_dev
*dev
,
5715 struct mdinfo
*array
)
5717 struct mdinfo
*rebuild
= NULL
;
5721 if (!is_rebuilding(dev
))
5724 /* Find the rebuild target, but punt on the dual rebuild case */
5725 for (d
= array
->devs
; d
; d
= d
->next
)
5726 if (d
->recovery_start
== 0) {
5733 /* (?) none of the disks are marked with
5734 * IMSM_ORD_REBUILD, so assume they are missing and the
5735 * disk_ord_tbl was not correctly updated
5737 dprintf("%s: failed to locate out-of-sync disk\n", __func__
);
5741 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
5742 rebuild
->recovery_start
= units
* blocks_per_migr_unit(super
, dev
);
5746 static int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
);
5749 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
5751 /* Given a container loaded by load_super_imsm_all,
5752 * extract information about all the arrays into
5754 * If 'subarray' is given, just extract info about that array.
5756 * For each imsm_dev create an mdinfo, fill it in,
5757 * then look for matching devices in super->disks
5758 * and create appropriate device mdinfo.
5760 struct intel_super
*super
= st
->sb
;
5761 struct imsm_super
*mpb
= super
->anchor
;
5762 struct mdinfo
*rest
= NULL
;
5766 int spare_disks
= 0;
5768 /* do not assemble arrays when not all attributes are supported */
5769 if (imsm_check_attributes(mpb
->attributes
) == 0) {
5771 fprintf(stderr
, Name
": Unsupported attributes in IMSM metadata."
5772 "Arrays activation is blocked.\n");
5775 /* check for bad blocks */
5776 if (imsm_bbm_log_size(super
->anchor
)) {
5777 fprintf(stderr
, Name
": BBM log found in IMSM metadata."
5778 "Arrays activation is blocked.\n");
5783 /* count spare devices, not used in maps
5785 for (d
= super
->disks
; d
; d
= d
->next
)
5789 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5790 struct imsm_dev
*dev
;
5791 struct imsm_map
*map
;
5792 struct imsm_map
*map2
;
5793 struct mdinfo
*this;
5798 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
5801 dev
= get_imsm_dev(super
, i
);
5802 map
= get_imsm_map(dev
, 0);
5803 map2
= get_imsm_map(dev
, 1);
5805 /* do not publish arrays that are in the middle of an
5806 * unsupported migration
5808 if (dev
->vol
.migr_state
&&
5809 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
5810 fprintf(stderr
, Name
": cannot assemble volume '%.16s':"
5811 " unsupported migration in progress\n",
5815 /* do not publish arrays that are not support by controller's
5819 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
5820 this = malloc(sizeof(*this));
5822 fprintf(stderr
, Name
": failed to allocate %zu bytes\n",
5827 super
->current_vol
= i
;
5828 getinfo_super_imsm_volume(st
, this, NULL
);
5831 /* mdadm does not support all metadata features- set the bit in all arrays state */
5832 if (!validate_geometry_imsm_orom(super
,
5833 get_imsm_raid_level(map
), /* RAID level */
5834 imsm_level_to_layout(get_imsm_raid_level(map
)),
5835 map
->num_members
, /* raid disks */
5838 fprintf(stderr
, Name
": IMSM RAID geometry validation"
5839 " failed. Array %s activation is blocked.\n",
5841 this->array
.state
|=
5842 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
5843 (1<<MD_SB_BLOCK_VOLUME
);
5847 /* if array has bad blocks, set suitable bit in all arrays state */
5849 this->array
.state
|=
5850 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
5851 (1<<MD_SB_BLOCK_VOLUME
);
5853 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
5854 unsigned long long recovery_start
;
5855 struct mdinfo
*info_d
;
5862 idx
= get_imsm_disk_idx(dev
, slot
, 0);
5863 ord
= get_imsm_ord_tbl_ent(dev
, slot
, -1);
5864 for (d
= super
->disks
; d
; d
= d
->next
)
5865 if (d
->index
== idx
)
5868 recovery_start
= MaxSector
;
5871 if (d
&& is_failed(&d
->disk
))
5873 if (ord
& IMSM_ORD_REBUILD
)
5877 * if we skip some disks the array will be assmebled degraded;
5878 * reset resync start to avoid a dirty-degraded
5879 * situation when performing the intial sync
5881 * FIXME handle dirty degraded
5883 if ((skip
|| recovery_start
== 0) && !dev
->vol
.dirty
)
5884 this->resync_start
= MaxSector
;
5888 info_d
= calloc(1, sizeof(*info_d
));
5890 fprintf(stderr
, Name
": failed to allocate disk"
5891 " for volume %.16s\n", dev
->volume
);
5892 info_d
= this->devs
;
5894 struct mdinfo
*d
= info_d
->next
;
5903 info_d
->next
= this->devs
;
5904 this->devs
= info_d
;
5906 info_d
->disk
.number
= d
->index
;
5907 info_d
->disk
.major
= d
->major
;
5908 info_d
->disk
.minor
= d
->minor
;
5909 info_d
->disk
.raid_disk
= slot
;
5910 info_d
->recovery_start
= recovery_start
;
5912 if (slot
< map2
->num_members
)
5913 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
5915 this->array
.spare_disks
++;
5917 if (slot
< map
->num_members
)
5918 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
5920 this->array
.spare_disks
++;
5922 if (info_d
->recovery_start
== MaxSector
)
5923 this->array
.working_disks
++;
5925 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
5926 info_d
->data_offset
= __le32_to_cpu(map
->pba_of_lba0
);
5927 info_d
->component_size
= __le32_to_cpu(map
->blocks_per_member
);
5929 /* now that the disk list is up-to-date fixup recovery_start */
5930 update_recovery_start(super
, dev
, this);
5931 this->array
.spare_disks
+= spare_disks
;
5934 /* check for reshape */
5935 if (this->reshape_active
== 1)
5936 recover_backup_imsm(st
, this);
5945 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
5946 int failed
, int look_in_map
)
5948 struct imsm_map
*map
;
5950 map
= get_imsm_map(dev
, look_in_map
);
5953 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
5954 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
5956 switch (get_imsm_raid_level(map
)) {
5958 return IMSM_T_STATE_FAILED
;
5961 if (failed
< map
->num_members
)
5962 return IMSM_T_STATE_DEGRADED
;
5964 return IMSM_T_STATE_FAILED
;
5969 * check to see if any mirrors have failed, otherwise we
5970 * are degraded. Even numbered slots are mirrored on
5974 /* gcc -Os complains that this is unused */
5975 int insync
= insync
;
5977 for (i
= 0; i
< map
->num_members
; i
++) {
5978 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, -1);
5979 int idx
= ord_to_idx(ord
);
5980 struct imsm_disk
*disk
;
5982 /* reset the potential in-sync count on even-numbered
5983 * slots. num_copies is always 2 for imsm raid10
5988 disk
= get_imsm_disk(super
, idx
);
5989 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
5992 /* no in-sync disks left in this mirror the
5996 return IMSM_T_STATE_FAILED
;
5999 return IMSM_T_STATE_DEGRADED
;
6003 return IMSM_T_STATE_DEGRADED
;
6005 return IMSM_T_STATE_FAILED
;
6011 return map
->map_state
;
6014 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
6019 struct imsm_disk
*disk
;
6020 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6021 struct imsm_map
*prev
= get_imsm_map(dev
, MAP_1
);
6022 struct imsm_map
*map_for_loop
;
6027 /* at the beginning of migration we set IMSM_ORD_REBUILD on
6028 * disks that are being rebuilt. New failures are recorded to
6029 * map[0]. So we look through all the disks we started with and
6030 * see if any failures are still present, or if any new ones
6034 if (prev
&& (map
->num_members
< prev
->num_members
))
6035 map_for_loop
= prev
;
6037 for (i
= 0; i
< map_for_loop
->num_members
; i
++) {
6040 (look_in_map
& MAP_1
) && (i
< prev
->num_members
)) {
6041 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
6042 idx_1
= ord_to_idx(ord
);
6044 disk
= get_imsm_disk(super
, idx_1
);
6045 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
6048 if ((look_in_map
& MAP_0
) && (i
< map
->num_members
)) {
6049 ord
= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
6050 idx
= ord_to_idx(ord
);
6053 disk
= get_imsm_disk(super
, idx
);
6054 if (!disk
|| is_failed(disk
) ||
6055 ord
& IMSM_ORD_REBUILD
)
6065 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
6068 struct intel_super
*super
= c
->sb
;
6069 struct imsm_super
*mpb
= super
->anchor
;
6071 if (atoi(inst
) >= mpb
->num_raid_devs
) {
6072 fprintf(stderr
, "%s: subarry index %d, out of range\n",
6073 __func__
, atoi(inst
));
6077 dprintf("imsm: open_new %s\n", inst
);
6078 a
->info
.container_member
= atoi(inst
);
6082 static int is_resyncing(struct imsm_dev
*dev
)
6084 struct imsm_map
*migr_map
;
6086 if (!dev
->vol
.migr_state
)
6089 if (migr_type(dev
) == MIGR_INIT
||
6090 migr_type(dev
) == MIGR_REPAIR
)
6093 if (migr_type(dev
) == MIGR_GEN_MIGR
)
6096 migr_map
= get_imsm_map(dev
, 1);
6098 if ((migr_map
->map_state
== IMSM_T_STATE_NORMAL
) &&
6099 (dev
->vol
.migr_type
!= MIGR_GEN_MIGR
))
6105 /* return true if we recorded new information */
6106 static int mark_failure(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
6110 struct imsm_map
*map
;
6111 char buf
[MAX_RAID_SERIAL_LEN
+3];
6112 unsigned int len
, shift
= 0;
6114 /* new failures are always set in map[0] */
6115 map
= get_imsm_map(dev
, 0);
6117 slot
= get_imsm_disk_slot(map
, idx
);
6121 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
6122 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
6125 memcpy(buf
, disk
->serial
, MAX_RAID_SERIAL_LEN
);
6126 buf
[MAX_RAID_SERIAL_LEN
] = '\000';
6128 if ((len
= strlen(buf
)) >= MAX_RAID_SERIAL_LEN
)
6129 shift
= len
- MAX_RAID_SERIAL_LEN
+ 1;
6130 strncpy((char *)disk
->serial
, &buf
[shift
], MAX_RAID_SERIAL_LEN
);
6132 disk
->status
|= FAILED_DISK
;
6133 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
6134 if (is_gen_migration(dev
)) {
6135 struct imsm_map
*map2
= get_imsm_map(dev
, 1);
6136 if (slot
< map2
->num_members
)
6137 set_imsm_ord_tbl_ent(map2
, slot
,
6138 idx
| IMSM_ORD_REBUILD
);
6140 if (map
->failed_disk_num
== 0xff)
6141 map
->failed_disk_num
= slot
;
6145 static void mark_missing(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
6147 mark_failure(dev
, disk
, idx
);
6149 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
6152 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
6153 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
6156 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
6160 if (!super
->missing
)
6163 dprintf("imsm: mark missing\n");
6164 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
6165 mark_missing(dev
, &dl
->disk
, dl
->index
);
6166 super
->updates_pending
++;
6169 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
)
6171 int used_disks
= imsm_num_data_members(dev
, 0);
6172 unsigned long long array_blocks
;
6173 struct imsm_map
*map
;
6175 if (used_disks
== 0) {
6176 /* when problems occures
6177 * return current array_blocks value
6179 array_blocks
= __le32_to_cpu(dev
->size_high
);
6180 array_blocks
= array_blocks
<< 32;
6181 array_blocks
+= __le32_to_cpu(dev
->size_low
);
6183 return array_blocks
;
6186 /* set array size in metadata
6188 map
= get_imsm_map(dev
, 0);
6189 array_blocks
= map
->blocks_per_member
* used_disks
;
6191 /* round array size down to closest MB
6193 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
6194 dev
->size_low
= __cpu_to_le32((__u32
)array_blocks
);
6195 dev
->size_high
= __cpu_to_le32((__u32
)(array_blocks
>> 32));
6197 return array_blocks
;
6200 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
6202 static void imsm_progress_container_reshape(struct intel_super
*super
)
6204 /* if no device has a migr_state, but some device has a
6205 * different number of members than the previous device, start
6206 * changing the number of devices in this device to match
6209 struct imsm_super
*mpb
= super
->anchor
;
6210 int prev_disks
= -1;
6214 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6215 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
6216 struct imsm_map
*map
= get_imsm_map(dev
, 0);
6217 struct imsm_map
*map2
;
6218 int prev_num_members
;
6220 if (dev
->vol
.migr_state
)
6223 if (prev_disks
== -1)
6224 prev_disks
= map
->num_members
;
6225 if (prev_disks
== map
->num_members
)
6228 /* OK, this array needs to enter reshape mode.
6229 * i.e it needs a migr_state
6232 copy_map_size
= sizeof_imsm_map(map
);
6233 prev_num_members
= map
->num_members
;
6234 map
->num_members
= prev_disks
;
6235 dev
->vol
.migr_state
= 1;
6236 dev
->vol
.curr_migr_unit
= 0;
6237 set_migr_type(dev
, MIGR_GEN_MIGR
);
6238 for (i
= prev_num_members
;
6239 i
< map
->num_members
; i
++)
6240 set_imsm_ord_tbl_ent(map
, i
, i
);
6241 map2
= get_imsm_map(dev
, 1);
6242 /* Copy the current map */
6243 memcpy(map2
, map
, copy_map_size
);
6244 map2
->num_members
= prev_num_members
;
6246 imsm_set_array_size(dev
);
6247 super
->updates_pending
++;
6251 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
6252 * states are handled in imsm_set_disk() with one exception, when a
6253 * resync is stopped due to a new failure this routine will set the
6254 * 'degraded' state for the array.
6256 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
6258 int inst
= a
->info
.container_member
;
6259 struct intel_super
*super
= a
->container
->sb
;
6260 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
6261 struct imsm_map
*map
= get_imsm_map(dev
, 0);
6262 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
6263 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
6264 __u32 blocks_per_unit
;
6266 if (dev
->vol
.migr_state
&&
6267 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
6268 /* array state change is blocked due to reshape action
6270 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
6271 * - finish the reshape (if last_checkpoint is big and action != reshape)
6272 * - update curr_migr_unit
6274 if (a
->curr_action
== reshape
) {
6275 /* still reshaping, maybe update curr_migr_unit */
6276 goto mark_checkpoint
;
6278 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
6279 /* for some reason we aborted the reshape.
6281 * disable automatic metadata rollback
6282 * user action is required to recover process
6285 struct imsm_map
*map2
= get_imsm_map(dev
, 1);
6286 dev
->vol
.migr_state
= 0;
6287 set_migr_type(dev
, 0);
6288 dev
->vol
.curr_migr_unit
= 0;
6289 memcpy(map
, map2
, sizeof_imsm_map(map2
));
6290 super
->updates_pending
++;
6293 if (a
->last_checkpoint
>= a
->info
.component_size
) {
6294 unsigned long long array_blocks
;
6298 used_disks
= imsm_num_data_members(dev
, 0);
6299 if (used_disks
> 0) {
6301 map
->blocks_per_member
*
6303 /* round array size down to closest MB
6305 array_blocks
= (array_blocks
6306 >> SECT_PER_MB_SHIFT
)
6307 << SECT_PER_MB_SHIFT
;
6308 a
->info
.custom_array_size
= array_blocks
;
6309 /* encourage manager to update array
6313 a
->check_reshape
= 1;
6315 /* finalize online capacity expansion/reshape */
6316 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
6318 mdi
->disk
.raid_disk
,
6321 imsm_progress_container_reshape(super
);
6326 /* before we activate this array handle any missing disks */
6327 if (consistent
== 2)
6328 handle_missing(super
, dev
);
6330 if (consistent
== 2 &&
6331 (!is_resync_complete(&a
->info
) ||
6332 map_state
!= IMSM_T_STATE_NORMAL
||
6333 dev
->vol
.migr_state
))
6336 if (is_resync_complete(&a
->info
)) {
6337 /* complete intialization / resync,
6338 * recovery and interrupted recovery is completed in
6341 if (is_resyncing(dev
)) {
6342 dprintf("imsm: mark resync done\n");
6343 end_migration(dev
, map_state
);
6344 super
->updates_pending
++;
6345 a
->last_checkpoint
= 0;
6347 } else if ((!is_resyncing(dev
) && !failed
) &&
6348 (imsm_reshape_blocks_arrays_changes(super
) == 0)) {
6349 /* mark the start of the init process if nothing is failed */
6350 dprintf("imsm: mark resync start\n");
6351 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
6352 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
6354 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
6355 super
->updates_pending
++;
6359 /* skip checkpointing for general migration,
6360 * it is controlled in mdadm
6362 if (is_gen_migration(dev
))
6363 goto skip_mark_checkpoint
;
6365 /* check if we can update curr_migr_unit from resync_start, recovery_start */
6366 blocks_per_unit
= blocks_per_migr_unit(super
, dev
);
6367 if (blocks_per_unit
) {
6371 units
= a
->last_checkpoint
/ blocks_per_unit
;
6374 /* check that we did not overflow 32-bits, and that
6375 * curr_migr_unit needs updating
6377 if (units32
== units
&&
6379 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
6380 dprintf("imsm: mark checkpoint (%u)\n", units32
);
6381 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
6382 super
->updates_pending
++;
6386 skip_mark_checkpoint
:
6387 /* mark dirty / clean */
6388 if (dev
->vol
.dirty
!= !consistent
) {
6389 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
6394 super
->updates_pending
++;
6400 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
6402 int inst
= a
->info
.container_member
;
6403 struct intel_super
*super
= a
->container
->sb
;
6404 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
6405 struct imsm_map
*map
= get_imsm_map(dev
, 0);
6406 struct imsm_disk
*disk
;
6411 if (n
> map
->num_members
)
6412 fprintf(stderr
, "imsm: set_disk %d out of range 0..%d\n",
6413 n
, map
->num_members
- 1);
6418 dprintf("imsm: set_disk %d:%x\n", n
, state
);
6420 ord
= get_imsm_ord_tbl_ent(dev
, n
, -2);
6421 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
6423 /* check for new failures */
6424 if (state
& DS_FAULTY
) {
6425 if (mark_failure(dev
, disk
, ord_to_idx(ord
)))
6426 super
->updates_pending
++;
6429 /* check if in_sync */
6430 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
6431 struct imsm_map
*migr_map
= get_imsm_map(dev
, 1);
6433 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
6434 super
->updates_pending
++;
6437 failed
= imsm_count_failed(super
, dev
, MAP_0
);
6438 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
6440 /* check if recovery complete, newly degraded, or failed */
6441 if (map_state
== IMSM_T_STATE_NORMAL
&& is_rebuilding(dev
)) {
6442 end_migration(dev
, map_state
);
6443 map
= get_imsm_map(dev
, 0);
6444 map
->failed_disk_num
= ~0;
6445 super
->updates_pending
++;
6446 a
->last_checkpoint
= 0;
6447 } else if (map_state
== IMSM_T_STATE_DEGRADED
&&
6448 map
->map_state
!= map_state
&&
6449 !dev
->vol
.migr_state
) {
6450 dprintf("imsm: mark degraded\n");
6451 map
->map_state
= map_state
;
6452 super
->updates_pending
++;
6453 a
->last_checkpoint
= 0;
6454 } else if (map_state
== IMSM_T_STATE_FAILED
&&
6455 map
->map_state
!= map_state
) {
6456 dprintf("imsm: mark failed\n");
6457 end_migration(dev
, map_state
);
6458 super
->updates_pending
++;
6459 a
->last_checkpoint
= 0;
6460 } else if (is_gen_migration(dev
)) {
6461 dprintf("imsm: Detected General Migration in state: ");
6463 switch (map_state
) {
6464 case IMSM_T_STATE_NORMAL
:
6465 dprintf("normal\n");
6466 if (a
->last_checkpoint
>= a
->info
.component_size
)
6467 end_migration(dev
, map_state
);
6468 map
= get_imsm_map(dev
, 0);
6469 map
->failed_disk_num
= ~0;
6471 case IMSM_T_STATE_DEGRADED
:
6472 dprintf("degraded\n");
6473 if (a
->last_checkpoint
>= a
->info
.component_size
)
6474 end_migration(dev
, map_state
);
6476 manage_second_map(super
, dev
);
6479 dprintf("failed\n");
6481 map
->map_state
= map_state
;
6482 super
->updates_pending
++;
6486 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
6489 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
6490 unsigned long long dsize
;
6491 unsigned long long sectors
;
6493 get_dev_size(fd
, NULL
, &dsize
);
6495 if (mpb_size
> 512) {
6496 /* -1 to account for anchor */
6497 sectors
= mpb_sectors(mpb
) - 1;
6499 /* write the extended mpb to the sectors preceeding the anchor */
6500 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0)
6503 if ((unsigned long long)write(fd
, buf
+ 512, 512 * sectors
)
6508 /* first block is stored on second to last sector of the disk */
6509 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0)
6512 if (write(fd
, buf
, 512) != 512)
6518 static void imsm_sync_metadata(struct supertype
*container
)
6520 struct intel_super
*super
= container
->sb
;
6522 dprintf("sync metadata: %d\n", super
->updates_pending
);
6523 if (!super
->updates_pending
)
6526 write_super_imsm(container
, 0);
6528 super
->updates_pending
= 0;
6531 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
6533 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
6534 int i
= get_imsm_disk_idx(dev
, idx
, -1);
6537 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6541 if (dl
&& is_failed(&dl
->disk
))
6545 dprintf("%s: found %x:%x\n", __func__
, dl
->major
, dl
->minor
);
6550 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
6551 struct active_array
*a
, int activate_new
,
6552 struct mdinfo
*additional_test_list
)
6554 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
6555 int idx
= get_imsm_disk_idx(dev
, slot
, -1);
6556 struct imsm_super
*mpb
= super
->anchor
;
6557 struct imsm_map
*map
;
6558 unsigned long long pos
;
6563 __u32 array_start
= 0;
6564 __u32 array_end
= 0;
6566 struct mdinfo
*test_list
;
6568 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6569 /* If in this array, skip */
6570 for (d
= a
->info
.devs
; d
; d
= d
->next
)
6571 if (d
->state_fd
>= 0 &&
6572 d
->disk
.major
== dl
->major
&&
6573 d
->disk
.minor
== dl
->minor
) {
6574 dprintf("%x:%x already in array\n",
6575 dl
->major
, dl
->minor
);
6580 test_list
= additional_test_list
;
6582 if (test_list
->disk
.major
== dl
->major
&&
6583 test_list
->disk
.minor
== dl
->minor
) {
6584 dprintf("%x:%x already in additional test list\n",
6585 dl
->major
, dl
->minor
);
6588 test_list
= test_list
->next
;
6593 /* skip in use or failed drives */
6594 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
6596 dprintf("%x:%x status (failed: %d index: %d)\n",
6597 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
6601 /* skip pure spares when we are looking for partially
6602 * assimilated drives
6604 if (dl
->index
== -1 && !activate_new
)
6607 /* Does this unused device have the requisite free space?
6608 * It needs to be able to cover all member volumes
6610 ex
= get_extents(super
, dl
);
6612 dprintf("cannot get extents\n");
6615 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6616 dev
= get_imsm_dev(super
, i
);
6617 map
= get_imsm_map(dev
, 0);
6619 /* check if this disk is already a member of
6622 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
6628 array_start
= __le32_to_cpu(map
->pba_of_lba0
);
6629 array_end
= array_start
+
6630 __le32_to_cpu(map
->blocks_per_member
) - 1;
6633 /* check that we can start at pba_of_lba0 with
6634 * blocks_per_member of space
6636 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
6640 pos
= ex
[j
].start
+ ex
[j
].size
;
6642 } while (ex
[j
-1].size
);
6649 if (i
< mpb
->num_raid_devs
) {
6650 dprintf("%x:%x does not have %u to %u available\n",
6651 dl
->major
, dl
->minor
, array_start
, array_end
);
6662 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
6664 struct imsm_dev
*dev2
;
6665 struct imsm_map
*map
;
6671 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
6673 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
,
6675 if (state
== IMSM_T_STATE_FAILED
) {
6676 map
= get_imsm_map(dev2
, 0);
6679 for (slot
= 0; slot
< map
->num_members
; slot
++) {
6681 * Check if failed disks are deleted from intel
6682 * disk list or are marked to be deleted
6684 idx
= get_imsm_disk_idx(dev2
, slot
, -1);
6685 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
6687 * Do not rebuild the array if failed disks
6688 * from failed sub-array are not removed from
6692 is_failed(&idisk
->disk
) &&
6693 (idisk
->action
!= DISK_REMOVE
))
6701 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
6702 struct metadata_update
**updates
)
6705 * Find a device with unused free space and use it to replace a
6706 * failed/vacant region in an array. We replace failed regions one a
6707 * array at a time. The result is that a new spare disk will be added
6708 * to the first failed array and after the monitor has finished
6709 * propagating failures the remainder will be consumed.
6711 * FIXME add a capability for mdmon to request spares from another
6715 struct intel_super
*super
= a
->container
->sb
;
6716 int inst
= a
->info
.container_member
;
6717 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
6718 struct imsm_map
*map
= get_imsm_map(dev
, 0);
6719 int failed
= a
->info
.array
.raid_disks
;
6720 struct mdinfo
*rv
= NULL
;
6723 struct metadata_update
*mu
;
6725 struct imsm_update_activate_spare
*u
;
6730 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
6731 if ((d
->curr_state
& DS_FAULTY
) &&
6733 /* wait for Removal to happen */
6735 if (d
->state_fd
>= 0)
6739 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
6740 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
6742 if (imsm_reshape_blocks_arrays_changes(super
))
6745 if (a
->info
.array
.level
== 4)
6746 /* No repair for takeovered array
6747 * imsm doesn't support raid4
6751 if (imsm_check_degraded(super
, dev
, failed
, MAP_0
) !=
6752 IMSM_T_STATE_DEGRADED
)
6756 * If there are any failed disks check state of the other volume.
6757 * Block rebuild if the another one is failed until failed disks
6758 * are removed from container.
6761 dprintf("found failed disks in %.*s, check if there another"
6762 "failed sub-array.\n",
6763 MAX_RAID_SERIAL_LEN
, dev
->volume
);
6764 /* check if states of the other volumes allow for rebuild */
6765 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
6767 allowed
= imsm_rebuild_allowed(a
->container
,
6775 /* For each slot, if it is not working, find a spare */
6776 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
6777 for (d
= a
->info
.devs
; d
; d
= d
->next
)
6778 if (d
->disk
.raid_disk
== i
)
6780 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
6781 if (d
&& (d
->state_fd
>= 0))
6785 * OK, this device needs recovery. Try to re-add the
6786 * previous occupant of this slot, if this fails see if
6787 * we can continue the assimilation of a spare that was
6788 * partially assimilated, finally try to activate a new
6791 dl
= imsm_readd(super
, i
, a
);
6793 dl
= imsm_add_spare(super
, i
, a
, 0, rv
);
6795 dl
= imsm_add_spare(super
, i
, a
, 1, rv
);
6799 /* found a usable disk with enough space */
6800 di
= malloc(sizeof(*di
));
6803 memset(di
, 0, sizeof(*di
));
6805 /* dl->index will be -1 in the case we are activating a
6806 * pristine spare. imsm_process_update() will create a
6807 * new index in this case. Once a disk is found to be
6808 * failed in all member arrays it is kicked from the
6811 di
->disk
.number
= dl
->index
;
6813 /* (ab)use di->devs to store a pointer to the device
6816 di
->devs
= (struct mdinfo
*) dl
;
6818 di
->disk
.raid_disk
= i
;
6819 di
->disk
.major
= dl
->major
;
6820 di
->disk
.minor
= dl
->minor
;
6822 di
->recovery_start
= 0;
6823 di
->data_offset
= __le32_to_cpu(map
->pba_of_lba0
);
6824 di
->component_size
= a
->info
.component_size
;
6825 di
->container_member
= inst
;
6826 super
->random
= random32();
6830 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
6831 i
, di
->data_offset
);
6835 /* No spares found */
6837 /* Now 'rv' has a list of devices to return.
6838 * Create a metadata_update record to update the
6839 * disk_ord_tbl for the array
6841 mu
= malloc(sizeof(*mu
));
6843 mu
->buf
= malloc(sizeof(struct imsm_update_activate_spare
) * num_spares
);
6844 if (mu
->buf
== NULL
) {
6851 struct mdinfo
*n
= rv
->next
;
6860 mu
->space_list
= NULL
;
6861 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
6862 mu
->next
= *updates
;
6863 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
6865 for (di
= rv
; di
; di
= di
->next
) {
6866 u
->type
= update_activate_spare
;
6867 u
->dl
= (struct dl
*) di
->devs
;
6869 u
->slot
= di
->disk
.raid_disk
;
6880 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
6882 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
6883 struct imsm_map
*map
= get_imsm_map(dev
, 0);
6884 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, 0);
6885 struct disk_info
*inf
= get_disk_info(u
);
6886 struct imsm_disk
*disk
;
6890 for (i
= 0; i
< map
->num_members
; i
++) {
6891 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, -1));
6892 for (j
= 0; j
< new_map
->num_members
; j
++)
6893 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
6901 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
6903 struct dl
*dl
= NULL
;
6904 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6905 if ((dl
->major
== major
) && (dl
->minor
== minor
))
6910 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
6912 struct dl
*prev
= NULL
;
6916 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6917 if ((dl
->major
== major
) && (dl
->minor
== minor
)) {
6920 prev
->next
= dl
->next
;
6922 super
->disks
= dl
->next
;
6924 __free_imsm_disk(dl
);
6925 dprintf("%s: removed %x:%x\n",
6926 __func__
, major
, minor
);
6934 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
6936 static int add_remove_disk_update(struct intel_super
*super
)
6938 int check_degraded
= 0;
6939 struct dl
*disk
= NULL
;
6940 /* add/remove some spares to/from the metadata/contrainer */
6941 while (super
->disk_mgmt_list
) {
6942 struct dl
*disk_cfg
;
6944 disk_cfg
= super
->disk_mgmt_list
;
6945 super
->disk_mgmt_list
= disk_cfg
->next
;
6946 disk_cfg
->next
= NULL
;
6948 if (disk_cfg
->action
== DISK_ADD
) {
6949 disk_cfg
->next
= super
->disks
;
6950 super
->disks
= disk_cfg
;
6952 dprintf("%s: added %x:%x\n",
6953 __func__
, disk_cfg
->major
,
6955 } else if (disk_cfg
->action
== DISK_REMOVE
) {
6956 dprintf("Disk remove action processed: %x.%x\n",
6957 disk_cfg
->major
, disk_cfg
->minor
);
6958 disk
= get_disk_super(super
,
6962 /* store action status */
6963 disk
->action
= DISK_REMOVE
;
6964 /* remove spare disks only */
6965 if (disk
->index
== -1) {
6966 remove_disk_super(super
,
6971 /* release allocate disk structure */
6972 __free_imsm_disk(disk_cfg
);
6975 return check_degraded
;
6979 static int apply_reshape_migration_update(struct imsm_update_reshape_migration
*u
,
6980 struct intel_super
*super
,
6983 struct intel_dev
*id
;
6984 void **tofree
= NULL
;
6987 dprintf("apply_reshape_migration_update()\n");
6988 if ((u
->subdev
< 0) ||
6990 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
6993 if ((space_list
== NULL
) || (*space_list
== NULL
)) {
6994 dprintf("imsm: Error: Memory is not allocated\n");
6998 for (id
= super
->devlist
; id
; id
= id
->next
) {
6999 if (id
->index
== (unsigned)u
->subdev
) {
7000 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
7001 struct imsm_map
*map
;
7002 struct imsm_dev
*new_dev
=
7003 (struct imsm_dev
*)*space_list
;
7004 struct imsm_map
*migr_map
= get_imsm_map(dev
, 1);
7006 struct dl
*new_disk
;
7008 if (new_dev
== NULL
)
7010 *space_list
= **space_list
;
7011 memcpy(new_dev
, dev
, sizeof_imsm_dev(dev
, 0));
7012 map
= get_imsm_map(new_dev
, 0);
7014 dprintf("imsm: Error: migration in progress");
7018 to_state
= map
->map_state
;
7019 if ((u
->new_level
== 5) && (map
->raid_level
== 0)) {
7021 /* this should not happen */
7022 if (u
->new_disks
[0] < 0) {
7023 map
->failed_disk_num
=
7024 map
->num_members
- 1;
7025 to_state
= IMSM_T_STATE_DEGRADED
;
7027 to_state
= IMSM_T_STATE_NORMAL
;
7029 migrate(new_dev
, super
, to_state
, MIGR_GEN_MIGR
);
7030 if (u
->new_level
> -1)
7031 map
->raid_level
= u
->new_level
;
7032 migr_map
= get_imsm_map(new_dev
, 1);
7033 if ((u
->new_level
== 5) &&
7034 (migr_map
->raid_level
== 0)) {
7035 int ord
= map
->num_members
- 1;
7036 migr_map
->num_members
--;
7037 if (u
->new_disks
[0] < 0)
7038 ord
|= IMSM_ORD_REBUILD
;
7039 set_imsm_ord_tbl_ent(map
,
7040 map
->num_members
- 1,
7044 tofree
= (void **)dev
;
7046 /* update chunk size
7048 if (u
->new_chunksize
> 0)
7049 map
->blocks_per_strip
=
7050 __cpu_to_le16(u
->new_chunksize
* 2);
7054 if ((u
->new_level
!= 5) ||
7055 (migr_map
->raid_level
!= 0) ||
7056 (migr_map
->raid_level
== map
->raid_level
))
7059 if (u
->new_disks
[0] >= 0) {
7062 new_disk
= get_disk_super(super
,
7063 major(u
->new_disks
[0]),
7064 minor(u
->new_disks
[0]));
7065 dprintf("imsm: new disk for reshape is: %i:%i "
7066 "(%p, index = %i)\n",
7067 major(u
->new_disks
[0]),
7068 minor(u
->new_disks
[0]),
7069 new_disk
, new_disk
->index
);
7070 if (new_disk
== NULL
)
7071 goto error_disk_add
;
7073 new_disk
->index
= map
->num_members
- 1;
7074 /* slot to fill in autolayout
7076 new_disk
->raiddisk
= new_disk
->index
;
7077 new_disk
->disk
.status
|= CONFIGURED_DISK
;
7078 new_disk
->disk
.status
&= ~SPARE_DISK
;
7080 goto error_disk_add
;
7083 *tofree
= *space_list
;
7084 /* calculate new size
7086 imsm_set_array_size(new_dev
);
7093 *space_list
= tofree
;
7097 dprintf("Error: imsm: Cannot find disk.\n");
7101 static int apply_update_activate_spare(struct imsm_update_activate_spare
*u
,
7102 struct intel_super
*super
,
7103 struct active_array
*active_array
)
7105 struct imsm_super
*mpb
= super
->anchor
;
7106 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
7107 struct imsm_map
*map
= get_imsm_map(dev
, 0);
7108 struct imsm_map
*migr_map
;
7109 struct active_array
*a
;
7110 struct imsm_disk
*disk
;
7117 int second_map_created
= 0;
7119 for (; u
; u
= u
->next
) {
7120 victim
= get_imsm_disk_idx(dev
, u
->slot
, -1);
7125 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7130 fprintf(stderr
, "error: imsm_activate_spare passed "
7131 "an unknown disk (index: %d)\n",
7136 /* count failures (excluding rebuilds and the victim)
7137 * to determine map[0] state
7140 for (i
= 0; i
< map
->num_members
; i
++) {
7143 disk
= get_imsm_disk(super
,
7144 get_imsm_disk_idx(dev
, i
, -1));
7145 if (!disk
|| is_failed(disk
))
7149 /* adding a pristine spare, assign a new index */
7150 if (dl
->index
< 0) {
7151 dl
->index
= super
->anchor
->num_disks
;
7152 super
->anchor
->num_disks
++;
7155 disk
->status
|= CONFIGURED_DISK
;
7156 disk
->status
&= ~SPARE_DISK
;
7159 to_state
= imsm_check_degraded(super
, dev
, failed
,
7161 if (!second_map_created
) {
7162 second_map_created
= 1;
7163 map
->map_state
= IMSM_T_STATE_DEGRADED
;
7164 migrate(dev
, super
, to_state
, MIGR_REBUILD
);
7166 map
->map_state
= to_state
;
7167 migr_map
= get_imsm_map(dev
, 1);
7168 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
7169 set_imsm_ord_tbl_ent(migr_map
, u
->slot
,
7170 dl
->index
| IMSM_ORD_REBUILD
);
7172 /* update the family_num to mark a new container
7173 * generation, being careful to record the existing
7174 * family_num in orig_family_num to clean up after
7175 * earlier mdadm versions that neglected to set it.
7177 if (mpb
->orig_family_num
== 0)
7178 mpb
->orig_family_num
= mpb
->family_num
;
7179 mpb
->family_num
+= super
->random
;
7181 /* count arrays using the victim in the metadata */
7183 for (a
= active_array
; a
; a
= a
->next
) {
7184 dev
= get_imsm_dev(super
, a
->info
.container_member
);
7185 map
= get_imsm_map(dev
, 0);
7187 if (get_imsm_disk_slot(map
, victim
) >= 0)
7191 /* delete the victim if it is no longer being
7197 /* We know that 'manager' isn't touching anything,
7198 * so it is safe to delete
7200 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
7201 if ((*dlp
)->index
== victim
)
7204 /* victim may be on the missing list */
7206 for (dlp
= &super
->missing
; *dlp
;
7207 dlp
= &(*dlp
)->next
)
7208 if ((*dlp
)->index
== victim
)
7210 imsm_delete(super
, dlp
, victim
);
7217 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
7218 struct intel_super
*super
,
7221 struct dl
*new_disk
;
7222 struct intel_dev
*id
;
7224 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
7225 int disk_count
= u
->old_raid_disks
;
7226 void **tofree
= NULL
;
7227 int devices_to_reshape
= 1;
7228 struct imsm_super
*mpb
= super
->anchor
;
7230 unsigned int dev_id
;
7232 dprintf("imsm: apply_reshape_container_disks_update()\n");
7234 /* enable spares to use in array */
7235 for (i
= 0; i
< delta_disks
; i
++) {
7236 new_disk
= get_disk_super(super
,
7237 major(u
->new_disks
[i
]),
7238 minor(u
->new_disks
[i
]));
7239 dprintf("imsm: new disk for reshape is: %i:%i "
7240 "(%p, index = %i)\n",
7241 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
7242 new_disk
, new_disk
->index
);
7243 if ((new_disk
== NULL
) ||
7244 ((new_disk
->index
>= 0) &&
7245 (new_disk
->index
< u
->old_raid_disks
)))
7246 goto update_reshape_exit
;
7247 new_disk
->index
= disk_count
++;
7248 /* slot to fill in autolayout
7250 new_disk
->raiddisk
= new_disk
->index
;
7251 new_disk
->disk
.status
|=
7253 new_disk
->disk
.status
&= ~SPARE_DISK
;
7256 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
7257 mpb
->num_raid_devs
);
7258 /* manage changes in volume
7260 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
7261 void **sp
= *space_list
;
7262 struct imsm_dev
*newdev
;
7263 struct imsm_map
*newmap
, *oldmap
;
7265 for (id
= super
->devlist
; id
; id
= id
->next
) {
7266 if (id
->index
== dev_id
)
7275 /* Copy the dev, but not (all of) the map */
7276 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
7277 oldmap
= get_imsm_map(id
->dev
, 0);
7278 newmap
= get_imsm_map(newdev
, 0);
7279 /* Copy the current map */
7280 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
7281 /* update one device only
7283 if (devices_to_reshape
) {
7284 dprintf("imsm: modifying subdev: %i\n",
7286 devices_to_reshape
--;
7287 newdev
->vol
.migr_state
= 1;
7288 newdev
->vol
.curr_migr_unit
= 0;
7289 set_migr_type(newdev
, MIGR_GEN_MIGR
);
7290 newmap
->num_members
= u
->new_raid_disks
;
7291 for (i
= 0; i
< delta_disks
; i
++) {
7292 set_imsm_ord_tbl_ent(newmap
,
7293 u
->old_raid_disks
+ i
,
7294 u
->old_raid_disks
+ i
);
7296 /* New map is correct, now need to save old map
7298 newmap
= get_imsm_map(newdev
, 1);
7299 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
7301 imsm_set_array_size(newdev
);
7304 sp
= (void **)id
->dev
;
7309 /* Clear migration record */
7310 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
7313 *space_list
= tofree
;
7316 update_reshape_exit
:
7321 static int apply_takeover_update(struct imsm_update_takeover
*u
,
7322 struct intel_super
*super
,
7325 struct imsm_dev
*dev
= NULL
;
7326 struct intel_dev
*dv
;
7327 struct imsm_dev
*dev_new
;
7328 struct imsm_map
*map
;
7332 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
7333 if (dv
->index
== (unsigned int)u
->subarray
) {
7341 map
= get_imsm_map(dev
, 0);
7343 if (u
->direction
== R10_TO_R0
) {
7344 /* Number of failed disks must be half of initial disk number */
7345 if (imsm_count_failed(super
, dev
, MAP_0
) !=
7346 (map
->num_members
/ 2))
7349 /* iterate through devices to mark removed disks as spare */
7350 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
7351 if (dm
->disk
.status
& FAILED_DISK
) {
7352 int idx
= dm
->index
;
7353 /* update indexes on the disk list */
7354 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
7355 the index values will end up being correct.... NB */
7356 for (du
= super
->disks
; du
; du
= du
->next
)
7357 if (du
->index
> idx
)
7359 /* mark as spare disk */
7364 map
->num_members
= map
->num_members
/ 2;
7365 map
->map_state
= IMSM_T_STATE_NORMAL
;
7366 map
->num_domains
= 1;
7367 map
->raid_level
= 0;
7368 map
->failed_disk_num
= -1;
7371 if (u
->direction
== R0_TO_R10
) {
7373 /* update slots in current disk list */
7374 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
7378 /* create new *missing* disks */
7379 for (i
= 0; i
< map
->num_members
; i
++) {
7380 space
= *space_list
;
7383 *space_list
= *space
;
7385 memcpy(du
, super
->disks
, sizeof(*du
));
7389 du
->index
= (i
* 2) + 1;
7390 sprintf((char *)du
->disk
.serial
,
7391 " MISSING_%d", du
->index
);
7392 sprintf((char *)du
->serial
,
7393 "MISSING_%d", du
->index
);
7394 du
->next
= super
->missing
;
7395 super
->missing
= du
;
7397 /* create new dev and map */
7398 space
= *space_list
;
7401 *space_list
= *space
;
7402 dev_new
= (void *)space
;
7403 memcpy(dev_new
, dev
, sizeof(*dev
));
7404 /* update new map */
7405 map
= get_imsm_map(dev_new
, 0);
7406 map
->num_members
= map
->num_members
* 2;
7407 map
->map_state
= IMSM_T_STATE_DEGRADED
;
7408 map
->num_domains
= 2;
7409 map
->raid_level
= 1;
7410 /* replace dev<->dev_new */
7413 /* update disk order table */
7414 for (du
= super
->disks
; du
; du
= du
->next
)
7416 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
7417 for (du
= super
->missing
; du
; du
= du
->next
)
7418 if (du
->index
>= 0) {
7419 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
7420 mark_missing(dv
->dev
, &du
->disk
, du
->index
);
7426 static void imsm_process_update(struct supertype
*st
,
7427 struct metadata_update
*update
)
7430 * crack open the metadata_update envelope to find the update record
7431 * update can be one of:
7432 * update_reshape_container_disks - all the arrays in the container
7433 * are being reshaped to have more devices. We need to mark
7434 * the arrays for general migration and convert selected spares
7435 * into active devices.
7436 * update_activate_spare - a spare device has replaced a failed
7437 * device in an array, update the disk_ord_tbl. If this disk is
7438 * present in all member arrays then also clear the SPARE_DISK
7440 * update_create_array
7442 * update_rename_array
7443 * update_add_remove_disk
7445 struct intel_super
*super
= st
->sb
;
7446 struct imsm_super
*mpb
;
7447 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
7449 /* update requires a larger buf but the allocation failed */
7450 if (super
->next_len
&& !super
->next_buf
) {
7451 super
->next_len
= 0;
7455 if (super
->next_buf
) {
7456 memcpy(super
->next_buf
, super
->buf
, super
->len
);
7458 super
->len
= super
->next_len
;
7459 super
->buf
= super
->next_buf
;
7461 super
->next_len
= 0;
7462 super
->next_buf
= NULL
;
7465 mpb
= super
->anchor
;
7468 case update_general_migration_checkpoint
: {
7469 struct intel_dev
*id
;
7470 struct imsm_update_general_migration_checkpoint
*u
=
7471 (void *)update
->buf
;
7473 dprintf("imsm: process_update() "
7474 "for update_general_migration_checkpoint called\n");
7476 /* find device under general migration */
7477 for (id
= super
->devlist
; id
; id
= id
->next
) {
7478 if (is_gen_migration(id
->dev
)) {
7479 id
->dev
->vol
.curr_migr_unit
=
7480 __cpu_to_le32(u
->curr_migr_unit
);
7481 super
->updates_pending
++;
7486 case update_takeover
: {
7487 struct imsm_update_takeover
*u
= (void *)update
->buf
;
7488 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
7489 imsm_update_version_info(super
);
7490 super
->updates_pending
++;
7495 case update_reshape_container_disks
: {
7496 struct imsm_update_reshape
*u
= (void *)update
->buf
;
7497 if (apply_reshape_container_disks_update(
7498 u
, super
, &update
->space_list
))
7499 super
->updates_pending
++;
7502 case update_reshape_migration
: {
7503 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
7504 if (apply_reshape_migration_update(
7505 u
, super
, &update
->space_list
))
7506 super
->updates_pending
++;
7509 case update_activate_spare
: {
7510 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
7511 if (apply_update_activate_spare(u
, super
, st
->arrays
))
7512 super
->updates_pending
++;
7515 case update_create_array
: {
7516 /* someone wants to create a new array, we need to be aware of
7517 * a few races/collisions:
7518 * 1/ 'Create' called by two separate instances of mdadm
7519 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
7520 * devices that have since been assimilated via
7522 * In the event this update can not be carried out mdadm will
7523 * (FIX ME) notice that its update did not take hold.
7525 struct imsm_update_create_array
*u
= (void *) update
->buf
;
7526 struct intel_dev
*dv
;
7527 struct imsm_dev
*dev
;
7528 struct imsm_map
*map
, *new_map
;
7529 unsigned long long start
, end
;
7530 unsigned long long new_start
, new_end
;
7532 struct disk_info
*inf
;
7535 /* handle racing creates: first come first serve */
7536 if (u
->dev_idx
< mpb
->num_raid_devs
) {
7537 dprintf("%s: subarray %d already defined\n",
7538 __func__
, u
->dev_idx
);
7542 /* check update is next in sequence */
7543 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
7544 dprintf("%s: can not create array %d expected index %d\n",
7545 __func__
, u
->dev_idx
, mpb
->num_raid_devs
);
7549 new_map
= get_imsm_map(&u
->dev
, 0);
7550 new_start
= __le32_to_cpu(new_map
->pba_of_lba0
);
7551 new_end
= new_start
+ __le32_to_cpu(new_map
->blocks_per_member
);
7552 inf
= get_disk_info(u
);
7554 /* handle activate_spare versus create race:
7555 * check to make sure that overlapping arrays do not include
7558 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7559 dev
= get_imsm_dev(super
, i
);
7560 map
= get_imsm_map(dev
, 0);
7561 start
= __le32_to_cpu(map
->pba_of_lba0
);
7562 end
= start
+ __le32_to_cpu(map
->blocks_per_member
);
7563 if ((new_start
>= start
&& new_start
<= end
) ||
7564 (start
>= new_start
&& start
<= new_end
))
7569 if (disks_overlap(super
, i
, u
)) {
7570 dprintf("%s: arrays overlap\n", __func__
);
7575 /* check that prepare update was successful */
7576 if (!update
->space
) {
7577 dprintf("%s: prepare update failed\n", __func__
);
7581 /* check that all disks are still active before committing
7582 * changes. FIXME: could we instead handle this by creating a
7583 * degraded array? That's probably not what the user expects,
7584 * so better to drop this update on the floor.
7586 for (i
= 0; i
< new_map
->num_members
; i
++) {
7587 dl
= serial_to_dl(inf
[i
].serial
, super
);
7589 dprintf("%s: disk disappeared\n", __func__
);
7594 super
->updates_pending
++;
7596 /* convert spares to members and fixup ord_tbl */
7597 for (i
= 0; i
< new_map
->num_members
; i
++) {
7598 dl
= serial_to_dl(inf
[i
].serial
, super
);
7599 if (dl
->index
== -1) {
7600 dl
->index
= mpb
->num_disks
;
7602 dl
->disk
.status
|= CONFIGURED_DISK
;
7603 dl
->disk
.status
&= ~SPARE_DISK
;
7605 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
7610 update
->space
= NULL
;
7611 imsm_copy_dev(dev
, &u
->dev
);
7612 dv
->index
= u
->dev_idx
;
7613 dv
->next
= super
->devlist
;
7614 super
->devlist
= dv
;
7615 mpb
->num_raid_devs
++;
7617 imsm_update_version_info(super
);
7620 /* mdmon knows how to release update->space, but not
7621 * ((struct intel_dev *) update->space)->dev
7623 if (update
->space
) {
7629 case update_kill_array
: {
7630 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
7631 int victim
= u
->dev_idx
;
7632 struct active_array
*a
;
7633 struct intel_dev
**dp
;
7634 struct imsm_dev
*dev
;
7636 /* sanity check that we are not affecting the uuid of
7637 * active arrays, or deleting an active array
7639 * FIXME when immutable ids are available, but note that
7640 * we'll also need to fixup the invalidated/active
7641 * subarray indexes in mdstat
7643 for (a
= st
->arrays
; a
; a
= a
->next
)
7644 if (a
->info
.container_member
>= victim
)
7646 /* by definition if mdmon is running at least one array
7647 * is active in the container, so checking
7648 * mpb->num_raid_devs is just extra paranoia
7650 dev
= get_imsm_dev(super
, victim
);
7651 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
7652 dprintf("failed to delete subarray-%d\n", victim
);
7656 for (dp
= &super
->devlist
; *dp
;)
7657 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
7660 if ((*dp
)->index
> (unsigned)victim
)
7664 mpb
->num_raid_devs
--;
7665 super
->updates_pending
++;
7668 case update_rename_array
: {
7669 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
7670 char name
[MAX_RAID_SERIAL_LEN
+1];
7671 int target
= u
->dev_idx
;
7672 struct active_array
*a
;
7673 struct imsm_dev
*dev
;
7675 /* sanity check that we are not affecting the uuid of
7678 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
7679 name
[MAX_RAID_SERIAL_LEN
] = '\0';
7680 for (a
= st
->arrays
; a
; a
= a
->next
)
7681 if (a
->info
.container_member
== target
)
7683 dev
= get_imsm_dev(super
, u
->dev_idx
);
7684 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
7685 dprintf("failed to rename subarray-%d\n", target
);
7689 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
7690 super
->updates_pending
++;
7693 case update_add_remove_disk
: {
7694 /* we may be able to repair some arrays if disks are
7695 * being added, check teh status of add_remove_disk
7696 * if discs has been added.
7698 if (add_remove_disk_update(super
)) {
7699 struct active_array
*a
;
7701 super
->updates_pending
++;
7702 for (a
= st
->arrays
; a
; a
= a
->next
)
7703 a
->check_degraded
= 1;
7708 fprintf(stderr
, "error: unsuported process update type:"
7709 "(type: %d)\n", type
);
7713 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
);
7715 static void imsm_prepare_update(struct supertype
*st
,
7716 struct metadata_update
*update
)
7719 * Allocate space to hold new disk entries, raid-device entries or a new
7720 * mpb if necessary. The manager synchronously waits for updates to
7721 * complete in the monitor, so new mpb buffers allocated here can be
7722 * integrated by the monitor thread without worrying about live pointers
7723 * in the manager thread.
7725 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
7726 struct intel_super
*super
= st
->sb
;
7727 struct imsm_super
*mpb
= super
->anchor
;
7732 case update_general_migration_checkpoint
:
7733 dprintf("imsm: prepare_update() "
7734 "for update_general_migration_checkpoint called\n");
7736 case update_takeover
: {
7737 struct imsm_update_takeover
*u
= (void *)update
->buf
;
7738 if (u
->direction
== R0_TO_R10
) {
7739 void **tail
= (void **)&update
->space_list
;
7740 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
7741 struct imsm_map
*map
= get_imsm_map(dev
, 0);
7742 int num_members
= map
->num_members
;
7746 /* allocate memory for added disks */
7747 for (i
= 0; i
< num_members
; i
++) {
7748 size
= sizeof(struct dl
);
7749 space
= malloc(size
);
7758 /* allocate memory for new device */
7759 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
7760 (num_members
* sizeof(__u32
));
7761 space
= malloc(size
);
7770 len
= disks_to_mpb_size(num_members
* 2);
7772 /* if allocation didn't success, free buffer */
7773 while (update
->space_list
) {
7774 void **sp
= update
->space_list
;
7775 update
->space_list
= *sp
;
7783 case update_reshape_container_disks
: {
7784 /* Every raid device in the container is about to
7785 * gain some more devices, and we will enter a
7787 * So each 'imsm_map' will be bigger, and the imsm_vol
7788 * will now hold 2 of them.
7789 * Thus we need new 'struct imsm_dev' allocations sized
7790 * as sizeof_imsm_dev but with more devices in both maps.
7792 struct imsm_update_reshape
*u
= (void *)update
->buf
;
7793 struct intel_dev
*dl
;
7794 void **space_tail
= (void**)&update
->space_list
;
7796 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
7798 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
7799 int size
= sizeof_imsm_dev(dl
->dev
, 1);
7801 if (u
->new_raid_disks
> u
->old_raid_disks
)
7802 size
+= sizeof(__u32
)*2*
7803 (u
->new_raid_disks
- u
->old_raid_disks
);
7812 len
= disks_to_mpb_size(u
->new_raid_disks
);
7813 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
7816 case update_reshape_migration
: {
7817 /* for migration level 0->5 we need to add disks
7818 * so the same as for container operation we will copy
7819 * device to the bigger location.
7820 * in memory prepared device and new disk area are prepared
7821 * for usage in process update
7823 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
7824 struct intel_dev
*id
;
7825 void **space_tail
= (void **)&update
->space_list
;
7828 int current_level
= -1;
7830 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
7832 /* add space for bigger array in update
7834 for (id
= super
->devlist
; id
; id
= id
->next
) {
7835 if (id
->index
== (unsigned)u
->subdev
) {
7836 size
= sizeof_imsm_dev(id
->dev
, 1);
7837 if (u
->new_raid_disks
> u
->old_raid_disks
)
7838 size
+= sizeof(__u32
)*2*
7839 (u
->new_raid_disks
- u
->old_raid_disks
);
7849 if (update
->space_list
== NULL
)
7852 /* add space for disk in update
7854 size
= sizeof(struct dl
);
7857 free(update
->space_list
);
7858 update
->space_list
= NULL
;
7865 /* add spare device to update
7867 for (id
= super
->devlist
; id
; id
= id
->next
)
7868 if (id
->index
== (unsigned)u
->subdev
) {
7869 struct imsm_dev
*dev
;
7870 struct imsm_map
*map
;
7872 dev
= get_imsm_dev(super
, u
->subdev
);
7873 map
= get_imsm_map(dev
, 0);
7874 current_level
= map
->raid_level
;
7877 if ((u
->new_level
== 5) && (u
->new_level
!= current_level
)) {
7878 struct mdinfo
*spares
;
7880 spares
= get_spares_for_grow(st
);
7888 makedev(dev
->disk
.major
,
7890 dl
= get_disk_super(super
,
7893 dl
->index
= u
->old_raid_disks
;
7899 len
= disks_to_mpb_size(u
->new_raid_disks
);
7900 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
7903 case update_create_array
: {
7904 struct imsm_update_create_array
*u
= (void *) update
->buf
;
7905 struct intel_dev
*dv
;
7906 struct imsm_dev
*dev
= &u
->dev
;
7907 struct imsm_map
*map
= get_imsm_map(dev
, 0);
7909 struct disk_info
*inf
;
7913 inf
= get_disk_info(u
);
7914 len
= sizeof_imsm_dev(dev
, 1);
7915 /* allocate a new super->devlist entry */
7916 dv
= malloc(sizeof(*dv
));
7918 dv
->dev
= malloc(len
);
7923 update
->space
= NULL
;
7927 /* count how many spares will be converted to members */
7928 for (i
= 0; i
< map
->num_members
; i
++) {
7929 dl
= serial_to_dl(inf
[i
].serial
, super
);
7931 /* hmm maybe it failed?, nothing we can do about
7936 if (count_memberships(dl
, super
) == 0)
7939 len
+= activate
* sizeof(struct imsm_disk
);
7946 /* check if we need a larger metadata buffer */
7947 if (super
->next_buf
)
7948 buf_len
= super
->next_len
;
7950 buf_len
= super
->len
;
7952 if (__le32_to_cpu(mpb
->mpb_size
) + len
> buf_len
) {
7953 /* ok we need a larger buf than what is currently allocated
7954 * if this allocation fails process_update will notice that
7955 * ->next_len is set and ->next_buf is NULL
7957 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + len
, 512);
7958 if (super
->next_buf
)
7959 free(super
->next_buf
);
7961 super
->next_len
= buf_len
;
7962 if (posix_memalign(&super
->next_buf
, 512, buf_len
) == 0)
7963 memset(super
->next_buf
, 0, buf_len
);
7965 super
->next_buf
= NULL
;
7969 /* must be called while manager is quiesced */
7970 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
7972 struct imsm_super
*mpb
= super
->anchor
;
7974 struct imsm_dev
*dev
;
7975 struct imsm_map
*map
;
7976 int i
, j
, num_members
;
7979 dprintf("%s: deleting device[%d] from imsm_super\n",
7982 /* shift all indexes down one */
7983 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
7984 if (iter
->index
> (int)index
)
7986 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
7987 if (iter
->index
> (int)index
)
7990 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7991 dev
= get_imsm_dev(super
, i
);
7992 map
= get_imsm_map(dev
, 0);
7993 num_members
= map
->num_members
;
7994 for (j
= 0; j
< num_members
; j
++) {
7995 /* update ord entries being careful not to propagate
7996 * ord-flags to the first map
7998 ord
= get_imsm_ord_tbl_ent(dev
, j
, -1);
8000 if (ord_to_idx(ord
) <= index
)
8003 map
= get_imsm_map(dev
, 0);
8004 set_imsm_ord_tbl_ent(map
, j
, ord_to_idx(ord
- 1));
8005 map
= get_imsm_map(dev
, 1);
8007 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
8012 super
->updates_pending
++;
8014 struct dl
*dl
= *dlp
;
8016 *dlp
= (*dlp
)->next
;
8017 __free_imsm_disk(dl
);
8020 #endif /* MDASSEMBLE */
8022 static void close_targets(int *targets
, int new_disks
)
8029 for (i
= 0; i
< new_disks
; i
++) {
8030 if (targets
[i
] >= 0) {
8037 static int imsm_get_allowed_degradation(int level
, int raid_disks
,
8038 struct intel_super
*super
,
8039 struct imsm_dev
*dev
)
8044 struct imsm_map
*map
;
8047 ret_val
= raid_disks
/2;
8048 /* check map if all disks pairs not failed
8051 map
= get_imsm_map(dev
, 0);
8052 for (i
= 0; i
< ret_val
; i
++) {
8053 int degradation
= 0;
8054 if (get_imsm_disk(super
, i
) == NULL
)
8056 if (get_imsm_disk(super
, i
+ 1) == NULL
)
8058 if (degradation
== 2)
8061 map
= get_imsm_map(dev
, 1);
8062 /* if there is no second map
8063 * result can be returned
8067 /* check degradation in second map
8069 for (i
= 0; i
< ret_val
; i
++) {
8070 int degradation
= 0;
8071 if (get_imsm_disk(super
, i
) == NULL
)
8073 if (get_imsm_disk(super
, i
+ 1) == NULL
)
8075 if (degradation
== 2)
8090 /*******************************************************************************
8091 * Function: open_backup_targets
8092 * Description: Function opens file descriptors for all devices given in
8095 * info : general array info
8096 * raid_disks : number of disks
8097 * raid_fds : table of device's file descriptors
8098 * super : intel super for raid10 degradation check
8099 * dev : intel device for raid10 degradation check
8103 ******************************************************************************/
8104 int open_backup_targets(struct mdinfo
*info
, int raid_disks
, int *raid_fds
,
8105 struct intel_super
*super
, struct imsm_dev
*dev
)
8111 for (i
= 0; i
< raid_disks
; i
++)
8114 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
8117 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
)) {
8118 dprintf("disk is faulty!!\n");
8122 if ((sd
->disk
.raid_disk
>= raid_disks
) ||
8123 (sd
->disk
.raid_disk
< 0))
8126 dn
= map_dev(sd
->disk
.major
,
8128 raid_fds
[sd
->disk
.raid_disk
] = dev_open(dn
, O_RDWR
);
8129 if (raid_fds
[sd
->disk
.raid_disk
] < 0) {
8130 fprintf(stderr
, "cannot open component\n");
8135 /* check if maximum array degradation level is not exceeded
8137 if ((raid_disks
- opened
) >
8138 imsm_get_allowed_degradation(info
->new_level
,
8141 fprintf(stderr
, "Not enough disks can be opened.\n");
8142 close_targets(raid_fds
, raid_disks
);
8149 /*******************************************************************************
8150 * Function: init_migr_record_imsm
8151 * Description: Function inits imsm migration record
8153 * super : imsm internal array info
8154 * dev : device under migration
8155 * info : general array info to find the smallest device
8158 ******************************************************************************/
8159 void init_migr_record_imsm(struct supertype
*st
, struct imsm_dev
*dev
,
8160 struct mdinfo
*info
)
8162 struct intel_super
*super
= st
->sb
;
8163 struct migr_record
*migr_rec
= super
->migr_rec
;
8165 unsigned long long dsize
, dev_sectors
;
8166 long long unsigned min_dev_sectors
= -1LLU;
8170 struct imsm_map
*map_dest
= get_imsm_map(dev
, 0);
8171 struct imsm_map
*map_src
= get_imsm_map(dev
, 1);
8172 unsigned long long num_migr_units
;
8173 unsigned long long array_blocks
;
8175 memset(migr_rec
, 0, sizeof(struct migr_record
));
8176 migr_rec
->family_num
= __cpu_to_le32(super
->anchor
->family_num
);
8178 /* only ascending reshape supported now */
8179 migr_rec
->ascending_migr
= __cpu_to_le32(1);
8181 migr_rec
->dest_depth_per_unit
= GEN_MIGR_AREA_SIZE
/
8182 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
8183 migr_rec
->dest_depth_per_unit
*= map_dest
->blocks_per_strip
;
8184 new_data_disks
= imsm_num_data_members(dev
, 0);
8185 migr_rec
->blocks_per_unit
=
8186 __cpu_to_le32(migr_rec
->dest_depth_per_unit
* new_data_disks
);
8187 migr_rec
->dest_depth_per_unit
=
8188 __cpu_to_le32(migr_rec
->dest_depth_per_unit
);
8189 array_blocks
= info
->component_size
* new_data_disks
;
8191 array_blocks
/ __le32_to_cpu(migr_rec
->blocks_per_unit
);
8193 if (array_blocks
% __le32_to_cpu(migr_rec
->blocks_per_unit
))
8195 migr_rec
->num_migr_units
= __cpu_to_le32(num_migr_units
);
8197 migr_rec
->post_migr_vol_cap
= dev
->size_low
;
8198 migr_rec
->post_migr_vol_cap_hi
= dev
->size_high
;
8201 /* Find the smallest dev */
8202 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
8203 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
8204 fd
= dev_open(nm
, O_RDONLY
);
8207 get_dev_size(fd
, NULL
, &dsize
);
8208 dev_sectors
= dsize
/ 512;
8209 if (dev_sectors
< min_dev_sectors
)
8210 min_dev_sectors
= dev_sectors
;
8213 migr_rec
->ckpt_area_pba
= __cpu_to_le32(min_dev_sectors
-
8214 RAID_DISK_RESERVED_BLOCKS_IMSM_HI
);
8216 write_imsm_migr_rec(st
);
8221 /*******************************************************************************
8222 * Function: save_backup_imsm
8223 * Description: Function saves critical data stripes to Migration Copy Area
8224 * and updates the current migration unit status.
8225 * Use restore_stripes() to form a destination stripe,
8226 * and to write it to the Copy Area.
8228 * st : supertype information
8229 * dev : imsm device that backup is saved for
8230 * info : general array info
8231 * buf : input buffer
8232 * length : length of data to backup (blocks_per_unit)
8236 ******************************************************************************/
8237 int save_backup_imsm(struct supertype
*st
,
8238 struct imsm_dev
*dev
,
8239 struct mdinfo
*info
,
8244 struct intel_super
*super
= st
->sb
;
8245 unsigned long long *target_offsets
= NULL
;
8246 int *targets
= NULL
;
8248 struct imsm_map
*map_dest
= get_imsm_map(dev
, 0);
8249 int new_disks
= map_dest
->num_members
;
8250 int dest_layout
= 0;
8252 unsigned long long start
;
8253 int data_disks
= imsm_num_data_members(dev
, 0);
8255 targets
= malloc(new_disks
* sizeof(int));
8259 for (i
= 0; i
< new_disks
; i
++)
8262 target_offsets
= malloc(new_disks
* sizeof(unsigned long long));
8263 if (!target_offsets
)
8266 start
= info
->reshape_progress
* 512;
8267 for (i
= 0; i
< new_disks
; i
++) {
8268 target_offsets
[i
] = (unsigned long long)
8269 __le32_to_cpu(super
->migr_rec
->ckpt_area_pba
) * 512;
8270 /* move back copy area adderss, it will be moved forward
8271 * in restore_stripes() using start input variable
8273 target_offsets
[i
] -= start
/data_disks
;
8276 if (open_backup_targets(info
, new_disks
, targets
,
8280 dest_layout
= imsm_level_to_layout(map_dest
->raid_level
);
8281 dest_chunk
= __le16_to_cpu(map_dest
->blocks_per_strip
) * 512;
8283 if (restore_stripes(targets
, /* list of dest devices */
8284 target_offsets
, /* migration record offsets */
8287 map_dest
->raid_level
,
8289 -1, /* source backup file descriptor */
8290 0, /* input buf offset
8291 * always 0 buf is already offseted */
8295 fprintf(stderr
, Name
": Error restoring stripes\n");
8303 close_targets(targets
, new_disks
);
8306 free(target_offsets
);
8311 /*******************************************************************************
8312 * Function: save_checkpoint_imsm
8313 * Description: Function called for current unit status update
8314 * in the migration record. It writes it to disk.
8316 * super : imsm internal array info
8317 * info : general array info
8321 * 2: failure, means no valid migration record
8322 * / no general migration in progress /
8323 ******************************************************************************/
8324 int save_checkpoint_imsm(struct supertype
*st
, struct mdinfo
*info
, int state
)
8326 struct intel_super
*super
= st
->sb
;
8327 unsigned long long blocks_per_unit
;
8328 unsigned long long curr_migr_unit
;
8330 if (load_imsm_migr_rec(super
, info
) != 0) {
8331 dprintf("imsm: ERROR: Cannot read migration record "
8332 "for checkpoint save.\n");
8336 blocks_per_unit
= __le32_to_cpu(super
->migr_rec
->blocks_per_unit
);
8337 if (blocks_per_unit
== 0) {
8338 dprintf("imsm: no migration in progress.\n");
8341 curr_migr_unit
= info
->reshape_progress
/ blocks_per_unit
;
8342 /* check if array is alligned to copy area
8343 * if it is not alligned, add one to current migration unit value
8344 * this can happend on array reshape finish only
8346 if (info
->reshape_progress
% blocks_per_unit
)
8349 super
->migr_rec
->curr_migr_unit
=
8350 __cpu_to_le32(curr_migr_unit
);
8351 super
->migr_rec
->rec_status
= __cpu_to_le32(state
);
8352 super
->migr_rec
->dest_1st_member_lba
=
8353 __cpu_to_le32(curr_migr_unit
*
8354 __le32_to_cpu(super
->migr_rec
->dest_depth_per_unit
));
8355 if (write_imsm_migr_rec(st
) < 0) {
8356 dprintf("imsm: Cannot write migration record "
8357 "outside backup area\n");
8364 /*******************************************************************************
8365 * Function: recover_backup_imsm
8366 * Description: Function recovers critical data from the Migration Copy Area
8367 * while assembling an array.
8369 * super : imsm internal array info
8370 * info : general array info
8372 * 0 : success (or there is no data to recover)
8374 ******************************************************************************/
8375 int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
)
8377 struct intel_super
*super
= st
->sb
;
8378 struct migr_record
*migr_rec
= super
->migr_rec
;
8379 struct imsm_map
*map_dest
= NULL
;
8380 struct intel_dev
*id
= NULL
;
8381 unsigned long long read_offset
;
8382 unsigned long long write_offset
;
8384 int *targets
= NULL
;
8385 int new_disks
, i
, err
;
8388 unsigned long curr_migr_unit
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
8389 unsigned long num_migr_units
= __le32_to_cpu(migr_rec
->num_migr_units
);
8391 int skipped_disks
= 0;
8392 int max_degradation
;
8394 err
= sysfs_get_str(info
, NULL
, "array_state", (char *)buffer
, 20);
8398 /* recover data only during assemblation */
8399 if (strncmp(buffer
, "inactive", 8) != 0)
8401 /* no data to recover */
8402 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
8404 if (curr_migr_unit
>= num_migr_units
)
8407 /* find device during reshape */
8408 for (id
= super
->devlist
; id
; id
= id
->next
)
8409 if (is_gen_migration(id
->dev
))
8414 map_dest
= get_imsm_map(id
->dev
, 0);
8415 new_disks
= map_dest
->num_members
;
8416 max_degradation
= new_disks
- imsm_num_data_members(id
->dev
, 0);
8418 read_offset
= (unsigned long long)
8419 __le32_to_cpu(migr_rec
->ckpt_area_pba
) * 512;
8421 write_offset
= ((unsigned long long)
8422 __le32_to_cpu(migr_rec
->dest_1st_member_lba
) +
8423 __le32_to_cpu(map_dest
->pba_of_lba0
)) * 512;
8425 unit_len
= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
8426 if (posix_memalign((void **)&buf
, 512, unit_len
) != 0)
8428 targets
= malloc(new_disks
* sizeof(int));
8432 if (open_backup_targets(info
, new_disks
, targets
, super
, id
->dev
)) {
8434 Name
": Cannot open some devices belonging to array.\n");
8438 for (i
= 0; i
< new_disks
; i
++) {
8439 if (targets
[i
] < 0) {
8443 if (lseek64(targets
[i
], read_offset
, SEEK_SET
) < 0) {
8445 Name
": Cannot seek to block: %s\n",
8449 if ((unsigned)read(targets
[i
], buf
, unit_len
) != unit_len
) {
8451 Name
": Cannot read copy area block: %s\n",
8455 if (lseek64(targets
[i
], write_offset
, SEEK_SET
) < 0) {
8457 Name
": Cannot seek to block: %s\n",
8461 if ((unsigned)write(targets
[i
], buf
, unit_len
) != unit_len
) {
8463 Name
": Cannot restore block: %s\n",
8469 if (skipped_disks
> max_degradation
) {
8471 Name
": Cannot restore data from backup."
8472 " Too many failed disks\n");
8476 if (save_checkpoint_imsm(st
, info
, UNIT_SRC_NORMAL
)) {
8477 /* ignore error == 2, this can mean end of reshape here
8479 dprintf("imsm: Cannot write checkpoint to "
8480 "migration record (UNIT_SRC_NORMAL) during restart\n");
8486 for (i
= 0; i
< new_disks
; i
++)
8495 static char disk_by_path
[] = "/dev/disk/by-path/";
8497 static const char *imsm_get_disk_controller_domain(const char *path
)
8499 char disk_path
[PATH_MAX
];
8503 strncpy(disk_path
, disk_by_path
, PATH_MAX
- 1);
8504 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
8505 if (stat(disk_path
, &st
) == 0) {
8506 struct sys_dev
* hba
;
8509 path
= devt_to_devpath(st
.st_rdev
);
8512 hba
= find_disk_attached_hba(-1, path
);
8513 if (hba
&& hba
->type
== SYS_DEV_SAS
)
8515 else if (hba
&& hba
->type
== SYS_DEV_SATA
)
8519 dprintf("path: %s hba: %s attached: %s\n",
8520 path
, (hba
) ? hba
->path
: "NULL", drv
);
8528 static int imsm_find_array_minor_by_subdev(int subdev
, int container
, int *minor
)
8530 char subdev_name
[20];
8531 struct mdstat_ent
*mdstat
;
8533 sprintf(subdev_name
, "%d", subdev
);
8534 mdstat
= mdstat_by_subdev(subdev_name
, container
);
8538 *minor
= mdstat
->devnum
;
8539 free_mdstat(mdstat
);
8543 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
8544 struct geo_params
*geo
,
8545 int *old_raid_disks
)
8547 /* currently we only support increasing the number of devices
8548 * for a container. This increases the number of device for each
8549 * member array. They must all be RAID0 or RAID5.
8552 struct mdinfo
*info
, *member
;
8553 int devices_that_can_grow
= 0;
8555 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): "
8556 "st->devnum = (%i)\n",
8559 if (geo
->size
!= -1 ||
8560 geo
->level
!= UnSet
||
8561 geo
->layout
!= UnSet
||
8562 geo
->chunksize
!= 0 ||
8563 geo
->raid_disks
== UnSet
) {
8564 dprintf("imsm: Container operation is allowed for "
8565 "raid disks number change only.\n");
8569 info
= container_content_imsm(st
, NULL
);
8570 for (member
= info
; member
; member
= member
->next
) {
8574 dprintf("imsm: checking device_num: %i\n",
8575 member
->container_member
);
8577 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
8578 /* we work on container for Online Capacity Expansion
8579 * only so raid_disks has to grow
8581 dprintf("imsm: for container operation raid disks "
8582 "increase is required\n");
8586 if ((info
->array
.level
!= 0) &&
8587 (info
->array
.level
!= 5)) {
8588 /* we cannot use this container with other raid level
8590 dprintf("imsm: for container operation wrong"
8591 " raid level (%i) detected\n",
8595 /* check for platform support
8596 * for this raid level configuration
8598 struct intel_super
*super
= st
->sb
;
8599 if (!is_raid_level_supported(super
->orom
,
8600 member
->array
.level
,
8602 dprintf("platform does not support raid%d with"
8606 geo
->raid_disks
> 1 ? "s" : "");
8609 /* check if component size is aligned to chunk size
8611 if (info
->component_size
%
8612 (info
->array
.chunk_size
/512)) {
8613 dprintf("Component size is not aligned to "
8619 if (*old_raid_disks
&&
8620 info
->array
.raid_disks
!= *old_raid_disks
)
8622 *old_raid_disks
= info
->array
.raid_disks
;
8624 /* All raid5 and raid0 volumes in container
8625 * have to be ready for Online Capacity Expansion
8626 * so they need to be assembled. We have already
8627 * checked that no recovery etc is happening.
8629 result
= imsm_find_array_minor_by_subdev(member
->container_member
,
8633 dprintf("imsm: cannot find array\n");
8636 devices_that_can_grow
++;
8639 if (!member
&& devices_that_can_grow
)
8643 dprintf("\tContainer operation allowed\n");
8645 dprintf("\tError: %i\n", ret_val
);
8650 /* Function: get_spares_for_grow
8651 * Description: Allocates memory and creates list of spare devices
8652 * avaliable in container. Checks if spare drive size is acceptable.
8653 * Parameters: Pointer to the supertype structure
8654 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
8657 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
8659 unsigned long long min_size
= min_acceptable_spare_size_imsm(st
);
8660 return container_choose_spares(st
, min_size
, NULL
, NULL
, NULL
, 0);
8663 /******************************************************************************
8664 * function: imsm_create_metadata_update_for_reshape
8665 * Function creates update for whole IMSM container.
8667 ******************************************************************************/
8668 static int imsm_create_metadata_update_for_reshape(
8669 struct supertype
*st
,
8670 struct geo_params
*geo
,
8672 struct imsm_update_reshape
**updatep
)
8674 struct intel_super
*super
= st
->sb
;
8675 struct imsm_super
*mpb
= super
->anchor
;
8676 int update_memory_size
= 0;
8677 struct imsm_update_reshape
*u
= NULL
;
8678 struct mdinfo
*spares
= NULL
;
8680 int delta_disks
= 0;
8683 dprintf("imsm_update_metadata_for_reshape(enter) raid_disks = %i\n",
8686 delta_disks
= geo
->raid_disks
- old_raid_disks
;
8688 /* size of all update data without anchor */
8689 update_memory_size
= sizeof(struct imsm_update_reshape
);
8691 /* now add space for spare disks that we need to add. */
8692 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
8694 u
= calloc(1, update_memory_size
);
8697 "cannot get memory for imsm_update_reshape update\n");
8700 u
->type
= update_reshape_container_disks
;
8701 u
->old_raid_disks
= old_raid_disks
;
8702 u
->new_raid_disks
= geo
->raid_disks
;
8704 /* now get spare disks list
8706 spares
= get_spares_for_grow(st
);
8709 || delta_disks
> spares
->array
.spare_disks
) {
8710 fprintf(stderr
, Name
": imsm: ERROR: Cannot get spare devices "
8711 "for %s.\n", geo
->dev_name
);
8716 /* we have got spares
8717 * update disk list in imsm_disk list table in anchor
8719 dprintf("imsm: %i spares are available.\n\n",
8720 spares
->array
.spare_disks
);
8723 for (i
= 0; i
< delta_disks
; i
++) {
8728 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
8730 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
8731 dl
->index
= mpb
->num_disks
;
8741 dprintf("imsm: reshape update preparation :");
8742 if (i
== delta_disks
) {
8745 return update_memory_size
;
8748 dprintf(" Error\n");
8753 /******************************************************************************
8754 * function: imsm_create_metadata_update_for_migration()
8755 * Creates update for IMSM array.
8757 ******************************************************************************/
8758 static int imsm_create_metadata_update_for_migration(
8759 struct supertype
*st
,
8760 struct geo_params
*geo
,
8761 struct imsm_update_reshape_migration
**updatep
)
8763 struct intel_super
*super
= st
->sb
;
8764 int update_memory_size
= 0;
8765 struct imsm_update_reshape_migration
*u
= NULL
;
8766 struct imsm_dev
*dev
;
8767 int previous_level
= -1;
8769 dprintf("imsm_create_metadata_update_for_migration(enter)"
8770 " New Level = %i\n", geo
->level
);
8772 /* size of all update data without anchor */
8773 update_memory_size
= sizeof(struct imsm_update_reshape_migration
);
8775 u
= calloc(1, update_memory_size
);
8777 dprintf("error: cannot get memory for "
8778 "imsm_create_metadata_update_for_migration\n");
8781 u
->type
= update_reshape_migration
;
8782 u
->subdev
= super
->current_vol
;
8783 u
->new_level
= geo
->level
;
8784 u
->new_layout
= geo
->layout
;
8785 u
->new_raid_disks
= u
->old_raid_disks
= geo
->raid_disks
;
8786 u
->new_disks
[0] = -1;
8787 u
->new_chunksize
= -1;
8789 dev
= get_imsm_dev(super
, u
->subdev
);
8791 struct imsm_map
*map
;
8793 map
= get_imsm_map(dev
, 0);
8795 int current_chunk_size
=
8796 __le16_to_cpu(map
->blocks_per_strip
) / 2;
8798 if (geo
->chunksize
!= current_chunk_size
) {
8799 u
->new_chunksize
= geo
->chunksize
/ 1024;
8801 "chunk size change from %i to %i\n",
8802 current_chunk_size
, u
->new_chunksize
);
8804 previous_level
= map
->raid_level
;
8807 if ((geo
->level
== 5) && (previous_level
== 0)) {
8808 struct mdinfo
*spares
= NULL
;
8810 u
->new_raid_disks
++;
8811 spares
= get_spares_for_grow(st
);
8812 if ((spares
== NULL
) || (spares
->array
.spare_disks
< 1)) {
8815 update_memory_size
= 0;
8816 dprintf("error: cannot get spare device "
8817 "for requested migration");
8822 dprintf("imsm: reshape update preparation : OK\n");
8825 return update_memory_size
;
8828 static void imsm_update_metadata_locally(struct supertype
*st
,
8831 struct metadata_update mu
;
8836 mu
.space_list
= NULL
;
8838 imsm_prepare_update(st
, &mu
);
8839 imsm_process_update(st
, &mu
);
8841 while (mu
.space_list
) {
8842 void **space
= mu
.space_list
;
8843 mu
.space_list
= *space
;
8848 /***************************************************************************
8849 * Function: imsm_analyze_change
8850 * Description: Function analyze change for single volume
8851 * and validate if transition is supported
8852 * Parameters: Geometry parameters, supertype structure
8853 * Returns: Operation type code on success, -1 if fail
8854 ****************************************************************************/
8855 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
8856 struct geo_params
*geo
)
8865 getinfo_super_imsm_volume(st
, &info
, NULL
);
8866 if ((geo
->level
!= info
.array
.level
) &&
8867 (geo
->level
>= 0) &&
8868 (geo
->level
!= UnSet
)) {
8869 switch (info
.array
.level
) {
8871 if (geo
->level
== 5) {
8872 change
= CH_MIGRATION
;
8873 if (geo
->layout
!= ALGORITHM_LEFT_ASYMMETRIC
) {
8875 Name
" Error. Requested Layout "
8876 "not supported (left-asymmetric layout "
8877 "is supported only)!\n");
8879 goto analyse_change_exit
;
8881 layout
= geo
->layout
;
8883 devNumChange
= 1; /* parity disk added */
8884 } else if (geo
->level
== 10) {
8885 change
= CH_TAKEOVER
;
8887 devNumChange
= 2; /* two mirrors added */
8888 layout
= 0x102; /* imsm supported layout */
8893 if (geo
->level
== 0) {
8894 change
= CH_TAKEOVER
;
8896 devNumChange
= -(geo
->raid_disks
/2);
8897 layout
= 0; /* imsm raid0 layout */
8903 Name
" Error. Level Migration from %d to %d "
8905 info
.array
.level
, geo
->level
);
8906 goto analyse_change_exit
;
8909 geo
->level
= info
.array
.level
;
8911 if ((geo
->layout
!= info
.array
.layout
)
8912 && ((geo
->layout
!= UnSet
) && (geo
->layout
!= -1))) {
8913 change
= CH_MIGRATION
;
8914 if ((info
.array
.layout
== 0)
8915 && (info
.array
.level
== 5)
8916 && (geo
->layout
== 5)) {
8917 /* reshape 5 -> 4 */
8918 } else if ((info
.array
.layout
== 5)
8919 && (info
.array
.level
== 5)
8920 && (geo
->layout
== 0)) {
8921 /* reshape 4 -> 5 */
8926 Name
" Error. Layout Migration from %d to %d "
8928 info
.array
.layout
, geo
->layout
);
8930 goto analyse_change_exit
;
8933 geo
->layout
= info
.array
.layout
;
8935 if ((geo
->chunksize
> 0) && (geo
->chunksize
!= UnSet
)
8936 && (geo
->chunksize
!= info
.array
.chunk_size
))
8937 change
= CH_MIGRATION
;
8939 geo
->chunksize
= info
.array
.chunk_size
;
8941 chunk
= geo
->chunksize
/ 1024;
8942 if (!validate_geometry_imsm(st
,
8945 geo
->raid_disks
+ devNumChange
,
8952 struct intel_super
*super
= st
->sb
;
8953 struct imsm_super
*mpb
= super
->anchor
;
8955 if (mpb
->num_raid_devs
> 1) {
8957 Name
" Error. Cannot perform operation on %s"
8958 "- for this operation it MUST be single "
8959 "array in container\n",
8965 analyse_change_exit
:
8970 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
8972 struct intel_super
*super
= st
->sb
;
8973 struct imsm_update_takeover
*u
;
8975 u
= malloc(sizeof(struct imsm_update_takeover
));
8979 u
->type
= update_takeover
;
8980 u
->subarray
= super
->current_vol
;
8982 /* 10->0 transition */
8983 if (geo
->level
== 0)
8984 u
->direction
= R10_TO_R0
;
8986 /* 0->10 transition */
8987 if (geo
->level
== 10)
8988 u
->direction
= R0_TO_R10
;
8990 /* update metadata locally */
8991 imsm_update_metadata_locally(st
, u
,
8992 sizeof(struct imsm_update_takeover
));
8993 /* and possibly remotely */
8994 if (st
->update_tail
)
8995 append_metadata_update(st
, u
,
8996 sizeof(struct imsm_update_takeover
));
9003 static int imsm_reshape_super(struct supertype
*st
, long long size
, int level
,
9004 int layout
, int chunksize
, int raid_disks
,
9005 int delta_disks
, char *backup
, char *dev
,
9009 struct geo_params geo
;
9011 dprintf("imsm: reshape_super called.\n");
9013 memset(&geo
, 0, sizeof(struct geo_params
));
9016 geo
.dev_id
= st
->devnum
;
9019 geo
.layout
= layout
;
9020 geo
.chunksize
= chunksize
;
9021 geo
.raid_disks
= raid_disks
;
9022 if (delta_disks
!= UnSet
)
9023 geo
.raid_disks
+= delta_disks
;
9025 dprintf("\tfor level : %i\n", geo
.level
);
9026 dprintf("\tfor raid_disks : %i\n", geo
.raid_disks
);
9028 if (experimental() == 0)
9031 if (st
->container_dev
== st
->devnum
) {
9032 /* On container level we can only increase number of devices. */
9033 dprintf("imsm: info: Container operation\n");
9034 int old_raid_disks
= 0;
9036 if (imsm_reshape_is_allowed_on_container(
9037 st
, &geo
, &old_raid_disks
)) {
9038 struct imsm_update_reshape
*u
= NULL
;
9041 len
= imsm_create_metadata_update_for_reshape(
9042 st
, &geo
, old_raid_disks
, &u
);
9045 dprintf("imsm: Cannot prepare update\n");
9046 goto exit_imsm_reshape_super
;
9050 /* update metadata locally */
9051 imsm_update_metadata_locally(st
, u
, len
);
9052 /* and possibly remotely */
9053 if (st
->update_tail
)
9054 append_metadata_update(st
, u
, len
);
9059 fprintf(stderr
, Name
": (imsm) Operation "
9060 "is not allowed on this container\n");
9063 /* On volume level we support following operations
9064 * - takeover: raid10 -> raid0; raid0 -> raid10
9065 * - chunk size migration
9066 * - migration: raid5 -> raid0; raid0 -> raid5
9068 struct intel_super
*super
= st
->sb
;
9069 struct intel_dev
*dev
= super
->devlist
;
9071 dprintf("imsm: info: Volume operation\n");
9072 /* find requested device */
9074 if (imsm_find_array_minor_by_subdev(
9075 dev
->index
, st
->container_dev
, &devnum
) == 0
9076 && devnum
== geo
.dev_id
)
9081 fprintf(stderr
, Name
" Cannot find %s (%i) subarray\n",
9082 geo
.dev_name
, geo
.dev_id
);
9083 goto exit_imsm_reshape_super
;
9085 super
->current_vol
= dev
->index
;
9086 change
= imsm_analyze_change(st
, &geo
);
9089 ret_val
= imsm_takeover(st
, &geo
);
9091 case CH_MIGRATION
: {
9092 struct imsm_update_reshape_migration
*u
= NULL
;
9094 imsm_create_metadata_update_for_migration(
9098 "Cannot prepare update\n");
9102 /* update metadata locally */
9103 imsm_update_metadata_locally(st
, u
, len
);
9104 /* and possibly remotely */
9105 if (st
->update_tail
)
9106 append_metadata_update(st
, u
, len
);
9116 exit_imsm_reshape_super
:
9117 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
9121 /*******************************************************************************
9122 * Function: wait_for_reshape_imsm
9123 * Description: Function writes new sync_max value and waits until
9124 * reshape process reach new position
9126 * sra : general array info
9127 * ndata : number of disks in new array's layout
9130 * 1 : there is no reshape in progress,
9132 ******************************************************************************/
9133 int wait_for_reshape_imsm(struct mdinfo
*sra
, int ndata
)
9135 int fd
= sysfs_get_fd(sra
, NULL
, "reshape_position");
9136 unsigned long long completed
;
9137 /* to_complete : new sync_max position */
9138 unsigned long long to_complete
= sra
->reshape_progress
;
9139 unsigned long long position_to_set
= to_complete
/ ndata
;
9142 dprintf("imsm: wait_for_reshape_imsm() "
9143 "cannot open reshape_position\n");
9147 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
9148 dprintf("imsm: wait_for_reshape_imsm() "
9149 "cannot read reshape_position (no reshape in progres)\n");
9154 if (completed
> to_complete
) {
9155 dprintf("imsm: wait_for_reshape_imsm() "
9156 "wrong next position to set %llu (%llu)\n",
9157 to_complete
, completed
);
9161 dprintf("Position set: %llu\n", position_to_set
);
9162 if (sysfs_set_num(sra
, NULL
, "sync_max",
9163 position_to_set
) != 0) {
9164 dprintf("imsm: wait_for_reshape_imsm() "
9165 "cannot set reshape position to %llu\n",
9176 select(fd
+1, &rfds
, NULL
, NULL
, NULL
);
9177 if (sysfs_get_str(sra
, NULL
, "sync_action",
9179 strncmp(action
, "reshape", 7) != 0)
9181 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
9182 dprintf("imsm: wait_for_reshape_imsm() "
9183 "cannot read reshape_position (in loop)\n");
9187 } while (completed
< to_complete
);
9193 /*******************************************************************************
9194 * Function: check_degradation_change
9195 * Description: Check that array hasn't become failed.
9197 * info : for sysfs access
9198 * sources : source disks descriptors
9199 * degraded: previous degradation level
9202 ******************************************************************************/
9203 int check_degradation_change(struct mdinfo
*info
,
9207 unsigned long long new_degraded
;
9208 sysfs_get_ll(info
, NULL
, "degraded", &new_degraded
);
9209 if (new_degraded
!= (unsigned long long)degraded
) {
9210 /* check each device to ensure it is still working */
9213 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
9214 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
9216 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
9218 if (sysfs_get_str(info
,
9219 sd
, "state", sbuf
, 20) < 0 ||
9220 strstr(sbuf
, "faulty") ||
9221 strstr(sbuf
, "in_sync") == NULL
) {
9222 /* this device is dead */
9223 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
9224 if (sd
->disk
.raid_disk
>= 0 &&
9225 sources
[sd
->disk
.raid_disk
] >= 0) {
9227 sd
->disk
.raid_disk
]);
9228 sources
[sd
->disk
.raid_disk
] =
9237 return new_degraded
;
9240 /*******************************************************************************
9241 * Function: imsm_manage_reshape
9242 * Description: Function finds array under reshape and it manages reshape
9243 * process. It creates stripes backups (if required) and sets
9246 * afd : Backup handle (nattive) - not used
9247 * sra : general array info
9248 * reshape : reshape parameters - not used
9249 * st : supertype structure
9250 * blocks : size of critical section [blocks]
9251 * fds : table of source device descriptor
9252 * offsets : start of array (offest per devices)
9254 * destfd : table of destination device descriptor
9255 * destoffsets : table of destination offsets (per device)
9257 * 1 : success, reshape is done
9259 ******************************************************************************/
9260 static int imsm_manage_reshape(
9261 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
9262 struct supertype
*st
, unsigned long backup_blocks
,
9263 int *fds
, unsigned long long *offsets
,
9264 int dests
, int *destfd
, unsigned long long *destoffsets
)
9267 struct intel_super
*super
= st
->sb
;
9268 struct intel_dev
*dv
= NULL
;
9269 struct imsm_dev
*dev
= NULL
;
9270 struct imsm_map
*map_src
;
9271 int migr_vol_qan
= 0;
9272 int ndata
, odata
; /* [bytes] */
9273 int chunk
; /* [bytes] */
9274 struct migr_record
*migr_rec
;
9276 unsigned int buf_size
; /* [bytes] */
9277 unsigned long long max_position
; /* array size [bytes] */
9278 unsigned long long next_step
; /* [blocks]/[bytes] */
9279 unsigned long long old_data_stripe_length
;
9280 unsigned long long start_src
; /* [bytes] */
9281 unsigned long long start
; /* [bytes] */
9282 unsigned long long start_buf_shift
; /* [bytes] */
9284 int source_layout
= 0;
9286 if (!fds
|| !offsets
|| !sra
)
9289 /* Find volume during the reshape */
9290 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
9291 if (dv
->dev
->vol
.migr_type
== MIGR_GEN_MIGR
9292 && dv
->dev
->vol
.migr_state
== 1) {
9297 /* Only one volume can migrate at the same time */
9298 if (migr_vol_qan
!= 1) {
9299 fprintf(stderr
, Name
" : %s", migr_vol_qan
?
9300 "Number of migrating volumes greater than 1\n" :
9301 "There is no volume during migrationg\n");
9305 map_src
= get_imsm_map(dev
, 1);
9306 if (map_src
== NULL
)
9309 ndata
= imsm_num_data_members(dev
, 0);
9310 odata
= imsm_num_data_members(dev
, 1);
9312 chunk
= __le16_to_cpu(map_src
->blocks_per_strip
) * 512;
9313 old_data_stripe_length
= odata
* chunk
;
9315 migr_rec
= super
->migr_rec
;
9317 /* initialize migration record for start condition */
9318 if (sra
->reshape_progress
== 0)
9319 init_migr_record_imsm(st
, dev
, sra
);
9321 if (__le32_to_cpu(migr_rec
->rec_status
) != UNIT_SRC_NORMAL
) {
9322 dprintf("imsm: cannot restart migration when data "
9323 "are present in copy area.\n");
9329 buf_size
= __le32_to_cpu(migr_rec
->blocks_per_unit
) * 512;
9330 /* extend buffer size for parity disk */
9331 buf_size
+= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
9332 /* add space for stripe aligment */
9333 buf_size
+= old_data_stripe_length
;
9334 if (posix_memalign((void **)&buf
, 4096, buf_size
)) {
9335 dprintf("imsm: Cannot allocate checpoint buffer\n");
9339 max_position
= sra
->component_size
* ndata
;
9340 source_layout
= imsm_level_to_layout(map_src
->raid_level
);
9342 while (__le32_to_cpu(migr_rec
->curr_migr_unit
) <
9343 __le32_to_cpu(migr_rec
->num_migr_units
)) {
9344 /* current reshape position [blocks] */
9345 unsigned long long current_position
=
9346 __le32_to_cpu(migr_rec
->blocks_per_unit
)
9347 * __le32_to_cpu(migr_rec
->curr_migr_unit
);
9348 unsigned long long border
;
9350 /* Check that array hasn't become failed.
9352 degraded
= check_degradation_change(sra
, fds
, degraded
);
9354 dprintf("imsm: Abort reshape due to degradation"
9355 " level (%i)\n", degraded
);
9359 next_step
= __le32_to_cpu(migr_rec
->blocks_per_unit
);
9361 if ((current_position
+ next_step
) > max_position
)
9362 next_step
= max_position
- current_position
;
9364 start
= current_position
* 512;
9366 /* allign reading start to old geometry */
9367 start_buf_shift
= start
% old_data_stripe_length
;
9368 start_src
= start
- start_buf_shift
;
9370 border
= (start_src
/ odata
) - (start
/ ndata
);
9372 if (border
<= __le32_to_cpu(migr_rec
->dest_depth_per_unit
)) {
9373 /* save critical stripes to buf
9374 * start - start address of current unit
9376 * start_src - start address of current unit
9377 * to backup alligned to source array
9380 unsigned long long next_step_filler
= 0;
9381 unsigned long long copy_length
= next_step
* 512;
9383 /* allign copy area length to stripe in old geometry */
9384 next_step_filler
= ((copy_length
+ start_buf_shift
)
9385 % old_data_stripe_length
);
9386 if (next_step_filler
)
9387 next_step_filler
= (old_data_stripe_length
9388 - next_step_filler
);
9389 dprintf("save_stripes() parameters: start = %llu,"
9390 "\tstart_src = %llu,\tnext_step*512 = %llu,"
9391 "\tstart_in_buf_shift = %llu,"
9392 "\tnext_step_filler = %llu\n",
9393 start
, start_src
, copy_length
,
9394 start_buf_shift
, next_step_filler
);
9396 if (save_stripes(fds
, offsets
, map_src
->num_members
,
9397 chunk
, map_src
->raid_level
,
9398 source_layout
, 0, NULL
, start_src
,
9400 next_step_filler
+ start_buf_shift
,
9402 dprintf("imsm: Cannot save stripes"
9406 /* Convert data to destination format and store it
9407 * in backup general migration area
9409 if (save_backup_imsm(st
, dev
, sra
,
9410 buf
+ start_buf_shift
, copy_length
)) {
9411 dprintf("imsm: Cannot save stripes to "
9412 "target devices\n");
9415 if (save_checkpoint_imsm(st
, sra
,
9416 UNIT_SRC_IN_CP_AREA
)) {
9417 dprintf("imsm: Cannot write checkpoint to "
9418 "migration record (UNIT_SRC_IN_CP_AREA)\n");
9422 /* set next step to use whole border area */
9423 border
/= next_step
;
9425 next_step
*= border
;
9427 /* When data backed up, checkpoint stored,
9428 * kick the kernel to reshape unit of data
9430 next_step
= next_step
+ sra
->reshape_progress
;
9431 /* limit next step to array max position */
9432 if (next_step
> max_position
)
9433 next_step
= max_position
;
9434 sysfs_set_num(sra
, NULL
, "suspend_lo", sra
->reshape_progress
);
9435 sysfs_set_num(sra
, NULL
, "suspend_hi", next_step
);
9436 sra
->reshape_progress
= next_step
;
9438 /* wait until reshape finish */
9439 if (wait_for_reshape_imsm(sra
, ndata
) < 0) {
9440 dprintf("wait_for_reshape_imsm returned error!\n");
9444 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
9445 /* ignore error == 2, this can mean end of reshape here
9447 dprintf("imsm: Cannot write checkpoint to "
9448 "migration record (UNIT_SRC_NORMAL)\n");
9454 /* return '1' if done */
9462 #endif /* MDASSEMBLE */
9464 struct superswitch super_imsm
= {
9466 .examine_super
= examine_super_imsm
,
9467 .brief_examine_super
= brief_examine_super_imsm
,
9468 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
9469 .export_examine_super
= export_examine_super_imsm
,
9470 .detail_super
= detail_super_imsm
,
9471 .brief_detail_super
= brief_detail_super_imsm
,
9472 .write_init_super
= write_init_super_imsm
,
9473 .validate_geometry
= validate_geometry_imsm
,
9474 .add_to_super
= add_to_super_imsm
,
9475 .remove_from_super
= remove_from_super_imsm
,
9476 .detail_platform
= detail_platform_imsm
,
9477 .kill_subarray
= kill_subarray_imsm
,
9478 .update_subarray
= update_subarray_imsm
,
9479 .load_container
= load_container_imsm
,
9480 .default_geometry
= default_geometry_imsm
,
9481 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
9482 .reshape_super
= imsm_reshape_super
,
9483 .manage_reshape
= imsm_manage_reshape
,
9484 .recover_backup
= recover_backup_imsm
,
9486 .match_home
= match_home_imsm
,
9487 .uuid_from_super
= uuid_from_super_imsm
,
9488 .getinfo_super
= getinfo_super_imsm
,
9489 .getinfo_super_disks
= getinfo_super_disks_imsm
,
9490 .update_super
= update_super_imsm
,
9492 .avail_size
= avail_size_imsm
,
9493 .min_acceptable_spare_size
= min_acceptable_spare_size_imsm
,
9495 .compare_super
= compare_super_imsm
,
9497 .load_super
= load_super_imsm
,
9498 .init_super
= init_super_imsm
,
9499 .store_super
= store_super_imsm
,
9500 .free_super
= free_super_imsm
,
9501 .match_metadata_desc
= match_metadata_desc_imsm
,
9502 .container_content
= container_content_imsm
,
9510 .open_new
= imsm_open_new
,
9511 .set_array_state
= imsm_set_array_state
,
9512 .set_disk
= imsm_set_disk
,
9513 .sync_metadata
= imsm_sync_metadata
,
9514 .activate_spare
= imsm_activate_spare
,
9515 .process_update
= imsm_process_update
,
9516 .prepare_update
= imsm_prepare_update
,
9517 #endif /* MDASSEMBLE */