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_lo
; /* 0xE8 - 0xEB total blocks lo */
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 __u32 total_blocks_hi
; /* 0xF4 - 0xF5 total blocks hi */
106 #define IMSM_DISK_FILLERS 3
107 __u32 filler
[IMSM_DISK_FILLERS
]; /* 0xF5 - 0x107 MPB_DISK_FILLERS for future expansion */
110 /* map selector for map managment
116 /* RAID map configuration infos. */
118 __u32 pba_of_lba0_lo
; /* start address of partition */
119 __u32 blocks_per_member_lo
;/* blocks per member */
120 __u32 num_data_stripes_lo
; /* number of data stripes */
121 __u16 blocks_per_strip
;
122 __u8 map_state
; /* Normal, Uninitialized, Degraded, Failed */
123 #define IMSM_T_STATE_NORMAL 0
124 #define IMSM_T_STATE_UNINITIALIZED 1
125 #define IMSM_T_STATE_DEGRADED 2
126 #define IMSM_T_STATE_FAILED 3
128 #define IMSM_T_RAID0 0
129 #define IMSM_T_RAID1 1
130 #define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */
131 __u8 num_members
; /* number of member disks */
132 __u8 num_domains
; /* number of parity domains */
133 __u8 failed_disk_num
; /* valid only when state is degraded */
135 __u32 pba_of_lba0_hi
;
136 __u32 blocks_per_member_hi
;
137 __u32 num_data_stripes_hi
;
138 __u32 filler
[4]; /* expansion area */
139 #define IMSM_ORD_REBUILD (1 << 24)
140 __u32 disk_ord_tbl
[1]; /* disk_ord_tbl[num_members],
141 * top byte contains some flags
143 } __attribute__ ((packed
));
146 __u32 curr_migr_unit
;
147 __u32 checkpoint_id
; /* id to access curr_migr_unit */
148 __u8 migr_state
; /* Normal or Migrating */
150 #define MIGR_REBUILD 1
151 #define MIGR_VERIFY 2 /* analagous to echo check > sync_action */
152 #define MIGR_GEN_MIGR 3
153 #define MIGR_STATE_CHANGE 4
154 #define MIGR_REPAIR 5
155 __u8 migr_type
; /* Initializing, Rebuilding, ... */
157 __u8 fs_state
; /* fast-sync state for CnG (0xff == disabled) */
158 __u16 verify_errors
; /* number of mismatches */
159 __u16 bad_blocks
; /* number of bad blocks during verify */
161 struct imsm_map map
[1];
162 /* here comes another one if migr_state */
163 } __attribute__ ((packed
));
166 __u8 volume
[MAX_RAID_SERIAL_LEN
];
169 #define DEV_BOOTABLE __cpu_to_le32(0x01)
170 #define DEV_BOOT_DEVICE __cpu_to_le32(0x02)
171 #define DEV_READ_COALESCING __cpu_to_le32(0x04)
172 #define DEV_WRITE_COALESCING __cpu_to_le32(0x08)
173 #define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10)
174 #define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20)
175 #define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40)
176 #define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80)
177 #define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100)
178 #define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200)
179 #define DEV_CLONE_N_GO __cpu_to_le32(0x400)
180 #define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800)
181 #define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000)
182 __u32 status
; /* Persistent RaidDev status */
183 __u32 reserved_blocks
; /* Reserved blocks at beginning of volume */
187 __u8 cng_master_disk
;
191 #define IMSM_DEV_FILLERS 10
192 __u32 filler
[IMSM_DEV_FILLERS
];
194 } __attribute__ ((packed
));
197 __u8 sig
[MAX_SIGNATURE_LENGTH
]; /* 0x00 - 0x1F */
198 __u32 check_sum
; /* 0x20 - 0x23 MPB Checksum */
199 __u32 mpb_size
; /* 0x24 - 0x27 Size of MPB */
200 __u32 family_num
; /* 0x28 - 0x2B Checksum from first time this config was written */
201 __u32 generation_num
; /* 0x2C - 0x2F Incremented each time this array's MPB is written */
202 __u32 error_log_size
; /* 0x30 - 0x33 in bytes */
203 __u32 attributes
; /* 0x34 - 0x37 */
204 __u8 num_disks
; /* 0x38 Number of configured disks */
205 __u8 num_raid_devs
; /* 0x39 Number of configured volumes */
206 __u8 error_log_pos
; /* 0x3A */
207 __u8 fill
[1]; /* 0x3B */
208 __u32 cache_size
; /* 0x3c - 0x40 in mb */
209 __u32 orig_family_num
; /* 0x40 - 0x43 original family num */
210 __u32 pwr_cycle_count
; /* 0x44 - 0x47 simulated power cycle count for array */
211 __u32 bbm_log_size
; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */
212 #define IMSM_FILLERS 35
213 __u32 filler
[IMSM_FILLERS
]; /* 0x4C - 0xD7 RAID_MPB_FILLERS */
214 struct imsm_disk disk
[1]; /* 0xD8 diskTbl[numDisks] */
215 /* here comes imsm_dev[num_raid_devs] */
216 /* here comes BBM logs */
217 } __attribute__ ((packed
));
219 #define BBM_LOG_MAX_ENTRIES 254
221 struct bbm_log_entry
{
222 __u64 defective_block_start
;
223 #define UNREADABLE 0xFFFFFFFF
224 __u32 spare_block_offset
;
225 __u16 remapped_marked_count
;
227 } __attribute__ ((__packed__
));
230 __u32 signature
; /* 0xABADB10C */
232 __u32 reserved_spare_block_count
; /* 0 */
233 __u32 reserved
; /* 0xFFFF */
234 __u64 first_spare_lba
;
235 struct bbm_log_entry mapped_block_entries
[BBM_LOG_MAX_ENTRIES
];
236 } __attribute__ ((__packed__
));
239 static char *map_state_str
[] = { "normal", "uninitialized", "degraded", "failed" };
242 #define RAID_DISK_RESERVED_BLOCKS_IMSM_HI 2209
244 #define GEN_MIGR_AREA_SIZE 2048 /* General Migration Copy Area size in blocks */
246 #define MIGR_REC_BUF_SIZE 512 /* size of migr_record i/o buffer */
247 #define MIGR_REC_POSITION 512 /* migr_record position offset on disk,
248 * MIGR_REC_BUF_SIZE <= MIGR_REC_POSITION
251 #define UNIT_SRC_NORMAL 0 /* Source data for curr_migr_unit must
252 * be recovered using srcMap */
253 #define UNIT_SRC_IN_CP_AREA 1 /* Source data for curr_migr_unit has
254 * already been migrated and must
255 * be recovered from checkpoint area */
257 __u32 rec_status
; /* Status used to determine how to restart
258 * migration in case it aborts
260 __u32 curr_migr_unit
; /* 0..numMigrUnits-1 */
261 __u32 family_num
; /* Family number of MPB
262 * containing the RaidDev
263 * that is migrating */
264 __u32 ascending_migr
; /* True if migrating in increasing
266 __u32 blocks_per_unit
; /* Num disk blocks per unit of operation */
267 __u32 dest_depth_per_unit
; /* Num member blocks each destMap
269 * advances per unit-of-operation */
270 __u32 ckpt_area_pba
; /* Pba of first block of ckpt copy area */
271 __u32 dest_1st_member_lba
; /* First member lba on first
272 * stripe of destination */
273 __u32 num_migr_units
; /* Total num migration units-of-op */
274 __u32 post_migr_vol_cap
; /* Size of volume after
275 * migration completes */
276 __u32 post_migr_vol_cap_hi
; /* Expansion space for LBA64 */
277 __u32 ckpt_read_disk_num
; /* Which member disk in destSubMap[0] the
278 * migration ckpt record was read from
279 * (for recovered migrations) */
280 } __attribute__ ((__packed__
));
285 * 2: metadata does not match
293 struct md_list
*next
;
296 #define pr_vrb(fmt, arg...) (void) (verbose && pr_err(fmt, ##arg))
298 static __u8
migr_type(struct imsm_dev
*dev
)
300 if (dev
->vol
.migr_type
== MIGR_VERIFY
&&
301 dev
->status
& DEV_VERIFY_AND_FIX
)
304 return dev
->vol
.migr_type
;
307 static void set_migr_type(struct imsm_dev
*dev
, __u8 migr_type
)
309 /* for compatibility with older oroms convert MIGR_REPAIR, into
310 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
312 if (migr_type
== MIGR_REPAIR
) {
313 dev
->vol
.migr_type
= MIGR_VERIFY
;
314 dev
->status
|= DEV_VERIFY_AND_FIX
;
316 dev
->vol
.migr_type
= migr_type
;
317 dev
->status
&= ~DEV_VERIFY_AND_FIX
;
321 static unsigned int sector_count(__u32 bytes
)
323 return ROUND_UP(bytes
, 512) / 512;
326 static unsigned int mpb_sectors(struct imsm_super
*mpb
)
328 return sector_count(__le32_to_cpu(mpb
->mpb_size
));
332 struct imsm_dev
*dev
;
333 struct intel_dev
*next
;
338 enum sys_dev_type type
;
341 struct intel_hba
*next
;
348 /* internal representation of IMSM metadata */
351 void *buf
; /* O_DIRECT buffer for reading/writing metadata */
352 struct imsm_super
*anchor
; /* immovable parameters */
355 void *migr_rec_buf
; /* buffer for I/O operations */
356 struct migr_record
*migr_rec
; /* migration record */
358 int clean_migration_record_by_mdmon
; /* when reshape is switched to next
359 array, it indicates that mdmon is allowed to clean migration
361 size_t len
; /* size of the 'buf' allocation */
362 void *next_buf
; /* for realloc'ing buf from the manager */
364 int updates_pending
; /* count of pending updates for mdmon */
365 int current_vol
; /* index of raid device undergoing creation */
366 unsigned long long create_offset
; /* common start for 'current_vol' */
367 __u32 random
; /* random data for seeding new family numbers */
368 struct intel_dev
*devlist
;
372 __u8 serial
[MAX_RAID_SERIAL_LEN
];
375 struct imsm_disk disk
;
378 struct extent
*e
; /* for determining freespace @ create */
379 int raiddisk
; /* slot to fill in autolayout */
381 } *disks
, *current_disk
;
382 struct dl
*disk_mgmt_list
; /* list of disks to add/remove while mdmon
384 struct dl
*missing
; /* disks removed while we weren't looking */
385 struct bbm_log
*bbm_log
;
386 struct intel_hba
*hba
; /* device path of the raid controller for this metadata */
387 const struct imsm_orom
*orom
; /* platform firmware support */
388 struct intel_super
*next
; /* (temp) list for disambiguating family_num */
392 struct imsm_disk disk
;
393 #define IMSM_UNKNOWN_OWNER (-1)
395 struct intel_disk
*next
;
399 unsigned long long start
, size
;
402 /* definitions of reshape process types */
403 enum imsm_reshape_type
{
409 /* definition of messages passed to imsm_process_update */
410 enum imsm_update_type
{
411 update_activate_spare
,
415 update_add_remove_disk
,
416 update_reshape_container_disks
,
417 update_reshape_migration
,
419 update_general_migration_checkpoint
,
423 struct imsm_update_activate_spare
{
424 enum imsm_update_type type
;
428 struct imsm_update_activate_spare
*next
;
434 unsigned long long size
;
441 enum takeover_direction
{
445 struct imsm_update_takeover
{
446 enum imsm_update_type type
;
448 enum takeover_direction direction
;
451 struct imsm_update_reshape
{
452 enum imsm_update_type type
;
456 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
459 struct imsm_update_reshape_migration
{
460 enum imsm_update_type type
;
463 /* fields for array migration changes
470 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
473 struct imsm_update_size_change
{
474 enum imsm_update_type type
;
479 struct imsm_update_general_migration_checkpoint
{
480 enum imsm_update_type type
;
481 __u32 curr_migr_unit
;
485 __u8 serial
[MAX_RAID_SERIAL_LEN
];
488 struct imsm_update_create_array
{
489 enum imsm_update_type type
;
494 struct imsm_update_kill_array
{
495 enum imsm_update_type type
;
499 struct imsm_update_rename_array
{
500 enum imsm_update_type type
;
501 __u8 name
[MAX_RAID_SERIAL_LEN
];
505 struct imsm_update_add_remove_disk
{
506 enum imsm_update_type type
;
509 static const char *_sys_dev_type
[] = {
510 [SYS_DEV_UNKNOWN
] = "Unknown",
511 [SYS_DEV_SAS
] = "SAS",
512 [SYS_DEV_SATA
] = "SATA"
515 const char *get_sys_dev_type(enum sys_dev_type type
)
517 if (type
>= SYS_DEV_MAX
)
518 type
= SYS_DEV_UNKNOWN
;
520 return _sys_dev_type
[type
];
523 static struct intel_hba
* alloc_intel_hba(struct sys_dev
*device
)
525 struct intel_hba
*result
= xmalloc(sizeof(*result
));
527 result
->type
= device
->type
;
528 result
->path
= xstrdup(device
->path
);
530 if (result
->path
&& (result
->pci_id
= strrchr(result
->path
, '/')) != NULL
)
536 static struct intel_hba
* find_intel_hba(struct intel_hba
*hba
, struct sys_dev
*device
)
538 struct intel_hba
*result
=NULL
;
539 for (result
= hba
; result
; result
= result
->next
) {
540 if (result
->type
== device
->type
&& strcmp(result
->path
, device
->path
) == 0)
546 static int attach_hba_to_super(struct intel_super
*super
, struct sys_dev
*device
)
548 struct intel_hba
*hba
;
550 /* check if disk attached to Intel HBA */
551 hba
= find_intel_hba(super
->hba
, device
);
554 /* Check if HBA is already attached to super */
555 if (super
->hba
== NULL
) {
556 super
->hba
= alloc_intel_hba(device
);
559 /* IMSM metadata disallows to attach disks to multiple
565 static struct sys_dev
* find_disk_attached_hba(int fd
, const char *devname
)
567 struct sys_dev
*list
, *elem
;
570 if ((list
= find_intel_devices()) == NULL
)
574 disk_path
= (char *) devname
;
576 disk_path
= diskfd_to_devpath(fd
);
581 for (elem
= list
; elem
; elem
= elem
->next
)
582 if (path_attached_to_hba(disk_path
, elem
->path
))
585 if (disk_path
!= devname
)
591 static int find_intel_hba_capability(int fd
, struct intel_super
*super
,
594 static struct supertype
*match_metadata_desc_imsm(char *arg
)
596 struct supertype
*st
;
598 if (strcmp(arg
, "imsm") != 0 &&
599 strcmp(arg
, "default") != 0
603 st
= xcalloc(1, sizeof(*st
));
604 st
->ss
= &super_imsm
;
605 st
->max_devs
= IMSM_MAX_DEVICES
;
606 st
->minor_version
= 0;
612 static __u8
*get_imsm_version(struct imsm_super
*mpb
)
614 return &mpb
->sig
[MPB_SIG_LEN
];
618 /* retrieve a disk directly from the anchor when the anchor is known to be
619 * up-to-date, currently only at load time
621 static struct imsm_disk
*__get_imsm_disk(struct imsm_super
*mpb
, __u8 index
)
623 if (index
>= mpb
->num_disks
)
625 return &mpb
->disk
[index
];
628 /* retrieve the disk description based on a index of the disk
631 static struct dl
*get_imsm_dl_disk(struct intel_super
*super
, __u8 index
)
635 for (d
= super
->disks
; d
; d
= d
->next
)
636 if (d
->index
== index
)
641 /* retrieve a disk from the parsed metadata */
642 static struct imsm_disk
*get_imsm_disk(struct intel_super
*super
, __u8 index
)
646 dl
= get_imsm_dl_disk(super
, index
);
653 /* generate a checksum directly from the anchor when the anchor is known to be
654 * up-to-date, currently only at load or write_super after coalescing
656 static __u32
__gen_imsm_checksum(struct imsm_super
*mpb
)
658 __u32 end
= mpb
->mpb_size
/ sizeof(end
);
659 __u32
*p
= (__u32
*) mpb
;
663 sum
+= __le32_to_cpu(*p
);
667 return sum
- __le32_to_cpu(mpb
->check_sum
);
670 static size_t sizeof_imsm_map(struct imsm_map
*map
)
672 return sizeof(struct imsm_map
) + sizeof(__u32
) * (map
->num_members
- 1);
675 struct imsm_map
*get_imsm_map(struct imsm_dev
*dev
, int second_map
)
677 /* A device can have 2 maps if it is in the middle of a migration.
679 * MAP_0 - we return the first map
680 * MAP_1 - we return the second map if it exists, else NULL
681 * MAP_X - we return the second map if it exists, else the first
683 struct imsm_map
*map
= &dev
->vol
.map
[0];
684 struct imsm_map
*map2
= NULL
;
686 if (dev
->vol
.migr_state
)
687 map2
= (void *)map
+ sizeof_imsm_map(map
);
689 switch (second_map
) {
706 /* return the size of the device.
707 * migr_state increases the returned size if map[0] were to be duplicated
709 static size_t sizeof_imsm_dev(struct imsm_dev
*dev
, int migr_state
)
711 size_t size
= sizeof(*dev
) - sizeof(struct imsm_map
) +
712 sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
714 /* migrating means an additional map */
715 if (dev
->vol
.migr_state
)
716 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_1
));
718 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
724 /* retrieve disk serial number list from a metadata update */
725 static struct disk_info
*get_disk_info(struct imsm_update_create_array
*update
)
728 struct disk_info
*inf
;
730 inf
= u
+ sizeof(*update
) - sizeof(struct imsm_dev
) +
731 sizeof_imsm_dev(&update
->dev
, 0);
737 static struct imsm_dev
*__get_imsm_dev(struct imsm_super
*mpb
, __u8 index
)
743 if (index
>= mpb
->num_raid_devs
)
746 /* devices start after all disks */
747 offset
= ((void *) &mpb
->disk
[mpb
->num_disks
]) - _mpb
;
749 for (i
= 0; i
<= index
; i
++)
751 return _mpb
+ offset
;
753 offset
+= sizeof_imsm_dev(_mpb
+ offset
, 0);
758 static struct imsm_dev
*get_imsm_dev(struct intel_super
*super
, __u8 index
)
760 struct intel_dev
*dv
;
762 if (index
>= super
->anchor
->num_raid_devs
)
764 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
765 if (dv
->index
== index
)
772 * == MAP_0 get first map
773 * == MAP_1 get second map
774 * == MAP_X than get map according to the current migr_state
776 static __u32
get_imsm_ord_tbl_ent(struct imsm_dev
*dev
,
780 struct imsm_map
*map
;
782 map
= get_imsm_map(dev
, second_map
);
784 /* top byte identifies disk under rebuild */
785 return __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
788 #define ord_to_idx(ord) (((ord) << 8) >> 8)
789 static __u32
get_imsm_disk_idx(struct imsm_dev
*dev
, int slot
, int second_map
)
791 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, second_map
);
793 return ord_to_idx(ord
);
796 static void set_imsm_ord_tbl_ent(struct imsm_map
*map
, int slot
, __u32 ord
)
798 map
->disk_ord_tbl
[slot
] = __cpu_to_le32(ord
);
801 static int get_imsm_disk_slot(struct imsm_map
*map
, unsigned idx
)
806 for (slot
= 0; slot
< map
->num_members
; slot
++) {
807 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
808 if (ord_to_idx(ord
) == idx
)
815 static int get_imsm_raid_level(struct imsm_map
*map
)
817 if (map
->raid_level
== 1) {
818 if (map
->num_members
== 2)
824 return map
->raid_level
;
827 static int cmp_extent(const void *av
, const void *bv
)
829 const struct extent
*a
= av
;
830 const struct extent
*b
= bv
;
831 if (a
->start
< b
->start
)
833 if (a
->start
> b
->start
)
838 static int count_memberships(struct dl
*dl
, struct intel_super
*super
)
843 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
844 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
845 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
847 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
854 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
);
856 static int split_ull(unsigned long long n
, __u32
*lo
, __u32
*hi
)
858 if (lo
== 0 || hi
== 0)
860 *lo
= __le32_to_cpu((unsigned)n
);
861 *hi
= __le32_to_cpu((unsigned)(n
>> 32));
865 static unsigned long long join_u32(__u32 lo
, __u32 hi
)
867 return (unsigned long long)__le32_to_cpu(lo
) |
868 (((unsigned long long)__le32_to_cpu(hi
)) << 32);
871 static unsigned long long total_blocks(struct imsm_disk
*disk
)
875 return join_u32(disk
->total_blocks_lo
, disk
->total_blocks_hi
);
878 static unsigned long long pba_of_lba0(struct imsm_map
*map
)
882 return join_u32(map
->pba_of_lba0_lo
, map
->pba_of_lba0_hi
);
885 static unsigned long long blocks_per_member(struct imsm_map
*map
)
889 return join_u32(map
->blocks_per_member_lo
, map
->blocks_per_member_hi
);
893 static unsigned long long num_data_stripes(struct imsm_map
*map
)
897 return join_u32(map
->num_data_stripes_lo
, map
->num_data_stripes_hi
);
900 static void set_total_blocks(struct imsm_disk
*disk
, unsigned long long n
)
902 split_ull(n
, &disk
->total_blocks_lo
, &disk
->total_blocks_hi
);
906 static void set_pba_of_lba0(struct imsm_map
*map
, unsigned long long n
)
908 split_ull(n
, &map
->pba_of_lba0_lo
, &map
->pba_of_lba0_hi
);
911 static void set_blocks_per_member(struct imsm_map
*map
, unsigned long long n
)
913 split_ull(n
, &map
->blocks_per_member_lo
, &map
->blocks_per_member_hi
);
916 static void set_num_data_stripes(struct imsm_map
*map
, unsigned long long n
)
918 split_ull(n
, &map
->num_data_stripes_lo
, &map
->num_data_stripes_hi
);
921 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
)
923 /* find a list of used extents on the given physical device */
924 struct extent
*rv
, *e
;
926 int memberships
= count_memberships(dl
, super
);
929 /* trim the reserved area for spares, so they can join any array
930 * regardless of whether the OROM has assigned sectors from the
931 * IMSM_RESERVED_SECTORS region
934 reservation
= imsm_min_reserved_sectors(super
);
936 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
938 rv
= xcalloc(sizeof(struct extent
), (memberships
+ 1));
941 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
942 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
943 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
945 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
946 e
->start
= pba_of_lba0(map
);
947 e
->size
= blocks_per_member(map
);
951 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
953 /* determine the start of the metadata
954 * when no raid devices are defined use the default
955 * ...otherwise allow the metadata to truncate the value
956 * as is the case with older versions of imsm
959 struct extent
*last
= &rv
[memberships
- 1];
960 unsigned long long remainder
;
962 remainder
= total_blocks(&dl
->disk
) - (last
->start
+ last
->size
);
963 /* round down to 1k block to satisfy precision of the kernel
967 /* make sure remainder is still sane */
968 if (remainder
< (unsigned)ROUND_UP(super
->len
, 512) >> 9)
969 remainder
= ROUND_UP(super
->len
, 512) >> 9;
970 if (reservation
> remainder
)
971 reservation
= remainder
;
973 e
->start
= total_blocks(&dl
->disk
) - reservation
;
978 /* try to determine how much space is reserved for metadata from
979 * the last get_extents() entry, otherwise fallback to the
982 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
988 /* for spares just return a minimal reservation which will grow
989 * once the spare is picked up by an array
992 return MPB_SECTOR_CNT
;
994 e
= get_extents(super
, dl
);
996 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
998 /* scroll to last entry */
999 for (i
= 0; e
[i
].size
; i
++)
1002 rv
= total_blocks(&dl
->disk
) - e
[i
].start
;
1009 static int is_spare(struct imsm_disk
*disk
)
1011 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
1014 static int is_configured(struct imsm_disk
*disk
)
1016 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
1019 static int is_failed(struct imsm_disk
*disk
)
1021 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
1024 /* try to determine how much space is reserved for metadata from
1025 * the last get_extents() entry on the smallest active disk,
1026 * otherwise fallback to the default
1028 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
)
1032 unsigned long long min_active
;
1034 __u32 rv
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1035 struct dl
*dl
, *dl_min
= NULL
;
1041 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
1044 unsigned long long blocks
= total_blocks(&dl
->disk
);
1045 if (blocks
< min_active
|| min_active
== 0) {
1047 min_active
= blocks
;
1053 /* find last lba used by subarrays on the smallest active disk */
1054 e
= get_extents(super
, dl_min
);
1057 for (i
= 0; e
[i
].size
; i
++)
1060 remainder
= min_active
- e
[i
].start
;
1063 /* to give priority to recovery we should not require full
1064 IMSM_RESERVED_SECTORS from the spare */
1065 rv
= MPB_SECTOR_CNT
+ NUM_BLOCKS_DIRTY_STRIPE_REGION
;
1067 /* if real reservation is smaller use that value */
1068 return (remainder
< rv
) ? remainder
: rv
;
1071 /* Return minimum size of a spare that can be used in this array*/
1072 static unsigned long long min_acceptable_spare_size_imsm(struct supertype
*st
)
1074 struct intel_super
*super
= st
->sb
;
1078 unsigned long long rv
= 0;
1082 /* find first active disk in array */
1084 while (dl
&& (is_failed(&dl
->disk
) || dl
->index
== -1))
1088 /* find last lba used by subarrays */
1089 e
= get_extents(super
, dl
);
1092 for (i
= 0; e
[i
].size
; i
++)
1095 rv
= e
[i
-1].start
+ e
[i
-1].size
;
1098 /* add the amount of space needed for metadata */
1099 rv
= rv
+ imsm_min_reserved_sectors(super
);
1104 static int is_gen_migration(struct imsm_dev
*dev
);
1107 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
1108 struct imsm_dev
*dev
);
1110 static void print_imsm_dev(struct intel_super
*super
,
1111 struct imsm_dev
*dev
,
1117 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1118 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
1122 printf("[%.16s]:\n", dev
->volume
);
1123 printf(" UUID : %s\n", uuid
);
1124 printf(" RAID Level : %d", get_imsm_raid_level(map
));
1126 printf(" <-- %d", get_imsm_raid_level(map2
));
1128 printf(" Members : %d", map
->num_members
);
1130 printf(" <-- %d", map2
->num_members
);
1132 printf(" Slots : [");
1133 for (i
= 0; i
< map
->num_members
; i
++) {
1134 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
1135 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1140 for (i
= 0; i
< map2
->num_members
; i
++) {
1141 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_1
);
1142 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1147 printf(" Failed disk : ");
1148 if (map
->failed_disk_num
== 0xff)
1151 printf("%i", map
->failed_disk_num
);
1153 slot
= get_imsm_disk_slot(map
, disk_idx
);
1155 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
1156 printf(" This Slot : %d%s\n", slot
,
1157 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
1159 printf(" This Slot : ?\n");
1160 sz
= __le32_to_cpu(dev
->size_high
);
1162 sz
+= __le32_to_cpu(dev
->size_low
);
1163 printf(" Array Size : %llu%s\n", (unsigned long long)sz
,
1164 human_size(sz
* 512));
1165 sz
= blocks_per_member(map
);
1166 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz
,
1167 human_size(sz
* 512));
1168 printf(" Sector Offset : %llu\n",
1170 printf(" Num Stripes : %llu\n",
1171 num_data_stripes(map
));
1172 printf(" Chunk Size : %u KiB",
1173 __le16_to_cpu(map
->blocks_per_strip
) / 2);
1175 printf(" <-- %u KiB",
1176 __le16_to_cpu(map2
->blocks_per_strip
) / 2);
1178 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
1179 printf(" Migrate State : ");
1180 if (dev
->vol
.migr_state
) {
1181 if (migr_type(dev
) == MIGR_INIT
)
1182 printf("initialize\n");
1183 else if (migr_type(dev
) == MIGR_REBUILD
)
1184 printf("rebuild\n");
1185 else if (migr_type(dev
) == MIGR_VERIFY
)
1187 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
1188 printf("general migration\n");
1189 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
1190 printf("state change\n");
1191 else if (migr_type(dev
) == MIGR_REPAIR
)
1194 printf("<unknown:%d>\n", migr_type(dev
));
1197 printf(" Map State : %s", map_state_str
[map
->map_state
]);
1198 if (dev
->vol
.migr_state
) {
1199 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1201 printf(" <-- %s", map_state_str
[map
->map_state
]);
1202 printf("\n Checkpoint : %u ",
1203 __le32_to_cpu(dev
->vol
.curr_migr_unit
));
1204 if ((is_gen_migration(dev
)) && ((slot
> 1) || (slot
< 0)))
1207 printf("(%llu)", (unsigned long long)
1208 blocks_per_migr_unit(super
, dev
));
1211 printf(" Dirty State : %s\n", dev
->vol
.dirty
? "dirty" : "clean");
1214 static void print_imsm_disk(struct imsm_disk
*disk
, int index
, __u32 reserved
)
1216 char str
[MAX_RAID_SERIAL_LEN
+ 1];
1219 if (index
< -1 || !disk
)
1223 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
1225 printf(" Disk%02d Serial : %s\n", index
, str
);
1227 printf(" Disk Serial : %s\n", str
);
1228 printf(" State :%s%s%s\n", is_spare(disk
) ? " spare" : "",
1229 is_configured(disk
) ? " active" : "",
1230 is_failed(disk
) ? " failed" : "");
1231 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
1232 sz
= total_blocks(disk
) - reserved
;
1233 printf(" Usable Size : %llu%s\n", (unsigned long long)sz
,
1234 human_size(sz
* 512));
1237 void examine_migr_rec_imsm(struct intel_super
*super
)
1239 struct migr_record
*migr_rec
= super
->migr_rec
;
1240 struct imsm_super
*mpb
= super
->anchor
;
1243 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1244 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1245 struct imsm_map
*map
;
1248 if (is_gen_migration(dev
) == 0)
1251 printf("\nMigration Record Information:");
1253 /* first map under migration */
1254 map
= get_imsm_map(dev
, MAP_0
);
1256 slot
= get_imsm_disk_slot(map
, super
->disks
->index
);
1257 if ((map
== NULL
) || (slot
> 1) || (slot
< 0)) {
1258 printf(" Empty\n ");
1259 printf("Examine one of first two disks in array\n");
1262 printf("\n Status : ");
1263 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
1266 printf("Contains Data\n");
1267 printf(" Current Unit : %u\n",
1268 __le32_to_cpu(migr_rec
->curr_migr_unit
));
1269 printf(" Family : %u\n",
1270 __le32_to_cpu(migr_rec
->family_num
));
1271 printf(" Ascending : %u\n",
1272 __le32_to_cpu(migr_rec
->ascending_migr
));
1273 printf(" Blocks Per Unit : %u\n",
1274 __le32_to_cpu(migr_rec
->blocks_per_unit
));
1275 printf(" Dest. Depth Per Unit : %u\n",
1276 __le32_to_cpu(migr_rec
->dest_depth_per_unit
));
1277 printf(" Checkpoint Area pba : %u\n",
1278 __le32_to_cpu(migr_rec
->ckpt_area_pba
));
1279 printf(" First member lba : %u\n",
1280 __le32_to_cpu(migr_rec
->dest_1st_member_lba
));
1281 printf(" Total Number of Units : %u\n",
1282 __le32_to_cpu(migr_rec
->num_migr_units
));
1283 printf(" Size of volume : %u\n",
1284 __le32_to_cpu(migr_rec
->post_migr_vol_cap
));
1285 printf(" Expansion space for LBA64 : %u\n",
1286 __le32_to_cpu(migr_rec
->post_migr_vol_cap_hi
));
1287 printf(" Record was read from : %u\n",
1288 __le32_to_cpu(migr_rec
->ckpt_read_disk_num
));
1293 #endif /* MDASSEMBLE */
1294 /*******************************************************************************
1295 * function: imsm_check_attributes
1296 * Description: Function checks if features represented by attributes flags
1297 * are supported by mdadm.
1299 * attributes - Attributes read from metadata
1301 * 0 - passed attributes contains unsupported features flags
1302 * 1 - all features are supported
1303 ******************************************************************************/
1304 static int imsm_check_attributes(__u32 attributes
)
1307 __u32 not_supported
= MPB_ATTRIB_SUPPORTED
^0xffffffff;
1309 not_supported
&= ~MPB_ATTRIB_IGNORED
;
1311 not_supported
&= attributes
;
1312 if (not_supported
) {
1313 pr_err("(IMSM): Unsupported attributes : %x\n",
1314 (unsigned)__le32_to_cpu(not_supported
));
1315 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1316 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
1317 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1319 if (not_supported
& MPB_ATTRIB_2TB
) {
1320 dprintf("\t\tMPB_ATTRIB_2TB\n");
1321 not_supported
^= MPB_ATTRIB_2TB
;
1323 if (not_supported
& MPB_ATTRIB_RAID0
) {
1324 dprintf("\t\tMPB_ATTRIB_RAID0\n");
1325 not_supported
^= MPB_ATTRIB_RAID0
;
1327 if (not_supported
& MPB_ATTRIB_RAID1
) {
1328 dprintf("\t\tMPB_ATTRIB_RAID1\n");
1329 not_supported
^= MPB_ATTRIB_RAID1
;
1331 if (not_supported
& MPB_ATTRIB_RAID10
) {
1332 dprintf("\t\tMPB_ATTRIB_RAID10\n");
1333 not_supported
^= MPB_ATTRIB_RAID10
;
1335 if (not_supported
& MPB_ATTRIB_RAID1E
) {
1336 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
1337 not_supported
^= MPB_ATTRIB_RAID1E
;
1339 if (not_supported
& MPB_ATTRIB_RAID5
) {
1340 dprintf("\t\tMPB_ATTRIB_RAID5\n");
1341 not_supported
^= MPB_ATTRIB_RAID5
;
1343 if (not_supported
& MPB_ATTRIB_RAIDCNG
) {
1344 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
1345 not_supported
^= MPB_ATTRIB_RAIDCNG
;
1347 if (not_supported
& MPB_ATTRIB_BBM
) {
1348 dprintf("\t\tMPB_ATTRIB_BBM\n");
1349 not_supported
^= MPB_ATTRIB_BBM
;
1351 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1352 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
1353 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1355 if (not_supported
& MPB_ATTRIB_EXP_STRIPE_SIZE
) {
1356 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
1357 not_supported
^= MPB_ATTRIB_EXP_STRIPE_SIZE
;
1359 if (not_supported
& MPB_ATTRIB_2TB_DISK
) {
1360 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
1361 not_supported
^= MPB_ATTRIB_2TB_DISK
;
1363 if (not_supported
& MPB_ATTRIB_NEVER_USE2
) {
1364 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
1365 not_supported
^= MPB_ATTRIB_NEVER_USE2
;
1367 if (not_supported
& MPB_ATTRIB_NEVER_USE
) {
1368 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
1369 not_supported
^= MPB_ATTRIB_NEVER_USE
;
1373 dprintf(Name
"(IMSM): Unknown attributes : %x\n", not_supported
);
1382 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
1384 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
1386 struct intel_super
*super
= st
->sb
;
1387 struct imsm_super
*mpb
= super
->anchor
;
1388 char str
[MAX_SIGNATURE_LENGTH
];
1393 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
1396 snprintf(str
, MPB_SIG_LEN
, "%s", mpb
->sig
);
1397 printf(" Magic : %s\n", str
);
1398 snprintf(str
, strlen(MPB_VERSION_RAID0
), "%s", get_imsm_version(mpb
));
1399 printf(" Version : %s\n", get_imsm_version(mpb
));
1400 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
1401 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
1402 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
1403 printf(" Attributes : ");
1404 if (imsm_check_attributes(mpb
->attributes
))
1405 printf("All supported\n");
1407 printf("not supported\n");
1408 getinfo_super_imsm(st
, &info
, NULL
);
1409 fname_from_uuid(st
, &info
, nbuf
, ':');
1410 printf(" UUID : %s\n", nbuf
+ 5);
1411 sum
= __le32_to_cpu(mpb
->check_sum
);
1412 printf(" Checksum : %08x %s\n", sum
,
1413 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
1414 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
));
1415 printf(" Disks : %d\n", mpb
->num_disks
);
1416 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
1417 print_imsm_disk(__get_imsm_disk(mpb
, super
->disks
->index
), super
->disks
->index
, reserved
);
1418 if (super
->bbm_log
) {
1419 struct bbm_log
*log
= super
->bbm_log
;
1422 printf("Bad Block Management Log:\n");
1423 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
1424 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
1425 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
1426 printf(" Spare Blocks : %d\n", __le32_to_cpu(log
->reserved_spare_block_count
));
1427 printf(" First Spare : %llx\n",
1428 (unsigned long long) __le64_to_cpu(log
->first_spare_lba
));
1430 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1432 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1434 super
->current_vol
= i
;
1435 getinfo_super_imsm(st
, &info
, NULL
);
1436 fname_from_uuid(st
, &info
, nbuf
, ':');
1437 print_imsm_dev(super
, dev
, nbuf
+ 5, super
->disks
->index
);
1439 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1440 if (i
== super
->disks
->index
)
1442 print_imsm_disk(__get_imsm_disk(mpb
, i
), i
, reserved
);
1445 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
1446 if (dl
->index
== -1)
1447 print_imsm_disk(&dl
->disk
, -1, reserved
);
1449 examine_migr_rec_imsm(super
);
1452 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
1454 /* We just write a generic IMSM ARRAY entry */
1457 struct intel_super
*super
= st
->sb
;
1459 if (!super
->anchor
->num_raid_devs
) {
1460 printf("ARRAY metadata=imsm\n");
1464 getinfo_super_imsm(st
, &info
, NULL
);
1465 fname_from_uuid(st
, &info
, nbuf
, ':');
1466 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
1469 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
1471 /* We just write a generic IMSM ARRAY entry */
1475 struct intel_super
*super
= st
->sb
;
1478 if (!super
->anchor
->num_raid_devs
)
1481 getinfo_super_imsm(st
, &info
, NULL
);
1482 fname_from_uuid(st
, &info
, nbuf
, ':');
1483 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1484 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1486 super
->current_vol
= i
;
1487 getinfo_super_imsm(st
, &info
, NULL
);
1488 fname_from_uuid(st
, &info
, nbuf1
, ':');
1489 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
1490 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
1494 static void export_examine_super_imsm(struct supertype
*st
)
1496 struct intel_super
*super
= st
->sb
;
1497 struct imsm_super
*mpb
= super
->anchor
;
1501 getinfo_super_imsm(st
, &info
, NULL
);
1502 fname_from_uuid(st
, &info
, nbuf
, ':');
1503 printf("MD_METADATA=imsm\n");
1504 printf("MD_LEVEL=container\n");
1505 printf("MD_UUID=%s\n", nbuf
+5);
1506 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
1509 static int copy_metadata_imsm(struct supertype
*st
, int from
, int to
)
1511 /* The second last 512byte sector of the device contains
1512 * the "struct imsm_super" metadata.
1513 * This contains mpb_size which is the size in bytes of the
1514 * extended metadata. This is located immediately before
1516 * We want to read all that, plus the last sector which
1517 * may contain a migration record, and write it all
1521 unsigned long long dsize
, offset
;
1523 struct imsm_super
*sb
;
1526 if (posix_memalign(&buf
, 4096, 4096) != 0)
1529 if (!get_dev_size(from
, NULL
, &dsize
))
1532 if (lseek64(from
, dsize
-1024, 0) < 0)
1534 if (read(from
, buf
, 512) != 512)
1537 if (strncmp((char*)sb
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0)
1540 sectors
= mpb_sectors(sb
) + 2;
1541 offset
= dsize
- sectors
* 512;
1542 if (lseek64(from
, offset
, 0) < 0 ||
1543 lseek64(to
, offset
, 0) < 0)
1545 while (written
< sectors
* 512) {
1546 int n
= sectors
*512 - written
;
1549 if (read(from
, buf
, n
) != n
)
1551 if (write(to
, buf
, n
) != n
)
1562 static void detail_super_imsm(struct supertype
*st
, char *homehost
)
1567 getinfo_super_imsm(st
, &info
, NULL
);
1568 fname_from_uuid(st
, &info
, nbuf
, ':');
1569 printf("\n UUID : %s\n", nbuf
+ 5);
1572 static void brief_detail_super_imsm(struct supertype
*st
)
1576 getinfo_super_imsm(st
, &info
, NULL
);
1577 fname_from_uuid(st
, &info
, nbuf
, ':');
1578 printf(" UUID=%s", nbuf
+ 5);
1581 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
);
1582 static void fd2devname(int fd
, char *name
);
1584 static int ahci_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
1586 /* dump an unsorted list of devices attached to AHCI Intel storage
1587 * controller, as well as non-connected ports
1589 int hba_len
= strlen(hba_path
) + 1;
1594 unsigned long port_mask
= (1 << port_count
) - 1;
1596 if (port_count
> (int)sizeof(port_mask
) * 8) {
1598 pr_err("port_count %d out of range\n", port_count
);
1602 /* scroll through /sys/dev/block looking for devices attached to
1605 dir
= opendir("/sys/dev/block");
1606 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
1617 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
1619 path
= devt_to_devpath(makedev(major
, minor
));
1622 if (!path_attached_to_hba(path
, hba_path
)) {
1628 /* retrieve the scsi device type */
1629 if (asprintf(&device
, "/sys/dev/block/%d:%d/device/xxxxxxx", major
, minor
) < 0) {
1631 pr_err("failed to allocate 'device'\n");
1635 sprintf(device
, "/sys/dev/block/%d:%d/device/type", major
, minor
);
1636 if (load_sys(device
, buf
) != 0) {
1638 pr_err("failed to read device type for %s\n",
1644 type
= strtoul(buf
, NULL
, 10);
1646 /* if it's not a disk print the vendor and model */
1647 if (!(type
== 0 || type
== 7 || type
== 14)) {
1650 sprintf(device
, "/sys/dev/block/%d:%d/device/vendor", major
, minor
);
1651 if (load_sys(device
, buf
) == 0) {
1652 strncpy(vendor
, buf
, sizeof(vendor
));
1653 vendor
[sizeof(vendor
) - 1] = '\0';
1654 c
= (char *) &vendor
[sizeof(vendor
) - 1];
1655 while (isspace(*c
) || *c
== '\0')
1659 sprintf(device
, "/sys/dev/block/%d:%d/device/model", major
, minor
);
1660 if (load_sys(device
, buf
) == 0) {
1661 strncpy(model
, buf
, sizeof(model
));
1662 model
[sizeof(model
) - 1] = '\0';
1663 c
= (char *) &model
[sizeof(model
) - 1];
1664 while (isspace(*c
) || *c
== '\0')
1668 if (vendor
[0] && model
[0])
1669 sprintf(buf
, "%.64s %.64s", vendor
, model
);
1671 switch (type
) { /* numbers from hald/linux/device.c */
1672 case 1: sprintf(buf
, "tape"); break;
1673 case 2: sprintf(buf
, "printer"); break;
1674 case 3: sprintf(buf
, "processor"); break;
1676 case 5: sprintf(buf
, "cdrom"); break;
1677 case 6: sprintf(buf
, "scanner"); break;
1678 case 8: sprintf(buf
, "media_changer"); break;
1679 case 9: sprintf(buf
, "comm"); break;
1680 case 12: sprintf(buf
, "raid"); break;
1681 default: sprintf(buf
, "unknown");
1687 /* chop device path to 'host%d' and calculate the port number */
1688 c
= strchr(&path
[hba_len
], '/');
1691 pr_err("%s - invalid path name\n", path
+ hba_len
);
1696 if (sscanf(&path
[hba_len
], "host%d", &port
) == 1)
1700 *c
= '/'; /* repair the full string */
1701 pr_err("failed to determine port number for %s\n",
1708 /* mark this port as used */
1709 port_mask
&= ~(1 << port
);
1711 /* print out the device information */
1713 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
1717 fd
= dev_open(ent
->d_name
, O_RDONLY
);
1719 printf(" Port%d : - disk info unavailable -\n", port
);
1721 fd2devname(fd
, buf
);
1722 printf(" Port%d : %s", port
, buf
);
1723 if (imsm_read_serial(fd
, NULL
, (__u8
*) buf
) == 0)
1724 printf(" (%.*s)\n", MAX_RAID_SERIAL_LEN
, buf
);
1739 for (i
= 0; i
< port_count
; i
++)
1740 if (port_mask
& (1 << i
))
1741 printf(" Port%d : - no device attached -\n", i
);
1747 static void print_found_intel_controllers(struct sys_dev
*elem
)
1749 for (; elem
; elem
= elem
->next
) {
1750 pr_err("found Intel(R) ");
1751 if (elem
->type
== SYS_DEV_SATA
)
1752 fprintf(stderr
, "SATA ");
1753 else if (elem
->type
== SYS_DEV_SAS
)
1754 fprintf(stderr
, "SAS ");
1755 fprintf(stderr
, "RAID controller");
1757 fprintf(stderr
, " at %s", elem
->pci_id
);
1758 fprintf(stderr
, ".\n");
1763 static int ahci_get_port_count(const char *hba_path
, int *port_count
)
1770 if ((dir
= opendir(hba_path
)) == NULL
)
1773 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
1776 if (sscanf(ent
->d_name
, "host%d", &host
) != 1)
1778 if (*port_count
== 0)
1780 else if (host
< host_base
)
1783 if (host
+ 1 > *port_count
+ host_base
)
1784 *port_count
= host
+ 1 - host_base
;
1790 static void print_imsm_capability(const struct imsm_orom
*orom
)
1792 printf(" Platform : Intel(R) Matrix Storage Manager\n");
1793 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
1794 orom
->hotfix_ver
, orom
->build
);
1795 printf(" RAID Levels :%s%s%s%s%s\n",
1796 imsm_orom_has_raid0(orom
) ? " raid0" : "",
1797 imsm_orom_has_raid1(orom
) ? " raid1" : "",
1798 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
1799 imsm_orom_has_raid10(orom
) ? " raid10" : "",
1800 imsm_orom_has_raid5(orom
) ? " raid5" : "");
1801 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1802 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
1803 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
1804 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
1805 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
1806 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
1807 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
1808 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
1809 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
1810 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
1811 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
1812 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
1813 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
1814 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
1815 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
1816 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
1817 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
1818 printf(" 2TB volumes :%s supported\n",
1819 (orom
->attr
& IMSM_OROM_ATTR_2TB
)?"":" not");
1820 printf(" 2TB disks :%s supported\n",
1821 (orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
)?"":" not");
1822 printf(" Max Disks : %d\n", orom
->tds
);
1823 printf(" Max Volumes : %d per array, %d per controller\n",
1824 orom
->vpa
, orom
->vphba
);
1828 static void print_imsm_capability_export(const struct imsm_orom
*orom
)
1830 printf("MD_FIRMWARE_TYPE=imsm\n");
1831 printf("IMSM_VERSION=%d.%d.%d.%d\n",orom
->major_ver
, orom
->minor_ver
,
1832 orom
->hotfix_ver
, orom
->build
);
1833 printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n",
1834 imsm_orom_has_raid0(orom
) ? "raid0 " : "",
1835 imsm_orom_has_raid1(orom
) ? "raid1 " : "",
1836 imsm_orom_has_raid1e(orom
) ? "raid1e " : "",
1837 imsm_orom_has_raid5(orom
) ? "raid10 " : "",
1838 imsm_orom_has_raid10(orom
) ? "raid5 " : "");
1839 printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1840 imsm_orom_has_chunk(orom
, 2) ? "2k " : "",
1841 imsm_orom_has_chunk(orom
, 4) ? "4k " : "",
1842 imsm_orom_has_chunk(orom
, 8) ? "8k " : "",
1843 imsm_orom_has_chunk(orom
, 16) ? "16k " : "",
1844 imsm_orom_has_chunk(orom
, 32) ? "32k " : "",
1845 imsm_orom_has_chunk(orom
, 64) ? "64k " : "",
1846 imsm_orom_has_chunk(orom
, 128) ? "128k " : "",
1847 imsm_orom_has_chunk(orom
, 256) ? "256k " : "",
1848 imsm_orom_has_chunk(orom
, 512) ? "512k " : "",
1849 imsm_orom_has_chunk(orom
, 1024*1) ? "1M " : "",
1850 imsm_orom_has_chunk(orom
, 1024*2) ? "2M " : "",
1851 imsm_orom_has_chunk(orom
, 1024*4) ? "4M " : "",
1852 imsm_orom_has_chunk(orom
, 1024*8) ? "8M " : "",
1853 imsm_orom_has_chunk(orom
, 1024*16) ? "16M " : "",
1854 imsm_orom_has_chunk(orom
, 1024*32) ? "32M " : "",
1855 imsm_orom_has_chunk(orom
, 1024*64) ? "64M " : "");
1856 printf("IMSM_2TB_VOLUMES=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB
) ? "yes" : "no");
1857 printf("IMSM_2TB_DISKS=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) ? "yes" : "no");
1858 printf("IMSM_MAX_DISKS=%d\n",orom
->tds
);
1859 printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom
->vpa
);
1860 printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom
->vphba
);
1863 static int detail_platform_imsm(int verbose
, int enumerate_only
, char *controller_path
)
1865 /* There are two components to imsm platform support, the ahci SATA
1866 * controller and the option-rom. To find the SATA controller we
1867 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
1868 * controller with the Intel vendor id is present. This approach
1869 * allows mdadm to leverage the kernel's ahci detection logic, with the
1870 * caveat that if ahci.ko is not loaded mdadm will not be able to
1871 * detect platform raid capabilities. The option-rom resides in a
1872 * platform "Adapter ROM". We scan for its signature to retrieve the
1873 * platform capabilities. If raid support is disabled in the BIOS the
1874 * option-rom capability structure will not be available.
1876 const struct imsm_orom
*orom
;
1877 struct sys_dev
*list
, *hba
;
1882 if (enumerate_only
) {
1883 if (check_env("IMSM_NO_PLATFORM"))
1885 list
= find_intel_devices();
1888 for (hba
= list
; hba
; hba
= hba
->next
) {
1889 orom
= find_imsm_capability(hba
->type
);
1900 list
= find_intel_devices();
1903 pr_err("no active Intel(R) RAID "
1904 "controller found.\n");
1906 } else if (verbose
> 0)
1907 print_found_intel_controllers(list
);
1909 for (hba
= list
; hba
; hba
= hba
->next
) {
1910 if (controller_path
&& (compare_paths(hba
->path
,controller_path
) != 0))
1912 orom
= find_imsm_capability(hba
->type
);
1914 pr_err("imsm capabilities not found for controller: %s (type %s)\n",
1915 hba
->path
, get_sys_dev_type(hba
->type
));
1918 print_imsm_capability(orom
);
1919 printf(" I/O Controller : %s (%s)\n",
1920 hba
->path
, get_sys_dev_type(hba
->type
));
1921 if (hba
->type
== SYS_DEV_SATA
) {
1922 host_base
= ahci_get_port_count(hba
->path
, &port_count
);
1923 if (ahci_enumerate_ports(hba
->path
, port_count
, host_base
, verbose
)) {
1925 pr_err("failed to enumerate "
1926 "ports on SATA controller at %s.\n", hba
->pci_id
);
1933 if (controller_path
&& result
== 1)
1934 pr_err("no active Intel(R) RAID "
1935 "controller found under %s\n",controller_path
);
1940 static int export_detail_platform_imsm(int verbose
, char *controller_path
)
1942 const struct imsm_orom
*orom
;
1943 struct sys_dev
*list
, *hba
;
1946 list
= find_intel_devices();
1949 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n");
1954 for (hba
= list
; hba
; hba
= hba
->next
) {
1955 if (controller_path
&& (compare_paths(hba
->path
,controller_path
) != 0))
1957 orom
= find_imsm_capability(hba
->type
);
1960 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n",hba
->path
);
1963 print_imsm_capability_export(orom
);
1973 static int match_home_imsm(struct supertype
*st
, char *homehost
)
1975 /* the imsm metadata format does not specify any host
1976 * identification information. We return -1 since we can never
1977 * confirm nor deny whether a given array is "meant" for this
1978 * host. We rely on compare_super and the 'family_num' fields to
1979 * exclude member disks that do not belong, and we rely on
1980 * mdadm.conf to specify the arrays that should be assembled.
1981 * Auto-assembly may still pick up "foreign" arrays.
1987 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
1989 /* The uuid returned here is used for:
1990 * uuid to put into bitmap file (Create, Grow)
1991 * uuid for backup header when saving critical section (Grow)
1992 * comparing uuids when re-adding a device into an array
1993 * In these cases the uuid required is that of the data-array,
1994 * not the device-set.
1995 * uuid to recognise same set when adding a missing device back
1996 * to an array. This is a uuid for the device-set.
1998 * For each of these we can make do with a truncated
1999 * or hashed uuid rather than the original, as long as
2001 * In each case the uuid required is that of the data-array,
2002 * not the device-set.
2004 /* imsm does not track uuid's so we synthesis one using sha1 on
2005 * - The signature (Which is constant for all imsm array, but no matter)
2006 * - the orig_family_num of the container
2007 * - the index number of the volume
2008 * - the 'serial' number of the volume.
2009 * Hopefully these are all constant.
2011 struct intel_super
*super
= st
->sb
;
2014 struct sha1_ctx ctx
;
2015 struct imsm_dev
*dev
= NULL
;
2018 /* some mdadm versions failed to set ->orig_family_num, in which
2019 * case fall back to ->family_num. orig_family_num will be
2020 * fixed up with the first metadata update.
2022 family_num
= super
->anchor
->orig_family_num
;
2023 if (family_num
== 0)
2024 family_num
= super
->anchor
->family_num
;
2025 sha1_init_ctx(&ctx
);
2026 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
2027 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
2028 if (super
->current_vol
>= 0)
2029 dev
= get_imsm_dev(super
, super
->current_vol
);
2031 __u32 vol
= super
->current_vol
;
2032 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
2033 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
2035 sha1_finish_ctx(&ctx
, buf
);
2036 memcpy(uuid
, buf
, 4*4);
2041 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
2043 __u8
*v
= get_imsm_version(mpb
);
2044 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
2045 char major
[] = { 0, 0, 0 };
2046 char minor
[] = { 0 ,0, 0 };
2047 char patch
[] = { 0, 0, 0 };
2048 char *ver_parse
[] = { major
, minor
, patch
};
2052 while (*v
!= '\0' && v
< end
) {
2053 if (*v
!= '.' && j
< 2)
2054 ver_parse
[i
][j
++] = *v
;
2062 *m
= strtol(minor
, NULL
, 0);
2063 *p
= strtol(patch
, NULL
, 0);
2067 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
2069 /* migr_strip_size when repairing or initializing parity */
2070 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2071 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2073 switch (get_imsm_raid_level(map
)) {
2078 return 128*1024 >> 9;
2082 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
2084 /* migr_strip_size when rebuilding a degraded disk, no idea why
2085 * this is different than migr_strip_size_resync(), but it's good
2088 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2089 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2091 switch (get_imsm_raid_level(map
)) {
2094 if (map
->num_members
% map
->num_domains
== 0)
2095 return 128*1024 >> 9;
2099 return max((__u32
) 64*1024 >> 9, chunk
);
2101 return 128*1024 >> 9;
2105 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
2107 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2108 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2109 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
2110 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
2112 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
2115 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
2117 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2118 int level
= get_imsm_raid_level(lo
);
2120 if (level
== 1 || level
== 10) {
2121 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2123 return hi
->num_domains
;
2125 return num_stripes_per_unit_resync(dev
);
2128 static __u8
imsm_num_data_members(struct imsm_dev
*dev
, int second_map
)
2130 /* named 'imsm_' because raid0, raid1 and raid10
2131 * counter-intuitively have the same number of data disks
2133 struct imsm_map
*map
= get_imsm_map(dev
, second_map
);
2135 switch (get_imsm_raid_level(map
)) {
2137 return map
->num_members
;
2141 return map
->num_members
/2;
2143 return map
->num_members
- 1;
2145 dprintf("%s: unsupported raid level\n", __func__
);
2150 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
2152 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2153 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2155 switch(get_imsm_raid_level(map
)) {
2158 return chunk
* map
->num_domains
;
2160 return chunk
* map
->num_members
;
2166 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
2168 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2169 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2170 __u32 strip
= block
/ chunk
;
2172 switch (get_imsm_raid_level(map
)) {
2175 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
2176 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
2178 return vol_stripe
* chunk
+ block
% chunk
;
2180 __u32 stripe
= strip
/ (map
->num_members
- 1);
2182 return stripe
* chunk
+ block
% chunk
;
2189 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
2190 struct imsm_dev
*dev
)
2192 /* calculate the conversion factor between per member 'blocks'
2193 * (md/{resync,rebuild}_start) and imsm migration units, return
2194 * 0 for the 'not migrating' and 'unsupported migration' cases
2196 if (!dev
->vol
.migr_state
)
2199 switch (migr_type(dev
)) {
2200 case MIGR_GEN_MIGR
: {
2201 struct migr_record
*migr_rec
= super
->migr_rec
;
2202 return __le32_to_cpu(migr_rec
->blocks_per_unit
);
2207 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2208 __u32 stripes_per_unit
;
2209 __u32 blocks_per_unit
;
2218 /* yes, this is really the translation of migr_units to
2219 * per-member blocks in the 'resync' case
2221 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
2222 migr_chunk
= migr_strip_blocks_resync(dev
);
2223 disks
= imsm_num_data_members(dev
, MAP_0
);
2224 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
2225 stripe
= __le16_to_cpu(map
->blocks_per_strip
) * disks
;
2226 segment
= blocks_per_unit
/ stripe
;
2227 block_rel
= blocks_per_unit
- segment
* stripe
;
2228 parity_depth
= parity_segment_depth(dev
);
2229 block_map
= map_migr_block(dev
, block_rel
);
2230 return block_map
+ parity_depth
* segment
;
2232 case MIGR_REBUILD
: {
2233 __u32 stripes_per_unit
;
2236 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
2237 migr_chunk
= migr_strip_blocks_rebuild(dev
);
2238 return migr_chunk
* stripes_per_unit
;
2240 case MIGR_STATE_CHANGE
:
2246 static int imsm_level_to_layout(int level
)
2254 return ALGORITHM_LEFT_ASYMMETRIC
;
2261 /*******************************************************************************
2262 * Function: read_imsm_migr_rec
2263 * Description: Function reads imsm migration record from last sector of disk
2265 * fd : disk descriptor
2266 * super : metadata info
2270 ******************************************************************************/
2271 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
)
2274 unsigned long long dsize
;
2276 get_dev_size(fd
, NULL
, &dsize
);
2277 if (lseek64(fd
, dsize
- MIGR_REC_POSITION
, SEEK_SET
) < 0) {
2278 pr_err("Cannot seek to anchor block: %s\n",
2282 if (read(fd
, super
->migr_rec_buf
, MIGR_REC_BUF_SIZE
) !=
2283 MIGR_REC_BUF_SIZE
) {
2284 pr_err("Cannot read migr record block: %s\n",
2294 static struct imsm_dev
*imsm_get_device_during_migration(
2295 struct intel_super
*super
)
2298 struct intel_dev
*dv
;
2300 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
2301 if (is_gen_migration(dv
->dev
))
2307 /*******************************************************************************
2308 * Function: load_imsm_migr_rec
2309 * Description: Function reads imsm migration record (it is stored at the last
2312 * super : imsm internal array info
2313 * info : general array info
2317 * -2 : no migration in progress
2318 ******************************************************************************/
2319 static int load_imsm_migr_rec(struct intel_super
*super
, struct mdinfo
*info
)
2322 struct dl
*dl
= NULL
;
2326 struct imsm_dev
*dev
;
2327 struct imsm_map
*map
= NULL
;
2330 /* find map under migration */
2331 dev
= imsm_get_device_during_migration(super
);
2332 /* nothing to load,no migration in progress?
2336 map
= get_imsm_map(dev
, MAP_0
);
2339 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
2340 /* skip spare and failed disks
2342 if (sd
->disk
.raid_disk
< 0)
2344 /* read only from one of the first two slots */
2346 slot
= get_imsm_disk_slot(map
,
2347 sd
->disk
.raid_disk
);
2348 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2351 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
2352 fd
= dev_open(nm
, O_RDONLY
);
2358 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
2359 /* skip spare and failed disks
2363 /* read only from one of the first two slots */
2365 slot
= get_imsm_disk_slot(map
, dl
->index
);
2366 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2368 sprintf(nm
, "%d:%d", dl
->major
, dl
->minor
);
2369 fd
= dev_open(nm
, O_RDONLY
);
2376 retval
= read_imsm_migr_rec(fd
, super
);
2385 /*******************************************************************************
2386 * function: imsm_create_metadata_checkpoint_update
2387 * Description: It creates update for checkpoint change.
2389 * super : imsm internal array info
2390 * u : pointer to prepared update
2393 * If length is equal to 0, input pointer u contains no update
2394 ******************************************************************************/
2395 static int imsm_create_metadata_checkpoint_update(
2396 struct intel_super
*super
,
2397 struct imsm_update_general_migration_checkpoint
**u
)
2400 int update_memory_size
= 0;
2402 dprintf("imsm_create_metadata_checkpoint_update(enter)\n");
2408 /* size of all update data without anchor */
2409 update_memory_size
=
2410 sizeof(struct imsm_update_general_migration_checkpoint
);
2412 *u
= xcalloc(1, update_memory_size
);
2414 dprintf("error: cannot get memory for "
2415 "imsm_create_metadata_checkpoint_update update\n");
2418 (*u
)->type
= update_general_migration_checkpoint
;
2419 (*u
)->curr_migr_unit
= __le32_to_cpu(super
->migr_rec
->curr_migr_unit
);
2420 dprintf("imsm_create_metadata_checkpoint_update: prepared for %u\n",
2421 (*u
)->curr_migr_unit
);
2423 return update_memory_size
;
2426 static void imsm_update_metadata_locally(struct supertype
*st
,
2427 void *buf
, int len
);
2429 /*******************************************************************************
2430 * Function: write_imsm_migr_rec
2431 * Description: Function writes imsm migration record
2432 * (at the last sector of disk)
2434 * super : imsm internal array info
2438 ******************************************************************************/
2439 static int write_imsm_migr_rec(struct supertype
*st
)
2441 struct intel_super
*super
= st
->sb
;
2442 unsigned long long dsize
;
2448 struct imsm_update_general_migration_checkpoint
*u
;
2449 struct imsm_dev
*dev
;
2450 struct imsm_map
*map
= NULL
;
2452 /* find map under migration */
2453 dev
= imsm_get_device_during_migration(super
);
2454 /* if no migration, write buffer anyway to clear migr_record
2455 * on disk based on first available device
2458 dev
= get_imsm_dev(super
, super
->current_vol
< 0 ? 0 :
2459 super
->current_vol
);
2461 map
= get_imsm_map(dev
, MAP_0
);
2463 for (sd
= super
->disks
; sd
; sd
= sd
->next
) {
2466 /* skip failed and spare devices */
2469 /* write to 2 first slots only */
2471 slot
= get_imsm_disk_slot(map
, sd
->index
);
2472 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2475 sprintf(nm
, "%d:%d", sd
->major
, sd
->minor
);
2476 fd
= dev_open(nm
, O_RDWR
);
2479 get_dev_size(fd
, NULL
, &dsize
);
2480 if (lseek64(fd
, dsize
- MIGR_REC_POSITION
, SEEK_SET
) < 0) {
2481 pr_err("Cannot seek to anchor block: %s\n",
2485 if (write(fd
, super
->migr_rec_buf
, MIGR_REC_BUF_SIZE
) !=
2486 MIGR_REC_BUF_SIZE
) {
2487 pr_err("Cannot write migr record block: %s\n",
2494 /* update checkpoint information in metadata */
2495 len
= imsm_create_metadata_checkpoint_update(super
, &u
);
2498 dprintf("imsm: Cannot prepare update\n");
2501 /* update metadata locally */
2502 imsm_update_metadata_locally(st
, u
, len
);
2503 /* and possibly remotely */
2504 if (st
->update_tail
) {
2505 append_metadata_update(st
, u
, len
);
2506 /* during reshape we do all work inside metadata handler
2507 * manage_reshape(), so metadata update has to be triggered
2510 flush_metadata_updates(st
);
2511 st
->update_tail
= &st
->updates
;
2521 #endif /* MDASSEMBLE */
2523 /* spare/missing disks activations are not allowe when
2524 * array/container performs reshape operation, because
2525 * all arrays in container works on the same disks set
2527 int imsm_reshape_blocks_arrays_changes(struct intel_super
*super
)
2530 struct intel_dev
*i_dev
;
2531 struct imsm_dev
*dev
;
2533 /* check whole container
2535 for (i_dev
= super
->devlist
; i_dev
; i_dev
= i_dev
->next
) {
2537 if (is_gen_migration(dev
)) {
2538 /* No repair during any migration in container
2546 static unsigned long long imsm_component_size_aligment_check(int level
,
2548 unsigned long long component_size
)
2550 unsigned int component_size_alligment
;
2552 /* check component size aligment
2554 component_size_alligment
= component_size
% (chunk_size
/512);
2556 dprintf("imsm_component_size_aligment_check(Level: %i, "
2557 "chunk_size = %i, component_size = %llu), "
2558 "component_size_alligment = %u\n",
2559 level
, chunk_size
, component_size
,
2560 component_size_alligment
);
2562 if (component_size_alligment
&& (level
!= 1) && (level
!= UnSet
)) {
2563 dprintf("imsm: reported component size alligned from %llu ",
2565 component_size
-= component_size_alligment
;
2566 dprintf("to %llu (%i).\n",
2567 component_size
, component_size_alligment
);
2570 return component_size
;
2573 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
2575 struct intel_super
*super
= st
->sb
;
2576 struct migr_record
*migr_rec
= super
->migr_rec
;
2577 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
2578 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2579 struct imsm_map
*prev_map
= get_imsm_map(dev
, MAP_1
);
2580 struct imsm_map
*map_to_analyse
= map
;
2582 int map_disks
= info
->array
.raid_disks
;
2584 memset(info
, 0, sizeof(*info
));
2586 map_to_analyse
= prev_map
;
2588 dl
= super
->current_disk
;
2590 info
->container_member
= super
->current_vol
;
2591 info
->array
.raid_disks
= map
->num_members
;
2592 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
2593 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
2594 info
->array
.md_minor
= -1;
2595 info
->array
.ctime
= 0;
2596 info
->array
.utime
= 0;
2597 info
->array
.chunk_size
=
2598 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
2599 info
->array
.state
= !dev
->vol
.dirty
;
2600 info
->custom_array_size
= __le32_to_cpu(dev
->size_high
);
2601 info
->custom_array_size
<<= 32;
2602 info
->custom_array_size
|= __le32_to_cpu(dev
->size_low
);
2603 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
2605 if (is_gen_migration(dev
)) {
2606 info
->reshape_active
= 1;
2607 info
->new_level
= get_imsm_raid_level(map
);
2608 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
2609 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
2610 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
2611 if (info
->delta_disks
) {
2612 /* this needs to be applied to every array
2615 info
->reshape_active
= CONTAINER_RESHAPE
;
2617 /* We shape information that we give to md might have to be
2618 * modify to cope with md's requirement for reshaping arrays.
2619 * For example, when reshaping a RAID0, md requires it to be
2620 * presented as a degraded RAID4.
2621 * Also if a RAID0 is migrating to a RAID5 we need to specify
2622 * the array as already being RAID5, but the 'before' layout
2623 * is a RAID4-like layout.
2625 switch (info
->array
.level
) {
2627 switch(info
->new_level
) {
2629 /* conversion is happening as RAID4 */
2630 info
->array
.level
= 4;
2631 info
->array
.raid_disks
+= 1;
2634 /* conversion is happening as RAID5 */
2635 info
->array
.level
= 5;
2636 info
->array
.layout
= ALGORITHM_PARITY_N
;
2637 info
->delta_disks
-= 1;
2640 /* FIXME error message */
2641 info
->array
.level
= UnSet
;
2647 info
->new_level
= UnSet
;
2648 info
->new_layout
= UnSet
;
2649 info
->new_chunk
= info
->array
.chunk_size
;
2650 info
->delta_disks
= 0;
2654 info
->disk
.major
= dl
->major
;
2655 info
->disk
.minor
= dl
->minor
;
2656 info
->disk
.number
= dl
->index
;
2657 info
->disk
.raid_disk
= get_imsm_disk_slot(map_to_analyse
,
2661 info
->data_offset
= pba_of_lba0(map_to_analyse
);
2662 info
->component_size
= blocks_per_member(map_to_analyse
);
2664 info
->component_size
= imsm_component_size_aligment_check(
2666 info
->array
.chunk_size
,
2667 info
->component_size
);
2669 memset(info
->uuid
, 0, sizeof(info
->uuid
));
2670 info
->recovery_start
= MaxSector
;
2672 info
->reshape_progress
= 0;
2673 info
->resync_start
= MaxSector
;
2674 if ((map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
2676 imsm_reshape_blocks_arrays_changes(super
) == 0) {
2677 info
->resync_start
= 0;
2679 if (dev
->vol
.migr_state
) {
2680 switch (migr_type(dev
)) {
2683 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2685 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
2687 info
->resync_start
= blocks_per_unit
* units
;
2690 case MIGR_GEN_MIGR
: {
2691 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2693 __u64 units
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
2694 unsigned long long array_blocks
;
2697 if (__le32_to_cpu(migr_rec
->ascending_migr
) &&
2699 (__le32_to_cpu(migr_rec
->num_migr_units
)-1)) &&
2700 (super
->migr_rec
->rec_status
==
2701 __cpu_to_le32(UNIT_SRC_IN_CP_AREA
)))
2704 info
->reshape_progress
= blocks_per_unit
* units
;
2706 dprintf("IMSM: General Migration checkpoint : %llu "
2707 "(%llu) -> read reshape progress : %llu\n",
2708 (unsigned long long)units
,
2709 (unsigned long long)blocks_per_unit
,
2710 info
->reshape_progress
);
2712 used_disks
= imsm_num_data_members(dev
, MAP_1
);
2713 if (used_disks
> 0) {
2714 array_blocks
= blocks_per_member(map
) *
2716 /* round array size down to closest MB
2718 info
->custom_array_size
= (array_blocks
2719 >> SECT_PER_MB_SHIFT
)
2720 << SECT_PER_MB_SHIFT
;
2724 /* we could emulate the checkpointing of
2725 * 'sync_action=check' migrations, but for now
2726 * we just immediately complete them
2729 /* this is handled by container_content_imsm() */
2730 case MIGR_STATE_CHANGE
:
2731 /* FIXME handle other migrations */
2733 /* we are not dirty, so... */
2734 info
->resync_start
= MaxSector
;
2738 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
2739 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
2741 info
->array
.major_version
= -1;
2742 info
->array
.minor_version
= -2;
2743 sprintf(info
->text_version
, "/%s/%d", st
->container_devnm
, info
->container_member
);
2744 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
2745 uuid_from_super_imsm(st
, info
->uuid
);
2749 for (i
=0; i
<map_disks
; i
++) {
2751 if (i
< info
->array
.raid_disks
) {
2752 struct imsm_disk
*dsk
;
2753 j
= get_imsm_disk_idx(dev
, i
, MAP_X
);
2754 dsk
= get_imsm_disk(super
, j
);
2755 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
2762 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
2763 int failed
, int look_in_map
);
2765 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
2769 static void manage_second_map(struct intel_super
*super
, struct imsm_dev
*dev
)
2771 if (is_gen_migration(dev
)) {
2774 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
2776 failed
= imsm_count_failed(super
, dev
, MAP_1
);
2777 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_1
);
2778 if (map2
->map_state
!= map_state
) {
2779 map2
->map_state
= map_state
;
2780 super
->updates_pending
++;
2786 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
2790 for (d
= super
->missing
; d
; d
= d
->next
)
2791 if (d
->index
== index
)
2796 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
2798 struct intel_super
*super
= st
->sb
;
2799 struct imsm_disk
*disk
;
2800 int map_disks
= info
->array
.raid_disks
;
2801 int max_enough
= -1;
2803 struct imsm_super
*mpb
;
2805 if (super
->current_vol
>= 0) {
2806 getinfo_super_imsm_volume(st
, info
, map
);
2809 memset(info
, 0, sizeof(*info
));
2811 /* Set raid_disks to zero so that Assemble will always pull in valid
2814 info
->array
.raid_disks
= 0;
2815 info
->array
.level
= LEVEL_CONTAINER
;
2816 info
->array
.layout
= 0;
2817 info
->array
.md_minor
= -1;
2818 info
->array
.ctime
= 0; /* N/A for imsm */
2819 info
->array
.utime
= 0;
2820 info
->array
.chunk_size
= 0;
2822 info
->disk
.major
= 0;
2823 info
->disk
.minor
= 0;
2824 info
->disk
.raid_disk
= -1;
2825 info
->reshape_active
= 0;
2826 info
->array
.major_version
= -1;
2827 info
->array
.minor_version
= -2;
2828 strcpy(info
->text_version
, "imsm");
2829 info
->safe_mode_delay
= 0;
2830 info
->disk
.number
= -1;
2831 info
->disk
.state
= 0;
2833 info
->recovery_start
= MaxSector
;
2834 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
2836 /* do we have the all the insync disks that we expect? */
2837 mpb
= super
->anchor
;
2839 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
2840 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
2841 int failed
, enough
, j
, missing
= 0;
2842 struct imsm_map
*map
;
2845 failed
= imsm_count_failed(super
, dev
, MAP_0
);
2846 state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
2847 map
= get_imsm_map(dev
, MAP_0
);
2849 /* any newly missing disks?
2850 * (catches single-degraded vs double-degraded)
2852 for (j
= 0; j
< map
->num_members
; j
++) {
2853 __u32 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
2854 __u32 idx
= ord_to_idx(ord
);
2856 if (!(ord
& IMSM_ORD_REBUILD
) &&
2857 get_imsm_missing(super
, idx
)) {
2863 if (state
== IMSM_T_STATE_FAILED
)
2865 else if (state
== IMSM_T_STATE_DEGRADED
&&
2866 (state
!= map
->map_state
|| missing
))
2868 else /* we're normal, or already degraded */
2870 if (is_gen_migration(dev
) && missing
) {
2871 /* during general migration we need all disks
2872 * that process is running on.
2873 * No new missing disk is allowed.
2877 /* no more checks necessary
2881 /* in the missing/failed disk case check to see
2882 * if at least one array is runnable
2884 max_enough
= max(max_enough
, enough
);
2886 dprintf("%s: enough: %d\n", __func__
, max_enough
);
2887 info
->container_enough
= max_enough
;
2890 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
2892 disk
= &super
->disks
->disk
;
2893 info
->data_offset
= total_blocks(&super
->disks
->disk
) - reserved
;
2894 info
->component_size
= reserved
;
2895 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
2896 /* we don't change info->disk.raid_disk here because
2897 * this state will be finalized in mdmon after we have
2898 * found the 'most fresh' version of the metadata
2900 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
2901 info
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
2904 /* only call uuid_from_super_imsm when this disk is part of a populated container,
2905 * ->compare_super may have updated the 'num_raid_devs' field for spares
2907 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
2908 uuid_from_super_imsm(st
, info
->uuid
);
2910 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
2912 /* I don't know how to compute 'map' on imsm, so use safe default */
2915 for (i
= 0; i
< map_disks
; i
++)
2921 /* allocates memory and fills disk in mdinfo structure
2922 * for each disk in array */
2923 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
2925 struct mdinfo
*mddev
= NULL
;
2926 struct intel_super
*super
= st
->sb
;
2927 struct imsm_disk
*disk
;
2930 if (!super
|| !super
->disks
)
2933 mddev
= xcalloc(1, sizeof(*mddev
));
2937 tmp
= xcalloc(1, sizeof(*tmp
));
2939 tmp
->next
= mddev
->devs
;
2941 tmp
->disk
.number
= count
++;
2942 tmp
->disk
.major
= dl
->major
;
2943 tmp
->disk
.minor
= dl
->minor
;
2944 tmp
->disk
.state
= is_configured(disk
) ?
2945 (1 << MD_DISK_ACTIVE
) : 0;
2946 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
2947 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
2948 tmp
->disk
.raid_disk
= -1;
2954 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
2955 char *update
, char *devname
, int verbose
,
2956 int uuid_set
, char *homehost
)
2958 /* For 'assemble' and 'force' we need to return non-zero if any
2959 * change was made. For others, the return value is ignored.
2960 * Update options are:
2961 * force-one : This device looks a bit old but needs to be included,
2962 * update age info appropriately.
2963 * assemble: clear any 'faulty' flag to allow this device to
2965 * force-array: Array is degraded but being forced, mark it clean
2966 * if that will be needed to assemble it.
2968 * newdev: not used ????
2969 * grow: Array has gained a new device - this is currently for
2971 * resync: mark as dirty so a resync will happen.
2972 * name: update the name - preserving the homehost
2973 * uuid: Change the uuid of the array to match watch is given
2975 * Following are not relevant for this imsm:
2976 * sparc2.2 : update from old dodgey metadata
2977 * super-minor: change the preferred_minor number
2978 * summaries: update redundant counters.
2979 * homehost: update the recorded homehost
2980 * _reshape_progress: record new reshape_progress position.
2983 struct intel_super
*super
= st
->sb
;
2984 struct imsm_super
*mpb
;
2986 /* we can only update container info */
2987 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
2990 mpb
= super
->anchor
;
2992 if (strcmp(update
, "uuid") == 0) {
2993 /* We take this to mean that the family_num should be updated.
2994 * However that is much smaller than the uuid so we cannot really
2995 * allow an explicit uuid to be given. And it is hard to reliably
2997 * So if !uuid_set we know the current uuid is random and just used
2998 * the first 'int' and copy it to the other 3 positions.
2999 * Otherwise we require the 4 'int's to be the same as would be the
3000 * case if we are using a random uuid. So an explicit uuid will be
3001 * accepted as long as all for ints are the same... which shouldn't hurt
3004 info
->uuid
[1] = info
->uuid
[2] = info
->uuid
[3] = info
->uuid
[0];
3007 if (info
->uuid
[0] != info
->uuid
[1] ||
3008 info
->uuid
[1] != info
->uuid
[2] ||
3009 info
->uuid
[2] != info
->uuid
[3])
3015 mpb
->orig_family_num
= info
->uuid
[0];
3016 } else if (strcmp(update
, "assemble") == 0)
3021 /* successful update? recompute checksum */
3023 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
3028 static size_t disks_to_mpb_size(int disks
)
3032 size
= sizeof(struct imsm_super
);
3033 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
3034 size
+= 2 * sizeof(struct imsm_dev
);
3035 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
3036 size
+= (4 - 2) * sizeof(struct imsm_map
);
3037 /* 4 possible disk_ord_tbl's */
3038 size
+= 4 * (disks
- 1) * sizeof(__u32
);
3043 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
,
3044 unsigned long long data_offset
)
3046 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
3049 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
3052 static void free_devlist(struct intel_super
*super
)
3054 struct intel_dev
*dv
;
3056 while (super
->devlist
) {
3057 dv
= super
->devlist
->next
;
3058 free(super
->devlist
->dev
);
3059 free(super
->devlist
);
3060 super
->devlist
= dv
;
3064 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
3066 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
3069 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
3073 * 0 same, or first was empty, and second was copied
3074 * 1 second had wrong number
3076 * 3 wrong other info
3078 struct intel_super
*first
= st
->sb
;
3079 struct intel_super
*sec
= tst
->sb
;
3086 /* in platform dependent environment test if the disks
3087 * use the same Intel hba
3088 * If not on Intel hba at all, allow anything.
3090 if (!check_env("IMSM_NO_PLATFORM")) {
3091 if (first
->hba
&& sec
->hba
&&
3092 strcmp(first
->hba
->path
, sec
->hba
->path
) != 0) {
3094 "HBAs of devices does not match %s != %s\n",
3095 first
->hba
? first
->hba
->path
: NULL
,
3096 sec
->hba
? sec
->hba
->path
: NULL
);
3101 /* if an anchor does not have num_raid_devs set then it is a free
3104 if (first
->anchor
->num_raid_devs
> 0 &&
3105 sec
->anchor
->num_raid_devs
> 0) {
3106 /* Determine if these disks might ever have been
3107 * related. Further disambiguation can only take place
3108 * in load_super_imsm_all
3110 __u32 first_family
= first
->anchor
->orig_family_num
;
3111 __u32 sec_family
= sec
->anchor
->orig_family_num
;
3113 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
3114 MAX_SIGNATURE_LENGTH
) != 0)
3117 if (first_family
== 0)
3118 first_family
= first
->anchor
->family_num
;
3119 if (sec_family
== 0)
3120 sec_family
= sec
->anchor
->family_num
;
3122 if (first_family
!= sec_family
)
3127 /* if 'first' is a spare promote it to a populated mpb with sec's
3130 if (first
->anchor
->num_raid_devs
== 0 &&
3131 sec
->anchor
->num_raid_devs
> 0) {
3133 struct intel_dev
*dv
;
3134 struct imsm_dev
*dev
;
3136 /* we need to copy raid device info from sec if an allocation
3137 * fails here we don't associate the spare
3139 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
3140 dv
= xmalloc(sizeof(*dv
));
3141 dev
= xmalloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
3144 dv
->next
= first
->devlist
;
3145 first
->devlist
= dv
;
3147 if (i
< sec
->anchor
->num_raid_devs
) {
3148 /* allocation failure */
3149 free_devlist(first
);
3150 pr_err("imsm: failed to associate spare\n");
3153 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
3154 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
3155 first
->anchor
->family_num
= sec
->anchor
->family_num
;
3156 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
3157 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
3158 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
3164 static void fd2devname(int fd
, char *name
)
3168 char dname
[PATH_MAX
];
3173 if (fstat(fd
, &st
) != 0)
3175 sprintf(path
, "/sys/dev/block/%d:%d",
3176 major(st
.st_rdev
), minor(st
.st_rdev
));
3178 rv
= readlink(path
, dname
, sizeof(dname
)-1);
3183 nm
= strrchr(dname
, '/');
3186 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
3190 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
3192 static int imsm_read_serial(int fd
, char *devname
,
3193 __u8 serial
[MAX_RAID_SERIAL_LEN
])
3195 unsigned char scsi_serial
[255];
3204 memset(scsi_serial
, 0, sizeof(scsi_serial
));
3206 rv
= scsi_get_serial(fd
, scsi_serial
, sizeof(scsi_serial
));
3208 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
3209 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3210 fd2devname(fd
, (char *) serial
);
3216 pr_err("Failed to retrieve serial for %s\n",
3221 rsp_len
= scsi_serial
[3];
3224 pr_err("Failed to retrieve serial for %s\n",
3228 rsp_buf
= (char *) &scsi_serial
[4];
3230 /* trim all whitespace and non-printable characters and convert
3233 for (i
= 0, dest
= rsp_buf
; i
< rsp_len
; i
++) {
3236 /* ':' is reserved for use in placeholder serial
3237 * numbers for missing disks
3245 len
= dest
- rsp_buf
;
3248 /* truncate leading characters */
3249 if (len
> MAX_RAID_SERIAL_LEN
) {
3250 dest
+= len
- MAX_RAID_SERIAL_LEN
;
3251 len
= MAX_RAID_SERIAL_LEN
;
3254 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3255 memcpy(serial
, dest
, len
);
3260 static int serialcmp(__u8
*s1
, __u8
*s2
)
3262 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
3265 static void serialcpy(__u8
*dest
, __u8
*src
)
3267 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
3270 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
3274 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3275 if (serialcmp(dl
->serial
, serial
) == 0)
3281 static struct imsm_disk
*
3282 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
3286 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3287 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3289 if (serialcmp(disk
->serial
, serial
) == 0) {
3300 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3302 struct imsm_disk
*disk
;
3307 __u8 serial
[MAX_RAID_SERIAL_LEN
];
3309 rv
= imsm_read_serial(fd
, devname
, serial
);
3314 dl
= xcalloc(1, sizeof(*dl
));
3317 dl
->major
= major(stb
.st_rdev
);
3318 dl
->minor
= minor(stb
.st_rdev
);
3319 dl
->next
= super
->disks
;
3320 dl
->fd
= keep_fd
? fd
: -1;
3321 assert(super
->disks
== NULL
);
3323 serialcpy(dl
->serial
, serial
);
3326 fd2devname(fd
, name
);
3328 dl
->devname
= xstrdup(devname
);
3330 dl
->devname
= xstrdup(name
);
3332 /* look up this disk's index in the current anchor */
3333 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
3336 /* only set index on disks that are a member of a
3337 * populated contianer, i.e. one with raid_devs
3339 if (is_failed(&dl
->disk
))
3341 else if (is_spare(&dl
->disk
))
3349 /* When migrating map0 contains the 'destination' state while map1
3350 * contains the current state. When not migrating map0 contains the
3351 * current state. This routine assumes that map[0].map_state is set to
3352 * the current array state before being called.
3354 * Migration is indicated by one of the following states
3355 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
3356 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
3357 * map1state=unitialized)
3358 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
3360 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
3361 * map1state=degraded)
3362 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
3365 static void migrate(struct imsm_dev
*dev
, struct intel_super
*super
,
3366 __u8 to_state
, int migr_type
)
3368 struct imsm_map
*dest
;
3369 struct imsm_map
*src
= get_imsm_map(dev
, MAP_0
);
3371 dev
->vol
.migr_state
= 1;
3372 set_migr_type(dev
, migr_type
);
3373 dev
->vol
.curr_migr_unit
= 0;
3374 dest
= get_imsm_map(dev
, MAP_1
);
3376 /* duplicate and then set the target end state in map[0] */
3377 memcpy(dest
, src
, sizeof_imsm_map(src
));
3378 if ((migr_type
== MIGR_REBUILD
) ||
3379 (migr_type
== MIGR_GEN_MIGR
)) {
3383 for (i
= 0; i
< src
->num_members
; i
++) {
3384 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
3385 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
3389 if (migr_type
== MIGR_GEN_MIGR
)
3390 /* Clear migration record */
3391 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
3393 src
->map_state
= to_state
;
3396 static void end_migration(struct imsm_dev
*dev
, struct intel_super
*super
,
3399 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3400 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
== 0 ?
3404 /* merge any IMSM_ORD_REBUILD bits that were not successfully
3405 * completed in the last migration.
3407 * FIXME add support for raid-level-migration
3409 if ((map_state
!= map
->map_state
) && (is_gen_migration(dev
) == 0) &&
3410 (prev
->map_state
!= IMSM_T_STATE_UNINITIALIZED
)) {
3411 /* when final map state is other than expected
3412 * merge maps (not for migration)
3416 for (i
= 0; i
< prev
->num_members
; i
++)
3417 for (j
= 0; j
< map
->num_members
; j
++)
3418 /* during online capacity expansion
3419 * disks position can be changed
3420 * if takeover is used
3422 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
3423 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
3424 map
->disk_ord_tbl
[j
] |=
3425 prev
->disk_ord_tbl
[i
];
3428 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3429 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3432 dev
->vol
.migr_state
= 0;
3433 set_migr_type(dev
, 0);
3434 dev
->vol
.curr_migr_unit
= 0;
3435 map
->map_state
= map_state
;
3439 static int parse_raid_devices(struct intel_super
*super
)
3442 struct imsm_dev
*dev_new
;
3443 size_t len
, len_migr
;
3445 size_t space_needed
= 0;
3446 struct imsm_super
*mpb
= super
->anchor
;
3448 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3449 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3450 struct intel_dev
*dv
;
3452 len
= sizeof_imsm_dev(dev_iter
, 0);
3453 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
3455 space_needed
+= len_migr
- len
;
3457 dv
= xmalloc(sizeof(*dv
));
3458 if (max_len
< len_migr
)
3460 if (max_len
> len_migr
)
3461 space_needed
+= max_len
- len_migr
;
3462 dev_new
= xmalloc(max_len
);
3463 imsm_copy_dev(dev_new
, dev_iter
);
3466 dv
->next
= super
->devlist
;
3467 super
->devlist
= dv
;
3470 /* ensure that super->buf is large enough when all raid devices
3473 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
3476 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
, 512);
3477 if (posix_memalign(&buf
, 512, len
) != 0)
3480 memcpy(buf
, super
->buf
, super
->len
);
3481 memset(buf
+ super
->len
, 0, len
- super
->len
);
3490 /* retrieve a pointer to the bbm log which starts after all raid devices */
3491 struct bbm_log
*__get_imsm_bbm_log(struct imsm_super
*mpb
)
3495 if (__le32_to_cpu(mpb
->bbm_log_size
)) {
3497 ptr
+= mpb
->mpb_size
- __le32_to_cpu(mpb
->bbm_log_size
);
3503 /*******************************************************************************
3504 * Function: check_mpb_migr_compatibility
3505 * Description: Function checks for unsupported migration features:
3506 * - migration optimization area (pba_of_lba0)
3507 * - descending reshape (ascending_migr)
3509 * super : imsm metadata information
3511 * 0 : migration is compatible
3512 * -1 : migration is not compatible
3513 ******************************************************************************/
3514 int check_mpb_migr_compatibility(struct intel_super
*super
)
3516 struct imsm_map
*map0
, *map1
;
3517 struct migr_record
*migr_rec
= super
->migr_rec
;
3520 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3521 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3524 dev_iter
->vol
.migr_state
== 1 &&
3525 dev_iter
->vol
.migr_type
== MIGR_GEN_MIGR
) {
3526 /* This device is migrating */
3527 map0
= get_imsm_map(dev_iter
, MAP_0
);
3528 map1
= get_imsm_map(dev_iter
, MAP_1
);
3529 if (pba_of_lba0(map0
) != pba_of_lba0(map1
))
3530 /* migration optimization area was used */
3532 if (migr_rec
->ascending_migr
== 0
3533 && migr_rec
->dest_depth_per_unit
> 0)
3534 /* descending reshape not supported yet */
3541 static void __free_imsm(struct intel_super
*super
, int free_disks
);
3543 /* load_imsm_mpb - read matrix metadata
3544 * allocates super->mpb to be freed by free_imsm
3546 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
3548 unsigned long long dsize
;
3549 unsigned long long sectors
;
3551 struct imsm_super
*anchor
;
3554 get_dev_size(fd
, NULL
, &dsize
);
3557 pr_err("%s: device to small for imsm\n",
3562 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0) {
3564 pr_err("Cannot seek to anchor block on %s: %s\n",
3565 devname
, strerror(errno
));
3569 if (posix_memalign((void**)&anchor
, 512, 512) != 0) {
3571 pr_err("Failed to allocate imsm anchor buffer"
3572 " on %s\n", devname
);
3575 if (read(fd
, anchor
, 512) != 512) {
3577 pr_err("Cannot read anchor block on %s: %s\n",
3578 devname
, strerror(errno
));
3583 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
3585 pr_err("no IMSM anchor on %s\n", devname
);
3590 __free_imsm(super
, 0);
3591 /* reload capability and hba */
3593 /* capability and hba must be updated with new super allocation */
3594 find_intel_hba_capability(fd
, super
, devname
);
3595 super
->len
= ROUND_UP(anchor
->mpb_size
, 512);
3596 if (posix_memalign(&super
->buf
, 512, super
->len
) != 0) {
3598 pr_err("unable to allocate %zu byte mpb buffer\n",
3603 memcpy(super
->buf
, anchor
, 512);
3605 sectors
= mpb_sectors(anchor
) - 1;
3608 if (posix_memalign(&super
->migr_rec_buf
, 512, MIGR_REC_BUF_SIZE
) != 0) {
3609 pr_err("%s could not allocate migr_rec buffer\n", __func__
);
3613 super
->clean_migration_record_by_mdmon
= 0;
3616 check_sum
= __gen_imsm_checksum(super
->anchor
);
3617 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3619 pr_err("IMSM checksum %x != %x on %s\n",
3621 __le32_to_cpu(super
->anchor
->check_sum
),
3629 /* read the extended mpb */
3630 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0) {
3632 pr_err("Cannot seek to extended mpb on %s: %s\n",
3633 devname
, strerror(errno
));
3637 if ((unsigned)read(fd
, super
->buf
+ 512, super
->len
- 512) != super
->len
- 512) {
3639 pr_err("Cannot read extended mpb on %s: %s\n",
3640 devname
, strerror(errno
));
3644 check_sum
= __gen_imsm_checksum(super
->anchor
);
3645 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3647 pr_err("IMSM checksum %x != %x on %s\n",
3648 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
3653 /* FIXME the BBM log is disk specific so we cannot use this global
3654 * buffer for all disks. Ok for now since we only look at the global
3655 * bbm_log_size parameter to gate assembly
3657 super
->bbm_log
= __get_imsm_bbm_log(super
->anchor
);
3662 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
);
3664 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
3665 static void clear_hi(struct intel_super
*super
)
3667 struct imsm_super
*mpb
= super
->anchor
;
3669 if (mpb
->attributes
& MPB_ATTRIB_2TB_DISK
)
3671 for (i
= 0; i
< mpb
->num_disks
; ++i
) {
3672 struct imsm_disk
*disk
= &mpb
->disk
[i
];
3673 disk
->total_blocks_hi
= 0;
3675 for (i
= 0; i
< mpb
->num_raid_devs
; ++i
) {
3676 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3679 for (n
= 0; n
< 2; ++n
) {
3680 struct imsm_map
*map
= get_imsm_map(dev
, n
);
3683 map
->pba_of_lba0_hi
= 0;
3684 map
->blocks_per_member_hi
= 0;
3685 map
->num_data_stripes_hi
= 0;
3691 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3695 err
= load_imsm_mpb(fd
, super
, devname
);
3698 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
3701 err
= parse_raid_devices(super
);
3706 static void __free_imsm_disk(struct dl
*d
)
3718 static void free_imsm_disks(struct intel_super
*super
)
3722 while (super
->disks
) {
3724 super
->disks
= d
->next
;
3725 __free_imsm_disk(d
);
3727 while (super
->disk_mgmt_list
) {
3728 d
= super
->disk_mgmt_list
;
3729 super
->disk_mgmt_list
= d
->next
;
3730 __free_imsm_disk(d
);
3732 while (super
->missing
) {
3734 super
->missing
= d
->next
;
3735 __free_imsm_disk(d
);
3740 /* free all the pieces hanging off of a super pointer */
3741 static void __free_imsm(struct intel_super
*super
, int free_disks
)
3743 struct intel_hba
*elem
, *next
;
3749 /* unlink capability description */
3751 if (super
->migr_rec_buf
) {
3752 free(super
->migr_rec_buf
);
3753 super
->migr_rec_buf
= NULL
;
3756 free_imsm_disks(super
);
3757 free_devlist(super
);
3761 free((void *)elem
->path
);
3769 static void free_imsm(struct intel_super
*super
)
3771 __free_imsm(super
, 1);
3775 static void free_super_imsm(struct supertype
*st
)
3777 struct intel_super
*super
= st
->sb
;
3786 static struct intel_super
*alloc_super(void)
3788 struct intel_super
*super
= xcalloc(1, sizeof(*super
));
3790 super
->current_vol
= -1;
3791 super
->create_offset
= ~((unsigned long long) 0);
3796 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
3798 static int find_intel_hba_capability(int fd
, struct intel_super
*super
, char *devname
)
3800 struct sys_dev
*hba_name
;
3803 if ((fd
< 0) || check_env("IMSM_NO_PLATFORM")) {
3808 hba_name
= find_disk_attached_hba(fd
, NULL
);
3811 pr_err("%s is not attached to Intel(R) RAID controller.\n",
3815 rv
= attach_hba_to_super(super
, hba_name
);
3818 struct intel_hba
*hba
= super
->hba
;
3820 pr_err("%s is attached to Intel(R) %s RAID "
3821 "controller (%s),\n"
3822 " but the container is assigned to Intel(R) "
3823 "%s RAID controller (",
3826 hba_name
->pci_id
? : "Err!",
3827 get_sys_dev_type(hba_name
->type
));
3830 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
3832 fprintf(stderr
, ", ");
3836 fprintf(stderr
, ").\n");
3837 cont_err("Mixing devices attached to multiple controllers "
3838 "is not allowed.\n");
3842 super
->orom
= find_imsm_capability(hba_name
->type
);
3848 /* find_missing - helper routine for load_super_imsm_all that identifies
3849 * disks that have disappeared from the system. This routine relies on
3850 * the mpb being uptodate, which it is at load time.
3852 static int find_missing(struct intel_super
*super
)
3855 struct imsm_super
*mpb
= super
->anchor
;
3857 struct imsm_disk
*disk
;
3859 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3860 disk
= __get_imsm_disk(mpb
, i
);
3861 dl
= serial_to_dl(disk
->serial
, super
);
3865 dl
= xmalloc(sizeof(*dl
));
3869 dl
->devname
= xstrdup("missing");
3871 serialcpy(dl
->serial
, disk
->serial
);
3874 dl
->next
= super
->missing
;
3875 super
->missing
= dl
;
3882 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
3884 struct intel_disk
*idisk
= disk_list
;
3887 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
3889 idisk
= idisk
->next
;
3895 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
3896 struct intel_super
*super
,
3897 struct intel_disk
**disk_list
)
3899 struct imsm_disk
*d
= &super
->disks
->disk
;
3900 struct imsm_super
*mpb
= super
->anchor
;
3903 for (i
= 0; i
< tbl_size
; i
++) {
3904 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
3905 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
3907 if (tbl_mpb
->family_num
== mpb
->family_num
) {
3908 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
3909 dprintf("%s: mpb from %d:%d matches %d:%d\n",
3910 __func__
, super
->disks
->major
,
3911 super
->disks
->minor
,
3912 table
[i
]->disks
->major
,
3913 table
[i
]->disks
->minor
);
3917 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
3918 is_configured(d
) == is_configured(tbl_d
)) &&
3919 tbl_mpb
->generation_num
< mpb
->generation_num
) {
3920 /* current version of the mpb is a
3921 * better candidate than the one in
3922 * super_table, but copy over "cross
3923 * generational" status
3925 struct intel_disk
*idisk
;
3927 dprintf("%s: mpb from %d:%d replaces %d:%d\n",
3928 __func__
, super
->disks
->major
,
3929 super
->disks
->minor
,
3930 table
[i
]->disks
->major
,
3931 table
[i
]->disks
->minor
);
3933 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
3934 if (idisk
&& is_failed(&idisk
->disk
))
3935 tbl_d
->status
|= FAILED_DISK
;
3938 struct intel_disk
*idisk
;
3939 struct imsm_disk
*disk
;
3941 /* tbl_mpb is more up to date, but copy
3942 * over cross generational status before
3945 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
3946 if (disk
&& is_failed(disk
))
3947 d
->status
|= FAILED_DISK
;
3949 idisk
= disk_list_get(d
->serial
, *disk_list
);
3952 if (disk
&& is_configured(disk
))
3953 idisk
->disk
.status
|= CONFIGURED_DISK
;
3956 dprintf("%s: mpb from %d:%d prefer %d:%d\n",
3957 __func__
, super
->disks
->major
,
3958 super
->disks
->minor
,
3959 table
[i
]->disks
->major
,
3960 table
[i
]->disks
->minor
);
3968 table
[tbl_size
++] = super
;
3972 /* update/extend the merged list of imsm_disk records */
3973 for (j
= 0; j
< mpb
->num_disks
; j
++) {
3974 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
3975 struct intel_disk
*idisk
;
3977 idisk
= disk_list_get(disk
->serial
, *disk_list
);
3979 idisk
->disk
.status
|= disk
->status
;
3980 if (is_configured(&idisk
->disk
) ||
3981 is_failed(&idisk
->disk
))
3982 idisk
->disk
.status
&= ~(SPARE_DISK
);
3984 idisk
= xcalloc(1, sizeof(*idisk
));
3985 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
3986 idisk
->disk
= *disk
;
3987 idisk
->next
= *disk_list
;
3991 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
3998 static struct intel_super
*
3999 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
4002 struct imsm_super
*mpb
= super
->anchor
;
4006 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4007 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
4008 struct intel_disk
*idisk
;
4010 idisk
= disk_list_get(disk
->serial
, disk_list
);
4012 if (idisk
->owner
== owner
||
4013 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
4016 dprintf("%s: '%.16s' owner %d != %d\n",
4017 __func__
, disk
->serial
, idisk
->owner
,
4020 dprintf("%s: unknown disk %x [%d]: %.16s\n",
4021 __func__
, __le32_to_cpu(mpb
->family_num
), i
,
4027 if (ok_count
== mpb
->num_disks
)
4032 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
4034 struct intel_super
*s
;
4036 for (s
= super_list
; s
; s
= s
->next
) {
4037 if (family_num
!= s
->anchor
->family_num
)
4039 pr_err("Conflict, offlining family %#x on '%s'\n",
4040 __le32_to_cpu(family_num
), s
->disks
->devname
);
4044 static struct intel_super
*
4045 imsm_thunderdome(struct intel_super
**super_list
, int len
)
4047 struct intel_super
*super_table
[len
];
4048 struct intel_disk
*disk_list
= NULL
;
4049 struct intel_super
*champion
, *spare
;
4050 struct intel_super
*s
, **del
;
4055 memset(super_table
, 0, sizeof(super_table
));
4056 for (s
= *super_list
; s
; s
= s
->next
)
4057 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
4059 for (i
= 0; i
< tbl_size
; i
++) {
4060 struct imsm_disk
*d
;
4061 struct intel_disk
*idisk
;
4062 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
4065 d
= &s
->disks
->disk
;
4067 /* 'd' must appear in merged disk list for its
4068 * configuration to be valid
4070 idisk
= disk_list_get(d
->serial
, disk_list
);
4071 if (idisk
&& idisk
->owner
== i
)
4072 s
= validate_members(s
, disk_list
, i
);
4077 dprintf("%s: marking family: %#x from %d:%d offline\n",
4078 __func__
, mpb
->family_num
,
4079 super_table
[i
]->disks
->major
,
4080 super_table
[i
]->disks
->minor
);
4084 /* This is where the mdadm implementation differs from the Windows
4085 * driver which has no strict concept of a container. We can only
4086 * assemble one family from a container, so when returning a prodigal
4087 * array member to this system the code will not be able to disambiguate
4088 * the container contents that should be assembled ("foreign" versus
4089 * "local"). It requires user intervention to set the orig_family_num
4090 * to a new value to establish a new container. The Windows driver in
4091 * this situation fixes up the volume name in place and manages the
4092 * foreign array as an independent entity.
4097 for (i
= 0; i
< tbl_size
; i
++) {
4098 struct intel_super
*tbl_ent
= super_table
[i
];
4104 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
4109 if (s
&& !is_spare
) {
4110 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
4112 } else if (!s
&& !is_spare
)
4125 pr_err("Chose family %#x on '%s', "
4126 "assemble conflicts to new container with '--update=uuid'\n",
4127 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
4129 /* collect all dl's onto 'champion', and update them to
4130 * champion's version of the status
4132 for (s
= *super_list
; s
; s
= s
->next
) {
4133 struct imsm_super
*mpb
= champion
->anchor
;
4134 struct dl
*dl
= s
->disks
;
4139 mpb
->attributes
|= s
->anchor
->attributes
& MPB_ATTRIB_2TB_DISK
;
4141 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4142 struct imsm_disk
*disk
;
4144 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
4147 /* only set index on disks that are a member of
4148 * a populated contianer, i.e. one with
4151 if (is_failed(&dl
->disk
))
4153 else if (is_spare(&dl
->disk
))
4159 if (i
>= mpb
->num_disks
) {
4160 struct intel_disk
*idisk
;
4162 idisk
= disk_list_get(dl
->serial
, disk_list
);
4163 if (idisk
&& is_spare(&idisk
->disk
) &&
4164 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
4172 dl
->next
= champion
->disks
;
4173 champion
->disks
= dl
;
4177 /* delete 'champion' from super_list */
4178 for (del
= super_list
; *del
; ) {
4179 if (*del
== champion
) {
4180 *del
= (*del
)->next
;
4183 del
= &(*del
)->next
;
4185 champion
->next
= NULL
;
4189 struct intel_disk
*idisk
= disk_list
;
4191 disk_list
= disk_list
->next
;
4199 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
);
4200 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
4201 int major
, int minor
, int keep_fd
);
4203 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4204 int *max
, int keep_fd
);
4206 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
4207 char *devname
, struct md_list
*devlist
,
4210 struct intel_super
*super_list
= NULL
;
4211 struct intel_super
*super
= NULL
;
4216 /* 'fd' is an opened container */
4217 err
= get_sra_super_block(fd
, &super_list
, devname
, &i
, keep_fd
);
4219 /* get super block from devlist devices */
4220 err
= get_devlist_super_block(devlist
, &super_list
, &i
, keep_fd
);
4223 /* all mpbs enter, maybe one leaves */
4224 super
= imsm_thunderdome(&super_list
, i
);
4230 if (find_missing(super
) != 0) {
4236 /* load migration record */
4237 err
= load_imsm_migr_rec(super
, NULL
);
4239 /* migration is in progress,
4240 * but migr_rec cannot be loaded,
4246 /* Check migration compatibility */
4247 if ((err
== 0) && (check_mpb_migr_compatibility(super
) != 0)) {
4248 pr_err("Unsupported migration detected");
4250 fprintf(stderr
, " on %s\n", devname
);
4252 fprintf(stderr
, " (IMSM).\n");
4261 while (super_list
) {
4262 struct intel_super
*s
= super_list
;
4264 super_list
= super_list
->next
;
4273 strcpy(st
->container_devnm
, fd2devnm(fd
));
4275 st
->container_devnm
[0] = 0;
4276 if (err
== 0 && st
->ss
== NULL
) {
4277 st
->ss
= &super_imsm
;
4278 st
->minor_version
= 0;
4279 st
->max_devs
= IMSM_MAX_DEVICES
;
4285 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4286 int *max
, int keep_fd
)
4288 struct md_list
*tmpdev
;
4292 for (i
= 0, tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
4293 if (tmpdev
->used
!= 1)
4295 if (tmpdev
->container
== 1) {
4297 int fd
= dev_open(tmpdev
->devname
, O_RDONLY
|O_EXCL
);
4299 pr_err("cannot open device %s: %s\n",
4300 tmpdev
->devname
, strerror(errno
));
4304 err
= get_sra_super_block(fd
, super_list
,
4305 tmpdev
->devname
, &lmax
,
4314 int major
= major(tmpdev
->st_rdev
);
4315 int minor
= minor(tmpdev
->st_rdev
);
4316 err
= get_super_block(super_list
,
4333 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
4334 int major
, int minor
, int keep_fd
)
4336 struct intel_super
*s
= NULL
;
4348 sprintf(nm
, "%d:%d", major
, minor
);
4349 dfd
= dev_open(nm
, O_RDWR
);
4355 find_intel_hba_capability(dfd
, s
, devname
);
4356 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4358 /* retry the load if we might have raced against mdmon */
4359 if (err
== 3 && devnm
&& mdmon_running(devnm
))
4360 for (retry
= 0; retry
< 3; retry
++) {
4362 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4368 s
->next
= *super_list
;
4376 if ((dfd
>= 0) && (!keep_fd
))
4383 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
)
4390 sra
= sysfs_read(fd
, NULL
, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
4394 if (sra
->array
.major_version
!= -1 ||
4395 sra
->array
.minor_version
!= -2 ||
4396 strcmp(sra
->text_version
, "imsm") != 0) {
4401 devnm
= fd2devnm(fd
);
4402 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
4403 if (get_super_block(super_list
, devnm
, devname
,
4404 sd
->disk
.major
, sd
->disk
.minor
, keep_fd
) != 0) {
4415 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
4417 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
, NULL
, 1);
4421 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
4423 struct intel_super
*super
;
4426 if (test_partition(fd
))
4427 /* IMSM not allowed on partitions */
4430 free_super_imsm(st
);
4432 super
= alloc_super();
4433 /* Load hba and capabilities if they exist.
4434 * But do not preclude loading metadata in case capabilities or hba are
4435 * non-compliant and ignore_hw_compat is set.
4437 rv
= find_intel_hba_capability(fd
, super
, devname
);
4438 /* no orom/efi or non-intel hba of the disk */
4439 if ((rv
!= 0) && (st
->ignore_hw_compat
== 0)) {
4441 pr_err("No OROM/EFI properties for %s\n", devname
);
4445 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
4449 pr_err("Failed to load all information "
4450 "sections on %s\n", devname
);
4456 if (st
->ss
== NULL
) {
4457 st
->ss
= &super_imsm
;
4458 st
->minor_version
= 0;
4459 st
->max_devs
= IMSM_MAX_DEVICES
;
4462 /* load migration record */
4463 if (load_imsm_migr_rec(super
, NULL
) == 0) {
4464 /* Check for unsupported migration features */
4465 if (check_mpb_migr_compatibility(super
) != 0) {
4466 pr_err("Unsupported migration detected");
4468 fprintf(stderr
, " on %s\n", devname
);
4470 fprintf(stderr
, " (IMSM).\n");
4478 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
4480 if (info
->level
== 1)
4482 return info
->chunk_size
>> 9;
4485 static unsigned long long info_to_blocks_per_member(mdu_array_info_t
*info
,
4486 unsigned long long size
)
4488 if (info
->level
== 1)
4491 return (size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
4494 static void imsm_update_version_info(struct intel_super
*super
)
4496 /* update the version and attributes */
4497 struct imsm_super
*mpb
= super
->anchor
;
4499 struct imsm_dev
*dev
;
4500 struct imsm_map
*map
;
4503 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4504 dev
= get_imsm_dev(super
, i
);
4505 map
= get_imsm_map(dev
, MAP_0
);
4506 if (__le32_to_cpu(dev
->size_high
) > 0)
4507 mpb
->attributes
|= MPB_ATTRIB_2TB
;
4509 /* FIXME detect when an array spans a port multiplier */
4511 mpb
->attributes
|= MPB_ATTRIB_PM
;
4514 if (mpb
->num_raid_devs
> 1 ||
4515 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
4516 version
= MPB_VERSION_ATTRIBS
;
4517 switch (get_imsm_raid_level(map
)) {
4518 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
4519 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
4520 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
4521 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
4524 if (map
->num_members
>= 5)
4525 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
4526 else if (dev
->status
== DEV_CLONE_N_GO
)
4527 version
= MPB_VERSION_CNG
;
4528 else if (get_imsm_raid_level(map
) == 5)
4529 version
= MPB_VERSION_RAID5
;
4530 else if (map
->num_members
>= 3)
4531 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
4532 else if (get_imsm_raid_level(map
) == 1)
4533 version
= MPB_VERSION_RAID1
;
4535 version
= MPB_VERSION_RAID0
;
4537 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
4541 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
4543 struct imsm_super
*mpb
= super
->anchor
;
4544 char *reason
= NULL
;
4547 if (strlen(name
) > MAX_RAID_SERIAL_LEN
)
4548 reason
= "must be 16 characters or less";
4550 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4551 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4553 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
4554 reason
= "already exists";
4559 if (reason
&& !quiet
)
4560 pr_err("imsm volume name %s\n", reason
);
4565 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
4566 unsigned long long size
, char *name
,
4567 char *homehost
, int *uuid
,
4568 long long data_offset
)
4570 /* We are creating a volume inside a pre-existing container.
4571 * so st->sb is already set.
4573 struct intel_super
*super
= st
->sb
;
4574 struct imsm_super
*mpb
= super
->anchor
;
4575 struct intel_dev
*dv
;
4576 struct imsm_dev
*dev
;
4577 struct imsm_vol
*vol
;
4578 struct imsm_map
*map
;
4579 int idx
= mpb
->num_raid_devs
;
4581 unsigned long long array_blocks
;
4582 size_t size_old
, size_new
;
4583 unsigned long long num_data_stripes
;
4585 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
4586 pr_err("This imsm-container already has the "
4587 "maximum of %d volumes\n", super
->orom
->vpa
);
4591 /* ensure the mpb is large enough for the new data */
4592 size_old
= __le32_to_cpu(mpb
->mpb_size
);
4593 size_new
= disks_to_mpb_size(info
->nr_disks
);
4594 if (size_new
> size_old
) {
4596 size_t size_round
= ROUND_UP(size_new
, 512);
4598 if (posix_memalign(&mpb_new
, 512, size_round
) != 0) {
4599 pr_err("could not allocate new mpb\n");
4602 if (posix_memalign(&super
->migr_rec_buf
, 512,
4603 MIGR_REC_BUF_SIZE
) != 0) {
4604 pr_err("%s could not allocate migr_rec buffer\n",
4611 memcpy(mpb_new
, mpb
, size_old
);
4614 super
->anchor
= mpb_new
;
4615 mpb
->mpb_size
= __cpu_to_le32(size_new
);
4616 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
4618 super
->current_vol
= idx
;
4620 /* handle 'failed_disks' by either:
4621 * a) create dummy disk entries in the table if this the first
4622 * volume in the array. We add them here as this is the only
4623 * opportunity to add them. add_to_super_imsm_volume()
4624 * handles the non-failed disks and continues incrementing
4626 * b) validate that 'failed_disks' matches the current number
4627 * of missing disks if the container is populated
4629 if (super
->current_vol
== 0) {
4631 for (i
= 0; i
< info
->failed_disks
; i
++) {
4632 struct imsm_disk
*disk
;
4635 disk
= __get_imsm_disk(mpb
, i
);
4636 disk
->status
= CONFIGURED_DISK
| FAILED_DISK
;
4637 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
4638 snprintf((char *) disk
->serial
, MAX_RAID_SERIAL_LEN
,
4641 find_missing(super
);
4646 for (d
= super
->missing
; d
; d
= d
->next
)
4648 if (info
->failed_disks
> missing
) {
4649 pr_err("unable to add 'missing' disk to container\n");
4654 if (!check_name(super
, name
, 0))
4656 dv
= xmalloc(sizeof(*dv
));
4657 dev
= xcalloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
4658 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
4659 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
4660 info
->layout
, info
->chunk_size
,
4662 /* round array size down to closest MB */
4663 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
4665 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
4666 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
4667 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
4669 vol
->migr_state
= 0;
4670 set_migr_type(dev
, MIGR_INIT
);
4671 vol
->dirty
= !info
->state
;
4672 vol
->curr_migr_unit
= 0;
4673 map
= get_imsm_map(dev
, MAP_0
);
4674 set_pba_of_lba0(map
, super
->create_offset
);
4675 set_blocks_per_member(map
, info_to_blocks_per_member(info
, size
));
4676 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
4677 map
->failed_disk_num
= ~0;
4678 if (info
->level
> 0)
4679 map
->map_state
= (info
->state
? IMSM_T_STATE_NORMAL
4680 : IMSM_T_STATE_UNINITIALIZED
);
4682 map
->map_state
= info
->failed_disks
? IMSM_T_STATE_FAILED
:
4683 IMSM_T_STATE_NORMAL
;
4686 if (info
->level
== 1 && info
->raid_disks
> 2) {
4689 pr_err("imsm does not support more than 2 disks"
4690 "in a raid1 volume\n");
4694 map
->raid_level
= info
->level
;
4695 if (info
->level
== 10) {
4696 map
->raid_level
= 1;
4697 map
->num_domains
= info
->raid_disks
/ 2;
4698 } else if (info
->level
== 1)
4699 map
->num_domains
= info
->raid_disks
;
4701 map
->num_domains
= 1;
4703 /* info->size is only int so use the 'size' parameter instead */
4704 num_data_stripes
= (size
* 2) / info_to_blocks_per_strip(info
);
4705 num_data_stripes
/= map
->num_domains
;
4706 set_num_data_stripes(map
, num_data_stripes
);
4708 map
->num_members
= info
->raid_disks
;
4709 for (i
= 0; i
< map
->num_members
; i
++) {
4710 /* initialized in add_to_super */
4711 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
4713 mpb
->num_raid_devs
++;
4716 dv
->index
= super
->current_vol
;
4717 dv
->next
= super
->devlist
;
4718 super
->devlist
= dv
;
4720 imsm_update_version_info(super
);
4725 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
4726 unsigned long long size
, char *name
,
4727 char *homehost
, int *uuid
,
4728 unsigned long long data_offset
)
4730 /* This is primarily called by Create when creating a new array.
4731 * We will then get add_to_super called for each component, and then
4732 * write_init_super called to write it out to each device.
4733 * For IMSM, Create can create on fresh devices or on a pre-existing
4735 * To create on a pre-existing array a different method will be called.
4736 * This one is just for fresh drives.
4738 struct intel_super
*super
;
4739 struct imsm_super
*mpb
;
4743 if (data_offset
!= INVALID_SECTORS
) {
4744 pr_err("data-offset not supported by imsm\n");
4749 return init_super_imsm_volume(st
, info
, size
, name
, homehost
, uuid
,
4753 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
4757 super
= alloc_super();
4758 if (super
&& posix_memalign(&super
->buf
, 512, mpb_size
) != 0) {
4763 pr_err("%s could not allocate superblock\n", __func__
);
4766 if (posix_memalign(&super
->migr_rec_buf
, 512, MIGR_REC_BUF_SIZE
) != 0) {
4767 pr_err("%s could not allocate migr_rec buffer\n", __func__
);
4772 memset(super
->buf
, 0, mpb_size
);
4774 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
4778 /* zeroing superblock */
4782 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
4784 version
= (char *) mpb
->sig
;
4785 strcpy(version
, MPB_SIGNATURE
);
4786 version
+= strlen(MPB_SIGNATURE
);
4787 strcpy(version
, MPB_VERSION_RAID0
);
4793 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
4794 int fd
, char *devname
)
4796 struct intel_super
*super
= st
->sb
;
4797 struct imsm_super
*mpb
= super
->anchor
;
4798 struct imsm_disk
*_disk
;
4799 struct imsm_dev
*dev
;
4800 struct imsm_map
*map
;
4804 dev
= get_imsm_dev(super
, super
->current_vol
);
4805 map
= get_imsm_map(dev
, MAP_0
);
4807 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
4808 pr_err("%s: Cannot add spare devices to IMSM volume\n",
4814 /* we're doing autolayout so grab the pre-marked (in
4815 * validate_geometry) raid_disk
4817 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4818 if (dl
->raiddisk
== dk
->raid_disk
)
4821 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4822 if (dl
->major
== dk
->major
&&
4823 dl
->minor
== dk
->minor
)
4828 pr_err("%s is not a member of the same container\n", devname
);
4832 /* add a pristine spare to the metadata */
4833 if (dl
->index
< 0) {
4834 dl
->index
= super
->anchor
->num_disks
;
4835 super
->anchor
->num_disks
++;
4837 /* Check the device has not already been added */
4838 slot
= get_imsm_disk_slot(map
, dl
->index
);
4840 (get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
) & IMSM_ORD_REBUILD
) == 0) {
4841 pr_err("%s has been included in this array twice\n",
4845 set_imsm_ord_tbl_ent(map
, dk
->raid_disk
, dl
->index
);
4846 dl
->disk
.status
= CONFIGURED_DISK
;
4848 /* update size of 'missing' disks to be at least as large as the
4849 * largest acitve member (we only have dummy missing disks when
4850 * creating the first volume)
4852 if (super
->current_vol
== 0) {
4853 for (df
= super
->missing
; df
; df
= df
->next
) {
4854 if (total_blocks(&dl
->disk
) > total_blocks(&df
->disk
))
4855 set_total_blocks(&df
->disk
, total_blocks(&dl
->disk
));
4856 _disk
= __get_imsm_disk(mpb
, df
->index
);
4861 /* refresh unset/failed slots to point to valid 'missing' entries */
4862 for (df
= super
->missing
; df
; df
= df
->next
)
4863 for (slot
= 0; slot
< mpb
->num_disks
; slot
++) {
4864 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
4866 if ((ord
& IMSM_ORD_REBUILD
) == 0)
4868 set_imsm_ord_tbl_ent(map
, slot
, df
->index
| IMSM_ORD_REBUILD
);
4869 if (is_gen_migration(dev
)) {
4870 struct imsm_map
*map2
= get_imsm_map(dev
,
4872 int slot2
= get_imsm_disk_slot(map2
, df
->index
);
4873 if ((slot2
< map2
->num_members
) &&
4875 __u32 ord2
= get_imsm_ord_tbl_ent(dev
,
4878 if ((unsigned)df
->index
==
4880 set_imsm_ord_tbl_ent(map2
,
4886 dprintf("set slot:%d to missing disk:%d\n", slot
, df
->index
);
4890 /* if we are creating the first raid device update the family number */
4891 if (super
->current_vol
== 0) {
4893 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
4895 _disk
= __get_imsm_disk(mpb
, dl
->index
);
4896 if (!_dev
|| !_disk
) {
4897 pr_err("BUG mpb setup error\n");
4903 sum
+= __gen_imsm_checksum(mpb
);
4904 mpb
->family_num
= __cpu_to_le32(sum
);
4905 mpb
->orig_family_num
= mpb
->family_num
;
4907 super
->current_disk
= dl
;
4912 * Function marks disk as spare and restores disk serial
4913 * in case it was previously marked as failed by takeover operation
4915 * -1 : critical error
4916 * 0 : disk is marked as spare but serial is not set
4919 int mark_spare(struct dl
*disk
)
4921 __u8 serial
[MAX_RAID_SERIAL_LEN
];
4928 if (!imsm_read_serial(disk
->fd
, NULL
, serial
)) {
4929 /* Restore disk serial number, because takeover marks disk
4930 * as failed and adds to serial ':0' before it becomes
4933 serialcpy(disk
->serial
, serial
);
4934 serialcpy(disk
->disk
.serial
, serial
);
4937 disk
->disk
.status
= SPARE_DISK
;
4943 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
4944 int fd
, char *devname
,
4945 unsigned long long data_offset
)
4947 struct intel_super
*super
= st
->sb
;
4949 unsigned long long size
;
4954 /* If we are on an RAID enabled platform check that the disk is
4955 * attached to the raid controller.
4956 * We do not need to test disks attachment for container based additions,
4957 * they shall be already tested when container was created/assembled.
4959 rv
= find_intel_hba_capability(fd
, super
, devname
);
4960 /* no orom/efi or non-intel hba of the disk */
4962 dprintf("capability: %p fd: %d ret: %d\n",
4963 super
->orom
, fd
, rv
);
4967 if (super
->current_vol
>= 0)
4968 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
4971 dd
= xcalloc(sizeof(*dd
), 1);
4972 dd
->major
= major(stb
.st_rdev
);
4973 dd
->minor
= minor(stb
.st_rdev
);
4974 dd
->devname
= devname
? xstrdup(devname
) : NULL
;
4977 dd
->action
= DISK_ADD
;
4978 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
4980 pr_err("failed to retrieve scsi serial, aborting\n");
4985 get_dev_size(fd
, NULL
, &size
);
4987 serialcpy(dd
->disk
.serial
, dd
->serial
);
4988 set_total_blocks(&dd
->disk
, size
);
4989 if (__le32_to_cpu(dd
->disk
.total_blocks_hi
) > 0) {
4990 struct imsm_super
*mpb
= super
->anchor
;
4991 mpb
->attributes
|= MPB_ATTRIB_2TB_DISK
;
4994 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
4995 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
4997 dd
->disk
.scsi_id
= __cpu_to_le32(0);
4999 if (st
->update_tail
) {
5000 dd
->next
= super
->disk_mgmt_list
;
5001 super
->disk_mgmt_list
= dd
;
5003 dd
->next
= super
->disks
;
5005 super
->updates_pending
++;
5011 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
5013 struct intel_super
*super
= st
->sb
;
5016 /* remove from super works only in mdmon - for communication
5017 * manager - monitor. Check if communication memory buffer
5020 if (!st
->update_tail
) {
5021 pr_err("%s shall be used in mdmon context only"
5022 "(line %d).\n", __func__
, __LINE__
);
5025 dd
= xcalloc(1, sizeof(*dd
));
5026 dd
->major
= dk
->major
;
5027 dd
->minor
= dk
->minor
;
5030 dd
->action
= DISK_REMOVE
;
5032 dd
->next
= super
->disk_mgmt_list
;
5033 super
->disk_mgmt_list
= dd
;
5038 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
5042 struct imsm_super anchor
;
5043 } spare_record
__attribute__ ((aligned(512)));
5045 /* spare records have their own family number and do not have any defined raid
5048 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
5050 struct imsm_super
*mpb
= super
->anchor
;
5051 struct imsm_super
*spare
= &spare_record
.anchor
;
5055 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
)),
5056 spare
->generation_num
= __cpu_to_le32(1UL),
5057 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5058 spare
->num_disks
= 1,
5059 spare
->num_raid_devs
= 0,
5060 spare
->cache_size
= mpb
->cache_size
,
5061 spare
->pwr_cycle_count
= __cpu_to_le32(1),
5063 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
5064 MPB_SIGNATURE MPB_VERSION_RAID0
);
5066 for (d
= super
->disks
; d
; d
= d
->next
) {
5070 spare
->disk
[0] = d
->disk
;
5071 if (__le32_to_cpu(d
->disk
.total_blocks_hi
) > 0)
5072 spare
->attributes
|= MPB_ATTRIB_2TB_DISK
;
5074 sum
= __gen_imsm_checksum(spare
);
5075 spare
->family_num
= __cpu_to_le32(sum
);
5076 spare
->orig_family_num
= 0;
5077 sum
= __gen_imsm_checksum(spare
);
5078 spare
->check_sum
= __cpu_to_le32(sum
);
5080 if (store_imsm_mpb(d
->fd
, spare
)) {
5081 pr_err("%s: failed for device %d:%d %s\n",
5082 __func__
, d
->major
, d
->minor
, strerror(errno
));
5094 static int write_super_imsm(struct supertype
*st
, int doclose
)
5096 struct intel_super
*super
= st
->sb
;
5097 struct imsm_super
*mpb
= super
->anchor
;
5103 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
5105 int clear_migration_record
= 1;
5107 /* 'generation' is incremented everytime the metadata is written */
5108 generation
= __le32_to_cpu(mpb
->generation_num
);
5110 mpb
->generation_num
= __cpu_to_le32(generation
);
5112 /* fix up cases where previous mdadm releases failed to set
5115 if (mpb
->orig_family_num
== 0)
5116 mpb
->orig_family_num
= mpb
->family_num
;
5118 for (d
= super
->disks
; d
; d
= d
->next
) {
5122 mpb
->disk
[d
->index
] = d
->disk
;
5126 for (d
= super
->missing
; d
; d
= d
->next
) {
5127 mpb
->disk
[d
->index
] = d
->disk
;
5130 mpb
->num_disks
= num_disks
;
5131 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
5133 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5134 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
5135 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
5137 imsm_copy_dev(dev
, dev2
);
5138 mpb_size
+= sizeof_imsm_dev(dev
, 0);
5140 if (is_gen_migration(dev2
))
5141 clear_migration_record
= 0;
5143 mpb_size
+= __le32_to_cpu(mpb
->bbm_log_size
);
5144 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5146 /* recalculate checksum */
5147 sum
= __gen_imsm_checksum(mpb
);
5148 mpb
->check_sum
= __cpu_to_le32(sum
);
5150 if (super
->clean_migration_record_by_mdmon
) {
5151 clear_migration_record
= 1;
5152 super
->clean_migration_record_by_mdmon
= 0;
5154 if (clear_migration_record
)
5155 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SIZE
);
5157 /* write the mpb for disks that compose raid devices */
5158 for (d
= super
->disks
; d
; d
= d
->next
) {
5159 if (d
->index
< 0 || is_failed(&d
->disk
))
5162 if (clear_migration_record
) {
5163 unsigned long long dsize
;
5165 get_dev_size(d
->fd
, NULL
, &dsize
);
5166 if (lseek64(d
->fd
, dsize
- 512, SEEK_SET
) >= 0) {
5167 if (write(d
->fd
, super
->migr_rec_buf
,
5168 MIGR_REC_BUF_SIZE
) != MIGR_REC_BUF_SIZE
)
5169 perror("Write migr_rec failed");
5173 if (store_imsm_mpb(d
->fd
, mpb
))
5175 "%s: failed for device %d:%d (fd: %d)%s\n",
5176 __func__
, d
->major
, d
->minor
,
5177 d
->fd
, strerror(errno
));
5186 return write_super_imsm_spares(super
, doclose
);
5191 static int create_array(struct supertype
*st
, int dev_idx
)
5194 struct imsm_update_create_array
*u
;
5195 struct intel_super
*super
= st
->sb
;
5196 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
5197 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
5198 struct disk_info
*inf
;
5199 struct imsm_disk
*disk
;
5202 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
5203 sizeof(*inf
) * map
->num_members
;
5205 u
->type
= update_create_array
;
5206 u
->dev_idx
= dev_idx
;
5207 imsm_copy_dev(&u
->dev
, dev
);
5208 inf
= get_disk_info(u
);
5209 for (i
= 0; i
< map
->num_members
; i
++) {
5210 int idx
= get_imsm_disk_idx(dev
, i
, MAP_X
);
5212 disk
= get_imsm_disk(super
, idx
);
5213 serialcpy(inf
[i
].serial
, disk
->serial
);
5215 append_metadata_update(st
, u
, len
);
5220 static int mgmt_disk(struct supertype
*st
)
5222 struct intel_super
*super
= st
->sb
;
5224 struct imsm_update_add_remove_disk
*u
;
5226 if (!super
->disk_mgmt_list
)
5231 u
->type
= update_add_remove_disk
;
5232 append_metadata_update(st
, u
, len
);
5237 static int write_init_super_imsm(struct supertype
*st
)
5239 struct intel_super
*super
= st
->sb
;
5240 int current_vol
= super
->current_vol
;
5242 /* we are done with current_vol reset it to point st at the container */
5243 super
->current_vol
= -1;
5245 if (st
->update_tail
) {
5246 /* queue the recently created array / added disk
5247 * as a metadata update */
5250 /* determine if we are creating a volume or adding a disk */
5251 if (current_vol
< 0) {
5252 /* in the mgmt (add/remove) disk case we are running
5253 * in mdmon context, so don't close fd's
5255 return mgmt_disk(st
);
5257 rv
= create_array(st
, current_vol
);
5262 for (d
= super
->disks
; d
; d
= d
->next
)
5263 Kill(d
->devname
, NULL
, 0, -1, 1);
5264 return write_super_imsm(st
, 1);
5269 static int store_super_imsm(struct supertype
*st
, int fd
)
5271 struct intel_super
*super
= st
->sb
;
5272 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
5278 return store_imsm_mpb(fd
, mpb
);
5284 static int imsm_bbm_log_size(struct imsm_super
*mpb
)
5286 return __le32_to_cpu(mpb
->bbm_log_size
);
5290 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
5291 int layout
, int raiddisks
, int chunk
,
5292 unsigned long long size
,
5293 unsigned long long data_offset
,
5295 unsigned long long *freesize
,
5299 unsigned long long ldsize
;
5300 struct intel_super
*super
=NULL
;
5303 if (level
!= LEVEL_CONTAINER
)
5308 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
5311 pr_err("imsm: Cannot open %s: %s\n",
5312 dev
, strerror(errno
));
5315 if (!get_dev_size(fd
, dev
, &ldsize
)) {
5320 /* capabilities retrieve could be possible
5321 * note that there is no fd for the disks in array.
5323 super
= alloc_super();
5324 rv
= find_intel_hba_capability(fd
, super
, verbose
> 0 ? dev
: NULL
);
5328 fd2devname(fd
, str
);
5329 dprintf("validate_geometry_imsm_container: fd: %d %s orom: %p rv: %d raiddisk: %d\n",
5330 fd
, str
, super
->orom
, rv
, raiddisks
);
5332 /* no orom/efi or non-intel hba of the disk */
5339 if (raiddisks
> super
->orom
->tds
) {
5341 pr_err("%d exceeds maximum number of"
5342 " platform supported disks: %d\n",
5343 raiddisks
, super
->orom
->tds
);
5347 if ((super
->orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) == 0 &&
5348 (ldsize
>> 9) >> 32 > 0) {
5350 pr_err("%s exceeds maximum platform supported size\n", dev
);
5356 *freesize
= avail_size_imsm(st
, ldsize
>> 9, data_offset
);
5362 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
5364 const unsigned long long base_start
= e
[*idx
].start
;
5365 unsigned long long end
= base_start
+ e
[*idx
].size
;
5368 if (base_start
== end
)
5372 for (i
= *idx
; i
< num_extents
; i
++) {
5373 /* extend overlapping extents */
5374 if (e
[i
].start
>= base_start
&&
5375 e
[i
].start
<= end
) {
5378 if (e
[i
].start
+ e
[i
].size
> end
)
5379 end
= e
[i
].start
+ e
[i
].size
;
5380 } else if (e
[i
].start
> end
) {
5386 return end
- base_start
;
5389 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
5391 /* build a composite disk with all known extents and generate a new
5392 * 'maxsize' given the "all disks in an array must share a common start
5393 * offset" constraint
5395 struct extent
*e
= xcalloc(sum_extents
, sizeof(*e
));
5399 unsigned long long pos
;
5400 unsigned long long start
= 0;
5401 unsigned long long maxsize
;
5402 unsigned long reserve
;
5404 /* coalesce and sort all extents. also, check to see if we need to
5405 * reserve space between member arrays
5408 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5411 for (i
= 0; i
< dl
->extent_cnt
; i
++)
5414 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
5419 while (i
< sum_extents
) {
5420 e
[j
].start
= e
[i
].start
;
5421 e
[j
].size
= find_size(e
, &i
, sum_extents
);
5423 if (e
[j
-1].size
== 0)
5432 unsigned long long esize
;
5434 esize
= e
[i
].start
- pos
;
5435 if (esize
>= maxsize
) {
5440 pos
= e
[i
].start
+ e
[i
].size
;
5442 } while (e
[i
-1].size
);
5448 /* FIXME assumes volume at offset 0 is the first volume in a
5451 if (start_extent
> 0)
5452 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
5456 if (maxsize
< reserve
)
5459 super
->create_offset
= ~((unsigned long long) 0);
5460 if (start
+ reserve
> super
->create_offset
)
5461 return 0; /* start overflows create_offset */
5462 super
->create_offset
= start
+ reserve
;
5464 return maxsize
- reserve
;
5467 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
5469 if (level
< 0 || level
== 6 || level
== 4)
5472 /* if we have an orom prevent invalid raid levels */
5475 case 0: return imsm_orom_has_raid0(orom
);
5478 return imsm_orom_has_raid1e(orom
);
5479 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
5480 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
5481 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
5484 return 1; /* not on an Intel RAID platform so anything goes */
5490 active_arrays_by_format(char *name
, char* hba
, struct md_list
**devlist
,
5491 int dpa
, int verbose
)
5493 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
5494 struct mdstat_ent
*memb
= NULL
;
5497 struct md_list
*dv
= NULL
;
5500 for (memb
= mdstat
; memb
; memb
= memb
->next
) {
5501 if (memb
->metadata_version
&&
5502 (strncmp(memb
->metadata_version
, "external:", 9) == 0) &&
5503 (strcmp(&memb
->metadata_version
[9], name
) == 0) &&
5504 !is_subarray(memb
->metadata_version
+9) &&
5506 struct dev_member
*dev
= memb
->members
;
5508 while(dev
&& (fd
< 0)) {
5509 char *path
= xmalloc(strlen(dev
->name
) + strlen("/dev/") + 1);
5510 num
= sprintf(path
, "%s%s", "/dev/", dev
->name
);
5512 fd
= open(path
, O_RDONLY
, 0);
5513 if ((num
<= 0) || (fd
< 0)) {
5514 pr_vrb(": Cannot open %s: %s\n",
5515 dev
->name
, strerror(errno
));
5521 if ((fd
>= 0) && disk_attached_to_hba(fd
, hba
)) {
5522 struct mdstat_ent
*vol
;
5523 for (vol
= mdstat
; vol
; vol
= vol
->next
) {
5524 if ((vol
->active
> 0) &&
5525 vol
->metadata_version
&&
5526 is_container_member(vol
, memb
->dev
)) {
5531 if (*devlist
&& (found
< dpa
)) {
5532 dv
= xcalloc(1, sizeof(*dv
));
5533 dv
->devname
= xmalloc(strlen(memb
->dev
) + strlen("/dev/") + 1);
5534 sprintf(dv
->devname
, "%s%s", "/dev/", memb
->dev
);
5537 dv
->next
= *devlist
;
5545 free_mdstat(mdstat
);
5550 static struct md_list
*
5551 get_loop_devices(void)
5554 struct md_list
*devlist
= NULL
;
5555 struct md_list
*dv
= NULL
;
5557 for(i
= 0; i
< 12; i
++) {
5558 dv
= xcalloc(1, sizeof(*dv
));
5559 dv
->devname
= xmalloc(40);
5560 sprintf(dv
->devname
, "/dev/loop%d", i
);
5568 static struct md_list
*
5569 get_devices(const char *hba_path
)
5571 struct md_list
*devlist
= NULL
;
5572 struct md_list
*dv
= NULL
;
5578 devlist
= get_loop_devices();
5581 /* scroll through /sys/dev/block looking for devices attached to
5584 dir
= opendir("/sys/dev/block");
5585 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
5590 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
5592 path
= devt_to_devpath(makedev(major
, minor
));
5595 if (!path_attached_to_hba(path
, hba_path
)) {
5602 fd
= dev_open(ent
->d_name
, O_RDONLY
);
5604 fd2devname(fd
, buf
);
5607 pr_err("cannot open device: %s\n",
5612 dv
= xcalloc(1, sizeof(*dv
));
5613 dv
->devname
= xstrdup(buf
);
5620 devlist
= devlist
->next
;
5630 count_volumes_list(struct md_list
*devlist
, char *homehost
,
5631 int verbose
, int *found
)
5633 struct md_list
*tmpdev
;
5635 struct supertype
*st
= NULL
;
5637 /* first walk the list of devices to find a consistent set
5638 * that match the criterea, if that is possible.
5639 * We flag the ones we like with 'used'.
5642 st
= match_metadata_desc_imsm("imsm");
5644 pr_vrb(": cannot allocate memory for imsm supertype\n");
5648 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5649 char *devname
= tmpdev
->devname
;
5651 struct supertype
*tst
;
5653 if (tmpdev
->used
> 1)
5655 tst
= dup_super(st
);
5657 pr_vrb(": cannot allocate memory for imsm supertype\n");
5660 tmpdev
->container
= 0;
5661 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
5663 dprintf(": cannot open device %s: %s\n",
5664 devname
, strerror(errno
));
5666 } else if (fstat(dfd
, &stb
)< 0) {
5668 dprintf(": fstat failed for %s: %s\n",
5669 devname
, strerror(errno
));
5671 } else if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
5672 dprintf(": %s is not a block device.\n",
5675 } else if (must_be_container(dfd
)) {
5676 struct supertype
*cst
;
5677 cst
= super_by_fd(dfd
, NULL
);
5679 dprintf(": cannot recognize container type %s\n",
5682 } else if (tst
->ss
!= st
->ss
) {
5683 dprintf(": non-imsm container - ignore it: %s\n",
5686 } else if (!tst
->ss
->load_container
||
5687 tst
->ss
->load_container(tst
, dfd
, NULL
))
5690 tmpdev
->container
= 1;
5693 cst
->ss
->free_super(cst
);
5695 tmpdev
->st_rdev
= stb
.st_rdev
;
5696 if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
5697 dprintf(": no RAID superblock on %s\n",
5700 } else if (tst
->ss
->compare_super
== NULL
) {
5701 dprintf(": Cannot assemble %s metadata on %s\n",
5702 tst
->ss
->name
, devname
);
5708 if (tmpdev
->used
== 2 || tmpdev
->used
== 4) {
5709 /* Ignore unrecognised devices during auto-assembly */
5714 tst
->ss
->getinfo_super(tst
, &info
, NULL
);
5716 if (st
->minor_version
== -1)
5717 st
->minor_version
= tst
->minor_version
;
5719 if (memcmp(info
.uuid
, uuid_zero
,
5720 sizeof(int[4])) == 0) {
5721 /* this is a floating spare. It cannot define
5722 * an array unless there are no more arrays of
5723 * this type to be found. It can be included
5724 * in an array of this type though.
5730 if (st
->ss
!= tst
->ss
||
5731 st
->minor_version
!= tst
->minor_version
||
5732 st
->ss
->compare_super(st
, tst
) != 0) {
5733 /* Some mismatch. If exactly one array matches this host,
5734 * we can resolve on that one.
5735 * Or, if we are auto assembling, we just ignore the second
5738 dprintf(": superblock on %s doesn't match others - assembly aborted\n",
5744 dprintf("found: devname: %s\n", devname
);
5748 tst
->ss
->free_super(tst
);
5752 if ((err
= load_super_imsm_all(st
, -1, &st
->sb
, NULL
, devlist
, 0)) == 0) {
5753 struct mdinfo
*iter
, *head
= st
->ss
->container_content(st
, NULL
);
5754 for (iter
= head
; iter
; iter
= iter
->next
) {
5755 dprintf("content->text_version: %s vol\n",
5756 iter
->text_version
);
5757 if (iter
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
5758 /* do not assemble arrays with unsupported
5760 dprintf(": Cannot activate member %s.\n",
5761 iter
->text_version
);
5768 dprintf(" no valid super block on device list: err: %d %p\n",
5772 dprintf(" no more devices to examin\n");
5775 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5776 if ((tmpdev
->used
== 1) && (tmpdev
->found
)) {
5778 if (count
< tmpdev
->found
)
5781 count
-= tmpdev
->found
;
5784 if (tmpdev
->used
== 1)
5789 st
->ss
->free_super(st
);
5794 count_volumes(char *hba
, int dpa
, int verbose
)
5796 struct md_list
*devlist
= NULL
;
5800 devlist
= get_devices(hba
);
5801 /* if no intel devices return zero volumes */
5802 if (devlist
== NULL
)
5805 count
= active_arrays_by_format("imsm", hba
, &devlist
, dpa
, verbose
);
5806 dprintf(" path: %s active arrays: %d\n", hba
, count
);
5807 if (devlist
== NULL
)
5811 count
+= count_volumes_list(devlist
,
5815 dprintf("found %d count: %d\n", found
, count
);
5818 dprintf("path: %s total number of volumes: %d\n", hba
, count
);
5821 struct md_list
*dv
= devlist
;
5822 devlist
= devlist
->next
;
5829 static int imsm_default_chunk(const struct imsm_orom
*orom
)
5831 /* up to 512 if the plaform supports it, otherwise the platform max.
5832 * 128 if no platform detected
5834 int fs
= max(7, orom
? fls(orom
->sss
) : 0);
5836 return min(512, (1 << fs
));
5840 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
5841 int raiddisks
, int *chunk
, unsigned long long size
, int verbose
)
5843 /* check/set platform and metadata limits/defaults */
5844 if (super
->orom
&& raiddisks
> super
->orom
->dpa
) {
5845 pr_vrb(": platform supports a maximum of %d disks per array\n",
5850 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
5851 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
5852 pr_vrb(": platform does not support raid%d with %d disk%s\n",
5853 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
5857 if (*chunk
== 0 || *chunk
== UnSet
)
5858 *chunk
= imsm_default_chunk(super
->orom
);
5860 if (super
->orom
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
5861 pr_vrb(": platform does not support a chunk size of: "
5866 if (layout
!= imsm_level_to_layout(level
)) {
5868 pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
5869 else if (level
== 10)
5870 pr_vrb(": imsm raid 10 only supports the n2 layout\n");
5872 pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
5877 if (super
->orom
&& (super
->orom
->attr
& IMSM_OROM_ATTR_2TB
) == 0 &&
5878 (calc_array_size(level
, raiddisks
, layout
, *chunk
, size
) >> 32) > 0) {
5879 pr_vrb(": platform does not support a volume size over 2TB\n");
5885 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
5886 * FIX ME add ahci details
5888 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
5889 int layout
, int raiddisks
, int *chunk
,
5890 unsigned long long size
,
5891 unsigned long long data_offset
,
5893 unsigned long long *freesize
,
5897 struct intel_super
*super
= st
->sb
;
5898 struct imsm_super
*mpb
;
5900 unsigned long long pos
= 0;
5901 unsigned long long maxsize
;
5905 /* We must have the container info already read in. */
5909 mpb
= super
->anchor
;
5911 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, size
, verbose
)) {
5912 pr_err("RAID gemetry validation failed. "
5913 "Cannot proceed with the action(s).\n");
5917 /* General test: make sure there is space for
5918 * 'raiddisks' device extents of size 'size' at a given
5921 unsigned long long minsize
= size
;
5922 unsigned long long start_offset
= MaxSector
;
5925 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
5926 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5931 e
= get_extents(super
, dl
);
5934 unsigned long long esize
;
5935 esize
= e
[i
].start
- pos
;
5936 if (esize
>= minsize
)
5938 if (found
&& start_offset
== MaxSector
) {
5941 } else if (found
&& pos
!= start_offset
) {
5945 pos
= e
[i
].start
+ e
[i
].size
;
5947 } while (e
[i
-1].size
);
5952 if (dcnt
< raiddisks
) {
5954 pr_err("imsm: Not enough "
5955 "devices with space for this array "
5963 /* This device must be a member of the set */
5964 if (stat(dev
, &stb
) < 0)
5966 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
5968 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5969 if (dl
->major
== (int)major(stb
.st_rdev
) &&
5970 dl
->minor
== (int)minor(stb
.st_rdev
))
5975 pr_err("%s is not in the "
5976 "same imsm set\n", dev
);
5978 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
5979 /* If a volume is present then the current creation attempt
5980 * cannot incorporate new spares because the orom may not
5981 * understand this configuration (all member disks must be
5982 * members of each array in the container).
5984 pr_err("%s is a spare and a volume"
5985 " is already defined for this container\n", dev
);
5986 pr_err("The option-rom requires all member"
5987 " disks to be a member of all volumes\n");
5989 } else if (super
->orom
&& mpb
->num_raid_devs
> 0 &&
5990 mpb
->num_disks
!= raiddisks
) {
5991 pr_err("The option-rom requires all member"
5992 " disks to be a member of all volumes\n");
5996 /* retrieve the largest free space block */
5997 e
= get_extents(super
, dl
);
6002 unsigned long long esize
;
6004 esize
= e
[i
].start
- pos
;
6005 if (esize
>= maxsize
)
6007 pos
= e
[i
].start
+ e
[i
].size
;
6009 } while (e
[i
-1].size
);
6014 pr_err("unable to determine free space for: %s\n",
6018 if (maxsize
< size
) {
6020 pr_err("%s not enough space (%llu < %llu)\n",
6021 dev
, maxsize
, size
);
6025 /* count total number of extents for merge */
6027 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6029 i
+= dl
->extent_cnt
;
6031 maxsize
= merge_extents(super
, i
);
6033 if (!check_env("IMSM_NO_PLATFORM") &&
6034 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6035 pr_err("attempting to create a second "
6036 "volume with size less then remaining space. "
6041 if (maxsize
< size
|| maxsize
== 0) {
6044 pr_err("no free space"
6045 " left on device. Aborting...\n");
6047 pr_err("not enough space"
6048 " to create volume of given size"
6049 " (%llu < %llu). Aborting...\n",
6055 *freesize
= maxsize
;
6058 int count
= count_volumes(super
->hba
->path
,
6059 super
->orom
->dpa
, verbose
);
6060 if (super
->orom
->vphba
<= count
) {
6061 pr_vrb(": platform does not support more than %d raid volumes.\n",
6062 super
->orom
->vphba
);
6069 static int imsm_get_free_size(struct supertype
*st
, int raiddisks
,
6070 unsigned long long size
, int chunk
,
6071 unsigned long long *freesize
)
6073 struct intel_super
*super
= st
->sb
;
6074 struct imsm_super
*mpb
= super
->anchor
;
6079 unsigned long long maxsize
;
6080 unsigned long long minsize
;
6084 /* find the largest common start free region of the possible disks */
6088 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6094 /* don't activate new spares if we are orom constrained
6095 * and there is already a volume active in the container
6097 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
6100 e
= get_extents(super
, dl
);
6103 for (i
= 1; e
[i
-1].size
; i
++)
6111 maxsize
= merge_extents(super
, extent_cnt
);
6115 minsize
= chunk
* 2;
6117 if (cnt
< raiddisks
||
6118 (super
->orom
&& used
&& used
!= raiddisks
) ||
6119 maxsize
< minsize
||
6121 pr_err("not enough devices with space to create array.\n");
6122 return 0; /* No enough free spaces large enough */
6133 if (!check_env("IMSM_NO_PLATFORM") &&
6134 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6135 pr_err("attempting to create a second "
6136 "volume with size less then remaining space. "
6141 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6143 dl
->raiddisk
= cnt
++;
6147 dprintf("imsm: imsm_get_free_size() returns : %llu\n", size
);
6152 static int reserve_space(struct supertype
*st
, int raiddisks
,
6153 unsigned long long size
, int chunk
,
6154 unsigned long long *freesize
)
6156 struct intel_super
*super
= st
->sb
;
6161 rv
= imsm_get_free_size(st
, raiddisks
, size
, chunk
, freesize
);
6164 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6166 dl
->raiddisk
= cnt
++;
6173 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
6174 int raiddisks
, int *chunk
, unsigned long long size
,
6175 unsigned long long data_offset
,
6176 char *dev
, unsigned long long *freesize
,
6184 * if given unused devices create a container
6185 * if given given devices in a container create a member volume
6187 if (level
== LEVEL_CONTAINER
) {
6188 /* Must be a fresh device to add to a container */
6189 return validate_geometry_imsm_container(st
, level
, layout
,
6199 struct intel_super
*super
= st
->sb
;
6200 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
6201 raiddisks
, chunk
, size
,
6204 /* we are being asked to automatically layout a
6205 * new volume based on the current contents of
6206 * the container. If the the parameters can be
6207 * satisfied reserve_space will record the disks,
6208 * start offset, and size of the volume to be
6209 * created. add_to_super and getinfo_super
6210 * detect when autolayout is in progress.
6212 /* assuming that freesize is always given when array is
6214 if (super
->orom
&& freesize
) {
6216 count
= count_volumes(super
->hba
->path
,
6217 super
->orom
->dpa
, verbose
);
6218 if (super
->orom
->vphba
<= count
) {
6219 pr_vrb(": platform does not support more"
6220 " than %d raid volumes.\n",
6221 super
->orom
->vphba
);
6226 return reserve_space(st
, raiddisks
, size
,
6232 /* creating in a given container */
6233 return validate_geometry_imsm_volume(st
, level
, layout
,
6234 raiddisks
, chunk
, size
,
6236 dev
, freesize
, verbose
);
6239 /* This device needs to be a device in an 'imsm' container */
6240 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
6243 pr_err("Cannot create this array on device %s\n",
6248 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
6250 pr_err("Cannot open %s: %s\n",
6251 dev
, strerror(errno
));
6254 /* Well, it is in use by someone, maybe an 'imsm' container. */
6255 cfd
= open_container(fd
);
6259 pr_err("Cannot use %s: It is busy\n",
6263 sra
= sysfs_read(cfd
, NULL
, GET_VERSION
);
6264 if (sra
&& sra
->array
.major_version
== -1 &&
6265 strcmp(sra
->text_version
, "imsm") == 0)
6269 /* This is a member of a imsm container. Load the container
6270 * and try to create a volume
6272 struct intel_super
*super
;
6274 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
, NULL
, 1) == 0) {
6276 strcpy(st
->container_devnm
, fd2devnm(cfd
));
6278 return validate_geometry_imsm_volume(st
, level
, layout
,
6280 size
, data_offset
, dev
,
6287 pr_err("failed container membership check\n");
6293 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
6295 struct intel_super
*super
= st
->sb
;
6297 if (level
&& *level
== UnSet
)
6298 *level
= LEVEL_CONTAINER
;
6300 if (level
&& layout
&& *layout
== UnSet
)
6301 *layout
= imsm_level_to_layout(*level
);
6303 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0))
6304 *chunk
= imsm_default_chunk(super
->orom
);
6307 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
6309 static int kill_subarray_imsm(struct supertype
*st
)
6311 /* remove the subarray currently referenced by ->current_vol */
6313 struct intel_dev
**dp
;
6314 struct intel_super
*super
= st
->sb
;
6315 __u8 current_vol
= super
->current_vol
;
6316 struct imsm_super
*mpb
= super
->anchor
;
6318 if (super
->current_vol
< 0)
6320 super
->current_vol
= -1; /* invalidate subarray cursor */
6322 /* block deletions that would change the uuid of active subarrays
6324 * FIXME when immutable ids are available, but note that we'll
6325 * also need to fixup the invalidated/active subarray indexes in
6328 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6331 if (i
< current_vol
)
6333 sprintf(subarray
, "%u", i
);
6334 if (is_subarray_active(subarray
, st
->devnm
)) {
6335 pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
6342 if (st
->update_tail
) {
6343 struct imsm_update_kill_array
*u
= xmalloc(sizeof(*u
));
6345 u
->type
= update_kill_array
;
6346 u
->dev_idx
= current_vol
;
6347 append_metadata_update(st
, u
, sizeof(*u
));
6352 for (dp
= &super
->devlist
; *dp
;)
6353 if ((*dp
)->index
== current_vol
) {
6356 handle_missing(super
, (*dp
)->dev
);
6357 if ((*dp
)->index
> current_vol
)
6362 /* no more raid devices, all active components are now spares,
6363 * but of course failed are still failed
6365 if (--mpb
->num_raid_devs
== 0) {
6368 for (d
= super
->disks
; d
; d
= d
->next
)
6373 super
->updates_pending
++;
6378 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
6379 char *update
, struct mddev_ident
*ident
)
6381 /* update the subarray currently referenced by ->current_vol */
6382 struct intel_super
*super
= st
->sb
;
6383 struct imsm_super
*mpb
= super
->anchor
;
6385 if (strcmp(update
, "name") == 0) {
6386 char *name
= ident
->name
;
6390 if (is_subarray_active(subarray
, st
->devnm
)) {
6391 pr_err("Unable to update name of active subarray\n");
6395 if (!check_name(super
, name
, 0))
6398 vol
= strtoul(subarray
, &ep
, 10);
6399 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
6402 if (st
->update_tail
) {
6403 struct imsm_update_rename_array
*u
= xmalloc(sizeof(*u
));
6405 u
->type
= update_rename_array
;
6407 snprintf((char *) u
->name
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6408 append_metadata_update(st
, u
, sizeof(*u
));
6410 struct imsm_dev
*dev
;
6413 dev
= get_imsm_dev(super
, vol
);
6414 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6415 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6416 dev
= get_imsm_dev(super
, i
);
6417 handle_missing(super
, dev
);
6419 super
->updates_pending
++;
6426 #endif /* MDASSEMBLE */
6428 static int is_gen_migration(struct imsm_dev
*dev
)
6433 if (!dev
->vol
.migr_state
)
6436 if (migr_type(dev
) == MIGR_GEN_MIGR
)
6442 static int is_rebuilding(struct imsm_dev
*dev
)
6444 struct imsm_map
*migr_map
;
6446 if (!dev
->vol
.migr_state
)
6449 if (migr_type(dev
) != MIGR_REBUILD
)
6452 migr_map
= get_imsm_map(dev
, MAP_1
);
6454 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
6461 static int is_initializing(struct imsm_dev
*dev
)
6463 struct imsm_map
*migr_map
;
6465 if (!dev
->vol
.migr_state
)
6468 if (migr_type(dev
) != MIGR_INIT
)
6471 migr_map
= get_imsm_map(dev
, MAP_1
);
6473 if (migr_map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
6480 static void update_recovery_start(struct intel_super
*super
,
6481 struct imsm_dev
*dev
,
6482 struct mdinfo
*array
)
6484 struct mdinfo
*rebuild
= NULL
;
6488 if (!is_rebuilding(dev
))
6491 /* Find the rebuild target, but punt on the dual rebuild case */
6492 for (d
= array
->devs
; d
; d
= d
->next
)
6493 if (d
->recovery_start
== 0) {
6500 /* (?) none of the disks are marked with
6501 * IMSM_ORD_REBUILD, so assume they are missing and the
6502 * disk_ord_tbl was not correctly updated
6504 dprintf("%s: failed to locate out-of-sync disk\n", __func__
);
6508 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
6509 rebuild
->recovery_start
= units
* blocks_per_migr_unit(super
, dev
);
6513 static int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
);
6516 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
6518 /* Given a container loaded by load_super_imsm_all,
6519 * extract information about all the arrays into
6521 * If 'subarray' is given, just extract info about that array.
6523 * For each imsm_dev create an mdinfo, fill it in,
6524 * then look for matching devices in super->disks
6525 * and create appropriate device mdinfo.
6527 struct intel_super
*super
= st
->sb
;
6528 struct imsm_super
*mpb
= super
->anchor
;
6529 struct mdinfo
*rest
= NULL
;
6533 int spare_disks
= 0;
6535 /* do not assemble arrays when not all attributes are supported */
6536 if (imsm_check_attributes(mpb
->attributes
) == 0) {
6538 pr_err("Unsupported attributes in IMSM metadata."
6539 "Arrays activation is blocked.\n");
6542 /* check for bad blocks */
6543 if (imsm_bbm_log_size(super
->anchor
)) {
6544 pr_err("BBM log found in IMSM metadata."
6545 "Arrays activation is blocked.\n");
6549 /* count spare devices, not used in maps
6551 for (d
= super
->disks
; d
; d
= d
->next
)
6555 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6556 struct imsm_dev
*dev
;
6557 struct imsm_map
*map
;
6558 struct imsm_map
*map2
;
6559 struct mdinfo
*this;
6567 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
6570 dev
= get_imsm_dev(super
, i
);
6571 map
= get_imsm_map(dev
, MAP_0
);
6572 map2
= get_imsm_map(dev
, MAP_1
);
6574 /* do not publish arrays that are in the middle of an
6575 * unsupported migration
6577 if (dev
->vol
.migr_state
&&
6578 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
6579 pr_err("cannot assemble volume '%.16s':"
6580 " unsupported migration in progress\n",
6584 /* do not publish arrays that are not support by controller's
6588 this = xmalloc(sizeof(*this));
6590 super
->current_vol
= i
;
6591 getinfo_super_imsm_volume(st
, this, NULL
);
6594 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
6595 /* mdadm does not support all metadata features- set the bit in all arrays state */
6596 if (!validate_geometry_imsm_orom(super
,
6597 get_imsm_raid_level(map
), /* RAID level */
6598 imsm_level_to_layout(get_imsm_raid_level(map
)),
6599 map
->num_members
, /* raid disks */
6600 &chunk
, join_u32(dev
->size_low
, dev
->size_high
),
6602 pr_err("IMSM RAID geometry validation"
6603 " failed. Array %s activation is blocked.\n",
6605 this->array
.state
|=
6606 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
6607 (1<<MD_SB_BLOCK_VOLUME
);
6611 /* if array has bad blocks, set suitable bit in all arrays state */
6613 this->array
.state
|=
6614 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
6615 (1<<MD_SB_BLOCK_VOLUME
);
6617 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
6618 unsigned long long recovery_start
;
6619 struct mdinfo
*info_d
;
6626 idx
= get_imsm_disk_idx(dev
, slot
, MAP_0
);
6627 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
6628 for (d
= super
->disks
; d
; d
= d
->next
)
6629 if (d
->index
== idx
)
6632 recovery_start
= MaxSector
;
6635 if (d
&& is_failed(&d
->disk
))
6637 if (ord
& IMSM_ORD_REBUILD
)
6641 * if we skip some disks the array will be assmebled degraded;
6642 * reset resync start to avoid a dirty-degraded
6643 * situation when performing the intial sync
6645 * FIXME handle dirty degraded
6647 if ((skip
|| recovery_start
== 0) && !dev
->vol
.dirty
)
6648 this->resync_start
= MaxSector
;
6652 info_d
= xcalloc(1, sizeof(*info_d
));
6653 info_d
->next
= this->devs
;
6654 this->devs
= info_d
;
6656 info_d
->disk
.number
= d
->index
;
6657 info_d
->disk
.major
= d
->major
;
6658 info_d
->disk
.minor
= d
->minor
;
6659 info_d
->disk
.raid_disk
= slot
;
6660 info_d
->recovery_start
= recovery_start
;
6662 if (slot
< map2
->num_members
)
6663 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
6665 this->array
.spare_disks
++;
6667 if (slot
< map
->num_members
)
6668 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
6670 this->array
.spare_disks
++;
6672 if (info_d
->recovery_start
== MaxSector
)
6673 this->array
.working_disks
++;
6675 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
6676 info_d
->data_offset
= pba_of_lba0(map
);
6677 info_d
->component_size
= blocks_per_member(map
);
6679 /* now that the disk list is up-to-date fixup recovery_start */
6680 update_recovery_start(super
, dev
, this);
6681 this->array
.spare_disks
+= spare_disks
;
6684 /* check for reshape */
6685 if (this->reshape_active
== 1)
6686 recover_backup_imsm(st
, this);
6694 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
6695 int failed
, int look_in_map
)
6697 struct imsm_map
*map
;
6699 map
= get_imsm_map(dev
, look_in_map
);
6702 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
6703 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
6705 switch (get_imsm_raid_level(map
)) {
6707 return IMSM_T_STATE_FAILED
;
6710 if (failed
< map
->num_members
)
6711 return IMSM_T_STATE_DEGRADED
;
6713 return IMSM_T_STATE_FAILED
;
6718 * check to see if any mirrors have failed, otherwise we
6719 * are degraded. Even numbered slots are mirrored on
6723 /* gcc -Os complains that this is unused */
6724 int insync
= insync
;
6726 for (i
= 0; i
< map
->num_members
; i
++) {
6727 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_X
);
6728 int idx
= ord_to_idx(ord
);
6729 struct imsm_disk
*disk
;
6731 /* reset the potential in-sync count on even-numbered
6732 * slots. num_copies is always 2 for imsm raid10
6737 disk
= get_imsm_disk(super
, idx
);
6738 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
6741 /* no in-sync disks left in this mirror the
6745 return IMSM_T_STATE_FAILED
;
6748 return IMSM_T_STATE_DEGRADED
;
6752 return IMSM_T_STATE_DEGRADED
;
6754 return IMSM_T_STATE_FAILED
;
6760 return map
->map_state
;
6763 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
6768 struct imsm_disk
*disk
;
6769 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6770 struct imsm_map
*prev
= get_imsm_map(dev
, MAP_1
);
6771 struct imsm_map
*map_for_loop
;
6776 /* at the beginning of migration we set IMSM_ORD_REBUILD on
6777 * disks that are being rebuilt. New failures are recorded to
6778 * map[0]. So we look through all the disks we started with and
6779 * see if any failures are still present, or if any new ones
6783 if (prev
&& (map
->num_members
< prev
->num_members
))
6784 map_for_loop
= prev
;
6786 for (i
= 0; i
< map_for_loop
->num_members
; i
++) {
6788 /* when MAP_X is passed both maps failures are counted
6791 ((look_in_map
== MAP_1
) || (look_in_map
== MAP_X
)) &&
6792 (i
< prev
->num_members
)) {
6793 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
6794 idx_1
= ord_to_idx(ord
);
6796 disk
= get_imsm_disk(super
, idx_1
);
6797 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
6800 if (((look_in_map
== MAP_0
) || (look_in_map
== MAP_X
)) &&
6801 (i
< map
->num_members
)) {
6802 ord
= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
6803 idx
= ord_to_idx(ord
);
6806 disk
= get_imsm_disk(super
, idx
);
6807 if (!disk
|| is_failed(disk
) ||
6808 ord
& IMSM_ORD_REBUILD
)
6818 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
6821 struct intel_super
*super
= c
->sb
;
6822 struct imsm_super
*mpb
= super
->anchor
;
6824 if (atoi(inst
) >= mpb
->num_raid_devs
) {
6825 pr_err("%s: subarry index %d, out of range\n",
6826 __func__
, atoi(inst
));
6830 dprintf("imsm: open_new %s\n", inst
);
6831 a
->info
.container_member
= atoi(inst
);
6835 static int is_resyncing(struct imsm_dev
*dev
)
6837 struct imsm_map
*migr_map
;
6839 if (!dev
->vol
.migr_state
)
6842 if (migr_type(dev
) == MIGR_INIT
||
6843 migr_type(dev
) == MIGR_REPAIR
)
6846 if (migr_type(dev
) == MIGR_GEN_MIGR
)
6849 migr_map
= get_imsm_map(dev
, MAP_1
);
6851 if ((migr_map
->map_state
== IMSM_T_STATE_NORMAL
) &&
6852 (dev
->vol
.migr_type
!= MIGR_GEN_MIGR
))
6858 /* return true if we recorded new information */
6859 static int mark_failure(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
6863 struct imsm_map
*map
;
6864 char buf
[MAX_RAID_SERIAL_LEN
+3];
6865 unsigned int len
, shift
= 0;
6867 /* new failures are always set in map[0] */
6868 map
= get_imsm_map(dev
, MAP_0
);
6870 slot
= get_imsm_disk_slot(map
, idx
);
6874 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
6875 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
6878 memcpy(buf
, disk
->serial
, MAX_RAID_SERIAL_LEN
);
6879 buf
[MAX_RAID_SERIAL_LEN
] = '\000';
6881 if ((len
= strlen(buf
)) >= MAX_RAID_SERIAL_LEN
)
6882 shift
= len
- MAX_RAID_SERIAL_LEN
+ 1;
6883 strncpy((char *)disk
->serial
, &buf
[shift
], MAX_RAID_SERIAL_LEN
);
6885 disk
->status
|= FAILED_DISK
;
6886 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
6887 /* mark failures in second map if second map exists and this disk
6889 * This is valid for migration, initialization and rebuild
6891 if (dev
->vol
.migr_state
) {
6892 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
6893 int slot2
= get_imsm_disk_slot(map2
, idx
);
6895 if ((slot2
< map2
->num_members
) &&
6897 set_imsm_ord_tbl_ent(map2
, slot2
,
6898 idx
| IMSM_ORD_REBUILD
);
6900 if (map
->failed_disk_num
== 0xff)
6901 map
->failed_disk_num
= slot
;
6905 static void mark_missing(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
6907 mark_failure(dev
, disk
, idx
);
6909 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
6912 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
6913 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
6916 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
6920 if (!super
->missing
)
6923 /* When orom adds replacement for missing disk it does
6924 * not remove entry of missing disk, but just updates map with
6925 * new added disk. So it is not enough just to test if there is
6926 * any missing disk, we have to look if there are any failed disks
6927 * in map to stop migration */
6929 dprintf("imsm: mark missing\n");
6930 /* end process for initialization and rebuild only
6932 if (is_gen_migration(dev
) == 0) {
6936 failed
= imsm_count_failed(super
, dev
, MAP_0
);
6937 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
6940 end_migration(dev
, super
, map_state
);
6942 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
6943 mark_missing(dev
, &dl
->disk
, dl
->index
);
6944 super
->updates_pending
++;
6947 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
,
6950 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
6951 unsigned long long array_blocks
;
6952 struct imsm_map
*map
;
6954 if (used_disks
== 0) {
6955 /* when problems occures
6956 * return current array_blocks value
6958 array_blocks
= __le32_to_cpu(dev
->size_high
);
6959 array_blocks
= array_blocks
<< 32;
6960 array_blocks
+= __le32_to_cpu(dev
->size_low
);
6962 return array_blocks
;
6965 /* set array size in metadata
6967 if (new_size
<= 0) {
6968 /* OLCE size change is caused by added disks
6970 map
= get_imsm_map(dev
, MAP_0
);
6971 array_blocks
= blocks_per_member(map
) * used_disks
;
6973 /* Online Volume Size Change
6974 * Using available free space
6976 array_blocks
= new_size
;
6979 /* round array size down to closest MB
6981 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
6982 dev
->size_low
= __cpu_to_le32((__u32
)array_blocks
);
6983 dev
->size_high
= __cpu_to_le32((__u32
)(array_blocks
>> 32));
6985 return array_blocks
;
6988 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
6990 static void imsm_progress_container_reshape(struct intel_super
*super
)
6992 /* if no device has a migr_state, but some device has a
6993 * different number of members than the previous device, start
6994 * changing the number of devices in this device to match
6997 struct imsm_super
*mpb
= super
->anchor
;
6998 int prev_disks
= -1;
7002 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7003 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
7004 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7005 struct imsm_map
*map2
;
7006 int prev_num_members
;
7008 if (dev
->vol
.migr_state
)
7011 if (prev_disks
== -1)
7012 prev_disks
= map
->num_members
;
7013 if (prev_disks
== map
->num_members
)
7016 /* OK, this array needs to enter reshape mode.
7017 * i.e it needs a migr_state
7020 copy_map_size
= sizeof_imsm_map(map
);
7021 prev_num_members
= map
->num_members
;
7022 map
->num_members
= prev_disks
;
7023 dev
->vol
.migr_state
= 1;
7024 dev
->vol
.curr_migr_unit
= 0;
7025 set_migr_type(dev
, MIGR_GEN_MIGR
);
7026 for (i
= prev_num_members
;
7027 i
< map
->num_members
; i
++)
7028 set_imsm_ord_tbl_ent(map
, i
, i
);
7029 map2
= get_imsm_map(dev
, MAP_1
);
7030 /* Copy the current map */
7031 memcpy(map2
, map
, copy_map_size
);
7032 map2
->num_members
= prev_num_members
;
7034 imsm_set_array_size(dev
, -1);
7035 super
->clean_migration_record_by_mdmon
= 1;
7036 super
->updates_pending
++;
7040 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
7041 * states are handled in imsm_set_disk() with one exception, when a
7042 * resync is stopped due to a new failure this routine will set the
7043 * 'degraded' state for the array.
7045 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
7047 int inst
= a
->info
.container_member
;
7048 struct intel_super
*super
= a
->container
->sb
;
7049 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7050 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7051 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
7052 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7053 __u32 blocks_per_unit
;
7055 if (dev
->vol
.migr_state
&&
7056 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
7057 /* array state change is blocked due to reshape action
7059 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
7060 * - finish the reshape (if last_checkpoint is big and action != reshape)
7061 * - update curr_migr_unit
7063 if (a
->curr_action
== reshape
) {
7064 /* still reshaping, maybe update curr_migr_unit */
7065 goto mark_checkpoint
;
7067 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
7068 /* for some reason we aborted the reshape.
7070 * disable automatic metadata rollback
7071 * user action is required to recover process
7074 struct imsm_map
*map2
=
7075 get_imsm_map(dev
, MAP_1
);
7076 dev
->vol
.migr_state
= 0;
7077 set_migr_type(dev
, 0);
7078 dev
->vol
.curr_migr_unit
= 0;
7080 sizeof_imsm_map(map2
));
7081 super
->updates_pending
++;
7084 if (a
->last_checkpoint
>= a
->info
.component_size
) {
7085 unsigned long long array_blocks
;
7089 used_disks
= imsm_num_data_members(dev
, MAP_0
);
7090 if (used_disks
> 0) {
7092 blocks_per_member(map
) *
7094 /* round array size down to closest MB
7096 array_blocks
= (array_blocks
7097 >> SECT_PER_MB_SHIFT
)
7098 << SECT_PER_MB_SHIFT
;
7099 a
->info
.custom_array_size
= array_blocks
;
7100 /* encourage manager to update array
7104 a
->check_reshape
= 1;
7106 /* finalize online capacity expansion/reshape */
7107 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
7109 mdi
->disk
.raid_disk
,
7112 imsm_progress_container_reshape(super
);
7117 /* before we activate this array handle any missing disks */
7118 if (consistent
== 2)
7119 handle_missing(super
, dev
);
7121 if (consistent
== 2 &&
7122 (!is_resync_complete(&a
->info
) ||
7123 map_state
!= IMSM_T_STATE_NORMAL
||
7124 dev
->vol
.migr_state
))
7127 if (is_resync_complete(&a
->info
)) {
7128 /* complete intialization / resync,
7129 * recovery and interrupted recovery is completed in
7132 if (is_resyncing(dev
)) {
7133 dprintf("imsm: mark resync done\n");
7134 end_migration(dev
, super
, map_state
);
7135 super
->updates_pending
++;
7136 a
->last_checkpoint
= 0;
7138 } else if ((!is_resyncing(dev
) && !failed
) &&
7139 (imsm_reshape_blocks_arrays_changes(super
) == 0)) {
7140 /* mark the start of the init process if nothing is failed */
7141 dprintf("imsm: mark resync start\n");
7142 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
7143 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
7145 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
7146 super
->updates_pending
++;
7150 /* skip checkpointing for general migration,
7151 * it is controlled in mdadm
7153 if (is_gen_migration(dev
))
7154 goto skip_mark_checkpoint
;
7156 /* check if we can update curr_migr_unit from resync_start, recovery_start */
7157 blocks_per_unit
= blocks_per_migr_unit(super
, dev
);
7158 if (blocks_per_unit
) {
7162 units
= a
->last_checkpoint
/ blocks_per_unit
;
7165 /* check that we did not overflow 32-bits, and that
7166 * curr_migr_unit needs updating
7168 if (units32
== units
&&
7170 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
7171 dprintf("imsm: mark checkpoint (%u)\n", units32
);
7172 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
7173 super
->updates_pending
++;
7177 skip_mark_checkpoint
:
7178 /* mark dirty / clean */
7179 if (dev
->vol
.dirty
!= !consistent
) {
7180 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
7185 super
->updates_pending
++;
7191 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
7193 int inst
= a
->info
.container_member
;
7194 struct intel_super
*super
= a
->container
->sb
;
7195 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7196 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7197 struct imsm_disk
*disk
;
7199 int recovery_not_finished
= 0;
7204 if (n
> map
->num_members
)
7205 pr_err("imsm: set_disk %d out of range 0..%d\n",
7206 n
, map
->num_members
- 1);
7211 dprintf("imsm: set_disk %d:%x\n", n
, state
);
7213 ord
= get_imsm_ord_tbl_ent(dev
, n
, MAP_0
);
7214 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
7216 /* check for new failures */
7217 if (state
& DS_FAULTY
) {
7218 if (mark_failure(dev
, disk
, ord_to_idx(ord
)))
7219 super
->updates_pending
++;
7222 /* check if in_sync */
7223 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
7224 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
7226 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
7227 super
->updates_pending
++;
7230 failed
= imsm_count_failed(super
, dev
, MAP_0
);
7231 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7233 /* check if recovery complete, newly degraded, or failed */
7234 dprintf("imsm: Detected transition to state ");
7235 switch (map_state
) {
7236 case IMSM_T_STATE_NORMAL
: /* transition to normal state */
7237 dprintf("normal: ");
7238 if (is_rebuilding(dev
)) {
7239 dprintf("while rebuilding");
7240 /* check if recovery is really finished */
7241 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
7242 if (mdi
->recovery_start
!= MaxSector
) {
7243 recovery_not_finished
= 1;
7246 if (recovery_not_finished
) {
7247 dprintf("\nimsm: Rebuild has not finished yet, "
7248 "state not changed");
7249 if (a
->last_checkpoint
< mdi
->recovery_start
) {
7250 a
->last_checkpoint
= mdi
->recovery_start
;
7251 super
->updates_pending
++;
7255 end_migration(dev
, super
, map_state
);
7256 map
= get_imsm_map(dev
, MAP_0
);
7257 map
->failed_disk_num
= ~0;
7258 super
->updates_pending
++;
7259 a
->last_checkpoint
= 0;
7262 if (is_gen_migration(dev
)) {
7263 dprintf("while general migration");
7264 if (a
->last_checkpoint
>= a
->info
.component_size
)
7265 end_migration(dev
, super
, map_state
);
7267 map
->map_state
= map_state
;
7268 map
= get_imsm_map(dev
, MAP_0
);
7269 map
->failed_disk_num
= ~0;
7270 super
->updates_pending
++;
7274 case IMSM_T_STATE_DEGRADED
: /* transition to degraded state */
7275 dprintf("degraded: ");
7276 if ((map
->map_state
!= map_state
) &&
7277 !dev
->vol
.migr_state
) {
7278 dprintf("mark degraded");
7279 map
->map_state
= map_state
;
7280 super
->updates_pending
++;
7281 a
->last_checkpoint
= 0;
7284 if (is_rebuilding(dev
)) {
7285 dprintf("while rebuilding.");
7286 if (map
->map_state
!= map_state
) {
7287 dprintf(" Map state change");
7288 end_migration(dev
, super
, map_state
);
7289 super
->updates_pending
++;
7293 if (is_gen_migration(dev
)) {
7294 dprintf("while general migration");
7295 if (a
->last_checkpoint
>= a
->info
.component_size
)
7296 end_migration(dev
, super
, map_state
);
7298 map
->map_state
= map_state
;
7299 manage_second_map(super
, dev
);
7301 super
->updates_pending
++;
7304 if (is_initializing(dev
)) {
7305 dprintf("while initialization.");
7306 map
->map_state
= map_state
;
7307 super
->updates_pending
++;
7311 case IMSM_T_STATE_FAILED
: /* transition to failed state */
7312 dprintf("failed: ");
7313 if (is_gen_migration(dev
)) {
7314 dprintf("while general migration");
7315 map
->map_state
= map_state
;
7316 super
->updates_pending
++;
7319 if (map
->map_state
!= map_state
) {
7320 dprintf("mark failed");
7321 end_migration(dev
, super
, map_state
);
7322 super
->updates_pending
++;
7323 a
->last_checkpoint
= 0;
7328 dprintf("state %i\n", map_state
);
7334 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
7337 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
7338 unsigned long long dsize
;
7339 unsigned long long sectors
;
7341 get_dev_size(fd
, NULL
, &dsize
);
7343 if (mpb_size
> 512) {
7344 /* -1 to account for anchor */
7345 sectors
= mpb_sectors(mpb
) - 1;
7347 /* write the extended mpb to the sectors preceeding the anchor */
7348 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0)
7351 if ((unsigned long long)write(fd
, buf
+ 512, 512 * sectors
)
7356 /* first block is stored on second to last sector of the disk */
7357 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0)
7360 if (write(fd
, buf
, 512) != 512)
7366 static void imsm_sync_metadata(struct supertype
*container
)
7368 struct intel_super
*super
= container
->sb
;
7370 dprintf("sync metadata: %d\n", super
->updates_pending
);
7371 if (!super
->updates_pending
)
7374 write_super_imsm(container
, 0);
7376 super
->updates_pending
= 0;
7379 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
7381 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7382 int i
= get_imsm_disk_idx(dev
, idx
, MAP_X
);
7385 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7389 if (dl
&& is_failed(&dl
->disk
))
7393 dprintf("%s: found %x:%x\n", __func__
, dl
->major
, dl
->minor
);
7398 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
7399 struct active_array
*a
, int activate_new
,
7400 struct mdinfo
*additional_test_list
)
7402 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7403 int idx
= get_imsm_disk_idx(dev
, slot
, MAP_X
);
7404 struct imsm_super
*mpb
= super
->anchor
;
7405 struct imsm_map
*map
;
7406 unsigned long long pos
;
7411 __u32 array_start
= 0;
7412 __u32 array_end
= 0;
7414 struct mdinfo
*test_list
;
7416 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7417 /* If in this array, skip */
7418 for (d
= a
->info
.devs
; d
; d
= d
->next
)
7419 if (d
->state_fd
>= 0 &&
7420 d
->disk
.major
== dl
->major
&&
7421 d
->disk
.minor
== dl
->minor
) {
7422 dprintf("%x:%x already in array\n",
7423 dl
->major
, dl
->minor
);
7428 test_list
= additional_test_list
;
7430 if (test_list
->disk
.major
== dl
->major
&&
7431 test_list
->disk
.minor
== dl
->minor
) {
7432 dprintf("%x:%x already in additional test list\n",
7433 dl
->major
, dl
->minor
);
7436 test_list
= test_list
->next
;
7441 /* skip in use or failed drives */
7442 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
7444 dprintf("%x:%x status (failed: %d index: %d)\n",
7445 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
7449 /* skip pure spares when we are looking for partially
7450 * assimilated drives
7452 if (dl
->index
== -1 && !activate_new
)
7455 /* Does this unused device have the requisite free space?
7456 * It needs to be able to cover all member volumes
7458 ex
= get_extents(super
, dl
);
7460 dprintf("cannot get extents\n");
7463 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7464 dev
= get_imsm_dev(super
, i
);
7465 map
= get_imsm_map(dev
, MAP_0
);
7467 /* check if this disk is already a member of
7470 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
7476 array_start
= pba_of_lba0(map
);
7477 array_end
= array_start
+
7478 blocks_per_member(map
) - 1;
7481 /* check that we can start at pba_of_lba0 with
7482 * blocks_per_member of space
7484 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
7488 pos
= ex
[j
].start
+ ex
[j
].size
;
7490 } while (ex
[j
-1].size
);
7497 if (i
< mpb
->num_raid_devs
) {
7498 dprintf("%x:%x does not have %u to %u available\n",
7499 dl
->major
, dl
->minor
, array_start
, array_end
);
7509 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
7511 struct imsm_dev
*dev2
;
7512 struct imsm_map
*map
;
7518 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
7520 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
, MAP_0
);
7521 if (state
== IMSM_T_STATE_FAILED
) {
7522 map
= get_imsm_map(dev2
, MAP_0
);
7525 for (slot
= 0; slot
< map
->num_members
; slot
++) {
7527 * Check if failed disks are deleted from intel
7528 * disk list or are marked to be deleted
7530 idx
= get_imsm_disk_idx(dev2
, slot
, MAP_X
);
7531 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
7533 * Do not rebuild the array if failed disks
7534 * from failed sub-array are not removed from
7538 is_failed(&idisk
->disk
) &&
7539 (idisk
->action
!= DISK_REMOVE
))
7547 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
7548 struct metadata_update
**updates
)
7551 * Find a device with unused free space and use it to replace a
7552 * failed/vacant region in an array. We replace failed regions one a
7553 * array at a time. The result is that a new spare disk will be added
7554 * to the first failed array and after the monitor has finished
7555 * propagating failures the remainder will be consumed.
7557 * FIXME add a capability for mdmon to request spares from another
7561 struct intel_super
*super
= a
->container
->sb
;
7562 int inst
= a
->info
.container_member
;
7563 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7564 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7565 int failed
= a
->info
.array
.raid_disks
;
7566 struct mdinfo
*rv
= NULL
;
7569 struct metadata_update
*mu
;
7571 struct imsm_update_activate_spare
*u
;
7576 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
7577 if ((d
->curr_state
& DS_FAULTY
) &&
7579 /* wait for Removal to happen */
7581 if (d
->state_fd
>= 0)
7585 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
7586 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
7588 if (imsm_reshape_blocks_arrays_changes(super
))
7591 /* Cannot activate another spare if rebuild is in progress already
7593 if (is_rebuilding(dev
)) {
7594 dprintf("imsm: No spare activation allowed. "
7595 "Rebuild in progress already.\n");
7599 if (a
->info
.array
.level
== 4)
7600 /* No repair for takeovered array
7601 * imsm doesn't support raid4
7605 if (imsm_check_degraded(super
, dev
, failed
, MAP_0
) !=
7606 IMSM_T_STATE_DEGRADED
)
7610 * If there are any failed disks check state of the other volume.
7611 * Block rebuild if the another one is failed until failed disks
7612 * are removed from container.
7615 dprintf("found failed disks in %.*s, check if there another"
7616 "failed sub-array.\n",
7617 MAX_RAID_SERIAL_LEN
, dev
->volume
);
7618 /* check if states of the other volumes allow for rebuild */
7619 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
7621 allowed
= imsm_rebuild_allowed(a
->container
,
7629 /* For each slot, if it is not working, find a spare */
7630 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
7631 for (d
= a
->info
.devs
; d
; d
= d
->next
)
7632 if (d
->disk
.raid_disk
== i
)
7634 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
7635 if (d
&& (d
->state_fd
>= 0))
7639 * OK, this device needs recovery. Try to re-add the
7640 * previous occupant of this slot, if this fails see if
7641 * we can continue the assimilation of a spare that was
7642 * partially assimilated, finally try to activate a new
7645 dl
= imsm_readd(super
, i
, a
);
7647 dl
= imsm_add_spare(super
, i
, a
, 0, rv
);
7649 dl
= imsm_add_spare(super
, i
, a
, 1, rv
);
7653 /* found a usable disk with enough space */
7654 di
= xcalloc(1, sizeof(*di
));
7656 /* dl->index will be -1 in the case we are activating a
7657 * pristine spare. imsm_process_update() will create a
7658 * new index in this case. Once a disk is found to be
7659 * failed in all member arrays it is kicked from the
7662 di
->disk
.number
= dl
->index
;
7664 /* (ab)use di->devs to store a pointer to the device
7667 di
->devs
= (struct mdinfo
*) dl
;
7669 di
->disk
.raid_disk
= i
;
7670 di
->disk
.major
= dl
->major
;
7671 di
->disk
.minor
= dl
->minor
;
7673 di
->recovery_start
= 0;
7674 di
->data_offset
= pba_of_lba0(map
);
7675 di
->component_size
= a
->info
.component_size
;
7676 di
->container_member
= inst
;
7677 super
->random
= random32();
7681 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
7682 i
, di
->data_offset
);
7686 /* No spares found */
7688 /* Now 'rv' has a list of devices to return.
7689 * Create a metadata_update record to update the
7690 * disk_ord_tbl for the array
7692 mu
= xmalloc(sizeof(*mu
));
7693 mu
->buf
= xcalloc(num_spares
,
7694 sizeof(struct imsm_update_activate_spare
));
7696 mu
->space_list
= NULL
;
7697 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
7698 mu
->next
= *updates
;
7699 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
7701 for (di
= rv
; di
; di
= di
->next
) {
7702 u
->type
= update_activate_spare
;
7703 u
->dl
= (struct dl
*) di
->devs
;
7705 u
->slot
= di
->disk
.raid_disk
;
7716 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
7718 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
7719 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7720 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, MAP_0
);
7721 struct disk_info
*inf
= get_disk_info(u
);
7722 struct imsm_disk
*disk
;
7726 for (i
= 0; i
< map
->num_members
; i
++) {
7727 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, MAP_X
));
7728 for (j
= 0; j
< new_map
->num_members
; j
++)
7729 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
7736 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
7738 struct dl
*dl
= NULL
;
7739 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7740 if ((dl
->major
== major
) && (dl
->minor
== minor
))
7745 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
7747 struct dl
*prev
= NULL
;
7751 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7752 if ((dl
->major
== major
) && (dl
->minor
== minor
)) {
7755 prev
->next
= dl
->next
;
7757 super
->disks
= dl
->next
;
7759 __free_imsm_disk(dl
);
7760 dprintf("%s: removed %x:%x\n",
7761 __func__
, major
, minor
);
7769 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
7771 static int add_remove_disk_update(struct intel_super
*super
)
7773 int check_degraded
= 0;
7774 struct dl
*disk
= NULL
;
7775 /* add/remove some spares to/from the metadata/contrainer */
7776 while (super
->disk_mgmt_list
) {
7777 struct dl
*disk_cfg
;
7779 disk_cfg
= super
->disk_mgmt_list
;
7780 super
->disk_mgmt_list
= disk_cfg
->next
;
7781 disk_cfg
->next
= NULL
;
7783 if (disk_cfg
->action
== DISK_ADD
) {
7784 disk_cfg
->next
= super
->disks
;
7785 super
->disks
= disk_cfg
;
7787 dprintf("%s: added %x:%x\n",
7788 __func__
, disk_cfg
->major
,
7790 } else if (disk_cfg
->action
== DISK_REMOVE
) {
7791 dprintf("Disk remove action processed: %x.%x\n",
7792 disk_cfg
->major
, disk_cfg
->minor
);
7793 disk
= get_disk_super(super
,
7797 /* store action status */
7798 disk
->action
= DISK_REMOVE
;
7799 /* remove spare disks only */
7800 if (disk
->index
== -1) {
7801 remove_disk_super(super
,
7806 /* release allocate disk structure */
7807 __free_imsm_disk(disk_cfg
);
7810 return check_degraded
;
7813 static int apply_reshape_migration_update(struct imsm_update_reshape_migration
*u
,
7814 struct intel_super
*super
,
7817 struct intel_dev
*id
;
7818 void **tofree
= NULL
;
7821 dprintf("apply_reshape_migration_update()\n");
7822 if ((u
->subdev
< 0) ||
7824 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
7827 if ((space_list
== NULL
) || (*space_list
== NULL
)) {
7828 dprintf("imsm: Error: Memory is not allocated\n");
7832 for (id
= super
->devlist
; id
; id
= id
->next
) {
7833 if (id
->index
== (unsigned)u
->subdev
) {
7834 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
7835 struct imsm_map
*map
;
7836 struct imsm_dev
*new_dev
=
7837 (struct imsm_dev
*)*space_list
;
7838 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
7840 struct dl
*new_disk
;
7842 if (new_dev
== NULL
)
7844 *space_list
= **space_list
;
7845 memcpy(new_dev
, dev
, sizeof_imsm_dev(dev
, 0));
7846 map
= get_imsm_map(new_dev
, MAP_0
);
7848 dprintf("imsm: Error: migration in progress");
7852 to_state
= map
->map_state
;
7853 if ((u
->new_level
== 5) && (map
->raid_level
== 0)) {
7855 /* this should not happen */
7856 if (u
->new_disks
[0] < 0) {
7857 map
->failed_disk_num
=
7858 map
->num_members
- 1;
7859 to_state
= IMSM_T_STATE_DEGRADED
;
7861 to_state
= IMSM_T_STATE_NORMAL
;
7863 migrate(new_dev
, super
, to_state
, MIGR_GEN_MIGR
);
7864 if (u
->new_level
> -1)
7865 map
->raid_level
= u
->new_level
;
7866 migr_map
= get_imsm_map(new_dev
, MAP_1
);
7867 if ((u
->new_level
== 5) &&
7868 (migr_map
->raid_level
== 0)) {
7869 int ord
= map
->num_members
- 1;
7870 migr_map
->num_members
--;
7871 if (u
->new_disks
[0] < 0)
7872 ord
|= IMSM_ORD_REBUILD
;
7873 set_imsm_ord_tbl_ent(map
,
7874 map
->num_members
- 1,
7878 tofree
= (void **)dev
;
7880 /* update chunk size
7882 if (u
->new_chunksize
> 0)
7883 map
->blocks_per_strip
=
7884 __cpu_to_le16(u
->new_chunksize
* 2);
7888 if ((u
->new_level
!= 5) ||
7889 (migr_map
->raid_level
!= 0) ||
7890 (migr_map
->raid_level
== map
->raid_level
))
7893 if (u
->new_disks
[0] >= 0) {
7896 new_disk
= get_disk_super(super
,
7897 major(u
->new_disks
[0]),
7898 minor(u
->new_disks
[0]));
7899 dprintf("imsm: new disk for reshape is: %i:%i "
7900 "(%p, index = %i)\n",
7901 major(u
->new_disks
[0]),
7902 minor(u
->new_disks
[0]),
7903 new_disk
, new_disk
->index
);
7904 if (new_disk
== NULL
)
7905 goto error_disk_add
;
7907 new_disk
->index
= map
->num_members
- 1;
7908 /* slot to fill in autolayout
7910 new_disk
->raiddisk
= new_disk
->index
;
7911 new_disk
->disk
.status
|= CONFIGURED_DISK
;
7912 new_disk
->disk
.status
&= ~SPARE_DISK
;
7914 goto error_disk_add
;
7917 *tofree
= *space_list
;
7918 /* calculate new size
7920 imsm_set_array_size(new_dev
, -1);
7927 *space_list
= tofree
;
7931 dprintf("Error: imsm: Cannot find disk.\n");
7935 static int apply_size_change_update(struct imsm_update_size_change
*u
,
7936 struct intel_super
*super
)
7938 struct intel_dev
*id
;
7941 dprintf("apply_size_change_update()\n");
7942 if ((u
->subdev
< 0) ||
7944 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
7948 for (id
= super
->devlist
; id
; id
= id
->next
) {
7949 if (id
->index
== (unsigned)u
->subdev
) {
7950 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
7951 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7952 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
7953 unsigned long long blocks_per_member
;
7955 /* calculate new size
7957 blocks_per_member
= u
->new_size
/ used_disks
;
7958 dprintf("imsm: apply_size_change_update(size: %llu, "
7959 "blocks per member: %llu)\n",
7960 u
->new_size
, blocks_per_member
);
7961 set_blocks_per_member(map
, blocks_per_member
);
7962 imsm_set_array_size(dev
, u
->new_size
);
7972 static int apply_update_activate_spare(struct imsm_update_activate_spare
*u
,
7973 struct intel_super
*super
,
7974 struct active_array
*active_array
)
7976 struct imsm_super
*mpb
= super
->anchor
;
7977 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
7978 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7979 struct imsm_map
*migr_map
;
7980 struct active_array
*a
;
7981 struct imsm_disk
*disk
;
7988 int second_map_created
= 0;
7990 for (; u
; u
= u
->next
) {
7991 victim
= get_imsm_disk_idx(dev
, u
->slot
, MAP_X
);
7996 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
8001 pr_err("error: imsm_activate_spare passed "
8002 "an unknown disk (index: %d)\n",
8007 /* count failures (excluding rebuilds and the victim)
8008 * to determine map[0] state
8011 for (i
= 0; i
< map
->num_members
; i
++) {
8014 disk
= get_imsm_disk(super
,
8015 get_imsm_disk_idx(dev
, i
, MAP_X
));
8016 if (!disk
|| is_failed(disk
))
8020 /* adding a pristine spare, assign a new index */
8021 if (dl
->index
< 0) {
8022 dl
->index
= super
->anchor
->num_disks
;
8023 super
->anchor
->num_disks
++;
8026 disk
->status
|= CONFIGURED_DISK
;
8027 disk
->status
&= ~SPARE_DISK
;
8030 to_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8031 if (!second_map_created
) {
8032 second_map_created
= 1;
8033 map
->map_state
= IMSM_T_STATE_DEGRADED
;
8034 migrate(dev
, super
, to_state
, MIGR_REBUILD
);
8036 map
->map_state
= to_state
;
8037 migr_map
= get_imsm_map(dev
, MAP_1
);
8038 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
8039 set_imsm_ord_tbl_ent(migr_map
, u
->slot
,
8040 dl
->index
| IMSM_ORD_REBUILD
);
8042 /* update the family_num to mark a new container
8043 * generation, being careful to record the existing
8044 * family_num in orig_family_num to clean up after
8045 * earlier mdadm versions that neglected to set it.
8047 if (mpb
->orig_family_num
== 0)
8048 mpb
->orig_family_num
= mpb
->family_num
;
8049 mpb
->family_num
+= super
->random
;
8051 /* count arrays using the victim in the metadata */
8053 for (a
= active_array
; a
; a
= a
->next
) {
8054 dev
= get_imsm_dev(super
, a
->info
.container_member
);
8055 map
= get_imsm_map(dev
, MAP_0
);
8057 if (get_imsm_disk_slot(map
, victim
) >= 0)
8061 /* delete the victim if it is no longer being
8067 /* We know that 'manager' isn't touching anything,
8068 * so it is safe to delete
8070 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
8071 if ((*dlp
)->index
== victim
)
8074 /* victim may be on the missing list */
8076 for (dlp
= &super
->missing
; *dlp
;
8077 dlp
= &(*dlp
)->next
)
8078 if ((*dlp
)->index
== victim
)
8080 imsm_delete(super
, dlp
, victim
);
8087 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
8088 struct intel_super
*super
,
8091 struct dl
*new_disk
;
8092 struct intel_dev
*id
;
8094 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
8095 int disk_count
= u
->old_raid_disks
;
8096 void **tofree
= NULL
;
8097 int devices_to_reshape
= 1;
8098 struct imsm_super
*mpb
= super
->anchor
;
8100 unsigned int dev_id
;
8102 dprintf("imsm: apply_reshape_container_disks_update()\n");
8104 /* enable spares to use in array */
8105 for (i
= 0; i
< delta_disks
; i
++) {
8106 new_disk
= get_disk_super(super
,
8107 major(u
->new_disks
[i
]),
8108 minor(u
->new_disks
[i
]));
8109 dprintf("imsm: new disk for reshape is: %i:%i "
8110 "(%p, index = %i)\n",
8111 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
8112 new_disk
, new_disk
->index
);
8113 if ((new_disk
== NULL
) ||
8114 ((new_disk
->index
>= 0) &&
8115 (new_disk
->index
< u
->old_raid_disks
)))
8116 goto update_reshape_exit
;
8117 new_disk
->index
= disk_count
++;
8118 /* slot to fill in autolayout
8120 new_disk
->raiddisk
= new_disk
->index
;
8121 new_disk
->disk
.status
|=
8123 new_disk
->disk
.status
&= ~SPARE_DISK
;
8126 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
8127 mpb
->num_raid_devs
);
8128 /* manage changes in volume
8130 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
8131 void **sp
= *space_list
;
8132 struct imsm_dev
*newdev
;
8133 struct imsm_map
*newmap
, *oldmap
;
8135 for (id
= super
->devlist
; id
; id
= id
->next
) {
8136 if (id
->index
== dev_id
)
8145 /* Copy the dev, but not (all of) the map */
8146 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
8147 oldmap
= get_imsm_map(id
->dev
, MAP_0
);
8148 newmap
= get_imsm_map(newdev
, MAP_0
);
8149 /* Copy the current map */
8150 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8151 /* update one device only
8153 if (devices_to_reshape
) {
8154 dprintf("imsm: modifying subdev: %i\n",
8156 devices_to_reshape
--;
8157 newdev
->vol
.migr_state
= 1;
8158 newdev
->vol
.curr_migr_unit
= 0;
8159 set_migr_type(newdev
, MIGR_GEN_MIGR
);
8160 newmap
->num_members
= u
->new_raid_disks
;
8161 for (i
= 0; i
< delta_disks
; i
++) {
8162 set_imsm_ord_tbl_ent(newmap
,
8163 u
->old_raid_disks
+ i
,
8164 u
->old_raid_disks
+ i
);
8166 /* New map is correct, now need to save old map
8168 newmap
= get_imsm_map(newdev
, MAP_1
);
8169 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8171 imsm_set_array_size(newdev
, -1);
8174 sp
= (void **)id
->dev
;
8179 /* Clear migration record */
8180 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
8183 *space_list
= tofree
;
8186 update_reshape_exit
:
8191 static int apply_takeover_update(struct imsm_update_takeover
*u
,
8192 struct intel_super
*super
,
8195 struct imsm_dev
*dev
= NULL
;
8196 struct intel_dev
*dv
;
8197 struct imsm_dev
*dev_new
;
8198 struct imsm_map
*map
;
8202 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
8203 if (dv
->index
== (unsigned int)u
->subarray
) {
8211 map
= get_imsm_map(dev
, MAP_0
);
8213 if (u
->direction
== R10_TO_R0
) {
8214 /* Number of failed disks must be half of initial disk number */
8215 if (imsm_count_failed(super
, dev
, MAP_0
) !=
8216 (map
->num_members
/ 2))
8219 /* iterate through devices to mark removed disks as spare */
8220 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8221 if (dm
->disk
.status
& FAILED_DISK
) {
8222 int idx
= dm
->index
;
8223 /* update indexes on the disk list */
8224 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
8225 the index values will end up being correct.... NB */
8226 for (du
= super
->disks
; du
; du
= du
->next
)
8227 if (du
->index
> idx
)
8229 /* mark as spare disk */
8234 map
->num_members
= map
->num_members
/ 2;
8235 map
->map_state
= IMSM_T_STATE_NORMAL
;
8236 map
->num_domains
= 1;
8237 map
->raid_level
= 0;
8238 map
->failed_disk_num
= -1;
8241 if (u
->direction
== R0_TO_R10
) {
8243 /* update slots in current disk list */
8244 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8248 /* create new *missing* disks */
8249 for (i
= 0; i
< map
->num_members
; i
++) {
8250 space
= *space_list
;
8253 *space_list
= *space
;
8255 memcpy(du
, super
->disks
, sizeof(*du
));
8259 du
->index
= (i
* 2) + 1;
8260 sprintf((char *)du
->disk
.serial
,
8261 " MISSING_%d", du
->index
);
8262 sprintf((char *)du
->serial
,
8263 "MISSING_%d", du
->index
);
8264 du
->next
= super
->missing
;
8265 super
->missing
= du
;
8267 /* create new dev and map */
8268 space
= *space_list
;
8271 *space_list
= *space
;
8272 dev_new
= (void *)space
;
8273 memcpy(dev_new
, dev
, sizeof(*dev
));
8274 /* update new map */
8275 map
= get_imsm_map(dev_new
, MAP_0
);
8276 map
->num_members
= map
->num_members
* 2;
8277 map
->map_state
= IMSM_T_STATE_DEGRADED
;
8278 map
->num_domains
= 2;
8279 map
->raid_level
= 1;
8280 /* replace dev<->dev_new */
8283 /* update disk order table */
8284 for (du
= super
->disks
; du
; du
= du
->next
)
8286 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8287 for (du
= super
->missing
; du
; du
= du
->next
)
8288 if (du
->index
>= 0) {
8289 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8290 mark_missing(dv
->dev
, &du
->disk
, du
->index
);
8296 static void imsm_process_update(struct supertype
*st
,
8297 struct metadata_update
*update
)
8300 * crack open the metadata_update envelope to find the update record
8301 * update can be one of:
8302 * update_reshape_container_disks - all the arrays in the container
8303 * are being reshaped to have more devices. We need to mark
8304 * the arrays for general migration and convert selected spares
8305 * into active devices.
8306 * update_activate_spare - a spare device has replaced a failed
8307 * device in an array, update the disk_ord_tbl. If this disk is
8308 * present in all member arrays then also clear the SPARE_DISK
8310 * update_create_array
8312 * update_rename_array
8313 * update_add_remove_disk
8315 struct intel_super
*super
= st
->sb
;
8316 struct imsm_super
*mpb
;
8317 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
8319 /* update requires a larger buf but the allocation failed */
8320 if (super
->next_len
&& !super
->next_buf
) {
8321 super
->next_len
= 0;
8325 if (super
->next_buf
) {
8326 memcpy(super
->next_buf
, super
->buf
, super
->len
);
8328 super
->len
= super
->next_len
;
8329 super
->buf
= super
->next_buf
;
8331 super
->next_len
= 0;
8332 super
->next_buf
= NULL
;
8335 mpb
= super
->anchor
;
8338 case update_general_migration_checkpoint
: {
8339 struct intel_dev
*id
;
8340 struct imsm_update_general_migration_checkpoint
*u
=
8341 (void *)update
->buf
;
8343 dprintf("imsm: process_update() "
8344 "for update_general_migration_checkpoint called\n");
8346 /* find device under general migration */
8347 for (id
= super
->devlist
; id
; id
= id
->next
) {
8348 if (is_gen_migration(id
->dev
)) {
8349 id
->dev
->vol
.curr_migr_unit
=
8350 __cpu_to_le32(u
->curr_migr_unit
);
8351 super
->updates_pending
++;
8356 case update_takeover
: {
8357 struct imsm_update_takeover
*u
= (void *)update
->buf
;
8358 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
8359 imsm_update_version_info(super
);
8360 super
->updates_pending
++;
8365 case update_reshape_container_disks
: {
8366 struct imsm_update_reshape
*u
= (void *)update
->buf
;
8367 if (apply_reshape_container_disks_update(
8368 u
, super
, &update
->space_list
))
8369 super
->updates_pending
++;
8372 case update_reshape_migration
: {
8373 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
8374 if (apply_reshape_migration_update(
8375 u
, super
, &update
->space_list
))
8376 super
->updates_pending
++;
8379 case update_size_change
: {
8380 struct imsm_update_size_change
*u
= (void *)update
->buf
;
8381 if (apply_size_change_update(u
, super
))
8382 super
->updates_pending
++;
8385 case update_activate_spare
: {
8386 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
8387 if (apply_update_activate_spare(u
, super
, st
->arrays
))
8388 super
->updates_pending
++;
8391 case update_create_array
: {
8392 /* someone wants to create a new array, we need to be aware of
8393 * a few races/collisions:
8394 * 1/ 'Create' called by two separate instances of mdadm
8395 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
8396 * devices that have since been assimilated via
8398 * In the event this update can not be carried out mdadm will
8399 * (FIX ME) notice that its update did not take hold.
8401 struct imsm_update_create_array
*u
= (void *) update
->buf
;
8402 struct intel_dev
*dv
;
8403 struct imsm_dev
*dev
;
8404 struct imsm_map
*map
, *new_map
;
8405 unsigned long long start
, end
;
8406 unsigned long long new_start
, new_end
;
8408 struct disk_info
*inf
;
8411 /* handle racing creates: first come first serve */
8412 if (u
->dev_idx
< mpb
->num_raid_devs
) {
8413 dprintf("%s: subarray %d already defined\n",
8414 __func__
, u
->dev_idx
);
8418 /* check update is next in sequence */
8419 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
8420 dprintf("%s: can not create array %d expected index %d\n",
8421 __func__
, u
->dev_idx
, mpb
->num_raid_devs
);
8425 new_map
= get_imsm_map(&u
->dev
, MAP_0
);
8426 new_start
= pba_of_lba0(new_map
);
8427 new_end
= new_start
+ blocks_per_member(new_map
);
8428 inf
= get_disk_info(u
);
8430 /* handle activate_spare versus create race:
8431 * check to make sure that overlapping arrays do not include
8434 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8435 dev
= get_imsm_dev(super
, i
);
8436 map
= get_imsm_map(dev
, MAP_0
);
8437 start
= pba_of_lba0(map
);
8438 end
= start
+ blocks_per_member(map
);
8439 if ((new_start
>= start
&& new_start
<= end
) ||
8440 (start
>= new_start
&& start
<= new_end
))
8445 if (disks_overlap(super
, i
, u
)) {
8446 dprintf("%s: arrays overlap\n", __func__
);
8451 /* check that prepare update was successful */
8452 if (!update
->space
) {
8453 dprintf("%s: prepare update failed\n", __func__
);
8457 /* check that all disks are still active before committing
8458 * changes. FIXME: could we instead handle this by creating a
8459 * degraded array? That's probably not what the user expects,
8460 * so better to drop this update on the floor.
8462 for (i
= 0; i
< new_map
->num_members
; i
++) {
8463 dl
= serial_to_dl(inf
[i
].serial
, super
);
8465 dprintf("%s: disk disappeared\n", __func__
);
8470 super
->updates_pending
++;
8472 /* convert spares to members and fixup ord_tbl */
8473 for (i
= 0; i
< new_map
->num_members
; i
++) {
8474 dl
= serial_to_dl(inf
[i
].serial
, super
);
8475 if (dl
->index
== -1) {
8476 dl
->index
= mpb
->num_disks
;
8478 dl
->disk
.status
|= CONFIGURED_DISK
;
8479 dl
->disk
.status
&= ~SPARE_DISK
;
8481 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
8486 update
->space
= NULL
;
8487 imsm_copy_dev(dev
, &u
->dev
);
8488 dv
->index
= u
->dev_idx
;
8489 dv
->next
= super
->devlist
;
8490 super
->devlist
= dv
;
8491 mpb
->num_raid_devs
++;
8493 imsm_update_version_info(super
);
8496 /* mdmon knows how to release update->space, but not
8497 * ((struct intel_dev *) update->space)->dev
8499 if (update
->space
) {
8505 case update_kill_array
: {
8506 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
8507 int victim
= u
->dev_idx
;
8508 struct active_array
*a
;
8509 struct intel_dev
**dp
;
8510 struct imsm_dev
*dev
;
8512 /* sanity check that we are not affecting the uuid of
8513 * active arrays, or deleting an active array
8515 * FIXME when immutable ids are available, but note that
8516 * we'll also need to fixup the invalidated/active
8517 * subarray indexes in mdstat
8519 for (a
= st
->arrays
; a
; a
= a
->next
)
8520 if (a
->info
.container_member
>= victim
)
8522 /* by definition if mdmon is running at least one array
8523 * is active in the container, so checking
8524 * mpb->num_raid_devs is just extra paranoia
8526 dev
= get_imsm_dev(super
, victim
);
8527 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
8528 dprintf("failed to delete subarray-%d\n", victim
);
8532 for (dp
= &super
->devlist
; *dp
;)
8533 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
8536 if ((*dp
)->index
> (unsigned)victim
)
8540 mpb
->num_raid_devs
--;
8541 super
->updates_pending
++;
8544 case update_rename_array
: {
8545 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
8546 char name
[MAX_RAID_SERIAL_LEN
+1];
8547 int target
= u
->dev_idx
;
8548 struct active_array
*a
;
8549 struct imsm_dev
*dev
;
8551 /* sanity check that we are not affecting the uuid of
8554 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
8555 name
[MAX_RAID_SERIAL_LEN
] = '\0';
8556 for (a
= st
->arrays
; a
; a
= a
->next
)
8557 if (a
->info
.container_member
== target
)
8559 dev
= get_imsm_dev(super
, u
->dev_idx
);
8560 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
8561 dprintf("failed to rename subarray-%d\n", target
);
8565 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
8566 super
->updates_pending
++;
8569 case update_add_remove_disk
: {
8570 /* we may be able to repair some arrays if disks are
8571 * being added, check teh status of add_remove_disk
8572 * if discs has been added.
8574 if (add_remove_disk_update(super
)) {
8575 struct active_array
*a
;
8577 super
->updates_pending
++;
8578 for (a
= st
->arrays
; a
; a
= a
->next
)
8579 a
->check_degraded
= 1;
8584 pr_err("error: unsuported process update type:"
8585 "(type: %d)\n", type
);
8589 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
);
8591 static void imsm_prepare_update(struct supertype
*st
,
8592 struct metadata_update
*update
)
8595 * Allocate space to hold new disk entries, raid-device entries or a new
8596 * mpb if necessary. The manager synchronously waits for updates to
8597 * complete in the monitor, so new mpb buffers allocated here can be
8598 * integrated by the monitor thread without worrying about live pointers
8599 * in the manager thread.
8601 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
8602 struct intel_super
*super
= st
->sb
;
8603 struct imsm_super
*mpb
= super
->anchor
;
8608 case update_general_migration_checkpoint
:
8609 dprintf("imsm: prepare_update() "
8610 "for update_general_migration_checkpoint called\n");
8612 case update_takeover
: {
8613 struct imsm_update_takeover
*u
= (void *)update
->buf
;
8614 if (u
->direction
== R0_TO_R10
) {
8615 void **tail
= (void **)&update
->space_list
;
8616 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
8617 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8618 int num_members
= map
->num_members
;
8621 /* allocate memory for added disks */
8622 for (i
= 0; i
< num_members
; i
++) {
8623 size
= sizeof(struct dl
);
8624 space
= xmalloc(size
);
8629 /* allocate memory for new device */
8630 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
8631 (num_members
* sizeof(__u32
));
8632 space
= xmalloc(size
);
8636 len
= disks_to_mpb_size(num_members
* 2);
8641 case update_reshape_container_disks
: {
8642 /* Every raid device in the container is about to
8643 * gain some more devices, and we will enter a
8645 * So each 'imsm_map' will be bigger, and the imsm_vol
8646 * will now hold 2 of them.
8647 * Thus we need new 'struct imsm_dev' allocations sized
8648 * as sizeof_imsm_dev but with more devices in both maps.
8650 struct imsm_update_reshape
*u
= (void *)update
->buf
;
8651 struct intel_dev
*dl
;
8652 void **space_tail
= (void**)&update
->space_list
;
8654 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
8656 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
8657 int size
= sizeof_imsm_dev(dl
->dev
, 1);
8659 if (u
->new_raid_disks
> u
->old_raid_disks
)
8660 size
+= sizeof(__u32
)*2*
8661 (u
->new_raid_disks
- u
->old_raid_disks
);
8668 len
= disks_to_mpb_size(u
->new_raid_disks
);
8669 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
8672 case update_reshape_migration
: {
8673 /* for migration level 0->5 we need to add disks
8674 * so the same as for container operation we will copy
8675 * device to the bigger location.
8676 * in memory prepared device and new disk area are prepared
8677 * for usage in process update
8679 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
8680 struct intel_dev
*id
;
8681 void **space_tail
= (void **)&update
->space_list
;
8684 int current_level
= -1;
8686 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
8688 /* add space for bigger array in update
8690 for (id
= super
->devlist
; id
; id
= id
->next
) {
8691 if (id
->index
== (unsigned)u
->subdev
) {
8692 size
= sizeof_imsm_dev(id
->dev
, 1);
8693 if (u
->new_raid_disks
> u
->old_raid_disks
)
8694 size
+= sizeof(__u32
)*2*
8695 (u
->new_raid_disks
- u
->old_raid_disks
);
8703 if (update
->space_list
== NULL
)
8706 /* add space for disk in update
8708 size
= sizeof(struct dl
);
8714 /* add spare device to update
8716 for (id
= super
->devlist
; id
; id
= id
->next
)
8717 if (id
->index
== (unsigned)u
->subdev
) {
8718 struct imsm_dev
*dev
;
8719 struct imsm_map
*map
;
8721 dev
= get_imsm_dev(super
, u
->subdev
);
8722 map
= get_imsm_map(dev
, MAP_0
);
8723 current_level
= map
->raid_level
;
8726 if ((u
->new_level
== 5) && (u
->new_level
!= current_level
)) {
8727 struct mdinfo
*spares
;
8729 spares
= get_spares_for_grow(st
);
8737 makedev(dev
->disk
.major
,
8739 dl
= get_disk_super(super
,
8742 dl
->index
= u
->old_raid_disks
;
8748 len
= disks_to_mpb_size(u
->new_raid_disks
);
8749 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
8752 case update_size_change
: {
8755 case update_create_array
: {
8756 struct imsm_update_create_array
*u
= (void *) update
->buf
;
8757 struct intel_dev
*dv
;
8758 struct imsm_dev
*dev
= &u
->dev
;
8759 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8761 struct disk_info
*inf
;
8765 inf
= get_disk_info(u
);
8766 len
= sizeof_imsm_dev(dev
, 1);
8767 /* allocate a new super->devlist entry */
8768 dv
= xmalloc(sizeof(*dv
));
8769 dv
->dev
= xmalloc(len
);
8772 /* count how many spares will be converted to members */
8773 for (i
= 0; i
< map
->num_members
; i
++) {
8774 dl
= serial_to_dl(inf
[i
].serial
, super
);
8776 /* hmm maybe it failed?, nothing we can do about
8781 if (count_memberships(dl
, super
) == 0)
8784 len
+= activate
* sizeof(struct imsm_disk
);
8791 /* check if we need a larger metadata buffer */
8792 if (super
->next_buf
)
8793 buf_len
= super
->next_len
;
8795 buf_len
= super
->len
;
8797 if (__le32_to_cpu(mpb
->mpb_size
) + len
> buf_len
) {
8798 /* ok we need a larger buf than what is currently allocated
8799 * if this allocation fails process_update will notice that
8800 * ->next_len is set and ->next_buf is NULL
8802 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + len
, 512);
8803 if (super
->next_buf
)
8804 free(super
->next_buf
);
8806 super
->next_len
= buf_len
;
8807 if (posix_memalign(&super
->next_buf
, 512, buf_len
) == 0)
8808 memset(super
->next_buf
, 0, buf_len
);
8810 super
->next_buf
= NULL
;
8814 /* must be called while manager is quiesced */
8815 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
8817 struct imsm_super
*mpb
= super
->anchor
;
8819 struct imsm_dev
*dev
;
8820 struct imsm_map
*map
;
8821 int i
, j
, num_members
;
8824 dprintf("%s: deleting device[%d] from imsm_super\n",
8827 /* shift all indexes down one */
8828 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
8829 if (iter
->index
> (int)index
)
8831 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
8832 if (iter
->index
> (int)index
)
8835 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8836 dev
= get_imsm_dev(super
, i
);
8837 map
= get_imsm_map(dev
, MAP_0
);
8838 num_members
= map
->num_members
;
8839 for (j
= 0; j
< num_members
; j
++) {
8840 /* update ord entries being careful not to propagate
8841 * ord-flags to the first map
8843 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_X
);
8845 if (ord_to_idx(ord
) <= index
)
8848 map
= get_imsm_map(dev
, MAP_0
);
8849 set_imsm_ord_tbl_ent(map
, j
, ord_to_idx(ord
- 1));
8850 map
= get_imsm_map(dev
, MAP_1
);
8852 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
8857 super
->updates_pending
++;
8859 struct dl
*dl
= *dlp
;
8861 *dlp
= (*dlp
)->next
;
8862 __free_imsm_disk(dl
);
8865 #endif /* MDASSEMBLE */
8867 static void close_targets(int *targets
, int new_disks
)
8874 for (i
= 0; i
< new_disks
; i
++) {
8875 if (targets
[i
] >= 0) {
8882 static int imsm_get_allowed_degradation(int level
, int raid_disks
,
8883 struct intel_super
*super
,
8884 struct imsm_dev
*dev
)
8890 struct imsm_map
*map
;
8893 ret_val
= raid_disks
/2;
8894 /* check map if all disks pairs not failed
8897 map
= get_imsm_map(dev
, MAP_0
);
8898 for (i
= 0; i
< ret_val
; i
++) {
8899 int degradation
= 0;
8900 if (get_imsm_disk(super
, i
) == NULL
)
8902 if (get_imsm_disk(super
, i
+ 1) == NULL
)
8904 if (degradation
== 2)
8907 map
= get_imsm_map(dev
, MAP_1
);
8908 /* if there is no second map
8909 * result can be returned
8913 /* check degradation in second map
8915 for (i
= 0; i
< ret_val
; i
++) {
8916 int degradation
= 0;
8917 if (get_imsm_disk(super
, i
) == NULL
)
8919 if (get_imsm_disk(super
, i
+ 1) == NULL
)
8921 if (degradation
== 2)
8935 /*******************************************************************************
8936 * Function: open_backup_targets
8937 * Description: Function opens file descriptors for all devices given in
8940 * info : general array info
8941 * raid_disks : number of disks
8942 * raid_fds : table of device's file descriptors
8943 * super : intel super for raid10 degradation check
8944 * dev : intel device for raid10 degradation check
8948 ******************************************************************************/
8949 int open_backup_targets(struct mdinfo
*info
, int raid_disks
, int *raid_fds
,
8950 struct intel_super
*super
, struct imsm_dev
*dev
)
8956 for (i
= 0; i
< raid_disks
; i
++)
8959 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
8962 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
)) {
8963 dprintf("disk is faulty!!\n");
8967 if ((sd
->disk
.raid_disk
>= raid_disks
) ||
8968 (sd
->disk
.raid_disk
< 0))
8971 dn
= map_dev(sd
->disk
.major
,
8973 raid_fds
[sd
->disk
.raid_disk
] = dev_open(dn
, O_RDWR
);
8974 if (raid_fds
[sd
->disk
.raid_disk
] < 0) {
8975 pr_err("cannot open component\n");
8980 /* check if maximum array degradation level is not exceeded
8982 if ((raid_disks
- opened
) >
8983 imsm_get_allowed_degradation(info
->new_level
,
8986 pr_err("Not enough disks can be opened.\n");
8987 close_targets(raid_fds
, raid_disks
);
8994 /*******************************************************************************
8995 * Function: init_migr_record_imsm
8996 * Description: Function inits imsm migration record
8998 * super : imsm internal array info
8999 * dev : device under migration
9000 * info : general array info to find the smallest device
9003 ******************************************************************************/
9004 void init_migr_record_imsm(struct supertype
*st
, struct imsm_dev
*dev
,
9005 struct mdinfo
*info
)
9007 struct intel_super
*super
= st
->sb
;
9008 struct migr_record
*migr_rec
= super
->migr_rec
;
9010 unsigned long long dsize
, dev_sectors
;
9011 long long unsigned min_dev_sectors
= -1LLU;
9015 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
9016 struct imsm_map
*map_src
= get_imsm_map(dev
, MAP_1
);
9017 unsigned long long num_migr_units
;
9018 unsigned long long array_blocks
;
9020 memset(migr_rec
, 0, sizeof(struct migr_record
));
9021 migr_rec
->family_num
= __cpu_to_le32(super
->anchor
->family_num
);
9023 /* only ascending reshape supported now */
9024 migr_rec
->ascending_migr
= __cpu_to_le32(1);
9026 migr_rec
->dest_depth_per_unit
= GEN_MIGR_AREA_SIZE
/
9027 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
9028 migr_rec
->dest_depth_per_unit
*=
9029 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
9030 new_data_disks
= imsm_num_data_members(dev
, MAP_0
);
9031 migr_rec
->blocks_per_unit
=
9032 __cpu_to_le32(migr_rec
->dest_depth_per_unit
* new_data_disks
);
9033 migr_rec
->dest_depth_per_unit
=
9034 __cpu_to_le32(migr_rec
->dest_depth_per_unit
);
9035 array_blocks
= info
->component_size
* new_data_disks
;
9037 array_blocks
/ __le32_to_cpu(migr_rec
->blocks_per_unit
);
9039 if (array_blocks
% __le32_to_cpu(migr_rec
->blocks_per_unit
))
9041 migr_rec
->num_migr_units
= __cpu_to_le32(num_migr_units
);
9043 migr_rec
->post_migr_vol_cap
= dev
->size_low
;
9044 migr_rec
->post_migr_vol_cap_hi
= dev
->size_high
;
9046 /* Find the smallest dev */
9047 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
9048 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
9049 fd
= dev_open(nm
, O_RDONLY
);
9052 get_dev_size(fd
, NULL
, &dsize
);
9053 dev_sectors
= dsize
/ 512;
9054 if (dev_sectors
< min_dev_sectors
)
9055 min_dev_sectors
= dev_sectors
;
9058 migr_rec
->ckpt_area_pba
= __cpu_to_le32(min_dev_sectors
-
9059 RAID_DISK_RESERVED_BLOCKS_IMSM_HI
);
9061 write_imsm_migr_rec(st
);
9066 /*******************************************************************************
9067 * Function: save_backup_imsm
9068 * Description: Function saves critical data stripes to Migration Copy Area
9069 * and updates the current migration unit status.
9070 * Use restore_stripes() to form a destination stripe,
9071 * and to write it to the Copy Area.
9073 * st : supertype information
9074 * dev : imsm device that backup is saved for
9075 * info : general array info
9076 * buf : input buffer
9077 * length : length of data to backup (blocks_per_unit)
9081 ******************************************************************************/
9082 int save_backup_imsm(struct supertype
*st
,
9083 struct imsm_dev
*dev
,
9084 struct mdinfo
*info
,
9089 struct intel_super
*super
= st
->sb
;
9090 unsigned long long *target_offsets
= NULL
;
9091 int *targets
= NULL
;
9093 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
9094 int new_disks
= map_dest
->num_members
;
9095 int dest_layout
= 0;
9097 unsigned long long start
;
9098 int data_disks
= imsm_num_data_members(dev
, MAP_0
);
9100 targets
= xmalloc(new_disks
* sizeof(int));
9102 for (i
= 0; i
< new_disks
; i
++)
9105 target_offsets
= xcalloc(new_disks
, sizeof(unsigned long long));
9107 start
= info
->reshape_progress
* 512;
9108 for (i
= 0; i
< new_disks
; i
++) {
9109 target_offsets
[i
] = (unsigned long long)
9110 __le32_to_cpu(super
->migr_rec
->ckpt_area_pba
) * 512;
9111 /* move back copy area adderss, it will be moved forward
9112 * in restore_stripes() using start input variable
9114 target_offsets
[i
] -= start
/data_disks
;
9117 if (open_backup_targets(info
, new_disks
, targets
,
9121 dest_layout
= imsm_level_to_layout(map_dest
->raid_level
);
9122 dest_chunk
= __le16_to_cpu(map_dest
->blocks_per_strip
) * 512;
9124 if (restore_stripes(targets
, /* list of dest devices */
9125 target_offsets
, /* migration record offsets */
9128 map_dest
->raid_level
,
9130 -1, /* source backup file descriptor */
9131 0, /* input buf offset
9132 * always 0 buf is already offseted */
9136 pr_err("Error restoring stripes\n");
9144 close_targets(targets
, new_disks
);
9147 free(target_offsets
);
9152 /*******************************************************************************
9153 * Function: save_checkpoint_imsm
9154 * Description: Function called for current unit status update
9155 * in the migration record. It writes it to disk.
9157 * super : imsm internal array info
9158 * info : general array info
9162 * 2: failure, means no valid migration record
9163 * / no general migration in progress /
9164 ******************************************************************************/
9165 int save_checkpoint_imsm(struct supertype
*st
, struct mdinfo
*info
, int state
)
9167 struct intel_super
*super
= st
->sb
;
9168 unsigned long long blocks_per_unit
;
9169 unsigned long long curr_migr_unit
;
9171 if (load_imsm_migr_rec(super
, info
) != 0) {
9172 dprintf("imsm: ERROR: Cannot read migration record "
9173 "for checkpoint save.\n");
9177 blocks_per_unit
= __le32_to_cpu(super
->migr_rec
->blocks_per_unit
);
9178 if (blocks_per_unit
== 0) {
9179 dprintf("imsm: no migration in progress.\n");
9182 curr_migr_unit
= info
->reshape_progress
/ blocks_per_unit
;
9183 /* check if array is alligned to copy area
9184 * if it is not alligned, add one to current migration unit value
9185 * this can happend on array reshape finish only
9187 if (info
->reshape_progress
% blocks_per_unit
)
9190 super
->migr_rec
->curr_migr_unit
=
9191 __cpu_to_le32(curr_migr_unit
);
9192 super
->migr_rec
->rec_status
= __cpu_to_le32(state
);
9193 super
->migr_rec
->dest_1st_member_lba
=
9194 __cpu_to_le32(curr_migr_unit
*
9195 __le32_to_cpu(super
->migr_rec
->dest_depth_per_unit
));
9196 if (write_imsm_migr_rec(st
) < 0) {
9197 dprintf("imsm: Cannot write migration record "
9198 "outside backup area\n");
9205 /*******************************************************************************
9206 * Function: recover_backup_imsm
9207 * Description: Function recovers critical data from the Migration Copy Area
9208 * while assembling an array.
9210 * super : imsm internal array info
9211 * info : general array info
9213 * 0 : success (or there is no data to recover)
9215 ******************************************************************************/
9216 int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
)
9218 struct intel_super
*super
= st
->sb
;
9219 struct migr_record
*migr_rec
= super
->migr_rec
;
9220 struct imsm_map
*map_dest
= NULL
;
9221 struct intel_dev
*id
= NULL
;
9222 unsigned long long read_offset
;
9223 unsigned long long write_offset
;
9225 int *targets
= NULL
;
9226 int new_disks
, i
, err
;
9229 unsigned long curr_migr_unit
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
9230 unsigned long num_migr_units
= __le32_to_cpu(migr_rec
->num_migr_units
);
9232 int skipped_disks
= 0;
9234 err
= sysfs_get_str(info
, NULL
, "array_state", (char *)buffer
, 20);
9238 /* recover data only during assemblation */
9239 if (strncmp(buffer
, "inactive", 8) != 0)
9241 /* no data to recover */
9242 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
9244 if (curr_migr_unit
>= num_migr_units
)
9247 /* find device during reshape */
9248 for (id
= super
->devlist
; id
; id
= id
->next
)
9249 if (is_gen_migration(id
->dev
))
9254 map_dest
= get_imsm_map(id
->dev
, MAP_0
);
9255 new_disks
= map_dest
->num_members
;
9257 read_offset
= (unsigned long long)
9258 __le32_to_cpu(migr_rec
->ckpt_area_pba
) * 512;
9260 write_offset
= ((unsigned long long)
9261 __le32_to_cpu(migr_rec
->dest_1st_member_lba
) +
9262 pba_of_lba0(map_dest
)) * 512;
9264 unit_len
= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
9265 if (posix_memalign((void **)&buf
, 512, unit_len
) != 0)
9267 targets
= xcalloc(new_disks
, sizeof(int));
9269 if (open_backup_targets(info
, new_disks
, targets
, super
, id
->dev
)) {
9270 pr_err("Cannot open some devices belonging to array.\n");
9274 for (i
= 0; i
< new_disks
; i
++) {
9275 if (targets
[i
] < 0) {
9279 if (lseek64(targets
[i
], read_offset
, SEEK_SET
) < 0) {
9280 pr_err("Cannot seek to block: %s\n",
9285 if ((unsigned)read(targets
[i
], buf
, unit_len
) != unit_len
) {
9286 pr_err("Cannot read copy area block: %s\n",
9291 if (lseek64(targets
[i
], write_offset
, SEEK_SET
) < 0) {
9292 pr_err("Cannot seek to block: %s\n",
9297 if ((unsigned)write(targets
[i
], buf
, unit_len
) != unit_len
) {
9298 pr_err("Cannot restore block: %s\n",
9305 if (skipped_disks
> imsm_get_allowed_degradation(info
->new_level
,
9309 pr_err("Cannot restore data from backup."
9310 " Too many failed disks\n");
9314 if (save_checkpoint_imsm(st
, info
, UNIT_SRC_NORMAL
)) {
9315 /* ignore error == 2, this can mean end of reshape here
9317 dprintf("imsm: Cannot write checkpoint to "
9318 "migration record (UNIT_SRC_NORMAL) during restart\n");
9324 for (i
= 0; i
< new_disks
; i
++)
9333 static char disk_by_path
[] = "/dev/disk/by-path/";
9335 static const char *imsm_get_disk_controller_domain(const char *path
)
9337 char disk_path
[PATH_MAX
];
9341 strncpy(disk_path
, disk_by_path
, PATH_MAX
- 1);
9342 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
9343 if (stat(disk_path
, &st
) == 0) {
9344 struct sys_dev
* hba
;
9347 path
= devt_to_devpath(st
.st_rdev
);
9350 hba
= find_disk_attached_hba(-1, path
);
9351 if (hba
&& hba
->type
== SYS_DEV_SAS
)
9353 else if (hba
&& hba
->type
== SYS_DEV_SATA
)
9357 dprintf("path: %s hba: %s attached: %s\n",
9358 path
, (hba
) ? hba
->path
: "NULL", drv
);
9364 static char *imsm_find_array_devnm_by_subdev(int subdev
, char *container
)
9366 static char devnm
[32];
9367 char subdev_name
[20];
9368 struct mdstat_ent
*mdstat
;
9370 sprintf(subdev_name
, "%d", subdev
);
9371 mdstat
= mdstat_by_subdev(subdev_name
, container
);
9375 strcpy(devnm
, mdstat
->devnm
);
9376 free_mdstat(mdstat
);
9380 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
9381 struct geo_params
*geo
,
9382 int *old_raid_disks
,
9385 /* currently we only support increasing the number of devices
9386 * for a container. This increases the number of device for each
9387 * member array. They must all be RAID0 or RAID5.
9390 struct mdinfo
*info
, *member
;
9391 int devices_that_can_grow
= 0;
9393 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): "
9394 "st->devnm = (%s)\n", st
->devnm
);
9396 if (geo
->size
> 0 ||
9397 geo
->level
!= UnSet
||
9398 geo
->layout
!= UnSet
||
9399 geo
->chunksize
!= 0 ||
9400 geo
->raid_disks
== UnSet
) {
9401 dprintf("imsm: Container operation is allowed for "
9402 "raid disks number change only.\n");
9406 if (direction
== ROLLBACK_METADATA_CHANGES
) {
9407 dprintf("imsm: Metadata changes rollback is not supported for "
9408 "container operation.\n");
9412 info
= container_content_imsm(st
, NULL
);
9413 for (member
= info
; member
; member
= member
->next
) {
9416 dprintf("imsm: checking device_num: %i\n",
9417 member
->container_member
);
9419 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
9420 /* we work on container for Online Capacity Expansion
9421 * only so raid_disks has to grow
9423 dprintf("imsm: for container operation raid disks "
9424 "increase is required\n");
9428 if ((info
->array
.level
!= 0) &&
9429 (info
->array
.level
!= 5)) {
9430 /* we cannot use this container with other raid level
9432 dprintf("imsm: for container operation wrong"
9433 " raid level (%i) detected\n",
9437 /* check for platform support
9438 * for this raid level configuration
9440 struct intel_super
*super
= st
->sb
;
9441 if (!is_raid_level_supported(super
->orom
,
9442 member
->array
.level
,
9444 dprintf("platform does not support raid%d with"
9448 geo
->raid_disks
> 1 ? "s" : "");
9451 /* check if component size is aligned to chunk size
9453 if (info
->component_size
%
9454 (info
->array
.chunk_size
/512)) {
9455 dprintf("Component size is not aligned to "
9461 if (*old_raid_disks
&&
9462 info
->array
.raid_disks
!= *old_raid_disks
)
9464 *old_raid_disks
= info
->array
.raid_disks
;
9466 /* All raid5 and raid0 volumes in container
9467 * have to be ready for Online Capacity Expansion
9468 * so they need to be assembled. We have already
9469 * checked that no recovery etc is happening.
9471 result
= imsm_find_array_devnm_by_subdev(member
->container_member
,
9472 st
->container_devnm
);
9473 if (result
== NULL
) {
9474 dprintf("imsm: cannot find array\n");
9477 devices_that_can_grow
++;
9480 if (!member
&& devices_that_can_grow
)
9484 dprintf("\tContainer operation allowed\n");
9486 dprintf("\tError: %i\n", ret_val
);
9491 /* Function: get_spares_for_grow
9492 * Description: Allocates memory and creates list of spare devices
9493 * avaliable in container. Checks if spare drive size is acceptable.
9494 * Parameters: Pointer to the supertype structure
9495 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
9498 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
9500 unsigned long long min_size
= min_acceptable_spare_size_imsm(st
);
9501 return container_choose_spares(st
, min_size
, NULL
, NULL
, NULL
, 0);
9504 /******************************************************************************
9505 * function: imsm_create_metadata_update_for_reshape
9506 * Function creates update for whole IMSM container.
9508 ******************************************************************************/
9509 static int imsm_create_metadata_update_for_reshape(
9510 struct supertype
*st
,
9511 struct geo_params
*geo
,
9513 struct imsm_update_reshape
**updatep
)
9515 struct intel_super
*super
= st
->sb
;
9516 struct imsm_super
*mpb
= super
->anchor
;
9517 int update_memory_size
= 0;
9518 struct imsm_update_reshape
*u
= NULL
;
9519 struct mdinfo
*spares
= NULL
;
9521 int delta_disks
= 0;
9524 dprintf("imsm_update_metadata_for_reshape(enter) raid_disks = %i\n",
9527 delta_disks
= geo
->raid_disks
- old_raid_disks
;
9529 /* size of all update data without anchor */
9530 update_memory_size
= sizeof(struct imsm_update_reshape
);
9532 /* now add space for spare disks that we need to add. */
9533 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
9535 u
= xcalloc(1, update_memory_size
);
9536 u
->type
= update_reshape_container_disks
;
9537 u
->old_raid_disks
= old_raid_disks
;
9538 u
->new_raid_disks
= geo
->raid_disks
;
9540 /* now get spare disks list
9542 spares
= get_spares_for_grow(st
);
9545 || delta_disks
> spares
->array
.spare_disks
) {
9546 pr_err("imsm: ERROR: Cannot get spare devices "
9547 "for %s.\n", geo
->dev_name
);
9552 /* we have got spares
9553 * update disk list in imsm_disk list table in anchor
9555 dprintf("imsm: %i spares are available.\n\n",
9556 spares
->array
.spare_disks
);
9559 for (i
= 0; i
< delta_disks
; i
++) {
9564 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
9566 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
9567 dl
->index
= mpb
->num_disks
;
9577 dprintf("imsm: reshape update preparation :");
9578 if (i
== delta_disks
) {
9581 return update_memory_size
;
9584 dprintf(" Error\n");
9589 /******************************************************************************
9590 * function: imsm_create_metadata_update_for_size_change()
9591 * Creates update for IMSM array for array size change.
9593 ******************************************************************************/
9594 static int imsm_create_metadata_update_for_size_change(
9595 struct supertype
*st
,
9596 struct geo_params
*geo
,
9597 struct imsm_update_size_change
**updatep
)
9599 struct intel_super
*super
= st
->sb
;
9600 int update_memory_size
= 0;
9601 struct imsm_update_size_change
*u
= NULL
;
9603 dprintf("imsm_create_metadata_update_for_size_change(enter)"
9604 " New size = %llu\n", geo
->size
);
9606 /* size of all update data without anchor */
9607 update_memory_size
= sizeof(struct imsm_update_size_change
);
9609 u
= xcalloc(1, update_memory_size
);
9610 u
->type
= update_size_change
;
9611 u
->subdev
= super
->current_vol
;
9612 u
->new_size
= geo
->size
;
9614 dprintf("imsm: reshape update preparation : OK\n");
9617 return update_memory_size
;
9620 /******************************************************************************
9621 * function: imsm_create_metadata_update_for_migration()
9622 * Creates update for IMSM array.
9624 ******************************************************************************/
9625 static int imsm_create_metadata_update_for_migration(
9626 struct supertype
*st
,
9627 struct geo_params
*geo
,
9628 struct imsm_update_reshape_migration
**updatep
)
9630 struct intel_super
*super
= st
->sb
;
9631 int update_memory_size
= 0;
9632 struct imsm_update_reshape_migration
*u
= NULL
;
9633 struct imsm_dev
*dev
;
9634 int previous_level
= -1;
9636 dprintf("imsm_create_metadata_update_for_migration(enter)"
9637 " New Level = %i\n", geo
->level
);
9639 /* size of all update data without anchor */
9640 update_memory_size
= sizeof(struct imsm_update_reshape_migration
);
9642 u
= xcalloc(1, update_memory_size
);
9643 u
->type
= update_reshape_migration
;
9644 u
->subdev
= super
->current_vol
;
9645 u
->new_level
= geo
->level
;
9646 u
->new_layout
= geo
->layout
;
9647 u
->new_raid_disks
= u
->old_raid_disks
= geo
->raid_disks
;
9648 u
->new_disks
[0] = -1;
9649 u
->new_chunksize
= -1;
9651 dev
= get_imsm_dev(super
, u
->subdev
);
9653 struct imsm_map
*map
;
9655 map
= get_imsm_map(dev
, MAP_0
);
9657 int current_chunk_size
=
9658 __le16_to_cpu(map
->blocks_per_strip
) / 2;
9660 if (geo
->chunksize
!= current_chunk_size
) {
9661 u
->new_chunksize
= geo
->chunksize
/ 1024;
9663 "chunk size change from %i to %i\n",
9664 current_chunk_size
, u
->new_chunksize
);
9666 previous_level
= map
->raid_level
;
9669 if ((geo
->level
== 5) && (previous_level
== 0)) {
9670 struct mdinfo
*spares
= NULL
;
9672 u
->new_raid_disks
++;
9673 spares
= get_spares_for_grow(st
);
9674 if ((spares
== NULL
) || (spares
->array
.spare_disks
< 1)) {
9677 update_memory_size
= 0;
9678 dprintf("error: cannot get spare device "
9679 "for requested migration");
9684 dprintf("imsm: reshape update preparation : OK\n");
9687 return update_memory_size
;
9690 static void imsm_update_metadata_locally(struct supertype
*st
,
9693 struct metadata_update mu
;
9698 mu
.space_list
= NULL
;
9700 imsm_prepare_update(st
, &mu
);
9701 imsm_process_update(st
, &mu
);
9703 while (mu
.space_list
) {
9704 void **space
= mu
.space_list
;
9705 mu
.space_list
= *space
;
9710 /***************************************************************************
9711 * Function: imsm_analyze_change
9712 * Description: Function analyze change for single volume
9713 * and validate if transition is supported
9714 * Parameters: Geometry parameters, supertype structure,
9715 * metadata change direction (apply/rollback)
9716 * Returns: Operation type code on success, -1 if fail
9717 ****************************************************************************/
9718 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
9719 struct geo_params
*geo
,
9726 /* number of added/removed disks in operation result */
9727 int devNumChange
= 0;
9728 /* imsm compatible layout value for array geometry verification */
9729 int imsm_layout
= -1;
9731 struct imsm_dev
*dev
;
9732 struct intel_super
*super
;
9733 unsigned long long current_size
;
9734 unsigned long long free_size
;
9735 unsigned long long max_size
;
9738 getinfo_super_imsm_volume(st
, &info
, NULL
);
9739 if ((geo
->level
!= info
.array
.level
) &&
9740 (geo
->level
>= 0) &&
9741 (geo
->level
!= UnSet
)) {
9742 switch (info
.array
.level
) {
9744 if (geo
->level
== 5) {
9745 change
= CH_MIGRATION
;
9746 if (geo
->layout
!= ALGORITHM_LEFT_ASYMMETRIC
) {
9747 pr_err("Error. Requested Layout "
9748 "not supported (left-asymmetric layout "
9749 "is supported only)!\n");
9751 goto analyse_change_exit
;
9753 imsm_layout
= geo
->layout
;
9755 devNumChange
= 1; /* parity disk added */
9756 } else if (geo
->level
== 10) {
9757 change
= CH_TAKEOVER
;
9759 devNumChange
= 2; /* two mirrors added */
9760 imsm_layout
= 0x102; /* imsm supported layout */
9765 if (geo
->level
== 0) {
9766 change
= CH_TAKEOVER
;
9768 devNumChange
= -(geo
->raid_disks
/2);
9769 imsm_layout
= 0; /* imsm raid0 layout */
9774 pr_err("Error. Level Migration from %d to %d "
9776 info
.array
.level
, geo
->level
);
9777 goto analyse_change_exit
;
9780 geo
->level
= info
.array
.level
;
9782 if ((geo
->layout
!= info
.array
.layout
)
9783 && ((geo
->layout
!= UnSet
) && (geo
->layout
!= -1))) {
9784 change
= CH_MIGRATION
;
9785 if ((info
.array
.layout
== 0)
9786 && (info
.array
.level
== 5)
9787 && (geo
->layout
== 5)) {
9788 /* reshape 5 -> 4 */
9789 } else if ((info
.array
.layout
== 5)
9790 && (info
.array
.level
== 5)
9791 && (geo
->layout
== 0)) {
9792 /* reshape 4 -> 5 */
9796 pr_err("Error. Layout Migration from %d to %d "
9798 info
.array
.layout
, geo
->layout
);
9800 goto analyse_change_exit
;
9803 geo
->layout
= info
.array
.layout
;
9804 if (imsm_layout
== -1)
9805 imsm_layout
= info
.array
.layout
;
9808 if ((geo
->chunksize
> 0) && (geo
->chunksize
!= UnSet
)
9809 && (geo
->chunksize
!= info
.array
.chunk_size
))
9810 change
= CH_MIGRATION
;
9812 geo
->chunksize
= info
.array
.chunk_size
;
9814 chunk
= geo
->chunksize
/ 1024;
9817 dev
= get_imsm_dev(super
, super
->current_vol
);
9818 data_disks
= imsm_num_data_members(dev
, MAP_0
);
9819 /* compute current size per disk member
9821 current_size
= info
.custom_array_size
/ data_disks
;
9823 if ((geo
->size
> 0) && (geo
->size
!= MAX_SIZE
)) {
9824 /* align component size
9826 geo
->size
= imsm_component_size_aligment_check(
9827 get_imsm_raid_level(dev
->vol
.map
),
9830 if (geo
->size
== 0) {
9831 pr_err("Error. Size expansion is " \
9832 "supported only (current size is %llu, " \
9833 "requested size /rounded/ is 0).\n",
9835 goto analyse_change_exit
;
9839 if ((current_size
!= geo
->size
) && (geo
->size
> 0)) {
9841 pr_err("Error. Size change should be the only "
9842 "one at a time.\n");
9844 goto analyse_change_exit
;
9846 if ((super
->current_vol
+ 1) != super
->anchor
->num_raid_devs
) {
9847 pr_err("Error. The last volume in container "
9848 "can be expanded only (%i/%s).\n",
9849 super
->current_vol
, st
->devnm
);
9850 goto analyse_change_exit
;
9852 /* check the maximum available size
9854 rv
= imsm_get_free_size(st
, dev
->vol
.map
->num_members
,
9855 0, chunk
, &free_size
);
9857 /* Cannot find maximum available space
9861 max_size
= free_size
+ current_size
;
9862 /* align component size
9864 max_size
= imsm_component_size_aligment_check(
9865 get_imsm_raid_level(dev
->vol
.map
),
9869 if (geo
->size
== MAX_SIZE
) {
9870 /* requested size change to the maximum available size
9872 if (max_size
== 0) {
9873 pr_err("Error. Cannot find "
9874 "maximum available space.\n");
9876 goto analyse_change_exit
;
9878 geo
->size
= max_size
;
9881 if ((direction
== ROLLBACK_METADATA_CHANGES
)) {
9882 /* accept size for rollback only
9885 /* round size due to metadata compatibility
9887 geo
->size
= (geo
->size
>> SECT_PER_MB_SHIFT
)
9888 << SECT_PER_MB_SHIFT
;
9889 dprintf("Prepare update for size change to %llu\n",
9891 if (current_size
>= geo
->size
) {
9892 pr_err("Error. Size expansion is "
9893 "supported only (current size is %llu, "
9894 "requested size /rounded/ is %llu).\n",
9895 current_size
, geo
->size
);
9896 goto analyse_change_exit
;
9898 if (max_size
&& geo
->size
> max_size
) {
9899 pr_err("Error. Requested size is larger "
9900 "than maximum available size (maximum "
9901 "available size is %llu, "
9902 "requested size /rounded/ is %llu).\n",
9903 max_size
, geo
->size
);
9904 goto analyse_change_exit
;
9907 geo
->size
*= data_disks
;
9908 geo
->raid_disks
= dev
->vol
.map
->num_members
;
9909 change
= CH_ARRAY_SIZE
;
9911 if (!validate_geometry_imsm(st
,
9914 geo
->raid_disks
+ devNumChange
,
9916 geo
->size
, INVALID_SECTORS
,
9921 struct intel_super
*super
= st
->sb
;
9922 struct imsm_super
*mpb
= super
->anchor
;
9924 if (mpb
->num_raid_devs
> 1) {
9925 pr_err("Error. Cannot perform operation on %s"
9926 "- for this operation it MUST be single "
9927 "array in container\n",
9933 analyse_change_exit
:
9934 if ((direction
== ROLLBACK_METADATA_CHANGES
) &&
9935 ((change
== CH_MIGRATION
) || (change
== CH_TAKEOVER
))) {
9936 dprintf("imsm: Metadata changes rollback is not supported for "
9937 "migration and takeover operations.\n");
9943 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
9945 struct intel_super
*super
= st
->sb
;
9946 struct imsm_update_takeover
*u
;
9948 u
= xmalloc(sizeof(struct imsm_update_takeover
));
9950 u
->type
= update_takeover
;
9951 u
->subarray
= super
->current_vol
;
9953 /* 10->0 transition */
9954 if (geo
->level
== 0)
9955 u
->direction
= R10_TO_R0
;
9957 /* 0->10 transition */
9958 if (geo
->level
== 10)
9959 u
->direction
= R0_TO_R10
;
9961 /* update metadata locally */
9962 imsm_update_metadata_locally(st
, u
,
9963 sizeof(struct imsm_update_takeover
));
9964 /* and possibly remotely */
9965 if (st
->update_tail
)
9966 append_metadata_update(st
, u
,
9967 sizeof(struct imsm_update_takeover
));
9974 static int imsm_reshape_super(struct supertype
*st
, unsigned long long size
,
9976 int layout
, int chunksize
, int raid_disks
,
9977 int delta_disks
, char *backup
, char *dev
,
9978 int direction
, int verbose
)
9981 struct geo_params geo
;
9983 dprintf("imsm: reshape_super called.\n");
9985 memset(&geo
, 0, sizeof(struct geo_params
));
9988 strcpy(geo
.devnm
, st
->devnm
);
9991 geo
.layout
= layout
;
9992 geo
.chunksize
= chunksize
;
9993 geo
.raid_disks
= raid_disks
;
9994 if (delta_disks
!= UnSet
)
9995 geo
.raid_disks
+= delta_disks
;
9997 dprintf("\tfor level : %i\n", geo
.level
);
9998 dprintf("\tfor raid_disks : %i\n", geo
.raid_disks
);
10000 if (experimental() == 0)
10003 if (strcmp(st
->container_devnm
, st
->devnm
) == 0) {
10004 /* On container level we can only increase number of devices. */
10005 dprintf("imsm: info: Container operation\n");
10006 int old_raid_disks
= 0;
10008 if (imsm_reshape_is_allowed_on_container(
10009 st
, &geo
, &old_raid_disks
, direction
)) {
10010 struct imsm_update_reshape
*u
= NULL
;
10013 len
= imsm_create_metadata_update_for_reshape(
10014 st
, &geo
, old_raid_disks
, &u
);
10017 dprintf("imsm: Cannot prepare update\n");
10018 goto exit_imsm_reshape_super
;
10022 /* update metadata locally */
10023 imsm_update_metadata_locally(st
, u
, len
);
10024 /* and possibly remotely */
10025 if (st
->update_tail
)
10026 append_metadata_update(st
, u
, len
);
10031 pr_err("(imsm) Operation "
10032 "is not allowed on this container\n");
10035 /* On volume level we support following operations
10036 * - takeover: raid10 -> raid0; raid0 -> raid10
10037 * - chunk size migration
10038 * - migration: raid5 -> raid0; raid0 -> raid5
10040 struct intel_super
*super
= st
->sb
;
10041 struct intel_dev
*dev
= super
->devlist
;
10043 dprintf("imsm: info: Volume operation\n");
10044 /* find requested device */
10047 imsm_find_array_devnm_by_subdev(
10048 dev
->index
, st
->container_devnm
);
10049 if (devnm
&& strcmp(devnm
, geo
.devnm
) == 0)
10054 pr_err("Cannot find %s (%s) subarray\n",
10055 geo
.dev_name
, geo
.devnm
);
10056 goto exit_imsm_reshape_super
;
10058 super
->current_vol
= dev
->index
;
10059 change
= imsm_analyze_change(st
, &geo
, direction
);
10062 ret_val
= imsm_takeover(st
, &geo
);
10064 case CH_MIGRATION
: {
10065 struct imsm_update_reshape_migration
*u
= NULL
;
10067 imsm_create_metadata_update_for_migration(
10071 "Cannot prepare update\n");
10075 /* update metadata locally */
10076 imsm_update_metadata_locally(st
, u
, len
);
10077 /* and possibly remotely */
10078 if (st
->update_tail
)
10079 append_metadata_update(st
, u
, len
);
10084 case CH_ARRAY_SIZE
: {
10085 struct imsm_update_size_change
*u
= NULL
;
10087 imsm_create_metadata_update_for_size_change(
10091 "Cannot prepare update\n");
10095 /* update metadata locally */
10096 imsm_update_metadata_locally(st
, u
, len
);
10097 /* and possibly remotely */
10098 if (st
->update_tail
)
10099 append_metadata_update(st
, u
, len
);
10109 exit_imsm_reshape_super
:
10110 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
10114 /*******************************************************************************
10115 * Function: wait_for_reshape_imsm
10116 * Description: Function writes new sync_max value and waits until
10117 * reshape process reach new position
10119 * sra : general array info
10120 * ndata : number of disks in new array's layout
10123 * 1 : there is no reshape in progress,
10125 ******************************************************************************/
10126 int wait_for_reshape_imsm(struct mdinfo
*sra
, int ndata
)
10128 int fd
= sysfs_get_fd(sra
, NULL
, "sync_completed");
10129 unsigned long long completed
;
10130 /* to_complete : new sync_max position */
10131 unsigned long long to_complete
= sra
->reshape_progress
;
10132 unsigned long long position_to_set
= to_complete
/ ndata
;
10135 dprintf("imsm: wait_for_reshape_imsm() "
10136 "cannot open reshape_position\n");
10140 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
10141 dprintf("imsm: wait_for_reshape_imsm() "
10142 "cannot read reshape_position (no reshape in progres)\n");
10147 if (completed
> position_to_set
) {
10148 dprintf("imsm: wait_for_reshape_imsm() "
10149 "wrong next position to set %llu (%llu)\n",
10150 to_complete
, position_to_set
);
10154 dprintf("Position set: %llu\n", position_to_set
);
10155 if (sysfs_set_num(sra
, NULL
, "sync_max",
10156 position_to_set
) != 0) {
10157 dprintf("imsm: wait_for_reshape_imsm() "
10158 "cannot set reshape position to %llu\n",
10166 sysfs_wait(fd
, NULL
);
10167 if (sysfs_get_str(sra
, NULL
, "sync_action",
10169 strncmp(action
, "reshape", 7) != 0)
10171 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
10172 dprintf("imsm: wait_for_reshape_imsm() "
10173 "cannot read reshape_position (in loop)\n");
10177 } while (completed
< position_to_set
);
10183 /*******************************************************************************
10184 * Function: check_degradation_change
10185 * Description: Check that array hasn't become failed.
10187 * info : for sysfs access
10188 * sources : source disks descriptors
10189 * degraded: previous degradation level
10191 * degradation level
10192 ******************************************************************************/
10193 int check_degradation_change(struct mdinfo
*info
,
10197 unsigned long long new_degraded
;
10200 rv
= sysfs_get_ll(info
, NULL
, "degraded", &new_degraded
);
10201 if ((rv
== -1) || (new_degraded
!= (unsigned long long)degraded
)) {
10202 /* check each device to ensure it is still working */
10205 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
10206 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
10208 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
10210 if (sysfs_get_str(info
,
10211 sd
, "state", sbuf
, 20) < 0 ||
10212 strstr(sbuf
, "faulty") ||
10213 strstr(sbuf
, "in_sync") == NULL
) {
10214 /* this device is dead */
10215 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
10216 if (sd
->disk
.raid_disk
>= 0 &&
10217 sources
[sd
->disk
.raid_disk
] >= 0) {
10219 sd
->disk
.raid_disk
]);
10220 sources
[sd
->disk
.raid_disk
] =
10229 return new_degraded
;
10232 /*******************************************************************************
10233 * Function: imsm_manage_reshape
10234 * Description: Function finds array under reshape and it manages reshape
10235 * process. It creates stripes backups (if required) and sets
10238 * afd : Backup handle (nattive) - not used
10239 * sra : general array info
10240 * reshape : reshape parameters - not used
10241 * st : supertype structure
10242 * blocks : size of critical section [blocks]
10243 * fds : table of source device descriptor
10244 * offsets : start of array (offest per devices)
10246 * destfd : table of destination device descriptor
10247 * destoffsets : table of destination offsets (per device)
10249 * 1 : success, reshape is done
10251 ******************************************************************************/
10252 static int imsm_manage_reshape(
10253 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
10254 struct supertype
*st
, unsigned long backup_blocks
,
10255 int *fds
, unsigned long long *offsets
,
10256 int dests
, int *destfd
, unsigned long long *destoffsets
)
10259 struct intel_super
*super
= st
->sb
;
10260 struct intel_dev
*dv
= NULL
;
10261 struct imsm_dev
*dev
= NULL
;
10262 struct imsm_map
*map_src
;
10263 int migr_vol_qan
= 0;
10264 int ndata
, odata
; /* [bytes] */
10265 int chunk
; /* [bytes] */
10266 struct migr_record
*migr_rec
;
10268 unsigned int buf_size
; /* [bytes] */
10269 unsigned long long max_position
; /* array size [bytes] */
10270 unsigned long long next_step
; /* [blocks]/[bytes] */
10271 unsigned long long old_data_stripe_length
;
10272 unsigned long long start_src
; /* [bytes] */
10273 unsigned long long start
; /* [bytes] */
10274 unsigned long long start_buf_shift
; /* [bytes] */
10276 int source_layout
= 0;
10278 if (!fds
|| !offsets
|| !sra
)
10281 /* Find volume during the reshape */
10282 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
10283 if (dv
->dev
->vol
.migr_type
== MIGR_GEN_MIGR
10284 && dv
->dev
->vol
.migr_state
== 1) {
10289 /* Only one volume can migrate at the same time */
10290 if (migr_vol_qan
!= 1) {
10291 pr_err(": %s", migr_vol_qan
?
10292 "Number of migrating volumes greater than 1\n" :
10293 "There is no volume during migrationg\n");
10297 map_src
= get_imsm_map(dev
, MAP_1
);
10298 if (map_src
== NULL
)
10301 ndata
= imsm_num_data_members(dev
, MAP_0
);
10302 odata
= imsm_num_data_members(dev
, MAP_1
);
10304 chunk
= __le16_to_cpu(map_src
->blocks_per_strip
) * 512;
10305 old_data_stripe_length
= odata
* chunk
;
10307 migr_rec
= super
->migr_rec
;
10309 /* initialize migration record for start condition */
10310 if (sra
->reshape_progress
== 0)
10311 init_migr_record_imsm(st
, dev
, sra
);
10313 if (__le32_to_cpu(migr_rec
->rec_status
) != UNIT_SRC_NORMAL
) {
10314 dprintf("imsm: cannot restart migration when data "
10315 "are present in copy area.\n");
10318 /* Save checkpoint to update migration record for current
10319 * reshape position (in md). It can be farther than current
10320 * reshape position in metadata.
10322 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
10323 /* ignore error == 2, this can mean end of reshape here
10325 dprintf("imsm: Cannot write checkpoint to "
10326 "migration record (UNIT_SRC_NORMAL, "
10327 "initial save)\n");
10332 /* size for data */
10333 buf_size
= __le32_to_cpu(migr_rec
->blocks_per_unit
) * 512;
10334 /* extend buffer size for parity disk */
10335 buf_size
+= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
10336 /* add space for stripe aligment */
10337 buf_size
+= old_data_stripe_length
;
10338 if (posix_memalign((void **)&buf
, 4096, buf_size
)) {
10339 dprintf("imsm: Cannot allocate checpoint buffer\n");
10343 max_position
= sra
->component_size
* ndata
;
10344 source_layout
= imsm_level_to_layout(map_src
->raid_level
);
10346 while (__le32_to_cpu(migr_rec
->curr_migr_unit
) <
10347 __le32_to_cpu(migr_rec
->num_migr_units
)) {
10348 /* current reshape position [blocks] */
10349 unsigned long long current_position
=
10350 __le32_to_cpu(migr_rec
->blocks_per_unit
)
10351 * __le32_to_cpu(migr_rec
->curr_migr_unit
);
10352 unsigned long long border
;
10354 /* Check that array hasn't become failed.
10356 degraded
= check_degradation_change(sra
, fds
, degraded
);
10357 if (degraded
> 1) {
10358 dprintf("imsm: Abort reshape due to degradation"
10359 " level (%i)\n", degraded
);
10363 next_step
= __le32_to_cpu(migr_rec
->blocks_per_unit
);
10365 if ((current_position
+ next_step
) > max_position
)
10366 next_step
= max_position
- current_position
;
10368 start
= current_position
* 512;
10370 /* allign reading start to old geometry */
10371 start_buf_shift
= start
% old_data_stripe_length
;
10372 start_src
= start
- start_buf_shift
;
10374 border
= (start_src
/ odata
) - (start
/ ndata
);
10376 if (border
<= __le32_to_cpu(migr_rec
->dest_depth_per_unit
)) {
10377 /* save critical stripes to buf
10378 * start - start address of current unit
10379 * to backup [bytes]
10380 * start_src - start address of current unit
10381 * to backup alligned to source array
10384 unsigned long long next_step_filler
= 0;
10385 unsigned long long copy_length
= next_step
* 512;
10387 /* allign copy area length to stripe in old geometry */
10388 next_step_filler
= ((copy_length
+ start_buf_shift
)
10389 % old_data_stripe_length
);
10390 if (next_step_filler
)
10391 next_step_filler
= (old_data_stripe_length
10392 - next_step_filler
);
10393 dprintf("save_stripes() parameters: start = %llu,"
10394 "\tstart_src = %llu,\tnext_step*512 = %llu,"
10395 "\tstart_in_buf_shift = %llu,"
10396 "\tnext_step_filler = %llu\n",
10397 start
, start_src
, copy_length
,
10398 start_buf_shift
, next_step_filler
);
10400 if (save_stripes(fds
, offsets
, map_src
->num_members
,
10401 chunk
, map_src
->raid_level
,
10402 source_layout
, 0, NULL
, start_src
,
10404 next_step_filler
+ start_buf_shift
,
10406 dprintf("imsm: Cannot save stripes"
10410 /* Convert data to destination format and store it
10411 * in backup general migration area
10413 if (save_backup_imsm(st
, dev
, sra
,
10414 buf
+ start_buf_shift
, copy_length
)) {
10415 dprintf("imsm: Cannot save stripes to "
10416 "target devices\n");
10419 if (save_checkpoint_imsm(st
, sra
,
10420 UNIT_SRC_IN_CP_AREA
)) {
10421 dprintf("imsm: Cannot write checkpoint to "
10422 "migration record (UNIT_SRC_IN_CP_AREA)\n");
10426 /* set next step to use whole border area */
10427 border
/= next_step
;
10429 next_step
*= border
;
10431 /* When data backed up, checkpoint stored,
10432 * kick the kernel to reshape unit of data
10434 next_step
= next_step
+ sra
->reshape_progress
;
10435 /* limit next step to array max position */
10436 if (next_step
> max_position
)
10437 next_step
= max_position
;
10438 sysfs_set_num(sra
, NULL
, "suspend_lo", sra
->reshape_progress
);
10439 sysfs_set_num(sra
, NULL
, "suspend_hi", next_step
);
10440 sra
->reshape_progress
= next_step
;
10442 /* wait until reshape finish */
10443 if (wait_for_reshape_imsm(sra
, ndata
) < 0) {
10444 dprintf("wait_for_reshape_imsm returned error!\n");
10450 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
10451 /* ignore error == 2, this can mean end of reshape here
10453 dprintf("imsm: Cannot write checkpoint to "
10454 "migration record (UNIT_SRC_NORMAL)\n");
10460 /* return '1' if done */
10464 abort_reshape(sra
);
10468 #endif /* MDASSEMBLE */
10470 struct superswitch super_imsm
= {
10472 .examine_super
= examine_super_imsm
,
10473 .brief_examine_super
= brief_examine_super_imsm
,
10474 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
10475 .export_examine_super
= export_examine_super_imsm
,
10476 .detail_super
= detail_super_imsm
,
10477 .brief_detail_super
= brief_detail_super_imsm
,
10478 .write_init_super
= write_init_super_imsm
,
10479 .validate_geometry
= validate_geometry_imsm
,
10480 .add_to_super
= add_to_super_imsm
,
10481 .remove_from_super
= remove_from_super_imsm
,
10482 .detail_platform
= detail_platform_imsm
,
10483 .export_detail_platform
= export_detail_platform_imsm
,
10484 .kill_subarray
= kill_subarray_imsm
,
10485 .update_subarray
= update_subarray_imsm
,
10486 .load_container
= load_container_imsm
,
10487 .default_geometry
= default_geometry_imsm
,
10488 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
10489 .reshape_super
= imsm_reshape_super
,
10490 .manage_reshape
= imsm_manage_reshape
,
10491 .recover_backup
= recover_backup_imsm
,
10492 .copy_metadata
= copy_metadata_imsm
,
10494 .match_home
= match_home_imsm
,
10495 .uuid_from_super
= uuid_from_super_imsm
,
10496 .getinfo_super
= getinfo_super_imsm
,
10497 .getinfo_super_disks
= getinfo_super_disks_imsm
,
10498 .update_super
= update_super_imsm
,
10500 .avail_size
= avail_size_imsm
,
10501 .min_acceptable_spare_size
= min_acceptable_spare_size_imsm
,
10503 .compare_super
= compare_super_imsm
,
10505 .load_super
= load_super_imsm
,
10506 .init_super
= init_super_imsm
,
10507 .store_super
= store_super_imsm
,
10508 .free_super
= free_super_imsm
,
10509 .match_metadata_desc
= match_metadata_desc_imsm
,
10510 .container_content
= container_content_imsm
,
10517 .open_new
= imsm_open_new
,
10518 .set_array_state
= imsm_set_array_state
,
10519 .set_disk
= imsm_set_disk
,
10520 .sync_metadata
= imsm_sync_metadata
,
10521 .activate_spare
= imsm_activate_spare
,
10522 .process_update
= imsm_process_update
,
10523 .prepare_update
= imsm_prepare_update
,
10524 #endif /* MDASSEMBLE */