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 #define JOURNAL_DISK __cpu_to_le32(0x2000000) /* Device marked as Journaling Drive */
106 __u32 status
; /* 0xF0 - 0xF3 */
107 __u32 owner_cfg_num
; /* which config 0,1,2... owns this disk */
108 __u32 total_blocks_hi
; /* 0xF4 - 0xF5 total blocks hi */
109 #define IMSM_DISK_FILLERS 3
110 __u32 filler
[IMSM_DISK_FILLERS
]; /* 0xF5 - 0x107 MPB_DISK_FILLERS for future expansion */
113 /* map selector for map managment
119 /* RAID map configuration infos. */
121 __u32 pba_of_lba0_lo
; /* start address of partition */
122 __u32 blocks_per_member_lo
;/* blocks per member */
123 __u32 num_data_stripes_lo
; /* number of data stripes */
124 __u16 blocks_per_strip
;
125 __u8 map_state
; /* Normal, Uninitialized, Degraded, Failed */
126 #define IMSM_T_STATE_NORMAL 0
127 #define IMSM_T_STATE_UNINITIALIZED 1
128 #define IMSM_T_STATE_DEGRADED 2
129 #define IMSM_T_STATE_FAILED 3
131 #define IMSM_T_RAID0 0
132 #define IMSM_T_RAID1 1
133 #define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */
134 __u8 num_members
; /* number of member disks */
135 __u8 num_domains
; /* number of parity domains */
136 __u8 failed_disk_num
; /* valid only when state is degraded */
138 __u32 pba_of_lba0_hi
;
139 __u32 blocks_per_member_hi
;
140 __u32 num_data_stripes_hi
;
141 __u32 filler
[4]; /* expansion area */
142 #define IMSM_ORD_REBUILD (1 << 24)
143 __u32 disk_ord_tbl
[1]; /* disk_ord_tbl[num_members],
144 * top byte contains some flags
146 } __attribute__ ((packed
));
149 __u32 curr_migr_unit
;
150 __u32 checkpoint_id
; /* id to access curr_migr_unit */
151 __u8 migr_state
; /* Normal or Migrating */
153 #define MIGR_REBUILD 1
154 #define MIGR_VERIFY 2 /* analagous to echo check > sync_action */
155 #define MIGR_GEN_MIGR 3
156 #define MIGR_STATE_CHANGE 4
157 #define MIGR_REPAIR 5
158 __u8 migr_type
; /* Initializing, Rebuilding, ... */
159 #define RAIDVOL_CLEAN 0
160 #define RAIDVOL_DIRTY 1
161 #define RAIDVOL_DSRECORD_VALID 2
163 __u8 fs_state
; /* fast-sync state for CnG (0xff == disabled) */
164 __u16 verify_errors
; /* number of mismatches */
165 __u16 bad_blocks
; /* number of bad blocks during verify */
167 struct imsm_map map
[1];
168 /* here comes another one if migr_state */
169 } __attribute__ ((packed
));
172 __u8 volume
[MAX_RAID_SERIAL_LEN
];
175 #define DEV_BOOTABLE __cpu_to_le32(0x01)
176 #define DEV_BOOT_DEVICE __cpu_to_le32(0x02)
177 #define DEV_READ_COALESCING __cpu_to_le32(0x04)
178 #define DEV_WRITE_COALESCING __cpu_to_le32(0x08)
179 #define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10)
180 #define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20)
181 #define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40)
182 #define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80)
183 #define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100)
184 #define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200)
185 #define DEV_CLONE_N_GO __cpu_to_le32(0x400)
186 #define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800)
187 #define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000)
188 __u32 status
; /* Persistent RaidDev status */
189 __u32 reserved_blocks
; /* Reserved blocks at beginning of volume */
193 __u8 cng_master_disk
;
197 __u16 my_vol_raid_dev_num
; /* Used in Unique volume Id for this RaidDev */
203 /* Unique Volume Id of the NvCache Volume associated with this volume */
204 __u32 nvc_vol_orig_family_num
;
205 __u16 nvc_vol_raid_dev_num
;
208 #define RWH_DISTRIBUTED 1
209 #define RWH_JOURNALING_DRIVE 2
210 __u8 rwh_policy
; /* Raid Write Hole Policy */
211 __u8 jd_serial
[MAX_RAID_SERIAL_LEN
]; /* Journal Drive serial number */
214 #define IMSM_DEV_FILLERS 3
215 __u32 filler
[IMSM_DEV_FILLERS
];
217 } __attribute__ ((packed
));
220 __u8 sig
[MAX_SIGNATURE_LENGTH
]; /* 0x00 - 0x1F */
221 __u32 check_sum
; /* 0x20 - 0x23 MPB Checksum */
222 __u32 mpb_size
; /* 0x24 - 0x27 Size of MPB */
223 __u32 family_num
; /* 0x28 - 0x2B Checksum from first time this config was written */
224 __u32 generation_num
; /* 0x2C - 0x2F Incremented each time this array's MPB is written */
225 __u32 error_log_size
; /* 0x30 - 0x33 in bytes */
226 __u32 attributes
; /* 0x34 - 0x37 */
227 __u8 num_disks
; /* 0x38 Number of configured disks */
228 __u8 num_raid_devs
; /* 0x39 Number of configured volumes */
229 __u8 error_log_pos
; /* 0x3A */
230 __u8 fill
[1]; /* 0x3B */
231 __u32 cache_size
; /* 0x3c - 0x40 in mb */
232 __u32 orig_family_num
; /* 0x40 - 0x43 original family num */
233 __u32 pwr_cycle_count
; /* 0x44 - 0x47 simulated power cycle count for array */
234 __u32 bbm_log_size
; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */
235 #define IMSM_FILLERS 35
236 __u32 filler
[IMSM_FILLERS
]; /* 0x4C - 0xD7 RAID_MPB_FILLERS */
237 struct imsm_disk disk
[1]; /* 0xD8 diskTbl[numDisks] */
238 /* here comes imsm_dev[num_raid_devs] */
239 /* here comes BBM logs */
240 } __attribute__ ((packed
));
242 #define BBM_LOG_MAX_ENTRIES 254
243 #define BBM_LOG_MAX_LBA_ENTRY_VAL 256 /* Represents 256 LBAs */
244 #define BBM_LOG_SIGNATURE 0xabadb10c
246 struct bbm_log_block_addr
{
249 } __attribute__ ((__packed__
));
251 struct bbm_log_entry
{
252 __u8 marked_count
; /* Number of blocks marked - 1 */
253 __u8 disk_ordinal
; /* Disk entry within the imsm_super */
254 struct bbm_log_block_addr defective_block_start
;
255 } __attribute__ ((__packed__
));
258 __u32 signature
; /* 0xABADB10C */
260 struct bbm_log_entry marked_block_entries
[BBM_LOG_MAX_ENTRIES
];
261 } __attribute__ ((__packed__
));
263 static char *map_state_str
[] = { "normal", "uninitialized", "degraded", "failed" };
265 #define BLOCKS_PER_KB (1024/512)
267 #define RAID_DISK_RESERVED_BLOCKS_IMSM_HI 2209
269 #define GEN_MIGR_AREA_SIZE 2048 /* General Migration Copy Area size in blocks */
271 #define MIGR_REC_BUF_SECTORS 1 /* size of migr_record i/o buffer in sectors */
272 #define MIGR_REC_SECTOR_POSITION 1 /* migr_record position offset on disk,
273 * MIGR_REC_BUF_SECTORS <= MIGR_REC_SECTOR_POS
276 #define UNIT_SRC_NORMAL 0 /* Source data for curr_migr_unit must
277 * be recovered using srcMap */
278 #define UNIT_SRC_IN_CP_AREA 1 /* Source data for curr_migr_unit has
279 * already been migrated and must
280 * be recovered from checkpoint area */
282 #define PPL_ENTRY_SPACE (128 * 1024) /* Size of the PPL, without the header */
285 __u32 rec_status
; /* Status used to determine how to restart
286 * migration in case it aborts
288 __u32 curr_migr_unit
; /* 0..numMigrUnits-1 */
289 __u32 family_num
; /* Family number of MPB
290 * containing the RaidDev
291 * that is migrating */
292 __u32 ascending_migr
; /* True if migrating in increasing
294 __u32 blocks_per_unit
; /* Num disk blocks per unit of operation */
295 __u32 dest_depth_per_unit
; /* Num member blocks each destMap
297 * advances per unit-of-operation */
298 __u32 ckpt_area_pba
; /* Pba of first block of ckpt copy area */
299 __u32 dest_1st_member_lba
; /* First member lba on first
300 * stripe of destination */
301 __u32 num_migr_units
; /* Total num migration units-of-op */
302 __u32 post_migr_vol_cap
; /* Size of volume after
303 * migration completes */
304 __u32 post_migr_vol_cap_hi
; /* Expansion space for LBA64 */
305 __u32 ckpt_read_disk_num
; /* Which member disk in destSubMap[0] the
306 * migration ckpt record was read from
307 * (for recovered migrations) */
308 } __attribute__ ((__packed__
));
313 * 2: metadata does not match
321 struct md_list
*next
;
324 #define pr_vrb(fmt, arg...) (void) (verbose && pr_err(fmt, ##arg))
326 static __u8
migr_type(struct imsm_dev
*dev
)
328 if (dev
->vol
.migr_type
== MIGR_VERIFY
&&
329 dev
->status
& DEV_VERIFY_AND_FIX
)
332 return dev
->vol
.migr_type
;
335 static void set_migr_type(struct imsm_dev
*dev
, __u8 migr_type
)
337 /* for compatibility with older oroms convert MIGR_REPAIR, into
338 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
340 if (migr_type
== MIGR_REPAIR
) {
341 dev
->vol
.migr_type
= MIGR_VERIFY
;
342 dev
->status
|= DEV_VERIFY_AND_FIX
;
344 dev
->vol
.migr_type
= migr_type
;
345 dev
->status
&= ~DEV_VERIFY_AND_FIX
;
349 static unsigned int sector_count(__u32 bytes
, unsigned int sector_size
)
351 return ROUND_UP(bytes
, sector_size
) / sector_size
;
354 static unsigned int mpb_sectors(struct imsm_super
*mpb
,
355 unsigned int sector_size
)
357 return sector_count(__le32_to_cpu(mpb
->mpb_size
), sector_size
);
361 struct imsm_dev
*dev
;
362 struct intel_dev
*next
;
367 enum sys_dev_type type
;
370 struct intel_hba
*next
;
377 /* internal representation of IMSM metadata */
380 void *buf
; /* O_DIRECT buffer for reading/writing metadata */
381 struct imsm_super
*anchor
; /* immovable parameters */
384 void *migr_rec_buf
; /* buffer for I/O operations */
385 struct migr_record
*migr_rec
; /* migration record */
387 int clean_migration_record_by_mdmon
; /* when reshape is switched to next
388 array, it indicates that mdmon is allowed to clean migration
390 size_t len
; /* size of the 'buf' allocation */
391 size_t extra_space
; /* extra space in 'buf' that is not used yet */
392 void *next_buf
; /* for realloc'ing buf from the manager */
394 int updates_pending
; /* count of pending updates for mdmon */
395 int current_vol
; /* index of raid device undergoing creation */
396 unsigned long long create_offset
; /* common start for 'current_vol' */
397 __u32 random
; /* random data for seeding new family numbers */
398 struct intel_dev
*devlist
;
399 unsigned int sector_size
; /* sector size of used member drives */
403 __u8 serial
[MAX_RAID_SERIAL_LEN
];
406 struct imsm_disk disk
;
409 struct extent
*e
; /* for determining freespace @ create */
410 int raiddisk
; /* slot to fill in autolayout */
412 } *disks
, *current_disk
;
413 struct dl
*disk_mgmt_list
; /* list of disks to add/remove while mdmon
415 struct dl
*missing
; /* disks removed while we weren't looking */
416 struct bbm_log
*bbm_log
;
417 struct intel_hba
*hba
; /* device path of the raid controller for this metadata */
418 const struct imsm_orom
*orom
; /* platform firmware support */
419 struct intel_super
*next
; /* (temp) list for disambiguating family_num */
420 struct md_bb bb
; /* memory for get_bad_blocks call */
424 struct imsm_disk disk
;
425 #define IMSM_UNKNOWN_OWNER (-1)
427 struct intel_disk
*next
;
431 unsigned long long start
, size
;
434 /* definitions of reshape process types */
435 enum imsm_reshape_type
{
441 /* definition of messages passed to imsm_process_update */
442 enum imsm_update_type
{
443 update_activate_spare
,
447 update_add_remove_disk
,
448 update_reshape_container_disks
,
449 update_reshape_migration
,
451 update_general_migration_checkpoint
,
453 update_prealloc_badblocks_mem
,
457 struct imsm_update_activate_spare
{
458 enum imsm_update_type type
;
462 struct imsm_update_activate_spare
*next
;
468 unsigned long long size
;
475 enum takeover_direction
{
479 struct imsm_update_takeover
{
480 enum imsm_update_type type
;
482 enum takeover_direction direction
;
485 struct imsm_update_reshape
{
486 enum imsm_update_type type
;
490 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
493 struct imsm_update_reshape_migration
{
494 enum imsm_update_type type
;
497 /* fields for array migration changes
504 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
507 struct imsm_update_size_change
{
508 enum imsm_update_type type
;
513 struct imsm_update_general_migration_checkpoint
{
514 enum imsm_update_type type
;
515 __u32 curr_migr_unit
;
519 __u8 serial
[MAX_RAID_SERIAL_LEN
];
522 struct imsm_update_create_array
{
523 enum imsm_update_type type
;
528 struct imsm_update_kill_array
{
529 enum imsm_update_type type
;
533 struct imsm_update_rename_array
{
534 enum imsm_update_type type
;
535 __u8 name
[MAX_RAID_SERIAL_LEN
];
539 struct imsm_update_add_remove_disk
{
540 enum imsm_update_type type
;
543 struct imsm_update_prealloc_bb_mem
{
544 enum imsm_update_type type
;
547 struct imsm_update_rwh_policy
{
548 enum imsm_update_type type
;
553 static const char *_sys_dev_type
[] = {
554 [SYS_DEV_UNKNOWN
] = "Unknown",
555 [SYS_DEV_SAS
] = "SAS",
556 [SYS_DEV_SATA
] = "SATA",
557 [SYS_DEV_NVME
] = "NVMe",
558 [SYS_DEV_VMD
] = "VMD"
561 const char *get_sys_dev_type(enum sys_dev_type type
)
563 if (type
>= SYS_DEV_MAX
)
564 type
= SYS_DEV_UNKNOWN
;
566 return _sys_dev_type
[type
];
569 static struct intel_hba
* alloc_intel_hba(struct sys_dev
*device
)
571 struct intel_hba
*result
= xmalloc(sizeof(*result
));
573 result
->type
= device
->type
;
574 result
->path
= xstrdup(device
->path
);
576 if (result
->path
&& (result
->pci_id
= strrchr(result
->path
, '/')) != NULL
)
582 static struct intel_hba
* find_intel_hba(struct intel_hba
*hba
, struct sys_dev
*device
)
584 struct intel_hba
*result
;
586 for (result
= hba
; result
; result
= result
->next
) {
587 if (result
->type
== device
->type
&& strcmp(result
->path
, device
->path
) == 0)
593 static int attach_hba_to_super(struct intel_super
*super
, struct sys_dev
*device
)
595 struct intel_hba
*hba
;
597 /* check if disk attached to Intel HBA */
598 hba
= find_intel_hba(super
->hba
, device
);
601 /* Check if HBA is already attached to super */
602 if (super
->hba
== NULL
) {
603 super
->hba
= alloc_intel_hba(device
);
608 /* Intel metadata allows for all disks attached to the same type HBA.
609 * Do not support HBA types mixing
611 if (device
->type
!= hba
->type
)
614 /* Multiple same type HBAs can be used if they share the same OROM */
615 const struct imsm_orom
*device_orom
= get_orom_by_device_id(device
->dev_id
);
617 if (device_orom
!= super
->orom
)
623 hba
->next
= alloc_intel_hba(device
);
627 static struct sys_dev
* find_disk_attached_hba(int fd
, const char *devname
)
629 struct sys_dev
*list
, *elem
;
632 if ((list
= find_intel_devices()) == NULL
)
636 disk_path
= (char *) devname
;
638 disk_path
= diskfd_to_devpath(fd
);
643 for (elem
= list
; elem
; elem
= elem
->next
)
644 if (path_attached_to_hba(disk_path
, elem
->path
))
647 if (disk_path
!= devname
)
653 static int find_intel_hba_capability(int fd
, struct intel_super
*super
,
656 static struct supertype
*match_metadata_desc_imsm(char *arg
)
658 struct supertype
*st
;
660 if (strcmp(arg
, "imsm") != 0 &&
661 strcmp(arg
, "default") != 0
665 st
= xcalloc(1, sizeof(*st
));
666 st
->ss
= &super_imsm
;
667 st
->max_devs
= IMSM_MAX_DEVICES
;
668 st
->minor_version
= 0;
673 static __u8
*get_imsm_version(struct imsm_super
*mpb
)
675 return &mpb
->sig
[MPB_SIG_LEN
];
678 /* retrieve a disk directly from the anchor when the anchor is known to be
679 * up-to-date, currently only at load time
681 static struct imsm_disk
*__get_imsm_disk(struct imsm_super
*mpb
, __u8 index
)
683 if (index
>= mpb
->num_disks
)
685 return &mpb
->disk
[index
];
688 /* retrieve the disk description based on a index of the disk
691 static struct dl
*get_imsm_dl_disk(struct intel_super
*super
, __u8 index
)
695 for (d
= super
->disks
; d
; d
= d
->next
)
696 if (d
->index
== index
)
701 /* retrieve a disk from the parsed metadata */
702 static struct imsm_disk
*get_imsm_disk(struct intel_super
*super
, __u8 index
)
706 dl
= get_imsm_dl_disk(super
, index
);
713 /* generate a checksum directly from the anchor when the anchor is known to be
714 * up-to-date, currently only at load or write_super after coalescing
716 static __u32
__gen_imsm_checksum(struct imsm_super
*mpb
)
718 __u32 end
= mpb
->mpb_size
/ sizeof(end
);
719 __u32
*p
= (__u32
*) mpb
;
723 sum
+= __le32_to_cpu(*p
);
727 return sum
- __le32_to_cpu(mpb
->check_sum
);
730 static size_t sizeof_imsm_map(struct imsm_map
*map
)
732 return sizeof(struct imsm_map
) + sizeof(__u32
) * (map
->num_members
- 1);
735 struct imsm_map
*get_imsm_map(struct imsm_dev
*dev
, int second_map
)
737 /* A device can have 2 maps if it is in the middle of a migration.
739 * MAP_0 - we return the first map
740 * MAP_1 - we return the second map if it exists, else NULL
741 * MAP_X - we return the second map if it exists, else the first
743 struct imsm_map
*map
= &dev
->vol
.map
[0];
744 struct imsm_map
*map2
= NULL
;
746 if (dev
->vol
.migr_state
)
747 map2
= (void *)map
+ sizeof_imsm_map(map
);
749 switch (second_map
) {
766 /* return the size of the device.
767 * migr_state increases the returned size if map[0] were to be duplicated
769 static size_t sizeof_imsm_dev(struct imsm_dev
*dev
, int migr_state
)
771 size_t size
= sizeof(*dev
) - sizeof(struct imsm_map
) +
772 sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
774 /* migrating means an additional map */
775 if (dev
->vol
.migr_state
)
776 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_1
));
778 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
783 /* retrieve disk serial number list from a metadata update */
784 static struct disk_info
*get_disk_info(struct imsm_update_create_array
*update
)
787 struct disk_info
*inf
;
789 inf
= u
+ sizeof(*update
) - sizeof(struct imsm_dev
) +
790 sizeof_imsm_dev(&update
->dev
, 0);
795 static struct imsm_dev
*__get_imsm_dev(struct imsm_super
*mpb
, __u8 index
)
801 if (index
>= mpb
->num_raid_devs
)
804 /* devices start after all disks */
805 offset
= ((void *) &mpb
->disk
[mpb
->num_disks
]) - _mpb
;
807 for (i
= 0; i
<= index
; i
++)
809 return _mpb
+ offset
;
811 offset
+= sizeof_imsm_dev(_mpb
+ offset
, 0);
816 static struct imsm_dev
*get_imsm_dev(struct intel_super
*super
, __u8 index
)
818 struct intel_dev
*dv
;
820 if (index
>= super
->anchor
->num_raid_devs
)
822 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
823 if (dv
->index
== index
)
828 static inline unsigned long long __le48_to_cpu(const struct bbm_log_block_addr
831 return ((((__u64
)__le32_to_cpu(addr
->dw1
)) << 16) |
832 __le16_to_cpu(addr
->w1
));
835 static inline struct bbm_log_block_addr
__cpu_to_le48(unsigned long long sec
)
837 struct bbm_log_block_addr addr
;
839 addr
.w1
= __cpu_to_le16((__u16
)(sec
& 0xffff));
840 addr
.dw1
= __cpu_to_le32((__u32
)(sec
>> 16) & 0xffffffff);
844 /* get size of the bbm log */
845 static __u32
get_imsm_bbm_log_size(struct bbm_log
*log
)
847 if (!log
|| log
->entry_count
== 0)
850 return sizeof(log
->signature
) +
851 sizeof(log
->entry_count
) +
852 log
->entry_count
* sizeof(struct bbm_log_entry
);
855 /* check if bad block is not partially stored in bbm log */
856 static int is_stored_in_bbm(struct bbm_log
*log
, const __u8 idx
, const unsigned
857 long long sector
, const int length
, __u32
*pos
)
861 for (i
= *pos
; i
< log
->entry_count
; i
++) {
862 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[i
];
863 unsigned long long bb_start
;
864 unsigned long long bb_end
;
866 bb_start
= __le48_to_cpu(&entry
->defective_block_start
);
867 bb_end
= bb_start
+ (entry
->marked_count
+ 1);
869 if ((entry
->disk_ordinal
== idx
) && (bb_start
>= sector
) &&
870 (bb_end
<= sector
+ length
)) {
878 /* record new bad block in bbm log */
879 static int record_new_badblock(struct bbm_log
*log
, const __u8 idx
, unsigned
880 long long sector
, int length
)
884 struct bbm_log_entry
*entry
= NULL
;
886 while (is_stored_in_bbm(log
, idx
, sector
, length
, &pos
)) {
887 struct bbm_log_entry
*e
= &log
->marked_block_entries
[pos
];
889 if ((e
->marked_count
+ 1 == BBM_LOG_MAX_LBA_ENTRY_VAL
) &&
890 (__le48_to_cpu(&e
->defective_block_start
) == sector
)) {
891 sector
+= BBM_LOG_MAX_LBA_ENTRY_VAL
;
892 length
-= BBM_LOG_MAX_LBA_ENTRY_VAL
;
901 int cnt
= (length
<= BBM_LOG_MAX_LBA_ENTRY_VAL
) ? length
:
902 BBM_LOG_MAX_LBA_ENTRY_VAL
;
903 entry
->defective_block_start
= __cpu_to_le48(sector
);
904 entry
->marked_count
= cnt
- 1;
911 new_bb
= ROUND_UP(length
, BBM_LOG_MAX_LBA_ENTRY_VAL
) /
912 BBM_LOG_MAX_LBA_ENTRY_VAL
;
913 if (log
->entry_count
+ new_bb
> BBM_LOG_MAX_ENTRIES
)
917 int cnt
= (length
<= BBM_LOG_MAX_LBA_ENTRY_VAL
) ? length
:
918 BBM_LOG_MAX_LBA_ENTRY_VAL
;
919 struct bbm_log_entry
*entry
=
920 &log
->marked_block_entries
[log
->entry_count
];
922 entry
->defective_block_start
= __cpu_to_le48(sector
);
923 entry
->marked_count
= cnt
- 1;
924 entry
->disk_ordinal
= idx
;
935 /* clear all bad blocks for given disk */
936 static void clear_disk_badblocks(struct bbm_log
*log
, const __u8 idx
)
940 while (i
< log
->entry_count
) {
941 struct bbm_log_entry
*entries
= log
->marked_block_entries
;
943 if (entries
[i
].disk_ordinal
== idx
) {
944 if (i
< log
->entry_count
- 1)
945 entries
[i
] = entries
[log
->entry_count
- 1];
953 /* clear given bad block */
954 static int clear_badblock(struct bbm_log
*log
, const __u8 idx
, const unsigned
955 long long sector
, const int length
) {
958 while (i
< log
->entry_count
) {
959 struct bbm_log_entry
*entries
= log
->marked_block_entries
;
961 if ((entries
[i
].disk_ordinal
== idx
) &&
962 (__le48_to_cpu(&entries
[i
].defective_block_start
) ==
963 sector
) && (entries
[i
].marked_count
+ 1 == length
)) {
964 if (i
< log
->entry_count
- 1)
965 entries
[i
] = entries
[log
->entry_count
- 1];
975 /* allocate and load BBM log from metadata */
976 static int load_bbm_log(struct intel_super
*super
)
978 struct imsm_super
*mpb
= super
->anchor
;
979 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
981 super
->bbm_log
= xcalloc(1, sizeof(struct bbm_log
));
986 struct bbm_log
*log
= (void *)mpb
+
987 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
991 if (bbm_log_size
< sizeof(log
->signature
) +
992 sizeof(log
->entry_count
))
995 entry_count
= __le32_to_cpu(log
->entry_count
);
996 if ((__le32_to_cpu(log
->signature
) != BBM_LOG_SIGNATURE
) ||
997 (entry_count
> BBM_LOG_MAX_ENTRIES
))
1001 sizeof(log
->signature
) + sizeof(log
->entry_count
) +
1002 entry_count
* sizeof(struct bbm_log_entry
))
1005 memcpy(super
->bbm_log
, log
, bbm_log_size
);
1007 super
->bbm_log
->signature
= __cpu_to_le32(BBM_LOG_SIGNATURE
);
1008 super
->bbm_log
->entry_count
= 0;
1014 /* checks if bad block is within volume boundaries */
1015 static int is_bad_block_in_volume(const struct bbm_log_entry
*entry
,
1016 const unsigned long long start_sector
,
1017 const unsigned long long size
)
1019 unsigned long long bb_start
;
1020 unsigned long long bb_end
;
1022 bb_start
= __le48_to_cpu(&entry
->defective_block_start
);
1023 bb_end
= bb_start
+ (entry
->marked_count
+ 1);
1025 if (((bb_start
>= start_sector
) && (bb_start
< start_sector
+ size
)) ||
1026 ((bb_end
>= start_sector
) && (bb_end
<= start_sector
+ size
)))
1032 /* get list of bad blocks on a drive for a volume */
1033 static void get_volume_badblocks(const struct bbm_log
*log
, const __u8 idx
,
1034 const unsigned long long start_sector
,
1035 const unsigned long long size
,
1041 for (i
= 0; i
< log
->entry_count
; i
++) {
1042 const struct bbm_log_entry
*ent
=
1043 &log
->marked_block_entries
[i
];
1044 struct md_bb_entry
*bb
;
1046 if ((ent
->disk_ordinal
== idx
) &&
1047 is_bad_block_in_volume(ent
, start_sector
, size
)) {
1049 if (!bbs
->entries
) {
1050 bbs
->entries
= xmalloc(BBM_LOG_MAX_ENTRIES
*
1056 bb
= &bbs
->entries
[count
++];
1057 bb
->sector
= __le48_to_cpu(&ent
->defective_block_start
);
1058 bb
->length
= ent
->marked_count
+ 1;
1066 * == MAP_0 get first map
1067 * == MAP_1 get second map
1068 * == MAP_X than get map according to the current migr_state
1070 static __u32
get_imsm_ord_tbl_ent(struct imsm_dev
*dev
,
1074 struct imsm_map
*map
;
1076 map
= get_imsm_map(dev
, second_map
);
1078 /* top byte identifies disk under rebuild */
1079 return __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
1082 #define ord_to_idx(ord) (((ord) << 8) >> 8)
1083 static __u32
get_imsm_disk_idx(struct imsm_dev
*dev
, int slot
, int second_map
)
1085 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, second_map
);
1087 return ord_to_idx(ord
);
1090 static void set_imsm_ord_tbl_ent(struct imsm_map
*map
, int slot
, __u32 ord
)
1092 map
->disk_ord_tbl
[slot
] = __cpu_to_le32(ord
);
1095 static int get_imsm_disk_slot(struct imsm_map
*map
, unsigned idx
)
1100 for (slot
= 0; slot
< map
->num_members
; slot
++) {
1101 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
1102 if (ord_to_idx(ord
) == idx
)
1109 static int get_imsm_raid_level(struct imsm_map
*map
)
1111 if (map
->raid_level
== 1) {
1112 if (map
->num_members
== 2)
1118 return map
->raid_level
;
1121 static int cmp_extent(const void *av
, const void *bv
)
1123 const struct extent
*a
= av
;
1124 const struct extent
*b
= bv
;
1125 if (a
->start
< b
->start
)
1127 if (a
->start
> b
->start
)
1132 static int count_memberships(struct dl
*dl
, struct intel_super
*super
)
1134 int memberships
= 0;
1137 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1138 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1139 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1141 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
1148 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
);
1150 static int split_ull(unsigned long long n
, __u32
*lo
, __u32
*hi
)
1152 if (lo
== 0 || hi
== 0)
1154 *lo
= __le32_to_cpu((unsigned)n
);
1155 *hi
= __le32_to_cpu((unsigned)(n
>> 32));
1159 static unsigned long long join_u32(__u32 lo
, __u32 hi
)
1161 return (unsigned long long)__le32_to_cpu(lo
) |
1162 (((unsigned long long)__le32_to_cpu(hi
)) << 32);
1165 static unsigned long long total_blocks(struct imsm_disk
*disk
)
1169 return join_u32(disk
->total_blocks_lo
, disk
->total_blocks_hi
);
1172 static unsigned long long pba_of_lba0(struct imsm_map
*map
)
1176 return join_u32(map
->pba_of_lba0_lo
, map
->pba_of_lba0_hi
);
1179 static unsigned long long blocks_per_member(struct imsm_map
*map
)
1183 return join_u32(map
->blocks_per_member_lo
, map
->blocks_per_member_hi
);
1186 static unsigned long long num_data_stripes(struct imsm_map
*map
)
1190 return join_u32(map
->num_data_stripes_lo
, map
->num_data_stripes_hi
);
1193 static void set_total_blocks(struct imsm_disk
*disk
, unsigned long long n
)
1195 split_ull(n
, &disk
->total_blocks_lo
, &disk
->total_blocks_hi
);
1198 static void set_pba_of_lba0(struct imsm_map
*map
, unsigned long long n
)
1200 split_ull(n
, &map
->pba_of_lba0_lo
, &map
->pba_of_lba0_hi
);
1203 static void set_blocks_per_member(struct imsm_map
*map
, unsigned long long n
)
1205 split_ull(n
, &map
->blocks_per_member_lo
, &map
->blocks_per_member_hi
);
1208 static void set_num_data_stripes(struct imsm_map
*map
, unsigned long long n
)
1210 split_ull(n
, &map
->num_data_stripes_lo
, &map
->num_data_stripes_hi
);
1213 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
)
1215 /* find a list of used extents on the given physical device */
1216 struct extent
*rv
, *e
;
1218 int memberships
= count_memberships(dl
, super
);
1221 /* trim the reserved area for spares, so they can join any array
1222 * regardless of whether the OROM has assigned sectors from the
1223 * IMSM_RESERVED_SECTORS region
1225 if (dl
->index
== -1)
1226 reservation
= imsm_min_reserved_sectors(super
);
1228 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1230 rv
= xcalloc(sizeof(struct extent
), (memberships
+ 1));
1233 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1234 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1235 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1237 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
1238 e
->start
= pba_of_lba0(map
);
1239 e
->size
= blocks_per_member(map
);
1243 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
1245 /* determine the start of the metadata
1246 * when no raid devices are defined use the default
1247 * ...otherwise allow the metadata to truncate the value
1248 * as is the case with older versions of imsm
1251 struct extent
*last
= &rv
[memberships
- 1];
1252 unsigned long long remainder
;
1254 remainder
= total_blocks(&dl
->disk
) - (last
->start
+ last
->size
);
1255 /* round down to 1k block to satisfy precision of the kernel
1259 /* make sure remainder is still sane */
1260 if (remainder
< (unsigned)ROUND_UP(super
->len
, 512) >> 9)
1261 remainder
= ROUND_UP(super
->len
, 512) >> 9;
1262 if (reservation
> remainder
)
1263 reservation
= remainder
;
1265 e
->start
= total_blocks(&dl
->disk
) - reservation
;
1270 /* try to determine how much space is reserved for metadata from
1271 * the last get_extents() entry, otherwise fallback to the
1274 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
1280 /* for spares just return a minimal reservation which will grow
1281 * once the spare is picked up by an array
1283 if (dl
->index
== -1)
1284 return MPB_SECTOR_CNT
;
1286 e
= get_extents(super
, dl
);
1288 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1290 /* scroll to last entry */
1291 for (i
= 0; e
[i
].size
; i
++)
1294 rv
= total_blocks(&dl
->disk
) - e
[i
].start
;
1301 static int is_spare(struct imsm_disk
*disk
)
1303 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
1306 static int is_configured(struct imsm_disk
*disk
)
1308 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
1311 static int is_failed(struct imsm_disk
*disk
)
1313 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
1316 static int is_journal(struct imsm_disk
*disk
)
1318 return (disk
->status
& JOURNAL_DISK
) == JOURNAL_DISK
;
1321 /* round array size down to closest MB and ensure it splits evenly
1324 static unsigned long long round_size_to_mb(unsigned long long size
, unsigned int
1328 size
= (size
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
1334 /* try to determine how much space is reserved for metadata from
1335 * the last get_extents() entry on the smallest active disk,
1336 * otherwise fallback to the default
1338 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
)
1342 unsigned long long min_active
;
1344 __u32 rv
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1345 struct dl
*dl
, *dl_min
= NULL
;
1351 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
1354 unsigned long long blocks
= total_blocks(&dl
->disk
);
1355 if (blocks
< min_active
|| min_active
== 0) {
1357 min_active
= blocks
;
1363 /* find last lba used by subarrays on the smallest active disk */
1364 e
= get_extents(super
, dl_min
);
1367 for (i
= 0; e
[i
].size
; i
++)
1370 remainder
= min_active
- e
[i
].start
;
1373 /* to give priority to recovery we should not require full
1374 IMSM_RESERVED_SECTORS from the spare */
1375 rv
= MPB_SECTOR_CNT
+ NUM_BLOCKS_DIRTY_STRIPE_REGION
;
1377 /* if real reservation is smaller use that value */
1378 return (remainder
< rv
) ? remainder
: rv
;
1381 /* Return minimum size of a spare that can be used in this array*/
1382 static unsigned long long min_acceptable_spare_size_imsm(struct supertype
*st
)
1384 struct intel_super
*super
= st
->sb
;
1388 unsigned long long rv
= 0;
1392 /* find first active disk in array */
1394 while (dl
&& (is_failed(&dl
->disk
) || dl
->index
== -1))
1398 /* find last lba used by subarrays */
1399 e
= get_extents(super
, dl
);
1402 for (i
= 0; e
[i
].size
; i
++)
1405 rv
= e
[i
-1].start
+ e
[i
-1].size
;
1408 /* add the amount of space needed for metadata */
1409 rv
= rv
+ imsm_min_reserved_sectors(super
);
1414 static int is_gen_migration(struct imsm_dev
*dev
);
1416 #define IMSM_4K_DIV 8
1418 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
1419 struct imsm_dev
*dev
);
1421 static void print_imsm_dev(struct intel_super
*super
,
1422 struct imsm_dev
*dev
,
1428 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1429 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
1433 printf("[%.16s]:\n", dev
->volume
);
1434 printf(" UUID : %s\n", uuid
);
1435 printf(" RAID Level : %d", get_imsm_raid_level(map
));
1437 printf(" <-- %d", get_imsm_raid_level(map2
));
1439 printf(" Members : %d", map
->num_members
);
1441 printf(" <-- %d", map2
->num_members
);
1443 printf(" Slots : [");
1444 for (i
= 0; i
< map
->num_members
; i
++) {
1445 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
1446 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1451 for (i
= 0; i
< map2
->num_members
; i
++) {
1452 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_1
);
1453 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1458 printf(" Failed disk : ");
1459 if (map
->failed_disk_num
== 0xff)
1462 printf("%i", map
->failed_disk_num
);
1464 slot
= get_imsm_disk_slot(map
, disk_idx
);
1466 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
1467 printf(" This Slot : %d%s\n", slot
,
1468 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
1470 printf(" This Slot : ?\n");
1471 sz
= __le32_to_cpu(dev
->size_high
);
1473 sz
+= __le32_to_cpu(dev
->size_low
);
1474 printf(" Array Size : %llu%s\n", (unsigned long long)sz
,
1475 human_size(sz
* 512));
1476 sz
= blocks_per_member(map
);
1477 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz
,
1478 human_size(sz
* 512));
1479 printf(" Sector Offset : %llu\n",
1481 printf(" Num Stripes : %llu\n",
1482 num_data_stripes(map
));
1483 printf(" Chunk Size : %u KiB",
1484 __le16_to_cpu(map
->blocks_per_strip
) / 2);
1486 printf(" <-- %u KiB",
1487 __le16_to_cpu(map2
->blocks_per_strip
) / 2);
1489 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
1490 printf(" Migrate State : ");
1491 if (dev
->vol
.migr_state
) {
1492 if (migr_type(dev
) == MIGR_INIT
)
1493 printf("initialize\n");
1494 else if (migr_type(dev
) == MIGR_REBUILD
)
1495 printf("rebuild\n");
1496 else if (migr_type(dev
) == MIGR_VERIFY
)
1498 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
1499 printf("general migration\n");
1500 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
1501 printf("state change\n");
1502 else if (migr_type(dev
) == MIGR_REPAIR
)
1505 printf("<unknown:%d>\n", migr_type(dev
));
1508 printf(" Map State : %s", map_state_str
[map
->map_state
]);
1509 if (dev
->vol
.migr_state
) {
1510 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1512 printf(" <-- %s", map_state_str
[map
->map_state
]);
1513 printf("\n Checkpoint : %u ",
1514 __le32_to_cpu(dev
->vol
.curr_migr_unit
));
1515 if (is_gen_migration(dev
) && (slot
> 1 || slot
< 0))
1518 printf("(%llu)", (unsigned long long)
1519 blocks_per_migr_unit(super
, dev
));
1522 printf(" Dirty State : %s\n", (dev
->vol
.dirty
& RAIDVOL_DIRTY
) ?
1524 printf(" RWH Policy : ");
1525 if (dev
->rwh_policy
== RWH_OFF
)
1527 else if (dev
->rwh_policy
== RWH_DISTRIBUTED
)
1528 printf("PPL distributed\n");
1529 else if (dev
->rwh_policy
== RWH_JOURNALING_DRIVE
)
1530 printf("PPL journaling drive\n");
1532 printf("<unknown:%d>\n", dev
->rwh_policy
);
1535 static void print_imsm_disk(struct imsm_disk
*disk
,
1538 unsigned int sector_size
) {
1539 char str
[MAX_RAID_SERIAL_LEN
+ 1];
1542 if (index
< -1 || !disk
)
1546 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
1548 printf(" Disk%02d Serial : %s\n", index
, str
);
1550 printf(" Disk Serial : %s\n", str
);
1551 printf(" State :%s%s%s%s\n", is_spare(disk
) ? " spare" : "",
1552 is_configured(disk
) ? " active" : "",
1553 is_failed(disk
) ? " failed" : "",
1554 is_journal(disk
) ? " journal" : "");
1555 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
1556 sz
= total_blocks(disk
) - reserved
;
1557 printf(" Usable Size : %llu%s\n",
1558 (unsigned long long)sz
* 512 / sector_size
,
1559 human_size(sz
* 512));
1562 void convert_to_4k_imsm_migr_rec(struct intel_super
*super
)
1564 struct migr_record
*migr_rec
= super
->migr_rec
;
1566 migr_rec
->blocks_per_unit
/= IMSM_4K_DIV
;
1567 migr_rec
->ckpt_area_pba
/= IMSM_4K_DIV
;
1568 migr_rec
->dest_1st_member_lba
/= IMSM_4K_DIV
;
1569 migr_rec
->dest_depth_per_unit
/= IMSM_4K_DIV
;
1570 split_ull((join_u32(migr_rec
->post_migr_vol_cap
,
1571 migr_rec
->post_migr_vol_cap_hi
) / IMSM_4K_DIV
),
1572 &migr_rec
->post_migr_vol_cap
, &migr_rec
->post_migr_vol_cap_hi
);
1575 void convert_to_4k_imsm_disk(struct imsm_disk
*disk
)
1577 set_total_blocks(disk
, (total_blocks(disk
)/IMSM_4K_DIV
));
1580 void convert_to_4k(struct intel_super
*super
)
1582 struct imsm_super
*mpb
= super
->anchor
;
1583 struct imsm_disk
*disk
;
1585 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
1587 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1588 disk
= __get_imsm_disk(mpb
, i
);
1590 convert_to_4k_imsm_disk(disk
);
1592 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1593 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1594 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1596 split_ull((join_u32(dev
->size_low
, dev
->size_high
)/IMSM_4K_DIV
),
1597 &dev
->size_low
, &dev
->size_high
);
1598 dev
->vol
.curr_migr_unit
/= IMSM_4K_DIV
;
1601 set_blocks_per_member(map
, blocks_per_member(map
)/IMSM_4K_DIV
);
1602 map
->blocks_per_strip
/= IMSM_4K_DIV
;
1603 set_pba_of_lba0(map
, pba_of_lba0(map
)/IMSM_4K_DIV
);
1605 if (dev
->vol
.migr_state
) {
1607 map
= get_imsm_map(dev
, MAP_1
);
1608 set_blocks_per_member(map
,
1609 blocks_per_member(map
)/IMSM_4K_DIV
);
1610 map
->blocks_per_strip
/= IMSM_4K_DIV
;
1611 set_pba_of_lba0(map
, pba_of_lba0(map
)/IMSM_4K_DIV
);
1615 struct bbm_log
*log
= (void *)mpb
+
1616 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
1619 for (i
= 0; i
< log
->entry_count
; i
++) {
1620 struct bbm_log_entry
*entry
=
1621 &log
->marked_block_entries
[i
];
1623 __u8 count
= entry
->marked_count
+ 1;
1624 unsigned long long sector
=
1625 __le48_to_cpu(&entry
->defective_block_start
);
1627 entry
->defective_block_start
=
1628 __cpu_to_le48(sector
/IMSM_4K_DIV
);
1629 entry
->marked_count
= max(count
/IMSM_4K_DIV
, 1) - 1;
1633 mpb
->check_sum
= __gen_imsm_checksum(mpb
);
1636 void examine_migr_rec_imsm(struct intel_super
*super
)
1638 struct migr_record
*migr_rec
= super
->migr_rec
;
1639 struct imsm_super
*mpb
= super
->anchor
;
1642 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1643 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1644 struct imsm_map
*map
;
1647 if (is_gen_migration(dev
) == 0)
1650 printf("\nMigration Record Information:");
1652 /* first map under migration */
1653 map
= get_imsm_map(dev
, MAP_0
);
1655 slot
= get_imsm_disk_slot(map
, super
->disks
->index
);
1656 if (map
== NULL
|| slot
> 1 || slot
< 0) {
1657 printf(" Empty\n ");
1658 printf("Examine one of first two disks in array\n");
1661 printf("\n Status : ");
1662 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
1665 printf("Contains Data\n");
1666 printf(" Current Unit : %u\n",
1667 __le32_to_cpu(migr_rec
->curr_migr_unit
));
1668 printf(" Family : %u\n",
1669 __le32_to_cpu(migr_rec
->family_num
));
1670 printf(" Ascending : %u\n",
1671 __le32_to_cpu(migr_rec
->ascending_migr
));
1672 printf(" Blocks Per Unit : %u\n",
1673 __le32_to_cpu(migr_rec
->blocks_per_unit
));
1674 printf(" Dest. Depth Per Unit : %u\n",
1675 __le32_to_cpu(migr_rec
->dest_depth_per_unit
));
1676 printf(" Checkpoint Area pba : %u\n",
1677 __le32_to_cpu(migr_rec
->ckpt_area_pba
));
1678 printf(" First member lba : %u\n",
1679 __le32_to_cpu(migr_rec
->dest_1st_member_lba
));
1680 printf(" Total Number of Units : %u\n",
1681 __le32_to_cpu(migr_rec
->num_migr_units
));
1682 printf(" Size of volume : %u\n",
1683 __le32_to_cpu(migr_rec
->post_migr_vol_cap
));
1684 printf(" Expansion space for LBA64 : %u\n",
1685 __le32_to_cpu(migr_rec
->post_migr_vol_cap_hi
));
1686 printf(" Record was read from : %u\n",
1687 __le32_to_cpu(migr_rec
->ckpt_read_disk_num
));
1693 void convert_from_4k_imsm_migr_rec(struct intel_super
*super
)
1695 struct migr_record
*migr_rec
= super
->migr_rec
;
1697 migr_rec
->blocks_per_unit
*= IMSM_4K_DIV
;
1698 migr_rec
->ckpt_area_pba
*= IMSM_4K_DIV
;
1699 migr_rec
->dest_1st_member_lba
*= IMSM_4K_DIV
;
1700 migr_rec
->dest_depth_per_unit
*= IMSM_4K_DIV
;
1701 split_ull((join_u32(migr_rec
->post_migr_vol_cap
,
1702 migr_rec
->post_migr_vol_cap_hi
) * IMSM_4K_DIV
),
1703 &migr_rec
->post_migr_vol_cap
,
1704 &migr_rec
->post_migr_vol_cap_hi
);
1707 void convert_from_4k(struct intel_super
*super
)
1709 struct imsm_super
*mpb
= super
->anchor
;
1710 struct imsm_disk
*disk
;
1712 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
1714 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1715 disk
= __get_imsm_disk(mpb
, i
);
1717 set_total_blocks(disk
, (total_blocks(disk
)*IMSM_4K_DIV
));
1720 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1721 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1722 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1724 split_ull((join_u32(dev
->size_low
, dev
->size_high
)*IMSM_4K_DIV
),
1725 &dev
->size_low
, &dev
->size_high
);
1726 dev
->vol
.curr_migr_unit
*= IMSM_4K_DIV
;
1729 set_blocks_per_member(map
, blocks_per_member(map
)*IMSM_4K_DIV
);
1730 map
->blocks_per_strip
*= IMSM_4K_DIV
;
1731 set_pba_of_lba0(map
, pba_of_lba0(map
)*IMSM_4K_DIV
);
1733 if (dev
->vol
.migr_state
) {
1735 map
= get_imsm_map(dev
, MAP_1
);
1736 set_blocks_per_member(map
,
1737 blocks_per_member(map
)*IMSM_4K_DIV
);
1738 map
->blocks_per_strip
*= IMSM_4K_DIV
;
1739 set_pba_of_lba0(map
, pba_of_lba0(map
)*IMSM_4K_DIV
);
1743 struct bbm_log
*log
= (void *)mpb
+
1744 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
1747 for (i
= 0; i
< log
->entry_count
; i
++) {
1748 struct bbm_log_entry
*entry
=
1749 &log
->marked_block_entries
[i
];
1751 __u8 count
= entry
->marked_count
+ 1;
1752 unsigned long long sector
=
1753 __le48_to_cpu(&entry
->defective_block_start
);
1755 entry
->defective_block_start
=
1756 __cpu_to_le48(sector
*IMSM_4K_DIV
);
1757 entry
->marked_count
= count
*IMSM_4K_DIV
- 1;
1761 mpb
->check_sum
= __gen_imsm_checksum(mpb
);
1764 /*******************************************************************************
1765 * function: imsm_check_attributes
1766 * Description: Function checks if features represented by attributes flags
1767 * are supported by mdadm.
1769 * attributes - Attributes read from metadata
1771 * 0 - passed attributes contains unsupported features flags
1772 * 1 - all features are supported
1773 ******************************************************************************/
1774 static int imsm_check_attributes(__u32 attributes
)
1777 __u32 not_supported
= MPB_ATTRIB_SUPPORTED
^0xffffffff;
1779 not_supported
&= ~MPB_ATTRIB_IGNORED
;
1781 not_supported
&= attributes
;
1782 if (not_supported
) {
1783 pr_err("(IMSM): Unsupported attributes : %x\n",
1784 (unsigned)__le32_to_cpu(not_supported
));
1785 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1786 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
1787 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1789 if (not_supported
& MPB_ATTRIB_2TB
) {
1790 dprintf("\t\tMPB_ATTRIB_2TB\n");
1791 not_supported
^= MPB_ATTRIB_2TB
;
1793 if (not_supported
& MPB_ATTRIB_RAID0
) {
1794 dprintf("\t\tMPB_ATTRIB_RAID0\n");
1795 not_supported
^= MPB_ATTRIB_RAID0
;
1797 if (not_supported
& MPB_ATTRIB_RAID1
) {
1798 dprintf("\t\tMPB_ATTRIB_RAID1\n");
1799 not_supported
^= MPB_ATTRIB_RAID1
;
1801 if (not_supported
& MPB_ATTRIB_RAID10
) {
1802 dprintf("\t\tMPB_ATTRIB_RAID10\n");
1803 not_supported
^= MPB_ATTRIB_RAID10
;
1805 if (not_supported
& MPB_ATTRIB_RAID1E
) {
1806 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
1807 not_supported
^= MPB_ATTRIB_RAID1E
;
1809 if (not_supported
& MPB_ATTRIB_RAID5
) {
1810 dprintf("\t\tMPB_ATTRIB_RAID5\n");
1811 not_supported
^= MPB_ATTRIB_RAID5
;
1813 if (not_supported
& MPB_ATTRIB_RAIDCNG
) {
1814 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
1815 not_supported
^= MPB_ATTRIB_RAIDCNG
;
1817 if (not_supported
& MPB_ATTRIB_BBM
) {
1818 dprintf("\t\tMPB_ATTRIB_BBM\n");
1819 not_supported
^= MPB_ATTRIB_BBM
;
1821 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1822 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
1823 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1825 if (not_supported
& MPB_ATTRIB_EXP_STRIPE_SIZE
) {
1826 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
1827 not_supported
^= MPB_ATTRIB_EXP_STRIPE_SIZE
;
1829 if (not_supported
& MPB_ATTRIB_2TB_DISK
) {
1830 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
1831 not_supported
^= MPB_ATTRIB_2TB_DISK
;
1833 if (not_supported
& MPB_ATTRIB_NEVER_USE2
) {
1834 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
1835 not_supported
^= MPB_ATTRIB_NEVER_USE2
;
1837 if (not_supported
& MPB_ATTRIB_NEVER_USE
) {
1838 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
1839 not_supported
^= MPB_ATTRIB_NEVER_USE
;
1843 dprintf("(IMSM): Unknown attributes : %x\n", not_supported
);
1851 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
1853 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
1855 struct intel_super
*super
= st
->sb
;
1856 struct imsm_super
*mpb
= super
->anchor
;
1857 char str
[MAX_SIGNATURE_LENGTH
];
1862 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
1865 strncpy(str
, (char *)mpb
->sig
, MPB_SIG_LEN
);
1866 str
[MPB_SIG_LEN
-1] = '\0';
1867 printf(" Magic : %s\n", str
);
1868 snprintf(str
, strlen(MPB_VERSION_RAID0
), "%s", get_imsm_version(mpb
));
1869 printf(" Version : %s\n", get_imsm_version(mpb
));
1870 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
1871 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
1872 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
1873 printf(" Attributes : ");
1874 if (imsm_check_attributes(mpb
->attributes
))
1875 printf("All supported\n");
1877 printf("not supported\n");
1878 getinfo_super_imsm(st
, &info
, NULL
);
1879 fname_from_uuid(st
, &info
, nbuf
, ':');
1880 printf(" UUID : %s\n", nbuf
+ 5);
1881 sum
= __le32_to_cpu(mpb
->check_sum
);
1882 printf(" Checksum : %08x %s\n", sum
,
1883 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
1884 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
, super
->sector_size
));
1885 printf(" Disks : %d\n", mpb
->num_disks
);
1886 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
1887 print_imsm_disk(__get_imsm_disk(mpb
, super
->disks
->index
),
1888 super
->disks
->index
, reserved
, super
->sector_size
);
1889 if (get_imsm_bbm_log_size(super
->bbm_log
)) {
1890 struct bbm_log
*log
= super
->bbm_log
;
1893 printf("Bad Block Management Log:\n");
1894 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
1895 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
1896 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
1898 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1900 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1902 super
->current_vol
= i
;
1903 getinfo_super_imsm(st
, &info
, NULL
);
1904 fname_from_uuid(st
, &info
, nbuf
, ':');
1905 print_imsm_dev(super
, dev
, nbuf
+ 5, super
->disks
->index
);
1907 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1908 if (i
== super
->disks
->index
)
1910 print_imsm_disk(__get_imsm_disk(mpb
, i
), i
, reserved
,
1911 super
->sector_size
);
1914 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
1915 if (dl
->index
== -1)
1916 print_imsm_disk(&dl
->disk
, -1, reserved
,
1917 super
->sector_size
);
1919 examine_migr_rec_imsm(super
);
1922 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
1924 /* We just write a generic IMSM ARRAY entry */
1927 struct intel_super
*super
= st
->sb
;
1929 if (!super
->anchor
->num_raid_devs
) {
1930 printf("ARRAY metadata=imsm\n");
1934 getinfo_super_imsm(st
, &info
, NULL
);
1935 fname_from_uuid(st
, &info
, nbuf
, ':');
1936 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
1939 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
1941 /* We just write a generic IMSM ARRAY entry */
1945 struct intel_super
*super
= st
->sb
;
1948 if (!super
->anchor
->num_raid_devs
)
1951 getinfo_super_imsm(st
, &info
, NULL
);
1952 fname_from_uuid(st
, &info
, nbuf
, ':');
1953 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1954 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1956 super
->current_vol
= i
;
1957 getinfo_super_imsm(st
, &info
, NULL
);
1958 fname_from_uuid(st
, &info
, nbuf1
, ':');
1959 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
1960 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
1964 static void export_examine_super_imsm(struct supertype
*st
)
1966 struct intel_super
*super
= st
->sb
;
1967 struct imsm_super
*mpb
= super
->anchor
;
1971 getinfo_super_imsm(st
, &info
, NULL
);
1972 fname_from_uuid(st
, &info
, nbuf
, ':');
1973 printf("MD_METADATA=imsm\n");
1974 printf("MD_LEVEL=container\n");
1975 printf("MD_UUID=%s\n", nbuf
+5);
1976 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
1979 static int copy_metadata_imsm(struct supertype
*st
, int from
, int to
)
1981 /* The second last sector of the device contains
1982 * the "struct imsm_super" metadata.
1983 * This contains mpb_size which is the size in bytes of the
1984 * extended metadata. This is located immediately before
1986 * We want to read all that, plus the last sector which
1987 * may contain a migration record, and write it all
1991 unsigned long long dsize
, offset
;
1993 struct imsm_super
*sb
;
1994 struct intel_super
*super
= st
->sb
;
1995 unsigned int sector_size
= super
->sector_size
;
1996 unsigned int written
= 0;
1998 if (posix_memalign(&buf
, MAX_SECTOR_SIZE
, MAX_SECTOR_SIZE
) != 0)
2001 if (!get_dev_size(from
, NULL
, &dsize
))
2004 if (lseek64(from
, dsize
-(2*sector_size
), 0) < 0)
2006 if ((unsigned int)read(from
, buf
, sector_size
) != sector_size
)
2009 if (strncmp((char*)sb
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0)
2012 sectors
= mpb_sectors(sb
, sector_size
) + 2;
2013 offset
= dsize
- sectors
* sector_size
;
2014 if (lseek64(from
, offset
, 0) < 0 ||
2015 lseek64(to
, offset
, 0) < 0)
2017 while (written
< sectors
* sector_size
) {
2018 int n
= sectors
*sector_size
- written
;
2021 if (read(from
, buf
, n
) != n
)
2023 if (write(to
, buf
, n
) != n
)
2034 static void detail_super_imsm(struct supertype
*st
, char *homehost
)
2039 getinfo_super_imsm(st
, &info
, NULL
);
2040 fname_from_uuid(st
, &info
, nbuf
, ':');
2041 printf("\n UUID : %s\n", nbuf
+ 5);
2044 static void brief_detail_super_imsm(struct supertype
*st
)
2048 getinfo_super_imsm(st
, &info
, NULL
);
2049 fname_from_uuid(st
, &info
, nbuf
, ':');
2050 printf(" UUID=%s", nbuf
+ 5);
2053 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
);
2054 static void fd2devname(int fd
, char *name
);
2056 static int ahci_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
2058 /* dump an unsorted list of devices attached to AHCI Intel storage
2059 * controller, as well as non-connected ports
2061 int hba_len
= strlen(hba_path
) + 1;
2066 unsigned long port_mask
= (1 << port_count
) - 1;
2068 if (port_count
> (int)sizeof(port_mask
) * 8) {
2070 pr_err("port_count %d out of range\n", port_count
);
2074 /* scroll through /sys/dev/block looking for devices attached to
2077 dir
= opendir("/sys/dev/block");
2081 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2092 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
2094 path
= devt_to_devpath(makedev(major
, minor
));
2097 if (!path_attached_to_hba(path
, hba_path
)) {
2103 /* retrieve the scsi device type */
2104 if (asprintf(&device
, "/sys/dev/block/%d:%d/device/xxxxxxx", major
, minor
) < 0) {
2106 pr_err("failed to allocate 'device'\n");
2110 sprintf(device
, "/sys/dev/block/%d:%d/device/type", major
, minor
);
2111 if (load_sys(device
, buf
, sizeof(buf
)) != 0) {
2113 pr_err("failed to read device type for %s\n",
2119 type
= strtoul(buf
, NULL
, 10);
2121 /* if it's not a disk print the vendor and model */
2122 if (!(type
== 0 || type
== 7 || type
== 14)) {
2125 sprintf(device
, "/sys/dev/block/%d:%d/device/vendor", major
, minor
);
2126 if (load_sys(device
, buf
, sizeof(buf
)) == 0) {
2127 strncpy(vendor
, buf
, sizeof(vendor
));
2128 vendor
[sizeof(vendor
) - 1] = '\0';
2129 c
= (char *) &vendor
[sizeof(vendor
) - 1];
2130 while (isspace(*c
) || *c
== '\0')
2134 sprintf(device
, "/sys/dev/block/%d:%d/device/model", major
, minor
);
2135 if (load_sys(device
, buf
, sizeof(buf
)) == 0) {
2136 strncpy(model
, buf
, sizeof(model
));
2137 model
[sizeof(model
) - 1] = '\0';
2138 c
= (char *) &model
[sizeof(model
) - 1];
2139 while (isspace(*c
) || *c
== '\0')
2143 if (vendor
[0] && model
[0])
2144 sprintf(buf
, "%.64s %.64s", vendor
, model
);
2146 switch (type
) { /* numbers from hald/linux/device.c */
2147 case 1: sprintf(buf
, "tape"); break;
2148 case 2: sprintf(buf
, "printer"); break;
2149 case 3: sprintf(buf
, "processor"); break;
2151 case 5: sprintf(buf
, "cdrom"); break;
2152 case 6: sprintf(buf
, "scanner"); break;
2153 case 8: sprintf(buf
, "media_changer"); break;
2154 case 9: sprintf(buf
, "comm"); break;
2155 case 12: sprintf(buf
, "raid"); break;
2156 default: sprintf(buf
, "unknown");
2162 /* chop device path to 'host%d' and calculate the port number */
2163 c
= strchr(&path
[hba_len
], '/');
2166 pr_err("%s - invalid path name\n", path
+ hba_len
);
2171 if ((sscanf(&path
[hba_len
], "ata%d", &port
) == 1) ||
2172 ((sscanf(&path
[hba_len
], "host%d", &port
) == 1)))
2176 *c
= '/'; /* repair the full string */
2177 pr_err("failed to determine port number for %s\n",
2184 /* mark this port as used */
2185 port_mask
&= ~(1 << port
);
2187 /* print out the device information */
2189 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
2193 fd
= dev_open(ent
->d_name
, O_RDONLY
);
2195 printf(" Port%d : - disk info unavailable -\n", port
);
2197 fd2devname(fd
, buf
);
2198 printf(" Port%d : %s", port
, buf
);
2199 if (imsm_read_serial(fd
, NULL
, (__u8
*) buf
) == 0)
2200 printf(" (%.*s)\n", MAX_RAID_SERIAL_LEN
, buf
);
2215 for (i
= 0; i
< port_count
; i
++)
2216 if (port_mask
& (1 << i
))
2217 printf(" Port%d : - no device attached -\n", i
);
2223 static int print_vmd_attached_devs(struct sys_dev
*hba
)
2231 if (hba
->type
!= SYS_DEV_VMD
)
2234 /* scroll through /sys/dev/block looking for devices attached to
2237 dir
= opendir("/sys/bus/pci/drivers/nvme");
2241 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2244 /* is 'ent' a device? check that the 'subsystem' link exists and
2245 * that its target matches 'bus'
2247 sprintf(path
, "/sys/bus/pci/drivers/nvme/%s/subsystem",
2249 n
= readlink(path
, link
, sizeof(link
));
2250 if (n
< 0 || n
>= (int)sizeof(link
))
2253 c
= strrchr(link
, '/');
2256 if (strncmp("pci", c
+1, strlen("pci")) != 0)
2259 sprintf(path
, "/sys/bus/pci/drivers/nvme/%s", ent
->d_name
);
2261 rp
= realpath(path
, NULL
);
2265 if (path_attached_to_hba(rp
, hba
->path
)) {
2266 printf(" NVMe under VMD : %s\n", rp
);
2275 static void print_found_intel_controllers(struct sys_dev
*elem
)
2277 for (; elem
; elem
= elem
->next
) {
2278 pr_err("found Intel(R) ");
2279 if (elem
->type
== SYS_DEV_SATA
)
2280 fprintf(stderr
, "SATA ");
2281 else if (elem
->type
== SYS_DEV_SAS
)
2282 fprintf(stderr
, "SAS ");
2283 else if (elem
->type
== SYS_DEV_NVME
)
2284 fprintf(stderr
, "NVMe ");
2286 if (elem
->type
== SYS_DEV_VMD
)
2287 fprintf(stderr
, "VMD domain");
2289 fprintf(stderr
, "RAID controller");
2292 fprintf(stderr
, " at %s", elem
->pci_id
);
2293 fprintf(stderr
, ".\n");
2298 static int ahci_get_port_count(const char *hba_path
, int *port_count
)
2305 if ((dir
= opendir(hba_path
)) == NULL
)
2308 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2311 if ((sscanf(ent
->d_name
, "ata%d", &host
) != 1) &&
2312 ((sscanf(ent
->d_name
, "host%d", &host
) != 1)))
2314 if (*port_count
== 0)
2316 else if (host
< host_base
)
2319 if (host
+ 1 > *port_count
+ host_base
)
2320 *port_count
= host
+ 1 - host_base
;
2326 static void print_imsm_capability(const struct imsm_orom
*orom
)
2328 printf(" Platform : Intel(R) ");
2329 if (orom
->capabilities
== 0 && orom
->driver_features
== 0)
2330 printf("Matrix Storage Manager\n");
2332 printf("Rapid Storage Technology%s\n",
2333 imsm_orom_is_enterprise(orom
) ? " enterprise" : "");
2334 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
)
2335 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
,
2336 orom
->minor_ver
, orom
->hotfix_ver
, orom
->build
);
2337 printf(" RAID Levels :%s%s%s%s%s\n",
2338 imsm_orom_has_raid0(orom
) ? " raid0" : "",
2339 imsm_orom_has_raid1(orom
) ? " raid1" : "",
2340 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
2341 imsm_orom_has_raid10(orom
) ? " raid10" : "",
2342 imsm_orom_has_raid5(orom
) ? " raid5" : "");
2343 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2344 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
2345 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
2346 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
2347 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
2348 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
2349 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
2350 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
2351 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
2352 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
2353 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
2354 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
2355 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
2356 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
2357 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
2358 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
2359 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
2360 printf(" 2TB volumes :%s supported\n",
2361 (orom
->attr
& IMSM_OROM_ATTR_2TB
)?"":" not");
2362 printf(" 2TB disks :%s supported\n",
2363 (orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
)?"":" not");
2364 printf(" Max Disks : %d\n", orom
->tds
);
2365 printf(" Max Volumes : %d per array, %d per %s\n",
2366 orom
->vpa
, orom
->vphba
,
2367 imsm_orom_is_nvme(orom
) ? "platform" : "controller");
2371 static void print_imsm_capability_export(const struct imsm_orom
*orom
)
2373 printf("MD_FIRMWARE_TYPE=imsm\n");
2374 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
)
2375 printf("IMSM_VERSION=%d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
2376 orom
->hotfix_ver
, orom
->build
);
2377 printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n",
2378 imsm_orom_has_raid0(orom
) ? "raid0 " : "",
2379 imsm_orom_has_raid1(orom
) ? "raid1 " : "",
2380 imsm_orom_has_raid1e(orom
) ? "raid1e " : "",
2381 imsm_orom_has_raid5(orom
) ? "raid10 " : "",
2382 imsm_orom_has_raid10(orom
) ? "raid5 " : "");
2383 printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2384 imsm_orom_has_chunk(orom
, 2) ? "2k " : "",
2385 imsm_orom_has_chunk(orom
, 4) ? "4k " : "",
2386 imsm_orom_has_chunk(orom
, 8) ? "8k " : "",
2387 imsm_orom_has_chunk(orom
, 16) ? "16k " : "",
2388 imsm_orom_has_chunk(orom
, 32) ? "32k " : "",
2389 imsm_orom_has_chunk(orom
, 64) ? "64k " : "",
2390 imsm_orom_has_chunk(orom
, 128) ? "128k " : "",
2391 imsm_orom_has_chunk(orom
, 256) ? "256k " : "",
2392 imsm_orom_has_chunk(orom
, 512) ? "512k " : "",
2393 imsm_orom_has_chunk(orom
, 1024*1) ? "1M " : "",
2394 imsm_orom_has_chunk(orom
, 1024*2) ? "2M " : "",
2395 imsm_orom_has_chunk(orom
, 1024*4) ? "4M " : "",
2396 imsm_orom_has_chunk(orom
, 1024*8) ? "8M " : "",
2397 imsm_orom_has_chunk(orom
, 1024*16) ? "16M " : "",
2398 imsm_orom_has_chunk(orom
, 1024*32) ? "32M " : "",
2399 imsm_orom_has_chunk(orom
, 1024*64) ? "64M " : "");
2400 printf("IMSM_2TB_VOLUMES=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB
) ? "yes" : "no");
2401 printf("IMSM_2TB_DISKS=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) ? "yes" : "no");
2402 printf("IMSM_MAX_DISKS=%d\n",orom
->tds
);
2403 printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom
->vpa
);
2404 printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom
->vphba
);
2407 static int detail_platform_imsm(int verbose
, int enumerate_only
, char *controller_path
)
2409 /* There are two components to imsm platform support, the ahci SATA
2410 * controller and the option-rom. To find the SATA controller we
2411 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
2412 * controller with the Intel vendor id is present. This approach
2413 * allows mdadm to leverage the kernel's ahci detection logic, with the
2414 * caveat that if ahci.ko is not loaded mdadm will not be able to
2415 * detect platform raid capabilities. The option-rom resides in a
2416 * platform "Adapter ROM". We scan for its signature to retrieve the
2417 * platform capabilities. If raid support is disabled in the BIOS the
2418 * option-rom capability structure will not be available.
2420 struct sys_dev
*list
, *hba
;
2425 if (enumerate_only
) {
2426 if (check_env("IMSM_NO_PLATFORM"))
2428 list
= find_intel_devices();
2431 for (hba
= list
; hba
; hba
= hba
->next
) {
2432 if (find_imsm_capability(hba
)) {
2442 list
= find_intel_devices();
2445 pr_err("no active Intel(R) RAID controller found.\n");
2447 } else if (verbose
> 0)
2448 print_found_intel_controllers(list
);
2450 for (hba
= list
; hba
; hba
= hba
->next
) {
2451 if (controller_path
&& (compare_paths(hba
->path
, controller_path
) != 0))
2453 if (!find_imsm_capability(hba
)) {
2455 pr_err("imsm capabilities not found for controller: %s (type %s)\n",
2456 hba
->type
== SYS_DEV_VMD
? vmd_domain_to_controller(hba
, buf
) : hba
->path
,
2457 get_sys_dev_type(hba
->type
));
2463 if (controller_path
&& result
== 1) {
2464 pr_err("no active Intel(R) RAID controller found under %s\n",
2469 const struct orom_entry
*entry
;
2471 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2472 if (entry
->type
== SYS_DEV_VMD
) {
2473 print_imsm_capability(&entry
->orom
);
2474 printf(" 3rd party NVMe :%s supported\n",
2475 imsm_orom_has_tpv_support(&entry
->orom
)?"":" not");
2476 for (hba
= list
; hba
; hba
= hba
->next
) {
2477 if (hba
->type
== SYS_DEV_VMD
) {
2479 printf(" I/O Controller : %s (%s)\n",
2480 vmd_domain_to_controller(hba
, buf
), get_sys_dev_type(hba
->type
));
2481 if (print_vmd_attached_devs(hba
)) {
2483 pr_err("failed to get devices attached to VMD domain.\n");
2492 print_imsm_capability(&entry
->orom
);
2493 if (entry
->type
== SYS_DEV_NVME
) {
2494 for (hba
= list
; hba
; hba
= hba
->next
) {
2495 if (hba
->type
== SYS_DEV_NVME
)
2496 printf(" NVMe Device : %s\n", hba
->path
);
2502 struct devid_list
*devid
;
2503 for (devid
= entry
->devid_list
; devid
; devid
= devid
->next
) {
2504 hba
= device_by_id(devid
->devid
);
2508 printf(" I/O Controller : %s (%s)\n",
2509 hba
->path
, get_sys_dev_type(hba
->type
));
2510 if (hba
->type
== SYS_DEV_SATA
) {
2511 host_base
= ahci_get_port_count(hba
->path
, &port_count
);
2512 if (ahci_enumerate_ports(hba
->path
, port_count
, host_base
, verbose
)) {
2514 pr_err("failed to enumerate ports on SATA controller at %s.\n", hba
->pci_id
);
2525 static int export_detail_platform_imsm(int verbose
, char *controller_path
)
2527 struct sys_dev
*list
, *hba
;
2530 list
= find_intel_devices();
2533 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n");
2538 for (hba
= list
; hba
; hba
= hba
->next
) {
2539 if (controller_path
&& (compare_paths(hba
->path
,controller_path
) != 0))
2541 if (!find_imsm_capability(hba
) && verbose
> 0) {
2543 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n",
2544 hba
->type
== SYS_DEV_VMD
? vmd_domain_to_controller(hba
, buf
) : hba
->path
);
2550 const struct orom_entry
*entry
;
2552 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2553 if (entry
->type
== SYS_DEV_VMD
) {
2554 for (hba
= list
; hba
; hba
= hba
->next
)
2555 print_imsm_capability_export(&entry
->orom
);
2558 print_imsm_capability_export(&entry
->orom
);
2564 static int match_home_imsm(struct supertype
*st
, char *homehost
)
2566 /* the imsm metadata format does not specify any host
2567 * identification information. We return -1 since we can never
2568 * confirm nor deny whether a given array is "meant" for this
2569 * host. We rely on compare_super and the 'family_num' fields to
2570 * exclude member disks that do not belong, and we rely on
2571 * mdadm.conf to specify the arrays that should be assembled.
2572 * Auto-assembly may still pick up "foreign" arrays.
2578 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
2580 /* The uuid returned here is used for:
2581 * uuid to put into bitmap file (Create, Grow)
2582 * uuid for backup header when saving critical section (Grow)
2583 * comparing uuids when re-adding a device into an array
2584 * In these cases the uuid required is that of the data-array,
2585 * not the device-set.
2586 * uuid to recognise same set when adding a missing device back
2587 * to an array. This is a uuid for the device-set.
2589 * For each of these we can make do with a truncated
2590 * or hashed uuid rather than the original, as long as
2592 * In each case the uuid required is that of the data-array,
2593 * not the device-set.
2595 /* imsm does not track uuid's so we synthesis one using sha1 on
2596 * - The signature (Which is constant for all imsm array, but no matter)
2597 * - the orig_family_num of the container
2598 * - the index number of the volume
2599 * - the 'serial' number of the volume.
2600 * Hopefully these are all constant.
2602 struct intel_super
*super
= st
->sb
;
2605 struct sha1_ctx ctx
;
2606 struct imsm_dev
*dev
= NULL
;
2609 /* some mdadm versions failed to set ->orig_family_num, in which
2610 * case fall back to ->family_num. orig_family_num will be
2611 * fixed up with the first metadata update.
2613 family_num
= super
->anchor
->orig_family_num
;
2614 if (family_num
== 0)
2615 family_num
= super
->anchor
->family_num
;
2616 sha1_init_ctx(&ctx
);
2617 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
2618 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
2619 if (super
->current_vol
>= 0)
2620 dev
= get_imsm_dev(super
, super
->current_vol
);
2622 __u32 vol
= super
->current_vol
;
2623 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
2624 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
2626 sha1_finish_ctx(&ctx
, buf
);
2627 memcpy(uuid
, buf
, 4*4);
2632 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
2634 __u8
*v
= get_imsm_version(mpb
);
2635 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
2636 char major
[] = { 0, 0, 0 };
2637 char minor
[] = { 0 ,0, 0 };
2638 char patch
[] = { 0, 0, 0 };
2639 char *ver_parse
[] = { major
, minor
, patch
};
2643 while (*v
!= '\0' && v
< end
) {
2644 if (*v
!= '.' && j
< 2)
2645 ver_parse
[i
][j
++] = *v
;
2653 *m
= strtol(minor
, NULL
, 0);
2654 *p
= strtol(patch
, NULL
, 0);
2658 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
2660 /* migr_strip_size when repairing or initializing parity */
2661 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2662 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2664 switch (get_imsm_raid_level(map
)) {
2669 return 128*1024 >> 9;
2673 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
2675 /* migr_strip_size when rebuilding a degraded disk, no idea why
2676 * this is different than migr_strip_size_resync(), but it's good
2679 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2680 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2682 switch (get_imsm_raid_level(map
)) {
2685 if (map
->num_members
% map
->num_domains
== 0)
2686 return 128*1024 >> 9;
2690 return max((__u32
) 64*1024 >> 9, chunk
);
2692 return 128*1024 >> 9;
2696 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
2698 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2699 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2700 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
2701 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
2703 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
2706 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
2708 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2709 int level
= get_imsm_raid_level(lo
);
2711 if (level
== 1 || level
== 10) {
2712 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2714 return hi
->num_domains
;
2716 return num_stripes_per_unit_resync(dev
);
2719 static __u8
imsm_num_data_members(struct imsm_dev
*dev
, int second_map
)
2721 /* named 'imsm_' because raid0, raid1 and raid10
2722 * counter-intuitively have the same number of data disks
2724 struct imsm_map
*map
= get_imsm_map(dev
, second_map
);
2726 switch (get_imsm_raid_level(map
)) {
2728 return map
->num_members
;
2732 return map
->num_members
/2;
2734 return map
->num_members
- 1;
2736 dprintf("unsupported raid level\n");
2741 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
2743 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2744 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2746 switch(get_imsm_raid_level(map
)) {
2749 return chunk
* map
->num_domains
;
2751 return chunk
* map
->num_members
;
2757 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
2759 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2760 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2761 __u32 strip
= block
/ chunk
;
2763 switch (get_imsm_raid_level(map
)) {
2766 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
2767 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
2769 return vol_stripe
* chunk
+ block
% chunk
;
2771 __u32 stripe
= strip
/ (map
->num_members
- 1);
2773 return stripe
* chunk
+ block
% chunk
;
2780 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
2781 struct imsm_dev
*dev
)
2783 /* calculate the conversion factor between per member 'blocks'
2784 * (md/{resync,rebuild}_start) and imsm migration units, return
2785 * 0 for the 'not migrating' and 'unsupported migration' cases
2787 if (!dev
->vol
.migr_state
)
2790 switch (migr_type(dev
)) {
2791 case MIGR_GEN_MIGR
: {
2792 struct migr_record
*migr_rec
= super
->migr_rec
;
2793 return __le32_to_cpu(migr_rec
->blocks_per_unit
);
2798 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2799 __u32 stripes_per_unit
;
2800 __u32 blocks_per_unit
;
2809 /* yes, this is really the translation of migr_units to
2810 * per-member blocks in the 'resync' case
2812 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
2813 migr_chunk
= migr_strip_blocks_resync(dev
);
2814 disks
= imsm_num_data_members(dev
, MAP_0
);
2815 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
2816 stripe
= __le16_to_cpu(map
->blocks_per_strip
) * disks
;
2817 segment
= blocks_per_unit
/ stripe
;
2818 block_rel
= blocks_per_unit
- segment
* stripe
;
2819 parity_depth
= parity_segment_depth(dev
);
2820 block_map
= map_migr_block(dev
, block_rel
);
2821 return block_map
+ parity_depth
* segment
;
2823 case MIGR_REBUILD
: {
2824 __u32 stripes_per_unit
;
2827 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
2828 migr_chunk
= migr_strip_blocks_rebuild(dev
);
2829 return migr_chunk
* stripes_per_unit
;
2831 case MIGR_STATE_CHANGE
:
2837 static int imsm_level_to_layout(int level
)
2845 return ALGORITHM_LEFT_ASYMMETRIC
;
2852 /*******************************************************************************
2853 * Function: read_imsm_migr_rec
2854 * Description: Function reads imsm migration record from last sector of disk
2856 * fd : disk descriptor
2857 * super : metadata info
2861 ******************************************************************************/
2862 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
)
2865 unsigned int sector_size
= super
->sector_size
;
2866 unsigned long long dsize
;
2868 get_dev_size(fd
, NULL
, &dsize
);
2869 if (lseek64(fd
, dsize
- (sector_size
*MIGR_REC_SECTOR_POSITION
),
2871 pr_err("Cannot seek to anchor block: %s\n",
2875 if ((unsigned int)read(fd
, super
->migr_rec_buf
,
2876 MIGR_REC_BUF_SECTORS
*sector_size
) !=
2877 MIGR_REC_BUF_SECTORS
*sector_size
) {
2878 pr_err("Cannot read migr record block: %s\n",
2883 if (sector_size
== 4096)
2884 convert_from_4k_imsm_migr_rec(super
);
2890 static struct imsm_dev
*imsm_get_device_during_migration(
2891 struct intel_super
*super
)
2894 struct intel_dev
*dv
;
2896 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
2897 if (is_gen_migration(dv
->dev
))
2903 /*******************************************************************************
2904 * Function: load_imsm_migr_rec
2905 * Description: Function reads imsm migration record (it is stored at the last
2908 * super : imsm internal array info
2909 * info : general array info
2913 * -2 : no migration in progress
2914 ******************************************************************************/
2915 static int load_imsm_migr_rec(struct intel_super
*super
, struct mdinfo
*info
)
2922 struct imsm_dev
*dev
;
2923 struct imsm_map
*map
;
2926 /* find map under migration */
2927 dev
= imsm_get_device_during_migration(super
);
2928 /* nothing to load,no migration in progress?
2934 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
2935 /* read only from one of the first two slots */
2936 if ((sd
->disk
.raid_disk
< 0) ||
2937 (sd
->disk
.raid_disk
> 1))
2940 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
2941 fd
= dev_open(nm
, O_RDONLY
);
2947 map
= get_imsm_map(dev
, MAP_0
);
2948 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
2949 /* skip spare and failed disks
2953 /* read only from one of the first two slots */
2955 slot
= get_imsm_disk_slot(map
, dl
->index
);
2956 if (map
== NULL
|| slot
> 1 || slot
< 0)
2958 sprintf(nm
, "%d:%d", dl
->major
, dl
->minor
);
2959 fd
= dev_open(nm
, O_RDONLY
);
2966 retval
= read_imsm_migr_rec(fd
, super
);
2974 /*******************************************************************************
2975 * function: imsm_create_metadata_checkpoint_update
2976 * Description: It creates update for checkpoint change.
2978 * super : imsm internal array info
2979 * u : pointer to prepared update
2982 * If length is equal to 0, input pointer u contains no update
2983 ******************************************************************************/
2984 static int imsm_create_metadata_checkpoint_update(
2985 struct intel_super
*super
,
2986 struct imsm_update_general_migration_checkpoint
**u
)
2989 int update_memory_size
= 0;
2991 dprintf("(enter)\n");
2997 /* size of all update data without anchor */
2998 update_memory_size
=
2999 sizeof(struct imsm_update_general_migration_checkpoint
);
3001 *u
= xcalloc(1, update_memory_size
);
3003 dprintf("error: cannot get memory\n");
3006 (*u
)->type
= update_general_migration_checkpoint
;
3007 (*u
)->curr_migr_unit
= __le32_to_cpu(super
->migr_rec
->curr_migr_unit
);
3008 dprintf("prepared for %u\n", (*u
)->curr_migr_unit
);
3010 return update_memory_size
;
3013 static void imsm_update_metadata_locally(struct supertype
*st
,
3014 void *buf
, int len
);
3016 /*******************************************************************************
3017 * Function: write_imsm_migr_rec
3018 * Description: Function writes imsm migration record
3019 * (at the last sector of disk)
3021 * super : imsm internal array info
3025 ******************************************************************************/
3026 static int write_imsm_migr_rec(struct supertype
*st
)
3028 struct intel_super
*super
= st
->sb
;
3029 unsigned int sector_size
= super
->sector_size
;
3030 unsigned long long dsize
;
3036 struct imsm_update_general_migration_checkpoint
*u
;
3037 struct imsm_dev
*dev
;
3038 struct imsm_map
*map
;
3040 /* find map under migration */
3041 dev
= imsm_get_device_during_migration(super
);
3042 /* if no migration, write buffer anyway to clear migr_record
3043 * on disk based on first available device
3046 dev
= get_imsm_dev(super
, super
->current_vol
< 0 ? 0 :
3047 super
->current_vol
);
3049 map
= get_imsm_map(dev
, MAP_0
);
3051 if (sector_size
== 4096)
3052 convert_to_4k_imsm_migr_rec(super
);
3053 for (sd
= super
->disks
; sd
; sd
= sd
->next
) {
3056 /* skip failed and spare devices */
3059 /* write to 2 first slots only */
3061 slot
= get_imsm_disk_slot(map
, sd
->index
);
3062 if (map
== NULL
|| slot
> 1 || slot
< 0)
3065 sprintf(nm
, "%d:%d", sd
->major
, sd
->minor
);
3066 fd
= dev_open(nm
, O_RDWR
);
3069 get_dev_size(fd
, NULL
, &dsize
);
3070 if (lseek64(fd
, dsize
- (MIGR_REC_SECTOR_POSITION
*sector_size
),
3072 pr_err("Cannot seek to anchor block: %s\n",
3076 if ((unsigned int)write(fd
, super
->migr_rec_buf
,
3077 MIGR_REC_BUF_SECTORS
*sector_size
) !=
3078 MIGR_REC_BUF_SECTORS
*sector_size
) {
3079 pr_err("Cannot write migr record block: %s\n",
3086 if (sector_size
== 4096)
3087 convert_from_4k_imsm_migr_rec(super
);
3088 /* update checkpoint information in metadata */
3089 len
= imsm_create_metadata_checkpoint_update(super
, &u
);
3091 dprintf("imsm: Cannot prepare update\n");
3094 /* update metadata locally */
3095 imsm_update_metadata_locally(st
, u
, len
);
3096 /* and possibly remotely */
3097 if (st
->update_tail
) {
3098 append_metadata_update(st
, u
, len
);
3099 /* during reshape we do all work inside metadata handler
3100 * manage_reshape(), so metadata update has to be triggered
3103 flush_metadata_updates(st
);
3104 st
->update_tail
= &st
->updates
;
3115 /* spare/missing disks activations are not allowe when
3116 * array/container performs reshape operation, because
3117 * all arrays in container works on the same disks set
3119 int imsm_reshape_blocks_arrays_changes(struct intel_super
*super
)
3122 struct intel_dev
*i_dev
;
3123 struct imsm_dev
*dev
;
3125 /* check whole container
3127 for (i_dev
= super
->devlist
; i_dev
; i_dev
= i_dev
->next
) {
3129 if (is_gen_migration(dev
)) {
3130 /* No repair during any migration in container
3138 static unsigned long long imsm_component_size_aligment_check(int level
,
3140 unsigned int sector_size
,
3141 unsigned long long component_size
)
3143 unsigned int component_size_alligment
;
3145 /* check component size aligment
3147 component_size_alligment
= component_size
% (chunk_size
/sector_size
);
3149 dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alligment = %u\n",
3150 level
, chunk_size
, component_size
,
3151 component_size_alligment
);
3153 if (component_size_alligment
&& (level
!= 1) && (level
!= UnSet
)) {
3154 dprintf("imsm: reported component size alligned from %llu ",
3156 component_size
-= component_size_alligment
;
3157 dprintf_cont("to %llu (%i).\n",
3158 component_size
, component_size_alligment
);
3161 return component_size
;
3164 static unsigned long long get_ppl_sector(struct intel_super
*super
, int dev_idx
)
3166 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
3167 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3169 return pba_of_lba0(map
) +
3170 (num_data_stripes(map
) * map
->blocks_per_strip
);
3173 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
3175 struct intel_super
*super
= st
->sb
;
3176 struct migr_record
*migr_rec
= super
->migr_rec
;
3177 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
3178 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3179 struct imsm_map
*prev_map
= get_imsm_map(dev
, MAP_1
);
3180 struct imsm_map
*map_to_analyse
= map
;
3182 int map_disks
= info
->array
.raid_disks
;
3184 memset(info
, 0, sizeof(*info
));
3186 map_to_analyse
= prev_map
;
3188 dl
= super
->current_disk
;
3190 info
->container_member
= super
->current_vol
;
3191 info
->array
.raid_disks
= map
->num_members
;
3192 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
3193 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
3194 info
->array
.md_minor
= -1;
3195 info
->array
.ctime
= 0;
3196 info
->array
.utime
= 0;
3197 info
->array
.chunk_size
=
3198 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
3199 info
->array
.state
= !(dev
->vol
.dirty
& RAIDVOL_DIRTY
);
3200 info
->custom_array_size
= __le32_to_cpu(dev
->size_high
);
3201 info
->custom_array_size
<<= 32;
3202 info
->custom_array_size
|= __le32_to_cpu(dev
->size_low
);
3203 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
3205 if (is_gen_migration(dev
)) {
3206 info
->reshape_active
= 1;
3207 info
->new_level
= get_imsm_raid_level(map
);
3208 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
3209 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
3210 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
3211 if (info
->delta_disks
) {
3212 /* this needs to be applied to every array
3215 info
->reshape_active
= CONTAINER_RESHAPE
;
3217 /* We shape information that we give to md might have to be
3218 * modify to cope with md's requirement for reshaping arrays.
3219 * For example, when reshaping a RAID0, md requires it to be
3220 * presented as a degraded RAID4.
3221 * Also if a RAID0 is migrating to a RAID5 we need to specify
3222 * the array as already being RAID5, but the 'before' layout
3223 * is a RAID4-like layout.
3225 switch (info
->array
.level
) {
3227 switch(info
->new_level
) {
3229 /* conversion is happening as RAID4 */
3230 info
->array
.level
= 4;
3231 info
->array
.raid_disks
+= 1;
3234 /* conversion is happening as RAID5 */
3235 info
->array
.level
= 5;
3236 info
->array
.layout
= ALGORITHM_PARITY_N
;
3237 info
->delta_disks
-= 1;
3240 /* FIXME error message */
3241 info
->array
.level
= UnSet
;
3247 info
->new_level
= UnSet
;
3248 info
->new_layout
= UnSet
;
3249 info
->new_chunk
= info
->array
.chunk_size
;
3250 info
->delta_disks
= 0;
3254 info
->disk
.major
= dl
->major
;
3255 info
->disk
.minor
= dl
->minor
;
3256 info
->disk
.number
= dl
->index
;
3257 info
->disk
.raid_disk
= get_imsm_disk_slot(map_to_analyse
,
3261 info
->data_offset
= pba_of_lba0(map_to_analyse
);
3263 if (info
->array
.level
== 5) {
3264 info
->component_size
= num_data_stripes(map_to_analyse
) *
3265 map_to_analyse
->blocks_per_strip
;
3267 info
->component_size
= blocks_per_member(map_to_analyse
);
3270 info
->component_size
= imsm_component_size_aligment_check(
3272 info
->array
.chunk_size
,
3274 info
->component_size
);
3275 info
->bb
.supported
= 1;
3277 memset(info
->uuid
, 0, sizeof(info
->uuid
));
3278 info
->recovery_start
= MaxSector
;
3280 if (info
->array
.level
== 5 && dev
->rwh_policy
== RWH_DISTRIBUTED
) {
3281 info
->consistency_policy
= CONSISTENCY_POLICY_PPL
;
3282 info
->ppl_sector
= get_ppl_sector(super
, super
->current_vol
);
3283 info
->ppl_size
= (PPL_HEADER_SIZE
+ PPL_ENTRY_SPACE
) >> 9;
3284 } else if (info
->array
.level
<= 0) {
3285 info
->consistency_policy
= CONSISTENCY_POLICY_NONE
;
3287 info
->consistency_policy
= CONSISTENCY_POLICY_RESYNC
;
3290 info
->reshape_progress
= 0;
3291 info
->resync_start
= MaxSector
;
3292 if ((map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
3293 !(info
->array
.state
& 1)) &&
3294 imsm_reshape_blocks_arrays_changes(super
) == 0) {
3295 info
->resync_start
= 0;
3297 if (dev
->vol
.migr_state
) {
3298 switch (migr_type(dev
)) {
3301 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
3303 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
3305 info
->resync_start
= blocks_per_unit
* units
;
3308 case MIGR_GEN_MIGR
: {
3309 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
3311 __u64 units
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
3312 unsigned long long array_blocks
;
3315 if (__le32_to_cpu(migr_rec
->ascending_migr
) &&
3317 (__le32_to_cpu(migr_rec
->num_migr_units
)-1)) &&
3318 (super
->migr_rec
->rec_status
==
3319 __cpu_to_le32(UNIT_SRC_IN_CP_AREA
)))
3322 info
->reshape_progress
= blocks_per_unit
* units
;
3324 dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n",
3325 (unsigned long long)units
,
3326 (unsigned long long)blocks_per_unit
,
3327 info
->reshape_progress
);
3329 used_disks
= imsm_num_data_members(dev
, MAP_1
);
3330 if (used_disks
> 0) {
3331 array_blocks
= blocks_per_member(map
) *
3333 info
->custom_array_size
=
3334 round_size_to_mb(array_blocks
,
3340 /* we could emulate the checkpointing of
3341 * 'sync_action=check' migrations, but for now
3342 * we just immediately complete them
3345 /* this is handled by container_content_imsm() */
3346 case MIGR_STATE_CHANGE
:
3347 /* FIXME handle other migrations */
3349 /* we are not dirty, so... */
3350 info
->resync_start
= MaxSector
;
3354 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
3355 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
3357 info
->array
.major_version
= -1;
3358 info
->array
.minor_version
= -2;
3359 sprintf(info
->text_version
, "/%s/%d", st
->container_devnm
, info
->container_member
);
3360 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
3361 uuid_from_super_imsm(st
, info
->uuid
);
3365 for (i
=0; i
<map_disks
; i
++) {
3367 if (i
< info
->array
.raid_disks
) {
3368 struct imsm_disk
*dsk
;
3369 j
= get_imsm_disk_idx(dev
, i
, MAP_X
);
3370 dsk
= get_imsm_disk(super
, j
);
3371 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
3378 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
3379 int failed
, int look_in_map
);
3381 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
3384 static void manage_second_map(struct intel_super
*super
, struct imsm_dev
*dev
)
3386 if (is_gen_migration(dev
)) {
3389 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
3391 failed
= imsm_count_failed(super
, dev
, MAP_1
);
3392 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_1
);
3393 if (map2
->map_state
!= map_state
) {
3394 map2
->map_state
= map_state
;
3395 super
->updates_pending
++;
3400 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
3404 for (d
= super
->missing
; d
; d
= d
->next
)
3405 if (d
->index
== index
)
3410 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
3412 struct intel_super
*super
= st
->sb
;
3413 struct imsm_disk
*disk
;
3414 int map_disks
= info
->array
.raid_disks
;
3415 int max_enough
= -1;
3417 struct imsm_super
*mpb
;
3419 if (super
->current_vol
>= 0) {
3420 getinfo_super_imsm_volume(st
, info
, map
);
3423 memset(info
, 0, sizeof(*info
));
3425 /* Set raid_disks to zero so that Assemble will always pull in valid
3428 info
->array
.raid_disks
= 0;
3429 info
->array
.level
= LEVEL_CONTAINER
;
3430 info
->array
.layout
= 0;
3431 info
->array
.md_minor
= -1;
3432 info
->array
.ctime
= 0; /* N/A for imsm */
3433 info
->array
.utime
= 0;
3434 info
->array
.chunk_size
= 0;
3436 info
->disk
.major
= 0;
3437 info
->disk
.minor
= 0;
3438 info
->disk
.raid_disk
= -1;
3439 info
->reshape_active
= 0;
3440 info
->array
.major_version
= -1;
3441 info
->array
.minor_version
= -2;
3442 strcpy(info
->text_version
, "imsm");
3443 info
->safe_mode_delay
= 0;
3444 info
->disk
.number
= -1;
3445 info
->disk
.state
= 0;
3447 info
->recovery_start
= MaxSector
;
3448 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
3449 info
->bb
.supported
= 1;
3451 /* do we have the all the insync disks that we expect? */
3452 mpb
= super
->anchor
;
3453 info
->events
= __le32_to_cpu(mpb
->generation_num
);
3455 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3456 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3457 int failed
, enough
, j
, missing
= 0;
3458 struct imsm_map
*map
;
3461 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3462 state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3463 map
= get_imsm_map(dev
, MAP_0
);
3465 /* any newly missing disks?
3466 * (catches single-degraded vs double-degraded)
3468 for (j
= 0; j
< map
->num_members
; j
++) {
3469 __u32 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
3470 __u32 idx
= ord_to_idx(ord
);
3472 if (!(ord
& IMSM_ORD_REBUILD
) &&
3473 get_imsm_missing(super
, idx
)) {
3479 if (state
== IMSM_T_STATE_FAILED
)
3481 else if (state
== IMSM_T_STATE_DEGRADED
&&
3482 (state
!= map
->map_state
|| missing
))
3484 else /* we're normal, or already degraded */
3486 if (is_gen_migration(dev
) && missing
) {
3487 /* during general migration we need all disks
3488 * that process is running on.
3489 * No new missing disk is allowed.
3493 /* no more checks necessary
3497 /* in the missing/failed disk case check to see
3498 * if at least one array is runnable
3500 max_enough
= max(max_enough
, enough
);
3502 dprintf("enough: %d\n", max_enough
);
3503 info
->container_enough
= max_enough
;
3506 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
3508 disk
= &super
->disks
->disk
;
3509 info
->data_offset
= total_blocks(&super
->disks
->disk
) - reserved
;
3510 info
->component_size
= reserved
;
3511 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
3512 /* we don't change info->disk.raid_disk here because
3513 * this state will be finalized in mdmon after we have
3514 * found the 'most fresh' version of the metadata
3516 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3517 info
->disk
.state
|= (is_spare(disk
) || is_journal(disk
)) ?
3518 0 : (1 << MD_DISK_SYNC
);
3521 /* only call uuid_from_super_imsm when this disk is part of a populated container,
3522 * ->compare_super may have updated the 'num_raid_devs' field for spares
3524 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
3525 uuid_from_super_imsm(st
, info
->uuid
);
3527 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
3529 /* I don't know how to compute 'map' on imsm, so use safe default */
3532 for (i
= 0; i
< map_disks
; i
++)
3538 /* allocates memory and fills disk in mdinfo structure
3539 * for each disk in array */
3540 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
3542 struct mdinfo
*mddev
;
3543 struct intel_super
*super
= st
->sb
;
3544 struct imsm_disk
*disk
;
3547 if (!super
|| !super
->disks
)
3550 mddev
= xcalloc(1, sizeof(*mddev
));
3554 tmp
= xcalloc(1, sizeof(*tmp
));
3556 tmp
->next
= mddev
->devs
;
3558 tmp
->disk
.number
= count
++;
3559 tmp
->disk
.major
= dl
->major
;
3560 tmp
->disk
.minor
= dl
->minor
;
3561 tmp
->disk
.state
= is_configured(disk
) ?
3562 (1 << MD_DISK_ACTIVE
) : 0;
3563 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3564 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
3565 tmp
->disk
.raid_disk
= -1;
3571 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
3572 char *update
, char *devname
, int verbose
,
3573 int uuid_set
, char *homehost
)
3575 /* For 'assemble' and 'force' we need to return non-zero if any
3576 * change was made. For others, the return value is ignored.
3577 * Update options are:
3578 * force-one : This device looks a bit old but needs to be included,
3579 * update age info appropriately.
3580 * assemble: clear any 'faulty' flag to allow this device to
3582 * force-array: Array is degraded but being forced, mark it clean
3583 * if that will be needed to assemble it.
3585 * newdev: not used ????
3586 * grow: Array has gained a new device - this is currently for
3588 * resync: mark as dirty so a resync will happen.
3589 * name: update the name - preserving the homehost
3590 * uuid: Change the uuid of the array to match watch is given
3592 * Following are not relevant for this imsm:
3593 * sparc2.2 : update from old dodgey metadata
3594 * super-minor: change the preferred_minor number
3595 * summaries: update redundant counters.
3596 * homehost: update the recorded homehost
3597 * _reshape_progress: record new reshape_progress position.
3600 struct intel_super
*super
= st
->sb
;
3601 struct imsm_super
*mpb
;
3603 /* we can only update container info */
3604 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
3607 mpb
= super
->anchor
;
3609 if (strcmp(update
, "uuid") == 0) {
3610 /* We take this to mean that the family_num should be updated.
3611 * However that is much smaller than the uuid so we cannot really
3612 * allow an explicit uuid to be given. And it is hard to reliably
3614 * So if !uuid_set we know the current uuid is random and just used
3615 * the first 'int' and copy it to the other 3 positions.
3616 * Otherwise we require the 4 'int's to be the same as would be the
3617 * case if we are using a random uuid. So an explicit uuid will be
3618 * accepted as long as all for ints are the same... which shouldn't hurt
3621 info
->uuid
[1] = info
->uuid
[2] = info
->uuid
[3] = info
->uuid
[0];
3624 if (info
->uuid
[0] != info
->uuid
[1] ||
3625 info
->uuid
[1] != info
->uuid
[2] ||
3626 info
->uuid
[2] != info
->uuid
[3])
3632 mpb
->orig_family_num
= info
->uuid
[0];
3633 } else if (strcmp(update
, "assemble") == 0)
3638 /* successful update? recompute checksum */
3640 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
3645 static size_t disks_to_mpb_size(int disks
)
3649 size
= sizeof(struct imsm_super
);
3650 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
3651 size
+= 2 * sizeof(struct imsm_dev
);
3652 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
3653 size
+= (4 - 2) * sizeof(struct imsm_map
);
3654 /* 4 possible disk_ord_tbl's */
3655 size
+= 4 * (disks
- 1) * sizeof(__u32
);
3656 /* maximum bbm log */
3657 size
+= sizeof(struct bbm_log
);
3662 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
,
3663 unsigned long long data_offset
)
3665 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
3668 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
3671 static void free_devlist(struct intel_super
*super
)
3673 struct intel_dev
*dv
;
3675 while (super
->devlist
) {
3676 dv
= super
->devlist
->next
;
3677 free(super
->devlist
->dev
);
3678 free(super
->devlist
);
3679 super
->devlist
= dv
;
3683 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
3685 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
3688 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
3692 * 0 same, or first was empty, and second was copied
3693 * 1 second had wrong number
3695 * 3 wrong other info
3697 struct intel_super
*first
= st
->sb
;
3698 struct intel_super
*sec
= tst
->sb
;
3705 /* in platform dependent environment test if the disks
3706 * use the same Intel hba
3707 * If not on Intel hba at all, allow anything.
3709 if (!check_env("IMSM_NO_PLATFORM") && first
->hba
&& sec
->hba
) {
3710 if (first
->hba
->type
!= sec
->hba
->type
) {
3712 "HBAs of devices do not match %s != %s\n",
3713 get_sys_dev_type(first
->hba
->type
),
3714 get_sys_dev_type(sec
->hba
->type
));
3717 if (first
->orom
!= sec
->orom
) {
3719 "HBAs of devices do not match %s != %s\n",
3720 first
->hba
->pci_id
, sec
->hba
->pci_id
);
3725 /* if an anchor does not have num_raid_devs set then it is a free
3728 if (first
->anchor
->num_raid_devs
> 0 &&
3729 sec
->anchor
->num_raid_devs
> 0) {
3730 /* Determine if these disks might ever have been
3731 * related. Further disambiguation can only take place
3732 * in load_super_imsm_all
3734 __u32 first_family
= first
->anchor
->orig_family_num
;
3735 __u32 sec_family
= sec
->anchor
->orig_family_num
;
3737 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
3738 MAX_SIGNATURE_LENGTH
) != 0)
3741 if (first_family
== 0)
3742 first_family
= first
->anchor
->family_num
;
3743 if (sec_family
== 0)
3744 sec_family
= sec
->anchor
->family_num
;
3746 if (first_family
!= sec_family
)
3751 /* if 'first' is a spare promote it to a populated mpb with sec's
3754 if (first
->anchor
->num_raid_devs
== 0 &&
3755 sec
->anchor
->num_raid_devs
> 0) {
3757 struct intel_dev
*dv
;
3758 struct imsm_dev
*dev
;
3760 /* we need to copy raid device info from sec if an allocation
3761 * fails here we don't associate the spare
3763 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
3764 dv
= xmalloc(sizeof(*dv
));
3765 dev
= xmalloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
3768 dv
->next
= first
->devlist
;
3769 first
->devlist
= dv
;
3771 if (i
< sec
->anchor
->num_raid_devs
) {
3772 /* allocation failure */
3773 free_devlist(first
);
3774 pr_err("imsm: failed to associate spare\n");
3777 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
3778 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
3779 first
->anchor
->family_num
= sec
->anchor
->family_num
;
3780 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
3781 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
3782 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
3788 static void fd2devname(int fd
, char *name
)
3792 char dname
[PATH_MAX
];
3797 if (fstat(fd
, &st
) != 0)
3799 sprintf(path
, "/sys/dev/block/%d:%d",
3800 major(st
.st_rdev
), minor(st
.st_rdev
));
3802 rv
= readlink(path
, dname
, sizeof(dname
)-1);
3807 nm
= strrchr(dname
, '/');
3810 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
3814 static int nvme_get_serial(int fd
, void *buf
, size_t buf_len
)
3817 char *name
= fd2kname(fd
);
3822 if (strncmp(name
, "nvme", 4) != 0)
3825 snprintf(path
, sizeof(path
) - 1, "/sys/block/%s/device/serial", name
);
3827 return load_sys(path
, buf
, buf_len
);
3830 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
3832 static int imsm_read_serial(int fd
, char *devname
,
3833 __u8 serial
[MAX_RAID_SERIAL_LEN
])
3842 memset(buf
, 0, sizeof(buf
));
3844 rv
= nvme_get_serial(fd
, buf
, sizeof(buf
));
3847 rv
= scsi_get_serial(fd
, buf
, sizeof(buf
));
3849 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
3850 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3851 fd2devname(fd
, (char *) serial
);
3857 pr_err("Failed to retrieve serial for %s\n",
3862 /* trim all whitespace and non-printable characters and convert
3865 for (i
= 0, dest
= buf
; i
< sizeof(buf
) && buf
[i
]; i
++) {
3868 /* ':' is reserved for use in placeholder serial
3869 * numbers for missing disks
3880 /* truncate leading characters */
3881 if (len
> MAX_RAID_SERIAL_LEN
) {
3882 dest
+= len
- MAX_RAID_SERIAL_LEN
;
3883 len
= MAX_RAID_SERIAL_LEN
;
3886 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3887 memcpy(serial
, dest
, len
);
3892 static int serialcmp(__u8
*s1
, __u8
*s2
)
3894 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
3897 static void serialcpy(__u8
*dest
, __u8
*src
)
3899 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
3902 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
3906 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3907 if (serialcmp(dl
->serial
, serial
) == 0)
3913 static struct imsm_disk
*
3914 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
3918 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3919 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3921 if (serialcmp(disk
->serial
, serial
) == 0) {
3932 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3934 struct imsm_disk
*disk
;
3939 __u8 serial
[MAX_RAID_SERIAL_LEN
];
3941 rv
= imsm_read_serial(fd
, devname
, serial
);
3946 dl
= xcalloc(1, sizeof(*dl
));
3949 dl
->major
= major(stb
.st_rdev
);
3950 dl
->minor
= minor(stb
.st_rdev
);
3951 dl
->next
= super
->disks
;
3952 dl
->fd
= keep_fd
? fd
: -1;
3953 assert(super
->disks
== NULL
);
3955 serialcpy(dl
->serial
, serial
);
3958 fd2devname(fd
, name
);
3960 dl
->devname
= xstrdup(devname
);
3962 dl
->devname
= xstrdup(name
);
3964 /* look up this disk's index in the current anchor */
3965 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
3968 /* only set index on disks that are a member of a
3969 * populated contianer, i.e. one with raid_devs
3971 if (is_failed(&dl
->disk
))
3973 else if (is_spare(&dl
->disk
) || is_journal(&dl
->disk
))
3980 /* When migrating map0 contains the 'destination' state while map1
3981 * contains the current state. When not migrating map0 contains the
3982 * current state. This routine assumes that map[0].map_state is set to
3983 * the current array state before being called.
3985 * Migration is indicated by one of the following states
3986 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
3987 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
3988 * map1state=unitialized)
3989 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
3991 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
3992 * map1state=degraded)
3993 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
3996 static void migrate(struct imsm_dev
*dev
, struct intel_super
*super
,
3997 __u8 to_state
, int migr_type
)
3999 struct imsm_map
*dest
;
4000 struct imsm_map
*src
= get_imsm_map(dev
, MAP_0
);
4002 dev
->vol
.migr_state
= 1;
4003 set_migr_type(dev
, migr_type
);
4004 dev
->vol
.curr_migr_unit
= 0;
4005 dest
= get_imsm_map(dev
, MAP_1
);
4007 /* duplicate and then set the target end state in map[0] */
4008 memcpy(dest
, src
, sizeof_imsm_map(src
));
4009 if (migr_type
== MIGR_REBUILD
|| migr_type
== MIGR_GEN_MIGR
) {
4013 for (i
= 0; i
< src
->num_members
; i
++) {
4014 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
4015 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
4019 if (migr_type
== MIGR_GEN_MIGR
)
4020 /* Clear migration record */
4021 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
4023 src
->map_state
= to_state
;
4026 static void end_migration(struct imsm_dev
*dev
, struct intel_super
*super
,
4029 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
4030 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
== 0 ?
4034 /* merge any IMSM_ORD_REBUILD bits that were not successfully
4035 * completed in the last migration.
4037 * FIXME add support for raid-level-migration
4039 if (map_state
!= map
->map_state
&& (is_gen_migration(dev
) == 0) &&
4040 prev
->map_state
!= IMSM_T_STATE_UNINITIALIZED
) {
4041 /* when final map state is other than expected
4042 * merge maps (not for migration)
4046 for (i
= 0; i
< prev
->num_members
; i
++)
4047 for (j
= 0; j
< map
->num_members
; j
++)
4048 /* during online capacity expansion
4049 * disks position can be changed
4050 * if takeover is used
4052 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
4053 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
4054 map
->disk_ord_tbl
[j
] |=
4055 prev
->disk_ord_tbl
[i
];
4058 failed
= imsm_count_failed(super
, dev
, MAP_0
);
4059 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
4062 dev
->vol
.migr_state
= 0;
4063 set_migr_type(dev
, 0);
4064 dev
->vol
.curr_migr_unit
= 0;
4065 map
->map_state
= map_state
;
4068 static int parse_raid_devices(struct intel_super
*super
)
4071 struct imsm_dev
*dev_new
;
4072 size_t len
, len_migr
;
4074 size_t space_needed
= 0;
4075 struct imsm_super
*mpb
= super
->anchor
;
4077 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
4078 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
4079 struct intel_dev
*dv
;
4081 len
= sizeof_imsm_dev(dev_iter
, 0);
4082 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
4084 space_needed
+= len_migr
- len
;
4086 dv
= xmalloc(sizeof(*dv
));
4087 if (max_len
< len_migr
)
4089 if (max_len
> len_migr
)
4090 space_needed
+= max_len
- len_migr
;
4091 dev_new
= xmalloc(max_len
);
4092 imsm_copy_dev(dev_new
, dev_iter
);
4095 dv
->next
= super
->devlist
;
4096 super
->devlist
= dv
;
4099 /* ensure that super->buf is large enough when all raid devices
4102 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
4105 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
,
4106 super
->sector_size
);
4107 if (posix_memalign(&buf
, MAX_SECTOR_SIZE
, len
) != 0)
4110 memcpy(buf
, super
->buf
, super
->len
);
4111 memset(buf
+ super
->len
, 0, len
- super
->len
);
4117 super
->extra_space
+= space_needed
;
4122 /*******************************************************************************
4123 * Function: check_mpb_migr_compatibility
4124 * Description: Function checks for unsupported migration features:
4125 * - migration optimization area (pba_of_lba0)
4126 * - descending reshape (ascending_migr)
4128 * super : imsm metadata information
4130 * 0 : migration is compatible
4131 * -1 : migration is not compatible
4132 ******************************************************************************/
4133 int check_mpb_migr_compatibility(struct intel_super
*super
)
4135 struct imsm_map
*map0
, *map1
;
4136 struct migr_record
*migr_rec
= super
->migr_rec
;
4139 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
4140 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
4143 dev_iter
->vol
.migr_state
== 1 &&
4144 dev_iter
->vol
.migr_type
== MIGR_GEN_MIGR
) {
4145 /* This device is migrating */
4146 map0
= get_imsm_map(dev_iter
, MAP_0
);
4147 map1
= get_imsm_map(dev_iter
, MAP_1
);
4148 if (pba_of_lba0(map0
) != pba_of_lba0(map1
))
4149 /* migration optimization area was used */
4151 if (migr_rec
->ascending_migr
== 0
4152 && migr_rec
->dest_depth_per_unit
> 0)
4153 /* descending reshape not supported yet */
4160 static void __free_imsm(struct intel_super
*super
, int free_disks
);
4162 /* load_imsm_mpb - read matrix metadata
4163 * allocates super->mpb to be freed by free_imsm
4165 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
4167 unsigned long long dsize
;
4168 unsigned long long sectors
;
4169 unsigned int sector_size
= super
->sector_size
;
4171 struct imsm_super
*anchor
;
4174 get_dev_size(fd
, NULL
, &dsize
);
4175 if (dsize
< 2*sector_size
) {
4177 pr_err("%s: device to small for imsm\n",
4182 if (lseek64(fd
, dsize
- (sector_size
* 2), SEEK_SET
) < 0) {
4184 pr_err("Cannot seek to anchor block on %s: %s\n",
4185 devname
, strerror(errno
));
4189 if (posix_memalign((void **)&anchor
, sector_size
, sector_size
) != 0) {
4191 pr_err("Failed to allocate imsm anchor buffer on %s\n", devname
);
4194 if ((unsigned int)read(fd
, anchor
, sector_size
) != sector_size
) {
4196 pr_err("Cannot read anchor block on %s: %s\n",
4197 devname
, strerror(errno
));
4202 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
4204 pr_err("no IMSM anchor on %s\n", devname
);
4209 __free_imsm(super
, 0);
4210 /* reload capability and hba */
4212 /* capability and hba must be updated with new super allocation */
4213 find_intel_hba_capability(fd
, super
, devname
);
4214 super
->len
= ROUND_UP(anchor
->mpb_size
, sector_size
);
4215 if (posix_memalign(&super
->buf
, MAX_SECTOR_SIZE
, super
->len
) != 0) {
4217 pr_err("unable to allocate %zu byte mpb buffer\n",
4222 memcpy(super
->buf
, anchor
, sector_size
);
4224 sectors
= mpb_sectors(anchor
, sector_size
) - 1;
4227 if (posix_memalign(&super
->migr_rec_buf
, sector_size
,
4228 MIGR_REC_BUF_SECTORS
*sector_size
) != 0) {
4229 pr_err("could not allocate migr_rec buffer\n");
4233 super
->clean_migration_record_by_mdmon
= 0;
4236 check_sum
= __gen_imsm_checksum(super
->anchor
);
4237 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
4239 pr_err("IMSM checksum %x != %x on %s\n",
4241 __le32_to_cpu(super
->anchor
->check_sum
),
4249 /* read the extended mpb */
4250 if (lseek64(fd
, dsize
- (sector_size
* (2 + sectors
)), SEEK_SET
) < 0) {
4252 pr_err("Cannot seek to extended mpb on %s: %s\n",
4253 devname
, strerror(errno
));
4257 if ((unsigned int)read(fd
, super
->buf
+ sector_size
,
4258 super
->len
- sector_size
) != super
->len
- sector_size
) {
4260 pr_err("Cannot read extended mpb on %s: %s\n",
4261 devname
, strerror(errno
));
4265 check_sum
= __gen_imsm_checksum(super
->anchor
);
4266 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
4268 pr_err("IMSM checksum %x != %x on %s\n",
4269 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
4277 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
);
4279 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
4280 static void clear_hi(struct intel_super
*super
)
4282 struct imsm_super
*mpb
= super
->anchor
;
4284 if (mpb
->attributes
& MPB_ATTRIB_2TB_DISK
)
4286 for (i
= 0; i
< mpb
->num_disks
; ++i
) {
4287 struct imsm_disk
*disk
= &mpb
->disk
[i
];
4288 disk
->total_blocks_hi
= 0;
4290 for (i
= 0; i
< mpb
->num_raid_devs
; ++i
) {
4291 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4294 for (n
= 0; n
< 2; ++n
) {
4295 struct imsm_map
*map
= get_imsm_map(dev
, n
);
4298 map
->pba_of_lba0_hi
= 0;
4299 map
->blocks_per_member_hi
= 0;
4300 map
->num_data_stripes_hi
= 0;
4306 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
4310 err
= load_imsm_mpb(fd
, super
, devname
);
4313 if (super
->sector_size
== 4096)
4314 convert_from_4k(super
);
4315 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
4318 err
= parse_raid_devices(super
);
4321 err
= load_bbm_log(super
);
4326 static void __free_imsm_disk(struct dl
*d
)
4338 static void free_imsm_disks(struct intel_super
*super
)
4342 while (super
->disks
) {
4344 super
->disks
= d
->next
;
4345 __free_imsm_disk(d
);
4347 while (super
->disk_mgmt_list
) {
4348 d
= super
->disk_mgmt_list
;
4349 super
->disk_mgmt_list
= d
->next
;
4350 __free_imsm_disk(d
);
4352 while (super
->missing
) {
4354 super
->missing
= d
->next
;
4355 __free_imsm_disk(d
);
4360 /* free all the pieces hanging off of a super pointer */
4361 static void __free_imsm(struct intel_super
*super
, int free_disks
)
4363 struct intel_hba
*elem
, *next
;
4369 /* unlink capability description */
4371 if (super
->migr_rec_buf
) {
4372 free(super
->migr_rec_buf
);
4373 super
->migr_rec_buf
= NULL
;
4376 free_imsm_disks(super
);
4377 free_devlist(super
);
4381 free((void *)elem
->path
);
4387 free(super
->bbm_log
);
4391 static void free_imsm(struct intel_super
*super
)
4393 __free_imsm(super
, 1);
4394 free(super
->bb
.entries
);
4398 static void free_super_imsm(struct supertype
*st
)
4400 struct intel_super
*super
= st
->sb
;
4409 static struct intel_super
*alloc_super(void)
4411 struct intel_super
*super
= xcalloc(1, sizeof(*super
));
4413 super
->current_vol
= -1;
4414 super
->create_offset
= ~((unsigned long long) 0);
4416 super
->bb
.entries
= xmalloc(BBM_LOG_MAX_ENTRIES
*
4417 sizeof(struct md_bb_entry
));
4418 if (!super
->bb
.entries
) {
4427 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
4429 static int find_intel_hba_capability(int fd
, struct intel_super
*super
, char *devname
)
4431 struct sys_dev
*hba_name
;
4434 if (fd
< 0 || check_env("IMSM_NO_PLATFORM")) {
4439 hba_name
= find_disk_attached_hba(fd
, NULL
);
4442 pr_err("%s is not attached to Intel(R) RAID controller.\n",
4446 rv
= attach_hba_to_super(super
, hba_name
);
4449 struct intel_hba
*hba
= super
->hba
;
4451 pr_err("%s is attached to Intel(R) %s %s (%s),\n"
4452 " but the container is assigned to Intel(R) %s %s (",
4454 get_sys_dev_type(hba_name
->type
),
4455 hba_name
->type
== SYS_DEV_VMD
? "domain" : "RAID controller",
4456 hba_name
->pci_id
? : "Err!",
4457 get_sys_dev_type(super
->hba
->type
),
4458 hba
->type
== SYS_DEV_VMD
? "domain" : "RAID controller");
4461 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
4463 fprintf(stderr
, ", ");
4466 fprintf(stderr
, ").\n"
4467 " Mixing devices attached to different %s is not allowed.\n",
4468 hba_name
->type
== SYS_DEV_VMD
? "VMD domains" : "controllers");
4472 super
->orom
= find_imsm_capability(hba_name
);
4479 /* find_missing - helper routine for load_super_imsm_all that identifies
4480 * disks that have disappeared from the system. This routine relies on
4481 * the mpb being uptodate, which it is at load time.
4483 static int find_missing(struct intel_super
*super
)
4486 struct imsm_super
*mpb
= super
->anchor
;
4488 struct imsm_disk
*disk
;
4490 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4491 disk
= __get_imsm_disk(mpb
, i
);
4492 dl
= serial_to_dl(disk
->serial
, super
);
4496 dl
= xmalloc(sizeof(*dl
));
4500 dl
->devname
= xstrdup("missing");
4502 serialcpy(dl
->serial
, disk
->serial
);
4505 dl
->next
= super
->missing
;
4506 super
->missing
= dl
;
4512 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
4514 struct intel_disk
*idisk
= disk_list
;
4517 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
4519 idisk
= idisk
->next
;
4525 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
4526 struct intel_super
*super
,
4527 struct intel_disk
**disk_list
)
4529 struct imsm_disk
*d
= &super
->disks
->disk
;
4530 struct imsm_super
*mpb
= super
->anchor
;
4533 for (i
= 0; i
< tbl_size
; i
++) {
4534 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
4535 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
4537 if (tbl_mpb
->family_num
== mpb
->family_num
) {
4538 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
4539 dprintf("mpb from %d:%d matches %d:%d\n",
4540 super
->disks
->major
,
4541 super
->disks
->minor
,
4542 table
[i
]->disks
->major
,
4543 table
[i
]->disks
->minor
);
4547 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
4548 is_configured(d
) == is_configured(tbl_d
)) &&
4549 tbl_mpb
->generation_num
< mpb
->generation_num
) {
4550 /* current version of the mpb is a
4551 * better candidate than the one in
4552 * super_table, but copy over "cross
4553 * generational" status
4555 struct intel_disk
*idisk
;
4557 dprintf("mpb from %d:%d replaces %d:%d\n",
4558 super
->disks
->major
,
4559 super
->disks
->minor
,
4560 table
[i
]->disks
->major
,
4561 table
[i
]->disks
->minor
);
4563 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
4564 if (idisk
&& is_failed(&idisk
->disk
))
4565 tbl_d
->status
|= FAILED_DISK
;
4568 struct intel_disk
*idisk
;
4569 struct imsm_disk
*disk
;
4571 /* tbl_mpb is more up to date, but copy
4572 * over cross generational status before
4575 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
4576 if (disk
&& is_failed(disk
))
4577 d
->status
|= FAILED_DISK
;
4579 idisk
= disk_list_get(d
->serial
, *disk_list
);
4582 if (disk
&& is_configured(disk
))
4583 idisk
->disk
.status
|= CONFIGURED_DISK
;
4586 dprintf("mpb from %d:%d prefer %d:%d\n",
4587 super
->disks
->major
,
4588 super
->disks
->minor
,
4589 table
[i
]->disks
->major
,
4590 table
[i
]->disks
->minor
);
4598 table
[tbl_size
++] = super
;
4602 /* update/extend the merged list of imsm_disk records */
4603 for (j
= 0; j
< mpb
->num_disks
; j
++) {
4604 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
4605 struct intel_disk
*idisk
;
4607 idisk
= disk_list_get(disk
->serial
, *disk_list
);
4609 idisk
->disk
.status
|= disk
->status
;
4610 if (is_configured(&idisk
->disk
) ||
4611 is_failed(&idisk
->disk
))
4612 idisk
->disk
.status
&= ~(SPARE_DISK
);
4614 idisk
= xcalloc(1, sizeof(*idisk
));
4615 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
4616 idisk
->disk
= *disk
;
4617 idisk
->next
= *disk_list
;
4621 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
4628 static struct intel_super
*
4629 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
4632 struct imsm_super
*mpb
= super
->anchor
;
4636 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4637 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
4638 struct intel_disk
*idisk
;
4640 idisk
= disk_list_get(disk
->serial
, disk_list
);
4642 if (idisk
->owner
== owner
||
4643 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
4646 dprintf("'%.16s' owner %d != %d\n",
4647 disk
->serial
, idisk
->owner
,
4650 dprintf("unknown disk %x [%d]: %.16s\n",
4651 __le32_to_cpu(mpb
->family_num
), i
,
4657 if (ok_count
== mpb
->num_disks
)
4662 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
4664 struct intel_super
*s
;
4666 for (s
= super_list
; s
; s
= s
->next
) {
4667 if (family_num
!= s
->anchor
->family_num
)
4669 pr_err("Conflict, offlining family %#x on '%s'\n",
4670 __le32_to_cpu(family_num
), s
->disks
->devname
);
4674 static struct intel_super
*
4675 imsm_thunderdome(struct intel_super
**super_list
, int len
)
4677 struct intel_super
*super_table
[len
];
4678 struct intel_disk
*disk_list
= NULL
;
4679 struct intel_super
*champion
, *spare
;
4680 struct intel_super
*s
, **del
;
4685 memset(super_table
, 0, sizeof(super_table
));
4686 for (s
= *super_list
; s
; s
= s
->next
)
4687 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
4689 for (i
= 0; i
< tbl_size
; i
++) {
4690 struct imsm_disk
*d
;
4691 struct intel_disk
*idisk
;
4692 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
4695 d
= &s
->disks
->disk
;
4697 /* 'd' must appear in merged disk list for its
4698 * configuration to be valid
4700 idisk
= disk_list_get(d
->serial
, disk_list
);
4701 if (idisk
&& idisk
->owner
== i
)
4702 s
= validate_members(s
, disk_list
, i
);
4707 dprintf("marking family: %#x from %d:%d offline\n",
4709 super_table
[i
]->disks
->major
,
4710 super_table
[i
]->disks
->minor
);
4714 /* This is where the mdadm implementation differs from the Windows
4715 * driver which has no strict concept of a container. We can only
4716 * assemble one family from a container, so when returning a prodigal
4717 * array member to this system the code will not be able to disambiguate
4718 * the container contents that should be assembled ("foreign" versus
4719 * "local"). It requires user intervention to set the orig_family_num
4720 * to a new value to establish a new container. The Windows driver in
4721 * this situation fixes up the volume name in place and manages the
4722 * foreign array as an independent entity.
4727 for (i
= 0; i
< tbl_size
; i
++) {
4728 struct intel_super
*tbl_ent
= super_table
[i
];
4734 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
4739 if (s
&& !is_spare
) {
4740 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
4742 } else if (!s
&& !is_spare
)
4755 pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n",
4756 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
4758 /* collect all dl's onto 'champion', and update them to
4759 * champion's version of the status
4761 for (s
= *super_list
; s
; s
= s
->next
) {
4762 struct imsm_super
*mpb
= champion
->anchor
;
4763 struct dl
*dl
= s
->disks
;
4768 mpb
->attributes
|= s
->anchor
->attributes
& MPB_ATTRIB_2TB_DISK
;
4770 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4771 struct imsm_disk
*disk
;
4773 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
4776 /* only set index on disks that are a member of
4777 * a populated contianer, i.e. one with
4780 if (is_failed(&dl
->disk
))
4782 else if (is_spare(&dl
->disk
))
4788 if (i
>= mpb
->num_disks
) {
4789 struct intel_disk
*idisk
;
4791 idisk
= disk_list_get(dl
->serial
, disk_list
);
4792 if (idisk
&& is_spare(&idisk
->disk
) &&
4793 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
4801 dl
->next
= champion
->disks
;
4802 champion
->disks
= dl
;
4806 /* delete 'champion' from super_list */
4807 for (del
= super_list
; *del
; ) {
4808 if (*del
== champion
) {
4809 *del
= (*del
)->next
;
4812 del
= &(*del
)->next
;
4814 champion
->next
= NULL
;
4818 struct intel_disk
*idisk
= disk_list
;
4820 disk_list
= disk_list
->next
;
4828 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
);
4829 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
4830 int major
, int minor
, int keep_fd
);
4832 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4833 int *max
, int keep_fd
);
4835 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
4836 char *devname
, struct md_list
*devlist
,
4839 struct intel_super
*super_list
= NULL
;
4840 struct intel_super
*super
= NULL
;
4845 /* 'fd' is an opened container */
4846 err
= get_sra_super_block(fd
, &super_list
, devname
, &i
, keep_fd
);
4848 /* get super block from devlist devices */
4849 err
= get_devlist_super_block(devlist
, &super_list
, &i
, keep_fd
);
4852 /* all mpbs enter, maybe one leaves */
4853 super
= imsm_thunderdome(&super_list
, i
);
4859 if (find_missing(super
) != 0) {
4865 /* load migration record */
4866 err
= load_imsm_migr_rec(super
, NULL
);
4868 /* migration is in progress,
4869 * but migr_rec cannot be loaded,
4875 /* Check migration compatibility */
4876 if (err
== 0 && check_mpb_migr_compatibility(super
) != 0) {
4877 pr_err("Unsupported migration detected");
4879 fprintf(stderr
, " on %s\n", devname
);
4881 fprintf(stderr
, " (IMSM).\n");
4890 while (super_list
) {
4891 struct intel_super
*s
= super_list
;
4893 super_list
= super_list
->next
;
4902 strcpy(st
->container_devnm
, fd2devnm(fd
));
4904 st
->container_devnm
[0] = 0;
4905 if (err
== 0 && st
->ss
== NULL
) {
4906 st
->ss
= &super_imsm
;
4907 st
->minor_version
= 0;
4908 st
->max_devs
= IMSM_MAX_DEVICES
;
4914 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4915 int *max
, int keep_fd
)
4917 struct md_list
*tmpdev
;
4921 for (i
= 0, tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
4922 if (tmpdev
->used
!= 1)
4924 if (tmpdev
->container
== 1) {
4926 int fd
= dev_open(tmpdev
->devname
, O_RDONLY
|O_EXCL
);
4928 pr_err("cannot open device %s: %s\n",
4929 tmpdev
->devname
, strerror(errno
));
4933 err
= get_sra_super_block(fd
, super_list
,
4934 tmpdev
->devname
, &lmax
,
4943 int major
= major(tmpdev
->st_rdev
);
4944 int minor
= minor(tmpdev
->st_rdev
);
4945 err
= get_super_block(super_list
,
4962 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
4963 int major
, int minor
, int keep_fd
)
4965 struct intel_super
*s
;
4977 sprintf(nm
, "%d:%d", major
, minor
);
4978 dfd
= dev_open(nm
, O_RDWR
);
4984 get_dev_sector_size(dfd
, NULL
, &s
->sector_size
);
4985 find_intel_hba_capability(dfd
, s
, devname
);
4986 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4988 /* retry the load if we might have raced against mdmon */
4989 if (err
== 3 && devnm
&& mdmon_running(devnm
))
4990 for (retry
= 0; retry
< 3; retry
++) {
4992 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4998 s
->next
= *super_list
;
5006 if (dfd
>= 0 && !keep_fd
)
5013 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
)
5020 sra
= sysfs_read(fd
, NULL
, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
5024 if (sra
->array
.major_version
!= -1 ||
5025 sra
->array
.minor_version
!= -2 ||
5026 strcmp(sra
->text_version
, "imsm") != 0) {
5031 devnm
= fd2devnm(fd
);
5032 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
5033 if (get_super_block(super_list
, devnm
, devname
,
5034 sd
->disk
.major
, sd
->disk
.minor
, keep_fd
) != 0) {
5045 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
5047 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
, NULL
, 1);
5050 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
5052 struct intel_super
*super
;
5056 if (test_partition(fd
))
5057 /* IMSM not allowed on partitions */
5060 free_super_imsm(st
);
5062 super
= alloc_super();
5063 get_dev_sector_size(fd
, NULL
, &super
->sector_size
);
5066 /* Load hba and capabilities if they exist.
5067 * But do not preclude loading metadata in case capabilities or hba are
5068 * non-compliant and ignore_hw_compat is set.
5070 rv
= find_intel_hba_capability(fd
, super
, devname
);
5071 /* no orom/efi or non-intel hba of the disk */
5072 if (rv
!= 0 && st
->ignore_hw_compat
== 0) {
5074 pr_err("No OROM/EFI properties for %s\n", devname
);
5078 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
5080 /* retry the load if we might have raced against mdmon */
5082 struct mdstat_ent
*mdstat
= NULL
;
5083 char *name
= fd2kname(fd
);
5086 mdstat
= mdstat_by_component(name
);
5088 if (mdstat
&& mdmon_running(mdstat
->devnm
) && getpid() != mdmon_pid(mdstat
->devnm
)) {
5089 for (retry
= 0; retry
< 3; retry
++) {
5091 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
5097 free_mdstat(mdstat
);
5102 pr_err("Failed to load all information sections on %s\n", devname
);
5108 if (st
->ss
== NULL
) {
5109 st
->ss
= &super_imsm
;
5110 st
->minor_version
= 0;
5111 st
->max_devs
= IMSM_MAX_DEVICES
;
5114 /* load migration record */
5115 if (load_imsm_migr_rec(super
, NULL
) == 0) {
5116 /* Check for unsupported migration features */
5117 if (check_mpb_migr_compatibility(super
) != 0) {
5118 pr_err("Unsupported migration detected");
5120 fprintf(stderr
, " on %s\n", devname
);
5122 fprintf(stderr
, " (IMSM).\n");
5130 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
5132 if (info
->level
== 1)
5134 return info
->chunk_size
>> 9;
5137 static unsigned long long info_to_blocks_per_member(mdu_array_info_t
*info
,
5138 unsigned long long size
)
5140 if (info
->level
== 1)
5143 return (size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
5146 static void imsm_update_version_info(struct intel_super
*super
)
5148 /* update the version and attributes */
5149 struct imsm_super
*mpb
= super
->anchor
;
5151 struct imsm_dev
*dev
;
5152 struct imsm_map
*map
;
5155 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5156 dev
= get_imsm_dev(super
, i
);
5157 map
= get_imsm_map(dev
, MAP_0
);
5158 if (__le32_to_cpu(dev
->size_high
) > 0)
5159 mpb
->attributes
|= MPB_ATTRIB_2TB
;
5161 /* FIXME detect when an array spans a port multiplier */
5163 mpb
->attributes
|= MPB_ATTRIB_PM
;
5166 if (mpb
->num_raid_devs
> 1 ||
5167 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
5168 version
= MPB_VERSION_ATTRIBS
;
5169 switch (get_imsm_raid_level(map
)) {
5170 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
5171 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
5172 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
5173 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
5176 if (map
->num_members
>= 5)
5177 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
5178 else if (dev
->status
== DEV_CLONE_N_GO
)
5179 version
= MPB_VERSION_CNG
;
5180 else if (get_imsm_raid_level(map
) == 5)
5181 version
= MPB_VERSION_RAID5
;
5182 else if (map
->num_members
>= 3)
5183 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
5184 else if (get_imsm_raid_level(map
) == 1)
5185 version
= MPB_VERSION_RAID1
;
5187 version
= MPB_VERSION_RAID0
;
5189 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
5193 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
5195 struct imsm_super
*mpb
= super
->anchor
;
5196 char *reason
= NULL
;
5199 if (strlen(name
) > MAX_RAID_SERIAL_LEN
)
5200 reason
= "must be 16 characters or less";
5202 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5203 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
5205 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
5206 reason
= "already exists";
5211 if (reason
&& !quiet
)
5212 pr_err("imsm volume name %s\n", reason
);
5217 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
5218 struct shape
*s
, char *name
,
5219 char *homehost
, int *uuid
,
5220 long long data_offset
)
5222 /* We are creating a volume inside a pre-existing container.
5223 * so st->sb is already set.
5225 struct intel_super
*super
= st
->sb
;
5226 unsigned int sector_size
= super
->sector_size
;
5227 struct imsm_super
*mpb
= super
->anchor
;
5228 struct intel_dev
*dv
;
5229 struct imsm_dev
*dev
;
5230 struct imsm_vol
*vol
;
5231 struct imsm_map
*map
;
5232 int idx
= mpb
->num_raid_devs
;
5234 unsigned long long array_blocks
;
5235 size_t size_old
, size_new
;
5236 unsigned long long num_data_stripes
;
5237 unsigned int data_disks
;
5238 unsigned long long size_per_member
;
5240 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
5241 pr_err("This imsm-container already has the maximum of %d volumes\n", super
->orom
->vpa
);
5245 /* ensure the mpb is large enough for the new data */
5246 size_old
= __le32_to_cpu(mpb
->mpb_size
);
5247 size_new
= disks_to_mpb_size(info
->nr_disks
);
5248 if (size_new
> size_old
) {
5250 size_t size_round
= ROUND_UP(size_new
, sector_size
);
5252 if (posix_memalign(&mpb_new
, sector_size
, size_round
) != 0) {
5253 pr_err("could not allocate new mpb\n");
5256 if (posix_memalign(&super
->migr_rec_buf
, sector_size
,
5257 MIGR_REC_BUF_SECTORS
*sector_size
) != 0) {
5258 pr_err("could not allocate migr_rec buffer\n");
5264 memcpy(mpb_new
, mpb
, size_old
);
5267 super
->anchor
= mpb_new
;
5268 mpb
->mpb_size
= __cpu_to_le32(size_new
);
5269 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
5270 super
->len
= size_round
;
5272 super
->current_vol
= idx
;
5274 /* handle 'failed_disks' by either:
5275 * a) create dummy disk entries in the table if this the first
5276 * volume in the array. We add them here as this is the only
5277 * opportunity to add them. add_to_super_imsm_volume()
5278 * handles the non-failed disks and continues incrementing
5280 * b) validate that 'failed_disks' matches the current number
5281 * of missing disks if the container is populated
5283 if (super
->current_vol
== 0) {
5285 for (i
= 0; i
< info
->failed_disks
; i
++) {
5286 struct imsm_disk
*disk
;
5289 disk
= __get_imsm_disk(mpb
, i
);
5290 disk
->status
= CONFIGURED_DISK
| FAILED_DISK
;
5291 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
5292 snprintf((char *) disk
->serial
, MAX_RAID_SERIAL_LEN
,
5293 "missing:%d", (__u8
)i
);
5295 find_missing(super
);
5300 for (d
= super
->missing
; d
; d
= d
->next
)
5302 if (info
->failed_disks
> missing
) {
5303 pr_err("unable to add 'missing' disk to container\n");
5308 if (!check_name(super
, name
, 0))
5310 dv
= xmalloc(sizeof(*dv
));
5311 dev
= xcalloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
5312 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
5313 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
5314 info
->layout
, info
->chunk_size
,
5315 s
->size
* BLOCKS_PER_KB
);
5316 data_disks
= get_data_disks(info
->level
, info
->layout
,
5318 array_blocks
= round_size_to_mb(array_blocks
, data_disks
);
5319 size_per_member
= array_blocks
/ data_disks
;
5321 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
5322 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
5323 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
5325 vol
->migr_state
= 0;
5326 set_migr_type(dev
, MIGR_INIT
);
5327 vol
->dirty
= !info
->state
;
5328 vol
->curr_migr_unit
= 0;
5329 map
= get_imsm_map(dev
, MAP_0
);
5330 set_pba_of_lba0(map
, super
->create_offset
);
5331 set_blocks_per_member(map
, info_to_blocks_per_member(info
,
5334 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
5335 map
->failed_disk_num
= ~0;
5336 if (info
->level
> 0)
5337 map
->map_state
= (info
->state
? IMSM_T_STATE_NORMAL
5338 : IMSM_T_STATE_UNINITIALIZED
);
5340 map
->map_state
= info
->failed_disks
? IMSM_T_STATE_FAILED
:
5341 IMSM_T_STATE_NORMAL
;
5344 if (info
->level
== 1 && info
->raid_disks
> 2) {
5347 pr_err("imsm does not support more than 2 disksin a raid1 volume\n");
5351 map
->raid_level
= info
->level
;
5352 if (info
->level
== 10) {
5353 map
->raid_level
= 1;
5354 map
->num_domains
= info
->raid_disks
/ 2;
5355 } else if (info
->level
== 1)
5356 map
->num_domains
= info
->raid_disks
;
5358 map
->num_domains
= 1;
5360 /* info->size is only int so use the 'size' parameter instead */
5361 num_data_stripes
= size_per_member
/ info_to_blocks_per_strip(info
);
5362 num_data_stripes
/= map
->num_domains
;
5363 set_num_data_stripes(map
, num_data_stripes
);
5365 map
->num_members
= info
->raid_disks
;
5366 for (i
= 0; i
< map
->num_members
; i
++) {
5367 /* initialized in add_to_super */
5368 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
5370 mpb
->num_raid_devs
++;
5372 if (s
->consistency_policy
== UnSet
||
5373 s
->consistency_policy
== CONSISTENCY_POLICY_RESYNC
||
5374 s
->consistency_policy
== CONSISTENCY_POLICY_NONE
) {
5375 dev
->rwh_policy
= RWH_OFF
;
5376 } else if (s
->consistency_policy
== CONSISTENCY_POLICY_PPL
) {
5377 dev
->rwh_policy
= RWH_DISTRIBUTED
;
5381 pr_err("imsm does not support consistency policy %s\n",
5382 map_num(consistency_policies
, s
->consistency_policy
));
5387 dv
->index
= super
->current_vol
;
5388 dv
->next
= super
->devlist
;
5389 super
->devlist
= dv
;
5391 imsm_update_version_info(super
);
5396 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
5397 struct shape
*s
, char *name
,
5398 char *homehost
, int *uuid
,
5399 unsigned long long data_offset
)
5401 /* This is primarily called by Create when creating a new array.
5402 * We will then get add_to_super called for each component, and then
5403 * write_init_super called to write it out to each device.
5404 * For IMSM, Create can create on fresh devices or on a pre-existing
5406 * To create on a pre-existing array a different method will be called.
5407 * This one is just for fresh drives.
5409 struct intel_super
*super
;
5410 struct imsm_super
*mpb
;
5414 if (data_offset
!= INVALID_SECTORS
) {
5415 pr_err("data-offset not supported by imsm\n");
5420 return init_super_imsm_volume(st
, info
, s
, name
, homehost
, uuid
,
5424 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
5426 mpb_size
= MAX_SECTOR_SIZE
;
5428 super
= alloc_super();
5430 posix_memalign(&super
->buf
, MAX_SECTOR_SIZE
, mpb_size
) != 0) {
5435 pr_err("could not allocate superblock\n");
5438 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
5439 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
) != 0) {
5440 pr_err("could not allocate migr_rec buffer\n");
5445 memset(super
->buf
, 0, mpb_size
);
5447 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5451 /* zeroing superblock */
5455 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5457 version
= (char *) mpb
->sig
;
5458 strcpy(version
, MPB_SIGNATURE
);
5459 version
+= strlen(MPB_SIGNATURE
);
5460 strcpy(version
, MPB_VERSION_RAID0
);
5465 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
5466 int fd
, char *devname
)
5468 struct intel_super
*super
= st
->sb
;
5469 struct imsm_super
*mpb
= super
->anchor
;
5470 struct imsm_disk
*_disk
;
5471 struct imsm_dev
*dev
;
5472 struct imsm_map
*map
;
5476 dev
= get_imsm_dev(super
, super
->current_vol
);
5477 map
= get_imsm_map(dev
, MAP_0
);
5479 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
5480 pr_err("%s: Cannot add spare devices to IMSM volume\n",
5486 /* we're doing autolayout so grab the pre-marked (in
5487 * validate_geometry) raid_disk
5489 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5490 if (dl
->raiddisk
== dk
->raid_disk
)
5493 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5494 if (dl
->major
== dk
->major
&&
5495 dl
->minor
== dk
->minor
)
5500 pr_err("%s is not a member of the same container\n", devname
);
5504 /* add a pristine spare to the metadata */
5505 if (dl
->index
< 0) {
5506 dl
->index
= super
->anchor
->num_disks
;
5507 super
->anchor
->num_disks
++;
5509 /* Check the device has not already been added */
5510 slot
= get_imsm_disk_slot(map
, dl
->index
);
5512 (get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
) & IMSM_ORD_REBUILD
) == 0) {
5513 pr_err("%s has been included in this array twice\n",
5517 set_imsm_ord_tbl_ent(map
, dk
->raid_disk
, dl
->index
);
5518 dl
->disk
.status
= CONFIGURED_DISK
;
5520 /* update size of 'missing' disks to be at least as large as the
5521 * largest acitve member (we only have dummy missing disks when
5522 * creating the first volume)
5524 if (super
->current_vol
== 0) {
5525 for (df
= super
->missing
; df
; df
= df
->next
) {
5526 if (total_blocks(&dl
->disk
) > total_blocks(&df
->disk
))
5527 set_total_blocks(&df
->disk
, total_blocks(&dl
->disk
));
5528 _disk
= __get_imsm_disk(mpb
, df
->index
);
5533 /* refresh unset/failed slots to point to valid 'missing' entries */
5534 for (df
= super
->missing
; df
; df
= df
->next
)
5535 for (slot
= 0; slot
< mpb
->num_disks
; slot
++) {
5536 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
5538 if ((ord
& IMSM_ORD_REBUILD
) == 0)
5540 set_imsm_ord_tbl_ent(map
, slot
, df
->index
| IMSM_ORD_REBUILD
);
5541 if (is_gen_migration(dev
)) {
5542 struct imsm_map
*map2
= get_imsm_map(dev
,
5544 int slot2
= get_imsm_disk_slot(map2
, df
->index
);
5545 if (slot2
< map2
->num_members
&& slot2
>= 0) {
5546 __u32 ord2
= get_imsm_ord_tbl_ent(dev
,
5549 if ((unsigned)df
->index
==
5551 set_imsm_ord_tbl_ent(map2
,
5557 dprintf("set slot:%d to missing disk:%d\n", slot
, df
->index
);
5561 /* if we are creating the first raid device update the family number */
5562 if (super
->current_vol
== 0) {
5564 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
5566 _disk
= __get_imsm_disk(mpb
, dl
->index
);
5567 if (!_dev
|| !_disk
) {
5568 pr_err("BUG mpb setup error\n");
5574 sum
+= __gen_imsm_checksum(mpb
);
5575 mpb
->family_num
= __cpu_to_le32(sum
);
5576 mpb
->orig_family_num
= mpb
->family_num
;
5578 super
->current_disk
= dl
;
5583 * Function marks disk as spare and restores disk serial
5584 * in case it was previously marked as failed by takeover operation
5586 * -1 : critical error
5587 * 0 : disk is marked as spare but serial is not set
5590 int mark_spare(struct dl
*disk
)
5592 __u8 serial
[MAX_RAID_SERIAL_LEN
];
5599 if (!imsm_read_serial(disk
->fd
, NULL
, serial
)) {
5600 /* Restore disk serial number, because takeover marks disk
5601 * as failed and adds to serial ':0' before it becomes
5604 serialcpy(disk
->serial
, serial
);
5605 serialcpy(disk
->disk
.serial
, serial
);
5608 disk
->disk
.status
= SPARE_DISK
;
5614 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
5615 int fd
, char *devname
,
5616 unsigned long long data_offset
)
5618 struct intel_super
*super
= st
->sb
;
5620 unsigned long long size
;
5621 unsigned int member_sector_size
;
5626 /* If we are on an RAID enabled platform check that the disk is
5627 * attached to the raid controller.
5628 * We do not need to test disks attachment for container based additions,
5629 * they shall be already tested when container was created/assembled.
5631 rv
= find_intel_hba_capability(fd
, super
, devname
);
5632 /* no orom/efi or non-intel hba of the disk */
5634 dprintf("capability: %p fd: %d ret: %d\n",
5635 super
->orom
, fd
, rv
);
5639 if (super
->current_vol
>= 0)
5640 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
5643 dd
= xcalloc(sizeof(*dd
), 1);
5644 dd
->major
= major(stb
.st_rdev
);
5645 dd
->minor
= minor(stb
.st_rdev
);
5646 dd
->devname
= devname
? xstrdup(devname
) : NULL
;
5649 dd
->action
= DISK_ADD
;
5650 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
5652 pr_err("failed to retrieve scsi serial, aborting\n");
5658 if (super
->hba
&& ((super
->hba
->type
== SYS_DEV_NVME
) ||
5659 (super
->hba
->type
== SYS_DEV_VMD
))) {
5661 char *devpath
= diskfd_to_devpath(fd
);
5662 char controller_path
[PATH_MAX
];
5665 pr_err("failed to get devpath, aborting\n");
5672 snprintf(controller_path
, PATH_MAX
-1, "%s/device", devpath
);
5675 if (devpath_to_vendor(controller_path
) == 0x8086) {
5677 * If Intel's NVMe drive has serial ended with
5678 * "-A","-B","-1" or "-2" it means that this is "x8"
5679 * device (double drive on single PCIe card).
5680 * User should be warned about potential data loss.
5682 for (i
= MAX_RAID_SERIAL_LEN
-1; i
> 0; i
--) {
5683 /* Skip empty character at the end */
5684 if (dd
->serial
[i
] == 0)
5687 if (((dd
->serial
[i
] == 'A') ||
5688 (dd
->serial
[i
] == 'B') ||
5689 (dd
->serial
[i
] == '1') ||
5690 (dd
->serial
[i
] == '2')) &&
5691 (dd
->serial
[i
-1] == '-'))
5692 pr_err("\tThe action you are about to take may put your data at risk.\n"
5693 "\tPlease note that x8 devices may consist of two separate x4 devices "
5694 "located on a single PCIe port.\n"
5695 "\tRAID 0 is the only supported configuration for this type of x8 device.\n");
5698 } else if (super
->hba
->type
== SYS_DEV_VMD
&& super
->orom
&&
5699 !imsm_orom_has_tpv_support(super
->orom
)) {
5700 pr_err("\tPlatform configuration does not support non-Intel NVMe drives.\n"
5701 "\tPlease refer to Intel(R) RSTe user guide.\n");
5708 get_dev_size(fd
, NULL
, &size
);
5709 get_dev_sector_size(fd
, NULL
, &member_sector_size
);
5711 if (super
->sector_size
== 0) {
5712 /* this a first device, so sector_size is not set yet */
5713 super
->sector_size
= member_sector_size
;
5714 } else if (member_sector_size
!= super
->sector_size
) {
5715 pr_err("Mixing between different sector size is forbidden, aborting...\n");
5722 /* clear migr_rec when adding disk to container */
5723 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SECTORS
*super
->sector_size
);
5724 if (lseek64(fd
, size
- MIGR_REC_SECTOR_POSITION
*super
->sector_size
,
5726 if ((unsigned int)write(fd
, super
->migr_rec_buf
,
5727 MIGR_REC_BUF_SECTORS
*super
->sector_size
) !=
5728 MIGR_REC_BUF_SECTORS
*super
->sector_size
)
5729 perror("Write migr_rec failed");
5733 serialcpy(dd
->disk
.serial
, dd
->serial
);
5734 set_total_blocks(&dd
->disk
, size
);
5735 if (__le32_to_cpu(dd
->disk
.total_blocks_hi
) > 0) {
5736 struct imsm_super
*mpb
= super
->anchor
;
5737 mpb
->attributes
|= MPB_ATTRIB_2TB_DISK
;
5740 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
5741 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
5743 dd
->disk
.scsi_id
= __cpu_to_le32(0);
5745 if (st
->update_tail
) {
5746 dd
->next
= super
->disk_mgmt_list
;
5747 super
->disk_mgmt_list
= dd
;
5749 dd
->next
= super
->disks
;
5751 super
->updates_pending
++;
5757 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
5759 struct intel_super
*super
= st
->sb
;
5762 /* remove from super works only in mdmon - for communication
5763 * manager - monitor. Check if communication memory buffer
5766 if (!st
->update_tail
) {
5767 pr_err("shall be used in mdmon context only\n");
5770 dd
= xcalloc(1, sizeof(*dd
));
5771 dd
->major
= dk
->major
;
5772 dd
->minor
= dk
->minor
;
5775 dd
->action
= DISK_REMOVE
;
5777 dd
->next
= super
->disk_mgmt_list
;
5778 super
->disk_mgmt_list
= dd
;
5783 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
5786 char buf
[MAX_SECTOR_SIZE
];
5787 struct imsm_super anchor
;
5788 } spare_record
__attribute__ ((aligned(MAX_SECTOR_SIZE
)));
5790 /* spare records have their own family number and do not have any defined raid
5793 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
5795 struct imsm_super
*mpb
= super
->anchor
;
5796 struct imsm_super
*spare
= &spare_record
.anchor
;
5800 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
));
5801 spare
->generation_num
= __cpu_to_le32(1UL);
5802 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5803 spare
->num_disks
= 1;
5804 spare
->num_raid_devs
= 0;
5805 spare
->cache_size
= mpb
->cache_size
;
5806 spare
->pwr_cycle_count
= __cpu_to_le32(1);
5808 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
5809 MPB_SIGNATURE MPB_VERSION_RAID0
);
5811 for (d
= super
->disks
; d
; d
= d
->next
) {
5815 spare
->disk
[0] = d
->disk
;
5816 if (__le32_to_cpu(d
->disk
.total_blocks_hi
) > 0)
5817 spare
->attributes
|= MPB_ATTRIB_2TB_DISK
;
5819 if (super
->sector_size
== 4096)
5820 convert_to_4k_imsm_disk(&spare
->disk
[0]);
5822 sum
= __gen_imsm_checksum(spare
);
5823 spare
->family_num
= __cpu_to_le32(sum
);
5824 spare
->orig_family_num
= 0;
5825 sum
= __gen_imsm_checksum(spare
);
5826 spare
->check_sum
= __cpu_to_le32(sum
);
5828 if (store_imsm_mpb(d
->fd
, spare
)) {
5829 pr_err("failed for device %d:%d %s\n",
5830 d
->major
, d
->minor
, strerror(errno
));
5842 static int write_super_imsm(struct supertype
*st
, int doclose
)
5844 struct intel_super
*super
= st
->sb
;
5845 unsigned int sector_size
= super
->sector_size
;
5846 struct imsm_super
*mpb
= super
->anchor
;
5852 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
5854 int clear_migration_record
= 1;
5857 /* 'generation' is incremented everytime the metadata is written */
5858 generation
= __le32_to_cpu(mpb
->generation_num
);
5860 mpb
->generation_num
= __cpu_to_le32(generation
);
5862 /* fix up cases where previous mdadm releases failed to set
5865 if (mpb
->orig_family_num
== 0)
5866 mpb
->orig_family_num
= mpb
->family_num
;
5868 for (d
= super
->disks
; d
; d
= d
->next
) {
5872 mpb
->disk
[d
->index
] = d
->disk
;
5876 for (d
= super
->missing
; d
; d
= d
->next
) {
5877 mpb
->disk
[d
->index
] = d
->disk
;
5880 mpb
->num_disks
= num_disks
;
5881 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
5883 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5884 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
5885 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
5887 imsm_copy_dev(dev
, dev2
);
5888 mpb_size
+= sizeof_imsm_dev(dev
, 0);
5890 if (is_gen_migration(dev2
))
5891 clear_migration_record
= 0;
5894 bbm_log_size
= get_imsm_bbm_log_size(super
->bbm_log
);
5897 memcpy((void *)mpb
+ mpb_size
, super
->bbm_log
, bbm_log_size
);
5898 mpb
->attributes
|= MPB_ATTRIB_BBM
;
5900 mpb
->attributes
&= ~MPB_ATTRIB_BBM
;
5902 super
->anchor
->bbm_log_size
= __cpu_to_le32(bbm_log_size
);
5903 mpb_size
+= bbm_log_size
;
5904 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5907 assert(super
->len
== 0 || mpb_size
<= super
->len
);
5910 /* recalculate checksum */
5911 sum
= __gen_imsm_checksum(mpb
);
5912 mpb
->check_sum
= __cpu_to_le32(sum
);
5914 if (super
->clean_migration_record_by_mdmon
) {
5915 clear_migration_record
= 1;
5916 super
->clean_migration_record_by_mdmon
= 0;
5918 if (clear_migration_record
)
5919 memset(super
->migr_rec_buf
, 0,
5920 MIGR_REC_BUF_SECTORS
*sector_size
);
5922 if (sector_size
== 4096)
5923 convert_to_4k(super
);
5925 /* write the mpb for disks that compose raid devices */
5926 for (d
= super
->disks
; d
; d
= d
->next
) {
5927 if (d
->index
< 0 || is_failed(&d
->disk
))
5930 if (clear_migration_record
) {
5931 unsigned long long dsize
;
5933 get_dev_size(d
->fd
, NULL
, &dsize
);
5934 if (lseek64(d
->fd
, dsize
- sector_size
,
5936 if ((unsigned int)write(d
->fd
,
5937 super
->migr_rec_buf
,
5938 MIGR_REC_BUF_SECTORS
*sector_size
) !=
5939 MIGR_REC_BUF_SECTORS
*sector_size
)
5940 perror("Write migr_rec failed");
5944 if (store_imsm_mpb(d
->fd
, mpb
))
5946 "failed for device %d:%d (fd: %d)%s\n",
5948 d
->fd
, strerror(errno
));
5957 return write_super_imsm_spares(super
, doclose
);
5962 static int create_array(struct supertype
*st
, int dev_idx
)
5965 struct imsm_update_create_array
*u
;
5966 struct intel_super
*super
= st
->sb
;
5967 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
5968 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
5969 struct disk_info
*inf
;
5970 struct imsm_disk
*disk
;
5973 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
5974 sizeof(*inf
) * map
->num_members
;
5976 u
->type
= update_create_array
;
5977 u
->dev_idx
= dev_idx
;
5978 imsm_copy_dev(&u
->dev
, dev
);
5979 inf
= get_disk_info(u
);
5980 for (i
= 0; i
< map
->num_members
; i
++) {
5981 int idx
= get_imsm_disk_idx(dev
, i
, MAP_X
);
5983 disk
= get_imsm_disk(super
, idx
);
5985 disk
= get_imsm_missing(super
, idx
);
5986 serialcpy(inf
[i
].serial
, disk
->serial
);
5988 append_metadata_update(st
, u
, len
);
5993 static int mgmt_disk(struct supertype
*st
)
5995 struct intel_super
*super
= st
->sb
;
5997 struct imsm_update_add_remove_disk
*u
;
5999 if (!super
->disk_mgmt_list
)
6004 u
->type
= update_add_remove_disk
;
6005 append_metadata_update(st
, u
, len
);
6010 __u32
crc32c_le(__u32 crc
, unsigned char const *p
, size_t len
);
6012 static int write_init_ppl_imsm(struct supertype
*st
, struct mdinfo
*info
, int fd
)
6014 struct intel_super
*super
= st
->sb
;
6016 struct ppl_header
*ppl_hdr
;
6019 ret
= posix_memalign(&buf
, 4096, PPL_HEADER_SIZE
);
6021 pr_err("Failed to allocate PPL header buffer\n");
6025 memset(buf
, 0, PPL_HEADER_SIZE
);
6027 memset(ppl_hdr
->reserved
, 0xff, PPL_HDR_RESERVED
);
6028 ppl_hdr
->signature
= __cpu_to_le32(super
->anchor
->orig_family_num
);
6029 ppl_hdr
->checksum
= __cpu_to_le32(~crc32c_le(~0, buf
, PPL_HEADER_SIZE
));
6031 if (lseek64(fd
, info
->ppl_sector
* 512, SEEK_SET
) < 0) {
6033 perror("Failed to seek to PPL header location");
6036 if (!ret
&& write(fd
, buf
, PPL_HEADER_SIZE
) != PPL_HEADER_SIZE
) {
6038 perror("Write PPL header failed");
6048 static int validate_ppl_imsm(struct supertype
*st
, struct mdinfo
*info
,
6049 struct mdinfo
*disk
)
6051 struct intel_super
*super
= st
->sb
;
6055 struct ppl_header
*ppl_hdr
;
6057 struct imsm_dev
*dev
;
6058 struct imsm_map
*map
;
6061 if (disk
->disk
.raid_disk
< 0)
6064 if (posix_memalign(&buf
, 4096, PPL_HEADER_SIZE
)) {
6065 pr_err("Failed to allocate PPL header buffer\n");
6069 dev
= get_imsm_dev(super
, info
->container_member
);
6070 map
= get_imsm_map(dev
, MAP_X
);
6071 idx
= get_imsm_disk_idx(dev
, disk
->disk
.raid_disk
, MAP_X
);
6072 d
= get_imsm_dl_disk(super
, idx
);
6074 if (!d
|| d
->index
< 0 || is_failed(&d
->disk
))
6077 if (lseek64(d
->fd
, info
->ppl_sector
* 512, SEEK_SET
) < 0) {
6078 perror("Failed to seek to PPL header location");
6083 if (read(d
->fd
, buf
, PPL_HEADER_SIZE
) != PPL_HEADER_SIZE
) {
6084 perror("Read PPL header failed");
6091 crc
= __le32_to_cpu(ppl_hdr
->checksum
);
6092 ppl_hdr
->checksum
= 0;
6094 if (crc
!= ~crc32c_le(~0, buf
, PPL_HEADER_SIZE
)) {
6095 dprintf("Wrong PPL header checksum on %s\n",
6100 if (!ret
&& (__le32_to_cpu(ppl_hdr
->signature
) !=
6101 super
->anchor
->orig_family_num
)) {
6102 dprintf("Wrong PPL header signature on %s\n",
6110 if (ret
== 1 && map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
6111 return st
->ss
->write_init_ppl(st
, info
, d
->fd
);
6116 static int write_init_ppl_imsm_all(struct supertype
*st
, struct mdinfo
*info
)
6118 struct intel_super
*super
= st
->sb
;
6122 if (info
->consistency_policy
!= CONSISTENCY_POLICY_PPL
||
6123 info
->array
.level
!= 5)
6126 for (d
= super
->disks
; d
; d
= d
->next
) {
6127 if (d
->index
< 0 || is_failed(&d
->disk
))
6130 ret
= st
->ss
->write_init_ppl(st
, info
, d
->fd
);
6138 static int write_init_super_imsm(struct supertype
*st
)
6140 struct intel_super
*super
= st
->sb
;
6141 int current_vol
= super
->current_vol
;
6145 getinfo_super_imsm(st
, &info
, NULL
);
6147 /* we are done with current_vol reset it to point st at the container */
6148 super
->current_vol
= -1;
6150 if (st
->update_tail
) {
6151 /* queue the recently created array / added disk
6152 * as a metadata update */
6154 /* determine if we are creating a volume or adding a disk */
6155 if (current_vol
< 0) {
6156 /* in the mgmt (add/remove) disk case we are running
6157 * in mdmon context, so don't close fd's
6161 rv
= write_init_ppl_imsm_all(st
, &info
);
6163 rv
= create_array(st
, current_vol
);
6167 for (d
= super
->disks
; d
; d
= d
->next
)
6168 Kill(d
->devname
, NULL
, 0, -1, 1);
6169 if (current_vol
>= 0)
6170 rv
= write_init_ppl_imsm_all(st
, &info
);
6172 rv
= write_super_imsm(st
, 1);
6178 static int store_super_imsm(struct supertype
*st
, int fd
)
6180 struct intel_super
*super
= st
->sb
;
6181 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
6186 if (super
->sector_size
== 4096)
6187 convert_to_4k(super
);
6188 return store_imsm_mpb(fd
, mpb
);
6191 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
6192 int layout
, int raiddisks
, int chunk
,
6193 unsigned long long size
,
6194 unsigned long long data_offset
,
6196 unsigned long long *freesize
,
6200 unsigned long long ldsize
;
6201 struct intel_super
*super
;
6204 if (level
!= LEVEL_CONTAINER
)
6209 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
6212 pr_err("imsm: Cannot open %s: %s\n",
6213 dev
, strerror(errno
));
6216 if (!get_dev_size(fd
, dev
, &ldsize
)) {
6221 /* capabilities retrieve could be possible
6222 * note that there is no fd for the disks in array.
6224 super
= alloc_super();
6229 if (!get_dev_sector_size(fd
, NULL
, &super
->sector_size
)) {
6235 rv
= find_intel_hba_capability(fd
, super
, verbose
> 0 ? dev
: NULL
);
6239 fd2devname(fd
, str
);
6240 dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n",
6241 fd
, str
, super
->orom
, rv
, raiddisks
);
6243 /* no orom/efi or non-intel hba of the disk */
6250 if (raiddisks
> super
->orom
->tds
) {
6252 pr_err("%d exceeds maximum number of platform supported disks: %d\n",
6253 raiddisks
, super
->orom
->tds
);
6257 if ((super
->orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) == 0 &&
6258 (ldsize
>> 9) >> 32 > 0) {
6260 pr_err("%s exceeds maximum platform supported size\n", dev
);
6266 *freesize
= avail_size_imsm(st
, ldsize
>> 9, data_offset
);
6272 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
6274 const unsigned long long base_start
= e
[*idx
].start
;
6275 unsigned long long end
= base_start
+ e
[*idx
].size
;
6278 if (base_start
== end
)
6282 for (i
= *idx
; i
< num_extents
; i
++) {
6283 /* extend overlapping extents */
6284 if (e
[i
].start
>= base_start
&&
6285 e
[i
].start
<= end
) {
6288 if (e
[i
].start
+ e
[i
].size
> end
)
6289 end
= e
[i
].start
+ e
[i
].size
;
6290 } else if (e
[i
].start
> end
) {
6296 return end
- base_start
;
6299 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
6301 /* build a composite disk with all known extents and generate a new
6302 * 'maxsize' given the "all disks in an array must share a common start
6303 * offset" constraint
6305 struct extent
*e
= xcalloc(sum_extents
, sizeof(*e
));
6309 unsigned long long pos
;
6310 unsigned long long start
= 0;
6311 unsigned long long maxsize
;
6312 unsigned long reserve
;
6314 /* coalesce and sort all extents. also, check to see if we need to
6315 * reserve space between member arrays
6318 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6321 for (i
= 0; i
< dl
->extent_cnt
; i
++)
6324 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
6329 while (i
< sum_extents
) {
6330 e
[j
].start
= e
[i
].start
;
6331 e
[j
].size
= find_size(e
, &i
, sum_extents
);
6333 if (e
[j
-1].size
== 0)
6342 unsigned long long esize
;
6344 esize
= e
[i
].start
- pos
;
6345 if (esize
>= maxsize
) {
6350 pos
= e
[i
].start
+ e
[i
].size
;
6352 } while (e
[i
-1].size
);
6358 /* FIXME assumes volume at offset 0 is the first volume in a
6361 if (start_extent
> 0)
6362 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
6366 if (maxsize
< reserve
)
6369 super
->create_offset
= ~((unsigned long long) 0);
6370 if (start
+ reserve
> super
->create_offset
)
6371 return 0; /* start overflows create_offset */
6372 super
->create_offset
= start
+ reserve
;
6374 return maxsize
- reserve
;
6377 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
6379 if (level
< 0 || level
== 6 || level
== 4)
6382 /* if we have an orom prevent invalid raid levels */
6385 case 0: return imsm_orom_has_raid0(orom
);
6388 return imsm_orom_has_raid1e(orom
);
6389 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
6390 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
6391 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
6394 return 1; /* not on an Intel RAID platform so anything goes */
6400 active_arrays_by_format(char *name
, char* hba
, struct md_list
**devlist
,
6401 int dpa
, int verbose
)
6403 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
6404 struct mdstat_ent
*memb
;
6410 for (memb
= mdstat
; memb
; memb
= memb
->next
) {
6411 if (memb
->metadata_version
&&
6412 (strncmp(memb
->metadata_version
, "external:", 9) == 0) &&
6413 (strcmp(&memb
->metadata_version
[9], name
) == 0) &&
6414 !is_subarray(memb
->metadata_version
+9) &&
6416 struct dev_member
*dev
= memb
->members
;
6418 while(dev
&& (fd
< 0)) {
6419 char *path
= xmalloc(strlen(dev
->name
) + strlen("/dev/") + 1);
6420 num
= sprintf(path
, "%s%s", "/dev/", dev
->name
);
6422 fd
= open(path
, O_RDONLY
, 0);
6423 if (num
<= 0 || fd
< 0) {
6424 pr_vrb("Cannot open %s: %s\n",
6425 dev
->name
, strerror(errno
));
6431 if (fd
>= 0 && disk_attached_to_hba(fd
, hba
)) {
6432 struct mdstat_ent
*vol
;
6433 for (vol
= mdstat
; vol
; vol
= vol
->next
) {
6434 if (vol
->active
> 0 &&
6435 vol
->metadata_version
&&
6436 is_container_member(vol
, memb
->devnm
)) {
6441 if (*devlist
&& (found
< dpa
)) {
6442 dv
= xcalloc(1, sizeof(*dv
));
6443 dv
->devname
= xmalloc(strlen(memb
->devnm
) + strlen("/dev/") + 1);
6444 sprintf(dv
->devname
, "%s%s", "/dev/", memb
->devnm
);
6447 dv
->next
= *devlist
;
6455 free_mdstat(mdstat
);
6460 static struct md_list
*
6461 get_loop_devices(void)
6464 struct md_list
*devlist
= NULL
;
6467 for(i
= 0; i
< 12; i
++) {
6468 dv
= xcalloc(1, sizeof(*dv
));
6469 dv
->devname
= xmalloc(40);
6470 sprintf(dv
->devname
, "/dev/loop%d", i
);
6478 static struct md_list
*
6479 get_devices(const char *hba_path
)
6481 struct md_list
*devlist
= NULL
;
6488 devlist
= get_loop_devices();
6491 /* scroll through /sys/dev/block looking for devices attached to
6494 dir
= opendir("/sys/dev/block");
6495 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
6500 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
6502 path
= devt_to_devpath(makedev(major
, minor
));
6505 if (!path_attached_to_hba(path
, hba_path
)) {
6512 fd
= dev_open(ent
->d_name
, O_RDONLY
);
6514 fd2devname(fd
, buf
);
6517 pr_err("cannot open device: %s\n",
6522 dv
= xcalloc(1, sizeof(*dv
));
6523 dv
->devname
= xstrdup(buf
);
6530 devlist
= devlist
->next
;
6540 count_volumes_list(struct md_list
*devlist
, char *homehost
,
6541 int verbose
, int *found
)
6543 struct md_list
*tmpdev
;
6545 struct supertype
*st
;
6547 /* first walk the list of devices to find a consistent set
6548 * that match the criterea, if that is possible.
6549 * We flag the ones we like with 'used'.
6552 st
= match_metadata_desc_imsm("imsm");
6554 pr_vrb("cannot allocate memory for imsm supertype\n");
6558 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
6559 char *devname
= tmpdev
->devname
;
6561 struct supertype
*tst
;
6563 if (tmpdev
->used
> 1)
6565 tst
= dup_super(st
);
6567 pr_vrb("cannot allocate memory for imsm supertype\n");
6570 tmpdev
->container
= 0;
6571 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
6573 dprintf("cannot open device %s: %s\n",
6574 devname
, strerror(errno
));
6576 } else if (fstat(dfd
, &stb
)< 0) {
6578 dprintf("fstat failed for %s: %s\n",
6579 devname
, strerror(errno
));
6581 } else if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
6582 dprintf("%s is not a block device.\n",
6585 } else if (must_be_container(dfd
)) {
6586 struct supertype
*cst
;
6587 cst
= super_by_fd(dfd
, NULL
);
6589 dprintf("cannot recognize container type %s\n",
6592 } else if (tst
->ss
!= st
->ss
) {
6593 dprintf("non-imsm container - ignore it: %s\n",
6596 } else if (!tst
->ss
->load_container
||
6597 tst
->ss
->load_container(tst
, dfd
, NULL
))
6600 tmpdev
->container
= 1;
6603 cst
->ss
->free_super(cst
);
6605 tmpdev
->st_rdev
= stb
.st_rdev
;
6606 if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
6607 dprintf("no RAID superblock on %s\n",
6610 } else if (tst
->ss
->compare_super
== NULL
) {
6611 dprintf("Cannot assemble %s metadata on %s\n",
6612 tst
->ss
->name
, devname
);
6618 if (tmpdev
->used
== 2 || tmpdev
->used
== 4) {
6619 /* Ignore unrecognised devices during auto-assembly */
6624 tst
->ss
->getinfo_super(tst
, &info
, NULL
);
6626 if (st
->minor_version
== -1)
6627 st
->minor_version
= tst
->minor_version
;
6629 if (memcmp(info
.uuid
, uuid_zero
,
6630 sizeof(int[4])) == 0) {
6631 /* this is a floating spare. It cannot define
6632 * an array unless there are no more arrays of
6633 * this type to be found. It can be included
6634 * in an array of this type though.
6640 if (st
->ss
!= tst
->ss
||
6641 st
->minor_version
!= tst
->minor_version
||
6642 st
->ss
->compare_super(st
, tst
) != 0) {
6643 /* Some mismatch. If exactly one array matches this host,
6644 * we can resolve on that one.
6645 * Or, if we are auto assembling, we just ignore the second
6648 dprintf("superblock on %s doesn't match others - assembly aborted\n",
6654 dprintf("found: devname: %s\n", devname
);
6658 tst
->ss
->free_super(tst
);
6662 if ((err
= load_super_imsm_all(st
, -1, &st
->sb
, NULL
, devlist
, 0)) == 0) {
6663 struct mdinfo
*iter
, *head
= st
->ss
->container_content(st
, NULL
);
6664 for (iter
= head
; iter
; iter
= iter
->next
) {
6665 dprintf("content->text_version: %s vol\n",
6666 iter
->text_version
);
6667 if (iter
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
6668 /* do not assemble arrays with unsupported
6670 dprintf("Cannot activate member %s.\n",
6671 iter
->text_version
);
6678 dprintf("No valid super block on device list: err: %d %p\n",
6682 dprintf("no more devices to examine\n");
6685 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
6686 if (tmpdev
->used
== 1 && tmpdev
->found
) {
6688 if (count
< tmpdev
->found
)
6691 count
-= tmpdev
->found
;
6694 if (tmpdev
->used
== 1)
6699 st
->ss
->free_super(st
);
6703 static int __count_volumes(char *hba_path
, int dpa
, int verbose
,
6706 struct sys_dev
*idev
, *intel_devices
= find_intel_devices();
6708 const struct orom_entry
*entry
;
6709 struct devid_list
*dv
, *devid_list
;
6714 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
6715 if (strstr(idev
->path
, hba_path
))
6719 if (!idev
|| !idev
->dev_id
)
6722 entry
= get_orom_entry_by_device_id(idev
->dev_id
);
6724 if (!entry
|| !entry
->devid_list
)
6727 devid_list
= entry
->devid_list
;
6728 for (dv
= devid_list
; dv
; dv
= dv
->next
) {
6729 struct md_list
*devlist
;
6730 struct sys_dev
*device
= NULL
;
6735 device
= device_by_id_and_path(dv
->devid
, hba_path
);
6737 device
= device_by_id(dv
->devid
);
6740 hpath
= device
->path
;
6744 devlist
= get_devices(hpath
);
6745 /* if no intel devices return zero volumes */
6746 if (devlist
== NULL
)
6749 count
+= active_arrays_by_format("imsm", hpath
, &devlist
, dpa
,
6751 dprintf("path: %s active arrays: %d\n", hpath
, count
);
6752 if (devlist
== NULL
)
6756 count
+= count_volumes_list(devlist
,
6760 dprintf("found %d count: %d\n", found
, count
);
6763 dprintf("path: %s total number of volumes: %d\n", hpath
, count
);
6766 struct md_list
*dv
= devlist
;
6767 devlist
= devlist
->next
;
6775 static int count_volumes(struct intel_hba
*hba
, int dpa
, int verbose
)
6779 if (hba
->type
== SYS_DEV_VMD
) {
6780 struct sys_dev
*dev
;
6783 for (dev
= find_intel_devices(); dev
; dev
= dev
->next
) {
6784 if (dev
->type
== SYS_DEV_VMD
)
6785 count
+= __count_volumes(dev
->path
, dpa
,
6790 return __count_volumes(hba
->path
, dpa
, verbose
, 0);
6793 static int imsm_default_chunk(const struct imsm_orom
*orom
)
6795 /* up to 512 if the plaform supports it, otherwise the platform max.
6796 * 128 if no platform detected
6798 int fs
= max(7, orom
? fls(orom
->sss
) : 0);
6800 return min(512, (1 << fs
));
6804 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
6805 int raiddisks
, int *chunk
, unsigned long long size
, int verbose
)
6807 /* check/set platform and metadata limits/defaults */
6808 if (super
->orom
&& raiddisks
> super
->orom
->dpa
) {
6809 pr_vrb("platform supports a maximum of %d disks per array\n",
6814 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
6815 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
6816 pr_vrb("platform does not support raid%d with %d disk%s\n",
6817 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
6821 if (*chunk
== 0 || *chunk
== UnSet
)
6822 *chunk
= imsm_default_chunk(super
->orom
);
6824 if (super
->orom
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
6825 pr_vrb("platform does not support a chunk size of: %d\n", *chunk
);
6829 if (layout
!= imsm_level_to_layout(level
)) {
6831 pr_vrb("imsm raid 5 only supports the left-asymmetric layout\n");
6832 else if (level
== 10)
6833 pr_vrb("imsm raid 10 only supports the n2 layout\n");
6835 pr_vrb("imsm unknown layout %#x for this raid level %d\n",
6840 if (super
->orom
&& (super
->orom
->attr
& IMSM_OROM_ATTR_2TB
) == 0 &&
6841 (calc_array_size(level
, raiddisks
, layout
, *chunk
, size
) >> 32) > 0) {
6842 pr_vrb("platform does not support a volume size over 2TB\n");
6849 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
6850 * FIX ME add ahci details
6852 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
6853 int layout
, int raiddisks
, int *chunk
,
6854 unsigned long long size
,
6855 unsigned long long data_offset
,
6857 unsigned long long *freesize
,
6861 struct intel_super
*super
= st
->sb
;
6862 struct imsm_super
*mpb
;
6864 unsigned long long pos
= 0;
6865 unsigned long long maxsize
;
6869 /* We must have the container info already read in. */
6873 mpb
= super
->anchor
;
6875 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, size
, verbose
)) {
6876 pr_err("RAID gemetry validation failed. Cannot proceed with the action(s).\n");
6880 /* General test: make sure there is space for
6881 * 'raiddisks' device extents of size 'size' at a given
6884 unsigned long long minsize
= size
;
6885 unsigned long long start_offset
= MaxSector
;
6888 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
6889 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6894 e
= get_extents(super
, dl
);
6897 unsigned long long esize
;
6898 esize
= e
[i
].start
- pos
;
6899 if (esize
>= minsize
)
6901 if (found
&& start_offset
== MaxSector
) {
6904 } else if (found
&& pos
!= start_offset
) {
6908 pos
= e
[i
].start
+ e
[i
].size
;
6910 } while (e
[i
-1].size
);
6915 if (dcnt
< raiddisks
) {
6917 pr_err("imsm: Not enough devices with space for this array (%d < %d)\n",
6924 /* This device must be a member of the set */
6925 if (stat(dev
, &stb
) < 0)
6927 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
6929 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6930 if (dl
->major
== (int)major(stb
.st_rdev
) &&
6931 dl
->minor
== (int)minor(stb
.st_rdev
))
6936 pr_err("%s is not in the same imsm set\n", dev
);
6938 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
6939 /* If a volume is present then the current creation attempt
6940 * cannot incorporate new spares because the orom may not
6941 * understand this configuration (all member disks must be
6942 * members of each array in the container).
6944 pr_err("%s is a spare and a volume is already defined for this container\n", dev
);
6945 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
6947 } else if (super
->orom
&& mpb
->num_raid_devs
> 0 &&
6948 mpb
->num_disks
!= raiddisks
) {
6949 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
6953 /* retrieve the largest free space block */
6954 e
= get_extents(super
, dl
);
6959 unsigned long long esize
;
6961 esize
= e
[i
].start
- pos
;
6962 if (esize
>= maxsize
)
6964 pos
= e
[i
].start
+ e
[i
].size
;
6966 } while (e
[i
-1].size
);
6971 pr_err("unable to determine free space for: %s\n",
6975 if (maxsize
< size
) {
6977 pr_err("%s not enough space (%llu < %llu)\n",
6978 dev
, maxsize
, size
);
6982 /* count total number of extents for merge */
6984 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6986 i
+= dl
->extent_cnt
;
6988 maxsize
= merge_extents(super
, i
);
6990 if (!check_env("IMSM_NO_PLATFORM") &&
6991 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6992 pr_err("attempting to create a second volume with size less then remaining space. Aborting...\n");
6996 if (maxsize
< size
|| maxsize
== 0) {
6999 pr_err("no free space left on device. Aborting...\n");
7001 pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n",
7007 *freesize
= maxsize
;
7010 int count
= count_volumes(super
->hba
,
7011 super
->orom
->dpa
, verbose
);
7012 if (super
->orom
->vphba
<= count
) {
7013 pr_vrb("platform does not support more than %d raid volumes.\n",
7014 super
->orom
->vphba
);
7021 static int imsm_get_free_size(struct supertype
*st
, int raiddisks
,
7022 unsigned long long size
, int chunk
,
7023 unsigned long long *freesize
)
7025 struct intel_super
*super
= st
->sb
;
7026 struct imsm_super
*mpb
= super
->anchor
;
7031 unsigned long long maxsize
;
7032 unsigned long long minsize
;
7036 /* find the largest common start free region of the possible disks */
7040 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7046 /* don't activate new spares if we are orom constrained
7047 * and there is already a volume active in the container
7049 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
7052 e
= get_extents(super
, dl
);
7055 for (i
= 1; e
[i
-1].size
; i
++)
7063 maxsize
= merge_extents(super
, extent_cnt
);
7067 minsize
= chunk
* 2;
7069 if (cnt
< raiddisks
||
7070 (super
->orom
&& used
&& used
!= raiddisks
) ||
7071 maxsize
< minsize
||
7073 pr_err("not enough devices with space to create array.\n");
7074 return 0; /* No enough free spaces large enough */
7085 if (!check_env("IMSM_NO_PLATFORM") &&
7086 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
7087 pr_err("attempting to create a second volume with size less then remaining space. Aborting...\n");
7091 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7093 dl
->raiddisk
= cnt
++;
7097 dprintf("imsm: imsm_get_free_size() returns : %llu\n", size
);
7102 static int reserve_space(struct supertype
*st
, int raiddisks
,
7103 unsigned long long size
, int chunk
,
7104 unsigned long long *freesize
)
7106 struct intel_super
*super
= st
->sb
;
7111 rv
= imsm_get_free_size(st
, raiddisks
, size
, chunk
, freesize
);
7114 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7116 dl
->raiddisk
= cnt
++;
7123 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
7124 int raiddisks
, int *chunk
, unsigned long long size
,
7125 unsigned long long data_offset
,
7126 char *dev
, unsigned long long *freesize
,
7127 int consistency_policy
, int verbose
)
7134 * if given unused devices create a container
7135 * if given given devices in a container create a member volume
7137 if (level
== LEVEL_CONTAINER
) {
7138 /* Must be a fresh device to add to a container */
7139 return validate_geometry_imsm_container(st
, level
, layout
,
7149 struct intel_super
*super
= st
->sb
;
7150 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
7151 raiddisks
, chunk
, size
,
7154 /* we are being asked to automatically layout a
7155 * new volume based on the current contents of
7156 * the container. If the the parameters can be
7157 * satisfied reserve_space will record the disks,
7158 * start offset, and size of the volume to be
7159 * created. add_to_super and getinfo_super
7160 * detect when autolayout is in progress.
7162 /* assuming that freesize is always given when array is
7164 if (super
->orom
&& freesize
) {
7166 count
= count_volumes(super
->hba
,
7167 super
->orom
->dpa
, verbose
);
7168 if (super
->orom
->vphba
<= count
) {
7169 pr_vrb("platform does not support more than %d raid volumes.\n",
7170 super
->orom
->vphba
);
7175 return reserve_space(st
, raiddisks
, size
,
7181 /* creating in a given container */
7182 return validate_geometry_imsm_volume(st
, level
, layout
,
7183 raiddisks
, chunk
, size
,
7185 dev
, freesize
, verbose
);
7188 /* This device needs to be a device in an 'imsm' container */
7189 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
7192 pr_err("Cannot create this array on device %s\n",
7197 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
7199 pr_err("Cannot open %s: %s\n",
7200 dev
, strerror(errno
));
7203 /* Well, it is in use by someone, maybe an 'imsm' container. */
7204 cfd
= open_container(fd
);
7208 pr_err("Cannot use %s: It is busy\n",
7212 sra
= sysfs_read(cfd
, NULL
, GET_VERSION
);
7213 if (sra
&& sra
->array
.major_version
== -1 &&
7214 strcmp(sra
->text_version
, "imsm") == 0)
7218 /* This is a member of a imsm container. Load the container
7219 * and try to create a volume
7221 struct intel_super
*super
;
7223 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
, NULL
, 1) == 0) {
7225 strcpy(st
->container_devnm
, fd2devnm(cfd
));
7227 return validate_geometry_imsm_volume(st
, level
, layout
,
7229 size
, data_offset
, dev
,
7236 pr_err("failed container membership check\n");
7242 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
7244 struct intel_super
*super
= st
->sb
;
7246 if (level
&& *level
== UnSet
)
7247 *level
= LEVEL_CONTAINER
;
7249 if (level
&& layout
&& *layout
== UnSet
)
7250 *layout
= imsm_level_to_layout(*level
);
7252 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0))
7253 *chunk
= imsm_default_chunk(super
->orom
);
7256 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
7258 static int kill_subarray_imsm(struct supertype
*st
)
7260 /* remove the subarray currently referenced by ->current_vol */
7262 struct intel_dev
**dp
;
7263 struct intel_super
*super
= st
->sb
;
7264 __u8 current_vol
= super
->current_vol
;
7265 struct imsm_super
*mpb
= super
->anchor
;
7267 if (super
->current_vol
< 0)
7269 super
->current_vol
= -1; /* invalidate subarray cursor */
7271 /* block deletions that would change the uuid of active subarrays
7273 * FIXME when immutable ids are available, but note that we'll
7274 * also need to fixup the invalidated/active subarray indexes in
7277 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7280 if (i
< current_vol
)
7282 sprintf(subarray
, "%u", i
);
7283 if (is_subarray_active(subarray
, st
->devnm
)) {
7284 pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
7291 if (st
->update_tail
) {
7292 struct imsm_update_kill_array
*u
= xmalloc(sizeof(*u
));
7294 u
->type
= update_kill_array
;
7295 u
->dev_idx
= current_vol
;
7296 append_metadata_update(st
, u
, sizeof(*u
));
7301 for (dp
= &super
->devlist
; *dp
;)
7302 if ((*dp
)->index
== current_vol
) {
7305 handle_missing(super
, (*dp
)->dev
);
7306 if ((*dp
)->index
> current_vol
)
7311 /* no more raid devices, all active components are now spares,
7312 * but of course failed are still failed
7314 if (--mpb
->num_raid_devs
== 0) {
7317 for (d
= super
->disks
; d
; d
= d
->next
)
7322 super
->updates_pending
++;
7327 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
7328 char *update
, struct mddev_ident
*ident
)
7330 /* update the subarray currently referenced by ->current_vol */
7331 struct intel_super
*super
= st
->sb
;
7332 struct imsm_super
*mpb
= super
->anchor
;
7334 if (strcmp(update
, "name") == 0) {
7335 char *name
= ident
->name
;
7339 if (is_subarray_active(subarray
, st
->devnm
)) {
7340 pr_err("Unable to update name of active subarray\n");
7344 if (!check_name(super
, name
, 0))
7347 vol
= strtoul(subarray
, &ep
, 10);
7348 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
7351 if (st
->update_tail
) {
7352 struct imsm_update_rename_array
*u
= xmalloc(sizeof(*u
));
7354 u
->type
= update_rename_array
;
7356 strncpy((char *) u
->name
, name
, MAX_RAID_SERIAL_LEN
);
7357 u
->name
[MAX_RAID_SERIAL_LEN
-1] = '\0';
7358 append_metadata_update(st
, u
, sizeof(*u
));
7360 struct imsm_dev
*dev
;
7363 dev
= get_imsm_dev(super
, vol
);
7364 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
7365 dev
->volume
[MAX_RAID_SERIAL_LEN
-1] = '\0';
7366 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7367 dev
= get_imsm_dev(super
, i
);
7368 handle_missing(super
, dev
);
7370 super
->updates_pending
++;
7372 } else if (strcmp(update
, "ppl") == 0 ||
7373 strcmp(update
, "no-ppl") == 0) {
7376 int vol
= strtoul(subarray
, &ep
, 10);
7378 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
7381 if (strcmp(update
, "ppl") == 0)
7382 new_policy
= RWH_DISTRIBUTED
;
7384 new_policy
= RWH_OFF
;
7386 if (st
->update_tail
) {
7387 struct imsm_update_rwh_policy
*u
= xmalloc(sizeof(*u
));
7389 u
->type
= update_rwh_policy
;
7391 u
->new_policy
= new_policy
;
7392 append_metadata_update(st
, u
, sizeof(*u
));
7394 struct imsm_dev
*dev
;
7396 dev
= get_imsm_dev(super
, vol
);
7397 dev
->rwh_policy
= new_policy
;
7398 super
->updates_pending
++;
7406 static int is_gen_migration(struct imsm_dev
*dev
)
7411 if (!dev
->vol
.migr_state
)
7414 if (migr_type(dev
) == MIGR_GEN_MIGR
)
7420 static int is_rebuilding(struct imsm_dev
*dev
)
7422 struct imsm_map
*migr_map
;
7424 if (!dev
->vol
.migr_state
)
7427 if (migr_type(dev
) != MIGR_REBUILD
)
7430 migr_map
= get_imsm_map(dev
, MAP_1
);
7432 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
7438 static int is_initializing(struct imsm_dev
*dev
)
7440 struct imsm_map
*migr_map
;
7442 if (!dev
->vol
.migr_state
)
7445 if (migr_type(dev
) != MIGR_INIT
)
7448 migr_map
= get_imsm_map(dev
, MAP_1
);
7450 if (migr_map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
7456 static void update_recovery_start(struct intel_super
*super
,
7457 struct imsm_dev
*dev
,
7458 struct mdinfo
*array
)
7460 struct mdinfo
*rebuild
= NULL
;
7464 if (!is_rebuilding(dev
))
7467 /* Find the rebuild target, but punt on the dual rebuild case */
7468 for (d
= array
->devs
; d
; d
= d
->next
)
7469 if (d
->recovery_start
== 0) {
7476 /* (?) none of the disks are marked with
7477 * IMSM_ORD_REBUILD, so assume they are missing and the
7478 * disk_ord_tbl was not correctly updated
7480 dprintf("failed to locate out-of-sync disk\n");
7484 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
7485 rebuild
->recovery_start
= units
* blocks_per_migr_unit(super
, dev
);
7488 static int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
);
7490 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
7492 /* Given a container loaded by load_super_imsm_all,
7493 * extract information about all the arrays into
7495 * If 'subarray' is given, just extract info about that array.
7497 * For each imsm_dev create an mdinfo, fill it in,
7498 * then look for matching devices in super->disks
7499 * and create appropriate device mdinfo.
7501 struct intel_super
*super
= st
->sb
;
7502 struct imsm_super
*mpb
= super
->anchor
;
7503 struct mdinfo
*rest
= NULL
;
7507 int spare_disks
= 0;
7509 /* do not assemble arrays when not all attributes are supported */
7510 if (imsm_check_attributes(mpb
->attributes
) == 0) {
7512 pr_err("Unsupported attributes in IMSM metadata.Arrays activation is blocked.\n");
7515 /* count spare devices, not used in maps
7517 for (d
= super
->disks
; d
; d
= d
->next
)
7521 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7522 struct imsm_dev
*dev
;
7523 struct imsm_map
*map
;
7524 struct imsm_map
*map2
;
7525 struct mdinfo
*this;
7531 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
7534 dev
= get_imsm_dev(super
, i
);
7535 map
= get_imsm_map(dev
, MAP_0
);
7536 map2
= get_imsm_map(dev
, MAP_1
);
7538 /* do not publish arrays that are in the middle of an
7539 * unsupported migration
7541 if (dev
->vol
.migr_state
&&
7542 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
7543 pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n",
7547 /* do not publish arrays that are not support by controller's
7551 this = xmalloc(sizeof(*this));
7553 super
->current_vol
= i
;
7554 getinfo_super_imsm_volume(st
, this, NULL
);
7556 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
7557 /* mdadm does not support all metadata features- set the bit in all arrays state */
7558 if (!validate_geometry_imsm_orom(super
,
7559 get_imsm_raid_level(map
), /* RAID level */
7560 imsm_level_to_layout(get_imsm_raid_level(map
)),
7561 map
->num_members
, /* raid disks */
7562 &chunk
, join_u32(dev
->size_low
, dev
->size_high
),
7564 pr_err("IMSM RAID geometry validation failed. Array %s activation is blocked.\n",
7566 this->array
.state
|=
7567 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
7568 (1<<MD_SB_BLOCK_VOLUME
);
7571 /* if array has bad blocks, set suitable bit in all arrays state */
7573 this->array
.state
|=
7574 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
7575 (1<<MD_SB_BLOCK_VOLUME
);
7577 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
7578 unsigned long long recovery_start
;
7579 struct mdinfo
*info_d
;
7586 idx
= get_imsm_disk_idx(dev
, slot
, MAP_0
);
7587 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
7588 for (d
= super
->disks
; d
; d
= d
->next
)
7589 if (d
->index
== idx
)
7592 recovery_start
= MaxSector
;
7595 if (d
&& is_failed(&d
->disk
))
7597 if (ord
& IMSM_ORD_REBUILD
)
7601 * if we skip some disks the array will be assmebled degraded;
7602 * reset resync start to avoid a dirty-degraded
7603 * situation when performing the intial sync
7605 * FIXME handle dirty degraded
7607 if ((skip
|| recovery_start
== 0) &&
7608 !(dev
->vol
.dirty
& RAIDVOL_DIRTY
))
7609 this->resync_start
= MaxSector
;
7613 info_d
= xcalloc(1, sizeof(*info_d
));
7614 info_d
->next
= this->devs
;
7615 this->devs
= info_d
;
7617 info_d
->disk
.number
= d
->index
;
7618 info_d
->disk
.major
= d
->major
;
7619 info_d
->disk
.minor
= d
->minor
;
7620 info_d
->disk
.raid_disk
= slot
;
7621 info_d
->recovery_start
= recovery_start
;
7623 if (slot
< map2
->num_members
)
7624 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
7626 this->array
.spare_disks
++;
7628 if (slot
< map
->num_members
)
7629 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
7631 this->array
.spare_disks
++;
7633 if (info_d
->recovery_start
== MaxSector
)
7634 this->array
.working_disks
++;
7636 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
7637 info_d
->data_offset
= pba_of_lba0(map
);
7639 if (map
->raid_level
== 5) {
7640 info_d
->component_size
=
7641 num_data_stripes(map
) *
7642 map
->blocks_per_strip
;
7643 info_d
->ppl_sector
= this->ppl_sector
;
7644 info_d
->ppl_size
= this->ppl_size
;
7646 info_d
->component_size
= blocks_per_member(map
);
7648 info_d
->consistency_policy
= this->consistency_policy
;
7650 info_d
->bb
.supported
= 1;
7651 get_volume_badblocks(super
->bbm_log
, ord_to_idx(ord
),
7652 info_d
->data_offset
,
7653 info_d
->component_size
,
7656 /* now that the disk list is up-to-date fixup recovery_start */
7657 update_recovery_start(super
, dev
, this);
7658 this->array
.spare_disks
+= spare_disks
;
7660 /* check for reshape */
7661 if (this->reshape_active
== 1)
7662 recover_backup_imsm(st
, this);
7669 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
7670 int failed
, int look_in_map
)
7672 struct imsm_map
*map
;
7674 map
= get_imsm_map(dev
, look_in_map
);
7677 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
7678 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
7680 switch (get_imsm_raid_level(map
)) {
7682 return IMSM_T_STATE_FAILED
;
7685 if (failed
< map
->num_members
)
7686 return IMSM_T_STATE_DEGRADED
;
7688 return IMSM_T_STATE_FAILED
;
7693 * check to see if any mirrors have failed, otherwise we
7694 * are degraded. Even numbered slots are mirrored on
7698 /* gcc -Os complains that this is unused */
7699 int insync
= insync
;
7701 for (i
= 0; i
< map
->num_members
; i
++) {
7702 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_X
);
7703 int idx
= ord_to_idx(ord
);
7704 struct imsm_disk
*disk
;
7706 /* reset the potential in-sync count on even-numbered
7707 * slots. num_copies is always 2 for imsm raid10
7712 disk
= get_imsm_disk(super
, idx
);
7713 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
7716 /* no in-sync disks left in this mirror the
7720 return IMSM_T_STATE_FAILED
;
7723 return IMSM_T_STATE_DEGRADED
;
7727 return IMSM_T_STATE_DEGRADED
;
7729 return IMSM_T_STATE_FAILED
;
7735 return map
->map_state
;
7738 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
7743 struct imsm_disk
*disk
;
7744 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7745 struct imsm_map
*prev
= get_imsm_map(dev
, MAP_1
);
7746 struct imsm_map
*map_for_loop
;
7751 /* at the beginning of migration we set IMSM_ORD_REBUILD on
7752 * disks that are being rebuilt. New failures are recorded to
7753 * map[0]. So we look through all the disks we started with and
7754 * see if any failures are still present, or if any new ones
7758 if (prev
&& (map
->num_members
< prev
->num_members
))
7759 map_for_loop
= prev
;
7761 for (i
= 0; i
< map_for_loop
->num_members
; i
++) {
7763 /* when MAP_X is passed both maps failures are counted
7766 (look_in_map
== MAP_1
|| look_in_map
== MAP_X
) &&
7767 i
< prev
->num_members
) {
7768 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
7769 idx_1
= ord_to_idx(ord
);
7771 disk
= get_imsm_disk(super
, idx_1
);
7772 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
7775 if ((look_in_map
== MAP_0
|| look_in_map
== MAP_X
) &&
7776 i
< map
->num_members
) {
7777 ord
= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
7778 idx
= ord_to_idx(ord
);
7781 disk
= get_imsm_disk(super
, idx
);
7782 if (!disk
|| is_failed(disk
) ||
7783 ord
& IMSM_ORD_REBUILD
)
7792 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
7795 struct intel_super
*super
= c
->sb
;
7796 struct imsm_super
*mpb
= super
->anchor
;
7797 struct imsm_update_prealloc_bb_mem u
;
7799 if (atoi(inst
) >= mpb
->num_raid_devs
) {
7800 pr_err("subarry index %d, out of range\n", atoi(inst
));
7804 dprintf("imsm: open_new %s\n", inst
);
7805 a
->info
.container_member
= atoi(inst
);
7807 u
.type
= update_prealloc_badblocks_mem
;
7808 imsm_update_metadata_locally(c
, &u
, sizeof(u
));
7813 static int is_resyncing(struct imsm_dev
*dev
)
7815 struct imsm_map
*migr_map
;
7817 if (!dev
->vol
.migr_state
)
7820 if (migr_type(dev
) == MIGR_INIT
||
7821 migr_type(dev
) == MIGR_REPAIR
)
7824 if (migr_type(dev
) == MIGR_GEN_MIGR
)
7827 migr_map
= get_imsm_map(dev
, MAP_1
);
7829 if (migr_map
->map_state
== IMSM_T_STATE_NORMAL
&&
7830 dev
->vol
.migr_type
!= MIGR_GEN_MIGR
)
7836 /* return true if we recorded new information */
7837 static int mark_failure(struct intel_super
*super
,
7838 struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
7842 struct imsm_map
*map
;
7843 char buf
[MAX_RAID_SERIAL_LEN
+3];
7844 unsigned int len
, shift
= 0;
7846 /* new failures are always set in map[0] */
7847 map
= get_imsm_map(dev
, MAP_0
);
7849 slot
= get_imsm_disk_slot(map
, idx
);
7853 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
7854 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
7857 memcpy(buf
, disk
->serial
, MAX_RAID_SERIAL_LEN
);
7858 buf
[MAX_RAID_SERIAL_LEN
] = '\000';
7860 if ((len
= strlen(buf
)) >= MAX_RAID_SERIAL_LEN
)
7861 shift
= len
- MAX_RAID_SERIAL_LEN
+ 1;
7862 strncpy((char *)disk
->serial
, &buf
[shift
], MAX_RAID_SERIAL_LEN
);
7864 disk
->status
|= FAILED_DISK
;
7865 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
7866 /* mark failures in second map if second map exists and this disk
7868 * This is valid for migration, initialization and rebuild
7870 if (dev
->vol
.migr_state
) {
7871 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
7872 int slot2
= get_imsm_disk_slot(map2
, idx
);
7874 if (slot2
< map2
->num_members
&& slot2
>= 0)
7875 set_imsm_ord_tbl_ent(map2
, slot2
,
7876 idx
| IMSM_ORD_REBUILD
);
7878 if (map
->failed_disk_num
== 0xff)
7879 map
->failed_disk_num
= slot
;
7881 clear_disk_badblocks(super
->bbm_log
, ord_to_idx(ord
));
7886 static void mark_missing(struct intel_super
*super
,
7887 struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
7889 mark_failure(super
, dev
, disk
, idx
);
7891 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
7894 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
7895 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
7898 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
7902 if (!super
->missing
)
7905 /* When orom adds replacement for missing disk it does
7906 * not remove entry of missing disk, but just updates map with
7907 * new added disk. So it is not enough just to test if there is
7908 * any missing disk, we have to look if there are any failed disks
7909 * in map to stop migration */
7911 dprintf("imsm: mark missing\n");
7912 /* end process for initialization and rebuild only
7914 if (is_gen_migration(dev
) == 0) {
7918 failed
= imsm_count_failed(super
, dev
, MAP_0
);
7919 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7922 end_migration(dev
, super
, map_state
);
7924 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
7925 mark_missing(super
, dev
, &dl
->disk
, dl
->index
);
7926 super
->updates_pending
++;
7929 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
,
7932 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
7933 unsigned long long array_blocks
;
7934 struct imsm_map
*map
;
7936 if (used_disks
== 0) {
7937 /* when problems occures
7938 * return current array_blocks value
7940 array_blocks
= __le32_to_cpu(dev
->size_high
);
7941 array_blocks
= array_blocks
<< 32;
7942 array_blocks
+= __le32_to_cpu(dev
->size_low
);
7944 return array_blocks
;
7947 /* set array size in metadata
7949 if (new_size
<= 0) {
7950 /* OLCE size change is caused by added disks
7952 map
= get_imsm_map(dev
, MAP_0
);
7953 array_blocks
= blocks_per_member(map
) * used_disks
;
7955 /* Online Volume Size Change
7956 * Using available free space
7958 array_blocks
= new_size
;
7961 array_blocks
= round_size_to_mb(array_blocks
, used_disks
);
7962 dev
->size_low
= __cpu_to_le32((__u32
)array_blocks
);
7963 dev
->size_high
= __cpu_to_le32((__u32
)(array_blocks
>> 32));
7965 return array_blocks
;
7968 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
7970 static void imsm_progress_container_reshape(struct intel_super
*super
)
7972 /* if no device has a migr_state, but some device has a
7973 * different number of members than the previous device, start
7974 * changing the number of devices in this device to match
7977 struct imsm_super
*mpb
= super
->anchor
;
7978 int prev_disks
= -1;
7982 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7983 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
7984 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7985 struct imsm_map
*map2
;
7986 int prev_num_members
;
7988 if (dev
->vol
.migr_state
)
7991 if (prev_disks
== -1)
7992 prev_disks
= map
->num_members
;
7993 if (prev_disks
== map
->num_members
)
7996 /* OK, this array needs to enter reshape mode.
7997 * i.e it needs a migr_state
8000 copy_map_size
= sizeof_imsm_map(map
);
8001 prev_num_members
= map
->num_members
;
8002 map
->num_members
= prev_disks
;
8003 dev
->vol
.migr_state
= 1;
8004 dev
->vol
.curr_migr_unit
= 0;
8005 set_migr_type(dev
, MIGR_GEN_MIGR
);
8006 for (i
= prev_num_members
;
8007 i
< map
->num_members
; i
++)
8008 set_imsm_ord_tbl_ent(map
, i
, i
);
8009 map2
= get_imsm_map(dev
, MAP_1
);
8010 /* Copy the current map */
8011 memcpy(map2
, map
, copy_map_size
);
8012 map2
->num_members
= prev_num_members
;
8014 imsm_set_array_size(dev
, -1);
8015 super
->clean_migration_record_by_mdmon
= 1;
8016 super
->updates_pending
++;
8020 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
8021 * states are handled in imsm_set_disk() with one exception, when a
8022 * resync is stopped due to a new failure this routine will set the
8023 * 'degraded' state for the array.
8025 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
8027 int inst
= a
->info
.container_member
;
8028 struct intel_super
*super
= a
->container
->sb
;
8029 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8030 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8031 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
8032 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8033 __u32 blocks_per_unit
;
8035 if (dev
->vol
.migr_state
&&
8036 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
8037 /* array state change is blocked due to reshape action
8039 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
8040 * - finish the reshape (if last_checkpoint is big and action != reshape)
8041 * - update curr_migr_unit
8043 if (a
->curr_action
== reshape
) {
8044 /* still reshaping, maybe update curr_migr_unit */
8045 goto mark_checkpoint
;
8047 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
8048 /* for some reason we aborted the reshape.
8050 * disable automatic metadata rollback
8051 * user action is required to recover process
8054 struct imsm_map
*map2
=
8055 get_imsm_map(dev
, MAP_1
);
8056 dev
->vol
.migr_state
= 0;
8057 set_migr_type(dev
, 0);
8058 dev
->vol
.curr_migr_unit
= 0;
8060 sizeof_imsm_map(map2
));
8061 super
->updates_pending
++;
8064 if (a
->last_checkpoint
>= a
->info
.component_size
) {
8065 unsigned long long array_blocks
;
8069 used_disks
= imsm_num_data_members(dev
, MAP_0
);
8070 if (used_disks
> 0) {
8072 blocks_per_member(map
) *
8075 round_size_to_mb(array_blocks
,
8077 a
->info
.custom_array_size
= array_blocks
;
8078 /* encourage manager to update array
8082 a
->check_reshape
= 1;
8084 /* finalize online capacity expansion/reshape */
8085 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
8087 mdi
->disk
.raid_disk
,
8090 imsm_progress_container_reshape(super
);
8095 /* before we activate this array handle any missing disks */
8096 if (consistent
== 2)
8097 handle_missing(super
, dev
);
8099 if (consistent
== 2 &&
8100 (!is_resync_complete(&a
->info
) ||
8101 map_state
!= IMSM_T_STATE_NORMAL
||
8102 dev
->vol
.migr_state
))
8105 if (is_resync_complete(&a
->info
)) {
8106 /* complete intialization / resync,
8107 * recovery and interrupted recovery is completed in
8110 if (is_resyncing(dev
)) {
8111 dprintf("imsm: mark resync done\n");
8112 end_migration(dev
, super
, map_state
);
8113 super
->updates_pending
++;
8114 a
->last_checkpoint
= 0;
8116 } else if ((!is_resyncing(dev
) && !failed
) &&
8117 (imsm_reshape_blocks_arrays_changes(super
) == 0)) {
8118 /* mark the start of the init process if nothing is failed */
8119 dprintf("imsm: mark resync start\n");
8120 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
8121 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
8123 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
8124 super
->updates_pending
++;
8128 /* skip checkpointing for general migration,
8129 * it is controlled in mdadm
8131 if (is_gen_migration(dev
))
8132 goto skip_mark_checkpoint
;
8134 /* check if we can update curr_migr_unit from resync_start, recovery_start */
8135 blocks_per_unit
= blocks_per_migr_unit(super
, dev
);
8136 if (blocks_per_unit
) {
8140 units
= a
->last_checkpoint
/ blocks_per_unit
;
8143 /* check that we did not overflow 32-bits, and that
8144 * curr_migr_unit needs updating
8146 if (units32
== units
&&
8148 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
8149 dprintf("imsm: mark checkpoint (%u)\n", units32
);
8150 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
8151 super
->updates_pending
++;
8155 skip_mark_checkpoint
:
8156 /* mark dirty / clean */
8157 if (((dev
->vol
.dirty
& RAIDVOL_DIRTY
) && consistent
) ||
8158 (!(dev
->vol
.dirty
& RAIDVOL_DIRTY
) && !consistent
)) {
8159 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
8161 dev
->vol
.dirty
= RAIDVOL_CLEAN
;
8163 dev
->vol
.dirty
= RAIDVOL_DIRTY
;
8164 if (dev
->rwh_policy
== RWH_DISTRIBUTED
)
8165 dev
->vol
.dirty
|= RAIDVOL_DSRECORD_VALID
;
8167 super
->updates_pending
++;
8173 static int imsm_disk_slot_to_ord(struct active_array
*a
, int slot
)
8175 int inst
= a
->info
.container_member
;
8176 struct intel_super
*super
= a
->container
->sb
;
8177 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8178 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8180 if (slot
> map
->num_members
) {
8181 pr_err("imsm: imsm_disk_slot_to_ord %d out of range 0..%d\n",
8182 slot
, map
->num_members
- 1);
8189 return get_imsm_ord_tbl_ent(dev
, slot
, MAP_0
);
8192 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
8194 int inst
= a
->info
.container_member
;
8195 struct intel_super
*super
= a
->container
->sb
;
8196 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8197 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8198 struct imsm_disk
*disk
;
8200 int recovery_not_finished
= 0;
8205 ord
= imsm_disk_slot_to_ord(a
, n
);
8209 dprintf("imsm: set_disk %d:%x\n", n
, state
);
8210 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
8212 /* check for new failures */
8213 if (state
& DS_FAULTY
) {
8214 if (mark_failure(super
, dev
, disk
, ord_to_idx(ord
)))
8215 super
->updates_pending
++;
8218 /* check if in_sync */
8219 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
8220 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
8222 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
8223 super
->updates_pending
++;
8226 failed
= imsm_count_failed(super
, dev
, MAP_0
);
8227 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8229 /* check if recovery complete, newly degraded, or failed */
8230 dprintf("imsm: Detected transition to state ");
8231 switch (map_state
) {
8232 case IMSM_T_STATE_NORMAL
: /* transition to normal state */
8233 dprintf("normal: ");
8234 if (is_rebuilding(dev
)) {
8235 dprintf_cont("while rebuilding");
8236 /* check if recovery is really finished */
8237 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
8238 if (mdi
->recovery_start
!= MaxSector
) {
8239 recovery_not_finished
= 1;
8242 if (recovery_not_finished
) {
8244 dprintf("Rebuild has not finished yet, state not changed");
8245 if (a
->last_checkpoint
< mdi
->recovery_start
) {
8246 a
->last_checkpoint
= mdi
->recovery_start
;
8247 super
->updates_pending
++;
8251 end_migration(dev
, super
, map_state
);
8252 map
= get_imsm_map(dev
, MAP_0
);
8253 map
->failed_disk_num
= ~0;
8254 super
->updates_pending
++;
8255 a
->last_checkpoint
= 0;
8258 if (is_gen_migration(dev
)) {
8259 dprintf_cont("while general migration");
8260 if (a
->last_checkpoint
>= a
->info
.component_size
)
8261 end_migration(dev
, super
, map_state
);
8263 map
->map_state
= map_state
;
8264 map
= get_imsm_map(dev
, MAP_0
);
8265 map
->failed_disk_num
= ~0;
8266 super
->updates_pending
++;
8270 case IMSM_T_STATE_DEGRADED
: /* transition to degraded state */
8271 dprintf_cont("degraded: ");
8272 if (map
->map_state
!= map_state
&& !dev
->vol
.migr_state
) {
8273 dprintf_cont("mark degraded");
8274 map
->map_state
= map_state
;
8275 super
->updates_pending
++;
8276 a
->last_checkpoint
= 0;
8279 if (is_rebuilding(dev
)) {
8280 dprintf_cont("while rebuilding.");
8281 if (map
->map_state
!= map_state
) {
8282 dprintf_cont(" Map state change");
8283 end_migration(dev
, super
, map_state
);
8284 super
->updates_pending
++;
8288 if (is_gen_migration(dev
)) {
8289 dprintf_cont("while general migration");
8290 if (a
->last_checkpoint
>= a
->info
.component_size
)
8291 end_migration(dev
, super
, map_state
);
8293 map
->map_state
= map_state
;
8294 manage_second_map(super
, dev
);
8296 super
->updates_pending
++;
8299 if (is_initializing(dev
)) {
8300 dprintf_cont("while initialization.");
8301 map
->map_state
= map_state
;
8302 super
->updates_pending
++;
8306 case IMSM_T_STATE_FAILED
: /* transition to failed state */
8307 dprintf_cont("failed: ");
8308 if (is_gen_migration(dev
)) {
8309 dprintf_cont("while general migration");
8310 map
->map_state
= map_state
;
8311 super
->updates_pending
++;
8314 if (map
->map_state
!= map_state
) {
8315 dprintf_cont("mark failed");
8316 end_migration(dev
, super
, map_state
);
8317 super
->updates_pending
++;
8318 a
->last_checkpoint
= 0;
8323 dprintf_cont("state %i\n", map_state
);
8328 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
8331 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
8332 unsigned long long dsize
;
8333 unsigned long long sectors
;
8334 unsigned int sector_size
;
8336 get_dev_sector_size(fd
, NULL
, §or_size
);
8337 get_dev_size(fd
, NULL
, &dsize
);
8339 if (mpb_size
> sector_size
) {
8340 /* -1 to account for anchor */
8341 sectors
= mpb_sectors(mpb
, sector_size
) - 1;
8343 /* write the extended mpb to the sectors preceeding the anchor */
8344 if (lseek64(fd
, dsize
- (sector_size
* (2 + sectors
)),
8348 if ((unsigned long long)write(fd
, buf
+ sector_size
,
8349 sector_size
* sectors
) != sector_size
* sectors
)
8353 /* first block is stored on second to last sector of the disk */
8354 if (lseek64(fd
, dsize
- (sector_size
* 2), SEEK_SET
) < 0)
8357 if ((unsigned int)write(fd
, buf
, sector_size
) != sector_size
)
8363 static void imsm_sync_metadata(struct supertype
*container
)
8365 struct intel_super
*super
= container
->sb
;
8367 dprintf("sync metadata: %d\n", super
->updates_pending
);
8368 if (!super
->updates_pending
)
8371 write_super_imsm(container
, 0);
8373 super
->updates_pending
= 0;
8376 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
8378 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
8379 int i
= get_imsm_disk_idx(dev
, idx
, MAP_X
);
8382 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
8386 if (dl
&& is_failed(&dl
->disk
))
8390 dprintf("found %x:%x\n", dl
->major
, dl
->minor
);
8395 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
8396 struct active_array
*a
, int activate_new
,
8397 struct mdinfo
*additional_test_list
)
8399 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
8400 int idx
= get_imsm_disk_idx(dev
, slot
, MAP_X
);
8401 struct imsm_super
*mpb
= super
->anchor
;
8402 struct imsm_map
*map
;
8403 unsigned long long pos
;
8408 __u32 array_start
= 0;
8409 __u32 array_end
= 0;
8411 struct mdinfo
*test_list
;
8413 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
8414 /* If in this array, skip */
8415 for (d
= a
->info
.devs
; d
; d
= d
->next
)
8416 if (d
->state_fd
>= 0 &&
8417 d
->disk
.major
== dl
->major
&&
8418 d
->disk
.minor
== dl
->minor
) {
8419 dprintf("%x:%x already in array\n",
8420 dl
->major
, dl
->minor
);
8425 test_list
= additional_test_list
;
8427 if (test_list
->disk
.major
== dl
->major
&&
8428 test_list
->disk
.minor
== dl
->minor
) {
8429 dprintf("%x:%x already in additional test list\n",
8430 dl
->major
, dl
->minor
);
8433 test_list
= test_list
->next
;
8438 /* skip in use or failed drives */
8439 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
8441 dprintf("%x:%x status (failed: %d index: %d)\n",
8442 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
8446 /* skip pure spares when we are looking for partially
8447 * assimilated drives
8449 if (dl
->index
== -1 && !activate_new
)
8452 /* Does this unused device have the requisite free space?
8453 * It needs to be able to cover all member volumes
8455 ex
= get_extents(super
, dl
);
8457 dprintf("cannot get extents\n");
8460 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8461 dev
= get_imsm_dev(super
, i
);
8462 map
= get_imsm_map(dev
, MAP_0
);
8464 /* check if this disk is already a member of
8467 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
8473 array_start
= pba_of_lba0(map
);
8474 array_end
= array_start
+
8475 blocks_per_member(map
) - 1;
8478 /* check that we can start at pba_of_lba0 with
8479 * blocks_per_member of space
8481 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
8485 pos
= ex
[j
].start
+ ex
[j
].size
;
8487 } while (ex
[j
-1].size
);
8494 if (i
< mpb
->num_raid_devs
) {
8495 dprintf("%x:%x does not have %u to %u available\n",
8496 dl
->major
, dl
->minor
, array_start
, array_end
);
8506 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
8508 struct imsm_dev
*dev2
;
8509 struct imsm_map
*map
;
8515 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
8517 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
, MAP_0
);
8518 if (state
== IMSM_T_STATE_FAILED
) {
8519 map
= get_imsm_map(dev2
, MAP_0
);
8522 for (slot
= 0; slot
< map
->num_members
; slot
++) {
8524 * Check if failed disks are deleted from intel
8525 * disk list or are marked to be deleted
8527 idx
= get_imsm_disk_idx(dev2
, slot
, MAP_X
);
8528 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
8530 * Do not rebuild the array if failed disks
8531 * from failed sub-array are not removed from
8535 is_failed(&idisk
->disk
) &&
8536 (idisk
->action
!= DISK_REMOVE
))
8544 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
8545 struct metadata_update
**updates
)
8548 * Find a device with unused free space and use it to replace a
8549 * failed/vacant region in an array. We replace failed regions one a
8550 * array at a time. The result is that a new spare disk will be added
8551 * to the first failed array and after the monitor has finished
8552 * propagating failures the remainder will be consumed.
8554 * FIXME add a capability for mdmon to request spares from another
8558 struct intel_super
*super
= a
->container
->sb
;
8559 int inst
= a
->info
.container_member
;
8560 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8561 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8562 int failed
= a
->info
.array
.raid_disks
;
8563 struct mdinfo
*rv
= NULL
;
8566 struct metadata_update
*mu
;
8568 struct imsm_update_activate_spare
*u
;
8573 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
8574 if ((d
->curr_state
& DS_FAULTY
) &&
8576 /* wait for Removal to happen */
8578 if (d
->state_fd
>= 0)
8582 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
8583 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
8585 if (imsm_reshape_blocks_arrays_changes(super
))
8588 /* Cannot activate another spare if rebuild is in progress already
8590 if (is_rebuilding(dev
)) {
8591 dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n");
8595 if (a
->info
.array
.level
== 4)
8596 /* No repair for takeovered array
8597 * imsm doesn't support raid4
8601 if (imsm_check_degraded(super
, dev
, failed
, MAP_0
) !=
8602 IMSM_T_STATE_DEGRADED
)
8605 if (get_imsm_map(dev
, MAP_0
)->map_state
== IMSM_T_STATE_UNINITIALIZED
) {
8606 dprintf("imsm: No spare activation allowed. Volume is not initialized.\n");
8611 * If there are any failed disks check state of the other volume.
8612 * Block rebuild if the another one is failed until failed disks
8613 * are removed from container.
8616 dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n",
8617 MAX_RAID_SERIAL_LEN
, dev
->volume
);
8618 /* check if states of the other volumes allow for rebuild */
8619 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
8621 allowed
= imsm_rebuild_allowed(a
->container
,
8629 /* For each slot, if it is not working, find a spare */
8630 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
8631 for (d
= a
->info
.devs
; d
; d
= d
->next
)
8632 if (d
->disk
.raid_disk
== i
)
8634 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
8635 if (d
&& (d
->state_fd
>= 0))
8639 * OK, this device needs recovery. Try to re-add the
8640 * previous occupant of this slot, if this fails see if
8641 * we can continue the assimilation of a spare that was
8642 * partially assimilated, finally try to activate a new
8645 dl
= imsm_readd(super
, i
, a
);
8647 dl
= imsm_add_spare(super
, i
, a
, 0, rv
);
8649 dl
= imsm_add_spare(super
, i
, a
, 1, rv
);
8653 /* found a usable disk with enough space */
8654 di
= xcalloc(1, sizeof(*di
));
8656 /* dl->index will be -1 in the case we are activating a
8657 * pristine spare. imsm_process_update() will create a
8658 * new index in this case. Once a disk is found to be
8659 * failed in all member arrays it is kicked from the
8662 di
->disk
.number
= dl
->index
;
8664 /* (ab)use di->devs to store a pointer to the device
8667 di
->devs
= (struct mdinfo
*) dl
;
8669 di
->disk
.raid_disk
= i
;
8670 di
->disk
.major
= dl
->major
;
8671 di
->disk
.minor
= dl
->minor
;
8673 di
->recovery_start
= 0;
8674 di
->data_offset
= pba_of_lba0(map
);
8675 di
->component_size
= a
->info
.component_size
;
8676 di
->container_member
= inst
;
8677 di
->bb
.supported
= 1;
8678 if (dev
->rwh_policy
== RWH_DISTRIBUTED
) {
8679 di
->consistency_policy
= CONSISTENCY_POLICY_PPL
;
8680 di
->ppl_sector
= get_ppl_sector(super
, inst
);
8681 di
->ppl_size
= (PPL_HEADER_SIZE
+ PPL_ENTRY_SPACE
) >> 9;
8683 super
->random
= random32();
8687 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
8688 i
, di
->data_offset
);
8692 /* No spares found */
8694 /* Now 'rv' has a list of devices to return.
8695 * Create a metadata_update record to update the
8696 * disk_ord_tbl for the array
8698 mu
= xmalloc(sizeof(*mu
));
8699 mu
->buf
= xcalloc(num_spares
,
8700 sizeof(struct imsm_update_activate_spare
));
8702 mu
->space_list
= NULL
;
8703 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
8704 mu
->next
= *updates
;
8705 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
8707 for (di
= rv
; di
; di
= di
->next
) {
8708 u
->type
= update_activate_spare
;
8709 u
->dl
= (struct dl
*) di
->devs
;
8711 u
->slot
= di
->disk
.raid_disk
;
8722 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
8724 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
8725 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8726 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, MAP_0
);
8727 struct disk_info
*inf
= get_disk_info(u
);
8728 struct imsm_disk
*disk
;
8732 for (i
= 0; i
< map
->num_members
; i
++) {
8733 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, MAP_X
));
8734 for (j
= 0; j
< new_map
->num_members
; j
++)
8735 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
8742 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
8746 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
8747 if (dl
->major
== major
&& dl
->minor
== minor
)
8752 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
8758 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
8759 if (dl
->major
== major
&& dl
->minor
== minor
) {
8762 prev
->next
= dl
->next
;
8764 super
->disks
= dl
->next
;
8766 __free_imsm_disk(dl
);
8767 dprintf("removed %x:%x\n", major
, minor
);
8775 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
8777 static int add_remove_disk_update(struct intel_super
*super
)
8779 int check_degraded
= 0;
8782 /* add/remove some spares to/from the metadata/contrainer */
8783 while (super
->disk_mgmt_list
) {
8784 struct dl
*disk_cfg
;
8786 disk_cfg
= super
->disk_mgmt_list
;
8787 super
->disk_mgmt_list
= disk_cfg
->next
;
8788 disk_cfg
->next
= NULL
;
8790 if (disk_cfg
->action
== DISK_ADD
) {
8791 disk_cfg
->next
= super
->disks
;
8792 super
->disks
= disk_cfg
;
8794 dprintf("added %x:%x\n",
8795 disk_cfg
->major
, disk_cfg
->minor
);
8796 } else if (disk_cfg
->action
== DISK_REMOVE
) {
8797 dprintf("Disk remove action processed: %x.%x\n",
8798 disk_cfg
->major
, disk_cfg
->minor
);
8799 disk
= get_disk_super(super
,
8803 /* store action status */
8804 disk
->action
= DISK_REMOVE
;
8805 /* remove spare disks only */
8806 if (disk
->index
== -1) {
8807 remove_disk_super(super
,
8812 /* release allocate disk structure */
8813 __free_imsm_disk(disk_cfg
);
8816 return check_degraded
;
8819 static int apply_reshape_migration_update(struct imsm_update_reshape_migration
*u
,
8820 struct intel_super
*super
,
8823 struct intel_dev
*id
;
8824 void **tofree
= NULL
;
8827 dprintf("(enter)\n");
8828 if (u
->subdev
< 0 || u
->subdev
> 1) {
8829 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
8832 if (space_list
== NULL
|| *space_list
== NULL
) {
8833 dprintf("imsm: Error: Memory is not allocated\n");
8837 for (id
= super
->devlist
; id
; id
= id
->next
) {
8838 if (id
->index
== (unsigned)u
->subdev
) {
8839 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
8840 struct imsm_map
*map
;
8841 struct imsm_dev
*new_dev
=
8842 (struct imsm_dev
*)*space_list
;
8843 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
8845 struct dl
*new_disk
;
8847 if (new_dev
== NULL
)
8849 *space_list
= **space_list
;
8850 memcpy(new_dev
, dev
, sizeof_imsm_dev(dev
, 0));
8851 map
= get_imsm_map(new_dev
, MAP_0
);
8853 dprintf("imsm: Error: migration in progress");
8857 to_state
= map
->map_state
;
8858 if ((u
->new_level
== 5) && (map
->raid_level
== 0)) {
8860 /* this should not happen */
8861 if (u
->new_disks
[0] < 0) {
8862 map
->failed_disk_num
=
8863 map
->num_members
- 1;
8864 to_state
= IMSM_T_STATE_DEGRADED
;
8866 to_state
= IMSM_T_STATE_NORMAL
;
8868 migrate(new_dev
, super
, to_state
, MIGR_GEN_MIGR
);
8869 if (u
->new_level
> -1)
8870 map
->raid_level
= u
->new_level
;
8871 migr_map
= get_imsm_map(new_dev
, MAP_1
);
8872 if ((u
->new_level
== 5) &&
8873 (migr_map
->raid_level
== 0)) {
8874 int ord
= map
->num_members
- 1;
8875 migr_map
->num_members
--;
8876 if (u
->new_disks
[0] < 0)
8877 ord
|= IMSM_ORD_REBUILD
;
8878 set_imsm_ord_tbl_ent(map
,
8879 map
->num_members
- 1,
8883 tofree
= (void **)dev
;
8885 /* update chunk size
8887 if (u
->new_chunksize
> 0) {
8888 unsigned long long num_data_stripes
;
8890 imsm_num_data_members(dev
, MAP_0
);
8892 if (used_disks
== 0)
8895 map
->blocks_per_strip
=
8896 __cpu_to_le16(u
->new_chunksize
* 2);
8898 (join_u32(dev
->size_low
, dev
->size_high
)
8900 num_data_stripes
/= map
->blocks_per_strip
;
8901 num_data_stripes
/= map
->num_domains
;
8902 set_num_data_stripes(map
, num_data_stripes
);
8907 if (u
->new_level
!= 5 || migr_map
->raid_level
!= 0 ||
8908 migr_map
->raid_level
== map
->raid_level
)
8911 if (u
->new_disks
[0] >= 0) {
8914 new_disk
= get_disk_super(super
,
8915 major(u
->new_disks
[0]),
8916 minor(u
->new_disks
[0]));
8917 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
8918 major(u
->new_disks
[0]),
8919 minor(u
->new_disks
[0]),
8920 new_disk
, new_disk
->index
);
8921 if (new_disk
== NULL
)
8922 goto error_disk_add
;
8924 new_disk
->index
= map
->num_members
- 1;
8925 /* slot to fill in autolayout
8927 new_disk
->raiddisk
= new_disk
->index
;
8928 new_disk
->disk
.status
|= CONFIGURED_DISK
;
8929 new_disk
->disk
.status
&= ~SPARE_DISK
;
8931 goto error_disk_add
;
8934 *tofree
= *space_list
;
8935 /* calculate new size
8937 imsm_set_array_size(new_dev
, -1);
8944 *space_list
= tofree
;
8948 dprintf("Error: imsm: Cannot find disk.\n");
8952 static int apply_size_change_update(struct imsm_update_size_change
*u
,
8953 struct intel_super
*super
)
8955 struct intel_dev
*id
;
8958 dprintf("(enter)\n");
8959 if (u
->subdev
< 0 || u
->subdev
> 1) {
8960 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
8964 for (id
= super
->devlist
; id
; id
= id
->next
) {
8965 if (id
->index
== (unsigned)u
->subdev
) {
8966 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
8967 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8968 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
8969 unsigned long long blocks_per_member
;
8970 unsigned long long num_data_stripes
;
8972 /* calculate new size
8974 blocks_per_member
= u
->new_size
/ used_disks
;
8975 num_data_stripes
= blocks_per_member
/
8976 map
->blocks_per_strip
;
8977 num_data_stripes
/= map
->num_domains
;
8978 dprintf("(size: %llu, blocks per member: %llu, num_data_stipes: %llu)\n",
8979 u
->new_size
, blocks_per_member
,
8981 set_blocks_per_member(map
, blocks_per_member
);
8982 set_num_data_stripes(map
, num_data_stripes
);
8983 imsm_set_array_size(dev
, u
->new_size
);
8993 static int apply_update_activate_spare(struct imsm_update_activate_spare
*u
,
8994 struct intel_super
*super
,
8995 struct active_array
*active_array
)
8997 struct imsm_super
*mpb
= super
->anchor
;
8998 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
8999 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9000 struct imsm_map
*migr_map
;
9001 struct active_array
*a
;
9002 struct imsm_disk
*disk
;
9009 int second_map_created
= 0;
9011 for (; u
; u
= u
->next
) {
9012 victim
= get_imsm_disk_idx(dev
, u
->slot
, MAP_X
);
9017 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
9022 pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n",
9027 /* count failures (excluding rebuilds and the victim)
9028 * to determine map[0] state
9031 for (i
= 0; i
< map
->num_members
; i
++) {
9034 disk
= get_imsm_disk(super
,
9035 get_imsm_disk_idx(dev
, i
, MAP_X
));
9036 if (!disk
|| is_failed(disk
))
9040 /* adding a pristine spare, assign a new index */
9041 if (dl
->index
< 0) {
9042 dl
->index
= super
->anchor
->num_disks
;
9043 super
->anchor
->num_disks
++;
9046 disk
->status
|= CONFIGURED_DISK
;
9047 disk
->status
&= ~SPARE_DISK
;
9050 to_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
9051 if (!second_map_created
) {
9052 second_map_created
= 1;
9053 map
->map_state
= IMSM_T_STATE_DEGRADED
;
9054 migrate(dev
, super
, to_state
, MIGR_REBUILD
);
9056 map
->map_state
= to_state
;
9057 migr_map
= get_imsm_map(dev
, MAP_1
);
9058 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
9059 set_imsm_ord_tbl_ent(migr_map
, u
->slot
,
9060 dl
->index
| IMSM_ORD_REBUILD
);
9062 /* update the family_num to mark a new container
9063 * generation, being careful to record the existing
9064 * family_num in orig_family_num to clean up after
9065 * earlier mdadm versions that neglected to set it.
9067 if (mpb
->orig_family_num
== 0)
9068 mpb
->orig_family_num
= mpb
->family_num
;
9069 mpb
->family_num
+= super
->random
;
9071 /* count arrays using the victim in the metadata */
9073 for (a
= active_array
; a
; a
= a
->next
) {
9074 dev
= get_imsm_dev(super
, a
->info
.container_member
);
9075 map
= get_imsm_map(dev
, MAP_0
);
9077 if (get_imsm_disk_slot(map
, victim
) >= 0)
9081 /* delete the victim if it is no longer being
9087 /* We know that 'manager' isn't touching anything,
9088 * so it is safe to delete
9090 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
9091 if ((*dlp
)->index
== victim
)
9094 /* victim may be on the missing list */
9096 for (dlp
= &super
->missing
; *dlp
;
9097 dlp
= &(*dlp
)->next
)
9098 if ((*dlp
)->index
== victim
)
9100 imsm_delete(super
, dlp
, victim
);
9107 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
9108 struct intel_super
*super
,
9111 struct dl
*new_disk
;
9112 struct intel_dev
*id
;
9114 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
9115 int disk_count
= u
->old_raid_disks
;
9116 void **tofree
= NULL
;
9117 int devices_to_reshape
= 1;
9118 struct imsm_super
*mpb
= super
->anchor
;
9120 unsigned int dev_id
;
9122 dprintf("(enter)\n");
9124 /* enable spares to use in array */
9125 for (i
= 0; i
< delta_disks
; i
++) {
9126 new_disk
= get_disk_super(super
,
9127 major(u
->new_disks
[i
]),
9128 minor(u
->new_disks
[i
]));
9129 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
9130 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
9131 new_disk
, new_disk
->index
);
9132 if (new_disk
== NULL
||
9133 (new_disk
->index
>= 0 &&
9134 new_disk
->index
< u
->old_raid_disks
))
9135 goto update_reshape_exit
;
9136 new_disk
->index
= disk_count
++;
9137 /* slot to fill in autolayout
9139 new_disk
->raiddisk
= new_disk
->index
;
9140 new_disk
->disk
.status
|=
9142 new_disk
->disk
.status
&= ~SPARE_DISK
;
9145 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
9146 mpb
->num_raid_devs
);
9147 /* manage changes in volume
9149 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
9150 void **sp
= *space_list
;
9151 struct imsm_dev
*newdev
;
9152 struct imsm_map
*newmap
, *oldmap
;
9154 for (id
= super
->devlist
; id
; id
= id
->next
) {
9155 if (id
->index
== dev_id
)
9164 /* Copy the dev, but not (all of) the map */
9165 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
9166 oldmap
= get_imsm_map(id
->dev
, MAP_0
);
9167 newmap
= get_imsm_map(newdev
, MAP_0
);
9168 /* Copy the current map */
9169 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
9170 /* update one device only
9172 if (devices_to_reshape
) {
9173 dprintf("imsm: modifying subdev: %i\n",
9175 devices_to_reshape
--;
9176 newdev
->vol
.migr_state
= 1;
9177 newdev
->vol
.curr_migr_unit
= 0;
9178 set_migr_type(newdev
, MIGR_GEN_MIGR
);
9179 newmap
->num_members
= u
->new_raid_disks
;
9180 for (i
= 0; i
< delta_disks
; i
++) {
9181 set_imsm_ord_tbl_ent(newmap
,
9182 u
->old_raid_disks
+ i
,
9183 u
->old_raid_disks
+ i
);
9185 /* New map is correct, now need to save old map
9187 newmap
= get_imsm_map(newdev
, MAP_1
);
9188 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
9190 imsm_set_array_size(newdev
, -1);
9193 sp
= (void **)id
->dev
;
9198 /* Clear migration record */
9199 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
9202 *space_list
= tofree
;
9205 update_reshape_exit
:
9210 static int apply_takeover_update(struct imsm_update_takeover
*u
,
9211 struct intel_super
*super
,
9214 struct imsm_dev
*dev
= NULL
;
9215 struct intel_dev
*dv
;
9216 struct imsm_dev
*dev_new
;
9217 struct imsm_map
*map
;
9221 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
9222 if (dv
->index
== (unsigned int)u
->subarray
) {
9230 map
= get_imsm_map(dev
, MAP_0
);
9232 if (u
->direction
== R10_TO_R0
) {
9233 unsigned long long num_data_stripes
;
9235 map
->num_domains
= 1;
9236 num_data_stripes
= blocks_per_member(map
);
9237 num_data_stripes
/= map
->blocks_per_strip
;
9238 num_data_stripes
/= map
->num_domains
;
9239 set_num_data_stripes(map
, num_data_stripes
);
9241 /* Number of failed disks must be half of initial disk number */
9242 if (imsm_count_failed(super
, dev
, MAP_0
) !=
9243 (map
->num_members
/ 2))
9246 /* iterate through devices to mark removed disks as spare */
9247 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
9248 if (dm
->disk
.status
& FAILED_DISK
) {
9249 int idx
= dm
->index
;
9250 /* update indexes on the disk list */
9251 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
9252 the index values will end up being correct.... NB */
9253 for (du
= super
->disks
; du
; du
= du
->next
)
9254 if (du
->index
> idx
)
9256 /* mark as spare disk */
9261 map
->num_members
= map
->num_members
/ 2;
9262 map
->map_state
= IMSM_T_STATE_NORMAL
;
9263 map
->num_domains
= 1;
9264 map
->raid_level
= 0;
9265 map
->failed_disk_num
= -1;
9268 if (u
->direction
== R0_TO_R10
) {
9270 /* update slots in current disk list */
9271 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
9275 /* create new *missing* disks */
9276 for (i
= 0; i
< map
->num_members
; i
++) {
9277 space
= *space_list
;
9280 *space_list
= *space
;
9282 memcpy(du
, super
->disks
, sizeof(*du
));
9286 du
->index
= (i
* 2) + 1;
9287 sprintf((char *)du
->disk
.serial
,
9288 " MISSING_%d", du
->index
);
9289 sprintf((char *)du
->serial
,
9290 "MISSING_%d", du
->index
);
9291 du
->next
= super
->missing
;
9292 super
->missing
= du
;
9294 /* create new dev and map */
9295 space
= *space_list
;
9298 *space_list
= *space
;
9299 dev_new
= (void *)space
;
9300 memcpy(dev_new
, dev
, sizeof(*dev
));
9301 /* update new map */
9302 map
= get_imsm_map(dev_new
, MAP_0
);
9303 map
->num_members
= map
->num_members
* 2;
9304 map
->map_state
= IMSM_T_STATE_DEGRADED
;
9305 map
->num_domains
= 2;
9306 map
->raid_level
= 1;
9307 /* replace dev<->dev_new */
9310 /* update disk order table */
9311 for (du
= super
->disks
; du
; du
= du
->next
)
9313 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
9314 for (du
= super
->missing
; du
; du
= du
->next
)
9315 if (du
->index
>= 0) {
9316 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
9317 mark_missing(super
, dv
->dev
, &du
->disk
, du
->index
);
9323 static void imsm_process_update(struct supertype
*st
,
9324 struct metadata_update
*update
)
9327 * crack open the metadata_update envelope to find the update record
9328 * update can be one of:
9329 * update_reshape_container_disks - all the arrays in the container
9330 * are being reshaped to have more devices. We need to mark
9331 * the arrays for general migration and convert selected spares
9332 * into active devices.
9333 * update_activate_spare - a spare device has replaced a failed
9334 * device in an array, update the disk_ord_tbl. If this disk is
9335 * present in all member arrays then also clear the SPARE_DISK
9337 * update_create_array
9339 * update_rename_array
9340 * update_add_remove_disk
9342 struct intel_super
*super
= st
->sb
;
9343 struct imsm_super
*mpb
;
9344 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
9346 /* update requires a larger buf but the allocation failed */
9347 if (super
->next_len
&& !super
->next_buf
) {
9348 super
->next_len
= 0;
9352 if (super
->next_buf
) {
9353 memcpy(super
->next_buf
, super
->buf
, super
->len
);
9355 super
->len
= super
->next_len
;
9356 super
->buf
= super
->next_buf
;
9358 super
->next_len
= 0;
9359 super
->next_buf
= NULL
;
9362 mpb
= super
->anchor
;
9365 case update_general_migration_checkpoint
: {
9366 struct intel_dev
*id
;
9367 struct imsm_update_general_migration_checkpoint
*u
=
9368 (void *)update
->buf
;
9370 dprintf("called for update_general_migration_checkpoint\n");
9372 /* find device under general migration */
9373 for (id
= super
->devlist
; id
; id
= id
->next
) {
9374 if (is_gen_migration(id
->dev
)) {
9375 id
->dev
->vol
.curr_migr_unit
=
9376 __cpu_to_le32(u
->curr_migr_unit
);
9377 super
->updates_pending
++;
9382 case update_takeover
: {
9383 struct imsm_update_takeover
*u
= (void *)update
->buf
;
9384 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
9385 imsm_update_version_info(super
);
9386 super
->updates_pending
++;
9391 case update_reshape_container_disks
: {
9392 struct imsm_update_reshape
*u
= (void *)update
->buf
;
9393 if (apply_reshape_container_disks_update(
9394 u
, super
, &update
->space_list
))
9395 super
->updates_pending
++;
9398 case update_reshape_migration
: {
9399 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
9400 if (apply_reshape_migration_update(
9401 u
, super
, &update
->space_list
))
9402 super
->updates_pending
++;
9405 case update_size_change
: {
9406 struct imsm_update_size_change
*u
= (void *)update
->buf
;
9407 if (apply_size_change_update(u
, super
))
9408 super
->updates_pending
++;
9411 case update_activate_spare
: {
9412 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
9413 if (apply_update_activate_spare(u
, super
, st
->arrays
))
9414 super
->updates_pending
++;
9417 case update_create_array
: {
9418 /* someone wants to create a new array, we need to be aware of
9419 * a few races/collisions:
9420 * 1/ 'Create' called by two separate instances of mdadm
9421 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
9422 * devices that have since been assimilated via
9424 * In the event this update can not be carried out mdadm will
9425 * (FIX ME) notice that its update did not take hold.
9427 struct imsm_update_create_array
*u
= (void *) update
->buf
;
9428 struct intel_dev
*dv
;
9429 struct imsm_dev
*dev
;
9430 struct imsm_map
*map
, *new_map
;
9431 unsigned long long start
, end
;
9432 unsigned long long new_start
, new_end
;
9434 struct disk_info
*inf
;
9437 /* handle racing creates: first come first serve */
9438 if (u
->dev_idx
< mpb
->num_raid_devs
) {
9439 dprintf("subarray %d already defined\n", u
->dev_idx
);
9443 /* check update is next in sequence */
9444 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
9445 dprintf("can not create array %d expected index %d\n",
9446 u
->dev_idx
, mpb
->num_raid_devs
);
9450 new_map
= get_imsm_map(&u
->dev
, MAP_0
);
9451 new_start
= pba_of_lba0(new_map
);
9452 new_end
= new_start
+ blocks_per_member(new_map
);
9453 inf
= get_disk_info(u
);
9455 /* handle activate_spare versus create race:
9456 * check to make sure that overlapping arrays do not include
9459 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
9460 dev
= get_imsm_dev(super
, i
);
9461 map
= get_imsm_map(dev
, MAP_0
);
9462 start
= pba_of_lba0(map
);
9463 end
= start
+ blocks_per_member(map
);
9464 if ((new_start
>= start
&& new_start
<= end
) ||
9465 (start
>= new_start
&& start
<= new_end
))
9470 if (disks_overlap(super
, i
, u
)) {
9471 dprintf("arrays overlap\n");
9476 /* check that prepare update was successful */
9477 if (!update
->space
) {
9478 dprintf("prepare update failed\n");
9482 /* check that all disks are still active before committing
9483 * changes. FIXME: could we instead handle this by creating a
9484 * degraded array? That's probably not what the user expects,
9485 * so better to drop this update on the floor.
9487 for (i
= 0; i
< new_map
->num_members
; i
++) {
9488 dl
= serial_to_dl(inf
[i
].serial
, super
);
9490 dprintf("disk disappeared\n");
9495 super
->updates_pending
++;
9497 /* convert spares to members and fixup ord_tbl */
9498 for (i
= 0; i
< new_map
->num_members
; i
++) {
9499 dl
= serial_to_dl(inf
[i
].serial
, super
);
9500 if (dl
->index
== -1) {
9501 dl
->index
= mpb
->num_disks
;
9503 dl
->disk
.status
|= CONFIGURED_DISK
;
9504 dl
->disk
.status
&= ~SPARE_DISK
;
9506 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
9511 update
->space
= NULL
;
9512 imsm_copy_dev(dev
, &u
->dev
);
9513 dv
->index
= u
->dev_idx
;
9514 dv
->next
= super
->devlist
;
9515 super
->devlist
= dv
;
9516 mpb
->num_raid_devs
++;
9518 imsm_update_version_info(super
);
9521 /* mdmon knows how to release update->space, but not
9522 * ((struct intel_dev *) update->space)->dev
9524 if (update
->space
) {
9530 case update_kill_array
: {
9531 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
9532 int victim
= u
->dev_idx
;
9533 struct active_array
*a
;
9534 struct intel_dev
**dp
;
9535 struct imsm_dev
*dev
;
9537 /* sanity check that we are not affecting the uuid of
9538 * active arrays, or deleting an active array
9540 * FIXME when immutable ids are available, but note that
9541 * we'll also need to fixup the invalidated/active
9542 * subarray indexes in mdstat
9544 for (a
= st
->arrays
; a
; a
= a
->next
)
9545 if (a
->info
.container_member
>= victim
)
9547 /* by definition if mdmon is running at least one array
9548 * is active in the container, so checking
9549 * mpb->num_raid_devs is just extra paranoia
9551 dev
= get_imsm_dev(super
, victim
);
9552 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
9553 dprintf("failed to delete subarray-%d\n", victim
);
9557 for (dp
= &super
->devlist
; *dp
;)
9558 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
9561 if ((*dp
)->index
> (unsigned)victim
)
9565 mpb
->num_raid_devs
--;
9566 super
->updates_pending
++;
9569 case update_rename_array
: {
9570 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
9571 char name
[MAX_RAID_SERIAL_LEN
+1];
9572 int target
= u
->dev_idx
;
9573 struct active_array
*a
;
9574 struct imsm_dev
*dev
;
9576 /* sanity check that we are not affecting the uuid of
9579 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
9580 name
[MAX_RAID_SERIAL_LEN
] = '\0';
9581 for (a
= st
->arrays
; a
; a
= a
->next
)
9582 if (a
->info
.container_member
== target
)
9584 dev
= get_imsm_dev(super
, u
->dev_idx
);
9585 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
9586 dprintf("failed to rename subarray-%d\n", target
);
9590 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
9591 super
->updates_pending
++;
9594 case update_add_remove_disk
: {
9595 /* we may be able to repair some arrays if disks are
9596 * being added, check the status of add_remove_disk
9597 * if discs has been added.
9599 if (add_remove_disk_update(super
)) {
9600 struct active_array
*a
;
9602 super
->updates_pending
++;
9603 for (a
= st
->arrays
; a
; a
= a
->next
)
9604 a
->check_degraded
= 1;
9608 case update_prealloc_badblocks_mem
:
9610 case update_rwh_policy
: {
9611 struct imsm_update_rwh_policy
*u
= (void *)update
->buf
;
9612 int target
= u
->dev_idx
;
9613 struct imsm_dev
*dev
= get_imsm_dev(super
, target
);
9615 dprintf("could not find subarray-%d\n", target
);
9619 if (dev
->rwh_policy
!= u
->new_policy
) {
9620 dev
->rwh_policy
= u
->new_policy
;
9621 super
->updates_pending
++;
9626 pr_err("error: unsuported process update type:(type: %d)\n", type
);
9630 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
);
9632 static int imsm_prepare_update(struct supertype
*st
,
9633 struct metadata_update
*update
)
9636 * Allocate space to hold new disk entries, raid-device entries or a new
9637 * mpb if necessary. The manager synchronously waits for updates to
9638 * complete in the monitor, so new mpb buffers allocated here can be
9639 * integrated by the monitor thread without worrying about live pointers
9640 * in the manager thread.
9642 enum imsm_update_type type
;
9643 struct intel_super
*super
= st
->sb
;
9644 unsigned int sector_size
= super
->sector_size
;
9645 struct imsm_super
*mpb
= super
->anchor
;
9649 if (update
->len
< (int)sizeof(type
))
9652 type
= *(enum imsm_update_type
*) update
->buf
;
9655 case update_general_migration_checkpoint
:
9656 if (update
->len
< (int)sizeof(struct imsm_update_general_migration_checkpoint
))
9658 dprintf("called for update_general_migration_checkpoint\n");
9660 case update_takeover
: {
9661 struct imsm_update_takeover
*u
= (void *)update
->buf
;
9662 if (update
->len
< (int)sizeof(*u
))
9664 if (u
->direction
== R0_TO_R10
) {
9665 void **tail
= (void **)&update
->space_list
;
9666 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
9667 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9668 int num_members
= map
->num_members
;
9671 /* allocate memory for added disks */
9672 for (i
= 0; i
< num_members
; i
++) {
9673 size
= sizeof(struct dl
);
9674 space
= xmalloc(size
);
9679 /* allocate memory for new device */
9680 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
9681 (num_members
* sizeof(__u32
));
9682 space
= xmalloc(size
);
9686 len
= disks_to_mpb_size(num_members
* 2);
9691 case update_reshape_container_disks
: {
9692 /* Every raid device in the container is about to
9693 * gain some more devices, and we will enter a
9695 * So each 'imsm_map' will be bigger, and the imsm_vol
9696 * will now hold 2 of them.
9697 * Thus we need new 'struct imsm_dev' allocations sized
9698 * as sizeof_imsm_dev but with more devices in both maps.
9700 struct imsm_update_reshape
*u
= (void *)update
->buf
;
9701 struct intel_dev
*dl
;
9702 void **space_tail
= (void**)&update
->space_list
;
9704 if (update
->len
< (int)sizeof(*u
))
9707 dprintf("for update_reshape\n");
9709 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
9710 int size
= sizeof_imsm_dev(dl
->dev
, 1);
9712 if (u
->new_raid_disks
> u
->old_raid_disks
)
9713 size
+= sizeof(__u32
)*2*
9714 (u
->new_raid_disks
- u
->old_raid_disks
);
9721 len
= disks_to_mpb_size(u
->new_raid_disks
);
9722 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
9725 case update_reshape_migration
: {
9726 /* for migration level 0->5 we need to add disks
9727 * so the same as for container operation we will copy
9728 * device to the bigger location.
9729 * in memory prepared device and new disk area are prepared
9730 * for usage in process update
9732 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
9733 struct intel_dev
*id
;
9734 void **space_tail
= (void **)&update
->space_list
;
9737 int current_level
= -1;
9739 if (update
->len
< (int)sizeof(*u
))
9742 dprintf("for update_reshape\n");
9744 /* add space for bigger array in update
9746 for (id
= super
->devlist
; id
; id
= id
->next
) {
9747 if (id
->index
== (unsigned)u
->subdev
) {
9748 size
= sizeof_imsm_dev(id
->dev
, 1);
9749 if (u
->new_raid_disks
> u
->old_raid_disks
)
9750 size
+= sizeof(__u32
)*2*
9751 (u
->new_raid_disks
- u
->old_raid_disks
);
9759 if (update
->space_list
== NULL
)
9762 /* add space for disk in update
9764 size
= sizeof(struct dl
);
9770 /* add spare device to update
9772 for (id
= super
->devlist
; id
; id
= id
->next
)
9773 if (id
->index
== (unsigned)u
->subdev
) {
9774 struct imsm_dev
*dev
;
9775 struct imsm_map
*map
;
9777 dev
= get_imsm_dev(super
, u
->subdev
);
9778 map
= get_imsm_map(dev
, MAP_0
);
9779 current_level
= map
->raid_level
;
9782 if (u
->new_level
== 5 && u
->new_level
!= current_level
) {
9783 struct mdinfo
*spares
;
9785 spares
= get_spares_for_grow(st
);
9793 makedev(dev
->disk
.major
,
9795 dl
= get_disk_super(super
,
9798 dl
->index
= u
->old_raid_disks
;
9804 len
= disks_to_mpb_size(u
->new_raid_disks
);
9805 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
9808 case update_size_change
: {
9809 if (update
->len
< (int)sizeof(struct imsm_update_size_change
))
9813 case update_activate_spare
: {
9814 if (update
->len
< (int)sizeof(struct imsm_update_activate_spare
))
9818 case update_create_array
: {
9819 struct imsm_update_create_array
*u
= (void *) update
->buf
;
9820 struct intel_dev
*dv
;
9821 struct imsm_dev
*dev
= &u
->dev
;
9822 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9824 struct disk_info
*inf
;
9828 if (update
->len
< (int)sizeof(*u
))
9831 inf
= get_disk_info(u
);
9832 len
= sizeof_imsm_dev(dev
, 1);
9833 /* allocate a new super->devlist entry */
9834 dv
= xmalloc(sizeof(*dv
));
9835 dv
->dev
= xmalloc(len
);
9838 /* count how many spares will be converted to members */
9839 for (i
= 0; i
< map
->num_members
; i
++) {
9840 dl
= serial_to_dl(inf
[i
].serial
, super
);
9842 /* hmm maybe it failed?, nothing we can do about
9847 if (count_memberships(dl
, super
) == 0)
9850 len
+= activate
* sizeof(struct imsm_disk
);
9853 case update_kill_array
: {
9854 if (update
->len
< (int)sizeof(struct imsm_update_kill_array
))
9858 case update_rename_array
: {
9859 if (update
->len
< (int)sizeof(struct imsm_update_rename_array
))
9863 case update_add_remove_disk
:
9864 /* no update->len needed */
9866 case update_prealloc_badblocks_mem
:
9867 super
->extra_space
+= sizeof(struct bbm_log
) -
9868 get_imsm_bbm_log_size(super
->bbm_log
);
9870 case update_rwh_policy
: {
9871 if (update
->len
< (int)sizeof(struct imsm_update_rwh_policy
))
9879 /* check if we need a larger metadata buffer */
9880 if (super
->next_buf
)
9881 buf_len
= super
->next_len
;
9883 buf_len
= super
->len
;
9885 if (__le32_to_cpu(mpb
->mpb_size
) + super
->extra_space
+ len
> buf_len
) {
9886 /* ok we need a larger buf than what is currently allocated
9887 * if this allocation fails process_update will notice that
9888 * ->next_len is set and ->next_buf is NULL
9890 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) +
9891 super
->extra_space
+ len
, sector_size
);
9892 if (super
->next_buf
)
9893 free(super
->next_buf
);
9895 super
->next_len
= buf_len
;
9896 if (posix_memalign(&super
->next_buf
, sector_size
, buf_len
) == 0)
9897 memset(super
->next_buf
, 0, buf_len
);
9899 super
->next_buf
= NULL
;
9904 /* must be called while manager is quiesced */
9905 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
9907 struct imsm_super
*mpb
= super
->anchor
;
9909 struct imsm_dev
*dev
;
9910 struct imsm_map
*map
;
9911 unsigned int i
, j
, num_members
;
9913 struct bbm_log
*log
= super
->bbm_log
;
9915 dprintf("deleting device[%d] from imsm_super\n", index
);
9917 /* shift all indexes down one */
9918 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
9919 if (iter
->index
> (int)index
)
9921 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
9922 if (iter
->index
> (int)index
)
9925 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
9926 dev
= get_imsm_dev(super
, i
);
9927 map
= get_imsm_map(dev
, MAP_0
);
9928 num_members
= map
->num_members
;
9929 for (j
= 0; j
< num_members
; j
++) {
9930 /* update ord entries being careful not to propagate
9931 * ord-flags to the first map
9933 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_X
);
9935 if (ord_to_idx(ord
) <= index
)
9938 map
= get_imsm_map(dev
, MAP_0
);
9939 set_imsm_ord_tbl_ent(map
, j
, ord_to_idx(ord
- 1));
9940 map
= get_imsm_map(dev
, MAP_1
);
9942 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
9946 for (i
= 0; i
< log
->entry_count
; i
++) {
9947 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[i
];
9949 if (entry
->disk_ordinal
<= index
)
9951 entry
->disk_ordinal
--;
9955 super
->updates_pending
++;
9957 struct dl
*dl
= *dlp
;
9959 *dlp
= (*dlp
)->next
;
9960 __free_imsm_disk(dl
);
9964 static void close_targets(int *targets
, int new_disks
)
9971 for (i
= 0; i
< new_disks
; i
++) {
9972 if (targets
[i
] >= 0) {
9979 static int imsm_get_allowed_degradation(int level
, int raid_disks
,
9980 struct intel_super
*super
,
9981 struct imsm_dev
*dev
)
9987 struct imsm_map
*map
;
9990 ret_val
= raid_disks
/2;
9991 /* check map if all disks pairs not failed
9994 map
= get_imsm_map(dev
, MAP_0
);
9995 for (i
= 0; i
< ret_val
; i
++) {
9996 int degradation
= 0;
9997 if (get_imsm_disk(super
, i
) == NULL
)
9999 if (get_imsm_disk(super
, i
+ 1) == NULL
)
10001 if (degradation
== 2)
10004 map
= get_imsm_map(dev
, MAP_1
);
10005 /* if there is no second map
10006 * result can be returned
10010 /* check degradation in second map
10012 for (i
= 0; i
< ret_val
; i
++) {
10013 int degradation
= 0;
10014 if (get_imsm_disk(super
, i
) == NULL
)
10016 if (get_imsm_disk(super
, i
+ 1) == NULL
)
10018 if (degradation
== 2)
10032 /*******************************************************************************
10033 * Function: open_backup_targets
10034 * Description: Function opens file descriptors for all devices given in
10037 * info : general array info
10038 * raid_disks : number of disks
10039 * raid_fds : table of device's file descriptors
10040 * super : intel super for raid10 degradation check
10041 * dev : intel device for raid10 degradation check
10045 ******************************************************************************/
10046 int open_backup_targets(struct mdinfo
*info
, int raid_disks
, int *raid_fds
,
10047 struct intel_super
*super
, struct imsm_dev
*dev
)
10053 for (i
= 0; i
< raid_disks
; i
++)
10056 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
10059 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
)) {
10060 dprintf("disk is faulty!!\n");
10064 if (sd
->disk
.raid_disk
>= raid_disks
|| sd
->disk
.raid_disk
< 0)
10067 dn
= map_dev(sd
->disk
.major
,
10068 sd
->disk
.minor
, 1);
10069 raid_fds
[sd
->disk
.raid_disk
] = dev_open(dn
, O_RDWR
);
10070 if (raid_fds
[sd
->disk
.raid_disk
] < 0) {
10071 pr_err("cannot open component\n");
10076 /* check if maximum array degradation level is not exceeded
10078 if ((raid_disks
- opened
) >
10079 imsm_get_allowed_degradation(info
->new_level
, raid_disks
,
10081 pr_err("Not enough disks can be opened.\n");
10082 close_targets(raid_fds
, raid_disks
);
10088 /*******************************************************************************
10089 * Function: validate_container_imsm
10090 * Description: This routine validates container after assemble,
10091 * eg. if devices in container are under the same controller.
10094 * info : linked list with info about devices used in array
10098 ******************************************************************************/
10099 int validate_container_imsm(struct mdinfo
*info
)
10101 if (check_env("IMSM_NO_PLATFORM"))
10104 struct sys_dev
*idev
;
10105 struct sys_dev
*hba
= NULL
;
10106 struct sys_dev
*intel_devices
= find_intel_devices();
10107 char *dev_path
= devt_to_devpath(makedev(info
->disk
.major
,
10108 info
->disk
.minor
));
10110 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
10111 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
10120 pr_err("WARNING - Cannot detect HBA for device %s!\n",
10121 devid2kname(makedev(info
->disk
.major
, info
->disk
.minor
)));
10125 const struct imsm_orom
*orom
= get_orom_by_device_id(hba
->dev_id
);
10126 struct mdinfo
*dev
;
10128 for (dev
= info
->next
; dev
; dev
= dev
->next
) {
10129 dev_path
= devt_to_devpath(makedev(dev
->disk
.major
, dev
->disk
.minor
));
10131 struct sys_dev
*hba2
= NULL
;
10132 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
10133 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
10141 const struct imsm_orom
*orom2
= hba2
== NULL
? NULL
:
10142 get_orom_by_device_id(hba2
->dev_id
);
10144 if (hba2
&& hba
->type
!= hba2
->type
) {
10145 pr_err("WARNING - HBAs of devices do not match %s != %s\n",
10146 get_sys_dev_type(hba
->type
), get_sys_dev_type(hba2
->type
));
10150 if (orom
!= orom2
) {
10151 pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n"
10152 " This operation is not supported and can lead to data loss.\n");
10157 pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n"
10158 " This operation is not supported and can lead to data loss.\n");
10166 /*******************************************************************************
10167 * Function: imsm_record_badblock
10168 * Description: This routine stores new bad block record in BBM log
10171 * a : array containing a bad block
10172 * slot : disk number containing a bad block
10173 * sector : bad block sector
10174 * length : bad block sectors range
10178 ******************************************************************************/
10179 static int imsm_record_badblock(struct active_array
*a
, int slot
,
10180 unsigned long long sector
, int length
)
10182 struct intel_super
*super
= a
->container
->sb
;
10186 ord
= imsm_disk_slot_to_ord(a
, slot
);
10190 ret
= record_new_badblock(super
->bbm_log
, ord_to_idx(ord
), sector
,
10193 super
->updates_pending
++;
10197 /*******************************************************************************
10198 * Function: imsm_clear_badblock
10199 * Description: This routine clears bad block record from BBM log
10202 * a : array containing a bad block
10203 * slot : disk number containing a bad block
10204 * sector : bad block sector
10205 * length : bad block sectors range
10209 ******************************************************************************/
10210 static int imsm_clear_badblock(struct active_array
*a
, int slot
,
10211 unsigned long long sector
, int length
)
10213 struct intel_super
*super
= a
->container
->sb
;
10217 ord
= imsm_disk_slot_to_ord(a
, slot
);
10221 ret
= clear_badblock(super
->bbm_log
, ord_to_idx(ord
), sector
, length
);
10223 super
->updates_pending
++;
10227 /*******************************************************************************
10228 * Function: imsm_get_badblocks
10229 * Description: This routine get list of bad blocks for an array
10233 * slot : disk number
10235 * bb : structure containing bad blocks
10237 ******************************************************************************/
10238 static struct md_bb
*imsm_get_badblocks(struct active_array
*a
, int slot
)
10240 int inst
= a
->info
.container_member
;
10241 struct intel_super
*super
= a
->container
->sb
;
10242 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
10243 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
10246 ord
= imsm_disk_slot_to_ord(a
, slot
);
10250 get_volume_badblocks(super
->bbm_log
, ord_to_idx(ord
), pba_of_lba0(map
),
10251 blocks_per_member(map
), &super
->bb
);
10255 /*******************************************************************************
10256 * Function: examine_badblocks_imsm
10257 * Description: Prints list of bad blocks on a disk to the standard output
10260 * st : metadata handler
10261 * fd : open file descriptor for device
10262 * devname : device name
10266 ******************************************************************************/
10267 static int examine_badblocks_imsm(struct supertype
*st
, int fd
, char *devname
)
10269 struct intel_super
*super
= st
->sb
;
10270 struct bbm_log
*log
= super
->bbm_log
;
10271 struct dl
*d
= NULL
;
10274 for (d
= super
->disks
; d
; d
= d
->next
) {
10275 if (strcmp(d
->devname
, devname
) == 0)
10279 if ((d
== NULL
) || (d
->index
< 0)) { /* serial mismatch probably */
10280 pr_err("%s doesn't appear to be part of a raid array\n",
10287 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[0];
10289 for (i
= 0; i
< log
->entry_count
; i
++) {
10290 if (entry
[i
].disk_ordinal
== d
->index
) {
10291 unsigned long long sector
= __le48_to_cpu(
10292 &entry
[i
].defective_block_start
);
10293 int cnt
= entry
[i
].marked_count
+ 1;
10296 printf("Bad-blocks on %s:\n", devname
);
10300 printf("%20llu for %d sectors\n", sector
, cnt
);
10306 printf("No bad-blocks list configured on %s\n", devname
);
10310 /*******************************************************************************
10311 * Function: init_migr_record_imsm
10312 * Description: Function inits imsm migration record
10314 * super : imsm internal array info
10315 * dev : device under migration
10316 * info : general array info to find the smallest device
10319 ******************************************************************************/
10320 void init_migr_record_imsm(struct supertype
*st
, struct imsm_dev
*dev
,
10321 struct mdinfo
*info
)
10323 struct intel_super
*super
= st
->sb
;
10324 struct migr_record
*migr_rec
= super
->migr_rec
;
10325 int new_data_disks
;
10326 unsigned long long dsize
, dev_sectors
;
10327 long long unsigned min_dev_sectors
= -1LLU;
10331 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
10332 struct imsm_map
*map_src
= get_imsm_map(dev
, MAP_1
);
10333 unsigned long long num_migr_units
;
10334 unsigned long long array_blocks
;
10336 memset(migr_rec
, 0, sizeof(struct migr_record
));
10337 migr_rec
->family_num
= __cpu_to_le32(super
->anchor
->family_num
);
10339 /* only ascending reshape supported now */
10340 migr_rec
->ascending_migr
= __cpu_to_le32(1);
10342 migr_rec
->dest_depth_per_unit
= GEN_MIGR_AREA_SIZE
/
10343 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
10344 migr_rec
->dest_depth_per_unit
*=
10345 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
10346 new_data_disks
= imsm_num_data_members(dev
, MAP_0
);
10347 migr_rec
->blocks_per_unit
=
10348 __cpu_to_le32(migr_rec
->dest_depth_per_unit
* new_data_disks
);
10349 migr_rec
->dest_depth_per_unit
=
10350 __cpu_to_le32(migr_rec
->dest_depth_per_unit
);
10351 array_blocks
= info
->component_size
* new_data_disks
;
10353 array_blocks
/ __le32_to_cpu(migr_rec
->blocks_per_unit
);
10355 if (array_blocks
% __le32_to_cpu(migr_rec
->blocks_per_unit
))
10357 migr_rec
->num_migr_units
= __cpu_to_le32(num_migr_units
);
10359 migr_rec
->post_migr_vol_cap
= dev
->size_low
;
10360 migr_rec
->post_migr_vol_cap_hi
= dev
->size_high
;
10362 /* Find the smallest dev */
10363 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
10364 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
10365 fd
= dev_open(nm
, O_RDONLY
);
10368 get_dev_size(fd
, NULL
, &dsize
);
10369 dev_sectors
= dsize
/ 512;
10370 if (dev_sectors
< min_dev_sectors
)
10371 min_dev_sectors
= dev_sectors
;
10374 migr_rec
->ckpt_area_pba
= __cpu_to_le32(min_dev_sectors
-
10375 RAID_DISK_RESERVED_BLOCKS_IMSM_HI
);
10377 write_imsm_migr_rec(st
);
10382 /*******************************************************************************
10383 * Function: save_backup_imsm
10384 * Description: Function saves critical data stripes to Migration Copy Area
10385 * and updates the current migration unit status.
10386 * Use restore_stripes() to form a destination stripe,
10387 * and to write it to the Copy Area.
10389 * st : supertype information
10390 * dev : imsm device that backup is saved for
10391 * info : general array info
10392 * buf : input buffer
10393 * length : length of data to backup (blocks_per_unit)
10397 ******************************************************************************/
10398 int save_backup_imsm(struct supertype
*st
,
10399 struct imsm_dev
*dev
,
10400 struct mdinfo
*info
,
10405 struct intel_super
*super
= st
->sb
;
10406 unsigned long long *target_offsets
;
10409 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
10410 int new_disks
= map_dest
->num_members
;
10411 int dest_layout
= 0;
10413 unsigned long long start
;
10414 int data_disks
= imsm_num_data_members(dev
, MAP_0
);
10416 targets
= xmalloc(new_disks
* sizeof(int));
10418 for (i
= 0; i
< new_disks
; i
++)
10421 target_offsets
= xcalloc(new_disks
, sizeof(unsigned long long));
10423 start
= info
->reshape_progress
* 512;
10424 for (i
= 0; i
< new_disks
; i
++) {
10425 target_offsets
[i
] = (unsigned long long)
10426 __le32_to_cpu(super
->migr_rec
->ckpt_area_pba
) * 512;
10427 /* move back copy area adderss, it will be moved forward
10428 * in restore_stripes() using start input variable
10430 target_offsets
[i
] -= start
/data_disks
;
10433 if (open_backup_targets(info
, new_disks
, targets
,
10437 dest_layout
= imsm_level_to_layout(map_dest
->raid_level
);
10438 dest_chunk
= __le16_to_cpu(map_dest
->blocks_per_strip
) * 512;
10440 if (restore_stripes(targets
, /* list of dest devices */
10441 target_offsets
, /* migration record offsets */
10444 map_dest
->raid_level
,
10446 -1, /* source backup file descriptor */
10447 0, /* input buf offset
10448 * always 0 buf is already offseted */
10452 pr_err("Error restoring stripes\n");
10460 close_targets(targets
, new_disks
);
10463 free(target_offsets
);
10468 /*******************************************************************************
10469 * Function: save_checkpoint_imsm
10470 * Description: Function called for current unit status update
10471 * in the migration record. It writes it to disk.
10473 * super : imsm internal array info
10474 * info : general array info
10478 * 2: failure, means no valid migration record
10479 * / no general migration in progress /
10480 ******************************************************************************/
10481 int save_checkpoint_imsm(struct supertype
*st
, struct mdinfo
*info
, int state
)
10483 struct intel_super
*super
= st
->sb
;
10484 unsigned long long blocks_per_unit
;
10485 unsigned long long curr_migr_unit
;
10487 if (load_imsm_migr_rec(super
, info
) != 0) {
10488 dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n");
10492 blocks_per_unit
= __le32_to_cpu(super
->migr_rec
->blocks_per_unit
);
10493 if (blocks_per_unit
== 0) {
10494 dprintf("imsm: no migration in progress.\n");
10497 curr_migr_unit
= info
->reshape_progress
/ blocks_per_unit
;
10498 /* check if array is alligned to copy area
10499 * if it is not alligned, add one to current migration unit value
10500 * this can happend on array reshape finish only
10502 if (info
->reshape_progress
% blocks_per_unit
)
10505 super
->migr_rec
->curr_migr_unit
=
10506 __cpu_to_le32(curr_migr_unit
);
10507 super
->migr_rec
->rec_status
= __cpu_to_le32(state
);
10508 super
->migr_rec
->dest_1st_member_lba
=
10509 __cpu_to_le32(curr_migr_unit
*
10510 __le32_to_cpu(super
->migr_rec
->dest_depth_per_unit
));
10511 if (write_imsm_migr_rec(st
) < 0) {
10512 dprintf("imsm: Cannot write migration record outside backup area\n");
10519 /*******************************************************************************
10520 * Function: recover_backup_imsm
10521 * Description: Function recovers critical data from the Migration Copy Area
10522 * while assembling an array.
10524 * super : imsm internal array info
10525 * info : general array info
10527 * 0 : success (or there is no data to recover)
10529 ******************************************************************************/
10530 int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
)
10532 struct intel_super
*super
= st
->sb
;
10533 struct migr_record
*migr_rec
= super
->migr_rec
;
10534 struct imsm_map
*map_dest
;
10535 struct intel_dev
*id
= NULL
;
10536 unsigned long long read_offset
;
10537 unsigned long long write_offset
;
10539 int *targets
= NULL
;
10540 int new_disks
, i
, err
;
10543 unsigned int sector_size
= super
->sector_size
;
10544 unsigned long curr_migr_unit
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
10545 unsigned long num_migr_units
= __le32_to_cpu(migr_rec
->num_migr_units
);
10547 int skipped_disks
= 0;
10549 err
= sysfs_get_str(info
, NULL
, "array_state", (char *)buffer
, 20);
10553 /* recover data only during assemblation */
10554 if (strncmp(buffer
, "inactive", 8) != 0)
10556 /* no data to recover */
10557 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
10559 if (curr_migr_unit
>= num_migr_units
)
10562 /* find device during reshape */
10563 for (id
= super
->devlist
; id
; id
= id
->next
)
10564 if (is_gen_migration(id
->dev
))
10569 map_dest
= get_imsm_map(id
->dev
, MAP_0
);
10570 new_disks
= map_dest
->num_members
;
10572 read_offset
= (unsigned long long)
10573 __le32_to_cpu(migr_rec
->ckpt_area_pba
) * 512;
10575 write_offset
= ((unsigned long long)
10576 __le32_to_cpu(migr_rec
->dest_1st_member_lba
) +
10577 pba_of_lba0(map_dest
)) * 512;
10579 unit_len
= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
10580 if (posix_memalign((void **)&buf
, sector_size
, unit_len
) != 0)
10582 targets
= xcalloc(new_disks
, sizeof(int));
10584 if (open_backup_targets(info
, new_disks
, targets
, super
, id
->dev
)) {
10585 pr_err("Cannot open some devices belonging to array.\n");
10589 for (i
= 0; i
< new_disks
; i
++) {
10590 if (targets
[i
] < 0) {
10594 if (lseek64(targets
[i
], read_offset
, SEEK_SET
) < 0) {
10595 pr_err("Cannot seek to block: %s\n",
10600 if ((unsigned)read(targets
[i
], buf
, unit_len
) != unit_len
) {
10601 pr_err("Cannot read copy area block: %s\n",
10606 if (lseek64(targets
[i
], write_offset
, SEEK_SET
) < 0) {
10607 pr_err("Cannot seek to block: %s\n",
10612 if ((unsigned)write(targets
[i
], buf
, unit_len
) != unit_len
) {
10613 pr_err("Cannot restore block: %s\n",
10620 if (skipped_disks
> imsm_get_allowed_degradation(info
->new_level
,
10624 pr_err("Cannot restore data from backup. Too many failed disks\n");
10628 if (save_checkpoint_imsm(st
, info
, UNIT_SRC_NORMAL
)) {
10629 /* ignore error == 2, this can mean end of reshape here
10631 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n");
10637 for (i
= 0; i
< new_disks
; i
++)
10646 static char disk_by_path
[] = "/dev/disk/by-path/";
10648 static const char *imsm_get_disk_controller_domain(const char *path
)
10650 char disk_path
[PATH_MAX
];
10654 strcpy(disk_path
, disk_by_path
);
10655 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
10656 if (stat(disk_path
, &st
) == 0) {
10657 struct sys_dev
* hba
;
10660 path
= devt_to_devpath(st
.st_rdev
);
10663 hba
= find_disk_attached_hba(-1, path
);
10664 if (hba
&& hba
->type
== SYS_DEV_SAS
)
10666 else if (hba
&& hba
->type
== SYS_DEV_SATA
)
10670 dprintf("path: %s hba: %s attached: %s\n",
10671 path
, (hba
) ? hba
->path
: "NULL", drv
);
10677 static char *imsm_find_array_devnm_by_subdev(int subdev
, char *container
)
10679 static char devnm
[32];
10680 char subdev_name
[20];
10681 struct mdstat_ent
*mdstat
;
10683 sprintf(subdev_name
, "%d", subdev
);
10684 mdstat
= mdstat_by_subdev(subdev_name
, container
);
10688 strcpy(devnm
, mdstat
->devnm
);
10689 free_mdstat(mdstat
);
10693 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
10694 struct geo_params
*geo
,
10695 int *old_raid_disks
,
10698 /* currently we only support increasing the number of devices
10699 * for a container. This increases the number of device for each
10700 * member array. They must all be RAID0 or RAID5.
10703 struct mdinfo
*info
, *member
;
10704 int devices_that_can_grow
= 0;
10706 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st
->devnm
);
10708 if (geo
->size
> 0 ||
10709 geo
->level
!= UnSet
||
10710 geo
->layout
!= UnSet
||
10711 geo
->chunksize
!= 0 ||
10712 geo
->raid_disks
== UnSet
) {
10713 dprintf("imsm: Container operation is allowed for raid disks number change only.\n");
10717 if (direction
== ROLLBACK_METADATA_CHANGES
) {
10718 dprintf("imsm: Metadata changes rollback is not supported for container operation.\n");
10722 info
= container_content_imsm(st
, NULL
);
10723 for (member
= info
; member
; member
= member
->next
) {
10726 dprintf("imsm: checking device_num: %i\n",
10727 member
->container_member
);
10729 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
10730 /* we work on container for Online Capacity Expansion
10731 * only so raid_disks has to grow
10733 dprintf("imsm: for container operation raid disks increase is required\n");
10737 if (info
->array
.level
!= 0 && info
->array
.level
!= 5) {
10738 /* we cannot use this container with other raid level
10740 dprintf("imsm: for container operation wrong raid level (%i) detected\n",
10741 info
->array
.level
);
10744 /* check for platform support
10745 * for this raid level configuration
10747 struct intel_super
*super
= st
->sb
;
10748 if (!is_raid_level_supported(super
->orom
,
10749 member
->array
.level
,
10750 geo
->raid_disks
)) {
10751 dprintf("platform does not support raid%d with %d disk%s\n",
10754 geo
->raid_disks
> 1 ? "s" : "");
10757 /* check if component size is aligned to chunk size
10759 if (info
->component_size
%
10760 (info
->array
.chunk_size
/512)) {
10761 dprintf("Component size is not aligned to chunk size\n");
10766 if (*old_raid_disks
&&
10767 info
->array
.raid_disks
!= *old_raid_disks
)
10769 *old_raid_disks
= info
->array
.raid_disks
;
10771 /* All raid5 and raid0 volumes in container
10772 * have to be ready for Online Capacity Expansion
10773 * so they need to be assembled. We have already
10774 * checked that no recovery etc is happening.
10776 result
= imsm_find_array_devnm_by_subdev(member
->container_member
,
10777 st
->container_devnm
);
10778 if (result
== NULL
) {
10779 dprintf("imsm: cannot find array\n");
10782 devices_that_can_grow
++;
10785 if (!member
&& devices_that_can_grow
)
10789 dprintf("Container operation allowed\n");
10791 dprintf("Error: %i\n", ret_val
);
10796 /* Function: get_spares_for_grow
10797 * Description: Allocates memory and creates list of spare devices
10798 * avaliable in container. Checks if spare drive size is acceptable.
10799 * Parameters: Pointer to the supertype structure
10800 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
10803 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
10805 unsigned long long min_size
= min_acceptable_spare_size_imsm(st
);
10806 return container_choose_spares(st
, min_size
, NULL
, NULL
, NULL
, 0);
10809 /******************************************************************************
10810 * function: imsm_create_metadata_update_for_reshape
10811 * Function creates update for whole IMSM container.
10813 ******************************************************************************/
10814 static int imsm_create_metadata_update_for_reshape(
10815 struct supertype
*st
,
10816 struct geo_params
*geo
,
10817 int old_raid_disks
,
10818 struct imsm_update_reshape
**updatep
)
10820 struct intel_super
*super
= st
->sb
;
10821 struct imsm_super
*mpb
= super
->anchor
;
10822 int update_memory_size
;
10823 struct imsm_update_reshape
*u
;
10824 struct mdinfo
*spares
;
10827 struct mdinfo
*dev
;
10829 dprintf("(enter) raid_disks = %i\n", geo
->raid_disks
);
10831 delta_disks
= geo
->raid_disks
- old_raid_disks
;
10833 /* size of all update data without anchor */
10834 update_memory_size
= sizeof(struct imsm_update_reshape
);
10836 /* now add space for spare disks that we need to add. */
10837 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
10839 u
= xcalloc(1, update_memory_size
);
10840 u
->type
= update_reshape_container_disks
;
10841 u
->old_raid_disks
= old_raid_disks
;
10842 u
->new_raid_disks
= geo
->raid_disks
;
10844 /* now get spare disks list
10846 spares
= get_spares_for_grow(st
);
10849 || delta_disks
> spares
->array
.spare_disks
) {
10850 pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo
->dev_name
);
10855 /* we have got spares
10856 * update disk list in imsm_disk list table in anchor
10858 dprintf("imsm: %i spares are available.\n\n",
10859 spares
->array
.spare_disks
);
10861 dev
= spares
->devs
;
10862 for (i
= 0; i
< delta_disks
; i
++) {
10867 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
10869 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
10870 dl
->index
= mpb
->num_disks
;
10878 sysfs_free(spares
);
10880 dprintf("imsm: reshape update preparation :");
10881 if (i
== delta_disks
) {
10882 dprintf_cont(" OK\n");
10884 return update_memory_size
;
10887 dprintf_cont(" Error\n");
10892 /******************************************************************************
10893 * function: imsm_create_metadata_update_for_size_change()
10894 * Creates update for IMSM array for array size change.
10896 ******************************************************************************/
10897 static int imsm_create_metadata_update_for_size_change(
10898 struct supertype
*st
,
10899 struct geo_params
*geo
,
10900 struct imsm_update_size_change
**updatep
)
10902 struct intel_super
*super
= st
->sb
;
10903 int update_memory_size
;
10904 struct imsm_update_size_change
*u
;
10906 dprintf("(enter) New size = %llu\n", geo
->size
);
10908 /* size of all update data without anchor */
10909 update_memory_size
= sizeof(struct imsm_update_size_change
);
10911 u
= xcalloc(1, update_memory_size
);
10912 u
->type
= update_size_change
;
10913 u
->subdev
= super
->current_vol
;
10914 u
->new_size
= geo
->size
;
10916 dprintf("imsm: reshape update preparation : OK\n");
10919 return update_memory_size
;
10922 /******************************************************************************
10923 * function: imsm_create_metadata_update_for_migration()
10924 * Creates update for IMSM array.
10926 ******************************************************************************/
10927 static int imsm_create_metadata_update_for_migration(
10928 struct supertype
*st
,
10929 struct geo_params
*geo
,
10930 struct imsm_update_reshape_migration
**updatep
)
10932 struct intel_super
*super
= st
->sb
;
10933 int update_memory_size
;
10934 struct imsm_update_reshape_migration
*u
;
10935 struct imsm_dev
*dev
;
10936 int previous_level
= -1;
10938 dprintf("(enter) New Level = %i\n", geo
->level
);
10940 /* size of all update data without anchor */
10941 update_memory_size
= sizeof(struct imsm_update_reshape_migration
);
10943 u
= xcalloc(1, update_memory_size
);
10944 u
->type
= update_reshape_migration
;
10945 u
->subdev
= super
->current_vol
;
10946 u
->new_level
= geo
->level
;
10947 u
->new_layout
= geo
->layout
;
10948 u
->new_raid_disks
= u
->old_raid_disks
= geo
->raid_disks
;
10949 u
->new_disks
[0] = -1;
10950 u
->new_chunksize
= -1;
10952 dev
= get_imsm_dev(super
, u
->subdev
);
10954 struct imsm_map
*map
;
10956 map
= get_imsm_map(dev
, MAP_0
);
10958 int current_chunk_size
=
10959 __le16_to_cpu(map
->blocks_per_strip
) / 2;
10961 if (geo
->chunksize
!= current_chunk_size
) {
10962 u
->new_chunksize
= geo
->chunksize
/ 1024;
10963 dprintf("imsm: chunk size change from %i to %i\n",
10964 current_chunk_size
, u
->new_chunksize
);
10966 previous_level
= map
->raid_level
;
10969 if (geo
->level
== 5 && previous_level
== 0) {
10970 struct mdinfo
*spares
= NULL
;
10972 u
->new_raid_disks
++;
10973 spares
= get_spares_for_grow(st
);
10974 if (spares
== NULL
|| spares
->array
.spare_disks
< 1) {
10976 sysfs_free(spares
);
10977 update_memory_size
= 0;
10978 pr_err("cannot get spare device for requested migration\n");
10981 sysfs_free(spares
);
10983 dprintf("imsm: reshape update preparation : OK\n");
10986 return update_memory_size
;
10989 static void imsm_update_metadata_locally(struct supertype
*st
,
10990 void *buf
, int len
)
10992 struct metadata_update mu
;
10997 mu
.space_list
= NULL
;
10999 if (imsm_prepare_update(st
, &mu
))
11000 imsm_process_update(st
, &mu
);
11002 while (mu
.space_list
) {
11003 void **space
= mu
.space_list
;
11004 mu
.space_list
= *space
;
11009 /***************************************************************************
11010 * Function: imsm_analyze_change
11011 * Description: Function analyze change for single volume
11012 * and validate if transition is supported
11013 * Parameters: Geometry parameters, supertype structure,
11014 * metadata change direction (apply/rollback)
11015 * Returns: Operation type code on success, -1 if fail
11016 ****************************************************************************/
11017 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
11018 struct geo_params
*geo
,
11021 struct mdinfo info
;
11023 int check_devs
= 0;
11025 /* number of added/removed disks in operation result */
11026 int devNumChange
= 0;
11027 /* imsm compatible layout value for array geometry verification */
11028 int imsm_layout
= -1;
11030 struct imsm_dev
*dev
;
11031 struct intel_super
*super
;
11032 unsigned long long current_size
;
11033 unsigned long long free_size
;
11034 unsigned long long max_size
;
11037 getinfo_super_imsm_volume(st
, &info
, NULL
);
11038 if (geo
->level
!= info
.array
.level
&& geo
->level
>= 0 &&
11039 geo
->level
!= UnSet
) {
11040 switch (info
.array
.level
) {
11042 if (geo
->level
== 5) {
11043 change
= CH_MIGRATION
;
11044 if (geo
->layout
!= ALGORITHM_LEFT_ASYMMETRIC
) {
11045 pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n");
11047 goto analyse_change_exit
;
11049 imsm_layout
= geo
->layout
;
11051 devNumChange
= 1; /* parity disk added */
11052 } else if (geo
->level
== 10) {
11053 change
= CH_TAKEOVER
;
11055 devNumChange
= 2; /* two mirrors added */
11056 imsm_layout
= 0x102; /* imsm supported layout */
11061 if (geo
->level
== 0) {
11062 change
= CH_TAKEOVER
;
11064 devNumChange
= -(geo
->raid_disks
/2);
11065 imsm_layout
= 0; /* imsm raid0 layout */
11069 if (change
== -1) {
11070 pr_err("Error. Level Migration from %d to %d not supported!\n",
11071 info
.array
.level
, geo
->level
);
11072 goto analyse_change_exit
;
11075 geo
->level
= info
.array
.level
;
11077 if (geo
->layout
!= info
.array
.layout
&&
11078 (geo
->layout
!= UnSet
&& geo
->layout
!= -1)) {
11079 change
= CH_MIGRATION
;
11080 if (info
.array
.layout
== 0 && info
.array
.level
== 5 &&
11081 geo
->layout
== 5) {
11082 /* reshape 5 -> 4 */
11083 } else if (info
.array
.layout
== 5 && info
.array
.level
== 5 &&
11084 geo
->layout
== 0) {
11085 /* reshape 4 -> 5 */
11089 pr_err("Error. Layout Migration from %d to %d not supported!\n",
11090 info
.array
.layout
, geo
->layout
);
11092 goto analyse_change_exit
;
11095 geo
->layout
= info
.array
.layout
;
11096 if (imsm_layout
== -1)
11097 imsm_layout
= info
.array
.layout
;
11100 if (geo
->chunksize
> 0 && geo
->chunksize
!= UnSet
&&
11101 geo
->chunksize
!= info
.array
.chunk_size
) {
11102 if (info
.array
.level
== 10) {
11103 pr_err("Error. Chunk size change for RAID 10 is not supported.\n");
11105 goto analyse_change_exit
;
11106 } else if (info
.component_size
% (geo
->chunksize
/512)) {
11107 pr_err("New chunk size (%dK) does not evenly divide device size (%lluk). Aborting...\n",
11108 geo
->chunksize
/1024, info
.component_size
/2);
11110 goto analyse_change_exit
;
11112 change
= CH_MIGRATION
;
11114 geo
->chunksize
= info
.array
.chunk_size
;
11117 chunk
= geo
->chunksize
/ 1024;
11120 dev
= get_imsm_dev(super
, super
->current_vol
);
11121 data_disks
= imsm_num_data_members(dev
, MAP_0
);
11122 /* compute current size per disk member
11124 current_size
= info
.custom_array_size
/ data_disks
;
11126 if (geo
->size
> 0 && geo
->size
!= MAX_SIZE
) {
11127 /* align component size
11129 geo
->size
= imsm_component_size_aligment_check(
11130 get_imsm_raid_level(dev
->vol
.map
),
11131 chunk
* 1024, super
->sector_size
,
11133 if (geo
->size
== 0) {
11134 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is 0).\n",
11136 goto analyse_change_exit
;
11140 if (current_size
!= geo
->size
&& geo
->size
> 0) {
11141 if (change
!= -1) {
11142 pr_err("Error. Size change should be the only one at a time.\n");
11144 goto analyse_change_exit
;
11146 if ((super
->current_vol
+ 1) != super
->anchor
->num_raid_devs
) {
11147 pr_err("Error. The last volume in container can be expanded only (%i/%s).\n",
11148 super
->current_vol
, st
->devnm
);
11149 goto analyse_change_exit
;
11151 /* check the maximum available size
11153 rv
= imsm_get_free_size(st
, dev
->vol
.map
->num_members
,
11154 0, chunk
, &free_size
);
11156 /* Cannot find maximum available space
11160 max_size
= free_size
+ current_size
;
11161 /* align component size
11163 max_size
= imsm_component_size_aligment_check(
11164 get_imsm_raid_level(dev
->vol
.map
),
11165 chunk
* 1024, super
->sector_size
,
11168 if (geo
->size
== MAX_SIZE
) {
11169 /* requested size change to the maximum available size
11171 if (max_size
== 0) {
11172 pr_err("Error. Cannot find maximum available space.\n");
11174 goto analyse_change_exit
;
11176 geo
->size
= max_size
;
11179 if (direction
== ROLLBACK_METADATA_CHANGES
) {
11180 /* accept size for rollback only
11183 /* round size due to metadata compatibility
11185 geo
->size
= (geo
->size
>> SECT_PER_MB_SHIFT
)
11186 << SECT_PER_MB_SHIFT
;
11187 dprintf("Prepare update for size change to %llu\n",
11189 if (current_size
>= geo
->size
) {
11190 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is %llu).\n",
11191 current_size
, geo
->size
);
11192 goto analyse_change_exit
;
11194 if (max_size
&& geo
->size
> max_size
) {
11195 pr_err("Error. Requested size is larger than maximum available size (maximum available size is %llu, requested size /rounded/ is %llu).\n",
11196 max_size
, geo
->size
);
11197 goto analyse_change_exit
;
11200 geo
->size
*= data_disks
;
11201 geo
->raid_disks
= dev
->vol
.map
->num_members
;
11202 change
= CH_ARRAY_SIZE
;
11204 if (!validate_geometry_imsm(st
,
11207 geo
->raid_disks
+ devNumChange
,
11209 geo
->size
, INVALID_SECTORS
,
11210 0, 0, info
.consistency_policy
, 1))
11214 struct intel_super
*super
= st
->sb
;
11215 struct imsm_super
*mpb
= super
->anchor
;
11217 if (mpb
->num_raid_devs
> 1) {
11218 pr_err("Error. Cannot perform operation on %s- for this operation it MUST be single array in container\n",
11224 analyse_change_exit
:
11225 if (direction
== ROLLBACK_METADATA_CHANGES
&&
11226 (change
== CH_MIGRATION
|| change
== CH_TAKEOVER
)) {
11227 dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n");
11233 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
11235 struct intel_super
*super
= st
->sb
;
11236 struct imsm_update_takeover
*u
;
11238 u
= xmalloc(sizeof(struct imsm_update_takeover
));
11240 u
->type
= update_takeover
;
11241 u
->subarray
= super
->current_vol
;
11243 /* 10->0 transition */
11244 if (geo
->level
== 0)
11245 u
->direction
= R10_TO_R0
;
11247 /* 0->10 transition */
11248 if (geo
->level
== 10)
11249 u
->direction
= R0_TO_R10
;
11251 /* update metadata locally */
11252 imsm_update_metadata_locally(st
, u
,
11253 sizeof(struct imsm_update_takeover
));
11254 /* and possibly remotely */
11255 if (st
->update_tail
)
11256 append_metadata_update(st
, u
,
11257 sizeof(struct imsm_update_takeover
));
11264 static int imsm_reshape_super(struct supertype
*st
, unsigned long long size
,
11266 int layout
, int chunksize
, int raid_disks
,
11267 int delta_disks
, char *backup
, char *dev
,
11268 int direction
, int verbose
)
11271 struct geo_params geo
;
11273 dprintf("(enter)\n");
11275 memset(&geo
, 0, sizeof(struct geo_params
));
11277 geo
.dev_name
= dev
;
11278 strcpy(geo
.devnm
, st
->devnm
);
11281 geo
.layout
= layout
;
11282 geo
.chunksize
= chunksize
;
11283 geo
.raid_disks
= raid_disks
;
11284 if (delta_disks
!= UnSet
)
11285 geo
.raid_disks
+= delta_disks
;
11287 dprintf("for level : %i\n", geo
.level
);
11288 dprintf("for raid_disks : %i\n", geo
.raid_disks
);
11290 if (experimental() == 0)
11293 if (strcmp(st
->container_devnm
, st
->devnm
) == 0) {
11294 /* On container level we can only increase number of devices. */
11295 dprintf("imsm: info: Container operation\n");
11296 int old_raid_disks
= 0;
11298 if (imsm_reshape_is_allowed_on_container(
11299 st
, &geo
, &old_raid_disks
, direction
)) {
11300 struct imsm_update_reshape
*u
= NULL
;
11303 len
= imsm_create_metadata_update_for_reshape(
11304 st
, &geo
, old_raid_disks
, &u
);
11307 dprintf("imsm: Cannot prepare update\n");
11308 goto exit_imsm_reshape_super
;
11312 /* update metadata locally */
11313 imsm_update_metadata_locally(st
, u
, len
);
11314 /* and possibly remotely */
11315 if (st
->update_tail
)
11316 append_metadata_update(st
, u
, len
);
11321 pr_err("(imsm) Operation is not allowed on this container\n");
11324 /* On volume level we support following operations
11325 * - takeover: raid10 -> raid0; raid0 -> raid10
11326 * - chunk size migration
11327 * - migration: raid5 -> raid0; raid0 -> raid5
11329 struct intel_super
*super
= st
->sb
;
11330 struct intel_dev
*dev
= super
->devlist
;
11332 dprintf("imsm: info: Volume operation\n");
11333 /* find requested device */
11336 imsm_find_array_devnm_by_subdev(
11337 dev
->index
, st
->container_devnm
);
11338 if (devnm
&& strcmp(devnm
, geo
.devnm
) == 0)
11343 pr_err("Cannot find %s (%s) subarray\n",
11344 geo
.dev_name
, geo
.devnm
);
11345 goto exit_imsm_reshape_super
;
11347 super
->current_vol
= dev
->index
;
11348 change
= imsm_analyze_change(st
, &geo
, direction
);
11351 ret_val
= imsm_takeover(st
, &geo
);
11353 case CH_MIGRATION
: {
11354 struct imsm_update_reshape_migration
*u
= NULL
;
11356 imsm_create_metadata_update_for_migration(
11359 dprintf("imsm: Cannot prepare update\n");
11363 /* update metadata locally */
11364 imsm_update_metadata_locally(st
, u
, len
);
11365 /* and possibly remotely */
11366 if (st
->update_tail
)
11367 append_metadata_update(st
, u
, len
);
11372 case CH_ARRAY_SIZE
: {
11373 struct imsm_update_size_change
*u
= NULL
;
11375 imsm_create_metadata_update_for_size_change(
11378 dprintf("imsm: Cannot prepare update\n");
11382 /* update metadata locally */
11383 imsm_update_metadata_locally(st
, u
, len
);
11384 /* and possibly remotely */
11385 if (st
->update_tail
)
11386 append_metadata_update(st
, u
, len
);
11396 exit_imsm_reshape_super
:
11397 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
11401 #define COMPLETED_OK 0
11402 #define COMPLETED_NONE 1
11403 #define COMPLETED_DELAYED 2
11405 static int read_completed(int fd
, unsigned long long *val
)
11410 ret
= sysfs_fd_get_str(fd
, buf
, 50);
11414 ret
= COMPLETED_OK
;
11415 if (strncmp(buf
, "none", 4) == 0) {
11416 ret
= COMPLETED_NONE
;
11417 } else if (strncmp(buf
, "delayed", 7) == 0) {
11418 ret
= COMPLETED_DELAYED
;
11421 *val
= strtoull(buf
, &ep
, 0);
11422 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))
11428 /*******************************************************************************
11429 * Function: wait_for_reshape_imsm
11430 * Description: Function writes new sync_max value and waits until
11431 * reshape process reach new position
11433 * sra : general array info
11434 * ndata : number of disks in new array's layout
11437 * 1 : there is no reshape in progress,
11439 ******************************************************************************/
11440 int wait_for_reshape_imsm(struct mdinfo
*sra
, int ndata
)
11442 int fd
= sysfs_get_fd(sra
, NULL
, "sync_completed");
11444 unsigned long long completed
;
11445 /* to_complete : new sync_max position */
11446 unsigned long long to_complete
= sra
->reshape_progress
;
11447 unsigned long long position_to_set
= to_complete
/ ndata
;
11450 dprintf("cannot open reshape_position\n");
11455 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
11457 dprintf("cannot read reshape_position (no reshape in progres)\n");
11466 if (completed
> position_to_set
) {
11467 dprintf("wrong next position to set %llu (%llu)\n",
11468 to_complete
, position_to_set
);
11472 dprintf("Position set: %llu\n", position_to_set
);
11473 if (sysfs_set_num(sra
, NULL
, "sync_max",
11474 position_to_set
) != 0) {
11475 dprintf("cannot set reshape position to %llu\n",
11484 int timeout
= 3000;
11486 sysfs_wait(fd
, &timeout
);
11487 if (sysfs_get_str(sra
, NULL
, "sync_action",
11489 strncmp(action
, "reshape", 7) != 0) {
11490 if (strncmp(action
, "idle", 4) == 0)
11496 rc
= read_completed(fd
, &completed
);
11498 dprintf("cannot read reshape_position (in loop)\n");
11501 } else if (rc
== COMPLETED_NONE
)
11503 } while (completed
< position_to_set
);
11509 /*******************************************************************************
11510 * Function: check_degradation_change
11511 * Description: Check that array hasn't become failed.
11513 * info : for sysfs access
11514 * sources : source disks descriptors
11515 * degraded: previous degradation level
11517 * degradation level
11518 ******************************************************************************/
11519 int check_degradation_change(struct mdinfo
*info
,
11523 unsigned long long new_degraded
;
11526 rv
= sysfs_get_ll(info
, NULL
, "degraded", &new_degraded
);
11527 if (rv
== -1 || (new_degraded
!= (unsigned long long)degraded
)) {
11528 /* check each device to ensure it is still working */
11531 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
11532 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
11534 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
11537 if (sysfs_get_str(info
,
11538 sd
, "state", sbuf
, sizeof(sbuf
)) < 0 ||
11539 strstr(sbuf
, "faulty") ||
11540 strstr(sbuf
, "in_sync") == NULL
) {
11541 /* this device is dead */
11542 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
11543 if (sd
->disk
.raid_disk
>= 0 &&
11544 sources
[sd
->disk
.raid_disk
] >= 0) {
11546 sd
->disk
.raid_disk
]);
11547 sources
[sd
->disk
.raid_disk
] =
11556 return new_degraded
;
11559 /*******************************************************************************
11560 * Function: imsm_manage_reshape
11561 * Description: Function finds array under reshape and it manages reshape
11562 * process. It creates stripes backups (if required) and sets
11565 * afd : Backup handle (nattive) - not used
11566 * sra : general array info
11567 * reshape : reshape parameters - not used
11568 * st : supertype structure
11569 * blocks : size of critical section [blocks]
11570 * fds : table of source device descriptor
11571 * offsets : start of array (offest per devices)
11573 * destfd : table of destination device descriptor
11574 * destoffsets : table of destination offsets (per device)
11576 * 1 : success, reshape is done
11578 ******************************************************************************/
11579 static int imsm_manage_reshape(
11580 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
11581 struct supertype
*st
, unsigned long backup_blocks
,
11582 int *fds
, unsigned long long *offsets
,
11583 int dests
, int *destfd
, unsigned long long *destoffsets
)
11586 struct intel_super
*super
= st
->sb
;
11587 struct intel_dev
*dv
;
11588 unsigned int sector_size
= super
->sector_size
;
11589 struct imsm_dev
*dev
= NULL
;
11590 struct imsm_map
*map_src
;
11591 int migr_vol_qan
= 0;
11592 int ndata
, odata
; /* [bytes] */
11593 int chunk
; /* [bytes] */
11594 struct migr_record
*migr_rec
;
11596 unsigned int buf_size
; /* [bytes] */
11597 unsigned long long max_position
; /* array size [bytes] */
11598 unsigned long long next_step
; /* [blocks]/[bytes] */
11599 unsigned long long old_data_stripe_length
;
11600 unsigned long long start_src
; /* [bytes] */
11601 unsigned long long start
; /* [bytes] */
11602 unsigned long long start_buf_shift
; /* [bytes] */
11604 int source_layout
= 0;
11609 if (!fds
|| !offsets
)
11612 /* Find volume during the reshape */
11613 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
11614 if (dv
->dev
->vol
.migr_type
== MIGR_GEN_MIGR
11615 && dv
->dev
->vol
.migr_state
== 1) {
11620 /* Only one volume can migrate at the same time */
11621 if (migr_vol_qan
!= 1) {
11622 pr_err("%s", migr_vol_qan
?
11623 "Number of migrating volumes greater than 1\n" :
11624 "There is no volume during migrationg\n");
11628 map_src
= get_imsm_map(dev
, MAP_1
);
11629 if (map_src
== NULL
)
11632 ndata
= imsm_num_data_members(dev
, MAP_0
);
11633 odata
= imsm_num_data_members(dev
, MAP_1
);
11635 chunk
= __le16_to_cpu(map_src
->blocks_per_strip
) * 512;
11636 old_data_stripe_length
= odata
* chunk
;
11638 migr_rec
= super
->migr_rec
;
11640 /* initialize migration record for start condition */
11641 if (sra
->reshape_progress
== 0)
11642 init_migr_record_imsm(st
, dev
, sra
);
11644 if (__le32_to_cpu(migr_rec
->rec_status
) != UNIT_SRC_NORMAL
) {
11645 dprintf("imsm: cannot restart migration when data are present in copy area.\n");
11648 /* Save checkpoint to update migration record for current
11649 * reshape position (in md). It can be farther than current
11650 * reshape position in metadata.
11652 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
11653 /* ignore error == 2, this can mean end of reshape here
11655 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n");
11660 /* size for data */
11661 buf_size
= __le32_to_cpu(migr_rec
->blocks_per_unit
) * 512;
11662 /* extend buffer size for parity disk */
11663 buf_size
+= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
11664 /* add space for stripe aligment */
11665 buf_size
+= old_data_stripe_length
;
11666 if (posix_memalign((void **)&buf
, MAX_SECTOR_SIZE
, buf_size
)) {
11667 dprintf("imsm: Cannot allocate checkpoint buffer\n");
11671 max_position
= sra
->component_size
* ndata
;
11672 source_layout
= imsm_level_to_layout(map_src
->raid_level
);
11674 while (__le32_to_cpu(migr_rec
->curr_migr_unit
) <
11675 __le32_to_cpu(migr_rec
->num_migr_units
)) {
11676 /* current reshape position [blocks] */
11677 unsigned long long current_position
=
11678 __le32_to_cpu(migr_rec
->blocks_per_unit
)
11679 * __le32_to_cpu(migr_rec
->curr_migr_unit
);
11680 unsigned long long border
;
11682 /* Check that array hasn't become failed.
11684 degraded
= check_degradation_change(sra
, fds
, degraded
);
11685 if (degraded
> 1) {
11686 dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded
);
11690 next_step
= __le32_to_cpu(migr_rec
->blocks_per_unit
);
11692 if ((current_position
+ next_step
) > max_position
)
11693 next_step
= max_position
- current_position
;
11695 start
= current_position
* 512;
11697 /* align reading start to old geometry */
11698 start_buf_shift
= start
% old_data_stripe_length
;
11699 start_src
= start
- start_buf_shift
;
11701 border
= (start_src
/ odata
) - (start
/ ndata
);
11703 if (border
<= __le32_to_cpu(migr_rec
->dest_depth_per_unit
)) {
11704 /* save critical stripes to buf
11705 * start - start address of current unit
11706 * to backup [bytes]
11707 * start_src - start address of current unit
11708 * to backup alligned to source array
11711 unsigned long long next_step_filler
;
11712 unsigned long long copy_length
= next_step
* 512;
11714 /* allign copy area length to stripe in old geometry */
11715 next_step_filler
= ((copy_length
+ start_buf_shift
)
11716 % old_data_stripe_length
);
11717 if (next_step_filler
)
11718 next_step_filler
= (old_data_stripe_length
11719 - next_step_filler
);
11720 dprintf("save_stripes() parameters: start = %llu,\tstart_src = %llu,\tnext_step*512 = %llu,\tstart_in_buf_shift = %llu,\tnext_step_filler = %llu\n",
11721 start
, start_src
, copy_length
,
11722 start_buf_shift
, next_step_filler
);
11724 if (save_stripes(fds
, offsets
, map_src
->num_members
,
11725 chunk
, map_src
->raid_level
,
11726 source_layout
, 0, NULL
, start_src
,
11728 next_step_filler
+ start_buf_shift
,
11730 dprintf("imsm: Cannot save stripes to buffer\n");
11733 /* Convert data to destination format and store it
11734 * in backup general migration area
11736 if (save_backup_imsm(st
, dev
, sra
,
11737 buf
+ start_buf_shift
, copy_length
)) {
11738 dprintf("imsm: Cannot save stripes to target devices\n");
11741 if (save_checkpoint_imsm(st
, sra
,
11742 UNIT_SRC_IN_CP_AREA
)) {
11743 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n");
11747 /* set next step to use whole border area */
11748 border
/= next_step
;
11750 next_step
*= border
;
11752 /* When data backed up, checkpoint stored,
11753 * kick the kernel to reshape unit of data
11755 next_step
= next_step
+ sra
->reshape_progress
;
11756 /* limit next step to array max position */
11757 if (next_step
> max_position
)
11758 next_step
= max_position
;
11759 sysfs_set_num(sra
, NULL
, "suspend_lo", sra
->reshape_progress
);
11760 sysfs_set_num(sra
, NULL
, "suspend_hi", next_step
);
11761 sra
->reshape_progress
= next_step
;
11763 /* wait until reshape finish */
11764 if (wait_for_reshape_imsm(sra
, ndata
)) {
11765 dprintf("wait_for_reshape_imsm returned error!\n");
11771 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
11772 /* ignore error == 2, this can mean end of reshape here
11774 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n");
11780 /* clear migr_rec on disks after successful migration */
11783 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SECTORS
*sector_size
);
11784 for (d
= super
->disks
; d
; d
= d
->next
) {
11785 if (d
->index
< 0 || is_failed(&d
->disk
))
11787 unsigned long long dsize
;
11789 get_dev_size(d
->fd
, NULL
, &dsize
);
11790 if (lseek64(d
->fd
, dsize
- MIGR_REC_SECTOR_POSITION
*sector_size
,
11792 if ((unsigned int)write(d
->fd
, super
->migr_rec_buf
,
11793 MIGR_REC_BUF_SECTORS
*sector_size
) !=
11794 MIGR_REC_BUF_SECTORS
*sector_size
)
11795 perror("Write migr_rec failed");
11799 /* return '1' if done */
11803 /* See Grow.c: abort_reshape() for further explanation */
11804 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
11805 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
11806 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
11811 struct superswitch super_imsm
= {
11812 .examine_super
= examine_super_imsm
,
11813 .brief_examine_super
= brief_examine_super_imsm
,
11814 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
11815 .export_examine_super
= export_examine_super_imsm
,
11816 .detail_super
= detail_super_imsm
,
11817 .brief_detail_super
= brief_detail_super_imsm
,
11818 .write_init_super
= write_init_super_imsm
,
11819 .validate_geometry
= validate_geometry_imsm
,
11820 .add_to_super
= add_to_super_imsm
,
11821 .remove_from_super
= remove_from_super_imsm
,
11822 .detail_platform
= detail_platform_imsm
,
11823 .export_detail_platform
= export_detail_platform_imsm
,
11824 .kill_subarray
= kill_subarray_imsm
,
11825 .update_subarray
= update_subarray_imsm
,
11826 .load_container
= load_container_imsm
,
11827 .default_geometry
= default_geometry_imsm
,
11828 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
11829 .reshape_super
= imsm_reshape_super
,
11830 .manage_reshape
= imsm_manage_reshape
,
11831 .recover_backup
= recover_backup_imsm
,
11832 .copy_metadata
= copy_metadata_imsm
,
11833 .examine_badblocks
= examine_badblocks_imsm
,
11834 .match_home
= match_home_imsm
,
11835 .uuid_from_super
= uuid_from_super_imsm
,
11836 .getinfo_super
= getinfo_super_imsm
,
11837 .getinfo_super_disks
= getinfo_super_disks_imsm
,
11838 .update_super
= update_super_imsm
,
11840 .avail_size
= avail_size_imsm
,
11841 .min_acceptable_spare_size
= min_acceptable_spare_size_imsm
,
11843 .compare_super
= compare_super_imsm
,
11845 .load_super
= load_super_imsm
,
11846 .init_super
= init_super_imsm
,
11847 .store_super
= store_super_imsm
,
11848 .free_super
= free_super_imsm
,
11849 .match_metadata_desc
= match_metadata_desc_imsm
,
11850 .container_content
= container_content_imsm
,
11851 .validate_container
= validate_container_imsm
,
11853 .write_init_ppl
= write_init_ppl_imsm
,
11854 .validate_ppl
= validate_ppl_imsm
,
11860 .open_new
= imsm_open_new
,
11861 .set_array_state
= imsm_set_array_state
,
11862 .set_disk
= imsm_set_disk
,
11863 .sync_metadata
= imsm_sync_metadata
,
11864 .activate_spare
= imsm_activate_spare
,
11865 .process_update
= imsm_process_update
,
11866 .prepare_update
= imsm_prepare_update
,
11867 .record_bad_block
= imsm_record_badblock
,
11868 .clear_bad_block
= imsm_clear_badblock
,
11869 .get_bad_blocks
= imsm_get_badblocks
,