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_SECTORS 1 /* size of migr_record i/o buffer in sectors */
248 #define MIGR_REC_SECTOR_POSITION 1 /* migr_record position offset on disk,
249 * MIGR_REC_BUF_SECTORS <= MIGR_REC_SECTOR_POS
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
);
913 static unsigned long long num_data_stripes(struct imsm_map
*map
)
917 return join_u32(map
->num_data_stripes_lo
, map
->num_data_stripes_hi
);
920 static void set_total_blocks(struct imsm_disk
*disk
, unsigned long long n
)
922 split_ull(n
, &disk
->total_blocks_lo
, &disk
->total_blocks_hi
);
925 static void set_pba_of_lba0(struct imsm_map
*map
, unsigned long long n
)
927 split_ull(n
, &map
->pba_of_lba0_lo
, &map
->pba_of_lba0_hi
);
930 static void set_blocks_per_member(struct imsm_map
*map
, unsigned long long n
)
932 split_ull(n
, &map
->blocks_per_member_lo
, &map
->blocks_per_member_hi
);
935 static void set_num_data_stripes(struct imsm_map
*map
, unsigned long long n
)
937 split_ull(n
, &map
->num_data_stripes_lo
, &map
->num_data_stripes_hi
);
940 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
)
942 /* find a list of used extents on the given physical device */
943 struct extent
*rv
, *e
;
945 int memberships
= count_memberships(dl
, super
);
948 /* trim the reserved area for spares, so they can join any array
949 * regardless of whether the OROM has assigned sectors from the
950 * IMSM_RESERVED_SECTORS region
953 reservation
= imsm_min_reserved_sectors(super
);
955 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
957 rv
= xcalloc(sizeof(struct extent
), (memberships
+ 1));
960 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
961 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
962 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
964 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
965 e
->start
= pba_of_lba0(map
);
966 e
->size
= blocks_per_member(map
);
970 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
972 /* determine the start of the metadata
973 * when no raid devices are defined use the default
974 * ...otherwise allow the metadata to truncate the value
975 * as is the case with older versions of imsm
978 struct extent
*last
= &rv
[memberships
- 1];
979 unsigned long long remainder
;
981 remainder
= total_blocks(&dl
->disk
) - (last
->start
+ last
->size
);
982 /* round down to 1k block to satisfy precision of the kernel
986 /* make sure remainder is still sane */
987 if (remainder
< (unsigned)ROUND_UP(super
->len
, 512) >> 9)
988 remainder
= ROUND_UP(super
->len
, 512) >> 9;
989 if (reservation
> remainder
)
990 reservation
= remainder
;
992 e
->start
= total_blocks(&dl
->disk
) - reservation
;
997 /* try to determine how much space is reserved for metadata from
998 * the last get_extents() entry, otherwise fallback to the
1001 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
1007 /* for spares just return a minimal reservation which will grow
1008 * once the spare is picked up by an array
1010 if (dl
->index
== -1)
1011 return MPB_SECTOR_CNT
;
1013 e
= get_extents(super
, dl
);
1015 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1017 /* scroll to last entry */
1018 for (i
= 0; e
[i
].size
; i
++)
1021 rv
= total_blocks(&dl
->disk
) - e
[i
].start
;
1028 static int is_spare(struct imsm_disk
*disk
)
1030 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
1033 static int is_configured(struct imsm_disk
*disk
)
1035 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
1038 static int is_failed(struct imsm_disk
*disk
)
1040 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
1043 /* try to determine how much space is reserved for metadata from
1044 * the last get_extents() entry on the smallest active disk,
1045 * otherwise fallback to the default
1047 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
)
1051 unsigned long long min_active
;
1053 __u32 rv
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1054 struct dl
*dl
, *dl_min
= NULL
;
1060 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
1063 unsigned long long blocks
= total_blocks(&dl
->disk
);
1064 if (blocks
< min_active
|| min_active
== 0) {
1066 min_active
= blocks
;
1072 /* find last lba used by subarrays on the smallest active disk */
1073 e
= get_extents(super
, dl_min
);
1076 for (i
= 0; e
[i
].size
; i
++)
1079 remainder
= min_active
- e
[i
].start
;
1082 /* to give priority to recovery we should not require full
1083 IMSM_RESERVED_SECTORS from the spare */
1084 rv
= MPB_SECTOR_CNT
+ NUM_BLOCKS_DIRTY_STRIPE_REGION
;
1086 /* if real reservation is smaller use that value */
1087 return (remainder
< rv
) ? remainder
: rv
;
1090 /* Return minimum size of a spare that can be used in this array*/
1091 static unsigned long long min_acceptable_spare_size_imsm(struct supertype
*st
)
1093 struct intel_super
*super
= st
->sb
;
1097 unsigned long long rv
= 0;
1101 /* find first active disk in array */
1103 while (dl
&& (is_failed(&dl
->disk
) || dl
->index
== -1))
1107 /* find last lba used by subarrays */
1108 e
= get_extents(super
, dl
);
1111 for (i
= 0; e
[i
].size
; i
++)
1114 rv
= e
[i
-1].start
+ e
[i
-1].size
;
1117 /* add the amount of space needed for metadata */
1118 rv
= rv
+ imsm_min_reserved_sectors(super
);
1123 static int is_gen_migration(struct imsm_dev
*dev
);
1125 #define IMSM_4K_DIV 8
1128 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
1129 struct imsm_dev
*dev
);
1131 static void print_imsm_dev(struct intel_super
*super
,
1132 struct imsm_dev
*dev
,
1138 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1139 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
1143 printf("[%.16s]:\n", dev
->volume
);
1144 printf(" UUID : %s\n", uuid
);
1145 printf(" RAID Level : %d", get_imsm_raid_level(map
));
1147 printf(" <-- %d", get_imsm_raid_level(map2
));
1149 printf(" Members : %d", map
->num_members
);
1151 printf(" <-- %d", map2
->num_members
);
1153 printf(" Slots : [");
1154 for (i
= 0; i
< map
->num_members
; i
++) {
1155 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
1156 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1161 for (i
= 0; i
< map2
->num_members
; i
++) {
1162 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_1
);
1163 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1168 printf(" Failed disk : ");
1169 if (map
->failed_disk_num
== 0xff)
1172 printf("%i", map
->failed_disk_num
);
1174 slot
= get_imsm_disk_slot(map
, disk_idx
);
1176 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
1177 printf(" This Slot : %d%s\n", slot
,
1178 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
1180 printf(" This Slot : ?\n");
1181 sz
= __le32_to_cpu(dev
->size_high
);
1183 sz
+= __le32_to_cpu(dev
->size_low
);
1184 printf(" Array Size : %llu%s\n", (unsigned long long)sz
,
1185 human_size(sz
* 512));
1186 sz
= blocks_per_member(map
);
1187 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz
,
1188 human_size(sz
* 512));
1189 printf(" Sector Offset : %llu\n",
1191 printf(" Num Stripes : %llu\n",
1192 num_data_stripes(map
));
1193 printf(" Chunk Size : %u KiB",
1194 __le16_to_cpu(map
->blocks_per_strip
) / 2);
1196 printf(" <-- %u KiB",
1197 __le16_to_cpu(map2
->blocks_per_strip
) / 2);
1199 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
1200 printf(" Migrate State : ");
1201 if (dev
->vol
.migr_state
) {
1202 if (migr_type(dev
) == MIGR_INIT
)
1203 printf("initialize\n");
1204 else if (migr_type(dev
) == MIGR_REBUILD
)
1205 printf("rebuild\n");
1206 else if (migr_type(dev
) == MIGR_VERIFY
)
1208 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
1209 printf("general migration\n");
1210 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
1211 printf("state change\n");
1212 else if (migr_type(dev
) == MIGR_REPAIR
)
1215 printf("<unknown:%d>\n", migr_type(dev
));
1218 printf(" Map State : %s", map_state_str
[map
->map_state
]);
1219 if (dev
->vol
.migr_state
) {
1220 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1222 printf(" <-- %s", map_state_str
[map
->map_state
]);
1223 printf("\n Checkpoint : %u ",
1224 __le32_to_cpu(dev
->vol
.curr_migr_unit
));
1225 if (is_gen_migration(dev
) && (slot
> 1 || slot
< 0))
1228 printf("(%llu)", (unsigned long long)
1229 blocks_per_migr_unit(super
, dev
));
1232 printf(" Dirty State : %s\n", dev
->vol
.dirty
? "dirty" : "clean");
1235 static void print_imsm_disk(struct imsm_disk
*disk
, int index
, __u32 reserved
)
1237 char str
[MAX_RAID_SERIAL_LEN
+ 1];
1240 if (index
< -1 || !disk
)
1244 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
1246 printf(" Disk%02d Serial : %s\n", index
, str
);
1248 printf(" Disk Serial : %s\n", str
);
1249 printf(" State :%s%s%s\n", is_spare(disk
) ? " spare" : "",
1250 is_configured(disk
) ? " active" : "",
1251 is_failed(disk
) ? " failed" : "");
1252 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
1253 sz
= total_blocks(disk
) - reserved
;
1254 printf(" Usable Size : %llu%s\n", (unsigned long long)sz
,
1255 human_size(sz
* 512));
1258 void convert_to_4k_imsm_migr_rec(struct intel_super
*super
)
1260 struct migr_record
*migr_rec
= super
->migr_rec
;
1262 migr_rec
->blocks_per_unit
/= IMSM_4K_DIV
;
1263 migr_rec
->ckpt_area_pba
/= IMSM_4K_DIV
;
1264 migr_rec
->dest_1st_member_lba
/= IMSM_4K_DIV
;
1265 migr_rec
->dest_depth_per_unit
/= IMSM_4K_DIV
;
1266 split_ull((join_u32(migr_rec
->post_migr_vol_cap
,
1267 migr_rec
->post_migr_vol_cap_hi
) / IMSM_4K_DIV
),
1268 &migr_rec
->post_migr_vol_cap
, &migr_rec
->post_migr_vol_cap_hi
);
1271 void convert_to_4k_imsm_disk(struct imsm_disk
*disk
)
1273 set_total_blocks(disk
, (total_blocks(disk
)/IMSM_4K_DIV
));
1276 void convert_to_4k(struct intel_super
*super
)
1278 struct imsm_super
*mpb
= super
->anchor
;
1279 struct imsm_disk
*disk
;
1282 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1283 disk
= __get_imsm_disk(mpb
, i
);
1285 convert_to_4k_imsm_disk(disk
);
1287 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1288 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1289 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1291 split_ull((join_u32(dev
->size_low
, dev
->size_high
)/IMSM_4K_DIV
),
1292 &dev
->size_low
, &dev
->size_high
);
1293 dev
->vol
.curr_migr_unit
/= IMSM_4K_DIV
;
1296 set_blocks_per_member(map
, blocks_per_member(map
)/IMSM_4K_DIV
);
1297 map
->blocks_per_strip
/= IMSM_4K_DIV
;
1298 set_pba_of_lba0(map
, pba_of_lba0(map
)/IMSM_4K_DIV
);
1300 if (dev
->vol
.migr_state
) {
1302 map
= get_imsm_map(dev
, MAP_1
);
1303 set_blocks_per_member(map
,
1304 blocks_per_member(map
)/IMSM_4K_DIV
);
1305 map
->blocks_per_strip
/= IMSM_4K_DIV
;
1306 set_pba_of_lba0(map
, pba_of_lba0(map
)/IMSM_4K_DIV
);
1310 mpb
->check_sum
= __gen_imsm_checksum(mpb
);
1313 void examine_migr_rec_imsm(struct intel_super
*super
)
1315 struct migr_record
*migr_rec
= super
->migr_rec
;
1316 struct imsm_super
*mpb
= super
->anchor
;
1319 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1320 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1321 struct imsm_map
*map
;
1324 if (is_gen_migration(dev
) == 0)
1327 printf("\nMigration Record Information:");
1329 /* first map under migration */
1330 map
= get_imsm_map(dev
, MAP_0
);
1332 slot
= get_imsm_disk_slot(map
, super
->disks
->index
);
1333 if (map
== NULL
|| slot
> 1 || slot
< 0) {
1334 printf(" Empty\n ");
1335 printf("Examine one of first two disks in array\n");
1338 printf("\n Status : ");
1339 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
1342 printf("Contains Data\n");
1343 printf(" Current Unit : %u\n",
1344 __le32_to_cpu(migr_rec
->curr_migr_unit
));
1345 printf(" Family : %u\n",
1346 __le32_to_cpu(migr_rec
->family_num
));
1347 printf(" Ascending : %u\n",
1348 __le32_to_cpu(migr_rec
->ascending_migr
));
1349 printf(" Blocks Per Unit : %u\n",
1350 __le32_to_cpu(migr_rec
->blocks_per_unit
));
1351 printf(" Dest. Depth Per Unit : %u\n",
1352 __le32_to_cpu(migr_rec
->dest_depth_per_unit
));
1353 printf(" Checkpoint Area pba : %u\n",
1354 __le32_to_cpu(migr_rec
->ckpt_area_pba
));
1355 printf(" First member lba : %u\n",
1356 __le32_to_cpu(migr_rec
->dest_1st_member_lba
));
1357 printf(" Total Number of Units : %u\n",
1358 __le32_to_cpu(migr_rec
->num_migr_units
));
1359 printf(" Size of volume : %u\n",
1360 __le32_to_cpu(migr_rec
->post_migr_vol_cap
));
1361 printf(" Expansion space for LBA64 : %u\n",
1362 __le32_to_cpu(migr_rec
->post_migr_vol_cap_hi
));
1363 printf(" Record was read from : %u\n",
1364 __le32_to_cpu(migr_rec
->ckpt_read_disk_num
));
1369 #endif /* MDASSEMBLE */
1371 void convert_from_4k_imsm_migr_rec(struct intel_super
*super
)
1373 struct migr_record
*migr_rec
= super
->migr_rec
;
1375 migr_rec
->blocks_per_unit
*= IMSM_4K_DIV
;
1376 migr_rec
->ckpt_area_pba
*= IMSM_4K_DIV
;
1377 migr_rec
->dest_1st_member_lba
*= IMSM_4K_DIV
;
1378 migr_rec
->dest_depth_per_unit
*= IMSM_4K_DIV
;
1379 split_ull((join_u32(migr_rec
->post_migr_vol_cap
,
1380 migr_rec
->post_migr_vol_cap_hi
) * IMSM_4K_DIV
),
1381 &migr_rec
->post_migr_vol_cap
,
1382 &migr_rec
->post_migr_vol_cap_hi
);
1385 void convert_from_4k(struct intel_super
*super
)
1387 struct imsm_super
*mpb
= super
->anchor
;
1388 struct imsm_disk
*disk
;
1391 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1392 disk
= __get_imsm_disk(mpb
, i
);
1394 set_total_blocks(disk
, (total_blocks(disk
)*IMSM_4K_DIV
));
1397 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1398 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1399 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1401 split_ull((join_u32(dev
->size_low
, dev
->size_high
)*IMSM_4K_DIV
),
1402 &dev
->size_low
, &dev
->size_high
);
1403 dev
->vol
.curr_migr_unit
*= IMSM_4K_DIV
;
1406 set_blocks_per_member(map
, blocks_per_member(map
)*IMSM_4K_DIV
);
1407 map
->blocks_per_strip
*= IMSM_4K_DIV
;
1408 set_pba_of_lba0(map
, pba_of_lba0(map
)*IMSM_4K_DIV
);
1410 if (dev
->vol
.migr_state
) {
1412 map
= get_imsm_map(dev
, MAP_1
);
1413 set_blocks_per_member(map
,
1414 blocks_per_member(map
)*IMSM_4K_DIV
);
1415 map
->blocks_per_strip
*= IMSM_4K_DIV
;
1416 set_pba_of_lba0(map
, pba_of_lba0(map
)*IMSM_4K_DIV
);
1420 mpb
->check_sum
= __gen_imsm_checksum(mpb
);
1423 /*******************************************************************************
1424 * function: imsm_check_attributes
1425 * Description: Function checks if features represented by attributes flags
1426 * are supported by mdadm.
1428 * attributes - Attributes read from metadata
1430 * 0 - passed attributes contains unsupported features flags
1431 * 1 - all features are supported
1432 ******************************************************************************/
1433 static int imsm_check_attributes(__u32 attributes
)
1436 __u32 not_supported
= MPB_ATTRIB_SUPPORTED
^0xffffffff;
1438 not_supported
&= ~MPB_ATTRIB_IGNORED
;
1440 not_supported
&= attributes
;
1441 if (not_supported
) {
1442 pr_err("(IMSM): Unsupported attributes : %x\n",
1443 (unsigned)__le32_to_cpu(not_supported
));
1444 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1445 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
1446 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1448 if (not_supported
& MPB_ATTRIB_2TB
) {
1449 dprintf("\t\tMPB_ATTRIB_2TB\n");
1450 not_supported
^= MPB_ATTRIB_2TB
;
1452 if (not_supported
& MPB_ATTRIB_RAID0
) {
1453 dprintf("\t\tMPB_ATTRIB_RAID0\n");
1454 not_supported
^= MPB_ATTRIB_RAID0
;
1456 if (not_supported
& MPB_ATTRIB_RAID1
) {
1457 dprintf("\t\tMPB_ATTRIB_RAID1\n");
1458 not_supported
^= MPB_ATTRIB_RAID1
;
1460 if (not_supported
& MPB_ATTRIB_RAID10
) {
1461 dprintf("\t\tMPB_ATTRIB_RAID10\n");
1462 not_supported
^= MPB_ATTRIB_RAID10
;
1464 if (not_supported
& MPB_ATTRIB_RAID1E
) {
1465 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
1466 not_supported
^= MPB_ATTRIB_RAID1E
;
1468 if (not_supported
& MPB_ATTRIB_RAID5
) {
1469 dprintf("\t\tMPB_ATTRIB_RAID5\n");
1470 not_supported
^= MPB_ATTRIB_RAID5
;
1472 if (not_supported
& MPB_ATTRIB_RAIDCNG
) {
1473 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
1474 not_supported
^= MPB_ATTRIB_RAIDCNG
;
1476 if (not_supported
& MPB_ATTRIB_BBM
) {
1477 dprintf("\t\tMPB_ATTRIB_BBM\n");
1478 not_supported
^= MPB_ATTRIB_BBM
;
1480 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1481 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
1482 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1484 if (not_supported
& MPB_ATTRIB_EXP_STRIPE_SIZE
) {
1485 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
1486 not_supported
^= MPB_ATTRIB_EXP_STRIPE_SIZE
;
1488 if (not_supported
& MPB_ATTRIB_2TB_DISK
) {
1489 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
1490 not_supported
^= MPB_ATTRIB_2TB_DISK
;
1492 if (not_supported
& MPB_ATTRIB_NEVER_USE2
) {
1493 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
1494 not_supported
^= MPB_ATTRIB_NEVER_USE2
;
1496 if (not_supported
& MPB_ATTRIB_NEVER_USE
) {
1497 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
1498 not_supported
^= MPB_ATTRIB_NEVER_USE
;
1502 dprintf("(IMSM): Unknown attributes : %x\n", not_supported
);
1511 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
1513 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
1515 struct intel_super
*super
= st
->sb
;
1516 struct imsm_super
*mpb
= super
->anchor
;
1517 char str
[MAX_SIGNATURE_LENGTH
];
1522 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
1525 snprintf(str
, MPB_SIG_LEN
, "%s", mpb
->sig
);
1526 printf(" Magic : %s\n", str
);
1527 snprintf(str
, strlen(MPB_VERSION_RAID0
), "%s", get_imsm_version(mpb
));
1528 printf(" Version : %s\n", get_imsm_version(mpb
));
1529 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
1530 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
1531 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
1532 printf(" Attributes : ");
1533 if (imsm_check_attributes(mpb
->attributes
))
1534 printf("All supported\n");
1536 printf("not supported\n");
1537 getinfo_super_imsm(st
, &info
, NULL
);
1538 fname_from_uuid(st
, &info
, nbuf
, ':');
1539 printf(" UUID : %s\n", nbuf
+ 5);
1540 sum
= __le32_to_cpu(mpb
->check_sum
);
1541 printf(" Checksum : %08x %s\n", sum
,
1542 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
1543 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
, super
->sector_size
));
1544 printf(" Disks : %d\n", mpb
->num_disks
);
1545 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
1546 print_imsm_disk(__get_imsm_disk(mpb
, super
->disks
->index
), super
->disks
->index
, reserved
);
1547 if (super
->bbm_log
) {
1548 struct bbm_log
*log
= super
->bbm_log
;
1551 printf("Bad Block Management Log:\n");
1552 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
1553 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
1554 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
1555 printf(" Spare Blocks : %d\n", __le32_to_cpu(log
->reserved_spare_block_count
));
1556 printf(" First Spare : %llx\n",
1557 (unsigned long long) __le64_to_cpu(log
->first_spare_lba
));
1559 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1561 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1563 super
->current_vol
= i
;
1564 getinfo_super_imsm(st
, &info
, NULL
);
1565 fname_from_uuid(st
, &info
, nbuf
, ':');
1566 print_imsm_dev(super
, dev
, nbuf
+ 5, super
->disks
->index
);
1568 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1569 if (i
== super
->disks
->index
)
1571 print_imsm_disk(__get_imsm_disk(mpb
, i
), i
, reserved
);
1574 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
1575 if (dl
->index
== -1)
1576 print_imsm_disk(&dl
->disk
, -1, reserved
);
1578 examine_migr_rec_imsm(super
);
1581 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
1583 /* We just write a generic IMSM ARRAY entry */
1586 struct intel_super
*super
= st
->sb
;
1588 if (!super
->anchor
->num_raid_devs
) {
1589 printf("ARRAY metadata=imsm\n");
1593 getinfo_super_imsm(st
, &info
, NULL
);
1594 fname_from_uuid(st
, &info
, nbuf
, ':');
1595 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
1598 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
1600 /* We just write a generic IMSM ARRAY entry */
1604 struct intel_super
*super
= st
->sb
;
1607 if (!super
->anchor
->num_raid_devs
)
1610 getinfo_super_imsm(st
, &info
, NULL
);
1611 fname_from_uuid(st
, &info
, nbuf
, ':');
1612 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1613 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1615 super
->current_vol
= i
;
1616 getinfo_super_imsm(st
, &info
, NULL
);
1617 fname_from_uuid(st
, &info
, nbuf1
, ':');
1618 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
1619 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
1623 static void export_examine_super_imsm(struct supertype
*st
)
1625 struct intel_super
*super
= st
->sb
;
1626 struct imsm_super
*mpb
= super
->anchor
;
1630 getinfo_super_imsm(st
, &info
, NULL
);
1631 fname_from_uuid(st
, &info
, nbuf
, ':');
1632 printf("MD_METADATA=imsm\n");
1633 printf("MD_LEVEL=container\n");
1634 printf("MD_UUID=%s\n", nbuf
+5);
1635 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
1638 static int copy_metadata_imsm(struct supertype
*st
, int from
, int to
)
1640 /* The second last sector of the device contains
1641 * the "struct imsm_super" metadata.
1642 * This contains mpb_size which is the size in bytes of the
1643 * extended metadata. This is located immediately before
1645 * We want to read all that, plus the last sector which
1646 * may contain a migration record, and write it all
1650 unsigned long long dsize
, offset
;
1652 struct imsm_super
*sb
;
1653 struct intel_super
*super
= st
->sb
;
1654 unsigned int sector_size
= super
->sector_size
;
1655 unsigned int written
= 0;
1657 if (posix_memalign(&buf
, MAX_SECTOR_SIZE
, MAX_SECTOR_SIZE
) != 0)
1660 if (!get_dev_size(from
, NULL
, &dsize
))
1663 if (lseek64(from
, dsize
-(2*sector_size
), 0) < 0)
1665 if (read(from
, buf
, sector_size
) != sector_size
)
1668 if (strncmp((char*)sb
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0)
1671 sectors
= mpb_sectors(sb
, sector_size
) + 2;
1672 offset
= dsize
- sectors
* sector_size
;
1673 if (lseek64(from
, offset
, 0) < 0 ||
1674 lseek64(to
, offset
, 0) < 0)
1676 while (written
< sectors
* sector_size
) {
1677 int n
= sectors
*sector_size
- written
;
1680 if (read(from
, buf
, n
) != n
)
1682 if (write(to
, buf
, n
) != n
)
1693 static void detail_super_imsm(struct supertype
*st
, char *homehost
)
1698 getinfo_super_imsm(st
, &info
, NULL
);
1699 fname_from_uuid(st
, &info
, nbuf
, ':');
1700 printf("\n UUID : %s\n", nbuf
+ 5);
1703 static void brief_detail_super_imsm(struct supertype
*st
)
1707 getinfo_super_imsm(st
, &info
, NULL
);
1708 fname_from_uuid(st
, &info
, nbuf
, ':');
1709 printf(" UUID=%s", nbuf
+ 5);
1712 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
);
1713 static void fd2devname(int fd
, char *name
);
1715 static int ahci_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
1717 /* dump an unsorted list of devices attached to AHCI Intel storage
1718 * controller, as well as non-connected ports
1720 int hba_len
= strlen(hba_path
) + 1;
1725 unsigned long port_mask
= (1 << port_count
) - 1;
1727 if (port_count
> (int)sizeof(port_mask
) * 8) {
1729 pr_err("port_count %d out of range\n", port_count
);
1733 /* scroll through /sys/dev/block looking for devices attached to
1736 dir
= opendir("/sys/dev/block");
1740 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
1751 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
1753 path
= devt_to_devpath(makedev(major
, minor
));
1756 if (!path_attached_to_hba(path
, hba_path
)) {
1762 /* retrieve the scsi device type */
1763 if (asprintf(&device
, "/sys/dev/block/%d:%d/device/xxxxxxx", major
, minor
) < 0) {
1765 pr_err("failed to allocate 'device'\n");
1769 sprintf(device
, "/sys/dev/block/%d:%d/device/type", major
, minor
);
1770 if (load_sys(device
, buf
, sizeof(buf
)) != 0) {
1772 pr_err("failed to read device type for %s\n",
1778 type
= strtoul(buf
, NULL
, 10);
1780 /* if it's not a disk print the vendor and model */
1781 if (!(type
== 0 || type
== 7 || type
== 14)) {
1784 sprintf(device
, "/sys/dev/block/%d:%d/device/vendor", major
, minor
);
1785 if (load_sys(device
, buf
, sizeof(buf
)) == 0) {
1786 strncpy(vendor
, buf
, sizeof(vendor
));
1787 vendor
[sizeof(vendor
) - 1] = '\0';
1788 c
= (char *) &vendor
[sizeof(vendor
) - 1];
1789 while (isspace(*c
) || *c
== '\0')
1793 sprintf(device
, "/sys/dev/block/%d:%d/device/model", major
, minor
);
1794 if (load_sys(device
, buf
, sizeof(buf
)) == 0) {
1795 strncpy(model
, buf
, sizeof(model
));
1796 model
[sizeof(model
) - 1] = '\0';
1797 c
= (char *) &model
[sizeof(model
) - 1];
1798 while (isspace(*c
) || *c
== '\0')
1802 if (vendor
[0] && model
[0])
1803 sprintf(buf
, "%.64s %.64s", vendor
, model
);
1805 switch (type
) { /* numbers from hald/linux/device.c */
1806 case 1: sprintf(buf
, "tape"); break;
1807 case 2: sprintf(buf
, "printer"); break;
1808 case 3: sprintf(buf
, "processor"); break;
1810 case 5: sprintf(buf
, "cdrom"); break;
1811 case 6: sprintf(buf
, "scanner"); break;
1812 case 8: sprintf(buf
, "media_changer"); break;
1813 case 9: sprintf(buf
, "comm"); break;
1814 case 12: sprintf(buf
, "raid"); break;
1815 default: sprintf(buf
, "unknown");
1821 /* chop device path to 'host%d' and calculate the port number */
1822 c
= strchr(&path
[hba_len
], '/');
1825 pr_err("%s - invalid path name\n", path
+ hba_len
);
1830 if ((sscanf(&path
[hba_len
], "ata%d", &port
) == 1) ||
1831 ((sscanf(&path
[hba_len
], "host%d", &port
) == 1)))
1835 *c
= '/'; /* repair the full string */
1836 pr_err("failed to determine port number for %s\n",
1843 /* mark this port as used */
1844 port_mask
&= ~(1 << port
);
1846 /* print out the device information */
1848 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
1852 fd
= dev_open(ent
->d_name
, O_RDONLY
);
1854 printf(" Port%d : - disk info unavailable -\n", port
);
1856 fd2devname(fd
, buf
);
1857 printf(" Port%d : %s", port
, buf
);
1858 if (imsm_read_serial(fd
, NULL
, (__u8
*) buf
) == 0)
1859 printf(" (%.*s)\n", MAX_RAID_SERIAL_LEN
, buf
);
1874 for (i
= 0; i
< port_count
; i
++)
1875 if (port_mask
& (1 << i
))
1876 printf(" Port%d : - no device attached -\n", i
);
1882 static int print_vmd_attached_devs(struct sys_dev
*hba
)
1890 if (hba
->type
!= SYS_DEV_VMD
)
1893 /* scroll through /sys/dev/block looking for devices attached to
1896 dir
= opendir("/sys/bus/pci/drivers/nvme");
1900 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
1903 /* is 'ent' a device? check that the 'subsystem' link exists and
1904 * that its target matches 'bus'
1906 sprintf(path
, "/sys/bus/pci/drivers/nvme/%s/subsystem",
1908 n
= readlink(path
, link
, sizeof(link
));
1909 if (n
< 0 || n
>= (int)sizeof(link
))
1912 c
= strrchr(link
, '/');
1915 if (strncmp("pci", c
+1, strlen("pci")) != 0)
1918 sprintf(path
, "/sys/bus/pci/drivers/nvme/%s", ent
->d_name
);
1919 /* if not a intel NVMe - skip it*/
1920 if (devpath_to_vendor(path
) != 0x8086)
1923 rp
= realpath(path
, NULL
);
1927 if (path_attached_to_hba(rp
, hba
->path
)) {
1928 printf(" NVMe under VMD : %s\n", rp
);
1937 static void print_found_intel_controllers(struct sys_dev
*elem
)
1939 for (; elem
; elem
= elem
->next
) {
1940 pr_err("found Intel(R) ");
1941 if (elem
->type
== SYS_DEV_SATA
)
1942 fprintf(stderr
, "SATA ");
1943 else if (elem
->type
== SYS_DEV_SAS
)
1944 fprintf(stderr
, "SAS ");
1945 else if (elem
->type
== SYS_DEV_NVME
)
1946 fprintf(stderr
, "NVMe ");
1948 if (elem
->type
== SYS_DEV_VMD
)
1949 fprintf(stderr
, "VMD domain");
1951 fprintf(stderr
, "RAID controller");
1954 fprintf(stderr
, " at %s", elem
->pci_id
);
1955 fprintf(stderr
, ".\n");
1960 static int ahci_get_port_count(const char *hba_path
, int *port_count
)
1967 if ((dir
= opendir(hba_path
)) == NULL
)
1970 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
1973 if ((sscanf(ent
->d_name
, "ata%d", &host
) != 1) &&
1974 ((sscanf(ent
->d_name
, "host%d", &host
) != 1)))
1976 if (*port_count
== 0)
1978 else if (host
< host_base
)
1981 if (host
+ 1 > *port_count
+ host_base
)
1982 *port_count
= host
+ 1 - host_base
;
1988 static void print_imsm_capability(const struct imsm_orom
*orom
)
1990 printf(" Platform : Intel(R) ");
1991 if (orom
->capabilities
== 0 && orom
->driver_features
== 0)
1992 printf("Matrix Storage Manager\n");
1994 printf("Rapid Storage Technology%s\n",
1995 imsm_orom_is_enterprise(orom
) ? " enterprise" : "");
1996 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
)
1997 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
,
1998 orom
->minor_ver
, orom
->hotfix_ver
, orom
->build
);
1999 printf(" RAID Levels :%s%s%s%s%s\n",
2000 imsm_orom_has_raid0(orom
) ? " raid0" : "",
2001 imsm_orom_has_raid1(orom
) ? " raid1" : "",
2002 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
2003 imsm_orom_has_raid10(orom
) ? " raid10" : "",
2004 imsm_orom_has_raid5(orom
) ? " raid5" : "");
2005 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2006 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
2007 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
2008 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
2009 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
2010 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
2011 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
2012 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
2013 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
2014 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
2015 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
2016 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
2017 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
2018 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
2019 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
2020 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
2021 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
2022 printf(" 2TB volumes :%s supported\n",
2023 (orom
->attr
& IMSM_OROM_ATTR_2TB
)?"":" not");
2024 printf(" 2TB disks :%s supported\n",
2025 (orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
)?"":" not");
2026 printf(" Max Disks : %d\n", orom
->tds
);
2027 printf(" Max Volumes : %d per array, %d per %s\n",
2028 orom
->vpa
, orom
->vphba
,
2029 imsm_orom_is_nvme(orom
) ? "platform" : "controller");
2033 static void print_imsm_capability_export(const struct imsm_orom
*orom
)
2035 printf("MD_FIRMWARE_TYPE=imsm\n");
2036 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
)
2037 printf("IMSM_VERSION=%d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
2038 orom
->hotfix_ver
, orom
->build
);
2039 printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n",
2040 imsm_orom_has_raid0(orom
) ? "raid0 " : "",
2041 imsm_orom_has_raid1(orom
) ? "raid1 " : "",
2042 imsm_orom_has_raid1e(orom
) ? "raid1e " : "",
2043 imsm_orom_has_raid5(orom
) ? "raid10 " : "",
2044 imsm_orom_has_raid10(orom
) ? "raid5 " : "");
2045 printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2046 imsm_orom_has_chunk(orom
, 2) ? "2k " : "",
2047 imsm_orom_has_chunk(orom
, 4) ? "4k " : "",
2048 imsm_orom_has_chunk(orom
, 8) ? "8k " : "",
2049 imsm_orom_has_chunk(orom
, 16) ? "16k " : "",
2050 imsm_orom_has_chunk(orom
, 32) ? "32k " : "",
2051 imsm_orom_has_chunk(orom
, 64) ? "64k " : "",
2052 imsm_orom_has_chunk(orom
, 128) ? "128k " : "",
2053 imsm_orom_has_chunk(orom
, 256) ? "256k " : "",
2054 imsm_orom_has_chunk(orom
, 512) ? "512k " : "",
2055 imsm_orom_has_chunk(orom
, 1024*1) ? "1M " : "",
2056 imsm_orom_has_chunk(orom
, 1024*2) ? "2M " : "",
2057 imsm_orom_has_chunk(orom
, 1024*4) ? "4M " : "",
2058 imsm_orom_has_chunk(orom
, 1024*8) ? "8M " : "",
2059 imsm_orom_has_chunk(orom
, 1024*16) ? "16M " : "",
2060 imsm_orom_has_chunk(orom
, 1024*32) ? "32M " : "",
2061 imsm_orom_has_chunk(orom
, 1024*64) ? "64M " : "");
2062 printf("IMSM_2TB_VOLUMES=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB
) ? "yes" : "no");
2063 printf("IMSM_2TB_DISKS=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) ? "yes" : "no");
2064 printf("IMSM_MAX_DISKS=%d\n",orom
->tds
);
2065 printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom
->vpa
);
2066 printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom
->vphba
);
2069 static int detail_platform_imsm(int verbose
, int enumerate_only
, char *controller_path
)
2071 /* There are two components to imsm platform support, the ahci SATA
2072 * controller and the option-rom. To find the SATA controller we
2073 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
2074 * controller with the Intel vendor id is present. This approach
2075 * allows mdadm to leverage the kernel's ahci detection logic, with the
2076 * caveat that if ahci.ko is not loaded mdadm will not be able to
2077 * detect platform raid capabilities. The option-rom resides in a
2078 * platform "Adapter ROM". We scan for its signature to retrieve the
2079 * platform capabilities. If raid support is disabled in the BIOS the
2080 * option-rom capability structure will not be available.
2082 struct sys_dev
*list
, *hba
;
2087 if (enumerate_only
) {
2088 if (check_env("IMSM_NO_PLATFORM"))
2090 list
= find_intel_devices();
2093 for (hba
= list
; hba
; hba
= hba
->next
) {
2094 if (find_imsm_capability(hba
)) {
2104 list
= find_intel_devices();
2107 pr_err("no active Intel(R) RAID controller found.\n");
2109 } else if (verbose
> 0)
2110 print_found_intel_controllers(list
);
2112 for (hba
= list
; hba
; hba
= hba
->next
) {
2113 if (controller_path
&& (compare_paths(hba
->path
, controller_path
) != 0))
2115 if (!find_imsm_capability(hba
)) {
2117 pr_err("imsm capabilities not found for controller: %s (type %s)\n",
2118 hba
->type
== SYS_DEV_VMD
? vmd_domain_to_controller(hba
, buf
) : hba
->path
,
2119 get_sys_dev_type(hba
->type
));
2125 if (controller_path
&& result
== 1) {
2126 pr_err("no active Intel(R) RAID controller found under %s\n",
2131 const struct orom_entry
*entry
;
2133 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2134 if (entry
->type
== SYS_DEV_VMD
) {
2135 print_imsm_capability(&entry
->orom
);
2136 for (hba
= list
; hba
; hba
= hba
->next
) {
2137 if (hba
->type
== SYS_DEV_VMD
) {
2139 printf(" I/O Controller : %s (%s)\n",
2140 vmd_domain_to_controller(hba
, buf
), get_sys_dev_type(hba
->type
));
2141 if (print_vmd_attached_devs(hba
)) {
2143 pr_err("failed to get devices attached to VMD domain.\n");
2152 print_imsm_capability(&entry
->orom
);
2153 if (entry
->type
== SYS_DEV_NVME
) {
2154 for (hba
= list
; hba
; hba
= hba
->next
) {
2155 if (hba
->type
== SYS_DEV_NVME
)
2156 printf(" NVMe Device : %s\n", hba
->path
);
2162 struct devid_list
*devid
;
2163 for (devid
= entry
->devid_list
; devid
; devid
= devid
->next
) {
2164 hba
= device_by_id(devid
->devid
);
2168 printf(" I/O Controller : %s (%s)\n",
2169 hba
->path
, get_sys_dev_type(hba
->type
));
2170 if (hba
->type
== SYS_DEV_SATA
) {
2171 host_base
= ahci_get_port_count(hba
->path
, &port_count
);
2172 if (ahci_enumerate_ports(hba
->path
, port_count
, host_base
, verbose
)) {
2174 pr_err("failed to enumerate ports on SATA controller at %s.\n", hba
->pci_id
);
2185 static int export_detail_platform_imsm(int verbose
, char *controller_path
)
2187 struct sys_dev
*list
, *hba
;
2190 list
= find_intel_devices();
2193 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n");
2198 for (hba
= list
; hba
; hba
= hba
->next
) {
2199 if (controller_path
&& (compare_paths(hba
->path
,controller_path
) != 0))
2201 if (!find_imsm_capability(hba
) && verbose
> 0) {
2203 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n",
2204 hba
->type
== SYS_DEV_VMD
? vmd_domain_to_controller(hba
, buf
) : hba
->path
);
2210 const struct orom_entry
*entry
;
2212 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2213 if (entry
->type
== SYS_DEV_VMD
) {
2214 for (hba
= list
; hba
; hba
= hba
->next
)
2215 print_imsm_capability_export(&entry
->orom
);
2218 print_imsm_capability_export(&entry
->orom
);
2226 static int match_home_imsm(struct supertype
*st
, char *homehost
)
2228 /* the imsm metadata format does not specify any host
2229 * identification information. We return -1 since we can never
2230 * confirm nor deny whether a given array is "meant" for this
2231 * host. We rely on compare_super and the 'family_num' fields to
2232 * exclude member disks that do not belong, and we rely on
2233 * mdadm.conf to specify the arrays that should be assembled.
2234 * Auto-assembly may still pick up "foreign" arrays.
2240 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
2242 /* The uuid returned here is used for:
2243 * uuid to put into bitmap file (Create, Grow)
2244 * uuid for backup header when saving critical section (Grow)
2245 * comparing uuids when re-adding a device into an array
2246 * In these cases the uuid required is that of the data-array,
2247 * not the device-set.
2248 * uuid to recognise same set when adding a missing device back
2249 * to an array. This is a uuid for the device-set.
2251 * For each of these we can make do with a truncated
2252 * or hashed uuid rather than the original, as long as
2254 * In each case the uuid required is that of the data-array,
2255 * not the device-set.
2257 /* imsm does not track uuid's so we synthesis one using sha1 on
2258 * - The signature (Which is constant for all imsm array, but no matter)
2259 * - the orig_family_num of the container
2260 * - the index number of the volume
2261 * - the 'serial' number of the volume.
2262 * Hopefully these are all constant.
2264 struct intel_super
*super
= st
->sb
;
2267 struct sha1_ctx ctx
;
2268 struct imsm_dev
*dev
= NULL
;
2271 /* some mdadm versions failed to set ->orig_family_num, in which
2272 * case fall back to ->family_num. orig_family_num will be
2273 * fixed up with the first metadata update.
2275 family_num
= super
->anchor
->orig_family_num
;
2276 if (family_num
== 0)
2277 family_num
= super
->anchor
->family_num
;
2278 sha1_init_ctx(&ctx
);
2279 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
2280 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
2281 if (super
->current_vol
>= 0)
2282 dev
= get_imsm_dev(super
, super
->current_vol
);
2284 __u32 vol
= super
->current_vol
;
2285 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
2286 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
2288 sha1_finish_ctx(&ctx
, buf
);
2289 memcpy(uuid
, buf
, 4*4);
2294 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
2296 __u8
*v
= get_imsm_version(mpb
);
2297 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
2298 char major
[] = { 0, 0, 0 };
2299 char minor
[] = { 0 ,0, 0 };
2300 char patch
[] = { 0, 0, 0 };
2301 char *ver_parse
[] = { major
, minor
, patch
};
2305 while (*v
!= '\0' && v
< end
) {
2306 if (*v
!= '.' && j
< 2)
2307 ver_parse
[i
][j
++] = *v
;
2315 *m
= strtol(minor
, NULL
, 0);
2316 *p
= strtol(patch
, NULL
, 0);
2320 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
2322 /* migr_strip_size when repairing or initializing parity */
2323 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2324 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2326 switch (get_imsm_raid_level(map
)) {
2331 return 128*1024 >> 9;
2335 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
2337 /* migr_strip_size when rebuilding a degraded disk, no idea why
2338 * this is different than migr_strip_size_resync(), but it's good
2341 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2342 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2344 switch (get_imsm_raid_level(map
)) {
2347 if (map
->num_members
% map
->num_domains
== 0)
2348 return 128*1024 >> 9;
2352 return max((__u32
) 64*1024 >> 9, chunk
);
2354 return 128*1024 >> 9;
2358 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
2360 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2361 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2362 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
2363 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
2365 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
2368 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
2370 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2371 int level
= get_imsm_raid_level(lo
);
2373 if (level
== 1 || level
== 10) {
2374 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2376 return hi
->num_domains
;
2378 return num_stripes_per_unit_resync(dev
);
2381 static __u8
imsm_num_data_members(struct imsm_dev
*dev
, int second_map
)
2383 /* named 'imsm_' because raid0, raid1 and raid10
2384 * counter-intuitively have the same number of data disks
2386 struct imsm_map
*map
= get_imsm_map(dev
, second_map
);
2388 switch (get_imsm_raid_level(map
)) {
2390 return map
->num_members
;
2394 return map
->num_members
/2;
2396 return map
->num_members
- 1;
2398 dprintf("unsupported raid level\n");
2403 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
2405 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2406 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2408 switch(get_imsm_raid_level(map
)) {
2411 return chunk
* map
->num_domains
;
2413 return chunk
* map
->num_members
;
2419 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
2421 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2422 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2423 __u32 strip
= block
/ chunk
;
2425 switch (get_imsm_raid_level(map
)) {
2428 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
2429 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
2431 return vol_stripe
* chunk
+ block
% chunk
;
2433 __u32 stripe
= strip
/ (map
->num_members
- 1);
2435 return stripe
* chunk
+ block
% chunk
;
2442 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
2443 struct imsm_dev
*dev
)
2445 /* calculate the conversion factor between per member 'blocks'
2446 * (md/{resync,rebuild}_start) and imsm migration units, return
2447 * 0 for the 'not migrating' and 'unsupported migration' cases
2449 if (!dev
->vol
.migr_state
)
2452 switch (migr_type(dev
)) {
2453 case MIGR_GEN_MIGR
: {
2454 struct migr_record
*migr_rec
= super
->migr_rec
;
2455 return __le32_to_cpu(migr_rec
->blocks_per_unit
);
2460 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2461 __u32 stripes_per_unit
;
2462 __u32 blocks_per_unit
;
2471 /* yes, this is really the translation of migr_units to
2472 * per-member blocks in the 'resync' case
2474 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
2475 migr_chunk
= migr_strip_blocks_resync(dev
);
2476 disks
= imsm_num_data_members(dev
, MAP_0
);
2477 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
2478 stripe
= __le16_to_cpu(map
->blocks_per_strip
) * disks
;
2479 segment
= blocks_per_unit
/ stripe
;
2480 block_rel
= blocks_per_unit
- segment
* stripe
;
2481 parity_depth
= parity_segment_depth(dev
);
2482 block_map
= map_migr_block(dev
, block_rel
);
2483 return block_map
+ parity_depth
* segment
;
2485 case MIGR_REBUILD
: {
2486 __u32 stripes_per_unit
;
2489 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
2490 migr_chunk
= migr_strip_blocks_rebuild(dev
);
2491 return migr_chunk
* stripes_per_unit
;
2493 case MIGR_STATE_CHANGE
:
2499 static int imsm_level_to_layout(int level
)
2507 return ALGORITHM_LEFT_ASYMMETRIC
;
2514 /*******************************************************************************
2515 * Function: read_imsm_migr_rec
2516 * Description: Function reads imsm migration record from last sector of disk
2518 * fd : disk descriptor
2519 * super : metadata info
2523 ******************************************************************************/
2524 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
)
2527 unsigned int sector_size
= super
->sector_size
;
2528 unsigned long long dsize
;
2530 get_dev_size(fd
, NULL
, &dsize
);
2531 if (lseek64(fd
, dsize
- (sector_size
*MIGR_REC_SECTOR_POSITION
),
2533 pr_err("Cannot seek to anchor block: %s\n",
2537 if (read(fd
, super
->migr_rec_buf
,
2538 MIGR_REC_BUF_SECTORS
*sector_size
) !=
2539 MIGR_REC_BUF_SECTORS
*sector_size
) {
2540 pr_err("Cannot read migr record block: %s\n",
2545 if (sector_size
== 4096)
2546 convert_from_4k_imsm_migr_rec(super
);
2552 static struct imsm_dev
*imsm_get_device_during_migration(
2553 struct intel_super
*super
)
2556 struct intel_dev
*dv
;
2558 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
2559 if (is_gen_migration(dv
->dev
))
2565 /*******************************************************************************
2566 * Function: load_imsm_migr_rec
2567 * Description: Function reads imsm migration record (it is stored at the last
2570 * super : imsm internal array info
2571 * info : general array info
2575 * -2 : no migration in progress
2576 ******************************************************************************/
2577 static int load_imsm_migr_rec(struct intel_super
*super
, struct mdinfo
*info
)
2584 struct imsm_dev
*dev
;
2585 struct imsm_map
*map
;
2588 /* find map under migration */
2589 dev
= imsm_get_device_during_migration(super
);
2590 /* nothing to load,no migration in progress?
2596 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
2597 /* read only from one of the first two slots */
2598 if ((sd
->disk
.raid_disk
< 0) ||
2599 (sd
->disk
.raid_disk
> 1))
2602 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
2603 fd
= dev_open(nm
, O_RDONLY
);
2609 map
= get_imsm_map(dev
, MAP_0
);
2610 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
2611 /* skip spare and failed disks
2615 /* read only from one of the first two slots */
2617 slot
= get_imsm_disk_slot(map
, dl
->index
);
2618 if (map
== NULL
|| slot
> 1 || slot
< 0)
2620 sprintf(nm
, "%d:%d", dl
->major
, dl
->minor
);
2621 fd
= dev_open(nm
, O_RDONLY
);
2628 retval
= read_imsm_migr_rec(fd
, super
);
2637 /*******************************************************************************
2638 * function: imsm_create_metadata_checkpoint_update
2639 * Description: It creates update for checkpoint change.
2641 * super : imsm internal array info
2642 * u : pointer to prepared update
2645 * If length is equal to 0, input pointer u contains no update
2646 ******************************************************************************/
2647 static int imsm_create_metadata_checkpoint_update(
2648 struct intel_super
*super
,
2649 struct imsm_update_general_migration_checkpoint
**u
)
2652 int update_memory_size
= 0;
2654 dprintf("(enter)\n");
2660 /* size of all update data without anchor */
2661 update_memory_size
=
2662 sizeof(struct imsm_update_general_migration_checkpoint
);
2664 *u
= xcalloc(1, update_memory_size
);
2666 dprintf("error: cannot get memory\n");
2669 (*u
)->type
= update_general_migration_checkpoint
;
2670 (*u
)->curr_migr_unit
= __le32_to_cpu(super
->migr_rec
->curr_migr_unit
);
2671 dprintf("prepared for %u\n", (*u
)->curr_migr_unit
);
2673 return update_memory_size
;
2676 static void imsm_update_metadata_locally(struct supertype
*st
,
2677 void *buf
, int len
);
2679 /*******************************************************************************
2680 * Function: write_imsm_migr_rec
2681 * Description: Function writes imsm migration record
2682 * (at the last sector of disk)
2684 * super : imsm internal array info
2688 ******************************************************************************/
2689 static int write_imsm_migr_rec(struct supertype
*st
)
2691 struct intel_super
*super
= st
->sb
;
2692 unsigned int sector_size
= super
->sector_size
;
2693 unsigned long long dsize
;
2699 struct imsm_update_general_migration_checkpoint
*u
;
2700 struct imsm_dev
*dev
;
2701 struct imsm_map
*map
;
2703 /* find map under migration */
2704 dev
= imsm_get_device_during_migration(super
);
2705 /* if no migration, write buffer anyway to clear migr_record
2706 * on disk based on first available device
2709 dev
= get_imsm_dev(super
, super
->current_vol
< 0 ? 0 :
2710 super
->current_vol
);
2712 map
= get_imsm_map(dev
, MAP_0
);
2714 if (sector_size
== 4096)
2715 convert_to_4k_imsm_migr_rec(super
);
2716 for (sd
= super
->disks
; sd
; sd
= sd
->next
) {
2719 /* skip failed and spare devices */
2722 /* write to 2 first slots only */
2724 slot
= get_imsm_disk_slot(map
, sd
->index
);
2725 if (map
== NULL
|| slot
> 1 || slot
< 0)
2728 sprintf(nm
, "%d:%d", sd
->major
, sd
->minor
);
2729 fd
= dev_open(nm
, O_RDWR
);
2732 get_dev_size(fd
, NULL
, &dsize
);
2733 if (lseek64(fd
, dsize
- (MIGR_REC_SECTOR_POSITION
*sector_size
),
2735 pr_err("Cannot seek to anchor block: %s\n",
2739 if (write(fd
, super
->migr_rec_buf
,
2740 MIGR_REC_BUF_SECTORS
*sector_size
) !=
2741 MIGR_REC_BUF_SECTORS
*sector_size
) {
2742 pr_err("Cannot write migr record block: %s\n",
2749 if (sector_size
== 4096)
2750 convert_from_4k_imsm_migr_rec(super
);
2751 /* update checkpoint information in metadata */
2752 len
= imsm_create_metadata_checkpoint_update(super
, &u
);
2754 dprintf("imsm: Cannot prepare update\n");
2757 /* update metadata locally */
2758 imsm_update_metadata_locally(st
, u
, len
);
2759 /* and possibly remotely */
2760 if (st
->update_tail
) {
2761 append_metadata_update(st
, u
, len
);
2762 /* during reshape we do all work inside metadata handler
2763 * manage_reshape(), so metadata update has to be triggered
2766 flush_metadata_updates(st
);
2767 st
->update_tail
= &st
->updates
;
2777 #endif /* MDASSEMBLE */
2779 /* spare/missing disks activations are not allowe when
2780 * array/container performs reshape operation, because
2781 * all arrays in container works on the same disks set
2783 int imsm_reshape_blocks_arrays_changes(struct intel_super
*super
)
2786 struct intel_dev
*i_dev
;
2787 struct imsm_dev
*dev
;
2789 /* check whole container
2791 for (i_dev
= super
->devlist
; i_dev
; i_dev
= i_dev
->next
) {
2793 if (is_gen_migration(dev
)) {
2794 /* No repair during any migration in container
2802 static unsigned long long imsm_component_size_aligment_check(int level
,
2804 unsigned int sector_size
,
2805 unsigned long long component_size
)
2807 unsigned int component_size_alligment
;
2809 /* check component size aligment
2811 component_size_alligment
= component_size
% (chunk_size
/sector_size
);
2813 dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alligment = %u\n",
2814 level
, chunk_size
, component_size
,
2815 component_size_alligment
);
2817 if (component_size_alligment
&& (level
!= 1) && (level
!= UnSet
)) {
2818 dprintf("imsm: reported component size alligned from %llu ",
2820 component_size
-= component_size_alligment
;
2821 dprintf_cont("to %llu (%i).\n",
2822 component_size
, component_size_alligment
);
2825 return component_size
;
2828 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
2830 struct intel_super
*super
= st
->sb
;
2831 struct migr_record
*migr_rec
= super
->migr_rec
;
2832 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
2833 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2834 struct imsm_map
*prev_map
= get_imsm_map(dev
, MAP_1
);
2835 struct imsm_map
*map_to_analyse
= map
;
2837 int map_disks
= info
->array
.raid_disks
;
2839 memset(info
, 0, sizeof(*info
));
2841 map_to_analyse
= prev_map
;
2843 dl
= super
->current_disk
;
2845 info
->container_member
= super
->current_vol
;
2846 info
->array
.raid_disks
= map
->num_members
;
2847 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
2848 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
2849 info
->array
.md_minor
= -1;
2850 info
->array
.ctime
= 0;
2851 info
->array
.utime
= 0;
2852 info
->array
.chunk_size
=
2853 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
2854 info
->array
.state
= !dev
->vol
.dirty
;
2855 info
->custom_array_size
= __le32_to_cpu(dev
->size_high
);
2856 info
->custom_array_size
<<= 32;
2857 info
->custom_array_size
|= __le32_to_cpu(dev
->size_low
);
2858 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
2860 if (is_gen_migration(dev
)) {
2861 info
->reshape_active
= 1;
2862 info
->new_level
= get_imsm_raid_level(map
);
2863 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
2864 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
2865 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
2866 if (info
->delta_disks
) {
2867 /* this needs to be applied to every array
2870 info
->reshape_active
= CONTAINER_RESHAPE
;
2872 /* We shape information that we give to md might have to be
2873 * modify to cope with md's requirement for reshaping arrays.
2874 * For example, when reshaping a RAID0, md requires it to be
2875 * presented as a degraded RAID4.
2876 * Also if a RAID0 is migrating to a RAID5 we need to specify
2877 * the array as already being RAID5, but the 'before' layout
2878 * is a RAID4-like layout.
2880 switch (info
->array
.level
) {
2882 switch(info
->new_level
) {
2884 /* conversion is happening as RAID4 */
2885 info
->array
.level
= 4;
2886 info
->array
.raid_disks
+= 1;
2889 /* conversion is happening as RAID5 */
2890 info
->array
.level
= 5;
2891 info
->array
.layout
= ALGORITHM_PARITY_N
;
2892 info
->delta_disks
-= 1;
2895 /* FIXME error message */
2896 info
->array
.level
= UnSet
;
2902 info
->new_level
= UnSet
;
2903 info
->new_layout
= UnSet
;
2904 info
->new_chunk
= info
->array
.chunk_size
;
2905 info
->delta_disks
= 0;
2909 info
->disk
.major
= dl
->major
;
2910 info
->disk
.minor
= dl
->minor
;
2911 info
->disk
.number
= dl
->index
;
2912 info
->disk
.raid_disk
= get_imsm_disk_slot(map_to_analyse
,
2916 info
->data_offset
= pba_of_lba0(map_to_analyse
);
2918 if (info
->array
.level
== 5) {
2919 info
->component_size
= num_data_stripes(map_to_analyse
) *
2920 map_to_analyse
->blocks_per_strip
;
2922 info
->component_size
= blocks_per_member(map_to_analyse
);
2925 info
->component_size
= imsm_component_size_aligment_check(
2927 info
->array
.chunk_size
,
2929 info
->component_size
);
2931 memset(info
->uuid
, 0, sizeof(info
->uuid
));
2932 info
->recovery_start
= MaxSector
;
2934 info
->reshape_progress
= 0;
2935 info
->resync_start
= MaxSector
;
2936 if ((map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
2938 imsm_reshape_blocks_arrays_changes(super
) == 0) {
2939 info
->resync_start
= 0;
2941 if (dev
->vol
.migr_state
) {
2942 switch (migr_type(dev
)) {
2945 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2947 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
2949 info
->resync_start
= blocks_per_unit
* units
;
2952 case MIGR_GEN_MIGR
: {
2953 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2955 __u64 units
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
2956 unsigned long long array_blocks
;
2959 if (__le32_to_cpu(migr_rec
->ascending_migr
) &&
2961 (__le32_to_cpu(migr_rec
->num_migr_units
)-1)) &&
2962 (super
->migr_rec
->rec_status
==
2963 __cpu_to_le32(UNIT_SRC_IN_CP_AREA
)))
2966 info
->reshape_progress
= blocks_per_unit
* units
;
2968 dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n",
2969 (unsigned long long)units
,
2970 (unsigned long long)blocks_per_unit
,
2971 info
->reshape_progress
);
2973 used_disks
= imsm_num_data_members(dev
, MAP_1
);
2974 if (used_disks
> 0) {
2975 array_blocks
= blocks_per_member(map
) *
2977 /* round array size down to closest MB
2979 info
->custom_array_size
= (array_blocks
2980 >> SECT_PER_MB_SHIFT
)
2981 << SECT_PER_MB_SHIFT
;
2985 /* we could emulate the checkpointing of
2986 * 'sync_action=check' migrations, but for now
2987 * we just immediately complete them
2990 /* this is handled by container_content_imsm() */
2991 case MIGR_STATE_CHANGE
:
2992 /* FIXME handle other migrations */
2994 /* we are not dirty, so... */
2995 info
->resync_start
= MaxSector
;
2999 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
3000 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
3002 info
->array
.major_version
= -1;
3003 info
->array
.minor_version
= -2;
3004 sprintf(info
->text_version
, "/%s/%d", st
->container_devnm
, info
->container_member
);
3005 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
3006 uuid_from_super_imsm(st
, info
->uuid
);
3010 for (i
=0; i
<map_disks
; i
++) {
3012 if (i
< info
->array
.raid_disks
) {
3013 struct imsm_disk
*dsk
;
3014 j
= get_imsm_disk_idx(dev
, i
, MAP_X
);
3015 dsk
= get_imsm_disk(super
, j
);
3016 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
3023 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
3024 int failed
, int look_in_map
);
3026 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
3030 static void manage_second_map(struct intel_super
*super
, struct imsm_dev
*dev
)
3032 if (is_gen_migration(dev
)) {
3035 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
3037 failed
= imsm_count_failed(super
, dev
, MAP_1
);
3038 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_1
);
3039 if (map2
->map_state
!= map_state
) {
3040 map2
->map_state
= map_state
;
3041 super
->updates_pending
++;
3047 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
3051 for (d
= super
->missing
; d
; d
= d
->next
)
3052 if (d
->index
== index
)
3057 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
3059 struct intel_super
*super
= st
->sb
;
3060 struct imsm_disk
*disk
;
3061 int map_disks
= info
->array
.raid_disks
;
3062 int max_enough
= -1;
3064 struct imsm_super
*mpb
;
3066 if (super
->current_vol
>= 0) {
3067 getinfo_super_imsm_volume(st
, info
, map
);
3070 memset(info
, 0, sizeof(*info
));
3072 /* Set raid_disks to zero so that Assemble will always pull in valid
3075 info
->array
.raid_disks
= 0;
3076 info
->array
.level
= LEVEL_CONTAINER
;
3077 info
->array
.layout
= 0;
3078 info
->array
.md_minor
= -1;
3079 info
->array
.ctime
= 0; /* N/A for imsm */
3080 info
->array
.utime
= 0;
3081 info
->array
.chunk_size
= 0;
3083 info
->disk
.major
= 0;
3084 info
->disk
.minor
= 0;
3085 info
->disk
.raid_disk
= -1;
3086 info
->reshape_active
= 0;
3087 info
->array
.major_version
= -1;
3088 info
->array
.minor_version
= -2;
3089 strcpy(info
->text_version
, "imsm");
3090 info
->safe_mode_delay
= 0;
3091 info
->disk
.number
= -1;
3092 info
->disk
.state
= 0;
3094 info
->recovery_start
= MaxSector
;
3095 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
3097 /* do we have the all the insync disks that we expect? */
3098 mpb
= super
->anchor
;
3100 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3101 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3102 int failed
, enough
, j
, missing
= 0;
3103 struct imsm_map
*map
;
3106 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3107 state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3108 map
= get_imsm_map(dev
, MAP_0
);
3110 /* any newly missing disks?
3111 * (catches single-degraded vs double-degraded)
3113 for (j
= 0; j
< map
->num_members
; j
++) {
3114 __u32 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
3115 __u32 idx
= ord_to_idx(ord
);
3117 if (!(ord
& IMSM_ORD_REBUILD
) &&
3118 get_imsm_missing(super
, idx
)) {
3124 if (state
== IMSM_T_STATE_FAILED
)
3126 else if (state
== IMSM_T_STATE_DEGRADED
&&
3127 (state
!= map
->map_state
|| missing
))
3129 else /* we're normal, or already degraded */
3131 if (is_gen_migration(dev
) && missing
) {
3132 /* during general migration we need all disks
3133 * that process is running on.
3134 * No new missing disk is allowed.
3138 /* no more checks necessary
3142 /* in the missing/failed disk case check to see
3143 * if at least one array is runnable
3145 max_enough
= max(max_enough
, enough
);
3147 dprintf("enough: %d\n", max_enough
);
3148 info
->container_enough
= max_enough
;
3151 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
3153 disk
= &super
->disks
->disk
;
3154 info
->data_offset
= total_blocks(&super
->disks
->disk
) - reserved
;
3155 info
->component_size
= reserved
;
3156 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
3157 /* we don't change info->disk.raid_disk here because
3158 * this state will be finalized in mdmon after we have
3159 * found the 'most fresh' version of the metadata
3161 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3162 info
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
3165 /* only call uuid_from_super_imsm when this disk is part of a populated container,
3166 * ->compare_super may have updated the 'num_raid_devs' field for spares
3168 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
3169 uuid_from_super_imsm(st
, info
->uuid
);
3171 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
3173 /* I don't know how to compute 'map' on imsm, so use safe default */
3176 for (i
= 0; i
< map_disks
; i
++)
3182 /* allocates memory and fills disk in mdinfo structure
3183 * for each disk in array */
3184 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
3186 struct mdinfo
*mddev
;
3187 struct intel_super
*super
= st
->sb
;
3188 struct imsm_disk
*disk
;
3191 if (!super
|| !super
->disks
)
3194 mddev
= xcalloc(1, sizeof(*mddev
));
3198 tmp
= xcalloc(1, sizeof(*tmp
));
3200 tmp
->next
= mddev
->devs
;
3202 tmp
->disk
.number
= count
++;
3203 tmp
->disk
.major
= dl
->major
;
3204 tmp
->disk
.minor
= dl
->minor
;
3205 tmp
->disk
.state
= is_configured(disk
) ?
3206 (1 << MD_DISK_ACTIVE
) : 0;
3207 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3208 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
3209 tmp
->disk
.raid_disk
= -1;
3215 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
3216 char *update
, char *devname
, int verbose
,
3217 int uuid_set
, char *homehost
)
3219 /* For 'assemble' and 'force' we need to return non-zero if any
3220 * change was made. For others, the return value is ignored.
3221 * Update options are:
3222 * force-one : This device looks a bit old but needs to be included,
3223 * update age info appropriately.
3224 * assemble: clear any 'faulty' flag to allow this device to
3226 * force-array: Array is degraded but being forced, mark it clean
3227 * if that will be needed to assemble it.
3229 * newdev: not used ????
3230 * grow: Array has gained a new device - this is currently for
3232 * resync: mark as dirty so a resync will happen.
3233 * name: update the name - preserving the homehost
3234 * uuid: Change the uuid of the array to match watch is given
3236 * Following are not relevant for this imsm:
3237 * sparc2.2 : update from old dodgey metadata
3238 * super-minor: change the preferred_minor number
3239 * summaries: update redundant counters.
3240 * homehost: update the recorded homehost
3241 * _reshape_progress: record new reshape_progress position.
3244 struct intel_super
*super
= st
->sb
;
3245 struct imsm_super
*mpb
;
3247 /* we can only update container info */
3248 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
3251 mpb
= super
->anchor
;
3253 if (strcmp(update
, "uuid") == 0) {
3254 /* We take this to mean that the family_num should be updated.
3255 * However that is much smaller than the uuid so we cannot really
3256 * allow an explicit uuid to be given. And it is hard to reliably
3258 * So if !uuid_set we know the current uuid is random and just used
3259 * the first 'int' and copy it to the other 3 positions.
3260 * Otherwise we require the 4 'int's to be the same as would be the
3261 * case if we are using a random uuid. So an explicit uuid will be
3262 * accepted as long as all for ints are the same... which shouldn't hurt
3265 info
->uuid
[1] = info
->uuid
[2] = info
->uuid
[3] = info
->uuid
[0];
3268 if (info
->uuid
[0] != info
->uuid
[1] ||
3269 info
->uuid
[1] != info
->uuid
[2] ||
3270 info
->uuid
[2] != info
->uuid
[3])
3276 mpb
->orig_family_num
= info
->uuid
[0];
3277 } else if (strcmp(update
, "assemble") == 0)
3282 /* successful update? recompute checksum */
3284 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
3289 static size_t disks_to_mpb_size(int disks
)
3293 size
= sizeof(struct imsm_super
);
3294 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
3295 size
+= 2 * sizeof(struct imsm_dev
);
3296 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
3297 size
+= (4 - 2) * sizeof(struct imsm_map
);
3298 /* 4 possible disk_ord_tbl's */
3299 size
+= 4 * (disks
- 1) * sizeof(__u32
);
3304 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
,
3305 unsigned long long data_offset
)
3307 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
3310 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
3313 static void free_devlist(struct intel_super
*super
)
3315 struct intel_dev
*dv
;
3317 while (super
->devlist
) {
3318 dv
= super
->devlist
->next
;
3319 free(super
->devlist
->dev
);
3320 free(super
->devlist
);
3321 super
->devlist
= dv
;
3325 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
3327 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
3330 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
3334 * 0 same, or first was empty, and second was copied
3335 * 1 second had wrong number
3337 * 3 wrong other info
3339 struct intel_super
*first
= st
->sb
;
3340 struct intel_super
*sec
= tst
->sb
;
3347 /* in platform dependent environment test if the disks
3348 * use the same Intel hba
3349 * If not on Intel hba at all, allow anything.
3351 if (!check_env("IMSM_NO_PLATFORM") && first
->hba
&& sec
->hba
) {
3352 if (first
->hba
->type
!= sec
->hba
->type
) {
3354 "HBAs of devices do not match %s != %s\n",
3355 get_sys_dev_type(first
->hba
->type
),
3356 get_sys_dev_type(sec
->hba
->type
));
3359 if (first
->orom
!= sec
->orom
) {
3361 "HBAs of devices do not match %s != %s\n",
3362 first
->hba
->pci_id
, sec
->hba
->pci_id
);
3367 /* if an anchor does not have num_raid_devs set then it is a free
3370 if (first
->anchor
->num_raid_devs
> 0 &&
3371 sec
->anchor
->num_raid_devs
> 0) {
3372 /* Determine if these disks might ever have been
3373 * related. Further disambiguation can only take place
3374 * in load_super_imsm_all
3376 __u32 first_family
= first
->anchor
->orig_family_num
;
3377 __u32 sec_family
= sec
->anchor
->orig_family_num
;
3379 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
3380 MAX_SIGNATURE_LENGTH
) != 0)
3383 if (first_family
== 0)
3384 first_family
= first
->anchor
->family_num
;
3385 if (sec_family
== 0)
3386 sec_family
= sec
->anchor
->family_num
;
3388 if (first_family
!= sec_family
)
3393 /* if 'first' is a spare promote it to a populated mpb with sec's
3396 if (first
->anchor
->num_raid_devs
== 0 &&
3397 sec
->anchor
->num_raid_devs
> 0) {
3399 struct intel_dev
*dv
;
3400 struct imsm_dev
*dev
;
3402 /* we need to copy raid device info from sec if an allocation
3403 * fails here we don't associate the spare
3405 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
3406 dv
= xmalloc(sizeof(*dv
));
3407 dev
= xmalloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
3410 dv
->next
= first
->devlist
;
3411 first
->devlist
= dv
;
3413 if (i
< sec
->anchor
->num_raid_devs
) {
3414 /* allocation failure */
3415 free_devlist(first
);
3416 pr_err("imsm: failed to associate spare\n");
3419 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
3420 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
3421 first
->anchor
->family_num
= sec
->anchor
->family_num
;
3422 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
3423 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
3424 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
3430 static void fd2devname(int fd
, char *name
)
3434 char dname
[PATH_MAX
];
3439 if (fstat(fd
, &st
) != 0)
3441 sprintf(path
, "/sys/dev/block/%d:%d",
3442 major(st
.st_rdev
), minor(st
.st_rdev
));
3444 rv
= readlink(path
, dname
, sizeof(dname
)-1);
3449 nm
= strrchr(dname
, '/');
3452 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
3456 static int nvme_get_serial(int fd
, void *buf
, size_t buf_len
)
3459 char *name
= fd2kname(fd
);
3464 if (strncmp(name
, "nvme", 4) != 0)
3467 snprintf(path
, sizeof(path
) - 1, "/sys/block/%s/device/serial", name
);
3469 return load_sys(path
, buf
, buf_len
);
3472 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
3474 static int imsm_read_serial(int fd
, char *devname
,
3475 __u8 serial
[MAX_RAID_SERIAL_LEN
])
3484 memset(buf
, 0, sizeof(buf
));
3486 rv
= nvme_get_serial(fd
, buf
, sizeof(buf
));
3489 rv
= scsi_get_serial(fd
, buf
, sizeof(buf
));
3491 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
3492 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3493 fd2devname(fd
, (char *) serial
);
3499 pr_err("Failed to retrieve serial for %s\n",
3504 /* trim all whitespace and non-printable characters and convert
3507 for (i
= 0, dest
= buf
; i
< sizeof(buf
) && buf
[i
]; i
++) {
3510 /* ':' is reserved for use in placeholder serial
3511 * numbers for missing disks
3522 /* truncate leading characters */
3523 if (len
> MAX_RAID_SERIAL_LEN
) {
3524 dest
+= len
- MAX_RAID_SERIAL_LEN
;
3525 len
= MAX_RAID_SERIAL_LEN
;
3528 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3529 memcpy(serial
, dest
, len
);
3534 static int serialcmp(__u8
*s1
, __u8
*s2
)
3536 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
3539 static void serialcpy(__u8
*dest
, __u8
*src
)
3541 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
3544 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
3548 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3549 if (serialcmp(dl
->serial
, serial
) == 0)
3555 static struct imsm_disk
*
3556 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
3560 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3561 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3563 if (serialcmp(disk
->serial
, serial
) == 0) {
3574 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3576 struct imsm_disk
*disk
;
3581 __u8 serial
[MAX_RAID_SERIAL_LEN
];
3583 rv
= imsm_read_serial(fd
, devname
, serial
);
3588 dl
= xcalloc(1, sizeof(*dl
));
3591 dl
->major
= major(stb
.st_rdev
);
3592 dl
->minor
= minor(stb
.st_rdev
);
3593 dl
->next
= super
->disks
;
3594 dl
->fd
= keep_fd
? fd
: -1;
3595 assert(super
->disks
== NULL
);
3597 serialcpy(dl
->serial
, serial
);
3600 fd2devname(fd
, name
);
3602 dl
->devname
= xstrdup(devname
);
3604 dl
->devname
= xstrdup(name
);
3606 /* look up this disk's index in the current anchor */
3607 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
3610 /* only set index on disks that are a member of a
3611 * populated contianer, i.e. one with raid_devs
3613 if (is_failed(&dl
->disk
))
3615 else if (is_spare(&dl
->disk
))
3623 /* When migrating map0 contains the 'destination' state while map1
3624 * contains the current state. When not migrating map0 contains the
3625 * current state. This routine assumes that map[0].map_state is set to
3626 * the current array state before being called.
3628 * Migration is indicated by one of the following states
3629 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
3630 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
3631 * map1state=unitialized)
3632 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
3634 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
3635 * map1state=degraded)
3636 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
3639 static void migrate(struct imsm_dev
*dev
, struct intel_super
*super
,
3640 __u8 to_state
, int migr_type
)
3642 struct imsm_map
*dest
;
3643 struct imsm_map
*src
= get_imsm_map(dev
, MAP_0
);
3645 dev
->vol
.migr_state
= 1;
3646 set_migr_type(dev
, migr_type
);
3647 dev
->vol
.curr_migr_unit
= 0;
3648 dest
= get_imsm_map(dev
, MAP_1
);
3650 /* duplicate and then set the target end state in map[0] */
3651 memcpy(dest
, src
, sizeof_imsm_map(src
));
3652 if (migr_type
== MIGR_REBUILD
|| migr_type
== MIGR_GEN_MIGR
) {
3656 for (i
= 0; i
< src
->num_members
; i
++) {
3657 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
3658 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
3662 if (migr_type
== MIGR_GEN_MIGR
)
3663 /* Clear migration record */
3664 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
3666 src
->map_state
= to_state
;
3669 static void end_migration(struct imsm_dev
*dev
, struct intel_super
*super
,
3672 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3673 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
== 0 ?
3677 /* merge any IMSM_ORD_REBUILD bits that were not successfully
3678 * completed in the last migration.
3680 * FIXME add support for raid-level-migration
3682 if (map_state
!= map
->map_state
&& (is_gen_migration(dev
) == 0) &&
3683 prev
->map_state
!= IMSM_T_STATE_UNINITIALIZED
) {
3684 /* when final map state is other than expected
3685 * merge maps (not for migration)
3689 for (i
= 0; i
< prev
->num_members
; i
++)
3690 for (j
= 0; j
< map
->num_members
; j
++)
3691 /* during online capacity expansion
3692 * disks position can be changed
3693 * if takeover is used
3695 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
3696 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
3697 map
->disk_ord_tbl
[j
] |=
3698 prev
->disk_ord_tbl
[i
];
3701 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3702 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3705 dev
->vol
.migr_state
= 0;
3706 set_migr_type(dev
, 0);
3707 dev
->vol
.curr_migr_unit
= 0;
3708 map
->map_state
= map_state
;
3712 static int parse_raid_devices(struct intel_super
*super
)
3715 struct imsm_dev
*dev_new
;
3716 size_t len
, len_migr
;
3718 size_t space_needed
= 0;
3719 struct imsm_super
*mpb
= super
->anchor
;
3721 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3722 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3723 struct intel_dev
*dv
;
3725 len
= sizeof_imsm_dev(dev_iter
, 0);
3726 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
3728 space_needed
+= len_migr
- len
;
3730 dv
= xmalloc(sizeof(*dv
));
3731 if (max_len
< len_migr
)
3733 if (max_len
> len_migr
)
3734 space_needed
+= max_len
- len_migr
;
3735 dev_new
= xmalloc(max_len
);
3736 imsm_copy_dev(dev_new
, dev_iter
);
3739 dv
->next
= super
->devlist
;
3740 super
->devlist
= dv
;
3743 /* ensure that super->buf is large enough when all raid devices
3746 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
3749 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
,
3750 super
->sector_size
);
3751 if (posix_memalign(&buf
, MAX_SECTOR_SIZE
, len
) != 0)
3754 memcpy(buf
, super
->buf
, super
->len
);
3755 memset(buf
+ super
->len
, 0, len
- super
->len
);
3764 /* retrieve a pointer to the bbm log which starts after all raid devices */
3765 struct bbm_log
*__get_imsm_bbm_log(struct imsm_super
*mpb
)
3769 if (__le32_to_cpu(mpb
->bbm_log_size
)) {
3771 ptr
+= mpb
->mpb_size
- __le32_to_cpu(mpb
->bbm_log_size
);
3777 /*******************************************************************************
3778 * Function: check_mpb_migr_compatibility
3779 * Description: Function checks for unsupported migration features:
3780 * - migration optimization area (pba_of_lba0)
3781 * - descending reshape (ascending_migr)
3783 * super : imsm metadata information
3785 * 0 : migration is compatible
3786 * -1 : migration is not compatible
3787 ******************************************************************************/
3788 int check_mpb_migr_compatibility(struct intel_super
*super
)
3790 struct imsm_map
*map0
, *map1
;
3791 struct migr_record
*migr_rec
= super
->migr_rec
;
3794 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3795 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3798 dev_iter
->vol
.migr_state
== 1 &&
3799 dev_iter
->vol
.migr_type
== MIGR_GEN_MIGR
) {
3800 /* This device is migrating */
3801 map0
= get_imsm_map(dev_iter
, MAP_0
);
3802 map1
= get_imsm_map(dev_iter
, MAP_1
);
3803 if (pba_of_lba0(map0
) != pba_of_lba0(map1
))
3804 /* migration optimization area was used */
3806 if (migr_rec
->ascending_migr
== 0
3807 && migr_rec
->dest_depth_per_unit
> 0)
3808 /* descending reshape not supported yet */
3815 static void __free_imsm(struct intel_super
*super
, int free_disks
);
3817 /* load_imsm_mpb - read matrix metadata
3818 * allocates super->mpb to be freed by free_imsm
3820 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
3822 unsigned long long dsize
;
3823 unsigned long long sectors
;
3824 unsigned int sector_size
= super
->sector_size
;
3826 struct imsm_super
*anchor
;
3829 get_dev_size(fd
, NULL
, &dsize
);
3830 if (dsize
< 2*sector_size
) {
3832 pr_err("%s: device to small for imsm\n",
3837 if (lseek64(fd
, dsize
- (sector_size
* 2), SEEK_SET
) < 0) {
3839 pr_err("Cannot seek to anchor block on %s: %s\n",
3840 devname
, strerror(errno
));
3844 if (posix_memalign((void **)&anchor
, sector_size
, sector_size
) != 0) {
3846 pr_err("Failed to allocate imsm anchor buffer on %s\n", devname
);
3849 if (read(fd
, anchor
, sector_size
) != sector_size
) {
3851 pr_err("Cannot read anchor block on %s: %s\n",
3852 devname
, strerror(errno
));
3857 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
3859 pr_err("no IMSM anchor on %s\n", devname
);
3864 __free_imsm(super
, 0);
3865 /* reload capability and hba */
3867 /* capability and hba must be updated with new super allocation */
3868 find_intel_hba_capability(fd
, super
, devname
);
3869 super
->len
= ROUND_UP(anchor
->mpb_size
, sector_size
);
3870 if (posix_memalign(&super
->buf
, MAX_SECTOR_SIZE
, super
->len
) != 0) {
3872 pr_err("unable to allocate %zu byte mpb buffer\n",
3877 memcpy(super
->buf
, anchor
, sector_size
);
3879 sectors
= mpb_sectors(anchor
, sector_size
) - 1;
3882 if (posix_memalign(&super
->migr_rec_buf
, sector_size
,
3883 MIGR_REC_BUF_SECTORS
*sector_size
) != 0) {
3884 pr_err("could not allocate migr_rec buffer\n");
3888 super
->clean_migration_record_by_mdmon
= 0;
3891 check_sum
= __gen_imsm_checksum(super
->anchor
);
3892 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3894 pr_err("IMSM checksum %x != %x on %s\n",
3896 __le32_to_cpu(super
->anchor
->check_sum
),
3904 /* read the extended mpb */
3905 if (lseek64(fd
, dsize
- (sector_size
* (2 + sectors
)), SEEK_SET
) < 0) {
3907 pr_err("Cannot seek to extended mpb on %s: %s\n",
3908 devname
, strerror(errno
));
3912 if ((unsigned int)read(fd
, super
->buf
+ sector_size
,
3913 super
->len
- sector_size
) != super
->len
- sector_size
) {
3915 pr_err("Cannot read extended mpb on %s: %s\n",
3916 devname
, strerror(errno
));
3920 check_sum
= __gen_imsm_checksum(super
->anchor
);
3921 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3923 pr_err("IMSM checksum %x != %x on %s\n",
3924 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
3929 /* FIXME the BBM log is disk specific so we cannot use this global
3930 * buffer for all disks. Ok for now since we only look at the global
3931 * bbm_log_size parameter to gate assembly
3933 super
->bbm_log
= __get_imsm_bbm_log(super
->anchor
);
3938 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
);
3940 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
3941 static void clear_hi(struct intel_super
*super
)
3943 struct imsm_super
*mpb
= super
->anchor
;
3945 if (mpb
->attributes
& MPB_ATTRIB_2TB_DISK
)
3947 for (i
= 0; i
< mpb
->num_disks
; ++i
) {
3948 struct imsm_disk
*disk
= &mpb
->disk
[i
];
3949 disk
->total_blocks_hi
= 0;
3951 for (i
= 0; i
< mpb
->num_raid_devs
; ++i
) {
3952 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3955 for (n
= 0; n
< 2; ++n
) {
3956 struct imsm_map
*map
= get_imsm_map(dev
, n
);
3959 map
->pba_of_lba0_hi
= 0;
3960 map
->blocks_per_member_hi
= 0;
3961 map
->num_data_stripes_hi
= 0;
3967 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3971 err
= load_imsm_mpb(fd
, super
, devname
);
3974 if (super
->sector_size
== 4096)
3975 convert_from_4k(super
);
3976 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
3979 err
= parse_raid_devices(super
);
3984 static void __free_imsm_disk(struct dl
*d
)
3996 static void free_imsm_disks(struct intel_super
*super
)
4000 while (super
->disks
) {
4002 super
->disks
= d
->next
;
4003 __free_imsm_disk(d
);
4005 while (super
->disk_mgmt_list
) {
4006 d
= super
->disk_mgmt_list
;
4007 super
->disk_mgmt_list
= d
->next
;
4008 __free_imsm_disk(d
);
4010 while (super
->missing
) {
4012 super
->missing
= d
->next
;
4013 __free_imsm_disk(d
);
4018 /* free all the pieces hanging off of a super pointer */
4019 static void __free_imsm(struct intel_super
*super
, int free_disks
)
4021 struct intel_hba
*elem
, *next
;
4027 /* unlink capability description */
4029 if (super
->migr_rec_buf
) {
4030 free(super
->migr_rec_buf
);
4031 super
->migr_rec_buf
= NULL
;
4034 free_imsm_disks(super
);
4035 free_devlist(super
);
4039 free((void *)elem
->path
);
4047 static void free_imsm(struct intel_super
*super
)
4049 __free_imsm(super
, 1);
4053 static void free_super_imsm(struct supertype
*st
)
4055 struct intel_super
*super
= st
->sb
;
4064 static struct intel_super
*alloc_super(void)
4066 struct intel_super
*super
= xcalloc(1, sizeof(*super
));
4068 super
->current_vol
= -1;
4069 super
->create_offset
= ~((unsigned long long) 0);
4074 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
4076 static int find_intel_hba_capability(int fd
, struct intel_super
*super
, char *devname
)
4078 struct sys_dev
*hba_name
;
4081 if (fd
< 0 || check_env("IMSM_NO_PLATFORM")) {
4086 hba_name
= find_disk_attached_hba(fd
, NULL
);
4089 pr_err("%s is not attached to Intel(R) RAID controller.\n",
4093 rv
= attach_hba_to_super(super
, hba_name
);
4096 struct intel_hba
*hba
= super
->hba
;
4098 pr_err("%s is attached to Intel(R) %s %s (%s),\n"
4099 " but the container is assigned to Intel(R) %s %s (",
4101 get_sys_dev_type(hba_name
->type
),
4102 hba_name
->type
== SYS_DEV_VMD
? "domain" : "RAID controller",
4103 hba_name
->pci_id
? : "Err!",
4104 get_sys_dev_type(super
->hba
->type
),
4105 hba
->type
== SYS_DEV_VMD
? "domain" : "RAID controller");
4108 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
4110 fprintf(stderr
, ", ");
4113 fprintf(stderr
, ").\n"
4114 " Mixing devices attached to different %s is not allowed.\n",
4115 hba_name
->type
== SYS_DEV_VMD
? "VMD domains" : "controllers");
4119 super
->orom
= find_imsm_capability(hba_name
);
4126 /* find_missing - helper routine for load_super_imsm_all that identifies
4127 * disks that have disappeared from the system. This routine relies on
4128 * the mpb being uptodate, which it is at load time.
4130 static int find_missing(struct intel_super
*super
)
4133 struct imsm_super
*mpb
= super
->anchor
;
4135 struct imsm_disk
*disk
;
4137 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4138 disk
= __get_imsm_disk(mpb
, i
);
4139 dl
= serial_to_dl(disk
->serial
, super
);
4143 dl
= xmalloc(sizeof(*dl
));
4147 dl
->devname
= xstrdup("missing");
4149 serialcpy(dl
->serial
, disk
->serial
);
4152 dl
->next
= super
->missing
;
4153 super
->missing
= dl
;
4160 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
4162 struct intel_disk
*idisk
= disk_list
;
4165 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
4167 idisk
= idisk
->next
;
4173 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
4174 struct intel_super
*super
,
4175 struct intel_disk
**disk_list
)
4177 struct imsm_disk
*d
= &super
->disks
->disk
;
4178 struct imsm_super
*mpb
= super
->anchor
;
4181 for (i
= 0; i
< tbl_size
; i
++) {
4182 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
4183 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
4185 if (tbl_mpb
->family_num
== mpb
->family_num
) {
4186 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
4187 dprintf("mpb from %d:%d matches %d:%d\n",
4188 super
->disks
->major
,
4189 super
->disks
->minor
,
4190 table
[i
]->disks
->major
,
4191 table
[i
]->disks
->minor
);
4195 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
4196 is_configured(d
) == is_configured(tbl_d
)) &&
4197 tbl_mpb
->generation_num
< mpb
->generation_num
) {
4198 /* current version of the mpb is a
4199 * better candidate than the one in
4200 * super_table, but copy over "cross
4201 * generational" status
4203 struct intel_disk
*idisk
;
4205 dprintf("mpb from %d:%d replaces %d:%d\n",
4206 super
->disks
->major
,
4207 super
->disks
->minor
,
4208 table
[i
]->disks
->major
,
4209 table
[i
]->disks
->minor
);
4211 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
4212 if (idisk
&& is_failed(&idisk
->disk
))
4213 tbl_d
->status
|= FAILED_DISK
;
4216 struct intel_disk
*idisk
;
4217 struct imsm_disk
*disk
;
4219 /* tbl_mpb is more up to date, but copy
4220 * over cross generational status before
4223 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
4224 if (disk
&& is_failed(disk
))
4225 d
->status
|= FAILED_DISK
;
4227 idisk
= disk_list_get(d
->serial
, *disk_list
);
4230 if (disk
&& is_configured(disk
))
4231 idisk
->disk
.status
|= CONFIGURED_DISK
;
4234 dprintf("mpb from %d:%d prefer %d:%d\n",
4235 super
->disks
->major
,
4236 super
->disks
->minor
,
4237 table
[i
]->disks
->major
,
4238 table
[i
]->disks
->minor
);
4246 table
[tbl_size
++] = super
;
4250 /* update/extend the merged list of imsm_disk records */
4251 for (j
= 0; j
< mpb
->num_disks
; j
++) {
4252 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
4253 struct intel_disk
*idisk
;
4255 idisk
= disk_list_get(disk
->serial
, *disk_list
);
4257 idisk
->disk
.status
|= disk
->status
;
4258 if (is_configured(&idisk
->disk
) ||
4259 is_failed(&idisk
->disk
))
4260 idisk
->disk
.status
&= ~(SPARE_DISK
);
4262 idisk
= xcalloc(1, sizeof(*idisk
));
4263 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
4264 idisk
->disk
= *disk
;
4265 idisk
->next
= *disk_list
;
4269 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
4276 static struct intel_super
*
4277 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
4280 struct imsm_super
*mpb
= super
->anchor
;
4284 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4285 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
4286 struct intel_disk
*idisk
;
4288 idisk
= disk_list_get(disk
->serial
, disk_list
);
4290 if (idisk
->owner
== owner
||
4291 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
4294 dprintf("'%.16s' owner %d != %d\n",
4295 disk
->serial
, idisk
->owner
,
4298 dprintf("unknown disk %x [%d]: %.16s\n",
4299 __le32_to_cpu(mpb
->family_num
), i
,
4305 if (ok_count
== mpb
->num_disks
)
4310 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
4312 struct intel_super
*s
;
4314 for (s
= super_list
; s
; s
= s
->next
) {
4315 if (family_num
!= s
->anchor
->family_num
)
4317 pr_err("Conflict, offlining family %#x on '%s'\n",
4318 __le32_to_cpu(family_num
), s
->disks
->devname
);
4322 static struct intel_super
*
4323 imsm_thunderdome(struct intel_super
**super_list
, int len
)
4325 struct intel_super
*super_table
[len
];
4326 struct intel_disk
*disk_list
= NULL
;
4327 struct intel_super
*champion
, *spare
;
4328 struct intel_super
*s
, **del
;
4333 memset(super_table
, 0, sizeof(super_table
));
4334 for (s
= *super_list
; s
; s
= s
->next
)
4335 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
4337 for (i
= 0; i
< tbl_size
; i
++) {
4338 struct imsm_disk
*d
;
4339 struct intel_disk
*idisk
;
4340 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
4343 d
= &s
->disks
->disk
;
4345 /* 'd' must appear in merged disk list for its
4346 * configuration to be valid
4348 idisk
= disk_list_get(d
->serial
, disk_list
);
4349 if (idisk
&& idisk
->owner
== i
)
4350 s
= validate_members(s
, disk_list
, i
);
4355 dprintf("marking family: %#x from %d:%d offline\n",
4357 super_table
[i
]->disks
->major
,
4358 super_table
[i
]->disks
->minor
);
4362 /* This is where the mdadm implementation differs from the Windows
4363 * driver which has no strict concept of a container. We can only
4364 * assemble one family from a container, so when returning a prodigal
4365 * array member to this system the code will not be able to disambiguate
4366 * the container contents that should be assembled ("foreign" versus
4367 * "local"). It requires user intervention to set the orig_family_num
4368 * to a new value to establish a new container. The Windows driver in
4369 * this situation fixes up the volume name in place and manages the
4370 * foreign array as an independent entity.
4375 for (i
= 0; i
< tbl_size
; i
++) {
4376 struct intel_super
*tbl_ent
= super_table
[i
];
4382 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
4387 if (s
&& !is_spare
) {
4388 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
4390 } else if (!s
&& !is_spare
)
4403 pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n",
4404 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
4406 /* collect all dl's onto 'champion', and update them to
4407 * champion's version of the status
4409 for (s
= *super_list
; s
; s
= s
->next
) {
4410 struct imsm_super
*mpb
= champion
->anchor
;
4411 struct dl
*dl
= s
->disks
;
4416 mpb
->attributes
|= s
->anchor
->attributes
& MPB_ATTRIB_2TB_DISK
;
4418 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4419 struct imsm_disk
*disk
;
4421 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
4424 /* only set index on disks that are a member of
4425 * a populated contianer, i.e. one with
4428 if (is_failed(&dl
->disk
))
4430 else if (is_spare(&dl
->disk
))
4436 if (i
>= mpb
->num_disks
) {
4437 struct intel_disk
*idisk
;
4439 idisk
= disk_list_get(dl
->serial
, disk_list
);
4440 if (idisk
&& is_spare(&idisk
->disk
) &&
4441 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
4449 dl
->next
= champion
->disks
;
4450 champion
->disks
= dl
;
4454 /* delete 'champion' from super_list */
4455 for (del
= super_list
; *del
; ) {
4456 if (*del
== champion
) {
4457 *del
= (*del
)->next
;
4460 del
= &(*del
)->next
;
4462 champion
->next
= NULL
;
4466 struct intel_disk
*idisk
= disk_list
;
4468 disk_list
= disk_list
->next
;
4476 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
);
4477 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
4478 int major
, int minor
, int keep_fd
);
4480 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4481 int *max
, int keep_fd
);
4483 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
4484 char *devname
, struct md_list
*devlist
,
4487 struct intel_super
*super_list
= NULL
;
4488 struct intel_super
*super
= NULL
;
4493 /* 'fd' is an opened container */
4494 err
= get_sra_super_block(fd
, &super_list
, devname
, &i
, keep_fd
);
4496 /* get super block from devlist devices */
4497 err
= get_devlist_super_block(devlist
, &super_list
, &i
, keep_fd
);
4500 /* all mpbs enter, maybe one leaves */
4501 super
= imsm_thunderdome(&super_list
, i
);
4507 if (find_missing(super
) != 0) {
4513 /* load migration record */
4514 err
= load_imsm_migr_rec(super
, NULL
);
4516 /* migration is in progress,
4517 * but migr_rec cannot be loaded,
4523 /* Check migration compatibility */
4524 if (err
== 0 && check_mpb_migr_compatibility(super
) != 0) {
4525 pr_err("Unsupported migration detected");
4527 fprintf(stderr
, " on %s\n", devname
);
4529 fprintf(stderr
, " (IMSM).\n");
4538 while (super_list
) {
4539 struct intel_super
*s
= super_list
;
4541 super_list
= super_list
->next
;
4550 strcpy(st
->container_devnm
, fd2devnm(fd
));
4552 st
->container_devnm
[0] = 0;
4553 if (err
== 0 && st
->ss
== NULL
) {
4554 st
->ss
= &super_imsm
;
4555 st
->minor_version
= 0;
4556 st
->max_devs
= IMSM_MAX_DEVICES
;
4562 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4563 int *max
, int keep_fd
)
4565 struct md_list
*tmpdev
;
4569 for (i
= 0, tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
4570 if (tmpdev
->used
!= 1)
4572 if (tmpdev
->container
== 1) {
4574 int fd
= dev_open(tmpdev
->devname
, O_RDONLY
|O_EXCL
);
4576 pr_err("cannot open device %s: %s\n",
4577 tmpdev
->devname
, strerror(errno
));
4581 err
= get_sra_super_block(fd
, super_list
,
4582 tmpdev
->devname
, &lmax
,
4591 int major
= major(tmpdev
->st_rdev
);
4592 int minor
= minor(tmpdev
->st_rdev
);
4593 err
= get_super_block(super_list
,
4610 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
4611 int major
, int minor
, int keep_fd
)
4613 struct intel_super
*s
;
4625 sprintf(nm
, "%d:%d", major
, minor
);
4626 dfd
= dev_open(nm
, O_RDWR
);
4632 get_dev_sector_size(dfd
, NULL
, &s
->sector_size
);
4633 find_intel_hba_capability(dfd
, s
, devname
);
4634 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4636 /* retry the load if we might have raced against mdmon */
4637 if (err
== 3 && devnm
&& mdmon_running(devnm
))
4638 for (retry
= 0; retry
< 3; retry
++) {
4640 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4646 s
->next
= *super_list
;
4654 if (dfd
>= 0 && !keep_fd
)
4661 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
)
4668 sra
= sysfs_read(fd
, NULL
, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
4672 if (sra
->array
.major_version
!= -1 ||
4673 sra
->array
.minor_version
!= -2 ||
4674 strcmp(sra
->text_version
, "imsm") != 0) {
4679 devnm
= fd2devnm(fd
);
4680 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
4681 if (get_super_block(super_list
, devnm
, devname
,
4682 sd
->disk
.major
, sd
->disk
.minor
, keep_fd
) != 0) {
4693 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
4695 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
, NULL
, 1);
4699 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
4701 struct intel_super
*super
;
4705 if (test_partition(fd
))
4706 /* IMSM not allowed on partitions */
4709 free_super_imsm(st
);
4711 super
= alloc_super();
4712 get_dev_sector_size(fd
, NULL
, &super
->sector_size
);
4713 /* Load hba and capabilities if they exist.
4714 * But do not preclude loading metadata in case capabilities or hba are
4715 * non-compliant and ignore_hw_compat is set.
4717 rv
= find_intel_hba_capability(fd
, super
, devname
);
4718 /* no orom/efi or non-intel hba of the disk */
4719 if (rv
!= 0 && st
->ignore_hw_compat
== 0) {
4721 pr_err("No OROM/EFI properties for %s\n", devname
);
4725 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
4727 /* retry the load if we might have raced against mdmon */
4729 struct mdstat_ent
*mdstat
= NULL
;
4730 char *name
= fd2kname(fd
);
4733 mdstat
= mdstat_by_component(name
);
4735 if (mdstat
&& mdmon_running(mdstat
->devnm
) && getpid() != mdmon_pid(mdstat
->devnm
)) {
4736 for (retry
= 0; retry
< 3; retry
++) {
4738 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
4744 free_mdstat(mdstat
);
4749 pr_err("Failed to load all information sections on %s\n", devname
);
4755 if (st
->ss
== NULL
) {
4756 st
->ss
= &super_imsm
;
4757 st
->minor_version
= 0;
4758 st
->max_devs
= IMSM_MAX_DEVICES
;
4761 /* load migration record */
4762 if (load_imsm_migr_rec(super
, NULL
) == 0) {
4763 /* Check for unsupported migration features */
4764 if (check_mpb_migr_compatibility(super
) != 0) {
4765 pr_err("Unsupported migration detected");
4767 fprintf(stderr
, " on %s\n", devname
);
4769 fprintf(stderr
, " (IMSM).\n");
4777 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
4779 if (info
->level
== 1)
4781 return info
->chunk_size
>> 9;
4784 static unsigned long long info_to_blocks_per_member(mdu_array_info_t
*info
,
4785 unsigned long long size
)
4787 if (info
->level
== 1)
4790 return (size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
4793 static void imsm_update_version_info(struct intel_super
*super
)
4795 /* update the version and attributes */
4796 struct imsm_super
*mpb
= super
->anchor
;
4798 struct imsm_dev
*dev
;
4799 struct imsm_map
*map
;
4802 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4803 dev
= get_imsm_dev(super
, i
);
4804 map
= get_imsm_map(dev
, MAP_0
);
4805 if (__le32_to_cpu(dev
->size_high
) > 0)
4806 mpb
->attributes
|= MPB_ATTRIB_2TB
;
4808 /* FIXME detect when an array spans a port multiplier */
4810 mpb
->attributes
|= MPB_ATTRIB_PM
;
4813 if (mpb
->num_raid_devs
> 1 ||
4814 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
4815 version
= MPB_VERSION_ATTRIBS
;
4816 switch (get_imsm_raid_level(map
)) {
4817 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
4818 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
4819 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
4820 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
4823 if (map
->num_members
>= 5)
4824 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
4825 else if (dev
->status
== DEV_CLONE_N_GO
)
4826 version
= MPB_VERSION_CNG
;
4827 else if (get_imsm_raid_level(map
) == 5)
4828 version
= MPB_VERSION_RAID5
;
4829 else if (map
->num_members
>= 3)
4830 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
4831 else if (get_imsm_raid_level(map
) == 1)
4832 version
= MPB_VERSION_RAID1
;
4834 version
= MPB_VERSION_RAID0
;
4836 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
4840 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
4842 struct imsm_super
*mpb
= super
->anchor
;
4843 char *reason
= NULL
;
4846 if (strlen(name
) > MAX_RAID_SERIAL_LEN
)
4847 reason
= "must be 16 characters or less";
4849 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4850 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4852 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
4853 reason
= "already exists";
4858 if (reason
&& !quiet
)
4859 pr_err("imsm volume name %s\n", reason
);
4864 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
4865 unsigned long long size
, char *name
,
4866 char *homehost
, int *uuid
,
4867 long long data_offset
)
4869 /* We are creating a volume inside a pre-existing container.
4870 * so st->sb is already set.
4872 struct intel_super
*super
= st
->sb
;
4873 unsigned int sector_size
= super
->sector_size
;
4874 struct imsm_super
*mpb
= super
->anchor
;
4875 struct intel_dev
*dv
;
4876 struct imsm_dev
*dev
;
4877 struct imsm_vol
*vol
;
4878 struct imsm_map
*map
;
4879 int idx
= mpb
->num_raid_devs
;
4881 unsigned long long array_blocks
;
4882 size_t size_old
, size_new
;
4883 unsigned long long num_data_stripes
;
4885 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
4886 pr_err("This imsm-container already has the maximum of %d volumes\n", super
->orom
->vpa
);
4890 /* ensure the mpb is large enough for the new data */
4891 size_old
= __le32_to_cpu(mpb
->mpb_size
);
4892 size_new
= disks_to_mpb_size(info
->nr_disks
);
4893 if (size_new
> size_old
) {
4895 size_t size_round
= ROUND_UP(size_new
, sector_size
);
4897 if (posix_memalign(&mpb_new
, sector_size
, size_round
) != 0) {
4898 pr_err("could not allocate new mpb\n");
4901 if (posix_memalign(&super
->migr_rec_buf
, sector_size
,
4902 MIGR_REC_BUF_SECTORS
*sector_size
) != 0) {
4903 pr_err("could not allocate migr_rec buffer\n");
4909 memcpy(mpb_new
, mpb
, size_old
);
4912 super
->anchor
= mpb_new
;
4913 mpb
->mpb_size
= __cpu_to_le32(size_new
);
4914 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
4916 super
->current_vol
= idx
;
4918 /* handle 'failed_disks' by either:
4919 * a) create dummy disk entries in the table if this the first
4920 * volume in the array. We add them here as this is the only
4921 * opportunity to add them. add_to_super_imsm_volume()
4922 * handles the non-failed disks and continues incrementing
4924 * b) validate that 'failed_disks' matches the current number
4925 * of missing disks if the container is populated
4927 if (super
->current_vol
== 0) {
4929 for (i
= 0; i
< info
->failed_disks
; i
++) {
4930 struct imsm_disk
*disk
;
4933 disk
= __get_imsm_disk(mpb
, i
);
4934 disk
->status
= CONFIGURED_DISK
| FAILED_DISK
;
4935 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
4936 snprintf((char *) disk
->serial
, MAX_RAID_SERIAL_LEN
,
4939 find_missing(super
);
4944 for (d
= super
->missing
; d
; d
= d
->next
)
4946 if (info
->failed_disks
> missing
) {
4947 pr_err("unable to add 'missing' disk to container\n");
4952 if (!check_name(super
, name
, 0))
4954 dv
= xmalloc(sizeof(*dv
));
4955 dev
= xcalloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
4956 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
4957 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
4958 info
->layout
, info
->chunk_size
,
4960 /* round array size down to closest MB */
4961 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
4963 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
4964 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
4965 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
4967 vol
->migr_state
= 0;
4968 set_migr_type(dev
, MIGR_INIT
);
4969 vol
->dirty
= !info
->state
;
4970 vol
->curr_migr_unit
= 0;
4971 map
= get_imsm_map(dev
, MAP_0
);
4972 set_pba_of_lba0(map
, super
->create_offset
);
4973 set_blocks_per_member(map
, info_to_blocks_per_member(info
, size
));
4974 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
4975 map
->failed_disk_num
= ~0;
4976 if (info
->level
> 0)
4977 map
->map_state
= (info
->state
? IMSM_T_STATE_NORMAL
4978 : IMSM_T_STATE_UNINITIALIZED
);
4980 map
->map_state
= info
->failed_disks
? IMSM_T_STATE_FAILED
:
4981 IMSM_T_STATE_NORMAL
;
4984 if (info
->level
== 1 && info
->raid_disks
> 2) {
4987 pr_err("imsm does not support more than 2 disksin a raid1 volume\n");
4991 map
->raid_level
= info
->level
;
4992 if (info
->level
== 10) {
4993 map
->raid_level
= 1;
4994 map
->num_domains
= info
->raid_disks
/ 2;
4995 } else if (info
->level
== 1)
4996 map
->num_domains
= info
->raid_disks
;
4998 map
->num_domains
= 1;
5000 /* info->size is only int so use the 'size' parameter instead */
5001 num_data_stripes
= (size
* 2) / info_to_blocks_per_strip(info
);
5002 num_data_stripes
/= map
->num_domains
;
5003 set_num_data_stripes(map
, num_data_stripes
);
5005 map
->num_members
= info
->raid_disks
;
5006 for (i
= 0; i
< map
->num_members
; i
++) {
5007 /* initialized in add_to_super */
5008 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
5010 mpb
->num_raid_devs
++;
5013 dv
->index
= super
->current_vol
;
5014 dv
->next
= super
->devlist
;
5015 super
->devlist
= dv
;
5017 imsm_update_version_info(super
);
5022 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
5023 unsigned long long size
, char *name
,
5024 char *homehost
, int *uuid
,
5025 unsigned long long data_offset
)
5027 /* This is primarily called by Create when creating a new array.
5028 * We will then get add_to_super called for each component, and then
5029 * write_init_super called to write it out to each device.
5030 * For IMSM, Create can create on fresh devices or on a pre-existing
5032 * To create on a pre-existing array a different method will be called.
5033 * This one is just for fresh drives.
5035 struct intel_super
*super
;
5036 struct imsm_super
*mpb
;
5040 if (data_offset
!= INVALID_SECTORS
) {
5041 pr_err("data-offset not supported by imsm\n");
5046 return init_super_imsm_volume(st
, info
, size
, name
, homehost
, uuid
,
5050 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
5052 mpb_size
= MAX_SECTOR_SIZE
;
5054 super
= alloc_super();
5056 posix_memalign(&super
->buf
, MAX_SECTOR_SIZE
, mpb_size
) != 0) {
5061 pr_err("could not allocate superblock\n");
5064 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
5065 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
) != 0) {
5066 pr_err("could not allocate migr_rec buffer\n");
5071 memset(super
->buf
, 0, mpb_size
);
5073 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5077 /* zeroing superblock */
5081 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5083 version
= (char *) mpb
->sig
;
5084 strcpy(version
, MPB_SIGNATURE
);
5085 version
+= strlen(MPB_SIGNATURE
);
5086 strcpy(version
, MPB_VERSION_RAID0
);
5092 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
5093 int fd
, char *devname
)
5095 struct intel_super
*super
= st
->sb
;
5096 struct imsm_super
*mpb
= super
->anchor
;
5097 struct imsm_disk
*_disk
;
5098 struct imsm_dev
*dev
;
5099 struct imsm_map
*map
;
5103 dev
= get_imsm_dev(super
, super
->current_vol
);
5104 map
= get_imsm_map(dev
, MAP_0
);
5106 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
5107 pr_err("%s: Cannot add spare devices to IMSM volume\n",
5113 /* we're doing autolayout so grab the pre-marked (in
5114 * validate_geometry) raid_disk
5116 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5117 if (dl
->raiddisk
== dk
->raid_disk
)
5120 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5121 if (dl
->major
== dk
->major
&&
5122 dl
->minor
== dk
->minor
)
5127 pr_err("%s is not a member of the same container\n", devname
);
5131 /* add a pristine spare to the metadata */
5132 if (dl
->index
< 0) {
5133 dl
->index
= super
->anchor
->num_disks
;
5134 super
->anchor
->num_disks
++;
5136 /* Check the device has not already been added */
5137 slot
= get_imsm_disk_slot(map
, dl
->index
);
5139 (get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
) & IMSM_ORD_REBUILD
) == 0) {
5140 pr_err("%s has been included in this array twice\n",
5144 set_imsm_ord_tbl_ent(map
, dk
->raid_disk
, dl
->index
);
5145 dl
->disk
.status
= CONFIGURED_DISK
;
5147 /* update size of 'missing' disks to be at least as large as the
5148 * largest acitve member (we only have dummy missing disks when
5149 * creating the first volume)
5151 if (super
->current_vol
== 0) {
5152 for (df
= super
->missing
; df
; df
= df
->next
) {
5153 if (total_blocks(&dl
->disk
) > total_blocks(&df
->disk
))
5154 set_total_blocks(&df
->disk
, total_blocks(&dl
->disk
));
5155 _disk
= __get_imsm_disk(mpb
, df
->index
);
5160 /* refresh unset/failed slots to point to valid 'missing' entries */
5161 for (df
= super
->missing
; df
; df
= df
->next
)
5162 for (slot
= 0; slot
< mpb
->num_disks
; slot
++) {
5163 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
5165 if ((ord
& IMSM_ORD_REBUILD
) == 0)
5167 set_imsm_ord_tbl_ent(map
, slot
, df
->index
| IMSM_ORD_REBUILD
);
5168 if (is_gen_migration(dev
)) {
5169 struct imsm_map
*map2
= get_imsm_map(dev
,
5171 int slot2
= get_imsm_disk_slot(map2
, df
->index
);
5172 if (slot2
< map2
->num_members
&& slot2
>= 0) {
5173 __u32 ord2
= get_imsm_ord_tbl_ent(dev
,
5176 if ((unsigned)df
->index
==
5178 set_imsm_ord_tbl_ent(map2
,
5184 dprintf("set slot:%d to missing disk:%d\n", slot
, df
->index
);
5188 /* if we are creating the first raid device update the family number */
5189 if (super
->current_vol
== 0) {
5191 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
5193 _disk
= __get_imsm_disk(mpb
, dl
->index
);
5194 if (!_dev
|| !_disk
) {
5195 pr_err("BUG mpb setup error\n");
5201 sum
+= __gen_imsm_checksum(mpb
);
5202 mpb
->family_num
= __cpu_to_le32(sum
);
5203 mpb
->orig_family_num
= mpb
->family_num
;
5205 super
->current_disk
= dl
;
5210 * Function marks disk as spare and restores disk serial
5211 * in case it was previously marked as failed by takeover operation
5213 * -1 : critical error
5214 * 0 : disk is marked as spare but serial is not set
5217 int mark_spare(struct dl
*disk
)
5219 __u8 serial
[MAX_RAID_SERIAL_LEN
];
5226 if (!imsm_read_serial(disk
->fd
, NULL
, serial
)) {
5227 /* Restore disk serial number, because takeover marks disk
5228 * as failed and adds to serial ':0' before it becomes
5231 serialcpy(disk
->serial
, serial
);
5232 serialcpy(disk
->disk
.serial
, serial
);
5235 disk
->disk
.status
= SPARE_DISK
;
5241 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
5242 int fd
, char *devname
,
5243 unsigned long long data_offset
)
5245 struct intel_super
*super
= st
->sb
;
5247 unsigned long long size
;
5248 unsigned int member_sector_size
;
5253 /* If we are on an RAID enabled platform check that the disk is
5254 * attached to the raid controller.
5255 * We do not need to test disks attachment for container based additions,
5256 * they shall be already tested when container was created/assembled.
5258 rv
= find_intel_hba_capability(fd
, super
, devname
);
5259 /* no orom/efi or non-intel hba of the disk */
5261 dprintf("capability: %p fd: %d ret: %d\n",
5262 super
->orom
, fd
, rv
);
5266 if (super
->current_vol
>= 0)
5267 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
5270 dd
= xcalloc(sizeof(*dd
), 1);
5271 dd
->major
= major(stb
.st_rdev
);
5272 dd
->minor
= minor(stb
.st_rdev
);
5273 dd
->devname
= devname
? xstrdup(devname
) : NULL
;
5276 dd
->action
= DISK_ADD
;
5277 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
5279 pr_err("failed to retrieve scsi serial, aborting\n");
5285 if (super
->hba
&& ((super
->hba
->type
== SYS_DEV_NVME
) ||
5286 (super
->hba
->type
== SYS_DEV_VMD
))) {
5288 char *devpath
= diskfd_to_devpath(fd
);
5289 char controller_path
[PATH_MAX
];
5292 pr_err("failed to get devpath, aborting\n");
5299 snprintf(controller_path
, PATH_MAX
-1, "%s/device", devpath
);
5302 if (devpath_to_vendor(controller_path
) == 0x8086) {
5304 * If Intel's NVMe drive has serial ended with
5305 * "-A","-B","-1" or "-2" it means that this is "x8"
5306 * device (double drive on single PCIe card).
5307 * User should be warned about potential data loss.
5309 for (i
= MAX_RAID_SERIAL_LEN
-1; i
> 0; i
--) {
5310 /* Skip empty character at the end */
5311 if (dd
->serial
[i
] == 0)
5314 if (((dd
->serial
[i
] == 'A') ||
5315 (dd
->serial
[i
] == 'B') ||
5316 (dd
->serial
[i
] == '1') ||
5317 (dd
->serial
[i
] == '2')) &&
5318 (dd
->serial
[i
-1] == '-'))
5319 pr_err("\tThe action you are about to take may put your data at risk.\n"
5320 "\tPlease note that x8 devices may consist of two separate x4 devices "
5321 "located on a single PCIe port.\n"
5322 "\tRAID 0 is the only supported configuration for this type of x8 device.\n");
5328 get_dev_size(fd
, NULL
, &size
);
5329 get_dev_sector_size(fd
, NULL
, &member_sector_size
);
5331 if (super
->sector_size
== 0) {
5332 /* this a first device, so sector_size is not set yet */
5333 super
->sector_size
= member_sector_size
;
5334 } else if (member_sector_size
!= super
->sector_size
) {
5335 pr_err("Mixing between different sector size is forbidden, aborting...\n");
5342 /* clear migr_rec when adding disk to container */
5343 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SECTORS
*super
->sector_size
);
5344 if (lseek64(fd
, size
- MIGR_REC_SECTOR_POSITION
*super
->sector_size
,
5346 if (write(fd
, super
->migr_rec_buf
,
5347 MIGR_REC_BUF_SECTORS
*super
->sector_size
) !=
5348 MIGR_REC_BUF_SECTORS
*super
->sector_size
)
5349 perror("Write migr_rec failed");
5353 serialcpy(dd
->disk
.serial
, dd
->serial
);
5354 set_total_blocks(&dd
->disk
, size
);
5355 if (__le32_to_cpu(dd
->disk
.total_blocks_hi
) > 0) {
5356 struct imsm_super
*mpb
= super
->anchor
;
5357 mpb
->attributes
|= MPB_ATTRIB_2TB_DISK
;
5360 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
5361 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
5363 dd
->disk
.scsi_id
= __cpu_to_le32(0);
5365 if (st
->update_tail
) {
5366 dd
->next
= super
->disk_mgmt_list
;
5367 super
->disk_mgmt_list
= dd
;
5369 dd
->next
= super
->disks
;
5371 super
->updates_pending
++;
5377 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
5379 struct intel_super
*super
= st
->sb
;
5382 /* remove from super works only in mdmon - for communication
5383 * manager - monitor. Check if communication memory buffer
5386 if (!st
->update_tail
) {
5387 pr_err("shall be used in mdmon context only\n");
5390 dd
= xcalloc(1, sizeof(*dd
));
5391 dd
->major
= dk
->major
;
5392 dd
->minor
= dk
->minor
;
5395 dd
->action
= DISK_REMOVE
;
5397 dd
->next
= super
->disk_mgmt_list
;
5398 super
->disk_mgmt_list
= dd
;
5403 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
5406 char buf
[MAX_SECTOR_SIZE
];
5407 struct imsm_super anchor
;
5408 } spare_record
__attribute__ ((aligned(MAX_SECTOR_SIZE
)));
5410 /* spare records have their own family number and do not have any defined raid
5413 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
5415 struct imsm_super
*mpb
= super
->anchor
;
5416 struct imsm_super
*spare
= &spare_record
.anchor
;
5420 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
));
5421 spare
->generation_num
= __cpu_to_le32(1UL);
5422 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5423 spare
->num_disks
= 1;
5424 spare
->num_raid_devs
= 0;
5425 spare
->cache_size
= mpb
->cache_size
;
5426 spare
->pwr_cycle_count
= __cpu_to_le32(1);
5428 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
5429 MPB_SIGNATURE MPB_VERSION_RAID0
);
5431 for (d
= super
->disks
; d
; d
= d
->next
) {
5435 spare
->disk
[0] = d
->disk
;
5436 if (__le32_to_cpu(d
->disk
.total_blocks_hi
) > 0)
5437 spare
->attributes
|= MPB_ATTRIB_2TB_DISK
;
5439 if (super
->sector_size
== 4096)
5440 convert_to_4k_imsm_disk(&spare
->disk
[0]);
5442 sum
= __gen_imsm_checksum(spare
);
5443 spare
->family_num
= __cpu_to_le32(sum
);
5444 spare
->orig_family_num
= 0;
5445 sum
= __gen_imsm_checksum(spare
);
5446 spare
->check_sum
= __cpu_to_le32(sum
);
5448 if (store_imsm_mpb(d
->fd
, spare
)) {
5449 pr_err("failed for device %d:%d %s\n",
5450 d
->major
, d
->minor
, strerror(errno
));
5462 static int write_super_imsm(struct supertype
*st
, int doclose
)
5464 struct intel_super
*super
= st
->sb
;
5465 unsigned int sector_size
= super
->sector_size
;
5466 struct imsm_super
*mpb
= super
->anchor
;
5472 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
5474 int clear_migration_record
= 1;
5476 /* 'generation' is incremented everytime the metadata is written */
5477 generation
= __le32_to_cpu(mpb
->generation_num
);
5479 mpb
->generation_num
= __cpu_to_le32(generation
);
5481 /* fix up cases where previous mdadm releases failed to set
5484 if (mpb
->orig_family_num
== 0)
5485 mpb
->orig_family_num
= mpb
->family_num
;
5487 for (d
= super
->disks
; d
; d
= d
->next
) {
5491 mpb
->disk
[d
->index
] = d
->disk
;
5495 for (d
= super
->missing
; d
; d
= d
->next
) {
5496 mpb
->disk
[d
->index
] = d
->disk
;
5499 mpb
->num_disks
= num_disks
;
5500 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
5502 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5503 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
5504 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
5506 imsm_copy_dev(dev
, dev2
);
5507 mpb_size
+= sizeof_imsm_dev(dev
, 0);
5509 if (is_gen_migration(dev2
))
5510 clear_migration_record
= 0;
5512 mpb_size
+= __le32_to_cpu(mpb
->bbm_log_size
);
5513 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5515 /* recalculate checksum */
5516 sum
= __gen_imsm_checksum(mpb
);
5517 mpb
->check_sum
= __cpu_to_le32(sum
);
5519 if (super
->clean_migration_record_by_mdmon
) {
5520 clear_migration_record
= 1;
5521 super
->clean_migration_record_by_mdmon
= 0;
5523 if (clear_migration_record
)
5524 memset(super
->migr_rec_buf
, 0,
5525 MIGR_REC_BUF_SECTORS
*sector_size
);
5527 if (sector_size
== 4096)
5528 convert_to_4k(super
);
5530 /* write the mpb for disks that compose raid devices */
5531 for (d
= super
->disks
; d
; d
= d
->next
) {
5532 if (d
->index
< 0 || is_failed(&d
->disk
))
5535 if (clear_migration_record
) {
5536 unsigned long long dsize
;
5538 get_dev_size(d
->fd
, NULL
, &dsize
);
5539 if (lseek64(d
->fd
, dsize
- sector_size
,
5541 if (write(d
->fd
, super
->migr_rec_buf
,
5542 MIGR_REC_BUF_SECTORS
*sector_size
) !=
5543 MIGR_REC_BUF_SECTORS
*sector_size
)
5544 perror("Write migr_rec failed");
5548 if (store_imsm_mpb(d
->fd
, mpb
))
5550 "failed for device %d:%d (fd: %d)%s\n",
5552 d
->fd
, strerror(errno
));
5561 return write_super_imsm_spares(super
, doclose
);
5566 static int create_array(struct supertype
*st
, int dev_idx
)
5569 struct imsm_update_create_array
*u
;
5570 struct intel_super
*super
= st
->sb
;
5571 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
5572 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
5573 struct disk_info
*inf
;
5574 struct imsm_disk
*disk
;
5577 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
5578 sizeof(*inf
) * map
->num_members
;
5580 u
->type
= update_create_array
;
5581 u
->dev_idx
= dev_idx
;
5582 imsm_copy_dev(&u
->dev
, dev
);
5583 inf
= get_disk_info(u
);
5584 for (i
= 0; i
< map
->num_members
; i
++) {
5585 int idx
= get_imsm_disk_idx(dev
, i
, MAP_X
);
5587 disk
= get_imsm_disk(super
, idx
);
5589 disk
= get_imsm_missing(super
, idx
);
5590 serialcpy(inf
[i
].serial
, disk
->serial
);
5592 append_metadata_update(st
, u
, len
);
5597 static int mgmt_disk(struct supertype
*st
)
5599 struct intel_super
*super
= st
->sb
;
5601 struct imsm_update_add_remove_disk
*u
;
5603 if (!super
->disk_mgmt_list
)
5608 u
->type
= update_add_remove_disk
;
5609 append_metadata_update(st
, u
, len
);
5614 static int write_init_super_imsm(struct supertype
*st
)
5616 struct intel_super
*super
= st
->sb
;
5617 int current_vol
= super
->current_vol
;
5619 /* we are done with current_vol reset it to point st at the container */
5620 super
->current_vol
= -1;
5622 if (st
->update_tail
) {
5623 /* queue the recently created array / added disk
5624 * as a metadata update */
5627 /* determine if we are creating a volume or adding a disk */
5628 if (current_vol
< 0) {
5629 /* in the mgmt (add/remove) disk case we are running
5630 * in mdmon context, so don't close fd's
5632 return mgmt_disk(st
);
5634 rv
= create_array(st
, current_vol
);
5639 for (d
= super
->disks
; d
; d
= d
->next
)
5640 Kill(d
->devname
, NULL
, 0, -1, 1);
5641 return write_super_imsm(st
, 1);
5646 static int store_super_imsm(struct supertype
*st
, int fd
)
5648 struct intel_super
*super
= st
->sb
;
5649 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
5655 if (super
->sector_size
== 4096)
5656 convert_to_4k(super
);
5657 return store_imsm_mpb(fd
, mpb
);
5663 static int imsm_bbm_log_size(struct imsm_super
*mpb
)
5665 return __le32_to_cpu(mpb
->bbm_log_size
);
5669 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
5670 int layout
, int raiddisks
, int chunk
,
5671 unsigned long long size
,
5672 unsigned long long data_offset
,
5674 unsigned long long *freesize
,
5678 unsigned long long ldsize
;
5679 struct intel_super
*super
;
5682 if (level
!= LEVEL_CONTAINER
)
5687 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
5690 pr_err("imsm: Cannot open %s: %s\n",
5691 dev
, strerror(errno
));
5694 if (!get_dev_size(fd
, dev
, &ldsize
)) {
5699 /* capabilities retrieve could be possible
5700 * note that there is no fd for the disks in array.
5702 super
= alloc_super();
5703 if (!get_dev_sector_size(fd
, NULL
, &super
->sector_size
)) {
5709 rv
= find_intel_hba_capability(fd
, super
, verbose
> 0 ? dev
: NULL
);
5713 fd2devname(fd
, str
);
5714 dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n",
5715 fd
, str
, super
->orom
, rv
, raiddisks
);
5717 /* no orom/efi or non-intel hba of the disk */
5724 if (raiddisks
> super
->orom
->tds
) {
5726 pr_err("%d exceeds maximum number of platform supported disks: %d\n",
5727 raiddisks
, super
->orom
->tds
);
5731 if ((super
->orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) == 0 &&
5732 (ldsize
>> 9) >> 32 > 0) {
5734 pr_err("%s exceeds maximum platform supported size\n", dev
);
5740 *freesize
= avail_size_imsm(st
, ldsize
>> 9, data_offset
);
5746 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
5748 const unsigned long long base_start
= e
[*idx
].start
;
5749 unsigned long long end
= base_start
+ e
[*idx
].size
;
5752 if (base_start
== end
)
5756 for (i
= *idx
; i
< num_extents
; i
++) {
5757 /* extend overlapping extents */
5758 if (e
[i
].start
>= base_start
&&
5759 e
[i
].start
<= end
) {
5762 if (e
[i
].start
+ e
[i
].size
> end
)
5763 end
= e
[i
].start
+ e
[i
].size
;
5764 } else if (e
[i
].start
> end
) {
5770 return end
- base_start
;
5773 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
5775 /* build a composite disk with all known extents and generate a new
5776 * 'maxsize' given the "all disks in an array must share a common start
5777 * offset" constraint
5779 struct extent
*e
= xcalloc(sum_extents
, sizeof(*e
));
5783 unsigned long long pos
;
5784 unsigned long long start
= 0;
5785 unsigned long long maxsize
;
5786 unsigned long reserve
;
5788 /* coalesce and sort all extents. also, check to see if we need to
5789 * reserve space between member arrays
5792 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5795 for (i
= 0; i
< dl
->extent_cnt
; i
++)
5798 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
5803 while (i
< sum_extents
) {
5804 e
[j
].start
= e
[i
].start
;
5805 e
[j
].size
= find_size(e
, &i
, sum_extents
);
5807 if (e
[j
-1].size
== 0)
5816 unsigned long long esize
;
5818 esize
= e
[i
].start
- pos
;
5819 if (esize
>= maxsize
) {
5824 pos
= e
[i
].start
+ e
[i
].size
;
5826 } while (e
[i
-1].size
);
5832 /* FIXME assumes volume at offset 0 is the first volume in a
5835 if (start_extent
> 0)
5836 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
5840 if (maxsize
< reserve
)
5843 super
->create_offset
= ~((unsigned long long) 0);
5844 if (start
+ reserve
> super
->create_offset
)
5845 return 0; /* start overflows create_offset */
5846 super
->create_offset
= start
+ reserve
;
5848 return maxsize
- reserve
;
5851 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
5853 if (level
< 0 || level
== 6 || level
== 4)
5856 /* if we have an orom prevent invalid raid levels */
5859 case 0: return imsm_orom_has_raid0(orom
);
5862 return imsm_orom_has_raid1e(orom
);
5863 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
5864 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
5865 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
5868 return 1; /* not on an Intel RAID platform so anything goes */
5874 active_arrays_by_format(char *name
, char* hba
, struct md_list
**devlist
,
5875 int dpa
, int verbose
)
5877 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
5878 struct mdstat_ent
*memb
;
5884 for (memb
= mdstat
; memb
; memb
= memb
->next
) {
5885 if (memb
->metadata_version
&&
5886 (strncmp(memb
->metadata_version
, "external:", 9) == 0) &&
5887 (strcmp(&memb
->metadata_version
[9], name
) == 0) &&
5888 !is_subarray(memb
->metadata_version
+9) &&
5890 struct dev_member
*dev
= memb
->members
;
5892 while(dev
&& (fd
< 0)) {
5893 char *path
= xmalloc(strlen(dev
->name
) + strlen("/dev/") + 1);
5894 num
= sprintf(path
, "%s%s", "/dev/", dev
->name
);
5896 fd
= open(path
, O_RDONLY
, 0);
5897 if (num
<= 0 || fd
< 0) {
5898 pr_vrb("Cannot open %s: %s\n",
5899 dev
->name
, strerror(errno
));
5905 if (fd
>= 0 && disk_attached_to_hba(fd
, hba
)) {
5906 struct mdstat_ent
*vol
;
5907 for (vol
= mdstat
; vol
; vol
= vol
->next
) {
5908 if (vol
->active
> 0 &&
5909 vol
->metadata_version
&&
5910 is_container_member(vol
, memb
->devnm
)) {
5915 if (*devlist
&& (found
< dpa
)) {
5916 dv
= xcalloc(1, sizeof(*dv
));
5917 dv
->devname
= xmalloc(strlen(memb
->devnm
) + strlen("/dev/") + 1);
5918 sprintf(dv
->devname
, "%s%s", "/dev/", memb
->devnm
);
5921 dv
->next
= *devlist
;
5929 free_mdstat(mdstat
);
5934 static struct md_list
*
5935 get_loop_devices(void)
5938 struct md_list
*devlist
= NULL
;
5941 for(i
= 0; i
< 12; i
++) {
5942 dv
= xcalloc(1, sizeof(*dv
));
5943 dv
->devname
= xmalloc(40);
5944 sprintf(dv
->devname
, "/dev/loop%d", i
);
5952 static struct md_list
*
5953 get_devices(const char *hba_path
)
5955 struct md_list
*devlist
= NULL
;
5962 devlist
= get_loop_devices();
5965 /* scroll through /sys/dev/block looking for devices attached to
5968 dir
= opendir("/sys/dev/block");
5969 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
5974 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
5976 path
= devt_to_devpath(makedev(major
, minor
));
5979 if (!path_attached_to_hba(path
, hba_path
)) {
5986 fd
= dev_open(ent
->d_name
, O_RDONLY
);
5988 fd2devname(fd
, buf
);
5991 pr_err("cannot open device: %s\n",
5996 dv
= xcalloc(1, sizeof(*dv
));
5997 dv
->devname
= xstrdup(buf
);
6004 devlist
= devlist
->next
;
6014 count_volumes_list(struct md_list
*devlist
, char *homehost
,
6015 int verbose
, int *found
)
6017 struct md_list
*tmpdev
;
6019 struct supertype
*st
;
6021 /* first walk the list of devices to find a consistent set
6022 * that match the criterea, if that is possible.
6023 * We flag the ones we like with 'used'.
6026 st
= match_metadata_desc_imsm("imsm");
6028 pr_vrb("cannot allocate memory for imsm supertype\n");
6032 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
6033 char *devname
= tmpdev
->devname
;
6035 struct supertype
*tst
;
6037 if (tmpdev
->used
> 1)
6039 tst
= dup_super(st
);
6041 pr_vrb("cannot allocate memory for imsm supertype\n");
6044 tmpdev
->container
= 0;
6045 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
6047 dprintf("cannot open device %s: %s\n",
6048 devname
, strerror(errno
));
6050 } else if (fstat(dfd
, &stb
)< 0) {
6052 dprintf("fstat failed for %s: %s\n",
6053 devname
, strerror(errno
));
6055 } else if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
6056 dprintf("%s is not a block device.\n",
6059 } else if (must_be_container(dfd
)) {
6060 struct supertype
*cst
;
6061 cst
= super_by_fd(dfd
, NULL
);
6063 dprintf("cannot recognize container type %s\n",
6066 } else if (tst
->ss
!= st
->ss
) {
6067 dprintf("non-imsm container - ignore it: %s\n",
6070 } else if (!tst
->ss
->load_container
||
6071 tst
->ss
->load_container(tst
, dfd
, NULL
))
6074 tmpdev
->container
= 1;
6077 cst
->ss
->free_super(cst
);
6079 tmpdev
->st_rdev
= stb
.st_rdev
;
6080 if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
6081 dprintf("no RAID superblock on %s\n",
6084 } else if (tst
->ss
->compare_super
== NULL
) {
6085 dprintf("Cannot assemble %s metadata on %s\n",
6086 tst
->ss
->name
, devname
);
6092 if (tmpdev
->used
== 2 || tmpdev
->used
== 4) {
6093 /* Ignore unrecognised devices during auto-assembly */
6098 tst
->ss
->getinfo_super(tst
, &info
, NULL
);
6100 if (st
->minor_version
== -1)
6101 st
->minor_version
= tst
->minor_version
;
6103 if (memcmp(info
.uuid
, uuid_zero
,
6104 sizeof(int[4])) == 0) {
6105 /* this is a floating spare. It cannot define
6106 * an array unless there are no more arrays of
6107 * this type to be found. It can be included
6108 * in an array of this type though.
6114 if (st
->ss
!= tst
->ss
||
6115 st
->minor_version
!= tst
->minor_version
||
6116 st
->ss
->compare_super(st
, tst
) != 0) {
6117 /* Some mismatch. If exactly one array matches this host,
6118 * we can resolve on that one.
6119 * Or, if we are auto assembling, we just ignore the second
6122 dprintf("superblock on %s doesn't match others - assembly aborted\n",
6128 dprintf("found: devname: %s\n", devname
);
6132 tst
->ss
->free_super(tst
);
6136 if ((err
= load_super_imsm_all(st
, -1, &st
->sb
, NULL
, devlist
, 0)) == 0) {
6137 struct mdinfo
*iter
, *head
= st
->ss
->container_content(st
, NULL
);
6138 for (iter
= head
; iter
; iter
= iter
->next
) {
6139 dprintf("content->text_version: %s vol\n",
6140 iter
->text_version
);
6141 if (iter
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
6142 /* do not assemble arrays with unsupported
6144 dprintf("Cannot activate member %s.\n",
6145 iter
->text_version
);
6152 dprintf("No valid super block on device list: err: %d %p\n",
6156 dprintf("no more devices to examine\n");
6159 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
6160 if (tmpdev
->used
== 1 && tmpdev
->found
) {
6162 if (count
< tmpdev
->found
)
6165 count
-= tmpdev
->found
;
6168 if (tmpdev
->used
== 1)
6173 st
->ss
->free_super(st
);
6178 count_volumes(struct intel_hba
*hba
, int dpa
, int verbose
)
6180 struct sys_dev
*idev
, *intel_devices
= find_intel_devices();
6182 const struct orom_entry
*entry
;
6183 struct devid_list
*dv
, *devid_list
;
6185 if (!hba
|| !hba
->path
)
6188 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
6189 if (strstr(idev
->path
, hba
->path
))
6193 if (!idev
|| !idev
->dev_id
)
6196 entry
= get_orom_entry_by_device_id(idev
->dev_id
);
6198 if (!entry
|| !entry
->devid_list
)
6201 devid_list
= entry
->devid_list
;
6202 for (dv
= devid_list
; dv
; dv
= dv
->next
) {
6203 struct md_list
*devlist
;
6204 struct sys_dev
*device
= device_by_id(dv
->devid
);
6209 hba_path
= device
->path
;
6213 devlist
= get_devices(hba_path
);
6214 /* if no intel devices return zero volumes */
6215 if (devlist
== NULL
)
6218 count
+= active_arrays_by_format("imsm", hba_path
, &devlist
, dpa
, verbose
);
6219 dprintf("path: %s active arrays: %d\n", hba_path
, count
);
6220 if (devlist
== NULL
)
6224 count
+= count_volumes_list(devlist
,
6228 dprintf("found %d count: %d\n", found
, count
);
6231 dprintf("path: %s total number of volumes: %d\n", hba_path
, count
);
6234 struct md_list
*dv
= devlist
;
6235 devlist
= devlist
->next
;
6243 static int imsm_default_chunk(const struct imsm_orom
*orom
)
6245 /* up to 512 if the plaform supports it, otherwise the platform max.
6246 * 128 if no platform detected
6248 int fs
= max(7, orom
? fls(orom
->sss
) : 0);
6250 return min(512, (1 << fs
));
6254 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
6255 int raiddisks
, int *chunk
, unsigned long long size
, int verbose
)
6257 /* check/set platform and metadata limits/defaults */
6258 if (super
->orom
&& raiddisks
> super
->orom
->dpa
) {
6259 pr_vrb("platform supports a maximum of %d disks per array\n",
6264 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
6265 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
6266 pr_vrb("platform does not support raid%d with %d disk%s\n",
6267 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
6271 if (*chunk
== 0 || *chunk
== UnSet
)
6272 *chunk
= imsm_default_chunk(super
->orom
);
6274 if (super
->orom
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
6275 pr_vrb("platform does not support a chunk size of: %d\n", *chunk
);
6279 if (layout
!= imsm_level_to_layout(level
)) {
6281 pr_vrb("imsm raid 5 only supports the left-asymmetric layout\n");
6282 else if (level
== 10)
6283 pr_vrb("imsm raid 10 only supports the n2 layout\n");
6285 pr_vrb("imsm unknown layout %#x for this raid level %d\n",
6290 if (super
->orom
&& (super
->orom
->attr
& IMSM_OROM_ATTR_2TB
) == 0 &&
6291 (calc_array_size(level
, raiddisks
, layout
, *chunk
, size
) >> 32) > 0) {
6292 pr_vrb("platform does not support a volume size over 2TB\n");
6299 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
6300 * FIX ME add ahci details
6302 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
6303 int layout
, int raiddisks
, int *chunk
,
6304 unsigned long long size
,
6305 unsigned long long data_offset
,
6307 unsigned long long *freesize
,
6311 struct intel_super
*super
= st
->sb
;
6312 struct imsm_super
*mpb
;
6314 unsigned long long pos
= 0;
6315 unsigned long long maxsize
;
6319 /* We must have the container info already read in. */
6323 mpb
= super
->anchor
;
6325 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, size
, verbose
)) {
6326 pr_err("RAID gemetry validation failed. Cannot proceed with the action(s).\n");
6330 /* General test: make sure there is space for
6331 * 'raiddisks' device extents of size 'size' at a given
6334 unsigned long long minsize
= size
;
6335 unsigned long long start_offset
= MaxSector
;
6338 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
6339 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6344 e
= get_extents(super
, dl
);
6347 unsigned long long esize
;
6348 esize
= e
[i
].start
- pos
;
6349 if (esize
>= minsize
)
6351 if (found
&& start_offset
== MaxSector
) {
6354 } else if (found
&& pos
!= start_offset
) {
6358 pos
= e
[i
].start
+ e
[i
].size
;
6360 } while (e
[i
-1].size
);
6365 if (dcnt
< raiddisks
) {
6367 pr_err("imsm: Not enough devices with space for this array (%d < %d)\n",
6374 /* This device must be a member of the set */
6375 if (stat(dev
, &stb
) < 0)
6377 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
6379 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6380 if (dl
->major
== (int)major(stb
.st_rdev
) &&
6381 dl
->minor
== (int)minor(stb
.st_rdev
))
6386 pr_err("%s is not in the same imsm set\n", dev
);
6388 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
6389 /* If a volume is present then the current creation attempt
6390 * cannot incorporate new spares because the orom may not
6391 * understand this configuration (all member disks must be
6392 * members of each array in the container).
6394 pr_err("%s is a spare and a volume is already defined for this container\n", dev
);
6395 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
6397 } else if (super
->orom
&& mpb
->num_raid_devs
> 0 &&
6398 mpb
->num_disks
!= raiddisks
) {
6399 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
6403 /* retrieve the largest free space block */
6404 e
= get_extents(super
, dl
);
6409 unsigned long long esize
;
6411 esize
= e
[i
].start
- pos
;
6412 if (esize
>= maxsize
)
6414 pos
= e
[i
].start
+ e
[i
].size
;
6416 } while (e
[i
-1].size
);
6421 pr_err("unable to determine free space for: %s\n",
6425 if (maxsize
< size
) {
6427 pr_err("%s not enough space (%llu < %llu)\n",
6428 dev
, maxsize
, size
);
6432 /* count total number of extents for merge */
6434 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6436 i
+= dl
->extent_cnt
;
6438 maxsize
= merge_extents(super
, i
);
6440 if (!check_env("IMSM_NO_PLATFORM") &&
6441 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6442 pr_err("attempting to create a second volume with size less then remaining space. Aborting...\n");
6446 if (maxsize
< size
|| maxsize
== 0) {
6449 pr_err("no free space left on device. Aborting...\n");
6451 pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n",
6457 *freesize
= maxsize
;
6460 int count
= count_volumes(super
->hba
,
6461 super
->orom
->dpa
, verbose
);
6462 if (super
->orom
->vphba
<= count
) {
6463 pr_vrb("platform does not support more than %d raid volumes.\n",
6464 super
->orom
->vphba
);
6471 static int imsm_get_free_size(struct supertype
*st
, int raiddisks
,
6472 unsigned long long size
, int chunk
,
6473 unsigned long long *freesize
)
6475 struct intel_super
*super
= st
->sb
;
6476 struct imsm_super
*mpb
= super
->anchor
;
6481 unsigned long long maxsize
;
6482 unsigned long long minsize
;
6486 /* find the largest common start free region of the possible disks */
6490 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6496 /* don't activate new spares if we are orom constrained
6497 * and there is already a volume active in the container
6499 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
6502 e
= get_extents(super
, dl
);
6505 for (i
= 1; e
[i
-1].size
; i
++)
6513 maxsize
= merge_extents(super
, extent_cnt
);
6517 minsize
= chunk
* 2;
6519 if (cnt
< raiddisks
||
6520 (super
->orom
&& used
&& used
!= raiddisks
) ||
6521 maxsize
< minsize
||
6523 pr_err("not enough devices with space to create array.\n");
6524 return 0; /* No enough free spaces large enough */
6535 if (!check_env("IMSM_NO_PLATFORM") &&
6536 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6537 pr_err("attempting to create a second volume with size less then remaining space. Aborting...\n");
6541 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6543 dl
->raiddisk
= cnt
++;
6547 dprintf("imsm: imsm_get_free_size() returns : %llu\n", size
);
6552 static int reserve_space(struct supertype
*st
, int raiddisks
,
6553 unsigned long long size
, int chunk
,
6554 unsigned long long *freesize
)
6556 struct intel_super
*super
= st
->sb
;
6561 rv
= imsm_get_free_size(st
, raiddisks
, size
, chunk
, freesize
);
6564 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6566 dl
->raiddisk
= cnt
++;
6573 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
6574 int raiddisks
, int *chunk
, unsigned long long size
,
6575 unsigned long long data_offset
,
6576 char *dev
, unsigned long long *freesize
,
6584 * if given unused devices create a container
6585 * if given given devices in a container create a member volume
6587 if (level
== LEVEL_CONTAINER
) {
6588 /* Must be a fresh device to add to a container */
6589 return validate_geometry_imsm_container(st
, level
, layout
,
6599 struct intel_super
*super
= st
->sb
;
6600 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
6601 raiddisks
, chunk
, size
,
6604 /* we are being asked to automatically layout a
6605 * new volume based on the current contents of
6606 * the container. If the the parameters can be
6607 * satisfied reserve_space will record the disks,
6608 * start offset, and size of the volume to be
6609 * created. add_to_super and getinfo_super
6610 * detect when autolayout is in progress.
6612 /* assuming that freesize is always given when array is
6614 if (super
->orom
&& freesize
) {
6616 count
= count_volumes(super
->hba
,
6617 super
->orom
->dpa
, verbose
);
6618 if (super
->orom
->vphba
<= count
) {
6619 pr_vrb("platform does not support more than %d raid volumes.\n",
6620 super
->orom
->vphba
);
6625 return reserve_space(st
, raiddisks
, size
,
6631 /* creating in a given container */
6632 return validate_geometry_imsm_volume(st
, level
, layout
,
6633 raiddisks
, chunk
, size
,
6635 dev
, freesize
, verbose
);
6638 /* This device needs to be a device in an 'imsm' container */
6639 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
6642 pr_err("Cannot create this array on device %s\n",
6647 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
6649 pr_err("Cannot open %s: %s\n",
6650 dev
, strerror(errno
));
6653 /* Well, it is in use by someone, maybe an 'imsm' container. */
6654 cfd
= open_container(fd
);
6658 pr_err("Cannot use %s: It is busy\n",
6662 sra
= sysfs_read(cfd
, NULL
, GET_VERSION
);
6663 if (sra
&& sra
->array
.major_version
== -1 &&
6664 strcmp(sra
->text_version
, "imsm") == 0)
6668 /* This is a member of a imsm container. Load the container
6669 * and try to create a volume
6671 struct intel_super
*super
;
6673 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
, NULL
, 1) == 0) {
6675 strcpy(st
->container_devnm
, fd2devnm(cfd
));
6677 return validate_geometry_imsm_volume(st
, level
, layout
,
6679 size
, data_offset
, dev
,
6686 pr_err("failed container membership check\n");
6692 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
6694 struct intel_super
*super
= st
->sb
;
6696 if (level
&& *level
== UnSet
)
6697 *level
= LEVEL_CONTAINER
;
6699 if (level
&& layout
&& *layout
== UnSet
)
6700 *layout
= imsm_level_to_layout(*level
);
6702 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0))
6703 *chunk
= imsm_default_chunk(super
->orom
);
6706 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
6708 static int kill_subarray_imsm(struct supertype
*st
)
6710 /* remove the subarray currently referenced by ->current_vol */
6712 struct intel_dev
**dp
;
6713 struct intel_super
*super
= st
->sb
;
6714 __u8 current_vol
= super
->current_vol
;
6715 struct imsm_super
*mpb
= super
->anchor
;
6717 if (super
->current_vol
< 0)
6719 super
->current_vol
= -1; /* invalidate subarray cursor */
6721 /* block deletions that would change the uuid of active subarrays
6723 * FIXME when immutable ids are available, but note that we'll
6724 * also need to fixup the invalidated/active subarray indexes in
6727 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6730 if (i
< current_vol
)
6732 sprintf(subarray
, "%u", i
);
6733 if (is_subarray_active(subarray
, st
->devnm
)) {
6734 pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
6741 if (st
->update_tail
) {
6742 struct imsm_update_kill_array
*u
= xmalloc(sizeof(*u
));
6744 u
->type
= update_kill_array
;
6745 u
->dev_idx
= current_vol
;
6746 append_metadata_update(st
, u
, sizeof(*u
));
6751 for (dp
= &super
->devlist
; *dp
;)
6752 if ((*dp
)->index
== current_vol
) {
6755 handle_missing(super
, (*dp
)->dev
);
6756 if ((*dp
)->index
> current_vol
)
6761 /* no more raid devices, all active components are now spares,
6762 * but of course failed are still failed
6764 if (--mpb
->num_raid_devs
== 0) {
6767 for (d
= super
->disks
; d
; d
= d
->next
)
6772 super
->updates_pending
++;
6777 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
6778 char *update
, struct mddev_ident
*ident
)
6780 /* update the subarray currently referenced by ->current_vol */
6781 struct intel_super
*super
= st
->sb
;
6782 struct imsm_super
*mpb
= super
->anchor
;
6784 if (strcmp(update
, "name") == 0) {
6785 char *name
= ident
->name
;
6789 if (is_subarray_active(subarray
, st
->devnm
)) {
6790 pr_err("Unable to update name of active subarray\n");
6794 if (!check_name(super
, name
, 0))
6797 vol
= strtoul(subarray
, &ep
, 10);
6798 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
6801 if (st
->update_tail
) {
6802 struct imsm_update_rename_array
*u
= xmalloc(sizeof(*u
));
6804 u
->type
= update_rename_array
;
6806 snprintf((char *) u
->name
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6807 append_metadata_update(st
, u
, sizeof(*u
));
6809 struct imsm_dev
*dev
;
6812 dev
= get_imsm_dev(super
, vol
);
6813 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6814 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6815 dev
= get_imsm_dev(super
, i
);
6816 handle_missing(super
, dev
);
6818 super
->updates_pending
++;
6825 #endif /* MDASSEMBLE */
6827 static int is_gen_migration(struct imsm_dev
*dev
)
6832 if (!dev
->vol
.migr_state
)
6835 if (migr_type(dev
) == MIGR_GEN_MIGR
)
6841 static int is_rebuilding(struct imsm_dev
*dev
)
6843 struct imsm_map
*migr_map
;
6845 if (!dev
->vol
.migr_state
)
6848 if (migr_type(dev
) != MIGR_REBUILD
)
6851 migr_map
= get_imsm_map(dev
, MAP_1
);
6853 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
6860 static int is_initializing(struct imsm_dev
*dev
)
6862 struct imsm_map
*migr_map
;
6864 if (!dev
->vol
.migr_state
)
6867 if (migr_type(dev
) != MIGR_INIT
)
6870 migr_map
= get_imsm_map(dev
, MAP_1
);
6872 if (migr_map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
6879 static void update_recovery_start(struct intel_super
*super
,
6880 struct imsm_dev
*dev
,
6881 struct mdinfo
*array
)
6883 struct mdinfo
*rebuild
= NULL
;
6887 if (!is_rebuilding(dev
))
6890 /* Find the rebuild target, but punt on the dual rebuild case */
6891 for (d
= array
->devs
; d
; d
= d
->next
)
6892 if (d
->recovery_start
== 0) {
6899 /* (?) none of the disks are marked with
6900 * IMSM_ORD_REBUILD, so assume they are missing and the
6901 * disk_ord_tbl was not correctly updated
6903 dprintf("failed to locate out-of-sync disk\n");
6907 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
6908 rebuild
->recovery_start
= units
* blocks_per_migr_unit(super
, dev
);
6912 static int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
);
6915 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
6917 /* Given a container loaded by load_super_imsm_all,
6918 * extract information about all the arrays into
6920 * If 'subarray' is given, just extract info about that array.
6922 * For each imsm_dev create an mdinfo, fill it in,
6923 * then look for matching devices in super->disks
6924 * and create appropriate device mdinfo.
6926 struct intel_super
*super
= st
->sb
;
6927 struct imsm_super
*mpb
= super
->anchor
;
6928 struct mdinfo
*rest
= NULL
;
6932 int spare_disks
= 0;
6934 /* do not assemble arrays when not all attributes are supported */
6935 if (imsm_check_attributes(mpb
->attributes
) == 0) {
6937 pr_err("Unsupported attributes in IMSM metadata.Arrays activation is blocked.\n");
6940 /* check for bad blocks */
6941 if (imsm_bbm_log_size(super
->anchor
)) {
6942 pr_err("BBM log found in IMSM metadata.Arrays activation is blocked.\n");
6946 /* count spare devices, not used in maps
6948 for (d
= super
->disks
; d
; d
= d
->next
)
6952 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6953 struct imsm_dev
*dev
;
6954 struct imsm_map
*map
;
6955 struct imsm_map
*map2
;
6956 struct mdinfo
*this;
6964 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
6967 dev
= get_imsm_dev(super
, i
);
6968 map
= get_imsm_map(dev
, MAP_0
);
6969 map2
= get_imsm_map(dev
, MAP_1
);
6971 /* do not publish arrays that are in the middle of an
6972 * unsupported migration
6974 if (dev
->vol
.migr_state
&&
6975 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
6976 pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n",
6980 /* do not publish arrays that are not support by controller's
6984 this = xmalloc(sizeof(*this));
6986 super
->current_vol
= i
;
6987 getinfo_super_imsm_volume(st
, this, NULL
);
6990 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
6991 /* mdadm does not support all metadata features- set the bit in all arrays state */
6992 if (!validate_geometry_imsm_orom(super
,
6993 get_imsm_raid_level(map
), /* RAID level */
6994 imsm_level_to_layout(get_imsm_raid_level(map
)),
6995 map
->num_members
, /* raid disks */
6996 &chunk
, join_u32(dev
->size_low
, dev
->size_high
),
6998 pr_err("IMSM RAID geometry validation failed. Array %s activation is blocked.\n",
7000 this->array
.state
|=
7001 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
7002 (1<<MD_SB_BLOCK_VOLUME
);
7006 /* if array has bad blocks, set suitable bit in all arrays state */
7008 this->array
.state
|=
7009 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
7010 (1<<MD_SB_BLOCK_VOLUME
);
7012 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
7013 unsigned long long recovery_start
;
7014 struct mdinfo
*info_d
;
7021 idx
= get_imsm_disk_idx(dev
, slot
, MAP_0
);
7022 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
7023 for (d
= super
->disks
; d
; d
= d
->next
)
7024 if (d
->index
== idx
)
7027 recovery_start
= MaxSector
;
7030 if (d
&& is_failed(&d
->disk
))
7032 if (ord
& IMSM_ORD_REBUILD
)
7036 * if we skip some disks the array will be assmebled degraded;
7037 * reset resync start to avoid a dirty-degraded
7038 * situation when performing the intial sync
7040 * FIXME handle dirty degraded
7042 if ((skip
|| recovery_start
== 0) && !dev
->vol
.dirty
)
7043 this->resync_start
= MaxSector
;
7047 info_d
= xcalloc(1, sizeof(*info_d
));
7048 info_d
->next
= this->devs
;
7049 this->devs
= info_d
;
7051 info_d
->disk
.number
= d
->index
;
7052 info_d
->disk
.major
= d
->major
;
7053 info_d
->disk
.minor
= d
->minor
;
7054 info_d
->disk
.raid_disk
= slot
;
7055 info_d
->recovery_start
= recovery_start
;
7057 if (slot
< map2
->num_members
)
7058 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
7060 this->array
.spare_disks
++;
7062 if (slot
< map
->num_members
)
7063 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
7065 this->array
.spare_disks
++;
7067 if (info_d
->recovery_start
== MaxSector
)
7068 this->array
.working_disks
++;
7070 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
7071 info_d
->data_offset
= pba_of_lba0(map
);
7073 if (map
->raid_level
== 5) {
7074 info_d
->component_size
=
7075 num_data_stripes(map
) *
7076 map
->blocks_per_strip
;
7078 info_d
->component_size
= blocks_per_member(map
);
7081 /* now that the disk list is up-to-date fixup recovery_start */
7082 update_recovery_start(super
, dev
, this);
7083 this->array
.spare_disks
+= spare_disks
;
7086 /* check for reshape */
7087 if (this->reshape_active
== 1)
7088 recover_backup_imsm(st
, this);
7096 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
7097 int failed
, int look_in_map
)
7099 struct imsm_map
*map
;
7101 map
= get_imsm_map(dev
, look_in_map
);
7104 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
7105 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
7107 switch (get_imsm_raid_level(map
)) {
7109 return IMSM_T_STATE_FAILED
;
7112 if (failed
< map
->num_members
)
7113 return IMSM_T_STATE_DEGRADED
;
7115 return IMSM_T_STATE_FAILED
;
7120 * check to see if any mirrors have failed, otherwise we
7121 * are degraded. Even numbered slots are mirrored on
7125 /* gcc -Os complains that this is unused */
7126 int insync
= insync
;
7128 for (i
= 0; i
< map
->num_members
; i
++) {
7129 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_X
);
7130 int idx
= ord_to_idx(ord
);
7131 struct imsm_disk
*disk
;
7133 /* reset the potential in-sync count on even-numbered
7134 * slots. num_copies is always 2 for imsm raid10
7139 disk
= get_imsm_disk(super
, idx
);
7140 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
7143 /* no in-sync disks left in this mirror the
7147 return IMSM_T_STATE_FAILED
;
7150 return IMSM_T_STATE_DEGRADED
;
7154 return IMSM_T_STATE_DEGRADED
;
7156 return IMSM_T_STATE_FAILED
;
7162 return map
->map_state
;
7165 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
7170 struct imsm_disk
*disk
;
7171 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7172 struct imsm_map
*prev
= get_imsm_map(dev
, MAP_1
);
7173 struct imsm_map
*map_for_loop
;
7178 /* at the beginning of migration we set IMSM_ORD_REBUILD on
7179 * disks that are being rebuilt. New failures are recorded to
7180 * map[0]. So we look through all the disks we started with and
7181 * see if any failures are still present, or if any new ones
7185 if (prev
&& (map
->num_members
< prev
->num_members
))
7186 map_for_loop
= prev
;
7188 for (i
= 0; i
< map_for_loop
->num_members
; i
++) {
7190 /* when MAP_X is passed both maps failures are counted
7193 (look_in_map
== MAP_1
|| look_in_map
== MAP_X
) &&
7194 i
< prev
->num_members
) {
7195 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
7196 idx_1
= ord_to_idx(ord
);
7198 disk
= get_imsm_disk(super
, idx_1
);
7199 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
7202 if ((look_in_map
== MAP_0
|| look_in_map
== MAP_X
) &&
7203 i
< map
->num_members
) {
7204 ord
= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
7205 idx
= ord_to_idx(ord
);
7208 disk
= get_imsm_disk(super
, idx
);
7209 if (!disk
|| is_failed(disk
) ||
7210 ord
& IMSM_ORD_REBUILD
)
7220 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
7223 struct intel_super
*super
= c
->sb
;
7224 struct imsm_super
*mpb
= super
->anchor
;
7226 if (atoi(inst
) >= mpb
->num_raid_devs
) {
7227 pr_err("subarry index %d, out of range\n", atoi(inst
));
7231 dprintf("imsm: open_new %s\n", inst
);
7232 a
->info
.container_member
= atoi(inst
);
7236 static int is_resyncing(struct imsm_dev
*dev
)
7238 struct imsm_map
*migr_map
;
7240 if (!dev
->vol
.migr_state
)
7243 if (migr_type(dev
) == MIGR_INIT
||
7244 migr_type(dev
) == MIGR_REPAIR
)
7247 if (migr_type(dev
) == MIGR_GEN_MIGR
)
7250 migr_map
= get_imsm_map(dev
, MAP_1
);
7252 if (migr_map
->map_state
== IMSM_T_STATE_NORMAL
&&
7253 dev
->vol
.migr_type
!= MIGR_GEN_MIGR
)
7259 /* return true if we recorded new information */
7260 static int mark_failure(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
7264 struct imsm_map
*map
;
7265 char buf
[MAX_RAID_SERIAL_LEN
+3];
7266 unsigned int len
, shift
= 0;
7268 /* new failures are always set in map[0] */
7269 map
= get_imsm_map(dev
, MAP_0
);
7271 slot
= get_imsm_disk_slot(map
, idx
);
7275 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
7276 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
7279 memcpy(buf
, disk
->serial
, MAX_RAID_SERIAL_LEN
);
7280 buf
[MAX_RAID_SERIAL_LEN
] = '\000';
7282 if ((len
= strlen(buf
)) >= MAX_RAID_SERIAL_LEN
)
7283 shift
= len
- MAX_RAID_SERIAL_LEN
+ 1;
7284 strncpy((char *)disk
->serial
, &buf
[shift
], MAX_RAID_SERIAL_LEN
);
7286 disk
->status
|= FAILED_DISK
;
7287 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
7288 /* mark failures in second map if second map exists and this disk
7290 * This is valid for migration, initialization and rebuild
7292 if (dev
->vol
.migr_state
) {
7293 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
7294 int slot2
= get_imsm_disk_slot(map2
, idx
);
7296 if (slot2
< map2
->num_members
&& slot2
>= 0)
7297 set_imsm_ord_tbl_ent(map2
, slot2
,
7298 idx
| IMSM_ORD_REBUILD
);
7300 if (map
->failed_disk_num
== 0xff)
7301 map
->failed_disk_num
= slot
;
7305 static void mark_missing(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
7307 mark_failure(dev
, disk
, idx
);
7309 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
7312 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
7313 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
7316 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
7320 if (!super
->missing
)
7323 /* When orom adds replacement for missing disk it does
7324 * not remove entry of missing disk, but just updates map with
7325 * new added disk. So it is not enough just to test if there is
7326 * any missing disk, we have to look if there are any failed disks
7327 * in map to stop migration */
7329 dprintf("imsm: mark missing\n");
7330 /* end process for initialization and rebuild only
7332 if (is_gen_migration(dev
) == 0) {
7336 failed
= imsm_count_failed(super
, dev
, MAP_0
);
7337 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7340 end_migration(dev
, super
, map_state
);
7342 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
7343 mark_missing(dev
, &dl
->disk
, dl
->index
);
7344 super
->updates_pending
++;
7347 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
,
7350 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
7351 unsigned long long array_blocks
;
7352 struct imsm_map
*map
;
7354 if (used_disks
== 0) {
7355 /* when problems occures
7356 * return current array_blocks value
7358 array_blocks
= __le32_to_cpu(dev
->size_high
);
7359 array_blocks
= array_blocks
<< 32;
7360 array_blocks
+= __le32_to_cpu(dev
->size_low
);
7362 return array_blocks
;
7365 /* set array size in metadata
7367 if (new_size
<= 0) {
7368 /* OLCE size change is caused by added disks
7370 map
= get_imsm_map(dev
, MAP_0
);
7371 array_blocks
= blocks_per_member(map
) * used_disks
;
7373 /* Online Volume Size Change
7374 * Using available free space
7376 array_blocks
= new_size
;
7379 /* round array size down to closest MB
7381 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
7382 dev
->size_low
= __cpu_to_le32((__u32
)array_blocks
);
7383 dev
->size_high
= __cpu_to_le32((__u32
)(array_blocks
>> 32));
7385 return array_blocks
;
7388 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
7390 static void imsm_progress_container_reshape(struct intel_super
*super
)
7392 /* if no device has a migr_state, but some device has a
7393 * different number of members than the previous device, start
7394 * changing the number of devices in this device to match
7397 struct imsm_super
*mpb
= super
->anchor
;
7398 int prev_disks
= -1;
7402 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7403 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
7404 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7405 struct imsm_map
*map2
;
7406 int prev_num_members
;
7408 if (dev
->vol
.migr_state
)
7411 if (prev_disks
== -1)
7412 prev_disks
= map
->num_members
;
7413 if (prev_disks
== map
->num_members
)
7416 /* OK, this array needs to enter reshape mode.
7417 * i.e it needs a migr_state
7420 copy_map_size
= sizeof_imsm_map(map
);
7421 prev_num_members
= map
->num_members
;
7422 map
->num_members
= prev_disks
;
7423 dev
->vol
.migr_state
= 1;
7424 dev
->vol
.curr_migr_unit
= 0;
7425 set_migr_type(dev
, MIGR_GEN_MIGR
);
7426 for (i
= prev_num_members
;
7427 i
< map
->num_members
; i
++)
7428 set_imsm_ord_tbl_ent(map
, i
, i
);
7429 map2
= get_imsm_map(dev
, MAP_1
);
7430 /* Copy the current map */
7431 memcpy(map2
, map
, copy_map_size
);
7432 map2
->num_members
= prev_num_members
;
7434 imsm_set_array_size(dev
, -1);
7435 super
->clean_migration_record_by_mdmon
= 1;
7436 super
->updates_pending
++;
7440 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
7441 * states are handled in imsm_set_disk() with one exception, when a
7442 * resync is stopped due to a new failure this routine will set the
7443 * 'degraded' state for the array.
7445 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
7447 int inst
= a
->info
.container_member
;
7448 struct intel_super
*super
= a
->container
->sb
;
7449 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7450 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7451 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
7452 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7453 __u32 blocks_per_unit
;
7455 if (dev
->vol
.migr_state
&&
7456 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
7457 /* array state change is blocked due to reshape action
7459 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
7460 * - finish the reshape (if last_checkpoint is big and action != reshape)
7461 * - update curr_migr_unit
7463 if (a
->curr_action
== reshape
) {
7464 /* still reshaping, maybe update curr_migr_unit */
7465 goto mark_checkpoint
;
7467 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
7468 /* for some reason we aborted the reshape.
7470 * disable automatic metadata rollback
7471 * user action is required to recover process
7474 struct imsm_map
*map2
=
7475 get_imsm_map(dev
, MAP_1
);
7476 dev
->vol
.migr_state
= 0;
7477 set_migr_type(dev
, 0);
7478 dev
->vol
.curr_migr_unit
= 0;
7480 sizeof_imsm_map(map2
));
7481 super
->updates_pending
++;
7484 if (a
->last_checkpoint
>= a
->info
.component_size
) {
7485 unsigned long long array_blocks
;
7489 used_disks
= imsm_num_data_members(dev
, MAP_0
);
7490 if (used_disks
> 0) {
7492 blocks_per_member(map
) *
7494 /* round array size down to closest MB
7496 array_blocks
= (array_blocks
7497 >> SECT_PER_MB_SHIFT
)
7498 << SECT_PER_MB_SHIFT
;
7499 a
->info
.custom_array_size
= array_blocks
;
7500 /* encourage manager to update array
7504 a
->check_reshape
= 1;
7506 /* finalize online capacity expansion/reshape */
7507 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
7509 mdi
->disk
.raid_disk
,
7512 imsm_progress_container_reshape(super
);
7517 /* before we activate this array handle any missing disks */
7518 if (consistent
== 2)
7519 handle_missing(super
, dev
);
7521 if (consistent
== 2 &&
7522 (!is_resync_complete(&a
->info
) ||
7523 map_state
!= IMSM_T_STATE_NORMAL
||
7524 dev
->vol
.migr_state
))
7527 if (is_resync_complete(&a
->info
)) {
7528 /* complete intialization / resync,
7529 * recovery and interrupted recovery is completed in
7532 if (is_resyncing(dev
)) {
7533 dprintf("imsm: mark resync done\n");
7534 end_migration(dev
, super
, map_state
);
7535 super
->updates_pending
++;
7536 a
->last_checkpoint
= 0;
7538 } else if ((!is_resyncing(dev
) && !failed
) &&
7539 (imsm_reshape_blocks_arrays_changes(super
) == 0)) {
7540 /* mark the start of the init process if nothing is failed */
7541 dprintf("imsm: mark resync start\n");
7542 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
7543 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
7545 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
7546 super
->updates_pending
++;
7550 /* skip checkpointing for general migration,
7551 * it is controlled in mdadm
7553 if (is_gen_migration(dev
))
7554 goto skip_mark_checkpoint
;
7556 /* check if we can update curr_migr_unit from resync_start, recovery_start */
7557 blocks_per_unit
= blocks_per_migr_unit(super
, dev
);
7558 if (blocks_per_unit
) {
7562 units
= a
->last_checkpoint
/ blocks_per_unit
;
7565 /* check that we did not overflow 32-bits, and that
7566 * curr_migr_unit needs updating
7568 if (units32
== units
&&
7570 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
7571 dprintf("imsm: mark checkpoint (%u)\n", units32
);
7572 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
7573 super
->updates_pending
++;
7577 skip_mark_checkpoint
:
7578 /* mark dirty / clean */
7579 if (dev
->vol
.dirty
!= !consistent
) {
7580 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
7585 super
->updates_pending
++;
7591 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
7593 int inst
= a
->info
.container_member
;
7594 struct intel_super
*super
= a
->container
->sb
;
7595 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7596 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7597 struct imsm_disk
*disk
;
7599 int recovery_not_finished
= 0;
7604 if (n
> map
->num_members
)
7605 pr_err("imsm: set_disk %d out of range 0..%d\n",
7606 n
, map
->num_members
- 1);
7611 dprintf("imsm: set_disk %d:%x\n", n
, state
);
7613 ord
= get_imsm_ord_tbl_ent(dev
, n
, MAP_0
);
7614 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
7616 /* check for new failures */
7617 if (state
& DS_FAULTY
) {
7618 if (mark_failure(dev
, disk
, ord_to_idx(ord
)))
7619 super
->updates_pending
++;
7622 /* check if in_sync */
7623 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
7624 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
7626 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
7627 super
->updates_pending
++;
7630 failed
= imsm_count_failed(super
, dev
, MAP_0
);
7631 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7633 /* check if recovery complete, newly degraded, or failed */
7634 dprintf("imsm: Detected transition to state ");
7635 switch (map_state
) {
7636 case IMSM_T_STATE_NORMAL
: /* transition to normal state */
7637 dprintf("normal: ");
7638 if (is_rebuilding(dev
)) {
7639 dprintf_cont("while rebuilding");
7640 /* check if recovery is really finished */
7641 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
7642 if (mdi
->recovery_start
!= MaxSector
) {
7643 recovery_not_finished
= 1;
7646 if (recovery_not_finished
) {
7648 dprintf("Rebuild has not finished yet, state not changed");
7649 if (a
->last_checkpoint
< mdi
->recovery_start
) {
7650 a
->last_checkpoint
= mdi
->recovery_start
;
7651 super
->updates_pending
++;
7655 end_migration(dev
, super
, map_state
);
7656 map
= get_imsm_map(dev
, MAP_0
);
7657 map
->failed_disk_num
= ~0;
7658 super
->updates_pending
++;
7659 a
->last_checkpoint
= 0;
7662 if (is_gen_migration(dev
)) {
7663 dprintf_cont("while general migration");
7664 if (a
->last_checkpoint
>= a
->info
.component_size
)
7665 end_migration(dev
, super
, map_state
);
7667 map
->map_state
= map_state
;
7668 map
= get_imsm_map(dev
, MAP_0
);
7669 map
->failed_disk_num
= ~0;
7670 super
->updates_pending
++;
7674 case IMSM_T_STATE_DEGRADED
: /* transition to degraded state */
7675 dprintf_cont("degraded: ");
7676 if (map
->map_state
!= map_state
&& !dev
->vol
.migr_state
) {
7677 dprintf_cont("mark degraded");
7678 map
->map_state
= map_state
;
7679 super
->updates_pending
++;
7680 a
->last_checkpoint
= 0;
7683 if (is_rebuilding(dev
)) {
7684 dprintf_cont("while rebuilding.");
7685 if (map
->map_state
!= map_state
) {
7686 dprintf_cont(" Map state change");
7687 end_migration(dev
, super
, map_state
);
7688 super
->updates_pending
++;
7692 if (is_gen_migration(dev
)) {
7693 dprintf_cont("while general migration");
7694 if (a
->last_checkpoint
>= a
->info
.component_size
)
7695 end_migration(dev
, super
, map_state
);
7697 map
->map_state
= map_state
;
7698 manage_second_map(super
, dev
);
7700 super
->updates_pending
++;
7703 if (is_initializing(dev
)) {
7704 dprintf_cont("while initialization.");
7705 map
->map_state
= map_state
;
7706 super
->updates_pending
++;
7710 case IMSM_T_STATE_FAILED
: /* transition to failed state */
7711 dprintf_cont("failed: ");
7712 if (is_gen_migration(dev
)) {
7713 dprintf_cont("while general migration");
7714 map
->map_state
= map_state
;
7715 super
->updates_pending
++;
7718 if (map
->map_state
!= map_state
) {
7719 dprintf_cont("mark failed");
7720 end_migration(dev
, super
, map_state
);
7721 super
->updates_pending
++;
7722 a
->last_checkpoint
= 0;
7727 dprintf_cont("state %i\n", map_state
);
7732 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
7735 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
7736 unsigned long long dsize
;
7737 unsigned long long sectors
;
7738 unsigned int sector_size
;
7740 get_dev_sector_size(fd
, NULL
, §or_size
);
7741 get_dev_size(fd
, NULL
, &dsize
);
7743 if (mpb_size
> sector_size
) {
7744 /* -1 to account for anchor */
7745 sectors
= mpb_sectors(mpb
, sector_size
) - 1;
7747 /* write the extended mpb to the sectors preceeding the anchor */
7748 if (lseek64(fd
, dsize
- (sector_size
* (2 + sectors
)),
7752 if ((unsigned long long)write(fd
, buf
+ sector_size
,
7753 sector_size
* sectors
) != sector_size
* sectors
)
7757 /* first block is stored on second to last sector of the disk */
7758 if (lseek64(fd
, dsize
- (sector_size
* 2), SEEK_SET
) < 0)
7761 if (write(fd
, buf
, sector_size
) != sector_size
)
7767 static void imsm_sync_metadata(struct supertype
*container
)
7769 struct intel_super
*super
= container
->sb
;
7771 dprintf("sync metadata: %d\n", super
->updates_pending
);
7772 if (!super
->updates_pending
)
7775 write_super_imsm(container
, 0);
7777 super
->updates_pending
= 0;
7780 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
7782 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7783 int i
= get_imsm_disk_idx(dev
, idx
, MAP_X
);
7786 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7790 if (dl
&& is_failed(&dl
->disk
))
7794 dprintf("found %x:%x\n", dl
->major
, dl
->minor
);
7799 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
7800 struct active_array
*a
, int activate_new
,
7801 struct mdinfo
*additional_test_list
)
7803 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7804 int idx
= get_imsm_disk_idx(dev
, slot
, MAP_X
);
7805 struct imsm_super
*mpb
= super
->anchor
;
7806 struct imsm_map
*map
;
7807 unsigned long long pos
;
7812 __u32 array_start
= 0;
7813 __u32 array_end
= 0;
7815 struct mdinfo
*test_list
;
7817 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7818 /* If in this array, skip */
7819 for (d
= a
->info
.devs
; d
; d
= d
->next
)
7820 if (d
->state_fd
>= 0 &&
7821 d
->disk
.major
== dl
->major
&&
7822 d
->disk
.minor
== dl
->minor
) {
7823 dprintf("%x:%x already in array\n",
7824 dl
->major
, dl
->minor
);
7829 test_list
= additional_test_list
;
7831 if (test_list
->disk
.major
== dl
->major
&&
7832 test_list
->disk
.minor
== dl
->minor
) {
7833 dprintf("%x:%x already in additional test list\n",
7834 dl
->major
, dl
->minor
);
7837 test_list
= test_list
->next
;
7842 /* skip in use or failed drives */
7843 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
7845 dprintf("%x:%x status (failed: %d index: %d)\n",
7846 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
7850 /* skip pure spares when we are looking for partially
7851 * assimilated drives
7853 if (dl
->index
== -1 && !activate_new
)
7856 /* Does this unused device have the requisite free space?
7857 * It needs to be able to cover all member volumes
7859 ex
= get_extents(super
, dl
);
7861 dprintf("cannot get extents\n");
7864 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7865 dev
= get_imsm_dev(super
, i
);
7866 map
= get_imsm_map(dev
, MAP_0
);
7868 /* check if this disk is already a member of
7871 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
7877 array_start
= pba_of_lba0(map
);
7878 array_end
= array_start
+
7879 blocks_per_member(map
) - 1;
7882 /* check that we can start at pba_of_lba0 with
7883 * blocks_per_member of space
7885 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
7889 pos
= ex
[j
].start
+ ex
[j
].size
;
7891 } while (ex
[j
-1].size
);
7898 if (i
< mpb
->num_raid_devs
) {
7899 dprintf("%x:%x does not have %u to %u available\n",
7900 dl
->major
, dl
->minor
, array_start
, array_end
);
7910 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
7912 struct imsm_dev
*dev2
;
7913 struct imsm_map
*map
;
7919 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
7921 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
, MAP_0
);
7922 if (state
== IMSM_T_STATE_FAILED
) {
7923 map
= get_imsm_map(dev2
, MAP_0
);
7926 for (slot
= 0; slot
< map
->num_members
; slot
++) {
7928 * Check if failed disks are deleted from intel
7929 * disk list or are marked to be deleted
7931 idx
= get_imsm_disk_idx(dev2
, slot
, MAP_X
);
7932 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
7934 * Do not rebuild the array if failed disks
7935 * from failed sub-array are not removed from
7939 is_failed(&idisk
->disk
) &&
7940 (idisk
->action
!= DISK_REMOVE
))
7948 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
7949 struct metadata_update
**updates
)
7952 * Find a device with unused free space and use it to replace a
7953 * failed/vacant region in an array. We replace failed regions one a
7954 * array at a time. The result is that a new spare disk will be added
7955 * to the first failed array and after the monitor has finished
7956 * propagating failures the remainder will be consumed.
7958 * FIXME add a capability for mdmon to request spares from another
7962 struct intel_super
*super
= a
->container
->sb
;
7963 int inst
= a
->info
.container_member
;
7964 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7965 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7966 int failed
= a
->info
.array
.raid_disks
;
7967 struct mdinfo
*rv
= NULL
;
7970 struct metadata_update
*mu
;
7972 struct imsm_update_activate_spare
*u
;
7977 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
7978 if ((d
->curr_state
& DS_FAULTY
) &&
7980 /* wait for Removal to happen */
7982 if (d
->state_fd
>= 0)
7986 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
7987 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
7989 if (imsm_reshape_blocks_arrays_changes(super
))
7992 /* Cannot activate another spare if rebuild is in progress already
7994 if (is_rebuilding(dev
)) {
7995 dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n");
7999 if (a
->info
.array
.level
== 4)
8000 /* No repair for takeovered array
8001 * imsm doesn't support raid4
8005 if (imsm_check_degraded(super
, dev
, failed
, MAP_0
) !=
8006 IMSM_T_STATE_DEGRADED
)
8009 if (get_imsm_map(dev
, MAP_0
)->map_state
== IMSM_T_STATE_UNINITIALIZED
) {
8010 dprintf("imsm: No spare activation allowed. Volume is not initialized.\n");
8015 * If there are any failed disks check state of the other volume.
8016 * Block rebuild if the another one is failed until failed disks
8017 * are removed from container.
8020 dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n",
8021 MAX_RAID_SERIAL_LEN
, dev
->volume
);
8022 /* check if states of the other volumes allow for rebuild */
8023 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
8025 allowed
= imsm_rebuild_allowed(a
->container
,
8033 /* For each slot, if it is not working, find a spare */
8034 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
8035 for (d
= a
->info
.devs
; d
; d
= d
->next
)
8036 if (d
->disk
.raid_disk
== i
)
8038 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
8039 if (d
&& (d
->state_fd
>= 0))
8043 * OK, this device needs recovery. Try to re-add the
8044 * previous occupant of this slot, if this fails see if
8045 * we can continue the assimilation of a spare that was
8046 * partially assimilated, finally try to activate a new
8049 dl
= imsm_readd(super
, i
, a
);
8051 dl
= imsm_add_spare(super
, i
, a
, 0, rv
);
8053 dl
= imsm_add_spare(super
, i
, a
, 1, rv
);
8057 /* found a usable disk with enough space */
8058 di
= xcalloc(1, sizeof(*di
));
8060 /* dl->index will be -1 in the case we are activating a
8061 * pristine spare. imsm_process_update() will create a
8062 * new index in this case. Once a disk is found to be
8063 * failed in all member arrays it is kicked from the
8066 di
->disk
.number
= dl
->index
;
8068 /* (ab)use di->devs to store a pointer to the device
8071 di
->devs
= (struct mdinfo
*) dl
;
8073 di
->disk
.raid_disk
= i
;
8074 di
->disk
.major
= dl
->major
;
8075 di
->disk
.minor
= dl
->minor
;
8077 di
->recovery_start
= 0;
8078 di
->data_offset
= pba_of_lba0(map
);
8079 di
->component_size
= a
->info
.component_size
;
8080 di
->container_member
= inst
;
8081 super
->random
= random32();
8085 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
8086 i
, di
->data_offset
);
8090 /* No spares found */
8092 /* Now 'rv' has a list of devices to return.
8093 * Create a metadata_update record to update the
8094 * disk_ord_tbl for the array
8096 mu
= xmalloc(sizeof(*mu
));
8097 mu
->buf
= xcalloc(num_spares
,
8098 sizeof(struct imsm_update_activate_spare
));
8100 mu
->space_list
= NULL
;
8101 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
8102 mu
->next
= *updates
;
8103 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
8105 for (di
= rv
; di
; di
= di
->next
) {
8106 u
->type
= update_activate_spare
;
8107 u
->dl
= (struct dl
*) di
->devs
;
8109 u
->slot
= di
->disk
.raid_disk
;
8120 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
8122 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
8123 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8124 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, MAP_0
);
8125 struct disk_info
*inf
= get_disk_info(u
);
8126 struct imsm_disk
*disk
;
8130 for (i
= 0; i
< map
->num_members
; i
++) {
8131 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, MAP_X
));
8132 for (j
= 0; j
< new_map
->num_members
; j
++)
8133 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
8140 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
8144 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
8145 if (dl
->major
== major
&& dl
->minor
== minor
)
8150 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
8156 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
8157 if (dl
->major
== major
&& dl
->minor
== minor
) {
8160 prev
->next
= dl
->next
;
8162 super
->disks
= dl
->next
;
8164 __free_imsm_disk(dl
);
8165 dprintf("removed %x:%x\n", major
, minor
);
8173 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
8175 static int add_remove_disk_update(struct intel_super
*super
)
8177 int check_degraded
= 0;
8180 /* add/remove some spares to/from the metadata/contrainer */
8181 while (super
->disk_mgmt_list
) {
8182 struct dl
*disk_cfg
;
8184 disk_cfg
= super
->disk_mgmt_list
;
8185 super
->disk_mgmt_list
= disk_cfg
->next
;
8186 disk_cfg
->next
= NULL
;
8188 if (disk_cfg
->action
== DISK_ADD
) {
8189 disk_cfg
->next
= super
->disks
;
8190 super
->disks
= disk_cfg
;
8192 dprintf("added %x:%x\n",
8193 disk_cfg
->major
, disk_cfg
->minor
);
8194 } else if (disk_cfg
->action
== DISK_REMOVE
) {
8195 dprintf("Disk remove action processed: %x.%x\n",
8196 disk_cfg
->major
, disk_cfg
->minor
);
8197 disk
= get_disk_super(super
,
8201 /* store action status */
8202 disk
->action
= DISK_REMOVE
;
8203 /* remove spare disks only */
8204 if (disk
->index
== -1) {
8205 remove_disk_super(super
,
8210 /* release allocate disk structure */
8211 __free_imsm_disk(disk_cfg
);
8214 return check_degraded
;
8217 static int apply_reshape_migration_update(struct imsm_update_reshape_migration
*u
,
8218 struct intel_super
*super
,
8221 struct intel_dev
*id
;
8222 void **tofree
= NULL
;
8225 dprintf("(enter)\n");
8226 if (u
->subdev
< 0 || u
->subdev
> 1) {
8227 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
8230 if (space_list
== NULL
|| *space_list
== NULL
) {
8231 dprintf("imsm: Error: Memory is not allocated\n");
8235 for (id
= super
->devlist
; id
; id
= id
->next
) {
8236 if (id
->index
== (unsigned)u
->subdev
) {
8237 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
8238 struct imsm_map
*map
;
8239 struct imsm_dev
*new_dev
=
8240 (struct imsm_dev
*)*space_list
;
8241 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
8243 struct dl
*new_disk
;
8245 if (new_dev
== NULL
)
8247 *space_list
= **space_list
;
8248 memcpy(new_dev
, dev
, sizeof_imsm_dev(dev
, 0));
8249 map
= get_imsm_map(new_dev
, MAP_0
);
8251 dprintf("imsm: Error: migration in progress");
8255 to_state
= map
->map_state
;
8256 if ((u
->new_level
== 5) && (map
->raid_level
== 0)) {
8258 /* this should not happen */
8259 if (u
->new_disks
[0] < 0) {
8260 map
->failed_disk_num
=
8261 map
->num_members
- 1;
8262 to_state
= IMSM_T_STATE_DEGRADED
;
8264 to_state
= IMSM_T_STATE_NORMAL
;
8266 migrate(new_dev
, super
, to_state
, MIGR_GEN_MIGR
);
8267 if (u
->new_level
> -1)
8268 map
->raid_level
= u
->new_level
;
8269 migr_map
= get_imsm_map(new_dev
, MAP_1
);
8270 if ((u
->new_level
== 5) &&
8271 (migr_map
->raid_level
== 0)) {
8272 int ord
= map
->num_members
- 1;
8273 migr_map
->num_members
--;
8274 if (u
->new_disks
[0] < 0)
8275 ord
|= IMSM_ORD_REBUILD
;
8276 set_imsm_ord_tbl_ent(map
,
8277 map
->num_members
- 1,
8281 tofree
= (void **)dev
;
8283 /* update chunk size
8285 if (u
->new_chunksize
> 0) {
8286 unsigned long long num_data_stripes
;
8288 imsm_num_data_members(dev
, MAP_0
);
8290 if (used_disks
== 0)
8293 map
->blocks_per_strip
=
8294 __cpu_to_le16(u
->new_chunksize
* 2);
8296 (join_u32(dev
->size_low
, dev
->size_high
)
8298 num_data_stripes
/= map
->blocks_per_strip
;
8299 num_data_stripes
/= map
->num_domains
;
8300 set_num_data_stripes(map
, num_data_stripes
);
8305 if (u
->new_level
!= 5 || migr_map
->raid_level
!= 0 ||
8306 migr_map
->raid_level
== map
->raid_level
)
8309 if (u
->new_disks
[0] >= 0) {
8312 new_disk
= get_disk_super(super
,
8313 major(u
->new_disks
[0]),
8314 minor(u
->new_disks
[0]));
8315 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
8316 major(u
->new_disks
[0]),
8317 minor(u
->new_disks
[0]),
8318 new_disk
, new_disk
->index
);
8319 if (new_disk
== NULL
)
8320 goto error_disk_add
;
8322 new_disk
->index
= map
->num_members
- 1;
8323 /* slot to fill in autolayout
8325 new_disk
->raiddisk
= new_disk
->index
;
8326 new_disk
->disk
.status
|= CONFIGURED_DISK
;
8327 new_disk
->disk
.status
&= ~SPARE_DISK
;
8329 goto error_disk_add
;
8332 *tofree
= *space_list
;
8333 /* calculate new size
8335 imsm_set_array_size(new_dev
, -1);
8342 *space_list
= tofree
;
8346 dprintf("Error: imsm: Cannot find disk.\n");
8350 static int apply_size_change_update(struct imsm_update_size_change
*u
,
8351 struct intel_super
*super
)
8353 struct intel_dev
*id
;
8356 dprintf("(enter)\n");
8357 if (u
->subdev
< 0 || u
->subdev
> 1) {
8358 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
8362 for (id
= super
->devlist
; id
; id
= id
->next
) {
8363 if (id
->index
== (unsigned)u
->subdev
) {
8364 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
8365 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8366 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
8367 unsigned long long blocks_per_member
;
8368 unsigned long long num_data_stripes
;
8370 /* calculate new size
8372 blocks_per_member
= u
->new_size
/ used_disks
;
8373 num_data_stripes
= blocks_per_member
/
8374 map
->blocks_per_strip
;
8375 num_data_stripes
/= map
->num_domains
;
8376 dprintf("(size: %llu, blocks per member: %llu, num_data_stipes: %llu)\n",
8377 u
->new_size
, blocks_per_member
,
8379 set_blocks_per_member(map
, blocks_per_member
);
8380 set_num_data_stripes(map
, num_data_stripes
);
8381 imsm_set_array_size(dev
, u
->new_size
);
8391 static int apply_update_activate_spare(struct imsm_update_activate_spare
*u
,
8392 struct intel_super
*super
,
8393 struct active_array
*active_array
)
8395 struct imsm_super
*mpb
= super
->anchor
;
8396 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
8397 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8398 struct imsm_map
*migr_map
;
8399 struct active_array
*a
;
8400 struct imsm_disk
*disk
;
8407 int second_map_created
= 0;
8409 for (; u
; u
= u
->next
) {
8410 victim
= get_imsm_disk_idx(dev
, u
->slot
, MAP_X
);
8415 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
8420 pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n",
8425 /* count failures (excluding rebuilds and the victim)
8426 * to determine map[0] state
8429 for (i
= 0; i
< map
->num_members
; i
++) {
8432 disk
= get_imsm_disk(super
,
8433 get_imsm_disk_idx(dev
, i
, MAP_X
));
8434 if (!disk
|| is_failed(disk
))
8438 /* adding a pristine spare, assign a new index */
8439 if (dl
->index
< 0) {
8440 dl
->index
= super
->anchor
->num_disks
;
8441 super
->anchor
->num_disks
++;
8444 disk
->status
|= CONFIGURED_DISK
;
8445 disk
->status
&= ~SPARE_DISK
;
8448 to_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8449 if (!second_map_created
) {
8450 second_map_created
= 1;
8451 map
->map_state
= IMSM_T_STATE_DEGRADED
;
8452 migrate(dev
, super
, to_state
, MIGR_REBUILD
);
8454 map
->map_state
= to_state
;
8455 migr_map
= get_imsm_map(dev
, MAP_1
);
8456 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
8457 set_imsm_ord_tbl_ent(migr_map
, u
->slot
,
8458 dl
->index
| IMSM_ORD_REBUILD
);
8460 /* update the family_num to mark a new container
8461 * generation, being careful to record the existing
8462 * family_num in orig_family_num to clean up after
8463 * earlier mdadm versions that neglected to set it.
8465 if (mpb
->orig_family_num
== 0)
8466 mpb
->orig_family_num
= mpb
->family_num
;
8467 mpb
->family_num
+= super
->random
;
8469 /* count arrays using the victim in the metadata */
8471 for (a
= active_array
; a
; a
= a
->next
) {
8472 dev
= get_imsm_dev(super
, a
->info
.container_member
);
8473 map
= get_imsm_map(dev
, MAP_0
);
8475 if (get_imsm_disk_slot(map
, victim
) >= 0)
8479 /* delete the victim if it is no longer being
8485 /* We know that 'manager' isn't touching anything,
8486 * so it is safe to delete
8488 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
8489 if ((*dlp
)->index
== victim
)
8492 /* victim may be on the missing list */
8494 for (dlp
= &super
->missing
; *dlp
;
8495 dlp
= &(*dlp
)->next
)
8496 if ((*dlp
)->index
== victim
)
8498 imsm_delete(super
, dlp
, victim
);
8505 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
8506 struct intel_super
*super
,
8509 struct dl
*new_disk
;
8510 struct intel_dev
*id
;
8512 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
8513 int disk_count
= u
->old_raid_disks
;
8514 void **tofree
= NULL
;
8515 int devices_to_reshape
= 1;
8516 struct imsm_super
*mpb
= super
->anchor
;
8518 unsigned int dev_id
;
8520 dprintf("(enter)\n");
8522 /* enable spares to use in array */
8523 for (i
= 0; i
< delta_disks
; i
++) {
8524 new_disk
= get_disk_super(super
,
8525 major(u
->new_disks
[i
]),
8526 minor(u
->new_disks
[i
]));
8527 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
8528 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
8529 new_disk
, new_disk
->index
);
8530 if (new_disk
== NULL
||
8531 (new_disk
->index
>= 0 &&
8532 new_disk
->index
< u
->old_raid_disks
))
8533 goto update_reshape_exit
;
8534 new_disk
->index
= disk_count
++;
8535 /* slot to fill in autolayout
8537 new_disk
->raiddisk
= new_disk
->index
;
8538 new_disk
->disk
.status
|=
8540 new_disk
->disk
.status
&= ~SPARE_DISK
;
8543 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
8544 mpb
->num_raid_devs
);
8545 /* manage changes in volume
8547 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
8548 void **sp
= *space_list
;
8549 struct imsm_dev
*newdev
;
8550 struct imsm_map
*newmap
, *oldmap
;
8552 for (id
= super
->devlist
; id
; id
= id
->next
) {
8553 if (id
->index
== dev_id
)
8562 /* Copy the dev, but not (all of) the map */
8563 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
8564 oldmap
= get_imsm_map(id
->dev
, MAP_0
);
8565 newmap
= get_imsm_map(newdev
, MAP_0
);
8566 /* Copy the current map */
8567 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8568 /* update one device only
8570 if (devices_to_reshape
) {
8571 dprintf("imsm: modifying subdev: %i\n",
8573 devices_to_reshape
--;
8574 newdev
->vol
.migr_state
= 1;
8575 newdev
->vol
.curr_migr_unit
= 0;
8576 set_migr_type(newdev
, MIGR_GEN_MIGR
);
8577 newmap
->num_members
= u
->new_raid_disks
;
8578 for (i
= 0; i
< delta_disks
; i
++) {
8579 set_imsm_ord_tbl_ent(newmap
,
8580 u
->old_raid_disks
+ i
,
8581 u
->old_raid_disks
+ i
);
8583 /* New map is correct, now need to save old map
8585 newmap
= get_imsm_map(newdev
, MAP_1
);
8586 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8588 imsm_set_array_size(newdev
, -1);
8591 sp
= (void **)id
->dev
;
8596 /* Clear migration record */
8597 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
8600 *space_list
= tofree
;
8603 update_reshape_exit
:
8608 static int apply_takeover_update(struct imsm_update_takeover
*u
,
8609 struct intel_super
*super
,
8612 struct imsm_dev
*dev
= NULL
;
8613 struct intel_dev
*dv
;
8614 struct imsm_dev
*dev_new
;
8615 struct imsm_map
*map
;
8619 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
8620 if (dv
->index
== (unsigned int)u
->subarray
) {
8628 map
= get_imsm_map(dev
, MAP_0
);
8630 if (u
->direction
== R10_TO_R0
) {
8631 unsigned long long num_data_stripes
;
8633 map
->num_domains
= 1;
8634 num_data_stripes
= blocks_per_member(map
);
8635 num_data_stripes
/= map
->blocks_per_strip
;
8636 num_data_stripes
/= map
->num_domains
;
8637 set_num_data_stripes(map
, num_data_stripes
);
8639 /* Number of failed disks must be half of initial disk number */
8640 if (imsm_count_failed(super
, dev
, MAP_0
) !=
8641 (map
->num_members
/ 2))
8644 /* iterate through devices to mark removed disks as spare */
8645 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8646 if (dm
->disk
.status
& FAILED_DISK
) {
8647 int idx
= dm
->index
;
8648 /* update indexes on the disk list */
8649 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
8650 the index values will end up being correct.... NB */
8651 for (du
= super
->disks
; du
; du
= du
->next
)
8652 if (du
->index
> idx
)
8654 /* mark as spare disk */
8659 map
->num_members
= map
->num_members
/ 2;
8660 map
->map_state
= IMSM_T_STATE_NORMAL
;
8661 map
->num_domains
= 1;
8662 map
->raid_level
= 0;
8663 map
->failed_disk_num
= -1;
8666 if (u
->direction
== R0_TO_R10
) {
8668 /* update slots in current disk list */
8669 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8673 /* create new *missing* disks */
8674 for (i
= 0; i
< map
->num_members
; i
++) {
8675 space
= *space_list
;
8678 *space_list
= *space
;
8680 memcpy(du
, super
->disks
, sizeof(*du
));
8684 du
->index
= (i
* 2) + 1;
8685 sprintf((char *)du
->disk
.serial
,
8686 " MISSING_%d", du
->index
);
8687 sprintf((char *)du
->serial
,
8688 "MISSING_%d", du
->index
);
8689 du
->next
= super
->missing
;
8690 super
->missing
= du
;
8692 /* create new dev and map */
8693 space
= *space_list
;
8696 *space_list
= *space
;
8697 dev_new
= (void *)space
;
8698 memcpy(dev_new
, dev
, sizeof(*dev
));
8699 /* update new map */
8700 map
= get_imsm_map(dev_new
, MAP_0
);
8701 map
->num_members
= map
->num_members
* 2;
8702 map
->map_state
= IMSM_T_STATE_DEGRADED
;
8703 map
->num_domains
= 2;
8704 map
->raid_level
= 1;
8705 /* replace dev<->dev_new */
8708 /* update disk order table */
8709 for (du
= super
->disks
; du
; du
= du
->next
)
8711 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8712 for (du
= super
->missing
; du
; du
= du
->next
)
8713 if (du
->index
>= 0) {
8714 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8715 mark_missing(dv
->dev
, &du
->disk
, du
->index
);
8721 static void imsm_process_update(struct supertype
*st
,
8722 struct metadata_update
*update
)
8725 * crack open the metadata_update envelope to find the update record
8726 * update can be one of:
8727 * update_reshape_container_disks - all the arrays in the container
8728 * are being reshaped to have more devices. We need to mark
8729 * the arrays for general migration and convert selected spares
8730 * into active devices.
8731 * update_activate_spare - a spare device has replaced a failed
8732 * device in an array, update the disk_ord_tbl. If this disk is
8733 * present in all member arrays then also clear the SPARE_DISK
8735 * update_create_array
8737 * update_rename_array
8738 * update_add_remove_disk
8740 struct intel_super
*super
= st
->sb
;
8741 struct imsm_super
*mpb
;
8742 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
8744 /* update requires a larger buf but the allocation failed */
8745 if (super
->next_len
&& !super
->next_buf
) {
8746 super
->next_len
= 0;
8750 if (super
->next_buf
) {
8751 memcpy(super
->next_buf
, super
->buf
, super
->len
);
8753 super
->len
= super
->next_len
;
8754 super
->buf
= super
->next_buf
;
8756 super
->next_len
= 0;
8757 super
->next_buf
= NULL
;
8760 mpb
= super
->anchor
;
8763 case update_general_migration_checkpoint
: {
8764 struct intel_dev
*id
;
8765 struct imsm_update_general_migration_checkpoint
*u
=
8766 (void *)update
->buf
;
8768 dprintf("called for update_general_migration_checkpoint\n");
8770 /* find device under general migration */
8771 for (id
= super
->devlist
; id
; id
= id
->next
) {
8772 if (is_gen_migration(id
->dev
)) {
8773 id
->dev
->vol
.curr_migr_unit
=
8774 __cpu_to_le32(u
->curr_migr_unit
);
8775 super
->updates_pending
++;
8780 case update_takeover
: {
8781 struct imsm_update_takeover
*u
= (void *)update
->buf
;
8782 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
8783 imsm_update_version_info(super
);
8784 super
->updates_pending
++;
8789 case update_reshape_container_disks
: {
8790 struct imsm_update_reshape
*u
= (void *)update
->buf
;
8791 if (apply_reshape_container_disks_update(
8792 u
, super
, &update
->space_list
))
8793 super
->updates_pending
++;
8796 case update_reshape_migration
: {
8797 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
8798 if (apply_reshape_migration_update(
8799 u
, super
, &update
->space_list
))
8800 super
->updates_pending
++;
8803 case update_size_change
: {
8804 struct imsm_update_size_change
*u
= (void *)update
->buf
;
8805 if (apply_size_change_update(u
, super
))
8806 super
->updates_pending
++;
8809 case update_activate_spare
: {
8810 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
8811 if (apply_update_activate_spare(u
, super
, st
->arrays
))
8812 super
->updates_pending
++;
8815 case update_create_array
: {
8816 /* someone wants to create a new array, we need to be aware of
8817 * a few races/collisions:
8818 * 1/ 'Create' called by two separate instances of mdadm
8819 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
8820 * devices that have since been assimilated via
8822 * In the event this update can not be carried out mdadm will
8823 * (FIX ME) notice that its update did not take hold.
8825 struct imsm_update_create_array
*u
= (void *) update
->buf
;
8826 struct intel_dev
*dv
;
8827 struct imsm_dev
*dev
;
8828 struct imsm_map
*map
, *new_map
;
8829 unsigned long long start
, end
;
8830 unsigned long long new_start
, new_end
;
8832 struct disk_info
*inf
;
8835 /* handle racing creates: first come first serve */
8836 if (u
->dev_idx
< mpb
->num_raid_devs
) {
8837 dprintf("subarray %d already defined\n", u
->dev_idx
);
8841 /* check update is next in sequence */
8842 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
8843 dprintf("can not create array %d expected index %d\n",
8844 u
->dev_idx
, mpb
->num_raid_devs
);
8848 new_map
= get_imsm_map(&u
->dev
, MAP_0
);
8849 new_start
= pba_of_lba0(new_map
);
8850 new_end
= new_start
+ blocks_per_member(new_map
);
8851 inf
= get_disk_info(u
);
8853 /* handle activate_spare versus create race:
8854 * check to make sure that overlapping arrays do not include
8857 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8858 dev
= get_imsm_dev(super
, i
);
8859 map
= get_imsm_map(dev
, MAP_0
);
8860 start
= pba_of_lba0(map
);
8861 end
= start
+ blocks_per_member(map
);
8862 if ((new_start
>= start
&& new_start
<= end
) ||
8863 (start
>= new_start
&& start
<= new_end
))
8868 if (disks_overlap(super
, i
, u
)) {
8869 dprintf("arrays overlap\n");
8874 /* check that prepare update was successful */
8875 if (!update
->space
) {
8876 dprintf("prepare update failed\n");
8880 /* check that all disks are still active before committing
8881 * changes. FIXME: could we instead handle this by creating a
8882 * degraded array? That's probably not what the user expects,
8883 * so better to drop this update on the floor.
8885 for (i
= 0; i
< new_map
->num_members
; i
++) {
8886 dl
= serial_to_dl(inf
[i
].serial
, super
);
8888 dprintf("disk disappeared\n");
8893 super
->updates_pending
++;
8895 /* convert spares to members and fixup ord_tbl */
8896 for (i
= 0; i
< new_map
->num_members
; i
++) {
8897 dl
= serial_to_dl(inf
[i
].serial
, super
);
8898 if (dl
->index
== -1) {
8899 dl
->index
= mpb
->num_disks
;
8901 dl
->disk
.status
|= CONFIGURED_DISK
;
8902 dl
->disk
.status
&= ~SPARE_DISK
;
8904 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
8909 update
->space
= NULL
;
8910 imsm_copy_dev(dev
, &u
->dev
);
8911 dv
->index
= u
->dev_idx
;
8912 dv
->next
= super
->devlist
;
8913 super
->devlist
= dv
;
8914 mpb
->num_raid_devs
++;
8916 imsm_update_version_info(super
);
8919 /* mdmon knows how to release update->space, but not
8920 * ((struct intel_dev *) update->space)->dev
8922 if (update
->space
) {
8928 case update_kill_array
: {
8929 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
8930 int victim
= u
->dev_idx
;
8931 struct active_array
*a
;
8932 struct intel_dev
**dp
;
8933 struct imsm_dev
*dev
;
8935 /* sanity check that we are not affecting the uuid of
8936 * active arrays, or deleting an active array
8938 * FIXME when immutable ids are available, but note that
8939 * we'll also need to fixup the invalidated/active
8940 * subarray indexes in mdstat
8942 for (a
= st
->arrays
; a
; a
= a
->next
)
8943 if (a
->info
.container_member
>= victim
)
8945 /* by definition if mdmon is running at least one array
8946 * is active in the container, so checking
8947 * mpb->num_raid_devs is just extra paranoia
8949 dev
= get_imsm_dev(super
, victim
);
8950 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
8951 dprintf("failed to delete subarray-%d\n", victim
);
8955 for (dp
= &super
->devlist
; *dp
;)
8956 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
8959 if ((*dp
)->index
> (unsigned)victim
)
8963 mpb
->num_raid_devs
--;
8964 super
->updates_pending
++;
8967 case update_rename_array
: {
8968 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
8969 char name
[MAX_RAID_SERIAL_LEN
+1];
8970 int target
= u
->dev_idx
;
8971 struct active_array
*a
;
8972 struct imsm_dev
*dev
;
8974 /* sanity check that we are not affecting the uuid of
8977 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
8978 name
[MAX_RAID_SERIAL_LEN
] = '\0';
8979 for (a
= st
->arrays
; a
; a
= a
->next
)
8980 if (a
->info
.container_member
== target
)
8982 dev
= get_imsm_dev(super
, u
->dev_idx
);
8983 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
8984 dprintf("failed to rename subarray-%d\n", target
);
8988 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
8989 super
->updates_pending
++;
8992 case update_add_remove_disk
: {
8993 /* we may be able to repair some arrays if disks are
8994 * being added, check the status of add_remove_disk
8995 * if discs has been added.
8997 if (add_remove_disk_update(super
)) {
8998 struct active_array
*a
;
9000 super
->updates_pending
++;
9001 for (a
= st
->arrays
; a
; a
= a
->next
)
9002 a
->check_degraded
= 1;
9007 pr_err("error: unsuported process update type:(type: %d)\n", type
);
9011 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
);
9013 static int imsm_prepare_update(struct supertype
*st
,
9014 struct metadata_update
*update
)
9017 * Allocate space to hold new disk entries, raid-device entries or a new
9018 * mpb if necessary. The manager synchronously waits for updates to
9019 * complete in the monitor, so new mpb buffers allocated here can be
9020 * integrated by the monitor thread without worrying about live pointers
9021 * in the manager thread.
9023 enum imsm_update_type type
;
9024 struct intel_super
*super
= st
->sb
;
9025 unsigned int sector_size
= super
->sector_size
;
9026 struct imsm_super
*mpb
= super
->anchor
;
9030 if (update
->len
< (int)sizeof(type
))
9033 type
= *(enum imsm_update_type
*) update
->buf
;
9036 case update_general_migration_checkpoint
:
9037 if (update
->len
< (int)sizeof(struct imsm_update_general_migration_checkpoint
))
9039 dprintf("called for update_general_migration_checkpoint\n");
9041 case update_takeover
: {
9042 struct imsm_update_takeover
*u
= (void *)update
->buf
;
9043 if (update
->len
< (int)sizeof(*u
))
9045 if (u
->direction
== R0_TO_R10
) {
9046 void **tail
= (void **)&update
->space_list
;
9047 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
9048 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9049 int num_members
= map
->num_members
;
9052 /* allocate memory for added disks */
9053 for (i
= 0; i
< num_members
; i
++) {
9054 size
= sizeof(struct dl
);
9055 space
= xmalloc(size
);
9060 /* allocate memory for new device */
9061 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
9062 (num_members
* sizeof(__u32
));
9063 space
= xmalloc(size
);
9067 len
= disks_to_mpb_size(num_members
* 2);
9072 case update_reshape_container_disks
: {
9073 /* Every raid device in the container is about to
9074 * gain some more devices, and we will enter a
9076 * So each 'imsm_map' will be bigger, and the imsm_vol
9077 * will now hold 2 of them.
9078 * Thus we need new 'struct imsm_dev' allocations sized
9079 * as sizeof_imsm_dev but with more devices in both maps.
9081 struct imsm_update_reshape
*u
= (void *)update
->buf
;
9082 struct intel_dev
*dl
;
9083 void **space_tail
= (void**)&update
->space_list
;
9085 if (update
->len
< (int)sizeof(*u
))
9088 dprintf("for update_reshape\n");
9090 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
9091 int size
= sizeof_imsm_dev(dl
->dev
, 1);
9093 if (u
->new_raid_disks
> u
->old_raid_disks
)
9094 size
+= sizeof(__u32
)*2*
9095 (u
->new_raid_disks
- u
->old_raid_disks
);
9102 len
= disks_to_mpb_size(u
->new_raid_disks
);
9103 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
9106 case update_reshape_migration
: {
9107 /* for migration level 0->5 we need to add disks
9108 * so the same as for container operation we will copy
9109 * device to the bigger location.
9110 * in memory prepared device and new disk area are prepared
9111 * for usage in process update
9113 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
9114 struct intel_dev
*id
;
9115 void **space_tail
= (void **)&update
->space_list
;
9118 int current_level
= -1;
9120 if (update
->len
< (int)sizeof(*u
))
9123 dprintf("for update_reshape\n");
9125 /* add space for bigger array in update
9127 for (id
= super
->devlist
; id
; id
= id
->next
) {
9128 if (id
->index
== (unsigned)u
->subdev
) {
9129 size
= sizeof_imsm_dev(id
->dev
, 1);
9130 if (u
->new_raid_disks
> u
->old_raid_disks
)
9131 size
+= sizeof(__u32
)*2*
9132 (u
->new_raid_disks
- u
->old_raid_disks
);
9140 if (update
->space_list
== NULL
)
9143 /* add space for disk in update
9145 size
= sizeof(struct dl
);
9151 /* add spare device to update
9153 for (id
= super
->devlist
; id
; id
= id
->next
)
9154 if (id
->index
== (unsigned)u
->subdev
) {
9155 struct imsm_dev
*dev
;
9156 struct imsm_map
*map
;
9158 dev
= get_imsm_dev(super
, u
->subdev
);
9159 map
= get_imsm_map(dev
, MAP_0
);
9160 current_level
= map
->raid_level
;
9163 if (u
->new_level
== 5 && u
->new_level
!= current_level
) {
9164 struct mdinfo
*spares
;
9166 spares
= get_spares_for_grow(st
);
9174 makedev(dev
->disk
.major
,
9176 dl
= get_disk_super(super
,
9179 dl
->index
= u
->old_raid_disks
;
9185 len
= disks_to_mpb_size(u
->new_raid_disks
);
9186 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
9189 case update_size_change
: {
9190 if (update
->len
< (int)sizeof(struct imsm_update_size_change
))
9194 case update_activate_spare
: {
9195 if (update
->len
< (int)sizeof(struct imsm_update_activate_spare
))
9199 case update_create_array
: {
9200 struct imsm_update_create_array
*u
= (void *) update
->buf
;
9201 struct intel_dev
*dv
;
9202 struct imsm_dev
*dev
= &u
->dev
;
9203 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9205 struct disk_info
*inf
;
9209 if (update
->len
< (int)sizeof(*u
))
9212 inf
= get_disk_info(u
);
9213 len
= sizeof_imsm_dev(dev
, 1);
9214 /* allocate a new super->devlist entry */
9215 dv
= xmalloc(sizeof(*dv
));
9216 dv
->dev
= xmalloc(len
);
9219 /* count how many spares will be converted to members */
9220 for (i
= 0; i
< map
->num_members
; i
++) {
9221 dl
= serial_to_dl(inf
[i
].serial
, super
);
9223 /* hmm maybe it failed?, nothing we can do about
9228 if (count_memberships(dl
, super
) == 0)
9231 len
+= activate
* sizeof(struct imsm_disk
);
9234 case update_kill_array
: {
9235 if (update
->len
< (int)sizeof(struct imsm_update_kill_array
))
9239 case update_rename_array
: {
9240 if (update
->len
< (int)sizeof(struct imsm_update_rename_array
))
9244 case update_add_remove_disk
:
9245 /* no update->len needed */
9251 /* check if we need a larger metadata buffer */
9252 if (super
->next_buf
)
9253 buf_len
= super
->next_len
;
9255 buf_len
= super
->len
;
9257 if (__le32_to_cpu(mpb
->mpb_size
) + len
> buf_len
) {
9258 /* ok we need a larger buf than what is currently allocated
9259 * if this allocation fails process_update will notice that
9260 * ->next_len is set and ->next_buf is NULL
9262 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + len
,
9264 if (super
->next_buf
)
9265 free(super
->next_buf
);
9267 super
->next_len
= buf_len
;
9268 if (posix_memalign(&super
->next_buf
, sector_size
, buf_len
) == 0)
9269 memset(super
->next_buf
, 0, buf_len
);
9271 super
->next_buf
= NULL
;
9276 /* must be called while manager is quiesced */
9277 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
9279 struct imsm_super
*mpb
= super
->anchor
;
9281 struct imsm_dev
*dev
;
9282 struct imsm_map
*map
;
9283 int i
, j
, num_members
;
9286 dprintf("deleting device[%d] from imsm_super\n", index
);
9288 /* shift all indexes down one */
9289 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
9290 if (iter
->index
> (int)index
)
9292 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
9293 if (iter
->index
> (int)index
)
9296 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
9297 dev
= get_imsm_dev(super
, i
);
9298 map
= get_imsm_map(dev
, MAP_0
);
9299 num_members
= map
->num_members
;
9300 for (j
= 0; j
< num_members
; j
++) {
9301 /* update ord entries being careful not to propagate
9302 * ord-flags to the first map
9304 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_X
);
9306 if (ord_to_idx(ord
) <= index
)
9309 map
= get_imsm_map(dev
, MAP_0
);
9310 set_imsm_ord_tbl_ent(map
, j
, ord_to_idx(ord
- 1));
9311 map
= get_imsm_map(dev
, MAP_1
);
9313 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
9318 super
->updates_pending
++;
9320 struct dl
*dl
= *dlp
;
9322 *dlp
= (*dlp
)->next
;
9323 __free_imsm_disk(dl
);
9326 #endif /* MDASSEMBLE */
9328 static void close_targets(int *targets
, int new_disks
)
9335 for (i
= 0; i
< new_disks
; i
++) {
9336 if (targets
[i
] >= 0) {
9343 static int imsm_get_allowed_degradation(int level
, int raid_disks
,
9344 struct intel_super
*super
,
9345 struct imsm_dev
*dev
)
9351 struct imsm_map
*map
;
9354 ret_val
= raid_disks
/2;
9355 /* check map if all disks pairs not failed
9358 map
= get_imsm_map(dev
, MAP_0
);
9359 for (i
= 0; i
< ret_val
; i
++) {
9360 int degradation
= 0;
9361 if (get_imsm_disk(super
, i
) == NULL
)
9363 if (get_imsm_disk(super
, i
+ 1) == NULL
)
9365 if (degradation
== 2)
9368 map
= get_imsm_map(dev
, MAP_1
);
9369 /* if there is no second map
9370 * result can be returned
9374 /* check degradation in second map
9376 for (i
= 0; i
< ret_val
; i
++) {
9377 int degradation
= 0;
9378 if (get_imsm_disk(super
, i
) == NULL
)
9380 if (get_imsm_disk(super
, i
+ 1) == NULL
)
9382 if (degradation
== 2)
9396 /*******************************************************************************
9397 * Function: open_backup_targets
9398 * Description: Function opens file descriptors for all devices given in
9401 * info : general array info
9402 * raid_disks : number of disks
9403 * raid_fds : table of device's file descriptors
9404 * super : intel super for raid10 degradation check
9405 * dev : intel device for raid10 degradation check
9409 ******************************************************************************/
9410 int open_backup_targets(struct mdinfo
*info
, int raid_disks
, int *raid_fds
,
9411 struct intel_super
*super
, struct imsm_dev
*dev
)
9417 for (i
= 0; i
< raid_disks
; i
++)
9420 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
9423 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
)) {
9424 dprintf("disk is faulty!!\n");
9428 if (sd
->disk
.raid_disk
>= raid_disks
|| sd
->disk
.raid_disk
< 0)
9431 dn
= map_dev(sd
->disk
.major
,
9433 raid_fds
[sd
->disk
.raid_disk
] = dev_open(dn
, O_RDWR
);
9434 if (raid_fds
[sd
->disk
.raid_disk
] < 0) {
9435 pr_err("cannot open component\n");
9440 /* check if maximum array degradation level is not exceeded
9442 if ((raid_disks
- opened
) >
9443 imsm_get_allowed_degradation(info
->new_level
, raid_disks
,
9445 pr_err("Not enough disks can be opened.\n");
9446 close_targets(raid_fds
, raid_disks
);
9452 /*******************************************************************************
9453 * Function: validate_container_imsm
9454 * Description: This routine validates container after assemble,
9455 * eg. if devices in container are under the same controller.
9458 * info : linked list with info about devices used in array
9462 ******************************************************************************/
9463 int validate_container_imsm(struct mdinfo
*info
)
9465 if (check_env("IMSM_NO_PLATFORM"))
9468 struct sys_dev
*idev
;
9469 struct sys_dev
*hba
= NULL
;
9470 struct sys_dev
*intel_devices
= find_intel_devices();
9471 char *dev_path
= devt_to_devpath(makedev(info
->disk
.major
,
9474 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
9475 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
9484 pr_err("WARNING - Cannot detect HBA for device %s!\n",
9485 devid2kname(makedev(info
->disk
.major
, info
->disk
.minor
)));
9489 const struct imsm_orom
*orom
= get_orom_by_device_id(hba
->dev_id
);
9492 for (dev
= info
->next
; dev
; dev
= dev
->next
) {
9493 dev_path
= devt_to_devpath(makedev(dev
->disk
.major
, dev
->disk
.minor
));
9495 struct sys_dev
*hba2
= NULL
;
9496 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
9497 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
9505 const struct imsm_orom
*orom2
= hba2
== NULL
? NULL
:
9506 get_orom_by_device_id(hba2
->dev_id
);
9508 if (hba2
&& hba
->type
!= hba2
->type
) {
9509 pr_err("WARNING - HBAs of devices do not match %s != %s\n",
9510 get_sys_dev_type(hba
->type
), get_sys_dev_type(hba2
->type
));
9514 if (orom
!= orom2
) {
9515 pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n"
9516 " This operation is not supported and can lead to data loss.\n");
9521 pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n"
9522 " This operation is not supported and can lead to data loss.\n");
9530 /*******************************************************************************
9531 * Function: init_migr_record_imsm
9532 * Description: Function inits imsm migration record
9534 * super : imsm internal array info
9535 * dev : device under migration
9536 * info : general array info to find the smallest device
9539 ******************************************************************************/
9540 void init_migr_record_imsm(struct supertype
*st
, struct imsm_dev
*dev
,
9541 struct mdinfo
*info
)
9543 struct intel_super
*super
= st
->sb
;
9544 struct migr_record
*migr_rec
= super
->migr_rec
;
9546 unsigned long long dsize
, dev_sectors
;
9547 long long unsigned min_dev_sectors
= -1LLU;
9551 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
9552 struct imsm_map
*map_src
= get_imsm_map(dev
, MAP_1
);
9553 unsigned long long num_migr_units
;
9554 unsigned long long array_blocks
;
9556 memset(migr_rec
, 0, sizeof(struct migr_record
));
9557 migr_rec
->family_num
= __cpu_to_le32(super
->anchor
->family_num
);
9559 /* only ascending reshape supported now */
9560 migr_rec
->ascending_migr
= __cpu_to_le32(1);
9562 migr_rec
->dest_depth_per_unit
= GEN_MIGR_AREA_SIZE
/
9563 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
9564 migr_rec
->dest_depth_per_unit
*=
9565 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
9566 new_data_disks
= imsm_num_data_members(dev
, MAP_0
);
9567 migr_rec
->blocks_per_unit
=
9568 __cpu_to_le32(migr_rec
->dest_depth_per_unit
* new_data_disks
);
9569 migr_rec
->dest_depth_per_unit
=
9570 __cpu_to_le32(migr_rec
->dest_depth_per_unit
);
9571 array_blocks
= info
->component_size
* new_data_disks
;
9573 array_blocks
/ __le32_to_cpu(migr_rec
->blocks_per_unit
);
9575 if (array_blocks
% __le32_to_cpu(migr_rec
->blocks_per_unit
))
9577 migr_rec
->num_migr_units
= __cpu_to_le32(num_migr_units
);
9579 migr_rec
->post_migr_vol_cap
= dev
->size_low
;
9580 migr_rec
->post_migr_vol_cap_hi
= dev
->size_high
;
9582 /* Find the smallest dev */
9583 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
9584 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
9585 fd
= dev_open(nm
, O_RDONLY
);
9588 get_dev_size(fd
, NULL
, &dsize
);
9589 dev_sectors
= dsize
/ 512;
9590 if (dev_sectors
< min_dev_sectors
)
9591 min_dev_sectors
= dev_sectors
;
9594 migr_rec
->ckpt_area_pba
= __cpu_to_le32(min_dev_sectors
-
9595 RAID_DISK_RESERVED_BLOCKS_IMSM_HI
);
9597 write_imsm_migr_rec(st
);
9602 /*******************************************************************************
9603 * Function: save_backup_imsm
9604 * Description: Function saves critical data stripes to Migration Copy Area
9605 * and updates the current migration unit status.
9606 * Use restore_stripes() to form a destination stripe,
9607 * and to write it to the Copy Area.
9609 * st : supertype information
9610 * dev : imsm device that backup is saved for
9611 * info : general array info
9612 * buf : input buffer
9613 * length : length of data to backup (blocks_per_unit)
9617 ******************************************************************************/
9618 int save_backup_imsm(struct supertype
*st
,
9619 struct imsm_dev
*dev
,
9620 struct mdinfo
*info
,
9625 struct intel_super
*super
= st
->sb
;
9626 unsigned long long *target_offsets
;
9629 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
9630 int new_disks
= map_dest
->num_members
;
9631 int dest_layout
= 0;
9633 unsigned long long start
;
9634 int data_disks
= imsm_num_data_members(dev
, MAP_0
);
9636 targets
= xmalloc(new_disks
* sizeof(int));
9638 for (i
= 0; i
< new_disks
; i
++)
9641 target_offsets
= xcalloc(new_disks
, sizeof(unsigned long long));
9643 start
= info
->reshape_progress
* 512;
9644 for (i
= 0; i
< new_disks
; i
++) {
9645 target_offsets
[i
] = (unsigned long long)
9646 __le32_to_cpu(super
->migr_rec
->ckpt_area_pba
) * 512;
9647 /* move back copy area adderss, it will be moved forward
9648 * in restore_stripes() using start input variable
9650 target_offsets
[i
] -= start
/data_disks
;
9653 if (open_backup_targets(info
, new_disks
, targets
,
9657 dest_layout
= imsm_level_to_layout(map_dest
->raid_level
);
9658 dest_chunk
= __le16_to_cpu(map_dest
->blocks_per_strip
) * 512;
9660 if (restore_stripes(targets
, /* list of dest devices */
9661 target_offsets
, /* migration record offsets */
9664 map_dest
->raid_level
,
9666 -1, /* source backup file descriptor */
9667 0, /* input buf offset
9668 * always 0 buf is already offseted */
9672 pr_err("Error restoring stripes\n");
9680 close_targets(targets
, new_disks
);
9683 free(target_offsets
);
9688 /*******************************************************************************
9689 * Function: save_checkpoint_imsm
9690 * Description: Function called for current unit status update
9691 * in the migration record. It writes it to disk.
9693 * super : imsm internal array info
9694 * info : general array info
9698 * 2: failure, means no valid migration record
9699 * / no general migration in progress /
9700 ******************************************************************************/
9701 int save_checkpoint_imsm(struct supertype
*st
, struct mdinfo
*info
, int state
)
9703 struct intel_super
*super
= st
->sb
;
9704 unsigned long long blocks_per_unit
;
9705 unsigned long long curr_migr_unit
;
9707 if (load_imsm_migr_rec(super
, info
) != 0) {
9708 dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n");
9712 blocks_per_unit
= __le32_to_cpu(super
->migr_rec
->blocks_per_unit
);
9713 if (blocks_per_unit
== 0) {
9714 dprintf("imsm: no migration in progress.\n");
9717 curr_migr_unit
= info
->reshape_progress
/ blocks_per_unit
;
9718 /* check if array is alligned to copy area
9719 * if it is not alligned, add one to current migration unit value
9720 * this can happend on array reshape finish only
9722 if (info
->reshape_progress
% blocks_per_unit
)
9725 super
->migr_rec
->curr_migr_unit
=
9726 __cpu_to_le32(curr_migr_unit
);
9727 super
->migr_rec
->rec_status
= __cpu_to_le32(state
);
9728 super
->migr_rec
->dest_1st_member_lba
=
9729 __cpu_to_le32(curr_migr_unit
*
9730 __le32_to_cpu(super
->migr_rec
->dest_depth_per_unit
));
9731 if (write_imsm_migr_rec(st
) < 0) {
9732 dprintf("imsm: Cannot write migration record outside backup area\n");
9739 /*******************************************************************************
9740 * Function: recover_backup_imsm
9741 * Description: Function recovers critical data from the Migration Copy Area
9742 * while assembling an array.
9744 * super : imsm internal array info
9745 * info : general array info
9747 * 0 : success (or there is no data to recover)
9749 ******************************************************************************/
9750 int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
)
9752 struct intel_super
*super
= st
->sb
;
9753 struct migr_record
*migr_rec
= super
->migr_rec
;
9754 struct imsm_map
*map_dest
;
9755 struct intel_dev
*id
= NULL
;
9756 unsigned long long read_offset
;
9757 unsigned long long write_offset
;
9759 int *targets
= NULL
;
9760 int new_disks
, i
, err
;
9763 unsigned int sector_size
= super
->sector_size
;
9764 unsigned long curr_migr_unit
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
9765 unsigned long num_migr_units
= __le32_to_cpu(migr_rec
->num_migr_units
);
9767 int skipped_disks
= 0;
9769 err
= sysfs_get_str(info
, NULL
, "array_state", (char *)buffer
, 20);
9773 /* recover data only during assemblation */
9774 if (strncmp(buffer
, "inactive", 8) != 0)
9776 /* no data to recover */
9777 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
9779 if (curr_migr_unit
>= num_migr_units
)
9782 /* find device during reshape */
9783 for (id
= super
->devlist
; id
; id
= id
->next
)
9784 if (is_gen_migration(id
->dev
))
9789 map_dest
= get_imsm_map(id
->dev
, MAP_0
);
9790 new_disks
= map_dest
->num_members
;
9792 read_offset
= (unsigned long long)
9793 __le32_to_cpu(migr_rec
->ckpt_area_pba
) * 512;
9795 write_offset
= ((unsigned long long)
9796 __le32_to_cpu(migr_rec
->dest_1st_member_lba
) +
9797 pba_of_lba0(map_dest
)) * 512;
9799 unit_len
= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
9800 if (posix_memalign((void **)&buf
, sector_size
, unit_len
) != 0)
9802 targets
= xcalloc(new_disks
, sizeof(int));
9804 if (open_backup_targets(info
, new_disks
, targets
, super
, id
->dev
)) {
9805 pr_err("Cannot open some devices belonging to array.\n");
9809 for (i
= 0; i
< new_disks
; i
++) {
9810 if (targets
[i
] < 0) {
9814 if (lseek64(targets
[i
], read_offset
, SEEK_SET
) < 0) {
9815 pr_err("Cannot seek to block: %s\n",
9820 if ((unsigned)read(targets
[i
], buf
, unit_len
) != unit_len
) {
9821 pr_err("Cannot read copy area block: %s\n",
9826 if (lseek64(targets
[i
], write_offset
, SEEK_SET
) < 0) {
9827 pr_err("Cannot seek to block: %s\n",
9832 if ((unsigned)write(targets
[i
], buf
, unit_len
) != unit_len
) {
9833 pr_err("Cannot restore block: %s\n",
9840 if (skipped_disks
> imsm_get_allowed_degradation(info
->new_level
,
9844 pr_err("Cannot restore data from backup. Too many failed disks\n");
9848 if (save_checkpoint_imsm(st
, info
, UNIT_SRC_NORMAL
)) {
9849 /* ignore error == 2, this can mean end of reshape here
9851 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n");
9857 for (i
= 0; i
< new_disks
; i
++)
9866 static char disk_by_path
[] = "/dev/disk/by-path/";
9868 static const char *imsm_get_disk_controller_domain(const char *path
)
9870 char disk_path
[PATH_MAX
];
9874 strcpy(disk_path
, disk_by_path
);
9875 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
9876 if (stat(disk_path
, &st
) == 0) {
9877 struct sys_dev
* hba
;
9880 path
= devt_to_devpath(st
.st_rdev
);
9883 hba
= find_disk_attached_hba(-1, path
);
9884 if (hba
&& hba
->type
== SYS_DEV_SAS
)
9886 else if (hba
&& hba
->type
== SYS_DEV_SATA
)
9890 dprintf("path: %s hba: %s attached: %s\n",
9891 path
, (hba
) ? hba
->path
: "NULL", drv
);
9897 static char *imsm_find_array_devnm_by_subdev(int subdev
, char *container
)
9899 static char devnm
[32];
9900 char subdev_name
[20];
9901 struct mdstat_ent
*mdstat
;
9903 sprintf(subdev_name
, "%d", subdev
);
9904 mdstat
= mdstat_by_subdev(subdev_name
, container
);
9908 strcpy(devnm
, mdstat
->devnm
);
9909 free_mdstat(mdstat
);
9913 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
9914 struct geo_params
*geo
,
9915 int *old_raid_disks
,
9918 /* currently we only support increasing the number of devices
9919 * for a container. This increases the number of device for each
9920 * member array. They must all be RAID0 or RAID5.
9923 struct mdinfo
*info
, *member
;
9924 int devices_that_can_grow
= 0;
9926 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st
->devnm
);
9928 if (geo
->size
> 0 ||
9929 geo
->level
!= UnSet
||
9930 geo
->layout
!= UnSet
||
9931 geo
->chunksize
!= 0 ||
9932 geo
->raid_disks
== UnSet
) {
9933 dprintf("imsm: Container operation is allowed for raid disks number change only.\n");
9937 if (direction
== ROLLBACK_METADATA_CHANGES
) {
9938 dprintf("imsm: Metadata changes rollback is not supported for container operation.\n");
9942 info
= container_content_imsm(st
, NULL
);
9943 for (member
= info
; member
; member
= member
->next
) {
9946 dprintf("imsm: checking device_num: %i\n",
9947 member
->container_member
);
9949 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
9950 /* we work on container for Online Capacity Expansion
9951 * only so raid_disks has to grow
9953 dprintf("imsm: for container operation raid disks increase is required\n");
9957 if (info
->array
.level
!= 0 && info
->array
.level
!= 5) {
9958 /* we cannot use this container with other raid level
9960 dprintf("imsm: for container operation wrong raid level (%i) detected\n",
9964 /* check for platform support
9965 * for this raid level configuration
9967 struct intel_super
*super
= st
->sb
;
9968 if (!is_raid_level_supported(super
->orom
,
9969 member
->array
.level
,
9971 dprintf("platform does not support raid%d with %d disk%s\n",
9974 geo
->raid_disks
> 1 ? "s" : "");
9977 /* check if component size is aligned to chunk size
9979 if (info
->component_size
%
9980 (info
->array
.chunk_size
/512)) {
9981 dprintf("Component size is not aligned to chunk size\n");
9986 if (*old_raid_disks
&&
9987 info
->array
.raid_disks
!= *old_raid_disks
)
9989 *old_raid_disks
= info
->array
.raid_disks
;
9991 /* All raid5 and raid0 volumes in container
9992 * have to be ready for Online Capacity Expansion
9993 * so they need to be assembled. We have already
9994 * checked that no recovery etc is happening.
9996 result
= imsm_find_array_devnm_by_subdev(member
->container_member
,
9997 st
->container_devnm
);
9998 if (result
== NULL
) {
9999 dprintf("imsm: cannot find array\n");
10002 devices_that_can_grow
++;
10005 if (!member
&& devices_that_can_grow
)
10009 dprintf("Container operation allowed\n");
10011 dprintf("Error: %i\n", ret_val
);
10016 /* Function: get_spares_for_grow
10017 * Description: Allocates memory and creates list of spare devices
10018 * avaliable in container. Checks if spare drive size is acceptable.
10019 * Parameters: Pointer to the supertype structure
10020 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
10023 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
10025 unsigned long long min_size
= min_acceptable_spare_size_imsm(st
);
10026 return container_choose_spares(st
, min_size
, NULL
, NULL
, NULL
, 0);
10029 /******************************************************************************
10030 * function: imsm_create_metadata_update_for_reshape
10031 * Function creates update for whole IMSM container.
10033 ******************************************************************************/
10034 static int imsm_create_metadata_update_for_reshape(
10035 struct supertype
*st
,
10036 struct geo_params
*geo
,
10037 int old_raid_disks
,
10038 struct imsm_update_reshape
**updatep
)
10040 struct intel_super
*super
= st
->sb
;
10041 struct imsm_super
*mpb
= super
->anchor
;
10042 int update_memory_size
;
10043 struct imsm_update_reshape
*u
;
10044 struct mdinfo
*spares
;
10047 struct mdinfo
*dev
;
10049 dprintf("(enter) raid_disks = %i\n", geo
->raid_disks
);
10051 delta_disks
= geo
->raid_disks
- old_raid_disks
;
10053 /* size of all update data without anchor */
10054 update_memory_size
= sizeof(struct imsm_update_reshape
);
10056 /* now add space for spare disks that we need to add. */
10057 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
10059 u
= xcalloc(1, update_memory_size
);
10060 u
->type
= update_reshape_container_disks
;
10061 u
->old_raid_disks
= old_raid_disks
;
10062 u
->new_raid_disks
= geo
->raid_disks
;
10064 /* now get spare disks list
10066 spares
= get_spares_for_grow(st
);
10069 || delta_disks
> spares
->array
.spare_disks
) {
10070 pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo
->dev_name
);
10075 /* we have got spares
10076 * update disk list in imsm_disk list table in anchor
10078 dprintf("imsm: %i spares are available.\n\n",
10079 spares
->array
.spare_disks
);
10081 dev
= spares
->devs
;
10082 for (i
= 0; i
< delta_disks
; i
++) {
10087 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
10089 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
10090 dl
->index
= mpb
->num_disks
;
10098 sysfs_free(spares
);
10100 dprintf("imsm: reshape update preparation :");
10101 if (i
== delta_disks
) {
10102 dprintf_cont(" OK\n");
10104 return update_memory_size
;
10107 dprintf_cont(" Error\n");
10112 /******************************************************************************
10113 * function: imsm_create_metadata_update_for_size_change()
10114 * Creates update for IMSM array for array size change.
10116 ******************************************************************************/
10117 static int imsm_create_metadata_update_for_size_change(
10118 struct supertype
*st
,
10119 struct geo_params
*geo
,
10120 struct imsm_update_size_change
**updatep
)
10122 struct intel_super
*super
= st
->sb
;
10123 int update_memory_size
;
10124 struct imsm_update_size_change
*u
;
10126 dprintf("(enter) New size = %llu\n", geo
->size
);
10128 /* size of all update data without anchor */
10129 update_memory_size
= sizeof(struct imsm_update_size_change
);
10131 u
= xcalloc(1, update_memory_size
);
10132 u
->type
= update_size_change
;
10133 u
->subdev
= super
->current_vol
;
10134 u
->new_size
= geo
->size
;
10136 dprintf("imsm: reshape update preparation : OK\n");
10139 return update_memory_size
;
10142 /******************************************************************************
10143 * function: imsm_create_metadata_update_for_migration()
10144 * Creates update for IMSM array.
10146 ******************************************************************************/
10147 static int imsm_create_metadata_update_for_migration(
10148 struct supertype
*st
,
10149 struct geo_params
*geo
,
10150 struct imsm_update_reshape_migration
**updatep
)
10152 struct intel_super
*super
= st
->sb
;
10153 int update_memory_size
;
10154 struct imsm_update_reshape_migration
*u
;
10155 struct imsm_dev
*dev
;
10156 int previous_level
= -1;
10158 dprintf("(enter) New Level = %i\n", geo
->level
);
10160 /* size of all update data without anchor */
10161 update_memory_size
= sizeof(struct imsm_update_reshape_migration
);
10163 u
= xcalloc(1, update_memory_size
);
10164 u
->type
= update_reshape_migration
;
10165 u
->subdev
= super
->current_vol
;
10166 u
->new_level
= geo
->level
;
10167 u
->new_layout
= geo
->layout
;
10168 u
->new_raid_disks
= u
->old_raid_disks
= geo
->raid_disks
;
10169 u
->new_disks
[0] = -1;
10170 u
->new_chunksize
= -1;
10172 dev
= get_imsm_dev(super
, u
->subdev
);
10174 struct imsm_map
*map
;
10176 map
= get_imsm_map(dev
, MAP_0
);
10178 int current_chunk_size
=
10179 __le16_to_cpu(map
->blocks_per_strip
) / 2;
10181 if (geo
->chunksize
!= current_chunk_size
) {
10182 u
->new_chunksize
= geo
->chunksize
/ 1024;
10183 dprintf("imsm: chunk size change from %i to %i\n",
10184 current_chunk_size
, u
->new_chunksize
);
10186 previous_level
= map
->raid_level
;
10189 if (geo
->level
== 5 && previous_level
== 0) {
10190 struct mdinfo
*spares
= NULL
;
10192 u
->new_raid_disks
++;
10193 spares
= get_spares_for_grow(st
);
10194 if (spares
== NULL
|| spares
->array
.spare_disks
< 1) {
10196 sysfs_free(spares
);
10197 update_memory_size
= 0;
10198 dprintf("error: cannot get spare device for requested migration");
10201 sysfs_free(spares
);
10203 dprintf("imsm: reshape update preparation : OK\n");
10206 return update_memory_size
;
10209 static void imsm_update_metadata_locally(struct supertype
*st
,
10210 void *buf
, int len
)
10212 struct metadata_update mu
;
10217 mu
.space_list
= NULL
;
10219 if (imsm_prepare_update(st
, &mu
))
10220 imsm_process_update(st
, &mu
);
10222 while (mu
.space_list
) {
10223 void **space
= mu
.space_list
;
10224 mu
.space_list
= *space
;
10229 /***************************************************************************
10230 * Function: imsm_analyze_change
10231 * Description: Function analyze change for single volume
10232 * and validate if transition is supported
10233 * Parameters: Geometry parameters, supertype structure,
10234 * metadata change direction (apply/rollback)
10235 * Returns: Operation type code on success, -1 if fail
10236 ****************************************************************************/
10237 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
10238 struct geo_params
*geo
,
10241 struct mdinfo info
;
10243 int check_devs
= 0;
10245 /* number of added/removed disks in operation result */
10246 int devNumChange
= 0;
10247 /* imsm compatible layout value for array geometry verification */
10248 int imsm_layout
= -1;
10250 struct imsm_dev
*dev
;
10251 struct intel_super
*super
;
10252 unsigned long long current_size
;
10253 unsigned long long free_size
;
10254 unsigned long long max_size
;
10257 getinfo_super_imsm_volume(st
, &info
, NULL
);
10258 if (geo
->level
!= info
.array
.level
&& geo
->level
>= 0 &&
10259 geo
->level
!= UnSet
) {
10260 switch (info
.array
.level
) {
10262 if (geo
->level
== 5) {
10263 change
= CH_MIGRATION
;
10264 if (geo
->layout
!= ALGORITHM_LEFT_ASYMMETRIC
) {
10265 pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n");
10267 goto analyse_change_exit
;
10269 imsm_layout
= geo
->layout
;
10271 devNumChange
= 1; /* parity disk added */
10272 } else if (geo
->level
== 10) {
10273 change
= CH_TAKEOVER
;
10275 devNumChange
= 2; /* two mirrors added */
10276 imsm_layout
= 0x102; /* imsm supported layout */
10281 if (geo
->level
== 0) {
10282 change
= CH_TAKEOVER
;
10284 devNumChange
= -(geo
->raid_disks
/2);
10285 imsm_layout
= 0; /* imsm raid0 layout */
10289 if (change
== -1) {
10290 pr_err("Error. Level Migration from %d to %d not supported!\n",
10291 info
.array
.level
, geo
->level
);
10292 goto analyse_change_exit
;
10295 geo
->level
= info
.array
.level
;
10297 if (geo
->layout
!= info
.array
.layout
&&
10298 (geo
->layout
!= UnSet
&& geo
->layout
!= -1)) {
10299 change
= CH_MIGRATION
;
10300 if (info
.array
.layout
== 0 && info
.array
.level
== 5 &&
10301 geo
->layout
== 5) {
10302 /* reshape 5 -> 4 */
10303 } else if (info
.array
.layout
== 5 && info
.array
.level
== 5 &&
10304 geo
->layout
== 0) {
10305 /* reshape 4 -> 5 */
10309 pr_err("Error. Layout Migration from %d to %d not supported!\n",
10310 info
.array
.layout
, geo
->layout
);
10312 goto analyse_change_exit
;
10315 geo
->layout
= info
.array
.layout
;
10316 if (imsm_layout
== -1)
10317 imsm_layout
= info
.array
.layout
;
10320 if (geo
->chunksize
> 0 && geo
->chunksize
!= UnSet
&&
10321 geo
->chunksize
!= info
.array
.chunk_size
) {
10322 if (info
.array
.level
== 10) {
10323 pr_err("Error. Chunk size change for RAID 10 is not supported.\n");
10325 goto analyse_change_exit
;
10327 change
= CH_MIGRATION
;
10329 geo
->chunksize
= info
.array
.chunk_size
;
10332 chunk
= geo
->chunksize
/ 1024;
10335 dev
= get_imsm_dev(super
, super
->current_vol
);
10336 data_disks
= imsm_num_data_members(dev
, MAP_0
);
10337 /* compute current size per disk member
10339 current_size
= info
.custom_array_size
/ data_disks
;
10341 if (geo
->size
> 0 && geo
->size
!= MAX_SIZE
) {
10342 /* align component size
10344 geo
->size
= imsm_component_size_aligment_check(
10345 get_imsm_raid_level(dev
->vol
.map
),
10346 chunk
* 1024, super
->sector_size
,
10348 if (geo
->size
== 0) {
10349 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is 0).\n",
10351 goto analyse_change_exit
;
10355 if (current_size
!= geo
->size
&& geo
->size
> 0) {
10356 if (change
!= -1) {
10357 pr_err("Error. Size change should be the only one at a time.\n");
10359 goto analyse_change_exit
;
10361 if ((super
->current_vol
+ 1) != super
->anchor
->num_raid_devs
) {
10362 pr_err("Error. The last volume in container can be expanded only (%i/%s).\n",
10363 super
->current_vol
, st
->devnm
);
10364 goto analyse_change_exit
;
10366 /* check the maximum available size
10368 rv
= imsm_get_free_size(st
, dev
->vol
.map
->num_members
,
10369 0, chunk
, &free_size
);
10371 /* Cannot find maximum available space
10375 max_size
= free_size
+ current_size
;
10376 /* align component size
10378 max_size
= imsm_component_size_aligment_check(
10379 get_imsm_raid_level(dev
->vol
.map
),
10380 chunk
* 1024, super
->sector_size
,
10383 if (geo
->size
== MAX_SIZE
) {
10384 /* requested size change to the maximum available size
10386 if (max_size
== 0) {
10387 pr_err("Error. Cannot find maximum available space.\n");
10389 goto analyse_change_exit
;
10391 geo
->size
= max_size
;
10394 if (direction
== ROLLBACK_METADATA_CHANGES
) {
10395 /* accept size for rollback only
10398 /* round size due to metadata compatibility
10400 geo
->size
= (geo
->size
>> SECT_PER_MB_SHIFT
)
10401 << SECT_PER_MB_SHIFT
;
10402 dprintf("Prepare update for size change to %llu\n",
10404 if (current_size
>= geo
->size
) {
10405 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is %llu).\n",
10406 current_size
, geo
->size
);
10407 goto analyse_change_exit
;
10409 if (max_size
&& geo
->size
> max_size
) {
10410 pr_err("Error. Requested size is larger than maximum available size (maximum available size is %llu, requested size /rounded/ is %llu).\n",
10411 max_size
, geo
->size
);
10412 goto analyse_change_exit
;
10415 geo
->size
*= data_disks
;
10416 geo
->raid_disks
= dev
->vol
.map
->num_members
;
10417 change
= CH_ARRAY_SIZE
;
10419 if (!validate_geometry_imsm(st
,
10422 geo
->raid_disks
+ devNumChange
,
10424 geo
->size
, INVALID_SECTORS
,
10429 struct intel_super
*super
= st
->sb
;
10430 struct imsm_super
*mpb
= super
->anchor
;
10432 if (mpb
->num_raid_devs
> 1) {
10433 pr_err("Error. Cannot perform operation on %s- for this operation it MUST be single array in container\n",
10439 analyse_change_exit
:
10440 if (direction
== ROLLBACK_METADATA_CHANGES
&&
10441 (change
== CH_MIGRATION
|| change
== CH_TAKEOVER
)) {
10442 dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n");
10448 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
10450 struct intel_super
*super
= st
->sb
;
10451 struct imsm_update_takeover
*u
;
10453 u
= xmalloc(sizeof(struct imsm_update_takeover
));
10455 u
->type
= update_takeover
;
10456 u
->subarray
= super
->current_vol
;
10458 /* 10->0 transition */
10459 if (geo
->level
== 0)
10460 u
->direction
= R10_TO_R0
;
10462 /* 0->10 transition */
10463 if (geo
->level
== 10)
10464 u
->direction
= R0_TO_R10
;
10466 /* update metadata locally */
10467 imsm_update_metadata_locally(st
, u
,
10468 sizeof(struct imsm_update_takeover
));
10469 /* and possibly remotely */
10470 if (st
->update_tail
)
10471 append_metadata_update(st
, u
,
10472 sizeof(struct imsm_update_takeover
));
10479 static int imsm_reshape_super(struct supertype
*st
, unsigned long long size
,
10481 int layout
, int chunksize
, int raid_disks
,
10482 int delta_disks
, char *backup
, char *dev
,
10483 int direction
, int verbose
)
10486 struct geo_params geo
;
10488 dprintf("(enter)\n");
10490 memset(&geo
, 0, sizeof(struct geo_params
));
10492 geo
.dev_name
= dev
;
10493 strcpy(geo
.devnm
, st
->devnm
);
10496 geo
.layout
= layout
;
10497 geo
.chunksize
= chunksize
;
10498 geo
.raid_disks
= raid_disks
;
10499 if (delta_disks
!= UnSet
)
10500 geo
.raid_disks
+= delta_disks
;
10502 dprintf("for level : %i\n", geo
.level
);
10503 dprintf("for raid_disks : %i\n", geo
.raid_disks
);
10505 if (experimental() == 0)
10508 if (strcmp(st
->container_devnm
, st
->devnm
) == 0) {
10509 /* On container level we can only increase number of devices. */
10510 dprintf("imsm: info: Container operation\n");
10511 int old_raid_disks
= 0;
10513 if (imsm_reshape_is_allowed_on_container(
10514 st
, &geo
, &old_raid_disks
, direction
)) {
10515 struct imsm_update_reshape
*u
= NULL
;
10518 len
= imsm_create_metadata_update_for_reshape(
10519 st
, &geo
, old_raid_disks
, &u
);
10522 dprintf("imsm: Cannot prepare update\n");
10523 goto exit_imsm_reshape_super
;
10527 /* update metadata locally */
10528 imsm_update_metadata_locally(st
, u
, len
);
10529 /* and possibly remotely */
10530 if (st
->update_tail
)
10531 append_metadata_update(st
, u
, len
);
10536 pr_err("(imsm) Operation is not allowed on this container\n");
10539 /* On volume level we support following operations
10540 * - takeover: raid10 -> raid0; raid0 -> raid10
10541 * - chunk size migration
10542 * - migration: raid5 -> raid0; raid0 -> raid5
10544 struct intel_super
*super
= st
->sb
;
10545 struct intel_dev
*dev
= super
->devlist
;
10547 dprintf("imsm: info: Volume operation\n");
10548 /* find requested device */
10551 imsm_find_array_devnm_by_subdev(
10552 dev
->index
, st
->container_devnm
);
10553 if (devnm
&& strcmp(devnm
, geo
.devnm
) == 0)
10558 pr_err("Cannot find %s (%s) subarray\n",
10559 geo
.dev_name
, geo
.devnm
);
10560 goto exit_imsm_reshape_super
;
10562 super
->current_vol
= dev
->index
;
10563 change
= imsm_analyze_change(st
, &geo
, direction
);
10566 ret_val
= imsm_takeover(st
, &geo
);
10568 case CH_MIGRATION
: {
10569 struct imsm_update_reshape_migration
*u
= NULL
;
10571 imsm_create_metadata_update_for_migration(
10574 dprintf("imsm: Cannot prepare update\n");
10578 /* update metadata locally */
10579 imsm_update_metadata_locally(st
, u
, len
);
10580 /* and possibly remotely */
10581 if (st
->update_tail
)
10582 append_metadata_update(st
, u
, len
);
10587 case CH_ARRAY_SIZE
: {
10588 struct imsm_update_size_change
*u
= NULL
;
10590 imsm_create_metadata_update_for_size_change(
10593 dprintf("imsm: Cannot prepare update\n");
10597 /* update metadata locally */
10598 imsm_update_metadata_locally(st
, u
, len
);
10599 /* and possibly remotely */
10600 if (st
->update_tail
)
10601 append_metadata_update(st
, u
, len
);
10611 exit_imsm_reshape_super
:
10612 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
10616 #define COMPLETED_OK 0
10617 #define COMPLETED_NONE 1
10618 #define COMPLETED_DELAYED 2
10620 static int read_completed(int fd
, unsigned long long *val
)
10625 ret
= sysfs_fd_get_str(fd
, buf
, 50);
10629 ret
= COMPLETED_OK
;
10630 if (strncmp(buf
, "none", 4) == 0) {
10631 ret
= COMPLETED_NONE
;
10632 } else if (strncmp(buf
, "delayed", 7) == 0) {
10633 ret
= COMPLETED_DELAYED
;
10636 *val
= strtoull(buf
, &ep
, 0);
10637 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))
10643 /*******************************************************************************
10644 * Function: wait_for_reshape_imsm
10645 * Description: Function writes new sync_max value and waits until
10646 * reshape process reach new position
10648 * sra : general array info
10649 * ndata : number of disks in new array's layout
10652 * 1 : there is no reshape in progress,
10654 ******************************************************************************/
10655 int wait_for_reshape_imsm(struct mdinfo
*sra
, int ndata
)
10657 int fd
= sysfs_get_fd(sra
, NULL
, "sync_completed");
10659 unsigned long long completed
;
10660 /* to_complete : new sync_max position */
10661 unsigned long long to_complete
= sra
->reshape_progress
;
10662 unsigned long long position_to_set
= to_complete
/ ndata
;
10665 dprintf("cannot open reshape_position\n");
10670 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
10672 dprintf("cannot read reshape_position (no reshape in progres)\n");
10681 if (completed
> position_to_set
) {
10682 dprintf("wrong next position to set %llu (%llu)\n",
10683 to_complete
, position_to_set
);
10687 dprintf("Position set: %llu\n", position_to_set
);
10688 if (sysfs_set_num(sra
, NULL
, "sync_max",
10689 position_to_set
) != 0) {
10690 dprintf("cannot set reshape position to %llu\n",
10699 int timeout
= 3000;
10701 sysfs_wait(fd
, &timeout
);
10702 if (sysfs_get_str(sra
, NULL
, "sync_action",
10704 strncmp(action
, "reshape", 7) != 0) {
10705 if (strncmp(action
, "idle", 4) == 0)
10711 rc
= read_completed(fd
, &completed
);
10713 dprintf("cannot read reshape_position (in loop)\n");
10716 } else if (rc
== COMPLETED_NONE
)
10718 } while (completed
< position_to_set
);
10724 /*******************************************************************************
10725 * Function: check_degradation_change
10726 * Description: Check that array hasn't become failed.
10728 * info : for sysfs access
10729 * sources : source disks descriptors
10730 * degraded: previous degradation level
10732 * degradation level
10733 ******************************************************************************/
10734 int check_degradation_change(struct mdinfo
*info
,
10738 unsigned long long new_degraded
;
10741 rv
= sysfs_get_ll(info
, NULL
, "degraded", &new_degraded
);
10742 if (rv
== -1 || (new_degraded
!= (unsigned long long)degraded
)) {
10743 /* check each device to ensure it is still working */
10746 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
10747 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
10749 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
10752 if (sysfs_get_str(info
,
10753 sd
, "state", sbuf
, sizeof(sbuf
)) < 0 ||
10754 strstr(sbuf
, "faulty") ||
10755 strstr(sbuf
, "in_sync") == NULL
) {
10756 /* this device is dead */
10757 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
10758 if (sd
->disk
.raid_disk
>= 0 &&
10759 sources
[sd
->disk
.raid_disk
] >= 0) {
10761 sd
->disk
.raid_disk
]);
10762 sources
[sd
->disk
.raid_disk
] =
10771 return new_degraded
;
10774 /*******************************************************************************
10775 * Function: imsm_manage_reshape
10776 * Description: Function finds array under reshape and it manages reshape
10777 * process. It creates stripes backups (if required) and sets
10780 * afd : Backup handle (nattive) - not used
10781 * sra : general array info
10782 * reshape : reshape parameters - not used
10783 * st : supertype structure
10784 * blocks : size of critical section [blocks]
10785 * fds : table of source device descriptor
10786 * offsets : start of array (offest per devices)
10788 * destfd : table of destination device descriptor
10789 * destoffsets : table of destination offsets (per device)
10791 * 1 : success, reshape is done
10793 ******************************************************************************/
10794 static int imsm_manage_reshape(
10795 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
10796 struct supertype
*st
, unsigned long backup_blocks
,
10797 int *fds
, unsigned long long *offsets
,
10798 int dests
, int *destfd
, unsigned long long *destoffsets
)
10801 struct intel_super
*super
= st
->sb
;
10802 struct intel_dev
*dv
;
10803 unsigned int sector_size
= super
->sector_size
;
10804 struct imsm_dev
*dev
= NULL
;
10805 struct imsm_map
*map_src
;
10806 int migr_vol_qan
= 0;
10807 int ndata
, odata
; /* [bytes] */
10808 int chunk
; /* [bytes] */
10809 struct migr_record
*migr_rec
;
10811 unsigned int buf_size
; /* [bytes] */
10812 unsigned long long max_position
; /* array size [bytes] */
10813 unsigned long long next_step
; /* [blocks]/[bytes] */
10814 unsigned long long old_data_stripe_length
;
10815 unsigned long long start_src
; /* [bytes] */
10816 unsigned long long start
; /* [bytes] */
10817 unsigned long long start_buf_shift
; /* [bytes] */
10819 int source_layout
= 0;
10824 if (!fds
|| !offsets
)
10827 /* Find volume during the reshape */
10828 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
10829 if (dv
->dev
->vol
.migr_type
== MIGR_GEN_MIGR
10830 && dv
->dev
->vol
.migr_state
== 1) {
10835 /* Only one volume can migrate at the same time */
10836 if (migr_vol_qan
!= 1) {
10837 pr_err("%s", migr_vol_qan
?
10838 "Number of migrating volumes greater than 1\n" :
10839 "There is no volume during migrationg\n");
10843 map_src
= get_imsm_map(dev
, MAP_1
);
10844 if (map_src
== NULL
)
10847 ndata
= imsm_num_data_members(dev
, MAP_0
);
10848 odata
= imsm_num_data_members(dev
, MAP_1
);
10850 chunk
= __le16_to_cpu(map_src
->blocks_per_strip
) * 512;
10851 old_data_stripe_length
= odata
* chunk
;
10853 migr_rec
= super
->migr_rec
;
10855 /* initialize migration record for start condition */
10856 if (sra
->reshape_progress
== 0)
10857 init_migr_record_imsm(st
, dev
, sra
);
10859 if (__le32_to_cpu(migr_rec
->rec_status
) != UNIT_SRC_NORMAL
) {
10860 dprintf("imsm: cannot restart migration when data are present in copy area.\n");
10863 /* Save checkpoint to update migration record for current
10864 * reshape position (in md). It can be farther than current
10865 * reshape position in metadata.
10867 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
10868 /* ignore error == 2, this can mean end of reshape here
10870 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n");
10875 /* size for data */
10876 buf_size
= __le32_to_cpu(migr_rec
->blocks_per_unit
) * 512;
10877 /* extend buffer size for parity disk */
10878 buf_size
+= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
10879 /* add space for stripe aligment */
10880 buf_size
+= old_data_stripe_length
;
10881 if (posix_memalign((void **)&buf
, MAX_SECTOR_SIZE
, buf_size
)) {
10882 dprintf("imsm: Cannot allocate checkpoint buffer\n");
10886 max_position
= sra
->component_size
* ndata
;
10887 source_layout
= imsm_level_to_layout(map_src
->raid_level
);
10889 while (__le32_to_cpu(migr_rec
->curr_migr_unit
) <
10890 __le32_to_cpu(migr_rec
->num_migr_units
)) {
10891 /* current reshape position [blocks] */
10892 unsigned long long current_position
=
10893 __le32_to_cpu(migr_rec
->blocks_per_unit
)
10894 * __le32_to_cpu(migr_rec
->curr_migr_unit
);
10895 unsigned long long border
;
10897 /* Check that array hasn't become failed.
10899 degraded
= check_degradation_change(sra
, fds
, degraded
);
10900 if (degraded
> 1) {
10901 dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded
);
10905 next_step
= __le32_to_cpu(migr_rec
->blocks_per_unit
);
10907 if ((current_position
+ next_step
) > max_position
)
10908 next_step
= max_position
- current_position
;
10910 start
= current_position
* 512;
10912 /* align reading start to old geometry */
10913 start_buf_shift
= start
% old_data_stripe_length
;
10914 start_src
= start
- start_buf_shift
;
10916 border
= (start_src
/ odata
) - (start
/ ndata
);
10918 if (border
<= __le32_to_cpu(migr_rec
->dest_depth_per_unit
)) {
10919 /* save critical stripes to buf
10920 * start - start address of current unit
10921 * to backup [bytes]
10922 * start_src - start address of current unit
10923 * to backup alligned to source array
10926 unsigned long long next_step_filler
;
10927 unsigned long long copy_length
= next_step
* 512;
10929 /* allign copy area length to stripe in old geometry */
10930 next_step_filler
= ((copy_length
+ start_buf_shift
)
10931 % old_data_stripe_length
);
10932 if (next_step_filler
)
10933 next_step_filler
= (old_data_stripe_length
10934 - next_step_filler
);
10935 dprintf("save_stripes() parameters: start = %llu,\tstart_src = %llu,\tnext_step*512 = %llu,\tstart_in_buf_shift = %llu,\tnext_step_filler = %llu\n",
10936 start
, start_src
, copy_length
,
10937 start_buf_shift
, next_step_filler
);
10939 if (save_stripes(fds
, offsets
, map_src
->num_members
,
10940 chunk
, map_src
->raid_level
,
10941 source_layout
, 0, NULL
, start_src
,
10943 next_step_filler
+ start_buf_shift
,
10945 dprintf("imsm: Cannot save stripes to buffer\n");
10948 /* Convert data to destination format and store it
10949 * in backup general migration area
10951 if (save_backup_imsm(st
, dev
, sra
,
10952 buf
+ start_buf_shift
, copy_length
)) {
10953 dprintf("imsm: Cannot save stripes to target devices\n");
10956 if (save_checkpoint_imsm(st
, sra
,
10957 UNIT_SRC_IN_CP_AREA
)) {
10958 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n");
10962 /* set next step to use whole border area */
10963 border
/= next_step
;
10965 next_step
*= border
;
10967 /* When data backed up, checkpoint stored,
10968 * kick the kernel to reshape unit of data
10970 next_step
= next_step
+ sra
->reshape_progress
;
10971 /* limit next step to array max position */
10972 if (next_step
> max_position
)
10973 next_step
= max_position
;
10974 sysfs_set_num(sra
, NULL
, "suspend_lo", sra
->reshape_progress
);
10975 sysfs_set_num(sra
, NULL
, "suspend_hi", next_step
);
10976 sra
->reshape_progress
= next_step
;
10978 /* wait until reshape finish */
10979 if (wait_for_reshape_imsm(sra
, ndata
)) {
10980 dprintf("wait_for_reshape_imsm returned error!\n");
10986 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
10987 /* ignore error == 2, this can mean end of reshape here
10989 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n");
10995 /* clear migr_rec on disks after successful migration */
10998 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SECTORS
*sector_size
);
10999 for (d
= super
->disks
; d
; d
= d
->next
) {
11000 if (d
->index
< 0 || is_failed(&d
->disk
))
11002 unsigned long long dsize
;
11004 get_dev_size(d
->fd
, NULL
, &dsize
);
11005 if (lseek64(d
->fd
, dsize
- MIGR_REC_SECTOR_POSITION
*sector_size
,
11007 if (write(d
->fd
, super
->migr_rec_buf
,
11008 MIGR_REC_BUF_SECTORS
*sector_size
) !=
11009 MIGR_REC_BUF_SECTORS
*sector_size
)
11010 perror("Write migr_rec failed");
11014 /* return '1' if done */
11018 /* See Grow.c: abort_reshape() for further explanation */
11019 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
11020 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
11021 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
11026 #endif /* MDASSEMBLE */
11028 struct superswitch super_imsm
= {
11030 .examine_super
= examine_super_imsm
,
11031 .brief_examine_super
= brief_examine_super_imsm
,
11032 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
11033 .export_examine_super
= export_examine_super_imsm
,
11034 .detail_super
= detail_super_imsm
,
11035 .brief_detail_super
= brief_detail_super_imsm
,
11036 .write_init_super
= write_init_super_imsm
,
11037 .validate_geometry
= validate_geometry_imsm
,
11038 .add_to_super
= add_to_super_imsm
,
11039 .remove_from_super
= remove_from_super_imsm
,
11040 .detail_platform
= detail_platform_imsm
,
11041 .export_detail_platform
= export_detail_platform_imsm
,
11042 .kill_subarray
= kill_subarray_imsm
,
11043 .update_subarray
= update_subarray_imsm
,
11044 .load_container
= load_container_imsm
,
11045 .default_geometry
= default_geometry_imsm
,
11046 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
11047 .reshape_super
= imsm_reshape_super
,
11048 .manage_reshape
= imsm_manage_reshape
,
11049 .recover_backup
= recover_backup_imsm
,
11050 .copy_metadata
= copy_metadata_imsm
,
11052 .match_home
= match_home_imsm
,
11053 .uuid_from_super
= uuid_from_super_imsm
,
11054 .getinfo_super
= getinfo_super_imsm
,
11055 .getinfo_super_disks
= getinfo_super_disks_imsm
,
11056 .update_super
= update_super_imsm
,
11058 .avail_size
= avail_size_imsm
,
11059 .min_acceptable_spare_size
= min_acceptable_spare_size_imsm
,
11061 .compare_super
= compare_super_imsm
,
11063 .load_super
= load_super_imsm
,
11064 .init_super
= init_super_imsm
,
11065 .store_super
= store_super_imsm
,
11066 .free_super
= free_super_imsm
,
11067 .match_metadata_desc
= match_metadata_desc_imsm
,
11068 .container_content
= container_content_imsm
,
11069 .validate_container
= validate_container_imsm
,
11076 .open_new
= imsm_open_new
,
11077 .set_array_state
= imsm_set_array_state
,
11078 .set_disk
= imsm_set_disk
,
11079 .sync_metadata
= imsm_sync_metadata
,
11080 .activate_spare
= imsm_activate_spare
,
11081 .process_update
= imsm_process_update
,
11082 .prepare_update
= imsm_prepare_update
,
11083 #endif /* MDASSEMBLE */