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 */
396 struct md_bb bb
; /* memory for get_bad_blocks call */
400 struct imsm_disk disk
;
401 #define IMSM_UNKNOWN_OWNER (-1)
403 struct intel_disk
*next
;
407 unsigned long long start
, size
;
410 /* definitions of reshape process types */
411 enum imsm_reshape_type
{
417 /* definition of messages passed to imsm_process_update */
418 enum imsm_update_type
{
419 update_activate_spare
,
423 update_add_remove_disk
,
424 update_reshape_container_disks
,
425 update_reshape_migration
,
427 update_general_migration_checkpoint
,
429 update_prealloc_badblocks_mem
,
432 struct imsm_update_activate_spare
{
433 enum imsm_update_type type
;
437 struct imsm_update_activate_spare
*next
;
443 unsigned long long size
;
450 enum takeover_direction
{
454 struct imsm_update_takeover
{
455 enum imsm_update_type type
;
457 enum takeover_direction direction
;
460 struct imsm_update_reshape
{
461 enum imsm_update_type type
;
465 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
468 struct imsm_update_reshape_migration
{
469 enum imsm_update_type type
;
472 /* fields for array migration changes
479 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
482 struct imsm_update_size_change
{
483 enum imsm_update_type type
;
488 struct imsm_update_general_migration_checkpoint
{
489 enum imsm_update_type type
;
490 __u32 curr_migr_unit
;
494 __u8 serial
[MAX_RAID_SERIAL_LEN
];
497 struct imsm_update_create_array
{
498 enum imsm_update_type type
;
503 struct imsm_update_kill_array
{
504 enum imsm_update_type type
;
508 struct imsm_update_rename_array
{
509 enum imsm_update_type type
;
510 __u8 name
[MAX_RAID_SERIAL_LEN
];
514 struct imsm_update_add_remove_disk
{
515 enum imsm_update_type type
;
518 struct imsm_update_prealloc_bb_mem
{
519 enum imsm_update_type type
;
522 static const char *_sys_dev_type
[] = {
523 [SYS_DEV_UNKNOWN
] = "Unknown",
524 [SYS_DEV_SAS
] = "SAS",
525 [SYS_DEV_SATA
] = "SATA",
526 [SYS_DEV_NVME
] = "NVMe",
527 [SYS_DEV_VMD
] = "VMD"
530 const char *get_sys_dev_type(enum sys_dev_type type
)
532 if (type
>= SYS_DEV_MAX
)
533 type
= SYS_DEV_UNKNOWN
;
535 return _sys_dev_type
[type
];
538 static struct intel_hba
* alloc_intel_hba(struct sys_dev
*device
)
540 struct intel_hba
*result
= xmalloc(sizeof(*result
));
542 result
->type
= device
->type
;
543 result
->path
= xstrdup(device
->path
);
545 if (result
->path
&& (result
->pci_id
= strrchr(result
->path
, '/')) != NULL
)
551 static struct intel_hba
* find_intel_hba(struct intel_hba
*hba
, struct sys_dev
*device
)
553 struct intel_hba
*result
;
555 for (result
= hba
; result
; result
= result
->next
) {
556 if (result
->type
== device
->type
&& strcmp(result
->path
, device
->path
) == 0)
562 static int attach_hba_to_super(struct intel_super
*super
, struct sys_dev
*device
)
564 struct intel_hba
*hba
;
566 /* check if disk attached to Intel HBA */
567 hba
= find_intel_hba(super
->hba
, device
);
570 /* Check if HBA is already attached to super */
571 if (super
->hba
== NULL
) {
572 super
->hba
= alloc_intel_hba(device
);
577 /* Intel metadata allows for all disks attached to the same type HBA.
578 * Do not support HBA types mixing
580 if (device
->type
!= hba
->type
)
583 /* Multiple same type HBAs can be used if they share the same OROM */
584 const struct imsm_orom
*device_orom
= get_orom_by_device_id(device
->dev_id
);
586 if (device_orom
!= super
->orom
)
592 hba
->next
= alloc_intel_hba(device
);
596 static struct sys_dev
* find_disk_attached_hba(int fd
, const char *devname
)
598 struct sys_dev
*list
, *elem
;
601 if ((list
= find_intel_devices()) == NULL
)
605 disk_path
= (char *) devname
;
607 disk_path
= diskfd_to_devpath(fd
);
612 for (elem
= list
; elem
; elem
= elem
->next
)
613 if (path_attached_to_hba(disk_path
, elem
->path
))
616 if (disk_path
!= devname
)
622 static int find_intel_hba_capability(int fd
, struct intel_super
*super
,
625 static struct supertype
*match_metadata_desc_imsm(char *arg
)
627 struct supertype
*st
;
629 if (strcmp(arg
, "imsm") != 0 &&
630 strcmp(arg
, "default") != 0
634 st
= xcalloc(1, sizeof(*st
));
635 st
->ss
= &super_imsm
;
636 st
->max_devs
= IMSM_MAX_DEVICES
;
637 st
->minor_version
= 0;
643 static __u8
*get_imsm_version(struct imsm_super
*mpb
)
645 return &mpb
->sig
[MPB_SIG_LEN
];
649 /* retrieve a disk directly from the anchor when the anchor is known to be
650 * up-to-date, currently only at load time
652 static struct imsm_disk
*__get_imsm_disk(struct imsm_super
*mpb
, __u8 index
)
654 if (index
>= mpb
->num_disks
)
656 return &mpb
->disk
[index
];
659 /* retrieve the disk description based on a index of the disk
662 static struct dl
*get_imsm_dl_disk(struct intel_super
*super
, __u8 index
)
666 for (d
= super
->disks
; d
; d
= d
->next
)
667 if (d
->index
== index
)
672 /* retrieve a disk from the parsed metadata */
673 static struct imsm_disk
*get_imsm_disk(struct intel_super
*super
, __u8 index
)
677 dl
= get_imsm_dl_disk(super
, index
);
684 /* generate a checksum directly from the anchor when the anchor is known to be
685 * up-to-date, currently only at load or write_super after coalescing
687 static __u32
__gen_imsm_checksum(struct imsm_super
*mpb
)
689 __u32 end
= mpb
->mpb_size
/ sizeof(end
);
690 __u32
*p
= (__u32
*) mpb
;
694 sum
+= __le32_to_cpu(*p
);
698 return sum
- __le32_to_cpu(mpb
->check_sum
);
701 static size_t sizeof_imsm_map(struct imsm_map
*map
)
703 return sizeof(struct imsm_map
) + sizeof(__u32
) * (map
->num_members
- 1);
706 struct imsm_map
*get_imsm_map(struct imsm_dev
*dev
, int second_map
)
708 /* A device can have 2 maps if it is in the middle of a migration.
710 * MAP_0 - we return the first map
711 * MAP_1 - we return the second map if it exists, else NULL
712 * MAP_X - we return the second map if it exists, else the first
714 struct imsm_map
*map
= &dev
->vol
.map
[0];
715 struct imsm_map
*map2
= NULL
;
717 if (dev
->vol
.migr_state
)
718 map2
= (void *)map
+ sizeof_imsm_map(map
);
720 switch (second_map
) {
737 /* return the size of the device.
738 * migr_state increases the returned size if map[0] were to be duplicated
740 static size_t sizeof_imsm_dev(struct imsm_dev
*dev
, int migr_state
)
742 size_t size
= sizeof(*dev
) - sizeof(struct imsm_map
) +
743 sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
745 /* migrating means an additional map */
746 if (dev
->vol
.migr_state
)
747 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_1
));
749 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
755 /* retrieve disk serial number list from a metadata update */
756 static struct disk_info
*get_disk_info(struct imsm_update_create_array
*update
)
759 struct disk_info
*inf
;
761 inf
= u
+ sizeof(*update
) - sizeof(struct imsm_dev
) +
762 sizeof_imsm_dev(&update
->dev
, 0);
768 static struct imsm_dev
*__get_imsm_dev(struct imsm_super
*mpb
, __u8 index
)
774 if (index
>= mpb
->num_raid_devs
)
777 /* devices start after all disks */
778 offset
= ((void *) &mpb
->disk
[mpb
->num_disks
]) - _mpb
;
780 for (i
= 0; i
<= index
; i
++)
782 return _mpb
+ offset
;
784 offset
+= sizeof_imsm_dev(_mpb
+ offset
, 0);
789 static struct imsm_dev
*get_imsm_dev(struct intel_super
*super
, __u8 index
)
791 struct intel_dev
*dv
;
793 if (index
>= super
->anchor
->num_raid_devs
)
795 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
796 if (dv
->index
== index
)
801 static inline unsigned long long __le48_to_cpu(const struct bbm_log_block_addr
804 return ((((__u64
)__le32_to_cpu(addr
->dw1
)) << 16) |
805 __le16_to_cpu(addr
->w1
));
808 static inline struct bbm_log_block_addr
__cpu_to_le48(unsigned long long sec
)
810 struct bbm_log_block_addr addr
;
812 addr
.w1
= __cpu_to_le16((__u16
)(sec
& 0xffff));
813 addr
.dw1
= __cpu_to_le32((__u32
)(sec
>> 16) & 0xffffffff);
818 /* get size of the bbm log */
819 static __u32
get_imsm_bbm_log_size(struct bbm_log
*log
)
821 if (!log
|| log
->entry_count
== 0)
824 return sizeof(log
->signature
) +
825 sizeof(log
->entry_count
) +
826 log
->entry_count
* sizeof(struct bbm_log_entry
);
829 /* check if bad block is not partially stored in bbm log */
830 static int is_stored_in_bbm(struct bbm_log
*log
, const __u8 idx
, const unsigned
831 long long sector
, const int length
, __u32
*pos
)
835 for (i
= *pos
; i
< log
->entry_count
; i
++) {
836 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[i
];
837 unsigned long long bb_start
;
838 unsigned long long bb_end
;
840 bb_start
= __le48_to_cpu(&entry
->defective_block_start
);
841 bb_end
= bb_start
+ (entry
->marked_count
+ 1);
843 if ((entry
->disk_ordinal
== idx
) && (bb_start
>= sector
) &&
844 (bb_end
<= sector
+ length
)) {
852 /* record new bad block in bbm log */
853 static int record_new_badblock(struct bbm_log
*log
, const __u8 idx
, unsigned
854 long long sector
, int length
)
858 struct bbm_log_entry
*entry
= NULL
;
860 while (is_stored_in_bbm(log
, idx
, sector
, length
, &pos
)) {
861 struct bbm_log_entry
*e
= &log
->marked_block_entries
[pos
];
863 if ((e
->marked_count
+ 1 == BBM_LOG_MAX_LBA_ENTRY_VAL
) &&
864 (__le48_to_cpu(&e
->defective_block_start
) == sector
)) {
865 sector
+= BBM_LOG_MAX_LBA_ENTRY_VAL
;
866 length
-= BBM_LOG_MAX_LBA_ENTRY_VAL
;
875 int cnt
= (length
<= BBM_LOG_MAX_LBA_ENTRY_VAL
) ? length
:
876 BBM_LOG_MAX_LBA_ENTRY_VAL
;
877 entry
->defective_block_start
= __cpu_to_le48(sector
);
878 entry
->marked_count
= cnt
- 1;
885 new_bb
= ROUND_UP(length
, BBM_LOG_MAX_LBA_ENTRY_VAL
) /
886 BBM_LOG_MAX_LBA_ENTRY_VAL
;
887 if (log
->entry_count
+ new_bb
> BBM_LOG_MAX_ENTRIES
)
891 int cnt
= (length
<= BBM_LOG_MAX_LBA_ENTRY_VAL
) ? length
:
892 BBM_LOG_MAX_LBA_ENTRY_VAL
;
893 struct bbm_log_entry
*entry
=
894 &log
->marked_block_entries
[log
->entry_count
];
896 entry
->defective_block_start
= __cpu_to_le48(sector
);
897 entry
->marked_count
= cnt
- 1;
898 entry
->disk_ordinal
= idx
;
909 /* clear all bad blocks for given disk */
910 static void clear_disk_badblocks(struct bbm_log
*log
, const __u8 idx
)
914 while (i
< log
->entry_count
) {
915 struct bbm_log_entry
*entries
= log
->marked_block_entries
;
917 if (entries
[i
].disk_ordinal
== idx
) {
918 if (i
< log
->entry_count
- 1)
919 entries
[i
] = entries
[log
->entry_count
- 1];
927 /* clear given bad block */
928 static int clear_badblock(struct bbm_log
*log
, const __u8 idx
, const unsigned
929 long long sector
, const int length
) {
932 while (i
< log
->entry_count
) {
933 struct bbm_log_entry
*entries
= log
->marked_block_entries
;
935 if ((entries
[i
].disk_ordinal
== idx
) &&
936 (__le48_to_cpu(&entries
[i
].defective_block_start
) ==
937 sector
) && (entries
[i
].marked_count
+ 1 == length
)) {
938 if (i
< log
->entry_count
- 1)
939 entries
[i
] = entries
[log
->entry_count
- 1];
948 #endif /* MDASSEMBLE */
950 /* allocate and load BBM log from metadata */
951 static int load_bbm_log(struct intel_super
*super
)
953 struct imsm_super
*mpb
= super
->anchor
;
954 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
956 super
->bbm_log
= xcalloc(1, sizeof(struct bbm_log
));
961 struct bbm_log
*log
= (void *)mpb
+
962 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
966 if (bbm_log_size
< sizeof(log
->signature
) +
967 sizeof(log
->entry_count
))
970 entry_count
= __le32_to_cpu(log
->entry_count
);
971 if ((__le32_to_cpu(log
->signature
) != BBM_LOG_SIGNATURE
) ||
972 (entry_count
> BBM_LOG_MAX_ENTRIES
))
976 sizeof(log
->signature
) + sizeof(log
->entry_count
) +
977 entry_count
* sizeof(struct bbm_log_entry
))
980 memcpy(super
->bbm_log
, log
, bbm_log_size
);
982 super
->bbm_log
->signature
= __cpu_to_le32(BBM_LOG_SIGNATURE
);
983 super
->bbm_log
->entry_count
= 0;
989 /* checks if bad block is within volume boundaries */
990 static int is_bad_block_in_volume(const struct bbm_log_entry
*entry
,
991 const unsigned long long start_sector
,
992 const unsigned long long size
)
994 unsigned long long bb_start
;
995 unsigned long long bb_end
;
997 bb_start
= __le48_to_cpu(&entry
->defective_block_start
);
998 bb_end
= bb_start
+ (entry
->marked_count
+ 1);
1000 if (((bb_start
>= start_sector
) && (bb_start
< start_sector
+ size
)) ||
1001 ((bb_end
>= start_sector
) && (bb_end
<= start_sector
+ size
)))
1007 /* get list of bad blocks on a drive for a volume */
1008 static void get_volume_badblocks(const struct bbm_log
*log
, const __u8 idx
,
1009 const unsigned long long start_sector
,
1010 const unsigned long long size
,
1016 for (i
= 0; i
< log
->entry_count
; i
++) {
1017 const struct bbm_log_entry
*ent
=
1018 &log
->marked_block_entries
[i
];
1019 struct md_bb_entry
*bb
;
1021 if ((ent
->disk_ordinal
== idx
) &&
1022 is_bad_block_in_volume(ent
, start_sector
, size
)) {
1024 if (!bbs
->entries
) {
1025 bbs
->entries
= xmalloc(BBM_LOG_MAX_ENTRIES
*
1031 bb
= &bbs
->entries
[count
++];
1032 bb
->sector
= __le48_to_cpu(&ent
->defective_block_start
);
1033 bb
->length
= ent
->marked_count
+ 1;
1041 * == MAP_0 get first map
1042 * == MAP_1 get second map
1043 * == MAP_X than get map according to the current migr_state
1045 static __u32
get_imsm_ord_tbl_ent(struct imsm_dev
*dev
,
1049 struct imsm_map
*map
;
1051 map
= get_imsm_map(dev
, second_map
);
1053 /* top byte identifies disk under rebuild */
1054 return __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
1057 #define ord_to_idx(ord) (((ord) << 8) >> 8)
1058 static __u32
get_imsm_disk_idx(struct imsm_dev
*dev
, int slot
, int second_map
)
1060 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, second_map
);
1062 return ord_to_idx(ord
);
1065 static void set_imsm_ord_tbl_ent(struct imsm_map
*map
, int slot
, __u32 ord
)
1067 map
->disk_ord_tbl
[slot
] = __cpu_to_le32(ord
);
1070 static int get_imsm_disk_slot(struct imsm_map
*map
, unsigned idx
)
1075 for (slot
= 0; slot
< map
->num_members
; slot
++) {
1076 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
1077 if (ord_to_idx(ord
) == idx
)
1084 static int get_imsm_raid_level(struct imsm_map
*map
)
1086 if (map
->raid_level
== 1) {
1087 if (map
->num_members
== 2)
1093 return map
->raid_level
;
1096 static int cmp_extent(const void *av
, const void *bv
)
1098 const struct extent
*a
= av
;
1099 const struct extent
*b
= bv
;
1100 if (a
->start
< b
->start
)
1102 if (a
->start
> b
->start
)
1107 static int count_memberships(struct dl
*dl
, struct intel_super
*super
)
1109 int memberships
= 0;
1112 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1113 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1114 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1116 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
1123 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
);
1125 static int split_ull(unsigned long long n
, __u32
*lo
, __u32
*hi
)
1127 if (lo
== 0 || hi
== 0)
1129 *lo
= __le32_to_cpu((unsigned)n
);
1130 *hi
= __le32_to_cpu((unsigned)(n
>> 32));
1134 static unsigned long long join_u32(__u32 lo
, __u32 hi
)
1136 return (unsigned long long)__le32_to_cpu(lo
) |
1137 (((unsigned long long)__le32_to_cpu(hi
)) << 32);
1140 static unsigned long long total_blocks(struct imsm_disk
*disk
)
1144 return join_u32(disk
->total_blocks_lo
, disk
->total_blocks_hi
);
1147 static unsigned long long pba_of_lba0(struct imsm_map
*map
)
1151 return join_u32(map
->pba_of_lba0_lo
, map
->pba_of_lba0_hi
);
1154 static unsigned long long blocks_per_member(struct imsm_map
*map
)
1158 return join_u32(map
->blocks_per_member_lo
, map
->blocks_per_member_hi
);
1161 static unsigned long long num_data_stripes(struct imsm_map
*map
)
1165 return join_u32(map
->num_data_stripes_lo
, map
->num_data_stripes_hi
);
1168 static void set_total_blocks(struct imsm_disk
*disk
, unsigned long long n
)
1170 split_ull(n
, &disk
->total_blocks_lo
, &disk
->total_blocks_hi
);
1173 static void set_pba_of_lba0(struct imsm_map
*map
, unsigned long long n
)
1175 split_ull(n
, &map
->pba_of_lba0_lo
, &map
->pba_of_lba0_hi
);
1178 static void set_blocks_per_member(struct imsm_map
*map
, unsigned long long n
)
1180 split_ull(n
, &map
->blocks_per_member_lo
, &map
->blocks_per_member_hi
);
1183 static void set_num_data_stripes(struct imsm_map
*map
, unsigned long long n
)
1185 split_ull(n
, &map
->num_data_stripes_lo
, &map
->num_data_stripes_hi
);
1188 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
)
1190 /* find a list of used extents on the given physical device */
1191 struct extent
*rv
, *e
;
1193 int memberships
= count_memberships(dl
, super
);
1196 /* trim the reserved area for spares, so they can join any array
1197 * regardless of whether the OROM has assigned sectors from the
1198 * IMSM_RESERVED_SECTORS region
1200 if (dl
->index
== -1)
1201 reservation
= imsm_min_reserved_sectors(super
);
1203 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1205 rv
= xcalloc(sizeof(struct extent
), (memberships
+ 1));
1208 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1209 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1210 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1212 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
1213 e
->start
= pba_of_lba0(map
);
1214 e
->size
= blocks_per_member(map
);
1218 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
1220 /* determine the start of the metadata
1221 * when no raid devices are defined use the default
1222 * ...otherwise allow the metadata to truncate the value
1223 * as is the case with older versions of imsm
1226 struct extent
*last
= &rv
[memberships
- 1];
1227 unsigned long long remainder
;
1229 remainder
= total_blocks(&dl
->disk
) - (last
->start
+ last
->size
);
1230 /* round down to 1k block to satisfy precision of the kernel
1234 /* make sure remainder is still sane */
1235 if (remainder
< (unsigned)ROUND_UP(super
->len
, 512) >> 9)
1236 remainder
= ROUND_UP(super
->len
, 512) >> 9;
1237 if (reservation
> remainder
)
1238 reservation
= remainder
;
1240 e
->start
= total_blocks(&dl
->disk
) - reservation
;
1245 /* try to determine how much space is reserved for metadata from
1246 * the last get_extents() entry, otherwise fallback to the
1249 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
1255 /* for spares just return a minimal reservation which will grow
1256 * once the spare is picked up by an array
1258 if (dl
->index
== -1)
1259 return MPB_SECTOR_CNT
;
1261 e
= get_extents(super
, dl
);
1263 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1265 /* scroll to last entry */
1266 for (i
= 0; e
[i
].size
; i
++)
1269 rv
= total_blocks(&dl
->disk
) - e
[i
].start
;
1276 static int is_spare(struct imsm_disk
*disk
)
1278 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
1281 static int is_configured(struct imsm_disk
*disk
)
1283 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
1286 static int is_failed(struct imsm_disk
*disk
)
1288 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
1291 /* try to determine how much space is reserved for metadata from
1292 * the last get_extents() entry on the smallest active disk,
1293 * otherwise fallback to the default
1295 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
)
1299 unsigned long long min_active
;
1301 __u32 rv
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1302 struct dl
*dl
, *dl_min
= NULL
;
1308 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
1311 unsigned long long blocks
= total_blocks(&dl
->disk
);
1312 if (blocks
< min_active
|| min_active
== 0) {
1314 min_active
= blocks
;
1320 /* find last lba used by subarrays on the smallest active disk */
1321 e
= get_extents(super
, dl_min
);
1324 for (i
= 0; e
[i
].size
; i
++)
1327 remainder
= min_active
- e
[i
].start
;
1330 /* to give priority to recovery we should not require full
1331 IMSM_RESERVED_SECTORS from the spare */
1332 rv
= MPB_SECTOR_CNT
+ NUM_BLOCKS_DIRTY_STRIPE_REGION
;
1334 /* if real reservation is smaller use that value */
1335 return (remainder
< rv
) ? remainder
: rv
;
1338 /* Return minimum size of a spare that can be used in this array*/
1339 static unsigned long long min_acceptable_spare_size_imsm(struct supertype
*st
)
1341 struct intel_super
*super
= st
->sb
;
1345 unsigned long long rv
= 0;
1349 /* find first active disk in array */
1351 while (dl
&& (is_failed(&dl
->disk
) || dl
->index
== -1))
1355 /* find last lba used by subarrays */
1356 e
= get_extents(super
, dl
);
1359 for (i
= 0; e
[i
].size
; i
++)
1362 rv
= e
[i
-1].start
+ e
[i
-1].size
;
1365 /* add the amount of space needed for metadata */
1366 rv
= rv
+ imsm_min_reserved_sectors(super
);
1371 static int is_gen_migration(struct imsm_dev
*dev
);
1373 #define IMSM_4K_DIV 8
1376 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
1377 struct imsm_dev
*dev
);
1379 static void print_imsm_dev(struct intel_super
*super
,
1380 struct imsm_dev
*dev
,
1386 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1387 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
1391 printf("[%.16s]:\n", dev
->volume
);
1392 printf(" UUID : %s\n", uuid
);
1393 printf(" RAID Level : %d", get_imsm_raid_level(map
));
1395 printf(" <-- %d", get_imsm_raid_level(map2
));
1397 printf(" Members : %d", map
->num_members
);
1399 printf(" <-- %d", map2
->num_members
);
1401 printf(" Slots : [");
1402 for (i
= 0; i
< map
->num_members
; i
++) {
1403 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
1404 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1409 for (i
= 0; i
< map2
->num_members
; i
++) {
1410 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_1
);
1411 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1416 printf(" Failed disk : ");
1417 if (map
->failed_disk_num
== 0xff)
1420 printf("%i", map
->failed_disk_num
);
1422 slot
= get_imsm_disk_slot(map
, disk_idx
);
1424 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
1425 printf(" This Slot : %d%s\n", slot
,
1426 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
1428 printf(" This Slot : ?\n");
1429 sz
= __le32_to_cpu(dev
->size_high
);
1431 sz
+= __le32_to_cpu(dev
->size_low
);
1432 printf(" Array Size : %llu%s\n", (unsigned long long)sz
,
1433 human_size(sz
* 512));
1434 sz
= blocks_per_member(map
);
1435 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz
,
1436 human_size(sz
* 512));
1437 printf(" Sector Offset : %llu\n",
1439 printf(" Num Stripes : %llu\n",
1440 num_data_stripes(map
));
1441 printf(" Chunk Size : %u KiB",
1442 __le16_to_cpu(map
->blocks_per_strip
) / 2);
1444 printf(" <-- %u KiB",
1445 __le16_to_cpu(map2
->blocks_per_strip
) / 2);
1447 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
1448 printf(" Migrate State : ");
1449 if (dev
->vol
.migr_state
) {
1450 if (migr_type(dev
) == MIGR_INIT
)
1451 printf("initialize\n");
1452 else if (migr_type(dev
) == MIGR_REBUILD
)
1453 printf("rebuild\n");
1454 else if (migr_type(dev
) == MIGR_VERIFY
)
1456 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
1457 printf("general migration\n");
1458 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
1459 printf("state change\n");
1460 else if (migr_type(dev
) == MIGR_REPAIR
)
1463 printf("<unknown:%d>\n", migr_type(dev
));
1466 printf(" Map State : %s", map_state_str
[map
->map_state
]);
1467 if (dev
->vol
.migr_state
) {
1468 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1470 printf(" <-- %s", map_state_str
[map
->map_state
]);
1471 printf("\n Checkpoint : %u ",
1472 __le32_to_cpu(dev
->vol
.curr_migr_unit
));
1473 if (is_gen_migration(dev
) && (slot
> 1 || slot
< 0))
1476 printf("(%llu)", (unsigned long long)
1477 blocks_per_migr_unit(super
, dev
));
1480 printf(" Dirty State : %s\n", dev
->vol
.dirty
? "dirty" : "clean");
1483 static void print_imsm_disk(struct imsm_disk
*disk
, int index
, __u32 reserved
)
1485 char str
[MAX_RAID_SERIAL_LEN
+ 1];
1488 if (index
< -1 || !disk
)
1492 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
1494 printf(" Disk%02d Serial : %s\n", index
, str
);
1496 printf(" Disk Serial : %s\n", str
);
1497 printf(" State :%s%s%s\n", is_spare(disk
) ? " spare" : "",
1498 is_configured(disk
) ? " active" : "",
1499 is_failed(disk
) ? " failed" : "");
1500 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
1501 sz
= total_blocks(disk
) - reserved
;
1502 printf(" Usable Size : %llu%s\n", (unsigned long long)sz
,
1503 human_size(sz
* 512));
1506 void convert_to_4k_imsm_migr_rec(struct intel_super
*super
)
1508 struct migr_record
*migr_rec
= super
->migr_rec
;
1510 migr_rec
->blocks_per_unit
/= IMSM_4K_DIV
;
1511 migr_rec
->ckpt_area_pba
/= IMSM_4K_DIV
;
1512 migr_rec
->dest_1st_member_lba
/= IMSM_4K_DIV
;
1513 migr_rec
->dest_depth_per_unit
/= IMSM_4K_DIV
;
1514 split_ull((join_u32(migr_rec
->post_migr_vol_cap
,
1515 migr_rec
->post_migr_vol_cap_hi
) / IMSM_4K_DIV
),
1516 &migr_rec
->post_migr_vol_cap
, &migr_rec
->post_migr_vol_cap_hi
);
1519 void convert_to_4k_imsm_disk(struct imsm_disk
*disk
)
1521 set_total_blocks(disk
, (total_blocks(disk
)/IMSM_4K_DIV
));
1524 void convert_to_4k(struct intel_super
*super
)
1526 struct imsm_super
*mpb
= super
->anchor
;
1527 struct imsm_disk
*disk
;
1529 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
1531 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1532 disk
= __get_imsm_disk(mpb
, i
);
1534 convert_to_4k_imsm_disk(disk
);
1536 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1537 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1538 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1540 split_ull((join_u32(dev
->size_low
, dev
->size_high
)/IMSM_4K_DIV
),
1541 &dev
->size_low
, &dev
->size_high
);
1542 dev
->vol
.curr_migr_unit
/= IMSM_4K_DIV
;
1545 set_blocks_per_member(map
, blocks_per_member(map
)/IMSM_4K_DIV
);
1546 map
->blocks_per_strip
/= IMSM_4K_DIV
;
1547 set_pba_of_lba0(map
, pba_of_lba0(map
)/IMSM_4K_DIV
);
1549 if (dev
->vol
.migr_state
) {
1551 map
= get_imsm_map(dev
, MAP_1
);
1552 set_blocks_per_member(map
,
1553 blocks_per_member(map
)/IMSM_4K_DIV
);
1554 map
->blocks_per_strip
/= IMSM_4K_DIV
;
1555 set_pba_of_lba0(map
, pba_of_lba0(map
)/IMSM_4K_DIV
);
1559 struct bbm_log
*log
= (void *)mpb
+
1560 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
1563 for (i
= 0; i
< log
->entry_count
; i
++) {
1564 struct bbm_log_entry
*entry
=
1565 &log
->marked_block_entries
[i
];
1567 __u8 count
= entry
->marked_count
+ 1;
1568 unsigned long long sector
=
1569 __le48_to_cpu(&entry
->defective_block_start
);
1571 entry
->defective_block_start
=
1572 __cpu_to_le48(sector
/IMSM_4K_DIV
);
1573 entry
->marked_count
= max(count
/IMSM_4K_DIV
, 1) - 1;
1577 mpb
->check_sum
= __gen_imsm_checksum(mpb
);
1580 void examine_migr_rec_imsm(struct intel_super
*super
)
1582 struct migr_record
*migr_rec
= super
->migr_rec
;
1583 struct imsm_super
*mpb
= super
->anchor
;
1586 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1587 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1588 struct imsm_map
*map
;
1591 if (is_gen_migration(dev
) == 0)
1594 printf("\nMigration Record Information:");
1596 /* first map under migration */
1597 map
= get_imsm_map(dev
, MAP_0
);
1599 slot
= get_imsm_disk_slot(map
, super
->disks
->index
);
1600 if (map
== NULL
|| slot
> 1 || slot
< 0) {
1601 printf(" Empty\n ");
1602 printf("Examine one of first two disks in array\n");
1605 printf("\n Status : ");
1606 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
1609 printf("Contains Data\n");
1610 printf(" Current Unit : %u\n",
1611 __le32_to_cpu(migr_rec
->curr_migr_unit
));
1612 printf(" Family : %u\n",
1613 __le32_to_cpu(migr_rec
->family_num
));
1614 printf(" Ascending : %u\n",
1615 __le32_to_cpu(migr_rec
->ascending_migr
));
1616 printf(" Blocks Per Unit : %u\n",
1617 __le32_to_cpu(migr_rec
->blocks_per_unit
));
1618 printf(" Dest. Depth Per Unit : %u\n",
1619 __le32_to_cpu(migr_rec
->dest_depth_per_unit
));
1620 printf(" Checkpoint Area pba : %u\n",
1621 __le32_to_cpu(migr_rec
->ckpt_area_pba
));
1622 printf(" First member lba : %u\n",
1623 __le32_to_cpu(migr_rec
->dest_1st_member_lba
));
1624 printf(" Total Number of Units : %u\n",
1625 __le32_to_cpu(migr_rec
->num_migr_units
));
1626 printf(" Size of volume : %u\n",
1627 __le32_to_cpu(migr_rec
->post_migr_vol_cap
));
1628 printf(" Expansion space for LBA64 : %u\n",
1629 __le32_to_cpu(migr_rec
->post_migr_vol_cap_hi
));
1630 printf(" Record was read from : %u\n",
1631 __le32_to_cpu(migr_rec
->ckpt_read_disk_num
));
1636 #endif /* MDASSEMBLE */
1638 void convert_from_4k_imsm_migr_rec(struct intel_super
*super
)
1640 struct migr_record
*migr_rec
= super
->migr_rec
;
1642 migr_rec
->blocks_per_unit
*= IMSM_4K_DIV
;
1643 migr_rec
->ckpt_area_pba
*= IMSM_4K_DIV
;
1644 migr_rec
->dest_1st_member_lba
*= IMSM_4K_DIV
;
1645 migr_rec
->dest_depth_per_unit
*= IMSM_4K_DIV
;
1646 split_ull((join_u32(migr_rec
->post_migr_vol_cap
,
1647 migr_rec
->post_migr_vol_cap_hi
) * IMSM_4K_DIV
),
1648 &migr_rec
->post_migr_vol_cap
,
1649 &migr_rec
->post_migr_vol_cap_hi
);
1652 void convert_from_4k(struct intel_super
*super
)
1654 struct imsm_super
*mpb
= super
->anchor
;
1655 struct imsm_disk
*disk
;
1657 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
1659 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1660 disk
= __get_imsm_disk(mpb
, i
);
1662 set_total_blocks(disk
, (total_blocks(disk
)*IMSM_4K_DIV
));
1665 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1666 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1667 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1669 split_ull((join_u32(dev
->size_low
, dev
->size_high
)*IMSM_4K_DIV
),
1670 &dev
->size_low
, &dev
->size_high
);
1671 dev
->vol
.curr_migr_unit
*= IMSM_4K_DIV
;
1674 set_blocks_per_member(map
, blocks_per_member(map
)*IMSM_4K_DIV
);
1675 map
->blocks_per_strip
*= IMSM_4K_DIV
;
1676 set_pba_of_lba0(map
, pba_of_lba0(map
)*IMSM_4K_DIV
);
1678 if (dev
->vol
.migr_state
) {
1680 map
= get_imsm_map(dev
, MAP_1
);
1681 set_blocks_per_member(map
,
1682 blocks_per_member(map
)*IMSM_4K_DIV
);
1683 map
->blocks_per_strip
*= IMSM_4K_DIV
;
1684 set_pba_of_lba0(map
, pba_of_lba0(map
)*IMSM_4K_DIV
);
1688 struct bbm_log
*log
= (void *)mpb
+
1689 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
1692 for (i
= 0; i
< log
->entry_count
; i
++) {
1693 struct bbm_log_entry
*entry
=
1694 &log
->marked_block_entries
[i
];
1696 __u8 count
= entry
->marked_count
+ 1;
1697 unsigned long long sector
=
1698 __le48_to_cpu(&entry
->defective_block_start
);
1700 entry
->defective_block_start
=
1701 __cpu_to_le48(sector
*IMSM_4K_DIV
);
1702 entry
->marked_count
= count
*IMSM_4K_DIV
- 1;
1706 mpb
->check_sum
= __gen_imsm_checksum(mpb
);
1709 /*******************************************************************************
1710 * function: imsm_check_attributes
1711 * Description: Function checks if features represented by attributes flags
1712 * are supported by mdadm.
1714 * attributes - Attributes read from metadata
1716 * 0 - passed attributes contains unsupported features flags
1717 * 1 - all features are supported
1718 ******************************************************************************/
1719 static int imsm_check_attributes(__u32 attributes
)
1722 __u32 not_supported
= MPB_ATTRIB_SUPPORTED
^0xffffffff;
1724 not_supported
&= ~MPB_ATTRIB_IGNORED
;
1726 not_supported
&= attributes
;
1727 if (not_supported
) {
1728 pr_err("(IMSM): Unsupported attributes : %x\n",
1729 (unsigned)__le32_to_cpu(not_supported
));
1730 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1731 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
1732 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1734 if (not_supported
& MPB_ATTRIB_2TB
) {
1735 dprintf("\t\tMPB_ATTRIB_2TB\n");
1736 not_supported
^= MPB_ATTRIB_2TB
;
1738 if (not_supported
& MPB_ATTRIB_RAID0
) {
1739 dprintf("\t\tMPB_ATTRIB_RAID0\n");
1740 not_supported
^= MPB_ATTRIB_RAID0
;
1742 if (not_supported
& MPB_ATTRIB_RAID1
) {
1743 dprintf("\t\tMPB_ATTRIB_RAID1\n");
1744 not_supported
^= MPB_ATTRIB_RAID1
;
1746 if (not_supported
& MPB_ATTRIB_RAID10
) {
1747 dprintf("\t\tMPB_ATTRIB_RAID10\n");
1748 not_supported
^= MPB_ATTRIB_RAID10
;
1750 if (not_supported
& MPB_ATTRIB_RAID1E
) {
1751 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
1752 not_supported
^= MPB_ATTRIB_RAID1E
;
1754 if (not_supported
& MPB_ATTRIB_RAID5
) {
1755 dprintf("\t\tMPB_ATTRIB_RAID5\n");
1756 not_supported
^= MPB_ATTRIB_RAID5
;
1758 if (not_supported
& MPB_ATTRIB_RAIDCNG
) {
1759 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
1760 not_supported
^= MPB_ATTRIB_RAIDCNG
;
1762 if (not_supported
& MPB_ATTRIB_BBM
) {
1763 dprintf("\t\tMPB_ATTRIB_BBM\n");
1764 not_supported
^= MPB_ATTRIB_BBM
;
1766 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1767 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
1768 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1770 if (not_supported
& MPB_ATTRIB_EXP_STRIPE_SIZE
) {
1771 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
1772 not_supported
^= MPB_ATTRIB_EXP_STRIPE_SIZE
;
1774 if (not_supported
& MPB_ATTRIB_2TB_DISK
) {
1775 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
1776 not_supported
^= MPB_ATTRIB_2TB_DISK
;
1778 if (not_supported
& MPB_ATTRIB_NEVER_USE2
) {
1779 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
1780 not_supported
^= MPB_ATTRIB_NEVER_USE2
;
1782 if (not_supported
& MPB_ATTRIB_NEVER_USE
) {
1783 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
1784 not_supported
^= MPB_ATTRIB_NEVER_USE
;
1788 dprintf("(IMSM): Unknown attributes : %x\n", not_supported
);
1797 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
1799 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
1801 struct intel_super
*super
= st
->sb
;
1802 struct imsm_super
*mpb
= super
->anchor
;
1803 char str
[MAX_SIGNATURE_LENGTH
];
1808 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
1811 snprintf(str
, MPB_SIG_LEN
, "%s", mpb
->sig
);
1812 printf(" Magic : %s\n", str
);
1813 snprintf(str
, strlen(MPB_VERSION_RAID0
), "%s", get_imsm_version(mpb
));
1814 printf(" Version : %s\n", get_imsm_version(mpb
));
1815 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
1816 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
1817 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
1818 printf(" Attributes : ");
1819 if (imsm_check_attributes(mpb
->attributes
))
1820 printf("All supported\n");
1822 printf("not supported\n");
1823 getinfo_super_imsm(st
, &info
, NULL
);
1824 fname_from_uuid(st
, &info
, nbuf
, ':');
1825 printf(" UUID : %s\n", nbuf
+ 5);
1826 sum
= __le32_to_cpu(mpb
->check_sum
);
1827 printf(" Checksum : %08x %s\n", sum
,
1828 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
1829 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
, super
->sector_size
));
1830 printf(" Disks : %d\n", mpb
->num_disks
);
1831 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
1832 print_imsm_disk(__get_imsm_disk(mpb
, super
->disks
->index
), super
->disks
->index
, reserved
);
1833 if (get_imsm_bbm_log_size(super
->bbm_log
)) {
1834 struct bbm_log
*log
= super
->bbm_log
;
1837 printf("Bad Block Management Log:\n");
1838 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
1839 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
1840 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
1842 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1844 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1846 super
->current_vol
= i
;
1847 getinfo_super_imsm(st
, &info
, NULL
);
1848 fname_from_uuid(st
, &info
, nbuf
, ':');
1849 print_imsm_dev(super
, dev
, nbuf
+ 5, super
->disks
->index
);
1851 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1852 if (i
== super
->disks
->index
)
1854 print_imsm_disk(__get_imsm_disk(mpb
, i
), i
, reserved
);
1857 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
1858 if (dl
->index
== -1)
1859 print_imsm_disk(&dl
->disk
, -1, reserved
);
1861 examine_migr_rec_imsm(super
);
1864 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
1866 /* We just write a generic IMSM ARRAY entry */
1869 struct intel_super
*super
= st
->sb
;
1871 if (!super
->anchor
->num_raid_devs
) {
1872 printf("ARRAY metadata=imsm\n");
1876 getinfo_super_imsm(st
, &info
, NULL
);
1877 fname_from_uuid(st
, &info
, nbuf
, ':');
1878 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
1881 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
1883 /* We just write a generic IMSM ARRAY entry */
1887 struct intel_super
*super
= st
->sb
;
1890 if (!super
->anchor
->num_raid_devs
)
1893 getinfo_super_imsm(st
, &info
, NULL
);
1894 fname_from_uuid(st
, &info
, nbuf
, ':');
1895 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1896 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1898 super
->current_vol
= i
;
1899 getinfo_super_imsm(st
, &info
, NULL
);
1900 fname_from_uuid(st
, &info
, nbuf1
, ':');
1901 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
1902 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
1906 static void export_examine_super_imsm(struct supertype
*st
)
1908 struct intel_super
*super
= st
->sb
;
1909 struct imsm_super
*mpb
= super
->anchor
;
1913 getinfo_super_imsm(st
, &info
, NULL
);
1914 fname_from_uuid(st
, &info
, nbuf
, ':');
1915 printf("MD_METADATA=imsm\n");
1916 printf("MD_LEVEL=container\n");
1917 printf("MD_UUID=%s\n", nbuf
+5);
1918 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
1921 static int copy_metadata_imsm(struct supertype
*st
, int from
, int to
)
1923 /* The second last sector of the device contains
1924 * the "struct imsm_super" metadata.
1925 * This contains mpb_size which is the size in bytes of the
1926 * extended metadata. This is located immediately before
1928 * We want to read all that, plus the last sector which
1929 * may contain a migration record, and write it all
1933 unsigned long long dsize
, offset
;
1935 struct imsm_super
*sb
;
1936 struct intel_super
*super
= st
->sb
;
1937 unsigned int sector_size
= super
->sector_size
;
1938 unsigned int written
= 0;
1940 if (posix_memalign(&buf
, MAX_SECTOR_SIZE
, MAX_SECTOR_SIZE
) != 0)
1943 if (!get_dev_size(from
, NULL
, &dsize
))
1946 if (lseek64(from
, dsize
-(2*sector_size
), 0) < 0)
1948 if ((unsigned int)read(from
, buf
, sector_size
) != sector_size
)
1951 if (strncmp((char*)sb
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0)
1954 sectors
= mpb_sectors(sb
, sector_size
) + 2;
1955 offset
= dsize
- sectors
* sector_size
;
1956 if (lseek64(from
, offset
, 0) < 0 ||
1957 lseek64(to
, offset
, 0) < 0)
1959 while (written
< sectors
* sector_size
) {
1960 int n
= sectors
*sector_size
- written
;
1963 if (read(from
, buf
, n
) != n
)
1965 if (write(to
, buf
, n
) != n
)
1976 static void detail_super_imsm(struct supertype
*st
, char *homehost
)
1981 getinfo_super_imsm(st
, &info
, NULL
);
1982 fname_from_uuid(st
, &info
, nbuf
, ':');
1983 printf("\n UUID : %s\n", nbuf
+ 5);
1986 static void brief_detail_super_imsm(struct supertype
*st
)
1990 getinfo_super_imsm(st
, &info
, NULL
);
1991 fname_from_uuid(st
, &info
, nbuf
, ':');
1992 printf(" UUID=%s", nbuf
+ 5);
1995 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
);
1996 static void fd2devname(int fd
, char *name
);
1998 static int ahci_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
2000 /* dump an unsorted list of devices attached to AHCI Intel storage
2001 * controller, as well as non-connected ports
2003 int hba_len
= strlen(hba_path
) + 1;
2008 unsigned long port_mask
= (1 << port_count
) - 1;
2010 if (port_count
> (int)sizeof(port_mask
) * 8) {
2012 pr_err("port_count %d out of range\n", port_count
);
2016 /* scroll through /sys/dev/block looking for devices attached to
2019 dir
= opendir("/sys/dev/block");
2023 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2034 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
2036 path
= devt_to_devpath(makedev(major
, minor
));
2039 if (!path_attached_to_hba(path
, hba_path
)) {
2045 /* retrieve the scsi device type */
2046 if (asprintf(&device
, "/sys/dev/block/%d:%d/device/xxxxxxx", major
, minor
) < 0) {
2048 pr_err("failed to allocate 'device'\n");
2052 sprintf(device
, "/sys/dev/block/%d:%d/device/type", major
, minor
);
2053 if (load_sys(device
, buf
, sizeof(buf
)) != 0) {
2055 pr_err("failed to read device type for %s\n",
2061 type
= strtoul(buf
, NULL
, 10);
2063 /* if it's not a disk print the vendor and model */
2064 if (!(type
== 0 || type
== 7 || type
== 14)) {
2067 sprintf(device
, "/sys/dev/block/%d:%d/device/vendor", major
, minor
);
2068 if (load_sys(device
, buf
, sizeof(buf
)) == 0) {
2069 strncpy(vendor
, buf
, sizeof(vendor
));
2070 vendor
[sizeof(vendor
) - 1] = '\0';
2071 c
= (char *) &vendor
[sizeof(vendor
) - 1];
2072 while (isspace(*c
) || *c
== '\0')
2076 sprintf(device
, "/sys/dev/block/%d:%d/device/model", major
, minor
);
2077 if (load_sys(device
, buf
, sizeof(buf
)) == 0) {
2078 strncpy(model
, buf
, sizeof(model
));
2079 model
[sizeof(model
) - 1] = '\0';
2080 c
= (char *) &model
[sizeof(model
) - 1];
2081 while (isspace(*c
) || *c
== '\0')
2085 if (vendor
[0] && model
[0])
2086 sprintf(buf
, "%.64s %.64s", vendor
, model
);
2088 switch (type
) { /* numbers from hald/linux/device.c */
2089 case 1: sprintf(buf
, "tape"); break;
2090 case 2: sprintf(buf
, "printer"); break;
2091 case 3: sprintf(buf
, "processor"); break;
2093 case 5: sprintf(buf
, "cdrom"); break;
2094 case 6: sprintf(buf
, "scanner"); break;
2095 case 8: sprintf(buf
, "media_changer"); break;
2096 case 9: sprintf(buf
, "comm"); break;
2097 case 12: sprintf(buf
, "raid"); break;
2098 default: sprintf(buf
, "unknown");
2104 /* chop device path to 'host%d' and calculate the port number */
2105 c
= strchr(&path
[hba_len
], '/');
2108 pr_err("%s - invalid path name\n", path
+ hba_len
);
2113 if ((sscanf(&path
[hba_len
], "ata%d", &port
) == 1) ||
2114 ((sscanf(&path
[hba_len
], "host%d", &port
) == 1)))
2118 *c
= '/'; /* repair the full string */
2119 pr_err("failed to determine port number for %s\n",
2126 /* mark this port as used */
2127 port_mask
&= ~(1 << port
);
2129 /* print out the device information */
2131 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
2135 fd
= dev_open(ent
->d_name
, O_RDONLY
);
2137 printf(" Port%d : - disk info unavailable -\n", port
);
2139 fd2devname(fd
, buf
);
2140 printf(" Port%d : %s", port
, buf
);
2141 if (imsm_read_serial(fd
, NULL
, (__u8
*) buf
) == 0)
2142 printf(" (%.*s)\n", MAX_RAID_SERIAL_LEN
, buf
);
2157 for (i
= 0; i
< port_count
; i
++)
2158 if (port_mask
& (1 << i
))
2159 printf(" Port%d : - no device attached -\n", i
);
2165 static int print_vmd_attached_devs(struct sys_dev
*hba
)
2173 if (hba
->type
!= SYS_DEV_VMD
)
2176 /* scroll through /sys/dev/block looking for devices attached to
2179 dir
= opendir("/sys/bus/pci/drivers/nvme");
2183 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2186 /* is 'ent' a device? check that the 'subsystem' link exists and
2187 * that its target matches 'bus'
2189 sprintf(path
, "/sys/bus/pci/drivers/nvme/%s/subsystem",
2191 n
= readlink(path
, link
, sizeof(link
));
2192 if (n
< 0 || n
>= (int)sizeof(link
))
2195 c
= strrchr(link
, '/');
2198 if (strncmp("pci", c
+1, strlen("pci")) != 0)
2201 sprintf(path
, "/sys/bus/pci/drivers/nvme/%s", ent
->d_name
);
2203 rp
= realpath(path
, NULL
);
2207 if (path_attached_to_hba(rp
, hba
->path
)) {
2208 printf(" NVMe under VMD : %s\n", rp
);
2217 static void print_found_intel_controllers(struct sys_dev
*elem
)
2219 for (; elem
; elem
= elem
->next
) {
2220 pr_err("found Intel(R) ");
2221 if (elem
->type
== SYS_DEV_SATA
)
2222 fprintf(stderr
, "SATA ");
2223 else if (elem
->type
== SYS_DEV_SAS
)
2224 fprintf(stderr
, "SAS ");
2225 else if (elem
->type
== SYS_DEV_NVME
)
2226 fprintf(stderr
, "NVMe ");
2228 if (elem
->type
== SYS_DEV_VMD
)
2229 fprintf(stderr
, "VMD domain");
2231 fprintf(stderr
, "RAID controller");
2234 fprintf(stderr
, " at %s", elem
->pci_id
);
2235 fprintf(stderr
, ".\n");
2240 static int ahci_get_port_count(const char *hba_path
, int *port_count
)
2247 if ((dir
= opendir(hba_path
)) == NULL
)
2250 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2253 if ((sscanf(ent
->d_name
, "ata%d", &host
) != 1) &&
2254 ((sscanf(ent
->d_name
, "host%d", &host
) != 1)))
2256 if (*port_count
== 0)
2258 else if (host
< host_base
)
2261 if (host
+ 1 > *port_count
+ host_base
)
2262 *port_count
= host
+ 1 - host_base
;
2268 static void print_imsm_capability(const struct imsm_orom
*orom
)
2270 printf(" Platform : Intel(R) ");
2271 if (orom
->capabilities
== 0 && orom
->driver_features
== 0)
2272 printf("Matrix Storage Manager\n");
2274 printf("Rapid Storage Technology%s\n",
2275 imsm_orom_is_enterprise(orom
) ? " enterprise" : "");
2276 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
)
2277 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
,
2278 orom
->minor_ver
, orom
->hotfix_ver
, orom
->build
);
2279 printf(" RAID Levels :%s%s%s%s%s\n",
2280 imsm_orom_has_raid0(orom
) ? " raid0" : "",
2281 imsm_orom_has_raid1(orom
) ? " raid1" : "",
2282 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
2283 imsm_orom_has_raid10(orom
) ? " raid10" : "",
2284 imsm_orom_has_raid5(orom
) ? " raid5" : "");
2285 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2286 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
2287 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
2288 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
2289 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
2290 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
2291 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
2292 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
2293 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
2294 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
2295 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
2296 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
2297 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
2298 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
2299 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
2300 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
2301 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
2302 printf(" 2TB volumes :%s supported\n",
2303 (orom
->attr
& IMSM_OROM_ATTR_2TB
)?"":" not");
2304 printf(" 2TB disks :%s supported\n",
2305 (orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
)?"":" not");
2306 printf(" Max Disks : %d\n", orom
->tds
);
2307 printf(" Max Volumes : %d per array, %d per %s\n",
2308 orom
->vpa
, orom
->vphba
,
2309 imsm_orom_is_nvme(orom
) ? "platform" : "controller");
2313 static void print_imsm_capability_export(const struct imsm_orom
*orom
)
2315 printf("MD_FIRMWARE_TYPE=imsm\n");
2316 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
)
2317 printf("IMSM_VERSION=%d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
2318 orom
->hotfix_ver
, orom
->build
);
2319 printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n",
2320 imsm_orom_has_raid0(orom
) ? "raid0 " : "",
2321 imsm_orom_has_raid1(orom
) ? "raid1 " : "",
2322 imsm_orom_has_raid1e(orom
) ? "raid1e " : "",
2323 imsm_orom_has_raid5(orom
) ? "raid10 " : "",
2324 imsm_orom_has_raid10(orom
) ? "raid5 " : "");
2325 printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2326 imsm_orom_has_chunk(orom
, 2) ? "2k " : "",
2327 imsm_orom_has_chunk(orom
, 4) ? "4k " : "",
2328 imsm_orom_has_chunk(orom
, 8) ? "8k " : "",
2329 imsm_orom_has_chunk(orom
, 16) ? "16k " : "",
2330 imsm_orom_has_chunk(orom
, 32) ? "32k " : "",
2331 imsm_orom_has_chunk(orom
, 64) ? "64k " : "",
2332 imsm_orom_has_chunk(orom
, 128) ? "128k " : "",
2333 imsm_orom_has_chunk(orom
, 256) ? "256k " : "",
2334 imsm_orom_has_chunk(orom
, 512) ? "512k " : "",
2335 imsm_orom_has_chunk(orom
, 1024*1) ? "1M " : "",
2336 imsm_orom_has_chunk(orom
, 1024*2) ? "2M " : "",
2337 imsm_orom_has_chunk(orom
, 1024*4) ? "4M " : "",
2338 imsm_orom_has_chunk(orom
, 1024*8) ? "8M " : "",
2339 imsm_orom_has_chunk(orom
, 1024*16) ? "16M " : "",
2340 imsm_orom_has_chunk(orom
, 1024*32) ? "32M " : "",
2341 imsm_orom_has_chunk(orom
, 1024*64) ? "64M " : "");
2342 printf("IMSM_2TB_VOLUMES=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB
) ? "yes" : "no");
2343 printf("IMSM_2TB_DISKS=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) ? "yes" : "no");
2344 printf("IMSM_MAX_DISKS=%d\n",orom
->tds
);
2345 printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom
->vpa
);
2346 printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom
->vphba
);
2349 static int detail_platform_imsm(int verbose
, int enumerate_only
, char *controller_path
)
2351 /* There are two components to imsm platform support, the ahci SATA
2352 * controller and the option-rom. To find the SATA controller we
2353 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
2354 * controller with the Intel vendor id is present. This approach
2355 * allows mdadm to leverage the kernel's ahci detection logic, with the
2356 * caveat that if ahci.ko is not loaded mdadm will not be able to
2357 * detect platform raid capabilities. The option-rom resides in a
2358 * platform "Adapter ROM". We scan for its signature to retrieve the
2359 * platform capabilities. If raid support is disabled in the BIOS the
2360 * option-rom capability structure will not be available.
2362 struct sys_dev
*list
, *hba
;
2367 if (enumerate_only
) {
2368 if (check_env("IMSM_NO_PLATFORM"))
2370 list
= find_intel_devices();
2373 for (hba
= list
; hba
; hba
= hba
->next
) {
2374 if (find_imsm_capability(hba
)) {
2384 list
= find_intel_devices();
2387 pr_err("no active Intel(R) RAID controller found.\n");
2389 } else if (verbose
> 0)
2390 print_found_intel_controllers(list
);
2392 for (hba
= list
; hba
; hba
= hba
->next
) {
2393 if (controller_path
&& (compare_paths(hba
->path
, controller_path
) != 0))
2395 if (!find_imsm_capability(hba
)) {
2397 pr_err("imsm capabilities not found for controller: %s (type %s)\n",
2398 hba
->type
== SYS_DEV_VMD
? vmd_domain_to_controller(hba
, buf
) : hba
->path
,
2399 get_sys_dev_type(hba
->type
));
2405 if (controller_path
&& result
== 1) {
2406 pr_err("no active Intel(R) RAID controller found under %s\n",
2411 const struct orom_entry
*entry
;
2413 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2414 if (entry
->type
== SYS_DEV_VMD
) {
2415 print_imsm_capability(&entry
->orom
);
2416 printf(" 3rd party NVMe :%s supported\n",
2417 imsm_orom_has_tpv_support(&entry
->orom
)?"":" not");
2418 for (hba
= list
; hba
; hba
= hba
->next
) {
2419 if (hba
->type
== SYS_DEV_VMD
) {
2421 printf(" I/O Controller : %s (%s)\n",
2422 vmd_domain_to_controller(hba
, buf
), get_sys_dev_type(hba
->type
));
2423 if (print_vmd_attached_devs(hba
)) {
2425 pr_err("failed to get devices attached to VMD domain.\n");
2434 print_imsm_capability(&entry
->orom
);
2435 if (entry
->type
== SYS_DEV_NVME
) {
2436 for (hba
= list
; hba
; hba
= hba
->next
) {
2437 if (hba
->type
== SYS_DEV_NVME
)
2438 printf(" NVMe Device : %s\n", hba
->path
);
2444 struct devid_list
*devid
;
2445 for (devid
= entry
->devid_list
; devid
; devid
= devid
->next
) {
2446 hba
= device_by_id(devid
->devid
);
2450 printf(" I/O Controller : %s (%s)\n",
2451 hba
->path
, get_sys_dev_type(hba
->type
));
2452 if (hba
->type
== SYS_DEV_SATA
) {
2453 host_base
= ahci_get_port_count(hba
->path
, &port_count
);
2454 if (ahci_enumerate_ports(hba
->path
, port_count
, host_base
, verbose
)) {
2456 pr_err("failed to enumerate ports on SATA controller at %s.\n", hba
->pci_id
);
2467 static int export_detail_platform_imsm(int verbose
, char *controller_path
)
2469 struct sys_dev
*list
, *hba
;
2472 list
= find_intel_devices();
2475 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n");
2480 for (hba
= list
; hba
; hba
= hba
->next
) {
2481 if (controller_path
&& (compare_paths(hba
->path
,controller_path
) != 0))
2483 if (!find_imsm_capability(hba
) && verbose
> 0) {
2485 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n",
2486 hba
->type
== SYS_DEV_VMD
? vmd_domain_to_controller(hba
, buf
) : hba
->path
);
2492 const struct orom_entry
*entry
;
2494 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2495 if (entry
->type
== SYS_DEV_VMD
) {
2496 for (hba
= list
; hba
; hba
= hba
->next
)
2497 print_imsm_capability_export(&entry
->orom
);
2500 print_imsm_capability_export(&entry
->orom
);
2508 static int match_home_imsm(struct supertype
*st
, char *homehost
)
2510 /* the imsm metadata format does not specify any host
2511 * identification information. We return -1 since we can never
2512 * confirm nor deny whether a given array is "meant" for this
2513 * host. We rely on compare_super and the 'family_num' fields to
2514 * exclude member disks that do not belong, and we rely on
2515 * mdadm.conf to specify the arrays that should be assembled.
2516 * Auto-assembly may still pick up "foreign" arrays.
2522 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
2524 /* The uuid returned here is used for:
2525 * uuid to put into bitmap file (Create, Grow)
2526 * uuid for backup header when saving critical section (Grow)
2527 * comparing uuids when re-adding a device into an array
2528 * In these cases the uuid required is that of the data-array,
2529 * not the device-set.
2530 * uuid to recognise same set when adding a missing device back
2531 * to an array. This is a uuid for the device-set.
2533 * For each of these we can make do with a truncated
2534 * or hashed uuid rather than the original, as long as
2536 * In each case the uuid required is that of the data-array,
2537 * not the device-set.
2539 /* imsm does not track uuid's so we synthesis one using sha1 on
2540 * - The signature (Which is constant for all imsm array, but no matter)
2541 * - the orig_family_num of the container
2542 * - the index number of the volume
2543 * - the 'serial' number of the volume.
2544 * Hopefully these are all constant.
2546 struct intel_super
*super
= st
->sb
;
2549 struct sha1_ctx ctx
;
2550 struct imsm_dev
*dev
= NULL
;
2553 /* some mdadm versions failed to set ->orig_family_num, in which
2554 * case fall back to ->family_num. orig_family_num will be
2555 * fixed up with the first metadata update.
2557 family_num
= super
->anchor
->orig_family_num
;
2558 if (family_num
== 0)
2559 family_num
= super
->anchor
->family_num
;
2560 sha1_init_ctx(&ctx
);
2561 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
2562 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
2563 if (super
->current_vol
>= 0)
2564 dev
= get_imsm_dev(super
, super
->current_vol
);
2566 __u32 vol
= super
->current_vol
;
2567 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
2568 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
2570 sha1_finish_ctx(&ctx
, buf
);
2571 memcpy(uuid
, buf
, 4*4);
2576 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
2578 __u8
*v
= get_imsm_version(mpb
);
2579 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
2580 char major
[] = { 0, 0, 0 };
2581 char minor
[] = { 0 ,0, 0 };
2582 char patch
[] = { 0, 0, 0 };
2583 char *ver_parse
[] = { major
, minor
, patch
};
2587 while (*v
!= '\0' && v
< end
) {
2588 if (*v
!= '.' && j
< 2)
2589 ver_parse
[i
][j
++] = *v
;
2597 *m
= strtol(minor
, NULL
, 0);
2598 *p
= strtol(patch
, NULL
, 0);
2602 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
2604 /* migr_strip_size when repairing or initializing parity */
2605 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2606 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2608 switch (get_imsm_raid_level(map
)) {
2613 return 128*1024 >> 9;
2617 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
2619 /* migr_strip_size when rebuilding a degraded disk, no idea why
2620 * this is different than migr_strip_size_resync(), but it's good
2623 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2624 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2626 switch (get_imsm_raid_level(map
)) {
2629 if (map
->num_members
% map
->num_domains
== 0)
2630 return 128*1024 >> 9;
2634 return max((__u32
) 64*1024 >> 9, chunk
);
2636 return 128*1024 >> 9;
2640 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
2642 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2643 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2644 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
2645 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
2647 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
2650 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
2652 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2653 int level
= get_imsm_raid_level(lo
);
2655 if (level
== 1 || level
== 10) {
2656 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2658 return hi
->num_domains
;
2660 return num_stripes_per_unit_resync(dev
);
2663 static __u8
imsm_num_data_members(struct imsm_dev
*dev
, int second_map
)
2665 /* named 'imsm_' because raid0, raid1 and raid10
2666 * counter-intuitively have the same number of data disks
2668 struct imsm_map
*map
= get_imsm_map(dev
, second_map
);
2670 switch (get_imsm_raid_level(map
)) {
2672 return map
->num_members
;
2676 return map
->num_members
/2;
2678 return map
->num_members
- 1;
2680 dprintf("unsupported raid level\n");
2685 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
2687 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2688 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2690 switch(get_imsm_raid_level(map
)) {
2693 return chunk
* map
->num_domains
;
2695 return chunk
* map
->num_members
;
2701 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
2703 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2704 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2705 __u32 strip
= block
/ chunk
;
2707 switch (get_imsm_raid_level(map
)) {
2710 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
2711 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
2713 return vol_stripe
* chunk
+ block
% chunk
;
2715 __u32 stripe
= strip
/ (map
->num_members
- 1);
2717 return stripe
* chunk
+ block
% chunk
;
2724 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
2725 struct imsm_dev
*dev
)
2727 /* calculate the conversion factor between per member 'blocks'
2728 * (md/{resync,rebuild}_start) and imsm migration units, return
2729 * 0 for the 'not migrating' and 'unsupported migration' cases
2731 if (!dev
->vol
.migr_state
)
2734 switch (migr_type(dev
)) {
2735 case MIGR_GEN_MIGR
: {
2736 struct migr_record
*migr_rec
= super
->migr_rec
;
2737 return __le32_to_cpu(migr_rec
->blocks_per_unit
);
2742 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2743 __u32 stripes_per_unit
;
2744 __u32 blocks_per_unit
;
2753 /* yes, this is really the translation of migr_units to
2754 * per-member blocks in the 'resync' case
2756 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
2757 migr_chunk
= migr_strip_blocks_resync(dev
);
2758 disks
= imsm_num_data_members(dev
, MAP_0
);
2759 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
2760 stripe
= __le16_to_cpu(map
->blocks_per_strip
) * disks
;
2761 segment
= blocks_per_unit
/ stripe
;
2762 block_rel
= blocks_per_unit
- segment
* stripe
;
2763 parity_depth
= parity_segment_depth(dev
);
2764 block_map
= map_migr_block(dev
, block_rel
);
2765 return block_map
+ parity_depth
* segment
;
2767 case MIGR_REBUILD
: {
2768 __u32 stripes_per_unit
;
2771 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
2772 migr_chunk
= migr_strip_blocks_rebuild(dev
);
2773 return migr_chunk
* stripes_per_unit
;
2775 case MIGR_STATE_CHANGE
:
2781 static int imsm_level_to_layout(int level
)
2789 return ALGORITHM_LEFT_ASYMMETRIC
;
2796 /*******************************************************************************
2797 * Function: read_imsm_migr_rec
2798 * Description: Function reads imsm migration record from last sector of disk
2800 * fd : disk descriptor
2801 * super : metadata info
2805 ******************************************************************************/
2806 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
)
2809 unsigned int sector_size
= super
->sector_size
;
2810 unsigned long long dsize
;
2812 get_dev_size(fd
, NULL
, &dsize
);
2813 if (lseek64(fd
, dsize
- (sector_size
*MIGR_REC_SECTOR_POSITION
),
2815 pr_err("Cannot seek to anchor block: %s\n",
2819 if ((unsigned int)read(fd
, super
->migr_rec_buf
,
2820 MIGR_REC_BUF_SECTORS
*sector_size
) !=
2821 MIGR_REC_BUF_SECTORS
*sector_size
) {
2822 pr_err("Cannot read migr record block: %s\n",
2827 if (sector_size
== 4096)
2828 convert_from_4k_imsm_migr_rec(super
);
2834 static struct imsm_dev
*imsm_get_device_during_migration(
2835 struct intel_super
*super
)
2838 struct intel_dev
*dv
;
2840 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
2841 if (is_gen_migration(dv
->dev
))
2847 /*******************************************************************************
2848 * Function: load_imsm_migr_rec
2849 * Description: Function reads imsm migration record (it is stored at the last
2852 * super : imsm internal array info
2853 * info : general array info
2857 * -2 : no migration in progress
2858 ******************************************************************************/
2859 static int load_imsm_migr_rec(struct intel_super
*super
, struct mdinfo
*info
)
2866 struct imsm_dev
*dev
;
2867 struct imsm_map
*map
;
2870 /* find map under migration */
2871 dev
= imsm_get_device_during_migration(super
);
2872 /* nothing to load,no migration in progress?
2878 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
2879 /* read only from one of the first two slots */
2880 if ((sd
->disk
.raid_disk
< 0) ||
2881 (sd
->disk
.raid_disk
> 1))
2884 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
2885 fd
= dev_open(nm
, O_RDONLY
);
2891 map
= get_imsm_map(dev
, MAP_0
);
2892 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
2893 /* skip spare and failed disks
2897 /* read only from one of the first two slots */
2899 slot
= get_imsm_disk_slot(map
, dl
->index
);
2900 if (map
== NULL
|| slot
> 1 || slot
< 0)
2902 sprintf(nm
, "%d:%d", dl
->major
, dl
->minor
);
2903 fd
= dev_open(nm
, O_RDONLY
);
2910 retval
= read_imsm_migr_rec(fd
, super
);
2919 /*******************************************************************************
2920 * function: imsm_create_metadata_checkpoint_update
2921 * Description: It creates update for checkpoint change.
2923 * super : imsm internal array info
2924 * u : pointer to prepared update
2927 * If length is equal to 0, input pointer u contains no update
2928 ******************************************************************************/
2929 static int imsm_create_metadata_checkpoint_update(
2930 struct intel_super
*super
,
2931 struct imsm_update_general_migration_checkpoint
**u
)
2934 int update_memory_size
= 0;
2936 dprintf("(enter)\n");
2942 /* size of all update data without anchor */
2943 update_memory_size
=
2944 sizeof(struct imsm_update_general_migration_checkpoint
);
2946 *u
= xcalloc(1, update_memory_size
);
2948 dprintf("error: cannot get memory\n");
2951 (*u
)->type
= update_general_migration_checkpoint
;
2952 (*u
)->curr_migr_unit
= __le32_to_cpu(super
->migr_rec
->curr_migr_unit
);
2953 dprintf("prepared for %u\n", (*u
)->curr_migr_unit
);
2955 return update_memory_size
;
2958 static void imsm_update_metadata_locally(struct supertype
*st
,
2959 void *buf
, int len
);
2961 /*******************************************************************************
2962 * Function: write_imsm_migr_rec
2963 * Description: Function writes imsm migration record
2964 * (at the last sector of disk)
2966 * super : imsm internal array info
2970 ******************************************************************************/
2971 static int write_imsm_migr_rec(struct supertype
*st
)
2973 struct intel_super
*super
= st
->sb
;
2974 unsigned int sector_size
= super
->sector_size
;
2975 unsigned long long dsize
;
2981 struct imsm_update_general_migration_checkpoint
*u
;
2982 struct imsm_dev
*dev
;
2983 struct imsm_map
*map
;
2985 /* find map under migration */
2986 dev
= imsm_get_device_during_migration(super
);
2987 /* if no migration, write buffer anyway to clear migr_record
2988 * on disk based on first available device
2991 dev
= get_imsm_dev(super
, super
->current_vol
< 0 ? 0 :
2992 super
->current_vol
);
2994 map
= get_imsm_map(dev
, MAP_0
);
2996 if (sector_size
== 4096)
2997 convert_to_4k_imsm_migr_rec(super
);
2998 for (sd
= super
->disks
; sd
; sd
= sd
->next
) {
3001 /* skip failed and spare devices */
3004 /* write to 2 first slots only */
3006 slot
= get_imsm_disk_slot(map
, sd
->index
);
3007 if (map
== NULL
|| slot
> 1 || slot
< 0)
3010 sprintf(nm
, "%d:%d", sd
->major
, sd
->minor
);
3011 fd
= dev_open(nm
, O_RDWR
);
3014 get_dev_size(fd
, NULL
, &dsize
);
3015 if (lseek64(fd
, dsize
- (MIGR_REC_SECTOR_POSITION
*sector_size
),
3017 pr_err("Cannot seek to anchor block: %s\n",
3021 if ((unsigned int)write(fd
, super
->migr_rec_buf
,
3022 MIGR_REC_BUF_SECTORS
*sector_size
) !=
3023 MIGR_REC_BUF_SECTORS
*sector_size
) {
3024 pr_err("Cannot write migr record block: %s\n",
3031 if (sector_size
== 4096)
3032 convert_from_4k_imsm_migr_rec(super
);
3033 /* update checkpoint information in metadata */
3034 len
= imsm_create_metadata_checkpoint_update(super
, &u
);
3036 dprintf("imsm: Cannot prepare update\n");
3039 /* update metadata locally */
3040 imsm_update_metadata_locally(st
, u
, len
);
3041 /* and possibly remotely */
3042 if (st
->update_tail
) {
3043 append_metadata_update(st
, u
, len
);
3044 /* during reshape we do all work inside metadata handler
3045 * manage_reshape(), so metadata update has to be triggered
3048 flush_metadata_updates(st
);
3049 st
->update_tail
= &st
->updates
;
3059 #endif /* MDASSEMBLE */
3061 /* spare/missing disks activations are not allowe when
3062 * array/container performs reshape operation, because
3063 * all arrays in container works on the same disks set
3065 int imsm_reshape_blocks_arrays_changes(struct intel_super
*super
)
3068 struct intel_dev
*i_dev
;
3069 struct imsm_dev
*dev
;
3071 /* check whole container
3073 for (i_dev
= super
->devlist
; i_dev
; i_dev
= i_dev
->next
) {
3075 if (is_gen_migration(dev
)) {
3076 /* No repair during any migration in container
3084 static unsigned long long imsm_component_size_aligment_check(int level
,
3086 unsigned int sector_size
,
3087 unsigned long long component_size
)
3089 unsigned int component_size_alligment
;
3091 /* check component size aligment
3093 component_size_alligment
= component_size
% (chunk_size
/sector_size
);
3095 dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alligment = %u\n",
3096 level
, chunk_size
, component_size
,
3097 component_size_alligment
);
3099 if (component_size_alligment
&& (level
!= 1) && (level
!= UnSet
)) {
3100 dprintf("imsm: reported component size alligned from %llu ",
3102 component_size
-= component_size_alligment
;
3103 dprintf_cont("to %llu (%i).\n",
3104 component_size
, component_size_alligment
);
3107 return component_size
;
3110 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
3112 struct intel_super
*super
= st
->sb
;
3113 struct migr_record
*migr_rec
= super
->migr_rec
;
3114 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
3115 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3116 struct imsm_map
*prev_map
= get_imsm_map(dev
, MAP_1
);
3117 struct imsm_map
*map_to_analyse
= map
;
3119 int map_disks
= info
->array
.raid_disks
;
3121 memset(info
, 0, sizeof(*info
));
3123 map_to_analyse
= prev_map
;
3125 dl
= super
->current_disk
;
3127 info
->container_member
= super
->current_vol
;
3128 info
->array
.raid_disks
= map
->num_members
;
3129 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
3130 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
3131 info
->array
.md_minor
= -1;
3132 info
->array
.ctime
= 0;
3133 info
->array
.utime
= 0;
3134 info
->array
.chunk_size
=
3135 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
3136 info
->array
.state
= !dev
->vol
.dirty
;
3137 info
->custom_array_size
= __le32_to_cpu(dev
->size_high
);
3138 info
->custom_array_size
<<= 32;
3139 info
->custom_array_size
|= __le32_to_cpu(dev
->size_low
);
3140 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
3142 if (is_gen_migration(dev
)) {
3143 info
->reshape_active
= 1;
3144 info
->new_level
= get_imsm_raid_level(map
);
3145 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
3146 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
3147 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
3148 if (info
->delta_disks
) {
3149 /* this needs to be applied to every array
3152 info
->reshape_active
= CONTAINER_RESHAPE
;
3154 /* We shape information that we give to md might have to be
3155 * modify to cope with md's requirement for reshaping arrays.
3156 * For example, when reshaping a RAID0, md requires it to be
3157 * presented as a degraded RAID4.
3158 * Also if a RAID0 is migrating to a RAID5 we need to specify
3159 * the array as already being RAID5, but the 'before' layout
3160 * is a RAID4-like layout.
3162 switch (info
->array
.level
) {
3164 switch(info
->new_level
) {
3166 /* conversion is happening as RAID4 */
3167 info
->array
.level
= 4;
3168 info
->array
.raid_disks
+= 1;
3171 /* conversion is happening as RAID5 */
3172 info
->array
.level
= 5;
3173 info
->array
.layout
= ALGORITHM_PARITY_N
;
3174 info
->delta_disks
-= 1;
3177 /* FIXME error message */
3178 info
->array
.level
= UnSet
;
3184 info
->new_level
= UnSet
;
3185 info
->new_layout
= UnSet
;
3186 info
->new_chunk
= info
->array
.chunk_size
;
3187 info
->delta_disks
= 0;
3191 info
->disk
.major
= dl
->major
;
3192 info
->disk
.minor
= dl
->minor
;
3193 info
->disk
.number
= dl
->index
;
3194 info
->disk
.raid_disk
= get_imsm_disk_slot(map_to_analyse
,
3198 info
->data_offset
= pba_of_lba0(map_to_analyse
);
3200 if (info
->array
.level
== 5) {
3201 info
->component_size
= num_data_stripes(map_to_analyse
) *
3202 map_to_analyse
->blocks_per_strip
;
3204 info
->component_size
= blocks_per_member(map_to_analyse
);
3207 info
->component_size
= imsm_component_size_aligment_check(
3209 info
->array
.chunk_size
,
3211 info
->component_size
);
3212 info
->bb
.supported
= 1;
3214 memset(info
->uuid
, 0, sizeof(info
->uuid
));
3215 info
->recovery_start
= MaxSector
;
3217 info
->reshape_progress
= 0;
3218 info
->resync_start
= MaxSector
;
3219 if ((map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
3221 imsm_reshape_blocks_arrays_changes(super
) == 0) {
3222 info
->resync_start
= 0;
3224 if (dev
->vol
.migr_state
) {
3225 switch (migr_type(dev
)) {
3228 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
3230 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
3232 info
->resync_start
= blocks_per_unit
* units
;
3235 case MIGR_GEN_MIGR
: {
3236 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
3238 __u64 units
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
3239 unsigned long long array_blocks
;
3242 if (__le32_to_cpu(migr_rec
->ascending_migr
) &&
3244 (__le32_to_cpu(migr_rec
->num_migr_units
)-1)) &&
3245 (super
->migr_rec
->rec_status
==
3246 __cpu_to_le32(UNIT_SRC_IN_CP_AREA
)))
3249 info
->reshape_progress
= blocks_per_unit
* units
;
3251 dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n",
3252 (unsigned long long)units
,
3253 (unsigned long long)blocks_per_unit
,
3254 info
->reshape_progress
);
3256 used_disks
= imsm_num_data_members(dev
, MAP_1
);
3257 if (used_disks
> 0) {
3258 array_blocks
= blocks_per_member(map
) *
3260 /* round array size down to closest MB
3262 info
->custom_array_size
= (array_blocks
3263 >> SECT_PER_MB_SHIFT
)
3264 << SECT_PER_MB_SHIFT
;
3268 /* we could emulate the checkpointing of
3269 * 'sync_action=check' migrations, but for now
3270 * we just immediately complete them
3273 /* this is handled by container_content_imsm() */
3274 case MIGR_STATE_CHANGE
:
3275 /* FIXME handle other migrations */
3277 /* we are not dirty, so... */
3278 info
->resync_start
= MaxSector
;
3282 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
3283 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
3285 info
->array
.major_version
= -1;
3286 info
->array
.minor_version
= -2;
3287 sprintf(info
->text_version
, "/%s/%d", st
->container_devnm
, info
->container_member
);
3288 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
3289 uuid_from_super_imsm(st
, info
->uuid
);
3293 for (i
=0; i
<map_disks
; i
++) {
3295 if (i
< info
->array
.raid_disks
) {
3296 struct imsm_disk
*dsk
;
3297 j
= get_imsm_disk_idx(dev
, i
, MAP_X
);
3298 dsk
= get_imsm_disk(super
, j
);
3299 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
3306 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
3307 int failed
, int look_in_map
);
3309 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
3313 static void manage_second_map(struct intel_super
*super
, struct imsm_dev
*dev
)
3315 if (is_gen_migration(dev
)) {
3318 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
3320 failed
= imsm_count_failed(super
, dev
, MAP_1
);
3321 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_1
);
3322 if (map2
->map_state
!= map_state
) {
3323 map2
->map_state
= map_state
;
3324 super
->updates_pending
++;
3330 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
3334 for (d
= super
->missing
; d
; d
= d
->next
)
3335 if (d
->index
== index
)
3340 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
3342 struct intel_super
*super
= st
->sb
;
3343 struct imsm_disk
*disk
;
3344 int map_disks
= info
->array
.raid_disks
;
3345 int max_enough
= -1;
3347 struct imsm_super
*mpb
;
3349 if (super
->current_vol
>= 0) {
3350 getinfo_super_imsm_volume(st
, info
, map
);
3353 memset(info
, 0, sizeof(*info
));
3355 /* Set raid_disks to zero so that Assemble will always pull in valid
3358 info
->array
.raid_disks
= 0;
3359 info
->array
.level
= LEVEL_CONTAINER
;
3360 info
->array
.layout
= 0;
3361 info
->array
.md_minor
= -1;
3362 info
->array
.ctime
= 0; /* N/A for imsm */
3363 info
->array
.utime
= 0;
3364 info
->array
.chunk_size
= 0;
3366 info
->disk
.major
= 0;
3367 info
->disk
.minor
= 0;
3368 info
->disk
.raid_disk
= -1;
3369 info
->reshape_active
= 0;
3370 info
->array
.major_version
= -1;
3371 info
->array
.minor_version
= -2;
3372 strcpy(info
->text_version
, "imsm");
3373 info
->safe_mode_delay
= 0;
3374 info
->disk
.number
= -1;
3375 info
->disk
.state
= 0;
3377 info
->recovery_start
= MaxSector
;
3378 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
3379 info
->bb
.supported
= 1;
3381 /* do we have the all the insync disks that we expect? */
3382 mpb
= super
->anchor
;
3383 info
->events
= __le32_to_cpu(mpb
->generation_num
);
3385 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3386 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3387 int failed
, enough
, j
, missing
= 0;
3388 struct imsm_map
*map
;
3391 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3392 state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3393 map
= get_imsm_map(dev
, MAP_0
);
3395 /* any newly missing disks?
3396 * (catches single-degraded vs double-degraded)
3398 for (j
= 0; j
< map
->num_members
; j
++) {
3399 __u32 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
3400 __u32 idx
= ord_to_idx(ord
);
3402 if (!(ord
& IMSM_ORD_REBUILD
) &&
3403 get_imsm_missing(super
, idx
)) {
3409 if (state
== IMSM_T_STATE_FAILED
)
3411 else if (state
== IMSM_T_STATE_DEGRADED
&&
3412 (state
!= map
->map_state
|| missing
))
3414 else /* we're normal, or already degraded */
3416 if (is_gen_migration(dev
) && missing
) {
3417 /* during general migration we need all disks
3418 * that process is running on.
3419 * No new missing disk is allowed.
3423 /* no more checks necessary
3427 /* in the missing/failed disk case check to see
3428 * if at least one array is runnable
3430 max_enough
= max(max_enough
, enough
);
3432 dprintf("enough: %d\n", max_enough
);
3433 info
->container_enough
= max_enough
;
3436 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
3438 disk
= &super
->disks
->disk
;
3439 info
->data_offset
= total_blocks(&super
->disks
->disk
) - reserved
;
3440 info
->component_size
= reserved
;
3441 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
3442 /* we don't change info->disk.raid_disk here because
3443 * this state will be finalized in mdmon after we have
3444 * found the 'most fresh' version of the metadata
3446 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3447 info
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
3450 /* only call uuid_from_super_imsm when this disk is part of a populated container,
3451 * ->compare_super may have updated the 'num_raid_devs' field for spares
3453 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
3454 uuid_from_super_imsm(st
, info
->uuid
);
3456 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
3458 /* I don't know how to compute 'map' on imsm, so use safe default */
3461 for (i
= 0; i
< map_disks
; i
++)
3467 /* allocates memory and fills disk in mdinfo structure
3468 * for each disk in array */
3469 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
3471 struct mdinfo
*mddev
;
3472 struct intel_super
*super
= st
->sb
;
3473 struct imsm_disk
*disk
;
3476 if (!super
|| !super
->disks
)
3479 mddev
= xcalloc(1, sizeof(*mddev
));
3483 tmp
= xcalloc(1, sizeof(*tmp
));
3485 tmp
->next
= mddev
->devs
;
3487 tmp
->disk
.number
= count
++;
3488 tmp
->disk
.major
= dl
->major
;
3489 tmp
->disk
.minor
= dl
->minor
;
3490 tmp
->disk
.state
= is_configured(disk
) ?
3491 (1 << MD_DISK_ACTIVE
) : 0;
3492 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3493 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
3494 tmp
->disk
.raid_disk
= -1;
3500 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
3501 char *update
, char *devname
, int verbose
,
3502 int uuid_set
, char *homehost
)
3504 /* For 'assemble' and 'force' we need to return non-zero if any
3505 * change was made. For others, the return value is ignored.
3506 * Update options are:
3507 * force-one : This device looks a bit old but needs to be included,
3508 * update age info appropriately.
3509 * assemble: clear any 'faulty' flag to allow this device to
3511 * force-array: Array is degraded but being forced, mark it clean
3512 * if that will be needed to assemble it.
3514 * newdev: not used ????
3515 * grow: Array has gained a new device - this is currently for
3517 * resync: mark as dirty so a resync will happen.
3518 * name: update the name - preserving the homehost
3519 * uuid: Change the uuid of the array to match watch is given
3521 * Following are not relevant for this imsm:
3522 * sparc2.2 : update from old dodgey metadata
3523 * super-minor: change the preferred_minor number
3524 * summaries: update redundant counters.
3525 * homehost: update the recorded homehost
3526 * _reshape_progress: record new reshape_progress position.
3529 struct intel_super
*super
= st
->sb
;
3530 struct imsm_super
*mpb
;
3532 /* we can only update container info */
3533 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
3536 mpb
= super
->anchor
;
3538 if (strcmp(update
, "uuid") == 0) {
3539 /* We take this to mean that the family_num should be updated.
3540 * However that is much smaller than the uuid so we cannot really
3541 * allow an explicit uuid to be given. And it is hard to reliably
3543 * So if !uuid_set we know the current uuid is random and just used
3544 * the first 'int' and copy it to the other 3 positions.
3545 * Otherwise we require the 4 'int's to be the same as would be the
3546 * case if we are using a random uuid. So an explicit uuid will be
3547 * accepted as long as all for ints are the same... which shouldn't hurt
3550 info
->uuid
[1] = info
->uuid
[2] = info
->uuid
[3] = info
->uuid
[0];
3553 if (info
->uuid
[0] != info
->uuid
[1] ||
3554 info
->uuid
[1] != info
->uuid
[2] ||
3555 info
->uuid
[2] != info
->uuid
[3])
3561 mpb
->orig_family_num
= info
->uuid
[0];
3562 } else if (strcmp(update
, "assemble") == 0)
3567 /* successful update? recompute checksum */
3569 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
3574 static size_t disks_to_mpb_size(int disks
)
3578 size
= sizeof(struct imsm_super
);
3579 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
3580 size
+= 2 * sizeof(struct imsm_dev
);
3581 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
3582 size
+= (4 - 2) * sizeof(struct imsm_map
);
3583 /* 4 possible disk_ord_tbl's */
3584 size
+= 4 * (disks
- 1) * sizeof(__u32
);
3585 /* maximum bbm log */
3586 size
+= sizeof(struct bbm_log
);
3591 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
,
3592 unsigned long long data_offset
)
3594 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
3597 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
3600 static void free_devlist(struct intel_super
*super
)
3602 struct intel_dev
*dv
;
3604 while (super
->devlist
) {
3605 dv
= super
->devlist
->next
;
3606 free(super
->devlist
->dev
);
3607 free(super
->devlist
);
3608 super
->devlist
= dv
;
3612 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
3614 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
3617 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
3621 * 0 same, or first was empty, and second was copied
3622 * 1 second had wrong number
3624 * 3 wrong other info
3626 struct intel_super
*first
= st
->sb
;
3627 struct intel_super
*sec
= tst
->sb
;
3634 /* in platform dependent environment test if the disks
3635 * use the same Intel hba
3636 * If not on Intel hba at all, allow anything.
3638 if (!check_env("IMSM_NO_PLATFORM") && first
->hba
&& sec
->hba
) {
3639 if (first
->hba
->type
!= sec
->hba
->type
) {
3641 "HBAs of devices do not match %s != %s\n",
3642 get_sys_dev_type(first
->hba
->type
),
3643 get_sys_dev_type(sec
->hba
->type
));
3646 if (first
->orom
!= sec
->orom
) {
3648 "HBAs of devices do not match %s != %s\n",
3649 first
->hba
->pci_id
, sec
->hba
->pci_id
);
3654 /* if an anchor does not have num_raid_devs set then it is a free
3657 if (first
->anchor
->num_raid_devs
> 0 &&
3658 sec
->anchor
->num_raid_devs
> 0) {
3659 /* Determine if these disks might ever have been
3660 * related. Further disambiguation can only take place
3661 * in load_super_imsm_all
3663 __u32 first_family
= first
->anchor
->orig_family_num
;
3664 __u32 sec_family
= sec
->anchor
->orig_family_num
;
3666 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
3667 MAX_SIGNATURE_LENGTH
) != 0)
3670 if (first_family
== 0)
3671 first_family
= first
->anchor
->family_num
;
3672 if (sec_family
== 0)
3673 sec_family
= sec
->anchor
->family_num
;
3675 if (first_family
!= sec_family
)
3680 /* if 'first' is a spare promote it to a populated mpb with sec's
3683 if (first
->anchor
->num_raid_devs
== 0 &&
3684 sec
->anchor
->num_raid_devs
> 0) {
3686 struct intel_dev
*dv
;
3687 struct imsm_dev
*dev
;
3689 /* we need to copy raid device info from sec if an allocation
3690 * fails here we don't associate the spare
3692 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
3693 dv
= xmalloc(sizeof(*dv
));
3694 dev
= xmalloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
3697 dv
->next
= first
->devlist
;
3698 first
->devlist
= dv
;
3700 if (i
< sec
->anchor
->num_raid_devs
) {
3701 /* allocation failure */
3702 free_devlist(first
);
3703 pr_err("imsm: failed to associate spare\n");
3706 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
3707 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
3708 first
->anchor
->family_num
= sec
->anchor
->family_num
;
3709 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
3710 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
3711 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
3717 static void fd2devname(int fd
, char *name
)
3721 char dname
[PATH_MAX
];
3726 if (fstat(fd
, &st
) != 0)
3728 sprintf(path
, "/sys/dev/block/%d:%d",
3729 major(st
.st_rdev
), minor(st
.st_rdev
));
3731 rv
= readlink(path
, dname
, sizeof(dname
)-1);
3736 nm
= strrchr(dname
, '/');
3739 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
3743 static int nvme_get_serial(int fd
, void *buf
, size_t buf_len
)
3746 char *name
= fd2kname(fd
);
3751 if (strncmp(name
, "nvme", 4) != 0)
3754 snprintf(path
, sizeof(path
) - 1, "/sys/block/%s/device/serial", name
);
3756 return load_sys(path
, buf
, buf_len
);
3759 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
3761 static int imsm_read_serial(int fd
, char *devname
,
3762 __u8 serial
[MAX_RAID_SERIAL_LEN
])
3771 memset(buf
, 0, sizeof(buf
));
3773 rv
= nvme_get_serial(fd
, buf
, sizeof(buf
));
3776 rv
= scsi_get_serial(fd
, buf
, sizeof(buf
));
3778 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
3779 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3780 fd2devname(fd
, (char *) serial
);
3786 pr_err("Failed to retrieve serial for %s\n",
3791 /* trim all whitespace and non-printable characters and convert
3794 for (i
= 0, dest
= buf
; i
< sizeof(buf
) && buf
[i
]; i
++) {
3797 /* ':' is reserved for use in placeholder serial
3798 * numbers for missing disks
3809 /* truncate leading characters */
3810 if (len
> MAX_RAID_SERIAL_LEN
) {
3811 dest
+= len
- MAX_RAID_SERIAL_LEN
;
3812 len
= MAX_RAID_SERIAL_LEN
;
3815 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3816 memcpy(serial
, dest
, len
);
3821 static int serialcmp(__u8
*s1
, __u8
*s2
)
3823 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
3826 static void serialcpy(__u8
*dest
, __u8
*src
)
3828 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
3831 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
3835 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3836 if (serialcmp(dl
->serial
, serial
) == 0)
3842 static struct imsm_disk
*
3843 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
3847 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3848 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3850 if (serialcmp(disk
->serial
, serial
) == 0) {
3861 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3863 struct imsm_disk
*disk
;
3868 __u8 serial
[MAX_RAID_SERIAL_LEN
];
3870 rv
= imsm_read_serial(fd
, devname
, serial
);
3875 dl
= xcalloc(1, sizeof(*dl
));
3878 dl
->major
= major(stb
.st_rdev
);
3879 dl
->minor
= minor(stb
.st_rdev
);
3880 dl
->next
= super
->disks
;
3881 dl
->fd
= keep_fd
? fd
: -1;
3882 assert(super
->disks
== NULL
);
3884 serialcpy(dl
->serial
, serial
);
3887 fd2devname(fd
, name
);
3889 dl
->devname
= xstrdup(devname
);
3891 dl
->devname
= xstrdup(name
);
3893 /* look up this disk's index in the current anchor */
3894 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
3897 /* only set index on disks that are a member of a
3898 * populated contianer, i.e. one with raid_devs
3900 if (is_failed(&dl
->disk
))
3902 else if (is_spare(&dl
->disk
))
3910 /* When migrating map0 contains the 'destination' state while map1
3911 * contains the current state. When not migrating map0 contains the
3912 * current state. This routine assumes that map[0].map_state is set to
3913 * the current array state before being called.
3915 * Migration is indicated by one of the following states
3916 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
3917 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
3918 * map1state=unitialized)
3919 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
3921 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
3922 * map1state=degraded)
3923 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
3926 static void migrate(struct imsm_dev
*dev
, struct intel_super
*super
,
3927 __u8 to_state
, int migr_type
)
3929 struct imsm_map
*dest
;
3930 struct imsm_map
*src
= get_imsm_map(dev
, MAP_0
);
3932 dev
->vol
.migr_state
= 1;
3933 set_migr_type(dev
, migr_type
);
3934 dev
->vol
.curr_migr_unit
= 0;
3935 dest
= get_imsm_map(dev
, MAP_1
);
3937 /* duplicate and then set the target end state in map[0] */
3938 memcpy(dest
, src
, sizeof_imsm_map(src
));
3939 if (migr_type
== MIGR_REBUILD
|| migr_type
== MIGR_GEN_MIGR
) {
3943 for (i
= 0; i
< src
->num_members
; i
++) {
3944 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
3945 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
3949 if (migr_type
== MIGR_GEN_MIGR
)
3950 /* Clear migration record */
3951 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
3953 src
->map_state
= to_state
;
3956 static void end_migration(struct imsm_dev
*dev
, struct intel_super
*super
,
3959 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3960 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
== 0 ?
3964 /* merge any IMSM_ORD_REBUILD bits that were not successfully
3965 * completed in the last migration.
3967 * FIXME add support for raid-level-migration
3969 if (map_state
!= map
->map_state
&& (is_gen_migration(dev
) == 0) &&
3970 prev
->map_state
!= IMSM_T_STATE_UNINITIALIZED
) {
3971 /* when final map state is other than expected
3972 * merge maps (not for migration)
3976 for (i
= 0; i
< prev
->num_members
; i
++)
3977 for (j
= 0; j
< map
->num_members
; j
++)
3978 /* during online capacity expansion
3979 * disks position can be changed
3980 * if takeover is used
3982 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
3983 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
3984 map
->disk_ord_tbl
[j
] |=
3985 prev
->disk_ord_tbl
[i
];
3988 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3989 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3992 dev
->vol
.migr_state
= 0;
3993 set_migr_type(dev
, 0);
3994 dev
->vol
.curr_migr_unit
= 0;
3995 map
->map_state
= map_state
;
3999 static int parse_raid_devices(struct intel_super
*super
)
4002 struct imsm_dev
*dev_new
;
4003 size_t len
, len_migr
;
4005 size_t space_needed
= 0;
4006 struct imsm_super
*mpb
= super
->anchor
;
4008 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
4009 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
4010 struct intel_dev
*dv
;
4012 len
= sizeof_imsm_dev(dev_iter
, 0);
4013 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
4015 space_needed
+= len_migr
- len
;
4017 dv
= xmalloc(sizeof(*dv
));
4018 if (max_len
< len_migr
)
4020 if (max_len
> len_migr
)
4021 space_needed
+= max_len
- len_migr
;
4022 dev_new
= xmalloc(max_len
);
4023 imsm_copy_dev(dev_new
, dev_iter
);
4026 dv
->next
= super
->devlist
;
4027 super
->devlist
= dv
;
4030 /* ensure that super->buf is large enough when all raid devices
4033 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
4036 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
,
4037 super
->sector_size
);
4038 if (posix_memalign(&buf
, MAX_SECTOR_SIZE
, len
) != 0)
4041 memcpy(buf
, super
->buf
, super
->len
);
4042 memset(buf
+ super
->len
, 0, len
- super
->len
);
4048 super
->extra_space
+= space_needed
;
4053 /*******************************************************************************
4054 * Function: check_mpb_migr_compatibility
4055 * Description: Function checks for unsupported migration features:
4056 * - migration optimization area (pba_of_lba0)
4057 * - descending reshape (ascending_migr)
4059 * super : imsm metadata information
4061 * 0 : migration is compatible
4062 * -1 : migration is not compatible
4063 ******************************************************************************/
4064 int check_mpb_migr_compatibility(struct intel_super
*super
)
4066 struct imsm_map
*map0
, *map1
;
4067 struct migr_record
*migr_rec
= super
->migr_rec
;
4070 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
4071 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
4074 dev_iter
->vol
.migr_state
== 1 &&
4075 dev_iter
->vol
.migr_type
== MIGR_GEN_MIGR
) {
4076 /* This device is migrating */
4077 map0
= get_imsm_map(dev_iter
, MAP_0
);
4078 map1
= get_imsm_map(dev_iter
, MAP_1
);
4079 if (pba_of_lba0(map0
) != pba_of_lba0(map1
))
4080 /* migration optimization area was used */
4082 if (migr_rec
->ascending_migr
== 0
4083 && migr_rec
->dest_depth_per_unit
> 0)
4084 /* descending reshape not supported yet */
4091 static void __free_imsm(struct intel_super
*super
, int free_disks
);
4093 /* load_imsm_mpb - read matrix metadata
4094 * allocates super->mpb to be freed by free_imsm
4096 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
4098 unsigned long long dsize
;
4099 unsigned long long sectors
;
4100 unsigned int sector_size
= super
->sector_size
;
4102 struct imsm_super
*anchor
;
4105 get_dev_size(fd
, NULL
, &dsize
);
4106 if (dsize
< 2*sector_size
) {
4108 pr_err("%s: device to small for imsm\n",
4113 if (lseek64(fd
, dsize
- (sector_size
* 2), SEEK_SET
) < 0) {
4115 pr_err("Cannot seek to anchor block on %s: %s\n",
4116 devname
, strerror(errno
));
4120 if (posix_memalign((void **)&anchor
, sector_size
, sector_size
) != 0) {
4122 pr_err("Failed to allocate imsm anchor buffer on %s\n", devname
);
4125 if ((unsigned int)read(fd
, anchor
, sector_size
) != sector_size
) {
4127 pr_err("Cannot read anchor block on %s: %s\n",
4128 devname
, strerror(errno
));
4133 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
4135 pr_err("no IMSM anchor on %s\n", devname
);
4140 __free_imsm(super
, 0);
4141 /* reload capability and hba */
4143 /* capability and hba must be updated with new super allocation */
4144 find_intel_hba_capability(fd
, super
, devname
);
4145 super
->len
= ROUND_UP(anchor
->mpb_size
, sector_size
);
4146 if (posix_memalign(&super
->buf
, MAX_SECTOR_SIZE
, super
->len
) != 0) {
4148 pr_err("unable to allocate %zu byte mpb buffer\n",
4153 memcpy(super
->buf
, anchor
, sector_size
);
4155 sectors
= mpb_sectors(anchor
, sector_size
) - 1;
4158 if (posix_memalign(&super
->migr_rec_buf
, sector_size
,
4159 MIGR_REC_BUF_SECTORS
*sector_size
) != 0) {
4160 pr_err("could not allocate migr_rec buffer\n");
4164 super
->clean_migration_record_by_mdmon
= 0;
4167 check_sum
= __gen_imsm_checksum(super
->anchor
);
4168 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
4170 pr_err("IMSM checksum %x != %x on %s\n",
4172 __le32_to_cpu(super
->anchor
->check_sum
),
4180 /* read the extended mpb */
4181 if (lseek64(fd
, dsize
- (sector_size
* (2 + sectors
)), SEEK_SET
) < 0) {
4183 pr_err("Cannot seek to extended mpb on %s: %s\n",
4184 devname
, strerror(errno
));
4188 if ((unsigned int)read(fd
, super
->buf
+ sector_size
,
4189 super
->len
- sector_size
) != super
->len
- sector_size
) {
4191 pr_err("Cannot read extended mpb on %s: %s\n",
4192 devname
, strerror(errno
));
4196 check_sum
= __gen_imsm_checksum(super
->anchor
);
4197 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
4199 pr_err("IMSM checksum %x != %x on %s\n",
4200 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
4208 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
);
4210 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
4211 static void clear_hi(struct intel_super
*super
)
4213 struct imsm_super
*mpb
= super
->anchor
;
4215 if (mpb
->attributes
& MPB_ATTRIB_2TB_DISK
)
4217 for (i
= 0; i
< mpb
->num_disks
; ++i
) {
4218 struct imsm_disk
*disk
= &mpb
->disk
[i
];
4219 disk
->total_blocks_hi
= 0;
4221 for (i
= 0; i
< mpb
->num_raid_devs
; ++i
) {
4222 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4225 for (n
= 0; n
< 2; ++n
) {
4226 struct imsm_map
*map
= get_imsm_map(dev
, n
);
4229 map
->pba_of_lba0_hi
= 0;
4230 map
->blocks_per_member_hi
= 0;
4231 map
->num_data_stripes_hi
= 0;
4237 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
4241 err
= load_imsm_mpb(fd
, super
, devname
);
4244 if (super
->sector_size
== 4096)
4245 convert_from_4k(super
);
4246 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
4249 err
= parse_raid_devices(super
);
4252 err
= load_bbm_log(super
);
4257 static void __free_imsm_disk(struct dl
*d
)
4269 static void free_imsm_disks(struct intel_super
*super
)
4273 while (super
->disks
) {
4275 super
->disks
= d
->next
;
4276 __free_imsm_disk(d
);
4278 while (super
->disk_mgmt_list
) {
4279 d
= super
->disk_mgmt_list
;
4280 super
->disk_mgmt_list
= d
->next
;
4281 __free_imsm_disk(d
);
4283 while (super
->missing
) {
4285 super
->missing
= d
->next
;
4286 __free_imsm_disk(d
);
4291 /* free all the pieces hanging off of a super pointer */
4292 static void __free_imsm(struct intel_super
*super
, int free_disks
)
4294 struct intel_hba
*elem
, *next
;
4300 /* unlink capability description */
4302 if (super
->migr_rec_buf
) {
4303 free(super
->migr_rec_buf
);
4304 super
->migr_rec_buf
= NULL
;
4307 free_imsm_disks(super
);
4308 free_devlist(super
);
4312 free((void *)elem
->path
);
4318 free(super
->bbm_log
);
4322 static void free_imsm(struct intel_super
*super
)
4324 __free_imsm(super
, 1);
4325 free(super
->bb
.entries
);
4329 static void free_super_imsm(struct supertype
*st
)
4331 struct intel_super
*super
= st
->sb
;
4340 static struct intel_super
*alloc_super(void)
4342 struct intel_super
*super
= xcalloc(1, sizeof(*super
));
4344 super
->current_vol
= -1;
4345 super
->create_offset
= ~((unsigned long long) 0);
4347 super
->bb
.entries
= xmalloc(BBM_LOG_MAX_ENTRIES
*
4348 sizeof(struct md_bb_entry
));
4349 if (!super
->bb
.entries
) {
4358 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
4360 static int find_intel_hba_capability(int fd
, struct intel_super
*super
, char *devname
)
4362 struct sys_dev
*hba_name
;
4365 if (fd
< 0 || check_env("IMSM_NO_PLATFORM")) {
4370 hba_name
= find_disk_attached_hba(fd
, NULL
);
4373 pr_err("%s is not attached to Intel(R) RAID controller.\n",
4377 rv
= attach_hba_to_super(super
, hba_name
);
4380 struct intel_hba
*hba
= super
->hba
;
4382 pr_err("%s is attached to Intel(R) %s %s (%s),\n"
4383 " but the container is assigned to Intel(R) %s %s (",
4385 get_sys_dev_type(hba_name
->type
),
4386 hba_name
->type
== SYS_DEV_VMD
? "domain" : "RAID controller",
4387 hba_name
->pci_id
? : "Err!",
4388 get_sys_dev_type(super
->hba
->type
),
4389 hba
->type
== SYS_DEV_VMD
? "domain" : "RAID controller");
4392 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
4394 fprintf(stderr
, ", ");
4397 fprintf(stderr
, ").\n"
4398 " Mixing devices attached to different %s is not allowed.\n",
4399 hba_name
->type
== SYS_DEV_VMD
? "VMD domains" : "controllers");
4403 super
->orom
= find_imsm_capability(hba_name
);
4410 /* find_missing - helper routine for load_super_imsm_all that identifies
4411 * disks that have disappeared from the system. This routine relies on
4412 * the mpb being uptodate, which it is at load time.
4414 static int find_missing(struct intel_super
*super
)
4417 struct imsm_super
*mpb
= super
->anchor
;
4419 struct imsm_disk
*disk
;
4421 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4422 disk
= __get_imsm_disk(mpb
, i
);
4423 dl
= serial_to_dl(disk
->serial
, super
);
4427 dl
= xmalloc(sizeof(*dl
));
4431 dl
->devname
= xstrdup("missing");
4433 serialcpy(dl
->serial
, disk
->serial
);
4436 dl
->next
= super
->missing
;
4437 super
->missing
= dl
;
4444 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
4446 struct intel_disk
*idisk
= disk_list
;
4449 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
4451 idisk
= idisk
->next
;
4457 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
4458 struct intel_super
*super
,
4459 struct intel_disk
**disk_list
)
4461 struct imsm_disk
*d
= &super
->disks
->disk
;
4462 struct imsm_super
*mpb
= super
->anchor
;
4465 for (i
= 0; i
< tbl_size
; i
++) {
4466 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
4467 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
4469 if (tbl_mpb
->family_num
== mpb
->family_num
) {
4470 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
4471 dprintf("mpb from %d:%d matches %d:%d\n",
4472 super
->disks
->major
,
4473 super
->disks
->minor
,
4474 table
[i
]->disks
->major
,
4475 table
[i
]->disks
->minor
);
4479 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
4480 is_configured(d
) == is_configured(tbl_d
)) &&
4481 tbl_mpb
->generation_num
< mpb
->generation_num
) {
4482 /* current version of the mpb is a
4483 * better candidate than the one in
4484 * super_table, but copy over "cross
4485 * generational" status
4487 struct intel_disk
*idisk
;
4489 dprintf("mpb from %d:%d replaces %d:%d\n",
4490 super
->disks
->major
,
4491 super
->disks
->minor
,
4492 table
[i
]->disks
->major
,
4493 table
[i
]->disks
->minor
);
4495 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
4496 if (idisk
&& is_failed(&idisk
->disk
))
4497 tbl_d
->status
|= FAILED_DISK
;
4500 struct intel_disk
*idisk
;
4501 struct imsm_disk
*disk
;
4503 /* tbl_mpb is more up to date, but copy
4504 * over cross generational status before
4507 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
4508 if (disk
&& is_failed(disk
))
4509 d
->status
|= FAILED_DISK
;
4511 idisk
= disk_list_get(d
->serial
, *disk_list
);
4514 if (disk
&& is_configured(disk
))
4515 idisk
->disk
.status
|= CONFIGURED_DISK
;
4518 dprintf("mpb from %d:%d prefer %d:%d\n",
4519 super
->disks
->major
,
4520 super
->disks
->minor
,
4521 table
[i
]->disks
->major
,
4522 table
[i
]->disks
->minor
);
4530 table
[tbl_size
++] = super
;
4534 /* update/extend the merged list of imsm_disk records */
4535 for (j
= 0; j
< mpb
->num_disks
; j
++) {
4536 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
4537 struct intel_disk
*idisk
;
4539 idisk
= disk_list_get(disk
->serial
, *disk_list
);
4541 idisk
->disk
.status
|= disk
->status
;
4542 if (is_configured(&idisk
->disk
) ||
4543 is_failed(&idisk
->disk
))
4544 idisk
->disk
.status
&= ~(SPARE_DISK
);
4546 idisk
= xcalloc(1, sizeof(*idisk
));
4547 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
4548 idisk
->disk
= *disk
;
4549 idisk
->next
= *disk_list
;
4553 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
4560 static struct intel_super
*
4561 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
4564 struct imsm_super
*mpb
= super
->anchor
;
4568 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4569 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
4570 struct intel_disk
*idisk
;
4572 idisk
= disk_list_get(disk
->serial
, disk_list
);
4574 if (idisk
->owner
== owner
||
4575 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
4578 dprintf("'%.16s' owner %d != %d\n",
4579 disk
->serial
, idisk
->owner
,
4582 dprintf("unknown disk %x [%d]: %.16s\n",
4583 __le32_to_cpu(mpb
->family_num
), i
,
4589 if (ok_count
== mpb
->num_disks
)
4594 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
4596 struct intel_super
*s
;
4598 for (s
= super_list
; s
; s
= s
->next
) {
4599 if (family_num
!= s
->anchor
->family_num
)
4601 pr_err("Conflict, offlining family %#x on '%s'\n",
4602 __le32_to_cpu(family_num
), s
->disks
->devname
);
4606 static struct intel_super
*
4607 imsm_thunderdome(struct intel_super
**super_list
, int len
)
4609 struct intel_super
*super_table
[len
];
4610 struct intel_disk
*disk_list
= NULL
;
4611 struct intel_super
*champion
, *spare
;
4612 struct intel_super
*s
, **del
;
4617 memset(super_table
, 0, sizeof(super_table
));
4618 for (s
= *super_list
; s
; s
= s
->next
)
4619 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
4621 for (i
= 0; i
< tbl_size
; i
++) {
4622 struct imsm_disk
*d
;
4623 struct intel_disk
*idisk
;
4624 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
4627 d
= &s
->disks
->disk
;
4629 /* 'd' must appear in merged disk list for its
4630 * configuration to be valid
4632 idisk
= disk_list_get(d
->serial
, disk_list
);
4633 if (idisk
&& idisk
->owner
== i
)
4634 s
= validate_members(s
, disk_list
, i
);
4639 dprintf("marking family: %#x from %d:%d offline\n",
4641 super_table
[i
]->disks
->major
,
4642 super_table
[i
]->disks
->minor
);
4646 /* This is where the mdadm implementation differs from the Windows
4647 * driver which has no strict concept of a container. We can only
4648 * assemble one family from a container, so when returning a prodigal
4649 * array member to this system the code will not be able to disambiguate
4650 * the container contents that should be assembled ("foreign" versus
4651 * "local"). It requires user intervention to set the orig_family_num
4652 * to a new value to establish a new container. The Windows driver in
4653 * this situation fixes up the volume name in place and manages the
4654 * foreign array as an independent entity.
4659 for (i
= 0; i
< tbl_size
; i
++) {
4660 struct intel_super
*tbl_ent
= super_table
[i
];
4666 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
4671 if (s
&& !is_spare
) {
4672 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
4674 } else if (!s
&& !is_spare
)
4687 pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n",
4688 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
4690 /* collect all dl's onto 'champion', and update them to
4691 * champion's version of the status
4693 for (s
= *super_list
; s
; s
= s
->next
) {
4694 struct imsm_super
*mpb
= champion
->anchor
;
4695 struct dl
*dl
= s
->disks
;
4700 mpb
->attributes
|= s
->anchor
->attributes
& MPB_ATTRIB_2TB_DISK
;
4702 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4703 struct imsm_disk
*disk
;
4705 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
4708 /* only set index on disks that are a member of
4709 * a populated contianer, i.e. one with
4712 if (is_failed(&dl
->disk
))
4714 else if (is_spare(&dl
->disk
))
4720 if (i
>= mpb
->num_disks
) {
4721 struct intel_disk
*idisk
;
4723 idisk
= disk_list_get(dl
->serial
, disk_list
);
4724 if (idisk
&& is_spare(&idisk
->disk
) &&
4725 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
4733 dl
->next
= champion
->disks
;
4734 champion
->disks
= dl
;
4738 /* delete 'champion' from super_list */
4739 for (del
= super_list
; *del
; ) {
4740 if (*del
== champion
) {
4741 *del
= (*del
)->next
;
4744 del
= &(*del
)->next
;
4746 champion
->next
= NULL
;
4750 struct intel_disk
*idisk
= disk_list
;
4752 disk_list
= disk_list
->next
;
4760 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
);
4761 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
4762 int major
, int minor
, int keep_fd
);
4764 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4765 int *max
, int keep_fd
);
4767 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
4768 char *devname
, struct md_list
*devlist
,
4771 struct intel_super
*super_list
= NULL
;
4772 struct intel_super
*super
= NULL
;
4777 /* 'fd' is an opened container */
4778 err
= get_sra_super_block(fd
, &super_list
, devname
, &i
, keep_fd
);
4780 /* get super block from devlist devices */
4781 err
= get_devlist_super_block(devlist
, &super_list
, &i
, keep_fd
);
4784 /* all mpbs enter, maybe one leaves */
4785 super
= imsm_thunderdome(&super_list
, i
);
4791 if (find_missing(super
) != 0) {
4797 /* load migration record */
4798 err
= load_imsm_migr_rec(super
, NULL
);
4800 /* migration is in progress,
4801 * but migr_rec cannot be loaded,
4807 /* Check migration compatibility */
4808 if (err
== 0 && check_mpb_migr_compatibility(super
) != 0) {
4809 pr_err("Unsupported migration detected");
4811 fprintf(stderr
, " on %s\n", devname
);
4813 fprintf(stderr
, " (IMSM).\n");
4822 while (super_list
) {
4823 struct intel_super
*s
= super_list
;
4825 super_list
= super_list
->next
;
4834 strcpy(st
->container_devnm
, fd2devnm(fd
));
4836 st
->container_devnm
[0] = 0;
4837 if (err
== 0 && st
->ss
== NULL
) {
4838 st
->ss
= &super_imsm
;
4839 st
->minor_version
= 0;
4840 st
->max_devs
= IMSM_MAX_DEVICES
;
4846 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4847 int *max
, int keep_fd
)
4849 struct md_list
*tmpdev
;
4853 for (i
= 0, tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
4854 if (tmpdev
->used
!= 1)
4856 if (tmpdev
->container
== 1) {
4858 int fd
= dev_open(tmpdev
->devname
, O_RDONLY
|O_EXCL
);
4860 pr_err("cannot open device %s: %s\n",
4861 tmpdev
->devname
, strerror(errno
));
4865 err
= get_sra_super_block(fd
, super_list
,
4866 tmpdev
->devname
, &lmax
,
4875 int major
= major(tmpdev
->st_rdev
);
4876 int minor
= minor(tmpdev
->st_rdev
);
4877 err
= get_super_block(super_list
,
4894 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
4895 int major
, int minor
, int keep_fd
)
4897 struct intel_super
*s
;
4909 sprintf(nm
, "%d:%d", major
, minor
);
4910 dfd
= dev_open(nm
, O_RDWR
);
4916 get_dev_sector_size(dfd
, NULL
, &s
->sector_size
);
4917 find_intel_hba_capability(dfd
, s
, devname
);
4918 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4920 /* retry the load if we might have raced against mdmon */
4921 if (err
== 3 && devnm
&& mdmon_running(devnm
))
4922 for (retry
= 0; retry
< 3; retry
++) {
4924 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4930 s
->next
= *super_list
;
4938 if (dfd
>= 0 && !keep_fd
)
4945 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
)
4952 sra
= sysfs_read(fd
, NULL
, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
4956 if (sra
->array
.major_version
!= -1 ||
4957 sra
->array
.minor_version
!= -2 ||
4958 strcmp(sra
->text_version
, "imsm") != 0) {
4963 devnm
= fd2devnm(fd
);
4964 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
4965 if (get_super_block(super_list
, devnm
, devname
,
4966 sd
->disk
.major
, sd
->disk
.minor
, keep_fd
) != 0) {
4977 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
4979 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
, NULL
, 1);
4983 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
4985 struct intel_super
*super
;
4989 if (test_partition(fd
))
4990 /* IMSM not allowed on partitions */
4993 free_super_imsm(st
);
4995 super
= alloc_super();
4996 get_dev_sector_size(fd
, NULL
, &super
->sector_size
);
4999 /* Load hba and capabilities if they exist.
5000 * But do not preclude loading metadata in case capabilities or hba are
5001 * non-compliant and ignore_hw_compat is set.
5003 rv
= find_intel_hba_capability(fd
, super
, devname
);
5004 /* no orom/efi or non-intel hba of the disk */
5005 if (rv
!= 0 && st
->ignore_hw_compat
== 0) {
5007 pr_err("No OROM/EFI properties for %s\n", devname
);
5011 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
5013 /* retry the load if we might have raced against mdmon */
5015 struct mdstat_ent
*mdstat
= NULL
;
5016 char *name
= fd2kname(fd
);
5019 mdstat
= mdstat_by_component(name
);
5021 if (mdstat
&& mdmon_running(mdstat
->devnm
) && getpid() != mdmon_pid(mdstat
->devnm
)) {
5022 for (retry
= 0; retry
< 3; retry
++) {
5024 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
5030 free_mdstat(mdstat
);
5035 pr_err("Failed to load all information sections on %s\n", devname
);
5041 if (st
->ss
== NULL
) {
5042 st
->ss
= &super_imsm
;
5043 st
->minor_version
= 0;
5044 st
->max_devs
= IMSM_MAX_DEVICES
;
5047 /* load migration record */
5048 if (load_imsm_migr_rec(super
, NULL
) == 0) {
5049 /* Check for unsupported migration features */
5050 if (check_mpb_migr_compatibility(super
) != 0) {
5051 pr_err("Unsupported migration detected");
5053 fprintf(stderr
, " on %s\n", devname
);
5055 fprintf(stderr
, " (IMSM).\n");
5063 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
5065 if (info
->level
== 1)
5067 return info
->chunk_size
>> 9;
5070 static unsigned long long info_to_blocks_per_member(mdu_array_info_t
*info
,
5071 unsigned long long size
)
5073 if (info
->level
== 1)
5076 return (size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
5079 static void imsm_update_version_info(struct intel_super
*super
)
5081 /* update the version and attributes */
5082 struct imsm_super
*mpb
= super
->anchor
;
5084 struct imsm_dev
*dev
;
5085 struct imsm_map
*map
;
5088 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5089 dev
= get_imsm_dev(super
, i
);
5090 map
= get_imsm_map(dev
, MAP_0
);
5091 if (__le32_to_cpu(dev
->size_high
) > 0)
5092 mpb
->attributes
|= MPB_ATTRIB_2TB
;
5094 /* FIXME detect when an array spans a port multiplier */
5096 mpb
->attributes
|= MPB_ATTRIB_PM
;
5099 if (mpb
->num_raid_devs
> 1 ||
5100 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
5101 version
= MPB_VERSION_ATTRIBS
;
5102 switch (get_imsm_raid_level(map
)) {
5103 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
5104 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
5105 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
5106 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
5109 if (map
->num_members
>= 5)
5110 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
5111 else if (dev
->status
== DEV_CLONE_N_GO
)
5112 version
= MPB_VERSION_CNG
;
5113 else if (get_imsm_raid_level(map
) == 5)
5114 version
= MPB_VERSION_RAID5
;
5115 else if (map
->num_members
>= 3)
5116 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
5117 else if (get_imsm_raid_level(map
) == 1)
5118 version
= MPB_VERSION_RAID1
;
5120 version
= MPB_VERSION_RAID0
;
5122 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
5126 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
5128 struct imsm_super
*mpb
= super
->anchor
;
5129 char *reason
= NULL
;
5132 if (strlen(name
) > MAX_RAID_SERIAL_LEN
)
5133 reason
= "must be 16 characters or less";
5135 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5136 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
5138 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
5139 reason
= "already exists";
5144 if (reason
&& !quiet
)
5145 pr_err("imsm volume name %s\n", reason
);
5150 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
5151 unsigned long long size
, char *name
,
5152 char *homehost
, int *uuid
,
5153 long long data_offset
)
5155 /* We are creating a volume inside a pre-existing container.
5156 * so st->sb is already set.
5158 struct intel_super
*super
= st
->sb
;
5159 unsigned int sector_size
= super
->sector_size
;
5160 struct imsm_super
*mpb
= super
->anchor
;
5161 struct intel_dev
*dv
;
5162 struct imsm_dev
*dev
;
5163 struct imsm_vol
*vol
;
5164 struct imsm_map
*map
;
5165 int idx
= mpb
->num_raid_devs
;
5167 unsigned long long array_blocks
;
5168 size_t size_old
, size_new
;
5169 unsigned long long num_data_stripes
;
5171 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
5172 pr_err("This imsm-container already has the maximum of %d volumes\n", super
->orom
->vpa
);
5176 /* ensure the mpb is large enough for the new data */
5177 size_old
= __le32_to_cpu(mpb
->mpb_size
);
5178 size_new
= disks_to_mpb_size(info
->nr_disks
);
5179 if (size_new
> size_old
) {
5181 size_t size_round
= ROUND_UP(size_new
, sector_size
);
5183 if (posix_memalign(&mpb_new
, sector_size
, size_round
) != 0) {
5184 pr_err("could not allocate new mpb\n");
5187 if (posix_memalign(&super
->migr_rec_buf
, sector_size
,
5188 MIGR_REC_BUF_SECTORS
*sector_size
) != 0) {
5189 pr_err("could not allocate migr_rec buffer\n");
5195 memcpy(mpb_new
, mpb
, size_old
);
5198 super
->anchor
= mpb_new
;
5199 mpb
->mpb_size
= __cpu_to_le32(size_new
);
5200 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
5201 super
->len
= size_round
;
5203 super
->current_vol
= idx
;
5205 /* handle 'failed_disks' by either:
5206 * a) create dummy disk entries in the table if this the first
5207 * volume in the array. We add them here as this is the only
5208 * opportunity to add them. add_to_super_imsm_volume()
5209 * handles the non-failed disks and continues incrementing
5211 * b) validate that 'failed_disks' matches the current number
5212 * of missing disks if the container is populated
5214 if (super
->current_vol
== 0) {
5216 for (i
= 0; i
< info
->failed_disks
; i
++) {
5217 struct imsm_disk
*disk
;
5220 disk
= __get_imsm_disk(mpb
, i
);
5221 disk
->status
= CONFIGURED_DISK
| FAILED_DISK
;
5222 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
5223 snprintf((char *) disk
->serial
, MAX_RAID_SERIAL_LEN
,
5226 find_missing(super
);
5231 for (d
= super
->missing
; d
; d
= d
->next
)
5233 if (info
->failed_disks
> missing
) {
5234 pr_err("unable to add 'missing' disk to container\n");
5239 if (!check_name(super
, name
, 0))
5241 dv
= xmalloc(sizeof(*dv
));
5242 dev
= xcalloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
5243 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
5244 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
5245 info
->layout
, info
->chunk_size
,
5247 /* round array size down to closest MB */
5248 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
5250 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
5251 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
5252 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
5254 vol
->migr_state
= 0;
5255 set_migr_type(dev
, MIGR_INIT
);
5256 vol
->dirty
= !info
->state
;
5257 vol
->curr_migr_unit
= 0;
5258 map
= get_imsm_map(dev
, MAP_0
);
5259 set_pba_of_lba0(map
, super
->create_offset
);
5260 set_blocks_per_member(map
, info_to_blocks_per_member(info
, size
));
5261 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
5262 map
->failed_disk_num
= ~0;
5263 if (info
->level
> 0)
5264 map
->map_state
= (info
->state
? IMSM_T_STATE_NORMAL
5265 : IMSM_T_STATE_UNINITIALIZED
);
5267 map
->map_state
= info
->failed_disks
? IMSM_T_STATE_FAILED
:
5268 IMSM_T_STATE_NORMAL
;
5271 if (info
->level
== 1 && info
->raid_disks
> 2) {
5274 pr_err("imsm does not support more than 2 disksin a raid1 volume\n");
5278 map
->raid_level
= info
->level
;
5279 if (info
->level
== 10) {
5280 map
->raid_level
= 1;
5281 map
->num_domains
= info
->raid_disks
/ 2;
5282 } else if (info
->level
== 1)
5283 map
->num_domains
= info
->raid_disks
;
5285 map
->num_domains
= 1;
5287 /* info->size is only int so use the 'size' parameter instead */
5288 num_data_stripes
= (size
* 2) / info_to_blocks_per_strip(info
);
5289 num_data_stripes
/= map
->num_domains
;
5290 set_num_data_stripes(map
, num_data_stripes
);
5292 map
->num_members
= info
->raid_disks
;
5293 for (i
= 0; i
< map
->num_members
; i
++) {
5294 /* initialized in add_to_super */
5295 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
5297 mpb
->num_raid_devs
++;
5300 dv
->index
= super
->current_vol
;
5301 dv
->next
= super
->devlist
;
5302 super
->devlist
= dv
;
5304 imsm_update_version_info(super
);
5309 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
5310 unsigned long long size
, char *name
,
5311 char *homehost
, int *uuid
,
5312 unsigned long long data_offset
)
5314 /* This is primarily called by Create when creating a new array.
5315 * We will then get add_to_super called for each component, and then
5316 * write_init_super called to write it out to each device.
5317 * For IMSM, Create can create on fresh devices or on a pre-existing
5319 * To create on a pre-existing array a different method will be called.
5320 * This one is just for fresh drives.
5322 struct intel_super
*super
;
5323 struct imsm_super
*mpb
;
5327 if (data_offset
!= INVALID_SECTORS
) {
5328 pr_err("data-offset not supported by imsm\n");
5333 return init_super_imsm_volume(st
, info
, size
, name
, homehost
, uuid
,
5337 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
5339 mpb_size
= MAX_SECTOR_SIZE
;
5341 super
= alloc_super();
5343 posix_memalign(&super
->buf
, MAX_SECTOR_SIZE
, mpb_size
) != 0) {
5348 pr_err("could not allocate superblock\n");
5351 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
5352 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
) != 0) {
5353 pr_err("could not allocate migr_rec buffer\n");
5358 memset(super
->buf
, 0, mpb_size
);
5360 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5364 /* zeroing superblock */
5368 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5370 version
= (char *) mpb
->sig
;
5371 strcpy(version
, MPB_SIGNATURE
);
5372 version
+= strlen(MPB_SIGNATURE
);
5373 strcpy(version
, MPB_VERSION_RAID0
);
5379 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
5380 int fd
, char *devname
)
5382 struct intel_super
*super
= st
->sb
;
5383 struct imsm_super
*mpb
= super
->anchor
;
5384 struct imsm_disk
*_disk
;
5385 struct imsm_dev
*dev
;
5386 struct imsm_map
*map
;
5390 dev
= get_imsm_dev(super
, super
->current_vol
);
5391 map
= get_imsm_map(dev
, MAP_0
);
5393 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
5394 pr_err("%s: Cannot add spare devices to IMSM volume\n",
5400 /* we're doing autolayout so grab the pre-marked (in
5401 * validate_geometry) raid_disk
5403 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5404 if (dl
->raiddisk
== dk
->raid_disk
)
5407 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5408 if (dl
->major
== dk
->major
&&
5409 dl
->minor
== dk
->minor
)
5414 pr_err("%s is not a member of the same container\n", devname
);
5418 /* add a pristine spare to the metadata */
5419 if (dl
->index
< 0) {
5420 dl
->index
= super
->anchor
->num_disks
;
5421 super
->anchor
->num_disks
++;
5423 /* Check the device has not already been added */
5424 slot
= get_imsm_disk_slot(map
, dl
->index
);
5426 (get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
) & IMSM_ORD_REBUILD
) == 0) {
5427 pr_err("%s has been included in this array twice\n",
5431 set_imsm_ord_tbl_ent(map
, dk
->raid_disk
, dl
->index
);
5432 dl
->disk
.status
= CONFIGURED_DISK
;
5434 /* update size of 'missing' disks to be at least as large as the
5435 * largest acitve member (we only have dummy missing disks when
5436 * creating the first volume)
5438 if (super
->current_vol
== 0) {
5439 for (df
= super
->missing
; df
; df
= df
->next
) {
5440 if (total_blocks(&dl
->disk
) > total_blocks(&df
->disk
))
5441 set_total_blocks(&df
->disk
, total_blocks(&dl
->disk
));
5442 _disk
= __get_imsm_disk(mpb
, df
->index
);
5447 /* refresh unset/failed slots to point to valid 'missing' entries */
5448 for (df
= super
->missing
; df
; df
= df
->next
)
5449 for (slot
= 0; slot
< mpb
->num_disks
; slot
++) {
5450 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
5452 if ((ord
& IMSM_ORD_REBUILD
) == 0)
5454 set_imsm_ord_tbl_ent(map
, slot
, df
->index
| IMSM_ORD_REBUILD
);
5455 if (is_gen_migration(dev
)) {
5456 struct imsm_map
*map2
= get_imsm_map(dev
,
5458 int slot2
= get_imsm_disk_slot(map2
, df
->index
);
5459 if (slot2
< map2
->num_members
&& slot2
>= 0) {
5460 __u32 ord2
= get_imsm_ord_tbl_ent(dev
,
5463 if ((unsigned)df
->index
==
5465 set_imsm_ord_tbl_ent(map2
,
5471 dprintf("set slot:%d to missing disk:%d\n", slot
, df
->index
);
5475 /* if we are creating the first raid device update the family number */
5476 if (super
->current_vol
== 0) {
5478 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
5480 _disk
= __get_imsm_disk(mpb
, dl
->index
);
5481 if (!_dev
|| !_disk
) {
5482 pr_err("BUG mpb setup error\n");
5488 sum
+= __gen_imsm_checksum(mpb
);
5489 mpb
->family_num
= __cpu_to_le32(sum
);
5490 mpb
->orig_family_num
= mpb
->family_num
;
5492 super
->current_disk
= dl
;
5497 * Function marks disk as spare and restores disk serial
5498 * in case it was previously marked as failed by takeover operation
5500 * -1 : critical error
5501 * 0 : disk is marked as spare but serial is not set
5504 int mark_spare(struct dl
*disk
)
5506 __u8 serial
[MAX_RAID_SERIAL_LEN
];
5513 if (!imsm_read_serial(disk
->fd
, NULL
, serial
)) {
5514 /* Restore disk serial number, because takeover marks disk
5515 * as failed and adds to serial ':0' before it becomes
5518 serialcpy(disk
->serial
, serial
);
5519 serialcpy(disk
->disk
.serial
, serial
);
5522 disk
->disk
.status
= SPARE_DISK
;
5528 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
5529 int fd
, char *devname
,
5530 unsigned long long data_offset
)
5532 struct intel_super
*super
= st
->sb
;
5534 unsigned long long size
;
5535 unsigned int member_sector_size
;
5540 /* If we are on an RAID enabled platform check that the disk is
5541 * attached to the raid controller.
5542 * We do not need to test disks attachment for container based additions,
5543 * they shall be already tested when container was created/assembled.
5545 rv
= find_intel_hba_capability(fd
, super
, devname
);
5546 /* no orom/efi or non-intel hba of the disk */
5548 dprintf("capability: %p fd: %d ret: %d\n",
5549 super
->orom
, fd
, rv
);
5553 if (super
->current_vol
>= 0)
5554 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
5557 dd
= xcalloc(sizeof(*dd
), 1);
5558 dd
->major
= major(stb
.st_rdev
);
5559 dd
->minor
= minor(stb
.st_rdev
);
5560 dd
->devname
= devname
? xstrdup(devname
) : NULL
;
5563 dd
->action
= DISK_ADD
;
5564 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
5566 pr_err("failed to retrieve scsi serial, aborting\n");
5572 if (super
->hba
&& ((super
->hba
->type
== SYS_DEV_NVME
) ||
5573 (super
->hba
->type
== SYS_DEV_VMD
))) {
5575 char *devpath
= diskfd_to_devpath(fd
);
5576 char controller_path
[PATH_MAX
];
5579 pr_err("failed to get devpath, aborting\n");
5586 snprintf(controller_path
, PATH_MAX
-1, "%s/device", devpath
);
5589 if (devpath_to_vendor(controller_path
) == 0x8086) {
5591 * If Intel's NVMe drive has serial ended with
5592 * "-A","-B","-1" or "-2" it means that this is "x8"
5593 * device (double drive on single PCIe card).
5594 * User should be warned about potential data loss.
5596 for (i
= MAX_RAID_SERIAL_LEN
-1; i
> 0; i
--) {
5597 /* Skip empty character at the end */
5598 if (dd
->serial
[i
] == 0)
5601 if (((dd
->serial
[i
] == 'A') ||
5602 (dd
->serial
[i
] == 'B') ||
5603 (dd
->serial
[i
] == '1') ||
5604 (dd
->serial
[i
] == '2')) &&
5605 (dd
->serial
[i
-1] == '-'))
5606 pr_err("\tThe action you are about to take may put your data at risk.\n"
5607 "\tPlease note that x8 devices may consist of two separate x4 devices "
5608 "located on a single PCIe port.\n"
5609 "\tRAID 0 is the only supported configuration for this type of x8 device.\n");
5612 } else if (super
->hba
->type
== SYS_DEV_VMD
&& super
->orom
&&
5613 !imsm_orom_has_tpv_support(super
->orom
)) {
5614 pr_err("\tPlatform configuration does not support non-Intel NVMe drives.\n"
5615 "\tPlease refer to Intel(R) RSTe user guide.\n");
5622 get_dev_size(fd
, NULL
, &size
);
5623 get_dev_sector_size(fd
, NULL
, &member_sector_size
);
5625 if (super
->sector_size
== 0) {
5626 /* this a first device, so sector_size is not set yet */
5627 super
->sector_size
= member_sector_size
;
5628 } else if (member_sector_size
!= super
->sector_size
) {
5629 pr_err("Mixing between different sector size is forbidden, aborting...\n");
5636 /* clear migr_rec when adding disk to container */
5637 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SECTORS
*super
->sector_size
);
5638 if (lseek64(fd
, size
- MIGR_REC_SECTOR_POSITION
*super
->sector_size
,
5640 if ((unsigned int)write(fd
, super
->migr_rec_buf
,
5641 MIGR_REC_BUF_SECTORS
*super
->sector_size
) !=
5642 MIGR_REC_BUF_SECTORS
*super
->sector_size
)
5643 perror("Write migr_rec failed");
5647 serialcpy(dd
->disk
.serial
, dd
->serial
);
5648 set_total_blocks(&dd
->disk
, size
);
5649 if (__le32_to_cpu(dd
->disk
.total_blocks_hi
) > 0) {
5650 struct imsm_super
*mpb
= super
->anchor
;
5651 mpb
->attributes
|= MPB_ATTRIB_2TB_DISK
;
5654 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
5655 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
5657 dd
->disk
.scsi_id
= __cpu_to_le32(0);
5659 if (st
->update_tail
) {
5660 dd
->next
= super
->disk_mgmt_list
;
5661 super
->disk_mgmt_list
= dd
;
5663 dd
->next
= super
->disks
;
5665 super
->updates_pending
++;
5671 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
5673 struct intel_super
*super
= st
->sb
;
5676 /* remove from super works only in mdmon - for communication
5677 * manager - monitor. Check if communication memory buffer
5680 if (!st
->update_tail
) {
5681 pr_err("shall be used in mdmon context only\n");
5684 dd
= xcalloc(1, sizeof(*dd
));
5685 dd
->major
= dk
->major
;
5686 dd
->minor
= dk
->minor
;
5689 dd
->action
= DISK_REMOVE
;
5691 dd
->next
= super
->disk_mgmt_list
;
5692 super
->disk_mgmt_list
= dd
;
5697 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
5700 char buf
[MAX_SECTOR_SIZE
];
5701 struct imsm_super anchor
;
5702 } spare_record
__attribute__ ((aligned(MAX_SECTOR_SIZE
)));
5704 /* spare records have their own family number and do not have any defined raid
5707 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
5709 struct imsm_super
*mpb
= super
->anchor
;
5710 struct imsm_super
*spare
= &spare_record
.anchor
;
5714 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
));
5715 spare
->generation_num
= __cpu_to_le32(1UL);
5716 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5717 spare
->num_disks
= 1;
5718 spare
->num_raid_devs
= 0;
5719 spare
->cache_size
= mpb
->cache_size
;
5720 spare
->pwr_cycle_count
= __cpu_to_le32(1);
5722 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
5723 MPB_SIGNATURE MPB_VERSION_RAID0
);
5725 for (d
= super
->disks
; d
; d
= d
->next
) {
5729 spare
->disk
[0] = d
->disk
;
5730 if (__le32_to_cpu(d
->disk
.total_blocks_hi
) > 0)
5731 spare
->attributes
|= MPB_ATTRIB_2TB_DISK
;
5733 if (super
->sector_size
== 4096)
5734 convert_to_4k_imsm_disk(&spare
->disk
[0]);
5736 sum
= __gen_imsm_checksum(spare
);
5737 spare
->family_num
= __cpu_to_le32(sum
);
5738 spare
->orig_family_num
= 0;
5739 sum
= __gen_imsm_checksum(spare
);
5740 spare
->check_sum
= __cpu_to_le32(sum
);
5742 if (store_imsm_mpb(d
->fd
, spare
)) {
5743 pr_err("failed for device %d:%d %s\n",
5744 d
->major
, d
->minor
, strerror(errno
));
5756 static int write_super_imsm(struct supertype
*st
, int doclose
)
5758 struct intel_super
*super
= st
->sb
;
5759 unsigned int sector_size
= super
->sector_size
;
5760 struct imsm_super
*mpb
= super
->anchor
;
5766 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
5768 int clear_migration_record
= 1;
5771 /* 'generation' is incremented everytime the metadata is written */
5772 generation
= __le32_to_cpu(mpb
->generation_num
);
5774 mpb
->generation_num
= __cpu_to_le32(generation
);
5776 /* fix up cases where previous mdadm releases failed to set
5779 if (mpb
->orig_family_num
== 0)
5780 mpb
->orig_family_num
= mpb
->family_num
;
5782 for (d
= super
->disks
; d
; d
= d
->next
) {
5786 mpb
->disk
[d
->index
] = d
->disk
;
5790 for (d
= super
->missing
; d
; d
= d
->next
) {
5791 mpb
->disk
[d
->index
] = d
->disk
;
5794 mpb
->num_disks
= num_disks
;
5795 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
5797 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5798 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
5799 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
5801 imsm_copy_dev(dev
, dev2
);
5802 mpb_size
+= sizeof_imsm_dev(dev
, 0);
5804 if (is_gen_migration(dev2
))
5805 clear_migration_record
= 0;
5808 bbm_log_size
= get_imsm_bbm_log_size(super
->bbm_log
);
5811 memcpy((void *)mpb
+ mpb_size
, super
->bbm_log
, bbm_log_size
);
5812 mpb
->attributes
|= MPB_ATTRIB_BBM
;
5814 mpb
->attributes
&= ~MPB_ATTRIB_BBM
;
5816 super
->anchor
->bbm_log_size
= __cpu_to_le32(bbm_log_size
);
5817 mpb_size
+= bbm_log_size
;
5818 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5821 assert(super
->len
== 0 || mpb_size
<= super
->len
);
5824 /* recalculate checksum */
5825 sum
= __gen_imsm_checksum(mpb
);
5826 mpb
->check_sum
= __cpu_to_le32(sum
);
5828 if (super
->clean_migration_record_by_mdmon
) {
5829 clear_migration_record
= 1;
5830 super
->clean_migration_record_by_mdmon
= 0;
5832 if (clear_migration_record
)
5833 memset(super
->migr_rec_buf
, 0,
5834 MIGR_REC_BUF_SECTORS
*sector_size
);
5836 if (sector_size
== 4096)
5837 convert_to_4k(super
);
5839 /* write the mpb for disks that compose raid devices */
5840 for (d
= super
->disks
; d
; d
= d
->next
) {
5841 if (d
->index
< 0 || is_failed(&d
->disk
))
5844 if (clear_migration_record
) {
5845 unsigned long long dsize
;
5847 get_dev_size(d
->fd
, NULL
, &dsize
);
5848 if (lseek64(d
->fd
, dsize
- sector_size
,
5850 if ((unsigned int)write(d
->fd
,
5851 super
->migr_rec_buf
,
5852 MIGR_REC_BUF_SECTORS
*sector_size
) !=
5853 MIGR_REC_BUF_SECTORS
*sector_size
)
5854 perror("Write migr_rec failed");
5858 if (store_imsm_mpb(d
->fd
, mpb
))
5860 "failed for device %d:%d (fd: %d)%s\n",
5862 d
->fd
, strerror(errno
));
5871 return write_super_imsm_spares(super
, doclose
);
5876 static int create_array(struct supertype
*st
, int dev_idx
)
5879 struct imsm_update_create_array
*u
;
5880 struct intel_super
*super
= st
->sb
;
5881 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
5882 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
5883 struct disk_info
*inf
;
5884 struct imsm_disk
*disk
;
5887 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
5888 sizeof(*inf
) * map
->num_members
;
5890 u
->type
= update_create_array
;
5891 u
->dev_idx
= dev_idx
;
5892 imsm_copy_dev(&u
->dev
, dev
);
5893 inf
= get_disk_info(u
);
5894 for (i
= 0; i
< map
->num_members
; i
++) {
5895 int idx
= get_imsm_disk_idx(dev
, i
, MAP_X
);
5897 disk
= get_imsm_disk(super
, idx
);
5899 disk
= get_imsm_missing(super
, idx
);
5900 serialcpy(inf
[i
].serial
, disk
->serial
);
5902 append_metadata_update(st
, u
, len
);
5907 static int mgmt_disk(struct supertype
*st
)
5909 struct intel_super
*super
= st
->sb
;
5911 struct imsm_update_add_remove_disk
*u
;
5913 if (!super
->disk_mgmt_list
)
5918 u
->type
= update_add_remove_disk
;
5919 append_metadata_update(st
, u
, len
);
5924 static int write_init_super_imsm(struct supertype
*st
)
5926 struct intel_super
*super
= st
->sb
;
5927 int current_vol
= super
->current_vol
;
5929 /* we are done with current_vol reset it to point st at the container */
5930 super
->current_vol
= -1;
5932 if (st
->update_tail
) {
5933 /* queue the recently created array / added disk
5934 * as a metadata update */
5937 /* determine if we are creating a volume or adding a disk */
5938 if (current_vol
< 0) {
5939 /* in the mgmt (add/remove) disk case we are running
5940 * in mdmon context, so don't close fd's
5942 return mgmt_disk(st
);
5944 rv
= create_array(st
, current_vol
);
5949 for (d
= super
->disks
; d
; d
= d
->next
)
5950 Kill(d
->devname
, NULL
, 0, -1, 1);
5951 return write_super_imsm(st
, 1);
5956 static int store_super_imsm(struct supertype
*st
, int fd
)
5958 struct intel_super
*super
= st
->sb
;
5959 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
5965 if (super
->sector_size
== 4096)
5966 convert_to_4k(super
);
5967 return store_imsm_mpb(fd
, mpb
);
5974 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
5975 int layout
, int raiddisks
, int chunk
,
5976 unsigned long long size
,
5977 unsigned long long data_offset
,
5979 unsigned long long *freesize
,
5983 unsigned long long ldsize
;
5984 struct intel_super
*super
;
5987 if (level
!= LEVEL_CONTAINER
)
5992 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
5995 pr_err("imsm: Cannot open %s: %s\n",
5996 dev
, strerror(errno
));
5999 if (!get_dev_size(fd
, dev
, &ldsize
)) {
6004 /* capabilities retrieve could be possible
6005 * note that there is no fd for the disks in array.
6007 super
= alloc_super();
6012 if (!get_dev_sector_size(fd
, NULL
, &super
->sector_size
)) {
6018 rv
= find_intel_hba_capability(fd
, super
, verbose
> 0 ? dev
: NULL
);
6022 fd2devname(fd
, str
);
6023 dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n",
6024 fd
, str
, super
->orom
, rv
, raiddisks
);
6026 /* no orom/efi or non-intel hba of the disk */
6033 if (raiddisks
> super
->orom
->tds
) {
6035 pr_err("%d exceeds maximum number of platform supported disks: %d\n",
6036 raiddisks
, super
->orom
->tds
);
6040 if ((super
->orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) == 0 &&
6041 (ldsize
>> 9) >> 32 > 0) {
6043 pr_err("%s exceeds maximum platform supported size\n", dev
);
6049 *freesize
= avail_size_imsm(st
, ldsize
>> 9, data_offset
);
6055 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
6057 const unsigned long long base_start
= e
[*idx
].start
;
6058 unsigned long long end
= base_start
+ e
[*idx
].size
;
6061 if (base_start
== end
)
6065 for (i
= *idx
; i
< num_extents
; i
++) {
6066 /* extend overlapping extents */
6067 if (e
[i
].start
>= base_start
&&
6068 e
[i
].start
<= end
) {
6071 if (e
[i
].start
+ e
[i
].size
> end
)
6072 end
= e
[i
].start
+ e
[i
].size
;
6073 } else if (e
[i
].start
> end
) {
6079 return end
- base_start
;
6082 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
6084 /* build a composite disk with all known extents and generate a new
6085 * 'maxsize' given the "all disks in an array must share a common start
6086 * offset" constraint
6088 struct extent
*e
= xcalloc(sum_extents
, sizeof(*e
));
6092 unsigned long long pos
;
6093 unsigned long long start
= 0;
6094 unsigned long long maxsize
;
6095 unsigned long reserve
;
6097 /* coalesce and sort all extents. also, check to see if we need to
6098 * reserve space between member arrays
6101 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6104 for (i
= 0; i
< dl
->extent_cnt
; i
++)
6107 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
6112 while (i
< sum_extents
) {
6113 e
[j
].start
= e
[i
].start
;
6114 e
[j
].size
= find_size(e
, &i
, sum_extents
);
6116 if (e
[j
-1].size
== 0)
6125 unsigned long long esize
;
6127 esize
= e
[i
].start
- pos
;
6128 if (esize
>= maxsize
) {
6133 pos
= e
[i
].start
+ e
[i
].size
;
6135 } while (e
[i
-1].size
);
6141 /* FIXME assumes volume at offset 0 is the first volume in a
6144 if (start_extent
> 0)
6145 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
6149 if (maxsize
< reserve
)
6152 super
->create_offset
= ~((unsigned long long) 0);
6153 if (start
+ reserve
> super
->create_offset
)
6154 return 0; /* start overflows create_offset */
6155 super
->create_offset
= start
+ reserve
;
6157 return maxsize
- reserve
;
6160 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
6162 if (level
< 0 || level
== 6 || level
== 4)
6165 /* if we have an orom prevent invalid raid levels */
6168 case 0: return imsm_orom_has_raid0(orom
);
6171 return imsm_orom_has_raid1e(orom
);
6172 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
6173 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
6174 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
6177 return 1; /* not on an Intel RAID platform so anything goes */
6183 active_arrays_by_format(char *name
, char* hba
, struct md_list
**devlist
,
6184 int dpa
, int verbose
)
6186 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
6187 struct mdstat_ent
*memb
;
6193 for (memb
= mdstat
; memb
; memb
= memb
->next
) {
6194 if (memb
->metadata_version
&&
6195 (strncmp(memb
->metadata_version
, "external:", 9) == 0) &&
6196 (strcmp(&memb
->metadata_version
[9], name
) == 0) &&
6197 !is_subarray(memb
->metadata_version
+9) &&
6199 struct dev_member
*dev
= memb
->members
;
6201 while(dev
&& (fd
< 0)) {
6202 char *path
= xmalloc(strlen(dev
->name
) + strlen("/dev/") + 1);
6203 num
= sprintf(path
, "%s%s", "/dev/", dev
->name
);
6205 fd
= open(path
, O_RDONLY
, 0);
6206 if (num
<= 0 || fd
< 0) {
6207 pr_vrb("Cannot open %s: %s\n",
6208 dev
->name
, strerror(errno
));
6214 if (fd
>= 0 && disk_attached_to_hba(fd
, hba
)) {
6215 struct mdstat_ent
*vol
;
6216 for (vol
= mdstat
; vol
; vol
= vol
->next
) {
6217 if (vol
->active
> 0 &&
6218 vol
->metadata_version
&&
6219 is_container_member(vol
, memb
->devnm
)) {
6224 if (*devlist
&& (found
< dpa
)) {
6225 dv
= xcalloc(1, sizeof(*dv
));
6226 dv
->devname
= xmalloc(strlen(memb
->devnm
) + strlen("/dev/") + 1);
6227 sprintf(dv
->devname
, "%s%s", "/dev/", memb
->devnm
);
6230 dv
->next
= *devlist
;
6238 free_mdstat(mdstat
);
6243 static struct md_list
*
6244 get_loop_devices(void)
6247 struct md_list
*devlist
= NULL
;
6250 for(i
= 0; i
< 12; i
++) {
6251 dv
= xcalloc(1, sizeof(*dv
));
6252 dv
->devname
= xmalloc(40);
6253 sprintf(dv
->devname
, "/dev/loop%d", i
);
6261 static struct md_list
*
6262 get_devices(const char *hba_path
)
6264 struct md_list
*devlist
= NULL
;
6271 devlist
= get_loop_devices();
6274 /* scroll through /sys/dev/block looking for devices attached to
6277 dir
= opendir("/sys/dev/block");
6278 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
6283 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
6285 path
= devt_to_devpath(makedev(major
, minor
));
6288 if (!path_attached_to_hba(path
, hba_path
)) {
6295 fd
= dev_open(ent
->d_name
, O_RDONLY
);
6297 fd2devname(fd
, buf
);
6300 pr_err("cannot open device: %s\n",
6305 dv
= xcalloc(1, sizeof(*dv
));
6306 dv
->devname
= xstrdup(buf
);
6313 devlist
= devlist
->next
;
6323 count_volumes_list(struct md_list
*devlist
, char *homehost
,
6324 int verbose
, int *found
)
6326 struct md_list
*tmpdev
;
6328 struct supertype
*st
;
6330 /* first walk the list of devices to find a consistent set
6331 * that match the criterea, if that is possible.
6332 * We flag the ones we like with 'used'.
6335 st
= match_metadata_desc_imsm("imsm");
6337 pr_vrb("cannot allocate memory for imsm supertype\n");
6341 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
6342 char *devname
= tmpdev
->devname
;
6344 struct supertype
*tst
;
6346 if (tmpdev
->used
> 1)
6348 tst
= dup_super(st
);
6350 pr_vrb("cannot allocate memory for imsm supertype\n");
6353 tmpdev
->container
= 0;
6354 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
6356 dprintf("cannot open device %s: %s\n",
6357 devname
, strerror(errno
));
6359 } else if (fstat(dfd
, &stb
)< 0) {
6361 dprintf("fstat failed for %s: %s\n",
6362 devname
, strerror(errno
));
6364 } else if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
6365 dprintf("%s is not a block device.\n",
6368 } else if (must_be_container(dfd
)) {
6369 struct supertype
*cst
;
6370 cst
= super_by_fd(dfd
, NULL
);
6372 dprintf("cannot recognize container type %s\n",
6375 } else if (tst
->ss
!= st
->ss
) {
6376 dprintf("non-imsm container - ignore it: %s\n",
6379 } else if (!tst
->ss
->load_container
||
6380 tst
->ss
->load_container(tst
, dfd
, NULL
))
6383 tmpdev
->container
= 1;
6386 cst
->ss
->free_super(cst
);
6388 tmpdev
->st_rdev
= stb
.st_rdev
;
6389 if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
6390 dprintf("no RAID superblock on %s\n",
6393 } else if (tst
->ss
->compare_super
== NULL
) {
6394 dprintf("Cannot assemble %s metadata on %s\n",
6395 tst
->ss
->name
, devname
);
6401 if (tmpdev
->used
== 2 || tmpdev
->used
== 4) {
6402 /* Ignore unrecognised devices during auto-assembly */
6407 tst
->ss
->getinfo_super(tst
, &info
, NULL
);
6409 if (st
->minor_version
== -1)
6410 st
->minor_version
= tst
->minor_version
;
6412 if (memcmp(info
.uuid
, uuid_zero
,
6413 sizeof(int[4])) == 0) {
6414 /* this is a floating spare. It cannot define
6415 * an array unless there are no more arrays of
6416 * this type to be found. It can be included
6417 * in an array of this type though.
6423 if (st
->ss
!= tst
->ss
||
6424 st
->minor_version
!= tst
->minor_version
||
6425 st
->ss
->compare_super(st
, tst
) != 0) {
6426 /* Some mismatch. If exactly one array matches this host,
6427 * we can resolve on that one.
6428 * Or, if we are auto assembling, we just ignore the second
6431 dprintf("superblock on %s doesn't match others - assembly aborted\n",
6437 dprintf("found: devname: %s\n", devname
);
6441 tst
->ss
->free_super(tst
);
6445 if ((err
= load_super_imsm_all(st
, -1, &st
->sb
, NULL
, devlist
, 0)) == 0) {
6446 struct mdinfo
*iter
, *head
= st
->ss
->container_content(st
, NULL
);
6447 for (iter
= head
; iter
; iter
= iter
->next
) {
6448 dprintf("content->text_version: %s vol\n",
6449 iter
->text_version
);
6450 if (iter
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
6451 /* do not assemble arrays with unsupported
6453 dprintf("Cannot activate member %s.\n",
6454 iter
->text_version
);
6461 dprintf("No valid super block on device list: err: %d %p\n",
6465 dprintf("no more devices to examine\n");
6468 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
6469 if (tmpdev
->used
== 1 && tmpdev
->found
) {
6471 if (count
< tmpdev
->found
)
6474 count
-= tmpdev
->found
;
6477 if (tmpdev
->used
== 1)
6482 st
->ss
->free_super(st
);
6486 static int __count_volumes(char *hba_path
, int dpa
, int verbose
,
6489 struct sys_dev
*idev
, *intel_devices
= find_intel_devices();
6491 const struct orom_entry
*entry
;
6492 struct devid_list
*dv
, *devid_list
;
6497 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
6498 if (strstr(idev
->path
, hba_path
))
6502 if (!idev
|| !idev
->dev_id
)
6505 entry
= get_orom_entry_by_device_id(idev
->dev_id
);
6507 if (!entry
|| !entry
->devid_list
)
6510 devid_list
= entry
->devid_list
;
6511 for (dv
= devid_list
; dv
; dv
= dv
->next
) {
6512 struct md_list
*devlist
;
6513 struct sys_dev
*device
= NULL
;
6518 device
= device_by_id_and_path(dv
->devid
, hba_path
);
6520 device
= device_by_id(dv
->devid
);
6523 hpath
= device
->path
;
6527 devlist
= get_devices(hpath
);
6528 /* if no intel devices return zero volumes */
6529 if (devlist
== NULL
)
6532 count
+= active_arrays_by_format("imsm", hpath
, &devlist
, dpa
,
6534 dprintf("path: %s active arrays: %d\n", hpath
, count
);
6535 if (devlist
== NULL
)
6539 count
+= count_volumes_list(devlist
,
6543 dprintf("found %d count: %d\n", found
, count
);
6546 dprintf("path: %s total number of volumes: %d\n", hpath
, count
);
6549 struct md_list
*dv
= devlist
;
6550 devlist
= devlist
->next
;
6558 static int count_volumes(struct intel_hba
*hba
, int dpa
, int verbose
)
6562 if (hba
->type
== SYS_DEV_VMD
) {
6563 struct sys_dev
*dev
;
6566 for (dev
= find_intel_devices(); dev
; dev
= dev
->next
) {
6567 if (dev
->type
== SYS_DEV_VMD
)
6568 count
+= __count_volumes(dev
->path
, dpa
,
6573 return __count_volumes(hba
->path
, dpa
, verbose
, 0);
6576 static int imsm_default_chunk(const struct imsm_orom
*orom
)
6578 /* up to 512 if the plaform supports it, otherwise the platform max.
6579 * 128 if no platform detected
6581 int fs
= max(7, orom
? fls(orom
->sss
) : 0);
6583 return min(512, (1 << fs
));
6587 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
6588 int raiddisks
, int *chunk
, unsigned long long size
, int verbose
)
6590 /* check/set platform and metadata limits/defaults */
6591 if (super
->orom
&& raiddisks
> super
->orom
->dpa
) {
6592 pr_vrb("platform supports a maximum of %d disks per array\n",
6597 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
6598 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
6599 pr_vrb("platform does not support raid%d with %d disk%s\n",
6600 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
6604 if (*chunk
== 0 || *chunk
== UnSet
)
6605 *chunk
= imsm_default_chunk(super
->orom
);
6607 if (super
->orom
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
6608 pr_vrb("platform does not support a chunk size of: %d\n", *chunk
);
6612 if (layout
!= imsm_level_to_layout(level
)) {
6614 pr_vrb("imsm raid 5 only supports the left-asymmetric layout\n");
6615 else if (level
== 10)
6616 pr_vrb("imsm raid 10 only supports the n2 layout\n");
6618 pr_vrb("imsm unknown layout %#x for this raid level %d\n",
6623 if (super
->orom
&& (super
->orom
->attr
& IMSM_OROM_ATTR_2TB
) == 0 &&
6624 (calc_array_size(level
, raiddisks
, layout
, *chunk
, size
) >> 32) > 0) {
6625 pr_vrb("platform does not support a volume size over 2TB\n");
6632 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
6633 * FIX ME add ahci details
6635 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
6636 int layout
, int raiddisks
, int *chunk
,
6637 unsigned long long size
,
6638 unsigned long long data_offset
,
6640 unsigned long long *freesize
,
6644 struct intel_super
*super
= st
->sb
;
6645 struct imsm_super
*mpb
;
6647 unsigned long long pos
= 0;
6648 unsigned long long maxsize
;
6652 /* We must have the container info already read in. */
6656 mpb
= super
->anchor
;
6658 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, size
, verbose
)) {
6659 pr_err("RAID gemetry validation failed. Cannot proceed with the action(s).\n");
6663 /* General test: make sure there is space for
6664 * 'raiddisks' device extents of size 'size' at a given
6667 unsigned long long minsize
= size
;
6668 unsigned long long start_offset
= MaxSector
;
6671 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
6672 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6677 e
= get_extents(super
, dl
);
6680 unsigned long long esize
;
6681 esize
= e
[i
].start
- pos
;
6682 if (esize
>= minsize
)
6684 if (found
&& start_offset
== MaxSector
) {
6687 } else if (found
&& pos
!= start_offset
) {
6691 pos
= e
[i
].start
+ e
[i
].size
;
6693 } while (e
[i
-1].size
);
6698 if (dcnt
< raiddisks
) {
6700 pr_err("imsm: Not enough devices with space for this array (%d < %d)\n",
6707 /* This device must be a member of the set */
6708 if (stat(dev
, &stb
) < 0)
6710 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
6712 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6713 if (dl
->major
== (int)major(stb
.st_rdev
) &&
6714 dl
->minor
== (int)minor(stb
.st_rdev
))
6719 pr_err("%s is not in the same imsm set\n", dev
);
6721 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
6722 /* If a volume is present then the current creation attempt
6723 * cannot incorporate new spares because the orom may not
6724 * understand this configuration (all member disks must be
6725 * members of each array in the container).
6727 pr_err("%s is a spare and a volume is already defined for this container\n", dev
);
6728 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
6730 } else if (super
->orom
&& mpb
->num_raid_devs
> 0 &&
6731 mpb
->num_disks
!= raiddisks
) {
6732 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
6736 /* retrieve the largest free space block */
6737 e
= get_extents(super
, dl
);
6742 unsigned long long esize
;
6744 esize
= e
[i
].start
- pos
;
6745 if (esize
>= maxsize
)
6747 pos
= e
[i
].start
+ e
[i
].size
;
6749 } while (e
[i
-1].size
);
6754 pr_err("unable to determine free space for: %s\n",
6758 if (maxsize
< size
) {
6760 pr_err("%s not enough space (%llu < %llu)\n",
6761 dev
, maxsize
, size
);
6765 /* count total number of extents for merge */
6767 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6769 i
+= dl
->extent_cnt
;
6771 maxsize
= merge_extents(super
, i
);
6773 if (!check_env("IMSM_NO_PLATFORM") &&
6774 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6775 pr_err("attempting to create a second volume with size less then remaining space. Aborting...\n");
6779 if (maxsize
< size
|| maxsize
== 0) {
6782 pr_err("no free space left on device. Aborting...\n");
6784 pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n",
6790 *freesize
= maxsize
;
6793 int count
= count_volumes(super
->hba
,
6794 super
->orom
->dpa
, verbose
);
6795 if (super
->orom
->vphba
<= count
) {
6796 pr_vrb("platform does not support more than %d raid volumes.\n",
6797 super
->orom
->vphba
);
6804 static int imsm_get_free_size(struct supertype
*st
, int raiddisks
,
6805 unsigned long long size
, int chunk
,
6806 unsigned long long *freesize
)
6808 struct intel_super
*super
= st
->sb
;
6809 struct imsm_super
*mpb
= super
->anchor
;
6814 unsigned long long maxsize
;
6815 unsigned long long minsize
;
6819 /* find the largest common start free region of the possible disks */
6823 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6829 /* don't activate new spares if we are orom constrained
6830 * and there is already a volume active in the container
6832 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
6835 e
= get_extents(super
, dl
);
6838 for (i
= 1; e
[i
-1].size
; i
++)
6846 maxsize
= merge_extents(super
, extent_cnt
);
6850 minsize
= chunk
* 2;
6852 if (cnt
< raiddisks
||
6853 (super
->orom
&& used
&& used
!= raiddisks
) ||
6854 maxsize
< minsize
||
6856 pr_err("not enough devices with space to create array.\n");
6857 return 0; /* No enough free spaces large enough */
6868 if (!check_env("IMSM_NO_PLATFORM") &&
6869 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6870 pr_err("attempting to create a second volume with size less then remaining space. Aborting...\n");
6874 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6876 dl
->raiddisk
= cnt
++;
6880 dprintf("imsm: imsm_get_free_size() returns : %llu\n", size
);
6885 static int reserve_space(struct supertype
*st
, int raiddisks
,
6886 unsigned long long size
, int chunk
,
6887 unsigned long long *freesize
)
6889 struct intel_super
*super
= st
->sb
;
6894 rv
= imsm_get_free_size(st
, raiddisks
, size
, chunk
, freesize
);
6897 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6899 dl
->raiddisk
= cnt
++;
6906 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
6907 int raiddisks
, int *chunk
, unsigned long long size
,
6908 unsigned long long data_offset
,
6909 char *dev
, unsigned long long *freesize
,
6917 * if given unused devices create a container
6918 * if given given devices in a container create a member volume
6920 if (level
== LEVEL_CONTAINER
) {
6921 /* Must be a fresh device to add to a container */
6922 return validate_geometry_imsm_container(st
, level
, layout
,
6932 struct intel_super
*super
= st
->sb
;
6933 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
6934 raiddisks
, chunk
, size
,
6937 /* we are being asked to automatically layout a
6938 * new volume based on the current contents of
6939 * the container. If the the parameters can be
6940 * satisfied reserve_space will record the disks,
6941 * start offset, and size of the volume to be
6942 * created. add_to_super and getinfo_super
6943 * detect when autolayout is in progress.
6945 /* assuming that freesize is always given when array is
6947 if (super
->orom
&& freesize
) {
6949 count
= count_volumes(super
->hba
,
6950 super
->orom
->dpa
, verbose
);
6951 if (super
->orom
->vphba
<= count
) {
6952 pr_vrb("platform does not support more than %d raid volumes.\n",
6953 super
->orom
->vphba
);
6958 return reserve_space(st
, raiddisks
, size
,
6964 /* creating in a given container */
6965 return validate_geometry_imsm_volume(st
, level
, layout
,
6966 raiddisks
, chunk
, size
,
6968 dev
, freesize
, verbose
);
6971 /* This device needs to be a device in an 'imsm' container */
6972 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
6975 pr_err("Cannot create this array on device %s\n",
6980 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
6982 pr_err("Cannot open %s: %s\n",
6983 dev
, strerror(errno
));
6986 /* Well, it is in use by someone, maybe an 'imsm' container. */
6987 cfd
= open_container(fd
);
6991 pr_err("Cannot use %s: It is busy\n",
6995 sra
= sysfs_read(cfd
, NULL
, GET_VERSION
);
6996 if (sra
&& sra
->array
.major_version
== -1 &&
6997 strcmp(sra
->text_version
, "imsm") == 0)
7001 /* This is a member of a imsm container. Load the container
7002 * and try to create a volume
7004 struct intel_super
*super
;
7006 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
, NULL
, 1) == 0) {
7008 strcpy(st
->container_devnm
, fd2devnm(cfd
));
7010 return validate_geometry_imsm_volume(st
, level
, layout
,
7012 size
, data_offset
, dev
,
7019 pr_err("failed container membership check\n");
7025 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
7027 struct intel_super
*super
= st
->sb
;
7029 if (level
&& *level
== UnSet
)
7030 *level
= LEVEL_CONTAINER
;
7032 if (level
&& layout
&& *layout
== UnSet
)
7033 *layout
= imsm_level_to_layout(*level
);
7035 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0))
7036 *chunk
= imsm_default_chunk(super
->orom
);
7039 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
7041 static int kill_subarray_imsm(struct supertype
*st
)
7043 /* remove the subarray currently referenced by ->current_vol */
7045 struct intel_dev
**dp
;
7046 struct intel_super
*super
= st
->sb
;
7047 __u8 current_vol
= super
->current_vol
;
7048 struct imsm_super
*mpb
= super
->anchor
;
7050 if (super
->current_vol
< 0)
7052 super
->current_vol
= -1; /* invalidate subarray cursor */
7054 /* block deletions that would change the uuid of active subarrays
7056 * FIXME when immutable ids are available, but note that we'll
7057 * also need to fixup the invalidated/active subarray indexes in
7060 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7063 if (i
< current_vol
)
7065 sprintf(subarray
, "%u", i
);
7066 if (is_subarray_active(subarray
, st
->devnm
)) {
7067 pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
7074 if (st
->update_tail
) {
7075 struct imsm_update_kill_array
*u
= xmalloc(sizeof(*u
));
7077 u
->type
= update_kill_array
;
7078 u
->dev_idx
= current_vol
;
7079 append_metadata_update(st
, u
, sizeof(*u
));
7084 for (dp
= &super
->devlist
; *dp
;)
7085 if ((*dp
)->index
== current_vol
) {
7088 handle_missing(super
, (*dp
)->dev
);
7089 if ((*dp
)->index
> current_vol
)
7094 /* no more raid devices, all active components are now spares,
7095 * but of course failed are still failed
7097 if (--mpb
->num_raid_devs
== 0) {
7100 for (d
= super
->disks
; d
; d
= d
->next
)
7105 super
->updates_pending
++;
7110 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
7111 char *update
, struct mddev_ident
*ident
)
7113 /* update the subarray currently referenced by ->current_vol */
7114 struct intel_super
*super
= st
->sb
;
7115 struct imsm_super
*mpb
= super
->anchor
;
7117 if (strcmp(update
, "name") == 0) {
7118 char *name
= ident
->name
;
7122 if (is_subarray_active(subarray
, st
->devnm
)) {
7123 pr_err("Unable to update name of active subarray\n");
7127 if (!check_name(super
, name
, 0))
7130 vol
= strtoul(subarray
, &ep
, 10);
7131 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
7134 if (st
->update_tail
) {
7135 struct imsm_update_rename_array
*u
= xmalloc(sizeof(*u
));
7137 u
->type
= update_rename_array
;
7139 snprintf((char *) u
->name
, MAX_RAID_SERIAL_LEN
, "%s", name
);
7140 append_metadata_update(st
, u
, sizeof(*u
));
7142 struct imsm_dev
*dev
;
7145 dev
= get_imsm_dev(super
, vol
);
7146 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
7147 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7148 dev
= get_imsm_dev(super
, i
);
7149 handle_missing(super
, dev
);
7151 super
->updates_pending
++;
7158 #endif /* MDASSEMBLE */
7160 static int is_gen_migration(struct imsm_dev
*dev
)
7165 if (!dev
->vol
.migr_state
)
7168 if (migr_type(dev
) == MIGR_GEN_MIGR
)
7174 static int is_rebuilding(struct imsm_dev
*dev
)
7176 struct imsm_map
*migr_map
;
7178 if (!dev
->vol
.migr_state
)
7181 if (migr_type(dev
) != MIGR_REBUILD
)
7184 migr_map
= get_imsm_map(dev
, MAP_1
);
7186 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
7193 static int is_initializing(struct imsm_dev
*dev
)
7195 struct imsm_map
*migr_map
;
7197 if (!dev
->vol
.migr_state
)
7200 if (migr_type(dev
) != MIGR_INIT
)
7203 migr_map
= get_imsm_map(dev
, MAP_1
);
7205 if (migr_map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
7212 static void update_recovery_start(struct intel_super
*super
,
7213 struct imsm_dev
*dev
,
7214 struct mdinfo
*array
)
7216 struct mdinfo
*rebuild
= NULL
;
7220 if (!is_rebuilding(dev
))
7223 /* Find the rebuild target, but punt on the dual rebuild case */
7224 for (d
= array
->devs
; d
; d
= d
->next
)
7225 if (d
->recovery_start
== 0) {
7232 /* (?) none of the disks are marked with
7233 * IMSM_ORD_REBUILD, so assume they are missing and the
7234 * disk_ord_tbl was not correctly updated
7236 dprintf("failed to locate out-of-sync disk\n");
7240 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
7241 rebuild
->recovery_start
= units
* blocks_per_migr_unit(super
, dev
);
7245 static int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
);
7248 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
7250 /* Given a container loaded by load_super_imsm_all,
7251 * extract information about all the arrays into
7253 * If 'subarray' is given, just extract info about that array.
7255 * For each imsm_dev create an mdinfo, fill it in,
7256 * then look for matching devices in super->disks
7257 * and create appropriate device mdinfo.
7259 struct intel_super
*super
= st
->sb
;
7260 struct imsm_super
*mpb
= super
->anchor
;
7261 struct mdinfo
*rest
= NULL
;
7265 int spare_disks
= 0;
7267 /* do not assemble arrays when not all attributes are supported */
7268 if (imsm_check_attributes(mpb
->attributes
) == 0) {
7270 pr_err("Unsupported attributes in IMSM metadata.Arrays activation is blocked.\n");
7273 /* count spare devices, not used in maps
7275 for (d
= super
->disks
; d
; d
= d
->next
)
7279 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7280 struct imsm_dev
*dev
;
7281 struct imsm_map
*map
;
7282 struct imsm_map
*map2
;
7283 struct mdinfo
*this;
7291 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
7294 dev
= get_imsm_dev(super
, i
);
7295 map
= get_imsm_map(dev
, MAP_0
);
7296 map2
= get_imsm_map(dev
, MAP_1
);
7298 /* do not publish arrays that are in the middle of an
7299 * unsupported migration
7301 if (dev
->vol
.migr_state
&&
7302 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
7303 pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n",
7307 /* do not publish arrays that are not support by controller's
7311 this = xmalloc(sizeof(*this));
7313 super
->current_vol
= i
;
7314 getinfo_super_imsm_volume(st
, this, NULL
);
7317 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
7318 /* mdadm does not support all metadata features- set the bit in all arrays state */
7319 if (!validate_geometry_imsm_orom(super
,
7320 get_imsm_raid_level(map
), /* RAID level */
7321 imsm_level_to_layout(get_imsm_raid_level(map
)),
7322 map
->num_members
, /* raid disks */
7323 &chunk
, join_u32(dev
->size_low
, dev
->size_high
),
7325 pr_err("IMSM RAID geometry validation failed. Array %s activation is blocked.\n",
7327 this->array
.state
|=
7328 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
7329 (1<<MD_SB_BLOCK_VOLUME
);
7333 /* if array has bad blocks, set suitable bit in all arrays state */
7335 this->array
.state
|=
7336 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
7337 (1<<MD_SB_BLOCK_VOLUME
);
7339 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
7340 unsigned long long recovery_start
;
7341 struct mdinfo
*info_d
;
7348 idx
= get_imsm_disk_idx(dev
, slot
, MAP_0
);
7349 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
7350 for (d
= super
->disks
; d
; d
= d
->next
)
7351 if (d
->index
== idx
)
7354 recovery_start
= MaxSector
;
7357 if (d
&& is_failed(&d
->disk
))
7359 if (ord
& IMSM_ORD_REBUILD
)
7363 * if we skip some disks the array will be assmebled degraded;
7364 * reset resync start to avoid a dirty-degraded
7365 * situation when performing the intial sync
7367 * FIXME handle dirty degraded
7369 if ((skip
|| recovery_start
== 0) && !dev
->vol
.dirty
)
7370 this->resync_start
= MaxSector
;
7374 info_d
= xcalloc(1, sizeof(*info_d
));
7375 info_d
->next
= this->devs
;
7376 this->devs
= info_d
;
7378 info_d
->disk
.number
= d
->index
;
7379 info_d
->disk
.major
= d
->major
;
7380 info_d
->disk
.minor
= d
->minor
;
7381 info_d
->disk
.raid_disk
= slot
;
7382 info_d
->recovery_start
= recovery_start
;
7384 if (slot
< map2
->num_members
)
7385 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
7387 this->array
.spare_disks
++;
7389 if (slot
< map
->num_members
)
7390 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
7392 this->array
.spare_disks
++;
7394 if (info_d
->recovery_start
== MaxSector
)
7395 this->array
.working_disks
++;
7397 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
7398 info_d
->data_offset
= pba_of_lba0(map
);
7400 if (map
->raid_level
== 5) {
7401 info_d
->component_size
=
7402 num_data_stripes(map
) *
7403 map
->blocks_per_strip
;
7405 info_d
->component_size
= blocks_per_member(map
);
7408 info_d
->bb
.supported
= 1;
7409 get_volume_badblocks(super
->bbm_log
, ord_to_idx(ord
),
7410 info_d
->data_offset
,
7411 info_d
->component_size
,
7414 /* now that the disk list is up-to-date fixup recovery_start */
7415 update_recovery_start(super
, dev
, this);
7416 this->array
.spare_disks
+= spare_disks
;
7419 /* check for reshape */
7420 if (this->reshape_active
== 1)
7421 recover_backup_imsm(st
, this);
7429 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
7430 int failed
, int look_in_map
)
7432 struct imsm_map
*map
;
7434 map
= get_imsm_map(dev
, look_in_map
);
7437 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
7438 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
7440 switch (get_imsm_raid_level(map
)) {
7442 return IMSM_T_STATE_FAILED
;
7445 if (failed
< map
->num_members
)
7446 return IMSM_T_STATE_DEGRADED
;
7448 return IMSM_T_STATE_FAILED
;
7453 * check to see if any mirrors have failed, otherwise we
7454 * are degraded. Even numbered slots are mirrored on
7458 /* gcc -Os complains that this is unused */
7459 int insync
= insync
;
7461 for (i
= 0; i
< map
->num_members
; i
++) {
7462 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_X
);
7463 int idx
= ord_to_idx(ord
);
7464 struct imsm_disk
*disk
;
7466 /* reset the potential in-sync count on even-numbered
7467 * slots. num_copies is always 2 for imsm raid10
7472 disk
= get_imsm_disk(super
, idx
);
7473 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
7476 /* no in-sync disks left in this mirror the
7480 return IMSM_T_STATE_FAILED
;
7483 return IMSM_T_STATE_DEGRADED
;
7487 return IMSM_T_STATE_DEGRADED
;
7489 return IMSM_T_STATE_FAILED
;
7495 return map
->map_state
;
7498 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
7503 struct imsm_disk
*disk
;
7504 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7505 struct imsm_map
*prev
= get_imsm_map(dev
, MAP_1
);
7506 struct imsm_map
*map_for_loop
;
7511 /* at the beginning of migration we set IMSM_ORD_REBUILD on
7512 * disks that are being rebuilt. New failures are recorded to
7513 * map[0]. So we look through all the disks we started with and
7514 * see if any failures are still present, or if any new ones
7518 if (prev
&& (map
->num_members
< prev
->num_members
))
7519 map_for_loop
= prev
;
7521 for (i
= 0; i
< map_for_loop
->num_members
; i
++) {
7523 /* when MAP_X is passed both maps failures are counted
7526 (look_in_map
== MAP_1
|| look_in_map
== MAP_X
) &&
7527 i
< prev
->num_members
) {
7528 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
7529 idx_1
= ord_to_idx(ord
);
7531 disk
= get_imsm_disk(super
, idx_1
);
7532 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
7535 if ((look_in_map
== MAP_0
|| look_in_map
== MAP_X
) &&
7536 i
< map
->num_members
) {
7537 ord
= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
7538 idx
= ord_to_idx(ord
);
7541 disk
= get_imsm_disk(super
, idx
);
7542 if (!disk
|| is_failed(disk
) ||
7543 ord
& IMSM_ORD_REBUILD
)
7553 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
7556 struct intel_super
*super
= c
->sb
;
7557 struct imsm_super
*mpb
= super
->anchor
;
7558 struct imsm_update_prealloc_bb_mem u
;
7560 if (atoi(inst
) >= mpb
->num_raid_devs
) {
7561 pr_err("subarry index %d, out of range\n", atoi(inst
));
7565 dprintf("imsm: open_new %s\n", inst
);
7566 a
->info
.container_member
= atoi(inst
);
7568 u
.type
= update_prealloc_badblocks_mem
;
7569 imsm_update_metadata_locally(c
, &u
, sizeof(u
));
7574 static int is_resyncing(struct imsm_dev
*dev
)
7576 struct imsm_map
*migr_map
;
7578 if (!dev
->vol
.migr_state
)
7581 if (migr_type(dev
) == MIGR_INIT
||
7582 migr_type(dev
) == MIGR_REPAIR
)
7585 if (migr_type(dev
) == MIGR_GEN_MIGR
)
7588 migr_map
= get_imsm_map(dev
, MAP_1
);
7590 if (migr_map
->map_state
== IMSM_T_STATE_NORMAL
&&
7591 dev
->vol
.migr_type
!= MIGR_GEN_MIGR
)
7597 /* return true if we recorded new information */
7598 static int mark_failure(struct intel_super
*super
,
7599 struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
7603 struct imsm_map
*map
;
7604 char buf
[MAX_RAID_SERIAL_LEN
+3];
7605 unsigned int len
, shift
= 0;
7607 /* new failures are always set in map[0] */
7608 map
= get_imsm_map(dev
, MAP_0
);
7610 slot
= get_imsm_disk_slot(map
, idx
);
7614 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
7615 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
7618 memcpy(buf
, disk
->serial
, MAX_RAID_SERIAL_LEN
);
7619 buf
[MAX_RAID_SERIAL_LEN
] = '\000';
7621 if ((len
= strlen(buf
)) >= MAX_RAID_SERIAL_LEN
)
7622 shift
= len
- MAX_RAID_SERIAL_LEN
+ 1;
7623 strncpy((char *)disk
->serial
, &buf
[shift
], MAX_RAID_SERIAL_LEN
);
7625 disk
->status
|= FAILED_DISK
;
7626 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
7627 /* mark failures in second map if second map exists and this disk
7629 * This is valid for migration, initialization and rebuild
7631 if (dev
->vol
.migr_state
) {
7632 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
7633 int slot2
= get_imsm_disk_slot(map2
, idx
);
7635 if (slot2
< map2
->num_members
&& slot2
>= 0)
7636 set_imsm_ord_tbl_ent(map2
, slot2
,
7637 idx
| IMSM_ORD_REBUILD
);
7639 if (map
->failed_disk_num
== 0xff)
7640 map
->failed_disk_num
= slot
;
7642 clear_disk_badblocks(super
->bbm_log
, ord_to_idx(ord
));
7647 static void mark_missing(struct intel_super
*super
,
7648 struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
7650 mark_failure(super
, dev
, disk
, idx
);
7652 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
7655 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
7656 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
7659 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
7663 if (!super
->missing
)
7666 /* When orom adds replacement for missing disk it does
7667 * not remove entry of missing disk, but just updates map with
7668 * new added disk. So it is not enough just to test if there is
7669 * any missing disk, we have to look if there are any failed disks
7670 * in map to stop migration */
7672 dprintf("imsm: mark missing\n");
7673 /* end process for initialization and rebuild only
7675 if (is_gen_migration(dev
) == 0) {
7679 failed
= imsm_count_failed(super
, dev
, MAP_0
);
7680 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7683 end_migration(dev
, super
, map_state
);
7685 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
7686 mark_missing(super
, dev
, &dl
->disk
, dl
->index
);
7687 super
->updates_pending
++;
7690 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
,
7693 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
7694 unsigned long long array_blocks
;
7695 struct imsm_map
*map
;
7697 if (used_disks
== 0) {
7698 /* when problems occures
7699 * return current array_blocks value
7701 array_blocks
= __le32_to_cpu(dev
->size_high
);
7702 array_blocks
= array_blocks
<< 32;
7703 array_blocks
+= __le32_to_cpu(dev
->size_low
);
7705 return array_blocks
;
7708 /* set array size in metadata
7710 if (new_size
<= 0) {
7711 /* OLCE size change is caused by added disks
7713 map
= get_imsm_map(dev
, MAP_0
);
7714 array_blocks
= blocks_per_member(map
) * used_disks
;
7716 /* Online Volume Size Change
7717 * Using available free space
7719 array_blocks
= new_size
;
7722 /* round array size down to closest MB
7724 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
7725 dev
->size_low
= __cpu_to_le32((__u32
)array_blocks
);
7726 dev
->size_high
= __cpu_to_le32((__u32
)(array_blocks
>> 32));
7728 return array_blocks
;
7731 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
7733 static void imsm_progress_container_reshape(struct intel_super
*super
)
7735 /* if no device has a migr_state, but some device has a
7736 * different number of members than the previous device, start
7737 * changing the number of devices in this device to match
7740 struct imsm_super
*mpb
= super
->anchor
;
7741 int prev_disks
= -1;
7745 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7746 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
7747 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7748 struct imsm_map
*map2
;
7749 int prev_num_members
;
7751 if (dev
->vol
.migr_state
)
7754 if (prev_disks
== -1)
7755 prev_disks
= map
->num_members
;
7756 if (prev_disks
== map
->num_members
)
7759 /* OK, this array needs to enter reshape mode.
7760 * i.e it needs a migr_state
7763 copy_map_size
= sizeof_imsm_map(map
);
7764 prev_num_members
= map
->num_members
;
7765 map
->num_members
= prev_disks
;
7766 dev
->vol
.migr_state
= 1;
7767 dev
->vol
.curr_migr_unit
= 0;
7768 set_migr_type(dev
, MIGR_GEN_MIGR
);
7769 for (i
= prev_num_members
;
7770 i
< map
->num_members
; i
++)
7771 set_imsm_ord_tbl_ent(map
, i
, i
);
7772 map2
= get_imsm_map(dev
, MAP_1
);
7773 /* Copy the current map */
7774 memcpy(map2
, map
, copy_map_size
);
7775 map2
->num_members
= prev_num_members
;
7777 imsm_set_array_size(dev
, -1);
7778 super
->clean_migration_record_by_mdmon
= 1;
7779 super
->updates_pending
++;
7783 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
7784 * states are handled in imsm_set_disk() with one exception, when a
7785 * resync is stopped due to a new failure this routine will set the
7786 * 'degraded' state for the array.
7788 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
7790 int inst
= a
->info
.container_member
;
7791 struct intel_super
*super
= a
->container
->sb
;
7792 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7793 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7794 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
7795 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7796 __u32 blocks_per_unit
;
7798 if (dev
->vol
.migr_state
&&
7799 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
7800 /* array state change is blocked due to reshape action
7802 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
7803 * - finish the reshape (if last_checkpoint is big and action != reshape)
7804 * - update curr_migr_unit
7806 if (a
->curr_action
== reshape
) {
7807 /* still reshaping, maybe update curr_migr_unit */
7808 goto mark_checkpoint
;
7810 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
7811 /* for some reason we aborted the reshape.
7813 * disable automatic metadata rollback
7814 * user action is required to recover process
7817 struct imsm_map
*map2
=
7818 get_imsm_map(dev
, MAP_1
);
7819 dev
->vol
.migr_state
= 0;
7820 set_migr_type(dev
, 0);
7821 dev
->vol
.curr_migr_unit
= 0;
7823 sizeof_imsm_map(map2
));
7824 super
->updates_pending
++;
7827 if (a
->last_checkpoint
>= a
->info
.component_size
) {
7828 unsigned long long array_blocks
;
7832 used_disks
= imsm_num_data_members(dev
, MAP_0
);
7833 if (used_disks
> 0) {
7835 blocks_per_member(map
) *
7837 /* round array size down to closest MB
7839 array_blocks
= (array_blocks
7840 >> SECT_PER_MB_SHIFT
)
7841 << SECT_PER_MB_SHIFT
;
7842 a
->info
.custom_array_size
= array_blocks
;
7843 /* encourage manager to update array
7847 a
->check_reshape
= 1;
7849 /* finalize online capacity expansion/reshape */
7850 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
7852 mdi
->disk
.raid_disk
,
7855 imsm_progress_container_reshape(super
);
7860 /* before we activate this array handle any missing disks */
7861 if (consistent
== 2)
7862 handle_missing(super
, dev
);
7864 if (consistent
== 2 &&
7865 (!is_resync_complete(&a
->info
) ||
7866 map_state
!= IMSM_T_STATE_NORMAL
||
7867 dev
->vol
.migr_state
))
7870 if (is_resync_complete(&a
->info
)) {
7871 /* complete intialization / resync,
7872 * recovery and interrupted recovery is completed in
7875 if (is_resyncing(dev
)) {
7876 dprintf("imsm: mark resync done\n");
7877 end_migration(dev
, super
, map_state
);
7878 super
->updates_pending
++;
7879 a
->last_checkpoint
= 0;
7881 } else if ((!is_resyncing(dev
) && !failed
) &&
7882 (imsm_reshape_blocks_arrays_changes(super
) == 0)) {
7883 /* mark the start of the init process if nothing is failed */
7884 dprintf("imsm: mark resync start\n");
7885 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
7886 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
7888 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
7889 super
->updates_pending
++;
7893 /* skip checkpointing for general migration,
7894 * it is controlled in mdadm
7896 if (is_gen_migration(dev
))
7897 goto skip_mark_checkpoint
;
7899 /* check if we can update curr_migr_unit from resync_start, recovery_start */
7900 blocks_per_unit
= blocks_per_migr_unit(super
, dev
);
7901 if (blocks_per_unit
) {
7905 units
= a
->last_checkpoint
/ blocks_per_unit
;
7908 /* check that we did not overflow 32-bits, and that
7909 * curr_migr_unit needs updating
7911 if (units32
== units
&&
7913 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
7914 dprintf("imsm: mark checkpoint (%u)\n", units32
);
7915 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
7916 super
->updates_pending
++;
7920 skip_mark_checkpoint
:
7921 /* mark dirty / clean */
7922 if (dev
->vol
.dirty
!= !consistent
) {
7923 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
7928 super
->updates_pending
++;
7934 static int imsm_disk_slot_to_ord(struct active_array
*a
, int slot
)
7936 int inst
= a
->info
.container_member
;
7937 struct intel_super
*super
= a
->container
->sb
;
7938 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7939 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7941 if (slot
> map
->num_members
) {
7942 pr_err("imsm: imsm_disk_slot_to_ord %d out of range 0..%d\n",
7943 slot
, map
->num_members
- 1);
7950 return get_imsm_ord_tbl_ent(dev
, slot
, MAP_0
);
7953 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
7955 int inst
= a
->info
.container_member
;
7956 struct intel_super
*super
= a
->container
->sb
;
7957 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7958 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7959 struct imsm_disk
*disk
;
7961 int recovery_not_finished
= 0;
7966 ord
= imsm_disk_slot_to_ord(a
, n
);
7970 dprintf("imsm: set_disk %d:%x\n", n
, state
);
7971 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
7973 /* check for new failures */
7974 if (state
& DS_FAULTY
) {
7975 if (mark_failure(super
, dev
, disk
, ord_to_idx(ord
)))
7976 super
->updates_pending
++;
7979 /* check if in_sync */
7980 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
7981 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
7983 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
7984 super
->updates_pending
++;
7987 failed
= imsm_count_failed(super
, dev
, MAP_0
);
7988 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7990 /* check if recovery complete, newly degraded, or failed */
7991 dprintf("imsm: Detected transition to state ");
7992 switch (map_state
) {
7993 case IMSM_T_STATE_NORMAL
: /* transition to normal state */
7994 dprintf("normal: ");
7995 if (is_rebuilding(dev
)) {
7996 dprintf_cont("while rebuilding");
7997 /* check if recovery is really finished */
7998 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
7999 if (mdi
->recovery_start
!= MaxSector
) {
8000 recovery_not_finished
= 1;
8003 if (recovery_not_finished
) {
8005 dprintf("Rebuild has not finished yet, state not changed");
8006 if (a
->last_checkpoint
< mdi
->recovery_start
) {
8007 a
->last_checkpoint
= mdi
->recovery_start
;
8008 super
->updates_pending
++;
8012 end_migration(dev
, super
, map_state
);
8013 map
= get_imsm_map(dev
, MAP_0
);
8014 map
->failed_disk_num
= ~0;
8015 super
->updates_pending
++;
8016 a
->last_checkpoint
= 0;
8019 if (is_gen_migration(dev
)) {
8020 dprintf_cont("while general migration");
8021 if (a
->last_checkpoint
>= a
->info
.component_size
)
8022 end_migration(dev
, super
, map_state
);
8024 map
->map_state
= map_state
;
8025 map
= get_imsm_map(dev
, MAP_0
);
8026 map
->failed_disk_num
= ~0;
8027 super
->updates_pending
++;
8031 case IMSM_T_STATE_DEGRADED
: /* transition to degraded state */
8032 dprintf_cont("degraded: ");
8033 if (map
->map_state
!= map_state
&& !dev
->vol
.migr_state
) {
8034 dprintf_cont("mark degraded");
8035 map
->map_state
= map_state
;
8036 super
->updates_pending
++;
8037 a
->last_checkpoint
= 0;
8040 if (is_rebuilding(dev
)) {
8041 dprintf_cont("while rebuilding.");
8042 if (map
->map_state
!= map_state
) {
8043 dprintf_cont(" Map state change");
8044 end_migration(dev
, super
, map_state
);
8045 super
->updates_pending
++;
8049 if (is_gen_migration(dev
)) {
8050 dprintf_cont("while general migration");
8051 if (a
->last_checkpoint
>= a
->info
.component_size
)
8052 end_migration(dev
, super
, map_state
);
8054 map
->map_state
= map_state
;
8055 manage_second_map(super
, dev
);
8057 super
->updates_pending
++;
8060 if (is_initializing(dev
)) {
8061 dprintf_cont("while initialization.");
8062 map
->map_state
= map_state
;
8063 super
->updates_pending
++;
8067 case IMSM_T_STATE_FAILED
: /* transition to failed state */
8068 dprintf_cont("failed: ");
8069 if (is_gen_migration(dev
)) {
8070 dprintf_cont("while general migration");
8071 map
->map_state
= map_state
;
8072 super
->updates_pending
++;
8075 if (map
->map_state
!= map_state
) {
8076 dprintf_cont("mark failed");
8077 end_migration(dev
, super
, map_state
);
8078 super
->updates_pending
++;
8079 a
->last_checkpoint
= 0;
8084 dprintf_cont("state %i\n", map_state
);
8089 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
8092 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
8093 unsigned long long dsize
;
8094 unsigned long long sectors
;
8095 unsigned int sector_size
;
8097 get_dev_sector_size(fd
, NULL
, §or_size
);
8098 get_dev_size(fd
, NULL
, &dsize
);
8100 if (mpb_size
> sector_size
) {
8101 /* -1 to account for anchor */
8102 sectors
= mpb_sectors(mpb
, sector_size
) - 1;
8104 /* write the extended mpb to the sectors preceeding the anchor */
8105 if (lseek64(fd
, dsize
- (sector_size
* (2 + sectors
)),
8109 if ((unsigned long long)write(fd
, buf
+ sector_size
,
8110 sector_size
* sectors
) != sector_size
* sectors
)
8114 /* first block is stored on second to last sector of the disk */
8115 if (lseek64(fd
, dsize
- (sector_size
* 2), SEEK_SET
) < 0)
8118 if ((unsigned int)write(fd
, buf
, sector_size
) != sector_size
)
8124 static void imsm_sync_metadata(struct supertype
*container
)
8126 struct intel_super
*super
= container
->sb
;
8128 dprintf("sync metadata: %d\n", super
->updates_pending
);
8129 if (!super
->updates_pending
)
8132 write_super_imsm(container
, 0);
8134 super
->updates_pending
= 0;
8137 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
8139 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
8140 int i
= get_imsm_disk_idx(dev
, idx
, MAP_X
);
8143 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
8147 if (dl
&& is_failed(&dl
->disk
))
8151 dprintf("found %x:%x\n", dl
->major
, dl
->minor
);
8156 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
8157 struct active_array
*a
, int activate_new
,
8158 struct mdinfo
*additional_test_list
)
8160 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
8161 int idx
= get_imsm_disk_idx(dev
, slot
, MAP_X
);
8162 struct imsm_super
*mpb
= super
->anchor
;
8163 struct imsm_map
*map
;
8164 unsigned long long pos
;
8169 __u32 array_start
= 0;
8170 __u32 array_end
= 0;
8172 struct mdinfo
*test_list
;
8174 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
8175 /* If in this array, skip */
8176 for (d
= a
->info
.devs
; d
; d
= d
->next
)
8177 if (d
->state_fd
>= 0 &&
8178 d
->disk
.major
== dl
->major
&&
8179 d
->disk
.minor
== dl
->minor
) {
8180 dprintf("%x:%x already in array\n",
8181 dl
->major
, dl
->minor
);
8186 test_list
= additional_test_list
;
8188 if (test_list
->disk
.major
== dl
->major
&&
8189 test_list
->disk
.minor
== dl
->minor
) {
8190 dprintf("%x:%x already in additional test list\n",
8191 dl
->major
, dl
->minor
);
8194 test_list
= test_list
->next
;
8199 /* skip in use or failed drives */
8200 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
8202 dprintf("%x:%x status (failed: %d index: %d)\n",
8203 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
8207 /* skip pure spares when we are looking for partially
8208 * assimilated drives
8210 if (dl
->index
== -1 && !activate_new
)
8213 /* Does this unused device have the requisite free space?
8214 * It needs to be able to cover all member volumes
8216 ex
= get_extents(super
, dl
);
8218 dprintf("cannot get extents\n");
8221 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8222 dev
= get_imsm_dev(super
, i
);
8223 map
= get_imsm_map(dev
, MAP_0
);
8225 /* check if this disk is already a member of
8228 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
8234 array_start
= pba_of_lba0(map
);
8235 array_end
= array_start
+
8236 blocks_per_member(map
) - 1;
8239 /* check that we can start at pba_of_lba0 with
8240 * blocks_per_member of space
8242 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
8246 pos
= ex
[j
].start
+ ex
[j
].size
;
8248 } while (ex
[j
-1].size
);
8255 if (i
< mpb
->num_raid_devs
) {
8256 dprintf("%x:%x does not have %u to %u available\n",
8257 dl
->major
, dl
->minor
, array_start
, array_end
);
8267 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
8269 struct imsm_dev
*dev2
;
8270 struct imsm_map
*map
;
8276 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
8278 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
, MAP_0
);
8279 if (state
== IMSM_T_STATE_FAILED
) {
8280 map
= get_imsm_map(dev2
, MAP_0
);
8283 for (slot
= 0; slot
< map
->num_members
; slot
++) {
8285 * Check if failed disks are deleted from intel
8286 * disk list or are marked to be deleted
8288 idx
= get_imsm_disk_idx(dev2
, slot
, MAP_X
);
8289 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
8291 * Do not rebuild the array if failed disks
8292 * from failed sub-array are not removed from
8296 is_failed(&idisk
->disk
) &&
8297 (idisk
->action
!= DISK_REMOVE
))
8305 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
8306 struct metadata_update
**updates
)
8309 * Find a device with unused free space and use it to replace a
8310 * failed/vacant region in an array. We replace failed regions one a
8311 * array at a time. The result is that a new spare disk will be added
8312 * to the first failed array and after the monitor has finished
8313 * propagating failures the remainder will be consumed.
8315 * FIXME add a capability for mdmon to request spares from another
8319 struct intel_super
*super
= a
->container
->sb
;
8320 int inst
= a
->info
.container_member
;
8321 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8322 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8323 int failed
= a
->info
.array
.raid_disks
;
8324 struct mdinfo
*rv
= NULL
;
8327 struct metadata_update
*mu
;
8329 struct imsm_update_activate_spare
*u
;
8334 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
8335 if ((d
->curr_state
& DS_FAULTY
) &&
8337 /* wait for Removal to happen */
8339 if (d
->state_fd
>= 0)
8343 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
8344 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
8346 if (imsm_reshape_blocks_arrays_changes(super
))
8349 /* Cannot activate another spare if rebuild is in progress already
8351 if (is_rebuilding(dev
)) {
8352 dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n");
8356 if (a
->info
.array
.level
== 4)
8357 /* No repair for takeovered array
8358 * imsm doesn't support raid4
8362 if (imsm_check_degraded(super
, dev
, failed
, MAP_0
) !=
8363 IMSM_T_STATE_DEGRADED
)
8366 if (get_imsm_map(dev
, MAP_0
)->map_state
== IMSM_T_STATE_UNINITIALIZED
) {
8367 dprintf("imsm: No spare activation allowed. Volume is not initialized.\n");
8372 * If there are any failed disks check state of the other volume.
8373 * Block rebuild if the another one is failed until failed disks
8374 * are removed from container.
8377 dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n",
8378 MAX_RAID_SERIAL_LEN
, dev
->volume
);
8379 /* check if states of the other volumes allow for rebuild */
8380 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
8382 allowed
= imsm_rebuild_allowed(a
->container
,
8390 /* For each slot, if it is not working, find a spare */
8391 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
8392 for (d
= a
->info
.devs
; d
; d
= d
->next
)
8393 if (d
->disk
.raid_disk
== i
)
8395 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
8396 if (d
&& (d
->state_fd
>= 0))
8400 * OK, this device needs recovery. Try to re-add the
8401 * previous occupant of this slot, if this fails see if
8402 * we can continue the assimilation of a spare that was
8403 * partially assimilated, finally try to activate a new
8406 dl
= imsm_readd(super
, i
, a
);
8408 dl
= imsm_add_spare(super
, i
, a
, 0, rv
);
8410 dl
= imsm_add_spare(super
, i
, a
, 1, rv
);
8414 /* found a usable disk with enough space */
8415 di
= xcalloc(1, sizeof(*di
));
8417 /* dl->index will be -1 in the case we are activating a
8418 * pristine spare. imsm_process_update() will create a
8419 * new index in this case. Once a disk is found to be
8420 * failed in all member arrays it is kicked from the
8423 di
->disk
.number
= dl
->index
;
8425 /* (ab)use di->devs to store a pointer to the device
8428 di
->devs
= (struct mdinfo
*) dl
;
8430 di
->disk
.raid_disk
= i
;
8431 di
->disk
.major
= dl
->major
;
8432 di
->disk
.minor
= dl
->minor
;
8434 di
->recovery_start
= 0;
8435 di
->data_offset
= pba_of_lba0(map
);
8436 di
->component_size
= a
->info
.component_size
;
8437 di
->container_member
= inst
;
8438 di
->bb
.supported
= 1;
8439 super
->random
= random32();
8443 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
8444 i
, di
->data_offset
);
8448 /* No spares found */
8450 /* Now 'rv' has a list of devices to return.
8451 * Create a metadata_update record to update the
8452 * disk_ord_tbl for the array
8454 mu
= xmalloc(sizeof(*mu
));
8455 mu
->buf
= xcalloc(num_spares
,
8456 sizeof(struct imsm_update_activate_spare
));
8458 mu
->space_list
= NULL
;
8459 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
8460 mu
->next
= *updates
;
8461 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
8463 for (di
= rv
; di
; di
= di
->next
) {
8464 u
->type
= update_activate_spare
;
8465 u
->dl
= (struct dl
*) di
->devs
;
8467 u
->slot
= di
->disk
.raid_disk
;
8478 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
8480 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
8481 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8482 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, MAP_0
);
8483 struct disk_info
*inf
= get_disk_info(u
);
8484 struct imsm_disk
*disk
;
8488 for (i
= 0; i
< map
->num_members
; i
++) {
8489 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, MAP_X
));
8490 for (j
= 0; j
< new_map
->num_members
; j
++)
8491 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
8498 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
8502 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
8503 if (dl
->major
== major
&& dl
->minor
== minor
)
8508 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
8514 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
8515 if (dl
->major
== major
&& dl
->minor
== minor
) {
8518 prev
->next
= dl
->next
;
8520 super
->disks
= dl
->next
;
8522 __free_imsm_disk(dl
);
8523 dprintf("removed %x:%x\n", major
, minor
);
8531 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
8533 static int add_remove_disk_update(struct intel_super
*super
)
8535 int check_degraded
= 0;
8538 /* add/remove some spares to/from the metadata/contrainer */
8539 while (super
->disk_mgmt_list
) {
8540 struct dl
*disk_cfg
;
8542 disk_cfg
= super
->disk_mgmt_list
;
8543 super
->disk_mgmt_list
= disk_cfg
->next
;
8544 disk_cfg
->next
= NULL
;
8546 if (disk_cfg
->action
== DISK_ADD
) {
8547 disk_cfg
->next
= super
->disks
;
8548 super
->disks
= disk_cfg
;
8550 dprintf("added %x:%x\n",
8551 disk_cfg
->major
, disk_cfg
->minor
);
8552 } else if (disk_cfg
->action
== DISK_REMOVE
) {
8553 dprintf("Disk remove action processed: %x.%x\n",
8554 disk_cfg
->major
, disk_cfg
->minor
);
8555 disk
= get_disk_super(super
,
8559 /* store action status */
8560 disk
->action
= DISK_REMOVE
;
8561 /* remove spare disks only */
8562 if (disk
->index
== -1) {
8563 remove_disk_super(super
,
8568 /* release allocate disk structure */
8569 __free_imsm_disk(disk_cfg
);
8572 return check_degraded
;
8575 static int apply_reshape_migration_update(struct imsm_update_reshape_migration
*u
,
8576 struct intel_super
*super
,
8579 struct intel_dev
*id
;
8580 void **tofree
= NULL
;
8583 dprintf("(enter)\n");
8584 if (u
->subdev
< 0 || u
->subdev
> 1) {
8585 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
8588 if (space_list
== NULL
|| *space_list
== NULL
) {
8589 dprintf("imsm: Error: Memory is not allocated\n");
8593 for (id
= super
->devlist
; id
; id
= id
->next
) {
8594 if (id
->index
== (unsigned)u
->subdev
) {
8595 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
8596 struct imsm_map
*map
;
8597 struct imsm_dev
*new_dev
=
8598 (struct imsm_dev
*)*space_list
;
8599 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
8601 struct dl
*new_disk
;
8603 if (new_dev
== NULL
)
8605 *space_list
= **space_list
;
8606 memcpy(new_dev
, dev
, sizeof_imsm_dev(dev
, 0));
8607 map
= get_imsm_map(new_dev
, MAP_0
);
8609 dprintf("imsm: Error: migration in progress");
8613 to_state
= map
->map_state
;
8614 if ((u
->new_level
== 5) && (map
->raid_level
== 0)) {
8616 /* this should not happen */
8617 if (u
->new_disks
[0] < 0) {
8618 map
->failed_disk_num
=
8619 map
->num_members
- 1;
8620 to_state
= IMSM_T_STATE_DEGRADED
;
8622 to_state
= IMSM_T_STATE_NORMAL
;
8624 migrate(new_dev
, super
, to_state
, MIGR_GEN_MIGR
);
8625 if (u
->new_level
> -1)
8626 map
->raid_level
= u
->new_level
;
8627 migr_map
= get_imsm_map(new_dev
, MAP_1
);
8628 if ((u
->new_level
== 5) &&
8629 (migr_map
->raid_level
== 0)) {
8630 int ord
= map
->num_members
- 1;
8631 migr_map
->num_members
--;
8632 if (u
->new_disks
[0] < 0)
8633 ord
|= IMSM_ORD_REBUILD
;
8634 set_imsm_ord_tbl_ent(map
,
8635 map
->num_members
- 1,
8639 tofree
= (void **)dev
;
8641 /* update chunk size
8643 if (u
->new_chunksize
> 0) {
8644 unsigned long long num_data_stripes
;
8646 imsm_num_data_members(dev
, MAP_0
);
8648 if (used_disks
== 0)
8651 map
->blocks_per_strip
=
8652 __cpu_to_le16(u
->new_chunksize
* 2);
8654 (join_u32(dev
->size_low
, dev
->size_high
)
8656 num_data_stripes
/= map
->blocks_per_strip
;
8657 num_data_stripes
/= map
->num_domains
;
8658 set_num_data_stripes(map
, num_data_stripes
);
8663 if (u
->new_level
!= 5 || migr_map
->raid_level
!= 0 ||
8664 migr_map
->raid_level
== map
->raid_level
)
8667 if (u
->new_disks
[0] >= 0) {
8670 new_disk
= get_disk_super(super
,
8671 major(u
->new_disks
[0]),
8672 minor(u
->new_disks
[0]));
8673 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
8674 major(u
->new_disks
[0]),
8675 minor(u
->new_disks
[0]),
8676 new_disk
, new_disk
->index
);
8677 if (new_disk
== NULL
)
8678 goto error_disk_add
;
8680 new_disk
->index
= map
->num_members
- 1;
8681 /* slot to fill in autolayout
8683 new_disk
->raiddisk
= new_disk
->index
;
8684 new_disk
->disk
.status
|= CONFIGURED_DISK
;
8685 new_disk
->disk
.status
&= ~SPARE_DISK
;
8687 goto error_disk_add
;
8690 *tofree
= *space_list
;
8691 /* calculate new size
8693 imsm_set_array_size(new_dev
, -1);
8700 *space_list
= tofree
;
8704 dprintf("Error: imsm: Cannot find disk.\n");
8708 static int apply_size_change_update(struct imsm_update_size_change
*u
,
8709 struct intel_super
*super
)
8711 struct intel_dev
*id
;
8714 dprintf("(enter)\n");
8715 if (u
->subdev
< 0 || u
->subdev
> 1) {
8716 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
8720 for (id
= super
->devlist
; id
; id
= id
->next
) {
8721 if (id
->index
== (unsigned)u
->subdev
) {
8722 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
8723 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8724 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
8725 unsigned long long blocks_per_member
;
8726 unsigned long long num_data_stripes
;
8728 /* calculate new size
8730 blocks_per_member
= u
->new_size
/ used_disks
;
8731 num_data_stripes
= blocks_per_member
/
8732 map
->blocks_per_strip
;
8733 num_data_stripes
/= map
->num_domains
;
8734 dprintf("(size: %llu, blocks per member: %llu, num_data_stipes: %llu)\n",
8735 u
->new_size
, blocks_per_member
,
8737 set_blocks_per_member(map
, blocks_per_member
);
8738 set_num_data_stripes(map
, num_data_stripes
);
8739 imsm_set_array_size(dev
, u
->new_size
);
8749 static int apply_update_activate_spare(struct imsm_update_activate_spare
*u
,
8750 struct intel_super
*super
,
8751 struct active_array
*active_array
)
8753 struct imsm_super
*mpb
= super
->anchor
;
8754 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
8755 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8756 struct imsm_map
*migr_map
;
8757 struct active_array
*a
;
8758 struct imsm_disk
*disk
;
8765 int second_map_created
= 0;
8767 for (; u
; u
= u
->next
) {
8768 victim
= get_imsm_disk_idx(dev
, u
->slot
, MAP_X
);
8773 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
8778 pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n",
8783 /* count failures (excluding rebuilds and the victim)
8784 * to determine map[0] state
8787 for (i
= 0; i
< map
->num_members
; i
++) {
8790 disk
= get_imsm_disk(super
,
8791 get_imsm_disk_idx(dev
, i
, MAP_X
));
8792 if (!disk
|| is_failed(disk
))
8796 /* adding a pristine spare, assign a new index */
8797 if (dl
->index
< 0) {
8798 dl
->index
= super
->anchor
->num_disks
;
8799 super
->anchor
->num_disks
++;
8802 disk
->status
|= CONFIGURED_DISK
;
8803 disk
->status
&= ~SPARE_DISK
;
8806 to_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8807 if (!second_map_created
) {
8808 second_map_created
= 1;
8809 map
->map_state
= IMSM_T_STATE_DEGRADED
;
8810 migrate(dev
, super
, to_state
, MIGR_REBUILD
);
8812 map
->map_state
= to_state
;
8813 migr_map
= get_imsm_map(dev
, MAP_1
);
8814 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
8815 set_imsm_ord_tbl_ent(migr_map
, u
->slot
,
8816 dl
->index
| IMSM_ORD_REBUILD
);
8818 /* update the family_num to mark a new container
8819 * generation, being careful to record the existing
8820 * family_num in orig_family_num to clean up after
8821 * earlier mdadm versions that neglected to set it.
8823 if (mpb
->orig_family_num
== 0)
8824 mpb
->orig_family_num
= mpb
->family_num
;
8825 mpb
->family_num
+= super
->random
;
8827 /* count arrays using the victim in the metadata */
8829 for (a
= active_array
; a
; a
= a
->next
) {
8830 dev
= get_imsm_dev(super
, a
->info
.container_member
);
8831 map
= get_imsm_map(dev
, MAP_0
);
8833 if (get_imsm_disk_slot(map
, victim
) >= 0)
8837 /* delete the victim if it is no longer being
8843 /* We know that 'manager' isn't touching anything,
8844 * so it is safe to delete
8846 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
8847 if ((*dlp
)->index
== victim
)
8850 /* victim may be on the missing list */
8852 for (dlp
= &super
->missing
; *dlp
;
8853 dlp
= &(*dlp
)->next
)
8854 if ((*dlp
)->index
== victim
)
8856 imsm_delete(super
, dlp
, victim
);
8863 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
8864 struct intel_super
*super
,
8867 struct dl
*new_disk
;
8868 struct intel_dev
*id
;
8870 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
8871 int disk_count
= u
->old_raid_disks
;
8872 void **tofree
= NULL
;
8873 int devices_to_reshape
= 1;
8874 struct imsm_super
*mpb
= super
->anchor
;
8876 unsigned int dev_id
;
8878 dprintf("(enter)\n");
8880 /* enable spares to use in array */
8881 for (i
= 0; i
< delta_disks
; i
++) {
8882 new_disk
= get_disk_super(super
,
8883 major(u
->new_disks
[i
]),
8884 minor(u
->new_disks
[i
]));
8885 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
8886 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
8887 new_disk
, new_disk
->index
);
8888 if (new_disk
== NULL
||
8889 (new_disk
->index
>= 0 &&
8890 new_disk
->index
< u
->old_raid_disks
))
8891 goto update_reshape_exit
;
8892 new_disk
->index
= disk_count
++;
8893 /* slot to fill in autolayout
8895 new_disk
->raiddisk
= new_disk
->index
;
8896 new_disk
->disk
.status
|=
8898 new_disk
->disk
.status
&= ~SPARE_DISK
;
8901 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
8902 mpb
->num_raid_devs
);
8903 /* manage changes in volume
8905 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
8906 void **sp
= *space_list
;
8907 struct imsm_dev
*newdev
;
8908 struct imsm_map
*newmap
, *oldmap
;
8910 for (id
= super
->devlist
; id
; id
= id
->next
) {
8911 if (id
->index
== dev_id
)
8920 /* Copy the dev, but not (all of) the map */
8921 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
8922 oldmap
= get_imsm_map(id
->dev
, MAP_0
);
8923 newmap
= get_imsm_map(newdev
, MAP_0
);
8924 /* Copy the current map */
8925 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8926 /* update one device only
8928 if (devices_to_reshape
) {
8929 dprintf("imsm: modifying subdev: %i\n",
8931 devices_to_reshape
--;
8932 newdev
->vol
.migr_state
= 1;
8933 newdev
->vol
.curr_migr_unit
= 0;
8934 set_migr_type(newdev
, MIGR_GEN_MIGR
);
8935 newmap
->num_members
= u
->new_raid_disks
;
8936 for (i
= 0; i
< delta_disks
; i
++) {
8937 set_imsm_ord_tbl_ent(newmap
,
8938 u
->old_raid_disks
+ i
,
8939 u
->old_raid_disks
+ i
);
8941 /* New map is correct, now need to save old map
8943 newmap
= get_imsm_map(newdev
, MAP_1
);
8944 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8946 imsm_set_array_size(newdev
, -1);
8949 sp
= (void **)id
->dev
;
8954 /* Clear migration record */
8955 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
8958 *space_list
= tofree
;
8961 update_reshape_exit
:
8966 static int apply_takeover_update(struct imsm_update_takeover
*u
,
8967 struct intel_super
*super
,
8970 struct imsm_dev
*dev
= NULL
;
8971 struct intel_dev
*dv
;
8972 struct imsm_dev
*dev_new
;
8973 struct imsm_map
*map
;
8977 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
8978 if (dv
->index
== (unsigned int)u
->subarray
) {
8986 map
= get_imsm_map(dev
, MAP_0
);
8988 if (u
->direction
== R10_TO_R0
) {
8989 unsigned long long num_data_stripes
;
8991 map
->num_domains
= 1;
8992 num_data_stripes
= blocks_per_member(map
);
8993 num_data_stripes
/= map
->blocks_per_strip
;
8994 num_data_stripes
/= map
->num_domains
;
8995 set_num_data_stripes(map
, num_data_stripes
);
8997 /* Number of failed disks must be half of initial disk number */
8998 if (imsm_count_failed(super
, dev
, MAP_0
) !=
8999 (map
->num_members
/ 2))
9002 /* iterate through devices to mark removed disks as spare */
9003 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
9004 if (dm
->disk
.status
& FAILED_DISK
) {
9005 int idx
= dm
->index
;
9006 /* update indexes on the disk list */
9007 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
9008 the index values will end up being correct.... NB */
9009 for (du
= super
->disks
; du
; du
= du
->next
)
9010 if (du
->index
> idx
)
9012 /* mark as spare disk */
9017 map
->num_members
= map
->num_members
/ 2;
9018 map
->map_state
= IMSM_T_STATE_NORMAL
;
9019 map
->num_domains
= 1;
9020 map
->raid_level
= 0;
9021 map
->failed_disk_num
= -1;
9024 if (u
->direction
== R0_TO_R10
) {
9026 /* update slots in current disk list */
9027 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
9031 /* create new *missing* disks */
9032 for (i
= 0; i
< map
->num_members
; i
++) {
9033 space
= *space_list
;
9036 *space_list
= *space
;
9038 memcpy(du
, super
->disks
, sizeof(*du
));
9042 du
->index
= (i
* 2) + 1;
9043 sprintf((char *)du
->disk
.serial
,
9044 " MISSING_%d", du
->index
);
9045 sprintf((char *)du
->serial
,
9046 "MISSING_%d", du
->index
);
9047 du
->next
= super
->missing
;
9048 super
->missing
= du
;
9050 /* create new dev and map */
9051 space
= *space_list
;
9054 *space_list
= *space
;
9055 dev_new
= (void *)space
;
9056 memcpy(dev_new
, dev
, sizeof(*dev
));
9057 /* update new map */
9058 map
= get_imsm_map(dev_new
, MAP_0
);
9059 map
->num_members
= map
->num_members
* 2;
9060 map
->map_state
= IMSM_T_STATE_DEGRADED
;
9061 map
->num_domains
= 2;
9062 map
->raid_level
= 1;
9063 /* replace dev<->dev_new */
9066 /* update disk order table */
9067 for (du
= super
->disks
; du
; du
= du
->next
)
9069 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
9070 for (du
= super
->missing
; du
; du
= du
->next
)
9071 if (du
->index
>= 0) {
9072 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
9073 mark_missing(super
, dv
->dev
, &du
->disk
, du
->index
);
9079 static void imsm_process_update(struct supertype
*st
,
9080 struct metadata_update
*update
)
9083 * crack open the metadata_update envelope to find the update record
9084 * update can be one of:
9085 * update_reshape_container_disks - all the arrays in the container
9086 * are being reshaped to have more devices. We need to mark
9087 * the arrays for general migration and convert selected spares
9088 * into active devices.
9089 * update_activate_spare - a spare device has replaced a failed
9090 * device in an array, update the disk_ord_tbl. If this disk is
9091 * present in all member arrays then also clear the SPARE_DISK
9093 * update_create_array
9095 * update_rename_array
9096 * update_add_remove_disk
9098 struct intel_super
*super
= st
->sb
;
9099 struct imsm_super
*mpb
;
9100 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
9102 /* update requires a larger buf but the allocation failed */
9103 if (super
->next_len
&& !super
->next_buf
) {
9104 super
->next_len
= 0;
9108 if (super
->next_buf
) {
9109 memcpy(super
->next_buf
, super
->buf
, super
->len
);
9111 super
->len
= super
->next_len
;
9112 super
->buf
= super
->next_buf
;
9114 super
->next_len
= 0;
9115 super
->next_buf
= NULL
;
9118 mpb
= super
->anchor
;
9121 case update_general_migration_checkpoint
: {
9122 struct intel_dev
*id
;
9123 struct imsm_update_general_migration_checkpoint
*u
=
9124 (void *)update
->buf
;
9126 dprintf("called for update_general_migration_checkpoint\n");
9128 /* find device under general migration */
9129 for (id
= super
->devlist
; id
; id
= id
->next
) {
9130 if (is_gen_migration(id
->dev
)) {
9131 id
->dev
->vol
.curr_migr_unit
=
9132 __cpu_to_le32(u
->curr_migr_unit
);
9133 super
->updates_pending
++;
9138 case update_takeover
: {
9139 struct imsm_update_takeover
*u
= (void *)update
->buf
;
9140 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
9141 imsm_update_version_info(super
);
9142 super
->updates_pending
++;
9147 case update_reshape_container_disks
: {
9148 struct imsm_update_reshape
*u
= (void *)update
->buf
;
9149 if (apply_reshape_container_disks_update(
9150 u
, super
, &update
->space_list
))
9151 super
->updates_pending
++;
9154 case update_reshape_migration
: {
9155 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
9156 if (apply_reshape_migration_update(
9157 u
, super
, &update
->space_list
))
9158 super
->updates_pending
++;
9161 case update_size_change
: {
9162 struct imsm_update_size_change
*u
= (void *)update
->buf
;
9163 if (apply_size_change_update(u
, super
))
9164 super
->updates_pending
++;
9167 case update_activate_spare
: {
9168 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
9169 if (apply_update_activate_spare(u
, super
, st
->arrays
))
9170 super
->updates_pending
++;
9173 case update_create_array
: {
9174 /* someone wants to create a new array, we need to be aware of
9175 * a few races/collisions:
9176 * 1/ 'Create' called by two separate instances of mdadm
9177 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
9178 * devices that have since been assimilated via
9180 * In the event this update can not be carried out mdadm will
9181 * (FIX ME) notice that its update did not take hold.
9183 struct imsm_update_create_array
*u
= (void *) update
->buf
;
9184 struct intel_dev
*dv
;
9185 struct imsm_dev
*dev
;
9186 struct imsm_map
*map
, *new_map
;
9187 unsigned long long start
, end
;
9188 unsigned long long new_start
, new_end
;
9190 struct disk_info
*inf
;
9193 /* handle racing creates: first come first serve */
9194 if (u
->dev_idx
< mpb
->num_raid_devs
) {
9195 dprintf("subarray %d already defined\n", u
->dev_idx
);
9199 /* check update is next in sequence */
9200 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
9201 dprintf("can not create array %d expected index %d\n",
9202 u
->dev_idx
, mpb
->num_raid_devs
);
9206 new_map
= get_imsm_map(&u
->dev
, MAP_0
);
9207 new_start
= pba_of_lba0(new_map
);
9208 new_end
= new_start
+ blocks_per_member(new_map
);
9209 inf
= get_disk_info(u
);
9211 /* handle activate_spare versus create race:
9212 * check to make sure that overlapping arrays do not include
9215 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
9216 dev
= get_imsm_dev(super
, i
);
9217 map
= get_imsm_map(dev
, MAP_0
);
9218 start
= pba_of_lba0(map
);
9219 end
= start
+ blocks_per_member(map
);
9220 if ((new_start
>= start
&& new_start
<= end
) ||
9221 (start
>= new_start
&& start
<= new_end
))
9226 if (disks_overlap(super
, i
, u
)) {
9227 dprintf("arrays overlap\n");
9232 /* check that prepare update was successful */
9233 if (!update
->space
) {
9234 dprintf("prepare update failed\n");
9238 /* check that all disks are still active before committing
9239 * changes. FIXME: could we instead handle this by creating a
9240 * degraded array? That's probably not what the user expects,
9241 * so better to drop this update on the floor.
9243 for (i
= 0; i
< new_map
->num_members
; i
++) {
9244 dl
= serial_to_dl(inf
[i
].serial
, super
);
9246 dprintf("disk disappeared\n");
9251 super
->updates_pending
++;
9253 /* convert spares to members and fixup ord_tbl */
9254 for (i
= 0; i
< new_map
->num_members
; i
++) {
9255 dl
= serial_to_dl(inf
[i
].serial
, super
);
9256 if (dl
->index
== -1) {
9257 dl
->index
= mpb
->num_disks
;
9259 dl
->disk
.status
|= CONFIGURED_DISK
;
9260 dl
->disk
.status
&= ~SPARE_DISK
;
9262 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
9267 update
->space
= NULL
;
9268 imsm_copy_dev(dev
, &u
->dev
);
9269 dv
->index
= u
->dev_idx
;
9270 dv
->next
= super
->devlist
;
9271 super
->devlist
= dv
;
9272 mpb
->num_raid_devs
++;
9274 imsm_update_version_info(super
);
9277 /* mdmon knows how to release update->space, but not
9278 * ((struct intel_dev *) update->space)->dev
9280 if (update
->space
) {
9286 case update_kill_array
: {
9287 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
9288 int victim
= u
->dev_idx
;
9289 struct active_array
*a
;
9290 struct intel_dev
**dp
;
9291 struct imsm_dev
*dev
;
9293 /* sanity check that we are not affecting the uuid of
9294 * active arrays, or deleting an active array
9296 * FIXME when immutable ids are available, but note that
9297 * we'll also need to fixup the invalidated/active
9298 * subarray indexes in mdstat
9300 for (a
= st
->arrays
; a
; a
= a
->next
)
9301 if (a
->info
.container_member
>= victim
)
9303 /* by definition if mdmon is running at least one array
9304 * is active in the container, so checking
9305 * mpb->num_raid_devs is just extra paranoia
9307 dev
= get_imsm_dev(super
, victim
);
9308 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
9309 dprintf("failed to delete subarray-%d\n", victim
);
9313 for (dp
= &super
->devlist
; *dp
;)
9314 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
9317 if ((*dp
)->index
> (unsigned)victim
)
9321 mpb
->num_raid_devs
--;
9322 super
->updates_pending
++;
9325 case update_rename_array
: {
9326 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
9327 char name
[MAX_RAID_SERIAL_LEN
+1];
9328 int target
= u
->dev_idx
;
9329 struct active_array
*a
;
9330 struct imsm_dev
*dev
;
9332 /* sanity check that we are not affecting the uuid of
9335 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
9336 name
[MAX_RAID_SERIAL_LEN
] = '\0';
9337 for (a
= st
->arrays
; a
; a
= a
->next
)
9338 if (a
->info
.container_member
== target
)
9340 dev
= get_imsm_dev(super
, u
->dev_idx
);
9341 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
9342 dprintf("failed to rename subarray-%d\n", target
);
9346 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
9347 super
->updates_pending
++;
9350 case update_add_remove_disk
: {
9351 /* we may be able to repair some arrays if disks are
9352 * being added, check the status of add_remove_disk
9353 * if discs has been added.
9355 if (add_remove_disk_update(super
)) {
9356 struct active_array
*a
;
9358 super
->updates_pending
++;
9359 for (a
= st
->arrays
; a
; a
= a
->next
)
9360 a
->check_degraded
= 1;
9364 case update_prealloc_badblocks_mem
:
9367 pr_err("error: unsuported process update type:(type: %d)\n", type
);
9371 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
);
9373 static int imsm_prepare_update(struct supertype
*st
,
9374 struct metadata_update
*update
)
9377 * Allocate space to hold new disk entries, raid-device entries or a new
9378 * mpb if necessary. The manager synchronously waits for updates to
9379 * complete in the monitor, so new mpb buffers allocated here can be
9380 * integrated by the monitor thread without worrying about live pointers
9381 * in the manager thread.
9383 enum imsm_update_type type
;
9384 struct intel_super
*super
= st
->sb
;
9385 unsigned int sector_size
= super
->sector_size
;
9386 struct imsm_super
*mpb
= super
->anchor
;
9390 if (update
->len
< (int)sizeof(type
))
9393 type
= *(enum imsm_update_type
*) update
->buf
;
9396 case update_general_migration_checkpoint
:
9397 if (update
->len
< (int)sizeof(struct imsm_update_general_migration_checkpoint
))
9399 dprintf("called for update_general_migration_checkpoint\n");
9401 case update_takeover
: {
9402 struct imsm_update_takeover
*u
= (void *)update
->buf
;
9403 if (update
->len
< (int)sizeof(*u
))
9405 if (u
->direction
== R0_TO_R10
) {
9406 void **tail
= (void **)&update
->space_list
;
9407 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
9408 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9409 int num_members
= map
->num_members
;
9412 /* allocate memory for added disks */
9413 for (i
= 0; i
< num_members
; i
++) {
9414 size
= sizeof(struct dl
);
9415 space
= xmalloc(size
);
9420 /* allocate memory for new device */
9421 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
9422 (num_members
* sizeof(__u32
));
9423 space
= xmalloc(size
);
9427 len
= disks_to_mpb_size(num_members
* 2);
9432 case update_reshape_container_disks
: {
9433 /* Every raid device in the container is about to
9434 * gain some more devices, and we will enter a
9436 * So each 'imsm_map' will be bigger, and the imsm_vol
9437 * will now hold 2 of them.
9438 * Thus we need new 'struct imsm_dev' allocations sized
9439 * as sizeof_imsm_dev but with more devices in both maps.
9441 struct imsm_update_reshape
*u
= (void *)update
->buf
;
9442 struct intel_dev
*dl
;
9443 void **space_tail
= (void**)&update
->space_list
;
9445 if (update
->len
< (int)sizeof(*u
))
9448 dprintf("for update_reshape\n");
9450 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
9451 int size
= sizeof_imsm_dev(dl
->dev
, 1);
9453 if (u
->new_raid_disks
> u
->old_raid_disks
)
9454 size
+= sizeof(__u32
)*2*
9455 (u
->new_raid_disks
- u
->old_raid_disks
);
9462 len
= disks_to_mpb_size(u
->new_raid_disks
);
9463 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
9466 case update_reshape_migration
: {
9467 /* for migration level 0->5 we need to add disks
9468 * so the same as for container operation we will copy
9469 * device to the bigger location.
9470 * in memory prepared device and new disk area are prepared
9471 * for usage in process update
9473 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
9474 struct intel_dev
*id
;
9475 void **space_tail
= (void **)&update
->space_list
;
9478 int current_level
= -1;
9480 if (update
->len
< (int)sizeof(*u
))
9483 dprintf("for update_reshape\n");
9485 /* add space for bigger array in update
9487 for (id
= super
->devlist
; id
; id
= id
->next
) {
9488 if (id
->index
== (unsigned)u
->subdev
) {
9489 size
= sizeof_imsm_dev(id
->dev
, 1);
9490 if (u
->new_raid_disks
> u
->old_raid_disks
)
9491 size
+= sizeof(__u32
)*2*
9492 (u
->new_raid_disks
- u
->old_raid_disks
);
9500 if (update
->space_list
== NULL
)
9503 /* add space for disk in update
9505 size
= sizeof(struct dl
);
9511 /* add spare device to update
9513 for (id
= super
->devlist
; id
; id
= id
->next
)
9514 if (id
->index
== (unsigned)u
->subdev
) {
9515 struct imsm_dev
*dev
;
9516 struct imsm_map
*map
;
9518 dev
= get_imsm_dev(super
, u
->subdev
);
9519 map
= get_imsm_map(dev
, MAP_0
);
9520 current_level
= map
->raid_level
;
9523 if (u
->new_level
== 5 && u
->new_level
!= current_level
) {
9524 struct mdinfo
*spares
;
9526 spares
= get_spares_for_grow(st
);
9534 makedev(dev
->disk
.major
,
9536 dl
= get_disk_super(super
,
9539 dl
->index
= u
->old_raid_disks
;
9545 len
= disks_to_mpb_size(u
->new_raid_disks
);
9546 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
9549 case update_size_change
: {
9550 if (update
->len
< (int)sizeof(struct imsm_update_size_change
))
9554 case update_activate_spare
: {
9555 if (update
->len
< (int)sizeof(struct imsm_update_activate_spare
))
9559 case update_create_array
: {
9560 struct imsm_update_create_array
*u
= (void *) update
->buf
;
9561 struct intel_dev
*dv
;
9562 struct imsm_dev
*dev
= &u
->dev
;
9563 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9565 struct disk_info
*inf
;
9569 if (update
->len
< (int)sizeof(*u
))
9572 inf
= get_disk_info(u
);
9573 len
= sizeof_imsm_dev(dev
, 1);
9574 /* allocate a new super->devlist entry */
9575 dv
= xmalloc(sizeof(*dv
));
9576 dv
->dev
= xmalloc(len
);
9579 /* count how many spares will be converted to members */
9580 for (i
= 0; i
< map
->num_members
; i
++) {
9581 dl
= serial_to_dl(inf
[i
].serial
, super
);
9583 /* hmm maybe it failed?, nothing we can do about
9588 if (count_memberships(dl
, super
) == 0)
9591 len
+= activate
* sizeof(struct imsm_disk
);
9594 case update_kill_array
: {
9595 if (update
->len
< (int)sizeof(struct imsm_update_kill_array
))
9599 case update_rename_array
: {
9600 if (update
->len
< (int)sizeof(struct imsm_update_rename_array
))
9604 case update_add_remove_disk
:
9605 /* no update->len needed */
9607 case update_prealloc_badblocks_mem
:
9608 super
->extra_space
+= sizeof(struct bbm_log
) -
9609 get_imsm_bbm_log_size(super
->bbm_log
);
9615 /* check if we need a larger metadata buffer */
9616 if (super
->next_buf
)
9617 buf_len
= super
->next_len
;
9619 buf_len
= super
->len
;
9621 if (__le32_to_cpu(mpb
->mpb_size
) + super
->extra_space
+ len
> buf_len
) {
9622 /* ok we need a larger buf than what is currently allocated
9623 * if this allocation fails process_update will notice that
9624 * ->next_len is set and ->next_buf is NULL
9626 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) +
9627 super
->extra_space
+ len
, sector_size
);
9628 if (super
->next_buf
)
9629 free(super
->next_buf
);
9631 super
->next_len
= buf_len
;
9632 if (posix_memalign(&super
->next_buf
, sector_size
, buf_len
) == 0)
9633 memset(super
->next_buf
, 0, buf_len
);
9635 super
->next_buf
= NULL
;
9640 /* must be called while manager is quiesced */
9641 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
9643 struct imsm_super
*mpb
= super
->anchor
;
9645 struct imsm_dev
*dev
;
9646 struct imsm_map
*map
;
9647 unsigned int i
, j
, num_members
;
9649 struct bbm_log
*log
= super
->bbm_log
;
9651 dprintf("deleting device[%d] from imsm_super\n", index
);
9653 /* shift all indexes down one */
9654 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
9655 if (iter
->index
> (int)index
)
9657 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
9658 if (iter
->index
> (int)index
)
9661 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
9662 dev
= get_imsm_dev(super
, i
);
9663 map
= get_imsm_map(dev
, MAP_0
);
9664 num_members
= map
->num_members
;
9665 for (j
= 0; j
< num_members
; j
++) {
9666 /* update ord entries being careful not to propagate
9667 * ord-flags to the first map
9669 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_X
);
9671 if (ord_to_idx(ord
) <= index
)
9674 map
= get_imsm_map(dev
, MAP_0
);
9675 set_imsm_ord_tbl_ent(map
, j
, ord_to_idx(ord
- 1));
9676 map
= get_imsm_map(dev
, MAP_1
);
9678 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
9682 for (i
= 0; i
< log
->entry_count
; i
++) {
9683 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[i
];
9685 if (entry
->disk_ordinal
<= index
)
9687 entry
->disk_ordinal
--;
9691 super
->updates_pending
++;
9693 struct dl
*dl
= *dlp
;
9695 *dlp
= (*dlp
)->next
;
9696 __free_imsm_disk(dl
);
9699 #endif /* MDASSEMBLE */
9701 static void close_targets(int *targets
, int new_disks
)
9708 for (i
= 0; i
< new_disks
; i
++) {
9709 if (targets
[i
] >= 0) {
9716 static int imsm_get_allowed_degradation(int level
, int raid_disks
,
9717 struct intel_super
*super
,
9718 struct imsm_dev
*dev
)
9724 struct imsm_map
*map
;
9727 ret_val
= raid_disks
/2;
9728 /* check map if all disks pairs not failed
9731 map
= get_imsm_map(dev
, MAP_0
);
9732 for (i
= 0; i
< ret_val
; i
++) {
9733 int degradation
= 0;
9734 if (get_imsm_disk(super
, i
) == NULL
)
9736 if (get_imsm_disk(super
, i
+ 1) == NULL
)
9738 if (degradation
== 2)
9741 map
= get_imsm_map(dev
, MAP_1
);
9742 /* if there is no second map
9743 * result can be returned
9747 /* check degradation in second map
9749 for (i
= 0; i
< ret_val
; i
++) {
9750 int degradation
= 0;
9751 if (get_imsm_disk(super
, i
) == NULL
)
9753 if (get_imsm_disk(super
, i
+ 1) == NULL
)
9755 if (degradation
== 2)
9769 /*******************************************************************************
9770 * Function: open_backup_targets
9771 * Description: Function opens file descriptors for all devices given in
9774 * info : general array info
9775 * raid_disks : number of disks
9776 * raid_fds : table of device's file descriptors
9777 * super : intel super for raid10 degradation check
9778 * dev : intel device for raid10 degradation check
9782 ******************************************************************************/
9783 int open_backup_targets(struct mdinfo
*info
, int raid_disks
, int *raid_fds
,
9784 struct intel_super
*super
, struct imsm_dev
*dev
)
9790 for (i
= 0; i
< raid_disks
; i
++)
9793 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
9796 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
)) {
9797 dprintf("disk is faulty!!\n");
9801 if (sd
->disk
.raid_disk
>= raid_disks
|| sd
->disk
.raid_disk
< 0)
9804 dn
= map_dev(sd
->disk
.major
,
9806 raid_fds
[sd
->disk
.raid_disk
] = dev_open(dn
, O_RDWR
);
9807 if (raid_fds
[sd
->disk
.raid_disk
] < 0) {
9808 pr_err("cannot open component\n");
9813 /* check if maximum array degradation level is not exceeded
9815 if ((raid_disks
- opened
) >
9816 imsm_get_allowed_degradation(info
->new_level
, raid_disks
,
9818 pr_err("Not enough disks can be opened.\n");
9819 close_targets(raid_fds
, raid_disks
);
9825 /*******************************************************************************
9826 * Function: validate_container_imsm
9827 * Description: This routine validates container after assemble,
9828 * eg. if devices in container are under the same controller.
9831 * info : linked list with info about devices used in array
9835 ******************************************************************************/
9836 int validate_container_imsm(struct mdinfo
*info
)
9838 if (check_env("IMSM_NO_PLATFORM"))
9841 struct sys_dev
*idev
;
9842 struct sys_dev
*hba
= NULL
;
9843 struct sys_dev
*intel_devices
= find_intel_devices();
9844 char *dev_path
= devt_to_devpath(makedev(info
->disk
.major
,
9847 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
9848 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
9857 pr_err("WARNING - Cannot detect HBA for device %s!\n",
9858 devid2kname(makedev(info
->disk
.major
, info
->disk
.minor
)));
9862 const struct imsm_orom
*orom
= get_orom_by_device_id(hba
->dev_id
);
9865 for (dev
= info
->next
; dev
; dev
= dev
->next
) {
9866 dev_path
= devt_to_devpath(makedev(dev
->disk
.major
, dev
->disk
.minor
));
9868 struct sys_dev
*hba2
= NULL
;
9869 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
9870 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
9878 const struct imsm_orom
*orom2
= hba2
== NULL
? NULL
:
9879 get_orom_by_device_id(hba2
->dev_id
);
9881 if (hba2
&& hba
->type
!= hba2
->type
) {
9882 pr_err("WARNING - HBAs of devices do not match %s != %s\n",
9883 get_sys_dev_type(hba
->type
), get_sys_dev_type(hba2
->type
));
9887 if (orom
!= orom2
) {
9888 pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n"
9889 " This operation is not supported and can lead to data loss.\n");
9894 pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n"
9895 " This operation is not supported and can lead to data loss.\n");
9903 /*******************************************************************************
9904 * Function: imsm_record_badblock
9905 * Description: This routine stores new bad block record in BBM log
9908 * a : array containing a bad block
9909 * slot : disk number containing a bad block
9910 * sector : bad block sector
9911 * length : bad block sectors range
9915 ******************************************************************************/
9916 static int imsm_record_badblock(struct active_array
*a
, int slot
,
9917 unsigned long long sector
, int length
)
9919 struct intel_super
*super
= a
->container
->sb
;
9923 ord
= imsm_disk_slot_to_ord(a
, slot
);
9927 ret
= record_new_badblock(super
->bbm_log
, ord_to_idx(ord
), sector
,
9930 super
->updates_pending
++;
9934 /*******************************************************************************
9935 * Function: imsm_clear_badblock
9936 * Description: This routine clears bad block record from BBM log
9939 * a : array containing a bad block
9940 * slot : disk number containing a bad block
9941 * sector : bad block sector
9942 * length : bad block sectors range
9946 ******************************************************************************/
9947 static int imsm_clear_badblock(struct active_array
*a
, int slot
,
9948 unsigned long long sector
, int length
)
9950 struct intel_super
*super
= a
->container
->sb
;
9954 ord
= imsm_disk_slot_to_ord(a
, slot
);
9958 ret
= clear_badblock(super
->bbm_log
, ord_to_idx(ord
), sector
, length
);
9960 super
->updates_pending
++;
9964 /*******************************************************************************
9965 * Function: imsm_get_badblocks
9966 * Description: This routine get list of bad blocks for an array
9970 * slot : disk number
9972 * bb : structure containing bad blocks
9974 ******************************************************************************/
9975 static struct md_bb
*imsm_get_badblocks(struct active_array
*a
, int slot
)
9977 int inst
= a
->info
.container_member
;
9978 struct intel_super
*super
= a
->container
->sb
;
9979 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
9980 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9983 ord
= imsm_disk_slot_to_ord(a
, slot
);
9987 get_volume_badblocks(super
->bbm_log
, ord_to_idx(ord
), pba_of_lba0(map
),
9988 blocks_per_member(map
), &super
->bb
);
9992 /*******************************************************************************
9993 * Function: examine_badblocks_imsm
9994 * Description: Prints list of bad blocks on a disk to the standard output
9997 * st : metadata handler
9998 * fd : open file descriptor for device
9999 * devname : device name
10003 ******************************************************************************/
10004 static int examine_badblocks_imsm(struct supertype
*st
, int fd
, char *devname
)
10006 struct intel_super
*super
= st
->sb
;
10007 struct bbm_log
*log
= super
->bbm_log
;
10008 struct dl
*d
= NULL
;
10011 for (d
= super
->disks
; d
; d
= d
->next
) {
10012 if (strcmp(d
->devname
, devname
) == 0)
10016 if ((d
== NULL
) || (d
->index
< 0)) { /* serial mismatch probably */
10017 pr_err("%s doesn't appear to be part of a raid array\n",
10024 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[0];
10026 for (i
= 0; i
< log
->entry_count
; i
++) {
10027 if (entry
[i
].disk_ordinal
== d
->index
) {
10028 unsigned long long sector
= __le48_to_cpu(
10029 &entry
[i
].defective_block_start
);
10030 int cnt
= entry
[i
].marked_count
+ 1;
10033 printf("Bad-blocks on %s:\n", devname
);
10037 printf("%20llu for %d sectors\n", sector
, cnt
);
10043 printf("No bad-blocks list configured on %s\n", devname
);
10047 /*******************************************************************************
10048 * Function: init_migr_record_imsm
10049 * Description: Function inits imsm migration record
10051 * super : imsm internal array info
10052 * dev : device under migration
10053 * info : general array info to find the smallest device
10056 ******************************************************************************/
10057 void init_migr_record_imsm(struct supertype
*st
, struct imsm_dev
*dev
,
10058 struct mdinfo
*info
)
10060 struct intel_super
*super
= st
->sb
;
10061 struct migr_record
*migr_rec
= super
->migr_rec
;
10062 int new_data_disks
;
10063 unsigned long long dsize
, dev_sectors
;
10064 long long unsigned min_dev_sectors
= -1LLU;
10068 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
10069 struct imsm_map
*map_src
= get_imsm_map(dev
, MAP_1
);
10070 unsigned long long num_migr_units
;
10071 unsigned long long array_blocks
;
10073 memset(migr_rec
, 0, sizeof(struct migr_record
));
10074 migr_rec
->family_num
= __cpu_to_le32(super
->anchor
->family_num
);
10076 /* only ascending reshape supported now */
10077 migr_rec
->ascending_migr
= __cpu_to_le32(1);
10079 migr_rec
->dest_depth_per_unit
= GEN_MIGR_AREA_SIZE
/
10080 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
10081 migr_rec
->dest_depth_per_unit
*=
10082 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
10083 new_data_disks
= imsm_num_data_members(dev
, MAP_0
);
10084 migr_rec
->blocks_per_unit
=
10085 __cpu_to_le32(migr_rec
->dest_depth_per_unit
* new_data_disks
);
10086 migr_rec
->dest_depth_per_unit
=
10087 __cpu_to_le32(migr_rec
->dest_depth_per_unit
);
10088 array_blocks
= info
->component_size
* new_data_disks
;
10090 array_blocks
/ __le32_to_cpu(migr_rec
->blocks_per_unit
);
10092 if (array_blocks
% __le32_to_cpu(migr_rec
->blocks_per_unit
))
10094 migr_rec
->num_migr_units
= __cpu_to_le32(num_migr_units
);
10096 migr_rec
->post_migr_vol_cap
= dev
->size_low
;
10097 migr_rec
->post_migr_vol_cap_hi
= dev
->size_high
;
10099 /* Find the smallest dev */
10100 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
10101 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
10102 fd
= dev_open(nm
, O_RDONLY
);
10105 get_dev_size(fd
, NULL
, &dsize
);
10106 dev_sectors
= dsize
/ 512;
10107 if (dev_sectors
< min_dev_sectors
)
10108 min_dev_sectors
= dev_sectors
;
10111 migr_rec
->ckpt_area_pba
= __cpu_to_le32(min_dev_sectors
-
10112 RAID_DISK_RESERVED_BLOCKS_IMSM_HI
);
10114 write_imsm_migr_rec(st
);
10119 /*******************************************************************************
10120 * Function: save_backup_imsm
10121 * Description: Function saves critical data stripes to Migration Copy Area
10122 * and updates the current migration unit status.
10123 * Use restore_stripes() to form a destination stripe,
10124 * and to write it to the Copy Area.
10126 * st : supertype information
10127 * dev : imsm device that backup is saved for
10128 * info : general array info
10129 * buf : input buffer
10130 * length : length of data to backup (blocks_per_unit)
10134 ******************************************************************************/
10135 int save_backup_imsm(struct supertype
*st
,
10136 struct imsm_dev
*dev
,
10137 struct mdinfo
*info
,
10142 struct intel_super
*super
= st
->sb
;
10143 unsigned long long *target_offsets
;
10146 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
10147 int new_disks
= map_dest
->num_members
;
10148 int dest_layout
= 0;
10150 unsigned long long start
;
10151 int data_disks
= imsm_num_data_members(dev
, MAP_0
);
10153 targets
= xmalloc(new_disks
* sizeof(int));
10155 for (i
= 0; i
< new_disks
; i
++)
10158 target_offsets
= xcalloc(new_disks
, sizeof(unsigned long long));
10160 start
= info
->reshape_progress
* 512;
10161 for (i
= 0; i
< new_disks
; i
++) {
10162 target_offsets
[i
] = (unsigned long long)
10163 __le32_to_cpu(super
->migr_rec
->ckpt_area_pba
) * 512;
10164 /* move back copy area adderss, it will be moved forward
10165 * in restore_stripes() using start input variable
10167 target_offsets
[i
] -= start
/data_disks
;
10170 if (open_backup_targets(info
, new_disks
, targets
,
10174 dest_layout
= imsm_level_to_layout(map_dest
->raid_level
);
10175 dest_chunk
= __le16_to_cpu(map_dest
->blocks_per_strip
) * 512;
10177 if (restore_stripes(targets
, /* list of dest devices */
10178 target_offsets
, /* migration record offsets */
10181 map_dest
->raid_level
,
10183 -1, /* source backup file descriptor */
10184 0, /* input buf offset
10185 * always 0 buf is already offseted */
10189 pr_err("Error restoring stripes\n");
10197 close_targets(targets
, new_disks
);
10200 free(target_offsets
);
10205 /*******************************************************************************
10206 * Function: save_checkpoint_imsm
10207 * Description: Function called for current unit status update
10208 * in the migration record. It writes it to disk.
10210 * super : imsm internal array info
10211 * info : general array info
10215 * 2: failure, means no valid migration record
10216 * / no general migration in progress /
10217 ******************************************************************************/
10218 int save_checkpoint_imsm(struct supertype
*st
, struct mdinfo
*info
, int state
)
10220 struct intel_super
*super
= st
->sb
;
10221 unsigned long long blocks_per_unit
;
10222 unsigned long long curr_migr_unit
;
10224 if (load_imsm_migr_rec(super
, info
) != 0) {
10225 dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n");
10229 blocks_per_unit
= __le32_to_cpu(super
->migr_rec
->blocks_per_unit
);
10230 if (blocks_per_unit
== 0) {
10231 dprintf("imsm: no migration in progress.\n");
10234 curr_migr_unit
= info
->reshape_progress
/ blocks_per_unit
;
10235 /* check if array is alligned to copy area
10236 * if it is not alligned, add one to current migration unit value
10237 * this can happend on array reshape finish only
10239 if (info
->reshape_progress
% blocks_per_unit
)
10242 super
->migr_rec
->curr_migr_unit
=
10243 __cpu_to_le32(curr_migr_unit
);
10244 super
->migr_rec
->rec_status
= __cpu_to_le32(state
);
10245 super
->migr_rec
->dest_1st_member_lba
=
10246 __cpu_to_le32(curr_migr_unit
*
10247 __le32_to_cpu(super
->migr_rec
->dest_depth_per_unit
));
10248 if (write_imsm_migr_rec(st
) < 0) {
10249 dprintf("imsm: Cannot write migration record outside backup area\n");
10256 /*******************************************************************************
10257 * Function: recover_backup_imsm
10258 * Description: Function recovers critical data from the Migration Copy Area
10259 * while assembling an array.
10261 * super : imsm internal array info
10262 * info : general array info
10264 * 0 : success (or there is no data to recover)
10266 ******************************************************************************/
10267 int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
)
10269 struct intel_super
*super
= st
->sb
;
10270 struct migr_record
*migr_rec
= super
->migr_rec
;
10271 struct imsm_map
*map_dest
;
10272 struct intel_dev
*id
= NULL
;
10273 unsigned long long read_offset
;
10274 unsigned long long write_offset
;
10276 int *targets
= NULL
;
10277 int new_disks
, i
, err
;
10280 unsigned int sector_size
= super
->sector_size
;
10281 unsigned long curr_migr_unit
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
10282 unsigned long num_migr_units
= __le32_to_cpu(migr_rec
->num_migr_units
);
10284 int skipped_disks
= 0;
10286 err
= sysfs_get_str(info
, NULL
, "array_state", (char *)buffer
, 20);
10290 /* recover data only during assemblation */
10291 if (strncmp(buffer
, "inactive", 8) != 0)
10293 /* no data to recover */
10294 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
10296 if (curr_migr_unit
>= num_migr_units
)
10299 /* find device during reshape */
10300 for (id
= super
->devlist
; id
; id
= id
->next
)
10301 if (is_gen_migration(id
->dev
))
10306 map_dest
= get_imsm_map(id
->dev
, MAP_0
);
10307 new_disks
= map_dest
->num_members
;
10309 read_offset
= (unsigned long long)
10310 __le32_to_cpu(migr_rec
->ckpt_area_pba
) * 512;
10312 write_offset
= ((unsigned long long)
10313 __le32_to_cpu(migr_rec
->dest_1st_member_lba
) +
10314 pba_of_lba0(map_dest
)) * 512;
10316 unit_len
= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
10317 if (posix_memalign((void **)&buf
, sector_size
, unit_len
) != 0)
10319 targets
= xcalloc(new_disks
, sizeof(int));
10321 if (open_backup_targets(info
, new_disks
, targets
, super
, id
->dev
)) {
10322 pr_err("Cannot open some devices belonging to array.\n");
10326 for (i
= 0; i
< new_disks
; i
++) {
10327 if (targets
[i
] < 0) {
10331 if (lseek64(targets
[i
], read_offset
, SEEK_SET
) < 0) {
10332 pr_err("Cannot seek to block: %s\n",
10337 if ((unsigned)read(targets
[i
], buf
, unit_len
) != unit_len
) {
10338 pr_err("Cannot read copy area block: %s\n",
10343 if (lseek64(targets
[i
], write_offset
, SEEK_SET
) < 0) {
10344 pr_err("Cannot seek to block: %s\n",
10349 if ((unsigned)write(targets
[i
], buf
, unit_len
) != unit_len
) {
10350 pr_err("Cannot restore block: %s\n",
10357 if (skipped_disks
> imsm_get_allowed_degradation(info
->new_level
,
10361 pr_err("Cannot restore data from backup. Too many failed disks\n");
10365 if (save_checkpoint_imsm(st
, info
, UNIT_SRC_NORMAL
)) {
10366 /* ignore error == 2, this can mean end of reshape here
10368 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n");
10374 for (i
= 0; i
< new_disks
; i
++)
10383 static char disk_by_path
[] = "/dev/disk/by-path/";
10385 static const char *imsm_get_disk_controller_domain(const char *path
)
10387 char disk_path
[PATH_MAX
];
10391 strcpy(disk_path
, disk_by_path
);
10392 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
10393 if (stat(disk_path
, &st
) == 0) {
10394 struct sys_dev
* hba
;
10397 path
= devt_to_devpath(st
.st_rdev
);
10400 hba
= find_disk_attached_hba(-1, path
);
10401 if (hba
&& hba
->type
== SYS_DEV_SAS
)
10403 else if (hba
&& hba
->type
== SYS_DEV_SATA
)
10407 dprintf("path: %s hba: %s attached: %s\n",
10408 path
, (hba
) ? hba
->path
: "NULL", drv
);
10414 static char *imsm_find_array_devnm_by_subdev(int subdev
, char *container
)
10416 static char devnm
[32];
10417 char subdev_name
[20];
10418 struct mdstat_ent
*mdstat
;
10420 sprintf(subdev_name
, "%d", subdev
);
10421 mdstat
= mdstat_by_subdev(subdev_name
, container
);
10425 strcpy(devnm
, mdstat
->devnm
);
10426 free_mdstat(mdstat
);
10430 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
10431 struct geo_params
*geo
,
10432 int *old_raid_disks
,
10435 /* currently we only support increasing the number of devices
10436 * for a container. This increases the number of device for each
10437 * member array. They must all be RAID0 or RAID5.
10440 struct mdinfo
*info
, *member
;
10441 int devices_that_can_grow
= 0;
10443 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st
->devnm
);
10445 if (geo
->size
> 0 ||
10446 geo
->level
!= UnSet
||
10447 geo
->layout
!= UnSet
||
10448 geo
->chunksize
!= 0 ||
10449 geo
->raid_disks
== UnSet
) {
10450 dprintf("imsm: Container operation is allowed for raid disks number change only.\n");
10454 if (direction
== ROLLBACK_METADATA_CHANGES
) {
10455 dprintf("imsm: Metadata changes rollback is not supported for container operation.\n");
10459 info
= container_content_imsm(st
, NULL
);
10460 for (member
= info
; member
; member
= member
->next
) {
10463 dprintf("imsm: checking device_num: %i\n",
10464 member
->container_member
);
10466 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
10467 /* we work on container for Online Capacity Expansion
10468 * only so raid_disks has to grow
10470 dprintf("imsm: for container operation raid disks increase is required\n");
10474 if (info
->array
.level
!= 0 && info
->array
.level
!= 5) {
10475 /* we cannot use this container with other raid level
10477 dprintf("imsm: for container operation wrong raid level (%i) detected\n",
10478 info
->array
.level
);
10481 /* check for platform support
10482 * for this raid level configuration
10484 struct intel_super
*super
= st
->sb
;
10485 if (!is_raid_level_supported(super
->orom
,
10486 member
->array
.level
,
10487 geo
->raid_disks
)) {
10488 dprintf("platform does not support raid%d with %d disk%s\n",
10491 geo
->raid_disks
> 1 ? "s" : "");
10494 /* check if component size is aligned to chunk size
10496 if (info
->component_size
%
10497 (info
->array
.chunk_size
/512)) {
10498 dprintf("Component size is not aligned to chunk size\n");
10503 if (*old_raid_disks
&&
10504 info
->array
.raid_disks
!= *old_raid_disks
)
10506 *old_raid_disks
= info
->array
.raid_disks
;
10508 /* All raid5 and raid0 volumes in container
10509 * have to be ready for Online Capacity Expansion
10510 * so they need to be assembled. We have already
10511 * checked that no recovery etc is happening.
10513 result
= imsm_find_array_devnm_by_subdev(member
->container_member
,
10514 st
->container_devnm
);
10515 if (result
== NULL
) {
10516 dprintf("imsm: cannot find array\n");
10519 devices_that_can_grow
++;
10522 if (!member
&& devices_that_can_grow
)
10526 dprintf("Container operation allowed\n");
10528 dprintf("Error: %i\n", ret_val
);
10533 /* Function: get_spares_for_grow
10534 * Description: Allocates memory and creates list of spare devices
10535 * avaliable in container. Checks if spare drive size is acceptable.
10536 * Parameters: Pointer to the supertype structure
10537 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
10540 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
10542 unsigned long long min_size
= min_acceptable_spare_size_imsm(st
);
10543 return container_choose_spares(st
, min_size
, NULL
, NULL
, NULL
, 0);
10546 /******************************************************************************
10547 * function: imsm_create_metadata_update_for_reshape
10548 * Function creates update for whole IMSM container.
10550 ******************************************************************************/
10551 static int imsm_create_metadata_update_for_reshape(
10552 struct supertype
*st
,
10553 struct geo_params
*geo
,
10554 int old_raid_disks
,
10555 struct imsm_update_reshape
**updatep
)
10557 struct intel_super
*super
= st
->sb
;
10558 struct imsm_super
*mpb
= super
->anchor
;
10559 int update_memory_size
;
10560 struct imsm_update_reshape
*u
;
10561 struct mdinfo
*spares
;
10564 struct mdinfo
*dev
;
10566 dprintf("(enter) raid_disks = %i\n", geo
->raid_disks
);
10568 delta_disks
= geo
->raid_disks
- old_raid_disks
;
10570 /* size of all update data without anchor */
10571 update_memory_size
= sizeof(struct imsm_update_reshape
);
10573 /* now add space for spare disks that we need to add. */
10574 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
10576 u
= xcalloc(1, update_memory_size
);
10577 u
->type
= update_reshape_container_disks
;
10578 u
->old_raid_disks
= old_raid_disks
;
10579 u
->new_raid_disks
= geo
->raid_disks
;
10581 /* now get spare disks list
10583 spares
= get_spares_for_grow(st
);
10586 || delta_disks
> spares
->array
.spare_disks
) {
10587 pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo
->dev_name
);
10592 /* we have got spares
10593 * update disk list in imsm_disk list table in anchor
10595 dprintf("imsm: %i spares are available.\n\n",
10596 spares
->array
.spare_disks
);
10598 dev
= spares
->devs
;
10599 for (i
= 0; i
< delta_disks
; i
++) {
10604 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
10606 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
10607 dl
->index
= mpb
->num_disks
;
10615 sysfs_free(spares
);
10617 dprintf("imsm: reshape update preparation :");
10618 if (i
== delta_disks
) {
10619 dprintf_cont(" OK\n");
10621 return update_memory_size
;
10624 dprintf_cont(" Error\n");
10629 /******************************************************************************
10630 * function: imsm_create_metadata_update_for_size_change()
10631 * Creates update for IMSM array for array size change.
10633 ******************************************************************************/
10634 static int imsm_create_metadata_update_for_size_change(
10635 struct supertype
*st
,
10636 struct geo_params
*geo
,
10637 struct imsm_update_size_change
**updatep
)
10639 struct intel_super
*super
= st
->sb
;
10640 int update_memory_size
;
10641 struct imsm_update_size_change
*u
;
10643 dprintf("(enter) New size = %llu\n", geo
->size
);
10645 /* size of all update data without anchor */
10646 update_memory_size
= sizeof(struct imsm_update_size_change
);
10648 u
= xcalloc(1, update_memory_size
);
10649 u
->type
= update_size_change
;
10650 u
->subdev
= super
->current_vol
;
10651 u
->new_size
= geo
->size
;
10653 dprintf("imsm: reshape update preparation : OK\n");
10656 return update_memory_size
;
10659 /******************************************************************************
10660 * function: imsm_create_metadata_update_for_migration()
10661 * Creates update for IMSM array.
10663 ******************************************************************************/
10664 static int imsm_create_metadata_update_for_migration(
10665 struct supertype
*st
,
10666 struct geo_params
*geo
,
10667 struct imsm_update_reshape_migration
**updatep
)
10669 struct intel_super
*super
= st
->sb
;
10670 int update_memory_size
;
10671 struct imsm_update_reshape_migration
*u
;
10672 struct imsm_dev
*dev
;
10673 int previous_level
= -1;
10675 dprintf("(enter) New Level = %i\n", geo
->level
);
10677 /* size of all update data without anchor */
10678 update_memory_size
= sizeof(struct imsm_update_reshape_migration
);
10680 u
= xcalloc(1, update_memory_size
);
10681 u
->type
= update_reshape_migration
;
10682 u
->subdev
= super
->current_vol
;
10683 u
->new_level
= geo
->level
;
10684 u
->new_layout
= geo
->layout
;
10685 u
->new_raid_disks
= u
->old_raid_disks
= geo
->raid_disks
;
10686 u
->new_disks
[0] = -1;
10687 u
->new_chunksize
= -1;
10689 dev
= get_imsm_dev(super
, u
->subdev
);
10691 struct imsm_map
*map
;
10693 map
= get_imsm_map(dev
, MAP_0
);
10695 int current_chunk_size
=
10696 __le16_to_cpu(map
->blocks_per_strip
) / 2;
10698 if (geo
->chunksize
!= current_chunk_size
) {
10699 u
->new_chunksize
= geo
->chunksize
/ 1024;
10700 dprintf("imsm: chunk size change from %i to %i\n",
10701 current_chunk_size
, u
->new_chunksize
);
10703 previous_level
= map
->raid_level
;
10706 if (geo
->level
== 5 && previous_level
== 0) {
10707 struct mdinfo
*spares
= NULL
;
10709 u
->new_raid_disks
++;
10710 spares
= get_spares_for_grow(st
);
10711 if (spares
== NULL
|| spares
->array
.spare_disks
< 1) {
10713 sysfs_free(spares
);
10714 update_memory_size
= 0;
10715 dprintf("error: cannot get spare device for requested migration");
10718 sysfs_free(spares
);
10720 dprintf("imsm: reshape update preparation : OK\n");
10723 return update_memory_size
;
10726 static void imsm_update_metadata_locally(struct supertype
*st
,
10727 void *buf
, int len
)
10729 struct metadata_update mu
;
10734 mu
.space_list
= NULL
;
10736 if (imsm_prepare_update(st
, &mu
))
10737 imsm_process_update(st
, &mu
);
10739 while (mu
.space_list
) {
10740 void **space
= mu
.space_list
;
10741 mu
.space_list
= *space
;
10746 /***************************************************************************
10747 * Function: imsm_analyze_change
10748 * Description: Function analyze change for single volume
10749 * and validate if transition is supported
10750 * Parameters: Geometry parameters, supertype structure,
10751 * metadata change direction (apply/rollback)
10752 * Returns: Operation type code on success, -1 if fail
10753 ****************************************************************************/
10754 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
10755 struct geo_params
*geo
,
10758 struct mdinfo info
;
10760 int check_devs
= 0;
10762 /* number of added/removed disks in operation result */
10763 int devNumChange
= 0;
10764 /* imsm compatible layout value for array geometry verification */
10765 int imsm_layout
= -1;
10767 struct imsm_dev
*dev
;
10768 struct intel_super
*super
;
10769 unsigned long long current_size
;
10770 unsigned long long free_size
;
10771 unsigned long long max_size
;
10774 getinfo_super_imsm_volume(st
, &info
, NULL
);
10775 if (geo
->level
!= info
.array
.level
&& geo
->level
>= 0 &&
10776 geo
->level
!= UnSet
) {
10777 switch (info
.array
.level
) {
10779 if (geo
->level
== 5) {
10780 change
= CH_MIGRATION
;
10781 if (geo
->layout
!= ALGORITHM_LEFT_ASYMMETRIC
) {
10782 pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n");
10784 goto analyse_change_exit
;
10786 imsm_layout
= geo
->layout
;
10788 devNumChange
= 1; /* parity disk added */
10789 } else if (geo
->level
== 10) {
10790 change
= CH_TAKEOVER
;
10792 devNumChange
= 2; /* two mirrors added */
10793 imsm_layout
= 0x102; /* imsm supported layout */
10798 if (geo
->level
== 0) {
10799 change
= CH_TAKEOVER
;
10801 devNumChange
= -(geo
->raid_disks
/2);
10802 imsm_layout
= 0; /* imsm raid0 layout */
10806 if (change
== -1) {
10807 pr_err("Error. Level Migration from %d to %d not supported!\n",
10808 info
.array
.level
, geo
->level
);
10809 goto analyse_change_exit
;
10812 geo
->level
= info
.array
.level
;
10814 if (geo
->layout
!= info
.array
.layout
&&
10815 (geo
->layout
!= UnSet
&& geo
->layout
!= -1)) {
10816 change
= CH_MIGRATION
;
10817 if (info
.array
.layout
== 0 && info
.array
.level
== 5 &&
10818 geo
->layout
== 5) {
10819 /* reshape 5 -> 4 */
10820 } else if (info
.array
.layout
== 5 && info
.array
.level
== 5 &&
10821 geo
->layout
== 0) {
10822 /* reshape 4 -> 5 */
10826 pr_err("Error. Layout Migration from %d to %d not supported!\n",
10827 info
.array
.layout
, geo
->layout
);
10829 goto analyse_change_exit
;
10832 geo
->layout
= info
.array
.layout
;
10833 if (imsm_layout
== -1)
10834 imsm_layout
= info
.array
.layout
;
10837 if (geo
->chunksize
> 0 && geo
->chunksize
!= UnSet
&&
10838 geo
->chunksize
!= info
.array
.chunk_size
) {
10839 if (info
.array
.level
== 10) {
10840 pr_err("Error. Chunk size change for RAID 10 is not supported.\n");
10842 goto analyse_change_exit
;
10843 } else if (info
.component_size
% (geo
->chunksize
/512)) {
10844 pr_err("New chunk size (%dK) does not evenly divide device size (%lluk). Aborting...\n",
10845 geo
->chunksize
/1024, info
.component_size
/2);
10847 goto analyse_change_exit
;
10849 change
= CH_MIGRATION
;
10851 geo
->chunksize
= info
.array
.chunk_size
;
10854 chunk
= geo
->chunksize
/ 1024;
10857 dev
= get_imsm_dev(super
, super
->current_vol
);
10858 data_disks
= imsm_num_data_members(dev
, MAP_0
);
10859 /* compute current size per disk member
10861 current_size
= info
.custom_array_size
/ data_disks
;
10863 if (geo
->size
> 0 && geo
->size
!= MAX_SIZE
) {
10864 /* align component size
10866 geo
->size
= imsm_component_size_aligment_check(
10867 get_imsm_raid_level(dev
->vol
.map
),
10868 chunk
* 1024, super
->sector_size
,
10870 if (geo
->size
== 0) {
10871 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is 0).\n",
10873 goto analyse_change_exit
;
10877 if (current_size
!= geo
->size
&& geo
->size
> 0) {
10878 if (change
!= -1) {
10879 pr_err("Error. Size change should be the only one at a time.\n");
10881 goto analyse_change_exit
;
10883 if ((super
->current_vol
+ 1) != super
->anchor
->num_raid_devs
) {
10884 pr_err("Error. The last volume in container can be expanded only (%i/%s).\n",
10885 super
->current_vol
, st
->devnm
);
10886 goto analyse_change_exit
;
10888 /* check the maximum available size
10890 rv
= imsm_get_free_size(st
, dev
->vol
.map
->num_members
,
10891 0, chunk
, &free_size
);
10893 /* Cannot find maximum available space
10897 max_size
= free_size
+ current_size
;
10898 /* align component size
10900 max_size
= imsm_component_size_aligment_check(
10901 get_imsm_raid_level(dev
->vol
.map
),
10902 chunk
* 1024, super
->sector_size
,
10905 if (geo
->size
== MAX_SIZE
) {
10906 /* requested size change to the maximum available size
10908 if (max_size
== 0) {
10909 pr_err("Error. Cannot find maximum available space.\n");
10911 goto analyse_change_exit
;
10913 geo
->size
= max_size
;
10916 if (direction
== ROLLBACK_METADATA_CHANGES
) {
10917 /* accept size for rollback only
10920 /* round size due to metadata compatibility
10922 geo
->size
= (geo
->size
>> SECT_PER_MB_SHIFT
)
10923 << SECT_PER_MB_SHIFT
;
10924 dprintf("Prepare update for size change to %llu\n",
10926 if (current_size
>= geo
->size
) {
10927 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is %llu).\n",
10928 current_size
, geo
->size
);
10929 goto analyse_change_exit
;
10931 if (max_size
&& geo
->size
> max_size
) {
10932 pr_err("Error. Requested size is larger than maximum available size (maximum available size is %llu, requested size /rounded/ is %llu).\n",
10933 max_size
, geo
->size
);
10934 goto analyse_change_exit
;
10937 geo
->size
*= data_disks
;
10938 geo
->raid_disks
= dev
->vol
.map
->num_members
;
10939 change
= CH_ARRAY_SIZE
;
10941 if (!validate_geometry_imsm(st
,
10944 geo
->raid_disks
+ devNumChange
,
10946 geo
->size
, INVALID_SECTORS
,
10951 struct intel_super
*super
= st
->sb
;
10952 struct imsm_super
*mpb
= super
->anchor
;
10954 if (mpb
->num_raid_devs
> 1) {
10955 pr_err("Error. Cannot perform operation on %s- for this operation it MUST be single array in container\n",
10961 analyse_change_exit
:
10962 if (direction
== ROLLBACK_METADATA_CHANGES
&&
10963 (change
== CH_MIGRATION
|| change
== CH_TAKEOVER
)) {
10964 dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n");
10970 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
10972 struct intel_super
*super
= st
->sb
;
10973 struct imsm_update_takeover
*u
;
10975 u
= xmalloc(sizeof(struct imsm_update_takeover
));
10977 u
->type
= update_takeover
;
10978 u
->subarray
= super
->current_vol
;
10980 /* 10->0 transition */
10981 if (geo
->level
== 0)
10982 u
->direction
= R10_TO_R0
;
10984 /* 0->10 transition */
10985 if (geo
->level
== 10)
10986 u
->direction
= R0_TO_R10
;
10988 /* update metadata locally */
10989 imsm_update_metadata_locally(st
, u
,
10990 sizeof(struct imsm_update_takeover
));
10991 /* and possibly remotely */
10992 if (st
->update_tail
)
10993 append_metadata_update(st
, u
,
10994 sizeof(struct imsm_update_takeover
));
11001 static int imsm_reshape_super(struct supertype
*st
, unsigned long long size
,
11003 int layout
, int chunksize
, int raid_disks
,
11004 int delta_disks
, char *backup
, char *dev
,
11005 int direction
, int verbose
)
11008 struct geo_params geo
;
11010 dprintf("(enter)\n");
11012 memset(&geo
, 0, sizeof(struct geo_params
));
11014 geo
.dev_name
= dev
;
11015 strcpy(geo
.devnm
, st
->devnm
);
11018 geo
.layout
= layout
;
11019 geo
.chunksize
= chunksize
;
11020 geo
.raid_disks
= raid_disks
;
11021 if (delta_disks
!= UnSet
)
11022 geo
.raid_disks
+= delta_disks
;
11024 dprintf("for level : %i\n", geo
.level
);
11025 dprintf("for raid_disks : %i\n", geo
.raid_disks
);
11027 if (experimental() == 0)
11030 if (strcmp(st
->container_devnm
, st
->devnm
) == 0) {
11031 /* On container level we can only increase number of devices. */
11032 dprintf("imsm: info: Container operation\n");
11033 int old_raid_disks
= 0;
11035 if (imsm_reshape_is_allowed_on_container(
11036 st
, &geo
, &old_raid_disks
, direction
)) {
11037 struct imsm_update_reshape
*u
= NULL
;
11040 len
= imsm_create_metadata_update_for_reshape(
11041 st
, &geo
, old_raid_disks
, &u
);
11044 dprintf("imsm: Cannot prepare update\n");
11045 goto exit_imsm_reshape_super
;
11049 /* update metadata locally */
11050 imsm_update_metadata_locally(st
, u
, len
);
11051 /* and possibly remotely */
11052 if (st
->update_tail
)
11053 append_metadata_update(st
, u
, len
);
11058 pr_err("(imsm) Operation is not allowed on this container\n");
11061 /* On volume level we support following operations
11062 * - takeover: raid10 -> raid0; raid0 -> raid10
11063 * - chunk size migration
11064 * - migration: raid5 -> raid0; raid0 -> raid5
11066 struct intel_super
*super
= st
->sb
;
11067 struct intel_dev
*dev
= super
->devlist
;
11069 dprintf("imsm: info: Volume operation\n");
11070 /* find requested device */
11073 imsm_find_array_devnm_by_subdev(
11074 dev
->index
, st
->container_devnm
);
11075 if (devnm
&& strcmp(devnm
, geo
.devnm
) == 0)
11080 pr_err("Cannot find %s (%s) subarray\n",
11081 geo
.dev_name
, geo
.devnm
);
11082 goto exit_imsm_reshape_super
;
11084 super
->current_vol
= dev
->index
;
11085 change
= imsm_analyze_change(st
, &geo
, direction
);
11088 ret_val
= imsm_takeover(st
, &geo
);
11090 case CH_MIGRATION
: {
11091 struct imsm_update_reshape_migration
*u
= NULL
;
11093 imsm_create_metadata_update_for_migration(
11096 dprintf("imsm: Cannot prepare update\n");
11100 /* update metadata locally */
11101 imsm_update_metadata_locally(st
, u
, len
);
11102 /* and possibly remotely */
11103 if (st
->update_tail
)
11104 append_metadata_update(st
, u
, len
);
11109 case CH_ARRAY_SIZE
: {
11110 struct imsm_update_size_change
*u
= NULL
;
11112 imsm_create_metadata_update_for_size_change(
11115 dprintf("imsm: Cannot prepare update\n");
11119 /* update metadata locally */
11120 imsm_update_metadata_locally(st
, u
, len
);
11121 /* and possibly remotely */
11122 if (st
->update_tail
)
11123 append_metadata_update(st
, u
, len
);
11133 exit_imsm_reshape_super
:
11134 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
11138 #define COMPLETED_OK 0
11139 #define COMPLETED_NONE 1
11140 #define COMPLETED_DELAYED 2
11142 static int read_completed(int fd
, unsigned long long *val
)
11147 ret
= sysfs_fd_get_str(fd
, buf
, 50);
11151 ret
= COMPLETED_OK
;
11152 if (strncmp(buf
, "none", 4) == 0) {
11153 ret
= COMPLETED_NONE
;
11154 } else if (strncmp(buf
, "delayed", 7) == 0) {
11155 ret
= COMPLETED_DELAYED
;
11158 *val
= strtoull(buf
, &ep
, 0);
11159 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))
11165 /*******************************************************************************
11166 * Function: wait_for_reshape_imsm
11167 * Description: Function writes new sync_max value and waits until
11168 * reshape process reach new position
11170 * sra : general array info
11171 * ndata : number of disks in new array's layout
11174 * 1 : there is no reshape in progress,
11176 ******************************************************************************/
11177 int wait_for_reshape_imsm(struct mdinfo
*sra
, int ndata
)
11179 int fd
= sysfs_get_fd(sra
, NULL
, "sync_completed");
11181 unsigned long long completed
;
11182 /* to_complete : new sync_max position */
11183 unsigned long long to_complete
= sra
->reshape_progress
;
11184 unsigned long long position_to_set
= to_complete
/ ndata
;
11187 dprintf("cannot open reshape_position\n");
11192 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
11194 dprintf("cannot read reshape_position (no reshape in progres)\n");
11203 if (completed
> position_to_set
) {
11204 dprintf("wrong next position to set %llu (%llu)\n",
11205 to_complete
, position_to_set
);
11209 dprintf("Position set: %llu\n", position_to_set
);
11210 if (sysfs_set_num(sra
, NULL
, "sync_max",
11211 position_to_set
) != 0) {
11212 dprintf("cannot set reshape position to %llu\n",
11221 int timeout
= 3000;
11223 sysfs_wait(fd
, &timeout
);
11224 if (sysfs_get_str(sra
, NULL
, "sync_action",
11226 strncmp(action
, "reshape", 7) != 0) {
11227 if (strncmp(action
, "idle", 4) == 0)
11233 rc
= read_completed(fd
, &completed
);
11235 dprintf("cannot read reshape_position (in loop)\n");
11238 } else if (rc
== COMPLETED_NONE
)
11240 } while (completed
< position_to_set
);
11246 /*******************************************************************************
11247 * Function: check_degradation_change
11248 * Description: Check that array hasn't become failed.
11250 * info : for sysfs access
11251 * sources : source disks descriptors
11252 * degraded: previous degradation level
11254 * degradation level
11255 ******************************************************************************/
11256 int check_degradation_change(struct mdinfo
*info
,
11260 unsigned long long new_degraded
;
11263 rv
= sysfs_get_ll(info
, NULL
, "degraded", &new_degraded
);
11264 if (rv
== -1 || (new_degraded
!= (unsigned long long)degraded
)) {
11265 /* check each device to ensure it is still working */
11268 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
11269 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
11271 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
11274 if (sysfs_get_str(info
,
11275 sd
, "state", sbuf
, sizeof(sbuf
)) < 0 ||
11276 strstr(sbuf
, "faulty") ||
11277 strstr(sbuf
, "in_sync") == NULL
) {
11278 /* this device is dead */
11279 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
11280 if (sd
->disk
.raid_disk
>= 0 &&
11281 sources
[sd
->disk
.raid_disk
] >= 0) {
11283 sd
->disk
.raid_disk
]);
11284 sources
[sd
->disk
.raid_disk
] =
11293 return new_degraded
;
11296 /*******************************************************************************
11297 * Function: imsm_manage_reshape
11298 * Description: Function finds array under reshape and it manages reshape
11299 * process. It creates stripes backups (if required) and sets
11302 * afd : Backup handle (nattive) - not used
11303 * sra : general array info
11304 * reshape : reshape parameters - not used
11305 * st : supertype structure
11306 * blocks : size of critical section [blocks]
11307 * fds : table of source device descriptor
11308 * offsets : start of array (offest per devices)
11310 * destfd : table of destination device descriptor
11311 * destoffsets : table of destination offsets (per device)
11313 * 1 : success, reshape is done
11315 ******************************************************************************/
11316 static int imsm_manage_reshape(
11317 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
11318 struct supertype
*st
, unsigned long backup_blocks
,
11319 int *fds
, unsigned long long *offsets
,
11320 int dests
, int *destfd
, unsigned long long *destoffsets
)
11323 struct intel_super
*super
= st
->sb
;
11324 struct intel_dev
*dv
;
11325 unsigned int sector_size
= super
->sector_size
;
11326 struct imsm_dev
*dev
= NULL
;
11327 struct imsm_map
*map_src
;
11328 int migr_vol_qan
= 0;
11329 int ndata
, odata
; /* [bytes] */
11330 int chunk
; /* [bytes] */
11331 struct migr_record
*migr_rec
;
11333 unsigned int buf_size
; /* [bytes] */
11334 unsigned long long max_position
; /* array size [bytes] */
11335 unsigned long long next_step
; /* [blocks]/[bytes] */
11336 unsigned long long old_data_stripe_length
;
11337 unsigned long long start_src
; /* [bytes] */
11338 unsigned long long start
; /* [bytes] */
11339 unsigned long long start_buf_shift
; /* [bytes] */
11341 int source_layout
= 0;
11346 if (!fds
|| !offsets
)
11349 /* Find volume during the reshape */
11350 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
11351 if (dv
->dev
->vol
.migr_type
== MIGR_GEN_MIGR
11352 && dv
->dev
->vol
.migr_state
== 1) {
11357 /* Only one volume can migrate at the same time */
11358 if (migr_vol_qan
!= 1) {
11359 pr_err("%s", migr_vol_qan
?
11360 "Number of migrating volumes greater than 1\n" :
11361 "There is no volume during migrationg\n");
11365 map_src
= get_imsm_map(dev
, MAP_1
);
11366 if (map_src
== NULL
)
11369 ndata
= imsm_num_data_members(dev
, MAP_0
);
11370 odata
= imsm_num_data_members(dev
, MAP_1
);
11372 chunk
= __le16_to_cpu(map_src
->blocks_per_strip
) * 512;
11373 old_data_stripe_length
= odata
* chunk
;
11375 migr_rec
= super
->migr_rec
;
11377 /* initialize migration record for start condition */
11378 if (sra
->reshape_progress
== 0)
11379 init_migr_record_imsm(st
, dev
, sra
);
11381 if (__le32_to_cpu(migr_rec
->rec_status
) != UNIT_SRC_NORMAL
) {
11382 dprintf("imsm: cannot restart migration when data are present in copy area.\n");
11385 /* Save checkpoint to update migration record for current
11386 * reshape position (in md). It can be farther than current
11387 * reshape position in metadata.
11389 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
11390 /* ignore error == 2, this can mean end of reshape here
11392 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n");
11397 /* size for data */
11398 buf_size
= __le32_to_cpu(migr_rec
->blocks_per_unit
) * 512;
11399 /* extend buffer size for parity disk */
11400 buf_size
+= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
11401 /* add space for stripe aligment */
11402 buf_size
+= old_data_stripe_length
;
11403 if (posix_memalign((void **)&buf
, MAX_SECTOR_SIZE
, buf_size
)) {
11404 dprintf("imsm: Cannot allocate checkpoint buffer\n");
11408 max_position
= sra
->component_size
* ndata
;
11409 source_layout
= imsm_level_to_layout(map_src
->raid_level
);
11411 while (__le32_to_cpu(migr_rec
->curr_migr_unit
) <
11412 __le32_to_cpu(migr_rec
->num_migr_units
)) {
11413 /* current reshape position [blocks] */
11414 unsigned long long current_position
=
11415 __le32_to_cpu(migr_rec
->blocks_per_unit
)
11416 * __le32_to_cpu(migr_rec
->curr_migr_unit
);
11417 unsigned long long border
;
11419 /* Check that array hasn't become failed.
11421 degraded
= check_degradation_change(sra
, fds
, degraded
);
11422 if (degraded
> 1) {
11423 dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded
);
11427 next_step
= __le32_to_cpu(migr_rec
->blocks_per_unit
);
11429 if ((current_position
+ next_step
) > max_position
)
11430 next_step
= max_position
- current_position
;
11432 start
= current_position
* 512;
11434 /* align reading start to old geometry */
11435 start_buf_shift
= start
% old_data_stripe_length
;
11436 start_src
= start
- start_buf_shift
;
11438 border
= (start_src
/ odata
) - (start
/ ndata
);
11440 if (border
<= __le32_to_cpu(migr_rec
->dest_depth_per_unit
)) {
11441 /* save critical stripes to buf
11442 * start - start address of current unit
11443 * to backup [bytes]
11444 * start_src - start address of current unit
11445 * to backup alligned to source array
11448 unsigned long long next_step_filler
;
11449 unsigned long long copy_length
= next_step
* 512;
11451 /* allign copy area length to stripe in old geometry */
11452 next_step_filler
= ((copy_length
+ start_buf_shift
)
11453 % old_data_stripe_length
);
11454 if (next_step_filler
)
11455 next_step_filler
= (old_data_stripe_length
11456 - next_step_filler
);
11457 dprintf("save_stripes() parameters: start = %llu,\tstart_src = %llu,\tnext_step*512 = %llu,\tstart_in_buf_shift = %llu,\tnext_step_filler = %llu\n",
11458 start
, start_src
, copy_length
,
11459 start_buf_shift
, next_step_filler
);
11461 if (save_stripes(fds
, offsets
, map_src
->num_members
,
11462 chunk
, map_src
->raid_level
,
11463 source_layout
, 0, NULL
, start_src
,
11465 next_step_filler
+ start_buf_shift
,
11467 dprintf("imsm: Cannot save stripes to buffer\n");
11470 /* Convert data to destination format and store it
11471 * in backup general migration area
11473 if (save_backup_imsm(st
, dev
, sra
,
11474 buf
+ start_buf_shift
, copy_length
)) {
11475 dprintf("imsm: Cannot save stripes to target devices\n");
11478 if (save_checkpoint_imsm(st
, sra
,
11479 UNIT_SRC_IN_CP_AREA
)) {
11480 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n");
11484 /* set next step to use whole border area */
11485 border
/= next_step
;
11487 next_step
*= border
;
11489 /* When data backed up, checkpoint stored,
11490 * kick the kernel to reshape unit of data
11492 next_step
= next_step
+ sra
->reshape_progress
;
11493 /* limit next step to array max position */
11494 if (next_step
> max_position
)
11495 next_step
= max_position
;
11496 sysfs_set_num(sra
, NULL
, "suspend_lo", sra
->reshape_progress
);
11497 sysfs_set_num(sra
, NULL
, "suspend_hi", next_step
);
11498 sra
->reshape_progress
= next_step
;
11500 /* wait until reshape finish */
11501 if (wait_for_reshape_imsm(sra
, ndata
)) {
11502 dprintf("wait_for_reshape_imsm returned error!\n");
11508 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
11509 /* ignore error == 2, this can mean end of reshape here
11511 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n");
11517 /* clear migr_rec on disks after successful migration */
11520 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SECTORS
*sector_size
);
11521 for (d
= super
->disks
; d
; d
= d
->next
) {
11522 if (d
->index
< 0 || is_failed(&d
->disk
))
11524 unsigned long long dsize
;
11526 get_dev_size(d
->fd
, NULL
, &dsize
);
11527 if (lseek64(d
->fd
, dsize
- MIGR_REC_SECTOR_POSITION
*sector_size
,
11529 if ((unsigned int)write(d
->fd
, super
->migr_rec_buf
,
11530 MIGR_REC_BUF_SECTORS
*sector_size
) !=
11531 MIGR_REC_BUF_SECTORS
*sector_size
)
11532 perror("Write migr_rec failed");
11536 /* return '1' if done */
11540 /* See Grow.c: abort_reshape() for further explanation */
11541 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
11542 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
11543 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
11548 #endif /* MDASSEMBLE */
11550 struct superswitch super_imsm
= {
11552 .examine_super
= examine_super_imsm
,
11553 .brief_examine_super
= brief_examine_super_imsm
,
11554 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
11555 .export_examine_super
= export_examine_super_imsm
,
11556 .detail_super
= detail_super_imsm
,
11557 .brief_detail_super
= brief_detail_super_imsm
,
11558 .write_init_super
= write_init_super_imsm
,
11559 .validate_geometry
= validate_geometry_imsm
,
11560 .add_to_super
= add_to_super_imsm
,
11561 .remove_from_super
= remove_from_super_imsm
,
11562 .detail_platform
= detail_platform_imsm
,
11563 .export_detail_platform
= export_detail_platform_imsm
,
11564 .kill_subarray
= kill_subarray_imsm
,
11565 .update_subarray
= update_subarray_imsm
,
11566 .load_container
= load_container_imsm
,
11567 .default_geometry
= default_geometry_imsm
,
11568 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
11569 .reshape_super
= imsm_reshape_super
,
11570 .manage_reshape
= imsm_manage_reshape
,
11571 .recover_backup
= recover_backup_imsm
,
11572 .copy_metadata
= copy_metadata_imsm
,
11573 .examine_badblocks
= examine_badblocks_imsm
,
11575 .match_home
= match_home_imsm
,
11576 .uuid_from_super
= uuid_from_super_imsm
,
11577 .getinfo_super
= getinfo_super_imsm
,
11578 .getinfo_super_disks
= getinfo_super_disks_imsm
,
11579 .update_super
= update_super_imsm
,
11581 .avail_size
= avail_size_imsm
,
11582 .min_acceptable_spare_size
= min_acceptable_spare_size_imsm
,
11584 .compare_super
= compare_super_imsm
,
11586 .load_super
= load_super_imsm
,
11587 .init_super
= init_super_imsm
,
11588 .store_super
= store_super_imsm
,
11589 .free_super
= free_super_imsm
,
11590 .match_metadata_desc
= match_metadata_desc_imsm
,
11591 .container_content
= container_content_imsm
,
11592 .validate_container
= validate_container_imsm
,
11599 .open_new
= imsm_open_new
,
11600 .set_array_state
= imsm_set_array_state
,
11601 .set_disk
= imsm_set_disk
,
11602 .sync_metadata
= imsm_sync_metadata
,
11603 .activate_spare
= imsm_activate_spare
,
11604 .process_update
= imsm_process_update
,
11605 .prepare_update
= imsm_prepare_update
,
11606 .record_bad_block
= imsm_record_badblock
,
11607 .clear_bad_block
= imsm_clear_badblock
,
11608 .get_bad_blocks
= imsm_get_badblocks
,
11609 #endif /* MDASSEMBLE */