2 * mdadm - Intel(R) Matrix Storage Manager Support
4 * Copyright (C) 2002-2008 Intel Corporation
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20 #define HAVE_STDINT_H 1
24 #include "platform-intel.h"
30 /* MPB == Metadata Parameter Block */
31 #define MPB_SIGNATURE "Intel Raid ISM Cfg Sig. "
32 #define MPB_SIG_LEN (strlen(MPB_SIGNATURE))
33 #define MPB_VERSION_RAID0 "1.0.00"
34 #define MPB_VERSION_RAID1 "1.1.00"
35 #define MPB_VERSION_MANY_VOLUMES_PER_ARRAY "1.2.00"
36 #define MPB_VERSION_3OR4_DISK_ARRAY "1.2.01"
37 #define MPB_VERSION_RAID5 "1.2.02"
38 #define MPB_VERSION_5OR6_DISK_ARRAY "1.2.04"
39 #define MPB_VERSION_CNG "1.2.06"
40 #define MPB_VERSION_ATTRIBS "1.3.00"
41 #define MAX_SIGNATURE_LENGTH 32
42 #define MAX_RAID_SERIAL_LEN 16
45 #define MPB_ATTRIB_RAID0 __cpu_to_le32(0x00000001)
47 #define MPB_ATTRIB_RAID1 __cpu_to_le32(0x00000002)
49 #define MPB_ATTRIB_RAID10 __cpu_to_le32(0x00000004)
51 #define MPB_ATTRIB_RAID1E __cpu_to_le32(0x00000008)
53 #define MPB_ATTRIB_RAID5 __cpu_to_le32(0x00000010)
54 /* supports RAID CNG */
55 #define MPB_ATTRIB_RAIDCNG __cpu_to_le32(0x00000020)
56 /* supports expanded stripe sizes of 256K, 512K and 1MB */
57 #define MPB_ATTRIB_EXP_STRIPE_SIZE __cpu_to_le32(0x00000040)
59 /* The OROM Support RST Caching of Volumes */
60 #define MPB_ATTRIB_NVM __cpu_to_le32(0x02000000)
61 /* The OROM supports creating disks greater than 2TB */
62 #define MPB_ATTRIB_2TB_DISK __cpu_to_le32(0x04000000)
63 /* The OROM supports Bad Block Management */
64 #define MPB_ATTRIB_BBM __cpu_to_le32(0x08000000)
66 /* THe OROM Supports NVM Caching of Volumes */
67 #define MPB_ATTRIB_NEVER_USE2 __cpu_to_le32(0x10000000)
68 /* The OROM supports creating volumes greater than 2TB */
69 #define MPB_ATTRIB_2TB __cpu_to_le32(0x20000000)
70 /* originally for PMP, now it's wasted b/c. Never use this bit! */
71 #define MPB_ATTRIB_NEVER_USE __cpu_to_le32(0x40000000)
72 /* Verify MPB contents against checksum after reading MPB */
73 #define MPB_ATTRIB_CHECKSUM_VERIFY __cpu_to_le32(0x80000000)
75 /* Define all supported attributes that have to be accepted by mdadm
77 #define MPB_ATTRIB_SUPPORTED (MPB_ATTRIB_CHECKSUM_VERIFY | \
79 MPB_ATTRIB_2TB_DISK | \
84 MPB_ATTRIB_EXP_STRIPE_SIZE)
86 /* Define attributes that are unused but not harmful */
87 #define MPB_ATTRIB_IGNORED (MPB_ATTRIB_NEVER_USE)
89 #define MPB_SECTOR_CNT 2210
90 #define IMSM_RESERVED_SECTORS 4096
91 #define NUM_BLOCKS_DIRTY_STRIPE_REGION 2056
92 #define SECT_PER_MB_SHIFT 11
94 /* Disk configuration info. */
95 #define IMSM_MAX_DEVICES 255
97 __u8 serial
[MAX_RAID_SERIAL_LEN
];/* 0xD8 - 0xE7 ascii serial number */
98 __u32 total_blocks
; /* 0xE8 - 0xEB total blocks */
99 __u32 scsi_id
; /* 0xEC - 0xEF scsi ID */
100 #define SPARE_DISK __cpu_to_le32(0x01) /* Spare */
101 #define CONFIGURED_DISK __cpu_to_le32(0x02) /* Member of some RaidDev */
102 #define FAILED_DISK __cpu_to_le32(0x04) /* Permanent failure */
103 __u32 status
; /* 0xF0 - 0xF3 */
104 __u32 owner_cfg_num
; /* which config 0,1,2... owns this disk */
105 #define IMSM_DISK_FILLERS 4
106 __u32 filler
[IMSM_DISK_FILLERS
]; /* 0xF4 - 0x107 MPB_DISK_FILLERS for future expansion */
109 /* map selector for map managment
115 /* RAID map configuration infos. */
117 __u32 pba_of_lba0
; /* start address of partition */
118 __u32 blocks_per_member
;/* blocks per member */
119 __u32 num_data_stripes
; /* number of data stripes */
120 __u16 blocks_per_strip
;
121 __u8 map_state
; /* Normal, Uninitialized, Degraded, Failed */
122 #define IMSM_T_STATE_NORMAL 0
123 #define IMSM_T_STATE_UNINITIALIZED 1
124 #define IMSM_T_STATE_DEGRADED 2
125 #define IMSM_T_STATE_FAILED 3
127 #define IMSM_T_RAID0 0
128 #define IMSM_T_RAID1 1
129 #define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */
130 __u8 num_members
; /* number of member disks */
131 __u8 num_domains
; /* number of parity domains */
132 __u8 failed_disk_num
; /* valid only when state is degraded */
134 __u32 filler
[7]; /* expansion area */
135 #define IMSM_ORD_REBUILD (1 << 24)
136 __u32 disk_ord_tbl
[1]; /* disk_ord_tbl[num_members],
137 * top byte contains some flags
139 } __attribute__ ((packed
));
142 __u32 curr_migr_unit
;
143 __u32 checkpoint_id
; /* id to access curr_migr_unit */
144 __u8 migr_state
; /* Normal or Migrating */
146 #define MIGR_REBUILD 1
147 #define MIGR_VERIFY 2 /* analagous to echo check > sync_action */
148 #define MIGR_GEN_MIGR 3
149 #define MIGR_STATE_CHANGE 4
150 #define MIGR_REPAIR 5
151 __u8 migr_type
; /* Initializing, Rebuilding, ... */
153 __u8 fs_state
; /* fast-sync state for CnG (0xff == disabled) */
154 __u16 verify_errors
; /* number of mismatches */
155 __u16 bad_blocks
; /* number of bad blocks during verify */
157 struct imsm_map map
[1];
158 /* here comes another one if migr_state */
159 } __attribute__ ((packed
));
162 __u8 volume
[MAX_RAID_SERIAL_LEN
];
165 #define DEV_BOOTABLE __cpu_to_le32(0x01)
166 #define DEV_BOOT_DEVICE __cpu_to_le32(0x02)
167 #define DEV_READ_COALESCING __cpu_to_le32(0x04)
168 #define DEV_WRITE_COALESCING __cpu_to_le32(0x08)
169 #define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10)
170 #define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20)
171 #define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40)
172 #define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80)
173 #define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100)
174 #define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200)
175 #define DEV_CLONE_N_GO __cpu_to_le32(0x400)
176 #define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800)
177 #define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000)
178 __u32 status
; /* Persistent RaidDev status */
179 __u32 reserved_blocks
; /* Reserved blocks at beginning of volume */
183 __u8 cng_master_disk
;
187 #define IMSM_DEV_FILLERS 10
188 __u32 filler
[IMSM_DEV_FILLERS
];
190 } __attribute__ ((packed
));
193 __u8 sig
[MAX_SIGNATURE_LENGTH
]; /* 0x00 - 0x1F */
194 __u32 check_sum
; /* 0x20 - 0x23 MPB Checksum */
195 __u32 mpb_size
; /* 0x24 - 0x27 Size of MPB */
196 __u32 family_num
; /* 0x28 - 0x2B Checksum from first time this config was written */
197 __u32 generation_num
; /* 0x2C - 0x2F Incremented each time this array's MPB is written */
198 __u32 error_log_size
; /* 0x30 - 0x33 in bytes */
199 __u32 attributes
; /* 0x34 - 0x37 */
200 __u8 num_disks
; /* 0x38 Number of configured disks */
201 __u8 num_raid_devs
; /* 0x39 Number of configured volumes */
202 __u8 error_log_pos
; /* 0x3A */
203 __u8 fill
[1]; /* 0x3B */
204 __u32 cache_size
; /* 0x3c - 0x40 in mb */
205 __u32 orig_family_num
; /* 0x40 - 0x43 original family num */
206 __u32 pwr_cycle_count
; /* 0x44 - 0x47 simulated power cycle count for array */
207 __u32 bbm_log_size
; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */
208 #define IMSM_FILLERS 35
209 __u32 filler
[IMSM_FILLERS
]; /* 0x4C - 0xD7 RAID_MPB_FILLERS */
210 struct imsm_disk disk
[1]; /* 0xD8 diskTbl[numDisks] */
211 /* here comes imsm_dev[num_raid_devs] */
212 /* here comes BBM logs */
213 } __attribute__ ((packed
));
215 #define BBM_LOG_MAX_ENTRIES 254
217 struct bbm_log_entry
{
218 __u64 defective_block_start
;
219 #define UNREADABLE 0xFFFFFFFF
220 __u32 spare_block_offset
;
221 __u16 remapped_marked_count
;
223 } __attribute__ ((__packed__
));
226 __u32 signature
; /* 0xABADB10C */
228 __u32 reserved_spare_block_count
; /* 0 */
229 __u32 reserved
; /* 0xFFFF */
230 __u64 first_spare_lba
;
231 struct bbm_log_entry mapped_block_entries
[BBM_LOG_MAX_ENTRIES
];
232 } __attribute__ ((__packed__
));
236 static char *map_state_str
[] = { "normal", "uninitialized", "degraded", "failed" };
239 #define RAID_DISK_RESERVED_BLOCKS_IMSM_HI 2209
241 #define GEN_MIGR_AREA_SIZE 2048 /* General Migration Copy Area size in blocks */
243 #define MIGR_REC_BUF_SIZE 512 /* size of migr_record i/o buffer */
244 #define MIGR_REC_POSITION 512 /* migr_record position offset on disk,
245 * MIGR_REC_BUF_SIZE <= MIGR_REC_POSITION
249 #define UNIT_SRC_NORMAL 0 /* Source data for curr_migr_unit must
250 * be recovered using srcMap */
251 #define UNIT_SRC_IN_CP_AREA 1 /* Source data for curr_migr_unit has
252 * already been migrated and must
253 * be recovered from checkpoint area */
255 __u32 rec_status
; /* Status used to determine how to restart
256 * migration in case it aborts
258 __u32 curr_migr_unit
; /* 0..numMigrUnits-1 */
259 __u32 family_num
; /* Family number of MPB
260 * containing the RaidDev
261 * that is migrating */
262 __u32 ascending_migr
; /* True if migrating in increasing
264 __u32 blocks_per_unit
; /* Num disk blocks per unit of operation */
265 __u32 dest_depth_per_unit
; /* Num member blocks each destMap
267 * advances per unit-of-operation */
268 __u32 ckpt_area_pba
; /* Pba of first block of ckpt copy area */
269 __u32 dest_1st_member_lba
; /* First member lba on first
270 * stripe of destination */
271 __u32 num_migr_units
; /* Total num migration units-of-op */
272 __u32 post_migr_vol_cap
; /* Size of volume after
273 * migration completes */
274 __u32 post_migr_vol_cap_hi
; /* Expansion space for LBA64 */
275 __u32 ckpt_read_disk_num
; /* Which member disk in destSubMap[0] the
276 * migration ckpt record was read from
277 * (for recovered migrations) */
278 } __attribute__ ((__packed__
));
283 * 2: metadata does not match
291 struct md_list
*next
;
294 #define pr_vrb(fmt, arg...) (void) (verbose && fprintf(stderr, Name fmt, ##arg))
296 static __u8
migr_type(struct imsm_dev
*dev
)
298 if (dev
->vol
.migr_type
== MIGR_VERIFY
&&
299 dev
->status
& DEV_VERIFY_AND_FIX
)
302 return dev
->vol
.migr_type
;
305 static void set_migr_type(struct imsm_dev
*dev
, __u8 migr_type
)
307 /* for compatibility with older oroms convert MIGR_REPAIR, into
308 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
310 if (migr_type
== MIGR_REPAIR
) {
311 dev
->vol
.migr_type
= MIGR_VERIFY
;
312 dev
->status
|= DEV_VERIFY_AND_FIX
;
314 dev
->vol
.migr_type
= migr_type
;
315 dev
->status
&= ~DEV_VERIFY_AND_FIX
;
319 static unsigned int sector_count(__u32 bytes
)
321 return ((bytes
+ (512-1)) & (~(512-1))) / 512;
324 static unsigned int mpb_sectors(struct imsm_super
*mpb
)
326 return sector_count(__le32_to_cpu(mpb
->mpb_size
));
330 struct imsm_dev
*dev
;
331 struct intel_dev
*next
;
336 enum sys_dev_type type
;
339 struct intel_hba
*next
;
346 /* internal representation of IMSM metadata */
349 void *buf
; /* O_DIRECT buffer for reading/writing metadata */
350 struct imsm_super
*anchor
; /* immovable parameters */
353 void *migr_rec_buf
; /* buffer for I/O operations */
354 struct migr_record
*migr_rec
; /* migration record */
356 size_t len
; /* size of the 'buf' allocation */
357 void *next_buf
; /* for realloc'ing buf from the manager */
359 int updates_pending
; /* count of pending updates for mdmon */
360 int current_vol
; /* index of raid device undergoing creation */
361 __u32 create_offset
; /* common start for 'current_vol' */
362 __u32 random
; /* random data for seeding new family numbers */
363 struct intel_dev
*devlist
;
367 __u8 serial
[MAX_RAID_SERIAL_LEN
];
370 struct imsm_disk disk
;
373 struct extent
*e
; /* for determining freespace @ create */
374 int raiddisk
; /* slot to fill in autolayout */
376 } *disks
, *current_disk
;
377 struct dl
*disk_mgmt_list
; /* list of disks to add/remove while mdmon
379 struct dl
*missing
; /* disks removed while we weren't looking */
380 struct bbm_log
*bbm_log
;
381 struct intel_hba
*hba
; /* device path of the raid controller for this metadata */
382 const struct imsm_orom
*orom
; /* platform firmware support */
383 struct intel_super
*next
; /* (temp) list for disambiguating family_num */
387 struct imsm_disk disk
;
388 #define IMSM_UNKNOWN_OWNER (-1)
390 struct intel_disk
*next
;
394 unsigned long long start
, size
;
397 /* definitions of reshape process types */
398 enum imsm_reshape_type
{
403 /* definition of messages passed to imsm_process_update */
404 enum imsm_update_type
{
405 update_activate_spare
,
409 update_add_remove_disk
,
410 update_reshape_container_disks
,
411 update_reshape_migration
,
413 update_general_migration_checkpoint
,
416 struct imsm_update_activate_spare
{
417 enum imsm_update_type type
;
421 struct imsm_update_activate_spare
*next
;
434 enum takeover_direction
{
438 struct imsm_update_takeover
{
439 enum imsm_update_type type
;
441 enum takeover_direction direction
;
444 struct imsm_update_reshape
{
445 enum imsm_update_type type
;
449 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
452 struct imsm_update_reshape_migration
{
453 enum imsm_update_type type
;
456 /* fields for array migration changes
463 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
466 struct imsm_update_general_migration_checkpoint
{
467 enum imsm_update_type type
;
468 __u32 curr_migr_unit
;
472 __u8 serial
[MAX_RAID_SERIAL_LEN
];
475 struct imsm_update_create_array
{
476 enum imsm_update_type type
;
481 struct imsm_update_kill_array
{
482 enum imsm_update_type type
;
486 struct imsm_update_rename_array
{
487 enum imsm_update_type type
;
488 __u8 name
[MAX_RAID_SERIAL_LEN
];
492 struct imsm_update_add_remove_disk
{
493 enum imsm_update_type type
;
497 static const char *_sys_dev_type
[] = {
498 [SYS_DEV_UNKNOWN
] = "Unknown",
499 [SYS_DEV_SAS
] = "SAS",
500 [SYS_DEV_SATA
] = "SATA"
503 const char *get_sys_dev_type(enum sys_dev_type type
)
505 if (type
>= SYS_DEV_MAX
)
506 type
= SYS_DEV_UNKNOWN
;
508 return _sys_dev_type
[type
];
511 static struct intel_hba
* alloc_intel_hba(struct sys_dev
*device
)
513 struct intel_hba
*result
= malloc(sizeof(*result
));
515 result
->type
= device
->type
;
516 result
->path
= strdup(device
->path
);
518 if (result
->path
&& (result
->pci_id
= strrchr(result
->path
, '/')) != NULL
)
524 static struct intel_hba
* find_intel_hba(struct intel_hba
*hba
, struct sys_dev
*device
)
526 struct intel_hba
*result
=NULL
;
527 for (result
= hba
; result
; result
= result
->next
) {
528 if (result
->type
== device
->type
&& strcmp(result
->path
, device
->path
) == 0)
534 static int attach_hba_to_super(struct intel_super
*super
, struct sys_dev
*device
)
536 struct intel_hba
*hba
;
538 /* check if disk attached to Intel HBA */
539 hba
= find_intel_hba(super
->hba
, device
);
542 /* Check if HBA is already attached to super */
543 if (super
->hba
== NULL
) {
544 super
->hba
= alloc_intel_hba(device
);
549 /* Intel metadata allows for all disks attached to the same type HBA.
550 * Do not sypport odf HBA types mixing
552 if (device
->type
!= hba
->type
)
558 hba
->next
= alloc_intel_hba(device
);
562 static struct sys_dev
* find_disk_attached_hba(int fd
, const char *devname
)
564 struct sys_dev
*list
, *elem
, *prev
;
567 if ((list
= find_intel_devices()) == NULL
)
571 disk_path
= (char *) devname
;
573 disk_path
= diskfd_to_devpath(fd
);
580 for (prev
= NULL
, elem
= list
; elem
; prev
= elem
, elem
= elem
->next
) {
581 if (path_attached_to_hba(disk_path
, elem
->path
)) {
585 prev
->next
= elem
->next
;
587 if (disk_path
!= devname
)
593 if (disk_path
!= devname
)
601 static int find_intel_hba_capability(int fd
, struct intel_super
*super
,
604 static struct supertype
*match_metadata_desc_imsm(char *arg
)
606 struct supertype
*st
;
608 if (strcmp(arg
, "imsm") != 0 &&
609 strcmp(arg
, "default") != 0
613 st
= malloc(sizeof(*st
));
616 memset(st
, 0, sizeof(*st
));
617 st
->container_dev
= NoMdDev
;
618 st
->ss
= &super_imsm
;
619 st
->max_devs
= IMSM_MAX_DEVICES
;
620 st
->minor_version
= 0;
626 static __u8
*get_imsm_version(struct imsm_super
*mpb
)
628 return &mpb
->sig
[MPB_SIG_LEN
];
632 /* retrieve a disk directly from the anchor when the anchor is known to be
633 * up-to-date, currently only at load time
635 static struct imsm_disk
*__get_imsm_disk(struct imsm_super
*mpb
, __u8 index
)
637 if (index
>= mpb
->num_disks
)
639 return &mpb
->disk
[index
];
642 /* retrieve the disk description based on a index of the disk
645 static struct dl
*get_imsm_dl_disk(struct intel_super
*super
, __u8 index
)
649 for (d
= super
->disks
; d
; d
= d
->next
)
650 if (d
->index
== index
)
655 /* retrieve a disk from the parsed metadata */
656 static struct imsm_disk
*get_imsm_disk(struct intel_super
*super
, __u8 index
)
660 dl
= get_imsm_dl_disk(super
, index
);
667 /* generate a checksum directly from the anchor when the anchor is known to be
668 * up-to-date, currently only at load or write_super after coalescing
670 static __u32
__gen_imsm_checksum(struct imsm_super
*mpb
)
672 __u32 end
= mpb
->mpb_size
/ sizeof(end
);
673 __u32
*p
= (__u32
*) mpb
;
677 sum
+= __le32_to_cpu(*p
);
681 return sum
- __le32_to_cpu(mpb
->check_sum
);
684 static size_t sizeof_imsm_map(struct imsm_map
*map
)
686 return sizeof(struct imsm_map
) + sizeof(__u32
) * (map
->num_members
- 1);
689 struct imsm_map
*get_imsm_map(struct imsm_dev
*dev
, int second_map
)
691 /* A device can have 2 maps if it is in the middle of a migration.
693 * MAP_0 - we return the first map
694 * MAP_1 - we return the second map if it exists, else NULL
695 * MAP_X - we return the second map if it exists, else the first
697 struct imsm_map
*map
= &dev
->vol
.map
[0];
698 struct imsm_map
*map2
= NULL
;
700 if (dev
->vol
.migr_state
)
701 map2
= (void *)map
+ sizeof_imsm_map(map
);
703 switch (second_map
) {
720 /* return the size of the device.
721 * migr_state increases the returned size if map[0] were to be duplicated
723 static size_t sizeof_imsm_dev(struct imsm_dev
*dev
, int migr_state
)
725 size_t size
= sizeof(*dev
) - sizeof(struct imsm_map
) +
726 sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
728 /* migrating means an additional map */
729 if (dev
->vol
.migr_state
)
730 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_1
));
732 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
738 /* retrieve disk serial number list from a metadata update */
739 static struct disk_info
*get_disk_info(struct imsm_update_create_array
*update
)
742 struct disk_info
*inf
;
744 inf
= u
+ sizeof(*update
) - sizeof(struct imsm_dev
) +
745 sizeof_imsm_dev(&update
->dev
, 0);
751 static struct imsm_dev
*__get_imsm_dev(struct imsm_super
*mpb
, __u8 index
)
757 if (index
>= mpb
->num_raid_devs
)
760 /* devices start after all disks */
761 offset
= ((void *) &mpb
->disk
[mpb
->num_disks
]) - _mpb
;
763 for (i
= 0; i
<= index
; i
++)
765 return _mpb
+ offset
;
767 offset
+= sizeof_imsm_dev(_mpb
+ offset
, 0);
772 static struct imsm_dev
*get_imsm_dev(struct intel_super
*super
, __u8 index
)
774 struct intel_dev
*dv
;
776 if (index
>= super
->anchor
->num_raid_devs
)
778 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
779 if (dv
->index
== index
)
786 * == MAP_0 get first map
787 * == MAP_1 get second map
788 * == MAP_X than get map according to the current migr_state
790 static __u32
get_imsm_ord_tbl_ent(struct imsm_dev
*dev
,
794 struct imsm_map
*map
;
796 map
= get_imsm_map(dev
, second_map
);
798 /* top byte identifies disk under rebuild */
799 return __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
802 #define ord_to_idx(ord) (((ord) << 8) >> 8)
803 static __u32
get_imsm_disk_idx(struct imsm_dev
*dev
, int slot
, int second_map
)
805 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, second_map
);
807 return ord_to_idx(ord
);
810 static void set_imsm_ord_tbl_ent(struct imsm_map
*map
, int slot
, __u32 ord
)
812 map
->disk_ord_tbl
[slot
] = __cpu_to_le32(ord
);
815 static int get_imsm_disk_slot(struct imsm_map
*map
, unsigned idx
)
820 for (slot
= 0; slot
< map
->num_members
; slot
++) {
821 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
822 if (ord_to_idx(ord
) == idx
)
829 static int get_imsm_raid_level(struct imsm_map
*map
)
831 if (map
->raid_level
== 1) {
832 if (map
->num_members
== 2)
838 return map
->raid_level
;
841 static int cmp_extent(const void *av
, const void *bv
)
843 const struct extent
*a
= av
;
844 const struct extent
*b
= bv
;
845 if (a
->start
< b
->start
)
847 if (a
->start
> b
->start
)
852 static int count_memberships(struct dl
*dl
, struct intel_super
*super
)
857 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
858 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
859 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
861 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
868 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
);
870 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
)
872 /* find a list of used extents on the given physical device */
873 struct extent
*rv
, *e
;
875 int memberships
= count_memberships(dl
, super
);
878 /* trim the reserved area for spares, so they can join any array
879 * regardless of whether the OROM has assigned sectors from the
880 * IMSM_RESERVED_SECTORS region
883 reservation
= imsm_min_reserved_sectors(super
);
885 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
887 rv
= malloc(sizeof(struct extent
) * (memberships
+ 1));
892 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
893 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
894 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
896 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
897 e
->start
= __le32_to_cpu(map
->pba_of_lba0
);
898 e
->size
= __le32_to_cpu(map
->blocks_per_member
);
902 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
904 /* determine the start of the metadata
905 * when no raid devices are defined use the default
906 * ...otherwise allow the metadata to truncate the value
907 * as is the case with older versions of imsm
910 struct extent
*last
= &rv
[memberships
- 1];
913 remainder
= __le32_to_cpu(dl
->disk
.total_blocks
) -
914 (last
->start
+ last
->size
);
915 /* round down to 1k block to satisfy precision of the kernel
919 /* make sure remainder is still sane */
920 if (remainder
< (unsigned)ROUND_UP(super
->len
, 512) >> 9)
921 remainder
= ROUND_UP(super
->len
, 512) >> 9;
922 if (reservation
> remainder
)
923 reservation
= remainder
;
925 e
->start
= __le32_to_cpu(dl
->disk
.total_blocks
) - reservation
;
930 /* try to determine how much space is reserved for metadata from
931 * the last get_extents() entry, otherwise fallback to the
934 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
940 /* for spares just return a minimal reservation which will grow
941 * once the spare is picked up by an array
944 return MPB_SECTOR_CNT
;
946 e
= get_extents(super
, dl
);
948 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
950 /* scroll to last entry */
951 for (i
= 0; e
[i
].size
; i
++)
954 rv
= __le32_to_cpu(dl
->disk
.total_blocks
) - e
[i
].start
;
961 static int is_spare(struct imsm_disk
*disk
)
963 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
966 static int is_configured(struct imsm_disk
*disk
)
968 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
971 static int is_failed(struct imsm_disk
*disk
)
973 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
976 /* try to determine how much space is reserved for metadata from
977 * the last get_extents() entry on the smallest active disk,
978 * otherwise fallback to the default
980 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
)
984 __u32 min_active
, remainder
;
985 __u32 rv
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
986 struct dl
*dl
, *dl_min
= NULL
;
992 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
995 if (dl
->disk
.total_blocks
< min_active
|| min_active
== 0) {
997 min_active
= dl
->disk
.total_blocks
;
1003 /* find last lba used by subarrays on the smallest active disk */
1004 e
= get_extents(super
, dl_min
);
1007 for (i
= 0; e
[i
].size
; i
++)
1010 remainder
= min_active
- e
[i
].start
;
1013 /* to give priority to recovery we should not require full
1014 IMSM_RESERVED_SECTORS from the spare */
1015 rv
= MPB_SECTOR_CNT
+ NUM_BLOCKS_DIRTY_STRIPE_REGION
;
1017 /* if real reservation is smaller use that value */
1018 return (remainder
< rv
) ? remainder
: rv
;
1021 /* Return minimum size of a spare that can be used in this array*/
1022 static unsigned long long min_acceptable_spare_size_imsm(struct supertype
*st
)
1024 struct intel_super
*super
= st
->sb
;
1028 unsigned long long rv
= 0;
1032 /* find first active disk in array */
1034 while (dl
&& (is_failed(&dl
->disk
) || dl
->index
== -1))
1038 /* find last lba used by subarrays */
1039 e
= get_extents(super
, dl
);
1042 for (i
= 0; e
[i
].size
; i
++)
1045 rv
= e
[i
-1].start
+ e
[i
-1].size
;
1048 /* add the amount of space needed for metadata */
1049 rv
= rv
+ imsm_min_reserved_sectors(super
);
1054 static int is_gen_migration(struct imsm_dev
*dev
);
1057 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
1058 struct imsm_dev
*dev
);
1060 static void print_imsm_dev(struct intel_super
*super
,
1061 struct imsm_dev
*dev
,
1067 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1068 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
1072 printf("[%.16s]:\n", dev
->volume
);
1073 printf(" UUID : %s\n", uuid
);
1074 printf(" RAID Level : %d", get_imsm_raid_level(map
));
1076 printf(" <-- %d", get_imsm_raid_level(map2
));
1078 printf(" Members : %d", map
->num_members
);
1080 printf(" <-- %d", map2
->num_members
);
1082 printf(" Slots : [");
1083 for (i
= 0; i
< map
->num_members
; i
++) {
1084 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
1085 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1090 for (i
= 0; i
< map2
->num_members
; i
++) {
1091 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_1
);
1092 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1097 printf(" Failed disk : ");
1098 if (map
->failed_disk_num
== 0xff)
1101 printf("%i", map
->failed_disk_num
);
1103 slot
= get_imsm_disk_slot(map
, disk_idx
);
1105 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
1106 printf(" This Slot : %d%s\n", slot
,
1107 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
1109 printf(" This Slot : ?\n");
1110 sz
= __le32_to_cpu(dev
->size_high
);
1112 sz
+= __le32_to_cpu(dev
->size_low
);
1113 printf(" Array Size : %llu%s\n", (unsigned long long)sz
,
1114 human_size(sz
* 512));
1115 sz
= __le32_to_cpu(map
->blocks_per_member
);
1116 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz
,
1117 human_size(sz
* 512));
1118 printf(" Sector Offset : %u\n",
1119 __le32_to_cpu(map
->pba_of_lba0
));
1120 printf(" Num Stripes : %u\n",
1121 __le32_to_cpu(map
->num_data_stripes
));
1122 printf(" Chunk Size : %u KiB",
1123 __le16_to_cpu(map
->blocks_per_strip
) / 2);
1125 printf(" <-- %u KiB",
1126 __le16_to_cpu(map2
->blocks_per_strip
) / 2);
1128 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
1129 printf(" Migrate State : ");
1130 if (dev
->vol
.migr_state
) {
1131 if (migr_type(dev
) == MIGR_INIT
)
1132 printf("initialize\n");
1133 else if (migr_type(dev
) == MIGR_REBUILD
)
1134 printf("rebuild\n");
1135 else if (migr_type(dev
) == MIGR_VERIFY
)
1137 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
1138 printf("general migration\n");
1139 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
1140 printf("state change\n");
1141 else if (migr_type(dev
) == MIGR_REPAIR
)
1144 printf("<unknown:%d>\n", migr_type(dev
));
1147 printf(" Map State : %s", map_state_str
[map
->map_state
]);
1148 if (dev
->vol
.migr_state
) {
1149 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1151 printf(" <-- %s", map_state_str
[map
->map_state
]);
1152 printf("\n Checkpoint : %u ",
1153 __le32_to_cpu(dev
->vol
.curr_migr_unit
));
1154 if ((is_gen_migration(dev
)) && ((slot
> 1) || (slot
< 0)))
1157 printf("(%llu)", (unsigned long long)
1158 blocks_per_migr_unit(super
, dev
));
1161 printf(" Dirty State : %s\n", dev
->vol
.dirty
? "dirty" : "clean");
1164 static void print_imsm_disk(struct imsm_disk
*disk
, int index
, __u32 reserved
)
1166 char str
[MAX_RAID_SERIAL_LEN
+ 1];
1169 if (index
< -1 || !disk
)
1173 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
1175 printf(" Disk%02d Serial : %s\n", index
, str
);
1177 printf(" Disk Serial : %s\n", str
);
1178 printf(" State :%s%s%s\n", is_spare(disk
) ? " spare" : "",
1179 is_configured(disk
) ? " active" : "",
1180 is_failed(disk
) ? " failed" : "");
1181 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
1182 sz
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
1183 printf(" Usable Size : %llu%s\n", (unsigned long long)sz
,
1184 human_size(sz
* 512));
1187 void examine_migr_rec_imsm(struct intel_super
*super
)
1189 struct migr_record
*migr_rec
= super
->migr_rec
;
1190 struct imsm_super
*mpb
= super
->anchor
;
1193 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1194 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1195 struct imsm_map
*map
;
1198 if (is_gen_migration(dev
) == 0)
1201 printf("\nMigration Record Information:");
1203 /* first map under migration */
1204 map
= get_imsm_map(dev
, MAP_0
);
1206 slot
= get_imsm_disk_slot(map
, super
->disks
->index
);
1207 if ((map
== NULL
) || (slot
> 1) || (slot
< 0)) {
1208 printf(" Empty\n ");
1209 printf("Examine one of first two disks in array\n");
1212 printf("\n Status : ");
1213 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
1216 printf("Contains Data\n");
1217 printf(" Current Unit : %u\n",
1218 __le32_to_cpu(migr_rec
->curr_migr_unit
));
1219 printf(" Family : %u\n",
1220 __le32_to_cpu(migr_rec
->family_num
));
1221 printf(" Ascending : %u\n",
1222 __le32_to_cpu(migr_rec
->ascending_migr
));
1223 printf(" Blocks Per Unit : %u\n",
1224 __le32_to_cpu(migr_rec
->blocks_per_unit
));
1225 printf(" Dest. Depth Per Unit : %u\n",
1226 __le32_to_cpu(migr_rec
->dest_depth_per_unit
));
1227 printf(" Checkpoint Area pba : %u\n",
1228 __le32_to_cpu(migr_rec
->ckpt_area_pba
));
1229 printf(" First member lba : %u\n",
1230 __le32_to_cpu(migr_rec
->dest_1st_member_lba
));
1231 printf(" Total Number of Units : %u\n",
1232 __le32_to_cpu(migr_rec
->num_migr_units
));
1233 printf(" Size of volume : %u\n",
1234 __le32_to_cpu(migr_rec
->post_migr_vol_cap
));
1235 printf(" Expansion space for LBA64 : %u\n",
1236 __le32_to_cpu(migr_rec
->post_migr_vol_cap_hi
));
1237 printf(" Record was read from : %u\n",
1238 __le32_to_cpu(migr_rec
->ckpt_read_disk_num
));
1243 #endif /* MDASSEMBLE */
1244 /*******************************************************************************
1245 * function: imsm_check_attributes
1246 * Description: Function checks if features represented by attributes flags
1247 * are supported by mdadm.
1249 * attributes - Attributes read from metadata
1251 * 0 - passed attributes contains unsupported features flags
1252 * 1 - all features are supported
1253 ******************************************************************************/
1254 static int imsm_check_attributes(__u32 attributes
)
1257 __u32 not_supported
= MPB_ATTRIB_SUPPORTED
^0xffffffff;
1259 not_supported
&= ~MPB_ATTRIB_IGNORED
;
1261 not_supported
&= attributes
;
1262 if (not_supported
) {
1263 fprintf(stderr
, Name
"(IMSM): Unsupported attributes : %x\n",
1264 (unsigned)__le32_to_cpu(not_supported
));
1265 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1266 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
1267 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1269 if (not_supported
& MPB_ATTRIB_2TB
) {
1270 dprintf("\t\tMPB_ATTRIB_2TB\n");
1271 not_supported
^= MPB_ATTRIB_2TB
;
1273 if (not_supported
& MPB_ATTRIB_RAID0
) {
1274 dprintf("\t\tMPB_ATTRIB_RAID0\n");
1275 not_supported
^= MPB_ATTRIB_RAID0
;
1277 if (not_supported
& MPB_ATTRIB_RAID1
) {
1278 dprintf("\t\tMPB_ATTRIB_RAID1\n");
1279 not_supported
^= MPB_ATTRIB_RAID1
;
1281 if (not_supported
& MPB_ATTRIB_RAID10
) {
1282 dprintf("\t\tMPB_ATTRIB_RAID10\n");
1283 not_supported
^= MPB_ATTRIB_RAID10
;
1285 if (not_supported
& MPB_ATTRIB_RAID1E
) {
1286 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
1287 not_supported
^= MPB_ATTRIB_RAID1E
;
1289 if (not_supported
& MPB_ATTRIB_RAID5
) {
1290 dprintf("\t\tMPB_ATTRIB_RAID5\n");
1291 not_supported
^= MPB_ATTRIB_RAID5
;
1293 if (not_supported
& MPB_ATTRIB_RAIDCNG
) {
1294 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
1295 not_supported
^= MPB_ATTRIB_RAIDCNG
;
1297 if (not_supported
& MPB_ATTRIB_BBM
) {
1298 dprintf("\t\tMPB_ATTRIB_BBM\n");
1299 not_supported
^= MPB_ATTRIB_BBM
;
1301 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1302 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
1303 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1305 if (not_supported
& MPB_ATTRIB_EXP_STRIPE_SIZE
) {
1306 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
1307 not_supported
^= MPB_ATTRIB_EXP_STRIPE_SIZE
;
1309 if (not_supported
& MPB_ATTRIB_2TB_DISK
) {
1310 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
1311 not_supported
^= MPB_ATTRIB_2TB_DISK
;
1313 if (not_supported
& MPB_ATTRIB_NEVER_USE2
) {
1314 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
1315 not_supported
^= MPB_ATTRIB_NEVER_USE2
;
1317 if (not_supported
& MPB_ATTRIB_NEVER_USE
) {
1318 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
1319 not_supported
^= MPB_ATTRIB_NEVER_USE
;
1323 dprintf(Name
"(IMSM): Unknown attributes : %x\n", not_supported
);
1332 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
1334 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
1336 struct intel_super
*super
= st
->sb
;
1337 struct imsm_super
*mpb
= super
->anchor
;
1338 char str
[MAX_SIGNATURE_LENGTH
];
1343 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
1346 snprintf(str
, MPB_SIG_LEN
, "%s", mpb
->sig
);
1347 printf(" Magic : %s\n", str
);
1348 snprintf(str
, strlen(MPB_VERSION_RAID0
), "%s", get_imsm_version(mpb
));
1349 printf(" Version : %s\n", get_imsm_version(mpb
));
1350 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
1351 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
1352 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
1353 printf(" Attributes : ");
1354 if (imsm_check_attributes(mpb
->attributes
))
1355 printf("All supported\n");
1357 printf("not supported\n");
1358 getinfo_super_imsm(st
, &info
, NULL
);
1359 fname_from_uuid(st
, &info
, nbuf
, ':');
1360 printf(" UUID : %s\n", nbuf
+ 5);
1361 sum
= __le32_to_cpu(mpb
->check_sum
);
1362 printf(" Checksum : %08x %s\n", sum
,
1363 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
1364 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
));
1365 printf(" Disks : %d\n", mpb
->num_disks
);
1366 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
1367 print_imsm_disk(__get_imsm_disk(mpb
, super
->disks
->index
), super
->disks
->index
, reserved
);
1368 if (super
->bbm_log
) {
1369 struct bbm_log
*log
= super
->bbm_log
;
1372 printf("Bad Block Management Log:\n");
1373 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
1374 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
1375 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
1376 printf(" Spare Blocks : %d\n", __le32_to_cpu(log
->reserved_spare_block_count
));
1377 printf(" First Spare : %llx\n",
1378 (unsigned long long) __le64_to_cpu(log
->first_spare_lba
));
1380 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1382 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1384 super
->current_vol
= i
;
1385 getinfo_super_imsm(st
, &info
, NULL
);
1386 fname_from_uuid(st
, &info
, nbuf
, ':');
1387 print_imsm_dev(super
, dev
, nbuf
+ 5, super
->disks
->index
);
1389 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1390 if (i
== super
->disks
->index
)
1392 print_imsm_disk(__get_imsm_disk(mpb
, i
), i
, reserved
);
1395 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
1396 if (dl
->index
== -1)
1397 print_imsm_disk(&dl
->disk
, -1, reserved
);
1399 examine_migr_rec_imsm(super
);
1402 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
1404 /* We just write a generic IMSM ARRAY entry */
1407 struct intel_super
*super
= st
->sb
;
1409 if (!super
->anchor
->num_raid_devs
) {
1410 printf("ARRAY metadata=imsm\n");
1414 getinfo_super_imsm(st
, &info
, NULL
);
1415 fname_from_uuid(st
, &info
, nbuf
, ':');
1416 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
1419 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
1421 /* We just write a generic IMSM ARRAY entry */
1425 struct intel_super
*super
= st
->sb
;
1428 if (!super
->anchor
->num_raid_devs
)
1431 getinfo_super_imsm(st
, &info
, NULL
);
1432 fname_from_uuid(st
, &info
, nbuf
, ':');
1433 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1434 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1436 super
->current_vol
= i
;
1437 getinfo_super_imsm(st
, &info
, NULL
);
1438 fname_from_uuid(st
, &info
, nbuf1
, ':');
1439 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
1440 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
1444 static void export_examine_super_imsm(struct supertype
*st
)
1446 struct intel_super
*super
= st
->sb
;
1447 struct imsm_super
*mpb
= super
->anchor
;
1451 getinfo_super_imsm(st
, &info
, NULL
);
1452 fname_from_uuid(st
, &info
, nbuf
, ':');
1453 printf("MD_METADATA=imsm\n");
1454 printf("MD_LEVEL=container\n");
1455 printf("MD_UUID=%s\n", nbuf
+5);
1456 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
1459 static void detail_super_imsm(struct supertype
*st
, char *homehost
)
1464 getinfo_super_imsm(st
, &info
, NULL
);
1465 fname_from_uuid(st
, &info
, nbuf
, ':');
1466 printf("\n UUID : %s\n", nbuf
+ 5);
1469 static void brief_detail_super_imsm(struct supertype
*st
)
1473 getinfo_super_imsm(st
, &info
, NULL
);
1474 fname_from_uuid(st
, &info
, nbuf
, ':');
1475 printf(" UUID=%s", nbuf
+ 5);
1478 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
);
1479 static void fd2devname(int fd
, char *name
);
1481 static int ahci_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
1483 /* dump an unsorted list of devices attached to AHCI Intel storage
1484 * controller, as well as non-connected ports
1486 int hba_len
= strlen(hba_path
) + 1;
1491 unsigned long port_mask
= (1 << port_count
) - 1;
1493 if (port_count
> (int)sizeof(port_mask
) * 8) {
1495 fprintf(stderr
, Name
": port_count %d out of range\n", port_count
);
1499 /* scroll through /sys/dev/block looking for devices attached to
1502 dir
= opendir("/sys/dev/block");
1503 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
1514 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
1516 path
= devt_to_devpath(makedev(major
, minor
));
1519 if (!path_attached_to_hba(path
, hba_path
)) {
1525 /* retrieve the scsi device type */
1526 if (asprintf(&device
, "/sys/dev/block/%d:%d/device/xxxxxxx", major
, minor
) < 0) {
1528 fprintf(stderr
, Name
": failed to allocate 'device'\n");
1532 sprintf(device
, "/sys/dev/block/%d:%d/device/type", major
, minor
);
1533 if (load_sys(device
, buf
) != 0) {
1535 fprintf(stderr
, Name
": failed to read device type for %s\n",
1541 type
= strtoul(buf
, NULL
, 10);
1543 /* if it's not a disk print the vendor and model */
1544 if (!(type
== 0 || type
== 7 || type
== 14)) {
1547 sprintf(device
, "/sys/dev/block/%d:%d/device/vendor", major
, minor
);
1548 if (load_sys(device
, buf
) == 0) {
1549 strncpy(vendor
, buf
, sizeof(vendor
));
1550 vendor
[sizeof(vendor
) - 1] = '\0';
1551 c
= (char *) &vendor
[sizeof(vendor
) - 1];
1552 while (isspace(*c
) || *c
== '\0')
1556 sprintf(device
, "/sys/dev/block/%d:%d/device/model", major
, minor
);
1557 if (load_sys(device
, buf
) == 0) {
1558 strncpy(model
, buf
, sizeof(model
));
1559 model
[sizeof(model
) - 1] = '\0';
1560 c
= (char *) &model
[sizeof(model
) - 1];
1561 while (isspace(*c
) || *c
== '\0')
1565 if (vendor
[0] && model
[0])
1566 sprintf(buf
, "%.64s %.64s", vendor
, model
);
1568 switch (type
) { /* numbers from hald/linux/device.c */
1569 case 1: sprintf(buf
, "tape"); break;
1570 case 2: sprintf(buf
, "printer"); break;
1571 case 3: sprintf(buf
, "processor"); break;
1573 case 5: sprintf(buf
, "cdrom"); break;
1574 case 6: sprintf(buf
, "scanner"); break;
1575 case 8: sprintf(buf
, "media_changer"); break;
1576 case 9: sprintf(buf
, "comm"); break;
1577 case 12: sprintf(buf
, "raid"); break;
1578 default: sprintf(buf
, "unknown");
1584 /* chop device path to 'host%d' and calculate the port number */
1585 c
= strchr(&path
[hba_len
], '/');
1588 fprintf(stderr
, Name
": %s - invalid path name\n", path
+ hba_len
);
1593 if (sscanf(&path
[hba_len
], "host%d", &port
) == 1)
1597 *c
= '/'; /* repair the full string */
1598 fprintf(stderr
, Name
": failed to determine port number for %s\n",
1605 /* mark this port as used */
1606 port_mask
&= ~(1 << port
);
1608 /* print out the device information */
1610 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
1614 fd
= dev_open(ent
->d_name
, O_RDONLY
);
1616 printf(" Port%d : - disk info unavailable -\n", port
);
1618 fd2devname(fd
, buf
);
1619 printf(" Port%d : %s", port
, buf
);
1620 if (imsm_read_serial(fd
, NULL
, (__u8
*) buf
) == 0)
1621 printf(" (%.*s)\n", MAX_RAID_SERIAL_LEN
, buf
);
1636 for (i
= 0; i
< port_count
; i
++)
1637 if (port_mask
& (1 << i
))
1638 printf(" Port%d : - no device attached -\n", i
);
1644 static void print_found_intel_controllers(struct sys_dev
*elem
)
1646 for (; elem
; elem
= elem
->next
) {
1647 fprintf(stderr
, Name
": found Intel(R) ");
1648 if (elem
->type
== SYS_DEV_SATA
)
1649 fprintf(stderr
, "SATA ");
1650 else if (elem
->type
== SYS_DEV_SAS
)
1651 fprintf(stderr
, "SAS ");
1652 fprintf(stderr
, "RAID controller");
1654 fprintf(stderr
, " at %s", elem
->pci_id
);
1655 fprintf(stderr
, ".\n");
1660 static int ahci_get_port_count(const char *hba_path
, int *port_count
)
1667 if ((dir
= opendir(hba_path
)) == NULL
)
1670 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
1673 if (sscanf(ent
->d_name
, "host%d", &host
) != 1)
1675 if (*port_count
== 0)
1677 else if (host
< host_base
)
1680 if (host
+ 1 > *port_count
+ host_base
)
1681 *port_count
= host
+ 1 - host_base
;
1687 static void print_imsm_capability(const struct imsm_orom
*orom
)
1689 printf(" Platform : Intel(R) Matrix Storage Manager\n");
1690 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
1691 orom
->hotfix_ver
, orom
->build
);
1692 printf(" RAID Levels :%s%s%s%s%s\n",
1693 imsm_orom_has_raid0(orom
) ? " raid0" : "",
1694 imsm_orom_has_raid1(orom
) ? " raid1" : "",
1695 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
1696 imsm_orom_has_raid10(orom
) ? " raid10" : "",
1697 imsm_orom_has_raid5(orom
) ? " raid5" : "");
1698 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1699 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
1700 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
1701 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
1702 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
1703 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
1704 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
1705 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
1706 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
1707 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
1708 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
1709 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
1710 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
1711 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
1712 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
1713 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
1714 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
1715 printf(" Max Disks : %d\n", orom
->tds
);
1716 printf(" Max Volumes : %d\n", orom
->vpa
);
1720 static int detail_platform_imsm(int verbose
, int enumerate_only
)
1722 /* There are two components to imsm platform support, the ahci SATA
1723 * controller and the option-rom. To find the SATA controller we
1724 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
1725 * controller with the Intel vendor id is present. This approach
1726 * allows mdadm to leverage the kernel's ahci detection logic, with the
1727 * caveat that if ahci.ko is not loaded mdadm will not be able to
1728 * detect platform raid capabilities. The option-rom resides in a
1729 * platform "Adapter ROM". We scan for its signature to retrieve the
1730 * platform capabilities. If raid support is disabled in the BIOS the
1731 * option-rom capability structure will not be available.
1733 const struct imsm_orom
*orom
;
1734 struct sys_dev
*list
, *hba
;
1739 if (enumerate_only
) {
1740 if (check_env("IMSM_NO_PLATFORM"))
1742 list
= find_intel_devices();
1745 for (hba
= list
; hba
; hba
= hba
->next
) {
1746 orom
= find_imsm_capability(hba
->type
);
1752 free_sys_dev(&list
);
1756 list
= find_intel_devices();
1759 fprintf(stderr
, Name
": no active Intel(R) RAID "
1760 "controller found.\n");
1761 free_sys_dev(&list
);
1764 print_found_intel_controllers(list
);
1766 for (hba
= list
; hba
; hba
= hba
->next
) {
1767 orom
= find_imsm_capability(hba
->type
);
1769 fprintf(stderr
, Name
": imsm capabilities not found for controller: %s (type %s)\n",
1770 hba
->path
, get_sys_dev_type(hba
->type
));
1772 print_imsm_capability(orom
);
1775 for (hba
= list
; hba
; hba
= hba
->next
) {
1776 printf(" I/O Controller : %s (%s)\n",
1777 hba
->path
, get_sys_dev_type(hba
->type
));
1779 if (hba
->type
== SYS_DEV_SATA
) {
1780 host_base
= ahci_get_port_count(hba
->path
, &port_count
);
1781 if (ahci_enumerate_ports(hba
->path
, port_count
, host_base
, verbose
)) {
1783 fprintf(stderr
, Name
": failed to enumerate "
1784 "ports on SATA controller at %s.", hba
->pci_id
);
1790 free_sys_dev(&list
);
1795 static int match_home_imsm(struct supertype
*st
, char *homehost
)
1797 /* the imsm metadata format does not specify any host
1798 * identification information. We return -1 since we can never
1799 * confirm nor deny whether a given array is "meant" for this
1800 * host. We rely on compare_super and the 'family_num' fields to
1801 * exclude member disks that do not belong, and we rely on
1802 * mdadm.conf to specify the arrays that should be assembled.
1803 * Auto-assembly may still pick up "foreign" arrays.
1809 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
1811 /* The uuid returned here is used for:
1812 * uuid to put into bitmap file (Create, Grow)
1813 * uuid for backup header when saving critical section (Grow)
1814 * comparing uuids when re-adding a device into an array
1815 * In these cases the uuid required is that of the data-array,
1816 * not the device-set.
1817 * uuid to recognise same set when adding a missing device back
1818 * to an array. This is a uuid for the device-set.
1820 * For each of these we can make do with a truncated
1821 * or hashed uuid rather than the original, as long as
1823 * In each case the uuid required is that of the data-array,
1824 * not the device-set.
1826 /* imsm does not track uuid's so we synthesis one using sha1 on
1827 * - The signature (Which is constant for all imsm array, but no matter)
1828 * - the orig_family_num of the container
1829 * - the index number of the volume
1830 * - the 'serial' number of the volume.
1831 * Hopefully these are all constant.
1833 struct intel_super
*super
= st
->sb
;
1836 struct sha1_ctx ctx
;
1837 struct imsm_dev
*dev
= NULL
;
1840 /* some mdadm versions failed to set ->orig_family_num, in which
1841 * case fall back to ->family_num. orig_family_num will be
1842 * fixed up with the first metadata update.
1844 family_num
= super
->anchor
->orig_family_num
;
1845 if (family_num
== 0)
1846 family_num
= super
->anchor
->family_num
;
1847 sha1_init_ctx(&ctx
);
1848 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
1849 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
1850 if (super
->current_vol
>= 0)
1851 dev
= get_imsm_dev(super
, super
->current_vol
);
1853 __u32 vol
= super
->current_vol
;
1854 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
1855 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
1857 sha1_finish_ctx(&ctx
, buf
);
1858 memcpy(uuid
, buf
, 4*4);
1863 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
1865 __u8
*v
= get_imsm_version(mpb
);
1866 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
1867 char major
[] = { 0, 0, 0 };
1868 char minor
[] = { 0 ,0, 0 };
1869 char patch
[] = { 0, 0, 0 };
1870 char *ver_parse
[] = { major
, minor
, patch
};
1874 while (*v
!= '\0' && v
< end
) {
1875 if (*v
!= '.' && j
< 2)
1876 ver_parse
[i
][j
++] = *v
;
1884 *m
= strtol(minor
, NULL
, 0);
1885 *p
= strtol(patch
, NULL
, 0);
1889 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
1891 /* migr_strip_size when repairing or initializing parity */
1892 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1893 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1895 switch (get_imsm_raid_level(map
)) {
1900 return 128*1024 >> 9;
1904 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
1906 /* migr_strip_size when rebuilding a degraded disk, no idea why
1907 * this is different than migr_strip_size_resync(), but it's good
1910 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1911 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1913 switch (get_imsm_raid_level(map
)) {
1916 if (map
->num_members
% map
->num_domains
== 0)
1917 return 128*1024 >> 9;
1921 return max((__u32
) 64*1024 >> 9, chunk
);
1923 return 128*1024 >> 9;
1927 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
1929 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
1930 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
1931 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
1932 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
1934 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
1937 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
1939 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
1940 int level
= get_imsm_raid_level(lo
);
1942 if (level
== 1 || level
== 10) {
1943 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
1945 return hi
->num_domains
;
1947 return num_stripes_per_unit_resync(dev
);
1950 static __u8
imsm_num_data_members(struct imsm_dev
*dev
, int second_map
)
1952 /* named 'imsm_' because raid0, raid1 and raid10
1953 * counter-intuitively have the same number of data disks
1955 struct imsm_map
*map
= get_imsm_map(dev
, second_map
);
1957 switch (get_imsm_raid_level(map
)) {
1961 return map
->num_members
;
1963 return map
->num_members
- 1;
1965 dprintf("%s: unsupported raid level\n", __func__
);
1970 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
1972 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1973 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1975 switch(get_imsm_raid_level(map
)) {
1978 return chunk
* map
->num_domains
;
1980 return chunk
* map
->num_members
;
1986 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
1988 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1989 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1990 __u32 strip
= block
/ chunk
;
1992 switch (get_imsm_raid_level(map
)) {
1995 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
1996 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
1998 return vol_stripe
* chunk
+ block
% chunk
;
2000 __u32 stripe
= strip
/ (map
->num_members
- 1);
2002 return stripe
* chunk
+ block
% chunk
;
2009 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
2010 struct imsm_dev
*dev
)
2012 /* calculate the conversion factor between per member 'blocks'
2013 * (md/{resync,rebuild}_start) and imsm migration units, return
2014 * 0 for the 'not migrating' and 'unsupported migration' cases
2016 if (!dev
->vol
.migr_state
)
2019 switch (migr_type(dev
)) {
2020 case MIGR_GEN_MIGR
: {
2021 struct migr_record
*migr_rec
= super
->migr_rec
;
2022 return __le32_to_cpu(migr_rec
->blocks_per_unit
);
2027 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2028 __u32 stripes_per_unit
;
2029 __u32 blocks_per_unit
;
2038 /* yes, this is really the translation of migr_units to
2039 * per-member blocks in the 'resync' case
2041 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
2042 migr_chunk
= migr_strip_blocks_resync(dev
);
2043 disks
= imsm_num_data_members(dev
, MAP_0
);
2044 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
2045 stripe
= __le16_to_cpu(map
->blocks_per_strip
) * disks
;
2046 segment
= blocks_per_unit
/ stripe
;
2047 block_rel
= blocks_per_unit
- segment
* stripe
;
2048 parity_depth
= parity_segment_depth(dev
);
2049 block_map
= map_migr_block(dev
, block_rel
);
2050 return block_map
+ parity_depth
* segment
;
2052 case MIGR_REBUILD
: {
2053 __u32 stripes_per_unit
;
2056 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
2057 migr_chunk
= migr_strip_blocks_rebuild(dev
);
2058 return migr_chunk
* stripes_per_unit
;
2060 case MIGR_STATE_CHANGE
:
2066 static int imsm_level_to_layout(int level
)
2074 return ALGORITHM_LEFT_ASYMMETRIC
;
2081 /*******************************************************************************
2082 * Function: read_imsm_migr_rec
2083 * Description: Function reads imsm migration record from last sector of disk
2085 * fd : disk descriptor
2086 * super : metadata info
2090 ******************************************************************************/
2091 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
)
2094 unsigned long long dsize
;
2096 get_dev_size(fd
, NULL
, &dsize
);
2097 if (lseek64(fd
, dsize
- MIGR_REC_POSITION
, SEEK_SET
) < 0) {
2099 Name
": Cannot seek to anchor block: %s\n",
2103 if (read(fd
, super
->migr_rec_buf
, MIGR_REC_BUF_SIZE
) !=
2104 MIGR_REC_BUF_SIZE
) {
2106 Name
": Cannot read migr record block: %s\n",
2116 static struct imsm_dev
*imsm_get_device_during_migration(
2117 struct intel_super
*super
)
2120 struct intel_dev
*dv
;
2122 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
2123 if (is_gen_migration(dv
->dev
))
2129 /*******************************************************************************
2130 * Function: load_imsm_migr_rec
2131 * Description: Function reads imsm migration record (it is stored at the last
2134 * super : imsm internal array info
2135 * info : general array info
2139 * -2 : no migration in progress
2140 ******************************************************************************/
2141 static int load_imsm_migr_rec(struct intel_super
*super
, struct mdinfo
*info
)
2144 struct dl
*dl
= NULL
;
2148 struct imsm_dev
*dev
;
2149 struct imsm_map
*map
= NULL
;
2152 /* find map under migration */
2153 dev
= imsm_get_device_during_migration(super
);
2154 /* nothing to load,no migration in progress?
2158 map
= get_imsm_map(dev
, MAP_0
);
2161 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
2162 /* skip spare and failed disks
2164 if (sd
->disk
.raid_disk
< 0)
2166 /* read only from one of the first two slots */
2168 slot
= get_imsm_disk_slot(map
,
2169 sd
->disk
.raid_disk
);
2170 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2173 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
2174 fd
= dev_open(nm
, O_RDONLY
);
2180 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
2181 /* skip spare and failed disks
2185 /* read only from one of the first two slots */
2187 slot
= get_imsm_disk_slot(map
, dl
->index
);
2188 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2190 sprintf(nm
, "%d:%d", dl
->major
, dl
->minor
);
2191 fd
= dev_open(nm
, O_RDONLY
);
2198 retval
= read_imsm_migr_rec(fd
, super
);
2207 /*******************************************************************************
2208 * function: imsm_create_metadata_checkpoint_update
2209 * Description: It creates update for checkpoint change.
2211 * super : imsm internal array info
2212 * u : pointer to prepared update
2215 * If length is equal to 0, input pointer u contains no update
2216 ******************************************************************************/
2217 static int imsm_create_metadata_checkpoint_update(
2218 struct intel_super
*super
,
2219 struct imsm_update_general_migration_checkpoint
**u
)
2222 int update_memory_size
= 0;
2224 dprintf("imsm_create_metadata_checkpoint_update(enter)\n");
2230 /* size of all update data without anchor */
2231 update_memory_size
=
2232 sizeof(struct imsm_update_general_migration_checkpoint
);
2234 *u
= calloc(1, update_memory_size
);
2236 dprintf("error: cannot get memory for "
2237 "imsm_create_metadata_checkpoint_update update\n");
2240 (*u
)->type
= update_general_migration_checkpoint
;
2241 (*u
)->curr_migr_unit
= __le32_to_cpu(super
->migr_rec
->curr_migr_unit
);
2242 dprintf("imsm_create_metadata_checkpoint_update: prepared for %u\n",
2243 (*u
)->curr_migr_unit
);
2245 return update_memory_size
;
2249 static void imsm_update_metadata_locally(struct supertype
*st
,
2250 void *buf
, int len
);
2252 /*******************************************************************************
2253 * Function: write_imsm_migr_rec
2254 * Description: Function writes imsm migration record
2255 * (at the last sector of disk)
2257 * super : imsm internal array info
2261 ******************************************************************************/
2262 static int write_imsm_migr_rec(struct supertype
*st
)
2264 struct intel_super
*super
= st
->sb
;
2265 unsigned long long dsize
;
2271 struct imsm_update_general_migration_checkpoint
*u
;
2272 struct imsm_dev
*dev
;
2273 struct imsm_map
*map
= NULL
;
2275 /* find map under migration */
2276 dev
= imsm_get_device_during_migration(super
);
2277 /* if no migration, write buffer anyway to clear migr_record
2278 * on disk based on first available device
2281 dev
= get_imsm_dev(super
, super
->current_vol
< 0 ? 0 :
2282 super
->current_vol
);
2284 map
= get_imsm_map(dev
, MAP_0
);
2286 for (sd
= super
->disks
; sd
; sd
= sd
->next
) {
2289 /* skip failed and spare devices */
2292 /* write to 2 first slots only */
2294 slot
= get_imsm_disk_slot(map
, sd
->index
);
2295 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2298 sprintf(nm
, "%d:%d", sd
->major
, sd
->minor
);
2299 fd
= dev_open(nm
, O_RDWR
);
2302 get_dev_size(fd
, NULL
, &dsize
);
2303 if (lseek64(fd
, dsize
- MIGR_REC_POSITION
, SEEK_SET
) < 0) {
2305 Name
": Cannot seek to anchor block: %s\n",
2309 if (write(fd
, super
->migr_rec_buf
, MIGR_REC_BUF_SIZE
) !=
2310 MIGR_REC_BUF_SIZE
) {
2312 Name
": Cannot write migr record block: %s\n",
2319 /* update checkpoint information in metadata */
2320 len
= imsm_create_metadata_checkpoint_update(super
, &u
);
2323 dprintf("imsm: Cannot prepare update\n");
2326 /* update metadata locally */
2327 imsm_update_metadata_locally(st
, u
, len
);
2328 /* and possibly remotely */
2329 if (st
->update_tail
) {
2330 append_metadata_update(st
, u
, len
);
2331 /* during reshape we do all work inside metadata handler
2332 * manage_reshape(), so metadata update has to be triggered
2335 flush_metadata_updates(st
);
2336 st
->update_tail
= &st
->updates
;
2346 #endif /* MDASSEMBLE */
2348 /* spare/missing disks activations are not allowe when
2349 * array/container performs reshape operation, because
2350 * all arrays in container works on the same disks set
2352 int imsm_reshape_blocks_arrays_changes(struct intel_super
*super
)
2355 struct intel_dev
*i_dev
;
2356 struct imsm_dev
*dev
;
2358 /* check whole container
2360 for (i_dev
= super
->devlist
; i_dev
; i_dev
= i_dev
->next
) {
2362 if (is_gen_migration(dev
)) {
2363 /* No repair during any migration in container
2372 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
2374 struct intel_super
*super
= st
->sb
;
2375 struct migr_record
*migr_rec
= super
->migr_rec
;
2376 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
2377 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2378 struct imsm_map
*prev_map
= get_imsm_map(dev
, MAP_1
);
2379 struct imsm_map
*map_to_analyse
= map
;
2382 unsigned int component_size_alligment
;
2383 int map_disks
= info
->array
.raid_disks
;
2385 memset(info
, 0, sizeof(*info
));
2387 map_to_analyse
= prev_map
;
2389 dl
= super
->current_disk
;
2391 info
->container_member
= super
->current_vol
;
2392 info
->array
.raid_disks
= map
->num_members
;
2393 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
2394 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
2395 info
->array
.md_minor
= -1;
2396 info
->array
.ctime
= 0;
2397 info
->array
.utime
= 0;
2398 info
->array
.chunk_size
=
2399 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
2400 info
->array
.state
= !dev
->vol
.dirty
;
2401 info
->custom_array_size
= __le32_to_cpu(dev
->size_high
);
2402 info
->custom_array_size
<<= 32;
2403 info
->custom_array_size
|= __le32_to_cpu(dev
->size_low
);
2404 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
2406 if (is_gen_migration(dev
)) {
2407 info
->reshape_active
= 1;
2408 info
->new_level
= get_imsm_raid_level(map
);
2409 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
2410 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
2411 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
2412 if (info
->delta_disks
) {
2413 /* this needs to be applied to every array
2416 info
->reshape_active
= CONTAINER_RESHAPE
;
2418 /* We shape information that we give to md might have to be
2419 * modify to cope with md's requirement for reshaping arrays.
2420 * For example, when reshaping a RAID0, md requires it to be
2421 * presented as a degraded RAID4.
2422 * Also if a RAID0 is migrating to a RAID5 we need to specify
2423 * the array as already being RAID5, but the 'before' layout
2424 * is a RAID4-like layout.
2426 switch (info
->array
.level
) {
2428 switch(info
->new_level
) {
2430 /* conversion is happening as RAID4 */
2431 info
->array
.level
= 4;
2432 info
->array
.raid_disks
+= 1;
2435 /* conversion is happening as RAID5 */
2436 info
->array
.level
= 5;
2437 info
->array
.layout
= ALGORITHM_PARITY_N
;
2438 info
->delta_disks
-= 1;
2441 /* FIXME error message */
2442 info
->array
.level
= UnSet
;
2448 info
->new_level
= UnSet
;
2449 info
->new_layout
= UnSet
;
2450 info
->new_chunk
= info
->array
.chunk_size
;
2451 info
->delta_disks
= 0;
2455 info
->disk
.major
= dl
->major
;
2456 info
->disk
.minor
= dl
->minor
;
2457 info
->disk
.number
= dl
->index
;
2458 info
->disk
.raid_disk
= get_imsm_disk_slot(map_to_analyse
,
2462 info
->data_offset
= __le32_to_cpu(map_to_analyse
->pba_of_lba0
);
2463 info
->component_size
=
2464 __le32_to_cpu(map_to_analyse
->blocks_per_member
);
2466 /* check component size aligment
2468 component_size_alligment
=
2469 info
->component_size
% (info
->array
.chunk_size
/512);
2471 if (component_size_alligment
&&
2472 (info
->array
.level
!= 1) && (info
->array
.level
!= UnSet
)) {
2473 dprintf("imsm: reported component size alligned from %llu ",
2474 info
->component_size
);
2475 info
->component_size
-= component_size_alligment
;
2476 dprintf("to %llu (%i).\n",
2477 info
->component_size
, component_size_alligment
);
2480 memset(info
->uuid
, 0, sizeof(info
->uuid
));
2481 info
->recovery_start
= MaxSector
;
2483 info
->reshape_progress
= 0;
2484 info
->resync_start
= MaxSector
;
2485 if ((map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
2487 imsm_reshape_blocks_arrays_changes(super
) == 0) {
2488 info
->resync_start
= 0;
2490 if (dev
->vol
.migr_state
) {
2491 switch (migr_type(dev
)) {
2494 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2496 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
2498 info
->resync_start
= blocks_per_unit
* units
;
2501 case MIGR_GEN_MIGR
: {
2502 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2504 __u64 units
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
2505 unsigned long long array_blocks
;
2508 if (__le32_to_cpu(migr_rec
->ascending_migr
) &&
2510 (__le32_to_cpu(migr_rec
->num_migr_units
)-1)) &&
2511 (super
->migr_rec
->rec_status
==
2512 __cpu_to_le32(UNIT_SRC_IN_CP_AREA
)))
2515 info
->reshape_progress
= blocks_per_unit
* units
;
2517 dprintf("IMSM: General Migration checkpoint : %llu "
2518 "(%llu) -> read reshape progress : %llu\n",
2519 (unsigned long long)units
,
2520 (unsigned long long)blocks_per_unit
,
2521 info
->reshape_progress
);
2523 used_disks
= imsm_num_data_members(dev
, MAP_1
);
2524 if (used_disks
> 0) {
2525 array_blocks
= map
->blocks_per_member
*
2527 /* round array size down to closest MB
2529 info
->custom_array_size
= (array_blocks
2530 >> SECT_PER_MB_SHIFT
)
2531 << SECT_PER_MB_SHIFT
;
2535 /* we could emulate the checkpointing of
2536 * 'sync_action=check' migrations, but for now
2537 * we just immediately complete them
2540 /* this is handled by container_content_imsm() */
2541 case MIGR_STATE_CHANGE
:
2542 /* FIXME handle other migrations */
2544 /* we are not dirty, so... */
2545 info
->resync_start
= MaxSector
;
2549 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
2550 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
2552 info
->array
.major_version
= -1;
2553 info
->array
.minor_version
= -2;
2554 devname
= devnum2devname(st
->container_dev
);
2555 *info
->text_version
= '\0';
2557 sprintf(info
->text_version
, "/%s/%d", devname
, info
->container_member
);
2559 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
2560 uuid_from_super_imsm(st
, info
->uuid
);
2564 for (i
=0; i
<map_disks
; i
++) {
2566 if (i
< info
->array
.raid_disks
) {
2567 struct imsm_disk
*dsk
;
2568 j
= get_imsm_disk_idx(dev
, i
, MAP_X
);
2569 dsk
= get_imsm_disk(super
, j
);
2570 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
2577 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
2578 int failed
, int look_in_map
);
2580 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
2585 static void manage_second_map(struct intel_super
*super
, struct imsm_dev
*dev
)
2587 if (is_gen_migration(dev
)) {
2590 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
2592 failed
= imsm_count_failed(super
, dev
, MAP_1
);
2593 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_1
);
2594 if (map2
->map_state
!= map_state
) {
2595 map2
->map_state
= map_state
;
2596 super
->updates_pending
++;
2602 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
2606 for (d
= super
->missing
; d
; d
= d
->next
)
2607 if (d
->index
== index
)
2612 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
2614 struct intel_super
*super
= st
->sb
;
2615 struct imsm_disk
*disk
;
2616 int map_disks
= info
->array
.raid_disks
;
2617 int max_enough
= -1;
2619 struct imsm_super
*mpb
;
2621 if (super
->current_vol
>= 0) {
2622 getinfo_super_imsm_volume(st
, info
, map
);
2625 memset(info
, 0, sizeof(*info
));
2627 /* Set raid_disks to zero so that Assemble will always pull in valid
2630 info
->array
.raid_disks
= 0;
2631 info
->array
.level
= LEVEL_CONTAINER
;
2632 info
->array
.layout
= 0;
2633 info
->array
.md_minor
= -1;
2634 info
->array
.ctime
= 0; /* N/A for imsm */
2635 info
->array
.utime
= 0;
2636 info
->array
.chunk_size
= 0;
2638 info
->disk
.major
= 0;
2639 info
->disk
.minor
= 0;
2640 info
->disk
.raid_disk
= -1;
2641 info
->reshape_active
= 0;
2642 info
->array
.major_version
= -1;
2643 info
->array
.minor_version
= -2;
2644 strcpy(info
->text_version
, "imsm");
2645 info
->safe_mode_delay
= 0;
2646 info
->disk
.number
= -1;
2647 info
->disk
.state
= 0;
2649 info
->recovery_start
= MaxSector
;
2650 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
2652 /* do we have the all the insync disks that we expect? */
2653 mpb
= super
->anchor
;
2655 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
2656 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
2657 int failed
, enough
, j
, missing
= 0;
2658 struct imsm_map
*map
;
2661 failed
= imsm_count_failed(super
, dev
, MAP_0
);
2662 state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
2663 map
= get_imsm_map(dev
, MAP_0
);
2665 /* any newly missing disks?
2666 * (catches single-degraded vs double-degraded)
2668 for (j
= 0; j
< map
->num_members
; j
++) {
2669 __u32 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
2670 __u32 idx
= ord_to_idx(ord
);
2672 if (!(ord
& IMSM_ORD_REBUILD
) &&
2673 get_imsm_missing(super
, idx
)) {
2679 if (state
== IMSM_T_STATE_FAILED
)
2681 else if (state
== IMSM_T_STATE_DEGRADED
&&
2682 (state
!= map
->map_state
|| missing
))
2684 else /* we're normal, or already degraded */
2687 /* in the missing/failed disk case check to see
2688 * if at least one array is runnable
2690 max_enough
= max(max_enough
, enough
);
2692 dprintf("%s: enough: %d\n", __func__
, max_enough
);
2693 info
->container_enough
= max_enough
;
2696 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
2698 disk
= &super
->disks
->disk
;
2699 info
->data_offset
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
2700 info
->component_size
= reserved
;
2701 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
2702 /* we don't change info->disk.raid_disk here because
2703 * this state will be finalized in mdmon after we have
2704 * found the 'most fresh' version of the metadata
2706 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
2707 info
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
2710 /* only call uuid_from_super_imsm when this disk is part of a populated container,
2711 * ->compare_super may have updated the 'num_raid_devs' field for spares
2713 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
2714 uuid_from_super_imsm(st
, info
->uuid
);
2716 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
2718 /* I don't know how to compute 'map' on imsm, so use safe default */
2721 for (i
= 0; i
< map_disks
; i
++)
2727 /* allocates memory and fills disk in mdinfo structure
2728 * for each disk in array */
2729 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
2731 struct mdinfo
*mddev
= NULL
;
2732 struct intel_super
*super
= st
->sb
;
2733 struct imsm_disk
*disk
;
2736 if (!super
|| !super
->disks
)
2739 mddev
= malloc(sizeof(*mddev
));
2741 fprintf(stderr
, Name
": Failed to allocate memory.\n");
2744 memset(mddev
, 0, sizeof(*mddev
));
2748 tmp
= malloc(sizeof(*tmp
));
2750 fprintf(stderr
, Name
": Failed to allocate memory.\n");
2755 memset(tmp
, 0, sizeof(*tmp
));
2757 tmp
->next
= mddev
->devs
;
2759 tmp
->disk
.number
= count
++;
2760 tmp
->disk
.major
= dl
->major
;
2761 tmp
->disk
.minor
= dl
->minor
;
2762 tmp
->disk
.state
= is_configured(disk
) ?
2763 (1 << MD_DISK_ACTIVE
) : 0;
2764 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
2765 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
2766 tmp
->disk
.raid_disk
= -1;
2772 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
2773 char *update
, char *devname
, int verbose
,
2774 int uuid_set
, char *homehost
)
2776 /* For 'assemble' and 'force' we need to return non-zero if any
2777 * change was made. For others, the return value is ignored.
2778 * Update options are:
2779 * force-one : This device looks a bit old but needs to be included,
2780 * update age info appropriately.
2781 * assemble: clear any 'faulty' flag to allow this device to
2783 * force-array: Array is degraded but being forced, mark it clean
2784 * if that will be needed to assemble it.
2786 * newdev: not used ????
2787 * grow: Array has gained a new device - this is currently for
2789 * resync: mark as dirty so a resync will happen.
2790 * name: update the name - preserving the homehost
2791 * uuid: Change the uuid of the array to match watch is given
2793 * Following are not relevant for this imsm:
2794 * sparc2.2 : update from old dodgey metadata
2795 * super-minor: change the preferred_minor number
2796 * summaries: update redundant counters.
2797 * homehost: update the recorded homehost
2798 * _reshape_progress: record new reshape_progress position.
2801 struct intel_super
*super
= st
->sb
;
2802 struct imsm_super
*mpb
;
2804 /* we can only update container info */
2805 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
2808 mpb
= super
->anchor
;
2810 if (strcmp(update
, "uuid") == 0) {
2811 /* We take this to mean that the family_num should be updated.
2812 * However that is much smaller than the uuid so we cannot really
2813 * allow an explicit uuid to be given. And it is hard to reliably
2815 * So if !uuid_set we know the current uuid is random and just used
2816 * the first 'int' and copy it to the other 3 positions.
2817 * Otherwise we require the 4 'int's to be the same as would be the
2818 * case if we are using a random uuid. So an explicit uuid will be
2819 * accepted as long as all for ints are the same... which shouldn't hurt
2822 info
->uuid
[1] = info
->uuid
[2] = info
->uuid
[3] = info
->uuid
[0];
2825 if (info
->uuid
[0] != info
->uuid
[1] ||
2826 info
->uuid
[1] != info
->uuid
[2] ||
2827 info
->uuid
[2] != info
->uuid
[3])
2833 mpb
->orig_family_num
= info
->uuid
[0];
2834 } else if (strcmp(update
, "assemble") == 0)
2839 /* successful update? recompute checksum */
2841 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
2846 static size_t disks_to_mpb_size(int disks
)
2850 size
= sizeof(struct imsm_super
);
2851 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
2852 size
+= 2 * sizeof(struct imsm_dev
);
2853 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
2854 size
+= (4 - 2) * sizeof(struct imsm_map
);
2855 /* 4 possible disk_ord_tbl's */
2856 size
+= 4 * (disks
- 1) * sizeof(__u32
);
2861 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
)
2863 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
2866 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
2869 static void free_devlist(struct intel_super
*super
)
2871 struct intel_dev
*dv
;
2873 while (super
->devlist
) {
2874 dv
= super
->devlist
->next
;
2875 free(super
->devlist
->dev
);
2876 free(super
->devlist
);
2877 super
->devlist
= dv
;
2881 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
2883 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
2886 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
2890 * 0 same, or first was empty, and second was copied
2891 * 1 second had wrong number
2893 * 3 wrong other info
2895 struct intel_super
*first
= st
->sb
;
2896 struct intel_super
*sec
= tst
->sb
;
2903 /* in platform dependent environment test if the disks
2904 * use the same Intel hba
2906 if (!check_env("IMSM_NO_PLATFORM")) {
2907 if (!first
->hba
|| !sec
->hba
||
2908 (first
->hba
->type
!= sec
->hba
->type
)) {
2910 "HBAs of devices does not match %s != %s\n",
2911 first
->hba
? get_sys_dev_type(first
->hba
->type
) : NULL
,
2912 sec
->hba
? get_sys_dev_type(sec
->hba
->type
) : NULL
);
2917 /* if an anchor does not have num_raid_devs set then it is a free
2920 if (first
->anchor
->num_raid_devs
> 0 &&
2921 sec
->anchor
->num_raid_devs
> 0) {
2922 /* Determine if these disks might ever have been
2923 * related. Further disambiguation can only take place
2924 * in load_super_imsm_all
2926 __u32 first_family
= first
->anchor
->orig_family_num
;
2927 __u32 sec_family
= sec
->anchor
->orig_family_num
;
2929 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
2930 MAX_SIGNATURE_LENGTH
) != 0)
2933 if (first_family
== 0)
2934 first_family
= first
->anchor
->family_num
;
2935 if (sec_family
== 0)
2936 sec_family
= sec
->anchor
->family_num
;
2938 if (first_family
!= sec_family
)
2944 /* if 'first' is a spare promote it to a populated mpb with sec's
2947 if (first
->anchor
->num_raid_devs
== 0 &&
2948 sec
->anchor
->num_raid_devs
> 0) {
2950 struct intel_dev
*dv
;
2951 struct imsm_dev
*dev
;
2953 /* we need to copy raid device info from sec if an allocation
2954 * fails here we don't associate the spare
2956 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
2957 dv
= malloc(sizeof(*dv
));
2960 dev
= malloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
2967 dv
->next
= first
->devlist
;
2968 first
->devlist
= dv
;
2970 if (i
< sec
->anchor
->num_raid_devs
) {
2971 /* allocation failure */
2972 free_devlist(first
);
2973 fprintf(stderr
, "imsm: failed to associate spare\n");
2976 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
2977 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
2978 first
->anchor
->family_num
= sec
->anchor
->family_num
;
2979 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
2980 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
2981 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
2987 static void fd2devname(int fd
, char *name
)
2991 char dname
[PATH_MAX
];
2996 if (fstat(fd
, &st
) != 0)
2998 sprintf(path
, "/sys/dev/block/%d:%d",
2999 major(st
.st_rdev
), minor(st
.st_rdev
));
3001 rv
= readlink(path
, dname
, sizeof(dname
)-1);
3006 nm
= strrchr(dname
, '/');
3009 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
3013 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
3015 static int imsm_read_serial(int fd
, char *devname
,
3016 __u8 serial
[MAX_RAID_SERIAL_LEN
])
3018 unsigned char scsi_serial
[255];
3027 memset(scsi_serial
, 0, sizeof(scsi_serial
));
3029 rv
= scsi_get_serial(fd
, scsi_serial
, sizeof(scsi_serial
));
3031 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
3032 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3033 fd2devname(fd
, (char *) serial
);
3040 Name
": Failed to retrieve serial for %s\n",
3045 rsp_len
= scsi_serial
[3];
3049 Name
": Failed to retrieve serial for %s\n",
3053 rsp_buf
= (char *) &scsi_serial
[4];
3055 /* trim all whitespace and non-printable characters and convert
3058 for (i
= 0, dest
= rsp_buf
; i
< rsp_len
; i
++) {
3061 /* ':' is reserved for use in placeholder serial
3062 * numbers for missing disks
3070 len
= dest
- rsp_buf
;
3073 /* truncate leading characters */
3074 if (len
> MAX_RAID_SERIAL_LEN
) {
3075 dest
+= len
- MAX_RAID_SERIAL_LEN
;
3076 len
= MAX_RAID_SERIAL_LEN
;
3079 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3080 memcpy(serial
, dest
, len
);
3085 static int serialcmp(__u8
*s1
, __u8
*s2
)
3087 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
3090 static void serialcpy(__u8
*dest
, __u8
*src
)
3092 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
3095 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
3099 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3100 if (serialcmp(dl
->serial
, serial
) == 0)
3106 static struct imsm_disk
*
3107 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
3111 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3112 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3114 if (serialcmp(disk
->serial
, serial
) == 0) {
3125 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3127 struct imsm_disk
*disk
;
3132 __u8 serial
[MAX_RAID_SERIAL_LEN
];
3134 rv
= imsm_read_serial(fd
, devname
, serial
);
3139 dl
= calloc(1, sizeof(*dl
));
3143 Name
": failed to allocate disk buffer for %s\n",
3149 dl
->major
= major(stb
.st_rdev
);
3150 dl
->minor
= minor(stb
.st_rdev
);
3151 dl
->next
= super
->disks
;
3152 dl
->fd
= keep_fd
? fd
: -1;
3153 assert(super
->disks
== NULL
);
3155 serialcpy(dl
->serial
, serial
);
3158 fd2devname(fd
, name
);
3160 dl
->devname
= strdup(devname
);
3162 dl
->devname
= strdup(name
);
3164 /* look up this disk's index in the current anchor */
3165 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
3168 /* only set index on disks that are a member of a
3169 * populated contianer, i.e. one with raid_devs
3171 if (is_failed(&dl
->disk
))
3173 else if (is_spare(&dl
->disk
))
3181 /* When migrating map0 contains the 'destination' state while map1
3182 * contains the current state. When not migrating map0 contains the
3183 * current state. This routine assumes that map[0].map_state is set to
3184 * the current array state before being called.
3186 * Migration is indicated by one of the following states
3187 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
3188 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
3189 * map1state=unitialized)
3190 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
3192 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
3193 * map1state=degraded)
3194 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
3197 static void migrate(struct imsm_dev
*dev
, struct intel_super
*super
,
3198 __u8 to_state
, int migr_type
)
3200 struct imsm_map
*dest
;
3201 struct imsm_map
*src
= get_imsm_map(dev
, MAP_0
);
3203 dev
->vol
.migr_state
= 1;
3204 set_migr_type(dev
, migr_type
);
3205 dev
->vol
.curr_migr_unit
= 0;
3206 dest
= get_imsm_map(dev
, MAP_1
);
3208 /* duplicate and then set the target end state in map[0] */
3209 memcpy(dest
, src
, sizeof_imsm_map(src
));
3210 if ((migr_type
== MIGR_REBUILD
) ||
3211 (migr_type
== MIGR_GEN_MIGR
)) {
3215 for (i
= 0; i
< src
->num_members
; i
++) {
3216 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
3217 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
3221 if (migr_type
== MIGR_GEN_MIGR
)
3222 /* Clear migration record */
3223 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
3225 src
->map_state
= to_state
;
3228 static void end_migration(struct imsm_dev
*dev
, struct intel_super
*super
,
3231 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3232 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
== 0 ?
3236 /* merge any IMSM_ORD_REBUILD bits that were not successfully
3237 * completed in the last migration.
3239 * FIXME add support for raid-level-migration
3241 if ((map_state
!= map
->map_state
) && (is_gen_migration(dev
) == 0) &&
3242 (prev
->map_state
!= IMSM_T_STATE_UNINITIALIZED
)) {
3243 /* when final map state is other than expected
3244 * merge maps (not for migration)
3248 for (i
= 0; i
< prev
->num_members
; i
++)
3249 for (j
= 0; j
< map
->num_members
; j
++)
3250 /* during online capacity expansion
3251 * disks position can be changed
3252 * if takeover is used
3254 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
3255 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
3256 map
->disk_ord_tbl
[j
] |=
3257 prev
->disk_ord_tbl
[i
];
3260 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3261 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3264 dev
->vol
.migr_state
= 0;
3265 set_migr_type(dev
, 0);
3266 dev
->vol
.curr_migr_unit
= 0;
3267 map
->map_state
= map_state
;
3271 static int parse_raid_devices(struct intel_super
*super
)
3274 struct imsm_dev
*dev_new
;
3275 size_t len
, len_migr
;
3277 size_t space_needed
= 0;
3278 struct imsm_super
*mpb
= super
->anchor
;
3280 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3281 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3282 struct intel_dev
*dv
;
3284 len
= sizeof_imsm_dev(dev_iter
, 0);
3285 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
3287 space_needed
+= len_migr
- len
;
3289 dv
= malloc(sizeof(*dv
));
3292 if (max_len
< len_migr
)
3294 if (max_len
> len_migr
)
3295 space_needed
+= max_len
- len_migr
;
3296 dev_new
= malloc(max_len
);
3301 imsm_copy_dev(dev_new
, dev_iter
);
3304 dv
->next
= super
->devlist
;
3305 super
->devlist
= dv
;
3308 /* ensure that super->buf is large enough when all raid devices
3311 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
3314 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
, 512);
3315 if (posix_memalign(&buf
, 512, len
) != 0)
3318 memcpy(buf
, super
->buf
, super
->len
);
3319 memset(buf
+ super
->len
, 0, len
- super
->len
);
3328 /* retrieve a pointer to the bbm log which starts after all raid devices */
3329 struct bbm_log
*__get_imsm_bbm_log(struct imsm_super
*mpb
)
3333 if (__le32_to_cpu(mpb
->bbm_log_size
)) {
3335 ptr
+= mpb
->mpb_size
- __le32_to_cpu(mpb
->bbm_log_size
);
3341 /*******************************************************************************
3342 * Function: check_mpb_migr_compatibility
3343 * Description: Function checks for unsupported migration features:
3344 * - migration optimization area (pba_of_lba0)
3345 * - descending reshape (ascending_migr)
3347 * super : imsm metadata information
3349 * 0 : migration is compatible
3350 * -1 : migration is not compatible
3351 ******************************************************************************/
3352 int check_mpb_migr_compatibility(struct intel_super
*super
)
3354 struct imsm_map
*map0
, *map1
;
3355 struct migr_record
*migr_rec
= super
->migr_rec
;
3358 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3359 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3362 dev_iter
->vol
.migr_state
== 1 &&
3363 dev_iter
->vol
.migr_type
== MIGR_GEN_MIGR
) {
3364 /* This device is migrating */
3365 map0
= get_imsm_map(dev_iter
, MAP_0
);
3366 map1
= get_imsm_map(dev_iter
, MAP_1
);
3367 if (map0
->pba_of_lba0
!= map1
->pba_of_lba0
)
3368 /* migration optimization area was used */
3370 if (migr_rec
->ascending_migr
== 0
3371 && migr_rec
->dest_depth_per_unit
> 0)
3372 /* descending reshape not supported yet */
3379 static void __free_imsm(struct intel_super
*super
, int free_disks
);
3381 /* load_imsm_mpb - read matrix metadata
3382 * allocates super->mpb to be freed by free_imsm
3384 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
3386 unsigned long long dsize
;
3387 unsigned long long sectors
;
3389 struct imsm_super
*anchor
;
3392 get_dev_size(fd
, NULL
, &dsize
);
3396 Name
": %s: device to small for imsm\n",
3401 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0) {
3403 fprintf(stderr
, Name
3404 ": Cannot seek to anchor block on %s: %s\n",
3405 devname
, strerror(errno
));
3409 if (posix_memalign((void**)&anchor
, 512, 512) != 0) {
3412 Name
": Failed to allocate imsm anchor buffer"
3413 " on %s\n", devname
);
3416 if (read(fd
, anchor
, 512) != 512) {
3419 Name
": Cannot read anchor block on %s: %s\n",
3420 devname
, strerror(errno
));
3425 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
3428 Name
": no IMSM anchor on %s\n", devname
);
3433 __free_imsm(super
, 0);
3434 /* reload capability and hba */
3436 /* capability and hba must be updated with new super allocation */
3437 find_intel_hba_capability(fd
, super
, devname
);
3438 super
->len
= ROUND_UP(anchor
->mpb_size
, 512);
3439 if (posix_memalign(&super
->buf
, 512, super
->len
) != 0) {
3442 Name
": unable to allocate %zu byte mpb buffer\n",
3447 memcpy(super
->buf
, anchor
, 512);
3449 sectors
= mpb_sectors(anchor
) - 1;
3452 if (posix_memalign(&super
->migr_rec_buf
, 512, MIGR_REC_BUF_SIZE
) != 0) {
3453 fprintf(stderr
, Name
3454 ": %s could not allocate migr_rec buffer\n", __func__
);
3460 check_sum
= __gen_imsm_checksum(super
->anchor
);
3461 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3464 Name
": IMSM checksum %x != %x on %s\n",
3466 __le32_to_cpu(super
->anchor
->check_sum
),
3474 /* read the extended mpb */
3475 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0) {
3478 Name
": Cannot seek to extended mpb on %s: %s\n",
3479 devname
, strerror(errno
));
3483 if ((unsigned)read(fd
, super
->buf
+ 512, super
->len
- 512) != super
->len
- 512) {
3486 Name
": Cannot read extended mpb on %s: %s\n",
3487 devname
, strerror(errno
));
3491 check_sum
= __gen_imsm_checksum(super
->anchor
);
3492 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3495 Name
": IMSM checksum %x != %x on %s\n",
3496 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
3501 /* FIXME the BBM log is disk specific so we cannot use this global
3502 * buffer for all disks. Ok for now since we only look at the global
3503 * bbm_log_size parameter to gate assembly
3505 super
->bbm_log
= __get_imsm_bbm_log(super
->anchor
);
3510 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
);
3513 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3517 err
= load_imsm_mpb(fd
, super
, devname
);
3520 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
3523 err
= parse_raid_devices(super
);
3528 static void __free_imsm_disk(struct dl
*d
)
3540 static void free_imsm_disks(struct intel_super
*super
)
3544 while (super
->disks
) {
3546 super
->disks
= d
->next
;
3547 __free_imsm_disk(d
);
3549 while (super
->disk_mgmt_list
) {
3550 d
= super
->disk_mgmt_list
;
3551 super
->disk_mgmt_list
= d
->next
;
3552 __free_imsm_disk(d
);
3554 while (super
->missing
) {
3556 super
->missing
= d
->next
;
3557 __free_imsm_disk(d
);
3562 /* free all the pieces hanging off of a super pointer */
3563 static void __free_imsm(struct intel_super
*super
, int free_disks
)
3565 struct intel_hba
*elem
, *next
;
3571 /* unlink capability description */
3573 if (super
->migr_rec_buf
) {
3574 free(super
->migr_rec_buf
);
3575 super
->migr_rec_buf
= NULL
;
3578 free_imsm_disks(super
);
3579 free_devlist(super
);
3583 free((void *)elem
->path
);
3591 static void free_imsm(struct intel_super
*super
)
3593 __free_imsm(super
, 1);
3597 static void free_super_imsm(struct supertype
*st
)
3599 struct intel_super
*super
= st
->sb
;
3608 static struct intel_super
*alloc_super(void)
3610 struct intel_super
*super
= malloc(sizeof(*super
));
3613 memset(super
, 0, sizeof(*super
));
3614 super
->current_vol
= -1;
3615 super
->create_offset
= ~((__u32
) 0);
3621 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
3623 static int find_intel_hba_capability(int fd
, struct intel_super
*super
, char *devname
)
3625 struct sys_dev
*hba_name
;
3628 if ((fd
< 0) || check_env("IMSM_NO_PLATFORM")) {
3633 hba_name
= find_disk_attached_hba(fd
, NULL
);
3637 Name
": %s is not attached to Intel(R) RAID controller.\n",
3641 rv
= attach_hba_to_super(super
, hba_name
);
3644 struct intel_hba
*hba
= super
->hba
;
3646 fprintf(stderr
, Name
": %s is attached to Intel(R) %s RAID "
3647 "controller (%s),\n"
3648 " but the container is assigned to Intel(R) "
3649 "%s RAID controller (",
3652 hba_name
->pci_id
? : "Err!",
3653 get_sys_dev_type(hba_name
->type
));
3656 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
3658 fprintf(stderr
, ", ");
3662 fprintf(stderr
, ").\n"
3663 " Mixing devices attached to different controllers "
3664 "is not allowed.\n");
3666 free_sys_dev(&hba_name
);
3669 super
->orom
= find_imsm_capability(hba_name
->type
);
3670 free_sys_dev(&hba_name
);
3676 /* find_missing - helper routine for load_super_imsm_all that identifies
3677 * disks that have disappeared from the system. This routine relies on
3678 * the mpb being uptodate, which it is at load time.
3680 static int find_missing(struct intel_super
*super
)
3683 struct imsm_super
*mpb
= super
->anchor
;
3685 struct imsm_disk
*disk
;
3687 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3688 disk
= __get_imsm_disk(mpb
, i
);
3689 dl
= serial_to_dl(disk
->serial
, super
);
3693 dl
= malloc(sizeof(*dl
));
3699 dl
->devname
= strdup("missing");
3701 serialcpy(dl
->serial
, disk
->serial
);
3704 dl
->next
= super
->missing
;
3705 super
->missing
= dl
;
3712 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
3714 struct intel_disk
*idisk
= disk_list
;
3717 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
3719 idisk
= idisk
->next
;
3725 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
3726 struct intel_super
*super
,
3727 struct intel_disk
**disk_list
)
3729 struct imsm_disk
*d
= &super
->disks
->disk
;
3730 struct imsm_super
*mpb
= super
->anchor
;
3733 for (i
= 0; i
< tbl_size
; i
++) {
3734 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
3735 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
3737 if (tbl_mpb
->family_num
== mpb
->family_num
) {
3738 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
3739 dprintf("%s: mpb from %d:%d matches %d:%d\n",
3740 __func__
, super
->disks
->major
,
3741 super
->disks
->minor
,
3742 table
[i
]->disks
->major
,
3743 table
[i
]->disks
->minor
);
3747 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
3748 is_configured(d
) == is_configured(tbl_d
)) &&
3749 tbl_mpb
->generation_num
< mpb
->generation_num
) {
3750 /* current version of the mpb is a
3751 * better candidate than the one in
3752 * super_table, but copy over "cross
3753 * generational" status
3755 struct intel_disk
*idisk
;
3757 dprintf("%s: mpb from %d:%d replaces %d:%d\n",
3758 __func__
, super
->disks
->major
,
3759 super
->disks
->minor
,
3760 table
[i
]->disks
->major
,
3761 table
[i
]->disks
->minor
);
3763 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
3764 if (idisk
&& is_failed(&idisk
->disk
))
3765 tbl_d
->status
|= FAILED_DISK
;
3768 struct intel_disk
*idisk
;
3769 struct imsm_disk
*disk
;
3771 /* tbl_mpb is more up to date, but copy
3772 * over cross generational status before
3775 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
3776 if (disk
&& is_failed(disk
))
3777 d
->status
|= FAILED_DISK
;
3779 idisk
= disk_list_get(d
->serial
, *disk_list
);
3782 if (disk
&& is_configured(disk
))
3783 idisk
->disk
.status
|= CONFIGURED_DISK
;
3786 dprintf("%s: mpb from %d:%d prefer %d:%d\n",
3787 __func__
, super
->disks
->major
,
3788 super
->disks
->minor
,
3789 table
[i
]->disks
->major
,
3790 table
[i
]->disks
->minor
);
3798 table
[tbl_size
++] = super
;
3802 /* update/extend the merged list of imsm_disk records */
3803 for (j
= 0; j
< mpb
->num_disks
; j
++) {
3804 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
3805 struct intel_disk
*idisk
;
3807 idisk
= disk_list_get(disk
->serial
, *disk_list
);
3809 idisk
->disk
.status
|= disk
->status
;
3810 if (is_configured(&idisk
->disk
) ||
3811 is_failed(&idisk
->disk
))
3812 idisk
->disk
.status
&= ~(SPARE_DISK
);
3814 idisk
= calloc(1, sizeof(*idisk
));
3817 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
3818 idisk
->disk
= *disk
;
3819 idisk
->next
= *disk_list
;
3823 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
3830 static struct intel_super
*
3831 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
3834 struct imsm_super
*mpb
= super
->anchor
;
3838 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3839 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3840 struct intel_disk
*idisk
;
3842 idisk
= disk_list_get(disk
->serial
, disk_list
);
3844 if (idisk
->owner
== owner
||
3845 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
3848 dprintf("%s: '%.16s' owner %d != %d\n",
3849 __func__
, disk
->serial
, idisk
->owner
,
3852 dprintf("%s: unknown disk %x [%d]: %.16s\n",
3853 __func__
, __le32_to_cpu(mpb
->family_num
), i
,
3859 if (ok_count
== mpb
->num_disks
)
3864 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
3866 struct intel_super
*s
;
3868 for (s
= super_list
; s
; s
= s
->next
) {
3869 if (family_num
!= s
->anchor
->family_num
)
3871 fprintf(stderr
, "Conflict, offlining family %#x on '%s'\n",
3872 __le32_to_cpu(family_num
), s
->disks
->devname
);
3876 static struct intel_super
*
3877 imsm_thunderdome(struct intel_super
**super_list
, int len
)
3879 struct intel_super
*super_table
[len
];
3880 struct intel_disk
*disk_list
= NULL
;
3881 struct intel_super
*champion
, *spare
;
3882 struct intel_super
*s
, **del
;
3887 memset(super_table
, 0, sizeof(super_table
));
3888 for (s
= *super_list
; s
; s
= s
->next
)
3889 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
3891 for (i
= 0; i
< tbl_size
; i
++) {
3892 struct imsm_disk
*d
;
3893 struct intel_disk
*idisk
;
3894 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
3897 d
= &s
->disks
->disk
;
3899 /* 'd' must appear in merged disk list for its
3900 * configuration to be valid
3902 idisk
= disk_list_get(d
->serial
, disk_list
);
3903 if (idisk
&& idisk
->owner
== i
)
3904 s
= validate_members(s
, disk_list
, i
);
3909 dprintf("%s: marking family: %#x from %d:%d offline\n",
3910 __func__
, mpb
->family_num
,
3911 super_table
[i
]->disks
->major
,
3912 super_table
[i
]->disks
->minor
);
3916 /* This is where the mdadm implementation differs from the Windows
3917 * driver which has no strict concept of a container. We can only
3918 * assemble one family from a container, so when returning a prodigal
3919 * array member to this system the code will not be able to disambiguate
3920 * the container contents that should be assembled ("foreign" versus
3921 * "local"). It requires user intervention to set the orig_family_num
3922 * to a new value to establish a new container. The Windows driver in
3923 * this situation fixes up the volume name in place and manages the
3924 * foreign array as an independent entity.
3929 for (i
= 0; i
< tbl_size
; i
++) {
3930 struct intel_super
*tbl_ent
= super_table
[i
];
3936 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
3941 if (s
&& !is_spare
) {
3942 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
3944 } else if (!s
&& !is_spare
)
3957 fprintf(stderr
, "Chose family %#x on '%s', "
3958 "assemble conflicts to new container with '--update=uuid'\n",
3959 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
3961 /* collect all dl's onto 'champion', and update them to
3962 * champion's version of the status
3964 for (s
= *super_list
; s
; s
= s
->next
) {
3965 struct imsm_super
*mpb
= champion
->anchor
;
3966 struct dl
*dl
= s
->disks
;
3971 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3972 struct imsm_disk
*disk
;
3974 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
3977 /* only set index on disks that are a member of
3978 * a populated contianer, i.e. one with
3981 if (is_failed(&dl
->disk
))
3983 else if (is_spare(&dl
->disk
))
3989 if (i
>= mpb
->num_disks
) {
3990 struct intel_disk
*idisk
;
3992 idisk
= disk_list_get(dl
->serial
, disk_list
);
3993 if (idisk
&& is_spare(&idisk
->disk
) &&
3994 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
4002 dl
->next
= champion
->disks
;
4003 champion
->disks
= dl
;
4007 /* delete 'champion' from super_list */
4008 for (del
= super_list
; *del
; ) {
4009 if (*del
== champion
) {
4010 *del
= (*del
)->next
;
4013 del
= &(*del
)->next
;
4015 champion
->next
= NULL
;
4019 struct intel_disk
*idisk
= disk_list
;
4021 disk_list
= disk_list
->next
;
4030 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
);
4031 static int get_super_block(struct intel_super
**super_list
, int devnum
, char *devname
,
4032 int major
, int minor
, int keep_fd
);
4034 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4035 int *max
, int keep_fd
);
4038 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
4039 char *devname
, struct md_list
*devlist
,
4042 struct intel_super
*super_list
= NULL
;
4043 struct intel_super
*super
= NULL
;
4048 /* 'fd' is an opened container */
4049 err
= get_sra_super_block(fd
, &super_list
, devname
, &i
, keep_fd
);
4051 /* get super block from devlist devices */
4052 err
= get_devlist_super_block(devlist
, &super_list
, &i
, keep_fd
);
4055 /* all mpbs enter, maybe one leaves */
4056 super
= imsm_thunderdome(&super_list
, i
);
4062 if (find_missing(super
) != 0) {
4068 /* load migration record */
4069 err
= load_imsm_migr_rec(super
, NULL
);
4071 /* migration is in progress,
4072 * but migr_rec cannot be loaded,
4078 /* Check migration compatibility */
4079 if ((err
== 0) && (check_mpb_migr_compatibility(super
) != 0)) {
4080 fprintf(stderr
, Name
": Unsupported migration detected");
4082 fprintf(stderr
, " on %s\n", devname
);
4084 fprintf(stderr
, " (IMSM).\n");
4093 while (super_list
) {
4094 struct intel_super
*s
= super_list
;
4096 super_list
= super_list
->next
;
4106 st
->container_dev
= fd2devnum(fd
);
4108 st
->container_dev
= NoMdDev
;
4109 if (err
== 0 && st
->ss
== NULL
) {
4110 st
->ss
= &super_imsm
;
4111 st
->minor_version
= 0;
4112 st
->max_devs
= IMSM_MAX_DEVICES
;
4119 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4120 int *max
, int keep_fd
)
4122 struct md_list
*tmpdev
;
4126 for (i
= 0, tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
4127 if (tmpdev
->used
!= 1)
4129 if (tmpdev
->container
== 1) {
4131 int fd
= dev_open(tmpdev
->devname
, O_RDONLY
|O_EXCL
);
4133 fprintf(stderr
, Name
": cannot open device %s: %s\n",
4134 tmpdev
->devname
, strerror(errno
));
4138 err
= get_sra_super_block(fd
, super_list
,
4139 tmpdev
->devname
, &lmax
,
4148 int major
= major(tmpdev
->st_rdev
);
4149 int minor
= minor(tmpdev
->st_rdev
);
4150 err
= get_super_block(super_list
,
4167 static int get_super_block(struct intel_super
**super_list
, int devnum
, char *devname
,
4168 int major
, int minor
, int keep_fd
)
4170 struct intel_super
*s
= NULL
;
4183 sprintf(nm
, "%d:%d", major
, minor
);
4184 dfd
= dev_open(nm
, O_RDWR
);
4190 rv
= find_intel_hba_capability(dfd
, s
, devname
);
4191 /* no orom/efi or non-intel hba of the disk */
4197 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4199 /* retry the load if we might have raced against mdmon */
4200 if (err
== 3 && (devnum
!= -1) && mdmon_running(devnum
))
4201 for (retry
= 0; retry
< 3; retry
++) {
4203 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4209 s
->next
= *super_list
;
4217 if ((dfd
>= 0) && (!keep_fd
))
4224 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
)
4231 sra
= sysfs_read(fd
, 0, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
4235 if (sra
->array
.major_version
!= -1 ||
4236 sra
->array
.minor_version
!= -2 ||
4237 strcmp(sra
->text_version
, "imsm") != 0) {
4242 devnum
= fd2devnum(fd
);
4243 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
4244 if (get_super_block(super_list
, devnum
, devname
,
4245 sd
->disk
.major
, sd
->disk
.minor
, keep_fd
) != 0) {
4256 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
4258 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
, NULL
, 1);
4262 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
4264 struct intel_super
*super
;
4267 if (test_partition(fd
))
4268 /* IMSM not allowed on partitions */
4271 free_super_imsm(st
);
4273 super
= alloc_super();
4276 Name
": malloc of %zu failed.\n",
4280 /* Load hba and capabilities if they exist.
4281 * But do not preclude loading metadata in case capabilities or hba are
4282 * non-compliant and ignore_hw_compat is set.
4284 rv
= find_intel_hba_capability(fd
, super
, devname
);
4285 /* no orom/efi or non-intel hba of the disk */
4286 if ((rv
!= 0) && (st
->ignore_hw_compat
== 0)) {
4289 Name
": No OROM/EFI properties for %s\n", devname
);
4293 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
4298 Name
": Failed to load all information "
4299 "sections on %s\n", devname
);
4305 if (st
->ss
== NULL
) {
4306 st
->ss
= &super_imsm
;
4307 st
->minor_version
= 0;
4308 st
->max_devs
= IMSM_MAX_DEVICES
;
4311 /* load migration record */
4312 if (load_imsm_migr_rec(super
, NULL
) == 0) {
4313 /* Check for unsupported migration features */
4314 if (check_mpb_migr_compatibility(super
) != 0) {
4316 Name
": Unsupported migration detected");
4318 fprintf(stderr
, " on %s\n", devname
);
4320 fprintf(stderr
, " (IMSM).\n");
4328 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
4330 if (info
->level
== 1)
4332 return info
->chunk_size
>> 9;
4335 static __u32
info_to_num_data_stripes(mdu_array_info_t
*info
, int num_domains
)
4339 num_stripes
= (info
->size
* 2) / info_to_blocks_per_strip(info
);
4340 num_stripes
/= num_domains
;
4345 static __u32
info_to_blocks_per_member(mdu_array_info_t
*info
)
4347 if (info
->level
== 1)
4348 return info
->size
* 2;
4350 return (info
->size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
4353 static void imsm_update_version_info(struct intel_super
*super
)
4355 /* update the version and attributes */
4356 struct imsm_super
*mpb
= super
->anchor
;
4358 struct imsm_dev
*dev
;
4359 struct imsm_map
*map
;
4362 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4363 dev
= get_imsm_dev(super
, i
);
4364 map
= get_imsm_map(dev
, MAP_0
);
4365 if (__le32_to_cpu(dev
->size_high
) > 0)
4366 mpb
->attributes
|= MPB_ATTRIB_2TB
;
4368 /* FIXME detect when an array spans a port multiplier */
4370 mpb
->attributes
|= MPB_ATTRIB_PM
;
4373 if (mpb
->num_raid_devs
> 1 ||
4374 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
4375 version
= MPB_VERSION_ATTRIBS
;
4376 switch (get_imsm_raid_level(map
)) {
4377 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
4378 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
4379 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
4380 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
4383 if (map
->num_members
>= 5)
4384 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
4385 else if (dev
->status
== DEV_CLONE_N_GO
)
4386 version
= MPB_VERSION_CNG
;
4387 else if (get_imsm_raid_level(map
) == 5)
4388 version
= MPB_VERSION_RAID5
;
4389 else if (map
->num_members
>= 3)
4390 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
4391 else if (get_imsm_raid_level(map
) == 1)
4392 version
= MPB_VERSION_RAID1
;
4394 version
= MPB_VERSION_RAID0
;
4396 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
4400 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
4402 struct imsm_super
*mpb
= super
->anchor
;
4403 char *reason
= NULL
;
4406 if (strlen(name
) > MAX_RAID_SERIAL_LEN
)
4407 reason
= "must be 16 characters or less";
4409 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4410 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4412 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
4413 reason
= "already exists";
4418 if (reason
&& !quiet
)
4419 fprintf(stderr
, Name
": imsm volume name %s\n", reason
);
4424 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
4425 unsigned long long size
, char *name
,
4426 char *homehost
, int *uuid
)
4428 /* We are creating a volume inside a pre-existing container.
4429 * so st->sb is already set.
4431 struct intel_super
*super
= st
->sb
;
4432 struct imsm_super
*mpb
= super
->anchor
;
4433 struct intel_dev
*dv
;
4434 struct imsm_dev
*dev
;
4435 struct imsm_vol
*vol
;
4436 struct imsm_map
*map
;
4437 int idx
= mpb
->num_raid_devs
;
4439 unsigned long long array_blocks
;
4440 size_t size_old
, size_new
;
4441 __u32 num_data_stripes
;
4443 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
4444 fprintf(stderr
, Name
": This imsm-container already has the "
4445 "maximum of %d volumes\n", super
->orom
->vpa
);
4449 /* ensure the mpb is large enough for the new data */
4450 size_old
= __le32_to_cpu(mpb
->mpb_size
);
4451 size_new
= disks_to_mpb_size(info
->nr_disks
);
4452 if (size_new
> size_old
) {
4454 size_t size_round
= ROUND_UP(size_new
, 512);
4456 if (posix_memalign(&mpb_new
, 512, size_round
) != 0) {
4457 fprintf(stderr
, Name
": could not allocate new mpb\n");
4460 if (posix_memalign(&super
->migr_rec_buf
, 512,
4461 MIGR_REC_BUF_SIZE
) != 0) {
4462 fprintf(stderr
, Name
4463 ": %s could not allocate migr_rec buffer\n",
4470 memcpy(mpb_new
, mpb
, size_old
);
4473 super
->anchor
= mpb_new
;
4474 mpb
->mpb_size
= __cpu_to_le32(size_new
);
4475 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
4477 super
->current_vol
= idx
;
4479 /* handle 'failed_disks' by either:
4480 * a) create dummy disk entries in the table if this the first
4481 * volume in the array. We add them here as this is the only
4482 * opportunity to add them. add_to_super_imsm_volume()
4483 * handles the non-failed disks and continues incrementing
4485 * b) validate that 'failed_disks' matches the current number
4486 * of missing disks if the container is populated
4488 if (super
->current_vol
== 0) {
4490 for (i
= 0; i
< info
->failed_disks
; i
++) {
4491 struct imsm_disk
*disk
;
4494 disk
= __get_imsm_disk(mpb
, i
);
4495 disk
->status
= CONFIGURED_DISK
| FAILED_DISK
;
4496 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
4497 snprintf((char *) disk
->serial
, MAX_RAID_SERIAL_LEN
,
4500 find_missing(super
);
4505 for (d
= super
->missing
; d
; d
= d
->next
)
4507 if (info
->failed_disks
> missing
) {
4508 fprintf(stderr
, Name
": unable to add 'missing' disk to container\n");
4513 if (!check_name(super
, name
, 0))
4515 dv
= malloc(sizeof(*dv
));
4517 fprintf(stderr
, Name
": failed to allocate device list entry\n");
4520 dev
= calloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
4523 fprintf(stderr
, Name
": could not allocate raid device\n");
4527 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
4528 if (info
->level
== 1)
4529 array_blocks
= info_to_blocks_per_member(info
);
4531 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
4532 info
->layout
, info
->chunk_size
,
4534 /* round array size down to closest MB */
4535 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
4537 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
4538 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
4539 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
4541 vol
->migr_state
= 0;
4542 set_migr_type(dev
, MIGR_INIT
);
4543 vol
->dirty
= !info
->state
;
4544 vol
->curr_migr_unit
= 0;
4545 map
= get_imsm_map(dev
, MAP_0
);
4546 map
->pba_of_lba0
= __cpu_to_le32(super
->create_offset
);
4547 map
->blocks_per_member
= __cpu_to_le32(info_to_blocks_per_member(info
));
4548 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
4549 map
->failed_disk_num
= ~0;
4550 if (info
->level
> 0)
4551 map
->map_state
= IMSM_T_STATE_UNINITIALIZED
;
4553 map
->map_state
= info
->failed_disks
? IMSM_T_STATE_FAILED
:
4554 IMSM_T_STATE_NORMAL
;
4557 if (info
->level
== 1 && info
->raid_disks
> 2) {
4560 fprintf(stderr
, Name
": imsm does not support more than 2 disks"
4561 "in a raid1 volume\n");
4565 map
->raid_level
= info
->level
;
4566 if (info
->level
== 10) {
4567 map
->raid_level
= 1;
4568 map
->num_domains
= info
->raid_disks
/ 2;
4569 } else if (info
->level
== 1)
4570 map
->num_domains
= info
->raid_disks
;
4572 map
->num_domains
= 1;
4574 num_data_stripes
= info_to_num_data_stripes(info
, map
->num_domains
);
4575 map
->num_data_stripes
= __cpu_to_le32(num_data_stripes
);
4577 map
->num_members
= info
->raid_disks
;
4578 for (i
= 0; i
< map
->num_members
; i
++) {
4579 /* initialized in add_to_super */
4580 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
4582 mpb
->num_raid_devs
++;
4585 dv
->index
= super
->current_vol
;
4586 dv
->next
= super
->devlist
;
4587 super
->devlist
= dv
;
4589 imsm_update_version_info(super
);
4594 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
4595 unsigned long long size
, char *name
,
4596 char *homehost
, int *uuid
)
4598 /* This is primarily called by Create when creating a new array.
4599 * We will then get add_to_super called for each component, and then
4600 * write_init_super called to write it out to each device.
4601 * For IMSM, Create can create on fresh devices or on a pre-existing
4603 * To create on a pre-existing array a different method will be called.
4604 * This one is just for fresh drives.
4606 struct intel_super
*super
;
4607 struct imsm_super
*mpb
;
4612 return init_super_imsm_volume(st
, info
, size
, name
, homehost
, uuid
);
4615 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
4619 super
= alloc_super();
4620 if (super
&& posix_memalign(&super
->buf
, 512, mpb_size
) != 0) {
4625 fprintf(stderr
, Name
4626 ": %s could not allocate superblock\n", __func__
);
4629 if (posix_memalign(&super
->migr_rec_buf
, 512, MIGR_REC_BUF_SIZE
) != 0) {
4630 fprintf(stderr
, Name
4631 ": %s could not allocate migr_rec buffer\n", __func__
);
4636 memset(super
->buf
, 0, mpb_size
);
4638 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
4642 /* zeroing superblock */
4646 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
4648 version
= (char *) mpb
->sig
;
4649 strcpy(version
, MPB_SIGNATURE
);
4650 version
+= strlen(MPB_SIGNATURE
);
4651 strcpy(version
, MPB_VERSION_RAID0
);
4657 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
4658 int fd
, char *devname
)
4660 struct intel_super
*super
= st
->sb
;
4661 struct imsm_super
*mpb
= super
->anchor
;
4662 struct imsm_disk
*_disk
;
4663 struct imsm_dev
*dev
;
4664 struct imsm_map
*map
;
4668 dev
= get_imsm_dev(super
, super
->current_vol
);
4669 map
= get_imsm_map(dev
, MAP_0
);
4671 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
4672 fprintf(stderr
, Name
": %s: Cannot add spare devices to IMSM volume\n",
4678 /* we're doing autolayout so grab the pre-marked (in
4679 * validate_geometry) raid_disk
4681 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4682 if (dl
->raiddisk
== dk
->raid_disk
)
4685 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4686 if (dl
->major
== dk
->major
&&
4687 dl
->minor
== dk
->minor
)
4692 fprintf(stderr
, Name
": %s is not a member of the same container\n", devname
);
4696 /* add a pristine spare to the metadata */
4697 if (dl
->index
< 0) {
4698 dl
->index
= super
->anchor
->num_disks
;
4699 super
->anchor
->num_disks
++;
4701 /* Check the device has not already been added */
4702 slot
= get_imsm_disk_slot(map
, dl
->index
);
4704 (get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
) & IMSM_ORD_REBUILD
) == 0) {
4705 fprintf(stderr
, Name
": %s has been included in this array twice\n",
4709 set_imsm_ord_tbl_ent(map
, dk
->raid_disk
, dl
->index
);
4710 dl
->disk
.status
= CONFIGURED_DISK
;
4712 /* update size of 'missing' disks to be at least as large as the
4713 * largest acitve member (we only have dummy missing disks when
4714 * creating the first volume)
4716 if (super
->current_vol
== 0) {
4717 for (df
= super
->missing
; df
; df
= df
->next
) {
4718 if (dl
->disk
.total_blocks
> df
->disk
.total_blocks
)
4719 df
->disk
.total_blocks
= dl
->disk
.total_blocks
;
4720 _disk
= __get_imsm_disk(mpb
, df
->index
);
4725 /* refresh unset/failed slots to point to valid 'missing' entries */
4726 for (df
= super
->missing
; df
; df
= df
->next
)
4727 for (slot
= 0; slot
< mpb
->num_disks
; slot
++) {
4728 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
4730 if ((ord
& IMSM_ORD_REBUILD
) == 0)
4732 set_imsm_ord_tbl_ent(map
, slot
, df
->index
| IMSM_ORD_REBUILD
);
4733 if (is_gen_migration(dev
)) {
4734 struct imsm_map
*map2
= get_imsm_map(dev
,
4736 int slot2
= get_imsm_disk_slot(map2
, df
->index
);
4737 if ((slot2
< map2
->num_members
) &&
4739 __u32 ord2
= get_imsm_ord_tbl_ent(dev
,
4742 if ((unsigned)df
->index
==
4744 set_imsm_ord_tbl_ent(map2
,
4750 dprintf("set slot:%d to missing disk:%d\n", slot
, df
->index
);
4754 /* if we are creating the first raid device update the family number */
4755 if (super
->current_vol
== 0) {
4757 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
4759 _disk
= __get_imsm_disk(mpb
, dl
->index
);
4760 if (!_dev
|| !_disk
) {
4761 fprintf(stderr
, Name
": BUG mpb setup error\n");
4767 sum
+= __gen_imsm_checksum(mpb
);
4768 mpb
->family_num
= __cpu_to_le32(sum
);
4769 mpb
->orig_family_num
= mpb
->family_num
;
4771 super
->current_disk
= dl
;
4776 * Function marks disk as spare and restores disk serial
4777 * in case it was previously marked as failed by takeover operation
4779 * -1 : critical error
4780 * 0 : disk is marked as spare but serial is not set
4783 int mark_spare(struct dl
*disk
)
4785 __u8 serial
[MAX_RAID_SERIAL_LEN
];
4792 if (!imsm_read_serial(disk
->fd
, NULL
, serial
)) {
4793 /* Restore disk serial number, because takeover marks disk
4794 * as failed and adds to serial ':0' before it becomes
4797 serialcpy(disk
->serial
, serial
);
4798 serialcpy(disk
->disk
.serial
, serial
);
4801 disk
->disk
.status
= SPARE_DISK
;
4807 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
4808 int fd
, char *devname
)
4810 struct intel_super
*super
= st
->sb
;
4812 unsigned long long size
;
4817 /* If we are on an RAID enabled platform check that the disk is
4818 * attached to the raid controller.
4819 * We do not need to test disks attachment for container based additions,
4820 * they shall be already tested when container was created/assembled.
4822 rv
= find_intel_hba_capability(fd
, super
, devname
);
4823 /* no orom/efi or non-intel hba of the disk */
4825 dprintf("capability: %p fd: %d ret: %d\n",
4826 super
->orom
, fd
, rv
);
4830 if (super
->current_vol
>= 0)
4831 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
4834 dd
= malloc(sizeof(*dd
));
4837 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
4840 memset(dd
, 0, sizeof(*dd
));
4841 dd
->major
= major(stb
.st_rdev
);
4842 dd
->minor
= minor(stb
.st_rdev
);
4843 dd
->devname
= devname
? strdup(devname
) : NULL
;
4846 dd
->action
= DISK_ADD
;
4847 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
4850 Name
": failed to retrieve scsi serial, aborting\n");
4855 get_dev_size(fd
, NULL
, &size
);
4857 serialcpy(dd
->disk
.serial
, dd
->serial
);
4858 dd
->disk
.total_blocks
= __cpu_to_le32(size
);
4860 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
4861 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
4863 dd
->disk
.scsi_id
= __cpu_to_le32(0);
4865 if (st
->update_tail
) {
4866 dd
->next
= super
->disk_mgmt_list
;
4867 super
->disk_mgmt_list
= dd
;
4869 dd
->next
= super
->disks
;
4871 super
->updates_pending
++;
4878 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
4880 struct intel_super
*super
= st
->sb
;
4883 /* remove from super works only in mdmon - for communication
4884 * manager - monitor. Check if communication memory buffer
4887 if (!st
->update_tail
) {
4889 Name
": %s shall be used in mdmon context only"
4890 "(line %d).\n", __func__
, __LINE__
);
4893 dd
= malloc(sizeof(*dd
));
4896 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
4899 memset(dd
, 0, sizeof(*dd
));
4900 dd
->major
= dk
->major
;
4901 dd
->minor
= dk
->minor
;
4904 dd
->action
= DISK_REMOVE
;
4906 dd
->next
= super
->disk_mgmt_list
;
4907 super
->disk_mgmt_list
= dd
;
4913 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
4917 struct imsm_super anchor
;
4918 } spare_record
__attribute__ ((aligned(512)));
4920 /* spare records have their own family number and do not have any defined raid
4923 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
4925 struct imsm_super
*mpb
= super
->anchor
;
4926 struct imsm_super
*spare
= &spare_record
.anchor
;
4930 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
)),
4931 spare
->generation_num
= __cpu_to_le32(1UL),
4932 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
4933 spare
->num_disks
= 1,
4934 spare
->num_raid_devs
= 0,
4935 spare
->cache_size
= mpb
->cache_size
,
4936 spare
->pwr_cycle_count
= __cpu_to_le32(1),
4938 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
4939 MPB_SIGNATURE MPB_VERSION_RAID0
);
4941 for (d
= super
->disks
; d
; d
= d
->next
) {
4945 spare
->disk
[0] = d
->disk
;
4946 sum
= __gen_imsm_checksum(spare
);
4947 spare
->family_num
= __cpu_to_le32(sum
);
4948 spare
->orig_family_num
= 0;
4949 sum
= __gen_imsm_checksum(spare
);
4950 spare
->check_sum
= __cpu_to_le32(sum
);
4952 if (store_imsm_mpb(d
->fd
, spare
)) {
4953 fprintf(stderr
, "%s: failed for device %d:%d %s\n",
4954 __func__
, d
->major
, d
->minor
, strerror(errno
));
4966 static int write_super_imsm(struct supertype
*st
, int doclose
)
4968 struct intel_super
*super
= st
->sb
;
4969 struct imsm_super
*mpb
= super
->anchor
;
4975 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
4977 int clear_migration_record
= 1;
4979 /* 'generation' is incremented everytime the metadata is written */
4980 generation
= __le32_to_cpu(mpb
->generation_num
);
4982 mpb
->generation_num
= __cpu_to_le32(generation
);
4984 /* fix up cases where previous mdadm releases failed to set
4987 if (mpb
->orig_family_num
== 0)
4988 mpb
->orig_family_num
= mpb
->family_num
;
4990 for (d
= super
->disks
; d
; d
= d
->next
) {
4994 mpb
->disk
[d
->index
] = d
->disk
;
4998 for (d
= super
->missing
; d
; d
= d
->next
) {
4999 mpb
->disk
[d
->index
] = d
->disk
;
5002 mpb
->num_disks
= num_disks
;
5003 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
5005 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5006 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
5007 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
5009 imsm_copy_dev(dev
, dev2
);
5010 mpb_size
+= sizeof_imsm_dev(dev
, 0);
5012 if (is_gen_migration(dev2
))
5013 clear_migration_record
= 0;
5015 mpb_size
+= __le32_to_cpu(mpb
->bbm_log_size
);
5016 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5018 /* recalculate checksum */
5019 sum
= __gen_imsm_checksum(mpb
);
5020 mpb
->check_sum
= __cpu_to_le32(sum
);
5022 if (clear_migration_record
)
5023 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SIZE
);
5025 /* write the mpb for disks that compose raid devices */
5026 for (d
= super
->disks
; d
; d
= d
->next
) {
5027 if (d
->index
< 0 || is_failed(&d
->disk
))
5029 if (store_imsm_mpb(d
->fd
, mpb
))
5030 fprintf(stderr
, "%s: failed for device %d:%d (fd: %d)%s\n",
5031 __func__
, d
->major
, d
->minor
, d
->fd
, strerror(errno
));
5033 if (clear_migration_record
) {
5034 unsigned long long dsize
;
5036 get_dev_size(d
->fd
, NULL
, &dsize
);
5037 if (lseek64(d
->fd
, dsize
- 512, SEEK_SET
) >= 0) {
5038 if (write(d
->fd
, super
->migr_rec_buf
,
5039 MIGR_REC_BUF_SIZE
) != MIGR_REC_BUF_SIZE
)
5040 perror("Write migr_rec failed");
5050 return write_super_imsm_spares(super
, doclose
);
5056 static int create_array(struct supertype
*st
, int dev_idx
)
5059 struct imsm_update_create_array
*u
;
5060 struct intel_super
*super
= st
->sb
;
5061 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
5062 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
5063 struct disk_info
*inf
;
5064 struct imsm_disk
*disk
;
5067 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
5068 sizeof(*inf
) * map
->num_members
;
5071 fprintf(stderr
, "%s: failed to allocate update buffer\n",
5076 u
->type
= update_create_array
;
5077 u
->dev_idx
= dev_idx
;
5078 imsm_copy_dev(&u
->dev
, dev
);
5079 inf
= get_disk_info(u
);
5080 for (i
= 0; i
< map
->num_members
; i
++) {
5081 int idx
= get_imsm_disk_idx(dev
, i
, MAP_X
);
5083 disk
= get_imsm_disk(super
, idx
);
5084 serialcpy(inf
[i
].serial
, disk
->serial
);
5086 append_metadata_update(st
, u
, len
);
5091 static int mgmt_disk(struct supertype
*st
)
5093 struct intel_super
*super
= st
->sb
;
5095 struct imsm_update_add_remove_disk
*u
;
5097 if (!super
->disk_mgmt_list
)
5103 fprintf(stderr
, "%s: failed to allocate update buffer\n",
5108 u
->type
= update_add_remove_disk
;
5109 append_metadata_update(st
, u
, len
);
5114 static int write_init_super_imsm(struct supertype
*st
)
5116 struct intel_super
*super
= st
->sb
;
5117 int current_vol
= super
->current_vol
;
5119 /* we are done with current_vol reset it to point st at the container */
5120 super
->current_vol
= -1;
5122 if (st
->update_tail
) {
5123 /* queue the recently created array / added disk
5124 * as a metadata update */
5127 /* determine if we are creating a volume or adding a disk */
5128 if (current_vol
< 0) {
5129 /* in the mgmt (add/remove) disk case we are running
5130 * in mdmon context, so don't close fd's
5132 return mgmt_disk(st
);
5134 rv
= create_array(st
, current_vol
);
5139 for (d
= super
->disks
; d
; d
= d
->next
)
5140 Kill(d
->devname
, NULL
, 0, 1, 1);
5141 return write_super_imsm(st
, 1);
5146 static int store_super_imsm(struct supertype
*st
, int fd
)
5148 struct intel_super
*super
= st
->sb
;
5149 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
5155 return store_imsm_mpb(fd
, mpb
);
5161 static int imsm_bbm_log_size(struct imsm_super
*mpb
)
5163 return __le32_to_cpu(mpb
->bbm_log_size
);
5167 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
5168 int layout
, int raiddisks
, int chunk
,
5169 unsigned long long size
, char *dev
,
5170 unsigned long long *freesize
,
5174 unsigned long long ldsize
;
5175 struct intel_super
*super
=NULL
;
5178 if (level
!= LEVEL_CONTAINER
)
5183 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
5186 fprintf(stderr
, Name
": imsm: Cannot open %s: %s\n",
5187 dev
, strerror(errno
));
5190 if (!get_dev_size(fd
, dev
, &ldsize
)) {
5195 /* capabilities retrieve could be possible
5196 * note that there is no fd for the disks in array.
5198 super
= alloc_super();
5201 Name
": malloc of %zu failed.\n",
5207 rv
= find_intel_hba_capability(fd
, super
, verbose
? dev
: NULL
);
5211 fd2devname(fd
, str
);
5212 dprintf("validate_geometry_imsm_container: fd: %d %s orom: %p rv: %d raiddisk: %d\n",
5213 fd
, str
, super
->orom
, rv
, raiddisks
);
5215 /* no orom/efi or non-intel hba of the disk */
5221 if (super
->orom
&& raiddisks
> super
->orom
->tds
) {
5223 fprintf(stderr
, Name
": %d exceeds maximum number of"
5224 " platform supported disks: %d\n",
5225 raiddisks
, super
->orom
->tds
);
5231 *freesize
= avail_size_imsm(st
, ldsize
>> 9);
5237 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
5239 const unsigned long long base_start
= e
[*idx
].start
;
5240 unsigned long long end
= base_start
+ e
[*idx
].size
;
5243 if (base_start
== end
)
5247 for (i
= *idx
; i
< num_extents
; i
++) {
5248 /* extend overlapping extents */
5249 if (e
[i
].start
>= base_start
&&
5250 e
[i
].start
<= end
) {
5253 if (e
[i
].start
+ e
[i
].size
> end
)
5254 end
= e
[i
].start
+ e
[i
].size
;
5255 } else if (e
[i
].start
> end
) {
5261 return end
- base_start
;
5264 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
5266 /* build a composite disk with all known extents and generate a new
5267 * 'maxsize' given the "all disks in an array must share a common start
5268 * offset" constraint
5270 struct extent
*e
= calloc(sum_extents
, sizeof(*e
));
5274 unsigned long long pos
;
5275 unsigned long long start
= 0;
5276 unsigned long long maxsize
;
5277 unsigned long reserve
;
5282 /* coalesce and sort all extents. also, check to see if we need to
5283 * reserve space between member arrays
5286 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5289 for (i
= 0; i
< dl
->extent_cnt
; i
++)
5292 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
5297 while (i
< sum_extents
) {
5298 e
[j
].start
= e
[i
].start
;
5299 e
[j
].size
= find_size(e
, &i
, sum_extents
);
5301 if (e
[j
-1].size
== 0)
5310 unsigned long long esize
;
5312 esize
= e
[i
].start
- pos
;
5313 if (esize
>= maxsize
) {
5318 pos
= e
[i
].start
+ e
[i
].size
;
5320 } while (e
[i
-1].size
);
5326 /* FIXME assumes volume at offset 0 is the first volume in a
5329 if (start_extent
> 0)
5330 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
5334 if (maxsize
< reserve
)
5337 super
->create_offset
= ~((__u32
) 0);
5338 if (start
+ reserve
> super
->create_offset
)
5339 return 0; /* start overflows create_offset */
5340 super
->create_offset
= start
+ reserve
;
5342 return maxsize
- reserve
;
5345 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
5347 if (level
< 0 || level
== 6 || level
== 4)
5350 /* if we have an orom prevent invalid raid levels */
5353 case 0: return imsm_orom_has_raid0(orom
);
5356 return imsm_orom_has_raid1e(orom
);
5357 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
5358 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
5359 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
5362 return 1; /* not on an Intel RAID platform so anything goes */
5369 active_arrays_by_format(char *name
, char* hba
, struct md_list
**devlist
,
5370 int dpa
, int verbose
)
5372 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
5373 struct mdstat_ent
*memb
= NULL
;
5376 struct md_list
*dv
= NULL
;
5379 for (memb
= mdstat
; memb
; memb
= memb
->next
) {
5380 if (memb
->metadata_version
&&
5381 (strncmp(memb
->metadata_version
, "external:", 9) == 0) &&
5382 (strcmp(&memb
->metadata_version
[9], name
) == 0) &&
5383 !is_subarray(memb
->metadata_version
+9) &&
5385 struct dev_member
*dev
= memb
->members
;
5387 while(dev
&& (fd
< 0)) {
5388 char *path
= malloc(strlen(dev
->name
) + strlen("/dev/") + 1);
5390 num
= sprintf(path
, "%s%s", "/dev/", dev
->name
);
5392 fd
= open(path
, O_RDONLY
, 0);
5393 if ((num
<= 0) || (fd
< 0)) {
5394 pr_vrb(": Cannot open %s: %s\n",
5395 dev
->name
, strerror(errno
));
5402 if ((fd
>= 0) && disk_attached_to_hba(fd
, hba
)) {
5403 struct mdstat_ent
*vol
;
5404 for (vol
= mdstat
; vol
; vol
= vol
->next
) {
5405 if ((vol
->active
> 0) &&
5406 vol
->metadata_version
&&
5407 is_container_member(vol
, memb
->dev
)) {
5412 if (*devlist
&& (found
< dpa
)) {
5413 dv
= calloc(1, sizeof(*dv
));
5415 fprintf(stderr
, Name
": calloc failed\n");
5417 dv
->devname
= malloc(strlen(memb
->dev
) + strlen("/dev/") + 1);
5418 if (dv
->devname
!= NULL
) {
5419 sprintf(dv
->devname
, "%s%s", "/dev/", memb
->dev
);
5422 dv
->next
= *devlist
;
5433 free_mdstat(mdstat
);
5438 static struct md_list
*
5439 get_loop_devices(void)
5442 struct md_list
*devlist
= NULL
;
5443 struct md_list
*dv
= NULL
;
5445 for(i
= 0; i
< 12; i
++) {
5446 dv
= calloc(1, sizeof(*dv
));
5448 fprintf(stderr
, Name
": calloc failed\n");
5451 dv
->devname
= malloc(40);
5452 if (dv
->devname
== NULL
) {
5453 fprintf(stderr
, Name
": malloc failed\n");
5457 sprintf(dv
->devname
, "/dev/loop%d", i
);
5465 static struct md_list
*
5466 get_devices(const char *hba_path
)
5468 struct md_list
*devlist
= NULL
;
5469 struct md_list
*dv
= NULL
;
5475 devlist
= get_loop_devices();
5478 /* scroll through /sys/dev/block looking for devices attached to
5481 dir
= opendir("/sys/dev/block");
5482 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
5487 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
5489 path
= devt_to_devpath(makedev(major
, minor
));
5492 if (!path_attached_to_hba(path
, hba_path
)) {
5499 fd
= dev_open(ent
->d_name
, O_RDONLY
);
5501 fd2devname(fd
, buf
);
5504 fprintf(stderr
, Name
": cannot open device: %s\n",
5510 dv
= calloc(1, sizeof(*dv
));
5512 fprintf(stderr
, Name
": malloc failed\n");
5516 dv
->devname
= strdup(buf
);
5517 if (dv
->devname
== NULL
) {
5518 fprintf(stderr
, Name
": malloc failed\n");
5529 devlist
= devlist
->next
;
5538 count_volumes_list(struct md_list
*devlist
, char *homehost
,
5539 int verbose
, int *found
)
5541 struct md_list
*tmpdev
;
5543 struct supertype
*st
= NULL
;
5545 /* first walk the list of devices to find a consistent set
5546 * that match the criterea, if that is possible.
5547 * We flag the ones we like with 'used'.
5550 st
= match_metadata_desc_imsm("imsm");
5552 pr_vrb(": cannot allocate memory for imsm supertype\n");
5556 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5557 char *devname
= tmpdev
->devname
;
5559 struct supertype
*tst
;
5561 if (tmpdev
->used
> 1)
5563 tst
= dup_super(st
);
5565 pr_vrb(": cannot allocate memory for imsm supertype\n");
5568 tmpdev
->container
= 0;
5569 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
5571 dprintf(": cannot open device %s: %s\n",
5572 devname
, strerror(errno
));
5574 } else if (fstat(dfd
, &stb
)< 0) {
5576 dprintf(": fstat failed for %s: %s\n",
5577 devname
, strerror(errno
));
5579 } else if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
5580 dprintf(": %s is not a block device.\n",
5583 } else if (must_be_container(dfd
)) {
5584 struct supertype
*cst
;
5585 cst
= super_by_fd(dfd
, NULL
);
5587 dprintf(": cannot recognize container type %s\n",
5590 } else if (tst
->ss
!= st
->ss
) {
5591 dprintf(": non-imsm container - ignore it: %s\n",
5594 } else if (!tst
->ss
->load_container
||
5595 tst
->ss
->load_container(tst
, dfd
, NULL
))
5598 tmpdev
->container
= 1;
5601 cst
->ss
->free_super(cst
);
5603 tmpdev
->st_rdev
= stb
.st_rdev
;
5604 if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
5605 dprintf(": no RAID superblock on %s\n",
5608 } else if (tst
->ss
->compare_super
== NULL
) {
5609 dprintf(": Cannot assemble %s metadata on %s\n",
5610 tst
->ss
->name
, devname
);
5616 if (tmpdev
->used
== 2 || tmpdev
->used
== 4) {
5617 /* Ignore unrecognised devices during auto-assembly */
5622 tst
->ss
->getinfo_super(tst
, &info
, NULL
);
5624 if (st
->minor_version
== -1)
5625 st
->minor_version
= tst
->minor_version
;
5627 if (memcmp(info
.uuid
, uuid_zero
,
5628 sizeof(int[4])) == 0) {
5629 /* this is a floating spare. It cannot define
5630 * an array unless there are no more arrays of
5631 * this type to be found. It can be included
5632 * in an array of this type though.
5638 if (st
->ss
!= tst
->ss
||
5639 st
->minor_version
!= tst
->minor_version
||
5640 st
->ss
->compare_super(st
, tst
) != 0) {
5641 /* Some mismatch. If exactly one array matches this host,
5642 * we can resolve on that one.
5643 * Or, if we are auto assembling, we just ignore the second
5646 dprintf(": superblock on %s doesn't match others - assembly aborted\n",
5652 dprintf("found: devname: %s\n", devname
);
5656 tst
->ss
->free_super(tst
);
5660 if ((err
= load_super_imsm_all(st
, -1, &st
->sb
, NULL
, devlist
, 0)) == 0) {
5661 struct mdinfo
*iter
, *head
= st
->ss
->container_content(st
, NULL
);
5662 for (iter
= head
; iter
; iter
= iter
->next
) {
5663 dprintf("content->text_version: %s vol\n",
5664 iter
->text_version
);
5665 if (iter
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
5666 /* do not assemble arrays with unsupported
5668 dprintf(": Cannot activate member %s.\n",
5669 iter
->text_version
);
5676 dprintf(" no valid super block on device list: err: %d %p\n",
5680 dprintf(" no more devices to examin\n");
5683 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5684 if ((tmpdev
->used
== 1) && (tmpdev
->found
)) {
5686 if (count
< tmpdev
->found
)
5689 count
-= tmpdev
->found
;
5692 if (tmpdev
->used
== 1)
5697 st
->ss
->free_super(st
);
5703 count_volumes(char *hba
, int dpa
, int verbose
)
5705 struct md_list
*devlist
= NULL
;
5709 devlist
= get_devices(hba
);
5710 /* if no intel devices return zero volumes */
5711 if (devlist
== NULL
)
5714 count
= active_arrays_by_format("imsm", hba
, &devlist
, dpa
, verbose
);
5715 dprintf(" path: %s active arrays: %d\n", hba
, count
);
5716 if (devlist
== NULL
)
5720 count
+= count_volumes_list(devlist
,
5724 dprintf("found %d count: %d\n", found
, count
);
5727 dprintf("path: %s total number of volumes: %d\n", hba
, count
);
5730 struct md_list
*dv
= devlist
;
5731 devlist
= devlist
->next
;
5738 static int imsm_default_chunk(const struct imsm_orom
*orom
)
5740 /* up to 512 if the plaform supports it, otherwise the platform max.
5741 * 128 if no platform detected
5743 int fs
= max(7, orom
? fls(orom
->sss
) : 0);
5745 return min(512, (1 << fs
));
5749 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
5750 int raiddisks
, int *chunk
, int verbose
)
5752 /* check/set platform and metadata limits/defaults */
5753 if (super
->orom
&& raiddisks
> super
->orom
->dpa
) {
5754 pr_vrb(": platform supports a maximum of %d disks per array\n",
5759 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
5760 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
5761 pr_vrb(": platform does not support raid%d with %d disk%s\n",
5762 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
5766 if (chunk
&& (*chunk
== 0 || *chunk
== UnSet
))
5767 *chunk
= imsm_default_chunk(super
->orom
);
5769 if (super
->orom
&& chunk
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
5770 pr_vrb(": platform does not support a chunk size of: "
5775 if (layout
!= imsm_level_to_layout(level
)) {
5777 pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
5778 else if (level
== 10)
5779 pr_vrb(": imsm raid 10 only supports the n2 layout\n");
5781 pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
5788 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
5789 * FIX ME add ahci details
5791 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
5792 int layout
, int raiddisks
, int *chunk
,
5793 unsigned long long size
, char *dev
,
5794 unsigned long long *freesize
,
5798 struct intel_super
*super
= st
->sb
;
5799 struct imsm_super
*mpb
;
5801 unsigned long long pos
= 0;
5802 unsigned long long maxsize
;
5806 /* We must have the container info already read in. */
5810 mpb
= super
->anchor
;
5812 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, verbose
)) {
5813 fprintf(stderr
, Name
": RAID gemetry validation failed. "
5814 "Cannot proceed with the action(s).\n");
5818 /* General test: make sure there is space for
5819 * 'raiddisks' device extents of size 'size' at a given
5822 unsigned long long minsize
= size
;
5823 unsigned long long start_offset
= MaxSector
;
5826 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
5827 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5832 e
= get_extents(super
, dl
);
5835 unsigned long long esize
;
5836 esize
= e
[i
].start
- pos
;
5837 if (esize
>= minsize
)
5839 if (found
&& start_offset
== MaxSector
) {
5842 } else if (found
&& pos
!= start_offset
) {
5846 pos
= e
[i
].start
+ e
[i
].size
;
5848 } while (e
[i
-1].size
);
5853 if (dcnt
< raiddisks
) {
5855 fprintf(stderr
, Name
": imsm: Not enough "
5856 "devices with space for this array "
5864 /* This device must be a member of the set */
5865 if (stat(dev
, &stb
) < 0)
5867 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
5869 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5870 if (dl
->major
== (int)major(stb
.st_rdev
) &&
5871 dl
->minor
== (int)minor(stb
.st_rdev
))
5876 fprintf(stderr
, Name
": %s is not in the "
5877 "same imsm set\n", dev
);
5879 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
5880 /* If a volume is present then the current creation attempt
5881 * cannot incorporate new spares because the orom may not
5882 * understand this configuration (all member disks must be
5883 * members of each array in the container).
5885 fprintf(stderr
, Name
": %s is a spare and a volume"
5886 " is already defined for this container\n", dev
);
5887 fprintf(stderr
, Name
": The option-rom requires all member"
5888 " disks to be a member of all volumes\n");
5890 } else if (super
->orom
&& mpb
->num_raid_devs
> 0 &&
5891 mpb
->num_disks
!= raiddisks
) {
5892 fprintf(stderr
, Name
": The option-rom requires all member"
5893 " disks to be a member of all volumes\n");
5897 /* retrieve the largest free space block */
5898 e
= get_extents(super
, dl
);
5903 unsigned long long esize
;
5905 esize
= e
[i
].start
- pos
;
5906 if (esize
>= maxsize
)
5908 pos
= e
[i
].start
+ e
[i
].size
;
5910 } while (e
[i
-1].size
);
5915 fprintf(stderr
, Name
": unable to determine free space for: %s\n",
5919 if (maxsize
< size
) {
5921 fprintf(stderr
, Name
": %s not enough space (%llu < %llu)\n",
5922 dev
, maxsize
, size
);
5926 /* count total number of extents for merge */
5928 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5930 i
+= dl
->extent_cnt
;
5932 maxsize
= merge_extents(super
, i
);
5934 if (!check_env("IMSM_NO_PLATFORM") &&
5935 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
5936 fprintf(stderr
, Name
": attempting to create a second "
5937 "volume with size less then remaining space. "
5942 if (maxsize
< size
|| maxsize
== 0) {
5945 fprintf(stderr
, Name
": no free space"
5946 " left on device. Aborting...\n");
5948 fprintf(stderr
, Name
": not enough space"
5949 " to create volume of given size"
5950 " (%llu < %llu). Aborting...\n",
5956 *freesize
= maxsize
;
5959 int count
= count_volumes(super
->hba
->path
,
5960 super
->orom
->dpa
, verbose
);
5961 if (super
->orom
->vphba
<= count
) {
5962 pr_vrb(": platform does not support more then %d raid volumes.\n",
5963 super
->orom
->vphba
);
5970 static int reserve_space(struct supertype
*st
, int raiddisks
,
5971 unsigned long long size
, int chunk
,
5972 unsigned long long *freesize
)
5974 struct intel_super
*super
= st
->sb
;
5975 struct imsm_super
*mpb
= super
->anchor
;
5980 unsigned long long maxsize
;
5981 unsigned long long minsize
;
5985 /* find the largest common start free region of the possible disks */
5989 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5995 /* don't activate new spares if we are orom constrained
5996 * and there is already a volume active in the container
5998 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
6001 e
= get_extents(super
, dl
);
6004 for (i
= 1; e
[i
-1].size
; i
++)
6012 maxsize
= merge_extents(super
, extent_cnt
);
6016 minsize
= chunk
* 2;
6018 if (cnt
< raiddisks
||
6019 (super
->orom
&& used
&& used
!= raiddisks
) ||
6020 maxsize
< minsize
||
6022 fprintf(stderr
, Name
": not enough devices with space to create array.\n");
6023 return 0; /* No enough free spaces large enough */
6035 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6037 dl
->raiddisk
= cnt
++;
6044 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
6045 int raiddisks
, int *chunk
, unsigned long long size
,
6046 char *dev
, unsigned long long *freesize
,
6054 * if given unused devices create a container
6055 * if given given devices in a container create a member volume
6057 if (level
== LEVEL_CONTAINER
) {
6058 /* Must be a fresh device to add to a container */
6059 return validate_geometry_imsm_container(st
, level
, layout
,
6061 chunk
?*chunk
:0, size
,
6068 struct intel_super
*super
= st
->sb
;
6069 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
6073 /* we are being asked to automatically layout a
6074 * new volume based on the current contents of
6075 * the container. If the the parameters can be
6076 * satisfied reserve_space will record the disks,
6077 * start offset, and size of the volume to be
6078 * created. add_to_super and getinfo_super
6079 * detect when autolayout is in progress.
6081 /* assuming that freesize is always given when array is
6083 if (super
->orom
&& freesize
) {
6085 count
= count_volumes(super
->hba
->path
,
6086 super
->orom
->dpa
, verbose
);
6087 if (super
->orom
->vphba
<= count
) {
6088 pr_vrb(": platform does not support more"
6089 "then %d raid volumes.\n",
6090 super
->orom
->vphba
);
6095 return reserve_space(st
, raiddisks
, size
,
6096 chunk
?*chunk
:0, freesize
);
6101 /* creating in a given container */
6102 return validate_geometry_imsm_volume(st
, level
, layout
,
6103 raiddisks
, chunk
, size
,
6104 dev
, freesize
, verbose
);
6107 /* This device needs to be a device in an 'imsm' container */
6108 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
6112 Name
": Cannot create this array on device %s\n",
6117 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
6119 fprintf(stderr
, Name
": Cannot open %s: %s\n",
6120 dev
, strerror(errno
));
6123 /* Well, it is in use by someone, maybe an 'imsm' container. */
6124 cfd
= open_container(fd
);
6128 fprintf(stderr
, Name
": Cannot use %s: It is busy\n",
6132 sra
= sysfs_read(cfd
, 0, GET_VERSION
);
6133 if (sra
&& sra
->array
.major_version
== -1 &&
6134 strcmp(sra
->text_version
, "imsm") == 0)
6138 /* This is a member of a imsm container. Load the container
6139 * and try to create a volume
6141 struct intel_super
*super
;
6143 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
, NULL
, 1) == 0) {
6145 st
->container_dev
= fd2devnum(cfd
);
6147 return validate_geometry_imsm_volume(st
, level
, layout
,
6156 fprintf(stderr
, Name
": failed container membership check\n");
6162 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
6164 struct intel_super
*super
= st
->sb
;
6166 if (level
&& *level
== UnSet
)
6167 *level
= LEVEL_CONTAINER
;
6169 if (level
&& layout
&& *layout
== UnSet
)
6170 *layout
= imsm_level_to_layout(*level
);
6172 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0))
6173 *chunk
= imsm_default_chunk(super
->orom
);
6176 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
6178 static int kill_subarray_imsm(struct supertype
*st
)
6180 /* remove the subarray currently referenced by ->current_vol */
6182 struct intel_dev
**dp
;
6183 struct intel_super
*super
= st
->sb
;
6184 __u8 current_vol
= super
->current_vol
;
6185 struct imsm_super
*mpb
= super
->anchor
;
6187 if (super
->current_vol
< 0)
6189 super
->current_vol
= -1; /* invalidate subarray cursor */
6191 /* block deletions that would change the uuid of active subarrays
6193 * FIXME when immutable ids are available, but note that we'll
6194 * also need to fixup the invalidated/active subarray indexes in
6197 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6200 if (i
< current_vol
)
6202 sprintf(subarray
, "%u", i
);
6203 if (is_subarray_active(subarray
, st
->devname
)) {
6205 Name
": deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
6212 if (st
->update_tail
) {
6213 struct imsm_update_kill_array
*u
= malloc(sizeof(*u
));
6217 u
->type
= update_kill_array
;
6218 u
->dev_idx
= current_vol
;
6219 append_metadata_update(st
, u
, sizeof(*u
));
6224 for (dp
= &super
->devlist
; *dp
;)
6225 if ((*dp
)->index
== current_vol
) {
6228 handle_missing(super
, (*dp
)->dev
);
6229 if ((*dp
)->index
> current_vol
)
6234 /* no more raid devices, all active components are now spares,
6235 * but of course failed are still failed
6237 if (--mpb
->num_raid_devs
== 0) {
6240 for (d
= super
->disks
; d
; d
= d
->next
)
6245 super
->updates_pending
++;
6250 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
6251 char *update
, struct mddev_ident
*ident
)
6253 /* update the subarray currently referenced by ->current_vol */
6254 struct intel_super
*super
= st
->sb
;
6255 struct imsm_super
*mpb
= super
->anchor
;
6257 if (strcmp(update
, "name") == 0) {
6258 char *name
= ident
->name
;
6262 if (is_subarray_active(subarray
, st
->devname
)) {
6264 Name
": Unable to update name of active subarray\n");
6268 if (!check_name(super
, name
, 0))
6271 vol
= strtoul(subarray
, &ep
, 10);
6272 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
6275 if (st
->update_tail
) {
6276 struct imsm_update_rename_array
*u
= malloc(sizeof(*u
));
6280 u
->type
= update_rename_array
;
6282 snprintf((char *) u
->name
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6283 append_metadata_update(st
, u
, sizeof(*u
));
6285 struct imsm_dev
*dev
;
6288 dev
= get_imsm_dev(super
, vol
);
6289 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6290 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6291 dev
= get_imsm_dev(super
, i
);
6292 handle_missing(super
, dev
);
6294 super
->updates_pending
++;
6301 #endif /* MDASSEMBLE */
6303 static int is_gen_migration(struct imsm_dev
*dev
)
6308 if (!dev
->vol
.migr_state
)
6311 if (migr_type(dev
) == MIGR_GEN_MIGR
)
6317 static int is_rebuilding(struct imsm_dev
*dev
)
6319 struct imsm_map
*migr_map
;
6321 if (!dev
->vol
.migr_state
)
6324 if (migr_type(dev
) != MIGR_REBUILD
)
6327 migr_map
= get_imsm_map(dev
, MAP_1
);
6329 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
6336 static int is_initializing(struct imsm_dev
*dev
)
6338 struct imsm_map
*migr_map
;
6340 if (!dev
->vol
.migr_state
)
6343 if (migr_type(dev
) != MIGR_INIT
)
6346 migr_map
= get_imsm_map(dev
, MAP_1
);
6348 if (migr_map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
6355 static void update_recovery_start(struct intel_super
*super
,
6356 struct imsm_dev
*dev
,
6357 struct mdinfo
*array
)
6359 struct mdinfo
*rebuild
= NULL
;
6363 if (!is_rebuilding(dev
))
6366 /* Find the rebuild target, but punt on the dual rebuild case */
6367 for (d
= array
->devs
; d
; d
= d
->next
)
6368 if (d
->recovery_start
== 0) {
6375 /* (?) none of the disks are marked with
6376 * IMSM_ORD_REBUILD, so assume they are missing and the
6377 * disk_ord_tbl was not correctly updated
6379 dprintf("%s: failed to locate out-of-sync disk\n", __func__
);
6383 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
6384 rebuild
->recovery_start
= units
* blocks_per_migr_unit(super
, dev
);
6388 static int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
);
6391 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
6393 /* Given a container loaded by load_super_imsm_all,
6394 * extract information about all the arrays into
6396 * If 'subarray' is given, just extract info about that array.
6398 * For each imsm_dev create an mdinfo, fill it in,
6399 * then look for matching devices in super->disks
6400 * and create appropriate device mdinfo.
6402 struct intel_super
*super
= st
->sb
;
6403 struct imsm_super
*mpb
= super
->anchor
;
6404 struct mdinfo
*rest
= NULL
;
6408 int spare_disks
= 0;
6410 /* do not assemble arrays when not all attributes are supported */
6411 if (imsm_check_attributes(mpb
->attributes
) == 0) {
6413 fprintf(stderr
, Name
": Unsupported attributes in IMSM metadata."
6414 "Arrays activation is blocked.\n");
6417 /* check for bad blocks */
6418 if (imsm_bbm_log_size(super
->anchor
)) {
6419 fprintf(stderr
, Name
": BBM log found in IMSM metadata."
6420 "Arrays activation is blocked.\n");
6425 /* count spare devices, not used in maps
6427 for (d
= super
->disks
; d
; d
= d
->next
)
6431 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6432 struct imsm_dev
*dev
;
6433 struct imsm_map
*map
;
6434 struct imsm_map
*map2
;
6435 struct mdinfo
*this;
6443 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
6446 dev
= get_imsm_dev(super
, i
);
6447 map
= get_imsm_map(dev
, MAP_0
);
6448 map2
= get_imsm_map(dev
, MAP_1
);
6450 /* do not publish arrays that are in the middle of an
6451 * unsupported migration
6453 if (dev
->vol
.migr_state
&&
6454 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
6455 fprintf(stderr
, Name
": cannot assemble volume '%.16s':"
6456 " unsupported migration in progress\n",
6460 /* do not publish arrays that are not support by controller's
6464 this = malloc(sizeof(*this));
6466 fprintf(stderr
, Name
": failed to allocate %zu bytes\n",
6471 super
->current_vol
= i
;
6472 getinfo_super_imsm_volume(st
, this, NULL
);
6475 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
6476 /* mdadm does not support all metadata features- set the bit in all arrays state */
6477 if (!validate_geometry_imsm_orom(super
,
6478 get_imsm_raid_level(map
), /* RAID level */
6479 imsm_level_to_layout(get_imsm_raid_level(map
)),
6480 map
->num_members
, /* raid disks */
6483 fprintf(stderr
, Name
": IMSM RAID geometry validation"
6484 " failed. Array %s activation is blocked.\n",
6486 this->array
.state
|=
6487 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
6488 (1<<MD_SB_BLOCK_VOLUME
);
6492 /* if array has bad blocks, set suitable bit in all arrays state */
6494 this->array
.state
|=
6495 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
6496 (1<<MD_SB_BLOCK_VOLUME
);
6498 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
6499 unsigned long long recovery_start
;
6500 struct mdinfo
*info_d
;
6507 idx
= get_imsm_disk_idx(dev
, slot
, MAP_0
);
6508 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
6509 for (d
= super
->disks
; d
; d
= d
->next
)
6510 if (d
->index
== idx
)
6513 recovery_start
= MaxSector
;
6516 if (d
&& is_failed(&d
->disk
))
6518 if (ord
& IMSM_ORD_REBUILD
)
6522 * if we skip some disks the array will be assmebled degraded;
6523 * reset resync start to avoid a dirty-degraded
6524 * situation when performing the intial sync
6526 * FIXME handle dirty degraded
6528 if ((skip
|| recovery_start
== 0) && !dev
->vol
.dirty
)
6529 this->resync_start
= MaxSector
;
6533 info_d
= calloc(1, sizeof(*info_d
));
6535 fprintf(stderr
, Name
": failed to allocate disk"
6536 " for volume %.16s\n", dev
->volume
);
6537 info_d
= this->devs
;
6539 struct mdinfo
*d
= info_d
->next
;
6548 info_d
->next
= this->devs
;
6549 this->devs
= info_d
;
6551 info_d
->disk
.number
= d
->index
;
6552 info_d
->disk
.major
= d
->major
;
6553 info_d
->disk
.minor
= d
->minor
;
6554 info_d
->disk
.raid_disk
= slot
;
6555 info_d
->recovery_start
= recovery_start
;
6557 if (slot
< map2
->num_members
)
6558 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
6560 this->array
.spare_disks
++;
6562 if (slot
< map
->num_members
)
6563 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
6565 this->array
.spare_disks
++;
6567 if (info_d
->recovery_start
== MaxSector
)
6568 this->array
.working_disks
++;
6570 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
6571 info_d
->data_offset
= __le32_to_cpu(map
->pba_of_lba0
);
6572 info_d
->component_size
= __le32_to_cpu(map
->blocks_per_member
);
6574 /* now that the disk list is up-to-date fixup recovery_start */
6575 update_recovery_start(super
, dev
, this);
6576 this->array
.spare_disks
+= spare_disks
;
6579 /* check for reshape */
6580 if (this->reshape_active
== 1)
6581 recover_backup_imsm(st
, this);
6590 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
6591 int failed
, int look_in_map
)
6593 struct imsm_map
*map
;
6595 map
= get_imsm_map(dev
, look_in_map
);
6598 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
6599 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
6601 switch (get_imsm_raid_level(map
)) {
6603 return IMSM_T_STATE_FAILED
;
6606 if (failed
< map
->num_members
)
6607 return IMSM_T_STATE_DEGRADED
;
6609 return IMSM_T_STATE_FAILED
;
6614 * check to see if any mirrors have failed, otherwise we
6615 * are degraded. Even numbered slots are mirrored on
6619 /* gcc -Os complains that this is unused */
6620 int insync
= insync
;
6622 for (i
= 0; i
< map
->num_members
; i
++) {
6623 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_X
);
6624 int idx
= ord_to_idx(ord
);
6625 struct imsm_disk
*disk
;
6627 /* reset the potential in-sync count on even-numbered
6628 * slots. num_copies is always 2 for imsm raid10
6633 disk
= get_imsm_disk(super
, idx
);
6634 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
6637 /* no in-sync disks left in this mirror the
6641 return IMSM_T_STATE_FAILED
;
6644 return IMSM_T_STATE_DEGRADED
;
6648 return IMSM_T_STATE_DEGRADED
;
6650 return IMSM_T_STATE_FAILED
;
6656 return map
->map_state
;
6659 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
6664 struct imsm_disk
*disk
;
6665 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6666 struct imsm_map
*prev
= get_imsm_map(dev
, MAP_1
);
6667 struct imsm_map
*map_for_loop
;
6672 /* at the beginning of migration we set IMSM_ORD_REBUILD on
6673 * disks that are being rebuilt. New failures are recorded to
6674 * map[0]. So we look through all the disks we started with and
6675 * see if any failures are still present, or if any new ones
6679 if (prev
&& (map
->num_members
< prev
->num_members
))
6680 map_for_loop
= prev
;
6682 for (i
= 0; i
< map_for_loop
->num_members
; i
++) {
6684 /* when MAP_X is passed both maps failures are counted
6687 ((look_in_map
== MAP_1
) || (look_in_map
== MAP_X
)) &&
6688 (i
< prev
->num_members
)) {
6689 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
6690 idx_1
= ord_to_idx(ord
);
6692 disk
= get_imsm_disk(super
, idx_1
);
6693 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
6696 if (((look_in_map
== MAP_0
) || (look_in_map
== MAP_X
)) &&
6697 (i
< map
->num_members
)) {
6698 ord
= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
6699 idx
= ord_to_idx(ord
);
6702 disk
= get_imsm_disk(super
, idx
);
6703 if (!disk
|| is_failed(disk
) ||
6704 ord
& IMSM_ORD_REBUILD
)
6714 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
6717 struct intel_super
*super
= c
->sb
;
6718 struct imsm_super
*mpb
= super
->anchor
;
6720 if (atoi(inst
) >= mpb
->num_raid_devs
) {
6721 fprintf(stderr
, "%s: subarry index %d, out of range\n",
6722 __func__
, atoi(inst
));
6726 dprintf("imsm: open_new %s\n", inst
);
6727 a
->info
.container_member
= atoi(inst
);
6731 static int is_resyncing(struct imsm_dev
*dev
)
6733 struct imsm_map
*migr_map
;
6735 if (!dev
->vol
.migr_state
)
6738 if (migr_type(dev
) == MIGR_INIT
||
6739 migr_type(dev
) == MIGR_REPAIR
)
6742 if (migr_type(dev
) == MIGR_GEN_MIGR
)
6745 migr_map
= get_imsm_map(dev
, MAP_1
);
6747 if ((migr_map
->map_state
== IMSM_T_STATE_NORMAL
) &&
6748 (dev
->vol
.migr_type
!= MIGR_GEN_MIGR
))
6754 /* return true if we recorded new information */
6755 static int mark_failure(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
6759 struct imsm_map
*map
;
6760 char buf
[MAX_RAID_SERIAL_LEN
+3];
6761 unsigned int len
, shift
= 0;
6763 /* new failures are always set in map[0] */
6764 map
= get_imsm_map(dev
, MAP_0
);
6766 slot
= get_imsm_disk_slot(map
, idx
);
6770 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
6771 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
6774 memcpy(buf
, disk
->serial
, MAX_RAID_SERIAL_LEN
);
6775 buf
[MAX_RAID_SERIAL_LEN
] = '\000';
6777 if ((len
= strlen(buf
)) >= MAX_RAID_SERIAL_LEN
)
6778 shift
= len
- MAX_RAID_SERIAL_LEN
+ 1;
6779 strncpy((char *)disk
->serial
, &buf
[shift
], MAX_RAID_SERIAL_LEN
);
6781 disk
->status
|= FAILED_DISK
;
6782 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
6783 /* mark failures in second map if second map exists and this disk
6785 * This is valid for migration, initialization and rebuild
6787 if (dev
->vol
.migr_state
) {
6788 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
6789 int slot2
= get_imsm_disk_slot(map2
, idx
);
6791 if ((slot2
< map2
->num_members
) &&
6793 set_imsm_ord_tbl_ent(map2
, slot2
,
6794 idx
| IMSM_ORD_REBUILD
);
6796 if (map
->failed_disk_num
== 0xff)
6797 map
->failed_disk_num
= slot
;
6801 static void mark_missing(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
6803 mark_failure(dev
, disk
, idx
);
6805 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
6808 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
6809 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
6812 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
6816 if (!super
->missing
)
6819 dprintf("imsm: mark missing\n");
6820 /* end process for initialization and rebuild only
6822 if (is_gen_migration(dev
) == 0) {
6826 failed
= imsm_count_failed(super
, dev
, MAP_0
);
6827 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
6829 end_migration(dev
, super
, map_state
);
6831 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
6832 mark_missing(dev
, &dl
->disk
, dl
->index
);
6833 super
->updates_pending
++;
6836 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
)
6838 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
6839 unsigned long long array_blocks
;
6840 struct imsm_map
*map
;
6842 if (used_disks
== 0) {
6843 /* when problems occures
6844 * return current array_blocks value
6846 array_blocks
= __le32_to_cpu(dev
->size_high
);
6847 array_blocks
= array_blocks
<< 32;
6848 array_blocks
+= __le32_to_cpu(dev
->size_low
);
6850 return array_blocks
;
6853 /* set array size in metadata
6855 map
= get_imsm_map(dev
, MAP_0
);
6856 array_blocks
= map
->blocks_per_member
* used_disks
;
6858 /* round array size down to closest MB
6860 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
6861 dev
->size_low
= __cpu_to_le32((__u32
)array_blocks
);
6862 dev
->size_high
= __cpu_to_le32((__u32
)(array_blocks
>> 32));
6864 return array_blocks
;
6867 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
6869 static void imsm_progress_container_reshape(struct intel_super
*super
)
6871 /* if no device has a migr_state, but some device has a
6872 * different number of members than the previous device, start
6873 * changing the number of devices in this device to match
6876 struct imsm_super
*mpb
= super
->anchor
;
6877 int prev_disks
= -1;
6881 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6882 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
6883 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6884 struct imsm_map
*map2
;
6885 int prev_num_members
;
6887 if (dev
->vol
.migr_state
)
6890 if (prev_disks
== -1)
6891 prev_disks
= map
->num_members
;
6892 if (prev_disks
== map
->num_members
)
6895 /* OK, this array needs to enter reshape mode.
6896 * i.e it needs a migr_state
6899 copy_map_size
= sizeof_imsm_map(map
);
6900 prev_num_members
= map
->num_members
;
6901 map
->num_members
= prev_disks
;
6902 dev
->vol
.migr_state
= 1;
6903 dev
->vol
.curr_migr_unit
= 0;
6904 set_migr_type(dev
, MIGR_GEN_MIGR
);
6905 for (i
= prev_num_members
;
6906 i
< map
->num_members
; i
++)
6907 set_imsm_ord_tbl_ent(map
, i
, i
);
6908 map2
= get_imsm_map(dev
, MAP_1
);
6909 /* Copy the current map */
6910 memcpy(map2
, map
, copy_map_size
);
6911 map2
->num_members
= prev_num_members
;
6913 imsm_set_array_size(dev
);
6914 super
->updates_pending
++;
6918 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
6919 * states are handled in imsm_set_disk() with one exception, when a
6920 * resync is stopped due to a new failure this routine will set the
6921 * 'degraded' state for the array.
6923 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
6925 int inst
= a
->info
.container_member
;
6926 struct intel_super
*super
= a
->container
->sb
;
6927 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
6928 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6929 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
6930 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
6931 __u32 blocks_per_unit
;
6933 if (dev
->vol
.migr_state
&&
6934 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
6935 /* array state change is blocked due to reshape action
6937 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
6938 * - finish the reshape (if last_checkpoint is big and action != reshape)
6939 * - update curr_migr_unit
6941 if (a
->curr_action
== reshape
) {
6942 /* still reshaping, maybe update curr_migr_unit */
6943 goto mark_checkpoint
;
6945 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
6946 /* for some reason we aborted the reshape.
6948 * disable automatic metadata rollback
6949 * user action is required to recover process
6952 struct imsm_map
*map2
=
6953 get_imsm_map(dev
, MAP_1
);
6954 dev
->vol
.migr_state
= 0;
6955 set_migr_type(dev
, 0);
6956 dev
->vol
.curr_migr_unit
= 0;
6958 sizeof_imsm_map(map2
));
6959 super
->updates_pending
++;
6962 if (a
->last_checkpoint
>= a
->info
.component_size
) {
6963 unsigned long long array_blocks
;
6967 used_disks
= imsm_num_data_members(dev
, MAP_0
);
6968 if (used_disks
> 0) {
6970 map
->blocks_per_member
*
6972 /* round array size down to closest MB
6974 array_blocks
= (array_blocks
6975 >> SECT_PER_MB_SHIFT
)
6976 << SECT_PER_MB_SHIFT
;
6977 a
->info
.custom_array_size
= array_blocks
;
6978 /* encourage manager to update array
6982 a
->check_reshape
= 1;
6984 /* finalize online capacity expansion/reshape */
6985 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
6987 mdi
->disk
.raid_disk
,
6990 imsm_progress_container_reshape(super
);
6995 /* before we activate this array handle any missing disks */
6996 if (consistent
== 2)
6997 handle_missing(super
, dev
);
6999 if (consistent
== 2 &&
7000 (!is_resync_complete(&a
->info
) ||
7001 map_state
!= IMSM_T_STATE_NORMAL
||
7002 dev
->vol
.migr_state
))
7005 if (is_resync_complete(&a
->info
)) {
7006 /* complete intialization / resync,
7007 * recovery and interrupted recovery is completed in
7010 if (is_resyncing(dev
)) {
7011 dprintf("imsm: mark resync done\n");
7012 end_migration(dev
, super
, map_state
);
7013 super
->updates_pending
++;
7014 a
->last_checkpoint
= 0;
7016 } else if ((!is_resyncing(dev
) && !failed
) &&
7017 (imsm_reshape_blocks_arrays_changes(super
) == 0)) {
7018 /* mark the start of the init process if nothing is failed */
7019 dprintf("imsm: mark resync start\n");
7020 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
7021 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
7023 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
7024 super
->updates_pending
++;
7028 /* skip checkpointing for general migration,
7029 * it is controlled in mdadm
7031 if (is_gen_migration(dev
))
7032 goto skip_mark_checkpoint
;
7034 /* check if we can update curr_migr_unit from resync_start, recovery_start */
7035 blocks_per_unit
= blocks_per_migr_unit(super
, dev
);
7036 if (blocks_per_unit
) {
7040 units
= a
->last_checkpoint
/ blocks_per_unit
;
7043 /* check that we did not overflow 32-bits, and that
7044 * curr_migr_unit needs updating
7046 if (units32
== units
&&
7048 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
7049 dprintf("imsm: mark checkpoint (%u)\n", units32
);
7050 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
7051 super
->updates_pending
++;
7055 skip_mark_checkpoint
:
7056 /* mark dirty / clean */
7057 if (dev
->vol
.dirty
!= !consistent
) {
7058 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
7063 super
->updates_pending
++;
7069 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
7071 int inst
= a
->info
.container_member
;
7072 struct intel_super
*super
= a
->container
->sb
;
7073 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7074 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7075 struct imsm_disk
*disk
;
7080 if (n
> map
->num_members
)
7081 fprintf(stderr
, "imsm: set_disk %d out of range 0..%d\n",
7082 n
, map
->num_members
- 1);
7087 dprintf("imsm: set_disk %d:%x\n", n
, state
);
7089 ord
= get_imsm_ord_tbl_ent(dev
, n
, MAP_0
);
7090 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
7092 /* check for new failures */
7093 if (state
& DS_FAULTY
) {
7094 if (mark_failure(dev
, disk
, ord_to_idx(ord
)))
7095 super
->updates_pending
++;
7098 /* check if in_sync */
7099 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
7100 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
7102 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
7103 super
->updates_pending
++;
7106 failed
= imsm_count_failed(super
, dev
, MAP_0
);
7107 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7109 /* check if recovery complete, newly degraded, or failed */
7110 dprintf("imsm: Detected transition to state ");
7111 switch (map_state
) {
7112 case IMSM_T_STATE_NORMAL
: /* transition to normal state */
7113 dprintf("normal: ");
7114 if (is_rebuilding(dev
)) {
7115 dprintf("while rebuilding");
7116 end_migration(dev
, super
, map_state
);
7117 map
= get_imsm_map(dev
, MAP_0
);
7118 map
->failed_disk_num
= ~0;
7119 super
->updates_pending
++;
7120 a
->last_checkpoint
= 0;
7123 if (is_gen_migration(dev
)) {
7124 dprintf("while general migration");
7125 if (a
->last_checkpoint
>= a
->info
.component_size
)
7126 end_migration(dev
, super
, map_state
);
7128 map
->map_state
= map_state
;
7129 map
= get_imsm_map(dev
, MAP_0
);
7130 map
->failed_disk_num
= ~0;
7131 super
->updates_pending
++;
7135 case IMSM_T_STATE_DEGRADED
: /* transition to degraded state */
7136 dprintf("degraded: ");
7137 if ((map
->map_state
!= map_state
) &&
7138 !dev
->vol
.migr_state
) {
7139 dprintf("mark degraded");
7140 map
->map_state
= map_state
;
7141 super
->updates_pending
++;
7142 a
->last_checkpoint
= 0;
7145 if (is_rebuilding(dev
)) {
7146 dprintf("while rebuilding.");
7147 if (map
->map_state
!= map_state
) {
7148 dprintf(" Map state change");
7149 end_migration(dev
, super
, map_state
);
7150 super
->updates_pending
++;
7154 if (is_gen_migration(dev
)) {
7155 dprintf("while general migration");
7156 if (a
->last_checkpoint
>= a
->info
.component_size
)
7157 end_migration(dev
, super
, map_state
);
7159 map
->map_state
= map_state
;
7160 manage_second_map(super
, dev
);
7162 super
->updates_pending
++;
7165 if (is_initializing(dev
)) {
7166 dprintf("while initialization.");
7167 map
->map_state
= map_state
;
7168 super
->updates_pending
++;
7172 case IMSM_T_STATE_FAILED
: /* transition to failed state */
7173 dprintf("failed: ");
7174 if (is_gen_migration(dev
)) {
7175 dprintf("while general migration");
7176 map
->map_state
= map_state
;
7177 super
->updates_pending
++;
7180 if (map
->map_state
!= map_state
) {
7181 dprintf("mark failed");
7182 end_migration(dev
, super
, map_state
);
7183 super
->updates_pending
++;
7184 a
->last_checkpoint
= 0;
7189 dprintf("state %i\n", map_state
);
7195 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
7198 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
7199 unsigned long long dsize
;
7200 unsigned long long sectors
;
7202 get_dev_size(fd
, NULL
, &dsize
);
7204 if (mpb_size
> 512) {
7205 /* -1 to account for anchor */
7206 sectors
= mpb_sectors(mpb
) - 1;
7208 /* write the extended mpb to the sectors preceeding the anchor */
7209 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0)
7212 if ((unsigned long long)write(fd
, buf
+ 512, 512 * sectors
)
7217 /* first block is stored on second to last sector of the disk */
7218 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0)
7221 if (write(fd
, buf
, 512) != 512)
7227 static void imsm_sync_metadata(struct supertype
*container
)
7229 struct intel_super
*super
= container
->sb
;
7231 dprintf("sync metadata: %d\n", super
->updates_pending
);
7232 if (!super
->updates_pending
)
7235 write_super_imsm(container
, 0);
7237 super
->updates_pending
= 0;
7240 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
7242 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7243 int i
= get_imsm_disk_idx(dev
, idx
, MAP_X
);
7246 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7250 if (dl
&& is_failed(&dl
->disk
))
7254 dprintf("%s: found %x:%x\n", __func__
, dl
->major
, dl
->minor
);
7259 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
7260 struct active_array
*a
, int activate_new
,
7261 struct mdinfo
*additional_test_list
)
7263 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7264 int idx
= get_imsm_disk_idx(dev
, slot
, MAP_X
);
7265 struct imsm_super
*mpb
= super
->anchor
;
7266 struct imsm_map
*map
;
7267 unsigned long long pos
;
7272 __u32 array_start
= 0;
7273 __u32 array_end
= 0;
7275 struct mdinfo
*test_list
;
7277 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7278 /* If in this array, skip */
7279 for (d
= a
->info
.devs
; d
; d
= d
->next
)
7280 if (d
->state_fd
>= 0 &&
7281 d
->disk
.major
== dl
->major
&&
7282 d
->disk
.minor
== dl
->minor
) {
7283 dprintf("%x:%x already in array\n",
7284 dl
->major
, dl
->minor
);
7289 test_list
= additional_test_list
;
7291 if (test_list
->disk
.major
== dl
->major
&&
7292 test_list
->disk
.minor
== dl
->minor
) {
7293 dprintf("%x:%x already in additional test list\n",
7294 dl
->major
, dl
->minor
);
7297 test_list
= test_list
->next
;
7302 /* skip in use or failed drives */
7303 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
7305 dprintf("%x:%x status (failed: %d index: %d)\n",
7306 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
7310 /* skip pure spares when we are looking for partially
7311 * assimilated drives
7313 if (dl
->index
== -1 && !activate_new
)
7316 /* Does this unused device have the requisite free space?
7317 * It needs to be able to cover all member volumes
7319 ex
= get_extents(super
, dl
);
7321 dprintf("cannot get extents\n");
7324 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7325 dev
= get_imsm_dev(super
, i
);
7326 map
= get_imsm_map(dev
, MAP_0
);
7328 /* check if this disk is already a member of
7331 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
7337 array_start
= __le32_to_cpu(map
->pba_of_lba0
);
7338 array_end
= array_start
+
7339 __le32_to_cpu(map
->blocks_per_member
) - 1;
7342 /* check that we can start at pba_of_lba0 with
7343 * blocks_per_member of space
7345 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
7349 pos
= ex
[j
].start
+ ex
[j
].size
;
7351 } while (ex
[j
-1].size
);
7358 if (i
< mpb
->num_raid_devs
) {
7359 dprintf("%x:%x does not have %u to %u available\n",
7360 dl
->major
, dl
->minor
, array_start
, array_end
);
7371 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
7373 struct imsm_dev
*dev2
;
7374 struct imsm_map
*map
;
7380 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
7382 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
, MAP_0
);
7383 if (state
== IMSM_T_STATE_FAILED
) {
7384 map
= get_imsm_map(dev2
, MAP_0
);
7387 for (slot
= 0; slot
< map
->num_members
; slot
++) {
7389 * Check if failed disks are deleted from intel
7390 * disk list or are marked to be deleted
7392 idx
= get_imsm_disk_idx(dev2
, slot
, MAP_X
);
7393 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
7395 * Do not rebuild the array if failed disks
7396 * from failed sub-array are not removed from
7400 is_failed(&idisk
->disk
) &&
7401 (idisk
->action
!= DISK_REMOVE
))
7409 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
7410 struct metadata_update
**updates
)
7413 * Find a device with unused free space and use it to replace a
7414 * failed/vacant region in an array. We replace failed regions one a
7415 * array at a time. The result is that a new spare disk will be added
7416 * to the first failed array and after the monitor has finished
7417 * propagating failures the remainder will be consumed.
7419 * FIXME add a capability for mdmon to request spares from another
7423 struct intel_super
*super
= a
->container
->sb
;
7424 int inst
= a
->info
.container_member
;
7425 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7426 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7427 int failed
= a
->info
.array
.raid_disks
;
7428 struct mdinfo
*rv
= NULL
;
7431 struct metadata_update
*mu
;
7433 struct imsm_update_activate_spare
*u
;
7438 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
7439 if ((d
->curr_state
& DS_FAULTY
) &&
7441 /* wait for Removal to happen */
7443 if (d
->state_fd
>= 0)
7447 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
7448 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
7450 if (imsm_reshape_blocks_arrays_changes(super
))
7453 /* Cannot activate another spare if rebuild is in progress already
7455 if (is_rebuilding(dev
)) {
7456 dprintf("imsm: No spare activation allowed. "
7457 "Rebuild in progress already.\n");
7461 if (a
->info
.array
.level
== 4)
7462 /* No repair for takeovered array
7463 * imsm doesn't support raid4
7467 if (imsm_check_degraded(super
, dev
, failed
, MAP_0
) !=
7468 IMSM_T_STATE_DEGRADED
)
7472 * If there are any failed disks check state of the other volume.
7473 * Block rebuild if the another one is failed until failed disks
7474 * are removed from container.
7477 dprintf("found failed disks in %.*s, check if there another"
7478 "failed sub-array.\n",
7479 MAX_RAID_SERIAL_LEN
, dev
->volume
);
7480 /* check if states of the other volumes allow for rebuild */
7481 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
7483 allowed
= imsm_rebuild_allowed(a
->container
,
7491 /* For each slot, if it is not working, find a spare */
7492 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
7493 for (d
= a
->info
.devs
; d
; d
= d
->next
)
7494 if (d
->disk
.raid_disk
== i
)
7496 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
7497 if (d
&& (d
->state_fd
>= 0))
7501 * OK, this device needs recovery. Try to re-add the
7502 * previous occupant of this slot, if this fails see if
7503 * we can continue the assimilation of a spare that was
7504 * partially assimilated, finally try to activate a new
7507 dl
= imsm_readd(super
, i
, a
);
7509 dl
= imsm_add_spare(super
, i
, a
, 0, rv
);
7511 dl
= imsm_add_spare(super
, i
, a
, 1, rv
);
7515 /* found a usable disk with enough space */
7516 di
= malloc(sizeof(*di
));
7519 memset(di
, 0, sizeof(*di
));
7521 /* dl->index will be -1 in the case we are activating a
7522 * pristine spare. imsm_process_update() will create a
7523 * new index in this case. Once a disk is found to be
7524 * failed in all member arrays it is kicked from the
7527 di
->disk
.number
= dl
->index
;
7529 /* (ab)use di->devs to store a pointer to the device
7532 di
->devs
= (struct mdinfo
*) dl
;
7534 di
->disk
.raid_disk
= i
;
7535 di
->disk
.major
= dl
->major
;
7536 di
->disk
.minor
= dl
->minor
;
7538 di
->recovery_start
= 0;
7539 di
->data_offset
= __le32_to_cpu(map
->pba_of_lba0
);
7540 di
->component_size
= a
->info
.component_size
;
7541 di
->container_member
= inst
;
7542 super
->random
= random32();
7546 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
7547 i
, di
->data_offset
);
7551 /* No spares found */
7553 /* Now 'rv' has a list of devices to return.
7554 * Create a metadata_update record to update the
7555 * disk_ord_tbl for the array
7557 mu
= malloc(sizeof(*mu
));
7559 mu
->buf
= malloc(sizeof(struct imsm_update_activate_spare
) * num_spares
);
7560 if (mu
->buf
== NULL
) {
7567 struct mdinfo
*n
= rv
->next
;
7576 mu
->space_list
= NULL
;
7577 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
7578 mu
->next
= *updates
;
7579 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
7581 for (di
= rv
; di
; di
= di
->next
) {
7582 u
->type
= update_activate_spare
;
7583 u
->dl
= (struct dl
*) di
->devs
;
7585 u
->slot
= di
->disk
.raid_disk
;
7596 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
7598 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
7599 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7600 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, MAP_0
);
7601 struct disk_info
*inf
= get_disk_info(u
);
7602 struct imsm_disk
*disk
;
7606 for (i
= 0; i
< map
->num_members
; i
++) {
7607 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, MAP_X
));
7608 for (j
= 0; j
< new_map
->num_members
; j
++)
7609 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
7617 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
7619 struct dl
*dl
= NULL
;
7620 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7621 if ((dl
->major
== major
) && (dl
->minor
== minor
))
7626 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
7628 struct dl
*prev
= NULL
;
7632 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7633 if ((dl
->major
== major
) && (dl
->minor
== minor
)) {
7636 prev
->next
= dl
->next
;
7638 super
->disks
= dl
->next
;
7640 __free_imsm_disk(dl
);
7641 dprintf("%s: removed %x:%x\n",
7642 __func__
, major
, minor
);
7650 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
7652 static int add_remove_disk_update(struct intel_super
*super
)
7654 int check_degraded
= 0;
7655 struct dl
*disk
= NULL
;
7656 /* add/remove some spares to/from the metadata/contrainer */
7657 while (super
->disk_mgmt_list
) {
7658 struct dl
*disk_cfg
;
7660 disk_cfg
= super
->disk_mgmt_list
;
7661 super
->disk_mgmt_list
= disk_cfg
->next
;
7662 disk_cfg
->next
= NULL
;
7664 if (disk_cfg
->action
== DISK_ADD
) {
7665 disk_cfg
->next
= super
->disks
;
7666 super
->disks
= disk_cfg
;
7668 dprintf("%s: added %x:%x\n",
7669 __func__
, disk_cfg
->major
,
7671 } else if (disk_cfg
->action
== DISK_REMOVE
) {
7672 dprintf("Disk remove action processed: %x.%x\n",
7673 disk_cfg
->major
, disk_cfg
->minor
);
7674 disk
= get_disk_super(super
,
7678 /* store action status */
7679 disk
->action
= DISK_REMOVE
;
7680 /* remove spare disks only */
7681 if (disk
->index
== -1) {
7682 remove_disk_super(super
,
7687 /* release allocate disk structure */
7688 __free_imsm_disk(disk_cfg
);
7691 return check_degraded
;
7695 static int apply_reshape_migration_update(struct imsm_update_reshape_migration
*u
,
7696 struct intel_super
*super
,
7699 struct intel_dev
*id
;
7700 void **tofree
= NULL
;
7703 dprintf("apply_reshape_migration_update()\n");
7704 if ((u
->subdev
< 0) ||
7706 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
7709 if ((space_list
== NULL
) || (*space_list
== NULL
)) {
7710 dprintf("imsm: Error: Memory is not allocated\n");
7714 for (id
= super
->devlist
; id
; id
= id
->next
) {
7715 if (id
->index
== (unsigned)u
->subdev
) {
7716 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
7717 struct imsm_map
*map
;
7718 struct imsm_dev
*new_dev
=
7719 (struct imsm_dev
*)*space_list
;
7720 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
7722 struct dl
*new_disk
;
7724 if (new_dev
== NULL
)
7726 *space_list
= **space_list
;
7727 memcpy(new_dev
, dev
, sizeof_imsm_dev(dev
, 0));
7728 map
= get_imsm_map(new_dev
, MAP_0
);
7730 dprintf("imsm: Error: migration in progress");
7734 to_state
= map
->map_state
;
7735 if ((u
->new_level
== 5) && (map
->raid_level
== 0)) {
7737 /* this should not happen */
7738 if (u
->new_disks
[0] < 0) {
7739 map
->failed_disk_num
=
7740 map
->num_members
- 1;
7741 to_state
= IMSM_T_STATE_DEGRADED
;
7743 to_state
= IMSM_T_STATE_NORMAL
;
7745 migrate(new_dev
, super
, to_state
, MIGR_GEN_MIGR
);
7746 if (u
->new_level
> -1)
7747 map
->raid_level
= u
->new_level
;
7748 migr_map
= get_imsm_map(new_dev
, MAP_1
);
7749 if ((u
->new_level
== 5) &&
7750 (migr_map
->raid_level
== 0)) {
7751 int ord
= map
->num_members
- 1;
7752 migr_map
->num_members
--;
7753 if (u
->new_disks
[0] < 0)
7754 ord
|= IMSM_ORD_REBUILD
;
7755 set_imsm_ord_tbl_ent(map
,
7756 map
->num_members
- 1,
7760 tofree
= (void **)dev
;
7762 /* update chunk size
7764 if (u
->new_chunksize
> 0)
7765 map
->blocks_per_strip
=
7766 __cpu_to_le16(u
->new_chunksize
* 2);
7770 if ((u
->new_level
!= 5) ||
7771 (migr_map
->raid_level
!= 0) ||
7772 (migr_map
->raid_level
== map
->raid_level
))
7775 if (u
->new_disks
[0] >= 0) {
7778 new_disk
= get_disk_super(super
,
7779 major(u
->new_disks
[0]),
7780 minor(u
->new_disks
[0]));
7781 dprintf("imsm: new disk for reshape is: %i:%i "
7782 "(%p, index = %i)\n",
7783 major(u
->new_disks
[0]),
7784 minor(u
->new_disks
[0]),
7785 new_disk
, new_disk
->index
);
7786 if (new_disk
== NULL
)
7787 goto error_disk_add
;
7789 new_disk
->index
= map
->num_members
- 1;
7790 /* slot to fill in autolayout
7792 new_disk
->raiddisk
= new_disk
->index
;
7793 new_disk
->disk
.status
|= CONFIGURED_DISK
;
7794 new_disk
->disk
.status
&= ~SPARE_DISK
;
7796 goto error_disk_add
;
7799 *tofree
= *space_list
;
7800 /* calculate new size
7802 imsm_set_array_size(new_dev
);
7809 *space_list
= tofree
;
7813 dprintf("Error: imsm: Cannot find disk.\n");
7817 static int apply_update_activate_spare(struct imsm_update_activate_spare
*u
,
7818 struct intel_super
*super
,
7819 struct active_array
*active_array
)
7821 struct imsm_super
*mpb
= super
->anchor
;
7822 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
7823 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7824 struct imsm_map
*migr_map
;
7825 struct active_array
*a
;
7826 struct imsm_disk
*disk
;
7833 int second_map_created
= 0;
7835 for (; u
; u
= u
->next
) {
7836 victim
= get_imsm_disk_idx(dev
, u
->slot
, MAP_X
);
7841 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7846 fprintf(stderr
, "error: imsm_activate_spare passed "
7847 "an unknown disk (index: %d)\n",
7852 /* count failures (excluding rebuilds and the victim)
7853 * to determine map[0] state
7856 for (i
= 0; i
< map
->num_members
; i
++) {
7859 disk
= get_imsm_disk(super
,
7860 get_imsm_disk_idx(dev
, i
, MAP_X
));
7861 if (!disk
|| is_failed(disk
))
7865 /* adding a pristine spare, assign a new index */
7866 if (dl
->index
< 0) {
7867 dl
->index
= super
->anchor
->num_disks
;
7868 super
->anchor
->num_disks
++;
7871 disk
->status
|= CONFIGURED_DISK
;
7872 disk
->status
&= ~SPARE_DISK
;
7875 to_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7876 if (!second_map_created
) {
7877 second_map_created
= 1;
7878 map
->map_state
= IMSM_T_STATE_DEGRADED
;
7879 migrate(dev
, super
, to_state
, MIGR_REBUILD
);
7881 map
->map_state
= to_state
;
7882 migr_map
= get_imsm_map(dev
, MAP_1
);
7883 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
7884 set_imsm_ord_tbl_ent(migr_map
, u
->slot
,
7885 dl
->index
| IMSM_ORD_REBUILD
);
7887 /* update the family_num to mark a new container
7888 * generation, being careful to record the existing
7889 * family_num in orig_family_num to clean up after
7890 * earlier mdadm versions that neglected to set it.
7892 if (mpb
->orig_family_num
== 0)
7893 mpb
->orig_family_num
= mpb
->family_num
;
7894 mpb
->family_num
+= super
->random
;
7896 /* count arrays using the victim in the metadata */
7898 for (a
= active_array
; a
; a
= a
->next
) {
7899 dev
= get_imsm_dev(super
, a
->info
.container_member
);
7900 map
= get_imsm_map(dev
, MAP_0
);
7902 if (get_imsm_disk_slot(map
, victim
) >= 0)
7906 /* delete the victim if it is no longer being
7912 /* We know that 'manager' isn't touching anything,
7913 * so it is safe to delete
7915 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
7916 if ((*dlp
)->index
== victim
)
7919 /* victim may be on the missing list */
7921 for (dlp
= &super
->missing
; *dlp
;
7922 dlp
= &(*dlp
)->next
)
7923 if ((*dlp
)->index
== victim
)
7925 imsm_delete(super
, dlp
, victim
);
7932 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
7933 struct intel_super
*super
,
7936 struct dl
*new_disk
;
7937 struct intel_dev
*id
;
7939 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
7940 int disk_count
= u
->old_raid_disks
;
7941 void **tofree
= NULL
;
7942 int devices_to_reshape
= 1;
7943 struct imsm_super
*mpb
= super
->anchor
;
7945 unsigned int dev_id
;
7947 dprintf("imsm: apply_reshape_container_disks_update()\n");
7949 /* enable spares to use in array */
7950 for (i
= 0; i
< delta_disks
; i
++) {
7951 new_disk
= get_disk_super(super
,
7952 major(u
->new_disks
[i
]),
7953 minor(u
->new_disks
[i
]));
7954 dprintf("imsm: new disk for reshape is: %i:%i "
7955 "(%p, index = %i)\n",
7956 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
7957 new_disk
, new_disk
->index
);
7958 if ((new_disk
== NULL
) ||
7959 ((new_disk
->index
>= 0) &&
7960 (new_disk
->index
< u
->old_raid_disks
)))
7961 goto update_reshape_exit
;
7962 new_disk
->index
= disk_count
++;
7963 /* slot to fill in autolayout
7965 new_disk
->raiddisk
= new_disk
->index
;
7966 new_disk
->disk
.status
|=
7968 new_disk
->disk
.status
&= ~SPARE_DISK
;
7971 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
7972 mpb
->num_raid_devs
);
7973 /* manage changes in volume
7975 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
7976 void **sp
= *space_list
;
7977 struct imsm_dev
*newdev
;
7978 struct imsm_map
*newmap
, *oldmap
;
7980 for (id
= super
->devlist
; id
; id
= id
->next
) {
7981 if (id
->index
== dev_id
)
7990 /* Copy the dev, but not (all of) the map */
7991 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
7992 oldmap
= get_imsm_map(id
->dev
, MAP_0
);
7993 newmap
= get_imsm_map(newdev
, MAP_0
);
7994 /* Copy the current map */
7995 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
7996 /* update one device only
7998 if (devices_to_reshape
) {
7999 dprintf("imsm: modifying subdev: %i\n",
8001 devices_to_reshape
--;
8002 newdev
->vol
.migr_state
= 1;
8003 newdev
->vol
.curr_migr_unit
= 0;
8004 set_migr_type(newdev
, MIGR_GEN_MIGR
);
8005 newmap
->num_members
= u
->new_raid_disks
;
8006 for (i
= 0; i
< delta_disks
; i
++) {
8007 set_imsm_ord_tbl_ent(newmap
,
8008 u
->old_raid_disks
+ i
,
8009 u
->old_raid_disks
+ i
);
8011 /* New map is correct, now need to save old map
8013 newmap
= get_imsm_map(newdev
, MAP_1
);
8014 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8016 imsm_set_array_size(newdev
);
8019 sp
= (void **)id
->dev
;
8024 /* Clear migration record */
8025 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
8028 *space_list
= tofree
;
8031 update_reshape_exit
:
8036 static int apply_takeover_update(struct imsm_update_takeover
*u
,
8037 struct intel_super
*super
,
8040 struct imsm_dev
*dev
= NULL
;
8041 struct intel_dev
*dv
;
8042 struct imsm_dev
*dev_new
;
8043 struct imsm_map
*map
;
8047 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
8048 if (dv
->index
== (unsigned int)u
->subarray
) {
8056 map
= get_imsm_map(dev
, MAP_0
);
8058 if (u
->direction
== R10_TO_R0
) {
8059 /* Number of failed disks must be half of initial disk number */
8060 if (imsm_count_failed(super
, dev
, MAP_0
) !=
8061 (map
->num_members
/ 2))
8064 /* iterate through devices to mark removed disks as spare */
8065 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8066 if (dm
->disk
.status
& FAILED_DISK
) {
8067 int idx
= dm
->index
;
8068 /* update indexes on the disk list */
8069 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
8070 the index values will end up being correct.... NB */
8071 for (du
= super
->disks
; du
; du
= du
->next
)
8072 if (du
->index
> idx
)
8074 /* mark as spare disk */
8079 map
->num_members
= map
->num_members
/ 2;
8080 map
->map_state
= IMSM_T_STATE_NORMAL
;
8081 map
->num_domains
= 1;
8082 map
->raid_level
= 0;
8083 map
->failed_disk_num
= -1;
8086 if (u
->direction
== R0_TO_R10
) {
8088 /* update slots in current disk list */
8089 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8093 /* create new *missing* disks */
8094 for (i
= 0; i
< map
->num_members
; i
++) {
8095 space
= *space_list
;
8098 *space_list
= *space
;
8100 memcpy(du
, super
->disks
, sizeof(*du
));
8104 du
->index
= (i
* 2) + 1;
8105 sprintf((char *)du
->disk
.serial
,
8106 " MISSING_%d", du
->index
);
8107 sprintf((char *)du
->serial
,
8108 "MISSING_%d", du
->index
);
8109 du
->next
= super
->missing
;
8110 super
->missing
= du
;
8112 /* create new dev and map */
8113 space
= *space_list
;
8116 *space_list
= *space
;
8117 dev_new
= (void *)space
;
8118 memcpy(dev_new
, dev
, sizeof(*dev
));
8119 /* update new map */
8120 map
= get_imsm_map(dev_new
, MAP_0
);
8121 map
->num_members
= map
->num_members
* 2;
8122 map
->map_state
= IMSM_T_STATE_DEGRADED
;
8123 map
->num_domains
= 2;
8124 map
->raid_level
= 1;
8125 /* replace dev<->dev_new */
8128 /* update disk order table */
8129 for (du
= super
->disks
; du
; du
= du
->next
)
8131 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8132 for (du
= super
->missing
; du
; du
= du
->next
)
8133 if (du
->index
>= 0) {
8134 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8135 mark_missing(dv
->dev
, &du
->disk
, du
->index
);
8141 static void imsm_process_update(struct supertype
*st
,
8142 struct metadata_update
*update
)
8145 * crack open the metadata_update envelope to find the update record
8146 * update can be one of:
8147 * update_reshape_container_disks - all the arrays in the container
8148 * are being reshaped to have more devices. We need to mark
8149 * the arrays for general migration and convert selected spares
8150 * into active devices.
8151 * update_activate_spare - a spare device has replaced a failed
8152 * device in an array, update the disk_ord_tbl. If this disk is
8153 * present in all member arrays then also clear the SPARE_DISK
8155 * update_create_array
8157 * update_rename_array
8158 * update_add_remove_disk
8160 struct intel_super
*super
= st
->sb
;
8161 struct imsm_super
*mpb
;
8162 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
8164 /* update requires a larger buf but the allocation failed */
8165 if (super
->next_len
&& !super
->next_buf
) {
8166 super
->next_len
= 0;
8170 if (super
->next_buf
) {
8171 memcpy(super
->next_buf
, super
->buf
, super
->len
);
8173 super
->len
= super
->next_len
;
8174 super
->buf
= super
->next_buf
;
8176 super
->next_len
= 0;
8177 super
->next_buf
= NULL
;
8180 mpb
= super
->anchor
;
8183 case update_general_migration_checkpoint
: {
8184 struct intel_dev
*id
;
8185 struct imsm_update_general_migration_checkpoint
*u
=
8186 (void *)update
->buf
;
8188 dprintf("imsm: process_update() "
8189 "for update_general_migration_checkpoint called\n");
8191 /* find device under general migration */
8192 for (id
= super
->devlist
; id
; id
= id
->next
) {
8193 if (is_gen_migration(id
->dev
)) {
8194 id
->dev
->vol
.curr_migr_unit
=
8195 __cpu_to_le32(u
->curr_migr_unit
);
8196 super
->updates_pending
++;
8201 case update_takeover
: {
8202 struct imsm_update_takeover
*u
= (void *)update
->buf
;
8203 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
8204 imsm_update_version_info(super
);
8205 super
->updates_pending
++;
8210 case update_reshape_container_disks
: {
8211 struct imsm_update_reshape
*u
= (void *)update
->buf
;
8212 if (apply_reshape_container_disks_update(
8213 u
, super
, &update
->space_list
))
8214 super
->updates_pending
++;
8217 case update_reshape_migration
: {
8218 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
8219 if (apply_reshape_migration_update(
8220 u
, super
, &update
->space_list
))
8221 super
->updates_pending
++;
8224 case update_activate_spare
: {
8225 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
8226 if (apply_update_activate_spare(u
, super
, st
->arrays
))
8227 super
->updates_pending
++;
8230 case update_create_array
: {
8231 /* someone wants to create a new array, we need to be aware of
8232 * a few races/collisions:
8233 * 1/ 'Create' called by two separate instances of mdadm
8234 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
8235 * devices that have since been assimilated via
8237 * In the event this update can not be carried out mdadm will
8238 * (FIX ME) notice that its update did not take hold.
8240 struct imsm_update_create_array
*u
= (void *) update
->buf
;
8241 struct intel_dev
*dv
;
8242 struct imsm_dev
*dev
;
8243 struct imsm_map
*map
, *new_map
;
8244 unsigned long long start
, end
;
8245 unsigned long long new_start
, new_end
;
8247 struct disk_info
*inf
;
8250 /* handle racing creates: first come first serve */
8251 if (u
->dev_idx
< mpb
->num_raid_devs
) {
8252 dprintf("%s: subarray %d already defined\n",
8253 __func__
, u
->dev_idx
);
8257 /* check update is next in sequence */
8258 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
8259 dprintf("%s: can not create array %d expected index %d\n",
8260 __func__
, u
->dev_idx
, mpb
->num_raid_devs
);
8264 new_map
= get_imsm_map(&u
->dev
, MAP_0
);
8265 new_start
= __le32_to_cpu(new_map
->pba_of_lba0
);
8266 new_end
= new_start
+ __le32_to_cpu(new_map
->blocks_per_member
);
8267 inf
= get_disk_info(u
);
8269 /* handle activate_spare versus create race:
8270 * check to make sure that overlapping arrays do not include
8273 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8274 dev
= get_imsm_dev(super
, i
);
8275 map
= get_imsm_map(dev
, MAP_0
);
8276 start
= __le32_to_cpu(map
->pba_of_lba0
);
8277 end
= start
+ __le32_to_cpu(map
->blocks_per_member
);
8278 if ((new_start
>= start
&& new_start
<= end
) ||
8279 (start
>= new_start
&& start
<= new_end
))
8284 if (disks_overlap(super
, i
, u
)) {
8285 dprintf("%s: arrays overlap\n", __func__
);
8290 /* check that prepare update was successful */
8291 if (!update
->space
) {
8292 dprintf("%s: prepare update failed\n", __func__
);
8296 /* check that all disks are still active before committing
8297 * changes. FIXME: could we instead handle this by creating a
8298 * degraded array? That's probably not what the user expects,
8299 * so better to drop this update on the floor.
8301 for (i
= 0; i
< new_map
->num_members
; i
++) {
8302 dl
= serial_to_dl(inf
[i
].serial
, super
);
8304 dprintf("%s: disk disappeared\n", __func__
);
8309 super
->updates_pending
++;
8311 /* convert spares to members and fixup ord_tbl */
8312 for (i
= 0; i
< new_map
->num_members
; i
++) {
8313 dl
= serial_to_dl(inf
[i
].serial
, super
);
8314 if (dl
->index
== -1) {
8315 dl
->index
= mpb
->num_disks
;
8317 dl
->disk
.status
|= CONFIGURED_DISK
;
8318 dl
->disk
.status
&= ~SPARE_DISK
;
8320 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
8325 update
->space
= NULL
;
8326 imsm_copy_dev(dev
, &u
->dev
);
8327 dv
->index
= u
->dev_idx
;
8328 dv
->next
= super
->devlist
;
8329 super
->devlist
= dv
;
8330 mpb
->num_raid_devs
++;
8332 imsm_update_version_info(super
);
8335 /* mdmon knows how to release update->space, but not
8336 * ((struct intel_dev *) update->space)->dev
8338 if (update
->space
) {
8344 case update_kill_array
: {
8345 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
8346 int victim
= u
->dev_idx
;
8347 struct active_array
*a
;
8348 struct intel_dev
**dp
;
8349 struct imsm_dev
*dev
;
8351 /* sanity check that we are not affecting the uuid of
8352 * active arrays, or deleting an active array
8354 * FIXME when immutable ids are available, but note that
8355 * we'll also need to fixup the invalidated/active
8356 * subarray indexes in mdstat
8358 for (a
= st
->arrays
; a
; a
= a
->next
)
8359 if (a
->info
.container_member
>= victim
)
8361 /* by definition if mdmon is running at least one array
8362 * is active in the container, so checking
8363 * mpb->num_raid_devs is just extra paranoia
8365 dev
= get_imsm_dev(super
, victim
);
8366 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
8367 dprintf("failed to delete subarray-%d\n", victim
);
8371 for (dp
= &super
->devlist
; *dp
;)
8372 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
8375 if ((*dp
)->index
> (unsigned)victim
)
8379 mpb
->num_raid_devs
--;
8380 super
->updates_pending
++;
8383 case update_rename_array
: {
8384 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
8385 char name
[MAX_RAID_SERIAL_LEN
+1];
8386 int target
= u
->dev_idx
;
8387 struct active_array
*a
;
8388 struct imsm_dev
*dev
;
8390 /* sanity check that we are not affecting the uuid of
8393 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
8394 name
[MAX_RAID_SERIAL_LEN
] = '\0';
8395 for (a
= st
->arrays
; a
; a
= a
->next
)
8396 if (a
->info
.container_member
== target
)
8398 dev
= get_imsm_dev(super
, u
->dev_idx
);
8399 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
8400 dprintf("failed to rename subarray-%d\n", target
);
8404 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
8405 super
->updates_pending
++;
8408 case update_add_remove_disk
: {
8409 /* we may be able to repair some arrays if disks are
8410 * being added, check teh status of add_remove_disk
8411 * if discs has been added.
8413 if (add_remove_disk_update(super
)) {
8414 struct active_array
*a
;
8416 super
->updates_pending
++;
8417 for (a
= st
->arrays
; a
; a
= a
->next
)
8418 a
->check_degraded
= 1;
8423 fprintf(stderr
, "error: unsuported process update type:"
8424 "(type: %d)\n", type
);
8428 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
);
8430 static void imsm_prepare_update(struct supertype
*st
,
8431 struct metadata_update
*update
)
8434 * Allocate space to hold new disk entries, raid-device entries or a new
8435 * mpb if necessary. The manager synchronously waits for updates to
8436 * complete in the monitor, so new mpb buffers allocated here can be
8437 * integrated by the monitor thread without worrying about live pointers
8438 * in the manager thread.
8440 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
8441 struct intel_super
*super
= st
->sb
;
8442 struct imsm_super
*mpb
= super
->anchor
;
8447 case update_general_migration_checkpoint
:
8448 dprintf("imsm: prepare_update() "
8449 "for update_general_migration_checkpoint called\n");
8451 case update_takeover
: {
8452 struct imsm_update_takeover
*u
= (void *)update
->buf
;
8453 if (u
->direction
== R0_TO_R10
) {
8454 void **tail
= (void **)&update
->space_list
;
8455 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
8456 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8457 int num_members
= map
->num_members
;
8461 /* allocate memory for added disks */
8462 for (i
= 0; i
< num_members
; i
++) {
8463 size
= sizeof(struct dl
);
8464 space
= malloc(size
);
8473 /* allocate memory for new device */
8474 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
8475 (num_members
* sizeof(__u32
));
8476 space
= malloc(size
);
8485 len
= disks_to_mpb_size(num_members
* 2);
8487 /* if allocation didn't success, free buffer */
8488 while (update
->space_list
) {
8489 void **sp
= update
->space_list
;
8490 update
->space_list
= *sp
;
8498 case update_reshape_container_disks
: {
8499 /* Every raid device in the container is about to
8500 * gain some more devices, and we will enter a
8502 * So each 'imsm_map' will be bigger, and the imsm_vol
8503 * will now hold 2 of them.
8504 * Thus we need new 'struct imsm_dev' allocations sized
8505 * as sizeof_imsm_dev but with more devices in both maps.
8507 struct imsm_update_reshape
*u
= (void *)update
->buf
;
8508 struct intel_dev
*dl
;
8509 void **space_tail
= (void**)&update
->space_list
;
8511 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
8513 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
8514 int size
= sizeof_imsm_dev(dl
->dev
, 1);
8516 if (u
->new_raid_disks
> u
->old_raid_disks
)
8517 size
+= sizeof(__u32
)*2*
8518 (u
->new_raid_disks
- u
->old_raid_disks
);
8527 len
= disks_to_mpb_size(u
->new_raid_disks
);
8528 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
8531 case update_reshape_migration
: {
8532 /* for migration level 0->5 we need to add disks
8533 * so the same as for container operation we will copy
8534 * device to the bigger location.
8535 * in memory prepared device and new disk area are prepared
8536 * for usage in process update
8538 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
8539 struct intel_dev
*id
;
8540 void **space_tail
= (void **)&update
->space_list
;
8543 int current_level
= -1;
8545 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
8547 /* add space for bigger array in update
8549 for (id
= super
->devlist
; id
; id
= id
->next
) {
8550 if (id
->index
== (unsigned)u
->subdev
) {
8551 size
= sizeof_imsm_dev(id
->dev
, 1);
8552 if (u
->new_raid_disks
> u
->old_raid_disks
)
8553 size
+= sizeof(__u32
)*2*
8554 (u
->new_raid_disks
- u
->old_raid_disks
);
8564 if (update
->space_list
== NULL
)
8567 /* add space for disk in update
8569 size
= sizeof(struct dl
);
8572 free(update
->space_list
);
8573 update
->space_list
= NULL
;
8580 /* add spare device to update
8582 for (id
= super
->devlist
; id
; id
= id
->next
)
8583 if (id
->index
== (unsigned)u
->subdev
) {
8584 struct imsm_dev
*dev
;
8585 struct imsm_map
*map
;
8587 dev
= get_imsm_dev(super
, u
->subdev
);
8588 map
= get_imsm_map(dev
, MAP_0
);
8589 current_level
= map
->raid_level
;
8592 if ((u
->new_level
== 5) && (u
->new_level
!= current_level
)) {
8593 struct mdinfo
*spares
;
8595 spares
= get_spares_for_grow(st
);
8603 makedev(dev
->disk
.major
,
8605 dl
= get_disk_super(super
,
8608 dl
->index
= u
->old_raid_disks
;
8614 len
= disks_to_mpb_size(u
->new_raid_disks
);
8615 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
8618 case update_create_array
: {
8619 struct imsm_update_create_array
*u
= (void *) update
->buf
;
8620 struct intel_dev
*dv
;
8621 struct imsm_dev
*dev
= &u
->dev
;
8622 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8624 struct disk_info
*inf
;
8628 inf
= get_disk_info(u
);
8629 len
= sizeof_imsm_dev(dev
, 1);
8630 /* allocate a new super->devlist entry */
8631 dv
= malloc(sizeof(*dv
));
8633 dv
->dev
= malloc(len
);
8638 update
->space
= NULL
;
8642 /* count how many spares will be converted to members */
8643 for (i
= 0; i
< map
->num_members
; i
++) {
8644 dl
= serial_to_dl(inf
[i
].serial
, super
);
8646 /* hmm maybe it failed?, nothing we can do about
8651 if (count_memberships(dl
, super
) == 0)
8654 len
+= activate
* sizeof(struct imsm_disk
);
8661 /* check if we need a larger metadata buffer */
8662 if (super
->next_buf
)
8663 buf_len
= super
->next_len
;
8665 buf_len
= super
->len
;
8667 if (__le32_to_cpu(mpb
->mpb_size
) + len
> buf_len
) {
8668 /* ok we need a larger buf than what is currently allocated
8669 * if this allocation fails process_update will notice that
8670 * ->next_len is set and ->next_buf is NULL
8672 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + len
, 512);
8673 if (super
->next_buf
)
8674 free(super
->next_buf
);
8676 super
->next_len
= buf_len
;
8677 if (posix_memalign(&super
->next_buf
, 512, buf_len
) == 0)
8678 memset(super
->next_buf
, 0, buf_len
);
8680 super
->next_buf
= NULL
;
8684 /* must be called while manager is quiesced */
8685 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
8687 struct imsm_super
*mpb
= super
->anchor
;
8689 struct imsm_dev
*dev
;
8690 struct imsm_map
*map
;
8691 int i
, j
, num_members
;
8694 dprintf("%s: deleting device[%d] from imsm_super\n",
8697 /* shift all indexes down one */
8698 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
8699 if (iter
->index
> (int)index
)
8701 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
8702 if (iter
->index
> (int)index
)
8705 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8706 dev
= get_imsm_dev(super
, i
);
8707 map
= get_imsm_map(dev
, MAP_0
);
8708 num_members
= map
->num_members
;
8709 for (j
= 0; j
< num_members
; j
++) {
8710 /* update ord entries being careful not to propagate
8711 * ord-flags to the first map
8713 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_X
);
8715 if (ord_to_idx(ord
) <= index
)
8718 map
= get_imsm_map(dev
, MAP_0
);
8719 set_imsm_ord_tbl_ent(map
, j
, ord_to_idx(ord
- 1));
8720 map
= get_imsm_map(dev
, MAP_1
);
8722 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
8727 super
->updates_pending
++;
8729 struct dl
*dl
= *dlp
;
8731 *dlp
= (*dlp
)->next
;
8732 __free_imsm_disk(dl
);
8735 #endif /* MDASSEMBLE */
8737 static void close_targets(int *targets
, int new_disks
)
8744 for (i
= 0; i
< new_disks
; i
++) {
8745 if (targets
[i
] >= 0) {
8752 static int imsm_get_allowed_degradation(int level
, int raid_disks
,
8753 struct intel_super
*super
,
8754 struct imsm_dev
*dev
)
8759 struct imsm_map
*map
;
8762 ret_val
= raid_disks
/2;
8763 /* check map if all disks pairs not failed
8766 map
= get_imsm_map(dev
, MAP_0
);
8767 for (i
= 0; i
< ret_val
; i
++) {
8768 int degradation
= 0;
8769 if (get_imsm_disk(super
, i
) == NULL
)
8771 if (get_imsm_disk(super
, i
+ 1) == NULL
)
8773 if (degradation
== 2)
8776 map
= get_imsm_map(dev
, MAP_1
);
8777 /* if there is no second map
8778 * result can be returned
8782 /* check degradation in second map
8784 for (i
= 0; i
< ret_val
; i
++) {
8785 int degradation
= 0;
8786 if (get_imsm_disk(super
, i
) == NULL
)
8788 if (get_imsm_disk(super
, i
+ 1) == NULL
)
8790 if (degradation
== 2)
8805 /*******************************************************************************
8806 * Function: open_backup_targets
8807 * Description: Function opens file descriptors for all devices given in
8810 * info : general array info
8811 * raid_disks : number of disks
8812 * raid_fds : table of device's file descriptors
8813 * super : intel super for raid10 degradation check
8814 * dev : intel device for raid10 degradation check
8818 ******************************************************************************/
8819 int open_backup_targets(struct mdinfo
*info
, int raid_disks
, int *raid_fds
,
8820 struct intel_super
*super
, struct imsm_dev
*dev
)
8826 for (i
= 0; i
< raid_disks
; i
++)
8829 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
8832 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
)) {
8833 dprintf("disk is faulty!!\n");
8837 if ((sd
->disk
.raid_disk
>= raid_disks
) ||
8838 (sd
->disk
.raid_disk
< 0))
8841 dn
= map_dev(sd
->disk
.major
,
8843 raid_fds
[sd
->disk
.raid_disk
] = dev_open(dn
, O_RDWR
);
8844 if (raid_fds
[sd
->disk
.raid_disk
] < 0) {
8845 fprintf(stderr
, "cannot open component\n");
8850 /* check if maximum array degradation level is not exceeded
8852 if ((raid_disks
- opened
) >
8853 imsm_get_allowed_degradation(info
->new_level
,
8856 fprintf(stderr
, "Not enough disks can be opened.\n");
8857 close_targets(raid_fds
, raid_disks
);
8864 /*******************************************************************************
8865 * Function: init_migr_record_imsm
8866 * Description: Function inits imsm migration record
8868 * super : imsm internal array info
8869 * dev : device under migration
8870 * info : general array info to find the smallest device
8873 ******************************************************************************/
8874 void init_migr_record_imsm(struct supertype
*st
, struct imsm_dev
*dev
,
8875 struct mdinfo
*info
)
8877 struct intel_super
*super
= st
->sb
;
8878 struct migr_record
*migr_rec
= super
->migr_rec
;
8880 unsigned long long dsize
, dev_sectors
;
8881 long long unsigned min_dev_sectors
= -1LLU;
8885 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
8886 struct imsm_map
*map_src
= get_imsm_map(dev
, MAP_1
);
8887 unsigned long long num_migr_units
;
8888 unsigned long long array_blocks
;
8890 memset(migr_rec
, 0, sizeof(struct migr_record
));
8891 migr_rec
->family_num
= __cpu_to_le32(super
->anchor
->family_num
);
8893 /* only ascending reshape supported now */
8894 migr_rec
->ascending_migr
= __cpu_to_le32(1);
8896 migr_rec
->dest_depth_per_unit
= GEN_MIGR_AREA_SIZE
/
8897 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
8898 migr_rec
->dest_depth_per_unit
*= map_dest
->blocks_per_strip
;
8899 new_data_disks
= imsm_num_data_members(dev
, MAP_0
);
8900 migr_rec
->blocks_per_unit
=
8901 __cpu_to_le32(migr_rec
->dest_depth_per_unit
* new_data_disks
);
8902 migr_rec
->dest_depth_per_unit
=
8903 __cpu_to_le32(migr_rec
->dest_depth_per_unit
);
8904 array_blocks
= info
->component_size
* new_data_disks
;
8906 array_blocks
/ __le32_to_cpu(migr_rec
->blocks_per_unit
);
8908 if (array_blocks
% __le32_to_cpu(migr_rec
->blocks_per_unit
))
8910 migr_rec
->num_migr_units
= __cpu_to_le32(num_migr_units
);
8912 migr_rec
->post_migr_vol_cap
= dev
->size_low
;
8913 migr_rec
->post_migr_vol_cap_hi
= dev
->size_high
;
8916 /* Find the smallest dev */
8917 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
8918 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
8919 fd
= dev_open(nm
, O_RDONLY
);
8922 get_dev_size(fd
, NULL
, &dsize
);
8923 dev_sectors
= dsize
/ 512;
8924 if (dev_sectors
< min_dev_sectors
)
8925 min_dev_sectors
= dev_sectors
;
8928 migr_rec
->ckpt_area_pba
= __cpu_to_le32(min_dev_sectors
-
8929 RAID_DISK_RESERVED_BLOCKS_IMSM_HI
);
8931 write_imsm_migr_rec(st
);
8936 /*******************************************************************************
8937 * Function: save_backup_imsm
8938 * Description: Function saves critical data stripes to Migration Copy Area
8939 * and updates the current migration unit status.
8940 * Use restore_stripes() to form a destination stripe,
8941 * and to write it to the Copy Area.
8943 * st : supertype information
8944 * dev : imsm device that backup is saved for
8945 * info : general array info
8946 * buf : input buffer
8947 * length : length of data to backup (blocks_per_unit)
8951 ******************************************************************************/
8952 int save_backup_imsm(struct supertype
*st
,
8953 struct imsm_dev
*dev
,
8954 struct mdinfo
*info
,
8959 struct intel_super
*super
= st
->sb
;
8960 unsigned long long *target_offsets
= NULL
;
8961 int *targets
= NULL
;
8963 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
8964 int new_disks
= map_dest
->num_members
;
8965 int dest_layout
= 0;
8967 unsigned long long start
;
8968 int data_disks
= imsm_num_data_members(dev
, MAP_0
);
8970 targets
= malloc(new_disks
* sizeof(int));
8974 for (i
= 0; i
< new_disks
; i
++)
8977 target_offsets
= malloc(new_disks
* sizeof(unsigned long long));
8978 if (!target_offsets
)
8981 start
= info
->reshape_progress
* 512;
8982 for (i
= 0; i
< new_disks
; i
++) {
8983 target_offsets
[i
] = (unsigned long long)
8984 __le32_to_cpu(super
->migr_rec
->ckpt_area_pba
) * 512;
8985 /* move back copy area adderss, it will be moved forward
8986 * in restore_stripes() using start input variable
8988 target_offsets
[i
] -= start
/data_disks
;
8991 if (open_backup_targets(info
, new_disks
, targets
,
8995 dest_layout
= imsm_level_to_layout(map_dest
->raid_level
);
8996 dest_chunk
= __le16_to_cpu(map_dest
->blocks_per_strip
) * 512;
8998 if (restore_stripes(targets
, /* list of dest devices */
8999 target_offsets
, /* migration record offsets */
9002 map_dest
->raid_level
,
9004 -1, /* source backup file descriptor */
9005 0, /* input buf offset
9006 * always 0 buf is already offseted */
9010 fprintf(stderr
, Name
": Error restoring stripes\n");
9018 close_targets(targets
, new_disks
);
9021 free(target_offsets
);
9026 /*******************************************************************************
9027 * Function: save_checkpoint_imsm
9028 * Description: Function called for current unit status update
9029 * in the migration record. It writes it to disk.
9031 * super : imsm internal array info
9032 * info : general array info
9036 * 2: failure, means no valid migration record
9037 * / no general migration in progress /
9038 ******************************************************************************/
9039 int save_checkpoint_imsm(struct supertype
*st
, struct mdinfo
*info
, int state
)
9041 struct intel_super
*super
= st
->sb
;
9042 unsigned long long blocks_per_unit
;
9043 unsigned long long curr_migr_unit
;
9045 if (load_imsm_migr_rec(super
, info
) != 0) {
9046 dprintf("imsm: ERROR: Cannot read migration record "
9047 "for checkpoint save.\n");
9051 blocks_per_unit
= __le32_to_cpu(super
->migr_rec
->blocks_per_unit
);
9052 if (blocks_per_unit
== 0) {
9053 dprintf("imsm: no migration in progress.\n");
9056 curr_migr_unit
= info
->reshape_progress
/ blocks_per_unit
;
9057 /* check if array is alligned to copy area
9058 * if it is not alligned, add one to current migration unit value
9059 * this can happend on array reshape finish only
9061 if (info
->reshape_progress
% blocks_per_unit
)
9064 super
->migr_rec
->curr_migr_unit
=
9065 __cpu_to_le32(curr_migr_unit
);
9066 super
->migr_rec
->rec_status
= __cpu_to_le32(state
);
9067 super
->migr_rec
->dest_1st_member_lba
=
9068 __cpu_to_le32(curr_migr_unit
*
9069 __le32_to_cpu(super
->migr_rec
->dest_depth_per_unit
));
9070 if (write_imsm_migr_rec(st
) < 0) {
9071 dprintf("imsm: Cannot write migration record "
9072 "outside backup area\n");
9079 /*******************************************************************************
9080 * Function: recover_backup_imsm
9081 * Description: Function recovers critical data from the Migration Copy Area
9082 * while assembling an array.
9084 * super : imsm internal array info
9085 * info : general array info
9087 * 0 : success (or there is no data to recover)
9089 ******************************************************************************/
9090 int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
)
9092 struct intel_super
*super
= st
->sb
;
9093 struct migr_record
*migr_rec
= super
->migr_rec
;
9094 struct imsm_map
*map_dest
= NULL
;
9095 struct intel_dev
*id
= NULL
;
9096 unsigned long long read_offset
;
9097 unsigned long long write_offset
;
9099 int *targets
= NULL
;
9100 int new_disks
, i
, err
;
9103 unsigned long curr_migr_unit
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
9104 unsigned long num_migr_units
= __le32_to_cpu(migr_rec
->num_migr_units
);
9106 int skipped_disks
= 0;
9108 err
= sysfs_get_str(info
, NULL
, "array_state", (char *)buffer
, 20);
9112 /* recover data only during assemblation */
9113 if (strncmp(buffer
, "inactive", 8) != 0)
9115 /* no data to recover */
9116 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
9118 if (curr_migr_unit
>= num_migr_units
)
9121 /* find device during reshape */
9122 for (id
= super
->devlist
; id
; id
= id
->next
)
9123 if (is_gen_migration(id
->dev
))
9128 map_dest
= get_imsm_map(id
->dev
, MAP_0
);
9129 new_disks
= map_dest
->num_members
;
9131 read_offset
= (unsigned long long)
9132 __le32_to_cpu(migr_rec
->ckpt_area_pba
) * 512;
9134 write_offset
= ((unsigned long long)
9135 __le32_to_cpu(migr_rec
->dest_1st_member_lba
) +
9136 __le32_to_cpu(map_dest
->pba_of_lba0
)) * 512;
9138 unit_len
= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
9139 if (posix_memalign((void **)&buf
, 512, unit_len
) != 0)
9141 targets
= malloc(new_disks
* sizeof(int));
9145 if (open_backup_targets(info
, new_disks
, targets
, super
, id
->dev
)) {
9147 Name
": Cannot open some devices belonging to array.\n");
9151 for (i
= 0; i
< new_disks
; i
++) {
9152 if (targets
[i
] < 0) {
9156 if (lseek64(targets
[i
], read_offset
, SEEK_SET
) < 0) {
9158 Name
": Cannot seek to block: %s\n",
9163 if ((unsigned)read(targets
[i
], buf
, unit_len
) != unit_len
) {
9165 Name
": Cannot read copy area block: %s\n",
9170 if (lseek64(targets
[i
], write_offset
, SEEK_SET
) < 0) {
9172 Name
": Cannot seek to block: %s\n",
9177 if ((unsigned)write(targets
[i
], buf
, unit_len
) != unit_len
) {
9179 Name
": Cannot restore block: %s\n",
9186 if (skipped_disks
> imsm_get_allowed_degradation(info
->new_level
,
9191 Name
": Cannot restore data from backup."
9192 " Too many failed disks\n");
9196 if (save_checkpoint_imsm(st
, info
, UNIT_SRC_NORMAL
)) {
9197 /* ignore error == 2, this can mean end of reshape here
9199 dprintf("imsm: Cannot write checkpoint to "
9200 "migration record (UNIT_SRC_NORMAL) during restart\n");
9206 for (i
= 0; i
< new_disks
; i
++)
9215 static char disk_by_path
[] = "/dev/disk/by-path/";
9217 static const char *imsm_get_disk_controller_domain(const char *path
)
9219 char disk_path
[PATH_MAX
];
9223 strncpy(disk_path
, disk_by_path
, PATH_MAX
- 1);
9224 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
9225 if (stat(disk_path
, &st
) == 0) {
9226 struct sys_dev
* hba
;
9229 path
= devt_to_devpath(st
.st_rdev
);
9232 hba
= find_disk_attached_hba(-1, path
);
9233 if (hba
&& hba
->type
== SYS_DEV_SAS
)
9235 else if (hba
&& hba
->type
== SYS_DEV_SATA
)
9239 dprintf("path: %s hba: %s attached: %s\n",
9240 path
, (hba
) ? hba
->path
: "NULL", drv
);
9248 static int imsm_find_array_minor_by_subdev(int subdev
, int container
, int *minor
)
9250 char subdev_name
[20];
9251 struct mdstat_ent
*mdstat
;
9253 sprintf(subdev_name
, "%d", subdev
);
9254 mdstat
= mdstat_by_subdev(subdev_name
, container
);
9258 *minor
= mdstat
->devnum
;
9259 free_mdstat(mdstat
);
9263 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
9264 struct geo_params
*geo
,
9265 int *old_raid_disks
)
9267 /* currently we only support increasing the number of devices
9268 * for a container. This increases the number of device for each
9269 * member array. They must all be RAID0 or RAID5.
9272 struct mdinfo
*info
, *member
;
9273 int devices_that_can_grow
= 0;
9275 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): "
9276 "st->devnum = (%i)\n",
9279 if (geo
->size
!= -1 ||
9280 geo
->level
!= UnSet
||
9281 geo
->layout
!= UnSet
||
9282 geo
->chunksize
!= 0 ||
9283 geo
->raid_disks
== UnSet
) {
9284 dprintf("imsm: Container operation is allowed for "
9285 "raid disks number change only.\n");
9289 info
= container_content_imsm(st
, NULL
);
9290 for (member
= info
; member
; member
= member
->next
) {
9294 dprintf("imsm: checking device_num: %i\n",
9295 member
->container_member
);
9297 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
9298 /* we work on container for Online Capacity Expansion
9299 * only so raid_disks has to grow
9301 dprintf("imsm: for container operation raid disks "
9302 "increase is required\n");
9306 if ((info
->array
.level
!= 0) &&
9307 (info
->array
.level
!= 5)) {
9308 /* we cannot use this container with other raid level
9310 dprintf("imsm: for container operation wrong"
9311 " raid level (%i) detected\n",
9315 /* check for platform support
9316 * for this raid level configuration
9318 struct intel_super
*super
= st
->sb
;
9319 if (!is_raid_level_supported(super
->orom
,
9320 member
->array
.level
,
9322 dprintf("platform does not support raid%d with"
9326 geo
->raid_disks
> 1 ? "s" : "");
9329 /* check if component size is aligned to chunk size
9331 if (info
->component_size
%
9332 (info
->array
.chunk_size
/512)) {
9333 dprintf("Component size is not aligned to "
9339 if (*old_raid_disks
&&
9340 info
->array
.raid_disks
!= *old_raid_disks
)
9342 *old_raid_disks
= info
->array
.raid_disks
;
9344 /* All raid5 and raid0 volumes in container
9345 * have to be ready for Online Capacity Expansion
9346 * so they need to be assembled. We have already
9347 * checked that no recovery etc is happening.
9349 result
= imsm_find_array_minor_by_subdev(member
->container_member
,
9353 dprintf("imsm: cannot find array\n");
9356 devices_that_can_grow
++;
9359 if (!member
&& devices_that_can_grow
)
9363 dprintf("\tContainer operation allowed\n");
9365 dprintf("\tError: %i\n", ret_val
);
9370 /* Function: get_spares_for_grow
9371 * Description: Allocates memory and creates list of spare devices
9372 * avaliable in container. Checks if spare drive size is acceptable.
9373 * Parameters: Pointer to the supertype structure
9374 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
9377 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
9379 unsigned long long min_size
= min_acceptable_spare_size_imsm(st
);
9380 return container_choose_spares(st
, min_size
, NULL
, NULL
, NULL
, 0);
9383 /******************************************************************************
9384 * function: imsm_create_metadata_update_for_reshape
9385 * Function creates update for whole IMSM container.
9387 ******************************************************************************/
9388 static int imsm_create_metadata_update_for_reshape(
9389 struct supertype
*st
,
9390 struct geo_params
*geo
,
9392 struct imsm_update_reshape
**updatep
)
9394 struct intel_super
*super
= st
->sb
;
9395 struct imsm_super
*mpb
= super
->anchor
;
9396 int update_memory_size
= 0;
9397 struct imsm_update_reshape
*u
= NULL
;
9398 struct mdinfo
*spares
= NULL
;
9400 int delta_disks
= 0;
9403 dprintf("imsm_update_metadata_for_reshape(enter) raid_disks = %i\n",
9406 delta_disks
= geo
->raid_disks
- old_raid_disks
;
9408 /* size of all update data without anchor */
9409 update_memory_size
= sizeof(struct imsm_update_reshape
);
9411 /* now add space for spare disks that we need to add. */
9412 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
9414 u
= calloc(1, update_memory_size
);
9417 "cannot get memory for imsm_update_reshape update\n");
9420 u
->type
= update_reshape_container_disks
;
9421 u
->old_raid_disks
= old_raid_disks
;
9422 u
->new_raid_disks
= geo
->raid_disks
;
9424 /* now get spare disks list
9426 spares
= get_spares_for_grow(st
);
9429 || delta_disks
> spares
->array
.spare_disks
) {
9430 fprintf(stderr
, Name
": imsm: ERROR: Cannot get spare devices "
9431 "for %s.\n", geo
->dev_name
);
9436 /* we have got spares
9437 * update disk list in imsm_disk list table in anchor
9439 dprintf("imsm: %i spares are available.\n\n",
9440 spares
->array
.spare_disks
);
9443 for (i
= 0; i
< delta_disks
; i
++) {
9448 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
9450 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
9451 dl
->index
= mpb
->num_disks
;
9461 dprintf("imsm: reshape update preparation :");
9462 if (i
== delta_disks
) {
9465 return update_memory_size
;
9468 dprintf(" Error\n");
9473 /******************************************************************************
9474 * function: imsm_create_metadata_update_for_migration()
9475 * Creates update for IMSM array.
9477 ******************************************************************************/
9478 static int imsm_create_metadata_update_for_migration(
9479 struct supertype
*st
,
9480 struct geo_params
*geo
,
9481 struct imsm_update_reshape_migration
**updatep
)
9483 struct intel_super
*super
= st
->sb
;
9484 int update_memory_size
= 0;
9485 struct imsm_update_reshape_migration
*u
= NULL
;
9486 struct imsm_dev
*dev
;
9487 int previous_level
= -1;
9489 dprintf("imsm_create_metadata_update_for_migration(enter)"
9490 " New Level = %i\n", geo
->level
);
9492 /* size of all update data without anchor */
9493 update_memory_size
= sizeof(struct imsm_update_reshape_migration
);
9495 u
= calloc(1, update_memory_size
);
9497 dprintf("error: cannot get memory for "
9498 "imsm_create_metadata_update_for_migration\n");
9501 u
->type
= update_reshape_migration
;
9502 u
->subdev
= super
->current_vol
;
9503 u
->new_level
= geo
->level
;
9504 u
->new_layout
= geo
->layout
;
9505 u
->new_raid_disks
= u
->old_raid_disks
= geo
->raid_disks
;
9506 u
->new_disks
[0] = -1;
9507 u
->new_chunksize
= -1;
9509 dev
= get_imsm_dev(super
, u
->subdev
);
9511 struct imsm_map
*map
;
9513 map
= get_imsm_map(dev
, MAP_0
);
9515 int current_chunk_size
=
9516 __le16_to_cpu(map
->blocks_per_strip
) / 2;
9518 if (geo
->chunksize
!= current_chunk_size
) {
9519 u
->new_chunksize
= geo
->chunksize
/ 1024;
9521 "chunk size change from %i to %i\n",
9522 current_chunk_size
, u
->new_chunksize
);
9524 previous_level
= map
->raid_level
;
9527 if ((geo
->level
== 5) && (previous_level
== 0)) {
9528 struct mdinfo
*spares
= NULL
;
9530 u
->new_raid_disks
++;
9531 spares
= get_spares_for_grow(st
);
9532 if ((spares
== NULL
) || (spares
->array
.spare_disks
< 1)) {
9535 update_memory_size
= 0;
9536 dprintf("error: cannot get spare device "
9537 "for requested migration");
9542 dprintf("imsm: reshape update preparation : OK\n");
9545 return update_memory_size
;
9548 static void imsm_update_metadata_locally(struct supertype
*st
,
9551 struct metadata_update mu
;
9556 mu
.space_list
= NULL
;
9558 imsm_prepare_update(st
, &mu
);
9559 imsm_process_update(st
, &mu
);
9561 while (mu
.space_list
) {
9562 void **space
= mu
.space_list
;
9563 mu
.space_list
= *space
;
9568 /***************************************************************************
9569 * Function: imsm_analyze_change
9570 * Description: Function analyze change for single volume
9571 * and validate if transition is supported
9572 * Parameters: Geometry parameters, supertype structure
9573 * Returns: Operation type code on success, -1 if fail
9574 ****************************************************************************/
9575 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
9576 struct geo_params
*geo
)
9582 /* number of added/removed disks in operation result */
9583 int devNumChange
= 0;
9584 /* imsm compatible layout value for array geometry verification */
9585 int imsm_layout
= -1;
9587 getinfo_super_imsm_volume(st
, &info
, NULL
);
9588 if ((geo
->level
!= info
.array
.level
) &&
9589 (geo
->level
>= 0) &&
9590 (geo
->level
!= UnSet
)) {
9591 switch (info
.array
.level
) {
9593 if (geo
->level
== 5) {
9594 change
= CH_MIGRATION
;
9595 if (geo
->layout
!= ALGORITHM_LEFT_ASYMMETRIC
) {
9597 Name
" Error. Requested Layout "
9598 "not supported (left-asymmetric layout "
9599 "is supported only)!\n");
9601 goto analyse_change_exit
;
9603 imsm_layout
= geo
->layout
;
9605 devNumChange
= 1; /* parity disk added */
9606 } else if (geo
->level
== 10) {
9607 change
= CH_TAKEOVER
;
9609 devNumChange
= 2; /* two mirrors added */
9610 imsm_layout
= 0x102; /* imsm supported layout */
9615 if (geo
->level
== 0) {
9616 change
= CH_TAKEOVER
;
9618 devNumChange
= -(geo
->raid_disks
/2);
9619 imsm_layout
= 0; /* imsm raid0 layout */
9625 Name
" Error. Level Migration from %d to %d "
9627 info
.array
.level
, geo
->level
);
9628 goto analyse_change_exit
;
9631 geo
->level
= info
.array
.level
;
9633 if ((geo
->layout
!= info
.array
.layout
)
9634 && ((geo
->layout
!= UnSet
) && (geo
->layout
!= -1))) {
9635 change
= CH_MIGRATION
;
9636 if ((info
.array
.layout
== 0)
9637 && (info
.array
.level
== 5)
9638 && (geo
->layout
== 5)) {
9639 /* reshape 5 -> 4 */
9640 } else if ((info
.array
.layout
== 5)
9641 && (info
.array
.level
== 5)
9642 && (geo
->layout
== 0)) {
9643 /* reshape 4 -> 5 */
9648 Name
" Error. Layout Migration from %d to %d "
9650 info
.array
.layout
, geo
->layout
);
9652 goto analyse_change_exit
;
9655 geo
->layout
= info
.array
.layout
;
9656 if (imsm_layout
== -1)
9657 imsm_layout
= info
.array
.layout
;
9660 if ((geo
->chunksize
> 0) && (geo
->chunksize
!= UnSet
)
9661 && (geo
->chunksize
!= info
.array
.chunk_size
))
9662 change
= CH_MIGRATION
;
9664 geo
->chunksize
= info
.array
.chunk_size
;
9666 chunk
= geo
->chunksize
/ 1024;
9667 if (!validate_geometry_imsm(st
,
9670 geo
->raid_disks
+ devNumChange
,
9677 struct intel_super
*super
= st
->sb
;
9678 struct imsm_super
*mpb
= super
->anchor
;
9680 if (mpb
->num_raid_devs
> 1) {
9682 Name
" Error. Cannot perform operation on %s"
9683 "- for this operation it MUST be single "
9684 "array in container\n",
9690 analyse_change_exit
:
9695 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
9697 struct intel_super
*super
= st
->sb
;
9698 struct imsm_update_takeover
*u
;
9700 u
= malloc(sizeof(struct imsm_update_takeover
));
9704 u
->type
= update_takeover
;
9705 u
->subarray
= super
->current_vol
;
9707 /* 10->0 transition */
9708 if (geo
->level
== 0)
9709 u
->direction
= R10_TO_R0
;
9711 /* 0->10 transition */
9712 if (geo
->level
== 10)
9713 u
->direction
= R0_TO_R10
;
9715 /* update metadata locally */
9716 imsm_update_metadata_locally(st
, u
,
9717 sizeof(struct imsm_update_takeover
));
9718 /* and possibly remotely */
9719 if (st
->update_tail
)
9720 append_metadata_update(st
, u
,
9721 sizeof(struct imsm_update_takeover
));
9728 static int imsm_reshape_super(struct supertype
*st
, long long size
, int level
,
9729 int layout
, int chunksize
, int raid_disks
,
9730 int delta_disks
, char *backup
, char *dev
,
9734 struct geo_params geo
;
9736 dprintf("imsm: reshape_super called.\n");
9738 memset(&geo
, 0, sizeof(struct geo_params
));
9741 geo
.dev_id
= st
->devnum
;
9744 geo
.layout
= layout
;
9745 geo
.chunksize
= chunksize
;
9746 geo
.raid_disks
= raid_disks
;
9747 if (delta_disks
!= UnSet
)
9748 geo
.raid_disks
+= delta_disks
;
9750 dprintf("\tfor level : %i\n", geo
.level
);
9751 dprintf("\tfor raid_disks : %i\n", geo
.raid_disks
);
9753 if (experimental() == 0)
9756 if (st
->container_dev
== st
->devnum
) {
9757 /* On container level we can only increase number of devices. */
9758 dprintf("imsm: info: Container operation\n");
9759 int old_raid_disks
= 0;
9761 if (imsm_reshape_is_allowed_on_container(
9762 st
, &geo
, &old_raid_disks
)) {
9763 struct imsm_update_reshape
*u
= NULL
;
9766 len
= imsm_create_metadata_update_for_reshape(
9767 st
, &geo
, old_raid_disks
, &u
);
9770 dprintf("imsm: Cannot prepare update\n");
9771 goto exit_imsm_reshape_super
;
9775 /* update metadata locally */
9776 imsm_update_metadata_locally(st
, u
, len
);
9777 /* and possibly remotely */
9778 if (st
->update_tail
)
9779 append_metadata_update(st
, u
, len
);
9784 fprintf(stderr
, Name
": (imsm) Operation "
9785 "is not allowed on this container\n");
9788 /* On volume level we support following operations
9789 * - takeover: raid10 -> raid0; raid0 -> raid10
9790 * - chunk size migration
9791 * - migration: raid5 -> raid0; raid0 -> raid5
9793 struct intel_super
*super
= st
->sb
;
9794 struct intel_dev
*dev
= super
->devlist
;
9796 dprintf("imsm: info: Volume operation\n");
9797 /* find requested device */
9799 if (imsm_find_array_minor_by_subdev(
9800 dev
->index
, st
->container_dev
, &devnum
) == 0
9801 && devnum
== geo
.dev_id
)
9806 fprintf(stderr
, Name
" Cannot find %s (%i) subarray\n",
9807 geo
.dev_name
, geo
.dev_id
);
9808 goto exit_imsm_reshape_super
;
9810 super
->current_vol
= dev
->index
;
9811 change
= imsm_analyze_change(st
, &geo
);
9814 ret_val
= imsm_takeover(st
, &geo
);
9816 case CH_MIGRATION
: {
9817 struct imsm_update_reshape_migration
*u
= NULL
;
9819 imsm_create_metadata_update_for_migration(
9823 "Cannot prepare update\n");
9827 /* update metadata locally */
9828 imsm_update_metadata_locally(st
, u
, len
);
9829 /* and possibly remotely */
9830 if (st
->update_tail
)
9831 append_metadata_update(st
, u
, len
);
9841 exit_imsm_reshape_super
:
9842 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
9846 /*******************************************************************************
9847 * Function: wait_for_reshape_imsm
9848 * Description: Function writes new sync_max value and waits until
9849 * reshape process reach new position
9851 * sra : general array info
9852 * ndata : number of disks in new array's layout
9855 * 1 : there is no reshape in progress,
9857 ******************************************************************************/
9858 int wait_for_reshape_imsm(struct mdinfo
*sra
, int ndata
)
9860 int fd
= sysfs_get_fd(sra
, NULL
, "reshape_position");
9861 unsigned long long completed
;
9862 /* to_complete : new sync_max position */
9863 unsigned long long to_complete
= sra
->reshape_progress
;
9864 unsigned long long position_to_set
= to_complete
/ ndata
;
9867 dprintf("imsm: wait_for_reshape_imsm() "
9868 "cannot open reshape_position\n");
9872 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
9873 dprintf("imsm: wait_for_reshape_imsm() "
9874 "cannot read reshape_position (no reshape in progres)\n");
9879 if (completed
> to_complete
) {
9880 dprintf("imsm: wait_for_reshape_imsm() "
9881 "wrong next position to set %llu (%llu)\n",
9882 to_complete
, completed
);
9886 dprintf("Position set: %llu\n", position_to_set
);
9887 if (sysfs_set_num(sra
, NULL
, "sync_max",
9888 position_to_set
) != 0) {
9889 dprintf("imsm: wait_for_reshape_imsm() "
9890 "cannot set reshape position to %llu\n",
9901 select(fd
+1, &rfds
, NULL
, NULL
, NULL
);
9902 if (sysfs_get_str(sra
, NULL
, "sync_action",
9904 strncmp(action
, "reshape", 7) != 0)
9906 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
9907 dprintf("imsm: wait_for_reshape_imsm() "
9908 "cannot read reshape_position (in loop)\n");
9912 } while (completed
< to_complete
);
9918 /*******************************************************************************
9919 * Function: check_degradation_change
9920 * Description: Check that array hasn't become failed.
9922 * info : for sysfs access
9923 * sources : source disks descriptors
9924 * degraded: previous degradation level
9927 ******************************************************************************/
9928 int check_degradation_change(struct mdinfo
*info
,
9932 unsigned long long new_degraded
;
9933 sysfs_get_ll(info
, NULL
, "degraded", &new_degraded
);
9934 if (new_degraded
!= (unsigned long long)degraded
) {
9935 /* check each device to ensure it is still working */
9938 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
9939 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
9941 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
9943 if (sysfs_get_str(info
,
9944 sd
, "state", sbuf
, 20) < 0 ||
9945 strstr(sbuf
, "faulty") ||
9946 strstr(sbuf
, "in_sync") == NULL
) {
9947 /* this device is dead */
9948 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
9949 if (sd
->disk
.raid_disk
>= 0 &&
9950 sources
[sd
->disk
.raid_disk
] >= 0) {
9952 sd
->disk
.raid_disk
]);
9953 sources
[sd
->disk
.raid_disk
] =
9962 return new_degraded
;
9965 /*******************************************************************************
9966 * Function: imsm_manage_reshape
9967 * Description: Function finds array under reshape and it manages reshape
9968 * process. It creates stripes backups (if required) and sets
9971 * afd : Backup handle (nattive) - not used
9972 * sra : general array info
9973 * reshape : reshape parameters - not used
9974 * st : supertype structure
9975 * blocks : size of critical section [blocks]
9976 * fds : table of source device descriptor
9977 * offsets : start of array (offest per devices)
9979 * destfd : table of destination device descriptor
9980 * destoffsets : table of destination offsets (per device)
9982 * 1 : success, reshape is done
9984 ******************************************************************************/
9985 static int imsm_manage_reshape(
9986 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
9987 struct supertype
*st
, unsigned long backup_blocks
,
9988 int *fds
, unsigned long long *offsets
,
9989 int dests
, int *destfd
, unsigned long long *destoffsets
)
9992 struct intel_super
*super
= st
->sb
;
9993 struct intel_dev
*dv
= NULL
;
9994 struct imsm_dev
*dev
= NULL
;
9995 struct imsm_map
*map_src
;
9996 int migr_vol_qan
= 0;
9997 int ndata
, odata
; /* [bytes] */
9998 int chunk
; /* [bytes] */
9999 struct migr_record
*migr_rec
;
10001 unsigned int buf_size
; /* [bytes] */
10002 unsigned long long max_position
; /* array size [bytes] */
10003 unsigned long long next_step
; /* [blocks]/[bytes] */
10004 unsigned long long old_data_stripe_length
;
10005 unsigned long long start_src
; /* [bytes] */
10006 unsigned long long start
; /* [bytes] */
10007 unsigned long long start_buf_shift
; /* [bytes] */
10009 int source_layout
= 0;
10011 if (!fds
|| !offsets
|| !sra
)
10014 /* Find volume during the reshape */
10015 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
10016 if (dv
->dev
->vol
.migr_type
== MIGR_GEN_MIGR
10017 && dv
->dev
->vol
.migr_state
== 1) {
10022 /* Only one volume can migrate at the same time */
10023 if (migr_vol_qan
!= 1) {
10024 fprintf(stderr
, Name
" : %s", migr_vol_qan
?
10025 "Number of migrating volumes greater than 1\n" :
10026 "There is no volume during migrationg\n");
10030 map_src
= get_imsm_map(dev
, MAP_1
);
10031 if (map_src
== NULL
)
10034 ndata
= imsm_num_data_members(dev
, MAP_0
);
10035 odata
= imsm_num_data_members(dev
, MAP_1
);
10037 chunk
= __le16_to_cpu(map_src
->blocks_per_strip
) * 512;
10038 old_data_stripe_length
= odata
* chunk
;
10040 migr_rec
= super
->migr_rec
;
10042 /* initialize migration record for start condition */
10043 if (sra
->reshape_progress
== 0)
10044 init_migr_record_imsm(st
, dev
, sra
);
10046 if (__le32_to_cpu(migr_rec
->rec_status
) != UNIT_SRC_NORMAL
) {
10047 dprintf("imsm: cannot restart migration when data "
10048 "are present in copy area.\n");
10053 /* size for data */
10054 buf_size
= __le32_to_cpu(migr_rec
->blocks_per_unit
) * 512;
10055 /* extend buffer size for parity disk */
10056 buf_size
+= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
10057 /* add space for stripe aligment */
10058 buf_size
+= old_data_stripe_length
;
10059 if (posix_memalign((void **)&buf
, 4096, buf_size
)) {
10060 dprintf("imsm: Cannot allocate checpoint buffer\n");
10064 max_position
= sra
->component_size
* ndata
;
10065 source_layout
= imsm_level_to_layout(map_src
->raid_level
);
10067 while (__le32_to_cpu(migr_rec
->curr_migr_unit
) <
10068 __le32_to_cpu(migr_rec
->num_migr_units
)) {
10069 /* current reshape position [blocks] */
10070 unsigned long long current_position
=
10071 __le32_to_cpu(migr_rec
->blocks_per_unit
)
10072 * __le32_to_cpu(migr_rec
->curr_migr_unit
);
10073 unsigned long long border
;
10075 /* Check that array hasn't become failed.
10077 degraded
= check_degradation_change(sra
, fds
, degraded
);
10078 if (degraded
> 1) {
10079 dprintf("imsm: Abort reshape due to degradation"
10080 " level (%i)\n", degraded
);
10084 next_step
= __le32_to_cpu(migr_rec
->blocks_per_unit
);
10086 if ((current_position
+ next_step
) > max_position
)
10087 next_step
= max_position
- current_position
;
10089 start
= current_position
* 512;
10091 /* allign reading start to old geometry */
10092 start_buf_shift
= start
% old_data_stripe_length
;
10093 start_src
= start
- start_buf_shift
;
10095 border
= (start_src
/ odata
) - (start
/ ndata
);
10097 if (border
<= __le32_to_cpu(migr_rec
->dest_depth_per_unit
)) {
10098 /* save critical stripes to buf
10099 * start - start address of current unit
10100 * to backup [bytes]
10101 * start_src - start address of current unit
10102 * to backup alligned to source array
10105 unsigned long long next_step_filler
= 0;
10106 unsigned long long copy_length
= next_step
* 512;
10108 /* allign copy area length to stripe in old geometry */
10109 next_step_filler
= ((copy_length
+ start_buf_shift
)
10110 % old_data_stripe_length
);
10111 if (next_step_filler
)
10112 next_step_filler
= (old_data_stripe_length
10113 - next_step_filler
);
10114 dprintf("save_stripes() parameters: start = %llu,"
10115 "\tstart_src = %llu,\tnext_step*512 = %llu,"
10116 "\tstart_in_buf_shift = %llu,"
10117 "\tnext_step_filler = %llu\n",
10118 start
, start_src
, copy_length
,
10119 start_buf_shift
, next_step_filler
);
10121 if (save_stripes(fds
, offsets
, map_src
->num_members
,
10122 chunk
, map_src
->raid_level
,
10123 source_layout
, 0, NULL
, start_src
,
10125 next_step_filler
+ start_buf_shift
,
10127 dprintf("imsm: Cannot save stripes"
10131 /* Convert data to destination format and store it
10132 * in backup general migration area
10134 if (save_backup_imsm(st
, dev
, sra
,
10135 buf
+ start_buf_shift
, copy_length
)) {
10136 dprintf("imsm: Cannot save stripes to "
10137 "target devices\n");
10140 if (save_checkpoint_imsm(st
, sra
,
10141 UNIT_SRC_IN_CP_AREA
)) {
10142 dprintf("imsm: Cannot write checkpoint to "
10143 "migration record (UNIT_SRC_IN_CP_AREA)\n");
10147 /* set next step to use whole border area */
10148 border
/= next_step
;
10150 next_step
*= border
;
10152 /* When data backed up, checkpoint stored,
10153 * kick the kernel to reshape unit of data
10155 next_step
= next_step
+ sra
->reshape_progress
;
10156 /* limit next step to array max position */
10157 if (next_step
> max_position
)
10158 next_step
= max_position
;
10159 sysfs_set_num(sra
, NULL
, "suspend_lo", sra
->reshape_progress
);
10160 sysfs_set_num(sra
, NULL
, "suspend_hi", next_step
);
10161 sra
->reshape_progress
= next_step
;
10163 /* wait until reshape finish */
10164 if (wait_for_reshape_imsm(sra
, ndata
) < 0) {
10165 dprintf("wait_for_reshape_imsm returned error!\n");
10169 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
10170 /* ignore error == 2, this can mean end of reshape here
10172 dprintf("imsm: Cannot write checkpoint to "
10173 "migration record (UNIT_SRC_NORMAL)\n");
10179 /* return '1' if done */
10183 abort_reshape(sra
);
10187 #endif /* MDASSEMBLE */
10189 struct superswitch super_imsm
= {
10191 .examine_super
= examine_super_imsm
,
10192 .brief_examine_super
= brief_examine_super_imsm
,
10193 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
10194 .export_examine_super
= export_examine_super_imsm
,
10195 .detail_super
= detail_super_imsm
,
10196 .brief_detail_super
= brief_detail_super_imsm
,
10197 .write_init_super
= write_init_super_imsm
,
10198 .validate_geometry
= validate_geometry_imsm
,
10199 .add_to_super
= add_to_super_imsm
,
10200 .remove_from_super
= remove_from_super_imsm
,
10201 .detail_platform
= detail_platform_imsm
,
10202 .kill_subarray
= kill_subarray_imsm
,
10203 .update_subarray
= update_subarray_imsm
,
10204 .load_container
= load_container_imsm
,
10205 .default_geometry
= default_geometry_imsm
,
10206 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
10207 .reshape_super
= imsm_reshape_super
,
10208 .manage_reshape
= imsm_manage_reshape
,
10209 .recover_backup
= recover_backup_imsm
,
10211 .match_home
= match_home_imsm
,
10212 .uuid_from_super
= uuid_from_super_imsm
,
10213 .getinfo_super
= getinfo_super_imsm
,
10214 .getinfo_super_disks
= getinfo_super_disks_imsm
,
10215 .update_super
= update_super_imsm
,
10217 .avail_size
= avail_size_imsm
,
10218 .min_acceptable_spare_size
= min_acceptable_spare_size_imsm
,
10220 .compare_super
= compare_super_imsm
,
10222 .load_super
= load_super_imsm
,
10223 .init_super
= init_super_imsm
,
10224 .store_super
= store_super_imsm
,
10225 .free_super
= free_super_imsm
,
10226 .match_metadata_desc
= match_metadata_desc_imsm
,
10227 .container_content
= container_content_imsm
,
10235 .open_new
= imsm_open_new
,
10236 .set_array_state
= imsm_set_array_state
,
10237 .set_disk
= imsm_set_disk
,
10238 .sync_metadata
= imsm_sync_metadata
,
10239 .activate_spare
= imsm_activate_spare
,
10240 .process_update
= imsm_process_update
,
10241 .prepare_update
= imsm_prepare_update
,
10242 #endif /* MDASSEMBLE */