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
;
4427 if (test_partition(fd
))
4428 /* IMSM not allowed on partitions */
4431 free_super_imsm(st
);
4433 super
= alloc_super();
4434 /* Load hba and capabilities if they exist.
4435 * But do not preclude loading metadata in case capabilities or hba are
4436 * non-compliant and ignore_hw_compat is set.
4438 rv
= find_intel_hba_capability(fd
, super
, devname
);
4439 /* no orom/efi or non-intel hba of the disk */
4440 if ((rv
!= 0) && (st
->ignore_hw_compat
== 0)) {
4442 pr_err("No OROM/EFI properties for %s\n", devname
);
4446 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
4448 /* retry the load if we might have raced against mdmon */
4450 struct mdstat_ent
*mdstat
= mdstat_by_component(fd2devnm(fd
));
4452 if (mdstat
&& mdmon_running(mdstat
->devnm
) && getpid() != mdmon_pid(mdstat
->devnm
)) {
4453 for (retry
= 0; retry
< 3; retry
++) {
4455 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
4461 free_mdstat(mdstat
);
4466 pr_err("Failed to load all information "
4467 "sections on %s\n", devname
);
4473 if (st
->ss
== NULL
) {
4474 st
->ss
= &super_imsm
;
4475 st
->minor_version
= 0;
4476 st
->max_devs
= IMSM_MAX_DEVICES
;
4479 /* load migration record */
4480 if (load_imsm_migr_rec(super
, NULL
) == 0) {
4481 /* Check for unsupported migration features */
4482 if (check_mpb_migr_compatibility(super
) != 0) {
4483 pr_err("Unsupported migration detected");
4485 fprintf(stderr
, " on %s\n", devname
);
4487 fprintf(stderr
, " (IMSM).\n");
4495 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
4497 if (info
->level
== 1)
4499 return info
->chunk_size
>> 9;
4502 static unsigned long long info_to_blocks_per_member(mdu_array_info_t
*info
,
4503 unsigned long long size
)
4505 if (info
->level
== 1)
4508 return (size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
4511 static void imsm_update_version_info(struct intel_super
*super
)
4513 /* update the version and attributes */
4514 struct imsm_super
*mpb
= super
->anchor
;
4516 struct imsm_dev
*dev
;
4517 struct imsm_map
*map
;
4520 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4521 dev
= get_imsm_dev(super
, i
);
4522 map
= get_imsm_map(dev
, MAP_0
);
4523 if (__le32_to_cpu(dev
->size_high
) > 0)
4524 mpb
->attributes
|= MPB_ATTRIB_2TB
;
4526 /* FIXME detect when an array spans a port multiplier */
4528 mpb
->attributes
|= MPB_ATTRIB_PM
;
4531 if (mpb
->num_raid_devs
> 1 ||
4532 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
4533 version
= MPB_VERSION_ATTRIBS
;
4534 switch (get_imsm_raid_level(map
)) {
4535 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
4536 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
4537 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
4538 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
4541 if (map
->num_members
>= 5)
4542 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
4543 else if (dev
->status
== DEV_CLONE_N_GO
)
4544 version
= MPB_VERSION_CNG
;
4545 else if (get_imsm_raid_level(map
) == 5)
4546 version
= MPB_VERSION_RAID5
;
4547 else if (map
->num_members
>= 3)
4548 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
4549 else if (get_imsm_raid_level(map
) == 1)
4550 version
= MPB_VERSION_RAID1
;
4552 version
= MPB_VERSION_RAID0
;
4554 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
4558 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
4560 struct imsm_super
*mpb
= super
->anchor
;
4561 char *reason
= NULL
;
4564 if (strlen(name
) > MAX_RAID_SERIAL_LEN
)
4565 reason
= "must be 16 characters or less";
4567 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4568 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4570 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
4571 reason
= "already exists";
4576 if (reason
&& !quiet
)
4577 pr_err("imsm volume name %s\n", reason
);
4582 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
4583 unsigned long long size
, char *name
,
4584 char *homehost
, int *uuid
,
4585 long long data_offset
)
4587 /* We are creating a volume inside a pre-existing container.
4588 * so st->sb is already set.
4590 struct intel_super
*super
= st
->sb
;
4591 struct imsm_super
*mpb
= super
->anchor
;
4592 struct intel_dev
*dv
;
4593 struct imsm_dev
*dev
;
4594 struct imsm_vol
*vol
;
4595 struct imsm_map
*map
;
4596 int idx
= mpb
->num_raid_devs
;
4598 unsigned long long array_blocks
;
4599 size_t size_old
, size_new
;
4600 unsigned long long num_data_stripes
;
4602 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
4603 pr_err("This imsm-container already has the "
4604 "maximum of %d volumes\n", super
->orom
->vpa
);
4608 /* ensure the mpb is large enough for the new data */
4609 size_old
= __le32_to_cpu(mpb
->mpb_size
);
4610 size_new
= disks_to_mpb_size(info
->nr_disks
);
4611 if (size_new
> size_old
) {
4613 size_t size_round
= ROUND_UP(size_new
, 512);
4615 if (posix_memalign(&mpb_new
, 512, size_round
) != 0) {
4616 pr_err("could not allocate new mpb\n");
4619 if (posix_memalign(&super
->migr_rec_buf
, 512,
4620 MIGR_REC_BUF_SIZE
) != 0) {
4621 pr_err("%s could not allocate migr_rec buffer\n",
4628 memcpy(mpb_new
, mpb
, size_old
);
4631 super
->anchor
= mpb_new
;
4632 mpb
->mpb_size
= __cpu_to_le32(size_new
);
4633 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
4635 super
->current_vol
= idx
;
4637 /* handle 'failed_disks' by either:
4638 * a) create dummy disk entries in the table if this the first
4639 * volume in the array. We add them here as this is the only
4640 * opportunity to add them. add_to_super_imsm_volume()
4641 * handles the non-failed disks and continues incrementing
4643 * b) validate that 'failed_disks' matches the current number
4644 * of missing disks if the container is populated
4646 if (super
->current_vol
== 0) {
4648 for (i
= 0; i
< info
->failed_disks
; i
++) {
4649 struct imsm_disk
*disk
;
4652 disk
= __get_imsm_disk(mpb
, i
);
4653 disk
->status
= CONFIGURED_DISK
| FAILED_DISK
;
4654 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
4655 snprintf((char *) disk
->serial
, MAX_RAID_SERIAL_LEN
,
4658 find_missing(super
);
4663 for (d
= super
->missing
; d
; d
= d
->next
)
4665 if (info
->failed_disks
> missing
) {
4666 pr_err("unable to add 'missing' disk to container\n");
4671 if (!check_name(super
, name
, 0))
4673 dv
= xmalloc(sizeof(*dv
));
4674 dev
= xcalloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
4675 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
4676 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
4677 info
->layout
, info
->chunk_size
,
4679 /* round array size down to closest MB */
4680 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
4682 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
4683 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
4684 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
4686 vol
->migr_state
= 0;
4687 set_migr_type(dev
, MIGR_INIT
);
4688 vol
->dirty
= !info
->state
;
4689 vol
->curr_migr_unit
= 0;
4690 map
= get_imsm_map(dev
, MAP_0
);
4691 set_pba_of_lba0(map
, super
->create_offset
);
4692 set_blocks_per_member(map
, info_to_blocks_per_member(info
, size
));
4693 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
4694 map
->failed_disk_num
= ~0;
4695 if (info
->level
> 0)
4696 map
->map_state
= (info
->state
? IMSM_T_STATE_NORMAL
4697 : IMSM_T_STATE_UNINITIALIZED
);
4699 map
->map_state
= info
->failed_disks
? IMSM_T_STATE_FAILED
:
4700 IMSM_T_STATE_NORMAL
;
4703 if (info
->level
== 1 && info
->raid_disks
> 2) {
4706 pr_err("imsm does not support more than 2 disks"
4707 "in a raid1 volume\n");
4711 map
->raid_level
= info
->level
;
4712 if (info
->level
== 10) {
4713 map
->raid_level
= 1;
4714 map
->num_domains
= info
->raid_disks
/ 2;
4715 } else if (info
->level
== 1)
4716 map
->num_domains
= info
->raid_disks
;
4718 map
->num_domains
= 1;
4720 /* info->size is only int so use the 'size' parameter instead */
4721 num_data_stripes
= (size
* 2) / info_to_blocks_per_strip(info
);
4722 num_data_stripes
/= map
->num_domains
;
4723 set_num_data_stripes(map
, num_data_stripes
);
4725 map
->num_members
= info
->raid_disks
;
4726 for (i
= 0; i
< map
->num_members
; i
++) {
4727 /* initialized in add_to_super */
4728 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
4730 mpb
->num_raid_devs
++;
4733 dv
->index
= super
->current_vol
;
4734 dv
->next
= super
->devlist
;
4735 super
->devlist
= dv
;
4737 imsm_update_version_info(super
);
4742 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
4743 unsigned long long size
, char *name
,
4744 char *homehost
, int *uuid
,
4745 unsigned long long data_offset
)
4747 /* This is primarily called by Create when creating a new array.
4748 * We will then get add_to_super called for each component, and then
4749 * write_init_super called to write it out to each device.
4750 * For IMSM, Create can create on fresh devices or on a pre-existing
4752 * To create on a pre-existing array a different method will be called.
4753 * This one is just for fresh drives.
4755 struct intel_super
*super
;
4756 struct imsm_super
*mpb
;
4760 if (data_offset
!= INVALID_SECTORS
) {
4761 pr_err("data-offset not supported by imsm\n");
4766 return init_super_imsm_volume(st
, info
, size
, name
, homehost
, uuid
,
4770 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
4774 super
= alloc_super();
4775 if (super
&& posix_memalign(&super
->buf
, 512, mpb_size
) != 0) {
4780 pr_err("%s could not allocate superblock\n", __func__
);
4783 if (posix_memalign(&super
->migr_rec_buf
, 512, MIGR_REC_BUF_SIZE
) != 0) {
4784 pr_err("%s could not allocate migr_rec buffer\n", __func__
);
4789 memset(super
->buf
, 0, mpb_size
);
4791 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
4795 /* zeroing superblock */
4799 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
4801 version
= (char *) mpb
->sig
;
4802 strcpy(version
, MPB_SIGNATURE
);
4803 version
+= strlen(MPB_SIGNATURE
);
4804 strcpy(version
, MPB_VERSION_RAID0
);
4810 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
4811 int fd
, char *devname
)
4813 struct intel_super
*super
= st
->sb
;
4814 struct imsm_super
*mpb
= super
->anchor
;
4815 struct imsm_disk
*_disk
;
4816 struct imsm_dev
*dev
;
4817 struct imsm_map
*map
;
4821 dev
= get_imsm_dev(super
, super
->current_vol
);
4822 map
= get_imsm_map(dev
, MAP_0
);
4824 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
4825 pr_err("%s: Cannot add spare devices to IMSM volume\n",
4831 /* we're doing autolayout so grab the pre-marked (in
4832 * validate_geometry) raid_disk
4834 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4835 if (dl
->raiddisk
== dk
->raid_disk
)
4838 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4839 if (dl
->major
== dk
->major
&&
4840 dl
->minor
== dk
->minor
)
4845 pr_err("%s is not a member of the same container\n", devname
);
4849 /* add a pristine spare to the metadata */
4850 if (dl
->index
< 0) {
4851 dl
->index
= super
->anchor
->num_disks
;
4852 super
->anchor
->num_disks
++;
4854 /* Check the device has not already been added */
4855 slot
= get_imsm_disk_slot(map
, dl
->index
);
4857 (get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
) & IMSM_ORD_REBUILD
) == 0) {
4858 pr_err("%s has been included in this array twice\n",
4862 set_imsm_ord_tbl_ent(map
, dk
->raid_disk
, dl
->index
);
4863 dl
->disk
.status
= CONFIGURED_DISK
;
4865 /* update size of 'missing' disks to be at least as large as the
4866 * largest acitve member (we only have dummy missing disks when
4867 * creating the first volume)
4869 if (super
->current_vol
== 0) {
4870 for (df
= super
->missing
; df
; df
= df
->next
) {
4871 if (total_blocks(&dl
->disk
) > total_blocks(&df
->disk
))
4872 set_total_blocks(&df
->disk
, total_blocks(&dl
->disk
));
4873 _disk
= __get_imsm_disk(mpb
, df
->index
);
4878 /* refresh unset/failed slots to point to valid 'missing' entries */
4879 for (df
= super
->missing
; df
; df
= df
->next
)
4880 for (slot
= 0; slot
< mpb
->num_disks
; slot
++) {
4881 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
4883 if ((ord
& IMSM_ORD_REBUILD
) == 0)
4885 set_imsm_ord_tbl_ent(map
, slot
, df
->index
| IMSM_ORD_REBUILD
);
4886 if (is_gen_migration(dev
)) {
4887 struct imsm_map
*map2
= get_imsm_map(dev
,
4889 int slot2
= get_imsm_disk_slot(map2
, df
->index
);
4890 if ((slot2
< map2
->num_members
) &&
4892 __u32 ord2
= get_imsm_ord_tbl_ent(dev
,
4895 if ((unsigned)df
->index
==
4897 set_imsm_ord_tbl_ent(map2
,
4903 dprintf("set slot:%d to missing disk:%d\n", slot
, df
->index
);
4907 /* if we are creating the first raid device update the family number */
4908 if (super
->current_vol
== 0) {
4910 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
4912 _disk
= __get_imsm_disk(mpb
, dl
->index
);
4913 if (!_dev
|| !_disk
) {
4914 pr_err("BUG mpb setup error\n");
4920 sum
+= __gen_imsm_checksum(mpb
);
4921 mpb
->family_num
= __cpu_to_le32(sum
);
4922 mpb
->orig_family_num
= mpb
->family_num
;
4924 super
->current_disk
= dl
;
4929 * Function marks disk as spare and restores disk serial
4930 * in case it was previously marked as failed by takeover operation
4932 * -1 : critical error
4933 * 0 : disk is marked as spare but serial is not set
4936 int mark_spare(struct dl
*disk
)
4938 __u8 serial
[MAX_RAID_SERIAL_LEN
];
4945 if (!imsm_read_serial(disk
->fd
, NULL
, serial
)) {
4946 /* Restore disk serial number, because takeover marks disk
4947 * as failed and adds to serial ':0' before it becomes
4950 serialcpy(disk
->serial
, serial
);
4951 serialcpy(disk
->disk
.serial
, serial
);
4954 disk
->disk
.status
= SPARE_DISK
;
4960 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
4961 int fd
, char *devname
,
4962 unsigned long long data_offset
)
4964 struct intel_super
*super
= st
->sb
;
4966 unsigned long long size
;
4971 /* If we are on an RAID enabled platform check that the disk is
4972 * attached to the raid controller.
4973 * We do not need to test disks attachment for container based additions,
4974 * they shall be already tested when container was created/assembled.
4976 rv
= find_intel_hba_capability(fd
, super
, devname
);
4977 /* no orom/efi or non-intel hba of the disk */
4979 dprintf("capability: %p fd: %d ret: %d\n",
4980 super
->orom
, fd
, rv
);
4984 if (super
->current_vol
>= 0)
4985 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
4988 dd
= xcalloc(sizeof(*dd
), 1);
4989 dd
->major
= major(stb
.st_rdev
);
4990 dd
->minor
= minor(stb
.st_rdev
);
4991 dd
->devname
= devname
? xstrdup(devname
) : NULL
;
4994 dd
->action
= DISK_ADD
;
4995 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
4997 pr_err("failed to retrieve scsi serial, aborting\n");
5002 get_dev_size(fd
, NULL
, &size
);
5004 serialcpy(dd
->disk
.serial
, dd
->serial
);
5005 set_total_blocks(&dd
->disk
, size
);
5006 if (__le32_to_cpu(dd
->disk
.total_blocks_hi
) > 0) {
5007 struct imsm_super
*mpb
= super
->anchor
;
5008 mpb
->attributes
|= MPB_ATTRIB_2TB_DISK
;
5011 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
5012 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
5014 dd
->disk
.scsi_id
= __cpu_to_le32(0);
5016 if (st
->update_tail
) {
5017 dd
->next
= super
->disk_mgmt_list
;
5018 super
->disk_mgmt_list
= dd
;
5020 dd
->next
= super
->disks
;
5022 super
->updates_pending
++;
5028 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
5030 struct intel_super
*super
= st
->sb
;
5033 /* remove from super works only in mdmon - for communication
5034 * manager - monitor. Check if communication memory buffer
5037 if (!st
->update_tail
) {
5038 pr_err("%s shall be used in mdmon context only"
5039 "(line %d).\n", __func__
, __LINE__
);
5042 dd
= xcalloc(1, sizeof(*dd
));
5043 dd
->major
= dk
->major
;
5044 dd
->minor
= dk
->minor
;
5047 dd
->action
= DISK_REMOVE
;
5049 dd
->next
= super
->disk_mgmt_list
;
5050 super
->disk_mgmt_list
= dd
;
5055 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
5059 struct imsm_super anchor
;
5060 } spare_record
__attribute__ ((aligned(512)));
5062 /* spare records have their own family number and do not have any defined raid
5065 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
5067 struct imsm_super
*mpb
= super
->anchor
;
5068 struct imsm_super
*spare
= &spare_record
.anchor
;
5072 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
)),
5073 spare
->generation_num
= __cpu_to_le32(1UL),
5074 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5075 spare
->num_disks
= 1,
5076 spare
->num_raid_devs
= 0,
5077 spare
->cache_size
= mpb
->cache_size
,
5078 spare
->pwr_cycle_count
= __cpu_to_le32(1),
5080 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
5081 MPB_SIGNATURE MPB_VERSION_RAID0
);
5083 for (d
= super
->disks
; d
; d
= d
->next
) {
5087 spare
->disk
[0] = d
->disk
;
5088 if (__le32_to_cpu(d
->disk
.total_blocks_hi
) > 0)
5089 spare
->attributes
|= MPB_ATTRIB_2TB_DISK
;
5091 sum
= __gen_imsm_checksum(spare
);
5092 spare
->family_num
= __cpu_to_le32(sum
);
5093 spare
->orig_family_num
= 0;
5094 sum
= __gen_imsm_checksum(spare
);
5095 spare
->check_sum
= __cpu_to_le32(sum
);
5097 if (store_imsm_mpb(d
->fd
, spare
)) {
5098 pr_err("%s: failed for device %d:%d %s\n",
5099 __func__
, d
->major
, d
->minor
, strerror(errno
));
5111 static int write_super_imsm(struct supertype
*st
, int doclose
)
5113 struct intel_super
*super
= st
->sb
;
5114 struct imsm_super
*mpb
= super
->anchor
;
5120 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
5122 int clear_migration_record
= 1;
5124 /* 'generation' is incremented everytime the metadata is written */
5125 generation
= __le32_to_cpu(mpb
->generation_num
);
5127 mpb
->generation_num
= __cpu_to_le32(generation
);
5129 /* fix up cases where previous mdadm releases failed to set
5132 if (mpb
->orig_family_num
== 0)
5133 mpb
->orig_family_num
= mpb
->family_num
;
5135 for (d
= super
->disks
; d
; d
= d
->next
) {
5139 mpb
->disk
[d
->index
] = d
->disk
;
5143 for (d
= super
->missing
; d
; d
= d
->next
) {
5144 mpb
->disk
[d
->index
] = d
->disk
;
5147 mpb
->num_disks
= num_disks
;
5148 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
5150 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5151 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
5152 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
5154 imsm_copy_dev(dev
, dev2
);
5155 mpb_size
+= sizeof_imsm_dev(dev
, 0);
5157 if (is_gen_migration(dev2
))
5158 clear_migration_record
= 0;
5160 mpb_size
+= __le32_to_cpu(mpb
->bbm_log_size
);
5161 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5163 /* recalculate checksum */
5164 sum
= __gen_imsm_checksum(mpb
);
5165 mpb
->check_sum
= __cpu_to_le32(sum
);
5167 if (super
->clean_migration_record_by_mdmon
) {
5168 clear_migration_record
= 1;
5169 super
->clean_migration_record_by_mdmon
= 0;
5171 if (clear_migration_record
)
5172 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SIZE
);
5174 /* write the mpb for disks that compose raid devices */
5175 for (d
= super
->disks
; d
; d
= d
->next
) {
5176 if (d
->index
< 0 || is_failed(&d
->disk
))
5179 if (clear_migration_record
) {
5180 unsigned long long dsize
;
5182 get_dev_size(d
->fd
, NULL
, &dsize
);
5183 if (lseek64(d
->fd
, dsize
- 512, SEEK_SET
) >= 0) {
5184 if (write(d
->fd
, super
->migr_rec_buf
,
5185 MIGR_REC_BUF_SIZE
) != MIGR_REC_BUF_SIZE
)
5186 perror("Write migr_rec failed");
5190 if (store_imsm_mpb(d
->fd
, mpb
))
5192 "%s: failed for device %d:%d (fd: %d)%s\n",
5193 __func__
, d
->major
, d
->minor
,
5194 d
->fd
, strerror(errno
));
5203 return write_super_imsm_spares(super
, doclose
);
5208 static int create_array(struct supertype
*st
, int dev_idx
)
5211 struct imsm_update_create_array
*u
;
5212 struct intel_super
*super
= st
->sb
;
5213 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
5214 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
5215 struct disk_info
*inf
;
5216 struct imsm_disk
*disk
;
5219 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
5220 sizeof(*inf
) * map
->num_members
;
5222 u
->type
= update_create_array
;
5223 u
->dev_idx
= dev_idx
;
5224 imsm_copy_dev(&u
->dev
, dev
);
5225 inf
= get_disk_info(u
);
5226 for (i
= 0; i
< map
->num_members
; i
++) {
5227 int idx
= get_imsm_disk_idx(dev
, i
, MAP_X
);
5229 disk
= get_imsm_disk(super
, idx
);
5231 disk
= get_imsm_missing(super
, idx
);
5232 serialcpy(inf
[i
].serial
, disk
->serial
);
5234 append_metadata_update(st
, u
, len
);
5239 static int mgmt_disk(struct supertype
*st
)
5241 struct intel_super
*super
= st
->sb
;
5243 struct imsm_update_add_remove_disk
*u
;
5245 if (!super
->disk_mgmt_list
)
5250 u
->type
= update_add_remove_disk
;
5251 append_metadata_update(st
, u
, len
);
5256 static int write_init_super_imsm(struct supertype
*st
)
5258 struct intel_super
*super
= st
->sb
;
5259 int current_vol
= super
->current_vol
;
5261 /* we are done with current_vol reset it to point st at the container */
5262 super
->current_vol
= -1;
5264 if (st
->update_tail
) {
5265 /* queue the recently created array / added disk
5266 * as a metadata update */
5269 /* determine if we are creating a volume or adding a disk */
5270 if (current_vol
< 0) {
5271 /* in the mgmt (add/remove) disk case we are running
5272 * in mdmon context, so don't close fd's
5274 return mgmt_disk(st
);
5276 rv
= create_array(st
, current_vol
);
5281 for (d
= super
->disks
; d
; d
= d
->next
)
5282 Kill(d
->devname
, NULL
, 0, -1, 1);
5283 return write_super_imsm(st
, 1);
5288 static int store_super_imsm(struct supertype
*st
, int fd
)
5290 struct intel_super
*super
= st
->sb
;
5291 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
5297 return store_imsm_mpb(fd
, mpb
);
5303 static int imsm_bbm_log_size(struct imsm_super
*mpb
)
5305 return __le32_to_cpu(mpb
->bbm_log_size
);
5309 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
5310 int layout
, int raiddisks
, int chunk
,
5311 unsigned long long size
,
5312 unsigned long long data_offset
,
5314 unsigned long long *freesize
,
5318 unsigned long long ldsize
;
5319 struct intel_super
*super
=NULL
;
5322 if (level
!= LEVEL_CONTAINER
)
5327 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
5330 pr_err("imsm: Cannot open %s: %s\n",
5331 dev
, strerror(errno
));
5334 if (!get_dev_size(fd
, dev
, &ldsize
)) {
5339 /* capabilities retrieve could be possible
5340 * note that there is no fd for the disks in array.
5342 super
= alloc_super();
5343 rv
= find_intel_hba_capability(fd
, super
, verbose
> 0 ? dev
: NULL
);
5347 fd2devname(fd
, str
);
5348 dprintf("validate_geometry_imsm_container: fd: %d %s orom: %p rv: %d raiddisk: %d\n",
5349 fd
, str
, super
->orom
, rv
, raiddisks
);
5351 /* no orom/efi or non-intel hba of the disk */
5358 if (raiddisks
> super
->orom
->tds
) {
5360 pr_err("%d exceeds maximum number of"
5361 " platform supported disks: %d\n",
5362 raiddisks
, super
->orom
->tds
);
5366 if ((super
->orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) == 0 &&
5367 (ldsize
>> 9) >> 32 > 0) {
5369 pr_err("%s exceeds maximum platform supported size\n", dev
);
5375 *freesize
= avail_size_imsm(st
, ldsize
>> 9, data_offset
);
5381 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
5383 const unsigned long long base_start
= e
[*idx
].start
;
5384 unsigned long long end
= base_start
+ e
[*idx
].size
;
5387 if (base_start
== end
)
5391 for (i
= *idx
; i
< num_extents
; i
++) {
5392 /* extend overlapping extents */
5393 if (e
[i
].start
>= base_start
&&
5394 e
[i
].start
<= end
) {
5397 if (e
[i
].start
+ e
[i
].size
> end
)
5398 end
= e
[i
].start
+ e
[i
].size
;
5399 } else if (e
[i
].start
> end
) {
5405 return end
- base_start
;
5408 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
5410 /* build a composite disk with all known extents and generate a new
5411 * 'maxsize' given the "all disks in an array must share a common start
5412 * offset" constraint
5414 struct extent
*e
= xcalloc(sum_extents
, sizeof(*e
));
5418 unsigned long long pos
;
5419 unsigned long long start
= 0;
5420 unsigned long long maxsize
;
5421 unsigned long reserve
;
5423 /* coalesce and sort all extents. also, check to see if we need to
5424 * reserve space between member arrays
5427 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5430 for (i
= 0; i
< dl
->extent_cnt
; i
++)
5433 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
5438 while (i
< sum_extents
) {
5439 e
[j
].start
= e
[i
].start
;
5440 e
[j
].size
= find_size(e
, &i
, sum_extents
);
5442 if (e
[j
-1].size
== 0)
5451 unsigned long long esize
;
5453 esize
= e
[i
].start
- pos
;
5454 if (esize
>= maxsize
) {
5459 pos
= e
[i
].start
+ e
[i
].size
;
5461 } while (e
[i
-1].size
);
5467 /* FIXME assumes volume at offset 0 is the first volume in a
5470 if (start_extent
> 0)
5471 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
5475 if (maxsize
< reserve
)
5478 super
->create_offset
= ~((unsigned long long) 0);
5479 if (start
+ reserve
> super
->create_offset
)
5480 return 0; /* start overflows create_offset */
5481 super
->create_offset
= start
+ reserve
;
5483 return maxsize
- reserve
;
5486 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
5488 if (level
< 0 || level
== 6 || level
== 4)
5491 /* if we have an orom prevent invalid raid levels */
5494 case 0: return imsm_orom_has_raid0(orom
);
5497 return imsm_orom_has_raid1e(orom
);
5498 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
5499 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
5500 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
5503 return 1; /* not on an Intel RAID platform so anything goes */
5509 active_arrays_by_format(char *name
, char* hba
, struct md_list
**devlist
,
5510 int dpa
, int verbose
)
5512 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
5513 struct mdstat_ent
*memb
= NULL
;
5516 struct md_list
*dv
= NULL
;
5519 for (memb
= mdstat
; memb
; memb
= memb
->next
) {
5520 if (memb
->metadata_version
&&
5521 (strncmp(memb
->metadata_version
, "external:", 9) == 0) &&
5522 (strcmp(&memb
->metadata_version
[9], name
) == 0) &&
5523 !is_subarray(memb
->metadata_version
+9) &&
5525 struct dev_member
*dev
= memb
->members
;
5527 while(dev
&& (fd
< 0)) {
5528 char *path
= xmalloc(strlen(dev
->name
) + strlen("/dev/") + 1);
5529 num
= sprintf(path
, "%s%s", "/dev/", dev
->name
);
5531 fd
= open(path
, O_RDONLY
, 0);
5532 if ((num
<= 0) || (fd
< 0)) {
5533 pr_vrb(": Cannot open %s: %s\n",
5534 dev
->name
, strerror(errno
));
5540 if ((fd
>= 0) && disk_attached_to_hba(fd
, hba
)) {
5541 struct mdstat_ent
*vol
;
5542 for (vol
= mdstat
; vol
; vol
= vol
->next
) {
5543 if ((vol
->active
> 0) &&
5544 vol
->metadata_version
&&
5545 is_container_member(vol
, memb
->dev
)) {
5550 if (*devlist
&& (found
< dpa
)) {
5551 dv
= xcalloc(1, sizeof(*dv
));
5552 dv
->devname
= xmalloc(strlen(memb
->dev
) + strlen("/dev/") + 1);
5553 sprintf(dv
->devname
, "%s%s", "/dev/", memb
->dev
);
5556 dv
->next
= *devlist
;
5564 free_mdstat(mdstat
);
5569 static struct md_list
*
5570 get_loop_devices(void)
5573 struct md_list
*devlist
= NULL
;
5574 struct md_list
*dv
= NULL
;
5576 for(i
= 0; i
< 12; i
++) {
5577 dv
= xcalloc(1, sizeof(*dv
));
5578 dv
->devname
= xmalloc(40);
5579 sprintf(dv
->devname
, "/dev/loop%d", i
);
5587 static struct md_list
*
5588 get_devices(const char *hba_path
)
5590 struct md_list
*devlist
= NULL
;
5591 struct md_list
*dv
= NULL
;
5597 devlist
= get_loop_devices();
5600 /* scroll through /sys/dev/block looking for devices attached to
5603 dir
= opendir("/sys/dev/block");
5604 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
5609 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
5611 path
= devt_to_devpath(makedev(major
, minor
));
5614 if (!path_attached_to_hba(path
, hba_path
)) {
5621 fd
= dev_open(ent
->d_name
, O_RDONLY
);
5623 fd2devname(fd
, buf
);
5626 pr_err("cannot open device: %s\n",
5631 dv
= xcalloc(1, sizeof(*dv
));
5632 dv
->devname
= xstrdup(buf
);
5639 devlist
= devlist
->next
;
5649 count_volumes_list(struct md_list
*devlist
, char *homehost
,
5650 int verbose
, int *found
)
5652 struct md_list
*tmpdev
;
5654 struct supertype
*st
= NULL
;
5656 /* first walk the list of devices to find a consistent set
5657 * that match the criterea, if that is possible.
5658 * We flag the ones we like with 'used'.
5661 st
= match_metadata_desc_imsm("imsm");
5663 pr_vrb(": cannot allocate memory for imsm supertype\n");
5667 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5668 char *devname
= tmpdev
->devname
;
5670 struct supertype
*tst
;
5672 if (tmpdev
->used
> 1)
5674 tst
= dup_super(st
);
5676 pr_vrb(": cannot allocate memory for imsm supertype\n");
5679 tmpdev
->container
= 0;
5680 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
5682 dprintf(": cannot open device %s: %s\n",
5683 devname
, strerror(errno
));
5685 } else if (fstat(dfd
, &stb
)< 0) {
5687 dprintf(": fstat failed for %s: %s\n",
5688 devname
, strerror(errno
));
5690 } else if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
5691 dprintf(": %s is not a block device.\n",
5694 } else if (must_be_container(dfd
)) {
5695 struct supertype
*cst
;
5696 cst
= super_by_fd(dfd
, NULL
);
5698 dprintf(": cannot recognize container type %s\n",
5701 } else if (tst
->ss
!= st
->ss
) {
5702 dprintf(": non-imsm container - ignore it: %s\n",
5705 } else if (!tst
->ss
->load_container
||
5706 tst
->ss
->load_container(tst
, dfd
, NULL
))
5709 tmpdev
->container
= 1;
5712 cst
->ss
->free_super(cst
);
5714 tmpdev
->st_rdev
= stb
.st_rdev
;
5715 if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
5716 dprintf(": no RAID superblock on %s\n",
5719 } else if (tst
->ss
->compare_super
== NULL
) {
5720 dprintf(": Cannot assemble %s metadata on %s\n",
5721 tst
->ss
->name
, devname
);
5727 if (tmpdev
->used
== 2 || tmpdev
->used
== 4) {
5728 /* Ignore unrecognised devices during auto-assembly */
5733 tst
->ss
->getinfo_super(tst
, &info
, NULL
);
5735 if (st
->minor_version
== -1)
5736 st
->minor_version
= tst
->minor_version
;
5738 if (memcmp(info
.uuid
, uuid_zero
,
5739 sizeof(int[4])) == 0) {
5740 /* this is a floating spare. It cannot define
5741 * an array unless there are no more arrays of
5742 * this type to be found. It can be included
5743 * in an array of this type though.
5749 if (st
->ss
!= tst
->ss
||
5750 st
->minor_version
!= tst
->minor_version
||
5751 st
->ss
->compare_super(st
, tst
) != 0) {
5752 /* Some mismatch. If exactly one array matches this host,
5753 * we can resolve on that one.
5754 * Or, if we are auto assembling, we just ignore the second
5757 dprintf(": superblock on %s doesn't match others - assembly aborted\n",
5763 dprintf("found: devname: %s\n", devname
);
5767 tst
->ss
->free_super(tst
);
5771 if ((err
= load_super_imsm_all(st
, -1, &st
->sb
, NULL
, devlist
, 0)) == 0) {
5772 struct mdinfo
*iter
, *head
= st
->ss
->container_content(st
, NULL
);
5773 for (iter
= head
; iter
; iter
= iter
->next
) {
5774 dprintf("content->text_version: %s vol\n",
5775 iter
->text_version
);
5776 if (iter
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
5777 /* do not assemble arrays with unsupported
5779 dprintf(": Cannot activate member %s.\n",
5780 iter
->text_version
);
5787 dprintf(" no valid super block on device list: err: %d %p\n",
5791 dprintf(" no more devices to examin\n");
5794 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5795 if ((tmpdev
->used
== 1) && (tmpdev
->found
)) {
5797 if (count
< tmpdev
->found
)
5800 count
-= tmpdev
->found
;
5803 if (tmpdev
->used
== 1)
5808 st
->ss
->free_super(st
);
5813 count_volumes(char *hba
, int dpa
, int verbose
)
5815 struct md_list
*devlist
= NULL
;
5819 devlist
= get_devices(hba
);
5820 /* if no intel devices return zero volumes */
5821 if (devlist
== NULL
)
5824 count
= active_arrays_by_format("imsm", hba
, &devlist
, dpa
, verbose
);
5825 dprintf(" path: %s active arrays: %d\n", hba
, count
);
5826 if (devlist
== NULL
)
5830 count
+= count_volumes_list(devlist
,
5834 dprintf("found %d count: %d\n", found
, count
);
5837 dprintf("path: %s total number of volumes: %d\n", hba
, count
);
5840 struct md_list
*dv
= devlist
;
5841 devlist
= devlist
->next
;
5848 static int imsm_default_chunk(const struct imsm_orom
*orom
)
5850 /* up to 512 if the plaform supports it, otherwise the platform max.
5851 * 128 if no platform detected
5853 int fs
= max(7, orom
? fls(orom
->sss
) : 0);
5855 return min(512, (1 << fs
));
5859 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
5860 int raiddisks
, int *chunk
, unsigned long long size
, int verbose
)
5862 /* check/set platform and metadata limits/defaults */
5863 if (super
->orom
&& raiddisks
> super
->orom
->dpa
) {
5864 pr_vrb(": platform supports a maximum of %d disks per array\n",
5869 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
5870 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
5871 pr_vrb(": platform does not support raid%d with %d disk%s\n",
5872 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
5876 if (*chunk
== 0 || *chunk
== UnSet
)
5877 *chunk
= imsm_default_chunk(super
->orom
);
5879 if (super
->orom
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
5880 pr_vrb(": platform does not support a chunk size of: "
5885 if (layout
!= imsm_level_to_layout(level
)) {
5887 pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
5888 else if (level
== 10)
5889 pr_vrb(": imsm raid 10 only supports the n2 layout\n");
5891 pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
5896 if (super
->orom
&& (super
->orom
->attr
& IMSM_OROM_ATTR_2TB
) == 0 &&
5897 (calc_array_size(level
, raiddisks
, layout
, *chunk
, size
) >> 32) > 0) {
5898 pr_vrb(": platform does not support a volume size over 2TB\n");
5904 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
5905 * FIX ME add ahci details
5907 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
5908 int layout
, int raiddisks
, int *chunk
,
5909 unsigned long long size
,
5910 unsigned long long data_offset
,
5912 unsigned long long *freesize
,
5916 struct intel_super
*super
= st
->sb
;
5917 struct imsm_super
*mpb
;
5919 unsigned long long pos
= 0;
5920 unsigned long long maxsize
;
5924 /* We must have the container info already read in. */
5928 mpb
= super
->anchor
;
5930 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, size
, verbose
)) {
5931 pr_err("RAID gemetry validation failed. "
5932 "Cannot proceed with the action(s).\n");
5936 /* General test: make sure there is space for
5937 * 'raiddisks' device extents of size 'size' at a given
5940 unsigned long long minsize
= size
;
5941 unsigned long long start_offset
= MaxSector
;
5944 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
5945 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5950 e
= get_extents(super
, dl
);
5953 unsigned long long esize
;
5954 esize
= e
[i
].start
- pos
;
5955 if (esize
>= minsize
)
5957 if (found
&& start_offset
== MaxSector
) {
5960 } else if (found
&& pos
!= start_offset
) {
5964 pos
= e
[i
].start
+ e
[i
].size
;
5966 } while (e
[i
-1].size
);
5971 if (dcnt
< raiddisks
) {
5973 pr_err("imsm: Not enough "
5974 "devices with space for this array "
5982 /* This device must be a member of the set */
5983 if (stat(dev
, &stb
) < 0)
5985 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
5987 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5988 if (dl
->major
== (int)major(stb
.st_rdev
) &&
5989 dl
->minor
== (int)minor(stb
.st_rdev
))
5994 pr_err("%s is not in the "
5995 "same imsm set\n", dev
);
5997 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
5998 /* If a volume is present then the current creation attempt
5999 * cannot incorporate new spares because the orom may not
6000 * understand this configuration (all member disks must be
6001 * members of each array in the container).
6003 pr_err("%s is a spare and a volume"
6004 " is already defined for this container\n", dev
);
6005 pr_err("The option-rom requires all member"
6006 " disks to be a member of all volumes\n");
6008 } else if (super
->orom
&& mpb
->num_raid_devs
> 0 &&
6009 mpb
->num_disks
!= raiddisks
) {
6010 pr_err("The option-rom requires all member"
6011 " disks to be a member of all volumes\n");
6015 /* retrieve the largest free space block */
6016 e
= get_extents(super
, dl
);
6021 unsigned long long esize
;
6023 esize
= e
[i
].start
- pos
;
6024 if (esize
>= maxsize
)
6026 pos
= e
[i
].start
+ e
[i
].size
;
6028 } while (e
[i
-1].size
);
6033 pr_err("unable to determine free space for: %s\n",
6037 if (maxsize
< size
) {
6039 pr_err("%s not enough space (%llu < %llu)\n",
6040 dev
, maxsize
, size
);
6044 /* count total number of extents for merge */
6046 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6048 i
+= dl
->extent_cnt
;
6050 maxsize
= merge_extents(super
, i
);
6052 if (!check_env("IMSM_NO_PLATFORM") &&
6053 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6054 pr_err("attempting to create a second "
6055 "volume with size less then remaining space. "
6060 if (maxsize
< size
|| maxsize
== 0) {
6063 pr_err("no free space"
6064 " left on device. Aborting...\n");
6066 pr_err("not enough space"
6067 " to create volume of given size"
6068 " (%llu < %llu). Aborting...\n",
6074 *freesize
= maxsize
;
6077 int count
= count_volumes(super
->hba
->path
,
6078 super
->orom
->dpa
, verbose
);
6079 if (super
->orom
->vphba
<= count
) {
6080 pr_vrb(": platform does not support more than %d raid volumes.\n",
6081 super
->orom
->vphba
);
6088 static int imsm_get_free_size(struct supertype
*st
, int raiddisks
,
6089 unsigned long long size
, int chunk
,
6090 unsigned long long *freesize
)
6092 struct intel_super
*super
= st
->sb
;
6093 struct imsm_super
*mpb
= super
->anchor
;
6098 unsigned long long maxsize
;
6099 unsigned long long minsize
;
6103 /* find the largest common start free region of the possible disks */
6107 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6113 /* don't activate new spares if we are orom constrained
6114 * and there is already a volume active in the container
6116 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
6119 e
= get_extents(super
, dl
);
6122 for (i
= 1; e
[i
-1].size
; i
++)
6130 maxsize
= merge_extents(super
, extent_cnt
);
6134 minsize
= chunk
* 2;
6136 if (cnt
< raiddisks
||
6137 (super
->orom
&& used
&& used
!= raiddisks
) ||
6138 maxsize
< minsize
||
6140 pr_err("not enough devices with space to create array.\n");
6141 return 0; /* No enough free spaces large enough */
6152 if (!check_env("IMSM_NO_PLATFORM") &&
6153 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6154 pr_err("attempting to create a second "
6155 "volume with size less then remaining space. "
6160 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6162 dl
->raiddisk
= cnt
++;
6166 dprintf("imsm: imsm_get_free_size() returns : %llu\n", size
);
6171 static int reserve_space(struct supertype
*st
, int raiddisks
,
6172 unsigned long long size
, int chunk
,
6173 unsigned long long *freesize
)
6175 struct intel_super
*super
= st
->sb
;
6180 rv
= imsm_get_free_size(st
, raiddisks
, size
, chunk
, freesize
);
6183 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6185 dl
->raiddisk
= cnt
++;
6192 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
6193 int raiddisks
, int *chunk
, unsigned long long size
,
6194 unsigned long long data_offset
,
6195 char *dev
, unsigned long long *freesize
,
6203 * if given unused devices create a container
6204 * if given given devices in a container create a member volume
6206 if (level
== LEVEL_CONTAINER
) {
6207 /* Must be a fresh device to add to a container */
6208 return validate_geometry_imsm_container(st
, level
, layout
,
6218 struct intel_super
*super
= st
->sb
;
6219 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
6220 raiddisks
, chunk
, size
,
6223 /* we are being asked to automatically layout a
6224 * new volume based on the current contents of
6225 * the container. If the the parameters can be
6226 * satisfied reserve_space will record the disks,
6227 * start offset, and size of the volume to be
6228 * created. add_to_super and getinfo_super
6229 * detect when autolayout is in progress.
6231 /* assuming that freesize is always given when array is
6233 if (super
->orom
&& freesize
) {
6235 count
= count_volumes(super
->hba
->path
,
6236 super
->orom
->dpa
, verbose
);
6237 if (super
->orom
->vphba
<= count
) {
6238 pr_vrb(": platform does not support more"
6239 " than %d raid volumes.\n",
6240 super
->orom
->vphba
);
6245 return reserve_space(st
, raiddisks
, size
,
6251 /* creating in a given container */
6252 return validate_geometry_imsm_volume(st
, level
, layout
,
6253 raiddisks
, chunk
, size
,
6255 dev
, freesize
, verbose
);
6258 /* This device needs to be a device in an 'imsm' container */
6259 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
6262 pr_err("Cannot create this array on device %s\n",
6267 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
6269 pr_err("Cannot open %s: %s\n",
6270 dev
, strerror(errno
));
6273 /* Well, it is in use by someone, maybe an 'imsm' container. */
6274 cfd
= open_container(fd
);
6278 pr_err("Cannot use %s: It is busy\n",
6282 sra
= sysfs_read(cfd
, NULL
, GET_VERSION
);
6283 if (sra
&& sra
->array
.major_version
== -1 &&
6284 strcmp(sra
->text_version
, "imsm") == 0)
6288 /* This is a member of a imsm container. Load the container
6289 * and try to create a volume
6291 struct intel_super
*super
;
6293 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
, NULL
, 1) == 0) {
6295 strcpy(st
->container_devnm
, fd2devnm(cfd
));
6297 return validate_geometry_imsm_volume(st
, level
, layout
,
6299 size
, data_offset
, dev
,
6306 pr_err("failed container membership check\n");
6312 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
6314 struct intel_super
*super
= st
->sb
;
6316 if (level
&& *level
== UnSet
)
6317 *level
= LEVEL_CONTAINER
;
6319 if (level
&& layout
&& *layout
== UnSet
)
6320 *layout
= imsm_level_to_layout(*level
);
6322 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0))
6323 *chunk
= imsm_default_chunk(super
->orom
);
6326 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
6328 static int kill_subarray_imsm(struct supertype
*st
)
6330 /* remove the subarray currently referenced by ->current_vol */
6332 struct intel_dev
**dp
;
6333 struct intel_super
*super
= st
->sb
;
6334 __u8 current_vol
= super
->current_vol
;
6335 struct imsm_super
*mpb
= super
->anchor
;
6337 if (super
->current_vol
< 0)
6339 super
->current_vol
= -1; /* invalidate subarray cursor */
6341 /* block deletions that would change the uuid of active subarrays
6343 * FIXME when immutable ids are available, but note that we'll
6344 * also need to fixup the invalidated/active subarray indexes in
6347 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6350 if (i
< current_vol
)
6352 sprintf(subarray
, "%u", i
);
6353 if (is_subarray_active(subarray
, st
->devnm
)) {
6354 pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
6361 if (st
->update_tail
) {
6362 struct imsm_update_kill_array
*u
= xmalloc(sizeof(*u
));
6364 u
->type
= update_kill_array
;
6365 u
->dev_idx
= current_vol
;
6366 append_metadata_update(st
, u
, sizeof(*u
));
6371 for (dp
= &super
->devlist
; *dp
;)
6372 if ((*dp
)->index
== current_vol
) {
6375 handle_missing(super
, (*dp
)->dev
);
6376 if ((*dp
)->index
> current_vol
)
6381 /* no more raid devices, all active components are now spares,
6382 * but of course failed are still failed
6384 if (--mpb
->num_raid_devs
== 0) {
6387 for (d
= super
->disks
; d
; d
= d
->next
)
6392 super
->updates_pending
++;
6397 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
6398 char *update
, struct mddev_ident
*ident
)
6400 /* update the subarray currently referenced by ->current_vol */
6401 struct intel_super
*super
= st
->sb
;
6402 struct imsm_super
*mpb
= super
->anchor
;
6404 if (strcmp(update
, "name") == 0) {
6405 char *name
= ident
->name
;
6409 if (is_subarray_active(subarray
, st
->devnm
)) {
6410 pr_err("Unable to update name of active subarray\n");
6414 if (!check_name(super
, name
, 0))
6417 vol
= strtoul(subarray
, &ep
, 10);
6418 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
6421 if (st
->update_tail
) {
6422 struct imsm_update_rename_array
*u
= xmalloc(sizeof(*u
));
6424 u
->type
= update_rename_array
;
6426 snprintf((char *) u
->name
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6427 append_metadata_update(st
, u
, sizeof(*u
));
6429 struct imsm_dev
*dev
;
6432 dev
= get_imsm_dev(super
, vol
);
6433 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6434 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6435 dev
= get_imsm_dev(super
, i
);
6436 handle_missing(super
, dev
);
6438 super
->updates_pending
++;
6445 #endif /* MDASSEMBLE */
6447 static int is_gen_migration(struct imsm_dev
*dev
)
6452 if (!dev
->vol
.migr_state
)
6455 if (migr_type(dev
) == MIGR_GEN_MIGR
)
6461 static int is_rebuilding(struct imsm_dev
*dev
)
6463 struct imsm_map
*migr_map
;
6465 if (!dev
->vol
.migr_state
)
6468 if (migr_type(dev
) != MIGR_REBUILD
)
6471 migr_map
= get_imsm_map(dev
, MAP_1
);
6473 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
6480 static int is_initializing(struct imsm_dev
*dev
)
6482 struct imsm_map
*migr_map
;
6484 if (!dev
->vol
.migr_state
)
6487 if (migr_type(dev
) != MIGR_INIT
)
6490 migr_map
= get_imsm_map(dev
, MAP_1
);
6492 if (migr_map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
6499 static void update_recovery_start(struct intel_super
*super
,
6500 struct imsm_dev
*dev
,
6501 struct mdinfo
*array
)
6503 struct mdinfo
*rebuild
= NULL
;
6507 if (!is_rebuilding(dev
))
6510 /* Find the rebuild target, but punt on the dual rebuild case */
6511 for (d
= array
->devs
; d
; d
= d
->next
)
6512 if (d
->recovery_start
== 0) {
6519 /* (?) none of the disks are marked with
6520 * IMSM_ORD_REBUILD, so assume they are missing and the
6521 * disk_ord_tbl was not correctly updated
6523 dprintf("%s: failed to locate out-of-sync disk\n", __func__
);
6527 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
6528 rebuild
->recovery_start
= units
* blocks_per_migr_unit(super
, dev
);
6532 static int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
);
6535 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
6537 /* Given a container loaded by load_super_imsm_all,
6538 * extract information about all the arrays into
6540 * If 'subarray' is given, just extract info about that array.
6542 * For each imsm_dev create an mdinfo, fill it in,
6543 * then look for matching devices in super->disks
6544 * and create appropriate device mdinfo.
6546 struct intel_super
*super
= st
->sb
;
6547 struct imsm_super
*mpb
= super
->anchor
;
6548 struct mdinfo
*rest
= NULL
;
6552 int spare_disks
= 0;
6554 /* do not assemble arrays when not all attributes are supported */
6555 if (imsm_check_attributes(mpb
->attributes
) == 0) {
6557 pr_err("Unsupported attributes in IMSM metadata."
6558 "Arrays activation is blocked.\n");
6561 /* check for bad blocks */
6562 if (imsm_bbm_log_size(super
->anchor
)) {
6563 pr_err("BBM log found in IMSM metadata."
6564 "Arrays activation is blocked.\n");
6568 /* count spare devices, not used in maps
6570 for (d
= super
->disks
; d
; d
= d
->next
)
6574 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6575 struct imsm_dev
*dev
;
6576 struct imsm_map
*map
;
6577 struct imsm_map
*map2
;
6578 struct mdinfo
*this;
6586 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
6589 dev
= get_imsm_dev(super
, i
);
6590 map
= get_imsm_map(dev
, MAP_0
);
6591 map2
= get_imsm_map(dev
, MAP_1
);
6593 /* do not publish arrays that are in the middle of an
6594 * unsupported migration
6596 if (dev
->vol
.migr_state
&&
6597 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
6598 pr_err("cannot assemble volume '%.16s':"
6599 " unsupported migration in progress\n",
6603 /* do not publish arrays that are not support by controller's
6607 this = xmalloc(sizeof(*this));
6609 super
->current_vol
= i
;
6610 getinfo_super_imsm_volume(st
, this, NULL
);
6613 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
6614 /* mdadm does not support all metadata features- set the bit in all arrays state */
6615 if (!validate_geometry_imsm_orom(super
,
6616 get_imsm_raid_level(map
), /* RAID level */
6617 imsm_level_to_layout(get_imsm_raid_level(map
)),
6618 map
->num_members
, /* raid disks */
6619 &chunk
, join_u32(dev
->size_low
, dev
->size_high
),
6621 pr_err("IMSM RAID geometry validation"
6622 " failed. Array %s activation is blocked.\n",
6624 this->array
.state
|=
6625 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
6626 (1<<MD_SB_BLOCK_VOLUME
);
6630 /* if array has bad blocks, set suitable bit in all arrays state */
6632 this->array
.state
|=
6633 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
6634 (1<<MD_SB_BLOCK_VOLUME
);
6636 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
6637 unsigned long long recovery_start
;
6638 struct mdinfo
*info_d
;
6645 idx
= get_imsm_disk_idx(dev
, slot
, MAP_0
);
6646 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
6647 for (d
= super
->disks
; d
; d
= d
->next
)
6648 if (d
->index
== idx
)
6651 recovery_start
= MaxSector
;
6654 if (d
&& is_failed(&d
->disk
))
6656 if (ord
& IMSM_ORD_REBUILD
)
6660 * if we skip some disks the array will be assmebled degraded;
6661 * reset resync start to avoid a dirty-degraded
6662 * situation when performing the intial sync
6664 * FIXME handle dirty degraded
6666 if ((skip
|| recovery_start
== 0) && !dev
->vol
.dirty
)
6667 this->resync_start
= MaxSector
;
6671 info_d
= xcalloc(1, sizeof(*info_d
));
6672 info_d
->next
= this->devs
;
6673 this->devs
= info_d
;
6675 info_d
->disk
.number
= d
->index
;
6676 info_d
->disk
.major
= d
->major
;
6677 info_d
->disk
.minor
= d
->minor
;
6678 info_d
->disk
.raid_disk
= slot
;
6679 info_d
->recovery_start
= recovery_start
;
6681 if (slot
< map2
->num_members
)
6682 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
6684 this->array
.spare_disks
++;
6686 if (slot
< map
->num_members
)
6687 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
6689 this->array
.spare_disks
++;
6691 if (info_d
->recovery_start
== MaxSector
)
6692 this->array
.working_disks
++;
6694 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
6695 info_d
->data_offset
= pba_of_lba0(map
);
6696 info_d
->component_size
= blocks_per_member(map
);
6698 /* now that the disk list is up-to-date fixup recovery_start */
6699 update_recovery_start(super
, dev
, this);
6700 this->array
.spare_disks
+= spare_disks
;
6703 /* check for reshape */
6704 if (this->reshape_active
== 1)
6705 recover_backup_imsm(st
, this);
6713 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
6714 int failed
, int look_in_map
)
6716 struct imsm_map
*map
;
6718 map
= get_imsm_map(dev
, look_in_map
);
6721 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
6722 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
6724 switch (get_imsm_raid_level(map
)) {
6726 return IMSM_T_STATE_FAILED
;
6729 if (failed
< map
->num_members
)
6730 return IMSM_T_STATE_DEGRADED
;
6732 return IMSM_T_STATE_FAILED
;
6737 * check to see if any mirrors have failed, otherwise we
6738 * are degraded. Even numbered slots are mirrored on
6742 /* gcc -Os complains that this is unused */
6743 int insync
= insync
;
6745 for (i
= 0; i
< map
->num_members
; i
++) {
6746 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_X
);
6747 int idx
= ord_to_idx(ord
);
6748 struct imsm_disk
*disk
;
6750 /* reset the potential in-sync count on even-numbered
6751 * slots. num_copies is always 2 for imsm raid10
6756 disk
= get_imsm_disk(super
, idx
);
6757 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
6760 /* no in-sync disks left in this mirror the
6764 return IMSM_T_STATE_FAILED
;
6767 return IMSM_T_STATE_DEGRADED
;
6771 return IMSM_T_STATE_DEGRADED
;
6773 return IMSM_T_STATE_FAILED
;
6779 return map
->map_state
;
6782 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
6787 struct imsm_disk
*disk
;
6788 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6789 struct imsm_map
*prev
= get_imsm_map(dev
, MAP_1
);
6790 struct imsm_map
*map_for_loop
;
6795 /* at the beginning of migration we set IMSM_ORD_REBUILD on
6796 * disks that are being rebuilt. New failures are recorded to
6797 * map[0]. So we look through all the disks we started with and
6798 * see if any failures are still present, or if any new ones
6802 if (prev
&& (map
->num_members
< prev
->num_members
))
6803 map_for_loop
= prev
;
6805 for (i
= 0; i
< map_for_loop
->num_members
; i
++) {
6807 /* when MAP_X is passed both maps failures are counted
6810 ((look_in_map
== MAP_1
) || (look_in_map
== MAP_X
)) &&
6811 (i
< prev
->num_members
)) {
6812 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
6813 idx_1
= ord_to_idx(ord
);
6815 disk
= get_imsm_disk(super
, idx_1
);
6816 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
6819 if (((look_in_map
== MAP_0
) || (look_in_map
== MAP_X
)) &&
6820 (i
< map
->num_members
)) {
6821 ord
= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
6822 idx
= ord_to_idx(ord
);
6825 disk
= get_imsm_disk(super
, idx
);
6826 if (!disk
|| is_failed(disk
) ||
6827 ord
& IMSM_ORD_REBUILD
)
6837 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
6840 struct intel_super
*super
= c
->sb
;
6841 struct imsm_super
*mpb
= super
->anchor
;
6843 if (atoi(inst
) >= mpb
->num_raid_devs
) {
6844 pr_err("%s: subarry index %d, out of range\n",
6845 __func__
, atoi(inst
));
6849 dprintf("imsm: open_new %s\n", inst
);
6850 a
->info
.container_member
= atoi(inst
);
6854 static int is_resyncing(struct imsm_dev
*dev
)
6856 struct imsm_map
*migr_map
;
6858 if (!dev
->vol
.migr_state
)
6861 if (migr_type(dev
) == MIGR_INIT
||
6862 migr_type(dev
) == MIGR_REPAIR
)
6865 if (migr_type(dev
) == MIGR_GEN_MIGR
)
6868 migr_map
= get_imsm_map(dev
, MAP_1
);
6870 if ((migr_map
->map_state
== IMSM_T_STATE_NORMAL
) &&
6871 (dev
->vol
.migr_type
!= MIGR_GEN_MIGR
))
6877 /* return true if we recorded new information */
6878 static int mark_failure(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
6882 struct imsm_map
*map
;
6883 char buf
[MAX_RAID_SERIAL_LEN
+3];
6884 unsigned int len
, shift
= 0;
6886 /* new failures are always set in map[0] */
6887 map
= get_imsm_map(dev
, MAP_0
);
6889 slot
= get_imsm_disk_slot(map
, idx
);
6893 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
6894 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
6897 memcpy(buf
, disk
->serial
, MAX_RAID_SERIAL_LEN
);
6898 buf
[MAX_RAID_SERIAL_LEN
] = '\000';
6900 if ((len
= strlen(buf
)) >= MAX_RAID_SERIAL_LEN
)
6901 shift
= len
- MAX_RAID_SERIAL_LEN
+ 1;
6902 strncpy((char *)disk
->serial
, &buf
[shift
], MAX_RAID_SERIAL_LEN
);
6904 disk
->status
|= FAILED_DISK
;
6905 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
6906 /* mark failures in second map if second map exists and this disk
6908 * This is valid for migration, initialization and rebuild
6910 if (dev
->vol
.migr_state
) {
6911 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
6912 int slot2
= get_imsm_disk_slot(map2
, idx
);
6914 if ((slot2
< map2
->num_members
) &&
6916 set_imsm_ord_tbl_ent(map2
, slot2
,
6917 idx
| IMSM_ORD_REBUILD
);
6919 if (map
->failed_disk_num
== 0xff)
6920 map
->failed_disk_num
= slot
;
6924 static void mark_missing(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
6926 mark_failure(dev
, disk
, idx
);
6928 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
6931 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
6932 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
6935 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
6939 if (!super
->missing
)
6942 /* When orom adds replacement for missing disk it does
6943 * not remove entry of missing disk, but just updates map with
6944 * new added disk. So it is not enough just to test if there is
6945 * any missing disk, we have to look if there are any failed disks
6946 * in map to stop migration */
6948 dprintf("imsm: mark missing\n");
6949 /* end process for initialization and rebuild only
6951 if (is_gen_migration(dev
) == 0) {
6955 failed
= imsm_count_failed(super
, dev
, MAP_0
);
6956 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
6959 end_migration(dev
, super
, map_state
);
6961 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
6962 mark_missing(dev
, &dl
->disk
, dl
->index
);
6963 super
->updates_pending
++;
6966 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
,
6969 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
6970 unsigned long long array_blocks
;
6971 struct imsm_map
*map
;
6973 if (used_disks
== 0) {
6974 /* when problems occures
6975 * return current array_blocks value
6977 array_blocks
= __le32_to_cpu(dev
->size_high
);
6978 array_blocks
= array_blocks
<< 32;
6979 array_blocks
+= __le32_to_cpu(dev
->size_low
);
6981 return array_blocks
;
6984 /* set array size in metadata
6986 if (new_size
<= 0) {
6987 /* OLCE size change is caused by added disks
6989 map
= get_imsm_map(dev
, MAP_0
);
6990 array_blocks
= blocks_per_member(map
) * used_disks
;
6992 /* Online Volume Size Change
6993 * Using available free space
6995 array_blocks
= new_size
;
6998 /* round array size down to closest MB
7000 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
7001 dev
->size_low
= __cpu_to_le32((__u32
)array_blocks
);
7002 dev
->size_high
= __cpu_to_le32((__u32
)(array_blocks
>> 32));
7004 return array_blocks
;
7007 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
7009 static void imsm_progress_container_reshape(struct intel_super
*super
)
7011 /* if no device has a migr_state, but some device has a
7012 * different number of members than the previous device, start
7013 * changing the number of devices in this device to match
7016 struct imsm_super
*mpb
= super
->anchor
;
7017 int prev_disks
= -1;
7021 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7022 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
7023 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7024 struct imsm_map
*map2
;
7025 int prev_num_members
;
7027 if (dev
->vol
.migr_state
)
7030 if (prev_disks
== -1)
7031 prev_disks
= map
->num_members
;
7032 if (prev_disks
== map
->num_members
)
7035 /* OK, this array needs to enter reshape mode.
7036 * i.e it needs a migr_state
7039 copy_map_size
= sizeof_imsm_map(map
);
7040 prev_num_members
= map
->num_members
;
7041 map
->num_members
= prev_disks
;
7042 dev
->vol
.migr_state
= 1;
7043 dev
->vol
.curr_migr_unit
= 0;
7044 set_migr_type(dev
, MIGR_GEN_MIGR
);
7045 for (i
= prev_num_members
;
7046 i
< map
->num_members
; i
++)
7047 set_imsm_ord_tbl_ent(map
, i
, i
);
7048 map2
= get_imsm_map(dev
, MAP_1
);
7049 /* Copy the current map */
7050 memcpy(map2
, map
, copy_map_size
);
7051 map2
->num_members
= prev_num_members
;
7053 imsm_set_array_size(dev
, -1);
7054 super
->clean_migration_record_by_mdmon
= 1;
7055 super
->updates_pending
++;
7059 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
7060 * states are handled in imsm_set_disk() with one exception, when a
7061 * resync is stopped due to a new failure this routine will set the
7062 * 'degraded' state for the array.
7064 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
7066 int inst
= a
->info
.container_member
;
7067 struct intel_super
*super
= a
->container
->sb
;
7068 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7069 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7070 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
7071 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7072 __u32 blocks_per_unit
;
7074 if (dev
->vol
.migr_state
&&
7075 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
7076 /* array state change is blocked due to reshape action
7078 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
7079 * - finish the reshape (if last_checkpoint is big and action != reshape)
7080 * - update curr_migr_unit
7082 if (a
->curr_action
== reshape
) {
7083 /* still reshaping, maybe update curr_migr_unit */
7084 goto mark_checkpoint
;
7086 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
7087 /* for some reason we aborted the reshape.
7089 * disable automatic metadata rollback
7090 * user action is required to recover process
7093 struct imsm_map
*map2
=
7094 get_imsm_map(dev
, MAP_1
);
7095 dev
->vol
.migr_state
= 0;
7096 set_migr_type(dev
, 0);
7097 dev
->vol
.curr_migr_unit
= 0;
7099 sizeof_imsm_map(map2
));
7100 super
->updates_pending
++;
7103 if (a
->last_checkpoint
>= a
->info
.component_size
) {
7104 unsigned long long array_blocks
;
7108 used_disks
= imsm_num_data_members(dev
, MAP_0
);
7109 if (used_disks
> 0) {
7111 blocks_per_member(map
) *
7113 /* round array size down to closest MB
7115 array_blocks
= (array_blocks
7116 >> SECT_PER_MB_SHIFT
)
7117 << SECT_PER_MB_SHIFT
;
7118 a
->info
.custom_array_size
= array_blocks
;
7119 /* encourage manager to update array
7123 a
->check_reshape
= 1;
7125 /* finalize online capacity expansion/reshape */
7126 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
7128 mdi
->disk
.raid_disk
,
7131 imsm_progress_container_reshape(super
);
7136 /* before we activate this array handle any missing disks */
7137 if (consistent
== 2)
7138 handle_missing(super
, dev
);
7140 if (consistent
== 2 &&
7141 (!is_resync_complete(&a
->info
) ||
7142 map_state
!= IMSM_T_STATE_NORMAL
||
7143 dev
->vol
.migr_state
))
7146 if (is_resync_complete(&a
->info
)) {
7147 /* complete intialization / resync,
7148 * recovery and interrupted recovery is completed in
7151 if (is_resyncing(dev
)) {
7152 dprintf("imsm: mark resync done\n");
7153 end_migration(dev
, super
, map_state
);
7154 super
->updates_pending
++;
7155 a
->last_checkpoint
= 0;
7157 } else if ((!is_resyncing(dev
) && !failed
) &&
7158 (imsm_reshape_blocks_arrays_changes(super
) == 0)) {
7159 /* mark the start of the init process if nothing is failed */
7160 dprintf("imsm: mark resync start\n");
7161 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
7162 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
7164 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
7165 super
->updates_pending
++;
7169 /* skip checkpointing for general migration,
7170 * it is controlled in mdadm
7172 if (is_gen_migration(dev
))
7173 goto skip_mark_checkpoint
;
7175 /* check if we can update curr_migr_unit from resync_start, recovery_start */
7176 blocks_per_unit
= blocks_per_migr_unit(super
, dev
);
7177 if (blocks_per_unit
) {
7181 units
= a
->last_checkpoint
/ blocks_per_unit
;
7184 /* check that we did not overflow 32-bits, and that
7185 * curr_migr_unit needs updating
7187 if (units32
== units
&&
7189 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
7190 dprintf("imsm: mark checkpoint (%u)\n", units32
);
7191 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
7192 super
->updates_pending
++;
7196 skip_mark_checkpoint
:
7197 /* mark dirty / clean */
7198 if (dev
->vol
.dirty
!= !consistent
) {
7199 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
7204 super
->updates_pending
++;
7210 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
7212 int inst
= a
->info
.container_member
;
7213 struct intel_super
*super
= a
->container
->sb
;
7214 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7215 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7216 struct imsm_disk
*disk
;
7218 int recovery_not_finished
= 0;
7223 if (n
> map
->num_members
)
7224 pr_err("imsm: set_disk %d out of range 0..%d\n",
7225 n
, map
->num_members
- 1);
7230 dprintf("imsm: set_disk %d:%x\n", n
, state
);
7232 ord
= get_imsm_ord_tbl_ent(dev
, n
, MAP_0
);
7233 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
7235 /* check for new failures */
7236 if (state
& DS_FAULTY
) {
7237 if (mark_failure(dev
, disk
, ord_to_idx(ord
)))
7238 super
->updates_pending
++;
7241 /* check if in_sync */
7242 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
7243 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
7245 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
7246 super
->updates_pending
++;
7249 failed
= imsm_count_failed(super
, dev
, MAP_0
);
7250 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7252 /* check if recovery complete, newly degraded, or failed */
7253 dprintf("imsm: Detected transition to state ");
7254 switch (map_state
) {
7255 case IMSM_T_STATE_NORMAL
: /* transition to normal state */
7256 dprintf("normal: ");
7257 if (is_rebuilding(dev
)) {
7258 dprintf("while rebuilding");
7259 /* check if recovery is really finished */
7260 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
7261 if (mdi
->recovery_start
!= MaxSector
) {
7262 recovery_not_finished
= 1;
7265 if (recovery_not_finished
) {
7266 dprintf("\nimsm: Rebuild has not finished yet, "
7267 "state not changed");
7268 if (a
->last_checkpoint
< mdi
->recovery_start
) {
7269 a
->last_checkpoint
= mdi
->recovery_start
;
7270 super
->updates_pending
++;
7274 end_migration(dev
, super
, map_state
);
7275 map
= get_imsm_map(dev
, MAP_0
);
7276 map
->failed_disk_num
= ~0;
7277 super
->updates_pending
++;
7278 a
->last_checkpoint
= 0;
7281 if (is_gen_migration(dev
)) {
7282 dprintf("while general migration");
7283 if (a
->last_checkpoint
>= a
->info
.component_size
)
7284 end_migration(dev
, super
, map_state
);
7286 map
->map_state
= map_state
;
7287 map
= get_imsm_map(dev
, MAP_0
);
7288 map
->failed_disk_num
= ~0;
7289 super
->updates_pending
++;
7293 case IMSM_T_STATE_DEGRADED
: /* transition to degraded state */
7294 dprintf("degraded: ");
7295 if ((map
->map_state
!= map_state
) &&
7296 !dev
->vol
.migr_state
) {
7297 dprintf("mark degraded");
7298 map
->map_state
= map_state
;
7299 super
->updates_pending
++;
7300 a
->last_checkpoint
= 0;
7303 if (is_rebuilding(dev
)) {
7304 dprintf("while rebuilding.");
7305 if (map
->map_state
!= map_state
) {
7306 dprintf(" Map state change");
7307 end_migration(dev
, super
, map_state
);
7308 super
->updates_pending
++;
7312 if (is_gen_migration(dev
)) {
7313 dprintf("while general migration");
7314 if (a
->last_checkpoint
>= a
->info
.component_size
)
7315 end_migration(dev
, super
, map_state
);
7317 map
->map_state
= map_state
;
7318 manage_second_map(super
, dev
);
7320 super
->updates_pending
++;
7323 if (is_initializing(dev
)) {
7324 dprintf("while initialization.");
7325 map
->map_state
= map_state
;
7326 super
->updates_pending
++;
7330 case IMSM_T_STATE_FAILED
: /* transition to failed state */
7331 dprintf("failed: ");
7332 if (is_gen_migration(dev
)) {
7333 dprintf("while general migration");
7334 map
->map_state
= map_state
;
7335 super
->updates_pending
++;
7338 if (map
->map_state
!= map_state
) {
7339 dprintf("mark failed");
7340 end_migration(dev
, super
, map_state
);
7341 super
->updates_pending
++;
7342 a
->last_checkpoint
= 0;
7347 dprintf("state %i\n", map_state
);
7353 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
7356 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
7357 unsigned long long dsize
;
7358 unsigned long long sectors
;
7360 get_dev_size(fd
, NULL
, &dsize
);
7362 if (mpb_size
> 512) {
7363 /* -1 to account for anchor */
7364 sectors
= mpb_sectors(mpb
) - 1;
7366 /* write the extended mpb to the sectors preceeding the anchor */
7367 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0)
7370 if ((unsigned long long)write(fd
, buf
+ 512, 512 * sectors
)
7375 /* first block is stored on second to last sector of the disk */
7376 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0)
7379 if (write(fd
, buf
, 512) != 512)
7385 static void imsm_sync_metadata(struct supertype
*container
)
7387 struct intel_super
*super
= container
->sb
;
7389 dprintf("sync metadata: %d\n", super
->updates_pending
);
7390 if (!super
->updates_pending
)
7393 write_super_imsm(container
, 0);
7395 super
->updates_pending
= 0;
7398 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
7400 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7401 int i
= get_imsm_disk_idx(dev
, idx
, MAP_X
);
7404 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7408 if (dl
&& is_failed(&dl
->disk
))
7412 dprintf("%s: found %x:%x\n", __func__
, dl
->major
, dl
->minor
);
7417 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
7418 struct active_array
*a
, int activate_new
,
7419 struct mdinfo
*additional_test_list
)
7421 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7422 int idx
= get_imsm_disk_idx(dev
, slot
, MAP_X
);
7423 struct imsm_super
*mpb
= super
->anchor
;
7424 struct imsm_map
*map
;
7425 unsigned long long pos
;
7430 __u32 array_start
= 0;
7431 __u32 array_end
= 0;
7433 struct mdinfo
*test_list
;
7435 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7436 /* If in this array, skip */
7437 for (d
= a
->info
.devs
; d
; d
= d
->next
)
7438 if (d
->state_fd
>= 0 &&
7439 d
->disk
.major
== dl
->major
&&
7440 d
->disk
.minor
== dl
->minor
) {
7441 dprintf("%x:%x already in array\n",
7442 dl
->major
, dl
->minor
);
7447 test_list
= additional_test_list
;
7449 if (test_list
->disk
.major
== dl
->major
&&
7450 test_list
->disk
.minor
== dl
->minor
) {
7451 dprintf("%x:%x already in additional test list\n",
7452 dl
->major
, dl
->minor
);
7455 test_list
= test_list
->next
;
7460 /* skip in use or failed drives */
7461 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
7463 dprintf("%x:%x status (failed: %d index: %d)\n",
7464 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
7468 /* skip pure spares when we are looking for partially
7469 * assimilated drives
7471 if (dl
->index
== -1 && !activate_new
)
7474 /* Does this unused device have the requisite free space?
7475 * It needs to be able to cover all member volumes
7477 ex
= get_extents(super
, dl
);
7479 dprintf("cannot get extents\n");
7482 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7483 dev
= get_imsm_dev(super
, i
);
7484 map
= get_imsm_map(dev
, MAP_0
);
7486 /* check if this disk is already a member of
7489 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
7495 array_start
= pba_of_lba0(map
);
7496 array_end
= array_start
+
7497 blocks_per_member(map
) - 1;
7500 /* check that we can start at pba_of_lba0 with
7501 * blocks_per_member of space
7503 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
7507 pos
= ex
[j
].start
+ ex
[j
].size
;
7509 } while (ex
[j
-1].size
);
7516 if (i
< mpb
->num_raid_devs
) {
7517 dprintf("%x:%x does not have %u to %u available\n",
7518 dl
->major
, dl
->minor
, array_start
, array_end
);
7528 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
7530 struct imsm_dev
*dev2
;
7531 struct imsm_map
*map
;
7537 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
7539 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
, MAP_0
);
7540 if (state
== IMSM_T_STATE_FAILED
) {
7541 map
= get_imsm_map(dev2
, MAP_0
);
7544 for (slot
= 0; slot
< map
->num_members
; slot
++) {
7546 * Check if failed disks are deleted from intel
7547 * disk list or are marked to be deleted
7549 idx
= get_imsm_disk_idx(dev2
, slot
, MAP_X
);
7550 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
7552 * Do not rebuild the array if failed disks
7553 * from failed sub-array are not removed from
7557 is_failed(&idisk
->disk
) &&
7558 (idisk
->action
!= DISK_REMOVE
))
7566 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
7567 struct metadata_update
**updates
)
7570 * Find a device with unused free space and use it to replace a
7571 * failed/vacant region in an array. We replace failed regions one a
7572 * array at a time. The result is that a new spare disk will be added
7573 * to the first failed array and after the monitor has finished
7574 * propagating failures the remainder will be consumed.
7576 * FIXME add a capability for mdmon to request spares from another
7580 struct intel_super
*super
= a
->container
->sb
;
7581 int inst
= a
->info
.container_member
;
7582 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7583 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7584 int failed
= a
->info
.array
.raid_disks
;
7585 struct mdinfo
*rv
= NULL
;
7588 struct metadata_update
*mu
;
7590 struct imsm_update_activate_spare
*u
;
7595 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
7596 if ((d
->curr_state
& DS_FAULTY
) &&
7598 /* wait for Removal to happen */
7600 if (d
->state_fd
>= 0)
7604 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
7605 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
7607 if (imsm_reshape_blocks_arrays_changes(super
))
7610 /* Cannot activate another spare if rebuild is in progress already
7612 if (is_rebuilding(dev
)) {
7613 dprintf("imsm: No spare activation allowed. "
7614 "Rebuild in progress already.\n");
7618 if (a
->info
.array
.level
== 4)
7619 /* No repair for takeovered array
7620 * imsm doesn't support raid4
7624 if (imsm_check_degraded(super
, dev
, failed
, MAP_0
) !=
7625 IMSM_T_STATE_DEGRADED
)
7629 * If there are any failed disks check state of the other volume.
7630 * Block rebuild if the another one is failed until failed disks
7631 * are removed from container.
7634 dprintf("found failed disks in %.*s, check if there another"
7635 "failed sub-array.\n",
7636 MAX_RAID_SERIAL_LEN
, dev
->volume
);
7637 /* check if states of the other volumes allow for rebuild */
7638 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
7640 allowed
= imsm_rebuild_allowed(a
->container
,
7648 /* For each slot, if it is not working, find a spare */
7649 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
7650 for (d
= a
->info
.devs
; d
; d
= d
->next
)
7651 if (d
->disk
.raid_disk
== i
)
7653 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
7654 if (d
&& (d
->state_fd
>= 0))
7658 * OK, this device needs recovery. Try to re-add the
7659 * previous occupant of this slot, if this fails see if
7660 * we can continue the assimilation of a spare that was
7661 * partially assimilated, finally try to activate a new
7664 dl
= imsm_readd(super
, i
, a
);
7666 dl
= imsm_add_spare(super
, i
, a
, 0, rv
);
7668 dl
= imsm_add_spare(super
, i
, a
, 1, rv
);
7672 /* found a usable disk with enough space */
7673 di
= xcalloc(1, sizeof(*di
));
7675 /* dl->index will be -1 in the case we are activating a
7676 * pristine spare. imsm_process_update() will create a
7677 * new index in this case. Once a disk is found to be
7678 * failed in all member arrays it is kicked from the
7681 di
->disk
.number
= dl
->index
;
7683 /* (ab)use di->devs to store a pointer to the device
7686 di
->devs
= (struct mdinfo
*) dl
;
7688 di
->disk
.raid_disk
= i
;
7689 di
->disk
.major
= dl
->major
;
7690 di
->disk
.minor
= dl
->minor
;
7692 di
->recovery_start
= 0;
7693 di
->data_offset
= pba_of_lba0(map
);
7694 di
->component_size
= a
->info
.component_size
;
7695 di
->container_member
= inst
;
7696 super
->random
= random32();
7700 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
7701 i
, di
->data_offset
);
7705 /* No spares found */
7707 /* Now 'rv' has a list of devices to return.
7708 * Create a metadata_update record to update the
7709 * disk_ord_tbl for the array
7711 mu
= xmalloc(sizeof(*mu
));
7712 mu
->buf
= xcalloc(num_spares
,
7713 sizeof(struct imsm_update_activate_spare
));
7715 mu
->space_list
= NULL
;
7716 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
7717 mu
->next
= *updates
;
7718 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
7720 for (di
= rv
; di
; di
= di
->next
) {
7721 u
->type
= update_activate_spare
;
7722 u
->dl
= (struct dl
*) di
->devs
;
7724 u
->slot
= di
->disk
.raid_disk
;
7735 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
7737 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
7738 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7739 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, MAP_0
);
7740 struct disk_info
*inf
= get_disk_info(u
);
7741 struct imsm_disk
*disk
;
7745 for (i
= 0; i
< map
->num_members
; i
++) {
7746 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, MAP_X
));
7747 for (j
= 0; j
< new_map
->num_members
; j
++)
7748 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
7755 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
7757 struct dl
*dl
= NULL
;
7758 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7759 if ((dl
->major
== major
) && (dl
->minor
== minor
))
7764 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
7766 struct dl
*prev
= NULL
;
7770 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7771 if ((dl
->major
== major
) && (dl
->minor
== minor
)) {
7774 prev
->next
= dl
->next
;
7776 super
->disks
= dl
->next
;
7778 __free_imsm_disk(dl
);
7779 dprintf("%s: removed %x:%x\n",
7780 __func__
, major
, minor
);
7788 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
7790 static int add_remove_disk_update(struct intel_super
*super
)
7792 int check_degraded
= 0;
7793 struct dl
*disk
= NULL
;
7794 /* add/remove some spares to/from the metadata/contrainer */
7795 while (super
->disk_mgmt_list
) {
7796 struct dl
*disk_cfg
;
7798 disk_cfg
= super
->disk_mgmt_list
;
7799 super
->disk_mgmt_list
= disk_cfg
->next
;
7800 disk_cfg
->next
= NULL
;
7802 if (disk_cfg
->action
== DISK_ADD
) {
7803 disk_cfg
->next
= super
->disks
;
7804 super
->disks
= disk_cfg
;
7806 dprintf("%s: added %x:%x\n",
7807 __func__
, disk_cfg
->major
,
7809 } else if (disk_cfg
->action
== DISK_REMOVE
) {
7810 dprintf("Disk remove action processed: %x.%x\n",
7811 disk_cfg
->major
, disk_cfg
->minor
);
7812 disk
= get_disk_super(super
,
7816 /* store action status */
7817 disk
->action
= DISK_REMOVE
;
7818 /* remove spare disks only */
7819 if (disk
->index
== -1) {
7820 remove_disk_super(super
,
7825 /* release allocate disk structure */
7826 __free_imsm_disk(disk_cfg
);
7829 return check_degraded
;
7832 static int apply_reshape_migration_update(struct imsm_update_reshape_migration
*u
,
7833 struct intel_super
*super
,
7836 struct intel_dev
*id
;
7837 void **tofree
= NULL
;
7840 dprintf("apply_reshape_migration_update()\n");
7841 if ((u
->subdev
< 0) ||
7843 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
7846 if ((space_list
== NULL
) || (*space_list
== NULL
)) {
7847 dprintf("imsm: Error: Memory is not allocated\n");
7851 for (id
= super
->devlist
; id
; id
= id
->next
) {
7852 if (id
->index
== (unsigned)u
->subdev
) {
7853 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
7854 struct imsm_map
*map
;
7855 struct imsm_dev
*new_dev
=
7856 (struct imsm_dev
*)*space_list
;
7857 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
7859 struct dl
*new_disk
;
7861 if (new_dev
== NULL
)
7863 *space_list
= **space_list
;
7864 memcpy(new_dev
, dev
, sizeof_imsm_dev(dev
, 0));
7865 map
= get_imsm_map(new_dev
, MAP_0
);
7867 dprintf("imsm: Error: migration in progress");
7871 to_state
= map
->map_state
;
7872 if ((u
->new_level
== 5) && (map
->raid_level
== 0)) {
7874 /* this should not happen */
7875 if (u
->new_disks
[0] < 0) {
7876 map
->failed_disk_num
=
7877 map
->num_members
- 1;
7878 to_state
= IMSM_T_STATE_DEGRADED
;
7880 to_state
= IMSM_T_STATE_NORMAL
;
7882 migrate(new_dev
, super
, to_state
, MIGR_GEN_MIGR
);
7883 if (u
->new_level
> -1)
7884 map
->raid_level
= u
->new_level
;
7885 migr_map
= get_imsm_map(new_dev
, MAP_1
);
7886 if ((u
->new_level
== 5) &&
7887 (migr_map
->raid_level
== 0)) {
7888 int ord
= map
->num_members
- 1;
7889 migr_map
->num_members
--;
7890 if (u
->new_disks
[0] < 0)
7891 ord
|= IMSM_ORD_REBUILD
;
7892 set_imsm_ord_tbl_ent(map
,
7893 map
->num_members
- 1,
7897 tofree
= (void **)dev
;
7899 /* update chunk size
7901 if (u
->new_chunksize
> 0)
7902 map
->blocks_per_strip
=
7903 __cpu_to_le16(u
->new_chunksize
* 2);
7907 if ((u
->new_level
!= 5) ||
7908 (migr_map
->raid_level
!= 0) ||
7909 (migr_map
->raid_level
== map
->raid_level
))
7912 if (u
->new_disks
[0] >= 0) {
7915 new_disk
= get_disk_super(super
,
7916 major(u
->new_disks
[0]),
7917 minor(u
->new_disks
[0]));
7918 dprintf("imsm: new disk for reshape is: %i:%i "
7919 "(%p, index = %i)\n",
7920 major(u
->new_disks
[0]),
7921 minor(u
->new_disks
[0]),
7922 new_disk
, new_disk
->index
);
7923 if (new_disk
== NULL
)
7924 goto error_disk_add
;
7926 new_disk
->index
= map
->num_members
- 1;
7927 /* slot to fill in autolayout
7929 new_disk
->raiddisk
= new_disk
->index
;
7930 new_disk
->disk
.status
|= CONFIGURED_DISK
;
7931 new_disk
->disk
.status
&= ~SPARE_DISK
;
7933 goto error_disk_add
;
7936 *tofree
= *space_list
;
7937 /* calculate new size
7939 imsm_set_array_size(new_dev
, -1);
7946 *space_list
= tofree
;
7950 dprintf("Error: imsm: Cannot find disk.\n");
7954 static int apply_size_change_update(struct imsm_update_size_change
*u
,
7955 struct intel_super
*super
)
7957 struct intel_dev
*id
;
7960 dprintf("apply_size_change_update()\n");
7961 if ((u
->subdev
< 0) ||
7963 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
7967 for (id
= super
->devlist
; id
; id
= id
->next
) {
7968 if (id
->index
== (unsigned)u
->subdev
) {
7969 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
7970 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7971 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
7972 unsigned long long blocks_per_member
;
7974 /* calculate new size
7976 blocks_per_member
= u
->new_size
/ used_disks
;
7977 dprintf("imsm: apply_size_change_update(size: %llu, "
7978 "blocks per member: %llu)\n",
7979 u
->new_size
, blocks_per_member
);
7980 set_blocks_per_member(map
, blocks_per_member
);
7981 imsm_set_array_size(dev
, u
->new_size
);
7991 static int apply_update_activate_spare(struct imsm_update_activate_spare
*u
,
7992 struct intel_super
*super
,
7993 struct active_array
*active_array
)
7995 struct imsm_super
*mpb
= super
->anchor
;
7996 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
7997 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7998 struct imsm_map
*migr_map
;
7999 struct active_array
*a
;
8000 struct imsm_disk
*disk
;
8007 int second_map_created
= 0;
8009 for (; u
; u
= u
->next
) {
8010 victim
= get_imsm_disk_idx(dev
, u
->slot
, MAP_X
);
8015 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
8020 pr_err("error: imsm_activate_spare passed "
8021 "an unknown disk (index: %d)\n",
8026 /* count failures (excluding rebuilds and the victim)
8027 * to determine map[0] state
8030 for (i
= 0; i
< map
->num_members
; i
++) {
8033 disk
= get_imsm_disk(super
,
8034 get_imsm_disk_idx(dev
, i
, MAP_X
));
8035 if (!disk
|| is_failed(disk
))
8039 /* adding a pristine spare, assign a new index */
8040 if (dl
->index
< 0) {
8041 dl
->index
= super
->anchor
->num_disks
;
8042 super
->anchor
->num_disks
++;
8045 disk
->status
|= CONFIGURED_DISK
;
8046 disk
->status
&= ~SPARE_DISK
;
8049 to_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8050 if (!second_map_created
) {
8051 second_map_created
= 1;
8052 map
->map_state
= IMSM_T_STATE_DEGRADED
;
8053 migrate(dev
, super
, to_state
, MIGR_REBUILD
);
8055 map
->map_state
= to_state
;
8056 migr_map
= get_imsm_map(dev
, MAP_1
);
8057 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
8058 set_imsm_ord_tbl_ent(migr_map
, u
->slot
,
8059 dl
->index
| IMSM_ORD_REBUILD
);
8061 /* update the family_num to mark a new container
8062 * generation, being careful to record the existing
8063 * family_num in orig_family_num to clean up after
8064 * earlier mdadm versions that neglected to set it.
8066 if (mpb
->orig_family_num
== 0)
8067 mpb
->orig_family_num
= mpb
->family_num
;
8068 mpb
->family_num
+= super
->random
;
8070 /* count arrays using the victim in the metadata */
8072 for (a
= active_array
; a
; a
= a
->next
) {
8073 dev
= get_imsm_dev(super
, a
->info
.container_member
);
8074 map
= get_imsm_map(dev
, MAP_0
);
8076 if (get_imsm_disk_slot(map
, victim
) >= 0)
8080 /* delete the victim if it is no longer being
8086 /* We know that 'manager' isn't touching anything,
8087 * so it is safe to delete
8089 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
8090 if ((*dlp
)->index
== victim
)
8093 /* victim may be on the missing list */
8095 for (dlp
= &super
->missing
; *dlp
;
8096 dlp
= &(*dlp
)->next
)
8097 if ((*dlp
)->index
== victim
)
8099 imsm_delete(super
, dlp
, victim
);
8106 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
8107 struct intel_super
*super
,
8110 struct dl
*new_disk
;
8111 struct intel_dev
*id
;
8113 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
8114 int disk_count
= u
->old_raid_disks
;
8115 void **tofree
= NULL
;
8116 int devices_to_reshape
= 1;
8117 struct imsm_super
*mpb
= super
->anchor
;
8119 unsigned int dev_id
;
8121 dprintf("imsm: apply_reshape_container_disks_update()\n");
8123 /* enable spares to use in array */
8124 for (i
= 0; i
< delta_disks
; i
++) {
8125 new_disk
= get_disk_super(super
,
8126 major(u
->new_disks
[i
]),
8127 minor(u
->new_disks
[i
]));
8128 dprintf("imsm: new disk for reshape is: %i:%i "
8129 "(%p, index = %i)\n",
8130 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
8131 new_disk
, new_disk
->index
);
8132 if ((new_disk
== NULL
) ||
8133 ((new_disk
->index
>= 0) &&
8134 (new_disk
->index
< u
->old_raid_disks
)))
8135 goto update_reshape_exit
;
8136 new_disk
->index
= disk_count
++;
8137 /* slot to fill in autolayout
8139 new_disk
->raiddisk
= new_disk
->index
;
8140 new_disk
->disk
.status
|=
8142 new_disk
->disk
.status
&= ~SPARE_DISK
;
8145 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
8146 mpb
->num_raid_devs
);
8147 /* manage changes in volume
8149 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
8150 void **sp
= *space_list
;
8151 struct imsm_dev
*newdev
;
8152 struct imsm_map
*newmap
, *oldmap
;
8154 for (id
= super
->devlist
; id
; id
= id
->next
) {
8155 if (id
->index
== dev_id
)
8164 /* Copy the dev, but not (all of) the map */
8165 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
8166 oldmap
= get_imsm_map(id
->dev
, MAP_0
);
8167 newmap
= get_imsm_map(newdev
, MAP_0
);
8168 /* Copy the current map */
8169 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8170 /* update one device only
8172 if (devices_to_reshape
) {
8173 dprintf("imsm: modifying subdev: %i\n",
8175 devices_to_reshape
--;
8176 newdev
->vol
.migr_state
= 1;
8177 newdev
->vol
.curr_migr_unit
= 0;
8178 set_migr_type(newdev
, MIGR_GEN_MIGR
);
8179 newmap
->num_members
= u
->new_raid_disks
;
8180 for (i
= 0; i
< delta_disks
; i
++) {
8181 set_imsm_ord_tbl_ent(newmap
,
8182 u
->old_raid_disks
+ i
,
8183 u
->old_raid_disks
+ i
);
8185 /* New map is correct, now need to save old map
8187 newmap
= get_imsm_map(newdev
, MAP_1
);
8188 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8190 imsm_set_array_size(newdev
, -1);
8193 sp
= (void **)id
->dev
;
8198 /* Clear migration record */
8199 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
8202 *space_list
= tofree
;
8205 update_reshape_exit
:
8210 static int apply_takeover_update(struct imsm_update_takeover
*u
,
8211 struct intel_super
*super
,
8214 struct imsm_dev
*dev
= NULL
;
8215 struct intel_dev
*dv
;
8216 struct imsm_dev
*dev_new
;
8217 struct imsm_map
*map
;
8221 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
8222 if (dv
->index
== (unsigned int)u
->subarray
) {
8230 map
= get_imsm_map(dev
, MAP_0
);
8232 if (u
->direction
== R10_TO_R0
) {
8233 /* Number of failed disks must be half of initial disk number */
8234 if (imsm_count_failed(super
, dev
, MAP_0
) !=
8235 (map
->num_members
/ 2))
8238 /* iterate through devices to mark removed disks as spare */
8239 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8240 if (dm
->disk
.status
& FAILED_DISK
) {
8241 int idx
= dm
->index
;
8242 /* update indexes on the disk list */
8243 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
8244 the index values will end up being correct.... NB */
8245 for (du
= super
->disks
; du
; du
= du
->next
)
8246 if (du
->index
> idx
)
8248 /* mark as spare disk */
8253 map
->num_members
= map
->num_members
/ 2;
8254 map
->map_state
= IMSM_T_STATE_NORMAL
;
8255 map
->num_domains
= 1;
8256 map
->raid_level
= 0;
8257 map
->failed_disk_num
= -1;
8260 if (u
->direction
== R0_TO_R10
) {
8262 /* update slots in current disk list */
8263 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8267 /* create new *missing* disks */
8268 for (i
= 0; i
< map
->num_members
; i
++) {
8269 space
= *space_list
;
8272 *space_list
= *space
;
8274 memcpy(du
, super
->disks
, sizeof(*du
));
8278 du
->index
= (i
* 2) + 1;
8279 sprintf((char *)du
->disk
.serial
,
8280 " MISSING_%d", du
->index
);
8281 sprintf((char *)du
->serial
,
8282 "MISSING_%d", du
->index
);
8283 du
->next
= super
->missing
;
8284 super
->missing
= du
;
8286 /* create new dev and map */
8287 space
= *space_list
;
8290 *space_list
= *space
;
8291 dev_new
= (void *)space
;
8292 memcpy(dev_new
, dev
, sizeof(*dev
));
8293 /* update new map */
8294 map
= get_imsm_map(dev_new
, MAP_0
);
8295 map
->num_members
= map
->num_members
* 2;
8296 map
->map_state
= IMSM_T_STATE_DEGRADED
;
8297 map
->num_domains
= 2;
8298 map
->raid_level
= 1;
8299 /* replace dev<->dev_new */
8302 /* update disk order table */
8303 for (du
= super
->disks
; du
; du
= du
->next
)
8305 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8306 for (du
= super
->missing
; du
; du
= du
->next
)
8307 if (du
->index
>= 0) {
8308 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8309 mark_missing(dv
->dev
, &du
->disk
, du
->index
);
8315 static void imsm_process_update(struct supertype
*st
,
8316 struct metadata_update
*update
)
8319 * crack open the metadata_update envelope to find the update record
8320 * update can be one of:
8321 * update_reshape_container_disks - all the arrays in the container
8322 * are being reshaped to have more devices. We need to mark
8323 * the arrays for general migration and convert selected spares
8324 * into active devices.
8325 * update_activate_spare - a spare device has replaced a failed
8326 * device in an array, update the disk_ord_tbl. If this disk is
8327 * present in all member arrays then also clear the SPARE_DISK
8329 * update_create_array
8331 * update_rename_array
8332 * update_add_remove_disk
8334 struct intel_super
*super
= st
->sb
;
8335 struct imsm_super
*mpb
;
8336 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
8338 /* update requires a larger buf but the allocation failed */
8339 if (super
->next_len
&& !super
->next_buf
) {
8340 super
->next_len
= 0;
8344 if (super
->next_buf
) {
8345 memcpy(super
->next_buf
, super
->buf
, super
->len
);
8347 super
->len
= super
->next_len
;
8348 super
->buf
= super
->next_buf
;
8350 super
->next_len
= 0;
8351 super
->next_buf
= NULL
;
8354 mpb
= super
->anchor
;
8357 case update_general_migration_checkpoint
: {
8358 struct intel_dev
*id
;
8359 struct imsm_update_general_migration_checkpoint
*u
=
8360 (void *)update
->buf
;
8362 dprintf("imsm: process_update() "
8363 "for update_general_migration_checkpoint called\n");
8365 /* find device under general migration */
8366 for (id
= super
->devlist
; id
; id
= id
->next
) {
8367 if (is_gen_migration(id
->dev
)) {
8368 id
->dev
->vol
.curr_migr_unit
=
8369 __cpu_to_le32(u
->curr_migr_unit
);
8370 super
->updates_pending
++;
8375 case update_takeover
: {
8376 struct imsm_update_takeover
*u
= (void *)update
->buf
;
8377 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
8378 imsm_update_version_info(super
);
8379 super
->updates_pending
++;
8384 case update_reshape_container_disks
: {
8385 struct imsm_update_reshape
*u
= (void *)update
->buf
;
8386 if (apply_reshape_container_disks_update(
8387 u
, super
, &update
->space_list
))
8388 super
->updates_pending
++;
8391 case update_reshape_migration
: {
8392 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
8393 if (apply_reshape_migration_update(
8394 u
, super
, &update
->space_list
))
8395 super
->updates_pending
++;
8398 case update_size_change
: {
8399 struct imsm_update_size_change
*u
= (void *)update
->buf
;
8400 if (apply_size_change_update(u
, super
))
8401 super
->updates_pending
++;
8404 case update_activate_spare
: {
8405 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
8406 if (apply_update_activate_spare(u
, super
, st
->arrays
))
8407 super
->updates_pending
++;
8410 case update_create_array
: {
8411 /* someone wants to create a new array, we need to be aware of
8412 * a few races/collisions:
8413 * 1/ 'Create' called by two separate instances of mdadm
8414 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
8415 * devices that have since been assimilated via
8417 * In the event this update can not be carried out mdadm will
8418 * (FIX ME) notice that its update did not take hold.
8420 struct imsm_update_create_array
*u
= (void *) update
->buf
;
8421 struct intel_dev
*dv
;
8422 struct imsm_dev
*dev
;
8423 struct imsm_map
*map
, *new_map
;
8424 unsigned long long start
, end
;
8425 unsigned long long new_start
, new_end
;
8427 struct disk_info
*inf
;
8430 /* handle racing creates: first come first serve */
8431 if (u
->dev_idx
< mpb
->num_raid_devs
) {
8432 dprintf("%s: subarray %d already defined\n",
8433 __func__
, u
->dev_idx
);
8437 /* check update is next in sequence */
8438 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
8439 dprintf("%s: can not create array %d expected index %d\n",
8440 __func__
, u
->dev_idx
, mpb
->num_raid_devs
);
8444 new_map
= get_imsm_map(&u
->dev
, MAP_0
);
8445 new_start
= pba_of_lba0(new_map
);
8446 new_end
= new_start
+ blocks_per_member(new_map
);
8447 inf
= get_disk_info(u
);
8449 /* handle activate_spare versus create race:
8450 * check to make sure that overlapping arrays do not include
8453 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8454 dev
= get_imsm_dev(super
, i
);
8455 map
= get_imsm_map(dev
, MAP_0
);
8456 start
= pba_of_lba0(map
);
8457 end
= start
+ blocks_per_member(map
);
8458 if ((new_start
>= start
&& new_start
<= end
) ||
8459 (start
>= new_start
&& start
<= new_end
))
8464 if (disks_overlap(super
, i
, u
)) {
8465 dprintf("%s: arrays overlap\n", __func__
);
8470 /* check that prepare update was successful */
8471 if (!update
->space
) {
8472 dprintf("%s: prepare update failed\n", __func__
);
8476 /* check that all disks are still active before committing
8477 * changes. FIXME: could we instead handle this by creating a
8478 * degraded array? That's probably not what the user expects,
8479 * so better to drop this update on the floor.
8481 for (i
= 0; i
< new_map
->num_members
; i
++) {
8482 dl
= serial_to_dl(inf
[i
].serial
, super
);
8484 dprintf("%s: disk disappeared\n", __func__
);
8489 super
->updates_pending
++;
8491 /* convert spares to members and fixup ord_tbl */
8492 for (i
= 0; i
< new_map
->num_members
; i
++) {
8493 dl
= serial_to_dl(inf
[i
].serial
, super
);
8494 if (dl
->index
== -1) {
8495 dl
->index
= mpb
->num_disks
;
8497 dl
->disk
.status
|= CONFIGURED_DISK
;
8498 dl
->disk
.status
&= ~SPARE_DISK
;
8500 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
8505 update
->space
= NULL
;
8506 imsm_copy_dev(dev
, &u
->dev
);
8507 dv
->index
= u
->dev_idx
;
8508 dv
->next
= super
->devlist
;
8509 super
->devlist
= dv
;
8510 mpb
->num_raid_devs
++;
8512 imsm_update_version_info(super
);
8515 /* mdmon knows how to release update->space, but not
8516 * ((struct intel_dev *) update->space)->dev
8518 if (update
->space
) {
8524 case update_kill_array
: {
8525 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
8526 int victim
= u
->dev_idx
;
8527 struct active_array
*a
;
8528 struct intel_dev
**dp
;
8529 struct imsm_dev
*dev
;
8531 /* sanity check that we are not affecting the uuid of
8532 * active arrays, or deleting an active array
8534 * FIXME when immutable ids are available, but note that
8535 * we'll also need to fixup the invalidated/active
8536 * subarray indexes in mdstat
8538 for (a
= st
->arrays
; a
; a
= a
->next
)
8539 if (a
->info
.container_member
>= victim
)
8541 /* by definition if mdmon is running at least one array
8542 * is active in the container, so checking
8543 * mpb->num_raid_devs is just extra paranoia
8545 dev
= get_imsm_dev(super
, victim
);
8546 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
8547 dprintf("failed to delete subarray-%d\n", victim
);
8551 for (dp
= &super
->devlist
; *dp
;)
8552 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
8555 if ((*dp
)->index
> (unsigned)victim
)
8559 mpb
->num_raid_devs
--;
8560 super
->updates_pending
++;
8563 case update_rename_array
: {
8564 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
8565 char name
[MAX_RAID_SERIAL_LEN
+1];
8566 int target
= u
->dev_idx
;
8567 struct active_array
*a
;
8568 struct imsm_dev
*dev
;
8570 /* sanity check that we are not affecting the uuid of
8573 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
8574 name
[MAX_RAID_SERIAL_LEN
] = '\0';
8575 for (a
= st
->arrays
; a
; a
= a
->next
)
8576 if (a
->info
.container_member
== target
)
8578 dev
= get_imsm_dev(super
, u
->dev_idx
);
8579 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
8580 dprintf("failed to rename subarray-%d\n", target
);
8584 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
8585 super
->updates_pending
++;
8588 case update_add_remove_disk
: {
8589 /* we may be able to repair some arrays if disks are
8590 * being added, check teh status of add_remove_disk
8591 * if discs has been added.
8593 if (add_remove_disk_update(super
)) {
8594 struct active_array
*a
;
8596 super
->updates_pending
++;
8597 for (a
= st
->arrays
; a
; a
= a
->next
)
8598 a
->check_degraded
= 1;
8603 pr_err("error: unsuported process update type:"
8604 "(type: %d)\n", type
);
8608 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
);
8610 static void imsm_prepare_update(struct supertype
*st
,
8611 struct metadata_update
*update
)
8614 * Allocate space to hold new disk entries, raid-device entries or a new
8615 * mpb if necessary. The manager synchronously waits for updates to
8616 * complete in the monitor, so new mpb buffers allocated here can be
8617 * integrated by the monitor thread without worrying about live pointers
8618 * in the manager thread.
8620 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
8621 struct intel_super
*super
= st
->sb
;
8622 struct imsm_super
*mpb
= super
->anchor
;
8627 case update_general_migration_checkpoint
:
8628 dprintf("imsm: prepare_update() "
8629 "for update_general_migration_checkpoint called\n");
8631 case update_takeover
: {
8632 struct imsm_update_takeover
*u
= (void *)update
->buf
;
8633 if (u
->direction
== R0_TO_R10
) {
8634 void **tail
= (void **)&update
->space_list
;
8635 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
8636 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8637 int num_members
= map
->num_members
;
8640 /* allocate memory for added disks */
8641 for (i
= 0; i
< num_members
; i
++) {
8642 size
= sizeof(struct dl
);
8643 space
= xmalloc(size
);
8648 /* allocate memory for new device */
8649 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
8650 (num_members
* sizeof(__u32
));
8651 space
= xmalloc(size
);
8655 len
= disks_to_mpb_size(num_members
* 2);
8660 case update_reshape_container_disks
: {
8661 /* Every raid device in the container is about to
8662 * gain some more devices, and we will enter a
8664 * So each 'imsm_map' will be bigger, and the imsm_vol
8665 * will now hold 2 of them.
8666 * Thus we need new 'struct imsm_dev' allocations sized
8667 * as sizeof_imsm_dev but with more devices in both maps.
8669 struct imsm_update_reshape
*u
= (void *)update
->buf
;
8670 struct intel_dev
*dl
;
8671 void **space_tail
= (void**)&update
->space_list
;
8673 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
8675 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
8676 int size
= sizeof_imsm_dev(dl
->dev
, 1);
8678 if (u
->new_raid_disks
> u
->old_raid_disks
)
8679 size
+= sizeof(__u32
)*2*
8680 (u
->new_raid_disks
- u
->old_raid_disks
);
8687 len
= disks_to_mpb_size(u
->new_raid_disks
);
8688 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
8691 case update_reshape_migration
: {
8692 /* for migration level 0->5 we need to add disks
8693 * so the same as for container operation we will copy
8694 * device to the bigger location.
8695 * in memory prepared device and new disk area are prepared
8696 * for usage in process update
8698 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
8699 struct intel_dev
*id
;
8700 void **space_tail
= (void **)&update
->space_list
;
8703 int current_level
= -1;
8705 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
8707 /* add space for bigger array in update
8709 for (id
= super
->devlist
; id
; id
= id
->next
) {
8710 if (id
->index
== (unsigned)u
->subdev
) {
8711 size
= sizeof_imsm_dev(id
->dev
, 1);
8712 if (u
->new_raid_disks
> u
->old_raid_disks
)
8713 size
+= sizeof(__u32
)*2*
8714 (u
->new_raid_disks
- u
->old_raid_disks
);
8722 if (update
->space_list
== NULL
)
8725 /* add space for disk in update
8727 size
= sizeof(struct dl
);
8733 /* add spare device to update
8735 for (id
= super
->devlist
; id
; id
= id
->next
)
8736 if (id
->index
== (unsigned)u
->subdev
) {
8737 struct imsm_dev
*dev
;
8738 struct imsm_map
*map
;
8740 dev
= get_imsm_dev(super
, u
->subdev
);
8741 map
= get_imsm_map(dev
, MAP_0
);
8742 current_level
= map
->raid_level
;
8745 if ((u
->new_level
== 5) && (u
->new_level
!= current_level
)) {
8746 struct mdinfo
*spares
;
8748 spares
= get_spares_for_grow(st
);
8756 makedev(dev
->disk
.major
,
8758 dl
= get_disk_super(super
,
8761 dl
->index
= u
->old_raid_disks
;
8767 len
= disks_to_mpb_size(u
->new_raid_disks
);
8768 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
8771 case update_size_change
: {
8774 case update_create_array
: {
8775 struct imsm_update_create_array
*u
= (void *) update
->buf
;
8776 struct intel_dev
*dv
;
8777 struct imsm_dev
*dev
= &u
->dev
;
8778 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8780 struct disk_info
*inf
;
8784 inf
= get_disk_info(u
);
8785 len
= sizeof_imsm_dev(dev
, 1);
8786 /* allocate a new super->devlist entry */
8787 dv
= xmalloc(sizeof(*dv
));
8788 dv
->dev
= xmalloc(len
);
8791 /* count how many spares will be converted to members */
8792 for (i
= 0; i
< map
->num_members
; i
++) {
8793 dl
= serial_to_dl(inf
[i
].serial
, super
);
8795 /* hmm maybe it failed?, nothing we can do about
8800 if (count_memberships(dl
, super
) == 0)
8803 len
+= activate
* sizeof(struct imsm_disk
);
8810 /* check if we need a larger metadata buffer */
8811 if (super
->next_buf
)
8812 buf_len
= super
->next_len
;
8814 buf_len
= super
->len
;
8816 if (__le32_to_cpu(mpb
->mpb_size
) + len
> buf_len
) {
8817 /* ok we need a larger buf than what is currently allocated
8818 * if this allocation fails process_update will notice that
8819 * ->next_len is set and ->next_buf is NULL
8821 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + len
, 512);
8822 if (super
->next_buf
)
8823 free(super
->next_buf
);
8825 super
->next_len
= buf_len
;
8826 if (posix_memalign(&super
->next_buf
, 512, buf_len
) == 0)
8827 memset(super
->next_buf
, 0, buf_len
);
8829 super
->next_buf
= NULL
;
8833 /* must be called while manager is quiesced */
8834 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
8836 struct imsm_super
*mpb
= super
->anchor
;
8838 struct imsm_dev
*dev
;
8839 struct imsm_map
*map
;
8840 int i
, j
, num_members
;
8843 dprintf("%s: deleting device[%d] from imsm_super\n",
8846 /* shift all indexes down one */
8847 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
8848 if (iter
->index
> (int)index
)
8850 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
8851 if (iter
->index
> (int)index
)
8854 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8855 dev
= get_imsm_dev(super
, i
);
8856 map
= get_imsm_map(dev
, MAP_0
);
8857 num_members
= map
->num_members
;
8858 for (j
= 0; j
< num_members
; j
++) {
8859 /* update ord entries being careful not to propagate
8860 * ord-flags to the first map
8862 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_X
);
8864 if (ord_to_idx(ord
) <= index
)
8867 map
= get_imsm_map(dev
, MAP_0
);
8868 set_imsm_ord_tbl_ent(map
, j
, ord_to_idx(ord
- 1));
8869 map
= get_imsm_map(dev
, MAP_1
);
8871 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
8876 super
->updates_pending
++;
8878 struct dl
*dl
= *dlp
;
8880 *dlp
= (*dlp
)->next
;
8881 __free_imsm_disk(dl
);
8884 #endif /* MDASSEMBLE */
8886 static void close_targets(int *targets
, int new_disks
)
8893 for (i
= 0; i
< new_disks
; i
++) {
8894 if (targets
[i
] >= 0) {
8901 static int imsm_get_allowed_degradation(int level
, int raid_disks
,
8902 struct intel_super
*super
,
8903 struct imsm_dev
*dev
)
8909 struct imsm_map
*map
;
8912 ret_val
= raid_disks
/2;
8913 /* check map if all disks pairs not failed
8916 map
= get_imsm_map(dev
, MAP_0
);
8917 for (i
= 0; i
< ret_val
; i
++) {
8918 int degradation
= 0;
8919 if (get_imsm_disk(super
, i
) == NULL
)
8921 if (get_imsm_disk(super
, i
+ 1) == NULL
)
8923 if (degradation
== 2)
8926 map
= get_imsm_map(dev
, MAP_1
);
8927 /* if there is no second map
8928 * result can be returned
8932 /* check degradation in second map
8934 for (i
= 0; i
< ret_val
; i
++) {
8935 int degradation
= 0;
8936 if (get_imsm_disk(super
, i
) == NULL
)
8938 if (get_imsm_disk(super
, i
+ 1) == NULL
)
8940 if (degradation
== 2)
8954 /*******************************************************************************
8955 * Function: open_backup_targets
8956 * Description: Function opens file descriptors for all devices given in
8959 * info : general array info
8960 * raid_disks : number of disks
8961 * raid_fds : table of device's file descriptors
8962 * super : intel super for raid10 degradation check
8963 * dev : intel device for raid10 degradation check
8967 ******************************************************************************/
8968 int open_backup_targets(struct mdinfo
*info
, int raid_disks
, int *raid_fds
,
8969 struct intel_super
*super
, struct imsm_dev
*dev
)
8975 for (i
= 0; i
< raid_disks
; i
++)
8978 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
8981 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
)) {
8982 dprintf("disk is faulty!!\n");
8986 if ((sd
->disk
.raid_disk
>= raid_disks
) ||
8987 (sd
->disk
.raid_disk
< 0))
8990 dn
= map_dev(sd
->disk
.major
,
8992 raid_fds
[sd
->disk
.raid_disk
] = dev_open(dn
, O_RDWR
);
8993 if (raid_fds
[sd
->disk
.raid_disk
] < 0) {
8994 pr_err("cannot open component\n");
8999 /* check if maximum array degradation level is not exceeded
9001 if ((raid_disks
- opened
) >
9002 imsm_get_allowed_degradation(info
->new_level
,
9005 pr_err("Not enough disks can be opened.\n");
9006 close_targets(raid_fds
, raid_disks
);
9013 /*******************************************************************************
9014 * Function: init_migr_record_imsm
9015 * Description: Function inits imsm migration record
9017 * super : imsm internal array info
9018 * dev : device under migration
9019 * info : general array info to find the smallest device
9022 ******************************************************************************/
9023 void init_migr_record_imsm(struct supertype
*st
, struct imsm_dev
*dev
,
9024 struct mdinfo
*info
)
9026 struct intel_super
*super
= st
->sb
;
9027 struct migr_record
*migr_rec
= super
->migr_rec
;
9029 unsigned long long dsize
, dev_sectors
;
9030 long long unsigned min_dev_sectors
= -1LLU;
9034 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
9035 struct imsm_map
*map_src
= get_imsm_map(dev
, MAP_1
);
9036 unsigned long long num_migr_units
;
9037 unsigned long long array_blocks
;
9039 memset(migr_rec
, 0, sizeof(struct migr_record
));
9040 migr_rec
->family_num
= __cpu_to_le32(super
->anchor
->family_num
);
9042 /* only ascending reshape supported now */
9043 migr_rec
->ascending_migr
= __cpu_to_le32(1);
9045 migr_rec
->dest_depth_per_unit
= GEN_MIGR_AREA_SIZE
/
9046 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
9047 migr_rec
->dest_depth_per_unit
*=
9048 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
9049 new_data_disks
= imsm_num_data_members(dev
, MAP_0
);
9050 migr_rec
->blocks_per_unit
=
9051 __cpu_to_le32(migr_rec
->dest_depth_per_unit
* new_data_disks
);
9052 migr_rec
->dest_depth_per_unit
=
9053 __cpu_to_le32(migr_rec
->dest_depth_per_unit
);
9054 array_blocks
= info
->component_size
* new_data_disks
;
9056 array_blocks
/ __le32_to_cpu(migr_rec
->blocks_per_unit
);
9058 if (array_blocks
% __le32_to_cpu(migr_rec
->blocks_per_unit
))
9060 migr_rec
->num_migr_units
= __cpu_to_le32(num_migr_units
);
9062 migr_rec
->post_migr_vol_cap
= dev
->size_low
;
9063 migr_rec
->post_migr_vol_cap_hi
= dev
->size_high
;
9065 /* Find the smallest dev */
9066 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
9067 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
9068 fd
= dev_open(nm
, O_RDONLY
);
9071 get_dev_size(fd
, NULL
, &dsize
);
9072 dev_sectors
= dsize
/ 512;
9073 if (dev_sectors
< min_dev_sectors
)
9074 min_dev_sectors
= dev_sectors
;
9077 migr_rec
->ckpt_area_pba
= __cpu_to_le32(min_dev_sectors
-
9078 RAID_DISK_RESERVED_BLOCKS_IMSM_HI
);
9080 write_imsm_migr_rec(st
);
9085 /*******************************************************************************
9086 * Function: save_backup_imsm
9087 * Description: Function saves critical data stripes to Migration Copy Area
9088 * and updates the current migration unit status.
9089 * Use restore_stripes() to form a destination stripe,
9090 * and to write it to the Copy Area.
9092 * st : supertype information
9093 * dev : imsm device that backup is saved for
9094 * info : general array info
9095 * buf : input buffer
9096 * length : length of data to backup (blocks_per_unit)
9100 ******************************************************************************/
9101 int save_backup_imsm(struct supertype
*st
,
9102 struct imsm_dev
*dev
,
9103 struct mdinfo
*info
,
9108 struct intel_super
*super
= st
->sb
;
9109 unsigned long long *target_offsets
= NULL
;
9110 int *targets
= NULL
;
9112 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
9113 int new_disks
= map_dest
->num_members
;
9114 int dest_layout
= 0;
9116 unsigned long long start
;
9117 int data_disks
= imsm_num_data_members(dev
, MAP_0
);
9119 targets
= xmalloc(new_disks
* sizeof(int));
9121 for (i
= 0; i
< new_disks
; i
++)
9124 target_offsets
= xcalloc(new_disks
, sizeof(unsigned long long));
9126 start
= info
->reshape_progress
* 512;
9127 for (i
= 0; i
< new_disks
; i
++) {
9128 target_offsets
[i
] = (unsigned long long)
9129 __le32_to_cpu(super
->migr_rec
->ckpt_area_pba
) * 512;
9130 /* move back copy area adderss, it will be moved forward
9131 * in restore_stripes() using start input variable
9133 target_offsets
[i
] -= start
/data_disks
;
9136 if (open_backup_targets(info
, new_disks
, targets
,
9140 dest_layout
= imsm_level_to_layout(map_dest
->raid_level
);
9141 dest_chunk
= __le16_to_cpu(map_dest
->blocks_per_strip
) * 512;
9143 if (restore_stripes(targets
, /* list of dest devices */
9144 target_offsets
, /* migration record offsets */
9147 map_dest
->raid_level
,
9149 -1, /* source backup file descriptor */
9150 0, /* input buf offset
9151 * always 0 buf is already offseted */
9155 pr_err("Error restoring stripes\n");
9163 close_targets(targets
, new_disks
);
9166 free(target_offsets
);
9171 /*******************************************************************************
9172 * Function: save_checkpoint_imsm
9173 * Description: Function called for current unit status update
9174 * in the migration record. It writes it to disk.
9176 * super : imsm internal array info
9177 * info : general array info
9181 * 2: failure, means no valid migration record
9182 * / no general migration in progress /
9183 ******************************************************************************/
9184 int save_checkpoint_imsm(struct supertype
*st
, struct mdinfo
*info
, int state
)
9186 struct intel_super
*super
= st
->sb
;
9187 unsigned long long blocks_per_unit
;
9188 unsigned long long curr_migr_unit
;
9190 if (load_imsm_migr_rec(super
, info
) != 0) {
9191 dprintf("imsm: ERROR: Cannot read migration record "
9192 "for checkpoint save.\n");
9196 blocks_per_unit
= __le32_to_cpu(super
->migr_rec
->blocks_per_unit
);
9197 if (blocks_per_unit
== 0) {
9198 dprintf("imsm: no migration in progress.\n");
9201 curr_migr_unit
= info
->reshape_progress
/ blocks_per_unit
;
9202 /* check if array is alligned to copy area
9203 * if it is not alligned, add one to current migration unit value
9204 * this can happend on array reshape finish only
9206 if (info
->reshape_progress
% blocks_per_unit
)
9209 super
->migr_rec
->curr_migr_unit
=
9210 __cpu_to_le32(curr_migr_unit
);
9211 super
->migr_rec
->rec_status
= __cpu_to_le32(state
);
9212 super
->migr_rec
->dest_1st_member_lba
=
9213 __cpu_to_le32(curr_migr_unit
*
9214 __le32_to_cpu(super
->migr_rec
->dest_depth_per_unit
));
9215 if (write_imsm_migr_rec(st
) < 0) {
9216 dprintf("imsm: Cannot write migration record "
9217 "outside backup area\n");
9224 /*******************************************************************************
9225 * Function: recover_backup_imsm
9226 * Description: Function recovers critical data from the Migration Copy Area
9227 * while assembling an array.
9229 * super : imsm internal array info
9230 * info : general array info
9232 * 0 : success (or there is no data to recover)
9234 ******************************************************************************/
9235 int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
)
9237 struct intel_super
*super
= st
->sb
;
9238 struct migr_record
*migr_rec
= super
->migr_rec
;
9239 struct imsm_map
*map_dest
= NULL
;
9240 struct intel_dev
*id
= NULL
;
9241 unsigned long long read_offset
;
9242 unsigned long long write_offset
;
9244 int *targets
= NULL
;
9245 int new_disks
, i
, err
;
9248 unsigned long curr_migr_unit
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
9249 unsigned long num_migr_units
= __le32_to_cpu(migr_rec
->num_migr_units
);
9251 int skipped_disks
= 0;
9253 err
= sysfs_get_str(info
, NULL
, "array_state", (char *)buffer
, 20);
9257 /* recover data only during assemblation */
9258 if (strncmp(buffer
, "inactive", 8) != 0)
9260 /* no data to recover */
9261 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
9263 if (curr_migr_unit
>= num_migr_units
)
9266 /* find device during reshape */
9267 for (id
= super
->devlist
; id
; id
= id
->next
)
9268 if (is_gen_migration(id
->dev
))
9273 map_dest
= get_imsm_map(id
->dev
, MAP_0
);
9274 new_disks
= map_dest
->num_members
;
9276 read_offset
= (unsigned long long)
9277 __le32_to_cpu(migr_rec
->ckpt_area_pba
) * 512;
9279 write_offset
= ((unsigned long long)
9280 __le32_to_cpu(migr_rec
->dest_1st_member_lba
) +
9281 pba_of_lba0(map_dest
)) * 512;
9283 unit_len
= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
9284 if (posix_memalign((void **)&buf
, 512, unit_len
) != 0)
9286 targets
= xcalloc(new_disks
, sizeof(int));
9288 if (open_backup_targets(info
, new_disks
, targets
, super
, id
->dev
)) {
9289 pr_err("Cannot open some devices belonging to array.\n");
9293 for (i
= 0; i
< new_disks
; i
++) {
9294 if (targets
[i
] < 0) {
9298 if (lseek64(targets
[i
], read_offset
, SEEK_SET
) < 0) {
9299 pr_err("Cannot seek to block: %s\n",
9304 if ((unsigned)read(targets
[i
], buf
, unit_len
) != unit_len
) {
9305 pr_err("Cannot read copy area block: %s\n",
9310 if (lseek64(targets
[i
], write_offset
, SEEK_SET
) < 0) {
9311 pr_err("Cannot seek to block: %s\n",
9316 if ((unsigned)write(targets
[i
], buf
, unit_len
) != unit_len
) {
9317 pr_err("Cannot restore block: %s\n",
9324 if (skipped_disks
> imsm_get_allowed_degradation(info
->new_level
,
9328 pr_err("Cannot restore data from backup."
9329 " Too many failed disks\n");
9333 if (save_checkpoint_imsm(st
, info
, UNIT_SRC_NORMAL
)) {
9334 /* ignore error == 2, this can mean end of reshape here
9336 dprintf("imsm: Cannot write checkpoint to "
9337 "migration record (UNIT_SRC_NORMAL) during restart\n");
9343 for (i
= 0; i
< new_disks
; i
++)
9352 static char disk_by_path
[] = "/dev/disk/by-path/";
9354 static const char *imsm_get_disk_controller_domain(const char *path
)
9356 char disk_path
[PATH_MAX
];
9360 strcpy(disk_path
, disk_by_path
);
9361 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
9362 if (stat(disk_path
, &st
) == 0) {
9363 struct sys_dev
* hba
;
9366 path
= devt_to_devpath(st
.st_rdev
);
9369 hba
= find_disk_attached_hba(-1, path
);
9370 if (hba
&& hba
->type
== SYS_DEV_SAS
)
9372 else if (hba
&& hba
->type
== SYS_DEV_SATA
)
9376 dprintf("path: %s hba: %s attached: %s\n",
9377 path
, (hba
) ? hba
->path
: "NULL", drv
);
9383 static char *imsm_find_array_devnm_by_subdev(int subdev
, char *container
)
9385 static char devnm
[32];
9386 char subdev_name
[20];
9387 struct mdstat_ent
*mdstat
;
9389 sprintf(subdev_name
, "%d", subdev
);
9390 mdstat
= mdstat_by_subdev(subdev_name
, container
);
9394 strcpy(devnm
, mdstat
->devnm
);
9395 free_mdstat(mdstat
);
9399 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
9400 struct geo_params
*geo
,
9401 int *old_raid_disks
,
9404 /* currently we only support increasing the number of devices
9405 * for a container. This increases the number of device for each
9406 * member array. They must all be RAID0 or RAID5.
9409 struct mdinfo
*info
, *member
;
9410 int devices_that_can_grow
= 0;
9412 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): "
9413 "st->devnm = (%s)\n", st
->devnm
);
9415 if (geo
->size
> 0 ||
9416 geo
->level
!= UnSet
||
9417 geo
->layout
!= UnSet
||
9418 geo
->chunksize
!= 0 ||
9419 geo
->raid_disks
== UnSet
) {
9420 dprintf("imsm: Container operation is allowed for "
9421 "raid disks number change only.\n");
9425 if (direction
== ROLLBACK_METADATA_CHANGES
) {
9426 dprintf("imsm: Metadata changes rollback is not supported for "
9427 "container operation.\n");
9431 info
= container_content_imsm(st
, NULL
);
9432 for (member
= info
; member
; member
= member
->next
) {
9435 dprintf("imsm: checking device_num: %i\n",
9436 member
->container_member
);
9438 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
9439 /* we work on container for Online Capacity Expansion
9440 * only so raid_disks has to grow
9442 dprintf("imsm: for container operation raid disks "
9443 "increase is required\n");
9447 if ((info
->array
.level
!= 0) &&
9448 (info
->array
.level
!= 5)) {
9449 /* we cannot use this container with other raid level
9451 dprintf("imsm: for container operation wrong"
9452 " raid level (%i) detected\n",
9456 /* check for platform support
9457 * for this raid level configuration
9459 struct intel_super
*super
= st
->sb
;
9460 if (!is_raid_level_supported(super
->orom
,
9461 member
->array
.level
,
9463 dprintf("platform does not support raid%d with"
9467 geo
->raid_disks
> 1 ? "s" : "");
9470 /* check if component size is aligned to chunk size
9472 if (info
->component_size
%
9473 (info
->array
.chunk_size
/512)) {
9474 dprintf("Component size is not aligned to "
9480 if (*old_raid_disks
&&
9481 info
->array
.raid_disks
!= *old_raid_disks
)
9483 *old_raid_disks
= info
->array
.raid_disks
;
9485 /* All raid5 and raid0 volumes in container
9486 * have to be ready for Online Capacity Expansion
9487 * so they need to be assembled. We have already
9488 * checked that no recovery etc is happening.
9490 result
= imsm_find_array_devnm_by_subdev(member
->container_member
,
9491 st
->container_devnm
);
9492 if (result
== NULL
) {
9493 dprintf("imsm: cannot find array\n");
9496 devices_that_can_grow
++;
9499 if (!member
&& devices_that_can_grow
)
9503 dprintf("\tContainer operation allowed\n");
9505 dprintf("\tError: %i\n", ret_val
);
9510 /* Function: get_spares_for_grow
9511 * Description: Allocates memory and creates list of spare devices
9512 * avaliable in container. Checks if spare drive size is acceptable.
9513 * Parameters: Pointer to the supertype structure
9514 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
9517 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
9519 unsigned long long min_size
= min_acceptable_spare_size_imsm(st
);
9520 return container_choose_spares(st
, min_size
, NULL
, NULL
, NULL
, 0);
9523 /******************************************************************************
9524 * function: imsm_create_metadata_update_for_reshape
9525 * Function creates update for whole IMSM container.
9527 ******************************************************************************/
9528 static int imsm_create_metadata_update_for_reshape(
9529 struct supertype
*st
,
9530 struct geo_params
*geo
,
9532 struct imsm_update_reshape
**updatep
)
9534 struct intel_super
*super
= st
->sb
;
9535 struct imsm_super
*mpb
= super
->anchor
;
9536 int update_memory_size
= 0;
9537 struct imsm_update_reshape
*u
= NULL
;
9538 struct mdinfo
*spares
= NULL
;
9540 int delta_disks
= 0;
9543 dprintf("imsm_update_metadata_for_reshape(enter) raid_disks = %i\n",
9546 delta_disks
= geo
->raid_disks
- old_raid_disks
;
9548 /* size of all update data without anchor */
9549 update_memory_size
= sizeof(struct imsm_update_reshape
);
9551 /* now add space for spare disks that we need to add. */
9552 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
9554 u
= xcalloc(1, update_memory_size
);
9555 u
->type
= update_reshape_container_disks
;
9556 u
->old_raid_disks
= old_raid_disks
;
9557 u
->new_raid_disks
= geo
->raid_disks
;
9559 /* now get spare disks list
9561 spares
= get_spares_for_grow(st
);
9564 || delta_disks
> spares
->array
.spare_disks
) {
9565 pr_err("imsm: ERROR: Cannot get spare devices "
9566 "for %s.\n", geo
->dev_name
);
9571 /* we have got spares
9572 * update disk list in imsm_disk list table in anchor
9574 dprintf("imsm: %i spares are available.\n\n",
9575 spares
->array
.spare_disks
);
9578 for (i
= 0; i
< delta_disks
; i
++) {
9583 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
9585 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
9586 dl
->index
= mpb
->num_disks
;
9596 dprintf("imsm: reshape update preparation :");
9597 if (i
== delta_disks
) {
9600 return update_memory_size
;
9603 dprintf(" Error\n");
9608 /******************************************************************************
9609 * function: imsm_create_metadata_update_for_size_change()
9610 * Creates update for IMSM array for array size change.
9612 ******************************************************************************/
9613 static int imsm_create_metadata_update_for_size_change(
9614 struct supertype
*st
,
9615 struct geo_params
*geo
,
9616 struct imsm_update_size_change
**updatep
)
9618 struct intel_super
*super
= st
->sb
;
9619 int update_memory_size
= 0;
9620 struct imsm_update_size_change
*u
= NULL
;
9622 dprintf("imsm_create_metadata_update_for_size_change(enter)"
9623 " New size = %llu\n", geo
->size
);
9625 /* size of all update data without anchor */
9626 update_memory_size
= sizeof(struct imsm_update_size_change
);
9628 u
= xcalloc(1, update_memory_size
);
9629 u
->type
= update_size_change
;
9630 u
->subdev
= super
->current_vol
;
9631 u
->new_size
= geo
->size
;
9633 dprintf("imsm: reshape update preparation : OK\n");
9636 return update_memory_size
;
9639 /******************************************************************************
9640 * function: imsm_create_metadata_update_for_migration()
9641 * Creates update for IMSM array.
9643 ******************************************************************************/
9644 static int imsm_create_metadata_update_for_migration(
9645 struct supertype
*st
,
9646 struct geo_params
*geo
,
9647 struct imsm_update_reshape_migration
**updatep
)
9649 struct intel_super
*super
= st
->sb
;
9650 int update_memory_size
= 0;
9651 struct imsm_update_reshape_migration
*u
= NULL
;
9652 struct imsm_dev
*dev
;
9653 int previous_level
= -1;
9655 dprintf("imsm_create_metadata_update_for_migration(enter)"
9656 " New Level = %i\n", geo
->level
);
9658 /* size of all update data without anchor */
9659 update_memory_size
= sizeof(struct imsm_update_reshape_migration
);
9661 u
= xcalloc(1, update_memory_size
);
9662 u
->type
= update_reshape_migration
;
9663 u
->subdev
= super
->current_vol
;
9664 u
->new_level
= geo
->level
;
9665 u
->new_layout
= geo
->layout
;
9666 u
->new_raid_disks
= u
->old_raid_disks
= geo
->raid_disks
;
9667 u
->new_disks
[0] = -1;
9668 u
->new_chunksize
= -1;
9670 dev
= get_imsm_dev(super
, u
->subdev
);
9672 struct imsm_map
*map
;
9674 map
= get_imsm_map(dev
, MAP_0
);
9676 int current_chunk_size
=
9677 __le16_to_cpu(map
->blocks_per_strip
) / 2;
9679 if (geo
->chunksize
!= current_chunk_size
) {
9680 u
->new_chunksize
= geo
->chunksize
/ 1024;
9682 "chunk size change from %i to %i\n",
9683 current_chunk_size
, u
->new_chunksize
);
9685 previous_level
= map
->raid_level
;
9688 if ((geo
->level
== 5) && (previous_level
== 0)) {
9689 struct mdinfo
*spares
= NULL
;
9691 u
->new_raid_disks
++;
9692 spares
= get_spares_for_grow(st
);
9693 if ((spares
== NULL
) || (spares
->array
.spare_disks
< 1)) {
9696 update_memory_size
= 0;
9697 dprintf("error: cannot get spare device "
9698 "for requested migration");
9703 dprintf("imsm: reshape update preparation : OK\n");
9706 return update_memory_size
;
9709 static void imsm_update_metadata_locally(struct supertype
*st
,
9712 struct metadata_update mu
;
9717 mu
.space_list
= NULL
;
9719 imsm_prepare_update(st
, &mu
);
9720 imsm_process_update(st
, &mu
);
9722 while (mu
.space_list
) {
9723 void **space
= mu
.space_list
;
9724 mu
.space_list
= *space
;
9729 /***************************************************************************
9730 * Function: imsm_analyze_change
9731 * Description: Function analyze change for single volume
9732 * and validate if transition is supported
9733 * Parameters: Geometry parameters, supertype structure,
9734 * metadata change direction (apply/rollback)
9735 * Returns: Operation type code on success, -1 if fail
9736 ****************************************************************************/
9737 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
9738 struct geo_params
*geo
,
9745 /* number of added/removed disks in operation result */
9746 int devNumChange
= 0;
9747 /* imsm compatible layout value for array geometry verification */
9748 int imsm_layout
= -1;
9750 struct imsm_dev
*dev
;
9751 struct intel_super
*super
;
9752 unsigned long long current_size
;
9753 unsigned long long free_size
;
9754 unsigned long long max_size
;
9757 getinfo_super_imsm_volume(st
, &info
, NULL
);
9758 if ((geo
->level
!= info
.array
.level
) &&
9759 (geo
->level
>= 0) &&
9760 (geo
->level
!= UnSet
)) {
9761 switch (info
.array
.level
) {
9763 if (geo
->level
== 5) {
9764 change
= CH_MIGRATION
;
9765 if (geo
->layout
!= ALGORITHM_LEFT_ASYMMETRIC
) {
9766 pr_err("Error. Requested Layout "
9767 "not supported (left-asymmetric layout "
9768 "is supported only)!\n");
9770 goto analyse_change_exit
;
9772 imsm_layout
= geo
->layout
;
9774 devNumChange
= 1; /* parity disk added */
9775 } else if (geo
->level
== 10) {
9776 change
= CH_TAKEOVER
;
9778 devNumChange
= 2; /* two mirrors added */
9779 imsm_layout
= 0x102; /* imsm supported layout */
9784 if (geo
->level
== 0) {
9785 change
= CH_TAKEOVER
;
9787 devNumChange
= -(geo
->raid_disks
/2);
9788 imsm_layout
= 0; /* imsm raid0 layout */
9793 pr_err("Error. Level Migration from %d to %d "
9795 info
.array
.level
, geo
->level
);
9796 goto analyse_change_exit
;
9799 geo
->level
= info
.array
.level
;
9801 if ((geo
->layout
!= info
.array
.layout
)
9802 && ((geo
->layout
!= UnSet
) && (geo
->layout
!= -1))) {
9803 change
= CH_MIGRATION
;
9804 if ((info
.array
.layout
== 0)
9805 && (info
.array
.level
== 5)
9806 && (geo
->layout
== 5)) {
9807 /* reshape 5 -> 4 */
9808 } else if ((info
.array
.layout
== 5)
9809 && (info
.array
.level
== 5)
9810 && (geo
->layout
== 0)) {
9811 /* reshape 4 -> 5 */
9815 pr_err("Error. Layout Migration from %d to %d "
9817 info
.array
.layout
, geo
->layout
);
9819 goto analyse_change_exit
;
9822 geo
->layout
= info
.array
.layout
;
9823 if (imsm_layout
== -1)
9824 imsm_layout
= info
.array
.layout
;
9827 if ((geo
->chunksize
> 0) && (geo
->chunksize
!= UnSet
)
9828 && (geo
->chunksize
!= info
.array
.chunk_size
))
9829 change
= CH_MIGRATION
;
9831 geo
->chunksize
= info
.array
.chunk_size
;
9833 chunk
= geo
->chunksize
/ 1024;
9836 dev
= get_imsm_dev(super
, super
->current_vol
);
9837 data_disks
= imsm_num_data_members(dev
, MAP_0
);
9838 /* compute current size per disk member
9840 current_size
= info
.custom_array_size
/ data_disks
;
9842 if ((geo
->size
> 0) && (geo
->size
!= MAX_SIZE
)) {
9843 /* align component size
9845 geo
->size
= imsm_component_size_aligment_check(
9846 get_imsm_raid_level(dev
->vol
.map
),
9849 if (geo
->size
== 0) {
9850 pr_err("Error. Size expansion is " \
9851 "supported only (current size is %llu, " \
9852 "requested size /rounded/ is 0).\n",
9854 goto analyse_change_exit
;
9858 if ((current_size
!= geo
->size
) && (geo
->size
> 0)) {
9860 pr_err("Error. Size change should be the only "
9861 "one at a time.\n");
9863 goto analyse_change_exit
;
9865 if ((super
->current_vol
+ 1) != super
->anchor
->num_raid_devs
) {
9866 pr_err("Error. The last volume in container "
9867 "can be expanded only (%i/%s).\n",
9868 super
->current_vol
, st
->devnm
);
9869 goto analyse_change_exit
;
9871 /* check the maximum available size
9873 rv
= imsm_get_free_size(st
, dev
->vol
.map
->num_members
,
9874 0, chunk
, &free_size
);
9876 /* Cannot find maximum available space
9880 max_size
= free_size
+ current_size
;
9881 /* align component size
9883 max_size
= imsm_component_size_aligment_check(
9884 get_imsm_raid_level(dev
->vol
.map
),
9888 if (geo
->size
== MAX_SIZE
) {
9889 /* requested size change to the maximum available size
9891 if (max_size
== 0) {
9892 pr_err("Error. Cannot find "
9893 "maximum available space.\n");
9895 goto analyse_change_exit
;
9897 geo
->size
= max_size
;
9900 if ((direction
== ROLLBACK_METADATA_CHANGES
)) {
9901 /* accept size for rollback only
9904 /* round size due to metadata compatibility
9906 geo
->size
= (geo
->size
>> SECT_PER_MB_SHIFT
)
9907 << SECT_PER_MB_SHIFT
;
9908 dprintf("Prepare update for size change to %llu\n",
9910 if (current_size
>= geo
->size
) {
9911 pr_err("Error. Size expansion is "
9912 "supported only (current size is %llu, "
9913 "requested size /rounded/ is %llu).\n",
9914 current_size
, geo
->size
);
9915 goto analyse_change_exit
;
9917 if (max_size
&& geo
->size
> max_size
) {
9918 pr_err("Error. Requested size is larger "
9919 "than maximum available size (maximum "
9920 "available size is %llu, "
9921 "requested size /rounded/ is %llu).\n",
9922 max_size
, geo
->size
);
9923 goto analyse_change_exit
;
9926 geo
->size
*= data_disks
;
9927 geo
->raid_disks
= dev
->vol
.map
->num_members
;
9928 change
= CH_ARRAY_SIZE
;
9930 if (!validate_geometry_imsm(st
,
9933 geo
->raid_disks
+ devNumChange
,
9935 geo
->size
, INVALID_SECTORS
,
9940 struct intel_super
*super
= st
->sb
;
9941 struct imsm_super
*mpb
= super
->anchor
;
9943 if (mpb
->num_raid_devs
> 1) {
9944 pr_err("Error. Cannot perform operation on %s"
9945 "- for this operation it MUST be single "
9946 "array in container\n",
9952 analyse_change_exit
:
9953 if ((direction
== ROLLBACK_METADATA_CHANGES
) &&
9954 ((change
== CH_MIGRATION
) || (change
== CH_TAKEOVER
))) {
9955 dprintf("imsm: Metadata changes rollback is not supported for "
9956 "migration and takeover operations.\n");
9962 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
9964 struct intel_super
*super
= st
->sb
;
9965 struct imsm_update_takeover
*u
;
9967 u
= xmalloc(sizeof(struct imsm_update_takeover
));
9969 u
->type
= update_takeover
;
9970 u
->subarray
= super
->current_vol
;
9972 /* 10->0 transition */
9973 if (geo
->level
== 0)
9974 u
->direction
= R10_TO_R0
;
9976 /* 0->10 transition */
9977 if (geo
->level
== 10)
9978 u
->direction
= R0_TO_R10
;
9980 /* update metadata locally */
9981 imsm_update_metadata_locally(st
, u
,
9982 sizeof(struct imsm_update_takeover
));
9983 /* and possibly remotely */
9984 if (st
->update_tail
)
9985 append_metadata_update(st
, u
,
9986 sizeof(struct imsm_update_takeover
));
9993 static int imsm_reshape_super(struct supertype
*st
, unsigned long long size
,
9995 int layout
, int chunksize
, int raid_disks
,
9996 int delta_disks
, char *backup
, char *dev
,
9997 int direction
, int verbose
)
10000 struct geo_params geo
;
10002 dprintf("imsm: reshape_super called.\n");
10004 memset(&geo
, 0, sizeof(struct geo_params
));
10006 geo
.dev_name
= dev
;
10007 strcpy(geo
.devnm
, st
->devnm
);
10010 geo
.layout
= layout
;
10011 geo
.chunksize
= chunksize
;
10012 geo
.raid_disks
= raid_disks
;
10013 if (delta_disks
!= UnSet
)
10014 geo
.raid_disks
+= delta_disks
;
10016 dprintf("\tfor level : %i\n", geo
.level
);
10017 dprintf("\tfor raid_disks : %i\n", geo
.raid_disks
);
10019 if (experimental() == 0)
10022 if (strcmp(st
->container_devnm
, st
->devnm
) == 0) {
10023 /* On container level we can only increase number of devices. */
10024 dprintf("imsm: info: Container operation\n");
10025 int old_raid_disks
= 0;
10027 if (imsm_reshape_is_allowed_on_container(
10028 st
, &geo
, &old_raid_disks
, direction
)) {
10029 struct imsm_update_reshape
*u
= NULL
;
10032 len
= imsm_create_metadata_update_for_reshape(
10033 st
, &geo
, old_raid_disks
, &u
);
10036 dprintf("imsm: Cannot prepare update\n");
10037 goto exit_imsm_reshape_super
;
10041 /* update metadata locally */
10042 imsm_update_metadata_locally(st
, u
, len
);
10043 /* and possibly remotely */
10044 if (st
->update_tail
)
10045 append_metadata_update(st
, u
, len
);
10050 pr_err("(imsm) Operation "
10051 "is not allowed on this container\n");
10054 /* On volume level we support following operations
10055 * - takeover: raid10 -> raid0; raid0 -> raid10
10056 * - chunk size migration
10057 * - migration: raid5 -> raid0; raid0 -> raid5
10059 struct intel_super
*super
= st
->sb
;
10060 struct intel_dev
*dev
= super
->devlist
;
10062 dprintf("imsm: info: Volume operation\n");
10063 /* find requested device */
10066 imsm_find_array_devnm_by_subdev(
10067 dev
->index
, st
->container_devnm
);
10068 if (devnm
&& strcmp(devnm
, geo
.devnm
) == 0)
10073 pr_err("Cannot find %s (%s) subarray\n",
10074 geo
.dev_name
, geo
.devnm
);
10075 goto exit_imsm_reshape_super
;
10077 super
->current_vol
= dev
->index
;
10078 change
= imsm_analyze_change(st
, &geo
, direction
);
10081 ret_val
= imsm_takeover(st
, &geo
);
10083 case CH_MIGRATION
: {
10084 struct imsm_update_reshape_migration
*u
= NULL
;
10086 imsm_create_metadata_update_for_migration(
10090 "Cannot prepare update\n");
10094 /* update metadata locally */
10095 imsm_update_metadata_locally(st
, u
, len
);
10096 /* and possibly remotely */
10097 if (st
->update_tail
)
10098 append_metadata_update(st
, u
, len
);
10103 case CH_ARRAY_SIZE
: {
10104 struct imsm_update_size_change
*u
= NULL
;
10106 imsm_create_metadata_update_for_size_change(
10110 "Cannot prepare update\n");
10114 /* update metadata locally */
10115 imsm_update_metadata_locally(st
, u
, len
);
10116 /* and possibly remotely */
10117 if (st
->update_tail
)
10118 append_metadata_update(st
, u
, len
);
10128 exit_imsm_reshape_super
:
10129 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
10133 /*******************************************************************************
10134 * Function: wait_for_reshape_imsm
10135 * Description: Function writes new sync_max value and waits until
10136 * reshape process reach new position
10138 * sra : general array info
10139 * ndata : number of disks in new array's layout
10142 * 1 : there is no reshape in progress,
10144 ******************************************************************************/
10145 int wait_for_reshape_imsm(struct mdinfo
*sra
, int ndata
)
10147 int fd
= sysfs_get_fd(sra
, NULL
, "sync_completed");
10148 unsigned long long completed
;
10149 /* to_complete : new sync_max position */
10150 unsigned long long to_complete
= sra
->reshape_progress
;
10151 unsigned long long position_to_set
= to_complete
/ ndata
;
10154 dprintf("imsm: wait_for_reshape_imsm() "
10155 "cannot open reshape_position\n");
10159 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
10160 dprintf("imsm: wait_for_reshape_imsm() "
10161 "cannot read reshape_position (no reshape in progres)\n");
10166 if (completed
> position_to_set
) {
10167 dprintf("imsm: wait_for_reshape_imsm() "
10168 "wrong next position to set %llu (%llu)\n",
10169 to_complete
, position_to_set
);
10173 dprintf("Position set: %llu\n", position_to_set
);
10174 if (sysfs_set_num(sra
, NULL
, "sync_max",
10175 position_to_set
) != 0) {
10176 dprintf("imsm: wait_for_reshape_imsm() "
10177 "cannot set reshape position to %llu\n",
10185 sysfs_wait(fd
, NULL
);
10186 if (sysfs_get_str(sra
, NULL
, "sync_action",
10188 strncmp(action
, "reshape", 7) != 0)
10190 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
10191 dprintf("imsm: wait_for_reshape_imsm() "
10192 "cannot read reshape_position (in loop)\n");
10196 } while (completed
< position_to_set
);
10202 /*******************************************************************************
10203 * Function: check_degradation_change
10204 * Description: Check that array hasn't become failed.
10206 * info : for sysfs access
10207 * sources : source disks descriptors
10208 * degraded: previous degradation level
10210 * degradation level
10211 ******************************************************************************/
10212 int check_degradation_change(struct mdinfo
*info
,
10216 unsigned long long new_degraded
;
10219 rv
= sysfs_get_ll(info
, NULL
, "degraded", &new_degraded
);
10220 if ((rv
== -1) || (new_degraded
!= (unsigned long long)degraded
)) {
10221 /* check each device to ensure it is still working */
10224 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
10225 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
10227 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
10229 if (sysfs_get_str(info
,
10230 sd
, "state", sbuf
, 20) < 0 ||
10231 strstr(sbuf
, "faulty") ||
10232 strstr(sbuf
, "in_sync") == NULL
) {
10233 /* this device is dead */
10234 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
10235 if (sd
->disk
.raid_disk
>= 0 &&
10236 sources
[sd
->disk
.raid_disk
] >= 0) {
10238 sd
->disk
.raid_disk
]);
10239 sources
[sd
->disk
.raid_disk
] =
10248 return new_degraded
;
10251 /*******************************************************************************
10252 * Function: imsm_manage_reshape
10253 * Description: Function finds array under reshape and it manages reshape
10254 * process. It creates stripes backups (if required) and sets
10257 * afd : Backup handle (nattive) - not used
10258 * sra : general array info
10259 * reshape : reshape parameters - not used
10260 * st : supertype structure
10261 * blocks : size of critical section [blocks]
10262 * fds : table of source device descriptor
10263 * offsets : start of array (offest per devices)
10265 * destfd : table of destination device descriptor
10266 * destoffsets : table of destination offsets (per device)
10268 * 1 : success, reshape is done
10270 ******************************************************************************/
10271 static int imsm_manage_reshape(
10272 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
10273 struct supertype
*st
, unsigned long backup_blocks
,
10274 int *fds
, unsigned long long *offsets
,
10275 int dests
, int *destfd
, unsigned long long *destoffsets
)
10278 struct intel_super
*super
= st
->sb
;
10279 struct intel_dev
*dv
= NULL
;
10280 struct imsm_dev
*dev
= NULL
;
10281 struct imsm_map
*map_src
;
10282 int migr_vol_qan
= 0;
10283 int ndata
, odata
; /* [bytes] */
10284 int chunk
; /* [bytes] */
10285 struct migr_record
*migr_rec
;
10287 unsigned int buf_size
; /* [bytes] */
10288 unsigned long long max_position
; /* array size [bytes] */
10289 unsigned long long next_step
; /* [blocks]/[bytes] */
10290 unsigned long long old_data_stripe_length
;
10291 unsigned long long start_src
; /* [bytes] */
10292 unsigned long long start
; /* [bytes] */
10293 unsigned long long start_buf_shift
; /* [bytes] */
10295 int source_layout
= 0;
10297 if (!fds
|| !offsets
|| !sra
)
10300 /* Find volume during the reshape */
10301 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
10302 if (dv
->dev
->vol
.migr_type
== MIGR_GEN_MIGR
10303 && dv
->dev
->vol
.migr_state
== 1) {
10308 /* Only one volume can migrate at the same time */
10309 if (migr_vol_qan
!= 1) {
10310 pr_err(": %s", migr_vol_qan
?
10311 "Number of migrating volumes greater than 1\n" :
10312 "There is no volume during migrationg\n");
10316 map_src
= get_imsm_map(dev
, MAP_1
);
10317 if (map_src
== NULL
)
10320 ndata
= imsm_num_data_members(dev
, MAP_0
);
10321 odata
= imsm_num_data_members(dev
, MAP_1
);
10323 chunk
= __le16_to_cpu(map_src
->blocks_per_strip
) * 512;
10324 old_data_stripe_length
= odata
* chunk
;
10326 migr_rec
= super
->migr_rec
;
10328 /* initialize migration record for start condition */
10329 if (sra
->reshape_progress
== 0)
10330 init_migr_record_imsm(st
, dev
, sra
);
10332 if (__le32_to_cpu(migr_rec
->rec_status
) != UNIT_SRC_NORMAL
) {
10333 dprintf("imsm: cannot restart migration when data "
10334 "are present in copy area.\n");
10337 /* Save checkpoint to update migration record for current
10338 * reshape position (in md). It can be farther than current
10339 * reshape position in metadata.
10341 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
10342 /* ignore error == 2, this can mean end of reshape here
10344 dprintf("imsm: Cannot write checkpoint to "
10345 "migration record (UNIT_SRC_NORMAL, "
10346 "initial save)\n");
10351 /* size for data */
10352 buf_size
= __le32_to_cpu(migr_rec
->blocks_per_unit
) * 512;
10353 /* extend buffer size for parity disk */
10354 buf_size
+= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
10355 /* add space for stripe aligment */
10356 buf_size
+= old_data_stripe_length
;
10357 if (posix_memalign((void **)&buf
, 4096, buf_size
)) {
10358 dprintf("imsm: Cannot allocate checpoint buffer\n");
10362 max_position
= sra
->component_size
* ndata
;
10363 source_layout
= imsm_level_to_layout(map_src
->raid_level
);
10365 while (__le32_to_cpu(migr_rec
->curr_migr_unit
) <
10366 __le32_to_cpu(migr_rec
->num_migr_units
)) {
10367 /* current reshape position [blocks] */
10368 unsigned long long current_position
=
10369 __le32_to_cpu(migr_rec
->blocks_per_unit
)
10370 * __le32_to_cpu(migr_rec
->curr_migr_unit
);
10371 unsigned long long border
;
10373 /* Check that array hasn't become failed.
10375 degraded
= check_degradation_change(sra
, fds
, degraded
);
10376 if (degraded
> 1) {
10377 dprintf("imsm: Abort reshape due to degradation"
10378 " level (%i)\n", degraded
);
10382 next_step
= __le32_to_cpu(migr_rec
->blocks_per_unit
);
10384 if ((current_position
+ next_step
) > max_position
)
10385 next_step
= max_position
- current_position
;
10387 start
= current_position
* 512;
10389 /* allign reading start to old geometry */
10390 start_buf_shift
= start
% old_data_stripe_length
;
10391 start_src
= start
- start_buf_shift
;
10393 border
= (start_src
/ odata
) - (start
/ ndata
);
10395 if (border
<= __le32_to_cpu(migr_rec
->dest_depth_per_unit
)) {
10396 /* save critical stripes to buf
10397 * start - start address of current unit
10398 * to backup [bytes]
10399 * start_src - start address of current unit
10400 * to backup alligned to source array
10403 unsigned long long next_step_filler
= 0;
10404 unsigned long long copy_length
= next_step
* 512;
10406 /* allign copy area length to stripe in old geometry */
10407 next_step_filler
= ((copy_length
+ start_buf_shift
)
10408 % old_data_stripe_length
);
10409 if (next_step_filler
)
10410 next_step_filler
= (old_data_stripe_length
10411 - next_step_filler
);
10412 dprintf("save_stripes() parameters: start = %llu,"
10413 "\tstart_src = %llu,\tnext_step*512 = %llu,"
10414 "\tstart_in_buf_shift = %llu,"
10415 "\tnext_step_filler = %llu\n",
10416 start
, start_src
, copy_length
,
10417 start_buf_shift
, next_step_filler
);
10419 if (save_stripes(fds
, offsets
, map_src
->num_members
,
10420 chunk
, map_src
->raid_level
,
10421 source_layout
, 0, NULL
, start_src
,
10423 next_step_filler
+ start_buf_shift
,
10425 dprintf("imsm: Cannot save stripes"
10429 /* Convert data to destination format and store it
10430 * in backup general migration area
10432 if (save_backup_imsm(st
, dev
, sra
,
10433 buf
+ start_buf_shift
, copy_length
)) {
10434 dprintf("imsm: Cannot save stripes to "
10435 "target devices\n");
10438 if (save_checkpoint_imsm(st
, sra
,
10439 UNIT_SRC_IN_CP_AREA
)) {
10440 dprintf("imsm: Cannot write checkpoint to "
10441 "migration record (UNIT_SRC_IN_CP_AREA)\n");
10445 /* set next step to use whole border area */
10446 border
/= next_step
;
10448 next_step
*= border
;
10450 /* When data backed up, checkpoint stored,
10451 * kick the kernel to reshape unit of data
10453 next_step
= next_step
+ sra
->reshape_progress
;
10454 /* limit next step to array max position */
10455 if (next_step
> max_position
)
10456 next_step
= max_position
;
10457 sysfs_set_num(sra
, NULL
, "suspend_lo", sra
->reshape_progress
);
10458 sysfs_set_num(sra
, NULL
, "suspend_hi", next_step
);
10459 sra
->reshape_progress
= next_step
;
10461 /* wait until reshape finish */
10462 if (wait_for_reshape_imsm(sra
, ndata
) < 0) {
10463 dprintf("wait_for_reshape_imsm returned error!\n");
10469 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
10470 /* ignore error == 2, this can mean end of reshape here
10472 dprintf("imsm: Cannot write checkpoint to "
10473 "migration record (UNIT_SRC_NORMAL)\n");
10479 /* return '1' if done */
10483 abort_reshape(sra
);
10487 #endif /* MDASSEMBLE */
10489 struct superswitch super_imsm
= {
10491 .examine_super
= examine_super_imsm
,
10492 .brief_examine_super
= brief_examine_super_imsm
,
10493 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
10494 .export_examine_super
= export_examine_super_imsm
,
10495 .detail_super
= detail_super_imsm
,
10496 .brief_detail_super
= brief_detail_super_imsm
,
10497 .write_init_super
= write_init_super_imsm
,
10498 .validate_geometry
= validate_geometry_imsm
,
10499 .add_to_super
= add_to_super_imsm
,
10500 .remove_from_super
= remove_from_super_imsm
,
10501 .detail_platform
= detail_platform_imsm
,
10502 .export_detail_platform
= export_detail_platform_imsm
,
10503 .kill_subarray
= kill_subarray_imsm
,
10504 .update_subarray
= update_subarray_imsm
,
10505 .load_container
= load_container_imsm
,
10506 .default_geometry
= default_geometry_imsm
,
10507 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
10508 .reshape_super
= imsm_reshape_super
,
10509 .manage_reshape
= imsm_manage_reshape
,
10510 .recover_backup
= recover_backup_imsm
,
10511 .copy_metadata
= copy_metadata_imsm
,
10513 .match_home
= match_home_imsm
,
10514 .uuid_from_super
= uuid_from_super_imsm
,
10515 .getinfo_super
= getinfo_super_imsm
,
10516 .getinfo_super_disks
= getinfo_super_disks_imsm
,
10517 .update_super
= update_super_imsm
,
10519 .avail_size
= avail_size_imsm
,
10520 .min_acceptable_spare_size
= min_acceptable_spare_size_imsm
,
10522 .compare_super
= compare_super_imsm
,
10524 .load_super
= load_super_imsm
,
10525 .init_super
= init_super_imsm
,
10526 .store_super
= store_super_imsm
,
10527 .free_super
= free_super_imsm
,
10528 .match_metadata_desc
= match_metadata_desc_imsm
,
10529 .container_content
= container_content_imsm
,
10536 .open_new
= imsm_open_new
,
10537 .set_array_state
= imsm_set_array_state
,
10538 .set_disk
= imsm_set_disk
,
10539 .sync_metadata
= imsm_sync_metadata
,
10540 .activate_spare
= imsm_activate_spare
,
10541 .process_update
= imsm_process_update
,
10542 .prepare_update
= imsm_prepare_update
,
10543 #endif /* MDASSEMBLE */