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
93 #define MAX_SECTOR_SIZE 4096
95 /* Disk configuration info. */
96 #define IMSM_MAX_DEVICES 255
98 __u8 serial
[MAX_RAID_SERIAL_LEN
];/* 0xD8 - 0xE7 ascii serial number */
99 __u32 total_blocks_lo
; /* 0xE8 - 0xEB total blocks lo */
100 __u32 scsi_id
; /* 0xEC - 0xEF scsi ID */
101 #define SPARE_DISK __cpu_to_le32(0x01) /* Spare */
102 #define CONFIGURED_DISK __cpu_to_le32(0x02) /* Member of some RaidDev */
103 #define FAILED_DISK __cpu_to_le32(0x04) /* Permanent failure */
104 __u32 status
; /* 0xF0 - 0xF3 */
105 __u32 owner_cfg_num
; /* which config 0,1,2... owns this disk */
106 __u32 total_blocks_hi
; /* 0xF4 - 0xF5 total blocks hi */
107 #define IMSM_DISK_FILLERS 3
108 __u32 filler
[IMSM_DISK_FILLERS
]; /* 0xF5 - 0x107 MPB_DISK_FILLERS for future expansion */
111 /* map selector for map managment
117 /* RAID map configuration infos. */
119 __u32 pba_of_lba0_lo
; /* start address of partition */
120 __u32 blocks_per_member_lo
;/* blocks per member */
121 __u32 num_data_stripes_lo
; /* number of data stripes */
122 __u16 blocks_per_strip
;
123 __u8 map_state
; /* Normal, Uninitialized, Degraded, Failed */
124 #define IMSM_T_STATE_NORMAL 0
125 #define IMSM_T_STATE_UNINITIALIZED 1
126 #define IMSM_T_STATE_DEGRADED 2
127 #define IMSM_T_STATE_FAILED 3
129 #define IMSM_T_RAID0 0
130 #define IMSM_T_RAID1 1
131 #define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */
132 __u8 num_members
; /* number of member disks */
133 __u8 num_domains
; /* number of parity domains */
134 __u8 failed_disk_num
; /* valid only when state is degraded */
136 __u32 pba_of_lba0_hi
;
137 __u32 blocks_per_member_hi
;
138 __u32 num_data_stripes_hi
;
139 __u32 filler
[4]; /* expansion area */
140 #define IMSM_ORD_REBUILD (1 << 24)
141 __u32 disk_ord_tbl
[1]; /* disk_ord_tbl[num_members],
142 * top byte contains some flags
144 } __attribute__ ((packed
));
147 __u32 curr_migr_unit
;
148 __u32 checkpoint_id
; /* id to access curr_migr_unit */
149 __u8 migr_state
; /* Normal or Migrating */
151 #define MIGR_REBUILD 1
152 #define MIGR_VERIFY 2 /* analagous to echo check > sync_action */
153 #define MIGR_GEN_MIGR 3
154 #define MIGR_STATE_CHANGE 4
155 #define MIGR_REPAIR 5
156 __u8 migr_type
; /* Initializing, Rebuilding, ... */
158 __u8 fs_state
; /* fast-sync state for CnG (0xff == disabled) */
159 __u16 verify_errors
; /* number of mismatches */
160 __u16 bad_blocks
; /* number of bad blocks during verify */
162 struct imsm_map map
[1];
163 /* here comes another one if migr_state */
164 } __attribute__ ((packed
));
167 __u8 volume
[MAX_RAID_SERIAL_LEN
];
170 #define DEV_BOOTABLE __cpu_to_le32(0x01)
171 #define DEV_BOOT_DEVICE __cpu_to_le32(0x02)
172 #define DEV_READ_COALESCING __cpu_to_le32(0x04)
173 #define DEV_WRITE_COALESCING __cpu_to_le32(0x08)
174 #define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10)
175 #define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20)
176 #define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40)
177 #define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80)
178 #define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100)
179 #define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200)
180 #define DEV_CLONE_N_GO __cpu_to_le32(0x400)
181 #define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800)
182 #define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000)
183 __u32 status
; /* Persistent RaidDev status */
184 __u32 reserved_blocks
; /* Reserved blocks at beginning of volume */
188 __u8 cng_master_disk
;
192 #define IMSM_DEV_FILLERS 10
193 __u32 filler
[IMSM_DEV_FILLERS
];
195 } __attribute__ ((packed
));
198 __u8 sig
[MAX_SIGNATURE_LENGTH
]; /* 0x00 - 0x1F */
199 __u32 check_sum
; /* 0x20 - 0x23 MPB Checksum */
200 __u32 mpb_size
; /* 0x24 - 0x27 Size of MPB */
201 __u32 family_num
; /* 0x28 - 0x2B Checksum from first time this config was written */
202 __u32 generation_num
; /* 0x2C - 0x2F Incremented each time this array's MPB is written */
203 __u32 error_log_size
; /* 0x30 - 0x33 in bytes */
204 __u32 attributes
; /* 0x34 - 0x37 */
205 __u8 num_disks
; /* 0x38 Number of configured disks */
206 __u8 num_raid_devs
; /* 0x39 Number of configured volumes */
207 __u8 error_log_pos
; /* 0x3A */
208 __u8 fill
[1]; /* 0x3B */
209 __u32 cache_size
; /* 0x3c - 0x40 in mb */
210 __u32 orig_family_num
; /* 0x40 - 0x43 original family num */
211 __u32 pwr_cycle_count
; /* 0x44 - 0x47 simulated power cycle count for array */
212 __u32 bbm_log_size
; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */
213 #define IMSM_FILLERS 35
214 __u32 filler
[IMSM_FILLERS
]; /* 0x4C - 0xD7 RAID_MPB_FILLERS */
215 struct imsm_disk disk
[1]; /* 0xD8 diskTbl[numDisks] */
216 /* here comes imsm_dev[num_raid_devs] */
217 /* here comes BBM logs */
218 } __attribute__ ((packed
));
220 #define BBM_LOG_MAX_ENTRIES 254
222 struct bbm_log_entry
{
223 __u64 defective_block_start
;
224 #define UNREADABLE 0xFFFFFFFF
225 __u32 spare_block_offset
;
226 __u16 remapped_marked_count
;
228 } __attribute__ ((__packed__
));
231 __u32 signature
; /* 0xABADB10C */
233 __u32 reserved_spare_block_count
; /* 0 */
234 __u32 reserved
; /* 0xFFFF */
235 __u64 first_spare_lba
;
236 struct bbm_log_entry mapped_block_entries
[BBM_LOG_MAX_ENTRIES
];
237 } __attribute__ ((__packed__
));
240 static char *map_state_str
[] = { "normal", "uninitialized", "degraded", "failed" };
243 #define RAID_DISK_RESERVED_BLOCKS_IMSM_HI 2209
245 #define GEN_MIGR_AREA_SIZE 2048 /* General Migration Copy Area size in blocks */
247 #define MIGR_REC_BUF_SIZE 512 /* size of migr_record i/o buffer */
248 #define MIGR_REC_POSITION 512 /* migr_record position offset on disk,
249 * MIGR_REC_BUF_SIZE <= MIGR_REC_POSITION
252 #define UNIT_SRC_NORMAL 0 /* Source data for curr_migr_unit must
253 * be recovered using srcMap */
254 #define UNIT_SRC_IN_CP_AREA 1 /* Source data for curr_migr_unit has
255 * already been migrated and must
256 * be recovered from checkpoint area */
258 __u32 rec_status
; /* Status used to determine how to restart
259 * migration in case it aborts
261 __u32 curr_migr_unit
; /* 0..numMigrUnits-1 */
262 __u32 family_num
; /* Family number of MPB
263 * containing the RaidDev
264 * that is migrating */
265 __u32 ascending_migr
; /* True if migrating in increasing
267 __u32 blocks_per_unit
; /* Num disk blocks per unit of operation */
268 __u32 dest_depth_per_unit
; /* Num member blocks each destMap
270 * advances per unit-of-operation */
271 __u32 ckpt_area_pba
; /* Pba of first block of ckpt copy area */
272 __u32 dest_1st_member_lba
; /* First member lba on first
273 * stripe of destination */
274 __u32 num_migr_units
; /* Total num migration units-of-op */
275 __u32 post_migr_vol_cap
; /* Size of volume after
276 * migration completes */
277 __u32 post_migr_vol_cap_hi
; /* Expansion space for LBA64 */
278 __u32 ckpt_read_disk_num
; /* Which member disk in destSubMap[0] the
279 * migration ckpt record was read from
280 * (for recovered migrations) */
281 } __attribute__ ((__packed__
));
286 * 2: metadata does not match
294 struct md_list
*next
;
297 #define pr_vrb(fmt, arg...) (void) (verbose && pr_err(fmt, ##arg))
299 static __u8
migr_type(struct imsm_dev
*dev
)
301 if (dev
->vol
.migr_type
== MIGR_VERIFY
&&
302 dev
->status
& DEV_VERIFY_AND_FIX
)
305 return dev
->vol
.migr_type
;
308 static void set_migr_type(struct imsm_dev
*dev
, __u8 migr_type
)
310 /* for compatibility with older oroms convert MIGR_REPAIR, into
311 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
313 if (migr_type
== MIGR_REPAIR
) {
314 dev
->vol
.migr_type
= MIGR_VERIFY
;
315 dev
->status
|= DEV_VERIFY_AND_FIX
;
317 dev
->vol
.migr_type
= migr_type
;
318 dev
->status
&= ~DEV_VERIFY_AND_FIX
;
322 static unsigned int sector_count(__u32 bytes
, unsigned int sector_size
)
324 return ROUND_UP(bytes
, sector_size
) / sector_size
;
327 static unsigned int mpb_sectors(struct imsm_super
*mpb
,
328 unsigned int sector_size
)
330 return sector_count(__le32_to_cpu(mpb
->mpb_size
), sector_size
);
334 struct imsm_dev
*dev
;
335 struct intel_dev
*next
;
340 enum sys_dev_type type
;
343 struct intel_hba
*next
;
350 /* internal representation of IMSM metadata */
353 void *buf
; /* O_DIRECT buffer for reading/writing metadata */
354 struct imsm_super
*anchor
; /* immovable parameters */
357 void *migr_rec_buf
; /* buffer for I/O operations */
358 struct migr_record
*migr_rec
; /* migration record */
360 int clean_migration_record_by_mdmon
; /* when reshape is switched to next
361 array, it indicates that mdmon is allowed to clean migration
363 size_t len
; /* size of the 'buf' allocation */
364 void *next_buf
; /* for realloc'ing buf from the manager */
366 int updates_pending
; /* count of pending updates for mdmon */
367 int current_vol
; /* index of raid device undergoing creation */
368 unsigned long long create_offset
; /* common start for 'current_vol' */
369 __u32 random
; /* random data for seeding new family numbers */
370 struct intel_dev
*devlist
;
371 unsigned int sector_size
; /* sector size of used member drives */
375 __u8 serial
[MAX_RAID_SERIAL_LEN
];
378 struct imsm_disk disk
;
381 struct extent
*e
; /* for determining freespace @ create */
382 int raiddisk
; /* slot to fill in autolayout */
384 } *disks
, *current_disk
;
385 struct dl
*disk_mgmt_list
; /* list of disks to add/remove while mdmon
387 struct dl
*missing
; /* disks removed while we weren't looking */
388 struct bbm_log
*bbm_log
;
389 struct intel_hba
*hba
; /* device path of the raid controller for this metadata */
390 const struct imsm_orom
*orom
; /* platform firmware support */
391 struct intel_super
*next
; /* (temp) list for disambiguating family_num */
395 struct imsm_disk disk
;
396 #define IMSM_UNKNOWN_OWNER (-1)
398 struct intel_disk
*next
;
402 unsigned long long start
, size
;
405 /* definitions of reshape process types */
406 enum imsm_reshape_type
{
412 /* definition of messages passed to imsm_process_update */
413 enum imsm_update_type
{
414 update_activate_spare
,
418 update_add_remove_disk
,
419 update_reshape_container_disks
,
420 update_reshape_migration
,
422 update_general_migration_checkpoint
,
426 struct imsm_update_activate_spare
{
427 enum imsm_update_type type
;
431 struct imsm_update_activate_spare
*next
;
437 unsigned long long size
;
444 enum takeover_direction
{
448 struct imsm_update_takeover
{
449 enum imsm_update_type type
;
451 enum takeover_direction direction
;
454 struct imsm_update_reshape
{
455 enum imsm_update_type type
;
459 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
462 struct imsm_update_reshape_migration
{
463 enum imsm_update_type type
;
466 /* fields for array migration changes
473 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
476 struct imsm_update_size_change
{
477 enum imsm_update_type type
;
482 struct imsm_update_general_migration_checkpoint
{
483 enum imsm_update_type type
;
484 __u32 curr_migr_unit
;
488 __u8 serial
[MAX_RAID_SERIAL_LEN
];
491 struct imsm_update_create_array
{
492 enum imsm_update_type type
;
497 struct imsm_update_kill_array
{
498 enum imsm_update_type type
;
502 struct imsm_update_rename_array
{
503 enum imsm_update_type type
;
504 __u8 name
[MAX_RAID_SERIAL_LEN
];
508 struct imsm_update_add_remove_disk
{
509 enum imsm_update_type type
;
512 static const char *_sys_dev_type
[] = {
513 [SYS_DEV_UNKNOWN
] = "Unknown",
514 [SYS_DEV_SAS
] = "SAS",
515 [SYS_DEV_SATA
] = "SATA",
516 [SYS_DEV_NVME
] = "NVMe",
517 [SYS_DEV_VMD
] = "VMD"
520 const char *get_sys_dev_type(enum sys_dev_type type
)
522 if (type
>= SYS_DEV_MAX
)
523 type
= SYS_DEV_UNKNOWN
;
525 return _sys_dev_type
[type
];
528 static struct intel_hba
* alloc_intel_hba(struct sys_dev
*device
)
530 struct intel_hba
*result
= xmalloc(sizeof(*result
));
532 result
->type
= device
->type
;
533 result
->path
= xstrdup(device
->path
);
535 if (result
->path
&& (result
->pci_id
= strrchr(result
->path
, '/')) != NULL
)
541 static struct intel_hba
* find_intel_hba(struct intel_hba
*hba
, struct sys_dev
*device
)
543 struct intel_hba
*result
;
545 for (result
= hba
; result
; result
= result
->next
) {
546 if (result
->type
== device
->type
&& strcmp(result
->path
, device
->path
) == 0)
552 static int attach_hba_to_super(struct intel_super
*super
, struct sys_dev
*device
)
554 struct intel_hba
*hba
;
556 /* check if disk attached to Intel HBA */
557 hba
= find_intel_hba(super
->hba
, device
);
560 /* Check if HBA is already attached to super */
561 if (super
->hba
== NULL
) {
562 super
->hba
= alloc_intel_hba(device
);
567 /* Intel metadata allows for all disks attached to the same type HBA.
568 * Do not support HBA types mixing
570 if (device
->type
!= hba
->type
)
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
);
922 static void set_total_blocks(struct imsm_disk
*disk
, unsigned long long n
)
924 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
);
1127 #define IMSM_4K_DIV 8
1130 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
1131 struct imsm_dev
*dev
);
1133 static void print_imsm_dev(struct intel_super
*super
,
1134 struct imsm_dev
*dev
,
1140 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1141 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
1145 printf("[%.16s]:\n", dev
->volume
);
1146 printf(" UUID : %s\n", uuid
);
1147 printf(" RAID Level : %d", get_imsm_raid_level(map
));
1149 printf(" <-- %d", get_imsm_raid_level(map2
));
1151 printf(" Members : %d", map
->num_members
);
1153 printf(" <-- %d", map2
->num_members
);
1155 printf(" Slots : [");
1156 for (i
= 0; i
< map
->num_members
; i
++) {
1157 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
1158 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1163 for (i
= 0; i
< map2
->num_members
; i
++) {
1164 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_1
);
1165 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1170 printf(" Failed disk : ");
1171 if (map
->failed_disk_num
== 0xff)
1174 printf("%i", map
->failed_disk_num
);
1176 slot
= get_imsm_disk_slot(map
, disk_idx
);
1178 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
1179 printf(" This Slot : %d%s\n", slot
,
1180 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
1182 printf(" This Slot : ?\n");
1183 sz
= __le32_to_cpu(dev
->size_high
);
1185 sz
+= __le32_to_cpu(dev
->size_low
);
1186 printf(" Array Size : %llu%s\n", (unsigned long long)sz
,
1187 human_size(sz
* 512));
1188 sz
= blocks_per_member(map
);
1189 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz
,
1190 human_size(sz
* 512));
1191 printf(" Sector Offset : %llu\n",
1193 printf(" Num Stripes : %llu\n",
1194 num_data_stripes(map
));
1195 printf(" Chunk Size : %u KiB",
1196 __le16_to_cpu(map
->blocks_per_strip
) / 2);
1198 printf(" <-- %u KiB",
1199 __le16_to_cpu(map2
->blocks_per_strip
) / 2);
1201 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
1202 printf(" Migrate State : ");
1203 if (dev
->vol
.migr_state
) {
1204 if (migr_type(dev
) == MIGR_INIT
)
1205 printf("initialize\n");
1206 else if (migr_type(dev
) == MIGR_REBUILD
)
1207 printf("rebuild\n");
1208 else if (migr_type(dev
) == MIGR_VERIFY
)
1210 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
1211 printf("general migration\n");
1212 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
1213 printf("state change\n");
1214 else if (migr_type(dev
) == MIGR_REPAIR
)
1217 printf("<unknown:%d>\n", migr_type(dev
));
1220 printf(" Map State : %s", map_state_str
[map
->map_state
]);
1221 if (dev
->vol
.migr_state
) {
1222 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1224 printf(" <-- %s", map_state_str
[map
->map_state
]);
1225 printf("\n Checkpoint : %u ",
1226 __le32_to_cpu(dev
->vol
.curr_migr_unit
));
1227 if (is_gen_migration(dev
) && (slot
> 1 || slot
< 0))
1230 printf("(%llu)", (unsigned long long)
1231 blocks_per_migr_unit(super
, dev
));
1234 printf(" Dirty State : %s\n", dev
->vol
.dirty
? "dirty" : "clean");
1237 static void print_imsm_disk(struct imsm_disk
*disk
, int index
, __u32 reserved
)
1239 char str
[MAX_RAID_SERIAL_LEN
+ 1];
1242 if (index
< -1 || !disk
)
1246 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
1248 printf(" Disk%02d Serial : %s\n", index
, str
);
1250 printf(" Disk Serial : %s\n", str
);
1251 printf(" State :%s%s%s\n", is_spare(disk
) ? " spare" : "",
1252 is_configured(disk
) ? " active" : "",
1253 is_failed(disk
) ? " failed" : "");
1254 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
1255 sz
= total_blocks(disk
) - reserved
;
1256 printf(" Usable Size : %llu%s\n", (unsigned long long)sz
,
1257 human_size(sz
* 512));
1260 void convert_to_4k_imsm_disk(struct imsm_disk
*disk
)
1262 set_total_blocks(disk
, (total_blocks(disk
)/IMSM_4K_DIV
));
1265 void convert_to_4k(struct intel_super
*super
)
1267 struct imsm_super
*mpb
= super
->anchor
;
1268 struct imsm_disk
*disk
;
1271 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1272 disk
= __get_imsm_disk(mpb
, i
);
1274 convert_to_4k_imsm_disk(disk
);
1276 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1277 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1278 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1280 split_ull((join_u32(dev
->size_low
, dev
->size_high
)/IMSM_4K_DIV
),
1281 &dev
->size_low
, &dev
->size_high
);
1282 dev
->vol
.curr_migr_unit
/= IMSM_4K_DIV
;
1285 set_blocks_per_member(map
, blocks_per_member(map
)/IMSM_4K_DIV
);
1286 map
->blocks_per_strip
/= IMSM_4K_DIV
;
1287 set_pba_of_lba0(map
, pba_of_lba0(map
)/IMSM_4K_DIV
);
1289 if (dev
->vol
.migr_state
) {
1291 map
= get_imsm_map(dev
, MAP_1
);
1292 set_blocks_per_member(map
,
1293 blocks_per_member(map
)/IMSM_4K_DIV
);
1294 map
->blocks_per_strip
/= IMSM_4K_DIV
;
1295 set_pba_of_lba0(map
, pba_of_lba0(map
)/IMSM_4K_DIV
);
1299 mpb
->check_sum
= __gen_imsm_checksum(mpb
);
1302 void examine_migr_rec_imsm(struct intel_super
*super
)
1304 struct migr_record
*migr_rec
= super
->migr_rec
;
1305 struct imsm_super
*mpb
= super
->anchor
;
1308 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1309 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1310 struct imsm_map
*map
;
1313 if (is_gen_migration(dev
) == 0)
1316 printf("\nMigration Record Information:");
1318 /* first map under migration */
1319 map
= get_imsm_map(dev
, MAP_0
);
1321 slot
= get_imsm_disk_slot(map
, super
->disks
->index
);
1322 if (map
== NULL
|| slot
> 1 || slot
< 0) {
1323 printf(" Empty\n ");
1324 printf("Examine one of first two disks in array\n");
1327 printf("\n Status : ");
1328 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
1331 printf("Contains Data\n");
1332 printf(" Current Unit : %u\n",
1333 __le32_to_cpu(migr_rec
->curr_migr_unit
));
1334 printf(" Family : %u\n",
1335 __le32_to_cpu(migr_rec
->family_num
));
1336 printf(" Ascending : %u\n",
1337 __le32_to_cpu(migr_rec
->ascending_migr
));
1338 printf(" Blocks Per Unit : %u\n",
1339 __le32_to_cpu(migr_rec
->blocks_per_unit
));
1340 printf(" Dest. Depth Per Unit : %u\n",
1341 __le32_to_cpu(migr_rec
->dest_depth_per_unit
));
1342 printf(" Checkpoint Area pba : %u\n",
1343 __le32_to_cpu(migr_rec
->ckpt_area_pba
));
1344 printf(" First member lba : %u\n",
1345 __le32_to_cpu(migr_rec
->dest_1st_member_lba
));
1346 printf(" Total Number of Units : %u\n",
1347 __le32_to_cpu(migr_rec
->num_migr_units
));
1348 printf(" Size of volume : %u\n",
1349 __le32_to_cpu(migr_rec
->post_migr_vol_cap
));
1350 printf(" Expansion space for LBA64 : %u\n",
1351 __le32_to_cpu(migr_rec
->post_migr_vol_cap_hi
));
1352 printf(" Record was read from : %u\n",
1353 __le32_to_cpu(migr_rec
->ckpt_read_disk_num
));
1358 #endif /* MDASSEMBLE */
1360 void convert_from_4k(struct intel_super
*super
)
1362 struct imsm_super
*mpb
= super
->anchor
;
1363 struct imsm_disk
*disk
;
1366 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1367 disk
= __get_imsm_disk(mpb
, i
);
1369 set_total_blocks(disk
, (total_blocks(disk
)*IMSM_4K_DIV
));
1372 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1373 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1374 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1376 split_ull((join_u32(dev
->size_low
, dev
->size_high
)*IMSM_4K_DIV
),
1377 &dev
->size_low
, &dev
->size_high
);
1378 dev
->vol
.curr_migr_unit
*= IMSM_4K_DIV
;
1381 set_blocks_per_member(map
, blocks_per_member(map
)*IMSM_4K_DIV
);
1382 map
->blocks_per_strip
*= IMSM_4K_DIV
;
1383 set_pba_of_lba0(map
, pba_of_lba0(map
)*IMSM_4K_DIV
);
1385 if (dev
->vol
.migr_state
) {
1387 map
= get_imsm_map(dev
, MAP_1
);
1388 set_blocks_per_member(map
,
1389 blocks_per_member(map
)*IMSM_4K_DIV
);
1390 map
->blocks_per_strip
*= IMSM_4K_DIV
;
1391 set_pba_of_lba0(map
, pba_of_lba0(map
)*IMSM_4K_DIV
);
1395 mpb
->check_sum
= __gen_imsm_checksum(mpb
);
1398 /*******************************************************************************
1399 * function: imsm_check_attributes
1400 * Description: Function checks if features represented by attributes flags
1401 * are supported by mdadm.
1403 * attributes - Attributes read from metadata
1405 * 0 - passed attributes contains unsupported features flags
1406 * 1 - all features are supported
1407 ******************************************************************************/
1408 static int imsm_check_attributes(__u32 attributes
)
1411 __u32 not_supported
= MPB_ATTRIB_SUPPORTED
^0xffffffff;
1413 not_supported
&= ~MPB_ATTRIB_IGNORED
;
1415 not_supported
&= attributes
;
1416 if (not_supported
) {
1417 pr_err("(IMSM): Unsupported attributes : %x\n",
1418 (unsigned)__le32_to_cpu(not_supported
));
1419 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1420 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
1421 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1423 if (not_supported
& MPB_ATTRIB_2TB
) {
1424 dprintf("\t\tMPB_ATTRIB_2TB\n");
1425 not_supported
^= MPB_ATTRIB_2TB
;
1427 if (not_supported
& MPB_ATTRIB_RAID0
) {
1428 dprintf("\t\tMPB_ATTRIB_RAID0\n");
1429 not_supported
^= MPB_ATTRIB_RAID0
;
1431 if (not_supported
& MPB_ATTRIB_RAID1
) {
1432 dprintf("\t\tMPB_ATTRIB_RAID1\n");
1433 not_supported
^= MPB_ATTRIB_RAID1
;
1435 if (not_supported
& MPB_ATTRIB_RAID10
) {
1436 dprintf("\t\tMPB_ATTRIB_RAID10\n");
1437 not_supported
^= MPB_ATTRIB_RAID10
;
1439 if (not_supported
& MPB_ATTRIB_RAID1E
) {
1440 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
1441 not_supported
^= MPB_ATTRIB_RAID1E
;
1443 if (not_supported
& MPB_ATTRIB_RAID5
) {
1444 dprintf("\t\tMPB_ATTRIB_RAID5\n");
1445 not_supported
^= MPB_ATTRIB_RAID5
;
1447 if (not_supported
& MPB_ATTRIB_RAIDCNG
) {
1448 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
1449 not_supported
^= MPB_ATTRIB_RAIDCNG
;
1451 if (not_supported
& MPB_ATTRIB_BBM
) {
1452 dprintf("\t\tMPB_ATTRIB_BBM\n");
1453 not_supported
^= MPB_ATTRIB_BBM
;
1455 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1456 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
1457 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1459 if (not_supported
& MPB_ATTRIB_EXP_STRIPE_SIZE
) {
1460 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
1461 not_supported
^= MPB_ATTRIB_EXP_STRIPE_SIZE
;
1463 if (not_supported
& MPB_ATTRIB_2TB_DISK
) {
1464 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
1465 not_supported
^= MPB_ATTRIB_2TB_DISK
;
1467 if (not_supported
& MPB_ATTRIB_NEVER_USE2
) {
1468 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
1469 not_supported
^= MPB_ATTRIB_NEVER_USE2
;
1471 if (not_supported
& MPB_ATTRIB_NEVER_USE
) {
1472 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
1473 not_supported
^= MPB_ATTRIB_NEVER_USE
;
1477 dprintf("(IMSM): Unknown attributes : %x\n", not_supported
);
1486 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
1488 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
1490 struct intel_super
*super
= st
->sb
;
1491 struct imsm_super
*mpb
= super
->anchor
;
1492 char str
[MAX_SIGNATURE_LENGTH
];
1497 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
1500 snprintf(str
, MPB_SIG_LEN
, "%s", mpb
->sig
);
1501 printf(" Magic : %s\n", str
);
1502 snprintf(str
, strlen(MPB_VERSION_RAID0
), "%s", get_imsm_version(mpb
));
1503 printf(" Version : %s\n", get_imsm_version(mpb
));
1504 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
1505 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
1506 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
1507 printf(" Attributes : ");
1508 if (imsm_check_attributes(mpb
->attributes
))
1509 printf("All supported\n");
1511 printf("not supported\n");
1512 getinfo_super_imsm(st
, &info
, NULL
);
1513 fname_from_uuid(st
, &info
, nbuf
, ':');
1514 printf(" UUID : %s\n", nbuf
+ 5);
1515 sum
= __le32_to_cpu(mpb
->check_sum
);
1516 printf(" Checksum : %08x %s\n", sum
,
1517 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
1518 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
, super
->sector_size
));
1519 printf(" Disks : %d\n", mpb
->num_disks
);
1520 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
1521 print_imsm_disk(__get_imsm_disk(mpb
, super
->disks
->index
), super
->disks
->index
, reserved
);
1522 if (super
->bbm_log
) {
1523 struct bbm_log
*log
= super
->bbm_log
;
1526 printf("Bad Block Management Log:\n");
1527 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
1528 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
1529 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
1530 printf(" Spare Blocks : %d\n", __le32_to_cpu(log
->reserved_spare_block_count
));
1531 printf(" First Spare : %llx\n",
1532 (unsigned long long) __le64_to_cpu(log
->first_spare_lba
));
1534 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1536 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1538 super
->current_vol
= i
;
1539 getinfo_super_imsm(st
, &info
, NULL
);
1540 fname_from_uuid(st
, &info
, nbuf
, ':');
1541 print_imsm_dev(super
, dev
, nbuf
+ 5, super
->disks
->index
);
1543 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1544 if (i
== super
->disks
->index
)
1546 print_imsm_disk(__get_imsm_disk(mpb
, i
), i
, reserved
);
1549 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
1550 if (dl
->index
== -1)
1551 print_imsm_disk(&dl
->disk
, -1, reserved
);
1553 examine_migr_rec_imsm(super
);
1556 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
1558 /* We just write a generic IMSM ARRAY entry */
1561 struct intel_super
*super
= st
->sb
;
1563 if (!super
->anchor
->num_raid_devs
) {
1564 printf("ARRAY metadata=imsm\n");
1568 getinfo_super_imsm(st
, &info
, NULL
);
1569 fname_from_uuid(st
, &info
, nbuf
, ':');
1570 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
1573 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
1575 /* We just write a generic IMSM ARRAY entry */
1579 struct intel_super
*super
= st
->sb
;
1582 if (!super
->anchor
->num_raid_devs
)
1585 getinfo_super_imsm(st
, &info
, NULL
);
1586 fname_from_uuid(st
, &info
, nbuf
, ':');
1587 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1588 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1590 super
->current_vol
= i
;
1591 getinfo_super_imsm(st
, &info
, NULL
);
1592 fname_from_uuid(st
, &info
, nbuf1
, ':');
1593 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
1594 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
1598 static void export_examine_super_imsm(struct supertype
*st
)
1600 struct intel_super
*super
= st
->sb
;
1601 struct imsm_super
*mpb
= super
->anchor
;
1605 getinfo_super_imsm(st
, &info
, NULL
);
1606 fname_from_uuid(st
, &info
, nbuf
, ':');
1607 printf("MD_METADATA=imsm\n");
1608 printf("MD_LEVEL=container\n");
1609 printf("MD_UUID=%s\n", nbuf
+5);
1610 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
1613 static int copy_metadata_imsm(struct supertype
*st
, int from
, int to
)
1615 /* The second last sector of the device contains
1616 * the "struct imsm_super" metadata.
1617 * This contains mpb_size which is the size in bytes of the
1618 * extended metadata. This is located immediately before
1620 * We want to read all that, plus the last sector which
1621 * may contain a migration record, and write it all
1625 unsigned long long dsize
, offset
;
1627 struct imsm_super
*sb
;
1628 struct intel_super
*super
= st
->sb
;
1629 unsigned int sector_size
= super
->sector_size
;
1630 unsigned int written
= 0;
1632 if (posix_memalign(&buf
, 4096, 4096) != 0)
1635 if (!get_dev_size(from
, NULL
, &dsize
))
1638 if (lseek64(from
, dsize
-(2*sector_size
), 0) < 0)
1640 if (read(from
, buf
, sector_size
) != sector_size
)
1643 if (strncmp((char*)sb
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0)
1646 sectors
= mpb_sectors(sb
, sector_size
) + 2;
1647 offset
= dsize
- sectors
* sector_size
;
1648 if (lseek64(from
, offset
, 0) < 0 ||
1649 lseek64(to
, offset
, 0) < 0)
1651 while (written
< sectors
* sector_size
) {
1652 int n
= sectors
*sector_size
- written
;
1655 if (read(from
, buf
, n
) != n
)
1657 if (write(to
, buf
, n
) != n
)
1668 static void detail_super_imsm(struct supertype
*st
, char *homehost
)
1673 getinfo_super_imsm(st
, &info
, NULL
);
1674 fname_from_uuid(st
, &info
, nbuf
, ':');
1675 printf("\n UUID : %s\n", nbuf
+ 5);
1678 static void brief_detail_super_imsm(struct supertype
*st
)
1682 getinfo_super_imsm(st
, &info
, NULL
);
1683 fname_from_uuid(st
, &info
, nbuf
, ':');
1684 printf(" UUID=%s", nbuf
+ 5);
1687 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
);
1688 static void fd2devname(int fd
, char *name
);
1690 static int ahci_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
1692 /* dump an unsorted list of devices attached to AHCI Intel storage
1693 * controller, as well as non-connected ports
1695 int hba_len
= strlen(hba_path
) + 1;
1700 unsigned long port_mask
= (1 << port_count
) - 1;
1702 if (port_count
> (int)sizeof(port_mask
) * 8) {
1704 pr_err("port_count %d out of range\n", port_count
);
1708 /* scroll through /sys/dev/block looking for devices attached to
1711 dir
= opendir("/sys/dev/block");
1715 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
1726 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
1728 path
= devt_to_devpath(makedev(major
, minor
));
1731 if (!path_attached_to_hba(path
, hba_path
)) {
1737 /* retrieve the scsi device type */
1738 if (asprintf(&device
, "/sys/dev/block/%d:%d/device/xxxxxxx", major
, minor
) < 0) {
1740 pr_err("failed to allocate 'device'\n");
1744 sprintf(device
, "/sys/dev/block/%d:%d/device/type", major
, minor
);
1745 if (load_sys(device
, buf
, sizeof(buf
)) != 0) {
1747 pr_err("failed to read device type for %s\n",
1753 type
= strtoul(buf
, NULL
, 10);
1755 /* if it's not a disk print the vendor and model */
1756 if (!(type
== 0 || type
== 7 || type
== 14)) {
1759 sprintf(device
, "/sys/dev/block/%d:%d/device/vendor", major
, minor
);
1760 if (load_sys(device
, buf
, sizeof(buf
)) == 0) {
1761 strncpy(vendor
, buf
, sizeof(vendor
));
1762 vendor
[sizeof(vendor
) - 1] = '\0';
1763 c
= (char *) &vendor
[sizeof(vendor
) - 1];
1764 while (isspace(*c
) || *c
== '\0')
1768 sprintf(device
, "/sys/dev/block/%d:%d/device/model", major
, minor
);
1769 if (load_sys(device
, buf
, sizeof(buf
)) == 0) {
1770 strncpy(model
, buf
, sizeof(model
));
1771 model
[sizeof(model
) - 1] = '\0';
1772 c
= (char *) &model
[sizeof(model
) - 1];
1773 while (isspace(*c
) || *c
== '\0')
1777 if (vendor
[0] && model
[0])
1778 sprintf(buf
, "%.64s %.64s", vendor
, model
);
1780 switch (type
) { /* numbers from hald/linux/device.c */
1781 case 1: sprintf(buf
, "tape"); break;
1782 case 2: sprintf(buf
, "printer"); break;
1783 case 3: sprintf(buf
, "processor"); break;
1785 case 5: sprintf(buf
, "cdrom"); break;
1786 case 6: sprintf(buf
, "scanner"); break;
1787 case 8: sprintf(buf
, "media_changer"); break;
1788 case 9: sprintf(buf
, "comm"); break;
1789 case 12: sprintf(buf
, "raid"); break;
1790 default: sprintf(buf
, "unknown");
1796 /* chop device path to 'host%d' and calculate the port number */
1797 c
= strchr(&path
[hba_len
], '/');
1800 pr_err("%s - invalid path name\n", path
+ hba_len
);
1805 if ((sscanf(&path
[hba_len
], "ata%d", &port
) == 1) ||
1806 ((sscanf(&path
[hba_len
], "host%d", &port
) == 1)))
1810 *c
= '/'; /* repair the full string */
1811 pr_err("failed to determine port number for %s\n",
1818 /* mark this port as used */
1819 port_mask
&= ~(1 << port
);
1821 /* print out the device information */
1823 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
1827 fd
= dev_open(ent
->d_name
, O_RDONLY
);
1829 printf(" Port%d : - disk info unavailable -\n", port
);
1831 fd2devname(fd
, buf
);
1832 printf(" Port%d : %s", port
, buf
);
1833 if (imsm_read_serial(fd
, NULL
, (__u8
*) buf
) == 0)
1834 printf(" (%.*s)\n", MAX_RAID_SERIAL_LEN
, buf
);
1849 for (i
= 0; i
< port_count
; i
++)
1850 if (port_mask
& (1 << i
))
1851 printf(" Port%d : - no device attached -\n", i
);
1857 static int print_vmd_attached_devs(struct sys_dev
*hba
)
1865 if (hba
->type
!= SYS_DEV_VMD
)
1868 /* scroll through /sys/dev/block looking for devices attached to
1871 dir
= opendir("/sys/bus/pci/drivers/nvme");
1875 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
1878 /* is 'ent' a device? check that the 'subsystem' link exists and
1879 * that its target matches 'bus'
1881 sprintf(path
, "/sys/bus/pci/drivers/nvme/%s/subsystem",
1883 n
= readlink(path
, link
, sizeof(link
));
1884 if (n
< 0 || n
>= (int)sizeof(link
))
1887 c
= strrchr(link
, '/');
1890 if (strncmp("pci", c
+1, strlen("pci")) != 0)
1893 sprintf(path
, "/sys/bus/pci/drivers/nvme/%s", ent
->d_name
);
1894 /* if not a intel NVMe - skip it*/
1895 if (devpath_to_vendor(path
) != 0x8086)
1898 rp
= realpath(path
, NULL
);
1902 if (path_attached_to_hba(rp
, hba
->path
)) {
1903 printf(" NVMe under VMD : %s\n", rp
);
1912 static void print_found_intel_controllers(struct sys_dev
*elem
)
1914 for (; elem
; elem
= elem
->next
) {
1915 pr_err("found Intel(R) ");
1916 if (elem
->type
== SYS_DEV_SATA
)
1917 fprintf(stderr
, "SATA ");
1918 else if (elem
->type
== SYS_DEV_SAS
)
1919 fprintf(stderr
, "SAS ");
1920 else if (elem
->type
== SYS_DEV_NVME
)
1921 fprintf(stderr
, "NVMe ");
1923 if (elem
->type
== SYS_DEV_VMD
)
1924 fprintf(stderr
, "VMD domain");
1926 fprintf(stderr
, "RAID controller");
1929 fprintf(stderr
, " at %s", elem
->pci_id
);
1930 fprintf(stderr
, ".\n");
1935 static int ahci_get_port_count(const char *hba_path
, int *port_count
)
1942 if ((dir
= opendir(hba_path
)) == NULL
)
1945 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
1948 if ((sscanf(ent
->d_name
, "ata%d", &host
) != 1) &&
1949 ((sscanf(ent
->d_name
, "host%d", &host
) != 1)))
1951 if (*port_count
== 0)
1953 else if (host
< host_base
)
1956 if (host
+ 1 > *port_count
+ host_base
)
1957 *port_count
= host
+ 1 - host_base
;
1963 static void print_imsm_capability(const struct imsm_orom
*orom
)
1965 printf(" Platform : Intel(R) ");
1966 if (orom
->capabilities
== 0 && orom
->driver_features
== 0)
1967 printf("Matrix Storage Manager\n");
1969 printf("Rapid Storage Technology%s\n",
1970 imsm_orom_is_enterprise(orom
) ? " enterprise" : "");
1971 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
)
1972 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
,
1973 orom
->minor_ver
, orom
->hotfix_ver
, orom
->build
);
1974 printf(" RAID Levels :%s%s%s%s%s\n",
1975 imsm_orom_has_raid0(orom
) ? " raid0" : "",
1976 imsm_orom_has_raid1(orom
) ? " raid1" : "",
1977 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
1978 imsm_orom_has_raid10(orom
) ? " raid10" : "",
1979 imsm_orom_has_raid5(orom
) ? " raid5" : "");
1980 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1981 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
1982 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
1983 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
1984 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
1985 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
1986 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
1987 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
1988 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
1989 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
1990 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
1991 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
1992 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
1993 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
1994 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
1995 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
1996 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
1997 printf(" 2TB volumes :%s supported\n",
1998 (orom
->attr
& IMSM_OROM_ATTR_2TB
)?"":" not");
1999 printf(" 2TB disks :%s supported\n",
2000 (orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
)?"":" not");
2001 printf(" Max Disks : %d\n", orom
->tds
);
2002 printf(" Max Volumes : %d per array, %d per %s\n",
2003 orom
->vpa
, orom
->vphba
,
2004 imsm_orom_is_nvme(orom
) ? "platform" : "controller");
2008 static void print_imsm_capability_export(const struct imsm_orom
*orom
)
2010 printf("MD_FIRMWARE_TYPE=imsm\n");
2011 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
)
2012 printf("IMSM_VERSION=%d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
2013 orom
->hotfix_ver
, orom
->build
);
2014 printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n",
2015 imsm_orom_has_raid0(orom
) ? "raid0 " : "",
2016 imsm_orom_has_raid1(orom
) ? "raid1 " : "",
2017 imsm_orom_has_raid1e(orom
) ? "raid1e " : "",
2018 imsm_orom_has_raid5(orom
) ? "raid10 " : "",
2019 imsm_orom_has_raid10(orom
) ? "raid5 " : "");
2020 printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2021 imsm_orom_has_chunk(orom
, 2) ? "2k " : "",
2022 imsm_orom_has_chunk(orom
, 4) ? "4k " : "",
2023 imsm_orom_has_chunk(orom
, 8) ? "8k " : "",
2024 imsm_orom_has_chunk(orom
, 16) ? "16k " : "",
2025 imsm_orom_has_chunk(orom
, 32) ? "32k " : "",
2026 imsm_orom_has_chunk(orom
, 64) ? "64k " : "",
2027 imsm_orom_has_chunk(orom
, 128) ? "128k " : "",
2028 imsm_orom_has_chunk(orom
, 256) ? "256k " : "",
2029 imsm_orom_has_chunk(orom
, 512) ? "512k " : "",
2030 imsm_orom_has_chunk(orom
, 1024*1) ? "1M " : "",
2031 imsm_orom_has_chunk(orom
, 1024*2) ? "2M " : "",
2032 imsm_orom_has_chunk(orom
, 1024*4) ? "4M " : "",
2033 imsm_orom_has_chunk(orom
, 1024*8) ? "8M " : "",
2034 imsm_orom_has_chunk(orom
, 1024*16) ? "16M " : "",
2035 imsm_orom_has_chunk(orom
, 1024*32) ? "32M " : "",
2036 imsm_orom_has_chunk(orom
, 1024*64) ? "64M " : "");
2037 printf("IMSM_2TB_VOLUMES=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB
) ? "yes" : "no");
2038 printf("IMSM_2TB_DISKS=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) ? "yes" : "no");
2039 printf("IMSM_MAX_DISKS=%d\n",orom
->tds
);
2040 printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom
->vpa
);
2041 printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom
->vphba
);
2044 static int detail_platform_imsm(int verbose
, int enumerate_only
, char *controller_path
)
2046 /* There are two components to imsm platform support, the ahci SATA
2047 * controller and the option-rom. To find the SATA controller we
2048 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
2049 * controller with the Intel vendor id is present. This approach
2050 * allows mdadm to leverage the kernel's ahci detection logic, with the
2051 * caveat that if ahci.ko is not loaded mdadm will not be able to
2052 * detect platform raid capabilities. The option-rom resides in a
2053 * platform "Adapter ROM". We scan for its signature to retrieve the
2054 * platform capabilities. If raid support is disabled in the BIOS the
2055 * option-rom capability structure will not be available.
2057 struct sys_dev
*list
, *hba
;
2062 if (enumerate_only
) {
2063 if (check_env("IMSM_NO_PLATFORM"))
2065 list
= find_intel_devices();
2068 for (hba
= list
; hba
; hba
= hba
->next
) {
2069 if (find_imsm_capability(hba
)) {
2079 list
= find_intel_devices();
2082 pr_err("no active Intel(R) RAID controller found.\n");
2084 } else if (verbose
> 0)
2085 print_found_intel_controllers(list
);
2087 for (hba
= list
; hba
; hba
= hba
->next
) {
2088 if (controller_path
&& (compare_paths(hba
->path
, controller_path
) != 0))
2090 if (!find_imsm_capability(hba
)) {
2092 pr_err("imsm capabilities not found for controller: %s (type %s)\n",
2093 hba
->type
== SYS_DEV_VMD
? vmd_domain_to_controller(hba
, buf
) : hba
->path
,
2094 get_sys_dev_type(hba
->type
));
2100 if (controller_path
&& result
== 1) {
2101 pr_err("no active Intel(R) RAID controller found under %s\n",
2106 const struct orom_entry
*entry
;
2108 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2109 if (entry
->type
== SYS_DEV_VMD
) {
2110 print_imsm_capability(&entry
->orom
);
2111 for (hba
= list
; hba
; hba
= hba
->next
) {
2112 if (hba
->type
== SYS_DEV_VMD
) {
2114 printf(" I/O Controller : %s (%s)\n",
2115 vmd_domain_to_controller(hba
, buf
), get_sys_dev_type(hba
->type
));
2116 if (print_vmd_attached_devs(hba
)) {
2118 pr_err("failed to get devices attached to VMD domain.\n");
2127 print_imsm_capability(&entry
->orom
);
2128 if (entry
->type
== SYS_DEV_NVME
) {
2129 for (hba
= list
; hba
; hba
= hba
->next
) {
2130 if (hba
->type
== SYS_DEV_NVME
)
2131 printf(" NVMe Device : %s\n", hba
->path
);
2137 struct devid_list
*devid
;
2138 for (devid
= entry
->devid_list
; devid
; devid
= devid
->next
) {
2139 hba
= device_by_id(devid
->devid
);
2143 printf(" I/O Controller : %s (%s)\n",
2144 hba
->path
, get_sys_dev_type(hba
->type
));
2145 if (hba
->type
== SYS_DEV_SATA
) {
2146 host_base
= ahci_get_port_count(hba
->path
, &port_count
);
2147 if (ahci_enumerate_ports(hba
->path
, port_count
, host_base
, verbose
)) {
2149 pr_err("failed to enumerate ports on SATA controller at %s.\n", hba
->pci_id
);
2160 static int export_detail_platform_imsm(int verbose
, char *controller_path
)
2162 struct sys_dev
*list
, *hba
;
2165 list
= find_intel_devices();
2168 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n");
2173 for (hba
= list
; hba
; hba
= hba
->next
) {
2174 if (controller_path
&& (compare_paths(hba
->path
,controller_path
) != 0))
2176 if (!find_imsm_capability(hba
) && verbose
> 0) {
2178 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n",
2179 hba
->type
== SYS_DEV_VMD
? vmd_domain_to_controller(hba
, buf
) : hba
->path
);
2185 const struct orom_entry
*entry
;
2187 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2188 if (entry
->type
== SYS_DEV_VMD
) {
2189 for (hba
= list
; hba
; hba
= hba
->next
)
2190 print_imsm_capability_export(&entry
->orom
);
2193 print_imsm_capability_export(&entry
->orom
);
2201 static int match_home_imsm(struct supertype
*st
, char *homehost
)
2203 /* the imsm metadata format does not specify any host
2204 * identification information. We return -1 since we can never
2205 * confirm nor deny whether a given array is "meant" for this
2206 * host. We rely on compare_super and the 'family_num' fields to
2207 * exclude member disks that do not belong, and we rely on
2208 * mdadm.conf to specify the arrays that should be assembled.
2209 * Auto-assembly may still pick up "foreign" arrays.
2215 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
2217 /* The uuid returned here is used for:
2218 * uuid to put into bitmap file (Create, Grow)
2219 * uuid for backup header when saving critical section (Grow)
2220 * comparing uuids when re-adding a device into an array
2221 * In these cases the uuid required is that of the data-array,
2222 * not the device-set.
2223 * uuid to recognise same set when adding a missing device back
2224 * to an array. This is a uuid for the device-set.
2226 * For each of these we can make do with a truncated
2227 * or hashed uuid rather than the original, as long as
2229 * In each case the uuid required is that of the data-array,
2230 * not the device-set.
2232 /* imsm does not track uuid's so we synthesis one using sha1 on
2233 * - The signature (Which is constant for all imsm array, but no matter)
2234 * - the orig_family_num of the container
2235 * - the index number of the volume
2236 * - the 'serial' number of the volume.
2237 * Hopefully these are all constant.
2239 struct intel_super
*super
= st
->sb
;
2242 struct sha1_ctx ctx
;
2243 struct imsm_dev
*dev
= NULL
;
2246 /* some mdadm versions failed to set ->orig_family_num, in which
2247 * case fall back to ->family_num. orig_family_num will be
2248 * fixed up with the first metadata update.
2250 family_num
= super
->anchor
->orig_family_num
;
2251 if (family_num
== 0)
2252 family_num
= super
->anchor
->family_num
;
2253 sha1_init_ctx(&ctx
);
2254 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
2255 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
2256 if (super
->current_vol
>= 0)
2257 dev
= get_imsm_dev(super
, super
->current_vol
);
2259 __u32 vol
= super
->current_vol
;
2260 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
2261 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
2263 sha1_finish_ctx(&ctx
, buf
);
2264 memcpy(uuid
, buf
, 4*4);
2269 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
2271 __u8
*v
= get_imsm_version(mpb
);
2272 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
2273 char major
[] = { 0, 0, 0 };
2274 char minor
[] = { 0 ,0, 0 };
2275 char patch
[] = { 0, 0, 0 };
2276 char *ver_parse
[] = { major
, minor
, patch
};
2280 while (*v
!= '\0' && v
< end
) {
2281 if (*v
!= '.' && j
< 2)
2282 ver_parse
[i
][j
++] = *v
;
2290 *m
= strtol(minor
, NULL
, 0);
2291 *p
= strtol(patch
, NULL
, 0);
2295 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
2297 /* migr_strip_size when repairing or initializing parity */
2298 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2299 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2301 switch (get_imsm_raid_level(map
)) {
2306 return 128*1024 >> 9;
2310 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
2312 /* migr_strip_size when rebuilding a degraded disk, no idea why
2313 * this is different than migr_strip_size_resync(), but it's good
2316 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2317 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2319 switch (get_imsm_raid_level(map
)) {
2322 if (map
->num_members
% map
->num_domains
== 0)
2323 return 128*1024 >> 9;
2327 return max((__u32
) 64*1024 >> 9, chunk
);
2329 return 128*1024 >> 9;
2333 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
2335 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2336 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2337 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
2338 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
2340 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
2343 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
2345 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2346 int level
= get_imsm_raid_level(lo
);
2348 if (level
== 1 || level
== 10) {
2349 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2351 return hi
->num_domains
;
2353 return num_stripes_per_unit_resync(dev
);
2356 static __u8
imsm_num_data_members(struct imsm_dev
*dev
, int second_map
)
2358 /* named 'imsm_' because raid0, raid1 and raid10
2359 * counter-intuitively have the same number of data disks
2361 struct imsm_map
*map
= get_imsm_map(dev
, second_map
);
2363 switch (get_imsm_raid_level(map
)) {
2365 return map
->num_members
;
2369 return map
->num_members
/2;
2371 return map
->num_members
- 1;
2373 dprintf("unsupported raid level\n");
2378 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
2380 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2381 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2383 switch(get_imsm_raid_level(map
)) {
2386 return chunk
* map
->num_domains
;
2388 return chunk
* map
->num_members
;
2394 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
2396 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2397 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2398 __u32 strip
= block
/ chunk
;
2400 switch (get_imsm_raid_level(map
)) {
2403 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
2404 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
2406 return vol_stripe
* chunk
+ block
% chunk
;
2408 __u32 stripe
= strip
/ (map
->num_members
- 1);
2410 return stripe
* chunk
+ block
% chunk
;
2417 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
2418 struct imsm_dev
*dev
)
2420 /* calculate the conversion factor between per member 'blocks'
2421 * (md/{resync,rebuild}_start) and imsm migration units, return
2422 * 0 for the 'not migrating' and 'unsupported migration' cases
2424 if (!dev
->vol
.migr_state
)
2427 switch (migr_type(dev
)) {
2428 case MIGR_GEN_MIGR
: {
2429 struct migr_record
*migr_rec
= super
->migr_rec
;
2430 return __le32_to_cpu(migr_rec
->blocks_per_unit
);
2435 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2436 __u32 stripes_per_unit
;
2437 __u32 blocks_per_unit
;
2446 /* yes, this is really the translation of migr_units to
2447 * per-member blocks in the 'resync' case
2449 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
2450 migr_chunk
= migr_strip_blocks_resync(dev
);
2451 disks
= imsm_num_data_members(dev
, MAP_0
);
2452 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
2453 stripe
= __le16_to_cpu(map
->blocks_per_strip
) * disks
;
2454 segment
= blocks_per_unit
/ stripe
;
2455 block_rel
= blocks_per_unit
- segment
* stripe
;
2456 parity_depth
= parity_segment_depth(dev
);
2457 block_map
= map_migr_block(dev
, block_rel
);
2458 return block_map
+ parity_depth
* segment
;
2460 case MIGR_REBUILD
: {
2461 __u32 stripes_per_unit
;
2464 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
2465 migr_chunk
= migr_strip_blocks_rebuild(dev
);
2466 return migr_chunk
* stripes_per_unit
;
2468 case MIGR_STATE_CHANGE
:
2474 static int imsm_level_to_layout(int level
)
2482 return ALGORITHM_LEFT_ASYMMETRIC
;
2489 /*******************************************************************************
2490 * Function: read_imsm_migr_rec
2491 * Description: Function reads imsm migration record from last sector of disk
2493 * fd : disk descriptor
2494 * super : metadata info
2498 ******************************************************************************/
2499 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
)
2502 unsigned long long dsize
;
2504 get_dev_size(fd
, NULL
, &dsize
);
2505 if (lseek64(fd
, dsize
- MIGR_REC_POSITION
, SEEK_SET
) < 0) {
2506 pr_err("Cannot seek to anchor block: %s\n",
2510 if (read(fd
, super
->migr_rec_buf
, MIGR_REC_BUF_SIZE
) !=
2511 MIGR_REC_BUF_SIZE
) {
2512 pr_err("Cannot read migr record block: %s\n",
2522 static struct imsm_dev
*imsm_get_device_during_migration(
2523 struct intel_super
*super
)
2526 struct intel_dev
*dv
;
2528 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
2529 if (is_gen_migration(dv
->dev
))
2535 /*******************************************************************************
2536 * Function: load_imsm_migr_rec
2537 * Description: Function reads imsm migration record (it is stored at the last
2540 * super : imsm internal array info
2541 * info : general array info
2545 * -2 : no migration in progress
2546 ******************************************************************************/
2547 static int load_imsm_migr_rec(struct intel_super
*super
, struct mdinfo
*info
)
2554 struct imsm_dev
*dev
;
2555 struct imsm_map
*map
;
2558 /* find map under migration */
2559 dev
= imsm_get_device_during_migration(super
);
2560 /* nothing to load,no migration in progress?
2566 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
2567 /* read only from one of the first two slots */
2568 if ((sd
->disk
.raid_disk
< 0) ||
2569 (sd
->disk
.raid_disk
> 1))
2572 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
2573 fd
= dev_open(nm
, O_RDONLY
);
2579 map
= get_imsm_map(dev
, MAP_0
);
2580 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
2581 /* skip spare and failed disks
2585 /* read only from one of the first two slots */
2587 slot
= get_imsm_disk_slot(map
, dl
->index
);
2588 if (map
== NULL
|| slot
> 1 || slot
< 0)
2590 sprintf(nm
, "%d:%d", dl
->major
, dl
->minor
);
2591 fd
= dev_open(nm
, O_RDONLY
);
2598 retval
= read_imsm_migr_rec(fd
, super
);
2607 /*******************************************************************************
2608 * function: imsm_create_metadata_checkpoint_update
2609 * Description: It creates update for checkpoint change.
2611 * super : imsm internal array info
2612 * u : pointer to prepared update
2615 * If length is equal to 0, input pointer u contains no update
2616 ******************************************************************************/
2617 static int imsm_create_metadata_checkpoint_update(
2618 struct intel_super
*super
,
2619 struct imsm_update_general_migration_checkpoint
**u
)
2622 int update_memory_size
= 0;
2624 dprintf("(enter)\n");
2630 /* size of all update data without anchor */
2631 update_memory_size
=
2632 sizeof(struct imsm_update_general_migration_checkpoint
);
2634 *u
= xcalloc(1, update_memory_size
);
2636 dprintf("error: cannot get memory\n");
2639 (*u
)->type
= update_general_migration_checkpoint
;
2640 (*u
)->curr_migr_unit
= __le32_to_cpu(super
->migr_rec
->curr_migr_unit
);
2641 dprintf("prepared for %u\n", (*u
)->curr_migr_unit
);
2643 return update_memory_size
;
2646 static void imsm_update_metadata_locally(struct supertype
*st
,
2647 void *buf
, int len
);
2649 /*******************************************************************************
2650 * Function: write_imsm_migr_rec
2651 * Description: Function writes imsm migration record
2652 * (at the last sector of disk)
2654 * super : imsm internal array info
2658 ******************************************************************************/
2659 static int write_imsm_migr_rec(struct supertype
*st
)
2661 struct intel_super
*super
= st
->sb
;
2662 unsigned long long dsize
;
2668 struct imsm_update_general_migration_checkpoint
*u
;
2669 struct imsm_dev
*dev
;
2670 struct imsm_map
*map
;
2672 /* find map under migration */
2673 dev
= imsm_get_device_during_migration(super
);
2674 /* if no migration, write buffer anyway to clear migr_record
2675 * on disk based on first available device
2678 dev
= get_imsm_dev(super
, super
->current_vol
< 0 ? 0 :
2679 super
->current_vol
);
2681 map
= get_imsm_map(dev
, MAP_0
);
2683 for (sd
= super
->disks
; sd
; sd
= sd
->next
) {
2686 /* skip failed and spare devices */
2689 /* write to 2 first slots only */
2691 slot
= get_imsm_disk_slot(map
, sd
->index
);
2692 if (map
== NULL
|| slot
> 1 || slot
< 0)
2695 sprintf(nm
, "%d:%d", sd
->major
, sd
->minor
);
2696 fd
= dev_open(nm
, O_RDWR
);
2699 get_dev_size(fd
, NULL
, &dsize
);
2700 if (lseek64(fd
, dsize
- MIGR_REC_POSITION
, SEEK_SET
) < 0) {
2701 pr_err("Cannot seek to anchor block: %s\n",
2705 if (write(fd
, super
->migr_rec_buf
, MIGR_REC_BUF_SIZE
) !=
2706 MIGR_REC_BUF_SIZE
) {
2707 pr_err("Cannot write migr record block: %s\n",
2714 /* update checkpoint information in metadata */
2715 len
= imsm_create_metadata_checkpoint_update(super
, &u
);
2718 dprintf("imsm: Cannot prepare update\n");
2721 /* update metadata locally */
2722 imsm_update_metadata_locally(st
, u
, len
);
2723 /* and possibly remotely */
2724 if (st
->update_tail
) {
2725 append_metadata_update(st
, u
, len
);
2726 /* during reshape we do all work inside metadata handler
2727 * manage_reshape(), so metadata update has to be triggered
2730 flush_metadata_updates(st
);
2731 st
->update_tail
= &st
->updates
;
2741 #endif /* MDASSEMBLE */
2743 /* spare/missing disks activations are not allowe when
2744 * array/container performs reshape operation, because
2745 * all arrays in container works on the same disks set
2747 int imsm_reshape_blocks_arrays_changes(struct intel_super
*super
)
2750 struct intel_dev
*i_dev
;
2751 struct imsm_dev
*dev
;
2753 /* check whole container
2755 for (i_dev
= super
->devlist
; i_dev
; i_dev
= i_dev
->next
) {
2757 if (is_gen_migration(dev
)) {
2758 /* No repair during any migration in container
2766 static unsigned long long imsm_component_size_aligment_check(int level
,
2768 unsigned int sector_size
,
2769 unsigned long long component_size
)
2771 unsigned int component_size_alligment
;
2773 /* check component size aligment
2775 component_size_alligment
= component_size
% (chunk_size
/sector_size
);
2777 dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alligment = %u\n",
2778 level
, chunk_size
, component_size
,
2779 component_size_alligment
);
2781 if (component_size_alligment
&& (level
!= 1) && (level
!= UnSet
)) {
2782 dprintf("imsm: reported component size alligned from %llu ",
2784 component_size
-= component_size_alligment
;
2785 dprintf_cont("to %llu (%i).\n",
2786 component_size
, component_size_alligment
);
2789 return component_size
;
2792 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
2794 struct intel_super
*super
= st
->sb
;
2795 struct migr_record
*migr_rec
= super
->migr_rec
;
2796 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
2797 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2798 struct imsm_map
*prev_map
= get_imsm_map(dev
, MAP_1
);
2799 struct imsm_map
*map_to_analyse
= map
;
2801 int map_disks
= info
->array
.raid_disks
;
2803 memset(info
, 0, sizeof(*info
));
2805 map_to_analyse
= prev_map
;
2807 dl
= super
->current_disk
;
2809 info
->container_member
= super
->current_vol
;
2810 info
->array
.raid_disks
= map
->num_members
;
2811 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
2812 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
2813 info
->array
.md_minor
= -1;
2814 info
->array
.ctime
= 0;
2815 info
->array
.utime
= 0;
2816 info
->array
.chunk_size
=
2817 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
2818 info
->array
.state
= !dev
->vol
.dirty
;
2819 info
->custom_array_size
= __le32_to_cpu(dev
->size_high
);
2820 info
->custom_array_size
<<= 32;
2821 info
->custom_array_size
|= __le32_to_cpu(dev
->size_low
);
2822 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
2824 if (is_gen_migration(dev
)) {
2825 info
->reshape_active
= 1;
2826 info
->new_level
= get_imsm_raid_level(map
);
2827 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
2828 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
2829 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
2830 if (info
->delta_disks
) {
2831 /* this needs to be applied to every array
2834 info
->reshape_active
= CONTAINER_RESHAPE
;
2836 /* We shape information that we give to md might have to be
2837 * modify to cope with md's requirement for reshaping arrays.
2838 * For example, when reshaping a RAID0, md requires it to be
2839 * presented as a degraded RAID4.
2840 * Also if a RAID0 is migrating to a RAID5 we need to specify
2841 * the array as already being RAID5, but the 'before' layout
2842 * is a RAID4-like layout.
2844 switch (info
->array
.level
) {
2846 switch(info
->new_level
) {
2848 /* conversion is happening as RAID4 */
2849 info
->array
.level
= 4;
2850 info
->array
.raid_disks
+= 1;
2853 /* conversion is happening as RAID5 */
2854 info
->array
.level
= 5;
2855 info
->array
.layout
= ALGORITHM_PARITY_N
;
2856 info
->delta_disks
-= 1;
2859 /* FIXME error message */
2860 info
->array
.level
= UnSet
;
2866 info
->new_level
= UnSet
;
2867 info
->new_layout
= UnSet
;
2868 info
->new_chunk
= info
->array
.chunk_size
;
2869 info
->delta_disks
= 0;
2873 info
->disk
.major
= dl
->major
;
2874 info
->disk
.minor
= dl
->minor
;
2875 info
->disk
.number
= dl
->index
;
2876 info
->disk
.raid_disk
= get_imsm_disk_slot(map_to_analyse
,
2880 info
->data_offset
= pba_of_lba0(map_to_analyse
);
2881 info
->component_size
= blocks_per_member(map_to_analyse
);
2883 info
->component_size
= imsm_component_size_aligment_check(
2885 info
->array
.chunk_size
,
2887 info
->component_size
);
2889 memset(info
->uuid
, 0, sizeof(info
->uuid
));
2890 info
->recovery_start
= MaxSector
;
2892 info
->reshape_progress
= 0;
2893 info
->resync_start
= MaxSector
;
2894 if ((map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
2896 imsm_reshape_blocks_arrays_changes(super
) == 0) {
2897 info
->resync_start
= 0;
2899 if (dev
->vol
.migr_state
) {
2900 switch (migr_type(dev
)) {
2903 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2905 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
2907 info
->resync_start
= blocks_per_unit
* units
;
2910 case MIGR_GEN_MIGR
: {
2911 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2913 __u64 units
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
2914 unsigned long long array_blocks
;
2917 if (__le32_to_cpu(migr_rec
->ascending_migr
) &&
2919 (__le32_to_cpu(migr_rec
->num_migr_units
)-1)) &&
2920 (super
->migr_rec
->rec_status
==
2921 __cpu_to_le32(UNIT_SRC_IN_CP_AREA
)))
2924 info
->reshape_progress
= blocks_per_unit
* units
;
2926 dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n",
2927 (unsigned long long)units
,
2928 (unsigned long long)blocks_per_unit
,
2929 info
->reshape_progress
);
2931 used_disks
= imsm_num_data_members(dev
, MAP_1
);
2932 if (used_disks
> 0) {
2933 array_blocks
= blocks_per_member(map
) *
2935 /* round array size down to closest MB
2937 info
->custom_array_size
= (array_blocks
2938 >> SECT_PER_MB_SHIFT
)
2939 << SECT_PER_MB_SHIFT
;
2943 /* we could emulate the checkpointing of
2944 * 'sync_action=check' migrations, but for now
2945 * we just immediately complete them
2948 /* this is handled by container_content_imsm() */
2949 case MIGR_STATE_CHANGE
:
2950 /* FIXME handle other migrations */
2952 /* we are not dirty, so... */
2953 info
->resync_start
= MaxSector
;
2957 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
2958 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
2960 info
->array
.major_version
= -1;
2961 info
->array
.minor_version
= -2;
2962 sprintf(info
->text_version
, "/%s/%d", st
->container_devnm
, info
->container_member
);
2963 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
2964 uuid_from_super_imsm(st
, info
->uuid
);
2968 for (i
=0; i
<map_disks
; i
++) {
2970 if (i
< info
->array
.raid_disks
) {
2971 struct imsm_disk
*dsk
;
2972 j
= get_imsm_disk_idx(dev
, i
, MAP_X
);
2973 dsk
= get_imsm_disk(super
, j
);
2974 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
2981 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
2982 int failed
, int look_in_map
);
2984 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
2988 static void manage_second_map(struct intel_super
*super
, struct imsm_dev
*dev
)
2990 if (is_gen_migration(dev
)) {
2993 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
2995 failed
= imsm_count_failed(super
, dev
, MAP_1
);
2996 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_1
);
2997 if (map2
->map_state
!= map_state
) {
2998 map2
->map_state
= map_state
;
2999 super
->updates_pending
++;
3005 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
3009 for (d
= super
->missing
; d
; d
= d
->next
)
3010 if (d
->index
== index
)
3015 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
3017 struct intel_super
*super
= st
->sb
;
3018 struct imsm_disk
*disk
;
3019 int map_disks
= info
->array
.raid_disks
;
3020 int max_enough
= -1;
3022 struct imsm_super
*mpb
;
3024 if (super
->current_vol
>= 0) {
3025 getinfo_super_imsm_volume(st
, info
, map
);
3028 memset(info
, 0, sizeof(*info
));
3030 /* Set raid_disks to zero so that Assemble will always pull in valid
3033 info
->array
.raid_disks
= 0;
3034 info
->array
.level
= LEVEL_CONTAINER
;
3035 info
->array
.layout
= 0;
3036 info
->array
.md_minor
= -1;
3037 info
->array
.ctime
= 0; /* N/A for imsm */
3038 info
->array
.utime
= 0;
3039 info
->array
.chunk_size
= 0;
3041 info
->disk
.major
= 0;
3042 info
->disk
.minor
= 0;
3043 info
->disk
.raid_disk
= -1;
3044 info
->reshape_active
= 0;
3045 info
->array
.major_version
= -1;
3046 info
->array
.minor_version
= -2;
3047 strcpy(info
->text_version
, "imsm");
3048 info
->safe_mode_delay
= 0;
3049 info
->disk
.number
= -1;
3050 info
->disk
.state
= 0;
3052 info
->recovery_start
= MaxSector
;
3053 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
3055 /* do we have the all the insync disks that we expect? */
3056 mpb
= super
->anchor
;
3058 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3059 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3060 int failed
, enough
, j
, missing
= 0;
3061 struct imsm_map
*map
;
3064 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3065 state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3066 map
= get_imsm_map(dev
, MAP_0
);
3068 /* any newly missing disks?
3069 * (catches single-degraded vs double-degraded)
3071 for (j
= 0; j
< map
->num_members
; j
++) {
3072 __u32 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
3073 __u32 idx
= ord_to_idx(ord
);
3075 if (!(ord
& IMSM_ORD_REBUILD
) &&
3076 get_imsm_missing(super
, idx
)) {
3082 if (state
== IMSM_T_STATE_FAILED
)
3084 else if (state
== IMSM_T_STATE_DEGRADED
&&
3085 (state
!= map
->map_state
|| missing
))
3087 else /* we're normal, or already degraded */
3089 if (is_gen_migration(dev
) && missing
) {
3090 /* during general migration we need all disks
3091 * that process is running on.
3092 * No new missing disk is allowed.
3096 /* no more checks necessary
3100 /* in the missing/failed disk case check to see
3101 * if at least one array is runnable
3103 max_enough
= max(max_enough
, enough
);
3105 dprintf("enough: %d\n", max_enough
);
3106 info
->container_enough
= max_enough
;
3109 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
3111 disk
= &super
->disks
->disk
;
3112 info
->data_offset
= total_blocks(&super
->disks
->disk
) - reserved
;
3113 info
->component_size
= reserved
;
3114 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
3115 /* we don't change info->disk.raid_disk here because
3116 * this state will be finalized in mdmon after we have
3117 * found the 'most fresh' version of the metadata
3119 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3120 info
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
3123 /* only call uuid_from_super_imsm when this disk is part of a populated container,
3124 * ->compare_super may have updated the 'num_raid_devs' field for spares
3126 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
3127 uuid_from_super_imsm(st
, info
->uuid
);
3129 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
3131 /* I don't know how to compute 'map' on imsm, so use safe default */
3134 for (i
= 0; i
< map_disks
; i
++)
3140 /* allocates memory and fills disk in mdinfo structure
3141 * for each disk in array */
3142 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
3144 struct mdinfo
*mddev
;
3145 struct intel_super
*super
= st
->sb
;
3146 struct imsm_disk
*disk
;
3149 if (!super
|| !super
->disks
)
3152 mddev
= xcalloc(1, sizeof(*mddev
));
3156 tmp
= xcalloc(1, sizeof(*tmp
));
3158 tmp
->next
= mddev
->devs
;
3160 tmp
->disk
.number
= count
++;
3161 tmp
->disk
.major
= dl
->major
;
3162 tmp
->disk
.minor
= dl
->minor
;
3163 tmp
->disk
.state
= is_configured(disk
) ?
3164 (1 << MD_DISK_ACTIVE
) : 0;
3165 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3166 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
3167 tmp
->disk
.raid_disk
= -1;
3173 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
3174 char *update
, char *devname
, int verbose
,
3175 int uuid_set
, char *homehost
)
3177 /* For 'assemble' and 'force' we need to return non-zero if any
3178 * change was made. For others, the return value is ignored.
3179 * Update options are:
3180 * force-one : This device looks a bit old but needs to be included,
3181 * update age info appropriately.
3182 * assemble: clear any 'faulty' flag to allow this device to
3184 * force-array: Array is degraded but being forced, mark it clean
3185 * if that will be needed to assemble it.
3187 * newdev: not used ????
3188 * grow: Array has gained a new device - this is currently for
3190 * resync: mark as dirty so a resync will happen.
3191 * name: update the name - preserving the homehost
3192 * uuid: Change the uuid of the array to match watch is given
3194 * Following are not relevant for this imsm:
3195 * sparc2.2 : update from old dodgey metadata
3196 * super-minor: change the preferred_minor number
3197 * summaries: update redundant counters.
3198 * homehost: update the recorded homehost
3199 * _reshape_progress: record new reshape_progress position.
3202 struct intel_super
*super
= st
->sb
;
3203 struct imsm_super
*mpb
;
3205 /* we can only update container info */
3206 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
3209 mpb
= super
->anchor
;
3211 if (strcmp(update
, "uuid") == 0) {
3212 /* We take this to mean that the family_num should be updated.
3213 * However that is much smaller than the uuid so we cannot really
3214 * allow an explicit uuid to be given. And it is hard to reliably
3216 * So if !uuid_set we know the current uuid is random and just used
3217 * the first 'int' and copy it to the other 3 positions.
3218 * Otherwise we require the 4 'int's to be the same as would be the
3219 * case if we are using a random uuid. So an explicit uuid will be
3220 * accepted as long as all for ints are the same... which shouldn't hurt
3223 info
->uuid
[1] = info
->uuid
[2] = info
->uuid
[3] = info
->uuid
[0];
3226 if (info
->uuid
[0] != info
->uuid
[1] ||
3227 info
->uuid
[1] != info
->uuid
[2] ||
3228 info
->uuid
[2] != info
->uuid
[3])
3234 mpb
->orig_family_num
= info
->uuid
[0];
3235 } else if (strcmp(update
, "assemble") == 0)
3240 /* successful update? recompute checksum */
3242 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
3247 static size_t disks_to_mpb_size(int disks
)
3251 size
= sizeof(struct imsm_super
);
3252 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
3253 size
+= 2 * sizeof(struct imsm_dev
);
3254 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
3255 size
+= (4 - 2) * sizeof(struct imsm_map
);
3256 /* 4 possible disk_ord_tbl's */
3257 size
+= 4 * (disks
- 1) * sizeof(__u32
);
3262 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
,
3263 unsigned long long data_offset
)
3265 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
3268 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
3271 static void free_devlist(struct intel_super
*super
)
3273 struct intel_dev
*dv
;
3275 while (super
->devlist
) {
3276 dv
= super
->devlist
->next
;
3277 free(super
->devlist
->dev
);
3278 free(super
->devlist
);
3279 super
->devlist
= dv
;
3283 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
3285 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
3288 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
3292 * 0 same, or first was empty, and second was copied
3293 * 1 second had wrong number
3295 * 3 wrong other info
3297 struct intel_super
*first
= st
->sb
;
3298 struct intel_super
*sec
= tst
->sb
;
3305 /* in platform dependent environment test if the disks
3306 * use the same Intel hba
3307 * If not on Intel hba at all, allow anything.
3309 if (!check_env("IMSM_NO_PLATFORM") && first
->hba
&& sec
->hba
) {
3310 if (first
->hba
->type
!= sec
->hba
->type
) {
3312 "HBAs of devices do not match %s != %s\n",
3313 get_sys_dev_type(first
->hba
->type
),
3314 get_sys_dev_type(sec
->hba
->type
));
3317 if (first
->orom
!= sec
->orom
) {
3319 "HBAs of devices do not match %s != %s\n",
3320 first
->hba
->pci_id
, sec
->hba
->pci_id
);
3325 /* if an anchor does not have num_raid_devs set then it is a free
3328 if (first
->anchor
->num_raid_devs
> 0 &&
3329 sec
->anchor
->num_raid_devs
> 0) {
3330 /* Determine if these disks might ever have been
3331 * related. Further disambiguation can only take place
3332 * in load_super_imsm_all
3334 __u32 first_family
= first
->anchor
->orig_family_num
;
3335 __u32 sec_family
= sec
->anchor
->orig_family_num
;
3337 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
3338 MAX_SIGNATURE_LENGTH
) != 0)
3341 if (first_family
== 0)
3342 first_family
= first
->anchor
->family_num
;
3343 if (sec_family
== 0)
3344 sec_family
= sec
->anchor
->family_num
;
3346 if (first_family
!= sec_family
)
3351 /* if 'first' is a spare promote it to a populated mpb with sec's
3354 if (first
->anchor
->num_raid_devs
== 0 &&
3355 sec
->anchor
->num_raid_devs
> 0) {
3357 struct intel_dev
*dv
;
3358 struct imsm_dev
*dev
;
3360 /* we need to copy raid device info from sec if an allocation
3361 * fails here we don't associate the spare
3363 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
3364 dv
= xmalloc(sizeof(*dv
));
3365 dev
= xmalloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
3368 dv
->next
= first
->devlist
;
3369 first
->devlist
= dv
;
3371 if (i
< sec
->anchor
->num_raid_devs
) {
3372 /* allocation failure */
3373 free_devlist(first
);
3374 pr_err("imsm: failed to associate spare\n");
3377 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
3378 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
3379 first
->anchor
->family_num
= sec
->anchor
->family_num
;
3380 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
3381 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
3382 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
3388 static void fd2devname(int fd
, char *name
)
3392 char dname
[PATH_MAX
];
3397 if (fstat(fd
, &st
) != 0)
3399 sprintf(path
, "/sys/dev/block/%d:%d",
3400 major(st
.st_rdev
), minor(st
.st_rdev
));
3402 rv
= readlink(path
, dname
, sizeof(dname
)-1);
3407 nm
= strrchr(dname
, '/');
3410 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
3414 static int nvme_get_serial(int fd
, void *buf
, size_t buf_len
)
3417 char *name
= fd2kname(fd
);
3422 if (strncmp(name
, "nvme", 4) != 0)
3425 snprintf(path
, sizeof(path
) - 1, "/sys/block/%s/device/serial", name
);
3427 return load_sys(path
, buf
, buf_len
);
3430 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
3432 static int imsm_read_serial(int fd
, char *devname
,
3433 __u8 serial
[MAX_RAID_SERIAL_LEN
])
3442 memset(buf
, 0, sizeof(buf
));
3444 rv
= nvme_get_serial(fd
, buf
, sizeof(buf
));
3447 rv
= scsi_get_serial(fd
, buf
, sizeof(buf
));
3449 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
3450 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3451 fd2devname(fd
, (char *) serial
);
3457 pr_err("Failed to retrieve serial for %s\n",
3462 /* trim all whitespace and non-printable characters and convert
3465 for (i
= 0, dest
= buf
; i
< sizeof(buf
) && buf
[i
]; i
++) {
3468 /* ':' is reserved for use in placeholder serial
3469 * numbers for missing disks
3480 /* truncate leading characters */
3481 if (len
> MAX_RAID_SERIAL_LEN
) {
3482 dest
+= len
- MAX_RAID_SERIAL_LEN
;
3483 len
= MAX_RAID_SERIAL_LEN
;
3486 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3487 memcpy(serial
, dest
, len
);
3492 static int serialcmp(__u8
*s1
, __u8
*s2
)
3494 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
3497 static void serialcpy(__u8
*dest
, __u8
*src
)
3499 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
3502 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
3506 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3507 if (serialcmp(dl
->serial
, serial
) == 0)
3513 static struct imsm_disk
*
3514 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
3518 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3519 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3521 if (serialcmp(disk
->serial
, serial
) == 0) {
3532 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3534 struct imsm_disk
*disk
;
3539 __u8 serial
[MAX_RAID_SERIAL_LEN
];
3541 rv
= imsm_read_serial(fd
, devname
, serial
);
3546 dl
= xcalloc(1, sizeof(*dl
));
3549 dl
->major
= major(stb
.st_rdev
);
3550 dl
->minor
= minor(stb
.st_rdev
);
3551 dl
->next
= super
->disks
;
3552 dl
->fd
= keep_fd
? fd
: -1;
3553 assert(super
->disks
== NULL
);
3555 serialcpy(dl
->serial
, serial
);
3558 fd2devname(fd
, name
);
3560 dl
->devname
= xstrdup(devname
);
3562 dl
->devname
= xstrdup(name
);
3564 /* look up this disk's index in the current anchor */
3565 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
3568 /* only set index on disks that are a member of a
3569 * populated contianer, i.e. one with raid_devs
3571 if (is_failed(&dl
->disk
))
3573 else if (is_spare(&dl
->disk
))
3581 /* When migrating map0 contains the 'destination' state while map1
3582 * contains the current state. When not migrating map0 contains the
3583 * current state. This routine assumes that map[0].map_state is set to
3584 * the current array state before being called.
3586 * Migration is indicated by one of the following states
3587 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
3588 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
3589 * map1state=unitialized)
3590 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
3592 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
3593 * map1state=degraded)
3594 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
3597 static void migrate(struct imsm_dev
*dev
, struct intel_super
*super
,
3598 __u8 to_state
, int migr_type
)
3600 struct imsm_map
*dest
;
3601 struct imsm_map
*src
= get_imsm_map(dev
, MAP_0
);
3603 dev
->vol
.migr_state
= 1;
3604 set_migr_type(dev
, migr_type
);
3605 dev
->vol
.curr_migr_unit
= 0;
3606 dest
= get_imsm_map(dev
, MAP_1
);
3608 /* duplicate and then set the target end state in map[0] */
3609 memcpy(dest
, src
, sizeof_imsm_map(src
));
3610 if (migr_type
== MIGR_REBUILD
|| migr_type
== MIGR_GEN_MIGR
) {
3614 for (i
= 0; i
< src
->num_members
; i
++) {
3615 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
3616 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
3620 if (migr_type
== MIGR_GEN_MIGR
)
3621 /* Clear migration record */
3622 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
3624 src
->map_state
= to_state
;
3627 static void end_migration(struct imsm_dev
*dev
, struct intel_super
*super
,
3630 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3631 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
== 0 ?
3635 /* merge any IMSM_ORD_REBUILD bits that were not successfully
3636 * completed in the last migration.
3638 * FIXME add support for raid-level-migration
3640 if (map_state
!= map
->map_state
&& (is_gen_migration(dev
) == 0) &&
3641 prev
->map_state
!= IMSM_T_STATE_UNINITIALIZED
) {
3642 /* when final map state is other than expected
3643 * merge maps (not for migration)
3647 for (i
= 0; i
< prev
->num_members
; i
++)
3648 for (j
= 0; j
< map
->num_members
; j
++)
3649 /* during online capacity expansion
3650 * disks position can be changed
3651 * if takeover is used
3653 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
3654 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
3655 map
->disk_ord_tbl
[j
] |=
3656 prev
->disk_ord_tbl
[i
];
3659 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3660 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3663 dev
->vol
.migr_state
= 0;
3664 set_migr_type(dev
, 0);
3665 dev
->vol
.curr_migr_unit
= 0;
3666 map
->map_state
= map_state
;
3670 static int parse_raid_devices(struct intel_super
*super
)
3673 struct imsm_dev
*dev_new
;
3674 size_t len
, len_migr
;
3676 size_t space_needed
= 0;
3677 struct imsm_super
*mpb
= super
->anchor
;
3679 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3680 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3681 struct intel_dev
*dv
;
3683 len
= sizeof_imsm_dev(dev_iter
, 0);
3684 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
3686 space_needed
+= len_migr
- len
;
3688 dv
= xmalloc(sizeof(*dv
));
3689 if (max_len
< len_migr
)
3691 if (max_len
> len_migr
)
3692 space_needed
+= max_len
- len_migr
;
3693 dev_new
= xmalloc(max_len
);
3694 imsm_copy_dev(dev_new
, dev_iter
);
3697 dv
->next
= super
->devlist
;
3698 super
->devlist
= dv
;
3701 /* ensure that super->buf is large enough when all raid devices
3704 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
3707 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
,
3708 super
->sector_size
);
3709 if (posix_memalign(&buf
, MAX_SECTOR_SIZE
, len
) != 0)
3712 memcpy(buf
, super
->buf
, super
->len
);
3713 memset(buf
+ super
->len
, 0, len
- super
->len
);
3722 /* retrieve a pointer to the bbm log which starts after all raid devices */
3723 struct bbm_log
*__get_imsm_bbm_log(struct imsm_super
*mpb
)
3727 if (__le32_to_cpu(mpb
->bbm_log_size
)) {
3729 ptr
+= mpb
->mpb_size
- __le32_to_cpu(mpb
->bbm_log_size
);
3735 /*******************************************************************************
3736 * Function: check_mpb_migr_compatibility
3737 * Description: Function checks for unsupported migration features:
3738 * - migration optimization area (pba_of_lba0)
3739 * - descending reshape (ascending_migr)
3741 * super : imsm metadata information
3743 * 0 : migration is compatible
3744 * -1 : migration is not compatible
3745 ******************************************************************************/
3746 int check_mpb_migr_compatibility(struct intel_super
*super
)
3748 struct imsm_map
*map0
, *map1
;
3749 struct migr_record
*migr_rec
= super
->migr_rec
;
3752 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3753 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3756 dev_iter
->vol
.migr_state
== 1 &&
3757 dev_iter
->vol
.migr_type
== MIGR_GEN_MIGR
) {
3758 /* This device is migrating */
3759 map0
= get_imsm_map(dev_iter
, MAP_0
);
3760 map1
= get_imsm_map(dev_iter
, MAP_1
);
3761 if (pba_of_lba0(map0
) != pba_of_lba0(map1
))
3762 /* migration optimization area was used */
3764 if (migr_rec
->ascending_migr
== 0
3765 && migr_rec
->dest_depth_per_unit
> 0)
3766 /* descending reshape not supported yet */
3773 static void __free_imsm(struct intel_super
*super
, int free_disks
);
3775 /* load_imsm_mpb - read matrix metadata
3776 * allocates super->mpb to be freed by free_imsm
3778 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
3780 unsigned long long dsize
;
3781 unsigned long long sectors
;
3782 unsigned int sector_size
= super
->sector_size
;
3784 struct imsm_super
*anchor
;
3787 get_dev_size(fd
, NULL
, &dsize
);
3788 if (dsize
< 2*sector_size
) {
3790 pr_err("%s: device to small for imsm\n",
3795 if (lseek64(fd
, dsize
- (sector_size
* 2), SEEK_SET
) < 0) {
3797 pr_err("Cannot seek to anchor block on %s: %s\n",
3798 devname
, strerror(errno
));
3802 if (posix_memalign((void **)&anchor
, sector_size
, sector_size
) != 0) {
3804 pr_err("Failed to allocate imsm anchor buffer on %s\n", devname
);
3807 if (read(fd
, anchor
, sector_size
) != sector_size
) {
3809 pr_err("Cannot read anchor block on %s: %s\n",
3810 devname
, strerror(errno
));
3815 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
3817 pr_err("no IMSM anchor on %s\n", devname
);
3822 __free_imsm(super
, 0);
3823 /* reload capability and hba */
3825 /* capability and hba must be updated with new super allocation */
3826 find_intel_hba_capability(fd
, super
, devname
);
3827 super
->len
= ROUND_UP(anchor
->mpb_size
, sector_size
);
3828 if (posix_memalign(&super
->buf
, MAX_SECTOR_SIZE
, super
->len
) != 0) {
3830 pr_err("unable to allocate %zu byte mpb buffer\n",
3835 memcpy(super
->buf
, anchor
, sector_size
);
3837 sectors
= mpb_sectors(anchor
, sector_size
) - 1;
3840 if (posix_memalign(&super
->migr_rec_buf
, 512, MIGR_REC_BUF_SIZE
) != 0) {
3841 pr_err("could not allocate migr_rec buffer\n");
3845 super
->clean_migration_record_by_mdmon
= 0;
3848 check_sum
= __gen_imsm_checksum(super
->anchor
);
3849 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3851 pr_err("IMSM checksum %x != %x on %s\n",
3853 __le32_to_cpu(super
->anchor
->check_sum
),
3861 /* read the extended mpb */
3862 if (lseek64(fd
, dsize
- (sector_size
* (2 + sectors
)), SEEK_SET
) < 0) {
3864 pr_err("Cannot seek to extended mpb on %s: %s\n",
3865 devname
, strerror(errno
));
3869 if ((unsigned int)read(fd
, super
->buf
+ sector_size
,
3870 super
->len
- sector_size
) != super
->len
- sector_size
) {
3872 pr_err("Cannot read extended mpb on %s: %s\n",
3873 devname
, strerror(errno
));
3877 check_sum
= __gen_imsm_checksum(super
->anchor
);
3878 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3880 pr_err("IMSM checksum %x != %x on %s\n",
3881 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
3886 /* FIXME the BBM log is disk specific so we cannot use this global
3887 * buffer for all disks. Ok for now since we only look at the global
3888 * bbm_log_size parameter to gate assembly
3890 super
->bbm_log
= __get_imsm_bbm_log(super
->anchor
);
3895 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
);
3897 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
3898 static void clear_hi(struct intel_super
*super
)
3900 struct imsm_super
*mpb
= super
->anchor
;
3902 if (mpb
->attributes
& MPB_ATTRIB_2TB_DISK
)
3904 for (i
= 0; i
< mpb
->num_disks
; ++i
) {
3905 struct imsm_disk
*disk
= &mpb
->disk
[i
];
3906 disk
->total_blocks_hi
= 0;
3908 for (i
= 0; i
< mpb
->num_raid_devs
; ++i
) {
3909 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3912 for (n
= 0; n
< 2; ++n
) {
3913 struct imsm_map
*map
= get_imsm_map(dev
, n
);
3916 map
->pba_of_lba0_hi
= 0;
3917 map
->blocks_per_member_hi
= 0;
3918 map
->num_data_stripes_hi
= 0;
3924 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3928 err
= load_imsm_mpb(fd
, super
, devname
);
3931 if (super
->sector_size
== 4096)
3932 convert_from_4k(super
);
3933 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
3936 err
= parse_raid_devices(super
);
3941 static void __free_imsm_disk(struct dl
*d
)
3953 static void free_imsm_disks(struct intel_super
*super
)
3957 while (super
->disks
) {
3959 super
->disks
= d
->next
;
3960 __free_imsm_disk(d
);
3962 while (super
->disk_mgmt_list
) {
3963 d
= super
->disk_mgmt_list
;
3964 super
->disk_mgmt_list
= d
->next
;
3965 __free_imsm_disk(d
);
3967 while (super
->missing
) {
3969 super
->missing
= d
->next
;
3970 __free_imsm_disk(d
);
3975 /* free all the pieces hanging off of a super pointer */
3976 static void __free_imsm(struct intel_super
*super
, int free_disks
)
3978 struct intel_hba
*elem
, *next
;
3984 /* unlink capability description */
3986 if (super
->migr_rec_buf
) {
3987 free(super
->migr_rec_buf
);
3988 super
->migr_rec_buf
= NULL
;
3991 free_imsm_disks(super
);
3992 free_devlist(super
);
3996 free((void *)elem
->path
);
4004 static void free_imsm(struct intel_super
*super
)
4006 __free_imsm(super
, 1);
4010 static void free_super_imsm(struct supertype
*st
)
4012 struct intel_super
*super
= st
->sb
;
4021 static struct intel_super
*alloc_super(void)
4023 struct intel_super
*super
= xcalloc(1, sizeof(*super
));
4025 super
->current_vol
= -1;
4026 super
->create_offset
= ~((unsigned long long) 0);
4031 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
4033 static int find_intel_hba_capability(int fd
, struct intel_super
*super
, char *devname
)
4035 struct sys_dev
*hba_name
;
4038 if (fd
< 0 || check_env("IMSM_NO_PLATFORM")) {
4043 hba_name
= find_disk_attached_hba(fd
, NULL
);
4046 pr_err("%s is not attached to Intel(R) RAID controller.\n",
4050 rv
= attach_hba_to_super(super
, hba_name
);
4053 struct intel_hba
*hba
= super
->hba
;
4055 pr_err("%s is attached to Intel(R) %s %s (%s),\n"
4056 " but the container is assigned to Intel(R) %s %s (",
4058 get_sys_dev_type(hba_name
->type
),
4059 hba_name
->type
== SYS_DEV_VMD
? "domain" : "RAID controller",
4060 hba_name
->pci_id
? : "Err!",
4061 get_sys_dev_type(super
->hba
->type
),
4062 hba
->type
== SYS_DEV_VMD
? "domain" : "RAID controller");
4065 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
4067 fprintf(stderr
, ", ");
4070 fprintf(stderr
, ").\n"
4071 " Mixing devices attached to different %s is not allowed.\n",
4072 hba_name
->type
== SYS_DEV_VMD
? "VMD domains" : "controllers");
4076 super
->orom
= find_imsm_capability(hba_name
);
4083 /* find_missing - helper routine for load_super_imsm_all that identifies
4084 * disks that have disappeared from the system. This routine relies on
4085 * the mpb being uptodate, which it is at load time.
4087 static int find_missing(struct intel_super
*super
)
4090 struct imsm_super
*mpb
= super
->anchor
;
4092 struct imsm_disk
*disk
;
4094 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4095 disk
= __get_imsm_disk(mpb
, i
);
4096 dl
= serial_to_dl(disk
->serial
, super
);
4100 dl
= xmalloc(sizeof(*dl
));
4104 dl
->devname
= xstrdup("missing");
4106 serialcpy(dl
->serial
, disk
->serial
);
4109 dl
->next
= super
->missing
;
4110 super
->missing
= dl
;
4117 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
4119 struct intel_disk
*idisk
= disk_list
;
4122 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
4124 idisk
= idisk
->next
;
4130 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
4131 struct intel_super
*super
,
4132 struct intel_disk
**disk_list
)
4134 struct imsm_disk
*d
= &super
->disks
->disk
;
4135 struct imsm_super
*mpb
= super
->anchor
;
4138 for (i
= 0; i
< tbl_size
; i
++) {
4139 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
4140 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
4142 if (tbl_mpb
->family_num
== mpb
->family_num
) {
4143 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
4144 dprintf("mpb from %d:%d matches %d:%d\n",
4145 super
->disks
->major
,
4146 super
->disks
->minor
,
4147 table
[i
]->disks
->major
,
4148 table
[i
]->disks
->minor
);
4152 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
4153 is_configured(d
) == is_configured(tbl_d
)) &&
4154 tbl_mpb
->generation_num
< mpb
->generation_num
) {
4155 /* current version of the mpb is a
4156 * better candidate than the one in
4157 * super_table, but copy over "cross
4158 * generational" status
4160 struct intel_disk
*idisk
;
4162 dprintf("mpb from %d:%d replaces %d:%d\n",
4163 super
->disks
->major
,
4164 super
->disks
->minor
,
4165 table
[i
]->disks
->major
,
4166 table
[i
]->disks
->minor
);
4168 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
4169 if (idisk
&& is_failed(&idisk
->disk
))
4170 tbl_d
->status
|= FAILED_DISK
;
4173 struct intel_disk
*idisk
;
4174 struct imsm_disk
*disk
;
4176 /* tbl_mpb is more up to date, but copy
4177 * over cross generational status before
4180 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
4181 if (disk
&& is_failed(disk
))
4182 d
->status
|= FAILED_DISK
;
4184 idisk
= disk_list_get(d
->serial
, *disk_list
);
4187 if (disk
&& is_configured(disk
))
4188 idisk
->disk
.status
|= CONFIGURED_DISK
;
4191 dprintf("mpb from %d:%d prefer %d:%d\n",
4192 super
->disks
->major
,
4193 super
->disks
->minor
,
4194 table
[i
]->disks
->major
,
4195 table
[i
]->disks
->minor
);
4203 table
[tbl_size
++] = super
;
4207 /* update/extend the merged list of imsm_disk records */
4208 for (j
= 0; j
< mpb
->num_disks
; j
++) {
4209 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
4210 struct intel_disk
*idisk
;
4212 idisk
= disk_list_get(disk
->serial
, *disk_list
);
4214 idisk
->disk
.status
|= disk
->status
;
4215 if (is_configured(&idisk
->disk
) ||
4216 is_failed(&idisk
->disk
))
4217 idisk
->disk
.status
&= ~(SPARE_DISK
);
4219 idisk
= xcalloc(1, sizeof(*idisk
));
4220 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
4221 idisk
->disk
= *disk
;
4222 idisk
->next
= *disk_list
;
4226 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
4233 static struct intel_super
*
4234 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
4237 struct imsm_super
*mpb
= super
->anchor
;
4241 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4242 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
4243 struct intel_disk
*idisk
;
4245 idisk
= disk_list_get(disk
->serial
, disk_list
);
4247 if (idisk
->owner
== owner
||
4248 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
4251 dprintf("'%.16s' owner %d != %d\n",
4252 disk
->serial
, idisk
->owner
,
4255 dprintf("unknown disk %x [%d]: %.16s\n",
4256 __le32_to_cpu(mpb
->family_num
), i
,
4262 if (ok_count
== mpb
->num_disks
)
4267 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
4269 struct intel_super
*s
;
4271 for (s
= super_list
; s
; s
= s
->next
) {
4272 if (family_num
!= s
->anchor
->family_num
)
4274 pr_err("Conflict, offlining family %#x on '%s'\n",
4275 __le32_to_cpu(family_num
), s
->disks
->devname
);
4279 static struct intel_super
*
4280 imsm_thunderdome(struct intel_super
**super_list
, int len
)
4282 struct intel_super
*super_table
[len
];
4283 struct intel_disk
*disk_list
= NULL
;
4284 struct intel_super
*champion
, *spare
;
4285 struct intel_super
*s
, **del
;
4290 memset(super_table
, 0, sizeof(super_table
));
4291 for (s
= *super_list
; s
; s
= s
->next
)
4292 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
4294 for (i
= 0; i
< tbl_size
; i
++) {
4295 struct imsm_disk
*d
;
4296 struct intel_disk
*idisk
;
4297 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
4300 d
= &s
->disks
->disk
;
4302 /* 'd' must appear in merged disk list for its
4303 * configuration to be valid
4305 idisk
= disk_list_get(d
->serial
, disk_list
);
4306 if (idisk
&& idisk
->owner
== i
)
4307 s
= validate_members(s
, disk_list
, i
);
4312 dprintf("marking family: %#x from %d:%d offline\n",
4314 super_table
[i
]->disks
->major
,
4315 super_table
[i
]->disks
->minor
);
4319 /* This is where the mdadm implementation differs from the Windows
4320 * driver which has no strict concept of a container. We can only
4321 * assemble one family from a container, so when returning a prodigal
4322 * array member to this system the code will not be able to disambiguate
4323 * the container contents that should be assembled ("foreign" versus
4324 * "local"). It requires user intervention to set the orig_family_num
4325 * to a new value to establish a new container. The Windows driver in
4326 * this situation fixes up the volume name in place and manages the
4327 * foreign array as an independent entity.
4332 for (i
= 0; i
< tbl_size
; i
++) {
4333 struct intel_super
*tbl_ent
= super_table
[i
];
4339 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
4344 if (s
&& !is_spare
) {
4345 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
4347 } else if (!s
&& !is_spare
)
4360 pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n",
4361 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
4363 /* collect all dl's onto 'champion', and update them to
4364 * champion's version of the status
4366 for (s
= *super_list
; s
; s
= s
->next
) {
4367 struct imsm_super
*mpb
= champion
->anchor
;
4368 struct dl
*dl
= s
->disks
;
4373 mpb
->attributes
|= s
->anchor
->attributes
& MPB_ATTRIB_2TB_DISK
;
4375 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4376 struct imsm_disk
*disk
;
4378 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
4381 /* only set index on disks that are a member of
4382 * a populated contianer, i.e. one with
4385 if (is_failed(&dl
->disk
))
4387 else if (is_spare(&dl
->disk
))
4393 if (i
>= mpb
->num_disks
) {
4394 struct intel_disk
*idisk
;
4396 idisk
= disk_list_get(dl
->serial
, disk_list
);
4397 if (idisk
&& is_spare(&idisk
->disk
) &&
4398 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
4406 dl
->next
= champion
->disks
;
4407 champion
->disks
= dl
;
4411 /* delete 'champion' from super_list */
4412 for (del
= super_list
; *del
; ) {
4413 if (*del
== champion
) {
4414 *del
= (*del
)->next
;
4417 del
= &(*del
)->next
;
4419 champion
->next
= NULL
;
4423 struct intel_disk
*idisk
= disk_list
;
4425 disk_list
= disk_list
->next
;
4433 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
);
4434 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
4435 int major
, int minor
, int keep_fd
);
4437 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4438 int *max
, int keep_fd
);
4440 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
4441 char *devname
, struct md_list
*devlist
,
4444 struct intel_super
*super_list
= NULL
;
4445 struct intel_super
*super
= NULL
;
4450 /* 'fd' is an opened container */
4451 err
= get_sra_super_block(fd
, &super_list
, devname
, &i
, keep_fd
);
4453 /* get super block from devlist devices */
4454 err
= get_devlist_super_block(devlist
, &super_list
, &i
, keep_fd
);
4457 /* all mpbs enter, maybe one leaves */
4458 super
= imsm_thunderdome(&super_list
, i
);
4464 if (find_missing(super
) != 0) {
4470 /* load migration record */
4471 err
= load_imsm_migr_rec(super
, NULL
);
4473 /* migration is in progress,
4474 * but migr_rec cannot be loaded,
4480 /* Check migration compatibility */
4481 if (err
== 0 && check_mpb_migr_compatibility(super
) != 0) {
4482 pr_err("Unsupported migration detected");
4484 fprintf(stderr
, " on %s\n", devname
);
4486 fprintf(stderr
, " (IMSM).\n");
4495 while (super_list
) {
4496 struct intel_super
*s
= super_list
;
4498 super_list
= super_list
->next
;
4507 strcpy(st
->container_devnm
, fd2devnm(fd
));
4509 st
->container_devnm
[0] = 0;
4510 if (err
== 0 && st
->ss
== NULL
) {
4511 st
->ss
= &super_imsm
;
4512 st
->minor_version
= 0;
4513 st
->max_devs
= IMSM_MAX_DEVICES
;
4519 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4520 int *max
, int keep_fd
)
4522 struct md_list
*tmpdev
;
4526 for (i
= 0, tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
4527 if (tmpdev
->used
!= 1)
4529 if (tmpdev
->container
== 1) {
4531 int fd
= dev_open(tmpdev
->devname
, O_RDONLY
|O_EXCL
);
4533 pr_err("cannot open device %s: %s\n",
4534 tmpdev
->devname
, strerror(errno
));
4538 err
= get_sra_super_block(fd
, super_list
,
4539 tmpdev
->devname
, &lmax
,
4548 int major
= major(tmpdev
->st_rdev
);
4549 int minor
= minor(tmpdev
->st_rdev
);
4550 err
= get_super_block(super_list
,
4567 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
4568 int major
, int minor
, int keep_fd
)
4570 struct intel_super
*s
;
4582 sprintf(nm
, "%d:%d", major
, minor
);
4583 dfd
= dev_open(nm
, O_RDWR
);
4589 get_dev_sector_size(dfd
, NULL
, &s
->sector_size
);
4590 find_intel_hba_capability(dfd
, s
, devname
);
4591 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4593 /* retry the load if we might have raced against mdmon */
4594 if (err
== 3 && devnm
&& mdmon_running(devnm
))
4595 for (retry
= 0; retry
< 3; retry
++) {
4597 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4603 s
->next
= *super_list
;
4611 if (dfd
>= 0 && !keep_fd
)
4618 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
)
4625 sra
= sysfs_read(fd
, NULL
, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
4629 if (sra
->array
.major_version
!= -1 ||
4630 sra
->array
.minor_version
!= -2 ||
4631 strcmp(sra
->text_version
, "imsm") != 0) {
4636 devnm
= fd2devnm(fd
);
4637 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
4638 if (get_super_block(super_list
, devnm
, devname
,
4639 sd
->disk
.major
, sd
->disk
.minor
, keep_fd
) != 0) {
4650 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
4652 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
, NULL
, 1);
4656 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
4658 struct intel_super
*super
;
4662 if (test_partition(fd
))
4663 /* IMSM not allowed on partitions */
4666 free_super_imsm(st
);
4668 super
= alloc_super();
4669 get_dev_sector_size(fd
, NULL
, &super
->sector_size
);
4670 /* Load hba and capabilities if they exist.
4671 * But do not preclude loading metadata in case capabilities or hba are
4672 * non-compliant and ignore_hw_compat is set.
4674 rv
= find_intel_hba_capability(fd
, super
, devname
);
4675 /* no orom/efi or non-intel hba of the disk */
4676 if (rv
!= 0 && st
->ignore_hw_compat
== 0) {
4678 pr_err("No OROM/EFI properties for %s\n", devname
);
4682 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
4684 /* retry the load if we might have raced against mdmon */
4686 struct mdstat_ent
*mdstat
= NULL
;
4687 char *name
= fd2kname(fd
);
4690 mdstat
= mdstat_by_component(name
);
4692 if (mdstat
&& mdmon_running(mdstat
->devnm
) && getpid() != mdmon_pid(mdstat
->devnm
)) {
4693 for (retry
= 0; retry
< 3; retry
++) {
4695 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
4701 free_mdstat(mdstat
);
4706 pr_err("Failed to load all information sections on %s\n", devname
);
4712 if (st
->ss
== NULL
) {
4713 st
->ss
= &super_imsm
;
4714 st
->minor_version
= 0;
4715 st
->max_devs
= IMSM_MAX_DEVICES
;
4718 /* load migration record */
4719 if (load_imsm_migr_rec(super
, NULL
) == 0) {
4720 /* Check for unsupported migration features */
4721 if (check_mpb_migr_compatibility(super
) != 0) {
4722 pr_err("Unsupported migration detected");
4724 fprintf(stderr
, " on %s\n", devname
);
4726 fprintf(stderr
, " (IMSM).\n");
4734 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
4736 if (info
->level
== 1)
4738 return info
->chunk_size
>> 9;
4741 static unsigned long long info_to_blocks_per_member(mdu_array_info_t
*info
,
4742 unsigned long long size
)
4744 if (info
->level
== 1)
4747 return (size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
4750 static void imsm_update_version_info(struct intel_super
*super
)
4752 /* update the version and attributes */
4753 struct imsm_super
*mpb
= super
->anchor
;
4755 struct imsm_dev
*dev
;
4756 struct imsm_map
*map
;
4759 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4760 dev
= get_imsm_dev(super
, i
);
4761 map
= get_imsm_map(dev
, MAP_0
);
4762 if (__le32_to_cpu(dev
->size_high
) > 0)
4763 mpb
->attributes
|= MPB_ATTRIB_2TB
;
4765 /* FIXME detect when an array spans a port multiplier */
4767 mpb
->attributes
|= MPB_ATTRIB_PM
;
4770 if (mpb
->num_raid_devs
> 1 ||
4771 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
4772 version
= MPB_VERSION_ATTRIBS
;
4773 switch (get_imsm_raid_level(map
)) {
4774 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
4775 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
4776 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
4777 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
4780 if (map
->num_members
>= 5)
4781 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
4782 else if (dev
->status
== DEV_CLONE_N_GO
)
4783 version
= MPB_VERSION_CNG
;
4784 else if (get_imsm_raid_level(map
) == 5)
4785 version
= MPB_VERSION_RAID5
;
4786 else if (map
->num_members
>= 3)
4787 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
4788 else if (get_imsm_raid_level(map
) == 1)
4789 version
= MPB_VERSION_RAID1
;
4791 version
= MPB_VERSION_RAID0
;
4793 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
4797 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
4799 struct imsm_super
*mpb
= super
->anchor
;
4800 char *reason
= NULL
;
4803 if (strlen(name
) > MAX_RAID_SERIAL_LEN
)
4804 reason
= "must be 16 characters or less";
4806 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4807 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4809 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
4810 reason
= "already exists";
4815 if (reason
&& !quiet
)
4816 pr_err("imsm volume name %s\n", reason
);
4821 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
4822 unsigned long long size
, char *name
,
4823 char *homehost
, int *uuid
,
4824 long long data_offset
)
4826 /* We are creating a volume inside a pre-existing container.
4827 * so st->sb is already set.
4829 struct intel_super
*super
= st
->sb
;
4830 unsigned int sector_size
= super
->sector_size
;
4831 struct imsm_super
*mpb
= super
->anchor
;
4832 struct intel_dev
*dv
;
4833 struct imsm_dev
*dev
;
4834 struct imsm_vol
*vol
;
4835 struct imsm_map
*map
;
4836 int idx
= mpb
->num_raid_devs
;
4838 unsigned long long array_blocks
;
4839 size_t size_old
, size_new
;
4840 unsigned long long num_data_stripes
;
4842 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
4843 pr_err("This imsm-container already has the maximum of %d volumes\n", super
->orom
->vpa
);
4847 /* ensure the mpb is large enough for the new data */
4848 size_old
= __le32_to_cpu(mpb
->mpb_size
);
4849 size_new
= disks_to_mpb_size(info
->nr_disks
);
4850 if (size_new
> size_old
) {
4852 size_t size_round
= ROUND_UP(size_new
, sector_size
);
4854 if (posix_memalign(&mpb_new
, sector_size
, size_round
) != 0) {
4855 pr_err("could not allocate new mpb\n");
4858 if (posix_memalign(&super
->migr_rec_buf
, 512,
4859 MIGR_REC_BUF_SIZE
) != 0) {
4860 pr_err("could not allocate migr_rec buffer\n");
4866 memcpy(mpb_new
, mpb
, size_old
);
4869 super
->anchor
= mpb_new
;
4870 mpb
->mpb_size
= __cpu_to_le32(size_new
);
4871 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
4873 super
->current_vol
= idx
;
4875 /* handle 'failed_disks' by either:
4876 * a) create dummy disk entries in the table if this the first
4877 * volume in the array. We add them here as this is the only
4878 * opportunity to add them. add_to_super_imsm_volume()
4879 * handles the non-failed disks and continues incrementing
4881 * b) validate that 'failed_disks' matches the current number
4882 * of missing disks if the container is populated
4884 if (super
->current_vol
== 0) {
4886 for (i
= 0; i
< info
->failed_disks
; i
++) {
4887 struct imsm_disk
*disk
;
4890 disk
= __get_imsm_disk(mpb
, i
);
4891 disk
->status
= CONFIGURED_DISK
| FAILED_DISK
;
4892 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
4893 snprintf((char *) disk
->serial
, MAX_RAID_SERIAL_LEN
,
4896 find_missing(super
);
4901 for (d
= super
->missing
; d
; d
= d
->next
)
4903 if (info
->failed_disks
> missing
) {
4904 pr_err("unable to add 'missing' disk to container\n");
4909 if (!check_name(super
, name
, 0))
4911 dv
= xmalloc(sizeof(*dv
));
4912 dev
= xcalloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
4913 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
4914 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
4915 info
->layout
, info
->chunk_size
,
4917 /* round array size down to closest MB */
4918 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
4920 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
4921 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
4922 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
4924 vol
->migr_state
= 0;
4925 set_migr_type(dev
, MIGR_INIT
);
4926 vol
->dirty
= !info
->state
;
4927 vol
->curr_migr_unit
= 0;
4928 map
= get_imsm_map(dev
, MAP_0
);
4929 set_pba_of_lba0(map
, super
->create_offset
);
4930 set_blocks_per_member(map
, info_to_blocks_per_member(info
, size
));
4931 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
4932 map
->failed_disk_num
= ~0;
4933 if (info
->level
> 0)
4934 map
->map_state
= (info
->state
? IMSM_T_STATE_NORMAL
4935 : IMSM_T_STATE_UNINITIALIZED
);
4937 map
->map_state
= info
->failed_disks
? IMSM_T_STATE_FAILED
:
4938 IMSM_T_STATE_NORMAL
;
4941 if (info
->level
== 1 && info
->raid_disks
> 2) {
4944 pr_err("imsm does not support more than 2 disksin a raid1 volume\n");
4948 map
->raid_level
= info
->level
;
4949 if (info
->level
== 10) {
4950 map
->raid_level
= 1;
4951 map
->num_domains
= info
->raid_disks
/ 2;
4952 } else if (info
->level
== 1)
4953 map
->num_domains
= info
->raid_disks
;
4955 map
->num_domains
= 1;
4957 /* info->size is only int so use the 'size' parameter instead */
4958 num_data_stripes
= (size
* 2) / info_to_blocks_per_strip(info
);
4959 num_data_stripes
/= map
->num_domains
;
4960 set_num_data_stripes(map
, num_data_stripes
);
4962 map
->num_members
= info
->raid_disks
;
4963 for (i
= 0; i
< map
->num_members
; i
++) {
4964 /* initialized in add_to_super */
4965 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
4967 mpb
->num_raid_devs
++;
4970 dv
->index
= super
->current_vol
;
4971 dv
->next
= super
->devlist
;
4972 super
->devlist
= dv
;
4974 imsm_update_version_info(super
);
4979 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
4980 unsigned long long size
, char *name
,
4981 char *homehost
, int *uuid
,
4982 unsigned long long data_offset
)
4984 /* This is primarily called by Create when creating a new array.
4985 * We will then get add_to_super called for each component, and then
4986 * write_init_super called to write it out to each device.
4987 * For IMSM, Create can create on fresh devices or on a pre-existing
4989 * To create on a pre-existing array a different method will be called.
4990 * This one is just for fresh drives.
4992 struct intel_super
*super
;
4993 struct imsm_super
*mpb
;
4997 if (data_offset
!= INVALID_SECTORS
) {
4998 pr_err("data-offset not supported by imsm\n");
5003 return init_super_imsm_volume(st
, info
, size
, name
, homehost
, uuid
,
5007 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
5009 mpb_size
= MAX_SECTOR_SIZE
;
5011 super
= alloc_super();
5013 posix_memalign(&super
->buf
, MAX_SECTOR_SIZE
, mpb_size
) != 0) {
5018 pr_err("could not allocate superblock\n");
5021 if (posix_memalign(&super
->migr_rec_buf
, 512, MIGR_REC_BUF_SIZE
) != 0) {
5022 pr_err("could not allocate migr_rec buffer\n");
5027 memset(super
->buf
, 0, mpb_size
);
5029 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5033 /* zeroing superblock */
5037 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5039 version
= (char *) mpb
->sig
;
5040 strcpy(version
, MPB_SIGNATURE
);
5041 version
+= strlen(MPB_SIGNATURE
);
5042 strcpy(version
, MPB_VERSION_RAID0
);
5048 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
5049 int fd
, char *devname
)
5051 struct intel_super
*super
= st
->sb
;
5052 struct imsm_super
*mpb
= super
->anchor
;
5053 struct imsm_disk
*_disk
;
5054 struct imsm_dev
*dev
;
5055 struct imsm_map
*map
;
5059 dev
= get_imsm_dev(super
, super
->current_vol
);
5060 map
= get_imsm_map(dev
, MAP_0
);
5062 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
5063 pr_err("%s: Cannot add spare devices to IMSM volume\n",
5069 /* we're doing autolayout so grab the pre-marked (in
5070 * validate_geometry) raid_disk
5072 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5073 if (dl
->raiddisk
== dk
->raid_disk
)
5076 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5077 if (dl
->major
== dk
->major
&&
5078 dl
->minor
== dk
->minor
)
5083 pr_err("%s is not a member of the same container\n", devname
);
5087 /* add a pristine spare to the metadata */
5088 if (dl
->index
< 0) {
5089 dl
->index
= super
->anchor
->num_disks
;
5090 super
->anchor
->num_disks
++;
5092 /* Check the device has not already been added */
5093 slot
= get_imsm_disk_slot(map
, dl
->index
);
5095 (get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
) & IMSM_ORD_REBUILD
) == 0) {
5096 pr_err("%s has been included in this array twice\n",
5100 set_imsm_ord_tbl_ent(map
, dk
->raid_disk
, dl
->index
);
5101 dl
->disk
.status
= CONFIGURED_DISK
;
5103 /* update size of 'missing' disks to be at least as large as the
5104 * largest acitve member (we only have dummy missing disks when
5105 * creating the first volume)
5107 if (super
->current_vol
== 0) {
5108 for (df
= super
->missing
; df
; df
= df
->next
) {
5109 if (total_blocks(&dl
->disk
) > total_blocks(&df
->disk
))
5110 set_total_blocks(&df
->disk
, total_blocks(&dl
->disk
));
5111 _disk
= __get_imsm_disk(mpb
, df
->index
);
5116 /* refresh unset/failed slots to point to valid 'missing' entries */
5117 for (df
= super
->missing
; df
; df
= df
->next
)
5118 for (slot
= 0; slot
< mpb
->num_disks
; slot
++) {
5119 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
5121 if ((ord
& IMSM_ORD_REBUILD
) == 0)
5123 set_imsm_ord_tbl_ent(map
, slot
, df
->index
| IMSM_ORD_REBUILD
);
5124 if (is_gen_migration(dev
)) {
5125 struct imsm_map
*map2
= get_imsm_map(dev
,
5127 int slot2
= get_imsm_disk_slot(map2
, df
->index
);
5128 if (slot2
< map2
->num_members
&& slot2
>= 0) {
5129 __u32 ord2
= get_imsm_ord_tbl_ent(dev
,
5132 if ((unsigned)df
->index
==
5134 set_imsm_ord_tbl_ent(map2
,
5140 dprintf("set slot:%d to missing disk:%d\n", slot
, df
->index
);
5144 /* if we are creating the first raid device update the family number */
5145 if (super
->current_vol
== 0) {
5147 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
5149 _disk
= __get_imsm_disk(mpb
, dl
->index
);
5150 if (!_dev
|| !_disk
) {
5151 pr_err("BUG mpb setup error\n");
5157 sum
+= __gen_imsm_checksum(mpb
);
5158 mpb
->family_num
= __cpu_to_le32(sum
);
5159 mpb
->orig_family_num
= mpb
->family_num
;
5161 super
->current_disk
= dl
;
5166 * Function marks disk as spare and restores disk serial
5167 * in case it was previously marked as failed by takeover operation
5169 * -1 : critical error
5170 * 0 : disk is marked as spare but serial is not set
5173 int mark_spare(struct dl
*disk
)
5175 __u8 serial
[MAX_RAID_SERIAL_LEN
];
5182 if (!imsm_read_serial(disk
->fd
, NULL
, serial
)) {
5183 /* Restore disk serial number, because takeover marks disk
5184 * as failed and adds to serial ':0' before it becomes
5187 serialcpy(disk
->serial
, serial
);
5188 serialcpy(disk
->disk
.serial
, serial
);
5191 disk
->disk
.status
= SPARE_DISK
;
5197 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
5198 int fd
, char *devname
,
5199 unsigned long long data_offset
)
5201 struct intel_super
*super
= st
->sb
;
5203 unsigned long long size
;
5204 unsigned int member_sector_size
;
5209 /* If we are on an RAID enabled platform check that the disk is
5210 * attached to the raid controller.
5211 * We do not need to test disks attachment for container based additions,
5212 * they shall be already tested when container was created/assembled.
5214 rv
= find_intel_hba_capability(fd
, super
, devname
);
5215 /* no orom/efi or non-intel hba of the disk */
5217 dprintf("capability: %p fd: %d ret: %d\n",
5218 super
->orom
, fd
, rv
);
5222 if (super
->current_vol
>= 0)
5223 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
5226 dd
= xcalloc(sizeof(*dd
), 1);
5227 dd
->major
= major(stb
.st_rdev
);
5228 dd
->minor
= minor(stb
.st_rdev
);
5229 dd
->devname
= devname
? xstrdup(devname
) : NULL
;
5232 dd
->action
= DISK_ADD
;
5233 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
5235 pr_err("failed to retrieve scsi serial, aborting\n");
5241 if (super
->hba
&& ((super
->hba
->type
== SYS_DEV_NVME
) ||
5242 (super
->hba
->type
== SYS_DEV_VMD
))) {
5244 char *devpath
= diskfd_to_devpath(fd
);
5245 char controller_path
[PATH_MAX
];
5248 pr_err("failed to get devpath, aborting\n");
5255 snprintf(controller_path
, PATH_MAX
-1, "%s/device", devpath
);
5258 if (devpath_to_vendor(controller_path
) == 0x8086) {
5260 * If Intel's NVMe drive has serial ended with
5261 * "-A","-B","-1" or "-2" it means that this is "x8"
5262 * device (double drive on single PCIe card).
5263 * User should be warned about potential data loss.
5265 for (i
= MAX_RAID_SERIAL_LEN
-1; i
> 0; i
--) {
5266 /* Skip empty character at the end */
5267 if (dd
->serial
[i
] == 0)
5270 if (((dd
->serial
[i
] == 'A') ||
5271 (dd
->serial
[i
] == 'B') ||
5272 (dd
->serial
[i
] == '1') ||
5273 (dd
->serial
[i
] == '2')) &&
5274 (dd
->serial
[i
-1] == '-'))
5275 pr_err("\tThe action you are about to take may put your data at risk.\n"
5276 "\tPlease note that x8 devices may consist of two separate x4 devices "
5277 "located on a single PCIe port.\n"
5278 "\tRAID 0 is the only supported configuration for this type of x8 device.\n");
5284 get_dev_size(fd
, NULL
, &size
);
5285 get_dev_sector_size(fd
, NULL
, &member_sector_size
);
5287 if (super
->sector_size
== 0) {
5288 /* this a first device, so sector_size is not set yet */
5289 super
->sector_size
= member_sector_size
;
5290 } else if (member_sector_size
!= super
->sector_size
) {
5291 pr_err("Mixing between different sector size is forbidden, aborting...\n");
5298 /* clear migr_rec when adding disk to container */
5299 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SIZE
);
5300 if (lseek64(fd
, size
- MIGR_REC_POSITION
, SEEK_SET
) >= 0) {
5301 if (write(fd
, super
->migr_rec_buf
,
5302 MIGR_REC_BUF_SIZE
) != MIGR_REC_BUF_SIZE
)
5303 perror("Write migr_rec failed");
5307 serialcpy(dd
->disk
.serial
, dd
->serial
);
5308 set_total_blocks(&dd
->disk
, size
);
5309 if (__le32_to_cpu(dd
->disk
.total_blocks_hi
) > 0) {
5310 struct imsm_super
*mpb
= super
->anchor
;
5311 mpb
->attributes
|= MPB_ATTRIB_2TB_DISK
;
5314 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
5315 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
5317 dd
->disk
.scsi_id
= __cpu_to_le32(0);
5319 if (st
->update_tail
) {
5320 dd
->next
= super
->disk_mgmt_list
;
5321 super
->disk_mgmt_list
= dd
;
5323 dd
->next
= super
->disks
;
5325 super
->updates_pending
++;
5331 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
5333 struct intel_super
*super
= st
->sb
;
5336 /* remove from super works only in mdmon - for communication
5337 * manager - monitor. Check if communication memory buffer
5340 if (!st
->update_tail
) {
5341 pr_err("shall be used in mdmon context only\n");
5344 dd
= xcalloc(1, sizeof(*dd
));
5345 dd
->major
= dk
->major
;
5346 dd
->minor
= dk
->minor
;
5349 dd
->action
= DISK_REMOVE
;
5351 dd
->next
= super
->disk_mgmt_list
;
5352 super
->disk_mgmt_list
= dd
;
5357 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
5360 char buf
[MAX_SECTOR_SIZE
];
5361 struct imsm_super anchor
;
5362 } spare_record
__attribute__ ((aligned(MAX_SECTOR_SIZE
)));
5364 /* spare records have their own family number and do not have any defined raid
5367 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
5369 struct imsm_super
*mpb
= super
->anchor
;
5370 struct imsm_super
*spare
= &spare_record
.anchor
;
5374 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
));
5375 spare
->generation_num
= __cpu_to_le32(1UL);
5376 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5377 spare
->num_disks
= 1;
5378 spare
->num_raid_devs
= 0;
5379 spare
->cache_size
= mpb
->cache_size
;
5380 spare
->pwr_cycle_count
= __cpu_to_le32(1);
5382 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
5383 MPB_SIGNATURE MPB_VERSION_RAID0
);
5385 for (d
= super
->disks
; d
; d
= d
->next
) {
5389 spare
->disk
[0] = d
->disk
;
5390 if (__le32_to_cpu(d
->disk
.total_blocks_hi
) > 0)
5391 spare
->attributes
|= MPB_ATTRIB_2TB_DISK
;
5393 if (super
->sector_size
== 4096)
5394 convert_to_4k_imsm_disk(&spare
->disk
[0]);
5396 sum
= __gen_imsm_checksum(spare
);
5397 spare
->family_num
= __cpu_to_le32(sum
);
5398 spare
->orig_family_num
= 0;
5399 sum
= __gen_imsm_checksum(spare
);
5400 spare
->check_sum
= __cpu_to_le32(sum
);
5402 if (store_imsm_mpb(d
->fd
, spare
)) {
5403 pr_err("failed for device %d:%d %s\n",
5404 d
->major
, d
->minor
, strerror(errno
));
5416 static int write_super_imsm(struct supertype
*st
, int doclose
)
5418 struct intel_super
*super
= st
->sb
;
5419 unsigned int sector_size
= super
->sector_size
;
5420 struct imsm_super
*mpb
= super
->anchor
;
5426 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
5428 int clear_migration_record
= 1;
5430 /* 'generation' is incremented everytime the metadata is written */
5431 generation
= __le32_to_cpu(mpb
->generation_num
);
5433 mpb
->generation_num
= __cpu_to_le32(generation
);
5435 /* fix up cases where previous mdadm releases failed to set
5438 if (mpb
->orig_family_num
== 0)
5439 mpb
->orig_family_num
= mpb
->family_num
;
5441 for (d
= super
->disks
; d
; d
= d
->next
) {
5445 mpb
->disk
[d
->index
] = d
->disk
;
5449 for (d
= super
->missing
; d
; d
= d
->next
) {
5450 mpb
->disk
[d
->index
] = d
->disk
;
5453 mpb
->num_disks
= num_disks
;
5454 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
5456 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5457 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
5458 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
5460 imsm_copy_dev(dev
, dev2
);
5461 mpb_size
+= sizeof_imsm_dev(dev
, 0);
5463 if (is_gen_migration(dev2
))
5464 clear_migration_record
= 0;
5466 mpb_size
+= __le32_to_cpu(mpb
->bbm_log_size
);
5467 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5469 /* recalculate checksum */
5470 sum
= __gen_imsm_checksum(mpb
);
5471 mpb
->check_sum
= __cpu_to_le32(sum
);
5473 if (super
->clean_migration_record_by_mdmon
) {
5474 clear_migration_record
= 1;
5475 super
->clean_migration_record_by_mdmon
= 0;
5477 if (clear_migration_record
)
5478 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SIZE
);
5480 if (sector_size
== 4096)
5481 convert_to_4k(super
);
5483 /* write the mpb for disks that compose raid devices */
5484 for (d
= super
->disks
; d
; d
= d
->next
) {
5485 if (d
->index
< 0 || is_failed(&d
->disk
))
5488 if (clear_migration_record
) {
5489 unsigned long long dsize
;
5491 get_dev_size(d
->fd
, NULL
, &dsize
);
5492 if (lseek64(d
->fd
, dsize
- 512, SEEK_SET
) >= 0) {
5493 if (write(d
->fd
, super
->migr_rec_buf
,
5494 MIGR_REC_BUF_SIZE
) != MIGR_REC_BUF_SIZE
)
5495 perror("Write migr_rec failed");
5499 if (store_imsm_mpb(d
->fd
, mpb
))
5501 "failed for device %d:%d (fd: %d)%s\n",
5503 d
->fd
, strerror(errno
));
5512 return write_super_imsm_spares(super
, doclose
);
5517 static int create_array(struct supertype
*st
, int dev_idx
)
5520 struct imsm_update_create_array
*u
;
5521 struct intel_super
*super
= st
->sb
;
5522 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
5523 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
5524 struct disk_info
*inf
;
5525 struct imsm_disk
*disk
;
5528 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
5529 sizeof(*inf
) * map
->num_members
;
5531 u
->type
= update_create_array
;
5532 u
->dev_idx
= dev_idx
;
5533 imsm_copy_dev(&u
->dev
, dev
);
5534 inf
= get_disk_info(u
);
5535 for (i
= 0; i
< map
->num_members
; i
++) {
5536 int idx
= get_imsm_disk_idx(dev
, i
, MAP_X
);
5538 disk
= get_imsm_disk(super
, idx
);
5540 disk
= get_imsm_missing(super
, idx
);
5541 serialcpy(inf
[i
].serial
, disk
->serial
);
5543 append_metadata_update(st
, u
, len
);
5548 static int mgmt_disk(struct supertype
*st
)
5550 struct intel_super
*super
= st
->sb
;
5552 struct imsm_update_add_remove_disk
*u
;
5554 if (!super
->disk_mgmt_list
)
5559 u
->type
= update_add_remove_disk
;
5560 append_metadata_update(st
, u
, len
);
5565 static int write_init_super_imsm(struct supertype
*st
)
5567 struct intel_super
*super
= st
->sb
;
5568 int current_vol
= super
->current_vol
;
5570 /* we are done with current_vol reset it to point st at the container */
5571 super
->current_vol
= -1;
5573 if (st
->update_tail
) {
5574 /* queue the recently created array / added disk
5575 * as a metadata update */
5578 /* determine if we are creating a volume or adding a disk */
5579 if (current_vol
< 0) {
5580 /* in the mgmt (add/remove) disk case we are running
5581 * in mdmon context, so don't close fd's
5583 return mgmt_disk(st
);
5585 rv
= create_array(st
, current_vol
);
5590 for (d
= super
->disks
; d
; d
= d
->next
)
5591 Kill(d
->devname
, NULL
, 0, -1, 1);
5592 return write_super_imsm(st
, 1);
5597 static int store_super_imsm(struct supertype
*st
, int fd
)
5599 struct intel_super
*super
= st
->sb
;
5600 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
5606 if (super
->sector_size
== 4096)
5607 convert_to_4k(super
);
5608 return store_imsm_mpb(fd
, mpb
);
5614 static int imsm_bbm_log_size(struct imsm_super
*mpb
)
5616 return __le32_to_cpu(mpb
->bbm_log_size
);
5620 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
5621 int layout
, int raiddisks
, int chunk
,
5622 unsigned long long size
,
5623 unsigned long long data_offset
,
5625 unsigned long long *freesize
,
5629 unsigned long long ldsize
;
5630 struct intel_super
*super
;
5633 if (level
!= LEVEL_CONTAINER
)
5638 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
5641 pr_err("imsm: Cannot open %s: %s\n",
5642 dev
, strerror(errno
));
5645 if (!get_dev_size(fd
, dev
, &ldsize
)) {
5650 /* capabilities retrieve could be possible
5651 * note that there is no fd for the disks in array.
5653 super
= alloc_super();
5654 if (!get_dev_sector_size(fd
, NULL
, &super
->sector_size
)) {
5660 rv
= find_intel_hba_capability(fd
, super
, verbose
> 0 ? dev
: NULL
);
5664 fd2devname(fd
, str
);
5665 dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n",
5666 fd
, str
, super
->orom
, rv
, raiddisks
);
5668 /* no orom/efi or non-intel hba of the disk */
5675 if (raiddisks
> super
->orom
->tds
) {
5677 pr_err("%d exceeds maximum number of platform supported disks: %d\n",
5678 raiddisks
, super
->orom
->tds
);
5682 if ((super
->orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) == 0 &&
5683 (ldsize
>> 9) >> 32 > 0) {
5685 pr_err("%s exceeds maximum platform supported size\n", dev
);
5691 *freesize
= avail_size_imsm(st
, ldsize
>> 9, data_offset
);
5697 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
5699 const unsigned long long base_start
= e
[*idx
].start
;
5700 unsigned long long end
= base_start
+ e
[*idx
].size
;
5703 if (base_start
== end
)
5707 for (i
= *idx
; i
< num_extents
; i
++) {
5708 /* extend overlapping extents */
5709 if (e
[i
].start
>= base_start
&&
5710 e
[i
].start
<= end
) {
5713 if (e
[i
].start
+ e
[i
].size
> end
)
5714 end
= e
[i
].start
+ e
[i
].size
;
5715 } else if (e
[i
].start
> end
) {
5721 return end
- base_start
;
5724 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
5726 /* build a composite disk with all known extents and generate a new
5727 * 'maxsize' given the "all disks in an array must share a common start
5728 * offset" constraint
5730 struct extent
*e
= xcalloc(sum_extents
, sizeof(*e
));
5734 unsigned long long pos
;
5735 unsigned long long start
= 0;
5736 unsigned long long maxsize
;
5737 unsigned long reserve
;
5739 /* coalesce and sort all extents. also, check to see if we need to
5740 * reserve space between member arrays
5743 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5746 for (i
= 0; i
< dl
->extent_cnt
; i
++)
5749 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
5754 while (i
< sum_extents
) {
5755 e
[j
].start
= e
[i
].start
;
5756 e
[j
].size
= find_size(e
, &i
, sum_extents
);
5758 if (e
[j
-1].size
== 0)
5767 unsigned long long esize
;
5769 esize
= e
[i
].start
- pos
;
5770 if (esize
>= maxsize
) {
5775 pos
= e
[i
].start
+ e
[i
].size
;
5777 } while (e
[i
-1].size
);
5783 /* FIXME assumes volume at offset 0 is the first volume in a
5786 if (start_extent
> 0)
5787 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
5791 if (maxsize
< reserve
)
5794 super
->create_offset
= ~((unsigned long long) 0);
5795 if (start
+ reserve
> super
->create_offset
)
5796 return 0; /* start overflows create_offset */
5797 super
->create_offset
= start
+ reserve
;
5799 return maxsize
- reserve
;
5802 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
5804 if (level
< 0 || level
== 6 || level
== 4)
5807 /* if we have an orom prevent invalid raid levels */
5810 case 0: return imsm_orom_has_raid0(orom
);
5813 return imsm_orom_has_raid1e(orom
);
5814 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
5815 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
5816 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
5819 return 1; /* not on an Intel RAID platform so anything goes */
5825 active_arrays_by_format(char *name
, char* hba
, struct md_list
**devlist
,
5826 int dpa
, int verbose
)
5828 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
5829 struct mdstat_ent
*memb
;
5835 for (memb
= mdstat
; memb
; memb
= memb
->next
) {
5836 if (memb
->metadata_version
&&
5837 (strncmp(memb
->metadata_version
, "external:", 9) == 0) &&
5838 (strcmp(&memb
->metadata_version
[9], name
) == 0) &&
5839 !is_subarray(memb
->metadata_version
+9) &&
5841 struct dev_member
*dev
= memb
->members
;
5843 while(dev
&& (fd
< 0)) {
5844 char *path
= xmalloc(strlen(dev
->name
) + strlen("/dev/") + 1);
5845 num
= sprintf(path
, "%s%s", "/dev/", dev
->name
);
5847 fd
= open(path
, O_RDONLY
, 0);
5848 if (num
<= 0 || fd
< 0) {
5849 pr_vrb("Cannot open %s: %s\n",
5850 dev
->name
, strerror(errno
));
5856 if (fd
>= 0 && disk_attached_to_hba(fd
, hba
)) {
5857 struct mdstat_ent
*vol
;
5858 for (vol
= mdstat
; vol
; vol
= vol
->next
) {
5859 if (vol
->active
> 0 &&
5860 vol
->metadata_version
&&
5861 is_container_member(vol
, memb
->devnm
)) {
5866 if (*devlist
&& (found
< dpa
)) {
5867 dv
= xcalloc(1, sizeof(*dv
));
5868 dv
->devname
= xmalloc(strlen(memb
->devnm
) + strlen("/dev/") + 1);
5869 sprintf(dv
->devname
, "%s%s", "/dev/", memb
->devnm
);
5872 dv
->next
= *devlist
;
5880 free_mdstat(mdstat
);
5885 static struct md_list
*
5886 get_loop_devices(void)
5889 struct md_list
*devlist
= NULL
;
5892 for(i
= 0; i
< 12; i
++) {
5893 dv
= xcalloc(1, sizeof(*dv
));
5894 dv
->devname
= xmalloc(40);
5895 sprintf(dv
->devname
, "/dev/loop%d", i
);
5903 static struct md_list
*
5904 get_devices(const char *hba_path
)
5906 struct md_list
*devlist
= NULL
;
5913 devlist
= get_loop_devices();
5916 /* scroll through /sys/dev/block looking for devices attached to
5919 dir
= opendir("/sys/dev/block");
5920 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
5925 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
5927 path
= devt_to_devpath(makedev(major
, minor
));
5930 if (!path_attached_to_hba(path
, hba_path
)) {
5937 fd
= dev_open(ent
->d_name
, O_RDONLY
);
5939 fd2devname(fd
, buf
);
5942 pr_err("cannot open device: %s\n",
5947 dv
= xcalloc(1, sizeof(*dv
));
5948 dv
->devname
= xstrdup(buf
);
5955 devlist
= devlist
->next
;
5965 count_volumes_list(struct md_list
*devlist
, char *homehost
,
5966 int verbose
, int *found
)
5968 struct md_list
*tmpdev
;
5970 struct supertype
*st
;
5972 /* first walk the list of devices to find a consistent set
5973 * that match the criterea, if that is possible.
5974 * We flag the ones we like with 'used'.
5977 st
= match_metadata_desc_imsm("imsm");
5979 pr_vrb("cannot allocate memory for imsm supertype\n");
5983 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5984 char *devname
= tmpdev
->devname
;
5986 struct supertype
*tst
;
5988 if (tmpdev
->used
> 1)
5990 tst
= dup_super(st
);
5992 pr_vrb("cannot allocate memory for imsm supertype\n");
5995 tmpdev
->container
= 0;
5996 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
5998 dprintf("cannot open device %s: %s\n",
5999 devname
, strerror(errno
));
6001 } else if (fstat(dfd
, &stb
)< 0) {
6003 dprintf("fstat failed for %s: %s\n",
6004 devname
, strerror(errno
));
6006 } else if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
6007 dprintf("%s is not a block device.\n",
6010 } else if (must_be_container(dfd
)) {
6011 struct supertype
*cst
;
6012 cst
= super_by_fd(dfd
, NULL
);
6014 dprintf("cannot recognize container type %s\n",
6017 } else if (tst
->ss
!= st
->ss
) {
6018 dprintf("non-imsm container - ignore it: %s\n",
6021 } else if (!tst
->ss
->load_container
||
6022 tst
->ss
->load_container(tst
, dfd
, NULL
))
6025 tmpdev
->container
= 1;
6028 cst
->ss
->free_super(cst
);
6030 tmpdev
->st_rdev
= stb
.st_rdev
;
6031 if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
6032 dprintf("no RAID superblock on %s\n",
6035 } else if (tst
->ss
->compare_super
== NULL
) {
6036 dprintf("Cannot assemble %s metadata on %s\n",
6037 tst
->ss
->name
, devname
);
6043 if (tmpdev
->used
== 2 || tmpdev
->used
== 4) {
6044 /* Ignore unrecognised devices during auto-assembly */
6049 tst
->ss
->getinfo_super(tst
, &info
, NULL
);
6051 if (st
->minor_version
== -1)
6052 st
->minor_version
= tst
->minor_version
;
6054 if (memcmp(info
.uuid
, uuid_zero
,
6055 sizeof(int[4])) == 0) {
6056 /* this is a floating spare. It cannot define
6057 * an array unless there are no more arrays of
6058 * this type to be found. It can be included
6059 * in an array of this type though.
6065 if (st
->ss
!= tst
->ss
||
6066 st
->minor_version
!= tst
->minor_version
||
6067 st
->ss
->compare_super(st
, tst
) != 0) {
6068 /* Some mismatch. If exactly one array matches this host,
6069 * we can resolve on that one.
6070 * Or, if we are auto assembling, we just ignore the second
6073 dprintf("superblock on %s doesn't match others - assembly aborted\n",
6079 dprintf("found: devname: %s\n", devname
);
6083 tst
->ss
->free_super(tst
);
6087 if ((err
= load_super_imsm_all(st
, -1, &st
->sb
, NULL
, devlist
, 0)) == 0) {
6088 struct mdinfo
*iter
, *head
= st
->ss
->container_content(st
, NULL
);
6089 for (iter
= head
; iter
; iter
= iter
->next
) {
6090 dprintf("content->text_version: %s vol\n",
6091 iter
->text_version
);
6092 if (iter
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
6093 /* do not assemble arrays with unsupported
6095 dprintf("Cannot activate member %s.\n",
6096 iter
->text_version
);
6103 dprintf("No valid super block on device list: err: %d %p\n",
6107 dprintf("no more devices to examine\n");
6110 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
6111 if (tmpdev
->used
== 1 && tmpdev
->found
) {
6113 if (count
< tmpdev
->found
)
6116 count
-= tmpdev
->found
;
6119 if (tmpdev
->used
== 1)
6124 st
->ss
->free_super(st
);
6129 count_volumes(struct intel_hba
*hba
, int dpa
, int verbose
)
6131 struct sys_dev
*idev
, *intel_devices
= find_intel_devices();
6133 const struct orom_entry
*entry
;
6134 struct devid_list
*dv
, *devid_list
;
6136 if (!hba
|| !hba
->path
)
6139 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
6140 if (strstr(idev
->path
, hba
->path
))
6144 if (!idev
|| !idev
->dev_id
)
6147 entry
= get_orom_entry_by_device_id(idev
->dev_id
);
6149 if (!entry
|| !entry
->devid_list
)
6152 devid_list
= entry
->devid_list
;
6153 for (dv
= devid_list
; dv
; dv
= dv
->next
) {
6154 struct md_list
*devlist
;
6155 struct sys_dev
*device
= device_by_id(dv
->devid
);
6160 hba_path
= device
->path
;
6164 devlist
= get_devices(hba_path
);
6165 /* if no intel devices return zero volumes */
6166 if (devlist
== NULL
)
6169 count
+= active_arrays_by_format("imsm", hba_path
, &devlist
, dpa
, verbose
);
6170 dprintf("path: %s active arrays: %d\n", hba_path
, count
);
6171 if (devlist
== NULL
)
6175 count
+= count_volumes_list(devlist
,
6179 dprintf("found %d count: %d\n", found
, count
);
6182 dprintf("path: %s total number of volumes: %d\n", hba_path
, count
);
6185 struct md_list
*dv
= devlist
;
6186 devlist
= devlist
->next
;
6194 static int imsm_default_chunk(const struct imsm_orom
*orom
)
6196 /* up to 512 if the plaform supports it, otherwise the platform max.
6197 * 128 if no platform detected
6199 int fs
= max(7, orom
? fls(orom
->sss
) : 0);
6201 return min(512, (1 << fs
));
6205 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
6206 int raiddisks
, int *chunk
, unsigned long long size
, int verbose
)
6208 /* check/set platform and metadata limits/defaults */
6209 if (super
->orom
&& raiddisks
> super
->orom
->dpa
) {
6210 pr_vrb("platform supports a maximum of %d disks per array\n",
6215 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
6216 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
6217 pr_vrb("platform does not support raid%d with %d disk%s\n",
6218 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
6222 if (*chunk
== 0 || *chunk
== UnSet
)
6223 *chunk
= imsm_default_chunk(super
->orom
);
6225 if (super
->orom
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
6226 pr_vrb("platform does not support a chunk size of: %d\n", *chunk
);
6230 if (layout
!= imsm_level_to_layout(level
)) {
6232 pr_vrb("imsm raid 5 only supports the left-asymmetric layout\n");
6233 else if (level
== 10)
6234 pr_vrb("imsm raid 10 only supports the n2 layout\n");
6236 pr_vrb("imsm unknown layout %#x for this raid level %d\n",
6241 if (super
->orom
&& (super
->orom
->attr
& IMSM_OROM_ATTR_2TB
) == 0 &&
6242 (calc_array_size(level
, raiddisks
, layout
, *chunk
, size
) >> 32) > 0) {
6243 pr_vrb("platform does not support a volume size over 2TB\n");
6250 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
6251 * FIX ME add ahci details
6253 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
6254 int layout
, int raiddisks
, int *chunk
,
6255 unsigned long long size
,
6256 unsigned long long data_offset
,
6258 unsigned long long *freesize
,
6262 struct intel_super
*super
= st
->sb
;
6263 struct imsm_super
*mpb
;
6265 unsigned long long pos
= 0;
6266 unsigned long long maxsize
;
6270 /* We must have the container info already read in. */
6274 mpb
= super
->anchor
;
6276 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, size
, verbose
)) {
6277 pr_err("RAID gemetry validation failed. Cannot proceed with the action(s).\n");
6281 /* General test: make sure there is space for
6282 * 'raiddisks' device extents of size 'size' at a given
6285 unsigned long long minsize
= size
;
6286 unsigned long long start_offset
= MaxSector
;
6289 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
6290 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6295 e
= get_extents(super
, dl
);
6298 unsigned long long esize
;
6299 esize
= e
[i
].start
- pos
;
6300 if (esize
>= minsize
)
6302 if (found
&& start_offset
== MaxSector
) {
6305 } else if (found
&& pos
!= start_offset
) {
6309 pos
= e
[i
].start
+ e
[i
].size
;
6311 } while (e
[i
-1].size
);
6316 if (dcnt
< raiddisks
) {
6318 pr_err("imsm: Not enough devices with space for this array (%d < %d)\n",
6325 /* This device must be a member of the set */
6326 if (stat(dev
, &stb
) < 0)
6328 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
6330 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6331 if (dl
->major
== (int)major(stb
.st_rdev
) &&
6332 dl
->minor
== (int)minor(stb
.st_rdev
))
6337 pr_err("%s is not in the same imsm set\n", dev
);
6339 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
6340 /* If a volume is present then the current creation attempt
6341 * cannot incorporate new spares because the orom may not
6342 * understand this configuration (all member disks must be
6343 * members of each array in the container).
6345 pr_err("%s is a spare and a volume is already defined for this container\n", dev
);
6346 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
6348 } else if (super
->orom
&& mpb
->num_raid_devs
> 0 &&
6349 mpb
->num_disks
!= raiddisks
) {
6350 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
6354 /* retrieve the largest free space block */
6355 e
= get_extents(super
, dl
);
6360 unsigned long long esize
;
6362 esize
= e
[i
].start
- pos
;
6363 if (esize
>= maxsize
)
6365 pos
= e
[i
].start
+ e
[i
].size
;
6367 } while (e
[i
-1].size
);
6372 pr_err("unable to determine free space for: %s\n",
6376 if (maxsize
< size
) {
6378 pr_err("%s not enough space (%llu < %llu)\n",
6379 dev
, maxsize
, size
);
6383 /* count total number of extents for merge */
6385 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6387 i
+= dl
->extent_cnt
;
6389 maxsize
= merge_extents(super
, i
);
6391 if (!check_env("IMSM_NO_PLATFORM") &&
6392 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6393 pr_err("attempting to create a second volume with size less then remaining space. Aborting...\n");
6397 if (maxsize
< size
|| maxsize
== 0) {
6400 pr_err("no free space left on device. Aborting...\n");
6402 pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n",
6408 *freesize
= maxsize
;
6411 int count
= count_volumes(super
->hba
,
6412 super
->orom
->dpa
, verbose
);
6413 if (super
->orom
->vphba
<= count
) {
6414 pr_vrb("platform does not support more than %d raid volumes.\n",
6415 super
->orom
->vphba
);
6422 static int imsm_get_free_size(struct supertype
*st
, int raiddisks
,
6423 unsigned long long size
, int chunk
,
6424 unsigned long long *freesize
)
6426 struct intel_super
*super
= st
->sb
;
6427 struct imsm_super
*mpb
= super
->anchor
;
6432 unsigned long long maxsize
;
6433 unsigned long long minsize
;
6437 /* find the largest common start free region of the possible disks */
6441 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6447 /* don't activate new spares if we are orom constrained
6448 * and there is already a volume active in the container
6450 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
6453 e
= get_extents(super
, dl
);
6456 for (i
= 1; e
[i
-1].size
; i
++)
6464 maxsize
= merge_extents(super
, extent_cnt
);
6468 minsize
= chunk
* 2;
6470 if (cnt
< raiddisks
||
6471 (super
->orom
&& used
&& used
!= raiddisks
) ||
6472 maxsize
< minsize
||
6474 pr_err("not enough devices with space to create array.\n");
6475 return 0; /* No enough free spaces large enough */
6486 if (!check_env("IMSM_NO_PLATFORM") &&
6487 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6488 pr_err("attempting to create a second volume with size less then remaining space. Aborting...\n");
6492 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6494 dl
->raiddisk
= cnt
++;
6498 dprintf("imsm: imsm_get_free_size() returns : %llu\n", size
);
6503 static int reserve_space(struct supertype
*st
, int raiddisks
,
6504 unsigned long long size
, int chunk
,
6505 unsigned long long *freesize
)
6507 struct intel_super
*super
= st
->sb
;
6512 rv
= imsm_get_free_size(st
, raiddisks
, size
, chunk
, freesize
);
6515 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6517 dl
->raiddisk
= cnt
++;
6524 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
6525 int raiddisks
, int *chunk
, unsigned long long size
,
6526 unsigned long long data_offset
,
6527 char *dev
, unsigned long long *freesize
,
6535 * if given unused devices create a container
6536 * if given given devices in a container create a member volume
6538 if (level
== LEVEL_CONTAINER
) {
6539 /* Must be a fresh device to add to a container */
6540 return validate_geometry_imsm_container(st
, level
, layout
,
6550 struct intel_super
*super
= st
->sb
;
6551 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
6552 raiddisks
, chunk
, size
,
6555 /* we are being asked to automatically layout a
6556 * new volume based on the current contents of
6557 * the container. If the the parameters can be
6558 * satisfied reserve_space will record the disks,
6559 * start offset, and size of the volume to be
6560 * created. add_to_super and getinfo_super
6561 * detect when autolayout is in progress.
6563 /* assuming that freesize is always given when array is
6565 if (super
->orom
&& freesize
) {
6567 count
= count_volumes(super
->hba
,
6568 super
->orom
->dpa
, verbose
);
6569 if (super
->orom
->vphba
<= count
) {
6570 pr_vrb("platform does not support more than %d raid volumes.\n",
6571 super
->orom
->vphba
);
6576 return reserve_space(st
, raiddisks
, size
,
6582 /* creating in a given container */
6583 return validate_geometry_imsm_volume(st
, level
, layout
,
6584 raiddisks
, chunk
, size
,
6586 dev
, freesize
, verbose
);
6589 /* This device needs to be a device in an 'imsm' container */
6590 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
6593 pr_err("Cannot create this array on device %s\n",
6598 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
6600 pr_err("Cannot open %s: %s\n",
6601 dev
, strerror(errno
));
6604 /* Well, it is in use by someone, maybe an 'imsm' container. */
6605 cfd
= open_container(fd
);
6609 pr_err("Cannot use %s: It is busy\n",
6613 sra
= sysfs_read(cfd
, NULL
, GET_VERSION
);
6614 if (sra
&& sra
->array
.major_version
== -1 &&
6615 strcmp(sra
->text_version
, "imsm") == 0)
6619 /* This is a member of a imsm container. Load the container
6620 * and try to create a volume
6622 struct intel_super
*super
;
6624 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
, NULL
, 1) == 0) {
6626 strcpy(st
->container_devnm
, fd2devnm(cfd
));
6628 return validate_geometry_imsm_volume(st
, level
, layout
,
6630 size
, data_offset
, dev
,
6637 pr_err("failed container membership check\n");
6643 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
6645 struct intel_super
*super
= st
->sb
;
6647 if (level
&& *level
== UnSet
)
6648 *level
= LEVEL_CONTAINER
;
6650 if (level
&& layout
&& *layout
== UnSet
)
6651 *layout
= imsm_level_to_layout(*level
);
6653 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0))
6654 *chunk
= imsm_default_chunk(super
->orom
);
6657 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
6659 static int kill_subarray_imsm(struct supertype
*st
)
6661 /* remove the subarray currently referenced by ->current_vol */
6663 struct intel_dev
**dp
;
6664 struct intel_super
*super
= st
->sb
;
6665 __u8 current_vol
= super
->current_vol
;
6666 struct imsm_super
*mpb
= super
->anchor
;
6668 if (super
->current_vol
< 0)
6670 super
->current_vol
= -1; /* invalidate subarray cursor */
6672 /* block deletions that would change the uuid of active subarrays
6674 * FIXME when immutable ids are available, but note that we'll
6675 * also need to fixup the invalidated/active subarray indexes in
6678 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6681 if (i
< current_vol
)
6683 sprintf(subarray
, "%u", i
);
6684 if (is_subarray_active(subarray
, st
->devnm
)) {
6685 pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
6692 if (st
->update_tail
) {
6693 struct imsm_update_kill_array
*u
= xmalloc(sizeof(*u
));
6695 u
->type
= update_kill_array
;
6696 u
->dev_idx
= current_vol
;
6697 append_metadata_update(st
, u
, sizeof(*u
));
6702 for (dp
= &super
->devlist
; *dp
;)
6703 if ((*dp
)->index
== current_vol
) {
6706 handle_missing(super
, (*dp
)->dev
);
6707 if ((*dp
)->index
> current_vol
)
6712 /* no more raid devices, all active components are now spares,
6713 * but of course failed are still failed
6715 if (--mpb
->num_raid_devs
== 0) {
6718 for (d
= super
->disks
; d
; d
= d
->next
)
6723 super
->updates_pending
++;
6728 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
6729 char *update
, struct mddev_ident
*ident
)
6731 /* update the subarray currently referenced by ->current_vol */
6732 struct intel_super
*super
= st
->sb
;
6733 struct imsm_super
*mpb
= super
->anchor
;
6735 if (strcmp(update
, "name") == 0) {
6736 char *name
= ident
->name
;
6740 if (is_subarray_active(subarray
, st
->devnm
)) {
6741 pr_err("Unable to update name of active subarray\n");
6745 if (!check_name(super
, name
, 0))
6748 vol
= strtoul(subarray
, &ep
, 10);
6749 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
6752 if (st
->update_tail
) {
6753 struct imsm_update_rename_array
*u
= xmalloc(sizeof(*u
));
6755 u
->type
= update_rename_array
;
6757 snprintf((char *) u
->name
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6758 append_metadata_update(st
, u
, sizeof(*u
));
6760 struct imsm_dev
*dev
;
6763 dev
= get_imsm_dev(super
, vol
);
6764 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6765 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6766 dev
= get_imsm_dev(super
, i
);
6767 handle_missing(super
, dev
);
6769 super
->updates_pending
++;
6776 #endif /* MDASSEMBLE */
6778 static int is_gen_migration(struct imsm_dev
*dev
)
6783 if (!dev
->vol
.migr_state
)
6786 if (migr_type(dev
) == MIGR_GEN_MIGR
)
6792 static int is_rebuilding(struct imsm_dev
*dev
)
6794 struct imsm_map
*migr_map
;
6796 if (!dev
->vol
.migr_state
)
6799 if (migr_type(dev
) != MIGR_REBUILD
)
6802 migr_map
= get_imsm_map(dev
, MAP_1
);
6804 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
6811 static int is_initializing(struct imsm_dev
*dev
)
6813 struct imsm_map
*migr_map
;
6815 if (!dev
->vol
.migr_state
)
6818 if (migr_type(dev
) != MIGR_INIT
)
6821 migr_map
= get_imsm_map(dev
, MAP_1
);
6823 if (migr_map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
6830 static void update_recovery_start(struct intel_super
*super
,
6831 struct imsm_dev
*dev
,
6832 struct mdinfo
*array
)
6834 struct mdinfo
*rebuild
= NULL
;
6838 if (!is_rebuilding(dev
))
6841 /* Find the rebuild target, but punt on the dual rebuild case */
6842 for (d
= array
->devs
; d
; d
= d
->next
)
6843 if (d
->recovery_start
== 0) {
6850 /* (?) none of the disks are marked with
6851 * IMSM_ORD_REBUILD, so assume they are missing and the
6852 * disk_ord_tbl was not correctly updated
6854 dprintf("failed to locate out-of-sync disk\n");
6858 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
6859 rebuild
->recovery_start
= units
* blocks_per_migr_unit(super
, dev
);
6863 static int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
);
6866 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
6868 /* Given a container loaded by load_super_imsm_all,
6869 * extract information about all the arrays into
6871 * If 'subarray' is given, just extract info about that array.
6873 * For each imsm_dev create an mdinfo, fill it in,
6874 * then look for matching devices in super->disks
6875 * and create appropriate device mdinfo.
6877 struct intel_super
*super
= st
->sb
;
6878 struct imsm_super
*mpb
= super
->anchor
;
6879 struct mdinfo
*rest
= NULL
;
6883 int spare_disks
= 0;
6885 /* do not assemble arrays when not all attributes are supported */
6886 if (imsm_check_attributes(mpb
->attributes
) == 0) {
6888 pr_err("Unsupported attributes in IMSM metadata.Arrays activation is blocked.\n");
6891 /* check for bad blocks */
6892 if (imsm_bbm_log_size(super
->anchor
)) {
6893 pr_err("BBM log found in IMSM metadata.Arrays activation is blocked.\n");
6897 /* count spare devices, not used in maps
6899 for (d
= super
->disks
; d
; d
= d
->next
)
6903 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6904 struct imsm_dev
*dev
;
6905 struct imsm_map
*map
;
6906 struct imsm_map
*map2
;
6907 struct mdinfo
*this;
6915 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
6918 dev
= get_imsm_dev(super
, i
);
6919 map
= get_imsm_map(dev
, MAP_0
);
6920 map2
= get_imsm_map(dev
, MAP_1
);
6922 /* do not publish arrays that are in the middle of an
6923 * unsupported migration
6925 if (dev
->vol
.migr_state
&&
6926 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
6927 pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n",
6931 /* do not publish arrays that are not support by controller's
6935 this = xmalloc(sizeof(*this));
6937 super
->current_vol
= i
;
6938 getinfo_super_imsm_volume(st
, this, NULL
);
6941 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
6942 /* mdadm does not support all metadata features- set the bit in all arrays state */
6943 if (!validate_geometry_imsm_orom(super
,
6944 get_imsm_raid_level(map
), /* RAID level */
6945 imsm_level_to_layout(get_imsm_raid_level(map
)),
6946 map
->num_members
, /* raid disks */
6947 &chunk
, join_u32(dev
->size_low
, dev
->size_high
),
6949 pr_err("IMSM RAID geometry validation failed. Array %s activation is blocked.\n",
6951 this->array
.state
|=
6952 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
6953 (1<<MD_SB_BLOCK_VOLUME
);
6957 /* if array has bad blocks, set suitable bit in all arrays state */
6959 this->array
.state
|=
6960 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
6961 (1<<MD_SB_BLOCK_VOLUME
);
6963 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
6964 unsigned long long recovery_start
;
6965 struct mdinfo
*info_d
;
6972 idx
= get_imsm_disk_idx(dev
, slot
, MAP_0
);
6973 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
6974 for (d
= super
->disks
; d
; d
= d
->next
)
6975 if (d
->index
== idx
)
6978 recovery_start
= MaxSector
;
6981 if (d
&& is_failed(&d
->disk
))
6983 if (ord
& IMSM_ORD_REBUILD
)
6987 * if we skip some disks the array will be assmebled degraded;
6988 * reset resync start to avoid a dirty-degraded
6989 * situation when performing the intial sync
6991 * FIXME handle dirty degraded
6993 if ((skip
|| recovery_start
== 0) && !dev
->vol
.dirty
)
6994 this->resync_start
= MaxSector
;
6998 info_d
= xcalloc(1, sizeof(*info_d
));
6999 info_d
->next
= this->devs
;
7000 this->devs
= info_d
;
7002 info_d
->disk
.number
= d
->index
;
7003 info_d
->disk
.major
= d
->major
;
7004 info_d
->disk
.minor
= d
->minor
;
7005 info_d
->disk
.raid_disk
= slot
;
7006 info_d
->recovery_start
= recovery_start
;
7008 if (slot
< map2
->num_members
)
7009 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
7011 this->array
.spare_disks
++;
7013 if (slot
< map
->num_members
)
7014 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
7016 this->array
.spare_disks
++;
7018 if (info_d
->recovery_start
== MaxSector
)
7019 this->array
.working_disks
++;
7021 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
7022 info_d
->data_offset
= pba_of_lba0(map
);
7023 info_d
->component_size
= blocks_per_member(map
);
7025 /* now that the disk list is up-to-date fixup recovery_start */
7026 update_recovery_start(super
, dev
, this);
7027 this->array
.spare_disks
+= spare_disks
;
7030 /* check for reshape */
7031 if (this->reshape_active
== 1)
7032 recover_backup_imsm(st
, this);
7040 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
7041 int failed
, int look_in_map
)
7043 struct imsm_map
*map
;
7045 map
= get_imsm_map(dev
, look_in_map
);
7048 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
7049 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
7051 switch (get_imsm_raid_level(map
)) {
7053 return IMSM_T_STATE_FAILED
;
7056 if (failed
< map
->num_members
)
7057 return IMSM_T_STATE_DEGRADED
;
7059 return IMSM_T_STATE_FAILED
;
7064 * check to see if any mirrors have failed, otherwise we
7065 * are degraded. Even numbered slots are mirrored on
7069 /* gcc -Os complains that this is unused */
7070 int insync
= insync
;
7072 for (i
= 0; i
< map
->num_members
; i
++) {
7073 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_X
);
7074 int idx
= ord_to_idx(ord
);
7075 struct imsm_disk
*disk
;
7077 /* reset the potential in-sync count on even-numbered
7078 * slots. num_copies is always 2 for imsm raid10
7083 disk
= get_imsm_disk(super
, idx
);
7084 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
7087 /* no in-sync disks left in this mirror the
7091 return IMSM_T_STATE_FAILED
;
7094 return IMSM_T_STATE_DEGRADED
;
7098 return IMSM_T_STATE_DEGRADED
;
7100 return IMSM_T_STATE_FAILED
;
7106 return map
->map_state
;
7109 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
7114 struct imsm_disk
*disk
;
7115 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7116 struct imsm_map
*prev
= get_imsm_map(dev
, MAP_1
);
7117 struct imsm_map
*map_for_loop
;
7122 /* at the beginning of migration we set IMSM_ORD_REBUILD on
7123 * disks that are being rebuilt. New failures are recorded to
7124 * map[0]. So we look through all the disks we started with and
7125 * see if any failures are still present, or if any new ones
7129 if (prev
&& (map
->num_members
< prev
->num_members
))
7130 map_for_loop
= prev
;
7132 for (i
= 0; i
< map_for_loop
->num_members
; i
++) {
7134 /* when MAP_X is passed both maps failures are counted
7137 (look_in_map
== MAP_1
|| look_in_map
== MAP_X
) &&
7138 i
< prev
->num_members
) {
7139 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
7140 idx_1
= ord_to_idx(ord
);
7142 disk
= get_imsm_disk(super
, idx_1
);
7143 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
7146 if ((look_in_map
== MAP_0
|| look_in_map
== MAP_X
) &&
7147 i
< map
->num_members
) {
7148 ord
= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
7149 idx
= ord_to_idx(ord
);
7152 disk
= get_imsm_disk(super
, idx
);
7153 if (!disk
|| is_failed(disk
) ||
7154 ord
& IMSM_ORD_REBUILD
)
7164 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
7167 struct intel_super
*super
= c
->sb
;
7168 struct imsm_super
*mpb
= super
->anchor
;
7170 if (atoi(inst
) >= mpb
->num_raid_devs
) {
7171 pr_err("subarry index %d, out of range\n", atoi(inst
));
7175 dprintf("imsm: open_new %s\n", inst
);
7176 a
->info
.container_member
= atoi(inst
);
7180 static int is_resyncing(struct imsm_dev
*dev
)
7182 struct imsm_map
*migr_map
;
7184 if (!dev
->vol
.migr_state
)
7187 if (migr_type(dev
) == MIGR_INIT
||
7188 migr_type(dev
) == MIGR_REPAIR
)
7191 if (migr_type(dev
) == MIGR_GEN_MIGR
)
7194 migr_map
= get_imsm_map(dev
, MAP_1
);
7196 if (migr_map
->map_state
== IMSM_T_STATE_NORMAL
&&
7197 dev
->vol
.migr_type
!= MIGR_GEN_MIGR
)
7203 /* return true if we recorded new information */
7204 static int mark_failure(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
7208 struct imsm_map
*map
;
7209 char buf
[MAX_RAID_SERIAL_LEN
+3];
7210 unsigned int len
, shift
= 0;
7212 /* new failures are always set in map[0] */
7213 map
= get_imsm_map(dev
, MAP_0
);
7215 slot
= get_imsm_disk_slot(map
, idx
);
7219 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
7220 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
7223 memcpy(buf
, disk
->serial
, MAX_RAID_SERIAL_LEN
);
7224 buf
[MAX_RAID_SERIAL_LEN
] = '\000';
7226 if ((len
= strlen(buf
)) >= MAX_RAID_SERIAL_LEN
)
7227 shift
= len
- MAX_RAID_SERIAL_LEN
+ 1;
7228 strncpy((char *)disk
->serial
, &buf
[shift
], MAX_RAID_SERIAL_LEN
);
7230 disk
->status
|= FAILED_DISK
;
7231 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
7232 /* mark failures in second map if second map exists and this disk
7234 * This is valid for migration, initialization and rebuild
7236 if (dev
->vol
.migr_state
) {
7237 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
7238 int slot2
= get_imsm_disk_slot(map2
, idx
);
7240 if (slot2
< map2
->num_members
&& slot2
>= 0)
7241 set_imsm_ord_tbl_ent(map2
, slot2
,
7242 idx
| IMSM_ORD_REBUILD
);
7244 if (map
->failed_disk_num
== 0xff)
7245 map
->failed_disk_num
= slot
;
7249 static void mark_missing(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
7251 mark_failure(dev
, disk
, idx
);
7253 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
7256 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
7257 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
7260 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
7264 if (!super
->missing
)
7267 /* When orom adds replacement for missing disk it does
7268 * not remove entry of missing disk, but just updates map with
7269 * new added disk. So it is not enough just to test if there is
7270 * any missing disk, we have to look if there are any failed disks
7271 * in map to stop migration */
7273 dprintf("imsm: mark missing\n");
7274 /* end process for initialization and rebuild only
7276 if (is_gen_migration(dev
) == 0) {
7280 failed
= imsm_count_failed(super
, dev
, MAP_0
);
7281 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7284 end_migration(dev
, super
, map_state
);
7286 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
7287 mark_missing(dev
, &dl
->disk
, dl
->index
);
7288 super
->updates_pending
++;
7291 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
,
7294 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
7295 unsigned long long array_blocks
;
7296 struct imsm_map
*map
;
7298 if (used_disks
== 0) {
7299 /* when problems occures
7300 * return current array_blocks value
7302 array_blocks
= __le32_to_cpu(dev
->size_high
);
7303 array_blocks
= array_blocks
<< 32;
7304 array_blocks
+= __le32_to_cpu(dev
->size_low
);
7306 return array_blocks
;
7309 /* set array size in metadata
7311 if (new_size
<= 0) {
7312 /* OLCE size change is caused by added disks
7314 map
= get_imsm_map(dev
, MAP_0
);
7315 array_blocks
= blocks_per_member(map
) * used_disks
;
7317 /* Online Volume Size Change
7318 * Using available free space
7320 array_blocks
= new_size
;
7323 /* round array size down to closest MB
7325 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
7326 dev
->size_low
= __cpu_to_le32((__u32
)array_blocks
);
7327 dev
->size_high
= __cpu_to_le32((__u32
)(array_blocks
>> 32));
7329 return array_blocks
;
7332 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
7334 static void imsm_progress_container_reshape(struct intel_super
*super
)
7336 /* if no device has a migr_state, but some device has a
7337 * different number of members than the previous device, start
7338 * changing the number of devices in this device to match
7341 struct imsm_super
*mpb
= super
->anchor
;
7342 int prev_disks
= -1;
7346 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7347 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
7348 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7349 struct imsm_map
*map2
;
7350 int prev_num_members
;
7352 if (dev
->vol
.migr_state
)
7355 if (prev_disks
== -1)
7356 prev_disks
= map
->num_members
;
7357 if (prev_disks
== map
->num_members
)
7360 /* OK, this array needs to enter reshape mode.
7361 * i.e it needs a migr_state
7364 copy_map_size
= sizeof_imsm_map(map
);
7365 prev_num_members
= map
->num_members
;
7366 map
->num_members
= prev_disks
;
7367 dev
->vol
.migr_state
= 1;
7368 dev
->vol
.curr_migr_unit
= 0;
7369 set_migr_type(dev
, MIGR_GEN_MIGR
);
7370 for (i
= prev_num_members
;
7371 i
< map
->num_members
; i
++)
7372 set_imsm_ord_tbl_ent(map
, i
, i
);
7373 map2
= get_imsm_map(dev
, MAP_1
);
7374 /* Copy the current map */
7375 memcpy(map2
, map
, copy_map_size
);
7376 map2
->num_members
= prev_num_members
;
7378 imsm_set_array_size(dev
, -1);
7379 super
->clean_migration_record_by_mdmon
= 1;
7380 super
->updates_pending
++;
7384 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
7385 * states are handled in imsm_set_disk() with one exception, when a
7386 * resync is stopped due to a new failure this routine will set the
7387 * 'degraded' state for the array.
7389 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
7391 int inst
= a
->info
.container_member
;
7392 struct intel_super
*super
= a
->container
->sb
;
7393 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7394 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7395 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
7396 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7397 __u32 blocks_per_unit
;
7399 if (dev
->vol
.migr_state
&&
7400 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
7401 /* array state change is blocked due to reshape action
7403 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
7404 * - finish the reshape (if last_checkpoint is big and action != reshape)
7405 * - update curr_migr_unit
7407 if (a
->curr_action
== reshape
) {
7408 /* still reshaping, maybe update curr_migr_unit */
7409 goto mark_checkpoint
;
7411 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
7412 /* for some reason we aborted the reshape.
7414 * disable automatic metadata rollback
7415 * user action is required to recover process
7418 struct imsm_map
*map2
=
7419 get_imsm_map(dev
, MAP_1
);
7420 dev
->vol
.migr_state
= 0;
7421 set_migr_type(dev
, 0);
7422 dev
->vol
.curr_migr_unit
= 0;
7424 sizeof_imsm_map(map2
));
7425 super
->updates_pending
++;
7428 if (a
->last_checkpoint
>= a
->info
.component_size
) {
7429 unsigned long long array_blocks
;
7433 used_disks
= imsm_num_data_members(dev
, MAP_0
);
7434 if (used_disks
> 0) {
7436 blocks_per_member(map
) *
7438 /* round array size down to closest MB
7440 array_blocks
= (array_blocks
7441 >> SECT_PER_MB_SHIFT
)
7442 << SECT_PER_MB_SHIFT
;
7443 a
->info
.custom_array_size
= array_blocks
;
7444 /* encourage manager to update array
7448 a
->check_reshape
= 1;
7450 /* finalize online capacity expansion/reshape */
7451 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
7453 mdi
->disk
.raid_disk
,
7456 imsm_progress_container_reshape(super
);
7461 /* before we activate this array handle any missing disks */
7462 if (consistent
== 2)
7463 handle_missing(super
, dev
);
7465 if (consistent
== 2 &&
7466 (!is_resync_complete(&a
->info
) ||
7467 map_state
!= IMSM_T_STATE_NORMAL
||
7468 dev
->vol
.migr_state
))
7471 if (is_resync_complete(&a
->info
)) {
7472 /* complete intialization / resync,
7473 * recovery and interrupted recovery is completed in
7476 if (is_resyncing(dev
)) {
7477 dprintf("imsm: mark resync done\n");
7478 end_migration(dev
, super
, map_state
);
7479 super
->updates_pending
++;
7480 a
->last_checkpoint
= 0;
7482 } else if ((!is_resyncing(dev
) && !failed
) &&
7483 (imsm_reshape_blocks_arrays_changes(super
) == 0)) {
7484 /* mark the start of the init process if nothing is failed */
7485 dprintf("imsm: mark resync start\n");
7486 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
7487 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
7489 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
7490 super
->updates_pending
++;
7494 /* skip checkpointing for general migration,
7495 * it is controlled in mdadm
7497 if (is_gen_migration(dev
))
7498 goto skip_mark_checkpoint
;
7500 /* check if we can update curr_migr_unit from resync_start, recovery_start */
7501 blocks_per_unit
= blocks_per_migr_unit(super
, dev
);
7502 if (blocks_per_unit
) {
7506 units
= a
->last_checkpoint
/ blocks_per_unit
;
7509 /* check that we did not overflow 32-bits, and that
7510 * curr_migr_unit needs updating
7512 if (units32
== units
&&
7514 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
7515 dprintf("imsm: mark checkpoint (%u)\n", units32
);
7516 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
7517 super
->updates_pending
++;
7521 skip_mark_checkpoint
:
7522 /* mark dirty / clean */
7523 if (dev
->vol
.dirty
!= !consistent
) {
7524 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
7529 super
->updates_pending
++;
7535 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
7537 int inst
= a
->info
.container_member
;
7538 struct intel_super
*super
= a
->container
->sb
;
7539 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7540 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7541 struct imsm_disk
*disk
;
7543 int recovery_not_finished
= 0;
7548 if (n
> map
->num_members
)
7549 pr_err("imsm: set_disk %d out of range 0..%d\n",
7550 n
, map
->num_members
- 1);
7555 dprintf("imsm: set_disk %d:%x\n", n
, state
);
7557 ord
= get_imsm_ord_tbl_ent(dev
, n
, MAP_0
);
7558 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
7560 /* check for new failures */
7561 if (state
& DS_FAULTY
) {
7562 if (mark_failure(dev
, disk
, ord_to_idx(ord
)))
7563 super
->updates_pending
++;
7566 /* check if in_sync */
7567 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
7568 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
7570 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
7571 super
->updates_pending
++;
7574 failed
= imsm_count_failed(super
, dev
, MAP_0
);
7575 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7577 /* check if recovery complete, newly degraded, or failed */
7578 dprintf("imsm: Detected transition to state ");
7579 switch (map_state
) {
7580 case IMSM_T_STATE_NORMAL
: /* transition to normal state */
7581 dprintf("normal: ");
7582 if (is_rebuilding(dev
)) {
7583 dprintf_cont("while rebuilding");
7584 /* check if recovery is really finished */
7585 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
7586 if (mdi
->recovery_start
!= MaxSector
) {
7587 recovery_not_finished
= 1;
7590 if (recovery_not_finished
) {
7592 dprintf("Rebuild has not finished yet, state not changed");
7593 if (a
->last_checkpoint
< mdi
->recovery_start
) {
7594 a
->last_checkpoint
= mdi
->recovery_start
;
7595 super
->updates_pending
++;
7599 end_migration(dev
, super
, map_state
);
7600 map
= get_imsm_map(dev
, MAP_0
);
7601 map
->failed_disk_num
= ~0;
7602 super
->updates_pending
++;
7603 a
->last_checkpoint
= 0;
7606 if (is_gen_migration(dev
)) {
7607 dprintf_cont("while general migration");
7608 if (a
->last_checkpoint
>= a
->info
.component_size
)
7609 end_migration(dev
, super
, map_state
);
7611 map
->map_state
= map_state
;
7612 map
= get_imsm_map(dev
, MAP_0
);
7613 map
->failed_disk_num
= ~0;
7614 super
->updates_pending
++;
7618 case IMSM_T_STATE_DEGRADED
: /* transition to degraded state */
7619 dprintf_cont("degraded: ");
7620 if (map
->map_state
!= map_state
&& !dev
->vol
.migr_state
) {
7621 dprintf_cont("mark degraded");
7622 map
->map_state
= map_state
;
7623 super
->updates_pending
++;
7624 a
->last_checkpoint
= 0;
7627 if (is_rebuilding(dev
)) {
7628 dprintf_cont("while rebuilding.");
7629 if (map
->map_state
!= map_state
) {
7630 dprintf_cont(" Map state change");
7631 end_migration(dev
, super
, map_state
);
7632 super
->updates_pending
++;
7636 if (is_gen_migration(dev
)) {
7637 dprintf_cont("while general migration");
7638 if (a
->last_checkpoint
>= a
->info
.component_size
)
7639 end_migration(dev
, super
, map_state
);
7641 map
->map_state
= map_state
;
7642 manage_second_map(super
, dev
);
7644 super
->updates_pending
++;
7647 if (is_initializing(dev
)) {
7648 dprintf_cont("while initialization.");
7649 map
->map_state
= map_state
;
7650 super
->updates_pending
++;
7654 case IMSM_T_STATE_FAILED
: /* transition to failed state */
7655 dprintf_cont("failed: ");
7656 if (is_gen_migration(dev
)) {
7657 dprintf_cont("while general migration");
7658 map
->map_state
= map_state
;
7659 super
->updates_pending
++;
7662 if (map
->map_state
!= map_state
) {
7663 dprintf_cont("mark failed");
7664 end_migration(dev
, super
, map_state
);
7665 super
->updates_pending
++;
7666 a
->last_checkpoint
= 0;
7671 dprintf_cont("state %i\n", map_state
);
7676 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
7679 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
7680 unsigned long long dsize
;
7681 unsigned long long sectors
;
7682 unsigned int sector_size
;
7684 get_dev_sector_size(fd
, NULL
, §or_size
);
7685 get_dev_size(fd
, NULL
, &dsize
);
7687 if (mpb_size
> sector_size
) {
7688 /* -1 to account for anchor */
7689 sectors
= mpb_sectors(mpb
, sector_size
) - 1;
7691 /* write the extended mpb to the sectors preceeding the anchor */
7692 if (lseek64(fd
, dsize
- (sector_size
* (2 + sectors
)),
7696 if ((unsigned long long)write(fd
, buf
+ sector_size
,
7697 sector_size
* sectors
) != sector_size
* sectors
)
7701 /* first block is stored on second to last sector of the disk */
7702 if (lseek64(fd
, dsize
- (sector_size
* 2), SEEK_SET
) < 0)
7705 if (write(fd
, buf
, sector_size
) != sector_size
)
7711 static void imsm_sync_metadata(struct supertype
*container
)
7713 struct intel_super
*super
= container
->sb
;
7715 dprintf("sync metadata: %d\n", super
->updates_pending
);
7716 if (!super
->updates_pending
)
7719 write_super_imsm(container
, 0);
7721 super
->updates_pending
= 0;
7724 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
7726 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7727 int i
= get_imsm_disk_idx(dev
, idx
, MAP_X
);
7730 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7734 if (dl
&& is_failed(&dl
->disk
))
7738 dprintf("found %x:%x\n", dl
->major
, dl
->minor
);
7743 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
7744 struct active_array
*a
, int activate_new
,
7745 struct mdinfo
*additional_test_list
)
7747 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7748 int idx
= get_imsm_disk_idx(dev
, slot
, MAP_X
);
7749 struct imsm_super
*mpb
= super
->anchor
;
7750 struct imsm_map
*map
;
7751 unsigned long long pos
;
7756 __u32 array_start
= 0;
7757 __u32 array_end
= 0;
7759 struct mdinfo
*test_list
;
7761 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7762 /* If in this array, skip */
7763 for (d
= a
->info
.devs
; d
; d
= d
->next
)
7764 if (d
->state_fd
>= 0 &&
7765 d
->disk
.major
== dl
->major
&&
7766 d
->disk
.minor
== dl
->minor
) {
7767 dprintf("%x:%x already in array\n",
7768 dl
->major
, dl
->minor
);
7773 test_list
= additional_test_list
;
7775 if (test_list
->disk
.major
== dl
->major
&&
7776 test_list
->disk
.minor
== dl
->minor
) {
7777 dprintf("%x:%x already in additional test list\n",
7778 dl
->major
, dl
->minor
);
7781 test_list
= test_list
->next
;
7786 /* skip in use or failed drives */
7787 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
7789 dprintf("%x:%x status (failed: %d index: %d)\n",
7790 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
7794 /* skip pure spares when we are looking for partially
7795 * assimilated drives
7797 if (dl
->index
== -1 && !activate_new
)
7800 /* Does this unused device have the requisite free space?
7801 * It needs to be able to cover all member volumes
7803 ex
= get_extents(super
, dl
);
7805 dprintf("cannot get extents\n");
7808 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7809 dev
= get_imsm_dev(super
, i
);
7810 map
= get_imsm_map(dev
, MAP_0
);
7812 /* check if this disk is already a member of
7815 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
7821 array_start
= pba_of_lba0(map
);
7822 array_end
= array_start
+
7823 blocks_per_member(map
) - 1;
7826 /* check that we can start at pba_of_lba0 with
7827 * blocks_per_member of space
7829 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
7833 pos
= ex
[j
].start
+ ex
[j
].size
;
7835 } while (ex
[j
-1].size
);
7842 if (i
< mpb
->num_raid_devs
) {
7843 dprintf("%x:%x does not have %u to %u available\n",
7844 dl
->major
, dl
->minor
, array_start
, array_end
);
7854 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
7856 struct imsm_dev
*dev2
;
7857 struct imsm_map
*map
;
7863 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
7865 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
, MAP_0
);
7866 if (state
== IMSM_T_STATE_FAILED
) {
7867 map
= get_imsm_map(dev2
, MAP_0
);
7870 for (slot
= 0; slot
< map
->num_members
; slot
++) {
7872 * Check if failed disks are deleted from intel
7873 * disk list or are marked to be deleted
7875 idx
= get_imsm_disk_idx(dev2
, slot
, MAP_X
);
7876 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
7878 * Do not rebuild the array if failed disks
7879 * from failed sub-array are not removed from
7883 is_failed(&idisk
->disk
) &&
7884 (idisk
->action
!= DISK_REMOVE
))
7892 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
7893 struct metadata_update
**updates
)
7896 * Find a device with unused free space and use it to replace a
7897 * failed/vacant region in an array. We replace failed regions one a
7898 * array at a time. The result is that a new spare disk will be added
7899 * to the first failed array and after the monitor has finished
7900 * propagating failures the remainder will be consumed.
7902 * FIXME add a capability for mdmon to request spares from another
7906 struct intel_super
*super
= a
->container
->sb
;
7907 int inst
= a
->info
.container_member
;
7908 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7909 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7910 int failed
= a
->info
.array
.raid_disks
;
7911 struct mdinfo
*rv
= NULL
;
7914 struct metadata_update
*mu
;
7916 struct imsm_update_activate_spare
*u
;
7921 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
7922 if ((d
->curr_state
& DS_FAULTY
) &&
7924 /* wait for Removal to happen */
7926 if (d
->state_fd
>= 0)
7930 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
7931 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
7933 if (imsm_reshape_blocks_arrays_changes(super
))
7936 /* Cannot activate another spare if rebuild is in progress already
7938 if (is_rebuilding(dev
)) {
7939 dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n");
7943 if (a
->info
.array
.level
== 4)
7944 /* No repair for takeovered array
7945 * imsm doesn't support raid4
7949 if (imsm_check_degraded(super
, dev
, failed
, MAP_0
) !=
7950 IMSM_T_STATE_DEGRADED
)
7953 if (get_imsm_map(dev
, MAP_0
)->map_state
== IMSM_T_STATE_UNINITIALIZED
) {
7954 dprintf("imsm: No spare activation allowed. Volume is not initialized.\n");
7959 * If there are any failed disks check state of the other volume.
7960 * Block rebuild if the another one is failed until failed disks
7961 * are removed from container.
7964 dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n",
7965 MAX_RAID_SERIAL_LEN
, dev
->volume
);
7966 /* check if states of the other volumes allow for rebuild */
7967 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
7969 allowed
= imsm_rebuild_allowed(a
->container
,
7977 /* For each slot, if it is not working, find a spare */
7978 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
7979 for (d
= a
->info
.devs
; d
; d
= d
->next
)
7980 if (d
->disk
.raid_disk
== i
)
7982 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
7983 if (d
&& (d
->state_fd
>= 0))
7987 * OK, this device needs recovery. Try to re-add the
7988 * previous occupant of this slot, if this fails see if
7989 * we can continue the assimilation of a spare that was
7990 * partially assimilated, finally try to activate a new
7993 dl
= imsm_readd(super
, i
, a
);
7995 dl
= imsm_add_spare(super
, i
, a
, 0, rv
);
7997 dl
= imsm_add_spare(super
, i
, a
, 1, rv
);
8001 /* found a usable disk with enough space */
8002 di
= xcalloc(1, sizeof(*di
));
8004 /* dl->index will be -1 in the case we are activating a
8005 * pristine spare. imsm_process_update() will create a
8006 * new index in this case. Once a disk is found to be
8007 * failed in all member arrays it is kicked from the
8010 di
->disk
.number
= dl
->index
;
8012 /* (ab)use di->devs to store a pointer to the device
8015 di
->devs
= (struct mdinfo
*) dl
;
8017 di
->disk
.raid_disk
= i
;
8018 di
->disk
.major
= dl
->major
;
8019 di
->disk
.minor
= dl
->minor
;
8021 di
->recovery_start
= 0;
8022 di
->data_offset
= pba_of_lba0(map
);
8023 di
->component_size
= a
->info
.component_size
;
8024 di
->container_member
= inst
;
8025 super
->random
= random32();
8029 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
8030 i
, di
->data_offset
);
8034 /* No spares found */
8036 /* Now 'rv' has a list of devices to return.
8037 * Create a metadata_update record to update the
8038 * disk_ord_tbl for the array
8040 mu
= xmalloc(sizeof(*mu
));
8041 mu
->buf
= xcalloc(num_spares
,
8042 sizeof(struct imsm_update_activate_spare
));
8044 mu
->space_list
= NULL
;
8045 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
8046 mu
->next
= *updates
;
8047 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
8049 for (di
= rv
; di
; di
= di
->next
) {
8050 u
->type
= update_activate_spare
;
8051 u
->dl
= (struct dl
*) di
->devs
;
8053 u
->slot
= di
->disk
.raid_disk
;
8064 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
8066 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
8067 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8068 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, MAP_0
);
8069 struct disk_info
*inf
= get_disk_info(u
);
8070 struct imsm_disk
*disk
;
8074 for (i
= 0; i
< map
->num_members
; i
++) {
8075 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, MAP_X
));
8076 for (j
= 0; j
< new_map
->num_members
; j
++)
8077 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
8084 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
8088 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
8089 if (dl
->major
== major
&& dl
->minor
== minor
)
8094 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
8100 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
8101 if (dl
->major
== major
&& dl
->minor
== minor
) {
8104 prev
->next
= dl
->next
;
8106 super
->disks
= dl
->next
;
8108 __free_imsm_disk(dl
);
8109 dprintf("removed %x:%x\n", major
, minor
);
8117 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
8119 static int add_remove_disk_update(struct intel_super
*super
)
8121 int check_degraded
= 0;
8124 /* add/remove some spares to/from the metadata/contrainer */
8125 while (super
->disk_mgmt_list
) {
8126 struct dl
*disk_cfg
;
8128 disk_cfg
= super
->disk_mgmt_list
;
8129 super
->disk_mgmt_list
= disk_cfg
->next
;
8130 disk_cfg
->next
= NULL
;
8132 if (disk_cfg
->action
== DISK_ADD
) {
8133 disk_cfg
->next
= super
->disks
;
8134 super
->disks
= disk_cfg
;
8136 dprintf("added %x:%x\n",
8137 disk_cfg
->major
, disk_cfg
->minor
);
8138 } else if (disk_cfg
->action
== DISK_REMOVE
) {
8139 dprintf("Disk remove action processed: %x.%x\n",
8140 disk_cfg
->major
, disk_cfg
->minor
);
8141 disk
= get_disk_super(super
,
8145 /* store action status */
8146 disk
->action
= DISK_REMOVE
;
8147 /* remove spare disks only */
8148 if (disk
->index
== -1) {
8149 remove_disk_super(super
,
8154 /* release allocate disk structure */
8155 __free_imsm_disk(disk_cfg
);
8158 return check_degraded
;
8161 static int apply_reshape_migration_update(struct imsm_update_reshape_migration
*u
,
8162 struct intel_super
*super
,
8165 struct intel_dev
*id
;
8166 void **tofree
= NULL
;
8169 dprintf("(enter)\n");
8170 if (u
->subdev
< 0 || u
->subdev
> 1) {
8171 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
8174 if (space_list
== NULL
|| *space_list
== NULL
) {
8175 dprintf("imsm: Error: Memory is not allocated\n");
8179 for (id
= super
->devlist
; id
; id
= id
->next
) {
8180 if (id
->index
== (unsigned)u
->subdev
) {
8181 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
8182 struct imsm_map
*map
;
8183 struct imsm_dev
*new_dev
=
8184 (struct imsm_dev
*)*space_list
;
8185 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
8187 struct dl
*new_disk
;
8189 if (new_dev
== NULL
)
8191 *space_list
= **space_list
;
8192 memcpy(new_dev
, dev
, sizeof_imsm_dev(dev
, 0));
8193 map
= get_imsm_map(new_dev
, MAP_0
);
8195 dprintf("imsm: Error: migration in progress");
8199 to_state
= map
->map_state
;
8200 if ((u
->new_level
== 5) && (map
->raid_level
== 0)) {
8202 /* this should not happen */
8203 if (u
->new_disks
[0] < 0) {
8204 map
->failed_disk_num
=
8205 map
->num_members
- 1;
8206 to_state
= IMSM_T_STATE_DEGRADED
;
8208 to_state
= IMSM_T_STATE_NORMAL
;
8210 migrate(new_dev
, super
, to_state
, MIGR_GEN_MIGR
);
8211 if (u
->new_level
> -1)
8212 map
->raid_level
= u
->new_level
;
8213 migr_map
= get_imsm_map(new_dev
, MAP_1
);
8214 if ((u
->new_level
== 5) &&
8215 (migr_map
->raid_level
== 0)) {
8216 int ord
= map
->num_members
- 1;
8217 migr_map
->num_members
--;
8218 if (u
->new_disks
[0] < 0)
8219 ord
|= IMSM_ORD_REBUILD
;
8220 set_imsm_ord_tbl_ent(map
,
8221 map
->num_members
- 1,
8225 tofree
= (void **)dev
;
8227 /* update chunk size
8229 if (u
->new_chunksize
> 0)
8230 map
->blocks_per_strip
=
8231 __cpu_to_le16(u
->new_chunksize
* 2);
8235 if (u
->new_level
!= 5 || migr_map
->raid_level
!= 0 ||
8236 migr_map
->raid_level
== map
->raid_level
)
8239 if (u
->new_disks
[0] >= 0) {
8242 new_disk
= get_disk_super(super
,
8243 major(u
->new_disks
[0]),
8244 minor(u
->new_disks
[0]));
8245 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
8246 major(u
->new_disks
[0]),
8247 minor(u
->new_disks
[0]),
8248 new_disk
, new_disk
->index
);
8249 if (new_disk
== NULL
)
8250 goto error_disk_add
;
8252 new_disk
->index
= map
->num_members
- 1;
8253 /* slot to fill in autolayout
8255 new_disk
->raiddisk
= new_disk
->index
;
8256 new_disk
->disk
.status
|= CONFIGURED_DISK
;
8257 new_disk
->disk
.status
&= ~SPARE_DISK
;
8259 goto error_disk_add
;
8262 *tofree
= *space_list
;
8263 /* calculate new size
8265 imsm_set_array_size(new_dev
, -1);
8272 *space_list
= tofree
;
8276 dprintf("Error: imsm: Cannot find disk.\n");
8280 static int apply_size_change_update(struct imsm_update_size_change
*u
,
8281 struct intel_super
*super
)
8283 struct intel_dev
*id
;
8286 dprintf("(enter)\n");
8287 if (u
->subdev
< 0 || u
->subdev
> 1) {
8288 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
8292 for (id
= super
->devlist
; id
; id
= id
->next
) {
8293 if (id
->index
== (unsigned)u
->subdev
) {
8294 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
8295 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8296 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
8297 unsigned long long blocks_per_member
;
8299 /* calculate new size
8301 blocks_per_member
= u
->new_size
/ used_disks
;
8302 dprintf("(size: %llu, blocks per member: %llu)\n",
8303 u
->new_size
, blocks_per_member
);
8304 set_blocks_per_member(map
, blocks_per_member
);
8305 imsm_set_array_size(dev
, u
->new_size
);
8315 static int apply_update_activate_spare(struct imsm_update_activate_spare
*u
,
8316 struct intel_super
*super
,
8317 struct active_array
*active_array
)
8319 struct imsm_super
*mpb
= super
->anchor
;
8320 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
8321 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8322 struct imsm_map
*migr_map
;
8323 struct active_array
*a
;
8324 struct imsm_disk
*disk
;
8331 int second_map_created
= 0;
8333 for (; u
; u
= u
->next
) {
8334 victim
= get_imsm_disk_idx(dev
, u
->slot
, MAP_X
);
8339 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
8344 pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n",
8349 /* count failures (excluding rebuilds and the victim)
8350 * to determine map[0] state
8353 for (i
= 0; i
< map
->num_members
; i
++) {
8356 disk
= get_imsm_disk(super
,
8357 get_imsm_disk_idx(dev
, i
, MAP_X
));
8358 if (!disk
|| is_failed(disk
))
8362 /* adding a pristine spare, assign a new index */
8363 if (dl
->index
< 0) {
8364 dl
->index
= super
->anchor
->num_disks
;
8365 super
->anchor
->num_disks
++;
8368 disk
->status
|= CONFIGURED_DISK
;
8369 disk
->status
&= ~SPARE_DISK
;
8372 to_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8373 if (!second_map_created
) {
8374 second_map_created
= 1;
8375 map
->map_state
= IMSM_T_STATE_DEGRADED
;
8376 migrate(dev
, super
, to_state
, MIGR_REBUILD
);
8378 map
->map_state
= to_state
;
8379 migr_map
= get_imsm_map(dev
, MAP_1
);
8380 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
8381 set_imsm_ord_tbl_ent(migr_map
, u
->slot
,
8382 dl
->index
| IMSM_ORD_REBUILD
);
8384 /* update the family_num to mark a new container
8385 * generation, being careful to record the existing
8386 * family_num in orig_family_num to clean up after
8387 * earlier mdadm versions that neglected to set it.
8389 if (mpb
->orig_family_num
== 0)
8390 mpb
->orig_family_num
= mpb
->family_num
;
8391 mpb
->family_num
+= super
->random
;
8393 /* count arrays using the victim in the metadata */
8395 for (a
= active_array
; a
; a
= a
->next
) {
8396 dev
= get_imsm_dev(super
, a
->info
.container_member
);
8397 map
= get_imsm_map(dev
, MAP_0
);
8399 if (get_imsm_disk_slot(map
, victim
) >= 0)
8403 /* delete the victim if it is no longer being
8409 /* We know that 'manager' isn't touching anything,
8410 * so it is safe to delete
8412 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
8413 if ((*dlp
)->index
== victim
)
8416 /* victim may be on the missing list */
8418 for (dlp
= &super
->missing
; *dlp
;
8419 dlp
= &(*dlp
)->next
)
8420 if ((*dlp
)->index
== victim
)
8422 imsm_delete(super
, dlp
, victim
);
8429 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
8430 struct intel_super
*super
,
8433 struct dl
*new_disk
;
8434 struct intel_dev
*id
;
8436 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
8437 int disk_count
= u
->old_raid_disks
;
8438 void **tofree
= NULL
;
8439 int devices_to_reshape
= 1;
8440 struct imsm_super
*mpb
= super
->anchor
;
8442 unsigned int dev_id
;
8444 dprintf("(enter)\n");
8446 /* enable spares to use in array */
8447 for (i
= 0; i
< delta_disks
; i
++) {
8448 new_disk
= get_disk_super(super
,
8449 major(u
->new_disks
[i
]),
8450 minor(u
->new_disks
[i
]));
8451 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
8452 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
8453 new_disk
, new_disk
->index
);
8454 if (new_disk
== NULL
||
8455 (new_disk
->index
>= 0 &&
8456 new_disk
->index
< u
->old_raid_disks
))
8457 goto update_reshape_exit
;
8458 new_disk
->index
= disk_count
++;
8459 /* slot to fill in autolayout
8461 new_disk
->raiddisk
= new_disk
->index
;
8462 new_disk
->disk
.status
|=
8464 new_disk
->disk
.status
&= ~SPARE_DISK
;
8467 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
8468 mpb
->num_raid_devs
);
8469 /* manage changes in volume
8471 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
8472 void **sp
= *space_list
;
8473 struct imsm_dev
*newdev
;
8474 struct imsm_map
*newmap
, *oldmap
;
8476 for (id
= super
->devlist
; id
; id
= id
->next
) {
8477 if (id
->index
== dev_id
)
8486 /* Copy the dev, but not (all of) the map */
8487 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
8488 oldmap
= get_imsm_map(id
->dev
, MAP_0
);
8489 newmap
= get_imsm_map(newdev
, MAP_0
);
8490 /* Copy the current map */
8491 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8492 /* update one device only
8494 if (devices_to_reshape
) {
8495 dprintf("imsm: modifying subdev: %i\n",
8497 devices_to_reshape
--;
8498 newdev
->vol
.migr_state
= 1;
8499 newdev
->vol
.curr_migr_unit
= 0;
8500 set_migr_type(newdev
, MIGR_GEN_MIGR
);
8501 newmap
->num_members
= u
->new_raid_disks
;
8502 for (i
= 0; i
< delta_disks
; i
++) {
8503 set_imsm_ord_tbl_ent(newmap
,
8504 u
->old_raid_disks
+ i
,
8505 u
->old_raid_disks
+ i
);
8507 /* New map is correct, now need to save old map
8509 newmap
= get_imsm_map(newdev
, MAP_1
);
8510 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8512 imsm_set_array_size(newdev
, -1);
8515 sp
= (void **)id
->dev
;
8520 /* Clear migration record */
8521 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
8524 *space_list
= tofree
;
8527 update_reshape_exit
:
8532 static int apply_takeover_update(struct imsm_update_takeover
*u
,
8533 struct intel_super
*super
,
8536 struct imsm_dev
*dev
= NULL
;
8537 struct intel_dev
*dv
;
8538 struct imsm_dev
*dev_new
;
8539 struct imsm_map
*map
;
8543 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
8544 if (dv
->index
== (unsigned int)u
->subarray
) {
8552 map
= get_imsm_map(dev
, MAP_0
);
8554 if (u
->direction
== R10_TO_R0
) {
8555 /* Number of failed disks must be half of initial disk number */
8556 if (imsm_count_failed(super
, dev
, MAP_0
) !=
8557 (map
->num_members
/ 2))
8560 /* iterate through devices to mark removed disks as spare */
8561 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8562 if (dm
->disk
.status
& FAILED_DISK
) {
8563 int idx
= dm
->index
;
8564 /* update indexes on the disk list */
8565 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
8566 the index values will end up being correct.... NB */
8567 for (du
= super
->disks
; du
; du
= du
->next
)
8568 if (du
->index
> idx
)
8570 /* mark as spare disk */
8575 map
->num_members
= map
->num_members
/ 2;
8576 map
->map_state
= IMSM_T_STATE_NORMAL
;
8577 map
->num_domains
= 1;
8578 map
->raid_level
= 0;
8579 map
->failed_disk_num
= -1;
8582 if (u
->direction
== R0_TO_R10
) {
8584 /* update slots in current disk list */
8585 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8589 /* create new *missing* disks */
8590 for (i
= 0; i
< map
->num_members
; i
++) {
8591 space
= *space_list
;
8594 *space_list
= *space
;
8596 memcpy(du
, super
->disks
, sizeof(*du
));
8600 du
->index
= (i
* 2) + 1;
8601 sprintf((char *)du
->disk
.serial
,
8602 " MISSING_%d", du
->index
);
8603 sprintf((char *)du
->serial
,
8604 "MISSING_%d", du
->index
);
8605 du
->next
= super
->missing
;
8606 super
->missing
= du
;
8608 /* create new dev and map */
8609 space
= *space_list
;
8612 *space_list
= *space
;
8613 dev_new
= (void *)space
;
8614 memcpy(dev_new
, dev
, sizeof(*dev
));
8615 /* update new map */
8616 map
= get_imsm_map(dev_new
, MAP_0
);
8617 map
->num_members
= map
->num_members
* 2;
8618 map
->map_state
= IMSM_T_STATE_DEGRADED
;
8619 map
->num_domains
= 2;
8620 map
->raid_level
= 1;
8621 /* replace dev<->dev_new */
8624 /* update disk order table */
8625 for (du
= super
->disks
; du
; du
= du
->next
)
8627 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8628 for (du
= super
->missing
; du
; du
= du
->next
)
8629 if (du
->index
>= 0) {
8630 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8631 mark_missing(dv
->dev
, &du
->disk
, du
->index
);
8637 static void imsm_process_update(struct supertype
*st
,
8638 struct metadata_update
*update
)
8641 * crack open the metadata_update envelope to find the update record
8642 * update can be one of:
8643 * update_reshape_container_disks - all the arrays in the container
8644 * are being reshaped to have more devices. We need to mark
8645 * the arrays for general migration and convert selected spares
8646 * into active devices.
8647 * update_activate_spare - a spare device has replaced a failed
8648 * device in an array, update the disk_ord_tbl. If this disk is
8649 * present in all member arrays then also clear the SPARE_DISK
8651 * update_create_array
8653 * update_rename_array
8654 * update_add_remove_disk
8656 struct intel_super
*super
= st
->sb
;
8657 struct imsm_super
*mpb
;
8658 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
8660 /* update requires a larger buf but the allocation failed */
8661 if (super
->next_len
&& !super
->next_buf
) {
8662 super
->next_len
= 0;
8666 if (super
->next_buf
) {
8667 memcpy(super
->next_buf
, super
->buf
, super
->len
);
8669 super
->len
= super
->next_len
;
8670 super
->buf
= super
->next_buf
;
8672 super
->next_len
= 0;
8673 super
->next_buf
= NULL
;
8676 mpb
= super
->anchor
;
8679 case update_general_migration_checkpoint
: {
8680 struct intel_dev
*id
;
8681 struct imsm_update_general_migration_checkpoint
*u
=
8682 (void *)update
->buf
;
8684 dprintf("called for update_general_migration_checkpoint\n");
8686 /* find device under general migration */
8687 for (id
= super
->devlist
; id
; id
= id
->next
) {
8688 if (is_gen_migration(id
->dev
)) {
8689 id
->dev
->vol
.curr_migr_unit
=
8690 __cpu_to_le32(u
->curr_migr_unit
);
8691 super
->updates_pending
++;
8696 case update_takeover
: {
8697 struct imsm_update_takeover
*u
= (void *)update
->buf
;
8698 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
8699 imsm_update_version_info(super
);
8700 super
->updates_pending
++;
8705 case update_reshape_container_disks
: {
8706 struct imsm_update_reshape
*u
= (void *)update
->buf
;
8707 if (apply_reshape_container_disks_update(
8708 u
, super
, &update
->space_list
))
8709 super
->updates_pending
++;
8712 case update_reshape_migration
: {
8713 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
8714 if (apply_reshape_migration_update(
8715 u
, super
, &update
->space_list
))
8716 super
->updates_pending
++;
8719 case update_size_change
: {
8720 struct imsm_update_size_change
*u
= (void *)update
->buf
;
8721 if (apply_size_change_update(u
, super
))
8722 super
->updates_pending
++;
8725 case update_activate_spare
: {
8726 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
8727 if (apply_update_activate_spare(u
, super
, st
->arrays
))
8728 super
->updates_pending
++;
8731 case update_create_array
: {
8732 /* someone wants to create a new array, we need to be aware of
8733 * a few races/collisions:
8734 * 1/ 'Create' called by two separate instances of mdadm
8735 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
8736 * devices that have since been assimilated via
8738 * In the event this update can not be carried out mdadm will
8739 * (FIX ME) notice that its update did not take hold.
8741 struct imsm_update_create_array
*u
= (void *) update
->buf
;
8742 struct intel_dev
*dv
;
8743 struct imsm_dev
*dev
;
8744 struct imsm_map
*map
, *new_map
;
8745 unsigned long long start
, end
;
8746 unsigned long long new_start
, new_end
;
8748 struct disk_info
*inf
;
8751 /* handle racing creates: first come first serve */
8752 if (u
->dev_idx
< mpb
->num_raid_devs
) {
8753 dprintf("subarray %d already defined\n", u
->dev_idx
);
8757 /* check update is next in sequence */
8758 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
8759 dprintf("can not create array %d expected index %d\n",
8760 u
->dev_idx
, mpb
->num_raid_devs
);
8764 new_map
= get_imsm_map(&u
->dev
, MAP_0
);
8765 new_start
= pba_of_lba0(new_map
);
8766 new_end
= new_start
+ blocks_per_member(new_map
);
8767 inf
= get_disk_info(u
);
8769 /* handle activate_spare versus create race:
8770 * check to make sure that overlapping arrays do not include
8773 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8774 dev
= get_imsm_dev(super
, i
);
8775 map
= get_imsm_map(dev
, MAP_0
);
8776 start
= pba_of_lba0(map
);
8777 end
= start
+ blocks_per_member(map
);
8778 if ((new_start
>= start
&& new_start
<= end
) ||
8779 (start
>= new_start
&& start
<= new_end
))
8784 if (disks_overlap(super
, i
, u
)) {
8785 dprintf("arrays overlap\n");
8790 /* check that prepare update was successful */
8791 if (!update
->space
) {
8792 dprintf("prepare update failed\n");
8796 /* check that all disks are still active before committing
8797 * changes. FIXME: could we instead handle this by creating a
8798 * degraded array? That's probably not what the user expects,
8799 * so better to drop this update on the floor.
8801 for (i
= 0; i
< new_map
->num_members
; i
++) {
8802 dl
= serial_to_dl(inf
[i
].serial
, super
);
8804 dprintf("disk disappeared\n");
8809 super
->updates_pending
++;
8811 /* convert spares to members and fixup ord_tbl */
8812 for (i
= 0; i
< new_map
->num_members
; i
++) {
8813 dl
= serial_to_dl(inf
[i
].serial
, super
);
8814 if (dl
->index
== -1) {
8815 dl
->index
= mpb
->num_disks
;
8817 dl
->disk
.status
|= CONFIGURED_DISK
;
8818 dl
->disk
.status
&= ~SPARE_DISK
;
8820 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
8825 update
->space
= NULL
;
8826 imsm_copy_dev(dev
, &u
->dev
);
8827 dv
->index
= u
->dev_idx
;
8828 dv
->next
= super
->devlist
;
8829 super
->devlist
= dv
;
8830 mpb
->num_raid_devs
++;
8832 imsm_update_version_info(super
);
8835 /* mdmon knows how to release update->space, but not
8836 * ((struct intel_dev *) update->space)->dev
8838 if (update
->space
) {
8844 case update_kill_array
: {
8845 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
8846 int victim
= u
->dev_idx
;
8847 struct active_array
*a
;
8848 struct intel_dev
**dp
;
8849 struct imsm_dev
*dev
;
8851 /* sanity check that we are not affecting the uuid of
8852 * active arrays, or deleting an active array
8854 * FIXME when immutable ids are available, but note that
8855 * we'll also need to fixup the invalidated/active
8856 * subarray indexes in mdstat
8858 for (a
= st
->arrays
; a
; a
= a
->next
)
8859 if (a
->info
.container_member
>= victim
)
8861 /* by definition if mdmon is running at least one array
8862 * is active in the container, so checking
8863 * mpb->num_raid_devs is just extra paranoia
8865 dev
= get_imsm_dev(super
, victim
);
8866 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
8867 dprintf("failed to delete subarray-%d\n", victim
);
8871 for (dp
= &super
->devlist
; *dp
;)
8872 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
8875 if ((*dp
)->index
> (unsigned)victim
)
8879 mpb
->num_raid_devs
--;
8880 super
->updates_pending
++;
8883 case update_rename_array
: {
8884 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
8885 char name
[MAX_RAID_SERIAL_LEN
+1];
8886 int target
= u
->dev_idx
;
8887 struct active_array
*a
;
8888 struct imsm_dev
*dev
;
8890 /* sanity check that we are not affecting the uuid of
8893 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
8894 name
[MAX_RAID_SERIAL_LEN
] = '\0';
8895 for (a
= st
->arrays
; a
; a
= a
->next
)
8896 if (a
->info
.container_member
== target
)
8898 dev
= get_imsm_dev(super
, u
->dev_idx
);
8899 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
8900 dprintf("failed to rename subarray-%d\n", target
);
8904 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
8905 super
->updates_pending
++;
8908 case update_add_remove_disk
: {
8909 /* we may be able to repair some arrays if disks are
8910 * being added, check the status of add_remove_disk
8911 * if discs has been added.
8913 if (add_remove_disk_update(super
)) {
8914 struct active_array
*a
;
8916 super
->updates_pending
++;
8917 for (a
= st
->arrays
; a
; a
= a
->next
)
8918 a
->check_degraded
= 1;
8923 pr_err("error: unsuported process update type:(type: %d)\n", type
);
8927 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
);
8929 static int imsm_prepare_update(struct supertype
*st
,
8930 struct metadata_update
*update
)
8933 * Allocate space to hold new disk entries, raid-device entries or a new
8934 * mpb if necessary. The manager synchronously waits for updates to
8935 * complete in the monitor, so new mpb buffers allocated here can be
8936 * integrated by the monitor thread without worrying about live pointers
8937 * in the manager thread.
8939 enum imsm_update_type type
;
8940 struct intel_super
*super
= st
->sb
;
8941 unsigned int sector_size
= super
->sector_size
;
8942 struct imsm_super
*mpb
= super
->anchor
;
8946 if (update
->len
< (int)sizeof(type
))
8949 type
= *(enum imsm_update_type
*) update
->buf
;
8952 case update_general_migration_checkpoint
:
8953 if (update
->len
< (int)sizeof(struct imsm_update_general_migration_checkpoint
))
8955 dprintf("called for update_general_migration_checkpoint\n");
8957 case update_takeover
: {
8958 struct imsm_update_takeover
*u
= (void *)update
->buf
;
8959 if (update
->len
< (int)sizeof(*u
))
8961 if (u
->direction
== R0_TO_R10
) {
8962 void **tail
= (void **)&update
->space_list
;
8963 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
8964 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8965 int num_members
= map
->num_members
;
8968 /* allocate memory for added disks */
8969 for (i
= 0; i
< num_members
; i
++) {
8970 size
= sizeof(struct dl
);
8971 space
= xmalloc(size
);
8976 /* allocate memory for new device */
8977 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
8978 (num_members
* sizeof(__u32
));
8979 space
= xmalloc(size
);
8983 len
= disks_to_mpb_size(num_members
* 2);
8988 case update_reshape_container_disks
: {
8989 /* Every raid device in the container is about to
8990 * gain some more devices, and we will enter a
8992 * So each 'imsm_map' will be bigger, and the imsm_vol
8993 * will now hold 2 of them.
8994 * Thus we need new 'struct imsm_dev' allocations sized
8995 * as sizeof_imsm_dev but with more devices in both maps.
8997 struct imsm_update_reshape
*u
= (void *)update
->buf
;
8998 struct intel_dev
*dl
;
8999 void **space_tail
= (void**)&update
->space_list
;
9001 if (update
->len
< (int)sizeof(*u
))
9004 dprintf("for update_reshape\n");
9006 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
9007 int size
= sizeof_imsm_dev(dl
->dev
, 1);
9009 if (u
->new_raid_disks
> u
->old_raid_disks
)
9010 size
+= sizeof(__u32
)*2*
9011 (u
->new_raid_disks
- u
->old_raid_disks
);
9018 len
= disks_to_mpb_size(u
->new_raid_disks
);
9019 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
9022 case update_reshape_migration
: {
9023 /* for migration level 0->5 we need to add disks
9024 * so the same as for container operation we will copy
9025 * device to the bigger location.
9026 * in memory prepared device and new disk area are prepared
9027 * for usage in process update
9029 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
9030 struct intel_dev
*id
;
9031 void **space_tail
= (void **)&update
->space_list
;
9034 int current_level
= -1;
9036 if (update
->len
< (int)sizeof(*u
))
9039 dprintf("for update_reshape\n");
9041 /* add space for bigger array in update
9043 for (id
= super
->devlist
; id
; id
= id
->next
) {
9044 if (id
->index
== (unsigned)u
->subdev
) {
9045 size
= sizeof_imsm_dev(id
->dev
, 1);
9046 if (u
->new_raid_disks
> u
->old_raid_disks
)
9047 size
+= sizeof(__u32
)*2*
9048 (u
->new_raid_disks
- u
->old_raid_disks
);
9056 if (update
->space_list
== NULL
)
9059 /* add space for disk in update
9061 size
= sizeof(struct dl
);
9067 /* add spare device to update
9069 for (id
= super
->devlist
; id
; id
= id
->next
)
9070 if (id
->index
== (unsigned)u
->subdev
) {
9071 struct imsm_dev
*dev
;
9072 struct imsm_map
*map
;
9074 dev
= get_imsm_dev(super
, u
->subdev
);
9075 map
= get_imsm_map(dev
, MAP_0
);
9076 current_level
= map
->raid_level
;
9079 if (u
->new_level
== 5 && u
->new_level
!= current_level
) {
9080 struct mdinfo
*spares
;
9082 spares
= get_spares_for_grow(st
);
9090 makedev(dev
->disk
.major
,
9092 dl
= get_disk_super(super
,
9095 dl
->index
= u
->old_raid_disks
;
9101 len
= disks_to_mpb_size(u
->new_raid_disks
);
9102 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
9105 case update_size_change
: {
9106 if (update
->len
< (int)sizeof(struct imsm_update_size_change
))
9110 case update_activate_spare
: {
9111 if (update
->len
< (int)sizeof(struct imsm_update_activate_spare
))
9115 case update_create_array
: {
9116 struct imsm_update_create_array
*u
= (void *) update
->buf
;
9117 struct intel_dev
*dv
;
9118 struct imsm_dev
*dev
= &u
->dev
;
9119 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9121 struct disk_info
*inf
;
9125 if (update
->len
< (int)sizeof(*u
))
9128 inf
= get_disk_info(u
);
9129 len
= sizeof_imsm_dev(dev
, 1);
9130 /* allocate a new super->devlist entry */
9131 dv
= xmalloc(sizeof(*dv
));
9132 dv
->dev
= xmalloc(len
);
9135 /* count how many spares will be converted to members */
9136 for (i
= 0; i
< map
->num_members
; i
++) {
9137 dl
= serial_to_dl(inf
[i
].serial
, super
);
9139 /* hmm maybe it failed?, nothing we can do about
9144 if (count_memberships(dl
, super
) == 0)
9147 len
+= activate
* sizeof(struct imsm_disk
);
9150 case update_kill_array
: {
9151 if (update
->len
< (int)sizeof(struct imsm_update_kill_array
))
9155 case update_rename_array
: {
9156 if (update
->len
< (int)sizeof(struct imsm_update_rename_array
))
9160 case update_add_remove_disk
:
9161 /* no update->len needed */
9167 /* check if we need a larger metadata buffer */
9168 if (super
->next_buf
)
9169 buf_len
= super
->next_len
;
9171 buf_len
= super
->len
;
9173 if (__le32_to_cpu(mpb
->mpb_size
) + len
> buf_len
) {
9174 /* ok we need a larger buf than what is currently allocated
9175 * if this allocation fails process_update will notice that
9176 * ->next_len is set and ->next_buf is NULL
9178 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + len
,
9180 if (super
->next_buf
)
9181 free(super
->next_buf
);
9183 super
->next_len
= buf_len
;
9184 if (posix_memalign(&super
->next_buf
, sector_size
, buf_len
) == 0)
9185 memset(super
->next_buf
, 0, buf_len
);
9187 super
->next_buf
= NULL
;
9192 /* must be called while manager is quiesced */
9193 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
9195 struct imsm_super
*mpb
= super
->anchor
;
9197 struct imsm_dev
*dev
;
9198 struct imsm_map
*map
;
9199 int i
, j
, num_members
;
9202 dprintf("deleting device[%d] from imsm_super\n", index
);
9204 /* shift all indexes down one */
9205 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
9206 if (iter
->index
> (int)index
)
9208 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
9209 if (iter
->index
> (int)index
)
9212 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
9213 dev
= get_imsm_dev(super
, i
);
9214 map
= get_imsm_map(dev
, MAP_0
);
9215 num_members
= map
->num_members
;
9216 for (j
= 0; j
< num_members
; j
++) {
9217 /* update ord entries being careful not to propagate
9218 * ord-flags to the first map
9220 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_X
);
9222 if (ord_to_idx(ord
) <= index
)
9225 map
= get_imsm_map(dev
, MAP_0
);
9226 set_imsm_ord_tbl_ent(map
, j
, ord_to_idx(ord
- 1));
9227 map
= get_imsm_map(dev
, MAP_1
);
9229 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
9234 super
->updates_pending
++;
9236 struct dl
*dl
= *dlp
;
9238 *dlp
= (*dlp
)->next
;
9239 __free_imsm_disk(dl
);
9242 #endif /* MDASSEMBLE */
9244 static void close_targets(int *targets
, int new_disks
)
9251 for (i
= 0; i
< new_disks
; i
++) {
9252 if (targets
[i
] >= 0) {
9259 static int imsm_get_allowed_degradation(int level
, int raid_disks
,
9260 struct intel_super
*super
,
9261 struct imsm_dev
*dev
)
9267 struct imsm_map
*map
;
9270 ret_val
= raid_disks
/2;
9271 /* check map if all disks pairs not failed
9274 map
= get_imsm_map(dev
, MAP_0
);
9275 for (i
= 0; i
< ret_val
; i
++) {
9276 int degradation
= 0;
9277 if (get_imsm_disk(super
, i
) == NULL
)
9279 if (get_imsm_disk(super
, i
+ 1) == NULL
)
9281 if (degradation
== 2)
9284 map
= get_imsm_map(dev
, MAP_1
);
9285 /* if there is no second map
9286 * result can be returned
9290 /* check degradation in second map
9292 for (i
= 0; i
< ret_val
; i
++) {
9293 int degradation
= 0;
9294 if (get_imsm_disk(super
, i
) == NULL
)
9296 if (get_imsm_disk(super
, i
+ 1) == NULL
)
9298 if (degradation
== 2)
9312 /*******************************************************************************
9313 * Function: open_backup_targets
9314 * Description: Function opens file descriptors for all devices given in
9317 * info : general array info
9318 * raid_disks : number of disks
9319 * raid_fds : table of device's file descriptors
9320 * super : intel super for raid10 degradation check
9321 * dev : intel device for raid10 degradation check
9325 ******************************************************************************/
9326 int open_backup_targets(struct mdinfo
*info
, int raid_disks
, int *raid_fds
,
9327 struct intel_super
*super
, struct imsm_dev
*dev
)
9333 for (i
= 0; i
< raid_disks
; i
++)
9336 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
9339 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
)) {
9340 dprintf("disk is faulty!!\n");
9344 if (sd
->disk
.raid_disk
>= raid_disks
|| sd
->disk
.raid_disk
< 0)
9347 dn
= map_dev(sd
->disk
.major
,
9349 raid_fds
[sd
->disk
.raid_disk
] = dev_open(dn
, O_RDWR
);
9350 if (raid_fds
[sd
->disk
.raid_disk
] < 0) {
9351 pr_err("cannot open component\n");
9356 /* check if maximum array degradation level is not exceeded
9358 if ((raid_disks
- opened
) >
9359 imsm_get_allowed_degradation(info
->new_level
, raid_disks
,
9361 pr_err("Not enough disks can be opened.\n");
9362 close_targets(raid_fds
, raid_disks
);
9368 /*******************************************************************************
9369 * Function: validate_container_imsm
9370 * Description: This routine validates container after assemble,
9371 * eg. if devices in container are under the same controller.
9374 * info : linked list with info about devices used in array
9378 ******************************************************************************/
9379 int validate_container_imsm(struct mdinfo
*info
)
9381 if (check_env("IMSM_NO_PLATFORM"))
9384 struct sys_dev
*idev
;
9385 struct sys_dev
*hba
= NULL
;
9386 struct sys_dev
*intel_devices
= find_intel_devices();
9387 char *dev_path
= devt_to_devpath(makedev(info
->disk
.major
,
9390 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
9391 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
9400 pr_err("WARNING - Cannot detect HBA for device %s!\n",
9401 devid2kname(makedev(info
->disk
.major
, info
->disk
.minor
)));
9405 const struct imsm_orom
*orom
= get_orom_by_device_id(hba
->dev_id
);
9408 for (dev
= info
->next
; dev
; dev
= dev
->next
) {
9409 dev_path
= devt_to_devpath(makedev(dev
->disk
.major
, dev
->disk
.minor
));
9411 struct sys_dev
*hba2
= NULL
;
9412 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
9413 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
9421 const struct imsm_orom
*orom2
= hba2
== NULL
? NULL
:
9422 get_orom_by_device_id(hba2
->dev_id
);
9424 if (hba2
&& hba
->type
!= hba2
->type
) {
9425 pr_err("WARNING - HBAs of devices do not match %s != %s\n",
9426 get_sys_dev_type(hba
->type
), get_sys_dev_type(hba2
->type
));
9430 if (orom
!= orom2
) {
9431 pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n"
9432 " This operation is not supported and can lead to data loss.\n");
9437 pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n"
9438 " This operation is not supported and can lead to data loss.\n");
9446 /*******************************************************************************
9447 * Function: init_migr_record_imsm
9448 * Description: Function inits imsm migration record
9450 * super : imsm internal array info
9451 * dev : device under migration
9452 * info : general array info to find the smallest device
9455 ******************************************************************************/
9456 void init_migr_record_imsm(struct supertype
*st
, struct imsm_dev
*dev
,
9457 struct mdinfo
*info
)
9459 struct intel_super
*super
= st
->sb
;
9460 struct migr_record
*migr_rec
= super
->migr_rec
;
9462 unsigned long long dsize
, dev_sectors
;
9463 long long unsigned min_dev_sectors
= -1LLU;
9467 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
9468 struct imsm_map
*map_src
= get_imsm_map(dev
, MAP_1
);
9469 unsigned long long num_migr_units
;
9470 unsigned long long array_blocks
;
9472 memset(migr_rec
, 0, sizeof(struct migr_record
));
9473 migr_rec
->family_num
= __cpu_to_le32(super
->anchor
->family_num
);
9475 /* only ascending reshape supported now */
9476 migr_rec
->ascending_migr
= __cpu_to_le32(1);
9478 migr_rec
->dest_depth_per_unit
= GEN_MIGR_AREA_SIZE
/
9479 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
9480 migr_rec
->dest_depth_per_unit
*=
9481 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
9482 new_data_disks
= imsm_num_data_members(dev
, MAP_0
);
9483 migr_rec
->blocks_per_unit
=
9484 __cpu_to_le32(migr_rec
->dest_depth_per_unit
* new_data_disks
);
9485 migr_rec
->dest_depth_per_unit
=
9486 __cpu_to_le32(migr_rec
->dest_depth_per_unit
);
9487 array_blocks
= info
->component_size
* new_data_disks
;
9489 array_blocks
/ __le32_to_cpu(migr_rec
->blocks_per_unit
);
9491 if (array_blocks
% __le32_to_cpu(migr_rec
->blocks_per_unit
))
9493 migr_rec
->num_migr_units
= __cpu_to_le32(num_migr_units
);
9495 migr_rec
->post_migr_vol_cap
= dev
->size_low
;
9496 migr_rec
->post_migr_vol_cap_hi
= dev
->size_high
;
9498 /* Find the smallest dev */
9499 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
9500 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
9501 fd
= dev_open(nm
, O_RDONLY
);
9504 get_dev_size(fd
, NULL
, &dsize
);
9505 dev_sectors
= dsize
/ 512;
9506 if (dev_sectors
< min_dev_sectors
)
9507 min_dev_sectors
= dev_sectors
;
9510 migr_rec
->ckpt_area_pba
= __cpu_to_le32(min_dev_sectors
-
9511 RAID_DISK_RESERVED_BLOCKS_IMSM_HI
);
9513 write_imsm_migr_rec(st
);
9518 /*******************************************************************************
9519 * Function: save_backup_imsm
9520 * Description: Function saves critical data stripes to Migration Copy Area
9521 * and updates the current migration unit status.
9522 * Use restore_stripes() to form a destination stripe,
9523 * and to write it to the Copy Area.
9525 * st : supertype information
9526 * dev : imsm device that backup is saved for
9527 * info : general array info
9528 * buf : input buffer
9529 * length : length of data to backup (blocks_per_unit)
9533 ******************************************************************************/
9534 int save_backup_imsm(struct supertype
*st
,
9535 struct imsm_dev
*dev
,
9536 struct mdinfo
*info
,
9541 struct intel_super
*super
= st
->sb
;
9542 unsigned long long *target_offsets
;
9545 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
9546 int new_disks
= map_dest
->num_members
;
9547 int dest_layout
= 0;
9549 unsigned long long start
;
9550 int data_disks
= imsm_num_data_members(dev
, MAP_0
);
9552 targets
= xmalloc(new_disks
* sizeof(int));
9554 for (i
= 0; i
< new_disks
; i
++)
9557 target_offsets
= xcalloc(new_disks
, sizeof(unsigned long long));
9559 start
= info
->reshape_progress
* 512;
9560 for (i
= 0; i
< new_disks
; i
++) {
9561 target_offsets
[i
] = (unsigned long long)
9562 __le32_to_cpu(super
->migr_rec
->ckpt_area_pba
) * 512;
9563 /* move back copy area adderss, it will be moved forward
9564 * in restore_stripes() using start input variable
9566 target_offsets
[i
] -= start
/data_disks
;
9569 if (open_backup_targets(info
, new_disks
, targets
,
9573 dest_layout
= imsm_level_to_layout(map_dest
->raid_level
);
9574 dest_chunk
= __le16_to_cpu(map_dest
->blocks_per_strip
) * 512;
9576 if (restore_stripes(targets
, /* list of dest devices */
9577 target_offsets
, /* migration record offsets */
9580 map_dest
->raid_level
,
9582 -1, /* source backup file descriptor */
9583 0, /* input buf offset
9584 * always 0 buf is already offseted */
9588 pr_err("Error restoring stripes\n");
9596 close_targets(targets
, new_disks
);
9599 free(target_offsets
);
9604 /*******************************************************************************
9605 * Function: save_checkpoint_imsm
9606 * Description: Function called for current unit status update
9607 * in the migration record. It writes it to disk.
9609 * super : imsm internal array info
9610 * info : general array info
9614 * 2: failure, means no valid migration record
9615 * / no general migration in progress /
9616 ******************************************************************************/
9617 int save_checkpoint_imsm(struct supertype
*st
, struct mdinfo
*info
, int state
)
9619 struct intel_super
*super
= st
->sb
;
9620 unsigned long long blocks_per_unit
;
9621 unsigned long long curr_migr_unit
;
9623 if (load_imsm_migr_rec(super
, info
) != 0) {
9624 dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n");
9628 blocks_per_unit
= __le32_to_cpu(super
->migr_rec
->blocks_per_unit
);
9629 if (blocks_per_unit
== 0) {
9630 dprintf("imsm: no migration in progress.\n");
9633 curr_migr_unit
= info
->reshape_progress
/ blocks_per_unit
;
9634 /* check if array is alligned to copy area
9635 * if it is not alligned, add one to current migration unit value
9636 * this can happend on array reshape finish only
9638 if (info
->reshape_progress
% blocks_per_unit
)
9641 super
->migr_rec
->curr_migr_unit
=
9642 __cpu_to_le32(curr_migr_unit
);
9643 super
->migr_rec
->rec_status
= __cpu_to_le32(state
);
9644 super
->migr_rec
->dest_1st_member_lba
=
9645 __cpu_to_le32(curr_migr_unit
*
9646 __le32_to_cpu(super
->migr_rec
->dest_depth_per_unit
));
9647 if (write_imsm_migr_rec(st
) < 0) {
9648 dprintf("imsm: Cannot write migration record outside backup area\n");
9655 /*******************************************************************************
9656 * Function: recover_backup_imsm
9657 * Description: Function recovers critical data from the Migration Copy Area
9658 * while assembling an array.
9660 * super : imsm internal array info
9661 * info : general array info
9663 * 0 : success (or there is no data to recover)
9665 ******************************************************************************/
9666 int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
)
9668 struct intel_super
*super
= st
->sb
;
9669 struct migr_record
*migr_rec
= super
->migr_rec
;
9670 struct imsm_map
*map_dest
;
9671 struct intel_dev
*id
= NULL
;
9672 unsigned long long read_offset
;
9673 unsigned long long write_offset
;
9675 int *targets
= NULL
;
9676 int new_disks
, i
, err
;
9679 unsigned int sector_size
= super
->sector_size
;
9680 unsigned long curr_migr_unit
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
9681 unsigned long num_migr_units
= __le32_to_cpu(migr_rec
->num_migr_units
);
9683 int skipped_disks
= 0;
9685 err
= sysfs_get_str(info
, NULL
, "array_state", (char *)buffer
, 20);
9689 /* recover data only during assemblation */
9690 if (strncmp(buffer
, "inactive", 8) != 0)
9692 /* no data to recover */
9693 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
9695 if (curr_migr_unit
>= num_migr_units
)
9698 /* find device during reshape */
9699 for (id
= super
->devlist
; id
; id
= id
->next
)
9700 if (is_gen_migration(id
->dev
))
9705 map_dest
= get_imsm_map(id
->dev
, MAP_0
);
9706 new_disks
= map_dest
->num_members
;
9708 read_offset
= (unsigned long long)
9709 __le32_to_cpu(migr_rec
->ckpt_area_pba
) * 512;
9711 write_offset
= ((unsigned long long)
9712 __le32_to_cpu(migr_rec
->dest_1st_member_lba
) +
9713 pba_of_lba0(map_dest
)) * 512;
9715 unit_len
= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
9716 if (posix_memalign((void **)&buf
, sector_size
, unit_len
) != 0)
9718 targets
= xcalloc(new_disks
, sizeof(int));
9720 if (open_backup_targets(info
, new_disks
, targets
, super
, id
->dev
)) {
9721 pr_err("Cannot open some devices belonging to array.\n");
9725 for (i
= 0; i
< new_disks
; i
++) {
9726 if (targets
[i
] < 0) {
9730 if (lseek64(targets
[i
], read_offset
, SEEK_SET
) < 0) {
9731 pr_err("Cannot seek to block: %s\n",
9736 if ((unsigned)read(targets
[i
], buf
, unit_len
) != unit_len
) {
9737 pr_err("Cannot read copy area block: %s\n",
9742 if (lseek64(targets
[i
], write_offset
, SEEK_SET
) < 0) {
9743 pr_err("Cannot seek to block: %s\n",
9748 if ((unsigned)write(targets
[i
], buf
, unit_len
) != unit_len
) {
9749 pr_err("Cannot restore block: %s\n",
9756 if (skipped_disks
> imsm_get_allowed_degradation(info
->new_level
,
9760 pr_err("Cannot restore data from backup. Too many failed disks\n");
9764 if (save_checkpoint_imsm(st
, info
, UNIT_SRC_NORMAL
)) {
9765 /* ignore error == 2, this can mean end of reshape here
9767 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n");
9773 for (i
= 0; i
< new_disks
; i
++)
9782 static char disk_by_path
[] = "/dev/disk/by-path/";
9784 static const char *imsm_get_disk_controller_domain(const char *path
)
9786 char disk_path
[PATH_MAX
];
9790 strcpy(disk_path
, disk_by_path
);
9791 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
9792 if (stat(disk_path
, &st
) == 0) {
9793 struct sys_dev
* hba
;
9796 path
= devt_to_devpath(st
.st_rdev
);
9799 hba
= find_disk_attached_hba(-1, path
);
9800 if (hba
&& hba
->type
== SYS_DEV_SAS
)
9802 else if (hba
&& hba
->type
== SYS_DEV_SATA
)
9806 dprintf("path: %s hba: %s attached: %s\n",
9807 path
, (hba
) ? hba
->path
: "NULL", drv
);
9813 static char *imsm_find_array_devnm_by_subdev(int subdev
, char *container
)
9815 static char devnm
[32];
9816 char subdev_name
[20];
9817 struct mdstat_ent
*mdstat
;
9819 sprintf(subdev_name
, "%d", subdev
);
9820 mdstat
= mdstat_by_subdev(subdev_name
, container
);
9824 strcpy(devnm
, mdstat
->devnm
);
9825 free_mdstat(mdstat
);
9829 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
9830 struct geo_params
*geo
,
9831 int *old_raid_disks
,
9834 /* currently we only support increasing the number of devices
9835 * for a container. This increases the number of device for each
9836 * member array. They must all be RAID0 or RAID5.
9839 struct mdinfo
*info
, *member
;
9840 int devices_that_can_grow
= 0;
9842 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st
->devnm
);
9844 if (geo
->size
> 0 ||
9845 geo
->level
!= UnSet
||
9846 geo
->layout
!= UnSet
||
9847 geo
->chunksize
!= 0 ||
9848 geo
->raid_disks
== UnSet
) {
9849 dprintf("imsm: Container operation is allowed for raid disks number change only.\n");
9853 if (direction
== ROLLBACK_METADATA_CHANGES
) {
9854 dprintf("imsm: Metadata changes rollback is not supported for container operation.\n");
9858 info
= container_content_imsm(st
, NULL
);
9859 for (member
= info
; member
; member
= member
->next
) {
9862 dprintf("imsm: checking device_num: %i\n",
9863 member
->container_member
);
9865 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
9866 /* we work on container for Online Capacity Expansion
9867 * only so raid_disks has to grow
9869 dprintf("imsm: for container operation raid disks increase is required\n");
9873 if (info
->array
.level
!= 0 && info
->array
.level
!= 5) {
9874 /* we cannot use this container with other raid level
9876 dprintf("imsm: for container operation wrong raid level (%i) detected\n",
9880 /* check for platform support
9881 * for this raid level configuration
9883 struct intel_super
*super
= st
->sb
;
9884 if (!is_raid_level_supported(super
->orom
,
9885 member
->array
.level
,
9887 dprintf("platform does not support raid%d with %d disk%s\n",
9890 geo
->raid_disks
> 1 ? "s" : "");
9893 /* check if component size is aligned to chunk size
9895 if (info
->component_size
%
9896 (info
->array
.chunk_size
/512)) {
9897 dprintf("Component size is not aligned to chunk size\n");
9902 if (*old_raid_disks
&&
9903 info
->array
.raid_disks
!= *old_raid_disks
)
9905 *old_raid_disks
= info
->array
.raid_disks
;
9907 /* All raid5 and raid0 volumes in container
9908 * have to be ready for Online Capacity Expansion
9909 * so they need to be assembled. We have already
9910 * checked that no recovery etc is happening.
9912 result
= imsm_find_array_devnm_by_subdev(member
->container_member
,
9913 st
->container_devnm
);
9914 if (result
== NULL
) {
9915 dprintf("imsm: cannot find array\n");
9918 devices_that_can_grow
++;
9921 if (!member
&& devices_that_can_grow
)
9925 dprintf("Container operation allowed\n");
9927 dprintf("Error: %i\n", ret_val
);
9932 /* Function: get_spares_for_grow
9933 * Description: Allocates memory and creates list of spare devices
9934 * avaliable in container. Checks if spare drive size is acceptable.
9935 * Parameters: Pointer to the supertype structure
9936 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
9939 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
9941 unsigned long long min_size
= min_acceptable_spare_size_imsm(st
);
9942 return container_choose_spares(st
, min_size
, NULL
, NULL
, NULL
, 0);
9945 /******************************************************************************
9946 * function: imsm_create_metadata_update_for_reshape
9947 * Function creates update for whole IMSM container.
9949 ******************************************************************************/
9950 static int imsm_create_metadata_update_for_reshape(
9951 struct supertype
*st
,
9952 struct geo_params
*geo
,
9954 struct imsm_update_reshape
**updatep
)
9956 struct intel_super
*super
= st
->sb
;
9957 struct imsm_super
*mpb
= super
->anchor
;
9958 int update_memory_size
;
9959 struct imsm_update_reshape
*u
;
9960 struct mdinfo
*spares
;
9965 dprintf("(enter) raid_disks = %i\n", geo
->raid_disks
);
9967 delta_disks
= geo
->raid_disks
- old_raid_disks
;
9969 /* size of all update data without anchor */
9970 update_memory_size
= sizeof(struct imsm_update_reshape
);
9972 /* now add space for spare disks that we need to add. */
9973 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
9975 u
= xcalloc(1, update_memory_size
);
9976 u
->type
= update_reshape_container_disks
;
9977 u
->old_raid_disks
= old_raid_disks
;
9978 u
->new_raid_disks
= geo
->raid_disks
;
9980 /* now get spare disks list
9982 spares
= get_spares_for_grow(st
);
9985 || delta_disks
> spares
->array
.spare_disks
) {
9986 pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo
->dev_name
);
9991 /* we have got spares
9992 * update disk list in imsm_disk list table in anchor
9994 dprintf("imsm: %i spares are available.\n\n",
9995 spares
->array
.spare_disks
);
9998 for (i
= 0; i
< delta_disks
; i
++) {
10003 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
10005 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
10006 dl
->index
= mpb
->num_disks
;
10014 sysfs_free(spares
);
10016 dprintf("imsm: reshape update preparation :");
10017 if (i
== delta_disks
) {
10018 dprintf_cont(" OK\n");
10020 return update_memory_size
;
10023 dprintf_cont(" Error\n");
10028 /******************************************************************************
10029 * function: imsm_create_metadata_update_for_size_change()
10030 * Creates update for IMSM array for array size change.
10032 ******************************************************************************/
10033 static int imsm_create_metadata_update_for_size_change(
10034 struct supertype
*st
,
10035 struct geo_params
*geo
,
10036 struct imsm_update_size_change
**updatep
)
10038 struct intel_super
*super
= st
->sb
;
10039 int update_memory_size
;
10040 struct imsm_update_size_change
*u
;
10042 dprintf("(enter) New size = %llu\n", geo
->size
);
10044 /* size of all update data without anchor */
10045 update_memory_size
= sizeof(struct imsm_update_size_change
);
10047 u
= xcalloc(1, update_memory_size
);
10048 u
->type
= update_size_change
;
10049 u
->subdev
= super
->current_vol
;
10050 u
->new_size
= geo
->size
;
10052 dprintf("imsm: reshape update preparation : OK\n");
10055 return update_memory_size
;
10058 /******************************************************************************
10059 * function: imsm_create_metadata_update_for_migration()
10060 * Creates update for IMSM array.
10062 ******************************************************************************/
10063 static int imsm_create_metadata_update_for_migration(
10064 struct supertype
*st
,
10065 struct geo_params
*geo
,
10066 struct imsm_update_reshape_migration
**updatep
)
10068 struct intel_super
*super
= st
->sb
;
10069 int update_memory_size
;
10070 struct imsm_update_reshape_migration
*u
;
10071 struct imsm_dev
*dev
;
10072 int previous_level
= -1;
10074 dprintf("(enter) New Level = %i\n", geo
->level
);
10076 /* size of all update data without anchor */
10077 update_memory_size
= sizeof(struct imsm_update_reshape_migration
);
10079 u
= xcalloc(1, update_memory_size
);
10080 u
->type
= update_reshape_migration
;
10081 u
->subdev
= super
->current_vol
;
10082 u
->new_level
= geo
->level
;
10083 u
->new_layout
= geo
->layout
;
10084 u
->new_raid_disks
= u
->old_raid_disks
= geo
->raid_disks
;
10085 u
->new_disks
[0] = -1;
10086 u
->new_chunksize
= -1;
10088 dev
= get_imsm_dev(super
, u
->subdev
);
10090 struct imsm_map
*map
;
10092 map
= get_imsm_map(dev
, MAP_0
);
10094 int current_chunk_size
=
10095 __le16_to_cpu(map
->blocks_per_strip
) / 2;
10097 if (geo
->chunksize
!= current_chunk_size
) {
10098 u
->new_chunksize
= geo
->chunksize
/ 1024;
10099 dprintf("imsm: chunk size change from %i to %i\n",
10100 current_chunk_size
, u
->new_chunksize
);
10102 previous_level
= map
->raid_level
;
10105 if (geo
->level
== 5 && previous_level
== 0) {
10106 struct mdinfo
*spares
= NULL
;
10108 u
->new_raid_disks
++;
10109 spares
= get_spares_for_grow(st
);
10110 if (spares
== NULL
|| spares
->array
.spare_disks
< 1) {
10112 sysfs_free(spares
);
10113 update_memory_size
= 0;
10114 dprintf("error: cannot get spare device for requested migration");
10117 sysfs_free(spares
);
10119 dprintf("imsm: reshape update preparation : OK\n");
10122 return update_memory_size
;
10125 static void imsm_update_metadata_locally(struct supertype
*st
,
10126 void *buf
, int len
)
10128 struct metadata_update mu
;
10133 mu
.space_list
= NULL
;
10135 if (imsm_prepare_update(st
, &mu
))
10136 imsm_process_update(st
, &mu
);
10138 while (mu
.space_list
) {
10139 void **space
= mu
.space_list
;
10140 mu
.space_list
= *space
;
10145 /***************************************************************************
10146 * Function: imsm_analyze_change
10147 * Description: Function analyze change for single volume
10148 * and validate if transition is supported
10149 * Parameters: Geometry parameters, supertype structure,
10150 * metadata change direction (apply/rollback)
10151 * Returns: Operation type code on success, -1 if fail
10152 ****************************************************************************/
10153 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
10154 struct geo_params
*geo
,
10157 struct mdinfo info
;
10159 int check_devs
= 0;
10161 /* number of added/removed disks in operation result */
10162 int devNumChange
= 0;
10163 /* imsm compatible layout value for array geometry verification */
10164 int imsm_layout
= -1;
10166 struct imsm_dev
*dev
;
10167 struct intel_super
*super
;
10168 unsigned long long current_size
;
10169 unsigned long long free_size
;
10170 unsigned long long max_size
;
10173 getinfo_super_imsm_volume(st
, &info
, NULL
);
10174 if (geo
->level
!= info
.array
.level
&& geo
->level
>= 0 &&
10175 geo
->level
!= UnSet
) {
10176 switch (info
.array
.level
) {
10178 if (geo
->level
== 5) {
10179 change
= CH_MIGRATION
;
10180 if (geo
->layout
!= ALGORITHM_LEFT_ASYMMETRIC
) {
10181 pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n");
10183 goto analyse_change_exit
;
10185 imsm_layout
= geo
->layout
;
10187 devNumChange
= 1; /* parity disk added */
10188 } else if (geo
->level
== 10) {
10189 change
= CH_TAKEOVER
;
10191 devNumChange
= 2; /* two mirrors added */
10192 imsm_layout
= 0x102; /* imsm supported layout */
10197 if (geo
->level
== 0) {
10198 change
= CH_TAKEOVER
;
10200 devNumChange
= -(geo
->raid_disks
/2);
10201 imsm_layout
= 0; /* imsm raid0 layout */
10205 if (change
== -1) {
10206 pr_err("Error. Level Migration from %d to %d not supported!\n",
10207 info
.array
.level
, geo
->level
);
10208 goto analyse_change_exit
;
10211 geo
->level
= info
.array
.level
;
10213 if (geo
->layout
!= info
.array
.layout
&&
10214 (geo
->layout
!= UnSet
&& geo
->layout
!= -1)) {
10215 change
= CH_MIGRATION
;
10216 if (info
.array
.layout
== 0 && info
.array
.level
== 5 &&
10217 geo
->layout
== 5) {
10218 /* reshape 5 -> 4 */
10219 } else if (info
.array
.layout
== 5 && info
.array
.level
== 5 &&
10220 geo
->layout
== 0) {
10221 /* reshape 4 -> 5 */
10225 pr_err("Error. Layout Migration from %d to %d not supported!\n",
10226 info
.array
.layout
, geo
->layout
);
10228 goto analyse_change_exit
;
10231 geo
->layout
= info
.array
.layout
;
10232 if (imsm_layout
== -1)
10233 imsm_layout
= info
.array
.layout
;
10236 if (geo
->chunksize
> 0 && geo
->chunksize
!= UnSet
&&
10237 geo
->chunksize
!= info
.array
.chunk_size
) {
10238 if (info
.array
.level
== 10) {
10239 pr_err("Error. Chunk size change for RAID 10 is not supported.\n");
10241 goto analyse_change_exit
;
10243 change
= CH_MIGRATION
;
10245 geo
->chunksize
= info
.array
.chunk_size
;
10248 chunk
= geo
->chunksize
/ 1024;
10251 dev
= get_imsm_dev(super
, super
->current_vol
);
10252 data_disks
= imsm_num_data_members(dev
, MAP_0
);
10253 /* compute current size per disk member
10255 current_size
= info
.custom_array_size
/ data_disks
;
10257 if (geo
->size
> 0 && geo
->size
!= MAX_SIZE
) {
10258 /* align component size
10260 geo
->size
= imsm_component_size_aligment_check(
10261 get_imsm_raid_level(dev
->vol
.map
),
10262 chunk
* 1024, super
->sector_size
,
10264 if (geo
->size
== 0) {
10265 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is 0).\n",
10267 goto analyse_change_exit
;
10271 if (current_size
!= geo
->size
&& geo
->size
> 0) {
10272 if (change
!= -1) {
10273 pr_err("Error. Size change should be the only one at a time.\n");
10275 goto analyse_change_exit
;
10277 if ((super
->current_vol
+ 1) != super
->anchor
->num_raid_devs
) {
10278 pr_err("Error. The last volume in container can be expanded only (%i/%s).\n",
10279 super
->current_vol
, st
->devnm
);
10280 goto analyse_change_exit
;
10282 /* check the maximum available size
10284 rv
= imsm_get_free_size(st
, dev
->vol
.map
->num_members
,
10285 0, chunk
, &free_size
);
10287 /* Cannot find maximum available space
10291 max_size
= free_size
+ current_size
;
10292 /* align component size
10294 max_size
= imsm_component_size_aligment_check(
10295 get_imsm_raid_level(dev
->vol
.map
),
10296 chunk
* 1024, super
->sector_size
,
10299 if (geo
->size
== MAX_SIZE
) {
10300 /* requested size change to the maximum available size
10302 if (max_size
== 0) {
10303 pr_err("Error. Cannot find maximum available space.\n");
10305 goto analyse_change_exit
;
10307 geo
->size
= max_size
;
10310 if (direction
== ROLLBACK_METADATA_CHANGES
) {
10311 /* accept size for rollback only
10314 /* round size due to metadata compatibility
10316 geo
->size
= (geo
->size
>> SECT_PER_MB_SHIFT
)
10317 << SECT_PER_MB_SHIFT
;
10318 dprintf("Prepare update for size change to %llu\n",
10320 if (current_size
>= geo
->size
) {
10321 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is %llu).\n",
10322 current_size
, geo
->size
);
10323 goto analyse_change_exit
;
10325 if (max_size
&& geo
->size
> max_size
) {
10326 pr_err("Error. Requested size is larger than maximum available size (maximum available size is %llu, requested size /rounded/ is %llu).\n",
10327 max_size
, geo
->size
);
10328 goto analyse_change_exit
;
10331 geo
->size
*= data_disks
;
10332 geo
->raid_disks
= dev
->vol
.map
->num_members
;
10333 change
= CH_ARRAY_SIZE
;
10335 if (!validate_geometry_imsm(st
,
10338 geo
->raid_disks
+ devNumChange
,
10340 geo
->size
, INVALID_SECTORS
,
10345 struct intel_super
*super
= st
->sb
;
10346 struct imsm_super
*mpb
= super
->anchor
;
10348 if (mpb
->num_raid_devs
> 1) {
10349 pr_err("Error. Cannot perform operation on %s- for this operation it MUST be single array in container\n",
10355 analyse_change_exit
:
10356 if (direction
== ROLLBACK_METADATA_CHANGES
&&
10357 (change
== CH_MIGRATION
|| change
== CH_TAKEOVER
)) {
10358 dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n");
10364 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
10366 struct intel_super
*super
= st
->sb
;
10367 struct imsm_update_takeover
*u
;
10369 u
= xmalloc(sizeof(struct imsm_update_takeover
));
10371 u
->type
= update_takeover
;
10372 u
->subarray
= super
->current_vol
;
10374 /* 10->0 transition */
10375 if (geo
->level
== 0)
10376 u
->direction
= R10_TO_R0
;
10378 /* 0->10 transition */
10379 if (geo
->level
== 10)
10380 u
->direction
= R0_TO_R10
;
10382 /* update metadata locally */
10383 imsm_update_metadata_locally(st
, u
,
10384 sizeof(struct imsm_update_takeover
));
10385 /* and possibly remotely */
10386 if (st
->update_tail
)
10387 append_metadata_update(st
, u
,
10388 sizeof(struct imsm_update_takeover
));
10395 static int imsm_reshape_super(struct supertype
*st
, unsigned long long size
,
10397 int layout
, int chunksize
, int raid_disks
,
10398 int delta_disks
, char *backup
, char *dev
,
10399 int direction
, int verbose
)
10402 struct geo_params geo
;
10404 dprintf("(enter)\n");
10406 memset(&geo
, 0, sizeof(struct geo_params
));
10408 geo
.dev_name
= dev
;
10409 strcpy(geo
.devnm
, st
->devnm
);
10412 geo
.layout
= layout
;
10413 geo
.chunksize
= chunksize
;
10414 geo
.raid_disks
= raid_disks
;
10415 if (delta_disks
!= UnSet
)
10416 geo
.raid_disks
+= delta_disks
;
10418 dprintf("for level : %i\n", geo
.level
);
10419 dprintf("for raid_disks : %i\n", geo
.raid_disks
);
10421 if (experimental() == 0)
10424 if (strcmp(st
->container_devnm
, st
->devnm
) == 0) {
10425 /* On container level we can only increase number of devices. */
10426 dprintf("imsm: info: Container operation\n");
10427 int old_raid_disks
= 0;
10429 if (imsm_reshape_is_allowed_on_container(
10430 st
, &geo
, &old_raid_disks
, direction
)) {
10431 struct imsm_update_reshape
*u
= NULL
;
10434 len
= imsm_create_metadata_update_for_reshape(
10435 st
, &geo
, old_raid_disks
, &u
);
10438 dprintf("imsm: Cannot prepare update\n");
10439 goto exit_imsm_reshape_super
;
10443 /* update metadata locally */
10444 imsm_update_metadata_locally(st
, u
, len
);
10445 /* and possibly remotely */
10446 if (st
->update_tail
)
10447 append_metadata_update(st
, u
, len
);
10452 pr_err("(imsm) Operation is not allowed on this container\n");
10455 /* On volume level we support following operations
10456 * - takeover: raid10 -> raid0; raid0 -> raid10
10457 * - chunk size migration
10458 * - migration: raid5 -> raid0; raid0 -> raid5
10460 struct intel_super
*super
= st
->sb
;
10461 struct intel_dev
*dev
= super
->devlist
;
10463 dprintf("imsm: info: Volume operation\n");
10464 /* find requested device */
10467 imsm_find_array_devnm_by_subdev(
10468 dev
->index
, st
->container_devnm
);
10469 if (devnm
&& strcmp(devnm
, geo
.devnm
) == 0)
10474 pr_err("Cannot find %s (%s) subarray\n",
10475 geo
.dev_name
, geo
.devnm
);
10476 goto exit_imsm_reshape_super
;
10478 super
->current_vol
= dev
->index
;
10479 change
= imsm_analyze_change(st
, &geo
, direction
);
10482 ret_val
= imsm_takeover(st
, &geo
);
10484 case CH_MIGRATION
: {
10485 struct imsm_update_reshape_migration
*u
= NULL
;
10487 imsm_create_metadata_update_for_migration(
10490 dprintf("imsm: Cannot prepare update\n");
10494 /* update metadata locally */
10495 imsm_update_metadata_locally(st
, u
, len
);
10496 /* and possibly remotely */
10497 if (st
->update_tail
)
10498 append_metadata_update(st
, u
, len
);
10503 case CH_ARRAY_SIZE
: {
10504 struct imsm_update_size_change
*u
= NULL
;
10506 imsm_create_metadata_update_for_size_change(
10509 dprintf("imsm: Cannot prepare update\n");
10513 /* update metadata locally */
10514 imsm_update_metadata_locally(st
, u
, len
);
10515 /* and possibly remotely */
10516 if (st
->update_tail
)
10517 append_metadata_update(st
, u
, len
);
10527 exit_imsm_reshape_super
:
10528 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
10532 #define COMPLETED_OK 0
10533 #define COMPLETED_NONE 1
10534 #define COMPLETED_DELAYED 2
10536 static int read_completed(int fd
, unsigned long long *val
)
10541 ret
= sysfs_fd_get_str(fd
, buf
, 50);
10545 ret
= COMPLETED_OK
;
10546 if (strncmp(buf
, "none", 4) == 0) {
10547 ret
= COMPLETED_NONE
;
10548 } else if (strncmp(buf
, "delayed", 7) == 0) {
10549 ret
= COMPLETED_DELAYED
;
10552 *val
= strtoull(buf
, &ep
, 0);
10553 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))
10559 /*******************************************************************************
10560 * Function: wait_for_reshape_imsm
10561 * Description: Function writes new sync_max value and waits until
10562 * reshape process reach new position
10564 * sra : general array info
10565 * ndata : number of disks in new array's layout
10568 * 1 : there is no reshape in progress,
10570 ******************************************************************************/
10571 int wait_for_reshape_imsm(struct mdinfo
*sra
, int ndata
)
10573 int fd
= sysfs_get_fd(sra
, NULL
, "sync_completed");
10575 unsigned long long completed
;
10576 /* to_complete : new sync_max position */
10577 unsigned long long to_complete
= sra
->reshape_progress
;
10578 unsigned long long position_to_set
= to_complete
/ ndata
;
10581 dprintf("cannot open reshape_position\n");
10586 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
10588 dprintf("cannot read reshape_position (no reshape in progres)\n");
10597 if (completed
> position_to_set
) {
10598 dprintf("wrong next position to set %llu (%llu)\n",
10599 to_complete
, position_to_set
);
10603 dprintf("Position set: %llu\n", position_to_set
);
10604 if (sysfs_set_num(sra
, NULL
, "sync_max",
10605 position_to_set
) != 0) {
10606 dprintf("cannot set reshape position to %llu\n",
10615 int timeout
= 3000;
10617 sysfs_wait(fd
, &timeout
);
10618 if (sysfs_get_str(sra
, NULL
, "sync_action",
10620 strncmp(action
, "reshape", 7) != 0) {
10621 if (strncmp(action
, "idle", 4) == 0)
10627 rc
= read_completed(fd
, &completed
);
10629 dprintf("cannot read reshape_position (in loop)\n");
10632 } else if (rc
== COMPLETED_NONE
)
10634 } while (completed
< position_to_set
);
10640 /*******************************************************************************
10641 * Function: check_degradation_change
10642 * Description: Check that array hasn't become failed.
10644 * info : for sysfs access
10645 * sources : source disks descriptors
10646 * degraded: previous degradation level
10648 * degradation level
10649 ******************************************************************************/
10650 int check_degradation_change(struct mdinfo
*info
,
10654 unsigned long long new_degraded
;
10657 rv
= sysfs_get_ll(info
, NULL
, "degraded", &new_degraded
);
10658 if (rv
== -1 || (new_degraded
!= (unsigned long long)degraded
)) {
10659 /* check each device to ensure it is still working */
10662 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
10663 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
10665 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
10667 if (sysfs_get_str(info
,
10668 sd
, "state", sbuf
, 20) < 0 ||
10669 strstr(sbuf
, "faulty") ||
10670 strstr(sbuf
, "in_sync") == NULL
) {
10671 /* this device is dead */
10672 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
10673 if (sd
->disk
.raid_disk
>= 0 &&
10674 sources
[sd
->disk
.raid_disk
] >= 0) {
10676 sd
->disk
.raid_disk
]);
10677 sources
[sd
->disk
.raid_disk
] =
10686 return new_degraded
;
10689 /*******************************************************************************
10690 * Function: imsm_manage_reshape
10691 * Description: Function finds array under reshape and it manages reshape
10692 * process. It creates stripes backups (if required) and sets
10695 * afd : Backup handle (nattive) - not used
10696 * sra : general array info
10697 * reshape : reshape parameters - not used
10698 * st : supertype structure
10699 * blocks : size of critical section [blocks]
10700 * fds : table of source device descriptor
10701 * offsets : start of array (offest per devices)
10703 * destfd : table of destination device descriptor
10704 * destoffsets : table of destination offsets (per device)
10706 * 1 : success, reshape is done
10708 ******************************************************************************/
10709 static int imsm_manage_reshape(
10710 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
10711 struct supertype
*st
, unsigned long backup_blocks
,
10712 int *fds
, unsigned long long *offsets
,
10713 int dests
, int *destfd
, unsigned long long *destoffsets
)
10716 struct intel_super
*super
= st
->sb
;
10717 struct intel_dev
*dv
;
10718 struct imsm_dev
*dev
= NULL
;
10719 struct imsm_map
*map_src
;
10720 int migr_vol_qan
= 0;
10721 int ndata
, odata
; /* [bytes] */
10722 int chunk
; /* [bytes] */
10723 struct migr_record
*migr_rec
;
10725 unsigned int buf_size
; /* [bytes] */
10726 unsigned long long max_position
; /* array size [bytes] */
10727 unsigned long long next_step
; /* [blocks]/[bytes] */
10728 unsigned long long old_data_stripe_length
;
10729 unsigned long long start_src
; /* [bytes] */
10730 unsigned long long start
; /* [bytes] */
10731 unsigned long long start_buf_shift
; /* [bytes] */
10733 int source_layout
= 0;
10738 if (!fds
|| !offsets
)
10741 /* Find volume during the reshape */
10742 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
10743 if (dv
->dev
->vol
.migr_type
== MIGR_GEN_MIGR
10744 && dv
->dev
->vol
.migr_state
== 1) {
10749 /* Only one volume can migrate at the same time */
10750 if (migr_vol_qan
!= 1) {
10751 pr_err("%s", migr_vol_qan
?
10752 "Number of migrating volumes greater than 1\n" :
10753 "There is no volume during migrationg\n");
10757 map_src
= get_imsm_map(dev
, MAP_1
);
10758 if (map_src
== NULL
)
10761 ndata
= imsm_num_data_members(dev
, MAP_0
);
10762 odata
= imsm_num_data_members(dev
, MAP_1
);
10764 chunk
= __le16_to_cpu(map_src
->blocks_per_strip
) * 512;
10765 old_data_stripe_length
= odata
* chunk
;
10767 migr_rec
= super
->migr_rec
;
10769 /* initialize migration record for start condition */
10770 if (sra
->reshape_progress
== 0)
10771 init_migr_record_imsm(st
, dev
, sra
);
10773 if (__le32_to_cpu(migr_rec
->rec_status
) != UNIT_SRC_NORMAL
) {
10774 dprintf("imsm: cannot restart migration when data are present in copy area.\n");
10777 /* Save checkpoint to update migration record for current
10778 * reshape position (in md). It can be farther than current
10779 * reshape position in metadata.
10781 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
10782 /* ignore error == 2, this can mean end of reshape here
10784 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n");
10789 /* size for data */
10790 buf_size
= __le32_to_cpu(migr_rec
->blocks_per_unit
) * 512;
10791 /* extend buffer size for parity disk */
10792 buf_size
+= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
10793 /* add space for stripe aligment */
10794 buf_size
+= old_data_stripe_length
;
10795 if (posix_memalign((void **)&buf
, 4096, buf_size
)) {
10796 dprintf("imsm: Cannot allocate checpoint buffer\n");
10800 max_position
= sra
->component_size
* ndata
;
10801 source_layout
= imsm_level_to_layout(map_src
->raid_level
);
10803 while (__le32_to_cpu(migr_rec
->curr_migr_unit
) <
10804 __le32_to_cpu(migr_rec
->num_migr_units
)) {
10805 /* current reshape position [blocks] */
10806 unsigned long long current_position
=
10807 __le32_to_cpu(migr_rec
->blocks_per_unit
)
10808 * __le32_to_cpu(migr_rec
->curr_migr_unit
);
10809 unsigned long long border
;
10811 /* Check that array hasn't become failed.
10813 degraded
= check_degradation_change(sra
, fds
, degraded
);
10814 if (degraded
> 1) {
10815 dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded
);
10819 next_step
= __le32_to_cpu(migr_rec
->blocks_per_unit
);
10821 if ((current_position
+ next_step
) > max_position
)
10822 next_step
= max_position
- current_position
;
10824 start
= current_position
* 512;
10826 /* align reading start to old geometry */
10827 start_buf_shift
= start
% old_data_stripe_length
;
10828 start_src
= start
- start_buf_shift
;
10830 border
= (start_src
/ odata
) - (start
/ ndata
);
10832 if (border
<= __le32_to_cpu(migr_rec
->dest_depth_per_unit
)) {
10833 /* save critical stripes to buf
10834 * start - start address of current unit
10835 * to backup [bytes]
10836 * start_src - start address of current unit
10837 * to backup alligned to source array
10840 unsigned long long next_step_filler
;
10841 unsigned long long copy_length
= next_step
* 512;
10843 /* allign copy area length to stripe in old geometry */
10844 next_step_filler
= ((copy_length
+ start_buf_shift
)
10845 % old_data_stripe_length
);
10846 if (next_step_filler
)
10847 next_step_filler
= (old_data_stripe_length
10848 - next_step_filler
);
10849 dprintf("save_stripes() parameters: start = %llu,\tstart_src = %llu,\tnext_step*512 = %llu,\tstart_in_buf_shift = %llu,\tnext_step_filler = %llu\n",
10850 start
, start_src
, copy_length
,
10851 start_buf_shift
, next_step_filler
);
10853 if (save_stripes(fds
, offsets
, map_src
->num_members
,
10854 chunk
, map_src
->raid_level
,
10855 source_layout
, 0, NULL
, start_src
,
10857 next_step_filler
+ start_buf_shift
,
10859 dprintf("imsm: Cannot save stripes to buffer\n");
10862 /* Convert data to destination format and store it
10863 * in backup general migration area
10865 if (save_backup_imsm(st
, dev
, sra
,
10866 buf
+ start_buf_shift
, copy_length
)) {
10867 dprintf("imsm: Cannot save stripes to target devices\n");
10870 if (save_checkpoint_imsm(st
, sra
,
10871 UNIT_SRC_IN_CP_AREA
)) {
10872 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n");
10876 /* set next step to use whole border area */
10877 border
/= next_step
;
10879 next_step
*= border
;
10881 /* When data backed up, checkpoint stored,
10882 * kick the kernel to reshape unit of data
10884 next_step
= next_step
+ sra
->reshape_progress
;
10885 /* limit next step to array max position */
10886 if (next_step
> max_position
)
10887 next_step
= max_position
;
10888 sysfs_set_num(sra
, NULL
, "suspend_lo", sra
->reshape_progress
);
10889 sysfs_set_num(sra
, NULL
, "suspend_hi", next_step
);
10890 sra
->reshape_progress
= next_step
;
10892 /* wait until reshape finish */
10893 if (wait_for_reshape_imsm(sra
, ndata
)) {
10894 dprintf("wait_for_reshape_imsm returned error!\n");
10900 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
10901 /* ignore error == 2, this can mean end of reshape here
10903 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n");
10909 /* clear migr_rec on disks after successful migration */
10912 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SIZE
);
10913 for (d
= super
->disks
; d
; d
= d
->next
) {
10914 if (d
->index
< 0 || is_failed(&d
->disk
))
10916 unsigned long long dsize
;
10918 get_dev_size(d
->fd
, NULL
, &dsize
);
10919 if (lseek64(d
->fd
, dsize
- MIGR_REC_POSITION
,
10921 if (write(d
->fd
, super
->migr_rec_buf
,
10922 MIGR_REC_BUF_SIZE
) != MIGR_REC_BUF_SIZE
)
10923 perror("Write migr_rec failed");
10927 /* return '1' if done */
10931 /* See Grow.c: abort_reshape() for further explanation */
10932 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
10933 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
10934 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
10939 #endif /* MDASSEMBLE */
10941 struct superswitch super_imsm
= {
10943 .examine_super
= examine_super_imsm
,
10944 .brief_examine_super
= brief_examine_super_imsm
,
10945 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
10946 .export_examine_super
= export_examine_super_imsm
,
10947 .detail_super
= detail_super_imsm
,
10948 .brief_detail_super
= brief_detail_super_imsm
,
10949 .write_init_super
= write_init_super_imsm
,
10950 .validate_geometry
= validate_geometry_imsm
,
10951 .add_to_super
= add_to_super_imsm
,
10952 .remove_from_super
= remove_from_super_imsm
,
10953 .detail_platform
= detail_platform_imsm
,
10954 .export_detail_platform
= export_detail_platform_imsm
,
10955 .kill_subarray
= kill_subarray_imsm
,
10956 .update_subarray
= update_subarray_imsm
,
10957 .load_container
= load_container_imsm
,
10958 .default_geometry
= default_geometry_imsm
,
10959 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
10960 .reshape_super
= imsm_reshape_super
,
10961 .manage_reshape
= imsm_manage_reshape
,
10962 .recover_backup
= recover_backup_imsm
,
10963 .copy_metadata
= copy_metadata_imsm
,
10965 .match_home
= match_home_imsm
,
10966 .uuid_from_super
= uuid_from_super_imsm
,
10967 .getinfo_super
= getinfo_super_imsm
,
10968 .getinfo_super_disks
= getinfo_super_disks_imsm
,
10969 .update_super
= update_super_imsm
,
10971 .avail_size
= avail_size_imsm
,
10972 .min_acceptable_spare_size
= min_acceptable_spare_size_imsm
,
10974 .compare_super
= compare_super_imsm
,
10976 .load_super
= load_super_imsm
,
10977 .init_super
= init_super_imsm
,
10978 .store_super
= store_super_imsm
,
10979 .free_super
= free_super_imsm
,
10980 .match_metadata_desc
= match_metadata_desc_imsm
,
10981 .container_content
= container_content_imsm
,
10982 .validate_container
= validate_container_imsm
,
10989 .open_new
= imsm_open_new
,
10990 .set_array_state
= imsm_set_array_state
,
10991 .set_disk
= imsm_set_disk
,
10992 .sync_metadata
= imsm_sync_metadata
,
10993 .activate_spare
= imsm_activate_spare
,
10994 .process_update
= imsm_process_update
,
10995 .prepare_update
= imsm_prepare_update
,
10996 #endif /* MDASSEMBLE */