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",
513 [SYS_DEV_NVME
] = "NVMe",
514 [SYS_DEV_VMD
] = "VMD"
517 const char *get_sys_dev_type(enum sys_dev_type type
)
519 if (type
>= SYS_DEV_MAX
)
520 type
= SYS_DEV_UNKNOWN
;
522 return _sys_dev_type
[type
];
525 static struct intel_hba
* alloc_intel_hba(struct sys_dev
*device
)
527 struct intel_hba
*result
= xmalloc(sizeof(*result
));
529 result
->type
= device
->type
;
530 result
->path
= xstrdup(device
->path
);
532 if (result
->path
&& (result
->pci_id
= strrchr(result
->path
, '/')) != NULL
)
538 static struct intel_hba
* find_intel_hba(struct intel_hba
*hba
, struct sys_dev
*device
)
540 struct intel_hba
*result
=NULL
;
541 for (result
= hba
; result
; result
= result
->next
) {
542 if (result
->type
== device
->type
&& strcmp(result
->path
, device
->path
) == 0)
548 static int attach_hba_to_super(struct intel_super
*super
, struct sys_dev
*device
)
550 struct intel_hba
*hba
;
552 /* check if disk attached to Intel HBA */
553 hba
= find_intel_hba(super
->hba
, device
);
556 /* Check if HBA is already attached to super */
557 if (super
->hba
== NULL
) {
558 super
->hba
= alloc_intel_hba(device
);
563 /* Intel metadata allows for all disks attached to the same type HBA.
564 * Do not support HBA types mixing
566 if (device
->type
!= hba
->type
)
569 /* Always forbid spanning between VMD domains (seen as different controllers by mdadm) */
570 if (device
->type
== SYS_DEV_VMD
&& !path_attached_to_hba(device
->path
, hba
->path
))
573 /* Multiple same type HBAs can be used if they share the same OROM */
574 const struct imsm_orom
*device_orom
= get_orom_by_device_id(device
->dev_id
);
576 if (device_orom
!= super
->orom
)
582 hba
->next
= alloc_intel_hba(device
);
586 static struct sys_dev
* find_disk_attached_hba(int fd
, const char *devname
)
588 struct sys_dev
*list
, *elem
;
591 if ((list
= find_intel_devices()) == NULL
)
595 disk_path
= (char *) devname
;
597 disk_path
= diskfd_to_devpath(fd
);
602 for (elem
= list
; elem
; elem
= elem
->next
)
603 if (path_attached_to_hba(disk_path
, elem
->path
))
606 if (disk_path
!= devname
)
612 static int find_intel_hba_capability(int fd
, struct intel_super
*super
,
615 static struct supertype
*match_metadata_desc_imsm(char *arg
)
617 struct supertype
*st
;
619 if (strcmp(arg
, "imsm") != 0 &&
620 strcmp(arg
, "default") != 0
624 st
= xcalloc(1, sizeof(*st
));
625 st
->ss
= &super_imsm
;
626 st
->max_devs
= IMSM_MAX_DEVICES
;
627 st
->minor_version
= 0;
633 static __u8
*get_imsm_version(struct imsm_super
*mpb
)
635 return &mpb
->sig
[MPB_SIG_LEN
];
639 /* retrieve a disk directly from the anchor when the anchor is known to be
640 * up-to-date, currently only at load time
642 static struct imsm_disk
*__get_imsm_disk(struct imsm_super
*mpb
, __u8 index
)
644 if (index
>= mpb
->num_disks
)
646 return &mpb
->disk
[index
];
649 /* retrieve the disk description based on a index of the disk
652 static struct dl
*get_imsm_dl_disk(struct intel_super
*super
, __u8 index
)
656 for (d
= super
->disks
; d
; d
= d
->next
)
657 if (d
->index
== index
)
662 /* retrieve a disk from the parsed metadata */
663 static struct imsm_disk
*get_imsm_disk(struct intel_super
*super
, __u8 index
)
667 dl
= get_imsm_dl_disk(super
, index
);
674 /* generate a checksum directly from the anchor when the anchor is known to be
675 * up-to-date, currently only at load or write_super after coalescing
677 static __u32
__gen_imsm_checksum(struct imsm_super
*mpb
)
679 __u32 end
= mpb
->mpb_size
/ sizeof(end
);
680 __u32
*p
= (__u32
*) mpb
;
684 sum
+= __le32_to_cpu(*p
);
688 return sum
- __le32_to_cpu(mpb
->check_sum
);
691 static size_t sizeof_imsm_map(struct imsm_map
*map
)
693 return sizeof(struct imsm_map
) + sizeof(__u32
) * (map
->num_members
- 1);
696 struct imsm_map
*get_imsm_map(struct imsm_dev
*dev
, int second_map
)
698 /* A device can have 2 maps if it is in the middle of a migration.
700 * MAP_0 - we return the first map
701 * MAP_1 - we return the second map if it exists, else NULL
702 * MAP_X - we return the second map if it exists, else the first
704 struct imsm_map
*map
= &dev
->vol
.map
[0];
705 struct imsm_map
*map2
= NULL
;
707 if (dev
->vol
.migr_state
)
708 map2
= (void *)map
+ sizeof_imsm_map(map
);
710 switch (second_map
) {
727 /* return the size of the device.
728 * migr_state increases the returned size if map[0] were to be duplicated
730 static size_t sizeof_imsm_dev(struct imsm_dev
*dev
, int migr_state
)
732 size_t size
= sizeof(*dev
) - sizeof(struct imsm_map
) +
733 sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
735 /* migrating means an additional map */
736 if (dev
->vol
.migr_state
)
737 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_1
));
739 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
745 /* retrieve disk serial number list from a metadata update */
746 static struct disk_info
*get_disk_info(struct imsm_update_create_array
*update
)
749 struct disk_info
*inf
;
751 inf
= u
+ sizeof(*update
) - sizeof(struct imsm_dev
) +
752 sizeof_imsm_dev(&update
->dev
, 0);
758 static struct imsm_dev
*__get_imsm_dev(struct imsm_super
*mpb
, __u8 index
)
764 if (index
>= mpb
->num_raid_devs
)
767 /* devices start after all disks */
768 offset
= ((void *) &mpb
->disk
[mpb
->num_disks
]) - _mpb
;
770 for (i
= 0; i
<= index
; i
++)
772 return _mpb
+ offset
;
774 offset
+= sizeof_imsm_dev(_mpb
+ offset
, 0);
779 static struct imsm_dev
*get_imsm_dev(struct intel_super
*super
, __u8 index
)
781 struct intel_dev
*dv
;
783 if (index
>= super
->anchor
->num_raid_devs
)
785 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
786 if (dv
->index
== index
)
793 * == MAP_0 get first map
794 * == MAP_1 get second map
795 * == MAP_X than get map according to the current migr_state
797 static __u32
get_imsm_ord_tbl_ent(struct imsm_dev
*dev
,
801 struct imsm_map
*map
;
803 map
= get_imsm_map(dev
, second_map
);
805 /* top byte identifies disk under rebuild */
806 return __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
809 #define ord_to_idx(ord) (((ord) << 8) >> 8)
810 static __u32
get_imsm_disk_idx(struct imsm_dev
*dev
, int slot
, int second_map
)
812 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, second_map
);
814 return ord_to_idx(ord
);
817 static void set_imsm_ord_tbl_ent(struct imsm_map
*map
, int slot
, __u32 ord
)
819 map
->disk_ord_tbl
[slot
] = __cpu_to_le32(ord
);
822 static int get_imsm_disk_slot(struct imsm_map
*map
, unsigned idx
)
827 for (slot
= 0; slot
< map
->num_members
; slot
++) {
828 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
829 if (ord_to_idx(ord
) == idx
)
836 static int get_imsm_raid_level(struct imsm_map
*map
)
838 if (map
->raid_level
== 1) {
839 if (map
->num_members
== 2)
845 return map
->raid_level
;
848 static int cmp_extent(const void *av
, const void *bv
)
850 const struct extent
*a
= av
;
851 const struct extent
*b
= bv
;
852 if (a
->start
< b
->start
)
854 if (a
->start
> b
->start
)
859 static int count_memberships(struct dl
*dl
, struct intel_super
*super
)
864 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
865 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
866 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
868 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
875 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
);
877 static int split_ull(unsigned long long n
, __u32
*lo
, __u32
*hi
)
879 if (lo
== 0 || hi
== 0)
881 *lo
= __le32_to_cpu((unsigned)n
);
882 *hi
= __le32_to_cpu((unsigned)(n
>> 32));
886 static unsigned long long join_u32(__u32 lo
, __u32 hi
)
888 return (unsigned long long)__le32_to_cpu(lo
) |
889 (((unsigned long long)__le32_to_cpu(hi
)) << 32);
892 static unsigned long long total_blocks(struct imsm_disk
*disk
)
896 return join_u32(disk
->total_blocks_lo
, disk
->total_blocks_hi
);
899 static unsigned long long pba_of_lba0(struct imsm_map
*map
)
903 return join_u32(map
->pba_of_lba0_lo
, map
->pba_of_lba0_hi
);
906 static unsigned long long blocks_per_member(struct imsm_map
*map
)
910 return join_u32(map
->blocks_per_member_lo
, map
->blocks_per_member_hi
);
914 static unsigned long long num_data_stripes(struct imsm_map
*map
)
918 return join_u32(map
->num_data_stripes_lo
, map
->num_data_stripes_hi
);
921 static void set_total_blocks(struct imsm_disk
*disk
, unsigned long long n
)
923 split_ull(n
, &disk
->total_blocks_lo
, &disk
->total_blocks_hi
);
927 static void set_pba_of_lba0(struct imsm_map
*map
, unsigned long long n
)
929 split_ull(n
, &map
->pba_of_lba0_lo
, &map
->pba_of_lba0_hi
);
932 static void set_blocks_per_member(struct imsm_map
*map
, unsigned long long n
)
934 split_ull(n
, &map
->blocks_per_member_lo
, &map
->blocks_per_member_hi
);
937 static void set_num_data_stripes(struct imsm_map
*map
, unsigned long long n
)
939 split_ull(n
, &map
->num_data_stripes_lo
, &map
->num_data_stripes_hi
);
942 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
)
944 /* find a list of used extents on the given physical device */
945 struct extent
*rv
, *e
;
947 int memberships
= count_memberships(dl
, super
);
950 /* trim the reserved area for spares, so they can join any array
951 * regardless of whether the OROM has assigned sectors from the
952 * IMSM_RESERVED_SECTORS region
955 reservation
= imsm_min_reserved_sectors(super
);
957 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
959 rv
= xcalloc(sizeof(struct extent
), (memberships
+ 1));
962 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
963 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
964 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
966 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
967 e
->start
= pba_of_lba0(map
);
968 e
->size
= blocks_per_member(map
);
972 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
974 /* determine the start of the metadata
975 * when no raid devices are defined use the default
976 * ...otherwise allow the metadata to truncate the value
977 * as is the case with older versions of imsm
980 struct extent
*last
= &rv
[memberships
- 1];
981 unsigned long long remainder
;
983 remainder
= total_blocks(&dl
->disk
) - (last
->start
+ last
->size
);
984 /* round down to 1k block to satisfy precision of the kernel
988 /* make sure remainder is still sane */
989 if (remainder
< (unsigned)ROUND_UP(super
->len
, 512) >> 9)
990 remainder
= ROUND_UP(super
->len
, 512) >> 9;
991 if (reservation
> remainder
)
992 reservation
= remainder
;
994 e
->start
= total_blocks(&dl
->disk
) - reservation
;
999 /* try to determine how much space is reserved for metadata from
1000 * the last get_extents() entry, otherwise fallback to the
1003 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
1009 /* for spares just return a minimal reservation which will grow
1010 * once the spare is picked up by an array
1012 if (dl
->index
== -1)
1013 return MPB_SECTOR_CNT
;
1015 e
= get_extents(super
, dl
);
1017 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1019 /* scroll to last entry */
1020 for (i
= 0; e
[i
].size
; i
++)
1023 rv
= total_blocks(&dl
->disk
) - e
[i
].start
;
1030 static int is_spare(struct imsm_disk
*disk
)
1032 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
1035 static int is_configured(struct imsm_disk
*disk
)
1037 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
1040 static int is_failed(struct imsm_disk
*disk
)
1042 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
1045 /* try to determine how much space is reserved for metadata from
1046 * the last get_extents() entry on the smallest active disk,
1047 * otherwise fallback to the default
1049 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
)
1053 unsigned long long min_active
;
1055 __u32 rv
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1056 struct dl
*dl
, *dl_min
= NULL
;
1062 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
1065 unsigned long long blocks
= total_blocks(&dl
->disk
);
1066 if (blocks
< min_active
|| min_active
== 0) {
1068 min_active
= blocks
;
1074 /* find last lba used by subarrays on the smallest active disk */
1075 e
= get_extents(super
, dl_min
);
1078 for (i
= 0; e
[i
].size
; i
++)
1081 remainder
= min_active
- e
[i
].start
;
1084 /* to give priority to recovery we should not require full
1085 IMSM_RESERVED_SECTORS from the spare */
1086 rv
= MPB_SECTOR_CNT
+ NUM_BLOCKS_DIRTY_STRIPE_REGION
;
1088 /* if real reservation is smaller use that value */
1089 return (remainder
< rv
) ? remainder
: rv
;
1092 /* Return minimum size of a spare that can be used in this array*/
1093 static unsigned long long min_acceptable_spare_size_imsm(struct supertype
*st
)
1095 struct intel_super
*super
= st
->sb
;
1099 unsigned long long rv
= 0;
1103 /* find first active disk in array */
1105 while (dl
&& (is_failed(&dl
->disk
) || dl
->index
== -1))
1109 /* find last lba used by subarrays */
1110 e
= get_extents(super
, dl
);
1113 for (i
= 0; e
[i
].size
; i
++)
1116 rv
= e
[i
-1].start
+ e
[i
-1].size
;
1119 /* add the amount of space needed for metadata */
1120 rv
= rv
+ imsm_min_reserved_sectors(super
);
1125 static int is_gen_migration(struct imsm_dev
*dev
);
1128 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
1129 struct imsm_dev
*dev
);
1131 static void print_imsm_dev(struct intel_super
*super
,
1132 struct imsm_dev
*dev
,
1138 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1139 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
1143 printf("[%.16s]:\n", dev
->volume
);
1144 printf(" UUID : %s\n", uuid
);
1145 printf(" RAID Level : %d", get_imsm_raid_level(map
));
1147 printf(" <-- %d", get_imsm_raid_level(map2
));
1149 printf(" Members : %d", map
->num_members
);
1151 printf(" <-- %d", map2
->num_members
);
1153 printf(" Slots : [");
1154 for (i
= 0; i
< map
->num_members
; i
++) {
1155 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
1156 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1161 for (i
= 0; i
< map2
->num_members
; i
++) {
1162 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_1
);
1163 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1168 printf(" Failed disk : ");
1169 if (map
->failed_disk_num
== 0xff)
1172 printf("%i", map
->failed_disk_num
);
1174 slot
= get_imsm_disk_slot(map
, disk_idx
);
1176 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
1177 printf(" This Slot : %d%s\n", slot
,
1178 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
1180 printf(" This Slot : ?\n");
1181 sz
= __le32_to_cpu(dev
->size_high
);
1183 sz
+= __le32_to_cpu(dev
->size_low
);
1184 printf(" Array Size : %llu%s\n", (unsigned long long)sz
,
1185 human_size(sz
* 512));
1186 sz
= blocks_per_member(map
);
1187 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz
,
1188 human_size(sz
* 512));
1189 printf(" Sector Offset : %llu\n",
1191 printf(" Num Stripes : %llu\n",
1192 num_data_stripes(map
));
1193 printf(" Chunk Size : %u KiB",
1194 __le16_to_cpu(map
->blocks_per_strip
) / 2);
1196 printf(" <-- %u KiB",
1197 __le16_to_cpu(map2
->blocks_per_strip
) / 2);
1199 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
1200 printf(" Migrate State : ");
1201 if (dev
->vol
.migr_state
) {
1202 if (migr_type(dev
) == MIGR_INIT
)
1203 printf("initialize\n");
1204 else if (migr_type(dev
) == MIGR_REBUILD
)
1205 printf("rebuild\n");
1206 else if (migr_type(dev
) == MIGR_VERIFY
)
1208 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
1209 printf("general migration\n");
1210 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
1211 printf("state change\n");
1212 else if (migr_type(dev
) == MIGR_REPAIR
)
1215 printf("<unknown:%d>\n", migr_type(dev
));
1218 printf(" Map State : %s", map_state_str
[map
->map_state
]);
1219 if (dev
->vol
.migr_state
) {
1220 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1222 printf(" <-- %s", map_state_str
[map
->map_state
]);
1223 printf("\n Checkpoint : %u ",
1224 __le32_to_cpu(dev
->vol
.curr_migr_unit
));
1225 if ((is_gen_migration(dev
)) && ((slot
> 1) || (slot
< 0)))
1228 printf("(%llu)", (unsigned long long)
1229 blocks_per_migr_unit(super
, dev
));
1232 printf(" Dirty State : %s\n", dev
->vol
.dirty
? "dirty" : "clean");
1235 static void print_imsm_disk(struct imsm_disk
*disk
, int index
, __u32 reserved
)
1237 char str
[MAX_RAID_SERIAL_LEN
+ 1];
1240 if (index
< -1 || !disk
)
1244 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
1246 printf(" Disk%02d Serial : %s\n", index
, str
);
1248 printf(" Disk Serial : %s\n", str
);
1249 printf(" State :%s%s%s\n", is_spare(disk
) ? " spare" : "",
1250 is_configured(disk
) ? " active" : "",
1251 is_failed(disk
) ? " failed" : "");
1252 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
1253 sz
= total_blocks(disk
) - reserved
;
1254 printf(" Usable Size : %llu%s\n", (unsigned long long)sz
,
1255 human_size(sz
* 512));
1258 void examine_migr_rec_imsm(struct intel_super
*super
)
1260 struct migr_record
*migr_rec
= super
->migr_rec
;
1261 struct imsm_super
*mpb
= super
->anchor
;
1264 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1265 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1266 struct imsm_map
*map
;
1269 if (is_gen_migration(dev
) == 0)
1272 printf("\nMigration Record Information:");
1274 /* first map under migration */
1275 map
= get_imsm_map(dev
, MAP_0
);
1277 slot
= get_imsm_disk_slot(map
, super
->disks
->index
);
1278 if ((map
== NULL
) || (slot
> 1) || (slot
< 0)) {
1279 printf(" Empty\n ");
1280 printf("Examine one of first two disks in array\n");
1283 printf("\n Status : ");
1284 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
1287 printf("Contains Data\n");
1288 printf(" Current Unit : %u\n",
1289 __le32_to_cpu(migr_rec
->curr_migr_unit
));
1290 printf(" Family : %u\n",
1291 __le32_to_cpu(migr_rec
->family_num
));
1292 printf(" Ascending : %u\n",
1293 __le32_to_cpu(migr_rec
->ascending_migr
));
1294 printf(" Blocks Per Unit : %u\n",
1295 __le32_to_cpu(migr_rec
->blocks_per_unit
));
1296 printf(" Dest. Depth Per Unit : %u\n",
1297 __le32_to_cpu(migr_rec
->dest_depth_per_unit
));
1298 printf(" Checkpoint Area pba : %u\n",
1299 __le32_to_cpu(migr_rec
->ckpt_area_pba
));
1300 printf(" First member lba : %u\n",
1301 __le32_to_cpu(migr_rec
->dest_1st_member_lba
));
1302 printf(" Total Number of Units : %u\n",
1303 __le32_to_cpu(migr_rec
->num_migr_units
));
1304 printf(" Size of volume : %u\n",
1305 __le32_to_cpu(migr_rec
->post_migr_vol_cap
));
1306 printf(" Expansion space for LBA64 : %u\n",
1307 __le32_to_cpu(migr_rec
->post_migr_vol_cap_hi
));
1308 printf(" Record was read from : %u\n",
1309 __le32_to_cpu(migr_rec
->ckpt_read_disk_num
));
1314 #endif /* MDASSEMBLE */
1315 /*******************************************************************************
1316 * function: imsm_check_attributes
1317 * Description: Function checks if features represented by attributes flags
1318 * are supported by mdadm.
1320 * attributes - Attributes read from metadata
1322 * 0 - passed attributes contains unsupported features flags
1323 * 1 - all features are supported
1324 ******************************************************************************/
1325 static int imsm_check_attributes(__u32 attributes
)
1328 __u32 not_supported
= MPB_ATTRIB_SUPPORTED
^0xffffffff;
1330 not_supported
&= ~MPB_ATTRIB_IGNORED
;
1332 not_supported
&= attributes
;
1333 if (not_supported
) {
1334 pr_err("(IMSM): Unsupported attributes : %x\n",
1335 (unsigned)__le32_to_cpu(not_supported
));
1336 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1337 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
1338 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1340 if (not_supported
& MPB_ATTRIB_2TB
) {
1341 dprintf("\t\tMPB_ATTRIB_2TB\n");
1342 not_supported
^= MPB_ATTRIB_2TB
;
1344 if (not_supported
& MPB_ATTRIB_RAID0
) {
1345 dprintf("\t\tMPB_ATTRIB_RAID0\n");
1346 not_supported
^= MPB_ATTRIB_RAID0
;
1348 if (not_supported
& MPB_ATTRIB_RAID1
) {
1349 dprintf("\t\tMPB_ATTRIB_RAID1\n");
1350 not_supported
^= MPB_ATTRIB_RAID1
;
1352 if (not_supported
& MPB_ATTRIB_RAID10
) {
1353 dprintf("\t\tMPB_ATTRIB_RAID10\n");
1354 not_supported
^= MPB_ATTRIB_RAID10
;
1356 if (not_supported
& MPB_ATTRIB_RAID1E
) {
1357 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
1358 not_supported
^= MPB_ATTRIB_RAID1E
;
1360 if (not_supported
& MPB_ATTRIB_RAID5
) {
1361 dprintf("\t\tMPB_ATTRIB_RAID5\n");
1362 not_supported
^= MPB_ATTRIB_RAID5
;
1364 if (not_supported
& MPB_ATTRIB_RAIDCNG
) {
1365 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
1366 not_supported
^= MPB_ATTRIB_RAIDCNG
;
1368 if (not_supported
& MPB_ATTRIB_BBM
) {
1369 dprintf("\t\tMPB_ATTRIB_BBM\n");
1370 not_supported
^= MPB_ATTRIB_BBM
;
1372 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1373 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
1374 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1376 if (not_supported
& MPB_ATTRIB_EXP_STRIPE_SIZE
) {
1377 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
1378 not_supported
^= MPB_ATTRIB_EXP_STRIPE_SIZE
;
1380 if (not_supported
& MPB_ATTRIB_2TB_DISK
) {
1381 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
1382 not_supported
^= MPB_ATTRIB_2TB_DISK
;
1384 if (not_supported
& MPB_ATTRIB_NEVER_USE2
) {
1385 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
1386 not_supported
^= MPB_ATTRIB_NEVER_USE2
;
1388 if (not_supported
& MPB_ATTRIB_NEVER_USE
) {
1389 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
1390 not_supported
^= MPB_ATTRIB_NEVER_USE
;
1394 dprintf("(IMSM): Unknown attributes : %x\n", not_supported
);
1403 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
1405 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
1407 struct intel_super
*super
= st
->sb
;
1408 struct imsm_super
*mpb
= super
->anchor
;
1409 char str
[MAX_SIGNATURE_LENGTH
];
1414 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
1417 snprintf(str
, MPB_SIG_LEN
, "%s", mpb
->sig
);
1418 printf(" Magic : %s\n", str
);
1419 snprintf(str
, strlen(MPB_VERSION_RAID0
), "%s", get_imsm_version(mpb
));
1420 printf(" Version : %s\n", get_imsm_version(mpb
));
1421 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
1422 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
1423 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
1424 printf(" Attributes : ");
1425 if (imsm_check_attributes(mpb
->attributes
))
1426 printf("All supported\n");
1428 printf("not supported\n");
1429 getinfo_super_imsm(st
, &info
, NULL
);
1430 fname_from_uuid(st
, &info
, nbuf
, ':');
1431 printf(" UUID : %s\n", nbuf
+ 5);
1432 sum
= __le32_to_cpu(mpb
->check_sum
);
1433 printf(" Checksum : %08x %s\n", sum
,
1434 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
1435 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
));
1436 printf(" Disks : %d\n", mpb
->num_disks
);
1437 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
1438 print_imsm_disk(__get_imsm_disk(mpb
, super
->disks
->index
), super
->disks
->index
, reserved
);
1439 if (super
->bbm_log
) {
1440 struct bbm_log
*log
= super
->bbm_log
;
1443 printf("Bad Block Management Log:\n");
1444 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
1445 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
1446 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
1447 printf(" Spare Blocks : %d\n", __le32_to_cpu(log
->reserved_spare_block_count
));
1448 printf(" First Spare : %llx\n",
1449 (unsigned long long) __le64_to_cpu(log
->first_spare_lba
));
1451 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1453 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1455 super
->current_vol
= i
;
1456 getinfo_super_imsm(st
, &info
, NULL
);
1457 fname_from_uuid(st
, &info
, nbuf
, ':');
1458 print_imsm_dev(super
, dev
, nbuf
+ 5, super
->disks
->index
);
1460 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1461 if (i
== super
->disks
->index
)
1463 print_imsm_disk(__get_imsm_disk(mpb
, i
), i
, reserved
);
1466 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
1467 if (dl
->index
== -1)
1468 print_imsm_disk(&dl
->disk
, -1, reserved
);
1470 examine_migr_rec_imsm(super
);
1473 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
1475 /* We just write a generic IMSM ARRAY entry */
1478 struct intel_super
*super
= st
->sb
;
1480 if (!super
->anchor
->num_raid_devs
) {
1481 printf("ARRAY metadata=imsm\n");
1485 getinfo_super_imsm(st
, &info
, NULL
);
1486 fname_from_uuid(st
, &info
, nbuf
, ':');
1487 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
1490 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
1492 /* We just write a generic IMSM ARRAY entry */
1496 struct intel_super
*super
= st
->sb
;
1499 if (!super
->anchor
->num_raid_devs
)
1502 getinfo_super_imsm(st
, &info
, NULL
);
1503 fname_from_uuid(st
, &info
, nbuf
, ':');
1504 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1505 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1507 super
->current_vol
= i
;
1508 getinfo_super_imsm(st
, &info
, NULL
);
1509 fname_from_uuid(st
, &info
, nbuf1
, ':');
1510 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
1511 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
1515 static void export_examine_super_imsm(struct supertype
*st
)
1517 struct intel_super
*super
= st
->sb
;
1518 struct imsm_super
*mpb
= super
->anchor
;
1522 getinfo_super_imsm(st
, &info
, NULL
);
1523 fname_from_uuid(st
, &info
, nbuf
, ':');
1524 printf("MD_METADATA=imsm\n");
1525 printf("MD_LEVEL=container\n");
1526 printf("MD_UUID=%s\n", nbuf
+5);
1527 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
1530 static int copy_metadata_imsm(struct supertype
*st
, int from
, int to
)
1532 /* The second last 512byte sector of the device contains
1533 * the "struct imsm_super" metadata.
1534 * This contains mpb_size which is the size in bytes of the
1535 * extended metadata. This is located immediately before
1537 * We want to read all that, plus the last sector which
1538 * may contain a migration record, and write it all
1542 unsigned long long dsize
, offset
;
1544 struct imsm_super
*sb
;
1547 if (posix_memalign(&buf
, 4096, 4096) != 0)
1550 if (!get_dev_size(from
, NULL
, &dsize
))
1553 if (lseek64(from
, dsize
-1024, 0) < 0)
1555 if (read(from
, buf
, 512) != 512)
1558 if (strncmp((char*)sb
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0)
1561 sectors
= mpb_sectors(sb
) + 2;
1562 offset
= dsize
- sectors
* 512;
1563 if (lseek64(from
, offset
, 0) < 0 ||
1564 lseek64(to
, offset
, 0) < 0)
1566 while (written
< sectors
* 512) {
1567 int n
= sectors
*512 - written
;
1570 if (read(from
, buf
, n
) != n
)
1572 if (write(to
, buf
, n
) != n
)
1583 static void detail_super_imsm(struct supertype
*st
, char *homehost
)
1588 getinfo_super_imsm(st
, &info
, NULL
);
1589 fname_from_uuid(st
, &info
, nbuf
, ':');
1590 printf("\n UUID : %s\n", nbuf
+ 5);
1593 static void brief_detail_super_imsm(struct supertype
*st
)
1597 getinfo_super_imsm(st
, &info
, NULL
);
1598 fname_from_uuid(st
, &info
, nbuf
, ':');
1599 printf(" UUID=%s", nbuf
+ 5);
1602 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
);
1603 static void fd2devname(int fd
, char *name
);
1605 static int ahci_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
1607 /* dump an unsorted list of devices attached to AHCI Intel storage
1608 * controller, as well as non-connected ports
1610 int hba_len
= strlen(hba_path
) + 1;
1615 unsigned long port_mask
= (1 << port_count
) - 1;
1617 if (port_count
> (int)sizeof(port_mask
) * 8) {
1619 pr_err("port_count %d out of range\n", port_count
);
1623 /* scroll through /sys/dev/block looking for devices attached to
1626 dir
= opendir("/sys/dev/block");
1627 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
1638 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
1640 path
= devt_to_devpath(makedev(major
, minor
));
1643 if (!path_attached_to_hba(path
, hba_path
)) {
1649 /* retrieve the scsi device type */
1650 if (asprintf(&device
, "/sys/dev/block/%d:%d/device/xxxxxxx", major
, minor
) < 0) {
1652 pr_err("failed to allocate 'device'\n");
1656 sprintf(device
, "/sys/dev/block/%d:%d/device/type", major
, minor
);
1657 if (load_sys(device
, buf
, sizeof(buf
)) != 0) {
1659 pr_err("failed to read device type for %s\n",
1665 type
= strtoul(buf
, NULL
, 10);
1667 /* if it's not a disk print the vendor and model */
1668 if (!(type
== 0 || type
== 7 || type
== 14)) {
1671 sprintf(device
, "/sys/dev/block/%d:%d/device/vendor", major
, minor
);
1672 if (load_sys(device
, buf
, sizeof(buf
)) == 0) {
1673 strncpy(vendor
, buf
, sizeof(vendor
));
1674 vendor
[sizeof(vendor
) - 1] = '\0';
1675 c
= (char *) &vendor
[sizeof(vendor
) - 1];
1676 while (isspace(*c
) || *c
== '\0')
1680 sprintf(device
, "/sys/dev/block/%d:%d/device/model", major
, minor
);
1681 if (load_sys(device
, buf
, sizeof(buf
)) == 0) {
1682 strncpy(model
, buf
, sizeof(model
));
1683 model
[sizeof(model
) - 1] = '\0';
1684 c
= (char *) &model
[sizeof(model
) - 1];
1685 while (isspace(*c
) || *c
== '\0')
1689 if (vendor
[0] && model
[0])
1690 sprintf(buf
, "%.64s %.64s", vendor
, model
);
1692 switch (type
) { /* numbers from hald/linux/device.c */
1693 case 1: sprintf(buf
, "tape"); break;
1694 case 2: sprintf(buf
, "printer"); break;
1695 case 3: sprintf(buf
, "processor"); break;
1697 case 5: sprintf(buf
, "cdrom"); break;
1698 case 6: sprintf(buf
, "scanner"); break;
1699 case 8: sprintf(buf
, "media_changer"); break;
1700 case 9: sprintf(buf
, "comm"); break;
1701 case 12: sprintf(buf
, "raid"); break;
1702 default: sprintf(buf
, "unknown");
1708 /* chop device path to 'host%d' and calculate the port number */
1709 c
= strchr(&path
[hba_len
], '/');
1712 pr_err("%s - invalid path name\n", path
+ hba_len
);
1717 if ((sscanf(&path
[hba_len
], "ata%d", &port
) == 1) ||
1718 ((sscanf(&path
[hba_len
], "host%d", &port
) == 1)))
1722 *c
= '/'; /* repair the full string */
1723 pr_err("failed to determine port number for %s\n",
1730 /* mark this port as used */
1731 port_mask
&= ~(1 << port
);
1733 /* print out the device information */
1735 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
1739 fd
= dev_open(ent
->d_name
, O_RDONLY
);
1741 printf(" Port%d : - disk info unavailable -\n", port
);
1743 fd2devname(fd
, buf
);
1744 printf(" Port%d : %s", port
, buf
);
1745 if (imsm_read_serial(fd
, NULL
, (__u8
*) buf
) == 0)
1746 printf(" (%.*s)\n", MAX_RAID_SERIAL_LEN
, buf
);
1761 for (i
= 0; i
< port_count
; i
++)
1762 if (port_mask
& (1 << i
))
1763 printf(" Port%d : - no device attached -\n", i
);
1769 static int print_vmd_attached_devs(struct sys_dev
*hba
)
1777 if (hba
->type
!= SYS_DEV_VMD
)
1780 /* scroll through /sys/dev/block looking for devices attached to
1783 dir
= opendir("/sys/bus/pci/drivers/nvme");
1787 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
1790 /* is 'ent' a device? check that the 'subsystem' link exists and
1791 * that its target matches 'bus'
1793 sprintf(path
, "/sys/bus/pci/drivers/nvme/%s/subsystem",
1795 n
= readlink(path
, link
, sizeof(link
));
1796 if (n
< 0 || n
>= (int)sizeof(link
))
1799 c
= strrchr(link
, '/');
1802 if (strncmp("pci", c
+1, strlen("pci")) != 0)
1805 sprintf(path
, "/sys/bus/pci/drivers/nvme/%s", ent
->d_name
);
1806 /* if not a intel NVMe - skip it*/
1807 if (devpath_to_vendor(path
) != 0x8086)
1810 rp
= realpath(path
, NULL
);
1814 if (path_attached_to_hba(rp
, hba
->path
)) {
1815 printf(" NVMe under VMD : %s\n", rp
);
1824 static void print_found_intel_controllers(struct sys_dev
*elem
)
1826 for (; elem
; elem
= elem
->next
) {
1827 pr_err("found Intel(R) ");
1828 if (elem
->type
== SYS_DEV_SATA
)
1829 fprintf(stderr
, "SATA ");
1830 else if (elem
->type
== SYS_DEV_SAS
)
1831 fprintf(stderr
, "SAS ");
1832 else if (elem
->type
== SYS_DEV_NVME
)
1833 fprintf(stderr
, "NVMe ");
1835 if (elem
->type
== SYS_DEV_VMD
)
1836 fprintf(stderr
, "VMD domain");
1838 fprintf(stderr
, "RAID controller");
1841 fprintf(stderr
, " at %s", elem
->pci_id
);
1842 fprintf(stderr
, ".\n");
1847 static int ahci_get_port_count(const char *hba_path
, int *port_count
)
1854 if ((dir
= opendir(hba_path
)) == NULL
)
1857 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
1860 if ((sscanf(ent
->d_name
, "ata%d", &host
) != 1) &&
1861 ((sscanf(ent
->d_name
, "host%d", &host
) != 1)))
1863 if (*port_count
== 0)
1865 else if (host
< host_base
)
1868 if (host
+ 1 > *port_count
+ host_base
)
1869 *port_count
= host
+ 1 - host_base
;
1875 static void print_imsm_capability(const struct imsm_orom
*orom
)
1877 printf(" Platform : Intel(R) ");
1878 if (orom
->capabilities
== 0 && orom
->driver_features
== 0)
1879 printf("Matrix Storage Manager\n");
1881 printf("Rapid Storage Technology%s\n",
1882 imsm_orom_is_enterprise(orom
) ? " enterprise" : "");
1883 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
)
1884 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
,
1885 orom
->minor_ver
, orom
->hotfix_ver
, orom
->build
);
1886 printf(" RAID Levels :%s%s%s%s%s\n",
1887 imsm_orom_has_raid0(orom
) ? " raid0" : "",
1888 imsm_orom_has_raid1(orom
) ? " raid1" : "",
1889 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
1890 imsm_orom_has_raid10(orom
) ? " raid10" : "",
1891 imsm_orom_has_raid5(orom
) ? " raid5" : "");
1892 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1893 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
1894 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
1895 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
1896 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
1897 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
1898 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
1899 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
1900 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
1901 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
1902 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
1903 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
1904 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
1905 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
1906 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
1907 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
1908 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
1909 printf(" 2TB volumes :%s supported\n",
1910 (orom
->attr
& IMSM_OROM_ATTR_2TB
)?"":" not");
1911 printf(" 2TB disks :%s supported\n",
1912 (orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
)?"":" not");
1913 printf(" Max Disks : %d\n", orom
->tds
);
1914 printf(" Max Volumes : %d per array, %d per %s\n",
1915 orom
->vpa
, orom
->vphba
,
1916 imsm_orom_is_nvme(orom
) ? "platform" : "controller");
1920 static void print_imsm_capability_export(const struct imsm_orom
*orom
)
1922 printf("MD_FIRMWARE_TYPE=imsm\n");
1923 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
)
1924 printf("IMSM_VERSION=%d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
1925 orom
->hotfix_ver
, orom
->build
);
1926 printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n",
1927 imsm_orom_has_raid0(orom
) ? "raid0 " : "",
1928 imsm_orom_has_raid1(orom
) ? "raid1 " : "",
1929 imsm_orom_has_raid1e(orom
) ? "raid1e " : "",
1930 imsm_orom_has_raid5(orom
) ? "raid10 " : "",
1931 imsm_orom_has_raid10(orom
) ? "raid5 " : "");
1932 printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1933 imsm_orom_has_chunk(orom
, 2) ? "2k " : "",
1934 imsm_orom_has_chunk(orom
, 4) ? "4k " : "",
1935 imsm_orom_has_chunk(orom
, 8) ? "8k " : "",
1936 imsm_orom_has_chunk(orom
, 16) ? "16k " : "",
1937 imsm_orom_has_chunk(orom
, 32) ? "32k " : "",
1938 imsm_orom_has_chunk(orom
, 64) ? "64k " : "",
1939 imsm_orom_has_chunk(orom
, 128) ? "128k " : "",
1940 imsm_orom_has_chunk(orom
, 256) ? "256k " : "",
1941 imsm_orom_has_chunk(orom
, 512) ? "512k " : "",
1942 imsm_orom_has_chunk(orom
, 1024*1) ? "1M " : "",
1943 imsm_orom_has_chunk(orom
, 1024*2) ? "2M " : "",
1944 imsm_orom_has_chunk(orom
, 1024*4) ? "4M " : "",
1945 imsm_orom_has_chunk(orom
, 1024*8) ? "8M " : "",
1946 imsm_orom_has_chunk(orom
, 1024*16) ? "16M " : "",
1947 imsm_orom_has_chunk(orom
, 1024*32) ? "32M " : "",
1948 imsm_orom_has_chunk(orom
, 1024*64) ? "64M " : "");
1949 printf("IMSM_2TB_VOLUMES=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB
) ? "yes" : "no");
1950 printf("IMSM_2TB_DISKS=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) ? "yes" : "no");
1951 printf("IMSM_MAX_DISKS=%d\n",orom
->tds
);
1952 printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom
->vpa
);
1953 printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom
->vphba
);
1956 static int detail_platform_imsm(int verbose
, int enumerate_only
, char *controller_path
)
1958 /* There are two components to imsm platform support, the ahci SATA
1959 * controller and the option-rom. To find the SATA controller we
1960 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
1961 * controller with the Intel vendor id is present. This approach
1962 * allows mdadm to leverage the kernel's ahci detection logic, with the
1963 * caveat that if ahci.ko is not loaded mdadm will not be able to
1964 * detect platform raid capabilities. The option-rom resides in a
1965 * platform "Adapter ROM". We scan for its signature to retrieve the
1966 * platform capabilities. If raid support is disabled in the BIOS the
1967 * option-rom capability structure will not be available.
1969 struct sys_dev
*list
, *hba
;
1974 if (enumerate_only
) {
1975 if (check_env("IMSM_NO_PLATFORM"))
1977 list
= find_intel_devices();
1980 for (hba
= list
; hba
; hba
= hba
->next
) {
1981 if (find_imsm_capability(hba
)) {
1991 list
= find_intel_devices();
1994 pr_err("no active Intel(R) RAID controller found.\n");
1996 } else if (verbose
> 0)
1997 print_found_intel_controllers(list
);
1999 for (hba
= list
; hba
; hba
= hba
->next
) {
2000 if (controller_path
&& (compare_paths(hba
->path
, controller_path
) != 0))
2002 if (!find_imsm_capability(hba
)) {
2004 pr_err("imsm capabilities not found for controller: %s (type %s)\n",
2005 hba
->type
== SYS_DEV_VMD
? vmd_domain_to_controller(hba
, buf
) : hba
->path
,
2006 get_sys_dev_type(hba
->type
));
2012 if (controller_path
&& result
== 1) {
2013 pr_err("no active Intel(R) RAID controller found under %s\n",
2018 const struct orom_entry
*entry
;
2020 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2021 if (entry
->type
== SYS_DEV_VMD
) {
2022 for (hba
= list
; hba
; hba
= hba
->next
) {
2023 if (hba
->type
== SYS_DEV_VMD
) {
2025 print_imsm_capability(&entry
->orom
);
2026 printf(" I/O Controller : %s (%s)\n",
2027 vmd_domain_to_controller(hba
, buf
), get_sys_dev_type(hba
->type
));
2028 if (print_vmd_attached_devs(hba
)) {
2030 pr_err("failed to get devices attached to VMD domain.\n");
2039 print_imsm_capability(&entry
->orom
);
2040 if (entry
->type
== SYS_DEV_NVME
) {
2041 for (hba
= list
; hba
; hba
= hba
->next
) {
2042 if (hba
->type
== SYS_DEV_NVME
)
2043 printf(" NVMe Device : %s\n", hba
->path
);
2049 struct devid_list
*devid
;
2050 for (devid
= entry
->devid_list
; devid
; devid
= devid
->next
) {
2051 hba
= device_by_id(devid
->devid
);
2055 printf(" I/O Controller : %s (%s)\n",
2056 hba
->path
, get_sys_dev_type(hba
->type
));
2057 if (hba
->type
== SYS_DEV_SATA
) {
2058 host_base
= ahci_get_port_count(hba
->path
, &port_count
);
2059 if (ahci_enumerate_ports(hba
->path
, port_count
, host_base
, verbose
)) {
2061 pr_err("failed to enumerate ports on SATA controller at %s.\n", hba
->pci_id
);
2072 static int export_detail_platform_imsm(int verbose
, char *controller_path
)
2074 struct sys_dev
*list
, *hba
;
2077 list
= find_intel_devices();
2080 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n");
2085 for (hba
= list
; hba
; hba
= hba
->next
) {
2086 if (controller_path
&& (compare_paths(hba
->path
,controller_path
) != 0))
2088 if (!find_imsm_capability(hba
) && verbose
> 0) {
2090 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n",
2091 hba
->type
== SYS_DEV_VMD
? vmd_domain_to_controller(hba
, buf
) : hba
->path
);
2097 const struct orom_entry
*entry
;
2099 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2100 if (entry
->type
== SYS_DEV_VMD
) {
2101 for (hba
= list
; hba
; hba
= hba
->next
)
2102 print_imsm_capability_export(&entry
->orom
);
2105 print_imsm_capability_export(&entry
->orom
);
2113 static int match_home_imsm(struct supertype
*st
, char *homehost
)
2115 /* the imsm metadata format does not specify any host
2116 * identification information. We return -1 since we can never
2117 * confirm nor deny whether a given array is "meant" for this
2118 * host. We rely on compare_super and the 'family_num' fields to
2119 * exclude member disks that do not belong, and we rely on
2120 * mdadm.conf to specify the arrays that should be assembled.
2121 * Auto-assembly may still pick up "foreign" arrays.
2127 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
2129 /* The uuid returned here is used for:
2130 * uuid to put into bitmap file (Create, Grow)
2131 * uuid for backup header when saving critical section (Grow)
2132 * comparing uuids when re-adding a device into an array
2133 * In these cases the uuid required is that of the data-array,
2134 * not the device-set.
2135 * uuid to recognise same set when adding a missing device back
2136 * to an array. This is a uuid for the device-set.
2138 * For each of these we can make do with a truncated
2139 * or hashed uuid rather than the original, as long as
2141 * In each case the uuid required is that of the data-array,
2142 * not the device-set.
2144 /* imsm does not track uuid's so we synthesis one using sha1 on
2145 * - The signature (Which is constant for all imsm array, but no matter)
2146 * - the orig_family_num of the container
2147 * - the index number of the volume
2148 * - the 'serial' number of the volume.
2149 * Hopefully these are all constant.
2151 struct intel_super
*super
= st
->sb
;
2154 struct sha1_ctx ctx
;
2155 struct imsm_dev
*dev
= NULL
;
2158 /* some mdadm versions failed to set ->orig_family_num, in which
2159 * case fall back to ->family_num. orig_family_num will be
2160 * fixed up with the first metadata update.
2162 family_num
= super
->anchor
->orig_family_num
;
2163 if (family_num
== 0)
2164 family_num
= super
->anchor
->family_num
;
2165 sha1_init_ctx(&ctx
);
2166 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
2167 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
2168 if (super
->current_vol
>= 0)
2169 dev
= get_imsm_dev(super
, super
->current_vol
);
2171 __u32 vol
= super
->current_vol
;
2172 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
2173 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
2175 sha1_finish_ctx(&ctx
, buf
);
2176 memcpy(uuid
, buf
, 4*4);
2181 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
2183 __u8
*v
= get_imsm_version(mpb
);
2184 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
2185 char major
[] = { 0, 0, 0 };
2186 char minor
[] = { 0 ,0, 0 };
2187 char patch
[] = { 0, 0, 0 };
2188 char *ver_parse
[] = { major
, minor
, patch
};
2192 while (*v
!= '\0' && v
< end
) {
2193 if (*v
!= '.' && j
< 2)
2194 ver_parse
[i
][j
++] = *v
;
2202 *m
= strtol(minor
, NULL
, 0);
2203 *p
= strtol(patch
, NULL
, 0);
2207 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
2209 /* migr_strip_size when repairing or initializing parity */
2210 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2211 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2213 switch (get_imsm_raid_level(map
)) {
2218 return 128*1024 >> 9;
2222 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
2224 /* migr_strip_size when rebuilding a degraded disk, no idea why
2225 * this is different than migr_strip_size_resync(), but it's good
2228 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2229 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2231 switch (get_imsm_raid_level(map
)) {
2234 if (map
->num_members
% map
->num_domains
== 0)
2235 return 128*1024 >> 9;
2239 return max((__u32
) 64*1024 >> 9, chunk
);
2241 return 128*1024 >> 9;
2245 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
2247 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2248 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2249 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
2250 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
2252 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
2255 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
2257 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2258 int level
= get_imsm_raid_level(lo
);
2260 if (level
== 1 || level
== 10) {
2261 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2263 return hi
->num_domains
;
2265 return num_stripes_per_unit_resync(dev
);
2268 static __u8
imsm_num_data_members(struct imsm_dev
*dev
, int second_map
)
2270 /* named 'imsm_' because raid0, raid1 and raid10
2271 * counter-intuitively have the same number of data disks
2273 struct imsm_map
*map
= get_imsm_map(dev
, second_map
);
2275 switch (get_imsm_raid_level(map
)) {
2277 return map
->num_members
;
2281 return map
->num_members
/2;
2283 return map
->num_members
- 1;
2285 dprintf("unsupported raid level\n");
2290 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
2292 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2293 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2295 switch(get_imsm_raid_level(map
)) {
2298 return chunk
* map
->num_domains
;
2300 return chunk
* map
->num_members
;
2306 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
2308 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2309 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2310 __u32 strip
= block
/ chunk
;
2312 switch (get_imsm_raid_level(map
)) {
2315 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
2316 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
2318 return vol_stripe
* chunk
+ block
% chunk
;
2320 __u32 stripe
= strip
/ (map
->num_members
- 1);
2322 return stripe
* chunk
+ block
% chunk
;
2329 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
2330 struct imsm_dev
*dev
)
2332 /* calculate the conversion factor between per member 'blocks'
2333 * (md/{resync,rebuild}_start) and imsm migration units, return
2334 * 0 for the 'not migrating' and 'unsupported migration' cases
2336 if (!dev
->vol
.migr_state
)
2339 switch (migr_type(dev
)) {
2340 case MIGR_GEN_MIGR
: {
2341 struct migr_record
*migr_rec
= super
->migr_rec
;
2342 return __le32_to_cpu(migr_rec
->blocks_per_unit
);
2347 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2348 __u32 stripes_per_unit
;
2349 __u32 blocks_per_unit
;
2358 /* yes, this is really the translation of migr_units to
2359 * per-member blocks in the 'resync' case
2361 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
2362 migr_chunk
= migr_strip_blocks_resync(dev
);
2363 disks
= imsm_num_data_members(dev
, MAP_0
);
2364 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
2365 stripe
= __le16_to_cpu(map
->blocks_per_strip
) * disks
;
2366 segment
= blocks_per_unit
/ stripe
;
2367 block_rel
= blocks_per_unit
- segment
* stripe
;
2368 parity_depth
= parity_segment_depth(dev
);
2369 block_map
= map_migr_block(dev
, block_rel
);
2370 return block_map
+ parity_depth
* segment
;
2372 case MIGR_REBUILD
: {
2373 __u32 stripes_per_unit
;
2376 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
2377 migr_chunk
= migr_strip_blocks_rebuild(dev
);
2378 return migr_chunk
* stripes_per_unit
;
2380 case MIGR_STATE_CHANGE
:
2386 static int imsm_level_to_layout(int level
)
2394 return ALGORITHM_LEFT_ASYMMETRIC
;
2401 /*******************************************************************************
2402 * Function: read_imsm_migr_rec
2403 * Description: Function reads imsm migration record from last sector of disk
2405 * fd : disk descriptor
2406 * super : metadata info
2410 ******************************************************************************/
2411 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
)
2414 unsigned long long dsize
;
2416 get_dev_size(fd
, NULL
, &dsize
);
2417 if (lseek64(fd
, dsize
- MIGR_REC_POSITION
, SEEK_SET
) < 0) {
2418 pr_err("Cannot seek to anchor block: %s\n",
2422 if (read(fd
, super
->migr_rec_buf
, MIGR_REC_BUF_SIZE
) !=
2423 MIGR_REC_BUF_SIZE
) {
2424 pr_err("Cannot read migr record block: %s\n",
2434 static struct imsm_dev
*imsm_get_device_during_migration(
2435 struct intel_super
*super
)
2438 struct intel_dev
*dv
;
2440 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
2441 if (is_gen_migration(dv
->dev
))
2447 /*******************************************************************************
2448 * Function: load_imsm_migr_rec
2449 * Description: Function reads imsm migration record (it is stored at the last
2452 * super : imsm internal array info
2453 * info : general array info
2457 * -2 : no migration in progress
2458 ******************************************************************************/
2459 static int load_imsm_migr_rec(struct intel_super
*super
, struct mdinfo
*info
)
2462 struct dl
*dl
= NULL
;
2466 struct imsm_dev
*dev
;
2467 struct imsm_map
*map
= NULL
;
2470 /* find map under migration */
2471 dev
= imsm_get_device_during_migration(super
);
2472 /* nothing to load,no migration in progress?
2476 map
= get_imsm_map(dev
, MAP_0
);
2479 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
2480 /* skip spare and failed disks
2482 if (sd
->disk
.raid_disk
< 0)
2484 /* read only from one of the first two slots */
2486 slot
= get_imsm_disk_slot(map
,
2487 sd
->disk
.raid_disk
);
2488 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2491 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
2492 fd
= dev_open(nm
, O_RDONLY
);
2498 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
2499 /* skip spare and failed disks
2503 /* read only from one of the first two slots */
2505 slot
= get_imsm_disk_slot(map
, dl
->index
);
2506 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2508 sprintf(nm
, "%d:%d", dl
->major
, dl
->minor
);
2509 fd
= dev_open(nm
, O_RDONLY
);
2516 retval
= read_imsm_migr_rec(fd
, super
);
2525 /*******************************************************************************
2526 * function: imsm_create_metadata_checkpoint_update
2527 * Description: It creates update for checkpoint change.
2529 * super : imsm internal array info
2530 * u : pointer to prepared update
2533 * If length is equal to 0, input pointer u contains no update
2534 ******************************************************************************/
2535 static int imsm_create_metadata_checkpoint_update(
2536 struct intel_super
*super
,
2537 struct imsm_update_general_migration_checkpoint
**u
)
2540 int update_memory_size
= 0;
2542 dprintf("(enter)\n");
2548 /* size of all update data without anchor */
2549 update_memory_size
=
2550 sizeof(struct imsm_update_general_migration_checkpoint
);
2552 *u
= xcalloc(1, update_memory_size
);
2554 dprintf("error: cannot get memory\n");
2557 (*u
)->type
= update_general_migration_checkpoint
;
2558 (*u
)->curr_migr_unit
= __le32_to_cpu(super
->migr_rec
->curr_migr_unit
);
2559 dprintf("prepared for %u\n", (*u
)->curr_migr_unit
);
2561 return update_memory_size
;
2564 static void imsm_update_metadata_locally(struct supertype
*st
,
2565 void *buf
, int len
);
2567 /*******************************************************************************
2568 * Function: write_imsm_migr_rec
2569 * Description: Function writes imsm migration record
2570 * (at the last sector of disk)
2572 * super : imsm internal array info
2576 ******************************************************************************/
2577 static int write_imsm_migr_rec(struct supertype
*st
)
2579 struct intel_super
*super
= st
->sb
;
2580 unsigned long long dsize
;
2586 struct imsm_update_general_migration_checkpoint
*u
;
2587 struct imsm_dev
*dev
;
2588 struct imsm_map
*map
= NULL
;
2590 /* find map under migration */
2591 dev
= imsm_get_device_during_migration(super
);
2592 /* if no migration, write buffer anyway to clear migr_record
2593 * on disk based on first available device
2596 dev
= get_imsm_dev(super
, super
->current_vol
< 0 ? 0 :
2597 super
->current_vol
);
2599 map
= get_imsm_map(dev
, MAP_0
);
2601 for (sd
= super
->disks
; sd
; sd
= sd
->next
) {
2604 /* skip failed and spare devices */
2607 /* write to 2 first slots only */
2609 slot
= get_imsm_disk_slot(map
, sd
->index
);
2610 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2613 sprintf(nm
, "%d:%d", sd
->major
, sd
->minor
);
2614 fd
= dev_open(nm
, O_RDWR
);
2617 get_dev_size(fd
, NULL
, &dsize
);
2618 if (lseek64(fd
, dsize
- MIGR_REC_POSITION
, SEEK_SET
) < 0) {
2619 pr_err("Cannot seek to anchor block: %s\n",
2623 if (write(fd
, super
->migr_rec_buf
, MIGR_REC_BUF_SIZE
) !=
2624 MIGR_REC_BUF_SIZE
) {
2625 pr_err("Cannot write migr record block: %s\n",
2632 /* update checkpoint information in metadata */
2633 len
= imsm_create_metadata_checkpoint_update(super
, &u
);
2636 dprintf("imsm: Cannot prepare update\n");
2639 /* update metadata locally */
2640 imsm_update_metadata_locally(st
, u
, len
);
2641 /* and possibly remotely */
2642 if (st
->update_tail
) {
2643 append_metadata_update(st
, u
, len
);
2644 /* during reshape we do all work inside metadata handler
2645 * manage_reshape(), so metadata update has to be triggered
2648 flush_metadata_updates(st
);
2649 st
->update_tail
= &st
->updates
;
2659 #endif /* MDASSEMBLE */
2661 /* spare/missing disks activations are not allowe when
2662 * array/container performs reshape operation, because
2663 * all arrays in container works on the same disks set
2665 int imsm_reshape_blocks_arrays_changes(struct intel_super
*super
)
2668 struct intel_dev
*i_dev
;
2669 struct imsm_dev
*dev
;
2671 /* check whole container
2673 for (i_dev
= super
->devlist
; i_dev
; i_dev
= i_dev
->next
) {
2675 if (is_gen_migration(dev
)) {
2676 /* No repair during any migration in container
2684 static unsigned long long imsm_component_size_aligment_check(int level
,
2686 unsigned long long component_size
)
2688 unsigned int component_size_alligment
;
2690 /* check component size aligment
2692 component_size_alligment
= component_size
% (chunk_size
/512);
2694 dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alligment = %u\n",
2695 level
, chunk_size
, component_size
,
2696 component_size_alligment
);
2698 if (component_size_alligment
&& (level
!= 1) && (level
!= UnSet
)) {
2699 dprintf("imsm: reported component size alligned from %llu ",
2701 component_size
-= component_size_alligment
;
2702 dprintf_cont("to %llu (%i).\n",
2703 component_size
, component_size_alligment
);
2706 return component_size
;
2709 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
2711 struct intel_super
*super
= st
->sb
;
2712 struct migr_record
*migr_rec
= super
->migr_rec
;
2713 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
2714 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2715 struct imsm_map
*prev_map
= get_imsm_map(dev
, MAP_1
);
2716 struct imsm_map
*map_to_analyse
= map
;
2718 int map_disks
= info
->array
.raid_disks
;
2720 memset(info
, 0, sizeof(*info
));
2722 map_to_analyse
= prev_map
;
2724 dl
= super
->current_disk
;
2726 info
->container_member
= super
->current_vol
;
2727 info
->array
.raid_disks
= map
->num_members
;
2728 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
2729 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
2730 info
->array
.md_minor
= -1;
2731 info
->array
.ctime
= 0;
2732 info
->array
.utime
= 0;
2733 info
->array
.chunk_size
=
2734 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
2735 info
->array
.state
= !dev
->vol
.dirty
;
2736 info
->custom_array_size
= __le32_to_cpu(dev
->size_high
);
2737 info
->custom_array_size
<<= 32;
2738 info
->custom_array_size
|= __le32_to_cpu(dev
->size_low
);
2739 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
2741 if (is_gen_migration(dev
)) {
2742 info
->reshape_active
= 1;
2743 info
->new_level
= get_imsm_raid_level(map
);
2744 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
2745 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
2746 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
2747 if (info
->delta_disks
) {
2748 /* this needs to be applied to every array
2751 info
->reshape_active
= CONTAINER_RESHAPE
;
2753 /* We shape information that we give to md might have to be
2754 * modify to cope with md's requirement for reshaping arrays.
2755 * For example, when reshaping a RAID0, md requires it to be
2756 * presented as a degraded RAID4.
2757 * Also if a RAID0 is migrating to a RAID5 we need to specify
2758 * the array as already being RAID5, but the 'before' layout
2759 * is a RAID4-like layout.
2761 switch (info
->array
.level
) {
2763 switch(info
->new_level
) {
2765 /* conversion is happening as RAID4 */
2766 info
->array
.level
= 4;
2767 info
->array
.raid_disks
+= 1;
2770 /* conversion is happening as RAID5 */
2771 info
->array
.level
= 5;
2772 info
->array
.layout
= ALGORITHM_PARITY_N
;
2773 info
->delta_disks
-= 1;
2776 /* FIXME error message */
2777 info
->array
.level
= UnSet
;
2783 info
->new_level
= UnSet
;
2784 info
->new_layout
= UnSet
;
2785 info
->new_chunk
= info
->array
.chunk_size
;
2786 info
->delta_disks
= 0;
2790 info
->disk
.major
= dl
->major
;
2791 info
->disk
.minor
= dl
->minor
;
2792 info
->disk
.number
= dl
->index
;
2793 info
->disk
.raid_disk
= get_imsm_disk_slot(map_to_analyse
,
2797 info
->data_offset
= pba_of_lba0(map_to_analyse
);
2798 info
->component_size
= blocks_per_member(map_to_analyse
);
2800 info
->component_size
= imsm_component_size_aligment_check(
2802 info
->array
.chunk_size
,
2803 info
->component_size
);
2805 memset(info
->uuid
, 0, sizeof(info
->uuid
));
2806 info
->recovery_start
= MaxSector
;
2808 info
->reshape_progress
= 0;
2809 info
->resync_start
= MaxSector
;
2810 if ((map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
2812 imsm_reshape_blocks_arrays_changes(super
) == 0) {
2813 info
->resync_start
= 0;
2815 if (dev
->vol
.migr_state
) {
2816 switch (migr_type(dev
)) {
2819 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2821 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
2823 info
->resync_start
= blocks_per_unit
* units
;
2826 case MIGR_GEN_MIGR
: {
2827 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2829 __u64 units
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
2830 unsigned long long array_blocks
;
2833 if (__le32_to_cpu(migr_rec
->ascending_migr
) &&
2835 (__le32_to_cpu(migr_rec
->num_migr_units
)-1)) &&
2836 (super
->migr_rec
->rec_status
==
2837 __cpu_to_le32(UNIT_SRC_IN_CP_AREA
)))
2840 info
->reshape_progress
= blocks_per_unit
* units
;
2842 dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n",
2843 (unsigned long long)units
,
2844 (unsigned long long)blocks_per_unit
,
2845 info
->reshape_progress
);
2847 used_disks
= imsm_num_data_members(dev
, MAP_1
);
2848 if (used_disks
> 0) {
2849 array_blocks
= blocks_per_member(map
) *
2851 /* round array size down to closest MB
2853 info
->custom_array_size
= (array_blocks
2854 >> SECT_PER_MB_SHIFT
)
2855 << SECT_PER_MB_SHIFT
;
2859 /* we could emulate the checkpointing of
2860 * 'sync_action=check' migrations, but for now
2861 * we just immediately complete them
2864 /* this is handled by container_content_imsm() */
2865 case MIGR_STATE_CHANGE
:
2866 /* FIXME handle other migrations */
2868 /* we are not dirty, so... */
2869 info
->resync_start
= MaxSector
;
2873 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
2874 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
2876 info
->array
.major_version
= -1;
2877 info
->array
.minor_version
= -2;
2878 sprintf(info
->text_version
, "/%s/%d", st
->container_devnm
, info
->container_member
);
2879 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
2880 uuid_from_super_imsm(st
, info
->uuid
);
2884 for (i
=0; i
<map_disks
; i
++) {
2886 if (i
< info
->array
.raid_disks
) {
2887 struct imsm_disk
*dsk
;
2888 j
= get_imsm_disk_idx(dev
, i
, MAP_X
);
2889 dsk
= get_imsm_disk(super
, j
);
2890 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
2897 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
2898 int failed
, int look_in_map
);
2900 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
2904 static void manage_second_map(struct intel_super
*super
, struct imsm_dev
*dev
)
2906 if (is_gen_migration(dev
)) {
2909 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
2911 failed
= imsm_count_failed(super
, dev
, MAP_1
);
2912 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_1
);
2913 if (map2
->map_state
!= map_state
) {
2914 map2
->map_state
= map_state
;
2915 super
->updates_pending
++;
2921 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
2925 for (d
= super
->missing
; d
; d
= d
->next
)
2926 if (d
->index
== index
)
2931 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
2933 struct intel_super
*super
= st
->sb
;
2934 struct imsm_disk
*disk
;
2935 int map_disks
= info
->array
.raid_disks
;
2936 int max_enough
= -1;
2938 struct imsm_super
*mpb
;
2940 if (super
->current_vol
>= 0) {
2941 getinfo_super_imsm_volume(st
, info
, map
);
2944 memset(info
, 0, sizeof(*info
));
2946 /* Set raid_disks to zero so that Assemble will always pull in valid
2949 info
->array
.raid_disks
= 0;
2950 info
->array
.level
= LEVEL_CONTAINER
;
2951 info
->array
.layout
= 0;
2952 info
->array
.md_minor
= -1;
2953 info
->array
.ctime
= 0; /* N/A for imsm */
2954 info
->array
.utime
= 0;
2955 info
->array
.chunk_size
= 0;
2957 info
->disk
.major
= 0;
2958 info
->disk
.minor
= 0;
2959 info
->disk
.raid_disk
= -1;
2960 info
->reshape_active
= 0;
2961 info
->array
.major_version
= -1;
2962 info
->array
.minor_version
= -2;
2963 strcpy(info
->text_version
, "imsm");
2964 info
->safe_mode_delay
= 0;
2965 info
->disk
.number
= -1;
2966 info
->disk
.state
= 0;
2968 info
->recovery_start
= MaxSector
;
2969 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
2971 /* do we have the all the insync disks that we expect? */
2972 mpb
= super
->anchor
;
2974 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
2975 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
2976 int failed
, enough
, j
, missing
= 0;
2977 struct imsm_map
*map
;
2980 failed
= imsm_count_failed(super
, dev
, MAP_0
);
2981 state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
2982 map
= get_imsm_map(dev
, MAP_0
);
2984 /* any newly missing disks?
2985 * (catches single-degraded vs double-degraded)
2987 for (j
= 0; j
< map
->num_members
; j
++) {
2988 __u32 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
2989 __u32 idx
= ord_to_idx(ord
);
2991 if (!(ord
& IMSM_ORD_REBUILD
) &&
2992 get_imsm_missing(super
, idx
)) {
2998 if (state
== IMSM_T_STATE_FAILED
)
3000 else if (state
== IMSM_T_STATE_DEGRADED
&&
3001 (state
!= map
->map_state
|| missing
))
3003 else /* we're normal, or already degraded */
3005 if (is_gen_migration(dev
) && missing
) {
3006 /* during general migration we need all disks
3007 * that process is running on.
3008 * No new missing disk is allowed.
3012 /* no more checks necessary
3016 /* in the missing/failed disk case check to see
3017 * if at least one array is runnable
3019 max_enough
= max(max_enough
, enough
);
3021 dprintf("enough: %d\n", max_enough
);
3022 info
->container_enough
= max_enough
;
3025 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
3027 disk
= &super
->disks
->disk
;
3028 info
->data_offset
= total_blocks(&super
->disks
->disk
) - reserved
;
3029 info
->component_size
= reserved
;
3030 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
3031 /* we don't change info->disk.raid_disk here because
3032 * this state will be finalized in mdmon after we have
3033 * found the 'most fresh' version of the metadata
3035 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3036 info
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
3039 /* only call uuid_from_super_imsm when this disk is part of a populated container,
3040 * ->compare_super may have updated the 'num_raid_devs' field for spares
3042 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
3043 uuid_from_super_imsm(st
, info
->uuid
);
3045 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
3047 /* I don't know how to compute 'map' on imsm, so use safe default */
3050 for (i
= 0; i
< map_disks
; i
++)
3056 /* allocates memory and fills disk in mdinfo structure
3057 * for each disk in array */
3058 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
3060 struct mdinfo
*mddev
= NULL
;
3061 struct intel_super
*super
= st
->sb
;
3062 struct imsm_disk
*disk
;
3065 if (!super
|| !super
->disks
)
3068 mddev
= xcalloc(1, sizeof(*mddev
));
3072 tmp
= xcalloc(1, sizeof(*tmp
));
3074 tmp
->next
= mddev
->devs
;
3076 tmp
->disk
.number
= count
++;
3077 tmp
->disk
.major
= dl
->major
;
3078 tmp
->disk
.minor
= dl
->minor
;
3079 tmp
->disk
.state
= is_configured(disk
) ?
3080 (1 << MD_DISK_ACTIVE
) : 0;
3081 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3082 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
3083 tmp
->disk
.raid_disk
= -1;
3089 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
3090 char *update
, char *devname
, int verbose
,
3091 int uuid_set
, char *homehost
)
3093 /* For 'assemble' and 'force' we need to return non-zero if any
3094 * change was made. For others, the return value is ignored.
3095 * Update options are:
3096 * force-one : This device looks a bit old but needs to be included,
3097 * update age info appropriately.
3098 * assemble: clear any 'faulty' flag to allow this device to
3100 * force-array: Array is degraded but being forced, mark it clean
3101 * if that will be needed to assemble it.
3103 * newdev: not used ????
3104 * grow: Array has gained a new device - this is currently for
3106 * resync: mark as dirty so a resync will happen.
3107 * name: update the name - preserving the homehost
3108 * uuid: Change the uuid of the array to match watch is given
3110 * Following are not relevant for this imsm:
3111 * sparc2.2 : update from old dodgey metadata
3112 * super-minor: change the preferred_minor number
3113 * summaries: update redundant counters.
3114 * homehost: update the recorded homehost
3115 * _reshape_progress: record new reshape_progress position.
3118 struct intel_super
*super
= st
->sb
;
3119 struct imsm_super
*mpb
;
3121 /* we can only update container info */
3122 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
3125 mpb
= super
->anchor
;
3127 if (strcmp(update
, "uuid") == 0) {
3128 /* We take this to mean that the family_num should be updated.
3129 * However that is much smaller than the uuid so we cannot really
3130 * allow an explicit uuid to be given. And it is hard to reliably
3132 * So if !uuid_set we know the current uuid is random and just used
3133 * the first 'int' and copy it to the other 3 positions.
3134 * Otherwise we require the 4 'int's to be the same as would be the
3135 * case if we are using a random uuid. So an explicit uuid will be
3136 * accepted as long as all for ints are the same... which shouldn't hurt
3139 info
->uuid
[1] = info
->uuid
[2] = info
->uuid
[3] = info
->uuid
[0];
3142 if (info
->uuid
[0] != info
->uuid
[1] ||
3143 info
->uuid
[1] != info
->uuid
[2] ||
3144 info
->uuid
[2] != info
->uuid
[3])
3150 mpb
->orig_family_num
= info
->uuid
[0];
3151 } else if (strcmp(update
, "assemble") == 0)
3156 /* successful update? recompute checksum */
3158 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
3163 static size_t disks_to_mpb_size(int disks
)
3167 size
= sizeof(struct imsm_super
);
3168 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
3169 size
+= 2 * sizeof(struct imsm_dev
);
3170 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
3171 size
+= (4 - 2) * sizeof(struct imsm_map
);
3172 /* 4 possible disk_ord_tbl's */
3173 size
+= 4 * (disks
- 1) * sizeof(__u32
);
3178 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
,
3179 unsigned long long data_offset
)
3181 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
3184 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
3187 static void free_devlist(struct intel_super
*super
)
3189 struct intel_dev
*dv
;
3191 while (super
->devlist
) {
3192 dv
= super
->devlist
->next
;
3193 free(super
->devlist
->dev
);
3194 free(super
->devlist
);
3195 super
->devlist
= dv
;
3199 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
3201 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
3204 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
3208 * 0 same, or first was empty, and second was copied
3209 * 1 second had wrong number
3211 * 3 wrong other info
3213 struct intel_super
*first
= st
->sb
;
3214 struct intel_super
*sec
= tst
->sb
;
3221 /* in platform dependent environment test if the disks
3222 * use the same Intel hba
3223 * If not on Intel hba at all, allow anything.
3225 if (!check_env("IMSM_NO_PLATFORM") && first
->hba
&& sec
->hba
) {
3226 if (first
->hba
->type
!= sec
->hba
->type
) {
3228 "HBAs of devices do not match %s != %s\n",
3229 get_sys_dev_type(first
->hba
->type
),
3230 get_sys_dev_type(sec
->hba
->type
));
3233 if (first
->orom
!= sec
->orom
) {
3235 "HBAs of devices do not match %s != %s\n",
3236 first
->hba
->pci_id
, sec
->hba
->pci_id
);
3241 /* if an anchor does not have num_raid_devs set then it is a free
3244 if (first
->anchor
->num_raid_devs
> 0 &&
3245 sec
->anchor
->num_raid_devs
> 0) {
3246 /* Determine if these disks might ever have been
3247 * related. Further disambiguation can only take place
3248 * in load_super_imsm_all
3250 __u32 first_family
= first
->anchor
->orig_family_num
;
3251 __u32 sec_family
= sec
->anchor
->orig_family_num
;
3253 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
3254 MAX_SIGNATURE_LENGTH
) != 0)
3257 if (first_family
== 0)
3258 first_family
= first
->anchor
->family_num
;
3259 if (sec_family
== 0)
3260 sec_family
= sec
->anchor
->family_num
;
3262 if (first_family
!= sec_family
)
3267 /* if 'first' is a spare promote it to a populated mpb with sec's
3270 if (first
->anchor
->num_raid_devs
== 0 &&
3271 sec
->anchor
->num_raid_devs
> 0) {
3273 struct intel_dev
*dv
;
3274 struct imsm_dev
*dev
;
3276 /* we need to copy raid device info from sec if an allocation
3277 * fails here we don't associate the spare
3279 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
3280 dv
= xmalloc(sizeof(*dv
));
3281 dev
= xmalloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
3284 dv
->next
= first
->devlist
;
3285 first
->devlist
= dv
;
3287 if (i
< sec
->anchor
->num_raid_devs
) {
3288 /* allocation failure */
3289 free_devlist(first
);
3290 pr_err("imsm: failed to associate spare\n");
3293 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
3294 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
3295 first
->anchor
->family_num
= sec
->anchor
->family_num
;
3296 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
3297 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
3298 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
3304 static void fd2devname(int fd
, char *name
)
3308 char dname
[PATH_MAX
];
3313 if (fstat(fd
, &st
) != 0)
3315 sprintf(path
, "/sys/dev/block/%d:%d",
3316 major(st
.st_rdev
), minor(st
.st_rdev
));
3318 rv
= readlink(path
, dname
, sizeof(dname
)-1);
3323 nm
= strrchr(dname
, '/');
3326 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
3330 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
3332 static int imsm_read_serial(int fd
, char *devname
,
3333 __u8 serial
[MAX_RAID_SERIAL_LEN
])
3335 unsigned char scsi_serial
[255];
3344 memset(scsi_serial
, 0, sizeof(scsi_serial
));
3346 rv
= scsi_get_serial(fd
, scsi_serial
, sizeof(scsi_serial
));
3348 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
3349 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3350 fd2devname(fd
, (char *) serial
);
3356 pr_err("Failed to retrieve serial for %s\n",
3361 rsp_len
= scsi_serial
[3];
3364 pr_err("Failed to retrieve serial for %s\n",
3368 rsp_buf
= (char *) &scsi_serial
[4];
3370 /* trim all whitespace and non-printable characters and convert
3373 for (i
= 0, dest
= rsp_buf
; i
< rsp_len
; i
++) {
3376 /* ':' is reserved for use in placeholder serial
3377 * numbers for missing disks
3385 len
= dest
- rsp_buf
;
3388 /* truncate leading characters */
3389 if (len
> MAX_RAID_SERIAL_LEN
) {
3390 dest
+= len
- MAX_RAID_SERIAL_LEN
;
3391 len
= MAX_RAID_SERIAL_LEN
;
3394 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3395 memcpy(serial
, dest
, len
);
3400 static int serialcmp(__u8
*s1
, __u8
*s2
)
3402 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
3405 static void serialcpy(__u8
*dest
, __u8
*src
)
3407 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
3410 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
3414 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3415 if (serialcmp(dl
->serial
, serial
) == 0)
3421 static struct imsm_disk
*
3422 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
3426 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3427 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3429 if (serialcmp(disk
->serial
, serial
) == 0) {
3440 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3442 struct imsm_disk
*disk
;
3447 __u8 serial
[MAX_RAID_SERIAL_LEN
];
3449 rv
= imsm_read_serial(fd
, devname
, serial
);
3454 dl
= xcalloc(1, sizeof(*dl
));
3457 dl
->major
= major(stb
.st_rdev
);
3458 dl
->minor
= minor(stb
.st_rdev
);
3459 dl
->next
= super
->disks
;
3460 dl
->fd
= keep_fd
? fd
: -1;
3461 assert(super
->disks
== NULL
);
3463 serialcpy(dl
->serial
, serial
);
3466 fd2devname(fd
, name
);
3468 dl
->devname
= xstrdup(devname
);
3470 dl
->devname
= xstrdup(name
);
3472 /* look up this disk's index in the current anchor */
3473 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
3476 /* only set index on disks that are a member of a
3477 * populated contianer, i.e. one with raid_devs
3479 if (is_failed(&dl
->disk
))
3481 else if (is_spare(&dl
->disk
))
3489 /* When migrating map0 contains the 'destination' state while map1
3490 * contains the current state. When not migrating map0 contains the
3491 * current state. This routine assumes that map[0].map_state is set to
3492 * the current array state before being called.
3494 * Migration is indicated by one of the following states
3495 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
3496 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
3497 * map1state=unitialized)
3498 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
3500 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
3501 * map1state=degraded)
3502 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
3505 static void migrate(struct imsm_dev
*dev
, struct intel_super
*super
,
3506 __u8 to_state
, int migr_type
)
3508 struct imsm_map
*dest
;
3509 struct imsm_map
*src
= get_imsm_map(dev
, MAP_0
);
3511 dev
->vol
.migr_state
= 1;
3512 set_migr_type(dev
, migr_type
);
3513 dev
->vol
.curr_migr_unit
= 0;
3514 dest
= get_imsm_map(dev
, MAP_1
);
3516 /* duplicate and then set the target end state in map[0] */
3517 memcpy(dest
, src
, sizeof_imsm_map(src
));
3518 if ((migr_type
== MIGR_REBUILD
) ||
3519 (migr_type
== MIGR_GEN_MIGR
)) {
3523 for (i
= 0; i
< src
->num_members
; i
++) {
3524 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
3525 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
3529 if (migr_type
== MIGR_GEN_MIGR
)
3530 /* Clear migration record */
3531 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
3533 src
->map_state
= to_state
;
3536 static void end_migration(struct imsm_dev
*dev
, struct intel_super
*super
,
3539 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3540 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
== 0 ?
3544 /* merge any IMSM_ORD_REBUILD bits that were not successfully
3545 * completed in the last migration.
3547 * FIXME add support for raid-level-migration
3549 if ((map_state
!= map
->map_state
) && (is_gen_migration(dev
) == 0) &&
3550 (prev
->map_state
!= IMSM_T_STATE_UNINITIALIZED
)) {
3551 /* when final map state is other than expected
3552 * merge maps (not for migration)
3556 for (i
= 0; i
< prev
->num_members
; i
++)
3557 for (j
= 0; j
< map
->num_members
; j
++)
3558 /* during online capacity expansion
3559 * disks position can be changed
3560 * if takeover is used
3562 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
3563 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
3564 map
->disk_ord_tbl
[j
] |=
3565 prev
->disk_ord_tbl
[i
];
3568 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3569 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3572 dev
->vol
.migr_state
= 0;
3573 set_migr_type(dev
, 0);
3574 dev
->vol
.curr_migr_unit
= 0;
3575 map
->map_state
= map_state
;
3579 static int parse_raid_devices(struct intel_super
*super
)
3582 struct imsm_dev
*dev_new
;
3583 size_t len
, len_migr
;
3585 size_t space_needed
= 0;
3586 struct imsm_super
*mpb
= super
->anchor
;
3588 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3589 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3590 struct intel_dev
*dv
;
3592 len
= sizeof_imsm_dev(dev_iter
, 0);
3593 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
3595 space_needed
+= len_migr
- len
;
3597 dv
= xmalloc(sizeof(*dv
));
3598 if (max_len
< len_migr
)
3600 if (max_len
> len_migr
)
3601 space_needed
+= max_len
- len_migr
;
3602 dev_new
= xmalloc(max_len
);
3603 imsm_copy_dev(dev_new
, dev_iter
);
3606 dv
->next
= super
->devlist
;
3607 super
->devlist
= dv
;
3610 /* ensure that super->buf is large enough when all raid devices
3613 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
3616 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
, 512);
3617 if (posix_memalign(&buf
, 512, len
) != 0)
3620 memcpy(buf
, super
->buf
, super
->len
);
3621 memset(buf
+ super
->len
, 0, len
- super
->len
);
3630 /* retrieve a pointer to the bbm log which starts after all raid devices */
3631 struct bbm_log
*__get_imsm_bbm_log(struct imsm_super
*mpb
)
3635 if (__le32_to_cpu(mpb
->bbm_log_size
)) {
3637 ptr
+= mpb
->mpb_size
- __le32_to_cpu(mpb
->bbm_log_size
);
3643 /*******************************************************************************
3644 * Function: check_mpb_migr_compatibility
3645 * Description: Function checks for unsupported migration features:
3646 * - migration optimization area (pba_of_lba0)
3647 * - descending reshape (ascending_migr)
3649 * super : imsm metadata information
3651 * 0 : migration is compatible
3652 * -1 : migration is not compatible
3653 ******************************************************************************/
3654 int check_mpb_migr_compatibility(struct intel_super
*super
)
3656 struct imsm_map
*map0
, *map1
;
3657 struct migr_record
*migr_rec
= super
->migr_rec
;
3660 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3661 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3664 dev_iter
->vol
.migr_state
== 1 &&
3665 dev_iter
->vol
.migr_type
== MIGR_GEN_MIGR
) {
3666 /* This device is migrating */
3667 map0
= get_imsm_map(dev_iter
, MAP_0
);
3668 map1
= get_imsm_map(dev_iter
, MAP_1
);
3669 if (pba_of_lba0(map0
) != pba_of_lba0(map1
))
3670 /* migration optimization area was used */
3672 if (migr_rec
->ascending_migr
== 0
3673 && migr_rec
->dest_depth_per_unit
> 0)
3674 /* descending reshape not supported yet */
3681 static void __free_imsm(struct intel_super
*super
, int free_disks
);
3683 /* load_imsm_mpb - read matrix metadata
3684 * allocates super->mpb to be freed by free_imsm
3686 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
3688 unsigned long long dsize
;
3689 unsigned long long sectors
;
3691 struct imsm_super
*anchor
;
3694 get_dev_size(fd
, NULL
, &dsize
);
3697 pr_err("%s: device to small for imsm\n",
3702 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0) {
3704 pr_err("Cannot seek to anchor block on %s: %s\n",
3705 devname
, strerror(errno
));
3709 if (posix_memalign((void**)&anchor
, 512, 512) != 0) {
3711 pr_err("Failed to allocate imsm anchor buffer on %s\n", devname
);
3714 if (read(fd
, anchor
, 512) != 512) {
3716 pr_err("Cannot read anchor block on %s: %s\n",
3717 devname
, strerror(errno
));
3722 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
3724 pr_err("no IMSM anchor on %s\n", devname
);
3729 __free_imsm(super
, 0);
3730 /* reload capability and hba */
3732 /* capability and hba must be updated with new super allocation */
3733 find_intel_hba_capability(fd
, super
, devname
);
3734 super
->len
= ROUND_UP(anchor
->mpb_size
, 512);
3735 if (posix_memalign(&super
->buf
, 512, super
->len
) != 0) {
3737 pr_err("unable to allocate %zu byte mpb buffer\n",
3742 memcpy(super
->buf
, anchor
, 512);
3744 sectors
= mpb_sectors(anchor
) - 1;
3747 if (posix_memalign(&super
->migr_rec_buf
, 512, MIGR_REC_BUF_SIZE
) != 0) {
3748 pr_err("could not allocate migr_rec buffer\n");
3752 super
->clean_migration_record_by_mdmon
= 0;
3755 check_sum
= __gen_imsm_checksum(super
->anchor
);
3756 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3758 pr_err("IMSM checksum %x != %x on %s\n",
3760 __le32_to_cpu(super
->anchor
->check_sum
),
3768 /* read the extended mpb */
3769 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0) {
3771 pr_err("Cannot seek to extended mpb on %s: %s\n",
3772 devname
, strerror(errno
));
3776 if ((unsigned)read(fd
, super
->buf
+ 512, super
->len
- 512) != super
->len
- 512) {
3778 pr_err("Cannot read extended mpb on %s: %s\n",
3779 devname
, strerror(errno
));
3783 check_sum
= __gen_imsm_checksum(super
->anchor
);
3784 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3786 pr_err("IMSM checksum %x != %x on %s\n",
3787 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
3792 /* FIXME the BBM log is disk specific so we cannot use this global
3793 * buffer for all disks. Ok for now since we only look at the global
3794 * bbm_log_size parameter to gate assembly
3796 super
->bbm_log
= __get_imsm_bbm_log(super
->anchor
);
3801 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
);
3803 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
3804 static void clear_hi(struct intel_super
*super
)
3806 struct imsm_super
*mpb
= super
->anchor
;
3808 if (mpb
->attributes
& MPB_ATTRIB_2TB_DISK
)
3810 for (i
= 0; i
< mpb
->num_disks
; ++i
) {
3811 struct imsm_disk
*disk
= &mpb
->disk
[i
];
3812 disk
->total_blocks_hi
= 0;
3814 for (i
= 0; i
< mpb
->num_raid_devs
; ++i
) {
3815 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3818 for (n
= 0; n
< 2; ++n
) {
3819 struct imsm_map
*map
= get_imsm_map(dev
, n
);
3822 map
->pba_of_lba0_hi
= 0;
3823 map
->blocks_per_member_hi
= 0;
3824 map
->num_data_stripes_hi
= 0;
3830 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3834 err
= load_imsm_mpb(fd
, super
, devname
);
3837 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
3840 err
= parse_raid_devices(super
);
3845 static void __free_imsm_disk(struct dl
*d
)
3857 static void free_imsm_disks(struct intel_super
*super
)
3861 while (super
->disks
) {
3863 super
->disks
= d
->next
;
3864 __free_imsm_disk(d
);
3866 while (super
->disk_mgmt_list
) {
3867 d
= super
->disk_mgmt_list
;
3868 super
->disk_mgmt_list
= d
->next
;
3869 __free_imsm_disk(d
);
3871 while (super
->missing
) {
3873 super
->missing
= d
->next
;
3874 __free_imsm_disk(d
);
3879 /* free all the pieces hanging off of a super pointer */
3880 static void __free_imsm(struct intel_super
*super
, int free_disks
)
3882 struct intel_hba
*elem
, *next
;
3888 /* unlink capability description */
3890 if (super
->migr_rec_buf
) {
3891 free(super
->migr_rec_buf
);
3892 super
->migr_rec_buf
= NULL
;
3895 free_imsm_disks(super
);
3896 free_devlist(super
);
3900 free((void *)elem
->path
);
3908 static void free_imsm(struct intel_super
*super
)
3910 __free_imsm(super
, 1);
3914 static void free_super_imsm(struct supertype
*st
)
3916 struct intel_super
*super
= st
->sb
;
3925 static struct intel_super
*alloc_super(void)
3927 struct intel_super
*super
= xcalloc(1, sizeof(*super
));
3929 super
->current_vol
= -1;
3930 super
->create_offset
= ~((unsigned long long) 0);
3935 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
3937 static int find_intel_hba_capability(int fd
, struct intel_super
*super
, char *devname
)
3939 struct sys_dev
*hba_name
;
3942 if ((fd
< 0) || check_env("IMSM_NO_PLATFORM")) {
3947 hba_name
= find_disk_attached_hba(fd
, NULL
);
3950 pr_err("%s is not attached to Intel(R) RAID controller.\n",
3954 rv
= attach_hba_to_super(super
, hba_name
);
3957 struct intel_hba
*hba
= super
->hba
;
3959 pr_err("%s is attached to Intel(R) %s %s (%s),\n"
3960 " but the container is assigned to Intel(R) %s %s (",
3962 get_sys_dev_type(hba_name
->type
),
3963 hba_name
->type
== SYS_DEV_VMD
? "domain" : "RAID controller",
3964 hba_name
->pci_id
? : "Err!",
3965 get_sys_dev_type(super
->hba
->type
),
3966 hba
->type
== SYS_DEV_VMD
? "domain" : "RAID controller");
3969 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
3971 fprintf(stderr
, ", ");
3974 fprintf(stderr
, ").\n"
3975 " Mixing devices attached to different %s is not allowed.\n",
3976 hba_name
->type
== SYS_DEV_VMD
? "VMD domains" : "controllers");
3980 super
->orom
= find_imsm_capability(hba_name
);
3987 /* find_missing - helper routine for load_super_imsm_all that identifies
3988 * disks that have disappeared from the system. This routine relies on
3989 * the mpb being uptodate, which it is at load time.
3991 static int find_missing(struct intel_super
*super
)
3994 struct imsm_super
*mpb
= super
->anchor
;
3996 struct imsm_disk
*disk
;
3998 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3999 disk
= __get_imsm_disk(mpb
, i
);
4000 dl
= serial_to_dl(disk
->serial
, super
);
4004 dl
= xmalloc(sizeof(*dl
));
4008 dl
->devname
= xstrdup("missing");
4010 serialcpy(dl
->serial
, disk
->serial
);
4013 dl
->next
= super
->missing
;
4014 super
->missing
= dl
;
4021 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
4023 struct intel_disk
*idisk
= disk_list
;
4026 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
4028 idisk
= idisk
->next
;
4034 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
4035 struct intel_super
*super
,
4036 struct intel_disk
**disk_list
)
4038 struct imsm_disk
*d
= &super
->disks
->disk
;
4039 struct imsm_super
*mpb
= super
->anchor
;
4042 for (i
= 0; i
< tbl_size
; i
++) {
4043 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
4044 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
4046 if (tbl_mpb
->family_num
== mpb
->family_num
) {
4047 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
4048 dprintf("mpb from %d:%d matches %d:%d\n",
4049 super
->disks
->major
,
4050 super
->disks
->minor
,
4051 table
[i
]->disks
->major
,
4052 table
[i
]->disks
->minor
);
4056 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
4057 is_configured(d
) == is_configured(tbl_d
)) &&
4058 tbl_mpb
->generation_num
< mpb
->generation_num
) {
4059 /* current version of the mpb is a
4060 * better candidate than the one in
4061 * super_table, but copy over "cross
4062 * generational" status
4064 struct intel_disk
*idisk
;
4066 dprintf("mpb from %d:%d replaces %d:%d\n",
4067 super
->disks
->major
,
4068 super
->disks
->minor
,
4069 table
[i
]->disks
->major
,
4070 table
[i
]->disks
->minor
);
4072 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
4073 if (idisk
&& is_failed(&idisk
->disk
))
4074 tbl_d
->status
|= FAILED_DISK
;
4077 struct intel_disk
*idisk
;
4078 struct imsm_disk
*disk
;
4080 /* tbl_mpb is more up to date, but copy
4081 * over cross generational status before
4084 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
4085 if (disk
&& is_failed(disk
))
4086 d
->status
|= FAILED_DISK
;
4088 idisk
= disk_list_get(d
->serial
, *disk_list
);
4091 if (disk
&& is_configured(disk
))
4092 idisk
->disk
.status
|= CONFIGURED_DISK
;
4095 dprintf("mpb from %d:%d prefer %d:%d\n",
4096 super
->disks
->major
,
4097 super
->disks
->minor
,
4098 table
[i
]->disks
->major
,
4099 table
[i
]->disks
->minor
);
4107 table
[tbl_size
++] = super
;
4111 /* update/extend the merged list of imsm_disk records */
4112 for (j
= 0; j
< mpb
->num_disks
; j
++) {
4113 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
4114 struct intel_disk
*idisk
;
4116 idisk
= disk_list_get(disk
->serial
, *disk_list
);
4118 idisk
->disk
.status
|= disk
->status
;
4119 if (is_configured(&idisk
->disk
) ||
4120 is_failed(&idisk
->disk
))
4121 idisk
->disk
.status
&= ~(SPARE_DISK
);
4123 idisk
= xcalloc(1, sizeof(*idisk
));
4124 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
4125 idisk
->disk
= *disk
;
4126 idisk
->next
= *disk_list
;
4130 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
4137 static struct intel_super
*
4138 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
4141 struct imsm_super
*mpb
= super
->anchor
;
4145 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4146 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
4147 struct intel_disk
*idisk
;
4149 idisk
= disk_list_get(disk
->serial
, disk_list
);
4151 if (idisk
->owner
== owner
||
4152 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
4155 dprintf("'%.16s' owner %d != %d\n",
4156 disk
->serial
, idisk
->owner
,
4159 dprintf("unknown disk %x [%d]: %.16s\n",
4160 __le32_to_cpu(mpb
->family_num
), i
,
4166 if (ok_count
== mpb
->num_disks
)
4171 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
4173 struct intel_super
*s
;
4175 for (s
= super_list
; s
; s
= s
->next
) {
4176 if (family_num
!= s
->anchor
->family_num
)
4178 pr_err("Conflict, offlining family %#x on '%s'\n",
4179 __le32_to_cpu(family_num
), s
->disks
->devname
);
4183 static struct intel_super
*
4184 imsm_thunderdome(struct intel_super
**super_list
, int len
)
4186 struct intel_super
*super_table
[len
];
4187 struct intel_disk
*disk_list
= NULL
;
4188 struct intel_super
*champion
, *spare
;
4189 struct intel_super
*s
, **del
;
4194 memset(super_table
, 0, sizeof(super_table
));
4195 for (s
= *super_list
; s
; s
= s
->next
)
4196 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
4198 for (i
= 0; i
< tbl_size
; i
++) {
4199 struct imsm_disk
*d
;
4200 struct intel_disk
*idisk
;
4201 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
4204 d
= &s
->disks
->disk
;
4206 /* 'd' must appear in merged disk list for its
4207 * configuration to be valid
4209 idisk
= disk_list_get(d
->serial
, disk_list
);
4210 if (idisk
&& idisk
->owner
== i
)
4211 s
= validate_members(s
, disk_list
, i
);
4216 dprintf("marking family: %#x from %d:%d offline\n",
4218 super_table
[i
]->disks
->major
,
4219 super_table
[i
]->disks
->minor
);
4223 /* This is where the mdadm implementation differs from the Windows
4224 * driver which has no strict concept of a container. We can only
4225 * assemble one family from a container, so when returning a prodigal
4226 * array member to this system the code will not be able to disambiguate
4227 * the container contents that should be assembled ("foreign" versus
4228 * "local"). It requires user intervention to set the orig_family_num
4229 * to a new value to establish a new container. The Windows driver in
4230 * this situation fixes up the volume name in place and manages the
4231 * foreign array as an independent entity.
4236 for (i
= 0; i
< tbl_size
; i
++) {
4237 struct intel_super
*tbl_ent
= super_table
[i
];
4243 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
4248 if (s
&& !is_spare
) {
4249 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
4251 } else if (!s
&& !is_spare
)
4264 pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n",
4265 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
4267 /* collect all dl's onto 'champion', and update them to
4268 * champion's version of the status
4270 for (s
= *super_list
; s
; s
= s
->next
) {
4271 struct imsm_super
*mpb
= champion
->anchor
;
4272 struct dl
*dl
= s
->disks
;
4277 mpb
->attributes
|= s
->anchor
->attributes
& MPB_ATTRIB_2TB_DISK
;
4279 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4280 struct imsm_disk
*disk
;
4282 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
4285 /* only set index on disks that are a member of
4286 * a populated contianer, i.e. one with
4289 if (is_failed(&dl
->disk
))
4291 else if (is_spare(&dl
->disk
))
4297 if (i
>= mpb
->num_disks
) {
4298 struct intel_disk
*idisk
;
4300 idisk
= disk_list_get(dl
->serial
, disk_list
);
4301 if (idisk
&& is_spare(&idisk
->disk
) &&
4302 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
4310 dl
->next
= champion
->disks
;
4311 champion
->disks
= dl
;
4315 /* delete 'champion' from super_list */
4316 for (del
= super_list
; *del
; ) {
4317 if (*del
== champion
) {
4318 *del
= (*del
)->next
;
4321 del
= &(*del
)->next
;
4323 champion
->next
= NULL
;
4327 struct intel_disk
*idisk
= disk_list
;
4329 disk_list
= disk_list
->next
;
4337 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
);
4338 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
4339 int major
, int minor
, int keep_fd
);
4341 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4342 int *max
, int keep_fd
);
4344 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
4345 char *devname
, struct md_list
*devlist
,
4348 struct intel_super
*super_list
= NULL
;
4349 struct intel_super
*super
= NULL
;
4354 /* 'fd' is an opened container */
4355 err
= get_sra_super_block(fd
, &super_list
, devname
, &i
, keep_fd
);
4357 /* get super block from devlist devices */
4358 err
= get_devlist_super_block(devlist
, &super_list
, &i
, keep_fd
);
4361 /* all mpbs enter, maybe one leaves */
4362 super
= imsm_thunderdome(&super_list
, i
);
4368 if (find_missing(super
) != 0) {
4374 /* load migration record */
4375 err
= load_imsm_migr_rec(super
, NULL
);
4377 /* migration is in progress,
4378 * but migr_rec cannot be loaded,
4384 /* Check migration compatibility */
4385 if ((err
== 0) && (check_mpb_migr_compatibility(super
) != 0)) {
4386 pr_err("Unsupported migration detected");
4388 fprintf(stderr
, " on %s\n", devname
);
4390 fprintf(stderr
, " (IMSM).\n");
4399 while (super_list
) {
4400 struct intel_super
*s
= super_list
;
4402 super_list
= super_list
->next
;
4411 strcpy(st
->container_devnm
, fd2devnm(fd
));
4413 st
->container_devnm
[0] = 0;
4414 if (err
== 0 && st
->ss
== NULL
) {
4415 st
->ss
= &super_imsm
;
4416 st
->minor_version
= 0;
4417 st
->max_devs
= IMSM_MAX_DEVICES
;
4423 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4424 int *max
, int keep_fd
)
4426 struct md_list
*tmpdev
;
4430 for (i
= 0, tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
4431 if (tmpdev
->used
!= 1)
4433 if (tmpdev
->container
== 1) {
4435 int fd
= dev_open(tmpdev
->devname
, O_RDONLY
|O_EXCL
);
4437 pr_err("cannot open device %s: %s\n",
4438 tmpdev
->devname
, strerror(errno
));
4442 err
= get_sra_super_block(fd
, super_list
,
4443 tmpdev
->devname
, &lmax
,
4452 int major
= major(tmpdev
->st_rdev
);
4453 int minor
= minor(tmpdev
->st_rdev
);
4454 err
= get_super_block(super_list
,
4471 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
4472 int major
, int minor
, int keep_fd
)
4474 struct intel_super
*s
= NULL
;
4486 sprintf(nm
, "%d:%d", major
, minor
);
4487 dfd
= dev_open(nm
, O_RDWR
);
4493 find_intel_hba_capability(dfd
, s
, devname
);
4494 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4496 /* retry the load if we might have raced against mdmon */
4497 if (err
== 3 && devnm
&& mdmon_running(devnm
))
4498 for (retry
= 0; retry
< 3; retry
++) {
4500 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4506 s
->next
= *super_list
;
4514 if ((dfd
>= 0) && (!keep_fd
))
4521 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
)
4528 sra
= sysfs_read(fd
, NULL
, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
4532 if (sra
->array
.major_version
!= -1 ||
4533 sra
->array
.minor_version
!= -2 ||
4534 strcmp(sra
->text_version
, "imsm") != 0) {
4539 devnm
= fd2devnm(fd
);
4540 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
4541 if (get_super_block(super_list
, devnm
, devname
,
4542 sd
->disk
.major
, sd
->disk
.minor
, keep_fd
) != 0) {
4553 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
4555 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
, NULL
, 1);
4559 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
4561 struct intel_super
*super
;
4565 if (test_partition(fd
))
4566 /* IMSM not allowed on partitions */
4569 free_super_imsm(st
);
4571 super
= alloc_super();
4572 /* Load hba and capabilities if they exist.
4573 * But do not preclude loading metadata in case capabilities or hba are
4574 * non-compliant and ignore_hw_compat is set.
4576 rv
= find_intel_hba_capability(fd
, super
, devname
);
4577 /* no orom/efi or non-intel hba of the disk */
4578 if ((rv
!= 0) && (st
->ignore_hw_compat
== 0)) {
4580 pr_err("No OROM/EFI properties for %s\n", devname
);
4584 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
4586 /* retry the load if we might have raced against mdmon */
4588 struct mdstat_ent
*mdstat
= mdstat_by_component(fd2devnm(fd
));
4590 if (mdstat
&& mdmon_running(mdstat
->devnm
) && getpid() != mdmon_pid(mdstat
->devnm
)) {
4591 for (retry
= 0; retry
< 3; retry
++) {
4593 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
4599 free_mdstat(mdstat
);
4604 pr_err("Failed to load all information sections on %s\n", devname
);
4610 if (st
->ss
== NULL
) {
4611 st
->ss
= &super_imsm
;
4612 st
->minor_version
= 0;
4613 st
->max_devs
= IMSM_MAX_DEVICES
;
4616 /* load migration record */
4617 if (load_imsm_migr_rec(super
, NULL
) == 0) {
4618 /* Check for unsupported migration features */
4619 if (check_mpb_migr_compatibility(super
) != 0) {
4620 pr_err("Unsupported migration detected");
4622 fprintf(stderr
, " on %s\n", devname
);
4624 fprintf(stderr
, " (IMSM).\n");
4632 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
4634 if (info
->level
== 1)
4636 return info
->chunk_size
>> 9;
4639 static unsigned long long info_to_blocks_per_member(mdu_array_info_t
*info
,
4640 unsigned long long size
)
4642 if (info
->level
== 1)
4645 return (size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
4648 static void imsm_update_version_info(struct intel_super
*super
)
4650 /* update the version and attributes */
4651 struct imsm_super
*mpb
= super
->anchor
;
4653 struct imsm_dev
*dev
;
4654 struct imsm_map
*map
;
4657 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4658 dev
= get_imsm_dev(super
, i
);
4659 map
= get_imsm_map(dev
, MAP_0
);
4660 if (__le32_to_cpu(dev
->size_high
) > 0)
4661 mpb
->attributes
|= MPB_ATTRIB_2TB
;
4663 /* FIXME detect when an array spans a port multiplier */
4665 mpb
->attributes
|= MPB_ATTRIB_PM
;
4668 if (mpb
->num_raid_devs
> 1 ||
4669 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
4670 version
= MPB_VERSION_ATTRIBS
;
4671 switch (get_imsm_raid_level(map
)) {
4672 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
4673 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
4674 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
4675 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
4678 if (map
->num_members
>= 5)
4679 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
4680 else if (dev
->status
== DEV_CLONE_N_GO
)
4681 version
= MPB_VERSION_CNG
;
4682 else if (get_imsm_raid_level(map
) == 5)
4683 version
= MPB_VERSION_RAID5
;
4684 else if (map
->num_members
>= 3)
4685 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
4686 else if (get_imsm_raid_level(map
) == 1)
4687 version
= MPB_VERSION_RAID1
;
4689 version
= MPB_VERSION_RAID0
;
4691 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
4695 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
4697 struct imsm_super
*mpb
= super
->anchor
;
4698 char *reason
= NULL
;
4701 if (strlen(name
) > MAX_RAID_SERIAL_LEN
)
4702 reason
= "must be 16 characters or less";
4704 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4705 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4707 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
4708 reason
= "already exists";
4713 if (reason
&& !quiet
)
4714 pr_err("imsm volume name %s\n", reason
);
4719 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
4720 unsigned long long size
, char *name
,
4721 char *homehost
, int *uuid
,
4722 long long data_offset
)
4724 /* We are creating a volume inside a pre-existing container.
4725 * so st->sb is already set.
4727 struct intel_super
*super
= st
->sb
;
4728 struct imsm_super
*mpb
= super
->anchor
;
4729 struct intel_dev
*dv
;
4730 struct imsm_dev
*dev
;
4731 struct imsm_vol
*vol
;
4732 struct imsm_map
*map
;
4733 int idx
= mpb
->num_raid_devs
;
4735 unsigned long long array_blocks
;
4736 size_t size_old
, size_new
;
4737 unsigned long long num_data_stripes
;
4739 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
4740 pr_err("This imsm-container already has the maximum of %d volumes\n", super
->orom
->vpa
);
4744 /* ensure the mpb is large enough for the new data */
4745 size_old
= __le32_to_cpu(mpb
->mpb_size
);
4746 size_new
= disks_to_mpb_size(info
->nr_disks
);
4747 if (size_new
> size_old
) {
4749 size_t size_round
= ROUND_UP(size_new
, 512);
4751 if (posix_memalign(&mpb_new
, 512, size_round
) != 0) {
4752 pr_err("could not allocate new mpb\n");
4755 if (posix_memalign(&super
->migr_rec_buf
, 512,
4756 MIGR_REC_BUF_SIZE
) != 0) {
4757 pr_err("could not allocate migr_rec buffer\n");
4763 memcpy(mpb_new
, mpb
, size_old
);
4766 super
->anchor
= mpb_new
;
4767 mpb
->mpb_size
= __cpu_to_le32(size_new
);
4768 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
4770 super
->current_vol
= idx
;
4772 /* handle 'failed_disks' by either:
4773 * a) create dummy disk entries in the table if this the first
4774 * volume in the array. We add them here as this is the only
4775 * opportunity to add them. add_to_super_imsm_volume()
4776 * handles the non-failed disks and continues incrementing
4778 * b) validate that 'failed_disks' matches the current number
4779 * of missing disks if the container is populated
4781 if (super
->current_vol
== 0) {
4783 for (i
= 0; i
< info
->failed_disks
; i
++) {
4784 struct imsm_disk
*disk
;
4787 disk
= __get_imsm_disk(mpb
, i
);
4788 disk
->status
= CONFIGURED_DISK
| FAILED_DISK
;
4789 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
4790 snprintf((char *) disk
->serial
, MAX_RAID_SERIAL_LEN
,
4793 find_missing(super
);
4798 for (d
= super
->missing
; d
; d
= d
->next
)
4800 if (info
->failed_disks
> missing
) {
4801 pr_err("unable to add 'missing' disk to container\n");
4806 if (!check_name(super
, name
, 0))
4808 dv
= xmalloc(sizeof(*dv
));
4809 dev
= xcalloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
4810 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
4811 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
4812 info
->layout
, info
->chunk_size
,
4814 /* round array size down to closest MB */
4815 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
4817 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
4818 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
4819 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
4821 vol
->migr_state
= 0;
4822 set_migr_type(dev
, MIGR_INIT
);
4823 vol
->dirty
= !info
->state
;
4824 vol
->curr_migr_unit
= 0;
4825 map
= get_imsm_map(dev
, MAP_0
);
4826 set_pba_of_lba0(map
, super
->create_offset
);
4827 set_blocks_per_member(map
, info_to_blocks_per_member(info
, size
));
4828 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
4829 map
->failed_disk_num
= ~0;
4830 if (info
->level
> 0)
4831 map
->map_state
= (info
->state
? IMSM_T_STATE_NORMAL
4832 : IMSM_T_STATE_UNINITIALIZED
);
4834 map
->map_state
= info
->failed_disks
? IMSM_T_STATE_FAILED
:
4835 IMSM_T_STATE_NORMAL
;
4838 if (info
->level
== 1 && info
->raid_disks
> 2) {
4841 pr_err("imsm does not support more than 2 disksin a raid1 volume\n");
4845 map
->raid_level
= info
->level
;
4846 if (info
->level
== 10) {
4847 map
->raid_level
= 1;
4848 map
->num_domains
= info
->raid_disks
/ 2;
4849 } else if (info
->level
== 1)
4850 map
->num_domains
= info
->raid_disks
;
4852 map
->num_domains
= 1;
4854 /* info->size is only int so use the 'size' parameter instead */
4855 num_data_stripes
= (size
* 2) / info_to_blocks_per_strip(info
);
4856 num_data_stripes
/= map
->num_domains
;
4857 set_num_data_stripes(map
, num_data_stripes
);
4859 map
->num_members
= info
->raid_disks
;
4860 for (i
= 0; i
< map
->num_members
; i
++) {
4861 /* initialized in add_to_super */
4862 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
4864 mpb
->num_raid_devs
++;
4867 dv
->index
= super
->current_vol
;
4868 dv
->next
= super
->devlist
;
4869 super
->devlist
= dv
;
4871 imsm_update_version_info(super
);
4876 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
4877 unsigned long long size
, char *name
,
4878 char *homehost
, int *uuid
,
4879 unsigned long long data_offset
)
4881 /* This is primarily called by Create when creating a new array.
4882 * We will then get add_to_super called for each component, and then
4883 * write_init_super called to write it out to each device.
4884 * For IMSM, Create can create on fresh devices or on a pre-existing
4886 * To create on a pre-existing array a different method will be called.
4887 * This one is just for fresh drives.
4889 struct intel_super
*super
;
4890 struct imsm_super
*mpb
;
4894 if (data_offset
!= INVALID_SECTORS
) {
4895 pr_err("data-offset not supported by imsm\n");
4900 return init_super_imsm_volume(st
, info
, size
, name
, homehost
, uuid
,
4904 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
4908 super
= alloc_super();
4909 if (super
&& posix_memalign(&super
->buf
, 512, mpb_size
) != 0) {
4914 pr_err("could not allocate superblock\n");
4917 if (posix_memalign(&super
->migr_rec_buf
, 512, MIGR_REC_BUF_SIZE
) != 0) {
4918 pr_err("could not allocate migr_rec buffer\n");
4923 memset(super
->buf
, 0, mpb_size
);
4925 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
4929 /* zeroing superblock */
4933 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
4935 version
= (char *) mpb
->sig
;
4936 strcpy(version
, MPB_SIGNATURE
);
4937 version
+= strlen(MPB_SIGNATURE
);
4938 strcpy(version
, MPB_VERSION_RAID0
);
4944 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
4945 int fd
, char *devname
)
4947 struct intel_super
*super
= st
->sb
;
4948 struct imsm_super
*mpb
= super
->anchor
;
4949 struct imsm_disk
*_disk
;
4950 struct imsm_dev
*dev
;
4951 struct imsm_map
*map
;
4955 dev
= get_imsm_dev(super
, super
->current_vol
);
4956 map
= get_imsm_map(dev
, MAP_0
);
4958 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
4959 pr_err("%s: Cannot add spare devices to IMSM volume\n",
4965 /* we're doing autolayout so grab the pre-marked (in
4966 * validate_geometry) raid_disk
4968 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4969 if (dl
->raiddisk
== dk
->raid_disk
)
4972 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4973 if (dl
->major
== dk
->major
&&
4974 dl
->minor
== dk
->minor
)
4979 pr_err("%s is not a member of the same container\n", devname
);
4983 /* add a pristine spare to the metadata */
4984 if (dl
->index
< 0) {
4985 dl
->index
= super
->anchor
->num_disks
;
4986 super
->anchor
->num_disks
++;
4988 /* Check the device has not already been added */
4989 slot
= get_imsm_disk_slot(map
, dl
->index
);
4991 (get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
) & IMSM_ORD_REBUILD
) == 0) {
4992 pr_err("%s has been included in this array twice\n",
4996 set_imsm_ord_tbl_ent(map
, dk
->raid_disk
, dl
->index
);
4997 dl
->disk
.status
= CONFIGURED_DISK
;
4999 /* update size of 'missing' disks to be at least as large as the
5000 * largest acitve member (we only have dummy missing disks when
5001 * creating the first volume)
5003 if (super
->current_vol
== 0) {
5004 for (df
= super
->missing
; df
; df
= df
->next
) {
5005 if (total_blocks(&dl
->disk
) > total_blocks(&df
->disk
))
5006 set_total_blocks(&df
->disk
, total_blocks(&dl
->disk
));
5007 _disk
= __get_imsm_disk(mpb
, df
->index
);
5012 /* refresh unset/failed slots to point to valid 'missing' entries */
5013 for (df
= super
->missing
; df
; df
= df
->next
)
5014 for (slot
= 0; slot
< mpb
->num_disks
; slot
++) {
5015 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
5017 if ((ord
& IMSM_ORD_REBUILD
) == 0)
5019 set_imsm_ord_tbl_ent(map
, slot
, df
->index
| IMSM_ORD_REBUILD
);
5020 if (is_gen_migration(dev
)) {
5021 struct imsm_map
*map2
= get_imsm_map(dev
,
5023 int slot2
= get_imsm_disk_slot(map2
, df
->index
);
5024 if ((slot2
< map2
->num_members
) &&
5026 __u32 ord2
= get_imsm_ord_tbl_ent(dev
,
5029 if ((unsigned)df
->index
==
5031 set_imsm_ord_tbl_ent(map2
,
5037 dprintf("set slot:%d to missing disk:%d\n", slot
, df
->index
);
5041 /* if we are creating the first raid device update the family number */
5042 if (super
->current_vol
== 0) {
5044 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
5046 _disk
= __get_imsm_disk(mpb
, dl
->index
);
5047 if (!_dev
|| !_disk
) {
5048 pr_err("BUG mpb setup error\n");
5054 sum
+= __gen_imsm_checksum(mpb
);
5055 mpb
->family_num
= __cpu_to_le32(sum
);
5056 mpb
->orig_family_num
= mpb
->family_num
;
5058 super
->current_disk
= dl
;
5063 * Function marks disk as spare and restores disk serial
5064 * in case it was previously marked as failed by takeover operation
5066 * -1 : critical error
5067 * 0 : disk is marked as spare but serial is not set
5070 int mark_spare(struct dl
*disk
)
5072 __u8 serial
[MAX_RAID_SERIAL_LEN
];
5079 if (!imsm_read_serial(disk
->fd
, NULL
, serial
)) {
5080 /* Restore disk serial number, because takeover marks disk
5081 * as failed and adds to serial ':0' before it becomes
5084 serialcpy(disk
->serial
, serial
);
5085 serialcpy(disk
->disk
.serial
, serial
);
5088 disk
->disk
.status
= SPARE_DISK
;
5094 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
5095 int fd
, char *devname
,
5096 unsigned long long data_offset
)
5098 struct intel_super
*super
= st
->sb
;
5100 unsigned long long size
;
5105 /* If we are on an RAID enabled platform check that the disk is
5106 * attached to the raid controller.
5107 * We do not need to test disks attachment for container based additions,
5108 * they shall be already tested when container was created/assembled.
5110 rv
= find_intel_hba_capability(fd
, super
, devname
);
5111 /* no orom/efi or non-intel hba of the disk */
5113 dprintf("capability: %p fd: %d ret: %d\n",
5114 super
->orom
, fd
, rv
);
5118 if (super
->current_vol
>= 0)
5119 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
5122 dd
= xcalloc(sizeof(*dd
), 1);
5123 dd
->major
= major(stb
.st_rdev
);
5124 dd
->minor
= minor(stb
.st_rdev
);
5125 dd
->devname
= devname
? xstrdup(devname
) : NULL
;
5128 dd
->action
= DISK_ADD
;
5129 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
5131 pr_err("failed to retrieve scsi serial, aborting\n");
5136 get_dev_size(fd
, NULL
, &size
);
5137 /* clear migr_rec when adding disk to container */
5138 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SIZE
);
5139 if (lseek64(fd
, size
- MIGR_REC_POSITION
, SEEK_SET
) >= 0) {
5140 if (write(fd
, super
->migr_rec_buf
,
5141 MIGR_REC_BUF_SIZE
) != MIGR_REC_BUF_SIZE
)
5142 perror("Write migr_rec failed");
5146 serialcpy(dd
->disk
.serial
, dd
->serial
);
5147 set_total_blocks(&dd
->disk
, size
);
5148 if (__le32_to_cpu(dd
->disk
.total_blocks_hi
) > 0) {
5149 struct imsm_super
*mpb
= super
->anchor
;
5150 mpb
->attributes
|= MPB_ATTRIB_2TB_DISK
;
5153 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
5154 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
5156 dd
->disk
.scsi_id
= __cpu_to_le32(0);
5158 if (st
->update_tail
) {
5159 dd
->next
= super
->disk_mgmt_list
;
5160 super
->disk_mgmt_list
= dd
;
5162 dd
->next
= super
->disks
;
5164 super
->updates_pending
++;
5170 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
5172 struct intel_super
*super
= st
->sb
;
5175 /* remove from super works only in mdmon - for communication
5176 * manager - monitor. Check if communication memory buffer
5179 if (!st
->update_tail
) {
5180 pr_err("shall be used in mdmon context only\n");
5183 dd
= xcalloc(1, sizeof(*dd
));
5184 dd
->major
= dk
->major
;
5185 dd
->minor
= dk
->minor
;
5188 dd
->action
= DISK_REMOVE
;
5190 dd
->next
= super
->disk_mgmt_list
;
5191 super
->disk_mgmt_list
= dd
;
5196 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
5200 struct imsm_super anchor
;
5201 } spare_record
__attribute__ ((aligned(512)));
5203 /* spare records have their own family number and do not have any defined raid
5206 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
5208 struct imsm_super
*mpb
= super
->anchor
;
5209 struct imsm_super
*spare
= &spare_record
.anchor
;
5213 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
));
5214 spare
->generation_num
= __cpu_to_le32(1UL);
5215 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5216 spare
->num_disks
= 1;
5217 spare
->num_raid_devs
= 0;
5218 spare
->cache_size
= mpb
->cache_size
;
5219 spare
->pwr_cycle_count
= __cpu_to_le32(1);
5221 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
5222 MPB_SIGNATURE MPB_VERSION_RAID0
);
5224 for (d
= super
->disks
; d
; d
= d
->next
) {
5228 spare
->disk
[0] = d
->disk
;
5229 if (__le32_to_cpu(d
->disk
.total_blocks_hi
) > 0)
5230 spare
->attributes
|= MPB_ATTRIB_2TB_DISK
;
5232 sum
= __gen_imsm_checksum(spare
);
5233 spare
->family_num
= __cpu_to_le32(sum
);
5234 spare
->orig_family_num
= 0;
5235 sum
= __gen_imsm_checksum(spare
);
5236 spare
->check_sum
= __cpu_to_le32(sum
);
5238 if (store_imsm_mpb(d
->fd
, spare
)) {
5239 pr_err("failed for device %d:%d %s\n",
5240 d
->major
, d
->minor
, strerror(errno
));
5252 static int write_super_imsm(struct supertype
*st
, int doclose
)
5254 struct intel_super
*super
= st
->sb
;
5255 struct imsm_super
*mpb
= super
->anchor
;
5261 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
5263 int clear_migration_record
= 1;
5265 /* 'generation' is incremented everytime the metadata is written */
5266 generation
= __le32_to_cpu(mpb
->generation_num
);
5268 mpb
->generation_num
= __cpu_to_le32(generation
);
5270 /* fix up cases where previous mdadm releases failed to set
5273 if (mpb
->orig_family_num
== 0)
5274 mpb
->orig_family_num
= mpb
->family_num
;
5276 for (d
= super
->disks
; d
; d
= d
->next
) {
5280 mpb
->disk
[d
->index
] = d
->disk
;
5284 for (d
= super
->missing
; d
; d
= d
->next
) {
5285 mpb
->disk
[d
->index
] = d
->disk
;
5288 mpb
->num_disks
= num_disks
;
5289 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
5291 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5292 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
5293 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
5295 imsm_copy_dev(dev
, dev2
);
5296 mpb_size
+= sizeof_imsm_dev(dev
, 0);
5298 if (is_gen_migration(dev2
))
5299 clear_migration_record
= 0;
5301 mpb_size
+= __le32_to_cpu(mpb
->bbm_log_size
);
5302 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5304 /* recalculate checksum */
5305 sum
= __gen_imsm_checksum(mpb
);
5306 mpb
->check_sum
= __cpu_to_le32(sum
);
5308 if (super
->clean_migration_record_by_mdmon
) {
5309 clear_migration_record
= 1;
5310 super
->clean_migration_record_by_mdmon
= 0;
5312 if (clear_migration_record
)
5313 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SIZE
);
5315 /* write the mpb for disks that compose raid devices */
5316 for (d
= super
->disks
; d
; d
= d
->next
) {
5317 if (d
->index
< 0 || is_failed(&d
->disk
))
5320 if (clear_migration_record
) {
5321 unsigned long long dsize
;
5323 get_dev_size(d
->fd
, NULL
, &dsize
);
5324 if (lseek64(d
->fd
, dsize
- 512, SEEK_SET
) >= 0) {
5325 if (write(d
->fd
, super
->migr_rec_buf
,
5326 MIGR_REC_BUF_SIZE
) != MIGR_REC_BUF_SIZE
)
5327 perror("Write migr_rec failed");
5331 if (store_imsm_mpb(d
->fd
, mpb
))
5333 "failed for device %d:%d (fd: %d)%s\n",
5335 d
->fd
, strerror(errno
));
5344 return write_super_imsm_spares(super
, doclose
);
5349 static int create_array(struct supertype
*st
, int dev_idx
)
5352 struct imsm_update_create_array
*u
;
5353 struct intel_super
*super
= st
->sb
;
5354 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
5355 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
5356 struct disk_info
*inf
;
5357 struct imsm_disk
*disk
;
5360 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
5361 sizeof(*inf
) * map
->num_members
;
5363 u
->type
= update_create_array
;
5364 u
->dev_idx
= dev_idx
;
5365 imsm_copy_dev(&u
->dev
, dev
);
5366 inf
= get_disk_info(u
);
5367 for (i
= 0; i
< map
->num_members
; i
++) {
5368 int idx
= get_imsm_disk_idx(dev
, i
, MAP_X
);
5370 disk
= get_imsm_disk(super
, idx
);
5372 disk
= get_imsm_missing(super
, idx
);
5373 serialcpy(inf
[i
].serial
, disk
->serial
);
5375 append_metadata_update(st
, u
, len
);
5380 static int mgmt_disk(struct supertype
*st
)
5382 struct intel_super
*super
= st
->sb
;
5384 struct imsm_update_add_remove_disk
*u
;
5386 if (!super
->disk_mgmt_list
)
5391 u
->type
= update_add_remove_disk
;
5392 append_metadata_update(st
, u
, len
);
5397 static int write_init_super_imsm(struct supertype
*st
)
5399 struct intel_super
*super
= st
->sb
;
5400 int current_vol
= super
->current_vol
;
5402 /* we are done with current_vol reset it to point st at the container */
5403 super
->current_vol
= -1;
5405 if (st
->update_tail
) {
5406 /* queue the recently created array / added disk
5407 * as a metadata update */
5410 /* determine if we are creating a volume or adding a disk */
5411 if (current_vol
< 0) {
5412 /* in the mgmt (add/remove) disk case we are running
5413 * in mdmon context, so don't close fd's
5415 return mgmt_disk(st
);
5417 rv
= create_array(st
, current_vol
);
5422 for (d
= super
->disks
; d
; d
= d
->next
)
5423 Kill(d
->devname
, NULL
, 0, -1, 1);
5424 return write_super_imsm(st
, 1);
5429 static int store_super_imsm(struct supertype
*st
, int fd
)
5431 struct intel_super
*super
= st
->sb
;
5432 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
5438 return store_imsm_mpb(fd
, mpb
);
5444 static int imsm_bbm_log_size(struct imsm_super
*mpb
)
5446 return __le32_to_cpu(mpb
->bbm_log_size
);
5450 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
5451 int layout
, int raiddisks
, int chunk
,
5452 unsigned long long size
,
5453 unsigned long long data_offset
,
5455 unsigned long long *freesize
,
5459 unsigned long long ldsize
;
5460 struct intel_super
*super
=NULL
;
5463 if (level
!= LEVEL_CONTAINER
)
5468 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
5471 pr_err("imsm: Cannot open %s: %s\n",
5472 dev
, strerror(errno
));
5475 if (!get_dev_size(fd
, dev
, &ldsize
)) {
5480 /* capabilities retrieve could be possible
5481 * note that there is no fd for the disks in array.
5483 super
= alloc_super();
5484 rv
= find_intel_hba_capability(fd
, super
, verbose
> 0 ? dev
: NULL
);
5488 fd2devname(fd
, str
);
5489 dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n",
5490 fd
, str
, super
->orom
, rv
, raiddisks
);
5492 /* no orom/efi or non-intel hba of the disk */
5499 if (raiddisks
> super
->orom
->tds
) {
5501 pr_err("%d exceeds maximum number of platform supported disks: %d\n",
5502 raiddisks
, super
->orom
->tds
);
5506 if ((super
->orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) == 0 &&
5507 (ldsize
>> 9) >> 32 > 0) {
5509 pr_err("%s exceeds maximum platform supported size\n", dev
);
5515 *freesize
= avail_size_imsm(st
, ldsize
>> 9, data_offset
);
5521 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
5523 const unsigned long long base_start
= e
[*idx
].start
;
5524 unsigned long long end
= base_start
+ e
[*idx
].size
;
5527 if (base_start
== end
)
5531 for (i
= *idx
; i
< num_extents
; i
++) {
5532 /* extend overlapping extents */
5533 if (e
[i
].start
>= base_start
&&
5534 e
[i
].start
<= end
) {
5537 if (e
[i
].start
+ e
[i
].size
> end
)
5538 end
= e
[i
].start
+ e
[i
].size
;
5539 } else if (e
[i
].start
> end
) {
5545 return end
- base_start
;
5548 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
5550 /* build a composite disk with all known extents and generate a new
5551 * 'maxsize' given the "all disks in an array must share a common start
5552 * offset" constraint
5554 struct extent
*e
= xcalloc(sum_extents
, sizeof(*e
));
5558 unsigned long long pos
;
5559 unsigned long long start
= 0;
5560 unsigned long long maxsize
;
5561 unsigned long reserve
;
5563 /* coalesce and sort all extents. also, check to see if we need to
5564 * reserve space between member arrays
5567 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5570 for (i
= 0; i
< dl
->extent_cnt
; i
++)
5573 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
5578 while (i
< sum_extents
) {
5579 e
[j
].start
= e
[i
].start
;
5580 e
[j
].size
= find_size(e
, &i
, sum_extents
);
5582 if (e
[j
-1].size
== 0)
5591 unsigned long long esize
;
5593 esize
= e
[i
].start
- pos
;
5594 if (esize
>= maxsize
) {
5599 pos
= e
[i
].start
+ e
[i
].size
;
5601 } while (e
[i
-1].size
);
5607 /* FIXME assumes volume at offset 0 is the first volume in a
5610 if (start_extent
> 0)
5611 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
5615 if (maxsize
< reserve
)
5618 super
->create_offset
= ~((unsigned long long) 0);
5619 if (start
+ reserve
> super
->create_offset
)
5620 return 0; /* start overflows create_offset */
5621 super
->create_offset
= start
+ reserve
;
5623 return maxsize
- reserve
;
5626 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
5628 if (level
< 0 || level
== 6 || level
== 4)
5631 /* if we have an orom prevent invalid raid levels */
5634 case 0: return imsm_orom_has_raid0(orom
);
5637 return imsm_orom_has_raid1e(orom
);
5638 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
5639 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
5640 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
5643 return 1; /* not on an Intel RAID platform so anything goes */
5649 active_arrays_by_format(char *name
, char* hba
, struct md_list
**devlist
,
5650 int dpa
, int verbose
)
5652 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
5653 struct mdstat_ent
*memb
= NULL
;
5656 struct md_list
*dv
= NULL
;
5659 for (memb
= mdstat
; memb
; memb
= memb
->next
) {
5660 if (memb
->metadata_version
&&
5661 (strncmp(memb
->metadata_version
, "external:", 9) == 0) &&
5662 (strcmp(&memb
->metadata_version
[9], name
) == 0) &&
5663 !is_subarray(memb
->metadata_version
+9) &&
5665 struct dev_member
*dev
= memb
->members
;
5667 while(dev
&& (fd
< 0)) {
5668 char *path
= xmalloc(strlen(dev
->name
) + strlen("/dev/") + 1);
5669 num
= sprintf(path
, "%s%s", "/dev/", dev
->name
);
5671 fd
= open(path
, O_RDONLY
, 0);
5672 if ((num
<= 0) || (fd
< 0)) {
5673 pr_vrb(": Cannot open %s: %s\n",
5674 dev
->name
, strerror(errno
));
5680 if ((fd
>= 0) && disk_attached_to_hba(fd
, hba
)) {
5681 struct mdstat_ent
*vol
;
5682 for (vol
= mdstat
; vol
; vol
= vol
->next
) {
5683 if ((vol
->active
> 0) &&
5684 vol
->metadata_version
&&
5685 is_container_member(vol
, memb
->devnm
)) {
5690 if (*devlist
&& (found
< dpa
)) {
5691 dv
= xcalloc(1, sizeof(*dv
));
5692 dv
->devname
= xmalloc(strlen(memb
->devnm
) + strlen("/dev/") + 1);
5693 sprintf(dv
->devname
, "%s%s", "/dev/", memb
->devnm
);
5696 dv
->next
= *devlist
;
5704 free_mdstat(mdstat
);
5709 static struct md_list
*
5710 get_loop_devices(void)
5713 struct md_list
*devlist
= NULL
;
5714 struct md_list
*dv
= NULL
;
5716 for(i
= 0; i
< 12; i
++) {
5717 dv
= xcalloc(1, sizeof(*dv
));
5718 dv
->devname
= xmalloc(40);
5719 sprintf(dv
->devname
, "/dev/loop%d", i
);
5727 static struct md_list
*
5728 get_devices(const char *hba_path
)
5730 struct md_list
*devlist
= NULL
;
5731 struct md_list
*dv
= NULL
;
5737 devlist
= get_loop_devices();
5740 /* scroll through /sys/dev/block looking for devices attached to
5743 dir
= opendir("/sys/dev/block");
5744 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
5749 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
5751 path
= devt_to_devpath(makedev(major
, minor
));
5754 if (!path_attached_to_hba(path
, hba_path
)) {
5761 fd
= dev_open(ent
->d_name
, O_RDONLY
);
5763 fd2devname(fd
, buf
);
5766 pr_err("cannot open device: %s\n",
5771 dv
= xcalloc(1, sizeof(*dv
));
5772 dv
->devname
= xstrdup(buf
);
5779 devlist
= devlist
->next
;
5789 count_volumes_list(struct md_list
*devlist
, char *homehost
,
5790 int verbose
, int *found
)
5792 struct md_list
*tmpdev
;
5794 struct supertype
*st
= NULL
;
5796 /* first walk the list of devices to find a consistent set
5797 * that match the criterea, if that is possible.
5798 * We flag the ones we like with 'used'.
5801 st
= match_metadata_desc_imsm("imsm");
5803 pr_vrb(": cannot allocate memory for imsm supertype\n");
5807 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5808 char *devname
= tmpdev
->devname
;
5810 struct supertype
*tst
;
5812 if (tmpdev
->used
> 1)
5814 tst
= dup_super(st
);
5816 pr_vrb(": cannot allocate memory for imsm supertype\n");
5819 tmpdev
->container
= 0;
5820 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
5822 dprintf("cannot open device %s: %s\n",
5823 devname
, strerror(errno
));
5825 } else if (fstat(dfd
, &stb
)< 0) {
5827 dprintf("fstat failed for %s: %s\n",
5828 devname
, strerror(errno
));
5830 } else if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
5831 dprintf("%s is not a block device.\n",
5834 } else if (must_be_container(dfd
)) {
5835 struct supertype
*cst
;
5836 cst
= super_by_fd(dfd
, NULL
);
5838 dprintf("cannot recognize container type %s\n",
5841 } else if (tst
->ss
!= st
->ss
) {
5842 dprintf("non-imsm container - ignore it: %s\n",
5845 } else if (!tst
->ss
->load_container
||
5846 tst
->ss
->load_container(tst
, dfd
, NULL
))
5849 tmpdev
->container
= 1;
5852 cst
->ss
->free_super(cst
);
5854 tmpdev
->st_rdev
= stb
.st_rdev
;
5855 if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
5856 dprintf("no RAID superblock on %s\n",
5859 } else if (tst
->ss
->compare_super
== NULL
) {
5860 dprintf("Cannot assemble %s metadata on %s\n",
5861 tst
->ss
->name
, devname
);
5867 if (tmpdev
->used
== 2 || tmpdev
->used
== 4) {
5868 /* Ignore unrecognised devices during auto-assembly */
5873 tst
->ss
->getinfo_super(tst
, &info
, NULL
);
5875 if (st
->minor_version
== -1)
5876 st
->minor_version
= tst
->minor_version
;
5878 if (memcmp(info
.uuid
, uuid_zero
,
5879 sizeof(int[4])) == 0) {
5880 /* this is a floating spare. It cannot define
5881 * an array unless there are no more arrays of
5882 * this type to be found. It can be included
5883 * in an array of this type though.
5889 if (st
->ss
!= tst
->ss
||
5890 st
->minor_version
!= tst
->minor_version
||
5891 st
->ss
->compare_super(st
, tst
) != 0) {
5892 /* Some mismatch. If exactly one array matches this host,
5893 * we can resolve on that one.
5894 * Or, if we are auto assembling, we just ignore the second
5897 dprintf("superblock on %s doesn't match others - assembly aborted\n",
5903 dprintf("found: devname: %s\n", devname
);
5907 tst
->ss
->free_super(tst
);
5911 if ((err
= load_super_imsm_all(st
, -1, &st
->sb
, NULL
, devlist
, 0)) == 0) {
5912 struct mdinfo
*iter
, *head
= st
->ss
->container_content(st
, NULL
);
5913 for (iter
= head
; iter
; iter
= iter
->next
) {
5914 dprintf("content->text_version: %s vol\n",
5915 iter
->text_version
);
5916 if (iter
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
5917 /* do not assemble arrays with unsupported
5919 dprintf("Cannot activate member %s.\n",
5920 iter
->text_version
);
5927 dprintf("No valid super block on device list: err: %d %p\n",
5931 dprintf("no more devices to examine\n");
5934 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5935 if ((tmpdev
->used
== 1) && (tmpdev
->found
)) {
5937 if (count
< tmpdev
->found
)
5940 count
-= tmpdev
->found
;
5943 if (tmpdev
->used
== 1)
5948 st
->ss
->free_super(st
);
5953 count_volumes(struct intel_hba
*hba
, int dpa
, int verbose
)
5955 struct sys_dev
*idev
, *intel_devices
= find_intel_devices();
5957 const struct orom_entry
*entry
;
5958 struct devid_list
*dv
, *devid_list
;
5960 if (!hba
|| !hba
->path
)
5963 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
5964 if (strstr(idev
->path
, hba
->path
))
5968 if (!idev
|| !idev
->dev_id
)
5971 entry
= get_orom_entry_by_device_id(idev
->dev_id
);
5973 if (!entry
|| !entry
->devid_list
)
5976 devid_list
= entry
->devid_list
;
5977 for (dv
= devid_list
; dv
; dv
= dv
->next
) {
5978 struct md_list
*devlist
= NULL
;
5979 struct sys_dev
*device
= device_by_id(dv
->devid
);
5984 hba_path
= device
->path
;
5988 /* VMD has one orom entry for all domain, but spanning is not allowed.
5989 * VMD arrays should be counted per domain (controller), so skip
5990 * domains that are not the given one.
5992 if ((hba
->type
== SYS_DEV_VMD
) &&
5993 (strncmp(device
->path
, hba
->path
, strlen(device
->path
)) != 0))
5996 devlist
= get_devices(hba_path
);
5997 /* if no intel devices return zero volumes */
5998 if (devlist
== NULL
)
6001 count
+= active_arrays_by_format("imsm", hba_path
, &devlist
, dpa
, verbose
);
6002 dprintf("path: %s active arrays: %d\n", hba_path
, count
);
6003 if (devlist
== NULL
)
6007 count
+= count_volumes_list(devlist
,
6011 dprintf("found %d count: %d\n", found
, count
);
6014 dprintf("path: %s total number of volumes: %d\n", hba_path
, count
);
6017 struct md_list
*dv
= devlist
;
6018 devlist
= devlist
->next
;
6026 static int imsm_default_chunk(const struct imsm_orom
*orom
)
6028 /* up to 512 if the plaform supports it, otherwise the platform max.
6029 * 128 if no platform detected
6031 int fs
= max(7, orom
? fls(orom
->sss
) : 0);
6033 return min(512, (1 << fs
));
6037 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
6038 int raiddisks
, int *chunk
, unsigned long long size
, int verbose
)
6040 /* check/set platform and metadata limits/defaults */
6041 if (super
->orom
&& raiddisks
> super
->orom
->dpa
) {
6042 pr_vrb(": platform supports a maximum of %d disks per array\n",
6047 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
6048 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
6049 pr_vrb(": platform does not support raid%d with %d disk%s\n",
6050 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
6054 if (*chunk
== 0 || *chunk
== UnSet
)
6055 *chunk
= imsm_default_chunk(super
->orom
);
6057 if (super
->orom
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
6058 pr_vrb(": platform does not support a chunk size of: %d\n", *chunk
);
6062 if (layout
!= imsm_level_to_layout(level
)) {
6064 pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
6065 else if (level
== 10)
6066 pr_vrb(": imsm raid 10 only supports the n2 layout\n");
6068 pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
6073 if (super
->orom
&& (super
->orom
->attr
& IMSM_OROM_ATTR_2TB
) == 0 &&
6074 (calc_array_size(level
, raiddisks
, layout
, *chunk
, size
) >> 32) > 0) {
6075 pr_vrb(": platform does not support a volume size over 2TB\n");
6082 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
6083 * FIX ME add ahci details
6085 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
6086 int layout
, int raiddisks
, int *chunk
,
6087 unsigned long long size
,
6088 unsigned long long data_offset
,
6090 unsigned long long *freesize
,
6094 struct intel_super
*super
= st
->sb
;
6095 struct imsm_super
*mpb
;
6097 unsigned long long pos
= 0;
6098 unsigned long long maxsize
;
6102 /* We must have the container info already read in. */
6106 mpb
= super
->anchor
;
6108 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, size
, verbose
)) {
6109 pr_err("RAID gemetry validation failed. Cannot proceed with the action(s).\n");
6113 /* General test: make sure there is space for
6114 * 'raiddisks' device extents of size 'size' at a given
6117 unsigned long long minsize
= size
;
6118 unsigned long long start_offset
= MaxSector
;
6121 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
6122 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6127 e
= get_extents(super
, dl
);
6130 unsigned long long esize
;
6131 esize
= e
[i
].start
- pos
;
6132 if (esize
>= minsize
)
6134 if (found
&& start_offset
== MaxSector
) {
6137 } else if (found
&& pos
!= start_offset
) {
6141 pos
= e
[i
].start
+ e
[i
].size
;
6143 } while (e
[i
-1].size
);
6148 if (dcnt
< raiddisks
) {
6150 pr_err("imsm: Not enough devices with space for this array (%d < %d)\n",
6157 /* This device must be a member of the set */
6158 if (stat(dev
, &stb
) < 0)
6160 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
6162 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6163 if (dl
->major
== (int)major(stb
.st_rdev
) &&
6164 dl
->minor
== (int)minor(stb
.st_rdev
))
6169 pr_err("%s is not in the same imsm set\n", dev
);
6171 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
6172 /* If a volume is present then the current creation attempt
6173 * cannot incorporate new spares because the orom may not
6174 * understand this configuration (all member disks must be
6175 * members of each array in the container).
6177 pr_err("%s is a spare and a volume is already defined for this container\n", dev
);
6178 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
6180 } else if (super
->orom
&& mpb
->num_raid_devs
> 0 &&
6181 mpb
->num_disks
!= raiddisks
) {
6182 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
6186 /* retrieve the largest free space block */
6187 e
= get_extents(super
, dl
);
6192 unsigned long long esize
;
6194 esize
= e
[i
].start
- pos
;
6195 if (esize
>= maxsize
)
6197 pos
= e
[i
].start
+ e
[i
].size
;
6199 } while (e
[i
-1].size
);
6204 pr_err("unable to determine free space for: %s\n",
6208 if (maxsize
< size
) {
6210 pr_err("%s not enough space (%llu < %llu)\n",
6211 dev
, maxsize
, size
);
6215 /* count total number of extents for merge */
6217 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6219 i
+= dl
->extent_cnt
;
6221 maxsize
= merge_extents(super
, i
);
6223 if (!check_env("IMSM_NO_PLATFORM") &&
6224 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6225 pr_err("attempting to create a second volume with size less then remaining space. Aborting...\n");
6229 if (maxsize
< size
|| maxsize
== 0) {
6232 pr_err("no free space left on device. Aborting...\n");
6234 pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n",
6240 *freesize
= maxsize
;
6243 int count
= count_volumes(super
->hba
,
6244 super
->orom
->dpa
, verbose
);
6245 if (super
->orom
->vphba
<= count
) {
6246 pr_vrb(": platform does not support more than %d raid volumes.\n",
6247 super
->orom
->vphba
);
6254 static int imsm_get_free_size(struct supertype
*st
, int raiddisks
,
6255 unsigned long long size
, int chunk
,
6256 unsigned long long *freesize
)
6258 struct intel_super
*super
= st
->sb
;
6259 struct imsm_super
*mpb
= super
->anchor
;
6264 unsigned long long maxsize
;
6265 unsigned long long minsize
;
6269 /* find the largest common start free region of the possible disks */
6273 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6279 /* don't activate new spares if we are orom constrained
6280 * and there is already a volume active in the container
6282 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
6285 e
= get_extents(super
, dl
);
6288 for (i
= 1; e
[i
-1].size
; i
++)
6296 maxsize
= merge_extents(super
, extent_cnt
);
6300 minsize
= chunk
* 2;
6302 if (cnt
< raiddisks
||
6303 (super
->orom
&& used
&& used
!= raiddisks
) ||
6304 maxsize
< minsize
||
6306 pr_err("not enough devices with space to create array.\n");
6307 return 0; /* No enough free spaces large enough */
6318 if (!check_env("IMSM_NO_PLATFORM") &&
6319 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6320 pr_err("attempting to create a second volume with size less then remaining space. Aborting...\n");
6324 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6326 dl
->raiddisk
= cnt
++;
6330 dprintf("imsm: imsm_get_free_size() returns : %llu\n", size
);
6335 static int reserve_space(struct supertype
*st
, int raiddisks
,
6336 unsigned long long size
, int chunk
,
6337 unsigned long long *freesize
)
6339 struct intel_super
*super
= st
->sb
;
6344 rv
= imsm_get_free_size(st
, raiddisks
, size
, chunk
, freesize
);
6347 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6349 dl
->raiddisk
= cnt
++;
6356 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
6357 int raiddisks
, int *chunk
, unsigned long long size
,
6358 unsigned long long data_offset
,
6359 char *dev
, unsigned long long *freesize
,
6367 * if given unused devices create a container
6368 * if given given devices in a container create a member volume
6370 if (level
== LEVEL_CONTAINER
) {
6371 /* Must be a fresh device to add to a container */
6372 return validate_geometry_imsm_container(st
, level
, layout
,
6382 struct intel_super
*super
= st
->sb
;
6383 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
6384 raiddisks
, chunk
, size
,
6387 /* we are being asked to automatically layout a
6388 * new volume based on the current contents of
6389 * the container. If the the parameters can be
6390 * satisfied reserve_space will record the disks,
6391 * start offset, and size of the volume to be
6392 * created. add_to_super and getinfo_super
6393 * detect when autolayout is in progress.
6395 /* assuming that freesize is always given when array is
6397 if (super
->orom
&& freesize
) {
6399 count
= count_volumes(super
->hba
,
6400 super
->orom
->dpa
, verbose
);
6401 if (super
->orom
->vphba
<= count
) {
6402 pr_vrb(": platform does not support more than %d raid volumes.\n",
6403 super
->orom
->vphba
);
6408 return reserve_space(st
, raiddisks
, size
,
6414 /* creating in a given container */
6415 return validate_geometry_imsm_volume(st
, level
, layout
,
6416 raiddisks
, chunk
, size
,
6418 dev
, freesize
, verbose
);
6421 /* This device needs to be a device in an 'imsm' container */
6422 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
6425 pr_err("Cannot create this array on device %s\n",
6430 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
6432 pr_err("Cannot open %s: %s\n",
6433 dev
, strerror(errno
));
6436 /* Well, it is in use by someone, maybe an 'imsm' container. */
6437 cfd
= open_container(fd
);
6441 pr_err("Cannot use %s: It is busy\n",
6445 sra
= sysfs_read(cfd
, NULL
, GET_VERSION
);
6446 if (sra
&& sra
->array
.major_version
== -1 &&
6447 strcmp(sra
->text_version
, "imsm") == 0)
6451 /* This is a member of a imsm container. Load the container
6452 * and try to create a volume
6454 struct intel_super
*super
;
6456 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
, NULL
, 1) == 0) {
6458 strcpy(st
->container_devnm
, fd2devnm(cfd
));
6460 return validate_geometry_imsm_volume(st
, level
, layout
,
6462 size
, data_offset
, dev
,
6469 pr_err("failed container membership check\n");
6475 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
6477 struct intel_super
*super
= st
->sb
;
6479 if (level
&& *level
== UnSet
)
6480 *level
= LEVEL_CONTAINER
;
6482 if (level
&& layout
&& *layout
== UnSet
)
6483 *layout
= imsm_level_to_layout(*level
);
6485 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0))
6486 *chunk
= imsm_default_chunk(super
->orom
);
6489 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
6491 static int kill_subarray_imsm(struct supertype
*st
)
6493 /* remove the subarray currently referenced by ->current_vol */
6495 struct intel_dev
**dp
;
6496 struct intel_super
*super
= st
->sb
;
6497 __u8 current_vol
= super
->current_vol
;
6498 struct imsm_super
*mpb
= super
->anchor
;
6500 if (super
->current_vol
< 0)
6502 super
->current_vol
= -1; /* invalidate subarray cursor */
6504 /* block deletions that would change the uuid of active subarrays
6506 * FIXME when immutable ids are available, but note that we'll
6507 * also need to fixup the invalidated/active subarray indexes in
6510 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6513 if (i
< current_vol
)
6515 sprintf(subarray
, "%u", i
);
6516 if (is_subarray_active(subarray
, st
->devnm
)) {
6517 pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
6524 if (st
->update_tail
) {
6525 struct imsm_update_kill_array
*u
= xmalloc(sizeof(*u
));
6527 u
->type
= update_kill_array
;
6528 u
->dev_idx
= current_vol
;
6529 append_metadata_update(st
, u
, sizeof(*u
));
6534 for (dp
= &super
->devlist
; *dp
;)
6535 if ((*dp
)->index
== current_vol
) {
6538 handle_missing(super
, (*dp
)->dev
);
6539 if ((*dp
)->index
> current_vol
)
6544 /* no more raid devices, all active components are now spares,
6545 * but of course failed are still failed
6547 if (--mpb
->num_raid_devs
== 0) {
6550 for (d
= super
->disks
; d
; d
= d
->next
)
6555 super
->updates_pending
++;
6560 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
6561 char *update
, struct mddev_ident
*ident
)
6563 /* update the subarray currently referenced by ->current_vol */
6564 struct intel_super
*super
= st
->sb
;
6565 struct imsm_super
*mpb
= super
->anchor
;
6567 if (strcmp(update
, "name") == 0) {
6568 char *name
= ident
->name
;
6572 if (is_subarray_active(subarray
, st
->devnm
)) {
6573 pr_err("Unable to update name of active subarray\n");
6577 if (!check_name(super
, name
, 0))
6580 vol
= strtoul(subarray
, &ep
, 10);
6581 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
6584 if (st
->update_tail
) {
6585 struct imsm_update_rename_array
*u
= xmalloc(sizeof(*u
));
6587 u
->type
= update_rename_array
;
6589 snprintf((char *) u
->name
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6590 append_metadata_update(st
, u
, sizeof(*u
));
6592 struct imsm_dev
*dev
;
6595 dev
= get_imsm_dev(super
, vol
);
6596 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6597 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6598 dev
= get_imsm_dev(super
, i
);
6599 handle_missing(super
, dev
);
6601 super
->updates_pending
++;
6608 #endif /* MDASSEMBLE */
6610 static int is_gen_migration(struct imsm_dev
*dev
)
6615 if (!dev
->vol
.migr_state
)
6618 if (migr_type(dev
) == MIGR_GEN_MIGR
)
6624 static int is_rebuilding(struct imsm_dev
*dev
)
6626 struct imsm_map
*migr_map
;
6628 if (!dev
->vol
.migr_state
)
6631 if (migr_type(dev
) != MIGR_REBUILD
)
6634 migr_map
= get_imsm_map(dev
, MAP_1
);
6636 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
6643 static int is_initializing(struct imsm_dev
*dev
)
6645 struct imsm_map
*migr_map
;
6647 if (!dev
->vol
.migr_state
)
6650 if (migr_type(dev
) != MIGR_INIT
)
6653 migr_map
= get_imsm_map(dev
, MAP_1
);
6655 if (migr_map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
6662 static void update_recovery_start(struct intel_super
*super
,
6663 struct imsm_dev
*dev
,
6664 struct mdinfo
*array
)
6666 struct mdinfo
*rebuild
= NULL
;
6670 if (!is_rebuilding(dev
))
6673 /* Find the rebuild target, but punt on the dual rebuild case */
6674 for (d
= array
->devs
; d
; d
= d
->next
)
6675 if (d
->recovery_start
== 0) {
6682 /* (?) none of the disks are marked with
6683 * IMSM_ORD_REBUILD, so assume they are missing and the
6684 * disk_ord_tbl was not correctly updated
6686 dprintf("failed to locate out-of-sync disk\n");
6690 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
6691 rebuild
->recovery_start
= units
* blocks_per_migr_unit(super
, dev
);
6695 static int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
);
6698 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
6700 /* Given a container loaded by load_super_imsm_all,
6701 * extract information about all the arrays into
6703 * If 'subarray' is given, just extract info about that array.
6705 * For each imsm_dev create an mdinfo, fill it in,
6706 * then look for matching devices in super->disks
6707 * and create appropriate device mdinfo.
6709 struct intel_super
*super
= st
->sb
;
6710 struct imsm_super
*mpb
= super
->anchor
;
6711 struct mdinfo
*rest
= NULL
;
6715 int spare_disks
= 0;
6717 /* do not assemble arrays when not all attributes are supported */
6718 if (imsm_check_attributes(mpb
->attributes
) == 0) {
6720 pr_err("Unsupported attributes in IMSM metadata.Arrays activation is blocked.\n");
6723 /* check for bad blocks */
6724 if (imsm_bbm_log_size(super
->anchor
)) {
6725 pr_err("BBM log found in IMSM metadata.Arrays activation is blocked.\n");
6729 /* count spare devices, not used in maps
6731 for (d
= super
->disks
; d
; d
= d
->next
)
6735 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6736 struct imsm_dev
*dev
;
6737 struct imsm_map
*map
;
6738 struct imsm_map
*map2
;
6739 struct mdinfo
*this;
6747 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
6750 dev
= get_imsm_dev(super
, i
);
6751 map
= get_imsm_map(dev
, MAP_0
);
6752 map2
= get_imsm_map(dev
, MAP_1
);
6754 /* do not publish arrays that are in the middle of an
6755 * unsupported migration
6757 if (dev
->vol
.migr_state
&&
6758 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
6759 pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n",
6763 /* do not publish arrays that are not support by controller's
6767 this = xmalloc(sizeof(*this));
6769 super
->current_vol
= i
;
6770 getinfo_super_imsm_volume(st
, this, NULL
);
6773 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
6774 /* mdadm does not support all metadata features- set the bit in all arrays state */
6775 if (!validate_geometry_imsm_orom(super
,
6776 get_imsm_raid_level(map
), /* RAID level */
6777 imsm_level_to_layout(get_imsm_raid_level(map
)),
6778 map
->num_members
, /* raid disks */
6779 &chunk
, join_u32(dev
->size_low
, dev
->size_high
),
6781 pr_err("IMSM RAID geometry validation failed. Array %s activation is blocked.\n",
6783 this->array
.state
|=
6784 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
6785 (1<<MD_SB_BLOCK_VOLUME
);
6789 /* if array has bad blocks, set suitable bit in all arrays state */
6791 this->array
.state
|=
6792 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
6793 (1<<MD_SB_BLOCK_VOLUME
);
6795 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
6796 unsigned long long recovery_start
;
6797 struct mdinfo
*info_d
;
6804 idx
= get_imsm_disk_idx(dev
, slot
, MAP_0
);
6805 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
6806 for (d
= super
->disks
; d
; d
= d
->next
)
6807 if (d
->index
== idx
)
6810 recovery_start
= MaxSector
;
6813 if (d
&& is_failed(&d
->disk
))
6815 if (ord
& IMSM_ORD_REBUILD
)
6819 * if we skip some disks the array will be assmebled degraded;
6820 * reset resync start to avoid a dirty-degraded
6821 * situation when performing the intial sync
6823 * FIXME handle dirty degraded
6825 if ((skip
|| recovery_start
== 0) && !dev
->vol
.dirty
)
6826 this->resync_start
= MaxSector
;
6830 info_d
= xcalloc(1, sizeof(*info_d
));
6831 info_d
->next
= this->devs
;
6832 this->devs
= info_d
;
6834 info_d
->disk
.number
= d
->index
;
6835 info_d
->disk
.major
= d
->major
;
6836 info_d
->disk
.minor
= d
->minor
;
6837 info_d
->disk
.raid_disk
= slot
;
6838 info_d
->recovery_start
= recovery_start
;
6840 if (slot
< map2
->num_members
)
6841 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
6843 this->array
.spare_disks
++;
6845 if (slot
< map
->num_members
)
6846 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
6848 this->array
.spare_disks
++;
6850 if (info_d
->recovery_start
== MaxSector
)
6851 this->array
.working_disks
++;
6853 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
6854 info_d
->data_offset
= pba_of_lba0(map
);
6855 info_d
->component_size
= blocks_per_member(map
);
6857 /* now that the disk list is up-to-date fixup recovery_start */
6858 update_recovery_start(super
, dev
, this);
6859 this->array
.spare_disks
+= spare_disks
;
6862 /* check for reshape */
6863 if (this->reshape_active
== 1)
6864 recover_backup_imsm(st
, this);
6872 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
6873 int failed
, int look_in_map
)
6875 struct imsm_map
*map
;
6877 map
= get_imsm_map(dev
, look_in_map
);
6880 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
6881 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
6883 switch (get_imsm_raid_level(map
)) {
6885 return IMSM_T_STATE_FAILED
;
6888 if (failed
< map
->num_members
)
6889 return IMSM_T_STATE_DEGRADED
;
6891 return IMSM_T_STATE_FAILED
;
6896 * check to see if any mirrors have failed, otherwise we
6897 * are degraded. Even numbered slots are mirrored on
6901 /* gcc -Os complains that this is unused */
6902 int insync
= insync
;
6904 for (i
= 0; i
< map
->num_members
; i
++) {
6905 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_X
);
6906 int idx
= ord_to_idx(ord
);
6907 struct imsm_disk
*disk
;
6909 /* reset the potential in-sync count on even-numbered
6910 * slots. num_copies is always 2 for imsm raid10
6915 disk
= get_imsm_disk(super
, idx
);
6916 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
6919 /* no in-sync disks left in this mirror the
6923 return IMSM_T_STATE_FAILED
;
6926 return IMSM_T_STATE_DEGRADED
;
6930 return IMSM_T_STATE_DEGRADED
;
6932 return IMSM_T_STATE_FAILED
;
6938 return map
->map_state
;
6941 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
6946 struct imsm_disk
*disk
;
6947 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6948 struct imsm_map
*prev
= get_imsm_map(dev
, MAP_1
);
6949 struct imsm_map
*map_for_loop
;
6954 /* at the beginning of migration we set IMSM_ORD_REBUILD on
6955 * disks that are being rebuilt. New failures are recorded to
6956 * map[0]. So we look through all the disks we started with and
6957 * see if any failures are still present, or if any new ones
6961 if (prev
&& (map
->num_members
< prev
->num_members
))
6962 map_for_loop
= prev
;
6964 for (i
= 0; i
< map_for_loop
->num_members
; i
++) {
6966 /* when MAP_X is passed both maps failures are counted
6969 ((look_in_map
== MAP_1
) || (look_in_map
== MAP_X
)) &&
6970 (i
< prev
->num_members
)) {
6971 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
6972 idx_1
= ord_to_idx(ord
);
6974 disk
= get_imsm_disk(super
, idx_1
);
6975 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
6978 if (((look_in_map
== MAP_0
) || (look_in_map
== MAP_X
)) &&
6979 (i
< map
->num_members
)) {
6980 ord
= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
6981 idx
= ord_to_idx(ord
);
6984 disk
= get_imsm_disk(super
, idx
);
6985 if (!disk
|| is_failed(disk
) ||
6986 ord
& IMSM_ORD_REBUILD
)
6996 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
6999 struct intel_super
*super
= c
->sb
;
7000 struct imsm_super
*mpb
= super
->anchor
;
7002 if (atoi(inst
) >= mpb
->num_raid_devs
) {
7003 pr_err("subarry index %d, out of range\n", atoi(inst
));
7007 dprintf("imsm: open_new %s\n", inst
);
7008 a
->info
.container_member
= atoi(inst
);
7012 static int is_resyncing(struct imsm_dev
*dev
)
7014 struct imsm_map
*migr_map
;
7016 if (!dev
->vol
.migr_state
)
7019 if (migr_type(dev
) == MIGR_INIT
||
7020 migr_type(dev
) == MIGR_REPAIR
)
7023 if (migr_type(dev
) == MIGR_GEN_MIGR
)
7026 migr_map
= get_imsm_map(dev
, MAP_1
);
7028 if ((migr_map
->map_state
== IMSM_T_STATE_NORMAL
) &&
7029 (dev
->vol
.migr_type
!= MIGR_GEN_MIGR
))
7035 /* return true if we recorded new information */
7036 static int mark_failure(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
7040 struct imsm_map
*map
;
7041 char buf
[MAX_RAID_SERIAL_LEN
+3];
7042 unsigned int len
, shift
= 0;
7044 /* new failures are always set in map[0] */
7045 map
= get_imsm_map(dev
, MAP_0
);
7047 slot
= get_imsm_disk_slot(map
, idx
);
7051 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
7052 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
7055 memcpy(buf
, disk
->serial
, MAX_RAID_SERIAL_LEN
);
7056 buf
[MAX_RAID_SERIAL_LEN
] = '\000';
7058 if ((len
= strlen(buf
)) >= MAX_RAID_SERIAL_LEN
)
7059 shift
= len
- MAX_RAID_SERIAL_LEN
+ 1;
7060 strncpy((char *)disk
->serial
, &buf
[shift
], MAX_RAID_SERIAL_LEN
);
7062 disk
->status
|= FAILED_DISK
;
7063 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
7064 /* mark failures in second map if second map exists and this disk
7066 * This is valid for migration, initialization and rebuild
7068 if (dev
->vol
.migr_state
) {
7069 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
7070 int slot2
= get_imsm_disk_slot(map2
, idx
);
7072 if ((slot2
< map2
->num_members
) &&
7074 set_imsm_ord_tbl_ent(map2
, slot2
,
7075 idx
| IMSM_ORD_REBUILD
);
7077 if (map
->failed_disk_num
== 0xff)
7078 map
->failed_disk_num
= slot
;
7082 static void mark_missing(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
7084 mark_failure(dev
, disk
, idx
);
7086 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
7089 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
7090 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
7093 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
7097 if (!super
->missing
)
7100 /* When orom adds replacement for missing disk it does
7101 * not remove entry of missing disk, but just updates map with
7102 * new added disk. So it is not enough just to test if there is
7103 * any missing disk, we have to look if there are any failed disks
7104 * in map to stop migration */
7106 dprintf("imsm: mark missing\n");
7107 /* end process for initialization and rebuild only
7109 if (is_gen_migration(dev
) == 0) {
7113 failed
= imsm_count_failed(super
, dev
, MAP_0
);
7114 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7117 end_migration(dev
, super
, map_state
);
7119 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
7120 mark_missing(dev
, &dl
->disk
, dl
->index
);
7121 super
->updates_pending
++;
7124 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
,
7127 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
7128 unsigned long long array_blocks
;
7129 struct imsm_map
*map
;
7131 if (used_disks
== 0) {
7132 /* when problems occures
7133 * return current array_blocks value
7135 array_blocks
= __le32_to_cpu(dev
->size_high
);
7136 array_blocks
= array_blocks
<< 32;
7137 array_blocks
+= __le32_to_cpu(dev
->size_low
);
7139 return array_blocks
;
7142 /* set array size in metadata
7144 if (new_size
<= 0) {
7145 /* OLCE size change is caused by added disks
7147 map
= get_imsm_map(dev
, MAP_0
);
7148 array_blocks
= blocks_per_member(map
) * used_disks
;
7150 /* Online Volume Size Change
7151 * Using available free space
7153 array_blocks
= new_size
;
7156 /* round array size down to closest MB
7158 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
7159 dev
->size_low
= __cpu_to_le32((__u32
)array_blocks
);
7160 dev
->size_high
= __cpu_to_le32((__u32
)(array_blocks
>> 32));
7162 return array_blocks
;
7165 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
7167 static void imsm_progress_container_reshape(struct intel_super
*super
)
7169 /* if no device has a migr_state, but some device has a
7170 * different number of members than the previous device, start
7171 * changing the number of devices in this device to match
7174 struct imsm_super
*mpb
= super
->anchor
;
7175 int prev_disks
= -1;
7179 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7180 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
7181 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7182 struct imsm_map
*map2
;
7183 int prev_num_members
;
7185 if (dev
->vol
.migr_state
)
7188 if (prev_disks
== -1)
7189 prev_disks
= map
->num_members
;
7190 if (prev_disks
== map
->num_members
)
7193 /* OK, this array needs to enter reshape mode.
7194 * i.e it needs a migr_state
7197 copy_map_size
= sizeof_imsm_map(map
);
7198 prev_num_members
= map
->num_members
;
7199 map
->num_members
= prev_disks
;
7200 dev
->vol
.migr_state
= 1;
7201 dev
->vol
.curr_migr_unit
= 0;
7202 set_migr_type(dev
, MIGR_GEN_MIGR
);
7203 for (i
= prev_num_members
;
7204 i
< map
->num_members
; i
++)
7205 set_imsm_ord_tbl_ent(map
, i
, i
);
7206 map2
= get_imsm_map(dev
, MAP_1
);
7207 /* Copy the current map */
7208 memcpy(map2
, map
, copy_map_size
);
7209 map2
->num_members
= prev_num_members
;
7211 imsm_set_array_size(dev
, -1);
7212 super
->clean_migration_record_by_mdmon
= 1;
7213 super
->updates_pending
++;
7217 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
7218 * states are handled in imsm_set_disk() with one exception, when a
7219 * resync is stopped due to a new failure this routine will set the
7220 * 'degraded' state for the array.
7222 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
7224 int inst
= a
->info
.container_member
;
7225 struct intel_super
*super
= a
->container
->sb
;
7226 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7227 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7228 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
7229 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7230 __u32 blocks_per_unit
;
7232 if (dev
->vol
.migr_state
&&
7233 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
7234 /* array state change is blocked due to reshape action
7236 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
7237 * - finish the reshape (if last_checkpoint is big and action != reshape)
7238 * - update curr_migr_unit
7240 if (a
->curr_action
== reshape
) {
7241 /* still reshaping, maybe update curr_migr_unit */
7242 goto mark_checkpoint
;
7244 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
7245 /* for some reason we aborted the reshape.
7247 * disable automatic metadata rollback
7248 * user action is required to recover process
7251 struct imsm_map
*map2
=
7252 get_imsm_map(dev
, MAP_1
);
7253 dev
->vol
.migr_state
= 0;
7254 set_migr_type(dev
, 0);
7255 dev
->vol
.curr_migr_unit
= 0;
7257 sizeof_imsm_map(map2
));
7258 super
->updates_pending
++;
7261 if (a
->last_checkpoint
>= a
->info
.component_size
) {
7262 unsigned long long array_blocks
;
7266 used_disks
= imsm_num_data_members(dev
, MAP_0
);
7267 if (used_disks
> 0) {
7269 blocks_per_member(map
) *
7271 /* round array size down to closest MB
7273 array_blocks
= (array_blocks
7274 >> SECT_PER_MB_SHIFT
)
7275 << SECT_PER_MB_SHIFT
;
7276 a
->info
.custom_array_size
= array_blocks
;
7277 /* encourage manager to update array
7281 a
->check_reshape
= 1;
7283 /* finalize online capacity expansion/reshape */
7284 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
7286 mdi
->disk
.raid_disk
,
7289 imsm_progress_container_reshape(super
);
7294 /* before we activate this array handle any missing disks */
7295 if (consistent
== 2)
7296 handle_missing(super
, dev
);
7298 if (consistent
== 2 &&
7299 (!is_resync_complete(&a
->info
) ||
7300 map_state
!= IMSM_T_STATE_NORMAL
||
7301 dev
->vol
.migr_state
))
7304 if (is_resync_complete(&a
->info
)) {
7305 /* complete intialization / resync,
7306 * recovery and interrupted recovery is completed in
7309 if (is_resyncing(dev
)) {
7310 dprintf("imsm: mark resync done\n");
7311 end_migration(dev
, super
, map_state
);
7312 super
->updates_pending
++;
7313 a
->last_checkpoint
= 0;
7315 } else if ((!is_resyncing(dev
) && !failed
) &&
7316 (imsm_reshape_blocks_arrays_changes(super
) == 0)) {
7317 /* mark the start of the init process if nothing is failed */
7318 dprintf("imsm: mark resync start\n");
7319 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
7320 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
7322 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
7323 super
->updates_pending
++;
7327 /* skip checkpointing for general migration,
7328 * it is controlled in mdadm
7330 if (is_gen_migration(dev
))
7331 goto skip_mark_checkpoint
;
7333 /* check if we can update curr_migr_unit from resync_start, recovery_start */
7334 blocks_per_unit
= blocks_per_migr_unit(super
, dev
);
7335 if (blocks_per_unit
) {
7339 units
= a
->last_checkpoint
/ blocks_per_unit
;
7342 /* check that we did not overflow 32-bits, and that
7343 * curr_migr_unit needs updating
7345 if (units32
== units
&&
7347 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
7348 dprintf("imsm: mark checkpoint (%u)\n", units32
);
7349 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
7350 super
->updates_pending
++;
7354 skip_mark_checkpoint
:
7355 /* mark dirty / clean */
7356 if (dev
->vol
.dirty
!= !consistent
) {
7357 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
7362 super
->updates_pending
++;
7368 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
7370 int inst
= a
->info
.container_member
;
7371 struct intel_super
*super
= a
->container
->sb
;
7372 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7373 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7374 struct imsm_disk
*disk
;
7376 int recovery_not_finished
= 0;
7381 if (n
> map
->num_members
)
7382 pr_err("imsm: set_disk %d out of range 0..%d\n",
7383 n
, map
->num_members
- 1);
7388 dprintf("imsm: set_disk %d:%x\n", n
, state
);
7390 ord
= get_imsm_ord_tbl_ent(dev
, n
, MAP_0
);
7391 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
7393 /* check for new failures */
7394 if (state
& DS_FAULTY
) {
7395 if (mark_failure(dev
, disk
, ord_to_idx(ord
)))
7396 super
->updates_pending
++;
7399 /* check if in_sync */
7400 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
7401 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
7403 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
7404 super
->updates_pending
++;
7407 failed
= imsm_count_failed(super
, dev
, MAP_0
);
7408 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7410 /* check if recovery complete, newly degraded, or failed */
7411 dprintf("imsm: Detected transition to state ");
7412 switch (map_state
) {
7413 case IMSM_T_STATE_NORMAL
: /* transition to normal state */
7414 dprintf("normal: ");
7415 if (is_rebuilding(dev
)) {
7416 dprintf_cont("while rebuilding");
7417 /* check if recovery is really finished */
7418 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
7419 if (mdi
->recovery_start
!= MaxSector
) {
7420 recovery_not_finished
= 1;
7423 if (recovery_not_finished
) {
7425 dprintf("Rebuild has not finished yet, state not changed");
7426 if (a
->last_checkpoint
< mdi
->recovery_start
) {
7427 a
->last_checkpoint
= mdi
->recovery_start
;
7428 super
->updates_pending
++;
7432 end_migration(dev
, super
, map_state
);
7433 map
= get_imsm_map(dev
, MAP_0
);
7434 map
->failed_disk_num
= ~0;
7435 super
->updates_pending
++;
7436 a
->last_checkpoint
= 0;
7439 if (is_gen_migration(dev
)) {
7440 dprintf_cont("while general migration");
7441 if (a
->last_checkpoint
>= a
->info
.component_size
)
7442 end_migration(dev
, super
, map_state
);
7444 map
->map_state
= map_state
;
7445 map
= get_imsm_map(dev
, MAP_0
);
7446 map
->failed_disk_num
= ~0;
7447 super
->updates_pending
++;
7451 case IMSM_T_STATE_DEGRADED
: /* transition to degraded state */
7452 dprintf_cont("degraded: ");
7453 if ((map
->map_state
!= map_state
) &&
7454 !dev
->vol
.migr_state
) {
7455 dprintf_cont("mark degraded");
7456 map
->map_state
= map_state
;
7457 super
->updates_pending
++;
7458 a
->last_checkpoint
= 0;
7461 if (is_rebuilding(dev
)) {
7462 dprintf_cont("while rebuilding.");
7463 if (map
->map_state
!= map_state
) {
7464 dprintf_cont(" Map state change");
7465 end_migration(dev
, super
, map_state
);
7466 super
->updates_pending
++;
7470 if (is_gen_migration(dev
)) {
7471 dprintf_cont("while general migration");
7472 if (a
->last_checkpoint
>= a
->info
.component_size
)
7473 end_migration(dev
, super
, map_state
);
7475 map
->map_state
= map_state
;
7476 manage_second_map(super
, dev
);
7478 super
->updates_pending
++;
7481 if (is_initializing(dev
)) {
7482 dprintf_cont("while initialization.");
7483 map
->map_state
= map_state
;
7484 super
->updates_pending
++;
7488 case IMSM_T_STATE_FAILED
: /* transition to failed state */
7489 dprintf_cont("failed: ");
7490 if (is_gen_migration(dev
)) {
7491 dprintf_cont("while general migration");
7492 map
->map_state
= map_state
;
7493 super
->updates_pending
++;
7496 if (map
->map_state
!= map_state
) {
7497 dprintf_cont("mark failed");
7498 end_migration(dev
, super
, map_state
);
7499 super
->updates_pending
++;
7500 a
->last_checkpoint
= 0;
7505 dprintf_cont("state %i\n", map_state
);
7510 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
7513 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
7514 unsigned long long dsize
;
7515 unsigned long long sectors
;
7517 get_dev_size(fd
, NULL
, &dsize
);
7519 if (mpb_size
> 512) {
7520 /* -1 to account for anchor */
7521 sectors
= mpb_sectors(mpb
) - 1;
7523 /* write the extended mpb to the sectors preceeding the anchor */
7524 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0)
7527 if ((unsigned long long)write(fd
, buf
+ 512, 512 * sectors
)
7532 /* first block is stored on second to last sector of the disk */
7533 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0)
7536 if (write(fd
, buf
, 512) != 512)
7542 static void imsm_sync_metadata(struct supertype
*container
)
7544 struct intel_super
*super
= container
->sb
;
7546 dprintf("sync metadata: %d\n", super
->updates_pending
);
7547 if (!super
->updates_pending
)
7550 write_super_imsm(container
, 0);
7552 super
->updates_pending
= 0;
7555 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
7557 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7558 int i
= get_imsm_disk_idx(dev
, idx
, MAP_X
);
7561 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7565 if (dl
&& is_failed(&dl
->disk
))
7569 dprintf("found %x:%x\n", dl
->major
, dl
->minor
);
7574 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
7575 struct active_array
*a
, int activate_new
,
7576 struct mdinfo
*additional_test_list
)
7578 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7579 int idx
= get_imsm_disk_idx(dev
, slot
, MAP_X
);
7580 struct imsm_super
*mpb
= super
->anchor
;
7581 struct imsm_map
*map
;
7582 unsigned long long pos
;
7587 __u32 array_start
= 0;
7588 __u32 array_end
= 0;
7590 struct mdinfo
*test_list
;
7592 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7593 /* If in this array, skip */
7594 for (d
= a
->info
.devs
; d
; d
= d
->next
)
7595 if (d
->state_fd
>= 0 &&
7596 d
->disk
.major
== dl
->major
&&
7597 d
->disk
.minor
== dl
->minor
) {
7598 dprintf("%x:%x already in array\n",
7599 dl
->major
, dl
->minor
);
7604 test_list
= additional_test_list
;
7606 if (test_list
->disk
.major
== dl
->major
&&
7607 test_list
->disk
.minor
== dl
->minor
) {
7608 dprintf("%x:%x already in additional test list\n",
7609 dl
->major
, dl
->minor
);
7612 test_list
= test_list
->next
;
7617 /* skip in use or failed drives */
7618 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
7620 dprintf("%x:%x status (failed: %d index: %d)\n",
7621 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
7625 /* skip pure spares when we are looking for partially
7626 * assimilated drives
7628 if (dl
->index
== -1 && !activate_new
)
7631 /* Does this unused device have the requisite free space?
7632 * It needs to be able to cover all member volumes
7634 ex
= get_extents(super
, dl
);
7636 dprintf("cannot get extents\n");
7639 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7640 dev
= get_imsm_dev(super
, i
);
7641 map
= get_imsm_map(dev
, MAP_0
);
7643 /* check if this disk is already a member of
7646 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
7652 array_start
= pba_of_lba0(map
);
7653 array_end
= array_start
+
7654 blocks_per_member(map
) - 1;
7657 /* check that we can start at pba_of_lba0 with
7658 * blocks_per_member of space
7660 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
7664 pos
= ex
[j
].start
+ ex
[j
].size
;
7666 } while (ex
[j
-1].size
);
7673 if (i
< mpb
->num_raid_devs
) {
7674 dprintf("%x:%x does not have %u to %u available\n",
7675 dl
->major
, dl
->minor
, array_start
, array_end
);
7685 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
7687 struct imsm_dev
*dev2
;
7688 struct imsm_map
*map
;
7694 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
7696 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
, MAP_0
);
7697 if (state
== IMSM_T_STATE_FAILED
) {
7698 map
= get_imsm_map(dev2
, MAP_0
);
7701 for (slot
= 0; slot
< map
->num_members
; slot
++) {
7703 * Check if failed disks are deleted from intel
7704 * disk list or are marked to be deleted
7706 idx
= get_imsm_disk_idx(dev2
, slot
, MAP_X
);
7707 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
7709 * Do not rebuild the array if failed disks
7710 * from failed sub-array are not removed from
7714 is_failed(&idisk
->disk
) &&
7715 (idisk
->action
!= DISK_REMOVE
))
7723 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
7724 struct metadata_update
**updates
)
7727 * Find a device with unused free space and use it to replace a
7728 * failed/vacant region in an array. We replace failed regions one a
7729 * array at a time. The result is that a new spare disk will be added
7730 * to the first failed array and after the monitor has finished
7731 * propagating failures the remainder will be consumed.
7733 * FIXME add a capability for mdmon to request spares from another
7737 struct intel_super
*super
= a
->container
->sb
;
7738 int inst
= a
->info
.container_member
;
7739 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7740 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7741 int failed
= a
->info
.array
.raid_disks
;
7742 struct mdinfo
*rv
= NULL
;
7745 struct metadata_update
*mu
;
7747 struct imsm_update_activate_spare
*u
;
7752 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
7753 if ((d
->curr_state
& DS_FAULTY
) &&
7755 /* wait for Removal to happen */
7757 if (d
->state_fd
>= 0)
7761 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
7762 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
7764 if (imsm_reshape_blocks_arrays_changes(super
))
7767 /* Cannot activate another spare if rebuild is in progress already
7769 if (is_rebuilding(dev
)) {
7770 dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n");
7774 if (a
->info
.array
.level
== 4)
7775 /* No repair for takeovered array
7776 * imsm doesn't support raid4
7780 if (imsm_check_degraded(super
, dev
, failed
, MAP_0
) !=
7781 IMSM_T_STATE_DEGRADED
)
7785 * If there are any failed disks check state of the other volume.
7786 * Block rebuild if the another one is failed until failed disks
7787 * are removed from container.
7790 dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n",
7791 MAX_RAID_SERIAL_LEN
, dev
->volume
);
7792 /* check if states of the other volumes allow for rebuild */
7793 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
7795 allowed
= imsm_rebuild_allowed(a
->container
,
7803 /* For each slot, if it is not working, find a spare */
7804 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
7805 for (d
= a
->info
.devs
; d
; d
= d
->next
)
7806 if (d
->disk
.raid_disk
== i
)
7808 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
7809 if (d
&& (d
->state_fd
>= 0))
7813 * OK, this device needs recovery. Try to re-add the
7814 * previous occupant of this slot, if this fails see if
7815 * we can continue the assimilation of a spare that was
7816 * partially assimilated, finally try to activate a new
7819 dl
= imsm_readd(super
, i
, a
);
7821 dl
= imsm_add_spare(super
, i
, a
, 0, rv
);
7823 dl
= imsm_add_spare(super
, i
, a
, 1, rv
);
7827 /* found a usable disk with enough space */
7828 di
= xcalloc(1, sizeof(*di
));
7830 /* dl->index will be -1 in the case we are activating a
7831 * pristine spare. imsm_process_update() will create a
7832 * new index in this case. Once a disk is found to be
7833 * failed in all member arrays it is kicked from the
7836 di
->disk
.number
= dl
->index
;
7838 /* (ab)use di->devs to store a pointer to the device
7841 di
->devs
= (struct mdinfo
*) dl
;
7843 di
->disk
.raid_disk
= i
;
7844 di
->disk
.major
= dl
->major
;
7845 di
->disk
.minor
= dl
->minor
;
7847 di
->recovery_start
= 0;
7848 di
->data_offset
= pba_of_lba0(map
);
7849 di
->component_size
= a
->info
.component_size
;
7850 di
->container_member
= inst
;
7851 super
->random
= random32();
7855 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
7856 i
, di
->data_offset
);
7860 /* No spares found */
7862 /* Now 'rv' has a list of devices to return.
7863 * Create a metadata_update record to update the
7864 * disk_ord_tbl for the array
7866 mu
= xmalloc(sizeof(*mu
));
7867 mu
->buf
= xcalloc(num_spares
,
7868 sizeof(struct imsm_update_activate_spare
));
7870 mu
->space_list
= NULL
;
7871 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
7872 mu
->next
= *updates
;
7873 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
7875 for (di
= rv
; di
; di
= di
->next
) {
7876 u
->type
= update_activate_spare
;
7877 u
->dl
= (struct dl
*) di
->devs
;
7879 u
->slot
= di
->disk
.raid_disk
;
7890 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
7892 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
7893 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7894 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, MAP_0
);
7895 struct disk_info
*inf
= get_disk_info(u
);
7896 struct imsm_disk
*disk
;
7900 for (i
= 0; i
< map
->num_members
; i
++) {
7901 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, MAP_X
));
7902 for (j
= 0; j
< new_map
->num_members
; j
++)
7903 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
7910 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
7912 struct dl
*dl
= NULL
;
7913 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7914 if ((dl
->major
== major
) && (dl
->minor
== minor
))
7919 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
7921 struct dl
*prev
= NULL
;
7925 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7926 if ((dl
->major
== major
) && (dl
->minor
== minor
)) {
7929 prev
->next
= dl
->next
;
7931 super
->disks
= dl
->next
;
7933 __free_imsm_disk(dl
);
7934 dprintf("removed %x:%x\n", major
, minor
);
7942 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
7944 static int add_remove_disk_update(struct intel_super
*super
)
7946 int check_degraded
= 0;
7947 struct dl
*disk
= NULL
;
7948 /* add/remove some spares to/from the metadata/contrainer */
7949 while (super
->disk_mgmt_list
) {
7950 struct dl
*disk_cfg
;
7952 disk_cfg
= super
->disk_mgmt_list
;
7953 super
->disk_mgmt_list
= disk_cfg
->next
;
7954 disk_cfg
->next
= NULL
;
7956 if (disk_cfg
->action
== DISK_ADD
) {
7957 disk_cfg
->next
= super
->disks
;
7958 super
->disks
= disk_cfg
;
7960 dprintf("added %x:%x\n",
7961 disk_cfg
->major
, disk_cfg
->minor
);
7962 } else if (disk_cfg
->action
== DISK_REMOVE
) {
7963 dprintf("Disk remove action processed: %x.%x\n",
7964 disk_cfg
->major
, disk_cfg
->minor
);
7965 disk
= get_disk_super(super
,
7969 /* store action status */
7970 disk
->action
= DISK_REMOVE
;
7971 /* remove spare disks only */
7972 if (disk
->index
== -1) {
7973 remove_disk_super(super
,
7978 /* release allocate disk structure */
7979 __free_imsm_disk(disk_cfg
);
7982 return check_degraded
;
7985 static int apply_reshape_migration_update(struct imsm_update_reshape_migration
*u
,
7986 struct intel_super
*super
,
7989 struct intel_dev
*id
;
7990 void **tofree
= NULL
;
7993 dprintf("(enter)\n");
7994 if ((u
->subdev
< 0) ||
7996 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
7999 if ((space_list
== NULL
) || (*space_list
== NULL
)) {
8000 dprintf("imsm: Error: Memory is not allocated\n");
8004 for (id
= super
->devlist
; id
; id
= id
->next
) {
8005 if (id
->index
== (unsigned)u
->subdev
) {
8006 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
8007 struct imsm_map
*map
;
8008 struct imsm_dev
*new_dev
=
8009 (struct imsm_dev
*)*space_list
;
8010 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
8012 struct dl
*new_disk
;
8014 if (new_dev
== NULL
)
8016 *space_list
= **space_list
;
8017 memcpy(new_dev
, dev
, sizeof_imsm_dev(dev
, 0));
8018 map
= get_imsm_map(new_dev
, MAP_0
);
8020 dprintf("imsm: Error: migration in progress");
8024 to_state
= map
->map_state
;
8025 if ((u
->new_level
== 5) && (map
->raid_level
== 0)) {
8027 /* this should not happen */
8028 if (u
->new_disks
[0] < 0) {
8029 map
->failed_disk_num
=
8030 map
->num_members
- 1;
8031 to_state
= IMSM_T_STATE_DEGRADED
;
8033 to_state
= IMSM_T_STATE_NORMAL
;
8035 migrate(new_dev
, super
, to_state
, MIGR_GEN_MIGR
);
8036 if (u
->new_level
> -1)
8037 map
->raid_level
= u
->new_level
;
8038 migr_map
= get_imsm_map(new_dev
, MAP_1
);
8039 if ((u
->new_level
== 5) &&
8040 (migr_map
->raid_level
== 0)) {
8041 int ord
= map
->num_members
- 1;
8042 migr_map
->num_members
--;
8043 if (u
->new_disks
[0] < 0)
8044 ord
|= IMSM_ORD_REBUILD
;
8045 set_imsm_ord_tbl_ent(map
,
8046 map
->num_members
- 1,
8050 tofree
= (void **)dev
;
8052 /* update chunk size
8054 if (u
->new_chunksize
> 0)
8055 map
->blocks_per_strip
=
8056 __cpu_to_le16(u
->new_chunksize
* 2);
8060 if ((u
->new_level
!= 5) ||
8061 (migr_map
->raid_level
!= 0) ||
8062 (migr_map
->raid_level
== map
->raid_level
))
8065 if (u
->new_disks
[0] >= 0) {
8068 new_disk
= get_disk_super(super
,
8069 major(u
->new_disks
[0]),
8070 minor(u
->new_disks
[0]));
8071 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
8072 major(u
->new_disks
[0]),
8073 minor(u
->new_disks
[0]),
8074 new_disk
, new_disk
->index
);
8075 if (new_disk
== NULL
)
8076 goto error_disk_add
;
8078 new_disk
->index
= map
->num_members
- 1;
8079 /* slot to fill in autolayout
8081 new_disk
->raiddisk
= new_disk
->index
;
8082 new_disk
->disk
.status
|= CONFIGURED_DISK
;
8083 new_disk
->disk
.status
&= ~SPARE_DISK
;
8085 goto error_disk_add
;
8088 *tofree
= *space_list
;
8089 /* calculate new size
8091 imsm_set_array_size(new_dev
, -1);
8098 *space_list
= tofree
;
8102 dprintf("Error: imsm: Cannot find disk.\n");
8106 static int apply_size_change_update(struct imsm_update_size_change
*u
,
8107 struct intel_super
*super
)
8109 struct intel_dev
*id
;
8112 dprintf("(enter)\n");
8113 if ((u
->subdev
< 0) ||
8115 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
8119 for (id
= super
->devlist
; id
; id
= id
->next
) {
8120 if (id
->index
== (unsigned)u
->subdev
) {
8121 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
8122 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8123 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
8124 unsigned long long blocks_per_member
;
8126 /* calculate new size
8128 blocks_per_member
= u
->new_size
/ used_disks
;
8129 dprintf("(size: %llu, blocks per member: %llu)\n",
8130 u
->new_size
, blocks_per_member
);
8131 set_blocks_per_member(map
, blocks_per_member
);
8132 imsm_set_array_size(dev
, u
->new_size
);
8142 static int apply_update_activate_spare(struct imsm_update_activate_spare
*u
,
8143 struct intel_super
*super
,
8144 struct active_array
*active_array
)
8146 struct imsm_super
*mpb
= super
->anchor
;
8147 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
8148 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8149 struct imsm_map
*migr_map
;
8150 struct active_array
*a
;
8151 struct imsm_disk
*disk
;
8158 int second_map_created
= 0;
8160 for (; u
; u
= u
->next
) {
8161 victim
= get_imsm_disk_idx(dev
, u
->slot
, MAP_X
);
8166 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
8171 pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n",
8176 /* count failures (excluding rebuilds and the victim)
8177 * to determine map[0] state
8180 for (i
= 0; i
< map
->num_members
; i
++) {
8183 disk
= get_imsm_disk(super
,
8184 get_imsm_disk_idx(dev
, i
, MAP_X
));
8185 if (!disk
|| is_failed(disk
))
8189 /* adding a pristine spare, assign a new index */
8190 if (dl
->index
< 0) {
8191 dl
->index
= super
->anchor
->num_disks
;
8192 super
->anchor
->num_disks
++;
8195 disk
->status
|= CONFIGURED_DISK
;
8196 disk
->status
&= ~SPARE_DISK
;
8199 to_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8200 if (!second_map_created
) {
8201 second_map_created
= 1;
8202 map
->map_state
= IMSM_T_STATE_DEGRADED
;
8203 migrate(dev
, super
, to_state
, MIGR_REBUILD
);
8205 map
->map_state
= to_state
;
8206 migr_map
= get_imsm_map(dev
, MAP_1
);
8207 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
8208 set_imsm_ord_tbl_ent(migr_map
, u
->slot
,
8209 dl
->index
| IMSM_ORD_REBUILD
);
8211 /* update the family_num to mark a new container
8212 * generation, being careful to record the existing
8213 * family_num in orig_family_num to clean up after
8214 * earlier mdadm versions that neglected to set it.
8216 if (mpb
->orig_family_num
== 0)
8217 mpb
->orig_family_num
= mpb
->family_num
;
8218 mpb
->family_num
+= super
->random
;
8220 /* count arrays using the victim in the metadata */
8222 for (a
= active_array
; a
; a
= a
->next
) {
8223 dev
= get_imsm_dev(super
, a
->info
.container_member
);
8224 map
= get_imsm_map(dev
, MAP_0
);
8226 if (get_imsm_disk_slot(map
, victim
) >= 0)
8230 /* delete the victim if it is no longer being
8236 /* We know that 'manager' isn't touching anything,
8237 * so it is safe to delete
8239 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
8240 if ((*dlp
)->index
== victim
)
8243 /* victim may be on the missing list */
8245 for (dlp
= &super
->missing
; *dlp
;
8246 dlp
= &(*dlp
)->next
)
8247 if ((*dlp
)->index
== victim
)
8249 imsm_delete(super
, dlp
, victim
);
8256 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
8257 struct intel_super
*super
,
8260 struct dl
*new_disk
;
8261 struct intel_dev
*id
;
8263 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
8264 int disk_count
= u
->old_raid_disks
;
8265 void **tofree
= NULL
;
8266 int devices_to_reshape
= 1;
8267 struct imsm_super
*mpb
= super
->anchor
;
8269 unsigned int dev_id
;
8271 dprintf("(enter)\n");
8273 /* enable spares to use in array */
8274 for (i
= 0; i
< delta_disks
; i
++) {
8275 new_disk
= get_disk_super(super
,
8276 major(u
->new_disks
[i
]),
8277 minor(u
->new_disks
[i
]));
8278 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
8279 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
8280 new_disk
, new_disk
->index
);
8281 if ((new_disk
== NULL
) ||
8282 ((new_disk
->index
>= 0) &&
8283 (new_disk
->index
< u
->old_raid_disks
)))
8284 goto update_reshape_exit
;
8285 new_disk
->index
= disk_count
++;
8286 /* slot to fill in autolayout
8288 new_disk
->raiddisk
= new_disk
->index
;
8289 new_disk
->disk
.status
|=
8291 new_disk
->disk
.status
&= ~SPARE_DISK
;
8294 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
8295 mpb
->num_raid_devs
);
8296 /* manage changes in volume
8298 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
8299 void **sp
= *space_list
;
8300 struct imsm_dev
*newdev
;
8301 struct imsm_map
*newmap
, *oldmap
;
8303 for (id
= super
->devlist
; id
; id
= id
->next
) {
8304 if (id
->index
== dev_id
)
8313 /* Copy the dev, but not (all of) the map */
8314 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
8315 oldmap
= get_imsm_map(id
->dev
, MAP_0
);
8316 newmap
= get_imsm_map(newdev
, MAP_0
);
8317 /* Copy the current map */
8318 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8319 /* update one device only
8321 if (devices_to_reshape
) {
8322 dprintf("imsm: modifying subdev: %i\n",
8324 devices_to_reshape
--;
8325 newdev
->vol
.migr_state
= 1;
8326 newdev
->vol
.curr_migr_unit
= 0;
8327 set_migr_type(newdev
, MIGR_GEN_MIGR
);
8328 newmap
->num_members
= u
->new_raid_disks
;
8329 for (i
= 0; i
< delta_disks
; i
++) {
8330 set_imsm_ord_tbl_ent(newmap
,
8331 u
->old_raid_disks
+ i
,
8332 u
->old_raid_disks
+ i
);
8334 /* New map is correct, now need to save old map
8336 newmap
= get_imsm_map(newdev
, MAP_1
);
8337 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8339 imsm_set_array_size(newdev
, -1);
8342 sp
= (void **)id
->dev
;
8347 /* Clear migration record */
8348 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
8351 *space_list
= tofree
;
8354 update_reshape_exit
:
8359 static int apply_takeover_update(struct imsm_update_takeover
*u
,
8360 struct intel_super
*super
,
8363 struct imsm_dev
*dev
= NULL
;
8364 struct intel_dev
*dv
;
8365 struct imsm_dev
*dev_new
;
8366 struct imsm_map
*map
;
8370 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
8371 if (dv
->index
== (unsigned int)u
->subarray
) {
8379 map
= get_imsm_map(dev
, MAP_0
);
8381 if (u
->direction
== R10_TO_R0
) {
8382 /* Number of failed disks must be half of initial disk number */
8383 if (imsm_count_failed(super
, dev
, MAP_0
) !=
8384 (map
->num_members
/ 2))
8387 /* iterate through devices to mark removed disks as spare */
8388 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8389 if (dm
->disk
.status
& FAILED_DISK
) {
8390 int idx
= dm
->index
;
8391 /* update indexes on the disk list */
8392 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
8393 the index values will end up being correct.... NB */
8394 for (du
= super
->disks
; du
; du
= du
->next
)
8395 if (du
->index
> idx
)
8397 /* mark as spare disk */
8402 map
->num_members
= map
->num_members
/ 2;
8403 map
->map_state
= IMSM_T_STATE_NORMAL
;
8404 map
->num_domains
= 1;
8405 map
->raid_level
= 0;
8406 map
->failed_disk_num
= -1;
8409 if (u
->direction
== R0_TO_R10
) {
8411 /* update slots in current disk list */
8412 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8416 /* create new *missing* disks */
8417 for (i
= 0; i
< map
->num_members
; i
++) {
8418 space
= *space_list
;
8421 *space_list
= *space
;
8423 memcpy(du
, super
->disks
, sizeof(*du
));
8427 du
->index
= (i
* 2) + 1;
8428 sprintf((char *)du
->disk
.serial
,
8429 " MISSING_%d", du
->index
);
8430 sprintf((char *)du
->serial
,
8431 "MISSING_%d", du
->index
);
8432 du
->next
= super
->missing
;
8433 super
->missing
= du
;
8435 /* create new dev and map */
8436 space
= *space_list
;
8439 *space_list
= *space
;
8440 dev_new
= (void *)space
;
8441 memcpy(dev_new
, dev
, sizeof(*dev
));
8442 /* update new map */
8443 map
= get_imsm_map(dev_new
, MAP_0
);
8444 map
->num_members
= map
->num_members
* 2;
8445 map
->map_state
= IMSM_T_STATE_DEGRADED
;
8446 map
->num_domains
= 2;
8447 map
->raid_level
= 1;
8448 /* replace dev<->dev_new */
8451 /* update disk order table */
8452 for (du
= super
->disks
; du
; du
= du
->next
)
8454 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8455 for (du
= super
->missing
; du
; du
= du
->next
)
8456 if (du
->index
>= 0) {
8457 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8458 mark_missing(dv
->dev
, &du
->disk
, du
->index
);
8464 static void imsm_process_update(struct supertype
*st
,
8465 struct metadata_update
*update
)
8468 * crack open the metadata_update envelope to find the update record
8469 * update can be one of:
8470 * update_reshape_container_disks - all the arrays in the container
8471 * are being reshaped to have more devices. We need to mark
8472 * the arrays for general migration and convert selected spares
8473 * into active devices.
8474 * update_activate_spare - a spare device has replaced a failed
8475 * device in an array, update the disk_ord_tbl. If this disk is
8476 * present in all member arrays then also clear the SPARE_DISK
8478 * update_create_array
8480 * update_rename_array
8481 * update_add_remove_disk
8483 struct intel_super
*super
= st
->sb
;
8484 struct imsm_super
*mpb
;
8485 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
8487 /* update requires a larger buf but the allocation failed */
8488 if (super
->next_len
&& !super
->next_buf
) {
8489 super
->next_len
= 0;
8493 if (super
->next_buf
) {
8494 memcpy(super
->next_buf
, super
->buf
, super
->len
);
8496 super
->len
= super
->next_len
;
8497 super
->buf
= super
->next_buf
;
8499 super
->next_len
= 0;
8500 super
->next_buf
= NULL
;
8503 mpb
= super
->anchor
;
8506 case update_general_migration_checkpoint
: {
8507 struct intel_dev
*id
;
8508 struct imsm_update_general_migration_checkpoint
*u
=
8509 (void *)update
->buf
;
8511 dprintf("called for update_general_migration_checkpoint\n");
8513 /* find device under general migration */
8514 for (id
= super
->devlist
; id
; id
= id
->next
) {
8515 if (is_gen_migration(id
->dev
)) {
8516 id
->dev
->vol
.curr_migr_unit
=
8517 __cpu_to_le32(u
->curr_migr_unit
);
8518 super
->updates_pending
++;
8523 case update_takeover
: {
8524 struct imsm_update_takeover
*u
= (void *)update
->buf
;
8525 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
8526 imsm_update_version_info(super
);
8527 super
->updates_pending
++;
8532 case update_reshape_container_disks
: {
8533 struct imsm_update_reshape
*u
= (void *)update
->buf
;
8534 if (apply_reshape_container_disks_update(
8535 u
, super
, &update
->space_list
))
8536 super
->updates_pending
++;
8539 case update_reshape_migration
: {
8540 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
8541 if (apply_reshape_migration_update(
8542 u
, super
, &update
->space_list
))
8543 super
->updates_pending
++;
8546 case update_size_change
: {
8547 struct imsm_update_size_change
*u
= (void *)update
->buf
;
8548 if (apply_size_change_update(u
, super
))
8549 super
->updates_pending
++;
8552 case update_activate_spare
: {
8553 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
8554 if (apply_update_activate_spare(u
, super
, st
->arrays
))
8555 super
->updates_pending
++;
8558 case update_create_array
: {
8559 /* someone wants to create a new array, we need to be aware of
8560 * a few races/collisions:
8561 * 1/ 'Create' called by two separate instances of mdadm
8562 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
8563 * devices that have since been assimilated via
8565 * In the event this update can not be carried out mdadm will
8566 * (FIX ME) notice that its update did not take hold.
8568 struct imsm_update_create_array
*u
= (void *) update
->buf
;
8569 struct intel_dev
*dv
;
8570 struct imsm_dev
*dev
;
8571 struct imsm_map
*map
, *new_map
;
8572 unsigned long long start
, end
;
8573 unsigned long long new_start
, new_end
;
8575 struct disk_info
*inf
;
8578 /* handle racing creates: first come first serve */
8579 if (u
->dev_idx
< mpb
->num_raid_devs
) {
8580 dprintf("subarray %d already defined\n", u
->dev_idx
);
8584 /* check update is next in sequence */
8585 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
8586 dprintf("can not create array %d expected index %d\n",
8587 u
->dev_idx
, mpb
->num_raid_devs
);
8591 new_map
= get_imsm_map(&u
->dev
, MAP_0
);
8592 new_start
= pba_of_lba0(new_map
);
8593 new_end
= new_start
+ blocks_per_member(new_map
);
8594 inf
= get_disk_info(u
);
8596 /* handle activate_spare versus create race:
8597 * check to make sure that overlapping arrays do not include
8600 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8601 dev
= get_imsm_dev(super
, i
);
8602 map
= get_imsm_map(dev
, MAP_0
);
8603 start
= pba_of_lba0(map
);
8604 end
= start
+ blocks_per_member(map
);
8605 if ((new_start
>= start
&& new_start
<= end
) ||
8606 (start
>= new_start
&& start
<= new_end
))
8611 if (disks_overlap(super
, i
, u
)) {
8612 dprintf("arrays overlap\n");
8617 /* check that prepare update was successful */
8618 if (!update
->space
) {
8619 dprintf("prepare update failed\n");
8623 /* check that all disks are still active before committing
8624 * changes. FIXME: could we instead handle this by creating a
8625 * degraded array? That's probably not what the user expects,
8626 * so better to drop this update on the floor.
8628 for (i
= 0; i
< new_map
->num_members
; i
++) {
8629 dl
= serial_to_dl(inf
[i
].serial
, super
);
8631 dprintf("disk disappeared\n");
8636 super
->updates_pending
++;
8638 /* convert spares to members and fixup ord_tbl */
8639 for (i
= 0; i
< new_map
->num_members
; i
++) {
8640 dl
= serial_to_dl(inf
[i
].serial
, super
);
8641 if (dl
->index
== -1) {
8642 dl
->index
= mpb
->num_disks
;
8644 dl
->disk
.status
|= CONFIGURED_DISK
;
8645 dl
->disk
.status
&= ~SPARE_DISK
;
8647 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
8652 update
->space
= NULL
;
8653 imsm_copy_dev(dev
, &u
->dev
);
8654 dv
->index
= u
->dev_idx
;
8655 dv
->next
= super
->devlist
;
8656 super
->devlist
= dv
;
8657 mpb
->num_raid_devs
++;
8659 imsm_update_version_info(super
);
8662 /* mdmon knows how to release update->space, but not
8663 * ((struct intel_dev *) update->space)->dev
8665 if (update
->space
) {
8671 case update_kill_array
: {
8672 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
8673 int victim
= u
->dev_idx
;
8674 struct active_array
*a
;
8675 struct intel_dev
**dp
;
8676 struct imsm_dev
*dev
;
8678 /* sanity check that we are not affecting the uuid of
8679 * active arrays, or deleting an active array
8681 * FIXME when immutable ids are available, but note that
8682 * we'll also need to fixup the invalidated/active
8683 * subarray indexes in mdstat
8685 for (a
= st
->arrays
; a
; a
= a
->next
)
8686 if (a
->info
.container_member
>= victim
)
8688 /* by definition if mdmon is running at least one array
8689 * is active in the container, so checking
8690 * mpb->num_raid_devs is just extra paranoia
8692 dev
= get_imsm_dev(super
, victim
);
8693 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
8694 dprintf("failed to delete subarray-%d\n", victim
);
8698 for (dp
= &super
->devlist
; *dp
;)
8699 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
8702 if ((*dp
)->index
> (unsigned)victim
)
8706 mpb
->num_raid_devs
--;
8707 super
->updates_pending
++;
8710 case update_rename_array
: {
8711 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
8712 char name
[MAX_RAID_SERIAL_LEN
+1];
8713 int target
= u
->dev_idx
;
8714 struct active_array
*a
;
8715 struct imsm_dev
*dev
;
8717 /* sanity check that we are not affecting the uuid of
8720 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
8721 name
[MAX_RAID_SERIAL_LEN
] = '\0';
8722 for (a
= st
->arrays
; a
; a
= a
->next
)
8723 if (a
->info
.container_member
== target
)
8725 dev
= get_imsm_dev(super
, u
->dev_idx
);
8726 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
8727 dprintf("failed to rename subarray-%d\n", target
);
8731 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
8732 super
->updates_pending
++;
8735 case update_add_remove_disk
: {
8736 /* we may be able to repair some arrays if disks are
8737 * being added, check the status of add_remove_disk
8738 * if discs has been added.
8740 if (add_remove_disk_update(super
)) {
8741 struct active_array
*a
;
8743 super
->updates_pending
++;
8744 for (a
= st
->arrays
; a
; a
= a
->next
)
8745 a
->check_degraded
= 1;
8750 pr_err("error: unsuported process update type:(type: %d)\n", type
);
8754 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
);
8756 static int imsm_prepare_update(struct supertype
*st
,
8757 struct metadata_update
*update
)
8760 * Allocate space to hold new disk entries, raid-device entries or a new
8761 * mpb if necessary. The manager synchronously waits for updates to
8762 * complete in the monitor, so new mpb buffers allocated here can be
8763 * integrated by the monitor thread without worrying about live pointers
8764 * in the manager thread.
8766 enum imsm_update_type type
;
8767 struct intel_super
*super
= st
->sb
;
8768 struct imsm_super
*mpb
= super
->anchor
;
8772 if (update
->len
< (int)sizeof(type
))
8775 type
= *(enum imsm_update_type
*) update
->buf
;
8778 case update_general_migration_checkpoint
:
8779 if (update
->len
< (int)sizeof(struct imsm_update_general_migration_checkpoint
))
8781 dprintf("called for update_general_migration_checkpoint\n");
8783 case update_takeover
: {
8784 struct imsm_update_takeover
*u
= (void *)update
->buf
;
8785 if (update
->len
< (int)sizeof(*u
))
8787 if (u
->direction
== R0_TO_R10
) {
8788 void **tail
= (void **)&update
->space_list
;
8789 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
8790 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8791 int num_members
= map
->num_members
;
8794 /* allocate memory for added disks */
8795 for (i
= 0; i
< num_members
; i
++) {
8796 size
= sizeof(struct dl
);
8797 space
= xmalloc(size
);
8802 /* allocate memory for new device */
8803 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
8804 (num_members
* sizeof(__u32
));
8805 space
= xmalloc(size
);
8809 len
= disks_to_mpb_size(num_members
* 2);
8814 case update_reshape_container_disks
: {
8815 /* Every raid device in the container is about to
8816 * gain some more devices, and we will enter a
8818 * So each 'imsm_map' will be bigger, and the imsm_vol
8819 * will now hold 2 of them.
8820 * Thus we need new 'struct imsm_dev' allocations sized
8821 * as sizeof_imsm_dev but with more devices in both maps.
8823 struct imsm_update_reshape
*u
= (void *)update
->buf
;
8824 struct intel_dev
*dl
;
8825 void **space_tail
= (void**)&update
->space_list
;
8827 if (update
->len
< (int)sizeof(*u
))
8830 dprintf("for update_reshape\n");
8832 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
8833 int size
= sizeof_imsm_dev(dl
->dev
, 1);
8835 if (u
->new_raid_disks
> u
->old_raid_disks
)
8836 size
+= sizeof(__u32
)*2*
8837 (u
->new_raid_disks
- u
->old_raid_disks
);
8844 len
= disks_to_mpb_size(u
->new_raid_disks
);
8845 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
8848 case update_reshape_migration
: {
8849 /* for migration level 0->5 we need to add disks
8850 * so the same as for container operation we will copy
8851 * device to the bigger location.
8852 * in memory prepared device and new disk area are prepared
8853 * for usage in process update
8855 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
8856 struct intel_dev
*id
;
8857 void **space_tail
= (void **)&update
->space_list
;
8860 int current_level
= -1;
8862 if (update
->len
< (int)sizeof(*u
))
8865 dprintf("for update_reshape\n");
8867 /* add space for bigger array in update
8869 for (id
= super
->devlist
; id
; id
= id
->next
) {
8870 if (id
->index
== (unsigned)u
->subdev
) {
8871 size
= sizeof_imsm_dev(id
->dev
, 1);
8872 if (u
->new_raid_disks
> u
->old_raid_disks
)
8873 size
+= sizeof(__u32
)*2*
8874 (u
->new_raid_disks
- u
->old_raid_disks
);
8882 if (update
->space_list
== NULL
)
8885 /* add space for disk in update
8887 size
= sizeof(struct dl
);
8893 /* add spare device to update
8895 for (id
= super
->devlist
; id
; id
= id
->next
)
8896 if (id
->index
== (unsigned)u
->subdev
) {
8897 struct imsm_dev
*dev
;
8898 struct imsm_map
*map
;
8900 dev
= get_imsm_dev(super
, u
->subdev
);
8901 map
= get_imsm_map(dev
, MAP_0
);
8902 current_level
= map
->raid_level
;
8905 if ((u
->new_level
== 5) && (u
->new_level
!= current_level
)) {
8906 struct mdinfo
*spares
;
8908 spares
= get_spares_for_grow(st
);
8916 makedev(dev
->disk
.major
,
8918 dl
= get_disk_super(super
,
8921 dl
->index
= u
->old_raid_disks
;
8927 len
= disks_to_mpb_size(u
->new_raid_disks
);
8928 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
8931 case update_size_change
: {
8932 if (update
->len
< (int)sizeof(struct imsm_update_size_change
))
8936 case update_activate_spare
: {
8937 if (update
->len
< (int)sizeof(struct imsm_update_activate_spare
))
8941 case update_create_array
: {
8942 struct imsm_update_create_array
*u
= (void *) update
->buf
;
8943 struct intel_dev
*dv
;
8944 struct imsm_dev
*dev
= &u
->dev
;
8945 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8947 struct disk_info
*inf
;
8951 if (update
->len
< (int)sizeof(*u
))
8954 inf
= get_disk_info(u
);
8955 len
= sizeof_imsm_dev(dev
, 1);
8956 /* allocate a new super->devlist entry */
8957 dv
= xmalloc(sizeof(*dv
));
8958 dv
->dev
= xmalloc(len
);
8961 /* count how many spares will be converted to members */
8962 for (i
= 0; i
< map
->num_members
; i
++) {
8963 dl
= serial_to_dl(inf
[i
].serial
, super
);
8965 /* hmm maybe it failed?, nothing we can do about
8970 if (count_memberships(dl
, super
) == 0)
8973 len
+= activate
* sizeof(struct imsm_disk
);
8976 case update_kill_array
: {
8977 if (update
->len
< (int)sizeof(struct imsm_update_kill_array
))
8981 case update_rename_array
: {
8982 if (update
->len
< (int)sizeof(struct imsm_update_rename_array
))
8986 case update_add_remove_disk
:
8987 /* no update->len needed */
8993 /* check if we need a larger metadata buffer */
8994 if (super
->next_buf
)
8995 buf_len
= super
->next_len
;
8997 buf_len
= super
->len
;
8999 if (__le32_to_cpu(mpb
->mpb_size
) + len
> buf_len
) {
9000 /* ok we need a larger buf than what is currently allocated
9001 * if this allocation fails process_update will notice that
9002 * ->next_len is set and ->next_buf is NULL
9004 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + len
, 512);
9005 if (super
->next_buf
)
9006 free(super
->next_buf
);
9008 super
->next_len
= buf_len
;
9009 if (posix_memalign(&super
->next_buf
, 512, buf_len
) == 0)
9010 memset(super
->next_buf
, 0, buf_len
);
9012 super
->next_buf
= NULL
;
9017 /* must be called while manager is quiesced */
9018 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
9020 struct imsm_super
*mpb
= super
->anchor
;
9022 struct imsm_dev
*dev
;
9023 struct imsm_map
*map
;
9024 int i
, j
, num_members
;
9027 dprintf("deleting device[%d] from imsm_super\n", index
);
9029 /* shift all indexes down one */
9030 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
9031 if (iter
->index
> (int)index
)
9033 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
9034 if (iter
->index
> (int)index
)
9037 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
9038 dev
= get_imsm_dev(super
, i
);
9039 map
= get_imsm_map(dev
, MAP_0
);
9040 num_members
= map
->num_members
;
9041 for (j
= 0; j
< num_members
; j
++) {
9042 /* update ord entries being careful not to propagate
9043 * ord-flags to the first map
9045 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_X
);
9047 if (ord_to_idx(ord
) <= index
)
9050 map
= get_imsm_map(dev
, MAP_0
);
9051 set_imsm_ord_tbl_ent(map
, j
, ord_to_idx(ord
- 1));
9052 map
= get_imsm_map(dev
, MAP_1
);
9054 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
9059 super
->updates_pending
++;
9061 struct dl
*dl
= *dlp
;
9063 *dlp
= (*dlp
)->next
;
9064 __free_imsm_disk(dl
);
9067 #endif /* MDASSEMBLE */
9069 static void close_targets(int *targets
, int new_disks
)
9076 for (i
= 0; i
< new_disks
; i
++) {
9077 if (targets
[i
] >= 0) {
9084 static int imsm_get_allowed_degradation(int level
, int raid_disks
,
9085 struct intel_super
*super
,
9086 struct imsm_dev
*dev
)
9092 struct imsm_map
*map
;
9095 ret_val
= raid_disks
/2;
9096 /* check map if all disks pairs not failed
9099 map
= get_imsm_map(dev
, MAP_0
);
9100 for (i
= 0; i
< ret_val
; i
++) {
9101 int degradation
= 0;
9102 if (get_imsm_disk(super
, i
) == NULL
)
9104 if (get_imsm_disk(super
, i
+ 1) == NULL
)
9106 if (degradation
== 2)
9109 map
= get_imsm_map(dev
, MAP_1
);
9110 /* if there is no second map
9111 * result can be returned
9115 /* check degradation in second map
9117 for (i
= 0; i
< ret_val
; i
++) {
9118 int degradation
= 0;
9119 if (get_imsm_disk(super
, i
) == NULL
)
9121 if (get_imsm_disk(super
, i
+ 1) == NULL
)
9123 if (degradation
== 2)
9137 /*******************************************************************************
9138 * Function: open_backup_targets
9139 * Description: Function opens file descriptors for all devices given in
9142 * info : general array info
9143 * raid_disks : number of disks
9144 * raid_fds : table of device's file descriptors
9145 * super : intel super for raid10 degradation check
9146 * dev : intel device for raid10 degradation check
9150 ******************************************************************************/
9151 int open_backup_targets(struct mdinfo
*info
, int raid_disks
, int *raid_fds
,
9152 struct intel_super
*super
, struct imsm_dev
*dev
)
9158 for (i
= 0; i
< raid_disks
; i
++)
9161 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
9164 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
)) {
9165 dprintf("disk is faulty!!\n");
9169 if ((sd
->disk
.raid_disk
>= raid_disks
) ||
9170 (sd
->disk
.raid_disk
< 0))
9173 dn
= map_dev(sd
->disk
.major
,
9175 raid_fds
[sd
->disk
.raid_disk
] = dev_open(dn
, O_RDWR
);
9176 if (raid_fds
[sd
->disk
.raid_disk
] < 0) {
9177 pr_err("cannot open component\n");
9182 /* check if maximum array degradation level is not exceeded
9184 if ((raid_disks
- opened
) >
9185 imsm_get_allowed_degradation(info
->new_level
,
9188 pr_err("Not enough disks can be opened.\n");
9189 close_targets(raid_fds
, raid_disks
);
9195 /*******************************************************************************
9196 * Function: validate_container_imsm
9197 * Description: This routine validates container after assemble,
9198 * eg. if devices in container are under the same controller.
9201 * info : linked list with info about devices used in array
9205 ******************************************************************************/
9206 int validate_container_imsm(struct mdinfo
*info
)
9208 if (check_env("IMSM_NO_PLATFORM"))
9211 struct sys_dev
*idev
;
9212 struct sys_dev
*hba
= NULL
;
9213 struct sys_dev
*intel_devices
= find_intel_devices();
9214 char *dev_path
= devt_to_devpath(makedev(info
->disk
.major
,
9217 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
9218 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
9227 pr_err("WARNING - Cannot detect HBA for device %s!\n",
9228 devid2kname(makedev(info
->disk
.major
, info
->disk
.minor
)));
9232 const struct imsm_orom
*orom
= get_orom_by_device_id(hba
->dev_id
);
9235 for (dev
= info
->next
; dev
; dev
= dev
->next
) {
9236 dev_path
= devt_to_devpath(makedev(dev
->disk
.major
, dev
->disk
.minor
));
9238 struct sys_dev
*hba2
= NULL
;
9239 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
9240 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
9248 const struct imsm_orom
*orom2
= hba2
== NULL
? NULL
:
9249 get_orom_by_device_id(hba2
->dev_id
);
9251 if (hba2
&& hba
->type
!= hba2
->type
) {
9252 pr_err("WARNING - HBAs of devices do not match %s != %s\n",
9253 get_sys_dev_type(hba
->type
), get_sys_dev_type(hba2
->type
));
9257 if ((orom
!= orom2
) || ((hba
->type
== SYS_DEV_VMD
) && (hba
!= hba2
))) {
9258 pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n"
9259 " This operation is not supported and can lead to data loss.\n");
9264 pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n"
9265 " This operation is not supported and can lead to data loss.\n");
9273 /*******************************************************************************
9274 * Function: init_migr_record_imsm
9275 * Description: Function inits imsm migration record
9277 * super : imsm internal array info
9278 * dev : device under migration
9279 * info : general array info to find the smallest device
9282 ******************************************************************************/
9283 void init_migr_record_imsm(struct supertype
*st
, struct imsm_dev
*dev
,
9284 struct mdinfo
*info
)
9286 struct intel_super
*super
= st
->sb
;
9287 struct migr_record
*migr_rec
= super
->migr_rec
;
9289 unsigned long long dsize
, dev_sectors
;
9290 long long unsigned min_dev_sectors
= -1LLU;
9294 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
9295 struct imsm_map
*map_src
= get_imsm_map(dev
, MAP_1
);
9296 unsigned long long num_migr_units
;
9297 unsigned long long array_blocks
;
9299 memset(migr_rec
, 0, sizeof(struct migr_record
));
9300 migr_rec
->family_num
= __cpu_to_le32(super
->anchor
->family_num
);
9302 /* only ascending reshape supported now */
9303 migr_rec
->ascending_migr
= __cpu_to_le32(1);
9305 migr_rec
->dest_depth_per_unit
= GEN_MIGR_AREA_SIZE
/
9306 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
9307 migr_rec
->dest_depth_per_unit
*=
9308 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
9309 new_data_disks
= imsm_num_data_members(dev
, MAP_0
);
9310 migr_rec
->blocks_per_unit
=
9311 __cpu_to_le32(migr_rec
->dest_depth_per_unit
* new_data_disks
);
9312 migr_rec
->dest_depth_per_unit
=
9313 __cpu_to_le32(migr_rec
->dest_depth_per_unit
);
9314 array_blocks
= info
->component_size
* new_data_disks
;
9316 array_blocks
/ __le32_to_cpu(migr_rec
->blocks_per_unit
);
9318 if (array_blocks
% __le32_to_cpu(migr_rec
->blocks_per_unit
))
9320 migr_rec
->num_migr_units
= __cpu_to_le32(num_migr_units
);
9322 migr_rec
->post_migr_vol_cap
= dev
->size_low
;
9323 migr_rec
->post_migr_vol_cap_hi
= dev
->size_high
;
9325 /* Find the smallest dev */
9326 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
9327 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
9328 fd
= dev_open(nm
, O_RDONLY
);
9331 get_dev_size(fd
, NULL
, &dsize
);
9332 dev_sectors
= dsize
/ 512;
9333 if (dev_sectors
< min_dev_sectors
)
9334 min_dev_sectors
= dev_sectors
;
9337 migr_rec
->ckpt_area_pba
= __cpu_to_le32(min_dev_sectors
-
9338 RAID_DISK_RESERVED_BLOCKS_IMSM_HI
);
9340 write_imsm_migr_rec(st
);
9345 /*******************************************************************************
9346 * Function: save_backup_imsm
9347 * Description: Function saves critical data stripes to Migration Copy Area
9348 * and updates the current migration unit status.
9349 * Use restore_stripes() to form a destination stripe,
9350 * and to write it to the Copy Area.
9352 * st : supertype information
9353 * dev : imsm device that backup is saved for
9354 * info : general array info
9355 * buf : input buffer
9356 * length : length of data to backup (blocks_per_unit)
9360 ******************************************************************************/
9361 int save_backup_imsm(struct supertype
*st
,
9362 struct imsm_dev
*dev
,
9363 struct mdinfo
*info
,
9368 struct intel_super
*super
= st
->sb
;
9369 unsigned long long *target_offsets
= NULL
;
9370 int *targets
= NULL
;
9372 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
9373 int new_disks
= map_dest
->num_members
;
9374 int dest_layout
= 0;
9376 unsigned long long start
;
9377 int data_disks
= imsm_num_data_members(dev
, MAP_0
);
9379 targets
= xmalloc(new_disks
* sizeof(int));
9381 for (i
= 0; i
< new_disks
; i
++)
9384 target_offsets
= xcalloc(new_disks
, sizeof(unsigned long long));
9386 start
= info
->reshape_progress
* 512;
9387 for (i
= 0; i
< new_disks
; i
++) {
9388 target_offsets
[i
] = (unsigned long long)
9389 __le32_to_cpu(super
->migr_rec
->ckpt_area_pba
) * 512;
9390 /* move back copy area adderss, it will be moved forward
9391 * in restore_stripes() using start input variable
9393 target_offsets
[i
] -= start
/data_disks
;
9396 if (open_backup_targets(info
, new_disks
, targets
,
9400 dest_layout
= imsm_level_to_layout(map_dest
->raid_level
);
9401 dest_chunk
= __le16_to_cpu(map_dest
->blocks_per_strip
) * 512;
9403 if (restore_stripes(targets
, /* list of dest devices */
9404 target_offsets
, /* migration record offsets */
9407 map_dest
->raid_level
,
9409 -1, /* source backup file descriptor */
9410 0, /* input buf offset
9411 * always 0 buf is already offseted */
9415 pr_err("Error restoring stripes\n");
9423 close_targets(targets
, new_disks
);
9426 free(target_offsets
);
9431 /*******************************************************************************
9432 * Function: save_checkpoint_imsm
9433 * Description: Function called for current unit status update
9434 * in the migration record. It writes it to disk.
9436 * super : imsm internal array info
9437 * info : general array info
9441 * 2: failure, means no valid migration record
9442 * / no general migration in progress /
9443 ******************************************************************************/
9444 int save_checkpoint_imsm(struct supertype
*st
, struct mdinfo
*info
, int state
)
9446 struct intel_super
*super
= st
->sb
;
9447 unsigned long long blocks_per_unit
;
9448 unsigned long long curr_migr_unit
;
9450 if (load_imsm_migr_rec(super
, info
) != 0) {
9451 dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n");
9455 blocks_per_unit
= __le32_to_cpu(super
->migr_rec
->blocks_per_unit
);
9456 if (blocks_per_unit
== 0) {
9457 dprintf("imsm: no migration in progress.\n");
9460 curr_migr_unit
= info
->reshape_progress
/ blocks_per_unit
;
9461 /* check if array is alligned to copy area
9462 * if it is not alligned, add one to current migration unit value
9463 * this can happend on array reshape finish only
9465 if (info
->reshape_progress
% blocks_per_unit
)
9468 super
->migr_rec
->curr_migr_unit
=
9469 __cpu_to_le32(curr_migr_unit
);
9470 super
->migr_rec
->rec_status
= __cpu_to_le32(state
);
9471 super
->migr_rec
->dest_1st_member_lba
=
9472 __cpu_to_le32(curr_migr_unit
*
9473 __le32_to_cpu(super
->migr_rec
->dest_depth_per_unit
));
9474 if (write_imsm_migr_rec(st
) < 0) {
9475 dprintf("imsm: Cannot write migration record outside backup area\n");
9482 /*******************************************************************************
9483 * Function: recover_backup_imsm
9484 * Description: Function recovers critical data from the Migration Copy Area
9485 * while assembling an array.
9487 * super : imsm internal array info
9488 * info : general array info
9490 * 0 : success (or there is no data to recover)
9492 ******************************************************************************/
9493 int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
)
9495 struct intel_super
*super
= st
->sb
;
9496 struct migr_record
*migr_rec
= super
->migr_rec
;
9497 struct imsm_map
*map_dest
= NULL
;
9498 struct intel_dev
*id
= NULL
;
9499 unsigned long long read_offset
;
9500 unsigned long long write_offset
;
9502 int *targets
= NULL
;
9503 int new_disks
, i
, err
;
9506 unsigned long curr_migr_unit
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
9507 unsigned long num_migr_units
= __le32_to_cpu(migr_rec
->num_migr_units
);
9509 int skipped_disks
= 0;
9511 err
= sysfs_get_str(info
, NULL
, "array_state", (char *)buffer
, 20);
9515 /* recover data only during assemblation */
9516 if (strncmp(buffer
, "inactive", 8) != 0)
9518 /* no data to recover */
9519 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
9521 if (curr_migr_unit
>= num_migr_units
)
9524 /* find device during reshape */
9525 for (id
= super
->devlist
; id
; id
= id
->next
)
9526 if (is_gen_migration(id
->dev
))
9531 map_dest
= get_imsm_map(id
->dev
, MAP_0
);
9532 new_disks
= map_dest
->num_members
;
9534 read_offset
= (unsigned long long)
9535 __le32_to_cpu(migr_rec
->ckpt_area_pba
) * 512;
9537 write_offset
= ((unsigned long long)
9538 __le32_to_cpu(migr_rec
->dest_1st_member_lba
) +
9539 pba_of_lba0(map_dest
)) * 512;
9541 unit_len
= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
9542 if (posix_memalign((void **)&buf
, 512, unit_len
) != 0)
9544 targets
= xcalloc(new_disks
, sizeof(int));
9546 if (open_backup_targets(info
, new_disks
, targets
, super
, id
->dev
)) {
9547 pr_err("Cannot open some devices belonging to array.\n");
9551 for (i
= 0; i
< new_disks
; i
++) {
9552 if (targets
[i
] < 0) {
9556 if (lseek64(targets
[i
], read_offset
, SEEK_SET
) < 0) {
9557 pr_err("Cannot seek to block: %s\n",
9562 if ((unsigned)read(targets
[i
], buf
, unit_len
) != unit_len
) {
9563 pr_err("Cannot read copy area block: %s\n",
9568 if (lseek64(targets
[i
], write_offset
, SEEK_SET
) < 0) {
9569 pr_err("Cannot seek to block: %s\n",
9574 if ((unsigned)write(targets
[i
], buf
, unit_len
) != unit_len
) {
9575 pr_err("Cannot restore block: %s\n",
9582 if (skipped_disks
> imsm_get_allowed_degradation(info
->new_level
,
9586 pr_err("Cannot restore data from backup. Too many failed disks\n");
9590 if (save_checkpoint_imsm(st
, info
, UNIT_SRC_NORMAL
)) {
9591 /* ignore error == 2, this can mean end of reshape here
9593 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n");
9599 for (i
= 0; i
< new_disks
; i
++)
9608 static char disk_by_path
[] = "/dev/disk/by-path/";
9610 static const char *imsm_get_disk_controller_domain(const char *path
)
9612 char disk_path
[PATH_MAX
];
9616 strcpy(disk_path
, disk_by_path
);
9617 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
9618 if (stat(disk_path
, &st
) == 0) {
9619 struct sys_dev
* hba
;
9622 path
= devt_to_devpath(st
.st_rdev
);
9625 hba
= find_disk_attached_hba(-1, path
);
9626 if (hba
&& hba
->type
== SYS_DEV_SAS
)
9628 else if (hba
&& hba
->type
== SYS_DEV_SATA
)
9632 dprintf("path: %s hba: %s attached: %s\n",
9633 path
, (hba
) ? hba
->path
: "NULL", drv
);
9639 static char *imsm_find_array_devnm_by_subdev(int subdev
, char *container
)
9641 static char devnm
[32];
9642 char subdev_name
[20];
9643 struct mdstat_ent
*mdstat
;
9645 sprintf(subdev_name
, "%d", subdev
);
9646 mdstat
= mdstat_by_subdev(subdev_name
, container
);
9650 strcpy(devnm
, mdstat
->devnm
);
9651 free_mdstat(mdstat
);
9655 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
9656 struct geo_params
*geo
,
9657 int *old_raid_disks
,
9660 /* currently we only support increasing the number of devices
9661 * for a container. This increases the number of device for each
9662 * member array. They must all be RAID0 or RAID5.
9665 struct mdinfo
*info
, *member
;
9666 int devices_that_can_grow
= 0;
9668 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st
->devnm
);
9670 if (geo
->size
> 0 ||
9671 geo
->level
!= UnSet
||
9672 geo
->layout
!= UnSet
||
9673 geo
->chunksize
!= 0 ||
9674 geo
->raid_disks
== UnSet
) {
9675 dprintf("imsm: Container operation is allowed for raid disks number change only.\n");
9679 if (direction
== ROLLBACK_METADATA_CHANGES
) {
9680 dprintf("imsm: Metadata changes rollback is not supported for container operation.\n");
9684 info
= container_content_imsm(st
, NULL
);
9685 for (member
= info
; member
; member
= member
->next
) {
9688 dprintf("imsm: checking device_num: %i\n",
9689 member
->container_member
);
9691 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
9692 /* we work on container for Online Capacity Expansion
9693 * only so raid_disks has to grow
9695 dprintf("imsm: for container operation raid disks increase is required\n");
9699 if ((info
->array
.level
!= 0) &&
9700 (info
->array
.level
!= 5)) {
9701 /* we cannot use this container with other raid level
9703 dprintf("imsm: for container operation wrong raid level (%i) detected\n",
9707 /* check for platform support
9708 * for this raid level configuration
9710 struct intel_super
*super
= st
->sb
;
9711 if (!is_raid_level_supported(super
->orom
,
9712 member
->array
.level
,
9714 dprintf("platform does not support raid%d with %d disk%s\n",
9717 geo
->raid_disks
> 1 ? "s" : "");
9720 /* check if component size is aligned to chunk size
9722 if (info
->component_size
%
9723 (info
->array
.chunk_size
/512)) {
9724 dprintf("Component size is not aligned to chunk size\n");
9729 if (*old_raid_disks
&&
9730 info
->array
.raid_disks
!= *old_raid_disks
)
9732 *old_raid_disks
= info
->array
.raid_disks
;
9734 /* All raid5 and raid0 volumes in container
9735 * have to be ready for Online Capacity Expansion
9736 * so they need to be assembled. We have already
9737 * checked that no recovery etc is happening.
9739 result
= imsm_find_array_devnm_by_subdev(member
->container_member
,
9740 st
->container_devnm
);
9741 if (result
== NULL
) {
9742 dprintf("imsm: cannot find array\n");
9745 devices_that_can_grow
++;
9748 if (!member
&& devices_that_can_grow
)
9752 dprintf("Container operation allowed\n");
9754 dprintf("Error: %i\n", ret_val
);
9759 /* Function: get_spares_for_grow
9760 * Description: Allocates memory and creates list of spare devices
9761 * avaliable in container. Checks if spare drive size is acceptable.
9762 * Parameters: Pointer to the supertype structure
9763 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
9766 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
9768 unsigned long long min_size
= min_acceptable_spare_size_imsm(st
);
9769 return container_choose_spares(st
, min_size
, NULL
, NULL
, NULL
, 0);
9772 /******************************************************************************
9773 * function: imsm_create_metadata_update_for_reshape
9774 * Function creates update for whole IMSM container.
9776 ******************************************************************************/
9777 static int imsm_create_metadata_update_for_reshape(
9778 struct supertype
*st
,
9779 struct geo_params
*geo
,
9781 struct imsm_update_reshape
**updatep
)
9783 struct intel_super
*super
= st
->sb
;
9784 struct imsm_super
*mpb
= super
->anchor
;
9785 int update_memory_size
= 0;
9786 struct imsm_update_reshape
*u
= NULL
;
9787 struct mdinfo
*spares
= NULL
;
9789 int delta_disks
= 0;
9792 dprintf("(enter) raid_disks = %i\n", geo
->raid_disks
);
9794 delta_disks
= geo
->raid_disks
- old_raid_disks
;
9796 /* size of all update data without anchor */
9797 update_memory_size
= sizeof(struct imsm_update_reshape
);
9799 /* now add space for spare disks that we need to add. */
9800 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
9802 u
= xcalloc(1, update_memory_size
);
9803 u
->type
= update_reshape_container_disks
;
9804 u
->old_raid_disks
= old_raid_disks
;
9805 u
->new_raid_disks
= geo
->raid_disks
;
9807 /* now get spare disks list
9809 spares
= get_spares_for_grow(st
);
9812 || delta_disks
> spares
->array
.spare_disks
) {
9813 pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo
->dev_name
);
9818 /* we have got spares
9819 * update disk list in imsm_disk list table in anchor
9821 dprintf("imsm: %i spares are available.\n\n",
9822 spares
->array
.spare_disks
);
9825 for (i
= 0; i
< delta_disks
; i
++) {
9830 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
9832 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
9833 dl
->index
= mpb
->num_disks
;
9843 dprintf("imsm: reshape update preparation :");
9844 if (i
== delta_disks
) {
9845 dprintf_cont(" OK\n");
9847 return update_memory_size
;
9850 dprintf_cont(" Error\n");
9855 /******************************************************************************
9856 * function: imsm_create_metadata_update_for_size_change()
9857 * Creates update for IMSM array for array size change.
9859 ******************************************************************************/
9860 static int imsm_create_metadata_update_for_size_change(
9861 struct supertype
*st
,
9862 struct geo_params
*geo
,
9863 struct imsm_update_size_change
**updatep
)
9865 struct intel_super
*super
= st
->sb
;
9866 int update_memory_size
= 0;
9867 struct imsm_update_size_change
*u
= NULL
;
9869 dprintf("(enter) New size = %llu\n", geo
->size
);
9871 /* size of all update data without anchor */
9872 update_memory_size
= sizeof(struct imsm_update_size_change
);
9874 u
= xcalloc(1, update_memory_size
);
9875 u
->type
= update_size_change
;
9876 u
->subdev
= super
->current_vol
;
9877 u
->new_size
= geo
->size
;
9879 dprintf("imsm: reshape update preparation : OK\n");
9882 return update_memory_size
;
9885 /******************************************************************************
9886 * function: imsm_create_metadata_update_for_migration()
9887 * Creates update for IMSM array.
9889 ******************************************************************************/
9890 static int imsm_create_metadata_update_for_migration(
9891 struct supertype
*st
,
9892 struct geo_params
*geo
,
9893 struct imsm_update_reshape_migration
**updatep
)
9895 struct intel_super
*super
= st
->sb
;
9896 int update_memory_size
= 0;
9897 struct imsm_update_reshape_migration
*u
= NULL
;
9898 struct imsm_dev
*dev
;
9899 int previous_level
= -1;
9901 dprintf("(enter) New Level = %i\n", geo
->level
);
9903 /* size of all update data without anchor */
9904 update_memory_size
= sizeof(struct imsm_update_reshape_migration
);
9906 u
= xcalloc(1, update_memory_size
);
9907 u
->type
= update_reshape_migration
;
9908 u
->subdev
= super
->current_vol
;
9909 u
->new_level
= geo
->level
;
9910 u
->new_layout
= geo
->layout
;
9911 u
->new_raid_disks
= u
->old_raid_disks
= geo
->raid_disks
;
9912 u
->new_disks
[0] = -1;
9913 u
->new_chunksize
= -1;
9915 dev
= get_imsm_dev(super
, u
->subdev
);
9917 struct imsm_map
*map
;
9919 map
= get_imsm_map(dev
, MAP_0
);
9921 int current_chunk_size
=
9922 __le16_to_cpu(map
->blocks_per_strip
) / 2;
9924 if (geo
->chunksize
!= current_chunk_size
) {
9925 u
->new_chunksize
= geo
->chunksize
/ 1024;
9926 dprintf("imsm: chunk size change from %i to %i\n",
9927 current_chunk_size
, u
->new_chunksize
);
9929 previous_level
= map
->raid_level
;
9932 if ((geo
->level
== 5) && (previous_level
== 0)) {
9933 struct mdinfo
*spares
= NULL
;
9935 u
->new_raid_disks
++;
9936 spares
= get_spares_for_grow(st
);
9937 if ((spares
== NULL
) || (spares
->array
.spare_disks
< 1)) {
9940 update_memory_size
= 0;
9941 dprintf("error: cannot get spare device for requested migration");
9946 dprintf("imsm: reshape update preparation : OK\n");
9949 return update_memory_size
;
9952 static void imsm_update_metadata_locally(struct supertype
*st
,
9955 struct metadata_update mu
;
9960 mu
.space_list
= NULL
;
9962 if (imsm_prepare_update(st
, &mu
))
9963 imsm_process_update(st
, &mu
);
9965 while (mu
.space_list
) {
9966 void **space
= mu
.space_list
;
9967 mu
.space_list
= *space
;
9972 /***************************************************************************
9973 * Function: imsm_analyze_change
9974 * Description: Function analyze change for single volume
9975 * and validate if transition is supported
9976 * Parameters: Geometry parameters, supertype structure,
9977 * metadata change direction (apply/rollback)
9978 * Returns: Operation type code on success, -1 if fail
9979 ****************************************************************************/
9980 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
9981 struct geo_params
*geo
,
9988 /* number of added/removed disks in operation result */
9989 int devNumChange
= 0;
9990 /* imsm compatible layout value for array geometry verification */
9991 int imsm_layout
= -1;
9993 struct imsm_dev
*dev
;
9994 struct intel_super
*super
;
9995 unsigned long long current_size
;
9996 unsigned long long free_size
;
9997 unsigned long long max_size
;
10000 getinfo_super_imsm_volume(st
, &info
, NULL
);
10001 if ((geo
->level
!= info
.array
.level
) &&
10002 (geo
->level
>= 0) &&
10003 (geo
->level
!= UnSet
)) {
10004 switch (info
.array
.level
) {
10006 if (geo
->level
== 5) {
10007 change
= CH_MIGRATION
;
10008 if (geo
->layout
!= ALGORITHM_LEFT_ASYMMETRIC
) {
10009 pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n");
10011 goto analyse_change_exit
;
10013 imsm_layout
= geo
->layout
;
10015 devNumChange
= 1; /* parity disk added */
10016 } else if (geo
->level
== 10) {
10017 change
= CH_TAKEOVER
;
10019 devNumChange
= 2; /* two mirrors added */
10020 imsm_layout
= 0x102; /* imsm supported layout */
10025 if (geo
->level
== 0) {
10026 change
= CH_TAKEOVER
;
10028 devNumChange
= -(geo
->raid_disks
/2);
10029 imsm_layout
= 0; /* imsm raid0 layout */
10033 if (change
== -1) {
10034 pr_err("Error. Level Migration from %d to %d not supported!\n",
10035 info
.array
.level
, geo
->level
);
10036 goto analyse_change_exit
;
10039 geo
->level
= info
.array
.level
;
10041 if ((geo
->layout
!= info
.array
.layout
)
10042 && ((geo
->layout
!= UnSet
) && (geo
->layout
!= -1))) {
10043 change
= CH_MIGRATION
;
10044 if ((info
.array
.layout
== 0)
10045 && (info
.array
.level
== 5)
10046 && (geo
->layout
== 5)) {
10047 /* reshape 5 -> 4 */
10048 } else if ((info
.array
.layout
== 5)
10049 && (info
.array
.level
== 5)
10050 && (geo
->layout
== 0)) {
10051 /* reshape 4 -> 5 */
10055 pr_err("Error. Layout Migration from %d to %d not supported!\n",
10056 info
.array
.layout
, geo
->layout
);
10058 goto analyse_change_exit
;
10061 geo
->layout
= info
.array
.layout
;
10062 if (imsm_layout
== -1)
10063 imsm_layout
= info
.array
.layout
;
10066 if ((geo
->chunksize
> 0) && (geo
->chunksize
!= UnSet
)
10067 && (geo
->chunksize
!= info
.array
.chunk_size
))
10068 change
= CH_MIGRATION
;
10070 geo
->chunksize
= info
.array
.chunk_size
;
10072 chunk
= geo
->chunksize
/ 1024;
10075 dev
= get_imsm_dev(super
, super
->current_vol
);
10076 data_disks
= imsm_num_data_members(dev
, MAP_0
);
10077 /* compute current size per disk member
10079 current_size
= info
.custom_array_size
/ data_disks
;
10081 if ((geo
->size
> 0) && (geo
->size
!= MAX_SIZE
)) {
10082 /* align component size
10084 geo
->size
= imsm_component_size_aligment_check(
10085 get_imsm_raid_level(dev
->vol
.map
),
10088 if (geo
->size
== 0) {
10089 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is 0).\n",
10091 goto analyse_change_exit
;
10095 if ((current_size
!= geo
->size
) && (geo
->size
> 0)) {
10096 if (change
!= -1) {
10097 pr_err("Error. Size change should be the only one at a time.\n");
10099 goto analyse_change_exit
;
10101 if ((super
->current_vol
+ 1) != super
->anchor
->num_raid_devs
) {
10102 pr_err("Error. The last volume in container can be expanded only (%i/%s).\n",
10103 super
->current_vol
, st
->devnm
);
10104 goto analyse_change_exit
;
10106 /* check the maximum available size
10108 rv
= imsm_get_free_size(st
, dev
->vol
.map
->num_members
,
10109 0, chunk
, &free_size
);
10111 /* Cannot find maximum available space
10115 max_size
= free_size
+ current_size
;
10116 /* align component size
10118 max_size
= imsm_component_size_aligment_check(
10119 get_imsm_raid_level(dev
->vol
.map
),
10123 if (geo
->size
== MAX_SIZE
) {
10124 /* requested size change to the maximum available size
10126 if (max_size
== 0) {
10127 pr_err("Error. Cannot find maximum available space.\n");
10129 goto analyse_change_exit
;
10131 geo
->size
= max_size
;
10134 if ((direction
== ROLLBACK_METADATA_CHANGES
)) {
10135 /* accept size for rollback only
10138 /* round size due to metadata compatibility
10140 geo
->size
= (geo
->size
>> SECT_PER_MB_SHIFT
)
10141 << SECT_PER_MB_SHIFT
;
10142 dprintf("Prepare update for size change to %llu\n",
10144 if (current_size
>= geo
->size
) {
10145 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is %llu).\n",
10146 current_size
, geo
->size
);
10147 goto analyse_change_exit
;
10149 if (max_size
&& geo
->size
> max_size
) {
10150 pr_err("Error. Requested size is larger than maximum available size (maximum available size is %llu, requested size /rounded/ is %llu).\n",
10151 max_size
, geo
->size
);
10152 goto analyse_change_exit
;
10155 geo
->size
*= data_disks
;
10156 geo
->raid_disks
= dev
->vol
.map
->num_members
;
10157 change
= CH_ARRAY_SIZE
;
10159 if (!validate_geometry_imsm(st
,
10162 geo
->raid_disks
+ devNumChange
,
10164 geo
->size
, INVALID_SECTORS
,
10169 struct intel_super
*super
= st
->sb
;
10170 struct imsm_super
*mpb
= super
->anchor
;
10172 if (mpb
->num_raid_devs
> 1) {
10173 pr_err("Error. Cannot perform operation on %s- for this operation it MUST be single array in container\n",
10179 analyse_change_exit
:
10180 if ((direction
== ROLLBACK_METADATA_CHANGES
) &&
10181 ((change
== CH_MIGRATION
) || (change
== CH_TAKEOVER
))) {
10182 dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n");
10188 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
10190 struct intel_super
*super
= st
->sb
;
10191 struct imsm_update_takeover
*u
;
10193 u
= xmalloc(sizeof(struct imsm_update_takeover
));
10195 u
->type
= update_takeover
;
10196 u
->subarray
= super
->current_vol
;
10198 /* 10->0 transition */
10199 if (geo
->level
== 0)
10200 u
->direction
= R10_TO_R0
;
10202 /* 0->10 transition */
10203 if (geo
->level
== 10)
10204 u
->direction
= R0_TO_R10
;
10206 /* update metadata locally */
10207 imsm_update_metadata_locally(st
, u
,
10208 sizeof(struct imsm_update_takeover
));
10209 /* and possibly remotely */
10210 if (st
->update_tail
)
10211 append_metadata_update(st
, u
,
10212 sizeof(struct imsm_update_takeover
));
10219 static int imsm_reshape_super(struct supertype
*st
, unsigned long long size
,
10221 int layout
, int chunksize
, int raid_disks
,
10222 int delta_disks
, char *backup
, char *dev
,
10223 int direction
, int verbose
)
10226 struct geo_params geo
;
10228 dprintf("(enter)\n");
10230 memset(&geo
, 0, sizeof(struct geo_params
));
10232 geo
.dev_name
= dev
;
10233 strcpy(geo
.devnm
, st
->devnm
);
10236 geo
.layout
= layout
;
10237 geo
.chunksize
= chunksize
;
10238 geo
.raid_disks
= raid_disks
;
10239 if (delta_disks
!= UnSet
)
10240 geo
.raid_disks
+= delta_disks
;
10242 dprintf("for level : %i\n", geo
.level
);
10243 dprintf("for raid_disks : %i\n", geo
.raid_disks
);
10245 if (experimental() == 0)
10248 if (strcmp(st
->container_devnm
, st
->devnm
) == 0) {
10249 /* On container level we can only increase number of devices. */
10250 dprintf("imsm: info: Container operation\n");
10251 int old_raid_disks
= 0;
10253 if (imsm_reshape_is_allowed_on_container(
10254 st
, &geo
, &old_raid_disks
, direction
)) {
10255 struct imsm_update_reshape
*u
= NULL
;
10258 len
= imsm_create_metadata_update_for_reshape(
10259 st
, &geo
, old_raid_disks
, &u
);
10262 dprintf("imsm: Cannot prepare update\n");
10263 goto exit_imsm_reshape_super
;
10267 /* update metadata locally */
10268 imsm_update_metadata_locally(st
, u
, len
);
10269 /* and possibly remotely */
10270 if (st
->update_tail
)
10271 append_metadata_update(st
, u
, len
);
10276 pr_err("(imsm) Operation is not allowed on this container\n");
10279 /* On volume level we support following operations
10280 * - takeover: raid10 -> raid0; raid0 -> raid10
10281 * - chunk size migration
10282 * - migration: raid5 -> raid0; raid0 -> raid5
10284 struct intel_super
*super
= st
->sb
;
10285 struct intel_dev
*dev
= super
->devlist
;
10287 dprintf("imsm: info: Volume operation\n");
10288 /* find requested device */
10291 imsm_find_array_devnm_by_subdev(
10292 dev
->index
, st
->container_devnm
);
10293 if (devnm
&& strcmp(devnm
, geo
.devnm
) == 0)
10298 pr_err("Cannot find %s (%s) subarray\n",
10299 geo
.dev_name
, geo
.devnm
);
10300 goto exit_imsm_reshape_super
;
10302 super
->current_vol
= dev
->index
;
10303 change
= imsm_analyze_change(st
, &geo
, direction
);
10306 ret_val
= imsm_takeover(st
, &geo
);
10308 case CH_MIGRATION
: {
10309 struct imsm_update_reshape_migration
*u
= NULL
;
10311 imsm_create_metadata_update_for_migration(
10314 dprintf("imsm: Cannot prepare update\n");
10318 /* update metadata locally */
10319 imsm_update_metadata_locally(st
, u
, len
);
10320 /* and possibly remotely */
10321 if (st
->update_tail
)
10322 append_metadata_update(st
, u
, len
);
10327 case CH_ARRAY_SIZE
: {
10328 struct imsm_update_size_change
*u
= NULL
;
10330 imsm_create_metadata_update_for_size_change(
10333 dprintf("imsm: Cannot prepare update\n");
10337 /* update metadata locally */
10338 imsm_update_metadata_locally(st
, u
, len
);
10339 /* and possibly remotely */
10340 if (st
->update_tail
)
10341 append_metadata_update(st
, u
, len
);
10351 exit_imsm_reshape_super
:
10352 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
10356 /*******************************************************************************
10357 * Function: wait_for_reshape_imsm
10358 * Description: Function writes new sync_max value and waits until
10359 * reshape process reach new position
10361 * sra : general array info
10362 * ndata : number of disks in new array's layout
10365 * 1 : there is no reshape in progress,
10367 ******************************************************************************/
10368 int wait_for_reshape_imsm(struct mdinfo
*sra
, int ndata
)
10370 int fd
= sysfs_get_fd(sra
, NULL
, "sync_completed");
10371 unsigned long long completed
;
10372 /* to_complete : new sync_max position */
10373 unsigned long long to_complete
= sra
->reshape_progress
;
10374 unsigned long long position_to_set
= to_complete
/ ndata
;
10377 dprintf("cannot open reshape_position\n");
10381 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
10382 dprintf("cannot read reshape_position (no reshape in progres)\n");
10387 if (completed
> position_to_set
) {
10388 dprintf("wrong next position to set %llu (%llu)\n",
10389 to_complete
, position_to_set
);
10393 dprintf("Position set: %llu\n", position_to_set
);
10394 if (sysfs_set_num(sra
, NULL
, "sync_max",
10395 position_to_set
) != 0) {
10396 dprintf("cannot set reshape position to %llu\n",
10404 int timeout
= 3000;
10405 sysfs_wait(fd
, &timeout
);
10406 if (sysfs_get_str(sra
, NULL
, "sync_action",
10408 strncmp(action
, "reshape", 7) != 0) {
10412 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
10413 dprintf("cannot read reshape_position (in loop)\n");
10417 } while (completed
< position_to_set
);
10423 /*******************************************************************************
10424 * Function: check_degradation_change
10425 * Description: Check that array hasn't become failed.
10427 * info : for sysfs access
10428 * sources : source disks descriptors
10429 * degraded: previous degradation level
10431 * degradation level
10432 ******************************************************************************/
10433 int check_degradation_change(struct mdinfo
*info
,
10437 unsigned long long new_degraded
;
10440 rv
= sysfs_get_ll(info
, NULL
, "degraded", &new_degraded
);
10441 if ((rv
== -1) || (new_degraded
!= (unsigned long long)degraded
)) {
10442 /* check each device to ensure it is still working */
10445 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
10446 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
10448 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
10450 if (sysfs_get_str(info
,
10451 sd
, "state", sbuf
, 20) < 0 ||
10452 strstr(sbuf
, "faulty") ||
10453 strstr(sbuf
, "in_sync") == NULL
) {
10454 /* this device is dead */
10455 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
10456 if (sd
->disk
.raid_disk
>= 0 &&
10457 sources
[sd
->disk
.raid_disk
] >= 0) {
10459 sd
->disk
.raid_disk
]);
10460 sources
[sd
->disk
.raid_disk
] =
10469 return new_degraded
;
10472 /*******************************************************************************
10473 * Function: imsm_manage_reshape
10474 * Description: Function finds array under reshape and it manages reshape
10475 * process. It creates stripes backups (if required) and sets
10478 * afd : Backup handle (nattive) - not used
10479 * sra : general array info
10480 * reshape : reshape parameters - not used
10481 * st : supertype structure
10482 * blocks : size of critical section [blocks]
10483 * fds : table of source device descriptor
10484 * offsets : start of array (offest per devices)
10486 * destfd : table of destination device descriptor
10487 * destoffsets : table of destination offsets (per device)
10489 * 1 : success, reshape is done
10491 ******************************************************************************/
10492 static int imsm_manage_reshape(
10493 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
10494 struct supertype
*st
, unsigned long backup_blocks
,
10495 int *fds
, unsigned long long *offsets
,
10496 int dests
, int *destfd
, unsigned long long *destoffsets
)
10499 struct intel_super
*super
= st
->sb
;
10500 struct intel_dev
*dv
= NULL
;
10501 struct imsm_dev
*dev
= NULL
;
10502 struct imsm_map
*map_src
;
10503 int migr_vol_qan
= 0;
10504 int ndata
, odata
; /* [bytes] */
10505 int chunk
; /* [bytes] */
10506 struct migr_record
*migr_rec
;
10508 unsigned int buf_size
; /* [bytes] */
10509 unsigned long long max_position
; /* array size [bytes] */
10510 unsigned long long next_step
; /* [blocks]/[bytes] */
10511 unsigned long long old_data_stripe_length
;
10512 unsigned long long start_src
; /* [bytes] */
10513 unsigned long long start
; /* [bytes] */
10514 unsigned long long start_buf_shift
; /* [bytes] */
10516 int source_layout
= 0;
10518 if (!fds
|| !offsets
|| !sra
)
10521 /* Find volume during the reshape */
10522 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
10523 if (dv
->dev
->vol
.migr_type
== MIGR_GEN_MIGR
10524 && dv
->dev
->vol
.migr_state
== 1) {
10529 /* Only one volume can migrate at the same time */
10530 if (migr_vol_qan
!= 1) {
10531 pr_err(": %s", migr_vol_qan
?
10532 "Number of migrating volumes greater than 1\n" :
10533 "There is no volume during migrationg\n");
10537 map_src
= get_imsm_map(dev
, MAP_1
);
10538 if (map_src
== NULL
)
10541 ndata
= imsm_num_data_members(dev
, MAP_0
);
10542 odata
= imsm_num_data_members(dev
, MAP_1
);
10544 chunk
= __le16_to_cpu(map_src
->blocks_per_strip
) * 512;
10545 old_data_stripe_length
= odata
* chunk
;
10547 migr_rec
= super
->migr_rec
;
10549 /* initialize migration record for start condition */
10550 if (sra
->reshape_progress
== 0)
10551 init_migr_record_imsm(st
, dev
, sra
);
10553 if (__le32_to_cpu(migr_rec
->rec_status
) != UNIT_SRC_NORMAL
) {
10554 dprintf("imsm: cannot restart migration when data are present in copy area.\n");
10557 /* Save checkpoint to update migration record for current
10558 * reshape position (in md). It can be farther than current
10559 * reshape position in metadata.
10561 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
10562 /* ignore error == 2, this can mean end of reshape here
10564 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n");
10569 /* size for data */
10570 buf_size
= __le32_to_cpu(migr_rec
->blocks_per_unit
) * 512;
10571 /* extend buffer size for parity disk */
10572 buf_size
+= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
10573 /* add space for stripe aligment */
10574 buf_size
+= old_data_stripe_length
;
10575 if (posix_memalign((void **)&buf
, 4096, buf_size
)) {
10576 dprintf("imsm: Cannot allocate checpoint buffer\n");
10580 max_position
= sra
->component_size
* ndata
;
10581 source_layout
= imsm_level_to_layout(map_src
->raid_level
);
10583 while (__le32_to_cpu(migr_rec
->curr_migr_unit
) <
10584 __le32_to_cpu(migr_rec
->num_migr_units
)) {
10585 /* current reshape position [blocks] */
10586 unsigned long long current_position
=
10587 __le32_to_cpu(migr_rec
->blocks_per_unit
)
10588 * __le32_to_cpu(migr_rec
->curr_migr_unit
);
10589 unsigned long long border
;
10591 /* Check that array hasn't become failed.
10593 degraded
= check_degradation_change(sra
, fds
, degraded
);
10594 if (degraded
> 1) {
10595 dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded
);
10599 next_step
= __le32_to_cpu(migr_rec
->blocks_per_unit
);
10601 if ((current_position
+ next_step
) > max_position
)
10602 next_step
= max_position
- current_position
;
10604 start
= current_position
* 512;
10606 /* align reading start to old geometry */
10607 start_buf_shift
= start
% old_data_stripe_length
;
10608 start_src
= start
- start_buf_shift
;
10610 border
= (start_src
/ odata
) - (start
/ ndata
);
10612 if (border
<= __le32_to_cpu(migr_rec
->dest_depth_per_unit
)) {
10613 /* save critical stripes to buf
10614 * start - start address of current unit
10615 * to backup [bytes]
10616 * start_src - start address of current unit
10617 * to backup alligned to source array
10620 unsigned long long next_step_filler
= 0;
10621 unsigned long long copy_length
= next_step
* 512;
10623 /* allign copy area length to stripe in old geometry */
10624 next_step_filler
= ((copy_length
+ start_buf_shift
)
10625 % old_data_stripe_length
);
10626 if (next_step_filler
)
10627 next_step_filler
= (old_data_stripe_length
10628 - next_step_filler
);
10629 dprintf("save_stripes() parameters: start = %llu,\tstart_src = %llu,\tnext_step*512 = %llu,\tstart_in_buf_shift = %llu,\tnext_step_filler = %llu\n",
10630 start
, start_src
, copy_length
,
10631 start_buf_shift
, next_step_filler
);
10633 if (save_stripes(fds
, offsets
, map_src
->num_members
,
10634 chunk
, map_src
->raid_level
,
10635 source_layout
, 0, NULL
, start_src
,
10637 next_step_filler
+ start_buf_shift
,
10639 dprintf("imsm: Cannot save stripes to buffer\n");
10642 /* Convert data to destination format and store it
10643 * in backup general migration area
10645 if (save_backup_imsm(st
, dev
, sra
,
10646 buf
+ start_buf_shift
, copy_length
)) {
10647 dprintf("imsm: Cannot save stripes to target devices\n");
10650 if (save_checkpoint_imsm(st
, sra
,
10651 UNIT_SRC_IN_CP_AREA
)) {
10652 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n");
10656 /* set next step to use whole border area */
10657 border
/= next_step
;
10659 next_step
*= border
;
10661 /* When data backed up, checkpoint stored,
10662 * kick the kernel to reshape unit of data
10664 next_step
= next_step
+ sra
->reshape_progress
;
10665 /* limit next step to array max position */
10666 if (next_step
> max_position
)
10667 next_step
= max_position
;
10668 sysfs_set_num(sra
, NULL
, "suspend_lo", sra
->reshape_progress
);
10669 sysfs_set_num(sra
, NULL
, "suspend_hi", next_step
);
10670 sra
->reshape_progress
= next_step
;
10672 /* wait until reshape finish */
10673 if (wait_for_reshape_imsm(sra
, ndata
)) {
10674 dprintf("wait_for_reshape_imsm returned error!\n");
10680 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
10681 /* ignore error == 2, this can mean end of reshape here
10683 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n");
10689 /* clear migr_rec on disks after successful migration */
10692 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SIZE
);
10693 for (d
= super
->disks
; d
; d
= d
->next
) {
10694 if (d
->index
< 0 || is_failed(&d
->disk
))
10696 unsigned long long dsize
;
10698 get_dev_size(d
->fd
, NULL
, &dsize
);
10699 if (lseek64(d
->fd
, dsize
- MIGR_REC_POSITION
,
10701 if (write(d
->fd
, super
->migr_rec_buf
,
10702 MIGR_REC_BUF_SIZE
) != MIGR_REC_BUF_SIZE
)
10703 perror("Write migr_rec failed");
10707 /* return '1' if done */
10711 /* See Grow.c: abort_reshape() for further explanation */
10712 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
10713 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
10714 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
10719 #endif /* MDASSEMBLE */
10721 struct superswitch super_imsm
= {
10723 .examine_super
= examine_super_imsm
,
10724 .brief_examine_super
= brief_examine_super_imsm
,
10725 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
10726 .export_examine_super
= export_examine_super_imsm
,
10727 .detail_super
= detail_super_imsm
,
10728 .brief_detail_super
= brief_detail_super_imsm
,
10729 .write_init_super
= write_init_super_imsm
,
10730 .validate_geometry
= validate_geometry_imsm
,
10731 .add_to_super
= add_to_super_imsm
,
10732 .remove_from_super
= remove_from_super_imsm
,
10733 .detail_platform
= detail_platform_imsm
,
10734 .export_detail_platform
= export_detail_platform_imsm
,
10735 .kill_subarray
= kill_subarray_imsm
,
10736 .update_subarray
= update_subarray_imsm
,
10737 .load_container
= load_container_imsm
,
10738 .default_geometry
= default_geometry_imsm
,
10739 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
10740 .reshape_super
= imsm_reshape_super
,
10741 .manage_reshape
= imsm_manage_reshape
,
10742 .recover_backup
= recover_backup_imsm
,
10743 .copy_metadata
= copy_metadata_imsm
,
10745 .match_home
= match_home_imsm
,
10746 .uuid_from_super
= uuid_from_super_imsm
,
10747 .getinfo_super
= getinfo_super_imsm
,
10748 .getinfo_super_disks
= getinfo_super_disks_imsm
,
10749 .update_super
= update_super_imsm
,
10751 .avail_size
= avail_size_imsm
,
10752 .min_acceptable_spare_size
= min_acceptable_spare_size_imsm
,
10754 .compare_super
= compare_super_imsm
,
10756 .load_super
= load_super_imsm
,
10757 .init_super
= init_super_imsm
,
10758 .store_super
= store_super_imsm
,
10759 .free_super
= free_super_imsm
,
10760 .match_metadata_desc
= match_metadata_desc_imsm
,
10761 .container_content
= container_content_imsm
,
10762 .validate_container
= validate_container_imsm
,
10769 .open_new
= imsm_open_new
,
10770 .set_array_state
= imsm_set_array_state
,
10771 .set_disk
= imsm_set_disk
,
10772 .sync_metadata
= imsm_sync_metadata
,
10773 .activate_spare
= imsm_activate_spare
,
10774 .process_update
= imsm_process_update
,
10775 .prepare_update
= imsm_prepare_update
,
10776 #endif /* MDASSEMBLE */