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 | \
87 /* Define attributes that are unused but not harmful */
88 #define MPB_ATTRIB_IGNORED (MPB_ATTRIB_NEVER_USE)
90 #define MPB_SECTOR_CNT 2210
91 #define IMSM_RESERVED_SECTORS 4096
92 #define NUM_BLOCKS_DIRTY_STRIPE_REGION 2056
93 #define SECT_PER_MB_SHIFT 11
94 #define MAX_SECTOR_SIZE 4096
96 /* Disk configuration info. */
97 #define IMSM_MAX_DEVICES 255
99 __u8 serial
[MAX_RAID_SERIAL_LEN
];/* 0xD8 - 0xE7 ascii serial number */
100 __u32 total_blocks_lo
; /* 0xE8 - 0xEB total blocks lo */
101 __u32 scsi_id
; /* 0xEC - 0xEF scsi ID */
102 #define SPARE_DISK __cpu_to_le32(0x01) /* Spare */
103 #define CONFIGURED_DISK __cpu_to_le32(0x02) /* Member of some RaidDev */
104 #define FAILED_DISK __cpu_to_le32(0x04) /* Permanent failure */
105 __u32 status
; /* 0xF0 - 0xF3 */
106 __u32 owner_cfg_num
; /* which config 0,1,2... owns this disk */
107 __u32 total_blocks_hi
; /* 0xF4 - 0xF5 total blocks hi */
108 #define IMSM_DISK_FILLERS 3
109 __u32 filler
[IMSM_DISK_FILLERS
]; /* 0xF5 - 0x107 MPB_DISK_FILLERS for future expansion */
112 /* map selector for map managment
118 /* RAID map configuration infos. */
120 __u32 pba_of_lba0_lo
; /* start address of partition */
121 __u32 blocks_per_member_lo
;/* blocks per member */
122 __u32 num_data_stripes_lo
; /* number of data stripes */
123 __u16 blocks_per_strip
;
124 __u8 map_state
; /* Normal, Uninitialized, Degraded, Failed */
125 #define IMSM_T_STATE_NORMAL 0
126 #define IMSM_T_STATE_UNINITIALIZED 1
127 #define IMSM_T_STATE_DEGRADED 2
128 #define IMSM_T_STATE_FAILED 3
130 #define IMSM_T_RAID0 0
131 #define IMSM_T_RAID1 1
132 #define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */
133 __u8 num_members
; /* number of member disks */
134 __u8 num_domains
; /* number of parity domains */
135 __u8 failed_disk_num
; /* valid only when state is degraded */
137 __u32 pba_of_lba0_hi
;
138 __u32 blocks_per_member_hi
;
139 __u32 num_data_stripes_hi
;
140 __u32 filler
[4]; /* expansion area */
141 #define IMSM_ORD_REBUILD (1 << 24)
142 __u32 disk_ord_tbl
[1]; /* disk_ord_tbl[num_members],
143 * top byte contains some flags
145 } __attribute__ ((packed
));
148 __u32 curr_migr_unit
;
149 __u32 checkpoint_id
; /* id to access curr_migr_unit */
150 __u8 migr_state
; /* Normal or Migrating */
152 #define MIGR_REBUILD 1
153 #define MIGR_VERIFY 2 /* analagous to echo check > sync_action */
154 #define MIGR_GEN_MIGR 3
155 #define MIGR_STATE_CHANGE 4
156 #define MIGR_REPAIR 5
157 __u8 migr_type
; /* Initializing, Rebuilding, ... */
159 __u8 fs_state
; /* fast-sync state for CnG (0xff == disabled) */
160 __u16 verify_errors
; /* number of mismatches */
161 __u16 bad_blocks
; /* number of bad blocks during verify */
163 struct imsm_map map
[1];
164 /* here comes another one if migr_state */
165 } __attribute__ ((packed
));
168 __u8 volume
[MAX_RAID_SERIAL_LEN
];
171 #define DEV_BOOTABLE __cpu_to_le32(0x01)
172 #define DEV_BOOT_DEVICE __cpu_to_le32(0x02)
173 #define DEV_READ_COALESCING __cpu_to_le32(0x04)
174 #define DEV_WRITE_COALESCING __cpu_to_le32(0x08)
175 #define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10)
176 #define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20)
177 #define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40)
178 #define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80)
179 #define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100)
180 #define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200)
181 #define DEV_CLONE_N_GO __cpu_to_le32(0x400)
182 #define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800)
183 #define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000)
184 __u32 status
; /* Persistent RaidDev status */
185 __u32 reserved_blocks
; /* Reserved blocks at beginning of volume */
189 __u8 cng_master_disk
;
193 #define IMSM_DEV_FILLERS 10
194 __u32 filler
[IMSM_DEV_FILLERS
];
196 } __attribute__ ((packed
));
199 __u8 sig
[MAX_SIGNATURE_LENGTH
]; /* 0x00 - 0x1F */
200 __u32 check_sum
; /* 0x20 - 0x23 MPB Checksum */
201 __u32 mpb_size
; /* 0x24 - 0x27 Size of MPB */
202 __u32 family_num
; /* 0x28 - 0x2B Checksum from first time this config was written */
203 __u32 generation_num
; /* 0x2C - 0x2F Incremented each time this array's MPB is written */
204 __u32 error_log_size
; /* 0x30 - 0x33 in bytes */
205 __u32 attributes
; /* 0x34 - 0x37 */
206 __u8 num_disks
; /* 0x38 Number of configured disks */
207 __u8 num_raid_devs
; /* 0x39 Number of configured volumes */
208 __u8 error_log_pos
; /* 0x3A */
209 __u8 fill
[1]; /* 0x3B */
210 __u32 cache_size
; /* 0x3c - 0x40 in mb */
211 __u32 orig_family_num
; /* 0x40 - 0x43 original family num */
212 __u32 pwr_cycle_count
; /* 0x44 - 0x47 simulated power cycle count for array */
213 __u32 bbm_log_size
; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */
214 #define IMSM_FILLERS 35
215 __u32 filler
[IMSM_FILLERS
]; /* 0x4C - 0xD7 RAID_MPB_FILLERS */
216 struct imsm_disk disk
[1]; /* 0xD8 diskTbl[numDisks] */
217 /* here comes imsm_dev[num_raid_devs] */
218 /* here comes BBM logs */
219 } __attribute__ ((packed
));
221 #define BBM_LOG_MAX_ENTRIES 254
222 #define BBM_LOG_MAX_LBA_ENTRY_VAL 256 /* Represents 256 LBAs */
223 #define BBM_LOG_SIGNATURE 0xabadb10c
225 struct bbm_log_block_addr
{
228 } __attribute__ ((__packed__
));
230 struct bbm_log_entry
{
231 __u8 marked_count
; /* Number of blocks marked - 1 */
232 __u8 disk_ordinal
; /* Disk entry within the imsm_super */
233 struct bbm_log_block_addr defective_block_start
;
234 } __attribute__ ((__packed__
));
237 __u32 signature
; /* 0xABADB10C */
239 struct bbm_log_entry marked_block_entries
[BBM_LOG_MAX_ENTRIES
];
240 } __attribute__ ((__packed__
));
243 static char *map_state_str
[] = { "normal", "uninitialized", "degraded", "failed" };
246 #define RAID_DISK_RESERVED_BLOCKS_IMSM_HI 2209
248 #define GEN_MIGR_AREA_SIZE 2048 /* General Migration Copy Area size in blocks */
250 #define MIGR_REC_BUF_SECTORS 1 /* size of migr_record i/o buffer in sectors */
251 #define MIGR_REC_SECTOR_POSITION 1 /* migr_record position offset on disk,
252 * MIGR_REC_BUF_SECTORS <= MIGR_REC_SECTOR_POS
255 #define UNIT_SRC_NORMAL 0 /* Source data for curr_migr_unit must
256 * be recovered using srcMap */
257 #define UNIT_SRC_IN_CP_AREA 1 /* Source data for curr_migr_unit has
258 * already been migrated and must
259 * be recovered from checkpoint area */
261 __u32 rec_status
; /* Status used to determine how to restart
262 * migration in case it aborts
264 __u32 curr_migr_unit
; /* 0..numMigrUnits-1 */
265 __u32 family_num
; /* Family number of MPB
266 * containing the RaidDev
267 * that is migrating */
268 __u32 ascending_migr
; /* True if migrating in increasing
270 __u32 blocks_per_unit
; /* Num disk blocks per unit of operation */
271 __u32 dest_depth_per_unit
; /* Num member blocks each destMap
273 * advances per unit-of-operation */
274 __u32 ckpt_area_pba
; /* Pba of first block of ckpt copy area */
275 __u32 dest_1st_member_lba
; /* First member lba on first
276 * stripe of destination */
277 __u32 num_migr_units
; /* Total num migration units-of-op */
278 __u32 post_migr_vol_cap
; /* Size of volume after
279 * migration completes */
280 __u32 post_migr_vol_cap_hi
; /* Expansion space for LBA64 */
281 __u32 ckpt_read_disk_num
; /* Which member disk in destSubMap[0] the
282 * migration ckpt record was read from
283 * (for recovered migrations) */
284 } __attribute__ ((__packed__
));
289 * 2: metadata does not match
297 struct md_list
*next
;
300 #define pr_vrb(fmt, arg...) (void) (verbose && pr_err(fmt, ##arg))
302 static __u8
migr_type(struct imsm_dev
*dev
)
304 if (dev
->vol
.migr_type
== MIGR_VERIFY
&&
305 dev
->status
& DEV_VERIFY_AND_FIX
)
308 return dev
->vol
.migr_type
;
311 static void set_migr_type(struct imsm_dev
*dev
, __u8 migr_type
)
313 /* for compatibility with older oroms convert MIGR_REPAIR, into
314 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
316 if (migr_type
== MIGR_REPAIR
) {
317 dev
->vol
.migr_type
= MIGR_VERIFY
;
318 dev
->status
|= DEV_VERIFY_AND_FIX
;
320 dev
->vol
.migr_type
= migr_type
;
321 dev
->status
&= ~DEV_VERIFY_AND_FIX
;
325 static unsigned int sector_count(__u32 bytes
, unsigned int sector_size
)
327 return ROUND_UP(bytes
, sector_size
) / sector_size
;
330 static unsigned int mpb_sectors(struct imsm_super
*mpb
,
331 unsigned int sector_size
)
333 return sector_count(__le32_to_cpu(mpb
->mpb_size
), sector_size
);
337 struct imsm_dev
*dev
;
338 struct intel_dev
*next
;
343 enum sys_dev_type type
;
346 struct intel_hba
*next
;
353 /* internal representation of IMSM metadata */
356 void *buf
; /* O_DIRECT buffer for reading/writing metadata */
357 struct imsm_super
*anchor
; /* immovable parameters */
360 void *migr_rec_buf
; /* buffer for I/O operations */
361 struct migr_record
*migr_rec
; /* migration record */
363 int clean_migration_record_by_mdmon
; /* when reshape is switched to next
364 array, it indicates that mdmon is allowed to clean migration
366 size_t len
; /* size of the 'buf' allocation */
367 size_t extra_space
; /* extra space in 'buf' that is not used yet */
368 void *next_buf
; /* for realloc'ing buf from the manager */
370 int updates_pending
; /* count of pending updates for mdmon */
371 int current_vol
; /* index of raid device undergoing creation */
372 unsigned long long create_offset
; /* common start for 'current_vol' */
373 __u32 random
; /* random data for seeding new family numbers */
374 struct intel_dev
*devlist
;
375 unsigned int sector_size
; /* sector size of used member drives */
379 __u8 serial
[MAX_RAID_SERIAL_LEN
];
382 struct imsm_disk disk
;
385 struct extent
*e
; /* for determining freespace @ create */
386 int raiddisk
; /* slot to fill in autolayout */
388 } *disks
, *current_disk
;
389 struct dl
*disk_mgmt_list
; /* list of disks to add/remove while mdmon
391 struct dl
*missing
; /* disks removed while we weren't looking */
392 struct bbm_log
*bbm_log
;
393 struct intel_hba
*hba
; /* device path of the raid controller for this metadata */
394 const struct imsm_orom
*orom
; /* platform firmware support */
395 struct intel_super
*next
; /* (temp) list for disambiguating family_num */
399 struct imsm_disk disk
;
400 #define IMSM_UNKNOWN_OWNER (-1)
402 struct intel_disk
*next
;
406 unsigned long long start
, size
;
409 /* definitions of reshape process types */
410 enum imsm_reshape_type
{
416 /* definition of messages passed to imsm_process_update */
417 enum imsm_update_type
{
418 update_activate_spare
,
422 update_add_remove_disk
,
423 update_reshape_container_disks
,
424 update_reshape_migration
,
426 update_general_migration_checkpoint
,
428 update_prealloc_badblocks_mem
,
431 struct imsm_update_activate_spare
{
432 enum imsm_update_type type
;
436 struct imsm_update_activate_spare
*next
;
442 unsigned long long size
;
449 enum takeover_direction
{
453 struct imsm_update_takeover
{
454 enum imsm_update_type type
;
456 enum takeover_direction direction
;
459 struct imsm_update_reshape
{
460 enum imsm_update_type type
;
464 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
467 struct imsm_update_reshape_migration
{
468 enum imsm_update_type type
;
471 /* fields for array migration changes
478 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
481 struct imsm_update_size_change
{
482 enum imsm_update_type type
;
487 struct imsm_update_general_migration_checkpoint
{
488 enum imsm_update_type type
;
489 __u32 curr_migr_unit
;
493 __u8 serial
[MAX_RAID_SERIAL_LEN
];
496 struct imsm_update_create_array
{
497 enum imsm_update_type type
;
502 struct imsm_update_kill_array
{
503 enum imsm_update_type type
;
507 struct imsm_update_rename_array
{
508 enum imsm_update_type type
;
509 __u8 name
[MAX_RAID_SERIAL_LEN
];
513 struct imsm_update_add_remove_disk
{
514 enum imsm_update_type type
;
517 struct imsm_update_prealloc_bb_mem
{
518 enum imsm_update_type type
;
521 static const char *_sys_dev_type
[] = {
522 [SYS_DEV_UNKNOWN
] = "Unknown",
523 [SYS_DEV_SAS
] = "SAS",
524 [SYS_DEV_SATA
] = "SATA",
525 [SYS_DEV_NVME
] = "NVMe",
526 [SYS_DEV_VMD
] = "VMD"
529 const char *get_sys_dev_type(enum sys_dev_type type
)
531 if (type
>= SYS_DEV_MAX
)
532 type
= SYS_DEV_UNKNOWN
;
534 return _sys_dev_type
[type
];
537 static struct intel_hba
* alloc_intel_hba(struct sys_dev
*device
)
539 struct intel_hba
*result
= xmalloc(sizeof(*result
));
541 result
->type
= device
->type
;
542 result
->path
= xstrdup(device
->path
);
544 if (result
->path
&& (result
->pci_id
= strrchr(result
->path
, '/')) != NULL
)
550 static struct intel_hba
* find_intel_hba(struct intel_hba
*hba
, struct sys_dev
*device
)
552 struct intel_hba
*result
;
554 for (result
= hba
; result
; result
= result
->next
) {
555 if (result
->type
== device
->type
&& strcmp(result
->path
, device
->path
) == 0)
561 static int attach_hba_to_super(struct intel_super
*super
, struct sys_dev
*device
)
563 struct intel_hba
*hba
;
565 /* check if disk attached to Intel HBA */
566 hba
= find_intel_hba(super
->hba
, device
);
569 /* Check if HBA is already attached to super */
570 if (super
->hba
== NULL
) {
571 super
->hba
= alloc_intel_hba(device
);
576 /* Intel metadata allows for all disks attached to the same type HBA.
577 * Do not support HBA types mixing
579 if (device
->type
!= hba
->type
)
582 /* Multiple same type HBAs can be used if they share the same OROM */
583 const struct imsm_orom
*device_orom
= get_orom_by_device_id(device
->dev_id
);
585 if (device_orom
!= super
->orom
)
591 hba
->next
= alloc_intel_hba(device
);
595 static struct sys_dev
* find_disk_attached_hba(int fd
, const char *devname
)
597 struct sys_dev
*list
, *elem
;
600 if ((list
= find_intel_devices()) == NULL
)
604 disk_path
= (char *) devname
;
606 disk_path
= diskfd_to_devpath(fd
);
611 for (elem
= list
; elem
; elem
= elem
->next
)
612 if (path_attached_to_hba(disk_path
, elem
->path
))
615 if (disk_path
!= devname
)
621 static int find_intel_hba_capability(int fd
, struct intel_super
*super
,
624 static struct supertype
*match_metadata_desc_imsm(char *arg
)
626 struct supertype
*st
;
628 if (strcmp(arg
, "imsm") != 0 &&
629 strcmp(arg
, "default") != 0
633 st
= xcalloc(1, sizeof(*st
));
634 st
->ss
= &super_imsm
;
635 st
->max_devs
= IMSM_MAX_DEVICES
;
636 st
->minor_version
= 0;
642 static __u8
*get_imsm_version(struct imsm_super
*mpb
)
644 return &mpb
->sig
[MPB_SIG_LEN
];
648 /* retrieve a disk directly from the anchor when the anchor is known to be
649 * up-to-date, currently only at load time
651 static struct imsm_disk
*__get_imsm_disk(struct imsm_super
*mpb
, __u8 index
)
653 if (index
>= mpb
->num_disks
)
655 return &mpb
->disk
[index
];
658 /* retrieve the disk description based on a index of the disk
661 static struct dl
*get_imsm_dl_disk(struct intel_super
*super
, __u8 index
)
665 for (d
= super
->disks
; d
; d
= d
->next
)
666 if (d
->index
== index
)
671 /* retrieve a disk from the parsed metadata */
672 static struct imsm_disk
*get_imsm_disk(struct intel_super
*super
, __u8 index
)
676 dl
= get_imsm_dl_disk(super
, index
);
683 /* generate a checksum directly from the anchor when the anchor is known to be
684 * up-to-date, currently only at load or write_super after coalescing
686 static __u32
__gen_imsm_checksum(struct imsm_super
*mpb
)
688 __u32 end
= mpb
->mpb_size
/ sizeof(end
);
689 __u32
*p
= (__u32
*) mpb
;
693 sum
+= __le32_to_cpu(*p
);
697 return sum
- __le32_to_cpu(mpb
->check_sum
);
700 static size_t sizeof_imsm_map(struct imsm_map
*map
)
702 return sizeof(struct imsm_map
) + sizeof(__u32
) * (map
->num_members
- 1);
705 struct imsm_map
*get_imsm_map(struct imsm_dev
*dev
, int second_map
)
707 /* A device can have 2 maps if it is in the middle of a migration.
709 * MAP_0 - we return the first map
710 * MAP_1 - we return the second map if it exists, else NULL
711 * MAP_X - we return the second map if it exists, else the first
713 struct imsm_map
*map
= &dev
->vol
.map
[0];
714 struct imsm_map
*map2
= NULL
;
716 if (dev
->vol
.migr_state
)
717 map2
= (void *)map
+ sizeof_imsm_map(map
);
719 switch (second_map
) {
736 /* return the size of the device.
737 * migr_state increases the returned size if map[0] were to be duplicated
739 static size_t sizeof_imsm_dev(struct imsm_dev
*dev
, int migr_state
)
741 size_t size
= sizeof(*dev
) - sizeof(struct imsm_map
) +
742 sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
744 /* migrating means an additional map */
745 if (dev
->vol
.migr_state
)
746 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_1
));
748 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
754 /* retrieve disk serial number list from a metadata update */
755 static struct disk_info
*get_disk_info(struct imsm_update_create_array
*update
)
758 struct disk_info
*inf
;
760 inf
= u
+ sizeof(*update
) - sizeof(struct imsm_dev
) +
761 sizeof_imsm_dev(&update
->dev
, 0);
767 static struct imsm_dev
*__get_imsm_dev(struct imsm_super
*mpb
, __u8 index
)
773 if (index
>= mpb
->num_raid_devs
)
776 /* devices start after all disks */
777 offset
= ((void *) &mpb
->disk
[mpb
->num_disks
]) - _mpb
;
779 for (i
= 0; i
<= index
; i
++)
781 return _mpb
+ offset
;
783 offset
+= sizeof_imsm_dev(_mpb
+ offset
, 0);
788 static struct imsm_dev
*get_imsm_dev(struct intel_super
*super
, __u8 index
)
790 struct intel_dev
*dv
;
792 if (index
>= super
->anchor
->num_raid_devs
)
794 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
795 if (dv
->index
== index
)
800 static inline unsigned long long __le48_to_cpu(const struct bbm_log_block_addr
803 return ((((__u64
)__le32_to_cpu(addr
->dw1
)) << 16) |
804 __le16_to_cpu(addr
->w1
));
807 static inline struct bbm_log_block_addr
__cpu_to_le48(unsigned long long sec
)
809 struct bbm_log_block_addr addr
;
811 addr
.w1
= __cpu_to_le16((__u16
)(sec
& 0xffff));
812 addr
.dw1
= __cpu_to_le32((__u32
)(sec
>> 16) & 0xffffffff);
817 /* get size of the bbm log */
818 static __u32
get_imsm_bbm_log_size(struct bbm_log
*log
)
820 if (!log
|| log
->entry_count
== 0)
823 return sizeof(log
->signature
) +
824 sizeof(log
->entry_count
) +
825 log
->entry_count
* sizeof(struct bbm_log_entry
);
827 #endif /* MDASSEMBLE */
829 /* allocate and load BBM log from metadata */
830 static int load_bbm_log(struct intel_super
*super
)
832 struct imsm_super
*mpb
= super
->anchor
;
833 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
835 super
->bbm_log
= xcalloc(1, sizeof(struct bbm_log
));
840 struct bbm_log
*log
= (void *)mpb
+
841 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
845 if (bbm_log_size
< sizeof(log
->signature
) +
846 sizeof(log
->entry_count
))
849 entry_count
= __le32_to_cpu(log
->entry_count
);
850 if ((__le32_to_cpu(log
->signature
) != BBM_LOG_SIGNATURE
) ||
851 (entry_count
> BBM_LOG_MAX_ENTRIES
))
855 sizeof(log
->signature
) + sizeof(log
->entry_count
) +
856 entry_count
* sizeof(struct bbm_log_entry
))
859 memcpy(super
->bbm_log
, log
, bbm_log_size
);
861 super
->bbm_log
->signature
= __cpu_to_le32(BBM_LOG_SIGNATURE
);
862 super
->bbm_log
->entry_count
= 0;
868 /* checks if bad block is within volume boundaries */
869 static int is_bad_block_in_volume(const struct bbm_log_entry
*entry
,
870 const unsigned long long start_sector
,
871 const unsigned long long size
)
873 unsigned long long bb_start
;
874 unsigned long long bb_end
;
876 bb_start
= __le48_to_cpu(&entry
->defective_block_start
);
877 bb_end
= bb_start
+ (entry
->marked_count
+ 1);
879 if (((bb_start
>= start_sector
) && (bb_start
< start_sector
+ size
)) ||
880 ((bb_end
>= start_sector
) && (bb_end
<= start_sector
+ size
)))
886 /* get list of bad blocks on a drive for a volume */
887 static void get_volume_badblocks(const struct bbm_log
*log
, const __u8 idx
,
888 const unsigned long long start_sector
,
889 const unsigned long long size
,
895 for (i
= 0; i
< log
->entry_count
; i
++) {
896 const struct bbm_log_entry
*ent
=
897 &log
->marked_block_entries
[i
];
898 struct md_bb_entry
*bb
;
900 if ((ent
->disk_ordinal
== idx
) &&
901 is_bad_block_in_volume(ent
, start_sector
, size
)) {
904 bbs
->entries
= xmalloc(BBM_LOG_MAX_ENTRIES
*
910 bb
= &bbs
->entries
[count
++];
911 bb
->sector
= __le48_to_cpu(&ent
->defective_block_start
);
912 bb
->length
= ent
->marked_count
+ 1;
920 * == MAP_0 get first map
921 * == MAP_1 get second map
922 * == MAP_X than get map according to the current migr_state
924 static __u32
get_imsm_ord_tbl_ent(struct imsm_dev
*dev
,
928 struct imsm_map
*map
;
930 map
= get_imsm_map(dev
, second_map
);
932 /* top byte identifies disk under rebuild */
933 return __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
936 #define ord_to_idx(ord) (((ord) << 8) >> 8)
937 static __u32
get_imsm_disk_idx(struct imsm_dev
*dev
, int slot
, int second_map
)
939 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, second_map
);
941 return ord_to_idx(ord
);
944 static void set_imsm_ord_tbl_ent(struct imsm_map
*map
, int slot
, __u32 ord
)
946 map
->disk_ord_tbl
[slot
] = __cpu_to_le32(ord
);
949 static int get_imsm_disk_slot(struct imsm_map
*map
, unsigned idx
)
954 for (slot
= 0; slot
< map
->num_members
; slot
++) {
955 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
956 if (ord_to_idx(ord
) == idx
)
963 static int get_imsm_raid_level(struct imsm_map
*map
)
965 if (map
->raid_level
== 1) {
966 if (map
->num_members
== 2)
972 return map
->raid_level
;
975 static int cmp_extent(const void *av
, const void *bv
)
977 const struct extent
*a
= av
;
978 const struct extent
*b
= bv
;
979 if (a
->start
< b
->start
)
981 if (a
->start
> b
->start
)
986 static int count_memberships(struct dl
*dl
, struct intel_super
*super
)
991 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
992 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
993 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
995 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
1002 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
);
1004 static int split_ull(unsigned long long n
, __u32
*lo
, __u32
*hi
)
1006 if (lo
== 0 || hi
== 0)
1008 *lo
= __le32_to_cpu((unsigned)n
);
1009 *hi
= __le32_to_cpu((unsigned)(n
>> 32));
1013 static unsigned long long join_u32(__u32 lo
, __u32 hi
)
1015 return (unsigned long long)__le32_to_cpu(lo
) |
1016 (((unsigned long long)__le32_to_cpu(hi
)) << 32);
1019 static unsigned long long total_blocks(struct imsm_disk
*disk
)
1023 return join_u32(disk
->total_blocks_lo
, disk
->total_blocks_hi
);
1026 static unsigned long long pba_of_lba0(struct imsm_map
*map
)
1030 return join_u32(map
->pba_of_lba0_lo
, map
->pba_of_lba0_hi
);
1033 static unsigned long long blocks_per_member(struct imsm_map
*map
)
1037 return join_u32(map
->blocks_per_member_lo
, map
->blocks_per_member_hi
);
1040 static unsigned long long num_data_stripes(struct imsm_map
*map
)
1044 return join_u32(map
->num_data_stripes_lo
, map
->num_data_stripes_hi
);
1047 static void set_total_blocks(struct imsm_disk
*disk
, unsigned long long n
)
1049 split_ull(n
, &disk
->total_blocks_lo
, &disk
->total_blocks_hi
);
1052 static void set_pba_of_lba0(struct imsm_map
*map
, unsigned long long n
)
1054 split_ull(n
, &map
->pba_of_lba0_lo
, &map
->pba_of_lba0_hi
);
1057 static void set_blocks_per_member(struct imsm_map
*map
, unsigned long long n
)
1059 split_ull(n
, &map
->blocks_per_member_lo
, &map
->blocks_per_member_hi
);
1062 static void set_num_data_stripes(struct imsm_map
*map
, unsigned long long n
)
1064 split_ull(n
, &map
->num_data_stripes_lo
, &map
->num_data_stripes_hi
);
1067 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
)
1069 /* find a list of used extents on the given physical device */
1070 struct extent
*rv
, *e
;
1072 int memberships
= count_memberships(dl
, super
);
1075 /* trim the reserved area for spares, so they can join any array
1076 * regardless of whether the OROM has assigned sectors from the
1077 * IMSM_RESERVED_SECTORS region
1079 if (dl
->index
== -1)
1080 reservation
= imsm_min_reserved_sectors(super
);
1082 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1084 rv
= xcalloc(sizeof(struct extent
), (memberships
+ 1));
1087 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1088 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1089 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1091 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
1092 e
->start
= pba_of_lba0(map
);
1093 e
->size
= blocks_per_member(map
);
1097 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
1099 /* determine the start of the metadata
1100 * when no raid devices are defined use the default
1101 * ...otherwise allow the metadata to truncate the value
1102 * as is the case with older versions of imsm
1105 struct extent
*last
= &rv
[memberships
- 1];
1106 unsigned long long remainder
;
1108 remainder
= total_blocks(&dl
->disk
) - (last
->start
+ last
->size
);
1109 /* round down to 1k block to satisfy precision of the kernel
1113 /* make sure remainder is still sane */
1114 if (remainder
< (unsigned)ROUND_UP(super
->len
, 512) >> 9)
1115 remainder
= ROUND_UP(super
->len
, 512) >> 9;
1116 if (reservation
> remainder
)
1117 reservation
= remainder
;
1119 e
->start
= total_blocks(&dl
->disk
) - reservation
;
1124 /* try to determine how much space is reserved for metadata from
1125 * the last get_extents() entry, otherwise fallback to the
1128 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
1134 /* for spares just return a minimal reservation which will grow
1135 * once the spare is picked up by an array
1137 if (dl
->index
== -1)
1138 return MPB_SECTOR_CNT
;
1140 e
= get_extents(super
, dl
);
1142 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1144 /* scroll to last entry */
1145 for (i
= 0; e
[i
].size
; i
++)
1148 rv
= total_blocks(&dl
->disk
) - e
[i
].start
;
1155 static int is_spare(struct imsm_disk
*disk
)
1157 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
1160 static int is_configured(struct imsm_disk
*disk
)
1162 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
1165 static int is_failed(struct imsm_disk
*disk
)
1167 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
1170 /* try to determine how much space is reserved for metadata from
1171 * the last get_extents() entry on the smallest active disk,
1172 * otherwise fallback to the default
1174 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
)
1178 unsigned long long min_active
;
1180 __u32 rv
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1181 struct dl
*dl
, *dl_min
= NULL
;
1187 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
1190 unsigned long long blocks
= total_blocks(&dl
->disk
);
1191 if (blocks
< min_active
|| min_active
== 0) {
1193 min_active
= blocks
;
1199 /* find last lba used by subarrays on the smallest active disk */
1200 e
= get_extents(super
, dl_min
);
1203 for (i
= 0; e
[i
].size
; i
++)
1206 remainder
= min_active
- e
[i
].start
;
1209 /* to give priority to recovery we should not require full
1210 IMSM_RESERVED_SECTORS from the spare */
1211 rv
= MPB_SECTOR_CNT
+ NUM_BLOCKS_DIRTY_STRIPE_REGION
;
1213 /* if real reservation is smaller use that value */
1214 return (remainder
< rv
) ? remainder
: rv
;
1217 /* Return minimum size of a spare that can be used in this array*/
1218 static unsigned long long min_acceptable_spare_size_imsm(struct supertype
*st
)
1220 struct intel_super
*super
= st
->sb
;
1224 unsigned long long rv
= 0;
1228 /* find first active disk in array */
1230 while (dl
&& (is_failed(&dl
->disk
) || dl
->index
== -1))
1234 /* find last lba used by subarrays */
1235 e
= get_extents(super
, dl
);
1238 for (i
= 0; e
[i
].size
; i
++)
1241 rv
= e
[i
-1].start
+ e
[i
-1].size
;
1244 /* add the amount of space needed for metadata */
1245 rv
= rv
+ imsm_min_reserved_sectors(super
);
1250 static int is_gen_migration(struct imsm_dev
*dev
);
1252 #define IMSM_4K_DIV 8
1255 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
1256 struct imsm_dev
*dev
);
1258 static void print_imsm_dev(struct intel_super
*super
,
1259 struct imsm_dev
*dev
,
1265 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1266 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
1270 printf("[%.16s]:\n", dev
->volume
);
1271 printf(" UUID : %s\n", uuid
);
1272 printf(" RAID Level : %d", get_imsm_raid_level(map
));
1274 printf(" <-- %d", get_imsm_raid_level(map2
));
1276 printf(" Members : %d", map
->num_members
);
1278 printf(" <-- %d", map2
->num_members
);
1280 printf(" Slots : [");
1281 for (i
= 0; i
< map
->num_members
; i
++) {
1282 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
1283 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1288 for (i
= 0; i
< map2
->num_members
; i
++) {
1289 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_1
);
1290 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1295 printf(" Failed disk : ");
1296 if (map
->failed_disk_num
== 0xff)
1299 printf("%i", map
->failed_disk_num
);
1301 slot
= get_imsm_disk_slot(map
, disk_idx
);
1303 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
1304 printf(" This Slot : %d%s\n", slot
,
1305 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
1307 printf(" This Slot : ?\n");
1308 sz
= __le32_to_cpu(dev
->size_high
);
1310 sz
+= __le32_to_cpu(dev
->size_low
);
1311 printf(" Array Size : %llu%s\n", (unsigned long long)sz
,
1312 human_size(sz
* 512));
1313 sz
= blocks_per_member(map
);
1314 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz
,
1315 human_size(sz
* 512));
1316 printf(" Sector Offset : %llu\n",
1318 printf(" Num Stripes : %llu\n",
1319 num_data_stripes(map
));
1320 printf(" Chunk Size : %u KiB",
1321 __le16_to_cpu(map
->blocks_per_strip
) / 2);
1323 printf(" <-- %u KiB",
1324 __le16_to_cpu(map2
->blocks_per_strip
) / 2);
1326 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
1327 printf(" Migrate State : ");
1328 if (dev
->vol
.migr_state
) {
1329 if (migr_type(dev
) == MIGR_INIT
)
1330 printf("initialize\n");
1331 else if (migr_type(dev
) == MIGR_REBUILD
)
1332 printf("rebuild\n");
1333 else if (migr_type(dev
) == MIGR_VERIFY
)
1335 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
1336 printf("general migration\n");
1337 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
1338 printf("state change\n");
1339 else if (migr_type(dev
) == MIGR_REPAIR
)
1342 printf("<unknown:%d>\n", migr_type(dev
));
1345 printf(" Map State : %s", map_state_str
[map
->map_state
]);
1346 if (dev
->vol
.migr_state
) {
1347 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1349 printf(" <-- %s", map_state_str
[map
->map_state
]);
1350 printf("\n Checkpoint : %u ",
1351 __le32_to_cpu(dev
->vol
.curr_migr_unit
));
1352 if (is_gen_migration(dev
) && (slot
> 1 || slot
< 0))
1355 printf("(%llu)", (unsigned long long)
1356 blocks_per_migr_unit(super
, dev
));
1359 printf(" Dirty State : %s\n", dev
->vol
.dirty
? "dirty" : "clean");
1362 static void print_imsm_disk(struct imsm_disk
*disk
, int index
, __u32 reserved
)
1364 char str
[MAX_RAID_SERIAL_LEN
+ 1];
1367 if (index
< -1 || !disk
)
1371 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
1373 printf(" Disk%02d Serial : %s\n", index
, str
);
1375 printf(" Disk Serial : %s\n", str
);
1376 printf(" State :%s%s%s\n", is_spare(disk
) ? " spare" : "",
1377 is_configured(disk
) ? " active" : "",
1378 is_failed(disk
) ? " failed" : "");
1379 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
1380 sz
= total_blocks(disk
) - reserved
;
1381 printf(" Usable Size : %llu%s\n", (unsigned long long)sz
,
1382 human_size(sz
* 512));
1385 void convert_to_4k_imsm_migr_rec(struct intel_super
*super
)
1387 struct migr_record
*migr_rec
= super
->migr_rec
;
1389 migr_rec
->blocks_per_unit
/= IMSM_4K_DIV
;
1390 migr_rec
->ckpt_area_pba
/= IMSM_4K_DIV
;
1391 migr_rec
->dest_1st_member_lba
/= IMSM_4K_DIV
;
1392 migr_rec
->dest_depth_per_unit
/= IMSM_4K_DIV
;
1393 split_ull((join_u32(migr_rec
->post_migr_vol_cap
,
1394 migr_rec
->post_migr_vol_cap_hi
) / IMSM_4K_DIV
),
1395 &migr_rec
->post_migr_vol_cap
, &migr_rec
->post_migr_vol_cap_hi
);
1398 void convert_to_4k_imsm_disk(struct imsm_disk
*disk
)
1400 set_total_blocks(disk
, (total_blocks(disk
)/IMSM_4K_DIV
));
1403 void convert_to_4k(struct intel_super
*super
)
1405 struct imsm_super
*mpb
= super
->anchor
;
1406 struct imsm_disk
*disk
;
1409 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1410 disk
= __get_imsm_disk(mpb
, i
);
1412 convert_to_4k_imsm_disk(disk
);
1414 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1415 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1416 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1418 split_ull((join_u32(dev
->size_low
, dev
->size_high
)/IMSM_4K_DIV
),
1419 &dev
->size_low
, &dev
->size_high
);
1420 dev
->vol
.curr_migr_unit
/= IMSM_4K_DIV
;
1423 set_blocks_per_member(map
, blocks_per_member(map
)/IMSM_4K_DIV
);
1424 map
->blocks_per_strip
/= IMSM_4K_DIV
;
1425 set_pba_of_lba0(map
, pba_of_lba0(map
)/IMSM_4K_DIV
);
1427 if (dev
->vol
.migr_state
) {
1429 map
= get_imsm_map(dev
, MAP_1
);
1430 set_blocks_per_member(map
,
1431 blocks_per_member(map
)/IMSM_4K_DIV
);
1432 map
->blocks_per_strip
/= IMSM_4K_DIV
;
1433 set_pba_of_lba0(map
, pba_of_lba0(map
)/IMSM_4K_DIV
);
1437 mpb
->check_sum
= __gen_imsm_checksum(mpb
);
1440 void examine_migr_rec_imsm(struct intel_super
*super
)
1442 struct migr_record
*migr_rec
= super
->migr_rec
;
1443 struct imsm_super
*mpb
= super
->anchor
;
1446 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1447 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1448 struct imsm_map
*map
;
1451 if (is_gen_migration(dev
) == 0)
1454 printf("\nMigration Record Information:");
1456 /* first map under migration */
1457 map
= get_imsm_map(dev
, MAP_0
);
1459 slot
= get_imsm_disk_slot(map
, super
->disks
->index
);
1460 if (map
== NULL
|| slot
> 1 || slot
< 0) {
1461 printf(" Empty\n ");
1462 printf("Examine one of first two disks in array\n");
1465 printf("\n Status : ");
1466 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
1469 printf("Contains Data\n");
1470 printf(" Current Unit : %u\n",
1471 __le32_to_cpu(migr_rec
->curr_migr_unit
));
1472 printf(" Family : %u\n",
1473 __le32_to_cpu(migr_rec
->family_num
));
1474 printf(" Ascending : %u\n",
1475 __le32_to_cpu(migr_rec
->ascending_migr
));
1476 printf(" Blocks Per Unit : %u\n",
1477 __le32_to_cpu(migr_rec
->blocks_per_unit
));
1478 printf(" Dest. Depth Per Unit : %u\n",
1479 __le32_to_cpu(migr_rec
->dest_depth_per_unit
));
1480 printf(" Checkpoint Area pba : %u\n",
1481 __le32_to_cpu(migr_rec
->ckpt_area_pba
));
1482 printf(" First member lba : %u\n",
1483 __le32_to_cpu(migr_rec
->dest_1st_member_lba
));
1484 printf(" Total Number of Units : %u\n",
1485 __le32_to_cpu(migr_rec
->num_migr_units
));
1486 printf(" Size of volume : %u\n",
1487 __le32_to_cpu(migr_rec
->post_migr_vol_cap
));
1488 printf(" Expansion space for LBA64 : %u\n",
1489 __le32_to_cpu(migr_rec
->post_migr_vol_cap_hi
));
1490 printf(" Record was read from : %u\n",
1491 __le32_to_cpu(migr_rec
->ckpt_read_disk_num
));
1496 #endif /* MDASSEMBLE */
1498 void convert_from_4k_imsm_migr_rec(struct intel_super
*super
)
1500 struct migr_record
*migr_rec
= super
->migr_rec
;
1502 migr_rec
->blocks_per_unit
*= IMSM_4K_DIV
;
1503 migr_rec
->ckpt_area_pba
*= IMSM_4K_DIV
;
1504 migr_rec
->dest_1st_member_lba
*= IMSM_4K_DIV
;
1505 migr_rec
->dest_depth_per_unit
*= IMSM_4K_DIV
;
1506 split_ull((join_u32(migr_rec
->post_migr_vol_cap
,
1507 migr_rec
->post_migr_vol_cap_hi
) * IMSM_4K_DIV
),
1508 &migr_rec
->post_migr_vol_cap
,
1509 &migr_rec
->post_migr_vol_cap_hi
);
1512 void convert_from_4k(struct intel_super
*super
)
1514 struct imsm_super
*mpb
= super
->anchor
;
1515 struct imsm_disk
*disk
;
1518 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1519 disk
= __get_imsm_disk(mpb
, i
);
1521 set_total_blocks(disk
, (total_blocks(disk
)*IMSM_4K_DIV
));
1524 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1525 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1526 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1528 split_ull((join_u32(dev
->size_low
, dev
->size_high
)*IMSM_4K_DIV
),
1529 &dev
->size_low
, &dev
->size_high
);
1530 dev
->vol
.curr_migr_unit
*= IMSM_4K_DIV
;
1533 set_blocks_per_member(map
, blocks_per_member(map
)*IMSM_4K_DIV
);
1534 map
->blocks_per_strip
*= IMSM_4K_DIV
;
1535 set_pba_of_lba0(map
, pba_of_lba0(map
)*IMSM_4K_DIV
);
1537 if (dev
->vol
.migr_state
) {
1539 map
= get_imsm_map(dev
, MAP_1
);
1540 set_blocks_per_member(map
,
1541 blocks_per_member(map
)*IMSM_4K_DIV
);
1542 map
->blocks_per_strip
*= IMSM_4K_DIV
;
1543 set_pba_of_lba0(map
, pba_of_lba0(map
)*IMSM_4K_DIV
);
1547 mpb
->check_sum
= __gen_imsm_checksum(mpb
);
1550 /*******************************************************************************
1551 * function: imsm_check_attributes
1552 * Description: Function checks if features represented by attributes flags
1553 * are supported by mdadm.
1555 * attributes - Attributes read from metadata
1557 * 0 - passed attributes contains unsupported features flags
1558 * 1 - all features are supported
1559 ******************************************************************************/
1560 static int imsm_check_attributes(__u32 attributes
)
1563 __u32 not_supported
= MPB_ATTRIB_SUPPORTED
^0xffffffff;
1565 not_supported
&= ~MPB_ATTRIB_IGNORED
;
1567 not_supported
&= attributes
;
1568 if (not_supported
) {
1569 pr_err("(IMSM): Unsupported attributes : %x\n",
1570 (unsigned)__le32_to_cpu(not_supported
));
1571 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1572 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
1573 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1575 if (not_supported
& MPB_ATTRIB_2TB
) {
1576 dprintf("\t\tMPB_ATTRIB_2TB\n");
1577 not_supported
^= MPB_ATTRIB_2TB
;
1579 if (not_supported
& MPB_ATTRIB_RAID0
) {
1580 dprintf("\t\tMPB_ATTRIB_RAID0\n");
1581 not_supported
^= MPB_ATTRIB_RAID0
;
1583 if (not_supported
& MPB_ATTRIB_RAID1
) {
1584 dprintf("\t\tMPB_ATTRIB_RAID1\n");
1585 not_supported
^= MPB_ATTRIB_RAID1
;
1587 if (not_supported
& MPB_ATTRIB_RAID10
) {
1588 dprintf("\t\tMPB_ATTRIB_RAID10\n");
1589 not_supported
^= MPB_ATTRIB_RAID10
;
1591 if (not_supported
& MPB_ATTRIB_RAID1E
) {
1592 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
1593 not_supported
^= MPB_ATTRIB_RAID1E
;
1595 if (not_supported
& MPB_ATTRIB_RAID5
) {
1596 dprintf("\t\tMPB_ATTRIB_RAID5\n");
1597 not_supported
^= MPB_ATTRIB_RAID5
;
1599 if (not_supported
& MPB_ATTRIB_RAIDCNG
) {
1600 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
1601 not_supported
^= MPB_ATTRIB_RAIDCNG
;
1603 if (not_supported
& MPB_ATTRIB_BBM
) {
1604 dprintf("\t\tMPB_ATTRIB_BBM\n");
1605 not_supported
^= MPB_ATTRIB_BBM
;
1607 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1608 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
1609 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1611 if (not_supported
& MPB_ATTRIB_EXP_STRIPE_SIZE
) {
1612 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
1613 not_supported
^= MPB_ATTRIB_EXP_STRIPE_SIZE
;
1615 if (not_supported
& MPB_ATTRIB_2TB_DISK
) {
1616 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
1617 not_supported
^= MPB_ATTRIB_2TB_DISK
;
1619 if (not_supported
& MPB_ATTRIB_NEVER_USE2
) {
1620 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
1621 not_supported
^= MPB_ATTRIB_NEVER_USE2
;
1623 if (not_supported
& MPB_ATTRIB_NEVER_USE
) {
1624 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
1625 not_supported
^= MPB_ATTRIB_NEVER_USE
;
1629 dprintf("(IMSM): Unknown attributes : %x\n", not_supported
);
1638 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
1640 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
1642 struct intel_super
*super
= st
->sb
;
1643 struct imsm_super
*mpb
= super
->anchor
;
1644 char str
[MAX_SIGNATURE_LENGTH
];
1649 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
1652 snprintf(str
, MPB_SIG_LEN
, "%s", mpb
->sig
);
1653 printf(" Magic : %s\n", str
);
1654 snprintf(str
, strlen(MPB_VERSION_RAID0
), "%s", get_imsm_version(mpb
));
1655 printf(" Version : %s\n", get_imsm_version(mpb
));
1656 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
1657 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
1658 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
1659 printf(" Attributes : ");
1660 if (imsm_check_attributes(mpb
->attributes
))
1661 printf("All supported\n");
1663 printf("not supported\n");
1664 getinfo_super_imsm(st
, &info
, NULL
);
1665 fname_from_uuid(st
, &info
, nbuf
, ':');
1666 printf(" UUID : %s\n", nbuf
+ 5);
1667 sum
= __le32_to_cpu(mpb
->check_sum
);
1668 printf(" Checksum : %08x %s\n", sum
,
1669 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
1670 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
, super
->sector_size
));
1671 printf(" Disks : %d\n", mpb
->num_disks
);
1672 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
1673 print_imsm_disk(__get_imsm_disk(mpb
, super
->disks
->index
), super
->disks
->index
, reserved
);
1674 if (get_imsm_bbm_log_size(super
->bbm_log
)) {
1675 struct bbm_log
*log
= super
->bbm_log
;
1678 printf("Bad Block Management Log:\n");
1679 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
1680 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
1681 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
1683 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1685 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1687 super
->current_vol
= i
;
1688 getinfo_super_imsm(st
, &info
, NULL
);
1689 fname_from_uuid(st
, &info
, nbuf
, ':');
1690 print_imsm_dev(super
, dev
, nbuf
+ 5, super
->disks
->index
);
1692 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1693 if (i
== super
->disks
->index
)
1695 print_imsm_disk(__get_imsm_disk(mpb
, i
), i
, reserved
);
1698 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
1699 if (dl
->index
== -1)
1700 print_imsm_disk(&dl
->disk
, -1, reserved
);
1702 examine_migr_rec_imsm(super
);
1705 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
1707 /* We just write a generic IMSM ARRAY entry */
1710 struct intel_super
*super
= st
->sb
;
1712 if (!super
->anchor
->num_raid_devs
) {
1713 printf("ARRAY metadata=imsm\n");
1717 getinfo_super_imsm(st
, &info
, NULL
);
1718 fname_from_uuid(st
, &info
, nbuf
, ':');
1719 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
1722 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
1724 /* We just write a generic IMSM ARRAY entry */
1728 struct intel_super
*super
= st
->sb
;
1731 if (!super
->anchor
->num_raid_devs
)
1734 getinfo_super_imsm(st
, &info
, NULL
);
1735 fname_from_uuid(st
, &info
, nbuf
, ':');
1736 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1737 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1739 super
->current_vol
= i
;
1740 getinfo_super_imsm(st
, &info
, NULL
);
1741 fname_from_uuid(st
, &info
, nbuf1
, ':');
1742 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
1743 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
1747 static void export_examine_super_imsm(struct supertype
*st
)
1749 struct intel_super
*super
= st
->sb
;
1750 struct imsm_super
*mpb
= super
->anchor
;
1754 getinfo_super_imsm(st
, &info
, NULL
);
1755 fname_from_uuid(st
, &info
, nbuf
, ':');
1756 printf("MD_METADATA=imsm\n");
1757 printf("MD_LEVEL=container\n");
1758 printf("MD_UUID=%s\n", nbuf
+5);
1759 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
1762 static int copy_metadata_imsm(struct supertype
*st
, int from
, int to
)
1764 /* The second last sector of the device contains
1765 * the "struct imsm_super" metadata.
1766 * This contains mpb_size which is the size in bytes of the
1767 * extended metadata. This is located immediately before
1769 * We want to read all that, plus the last sector which
1770 * may contain a migration record, and write it all
1774 unsigned long long dsize
, offset
;
1776 struct imsm_super
*sb
;
1777 struct intel_super
*super
= st
->sb
;
1778 unsigned int sector_size
= super
->sector_size
;
1779 unsigned int written
= 0;
1781 if (posix_memalign(&buf
, MAX_SECTOR_SIZE
, MAX_SECTOR_SIZE
) != 0)
1784 if (!get_dev_size(from
, NULL
, &dsize
))
1787 if (lseek64(from
, dsize
-(2*sector_size
), 0) < 0)
1789 if (read(from
, buf
, sector_size
) != sector_size
)
1792 if (strncmp((char*)sb
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0)
1795 sectors
= mpb_sectors(sb
, sector_size
) + 2;
1796 offset
= dsize
- sectors
* sector_size
;
1797 if (lseek64(from
, offset
, 0) < 0 ||
1798 lseek64(to
, offset
, 0) < 0)
1800 while (written
< sectors
* sector_size
) {
1801 int n
= sectors
*sector_size
- written
;
1804 if (read(from
, buf
, n
) != n
)
1806 if (write(to
, buf
, n
) != n
)
1817 static void detail_super_imsm(struct supertype
*st
, char *homehost
)
1822 getinfo_super_imsm(st
, &info
, NULL
);
1823 fname_from_uuid(st
, &info
, nbuf
, ':');
1824 printf("\n UUID : %s\n", nbuf
+ 5);
1827 static void brief_detail_super_imsm(struct supertype
*st
)
1831 getinfo_super_imsm(st
, &info
, NULL
);
1832 fname_from_uuid(st
, &info
, nbuf
, ':');
1833 printf(" UUID=%s", nbuf
+ 5);
1836 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
);
1837 static void fd2devname(int fd
, char *name
);
1839 static int ahci_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
1841 /* dump an unsorted list of devices attached to AHCI Intel storage
1842 * controller, as well as non-connected ports
1844 int hba_len
= strlen(hba_path
) + 1;
1849 unsigned long port_mask
= (1 << port_count
) - 1;
1851 if (port_count
> (int)sizeof(port_mask
) * 8) {
1853 pr_err("port_count %d out of range\n", port_count
);
1857 /* scroll through /sys/dev/block looking for devices attached to
1860 dir
= opendir("/sys/dev/block");
1864 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
1875 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
1877 path
= devt_to_devpath(makedev(major
, minor
));
1880 if (!path_attached_to_hba(path
, hba_path
)) {
1886 /* retrieve the scsi device type */
1887 if (asprintf(&device
, "/sys/dev/block/%d:%d/device/xxxxxxx", major
, minor
) < 0) {
1889 pr_err("failed to allocate 'device'\n");
1893 sprintf(device
, "/sys/dev/block/%d:%d/device/type", major
, minor
);
1894 if (load_sys(device
, buf
, sizeof(buf
)) != 0) {
1896 pr_err("failed to read device type for %s\n",
1902 type
= strtoul(buf
, NULL
, 10);
1904 /* if it's not a disk print the vendor and model */
1905 if (!(type
== 0 || type
== 7 || type
== 14)) {
1908 sprintf(device
, "/sys/dev/block/%d:%d/device/vendor", major
, minor
);
1909 if (load_sys(device
, buf
, sizeof(buf
)) == 0) {
1910 strncpy(vendor
, buf
, sizeof(vendor
));
1911 vendor
[sizeof(vendor
) - 1] = '\0';
1912 c
= (char *) &vendor
[sizeof(vendor
) - 1];
1913 while (isspace(*c
) || *c
== '\0')
1917 sprintf(device
, "/sys/dev/block/%d:%d/device/model", major
, minor
);
1918 if (load_sys(device
, buf
, sizeof(buf
)) == 0) {
1919 strncpy(model
, buf
, sizeof(model
));
1920 model
[sizeof(model
) - 1] = '\0';
1921 c
= (char *) &model
[sizeof(model
) - 1];
1922 while (isspace(*c
) || *c
== '\0')
1926 if (vendor
[0] && model
[0])
1927 sprintf(buf
, "%.64s %.64s", vendor
, model
);
1929 switch (type
) { /* numbers from hald/linux/device.c */
1930 case 1: sprintf(buf
, "tape"); break;
1931 case 2: sprintf(buf
, "printer"); break;
1932 case 3: sprintf(buf
, "processor"); break;
1934 case 5: sprintf(buf
, "cdrom"); break;
1935 case 6: sprintf(buf
, "scanner"); break;
1936 case 8: sprintf(buf
, "media_changer"); break;
1937 case 9: sprintf(buf
, "comm"); break;
1938 case 12: sprintf(buf
, "raid"); break;
1939 default: sprintf(buf
, "unknown");
1945 /* chop device path to 'host%d' and calculate the port number */
1946 c
= strchr(&path
[hba_len
], '/');
1949 pr_err("%s - invalid path name\n", path
+ hba_len
);
1954 if ((sscanf(&path
[hba_len
], "ata%d", &port
) == 1) ||
1955 ((sscanf(&path
[hba_len
], "host%d", &port
) == 1)))
1959 *c
= '/'; /* repair the full string */
1960 pr_err("failed to determine port number for %s\n",
1967 /* mark this port as used */
1968 port_mask
&= ~(1 << port
);
1970 /* print out the device information */
1972 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
1976 fd
= dev_open(ent
->d_name
, O_RDONLY
);
1978 printf(" Port%d : - disk info unavailable -\n", port
);
1980 fd2devname(fd
, buf
);
1981 printf(" Port%d : %s", port
, buf
);
1982 if (imsm_read_serial(fd
, NULL
, (__u8
*) buf
) == 0)
1983 printf(" (%.*s)\n", MAX_RAID_SERIAL_LEN
, buf
);
1998 for (i
= 0; i
< port_count
; i
++)
1999 if (port_mask
& (1 << i
))
2000 printf(" Port%d : - no device attached -\n", i
);
2006 static int print_vmd_attached_devs(struct sys_dev
*hba
)
2014 if (hba
->type
!= SYS_DEV_VMD
)
2017 /* scroll through /sys/dev/block looking for devices attached to
2020 dir
= opendir("/sys/bus/pci/drivers/nvme");
2024 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2027 /* is 'ent' a device? check that the 'subsystem' link exists and
2028 * that its target matches 'bus'
2030 sprintf(path
, "/sys/bus/pci/drivers/nvme/%s/subsystem",
2032 n
= readlink(path
, link
, sizeof(link
));
2033 if (n
< 0 || n
>= (int)sizeof(link
))
2036 c
= strrchr(link
, '/');
2039 if (strncmp("pci", c
+1, strlen("pci")) != 0)
2042 sprintf(path
, "/sys/bus/pci/drivers/nvme/%s", ent
->d_name
);
2043 /* if not a intel NVMe - skip it*/
2044 if (devpath_to_vendor(path
) != 0x8086)
2047 rp
= realpath(path
, NULL
);
2051 if (path_attached_to_hba(rp
, hba
->path
)) {
2052 printf(" NVMe under VMD : %s\n", rp
);
2061 static void print_found_intel_controllers(struct sys_dev
*elem
)
2063 for (; elem
; elem
= elem
->next
) {
2064 pr_err("found Intel(R) ");
2065 if (elem
->type
== SYS_DEV_SATA
)
2066 fprintf(stderr
, "SATA ");
2067 else if (elem
->type
== SYS_DEV_SAS
)
2068 fprintf(stderr
, "SAS ");
2069 else if (elem
->type
== SYS_DEV_NVME
)
2070 fprintf(stderr
, "NVMe ");
2072 if (elem
->type
== SYS_DEV_VMD
)
2073 fprintf(stderr
, "VMD domain");
2075 fprintf(stderr
, "RAID controller");
2078 fprintf(stderr
, " at %s", elem
->pci_id
);
2079 fprintf(stderr
, ".\n");
2084 static int ahci_get_port_count(const char *hba_path
, int *port_count
)
2091 if ((dir
= opendir(hba_path
)) == NULL
)
2094 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2097 if ((sscanf(ent
->d_name
, "ata%d", &host
) != 1) &&
2098 ((sscanf(ent
->d_name
, "host%d", &host
) != 1)))
2100 if (*port_count
== 0)
2102 else if (host
< host_base
)
2105 if (host
+ 1 > *port_count
+ host_base
)
2106 *port_count
= host
+ 1 - host_base
;
2112 static void print_imsm_capability(const struct imsm_orom
*orom
)
2114 printf(" Platform : Intel(R) ");
2115 if (orom
->capabilities
== 0 && orom
->driver_features
== 0)
2116 printf("Matrix Storage Manager\n");
2118 printf("Rapid Storage Technology%s\n",
2119 imsm_orom_is_enterprise(orom
) ? " enterprise" : "");
2120 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
)
2121 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
,
2122 orom
->minor_ver
, orom
->hotfix_ver
, orom
->build
);
2123 printf(" RAID Levels :%s%s%s%s%s\n",
2124 imsm_orom_has_raid0(orom
) ? " raid0" : "",
2125 imsm_orom_has_raid1(orom
) ? " raid1" : "",
2126 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
2127 imsm_orom_has_raid10(orom
) ? " raid10" : "",
2128 imsm_orom_has_raid5(orom
) ? " raid5" : "");
2129 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2130 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
2131 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
2132 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
2133 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
2134 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
2135 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
2136 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
2137 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
2138 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
2139 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
2140 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
2141 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
2142 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
2143 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
2144 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
2145 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
2146 printf(" 2TB volumes :%s supported\n",
2147 (orom
->attr
& IMSM_OROM_ATTR_2TB
)?"":" not");
2148 printf(" 2TB disks :%s supported\n",
2149 (orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
)?"":" not");
2150 printf(" Max Disks : %d\n", orom
->tds
);
2151 printf(" Max Volumes : %d per array, %d per %s\n",
2152 orom
->vpa
, orom
->vphba
,
2153 imsm_orom_is_nvme(orom
) ? "platform" : "controller");
2157 static void print_imsm_capability_export(const struct imsm_orom
*orom
)
2159 printf("MD_FIRMWARE_TYPE=imsm\n");
2160 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
)
2161 printf("IMSM_VERSION=%d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
2162 orom
->hotfix_ver
, orom
->build
);
2163 printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n",
2164 imsm_orom_has_raid0(orom
) ? "raid0 " : "",
2165 imsm_orom_has_raid1(orom
) ? "raid1 " : "",
2166 imsm_orom_has_raid1e(orom
) ? "raid1e " : "",
2167 imsm_orom_has_raid5(orom
) ? "raid10 " : "",
2168 imsm_orom_has_raid10(orom
) ? "raid5 " : "");
2169 printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2170 imsm_orom_has_chunk(orom
, 2) ? "2k " : "",
2171 imsm_orom_has_chunk(orom
, 4) ? "4k " : "",
2172 imsm_orom_has_chunk(orom
, 8) ? "8k " : "",
2173 imsm_orom_has_chunk(orom
, 16) ? "16k " : "",
2174 imsm_orom_has_chunk(orom
, 32) ? "32k " : "",
2175 imsm_orom_has_chunk(orom
, 64) ? "64k " : "",
2176 imsm_orom_has_chunk(orom
, 128) ? "128k " : "",
2177 imsm_orom_has_chunk(orom
, 256) ? "256k " : "",
2178 imsm_orom_has_chunk(orom
, 512) ? "512k " : "",
2179 imsm_orom_has_chunk(orom
, 1024*1) ? "1M " : "",
2180 imsm_orom_has_chunk(orom
, 1024*2) ? "2M " : "",
2181 imsm_orom_has_chunk(orom
, 1024*4) ? "4M " : "",
2182 imsm_orom_has_chunk(orom
, 1024*8) ? "8M " : "",
2183 imsm_orom_has_chunk(orom
, 1024*16) ? "16M " : "",
2184 imsm_orom_has_chunk(orom
, 1024*32) ? "32M " : "",
2185 imsm_orom_has_chunk(orom
, 1024*64) ? "64M " : "");
2186 printf("IMSM_2TB_VOLUMES=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB
) ? "yes" : "no");
2187 printf("IMSM_2TB_DISKS=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) ? "yes" : "no");
2188 printf("IMSM_MAX_DISKS=%d\n",orom
->tds
);
2189 printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom
->vpa
);
2190 printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom
->vphba
);
2193 static int detail_platform_imsm(int verbose
, int enumerate_only
, char *controller_path
)
2195 /* There are two components to imsm platform support, the ahci SATA
2196 * controller and the option-rom. To find the SATA controller we
2197 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
2198 * controller with the Intel vendor id is present. This approach
2199 * allows mdadm to leverage the kernel's ahci detection logic, with the
2200 * caveat that if ahci.ko is not loaded mdadm will not be able to
2201 * detect platform raid capabilities. The option-rom resides in a
2202 * platform "Adapter ROM". We scan for its signature to retrieve the
2203 * platform capabilities. If raid support is disabled in the BIOS the
2204 * option-rom capability structure will not be available.
2206 struct sys_dev
*list
, *hba
;
2211 if (enumerate_only
) {
2212 if (check_env("IMSM_NO_PLATFORM"))
2214 list
= find_intel_devices();
2217 for (hba
= list
; hba
; hba
= hba
->next
) {
2218 if (find_imsm_capability(hba
)) {
2228 list
= find_intel_devices();
2231 pr_err("no active Intel(R) RAID controller found.\n");
2233 } else if (verbose
> 0)
2234 print_found_intel_controllers(list
);
2236 for (hba
= list
; hba
; hba
= hba
->next
) {
2237 if (controller_path
&& (compare_paths(hba
->path
, controller_path
) != 0))
2239 if (!find_imsm_capability(hba
)) {
2241 pr_err("imsm capabilities not found for controller: %s (type %s)\n",
2242 hba
->type
== SYS_DEV_VMD
? vmd_domain_to_controller(hba
, buf
) : hba
->path
,
2243 get_sys_dev_type(hba
->type
));
2249 if (controller_path
&& result
== 1) {
2250 pr_err("no active Intel(R) RAID controller found under %s\n",
2255 const struct orom_entry
*entry
;
2257 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2258 if (entry
->type
== SYS_DEV_VMD
) {
2259 print_imsm_capability(&entry
->orom
);
2260 for (hba
= list
; hba
; hba
= hba
->next
) {
2261 if (hba
->type
== SYS_DEV_VMD
) {
2263 printf(" I/O Controller : %s (%s)\n",
2264 vmd_domain_to_controller(hba
, buf
), get_sys_dev_type(hba
->type
));
2265 if (print_vmd_attached_devs(hba
)) {
2267 pr_err("failed to get devices attached to VMD domain.\n");
2276 print_imsm_capability(&entry
->orom
);
2277 if (entry
->type
== SYS_DEV_NVME
) {
2278 for (hba
= list
; hba
; hba
= hba
->next
) {
2279 if (hba
->type
== SYS_DEV_NVME
)
2280 printf(" NVMe Device : %s\n", hba
->path
);
2286 struct devid_list
*devid
;
2287 for (devid
= entry
->devid_list
; devid
; devid
= devid
->next
) {
2288 hba
= device_by_id(devid
->devid
);
2292 printf(" I/O Controller : %s (%s)\n",
2293 hba
->path
, get_sys_dev_type(hba
->type
));
2294 if (hba
->type
== SYS_DEV_SATA
) {
2295 host_base
= ahci_get_port_count(hba
->path
, &port_count
);
2296 if (ahci_enumerate_ports(hba
->path
, port_count
, host_base
, verbose
)) {
2298 pr_err("failed to enumerate ports on SATA controller at %s.\n", hba
->pci_id
);
2309 static int export_detail_platform_imsm(int verbose
, char *controller_path
)
2311 struct sys_dev
*list
, *hba
;
2314 list
= find_intel_devices();
2317 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n");
2322 for (hba
= list
; hba
; hba
= hba
->next
) {
2323 if (controller_path
&& (compare_paths(hba
->path
,controller_path
) != 0))
2325 if (!find_imsm_capability(hba
) && verbose
> 0) {
2327 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n",
2328 hba
->type
== SYS_DEV_VMD
? vmd_domain_to_controller(hba
, buf
) : hba
->path
);
2334 const struct orom_entry
*entry
;
2336 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2337 if (entry
->type
== SYS_DEV_VMD
) {
2338 for (hba
= list
; hba
; hba
= hba
->next
)
2339 print_imsm_capability_export(&entry
->orom
);
2342 print_imsm_capability_export(&entry
->orom
);
2350 static int match_home_imsm(struct supertype
*st
, char *homehost
)
2352 /* the imsm metadata format does not specify any host
2353 * identification information. We return -1 since we can never
2354 * confirm nor deny whether a given array is "meant" for this
2355 * host. We rely on compare_super and the 'family_num' fields to
2356 * exclude member disks that do not belong, and we rely on
2357 * mdadm.conf to specify the arrays that should be assembled.
2358 * Auto-assembly may still pick up "foreign" arrays.
2364 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
2366 /* The uuid returned here is used for:
2367 * uuid to put into bitmap file (Create, Grow)
2368 * uuid for backup header when saving critical section (Grow)
2369 * comparing uuids when re-adding a device into an array
2370 * In these cases the uuid required is that of the data-array,
2371 * not the device-set.
2372 * uuid to recognise same set when adding a missing device back
2373 * to an array. This is a uuid for the device-set.
2375 * For each of these we can make do with a truncated
2376 * or hashed uuid rather than the original, as long as
2378 * In each case the uuid required is that of the data-array,
2379 * not the device-set.
2381 /* imsm does not track uuid's so we synthesis one using sha1 on
2382 * - The signature (Which is constant for all imsm array, but no matter)
2383 * - the orig_family_num of the container
2384 * - the index number of the volume
2385 * - the 'serial' number of the volume.
2386 * Hopefully these are all constant.
2388 struct intel_super
*super
= st
->sb
;
2391 struct sha1_ctx ctx
;
2392 struct imsm_dev
*dev
= NULL
;
2395 /* some mdadm versions failed to set ->orig_family_num, in which
2396 * case fall back to ->family_num. orig_family_num will be
2397 * fixed up with the first metadata update.
2399 family_num
= super
->anchor
->orig_family_num
;
2400 if (family_num
== 0)
2401 family_num
= super
->anchor
->family_num
;
2402 sha1_init_ctx(&ctx
);
2403 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
2404 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
2405 if (super
->current_vol
>= 0)
2406 dev
= get_imsm_dev(super
, super
->current_vol
);
2408 __u32 vol
= super
->current_vol
;
2409 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
2410 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
2412 sha1_finish_ctx(&ctx
, buf
);
2413 memcpy(uuid
, buf
, 4*4);
2418 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
2420 __u8
*v
= get_imsm_version(mpb
);
2421 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
2422 char major
[] = { 0, 0, 0 };
2423 char minor
[] = { 0 ,0, 0 };
2424 char patch
[] = { 0, 0, 0 };
2425 char *ver_parse
[] = { major
, minor
, patch
};
2429 while (*v
!= '\0' && v
< end
) {
2430 if (*v
!= '.' && j
< 2)
2431 ver_parse
[i
][j
++] = *v
;
2439 *m
= strtol(minor
, NULL
, 0);
2440 *p
= strtol(patch
, NULL
, 0);
2444 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
2446 /* migr_strip_size when repairing or initializing parity */
2447 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2448 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2450 switch (get_imsm_raid_level(map
)) {
2455 return 128*1024 >> 9;
2459 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
2461 /* migr_strip_size when rebuilding a degraded disk, no idea why
2462 * this is different than migr_strip_size_resync(), but it's good
2465 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2466 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2468 switch (get_imsm_raid_level(map
)) {
2471 if (map
->num_members
% map
->num_domains
== 0)
2472 return 128*1024 >> 9;
2476 return max((__u32
) 64*1024 >> 9, chunk
);
2478 return 128*1024 >> 9;
2482 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
2484 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2485 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2486 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
2487 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
2489 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
2492 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
2494 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2495 int level
= get_imsm_raid_level(lo
);
2497 if (level
== 1 || level
== 10) {
2498 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2500 return hi
->num_domains
;
2502 return num_stripes_per_unit_resync(dev
);
2505 static __u8
imsm_num_data_members(struct imsm_dev
*dev
, int second_map
)
2507 /* named 'imsm_' because raid0, raid1 and raid10
2508 * counter-intuitively have the same number of data disks
2510 struct imsm_map
*map
= get_imsm_map(dev
, second_map
);
2512 switch (get_imsm_raid_level(map
)) {
2514 return map
->num_members
;
2518 return map
->num_members
/2;
2520 return map
->num_members
- 1;
2522 dprintf("unsupported raid level\n");
2527 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
2529 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2530 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2532 switch(get_imsm_raid_level(map
)) {
2535 return chunk
* map
->num_domains
;
2537 return chunk
* map
->num_members
;
2543 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
2545 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2546 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2547 __u32 strip
= block
/ chunk
;
2549 switch (get_imsm_raid_level(map
)) {
2552 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
2553 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
2555 return vol_stripe
* chunk
+ block
% chunk
;
2557 __u32 stripe
= strip
/ (map
->num_members
- 1);
2559 return stripe
* chunk
+ block
% chunk
;
2566 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
2567 struct imsm_dev
*dev
)
2569 /* calculate the conversion factor between per member 'blocks'
2570 * (md/{resync,rebuild}_start) and imsm migration units, return
2571 * 0 for the 'not migrating' and 'unsupported migration' cases
2573 if (!dev
->vol
.migr_state
)
2576 switch (migr_type(dev
)) {
2577 case MIGR_GEN_MIGR
: {
2578 struct migr_record
*migr_rec
= super
->migr_rec
;
2579 return __le32_to_cpu(migr_rec
->blocks_per_unit
);
2584 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2585 __u32 stripes_per_unit
;
2586 __u32 blocks_per_unit
;
2595 /* yes, this is really the translation of migr_units to
2596 * per-member blocks in the 'resync' case
2598 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
2599 migr_chunk
= migr_strip_blocks_resync(dev
);
2600 disks
= imsm_num_data_members(dev
, MAP_0
);
2601 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
2602 stripe
= __le16_to_cpu(map
->blocks_per_strip
) * disks
;
2603 segment
= blocks_per_unit
/ stripe
;
2604 block_rel
= blocks_per_unit
- segment
* stripe
;
2605 parity_depth
= parity_segment_depth(dev
);
2606 block_map
= map_migr_block(dev
, block_rel
);
2607 return block_map
+ parity_depth
* segment
;
2609 case MIGR_REBUILD
: {
2610 __u32 stripes_per_unit
;
2613 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
2614 migr_chunk
= migr_strip_blocks_rebuild(dev
);
2615 return migr_chunk
* stripes_per_unit
;
2617 case MIGR_STATE_CHANGE
:
2623 static int imsm_level_to_layout(int level
)
2631 return ALGORITHM_LEFT_ASYMMETRIC
;
2638 /*******************************************************************************
2639 * Function: read_imsm_migr_rec
2640 * Description: Function reads imsm migration record from last sector of disk
2642 * fd : disk descriptor
2643 * super : metadata info
2647 ******************************************************************************/
2648 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
)
2651 unsigned int sector_size
= super
->sector_size
;
2652 unsigned long long dsize
;
2654 get_dev_size(fd
, NULL
, &dsize
);
2655 if (lseek64(fd
, dsize
- (sector_size
*MIGR_REC_SECTOR_POSITION
),
2657 pr_err("Cannot seek to anchor block: %s\n",
2661 if (read(fd
, super
->migr_rec_buf
,
2662 MIGR_REC_BUF_SECTORS
*sector_size
) !=
2663 MIGR_REC_BUF_SECTORS
*sector_size
) {
2664 pr_err("Cannot read migr record block: %s\n",
2669 if (sector_size
== 4096)
2670 convert_from_4k_imsm_migr_rec(super
);
2676 static struct imsm_dev
*imsm_get_device_during_migration(
2677 struct intel_super
*super
)
2680 struct intel_dev
*dv
;
2682 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
2683 if (is_gen_migration(dv
->dev
))
2689 /*******************************************************************************
2690 * Function: load_imsm_migr_rec
2691 * Description: Function reads imsm migration record (it is stored at the last
2694 * super : imsm internal array info
2695 * info : general array info
2699 * -2 : no migration in progress
2700 ******************************************************************************/
2701 static int load_imsm_migr_rec(struct intel_super
*super
, struct mdinfo
*info
)
2708 struct imsm_dev
*dev
;
2709 struct imsm_map
*map
;
2712 /* find map under migration */
2713 dev
= imsm_get_device_during_migration(super
);
2714 /* nothing to load,no migration in progress?
2720 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
2721 /* read only from one of the first two slots */
2722 if ((sd
->disk
.raid_disk
< 0) ||
2723 (sd
->disk
.raid_disk
> 1))
2726 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
2727 fd
= dev_open(nm
, O_RDONLY
);
2733 map
= get_imsm_map(dev
, MAP_0
);
2734 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
2735 /* skip spare and failed disks
2739 /* read only from one of the first two slots */
2741 slot
= get_imsm_disk_slot(map
, dl
->index
);
2742 if (map
== NULL
|| slot
> 1 || slot
< 0)
2744 sprintf(nm
, "%d:%d", dl
->major
, dl
->minor
);
2745 fd
= dev_open(nm
, O_RDONLY
);
2752 retval
= read_imsm_migr_rec(fd
, super
);
2761 /*******************************************************************************
2762 * function: imsm_create_metadata_checkpoint_update
2763 * Description: It creates update for checkpoint change.
2765 * super : imsm internal array info
2766 * u : pointer to prepared update
2769 * If length is equal to 0, input pointer u contains no update
2770 ******************************************************************************/
2771 static int imsm_create_metadata_checkpoint_update(
2772 struct intel_super
*super
,
2773 struct imsm_update_general_migration_checkpoint
**u
)
2776 int update_memory_size
= 0;
2778 dprintf("(enter)\n");
2784 /* size of all update data without anchor */
2785 update_memory_size
=
2786 sizeof(struct imsm_update_general_migration_checkpoint
);
2788 *u
= xcalloc(1, update_memory_size
);
2790 dprintf("error: cannot get memory\n");
2793 (*u
)->type
= update_general_migration_checkpoint
;
2794 (*u
)->curr_migr_unit
= __le32_to_cpu(super
->migr_rec
->curr_migr_unit
);
2795 dprintf("prepared for %u\n", (*u
)->curr_migr_unit
);
2797 return update_memory_size
;
2800 static void imsm_update_metadata_locally(struct supertype
*st
,
2801 void *buf
, int len
);
2803 /*******************************************************************************
2804 * Function: write_imsm_migr_rec
2805 * Description: Function writes imsm migration record
2806 * (at the last sector of disk)
2808 * super : imsm internal array info
2812 ******************************************************************************/
2813 static int write_imsm_migr_rec(struct supertype
*st
)
2815 struct intel_super
*super
= st
->sb
;
2816 unsigned int sector_size
= super
->sector_size
;
2817 unsigned long long dsize
;
2823 struct imsm_update_general_migration_checkpoint
*u
;
2824 struct imsm_dev
*dev
;
2825 struct imsm_map
*map
;
2827 /* find map under migration */
2828 dev
= imsm_get_device_during_migration(super
);
2829 /* if no migration, write buffer anyway to clear migr_record
2830 * on disk based on first available device
2833 dev
= get_imsm_dev(super
, super
->current_vol
< 0 ? 0 :
2834 super
->current_vol
);
2836 map
= get_imsm_map(dev
, MAP_0
);
2838 if (sector_size
== 4096)
2839 convert_to_4k_imsm_migr_rec(super
);
2840 for (sd
= super
->disks
; sd
; sd
= sd
->next
) {
2843 /* skip failed and spare devices */
2846 /* write to 2 first slots only */
2848 slot
= get_imsm_disk_slot(map
, sd
->index
);
2849 if (map
== NULL
|| slot
> 1 || slot
< 0)
2852 sprintf(nm
, "%d:%d", sd
->major
, sd
->minor
);
2853 fd
= dev_open(nm
, O_RDWR
);
2856 get_dev_size(fd
, NULL
, &dsize
);
2857 if (lseek64(fd
, dsize
- (MIGR_REC_SECTOR_POSITION
*sector_size
),
2859 pr_err("Cannot seek to anchor block: %s\n",
2863 if (write(fd
, super
->migr_rec_buf
,
2864 MIGR_REC_BUF_SECTORS
*sector_size
) !=
2865 MIGR_REC_BUF_SECTORS
*sector_size
) {
2866 pr_err("Cannot write migr record block: %s\n",
2873 if (sector_size
== 4096)
2874 convert_from_4k_imsm_migr_rec(super
);
2875 /* update checkpoint information in metadata */
2876 len
= imsm_create_metadata_checkpoint_update(super
, &u
);
2878 dprintf("imsm: Cannot prepare update\n");
2881 /* update metadata locally */
2882 imsm_update_metadata_locally(st
, u
, len
);
2883 /* and possibly remotely */
2884 if (st
->update_tail
) {
2885 append_metadata_update(st
, u
, len
);
2886 /* during reshape we do all work inside metadata handler
2887 * manage_reshape(), so metadata update has to be triggered
2890 flush_metadata_updates(st
);
2891 st
->update_tail
= &st
->updates
;
2901 #endif /* MDASSEMBLE */
2903 /* spare/missing disks activations are not allowe when
2904 * array/container performs reshape operation, because
2905 * all arrays in container works on the same disks set
2907 int imsm_reshape_blocks_arrays_changes(struct intel_super
*super
)
2910 struct intel_dev
*i_dev
;
2911 struct imsm_dev
*dev
;
2913 /* check whole container
2915 for (i_dev
= super
->devlist
; i_dev
; i_dev
= i_dev
->next
) {
2917 if (is_gen_migration(dev
)) {
2918 /* No repair during any migration in container
2926 static unsigned long long imsm_component_size_aligment_check(int level
,
2928 unsigned int sector_size
,
2929 unsigned long long component_size
)
2931 unsigned int component_size_alligment
;
2933 /* check component size aligment
2935 component_size_alligment
= component_size
% (chunk_size
/sector_size
);
2937 dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alligment = %u\n",
2938 level
, chunk_size
, component_size
,
2939 component_size_alligment
);
2941 if (component_size_alligment
&& (level
!= 1) && (level
!= UnSet
)) {
2942 dprintf("imsm: reported component size alligned from %llu ",
2944 component_size
-= component_size_alligment
;
2945 dprintf_cont("to %llu (%i).\n",
2946 component_size
, component_size_alligment
);
2949 return component_size
;
2952 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
2954 struct intel_super
*super
= st
->sb
;
2955 struct migr_record
*migr_rec
= super
->migr_rec
;
2956 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
2957 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2958 struct imsm_map
*prev_map
= get_imsm_map(dev
, MAP_1
);
2959 struct imsm_map
*map_to_analyse
= map
;
2961 int map_disks
= info
->array
.raid_disks
;
2963 memset(info
, 0, sizeof(*info
));
2965 map_to_analyse
= prev_map
;
2967 dl
= super
->current_disk
;
2969 info
->container_member
= super
->current_vol
;
2970 info
->array
.raid_disks
= map
->num_members
;
2971 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
2972 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
2973 info
->array
.md_minor
= -1;
2974 info
->array
.ctime
= 0;
2975 info
->array
.utime
= 0;
2976 info
->array
.chunk_size
=
2977 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
2978 info
->array
.state
= !dev
->vol
.dirty
;
2979 info
->custom_array_size
= __le32_to_cpu(dev
->size_high
);
2980 info
->custom_array_size
<<= 32;
2981 info
->custom_array_size
|= __le32_to_cpu(dev
->size_low
);
2982 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
2984 if (is_gen_migration(dev
)) {
2985 info
->reshape_active
= 1;
2986 info
->new_level
= get_imsm_raid_level(map
);
2987 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
2988 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
2989 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
2990 if (info
->delta_disks
) {
2991 /* this needs to be applied to every array
2994 info
->reshape_active
= CONTAINER_RESHAPE
;
2996 /* We shape information that we give to md might have to be
2997 * modify to cope with md's requirement for reshaping arrays.
2998 * For example, when reshaping a RAID0, md requires it to be
2999 * presented as a degraded RAID4.
3000 * Also if a RAID0 is migrating to a RAID5 we need to specify
3001 * the array as already being RAID5, but the 'before' layout
3002 * is a RAID4-like layout.
3004 switch (info
->array
.level
) {
3006 switch(info
->new_level
) {
3008 /* conversion is happening as RAID4 */
3009 info
->array
.level
= 4;
3010 info
->array
.raid_disks
+= 1;
3013 /* conversion is happening as RAID5 */
3014 info
->array
.level
= 5;
3015 info
->array
.layout
= ALGORITHM_PARITY_N
;
3016 info
->delta_disks
-= 1;
3019 /* FIXME error message */
3020 info
->array
.level
= UnSet
;
3026 info
->new_level
= UnSet
;
3027 info
->new_layout
= UnSet
;
3028 info
->new_chunk
= info
->array
.chunk_size
;
3029 info
->delta_disks
= 0;
3033 info
->disk
.major
= dl
->major
;
3034 info
->disk
.minor
= dl
->minor
;
3035 info
->disk
.number
= dl
->index
;
3036 info
->disk
.raid_disk
= get_imsm_disk_slot(map_to_analyse
,
3040 info
->data_offset
= pba_of_lba0(map_to_analyse
);
3042 if (info
->array
.level
== 5) {
3043 info
->component_size
= num_data_stripes(map_to_analyse
) *
3044 map_to_analyse
->blocks_per_strip
;
3046 info
->component_size
= blocks_per_member(map_to_analyse
);
3049 info
->component_size
= imsm_component_size_aligment_check(
3051 info
->array
.chunk_size
,
3053 info
->component_size
);
3054 info
->bb
.supported
= 0;
3056 memset(info
->uuid
, 0, sizeof(info
->uuid
));
3057 info
->recovery_start
= MaxSector
;
3059 info
->reshape_progress
= 0;
3060 info
->resync_start
= MaxSector
;
3061 if ((map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
3063 imsm_reshape_blocks_arrays_changes(super
) == 0) {
3064 info
->resync_start
= 0;
3066 if (dev
->vol
.migr_state
) {
3067 switch (migr_type(dev
)) {
3070 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
3072 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
3074 info
->resync_start
= blocks_per_unit
* units
;
3077 case MIGR_GEN_MIGR
: {
3078 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
3080 __u64 units
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
3081 unsigned long long array_blocks
;
3084 if (__le32_to_cpu(migr_rec
->ascending_migr
) &&
3086 (__le32_to_cpu(migr_rec
->num_migr_units
)-1)) &&
3087 (super
->migr_rec
->rec_status
==
3088 __cpu_to_le32(UNIT_SRC_IN_CP_AREA
)))
3091 info
->reshape_progress
= blocks_per_unit
* units
;
3093 dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n",
3094 (unsigned long long)units
,
3095 (unsigned long long)blocks_per_unit
,
3096 info
->reshape_progress
);
3098 used_disks
= imsm_num_data_members(dev
, MAP_1
);
3099 if (used_disks
> 0) {
3100 array_blocks
= blocks_per_member(map
) *
3102 /* round array size down to closest MB
3104 info
->custom_array_size
= (array_blocks
3105 >> SECT_PER_MB_SHIFT
)
3106 << SECT_PER_MB_SHIFT
;
3110 /* we could emulate the checkpointing of
3111 * 'sync_action=check' migrations, but for now
3112 * we just immediately complete them
3115 /* this is handled by container_content_imsm() */
3116 case MIGR_STATE_CHANGE
:
3117 /* FIXME handle other migrations */
3119 /* we are not dirty, so... */
3120 info
->resync_start
= MaxSector
;
3124 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
3125 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
3127 info
->array
.major_version
= -1;
3128 info
->array
.minor_version
= -2;
3129 sprintf(info
->text_version
, "/%s/%d", st
->container_devnm
, info
->container_member
);
3130 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
3131 uuid_from_super_imsm(st
, info
->uuid
);
3135 for (i
=0; i
<map_disks
; i
++) {
3137 if (i
< info
->array
.raid_disks
) {
3138 struct imsm_disk
*dsk
;
3139 j
= get_imsm_disk_idx(dev
, i
, MAP_X
);
3140 dsk
= get_imsm_disk(super
, j
);
3141 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
3148 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
3149 int failed
, int look_in_map
);
3151 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
3155 static void manage_second_map(struct intel_super
*super
, struct imsm_dev
*dev
)
3157 if (is_gen_migration(dev
)) {
3160 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
3162 failed
= imsm_count_failed(super
, dev
, MAP_1
);
3163 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_1
);
3164 if (map2
->map_state
!= map_state
) {
3165 map2
->map_state
= map_state
;
3166 super
->updates_pending
++;
3172 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
3176 for (d
= super
->missing
; d
; d
= d
->next
)
3177 if (d
->index
== index
)
3182 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
3184 struct intel_super
*super
= st
->sb
;
3185 struct imsm_disk
*disk
;
3186 int map_disks
= info
->array
.raid_disks
;
3187 int max_enough
= -1;
3189 struct imsm_super
*mpb
;
3191 if (super
->current_vol
>= 0) {
3192 getinfo_super_imsm_volume(st
, info
, map
);
3195 memset(info
, 0, sizeof(*info
));
3197 /* Set raid_disks to zero so that Assemble will always pull in valid
3200 info
->array
.raid_disks
= 0;
3201 info
->array
.level
= LEVEL_CONTAINER
;
3202 info
->array
.layout
= 0;
3203 info
->array
.md_minor
= -1;
3204 info
->array
.ctime
= 0; /* N/A for imsm */
3205 info
->array
.utime
= 0;
3206 info
->array
.chunk_size
= 0;
3208 info
->disk
.major
= 0;
3209 info
->disk
.minor
= 0;
3210 info
->disk
.raid_disk
= -1;
3211 info
->reshape_active
= 0;
3212 info
->array
.major_version
= -1;
3213 info
->array
.minor_version
= -2;
3214 strcpy(info
->text_version
, "imsm");
3215 info
->safe_mode_delay
= 0;
3216 info
->disk
.number
= -1;
3217 info
->disk
.state
= 0;
3219 info
->recovery_start
= MaxSector
;
3220 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
3221 info
->bb
.supported
= 0;
3223 /* do we have the all the insync disks that we expect? */
3224 mpb
= super
->anchor
;
3226 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3227 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3228 int failed
, enough
, j
, missing
= 0;
3229 struct imsm_map
*map
;
3232 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3233 state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3234 map
= get_imsm_map(dev
, MAP_0
);
3236 /* any newly missing disks?
3237 * (catches single-degraded vs double-degraded)
3239 for (j
= 0; j
< map
->num_members
; j
++) {
3240 __u32 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
3241 __u32 idx
= ord_to_idx(ord
);
3243 if (!(ord
& IMSM_ORD_REBUILD
) &&
3244 get_imsm_missing(super
, idx
)) {
3250 if (state
== IMSM_T_STATE_FAILED
)
3252 else if (state
== IMSM_T_STATE_DEGRADED
&&
3253 (state
!= map
->map_state
|| missing
))
3255 else /* we're normal, or already degraded */
3257 if (is_gen_migration(dev
) && missing
) {
3258 /* during general migration we need all disks
3259 * that process is running on.
3260 * No new missing disk is allowed.
3264 /* no more checks necessary
3268 /* in the missing/failed disk case check to see
3269 * if at least one array is runnable
3271 max_enough
= max(max_enough
, enough
);
3273 dprintf("enough: %d\n", max_enough
);
3274 info
->container_enough
= max_enough
;
3277 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
3279 disk
= &super
->disks
->disk
;
3280 info
->data_offset
= total_blocks(&super
->disks
->disk
) - reserved
;
3281 info
->component_size
= reserved
;
3282 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
3283 /* we don't change info->disk.raid_disk here because
3284 * this state will be finalized in mdmon after we have
3285 * found the 'most fresh' version of the metadata
3287 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3288 info
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
3291 /* only call uuid_from_super_imsm when this disk is part of a populated container,
3292 * ->compare_super may have updated the 'num_raid_devs' field for spares
3294 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
3295 uuid_from_super_imsm(st
, info
->uuid
);
3297 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
3299 /* I don't know how to compute 'map' on imsm, so use safe default */
3302 for (i
= 0; i
< map_disks
; i
++)
3308 /* allocates memory and fills disk in mdinfo structure
3309 * for each disk in array */
3310 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
3312 struct mdinfo
*mddev
;
3313 struct intel_super
*super
= st
->sb
;
3314 struct imsm_disk
*disk
;
3317 if (!super
|| !super
->disks
)
3320 mddev
= xcalloc(1, sizeof(*mddev
));
3324 tmp
= xcalloc(1, sizeof(*tmp
));
3326 tmp
->next
= mddev
->devs
;
3328 tmp
->disk
.number
= count
++;
3329 tmp
->disk
.major
= dl
->major
;
3330 tmp
->disk
.minor
= dl
->minor
;
3331 tmp
->disk
.state
= is_configured(disk
) ?
3332 (1 << MD_DISK_ACTIVE
) : 0;
3333 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3334 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
3335 tmp
->disk
.raid_disk
= -1;
3341 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
3342 char *update
, char *devname
, int verbose
,
3343 int uuid_set
, char *homehost
)
3345 /* For 'assemble' and 'force' we need to return non-zero if any
3346 * change was made. For others, the return value is ignored.
3347 * Update options are:
3348 * force-one : This device looks a bit old but needs to be included,
3349 * update age info appropriately.
3350 * assemble: clear any 'faulty' flag to allow this device to
3352 * force-array: Array is degraded but being forced, mark it clean
3353 * if that will be needed to assemble it.
3355 * newdev: not used ????
3356 * grow: Array has gained a new device - this is currently for
3358 * resync: mark as dirty so a resync will happen.
3359 * name: update the name - preserving the homehost
3360 * uuid: Change the uuid of the array to match watch is given
3362 * Following are not relevant for this imsm:
3363 * sparc2.2 : update from old dodgey metadata
3364 * super-minor: change the preferred_minor number
3365 * summaries: update redundant counters.
3366 * homehost: update the recorded homehost
3367 * _reshape_progress: record new reshape_progress position.
3370 struct intel_super
*super
= st
->sb
;
3371 struct imsm_super
*mpb
;
3373 /* we can only update container info */
3374 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
3377 mpb
= super
->anchor
;
3379 if (strcmp(update
, "uuid") == 0) {
3380 /* We take this to mean that the family_num should be updated.
3381 * However that is much smaller than the uuid so we cannot really
3382 * allow an explicit uuid to be given. And it is hard to reliably
3384 * So if !uuid_set we know the current uuid is random and just used
3385 * the first 'int' and copy it to the other 3 positions.
3386 * Otherwise we require the 4 'int's to be the same as would be the
3387 * case if we are using a random uuid. So an explicit uuid will be
3388 * accepted as long as all for ints are the same... which shouldn't hurt
3391 info
->uuid
[1] = info
->uuid
[2] = info
->uuid
[3] = info
->uuid
[0];
3394 if (info
->uuid
[0] != info
->uuid
[1] ||
3395 info
->uuid
[1] != info
->uuid
[2] ||
3396 info
->uuid
[2] != info
->uuid
[3])
3402 mpb
->orig_family_num
= info
->uuid
[0];
3403 } else if (strcmp(update
, "assemble") == 0)
3408 /* successful update? recompute checksum */
3410 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
3415 static size_t disks_to_mpb_size(int disks
)
3419 size
= sizeof(struct imsm_super
);
3420 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
3421 size
+= 2 * sizeof(struct imsm_dev
);
3422 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
3423 size
+= (4 - 2) * sizeof(struct imsm_map
);
3424 /* 4 possible disk_ord_tbl's */
3425 size
+= 4 * (disks
- 1) * sizeof(__u32
);
3426 /* maximum bbm log */
3427 size
+= sizeof(struct bbm_log
);
3432 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
,
3433 unsigned long long data_offset
)
3435 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
3438 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
3441 static void free_devlist(struct intel_super
*super
)
3443 struct intel_dev
*dv
;
3445 while (super
->devlist
) {
3446 dv
= super
->devlist
->next
;
3447 free(super
->devlist
->dev
);
3448 free(super
->devlist
);
3449 super
->devlist
= dv
;
3453 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
3455 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
3458 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
3462 * 0 same, or first was empty, and second was copied
3463 * 1 second had wrong number
3465 * 3 wrong other info
3467 struct intel_super
*first
= st
->sb
;
3468 struct intel_super
*sec
= tst
->sb
;
3475 /* in platform dependent environment test if the disks
3476 * use the same Intel hba
3477 * If not on Intel hba at all, allow anything.
3479 if (!check_env("IMSM_NO_PLATFORM") && first
->hba
&& sec
->hba
) {
3480 if (first
->hba
->type
!= sec
->hba
->type
) {
3482 "HBAs of devices do not match %s != %s\n",
3483 get_sys_dev_type(first
->hba
->type
),
3484 get_sys_dev_type(sec
->hba
->type
));
3487 if (first
->orom
!= sec
->orom
) {
3489 "HBAs of devices do not match %s != %s\n",
3490 first
->hba
->pci_id
, sec
->hba
->pci_id
);
3495 /* if an anchor does not have num_raid_devs set then it is a free
3498 if (first
->anchor
->num_raid_devs
> 0 &&
3499 sec
->anchor
->num_raid_devs
> 0) {
3500 /* Determine if these disks might ever have been
3501 * related. Further disambiguation can only take place
3502 * in load_super_imsm_all
3504 __u32 first_family
= first
->anchor
->orig_family_num
;
3505 __u32 sec_family
= sec
->anchor
->orig_family_num
;
3507 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
3508 MAX_SIGNATURE_LENGTH
) != 0)
3511 if (first_family
== 0)
3512 first_family
= first
->anchor
->family_num
;
3513 if (sec_family
== 0)
3514 sec_family
= sec
->anchor
->family_num
;
3516 if (first_family
!= sec_family
)
3521 /* if 'first' is a spare promote it to a populated mpb with sec's
3524 if (first
->anchor
->num_raid_devs
== 0 &&
3525 sec
->anchor
->num_raid_devs
> 0) {
3527 struct intel_dev
*dv
;
3528 struct imsm_dev
*dev
;
3530 /* we need to copy raid device info from sec if an allocation
3531 * fails here we don't associate the spare
3533 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
3534 dv
= xmalloc(sizeof(*dv
));
3535 dev
= xmalloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
3538 dv
->next
= first
->devlist
;
3539 first
->devlist
= dv
;
3541 if (i
< sec
->anchor
->num_raid_devs
) {
3542 /* allocation failure */
3543 free_devlist(first
);
3544 pr_err("imsm: failed to associate spare\n");
3547 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
3548 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
3549 first
->anchor
->family_num
= sec
->anchor
->family_num
;
3550 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
3551 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
3552 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
3558 static void fd2devname(int fd
, char *name
)
3562 char dname
[PATH_MAX
];
3567 if (fstat(fd
, &st
) != 0)
3569 sprintf(path
, "/sys/dev/block/%d:%d",
3570 major(st
.st_rdev
), minor(st
.st_rdev
));
3572 rv
= readlink(path
, dname
, sizeof(dname
)-1);
3577 nm
= strrchr(dname
, '/');
3580 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
3584 static int nvme_get_serial(int fd
, void *buf
, size_t buf_len
)
3587 char *name
= fd2kname(fd
);
3592 if (strncmp(name
, "nvme", 4) != 0)
3595 snprintf(path
, sizeof(path
) - 1, "/sys/block/%s/device/serial", name
);
3597 return load_sys(path
, buf
, buf_len
);
3600 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
3602 static int imsm_read_serial(int fd
, char *devname
,
3603 __u8 serial
[MAX_RAID_SERIAL_LEN
])
3612 memset(buf
, 0, sizeof(buf
));
3614 rv
= nvme_get_serial(fd
, buf
, sizeof(buf
));
3617 rv
= scsi_get_serial(fd
, buf
, sizeof(buf
));
3619 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
3620 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3621 fd2devname(fd
, (char *) serial
);
3627 pr_err("Failed to retrieve serial for %s\n",
3632 /* trim all whitespace and non-printable characters and convert
3635 for (i
= 0, dest
= buf
; i
< sizeof(buf
) && buf
[i
]; i
++) {
3638 /* ':' is reserved for use in placeholder serial
3639 * numbers for missing disks
3650 /* truncate leading characters */
3651 if (len
> MAX_RAID_SERIAL_LEN
) {
3652 dest
+= len
- MAX_RAID_SERIAL_LEN
;
3653 len
= MAX_RAID_SERIAL_LEN
;
3656 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3657 memcpy(serial
, dest
, len
);
3662 static int serialcmp(__u8
*s1
, __u8
*s2
)
3664 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
3667 static void serialcpy(__u8
*dest
, __u8
*src
)
3669 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
3672 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
3676 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3677 if (serialcmp(dl
->serial
, serial
) == 0)
3683 static struct imsm_disk
*
3684 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
3688 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3689 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3691 if (serialcmp(disk
->serial
, serial
) == 0) {
3702 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3704 struct imsm_disk
*disk
;
3709 __u8 serial
[MAX_RAID_SERIAL_LEN
];
3711 rv
= imsm_read_serial(fd
, devname
, serial
);
3716 dl
= xcalloc(1, sizeof(*dl
));
3719 dl
->major
= major(stb
.st_rdev
);
3720 dl
->minor
= minor(stb
.st_rdev
);
3721 dl
->next
= super
->disks
;
3722 dl
->fd
= keep_fd
? fd
: -1;
3723 assert(super
->disks
== NULL
);
3725 serialcpy(dl
->serial
, serial
);
3728 fd2devname(fd
, name
);
3730 dl
->devname
= xstrdup(devname
);
3732 dl
->devname
= xstrdup(name
);
3734 /* look up this disk's index in the current anchor */
3735 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
3738 /* only set index on disks that are a member of a
3739 * populated contianer, i.e. one with raid_devs
3741 if (is_failed(&dl
->disk
))
3743 else if (is_spare(&dl
->disk
))
3751 /* When migrating map0 contains the 'destination' state while map1
3752 * contains the current state. When not migrating map0 contains the
3753 * current state. This routine assumes that map[0].map_state is set to
3754 * the current array state before being called.
3756 * Migration is indicated by one of the following states
3757 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
3758 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
3759 * map1state=unitialized)
3760 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
3762 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
3763 * map1state=degraded)
3764 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
3767 static void migrate(struct imsm_dev
*dev
, struct intel_super
*super
,
3768 __u8 to_state
, int migr_type
)
3770 struct imsm_map
*dest
;
3771 struct imsm_map
*src
= get_imsm_map(dev
, MAP_0
);
3773 dev
->vol
.migr_state
= 1;
3774 set_migr_type(dev
, migr_type
);
3775 dev
->vol
.curr_migr_unit
= 0;
3776 dest
= get_imsm_map(dev
, MAP_1
);
3778 /* duplicate and then set the target end state in map[0] */
3779 memcpy(dest
, src
, sizeof_imsm_map(src
));
3780 if (migr_type
== MIGR_REBUILD
|| migr_type
== MIGR_GEN_MIGR
) {
3784 for (i
= 0; i
< src
->num_members
; i
++) {
3785 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
3786 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
3790 if (migr_type
== MIGR_GEN_MIGR
)
3791 /* Clear migration record */
3792 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
3794 src
->map_state
= to_state
;
3797 static void end_migration(struct imsm_dev
*dev
, struct intel_super
*super
,
3800 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3801 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
== 0 ?
3805 /* merge any IMSM_ORD_REBUILD bits that were not successfully
3806 * completed in the last migration.
3808 * FIXME add support for raid-level-migration
3810 if (map_state
!= map
->map_state
&& (is_gen_migration(dev
) == 0) &&
3811 prev
->map_state
!= IMSM_T_STATE_UNINITIALIZED
) {
3812 /* when final map state is other than expected
3813 * merge maps (not for migration)
3817 for (i
= 0; i
< prev
->num_members
; i
++)
3818 for (j
= 0; j
< map
->num_members
; j
++)
3819 /* during online capacity expansion
3820 * disks position can be changed
3821 * if takeover is used
3823 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
3824 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
3825 map
->disk_ord_tbl
[j
] |=
3826 prev
->disk_ord_tbl
[i
];
3829 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3830 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3833 dev
->vol
.migr_state
= 0;
3834 set_migr_type(dev
, 0);
3835 dev
->vol
.curr_migr_unit
= 0;
3836 map
->map_state
= map_state
;
3840 static int parse_raid_devices(struct intel_super
*super
)
3843 struct imsm_dev
*dev_new
;
3844 size_t len
, len_migr
;
3846 size_t space_needed
= 0;
3847 struct imsm_super
*mpb
= super
->anchor
;
3849 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3850 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3851 struct intel_dev
*dv
;
3853 len
= sizeof_imsm_dev(dev_iter
, 0);
3854 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
3856 space_needed
+= len_migr
- len
;
3858 dv
= xmalloc(sizeof(*dv
));
3859 if (max_len
< len_migr
)
3861 if (max_len
> len_migr
)
3862 space_needed
+= max_len
- len_migr
;
3863 dev_new
= xmalloc(max_len
);
3864 imsm_copy_dev(dev_new
, dev_iter
);
3867 dv
->next
= super
->devlist
;
3868 super
->devlist
= dv
;
3871 /* ensure that super->buf is large enough when all raid devices
3874 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
3877 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
,
3878 super
->sector_size
);
3879 if (posix_memalign(&buf
, MAX_SECTOR_SIZE
, len
) != 0)
3882 memcpy(buf
, super
->buf
, super
->len
);
3883 memset(buf
+ super
->len
, 0, len
- super
->len
);
3889 super
->extra_space
+= space_needed
;
3894 /*******************************************************************************
3895 * Function: check_mpb_migr_compatibility
3896 * Description: Function checks for unsupported migration features:
3897 * - migration optimization area (pba_of_lba0)
3898 * - descending reshape (ascending_migr)
3900 * super : imsm metadata information
3902 * 0 : migration is compatible
3903 * -1 : migration is not compatible
3904 ******************************************************************************/
3905 int check_mpb_migr_compatibility(struct intel_super
*super
)
3907 struct imsm_map
*map0
, *map1
;
3908 struct migr_record
*migr_rec
= super
->migr_rec
;
3911 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3912 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3915 dev_iter
->vol
.migr_state
== 1 &&
3916 dev_iter
->vol
.migr_type
== MIGR_GEN_MIGR
) {
3917 /* This device is migrating */
3918 map0
= get_imsm_map(dev_iter
, MAP_0
);
3919 map1
= get_imsm_map(dev_iter
, MAP_1
);
3920 if (pba_of_lba0(map0
) != pba_of_lba0(map1
))
3921 /* migration optimization area was used */
3923 if (migr_rec
->ascending_migr
== 0
3924 && migr_rec
->dest_depth_per_unit
> 0)
3925 /* descending reshape not supported yet */
3932 static void __free_imsm(struct intel_super
*super
, int free_disks
);
3934 /* load_imsm_mpb - read matrix metadata
3935 * allocates super->mpb to be freed by free_imsm
3937 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
3939 unsigned long long dsize
;
3940 unsigned long long sectors
;
3941 unsigned int sector_size
= super
->sector_size
;
3943 struct imsm_super
*anchor
;
3946 get_dev_size(fd
, NULL
, &dsize
);
3947 if (dsize
< 2*sector_size
) {
3949 pr_err("%s: device to small for imsm\n",
3954 if (lseek64(fd
, dsize
- (sector_size
* 2), SEEK_SET
) < 0) {
3956 pr_err("Cannot seek to anchor block on %s: %s\n",
3957 devname
, strerror(errno
));
3961 if (posix_memalign((void **)&anchor
, sector_size
, sector_size
) != 0) {
3963 pr_err("Failed to allocate imsm anchor buffer on %s\n", devname
);
3966 if (read(fd
, anchor
, sector_size
) != sector_size
) {
3968 pr_err("Cannot read anchor block on %s: %s\n",
3969 devname
, strerror(errno
));
3974 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
3976 pr_err("no IMSM anchor on %s\n", devname
);
3981 __free_imsm(super
, 0);
3982 /* reload capability and hba */
3984 /* capability and hba must be updated with new super allocation */
3985 find_intel_hba_capability(fd
, super
, devname
);
3986 super
->len
= ROUND_UP(anchor
->mpb_size
, sector_size
);
3987 if (posix_memalign(&super
->buf
, MAX_SECTOR_SIZE
, super
->len
) != 0) {
3989 pr_err("unable to allocate %zu byte mpb buffer\n",
3994 memcpy(super
->buf
, anchor
, sector_size
);
3996 sectors
= mpb_sectors(anchor
, sector_size
) - 1;
3999 if (posix_memalign(&super
->migr_rec_buf
, sector_size
,
4000 MIGR_REC_BUF_SECTORS
*sector_size
) != 0) {
4001 pr_err("could not allocate migr_rec buffer\n");
4005 super
->clean_migration_record_by_mdmon
= 0;
4008 check_sum
= __gen_imsm_checksum(super
->anchor
);
4009 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
4011 pr_err("IMSM checksum %x != %x on %s\n",
4013 __le32_to_cpu(super
->anchor
->check_sum
),
4021 /* read the extended mpb */
4022 if (lseek64(fd
, dsize
- (sector_size
* (2 + sectors
)), SEEK_SET
) < 0) {
4024 pr_err("Cannot seek to extended mpb on %s: %s\n",
4025 devname
, strerror(errno
));
4029 if ((unsigned int)read(fd
, super
->buf
+ sector_size
,
4030 super
->len
- sector_size
) != super
->len
- sector_size
) {
4032 pr_err("Cannot read extended mpb on %s: %s\n",
4033 devname
, strerror(errno
));
4037 check_sum
= __gen_imsm_checksum(super
->anchor
);
4038 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
4040 pr_err("IMSM checksum %x != %x on %s\n",
4041 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
4049 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
);
4051 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
4052 static void clear_hi(struct intel_super
*super
)
4054 struct imsm_super
*mpb
= super
->anchor
;
4056 if (mpb
->attributes
& MPB_ATTRIB_2TB_DISK
)
4058 for (i
= 0; i
< mpb
->num_disks
; ++i
) {
4059 struct imsm_disk
*disk
= &mpb
->disk
[i
];
4060 disk
->total_blocks_hi
= 0;
4062 for (i
= 0; i
< mpb
->num_raid_devs
; ++i
) {
4063 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4066 for (n
= 0; n
< 2; ++n
) {
4067 struct imsm_map
*map
= get_imsm_map(dev
, n
);
4070 map
->pba_of_lba0_hi
= 0;
4071 map
->blocks_per_member_hi
= 0;
4072 map
->num_data_stripes_hi
= 0;
4078 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
4082 err
= load_imsm_mpb(fd
, super
, devname
);
4085 if (super
->sector_size
== 4096)
4086 convert_from_4k(super
);
4087 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
4090 err
= parse_raid_devices(super
);
4093 err
= load_bbm_log(super
);
4098 static void __free_imsm_disk(struct dl
*d
)
4110 static void free_imsm_disks(struct intel_super
*super
)
4114 while (super
->disks
) {
4116 super
->disks
= d
->next
;
4117 __free_imsm_disk(d
);
4119 while (super
->disk_mgmt_list
) {
4120 d
= super
->disk_mgmt_list
;
4121 super
->disk_mgmt_list
= d
->next
;
4122 __free_imsm_disk(d
);
4124 while (super
->missing
) {
4126 super
->missing
= d
->next
;
4127 __free_imsm_disk(d
);
4132 /* free all the pieces hanging off of a super pointer */
4133 static void __free_imsm(struct intel_super
*super
, int free_disks
)
4135 struct intel_hba
*elem
, *next
;
4141 /* unlink capability description */
4143 if (super
->migr_rec_buf
) {
4144 free(super
->migr_rec_buf
);
4145 super
->migr_rec_buf
= NULL
;
4148 free_imsm_disks(super
);
4149 free_devlist(super
);
4153 free((void *)elem
->path
);
4159 free(super
->bbm_log
);
4163 static void free_imsm(struct intel_super
*super
)
4165 __free_imsm(super
, 1);
4169 static void free_super_imsm(struct supertype
*st
)
4171 struct intel_super
*super
= st
->sb
;
4180 static struct intel_super
*alloc_super(void)
4182 struct intel_super
*super
= xcalloc(1, sizeof(*super
));
4184 super
->current_vol
= -1;
4185 super
->create_offset
= ~((unsigned long long) 0);
4190 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
4192 static int find_intel_hba_capability(int fd
, struct intel_super
*super
, char *devname
)
4194 struct sys_dev
*hba_name
;
4197 if (fd
< 0 || check_env("IMSM_NO_PLATFORM")) {
4202 hba_name
= find_disk_attached_hba(fd
, NULL
);
4205 pr_err("%s is not attached to Intel(R) RAID controller.\n",
4209 rv
= attach_hba_to_super(super
, hba_name
);
4212 struct intel_hba
*hba
= super
->hba
;
4214 pr_err("%s is attached to Intel(R) %s %s (%s),\n"
4215 " but the container is assigned to Intel(R) %s %s (",
4217 get_sys_dev_type(hba_name
->type
),
4218 hba_name
->type
== SYS_DEV_VMD
? "domain" : "RAID controller",
4219 hba_name
->pci_id
? : "Err!",
4220 get_sys_dev_type(super
->hba
->type
),
4221 hba
->type
== SYS_DEV_VMD
? "domain" : "RAID controller");
4224 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
4226 fprintf(stderr
, ", ");
4229 fprintf(stderr
, ").\n"
4230 " Mixing devices attached to different %s is not allowed.\n",
4231 hba_name
->type
== SYS_DEV_VMD
? "VMD domains" : "controllers");
4235 super
->orom
= find_imsm_capability(hba_name
);
4242 /* find_missing - helper routine for load_super_imsm_all that identifies
4243 * disks that have disappeared from the system. This routine relies on
4244 * the mpb being uptodate, which it is at load time.
4246 static int find_missing(struct intel_super
*super
)
4249 struct imsm_super
*mpb
= super
->anchor
;
4251 struct imsm_disk
*disk
;
4253 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4254 disk
= __get_imsm_disk(mpb
, i
);
4255 dl
= serial_to_dl(disk
->serial
, super
);
4259 dl
= xmalloc(sizeof(*dl
));
4263 dl
->devname
= xstrdup("missing");
4265 serialcpy(dl
->serial
, disk
->serial
);
4268 dl
->next
= super
->missing
;
4269 super
->missing
= dl
;
4276 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
4278 struct intel_disk
*idisk
= disk_list
;
4281 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
4283 idisk
= idisk
->next
;
4289 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
4290 struct intel_super
*super
,
4291 struct intel_disk
**disk_list
)
4293 struct imsm_disk
*d
= &super
->disks
->disk
;
4294 struct imsm_super
*mpb
= super
->anchor
;
4297 for (i
= 0; i
< tbl_size
; i
++) {
4298 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
4299 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
4301 if (tbl_mpb
->family_num
== mpb
->family_num
) {
4302 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
4303 dprintf("mpb from %d:%d matches %d:%d\n",
4304 super
->disks
->major
,
4305 super
->disks
->minor
,
4306 table
[i
]->disks
->major
,
4307 table
[i
]->disks
->minor
);
4311 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
4312 is_configured(d
) == is_configured(tbl_d
)) &&
4313 tbl_mpb
->generation_num
< mpb
->generation_num
) {
4314 /* current version of the mpb is a
4315 * better candidate than the one in
4316 * super_table, but copy over "cross
4317 * generational" status
4319 struct intel_disk
*idisk
;
4321 dprintf("mpb from %d:%d replaces %d:%d\n",
4322 super
->disks
->major
,
4323 super
->disks
->minor
,
4324 table
[i
]->disks
->major
,
4325 table
[i
]->disks
->minor
);
4327 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
4328 if (idisk
&& is_failed(&idisk
->disk
))
4329 tbl_d
->status
|= FAILED_DISK
;
4332 struct intel_disk
*idisk
;
4333 struct imsm_disk
*disk
;
4335 /* tbl_mpb is more up to date, but copy
4336 * over cross generational status before
4339 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
4340 if (disk
&& is_failed(disk
))
4341 d
->status
|= FAILED_DISK
;
4343 idisk
= disk_list_get(d
->serial
, *disk_list
);
4346 if (disk
&& is_configured(disk
))
4347 idisk
->disk
.status
|= CONFIGURED_DISK
;
4350 dprintf("mpb from %d:%d prefer %d:%d\n",
4351 super
->disks
->major
,
4352 super
->disks
->minor
,
4353 table
[i
]->disks
->major
,
4354 table
[i
]->disks
->minor
);
4362 table
[tbl_size
++] = super
;
4366 /* update/extend the merged list of imsm_disk records */
4367 for (j
= 0; j
< mpb
->num_disks
; j
++) {
4368 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
4369 struct intel_disk
*idisk
;
4371 idisk
= disk_list_get(disk
->serial
, *disk_list
);
4373 idisk
->disk
.status
|= disk
->status
;
4374 if (is_configured(&idisk
->disk
) ||
4375 is_failed(&idisk
->disk
))
4376 idisk
->disk
.status
&= ~(SPARE_DISK
);
4378 idisk
= xcalloc(1, sizeof(*idisk
));
4379 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
4380 idisk
->disk
= *disk
;
4381 idisk
->next
= *disk_list
;
4385 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
4392 static struct intel_super
*
4393 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
4396 struct imsm_super
*mpb
= super
->anchor
;
4400 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4401 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
4402 struct intel_disk
*idisk
;
4404 idisk
= disk_list_get(disk
->serial
, disk_list
);
4406 if (idisk
->owner
== owner
||
4407 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
4410 dprintf("'%.16s' owner %d != %d\n",
4411 disk
->serial
, idisk
->owner
,
4414 dprintf("unknown disk %x [%d]: %.16s\n",
4415 __le32_to_cpu(mpb
->family_num
), i
,
4421 if (ok_count
== mpb
->num_disks
)
4426 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
4428 struct intel_super
*s
;
4430 for (s
= super_list
; s
; s
= s
->next
) {
4431 if (family_num
!= s
->anchor
->family_num
)
4433 pr_err("Conflict, offlining family %#x on '%s'\n",
4434 __le32_to_cpu(family_num
), s
->disks
->devname
);
4438 static struct intel_super
*
4439 imsm_thunderdome(struct intel_super
**super_list
, int len
)
4441 struct intel_super
*super_table
[len
];
4442 struct intel_disk
*disk_list
= NULL
;
4443 struct intel_super
*champion
, *spare
;
4444 struct intel_super
*s
, **del
;
4449 memset(super_table
, 0, sizeof(super_table
));
4450 for (s
= *super_list
; s
; s
= s
->next
)
4451 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
4453 for (i
= 0; i
< tbl_size
; i
++) {
4454 struct imsm_disk
*d
;
4455 struct intel_disk
*idisk
;
4456 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
4459 d
= &s
->disks
->disk
;
4461 /* 'd' must appear in merged disk list for its
4462 * configuration to be valid
4464 idisk
= disk_list_get(d
->serial
, disk_list
);
4465 if (idisk
&& idisk
->owner
== i
)
4466 s
= validate_members(s
, disk_list
, i
);
4471 dprintf("marking family: %#x from %d:%d offline\n",
4473 super_table
[i
]->disks
->major
,
4474 super_table
[i
]->disks
->minor
);
4478 /* This is where the mdadm implementation differs from the Windows
4479 * driver which has no strict concept of a container. We can only
4480 * assemble one family from a container, so when returning a prodigal
4481 * array member to this system the code will not be able to disambiguate
4482 * the container contents that should be assembled ("foreign" versus
4483 * "local"). It requires user intervention to set the orig_family_num
4484 * to a new value to establish a new container. The Windows driver in
4485 * this situation fixes up the volume name in place and manages the
4486 * foreign array as an independent entity.
4491 for (i
= 0; i
< tbl_size
; i
++) {
4492 struct intel_super
*tbl_ent
= super_table
[i
];
4498 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
4503 if (s
&& !is_spare
) {
4504 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
4506 } else if (!s
&& !is_spare
)
4519 pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n",
4520 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
4522 /* collect all dl's onto 'champion', and update them to
4523 * champion's version of the status
4525 for (s
= *super_list
; s
; s
= s
->next
) {
4526 struct imsm_super
*mpb
= champion
->anchor
;
4527 struct dl
*dl
= s
->disks
;
4532 mpb
->attributes
|= s
->anchor
->attributes
& MPB_ATTRIB_2TB_DISK
;
4534 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4535 struct imsm_disk
*disk
;
4537 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
4540 /* only set index on disks that are a member of
4541 * a populated contianer, i.e. one with
4544 if (is_failed(&dl
->disk
))
4546 else if (is_spare(&dl
->disk
))
4552 if (i
>= mpb
->num_disks
) {
4553 struct intel_disk
*idisk
;
4555 idisk
= disk_list_get(dl
->serial
, disk_list
);
4556 if (idisk
&& is_spare(&idisk
->disk
) &&
4557 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
4565 dl
->next
= champion
->disks
;
4566 champion
->disks
= dl
;
4570 /* delete 'champion' from super_list */
4571 for (del
= super_list
; *del
; ) {
4572 if (*del
== champion
) {
4573 *del
= (*del
)->next
;
4576 del
= &(*del
)->next
;
4578 champion
->next
= NULL
;
4582 struct intel_disk
*idisk
= disk_list
;
4584 disk_list
= disk_list
->next
;
4592 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
);
4593 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
4594 int major
, int minor
, int keep_fd
);
4596 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4597 int *max
, int keep_fd
);
4599 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
4600 char *devname
, struct md_list
*devlist
,
4603 struct intel_super
*super_list
= NULL
;
4604 struct intel_super
*super
= NULL
;
4609 /* 'fd' is an opened container */
4610 err
= get_sra_super_block(fd
, &super_list
, devname
, &i
, keep_fd
);
4612 /* get super block from devlist devices */
4613 err
= get_devlist_super_block(devlist
, &super_list
, &i
, keep_fd
);
4616 /* all mpbs enter, maybe one leaves */
4617 super
= imsm_thunderdome(&super_list
, i
);
4623 if (find_missing(super
) != 0) {
4629 /* load migration record */
4630 err
= load_imsm_migr_rec(super
, NULL
);
4632 /* migration is in progress,
4633 * but migr_rec cannot be loaded,
4639 /* Check migration compatibility */
4640 if (err
== 0 && check_mpb_migr_compatibility(super
) != 0) {
4641 pr_err("Unsupported migration detected");
4643 fprintf(stderr
, " on %s\n", devname
);
4645 fprintf(stderr
, " (IMSM).\n");
4654 while (super_list
) {
4655 struct intel_super
*s
= super_list
;
4657 super_list
= super_list
->next
;
4666 strcpy(st
->container_devnm
, fd2devnm(fd
));
4668 st
->container_devnm
[0] = 0;
4669 if (err
== 0 && st
->ss
== NULL
) {
4670 st
->ss
= &super_imsm
;
4671 st
->minor_version
= 0;
4672 st
->max_devs
= IMSM_MAX_DEVICES
;
4678 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4679 int *max
, int keep_fd
)
4681 struct md_list
*tmpdev
;
4685 for (i
= 0, tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
4686 if (tmpdev
->used
!= 1)
4688 if (tmpdev
->container
== 1) {
4690 int fd
= dev_open(tmpdev
->devname
, O_RDONLY
|O_EXCL
);
4692 pr_err("cannot open device %s: %s\n",
4693 tmpdev
->devname
, strerror(errno
));
4697 err
= get_sra_super_block(fd
, super_list
,
4698 tmpdev
->devname
, &lmax
,
4707 int major
= major(tmpdev
->st_rdev
);
4708 int minor
= minor(tmpdev
->st_rdev
);
4709 err
= get_super_block(super_list
,
4726 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
4727 int major
, int minor
, int keep_fd
)
4729 struct intel_super
*s
;
4741 sprintf(nm
, "%d:%d", major
, minor
);
4742 dfd
= dev_open(nm
, O_RDWR
);
4748 get_dev_sector_size(dfd
, NULL
, &s
->sector_size
);
4749 find_intel_hba_capability(dfd
, s
, devname
);
4750 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4752 /* retry the load if we might have raced against mdmon */
4753 if (err
== 3 && devnm
&& mdmon_running(devnm
))
4754 for (retry
= 0; retry
< 3; retry
++) {
4756 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4762 s
->next
= *super_list
;
4770 if (dfd
>= 0 && !keep_fd
)
4777 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
)
4784 sra
= sysfs_read(fd
, NULL
, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
4788 if (sra
->array
.major_version
!= -1 ||
4789 sra
->array
.minor_version
!= -2 ||
4790 strcmp(sra
->text_version
, "imsm") != 0) {
4795 devnm
= fd2devnm(fd
);
4796 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
4797 if (get_super_block(super_list
, devnm
, devname
,
4798 sd
->disk
.major
, sd
->disk
.minor
, keep_fd
) != 0) {
4809 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
4811 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
, NULL
, 1);
4815 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
4817 struct intel_super
*super
;
4821 if (test_partition(fd
))
4822 /* IMSM not allowed on partitions */
4825 free_super_imsm(st
);
4827 super
= alloc_super();
4828 get_dev_sector_size(fd
, NULL
, &super
->sector_size
);
4831 /* Load hba and capabilities if they exist.
4832 * But do not preclude loading metadata in case capabilities or hba are
4833 * non-compliant and ignore_hw_compat is set.
4835 rv
= find_intel_hba_capability(fd
, super
, devname
);
4836 /* no orom/efi or non-intel hba of the disk */
4837 if (rv
!= 0 && st
->ignore_hw_compat
== 0) {
4839 pr_err("No OROM/EFI properties for %s\n", devname
);
4843 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
4845 /* retry the load if we might have raced against mdmon */
4847 struct mdstat_ent
*mdstat
= NULL
;
4848 char *name
= fd2kname(fd
);
4851 mdstat
= mdstat_by_component(name
);
4853 if (mdstat
&& mdmon_running(mdstat
->devnm
) && getpid() != mdmon_pid(mdstat
->devnm
)) {
4854 for (retry
= 0; retry
< 3; retry
++) {
4856 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
4862 free_mdstat(mdstat
);
4867 pr_err("Failed to load all information sections on %s\n", devname
);
4873 if (st
->ss
== NULL
) {
4874 st
->ss
= &super_imsm
;
4875 st
->minor_version
= 0;
4876 st
->max_devs
= IMSM_MAX_DEVICES
;
4879 /* load migration record */
4880 if (load_imsm_migr_rec(super
, NULL
) == 0) {
4881 /* Check for unsupported migration features */
4882 if (check_mpb_migr_compatibility(super
) != 0) {
4883 pr_err("Unsupported migration detected");
4885 fprintf(stderr
, " on %s\n", devname
);
4887 fprintf(stderr
, " (IMSM).\n");
4895 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
4897 if (info
->level
== 1)
4899 return info
->chunk_size
>> 9;
4902 static unsigned long long info_to_blocks_per_member(mdu_array_info_t
*info
,
4903 unsigned long long size
)
4905 if (info
->level
== 1)
4908 return (size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
4911 static void imsm_update_version_info(struct intel_super
*super
)
4913 /* update the version and attributes */
4914 struct imsm_super
*mpb
= super
->anchor
;
4916 struct imsm_dev
*dev
;
4917 struct imsm_map
*map
;
4920 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4921 dev
= get_imsm_dev(super
, i
);
4922 map
= get_imsm_map(dev
, MAP_0
);
4923 if (__le32_to_cpu(dev
->size_high
) > 0)
4924 mpb
->attributes
|= MPB_ATTRIB_2TB
;
4926 /* FIXME detect when an array spans a port multiplier */
4928 mpb
->attributes
|= MPB_ATTRIB_PM
;
4931 if (mpb
->num_raid_devs
> 1 ||
4932 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
4933 version
= MPB_VERSION_ATTRIBS
;
4934 switch (get_imsm_raid_level(map
)) {
4935 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
4936 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
4937 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
4938 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
4941 if (map
->num_members
>= 5)
4942 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
4943 else if (dev
->status
== DEV_CLONE_N_GO
)
4944 version
= MPB_VERSION_CNG
;
4945 else if (get_imsm_raid_level(map
) == 5)
4946 version
= MPB_VERSION_RAID5
;
4947 else if (map
->num_members
>= 3)
4948 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
4949 else if (get_imsm_raid_level(map
) == 1)
4950 version
= MPB_VERSION_RAID1
;
4952 version
= MPB_VERSION_RAID0
;
4954 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
4958 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
4960 struct imsm_super
*mpb
= super
->anchor
;
4961 char *reason
= NULL
;
4964 if (strlen(name
) > MAX_RAID_SERIAL_LEN
)
4965 reason
= "must be 16 characters or less";
4967 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4968 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4970 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
4971 reason
= "already exists";
4976 if (reason
&& !quiet
)
4977 pr_err("imsm volume name %s\n", reason
);
4982 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
4983 unsigned long long size
, char *name
,
4984 char *homehost
, int *uuid
,
4985 long long data_offset
)
4987 /* We are creating a volume inside a pre-existing container.
4988 * so st->sb is already set.
4990 struct intel_super
*super
= st
->sb
;
4991 unsigned int sector_size
= super
->sector_size
;
4992 struct imsm_super
*mpb
= super
->anchor
;
4993 struct intel_dev
*dv
;
4994 struct imsm_dev
*dev
;
4995 struct imsm_vol
*vol
;
4996 struct imsm_map
*map
;
4997 int idx
= mpb
->num_raid_devs
;
4999 unsigned long long array_blocks
;
5000 size_t size_old
, size_new
;
5001 unsigned long long num_data_stripes
;
5003 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
5004 pr_err("This imsm-container already has the maximum of %d volumes\n", super
->orom
->vpa
);
5008 /* ensure the mpb is large enough for the new data */
5009 size_old
= __le32_to_cpu(mpb
->mpb_size
);
5010 size_new
= disks_to_mpb_size(info
->nr_disks
);
5011 if (size_new
> size_old
) {
5013 size_t size_round
= ROUND_UP(size_new
, sector_size
);
5015 if (posix_memalign(&mpb_new
, sector_size
, size_round
) != 0) {
5016 pr_err("could not allocate new mpb\n");
5019 if (posix_memalign(&super
->migr_rec_buf
, sector_size
,
5020 MIGR_REC_BUF_SECTORS
*sector_size
) != 0) {
5021 pr_err("could not allocate migr_rec buffer\n");
5027 memcpy(mpb_new
, mpb
, size_old
);
5030 super
->anchor
= mpb_new
;
5031 mpb
->mpb_size
= __cpu_to_le32(size_new
);
5032 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
5033 super
->len
= size_round
;
5035 super
->current_vol
= idx
;
5037 /* handle 'failed_disks' by either:
5038 * a) create dummy disk entries in the table if this the first
5039 * volume in the array. We add them here as this is the only
5040 * opportunity to add them. add_to_super_imsm_volume()
5041 * handles the non-failed disks and continues incrementing
5043 * b) validate that 'failed_disks' matches the current number
5044 * of missing disks if the container is populated
5046 if (super
->current_vol
== 0) {
5048 for (i
= 0; i
< info
->failed_disks
; i
++) {
5049 struct imsm_disk
*disk
;
5052 disk
= __get_imsm_disk(mpb
, i
);
5053 disk
->status
= CONFIGURED_DISK
| FAILED_DISK
;
5054 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
5055 snprintf((char *) disk
->serial
, MAX_RAID_SERIAL_LEN
,
5058 find_missing(super
);
5063 for (d
= super
->missing
; d
; d
= d
->next
)
5065 if (info
->failed_disks
> missing
) {
5066 pr_err("unable to add 'missing' disk to container\n");
5071 if (!check_name(super
, name
, 0))
5073 dv
= xmalloc(sizeof(*dv
));
5074 dev
= xcalloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
5075 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
5076 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
5077 info
->layout
, info
->chunk_size
,
5079 /* round array size down to closest MB */
5080 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
5082 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
5083 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
5084 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
5086 vol
->migr_state
= 0;
5087 set_migr_type(dev
, MIGR_INIT
);
5088 vol
->dirty
= !info
->state
;
5089 vol
->curr_migr_unit
= 0;
5090 map
= get_imsm_map(dev
, MAP_0
);
5091 set_pba_of_lba0(map
, super
->create_offset
);
5092 set_blocks_per_member(map
, info_to_blocks_per_member(info
, size
));
5093 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
5094 map
->failed_disk_num
= ~0;
5095 if (info
->level
> 0)
5096 map
->map_state
= (info
->state
? IMSM_T_STATE_NORMAL
5097 : IMSM_T_STATE_UNINITIALIZED
);
5099 map
->map_state
= info
->failed_disks
? IMSM_T_STATE_FAILED
:
5100 IMSM_T_STATE_NORMAL
;
5103 if (info
->level
== 1 && info
->raid_disks
> 2) {
5106 pr_err("imsm does not support more than 2 disksin a raid1 volume\n");
5110 map
->raid_level
= info
->level
;
5111 if (info
->level
== 10) {
5112 map
->raid_level
= 1;
5113 map
->num_domains
= info
->raid_disks
/ 2;
5114 } else if (info
->level
== 1)
5115 map
->num_domains
= info
->raid_disks
;
5117 map
->num_domains
= 1;
5119 /* info->size is only int so use the 'size' parameter instead */
5120 num_data_stripes
= (size
* 2) / info_to_blocks_per_strip(info
);
5121 num_data_stripes
/= map
->num_domains
;
5122 set_num_data_stripes(map
, num_data_stripes
);
5124 map
->num_members
= info
->raid_disks
;
5125 for (i
= 0; i
< map
->num_members
; i
++) {
5126 /* initialized in add_to_super */
5127 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
5129 mpb
->num_raid_devs
++;
5132 dv
->index
= super
->current_vol
;
5133 dv
->next
= super
->devlist
;
5134 super
->devlist
= dv
;
5136 imsm_update_version_info(super
);
5141 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
5142 unsigned long long size
, char *name
,
5143 char *homehost
, int *uuid
,
5144 unsigned long long data_offset
)
5146 /* This is primarily called by Create when creating a new array.
5147 * We will then get add_to_super called for each component, and then
5148 * write_init_super called to write it out to each device.
5149 * For IMSM, Create can create on fresh devices or on a pre-existing
5151 * To create on a pre-existing array a different method will be called.
5152 * This one is just for fresh drives.
5154 struct intel_super
*super
;
5155 struct imsm_super
*mpb
;
5159 if (data_offset
!= INVALID_SECTORS
) {
5160 pr_err("data-offset not supported by imsm\n");
5165 return init_super_imsm_volume(st
, info
, size
, name
, homehost
, uuid
,
5169 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
5171 mpb_size
= MAX_SECTOR_SIZE
;
5173 super
= alloc_super();
5175 posix_memalign(&super
->buf
, MAX_SECTOR_SIZE
, mpb_size
) != 0) {
5180 pr_err("could not allocate superblock\n");
5183 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
5184 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
) != 0) {
5185 pr_err("could not allocate migr_rec buffer\n");
5190 memset(super
->buf
, 0, mpb_size
);
5192 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5196 /* zeroing superblock */
5200 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5202 version
= (char *) mpb
->sig
;
5203 strcpy(version
, MPB_SIGNATURE
);
5204 version
+= strlen(MPB_SIGNATURE
);
5205 strcpy(version
, MPB_VERSION_RAID0
);
5211 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
5212 int fd
, char *devname
)
5214 struct intel_super
*super
= st
->sb
;
5215 struct imsm_super
*mpb
= super
->anchor
;
5216 struct imsm_disk
*_disk
;
5217 struct imsm_dev
*dev
;
5218 struct imsm_map
*map
;
5222 dev
= get_imsm_dev(super
, super
->current_vol
);
5223 map
= get_imsm_map(dev
, MAP_0
);
5225 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
5226 pr_err("%s: Cannot add spare devices to IMSM volume\n",
5232 /* we're doing autolayout so grab the pre-marked (in
5233 * validate_geometry) raid_disk
5235 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5236 if (dl
->raiddisk
== dk
->raid_disk
)
5239 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5240 if (dl
->major
== dk
->major
&&
5241 dl
->minor
== dk
->minor
)
5246 pr_err("%s is not a member of the same container\n", devname
);
5250 /* add a pristine spare to the metadata */
5251 if (dl
->index
< 0) {
5252 dl
->index
= super
->anchor
->num_disks
;
5253 super
->anchor
->num_disks
++;
5255 /* Check the device has not already been added */
5256 slot
= get_imsm_disk_slot(map
, dl
->index
);
5258 (get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
) & IMSM_ORD_REBUILD
) == 0) {
5259 pr_err("%s has been included in this array twice\n",
5263 set_imsm_ord_tbl_ent(map
, dk
->raid_disk
, dl
->index
);
5264 dl
->disk
.status
= CONFIGURED_DISK
;
5266 /* update size of 'missing' disks to be at least as large as the
5267 * largest acitve member (we only have dummy missing disks when
5268 * creating the first volume)
5270 if (super
->current_vol
== 0) {
5271 for (df
= super
->missing
; df
; df
= df
->next
) {
5272 if (total_blocks(&dl
->disk
) > total_blocks(&df
->disk
))
5273 set_total_blocks(&df
->disk
, total_blocks(&dl
->disk
));
5274 _disk
= __get_imsm_disk(mpb
, df
->index
);
5279 /* refresh unset/failed slots to point to valid 'missing' entries */
5280 for (df
= super
->missing
; df
; df
= df
->next
)
5281 for (slot
= 0; slot
< mpb
->num_disks
; slot
++) {
5282 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
5284 if ((ord
& IMSM_ORD_REBUILD
) == 0)
5286 set_imsm_ord_tbl_ent(map
, slot
, df
->index
| IMSM_ORD_REBUILD
);
5287 if (is_gen_migration(dev
)) {
5288 struct imsm_map
*map2
= get_imsm_map(dev
,
5290 int slot2
= get_imsm_disk_slot(map2
, df
->index
);
5291 if (slot2
< map2
->num_members
&& slot2
>= 0) {
5292 __u32 ord2
= get_imsm_ord_tbl_ent(dev
,
5295 if ((unsigned)df
->index
==
5297 set_imsm_ord_tbl_ent(map2
,
5303 dprintf("set slot:%d to missing disk:%d\n", slot
, df
->index
);
5307 /* if we are creating the first raid device update the family number */
5308 if (super
->current_vol
== 0) {
5310 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
5312 _disk
= __get_imsm_disk(mpb
, dl
->index
);
5313 if (!_dev
|| !_disk
) {
5314 pr_err("BUG mpb setup error\n");
5320 sum
+= __gen_imsm_checksum(mpb
);
5321 mpb
->family_num
= __cpu_to_le32(sum
);
5322 mpb
->orig_family_num
= mpb
->family_num
;
5324 super
->current_disk
= dl
;
5329 * Function marks disk as spare and restores disk serial
5330 * in case it was previously marked as failed by takeover operation
5332 * -1 : critical error
5333 * 0 : disk is marked as spare but serial is not set
5336 int mark_spare(struct dl
*disk
)
5338 __u8 serial
[MAX_RAID_SERIAL_LEN
];
5345 if (!imsm_read_serial(disk
->fd
, NULL
, serial
)) {
5346 /* Restore disk serial number, because takeover marks disk
5347 * as failed and adds to serial ':0' before it becomes
5350 serialcpy(disk
->serial
, serial
);
5351 serialcpy(disk
->disk
.serial
, serial
);
5354 disk
->disk
.status
= SPARE_DISK
;
5360 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
5361 int fd
, char *devname
,
5362 unsigned long long data_offset
)
5364 struct intel_super
*super
= st
->sb
;
5366 unsigned long long size
;
5367 unsigned int member_sector_size
;
5372 /* If we are on an RAID enabled platform check that the disk is
5373 * attached to the raid controller.
5374 * We do not need to test disks attachment for container based additions,
5375 * they shall be already tested when container was created/assembled.
5377 rv
= find_intel_hba_capability(fd
, super
, devname
);
5378 /* no orom/efi or non-intel hba of the disk */
5380 dprintf("capability: %p fd: %d ret: %d\n",
5381 super
->orom
, fd
, rv
);
5385 if (super
->current_vol
>= 0)
5386 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
5389 dd
= xcalloc(sizeof(*dd
), 1);
5390 dd
->major
= major(stb
.st_rdev
);
5391 dd
->minor
= minor(stb
.st_rdev
);
5392 dd
->devname
= devname
? xstrdup(devname
) : NULL
;
5395 dd
->action
= DISK_ADD
;
5396 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
5398 pr_err("failed to retrieve scsi serial, aborting\n");
5404 if (super
->hba
&& ((super
->hba
->type
== SYS_DEV_NVME
) ||
5405 (super
->hba
->type
== SYS_DEV_VMD
))) {
5407 char *devpath
= diskfd_to_devpath(fd
);
5408 char controller_path
[PATH_MAX
];
5411 pr_err("failed to get devpath, aborting\n");
5418 snprintf(controller_path
, PATH_MAX
-1, "%s/device", devpath
);
5421 if (devpath_to_vendor(controller_path
) == 0x8086) {
5423 * If Intel's NVMe drive has serial ended with
5424 * "-A","-B","-1" or "-2" it means that this is "x8"
5425 * device (double drive on single PCIe card).
5426 * User should be warned about potential data loss.
5428 for (i
= MAX_RAID_SERIAL_LEN
-1; i
> 0; i
--) {
5429 /* Skip empty character at the end */
5430 if (dd
->serial
[i
] == 0)
5433 if (((dd
->serial
[i
] == 'A') ||
5434 (dd
->serial
[i
] == 'B') ||
5435 (dd
->serial
[i
] == '1') ||
5436 (dd
->serial
[i
] == '2')) &&
5437 (dd
->serial
[i
-1] == '-'))
5438 pr_err("\tThe action you are about to take may put your data at risk.\n"
5439 "\tPlease note that x8 devices may consist of two separate x4 devices "
5440 "located on a single PCIe port.\n"
5441 "\tRAID 0 is the only supported configuration for this type of x8 device.\n");
5447 get_dev_size(fd
, NULL
, &size
);
5448 get_dev_sector_size(fd
, NULL
, &member_sector_size
);
5450 if (super
->sector_size
== 0) {
5451 /* this a first device, so sector_size is not set yet */
5452 super
->sector_size
= member_sector_size
;
5453 } else if (member_sector_size
!= super
->sector_size
) {
5454 pr_err("Mixing between different sector size is forbidden, aborting...\n");
5461 /* clear migr_rec when adding disk to container */
5462 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SECTORS
*super
->sector_size
);
5463 if (lseek64(fd
, size
- MIGR_REC_SECTOR_POSITION
*super
->sector_size
,
5465 if (write(fd
, super
->migr_rec_buf
,
5466 MIGR_REC_BUF_SECTORS
*super
->sector_size
) !=
5467 MIGR_REC_BUF_SECTORS
*super
->sector_size
)
5468 perror("Write migr_rec failed");
5472 serialcpy(dd
->disk
.serial
, dd
->serial
);
5473 set_total_blocks(&dd
->disk
, size
);
5474 if (__le32_to_cpu(dd
->disk
.total_blocks_hi
) > 0) {
5475 struct imsm_super
*mpb
= super
->anchor
;
5476 mpb
->attributes
|= MPB_ATTRIB_2TB_DISK
;
5479 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
5480 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
5482 dd
->disk
.scsi_id
= __cpu_to_le32(0);
5484 if (st
->update_tail
) {
5485 dd
->next
= super
->disk_mgmt_list
;
5486 super
->disk_mgmt_list
= dd
;
5488 dd
->next
= super
->disks
;
5490 super
->updates_pending
++;
5496 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
5498 struct intel_super
*super
= st
->sb
;
5501 /* remove from super works only in mdmon - for communication
5502 * manager - monitor. Check if communication memory buffer
5505 if (!st
->update_tail
) {
5506 pr_err("shall be used in mdmon context only\n");
5509 dd
= xcalloc(1, sizeof(*dd
));
5510 dd
->major
= dk
->major
;
5511 dd
->minor
= dk
->minor
;
5514 dd
->action
= DISK_REMOVE
;
5516 dd
->next
= super
->disk_mgmt_list
;
5517 super
->disk_mgmt_list
= dd
;
5522 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
5525 char buf
[MAX_SECTOR_SIZE
];
5526 struct imsm_super anchor
;
5527 } spare_record
__attribute__ ((aligned(MAX_SECTOR_SIZE
)));
5529 /* spare records have their own family number and do not have any defined raid
5532 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
5534 struct imsm_super
*mpb
= super
->anchor
;
5535 struct imsm_super
*spare
= &spare_record
.anchor
;
5539 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
));
5540 spare
->generation_num
= __cpu_to_le32(1UL);
5541 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5542 spare
->num_disks
= 1;
5543 spare
->num_raid_devs
= 0;
5544 spare
->cache_size
= mpb
->cache_size
;
5545 spare
->pwr_cycle_count
= __cpu_to_le32(1);
5547 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
5548 MPB_SIGNATURE MPB_VERSION_RAID0
);
5550 for (d
= super
->disks
; d
; d
= d
->next
) {
5554 spare
->disk
[0] = d
->disk
;
5555 if (__le32_to_cpu(d
->disk
.total_blocks_hi
) > 0)
5556 spare
->attributes
|= MPB_ATTRIB_2TB_DISK
;
5558 if (super
->sector_size
== 4096)
5559 convert_to_4k_imsm_disk(&spare
->disk
[0]);
5561 sum
= __gen_imsm_checksum(spare
);
5562 spare
->family_num
= __cpu_to_le32(sum
);
5563 spare
->orig_family_num
= 0;
5564 sum
= __gen_imsm_checksum(spare
);
5565 spare
->check_sum
= __cpu_to_le32(sum
);
5567 if (store_imsm_mpb(d
->fd
, spare
)) {
5568 pr_err("failed for device %d:%d %s\n",
5569 d
->major
, d
->minor
, strerror(errno
));
5581 static int write_super_imsm(struct supertype
*st
, int doclose
)
5583 struct intel_super
*super
= st
->sb
;
5584 unsigned int sector_size
= super
->sector_size
;
5585 struct imsm_super
*mpb
= super
->anchor
;
5591 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
5593 int clear_migration_record
= 1;
5596 /* 'generation' is incremented everytime the metadata is written */
5597 generation
= __le32_to_cpu(mpb
->generation_num
);
5599 mpb
->generation_num
= __cpu_to_le32(generation
);
5601 /* fix up cases where previous mdadm releases failed to set
5604 if (mpb
->orig_family_num
== 0)
5605 mpb
->orig_family_num
= mpb
->family_num
;
5607 for (d
= super
->disks
; d
; d
= d
->next
) {
5611 mpb
->disk
[d
->index
] = d
->disk
;
5615 for (d
= super
->missing
; d
; d
= d
->next
) {
5616 mpb
->disk
[d
->index
] = d
->disk
;
5619 mpb
->num_disks
= num_disks
;
5620 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
5622 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5623 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
5624 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
5626 imsm_copy_dev(dev
, dev2
);
5627 mpb_size
+= sizeof_imsm_dev(dev
, 0);
5629 if (is_gen_migration(dev2
))
5630 clear_migration_record
= 0;
5633 bbm_log_size
= get_imsm_bbm_log_size(super
->bbm_log
);
5636 memcpy((void *)mpb
+ mpb_size
, super
->bbm_log
, bbm_log_size
);
5637 mpb
->attributes
|= MPB_ATTRIB_BBM
;
5639 mpb
->attributes
&= ~MPB_ATTRIB_BBM
;
5641 super
->anchor
->bbm_log_size
= __cpu_to_le32(bbm_log_size
);
5642 mpb_size
+= bbm_log_size
;
5643 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5646 assert(super
->len
== 0 || mpb_size
<= super
->len
);
5649 /* recalculate checksum */
5650 sum
= __gen_imsm_checksum(mpb
);
5651 mpb
->check_sum
= __cpu_to_le32(sum
);
5653 if (super
->clean_migration_record_by_mdmon
) {
5654 clear_migration_record
= 1;
5655 super
->clean_migration_record_by_mdmon
= 0;
5657 if (clear_migration_record
)
5658 memset(super
->migr_rec_buf
, 0,
5659 MIGR_REC_BUF_SECTORS
*sector_size
);
5661 if (sector_size
== 4096)
5662 convert_to_4k(super
);
5664 /* write the mpb for disks that compose raid devices */
5665 for (d
= super
->disks
; d
; d
= d
->next
) {
5666 if (d
->index
< 0 || is_failed(&d
->disk
))
5669 if (clear_migration_record
) {
5670 unsigned long long dsize
;
5672 get_dev_size(d
->fd
, NULL
, &dsize
);
5673 if (lseek64(d
->fd
, dsize
- sector_size
,
5675 if (write(d
->fd
, super
->migr_rec_buf
,
5676 MIGR_REC_BUF_SECTORS
*sector_size
) !=
5677 MIGR_REC_BUF_SECTORS
*sector_size
)
5678 perror("Write migr_rec failed");
5682 if (store_imsm_mpb(d
->fd
, mpb
))
5684 "failed for device %d:%d (fd: %d)%s\n",
5686 d
->fd
, strerror(errno
));
5695 return write_super_imsm_spares(super
, doclose
);
5700 static int create_array(struct supertype
*st
, int dev_idx
)
5703 struct imsm_update_create_array
*u
;
5704 struct intel_super
*super
= st
->sb
;
5705 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
5706 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
5707 struct disk_info
*inf
;
5708 struct imsm_disk
*disk
;
5711 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
5712 sizeof(*inf
) * map
->num_members
;
5714 u
->type
= update_create_array
;
5715 u
->dev_idx
= dev_idx
;
5716 imsm_copy_dev(&u
->dev
, dev
);
5717 inf
= get_disk_info(u
);
5718 for (i
= 0; i
< map
->num_members
; i
++) {
5719 int idx
= get_imsm_disk_idx(dev
, i
, MAP_X
);
5721 disk
= get_imsm_disk(super
, idx
);
5723 disk
= get_imsm_missing(super
, idx
);
5724 serialcpy(inf
[i
].serial
, disk
->serial
);
5726 append_metadata_update(st
, u
, len
);
5731 static int mgmt_disk(struct supertype
*st
)
5733 struct intel_super
*super
= st
->sb
;
5735 struct imsm_update_add_remove_disk
*u
;
5737 if (!super
->disk_mgmt_list
)
5742 u
->type
= update_add_remove_disk
;
5743 append_metadata_update(st
, u
, len
);
5748 static int write_init_super_imsm(struct supertype
*st
)
5750 struct intel_super
*super
= st
->sb
;
5751 int current_vol
= super
->current_vol
;
5753 /* we are done with current_vol reset it to point st at the container */
5754 super
->current_vol
= -1;
5756 if (st
->update_tail
) {
5757 /* queue the recently created array / added disk
5758 * as a metadata update */
5761 /* determine if we are creating a volume or adding a disk */
5762 if (current_vol
< 0) {
5763 /* in the mgmt (add/remove) disk case we are running
5764 * in mdmon context, so don't close fd's
5766 return mgmt_disk(st
);
5768 rv
= create_array(st
, current_vol
);
5773 for (d
= super
->disks
; d
; d
= d
->next
)
5774 Kill(d
->devname
, NULL
, 0, -1, 1);
5775 return write_super_imsm(st
, 1);
5780 static int store_super_imsm(struct supertype
*st
, int fd
)
5782 struct intel_super
*super
= st
->sb
;
5783 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
5789 if (super
->sector_size
== 4096)
5790 convert_to_4k(super
);
5791 return store_imsm_mpb(fd
, mpb
);
5798 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
5799 int layout
, int raiddisks
, int chunk
,
5800 unsigned long long size
,
5801 unsigned long long data_offset
,
5803 unsigned long long *freesize
,
5807 unsigned long long ldsize
;
5808 struct intel_super
*super
;
5811 if (level
!= LEVEL_CONTAINER
)
5816 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
5819 pr_err("imsm: Cannot open %s: %s\n",
5820 dev
, strerror(errno
));
5823 if (!get_dev_size(fd
, dev
, &ldsize
)) {
5828 /* capabilities retrieve could be possible
5829 * note that there is no fd for the disks in array.
5831 super
= alloc_super();
5836 if (!get_dev_sector_size(fd
, NULL
, &super
->sector_size
)) {
5842 rv
= find_intel_hba_capability(fd
, super
, verbose
> 0 ? dev
: NULL
);
5846 fd2devname(fd
, str
);
5847 dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n",
5848 fd
, str
, super
->orom
, rv
, raiddisks
);
5850 /* no orom/efi or non-intel hba of the disk */
5857 if (raiddisks
> super
->orom
->tds
) {
5859 pr_err("%d exceeds maximum number of platform supported disks: %d\n",
5860 raiddisks
, super
->orom
->tds
);
5864 if ((super
->orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) == 0 &&
5865 (ldsize
>> 9) >> 32 > 0) {
5867 pr_err("%s exceeds maximum platform supported size\n", dev
);
5873 *freesize
= avail_size_imsm(st
, ldsize
>> 9, data_offset
);
5879 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
5881 const unsigned long long base_start
= e
[*idx
].start
;
5882 unsigned long long end
= base_start
+ e
[*idx
].size
;
5885 if (base_start
== end
)
5889 for (i
= *idx
; i
< num_extents
; i
++) {
5890 /* extend overlapping extents */
5891 if (e
[i
].start
>= base_start
&&
5892 e
[i
].start
<= end
) {
5895 if (e
[i
].start
+ e
[i
].size
> end
)
5896 end
= e
[i
].start
+ e
[i
].size
;
5897 } else if (e
[i
].start
> end
) {
5903 return end
- base_start
;
5906 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
5908 /* build a composite disk with all known extents and generate a new
5909 * 'maxsize' given the "all disks in an array must share a common start
5910 * offset" constraint
5912 struct extent
*e
= xcalloc(sum_extents
, sizeof(*e
));
5916 unsigned long long pos
;
5917 unsigned long long start
= 0;
5918 unsigned long long maxsize
;
5919 unsigned long reserve
;
5921 /* coalesce and sort all extents. also, check to see if we need to
5922 * reserve space between member arrays
5925 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5928 for (i
= 0; i
< dl
->extent_cnt
; i
++)
5931 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
5936 while (i
< sum_extents
) {
5937 e
[j
].start
= e
[i
].start
;
5938 e
[j
].size
= find_size(e
, &i
, sum_extents
);
5940 if (e
[j
-1].size
== 0)
5949 unsigned long long esize
;
5951 esize
= e
[i
].start
- pos
;
5952 if (esize
>= maxsize
) {
5957 pos
= e
[i
].start
+ e
[i
].size
;
5959 } while (e
[i
-1].size
);
5965 /* FIXME assumes volume at offset 0 is the first volume in a
5968 if (start_extent
> 0)
5969 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
5973 if (maxsize
< reserve
)
5976 super
->create_offset
= ~((unsigned long long) 0);
5977 if (start
+ reserve
> super
->create_offset
)
5978 return 0; /* start overflows create_offset */
5979 super
->create_offset
= start
+ reserve
;
5981 return maxsize
- reserve
;
5984 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
5986 if (level
< 0 || level
== 6 || level
== 4)
5989 /* if we have an orom prevent invalid raid levels */
5992 case 0: return imsm_orom_has_raid0(orom
);
5995 return imsm_orom_has_raid1e(orom
);
5996 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
5997 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
5998 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
6001 return 1; /* not on an Intel RAID platform so anything goes */
6007 active_arrays_by_format(char *name
, char* hba
, struct md_list
**devlist
,
6008 int dpa
, int verbose
)
6010 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
6011 struct mdstat_ent
*memb
;
6017 for (memb
= mdstat
; memb
; memb
= memb
->next
) {
6018 if (memb
->metadata_version
&&
6019 (strncmp(memb
->metadata_version
, "external:", 9) == 0) &&
6020 (strcmp(&memb
->metadata_version
[9], name
) == 0) &&
6021 !is_subarray(memb
->metadata_version
+9) &&
6023 struct dev_member
*dev
= memb
->members
;
6025 while(dev
&& (fd
< 0)) {
6026 char *path
= xmalloc(strlen(dev
->name
) + strlen("/dev/") + 1);
6027 num
= sprintf(path
, "%s%s", "/dev/", dev
->name
);
6029 fd
= open(path
, O_RDONLY
, 0);
6030 if (num
<= 0 || fd
< 0) {
6031 pr_vrb("Cannot open %s: %s\n",
6032 dev
->name
, strerror(errno
));
6038 if (fd
>= 0 && disk_attached_to_hba(fd
, hba
)) {
6039 struct mdstat_ent
*vol
;
6040 for (vol
= mdstat
; vol
; vol
= vol
->next
) {
6041 if (vol
->active
> 0 &&
6042 vol
->metadata_version
&&
6043 is_container_member(vol
, memb
->devnm
)) {
6048 if (*devlist
&& (found
< dpa
)) {
6049 dv
= xcalloc(1, sizeof(*dv
));
6050 dv
->devname
= xmalloc(strlen(memb
->devnm
) + strlen("/dev/") + 1);
6051 sprintf(dv
->devname
, "%s%s", "/dev/", memb
->devnm
);
6054 dv
->next
= *devlist
;
6062 free_mdstat(mdstat
);
6067 static struct md_list
*
6068 get_loop_devices(void)
6071 struct md_list
*devlist
= NULL
;
6074 for(i
= 0; i
< 12; i
++) {
6075 dv
= xcalloc(1, sizeof(*dv
));
6076 dv
->devname
= xmalloc(40);
6077 sprintf(dv
->devname
, "/dev/loop%d", i
);
6085 static struct md_list
*
6086 get_devices(const char *hba_path
)
6088 struct md_list
*devlist
= NULL
;
6095 devlist
= get_loop_devices();
6098 /* scroll through /sys/dev/block looking for devices attached to
6101 dir
= opendir("/sys/dev/block");
6102 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
6107 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
6109 path
= devt_to_devpath(makedev(major
, minor
));
6112 if (!path_attached_to_hba(path
, hba_path
)) {
6119 fd
= dev_open(ent
->d_name
, O_RDONLY
);
6121 fd2devname(fd
, buf
);
6124 pr_err("cannot open device: %s\n",
6129 dv
= xcalloc(1, sizeof(*dv
));
6130 dv
->devname
= xstrdup(buf
);
6137 devlist
= devlist
->next
;
6147 count_volumes_list(struct md_list
*devlist
, char *homehost
,
6148 int verbose
, int *found
)
6150 struct md_list
*tmpdev
;
6152 struct supertype
*st
;
6154 /* first walk the list of devices to find a consistent set
6155 * that match the criterea, if that is possible.
6156 * We flag the ones we like with 'used'.
6159 st
= match_metadata_desc_imsm("imsm");
6161 pr_vrb("cannot allocate memory for imsm supertype\n");
6165 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
6166 char *devname
= tmpdev
->devname
;
6168 struct supertype
*tst
;
6170 if (tmpdev
->used
> 1)
6172 tst
= dup_super(st
);
6174 pr_vrb("cannot allocate memory for imsm supertype\n");
6177 tmpdev
->container
= 0;
6178 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
6180 dprintf("cannot open device %s: %s\n",
6181 devname
, strerror(errno
));
6183 } else if (fstat(dfd
, &stb
)< 0) {
6185 dprintf("fstat failed for %s: %s\n",
6186 devname
, strerror(errno
));
6188 } else if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
6189 dprintf("%s is not a block device.\n",
6192 } else if (must_be_container(dfd
)) {
6193 struct supertype
*cst
;
6194 cst
= super_by_fd(dfd
, NULL
);
6196 dprintf("cannot recognize container type %s\n",
6199 } else if (tst
->ss
!= st
->ss
) {
6200 dprintf("non-imsm container - ignore it: %s\n",
6203 } else if (!tst
->ss
->load_container
||
6204 tst
->ss
->load_container(tst
, dfd
, NULL
))
6207 tmpdev
->container
= 1;
6210 cst
->ss
->free_super(cst
);
6212 tmpdev
->st_rdev
= stb
.st_rdev
;
6213 if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
6214 dprintf("no RAID superblock on %s\n",
6217 } else if (tst
->ss
->compare_super
== NULL
) {
6218 dprintf("Cannot assemble %s metadata on %s\n",
6219 tst
->ss
->name
, devname
);
6225 if (tmpdev
->used
== 2 || tmpdev
->used
== 4) {
6226 /* Ignore unrecognised devices during auto-assembly */
6231 tst
->ss
->getinfo_super(tst
, &info
, NULL
);
6233 if (st
->minor_version
== -1)
6234 st
->minor_version
= tst
->minor_version
;
6236 if (memcmp(info
.uuid
, uuid_zero
,
6237 sizeof(int[4])) == 0) {
6238 /* this is a floating spare. It cannot define
6239 * an array unless there are no more arrays of
6240 * this type to be found. It can be included
6241 * in an array of this type though.
6247 if (st
->ss
!= tst
->ss
||
6248 st
->minor_version
!= tst
->minor_version
||
6249 st
->ss
->compare_super(st
, tst
) != 0) {
6250 /* Some mismatch. If exactly one array matches this host,
6251 * we can resolve on that one.
6252 * Or, if we are auto assembling, we just ignore the second
6255 dprintf("superblock on %s doesn't match others - assembly aborted\n",
6261 dprintf("found: devname: %s\n", devname
);
6265 tst
->ss
->free_super(tst
);
6269 if ((err
= load_super_imsm_all(st
, -1, &st
->sb
, NULL
, devlist
, 0)) == 0) {
6270 struct mdinfo
*iter
, *head
= st
->ss
->container_content(st
, NULL
);
6271 for (iter
= head
; iter
; iter
= iter
->next
) {
6272 dprintf("content->text_version: %s vol\n",
6273 iter
->text_version
);
6274 if (iter
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
6275 /* do not assemble arrays with unsupported
6277 dprintf("Cannot activate member %s.\n",
6278 iter
->text_version
);
6285 dprintf("No valid super block on device list: err: %d %p\n",
6289 dprintf("no more devices to examine\n");
6292 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
6293 if (tmpdev
->used
== 1 && tmpdev
->found
) {
6295 if (count
< tmpdev
->found
)
6298 count
-= tmpdev
->found
;
6301 if (tmpdev
->used
== 1)
6306 st
->ss
->free_super(st
);
6311 count_volumes(struct intel_hba
*hba
, int dpa
, int verbose
)
6313 struct sys_dev
*idev
, *intel_devices
= find_intel_devices();
6315 const struct orom_entry
*entry
;
6316 struct devid_list
*dv
, *devid_list
;
6318 if (!hba
|| !hba
->path
)
6321 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
6322 if (strstr(idev
->path
, hba
->path
))
6326 if (!idev
|| !idev
->dev_id
)
6329 entry
= get_orom_entry_by_device_id(idev
->dev_id
);
6331 if (!entry
|| !entry
->devid_list
)
6334 devid_list
= entry
->devid_list
;
6335 for (dv
= devid_list
; dv
; dv
= dv
->next
) {
6336 struct md_list
*devlist
;
6337 struct sys_dev
*device
= device_by_id(dv
->devid
);
6342 hba_path
= device
->path
;
6346 devlist
= get_devices(hba_path
);
6347 /* if no intel devices return zero volumes */
6348 if (devlist
== NULL
)
6351 count
+= active_arrays_by_format("imsm", hba_path
, &devlist
, dpa
, verbose
);
6352 dprintf("path: %s active arrays: %d\n", hba_path
, count
);
6353 if (devlist
== NULL
)
6357 count
+= count_volumes_list(devlist
,
6361 dprintf("found %d count: %d\n", found
, count
);
6364 dprintf("path: %s total number of volumes: %d\n", hba_path
, count
);
6367 struct md_list
*dv
= devlist
;
6368 devlist
= devlist
->next
;
6376 static int imsm_default_chunk(const struct imsm_orom
*orom
)
6378 /* up to 512 if the plaform supports it, otherwise the platform max.
6379 * 128 if no platform detected
6381 int fs
= max(7, orom
? fls(orom
->sss
) : 0);
6383 return min(512, (1 << fs
));
6387 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
6388 int raiddisks
, int *chunk
, unsigned long long size
, int verbose
)
6390 /* check/set platform and metadata limits/defaults */
6391 if (super
->orom
&& raiddisks
> super
->orom
->dpa
) {
6392 pr_vrb("platform supports a maximum of %d disks per array\n",
6397 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
6398 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
6399 pr_vrb("platform does not support raid%d with %d disk%s\n",
6400 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
6404 if (*chunk
== 0 || *chunk
== UnSet
)
6405 *chunk
= imsm_default_chunk(super
->orom
);
6407 if (super
->orom
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
6408 pr_vrb("platform does not support a chunk size of: %d\n", *chunk
);
6412 if (layout
!= imsm_level_to_layout(level
)) {
6414 pr_vrb("imsm raid 5 only supports the left-asymmetric layout\n");
6415 else if (level
== 10)
6416 pr_vrb("imsm raid 10 only supports the n2 layout\n");
6418 pr_vrb("imsm unknown layout %#x for this raid level %d\n",
6423 if (super
->orom
&& (super
->orom
->attr
& IMSM_OROM_ATTR_2TB
) == 0 &&
6424 (calc_array_size(level
, raiddisks
, layout
, *chunk
, size
) >> 32) > 0) {
6425 pr_vrb("platform does not support a volume size over 2TB\n");
6432 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
6433 * FIX ME add ahci details
6435 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
6436 int layout
, int raiddisks
, int *chunk
,
6437 unsigned long long size
,
6438 unsigned long long data_offset
,
6440 unsigned long long *freesize
,
6444 struct intel_super
*super
= st
->sb
;
6445 struct imsm_super
*mpb
;
6447 unsigned long long pos
= 0;
6448 unsigned long long maxsize
;
6452 /* We must have the container info already read in. */
6456 mpb
= super
->anchor
;
6458 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, size
, verbose
)) {
6459 pr_err("RAID gemetry validation failed. Cannot proceed with the action(s).\n");
6463 /* General test: make sure there is space for
6464 * 'raiddisks' device extents of size 'size' at a given
6467 unsigned long long minsize
= size
;
6468 unsigned long long start_offset
= MaxSector
;
6471 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
6472 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6477 e
= get_extents(super
, dl
);
6480 unsigned long long esize
;
6481 esize
= e
[i
].start
- pos
;
6482 if (esize
>= minsize
)
6484 if (found
&& start_offset
== MaxSector
) {
6487 } else if (found
&& pos
!= start_offset
) {
6491 pos
= e
[i
].start
+ e
[i
].size
;
6493 } while (e
[i
-1].size
);
6498 if (dcnt
< raiddisks
) {
6500 pr_err("imsm: Not enough devices with space for this array (%d < %d)\n",
6507 /* This device must be a member of the set */
6508 if (stat(dev
, &stb
) < 0)
6510 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
6512 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6513 if (dl
->major
== (int)major(stb
.st_rdev
) &&
6514 dl
->minor
== (int)minor(stb
.st_rdev
))
6519 pr_err("%s is not in the same imsm set\n", dev
);
6521 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
6522 /* If a volume is present then the current creation attempt
6523 * cannot incorporate new spares because the orom may not
6524 * understand this configuration (all member disks must be
6525 * members of each array in the container).
6527 pr_err("%s is a spare and a volume is already defined for this container\n", dev
);
6528 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
6530 } else if (super
->orom
&& mpb
->num_raid_devs
> 0 &&
6531 mpb
->num_disks
!= raiddisks
) {
6532 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
6536 /* retrieve the largest free space block */
6537 e
= get_extents(super
, dl
);
6542 unsigned long long esize
;
6544 esize
= e
[i
].start
- pos
;
6545 if (esize
>= maxsize
)
6547 pos
= e
[i
].start
+ e
[i
].size
;
6549 } while (e
[i
-1].size
);
6554 pr_err("unable to determine free space for: %s\n",
6558 if (maxsize
< size
) {
6560 pr_err("%s not enough space (%llu < %llu)\n",
6561 dev
, maxsize
, size
);
6565 /* count total number of extents for merge */
6567 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6569 i
+= dl
->extent_cnt
;
6571 maxsize
= merge_extents(super
, i
);
6573 if (!check_env("IMSM_NO_PLATFORM") &&
6574 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6575 pr_err("attempting to create a second volume with size less then remaining space. Aborting...\n");
6579 if (maxsize
< size
|| maxsize
== 0) {
6582 pr_err("no free space left on device. Aborting...\n");
6584 pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n",
6590 *freesize
= maxsize
;
6593 int count
= count_volumes(super
->hba
,
6594 super
->orom
->dpa
, verbose
);
6595 if (super
->orom
->vphba
<= count
) {
6596 pr_vrb("platform does not support more than %d raid volumes.\n",
6597 super
->orom
->vphba
);
6604 static int imsm_get_free_size(struct supertype
*st
, int raiddisks
,
6605 unsigned long long size
, int chunk
,
6606 unsigned long long *freesize
)
6608 struct intel_super
*super
= st
->sb
;
6609 struct imsm_super
*mpb
= super
->anchor
;
6614 unsigned long long maxsize
;
6615 unsigned long long minsize
;
6619 /* find the largest common start free region of the possible disks */
6623 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6629 /* don't activate new spares if we are orom constrained
6630 * and there is already a volume active in the container
6632 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
6635 e
= get_extents(super
, dl
);
6638 for (i
= 1; e
[i
-1].size
; i
++)
6646 maxsize
= merge_extents(super
, extent_cnt
);
6650 minsize
= chunk
* 2;
6652 if (cnt
< raiddisks
||
6653 (super
->orom
&& used
&& used
!= raiddisks
) ||
6654 maxsize
< minsize
||
6656 pr_err("not enough devices with space to create array.\n");
6657 return 0; /* No enough free spaces large enough */
6668 if (!check_env("IMSM_NO_PLATFORM") &&
6669 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6670 pr_err("attempting to create a second volume with size less then remaining space. Aborting...\n");
6674 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6676 dl
->raiddisk
= cnt
++;
6680 dprintf("imsm: imsm_get_free_size() returns : %llu\n", size
);
6685 static int reserve_space(struct supertype
*st
, int raiddisks
,
6686 unsigned long long size
, int chunk
,
6687 unsigned long long *freesize
)
6689 struct intel_super
*super
= st
->sb
;
6694 rv
= imsm_get_free_size(st
, raiddisks
, size
, chunk
, freesize
);
6697 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6699 dl
->raiddisk
= cnt
++;
6706 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
6707 int raiddisks
, int *chunk
, unsigned long long size
,
6708 unsigned long long data_offset
,
6709 char *dev
, unsigned long long *freesize
,
6717 * if given unused devices create a container
6718 * if given given devices in a container create a member volume
6720 if (level
== LEVEL_CONTAINER
) {
6721 /* Must be a fresh device to add to a container */
6722 return validate_geometry_imsm_container(st
, level
, layout
,
6732 struct intel_super
*super
= st
->sb
;
6733 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
6734 raiddisks
, chunk
, size
,
6737 /* we are being asked to automatically layout a
6738 * new volume based on the current contents of
6739 * the container. If the the parameters can be
6740 * satisfied reserve_space will record the disks,
6741 * start offset, and size of the volume to be
6742 * created. add_to_super and getinfo_super
6743 * detect when autolayout is in progress.
6745 /* assuming that freesize is always given when array is
6747 if (super
->orom
&& freesize
) {
6749 count
= count_volumes(super
->hba
,
6750 super
->orom
->dpa
, verbose
);
6751 if (super
->orom
->vphba
<= count
) {
6752 pr_vrb("platform does not support more than %d raid volumes.\n",
6753 super
->orom
->vphba
);
6758 return reserve_space(st
, raiddisks
, size
,
6764 /* creating in a given container */
6765 return validate_geometry_imsm_volume(st
, level
, layout
,
6766 raiddisks
, chunk
, size
,
6768 dev
, freesize
, verbose
);
6771 /* This device needs to be a device in an 'imsm' container */
6772 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
6775 pr_err("Cannot create this array on device %s\n",
6780 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
6782 pr_err("Cannot open %s: %s\n",
6783 dev
, strerror(errno
));
6786 /* Well, it is in use by someone, maybe an 'imsm' container. */
6787 cfd
= open_container(fd
);
6791 pr_err("Cannot use %s: It is busy\n",
6795 sra
= sysfs_read(cfd
, NULL
, GET_VERSION
);
6796 if (sra
&& sra
->array
.major_version
== -1 &&
6797 strcmp(sra
->text_version
, "imsm") == 0)
6801 /* This is a member of a imsm container. Load the container
6802 * and try to create a volume
6804 struct intel_super
*super
;
6806 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
, NULL
, 1) == 0) {
6808 strcpy(st
->container_devnm
, fd2devnm(cfd
));
6810 return validate_geometry_imsm_volume(st
, level
, layout
,
6812 size
, data_offset
, dev
,
6819 pr_err("failed container membership check\n");
6825 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
6827 struct intel_super
*super
= st
->sb
;
6829 if (level
&& *level
== UnSet
)
6830 *level
= LEVEL_CONTAINER
;
6832 if (level
&& layout
&& *layout
== UnSet
)
6833 *layout
= imsm_level_to_layout(*level
);
6835 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0))
6836 *chunk
= imsm_default_chunk(super
->orom
);
6839 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
6841 static int kill_subarray_imsm(struct supertype
*st
)
6843 /* remove the subarray currently referenced by ->current_vol */
6845 struct intel_dev
**dp
;
6846 struct intel_super
*super
= st
->sb
;
6847 __u8 current_vol
= super
->current_vol
;
6848 struct imsm_super
*mpb
= super
->anchor
;
6850 if (super
->current_vol
< 0)
6852 super
->current_vol
= -1; /* invalidate subarray cursor */
6854 /* block deletions that would change the uuid of active subarrays
6856 * FIXME when immutable ids are available, but note that we'll
6857 * also need to fixup the invalidated/active subarray indexes in
6860 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6863 if (i
< current_vol
)
6865 sprintf(subarray
, "%u", i
);
6866 if (is_subarray_active(subarray
, st
->devnm
)) {
6867 pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
6874 if (st
->update_tail
) {
6875 struct imsm_update_kill_array
*u
= xmalloc(sizeof(*u
));
6877 u
->type
= update_kill_array
;
6878 u
->dev_idx
= current_vol
;
6879 append_metadata_update(st
, u
, sizeof(*u
));
6884 for (dp
= &super
->devlist
; *dp
;)
6885 if ((*dp
)->index
== current_vol
) {
6888 handle_missing(super
, (*dp
)->dev
);
6889 if ((*dp
)->index
> current_vol
)
6894 /* no more raid devices, all active components are now spares,
6895 * but of course failed are still failed
6897 if (--mpb
->num_raid_devs
== 0) {
6900 for (d
= super
->disks
; d
; d
= d
->next
)
6905 super
->updates_pending
++;
6910 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
6911 char *update
, struct mddev_ident
*ident
)
6913 /* update the subarray currently referenced by ->current_vol */
6914 struct intel_super
*super
= st
->sb
;
6915 struct imsm_super
*mpb
= super
->anchor
;
6917 if (strcmp(update
, "name") == 0) {
6918 char *name
= ident
->name
;
6922 if (is_subarray_active(subarray
, st
->devnm
)) {
6923 pr_err("Unable to update name of active subarray\n");
6927 if (!check_name(super
, name
, 0))
6930 vol
= strtoul(subarray
, &ep
, 10);
6931 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
6934 if (st
->update_tail
) {
6935 struct imsm_update_rename_array
*u
= xmalloc(sizeof(*u
));
6937 u
->type
= update_rename_array
;
6939 snprintf((char *) u
->name
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6940 append_metadata_update(st
, u
, sizeof(*u
));
6942 struct imsm_dev
*dev
;
6945 dev
= get_imsm_dev(super
, vol
);
6946 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6947 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6948 dev
= get_imsm_dev(super
, i
);
6949 handle_missing(super
, dev
);
6951 super
->updates_pending
++;
6958 #endif /* MDASSEMBLE */
6960 static int is_gen_migration(struct imsm_dev
*dev
)
6965 if (!dev
->vol
.migr_state
)
6968 if (migr_type(dev
) == MIGR_GEN_MIGR
)
6974 static int is_rebuilding(struct imsm_dev
*dev
)
6976 struct imsm_map
*migr_map
;
6978 if (!dev
->vol
.migr_state
)
6981 if (migr_type(dev
) != MIGR_REBUILD
)
6984 migr_map
= get_imsm_map(dev
, MAP_1
);
6986 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
6993 static int is_initializing(struct imsm_dev
*dev
)
6995 struct imsm_map
*migr_map
;
6997 if (!dev
->vol
.migr_state
)
7000 if (migr_type(dev
) != MIGR_INIT
)
7003 migr_map
= get_imsm_map(dev
, MAP_1
);
7005 if (migr_map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
7012 static void update_recovery_start(struct intel_super
*super
,
7013 struct imsm_dev
*dev
,
7014 struct mdinfo
*array
)
7016 struct mdinfo
*rebuild
= NULL
;
7020 if (!is_rebuilding(dev
))
7023 /* Find the rebuild target, but punt on the dual rebuild case */
7024 for (d
= array
->devs
; d
; d
= d
->next
)
7025 if (d
->recovery_start
== 0) {
7032 /* (?) none of the disks are marked with
7033 * IMSM_ORD_REBUILD, so assume they are missing and the
7034 * disk_ord_tbl was not correctly updated
7036 dprintf("failed to locate out-of-sync disk\n");
7040 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
7041 rebuild
->recovery_start
= units
* blocks_per_migr_unit(super
, dev
);
7045 static int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
);
7048 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
7050 /* Given a container loaded by load_super_imsm_all,
7051 * extract information about all the arrays into
7053 * If 'subarray' is given, just extract info about that array.
7055 * For each imsm_dev create an mdinfo, fill it in,
7056 * then look for matching devices in super->disks
7057 * and create appropriate device mdinfo.
7059 struct intel_super
*super
= st
->sb
;
7060 struct imsm_super
*mpb
= super
->anchor
;
7061 struct mdinfo
*rest
= NULL
;
7065 int spare_disks
= 0;
7067 /* do not assemble arrays when not all attributes are supported */
7068 if (imsm_check_attributes(mpb
->attributes
) == 0) {
7070 pr_err("Unsupported attributes in IMSM metadata.Arrays activation is blocked.\n");
7073 /* count spare devices, not used in maps
7075 for (d
= super
->disks
; d
; d
= d
->next
)
7079 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7080 struct imsm_dev
*dev
;
7081 struct imsm_map
*map
;
7082 struct imsm_map
*map2
;
7083 struct mdinfo
*this;
7091 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
7094 dev
= get_imsm_dev(super
, i
);
7095 map
= get_imsm_map(dev
, MAP_0
);
7096 map2
= get_imsm_map(dev
, MAP_1
);
7098 /* do not publish arrays that are in the middle of an
7099 * unsupported migration
7101 if (dev
->vol
.migr_state
&&
7102 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
7103 pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n",
7107 /* do not publish arrays that are not support by controller's
7111 this = xmalloc(sizeof(*this));
7113 super
->current_vol
= i
;
7114 getinfo_super_imsm_volume(st
, this, NULL
);
7117 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
7118 /* mdadm does not support all metadata features- set the bit in all arrays state */
7119 if (!validate_geometry_imsm_orom(super
,
7120 get_imsm_raid_level(map
), /* RAID level */
7121 imsm_level_to_layout(get_imsm_raid_level(map
)),
7122 map
->num_members
, /* raid disks */
7123 &chunk
, join_u32(dev
->size_low
, dev
->size_high
),
7125 pr_err("IMSM RAID geometry validation failed. Array %s activation is blocked.\n",
7127 this->array
.state
|=
7128 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
7129 (1<<MD_SB_BLOCK_VOLUME
);
7133 /* if array has bad blocks, set suitable bit in all arrays state */
7135 this->array
.state
|=
7136 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
7137 (1<<MD_SB_BLOCK_VOLUME
);
7139 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
7140 unsigned long long recovery_start
;
7141 struct mdinfo
*info_d
;
7148 idx
= get_imsm_disk_idx(dev
, slot
, MAP_0
);
7149 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
7150 for (d
= super
->disks
; d
; d
= d
->next
)
7151 if (d
->index
== idx
)
7154 recovery_start
= MaxSector
;
7157 if (d
&& is_failed(&d
->disk
))
7159 if (ord
& IMSM_ORD_REBUILD
)
7163 * if we skip some disks the array will be assmebled degraded;
7164 * reset resync start to avoid a dirty-degraded
7165 * situation when performing the intial sync
7167 * FIXME handle dirty degraded
7169 if ((skip
|| recovery_start
== 0) && !dev
->vol
.dirty
)
7170 this->resync_start
= MaxSector
;
7174 info_d
= xcalloc(1, sizeof(*info_d
));
7175 info_d
->next
= this->devs
;
7176 this->devs
= info_d
;
7178 info_d
->disk
.number
= d
->index
;
7179 info_d
->disk
.major
= d
->major
;
7180 info_d
->disk
.minor
= d
->minor
;
7181 info_d
->disk
.raid_disk
= slot
;
7182 info_d
->recovery_start
= recovery_start
;
7184 if (slot
< map2
->num_members
)
7185 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
7187 this->array
.spare_disks
++;
7189 if (slot
< map
->num_members
)
7190 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
7192 this->array
.spare_disks
++;
7194 if (info_d
->recovery_start
== MaxSector
)
7195 this->array
.working_disks
++;
7197 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
7198 info_d
->data_offset
= pba_of_lba0(map
);
7200 if (map
->raid_level
== 5) {
7201 info_d
->component_size
=
7202 num_data_stripes(map
) *
7203 map
->blocks_per_strip
;
7205 info_d
->component_size
= blocks_per_member(map
);
7208 info_d
->bb
.supported
= 0;
7209 get_volume_badblocks(super
->bbm_log
, ord_to_idx(ord
),
7210 info_d
->data_offset
,
7211 info_d
->component_size
,
7214 /* now that the disk list is up-to-date fixup recovery_start */
7215 update_recovery_start(super
, dev
, this);
7216 this->array
.spare_disks
+= spare_disks
;
7219 /* check for reshape */
7220 if (this->reshape_active
== 1)
7221 recover_backup_imsm(st
, this);
7229 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
7230 int failed
, int look_in_map
)
7232 struct imsm_map
*map
;
7234 map
= get_imsm_map(dev
, look_in_map
);
7237 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
7238 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
7240 switch (get_imsm_raid_level(map
)) {
7242 return IMSM_T_STATE_FAILED
;
7245 if (failed
< map
->num_members
)
7246 return IMSM_T_STATE_DEGRADED
;
7248 return IMSM_T_STATE_FAILED
;
7253 * check to see if any mirrors have failed, otherwise we
7254 * are degraded. Even numbered slots are mirrored on
7258 /* gcc -Os complains that this is unused */
7259 int insync
= insync
;
7261 for (i
= 0; i
< map
->num_members
; i
++) {
7262 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_X
);
7263 int idx
= ord_to_idx(ord
);
7264 struct imsm_disk
*disk
;
7266 /* reset the potential in-sync count on even-numbered
7267 * slots. num_copies is always 2 for imsm raid10
7272 disk
= get_imsm_disk(super
, idx
);
7273 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
7276 /* no in-sync disks left in this mirror the
7280 return IMSM_T_STATE_FAILED
;
7283 return IMSM_T_STATE_DEGRADED
;
7287 return IMSM_T_STATE_DEGRADED
;
7289 return IMSM_T_STATE_FAILED
;
7295 return map
->map_state
;
7298 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
7303 struct imsm_disk
*disk
;
7304 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7305 struct imsm_map
*prev
= get_imsm_map(dev
, MAP_1
);
7306 struct imsm_map
*map_for_loop
;
7311 /* at the beginning of migration we set IMSM_ORD_REBUILD on
7312 * disks that are being rebuilt. New failures are recorded to
7313 * map[0]. So we look through all the disks we started with and
7314 * see if any failures are still present, or if any new ones
7318 if (prev
&& (map
->num_members
< prev
->num_members
))
7319 map_for_loop
= prev
;
7321 for (i
= 0; i
< map_for_loop
->num_members
; i
++) {
7323 /* when MAP_X is passed both maps failures are counted
7326 (look_in_map
== MAP_1
|| look_in_map
== MAP_X
) &&
7327 i
< prev
->num_members
) {
7328 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
7329 idx_1
= ord_to_idx(ord
);
7331 disk
= get_imsm_disk(super
, idx_1
);
7332 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
7335 if ((look_in_map
== MAP_0
|| look_in_map
== MAP_X
) &&
7336 i
< map
->num_members
) {
7337 ord
= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
7338 idx
= ord_to_idx(ord
);
7341 disk
= get_imsm_disk(super
, idx
);
7342 if (!disk
|| is_failed(disk
) ||
7343 ord
& IMSM_ORD_REBUILD
)
7353 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
7356 struct intel_super
*super
= c
->sb
;
7357 struct imsm_super
*mpb
= super
->anchor
;
7358 struct imsm_update_prealloc_bb_mem u
;
7360 if (atoi(inst
) >= mpb
->num_raid_devs
) {
7361 pr_err("subarry index %d, out of range\n", atoi(inst
));
7365 dprintf("imsm: open_new %s\n", inst
);
7366 a
->info
.container_member
= atoi(inst
);
7368 u
.type
= update_prealloc_badblocks_mem
;
7369 imsm_update_metadata_locally(c
, &u
, sizeof(u
));
7374 static int is_resyncing(struct imsm_dev
*dev
)
7376 struct imsm_map
*migr_map
;
7378 if (!dev
->vol
.migr_state
)
7381 if (migr_type(dev
) == MIGR_INIT
||
7382 migr_type(dev
) == MIGR_REPAIR
)
7385 if (migr_type(dev
) == MIGR_GEN_MIGR
)
7388 migr_map
= get_imsm_map(dev
, MAP_1
);
7390 if (migr_map
->map_state
== IMSM_T_STATE_NORMAL
&&
7391 dev
->vol
.migr_type
!= MIGR_GEN_MIGR
)
7397 /* return true if we recorded new information */
7398 static int mark_failure(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
7402 struct imsm_map
*map
;
7403 char buf
[MAX_RAID_SERIAL_LEN
+3];
7404 unsigned int len
, shift
= 0;
7406 /* new failures are always set in map[0] */
7407 map
= get_imsm_map(dev
, MAP_0
);
7409 slot
= get_imsm_disk_slot(map
, idx
);
7413 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
7414 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
7417 memcpy(buf
, disk
->serial
, MAX_RAID_SERIAL_LEN
);
7418 buf
[MAX_RAID_SERIAL_LEN
] = '\000';
7420 if ((len
= strlen(buf
)) >= MAX_RAID_SERIAL_LEN
)
7421 shift
= len
- MAX_RAID_SERIAL_LEN
+ 1;
7422 strncpy((char *)disk
->serial
, &buf
[shift
], MAX_RAID_SERIAL_LEN
);
7424 disk
->status
|= FAILED_DISK
;
7425 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
7426 /* mark failures in second map if second map exists and this disk
7428 * This is valid for migration, initialization and rebuild
7430 if (dev
->vol
.migr_state
) {
7431 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
7432 int slot2
= get_imsm_disk_slot(map2
, idx
);
7434 if (slot2
< map2
->num_members
&& slot2
>= 0)
7435 set_imsm_ord_tbl_ent(map2
, slot2
,
7436 idx
| IMSM_ORD_REBUILD
);
7438 if (map
->failed_disk_num
== 0xff)
7439 map
->failed_disk_num
= slot
;
7443 static void mark_missing(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
7445 mark_failure(dev
, disk
, idx
);
7447 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
7450 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
7451 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
7454 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
7458 if (!super
->missing
)
7461 /* When orom adds replacement for missing disk it does
7462 * not remove entry of missing disk, but just updates map with
7463 * new added disk. So it is not enough just to test if there is
7464 * any missing disk, we have to look if there are any failed disks
7465 * in map to stop migration */
7467 dprintf("imsm: mark missing\n");
7468 /* end process for initialization and rebuild only
7470 if (is_gen_migration(dev
) == 0) {
7474 failed
= imsm_count_failed(super
, dev
, MAP_0
);
7475 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7478 end_migration(dev
, super
, map_state
);
7480 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
7481 mark_missing(dev
, &dl
->disk
, dl
->index
);
7482 super
->updates_pending
++;
7485 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
,
7488 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
7489 unsigned long long array_blocks
;
7490 struct imsm_map
*map
;
7492 if (used_disks
== 0) {
7493 /* when problems occures
7494 * return current array_blocks value
7496 array_blocks
= __le32_to_cpu(dev
->size_high
);
7497 array_blocks
= array_blocks
<< 32;
7498 array_blocks
+= __le32_to_cpu(dev
->size_low
);
7500 return array_blocks
;
7503 /* set array size in metadata
7505 if (new_size
<= 0) {
7506 /* OLCE size change is caused by added disks
7508 map
= get_imsm_map(dev
, MAP_0
);
7509 array_blocks
= blocks_per_member(map
) * used_disks
;
7511 /* Online Volume Size Change
7512 * Using available free space
7514 array_blocks
= new_size
;
7517 /* round array size down to closest MB
7519 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
7520 dev
->size_low
= __cpu_to_le32((__u32
)array_blocks
);
7521 dev
->size_high
= __cpu_to_le32((__u32
)(array_blocks
>> 32));
7523 return array_blocks
;
7526 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
7528 static void imsm_progress_container_reshape(struct intel_super
*super
)
7530 /* if no device has a migr_state, but some device has a
7531 * different number of members than the previous device, start
7532 * changing the number of devices in this device to match
7535 struct imsm_super
*mpb
= super
->anchor
;
7536 int prev_disks
= -1;
7540 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7541 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
7542 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7543 struct imsm_map
*map2
;
7544 int prev_num_members
;
7546 if (dev
->vol
.migr_state
)
7549 if (prev_disks
== -1)
7550 prev_disks
= map
->num_members
;
7551 if (prev_disks
== map
->num_members
)
7554 /* OK, this array needs to enter reshape mode.
7555 * i.e it needs a migr_state
7558 copy_map_size
= sizeof_imsm_map(map
);
7559 prev_num_members
= map
->num_members
;
7560 map
->num_members
= prev_disks
;
7561 dev
->vol
.migr_state
= 1;
7562 dev
->vol
.curr_migr_unit
= 0;
7563 set_migr_type(dev
, MIGR_GEN_MIGR
);
7564 for (i
= prev_num_members
;
7565 i
< map
->num_members
; i
++)
7566 set_imsm_ord_tbl_ent(map
, i
, i
);
7567 map2
= get_imsm_map(dev
, MAP_1
);
7568 /* Copy the current map */
7569 memcpy(map2
, map
, copy_map_size
);
7570 map2
->num_members
= prev_num_members
;
7572 imsm_set_array_size(dev
, -1);
7573 super
->clean_migration_record_by_mdmon
= 1;
7574 super
->updates_pending
++;
7578 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
7579 * states are handled in imsm_set_disk() with one exception, when a
7580 * resync is stopped due to a new failure this routine will set the
7581 * 'degraded' state for the array.
7583 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
7585 int inst
= a
->info
.container_member
;
7586 struct intel_super
*super
= a
->container
->sb
;
7587 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7588 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7589 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
7590 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7591 __u32 blocks_per_unit
;
7593 if (dev
->vol
.migr_state
&&
7594 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
7595 /* array state change is blocked due to reshape action
7597 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
7598 * - finish the reshape (if last_checkpoint is big and action != reshape)
7599 * - update curr_migr_unit
7601 if (a
->curr_action
== reshape
) {
7602 /* still reshaping, maybe update curr_migr_unit */
7603 goto mark_checkpoint
;
7605 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
7606 /* for some reason we aborted the reshape.
7608 * disable automatic metadata rollback
7609 * user action is required to recover process
7612 struct imsm_map
*map2
=
7613 get_imsm_map(dev
, MAP_1
);
7614 dev
->vol
.migr_state
= 0;
7615 set_migr_type(dev
, 0);
7616 dev
->vol
.curr_migr_unit
= 0;
7618 sizeof_imsm_map(map2
));
7619 super
->updates_pending
++;
7622 if (a
->last_checkpoint
>= a
->info
.component_size
) {
7623 unsigned long long array_blocks
;
7627 used_disks
= imsm_num_data_members(dev
, MAP_0
);
7628 if (used_disks
> 0) {
7630 blocks_per_member(map
) *
7632 /* round array size down to closest MB
7634 array_blocks
= (array_blocks
7635 >> SECT_PER_MB_SHIFT
)
7636 << SECT_PER_MB_SHIFT
;
7637 a
->info
.custom_array_size
= array_blocks
;
7638 /* encourage manager to update array
7642 a
->check_reshape
= 1;
7644 /* finalize online capacity expansion/reshape */
7645 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
7647 mdi
->disk
.raid_disk
,
7650 imsm_progress_container_reshape(super
);
7655 /* before we activate this array handle any missing disks */
7656 if (consistent
== 2)
7657 handle_missing(super
, dev
);
7659 if (consistent
== 2 &&
7660 (!is_resync_complete(&a
->info
) ||
7661 map_state
!= IMSM_T_STATE_NORMAL
||
7662 dev
->vol
.migr_state
))
7665 if (is_resync_complete(&a
->info
)) {
7666 /* complete intialization / resync,
7667 * recovery and interrupted recovery is completed in
7670 if (is_resyncing(dev
)) {
7671 dprintf("imsm: mark resync done\n");
7672 end_migration(dev
, super
, map_state
);
7673 super
->updates_pending
++;
7674 a
->last_checkpoint
= 0;
7676 } else if ((!is_resyncing(dev
) && !failed
) &&
7677 (imsm_reshape_blocks_arrays_changes(super
) == 0)) {
7678 /* mark the start of the init process if nothing is failed */
7679 dprintf("imsm: mark resync start\n");
7680 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
7681 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
7683 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
7684 super
->updates_pending
++;
7688 /* skip checkpointing for general migration,
7689 * it is controlled in mdadm
7691 if (is_gen_migration(dev
))
7692 goto skip_mark_checkpoint
;
7694 /* check if we can update curr_migr_unit from resync_start, recovery_start */
7695 blocks_per_unit
= blocks_per_migr_unit(super
, dev
);
7696 if (blocks_per_unit
) {
7700 units
= a
->last_checkpoint
/ blocks_per_unit
;
7703 /* check that we did not overflow 32-bits, and that
7704 * curr_migr_unit needs updating
7706 if (units32
== units
&&
7708 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
7709 dprintf("imsm: mark checkpoint (%u)\n", units32
);
7710 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
7711 super
->updates_pending
++;
7715 skip_mark_checkpoint
:
7716 /* mark dirty / clean */
7717 if (dev
->vol
.dirty
!= !consistent
) {
7718 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
7723 super
->updates_pending
++;
7729 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
7731 int inst
= a
->info
.container_member
;
7732 struct intel_super
*super
= a
->container
->sb
;
7733 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7734 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7735 struct imsm_disk
*disk
;
7737 int recovery_not_finished
= 0;
7742 if (n
> map
->num_members
)
7743 pr_err("imsm: set_disk %d out of range 0..%d\n",
7744 n
, map
->num_members
- 1);
7749 dprintf("imsm: set_disk %d:%x\n", n
, state
);
7751 ord
= get_imsm_ord_tbl_ent(dev
, n
, MAP_0
);
7752 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
7754 /* check for new failures */
7755 if (state
& DS_FAULTY
) {
7756 if (mark_failure(dev
, disk
, ord_to_idx(ord
)))
7757 super
->updates_pending
++;
7760 /* check if in_sync */
7761 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
7762 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
7764 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
7765 super
->updates_pending
++;
7768 failed
= imsm_count_failed(super
, dev
, MAP_0
);
7769 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7771 /* check if recovery complete, newly degraded, or failed */
7772 dprintf("imsm: Detected transition to state ");
7773 switch (map_state
) {
7774 case IMSM_T_STATE_NORMAL
: /* transition to normal state */
7775 dprintf("normal: ");
7776 if (is_rebuilding(dev
)) {
7777 dprintf_cont("while rebuilding");
7778 /* check if recovery is really finished */
7779 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
7780 if (mdi
->recovery_start
!= MaxSector
) {
7781 recovery_not_finished
= 1;
7784 if (recovery_not_finished
) {
7786 dprintf("Rebuild has not finished yet, state not changed");
7787 if (a
->last_checkpoint
< mdi
->recovery_start
) {
7788 a
->last_checkpoint
= mdi
->recovery_start
;
7789 super
->updates_pending
++;
7793 end_migration(dev
, super
, map_state
);
7794 map
= get_imsm_map(dev
, MAP_0
);
7795 map
->failed_disk_num
= ~0;
7796 super
->updates_pending
++;
7797 a
->last_checkpoint
= 0;
7800 if (is_gen_migration(dev
)) {
7801 dprintf_cont("while general migration");
7802 if (a
->last_checkpoint
>= a
->info
.component_size
)
7803 end_migration(dev
, super
, map_state
);
7805 map
->map_state
= map_state
;
7806 map
= get_imsm_map(dev
, MAP_0
);
7807 map
->failed_disk_num
= ~0;
7808 super
->updates_pending
++;
7812 case IMSM_T_STATE_DEGRADED
: /* transition to degraded state */
7813 dprintf_cont("degraded: ");
7814 if (map
->map_state
!= map_state
&& !dev
->vol
.migr_state
) {
7815 dprintf_cont("mark degraded");
7816 map
->map_state
= map_state
;
7817 super
->updates_pending
++;
7818 a
->last_checkpoint
= 0;
7821 if (is_rebuilding(dev
)) {
7822 dprintf_cont("while rebuilding.");
7823 if (map
->map_state
!= map_state
) {
7824 dprintf_cont(" Map state change");
7825 end_migration(dev
, super
, map_state
);
7826 super
->updates_pending
++;
7830 if (is_gen_migration(dev
)) {
7831 dprintf_cont("while general migration");
7832 if (a
->last_checkpoint
>= a
->info
.component_size
)
7833 end_migration(dev
, super
, map_state
);
7835 map
->map_state
= map_state
;
7836 manage_second_map(super
, dev
);
7838 super
->updates_pending
++;
7841 if (is_initializing(dev
)) {
7842 dprintf_cont("while initialization.");
7843 map
->map_state
= map_state
;
7844 super
->updates_pending
++;
7848 case IMSM_T_STATE_FAILED
: /* transition to failed state */
7849 dprintf_cont("failed: ");
7850 if (is_gen_migration(dev
)) {
7851 dprintf_cont("while general migration");
7852 map
->map_state
= map_state
;
7853 super
->updates_pending
++;
7856 if (map
->map_state
!= map_state
) {
7857 dprintf_cont("mark failed");
7858 end_migration(dev
, super
, map_state
);
7859 super
->updates_pending
++;
7860 a
->last_checkpoint
= 0;
7865 dprintf_cont("state %i\n", map_state
);
7870 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
7873 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
7874 unsigned long long dsize
;
7875 unsigned long long sectors
;
7876 unsigned int sector_size
;
7878 get_dev_sector_size(fd
, NULL
, §or_size
);
7879 get_dev_size(fd
, NULL
, &dsize
);
7881 if (mpb_size
> sector_size
) {
7882 /* -1 to account for anchor */
7883 sectors
= mpb_sectors(mpb
, sector_size
) - 1;
7885 /* write the extended mpb to the sectors preceeding the anchor */
7886 if (lseek64(fd
, dsize
- (sector_size
* (2 + sectors
)),
7890 if ((unsigned long long)write(fd
, buf
+ sector_size
,
7891 sector_size
* sectors
) != sector_size
* sectors
)
7895 /* first block is stored on second to last sector of the disk */
7896 if (lseek64(fd
, dsize
- (sector_size
* 2), SEEK_SET
) < 0)
7899 if (write(fd
, buf
, sector_size
) != sector_size
)
7905 static void imsm_sync_metadata(struct supertype
*container
)
7907 struct intel_super
*super
= container
->sb
;
7909 dprintf("sync metadata: %d\n", super
->updates_pending
);
7910 if (!super
->updates_pending
)
7913 write_super_imsm(container
, 0);
7915 super
->updates_pending
= 0;
7918 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
7920 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7921 int i
= get_imsm_disk_idx(dev
, idx
, MAP_X
);
7924 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7928 if (dl
&& is_failed(&dl
->disk
))
7932 dprintf("found %x:%x\n", dl
->major
, dl
->minor
);
7937 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
7938 struct active_array
*a
, int activate_new
,
7939 struct mdinfo
*additional_test_list
)
7941 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7942 int idx
= get_imsm_disk_idx(dev
, slot
, MAP_X
);
7943 struct imsm_super
*mpb
= super
->anchor
;
7944 struct imsm_map
*map
;
7945 unsigned long long pos
;
7950 __u32 array_start
= 0;
7951 __u32 array_end
= 0;
7953 struct mdinfo
*test_list
;
7955 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7956 /* If in this array, skip */
7957 for (d
= a
->info
.devs
; d
; d
= d
->next
)
7958 if (d
->state_fd
>= 0 &&
7959 d
->disk
.major
== dl
->major
&&
7960 d
->disk
.minor
== dl
->minor
) {
7961 dprintf("%x:%x already in array\n",
7962 dl
->major
, dl
->minor
);
7967 test_list
= additional_test_list
;
7969 if (test_list
->disk
.major
== dl
->major
&&
7970 test_list
->disk
.minor
== dl
->minor
) {
7971 dprintf("%x:%x already in additional test list\n",
7972 dl
->major
, dl
->minor
);
7975 test_list
= test_list
->next
;
7980 /* skip in use or failed drives */
7981 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
7983 dprintf("%x:%x status (failed: %d index: %d)\n",
7984 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
7988 /* skip pure spares when we are looking for partially
7989 * assimilated drives
7991 if (dl
->index
== -1 && !activate_new
)
7994 /* Does this unused device have the requisite free space?
7995 * It needs to be able to cover all member volumes
7997 ex
= get_extents(super
, dl
);
7999 dprintf("cannot get extents\n");
8002 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8003 dev
= get_imsm_dev(super
, i
);
8004 map
= get_imsm_map(dev
, MAP_0
);
8006 /* check if this disk is already a member of
8009 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
8015 array_start
= pba_of_lba0(map
);
8016 array_end
= array_start
+
8017 blocks_per_member(map
) - 1;
8020 /* check that we can start at pba_of_lba0 with
8021 * blocks_per_member of space
8023 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
8027 pos
= ex
[j
].start
+ ex
[j
].size
;
8029 } while (ex
[j
-1].size
);
8036 if (i
< mpb
->num_raid_devs
) {
8037 dprintf("%x:%x does not have %u to %u available\n",
8038 dl
->major
, dl
->minor
, array_start
, array_end
);
8048 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
8050 struct imsm_dev
*dev2
;
8051 struct imsm_map
*map
;
8057 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
8059 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
, MAP_0
);
8060 if (state
== IMSM_T_STATE_FAILED
) {
8061 map
= get_imsm_map(dev2
, MAP_0
);
8064 for (slot
= 0; slot
< map
->num_members
; slot
++) {
8066 * Check if failed disks are deleted from intel
8067 * disk list or are marked to be deleted
8069 idx
= get_imsm_disk_idx(dev2
, slot
, MAP_X
);
8070 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
8072 * Do not rebuild the array if failed disks
8073 * from failed sub-array are not removed from
8077 is_failed(&idisk
->disk
) &&
8078 (idisk
->action
!= DISK_REMOVE
))
8086 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
8087 struct metadata_update
**updates
)
8090 * Find a device with unused free space and use it to replace a
8091 * failed/vacant region in an array. We replace failed regions one a
8092 * array at a time. The result is that a new spare disk will be added
8093 * to the first failed array and after the monitor has finished
8094 * propagating failures the remainder will be consumed.
8096 * FIXME add a capability for mdmon to request spares from another
8100 struct intel_super
*super
= a
->container
->sb
;
8101 int inst
= a
->info
.container_member
;
8102 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8103 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8104 int failed
= a
->info
.array
.raid_disks
;
8105 struct mdinfo
*rv
= NULL
;
8108 struct metadata_update
*mu
;
8110 struct imsm_update_activate_spare
*u
;
8115 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
8116 if ((d
->curr_state
& DS_FAULTY
) &&
8118 /* wait for Removal to happen */
8120 if (d
->state_fd
>= 0)
8124 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
8125 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
8127 if (imsm_reshape_blocks_arrays_changes(super
))
8130 /* Cannot activate another spare if rebuild is in progress already
8132 if (is_rebuilding(dev
)) {
8133 dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n");
8137 if (a
->info
.array
.level
== 4)
8138 /* No repair for takeovered array
8139 * imsm doesn't support raid4
8143 if (imsm_check_degraded(super
, dev
, failed
, MAP_0
) !=
8144 IMSM_T_STATE_DEGRADED
)
8147 if (get_imsm_map(dev
, MAP_0
)->map_state
== IMSM_T_STATE_UNINITIALIZED
) {
8148 dprintf("imsm: No spare activation allowed. Volume is not initialized.\n");
8153 * If there are any failed disks check state of the other volume.
8154 * Block rebuild if the another one is failed until failed disks
8155 * are removed from container.
8158 dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n",
8159 MAX_RAID_SERIAL_LEN
, dev
->volume
);
8160 /* check if states of the other volumes allow for rebuild */
8161 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
8163 allowed
= imsm_rebuild_allowed(a
->container
,
8171 /* For each slot, if it is not working, find a spare */
8172 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
8173 for (d
= a
->info
.devs
; d
; d
= d
->next
)
8174 if (d
->disk
.raid_disk
== i
)
8176 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
8177 if (d
&& (d
->state_fd
>= 0))
8181 * OK, this device needs recovery. Try to re-add the
8182 * previous occupant of this slot, if this fails see if
8183 * we can continue the assimilation of a spare that was
8184 * partially assimilated, finally try to activate a new
8187 dl
= imsm_readd(super
, i
, a
);
8189 dl
= imsm_add_spare(super
, i
, a
, 0, rv
);
8191 dl
= imsm_add_spare(super
, i
, a
, 1, rv
);
8195 /* found a usable disk with enough space */
8196 di
= xcalloc(1, sizeof(*di
));
8198 /* dl->index will be -1 in the case we are activating a
8199 * pristine spare. imsm_process_update() will create a
8200 * new index in this case. Once a disk is found to be
8201 * failed in all member arrays it is kicked from the
8204 di
->disk
.number
= dl
->index
;
8206 /* (ab)use di->devs to store a pointer to the device
8209 di
->devs
= (struct mdinfo
*) dl
;
8211 di
->disk
.raid_disk
= i
;
8212 di
->disk
.major
= dl
->major
;
8213 di
->disk
.minor
= dl
->minor
;
8215 di
->recovery_start
= 0;
8216 di
->data_offset
= pba_of_lba0(map
);
8217 di
->component_size
= a
->info
.component_size
;
8218 di
->container_member
= inst
;
8219 di
->bb
.supported
= 0;
8220 super
->random
= random32();
8224 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
8225 i
, di
->data_offset
);
8229 /* No spares found */
8231 /* Now 'rv' has a list of devices to return.
8232 * Create a metadata_update record to update the
8233 * disk_ord_tbl for the array
8235 mu
= xmalloc(sizeof(*mu
));
8236 mu
->buf
= xcalloc(num_spares
,
8237 sizeof(struct imsm_update_activate_spare
));
8239 mu
->space_list
= NULL
;
8240 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
8241 mu
->next
= *updates
;
8242 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
8244 for (di
= rv
; di
; di
= di
->next
) {
8245 u
->type
= update_activate_spare
;
8246 u
->dl
= (struct dl
*) di
->devs
;
8248 u
->slot
= di
->disk
.raid_disk
;
8259 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
8261 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
8262 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8263 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, MAP_0
);
8264 struct disk_info
*inf
= get_disk_info(u
);
8265 struct imsm_disk
*disk
;
8269 for (i
= 0; i
< map
->num_members
; i
++) {
8270 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, MAP_X
));
8271 for (j
= 0; j
< new_map
->num_members
; j
++)
8272 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
8279 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
8283 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
8284 if (dl
->major
== major
&& dl
->minor
== minor
)
8289 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
8295 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
8296 if (dl
->major
== major
&& dl
->minor
== minor
) {
8299 prev
->next
= dl
->next
;
8301 super
->disks
= dl
->next
;
8303 __free_imsm_disk(dl
);
8304 dprintf("removed %x:%x\n", major
, minor
);
8312 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
8314 static int add_remove_disk_update(struct intel_super
*super
)
8316 int check_degraded
= 0;
8319 /* add/remove some spares to/from the metadata/contrainer */
8320 while (super
->disk_mgmt_list
) {
8321 struct dl
*disk_cfg
;
8323 disk_cfg
= super
->disk_mgmt_list
;
8324 super
->disk_mgmt_list
= disk_cfg
->next
;
8325 disk_cfg
->next
= NULL
;
8327 if (disk_cfg
->action
== DISK_ADD
) {
8328 disk_cfg
->next
= super
->disks
;
8329 super
->disks
= disk_cfg
;
8331 dprintf("added %x:%x\n",
8332 disk_cfg
->major
, disk_cfg
->minor
);
8333 } else if (disk_cfg
->action
== DISK_REMOVE
) {
8334 dprintf("Disk remove action processed: %x.%x\n",
8335 disk_cfg
->major
, disk_cfg
->minor
);
8336 disk
= get_disk_super(super
,
8340 /* store action status */
8341 disk
->action
= DISK_REMOVE
;
8342 /* remove spare disks only */
8343 if (disk
->index
== -1) {
8344 remove_disk_super(super
,
8349 /* release allocate disk structure */
8350 __free_imsm_disk(disk_cfg
);
8353 return check_degraded
;
8356 static int apply_reshape_migration_update(struct imsm_update_reshape_migration
*u
,
8357 struct intel_super
*super
,
8360 struct intel_dev
*id
;
8361 void **tofree
= NULL
;
8364 dprintf("(enter)\n");
8365 if (u
->subdev
< 0 || u
->subdev
> 1) {
8366 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
8369 if (space_list
== NULL
|| *space_list
== NULL
) {
8370 dprintf("imsm: Error: Memory is not allocated\n");
8374 for (id
= super
->devlist
; id
; id
= id
->next
) {
8375 if (id
->index
== (unsigned)u
->subdev
) {
8376 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
8377 struct imsm_map
*map
;
8378 struct imsm_dev
*new_dev
=
8379 (struct imsm_dev
*)*space_list
;
8380 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
8382 struct dl
*new_disk
;
8384 if (new_dev
== NULL
)
8386 *space_list
= **space_list
;
8387 memcpy(new_dev
, dev
, sizeof_imsm_dev(dev
, 0));
8388 map
= get_imsm_map(new_dev
, MAP_0
);
8390 dprintf("imsm: Error: migration in progress");
8394 to_state
= map
->map_state
;
8395 if ((u
->new_level
== 5) && (map
->raid_level
== 0)) {
8397 /* this should not happen */
8398 if (u
->new_disks
[0] < 0) {
8399 map
->failed_disk_num
=
8400 map
->num_members
- 1;
8401 to_state
= IMSM_T_STATE_DEGRADED
;
8403 to_state
= IMSM_T_STATE_NORMAL
;
8405 migrate(new_dev
, super
, to_state
, MIGR_GEN_MIGR
);
8406 if (u
->new_level
> -1)
8407 map
->raid_level
= u
->new_level
;
8408 migr_map
= get_imsm_map(new_dev
, MAP_1
);
8409 if ((u
->new_level
== 5) &&
8410 (migr_map
->raid_level
== 0)) {
8411 int ord
= map
->num_members
- 1;
8412 migr_map
->num_members
--;
8413 if (u
->new_disks
[0] < 0)
8414 ord
|= IMSM_ORD_REBUILD
;
8415 set_imsm_ord_tbl_ent(map
,
8416 map
->num_members
- 1,
8420 tofree
= (void **)dev
;
8422 /* update chunk size
8424 if (u
->new_chunksize
> 0) {
8425 unsigned long long num_data_stripes
;
8427 imsm_num_data_members(dev
, MAP_0
);
8429 if (used_disks
== 0)
8432 map
->blocks_per_strip
=
8433 __cpu_to_le16(u
->new_chunksize
* 2);
8435 (join_u32(dev
->size_low
, dev
->size_high
)
8437 num_data_stripes
/= map
->blocks_per_strip
;
8438 num_data_stripes
/= map
->num_domains
;
8439 set_num_data_stripes(map
, num_data_stripes
);
8444 if (u
->new_level
!= 5 || migr_map
->raid_level
!= 0 ||
8445 migr_map
->raid_level
== map
->raid_level
)
8448 if (u
->new_disks
[0] >= 0) {
8451 new_disk
= get_disk_super(super
,
8452 major(u
->new_disks
[0]),
8453 minor(u
->new_disks
[0]));
8454 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
8455 major(u
->new_disks
[0]),
8456 minor(u
->new_disks
[0]),
8457 new_disk
, new_disk
->index
);
8458 if (new_disk
== NULL
)
8459 goto error_disk_add
;
8461 new_disk
->index
= map
->num_members
- 1;
8462 /* slot to fill in autolayout
8464 new_disk
->raiddisk
= new_disk
->index
;
8465 new_disk
->disk
.status
|= CONFIGURED_DISK
;
8466 new_disk
->disk
.status
&= ~SPARE_DISK
;
8468 goto error_disk_add
;
8471 *tofree
= *space_list
;
8472 /* calculate new size
8474 imsm_set_array_size(new_dev
, -1);
8481 *space_list
= tofree
;
8485 dprintf("Error: imsm: Cannot find disk.\n");
8489 static int apply_size_change_update(struct imsm_update_size_change
*u
,
8490 struct intel_super
*super
)
8492 struct intel_dev
*id
;
8495 dprintf("(enter)\n");
8496 if (u
->subdev
< 0 || u
->subdev
> 1) {
8497 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
8501 for (id
= super
->devlist
; id
; id
= id
->next
) {
8502 if (id
->index
== (unsigned)u
->subdev
) {
8503 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
8504 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8505 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
8506 unsigned long long blocks_per_member
;
8507 unsigned long long num_data_stripes
;
8509 /* calculate new size
8511 blocks_per_member
= u
->new_size
/ used_disks
;
8512 num_data_stripes
= blocks_per_member
/
8513 map
->blocks_per_strip
;
8514 num_data_stripes
/= map
->num_domains
;
8515 dprintf("(size: %llu, blocks per member: %llu, num_data_stipes: %llu)\n",
8516 u
->new_size
, blocks_per_member
,
8518 set_blocks_per_member(map
, blocks_per_member
);
8519 set_num_data_stripes(map
, num_data_stripes
);
8520 imsm_set_array_size(dev
, u
->new_size
);
8530 static int apply_update_activate_spare(struct imsm_update_activate_spare
*u
,
8531 struct intel_super
*super
,
8532 struct active_array
*active_array
)
8534 struct imsm_super
*mpb
= super
->anchor
;
8535 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
8536 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8537 struct imsm_map
*migr_map
;
8538 struct active_array
*a
;
8539 struct imsm_disk
*disk
;
8546 int second_map_created
= 0;
8548 for (; u
; u
= u
->next
) {
8549 victim
= get_imsm_disk_idx(dev
, u
->slot
, MAP_X
);
8554 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
8559 pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n",
8564 /* count failures (excluding rebuilds and the victim)
8565 * to determine map[0] state
8568 for (i
= 0; i
< map
->num_members
; i
++) {
8571 disk
= get_imsm_disk(super
,
8572 get_imsm_disk_idx(dev
, i
, MAP_X
));
8573 if (!disk
|| is_failed(disk
))
8577 /* adding a pristine spare, assign a new index */
8578 if (dl
->index
< 0) {
8579 dl
->index
= super
->anchor
->num_disks
;
8580 super
->anchor
->num_disks
++;
8583 disk
->status
|= CONFIGURED_DISK
;
8584 disk
->status
&= ~SPARE_DISK
;
8587 to_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8588 if (!second_map_created
) {
8589 second_map_created
= 1;
8590 map
->map_state
= IMSM_T_STATE_DEGRADED
;
8591 migrate(dev
, super
, to_state
, MIGR_REBUILD
);
8593 map
->map_state
= to_state
;
8594 migr_map
= get_imsm_map(dev
, MAP_1
);
8595 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
8596 set_imsm_ord_tbl_ent(migr_map
, u
->slot
,
8597 dl
->index
| IMSM_ORD_REBUILD
);
8599 /* update the family_num to mark a new container
8600 * generation, being careful to record the existing
8601 * family_num in orig_family_num to clean up after
8602 * earlier mdadm versions that neglected to set it.
8604 if (mpb
->orig_family_num
== 0)
8605 mpb
->orig_family_num
= mpb
->family_num
;
8606 mpb
->family_num
+= super
->random
;
8608 /* count arrays using the victim in the metadata */
8610 for (a
= active_array
; a
; a
= a
->next
) {
8611 dev
= get_imsm_dev(super
, a
->info
.container_member
);
8612 map
= get_imsm_map(dev
, MAP_0
);
8614 if (get_imsm_disk_slot(map
, victim
) >= 0)
8618 /* delete the victim if it is no longer being
8624 /* We know that 'manager' isn't touching anything,
8625 * so it is safe to delete
8627 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
8628 if ((*dlp
)->index
== victim
)
8631 /* victim may be on the missing list */
8633 for (dlp
= &super
->missing
; *dlp
;
8634 dlp
= &(*dlp
)->next
)
8635 if ((*dlp
)->index
== victim
)
8637 imsm_delete(super
, dlp
, victim
);
8644 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
8645 struct intel_super
*super
,
8648 struct dl
*new_disk
;
8649 struct intel_dev
*id
;
8651 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
8652 int disk_count
= u
->old_raid_disks
;
8653 void **tofree
= NULL
;
8654 int devices_to_reshape
= 1;
8655 struct imsm_super
*mpb
= super
->anchor
;
8657 unsigned int dev_id
;
8659 dprintf("(enter)\n");
8661 /* enable spares to use in array */
8662 for (i
= 0; i
< delta_disks
; i
++) {
8663 new_disk
= get_disk_super(super
,
8664 major(u
->new_disks
[i
]),
8665 minor(u
->new_disks
[i
]));
8666 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
8667 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
8668 new_disk
, new_disk
->index
);
8669 if (new_disk
== NULL
||
8670 (new_disk
->index
>= 0 &&
8671 new_disk
->index
< u
->old_raid_disks
))
8672 goto update_reshape_exit
;
8673 new_disk
->index
= disk_count
++;
8674 /* slot to fill in autolayout
8676 new_disk
->raiddisk
= new_disk
->index
;
8677 new_disk
->disk
.status
|=
8679 new_disk
->disk
.status
&= ~SPARE_DISK
;
8682 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
8683 mpb
->num_raid_devs
);
8684 /* manage changes in volume
8686 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
8687 void **sp
= *space_list
;
8688 struct imsm_dev
*newdev
;
8689 struct imsm_map
*newmap
, *oldmap
;
8691 for (id
= super
->devlist
; id
; id
= id
->next
) {
8692 if (id
->index
== dev_id
)
8701 /* Copy the dev, but not (all of) the map */
8702 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
8703 oldmap
= get_imsm_map(id
->dev
, MAP_0
);
8704 newmap
= get_imsm_map(newdev
, MAP_0
);
8705 /* Copy the current map */
8706 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8707 /* update one device only
8709 if (devices_to_reshape
) {
8710 dprintf("imsm: modifying subdev: %i\n",
8712 devices_to_reshape
--;
8713 newdev
->vol
.migr_state
= 1;
8714 newdev
->vol
.curr_migr_unit
= 0;
8715 set_migr_type(newdev
, MIGR_GEN_MIGR
);
8716 newmap
->num_members
= u
->new_raid_disks
;
8717 for (i
= 0; i
< delta_disks
; i
++) {
8718 set_imsm_ord_tbl_ent(newmap
,
8719 u
->old_raid_disks
+ i
,
8720 u
->old_raid_disks
+ i
);
8722 /* New map is correct, now need to save old map
8724 newmap
= get_imsm_map(newdev
, MAP_1
);
8725 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8727 imsm_set_array_size(newdev
, -1);
8730 sp
= (void **)id
->dev
;
8735 /* Clear migration record */
8736 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
8739 *space_list
= tofree
;
8742 update_reshape_exit
:
8747 static int apply_takeover_update(struct imsm_update_takeover
*u
,
8748 struct intel_super
*super
,
8751 struct imsm_dev
*dev
= NULL
;
8752 struct intel_dev
*dv
;
8753 struct imsm_dev
*dev_new
;
8754 struct imsm_map
*map
;
8758 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
8759 if (dv
->index
== (unsigned int)u
->subarray
) {
8767 map
= get_imsm_map(dev
, MAP_0
);
8769 if (u
->direction
== R10_TO_R0
) {
8770 unsigned long long num_data_stripes
;
8772 map
->num_domains
= 1;
8773 num_data_stripes
= blocks_per_member(map
);
8774 num_data_stripes
/= map
->blocks_per_strip
;
8775 num_data_stripes
/= map
->num_domains
;
8776 set_num_data_stripes(map
, num_data_stripes
);
8778 /* Number of failed disks must be half of initial disk number */
8779 if (imsm_count_failed(super
, dev
, MAP_0
) !=
8780 (map
->num_members
/ 2))
8783 /* iterate through devices to mark removed disks as spare */
8784 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8785 if (dm
->disk
.status
& FAILED_DISK
) {
8786 int idx
= dm
->index
;
8787 /* update indexes on the disk list */
8788 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
8789 the index values will end up being correct.... NB */
8790 for (du
= super
->disks
; du
; du
= du
->next
)
8791 if (du
->index
> idx
)
8793 /* mark as spare disk */
8798 map
->num_members
= map
->num_members
/ 2;
8799 map
->map_state
= IMSM_T_STATE_NORMAL
;
8800 map
->num_domains
= 1;
8801 map
->raid_level
= 0;
8802 map
->failed_disk_num
= -1;
8805 if (u
->direction
== R0_TO_R10
) {
8807 /* update slots in current disk list */
8808 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8812 /* create new *missing* disks */
8813 for (i
= 0; i
< map
->num_members
; i
++) {
8814 space
= *space_list
;
8817 *space_list
= *space
;
8819 memcpy(du
, super
->disks
, sizeof(*du
));
8823 du
->index
= (i
* 2) + 1;
8824 sprintf((char *)du
->disk
.serial
,
8825 " MISSING_%d", du
->index
);
8826 sprintf((char *)du
->serial
,
8827 "MISSING_%d", du
->index
);
8828 du
->next
= super
->missing
;
8829 super
->missing
= du
;
8831 /* create new dev and map */
8832 space
= *space_list
;
8835 *space_list
= *space
;
8836 dev_new
= (void *)space
;
8837 memcpy(dev_new
, dev
, sizeof(*dev
));
8838 /* update new map */
8839 map
= get_imsm_map(dev_new
, MAP_0
);
8840 map
->num_members
= map
->num_members
* 2;
8841 map
->map_state
= IMSM_T_STATE_DEGRADED
;
8842 map
->num_domains
= 2;
8843 map
->raid_level
= 1;
8844 /* replace dev<->dev_new */
8847 /* update disk order table */
8848 for (du
= super
->disks
; du
; du
= du
->next
)
8850 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8851 for (du
= super
->missing
; du
; du
= du
->next
)
8852 if (du
->index
>= 0) {
8853 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8854 mark_missing(dv
->dev
, &du
->disk
, du
->index
);
8860 static void imsm_process_update(struct supertype
*st
,
8861 struct metadata_update
*update
)
8864 * crack open the metadata_update envelope to find the update record
8865 * update can be one of:
8866 * update_reshape_container_disks - all the arrays in the container
8867 * are being reshaped to have more devices. We need to mark
8868 * the arrays for general migration and convert selected spares
8869 * into active devices.
8870 * update_activate_spare - a spare device has replaced a failed
8871 * device in an array, update the disk_ord_tbl. If this disk is
8872 * present in all member arrays then also clear the SPARE_DISK
8874 * update_create_array
8876 * update_rename_array
8877 * update_add_remove_disk
8879 struct intel_super
*super
= st
->sb
;
8880 struct imsm_super
*mpb
;
8881 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
8883 /* update requires a larger buf but the allocation failed */
8884 if (super
->next_len
&& !super
->next_buf
) {
8885 super
->next_len
= 0;
8889 if (super
->next_buf
) {
8890 memcpy(super
->next_buf
, super
->buf
, super
->len
);
8892 super
->len
= super
->next_len
;
8893 super
->buf
= super
->next_buf
;
8895 super
->next_len
= 0;
8896 super
->next_buf
= NULL
;
8899 mpb
= super
->anchor
;
8902 case update_general_migration_checkpoint
: {
8903 struct intel_dev
*id
;
8904 struct imsm_update_general_migration_checkpoint
*u
=
8905 (void *)update
->buf
;
8907 dprintf("called for update_general_migration_checkpoint\n");
8909 /* find device under general migration */
8910 for (id
= super
->devlist
; id
; id
= id
->next
) {
8911 if (is_gen_migration(id
->dev
)) {
8912 id
->dev
->vol
.curr_migr_unit
=
8913 __cpu_to_le32(u
->curr_migr_unit
);
8914 super
->updates_pending
++;
8919 case update_takeover
: {
8920 struct imsm_update_takeover
*u
= (void *)update
->buf
;
8921 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
8922 imsm_update_version_info(super
);
8923 super
->updates_pending
++;
8928 case update_reshape_container_disks
: {
8929 struct imsm_update_reshape
*u
= (void *)update
->buf
;
8930 if (apply_reshape_container_disks_update(
8931 u
, super
, &update
->space_list
))
8932 super
->updates_pending
++;
8935 case update_reshape_migration
: {
8936 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
8937 if (apply_reshape_migration_update(
8938 u
, super
, &update
->space_list
))
8939 super
->updates_pending
++;
8942 case update_size_change
: {
8943 struct imsm_update_size_change
*u
= (void *)update
->buf
;
8944 if (apply_size_change_update(u
, super
))
8945 super
->updates_pending
++;
8948 case update_activate_spare
: {
8949 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
8950 if (apply_update_activate_spare(u
, super
, st
->arrays
))
8951 super
->updates_pending
++;
8954 case update_create_array
: {
8955 /* someone wants to create a new array, we need to be aware of
8956 * a few races/collisions:
8957 * 1/ 'Create' called by two separate instances of mdadm
8958 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
8959 * devices that have since been assimilated via
8961 * In the event this update can not be carried out mdadm will
8962 * (FIX ME) notice that its update did not take hold.
8964 struct imsm_update_create_array
*u
= (void *) update
->buf
;
8965 struct intel_dev
*dv
;
8966 struct imsm_dev
*dev
;
8967 struct imsm_map
*map
, *new_map
;
8968 unsigned long long start
, end
;
8969 unsigned long long new_start
, new_end
;
8971 struct disk_info
*inf
;
8974 /* handle racing creates: first come first serve */
8975 if (u
->dev_idx
< mpb
->num_raid_devs
) {
8976 dprintf("subarray %d already defined\n", u
->dev_idx
);
8980 /* check update is next in sequence */
8981 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
8982 dprintf("can not create array %d expected index %d\n",
8983 u
->dev_idx
, mpb
->num_raid_devs
);
8987 new_map
= get_imsm_map(&u
->dev
, MAP_0
);
8988 new_start
= pba_of_lba0(new_map
);
8989 new_end
= new_start
+ blocks_per_member(new_map
);
8990 inf
= get_disk_info(u
);
8992 /* handle activate_spare versus create race:
8993 * check to make sure that overlapping arrays do not include
8996 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8997 dev
= get_imsm_dev(super
, i
);
8998 map
= get_imsm_map(dev
, MAP_0
);
8999 start
= pba_of_lba0(map
);
9000 end
= start
+ blocks_per_member(map
);
9001 if ((new_start
>= start
&& new_start
<= end
) ||
9002 (start
>= new_start
&& start
<= new_end
))
9007 if (disks_overlap(super
, i
, u
)) {
9008 dprintf("arrays overlap\n");
9013 /* check that prepare update was successful */
9014 if (!update
->space
) {
9015 dprintf("prepare update failed\n");
9019 /* check that all disks are still active before committing
9020 * changes. FIXME: could we instead handle this by creating a
9021 * degraded array? That's probably not what the user expects,
9022 * so better to drop this update on the floor.
9024 for (i
= 0; i
< new_map
->num_members
; i
++) {
9025 dl
= serial_to_dl(inf
[i
].serial
, super
);
9027 dprintf("disk disappeared\n");
9032 super
->updates_pending
++;
9034 /* convert spares to members and fixup ord_tbl */
9035 for (i
= 0; i
< new_map
->num_members
; i
++) {
9036 dl
= serial_to_dl(inf
[i
].serial
, super
);
9037 if (dl
->index
== -1) {
9038 dl
->index
= mpb
->num_disks
;
9040 dl
->disk
.status
|= CONFIGURED_DISK
;
9041 dl
->disk
.status
&= ~SPARE_DISK
;
9043 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
9048 update
->space
= NULL
;
9049 imsm_copy_dev(dev
, &u
->dev
);
9050 dv
->index
= u
->dev_idx
;
9051 dv
->next
= super
->devlist
;
9052 super
->devlist
= dv
;
9053 mpb
->num_raid_devs
++;
9055 imsm_update_version_info(super
);
9058 /* mdmon knows how to release update->space, but not
9059 * ((struct intel_dev *) update->space)->dev
9061 if (update
->space
) {
9067 case update_kill_array
: {
9068 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
9069 int victim
= u
->dev_idx
;
9070 struct active_array
*a
;
9071 struct intel_dev
**dp
;
9072 struct imsm_dev
*dev
;
9074 /* sanity check that we are not affecting the uuid of
9075 * active arrays, or deleting an active array
9077 * FIXME when immutable ids are available, but note that
9078 * we'll also need to fixup the invalidated/active
9079 * subarray indexes in mdstat
9081 for (a
= st
->arrays
; a
; a
= a
->next
)
9082 if (a
->info
.container_member
>= victim
)
9084 /* by definition if mdmon is running at least one array
9085 * is active in the container, so checking
9086 * mpb->num_raid_devs is just extra paranoia
9088 dev
= get_imsm_dev(super
, victim
);
9089 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
9090 dprintf("failed to delete subarray-%d\n", victim
);
9094 for (dp
= &super
->devlist
; *dp
;)
9095 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
9098 if ((*dp
)->index
> (unsigned)victim
)
9102 mpb
->num_raid_devs
--;
9103 super
->updates_pending
++;
9106 case update_rename_array
: {
9107 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
9108 char name
[MAX_RAID_SERIAL_LEN
+1];
9109 int target
= u
->dev_idx
;
9110 struct active_array
*a
;
9111 struct imsm_dev
*dev
;
9113 /* sanity check that we are not affecting the uuid of
9116 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
9117 name
[MAX_RAID_SERIAL_LEN
] = '\0';
9118 for (a
= st
->arrays
; a
; a
= a
->next
)
9119 if (a
->info
.container_member
== target
)
9121 dev
= get_imsm_dev(super
, u
->dev_idx
);
9122 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
9123 dprintf("failed to rename subarray-%d\n", target
);
9127 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
9128 super
->updates_pending
++;
9131 case update_add_remove_disk
: {
9132 /* we may be able to repair some arrays if disks are
9133 * being added, check the status of add_remove_disk
9134 * if discs has been added.
9136 if (add_remove_disk_update(super
)) {
9137 struct active_array
*a
;
9139 super
->updates_pending
++;
9140 for (a
= st
->arrays
; a
; a
= a
->next
)
9141 a
->check_degraded
= 1;
9145 case update_prealloc_badblocks_mem
:
9148 pr_err("error: unsuported process update type:(type: %d)\n", type
);
9152 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
);
9154 static int imsm_prepare_update(struct supertype
*st
,
9155 struct metadata_update
*update
)
9158 * Allocate space to hold new disk entries, raid-device entries or a new
9159 * mpb if necessary. The manager synchronously waits for updates to
9160 * complete in the monitor, so new mpb buffers allocated here can be
9161 * integrated by the monitor thread without worrying about live pointers
9162 * in the manager thread.
9164 enum imsm_update_type type
;
9165 struct intel_super
*super
= st
->sb
;
9166 unsigned int sector_size
= super
->sector_size
;
9167 struct imsm_super
*mpb
= super
->anchor
;
9171 if (update
->len
< (int)sizeof(type
))
9174 type
= *(enum imsm_update_type
*) update
->buf
;
9177 case update_general_migration_checkpoint
:
9178 if (update
->len
< (int)sizeof(struct imsm_update_general_migration_checkpoint
))
9180 dprintf("called for update_general_migration_checkpoint\n");
9182 case update_takeover
: {
9183 struct imsm_update_takeover
*u
= (void *)update
->buf
;
9184 if (update
->len
< (int)sizeof(*u
))
9186 if (u
->direction
== R0_TO_R10
) {
9187 void **tail
= (void **)&update
->space_list
;
9188 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
9189 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9190 int num_members
= map
->num_members
;
9193 /* allocate memory for added disks */
9194 for (i
= 0; i
< num_members
; i
++) {
9195 size
= sizeof(struct dl
);
9196 space
= xmalloc(size
);
9201 /* allocate memory for new device */
9202 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
9203 (num_members
* sizeof(__u32
));
9204 space
= xmalloc(size
);
9208 len
= disks_to_mpb_size(num_members
* 2);
9213 case update_reshape_container_disks
: {
9214 /* Every raid device in the container is about to
9215 * gain some more devices, and we will enter a
9217 * So each 'imsm_map' will be bigger, and the imsm_vol
9218 * will now hold 2 of them.
9219 * Thus we need new 'struct imsm_dev' allocations sized
9220 * as sizeof_imsm_dev but with more devices in both maps.
9222 struct imsm_update_reshape
*u
= (void *)update
->buf
;
9223 struct intel_dev
*dl
;
9224 void **space_tail
= (void**)&update
->space_list
;
9226 if (update
->len
< (int)sizeof(*u
))
9229 dprintf("for update_reshape\n");
9231 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
9232 int size
= sizeof_imsm_dev(dl
->dev
, 1);
9234 if (u
->new_raid_disks
> u
->old_raid_disks
)
9235 size
+= sizeof(__u32
)*2*
9236 (u
->new_raid_disks
- u
->old_raid_disks
);
9243 len
= disks_to_mpb_size(u
->new_raid_disks
);
9244 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
9247 case update_reshape_migration
: {
9248 /* for migration level 0->5 we need to add disks
9249 * so the same as for container operation we will copy
9250 * device to the bigger location.
9251 * in memory prepared device and new disk area are prepared
9252 * for usage in process update
9254 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
9255 struct intel_dev
*id
;
9256 void **space_tail
= (void **)&update
->space_list
;
9259 int current_level
= -1;
9261 if (update
->len
< (int)sizeof(*u
))
9264 dprintf("for update_reshape\n");
9266 /* add space for bigger array in update
9268 for (id
= super
->devlist
; id
; id
= id
->next
) {
9269 if (id
->index
== (unsigned)u
->subdev
) {
9270 size
= sizeof_imsm_dev(id
->dev
, 1);
9271 if (u
->new_raid_disks
> u
->old_raid_disks
)
9272 size
+= sizeof(__u32
)*2*
9273 (u
->new_raid_disks
- u
->old_raid_disks
);
9281 if (update
->space_list
== NULL
)
9284 /* add space for disk in update
9286 size
= sizeof(struct dl
);
9292 /* add spare device to update
9294 for (id
= super
->devlist
; id
; id
= id
->next
)
9295 if (id
->index
== (unsigned)u
->subdev
) {
9296 struct imsm_dev
*dev
;
9297 struct imsm_map
*map
;
9299 dev
= get_imsm_dev(super
, u
->subdev
);
9300 map
= get_imsm_map(dev
, MAP_0
);
9301 current_level
= map
->raid_level
;
9304 if (u
->new_level
== 5 && u
->new_level
!= current_level
) {
9305 struct mdinfo
*spares
;
9307 spares
= get_spares_for_grow(st
);
9315 makedev(dev
->disk
.major
,
9317 dl
= get_disk_super(super
,
9320 dl
->index
= u
->old_raid_disks
;
9326 len
= disks_to_mpb_size(u
->new_raid_disks
);
9327 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
9330 case update_size_change
: {
9331 if (update
->len
< (int)sizeof(struct imsm_update_size_change
))
9335 case update_activate_spare
: {
9336 if (update
->len
< (int)sizeof(struct imsm_update_activate_spare
))
9340 case update_create_array
: {
9341 struct imsm_update_create_array
*u
= (void *) update
->buf
;
9342 struct intel_dev
*dv
;
9343 struct imsm_dev
*dev
= &u
->dev
;
9344 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9346 struct disk_info
*inf
;
9350 if (update
->len
< (int)sizeof(*u
))
9353 inf
= get_disk_info(u
);
9354 len
= sizeof_imsm_dev(dev
, 1);
9355 /* allocate a new super->devlist entry */
9356 dv
= xmalloc(sizeof(*dv
));
9357 dv
->dev
= xmalloc(len
);
9360 /* count how many spares will be converted to members */
9361 for (i
= 0; i
< map
->num_members
; i
++) {
9362 dl
= serial_to_dl(inf
[i
].serial
, super
);
9364 /* hmm maybe it failed?, nothing we can do about
9369 if (count_memberships(dl
, super
) == 0)
9372 len
+= activate
* sizeof(struct imsm_disk
);
9375 case update_kill_array
: {
9376 if (update
->len
< (int)sizeof(struct imsm_update_kill_array
))
9380 case update_rename_array
: {
9381 if (update
->len
< (int)sizeof(struct imsm_update_rename_array
))
9385 case update_add_remove_disk
:
9386 /* no update->len needed */
9388 case update_prealloc_badblocks_mem
:
9389 super
->extra_space
+= sizeof(struct bbm_log
) -
9390 get_imsm_bbm_log_size(super
->bbm_log
);
9396 /* check if we need a larger metadata buffer */
9397 if (super
->next_buf
)
9398 buf_len
= super
->next_len
;
9400 buf_len
= super
->len
;
9402 if (__le32_to_cpu(mpb
->mpb_size
) + super
->extra_space
+ len
> buf_len
) {
9403 /* ok we need a larger buf than what is currently allocated
9404 * if this allocation fails process_update will notice that
9405 * ->next_len is set and ->next_buf is NULL
9407 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) +
9408 super
->extra_space
+ len
, sector_size
);
9409 if (super
->next_buf
)
9410 free(super
->next_buf
);
9412 super
->next_len
= buf_len
;
9413 if (posix_memalign(&super
->next_buf
, sector_size
, buf_len
) == 0)
9414 memset(super
->next_buf
, 0, buf_len
);
9416 super
->next_buf
= NULL
;
9421 /* must be called while manager is quiesced */
9422 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
9424 struct imsm_super
*mpb
= super
->anchor
;
9426 struct imsm_dev
*dev
;
9427 struct imsm_map
*map
;
9428 int i
, j
, num_members
;
9431 dprintf("deleting device[%d] from imsm_super\n", index
);
9433 /* shift all indexes down one */
9434 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
9435 if (iter
->index
> (int)index
)
9437 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
9438 if (iter
->index
> (int)index
)
9441 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
9442 dev
= get_imsm_dev(super
, i
);
9443 map
= get_imsm_map(dev
, MAP_0
);
9444 num_members
= map
->num_members
;
9445 for (j
= 0; j
< num_members
; j
++) {
9446 /* update ord entries being careful not to propagate
9447 * ord-flags to the first map
9449 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_X
);
9451 if (ord_to_idx(ord
) <= index
)
9454 map
= get_imsm_map(dev
, MAP_0
);
9455 set_imsm_ord_tbl_ent(map
, j
, ord_to_idx(ord
- 1));
9456 map
= get_imsm_map(dev
, MAP_1
);
9458 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
9463 super
->updates_pending
++;
9465 struct dl
*dl
= *dlp
;
9467 *dlp
= (*dlp
)->next
;
9468 __free_imsm_disk(dl
);
9471 #endif /* MDASSEMBLE */
9473 static void close_targets(int *targets
, int new_disks
)
9480 for (i
= 0; i
< new_disks
; i
++) {
9481 if (targets
[i
] >= 0) {
9488 static int imsm_get_allowed_degradation(int level
, int raid_disks
,
9489 struct intel_super
*super
,
9490 struct imsm_dev
*dev
)
9496 struct imsm_map
*map
;
9499 ret_val
= raid_disks
/2;
9500 /* check map if all disks pairs not failed
9503 map
= get_imsm_map(dev
, MAP_0
);
9504 for (i
= 0; i
< ret_val
; i
++) {
9505 int degradation
= 0;
9506 if (get_imsm_disk(super
, i
) == NULL
)
9508 if (get_imsm_disk(super
, i
+ 1) == NULL
)
9510 if (degradation
== 2)
9513 map
= get_imsm_map(dev
, MAP_1
);
9514 /* if there is no second map
9515 * result can be returned
9519 /* check degradation in second map
9521 for (i
= 0; i
< ret_val
; i
++) {
9522 int degradation
= 0;
9523 if (get_imsm_disk(super
, i
) == NULL
)
9525 if (get_imsm_disk(super
, i
+ 1) == NULL
)
9527 if (degradation
== 2)
9541 /*******************************************************************************
9542 * Function: open_backup_targets
9543 * Description: Function opens file descriptors for all devices given in
9546 * info : general array info
9547 * raid_disks : number of disks
9548 * raid_fds : table of device's file descriptors
9549 * super : intel super for raid10 degradation check
9550 * dev : intel device for raid10 degradation check
9554 ******************************************************************************/
9555 int open_backup_targets(struct mdinfo
*info
, int raid_disks
, int *raid_fds
,
9556 struct intel_super
*super
, struct imsm_dev
*dev
)
9562 for (i
= 0; i
< raid_disks
; i
++)
9565 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
9568 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
)) {
9569 dprintf("disk is faulty!!\n");
9573 if (sd
->disk
.raid_disk
>= raid_disks
|| sd
->disk
.raid_disk
< 0)
9576 dn
= map_dev(sd
->disk
.major
,
9578 raid_fds
[sd
->disk
.raid_disk
] = dev_open(dn
, O_RDWR
);
9579 if (raid_fds
[sd
->disk
.raid_disk
] < 0) {
9580 pr_err("cannot open component\n");
9585 /* check if maximum array degradation level is not exceeded
9587 if ((raid_disks
- opened
) >
9588 imsm_get_allowed_degradation(info
->new_level
, raid_disks
,
9590 pr_err("Not enough disks can be opened.\n");
9591 close_targets(raid_fds
, raid_disks
);
9597 /*******************************************************************************
9598 * Function: validate_container_imsm
9599 * Description: This routine validates container after assemble,
9600 * eg. if devices in container are under the same controller.
9603 * info : linked list with info about devices used in array
9607 ******************************************************************************/
9608 int validate_container_imsm(struct mdinfo
*info
)
9610 if (check_env("IMSM_NO_PLATFORM"))
9613 struct sys_dev
*idev
;
9614 struct sys_dev
*hba
= NULL
;
9615 struct sys_dev
*intel_devices
= find_intel_devices();
9616 char *dev_path
= devt_to_devpath(makedev(info
->disk
.major
,
9619 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
9620 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
9629 pr_err("WARNING - Cannot detect HBA for device %s!\n",
9630 devid2kname(makedev(info
->disk
.major
, info
->disk
.minor
)));
9634 const struct imsm_orom
*orom
= get_orom_by_device_id(hba
->dev_id
);
9637 for (dev
= info
->next
; dev
; dev
= dev
->next
) {
9638 dev_path
= devt_to_devpath(makedev(dev
->disk
.major
, dev
->disk
.minor
));
9640 struct sys_dev
*hba2
= NULL
;
9641 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
9642 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
9650 const struct imsm_orom
*orom2
= hba2
== NULL
? NULL
:
9651 get_orom_by_device_id(hba2
->dev_id
);
9653 if (hba2
&& hba
->type
!= hba2
->type
) {
9654 pr_err("WARNING - HBAs of devices do not match %s != %s\n",
9655 get_sys_dev_type(hba
->type
), get_sys_dev_type(hba2
->type
));
9659 if (orom
!= orom2
) {
9660 pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n"
9661 " This operation is not supported and can lead to data loss.\n");
9666 pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n"
9667 " This operation is not supported and can lead to data loss.\n");
9675 /*******************************************************************************
9676 * Function: init_migr_record_imsm
9677 * Description: Function inits imsm migration record
9679 * super : imsm internal array info
9680 * dev : device under migration
9681 * info : general array info to find the smallest device
9684 ******************************************************************************/
9685 void init_migr_record_imsm(struct supertype
*st
, struct imsm_dev
*dev
,
9686 struct mdinfo
*info
)
9688 struct intel_super
*super
= st
->sb
;
9689 struct migr_record
*migr_rec
= super
->migr_rec
;
9691 unsigned long long dsize
, dev_sectors
;
9692 long long unsigned min_dev_sectors
= -1LLU;
9696 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
9697 struct imsm_map
*map_src
= get_imsm_map(dev
, MAP_1
);
9698 unsigned long long num_migr_units
;
9699 unsigned long long array_blocks
;
9701 memset(migr_rec
, 0, sizeof(struct migr_record
));
9702 migr_rec
->family_num
= __cpu_to_le32(super
->anchor
->family_num
);
9704 /* only ascending reshape supported now */
9705 migr_rec
->ascending_migr
= __cpu_to_le32(1);
9707 migr_rec
->dest_depth_per_unit
= GEN_MIGR_AREA_SIZE
/
9708 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
9709 migr_rec
->dest_depth_per_unit
*=
9710 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
9711 new_data_disks
= imsm_num_data_members(dev
, MAP_0
);
9712 migr_rec
->blocks_per_unit
=
9713 __cpu_to_le32(migr_rec
->dest_depth_per_unit
* new_data_disks
);
9714 migr_rec
->dest_depth_per_unit
=
9715 __cpu_to_le32(migr_rec
->dest_depth_per_unit
);
9716 array_blocks
= info
->component_size
* new_data_disks
;
9718 array_blocks
/ __le32_to_cpu(migr_rec
->blocks_per_unit
);
9720 if (array_blocks
% __le32_to_cpu(migr_rec
->blocks_per_unit
))
9722 migr_rec
->num_migr_units
= __cpu_to_le32(num_migr_units
);
9724 migr_rec
->post_migr_vol_cap
= dev
->size_low
;
9725 migr_rec
->post_migr_vol_cap_hi
= dev
->size_high
;
9727 /* Find the smallest dev */
9728 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
9729 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
9730 fd
= dev_open(nm
, O_RDONLY
);
9733 get_dev_size(fd
, NULL
, &dsize
);
9734 dev_sectors
= dsize
/ 512;
9735 if (dev_sectors
< min_dev_sectors
)
9736 min_dev_sectors
= dev_sectors
;
9739 migr_rec
->ckpt_area_pba
= __cpu_to_le32(min_dev_sectors
-
9740 RAID_DISK_RESERVED_BLOCKS_IMSM_HI
);
9742 write_imsm_migr_rec(st
);
9747 /*******************************************************************************
9748 * Function: save_backup_imsm
9749 * Description: Function saves critical data stripes to Migration Copy Area
9750 * and updates the current migration unit status.
9751 * Use restore_stripes() to form a destination stripe,
9752 * and to write it to the Copy Area.
9754 * st : supertype information
9755 * dev : imsm device that backup is saved for
9756 * info : general array info
9757 * buf : input buffer
9758 * length : length of data to backup (blocks_per_unit)
9762 ******************************************************************************/
9763 int save_backup_imsm(struct supertype
*st
,
9764 struct imsm_dev
*dev
,
9765 struct mdinfo
*info
,
9770 struct intel_super
*super
= st
->sb
;
9771 unsigned long long *target_offsets
;
9774 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
9775 int new_disks
= map_dest
->num_members
;
9776 int dest_layout
= 0;
9778 unsigned long long start
;
9779 int data_disks
= imsm_num_data_members(dev
, MAP_0
);
9781 targets
= xmalloc(new_disks
* sizeof(int));
9783 for (i
= 0; i
< new_disks
; i
++)
9786 target_offsets
= xcalloc(new_disks
, sizeof(unsigned long long));
9788 start
= info
->reshape_progress
* 512;
9789 for (i
= 0; i
< new_disks
; i
++) {
9790 target_offsets
[i
] = (unsigned long long)
9791 __le32_to_cpu(super
->migr_rec
->ckpt_area_pba
) * 512;
9792 /* move back copy area adderss, it will be moved forward
9793 * in restore_stripes() using start input variable
9795 target_offsets
[i
] -= start
/data_disks
;
9798 if (open_backup_targets(info
, new_disks
, targets
,
9802 dest_layout
= imsm_level_to_layout(map_dest
->raid_level
);
9803 dest_chunk
= __le16_to_cpu(map_dest
->blocks_per_strip
) * 512;
9805 if (restore_stripes(targets
, /* list of dest devices */
9806 target_offsets
, /* migration record offsets */
9809 map_dest
->raid_level
,
9811 -1, /* source backup file descriptor */
9812 0, /* input buf offset
9813 * always 0 buf is already offseted */
9817 pr_err("Error restoring stripes\n");
9825 close_targets(targets
, new_disks
);
9828 free(target_offsets
);
9833 /*******************************************************************************
9834 * Function: save_checkpoint_imsm
9835 * Description: Function called for current unit status update
9836 * in the migration record. It writes it to disk.
9838 * super : imsm internal array info
9839 * info : general array info
9843 * 2: failure, means no valid migration record
9844 * / no general migration in progress /
9845 ******************************************************************************/
9846 int save_checkpoint_imsm(struct supertype
*st
, struct mdinfo
*info
, int state
)
9848 struct intel_super
*super
= st
->sb
;
9849 unsigned long long blocks_per_unit
;
9850 unsigned long long curr_migr_unit
;
9852 if (load_imsm_migr_rec(super
, info
) != 0) {
9853 dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n");
9857 blocks_per_unit
= __le32_to_cpu(super
->migr_rec
->blocks_per_unit
);
9858 if (blocks_per_unit
== 0) {
9859 dprintf("imsm: no migration in progress.\n");
9862 curr_migr_unit
= info
->reshape_progress
/ blocks_per_unit
;
9863 /* check if array is alligned to copy area
9864 * if it is not alligned, add one to current migration unit value
9865 * this can happend on array reshape finish only
9867 if (info
->reshape_progress
% blocks_per_unit
)
9870 super
->migr_rec
->curr_migr_unit
=
9871 __cpu_to_le32(curr_migr_unit
);
9872 super
->migr_rec
->rec_status
= __cpu_to_le32(state
);
9873 super
->migr_rec
->dest_1st_member_lba
=
9874 __cpu_to_le32(curr_migr_unit
*
9875 __le32_to_cpu(super
->migr_rec
->dest_depth_per_unit
));
9876 if (write_imsm_migr_rec(st
) < 0) {
9877 dprintf("imsm: Cannot write migration record outside backup area\n");
9884 /*******************************************************************************
9885 * Function: recover_backup_imsm
9886 * Description: Function recovers critical data from the Migration Copy Area
9887 * while assembling an array.
9889 * super : imsm internal array info
9890 * info : general array info
9892 * 0 : success (or there is no data to recover)
9894 ******************************************************************************/
9895 int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
)
9897 struct intel_super
*super
= st
->sb
;
9898 struct migr_record
*migr_rec
= super
->migr_rec
;
9899 struct imsm_map
*map_dest
;
9900 struct intel_dev
*id
= NULL
;
9901 unsigned long long read_offset
;
9902 unsigned long long write_offset
;
9904 int *targets
= NULL
;
9905 int new_disks
, i
, err
;
9908 unsigned int sector_size
= super
->sector_size
;
9909 unsigned long curr_migr_unit
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
9910 unsigned long num_migr_units
= __le32_to_cpu(migr_rec
->num_migr_units
);
9912 int skipped_disks
= 0;
9914 err
= sysfs_get_str(info
, NULL
, "array_state", (char *)buffer
, 20);
9918 /* recover data only during assemblation */
9919 if (strncmp(buffer
, "inactive", 8) != 0)
9921 /* no data to recover */
9922 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
9924 if (curr_migr_unit
>= num_migr_units
)
9927 /* find device during reshape */
9928 for (id
= super
->devlist
; id
; id
= id
->next
)
9929 if (is_gen_migration(id
->dev
))
9934 map_dest
= get_imsm_map(id
->dev
, MAP_0
);
9935 new_disks
= map_dest
->num_members
;
9937 read_offset
= (unsigned long long)
9938 __le32_to_cpu(migr_rec
->ckpt_area_pba
) * 512;
9940 write_offset
= ((unsigned long long)
9941 __le32_to_cpu(migr_rec
->dest_1st_member_lba
) +
9942 pba_of_lba0(map_dest
)) * 512;
9944 unit_len
= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
9945 if (posix_memalign((void **)&buf
, sector_size
, unit_len
) != 0)
9947 targets
= xcalloc(new_disks
, sizeof(int));
9949 if (open_backup_targets(info
, new_disks
, targets
, super
, id
->dev
)) {
9950 pr_err("Cannot open some devices belonging to array.\n");
9954 for (i
= 0; i
< new_disks
; i
++) {
9955 if (targets
[i
] < 0) {
9959 if (lseek64(targets
[i
], read_offset
, SEEK_SET
) < 0) {
9960 pr_err("Cannot seek to block: %s\n",
9965 if ((unsigned)read(targets
[i
], buf
, unit_len
) != unit_len
) {
9966 pr_err("Cannot read copy area block: %s\n",
9971 if (lseek64(targets
[i
], write_offset
, SEEK_SET
) < 0) {
9972 pr_err("Cannot seek to block: %s\n",
9977 if ((unsigned)write(targets
[i
], buf
, unit_len
) != unit_len
) {
9978 pr_err("Cannot restore block: %s\n",
9985 if (skipped_disks
> imsm_get_allowed_degradation(info
->new_level
,
9989 pr_err("Cannot restore data from backup. Too many failed disks\n");
9993 if (save_checkpoint_imsm(st
, info
, UNIT_SRC_NORMAL
)) {
9994 /* ignore error == 2, this can mean end of reshape here
9996 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n");
10002 for (i
= 0; i
< new_disks
; i
++)
10011 static char disk_by_path
[] = "/dev/disk/by-path/";
10013 static const char *imsm_get_disk_controller_domain(const char *path
)
10015 char disk_path
[PATH_MAX
];
10019 strcpy(disk_path
, disk_by_path
);
10020 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
10021 if (stat(disk_path
, &st
) == 0) {
10022 struct sys_dev
* hba
;
10025 path
= devt_to_devpath(st
.st_rdev
);
10028 hba
= find_disk_attached_hba(-1, path
);
10029 if (hba
&& hba
->type
== SYS_DEV_SAS
)
10031 else if (hba
&& hba
->type
== SYS_DEV_SATA
)
10035 dprintf("path: %s hba: %s attached: %s\n",
10036 path
, (hba
) ? hba
->path
: "NULL", drv
);
10042 static char *imsm_find_array_devnm_by_subdev(int subdev
, char *container
)
10044 static char devnm
[32];
10045 char subdev_name
[20];
10046 struct mdstat_ent
*mdstat
;
10048 sprintf(subdev_name
, "%d", subdev
);
10049 mdstat
= mdstat_by_subdev(subdev_name
, container
);
10053 strcpy(devnm
, mdstat
->devnm
);
10054 free_mdstat(mdstat
);
10058 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
10059 struct geo_params
*geo
,
10060 int *old_raid_disks
,
10063 /* currently we only support increasing the number of devices
10064 * for a container. This increases the number of device for each
10065 * member array. They must all be RAID0 or RAID5.
10068 struct mdinfo
*info
, *member
;
10069 int devices_that_can_grow
= 0;
10071 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st
->devnm
);
10073 if (geo
->size
> 0 ||
10074 geo
->level
!= UnSet
||
10075 geo
->layout
!= UnSet
||
10076 geo
->chunksize
!= 0 ||
10077 geo
->raid_disks
== UnSet
) {
10078 dprintf("imsm: Container operation is allowed for raid disks number change only.\n");
10082 if (direction
== ROLLBACK_METADATA_CHANGES
) {
10083 dprintf("imsm: Metadata changes rollback is not supported for container operation.\n");
10087 info
= container_content_imsm(st
, NULL
);
10088 for (member
= info
; member
; member
= member
->next
) {
10091 dprintf("imsm: checking device_num: %i\n",
10092 member
->container_member
);
10094 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
10095 /* we work on container for Online Capacity Expansion
10096 * only so raid_disks has to grow
10098 dprintf("imsm: for container operation raid disks increase is required\n");
10102 if (info
->array
.level
!= 0 && info
->array
.level
!= 5) {
10103 /* we cannot use this container with other raid level
10105 dprintf("imsm: for container operation wrong raid level (%i) detected\n",
10106 info
->array
.level
);
10109 /* check for platform support
10110 * for this raid level configuration
10112 struct intel_super
*super
= st
->sb
;
10113 if (!is_raid_level_supported(super
->orom
,
10114 member
->array
.level
,
10115 geo
->raid_disks
)) {
10116 dprintf("platform does not support raid%d with %d disk%s\n",
10119 geo
->raid_disks
> 1 ? "s" : "");
10122 /* check if component size is aligned to chunk size
10124 if (info
->component_size
%
10125 (info
->array
.chunk_size
/512)) {
10126 dprintf("Component size is not aligned to chunk size\n");
10131 if (*old_raid_disks
&&
10132 info
->array
.raid_disks
!= *old_raid_disks
)
10134 *old_raid_disks
= info
->array
.raid_disks
;
10136 /* All raid5 and raid0 volumes in container
10137 * have to be ready for Online Capacity Expansion
10138 * so they need to be assembled. We have already
10139 * checked that no recovery etc is happening.
10141 result
= imsm_find_array_devnm_by_subdev(member
->container_member
,
10142 st
->container_devnm
);
10143 if (result
== NULL
) {
10144 dprintf("imsm: cannot find array\n");
10147 devices_that_can_grow
++;
10150 if (!member
&& devices_that_can_grow
)
10154 dprintf("Container operation allowed\n");
10156 dprintf("Error: %i\n", ret_val
);
10161 /* Function: get_spares_for_grow
10162 * Description: Allocates memory and creates list of spare devices
10163 * avaliable in container. Checks if spare drive size is acceptable.
10164 * Parameters: Pointer to the supertype structure
10165 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
10168 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
10170 unsigned long long min_size
= min_acceptable_spare_size_imsm(st
);
10171 return container_choose_spares(st
, min_size
, NULL
, NULL
, NULL
, 0);
10174 /******************************************************************************
10175 * function: imsm_create_metadata_update_for_reshape
10176 * Function creates update for whole IMSM container.
10178 ******************************************************************************/
10179 static int imsm_create_metadata_update_for_reshape(
10180 struct supertype
*st
,
10181 struct geo_params
*geo
,
10182 int old_raid_disks
,
10183 struct imsm_update_reshape
**updatep
)
10185 struct intel_super
*super
= st
->sb
;
10186 struct imsm_super
*mpb
= super
->anchor
;
10187 int update_memory_size
;
10188 struct imsm_update_reshape
*u
;
10189 struct mdinfo
*spares
;
10192 struct mdinfo
*dev
;
10194 dprintf("(enter) raid_disks = %i\n", geo
->raid_disks
);
10196 delta_disks
= geo
->raid_disks
- old_raid_disks
;
10198 /* size of all update data without anchor */
10199 update_memory_size
= sizeof(struct imsm_update_reshape
);
10201 /* now add space for spare disks that we need to add. */
10202 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
10204 u
= xcalloc(1, update_memory_size
);
10205 u
->type
= update_reshape_container_disks
;
10206 u
->old_raid_disks
= old_raid_disks
;
10207 u
->new_raid_disks
= geo
->raid_disks
;
10209 /* now get spare disks list
10211 spares
= get_spares_for_grow(st
);
10214 || delta_disks
> spares
->array
.spare_disks
) {
10215 pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo
->dev_name
);
10220 /* we have got spares
10221 * update disk list in imsm_disk list table in anchor
10223 dprintf("imsm: %i spares are available.\n\n",
10224 spares
->array
.spare_disks
);
10226 dev
= spares
->devs
;
10227 for (i
= 0; i
< delta_disks
; i
++) {
10232 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
10234 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
10235 dl
->index
= mpb
->num_disks
;
10243 sysfs_free(spares
);
10245 dprintf("imsm: reshape update preparation :");
10246 if (i
== delta_disks
) {
10247 dprintf_cont(" OK\n");
10249 return update_memory_size
;
10252 dprintf_cont(" Error\n");
10257 /******************************************************************************
10258 * function: imsm_create_metadata_update_for_size_change()
10259 * Creates update for IMSM array for array size change.
10261 ******************************************************************************/
10262 static int imsm_create_metadata_update_for_size_change(
10263 struct supertype
*st
,
10264 struct geo_params
*geo
,
10265 struct imsm_update_size_change
**updatep
)
10267 struct intel_super
*super
= st
->sb
;
10268 int update_memory_size
;
10269 struct imsm_update_size_change
*u
;
10271 dprintf("(enter) New size = %llu\n", geo
->size
);
10273 /* size of all update data without anchor */
10274 update_memory_size
= sizeof(struct imsm_update_size_change
);
10276 u
= xcalloc(1, update_memory_size
);
10277 u
->type
= update_size_change
;
10278 u
->subdev
= super
->current_vol
;
10279 u
->new_size
= geo
->size
;
10281 dprintf("imsm: reshape update preparation : OK\n");
10284 return update_memory_size
;
10287 /******************************************************************************
10288 * function: imsm_create_metadata_update_for_migration()
10289 * Creates update for IMSM array.
10291 ******************************************************************************/
10292 static int imsm_create_metadata_update_for_migration(
10293 struct supertype
*st
,
10294 struct geo_params
*geo
,
10295 struct imsm_update_reshape_migration
**updatep
)
10297 struct intel_super
*super
= st
->sb
;
10298 int update_memory_size
;
10299 struct imsm_update_reshape_migration
*u
;
10300 struct imsm_dev
*dev
;
10301 int previous_level
= -1;
10303 dprintf("(enter) New Level = %i\n", geo
->level
);
10305 /* size of all update data without anchor */
10306 update_memory_size
= sizeof(struct imsm_update_reshape_migration
);
10308 u
= xcalloc(1, update_memory_size
);
10309 u
->type
= update_reshape_migration
;
10310 u
->subdev
= super
->current_vol
;
10311 u
->new_level
= geo
->level
;
10312 u
->new_layout
= geo
->layout
;
10313 u
->new_raid_disks
= u
->old_raid_disks
= geo
->raid_disks
;
10314 u
->new_disks
[0] = -1;
10315 u
->new_chunksize
= -1;
10317 dev
= get_imsm_dev(super
, u
->subdev
);
10319 struct imsm_map
*map
;
10321 map
= get_imsm_map(dev
, MAP_0
);
10323 int current_chunk_size
=
10324 __le16_to_cpu(map
->blocks_per_strip
) / 2;
10326 if (geo
->chunksize
!= current_chunk_size
) {
10327 u
->new_chunksize
= geo
->chunksize
/ 1024;
10328 dprintf("imsm: chunk size change from %i to %i\n",
10329 current_chunk_size
, u
->new_chunksize
);
10331 previous_level
= map
->raid_level
;
10334 if (geo
->level
== 5 && previous_level
== 0) {
10335 struct mdinfo
*spares
= NULL
;
10337 u
->new_raid_disks
++;
10338 spares
= get_spares_for_grow(st
);
10339 if (spares
== NULL
|| spares
->array
.spare_disks
< 1) {
10341 sysfs_free(spares
);
10342 update_memory_size
= 0;
10343 dprintf("error: cannot get spare device for requested migration");
10346 sysfs_free(spares
);
10348 dprintf("imsm: reshape update preparation : OK\n");
10351 return update_memory_size
;
10354 static void imsm_update_metadata_locally(struct supertype
*st
,
10355 void *buf
, int len
)
10357 struct metadata_update mu
;
10362 mu
.space_list
= NULL
;
10364 if (imsm_prepare_update(st
, &mu
))
10365 imsm_process_update(st
, &mu
);
10367 while (mu
.space_list
) {
10368 void **space
= mu
.space_list
;
10369 mu
.space_list
= *space
;
10374 /***************************************************************************
10375 * Function: imsm_analyze_change
10376 * Description: Function analyze change for single volume
10377 * and validate if transition is supported
10378 * Parameters: Geometry parameters, supertype structure,
10379 * metadata change direction (apply/rollback)
10380 * Returns: Operation type code on success, -1 if fail
10381 ****************************************************************************/
10382 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
10383 struct geo_params
*geo
,
10386 struct mdinfo info
;
10388 int check_devs
= 0;
10390 /* number of added/removed disks in operation result */
10391 int devNumChange
= 0;
10392 /* imsm compatible layout value for array geometry verification */
10393 int imsm_layout
= -1;
10395 struct imsm_dev
*dev
;
10396 struct intel_super
*super
;
10397 unsigned long long current_size
;
10398 unsigned long long free_size
;
10399 unsigned long long max_size
;
10402 getinfo_super_imsm_volume(st
, &info
, NULL
);
10403 if (geo
->level
!= info
.array
.level
&& geo
->level
>= 0 &&
10404 geo
->level
!= UnSet
) {
10405 switch (info
.array
.level
) {
10407 if (geo
->level
== 5) {
10408 change
= CH_MIGRATION
;
10409 if (geo
->layout
!= ALGORITHM_LEFT_ASYMMETRIC
) {
10410 pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n");
10412 goto analyse_change_exit
;
10414 imsm_layout
= geo
->layout
;
10416 devNumChange
= 1; /* parity disk added */
10417 } else if (geo
->level
== 10) {
10418 change
= CH_TAKEOVER
;
10420 devNumChange
= 2; /* two mirrors added */
10421 imsm_layout
= 0x102; /* imsm supported layout */
10426 if (geo
->level
== 0) {
10427 change
= CH_TAKEOVER
;
10429 devNumChange
= -(geo
->raid_disks
/2);
10430 imsm_layout
= 0; /* imsm raid0 layout */
10434 if (change
== -1) {
10435 pr_err("Error. Level Migration from %d to %d not supported!\n",
10436 info
.array
.level
, geo
->level
);
10437 goto analyse_change_exit
;
10440 geo
->level
= info
.array
.level
;
10442 if (geo
->layout
!= info
.array
.layout
&&
10443 (geo
->layout
!= UnSet
&& geo
->layout
!= -1)) {
10444 change
= CH_MIGRATION
;
10445 if (info
.array
.layout
== 0 && info
.array
.level
== 5 &&
10446 geo
->layout
== 5) {
10447 /* reshape 5 -> 4 */
10448 } else if (info
.array
.layout
== 5 && info
.array
.level
== 5 &&
10449 geo
->layout
== 0) {
10450 /* reshape 4 -> 5 */
10454 pr_err("Error. Layout Migration from %d to %d not supported!\n",
10455 info
.array
.layout
, geo
->layout
);
10457 goto analyse_change_exit
;
10460 geo
->layout
= info
.array
.layout
;
10461 if (imsm_layout
== -1)
10462 imsm_layout
= info
.array
.layout
;
10465 if (geo
->chunksize
> 0 && geo
->chunksize
!= UnSet
&&
10466 geo
->chunksize
!= info
.array
.chunk_size
) {
10467 if (info
.array
.level
== 10) {
10468 pr_err("Error. Chunk size change for RAID 10 is not supported.\n");
10470 goto analyse_change_exit
;
10472 change
= CH_MIGRATION
;
10474 geo
->chunksize
= info
.array
.chunk_size
;
10477 chunk
= geo
->chunksize
/ 1024;
10480 dev
= get_imsm_dev(super
, super
->current_vol
);
10481 data_disks
= imsm_num_data_members(dev
, MAP_0
);
10482 /* compute current size per disk member
10484 current_size
= info
.custom_array_size
/ data_disks
;
10486 if (geo
->size
> 0 && geo
->size
!= MAX_SIZE
) {
10487 /* align component size
10489 geo
->size
= imsm_component_size_aligment_check(
10490 get_imsm_raid_level(dev
->vol
.map
),
10491 chunk
* 1024, super
->sector_size
,
10493 if (geo
->size
== 0) {
10494 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is 0).\n",
10496 goto analyse_change_exit
;
10500 if (current_size
!= geo
->size
&& geo
->size
> 0) {
10501 if (change
!= -1) {
10502 pr_err("Error. Size change should be the only one at a time.\n");
10504 goto analyse_change_exit
;
10506 if ((super
->current_vol
+ 1) != super
->anchor
->num_raid_devs
) {
10507 pr_err("Error. The last volume in container can be expanded only (%i/%s).\n",
10508 super
->current_vol
, st
->devnm
);
10509 goto analyse_change_exit
;
10511 /* check the maximum available size
10513 rv
= imsm_get_free_size(st
, dev
->vol
.map
->num_members
,
10514 0, chunk
, &free_size
);
10516 /* Cannot find maximum available space
10520 max_size
= free_size
+ current_size
;
10521 /* align component size
10523 max_size
= imsm_component_size_aligment_check(
10524 get_imsm_raid_level(dev
->vol
.map
),
10525 chunk
* 1024, super
->sector_size
,
10528 if (geo
->size
== MAX_SIZE
) {
10529 /* requested size change to the maximum available size
10531 if (max_size
== 0) {
10532 pr_err("Error. Cannot find maximum available space.\n");
10534 goto analyse_change_exit
;
10536 geo
->size
= max_size
;
10539 if (direction
== ROLLBACK_METADATA_CHANGES
) {
10540 /* accept size for rollback only
10543 /* round size due to metadata compatibility
10545 geo
->size
= (geo
->size
>> SECT_PER_MB_SHIFT
)
10546 << SECT_PER_MB_SHIFT
;
10547 dprintf("Prepare update for size change to %llu\n",
10549 if (current_size
>= geo
->size
) {
10550 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is %llu).\n",
10551 current_size
, geo
->size
);
10552 goto analyse_change_exit
;
10554 if (max_size
&& geo
->size
> max_size
) {
10555 pr_err("Error. Requested size is larger than maximum available size (maximum available size is %llu, requested size /rounded/ is %llu).\n",
10556 max_size
, geo
->size
);
10557 goto analyse_change_exit
;
10560 geo
->size
*= data_disks
;
10561 geo
->raid_disks
= dev
->vol
.map
->num_members
;
10562 change
= CH_ARRAY_SIZE
;
10564 if (!validate_geometry_imsm(st
,
10567 geo
->raid_disks
+ devNumChange
,
10569 geo
->size
, INVALID_SECTORS
,
10574 struct intel_super
*super
= st
->sb
;
10575 struct imsm_super
*mpb
= super
->anchor
;
10577 if (mpb
->num_raid_devs
> 1) {
10578 pr_err("Error. Cannot perform operation on %s- for this operation it MUST be single array in container\n",
10584 analyse_change_exit
:
10585 if (direction
== ROLLBACK_METADATA_CHANGES
&&
10586 (change
== CH_MIGRATION
|| change
== CH_TAKEOVER
)) {
10587 dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n");
10593 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
10595 struct intel_super
*super
= st
->sb
;
10596 struct imsm_update_takeover
*u
;
10598 u
= xmalloc(sizeof(struct imsm_update_takeover
));
10600 u
->type
= update_takeover
;
10601 u
->subarray
= super
->current_vol
;
10603 /* 10->0 transition */
10604 if (geo
->level
== 0)
10605 u
->direction
= R10_TO_R0
;
10607 /* 0->10 transition */
10608 if (geo
->level
== 10)
10609 u
->direction
= R0_TO_R10
;
10611 /* update metadata locally */
10612 imsm_update_metadata_locally(st
, u
,
10613 sizeof(struct imsm_update_takeover
));
10614 /* and possibly remotely */
10615 if (st
->update_tail
)
10616 append_metadata_update(st
, u
,
10617 sizeof(struct imsm_update_takeover
));
10624 static int imsm_reshape_super(struct supertype
*st
, unsigned long long size
,
10626 int layout
, int chunksize
, int raid_disks
,
10627 int delta_disks
, char *backup
, char *dev
,
10628 int direction
, int verbose
)
10631 struct geo_params geo
;
10633 dprintf("(enter)\n");
10635 memset(&geo
, 0, sizeof(struct geo_params
));
10637 geo
.dev_name
= dev
;
10638 strcpy(geo
.devnm
, st
->devnm
);
10641 geo
.layout
= layout
;
10642 geo
.chunksize
= chunksize
;
10643 geo
.raid_disks
= raid_disks
;
10644 if (delta_disks
!= UnSet
)
10645 geo
.raid_disks
+= delta_disks
;
10647 dprintf("for level : %i\n", geo
.level
);
10648 dprintf("for raid_disks : %i\n", geo
.raid_disks
);
10650 if (experimental() == 0)
10653 if (strcmp(st
->container_devnm
, st
->devnm
) == 0) {
10654 /* On container level we can only increase number of devices. */
10655 dprintf("imsm: info: Container operation\n");
10656 int old_raid_disks
= 0;
10658 if (imsm_reshape_is_allowed_on_container(
10659 st
, &geo
, &old_raid_disks
, direction
)) {
10660 struct imsm_update_reshape
*u
= NULL
;
10663 len
= imsm_create_metadata_update_for_reshape(
10664 st
, &geo
, old_raid_disks
, &u
);
10667 dprintf("imsm: Cannot prepare update\n");
10668 goto exit_imsm_reshape_super
;
10672 /* update metadata locally */
10673 imsm_update_metadata_locally(st
, u
, len
);
10674 /* and possibly remotely */
10675 if (st
->update_tail
)
10676 append_metadata_update(st
, u
, len
);
10681 pr_err("(imsm) Operation is not allowed on this container\n");
10684 /* On volume level we support following operations
10685 * - takeover: raid10 -> raid0; raid0 -> raid10
10686 * - chunk size migration
10687 * - migration: raid5 -> raid0; raid0 -> raid5
10689 struct intel_super
*super
= st
->sb
;
10690 struct intel_dev
*dev
= super
->devlist
;
10692 dprintf("imsm: info: Volume operation\n");
10693 /* find requested device */
10696 imsm_find_array_devnm_by_subdev(
10697 dev
->index
, st
->container_devnm
);
10698 if (devnm
&& strcmp(devnm
, geo
.devnm
) == 0)
10703 pr_err("Cannot find %s (%s) subarray\n",
10704 geo
.dev_name
, geo
.devnm
);
10705 goto exit_imsm_reshape_super
;
10707 super
->current_vol
= dev
->index
;
10708 change
= imsm_analyze_change(st
, &geo
, direction
);
10711 ret_val
= imsm_takeover(st
, &geo
);
10713 case CH_MIGRATION
: {
10714 struct imsm_update_reshape_migration
*u
= NULL
;
10716 imsm_create_metadata_update_for_migration(
10719 dprintf("imsm: Cannot prepare update\n");
10723 /* update metadata locally */
10724 imsm_update_metadata_locally(st
, u
, len
);
10725 /* and possibly remotely */
10726 if (st
->update_tail
)
10727 append_metadata_update(st
, u
, len
);
10732 case CH_ARRAY_SIZE
: {
10733 struct imsm_update_size_change
*u
= NULL
;
10735 imsm_create_metadata_update_for_size_change(
10738 dprintf("imsm: Cannot prepare update\n");
10742 /* update metadata locally */
10743 imsm_update_metadata_locally(st
, u
, len
);
10744 /* and possibly remotely */
10745 if (st
->update_tail
)
10746 append_metadata_update(st
, u
, len
);
10756 exit_imsm_reshape_super
:
10757 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
10761 #define COMPLETED_OK 0
10762 #define COMPLETED_NONE 1
10763 #define COMPLETED_DELAYED 2
10765 static int read_completed(int fd
, unsigned long long *val
)
10770 ret
= sysfs_fd_get_str(fd
, buf
, 50);
10774 ret
= COMPLETED_OK
;
10775 if (strncmp(buf
, "none", 4) == 0) {
10776 ret
= COMPLETED_NONE
;
10777 } else if (strncmp(buf
, "delayed", 7) == 0) {
10778 ret
= COMPLETED_DELAYED
;
10781 *val
= strtoull(buf
, &ep
, 0);
10782 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))
10788 /*******************************************************************************
10789 * Function: wait_for_reshape_imsm
10790 * Description: Function writes new sync_max value and waits until
10791 * reshape process reach new position
10793 * sra : general array info
10794 * ndata : number of disks in new array's layout
10797 * 1 : there is no reshape in progress,
10799 ******************************************************************************/
10800 int wait_for_reshape_imsm(struct mdinfo
*sra
, int ndata
)
10802 int fd
= sysfs_get_fd(sra
, NULL
, "sync_completed");
10804 unsigned long long completed
;
10805 /* to_complete : new sync_max position */
10806 unsigned long long to_complete
= sra
->reshape_progress
;
10807 unsigned long long position_to_set
= to_complete
/ ndata
;
10810 dprintf("cannot open reshape_position\n");
10815 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
10817 dprintf("cannot read reshape_position (no reshape in progres)\n");
10826 if (completed
> position_to_set
) {
10827 dprintf("wrong next position to set %llu (%llu)\n",
10828 to_complete
, position_to_set
);
10832 dprintf("Position set: %llu\n", position_to_set
);
10833 if (sysfs_set_num(sra
, NULL
, "sync_max",
10834 position_to_set
) != 0) {
10835 dprintf("cannot set reshape position to %llu\n",
10844 int timeout
= 3000;
10846 sysfs_wait(fd
, &timeout
);
10847 if (sysfs_get_str(sra
, NULL
, "sync_action",
10849 strncmp(action
, "reshape", 7) != 0) {
10850 if (strncmp(action
, "idle", 4) == 0)
10856 rc
= read_completed(fd
, &completed
);
10858 dprintf("cannot read reshape_position (in loop)\n");
10861 } else if (rc
== COMPLETED_NONE
)
10863 } while (completed
< position_to_set
);
10869 /*******************************************************************************
10870 * Function: check_degradation_change
10871 * Description: Check that array hasn't become failed.
10873 * info : for sysfs access
10874 * sources : source disks descriptors
10875 * degraded: previous degradation level
10877 * degradation level
10878 ******************************************************************************/
10879 int check_degradation_change(struct mdinfo
*info
,
10883 unsigned long long new_degraded
;
10886 rv
= sysfs_get_ll(info
, NULL
, "degraded", &new_degraded
);
10887 if (rv
== -1 || (new_degraded
!= (unsigned long long)degraded
)) {
10888 /* check each device to ensure it is still working */
10891 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
10892 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
10894 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
10897 if (sysfs_get_str(info
,
10898 sd
, "state", sbuf
, sizeof(sbuf
)) < 0 ||
10899 strstr(sbuf
, "faulty") ||
10900 strstr(sbuf
, "in_sync") == NULL
) {
10901 /* this device is dead */
10902 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
10903 if (sd
->disk
.raid_disk
>= 0 &&
10904 sources
[sd
->disk
.raid_disk
] >= 0) {
10906 sd
->disk
.raid_disk
]);
10907 sources
[sd
->disk
.raid_disk
] =
10916 return new_degraded
;
10919 /*******************************************************************************
10920 * Function: imsm_manage_reshape
10921 * Description: Function finds array under reshape and it manages reshape
10922 * process. It creates stripes backups (if required) and sets
10925 * afd : Backup handle (nattive) - not used
10926 * sra : general array info
10927 * reshape : reshape parameters - not used
10928 * st : supertype structure
10929 * blocks : size of critical section [blocks]
10930 * fds : table of source device descriptor
10931 * offsets : start of array (offest per devices)
10933 * destfd : table of destination device descriptor
10934 * destoffsets : table of destination offsets (per device)
10936 * 1 : success, reshape is done
10938 ******************************************************************************/
10939 static int imsm_manage_reshape(
10940 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
10941 struct supertype
*st
, unsigned long backup_blocks
,
10942 int *fds
, unsigned long long *offsets
,
10943 int dests
, int *destfd
, unsigned long long *destoffsets
)
10946 struct intel_super
*super
= st
->sb
;
10947 struct intel_dev
*dv
;
10948 unsigned int sector_size
= super
->sector_size
;
10949 struct imsm_dev
*dev
= NULL
;
10950 struct imsm_map
*map_src
;
10951 int migr_vol_qan
= 0;
10952 int ndata
, odata
; /* [bytes] */
10953 int chunk
; /* [bytes] */
10954 struct migr_record
*migr_rec
;
10956 unsigned int buf_size
; /* [bytes] */
10957 unsigned long long max_position
; /* array size [bytes] */
10958 unsigned long long next_step
; /* [blocks]/[bytes] */
10959 unsigned long long old_data_stripe_length
;
10960 unsigned long long start_src
; /* [bytes] */
10961 unsigned long long start
; /* [bytes] */
10962 unsigned long long start_buf_shift
; /* [bytes] */
10964 int source_layout
= 0;
10969 if (!fds
|| !offsets
)
10972 /* Find volume during the reshape */
10973 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
10974 if (dv
->dev
->vol
.migr_type
== MIGR_GEN_MIGR
10975 && dv
->dev
->vol
.migr_state
== 1) {
10980 /* Only one volume can migrate at the same time */
10981 if (migr_vol_qan
!= 1) {
10982 pr_err("%s", migr_vol_qan
?
10983 "Number of migrating volumes greater than 1\n" :
10984 "There is no volume during migrationg\n");
10988 map_src
= get_imsm_map(dev
, MAP_1
);
10989 if (map_src
== NULL
)
10992 ndata
= imsm_num_data_members(dev
, MAP_0
);
10993 odata
= imsm_num_data_members(dev
, MAP_1
);
10995 chunk
= __le16_to_cpu(map_src
->blocks_per_strip
) * 512;
10996 old_data_stripe_length
= odata
* chunk
;
10998 migr_rec
= super
->migr_rec
;
11000 /* initialize migration record for start condition */
11001 if (sra
->reshape_progress
== 0)
11002 init_migr_record_imsm(st
, dev
, sra
);
11004 if (__le32_to_cpu(migr_rec
->rec_status
) != UNIT_SRC_NORMAL
) {
11005 dprintf("imsm: cannot restart migration when data are present in copy area.\n");
11008 /* Save checkpoint to update migration record for current
11009 * reshape position (in md). It can be farther than current
11010 * reshape position in metadata.
11012 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
11013 /* ignore error == 2, this can mean end of reshape here
11015 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n");
11020 /* size for data */
11021 buf_size
= __le32_to_cpu(migr_rec
->blocks_per_unit
) * 512;
11022 /* extend buffer size for parity disk */
11023 buf_size
+= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
11024 /* add space for stripe aligment */
11025 buf_size
+= old_data_stripe_length
;
11026 if (posix_memalign((void **)&buf
, MAX_SECTOR_SIZE
, buf_size
)) {
11027 dprintf("imsm: Cannot allocate checkpoint buffer\n");
11031 max_position
= sra
->component_size
* ndata
;
11032 source_layout
= imsm_level_to_layout(map_src
->raid_level
);
11034 while (__le32_to_cpu(migr_rec
->curr_migr_unit
) <
11035 __le32_to_cpu(migr_rec
->num_migr_units
)) {
11036 /* current reshape position [blocks] */
11037 unsigned long long current_position
=
11038 __le32_to_cpu(migr_rec
->blocks_per_unit
)
11039 * __le32_to_cpu(migr_rec
->curr_migr_unit
);
11040 unsigned long long border
;
11042 /* Check that array hasn't become failed.
11044 degraded
= check_degradation_change(sra
, fds
, degraded
);
11045 if (degraded
> 1) {
11046 dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded
);
11050 next_step
= __le32_to_cpu(migr_rec
->blocks_per_unit
);
11052 if ((current_position
+ next_step
) > max_position
)
11053 next_step
= max_position
- current_position
;
11055 start
= current_position
* 512;
11057 /* align reading start to old geometry */
11058 start_buf_shift
= start
% old_data_stripe_length
;
11059 start_src
= start
- start_buf_shift
;
11061 border
= (start_src
/ odata
) - (start
/ ndata
);
11063 if (border
<= __le32_to_cpu(migr_rec
->dest_depth_per_unit
)) {
11064 /* save critical stripes to buf
11065 * start - start address of current unit
11066 * to backup [bytes]
11067 * start_src - start address of current unit
11068 * to backup alligned to source array
11071 unsigned long long next_step_filler
;
11072 unsigned long long copy_length
= next_step
* 512;
11074 /* allign copy area length to stripe in old geometry */
11075 next_step_filler
= ((copy_length
+ start_buf_shift
)
11076 % old_data_stripe_length
);
11077 if (next_step_filler
)
11078 next_step_filler
= (old_data_stripe_length
11079 - next_step_filler
);
11080 dprintf("save_stripes() parameters: start = %llu,\tstart_src = %llu,\tnext_step*512 = %llu,\tstart_in_buf_shift = %llu,\tnext_step_filler = %llu\n",
11081 start
, start_src
, copy_length
,
11082 start_buf_shift
, next_step_filler
);
11084 if (save_stripes(fds
, offsets
, map_src
->num_members
,
11085 chunk
, map_src
->raid_level
,
11086 source_layout
, 0, NULL
, start_src
,
11088 next_step_filler
+ start_buf_shift
,
11090 dprintf("imsm: Cannot save stripes to buffer\n");
11093 /* Convert data to destination format and store it
11094 * in backup general migration area
11096 if (save_backup_imsm(st
, dev
, sra
,
11097 buf
+ start_buf_shift
, copy_length
)) {
11098 dprintf("imsm: Cannot save stripes to target devices\n");
11101 if (save_checkpoint_imsm(st
, sra
,
11102 UNIT_SRC_IN_CP_AREA
)) {
11103 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n");
11107 /* set next step to use whole border area */
11108 border
/= next_step
;
11110 next_step
*= border
;
11112 /* When data backed up, checkpoint stored,
11113 * kick the kernel to reshape unit of data
11115 next_step
= next_step
+ sra
->reshape_progress
;
11116 /* limit next step to array max position */
11117 if (next_step
> max_position
)
11118 next_step
= max_position
;
11119 sysfs_set_num(sra
, NULL
, "suspend_lo", sra
->reshape_progress
);
11120 sysfs_set_num(sra
, NULL
, "suspend_hi", next_step
);
11121 sra
->reshape_progress
= next_step
;
11123 /* wait until reshape finish */
11124 if (wait_for_reshape_imsm(sra
, ndata
)) {
11125 dprintf("wait_for_reshape_imsm returned error!\n");
11131 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
11132 /* ignore error == 2, this can mean end of reshape here
11134 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n");
11140 /* clear migr_rec on disks after successful migration */
11143 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SECTORS
*sector_size
);
11144 for (d
= super
->disks
; d
; d
= d
->next
) {
11145 if (d
->index
< 0 || is_failed(&d
->disk
))
11147 unsigned long long dsize
;
11149 get_dev_size(d
->fd
, NULL
, &dsize
);
11150 if (lseek64(d
->fd
, dsize
- MIGR_REC_SECTOR_POSITION
*sector_size
,
11152 if (write(d
->fd
, super
->migr_rec_buf
,
11153 MIGR_REC_BUF_SECTORS
*sector_size
) !=
11154 MIGR_REC_BUF_SECTORS
*sector_size
)
11155 perror("Write migr_rec failed");
11159 /* return '1' if done */
11163 /* See Grow.c: abort_reshape() for further explanation */
11164 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
11165 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
11166 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
11171 #endif /* MDASSEMBLE */
11173 struct superswitch super_imsm
= {
11175 .examine_super
= examine_super_imsm
,
11176 .brief_examine_super
= brief_examine_super_imsm
,
11177 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
11178 .export_examine_super
= export_examine_super_imsm
,
11179 .detail_super
= detail_super_imsm
,
11180 .brief_detail_super
= brief_detail_super_imsm
,
11181 .write_init_super
= write_init_super_imsm
,
11182 .validate_geometry
= validate_geometry_imsm
,
11183 .add_to_super
= add_to_super_imsm
,
11184 .remove_from_super
= remove_from_super_imsm
,
11185 .detail_platform
= detail_platform_imsm
,
11186 .export_detail_platform
= export_detail_platform_imsm
,
11187 .kill_subarray
= kill_subarray_imsm
,
11188 .update_subarray
= update_subarray_imsm
,
11189 .load_container
= load_container_imsm
,
11190 .default_geometry
= default_geometry_imsm
,
11191 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
11192 .reshape_super
= imsm_reshape_super
,
11193 .manage_reshape
= imsm_manage_reshape
,
11194 .recover_backup
= recover_backup_imsm
,
11195 .copy_metadata
= copy_metadata_imsm
,
11197 .match_home
= match_home_imsm
,
11198 .uuid_from_super
= uuid_from_super_imsm
,
11199 .getinfo_super
= getinfo_super_imsm
,
11200 .getinfo_super_disks
= getinfo_super_disks_imsm
,
11201 .update_super
= update_super_imsm
,
11203 .avail_size
= avail_size_imsm
,
11204 .min_acceptable_spare_size
= min_acceptable_spare_size_imsm
,
11206 .compare_super
= compare_super_imsm
,
11208 .load_super
= load_super_imsm
,
11209 .init_super
= init_super_imsm
,
11210 .store_super
= store_super_imsm
,
11211 .free_super
= free_super_imsm
,
11212 .match_metadata_desc
= match_metadata_desc_imsm
,
11213 .container_content
= container_content_imsm
,
11214 .validate_container
= validate_container_imsm
,
11221 .open_new
= imsm_open_new
,
11222 .set_array_state
= imsm_set_array_state
,
11223 .set_disk
= imsm_set_disk
,
11224 .sync_metadata
= imsm_sync_metadata
,
11225 .activate_spare
= imsm_activate_spare
,
11226 .process_update
= imsm_process_update
,
11227 .prepare_update
= imsm_prepare_update
,
11228 #endif /* MDASSEMBLE */