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
);
914 static unsigned long long num_data_stripes(struct imsm_map
*map
)
918 return join_u32(map
->num_data_stripes_lo
, map
->num_data_stripes_hi
);
922 static void set_total_blocks(struct imsm_disk
*disk
, unsigned long long n
)
924 split_ull(n
, &disk
->total_blocks_lo
, &disk
->total_blocks_hi
);
927 static void set_pba_of_lba0(struct imsm_map
*map
, unsigned long long n
)
929 split_ull(n
, &map
->pba_of_lba0_lo
, &map
->pba_of_lba0_hi
);
932 static void set_blocks_per_member(struct imsm_map
*map
, unsigned long long n
)
934 split_ull(n
, &map
->blocks_per_member_lo
, &map
->blocks_per_member_hi
);
937 static void set_num_data_stripes(struct imsm_map
*map
, unsigned long long n
)
939 split_ull(n
, &map
->num_data_stripes_lo
, &map
->num_data_stripes_hi
);
942 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
)
944 /* find a list of used extents on the given physical device */
945 struct extent
*rv
, *e
;
947 int memberships
= count_memberships(dl
, super
);
950 /* trim the reserved area for spares, so they can join any array
951 * regardless of whether the OROM has assigned sectors from the
952 * IMSM_RESERVED_SECTORS region
955 reservation
= imsm_min_reserved_sectors(super
);
957 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
959 rv
= xcalloc(sizeof(struct extent
), (memberships
+ 1));
962 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
963 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
964 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
966 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
967 e
->start
= pba_of_lba0(map
);
968 e
->size
= blocks_per_member(map
);
972 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
974 /* determine the start of the metadata
975 * when no raid devices are defined use the default
976 * ...otherwise allow the metadata to truncate the value
977 * as is the case with older versions of imsm
980 struct extent
*last
= &rv
[memberships
- 1];
981 unsigned long long remainder
;
983 remainder
= total_blocks(&dl
->disk
) - (last
->start
+ last
->size
);
984 /* round down to 1k block to satisfy precision of the kernel
988 /* make sure remainder is still sane */
989 if (remainder
< (unsigned)ROUND_UP(super
->len
, 512) >> 9)
990 remainder
= ROUND_UP(super
->len
, 512) >> 9;
991 if (reservation
> remainder
)
992 reservation
= remainder
;
994 e
->start
= total_blocks(&dl
->disk
) - reservation
;
999 /* try to determine how much space is reserved for metadata from
1000 * the last get_extents() entry, otherwise fallback to the
1003 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
1009 /* for spares just return a minimal reservation which will grow
1010 * once the spare is picked up by an array
1012 if (dl
->index
== -1)
1013 return MPB_SECTOR_CNT
;
1015 e
= get_extents(super
, dl
);
1017 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1019 /* scroll to last entry */
1020 for (i
= 0; e
[i
].size
; i
++)
1023 rv
= total_blocks(&dl
->disk
) - e
[i
].start
;
1030 static int is_spare(struct imsm_disk
*disk
)
1032 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
1035 static int is_configured(struct imsm_disk
*disk
)
1037 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
1040 static int is_failed(struct imsm_disk
*disk
)
1042 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
1045 /* try to determine how much space is reserved for metadata from
1046 * the last get_extents() entry on the smallest active disk,
1047 * otherwise fallback to the default
1049 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
)
1053 unsigned long long min_active
;
1055 __u32 rv
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1056 struct dl
*dl
, *dl_min
= NULL
;
1062 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
1065 unsigned long long blocks
= total_blocks(&dl
->disk
);
1066 if (blocks
< min_active
|| min_active
== 0) {
1068 min_active
= blocks
;
1074 /* find last lba used by subarrays on the smallest active disk */
1075 e
= get_extents(super
, dl_min
);
1078 for (i
= 0; e
[i
].size
; i
++)
1081 remainder
= min_active
- e
[i
].start
;
1084 /* to give priority to recovery we should not require full
1085 IMSM_RESERVED_SECTORS from the spare */
1086 rv
= MPB_SECTOR_CNT
+ NUM_BLOCKS_DIRTY_STRIPE_REGION
;
1088 /* if real reservation is smaller use that value */
1089 return (remainder
< rv
) ? remainder
: rv
;
1092 /* Return minimum size of a spare that can be used in this array*/
1093 static unsigned long long min_acceptable_spare_size_imsm(struct supertype
*st
)
1095 struct intel_super
*super
= st
->sb
;
1099 unsigned long long rv
= 0;
1103 /* find first active disk in array */
1105 while (dl
&& (is_failed(&dl
->disk
) || dl
->index
== -1))
1109 /* find last lba used by subarrays */
1110 e
= get_extents(super
, dl
);
1113 for (i
= 0; e
[i
].size
; i
++)
1116 rv
= e
[i
-1].start
+ e
[i
-1].size
;
1119 /* add the amount of space needed for metadata */
1120 rv
= rv
+ imsm_min_reserved_sectors(super
);
1125 static int is_gen_migration(struct imsm_dev
*dev
);
1127 #define IMSM_4K_DIV 8
1130 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
1131 struct imsm_dev
*dev
);
1133 static void print_imsm_dev(struct intel_super
*super
,
1134 struct imsm_dev
*dev
,
1140 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1141 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
1145 printf("[%.16s]:\n", dev
->volume
);
1146 printf(" UUID : %s\n", uuid
);
1147 printf(" RAID Level : %d", get_imsm_raid_level(map
));
1149 printf(" <-- %d", get_imsm_raid_level(map2
));
1151 printf(" Members : %d", map
->num_members
);
1153 printf(" <-- %d", map2
->num_members
);
1155 printf(" Slots : [");
1156 for (i
= 0; i
< map
->num_members
; i
++) {
1157 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
1158 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1163 for (i
= 0; i
< map2
->num_members
; i
++) {
1164 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_1
);
1165 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1170 printf(" Failed disk : ");
1171 if (map
->failed_disk_num
== 0xff)
1174 printf("%i", map
->failed_disk_num
);
1176 slot
= get_imsm_disk_slot(map
, disk_idx
);
1178 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
1179 printf(" This Slot : %d%s\n", slot
,
1180 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
1182 printf(" This Slot : ?\n");
1183 sz
= __le32_to_cpu(dev
->size_high
);
1185 sz
+= __le32_to_cpu(dev
->size_low
);
1186 printf(" Array Size : %llu%s\n", (unsigned long long)sz
,
1187 human_size(sz
* 512));
1188 sz
= blocks_per_member(map
);
1189 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz
,
1190 human_size(sz
* 512));
1191 printf(" Sector Offset : %llu\n",
1193 printf(" Num Stripes : %llu\n",
1194 num_data_stripes(map
));
1195 printf(" Chunk Size : %u KiB",
1196 __le16_to_cpu(map
->blocks_per_strip
) / 2);
1198 printf(" <-- %u KiB",
1199 __le16_to_cpu(map2
->blocks_per_strip
) / 2);
1201 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
1202 printf(" Migrate State : ");
1203 if (dev
->vol
.migr_state
) {
1204 if (migr_type(dev
) == MIGR_INIT
)
1205 printf("initialize\n");
1206 else if (migr_type(dev
) == MIGR_REBUILD
)
1207 printf("rebuild\n");
1208 else if (migr_type(dev
) == MIGR_VERIFY
)
1210 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
1211 printf("general migration\n");
1212 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
1213 printf("state change\n");
1214 else if (migr_type(dev
) == MIGR_REPAIR
)
1217 printf("<unknown:%d>\n", migr_type(dev
));
1220 printf(" Map State : %s", map_state_str
[map
->map_state
]);
1221 if (dev
->vol
.migr_state
) {
1222 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1224 printf(" <-- %s", map_state_str
[map
->map_state
]);
1225 printf("\n Checkpoint : %u ",
1226 __le32_to_cpu(dev
->vol
.curr_migr_unit
));
1227 if (is_gen_migration(dev
) && (slot
> 1 || slot
< 0))
1230 printf("(%llu)", (unsigned long long)
1231 blocks_per_migr_unit(super
, dev
));
1234 printf(" Dirty State : %s\n", dev
->vol
.dirty
? "dirty" : "clean");
1237 static void print_imsm_disk(struct imsm_disk
*disk
, int index
, __u32 reserved
)
1239 char str
[MAX_RAID_SERIAL_LEN
+ 1];
1242 if (index
< -1 || !disk
)
1246 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
1248 printf(" Disk%02d Serial : %s\n", index
, str
);
1250 printf(" Disk Serial : %s\n", str
);
1251 printf(" State :%s%s%s\n", is_spare(disk
) ? " spare" : "",
1252 is_configured(disk
) ? " active" : "",
1253 is_failed(disk
) ? " failed" : "");
1254 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
1255 sz
= total_blocks(disk
) - reserved
;
1256 printf(" Usable Size : %llu%s\n", (unsigned long long)sz
,
1257 human_size(sz
* 512));
1260 void convert_to_4k_imsm_migr_rec(struct intel_super
*super
)
1262 struct migr_record
*migr_rec
= super
->migr_rec
;
1264 migr_rec
->blocks_per_unit
/= IMSM_4K_DIV
;
1265 migr_rec
->ckpt_area_pba
/= IMSM_4K_DIV
;
1266 migr_rec
->dest_1st_member_lba
/= IMSM_4K_DIV
;
1267 migr_rec
->dest_depth_per_unit
/= IMSM_4K_DIV
;
1268 split_ull((join_u32(migr_rec
->post_migr_vol_cap
,
1269 migr_rec
->post_migr_vol_cap_hi
) / IMSM_4K_DIV
),
1270 &migr_rec
->post_migr_vol_cap
, &migr_rec
->post_migr_vol_cap_hi
);
1273 void convert_to_4k_imsm_disk(struct imsm_disk
*disk
)
1275 set_total_blocks(disk
, (total_blocks(disk
)/IMSM_4K_DIV
));
1278 void convert_to_4k(struct intel_super
*super
)
1280 struct imsm_super
*mpb
= super
->anchor
;
1281 struct imsm_disk
*disk
;
1284 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1285 disk
= __get_imsm_disk(mpb
, i
);
1287 convert_to_4k_imsm_disk(disk
);
1289 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1290 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1291 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1293 split_ull((join_u32(dev
->size_low
, dev
->size_high
)/IMSM_4K_DIV
),
1294 &dev
->size_low
, &dev
->size_high
);
1295 dev
->vol
.curr_migr_unit
/= IMSM_4K_DIV
;
1298 set_blocks_per_member(map
, blocks_per_member(map
)/IMSM_4K_DIV
);
1299 map
->blocks_per_strip
/= IMSM_4K_DIV
;
1300 set_pba_of_lba0(map
, pba_of_lba0(map
)/IMSM_4K_DIV
);
1302 if (dev
->vol
.migr_state
) {
1304 map
= get_imsm_map(dev
, MAP_1
);
1305 set_blocks_per_member(map
,
1306 blocks_per_member(map
)/IMSM_4K_DIV
);
1307 map
->blocks_per_strip
/= IMSM_4K_DIV
;
1308 set_pba_of_lba0(map
, pba_of_lba0(map
)/IMSM_4K_DIV
);
1312 mpb
->check_sum
= __gen_imsm_checksum(mpb
);
1315 void examine_migr_rec_imsm(struct intel_super
*super
)
1317 struct migr_record
*migr_rec
= super
->migr_rec
;
1318 struct imsm_super
*mpb
= super
->anchor
;
1321 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1322 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1323 struct imsm_map
*map
;
1326 if (is_gen_migration(dev
) == 0)
1329 printf("\nMigration Record Information:");
1331 /* first map under migration */
1332 map
= get_imsm_map(dev
, MAP_0
);
1334 slot
= get_imsm_disk_slot(map
, super
->disks
->index
);
1335 if (map
== NULL
|| slot
> 1 || slot
< 0) {
1336 printf(" Empty\n ");
1337 printf("Examine one of first two disks in array\n");
1340 printf("\n Status : ");
1341 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
1344 printf("Contains Data\n");
1345 printf(" Current Unit : %u\n",
1346 __le32_to_cpu(migr_rec
->curr_migr_unit
));
1347 printf(" Family : %u\n",
1348 __le32_to_cpu(migr_rec
->family_num
));
1349 printf(" Ascending : %u\n",
1350 __le32_to_cpu(migr_rec
->ascending_migr
));
1351 printf(" Blocks Per Unit : %u\n",
1352 __le32_to_cpu(migr_rec
->blocks_per_unit
));
1353 printf(" Dest. Depth Per Unit : %u\n",
1354 __le32_to_cpu(migr_rec
->dest_depth_per_unit
));
1355 printf(" Checkpoint Area pba : %u\n",
1356 __le32_to_cpu(migr_rec
->ckpt_area_pba
));
1357 printf(" First member lba : %u\n",
1358 __le32_to_cpu(migr_rec
->dest_1st_member_lba
));
1359 printf(" Total Number of Units : %u\n",
1360 __le32_to_cpu(migr_rec
->num_migr_units
));
1361 printf(" Size of volume : %u\n",
1362 __le32_to_cpu(migr_rec
->post_migr_vol_cap
));
1363 printf(" Expansion space for LBA64 : %u\n",
1364 __le32_to_cpu(migr_rec
->post_migr_vol_cap_hi
));
1365 printf(" Record was read from : %u\n",
1366 __le32_to_cpu(migr_rec
->ckpt_read_disk_num
));
1371 #endif /* MDASSEMBLE */
1373 void convert_from_4k_imsm_migr_rec(struct intel_super
*super
)
1375 struct migr_record
*migr_rec
= super
->migr_rec
;
1377 migr_rec
->blocks_per_unit
*= IMSM_4K_DIV
;
1378 migr_rec
->ckpt_area_pba
*= IMSM_4K_DIV
;
1379 migr_rec
->dest_1st_member_lba
*= IMSM_4K_DIV
;
1380 migr_rec
->dest_depth_per_unit
*= IMSM_4K_DIV
;
1381 split_ull((join_u32(migr_rec
->post_migr_vol_cap
,
1382 migr_rec
->post_migr_vol_cap_hi
) * IMSM_4K_DIV
),
1383 &migr_rec
->post_migr_vol_cap
,
1384 &migr_rec
->post_migr_vol_cap_hi
);
1387 void convert_from_4k(struct intel_super
*super
)
1389 struct imsm_super
*mpb
= super
->anchor
;
1390 struct imsm_disk
*disk
;
1393 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1394 disk
= __get_imsm_disk(mpb
, i
);
1396 set_total_blocks(disk
, (total_blocks(disk
)*IMSM_4K_DIV
));
1399 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1400 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1401 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1403 split_ull((join_u32(dev
->size_low
, dev
->size_high
)*IMSM_4K_DIV
),
1404 &dev
->size_low
, &dev
->size_high
);
1405 dev
->vol
.curr_migr_unit
*= IMSM_4K_DIV
;
1408 set_blocks_per_member(map
, blocks_per_member(map
)*IMSM_4K_DIV
);
1409 map
->blocks_per_strip
*= IMSM_4K_DIV
;
1410 set_pba_of_lba0(map
, pba_of_lba0(map
)*IMSM_4K_DIV
);
1412 if (dev
->vol
.migr_state
) {
1414 map
= get_imsm_map(dev
, MAP_1
);
1415 set_blocks_per_member(map
,
1416 blocks_per_member(map
)*IMSM_4K_DIV
);
1417 map
->blocks_per_strip
*= IMSM_4K_DIV
;
1418 set_pba_of_lba0(map
, pba_of_lba0(map
)*IMSM_4K_DIV
);
1422 mpb
->check_sum
= __gen_imsm_checksum(mpb
);
1425 /*******************************************************************************
1426 * function: imsm_check_attributes
1427 * Description: Function checks if features represented by attributes flags
1428 * are supported by mdadm.
1430 * attributes - Attributes read from metadata
1432 * 0 - passed attributes contains unsupported features flags
1433 * 1 - all features are supported
1434 ******************************************************************************/
1435 static int imsm_check_attributes(__u32 attributes
)
1438 __u32 not_supported
= MPB_ATTRIB_SUPPORTED
^0xffffffff;
1440 not_supported
&= ~MPB_ATTRIB_IGNORED
;
1442 not_supported
&= attributes
;
1443 if (not_supported
) {
1444 pr_err("(IMSM): Unsupported attributes : %x\n",
1445 (unsigned)__le32_to_cpu(not_supported
));
1446 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1447 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
1448 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1450 if (not_supported
& MPB_ATTRIB_2TB
) {
1451 dprintf("\t\tMPB_ATTRIB_2TB\n");
1452 not_supported
^= MPB_ATTRIB_2TB
;
1454 if (not_supported
& MPB_ATTRIB_RAID0
) {
1455 dprintf("\t\tMPB_ATTRIB_RAID0\n");
1456 not_supported
^= MPB_ATTRIB_RAID0
;
1458 if (not_supported
& MPB_ATTRIB_RAID1
) {
1459 dprintf("\t\tMPB_ATTRIB_RAID1\n");
1460 not_supported
^= MPB_ATTRIB_RAID1
;
1462 if (not_supported
& MPB_ATTRIB_RAID10
) {
1463 dprintf("\t\tMPB_ATTRIB_RAID10\n");
1464 not_supported
^= MPB_ATTRIB_RAID10
;
1466 if (not_supported
& MPB_ATTRIB_RAID1E
) {
1467 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
1468 not_supported
^= MPB_ATTRIB_RAID1E
;
1470 if (not_supported
& MPB_ATTRIB_RAID5
) {
1471 dprintf("\t\tMPB_ATTRIB_RAID5\n");
1472 not_supported
^= MPB_ATTRIB_RAID5
;
1474 if (not_supported
& MPB_ATTRIB_RAIDCNG
) {
1475 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
1476 not_supported
^= MPB_ATTRIB_RAIDCNG
;
1478 if (not_supported
& MPB_ATTRIB_BBM
) {
1479 dprintf("\t\tMPB_ATTRIB_BBM\n");
1480 not_supported
^= MPB_ATTRIB_BBM
;
1482 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1483 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
1484 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1486 if (not_supported
& MPB_ATTRIB_EXP_STRIPE_SIZE
) {
1487 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
1488 not_supported
^= MPB_ATTRIB_EXP_STRIPE_SIZE
;
1490 if (not_supported
& MPB_ATTRIB_2TB_DISK
) {
1491 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
1492 not_supported
^= MPB_ATTRIB_2TB_DISK
;
1494 if (not_supported
& MPB_ATTRIB_NEVER_USE2
) {
1495 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
1496 not_supported
^= MPB_ATTRIB_NEVER_USE2
;
1498 if (not_supported
& MPB_ATTRIB_NEVER_USE
) {
1499 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
1500 not_supported
^= MPB_ATTRIB_NEVER_USE
;
1504 dprintf("(IMSM): Unknown attributes : %x\n", not_supported
);
1513 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
1515 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
1517 struct intel_super
*super
= st
->sb
;
1518 struct imsm_super
*mpb
= super
->anchor
;
1519 char str
[MAX_SIGNATURE_LENGTH
];
1524 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
1527 snprintf(str
, MPB_SIG_LEN
, "%s", mpb
->sig
);
1528 printf(" Magic : %s\n", str
);
1529 snprintf(str
, strlen(MPB_VERSION_RAID0
), "%s", get_imsm_version(mpb
));
1530 printf(" Version : %s\n", get_imsm_version(mpb
));
1531 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
1532 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
1533 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
1534 printf(" Attributes : ");
1535 if (imsm_check_attributes(mpb
->attributes
))
1536 printf("All supported\n");
1538 printf("not supported\n");
1539 getinfo_super_imsm(st
, &info
, NULL
);
1540 fname_from_uuid(st
, &info
, nbuf
, ':');
1541 printf(" UUID : %s\n", nbuf
+ 5);
1542 sum
= __le32_to_cpu(mpb
->check_sum
);
1543 printf(" Checksum : %08x %s\n", sum
,
1544 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
1545 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
, super
->sector_size
));
1546 printf(" Disks : %d\n", mpb
->num_disks
);
1547 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
1548 print_imsm_disk(__get_imsm_disk(mpb
, super
->disks
->index
), super
->disks
->index
, reserved
);
1549 if (super
->bbm_log
) {
1550 struct bbm_log
*log
= super
->bbm_log
;
1553 printf("Bad Block Management Log:\n");
1554 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
1555 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
1556 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
1557 printf(" Spare Blocks : %d\n", __le32_to_cpu(log
->reserved_spare_block_count
));
1558 printf(" First Spare : %llx\n",
1559 (unsigned long long) __le64_to_cpu(log
->first_spare_lba
));
1561 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1563 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1565 super
->current_vol
= i
;
1566 getinfo_super_imsm(st
, &info
, NULL
);
1567 fname_from_uuid(st
, &info
, nbuf
, ':');
1568 print_imsm_dev(super
, dev
, nbuf
+ 5, super
->disks
->index
);
1570 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1571 if (i
== super
->disks
->index
)
1573 print_imsm_disk(__get_imsm_disk(mpb
, i
), i
, reserved
);
1576 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
1577 if (dl
->index
== -1)
1578 print_imsm_disk(&dl
->disk
, -1, reserved
);
1580 examine_migr_rec_imsm(super
);
1583 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
1585 /* We just write a generic IMSM ARRAY entry */
1588 struct intel_super
*super
= st
->sb
;
1590 if (!super
->anchor
->num_raid_devs
) {
1591 printf("ARRAY metadata=imsm\n");
1595 getinfo_super_imsm(st
, &info
, NULL
);
1596 fname_from_uuid(st
, &info
, nbuf
, ':');
1597 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
1600 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
1602 /* We just write a generic IMSM ARRAY entry */
1606 struct intel_super
*super
= st
->sb
;
1609 if (!super
->anchor
->num_raid_devs
)
1612 getinfo_super_imsm(st
, &info
, NULL
);
1613 fname_from_uuid(st
, &info
, nbuf
, ':');
1614 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1615 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1617 super
->current_vol
= i
;
1618 getinfo_super_imsm(st
, &info
, NULL
);
1619 fname_from_uuid(st
, &info
, nbuf1
, ':');
1620 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
1621 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
1625 static void export_examine_super_imsm(struct supertype
*st
)
1627 struct intel_super
*super
= st
->sb
;
1628 struct imsm_super
*mpb
= super
->anchor
;
1632 getinfo_super_imsm(st
, &info
, NULL
);
1633 fname_from_uuid(st
, &info
, nbuf
, ':');
1634 printf("MD_METADATA=imsm\n");
1635 printf("MD_LEVEL=container\n");
1636 printf("MD_UUID=%s\n", nbuf
+5);
1637 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
1640 static int copy_metadata_imsm(struct supertype
*st
, int from
, int to
)
1642 /* The second last sector of the device contains
1643 * the "struct imsm_super" metadata.
1644 * This contains mpb_size which is the size in bytes of the
1645 * extended metadata. This is located immediately before
1647 * We want to read all that, plus the last sector which
1648 * may contain a migration record, and write it all
1652 unsigned long long dsize
, offset
;
1654 struct imsm_super
*sb
;
1655 struct intel_super
*super
= st
->sb
;
1656 unsigned int sector_size
= super
->sector_size
;
1657 unsigned int written
= 0;
1659 if (posix_memalign(&buf
, MAX_SECTOR_SIZE
, MAX_SECTOR_SIZE
) != 0)
1662 if (!get_dev_size(from
, NULL
, &dsize
))
1665 if (lseek64(from
, dsize
-(2*sector_size
), 0) < 0)
1667 if (read(from
, buf
, sector_size
) != sector_size
)
1670 if (strncmp((char*)sb
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0)
1673 sectors
= mpb_sectors(sb
, sector_size
) + 2;
1674 offset
= dsize
- sectors
* sector_size
;
1675 if (lseek64(from
, offset
, 0) < 0 ||
1676 lseek64(to
, offset
, 0) < 0)
1678 while (written
< sectors
* sector_size
) {
1679 int n
= sectors
*sector_size
- written
;
1682 if (read(from
, buf
, n
) != n
)
1684 if (write(to
, buf
, n
) != n
)
1695 static void detail_super_imsm(struct supertype
*st
, char *homehost
)
1700 getinfo_super_imsm(st
, &info
, NULL
);
1701 fname_from_uuid(st
, &info
, nbuf
, ':');
1702 printf("\n UUID : %s\n", nbuf
+ 5);
1705 static void brief_detail_super_imsm(struct supertype
*st
)
1709 getinfo_super_imsm(st
, &info
, NULL
);
1710 fname_from_uuid(st
, &info
, nbuf
, ':');
1711 printf(" UUID=%s", nbuf
+ 5);
1714 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
);
1715 static void fd2devname(int fd
, char *name
);
1717 static int ahci_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
1719 /* dump an unsorted list of devices attached to AHCI Intel storage
1720 * controller, as well as non-connected ports
1722 int hba_len
= strlen(hba_path
) + 1;
1727 unsigned long port_mask
= (1 << port_count
) - 1;
1729 if (port_count
> (int)sizeof(port_mask
) * 8) {
1731 pr_err("port_count %d out of range\n", port_count
);
1735 /* scroll through /sys/dev/block looking for devices attached to
1738 dir
= opendir("/sys/dev/block");
1742 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
1753 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
1755 path
= devt_to_devpath(makedev(major
, minor
));
1758 if (!path_attached_to_hba(path
, hba_path
)) {
1764 /* retrieve the scsi device type */
1765 if (asprintf(&device
, "/sys/dev/block/%d:%d/device/xxxxxxx", major
, minor
) < 0) {
1767 pr_err("failed to allocate 'device'\n");
1771 sprintf(device
, "/sys/dev/block/%d:%d/device/type", major
, minor
);
1772 if (load_sys(device
, buf
, sizeof(buf
)) != 0) {
1774 pr_err("failed to read device type for %s\n",
1780 type
= strtoul(buf
, NULL
, 10);
1782 /* if it's not a disk print the vendor and model */
1783 if (!(type
== 0 || type
== 7 || type
== 14)) {
1786 sprintf(device
, "/sys/dev/block/%d:%d/device/vendor", major
, minor
);
1787 if (load_sys(device
, buf
, sizeof(buf
)) == 0) {
1788 strncpy(vendor
, buf
, sizeof(vendor
));
1789 vendor
[sizeof(vendor
) - 1] = '\0';
1790 c
= (char *) &vendor
[sizeof(vendor
) - 1];
1791 while (isspace(*c
) || *c
== '\0')
1795 sprintf(device
, "/sys/dev/block/%d:%d/device/model", major
, minor
);
1796 if (load_sys(device
, buf
, sizeof(buf
)) == 0) {
1797 strncpy(model
, buf
, sizeof(model
));
1798 model
[sizeof(model
) - 1] = '\0';
1799 c
= (char *) &model
[sizeof(model
) - 1];
1800 while (isspace(*c
) || *c
== '\0')
1804 if (vendor
[0] && model
[0])
1805 sprintf(buf
, "%.64s %.64s", vendor
, model
);
1807 switch (type
) { /* numbers from hald/linux/device.c */
1808 case 1: sprintf(buf
, "tape"); break;
1809 case 2: sprintf(buf
, "printer"); break;
1810 case 3: sprintf(buf
, "processor"); break;
1812 case 5: sprintf(buf
, "cdrom"); break;
1813 case 6: sprintf(buf
, "scanner"); break;
1814 case 8: sprintf(buf
, "media_changer"); break;
1815 case 9: sprintf(buf
, "comm"); break;
1816 case 12: sprintf(buf
, "raid"); break;
1817 default: sprintf(buf
, "unknown");
1823 /* chop device path to 'host%d' and calculate the port number */
1824 c
= strchr(&path
[hba_len
], '/');
1827 pr_err("%s - invalid path name\n", path
+ hba_len
);
1832 if ((sscanf(&path
[hba_len
], "ata%d", &port
) == 1) ||
1833 ((sscanf(&path
[hba_len
], "host%d", &port
) == 1)))
1837 *c
= '/'; /* repair the full string */
1838 pr_err("failed to determine port number for %s\n",
1845 /* mark this port as used */
1846 port_mask
&= ~(1 << port
);
1848 /* print out the device information */
1850 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
1854 fd
= dev_open(ent
->d_name
, O_RDONLY
);
1856 printf(" Port%d : - disk info unavailable -\n", port
);
1858 fd2devname(fd
, buf
);
1859 printf(" Port%d : %s", port
, buf
);
1860 if (imsm_read_serial(fd
, NULL
, (__u8
*) buf
) == 0)
1861 printf(" (%.*s)\n", MAX_RAID_SERIAL_LEN
, buf
);
1876 for (i
= 0; i
< port_count
; i
++)
1877 if (port_mask
& (1 << i
))
1878 printf(" Port%d : - no device attached -\n", i
);
1884 static int print_vmd_attached_devs(struct sys_dev
*hba
)
1892 if (hba
->type
!= SYS_DEV_VMD
)
1895 /* scroll through /sys/dev/block looking for devices attached to
1898 dir
= opendir("/sys/bus/pci/drivers/nvme");
1902 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
1905 /* is 'ent' a device? check that the 'subsystem' link exists and
1906 * that its target matches 'bus'
1908 sprintf(path
, "/sys/bus/pci/drivers/nvme/%s/subsystem",
1910 n
= readlink(path
, link
, sizeof(link
));
1911 if (n
< 0 || n
>= (int)sizeof(link
))
1914 c
= strrchr(link
, '/');
1917 if (strncmp("pci", c
+1, strlen("pci")) != 0)
1920 sprintf(path
, "/sys/bus/pci/drivers/nvme/%s", ent
->d_name
);
1921 /* if not a intel NVMe - skip it*/
1922 if (devpath_to_vendor(path
) != 0x8086)
1925 rp
= realpath(path
, NULL
);
1929 if (path_attached_to_hba(rp
, hba
->path
)) {
1930 printf(" NVMe under VMD : %s\n", rp
);
1939 static void print_found_intel_controllers(struct sys_dev
*elem
)
1941 for (; elem
; elem
= elem
->next
) {
1942 pr_err("found Intel(R) ");
1943 if (elem
->type
== SYS_DEV_SATA
)
1944 fprintf(stderr
, "SATA ");
1945 else if (elem
->type
== SYS_DEV_SAS
)
1946 fprintf(stderr
, "SAS ");
1947 else if (elem
->type
== SYS_DEV_NVME
)
1948 fprintf(stderr
, "NVMe ");
1950 if (elem
->type
== SYS_DEV_VMD
)
1951 fprintf(stderr
, "VMD domain");
1953 fprintf(stderr
, "RAID controller");
1956 fprintf(stderr
, " at %s", elem
->pci_id
);
1957 fprintf(stderr
, ".\n");
1962 static int ahci_get_port_count(const char *hba_path
, int *port_count
)
1969 if ((dir
= opendir(hba_path
)) == NULL
)
1972 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
1975 if ((sscanf(ent
->d_name
, "ata%d", &host
) != 1) &&
1976 ((sscanf(ent
->d_name
, "host%d", &host
) != 1)))
1978 if (*port_count
== 0)
1980 else if (host
< host_base
)
1983 if (host
+ 1 > *port_count
+ host_base
)
1984 *port_count
= host
+ 1 - host_base
;
1990 static void print_imsm_capability(const struct imsm_orom
*orom
)
1992 printf(" Platform : Intel(R) ");
1993 if (orom
->capabilities
== 0 && orom
->driver_features
== 0)
1994 printf("Matrix Storage Manager\n");
1996 printf("Rapid Storage Technology%s\n",
1997 imsm_orom_is_enterprise(orom
) ? " enterprise" : "");
1998 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
)
1999 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
,
2000 orom
->minor_ver
, orom
->hotfix_ver
, orom
->build
);
2001 printf(" RAID Levels :%s%s%s%s%s\n",
2002 imsm_orom_has_raid0(orom
) ? " raid0" : "",
2003 imsm_orom_has_raid1(orom
) ? " raid1" : "",
2004 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
2005 imsm_orom_has_raid10(orom
) ? " raid10" : "",
2006 imsm_orom_has_raid5(orom
) ? " raid5" : "");
2007 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2008 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
2009 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
2010 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
2011 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
2012 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
2013 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
2014 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
2015 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
2016 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
2017 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
2018 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
2019 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
2020 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
2021 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
2022 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
2023 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
2024 printf(" 2TB volumes :%s supported\n",
2025 (orom
->attr
& IMSM_OROM_ATTR_2TB
)?"":" not");
2026 printf(" 2TB disks :%s supported\n",
2027 (orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
)?"":" not");
2028 printf(" Max Disks : %d\n", orom
->tds
);
2029 printf(" Max Volumes : %d per array, %d per %s\n",
2030 orom
->vpa
, orom
->vphba
,
2031 imsm_orom_is_nvme(orom
) ? "platform" : "controller");
2035 static void print_imsm_capability_export(const struct imsm_orom
*orom
)
2037 printf("MD_FIRMWARE_TYPE=imsm\n");
2038 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
)
2039 printf("IMSM_VERSION=%d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
2040 orom
->hotfix_ver
, orom
->build
);
2041 printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n",
2042 imsm_orom_has_raid0(orom
) ? "raid0 " : "",
2043 imsm_orom_has_raid1(orom
) ? "raid1 " : "",
2044 imsm_orom_has_raid1e(orom
) ? "raid1e " : "",
2045 imsm_orom_has_raid5(orom
) ? "raid10 " : "",
2046 imsm_orom_has_raid10(orom
) ? "raid5 " : "");
2047 printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2048 imsm_orom_has_chunk(orom
, 2) ? "2k " : "",
2049 imsm_orom_has_chunk(orom
, 4) ? "4k " : "",
2050 imsm_orom_has_chunk(orom
, 8) ? "8k " : "",
2051 imsm_orom_has_chunk(orom
, 16) ? "16k " : "",
2052 imsm_orom_has_chunk(orom
, 32) ? "32k " : "",
2053 imsm_orom_has_chunk(orom
, 64) ? "64k " : "",
2054 imsm_orom_has_chunk(orom
, 128) ? "128k " : "",
2055 imsm_orom_has_chunk(orom
, 256) ? "256k " : "",
2056 imsm_orom_has_chunk(orom
, 512) ? "512k " : "",
2057 imsm_orom_has_chunk(orom
, 1024*1) ? "1M " : "",
2058 imsm_orom_has_chunk(orom
, 1024*2) ? "2M " : "",
2059 imsm_orom_has_chunk(orom
, 1024*4) ? "4M " : "",
2060 imsm_orom_has_chunk(orom
, 1024*8) ? "8M " : "",
2061 imsm_orom_has_chunk(orom
, 1024*16) ? "16M " : "",
2062 imsm_orom_has_chunk(orom
, 1024*32) ? "32M " : "",
2063 imsm_orom_has_chunk(orom
, 1024*64) ? "64M " : "");
2064 printf("IMSM_2TB_VOLUMES=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB
) ? "yes" : "no");
2065 printf("IMSM_2TB_DISKS=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) ? "yes" : "no");
2066 printf("IMSM_MAX_DISKS=%d\n",orom
->tds
);
2067 printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom
->vpa
);
2068 printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom
->vphba
);
2071 static int detail_platform_imsm(int verbose
, int enumerate_only
, char *controller_path
)
2073 /* There are two components to imsm platform support, the ahci SATA
2074 * controller and the option-rom. To find the SATA controller we
2075 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
2076 * controller with the Intel vendor id is present. This approach
2077 * allows mdadm to leverage the kernel's ahci detection logic, with the
2078 * caveat that if ahci.ko is not loaded mdadm will not be able to
2079 * detect platform raid capabilities. The option-rom resides in a
2080 * platform "Adapter ROM". We scan for its signature to retrieve the
2081 * platform capabilities. If raid support is disabled in the BIOS the
2082 * option-rom capability structure will not be available.
2084 struct sys_dev
*list
, *hba
;
2089 if (enumerate_only
) {
2090 if (check_env("IMSM_NO_PLATFORM"))
2092 list
= find_intel_devices();
2095 for (hba
= list
; hba
; hba
= hba
->next
) {
2096 if (find_imsm_capability(hba
)) {
2106 list
= find_intel_devices();
2109 pr_err("no active Intel(R) RAID controller found.\n");
2111 } else if (verbose
> 0)
2112 print_found_intel_controllers(list
);
2114 for (hba
= list
; hba
; hba
= hba
->next
) {
2115 if (controller_path
&& (compare_paths(hba
->path
, controller_path
) != 0))
2117 if (!find_imsm_capability(hba
)) {
2119 pr_err("imsm capabilities not found for controller: %s (type %s)\n",
2120 hba
->type
== SYS_DEV_VMD
? vmd_domain_to_controller(hba
, buf
) : hba
->path
,
2121 get_sys_dev_type(hba
->type
));
2127 if (controller_path
&& result
== 1) {
2128 pr_err("no active Intel(R) RAID controller found under %s\n",
2133 const struct orom_entry
*entry
;
2135 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2136 if (entry
->type
== SYS_DEV_VMD
) {
2137 print_imsm_capability(&entry
->orom
);
2138 for (hba
= list
; hba
; hba
= hba
->next
) {
2139 if (hba
->type
== SYS_DEV_VMD
) {
2141 printf(" I/O Controller : %s (%s)\n",
2142 vmd_domain_to_controller(hba
, buf
), get_sys_dev_type(hba
->type
));
2143 if (print_vmd_attached_devs(hba
)) {
2145 pr_err("failed to get devices attached to VMD domain.\n");
2154 print_imsm_capability(&entry
->orom
);
2155 if (entry
->type
== SYS_DEV_NVME
) {
2156 for (hba
= list
; hba
; hba
= hba
->next
) {
2157 if (hba
->type
== SYS_DEV_NVME
)
2158 printf(" NVMe Device : %s\n", hba
->path
);
2164 struct devid_list
*devid
;
2165 for (devid
= entry
->devid_list
; devid
; devid
= devid
->next
) {
2166 hba
= device_by_id(devid
->devid
);
2170 printf(" I/O Controller : %s (%s)\n",
2171 hba
->path
, get_sys_dev_type(hba
->type
));
2172 if (hba
->type
== SYS_DEV_SATA
) {
2173 host_base
= ahci_get_port_count(hba
->path
, &port_count
);
2174 if (ahci_enumerate_ports(hba
->path
, port_count
, host_base
, verbose
)) {
2176 pr_err("failed to enumerate ports on SATA controller at %s.\n", hba
->pci_id
);
2187 static int export_detail_platform_imsm(int verbose
, char *controller_path
)
2189 struct sys_dev
*list
, *hba
;
2192 list
= find_intel_devices();
2195 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n");
2200 for (hba
= list
; hba
; hba
= hba
->next
) {
2201 if (controller_path
&& (compare_paths(hba
->path
,controller_path
) != 0))
2203 if (!find_imsm_capability(hba
) && verbose
> 0) {
2205 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n",
2206 hba
->type
== SYS_DEV_VMD
? vmd_domain_to_controller(hba
, buf
) : hba
->path
);
2212 const struct orom_entry
*entry
;
2214 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2215 if (entry
->type
== SYS_DEV_VMD
) {
2216 for (hba
= list
; hba
; hba
= hba
->next
)
2217 print_imsm_capability_export(&entry
->orom
);
2220 print_imsm_capability_export(&entry
->orom
);
2228 static int match_home_imsm(struct supertype
*st
, char *homehost
)
2230 /* the imsm metadata format does not specify any host
2231 * identification information. We return -1 since we can never
2232 * confirm nor deny whether a given array is "meant" for this
2233 * host. We rely on compare_super and the 'family_num' fields to
2234 * exclude member disks that do not belong, and we rely on
2235 * mdadm.conf to specify the arrays that should be assembled.
2236 * Auto-assembly may still pick up "foreign" arrays.
2242 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
2244 /* The uuid returned here is used for:
2245 * uuid to put into bitmap file (Create, Grow)
2246 * uuid for backup header when saving critical section (Grow)
2247 * comparing uuids when re-adding a device into an array
2248 * In these cases the uuid required is that of the data-array,
2249 * not the device-set.
2250 * uuid to recognise same set when adding a missing device back
2251 * to an array. This is a uuid for the device-set.
2253 * For each of these we can make do with a truncated
2254 * or hashed uuid rather than the original, as long as
2256 * In each case the uuid required is that of the data-array,
2257 * not the device-set.
2259 /* imsm does not track uuid's so we synthesis one using sha1 on
2260 * - The signature (Which is constant for all imsm array, but no matter)
2261 * - the orig_family_num of the container
2262 * - the index number of the volume
2263 * - the 'serial' number of the volume.
2264 * Hopefully these are all constant.
2266 struct intel_super
*super
= st
->sb
;
2269 struct sha1_ctx ctx
;
2270 struct imsm_dev
*dev
= NULL
;
2273 /* some mdadm versions failed to set ->orig_family_num, in which
2274 * case fall back to ->family_num. orig_family_num will be
2275 * fixed up with the first metadata update.
2277 family_num
= super
->anchor
->orig_family_num
;
2278 if (family_num
== 0)
2279 family_num
= super
->anchor
->family_num
;
2280 sha1_init_ctx(&ctx
);
2281 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
2282 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
2283 if (super
->current_vol
>= 0)
2284 dev
= get_imsm_dev(super
, super
->current_vol
);
2286 __u32 vol
= super
->current_vol
;
2287 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
2288 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
2290 sha1_finish_ctx(&ctx
, buf
);
2291 memcpy(uuid
, buf
, 4*4);
2296 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
2298 __u8
*v
= get_imsm_version(mpb
);
2299 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
2300 char major
[] = { 0, 0, 0 };
2301 char minor
[] = { 0 ,0, 0 };
2302 char patch
[] = { 0, 0, 0 };
2303 char *ver_parse
[] = { major
, minor
, patch
};
2307 while (*v
!= '\0' && v
< end
) {
2308 if (*v
!= '.' && j
< 2)
2309 ver_parse
[i
][j
++] = *v
;
2317 *m
= strtol(minor
, NULL
, 0);
2318 *p
= strtol(patch
, NULL
, 0);
2322 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
2324 /* migr_strip_size when repairing or initializing parity */
2325 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2326 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2328 switch (get_imsm_raid_level(map
)) {
2333 return 128*1024 >> 9;
2337 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
2339 /* migr_strip_size when rebuilding a degraded disk, no idea why
2340 * this is different than migr_strip_size_resync(), but it's good
2343 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2344 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2346 switch (get_imsm_raid_level(map
)) {
2349 if (map
->num_members
% map
->num_domains
== 0)
2350 return 128*1024 >> 9;
2354 return max((__u32
) 64*1024 >> 9, chunk
);
2356 return 128*1024 >> 9;
2360 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
2362 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2363 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2364 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
2365 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
2367 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
2370 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
2372 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2373 int level
= get_imsm_raid_level(lo
);
2375 if (level
== 1 || level
== 10) {
2376 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2378 return hi
->num_domains
;
2380 return num_stripes_per_unit_resync(dev
);
2383 static __u8
imsm_num_data_members(struct imsm_dev
*dev
, int second_map
)
2385 /* named 'imsm_' because raid0, raid1 and raid10
2386 * counter-intuitively have the same number of data disks
2388 struct imsm_map
*map
= get_imsm_map(dev
, second_map
);
2390 switch (get_imsm_raid_level(map
)) {
2392 return map
->num_members
;
2396 return map
->num_members
/2;
2398 return map
->num_members
- 1;
2400 dprintf("unsupported raid level\n");
2405 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
2407 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2408 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2410 switch(get_imsm_raid_level(map
)) {
2413 return chunk
* map
->num_domains
;
2415 return chunk
* map
->num_members
;
2421 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
2423 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2424 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2425 __u32 strip
= block
/ chunk
;
2427 switch (get_imsm_raid_level(map
)) {
2430 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
2431 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
2433 return vol_stripe
* chunk
+ block
% chunk
;
2435 __u32 stripe
= strip
/ (map
->num_members
- 1);
2437 return stripe
* chunk
+ block
% chunk
;
2444 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
2445 struct imsm_dev
*dev
)
2447 /* calculate the conversion factor between per member 'blocks'
2448 * (md/{resync,rebuild}_start) and imsm migration units, return
2449 * 0 for the 'not migrating' and 'unsupported migration' cases
2451 if (!dev
->vol
.migr_state
)
2454 switch (migr_type(dev
)) {
2455 case MIGR_GEN_MIGR
: {
2456 struct migr_record
*migr_rec
= super
->migr_rec
;
2457 return __le32_to_cpu(migr_rec
->blocks_per_unit
);
2462 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2463 __u32 stripes_per_unit
;
2464 __u32 blocks_per_unit
;
2473 /* yes, this is really the translation of migr_units to
2474 * per-member blocks in the 'resync' case
2476 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
2477 migr_chunk
= migr_strip_blocks_resync(dev
);
2478 disks
= imsm_num_data_members(dev
, MAP_0
);
2479 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
2480 stripe
= __le16_to_cpu(map
->blocks_per_strip
) * disks
;
2481 segment
= blocks_per_unit
/ stripe
;
2482 block_rel
= blocks_per_unit
- segment
* stripe
;
2483 parity_depth
= parity_segment_depth(dev
);
2484 block_map
= map_migr_block(dev
, block_rel
);
2485 return block_map
+ parity_depth
* segment
;
2487 case MIGR_REBUILD
: {
2488 __u32 stripes_per_unit
;
2491 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
2492 migr_chunk
= migr_strip_blocks_rebuild(dev
);
2493 return migr_chunk
* stripes_per_unit
;
2495 case MIGR_STATE_CHANGE
:
2501 static int imsm_level_to_layout(int level
)
2509 return ALGORITHM_LEFT_ASYMMETRIC
;
2516 /*******************************************************************************
2517 * Function: read_imsm_migr_rec
2518 * Description: Function reads imsm migration record from last sector of disk
2520 * fd : disk descriptor
2521 * super : metadata info
2525 ******************************************************************************/
2526 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
)
2529 unsigned int sector_size
= super
->sector_size
;
2530 unsigned long long dsize
;
2532 get_dev_size(fd
, NULL
, &dsize
);
2533 if (lseek64(fd
, dsize
- (sector_size
*MIGR_REC_SECTOR_POSITION
),
2535 pr_err("Cannot seek to anchor block: %s\n",
2539 if (read(fd
, super
->migr_rec_buf
,
2540 MIGR_REC_BUF_SECTORS
*sector_size
) !=
2541 MIGR_REC_BUF_SECTORS
*sector_size
) {
2542 pr_err("Cannot read migr record block: %s\n",
2547 if (sector_size
== 4096)
2548 convert_from_4k_imsm_migr_rec(super
);
2554 static struct imsm_dev
*imsm_get_device_during_migration(
2555 struct intel_super
*super
)
2558 struct intel_dev
*dv
;
2560 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
2561 if (is_gen_migration(dv
->dev
))
2567 /*******************************************************************************
2568 * Function: load_imsm_migr_rec
2569 * Description: Function reads imsm migration record (it is stored at the last
2572 * super : imsm internal array info
2573 * info : general array info
2577 * -2 : no migration in progress
2578 ******************************************************************************/
2579 static int load_imsm_migr_rec(struct intel_super
*super
, struct mdinfo
*info
)
2586 struct imsm_dev
*dev
;
2587 struct imsm_map
*map
;
2590 /* find map under migration */
2591 dev
= imsm_get_device_during_migration(super
);
2592 /* nothing to load,no migration in progress?
2598 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
2599 /* read only from one of the first two slots */
2600 if ((sd
->disk
.raid_disk
< 0) ||
2601 (sd
->disk
.raid_disk
> 1))
2604 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
2605 fd
= dev_open(nm
, O_RDONLY
);
2611 map
= get_imsm_map(dev
, MAP_0
);
2612 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
2613 /* skip spare and failed disks
2617 /* read only from one of the first two slots */
2619 slot
= get_imsm_disk_slot(map
, dl
->index
);
2620 if (map
== NULL
|| slot
> 1 || slot
< 0)
2622 sprintf(nm
, "%d:%d", dl
->major
, dl
->minor
);
2623 fd
= dev_open(nm
, O_RDONLY
);
2630 retval
= read_imsm_migr_rec(fd
, super
);
2639 /*******************************************************************************
2640 * function: imsm_create_metadata_checkpoint_update
2641 * Description: It creates update for checkpoint change.
2643 * super : imsm internal array info
2644 * u : pointer to prepared update
2647 * If length is equal to 0, input pointer u contains no update
2648 ******************************************************************************/
2649 static int imsm_create_metadata_checkpoint_update(
2650 struct intel_super
*super
,
2651 struct imsm_update_general_migration_checkpoint
**u
)
2654 int update_memory_size
= 0;
2656 dprintf("(enter)\n");
2662 /* size of all update data without anchor */
2663 update_memory_size
=
2664 sizeof(struct imsm_update_general_migration_checkpoint
);
2666 *u
= xcalloc(1, update_memory_size
);
2668 dprintf("error: cannot get memory\n");
2671 (*u
)->type
= update_general_migration_checkpoint
;
2672 (*u
)->curr_migr_unit
= __le32_to_cpu(super
->migr_rec
->curr_migr_unit
);
2673 dprintf("prepared for %u\n", (*u
)->curr_migr_unit
);
2675 return update_memory_size
;
2678 static void imsm_update_metadata_locally(struct supertype
*st
,
2679 void *buf
, int len
);
2681 /*******************************************************************************
2682 * Function: write_imsm_migr_rec
2683 * Description: Function writes imsm migration record
2684 * (at the last sector of disk)
2686 * super : imsm internal array info
2690 ******************************************************************************/
2691 static int write_imsm_migr_rec(struct supertype
*st
)
2693 struct intel_super
*super
= st
->sb
;
2694 unsigned int sector_size
= super
->sector_size
;
2695 unsigned long long dsize
;
2701 struct imsm_update_general_migration_checkpoint
*u
;
2702 struct imsm_dev
*dev
;
2703 struct imsm_map
*map
;
2705 /* find map under migration */
2706 dev
= imsm_get_device_during_migration(super
);
2707 /* if no migration, write buffer anyway to clear migr_record
2708 * on disk based on first available device
2711 dev
= get_imsm_dev(super
, super
->current_vol
< 0 ? 0 :
2712 super
->current_vol
);
2714 map
= get_imsm_map(dev
, MAP_0
);
2716 if (sector_size
== 4096)
2717 convert_to_4k_imsm_migr_rec(super
);
2718 for (sd
= super
->disks
; sd
; sd
= sd
->next
) {
2721 /* skip failed and spare devices */
2724 /* write to 2 first slots only */
2726 slot
= get_imsm_disk_slot(map
, sd
->index
);
2727 if (map
== NULL
|| slot
> 1 || slot
< 0)
2730 sprintf(nm
, "%d:%d", sd
->major
, sd
->minor
);
2731 fd
= dev_open(nm
, O_RDWR
);
2734 get_dev_size(fd
, NULL
, &dsize
);
2735 if (lseek64(fd
, dsize
- (MIGR_REC_SECTOR_POSITION
*sector_size
),
2737 pr_err("Cannot seek to anchor block: %s\n",
2741 if (write(fd
, super
->migr_rec_buf
,
2742 MIGR_REC_BUF_SECTORS
*sector_size
) !=
2743 MIGR_REC_BUF_SECTORS
*sector_size
) {
2744 pr_err("Cannot write migr record block: %s\n",
2751 if (sector_size
== 4096)
2752 convert_from_4k_imsm_migr_rec(super
);
2753 /* update checkpoint information in metadata */
2754 len
= imsm_create_metadata_checkpoint_update(super
, &u
);
2756 dprintf("imsm: Cannot prepare update\n");
2759 /* update metadata locally */
2760 imsm_update_metadata_locally(st
, u
, len
);
2761 /* and possibly remotely */
2762 if (st
->update_tail
) {
2763 append_metadata_update(st
, u
, len
);
2764 /* during reshape we do all work inside metadata handler
2765 * manage_reshape(), so metadata update has to be triggered
2768 flush_metadata_updates(st
);
2769 st
->update_tail
= &st
->updates
;
2779 #endif /* MDASSEMBLE */
2781 /* spare/missing disks activations are not allowe when
2782 * array/container performs reshape operation, because
2783 * all arrays in container works on the same disks set
2785 int imsm_reshape_blocks_arrays_changes(struct intel_super
*super
)
2788 struct intel_dev
*i_dev
;
2789 struct imsm_dev
*dev
;
2791 /* check whole container
2793 for (i_dev
= super
->devlist
; i_dev
; i_dev
= i_dev
->next
) {
2795 if (is_gen_migration(dev
)) {
2796 /* No repair during any migration in container
2804 static unsigned long long imsm_component_size_aligment_check(int level
,
2806 unsigned int sector_size
,
2807 unsigned long long component_size
)
2809 unsigned int component_size_alligment
;
2811 /* check component size aligment
2813 component_size_alligment
= component_size
% (chunk_size
/sector_size
);
2815 dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alligment = %u\n",
2816 level
, chunk_size
, component_size
,
2817 component_size_alligment
);
2819 if (component_size_alligment
&& (level
!= 1) && (level
!= UnSet
)) {
2820 dprintf("imsm: reported component size alligned from %llu ",
2822 component_size
-= component_size_alligment
;
2823 dprintf_cont("to %llu (%i).\n",
2824 component_size
, component_size_alligment
);
2827 return component_size
;
2830 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
2832 struct intel_super
*super
= st
->sb
;
2833 struct migr_record
*migr_rec
= super
->migr_rec
;
2834 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
2835 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2836 struct imsm_map
*prev_map
= get_imsm_map(dev
, MAP_1
);
2837 struct imsm_map
*map_to_analyse
= map
;
2839 int map_disks
= info
->array
.raid_disks
;
2841 memset(info
, 0, sizeof(*info
));
2843 map_to_analyse
= prev_map
;
2845 dl
= super
->current_disk
;
2847 info
->container_member
= super
->current_vol
;
2848 info
->array
.raid_disks
= map
->num_members
;
2849 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
2850 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
2851 info
->array
.md_minor
= -1;
2852 info
->array
.ctime
= 0;
2853 info
->array
.utime
= 0;
2854 info
->array
.chunk_size
=
2855 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
2856 info
->array
.state
= !dev
->vol
.dirty
;
2857 info
->custom_array_size
= __le32_to_cpu(dev
->size_high
);
2858 info
->custom_array_size
<<= 32;
2859 info
->custom_array_size
|= __le32_to_cpu(dev
->size_low
);
2860 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
2862 if (is_gen_migration(dev
)) {
2863 info
->reshape_active
= 1;
2864 info
->new_level
= get_imsm_raid_level(map
);
2865 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
2866 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
2867 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
2868 if (info
->delta_disks
) {
2869 /* this needs to be applied to every array
2872 info
->reshape_active
= CONTAINER_RESHAPE
;
2874 /* We shape information that we give to md might have to be
2875 * modify to cope with md's requirement for reshaping arrays.
2876 * For example, when reshaping a RAID0, md requires it to be
2877 * presented as a degraded RAID4.
2878 * Also if a RAID0 is migrating to a RAID5 we need to specify
2879 * the array as already being RAID5, but the 'before' layout
2880 * is a RAID4-like layout.
2882 switch (info
->array
.level
) {
2884 switch(info
->new_level
) {
2886 /* conversion is happening as RAID4 */
2887 info
->array
.level
= 4;
2888 info
->array
.raid_disks
+= 1;
2891 /* conversion is happening as RAID5 */
2892 info
->array
.level
= 5;
2893 info
->array
.layout
= ALGORITHM_PARITY_N
;
2894 info
->delta_disks
-= 1;
2897 /* FIXME error message */
2898 info
->array
.level
= UnSet
;
2904 info
->new_level
= UnSet
;
2905 info
->new_layout
= UnSet
;
2906 info
->new_chunk
= info
->array
.chunk_size
;
2907 info
->delta_disks
= 0;
2911 info
->disk
.major
= dl
->major
;
2912 info
->disk
.minor
= dl
->minor
;
2913 info
->disk
.number
= dl
->index
;
2914 info
->disk
.raid_disk
= get_imsm_disk_slot(map_to_analyse
,
2918 info
->data_offset
= pba_of_lba0(map_to_analyse
);
2919 info
->component_size
= blocks_per_member(map_to_analyse
);
2921 info
->component_size
= imsm_component_size_aligment_check(
2923 info
->array
.chunk_size
,
2925 info
->component_size
);
2927 memset(info
->uuid
, 0, sizeof(info
->uuid
));
2928 info
->recovery_start
= MaxSector
;
2930 info
->reshape_progress
= 0;
2931 info
->resync_start
= MaxSector
;
2932 if ((map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
2934 imsm_reshape_blocks_arrays_changes(super
) == 0) {
2935 info
->resync_start
= 0;
2937 if (dev
->vol
.migr_state
) {
2938 switch (migr_type(dev
)) {
2941 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2943 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
2945 info
->resync_start
= blocks_per_unit
* units
;
2948 case MIGR_GEN_MIGR
: {
2949 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2951 __u64 units
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
2952 unsigned long long array_blocks
;
2955 if (__le32_to_cpu(migr_rec
->ascending_migr
) &&
2957 (__le32_to_cpu(migr_rec
->num_migr_units
)-1)) &&
2958 (super
->migr_rec
->rec_status
==
2959 __cpu_to_le32(UNIT_SRC_IN_CP_AREA
)))
2962 info
->reshape_progress
= blocks_per_unit
* units
;
2964 dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n",
2965 (unsigned long long)units
,
2966 (unsigned long long)blocks_per_unit
,
2967 info
->reshape_progress
);
2969 used_disks
= imsm_num_data_members(dev
, MAP_1
);
2970 if (used_disks
> 0) {
2971 array_blocks
= blocks_per_member(map
) *
2973 /* round array size down to closest MB
2975 info
->custom_array_size
= (array_blocks
2976 >> SECT_PER_MB_SHIFT
)
2977 << SECT_PER_MB_SHIFT
;
2981 /* we could emulate the checkpointing of
2982 * 'sync_action=check' migrations, but for now
2983 * we just immediately complete them
2986 /* this is handled by container_content_imsm() */
2987 case MIGR_STATE_CHANGE
:
2988 /* FIXME handle other migrations */
2990 /* we are not dirty, so... */
2991 info
->resync_start
= MaxSector
;
2995 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
2996 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
2998 info
->array
.major_version
= -1;
2999 info
->array
.minor_version
= -2;
3000 sprintf(info
->text_version
, "/%s/%d", st
->container_devnm
, info
->container_member
);
3001 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
3002 uuid_from_super_imsm(st
, info
->uuid
);
3006 for (i
=0; i
<map_disks
; i
++) {
3008 if (i
< info
->array
.raid_disks
) {
3009 struct imsm_disk
*dsk
;
3010 j
= get_imsm_disk_idx(dev
, i
, MAP_X
);
3011 dsk
= get_imsm_disk(super
, j
);
3012 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
3019 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
3020 int failed
, int look_in_map
);
3022 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
3026 static void manage_second_map(struct intel_super
*super
, struct imsm_dev
*dev
)
3028 if (is_gen_migration(dev
)) {
3031 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
3033 failed
= imsm_count_failed(super
, dev
, MAP_1
);
3034 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_1
);
3035 if (map2
->map_state
!= map_state
) {
3036 map2
->map_state
= map_state
;
3037 super
->updates_pending
++;
3043 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
3047 for (d
= super
->missing
; d
; d
= d
->next
)
3048 if (d
->index
== index
)
3053 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
3055 struct intel_super
*super
= st
->sb
;
3056 struct imsm_disk
*disk
;
3057 int map_disks
= info
->array
.raid_disks
;
3058 int max_enough
= -1;
3060 struct imsm_super
*mpb
;
3062 if (super
->current_vol
>= 0) {
3063 getinfo_super_imsm_volume(st
, info
, map
);
3066 memset(info
, 0, sizeof(*info
));
3068 /* Set raid_disks to zero so that Assemble will always pull in valid
3071 info
->array
.raid_disks
= 0;
3072 info
->array
.level
= LEVEL_CONTAINER
;
3073 info
->array
.layout
= 0;
3074 info
->array
.md_minor
= -1;
3075 info
->array
.ctime
= 0; /* N/A for imsm */
3076 info
->array
.utime
= 0;
3077 info
->array
.chunk_size
= 0;
3079 info
->disk
.major
= 0;
3080 info
->disk
.minor
= 0;
3081 info
->disk
.raid_disk
= -1;
3082 info
->reshape_active
= 0;
3083 info
->array
.major_version
= -1;
3084 info
->array
.minor_version
= -2;
3085 strcpy(info
->text_version
, "imsm");
3086 info
->safe_mode_delay
= 0;
3087 info
->disk
.number
= -1;
3088 info
->disk
.state
= 0;
3090 info
->recovery_start
= MaxSector
;
3091 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
3093 /* do we have the all the insync disks that we expect? */
3094 mpb
= super
->anchor
;
3096 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3097 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3098 int failed
, enough
, j
, missing
= 0;
3099 struct imsm_map
*map
;
3102 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3103 state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3104 map
= get_imsm_map(dev
, MAP_0
);
3106 /* any newly missing disks?
3107 * (catches single-degraded vs double-degraded)
3109 for (j
= 0; j
< map
->num_members
; j
++) {
3110 __u32 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
3111 __u32 idx
= ord_to_idx(ord
);
3113 if (!(ord
& IMSM_ORD_REBUILD
) &&
3114 get_imsm_missing(super
, idx
)) {
3120 if (state
== IMSM_T_STATE_FAILED
)
3122 else if (state
== IMSM_T_STATE_DEGRADED
&&
3123 (state
!= map
->map_state
|| missing
))
3125 else /* we're normal, or already degraded */
3127 if (is_gen_migration(dev
) && missing
) {
3128 /* during general migration we need all disks
3129 * that process is running on.
3130 * No new missing disk is allowed.
3134 /* no more checks necessary
3138 /* in the missing/failed disk case check to see
3139 * if at least one array is runnable
3141 max_enough
= max(max_enough
, enough
);
3143 dprintf("enough: %d\n", max_enough
);
3144 info
->container_enough
= max_enough
;
3147 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
3149 disk
= &super
->disks
->disk
;
3150 info
->data_offset
= total_blocks(&super
->disks
->disk
) - reserved
;
3151 info
->component_size
= reserved
;
3152 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
3153 /* we don't change info->disk.raid_disk here because
3154 * this state will be finalized in mdmon after we have
3155 * found the 'most fresh' version of the metadata
3157 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3158 info
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
3161 /* only call uuid_from_super_imsm when this disk is part of a populated container,
3162 * ->compare_super may have updated the 'num_raid_devs' field for spares
3164 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
3165 uuid_from_super_imsm(st
, info
->uuid
);
3167 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
3169 /* I don't know how to compute 'map' on imsm, so use safe default */
3172 for (i
= 0; i
< map_disks
; i
++)
3178 /* allocates memory and fills disk in mdinfo structure
3179 * for each disk in array */
3180 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
3182 struct mdinfo
*mddev
;
3183 struct intel_super
*super
= st
->sb
;
3184 struct imsm_disk
*disk
;
3187 if (!super
|| !super
->disks
)
3190 mddev
= xcalloc(1, sizeof(*mddev
));
3194 tmp
= xcalloc(1, sizeof(*tmp
));
3196 tmp
->next
= mddev
->devs
;
3198 tmp
->disk
.number
= count
++;
3199 tmp
->disk
.major
= dl
->major
;
3200 tmp
->disk
.minor
= dl
->minor
;
3201 tmp
->disk
.state
= is_configured(disk
) ?
3202 (1 << MD_DISK_ACTIVE
) : 0;
3203 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3204 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
3205 tmp
->disk
.raid_disk
= -1;
3211 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
3212 char *update
, char *devname
, int verbose
,
3213 int uuid_set
, char *homehost
)
3215 /* For 'assemble' and 'force' we need to return non-zero if any
3216 * change was made. For others, the return value is ignored.
3217 * Update options are:
3218 * force-one : This device looks a bit old but needs to be included,
3219 * update age info appropriately.
3220 * assemble: clear any 'faulty' flag to allow this device to
3222 * force-array: Array is degraded but being forced, mark it clean
3223 * if that will be needed to assemble it.
3225 * newdev: not used ????
3226 * grow: Array has gained a new device - this is currently for
3228 * resync: mark as dirty so a resync will happen.
3229 * name: update the name - preserving the homehost
3230 * uuid: Change the uuid of the array to match watch is given
3232 * Following are not relevant for this imsm:
3233 * sparc2.2 : update from old dodgey metadata
3234 * super-minor: change the preferred_minor number
3235 * summaries: update redundant counters.
3236 * homehost: update the recorded homehost
3237 * _reshape_progress: record new reshape_progress position.
3240 struct intel_super
*super
= st
->sb
;
3241 struct imsm_super
*mpb
;
3243 /* we can only update container info */
3244 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
3247 mpb
= super
->anchor
;
3249 if (strcmp(update
, "uuid") == 0) {
3250 /* We take this to mean that the family_num should be updated.
3251 * However that is much smaller than the uuid so we cannot really
3252 * allow an explicit uuid to be given. And it is hard to reliably
3254 * So if !uuid_set we know the current uuid is random and just used
3255 * the first 'int' and copy it to the other 3 positions.
3256 * Otherwise we require the 4 'int's to be the same as would be the
3257 * case if we are using a random uuid. So an explicit uuid will be
3258 * accepted as long as all for ints are the same... which shouldn't hurt
3261 info
->uuid
[1] = info
->uuid
[2] = info
->uuid
[3] = info
->uuid
[0];
3264 if (info
->uuid
[0] != info
->uuid
[1] ||
3265 info
->uuid
[1] != info
->uuid
[2] ||
3266 info
->uuid
[2] != info
->uuid
[3])
3272 mpb
->orig_family_num
= info
->uuid
[0];
3273 } else if (strcmp(update
, "assemble") == 0)
3278 /* successful update? recompute checksum */
3280 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
3285 static size_t disks_to_mpb_size(int disks
)
3289 size
= sizeof(struct imsm_super
);
3290 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
3291 size
+= 2 * sizeof(struct imsm_dev
);
3292 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
3293 size
+= (4 - 2) * sizeof(struct imsm_map
);
3294 /* 4 possible disk_ord_tbl's */
3295 size
+= 4 * (disks
- 1) * sizeof(__u32
);
3300 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
,
3301 unsigned long long data_offset
)
3303 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
3306 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
3309 static void free_devlist(struct intel_super
*super
)
3311 struct intel_dev
*dv
;
3313 while (super
->devlist
) {
3314 dv
= super
->devlist
->next
;
3315 free(super
->devlist
->dev
);
3316 free(super
->devlist
);
3317 super
->devlist
= dv
;
3321 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
3323 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
3326 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
3330 * 0 same, or first was empty, and second was copied
3331 * 1 second had wrong number
3333 * 3 wrong other info
3335 struct intel_super
*first
= st
->sb
;
3336 struct intel_super
*sec
= tst
->sb
;
3343 /* in platform dependent environment test if the disks
3344 * use the same Intel hba
3345 * If not on Intel hba at all, allow anything.
3347 if (!check_env("IMSM_NO_PLATFORM") && first
->hba
&& sec
->hba
) {
3348 if (first
->hba
->type
!= sec
->hba
->type
) {
3350 "HBAs of devices do not match %s != %s\n",
3351 get_sys_dev_type(first
->hba
->type
),
3352 get_sys_dev_type(sec
->hba
->type
));
3355 if (first
->orom
!= sec
->orom
) {
3357 "HBAs of devices do not match %s != %s\n",
3358 first
->hba
->pci_id
, sec
->hba
->pci_id
);
3363 /* if an anchor does not have num_raid_devs set then it is a free
3366 if (first
->anchor
->num_raid_devs
> 0 &&
3367 sec
->anchor
->num_raid_devs
> 0) {
3368 /* Determine if these disks might ever have been
3369 * related. Further disambiguation can only take place
3370 * in load_super_imsm_all
3372 __u32 first_family
= first
->anchor
->orig_family_num
;
3373 __u32 sec_family
= sec
->anchor
->orig_family_num
;
3375 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
3376 MAX_SIGNATURE_LENGTH
) != 0)
3379 if (first_family
== 0)
3380 first_family
= first
->anchor
->family_num
;
3381 if (sec_family
== 0)
3382 sec_family
= sec
->anchor
->family_num
;
3384 if (first_family
!= sec_family
)
3389 /* if 'first' is a spare promote it to a populated mpb with sec's
3392 if (first
->anchor
->num_raid_devs
== 0 &&
3393 sec
->anchor
->num_raid_devs
> 0) {
3395 struct intel_dev
*dv
;
3396 struct imsm_dev
*dev
;
3398 /* we need to copy raid device info from sec if an allocation
3399 * fails here we don't associate the spare
3401 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
3402 dv
= xmalloc(sizeof(*dv
));
3403 dev
= xmalloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
3406 dv
->next
= first
->devlist
;
3407 first
->devlist
= dv
;
3409 if (i
< sec
->anchor
->num_raid_devs
) {
3410 /* allocation failure */
3411 free_devlist(first
);
3412 pr_err("imsm: failed to associate spare\n");
3415 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
3416 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
3417 first
->anchor
->family_num
= sec
->anchor
->family_num
;
3418 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
3419 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
3420 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
3426 static void fd2devname(int fd
, char *name
)
3430 char dname
[PATH_MAX
];
3435 if (fstat(fd
, &st
) != 0)
3437 sprintf(path
, "/sys/dev/block/%d:%d",
3438 major(st
.st_rdev
), minor(st
.st_rdev
));
3440 rv
= readlink(path
, dname
, sizeof(dname
)-1);
3445 nm
= strrchr(dname
, '/');
3448 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
3452 static int nvme_get_serial(int fd
, void *buf
, size_t buf_len
)
3455 char *name
= fd2kname(fd
);
3460 if (strncmp(name
, "nvme", 4) != 0)
3463 snprintf(path
, sizeof(path
) - 1, "/sys/block/%s/device/serial", name
);
3465 return load_sys(path
, buf
, buf_len
);
3468 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
3470 static int imsm_read_serial(int fd
, char *devname
,
3471 __u8 serial
[MAX_RAID_SERIAL_LEN
])
3480 memset(buf
, 0, sizeof(buf
));
3482 rv
= nvme_get_serial(fd
, buf
, sizeof(buf
));
3485 rv
= scsi_get_serial(fd
, buf
, sizeof(buf
));
3487 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
3488 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3489 fd2devname(fd
, (char *) serial
);
3495 pr_err("Failed to retrieve serial for %s\n",
3500 /* trim all whitespace and non-printable characters and convert
3503 for (i
= 0, dest
= buf
; i
< sizeof(buf
) && buf
[i
]; i
++) {
3506 /* ':' is reserved for use in placeholder serial
3507 * numbers for missing disks
3518 /* truncate leading characters */
3519 if (len
> MAX_RAID_SERIAL_LEN
) {
3520 dest
+= len
- MAX_RAID_SERIAL_LEN
;
3521 len
= MAX_RAID_SERIAL_LEN
;
3524 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3525 memcpy(serial
, dest
, len
);
3530 static int serialcmp(__u8
*s1
, __u8
*s2
)
3532 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
3535 static void serialcpy(__u8
*dest
, __u8
*src
)
3537 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
3540 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
3544 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3545 if (serialcmp(dl
->serial
, serial
) == 0)
3551 static struct imsm_disk
*
3552 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
3556 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3557 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3559 if (serialcmp(disk
->serial
, serial
) == 0) {
3570 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3572 struct imsm_disk
*disk
;
3577 __u8 serial
[MAX_RAID_SERIAL_LEN
];
3579 rv
= imsm_read_serial(fd
, devname
, serial
);
3584 dl
= xcalloc(1, sizeof(*dl
));
3587 dl
->major
= major(stb
.st_rdev
);
3588 dl
->minor
= minor(stb
.st_rdev
);
3589 dl
->next
= super
->disks
;
3590 dl
->fd
= keep_fd
? fd
: -1;
3591 assert(super
->disks
== NULL
);
3593 serialcpy(dl
->serial
, serial
);
3596 fd2devname(fd
, name
);
3598 dl
->devname
= xstrdup(devname
);
3600 dl
->devname
= xstrdup(name
);
3602 /* look up this disk's index in the current anchor */
3603 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
3606 /* only set index on disks that are a member of a
3607 * populated contianer, i.e. one with raid_devs
3609 if (is_failed(&dl
->disk
))
3611 else if (is_spare(&dl
->disk
))
3619 /* When migrating map0 contains the 'destination' state while map1
3620 * contains the current state. When not migrating map0 contains the
3621 * current state. This routine assumes that map[0].map_state is set to
3622 * the current array state before being called.
3624 * Migration is indicated by one of the following states
3625 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
3626 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
3627 * map1state=unitialized)
3628 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
3630 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
3631 * map1state=degraded)
3632 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
3635 static void migrate(struct imsm_dev
*dev
, struct intel_super
*super
,
3636 __u8 to_state
, int migr_type
)
3638 struct imsm_map
*dest
;
3639 struct imsm_map
*src
= get_imsm_map(dev
, MAP_0
);
3641 dev
->vol
.migr_state
= 1;
3642 set_migr_type(dev
, migr_type
);
3643 dev
->vol
.curr_migr_unit
= 0;
3644 dest
= get_imsm_map(dev
, MAP_1
);
3646 /* duplicate and then set the target end state in map[0] */
3647 memcpy(dest
, src
, sizeof_imsm_map(src
));
3648 if (migr_type
== MIGR_REBUILD
|| migr_type
== MIGR_GEN_MIGR
) {
3652 for (i
= 0; i
< src
->num_members
; i
++) {
3653 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
3654 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
3658 if (migr_type
== MIGR_GEN_MIGR
)
3659 /* Clear migration record */
3660 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
3662 src
->map_state
= to_state
;
3665 static void end_migration(struct imsm_dev
*dev
, struct intel_super
*super
,
3668 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3669 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
== 0 ?
3673 /* merge any IMSM_ORD_REBUILD bits that were not successfully
3674 * completed in the last migration.
3676 * FIXME add support for raid-level-migration
3678 if (map_state
!= map
->map_state
&& (is_gen_migration(dev
) == 0) &&
3679 prev
->map_state
!= IMSM_T_STATE_UNINITIALIZED
) {
3680 /* when final map state is other than expected
3681 * merge maps (not for migration)
3685 for (i
= 0; i
< prev
->num_members
; i
++)
3686 for (j
= 0; j
< map
->num_members
; j
++)
3687 /* during online capacity expansion
3688 * disks position can be changed
3689 * if takeover is used
3691 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
3692 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
3693 map
->disk_ord_tbl
[j
] |=
3694 prev
->disk_ord_tbl
[i
];
3697 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3698 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3701 dev
->vol
.migr_state
= 0;
3702 set_migr_type(dev
, 0);
3703 dev
->vol
.curr_migr_unit
= 0;
3704 map
->map_state
= map_state
;
3708 static int parse_raid_devices(struct intel_super
*super
)
3711 struct imsm_dev
*dev_new
;
3712 size_t len
, len_migr
;
3714 size_t space_needed
= 0;
3715 struct imsm_super
*mpb
= super
->anchor
;
3717 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3718 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3719 struct intel_dev
*dv
;
3721 len
= sizeof_imsm_dev(dev_iter
, 0);
3722 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
3724 space_needed
+= len_migr
- len
;
3726 dv
= xmalloc(sizeof(*dv
));
3727 if (max_len
< len_migr
)
3729 if (max_len
> len_migr
)
3730 space_needed
+= max_len
- len_migr
;
3731 dev_new
= xmalloc(max_len
);
3732 imsm_copy_dev(dev_new
, dev_iter
);
3735 dv
->next
= super
->devlist
;
3736 super
->devlist
= dv
;
3739 /* ensure that super->buf is large enough when all raid devices
3742 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
3745 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
,
3746 super
->sector_size
);
3747 if (posix_memalign(&buf
, MAX_SECTOR_SIZE
, len
) != 0)
3750 memcpy(buf
, super
->buf
, super
->len
);
3751 memset(buf
+ super
->len
, 0, len
- super
->len
);
3760 /* retrieve a pointer to the bbm log which starts after all raid devices */
3761 struct bbm_log
*__get_imsm_bbm_log(struct imsm_super
*mpb
)
3765 if (__le32_to_cpu(mpb
->bbm_log_size
)) {
3767 ptr
+= mpb
->mpb_size
- __le32_to_cpu(mpb
->bbm_log_size
);
3773 /*******************************************************************************
3774 * Function: check_mpb_migr_compatibility
3775 * Description: Function checks for unsupported migration features:
3776 * - migration optimization area (pba_of_lba0)
3777 * - descending reshape (ascending_migr)
3779 * super : imsm metadata information
3781 * 0 : migration is compatible
3782 * -1 : migration is not compatible
3783 ******************************************************************************/
3784 int check_mpb_migr_compatibility(struct intel_super
*super
)
3786 struct imsm_map
*map0
, *map1
;
3787 struct migr_record
*migr_rec
= super
->migr_rec
;
3790 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3791 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3794 dev_iter
->vol
.migr_state
== 1 &&
3795 dev_iter
->vol
.migr_type
== MIGR_GEN_MIGR
) {
3796 /* This device is migrating */
3797 map0
= get_imsm_map(dev_iter
, MAP_0
);
3798 map1
= get_imsm_map(dev_iter
, MAP_1
);
3799 if (pba_of_lba0(map0
) != pba_of_lba0(map1
))
3800 /* migration optimization area was used */
3802 if (migr_rec
->ascending_migr
== 0
3803 && migr_rec
->dest_depth_per_unit
> 0)
3804 /* descending reshape not supported yet */
3811 static void __free_imsm(struct intel_super
*super
, int free_disks
);
3813 /* load_imsm_mpb - read matrix metadata
3814 * allocates super->mpb to be freed by free_imsm
3816 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
3818 unsigned long long dsize
;
3819 unsigned long long sectors
;
3820 unsigned int sector_size
= super
->sector_size
;
3822 struct imsm_super
*anchor
;
3825 get_dev_size(fd
, NULL
, &dsize
);
3826 if (dsize
< 2*sector_size
) {
3828 pr_err("%s: device to small for imsm\n",
3833 if (lseek64(fd
, dsize
- (sector_size
* 2), SEEK_SET
) < 0) {
3835 pr_err("Cannot seek to anchor block on %s: %s\n",
3836 devname
, strerror(errno
));
3840 if (posix_memalign((void **)&anchor
, sector_size
, sector_size
) != 0) {
3842 pr_err("Failed to allocate imsm anchor buffer on %s\n", devname
);
3845 if (read(fd
, anchor
, sector_size
) != sector_size
) {
3847 pr_err("Cannot read anchor block on %s: %s\n",
3848 devname
, strerror(errno
));
3853 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
3855 pr_err("no IMSM anchor on %s\n", devname
);
3860 __free_imsm(super
, 0);
3861 /* reload capability and hba */
3863 /* capability and hba must be updated with new super allocation */
3864 find_intel_hba_capability(fd
, super
, devname
);
3865 super
->len
= ROUND_UP(anchor
->mpb_size
, sector_size
);
3866 if (posix_memalign(&super
->buf
, MAX_SECTOR_SIZE
, super
->len
) != 0) {
3868 pr_err("unable to allocate %zu byte mpb buffer\n",
3873 memcpy(super
->buf
, anchor
, sector_size
);
3875 sectors
= mpb_sectors(anchor
, sector_size
) - 1;
3878 if (posix_memalign(&super
->migr_rec_buf
, sector_size
,
3879 MIGR_REC_BUF_SECTORS
*sector_size
) != 0) {
3880 pr_err("could not allocate migr_rec buffer\n");
3884 super
->clean_migration_record_by_mdmon
= 0;
3887 check_sum
= __gen_imsm_checksum(super
->anchor
);
3888 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3890 pr_err("IMSM checksum %x != %x on %s\n",
3892 __le32_to_cpu(super
->anchor
->check_sum
),
3900 /* read the extended mpb */
3901 if (lseek64(fd
, dsize
- (sector_size
* (2 + sectors
)), SEEK_SET
) < 0) {
3903 pr_err("Cannot seek to extended mpb on %s: %s\n",
3904 devname
, strerror(errno
));
3908 if ((unsigned int)read(fd
, super
->buf
+ sector_size
,
3909 super
->len
- sector_size
) != super
->len
- sector_size
) {
3911 pr_err("Cannot read extended mpb on %s: %s\n",
3912 devname
, strerror(errno
));
3916 check_sum
= __gen_imsm_checksum(super
->anchor
);
3917 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3919 pr_err("IMSM checksum %x != %x on %s\n",
3920 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
3925 /* FIXME the BBM log is disk specific so we cannot use this global
3926 * buffer for all disks. Ok for now since we only look at the global
3927 * bbm_log_size parameter to gate assembly
3929 super
->bbm_log
= __get_imsm_bbm_log(super
->anchor
);
3934 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
);
3936 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
3937 static void clear_hi(struct intel_super
*super
)
3939 struct imsm_super
*mpb
= super
->anchor
;
3941 if (mpb
->attributes
& MPB_ATTRIB_2TB_DISK
)
3943 for (i
= 0; i
< mpb
->num_disks
; ++i
) {
3944 struct imsm_disk
*disk
= &mpb
->disk
[i
];
3945 disk
->total_blocks_hi
= 0;
3947 for (i
= 0; i
< mpb
->num_raid_devs
; ++i
) {
3948 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3951 for (n
= 0; n
< 2; ++n
) {
3952 struct imsm_map
*map
= get_imsm_map(dev
, n
);
3955 map
->pba_of_lba0_hi
= 0;
3956 map
->blocks_per_member_hi
= 0;
3957 map
->num_data_stripes_hi
= 0;
3963 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3967 err
= load_imsm_mpb(fd
, super
, devname
);
3970 if (super
->sector_size
== 4096)
3971 convert_from_4k(super
);
3972 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
3975 err
= parse_raid_devices(super
);
3980 static void __free_imsm_disk(struct dl
*d
)
3992 static void free_imsm_disks(struct intel_super
*super
)
3996 while (super
->disks
) {
3998 super
->disks
= d
->next
;
3999 __free_imsm_disk(d
);
4001 while (super
->disk_mgmt_list
) {
4002 d
= super
->disk_mgmt_list
;
4003 super
->disk_mgmt_list
= d
->next
;
4004 __free_imsm_disk(d
);
4006 while (super
->missing
) {
4008 super
->missing
= d
->next
;
4009 __free_imsm_disk(d
);
4014 /* free all the pieces hanging off of a super pointer */
4015 static void __free_imsm(struct intel_super
*super
, int free_disks
)
4017 struct intel_hba
*elem
, *next
;
4023 /* unlink capability description */
4025 if (super
->migr_rec_buf
) {
4026 free(super
->migr_rec_buf
);
4027 super
->migr_rec_buf
= NULL
;
4030 free_imsm_disks(super
);
4031 free_devlist(super
);
4035 free((void *)elem
->path
);
4043 static void free_imsm(struct intel_super
*super
)
4045 __free_imsm(super
, 1);
4049 static void free_super_imsm(struct supertype
*st
)
4051 struct intel_super
*super
= st
->sb
;
4060 static struct intel_super
*alloc_super(void)
4062 struct intel_super
*super
= xcalloc(1, sizeof(*super
));
4064 super
->current_vol
= -1;
4065 super
->create_offset
= ~((unsigned long long) 0);
4070 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
4072 static int find_intel_hba_capability(int fd
, struct intel_super
*super
, char *devname
)
4074 struct sys_dev
*hba_name
;
4077 if (fd
< 0 || check_env("IMSM_NO_PLATFORM")) {
4082 hba_name
= find_disk_attached_hba(fd
, NULL
);
4085 pr_err("%s is not attached to Intel(R) RAID controller.\n",
4089 rv
= attach_hba_to_super(super
, hba_name
);
4092 struct intel_hba
*hba
= super
->hba
;
4094 pr_err("%s is attached to Intel(R) %s %s (%s),\n"
4095 " but the container is assigned to Intel(R) %s %s (",
4097 get_sys_dev_type(hba_name
->type
),
4098 hba_name
->type
== SYS_DEV_VMD
? "domain" : "RAID controller",
4099 hba_name
->pci_id
? : "Err!",
4100 get_sys_dev_type(super
->hba
->type
),
4101 hba
->type
== SYS_DEV_VMD
? "domain" : "RAID controller");
4104 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
4106 fprintf(stderr
, ", ");
4109 fprintf(stderr
, ").\n"
4110 " Mixing devices attached to different %s is not allowed.\n",
4111 hba_name
->type
== SYS_DEV_VMD
? "VMD domains" : "controllers");
4115 super
->orom
= find_imsm_capability(hba_name
);
4122 /* find_missing - helper routine for load_super_imsm_all that identifies
4123 * disks that have disappeared from the system. This routine relies on
4124 * the mpb being uptodate, which it is at load time.
4126 static int find_missing(struct intel_super
*super
)
4129 struct imsm_super
*mpb
= super
->anchor
;
4131 struct imsm_disk
*disk
;
4133 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4134 disk
= __get_imsm_disk(mpb
, i
);
4135 dl
= serial_to_dl(disk
->serial
, super
);
4139 dl
= xmalloc(sizeof(*dl
));
4143 dl
->devname
= xstrdup("missing");
4145 serialcpy(dl
->serial
, disk
->serial
);
4148 dl
->next
= super
->missing
;
4149 super
->missing
= dl
;
4156 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
4158 struct intel_disk
*idisk
= disk_list
;
4161 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
4163 idisk
= idisk
->next
;
4169 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
4170 struct intel_super
*super
,
4171 struct intel_disk
**disk_list
)
4173 struct imsm_disk
*d
= &super
->disks
->disk
;
4174 struct imsm_super
*mpb
= super
->anchor
;
4177 for (i
= 0; i
< tbl_size
; i
++) {
4178 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
4179 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
4181 if (tbl_mpb
->family_num
== mpb
->family_num
) {
4182 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
4183 dprintf("mpb from %d:%d matches %d:%d\n",
4184 super
->disks
->major
,
4185 super
->disks
->minor
,
4186 table
[i
]->disks
->major
,
4187 table
[i
]->disks
->minor
);
4191 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
4192 is_configured(d
) == is_configured(tbl_d
)) &&
4193 tbl_mpb
->generation_num
< mpb
->generation_num
) {
4194 /* current version of the mpb is a
4195 * better candidate than the one in
4196 * super_table, but copy over "cross
4197 * generational" status
4199 struct intel_disk
*idisk
;
4201 dprintf("mpb from %d:%d replaces %d:%d\n",
4202 super
->disks
->major
,
4203 super
->disks
->minor
,
4204 table
[i
]->disks
->major
,
4205 table
[i
]->disks
->minor
);
4207 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
4208 if (idisk
&& is_failed(&idisk
->disk
))
4209 tbl_d
->status
|= FAILED_DISK
;
4212 struct intel_disk
*idisk
;
4213 struct imsm_disk
*disk
;
4215 /* tbl_mpb is more up to date, but copy
4216 * over cross generational status before
4219 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
4220 if (disk
&& is_failed(disk
))
4221 d
->status
|= FAILED_DISK
;
4223 idisk
= disk_list_get(d
->serial
, *disk_list
);
4226 if (disk
&& is_configured(disk
))
4227 idisk
->disk
.status
|= CONFIGURED_DISK
;
4230 dprintf("mpb from %d:%d prefer %d:%d\n",
4231 super
->disks
->major
,
4232 super
->disks
->minor
,
4233 table
[i
]->disks
->major
,
4234 table
[i
]->disks
->minor
);
4242 table
[tbl_size
++] = super
;
4246 /* update/extend the merged list of imsm_disk records */
4247 for (j
= 0; j
< mpb
->num_disks
; j
++) {
4248 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
4249 struct intel_disk
*idisk
;
4251 idisk
= disk_list_get(disk
->serial
, *disk_list
);
4253 idisk
->disk
.status
|= disk
->status
;
4254 if (is_configured(&idisk
->disk
) ||
4255 is_failed(&idisk
->disk
))
4256 idisk
->disk
.status
&= ~(SPARE_DISK
);
4258 idisk
= xcalloc(1, sizeof(*idisk
));
4259 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
4260 idisk
->disk
= *disk
;
4261 idisk
->next
= *disk_list
;
4265 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
4272 static struct intel_super
*
4273 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
4276 struct imsm_super
*mpb
= super
->anchor
;
4280 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4281 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
4282 struct intel_disk
*idisk
;
4284 idisk
= disk_list_get(disk
->serial
, disk_list
);
4286 if (idisk
->owner
== owner
||
4287 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
4290 dprintf("'%.16s' owner %d != %d\n",
4291 disk
->serial
, idisk
->owner
,
4294 dprintf("unknown disk %x [%d]: %.16s\n",
4295 __le32_to_cpu(mpb
->family_num
), i
,
4301 if (ok_count
== mpb
->num_disks
)
4306 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
4308 struct intel_super
*s
;
4310 for (s
= super_list
; s
; s
= s
->next
) {
4311 if (family_num
!= s
->anchor
->family_num
)
4313 pr_err("Conflict, offlining family %#x on '%s'\n",
4314 __le32_to_cpu(family_num
), s
->disks
->devname
);
4318 static struct intel_super
*
4319 imsm_thunderdome(struct intel_super
**super_list
, int len
)
4321 struct intel_super
*super_table
[len
];
4322 struct intel_disk
*disk_list
= NULL
;
4323 struct intel_super
*champion
, *spare
;
4324 struct intel_super
*s
, **del
;
4329 memset(super_table
, 0, sizeof(super_table
));
4330 for (s
= *super_list
; s
; s
= s
->next
)
4331 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
4333 for (i
= 0; i
< tbl_size
; i
++) {
4334 struct imsm_disk
*d
;
4335 struct intel_disk
*idisk
;
4336 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
4339 d
= &s
->disks
->disk
;
4341 /* 'd' must appear in merged disk list for its
4342 * configuration to be valid
4344 idisk
= disk_list_get(d
->serial
, disk_list
);
4345 if (idisk
&& idisk
->owner
== i
)
4346 s
= validate_members(s
, disk_list
, i
);
4351 dprintf("marking family: %#x from %d:%d offline\n",
4353 super_table
[i
]->disks
->major
,
4354 super_table
[i
]->disks
->minor
);
4358 /* This is where the mdadm implementation differs from the Windows
4359 * driver which has no strict concept of a container. We can only
4360 * assemble one family from a container, so when returning a prodigal
4361 * array member to this system the code will not be able to disambiguate
4362 * the container contents that should be assembled ("foreign" versus
4363 * "local"). It requires user intervention to set the orig_family_num
4364 * to a new value to establish a new container. The Windows driver in
4365 * this situation fixes up the volume name in place and manages the
4366 * foreign array as an independent entity.
4371 for (i
= 0; i
< tbl_size
; i
++) {
4372 struct intel_super
*tbl_ent
= super_table
[i
];
4378 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
4383 if (s
&& !is_spare
) {
4384 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
4386 } else if (!s
&& !is_spare
)
4399 pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n",
4400 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
4402 /* collect all dl's onto 'champion', and update them to
4403 * champion's version of the status
4405 for (s
= *super_list
; s
; s
= s
->next
) {
4406 struct imsm_super
*mpb
= champion
->anchor
;
4407 struct dl
*dl
= s
->disks
;
4412 mpb
->attributes
|= s
->anchor
->attributes
& MPB_ATTRIB_2TB_DISK
;
4414 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4415 struct imsm_disk
*disk
;
4417 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
4420 /* only set index on disks that are a member of
4421 * a populated contianer, i.e. one with
4424 if (is_failed(&dl
->disk
))
4426 else if (is_spare(&dl
->disk
))
4432 if (i
>= mpb
->num_disks
) {
4433 struct intel_disk
*idisk
;
4435 idisk
= disk_list_get(dl
->serial
, disk_list
);
4436 if (idisk
&& is_spare(&idisk
->disk
) &&
4437 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
4445 dl
->next
= champion
->disks
;
4446 champion
->disks
= dl
;
4450 /* delete 'champion' from super_list */
4451 for (del
= super_list
; *del
; ) {
4452 if (*del
== champion
) {
4453 *del
= (*del
)->next
;
4456 del
= &(*del
)->next
;
4458 champion
->next
= NULL
;
4462 struct intel_disk
*idisk
= disk_list
;
4464 disk_list
= disk_list
->next
;
4472 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
);
4473 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
4474 int major
, int minor
, int keep_fd
);
4476 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4477 int *max
, int keep_fd
);
4479 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
4480 char *devname
, struct md_list
*devlist
,
4483 struct intel_super
*super_list
= NULL
;
4484 struct intel_super
*super
= NULL
;
4489 /* 'fd' is an opened container */
4490 err
= get_sra_super_block(fd
, &super_list
, devname
, &i
, keep_fd
);
4492 /* get super block from devlist devices */
4493 err
= get_devlist_super_block(devlist
, &super_list
, &i
, keep_fd
);
4496 /* all mpbs enter, maybe one leaves */
4497 super
= imsm_thunderdome(&super_list
, i
);
4503 if (find_missing(super
) != 0) {
4509 /* load migration record */
4510 err
= load_imsm_migr_rec(super
, NULL
);
4512 /* migration is in progress,
4513 * but migr_rec cannot be loaded,
4519 /* Check migration compatibility */
4520 if (err
== 0 && check_mpb_migr_compatibility(super
) != 0) {
4521 pr_err("Unsupported migration detected");
4523 fprintf(stderr
, " on %s\n", devname
);
4525 fprintf(stderr
, " (IMSM).\n");
4534 while (super_list
) {
4535 struct intel_super
*s
= super_list
;
4537 super_list
= super_list
->next
;
4546 strcpy(st
->container_devnm
, fd2devnm(fd
));
4548 st
->container_devnm
[0] = 0;
4549 if (err
== 0 && st
->ss
== NULL
) {
4550 st
->ss
= &super_imsm
;
4551 st
->minor_version
= 0;
4552 st
->max_devs
= IMSM_MAX_DEVICES
;
4558 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4559 int *max
, int keep_fd
)
4561 struct md_list
*tmpdev
;
4565 for (i
= 0, tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
4566 if (tmpdev
->used
!= 1)
4568 if (tmpdev
->container
== 1) {
4570 int fd
= dev_open(tmpdev
->devname
, O_RDONLY
|O_EXCL
);
4572 pr_err("cannot open device %s: %s\n",
4573 tmpdev
->devname
, strerror(errno
));
4577 err
= get_sra_super_block(fd
, super_list
,
4578 tmpdev
->devname
, &lmax
,
4587 int major
= major(tmpdev
->st_rdev
);
4588 int minor
= minor(tmpdev
->st_rdev
);
4589 err
= get_super_block(super_list
,
4606 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
4607 int major
, int minor
, int keep_fd
)
4609 struct intel_super
*s
;
4621 sprintf(nm
, "%d:%d", major
, minor
);
4622 dfd
= dev_open(nm
, O_RDWR
);
4628 get_dev_sector_size(dfd
, NULL
, &s
->sector_size
);
4629 find_intel_hba_capability(dfd
, s
, devname
);
4630 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4632 /* retry the load if we might have raced against mdmon */
4633 if (err
== 3 && devnm
&& mdmon_running(devnm
))
4634 for (retry
= 0; retry
< 3; retry
++) {
4636 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4642 s
->next
= *super_list
;
4650 if (dfd
>= 0 && !keep_fd
)
4657 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
)
4664 sra
= sysfs_read(fd
, NULL
, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
4668 if (sra
->array
.major_version
!= -1 ||
4669 sra
->array
.minor_version
!= -2 ||
4670 strcmp(sra
->text_version
, "imsm") != 0) {
4675 devnm
= fd2devnm(fd
);
4676 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
4677 if (get_super_block(super_list
, devnm
, devname
,
4678 sd
->disk
.major
, sd
->disk
.minor
, keep_fd
) != 0) {
4689 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
4691 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
, NULL
, 1);
4695 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
4697 struct intel_super
*super
;
4701 if (test_partition(fd
))
4702 /* IMSM not allowed on partitions */
4705 free_super_imsm(st
);
4707 super
= alloc_super();
4708 get_dev_sector_size(fd
, NULL
, &super
->sector_size
);
4709 /* Load hba and capabilities if they exist.
4710 * But do not preclude loading metadata in case capabilities or hba are
4711 * non-compliant and ignore_hw_compat is set.
4713 rv
= find_intel_hba_capability(fd
, super
, devname
);
4714 /* no orom/efi or non-intel hba of the disk */
4715 if (rv
!= 0 && st
->ignore_hw_compat
== 0) {
4717 pr_err("No OROM/EFI properties for %s\n", devname
);
4721 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
4723 /* retry the load if we might have raced against mdmon */
4725 struct mdstat_ent
*mdstat
= NULL
;
4726 char *name
= fd2kname(fd
);
4729 mdstat
= mdstat_by_component(name
);
4731 if (mdstat
&& mdmon_running(mdstat
->devnm
) && getpid() != mdmon_pid(mdstat
->devnm
)) {
4732 for (retry
= 0; retry
< 3; retry
++) {
4734 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
4740 free_mdstat(mdstat
);
4745 pr_err("Failed to load all information sections on %s\n", devname
);
4751 if (st
->ss
== NULL
) {
4752 st
->ss
= &super_imsm
;
4753 st
->minor_version
= 0;
4754 st
->max_devs
= IMSM_MAX_DEVICES
;
4757 /* load migration record */
4758 if (load_imsm_migr_rec(super
, NULL
) == 0) {
4759 /* Check for unsupported migration features */
4760 if (check_mpb_migr_compatibility(super
) != 0) {
4761 pr_err("Unsupported migration detected");
4763 fprintf(stderr
, " on %s\n", devname
);
4765 fprintf(stderr
, " (IMSM).\n");
4773 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
4775 if (info
->level
== 1)
4777 return info
->chunk_size
>> 9;
4780 static unsigned long long info_to_blocks_per_member(mdu_array_info_t
*info
,
4781 unsigned long long size
)
4783 if (info
->level
== 1)
4786 return (size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
4789 static void imsm_update_version_info(struct intel_super
*super
)
4791 /* update the version and attributes */
4792 struct imsm_super
*mpb
= super
->anchor
;
4794 struct imsm_dev
*dev
;
4795 struct imsm_map
*map
;
4798 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4799 dev
= get_imsm_dev(super
, i
);
4800 map
= get_imsm_map(dev
, MAP_0
);
4801 if (__le32_to_cpu(dev
->size_high
) > 0)
4802 mpb
->attributes
|= MPB_ATTRIB_2TB
;
4804 /* FIXME detect when an array spans a port multiplier */
4806 mpb
->attributes
|= MPB_ATTRIB_PM
;
4809 if (mpb
->num_raid_devs
> 1 ||
4810 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
4811 version
= MPB_VERSION_ATTRIBS
;
4812 switch (get_imsm_raid_level(map
)) {
4813 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
4814 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
4815 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
4816 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
4819 if (map
->num_members
>= 5)
4820 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
4821 else if (dev
->status
== DEV_CLONE_N_GO
)
4822 version
= MPB_VERSION_CNG
;
4823 else if (get_imsm_raid_level(map
) == 5)
4824 version
= MPB_VERSION_RAID5
;
4825 else if (map
->num_members
>= 3)
4826 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
4827 else if (get_imsm_raid_level(map
) == 1)
4828 version
= MPB_VERSION_RAID1
;
4830 version
= MPB_VERSION_RAID0
;
4832 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
4836 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
4838 struct imsm_super
*mpb
= super
->anchor
;
4839 char *reason
= NULL
;
4842 if (strlen(name
) > MAX_RAID_SERIAL_LEN
)
4843 reason
= "must be 16 characters or less";
4845 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4846 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4848 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
4849 reason
= "already exists";
4854 if (reason
&& !quiet
)
4855 pr_err("imsm volume name %s\n", reason
);
4860 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
4861 unsigned long long size
, char *name
,
4862 char *homehost
, int *uuid
,
4863 long long data_offset
)
4865 /* We are creating a volume inside a pre-existing container.
4866 * so st->sb is already set.
4868 struct intel_super
*super
= st
->sb
;
4869 unsigned int sector_size
= super
->sector_size
;
4870 struct imsm_super
*mpb
= super
->anchor
;
4871 struct intel_dev
*dv
;
4872 struct imsm_dev
*dev
;
4873 struct imsm_vol
*vol
;
4874 struct imsm_map
*map
;
4875 int idx
= mpb
->num_raid_devs
;
4877 unsigned long long array_blocks
;
4878 size_t size_old
, size_new
;
4879 unsigned long long num_data_stripes
;
4881 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
4882 pr_err("This imsm-container already has the maximum of %d volumes\n", super
->orom
->vpa
);
4886 /* ensure the mpb is large enough for the new data */
4887 size_old
= __le32_to_cpu(mpb
->mpb_size
);
4888 size_new
= disks_to_mpb_size(info
->nr_disks
);
4889 if (size_new
> size_old
) {
4891 size_t size_round
= ROUND_UP(size_new
, sector_size
);
4893 if (posix_memalign(&mpb_new
, sector_size
, size_round
) != 0) {
4894 pr_err("could not allocate new mpb\n");
4897 if (posix_memalign(&super
->migr_rec_buf
, sector_size
,
4898 MIGR_REC_BUF_SECTORS
*sector_size
) != 0) {
4899 pr_err("could not allocate migr_rec buffer\n");
4905 memcpy(mpb_new
, mpb
, size_old
);
4908 super
->anchor
= mpb_new
;
4909 mpb
->mpb_size
= __cpu_to_le32(size_new
);
4910 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
4912 super
->current_vol
= idx
;
4914 /* handle 'failed_disks' by either:
4915 * a) create dummy disk entries in the table if this the first
4916 * volume in the array. We add them here as this is the only
4917 * opportunity to add them. add_to_super_imsm_volume()
4918 * handles the non-failed disks and continues incrementing
4920 * b) validate that 'failed_disks' matches the current number
4921 * of missing disks if the container is populated
4923 if (super
->current_vol
== 0) {
4925 for (i
= 0; i
< info
->failed_disks
; i
++) {
4926 struct imsm_disk
*disk
;
4929 disk
= __get_imsm_disk(mpb
, i
);
4930 disk
->status
= CONFIGURED_DISK
| FAILED_DISK
;
4931 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
4932 snprintf((char *) disk
->serial
, MAX_RAID_SERIAL_LEN
,
4935 find_missing(super
);
4940 for (d
= super
->missing
; d
; d
= d
->next
)
4942 if (info
->failed_disks
> missing
) {
4943 pr_err("unable to add 'missing' disk to container\n");
4948 if (!check_name(super
, name
, 0))
4950 dv
= xmalloc(sizeof(*dv
));
4951 dev
= xcalloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
4952 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
4953 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
4954 info
->layout
, info
->chunk_size
,
4956 /* round array size down to closest MB */
4957 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
4959 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
4960 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
4961 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
4963 vol
->migr_state
= 0;
4964 set_migr_type(dev
, MIGR_INIT
);
4965 vol
->dirty
= !info
->state
;
4966 vol
->curr_migr_unit
= 0;
4967 map
= get_imsm_map(dev
, MAP_0
);
4968 set_pba_of_lba0(map
, super
->create_offset
);
4969 set_blocks_per_member(map
, info_to_blocks_per_member(info
, size
));
4970 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
4971 map
->failed_disk_num
= ~0;
4972 if (info
->level
> 0)
4973 map
->map_state
= (info
->state
? IMSM_T_STATE_NORMAL
4974 : IMSM_T_STATE_UNINITIALIZED
);
4976 map
->map_state
= info
->failed_disks
? IMSM_T_STATE_FAILED
:
4977 IMSM_T_STATE_NORMAL
;
4980 if (info
->level
== 1 && info
->raid_disks
> 2) {
4983 pr_err("imsm does not support more than 2 disksin a raid1 volume\n");
4987 map
->raid_level
= info
->level
;
4988 if (info
->level
== 10) {
4989 map
->raid_level
= 1;
4990 map
->num_domains
= info
->raid_disks
/ 2;
4991 } else if (info
->level
== 1)
4992 map
->num_domains
= info
->raid_disks
;
4994 map
->num_domains
= 1;
4996 /* info->size is only int so use the 'size' parameter instead */
4997 num_data_stripes
= (size
* 2) / info_to_blocks_per_strip(info
);
4998 num_data_stripes
/= map
->num_domains
;
4999 set_num_data_stripes(map
, num_data_stripes
);
5001 map
->num_members
= info
->raid_disks
;
5002 for (i
= 0; i
< map
->num_members
; i
++) {
5003 /* initialized in add_to_super */
5004 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
5006 mpb
->num_raid_devs
++;
5009 dv
->index
= super
->current_vol
;
5010 dv
->next
= super
->devlist
;
5011 super
->devlist
= dv
;
5013 imsm_update_version_info(super
);
5018 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
5019 unsigned long long size
, char *name
,
5020 char *homehost
, int *uuid
,
5021 unsigned long long data_offset
)
5023 /* This is primarily called by Create when creating a new array.
5024 * We will then get add_to_super called for each component, and then
5025 * write_init_super called to write it out to each device.
5026 * For IMSM, Create can create on fresh devices or on a pre-existing
5028 * To create on a pre-existing array a different method will be called.
5029 * This one is just for fresh drives.
5031 struct intel_super
*super
;
5032 struct imsm_super
*mpb
;
5036 if (data_offset
!= INVALID_SECTORS
) {
5037 pr_err("data-offset not supported by imsm\n");
5042 return init_super_imsm_volume(st
, info
, size
, name
, homehost
, uuid
,
5046 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
5048 mpb_size
= MAX_SECTOR_SIZE
;
5050 super
= alloc_super();
5052 posix_memalign(&super
->buf
, MAX_SECTOR_SIZE
, mpb_size
) != 0) {
5057 pr_err("could not allocate superblock\n");
5060 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
5061 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
) != 0) {
5062 pr_err("could not allocate migr_rec buffer\n");
5067 memset(super
->buf
, 0, mpb_size
);
5069 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5073 /* zeroing superblock */
5077 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5079 version
= (char *) mpb
->sig
;
5080 strcpy(version
, MPB_SIGNATURE
);
5081 version
+= strlen(MPB_SIGNATURE
);
5082 strcpy(version
, MPB_VERSION_RAID0
);
5088 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
5089 int fd
, char *devname
)
5091 struct intel_super
*super
= st
->sb
;
5092 struct imsm_super
*mpb
= super
->anchor
;
5093 struct imsm_disk
*_disk
;
5094 struct imsm_dev
*dev
;
5095 struct imsm_map
*map
;
5099 dev
= get_imsm_dev(super
, super
->current_vol
);
5100 map
= get_imsm_map(dev
, MAP_0
);
5102 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
5103 pr_err("%s: Cannot add spare devices to IMSM volume\n",
5109 /* we're doing autolayout so grab the pre-marked (in
5110 * validate_geometry) raid_disk
5112 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5113 if (dl
->raiddisk
== dk
->raid_disk
)
5116 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5117 if (dl
->major
== dk
->major
&&
5118 dl
->minor
== dk
->minor
)
5123 pr_err("%s is not a member of the same container\n", devname
);
5127 /* add a pristine spare to the metadata */
5128 if (dl
->index
< 0) {
5129 dl
->index
= super
->anchor
->num_disks
;
5130 super
->anchor
->num_disks
++;
5132 /* Check the device has not already been added */
5133 slot
= get_imsm_disk_slot(map
, dl
->index
);
5135 (get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
) & IMSM_ORD_REBUILD
) == 0) {
5136 pr_err("%s has been included in this array twice\n",
5140 set_imsm_ord_tbl_ent(map
, dk
->raid_disk
, dl
->index
);
5141 dl
->disk
.status
= CONFIGURED_DISK
;
5143 /* update size of 'missing' disks to be at least as large as the
5144 * largest acitve member (we only have dummy missing disks when
5145 * creating the first volume)
5147 if (super
->current_vol
== 0) {
5148 for (df
= super
->missing
; df
; df
= df
->next
) {
5149 if (total_blocks(&dl
->disk
) > total_blocks(&df
->disk
))
5150 set_total_blocks(&df
->disk
, total_blocks(&dl
->disk
));
5151 _disk
= __get_imsm_disk(mpb
, df
->index
);
5156 /* refresh unset/failed slots to point to valid 'missing' entries */
5157 for (df
= super
->missing
; df
; df
= df
->next
)
5158 for (slot
= 0; slot
< mpb
->num_disks
; slot
++) {
5159 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
5161 if ((ord
& IMSM_ORD_REBUILD
) == 0)
5163 set_imsm_ord_tbl_ent(map
, slot
, df
->index
| IMSM_ORD_REBUILD
);
5164 if (is_gen_migration(dev
)) {
5165 struct imsm_map
*map2
= get_imsm_map(dev
,
5167 int slot2
= get_imsm_disk_slot(map2
, df
->index
);
5168 if (slot2
< map2
->num_members
&& slot2
>= 0) {
5169 __u32 ord2
= get_imsm_ord_tbl_ent(dev
,
5172 if ((unsigned)df
->index
==
5174 set_imsm_ord_tbl_ent(map2
,
5180 dprintf("set slot:%d to missing disk:%d\n", slot
, df
->index
);
5184 /* if we are creating the first raid device update the family number */
5185 if (super
->current_vol
== 0) {
5187 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
5189 _disk
= __get_imsm_disk(mpb
, dl
->index
);
5190 if (!_dev
|| !_disk
) {
5191 pr_err("BUG mpb setup error\n");
5197 sum
+= __gen_imsm_checksum(mpb
);
5198 mpb
->family_num
= __cpu_to_le32(sum
);
5199 mpb
->orig_family_num
= mpb
->family_num
;
5201 super
->current_disk
= dl
;
5206 * Function marks disk as spare and restores disk serial
5207 * in case it was previously marked as failed by takeover operation
5209 * -1 : critical error
5210 * 0 : disk is marked as spare but serial is not set
5213 int mark_spare(struct dl
*disk
)
5215 __u8 serial
[MAX_RAID_SERIAL_LEN
];
5222 if (!imsm_read_serial(disk
->fd
, NULL
, serial
)) {
5223 /* Restore disk serial number, because takeover marks disk
5224 * as failed and adds to serial ':0' before it becomes
5227 serialcpy(disk
->serial
, serial
);
5228 serialcpy(disk
->disk
.serial
, serial
);
5231 disk
->disk
.status
= SPARE_DISK
;
5237 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
5238 int fd
, char *devname
,
5239 unsigned long long data_offset
)
5241 struct intel_super
*super
= st
->sb
;
5243 unsigned long long size
;
5244 unsigned int member_sector_size
;
5249 /* If we are on an RAID enabled platform check that the disk is
5250 * attached to the raid controller.
5251 * We do not need to test disks attachment for container based additions,
5252 * they shall be already tested when container was created/assembled.
5254 rv
= find_intel_hba_capability(fd
, super
, devname
);
5255 /* no orom/efi or non-intel hba of the disk */
5257 dprintf("capability: %p fd: %d ret: %d\n",
5258 super
->orom
, fd
, rv
);
5262 if (super
->current_vol
>= 0)
5263 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
5266 dd
= xcalloc(sizeof(*dd
), 1);
5267 dd
->major
= major(stb
.st_rdev
);
5268 dd
->minor
= minor(stb
.st_rdev
);
5269 dd
->devname
= devname
? xstrdup(devname
) : NULL
;
5272 dd
->action
= DISK_ADD
;
5273 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
5275 pr_err("failed to retrieve scsi serial, aborting\n");
5281 if (super
->hba
&& ((super
->hba
->type
== SYS_DEV_NVME
) ||
5282 (super
->hba
->type
== SYS_DEV_VMD
))) {
5284 char *devpath
= diskfd_to_devpath(fd
);
5285 char controller_path
[PATH_MAX
];
5288 pr_err("failed to get devpath, aborting\n");
5295 snprintf(controller_path
, PATH_MAX
-1, "%s/device", devpath
);
5298 if (devpath_to_vendor(controller_path
) == 0x8086) {
5300 * If Intel's NVMe drive has serial ended with
5301 * "-A","-B","-1" or "-2" it means that this is "x8"
5302 * device (double drive on single PCIe card).
5303 * User should be warned about potential data loss.
5305 for (i
= MAX_RAID_SERIAL_LEN
-1; i
> 0; i
--) {
5306 /* Skip empty character at the end */
5307 if (dd
->serial
[i
] == 0)
5310 if (((dd
->serial
[i
] == 'A') ||
5311 (dd
->serial
[i
] == 'B') ||
5312 (dd
->serial
[i
] == '1') ||
5313 (dd
->serial
[i
] == '2')) &&
5314 (dd
->serial
[i
-1] == '-'))
5315 pr_err("\tThe action you are about to take may put your data at risk.\n"
5316 "\tPlease note that x8 devices may consist of two separate x4 devices "
5317 "located on a single PCIe port.\n"
5318 "\tRAID 0 is the only supported configuration for this type of x8 device.\n");
5324 get_dev_size(fd
, NULL
, &size
);
5325 get_dev_sector_size(fd
, NULL
, &member_sector_size
);
5327 if (super
->sector_size
== 0) {
5328 /* this a first device, so sector_size is not set yet */
5329 super
->sector_size
= member_sector_size
;
5330 } else if (member_sector_size
!= super
->sector_size
) {
5331 pr_err("Mixing between different sector size is forbidden, aborting...\n");
5338 /* clear migr_rec when adding disk to container */
5339 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SECTORS
*super
->sector_size
);
5340 if (lseek64(fd
, size
- MIGR_REC_SECTOR_POSITION
*super
->sector_size
,
5342 if (write(fd
, super
->migr_rec_buf
,
5343 MIGR_REC_BUF_SECTORS
*super
->sector_size
) !=
5344 MIGR_REC_BUF_SECTORS
*super
->sector_size
)
5345 perror("Write migr_rec failed");
5349 serialcpy(dd
->disk
.serial
, dd
->serial
);
5350 set_total_blocks(&dd
->disk
, size
);
5351 if (__le32_to_cpu(dd
->disk
.total_blocks_hi
) > 0) {
5352 struct imsm_super
*mpb
= super
->anchor
;
5353 mpb
->attributes
|= MPB_ATTRIB_2TB_DISK
;
5356 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
5357 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
5359 dd
->disk
.scsi_id
= __cpu_to_le32(0);
5361 if (st
->update_tail
) {
5362 dd
->next
= super
->disk_mgmt_list
;
5363 super
->disk_mgmt_list
= dd
;
5365 dd
->next
= super
->disks
;
5367 super
->updates_pending
++;
5373 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
5375 struct intel_super
*super
= st
->sb
;
5378 /* remove from super works only in mdmon - for communication
5379 * manager - monitor. Check if communication memory buffer
5382 if (!st
->update_tail
) {
5383 pr_err("shall be used in mdmon context only\n");
5386 dd
= xcalloc(1, sizeof(*dd
));
5387 dd
->major
= dk
->major
;
5388 dd
->minor
= dk
->minor
;
5391 dd
->action
= DISK_REMOVE
;
5393 dd
->next
= super
->disk_mgmt_list
;
5394 super
->disk_mgmt_list
= dd
;
5399 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
5402 char buf
[MAX_SECTOR_SIZE
];
5403 struct imsm_super anchor
;
5404 } spare_record
__attribute__ ((aligned(MAX_SECTOR_SIZE
)));
5406 /* spare records have their own family number and do not have any defined raid
5409 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
5411 struct imsm_super
*mpb
= super
->anchor
;
5412 struct imsm_super
*spare
= &spare_record
.anchor
;
5416 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
));
5417 spare
->generation_num
= __cpu_to_le32(1UL);
5418 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5419 spare
->num_disks
= 1;
5420 spare
->num_raid_devs
= 0;
5421 spare
->cache_size
= mpb
->cache_size
;
5422 spare
->pwr_cycle_count
= __cpu_to_le32(1);
5424 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
5425 MPB_SIGNATURE MPB_VERSION_RAID0
);
5427 for (d
= super
->disks
; d
; d
= d
->next
) {
5431 spare
->disk
[0] = d
->disk
;
5432 if (__le32_to_cpu(d
->disk
.total_blocks_hi
) > 0)
5433 spare
->attributes
|= MPB_ATTRIB_2TB_DISK
;
5435 if (super
->sector_size
== 4096)
5436 convert_to_4k_imsm_disk(&spare
->disk
[0]);
5438 sum
= __gen_imsm_checksum(spare
);
5439 spare
->family_num
= __cpu_to_le32(sum
);
5440 spare
->orig_family_num
= 0;
5441 sum
= __gen_imsm_checksum(spare
);
5442 spare
->check_sum
= __cpu_to_le32(sum
);
5444 if (store_imsm_mpb(d
->fd
, spare
)) {
5445 pr_err("failed for device %d:%d %s\n",
5446 d
->major
, d
->minor
, strerror(errno
));
5458 static int write_super_imsm(struct supertype
*st
, int doclose
)
5460 struct intel_super
*super
= st
->sb
;
5461 unsigned int sector_size
= super
->sector_size
;
5462 struct imsm_super
*mpb
= super
->anchor
;
5468 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
5470 int clear_migration_record
= 1;
5472 /* 'generation' is incremented everytime the metadata is written */
5473 generation
= __le32_to_cpu(mpb
->generation_num
);
5475 mpb
->generation_num
= __cpu_to_le32(generation
);
5477 /* fix up cases where previous mdadm releases failed to set
5480 if (mpb
->orig_family_num
== 0)
5481 mpb
->orig_family_num
= mpb
->family_num
;
5483 for (d
= super
->disks
; d
; d
= d
->next
) {
5487 mpb
->disk
[d
->index
] = d
->disk
;
5491 for (d
= super
->missing
; d
; d
= d
->next
) {
5492 mpb
->disk
[d
->index
] = d
->disk
;
5495 mpb
->num_disks
= num_disks
;
5496 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
5498 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5499 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
5500 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
5502 imsm_copy_dev(dev
, dev2
);
5503 mpb_size
+= sizeof_imsm_dev(dev
, 0);
5505 if (is_gen_migration(dev2
))
5506 clear_migration_record
= 0;
5508 mpb_size
+= __le32_to_cpu(mpb
->bbm_log_size
);
5509 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5511 /* recalculate checksum */
5512 sum
= __gen_imsm_checksum(mpb
);
5513 mpb
->check_sum
= __cpu_to_le32(sum
);
5515 if (super
->clean_migration_record_by_mdmon
) {
5516 clear_migration_record
= 1;
5517 super
->clean_migration_record_by_mdmon
= 0;
5519 if (clear_migration_record
)
5520 memset(super
->migr_rec_buf
, 0,
5521 MIGR_REC_BUF_SECTORS
*sector_size
);
5523 if (sector_size
== 4096)
5524 convert_to_4k(super
);
5526 /* write the mpb for disks that compose raid devices */
5527 for (d
= super
->disks
; d
; d
= d
->next
) {
5528 if (d
->index
< 0 || is_failed(&d
->disk
))
5531 if (clear_migration_record
) {
5532 unsigned long long dsize
;
5534 get_dev_size(d
->fd
, NULL
, &dsize
);
5535 if (lseek64(d
->fd
, dsize
- sector_size
,
5537 if (write(d
->fd
, super
->migr_rec_buf
,
5538 MIGR_REC_BUF_SECTORS
*sector_size
) !=
5539 MIGR_REC_BUF_SECTORS
*sector_size
)
5540 perror("Write migr_rec failed");
5544 if (store_imsm_mpb(d
->fd
, mpb
))
5546 "failed for device %d:%d (fd: %d)%s\n",
5548 d
->fd
, strerror(errno
));
5557 return write_super_imsm_spares(super
, doclose
);
5562 static int create_array(struct supertype
*st
, int dev_idx
)
5565 struct imsm_update_create_array
*u
;
5566 struct intel_super
*super
= st
->sb
;
5567 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
5568 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
5569 struct disk_info
*inf
;
5570 struct imsm_disk
*disk
;
5573 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
5574 sizeof(*inf
) * map
->num_members
;
5576 u
->type
= update_create_array
;
5577 u
->dev_idx
= dev_idx
;
5578 imsm_copy_dev(&u
->dev
, dev
);
5579 inf
= get_disk_info(u
);
5580 for (i
= 0; i
< map
->num_members
; i
++) {
5581 int idx
= get_imsm_disk_idx(dev
, i
, MAP_X
);
5583 disk
= get_imsm_disk(super
, idx
);
5585 disk
= get_imsm_missing(super
, idx
);
5586 serialcpy(inf
[i
].serial
, disk
->serial
);
5588 append_metadata_update(st
, u
, len
);
5593 static int mgmt_disk(struct supertype
*st
)
5595 struct intel_super
*super
= st
->sb
;
5597 struct imsm_update_add_remove_disk
*u
;
5599 if (!super
->disk_mgmt_list
)
5604 u
->type
= update_add_remove_disk
;
5605 append_metadata_update(st
, u
, len
);
5610 static int write_init_super_imsm(struct supertype
*st
)
5612 struct intel_super
*super
= st
->sb
;
5613 int current_vol
= super
->current_vol
;
5615 /* we are done with current_vol reset it to point st at the container */
5616 super
->current_vol
= -1;
5618 if (st
->update_tail
) {
5619 /* queue the recently created array / added disk
5620 * as a metadata update */
5623 /* determine if we are creating a volume or adding a disk */
5624 if (current_vol
< 0) {
5625 /* in the mgmt (add/remove) disk case we are running
5626 * in mdmon context, so don't close fd's
5628 return mgmt_disk(st
);
5630 rv
= create_array(st
, current_vol
);
5635 for (d
= super
->disks
; d
; d
= d
->next
)
5636 Kill(d
->devname
, NULL
, 0, -1, 1);
5637 return write_super_imsm(st
, 1);
5642 static int store_super_imsm(struct supertype
*st
, int fd
)
5644 struct intel_super
*super
= st
->sb
;
5645 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
5651 if (super
->sector_size
== 4096)
5652 convert_to_4k(super
);
5653 return store_imsm_mpb(fd
, mpb
);
5659 static int imsm_bbm_log_size(struct imsm_super
*mpb
)
5661 return __le32_to_cpu(mpb
->bbm_log_size
);
5665 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
5666 int layout
, int raiddisks
, int chunk
,
5667 unsigned long long size
,
5668 unsigned long long data_offset
,
5670 unsigned long long *freesize
,
5674 unsigned long long ldsize
;
5675 struct intel_super
*super
;
5678 if (level
!= LEVEL_CONTAINER
)
5683 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
5686 pr_err("imsm: Cannot open %s: %s\n",
5687 dev
, strerror(errno
));
5690 if (!get_dev_size(fd
, dev
, &ldsize
)) {
5695 /* capabilities retrieve could be possible
5696 * note that there is no fd for the disks in array.
5698 super
= alloc_super();
5699 if (!get_dev_sector_size(fd
, NULL
, &super
->sector_size
)) {
5705 rv
= find_intel_hba_capability(fd
, super
, verbose
> 0 ? dev
: NULL
);
5709 fd2devname(fd
, str
);
5710 dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n",
5711 fd
, str
, super
->orom
, rv
, raiddisks
);
5713 /* no orom/efi or non-intel hba of the disk */
5720 if (raiddisks
> super
->orom
->tds
) {
5722 pr_err("%d exceeds maximum number of platform supported disks: %d\n",
5723 raiddisks
, super
->orom
->tds
);
5727 if ((super
->orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) == 0 &&
5728 (ldsize
>> 9) >> 32 > 0) {
5730 pr_err("%s exceeds maximum platform supported size\n", dev
);
5736 *freesize
= avail_size_imsm(st
, ldsize
>> 9, data_offset
);
5742 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
5744 const unsigned long long base_start
= e
[*idx
].start
;
5745 unsigned long long end
= base_start
+ e
[*idx
].size
;
5748 if (base_start
== end
)
5752 for (i
= *idx
; i
< num_extents
; i
++) {
5753 /* extend overlapping extents */
5754 if (e
[i
].start
>= base_start
&&
5755 e
[i
].start
<= end
) {
5758 if (e
[i
].start
+ e
[i
].size
> end
)
5759 end
= e
[i
].start
+ e
[i
].size
;
5760 } else if (e
[i
].start
> end
) {
5766 return end
- base_start
;
5769 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
5771 /* build a composite disk with all known extents and generate a new
5772 * 'maxsize' given the "all disks in an array must share a common start
5773 * offset" constraint
5775 struct extent
*e
= xcalloc(sum_extents
, sizeof(*e
));
5779 unsigned long long pos
;
5780 unsigned long long start
= 0;
5781 unsigned long long maxsize
;
5782 unsigned long reserve
;
5784 /* coalesce and sort all extents. also, check to see if we need to
5785 * reserve space between member arrays
5788 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5791 for (i
= 0; i
< dl
->extent_cnt
; i
++)
5794 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
5799 while (i
< sum_extents
) {
5800 e
[j
].start
= e
[i
].start
;
5801 e
[j
].size
= find_size(e
, &i
, sum_extents
);
5803 if (e
[j
-1].size
== 0)
5812 unsigned long long esize
;
5814 esize
= e
[i
].start
- pos
;
5815 if (esize
>= maxsize
) {
5820 pos
= e
[i
].start
+ e
[i
].size
;
5822 } while (e
[i
-1].size
);
5828 /* FIXME assumes volume at offset 0 is the first volume in a
5831 if (start_extent
> 0)
5832 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
5836 if (maxsize
< reserve
)
5839 super
->create_offset
= ~((unsigned long long) 0);
5840 if (start
+ reserve
> super
->create_offset
)
5841 return 0; /* start overflows create_offset */
5842 super
->create_offset
= start
+ reserve
;
5844 return maxsize
- reserve
;
5847 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
5849 if (level
< 0 || level
== 6 || level
== 4)
5852 /* if we have an orom prevent invalid raid levels */
5855 case 0: return imsm_orom_has_raid0(orom
);
5858 return imsm_orom_has_raid1e(orom
);
5859 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
5860 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
5861 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
5864 return 1; /* not on an Intel RAID platform so anything goes */
5870 active_arrays_by_format(char *name
, char* hba
, struct md_list
**devlist
,
5871 int dpa
, int verbose
)
5873 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
5874 struct mdstat_ent
*memb
;
5880 for (memb
= mdstat
; memb
; memb
= memb
->next
) {
5881 if (memb
->metadata_version
&&
5882 (strncmp(memb
->metadata_version
, "external:", 9) == 0) &&
5883 (strcmp(&memb
->metadata_version
[9], name
) == 0) &&
5884 !is_subarray(memb
->metadata_version
+9) &&
5886 struct dev_member
*dev
= memb
->members
;
5888 while(dev
&& (fd
< 0)) {
5889 char *path
= xmalloc(strlen(dev
->name
) + strlen("/dev/") + 1);
5890 num
= sprintf(path
, "%s%s", "/dev/", dev
->name
);
5892 fd
= open(path
, O_RDONLY
, 0);
5893 if (num
<= 0 || fd
< 0) {
5894 pr_vrb("Cannot open %s: %s\n",
5895 dev
->name
, strerror(errno
));
5901 if (fd
>= 0 && disk_attached_to_hba(fd
, hba
)) {
5902 struct mdstat_ent
*vol
;
5903 for (vol
= mdstat
; vol
; vol
= vol
->next
) {
5904 if (vol
->active
> 0 &&
5905 vol
->metadata_version
&&
5906 is_container_member(vol
, memb
->devnm
)) {
5911 if (*devlist
&& (found
< dpa
)) {
5912 dv
= xcalloc(1, sizeof(*dv
));
5913 dv
->devname
= xmalloc(strlen(memb
->devnm
) + strlen("/dev/") + 1);
5914 sprintf(dv
->devname
, "%s%s", "/dev/", memb
->devnm
);
5917 dv
->next
= *devlist
;
5925 free_mdstat(mdstat
);
5930 static struct md_list
*
5931 get_loop_devices(void)
5934 struct md_list
*devlist
= NULL
;
5937 for(i
= 0; i
< 12; i
++) {
5938 dv
= xcalloc(1, sizeof(*dv
));
5939 dv
->devname
= xmalloc(40);
5940 sprintf(dv
->devname
, "/dev/loop%d", i
);
5948 static struct md_list
*
5949 get_devices(const char *hba_path
)
5951 struct md_list
*devlist
= NULL
;
5958 devlist
= get_loop_devices();
5961 /* scroll through /sys/dev/block looking for devices attached to
5964 dir
= opendir("/sys/dev/block");
5965 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
5970 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
5972 path
= devt_to_devpath(makedev(major
, minor
));
5975 if (!path_attached_to_hba(path
, hba_path
)) {
5982 fd
= dev_open(ent
->d_name
, O_RDONLY
);
5984 fd2devname(fd
, buf
);
5987 pr_err("cannot open device: %s\n",
5992 dv
= xcalloc(1, sizeof(*dv
));
5993 dv
->devname
= xstrdup(buf
);
6000 devlist
= devlist
->next
;
6010 count_volumes_list(struct md_list
*devlist
, char *homehost
,
6011 int verbose
, int *found
)
6013 struct md_list
*tmpdev
;
6015 struct supertype
*st
;
6017 /* first walk the list of devices to find a consistent set
6018 * that match the criterea, if that is possible.
6019 * We flag the ones we like with 'used'.
6022 st
= match_metadata_desc_imsm("imsm");
6024 pr_vrb("cannot allocate memory for imsm supertype\n");
6028 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
6029 char *devname
= tmpdev
->devname
;
6031 struct supertype
*tst
;
6033 if (tmpdev
->used
> 1)
6035 tst
= dup_super(st
);
6037 pr_vrb("cannot allocate memory for imsm supertype\n");
6040 tmpdev
->container
= 0;
6041 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
6043 dprintf("cannot open device %s: %s\n",
6044 devname
, strerror(errno
));
6046 } else if (fstat(dfd
, &stb
)< 0) {
6048 dprintf("fstat failed for %s: %s\n",
6049 devname
, strerror(errno
));
6051 } else if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
6052 dprintf("%s is not a block device.\n",
6055 } else if (must_be_container(dfd
)) {
6056 struct supertype
*cst
;
6057 cst
= super_by_fd(dfd
, NULL
);
6059 dprintf("cannot recognize container type %s\n",
6062 } else if (tst
->ss
!= st
->ss
) {
6063 dprintf("non-imsm container - ignore it: %s\n",
6066 } else if (!tst
->ss
->load_container
||
6067 tst
->ss
->load_container(tst
, dfd
, NULL
))
6070 tmpdev
->container
= 1;
6073 cst
->ss
->free_super(cst
);
6075 tmpdev
->st_rdev
= stb
.st_rdev
;
6076 if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
6077 dprintf("no RAID superblock on %s\n",
6080 } else if (tst
->ss
->compare_super
== NULL
) {
6081 dprintf("Cannot assemble %s metadata on %s\n",
6082 tst
->ss
->name
, devname
);
6088 if (tmpdev
->used
== 2 || tmpdev
->used
== 4) {
6089 /* Ignore unrecognised devices during auto-assembly */
6094 tst
->ss
->getinfo_super(tst
, &info
, NULL
);
6096 if (st
->minor_version
== -1)
6097 st
->minor_version
= tst
->minor_version
;
6099 if (memcmp(info
.uuid
, uuid_zero
,
6100 sizeof(int[4])) == 0) {
6101 /* this is a floating spare. It cannot define
6102 * an array unless there are no more arrays of
6103 * this type to be found. It can be included
6104 * in an array of this type though.
6110 if (st
->ss
!= tst
->ss
||
6111 st
->minor_version
!= tst
->minor_version
||
6112 st
->ss
->compare_super(st
, tst
) != 0) {
6113 /* Some mismatch. If exactly one array matches this host,
6114 * we can resolve on that one.
6115 * Or, if we are auto assembling, we just ignore the second
6118 dprintf("superblock on %s doesn't match others - assembly aborted\n",
6124 dprintf("found: devname: %s\n", devname
);
6128 tst
->ss
->free_super(tst
);
6132 if ((err
= load_super_imsm_all(st
, -1, &st
->sb
, NULL
, devlist
, 0)) == 0) {
6133 struct mdinfo
*iter
, *head
= st
->ss
->container_content(st
, NULL
);
6134 for (iter
= head
; iter
; iter
= iter
->next
) {
6135 dprintf("content->text_version: %s vol\n",
6136 iter
->text_version
);
6137 if (iter
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
6138 /* do not assemble arrays with unsupported
6140 dprintf("Cannot activate member %s.\n",
6141 iter
->text_version
);
6148 dprintf("No valid super block on device list: err: %d %p\n",
6152 dprintf("no more devices to examine\n");
6155 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
6156 if (tmpdev
->used
== 1 && tmpdev
->found
) {
6158 if (count
< tmpdev
->found
)
6161 count
-= tmpdev
->found
;
6164 if (tmpdev
->used
== 1)
6169 st
->ss
->free_super(st
);
6174 count_volumes(struct intel_hba
*hba
, int dpa
, int verbose
)
6176 struct sys_dev
*idev
, *intel_devices
= find_intel_devices();
6178 const struct orom_entry
*entry
;
6179 struct devid_list
*dv
, *devid_list
;
6181 if (!hba
|| !hba
->path
)
6184 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
6185 if (strstr(idev
->path
, hba
->path
))
6189 if (!idev
|| !idev
->dev_id
)
6192 entry
= get_orom_entry_by_device_id(idev
->dev_id
);
6194 if (!entry
|| !entry
->devid_list
)
6197 devid_list
= entry
->devid_list
;
6198 for (dv
= devid_list
; dv
; dv
= dv
->next
) {
6199 struct md_list
*devlist
;
6200 struct sys_dev
*device
= device_by_id(dv
->devid
);
6205 hba_path
= device
->path
;
6209 devlist
= get_devices(hba_path
);
6210 /* if no intel devices return zero volumes */
6211 if (devlist
== NULL
)
6214 count
+= active_arrays_by_format("imsm", hba_path
, &devlist
, dpa
, verbose
);
6215 dprintf("path: %s active arrays: %d\n", hba_path
, count
);
6216 if (devlist
== NULL
)
6220 count
+= count_volumes_list(devlist
,
6224 dprintf("found %d count: %d\n", found
, count
);
6227 dprintf("path: %s total number of volumes: %d\n", hba_path
, count
);
6230 struct md_list
*dv
= devlist
;
6231 devlist
= devlist
->next
;
6239 static int imsm_default_chunk(const struct imsm_orom
*orom
)
6241 /* up to 512 if the plaform supports it, otherwise the platform max.
6242 * 128 if no platform detected
6244 int fs
= max(7, orom
? fls(orom
->sss
) : 0);
6246 return min(512, (1 << fs
));
6250 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
6251 int raiddisks
, int *chunk
, unsigned long long size
, int verbose
)
6253 /* check/set platform and metadata limits/defaults */
6254 if (super
->orom
&& raiddisks
> super
->orom
->dpa
) {
6255 pr_vrb("platform supports a maximum of %d disks per array\n",
6260 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
6261 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
6262 pr_vrb("platform does not support raid%d with %d disk%s\n",
6263 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
6267 if (*chunk
== 0 || *chunk
== UnSet
)
6268 *chunk
= imsm_default_chunk(super
->orom
);
6270 if (super
->orom
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
6271 pr_vrb("platform does not support a chunk size of: %d\n", *chunk
);
6275 if (layout
!= imsm_level_to_layout(level
)) {
6277 pr_vrb("imsm raid 5 only supports the left-asymmetric layout\n");
6278 else if (level
== 10)
6279 pr_vrb("imsm raid 10 only supports the n2 layout\n");
6281 pr_vrb("imsm unknown layout %#x for this raid level %d\n",
6286 if (super
->orom
&& (super
->orom
->attr
& IMSM_OROM_ATTR_2TB
) == 0 &&
6287 (calc_array_size(level
, raiddisks
, layout
, *chunk
, size
) >> 32) > 0) {
6288 pr_vrb("platform does not support a volume size over 2TB\n");
6295 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
6296 * FIX ME add ahci details
6298 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
6299 int layout
, int raiddisks
, int *chunk
,
6300 unsigned long long size
,
6301 unsigned long long data_offset
,
6303 unsigned long long *freesize
,
6307 struct intel_super
*super
= st
->sb
;
6308 struct imsm_super
*mpb
;
6310 unsigned long long pos
= 0;
6311 unsigned long long maxsize
;
6315 /* We must have the container info already read in. */
6319 mpb
= super
->anchor
;
6321 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, size
, verbose
)) {
6322 pr_err("RAID gemetry validation failed. Cannot proceed with the action(s).\n");
6326 /* General test: make sure there is space for
6327 * 'raiddisks' device extents of size 'size' at a given
6330 unsigned long long minsize
= size
;
6331 unsigned long long start_offset
= MaxSector
;
6334 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
6335 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6340 e
= get_extents(super
, dl
);
6343 unsigned long long esize
;
6344 esize
= e
[i
].start
- pos
;
6345 if (esize
>= minsize
)
6347 if (found
&& start_offset
== MaxSector
) {
6350 } else if (found
&& pos
!= start_offset
) {
6354 pos
= e
[i
].start
+ e
[i
].size
;
6356 } while (e
[i
-1].size
);
6361 if (dcnt
< raiddisks
) {
6363 pr_err("imsm: Not enough devices with space for this array (%d < %d)\n",
6370 /* This device must be a member of the set */
6371 if (stat(dev
, &stb
) < 0)
6373 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
6375 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6376 if (dl
->major
== (int)major(stb
.st_rdev
) &&
6377 dl
->minor
== (int)minor(stb
.st_rdev
))
6382 pr_err("%s is not in the same imsm set\n", dev
);
6384 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
6385 /* If a volume is present then the current creation attempt
6386 * cannot incorporate new spares because the orom may not
6387 * understand this configuration (all member disks must be
6388 * members of each array in the container).
6390 pr_err("%s is a spare and a volume is already defined for this container\n", dev
);
6391 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
6393 } else if (super
->orom
&& mpb
->num_raid_devs
> 0 &&
6394 mpb
->num_disks
!= raiddisks
) {
6395 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
6399 /* retrieve the largest free space block */
6400 e
= get_extents(super
, dl
);
6405 unsigned long long esize
;
6407 esize
= e
[i
].start
- pos
;
6408 if (esize
>= maxsize
)
6410 pos
= e
[i
].start
+ e
[i
].size
;
6412 } while (e
[i
-1].size
);
6417 pr_err("unable to determine free space for: %s\n",
6421 if (maxsize
< size
) {
6423 pr_err("%s not enough space (%llu < %llu)\n",
6424 dev
, maxsize
, size
);
6428 /* count total number of extents for merge */
6430 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6432 i
+= dl
->extent_cnt
;
6434 maxsize
= merge_extents(super
, i
);
6436 if (!check_env("IMSM_NO_PLATFORM") &&
6437 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6438 pr_err("attempting to create a second volume with size less then remaining space. Aborting...\n");
6442 if (maxsize
< size
|| maxsize
== 0) {
6445 pr_err("no free space left on device. Aborting...\n");
6447 pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n",
6453 *freesize
= maxsize
;
6456 int count
= count_volumes(super
->hba
,
6457 super
->orom
->dpa
, verbose
);
6458 if (super
->orom
->vphba
<= count
) {
6459 pr_vrb("platform does not support more than %d raid volumes.\n",
6460 super
->orom
->vphba
);
6467 static int imsm_get_free_size(struct supertype
*st
, int raiddisks
,
6468 unsigned long long size
, int chunk
,
6469 unsigned long long *freesize
)
6471 struct intel_super
*super
= st
->sb
;
6472 struct imsm_super
*mpb
= super
->anchor
;
6477 unsigned long long maxsize
;
6478 unsigned long long minsize
;
6482 /* find the largest common start free region of the possible disks */
6486 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6492 /* don't activate new spares if we are orom constrained
6493 * and there is already a volume active in the container
6495 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
6498 e
= get_extents(super
, dl
);
6501 for (i
= 1; e
[i
-1].size
; i
++)
6509 maxsize
= merge_extents(super
, extent_cnt
);
6513 minsize
= chunk
* 2;
6515 if (cnt
< raiddisks
||
6516 (super
->orom
&& used
&& used
!= raiddisks
) ||
6517 maxsize
< minsize
||
6519 pr_err("not enough devices with space to create array.\n");
6520 return 0; /* No enough free spaces large enough */
6531 if (!check_env("IMSM_NO_PLATFORM") &&
6532 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6533 pr_err("attempting to create a second volume with size less then remaining space. Aborting...\n");
6537 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6539 dl
->raiddisk
= cnt
++;
6543 dprintf("imsm: imsm_get_free_size() returns : %llu\n", size
);
6548 static int reserve_space(struct supertype
*st
, int raiddisks
,
6549 unsigned long long size
, int chunk
,
6550 unsigned long long *freesize
)
6552 struct intel_super
*super
= st
->sb
;
6557 rv
= imsm_get_free_size(st
, raiddisks
, size
, chunk
, freesize
);
6560 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6562 dl
->raiddisk
= cnt
++;
6569 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
6570 int raiddisks
, int *chunk
, unsigned long long size
,
6571 unsigned long long data_offset
,
6572 char *dev
, unsigned long long *freesize
,
6580 * if given unused devices create a container
6581 * if given given devices in a container create a member volume
6583 if (level
== LEVEL_CONTAINER
) {
6584 /* Must be a fresh device to add to a container */
6585 return validate_geometry_imsm_container(st
, level
, layout
,
6595 struct intel_super
*super
= st
->sb
;
6596 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
6597 raiddisks
, chunk
, size
,
6600 /* we are being asked to automatically layout a
6601 * new volume based on the current contents of
6602 * the container. If the the parameters can be
6603 * satisfied reserve_space will record the disks,
6604 * start offset, and size of the volume to be
6605 * created. add_to_super and getinfo_super
6606 * detect when autolayout is in progress.
6608 /* assuming that freesize is always given when array is
6610 if (super
->orom
&& freesize
) {
6612 count
= count_volumes(super
->hba
,
6613 super
->orom
->dpa
, verbose
);
6614 if (super
->orom
->vphba
<= count
) {
6615 pr_vrb("platform does not support more than %d raid volumes.\n",
6616 super
->orom
->vphba
);
6621 return reserve_space(st
, raiddisks
, size
,
6627 /* creating in a given container */
6628 return validate_geometry_imsm_volume(st
, level
, layout
,
6629 raiddisks
, chunk
, size
,
6631 dev
, freesize
, verbose
);
6634 /* This device needs to be a device in an 'imsm' container */
6635 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
6638 pr_err("Cannot create this array on device %s\n",
6643 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
6645 pr_err("Cannot open %s: %s\n",
6646 dev
, strerror(errno
));
6649 /* Well, it is in use by someone, maybe an 'imsm' container. */
6650 cfd
= open_container(fd
);
6654 pr_err("Cannot use %s: It is busy\n",
6658 sra
= sysfs_read(cfd
, NULL
, GET_VERSION
);
6659 if (sra
&& sra
->array
.major_version
== -1 &&
6660 strcmp(sra
->text_version
, "imsm") == 0)
6664 /* This is a member of a imsm container. Load the container
6665 * and try to create a volume
6667 struct intel_super
*super
;
6669 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
, NULL
, 1) == 0) {
6671 strcpy(st
->container_devnm
, fd2devnm(cfd
));
6673 return validate_geometry_imsm_volume(st
, level
, layout
,
6675 size
, data_offset
, dev
,
6682 pr_err("failed container membership check\n");
6688 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
6690 struct intel_super
*super
= st
->sb
;
6692 if (level
&& *level
== UnSet
)
6693 *level
= LEVEL_CONTAINER
;
6695 if (level
&& layout
&& *layout
== UnSet
)
6696 *layout
= imsm_level_to_layout(*level
);
6698 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0))
6699 *chunk
= imsm_default_chunk(super
->orom
);
6702 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
6704 static int kill_subarray_imsm(struct supertype
*st
)
6706 /* remove the subarray currently referenced by ->current_vol */
6708 struct intel_dev
**dp
;
6709 struct intel_super
*super
= st
->sb
;
6710 __u8 current_vol
= super
->current_vol
;
6711 struct imsm_super
*mpb
= super
->anchor
;
6713 if (super
->current_vol
< 0)
6715 super
->current_vol
= -1; /* invalidate subarray cursor */
6717 /* block deletions that would change the uuid of active subarrays
6719 * FIXME when immutable ids are available, but note that we'll
6720 * also need to fixup the invalidated/active subarray indexes in
6723 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6726 if (i
< current_vol
)
6728 sprintf(subarray
, "%u", i
);
6729 if (is_subarray_active(subarray
, st
->devnm
)) {
6730 pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
6737 if (st
->update_tail
) {
6738 struct imsm_update_kill_array
*u
= xmalloc(sizeof(*u
));
6740 u
->type
= update_kill_array
;
6741 u
->dev_idx
= current_vol
;
6742 append_metadata_update(st
, u
, sizeof(*u
));
6747 for (dp
= &super
->devlist
; *dp
;)
6748 if ((*dp
)->index
== current_vol
) {
6751 handle_missing(super
, (*dp
)->dev
);
6752 if ((*dp
)->index
> current_vol
)
6757 /* no more raid devices, all active components are now spares,
6758 * but of course failed are still failed
6760 if (--mpb
->num_raid_devs
== 0) {
6763 for (d
= super
->disks
; d
; d
= d
->next
)
6768 super
->updates_pending
++;
6773 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
6774 char *update
, struct mddev_ident
*ident
)
6776 /* update the subarray currently referenced by ->current_vol */
6777 struct intel_super
*super
= st
->sb
;
6778 struct imsm_super
*mpb
= super
->anchor
;
6780 if (strcmp(update
, "name") == 0) {
6781 char *name
= ident
->name
;
6785 if (is_subarray_active(subarray
, st
->devnm
)) {
6786 pr_err("Unable to update name of active subarray\n");
6790 if (!check_name(super
, name
, 0))
6793 vol
= strtoul(subarray
, &ep
, 10);
6794 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
6797 if (st
->update_tail
) {
6798 struct imsm_update_rename_array
*u
= xmalloc(sizeof(*u
));
6800 u
->type
= update_rename_array
;
6802 snprintf((char *) u
->name
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6803 append_metadata_update(st
, u
, sizeof(*u
));
6805 struct imsm_dev
*dev
;
6808 dev
= get_imsm_dev(super
, vol
);
6809 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6810 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6811 dev
= get_imsm_dev(super
, i
);
6812 handle_missing(super
, dev
);
6814 super
->updates_pending
++;
6821 #endif /* MDASSEMBLE */
6823 static int is_gen_migration(struct imsm_dev
*dev
)
6828 if (!dev
->vol
.migr_state
)
6831 if (migr_type(dev
) == MIGR_GEN_MIGR
)
6837 static int is_rebuilding(struct imsm_dev
*dev
)
6839 struct imsm_map
*migr_map
;
6841 if (!dev
->vol
.migr_state
)
6844 if (migr_type(dev
) != MIGR_REBUILD
)
6847 migr_map
= get_imsm_map(dev
, MAP_1
);
6849 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
6856 static int is_initializing(struct imsm_dev
*dev
)
6858 struct imsm_map
*migr_map
;
6860 if (!dev
->vol
.migr_state
)
6863 if (migr_type(dev
) != MIGR_INIT
)
6866 migr_map
= get_imsm_map(dev
, MAP_1
);
6868 if (migr_map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
6875 static void update_recovery_start(struct intel_super
*super
,
6876 struct imsm_dev
*dev
,
6877 struct mdinfo
*array
)
6879 struct mdinfo
*rebuild
= NULL
;
6883 if (!is_rebuilding(dev
))
6886 /* Find the rebuild target, but punt on the dual rebuild case */
6887 for (d
= array
->devs
; d
; d
= d
->next
)
6888 if (d
->recovery_start
== 0) {
6895 /* (?) none of the disks are marked with
6896 * IMSM_ORD_REBUILD, so assume they are missing and the
6897 * disk_ord_tbl was not correctly updated
6899 dprintf("failed to locate out-of-sync disk\n");
6903 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
6904 rebuild
->recovery_start
= units
* blocks_per_migr_unit(super
, dev
);
6908 static int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
);
6911 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
6913 /* Given a container loaded by load_super_imsm_all,
6914 * extract information about all the arrays into
6916 * If 'subarray' is given, just extract info about that array.
6918 * For each imsm_dev create an mdinfo, fill it in,
6919 * then look for matching devices in super->disks
6920 * and create appropriate device mdinfo.
6922 struct intel_super
*super
= st
->sb
;
6923 struct imsm_super
*mpb
= super
->anchor
;
6924 struct mdinfo
*rest
= NULL
;
6928 int spare_disks
= 0;
6930 /* do not assemble arrays when not all attributes are supported */
6931 if (imsm_check_attributes(mpb
->attributes
) == 0) {
6933 pr_err("Unsupported attributes in IMSM metadata.Arrays activation is blocked.\n");
6936 /* check for bad blocks */
6937 if (imsm_bbm_log_size(super
->anchor
)) {
6938 pr_err("BBM log found in IMSM metadata.Arrays activation is blocked.\n");
6942 /* count spare devices, not used in maps
6944 for (d
= super
->disks
; d
; d
= d
->next
)
6948 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6949 struct imsm_dev
*dev
;
6950 struct imsm_map
*map
;
6951 struct imsm_map
*map2
;
6952 struct mdinfo
*this;
6960 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
6963 dev
= get_imsm_dev(super
, i
);
6964 map
= get_imsm_map(dev
, MAP_0
);
6965 map2
= get_imsm_map(dev
, MAP_1
);
6967 /* do not publish arrays that are in the middle of an
6968 * unsupported migration
6970 if (dev
->vol
.migr_state
&&
6971 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
6972 pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n",
6976 /* do not publish arrays that are not support by controller's
6980 this = xmalloc(sizeof(*this));
6982 super
->current_vol
= i
;
6983 getinfo_super_imsm_volume(st
, this, NULL
);
6986 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
6987 /* mdadm does not support all metadata features- set the bit in all arrays state */
6988 if (!validate_geometry_imsm_orom(super
,
6989 get_imsm_raid_level(map
), /* RAID level */
6990 imsm_level_to_layout(get_imsm_raid_level(map
)),
6991 map
->num_members
, /* raid disks */
6992 &chunk
, join_u32(dev
->size_low
, dev
->size_high
),
6994 pr_err("IMSM RAID geometry validation failed. Array %s activation is blocked.\n",
6996 this->array
.state
|=
6997 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
6998 (1<<MD_SB_BLOCK_VOLUME
);
7002 /* if array has bad blocks, set suitable bit in all arrays state */
7004 this->array
.state
|=
7005 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
7006 (1<<MD_SB_BLOCK_VOLUME
);
7008 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
7009 unsigned long long recovery_start
;
7010 struct mdinfo
*info_d
;
7017 idx
= get_imsm_disk_idx(dev
, slot
, MAP_0
);
7018 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
7019 for (d
= super
->disks
; d
; d
= d
->next
)
7020 if (d
->index
== idx
)
7023 recovery_start
= MaxSector
;
7026 if (d
&& is_failed(&d
->disk
))
7028 if (ord
& IMSM_ORD_REBUILD
)
7032 * if we skip some disks the array will be assmebled degraded;
7033 * reset resync start to avoid a dirty-degraded
7034 * situation when performing the intial sync
7036 * FIXME handle dirty degraded
7038 if ((skip
|| recovery_start
== 0) && !dev
->vol
.dirty
)
7039 this->resync_start
= MaxSector
;
7043 info_d
= xcalloc(1, sizeof(*info_d
));
7044 info_d
->next
= this->devs
;
7045 this->devs
= info_d
;
7047 info_d
->disk
.number
= d
->index
;
7048 info_d
->disk
.major
= d
->major
;
7049 info_d
->disk
.minor
= d
->minor
;
7050 info_d
->disk
.raid_disk
= slot
;
7051 info_d
->recovery_start
= recovery_start
;
7053 if (slot
< map2
->num_members
)
7054 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
7056 this->array
.spare_disks
++;
7058 if (slot
< map
->num_members
)
7059 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
7061 this->array
.spare_disks
++;
7063 if (info_d
->recovery_start
== MaxSector
)
7064 this->array
.working_disks
++;
7066 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
7067 info_d
->data_offset
= pba_of_lba0(map
);
7068 info_d
->component_size
= blocks_per_member(map
);
7070 /* now that the disk list is up-to-date fixup recovery_start */
7071 update_recovery_start(super
, dev
, this);
7072 this->array
.spare_disks
+= spare_disks
;
7075 /* check for reshape */
7076 if (this->reshape_active
== 1)
7077 recover_backup_imsm(st
, this);
7085 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
7086 int failed
, int look_in_map
)
7088 struct imsm_map
*map
;
7090 map
= get_imsm_map(dev
, look_in_map
);
7093 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
7094 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
7096 switch (get_imsm_raid_level(map
)) {
7098 return IMSM_T_STATE_FAILED
;
7101 if (failed
< map
->num_members
)
7102 return IMSM_T_STATE_DEGRADED
;
7104 return IMSM_T_STATE_FAILED
;
7109 * check to see if any mirrors have failed, otherwise we
7110 * are degraded. Even numbered slots are mirrored on
7114 /* gcc -Os complains that this is unused */
7115 int insync
= insync
;
7117 for (i
= 0; i
< map
->num_members
; i
++) {
7118 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_X
);
7119 int idx
= ord_to_idx(ord
);
7120 struct imsm_disk
*disk
;
7122 /* reset the potential in-sync count on even-numbered
7123 * slots. num_copies is always 2 for imsm raid10
7128 disk
= get_imsm_disk(super
, idx
);
7129 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
7132 /* no in-sync disks left in this mirror the
7136 return IMSM_T_STATE_FAILED
;
7139 return IMSM_T_STATE_DEGRADED
;
7143 return IMSM_T_STATE_DEGRADED
;
7145 return IMSM_T_STATE_FAILED
;
7151 return map
->map_state
;
7154 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
7159 struct imsm_disk
*disk
;
7160 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7161 struct imsm_map
*prev
= get_imsm_map(dev
, MAP_1
);
7162 struct imsm_map
*map_for_loop
;
7167 /* at the beginning of migration we set IMSM_ORD_REBUILD on
7168 * disks that are being rebuilt. New failures are recorded to
7169 * map[0]. So we look through all the disks we started with and
7170 * see if any failures are still present, or if any new ones
7174 if (prev
&& (map
->num_members
< prev
->num_members
))
7175 map_for_loop
= prev
;
7177 for (i
= 0; i
< map_for_loop
->num_members
; i
++) {
7179 /* when MAP_X is passed both maps failures are counted
7182 (look_in_map
== MAP_1
|| look_in_map
== MAP_X
) &&
7183 i
< prev
->num_members
) {
7184 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
7185 idx_1
= ord_to_idx(ord
);
7187 disk
= get_imsm_disk(super
, idx_1
);
7188 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
7191 if ((look_in_map
== MAP_0
|| look_in_map
== MAP_X
) &&
7192 i
< map
->num_members
) {
7193 ord
= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
7194 idx
= ord_to_idx(ord
);
7197 disk
= get_imsm_disk(super
, idx
);
7198 if (!disk
|| is_failed(disk
) ||
7199 ord
& IMSM_ORD_REBUILD
)
7209 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
7212 struct intel_super
*super
= c
->sb
;
7213 struct imsm_super
*mpb
= super
->anchor
;
7215 if (atoi(inst
) >= mpb
->num_raid_devs
) {
7216 pr_err("subarry index %d, out of range\n", atoi(inst
));
7220 dprintf("imsm: open_new %s\n", inst
);
7221 a
->info
.container_member
= atoi(inst
);
7225 static int is_resyncing(struct imsm_dev
*dev
)
7227 struct imsm_map
*migr_map
;
7229 if (!dev
->vol
.migr_state
)
7232 if (migr_type(dev
) == MIGR_INIT
||
7233 migr_type(dev
) == MIGR_REPAIR
)
7236 if (migr_type(dev
) == MIGR_GEN_MIGR
)
7239 migr_map
= get_imsm_map(dev
, MAP_1
);
7241 if (migr_map
->map_state
== IMSM_T_STATE_NORMAL
&&
7242 dev
->vol
.migr_type
!= MIGR_GEN_MIGR
)
7248 /* return true if we recorded new information */
7249 static int mark_failure(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
7253 struct imsm_map
*map
;
7254 char buf
[MAX_RAID_SERIAL_LEN
+3];
7255 unsigned int len
, shift
= 0;
7257 /* new failures are always set in map[0] */
7258 map
= get_imsm_map(dev
, MAP_0
);
7260 slot
= get_imsm_disk_slot(map
, idx
);
7264 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
7265 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
7268 memcpy(buf
, disk
->serial
, MAX_RAID_SERIAL_LEN
);
7269 buf
[MAX_RAID_SERIAL_LEN
] = '\000';
7271 if ((len
= strlen(buf
)) >= MAX_RAID_SERIAL_LEN
)
7272 shift
= len
- MAX_RAID_SERIAL_LEN
+ 1;
7273 strncpy((char *)disk
->serial
, &buf
[shift
], MAX_RAID_SERIAL_LEN
);
7275 disk
->status
|= FAILED_DISK
;
7276 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
7277 /* mark failures in second map if second map exists and this disk
7279 * This is valid for migration, initialization and rebuild
7281 if (dev
->vol
.migr_state
) {
7282 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
7283 int slot2
= get_imsm_disk_slot(map2
, idx
);
7285 if (slot2
< map2
->num_members
&& slot2
>= 0)
7286 set_imsm_ord_tbl_ent(map2
, slot2
,
7287 idx
| IMSM_ORD_REBUILD
);
7289 if (map
->failed_disk_num
== 0xff)
7290 map
->failed_disk_num
= slot
;
7294 static void mark_missing(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
7296 mark_failure(dev
, disk
, idx
);
7298 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
7301 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
7302 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
7305 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
7309 if (!super
->missing
)
7312 /* When orom adds replacement for missing disk it does
7313 * not remove entry of missing disk, but just updates map with
7314 * new added disk. So it is not enough just to test if there is
7315 * any missing disk, we have to look if there are any failed disks
7316 * in map to stop migration */
7318 dprintf("imsm: mark missing\n");
7319 /* end process for initialization and rebuild only
7321 if (is_gen_migration(dev
) == 0) {
7325 failed
= imsm_count_failed(super
, dev
, MAP_0
);
7326 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7329 end_migration(dev
, super
, map_state
);
7331 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
7332 mark_missing(dev
, &dl
->disk
, dl
->index
);
7333 super
->updates_pending
++;
7336 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
,
7339 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
7340 unsigned long long array_blocks
;
7341 struct imsm_map
*map
;
7343 if (used_disks
== 0) {
7344 /* when problems occures
7345 * return current array_blocks value
7347 array_blocks
= __le32_to_cpu(dev
->size_high
);
7348 array_blocks
= array_blocks
<< 32;
7349 array_blocks
+= __le32_to_cpu(dev
->size_low
);
7351 return array_blocks
;
7354 /* set array size in metadata
7356 if (new_size
<= 0) {
7357 /* OLCE size change is caused by added disks
7359 map
= get_imsm_map(dev
, MAP_0
);
7360 array_blocks
= blocks_per_member(map
) * used_disks
;
7362 /* Online Volume Size Change
7363 * Using available free space
7365 array_blocks
= new_size
;
7368 /* round array size down to closest MB
7370 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
7371 dev
->size_low
= __cpu_to_le32((__u32
)array_blocks
);
7372 dev
->size_high
= __cpu_to_le32((__u32
)(array_blocks
>> 32));
7374 return array_blocks
;
7377 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
7379 static void imsm_progress_container_reshape(struct intel_super
*super
)
7381 /* if no device has a migr_state, but some device has a
7382 * different number of members than the previous device, start
7383 * changing the number of devices in this device to match
7386 struct imsm_super
*mpb
= super
->anchor
;
7387 int prev_disks
= -1;
7391 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7392 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
7393 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7394 struct imsm_map
*map2
;
7395 int prev_num_members
;
7397 if (dev
->vol
.migr_state
)
7400 if (prev_disks
== -1)
7401 prev_disks
= map
->num_members
;
7402 if (prev_disks
== map
->num_members
)
7405 /* OK, this array needs to enter reshape mode.
7406 * i.e it needs a migr_state
7409 copy_map_size
= sizeof_imsm_map(map
);
7410 prev_num_members
= map
->num_members
;
7411 map
->num_members
= prev_disks
;
7412 dev
->vol
.migr_state
= 1;
7413 dev
->vol
.curr_migr_unit
= 0;
7414 set_migr_type(dev
, MIGR_GEN_MIGR
);
7415 for (i
= prev_num_members
;
7416 i
< map
->num_members
; i
++)
7417 set_imsm_ord_tbl_ent(map
, i
, i
);
7418 map2
= get_imsm_map(dev
, MAP_1
);
7419 /* Copy the current map */
7420 memcpy(map2
, map
, copy_map_size
);
7421 map2
->num_members
= prev_num_members
;
7423 imsm_set_array_size(dev
, -1);
7424 super
->clean_migration_record_by_mdmon
= 1;
7425 super
->updates_pending
++;
7429 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
7430 * states are handled in imsm_set_disk() with one exception, when a
7431 * resync is stopped due to a new failure this routine will set the
7432 * 'degraded' state for the array.
7434 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
7436 int inst
= a
->info
.container_member
;
7437 struct intel_super
*super
= a
->container
->sb
;
7438 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7439 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7440 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
7441 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7442 __u32 blocks_per_unit
;
7444 if (dev
->vol
.migr_state
&&
7445 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
7446 /* array state change is blocked due to reshape action
7448 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
7449 * - finish the reshape (if last_checkpoint is big and action != reshape)
7450 * - update curr_migr_unit
7452 if (a
->curr_action
== reshape
) {
7453 /* still reshaping, maybe update curr_migr_unit */
7454 goto mark_checkpoint
;
7456 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
7457 /* for some reason we aborted the reshape.
7459 * disable automatic metadata rollback
7460 * user action is required to recover process
7463 struct imsm_map
*map2
=
7464 get_imsm_map(dev
, MAP_1
);
7465 dev
->vol
.migr_state
= 0;
7466 set_migr_type(dev
, 0);
7467 dev
->vol
.curr_migr_unit
= 0;
7469 sizeof_imsm_map(map2
));
7470 super
->updates_pending
++;
7473 if (a
->last_checkpoint
>= a
->info
.component_size
) {
7474 unsigned long long array_blocks
;
7478 used_disks
= imsm_num_data_members(dev
, MAP_0
);
7479 if (used_disks
> 0) {
7481 blocks_per_member(map
) *
7483 /* round array size down to closest MB
7485 array_blocks
= (array_blocks
7486 >> SECT_PER_MB_SHIFT
)
7487 << SECT_PER_MB_SHIFT
;
7488 a
->info
.custom_array_size
= array_blocks
;
7489 /* encourage manager to update array
7493 a
->check_reshape
= 1;
7495 /* finalize online capacity expansion/reshape */
7496 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
7498 mdi
->disk
.raid_disk
,
7501 imsm_progress_container_reshape(super
);
7506 /* before we activate this array handle any missing disks */
7507 if (consistent
== 2)
7508 handle_missing(super
, dev
);
7510 if (consistent
== 2 &&
7511 (!is_resync_complete(&a
->info
) ||
7512 map_state
!= IMSM_T_STATE_NORMAL
||
7513 dev
->vol
.migr_state
))
7516 if (is_resync_complete(&a
->info
)) {
7517 /* complete intialization / resync,
7518 * recovery and interrupted recovery is completed in
7521 if (is_resyncing(dev
)) {
7522 dprintf("imsm: mark resync done\n");
7523 end_migration(dev
, super
, map_state
);
7524 super
->updates_pending
++;
7525 a
->last_checkpoint
= 0;
7527 } else if ((!is_resyncing(dev
) && !failed
) &&
7528 (imsm_reshape_blocks_arrays_changes(super
) == 0)) {
7529 /* mark the start of the init process if nothing is failed */
7530 dprintf("imsm: mark resync start\n");
7531 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
7532 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
7534 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
7535 super
->updates_pending
++;
7539 /* skip checkpointing for general migration,
7540 * it is controlled in mdadm
7542 if (is_gen_migration(dev
))
7543 goto skip_mark_checkpoint
;
7545 /* check if we can update curr_migr_unit from resync_start, recovery_start */
7546 blocks_per_unit
= blocks_per_migr_unit(super
, dev
);
7547 if (blocks_per_unit
) {
7551 units
= a
->last_checkpoint
/ blocks_per_unit
;
7554 /* check that we did not overflow 32-bits, and that
7555 * curr_migr_unit needs updating
7557 if (units32
== units
&&
7559 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
7560 dprintf("imsm: mark checkpoint (%u)\n", units32
);
7561 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
7562 super
->updates_pending
++;
7566 skip_mark_checkpoint
:
7567 /* mark dirty / clean */
7568 if (dev
->vol
.dirty
!= !consistent
) {
7569 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
7574 super
->updates_pending
++;
7580 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
7582 int inst
= a
->info
.container_member
;
7583 struct intel_super
*super
= a
->container
->sb
;
7584 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7585 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7586 struct imsm_disk
*disk
;
7588 int recovery_not_finished
= 0;
7593 if (n
> map
->num_members
)
7594 pr_err("imsm: set_disk %d out of range 0..%d\n",
7595 n
, map
->num_members
- 1);
7600 dprintf("imsm: set_disk %d:%x\n", n
, state
);
7602 ord
= get_imsm_ord_tbl_ent(dev
, n
, MAP_0
);
7603 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
7605 /* check for new failures */
7606 if (state
& DS_FAULTY
) {
7607 if (mark_failure(dev
, disk
, ord_to_idx(ord
)))
7608 super
->updates_pending
++;
7611 /* check if in_sync */
7612 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
7613 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
7615 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
7616 super
->updates_pending
++;
7619 failed
= imsm_count_failed(super
, dev
, MAP_0
);
7620 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7622 /* check if recovery complete, newly degraded, or failed */
7623 dprintf("imsm: Detected transition to state ");
7624 switch (map_state
) {
7625 case IMSM_T_STATE_NORMAL
: /* transition to normal state */
7626 dprintf("normal: ");
7627 if (is_rebuilding(dev
)) {
7628 dprintf_cont("while rebuilding");
7629 /* check if recovery is really finished */
7630 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
7631 if (mdi
->recovery_start
!= MaxSector
) {
7632 recovery_not_finished
= 1;
7635 if (recovery_not_finished
) {
7637 dprintf("Rebuild has not finished yet, state not changed");
7638 if (a
->last_checkpoint
< mdi
->recovery_start
) {
7639 a
->last_checkpoint
= mdi
->recovery_start
;
7640 super
->updates_pending
++;
7644 end_migration(dev
, super
, map_state
);
7645 map
= get_imsm_map(dev
, MAP_0
);
7646 map
->failed_disk_num
= ~0;
7647 super
->updates_pending
++;
7648 a
->last_checkpoint
= 0;
7651 if (is_gen_migration(dev
)) {
7652 dprintf_cont("while general migration");
7653 if (a
->last_checkpoint
>= a
->info
.component_size
)
7654 end_migration(dev
, super
, map_state
);
7656 map
->map_state
= map_state
;
7657 map
= get_imsm_map(dev
, MAP_0
);
7658 map
->failed_disk_num
= ~0;
7659 super
->updates_pending
++;
7663 case IMSM_T_STATE_DEGRADED
: /* transition to degraded state */
7664 dprintf_cont("degraded: ");
7665 if (map
->map_state
!= map_state
&& !dev
->vol
.migr_state
) {
7666 dprintf_cont("mark degraded");
7667 map
->map_state
= map_state
;
7668 super
->updates_pending
++;
7669 a
->last_checkpoint
= 0;
7672 if (is_rebuilding(dev
)) {
7673 dprintf_cont("while rebuilding.");
7674 if (map
->map_state
!= map_state
) {
7675 dprintf_cont(" Map state change");
7676 end_migration(dev
, super
, map_state
);
7677 super
->updates_pending
++;
7681 if (is_gen_migration(dev
)) {
7682 dprintf_cont("while general migration");
7683 if (a
->last_checkpoint
>= a
->info
.component_size
)
7684 end_migration(dev
, super
, map_state
);
7686 map
->map_state
= map_state
;
7687 manage_second_map(super
, dev
);
7689 super
->updates_pending
++;
7692 if (is_initializing(dev
)) {
7693 dprintf_cont("while initialization.");
7694 map
->map_state
= map_state
;
7695 super
->updates_pending
++;
7699 case IMSM_T_STATE_FAILED
: /* transition to failed state */
7700 dprintf_cont("failed: ");
7701 if (is_gen_migration(dev
)) {
7702 dprintf_cont("while general migration");
7703 map
->map_state
= map_state
;
7704 super
->updates_pending
++;
7707 if (map
->map_state
!= map_state
) {
7708 dprintf_cont("mark failed");
7709 end_migration(dev
, super
, map_state
);
7710 super
->updates_pending
++;
7711 a
->last_checkpoint
= 0;
7716 dprintf_cont("state %i\n", map_state
);
7721 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
7724 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
7725 unsigned long long dsize
;
7726 unsigned long long sectors
;
7727 unsigned int sector_size
;
7729 get_dev_sector_size(fd
, NULL
, §or_size
);
7730 get_dev_size(fd
, NULL
, &dsize
);
7732 if (mpb_size
> sector_size
) {
7733 /* -1 to account for anchor */
7734 sectors
= mpb_sectors(mpb
, sector_size
) - 1;
7736 /* write the extended mpb to the sectors preceeding the anchor */
7737 if (lseek64(fd
, dsize
- (sector_size
* (2 + sectors
)),
7741 if ((unsigned long long)write(fd
, buf
+ sector_size
,
7742 sector_size
* sectors
) != sector_size
* sectors
)
7746 /* first block is stored on second to last sector of the disk */
7747 if (lseek64(fd
, dsize
- (sector_size
* 2), SEEK_SET
) < 0)
7750 if (write(fd
, buf
, sector_size
) != sector_size
)
7756 static void imsm_sync_metadata(struct supertype
*container
)
7758 struct intel_super
*super
= container
->sb
;
7760 dprintf("sync metadata: %d\n", super
->updates_pending
);
7761 if (!super
->updates_pending
)
7764 write_super_imsm(container
, 0);
7766 super
->updates_pending
= 0;
7769 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
7771 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7772 int i
= get_imsm_disk_idx(dev
, idx
, MAP_X
);
7775 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7779 if (dl
&& is_failed(&dl
->disk
))
7783 dprintf("found %x:%x\n", dl
->major
, dl
->minor
);
7788 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
7789 struct active_array
*a
, int activate_new
,
7790 struct mdinfo
*additional_test_list
)
7792 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7793 int idx
= get_imsm_disk_idx(dev
, slot
, MAP_X
);
7794 struct imsm_super
*mpb
= super
->anchor
;
7795 struct imsm_map
*map
;
7796 unsigned long long pos
;
7801 __u32 array_start
= 0;
7802 __u32 array_end
= 0;
7804 struct mdinfo
*test_list
;
7806 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7807 /* If in this array, skip */
7808 for (d
= a
->info
.devs
; d
; d
= d
->next
)
7809 if (d
->state_fd
>= 0 &&
7810 d
->disk
.major
== dl
->major
&&
7811 d
->disk
.minor
== dl
->minor
) {
7812 dprintf("%x:%x already in array\n",
7813 dl
->major
, dl
->minor
);
7818 test_list
= additional_test_list
;
7820 if (test_list
->disk
.major
== dl
->major
&&
7821 test_list
->disk
.minor
== dl
->minor
) {
7822 dprintf("%x:%x already in additional test list\n",
7823 dl
->major
, dl
->minor
);
7826 test_list
= test_list
->next
;
7831 /* skip in use or failed drives */
7832 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
7834 dprintf("%x:%x status (failed: %d index: %d)\n",
7835 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
7839 /* skip pure spares when we are looking for partially
7840 * assimilated drives
7842 if (dl
->index
== -1 && !activate_new
)
7845 /* Does this unused device have the requisite free space?
7846 * It needs to be able to cover all member volumes
7848 ex
= get_extents(super
, dl
);
7850 dprintf("cannot get extents\n");
7853 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7854 dev
= get_imsm_dev(super
, i
);
7855 map
= get_imsm_map(dev
, MAP_0
);
7857 /* check if this disk is already a member of
7860 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
7866 array_start
= pba_of_lba0(map
);
7867 array_end
= array_start
+
7868 blocks_per_member(map
) - 1;
7871 /* check that we can start at pba_of_lba0 with
7872 * blocks_per_member of space
7874 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
7878 pos
= ex
[j
].start
+ ex
[j
].size
;
7880 } while (ex
[j
-1].size
);
7887 if (i
< mpb
->num_raid_devs
) {
7888 dprintf("%x:%x does not have %u to %u available\n",
7889 dl
->major
, dl
->minor
, array_start
, array_end
);
7899 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
7901 struct imsm_dev
*dev2
;
7902 struct imsm_map
*map
;
7908 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
7910 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
, MAP_0
);
7911 if (state
== IMSM_T_STATE_FAILED
) {
7912 map
= get_imsm_map(dev2
, MAP_0
);
7915 for (slot
= 0; slot
< map
->num_members
; slot
++) {
7917 * Check if failed disks are deleted from intel
7918 * disk list or are marked to be deleted
7920 idx
= get_imsm_disk_idx(dev2
, slot
, MAP_X
);
7921 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
7923 * Do not rebuild the array if failed disks
7924 * from failed sub-array are not removed from
7928 is_failed(&idisk
->disk
) &&
7929 (idisk
->action
!= DISK_REMOVE
))
7937 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
7938 struct metadata_update
**updates
)
7941 * Find a device with unused free space and use it to replace a
7942 * failed/vacant region in an array. We replace failed regions one a
7943 * array at a time. The result is that a new spare disk will be added
7944 * to the first failed array and after the monitor has finished
7945 * propagating failures the remainder will be consumed.
7947 * FIXME add a capability for mdmon to request spares from another
7951 struct intel_super
*super
= a
->container
->sb
;
7952 int inst
= a
->info
.container_member
;
7953 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7954 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7955 int failed
= a
->info
.array
.raid_disks
;
7956 struct mdinfo
*rv
= NULL
;
7959 struct metadata_update
*mu
;
7961 struct imsm_update_activate_spare
*u
;
7966 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
7967 if ((d
->curr_state
& DS_FAULTY
) &&
7969 /* wait for Removal to happen */
7971 if (d
->state_fd
>= 0)
7975 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
7976 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
7978 if (imsm_reshape_blocks_arrays_changes(super
))
7981 /* Cannot activate another spare if rebuild is in progress already
7983 if (is_rebuilding(dev
)) {
7984 dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n");
7988 if (a
->info
.array
.level
== 4)
7989 /* No repair for takeovered array
7990 * imsm doesn't support raid4
7994 if (imsm_check_degraded(super
, dev
, failed
, MAP_0
) !=
7995 IMSM_T_STATE_DEGRADED
)
7998 if (get_imsm_map(dev
, MAP_0
)->map_state
== IMSM_T_STATE_UNINITIALIZED
) {
7999 dprintf("imsm: No spare activation allowed. Volume is not initialized.\n");
8004 * If there are any failed disks check state of the other volume.
8005 * Block rebuild if the another one is failed until failed disks
8006 * are removed from container.
8009 dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n",
8010 MAX_RAID_SERIAL_LEN
, dev
->volume
);
8011 /* check if states of the other volumes allow for rebuild */
8012 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
8014 allowed
= imsm_rebuild_allowed(a
->container
,
8022 /* For each slot, if it is not working, find a spare */
8023 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
8024 for (d
= a
->info
.devs
; d
; d
= d
->next
)
8025 if (d
->disk
.raid_disk
== i
)
8027 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
8028 if (d
&& (d
->state_fd
>= 0))
8032 * OK, this device needs recovery. Try to re-add the
8033 * previous occupant of this slot, if this fails see if
8034 * we can continue the assimilation of a spare that was
8035 * partially assimilated, finally try to activate a new
8038 dl
= imsm_readd(super
, i
, a
);
8040 dl
= imsm_add_spare(super
, i
, a
, 0, rv
);
8042 dl
= imsm_add_spare(super
, i
, a
, 1, rv
);
8046 /* found a usable disk with enough space */
8047 di
= xcalloc(1, sizeof(*di
));
8049 /* dl->index will be -1 in the case we are activating a
8050 * pristine spare. imsm_process_update() will create a
8051 * new index in this case. Once a disk is found to be
8052 * failed in all member arrays it is kicked from the
8055 di
->disk
.number
= dl
->index
;
8057 /* (ab)use di->devs to store a pointer to the device
8060 di
->devs
= (struct mdinfo
*) dl
;
8062 di
->disk
.raid_disk
= i
;
8063 di
->disk
.major
= dl
->major
;
8064 di
->disk
.minor
= dl
->minor
;
8066 di
->recovery_start
= 0;
8067 di
->data_offset
= pba_of_lba0(map
);
8068 di
->component_size
= a
->info
.component_size
;
8069 di
->container_member
= inst
;
8070 super
->random
= random32();
8074 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
8075 i
, di
->data_offset
);
8079 /* No spares found */
8081 /* Now 'rv' has a list of devices to return.
8082 * Create a metadata_update record to update the
8083 * disk_ord_tbl for the array
8085 mu
= xmalloc(sizeof(*mu
));
8086 mu
->buf
= xcalloc(num_spares
,
8087 sizeof(struct imsm_update_activate_spare
));
8089 mu
->space_list
= NULL
;
8090 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
8091 mu
->next
= *updates
;
8092 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
8094 for (di
= rv
; di
; di
= di
->next
) {
8095 u
->type
= update_activate_spare
;
8096 u
->dl
= (struct dl
*) di
->devs
;
8098 u
->slot
= di
->disk
.raid_disk
;
8109 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
8111 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
8112 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8113 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, MAP_0
);
8114 struct disk_info
*inf
= get_disk_info(u
);
8115 struct imsm_disk
*disk
;
8119 for (i
= 0; i
< map
->num_members
; i
++) {
8120 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, MAP_X
));
8121 for (j
= 0; j
< new_map
->num_members
; j
++)
8122 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
8129 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
8133 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
8134 if (dl
->major
== major
&& dl
->minor
== minor
)
8139 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
8145 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
8146 if (dl
->major
== major
&& dl
->minor
== minor
) {
8149 prev
->next
= dl
->next
;
8151 super
->disks
= dl
->next
;
8153 __free_imsm_disk(dl
);
8154 dprintf("removed %x:%x\n", major
, minor
);
8162 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
8164 static int add_remove_disk_update(struct intel_super
*super
)
8166 int check_degraded
= 0;
8169 /* add/remove some spares to/from the metadata/contrainer */
8170 while (super
->disk_mgmt_list
) {
8171 struct dl
*disk_cfg
;
8173 disk_cfg
= super
->disk_mgmt_list
;
8174 super
->disk_mgmt_list
= disk_cfg
->next
;
8175 disk_cfg
->next
= NULL
;
8177 if (disk_cfg
->action
== DISK_ADD
) {
8178 disk_cfg
->next
= super
->disks
;
8179 super
->disks
= disk_cfg
;
8181 dprintf("added %x:%x\n",
8182 disk_cfg
->major
, disk_cfg
->minor
);
8183 } else if (disk_cfg
->action
== DISK_REMOVE
) {
8184 dprintf("Disk remove action processed: %x.%x\n",
8185 disk_cfg
->major
, disk_cfg
->minor
);
8186 disk
= get_disk_super(super
,
8190 /* store action status */
8191 disk
->action
= DISK_REMOVE
;
8192 /* remove spare disks only */
8193 if (disk
->index
== -1) {
8194 remove_disk_super(super
,
8199 /* release allocate disk structure */
8200 __free_imsm_disk(disk_cfg
);
8203 return check_degraded
;
8206 static int apply_reshape_migration_update(struct imsm_update_reshape_migration
*u
,
8207 struct intel_super
*super
,
8210 struct intel_dev
*id
;
8211 void **tofree
= NULL
;
8214 dprintf("(enter)\n");
8215 if (u
->subdev
< 0 || u
->subdev
> 1) {
8216 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
8219 if (space_list
== NULL
|| *space_list
== NULL
) {
8220 dprintf("imsm: Error: Memory is not allocated\n");
8224 for (id
= super
->devlist
; id
; id
= id
->next
) {
8225 if (id
->index
== (unsigned)u
->subdev
) {
8226 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
8227 struct imsm_map
*map
;
8228 struct imsm_dev
*new_dev
=
8229 (struct imsm_dev
*)*space_list
;
8230 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
8232 struct dl
*new_disk
;
8234 if (new_dev
== NULL
)
8236 *space_list
= **space_list
;
8237 memcpy(new_dev
, dev
, sizeof_imsm_dev(dev
, 0));
8238 map
= get_imsm_map(new_dev
, MAP_0
);
8240 dprintf("imsm: Error: migration in progress");
8244 to_state
= map
->map_state
;
8245 if ((u
->new_level
== 5) && (map
->raid_level
== 0)) {
8247 /* this should not happen */
8248 if (u
->new_disks
[0] < 0) {
8249 map
->failed_disk_num
=
8250 map
->num_members
- 1;
8251 to_state
= IMSM_T_STATE_DEGRADED
;
8253 to_state
= IMSM_T_STATE_NORMAL
;
8255 migrate(new_dev
, super
, to_state
, MIGR_GEN_MIGR
);
8256 if (u
->new_level
> -1)
8257 map
->raid_level
= u
->new_level
;
8258 migr_map
= get_imsm_map(new_dev
, MAP_1
);
8259 if ((u
->new_level
== 5) &&
8260 (migr_map
->raid_level
== 0)) {
8261 int ord
= map
->num_members
- 1;
8262 migr_map
->num_members
--;
8263 if (u
->new_disks
[0] < 0)
8264 ord
|= IMSM_ORD_REBUILD
;
8265 set_imsm_ord_tbl_ent(map
,
8266 map
->num_members
- 1,
8270 tofree
= (void **)dev
;
8272 /* update chunk size
8274 if (u
->new_chunksize
> 0)
8275 map
->blocks_per_strip
=
8276 __cpu_to_le16(u
->new_chunksize
* 2);
8280 if (u
->new_level
!= 5 || migr_map
->raid_level
!= 0 ||
8281 migr_map
->raid_level
== map
->raid_level
)
8284 if (u
->new_disks
[0] >= 0) {
8287 new_disk
= get_disk_super(super
,
8288 major(u
->new_disks
[0]),
8289 minor(u
->new_disks
[0]));
8290 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
8291 major(u
->new_disks
[0]),
8292 minor(u
->new_disks
[0]),
8293 new_disk
, new_disk
->index
);
8294 if (new_disk
== NULL
)
8295 goto error_disk_add
;
8297 new_disk
->index
= map
->num_members
- 1;
8298 /* slot to fill in autolayout
8300 new_disk
->raiddisk
= new_disk
->index
;
8301 new_disk
->disk
.status
|= CONFIGURED_DISK
;
8302 new_disk
->disk
.status
&= ~SPARE_DISK
;
8304 goto error_disk_add
;
8307 *tofree
= *space_list
;
8308 /* calculate new size
8310 imsm_set_array_size(new_dev
, -1);
8317 *space_list
= tofree
;
8321 dprintf("Error: imsm: Cannot find disk.\n");
8325 static int apply_size_change_update(struct imsm_update_size_change
*u
,
8326 struct intel_super
*super
)
8328 struct intel_dev
*id
;
8331 dprintf("(enter)\n");
8332 if (u
->subdev
< 0 || u
->subdev
> 1) {
8333 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
8337 for (id
= super
->devlist
; id
; id
= id
->next
) {
8338 if (id
->index
== (unsigned)u
->subdev
) {
8339 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
8340 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8341 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
8342 unsigned long long blocks_per_member
;
8344 /* calculate new size
8346 blocks_per_member
= u
->new_size
/ used_disks
;
8347 dprintf("(size: %llu, blocks per member: %llu)\n",
8348 u
->new_size
, blocks_per_member
);
8349 set_blocks_per_member(map
, blocks_per_member
);
8350 imsm_set_array_size(dev
, u
->new_size
);
8360 static int apply_update_activate_spare(struct imsm_update_activate_spare
*u
,
8361 struct intel_super
*super
,
8362 struct active_array
*active_array
)
8364 struct imsm_super
*mpb
= super
->anchor
;
8365 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
8366 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8367 struct imsm_map
*migr_map
;
8368 struct active_array
*a
;
8369 struct imsm_disk
*disk
;
8376 int second_map_created
= 0;
8378 for (; u
; u
= u
->next
) {
8379 victim
= get_imsm_disk_idx(dev
, u
->slot
, MAP_X
);
8384 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
8389 pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n",
8394 /* count failures (excluding rebuilds and the victim)
8395 * to determine map[0] state
8398 for (i
= 0; i
< map
->num_members
; i
++) {
8401 disk
= get_imsm_disk(super
,
8402 get_imsm_disk_idx(dev
, i
, MAP_X
));
8403 if (!disk
|| is_failed(disk
))
8407 /* adding a pristine spare, assign a new index */
8408 if (dl
->index
< 0) {
8409 dl
->index
= super
->anchor
->num_disks
;
8410 super
->anchor
->num_disks
++;
8413 disk
->status
|= CONFIGURED_DISK
;
8414 disk
->status
&= ~SPARE_DISK
;
8417 to_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8418 if (!second_map_created
) {
8419 second_map_created
= 1;
8420 map
->map_state
= IMSM_T_STATE_DEGRADED
;
8421 migrate(dev
, super
, to_state
, MIGR_REBUILD
);
8423 map
->map_state
= to_state
;
8424 migr_map
= get_imsm_map(dev
, MAP_1
);
8425 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
8426 set_imsm_ord_tbl_ent(migr_map
, u
->slot
,
8427 dl
->index
| IMSM_ORD_REBUILD
);
8429 /* update the family_num to mark a new container
8430 * generation, being careful to record the existing
8431 * family_num in orig_family_num to clean up after
8432 * earlier mdadm versions that neglected to set it.
8434 if (mpb
->orig_family_num
== 0)
8435 mpb
->orig_family_num
= mpb
->family_num
;
8436 mpb
->family_num
+= super
->random
;
8438 /* count arrays using the victim in the metadata */
8440 for (a
= active_array
; a
; a
= a
->next
) {
8441 dev
= get_imsm_dev(super
, a
->info
.container_member
);
8442 map
= get_imsm_map(dev
, MAP_0
);
8444 if (get_imsm_disk_slot(map
, victim
) >= 0)
8448 /* delete the victim if it is no longer being
8454 /* We know that 'manager' isn't touching anything,
8455 * so it is safe to delete
8457 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
8458 if ((*dlp
)->index
== victim
)
8461 /* victim may be on the missing list */
8463 for (dlp
= &super
->missing
; *dlp
;
8464 dlp
= &(*dlp
)->next
)
8465 if ((*dlp
)->index
== victim
)
8467 imsm_delete(super
, dlp
, victim
);
8474 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
8475 struct intel_super
*super
,
8478 struct dl
*new_disk
;
8479 struct intel_dev
*id
;
8481 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
8482 int disk_count
= u
->old_raid_disks
;
8483 void **tofree
= NULL
;
8484 int devices_to_reshape
= 1;
8485 struct imsm_super
*mpb
= super
->anchor
;
8487 unsigned int dev_id
;
8489 dprintf("(enter)\n");
8491 /* enable spares to use in array */
8492 for (i
= 0; i
< delta_disks
; i
++) {
8493 new_disk
= get_disk_super(super
,
8494 major(u
->new_disks
[i
]),
8495 minor(u
->new_disks
[i
]));
8496 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
8497 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
8498 new_disk
, new_disk
->index
);
8499 if (new_disk
== NULL
||
8500 (new_disk
->index
>= 0 &&
8501 new_disk
->index
< u
->old_raid_disks
))
8502 goto update_reshape_exit
;
8503 new_disk
->index
= disk_count
++;
8504 /* slot to fill in autolayout
8506 new_disk
->raiddisk
= new_disk
->index
;
8507 new_disk
->disk
.status
|=
8509 new_disk
->disk
.status
&= ~SPARE_DISK
;
8512 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
8513 mpb
->num_raid_devs
);
8514 /* manage changes in volume
8516 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
8517 void **sp
= *space_list
;
8518 struct imsm_dev
*newdev
;
8519 struct imsm_map
*newmap
, *oldmap
;
8521 for (id
= super
->devlist
; id
; id
= id
->next
) {
8522 if (id
->index
== dev_id
)
8531 /* Copy the dev, but not (all of) the map */
8532 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
8533 oldmap
= get_imsm_map(id
->dev
, MAP_0
);
8534 newmap
= get_imsm_map(newdev
, MAP_0
);
8535 /* Copy the current map */
8536 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8537 /* update one device only
8539 if (devices_to_reshape
) {
8540 dprintf("imsm: modifying subdev: %i\n",
8542 devices_to_reshape
--;
8543 newdev
->vol
.migr_state
= 1;
8544 newdev
->vol
.curr_migr_unit
= 0;
8545 set_migr_type(newdev
, MIGR_GEN_MIGR
);
8546 newmap
->num_members
= u
->new_raid_disks
;
8547 for (i
= 0; i
< delta_disks
; i
++) {
8548 set_imsm_ord_tbl_ent(newmap
,
8549 u
->old_raid_disks
+ i
,
8550 u
->old_raid_disks
+ i
);
8552 /* New map is correct, now need to save old map
8554 newmap
= get_imsm_map(newdev
, MAP_1
);
8555 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8557 imsm_set_array_size(newdev
, -1);
8560 sp
= (void **)id
->dev
;
8565 /* Clear migration record */
8566 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
8569 *space_list
= tofree
;
8572 update_reshape_exit
:
8577 static int apply_takeover_update(struct imsm_update_takeover
*u
,
8578 struct intel_super
*super
,
8581 struct imsm_dev
*dev
= NULL
;
8582 struct intel_dev
*dv
;
8583 struct imsm_dev
*dev_new
;
8584 struct imsm_map
*map
;
8588 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
8589 if (dv
->index
== (unsigned int)u
->subarray
) {
8597 map
= get_imsm_map(dev
, MAP_0
);
8599 if (u
->direction
== R10_TO_R0
) {
8600 /* Number of failed disks must be half of initial disk number */
8601 if (imsm_count_failed(super
, dev
, MAP_0
) !=
8602 (map
->num_members
/ 2))
8605 /* iterate through devices to mark removed disks as spare */
8606 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8607 if (dm
->disk
.status
& FAILED_DISK
) {
8608 int idx
= dm
->index
;
8609 /* update indexes on the disk list */
8610 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
8611 the index values will end up being correct.... NB */
8612 for (du
= super
->disks
; du
; du
= du
->next
)
8613 if (du
->index
> idx
)
8615 /* mark as spare disk */
8620 map
->num_members
= map
->num_members
/ 2;
8621 map
->map_state
= IMSM_T_STATE_NORMAL
;
8622 map
->num_domains
= 1;
8623 map
->raid_level
= 0;
8624 map
->failed_disk_num
= -1;
8627 if (u
->direction
== R0_TO_R10
) {
8629 /* update slots in current disk list */
8630 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8634 /* create new *missing* disks */
8635 for (i
= 0; i
< map
->num_members
; i
++) {
8636 space
= *space_list
;
8639 *space_list
= *space
;
8641 memcpy(du
, super
->disks
, sizeof(*du
));
8645 du
->index
= (i
* 2) + 1;
8646 sprintf((char *)du
->disk
.serial
,
8647 " MISSING_%d", du
->index
);
8648 sprintf((char *)du
->serial
,
8649 "MISSING_%d", du
->index
);
8650 du
->next
= super
->missing
;
8651 super
->missing
= du
;
8653 /* create new dev and map */
8654 space
= *space_list
;
8657 *space_list
= *space
;
8658 dev_new
= (void *)space
;
8659 memcpy(dev_new
, dev
, sizeof(*dev
));
8660 /* update new map */
8661 map
= get_imsm_map(dev_new
, MAP_0
);
8662 map
->num_members
= map
->num_members
* 2;
8663 map
->map_state
= IMSM_T_STATE_DEGRADED
;
8664 map
->num_domains
= 2;
8665 map
->raid_level
= 1;
8666 /* replace dev<->dev_new */
8669 /* update disk order table */
8670 for (du
= super
->disks
; du
; du
= du
->next
)
8672 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8673 for (du
= super
->missing
; du
; du
= du
->next
)
8674 if (du
->index
>= 0) {
8675 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8676 mark_missing(dv
->dev
, &du
->disk
, du
->index
);
8682 static void imsm_process_update(struct supertype
*st
,
8683 struct metadata_update
*update
)
8686 * crack open the metadata_update envelope to find the update record
8687 * update can be one of:
8688 * update_reshape_container_disks - all the arrays in the container
8689 * are being reshaped to have more devices. We need to mark
8690 * the arrays for general migration and convert selected spares
8691 * into active devices.
8692 * update_activate_spare - a spare device has replaced a failed
8693 * device in an array, update the disk_ord_tbl. If this disk is
8694 * present in all member arrays then also clear the SPARE_DISK
8696 * update_create_array
8698 * update_rename_array
8699 * update_add_remove_disk
8701 struct intel_super
*super
= st
->sb
;
8702 struct imsm_super
*mpb
;
8703 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
8705 /* update requires a larger buf but the allocation failed */
8706 if (super
->next_len
&& !super
->next_buf
) {
8707 super
->next_len
= 0;
8711 if (super
->next_buf
) {
8712 memcpy(super
->next_buf
, super
->buf
, super
->len
);
8714 super
->len
= super
->next_len
;
8715 super
->buf
= super
->next_buf
;
8717 super
->next_len
= 0;
8718 super
->next_buf
= NULL
;
8721 mpb
= super
->anchor
;
8724 case update_general_migration_checkpoint
: {
8725 struct intel_dev
*id
;
8726 struct imsm_update_general_migration_checkpoint
*u
=
8727 (void *)update
->buf
;
8729 dprintf("called for update_general_migration_checkpoint\n");
8731 /* find device under general migration */
8732 for (id
= super
->devlist
; id
; id
= id
->next
) {
8733 if (is_gen_migration(id
->dev
)) {
8734 id
->dev
->vol
.curr_migr_unit
=
8735 __cpu_to_le32(u
->curr_migr_unit
);
8736 super
->updates_pending
++;
8741 case update_takeover
: {
8742 struct imsm_update_takeover
*u
= (void *)update
->buf
;
8743 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
8744 imsm_update_version_info(super
);
8745 super
->updates_pending
++;
8750 case update_reshape_container_disks
: {
8751 struct imsm_update_reshape
*u
= (void *)update
->buf
;
8752 if (apply_reshape_container_disks_update(
8753 u
, super
, &update
->space_list
))
8754 super
->updates_pending
++;
8757 case update_reshape_migration
: {
8758 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
8759 if (apply_reshape_migration_update(
8760 u
, super
, &update
->space_list
))
8761 super
->updates_pending
++;
8764 case update_size_change
: {
8765 struct imsm_update_size_change
*u
= (void *)update
->buf
;
8766 if (apply_size_change_update(u
, super
))
8767 super
->updates_pending
++;
8770 case update_activate_spare
: {
8771 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
8772 if (apply_update_activate_spare(u
, super
, st
->arrays
))
8773 super
->updates_pending
++;
8776 case update_create_array
: {
8777 /* someone wants to create a new array, we need to be aware of
8778 * a few races/collisions:
8779 * 1/ 'Create' called by two separate instances of mdadm
8780 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
8781 * devices that have since been assimilated via
8783 * In the event this update can not be carried out mdadm will
8784 * (FIX ME) notice that its update did not take hold.
8786 struct imsm_update_create_array
*u
= (void *) update
->buf
;
8787 struct intel_dev
*dv
;
8788 struct imsm_dev
*dev
;
8789 struct imsm_map
*map
, *new_map
;
8790 unsigned long long start
, end
;
8791 unsigned long long new_start
, new_end
;
8793 struct disk_info
*inf
;
8796 /* handle racing creates: first come first serve */
8797 if (u
->dev_idx
< mpb
->num_raid_devs
) {
8798 dprintf("subarray %d already defined\n", u
->dev_idx
);
8802 /* check update is next in sequence */
8803 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
8804 dprintf("can not create array %d expected index %d\n",
8805 u
->dev_idx
, mpb
->num_raid_devs
);
8809 new_map
= get_imsm_map(&u
->dev
, MAP_0
);
8810 new_start
= pba_of_lba0(new_map
);
8811 new_end
= new_start
+ blocks_per_member(new_map
);
8812 inf
= get_disk_info(u
);
8814 /* handle activate_spare versus create race:
8815 * check to make sure that overlapping arrays do not include
8818 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8819 dev
= get_imsm_dev(super
, i
);
8820 map
= get_imsm_map(dev
, MAP_0
);
8821 start
= pba_of_lba0(map
);
8822 end
= start
+ blocks_per_member(map
);
8823 if ((new_start
>= start
&& new_start
<= end
) ||
8824 (start
>= new_start
&& start
<= new_end
))
8829 if (disks_overlap(super
, i
, u
)) {
8830 dprintf("arrays overlap\n");
8835 /* check that prepare update was successful */
8836 if (!update
->space
) {
8837 dprintf("prepare update failed\n");
8841 /* check that all disks are still active before committing
8842 * changes. FIXME: could we instead handle this by creating a
8843 * degraded array? That's probably not what the user expects,
8844 * so better to drop this update on the floor.
8846 for (i
= 0; i
< new_map
->num_members
; i
++) {
8847 dl
= serial_to_dl(inf
[i
].serial
, super
);
8849 dprintf("disk disappeared\n");
8854 super
->updates_pending
++;
8856 /* convert spares to members and fixup ord_tbl */
8857 for (i
= 0; i
< new_map
->num_members
; i
++) {
8858 dl
= serial_to_dl(inf
[i
].serial
, super
);
8859 if (dl
->index
== -1) {
8860 dl
->index
= mpb
->num_disks
;
8862 dl
->disk
.status
|= CONFIGURED_DISK
;
8863 dl
->disk
.status
&= ~SPARE_DISK
;
8865 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
8870 update
->space
= NULL
;
8871 imsm_copy_dev(dev
, &u
->dev
);
8872 dv
->index
= u
->dev_idx
;
8873 dv
->next
= super
->devlist
;
8874 super
->devlist
= dv
;
8875 mpb
->num_raid_devs
++;
8877 imsm_update_version_info(super
);
8880 /* mdmon knows how to release update->space, but not
8881 * ((struct intel_dev *) update->space)->dev
8883 if (update
->space
) {
8889 case update_kill_array
: {
8890 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
8891 int victim
= u
->dev_idx
;
8892 struct active_array
*a
;
8893 struct intel_dev
**dp
;
8894 struct imsm_dev
*dev
;
8896 /* sanity check that we are not affecting the uuid of
8897 * active arrays, or deleting an active array
8899 * FIXME when immutable ids are available, but note that
8900 * we'll also need to fixup the invalidated/active
8901 * subarray indexes in mdstat
8903 for (a
= st
->arrays
; a
; a
= a
->next
)
8904 if (a
->info
.container_member
>= victim
)
8906 /* by definition if mdmon is running at least one array
8907 * is active in the container, so checking
8908 * mpb->num_raid_devs is just extra paranoia
8910 dev
= get_imsm_dev(super
, victim
);
8911 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
8912 dprintf("failed to delete subarray-%d\n", victim
);
8916 for (dp
= &super
->devlist
; *dp
;)
8917 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
8920 if ((*dp
)->index
> (unsigned)victim
)
8924 mpb
->num_raid_devs
--;
8925 super
->updates_pending
++;
8928 case update_rename_array
: {
8929 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
8930 char name
[MAX_RAID_SERIAL_LEN
+1];
8931 int target
= u
->dev_idx
;
8932 struct active_array
*a
;
8933 struct imsm_dev
*dev
;
8935 /* sanity check that we are not affecting the uuid of
8938 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
8939 name
[MAX_RAID_SERIAL_LEN
] = '\0';
8940 for (a
= st
->arrays
; a
; a
= a
->next
)
8941 if (a
->info
.container_member
== target
)
8943 dev
= get_imsm_dev(super
, u
->dev_idx
);
8944 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
8945 dprintf("failed to rename subarray-%d\n", target
);
8949 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
8950 super
->updates_pending
++;
8953 case update_add_remove_disk
: {
8954 /* we may be able to repair some arrays if disks are
8955 * being added, check the status of add_remove_disk
8956 * if discs has been added.
8958 if (add_remove_disk_update(super
)) {
8959 struct active_array
*a
;
8961 super
->updates_pending
++;
8962 for (a
= st
->arrays
; a
; a
= a
->next
)
8963 a
->check_degraded
= 1;
8968 pr_err("error: unsuported process update type:(type: %d)\n", type
);
8972 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
);
8974 static int imsm_prepare_update(struct supertype
*st
,
8975 struct metadata_update
*update
)
8978 * Allocate space to hold new disk entries, raid-device entries or a new
8979 * mpb if necessary. The manager synchronously waits for updates to
8980 * complete in the monitor, so new mpb buffers allocated here can be
8981 * integrated by the monitor thread without worrying about live pointers
8982 * in the manager thread.
8984 enum imsm_update_type type
;
8985 struct intel_super
*super
= st
->sb
;
8986 unsigned int sector_size
= super
->sector_size
;
8987 struct imsm_super
*mpb
= super
->anchor
;
8991 if (update
->len
< (int)sizeof(type
))
8994 type
= *(enum imsm_update_type
*) update
->buf
;
8997 case update_general_migration_checkpoint
:
8998 if (update
->len
< (int)sizeof(struct imsm_update_general_migration_checkpoint
))
9000 dprintf("called for update_general_migration_checkpoint\n");
9002 case update_takeover
: {
9003 struct imsm_update_takeover
*u
= (void *)update
->buf
;
9004 if (update
->len
< (int)sizeof(*u
))
9006 if (u
->direction
== R0_TO_R10
) {
9007 void **tail
= (void **)&update
->space_list
;
9008 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
9009 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9010 int num_members
= map
->num_members
;
9013 /* allocate memory for added disks */
9014 for (i
= 0; i
< num_members
; i
++) {
9015 size
= sizeof(struct dl
);
9016 space
= xmalloc(size
);
9021 /* allocate memory for new device */
9022 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
9023 (num_members
* sizeof(__u32
));
9024 space
= xmalloc(size
);
9028 len
= disks_to_mpb_size(num_members
* 2);
9033 case update_reshape_container_disks
: {
9034 /* Every raid device in the container is about to
9035 * gain some more devices, and we will enter a
9037 * So each 'imsm_map' will be bigger, and the imsm_vol
9038 * will now hold 2 of them.
9039 * Thus we need new 'struct imsm_dev' allocations sized
9040 * as sizeof_imsm_dev but with more devices in both maps.
9042 struct imsm_update_reshape
*u
= (void *)update
->buf
;
9043 struct intel_dev
*dl
;
9044 void **space_tail
= (void**)&update
->space_list
;
9046 if (update
->len
< (int)sizeof(*u
))
9049 dprintf("for update_reshape\n");
9051 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
9052 int size
= sizeof_imsm_dev(dl
->dev
, 1);
9054 if (u
->new_raid_disks
> u
->old_raid_disks
)
9055 size
+= sizeof(__u32
)*2*
9056 (u
->new_raid_disks
- u
->old_raid_disks
);
9063 len
= disks_to_mpb_size(u
->new_raid_disks
);
9064 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
9067 case update_reshape_migration
: {
9068 /* for migration level 0->5 we need to add disks
9069 * so the same as for container operation we will copy
9070 * device to the bigger location.
9071 * in memory prepared device and new disk area are prepared
9072 * for usage in process update
9074 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
9075 struct intel_dev
*id
;
9076 void **space_tail
= (void **)&update
->space_list
;
9079 int current_level
= -1;
9081 if (update
->len
< (int)sizeof(*u
))
9084 dprintf("for update_reshape\n");
9086 /* add space for bigger array in update
9088 for (id
= super
->devlist
; id
; id
= id
->next
) {
9089 if (id
->index
== (unsigned)u
->subdev
) {
9090 size
= sizeof_imsm_dev(id
->dev
, 1);
9091 if (u
->new_raid_disks
> u
->old_raid_disks
)
9092 size
+= sizeof(__u32
)*2*
9093 (u
->new_raid_disks
- u
->old_raid_disks
);
9101 if (update
->space_list
== NULL
)
9104 /* add space for disk in update
9106 size
= sizeof(struct dl
);
9112 /* add spare device to update
9114 for (id
= super
->devlist
; id
; id
= id
->next
)
9115 if (id
->index
== (unsigned)u
->subdev
) {
9116 struct imsm_dev
*dev
;
9117 struct imsm_map
*map
;
9119 dev
= get_imsm_dev(super
, u
->subdev
);
9120 map
= get_imsm_map(dev
, MAP_0
);
9121 current_level
= map
->raid_level
;
9124 if (u
->new_level
== 5 && u
->new_level
!= current_level
) {
9125 struct mdinfo
*spares
;
9127 spares
= get_spares_for_grow(st
);
9135 makedev(dev
->disk
.major
,
9137 dl
= get_disk_super(super
,
9140 dl
->index
= u
->old_raid_disks
;
9146 len
= disks_to_mpb_size(u
->new_raid_disks
);
9147 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
9150 case update_size_change
: {
9151 if (update
->len
< (int)sizeof(struct imsm_update_size_change
))
9155 case update_activate_spare
: {
9156 if (update
->len
< (int)sizeof(struct imsm_update_activate_spare
))
9160 case update_create_array
: {
9161 struct imsm_update_create_array
*u
= (void *) update
->buf
;
9162 struct intel_dev
*dv
;
9163 struct imsm_dev
*dev
= &u
->dev
;
9164 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9166 struct disk_info
*inf
;
9170 if (update
->len
< (int)sizeof(*u
))
9173 inf
= get_disk_info(u
);
9174 len
= sizeof_imsm_dev(dev
, 1);
9175 /* allocate a new super->devlist entry */
9176 dv
= xmalloc(sizeof(*dv
));
9177 dv
->dev
= xmalloc(len
);
9180 /* count how many spares will be converted to members */
9181 for (i
= 0; i
< map
->num_members
; i
++) {
9182 dl
= serial_to_dl(inf
[i
].serial
, super
);
9184 /* hmm maybe it failed?, nothing we can do about
9189 if (count_memberships(dl
, super
) == 0)
9192 len
+= activate
* sizeof(struct imsm_disk
);
9195 case update_kill_array
: {
9196 if (update
->len
< (int)sizeof(struct imsm_update_kill_array
))
9200 case update_rename_array
: {
9201 if (update
->len
< (int)sizeof(struct imsm_update_rename_array
))
9205 case update_add_remove_disk
:
9206 /* no update->len needed */
9212 /* check if we need a larger metadata buffer */
9213 if (super
->next_buf
)
9214 buf_len
= super
->next_len
;
9216 buf_len
= super
->len
;
9218 if (__le32_to_cpu(mpb
->mpb_size
) + len
> buf_len
) {
9219 /* ok we need a larger buf than what is currently allocated
9220 * if this allocation fails process_update will notice that
9221 * ->next_len is set and ->next_buf is NULL
9223 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + len
,
9225 if (super
->next_buf
)
9226 free(super
->next_buf
);
9228 super
->next_len
= buf_len
;
9229 if (posix_memalign(&super
->next_buf
, sector_size
, buf_len
) == 0)
9230 memset(super
->next_buf
, 0, buf_len
);
9232 super
->next_buf
= NULL
;
9237 /* must be called while manager is quiesced */
9238 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
9240 struct imsm_super
*mpb
= super
->anchor
;
9242 struct imsm_dev
*dev
;
9243 struct imsm_map
*map
;
9244 int i
, j
, num_members
;
9247 dprintf("deleting device[%d] from imsm_super\n", index
);
9249 /* shift all indexes down one */
9250 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
9251 if (iter
->index
> (int)index
)
9253 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
9254 if (iter
->index
> (int)index
)
9257 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
9258 dev
= get_imsm_dev(super
, i
);
9259 map
= get_imsm_map(dev
, MAP_0
);
9260 num_members
= map
->num_members
;
9261 for (j
= 0; j
< num_members
; j
++) {
9262 /* update ord entries being careful not to propagate
9263 * ord-flags to the first map
9265 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_X
);
9267 if (ord_to_idx(ord
) <= index
)
9270 map
= get_imsm_map(dev
, MAP_0
);
9271 set_imsm_ord_tbl_ent(map
, j
, ord_to_idx(ord
- 1));
9272 map
= get_imsm_map(dev
, MAP_1
);
9274 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
9279 super
->updates_pending
++;
9281 struct dl
*dl
= *dlp
;
9283 *dlp
= (*dlp
)->next
;
9284 __free_imsm_disk(dl
);
9287 #endif /* MDASSEMBLE */
9289 static void close_targets(int *targets
, int new_disks
)
9296 for (i
= 0; i
< new_disks
; i
++) {
9297 if (targets
[i
] >= 0) {
9304 static int imsm_get_allowed_degradation(int level
, int raid_disks
,
9305 struct intel_super
*super
,
9306 struct imsm_dev
*dev
)
9312 struct imsm_map
*map
;
9315 ret_val
= raid_disks
/2;
9316 /* check map if all disks pairs not failed
9319 map
= get_imsm_map(dev
, MAP_0
);
9320 for (i
= 0; i
< ret_val
; i
++) {
9321 int degradation
= 0;
9322 if (get_imsm_disk(super
, i
) == NULL
)
9324 if (get_imsm_disk(super
, i
+ 1) == NULL
)
9326 if (degradation
== 2)
9329 map
= get_imsm_map(dev
, MAP_1
);
9330 /* if there is no second map
9331 * result can be returned
9335 /* check degradation in second map
9337 for (i
= 0; i
< ret_val
; i
++) {
9338 int degradation
= 0;
9339 if (get_imsm_disk(super
, i
) == NULL
)
9341 if (get_imsm_disk(super
, i
+ 1) == NULL
)
9343 if (degradation
== 2)
9357 /*******************************************************************************
9358 * Function: open_backup_targets
9359 * Description: Function opens file descriptors for all devices given in
9362 * info : general array info
9363 * raid_disks : number of disks
9364 * raid_fds : table of device's file descriptors
9365 * super : intel super for raid10 degradation check
9366 * dev : intel device for raid10 degradation check
9370 ******************************************************************************/
9371 int open_backup_targets(struct mdinfo
*info
, int raid_disks
, int *raid_fds
,
9372 struct intel_super
*super
, struct imsm_dev
*dev
)
9378 for (i
= 0; i
< raid_disks
; i
++)
9381 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
9384 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
)) {
9385 dprintf("disk is faulty!!\n");
9389 if (sd
->disk
.raid_disk
>= raid_disks
|| sd
->disk
.raid_disk
< 0)
9392 dn
= map_dev(sd
->disk
.major
,
9394 raid_fds
[sd
->disk
.raid_disk
] = dev_open(dn
, O_RDWR
);
9395 if (raid_fds
[sd
->disk
.raid_disk
] < 0) {
9396 pr_err("cannot open component\n");
9401 /* check if maximum array degradation level is not exceeded
9403 if ((raid_disks
- opened
) >
9404 imsm_get_allowed_degradation(info
->new_level
, raid_disks
,
9406 pr_err("Not enough disks can be opened.\n");
9407 close_targets(raid_fds
, raid_disks
);
9413 /*******************************************************************************
9414 * Function: validate_container_imsm
9415 * Description: This routine validates container after assemble,
9416 * eg. if devices in container are under the same controller.
9419 * info : linked list with info about devices used in array
9423 ******************************************************************************/
9424 int validate_container_imsm(struct mdinfo
*info
)
9426 if (check_env("IMSM_NO_PLATFORM"))
9429 struct sys_dev
*idev
;
9430 struct sys_dev
*hba
= NULL
;
9431 struct sys_dev
*intel_devices
= find_intel_devices();
9432 char *dev_path
= devt_to_devpath(makedev(info
->disk
.major
,
9435 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
9436 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
9445 pr_err("WARNING - Cannot detect HBA for device %s!\n",
9446 devid2kname(makedev(info
->disk
.major
, info
->disk
.minor
)));
9450 const struct imsm_orom
*orom
= get_orom_by_device_id(hba
->dev_id
);
9453 for (dev
= info
->next
; dev
; dev
= dev
->next
) {
9454 dev_path
= devt_to_devpath(makedev(dev
->disk
.major
, dev
->disk
.minor
));
9456 struct sys_dev
*hba2
= NULL
;
9457 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
9458 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
9466 const struct imsm_orom
*orom2
= hba2
== NULL
? NULL
:
9467 get_orom_by_device_id(hba2
->dev_id
);
9469 if (hba2
&& hba
->type
!= hba2
->type
) {
9470 pr_err("WARNING - HBAs of devices do not match %s != %s\n",
9471 get_sys_dev_type(hba
->type
), get_sys_dev_type(hba2
->type
));
9475 if (orom
!= orom2
) {
9476 pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n"
9477 " This operation is not supported and can lead to data loss.\n");
9482 pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n"
9483 " This operation is not supported and can lead to data loss.\n");
9491 /*******************************************************************************
9492 * Function: init_migr_record_imsm
9493 * Description: Function inits imsm migration record
9495 * super : imsm internal array info
9496 * dev : device under migration
9497 * info : general array info to find the smallest device
9500 ******************************************************************************/
9501 void init_migr_record_imsm(struct supertype
*st
, struct imsm_dev
*dev
,
9502 struct mdinfo
*info
)
9504 struct intel_super
*super
= st
->sb
;
9505 struct migr_record
*migr_rec
= super
->migr_rec
;
9507 unsigned long long dsize
, dev_sectors
;
9508 long long unsigned min_dev_sectors
= -1LLU;
9512 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
9513 struct imsm_map
*map_src
= get_imsm_map(dev
, MAP_1
);
9514 unsigned long long num_migr_units
;
9515 unsigned long long array_blocks
;
9517 memset(migr_rec
, 0, sizeof(struct migr_record
));
9518 migr_rec
->family_num
= __cpu_to_le32(super
->anchor
->family_num
);
9520 /* only ascending reshape supported now */
9521 migr_rec
->ascending_migr
= __cpu_to_le32(1);
9523 migr_rec
->dest_depth_per_unit
= GEN_MIGR_AREA_SIZE
/
9524 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
9525 migr_rec
->dest_depth_per_unit
*=
9526 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
9527 new_data_disks
= imsm_num_data_members(dev
, MAP_0
);
9528 migr_rec
->blocks_per_unit
=
9529 __cpu_to_le32(migr_rec
->dest_depth_per_unit
* new_data_disks
);
9530 migr_rec
->dest_depth_per_unit
=
9531 __cpu_to_le32(migr_rec
->dest_depth_per_unit
);
9532 array_blocks
= info
->component_size
* new_data_disks
;
9534 array_blocks
/ __le32_to_cpu(migr_rec
->blocks_per_unit
);
9536 if (array_blocks
% __le32_to_cpu(migr_rec
->blocks_per_unit
))
9538 migr_rec
->num_migr_units
= __cpu_to_le32(num_migr_units
);
9540 migr_rec
->post_migr_vol_cap
= dev
->size_low
;
9541 migr_rec
->post_migr_vol_cap_hi
= dev
->size_high
;
9543 /* Find the smallest dev */
9544 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
9545 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
9546 fd
= dev_open(nm
, O_RDONLY
);
9549 get_dev_size(fd
, NULL
, &dsize
);
9550 dev_sectors
= dsize
/ 512;
9551 if (dev_sectors
< min_dev_sectors
)
9552 min_dev_sectors
= dev_sectors
;
9555 migr_rec
->ckpt_area_pba
= __cpu_to_le32(min_dev_sectors
-
9556 RAID_DISK_RESERVED_BLOCKS_IMSM_HI
);
9558 write_imsm_migr_rec(st
);
9563 /*******************************************************************************
9564 * Function: save_backup_imsm
9565 * Description: Function saves critical data stripes to Migration Copy Area
9566 * and updates the current migration unit status.
9567 * Use restore_stripes() to form a destination stripe,
9568 * and to write it to the Copy Area.
9570 * st : supertype information
9571 * dev : imsm device that backup is saved for
9572 * info : general array info
9573 * buf : input buffer
9574 * length : length of data to backup (blocks_per_unit)
9578 ******************************************************************************/
9579 int save_backup_imsm(struct supertype
*st
,
9580 struct imsm_dev
*dev
,
9581 struct mdinfo
*info
,
9586 struct intel_super
*super
= st
->sb
;
9587 unsigned long long *target_offsets
;
9590 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
9591 int new_disks
= map_dest
->num_members
;
9592 int dest_layout
= 0;
9594 unsigned long long start
;
9595 int data_disks
= imsm_num_data_members(dev
, MAP_0
);
9597 targets
= xmalloc(new_disks
* sizeof(int));
9599 for (i
= 0; i
< new_disks
; i
++)
9602 target_offsets
= xcalloc(new_disks
, sizeof(unsigned long long));
9604 start
= info
->reshape_progress
* 512;
9605 for (i
= 0; i
< new_disks
; i
++) {
9606 target_offsets
[i
] = (unsigned long long)
9607 __le32_to_cpu(super
->migr_rec
->ckpt_area_pba
) * 512;
9608 /* move back copy area adderss, it will be moved forward
9609 * in restore_stripes() using start input variable
9611 target_offsets
[i
] -= start
/data_disks
;
9614 if (open_backup_targets(info
, new_disks
, targets
,
9618 dest_layout
= imsm_level_to_layout(map_dest
->raid_level
);
9619 dest_chunk
= __le16_to_cpu(map_dest
->blocks_per_strip
) * 512;
9621 if (restore_stripes(targets
, /* list of dest devices */
9622 target_offsets
, /* migration record offsets */
9625 map_dest
->raid_level
,
9627 -1, /* source backup file descriptor */
9628 0, /* input buf offset
9629 * always 0 buf is already offseted */
9633 pr_err("Error restoring stripes\n");
9641 close_targets(targets
, new_disks
);
9644 free(target_offsets
);
9649 /*******************************************************************************
9650 * Function: save_checkpoint_imsm
9651 * Description: Function called for current unit status update
9652 * in the migration record. It writes it to disk.
9654 * super : imsm internal array info
9655 * info : general array info
9659 * 2: failure, means no valid migration record
9660 * / no general migration in progress /
9661 ******************************************************************************/
9662 int save_checkpoint_imsm(struct supertype
*st
, struct mdinfo
*info
, int state
)
9664 struct intel_super
*super
= st
->sb
;
9665 unsigned long long blocks_per_unit
;
9666 unsigned long long curr_migr_unit
;
9668 if (load_imsm_migr_rec(super
, info
) != 0) {
9669 dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n");
9673 blocks_per_unit
= __le32_to_cpu(super
->migr_rec
->blocks_per_unit
);
9674 if (blocks_per_unit
== 0) {
9675 dprintf("imsm: no migration in progress.\n");
9678 curr_migr_unit
= info
->reshape_progress
/ blocks_per_unit
;
9679 /* check if array is alligned to copy area
9680 * if it is not alligned, add one to current migration unit value
9681 * this can happend on array reshape finish only
9683 if (info
->reshape_progress
% blocks_per_unit
)
9686 super
->migr_rec
->curr_migr_unit
=
9687 __cpu_to_le32(curr_migr_unit
);
9688 super
->migr_rec
->rec_status
= __cpu_to_le32(state
);
9689 super
->migr_rec
->dest_1st_member_lba
=
9690 __cpu_to_le32(curr_migr_unit
*
9691 __le32_to_cpu(super
->migr_rec
->dest_depth_per_unit
));
9692 if (write_imsm_migr_rec(st
) < 0) {
9693 dprintf("imsm: Cannot write migration record outside backup area\n");
9700 /*******************************************************************************
9701 * Function: recover_backup_imsm
9702 * Description: Function recovers critical data from the Migration Copy Area
9703 * while assembling an array.
9705 * super : imsm internal array info
9706 * info : general array info
9708 * 0 : success (or there is no data to recover)
9710 ******************************************************************************/
9711 int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
)
9713 struct intel_super
*super
= st
->sb
;
9714 struct migr_record
*migr_rec
= super
->migr_rec
;
9715 struct imsm_map
*map_dest
;
9716 struct intel_dev
*id
= NULL
;
9717 unsigned long long read_offset
;
9718 unsigned long long write_offset
;
9720 int *targets
= NULL
;
9721 int new_disks
, i
, err
;
9724 unsigned int sector_size
= super
->sector_size
;
9725 unsigned long curr_migr_unit
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
9726 unsigned long num_migr_units
= __le32_to_cpu(migr_rec
->num_migr_units
);
9728 int skipped_disks
= 0;
9730 err
= sysfs_get_str(info
, NULL
, "array_state", (char *)buffer
, 20);
9734 /* recover data only during assemblation */
9735 if (strncmp(buffer
, "inactive", 8) != 0)
9737 /* no data to recover */
9738 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
9740 if (curr_migr_unit
>= num_migr_units
)
9743 /* find device during reshape */
9744 for (id
= super
->devlist
; id
; id
= id
->next
)
9745 if (is_gen_migration(id
->dev
))
9750 map_dest
= get_imsm_map(id
->dev
, MAP_0
);
9751 new_disks
= map_dest
->num_members
;
9753 read_offset
= (unsigned long long)
9754 __le32_to_cpu(migr_rec
->ckpt_area_pba
) * 512;
9756 write_offset
= ((unsigned long long)
9757 __le32_to_cpu(migr_rec
->dest_1st_member_lba
) +
9758 pba_of_lba0(map_dest
)) * 512;
9760 unit_len
= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
9761 if (posix_memalign((void **)&buf
, sector_size
, unit_len
) != 0)
9763 targets
= xcalloc(new_disks
, sizeof(int));
9765 if (open_backup_targets(info
, new_disks
, targets
, super
, id
->dev
)) {
9766 pr_err("Cannot open some devices belonging to array.\n");
9770 for (i
= 0; i
< new_disks
; i
++) {
9771 if (targets
[i
] < 0) {
9775 if (lseek64(targets
[i
], read_offset
, SEEK_SET
) < 0) {
9776 pr_err("Cannot seek to block: %s\n",
9781 if ((unsigned)read(targets
[i
], buf
, unit_len
) != unit_len
) {
9782 pr_err("Cannot read copy area block: %s\n",
9787 if (lseek64(targets
[i
], write_offset
, SEEK_SET
) < 0) {
9788 pr_err("Cannot seek to block: %s\n",
9793 if ((unsigned)write(targets
[i
], buf
, unit_len
) != unit_len
) {
9794 pr_err("Cannot restore block: %s\n",
9801 if (skipped_disks
> imsm_get_allowed_degradation(info
->new_level
,
9805 pr_err("Cannot restore data from backup. Too many failed disks\n");
9809 if (save_checkpoint_imsm(st
, info
, UNIT_SRC_NORMAL
)) {
9810 /* ignore error == 2, this can mean end of reshape here
9812 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n");
9818 for (i
= 0; i
< new_disks
; i
++)
9827 static char disk_by_path
[] = "/dev/disk/by-path/";
9829 static const char *imsm_get_disk_controller_domain(const char *path
)
9831 char disk_path
[PATH_MAX
];
9835 strcpy(disk_path
, disk_by_path
);
9836 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
9837 if (stat(disk_path
, &st
) == 0) {
9838 struct sys_dev
* hba
;
9841 path
= devt_to_devpath(st
.st_rdev
);
9844 hba
= find_disk_attached_hba(-1, path
);
9845 if (hba
&& hba
->type
== SYS_DEV_SAS
)
9847 else if (hba
&& hba
->type
== SYS_DEV_SATA
)
9851 dprintf("path: %s hba: %s attached: %s\n",
9852 path
, (hba
) ? hba
->path
: "NULL", drv
);
9858 static char *imsm_find_array_devnm_by_subdev(int subdev
, char *container
)
9860 static char devnm
[32];
9861 char subdev_name
[20];
9862 struct mdstat_ent
*mdstat
;
9864 sprintf(subdev_name
, "%d", subdev
);
9865 mdstat
= mdstat_by_subdev(subdev_name
, container
);
9869 strcpy(devnm
, mdstat
->devnm
);
9870 free_mdstat(mdstat
);
9874 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
9875 struct geo_params
*geo
,
9876 int *old_raid_disks
,
9879 /* currently we only support increasing the number of devices
9880 * for a container. This increases the number of device for each
9881 * member array. They must all be RAID0 or RAID5.
9884 struct mdinfo
*info
, *member
;
9885 int devices_that_can_grow
= 0;
9887 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st
->devnm
);
9889 if (geo
->size
> 0 ||
9890 geo
->level
!= UnSet
||
9891 geo
->layout
!= UnSet
||
9892 geo
->chunksize
!= 0 ||
9893 geo
->raid_disks
== UnSet
) {
9894 dprintf("imsm: Container operation is allowed for raid disks number change only.\n");
9898 if (direction
== ROLLBACK_METADATA_CHANGES
) {
9899 dprintf("imsm: Metadata changes rollback is not supported for container operation.\n");
9903 info
= container_content_imsm(st
, NULL
);
9904 for (member
= info
; member
; member
= member
->next
) {
9907 dprintf("imsm: checking device_num: %i\n",
9908 member
->container_member
);
9910 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
9911 /* we work on container for Online Capacity Expansion
9912 * only so raid_disks has to grow
9914 dprintf("imsm: for container operation raid disks increase is required\n");
9918 if (info
->array
.level
!= 0 && info
->array
.level
!= 5) {
9919 /* we cannot use this container with other raid level
9921 dprintf("imsm: for container operation wrong raid level (%i) detected\n",
9925 /* check for platform support
9926 * for this raid level configuration
9928 struct intel_super
*super
= st
->sb
;
9929 if (!is_raid_level_supported(super
->orom
,
9930 member
->array
.level
,
9932 dprintf("platform does not support raid%d with %d disk%s\n",
9935 geo
->raid_disks
> 1 ? "s" : "");
9938 /* check if component size is aligned to chunk size
9940 if (info
->component_size
%
9941 (info
->array
.chunk_size
/512)) {
9942 dprintf("Component size is not aligned to chunk size\n");
9947 if (*old_raid_disks
&&
9948 info
->array
.raid_disks
!= *old_raid_disks
)
9950 *old_raid_disks
= info
->array
.raid_disks
;
9952 /* All raid5 and raid0 volumes in container
9953 * have to be ready for Online Capacity Expansion
9954 * so they need to be assembled. We have already
9955 * checked that no recovery etc is happening.
9957 result
= imsm_find_array_devnm_by_subdev(member
->container_member
,
9958 st
->container_devnm
);
9959 if (result
== NULL
) {
9960 dprintf("imsm: cannot find array\n");
9963 devices_that_can_grow
++;
9966 if (!member
&& devices_that_can_grow
)
9970 dprintf("Container operation allowed\n");
9972 dprintf("Error: %i\n", ret_val
);
9977 /* Function: get_spares_for_grow
9978 * Description: Allocates memory and creates list of spare devices
9979 * avaliable in container. Checks if spare drive size is acceptable.
9980 * Parameters: Pointer to the supertype structure
9981 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
9984 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
9986 unsigned long long min_size
= min_acceptable_spare_size_imsm(st
);
9987 return container_choose_spares(st
, min_size
, NULL
, NULL
, NULL
, 0);
9990 /******************************************************************************
9991 * function: imsm_create_metadata_update_for_reshape
9992 * Function creates update for whole IMSM container.
9994 ******************************************************************************/
9995 static int imsm_create_metadata_update_for_reshape(
9996 struct supertype
*st
,
9997 struct geo_params
*geo
,
9999 struct imsm_update_reshape
**updatep
)
10001 struct intel_super
*super
= st
->sb
;
10002 struct imsm_super
*mpb
= super
->anchor
;
10003 int update_memory_size
;
10004 struct imsm_update_reshape
*u
;
10005 struct mdinfo
*spares
;
10008 struct mdinfo
*dev
;
10010 dprintf("(enter) raid_disks = %i\n", geo
->raid_disks
);
10012 delta_disks
= geo
->raid_disks
- old_raid_disks
;
10014 /* size of all update data without anchor */
10015 update_memory_size
= sizeof(struct imsm_update_reshape
);
10017 /* now add space for spare disks that we need to add. */
10018 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
10020 u
= xcalloc(1, update_memory_size
);
10021 u
->type
= update_reshape_container_disks
;
10022 u
->old_raid_disks
= old_raid_disks
;
10023 u
->new_raid_disks
= geo
->raid_disks
;
10025 /* now get spare disks list
10027 spares
= get_spares_for_grow(st
);
10030 || delta_disks
> spares
->array
.spare_disks
) {
10031 pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo
->dev_name
);
10036 /* we have got spares
10037 * update disk list in imsm_disk list table in anchor
10039 dprintf("imsm: %i spares are available.\n\n",
10040 spares
->array
.spare_disks
);
10042 dev
= spares
->devs
;
10043 for (i
= 0; i
< delta_disks
; i
++) {
10048 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
10050 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
10051 dl
->index
= mpb
->num_disks
;
10059 sysfs_free(spares
);
10061 dprintf("imsm: reshape update preparation :");
10062 if (i
== delta_disks
) {
10063 dprintf_cont(" OK\n");
10065 return update_memory_size
;
10068 dprintf_cont(" Error\n");
10073 /******************************************************************************
10074 * function: imsm_create_metadata_update_for_size_change()
10075 * Creates update for IMSM array for array size change.
10077 ******************************************************************************/
10078 static int imsm_create_metadata_update_for_size_change(
10079 struct supertype
*st
,
10080 struct geo_params
*geo
,
10081 struct imsm_update_size_change
**updatep
)
10083 struct intel_super
*super
= st
->sb
;
10084 int update_memory_size
;
10085 struct imsm_update_size_change
*u
;
10087 dprintf("(enter) New size = %llu\n", geo
->size
);
10089 /* size of all update data without anchor */
10090 update_memory_size
= sizeof(struct imsm_update_size_change
);
10092 u
= xcalloc(1, update_memory_size
);
10093 u
->type
= update_size_change
;
10094 u
->subdev
= super
->current_vol
;
10095 u
->new_size
= geo
->size
;
10097 dprintf("imsm: reshape update preparation : OK\n");
10100 return update_memory_size
;
10103 /******************************************************************************
10104 * function: imsm_create_metadata_update_for_migration()
10105 * Creates update for IMSM array.
10107 ******************************************************************************/
10108 static int imsm_create_metadata_update_for_migration(
10109 struct supertype
*st
,
10110 struct geo_params
*geo
,
10111 struct imsm_update_reshape_migration
**updatep
)
10113 struct intel_super
*super
= st
->sb
;
10114 int update_memory_size
;
10115 struct imsm_update_reshape_migration
*u
;
10116 struct imsm_dev
*dev
;
10117 int previous_level
= -1;
10119 dprintf("(enter) New Level = %i\n", geo
->level
);
10121 /* size of all update data without anchor */
10122 update_memory_size
= sizeof(struct imsm_update_reshape_migration
);
10124 u
= xcalloc(1, update_memory_size
);
10125 u
->type
= update_reshape_migration
;
10126 u
->subdev
= super
->current_vol
;
10127 u
->new_level
= geo
->level
;
10128 u
->new_layout
= geo
->layout
;
10129 u
->new_raid_disks
= u
->old_raid_disks
= geo
->raid_disks
;
10130 u
->new_disks
[0] = -1;
10131 u
->new_chunksize
= -1;
10133 dev
= get_imsm_dev(super
, u
->subdev
);
10135 struct imsm_map
*map
;
10137 map
= get_imsm_map(dev
, MAP_0
);
10139 int current_chunk_size
=
10140 __le16_to_cpu(map
->blocks_per_strip
) / 2;
10142 if (geo
->chunksize
!= current_chunk_size
) {
10143 u
->new_chunksize
= geo
->chunksize
/ 1024;
10144 dprintf("imsm: chunk size change from %i to %i\n",
10145 current_chunk_size
, u
->new_chunksize
);
10147 previous_level
= map
->raid_level
;
10150 if (geo
->level
== 5 && previous_level
== 0) {
10151 struct mdinfo
*spares
= NULL
;
10153 u
->new_raid_disks
++;
10154 spares
= get_spares_for_grow(st
);
10155 if (spares
== NULL
|| spares
->array
.spare_disks
< 1) {
10157 sysfs_free(spares
);
10158 update_memory_size
= 0;
10159 dprintf("error: cannot get spare device for requested migration");
10162 sysfs_free(spares
);
10164 dprintf("imsm: reshape update preparation : OK\n");
10167 return update_memory_size
;
10170 static void imsm_update_metadata_locally(struct supertype
*st
,
10171 void *buf
, int len
)
10173 struct metadata_update mu
;
10178 mu
.space_list
= NULL
;
10180 if (imsm_prepare_update(st
, &mu
))
10181 imsm_process_update(st
, &mu
);
10183 while (mu
.space_list
) {
10184 void **space
= mu
.space_list
;
10185 mu
.space_list
= *space
;
10190 /***************************************************************************
10191 * Function: imsm_analyze_change
10192 * Description: Function analyze change for single volume
10193 * and validate if transition is supported
10194 * Parameters: Geometry parameters, supertype structure,
10195 * metadata change direction (apply/rollback)
10196 * Returns: Operation type code on success, -1 if fail
10197 ****************************************************************************/
10198 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
10199 struct geo_params
*geo
,
10202 struct mdinfo info
;
10204 int check_devs
= 0;
10206 /* number of added/removed disks in operation result */
10207 int devNumChange
= 0;
10208 /* imsm compatible layout value for array geometry verification */
10209 int imsm_layout
= -1;
10211 struct imsm_dev
*dev
;
10212 struct intel_super
*super
;
10213 unsigned long long current_size
;
10214 unsigned long long free_size
;
10215 unsigned long long max_size
;
10218 getinfo_super_imsm_volume(st
, &info
, NULL
);
10219 if (geo
->level
!= info
.array
.level
&& geo
->level
>= 0 &&
10220 geo
->level
!= UnSet
) {
10221 switch (info
.array
.level
) {
10223 if (geo
->level
== 5) {
10224 change
= CH_MIGRATION
;
10225 if (geo
->layout
!= ALGORITHM_LEFT_ASYMMETRIC
) {
10226 pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n");
10228 goto analyse_change_exit
;
10230 imsm_layout
= geo
->layout
;
10232 devNumChange
= 1; /* parity disk added */
10233 } else if (geo
->level
== 10) {
10234 change
= CH_TAKEOVER
;
10236 devNumChange
= 2; /* two mirrors added */
10237 imsm_layout
= 0x102; /* imsm supported layout */
10242 if (geo
->level
== 0) {
10243 change
= CH_TAKEOVER
;
10245 devNumChange
= -(geo
->raid_disks
/2);
10246 imsm_layout
= 0; /* imsm raid0 layout */
10250 if (change
== -1) {
10251 pr_err("Error. Level Migration from %d to %d not supported!\n",
10252 info
.array
.level
, geo
->level
);
10253 goto analyse_change_exit
;
10256 geo
->level
= info
.array
.level
;
10258 if (geo
->layout
!= info
.array
.layout
&&
10259 (geo
->layout
!= UnSet
&& geo
->layout
!= -1)) {
10260 change
= CH_MIGRATION
;
10261 if (info
.array
.layout
== 0 && info
.array
.level
== 5 &&
10262 geo
->layout
== 5) {
10263 /* reshape 5 -> 4 */
10264 } else if (info
.array
.layout
== 5 && info
.array
.level
== 5 &&
10265 geo
->layout
== 0) {
10266 /* reshape 4 -> 5 */
10270 pr_err("Error. Layout Migration from %d to %d not supported!\n",
10271 info
.array
.layout
, geo
->layout
);
10273 goto analyse_change_exit
;
10276 geo
->layout
= info
.array
.layout
;
10277 if (imsm_layout
== -1)
10278 imsm_layout
= info
.array
.layout
;
10281 if (geo
->chunksize
> 0 && geo
->chunksize
!= UnSet
&&
10282 geo
->chunksize
!= info
.array
.chunk_size
) {
10283 if (info
.array
.level
== 10) {
10284 pr_err("Error. Chunk size change for RAID 10 is not supported.\n");
10286 goto analyse_change_exit
;
10288 change
= CH_MIGRATION
;
10290 geo
->chunksize
= info
.array
.chunk_size
;
10293 chunk
= geo
->chunksize
/ 1024;
10296 dev
= get_imsm_dev(super
, super
->current_vol
);
10297 data_disks
= imsm_num_data_members(dev
, MAP_0
);
10298 /* compute current size per disk member
10300 current_size
= info
.custom_array_size
/ data_disks
;
10302 if (geo
->size
> 0 && geo
->size
!= MAX_SIZE
) {
10303 /* align component size
10305 geo
->size
= imsm_component_size_aligment_check(
10306 get_imsm_raid_level(dev
->vol
.map
),
10307 chunk
* 1024, super
->sector_size
,
10309 if (geo
->size
== 0) {
10310 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is 0).\n",
10312 goto analyse_change_exit
;
10316 if (current_size
!= geo
->size
&& geo
->size
> 0) {
10317 if (change
!= -1) {
10318 pr_err("Error. Size change should be the only one at a time.\n");
10320 goto analyse_change_exit
;
10322 if ((super
->current_vol
+ 1) != super
->anchor
->num_raid_devs
) {
10323 pr_err("Error. The last volume in container can be expanded only (%i/%s).\n",
10324 super
->current_vol
, st
->devnm
);
10325 goto analyse_change_exit
;
10327 /* check the maximum available size
10329 rv
= imsm_get_free_size(st
, dev
->vol
.map
->num_members
,
10330 0, chunk
, &free_size
);
10332 /* Cannot find maximum available space
10336 max_size
= free_size
+ current_size
;
10337 /* align component size
10339 max_size
= imsm_component_size_aligment_check(
10340 get_imsm_raid_level(dev
->vol
.map
),
10341 chunk
* 1024, super
->sector_size
,
10344 if (geo
->size
== MAX_SIZE
) {
10345 /* requested size change to the maximum available size
10347 if (max_size
== 0) {
10348 pr_err("Error. Cannot find maximum available space.\n");
10350 goto analyse_change_exit
;
10352 geo
->size
= max_size
;
10355 if (direction
== ROLLBACK_METADATA_CHANGES
) {
10356 /* accept size for rollback only
10359 /* round size due to metadata compatibility
10361 geo
->size
= (geo
->size
>> SECT_PER_MB_SHIFT
)
10362 << SECT_PER_MB_SHIFT
;
10363 dprintf("Prepare update for size change to %llu\n",
10365 if (current_size
>= geo
->size
) {
10366 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is %llu).\n",
10367 current_size
, geo
->size
);
10368 goto analyse_change_exit
;
10370 if (max_size
&& geo
->size
> max_size
) {
10371 pr_err("Error. Requested size is larger than maximum available size (maximum available size is %llu, requested size /rounded/ is %llu).\n",
10372 max_size
, geo
->size
);
10373 goto analyse_change_exit
;
10376 geo
->size
*= data_disks
;
10377 geo
->raid_disks
= dev
->vol
.map
->num_members
;
10378 change
= CH_ARRAY_SIZE
;
10380 if (!validate_geometry_imsm(st
,
10383 geo
->raid_disks
+ devNumChange
,
10385 geo
->size
, INVALID_SECTORS
,
10390 struct intel_super
*super
= st
->sb
;
10391 struct imsm_super
*mpb
= super
->anchor
;
10393 if (mpb
->num_raid_devs
> 1) {
10394 pr_err("Error. Cannot perform operation on %s- for this operation it MUST be single array in container\n",
10400 analyse_change_exit
:
10401 if (direction
== ROLLBACK_METADATA_CHANGES
&&
10402 (change
== CH_MIGRATION
|| change
== CH_TAKEOVER
)) {
10403 dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n");
10409 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
10411 struct intel_super
*super
= st
->sb
;
10412 struct imsm_update_takeover
*u
;
10414 u
= xmalloc(sizeof(struct imsm_update_takeover
));
10416 u
->type
= update_takeover
;
10417 u
->subarray
= super
->current_vol
;
10419 /* 10->0 transition */
10420 if (geo
->level
== 0)
10421 u
->direction
= R10_TO_R0
;
10423 /* 0->10 transition */
10424 if (geo
->level
== 10)
10425 u
->direction
= R0_TO_R10
;
10427 /* update metadata locally */
10428 imsm_update_metadata_locally(st
, u
,
10429 sizeof(struct imsm_update_takeover
));
10430 /* and possibly remotely */
10431 if (st
->update_tail
)
10432 append_metadata_update(st
, u
,
10433 sizeof(struct imsm_update_takeover
));
10440 static int imsm_reshape_super(struct supertype
*st
, unsigned long long size
,
10442 int layout
, int chunksize
, int raid_disks
,
10443 int delta_disks
, char *backup
, char *dev
,
10444 int direction
, int verbose
)
10447 struct geo_params geo
;
10449 dprintf("(enter)\n");
10451 memset(&geo
, 0, sizeof(struct geo_params
));
10453 geo
.dev_name
= dev
;
10454 strcpy(geo
.devnm
, st
->devnm
);
10457 geo
.layout
= layout
;
10458 geo
.chunksize
= chunksize
;
10459 geo
.raid_disks
= raid_disks
;
10460 if (delta_disks
!= UnSet
)
10461 geo
.raid_disks
+= delta_disks
;
10463 dprintf("for level : %i\n", geo
.level
);
10464 dprintf("for raid_disks : %i\n", geo
.raid_disks
);
10466 if (experimental() == 0)
10469 if (strcmp(st
->container_devnm
, st
->devnm
) == 0) {
10470 /* On container level we can only increase number of devices. */
10471 dprintf("imsm: info: Container operation\n");
10472 int old_raid_disks
= 0;
10474 if (imsm_reshape_is_allowed_on_container(
10475 st
, &geo
, &old_raid_disks
, direction
)) {
10476 struct imsm_update_reshape
*u
= NULL
;
10479 len
= imsm_create_metadata_update_for_reshape(
10480 st
, &geo
, old_raid_disks
, &u
);
10483 dprintf("imsm: Cannot prepare update\n");
10484 goto exit_imsm_reshape_super
;
10488 /* update metadata locally */
10489 imsm_update_metadata_locally(st
, u
, len
);
10490 /* and possibly remotely */
10491 if (st
->update_tail
)
10492 append_metadata_update(st
, u
, len
);
10497 pr_err("(imsm) Operation is not allowed on this container\n");
10500 /* On volume level we support following operations
10501 * - takeover: raid10 -> raid0; raid0 -> raid10
10502 * - chunk size migration
10503 * - migration: raid5 -> raid0; raid0 -> raid5
10505 struct intel_super
*super
= st
->sb
;
10506 struct intel_dev
*dev
= super
->devlist
;
10508 dprintf("imsm: info: Volume operation\n");
10509 /* find requested device */
10512 imsm_find_array_devnm_by_subdev(
10513 dev
->index
, st
->container_devnm
);
10514 if (devnm
&& strcmp(devnm
, geo
.devnm
) == 0)
10519 pr_err("Cannot find %s (%s) subarray\n",
10520 geo
.dev_name
, geo
.devnm
);
10521 goto exit_imsm_reshape_super
;
10523 super
->current_vol
= dev
->index
;
10524 change
= imsm_analyze_change(st
, &geo
, direction
);
10527 ret_val
= imsm_takeover(st
, &geo
);
10529 case CH_MIGRATION
: {
10530 struct imsm_update_reshape_migration
*u
= NULL
;
10532 imsm_create_metadata_update_for_migration(
10535 dprintf("imsm: Cannot prepare update\n");
10539 /* update metadata locally */
10540 imsm_update_metadata_locally(st
, u
, len
);
10541 /* and possibly remotely */
10542 if (st
->update_tail
)
10543 append_metadata_update(st
, u
, len
);
10548 case CH_ARRAY_SIZE
: {
10549 struct imsm_update_size_change
*u
= NULL
;
10551 imsm_create_metadata_update_for_size_change(
10554 dprintf("imsm: Cannot prepare update\n");
10558 /* update metadata locally */
10559 imsm_update_metadata_locally(st
, u
, len
);
10560 /* and possibly remotely */
10561 if (st
->update_tail
)
10562 append_metadata_update(st
, u
, len
);
10572 exit_imsm_reshape_super
:
10573 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
10577 #define COMPLETED_OK 0
10578 #define COMPLETED_NONE 1
10579 #define COMPLETED_DELAYED 2
10581 static int read_completed(int fd
, unsigned long long *val
)
10586 ret
= sysfs_fd_get_str(fd
, buf
, 50);
10590 ret
= COMPLETED_OK
;
10591 if (strncmp(buf
, "none", 4) == 0) {
10592 ret
= COMPLETED_NONE
;
10593 } else if (strncmp(buf
, "delayed", 7) == 0) {
10594 ret
= COMPLETED_DELAYED
;
10597 *val
= strtoull(buf
, &ep
, 0);
10598 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))
10604 /*******************************************************************************
10605 * Function: wait_for_reshape_imsm
10606 * Description: Function writes new sync_max value and waits until
10607 * reshape process reach new position
10609 * sra : general array info
10610 * ndata : number of disks in new array's layout
10613 * 1 : there is no reshape in progress,
10615 ******************************************************************************/
10616 int wait_for_reshape_imsm(struct mdinfo
*sra
, int ndata
)
10618 int fd
= sysfs_get_fd(sra
, NULL
, "sync_completed");
10620 unsigned long long completed
;
10621 /* to_complete : new sync_max position */
10622 unsigned long long to_complete
= sra
->reshape_progress
;
10623 unsigned long long position_to_set
= to_complete
/ ndata
;
10626 dprintf("cannot open reshape_position\n");
10631 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
10633 dprintf("cannot read reshape_position (no reshape in progres)\n");
10642 if (completed
> position_to_set
) {
10643 dprintf("wrong next position to set %llu (%llu)\n",
10644 to_complete
, position_to_set
);
10648 dprintf("Position set: %llu\n", position_to_set
);
10649 if (sysfs_set_num(sra
, NULL
, "sync_max",
10650 position_to_set
) != 0) {
10651 dprintf("cannot set reshape position to %llu\n",
10660 int timeout
= 3000;
10662 sysfs_wait(fd
, &timeout
);
10663 if (sysfs_get_str(sra
, NULL
, "sync_action",
10665 strncmp(action
, "reshape", 7) != 0) {
10666 if (strncmp(action
, "idle", 4) == 0)
10672 rc
= read_completed(fd
, &completed
);
10674 dprintf("cannot read reshape_position (in loop)\n");
10677 } else if (rc
== COMPLETED_NONE
)
10679 } while (completed
< position_to_set
);
10685 /*******************************************************************************
10686 * Function: check_degradation_change
10687 * Description: Check that array hasn't become failed.
10689 * info : for sysfs access
10690 * sources : source disks descriptors
10691 * degraded: previous degradation level
10693 * degradation level
10694 ******************************************************************************/
10695 int check_degradation_change(struct mdinfo
*info
,
10699 unsigned long long new_degraded
;
10702 rv
= sysfs_get_ll(info
, NULL
, "degraded", &new_degraded
);
10703 if (rv
== -1 || (new_degraded
!= (unsigned long long)degraded
)) {
10704 /* check each device to ensure it is still working */
10707 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
10708 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
10710 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
10713 if (sysfs_get_str(info
,
10714 sd
, "state", sbuf
, sizeof(sbuf
)) < 0 ||
10715 strstr(sbuf
, "faulty") ||
10716 strstr(sbuf
, "in_sync") == NULL
) {
10717 /* this device is dead */
10718 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
10719 if (sd
->disk
.raid_disk
>= 0 &&
10720 sources
[sd
->disk
.raid_disk
] >= 0) {
10722 sd
->disk
.raid_disk
]);
10723 sources
[sd
->disk
.raid_disk
] =
10732 return new_degraded
;
10735 /*******************************************************************************
10736 * Function: imsm_manage_reshape
10737 * Description: Function finds array under reshape and it manages reshape
10738 * process. It creates stripes backups (if required) and sets
10741 * afd : Backup handle (nattive) - not used
10742 * sra : general array info
10743 * reshape : reshape parameters - not used
10744 * st : supertype structure
10745 * blocks : size of critical section [blocks]
10746 * fds : table of source device descriptor
10747 * offsets : start of array (offest per devices)
10749 * destfd : table of destination device descriptor
10750 * destoffsets : table of destination offsets (per device)
10752 * 1 : success, reshape is done
10754 ******************************************************************************/
10755 static int imsm_manage_reshape(
10756 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
10757 struct supertype
*st
, unsigned long backup_blocks
,
10758 int *fds
, unsigned long long *offsets
,
10759 int dests
, int *destfd
, unsigned long long *destoffsets
)
10762 struct intel_super
*super
= st
->sb
;
10763 struct intel_dev
*dv
;
10764 unsigned int sector_size
= super
->sector_size
;
10765 struct imsm_dev
*dev
= NULL
;
10766 struct imsm_map
*map_src
;
10767 int migr_vol_qan
= 0;
10768 int ndata
, odata
; /* [bytes] */
10769 int chunk
; /* [bytes] */
10770 struct migr_record
*migr_rec
;
10772 unsigned int buf_size
; /* [bytes] */
10773 unsigned long long max_position
; /* array size [bytes] */
10774 unsigned long long next_step
; /* [blocks]/[bytes] */
10775 unsigned long long old_data_stripe_length
;
10776 unsigned long long start_src
; /* [bytes] */
10777 unsigned long long start
; /* [bytes] */
10778 unsigned long long start_buf_shift
; /* [bytes] */
10780 int source_layout
= 0;
10785 if (!fds
|| !offsets
)
10788 /* Find volume during the reshape */
10789 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
10790 if (dv
->dev
->vol
.migr_type
== MIGR_GEN_MIGR
10791 && dv
->dev
->vol
.migr_state
== 1) {
10796 /* Only one volume can migrate at the same time */
10797 if (migr_vol_qan
!= 1) {
10798 pr_err("%s", migr_vol_qan
?
10799 "Number of migrating volumes greater than 1\n" :
10800 "There is no volume during migrationg\n");
10804 map_src
= get_imsm_map(dev
, MAP_1
);
10805 if (map_src
== NULL
)
10808 ndata
= imsm_num_data_members(dev
, MAP_0
);
10809 odata
= imsm_num_data_members(dev
, MAP_1
);
10811 chunk
= __le16_to_cpu(map_src
->blocks_per_strip
) * 512;
10812 old_data_stripe_length
= odata
* chunk
;
10814 migr_rec
= super
->migr_rec
;
10816 /* initialize migration record for start condition */
10817 if (sra
->reshape_progress
== 0)
10818 init_migr_record_imsm(st
, dev
, sra
);
10820 if (__le32_to_cpu(migr_rec
->rec_status
) != UNIT_SRC_NORMAL
) {
10821 dprintf("imsm: cannot restart migration when data are present in copy area.\n");
10824 /* Save checkpoint to update migration record for current
10825 * reshape position (in md). It can be farther than current
10826 * reshape position in metadata.
10828 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
10829 /* ignore error == 2, this can mean end of reshape here
10831 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n");
10836 /* size for data */
10837 buf_size
= __le32_to_cpu(migr_rec
->blocks_per_unit
) * 512;
10838 /* extend buffer size for parity disk */
10839 buf_size
+= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
10840 /* add space for stripe aligment */
10841 buf_size
+= old_data_stripe_length
;
10842 if (posix_memalign((void **)&buf
, MAX_SECTOR_SIZE
, buf_size
)) {
10843 dprintf("imsm: Cannot allocate checkpoint buffer\n");
10847 max_position
= sra
->component_size
* ndata
;
10848 source_layout
= imsm_level_to_layout(map_src
->raid_level
);
10850 while (__le32_to_cpu(migr_rec
->curr_migr_unit
) <
10851 __le32_to_cpu(migr_rec
->num_migr_units
)) {
10852 /* current reshape position [blocks] */
10853 unsigned long long current_position
=
10854 __le32_to_cpu(migr_rec
->blocks_per_unit
)
10855 * __le32_to_cpu(migr_rec
->curr_migr_unit
);
10856 unsigned long long border
;
10858 /* Check that array hasn't become failed.
10860 degraded
= check_degradation_change(sra
, fds
, degraded
);
10861 if (degraded
> 1) {
10862 dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded
);
10866 next_step
= __le32_to_cpu(migr_rec
->blocks_per_unit
);
10868 if ((current_position
+ next_step
) > max_position
)
10869 next_step
= max_position
- current_position
;
10871 start
= current_position
* 512;
10873 /* align reading start to old geometry */
10874 start_buf_shift
= start
% old_data_stripe_length
;
10875 start_src
= start
- start_buf_shift
;
10877 border
= (start_src
/ odata
) - (start
/ ndata
);
10879 if (border
<= __le32_to_cpu(migr_rec
->dest_depth_per_unit
)) {
10880 /* save critical stripes to buf
10881 * start - start address of current unit
10882 * to backup [bytes]
10883 * start_src - start address of current unit
10884 * to backup alligned to source array
10887 unsigned long long next_step_filler
;
10888 unsigned long long copy_length
= next_step
* 512;
10890 /* allign copy area length to stripe in old geometry */
10891 next_step_filler
= ((copy_length
+ start_buf_shift
)
10892 % old_data_stripe_length
);
10893 if (next_step_filler
)
10894 next_step_filler
= (old_data_stripe_length
10895 - next_step_filler
);
10896 dprintf("save_stripes() parameters: start = %llu,\tstart_src = %llu,\tnext_step*512 = %llu,\tstart_in_buf_shift = %llu,\tnext_step_filler = %llu\n",
10897 start
, start_src
, copy_length
,
10898 start_buf_shift
, next_step_filler
);
10900 if (save_stripes(fds
, offsets
, map_src
->num_members
,
10901 chunk
, map_src
->raid_level
,
10902 source_layout
, 0, NULL
, start_src
,
10904 next_step_filler
+ start_buf_shift
,
10906 dprintf("imsm: Cannot save stripes to buffer\n");
10909 /* Convert data to destination format and store it
10910 * in backup general migration area
10912 if (save_backup_imsm(st
, dev
, sra
,
10913 buf
+ start_buf_shift
, copy_length
)) {
10914 dprintf("imsm: Cannot save stripes to target devices\n");
10917 if (save_checkpoint_imsm(st
, sra
,
10918 UNIT_SRC_IN_CP_AREA
)) {
10919 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n");
10923 /* set next step to use whole border area */
10924 border
/= next_step
;
10926 next_step
*= border
;
10928 /* When data backed up, checkpoint stored,
10929 * kick the kernel to reshape unit of data
10931 next_step
= next_step
+ sra
->reshape_progress
;
10932 /* limit next step to array max position */
10933 if (next_step
> max_position
)
10934 next_step
= max_position
;
10935 sysfs_set_num(sra
, NULL
, "suspend_lo", sra
->reshape_progress
);
10936 sysfs_set_num(sra
, NULL
, "suspend_hi", next_step
);
10937 sra
->reshape_progress
= next_step
;
10939 /* wait until reshape finish */
10940 if (wait_for_reshape_imsm(sra
, ndata
)) {
10941 dprintf("wait_for_reshape_imsm returned error!\n");
10947 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
10948 /* ignore error == 2, this can mean end of reshape here
10950 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n");
10956 /* clear migr_rec on disks after successful migration */
10959 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SECTORS
*sector_size
);
10960 for (d
= super
->disks
; d
; d
= d
->next
) {
10961 if (d
->index
< 0 || is_failed(&d
->disk
))
10963 unsigned long long dsize
;
10965 get_dev_size(d
->fd
, NULL
, &dsize
);
10966 if (lseek64(d
->fd
, dsize
- MIGR_REC_SECTOR_POSITION
*sector_size
,
10968 if (write(d
->fd
, super
->migr_rec_buf
,
10969 MIGR_REC_BUF_SECTORS
*sector_size
) !=
10970 MIGR_REC_BUF_SECTORS
*sector_size
)
10971 perror("Write migr_rec failed");
10975 /* return '1' if done */
10979 /* See Grow.c: abort_reshape() for further explanation */
10980 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
10981 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
10982 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
10987 #endif /* MDASSEMBLE */
10989 struct superswitch super_imsm
= {
10991 .examine_super
= examine_super_imsm
,
10992 .brief_examine_super
= brief_examine_super_imsm
,
10993 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
10994 .export_examine_super
= export_examine_super_imsm
,
10995 .detail_super
= detail_super_imsm
,
10996 .brief_detail_super
= brief_detail_super_imsm
,
10997 .write_init_super
= write_init_super_imsm
,
10998 .validate_geometry
= validate_geometry_imsm
,
10999 .add_to_super
= add_to_super_imsm
,
11000 .remove_from_super
= remove_from_super_imsm
,
11001 .detail_platform
= detail_platform_imsm
,
11002 .export_detail_platform
= export_detail_platform_imsm
,
11003 .kill_subarray
= kill_subarray_imsm
,
11004 .update_subarray
= update_subarray_imsm
,
11005 .load_container
= load_container_imsm
,
11006 .default_geometry
= default_geometry_imsm
,
11007 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
11008 .reshape_super
= imsm_reshape_super
,
11009 .manage_reshape
= imsm_manage_reshape
,
11010 .recover_backup
= recover_backup_imsm
,
11011 .copy_metadata
= copy_metadata_imsm
,
11013 .match_home
= match_home_imsm
,
11014 .uuid_from_super
= uuid_from_super_imsm
,
11015 .getinfo_super
= getinfo_super_imsm
,
11016 .getinfo_super_disks
= getinfo_super_disks_imsm
,
11017 .update_super
= update_super_imsm
,
11019 .avail_size
= avail_size_imsm
,
11020 .min_acceptable_spare_size
= min_acceptable_spare_size_imsm
,
11022 .compare_super
= compare_super_imsm
,
11024 .load_super
= load_super_imsm
,
11025 .init_super
= init_super_imsm
,
11026 .store_super
= store_super_imsm
,
11027 .free_super
= free_super_imsm
,
11028 .match_metadata_desc
= match_metadata_desc_imsm
,
11029 .container_content
= container_content_imsm
,
11030 .validate_container
= validate_container_imsm
,
11037 .open_new
= imsm_open_new
,
11038 .set_array_state
= imsm_set_array_state
,
11039 .set_disk
= imsm_set_disk
,
11040 .sync_metadata
= imsm_sync_metadata
,
11041 .activate_spare
= imsm_activate_spare
,
11042 .process_update
= imsm_process_update
,
11043 .prepare_update
= imsm_prepare_update
,
11044 #endif /* MDASSEMBLE */