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__
));
264 static char *map_state_str
[] = { "normal", "uninitialized", "degraded", "failed" };
267 #define BLOCKS_PER_KB (1024/512)
269 #define RAID_DISK_RESERVED_BLOCKS_IMSM_HI 2209
271 #define GEN_MIGR_AREA_SIZE 2048 /* General Migration Copy Area size in blocks */
273 #define MIGR_REC_BUF_SECTORS 1 /* size of migr_record i/o buffer in sectors */
274 #define MIGR_REC_SECTOR_POSITION 1 /* migr_record position offset on disk,
275 * MIGR_REC_BUF_SECTORS <= MIGR_REC_SECTOR_POS
278 #define UNIT_SRC_NORMAL 0 /* Source data for curr_migr_unit must
279 * be recovered using srcMap */
280 #define UNIT_SRC_IN_CP_AREA 1 /* Source data for curr_migr_unit has
281 * already been migrated and must
282 * be recovered from checkpoint area */
284 #define PPL_ENTRY_SPACE (128 * 1024) /* Size of the PPL, without the header */
287 __u32 rec_status
; /* Status used to determine how to restart
288 * migration in case it aborts
290 __u32 curr_migr_unit
; /* 0..numMigrUnits-1 */
291 __u32 family_num
; /* Family number of MPB
292 * containing the RaidDev
293 * that is migrating */
294 __u32 ascending_migr
; /* True if migrating in increasing
296 __u32 blocks_per_unit
; /* Num disk blocks per unit of operation */
297 __u32 dest_depth_per_unit
; /* Num member blocks each destMap
299 * advances per unit-of-operation */
300 __u32 ckpt_area_pba
; /* Pba of first block of ckpt copy area */
301 __u32 dest_1st_member_lba
; /* First member lba on first
302 * stripe of destination */
303 __u32 num_migr_units
; /* Total num migration units-of-op */
304 __u32 post_migr_vol_cap
; /* Size of volume after
305 * migration completes */
306 __u32 post_migr_vol_cap_hi
; /* Expansion space for LBA64 */
307 __u32 ckpt_read_disk_num
; /* Which member disk in destSubMap[0] the
308 * migration ckpt record was read from
309 * (for recovered migrations) */
310 } __attribute__ ((__packed__
));
315 * 2: metadata does not match
323 struct md_list
*next
;
326 #define pr_vrb(fmt, arg...) (void) (verbose && pr_err(fmt, ##arg))
328 static __u8
migr_type(struct imsm_dev
*dev
)
330 if (dev
->vol
.migr_type
== MIGR_VERIFY
&&
331 dev
->status
& DEV_VERIFY_AND_FIX
)
334 return dev
->vol
.migr_type
;
337 static void set_migr_type(struct imsm_dev
*dev
, __u8 migr_type
)
339 /* for compatibility with older oroms convert MIGR_REPAIR, into
340 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
342 if (migr_type
== MIGR_REPAIR
) {
343 dev
->vol
.migr_type
= MIGR_VERIFY
;
344 dev
->status
|= DEV_VERIFY_AND_FIX
;
346 dev
->vol
.migr_type
= migr_type
;
347 dev
->status
&= ~DEV_VERIFY_AND_FIX
;
351 static unsigned int sector_count(__u32 bytes
, unsigned int sector_size
)
353 return ROUND_UP(bytes
, sector_size
) / sector_size
;
356 static unsigned int mpb_sectors(struct imsm_super
*mpb
,
357 unsigned int sector_size
)
359 return sector_count(__le32_to_cpu(mpb
->mpb_size
), sector_size
);
363 struct imsm_dev
*dev
;
364 struct intel_dev
*next
;
369 enum sys_dev_type type
;
372 struct intel_hba
*next
;
379 /* internal representation of IMSM metadata */
382 void *buf
; /* O_DIRECT buffer for reading/writing metadata */
383 struct imsm_super
*anchor
; /* immovable parameters */
386 void *migr_rec_buf
; /* buffer for I/O operations */
387 struct migr_record
*migr_rec
; /* migration record */
389 int clean_migration_record_by_mdmon
; /* when reshape is switched to next
390 array, it indicates that mdmon is allowed to clean migration
392 size_t len
; /* size of the 'buf' allocation */
393 size_t extra_space
; /* extra space in 'buf' that is not used yet */
394 void *next_buf
; /* for realloc'ing buf from the manager */
396 int updates_pending
; /* count of pending updates for mdmon */
397 int current_vol
; /* index of raid device undergoing creation */
398 unsigned long long create_offset
; /* common start for 'current_vol' */
399 __u32 random
; /* random data for seeding new family numbers */
400 struct intel_dev
*devlist
;
401 unsigned int sector_size
; /* sector size of used member drives */
405 __u8 serial
[MAX_RAID_SERIAL_LEN
];
408 struct imsm_disk disk
;
411 struct extent
*e
; /* for determining freespace @ create */
412 int raiddisk
; /* slot to fill in autolayout */
414 } *disks
, *current_disk
;
415 struct dl
*disk_mgmt_list
; /* list of disks to add/remove while mdmon
417 struct dl
*missing
; /* disks removed while we weren't looking */
418 struct bbm_log
*bbm_log
;
419 struct intel_hba
*hba
; /* device path of the raid controller for this metadata */
420 const struct imsm_orom
*orom
; /* platform firmware support */
421 struct intel_super
*next
; /* (temp) list for disambiguating family_num */
422 struct md_bb bb
; /* memory for get_bad_blocks call */
426 struct imsm_disk disk
;
427 #define IMSM_UNKNOWN_OWNER (-1)
429 struct intel_disk
*next
;
433 unsigned long long start
, size
;
436 /* definitions of reshape process types */
437 enum imsm_reshape_type
{
443 /* definition of messages passed to imsm_process_update */
444 enum imsm_update_type
{
445 update_activate_spare
,
449 update_add_remove_disk
,
450 update_reshape_container_disks
,
451 update_reshape_migration
,
453 update_general_migration_checkpoint
,
455 update_prealloc_badblocks_mem
,
459 struct imsm_update_activate_spare
{
460 enum imsm_update_type type
;
464 struct imsm_update_activate_spare
*next
;
470 unsigned long long size
;
477 enum takeover_direction
{
481 struct imsm_update_takeover
{
482 enum imsm_update_type type
;
484 enum takeover_direction direction
;
487 struct imsm_update_reshape
{
488 enum imsm_update_type type
;
492 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
495 struct imsm_update_reshape_migration
{
496 enum imsm_update_type type
;
499 /* fields for array migration changes
506 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
509 struct imsm_update_size_change
{
510 enum imsm_update_type type
;
515 struct imsm_update_general_migration_checkpoint
{
516 enum imsm_update_type type
;
517 __u32 curr_migr_unit
;
521 __u8 serial
[MAX_RAID_SERIAL_LEN
];
524 struct imsm_update_create_array
{
525 enum imsm_update_type type
;
530 struct imsm_update_kill_array
{
531 enum imsm_update_type type
;
535 struct imsm_update_rename_array
{
536 enum imsm_update_type type
;
537 __u8 name
[MAX_RAID_SERIAL_LEN
];
541 struct imsm_update_add_remove_disk
{
542 enum imsm_update_type type
;
545 struct imsm_update_prealloc_bb_mem
{
546 enum imsm_update_type type
;
549 struct imsm_update_rwh_policy
{
550 enum imsm_update_type type
;
555 static const char *_sys_dev_type
[] = {
556 [SYS_DEV_UNKNOWN
] = "Unknown",
557 [SYS_DEV_SAS
] = "SAS",
558 [SYS_DEV_SATA
] = "SATA",
559 [SYS_DEV_NVME
] = "NVMe",
560 [SYS_DEV_VMD
] = "VMD"
563 const char *get_sys_dev_type(enum sys_dev_type type
)
565 if (type
>= SYS_DEV_MAX
)
566 type
= SYS_DEV_UNKNOWN
;
568 return _sys_dev_type
[type
];
571 static struct intel_hba
* alloc_intel_hba(struct sys_dev
*device
)
573 struct intel_hba
*result
= xmalloc(sizeof(*result
));
575 result
->type
= device
->type
;
576 result
->path
= xstrdup(device
->path
);
578 if (result
->path
&& (result
->pci_id
= strrchr(result
->path
, '/')) != NULL
)
584 static struct intel_hba
* find_intel_hba(struct intel_hba
*hba
, struct sys_dev
*device
)
586 struct intel_hba
*result
;
588 for (result
= hba
; result
; result
= result
->next
) {
589 if (result
->type
== device
->type
&& strcmp(result
->path
, device
->path
) == 0)
595 static int attach_hba_to_super(struct intel_super
*super
, struct sys_dev
*device
)
597 struct intel_hba
*hba
;
599 /* check if disk attached to Intel HBA */
600 hba
= find_intel_hba(super
->hba
, device
);
603 /* Check if HBA is already attached to super */
604 if (super
->hba
== NULL
) {
605 super
->hba
= alloc_intel_hba(device
);
610 /* Intel metadata allows for all disks attached to the same type HBA.
611 * Do not support HBA types mixing
613 if (device
->type
!= hba
->type
)
616 /* Multiple same type HBAs can be used if they share the same OROM */
617 const struct imsm_orom
*device_orom
= get_orom_by_device_id(device
->dev_id
);
619 if (device_orom
!= super
->orom
)
625 hba
->next
= alloc_intel_hba(device
);
629 static struct sys_dev
* find_disk_attached_hba(int fd
, const char *devname
)
631 struct sys_dev
*list
, *elem
;
634 if ((list
= find_intel_devices()) == NULL
)
638 disk_path
= (char *) devname
;
640 disk_path
= diskfd_to_devpath(fd
);
645 for (elem
= list
; elem
; elem
= elem
->next
)
646 if (path_attached_to_hba(disk_path
, elem
->path
))
649 if (disk_path
!= devname
)
655 static int find_intel_hba_capability(int fd
, struct intel_super
*super
,
658 static struct supertype
*match_metadata_desc_imsm(char *arg
)
660 struct supertype
*st
;
662 if (strcmp(arg
, "imsm") != 0 &&
663 strcmp(arg
, "default") != 0
667 st
= xcalloc(1, sizeof(*st
));
668 st
->ss
= &super_imsm
;
669 st
->max_devs
= IMSM_MAX_DEVICES
;
670 st
->minor_version
= 0;
676 static __u8
*get_imsm_version(struct imsm_super
*mpb
)
678 return &mpb
->sig
[MPB_SIG_LEN
];
682 /* retrieve a disk directly from the anchor when the anchor is known to be
683 * up-to-date, currently only at load time
685 static struct imsm_disk
*__get_imsm_disk(struct imsm_super
*mpb
, __u8 index
)
687 if (index
>= mpb
->num_disks
)
689 return &mpb
->disk
[index
];
692 /* retrieve the disk description based on a index of the disk
695 static struct dl
*get_imsm_dl_disk(struct intel_super
*super
, __u8 index
)
699 for (d
= super
->disks
; d
; d
= d
->next
)
700 if (d
->index
== index
)
705 /* retrieve a disk from the parsed metadata */
706 static struct imsm_disk
*get_imsm_disk(struct intel_super
*super
, __u8 index
)
710 dl
= get_imsm_dl_disk(super
, index
);
717 /* generate a checksum directly from the anchor when the anchor is known to be
718 * up-to-date, currently only at load or write_super after coalescing
720 static __u32
__gen_imsm_checksum(struct imsm_super
*mpb
)
722 __u32 end
= mpb
->mpb_size
/ sizeof(end
);
723 __u32
*p
= (__u32
*) mpb
;
727 sum
+= __le32_to_cpu(*p
);
731 return sum
- __le32_to_cpu(mpb
->check_sum
);
734 static size_t sizeof_imsm_map(struct imsm_map
*map
)
736 return sizeof(struct imsm_map
) + sizeof(__u32
) * (map
->num_members
- 1);
739 struct imsm_map
*get_imsm_map(struct imsm_dev
*dev
, int second_map
)
741 /* A device can have 2 maps if it is in the middle of a migration.
743 * MAP_0 - we return the first map
744 * MAP_1 - we return the second map if it exists, else NULL
745 * MAP_X - we return the second map if it exists, else the first
747 struct imsm_map
*map
= &dev
->vol
.map
[0];
748 struct imsm_map
*map2
= NULL
;
750 if (dev
->vol
.migr_state
)
751 map2
= (void *)map
+ sizeof_imsm_map(map
);
753 switch (second_map
) {
770 /* return the size of the device.
771 * migr_state increases the returned size if map[0] were to be duplicated
773 static size_t sizeof_imsm_dev(struct imsm_dev
*dev
, int migr_state
)
775 size_t size
= sizeof(*dev
) - sizeof(struct imsm_map
) +
776 sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
778 /* migrating means an additional map */
779 if (dev
->vol
.migr_state
)
780 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_1
));
782 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
788 /* retrieve disk serial number list from a metadata update */
789 static struct disk_info
*get_disk_info(struct imsm_update_create_array
*update
)
792 struct disk_info
*inf
;
794 inf
= u
+ sizeof(*update
) - sizeof(struct imsm_dev
) +
795 sizeof_imsm_dev(&update
->dev
, 0);
801 static struct imsm_dev
*__get_imsm_dev(struct imsm_super
*mpb
, __u8 index
)
807 if (index
>= mpb
->num_raid_devs
)
810 /* devices start after all disks */
811 offset
= ((void *) &mpb
->disk
[mpb
->num_disks
]) - _mpb
;
813 for (i
= 0; i
<= index
; i
++)
815 return _mpb
+ offset
;
817 offset
+= sizeof_imsm_dev(_mpb
+ offset
, 0);
822 static struct imsm_dev
*get_imsm_dev(struct intel_super
*super
, __u8 index
)
824 struct intel_dev
*dv
;
826 if (index
>= super
->anchor
->num_raid_devs
)
828 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
829 if (dv
->index
== index
)
834 static inline unsigned long long __le48_to_cpu(const struct bbm_log_block_addr
837 return ((((__u64
)__le32_to_cpu(addr
->dw1
)) << 16) |
838 __le16_to_cpu(addr
->w1
));
841 static inline struct bbm_log_block_addr
__cpu_to_le48(unsigned long long sec
)
843 struct bbm_log_block_addr addr
;
845 addr
.w1
= __cpu_to_le16((__u16
)(sec
& 0xffff));
846 addr
.dw1
= __cpu_to_le32((__u32
)(sec
>> 16) & 0xffffffff);
851 /* get size of the bbm log */
852 static __u32
get_imsm_bbm_log_size(struct bbm_log
*log
)
854 if (!log
|| log
->entry_count
== 0)
857 return sizeof(log
->signature
) +
858 sizeof(log
->entry_count
) +
859 log
->entry_count
* sizeof(struct bbm_log_entry
);
862 /* check if bad block is not partially stored in bbm log */
863 static int is_stored_in_bbm(struct bbm_log
*log
, const __u8 idx
, const unsigned
864 long long sector
, const int length
, __u32
*pos
)
868 for (i
= *pos
; i
< log
->entry_count
; i
++) {
869 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[i
];
870 unsigned long long bb_start
;
871 unsigned long long bb_end
;
873 bb_start
= __le48_to_cpu(&entry
->defective_block_start
);
874 bb_end
= bb_start
+ (entry
->marked_count
+ 1);
876 if ((entry
->disk_ordinal
== idx
) && (bb_start
>= sector
) &&
877 (bb_end
<= sector
+ length
)) {
885 /* record new bad block in bbm log */
886 static int record_new_badblock(struct bbm_log
*log
, const __u8 idx
, unsigned
887 long long sector
, int length
)
891 struct bbm_log_entry
*entry
= NULL
;
893 while (is_stored_in_bbm(log
, idx
, sector
, length
, &pos
)) {
894 struct bbm_log_entry
*e
= &log
->marked_block_entries
[pos
];
896 if ((e
->marked_count
+ 1 == BBM_LOG_MAX_LBA_ENTRY_VAL
) &&
897 (__le48_to_cpu(&e
->defective_block_start
) == sector
)) {
898 sector
+= BBM_LOG_MAX_LBA_ENTRY_VAL
;
899 length
-= BBM_LOG_MAX_LBA_ENTRY_VAL
;
908 int cnt
= (length
<= BBM_LOG_MAX_LBA_ENTRY_VAL
) ? length
:
909 BBM_LOG_MAX_LBA_ENTRY_VAL
;
910 entry
->defective_block_start
= __cpu_to_le48(sector
);
911 entry
->marked_count
= cnt
- 1;
918 new_bb
= ROUND_UP(length
, BBM_LOG_MAX_LBA_ENTRY_VAL
) /
919 BBM_LOG_MAX_LBA_ENTRY_VAL
;
920 if (log
->entry_count
+ new_bb
> BBM_LOG_MAX_ENTRIES
)
924 int cnt
= (length
<= BBM_LOG_MAX_LBA_ENTRY_VAL
) ? length
:
925 BBM_LOG_MAX_LBA_ENTRY_VAL
;
926 struct bbm_log_entry
*entry
=
927 &log
->marked_block_entries
[log
->entry_count
];
929 entry
->defective_block_start
= __cpu_to_le48(sector
);
930 entry
->marked_count
= cnt
- 1;
931 entry
->disk_ordinal
= idx
;
942 /* clear all bad blocks for given disk */
943 static void clear_disk_badblocks(struct bbm_log
*log
, const __u8 idx
)
947 while (i
< log
->entry_count
) {
948 struct bbm_log_entry
*entries
= log
->marked_block_entries
;
950 if (entries
[i
].disk_ordinal
== idx
) {
951 if (i
< log
->entry_count
- 1)
952 entries
[i
] = entries
[log
->entry_count
- 1];
960 /* clear given bad block */
961 static int clear_badblock(struct bbm_log
*log
, const __u8 idx
, const unsigned
962 long long sector
, const int length
) {
965 while (i
< log
->entry_count
) {
966 struct bbm_log_entry
*entries
= log
->marked_block_entries
;
968 if ((entries
[i
].disk_ordinal
== idx
) &&
969 (__le48_to_cpu(&entries
[i
].defective_block_start
) ==
970 sector
) && (entries
[i
].marked_count
+ 1 == length
)) {
971 if (i
< log
->entry_count
- 1)
972 entries
[i
] = entries
[log
->entry_count
- 1];
981 #endif /* MDASSEMBLE */
983 /* allocate and load BBM log from metadata */
984 static int load_bbm_log(struct intel_super
*super
)
986 struct imsm_super
*mpb
= super
->anchor
;
987 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
989 super
->bbm_log
= xcalloc(1, sizeof(struct bbm_log
));
994 struct bbm_log
*log
= (void *)mpb
+
995 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
999 if (bbm_log_size
< sizeof(log
->signature
) +
1000 sizeof(log
->entry_count
))
1003 entry_count
= __le32_to_cpu(log
->entry_count
);
1004 if ((__le32_to_cpu(log
->signature
) != BBM_LOG_SIGNATURE
) ||
1005 (entry_count
> BBM_LOG_MAX_ENTRIES
))
1009 sizeof(log
->signature
) + sizeof(log
->entry_count
) +
1010 entry_count
* sizeof(struct bbm_log_entry
))
1013 memcpy(super
->bbm_log
, log
, bbm_log_size
);
1015 super
->bbm_log
->signature
= __cpu_to_le32(BBM_LOG_SIGNATURE
);
1016 super
->bbm_log
->entry_count
= 0;
1022 /* checks if bad block is within volume boundaries */
1023 static int is_bad_block_in_volume(const struct bbm_log_entry
*entry
,
1024 const unsigned long long start_sector
,
1025 const unsigned long long size
)
1027 unsigned long long bb_start
;
1028 unsigned long long bb_end
;
1030 bb_start
= __le48_to_cpu(&entry
->defective_block_start
);
1031 bb_end
= bb_start
+ (entry
->marked_count
+ 1);
1033 if (((bb_start
>= start_sector
) && (bb_start
< start_sector
+ size
)) ||
1034 ((bb_end
>= start_sector
) && (bb_end
<= start_sector
+ size
)))
1040 /* get list of bad blocks on a drive for a volume */
1041 static void get_volume_badblocks(const struct bbm_log
*log
, const __u8 idx
,
1042 const unsigned long long start_sector
,
1043 const unsigned long long size
,
1049 for (i
= 0; i
< log
->entry_count
; i
++) {
1050 const struct bbm_log_entry
*ent
=
1051 &log
->marked_block_entries
[i
];
1052 struct md_bb_entry
*bb
;
1054 if ((ent
->disk_ordinal
== idx
) &&
1055 is_bad_block_in_volume(ent
, start_sector
, size
)) {
1057 if (!bbs
->entries
) {
1058 bbs
->entries
= xmalloc(BBM_LOG_MAX_ENTRIES
*
1064 bb
= &bbs
->entries
[count
++];
1065 bb
->sector
= __le48_to_cpu(&ent
->defective_block_start
);
1066 bb
->length
= ent
->marked_count
+ 1;
1074 * == MAP_0 get first map
1075 * == MAP_1 get second map
1076 * == MAP_X than get map according to the current migr_state
1078 static __u32
get_imsm_ord_tbl_ent(struct imsm_dev
*dev
,
1082 struct imsm_map
*map
;
1084 map
= get_imsm_map(dev
, second_map
);
1086 /* top byte identifies disk under rebuild */
1087 return __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
1090 #define ord_to_idx(ord) (((ord) << 8) >> 8)
1091 static __u32
get_imsm_disk_idx(struct imsm_dev
*dev
, int slot
, int second_map
)
1093 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, second_map
);
1095 return ord_to_idx(ord
);
1098 static void set_imsm_ord_tbl_ent(struct imsm_map
*map
, int slot
, __u32 ord
)
1100 map
->disk_ord_tbl
[slot
] = __cpu_to_le32(ord
);
1103 static int get_imsm_disk_slot(struct imsm_map
*map
, unsigned idx
)
1108 for (slot
= 0; slot
< map
->num_members
; slot
++) {
1109 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
1110 if (ord_to_idx(ord
) == idx
)
1117 static int get_imsm_raid_level(struct imsm_map
*map
)
1119 if (map
->raid_level
== 1) {
1120 if (map
->num_members
== 2)
1126 return map
->raid_level
;
1129 static int cmp_extent(const void *av
, const void *bv
)
1131 const struct extent
*a
= av
;
1132 const struct extent
*b
= bv
;
1133 if (a
->start
< b
->start
)
1135 if (a
->start
> b
->start
)
1140 static int count_memberships(struct dl
*dl
, struct intel_super
*super
)
1142 int memberships
= 0;
1145 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1146 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1147 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1149 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
1156 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
);
1158 static int split_ull(unsigned long long n
, __u32
*lo
, __u32
*hi
)
1160 if (lo
== 0 || hi
== 0)
1162 *lo
= __le32_to_cpu((unsigned)n
);
1163 *hi
= __le32_to_cpu((unsigned)(n
>> 32));
1167 static unsigned long long join_u32(__u32 lo
, __u32 hi
)
1169 return (unsigned long long)__le32_to_cpu(lo
) |
1170 (((unsigned long long)__le32_to_cpu(hi
)) << 32);
1173 static unsigned long long total_blocks(struct imsm_disk
*disk
)
1177 return join_u32(disk
->total_blocks_lo
, disk
->total_blocks_hi
);
1180 static unsigned long long pba_of_lba0(struct imsm_map
*map
)
1184 return join_u32(map
->pba_of_lba0_lo
, map
->pba_of_lba0_hi
);
1187 static unsigned long long blocks_per_member(struct imsm_map
*map
)
1191 return join_u32(map
->blocks_per_member_lo
, map
->blocks_per_member_hi
);
1194 static unsigned long long num_data_stripes(struct imsm_map
*map
)
1198 return join_u32(map
->num_data_stripes_lo
, map
->num_data_stripes_hi
);
1201 static void set_total_blocks(struct imsm_disk
*disk
, unsigned long long n
)
1203 split_ull(n
, &disk
->total_blocks_lo
, &disk
->total_blocks_hi
);
1206 static void set_pba_of_lba0(struct imsm_map
*map
, unsigned long long n
)
1208 split_ull(n
, &map
->pba_of_lba0_lo
, &map
->pba_of_lba0_hi
);
1211 static void set_blocks_per_member(struct imsm_map
*map
, unsigned long long n
)
1213 split_ull(n
, &map
->blocks_per_member_lo
, &map
->blocks_per_member_hi
);
1216 static void set_num_data_stripes(struct imsm_map
*map
, unsigned long long n
)
1218 split_ull(n
, &map
->num_data_stripes_lo
, &map
->num_data_stripes_hi
);
1221 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
)
1223 /* find a list of used extents on the given physical device */
1224 struct extent
*rv
, *e
;
1226 int memberships
= count_memberships(dl
, super
);
1229 /* trim the reserved area for spares, so they can join any array
1230 * regardless of whether the OROM has assigned sectors from the
1231 * IMSM_RESERVED_SECTORS region
1233 if (dl
->index
== -1)
1234 reservation
= imsm_min_reserved_sectors(super
);
1236 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1238 rv
= xcalloc(sizeof(struct extent
), (memberships
+ 1));
1241 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1242 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1243 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1245 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
1246 e
->start
= pba_of_lba0(map
);
1247 e
->size
= blocks_per_member(map
);
1251 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
1253 /* determine the start of the metadata
1254 * when no raid devices are defined use the default
1255 * ...otherwise allow the metadata to truncate the value
1256 * as is the case with older versions of imsm
1259 struct extent
*last
= &rv
[memberships
- 1];
1260 unsigned long long remainder
;
1262 remainder
= total_blocks(&dl
->disk
) - (last
->start
+ last
->size
);
1263 /* round down to 1k block to satisfy precision of the kernel
1267 /* make sure remainder is still sane */
1268 if (remainder
< (unsigned)ROUND_UP(super
->len
, 512) >> 9)
1269 remainder
= ROUND_UP(super
->len
, 512) >> 9;
1270 if (reservation
> remainder
)
1271 reservation
= remainder
;
1273 e
->start
= total_blocks(&dl
->disk
) - reservation
;
1278 /* try to determine how much space is reserved for metadata from
1279 * the last get_extents() entry, otherwise fallback to the
1282 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
1288 /* for spares just return a minimal reservation which will grow
1289 * once the spare is picked up by an array
1291 if (dl
->index
== -1)
1292 return MPB_SECTOR_CNT
;
1294 e
= get_extents(super
, dl
);
1296 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1298 /* scroll to last entry */
1299 for (i
= 0; e
[i
].size
; i
++)
1302 rv
= total_blocks(&dl
->disk
) - e
[i
].start
;
1309 static int is_spare(struct imsm_disk
*disk
)
1311 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
1314 static int is_configured(struct imsm_disk
*disk
)
1316 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
1319 static int is_failed(struct imsm_disk
*disk
)
1321 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
1324 static int is_journal(struct imsm_disk
*disk
)
1326 return (disk
->status
& JOURNAL_DISK
) == JOURNAL_DISK
;
1329 /* round array size down to closest MB and ensure it splits evenly
1332 static unsigned long long round_size_to_mb(unsigned long long size
, unsigned int
1336 size
= (size
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
1342 /* try to determine how much space is reserved for metadata from
1343 * the last get_extents() entry on the smallest active disk,
1344 * otherwise fallback to the default
1346 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
)
1350 unsigned long long min_active
;
1352 __u32 rv
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1353 struct dl
*dl
, *dl_min
= NULL
;
1359 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
1362 unsigned long long blocks
= total_blocks(&dl
->disk
);
1363 if (blocks
< min_active
|| min_active
== 0) {
1365 min_active
= blocks
;
1371 /* find last lba used by subarrays on the smallest active disk */
1372 e
= get_extents(super
, dl_min
);
1375 for (i
= 0; e
[i
].size
; i
++)
1378 remainder
= min_active
- e
[i
].start
;
1381 /* to give priority to recovery we should not require full
1382 IMSM_RESERVED_SECTORS from the spare */
1383 rv
= MPB_SECTOR_CNT
+ NUM_BLOCKS_DIRTY_STRIPE_REGION
;
1385 /* if real reservation is smaller use that value */
1386 return (remainder
< rv
) ? remainder
: rv
;
1389 /* Return minimum size of a spare that can be used in this array*/
1390 static unsigned long long min_acceptable_spare_size_imsm(struct supertype
*st
)
1392 struct intel_super
*super
= st
->sb
;
1396 unsigned long long rv
= 0;
1400 /* find first active disk in array */
1402 while (dl
&& (is_failed(&dl
->disk
) || dl
->index
== -1))
1406 /* find last lba used by subarrays */
1407 e
= get_extents(super
, dl
);
1410 for (i
= 0; e
[i
].size
; i
++)
1413 rv
= e
[i
-1].start
+ e
[i
-1].size
;
1416 /* add the amount of space needed for metadata */
1417 rv
= rv
+ imsm_min_reserved_sectors(super
);
1422 static int is_gen_migration(struct imsm_dev
*dev
);
1424 #define IMSM_4K_DIV 8
1427 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
1428 struct imsm_dev
*dev
);
1430 static void print_imsm_dev(struct intel_super
*super
,
1431 struct imsm_dev
*dev
,
1437 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1438 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
1442 printf("[%.16s]:\n", dev
->volume
);
1443 printf(" UUID : %s\n", uuid
);
1444 printf(" RAID Level : %d", get_imsm_raid_level(map
));
1446 printf(" <-- %d", get_imsm_raid_level(map2
));
1448 printf(" Members : %d", map
->num_members
);
1450 printf(" <-- %d", map2
->num_members
);
1452 printf(" Slots : [");
1453 for (i
= 0; i
< map
->num_members
; i
++) {
1454 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
1455 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1460 for (i
= 0; i
< map2
->num_members
; i
++) {
1461 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_1
);
1462 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1467 printf(" Failed disk : ");
1468 if (map
->failed_disk_num
== 0xff)
1471 printf("%i", map
->failed_disk_num
);
1473 slot
= get_imsm_disk_slot(map
, disk_idx
);
1475 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
1476 printf(" This Slot : %d%s\n", slot
,
1477 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
1479 printf(" This Slot : ?\n");
1480 sz
= __le32_to_cpu(dev
->size_high
);
1482 sz
+= __le32_to_cpu(dev
->size_low
);
1483 printf(" Array Size : %llu%s\n", (unsigned long long)sz
,
1484 human_size(sz
* 512));
1485 sz
= blocks_per_member(map
);
1486 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz
,
1487 human_size(sz
* 512));
1488 printf(" Sector Offset : %llu\n",
1490 printf(" Num Stripes : %llu\n",
1491 num_data_stripes(map
));
1492 printf(" Chunk Size : %u KiB",
1493 __le16_to_cpu(map
->blocks_per_strip
) / 2);
1495 printf(" <-- %u KiB",
1496 __le16_to_cpu(map2
->blocks_per_strip
) / 2);
1498 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
1499 printf(" Migrate State : ");
1500 if (dev
->vol
.migr_state
) {
1501 if (migr_type(dev
) == MIGR_INIT
)
1502 printf("initialize\n");
1503 else if (migr_type(dev
) == MIGR_REBUILD
)
1504 printf("rebuild\n");
1505 else if (migr_type(dev
) == MIGR_VERIFY
)
1507 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
1508 printf("general migration\n");
1509 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
1510 printf("state change\n");
1511 else if (migr_type(dev
) == MIGR_REPAIR
)
1514 printf("<unknown:%d>\n", migr_type(dev
));
1517 printf(" Map State : %s", map_state_str
[map
->map_state
]);
1518 if (dev
->vol
.migr_state
) {
1519 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1521 printf(" <-- %s", map_state_str
[map
->map_state
]);
1522 printf("\n Checkpoint : %u ",
1523 __le32_to_cpu(dev
->vol
.curr_migr_unit
));
1524 if (is_gen_migration(dev
) && (slot
> 1 || slot
< 0))
1527 printf("(%llu)", (unsigned long long)
1528 blocks_per_migr_unit(super
, dev
));
1531 printf(" Dirty State : %s\n", (dev
->vol
.dirty
& RAIDVOL_DIRTY
) ?
1533 printf(" RWH Policy : ");
1534 if (dev
->rwh_policy
== RWH_OFF
)
1536 else if (dev
->rwh_policy
== RWH_DISTRIBUTED
)
1537 printf("PPL distributed\n");
1538 else if (dev
->rwh_policy
== RWH_JOURNALING_DRIVE
)
1539 printf("PPL journaling drive\n");
1541 printf("<unknown:%d>\n", dev
->rwh_policy
);
1544 static void print_imsm_disk(struct imsm_disk
*disk
,
1547 unsigned int sector_size
) {
1548 char str
[MAX_RAID_SERIAL_LEN
+ 1];
1551 if (index
< -1 || !disk
)
1555 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
1557 printf(" Disk%02d Serial : %s\n", index
, str
);
1559 printf(" Disk Serial : %s\n", str
);
1560 printf(" State :%s%s%s%s\n", is_spare(disk
) ? " spare" : "",
1561 is_configured(disk
) ? " active" : "",
1562 is_failed(disk
) ? " failed" : "",
1563 is_journal(disk
) ? " journal" : "");
1564 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
1565 sz
= total_blocks(disk
) - reserved
;
1566 printf(" Usable Size : %llu%s\n",
1567 (unsigned long long)sz
* 512 / sector_size
,
1568 human_size(sz
* 512));
1571 void convert_to_4k_imsm_migr_rec(struct intel_super
*super
)
1573 struct migr_record
*migr_rec
= super
->migr_rec
;
1575 migr_rec
->blocks_per_unit
/= IMSM_4K_DIV
;
1576 migr_rec
->ckpt_area_pba
/= IMSM_4K_DIV
;
1577 migr_rec
->dest_1st_member_lba
/= IMSM_4K_DIV
;
1578 migr_rec
->dest_depth_per_unit
/= IMSM_4K_DIV
;
1579 split_ull((join_u32(migr_rec
->post_migr_vol_cap
,
1580 migr_rec
->post_migr_vol_cap_hi
) / IMSM_4K_DIV
),
1581 &migr_rec
->post_migr_vol_cap
, &migr_rec
->post_migr_vol_cap_hi
);
1584 void convert_to_4k_imsm_disk(struct imsm_disk
*disk
)
1586 set_total_blocks(disk
, (total_blocks(disk
)/IMSM_4K_DIV
));
1589 void convert_to_4k(struct intel_super
*super
)
1591 struct imsm_super
*mpb
= super
->anchor
;
1592 struct imsm_disk
*disk
;
1594 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
1596 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1597 disk
= __get_imsm_disk(mpb
, i
);
1599 convert_to_4k_imsm_disk(disk
);
1601 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1602 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1603 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1605 split_ull((join_u32(dev
->size_low
, dev
->size_high
)/IMSM_4K_DIV
),
1606 &dev
->size_low
, &dev
->size_high
);
1607 dev
->vol
.curr_migr_unit
/= IMSM_4K_DIV
;
1610 set_blocks_per_member(map
, blocks_per_member(map
)/IMSM_4K_DIV
);
1611 map
->blocks_per_strip
/= IMSM_4K_DIV
;
1612 set_pba_of_lba0(map
, pba_of_lba0(map
)/IMSM_4K_DIV
);
1614 if (dev
->vol
.migr_state
) {
1616 map
= get_imsm_map(dev
, MAP_1
);
1617 set_blocks_per_member(map
,
1618 blocks_per_member(map
)/IMSM_4K_DIV
);
1619 map
->blocks_per_strip
/= IMSM_4K_DIV
;
1620 set_pba_of_lba0(map
, pba_of_lba0(map
)/IMSM_4K_DIV
);
1624 struct bbm_log
*log
= (void *)mpb
+
1625 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
1628 for (i
= 0; i
< log
->entry_count
; i
++) {
1629 struct bbm_log_entry
*entry
=
1630 &log
->marked_block_entries
[i
];
1632 __u8 count
= entry
->marked_count
+ 1;
1633 unsigned long long sector
=
1634 __le48_to_cpu(&entry
->defective_block_start
);
1636 entry
->defective_block_start
=
1637 __cpu_to_le48(sector
/IMSM_4K_DIV
);
1638 entry
->marked_count
= max(count
/IMSM_4K_DIV
, 1) - 1;
1642 mpb
->check_sum
= __gen_imsm_checksum(mpb
);
1645 void examine_migr_rec_imsm(struct intel_super
*super
)
1647 struct migr_record
*migr_rec
= super
->migr_rec
;
1648 struct imsm_super
*mpb
= super
->anchor
;
1651 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1652 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1653 struct imsm_map
*map
;
1656 if (is_gen_migration(dev
) == 0)
1659 printf("\nMigration Record Information:");
1661 /* first map under migration */
1662 map
= get_imsm_map(dev
, MAP_0
);
1664 slot
= get_imsm_disk_slot(map
, super
->disks
->index
);
1665 if (map
== NULL
|| slot
> 1 || slot
< 0) {
1666 printf(" Empty\n ");
1667 printf("Examine one of first two disks in array\n");
1670 printf("\n Status : ");
1671 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
1674 printf("Contains Data\n");
1675 printf(" Current Unit : %u\n",
1676 __le32_to_cpu(migr_rec
->curr_migr_unit
));
1677 printf(" Family : %u\n",
1678 __le32_to_cpu(migr_rec
->family_num
));
1679 printf(" Ascending : %u\n",
1680 __le32_to_cpu(migr_rec
->ascending_migr
));
1681 printf(" Blocks Per Unit : %u\n",
1682 __le32_to_cpu(migr_rec
->blocks_per_unit
));
1683 printf(" Dest. Depth Per Unit : %u\n",
1684 __le32_to_cpu(migr_rec
->dest_depth_per_unit
));
1685 printf(" Checkpoint Area pba : %u\n",
1686 __le32_to_cpu(migr_rec
->ckpt_area_pba
));
1687 printf(" First member lba : %u\n",
1688 __le32_to_cpu(migr_rec
->dest_1st_member_lba
));
1689 printf(" Total Number of Units : %u\n",
1690 __le32_to_cpu(migr_rec
->num_migr_units
));
1691 printf(" Size of volume : %u\n",
1692 __le32_to_cpu(migr_rec
->post_migr_vol_cap
));
1693 printf(" Expansion space for LBA64 : %u\n",
1694 __le32_to_cpu(migr_rec
->post_migr_vol_cap_hi
));
1695 printf(" Record was read from : %u\n",
1696 __le32_to_cpu(migr_rec
->ckpt_read_disk_num
));
1701 #endif /* MDASSEMBLE */
1703 void convert_from_4k_imsm_migr_rec(struct intel_super
*super
)
1705 struct migr_record
*migr_rec
= super
->migr_rec
;
1707 migr_rec
->blocks_per_unit
*= IMSM_4K_DIV
;
1708 migr_rec
->ckpt_area_pba
*= IMSM_4K_DIV
;
1709 migr_rec
->dest_1st_member_lba
*= IMSM_4K_DIV
;
1710 migr_rec
->dest_depth_per_unit
*= IMSM_4K_DIV
;
1711 split_ull((join_u32(migr_rec
->post_migr_vol_cap
,
1712 migr_rec
->post_migr_vol_cap_hi
) * IMSM_4K_DIV
),
1713 &migr_rec
->post_migr_vol_cap
,
1714 &migr_rec
->post_migr_vol_cap_hi
);
1717 void convert_from_4k(struct intel_super
*super
)
1719 struct imsm_super
*mpb
= super
->anchor
;
1720 struct imsm_disk
*disk
;
1722 __u32 bbm_log_size
= __le32_to_cpu(mpb
->bbm_log_size
);
1724 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1725 disk
= __get_imsm_disk(mpb
, i
);
1727 set_total_blocks(disk
, (total_blocks(disk
)*IMSM_4K_DIV
));
1730 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1731 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1732 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1734 split_ull((join_u32(dev
->size_low
, dev
->size_high
)*IMSM_4K_DIV
),
1735 &dev
->size_low
, &dev
->size_high
);
1736 dev
->vol
.curr_migr_unit
*= IMSM_4K_DIV
;
1739 set_blocks_per_member(map
, blocks_per_member(map
)*IMSM_4K_DIV
);
1740 map
->blocks_per_strip
*= IMSM_4K_DIV
;
1741 set_pba_of_lba0(map
, pba_of_lba0(map
)*IMSM_4K_DIV
);
1743 if (dev
->vol
.migr_state
) {
1745 map
= get_imsm_map(dev
, MAP_1
);
1746 set_blocks_per_member(map
,
1747 blocks_per_member(map
)*IMSM_4K_DIV
);
1748 map
->blocks_per_strip
*= IMSM_4K_DIV
;
1749 set_pba_of_lba0(map
, pba_of_lba0(map
)*IMSM_4K_DIV
);
1753 struct bbm_log
*log
= (void *)mpb
+
1754 __le32_to_cpu(mpb
->mpb_size
) - bbm_log_size
;
1757 for (i
= 0; i
< log
->entry_count
; i
++) {
1758 struct bbm_log_entry
*entry
=
1759 &log
->marked_block_entries
[i
];
1761 __u8 count
= entry
->marked_count
+ 1;
1762 unsigned long long sector
=
1763 __le48_to_cpu(&entry
->defective_block_start
);
1765 entry
->defective_block_start
=
1766 __cpu_to_le48(sector
*IMSM_4K_DIV
);
1767 entry
->marked_count
= count
*IMSM_4K_DIV
- 1;
1771 mpb
->check_sum
= __gen_imsm_checksum(mpb
);
1774 /*******************************************************************************
1775 * function: imsm_check_attributes
1776 * Description: Function checks if features represented by attributes flags
1777 * are supported by mdadm.
1779 * attributes - Attributes read from metadata
1781 * 0 - passed attributes contains unsupported features flags
1782 * 1 - all features are supported
1783 ******************************************************************************/
1784 static int imsm_check_attributes(__u32 attributes
)
1787 __u32 not_supported
= MPB_ATTRIB_SUPPORTED
^0xffffffff;
1789 not_supported
&= ~MPB_ATTRIB_IGNORED
;
1791 not_supported
&= attributes
;
1792 if (not_supported
) {
1793 pr_err("(IMSM): Unsupported attributes : %x\n",
1794 (unsigned)__le32_to_cpu(not_supported
));
1795 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1796 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
1797 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1799 if (not_supported
& MPB_ATTRIB_2TB
) {
1800 dprintf("\t\tMPB_ATTRIB_2TB\n");
1801 not_supported
^= MPB_ATTRIB_2TB
;
1803 if (not_supported
& MPB_ATTRIB_RAID0
) {
1804 dprintf("\t\tMPB_ATTRIB_RAID0\n");
1805 not_supported
^= MPB_ATTRIB_RAID0
;
1807 if (not_supported
& MPB_ATTRIB_RAID1
) {
1808 dprintf("\t\tMPB_ATTRIB_RAID1\n");
1809 not_supported
^= MPB_ATTRIB_RAID1
;
1811 if (not_supported
& MPB_ATTRIB_RAID10
) {
1812 dprintf("\t\tMPB_ATTRIB_RAID10\n");
1813 not_supported
^= MPB_ATTRIB_RAID10
;
1815 if (not_supported
& MPB_ATTRIB_RAID1E
) {
1816 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
1817 not_supported
^= MPB_ATTRIB_RAID1E
;
1819 if (not_supported
& MPB_ATTRIB_RAID5
) {
1820 dprintf("\t\tMPB_ATTRIB_RAID5\n");
1821 not_supported
^= MPB_ATTRIB_RAID5
;
1823 if (not_supported
& MPB_ATTRIB_RAIDCNG
) {
1824 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
1825 not_supported
^= MPB_ATTRIB_RAIDCNG
;
1827 if (not_supported
& MPB_ATTRIB_BBM
) {
1828 dprintf("\t\tMPB_ATTRIB_BBM\n");
1829 not_supported
^= MPB_ATTRIB_BBM
;
1831 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1832 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
1833 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1835 if (not_supported
& MPB_ATTRIB_EXP_STRIPE_SIZE
) {
1836 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
1837 not_supported
^= MPB_ATTRIB_EXP_STRIPE_SIZE
;
1839 if (not_supported
& MPB_ATTRIB_2TB_DISK
) {
1840 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
1841 not_supported
^= MPB_ATTRIB_2TB_DISK
;
1843 if (not_supported
& MPB_ATTRIB_NEVER_USE2
) {
1844 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
1845 not_supported
^= MPB_ATTRIB_NEVER_USE2
;
1847 if (not_supported
& MPB_ATTRIB_NEVER_USE
) {
1848 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
1849 not_supported
^= MPB_ATTRIB_NEVER_USE
;
1853 dprintf("(IMSM): Unknown attributes : %x\n", not_supported
);
1862 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
1864 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
1866 struct intel_super
*super
= st
->sb
;
1867 struct imsm_super
*mpb
= super
->anchor
;
1868 char str
[MAX_SIGNATURE_LENGTH
];
1873 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
1876 strncpy(str
, (char *)mpb
->sig
, MPB_SIG_LEN
);
1877 str
[MPB_SIG_LEN
-1] = '\0';
1878 printf(" Magic : %s\n", str
);
1879 snprintf(str
, strlen(MPB_VERSION_RAID0
), "%s", get_imsm_version(mpb
));
1880 printf(" Version : %s\n", get_imsm_version(mpb
));
1881 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
1882 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
1883 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
1884 printf(" Attributes : ");
1885 if (imsm_check_attributes(mpb
->attributes
))
1886 printf("All supported\n");
1888 printf("not supported\n");
1889 getinfo_super_imsm(st
, &info
, NULL
);
1890 fname_from_uuid(st
, &info
, nbuf
, ':');
1891 printf(" UUID : %s\n", nbuf
+ 5);
1892 sum
= __le32_to_cpu(mpb
->check_sum
);
1893 printf(" Checksum : %08x %s\n", sum
,
1894 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
1895 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
, super
->sector_size
));
1896 printf(" Disks : %d\n", mpb
->num_disks
);
1897 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
1898 print_imsm_disk(__get_imsm_disk(mpb
, super
->disks
->index
),
1899 super
->disks
->index
, reserved
, super
->sector_size
);
1900 if (get_imsm_bbm_log_size(super
->bbm_log
)) {
1901 struct bbm_log
*log
= super
->bbm_log
;
1904 printf("Bad Block Management Log:\n");
1905 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
1906 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
1907 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
1909 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1911 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1913 super
->current_vol
= i
;
1914 getinfo_super_imsm(st
, &info
, NULL
);
1915 fname_from_uuid(st
, &info
, nbuf
, ':');
1916 print_imsm_dev(super
, dev
, nbuf
+ 5, super
->disks
->index
);
1918 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1919 if (i
== super
->disks
->index
)
1921 print_imsm_disk(__get_imsm_disk(mpb
, i
), i
, reserved
,
1922 super
->sector_size
);
1925 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
1926 if (dl
->index
== -1)
1927 print_imsm_disk(&dl
->disk
, -1, reserved
,
1928 super
->sector_size
);
1930 examine_migr_rec_imsm(super
);
1933 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
1935 /* We just write a generic IMSM ARRAY entry */
1938 struct intel_super
*super
= st
->sb
;
1940 if (!super
->anchor
->num_raid_devs
) {
1941 printf("ARRAY metadata=imsm\n");
1945 getinfo_super_imsm(st
, &info
, NULL
);
1946 fname_from_uuid(st
, &info
, nbuf
, ':');
1947 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
1950 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
1952 /* We just write a generic IMSM ARRAY entry */
1956 struct intel_super
*super
= st
->sb
;
1959 if (!super
->anchor
->num_raid_devs
)
1962 getinfo_super_imsm(st
, &info
, NULL
);
1963 fname_from_uuid(st
, &info
, nbuf
, ':');
1964 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1965 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1967 super
->current_vol
= i
;
1968 getinfo_super_imsm(st
, &info
, NULL
);
1969 fname_from_uuid(st
, &info
, nbuf1
, ':');
1970 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
1971 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
1975 static void export_examine_super_imsm(struct supertype
*st
)
1977 struct intel_super
*super
= st
->sb
;
1978 struct imsm_super
*mpb
= super
->anchor
;
1982 getinfo_super_imsm(st
, &info
, NULL
);
1983 fname_from_uuid(st
, &info
, nbuf
, ':');
1984 printf("MD_METADATA=imsm\n");
1985 printf("MD_LEVEL=container\n");
1986 printf("MD_UUID=%s\n", nbuf
+5);
1987 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
1990 static int copy_metadata_imsm(struct supertype
*st
, int from
, int to
)
1992 /* The second last sector of the device contains
1993 * the "struct imsm_super" metadata.
1994 * This contains mpb_size which is the size in bytes of the
1995 * extended metadata. This is located immediately before
1997 * We want to read all that, plus the last sector which
1998 * may contain a migration record, and write it all
2002 unsigned long long dsize
, offset
;
2004 struct imsm_super
*sb
;
2005 struct intel_super
*super
= st
->sb
;
2006 unsigned int sector_size
= super
->sector_size
;
2007 unsigned int written
= 0;
2009 if (posix_memalign(&buf
, MAX_SECTOR_SIZE
, MAX_SECTOR_SIZE
) != 0)
2012 if (!get_dev_size(from
, NULL
, &dsize
))
2015 if (lseek64(from
, dsize
-(2*sector_size
), 0) < 0)
2017 if ((unsigned int)read(from
, buf
, sector_size
) != sector_size
)
2020 if (strncmp((char*)sb
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0)
2023 sectors
= mpb_sectors(sb
, sector_size
) + 2;
2024 offset
= dsize
- sectors
* sector_size
;
2025 if (lseek64(from
, offset
, 0) < 0 ||
2026 lseek64(to
, offset
, 0) < 0)
2028 while (written
< sectors
* sector_size
) {
2029 int n
= sectors
*sector_size
- written
;
2032 if (read(from
, buf
, n
) != n
)
2034 if (write(to
, buf
, n
) != n
)
2045 static void detail_super_imsm(struct supertype
*st
, char *homehost
)
2050 getinfo_super_imsm(st
, &info
, NULL
);
2051 fname_from_uuid(st
, &info
, nbuf
, ':');
2052 printf("\n UUID : %s\n", nbuf
+ 5);
2055 static void brief_detail_super_imsm(struct supertype
*st
)
2059 getinfo_super_imsm(st
, &info
, NULL
);
2060 fname_from_uuid(st
, &info
, nbuf
, ':');
2061 printf(" UUID=%s", nbuf
+ 5);
2064 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
);
2065 static void fd2devname(int fd
, char *name
);
2067 static int ahci_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
2069 /* dump an unsorted list of devices attached to AHCI Intel storage
2070 * controller, as well as non-connected ports
2072 int hba_len
= strlen(hba_path
) + 1;
2077 unsigned long port_mask
= (1 << port_count
) - 1;
2079 if (port_count
> (int)sizeof(port_mask
) * 8) {
2081 pr_err("port_count %d out of range\n", port_count
);
2085 /* scroll through /sys/dev/block looking for devices attached to
2088 dir
= opendir("/sys/dev/block");
2092 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2103 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
2105 path
= devt_to_devpath(makedev(major
, minor
));
2108 if (!path_attached_to_hba(path
, hba_path
)) {
2114 /* retrieve the scsi device type */
2115 if (asprintf(&device
, "/sys/dev/block/%d:%d/device/xxxxxxx", major
, minor
) < 0) {
2117 pr_err("failed to allocate 'device'\n");
2121 sprintf(device
, "/sys/dev/block/%d:%d/device/type", major
, minor
);
2122 if (load_sys(device
, buf
, sizeof(buf
)) != 0) {
2124 pr_err("failed to read device type for %s\n",
2130 type
= strtoul(buf
, NULL
, 10);
2132 /* if it's not a disk print the vendor and model */
2133 if (!(type
== 0 || type
== 7 || type
== 14)) {
2136 sprintf(device
, "/sys/dev/block/%d:%d/device/vendor", major
, minor
);
2137 if (load_sys(device
, buf
, sizeof(buf
)) == 0) {
2138 strncpy(vendor
, buf
, sizeof(vendor
));
2139 vendor
[sizeof(vendor
) - 1] = '\0';
2140 c
= (char *) &vendor
[sizeof(vendor
) - 1];
2141 while (isspace(*c
) || *c
== '\0')
2145 sprintf(device
, "/sys/dev/block/%d:%d/device/model", major
, minor
);
2146 if (load_sys(device
, buf
, sizeof(buf
)) == 0) {
2147 strncpy(model
, buf
, sizeof(model
));
2148 model
[sizeof(model
) - 1] = '\0';
2149 c
= (char *) &model
[sizeof(model
) - 1];
2150 while (isspace(*c
) || *c
== '\0')
2154 if (vendor
[0] && model
[0])
2155 sprintf(buf
, "%.64s %.64s", vendor
, model
);
2157 switch (type
) { /* numbers from hald/linux/device.c */
2158 case 1: sprintf(buf
, "tape"); break;
2159 case 2: sprintf(buf
, "printer"); break;
2160 case 3: sprintf(buf
, "processor"); break;
2162 case 5: sprintf(buf
, "cdrom"); break;
2163 case 6: sprintf(buf
, "scanner"); break;
2164 case 8: sprintf(buf
, "media_changer"); break;
2165 case 9: sprintf(buf
, "comm"); break;
2166 case 12: sprintf(buf
, "raid"); break;
2167 default: sprintf(buf
, "unknown");
2173 /* chop device path to 'host%d' and calculate the port number */
2174 c
= strchr(&path
[hba_len
], '/');
2177 pr_err("%s - invalid path name\n", path
+ hba_len
);
2182 if ((sscanf(&path
[hba_len
], "ata%d", &port
) == 1) ||
2183 ((sscanf(&path
[hba_len
], "host%d", &port
) == 1)))
2187 *c
= '/'; /* repair the full string */
2188 pr_err("failed to determine port number for %s\n",
2195 /* mark this port as used */
2196 port_mask
&= ~(1 << port
);
2198 /* print out the device information */
2200 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
2204 fd
= dev_open(ent
->d_name
, O_RDONLY
);
2206 printf(" Port%d : - disk info unavailable -\n", port
);
2208 fd2devname(fd
, buf
);
2209 printf(" Port%d : %s", port
, buf
);
2210 if (imsm_read_serial(fd
, NULL
, (__u8
*) buf
) == 0)
2211 printf(" (%.*s)\n", MAX_RAID_SERIAL_LEN
, buf
);
2226 for (i
= 0; i
< port_count
; i
++)
2227 if (port_mask
& (1 << i
))
2228 printf(" Port%d : - no device attached -\n", i
);
2234 static int print_vmd_attached_devs(struct sys_dev
*hba
)
2242 if (hba
->type
!= SYS_DEV_VMD
)
2245 /* scroll through /sys/dev/block looking for devices attached to
2248 dir
= opendir("/sys/bus/pci/drivers/nvme");
2252 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2255 /* is 'ent' a device? check that the 'subsystem' link exists and
2256 * that its target matches 'bus'
2258 sprintf(path
, "/sys/bus/pci/drivers/nvme/%s/subsystem",
2260 n
= readlink(path
, link
, sizeof(link
));
2261 if (n
< 0 || n
>= (int)sizeof(link
))
2264 c
= strrchr(link
, '/');
2267 if (strncmp("pci", c
+1, strlen("pci")) != 0)
2270 sprintf(path
, "/sys/bus/pci/drivers/nvme/%s", ent
->d_name
);
2272 rp
= realpath(path
, NULL
);
2276 if (path_attached_to_hba(rp
, hba
->path
)) {
2277 printf(" NVMe under VMD : %s\n", rp
);
2286 static void print_found_intel_controllers(struct sys_dev
*elem
)
2288 for (; elem
; elem
= elem
->next
) {
2289 pr_err("found Intel(R) ");
2290 if (elem
->type
== SYS_DEV_SATA
)
2291 fprintf(stderr
, "SATA ");
2292 else if (elem
->type
== SYS_DEV_SAS
)
2293 fprintf(stderr
, "SAS ");
2294 else if (elem
->type
== SYS_DEV_NVME
)
2295 fprintf(stderr
, "NVMe ");
2297 if (elem
->type
== SYS_DEV_VMD
)
2298 fprintf(stderr
, "VMD domain");
2300 fprintf(stderr
, "RAID controller");
2303 fprintf(stderr
, " at %s", elem
->pci_id
);
2304 fprintf(stderr
, ".\n");
2309 static int ahci_get_port_count(const char *hba_path
, int *port_count
)
2316 if ((dir
= opendir(hba_path
)) == NULL
)
2319 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
2322 if ((sscanf(ent
->d_name
, "ata%d", &host
) != 1) &&
2323 ((sscanf(ent
->d_name
, "host%d", &host
) != 1)))
2325 if (*port_count
== 0)
2327 else if (host
< host_base
)
2330 if (host
+ 1 > *port_count
+ host_base
)
2331 *port_count
= host
+ 1 - host_base
;
2337 static void print_imsm_capability(const struct imsm_orom
*orom
)
2339 printf(" Platform : Intel(R) ");
2340 if (orom
->capabilities
== 0 && orom
->driver_features
== 0)
2341 printf("Matrix Storage Manager\n");
2343 printf("Rapid Storage Technology%s\n",
2344 imsm_orom_is_enterprise(orom
) ? " enterprise" : "");
2345 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
)
2346 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
,
2347 orom
->minor_ver
, orom
->hotfix_ver
, orom
->build
);
2348 printf(" RAID Levels :%s%s%s%s%s\n",
2349 imsm_orom_has_raid0(orom
) ? " raid0" : "",
2350 imsm_orom_has_raid1(orom
) ? " raid1" : "",
2351 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
2352 imsm_orom_has_raid10(orom
) ? " raid10" : "",
2353 imsm_orom_has_raid5(orom
) ? " raid5" : "");
2354 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2355 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
2356 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
2357 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
2358 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
2359 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
2360 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
2361 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
2362 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
2363 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
2364 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
2365 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
2366 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
2367 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
2368 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
2369 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
2370 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
2371 printf(" 2TB volumes :%s supported\n",
2372 (orom
->attr
& IMSM_OROM_ATTR_2TB
)?"":" not");
2373 printf(" 2TB disks :%s supported\n",
2374 (orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
)?"":" not");
2375 printf(" Max Disks : %d\n", orom
->tds
);
2376 printf(" Max Volumes : %d per array, %d per %s\n",
2377 orom
->vpa
, orom
->vphba
,
2378 imsm_orom_is_nvme(orom
) ? "platform" : "controller");
2382 static void print_imsm_capability_export(const struct imsm_orom
*orom
)
2384 printf("MD_FIRMWARE_TYPE=imsm\n");
2385 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
)
2386 printf("IMSM_VERSION=%d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
2387 orom
->hotfix_ver
, orom
->build
);
2388 printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n",
2389 imsm_orom_has_raid0(orom
) ? "raid0 " : "",
2390 imsm_orom_has_raid1(orom
) ? "raid1 " : "",
2391 imsm_orom_has_raid1e(orom
) ? "raid1e " : "",
2392 imsm_orom_has_raid5(orom
) ? "raid10 " : "",
2393 imsm_orom_has_raid10(orom
) ? "raid5 " : "");
2394 printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2395 imsm_orom_has_chunk(orom
, 2) ? "2k " : "",
2396 imsm_orom_has_chunk(orom
, 4) ? "4k " : "",
2397 imsm_orom_has_chunk(orom
, 8) ? "8k " : "",
2398 imsm_orom_has_chunk(orom
, 16) ? "16k " : "",
2399 imsm_orom_has_chunk(orom
, 32) ? "32k " : "",
2400 imsm_orom_has_chunk(orom
, 64) ? "64k " : "",
2401 imsm_orom_has_chunk(orom
, 128) ? "128k " : "",
2402 imsm_orom_has_chunk(orom
, 256) ? "256k " : "",
2403 imsm_orom_has_chunk(orom
, 512) ? "512k " : "",
2404 imsm_orom_has_chunk(orom
, 1024*1) ? "1M " : "",
2405 imsm_orom_has_chunk(orom
, 1024*2) ? "2M " : "",
2406 imsm_orom_has_chunk(orom
, 1024*4) ? "4M " : "",
2407 imsm_orom_has_chunk(orom
, 1024*8) ? "8M " : "",
2408 imsm_orom_has_chunk(orom
, 1024*16) ? "16M " : "",
2409 imsm_orom_has_chunk(orom
, 1024*32) ? "32M " : "",
2410 imsm_orom_has_chunk(orom
, 1024*64) ? "64M " : "");
2411 printf("IMSM_2TB_VOLUMES=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB
) ? "yes" : "no");
2412 printf("IMSM_2TB_DISKS=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) ? "yes" : "no");
2413 printf("IMSM_MAX_DISKS=%d\n",orom
->tds
);
2414 printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom
->vpa
);
2415 printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom
->vphba
);
2418 static int detail_platform_imsm(int verbose
, int enumerate_only
, char *controller_path
)
2420 /* There are two components to imsm platform support, the ahci SATA
2421 * controller and the option-rom. To find the SATA controller we
2422 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
2423 * controller with the Intel vendor id is present. This approach
2424 * allows mdadm to leverage the kernel's ahci detection logic, with the
2425 * caveat that if ahci.ko is not loaded mdadm will not be able to
2426 * detect platform raid capabilities. The option-rom resides in a
2427 * platform "Adapter ROM". We scan for its signature to retrieve the
2428 * platform capabilities. If raid support is disabled in the BIOS the
2429 * option-rom capability structure will not be available.
2431 struct sys_dev
*list
, *hba
;
2436 if (enumerate_only
) {
2437 if (check_env("IMSM_NO_PLATFORM"))
2439 list
= find_intel_devices();
2442 for (hba
= list
; hba
; hba
= hba
->next
) {
2443 if (find_imsm_capability(hba
)) {
2453 list
= find_intel_devices();
2456 pr_err("no active Intel(R) RAID controller found.\n");
2458 } else if (verbose
> 0)
2459 print_found_intel_controllers(list
);
2461 for (hba
= list
; hba
; hba
= hba
->next
) {
2462 if (controller_path
&& (compare_paths(hba
->path
, controller_path
) != 0))
2464 if (!find_imsm_capability(hba
)) {
2466 pr_err("imsm capabilities not found for controller: %s (type %s)\n",
2467 hba
->type
== SYS_DEV_VMD
? vmd_domain_to_controller(hba
, buf
) : hba
->path
,
2468 get_sys_dev_type(hba
->type
));
2474 if (controller_path
&& result
== 1) {
2475 pr_err("no active Intel(R) RAID controller found under %s\n",
2480 const struct orom_entry
*entry
;
2482 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2483 if (entry
->type
== SYS_DEV_VMD
) {
2484 print_imsm_capability(&entry
->orom
);
2485 printf(" 3rd party NVMe :%s supported\n",
2486 imsm_orom_has_tpv_support(&entry
->orom
)?"":" not");
2487 for (hba
= list
; hba
; hba
= hba
->next
) {
2488 if (hba
->type
== SYS_DEV_VMD
) {
2490 printf(" I/O Controller : %s (%s)\n",
2491 vmd_domain_to_controller(hba
, buf
), get_sys_dev_type(hba
->type
));
2492 if (print_vmd_attached_devs(hba
)) {
2494 pr_err("failed to get devices attached to VMD domain.\n");
2503 print_imsm_capability(&entry
->orom
);
2504 if (entry
->type
== SYS_DEV_NVME
) {
2505 for (hba
= list
; hba
; hba
= hba
->next
) {
2506 if (hba
->type
== SYS_DEV_NVME
)
2507 printf(" NVMe Device : %s\n", hba
->path
);
2513 struct devid_list
*devid
;
2514 for (devid
= entry
->devid_list
; devid
; devid
= devid
->next
) {
2515 hba
= device_by_id(devid
->devid
);
2519 printf(" I/O Controller : %s (%s)\n",
2520 hba
->path
, get_sys_dev_type(hba
->type
));
2521 if (hba
->type
== SYS_DEV_SATA
) {
2522 host_base
= ahci_get_port_count(hba
->path
, &port_count
);
2523 if (ahci_enumerate_ports(hba
->path
, port_count
, host_base
, verbose
)) {
2525 pr_err("failed to enumerate ports on SATA controller at %s.\n", hba
->pci_id
);
2536 static int export_detail_platform_imsm(int verbose
, char *controller_path
)
2538 struct sys_dev
*list
, *hba
;
2541 list
= find_intel_devices();
2544 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n");
2549 for (hba
= list
; hba
; hba
= hba
->next
) {
2550 if (controller_path
&& (compare_paths(hba
->path
,controller_path
) != 0))
2552 if (!find_imsm_capability(hba
) && verbose
> 0) {
2554 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n",
2555 hba
->type
== SYS_DEV_VMD
? vmd_domain_to_controller(hba
, buf
) : hba
->path
);
2561 const struct orom_entry
*entry
;
2563 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2564 if (entry
->type
== SYS_DEV_VMD
) {
2565 for (hba
= list
; hba
; hba
= hba
->next
)
2566 print_imsm_capability_export(&entry
->orom
);
2569 print_imsm_capability_export(&entry
->orom
);
2577 static int match_home_imsm(struct supertype
*st
, char *homehost
)
2579 /* the imsm metadata format does not specify any host
2580 * identification information. We return -1 since we can never
2581 * confirm nor deny whether a given array is "meant" for this
2582 * host. We rely on compare_super and the 'family_num' fields to
2583 * exclude member disks that do not belong, and we rely on
2584 * mdadm.conf to specify the arrays that should be assembled.
2585 * Auto-assembly may still pick up "foreign" arrays.
2591 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
2593 /* The uuid returned here is used for:
2594 * uuid to put into bitmap file (Create, Grow)
2595 * uuid for backup header when saving critical section (Grow)
2596 * comparing uuids when re-adding a device into an array
2597 * In these cases the uuid required is that of the data-array,
2598 * not the device-set.
2599 * uuid to recognise same set when adding a missing device back
2600 * to an array. This is a uuid for the device-set.
2602 * For each of these we can make do with a truncated
2603 * or hashed uuid rather than the original, as long as
2605 * In each case the uuid required is that of the data-array,
2606 * not the device-set.
2608 /* imsm does not track uuid's so we synthesis one using sha1 on
2609 * - The signature (Which is constant for all imsm array, but no matter)
2610 * - the orig_family_num of the container
2611 * - the index number of the volume
2612 * - the 'serial' number of the volume.
2613 * Hopefully these are all constant.
2615 struct intel_super
*super
= st
->sb
;
2618 struct sha1_ctx ctx
;
2619 struct imsm_dev
*dev
= NULL
;
2622 /* some mdadm versions failed to set ->orig_family_num, in which
2623 * case fall back to ->family_num. orig_family_num will be
2624 * fixed up with the first metadata update.
2626 family_num
= super
->anchor
->orig_family_num
;
2627 if (family_num
== 0)
2628 family_num
= super
->anchor
->family_num
;
2629 sha1_init_ctx(&ctx
);
2630 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
2631 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
2632 if (super
->current_vol
>= 0)
2633 dev
= get_imsm_dev(super
, super
->current_vol
);
2635 __u32 vol
= super
->current_vol
;
2636 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
2637 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
2639 sha1_finish_ctx(&ctx
, buf
);
2640 memcpy(uuid
, buf
, 4*4);
2645 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
2647 __u8
*v
= get_imsm_version(mpb
);
2648 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
2649 char major
[] = { 0, 0, 0 };
2650 char minor
[] = { 0 ,0, 0 };
2651 char patch
[] = { 0, 0, 0 };
2652 char *ver_parse
[] = { major
, minor
, patch
};
2656 while (*v
!= '\0' && v
< end
) {
2657 if (*v
!= '.' && j
< 2)
2658 ver_parse
[i
][j
++] = *v
;
2666 *m
= strtol(minor
, NULL
, 0);
2667 *p
= strtol(patch
, NULL
, 0);
2671 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
2673 /* migr_strip_size when repairing or initializing parity */
2674 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2675 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2677 switch (get_imsm_raid_level(map
)) {
2682 return 128*1024 >> 9;
2686 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
2688 /* migr_strip_size when rebuilding a degraded disk, no idea why
2689 * this is different than migr_strip_size_resync(), but it's good
2692 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2693 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2695 switch (get_imsm_raid_level(map
)) {
2698 if (map
->num_members
% map
->num_domains
== 0)
2699 return 128*1024 >> 9;
2703 return max((__u32
) 64*1024 >> 9, chunk
);
2705 return 128*1024 >> 9;
2709 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
2711 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2712 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2713 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
2714 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
2716 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
2719 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
2721 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2722 int level
= get_imsm_raid_level(lo
);
2724 if (level
== 1 || level
== 10) {
2725 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2727 return hi
->num_domains
;
2729 return num_stripes_per_unit_resync(dev
);
2732 static __u8
imsm_num_data_members(struct imsm_dev
*dev
, int second_map
)
2734 /* named 'imsm_' because raid0, raid1 and raid10
2735 * counter-intuitively have the same number of data disks
2737 struct imsm_map
*map
= get_imsm_map(dev
, second_map
);
2739 switch (get_imsm_raid_level(map
)) {
2741 return map
->num_members
;
2745 return map
->num_members
/2;
2747 return map
->num_members
- 1;
2749 dprintf("unsupported raid level\n");
2754 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
2756 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2757 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2759 switch(get_imsm_raid_level(map
)) {
2762 return chunk
* map
->num_domains
;
2764 return chunk
* map
->num_members
;
2770 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
2772 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2773 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2774 __u32 strip
= block
/ chunk
;
2776 switch (get_imsm_raid_level(map
)) {
2779 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
2780 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
2782 return vol_stripe
* chunk
+ block
% chunk
;
2784 __u32 stripe
= strip
/ (map
->num_members
- 1);
2786 return stripe
* chunk
+ block
% chunk
;
2793 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
2794 struct imsm_dev
*dev
)
2796 /* calculate the conversion factor between per member 'blocks'
2797 * (md/{resync,rebuild}_start) and imsm migration units, return
2798 * 0 for the 'not migrating' and 'unsupported migration' cases
2800 if (!dev
->vol
.migr_state
)
2803 switch (migr_type(dev
)) {
2804 case MIGR_GEN_MIGR
: {
2805 struct migr_record
*migr_rec
= super
->migr_rec
;
2806 return __le32_to_cpu(migr_rec
->blocks_per_unit
);
2811 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2812 __u32 stripes_per_unit
;
2813 __u32 blocks_per_unit
;
2822 /* yes, this is really the translation of migr_units to
2823 * per-member blocks in the 'resync' case
2825 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
2826 migr_chunk
= migr_strip_blocks_resync(dev
);
2827 disks
= imsm_num_data_members(dev
, MAP_0
);
2828 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
2829 stripe
= __le16_to_cpu(map
->blocks_per_strip
) * disks
;
2830 segment
= blocks_per_unit
/ stripe
;
2831 block_rel
= blocks_per_unit
- segment
* stripe
;
2832 parity_depth
= parity_segment_depth(dev
);
2833 block_map
= map_migr_block(dev
, block_rel
);
2834 return block_map
+ parity_depth
* segment
;
2836 case MIGR_REBUILD
: {
2837 __u32 stripes_per_unit
;
2840 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
2841 migr_chunk
= migr_strip_blocks_rebuild(dev
);
2842 return migr_chunk
* stripes_per_unit
;
2844 case MIGR_STATE_CHANGE
:
2850 static int imsm_level_to_layout(int level
)
2858 return ALGORITHM_LEFT_ASYMMETRIC
;
2865 /*******************************************************************************
2866 * Function: read_imsm_migr_rec
2867 * Description: Function reads imsm migration record from last sector of disk
2869 * fd : disk descriptor
2870 * super : metadata info
2874 ******************************************************************************/
2875 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
)
2878 unsigned int sector_size
= super
->sector_size
;
2879 unsigned long long dsize
;
2881 get_dev_size(fd
, NULL
, &dsize
);
2882 if (lseek64(fd
, dsize
- (sector_size
*MIGR_REC_SECTOR_POSITION
),
2884 pr_err("Cannot seek to anchor block: %s\n",
2888 if ((unsigned int)read(fd
, super
->migr_rec_buf
,
2889 MIGR_REC_BUF_SECTORS
*sector_size
) !=
2890 MIGR_REC_BUF_SECTORS
*sector_size
) {
2891 pr_err("Cannot read migr record block: %s\n",
2896 if (sector_size
== 4096)
2897 convert_from_4k_imsm_migr_rec(super
);
2903 static struct imsm_dev
*imsm_get_device_during_migration(
2904 struct intel_super
*super
)
2907 struct intel_dev
*dv
;
2909 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
2910 if (is_gen_migration(dv
->dev
))
2916 /*******************************************************************************
2917 * Function: load_imsm_migr_rec
2918 * Description: Function reads imsm migration record (it is stored at the last
2921 * super : imsm internal array info
2922 * info : general array info
2926 * -2 : no migration in progress
2927 ******************************************************************************/
2928 static int load_imsm_migr_rec(struct intel_super
*super
, struct mdinfo
*info
)
2935 struct imsm_dev
*dev
;
2936 struct imsm_map
*map
;
2939 /* find map under migration */
2940 dev
= imsm_get_device_during_migration(super
);
2941 /* nothing to load,no migration in progress?
2947 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
2948 /* read only from one of the first two slots */
2949 if ((sd
->disk
.raid_disk
< 0) ||
2950 (sd
->disk
.raid_disk
> 1))
2953 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
2954 fd
= dev_open(nm
, O_RDONLY
);
2960 map
= get_imsm_map(dev
, MAP_0
);
2961 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
2962 /* skip spare and failed disks
2966 /* read only from one of the first two slots */
2968 slot
= get_imsm_disk_slot(map
, dl
->index
);
2969 if (map
== NULL
|| slot
> 1 || slot
< 0)
2971 sprintf(nm
, "%d:%d", dl
->major
, dl
->minor
);
2972 fd
= dev_open(nm
, O_RDONLY
);
2979 retval
= read_imsm_migr_rec(fd
, super
);
2988 /*******************************************************************************
2989 * function: imsm_create_metadata_checkpoint_update
2990 * Description: It creates update for checkpoint change.
2992 * super : imsm internal array info
2993 * u : pointer to prepared update
2996 * If length is equal to 0, input pointer u contains no update
2997 ******************************************************************************/
2998 static int imsm_create_metadata_checkpoint_update(
2999 struct intel_super
*super
,
3000 struct imsm_update_general_migration_checkpoint
**u
)
3003 int update_memory_size
= 0;
3005 dprintf("(enter)\n");
3011 /* size of all update data without anchor */
3012 update_memory_size
=
3013 sizeof(struct imsm_update_general_migration_checkpoint
);
3015 *u
= xcalloc(1, update_memory_size
);
3017 dprintf("error: cannot get memory\n");
3020 (*u
)->type
= update_general_migration_checkpoint
;
3021 (*u
)->curr_migr_unit
= __le32_to_cpu(super
->migr_rec
->curr_migr_unit
);
3022 dprintf("prepared for %u\n", (*u
)->curr_migr_unit
);
3024 return update_memory_size
;
3027 static void imsm_update_metadata_locally(struct supertype
*st
,
3028 void *buf
, int len
);
3030 /*******************************************************************************
3031 * Function: write_imsm_migr_rec
3032 * Description: Function writes imsm migration record
3033 * (at the last sector of disk)
3035 * super : imsm internal array info
3039 ******************************************************************************/
3040 static int write_imsm_migr_rec(struct supertype
*st
)
3042 struct intel_super
*super
= st
->sb
;
3043 unsigned int sector_size
= super
->sector_size
;
3044 unsigned long long dsize
;
3050 struct imsm_update_general_migration_checkpoint
*u
;
3051 struct imsm_dev
*dev
;
3052 struct imsm_map
*map
;
3054 /* find map under migration */
3055 dev
= imsm_get_device_during_migration(super
);
3056 /* if no migration, write buffer anyway to clear migr_record
3057 * on disk based on first available device
3060 dev
= get_imsm_dev(super
, super
->current_vol
< 0 ? 0 :
3061 super
->current_vol
);
3063 map
= get_imsm_map(dev
, MAP_0
);
3065 if (sector_size
== 4096)
3066 convert_to_4k_imsm_migr_rec(super
);
3067 for (sd
= super
->disks
; sd
; sd
= sd
->next
) {
3070 /* skip failed and spare devices */
3073 /* write to 2 first slots only */
3075 slot
= get_imsm_disk_slot(map
, sd
->index
);
3076 if (map
== NULL
|| slot
> 1 || slot
< 0)
3079 sprintf(nm
, "%d:%d", sd
->major
, sd
->minor
);
3080 fd
= dev_open(nm
, O_RDWR
);
3083 get_dev_size(fd
, NULL
, &dsize
);
3084 if (lseek64(fd
, dsize
- (MIGR_REC_SECTOR_POSITION
*sector_size
),
3086 pr_err("Cannot seek to anchor block: %s\n",
3090 if ((unsigned int)write(fd
, super
->migr_rec_buf
,
3091 MIGR_REC_BUF_SECTORS
*sector_size
) !=
3092 MIGR_REC_BUF_SECTORS
*sector_size
) {
3093 pr_err("Cannot write migr record block: %s\n",
3100 if (sector_size
== 4096)
3101 convert_from_4k_imsm_migr_rec(super
);
3102 /* update checkpoint information in metadata */
3103 len
= imsm_create_metadata_checkpoint_update(super
, &u
);
3105 dprintf("imsm: Cannot prepare update\n");
3108 /* update metadata locally */
3109 imsm_update_metadata_locally(st
, u
, len
);
3110 /* and possibly remotely */
3111 if (st
->update_tail
) {
3112 append_metadata_update(st
, u
, len
);
3113 /* during reshape we do all work inside metadata handler
3114 * manage_reshape(), so metadata update has to be triggered
3117 flush_metadata_updates(st
);
3118 st
->update_tail
= &st
->updates
;
3128 #endif /* MDASSEMBLE */
3130 /* spare/missing disks activations are not allowe when
3131 * array/container performs reshape operation, because
3132 * all arrays in container works on the same disks set
3134 int imsm_reshape_blocks_arrays_changes(struct intel_super
*super
)
3137 struct intel_dev
*i_dev
;
3138 struct imsm_dev
*dev
;
3140 /* check whole container
3142 for (i_dev
= super
->devlist
; i_dev
; i_dev
= i_dev
->next
) {
3144 if (is_gen_migration(dev
)) {
3145 /* No repair during any migration in container
3153 static unsigned long long imsm_component_size_aligment_check(int level
,
3155 unsigned int sector_size
,
3156 unsigned long long component_size
)
3158 unsigned int component_size_alligment
;
3160 /* check component size aligment
3162 component_size_alligment
= component_size
% (chunk_size
/sector_size
);
3164 dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alligment = %u\n",
3165 level
, chunk_size
, component_size
,
3166 component_size_alligment
);
3168 if (component_size_alligment
&& (level
!= 1) && (level
!= UnSet
)) {
3169 dprintf("imsm: reported component size alligned from %llu ",
3171 component_size
-= component_size_alligment
;
3172 dprintf_cont("to %llu (%i).\n",
3173 component_size
, component_size_alligment
);
3176 return component_size
;
3179 static unsigned long long get_ppl_sector(struct intel_super
*super
, int dev_idx
)
3181 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
3182 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3184 return pba_of_lba0(map
) +
3185 (num_data_stripes(map
) * map
->blocks_per_strip
);
3188 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
3190 struct intel_super
*super
= st
->sb
;
3191 struct migr_record
*migr_rec
= super
->migr_rec
;
3192 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
3193 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3194 struct imsm_map
*prev_map
= get_imsm_map(dev
, MAP_1
);
3195 struct imsm_map
*map_to_analyse
= map
;
3197 int map_disks
= info
->array
.raid_disks
;
3199 memset(info
, 0, sizeof(*info
));
3201 map_to_analyse
= prev_map
;
3203 dl
= super
->current_disk
;
3205 info
->container_member
= super
->current_vol
;
3206 info
->array
.raid_disks
= map
->num_members
;
3207 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
3208 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
3209 info
->array
.md_minor
= -1;
3210 info
->array
.ctime
= 0;
3211 info
->array
.utime
= 0;
3212 info
->array
.chunk_size
=
3213 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
3214 info
->array
.state
= !(dev
->vol
.dirty
& RAIDVOL_DIRTY
);
3215 info
->custom_array_size
= __le32_to_cpu(dev
->size_high
);
3216 info
->custom_array_size
<<= 32;
3217 info
->custom_array_size
|= __le32_to_cpu(dev
->size_low
);
3218 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
3220 if (is_gen_migration(dev
)) {
3221 info
->reshape_active
= 1;
3222 info
->new_level
= get_imsm_raid_level(map
);
3223 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
3224 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
3225 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
3226 if (info
->delta_disks
) {
3227 /* this needs to be applied to every array
3230 info
->reshape_active
= CONTAINER_RESHAPE
;
3232 /* We shape information that we give to md might have to be
3233 * modify to cope with md's requirement for reshaping arrays.
3234 * For example, when reshaping a RAID0, md requires it to be
3235 * presented as a degraded RAID4.
3236 * Also if a RAID0 is migrating to a RAID5 we need to specify
3237 * the array as already being RAID5, but the 'before' layout
3238 * is a RAID4-like layout.
3240 switch (info
->array
.level
) {
3242 switch(info
->new_level
) {
3244 /* conversion is happening as RAID4 */
3245 info
->array
.level
= 4;
3246 info
->array
.raid_disks
+= 1;
3249 /* conversion is happening as RAID5 */
3250 info
->array
.level
= 5;
3251 info
->array
.layout
= ALGORITHM_PARITY_N
;
3252 info
->delta_disks
-= 1;
3255 /* FIXME error message */
3256 info
->array
.level
= UnSet
;
3262 info
->new_level
= UnSet
;
3263 info
->new_layout
= UnSet
;
3264 info
->new_chunk
= info
->array
.chunk_size
;
3265 info
->delta_disks
= 0;
3269 info
->disk
.major
= dl
->major
;
3270 info
->disk
.minor
= dl
->minor
;
3271 info
->disk
.number
= dl
->index
;
3272 info
->disk
.raid_disk
= get_imsm_disk_slot(map_to_analyse
,
3276 info
->data_offset
= pba_of_lba0(map_to_analyse
);
3278 if (info
->array
.level
== 5) {
3279 info
->component_size
= num_data_stripes(map_to_analyse
) *
3280 map_to_analyse
->blocks_per_strip
;
3282 info
->component_size
= blocks_per_member(map_to_analyse
);
3285 info
->component_size
= imsm_component_size_aligment_check(
3287 info
->array
.chunk_size
,
3289 info
->component_size
);
3290 info
->bb
.supported
= 1;
3292 memset(info
->uuid
, 0, sizeof(info
->uuid
));
3293 info
->recovery_start
= MaxSector
;
3295 if (info
->array
.level
== 5 && dev
->rwh_policy
== RWH_DISTRIBUTED
) {
3296 info
->consistency_policy
= CONSISTENCY_POLICY_PPL
;
3297 info
->ppl_sector
= get_ppl_sector(super
, super
->current_vol
);
3298 info
->ppl_size
= (PPL_HEADER_SIZE
+ PPL_ENTRY_SPACE
) >> 9;
3299 } else if (info
->array
.level
<= 0) {
3300 info
->consistency_policy
= CONSISTENCY_POLICY_NONE
;
3302 info
->consistency_policy
= CONSISTENCY_POLICY_RESYNC
;
3305 info
->reshape_progress
= 0;
3306 info
->resync_start
= MaxSector
;
3307 if ((map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
3308 !(info
->array
.state
& 1)) &&
3309 imsm_reshape_blocks_arrays_changes(super
) == 0) {
3310 info
->resync_start
= 0;
3312 if (dev
->vol
.migr_state
) {
3313 switch (migr_type(dev
)) {
3316 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
3318 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
3320 info
->resync_start
= blocks_per_unit
* units
;
3323 case MIGR_GEN_MIGR
: {
3324 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
3326 __u64 units
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
3327 unsigned long long array_blocks
;
3330 if (__le32_to_cpu(migr_rec
->ascending_migr
) &&
3332 (__le32_to_cpu(migr_rec
->num_migr_units
)-1)) &&
3333 (super
->migr_rec
->rec_status
==
3334 __cpu_to_le32(UNIT_SRC_IN_CP_AREA
)))
3337 info
->reshape_progress
= blocks_per_unit
* units
;
3339 dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n",
3340 (unsigned long long)units
,
3341 (unsigned long long)blocks_per_unit
,
3342 info
->reshape_progress
);
3344 used_disks
= imsm_num_data_members(dev
, MAP_1
);
3345 if (used_disks
> 0) {
3346 array_blocks
= blocks_per_member(map
) *
3348 info
->custom_array_size
=
3349 round_size_to_mb(array_blocks
,
3355 /* we could emulate the checkpointing of
3356 * 'sync_action=check' migrations, but for now
3357 * we just immediately complete them
3360 /* this is handled by container_content_imsm() */
3361 case MIGR_STATE_CHANGE
:
3362 /* FIXME handle other migrations */
3364 /* we are not dirty, so... */
3365 info
->resync_start
= MaxSector
;
3369 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
3370 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
3372 info
->array
.major_version
= -1;
3373 info
->array
.minor_version
= -2;
3374 sprintf(info
->text_version
, "/%s/%d", st
->container_devnm
, info
->container_member
);
3375 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
3376 uuid_from_super_imsm(st
, info
->uuid
);
3380 for (i
=0; i
<map_disks
; i
++) {
3382 if (i
< info
->array
.raid_disks
) {
3383 struct imsm_disk
*dsk
;
3384 j
= get_imsm_disk_idx(dev
, i
, MAP_X
);
3385 dsk
= get_imsm_disk(super
, j
);
3386 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
3393 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
3394 int failed
, int look_in_map
);
3396 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
3400 static void manage_second_map(struct intel_super
*super
, struct imsm_dev
*dev
)
3402 if (is_gen_migration(dev
)) {
3405 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
3407 failed
= imsm_count_failed(super
, dev
, MAP_1
);
3408 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_1
);
3409 if (map2
->map_state
!= map_state
) {
3410 map2
->map_state
= map_state
;
3411 super
->updates_pending
++;
3417 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
3421 for (d
= super
->missing
; d
; d
= d
->next
)
3422 if (d
->index
== index
)
3427 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
3429 struct intel_super
*super
= st
->sb
;
3430 struct imsm_disk
*disk
;
3431 int map_disks
= info
->array
.raid_disks
;
3432 int max_enough
= -1;
3434 struct imsm_super
*mpb
;
3436 if (super
->current_vol
>= 0) {
3437 getinfo_super_imsm_volume(st
, info
, map
);
3440 memset(info
, 0, sizeof(*info
));
3442 /* Set raid_disks to zero so that Assemble will always pull in valid
3445 info
->array
.raid_disks
= 0;
3446 info
->array
.level
= LEVEL_CONTAINER
;
3447 info
->array
.layout
= 0;
3448 info
->array
.md_minor
= -1;
3449 info
->array
.ctime
= 0; /* N/A for imsm */
3450 info
->array
.utime
= 0;
3451 info
->array
.chunk_size
= 0;
3453 info
->disk
.major
= 0;
3454 info
->disk
.minor
= 0;
3455 info
->disk
.raid_disk
= -1;
3456 info
->reshape_active
= 0;
3457 info
->array
.major_version
= -1;
3458 info
->array
.minor_version
= -2;
3459 strcpy(info
->text_version
, "imsm");
3460 info
->safe_mode_delay
= 0;
3461 info
->disk
.number
= -1;
3462 info
->disk
.state
= 0;
3464 info
->recovery_start
= MaxSector
;
3465 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
3466 info
->bb
.supported
= 1;
3468 /* do we have the all the insync disks that we expect? */
3469 mpb
= super
->anchor
;
3470 info
->events
= __le32_to_cpu(mpb
->generation_num
);
3472 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3473 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3474 int failed
, enough
, j
, missing
= 0;
3475 struct imsm_map
*map
;
3478 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3479 state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3480 map
= get_imsm_map(dev
, MAP_0
);
3482 /* any newly missing disks?
3483 * (catches single-degraded vs double-degraded)
3485 for (j
= 0; j
< map
->num_members
; j
++) {
3486 __u32 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
3487 __u32 idx
= ord_to_idx(ord
);
3489 if (!(ord
& IMSM_ORD_REBUILD
) &&
3490 get_imsm_missing(super
, idx
)) {
3496 if (state
== IMSM_T_STATE_FAILED
)
3498 else if (state
== IMSM_T_STATE_DEGRADED
&&
3499 (state
!= map
->map_state
|| missing
))
3501 else /* we're normal, or already degraded */
3503 if (is_gen_migration(dev
) && missing
) {
3504 /* during general migration we need all disks
3505 * that process is running on.
3506 * No new missing disk is allowed.
3510 /* no more checks necessary
3514 /* in the missing/failed disk case check to see
3515 * if at least one array is runnable
3517 max_enough
= max(max_enough
, enough
);
3519 dprintf("enough: %d\n", max_enough
);
3520 info
->container_enough
= max_enough
;
3523 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
3525 disk
= &super
->disks
->disk
;
3526 info
->data_offset
= total_blocks(&super
->disks
->disk
) - reserved
;
3527 info
->component_size
= reserved
;
3528 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
3529 /* we don't change info->disk.raid_disk here because
3530 * this state will be finalized in mdmon after we have
3531 * found the 'most fresh' version of the metadata
3533 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3534 info
->disk
.state
|= (is_spare(disk
) || is_journal(disk
)) ?
3535 0 : (1 << MD_DISK_SYNC
);
3538 /* only call uuid_from_super_imsm when this disk is part of a populated container,
3539 * ->compare_super may have updated the 'num_raid_devs' field for spares
3541 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
3542 uuid_from_super_imsm(st
, info
->uuid
);
3544 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
3546 /* I don't know how to compute 'map' on imsm, so use safe default */
3549 for (i
= 0; i
< map_disks
; i
++)
3555 /* allocates memory and fills disk in mdinfo structure
3556 * for each disk in array */
3557 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
3559 struct mdinfo
*mddev
;
3560 struct intel_super
*super
= st
->sb
;
3561 struct imsm_disk
*disk
;
3564 if (!super
|| !super
->disks
)
3567 mddev
= xcalloc(1, sizeof(*mddev
));
3571 tmp
= xcalloc(1, sizeof(*tmp
));
3573 tmp
->next
= mddev
->devs
;
3575 tmp
->disk
.number
= count
++;
3576 tmp
->disk
.major
= dl
->major
;
3577 tmp
->disk
.minor
= dl
->minor
;
3578 tmp
->disk
.state
= is_configured(disk
) ?
3579 (1 << MD_DISK_ACTIVE
) : 0;
3580 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3581 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
3582 tmp
->disk
.raid_disk
= -1;
3588 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
3589 char *update
, char *devname
, int verbose
,
3590 int uuid_set
, char *homehost
)
3592 /* For 'assemble' and 'force' we need to return non-zero if any
3593 * change was made. For others, the return value is ignored.
3594 * Update options are:
3595 * force-one : This device looks a bit old but needs to be included,
3596 * update age info appropriately.
3597 * assemble: clear any 'faulty' flag to allow this device to
3599 * force-array: Array is degraded but being forced, mark it clean
3600 * if that will be needed to assemble it.
3602 * newdev: not used ????
3603 * grow: Array has gained a new device - this is currently for
3605 * resync: mark as dirty so a resync will happen.
3606 * name: update the name - preserving the homehost
3607 * uuid: Change the uuid of the array to match watch is given
3609 * Following are not relevant for this imsm:
3610 * sparc2.2 : update from old dodgey metadata
3611 * super-minor: change the preferred_minor number
3612 * summaries: update redundant counters.
3613 * homehost: update the recorded homehost
3614 * _reshape_progress: record new reshape_progress position.
3617 struct intel_super
*super
= st
->sb
;
3618 struct imsm_super
*mpb
;
3620 /* we can only update container info */
3621 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
3624 mpb
= super
->anchor
;
3626 if (strcmp(update
, "uuid") == 0) {
3627 /* We take this to mean that the family_num should be updated.
3628 * However that is much smaller than the uuid so we cannot really
3629 * allow an explicit uuid to be given. And it is hard to reliably
3631 * So if !uuid_set we know the current uuid is random and just used
3632 * the first 'int' and copy it to the other 3 positions.
3633 * Otherwise we require the 4 'int's to be the same as would be the
3634 * case if we are using a random uuid. So an explicit uuid will be
3635 * accepted as long as all for ints are the same... which shouldn't hurt
3638 info
->uuid
[1] = info
->uuid
[2] = info
->uuid
[3] = info
->uuid
[0];
3641 if (info
->uuid
[0] != info
->uuid
[1] ||
3642 info
->uuid
[1] != info
->uuid
[2] ||
3643 info
->uuid
[2] != info
->uuid
[3])
3649 mpb
->orig_family_num
= info
->uuid
[0];
3650 } else if (strcmp(update
, "assemble") == 0)
3655 /* successful update? recompute checksum */
3657 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
3662 static size_t disks_to_mpb_size(int disks
)
3666 size
= sizeof(struct imsm_super
);
3667 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
3668 size
+= 2 * sizeof(struct imsm_dev
);
3669 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
3670 size
+= (4 - 2) * sizeof(struct imsm_map
);
3671 /* 4 possible disk_ord_tbl's */
3672 size
+= 4 * (disks
- 1) * sizeof(__u32
);
3673 /* maximum bbm log */
3674 size
+= sizeof(struct bbm_log
);
3679 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
,
3680 unsigned long long data_offset
)
3682 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
3685 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
3688 static void free_devlist(struct intel_super
*super
)
3690 struct intel_dev
*dv
;
3692 while (super
->devlist
) {
3693 dv
= super
->devlist
->next
;
3694 free(super
->devlist
->dev
);
3695 free(super
->devlist
);
3696 super
->devlist
= dv
;
3700 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
3702 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
3705 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
3709 * 0 same, or first was empty, and second was copied
3710 * 1 second had wrong number
3712 * 3 wrong other info
3714 struct intel_super
*first
= st
->sb
;
3715 struct intel_super
*sec
= tst
->sb
;
3722 /* in platform dependent environment test if the disks
3723 * use the same Intel hba
3724 * If not on Intel hba at all, allow anything.
3726 if (!check_env("IMSM_NO_PLATFORM") && first
->hba
&& sec
->hba
) {
3727 if (first
->hba
->type
!= sec
->hba
->type
) {
3729 "HBAs of devices do not match %s != %s\n",
3730 get_sys_dev_type(first
->hba
->type
),
3731 get_sys_dev_type(sec
->hba
->type
));
3734 if (first
->orom
!= sec
->orom
) {
3736 "HBAs of devices do not match %s != %s\n",
3737 first
->hba
->pci_id
, sec
->hba
->pci_id
);
3742 /* if an anchor does not have num_raid_devs set then it is a free
3745 if (first
->anchor
->num_raid_devs
> 0 &&
3746 sec
->anchor
->num_raid_devs
> 0) {
3747 /* Determine if these disks might ever have been
3748 * related. Further disambiguation can only take place
3749 * in load_super_imsm_all
3751 __u32 first_family
= first
->anchor
->orig_family_num
;
3752 __u32 sec_family
= sec
->anchor
->orig_family_num
;
3754 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
3755 MAX_SIGNATURE_LENGTH
) != 0)
3758 if (first_family
== 0)
3759 first_family
= first
->anchor
->family_num
;
3760 if (sec_family
== 0)
3761 sec_family
= sec
->anchor
->family_num
;
3763 if (first_family
!= sec_family
)
3768 /* if 'first' is a spare promote it to a populated mpb with sec's
3771 if (first
->anchor
->num_raid_devs
== 0 &&
3772 sec
->anchor
->num_raid_devs
> 0) {
3774 struct intel_dev
*dv
;
3775 struct imsm_dev
*dev
;
3777 /* we need to copy raid device info from sec if an allocation
3778 * fails here we don't associate the spare
3780 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
3781 dv
= xmalloc(sizeof(*dv
));
3782 dev
= xmalloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
3785 dv
->next
= first
->devlist
;
3786 first
->devlist
= dv
;
3788 if (i
< sec
->anchor
->num_raid_devs
) {
3789 /* allocation failure */
3790 free_devlist(first
);
3791 pr_err("imsm: failed to associate spare\n");
3794 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
3795 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
3796 first
->anchor
->family_num
= sec
->anchor
->family_num
;
3797 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
3798 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
3799 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
3805 static void fd2devname(int fd
, char *name
)
3809 char dname
[PATH_MAX
];
3814 if (fstat(fd
, &st
) != 0)
3816 sprintf(path
, "/sys/dev/block/%d:%d",
3817 major(st
.st_rdev
), minor(st
.st_rdev
));
3819 rv
= readlink(path
, dname
, sizeof(dname
)-1);
3824 nm
= strrchr(dname
, '/');
3827 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
3831 static int nvme_get_serial(int fd
, void *buf
, size_t buf_len
)
3834 char *name
= fd2kname(fd
);
3839 if (strncmp(name
, "nvme", 4) != 0)
3842 snprintf(path
, sizeof(path
) - 1, "/sys/block/%s/device/serial", name
);
3844 return load_sys(path
, buf
, buf_len
);
3847 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
3849 static int imsm_read_serial(int fd
, char *devname
,
3850 __u8 serial
[MAX_RAID_SERIAL_LEN
])
3859 memset(buf
, 0, sizeof(buf
));
3861 rv
= nvme_get_serial(fd
, buf
, sizeof(buf
));
3864 rv
= scsi_get_serial(fd
, buf
, sizeof(buf
));
3866 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
3867 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3868 fd2devname(fd
, (char *) serial
);
3874 pr_err("Failed to retrieve serial for %s\n",
3879 /* trim all whitespace and non-printable characters and convert
3882 for (i
= 0, dest
= buf
; i
< sizeof(buf
) && buf
[i
]; i
++) {
3885 /* ':' is reserved for use in placeholder serial
3886 * numbers for missing disks
3897 /* truncate leading characters */
3898 if (len
> MAX_RAID_SERIAL_LEN
) {
3899 dest
+= len
- MAX_RAID_SERIAL_LEN
;
3900 len
= MAX_RAID_SERIAL_LEN
;
3903 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3904 memcpy(serial
, dest
, len
);
3909 static int serialcmp(__u8
*s1
, __u8
*s2
)
3911 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
3914 static void serialcpy(__u8
*dest
, __u8
*src
)
3916 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
3919 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
3923 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3924 if (serialcmp(dl
->serial
, serial
) == 0)
3930 static struct imsm_disk
*
3931 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
3935 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3936 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3938 if (serialcmp(disk
->serial
, serial
) == 0) {
3949 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3951 struct imsm_disk
*disk
;
3956 __u8 serial
[MAX_RAID_SERIAL_LEN
];
3958 rv
= imsm_read_serial(fd
, devname
, serial
);
3963 dl
= xcalloc(1, sizeof(*dl
));
3966 dl
->major
= major(stb
.st_rdev
);
3967 dl
->minor
= minor(stb
.st_rdev
);
3968 dl
->next
= super
->disks
;
3969 dl
->fd
= keep_fd
? fd
: -1;
3970 assert(super
->disks
== NULL
);
3972 serialcpy(dl
->serial
, serial
);
3975 fd2devname(fd
, name
);
3977 dl
->devname
= xstrdup(devname
);
3979 dl
->devname
= xstrdup(name
);
3981 /* look up this disk's index in the current anchor */
3982 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
3985 /* only set index on disks that are a member of a
3986 * populated contianer, i.e. one with raid_devs
3988 if (is_failed(&dl
->disk
))
3990 else if (is_spare(&dl
->disk
) || is_journal(&dl
->disk
))
3998 /* When migrating map0 contains the 'destination' state while map1
3999 * contains the current state. When not migrating map0 contains the
4000 * current state. This routine assumes that map[0].map_state is set to
4001 * the current array state before being called.
4003 * Migration is indicated by one of the following states
4004 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
4005 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
4006 * map1state=unitialized)
4007 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
4009 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
4010 * map1state=degraded)
4011 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
4014 static void migrate(struct imsm_dev
*dev
, struct intel_super
*super
,
4015 __u8 to_state
, int migr_type
)
4017 struct imsm_map
*dest
;
4018 struct imsm_map
*src
= get_imsm_map(dev
, MAP_0
);
4020 dev
->vol
.migr_state
= 1;
4021 set_migr_type(dev
, migr_type
);
4022 dev
->vol
.curr_migr_unit
= 0;
4023 dest
= get_imsm_map(dev
, MAP_1
);
4025 /* duplicate and then set the target end state in map[0] */
4026 memcpy(dest
, src
, sizeof_imsm_map(src
));
4027 if (migr_type
== MIGR_REBUILD
|| migr_type
== MIGR_GEN_MIGR
) {
4031 for (i
= 0; i
< src
->num_members
; i
++) {
4032 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
4033 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
4037 if (migr_type
== MIGR_GEN_MIGR
)
4038 /* Clear migration record */
4039 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
4041 src
->map_state
= to_state
;
4044 static void end_migration(struct imsm_dev
*dev
, struct intel_super
*super
,
4047 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
4048 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
== 0 ?
4052 /* merge any IMSM_ORD_REBUILD bits that were not successfully
4053 * completed in the last migration.
4055 * FIXME add support for raid-level-migration
4057 if (map_state
!= map
->map_state
&& (is_gen_migration(dev
) == 0) &&
4058 prev
->map_state
!= IMSM_T_STATE_UNINITIALIZED
) {
4059 /* when final map state is other than expected
4060 * merge maps (not for migration)
4064 for (i
= 0; i
< prev
->num_members
; i
++)
4065 for (j
= 0; j
< map
->num_members
; j
++)
4066 /* during online capacity expansion
4067 * disks position can be changed
4068 * if takeover is used
4070 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
4071 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
4072 map
->disk_ord_tbl
[j
] |=
4073 prev
->disk_ord_tbl
[i
];
4076 failed
= imsm_count_failed(super
, dev
, MAP_0
);
4077 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
4080 dev
->vol
.migr_state
= 0;
4081 set_migr_type(dev
, 0);
4082 dev
->vol
.curr_migr_unit
= 0;
4083 map
->map_state
= map_state
;
4087 static int parse_raid_devices(struct intel_super
*super
)
4090 struct imsm_dev
*dev_new
;
4091 size_t len
, len_migr
;
4093 size_t space_needed
= 0;
4094 struct imsm_super
*mpb
= super
->anchor
;
4096 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
4097 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
4098 struct intel_dev
*dv
;
4100 len
= sizeof_imsm_dev(dev_iter
, 0);
4101 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
4103 space_needed
+= len_migr
- len
;
4105 dv
= xmalloc(sizeof(*dv
));
4106 if (max_len
< len_migr
)
4108 if (max_len
> len_migr
)
4109 space_needed
+= max_len
- len_migr
;
4110 dev_new
= xmalloc(max_len
);
4111 imsm_copy_dev(dev_new
, dev_iter
);
4114 dv
->next
= super
->devlist
;
4115 super
->devlist
= dv
;
4118 /* ensure that super->buf is large enough when all raid devices
4121 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
4124 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
,
4125 super
->sector_size
);
4126 if (posix_memalign(&buf
, MAX_SECTOR_SIZE
, len
) != 0)
4129 memcpy(buf
, super
->buf
, super
->len
);
4130 memset(buf
+ super
->len
, 0, len
- super
->len
);
4136 super
->extra_space
+= space_needed
;
4141 /*******************************************************************************
4142 * Function: check_mpb_migr_compatibility
4143 * Description: Function checks for unsupported migration features:
4144 * - migration optimization area (pba_of_lba0)
4145 * - descending reshape (ascending_migr)
4147 * super : imsm metadata information
4149 * 0 : migration is compatible
4150 * -1 : migration is not compatible
4151 ******************************************************************************/
4152 int check_mpb_migr_compatibility(struct intel_super
*super
)
4154 struct imsm_map
*map0
, *map1
;
4155 struct migr_record
*migr_rec
= super
->migr_rec
;
4158 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
4159 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
4162 dev_iter
->vol
.migr_state
== 1 &&
4163 dev_iter
->vol
.migr_type
== MIGR_GEN_MIGR
) {
4164 /* This device is migrating */
4165 map0
= get_imsm_map(dev_iter
, MAP_0
);
4166 map1
= get_imsm_map(dev_iter
, MAP_1
);
4167 if (pba_of_lba0(map0
) != pba_of_lba0(map1
))
4168 /* migration optimization area was used */
4170 if (migr_rec
->ascending_migr
== 0
4171 && migr_rec
->dest_depth_per_unit
> 0)
4172 /* descending reshape not supported yet */
4179 static void __free_imsm(struct intel_super
*super
, int free_disks
);
4181 /* load_imsm_mpb - read matrix metadata
4182 * allocates super->mpb to be freed by free_imsm
4184 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
4186 unsigned long long dsize
;
4187 unsigned long long sectors
;
4188 unsigned int sector_size
= super
->sector_size
;
4190 struct imsm_super
*anchor
;
4193 get_dev_size(fd
, NULL
, &dsize
);
4194 if (dsize
< 2*sector_size
) {
4196 pr_err("%s: device to small for imsm\n",
4201 if (lseek64(fd
, dsize
- (sector_size
* 2), SEEK_SET
) < 0) {
4203 pr_err("Cannot seek to anchor block on %s: %s\n",
4204 devname
, strerror(errno
));
4208 if (posix_memalign((void **)&anchor
, sector_size
, sector_size
) != 0) {
4210 pr_err("Failed to allocate imsm anchor buffer on %s\n", devname
);
4213 if ((unsigned int)read(fd
, anchor
, sector_size
) != sector_size
) {
4215 pr_err("Cannot read anchor block on %s: %s\n",
4216 devname
, strerror(errno
));
4221 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
4223 pr_err("no IMSM anchor on %s\n", devname
);
4228 __free_imsm(super
, 0);
4229 /* reload capability and hba */
4231 /* capability and hba must be updated with new super allocation */
4232 find_intel_hba_capability(fd
, super
, devname
);
4233 super
->len
= ROUND_UP(anchor
->mpb_size
, sector_size
);
4234 if (posix_memalign(&super
->buf
, MAX_SECTOR_SIZE
, super
->len
) != 0) {
4236 pr_err("unable to allocate %zu byte mpb buffer\n",
4241 memcpy(super
->buf
, anchor
, sector_size
);
4243 sectors
= mpb_sectors(anchor
, sector_size
) - 1;
4246 if (posix_memalign(&super
->migr_rec_buf
, sector_size
,
4247 MIGR_REC_BUF_SECTORS
*sector_size
) != 0) {
4248 pr_err("could not allocate migr_rec buffer\n");
4252 super
->clean_migration_record_by_mdmon
= 0;
4255 check_sum
= __gen_imsm_checksum(super
->anchor
);
4256 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
4258 pr_err("IMSM checksum %x != %x on %s\n",
4260 __le32_to_cpu(super
->anchor
->check_sum
),
4268 /* read the extended mpb */
4269 if (lseek64(fd
, dsize
- (sector_size
* (2 + sectors
)), SEEK_SET
) < 0) {
4271 pr_err("Cannot seek to extended mpb on %s: %s\n",
4272 devname
, strerror(errno
));
4276 if ((unsigned int)read(fd
, super
->buf
+ sector_size
,
4277 super
->len
- sector_size
) != super
->len
- sector_size
) {
4279 pr_err("Cannot read extended mpb on %s: %s\n",
4280 devname
, strerror(errno
));
4284 check_sum
= __gen_imsm_checksum(super
->anchor
);
4285 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
4287 pr_err("IMSM checksum %x != %x on %s\n",
4288 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
4296 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
);
4298 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
4299 static void clear_hi(struct intel_super
*super
)
4301 struct imsm_super
*mpb
= super
->anchor
;
4303 if (mpb
->attributes
& MPB_ATTRIB_2TB_DISK
)
4305 for (i
= 0; i
< mpb
->num_disks
; ++i
) {
4306 struct imsm_disk
*disk
= &mpb
->disk
[i
];
4307 disk
->total_blocks_hi
= 0;
4309 for (i
= 0; i
< mpb
->num_raid_devs
; ++i
) {
4310 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4313 for (n
= 0; n
< 2; ++n
) {
4314 struct imsm_map
*map
= get_imsm_map(dev
, n
);
4317 map
->pba_of_lba0_hi
= 0;
4318 map
->blocks_per_member_hi
= 0;
4319 map
->num_data_stripes_hi
= 0;
4325 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
4329 err
= load_imsm_mpb(fd
, super
, devname
);
4332 if (super
->sector_size
== 4096)
4333 convert_from_4k(super
);
4334 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
4337 err
= parse_raid_devices(super
);
4340 err
= load_bbm_log(super
);
4345 static void __free_imsm_disk(struct dl
*d
)
4357 static void free_imsm_disks(struct intel_super
*super
)
4361 while (super
->disks
) {
4363 super
->disks
= d
->next
;
4364 __free_imsm_disk(d
);
4366 while (super
->disk_mgmt_list
) {
4367 d
= super
->disk_mgmt_list
;
4368 super
->disk_mgmt_list
= d
->next
;
4369 __free_imsm_disk(d
);
4371 while (super
->missing
) {
4373 super
->missing
= d
->next
;
4374 __free_imsm_disk(d
);
4379 /* free all the pieces hanging off of a super pointer */
4380 static void __free_imsm(struct intel_super
*super
, int free_disks
)
4382 struct intel_hba
*elem
, *next
;
4388 /* unlink capability description */
4390 if (super
->migr_rec_buf
) {
4391 free(super
->migr_rec_buf
);
4392 super
->migr_rec_buf
= NULL
;
4395 free_imsm_disks(super
);
4396 free_devlist(super
);
4400 free((void *)elem
->path
);
4406 free(super
->bbm_log
);
4410 static void free_imsm(struct intel_super
*super
)
4412 __free_imsm(super
, 1);
4413 free(super
->bb
.entries
);
4417 static void free_super_imsm(struct supertype
*st
)
4419 struct intel_super
*super
= st
->sb
;
4428 static struct intel_super
*alloc_super(void)
4430 struct intel_super
*super
= xcalloc(1, sizeof(*super
));
4432 super
->current_vol
= -1;
4433 super
->create_offset
= ~((unsigned long long) 0);
4435 super
->bb
.entries
= xmalloc(BBM_LOG_MAX_ENTRIES
*
4436 sizeof(struct md_bb_entry
));
4437 if (!super
->bb
.entries
) {
4446 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
4448 static int find_intel_hba_capability(int fd
, struct intel_super
*super
, char *devname
)
4450 struct sys_dev
*hba_name
;
4453 if (fd
< 0 || check_env("IMSM_NO_PLATFORM")) {
4458 hba_name
= find_disk_attached_hba(fd
, NULL
);
4461 pr_err("%s is not attached to Intel(R) RAID controller.\n",
4465 rv
= attach_hba_to_super(super
, hba_name
);
4468 struct intel_hba
*hba
= super
->hba
;
4470 pr_err("%s is attached to Intel(R) %s %s (%s),\n"
4471 " but the container is assigned to Intel(R) %s %s (",
4473 get_sys_dev_type(hba_name
->type
),
4474 hba_name
->type
== SYS_DEV_VMD
? "domain" : "RAID controller",
4475 hba_name
->pci_id
? : "Err!",
4476 get_sys_dev_type(super
->hba
->type
),
4477 hba
->type
== SYS_DEV_VMD
? "domain" : "RAID controller");
4480 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
4482 fprintf(stderr
, ", ");
4485 fprintf(stderr
, ").\n"
4486 " Mixing devices attached to different %s is not allowed.\n",
4487 hba_name
->type
== SYS_DEV_VMD
? "VMD domains" : "controllers");
4491 super
->orom
= find_imsm_capability(hba_name
);
4498 /* find_missing - helper routine for load_super_imsm_all that identifies
4499 * disks that have disappeared from the system. This routine relies on
4500 * the mpb being uptodate, which it is at load time.
4502 static int find_missing(struct intel_super
*super
)
4505 struct imsm_super
*mpb
= super
->anchor
;
4507 struct imsm_disk
*disk
;
4509 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4510 disk
= __get_imsm_disk(mpb
, i
);
4511 dl
= serial_to_dl(disk
->serial
, super
);
4515 dl
= xmalloc(sizeof(*dl
));
4519 dl
->devname
= xstrdup("missing");
4521 serialcpy(dl
->serial
, disk
->serial
);
4524 dl
->next
= super
->missing
;
4525 super
->missing
= dl
;
4532 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
4534 struct intel_disk
*idisk
= disk_list
;
4537 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
4539 idisk
= idisk
->next
;
4545 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
4546 struct intel_super
*super
,
4547 struct intel_disk
**disk_list
)
4549 struct imsm_disk
*d
= &super
->disks
->disk
;
4550 struct imsm_super
*mpb
= super
->anchor
;
4553 for (i
= 0; i
< tbl_size
; i
++) {
4554 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
4555 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
4557 if (tbl_mpb
->family_num
== mpb
->family_num
) {
4558 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
4559 dprintf("mpb from %d:%d matches %d:%d\n",
4560 super
->disks
->major
,
4561 super
->disks
->minor
,
4562 table
[i
]->disks
->major
,
4563 table
[i
]->disks
->minor
);
4567 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
4568 is_configured(d
) == is_configured(tbl_d
)) &&
4569 tbl_mpb
->generation_num
< mpb
->generation_num
) {
4570 /* current version of the mpb is a
4571 * better candidate than the one in
4572 * super_table, but copy over "cross
4573 * generational" status
4575 struct intel_disk
*idisk
;
4577 dprintf("mpb from %d:%d replaces %d:%d\n",
4578 super
->disks
->major
,
4579 super
->disks
->minor
,
4580 table
[i
]->disks
->major
,
4581 table
[i
]->disks
->minor
);
4583 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
4584 if (idisk
&& is_failed(&idisk
->disk
))
4585 tbl_d
->status
|= FAILED_DISK
;
4588 struct intel_disk
*idisk
;
4589 struct imsm_disk
*disk
;
4591 /* tbl_mpb is more up to date, but copy
4592 * over cross generational status before
4595 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
4596 if (disk
&& is_failed(disk
))
4597 d
->status
|= FAILED_DISK
;
4599 idisk
= disk_list_get(d
->serial
, *disk_list
);
4602 if (disk
&& is_configured(disk
))
4603 idisk
->disk
.status
|= CONFIGURED_DISK
;
4606 dprintf("mpb from %d:%d prefer %d:%d\n",
4607 super
->disks
->major
,
4608 super
->disks
->minor
,
4609 table
[i
]->disks
->major
,
4610 table
[i
]->disks
->minor
);
4618 table
[tbl_size
++] = super
;
4622 /* update/extend the merged list of imsm_disk records */
4623 for (j
= 0; j
< mpb
->num_disks
; j
++) {
4624 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
4625 struct intel_disk
*idisk
;
4627 idisk
= disk_list_get(disk
->serial
, *disk_list
);
4629 idisk
->disk
.status
|= disk
->status
;
4630 if (is_configured(&idisk
->disk
) ||
4631 is_failed(&idisk
->disk
))
4632 idisk
->disk
.status
&= ~(SPARE_DISK
);
4634 idisk
= xcalloc(1, sizeof(*idisk
));
4635 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
4636 idisk
->disk
= *disk
;
4637 idisk
->next
= *disk_list
;
4641 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
4648 static struct intel_super
*
4649 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
4652 struct imsm_super
*mpb
= super
->anchor
;
4656 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4657 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
4658 struct intel_disk
*idisk
;
4660 idisk
= disk_list_get(disk
->serial
, disk_list
);
4662 if (idisk
->owner
== owner
||
4663 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
4666 dprintf("'%.16s' owner %d != %d\n",
4667 disk
->serial
, idisk
->owner
,
4670 dprintf("unknown disk %x [%d]: %.16s\n",
4671 __le32_to_cpu(mpb
->family_num
), i
,
4677 if (ok_count
== mpb
->num_disks
)
4682 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
4684 struct intel_super
*s
;
4686 for (s
= super_list
; s
; s
= s
->next
) {
4687 if (family_num
!= s
->anchor
->family_num
)
4689 pr_err("Conflict, offlining family %#x on '%s'\n",
4690 __le32_to_cpu(family_num
), s
->disks
->devname
);
4694 static struct intel_super
*
4695 imsm_thunderdome(struct intel_super
**super_list
, int len
)
4697 struct intel_super
*super_table
[len
];
4698 struct intel_disk
*disk_list
= NULL
;
4699 struct intel_super
*champion
, *spare
;
4700 struct intel_super
*s
, **del
;
4705 memset(super_table
, 0, sizeof(super_table
));
4706 for (s
= *super_list
; s
; s
= s
->next
)
4707 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
4709 for (i
= 0; i
< tbl_size
; i
++) {
4710 struct imsm_disk
*d
;
4711 struct intel_disk
*idisk
;
4712 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
4715 d
= &s
->disks
->disk
;
4717 /* 'd' must appear in merged disk list for its
4718 * configuration to be valid
4720 idisk
= disk_list_get(d
->serial
, disk_list
);
4721 if (idisk
&& idisk
->owner
== i
)
4722 s
= validate_members(s
, disk_list
, i
);
4727 dprintf("marking family: %#x from %d:%d offline\n",
4729 super_table
[i
]->disks
->major
,
4730 super_table
[i
]->disks
->minor
);
4734 /* This is where the mdadm implementation differs from the Windows
4735 * driver which has no strict concept of a container. We can only
4736 * assemble one family from a container, so when returning a prodigal
4737 * array member to this system the code will not be able to disambiguate
4738 * the container contents that should be assembled ("foreign" versus
4739 * "local"). It requires user intervention to set the orig_family_num
4740 * to a new value to establish a new container. The Windows driver in
4741 * this situation fixes up the volume name in place and manages the
4742 * foreign array as an independent entity.
4747 for (i
= 0; i
< tbl_size
; i
++) {
4748 struct intel_super
*tbl_ent
= super_table
[i
];
4754 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
4759 if (s
&& !is_spare
) {
4760 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
4762 } else if (!s
&& !is_spare
)
4775 pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n",
4776 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
4778 /* collect all dl's onto 'champion', and update them to
4779 * champion's version of the status
4781 for (s
= *super_list
; s
; s
= s
->next
) {
4782 struct imsm_super
*mpb
= champion
->anchor
;
4783 struct dl
*dl
= s
->disks
;
4788 mpb
->attributes
|= s
->anchor
->attributes
& MPB_ATTRIB_2TB_DISK
;
4790 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4791 struct imsm_disk
*disk
;
4793 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
4796 /* only set index on disks that are a member of
4797 * a populated contianer, i.e. one with
4800 if (is_failed(&dl
->disk
))
4802 else if (is_spare(&dl
->disk
))
4808 if (i
>= mpb
->num_disks
) {
4809 struct intel_disk
*idisk
;
4811 idisk
= disk_list_get(dl
->serial
, disk_list
);
4812 if (idisk
&& is_spare(&idisk
->disk
) &&
4813 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
4821 dl
->next
= champion
->disks
;
4822 champion
->disks
= dl
;
4826 /* delete 'champion' from super_list */
4827 for (del
= super_list
; *del
; ) {
4828 if (*del
== champion
) {
4829 *del
= (*del
)->next
;
4832 del
= &(*del
)->next
;
4834 champion
->next
= NULL
;
4838 struct intel_disk
*idisk
= disk_list
;
4840 disk_list
= disk_list
->next
;
4848 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
);
4849 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
4850 int major
, int minor
, int keep_fd
);
4852 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4853 int *max
, int keep_fd
);
4855 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
4856 char *devname
, struct md_list
*devlist
,
4859 struct intel_super
*super_list
= NULL
;
4860 struct intel_super
*super
= NULL
;
4865 /* 'fd' is an opened container */
4866 err
= get_sra_super_block(fd
, &super_list
, devname
, &i
, keep_fd
);
4868 /* get super block from devlist devices */
4869 err
= get_devlist_super_block(devlist
, &super_list
, &i
, keep_fd
);
4872 /* all mpbs enter, maybe one leaves */
4873 super
= imsm_thunderdome(&super_list
, i
);
4879 if (find_missing(super
) != 0) {
4885 /* load migration record */
4886 err
= load_imsm_migr_rec(super
, NULL
);
4888 /* migration is in progress,
4889 * but migr_rec cannot be loaded,
4895 /* Check migration compatibility */
4896 if (err
== 0 && check_mpb_migr_compatibility(super
) != 0) {
4897 pr_err("Unsupported migration detected");
4899 fprintf(stderr
, " on %s\n", devname
);
4901 fprintf(stderr
, " (IMSM).\n");
4910 while (super_list
) {
4911 struct intel_super
*s
= super_list
;
4913 super_list
= super_list
->next
;
4922 strcpy(st
->container_devnm
, fd2devnm(fd
));
4924 st
->container_devnm
[0] = 0;
4925 if (err
== 0 && st
->ss
== NULL
) {
4926 st
->ss
= &super_imsm
;
4927 st
->minor_version
= 0;
4928 st
->max_devs
= IMSM_MAX_DEVICES
;
4934 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4935 int *max
, int keep_fd
)
4937 struct md_list
*tmpdev
;
4941 for (i
= 0, tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
4942 if (tmpdev
->used
!= 1)
4944 if (tmpdev
->container
== 1) {
4946 int fd
= dev_open(tmpdev
->devname
, O_RDONLY
|O_EXCL
);
4948 pr_err("cannot open device %s: %s\n",
4949 tmpdev
->devname
, strerror(errno
));
4953 err
= get_sra_super_block(fd
, super_list
,
4954 tmpdev
->devname
, &lmax
,
4963 int major
= major(tmpdev
->st_rdev
);
4964 int minor
= minor(tmpdev
->st_rdev
);
4965 err
= get_super_block(super_list
,
4982 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
4983 int major
, int minor
, int keep_fd
)
4985 struct intel_super
*s
;
4997 sprintf(nm
, "%d:%d", major
, minor
);
4998 dfd
= dev_open(nm
, O_RDWR
);
5004 get_dev_sector_size(dfd
, NULL
, &s
->sector_size
);
5005 find_intel_hba_capability(dfd
, s
, devname
);
5006 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
5008 /* retry the load if we might have raced against mdmon */
5009 if (err
== 3 && devnm
&& mdmon_running(devnm
))
5010 for (retry
= 0; retry
< 3; retry
++) {
5012 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
5018 s
->next
= *super_list
;
5026 if (dfd
>= 0 && !keep_fd
)
5033 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
)
5040 sra
= sysfs_read(fd
, NULL
, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
5044 if (sra
->array
.major_version
!= -1 ||
5045 sra
->array
.minor_version
!= -2 ||
5046 strcmp(sra
->text_version
, "imsm") != 0) {
5051 devnm
= fd2devnm(fd
);
5052 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
5053 if (get_super_block(super_list
, devnm
, devname
,
5054 sd
->disk
.major
, sd
->disk
.minor
, keep_fd
) != 0) {
5065 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
5067 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
, NULL
, 1);
5071 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
5073 struct intel_super
*super
;
5077 if (test_partition(fd
))
5078 /* IMSM not allowed on partitions */
5081 free_super_imsm(st
);
5083 super
= alloc_super();
5084 get_dev_sector_size(fd
, NULL
, &super
->sector_size
);
5087 /* Load hba and capabilities if they exist.
5088 * But do not preclude loading metadata in case capabilities or hba are
5089 * non-compliant and ignore_hw_compat is set.
5091 rv
= find_intel_hba_capability(fd
, super
, devname
);
5092 /* no orom/efi or non-intel hba of the disk */
5093 if (rv
!= 0 && st
->ignore_hw_compat
== 0) {
5095 pr_err("No OROM/EFI properties for %s\n", devname
);
5099 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
5101 /* retry the load if we might have raced against mdmon */
5103 struct mdstat_ent
*mdstat
= NULL
;
5104 char *name
= fd2kname(fd
);
5107 mdstat
= mdstat_by_component(name
);
5109 if (mdstat
&& mdmon_running(mdstat
->devnm
) && getpid() != mdmon_pid(mdstat
->devnm
)) {
5110 for (retry
= 0; retry
< 3; retry
++) {
5112 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
5118 free_mdstat(mdstat
);
5123 pr_err("Failed to load all information sections on %s\n", devname
);
5129 if (st
->ss
== NULL
) {
5130 st
->ss
= &super_imsm
;
5131 st
->minor_version
= 0;
5132 st
->max_devs
= IMSM_MAX_DEVICES
;
5135 /* load migration record */
5136 if (load_imsm_migr_rec(super
, NULL
) == 0) {
5137 /* Check for unsupported migration features */
5138 if (check_mpb_migr_compatibility(super
) != 0) {
5139 pr_err("Unsupported migration detected");
5141 fprintf(stderr
, " on %s\n", devname
);
5143 fprintf(stderr
, " (IMSM).\n");
5151 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
5153 if (info
->level
== 1)
5155 return info
->chunk_size
>> 9;
5158 static unsigned long long info_to_blocks_per_member(mdu_array_info_t
*info
,
5159 unsigned long long size
)
5161 if (info
->level
== 1)
5164 return (size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
5167 static void imsm_update_version_info(struct intel_super
*super
)
5169 /* update the version and attributes */
5170 struct imsm_super
*mpb
= super
->anchor
;
5172 struct imsm_dev
*dev
;
5173 struct imsm_map
*map
;
5176 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5177 dev
= get_imsm_dev(super
, i
);
5178 map
= get_imsm_map(dev
, MAP_0
);
5179 if (__le32_to_cpu(dev
->size_high
) > 0)
5180 mpb
->attributes
|= MPB_ATTRIB_2TB
;
5182 /* FIXME detect when an array spans a port multiplier */
5184 mpb
->attributes
|= MPB_ATTRIB_PM
;
5187 if (mpb
->num_raid_devs
> 1 ||
5188 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
5189 version
= MPB_VERSION_ATTRIBS
;
5190 switch (get_imsm_raid_level(map
)) {
5191 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
5192 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
5193 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
5194 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
5197 if (map
->num_members
>= 5)
5198 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
5199 else if (dev
->status
== DEV_CLONE_N_GO
)
5200 version
= MPB_VERSION_CNG
;
5201 else if (get_imsm_raid_level(map
) == 5)
5202 version
= MPB_VERSION_RAID5
;
5203 else if (map
->num_members
>= 3)
5204 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
5205 else if (get_imsm_raid_level(map
) == 1)
5206 version
= MPB_VERSION_RAID1
;
5208 version
= MPB_VERSION_RAID0
;
5210 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
5214 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
5216 struct imsm_super
*mpb
= super
->anchor
;
5217 char *reason
= NULL
;
5220 if (strlen(name
) > MAX_RAID_SERIAL_LEN
)
5221 reason
= "must be 16 characters or less";
5223 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5224 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
5226 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
5227 reason
= "already exists";
5232 if (reason
&& !quiet
)
5233 pr_err("imsm volume name %s\n", reason
);
5238 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
5239 struct shape
*s
, char *name
,
5240 char *homehost
, int *uuid
,
5241 long long data_offset
)
5243 /* We are creating a volume inside a pre-existing container.
5244 * so st->sb is already set.
5246 struct intel_super
*super
= st
->sb
;
5247 unsigned int sector_size
= super
->sector_size
;
5248 struct imsm_super
*mpb
= super
->anchor
;
5249 struct intel_dev
*dv
;
5250 struct imsm_dev
*dev
;
5251 struct imsm_vol
*vol
;
5252 struct imsm_map
*map
;
5253 int idx
= mpb
->num_raid_devs
;
5255 unsigned long long array_blocks
;
5256 size_t size_old
, size_new
;
5257 unsigned long long num_data_stripes
;
5258 unsigned int data_disks
;
5259 unsigned long long size_per_member
;
5261 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
5262 pr_err("This imsm-container already has the maximum of %d volumes\n", super
->orom
->vpa
);
5266 /* ensure the mpb is large enough for the new data */
5267 size_old
= __le32_to_cpu(mpb
->mpb_size
);
5268 size_new
= disks_to_mpb_size(info
->nr_disks
);
5269 if (size_new
> size_old
) {
5271 size_t size_round
= ROUND_UP(size_new
, sector_size
);
5273 if (posix_memalign(&mpb_new
, sector_size
, size_round
) != 0) {
5274 pr_err("could not allocate new mpb\n");
5277 if (posix_memalign(&super
->migr_rec_buf
, sector_size
,
5278 MIGR_REC_BUF_SECTORS
*sector_size
) != 0) {
5279 pr_err("could not allocate migr_rec buffer\n");
5285 memcpy(mpb_new
, mpb
, size_old
);
5288 super
->anchor
= mpb_new
;
5289 mpb
->mpb_size
= __cpu_to_le32(size_new
);
5290 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
5291 super
->len
= size_round
;
5293 super
->current_vol
= idx
;
5295 /* handle 'failed_disks' by either:
5296 * a) create dummy disk entries in the table if this the first
5297 * volume in the array. We add them here as this is the only
5298 * opportunity to add them. add_to_super_imsm_volume()
5299 * handles the non-failed disks and continues incrementing
5301 * b) validate that 'failed_disks' matches the current number
5302 * of missing disks if the container is populated
5304 if (super
->current_vol
== 0) {
5306 for (i
= 0; i
< info
->failed_disks
; i
++) {
5307 struct imsm_disk
*disk
;
5310 disk
= __get_imsm_disk(mpb
, i
);
5311 disk
->status
= CONFIGURED_DISK
| FAILED_DISK
;
5312 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
5313 snprintf((char *) disk
->serial
, MAX_RAID_SERIAL_LEN
,
5314 "missing:%d", (__u8
)i
);
5316 find_missing(super
);
5321 for (d
= super
->missing
; d
; d
= d
->next
)
5323 if (info
->failed_disks
> missing
) {
5324 pr_err("unable to add 'missing' disk to container\n");
5329 if (!check_name(super
, name
, 0))
5331 dv
= xmalloc(sizeof(*dv
));
5332 dev
= xcalloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
5333 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
5334 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
5335 info
->layout
, info
->chunk_size
,
5336 s
->size
* BLOCKS_PER_KB
);
5337 data_disks
= get_data_disks(info
->level
, info
->layout
,
5339 array_blocks
= round_size_to_mb(array_blocks
, data_disks
);
5340 size_per_member
= array_blocks
/ data_disks
;
5342 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
5343 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
5344 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
5346 vol
->migr_state
= 0;
5347 set_migr_type(dev
, MIGR_INIT
);
5348 vol
->dirty
= !info
->state
;
5349 vol
->curr_migr_unit
= 0;
5350 map
= get_imsm_map(dev
, MAP_0
);
5351 set_pba_of_lba0(map
, super
->create_offset
);
5352 set_blocks_per_member(map
, info_to_blocks_per_member(info
,
5355 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
5356 map
->failed_disk_num
= ~0;
5357 if (info
->level
> 0)
5358 map
->map_state
= (info
->state
? IMSM_T_STATE_NORMAL
5359 : IMSM_T_STATE_UNINITIALIZED
);
5361 map
->map_state
= info
->failed_disks
? IMSM_T_STATE_FAILED
:
5362 IMSM_T_STATE_NORMAL
;
5365 if (info
->level
== 1 && info
->raid_disks
> 2) {
5368 pr_err("imsm does not support more than 2 disksin a raid1 volume\n");
5372 map
->raid_level
= info
->level
;
5373 if (info
->level
== 10) {
5374 map
->raid_level
= 1;
5375 map
->num_domains
= info
->raid_disks
/ 2;
5376 } else if (info
->level
== 1)
5377 map
->num_domains
= info
->raid_disks
;
5379 map
->num_domains
= 1;
5381 /* info->size is only int so use the 'size' parameter instead */
5382 num_data_stripes
= size_per_member
/ info_to_blocks_per_strip(info
);
5383 num_data_stripes
/= map
->num_domains
;
5384 set_num_data_stripes(map
, num_data_stripes
);
5386 map
->num_members
= info
->raid_disks
;
5387 for (i
= 0; i
< map
->num_members
; i
++) {
5388 /* initialized in add_to_super */
5389 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
5391 mpb
->num_raid_devs
++;
5393 if (s
->consistency_policy
== UnSet
||
5394 s
->consistency_policy
== CONSISTENCY_POLICY_RESYNC
||
5395 s
->consistency_policy
== CONSISTENCY_POLICY_NONE
) {
5396 dev
->rwh_policy
= RWH_OFF
;
5397 } else if (s
->consistency_policy
== CONSISTENCY_POLICY_PPL
) {
5398 dev
->rwh_policy
= RWH_DISTRIBUTED
;
5402 pr_err("imsm does not support consistency policy %s\n",
5403 map_num(consistency_policies
, s
->consistency_policy
));
5408 dv
->index
= super
->current_vol
;
5409 dv
->next
= super
->devlist
;
5410 super
->devlist
= dv
;
5412 imsm_update_version_info(super
);
5417 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
5418 struct shape
*s
, char *name
,
5419 char *homehost
, int *uuid
,
5420 unsigned long long data_offset
)
5422 /* This is primarily called by Create when creating a new array.
5423 * We will then get add_to_super called for each component, and then
5424 * write_init_super called to write it out to each device.
5425 * For IMSM, Create can create on fresh devices or on a pre-existing
5427 * To create on a pre-existing array a different method will be called.
5428 * This one is just for fresh drives.
5430 struct intel_super
*super
;
5431 struct imsm_super
*mpb
;
5435 if (data_offset
!= INVALID_SECTORS
) {
5436 pr_err("data-offset not supported by imsm\n");
5441 return init_super_imsm_volume(st
, info
, s
, name
, homehost
, uuid
,
5445 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
5447 mpb_size
= MAX_SECTOR_SIZE
;
5449 super
= alloc_super();
5451 posix_memalign(&super
->buf
, MAX_SECTOR_SIZE
, mpb_size
) != 0) {
5456 pr_err("could not allocate superblock\n");
5459 if (posix_memalign(&super
->migr_rec_buf
, MAX_SECTOR_SIZE
,
5460 MIGR_REC_BUF_SECTORS
*MAX_SECTOR_SIZE
) != 0) {
5461 pr_err("could not allocate migr_rec buffer\n");
5466 memset(super
->buf
, 0, mpb_size
);
5468 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5472 /* zeroing superblock */
5476 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5478 version
= (char *) mpb
->sig
;
5479 strcpy(version
, MPB_SIGNATURE
);
5480 version
+= strlen(MPB_SIGNATURE
);
5481 strcpy(version
, MPB_VERSION_RAID0
);
5487 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
5488 int fd
, char *devname
)
5490 struct intel_super
*super
= st
->sb
;
5491 struct imsm_super
*mpb
= super
->anchor
;
5492 struct imsm_disk
*_disk
;
5493 struct imsm_dev
*dev
;
5494 struct imsm_map
*map
;
5498 dev
= get_imsm_dev(super
, super
->current_vol
);
5499 map
= get_imsm_map(dev
, MAP_0
);
5501 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
5502 pr_err("%s: Cannot add spare devices to IMSM volume\n",
5508 /* we're doing autolayout so grab the pre-marked (in
5509 * validate_geometry) raid_disk
5511 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5512 if (dl
->raiddisk
== dk
->raid_disk
)
5515 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5516 if (dl
->major
== dk
->major
&&
5517 dl
->minor
== dk
->minor
)
5522 pr_err("%s is not a member of the same container\n", devname
);
5526 /* add a pristine spare to the metadata */
5527 if (dl
->index
< 0) {
5528 dl
->index
= super
->anchor
->num_disks
;
5529 super
->anchor
->num_disks
++;
5531 /* Check the device has not already been added */
5532 slot
= get_imsm_disk_slot(map
, dl
->index
);
5534 (get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
) & IMSM_ORD_REBUILD
) == 0) {
5535 pr_err("%s has been included in this array twice\n",
5539 set_imsm_ord_tbl_ent(map
, dk
->raid_disk
, dl
->index
);
5540 dl
->disk
.status
= CONFIGURED_DISK
;
5542 /* update size of 'missing' disks to be at least as large as the
5543 * largest acitve member (we only have dummy missing disks when
5544 * creating the first volume)
5546 if (super
->current_vol
== 0) {
5547 for (df
= super
->missing
; df
; df
= df
->next
) {
5548 if (total_blocks(&dl
->disk
) > total_blocks(&df
->disk
))
5549 set_total_blocks(&df
->disk
, total_blocks(&dl
->disk
));
5550 _disk
= __get_imsm_disk(mpb
, df
->index
);
5555 /* refresh unset/failed slots to point to valid 'missing' entries */
5556 for (df
= super
->missing
; df
; df
= df
->next
)
5557 for (slot
= 0; slot
< mpb
->num_disks
; slot
++) {
5558 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
5560 if ((ord
& IMSM_ORD_REBUILD
) == 0)
5562 set_imsm_ord_tbl_ent(map
, slot
, df
->index
| IMSM_ORD_REBUILD
);
5563 if (is_gen_migration(dev
)) {
5564 struct imsm_map
*map2
= get_imsm_map(dev
,
5566 int slot2
= get_imsm_disk_slot(map2
, df
->index
);
5567 if (slot2
< map2
->num_members
&& slot2
>= 0) {
5568 __u32 ord2
= get_imsm_ord_tbl_ent(dev
,
5571 if ((unsigned)df
->index
==
5573 set_imsm_ord_tbl_ent(map2
,
5579 dprintf("set slot:%d to missing disk:%d\n", slot
, df
->index
);
5583 /* if we are creating the first raid device update the family number */
5584 if (super
->current_vol
== 0) {
5586 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
5588 _disk
= __get_imsm_disk(mpb
, dl
->index
);
5589 if (!_dev
|| !_disk
) {
5590 pr_err("BUG mpb setup error\n");
5596 sum
+= __gen_imsm_checksum(mpb
);
5597 mpb
->family_num
= __cpu_to_le32(sum
);
5598 mpb
->orig_family_num
= mpb
->family_num
;
5600 super
->current_disk
= dl
;
5605 * Function marks disk as spare and restores disk serial
5606 * in case it was previously marked as failed by takeover operation
5608 * -1 : critical error
5609 * 0 : disk is marked as spare but serial is not set
5612 int mark_spare(struct dl
*disk
)
5614 __u8 serial
[MAX_RAID_SERIAL_LEN
];
5621 if (!imsm_read_serial(disk
->fd
, NULL
, serial
)) {
5622 /* Restore disk serial number, because takeover marks disk
5623 * as failed and adds to serial ':0' before it becomes
5626 serialcpy(disk
->serial
, serial
);
5627 serialcpy(disk
->disk
.serial
, serial
);
5630 disk
->disk
.status
= SPARE_DISK
;
5636 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
5637 int fd
, char *devname
,
5638 unsigned long long data_offset
)
5640 struct intel_super
*super
= st
->sb
;
5642 unsigned long long size
;
5643 unsigned int member_sector_size
;
5648 /* If we are on an RAID enabled platform check that the disk is
5649 * attached to the raid controller.
5650 * We do not need to test disks attachment for container based additions,
5651 * they shall be already tested when container was created/assembled.
5653 rv
= find_intel_hba_capability(fd
, super
, devname
);
5654 /* no orom/efi or non-intel hba of the disk */
5656 dprintf("capability: %p fd: %d ret: %d\n",
5657 super
->orom
, fd
, rv
);
5661 if (super
->current_vol
>= 0)
5662 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
5665 dd
= xcalloc(sizeof(*dd
), 1);
5666 dd
->major
= major(stb
.st_rdev
);
5667 dd
->minor
= minor(stb
.st_rdev
);
5668 dd
->devname
= devname
? xstrdup(devname
) : NULL
;
5671 dd
->action
= DISK_ADD
;
5672 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
5674 pr_err("failed to retrieve scsi serial, aborting\n");
5680 if (super
->hba
&& ((super
->hba
->type
== SYS_DEV_NVME
) ||
5681 (super
->hba
->type
== SYS_DEV_VMD
))) {
5683 char *devpath
= diskfd_to_devpath(fd
);
5684 char controller_path
[PATH_MAX
];
5687 pr_err("failed to get devpath, aborting\n");
5694 snprintf(controller_path
, PATH_MAX
-1, "%s/device", devpath
);
5697 if (devpath_to_vendor(controller_path
) == 0x8086) {
5699 * If Intel's NVMe drive has serial ended with
5700 * "-A","-B","-1" or "-2" it means that this is "x8"
5701 * device (double drive on single PCIe card).
5702 * User should be warned about potential data loss.
5704 for (i
= MAX_RAID_SERIAL_LEN
-1; i
> 0; i
--) {
5705 /* Skip empty character at the end */
5706 if (dd
->serial
[i
] == 0)
5709 if (((dd
->serial
[i
] == 'A') ||
5710 (dd
->serial
[i
] == 'B') ||
5711 (dd
->serial
[i
] == '1') ||
5712 (dd
->serial
[i
] == '2')) &&
5713 (dd
->serial
[i
-1] == '-'))
5714 pr_err("\tThe action you are about to take may put your data at risk.\n"
5715 "\tPlease note that x8 devices may consist of two separate x4 devices "
5716 "located on a single PCIe port.\n"
5717 "\tRAID 0 is the only supported configuration for this type of x8 device.\n");
5720 } else if (super
->hba
->type
== SYS_DEV_VMD
&& super
->orom
&&
5721 !imsm_orom_has_tpv_support(super
->orom
)) {
5722 pr_err("\tPlatform configuration does not support non-Intel NVMe drives.\n"
5723 "\tPlease refer to Intel(R) RSTe user guide.\n");
5730 get_dev_size(fd
, NULL
, &size
);
5731 get_dev_sector_size(fd
, NULL
, &member_sector_size
);
5733 if (super
->sector_size
== 0) {
5734 /* this a first device, so sector_size is not set yet */
5735 super
->sector_size
= member_sector_size
;
5736 } else if (member_sector_size
!= super
->sector_size
) {
5737 pr_err("Mixing between different sector size is forbidden, aborting...\n");
5744 /* clear migr_rec when adding disk to container */
5745 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SECTORS
*super
->sector_size
);
5746 if (lseek64(fd
, size
- MIGR_REC_SECTOR_POSITION
*super
->sector_size
,
5748 if ((unsigned int)write(fd
, super
->migr_rec_buf
,
5749 MIGR_REC_BUF_SECTORS
*super
->sector_size
) !=
5750 MIGR_REC_BUF_SECTORS
*super
->sector_size
)
5751 perror("Write migr_rec failed");
5755 serialcpy(dd
->disk
.serial
, dd
->serial
);
5756 set_total_blocks(&dd
->disk
, size
);
5757 if (__le32_to_cpu(dd
->disk
.total_blocks_hi
) > 0) {
5758 struct imsm_super
*mpb
= super
->anchor
;
5759 mpb
->attributes
|= MPB_ATTRIB_2TB_DISK
;
5762 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
5763 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
5765 dd
->disk
.scsi_id
= __cpu_to_le32(0);
5767 if (st
->update_tail
) {
5768 dd
->next
= super
->disk_mgmt_list
;
5769 super
->disk_mgmt_list
= dd
;
5771 dd
->next
= super
->disks
;
5773 super
->updates_pending
++;
5779 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
5781 struct intel_super
*super
= st
->sb
;
5784 /* remove from super works only in mdmon - for communication
5785 * manager - monitor. Check if communication memory buffer
5788 if (!st
->update_tail
) {
5789 pr_err("shall be used in mdmon context only\n");
5792 dd
= xcalloc(1, sizeof(*dd
));
5793 dd
->major
= dk
->major
;
5794 dd
->minor
= dk
->minor
;
5797 dd
->action
= DISK_REMOVE
;
5799 dd
->next
= super
->disk_mgmt_list
;
5800 super
->disk_mgmt_list
= dd
;
5805 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
5808 char buf
[MAX_SECTOR_SIZE
];
5809 struct imsm_super anchor
;
5810 } spare_record
__attribute__ ((aligned(MAX_SECTOR_SIZE
)));
5812 /* spare records have their own family number and do not have any defined raid
5815 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
5817 struct imsm_super
*mpb
= super
->anchor
;
5818 struct imsm_super
*spare
= &spare_record
.anchor
;
5822 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
));
5823 spare
->generation_num
= __cpu_to_le32(1UL);
5824 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5825 spare
->num_disks
= 1;
5826 spare
->num_raid_devs
= 0;
5827 spare
->cache_size
= mpb
->cache_size
;
5828 spare
->pwr_cycle_count
= __cpu_to_le32(1);
5830 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
5831 MPB_SIGNATURE MPB_VERSION_RAID0
);
5833 for (d
= super
->disks
; d
; d
= d
->next
) {
5837 spare
->disk
[0] = d
->disk
;
5838 if (__le32_to_cpu(d
->disk
.total_blocks_hi
) > 0)
5839 spare
->attributes
|= MPB_ATTRIB_2TB_DISK
;
5841 if (super
->sector_size
== 4096)
5842 convert_to_4k_imsm_disk(&spare
->disk
[0]);
5844 sum
= __gen_imsm_checksum(spare
);
5845 spare
->family_num
= __cpu_to_le32(sum
);
5846 spare
->orig_family_num
= 0;
5847 sum
= __gen_imsm_checksum(spare
);
5848 spare
->check_sum
= __cpu_to_le32(sum
);
5850 if (store_imsm_mpb(d
->fd
, spare
)) {
5851 pr_err("failed for device %d:%d %s\n",
5852 d
->major
, d
->minor
, strerror(errno
));
5864 static int write_super_imsm(struct supertype
*st
, int doclose
)
5866 struct intel_super
*super
= st
->sb
;
5867 unsigned int sector_size
= super
->sector_size
;
5868 struct imsm_super
*mpb
= super
->anchor
;
5874 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
5876 int clear_migration_record
= 1;
5879 /* 'generation' is incremented everytime the metadata is written */
5880 generation
= __le32_to_cpu(mpb
->generation_num
);
5882 mpb
->generation_num
= __cpu_to_le32(generation
);
5884 /* fix up cases where previous mdadm releases failed to set
5887 if (mpb
->orig_family_num
== 0)
5888 mpb
->orig_family_num
= mpb
->family_num
;
5890 for (d
= super
->disks
; d
; d
= d
->next
) {
5894 mpb
->disk
[d
->index
] = d
->disk
;
5898 for (d
= super
->missing
; d
; d
= d
->next
) {
5899 mpb
->disk
[d
->index
] = d
->disk
;
5902 mpb
->num_disks
= num_disks
;
5903 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
5905 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5906 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
5907 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
5909 imsm_copy_dev(dev
, dev2
);
5910 mpb_size
+= sizeof_imsm_dev(dev
, 0);
5912 if (is_gen_migration(dev2
))
5913 clear_migration_record
= 0;
5916 bbm_log_size
= get_imsm_bbm_log_size(super
->bbm_log
);
5919 memcpy((void *)mpb
+ mpb_size
, super
->bbm_log
, bbm_log_size
);
5920 mpb
->attributes
|= MPB_ATTRIB_BBM
;
5922 mpb
->attributes
&= ~MPB_ATTRIB_BBM
;
5924 super
->anchor
->bbm_log_size
= __cpu_to_le32(bbm_log_size
);
5925 mpb_size
+= bbm_log_size
;
5926 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5929 assert(super
->len
== 0 || mpb_size
<= super
->len
);
5932 /* recalculate checksum */
5933 sum
= __gen_imsm_checksum(mpb
);
5934 mpb
->check_sum
= __cpu_to_le32(sum
);
5936 if (super
->clean_migration_record_by_mdmon
) {
5937 clear_migration_record
= 1;
5938 super
->clean_migration_record_by_mdmon
= 0;
5940 if (clear_migration_record
)
5941 memset(super
->migr_rec_buf
, 0,
5942 MIGR_REC_BUF_SECTORS
*sector_size
);
5944 if (sector_size
== 4096)
5945 convert_to_4k(super
);
5947 /* write the mpb for disks that compose raid devices */
5948 for (d
= super
->disks
; d
; d
= d
->next
) {
5949 if (d
->index
< 0 || is_failed(&d
->disk
))
5952 if (clear_migration_record
) {
5953 unsigned long long dsize
;
5955 get_dev_size(d
->fd
, NULL
, &dsize
);
5956 if (lseek64(d
->fd
, dsize
- sector_size
,
5958 if ((unsigned int)write(d
->fd
,
5959 super
->migr_rec_buf
,
5960 MIGR_REC_BUF_SECTORS
*sector_size
) !=
5961 MIGR_REC_BUF_SECTORS
*sector_size
)
5962 perror("Write migr_rec failed");
5966 if (store_imsm_mpb(d
->fd
, mpb
))
5968 "failed for device %d:%d (fd: %d)%s\n",
5970 d
->fd
, strerror(errno
));
5979 return write_super_imsm_spares(super
, doclose
);
5984 static int create_array(struct supertype
*st
, int dev_idx
)
5987 struct imsm_update_create_array
*u
;
5988 struct intel_super
*super
= st
->sb
;
5989 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
5990 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
5991 struct disk_info
*inf
;
5992 struct imsm_disk
*disk
;
5995 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
5996 sizeof(*inf
) * map
->num_members
;
5998 u
->type
= update_create_array
;
5999 u
->dev_idx
= dev_idx
;
6000 imsm_copy_dev(&u
->dev
, dev
);
6001 inf
= get_disk_info(u
);
6002 for (i
= 0; i
< map
->num_members
; i
++) {
6003 int idx
= get_imsm_disk_idx(dev
, i
, MAP_X
);
6005 disk
= get_imsm_disk(super
, idx
);
6007 disk
= get_imsm_missing(super
, idx
);
6008 serialcpy(inf
[i
].serial
, disk
->serial
);
6010 append_metadata_update(st
, u
, len
);
6015 static int mgmt_disk(struct supertype
*st
)
6017 struct intel_super
*super
= st
->sb
;
6019 struct imsm_update_add_remove_disk
*u
;
6021 if (!super
->disk_mgmt_list
)
6026 u
->type
= update_add_remove_disk
;
6027 append_metadata_update(st
, u
, len
);
6033 __u32
crc32c_le(__u32 crc
, unsigned char const *p
, size_t len
);
6035 static int write_init_ppl_imsm(struct supertype
*st
, struct mdinfo
*info
, int fd
)
6037 struct intel_super
*super
= st
->sb
;
6039 struct ppl_header
*ppl_hdr
;
6042 ret
= posix_memalign(&buf
, 4096, PPL_HEADER_SIZE
);
6044 pr_err("Failed to allocate PPL header buffer\n");
6048 memset(buf
, 0, PPL_HEADER_SIZE
);
6050 memset(ppl_hdr
->reserved
, 0xff, PPL_HDR_RESERVED
);
6051 ppl_hdr
->signature
= __cpu_to_le32(super
->anchor
->orig_family_num
);
6052 ppl_hdr
->checksum
= __cpu_to_le32(~crc32c_le(~0, buf
, PPL_HEADER_SIZE
));
6054 if (lseek64(fd
, info
->ppl_sector
* 512, SEEK_SET
) < 0) {
6056 perror("Failed to seek to PPL header location");
6059 if (!ret
&& write(fd
, buf
, PPL_HEADER_SIZE
) != PPL_HEADER_SIZE
) {
6061 perror("Write PPL header failed");
6071 static int validate_ppl_imsm(struct supertype
*st
, struct mdinfo
*info
,
6072 struct mdinfo
*disk
)
6074 struct intel_super
*super
= st
->sb
;
6078 struct ppl_header
*ppl_hdr
;
6080 struct imsm_dev
*dev
;
6081 struct imsm_map
*map
;
6084 if (disk
->disk
.raid_disk
< 0)
6087 if (posix_memalign(&buf
, 4096, PPL_HEADER_SIZE
)) {
6088 pr_err("Failed to allocate PPL header buffer\n");
6092 dev
= get_imsm_dev(super
, info
->container_member
);
6093 map
= get_imsm_map(dev
, MAP_X
);
6094 idx
= get_imsm_disk_idx(dev
, disk
->disk
.raid_disk
, MAP_X
);
6095 d
= get_imsm_dl_disk(super
, idx
);
6097 if (!d
|| d
->index
< 0 || is_failed(&d
->disk
))
6100 if (lseek64(d
->fd
, info
->ppl_sector
* 512, SEEK_SET
) < 0) {
6101 perror("Failed to seek to PPL header location");
6106 if (read(d
->fd
, buf
, PPL_HEADER_SIZE
) != PPL_HEADER_SIZE
) {
6107 perror("Read PPL header failed");
6114 crc
= __le32_to_cpu(ppl_hdr
->checksum
);
6115 ppl_hdr
->checksum
= 0;
6117 if (crc
!= ~crc32c_le(~0, buf
, PPL_HEADER_SIZE
)) {
6118 dprintf("Wrong PPL header checksum on %s\n",
6123 if (!ret
&& (__le32_to_cpu(ppl_hdr
->signature
) !=
6124 super
->anchor
->orig_family_num
)) {
6125 dprintf("Wrong PPL header signature on %s\n",
6133 if (ret
== 1 && map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
6134 return st
->ss
->write_init_ppl(st
, info
, d
->fd
);
6141 static int write_init_ppl_imsm_all(struct supertype
*st
, struct mdinfo
*info
)
6143 struct intel_super
*super
= st
->sb
;
6147 if (info
->consistency_policy
!= CONSISTENCY_POLICY_PPL
||
6148 info
->array
.level
!= 5)
6151 for (d
= super
->disks
; d
; d
= d
->next
) {
6152 if (d
->index
< 0 || is_failed(&d
->disk
))
6155 ret
= st
->ss
->write_init_ppl(st
, info
, d
->fd
);
6163 static int write_init_super_imsm(struct supertype
*st
)
6165 struct intel_super
*super
= st
->sb
;
6166 int current_vol
= super
->current_vol
;
6170 getinfo_super_imsm(st
, &info
, NULL
);
6172 /* we are done with current_vol reset it to point st at the container */
6173 super
->current_vol
= -1;
6175 if (st
->update_tail
) {
6176 /* queue the recently created array / added disk
6177 * as a metadata update */
6179 /* determine if we are creating a volume or adding a disk */
6180 if (current_vol
< 0) {
6181 /* in the mgmt (add/remove) disk case we are running
6182 * in mdmon context, so don't close fd's
6186 rv
= write_init_ppl_imsm_all(st
, &info
);
6188 rv
= create_array(st
, current_vol
);
6192 for (d
= super
->disks
; d
; d
= d
->next
)
6193 Kill(d
->devname
, NULL
, 0, -1, 1);
6194 if (current_vol
>= 0)
6195 rv
= write_init_ppl_imsm_all(st
, &info
);
6197 rv
= write_super_imsm(st
, 1);
6204 static int store_super_imsm(struct supertype
*st
, int fd
)
6206 struct intel_super
*super
= st
->sb
;
6207 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
6213 if (super
->sector_size
== 4096)
6214 convert_to_4k(super
);
6215 return store_imsm_mpb(fd
, mpb
);
6222 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
6223 int layout
, int raiddisks
, int chunk
,
6224 unsigned long long size
,
6225 unsigned long long data_offset
,
6227 unsigned long long *freesize
,
6231 unsigned long long ldsize
;
6232 struct intel_super
*super
;
6235 if (level
!= LEVEL_CONTAINER
)
6240 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
6243 pr_err("imsm: Cannot open %s: %s\n",
6244 dev
, strerror(errno
));
6247 if (!get_dev_size(fd
, dev
, &ldsize
)) {
6252 /* capabilities retrieve could be possible
6253 * note that there is no fd for the disks in array.
6255 super
= alloc_super();
6260 if (!get_dev_sector_size(fd
, NULL
, &super
->sector_size
)) {
6266 rv
= find_intel_hba_capability(fd
, super
, verbose
> 0 ? dev
: NULL
);
6270 fd2devname(fd
, str
);
6271 dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n",
6272 fd
, str
, super
->orom
, rv
, raiddisks
);
6274 /* no orom/efi or non-intel hba of the disk */
6281 if (raiddisks
> super
->orom
->tds
) {
6283 pr_err("%d exceeds maximum number of platform supported disks: %d\n",
6284 raiddisks
, super
->orom
->tds
);
6288 if ((super
->orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) == 0 &&
6289 (ldsize
>> 9) >> 32 > 0) {
6291 pr_err("%s exceeds maximum platform supported size\n", dev
);
6297 *freesize
= avail_size_imsm(st
, ldsize
>> 9, data_offset
);
6303 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
6305 const unsigned long long base_start
= e
[*idx
].start
;
6306 unsigned long long end
= base_start
+ e
[*idx
].size
;
6309 if (base_start
== end
)
6313 for (i
= *idx
; i
< num_extents
; i
++) {
6314 /* extend overlapping extents */
6315 if (e
[i
].start
>= base_start
&&
6316 e
[i
].start
<= end
) {
6319 if (e
[i
].start
+ e
[i
].size
> end
)
6320 end
= e
[i
].start
+ e
[i
].size
;
6321 } else if (e
[i
].start
> end
) {
6327 return end
- base_start
;
6330 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
6332 /* build a composite disk with all known extents and generate a new
6333 * 'maxsize' given the "all disks in an array must share a common start
6334 * offset" constraint
6336 struct extent
*e
= xcalloc(sum_extents
, sizeof(*e
));
6340 unsigned long long pos
;
6341 unsigned long long start
= 0;
6342 unsigned long long maxsize
;
6343 unsigned long reserve
;
6345 /* coalesce and sort all extents. also, check to see if we need to
6346 * reserve space between member arrays
6349 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6352 for (i
= 0; i
< dl
->extent_cnt
; i
++)
6355 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
6360 while (i
< sum_extents
) {
6361 e
[j
].start
= e
[i
].start
;
6362 e
[j
].size
= find_size(e
, &i
, sum_extents
);
6364 if (e
[j
-1].size
== 0)
6373 unsigned long long esize
;
6375 esize
= e
[i
].start
- pos
;
6376 if (esize
>= maxsize
) {
6381 pos
= e
[i
].start
+ e
[i
].size
;
6383 } while (e
[i
-1].size
);
6389 /* FIXME assumes volume at offset 0 is the first volume in a
6392 if (start_extent
> 0)
6393 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
6397 if (maxsize
< reserve
)
6400 super
->create_offset
= ~((unsigned long long) 0);
6401 if (start
+ reserve
> super
->create_offset
)
6402 return 0; /* start overflows create_offset */
6403 super
->create_offset
= start
+ reserve
;
6405 return maxsize
- reserve
;
6408 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
6410 if (level
< 0 || level
== 6 || level
== 4)
6413 /* if we have an orom prevent invalid raid levels */
6416 case 0: return imsm_orom_has_raid0(orom
);
6419 return imsm_orom_has_raid1e(orom
);
6420 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
6421 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
6422 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
6425 return 1; /* not on an Intel RAID platform so anything goes */
6431 active_arrays_by_format(char *name
, char* hba
, struct md_list
**devlist
,
6432 int dpa
, int verbose
)
6434 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
6435 struct mdstat_ent
*memb
;
6441 for (memb
= mdstat
; memb
; memb
= memb
->next
) {
6442 if (memb
->metadata_version
&&
6443 (strncmp(memb
->metadata_version
, "external:", 9) == 0) &&
6444 (strcmp(&memb
->metadata_version
[9], name
) == 0) &&
6445 !is_subarray(memb
->metadata_version
+9) &&
6447 struct dev_member
*dev
= memb
->members
;
6449 while(dev
&& (fd
< 0)) {
6450 char *path
= xmalloc(strlen(dev
->name
) + strlen("/dev/") + 1);
6451 num
= sprintf(path
, "%s%s", "/dev/", dev
->name
);
6453 fd
= open(path
, O_RDONLY
, 0);
6454 if (num
<= 0 || fd
< 0) {
6455 pr_vrb("Cannot open %s: %s\n",
6456 dev
->name
, strerror(errno
));
6462 if (fd
>= 0 && disk_attached_to_hba(fd
, hba
)) {
6463 struct mdstat_ent
*vol
;
6464 for (vol
= mdstat
; vol
; vol
= vol
->next
) {
6465 if (vol
->active
> 0 &&
6466 vol
->metadata_version
&&
6467 is_container_member(vol
, memb
->devnm
)) {
6472 if (*devlist
&& (found
< dpa
)) {
6473 dv
= xcalloc(1, sizeof(*dv
));
6474 dv
->devname
= xmalloc(strlen(memb
->devnm
) + strlen("/dev/") + 1);
6475 sprintf(dv
->devname
, "%s%s", "/dev/", memb
->devnm
);
6478 dv
->next
= *devlist
;
6486 free_mdstat(mdstat
);
6491 static struct md_list
*
6492 get_loop_devices(void)
6495 struct md_list
*devlist
= NULL
;
6498 for(i
= 0; i
< 12; i
++) {
6499 dv
= xcalloc(1, sizeof(*dv
));
6500 dv
->devname
= xmalloc(40);
6501 sprintf(dv
->devname
, "/dev/loop%d", i
);
6509 static struct md_list
*
6510 get_devices(const char *hba_path
)
6512 struct md_list
*devlist
= NULL
;
6519 devlist
= get_loop_devices();
6522 /* scroll through /sys/dev/block looking for devices attached to
6525 dir
= opendir("/sys/dev/block");
6526 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
6531 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
6533 path
= devt_to_devpath(makedev(major
, minor
));
6536 if (!path_attached_to_hba(path
, hba_path
)) {
6543 fd
= dev_open(ent
->d_name
, O_RDONLY
);
6545 fd2devname(fd
, buf
);
6548 pr_err("cannot open device: %s\n",
6553 dv
= xcalloc(1, sizeof(*dv
));
6554 dv
->devname
= xstrdup(buf
);
6561 devlist
= devlist
->next
;
6571 count_volumes_list(struct md_list
*devlist
, char *homehost
,
6572 int verbose
, int *found
)
6574 struct md_list
*tmpdev
;
6576 struct supertype
*st
;
6578 /* first walk the list of devices to find a consistent set
6579 * that match the criterea, if that is possible.
6580 * We flag the ones we like with 'used'.
6583 st
= match_metadata_desc_imsm("imsm");
6585 pr_vrb("cannot allocate memory for imsm supertype\n");
6589 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
6590 char *devname
= tmpdev
->devname
;
6592 struct supertype
*tst
;
6594 if (tmpdev
->used
> 1)
6596 tst
= dup_super(st
);
6598 pr_vrb("cannot allocate memory for imsm supertype\n");
6601 tmpdev
->container
= 0;
6602 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
6604 dprintf("cannot open device %s: %s\n",
6605 devname
, strerror(errno
));
6607 } else if (fstat(dfd
, &stb
)< 0) {
6609 dprintf("fstat failed for %s: %s\n",
6610 devname
, strerror(errno
));
6612 } else if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
6613 dprintf("%s is not a block device.\n",
6616 } else if (must_be_container(dfd
)) {
6617 struct supertype
*cst
;
6618 cst
= super_by_fd(dfd
, NULL
);
6620 dprintf("cannot recognize container type %s\n",
6623 } else if (tst
->ss
!= st
->ss
) {
6624 dprintf("non-imsm container - ignore it: %s\n",
6627 } else if (!tst
->ss
->load_container
||
6628 tst
->ss
->load_container(tst
, dfd
, NULL
))
6631 tmpdev
->container
= 1;
6634 cst
->ss
->free_super(cst
);
6636 tmpdev
->st_rdev
= stb
.st_rdev
;
6637 if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
6638 dprintf("no RAID superblock on %s\n",
6641 } else if (tst
->ss
->compare_super
== NULL
) {
6642 dprintf("Cannot assemble %s metadata on %s\n",
6643 tst
->ss
->name
, devname
);
6649 if (tmpdev
->used
== 2 || tmpdev
->used
== 4) {
6650 /* Ignore unrecognised devices during auto-assembly */
6655 tst
->ss
->getinfo_super(tst
, &info
, NULL
);
6657 if (st
->minor_version
== -1)
6658 st
->minor_version
= tst
->minor_version
;
6660 if (memcmp(info
.uuid
, uuid_zero
,
6661 sizeof(int[4])) == 0) {
6662 /* this is a floating spare. It cannot define
6663 * an array unless there are no more arrays of
6664 * this type to be found. It can be included
6665 * in an array of this type though.
6671 if (st
->ss
!= tst
->ss
||
6672 st
->minor_version
!= tst
->minor_version
||
6673 st
->ss
->compare_super(st
, tst
) != 0) {
6674 /* Some mismatch. If exactly one array matches this host,
6675 * we can resolve on that one.
6676 * Or, if we are auto assembling, we just ignore the second
6679 dprintf("superblock on %s doesn't match others - assembly aborted\n",
6685 dprintf("found: devname: %s\n", devname
);
6689 tst
->ss
->free_super(tst
);
6693 if ((err
= load_super_imsm_all(st
, -1, &st
->sb
, NULL
, devlist
, 0)) == 0) {
6694 struct mdinfo
*iter
, *head
= st
->ss
->container_content(st
, NULL
);
6695 for (iter
= head
; iter
; iter
= iter
->next
) {
6696 dprintf("content->text_version: %s vol\n",
6697 iter
->text_version
);
6698 if (iter
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
6699 /* do not assemble arrays with unsupported
6701 dprintf("Cannot activate member %s.\n",
6702 iter
->text_version
);
6709 dprintf("No valid super block on device list: err: %d %p\n",
6713 dprintf("no more devices to examine\n");
6716 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
6717 if (tmpdev
->used
== 1 && tmpdev
->found
) {
6719 if (count
< tmpdev
->found
)
6722 count
-= tmpdev
->found
;
6725 if (tmpdev
->used
== 1)
6730 st
->ss
->free_super(st
);
6734 static int __count_volumes(char *hba_path
, int dpa
, int verbose
,
6737 struct sys_dev
*idev
, *intel_devices
= find_intel_devices();
6739 const struct orom_entry
*entry
;
6740 struct devid_list
*dv
, *devid_list
;
6745 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
6746 if (strstr(idev
->path
, hba_path
))
6750 if (!idev
|| !idev
->dev_id
)
6753 entry
= get_orom_entry_by_device_id(idev
->dev_id
);
6755 if (!entry
|| !entry
->devid_list
)
6758 devid_list
= entry
->devid_list
;
6759 for (dv
= devid_list
; dv
; dv
= dv
->next
) {
6760 struct md_list
*devlist
;
6761 struct sys_dev
*device
= NULL
;
6766 device
= device_by_id_and_path(dv
->devid
, hba_path
);
6768 device
= device_by_id(dv
->devid
);
6771 hpath
= device
->path
;
6775 devlist
= get_devices(hpath
);
6776 /* if no intel devices return zero volumes */
6777 if (devlist
== NULL
)
6780 count
+= active_arrays_by_format("imsm", hpath
, &devlist
, dpa
,
6782 dprintf("path: %s active arrays: %d\n", hpath
, count
);
6783 if (devlist
== NULL
)
6787 count
+= count_volumes_list(devlist
,
6791 dprintf("found %d count: %d\n", found
, count
);
6794 dprintf("path: %s total number of volumes: %d\n", hpath
, count
);
6797 struct md_list
*dv
= devlist
;
6798 devlist
= devlist
->next
;
6806 static int count_volumes(struct intel_hba
*hba
, int dpa
, int verbose
)
6810 if (hba
->type
== SYS_DEV_VMD
) {
6811 struct sys_dev
*dev
;
6814 for (dev
= find_intel_devices(); dev
; dev
= dev
->next
) {
6815 if (dev
->type
== SYS_DEV_VMD
)
6816 count
+= __count_volumes(dev
->path
, dpa
,
6821 return __count_volumes(hba
->path
, dpa
, verbose
, 0);
6824 static int imsm_default_chunk(const struct imsm_orom
*orom
)
6826 /* up to 512 if the plaform supports it, otherwise the platform max.
6827 * 128 if no platform detected
6829 int fs
= max(7, orom
? fls(orom
->sss
) : 0);
6831 return min(512, (1 << fs
));
6835 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
6836 int raiddisks
, int *chunk
, unsigned long long size
, int verbose
)
6838 /* check/set platform and metadata limits/defaults */
6839 if (super
->orom
&& raiddisks
> super
->orom
->dpa
) {
6840 pr_vrb("platform supports a maximum of %d disks per array\n",
6845 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
6846 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
6847 pr_vrb("platform does not support raid%d with %d disk%s\n",
6848 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
6852 if (*chunk
== 0 || *chunk
== UnSet
)
6853 *chunk
= imsm_default_chunk(super
->orom
);
6855 if (super
->orom
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
6856 pr_vrb("platform does not support a chunk size of: %d\n", *chunk
);
6860 if (layout
!= imsm_level_to_layout(level
)) {
6862 pr_vrb("imsm raid 5 only supports the left-asymmetric layout\n");
6863 else if (level
== 10)
6864 pr_vrb("imsm raid 10 only supports the n2 layout\n");
6866 pr_vrb("imsm unknown layout %#x for this raid level %d\n",
6871 if (super
->orom
&& (super
->orom
->attr
& IMSM_OROM_ATTR_2TB
) == 0 &&
6872 (calc_array_size(level
, raiddisks
, layout
, *chunk
, size
) >> 32) > 0) {
6873 pr_vrb("platform does not support a volume size over 2TB\n");
6880 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
6881 * FIX ME add ahci details
6883 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
6884 int layout
, int raiddisks
, int *chunk
,
6885 unsigned long long size
,
6886 unsigned long long data_offset
,
6888 unsigned long long *freesize
,
6892 struct intel_super
*super
= st
->sb
;
6893 struct imsm_super
*mpb
;
6895 unsigned long long pos
= 0;
6896 unsigned long long maxsize
;
6900 /* We must have the container info already read in. */
6904 mpb
= super
->anchor
;
6906 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, size
, verbose
)) {
6907 pr_err("RAID gemetry validation failed. Cannot proceed with the action(s).\n");
6911 /* General test: make sure there is space for
6912 * 'raiddisks' device extents of size 'size' at a given
6915 unsigned long long minsize
= size
;
6916 unsigned long long start_offset
= MaxSector
;
6919 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
6920 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6925 e
= get_extents(super
, dl
);
6928 unsigned long long esize
;
6929 esize
= e
[i
].start
- pos
;
6930 if (esize
>= minsize
)
6932 if (found
&& start_offset
== MaxSector
) {
6935 } else if (found
&& pos
!= start_offset
) {
6939 pos
= e
[i
].start
+ e
[i
].size
;
6941 } while (e
[i
-1].size
);
6946 if (dcnt
< raiddisks
) {
6948 pr_err("imsm: Not enough devices with space for this array (%d < %d)\n",
6955 /* This device must be a member of the set */
6956 if (stat(dev
, &stb
) < 0)
6958 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
6960 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6961 if (dl
->major
== (int)major(stb
.st_rdev
) &&
6962 dl
->minor
== (int)minor(stb
.st_rdev
))
6967 pr_err("%s is not in the same imsm set\n", dev
);
6969 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
6970 /* If a volume is present then the current creation attempt
6971 * cannot incorporate new spares because the orom may not
6972 * understand this configuration (all member disks must be
6973 * members of each array in the container).
6975 pr_err("%s is a spare and a volume is already defined for this container\n", dev
);
6976 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
6978 } else if (super
->orom
&& mpb
->num_raid_devs
> 0 &&
6979 mpb
->num_disks
!= raiddisks
) {
6980 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
6984 /* retrieve the largest free space block */
6985 e
= get_extents(super
, dl
);
6990 unsigned long long esize
;
6992 esize
= e
[i
].start
- pos
;
6993 if (esize
>= maxsize
)
6995 pos
= e
[i
].start
+ e
[i
].size
;
6997 } while (e
[i
-1].size
);
7002 pr_err("unable to determine free space for: %s\n",
7006 if (maxsize
< size
) {
7008 pr_err("%s not enough space (%llu < %llu)\n",
7009 dev
, maxsize
, size
);
7013 /* count total number of extents for merge */
7015 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7017 i
+= dl
->extent_cnt
;
7019 maxsize
= merge_extents(super
, i
);
7021 if (!check_env("IMSM_NO_PLATFORM") &&
7022 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
7023 pr_err("attempting to create a second volume with size less then remaining space. Aborting...\n");
7027 if (maxsize
< size
|| maxsize
== 0) {
7030 pr_err("no free space left on device. Aborting...\n");
7032 pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n",
7038 *freesize
= maxsize
;
7041 int count
= count_volumes(super
->hba
,
7042 super
->orom
->dpa
, verbose
);
7043 if (super
->orom
->vphba
<= count
) {
7044 pr_vrb("platform does not support more than %d raid volumes.\n",
7045 super
->orom
->vphba
);
7052 static int imsm_get_free_size(struct supertype
*st
, int raiddisks
,
7053 unsigned long long size
, int chunk
,
7054 unsigned long long *freesize
)
7056 struct intel_super
*super
= st
->sb
;
7057 struct imsm_super
*mpb
= super
->anchor
;
7062 unsigned long long maxsize
;
7063 unsigned long long minsize
;
7067 /* find the largest common start free region of the possible disks */
7071 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7077 /* don't activate new spares if we are orom constrained
7078 * and there is already a volume active in the container
7080 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
7083 e
= get_extents(super
, dl
);
7086 for (i
= 1; e
[i
-1].size
; i
++)
7094 maxsize
= merge_extents(super
, extent_cnt
);
7098 minsize
= chunk
* 2;
7100 if (cnt
< raiddisks
||
7101 (super
->orom
&& used
&& used
!= raiddisks
) ||
7102 maxsize
< minsize
||
7104 pr_err("not enough devices with space to create array.\n");
7105 return 0; /* No enough free spaces large enough */
7116 if (!check_env("IMSM_NO_PLATFORM") &&
7117 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
7118 pr_err("attempting to create a second volume with size less then remaining space. Aborting...\n");
7122 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7124 dl
->raiddisk
= cnt
++;
7128 dprintf("imsm: imsm_get_free_size() returns : %llu\n", size
);
7133 static int reserve_space(struct supertype
*st
, int raiddisks
,
7134 unsigned long long size
, int chunk
,
7135 unsigned long long *freesize
)
7137 struct intel_super
*super
= st
->sb
;
7142 rv
= imsm_get_free_size(st
, raiddisks
, size
, chunk
, freesize
);
7145 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7147 dl
->raiddisk
= cnt
++;
7154 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
7155 int raiddisks
, int *chunk
, unsigned long long size
,
7156 unsigned long long data_offset
,
7157 char *dev
, unsigned long long *freesize
,
7158 int consistency_policy
, int verbose
)
7165 * if given unused devices create a container
7166 * if given given devices in a container create a member volume
7168 if (level
== LEVEL_CONTAINER
) {
7169 /* Must be a fresh device to add to a container */
7170 return validate_geometry_imsm_container(st
, level
, layout
,
7180 struct intel_super
*super
= st
->sb
;
7181 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
7182 raiddisks
, chunk
, size
,
7185 /* we are being asked to automatically layout a
7186 * new volume based on the current contents of
7187 * the container. If the the parameters can be
7188 * satisfied reserve_space will record the disks,
7189 * start offset, and size of the volume to be
7190 * created. add_to_super and getinfo_super
7191 * detect when autolayout is in progress.
7193 /* assuming that freesize is always given when array is
7195 if (super
->orom
&& freesize
) {
7197 count
= count_volumes(super
->hba
,
7198 super
->orom
->dpa
, verbose
);
7199 if (super
->orom
->vphba
<= count
) {
7200 pr_vrb("platform does not support more than %d raid volumes.\n",
7201 super
->orom
->vphba
);
7206 return reserve_space(st
, raiddisks
, size
,
7212 /* creating in a given container */
7213 return validate_geometry_imsm_volume(st
, level
, layout
,
7214 raiddisks
, chunk
, size
,
7216 dev
, freesize
, verbose
);
7219 /* This device needs to be a device in an 'imsm' container */
7220 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
7223 pr_err("Cannot create this array on device %s\n",
7228 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
7230 pr_err("Cannot open %s: %s\n",
7231 dev
, strerror(errno
));
7234 /* Well, it is in use by someone, maybe an 'imsm' container. */
7235 cfd
= open_container(fd
);
7239 pr_err("Cannot use %s: It is busy\n",
7243 sra
= sysfs_read(cfd
, NULL
, GET_VERSION
);
7244 if (sra
&& sra
->array
.major_version
== -1 &&
7245 strcmp(sra
->text_version
, "imsm") == 0)
7249 /* This is a member of a imsm container. Load the container
7250 * and try to create a volume
7252 struct intel_super
*super
;
7254 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
, NULL
, 1) == 0) {
7256 strcpy(st
->container_devnm
, fd2devnm(cfd
));
7258 return validate_geometry_imsm_volume(st
, level
, layout
,
7260 size
, data_offset
, dev
,
7267 pr_err("failed container membership check\n");
7273 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
7275 struct intel_super
*super
= st
->sb
;
7277 if (level
&& *level
== UnSet
)
7278 *level
= LEVEL_CONTAINER
;
7280 if (level
&& layout
&& *layout
== UnSet
)
7281 *layout
= imsm_level_to_layout(*level
);
7283 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0))
7284 *chunk
= imsm_default_chunk(super
->orom
);
7287 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
7289 static int kill_subarray_imsm(struct supertype
*st
)
7291 /* remove the subarray currently referenced by ->current_vol */
7293 struct intel_dev
**dp
;
7294 struct intel_super
*super
= st
->sb
;
7295 __u8 current_vol
= super
->current_vol
;
7296 struct imsm_super
*mpb
= super
->anchor
;
7298 if (super
->current_vol
< 0)
7300 super
->current_vol
= -1; /* invalidate subarray cursor */
7302 /* block deletions that would change the uuid of active subarrays
7304 * FIXME when immutable ids are available, but note that we'll
7305 * also need to fixup the invalidated/active subarray indexes in
7308 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7311 if (i
< current_vol
)
7313 sprintf(subarray
, "%u", i
);
7314 if (is_subarray_active(subarray
, st
->devnm
)) {
7315 pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
7322 if (st
->update_tail
) {
7323 struct imsm_update_kill_array
*u
= xmalloc(sizeof(*u
));
7325 u
->type
= update_kill_array
;
7326 u
->dev_idx
= current_vol
;
7327 append_metadata_update(st
, u
, sizeof(*u
));
7332 for (dp
= &super
->devlist
; *dp
;)
7333 if ((*dp
)->index
== current_vol
) {
7336 handle_missing(super
, (*dp
)->dev
);
7337 if ((*dp
)->index
> current_vol
)
7342 /* no more raid devices, all active components are now spares,
7343 * but of course failed are still failed
7345 if (--mpb
->num_raid_devs
== 0) {
7348 for (d
= super
->disks
; d
; d
= d
->next
)
7353 super
->updates_pending
++;
7358 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
7359 char *update
, struct mddev_ident
*ident
)
7361 /* update the subarray currently referenced by ->current_vol */
7362 struct intel_super
*super
= st
->sb
;
7363 struct imsm_super
*mpb
= super
->anchor
;
7365 if (strcmp(update
, "name") == 0) {
7366 char *name
= ident
->name
;
7370 if (is_subarray_active(subarray
, st
->devnm
)) {
7371 pr_err("Unable to update name of active subarray\n");
7375 if (!check_name(super
, name
, 0))
7378 vol
= strtoul(subarray
, &ep
, 10);
7379 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
7382 if (st
->update_tail
) {
7383 struct imsm_update_rename_array
*u
= xmalloc(sizeof(*u
));
7385 u
->type
= update_rename_array
;
7387 strncpy((char *) u
->name
, name
, MAX_RAID_SERIAL_LEN
);
7388 u
->name
[MAX_RAID_SERIAL_LEN
-1] = '\0';
7389 append_metadata_update(st
, u
, sizeof(*u
));
7391 struct imsm_dev
*dev
;
7394 dev
= get_imsm_dev(super
, vol
);
7395 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
7396 dev
->volume
[MAX_RAID_SERIAL_LEN
-1] = '\0';
7397 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7398 dev
= get_imsm_dev(super
, i
);
7399 handle_missing(super
, dev
);
7401 super
->updates_pending
++;
7403 } else if (strcmp(update
, "ppl") == 0 ||
7404 strcmp(update
, "no-ppl") == 0) {
7407 int vol
= strtoul(subarray
, &ep
, 10);
7409 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
7412 if (strcmp(update
, "ppl") == 0)
7413 new_policy
= RWH_DISTRIBUTED
;
7415 new_policy
= RWH_OFF
;
7417 if (st
->update_tail
) {
7418 struct imsm_update_rwh_policy
*u
= xmalloc(sizeof(*u
));
7420 u
->type
= update_rwh_policy
;
7422 u
->new_policy
= new_policy
;
7423 append_metadata_update(st
, u
, sizeof(*u
));
7425 struct imsm_dev
*dev
;
7427 dev
= get_imsm_dev(super
, vol
);
7428 dev
->rwh_policy
= new_policy
;
7429 super
->updates_pending
++;
7436 #endif /* MDASSEMBLE */
7438 static int is_gen_migration(struct imsm_dev
*dev
)
7443 if (!dev
->vol
.migr_state
)
7446 if (migr_type(dev
) == MIGR_GEN_MIGR
)
7452 static int is_rebuilding(struct imsm_dev
*dev
)
7454 struct imsm_map
*migr_map
;
7456 if (!dev
->vol
.migr_state
)
7459 if (migr_type(dev
) != MIGR_REBUILD
)
7462 migr_map
= get_imsm_map(dev
, MAP_1
);
7464 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
7471 static int is_initializing(struct imsm_dev
*dev
)
7473 struct imsm_map
*migr_map
;
7475 if (!dev
->vol
.migr_state
)
7478 if (migr_type(dev
) != MIGR_INIT
)
7481 migr_map
= get_imsm_map(dev
, MAP_1
);
7483 if (migr_map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
7490 static void update_recovery_start(struct intel_super
*super
,
7491 struct imsm_dev
*dev
,
7492 struct mdinfo
*array
)
7494 struct mdinfo
*rebuild
= NULL
;
7498 if (!is_rebuilding(dev
))
7501 /* Find the rebuild target, but punt on the dual rebuild case */
7502 for (d
= array
->devs
; d
; d
= d
->next
)
7503 if (d
->recovery_start
== 0) {
7510 /* (?) none of the disks are marked with
7511 * IMSM_ORD_REBUILD, so assume they are missing and the
7512 * disk_ord_tbl was not correctly updated
7514 dprintf("failed to locate out-of-sync disk\n");
7518 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
7519 rebuild
->recovery_start
= units
* blocks_per_migr_unit(super
, dev
);
7523 static int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
);
7526 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
7528 /* Given a container loaded by load_super_imsm_all,
7529 * extract information about all the arrays into
7531 * If 'subarray' is given, just extract info about that array.
7533 * For each imsm_dev create an mdinfo, fill it in,
7534 * then look for matching devices in super->disks
7535 * and create appropriate device mdinfo.
7537 struct intel_super
*super
= st
->sb
;
7538 struct imsm_super
*mpb
= super
->anchor
;
7539 struct mdinfo
*rest
= NULL
;
7543 int spare_disks
= 0;
7545 /* do not assemble arrays when not all attributes are supported */
7546 if (imsm_check_attributes(mpb
->attributes
) == 0) {
7548 pr_err("Unsupported attributes in IMSM metadata.Arrays activation is blocked.\n");
7551 /* count spare devices, not used in maps
7553 for (d
= super
->disks
; d
; d
= d
->next
)
7557 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7558 struct imsm_dev
*dev
;
7559 struct imsm_map
*map
;
7560 struct imsm_map
*map2
;
7561 struct mdinfo
*this;
7569 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
7572 dev
= get_imsm_dev(super
, i
);
7573 map
= get_imsm_map(dev
, MAP_0
);
7574 map2
= get_imsm_map(dev
, MAP_1
);
7576 /* do not publish arrays that are in the middle of an
7577 * unsupported migration
7579 if (dev
->vol
.migr_state
&&
7580 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
7581 pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n",
7585 /* do not publish arrays that are not support by controller's
7589 this = xmalloc(sizeof(*this));
7591 super
->current_vol
= i
;
7592 getinfo_super_imsm_volume(st
, this, NULL
);
7595 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
7596 /* mdadm does not support all metadata features- set the bit in all arrays state */
7597 if (!validate_geometry_imsm_orom(super
,
7598 get_imsm_raid_level(map
), /* RAID level */
7599 imsm_level_to_layout(get_imsm_raid_level(map
)),
7600 map
->num_members
, /* raid disks */
7601 &chunk
, join_u32(dev
->size_low
, dev
->size_high
),
7603 pr_err("IMSM RAID geometry validation failed. Array %s activation is blocked.\n",
7605 this->array
.state
|=
7606 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
7607 (1<<MD_SB_BLOCK_VOLUME
);
7611 /* if array has bad blocks, set suitable bit in all arrays state */
7613 this->array
.state
|=
7614 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
7615 (1<<MD_SB_BLOCK_VOLUME
);
7617 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
7618 unsigned long long recovery_start
;
7619 struct mdinfo
*info_d
;
7626 idx
= get_imsm_disk_idx(dev
, slot
, MAP_0
);
7627 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
7628 for (d
= super
->disks
; d
; d
= d
->next
)
7629 if (d
->index
== idx
)
7632 recovery_start
= MaxSector
;
7635 if (d
&& is_failed(&d
->disk
))
7637 if (ord
& IMSM_ORD_REBUILD
)
7641 * if we skip some disks the array will be assmebled degraded;
7642 * reset resync start to avoid a dirty-degraded
7643 * situation when performing the intial sync
7645 * FIXME handle dirty degraded
7647 if ((skip
|| recovery_start
== 0) &&
7648 !(dev
->vol
.dirty
& RAIDVOL_DIRTY
))
7649 this->resync_start
= MaxSector
;
7653 info_d
= xcalloc(1, sizeof(*info_d
));
7654 info_d
->next
= this->devs
;
7655 this->devs
= info_d
;
7657 info_d
->disk
.number
= d
->index
;
7658 info_d
->disk
.major
= d
->major
;
7659 info_d
->disk
.minor
= d
->minor
;
7660 info_d
->disk
.raid_disk
= slot
;
7661 info_d
->recovery_start
= recovery_start
;
7663 if (slot
< map2
->num_members
)
7664 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
7666 this->array
.spare_disks
++;
7668 if (slot
< map
->num_members
)
7669 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
7671 this->array
.spare_disks
++;
7673 if (info_d
->recovery_start
== MaxSector
)
7674 this->array
.working_disks
++;
7676 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
7677 info_d
->data_offset
= pba_of_lba0(map
);
7679 if (map
->raid_level
== 5) {
7680 info_d
->component_size
=
7681 num_data_stripes(map
) *
7682 map
->blocks_per_strip
;
7683 info_d
->ppl_sector
= this->ppl_sector
;
7684 info_d
->ppl_size
= this->ppl_size
;
7686 info_d
->component_size
= blocks_per_member(map
);
7688 info_d
->consistency_policy
= this->consistency_policy
;
7690 info_d
->bb
.supported
= 1;
7691 get_volume_badblocks(super
->bbm_log
, ord_to_idx(ord
),
7692 info_d
->data_offset
,
7693 info_d
->component_size
,
7696 /* now that the disk list is up-to-date fixup recovery_start */
7697 update_recovery_start(super
, dev
, this);
7698 this->array
.spare_disks
+= spare_disks
;
7701 /* check for reshape */
7702 if (this->reshape_active
== 1)
7703 recover_backup_imsm(st
, this);
7711 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
7712 int failed
, int look_in_map
)
7714 struct imsm_map
*map
;
7716 map
= get_imsm_map(dev
, look_in_map
);
7719 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
7720 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
7722 switch (get_imsm_raid_level(map
)) {
7724 return IMSM_T_STATE_FAILED
;
7727 if (failed
< map
->num_members
)
7728 return IMSM_T_STATE_DEGRADED
;
7730 return IMSM_T_STATE_FAILED
;
7735 * check to see if any mirrors have failed, otherwise we
7736 * are degraded. Even numbered slots are mirrored on
7740 /* gcc -Os complains that this is unused */
7741 int insync
= insync
;
7743 for (i
= 0; i
< map
->num_members
; i
++) {
7744 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_X
);
7745 int idx
= ord_to_idx(ord
);
7746 struct imsm_disk
*disk
;
7748 /* reset the potential in-sync count on even-numbered
7749 * slots. num_copies is always 2 for imsm raid10
7754 disk
= get_imsm_disk(super
, idx
);
7755 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
7758 /* no in-sync disks left in this mirror the
7762 return IMSM_T_STATE_FAILED
;
7765 return IMSM_T_STATE_DEGRADED
;
7769 return IMSM_T_STATE_DEGRADED
;
7771 return IMSM_T_STATE_FAILED
;
7777 return map
->map_state
;
7780 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
7785 struct imsm_disk
*disk
;
7786 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7787 struct imsm_map
*prev
= get_imsm_map(dev
, MAP_1
);
7788 struct imsm_map
*map_for_loop
;
7793 /* at the beginning of migration we set IMSM_ORD_REBUILD on
7794 * disks that are being rebuilt. New failures are recorded to
7795 * map[0]. So we look through all the disks we started with and
7796 * see if any failures are still present, or if any new ones
7800 if (prev
&& (map
->num_members
< prev
->num_members
))
7801 map_for_loop
= prev
;
7803 for (i
= 0; i
< map_for_loop
->num_members
; i
++) {
7805 /* when MAP_X is passed both maps failures are counted
7808 (look_in_map
== MAP_1
|| look_in_map
== MAP_X
) &&
7809 i
< prev
->num_members
) {
7810 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
7811 idx_1
= ord_to_idx(ord
);
7813 disk
= get_imsm_disk(super
, idx_1
);
7814 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
7817 if ((look_in_map
== MAP_0
|| look_in_map
== MAP_X
) &&
7818 i
< map
->num_members
) {
7819 ord
= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
7820 idx
= ord_to_idx(ord
);
7823 disk
= get_imsm_disk(super
, idx
);
7824 if (!disk
|| is_failed(disk
) ||
7825 ord
& IMSM_ORD_REBUILD
)
7835 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
7838 struct intel_super
*super
= c
->sb
;
7839 struct imsm_super
*mpb
= super
->anchor
;
7840 struct imsm_update_prealloc_bb_mem u
;
7842 if (atoi(inst
) >= mpb
->num_raid_devs
) {
7843 pr_err("subarry index %d, out of range\n", atoi(inst
));
7847 dprintf("imsm: open_new %s\n", inst
);
7848 a
->info
.container_member
= atoi(inst
);
7850 u
.type
= update_prealloc_badblocks_mem
;
7851 imsm_update_metadata_locally(c
, &u
, sizeof(u
));
7856 static int is_resyncing(struct imsm_dev
*dev
)
7858 struct imsm_map
*migr_map
;
7860 if (!dev
->vol
.migr_state
)
7863 if (migr_type(dev
) == MIGR_INIT
||
7864 migr_type(dev
) == MIGR_REPAIR
)
7867 if (migr_type(dev
) == MIGR_GEN_MIGR
)
7870 migr_map
= get_imsm_map(dev
, MAP_1
);
7872 if (migr_map
->map_state
== IMSM_T_STATE_NORMAL
&&
7873 dev
->vol
.migr_type
!= MIGR_GEN_MIGR
)
7879 /* return true if we recorded new information */
7880 static int mark_failure(struct intel_super
*super
,
7881 struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
7885 struct imsm_map
*map
;
7886 char buf
[MAX_RAID_SERIAL_LEN
+3];
7887 unsigned int len
, shift
= 0;
7889 /* new failures are always set in map[0] */
7890 map
= get_imsm_map(dev
, MAP_0
);
7892 slot
= get_imsm_disk_slot(map
, idx
);
7896 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
7897 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
7900 memcpy(buf
, disk
->serial
, MAX_RAID_SERIAL_LEN
);
7901 buf
[MAX_RAID_SERIAL_LEN
] = '\000';
7903 if ((len
= strlen(buf
)) >= MAX_RAID_SERIAL_LEN
)
7904 shift
= len
- MAX_RAID_SERIAL_LEN
+ 1;
7905 strncpy((char *)disk
->serial
, &buf
[shift
], MAX_RAID_SERIAL_LEN
);
7907 disk
->status
|= FAILED_DISK
;
7908 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
7909 /* mark failures in second map if second map exists and this disk
7911 * This is valid for migration, initialization and rebuild
7913 if (dev
->vol
.migr_state
) {
7914 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
7915 int slot2
= get_imsm_disk_slot(map2
, idx
);
7917 if (slot2
< map2
->num_members
&& slot2
>= 0)
7918 set_imsm_ord_tbl_ent(map2
, slot2
,
7919 idx
| IMSM_ORD_REBUILD
);
7921 if (map
->failed_disk_num
== 0xff)
7922 map
->failed_disk_num
= slot
;
7924 clear_disk_badblocks(super
->bbm_log
, ord_to_idx(ord
));
7929 static void mark_missing(struct intel_super
*super
,
7930 struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
7932 mark_failure(super
, dev
, disk
, idx
);
7934 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
7937 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
7938 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
7941 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
7945 if (!super
->missing
)
7948 /* When orom adds replacement for missing disk it does
7949 * not remove entry of missing disk, but just updates map with
7950 * new added disk. So it is not enough just to test if there is
7951 * any missing disk, we have to look if there are any failed disks
7952 * in map to stop migration */
7954 dprintf("imsm: mark missing\n");
7955 /* end process for initialization and rebuild only
7957 if (is_gen_migration(dev
) == 0) {
7961 failed
= imsm_count_failed(super
, dev
, MAP_0
);
7962 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7965 end_migration(dev
, super
, map_state
);
7967 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
7968 mark_missing(super
, dev
, &dl
->disk
, dl
->index
);
7969 super
->updates_pending
++;
7972 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
,
7975 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
7976 unsigned long long array_blocks
;
7977 struct imsm_map
*map
;
7979 if (used_disks
== 0) {
7980 /* when problems occures
7981 * return current array_blocks value
7983 array_blocks
= __le32_to_cpu(dev
->size_high
);
7984 array_blocks
= array_blocks
<< 32;
7985 array_blocks
+= __le32_to_cpu(dev
->size_low
);
7987 return array_blocks
;
7990 /* set array size in metadata
7992 if (new_size
<= 0) {
7993 /* OLCE size change is caused by added disks
7995 map
= get_imsm_map(dev
, MAP_0
);
7996 array_blocks
= blocks_per_member(map
) * used_disks
;
7998 /* Online Volume Size Change
7999 * Using available free space
8001 array_blocks
= new_size
;
8004 array_blocks
= round_size_to_mb(array_blocks
, used_disks
);
8005 dev
->size_low
= __cpu_to_le32((__u32
)array_blocks
);
8006 dev
->size_high
= __cpu_to_le32((__u32
)(array_blocks
>> 32));
8008 return array_blocks
;
8011 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
8013 static void imsm_progress_container_reshape(struct intel_super
*super
)
8015 /* if no device has a migr_state, but some device has a
8016 * different number of members than the previous device, start
8017 * changing the number of devices in this device to match
8020 struct imsm_super
*mpb
= super
->anchor
;
8021 int prev_disks
= -1;
8025 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8026 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
8027 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8028 struct imsm_map
*map2
;
8029 int prev_num_members
;
8031 if (dev
->vol
.migr_state
)
8034 if (prev_disks
== -1)
8035 prev_disks
= map
->num_members
;
8036 if (prev_disks
== map
->num_members
)
8039 /* OK, this array needs to enter reshape mode.
8040 * i.e it needs a migr_state
8043 copy_map_size
= sizeof_imsm_map(map
);
8044 prev_num_members
= map
->num_members
;
8045 map
->num_members
= prev_disks
;
8046 dev
->vol
.migr_state
= 1;
8047 dev
->vol
.curr_migr_unit
= 0;
8048 set_migr_type(dev
, MIGR_GEN_MIGR
);
8049 for (i
= prev_num_members
;
8050 i
< map
->num_members
; i
++)
8051 set_imsm_ord_tbl_ent(map
, i
, i
);
8052 map2
= get_imsm_map(dev
, MAP_1
);
8053 /* Copy the current map */
8054 memcpy(map2
, map
, copy_map_size
);
8055 map2
->num_members
= prev_num_members
;
8057 imsm_set_array_size(dev
, -1);
8058 super
->clean_migration_record_by_mdmon
= 1;
8059 super
->updates_pending
++;
8063 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
8064 * states are handled in imsm_set_disk() with one exception, when a
8065 * resync is stopped due to a new failure this routine will set the
8066 * 'degraded' state for the array.
8068 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
8070 int inst
= a
->info
.container_member
;
8071 struct intel_super
*super
= a
->container
->sb
;
8072 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8073 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8074 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
8075 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8076 __u32 blocks_per_unit
;
8078 if (dev
->vol
.migr_state
&&
8079 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
8080 /* array state change is blocked due to reshape action
8082 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
8083 * - finish the reshape (if last_checkpoint is big and action != reshape)
8084 * - update curr_migr_unit
8086 if (a
->curr_action
== reshape
) {
8087 /* still reshaping, maybe update curr_migr_unit */
8088 goto mark_checkpoint
;
8090 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
8091 /* for some reason we aborted the reshape.
8093 * disable automatic metadata rollback
8094 * user action is required to recover process
8097 struct imsm_map
*map2
=
8098 get_imsm_map(dev
, MAP_1
);
8099 dev
->vol
.migr_state
= 0;
8100 set_migr_type(dev
, 0);
8101 dev
->vol
.curr_migr_unit
= 0;
8103 sizeof_imsm_map(map2
));
8104 super
->updates_pending
++;
8107 if (a
->last_checkpoint
>= a
->info
.component_size
) {
8108 unsigned long long array_blocks
;
8112 used_disks
= imsm_num_data_members(dev
, MAP_0
);
8113 if (used_disks
> 0) {
8115 blocks_per_member(map
) *
8118 round_size_to_mb(array_blocks
,
8120 a
->info
.custom_array_size
= array_blocks
;
8121 /* encourage manager to update array
8125 a
->check_reshape
= 1;
8127 /* finalize online capacity expansion/reshape */
8128 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
8130 mdi
->disk
.raid_disk
,
8133 imsm_progress_container_reshape(super
);
8138 /* before we activate this array handle any missing disks */
8139 if (consistent
== 2)
8140 handle_missing(super
, dev
);
8142 if (consistent
== 2 &&
8143 (!is_resync_complete(&a
->info
) ||
8144 map_state
!= IMSM_T_STATE_NORMAL
||
8145 dev
->vol
.migr_state
))
8148 if (is_resync_complete(&a
->info
)) {
8149 /* complete intialization / resync,
8150 * recovery and interrupted recovery is completed in
8153 if (is_resyncing(dev
)) {
8154 dprintf("imsm: mark resync done\n");
8155 end_migration(dev
, super
, map_state
);
8156 super
->updates_pending
++;
8157 a
->last_checkpoint
= 0;
8159 } else if ((!is_resyncing(dev
) && !failed
) &&
8160 (imsm_reshape_blocks_arrays_changes(super
) == 0)) {
8161 /* mark the start of the init process if nothing is failed */
8162 dprintf("imsm: mark resync start\n");
8163 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
8164 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
8166 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
8167 super
->updates_pending
++;
8171 /* skip checkpointing for general migration,
8172 * it is controlled in mdadm
8174 if (is_gen_migration(dev
))
8175 goto skip_mark_checkpoint
;
8177 /* check if we can update curr_migr_unit from resync_start, recovery_start */
8178 blocks_per_unit
= blocks_per_migr_unit(super
, dev
);
8179 if (blocks_per_unit
) {
8183 units
= a
->last_checkpoint
/ blocks_per_unit
;
8186 /* check that we did not overflow 32-bits, and that
8187 * curr_migr_unit needs updating
8189 if (units32
== units
&&
8191 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
8192 dprintf("imsm: mark checkpoint (%u)\n", units32
);
8193 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
8194 super
->updates_pending
++;
8198 skip_mark_checkpoint
:
8199 /* mark dirty / clean */
8200 if (((dev
->vol
.dirty
& RAIDVOL_DIRTY
) && consistent
) ||
8201 (!(dev
->vol
.dirty
& RAIDVOL_DIRTY
) && !consistent
)) {
8202 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
8204 dev
->vol
.dirty
= RAIDVOL_CLEAN
;
8206 dev
->vol
.dirty
= RAIDVOL_DIRTY
;
8207 if (dev
->rwh_policy
== RWH_DISTRIBUTED
)
8208 dev
->vol
.dirty
|= RAIDVOL_DSRECORD_VALID
;
8210 super
->updates_pending
++;
8216 static int imsm_disk_slot_to_ord(struct active_array
*a
, int slot
)
8218 int inst
= a
->info
.container_member
;
8219 struct intel_super
*super
= a
->container
->sb
;
8220 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8221 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8223 if (slot
> map
->num_members
) {
8224 pr_err("imsm: imsm_disk_slot_to_ord %d out of range 0..%d\n",
8225 slot
, map
->num_members
- 1);
8232 return get_imsm_ord_tbl_ent(dev
, slot
, MAP_0
);
8235 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
8237 int inst
= a
->info
.container_member
;
8238 struct intel_super
*super
= a
->container
->sb
;
8239 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8240 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8241 struct imsm_disk
*disk
;
8243 int recovery_not_finished
= 0;
8248 ord
= imsm_disk_slot_to_ord(a
, n
);
8252 dprintf("imsm: set_disk %d:%x\n", n
, state
);
8253 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
8255 /* check for new failures */
8256 if (state
& DS_FAULTY
) {
8257 if (mark_failure(super
, dev
, disk
, ord_to_idx(ord
)))
8258 super
->updates_pending
++;
8261 /* check if in_sync */
8262 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
8263 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
8265 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
8266 super
->updates_pending
++;
8269 failed
= imsm_count_failed(super
, dev
, MAP_0
);
8270 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8272 /* check if recovery complete, newly degraded, or failed */
8273 dprintf("imsm: Detected transition to state ");
8274 switch (map_state
) {
8275 case IMSM_T_STATE_NORMAL
: /* transition to normal state */
8276 dprintf("normal: ");
8277 if (is_rebuilding(dev
)) {
8278 dprintf_cont("while rebuilding");
8279 /* check if recovery is really finished */
8280 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
8281 if (mdi
->recovery_start
!= MaxSector
) {
8282 recovery_not_finished
= 1;
8285 if (recovery_not_finished
) {
8287 dprintf("Rebuild has not finished yet, state not changed");
8288 if (a
->last_checkpoint
< mdi
->recovery_start
) {
8289 a
->last_checkpoint
= mdi
->recovery_start
;
8290 super
->updates_pending
++;
8294 end_migration(dev
, super
, map_state
);
8295 map
= get_imsm_map(dev
, MAP_0
);
8296 map
->failed_disk_num
= ~0;
8297 super
->updates_pending
++;
8298 a
->last_checkpoint
= 0;
8301 if (is_gen_migration(dev
)) {
8302 dprintf_cont("while general migration");
8303 if (a
->last_checkpoint
>= a
->info
.component_size
)
8304 end_migration(dev
, super
, map_state
);
8306 map
->map_state
= map_state
;
8307 map
= get_imsm_map(dev
, MAP_0
);
8308 map
->failed_disk_num
= ~0;
8309 super
->updates_pending
++;
8313 case IMSM_T_STATE_DEGRADED
: /* transition to degraded state */
8314 dprintf_cont("degraded: ");
8315 if (map
->map_state
!= map_state
&& !dev
->vol
.migr_state
) {
8316 dprintf_cont("mark degraded");
8317 map
->map_state
= map_state
;
8318 super
->updates_pending
++;
8319 a
->last_checkpoint
= 0;
8322 if (is_rebuilding(dev
)) {
8323 dprintf_cont("while rebuilding.");
8324 if (map
->map_state
!= map_state
) {
8325 dprintf_cont(" Map state change");
8326 end_migration(dev
, super
, map_state
);
8327 super
->updates_pending
++;
8331 if (is_gen_migration(dev
)) {
8332 dprintf_cont("while general migration");
8333 if (a
->last_checkpoint
>= a
->info
.component_size
)
8334 end_migration(dev
, super
, map_state
);
8336 map
->map_state
= map_state
;
8337 manage_second_map(super
, dev
);
8339 super
->updates_pending
++;
8342 if (is_initializing(dev
)) {
8343 dprintf_cont("while initialization.");
8344 map
->map_state
= map_state
;
8345 super
->updates_pending
++;
8349 case IMSM_T_STATE_FAILED
: /* transition to failed state */
8350 dprintf_cont("failed: ");
8351 if (is_gen_migration(dev
)) {
8352 dprintf_cont("while general migration");
8353 map
->map_state
= map_state
;
8354 super
->updates_pending
++;
8357 if (map
->map_state
!= map_state
) {
8358 dprintf_cont("mark failed");
8359 end_migration(dev
, super
, map_state
);
8360 super
->updates_pending
++;
8361 a
->last_checkpoint
= 0;
8366 dprintf_cont("state %i\n", map_state
);
8371 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
8374 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
8375 unsigned long long dsize
;
8376 unsigned long long sectors
;
8377 unsigned int sector_size
;
8379 get_dev_sector_size(fd
, NULL
, §or_size
);
8380 get_dev_size(fd
, NULL
, &dsize
);
8382 if (mpb_size
> sector_size
) {
8383 /* -1 to account for anchor */
8384 sectors
= mpb_sectors(mpb
, sector_size
) - 1;
8386 /* write the extended mpb to the sectors preceeding the anchor */
8387 if (lseek64(fd
, dsize
- (sector_size
* (2 + sectors
)),
8391 if ((unsigned long long)write(fd
, buf
+ sector_size
,
8392 sector_size
* sectors
) != sector_size
* sectors
)
8396 /* first block is stored on second to last sector of the disk */
8397 if (lseek64(fd
, dsize
- (sector_size
* 2), SEEK_SET
) < 0)
8400 if ((unsigned int)write(fd
, buf
, sector_size
) != sector_size
)
8406 static void imsm_sync_metadata(struct supertype
*container
)
8408 struct intel_super
*super
= container
->sb
;
8410 dprintf("sync metadata: %d\n", super
->updates_pending
);
8411 if (!super
->updates_pending
)
8414 write_super_imsm(container
, 0);
8416 super
->updates_pending
= 0;
8419 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
8421 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
8422 int i
= get_imsm_disk_idx(dev
, idx
, MAP_X
);
8425 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
8429 if (dl
&& is_failed(&dl
->disk
))
8433 dprintf("found %x:%x\n", dl
->major
, dl
->minor
);
8438 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
8439 struct active_array
*a
, int activate_new
,
8440 struct mdinfo
*additional_test_list
)
8442 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
8443 int idx
= get_imsm_disk_idx(dev
, slot
, MAP_X
);
8444 struct imsm_super
*mpb
= super
->anchor
;
8445 struct imsm_map
*map
;
8446 unsigned long long pos
;
8451 __u32 array_start
= 0;
8452 __u32 array_end
= 0;
8454 struct mdinfo
*test_list
;
8456 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
8457 /* If in this array, skip */
8458 for (d
= a
->info
.devs
; d
; d
= d
->next
)
8459 if (d
->state_fd
>= 0 &&
8460 d
->disk
.major
== dl
->major
&&
8461 d
->disk
.minor
== dl
->minor
) {
8462 dprintf("%x:%x already in array\n",
8463 dl
->major
, dl
->minor
);
8468 test_list
= additional_test_list
;
8470 if (test_list
->disk
.major
== dl
->major
&&
8471 test_list
->disk
.minor
== dl
->minor
) {
8472 dprintf("%x:%x already in additional test list\n",
8473 dl
->major
, dl
->minor
);
8476 test_list
= test_list
->next
;
8481 /* skip in use or failed drives */
8482 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
8484 dprintf("%x:%x status (failed: %d index: %d)\n",
8485 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
8489 /* skip pure spares when we are looking for partially
8490 * assimilated drives
8492 if (dl
->index
== -1 && !activate_new
)
8495 /* Does this unused device have the requisite free space?
8496 * It needs to be able to cover all member volumes
8498 ex
= get_extents(super
, dl
);
8500 dprintf("cannot get extents\n");
8503 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8504 dev
= get_imsm_dev(super
, i
);
8505 map
= get_imsm_map(dev
, MAP_0
);
8507 /* check if this disk is already a member of
8510 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
8516 array_start
= pba_of_lba0(map
);
8517 array_end
= array_start
+
8518 blocks_per_member(map
) - 1;
8521 /* check that we can start at pba_of_lba0 with
8522 * blocks_per_member of space
8524 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
8528 pos
= ex
[j
].start
+ ex
[j
].size
;
8530 } while (ex
[j
-1].size
);
8537 if (i
< mpb
->num_raid_devs
) {
8538 dprintf("%x:%x does not have %u to %u available\n",
8539 dl
->major
, dl
->minor
, array_start
, array_end
);
8549 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
8551 struct imsm_dev
*dev2
;
8552 struct imsm_map
*map
;
8558 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
8560 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
, MAP_0
);
8561 if (state
== IMSM_T_STATE_FAILED
) {
8562 map
= get_imsm_map(dev2
, MAP_0
);
8565 for (slot
= 0; slot
< map
->num_members
; slot
++) {
8567 * Check if failed disks are deleted from intel
8568 * disk list or are marked to be deleted
8570 idx
= get_imsm_disk_idx(dev2
, slot
, MAP_X
);
8571 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
8573 * Do not rebuild the array if failed disks
8574 * from failed sub-array are not removed from
8578 is_failed(&idisk
->disk
) &&
8579 (idisk
->action
!= DISK_REMOVE
))
8587 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
8588 struct metadata_update
**updates
)
8591 * Find a device with unused free space and use it to replace a
8592 * failed/vacant region in an array. We replace failed regions one a
8593 * array at a time. The result is that a new spare disk will be added
8594 * to the first failed array and after the monitor has finished
8595 * propagating failures the remainder will be consumed.
8597 * FIXME add a capability for mdmon to request spares from another
8601 struct intel_super
*super
= a
->container
->sb
;
8602 int inst
= a
->info
.container_member
;
8603 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
8604 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8605 int failed
= a
->info
.array
.raid_disks
;
8606 struct mdinfo
*rv
= NULL
;
8609 struct metadata_update
*mu
;
8611 struct imsm_update_activate_spare
*u
;
8616 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
8617 if ((d
->curr_state
& DS_FAULTY
) &&
8619 /* wait for Removal to happen */
8621 if (d
->state_fd
>= 0)
8625 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
8626 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
8628 if (imsm_reshape_blocks_arrays_changes(super
))
8631 /* Cannot activate another spare if rebuild is in progress already
8633 if (is_rebuilding(dev
)) {
8634 dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n");
8638 if (a
->info
.array
.level
== 4)
8639 /* No repair for takeovered array
8640 * imsm doesn't support raid4
8644 if (imsm_check_degraded(super
, dev
, failed
, MAP_0
) !=
8645 IMSM_T_STATE_DEGRADED
)
8648 if (get_imsm_map(dev
, MAP_0
)->map_state
== IMSM_T_STATE_UNINITIALIZED
) {
8649 dprintf("imsm: No spare activation allowed. Volume is not initialized.\n");
8654 * If there are any failed disks check state of the other volume.
8655 * Block rebuild if the another one is failed until failed disks
8656 * are removed from container.
8659 dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n",
8660 MAX_RAID_SERIAL_LEN
, dev
->volume
);
8661 /* check if states of the other volumes allow for rebuild */
8662 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
8664 allowed
= imsm_rebuild_allowed(a
->container
,
8672 /* For each slot, if it is not working, find a spare */
8673 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
8674 for (d
= a
->info
.devs
; d
; d
= d
->next
)
8675 if (d
->disk
.raid_disk
== i
)
8677 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
8678 if (d
&& (d
->state_fd
>= 0))
8682 * OK, this device needs recovery. Try to re-add the
8683 * previous occupant of this slot, if this fails see if
8684 * we can continue the assimilation of a spare that was
8685 * partially assimilated, finally try to activate a new
8688 dl
= imsm_readd(super
, i
, a
);
8690 dl
= imsm_add_spare(super
, i
, a
, 0, rv
);
8692 dl
= imsm_add_spare(super
, i
, a
, 1, rv
);
8696 /* found a usable disk with enough space */
8697 di
= xcalloc(1, sizeof(*di
));
8699 /* dl->index will be -1 in the case we are activating a
8700 * pristine spare. imsm_process_update() will create a
8701 * new index in this case. Once a disk is found to be
8702 * failed in all member arrays it is kicked from the
8705 di
->disk
.number
= dl
->index
;
8707 /* (ab)use di->devs to store a pointer to the device
8710 di
->devs
= (struct mdinfo
*) dl
;
8712 di
->disk
.raid_disk
= i
;
8713 di
->disk
.major
= dl
->major
;
8714 di
->disk
.minor
= dl
->minor
;
8716 di
->recovery_start
= 0;
8717 di
->data_offset
= pba_of_lba0(map
);
8718 di
->component_size
= a
->info
.component_size
;
8719 di
->container_member
= inst
;
8720 di
->bb
.supported
= 1;
8721 if (dev
->rwh_policy
== RWH_DISTRIBUTED
) {
8722 di
->consistency_policy
= CONSISTENCY_POLICY_PPL
;
8723 di
->ppl_sector
= get_ppl_sector(super
, inst
);
8724 di
->ppl_size
= (PPL_HEADER_SIZE
+ PPL_ENTRY_SPACE
) >> 9;
8726 super
->random
= random32();
8730 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
8731 i
, di
->data_offset
);
8735 /* No spares found */
8737 /* Now 'rv' has a list of devices to return.
8738 * Create a metadata_update record to update the
8739 * disk_ord_tbl for the array
8741 mu
= xmalloc(sizeof(*mu
));
8742 mu
->buf
= xcalloc(num_spares
,
8743 sizeof(struct imsm_update_activate_spare
));
8745 mu
->space_list
= NULL
;
8746 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
8747 mu
->next
= *updates
;
8748 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
8750 for (di
= rv
; di
; di
= di
->next
) {
8751 u
->type
= update_activate_spare
;
8752 u
->dl
= (struct dl
*) di
->devs
;
8754 u
->slot
= di
->disk
.raid_disk
;
8765 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
8767 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
8768 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8769 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, MAP_0
);
8770 struct disk_info
*inf
= get_disk_info(u
);
8771 struct imsm_disk
*disk
;
8775 for (i
= 0; i
< map
->num_members
; i
++) {
8776 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, MAP_X
));
8777 for (j
= 0; j
< new_map
->num_members
; j
++)
8778 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
8785 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
8789 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
8790 if (dl
->major
== major
&& dl
->minor
== minor
)
8795 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
8801 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
8802 if (dl
->major
== major
&& dl
->minor
== minor
) {
8805 prev
->next
= dl
->next
;
8807 super
->disks
= dl
->next
;
8809 __free_imsm_disk(dl
);
8810 dprintf("removed %x:%x\n", major
, minor
);
8818 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
8820 static int add_remove_disk_update(struct intel_super
*super
)
8822 int check_degraded
= 0;
8825 /* add/remove some spares to/from the metadata/contrainer */
8826 while (super
->disk_mgmt_list
) {
8827 struct dl
*disk_cfg
;
8829 disk_cfg
= super
->disk_mgmt_list
;
8830 super
->disk_mgmt_list
= disk_cfg
->next
;
8831 disk_cfg
->next
= NULL
;
8833 if (disk_cfg
->action
== DISK_ADD
) {
8834 disk_cfg
->next
= super
->disks
;
8835 super
->disks
= disk_cfg
;
8837 dprintf("added %x:%x\n",
8838 disk_cfg
->major
, disk_cfg
->minor
);
8839 } else if (disk_cfg
->action
== DISK_REMOVE
) {
8840 dprintf("Disk remove action processed: %x.%x\n",
8841 disk_cfg
->major
, disk_cfg
->minor
);
8842 disk
= get_disk_super(super
,
8846 /* store action status */
8847 disk
->action
= DISK_REMOVE
;
8848 /* remove spare disks only */
8849 if (disk
->index
== -1) {
8850 remove_disk_super(super
,
8855 /* release allocate disk structure */
8856 __free_imsm_disk(disk_cfg
);
8859 return check_degraded
;
8862 static int apply_reshape_migration_update(struct imsm_update_reshape_migration
*u
,
8863 struct intel_super
*super
,
8866 struct intel_dev
*id
;
8867 void **tofree
= NULL
;
8870 dprintf("(enter)\n");
8871 if (u
->subdev
< 0 || u
->subdev
> 1) {
8872 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
8875 if (space_list
== NULL
|| *space_list
== NULL
) {
8876 dprintf("imsm: Error: Memory is not allocated\n");
8880 for (id
= super
->devlist
; id
; id
= id
->next
) {
8881 if (id
->index
== (unsigned)u
->subdev
) {
8882 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
8883 struct imsm_map
*map
;
8884 struct imsm_dev
*new_dev
=
8885 (struct imsm_dev
*)*space_list
;
8886 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
8888 struct dl
*new_disk
;
8890 if (new_dev
== NULL
)
8892 *space_list
= **space_list
;
8893 memcpy(new_dev
, dev
, sizeof_imsm_dev(dev
, 0));
8894 map
= get_imsm_map(new_dev
, MAP_0
);
8896 dprintf("imsm: Error: migration in progress");
8900 to_state
= map
->map_state
;
8901 if ((u
->new_level
== 5) && (map
->raid_level
== 0)) {
8903 /* this should not happen */
8904 if (u
->new_disks
[0] < 0) {
8905 map
->failed_disk_num
=
8906 map
->num_members
- 1;
8907 to_state
= IMSM_T_STATE_DEGRADED
;
8909 to_state
= IMSM_T_STATE_NORMAL
;
8911 migrate(new_dev
, super
, to_state
, MIGR_GEN_MIGR
);
8912 if (u
->new_level
> -1)
8913 map
->raid_level
= u
->new_level
;
8914 migr_map
= get_imsm_map(new_dev
, MAP_1
);
8915 if ((u
->new_level
== 5) &&
8916 (migr_map
->raid_level
== 0)) {
8917 int ord
= map
->num_members
- 1;
8918 migr_map
->num_members
--;
8919 if (u
->new_disks
[0] < 0)
8920 ord
|= IMSM_ORD_REBUILD
;
8921 set_imsm_ord_tbl_ent(map
,
8922 map
->num_members
- 1,
8926 tofree
= (void **)dev
;
8928 /* update chunk size
8930 if (u
->new_chunksize
> 0) {
8931 unsigned long long num_data_stripes
;
8933 imsm_num_data_members(dev
, MAP_0
);
8935 if (used_disks
== 0)
8938 map
->blocks_per_strip
=
8939 __cpu_to_le16(u
->new_chunksize
* 2);
8941 (join_u32(dev
->size_low
, dev
->size_high
)
8943 num_data_stripes
/= map
->blocks_per_strip
;
8944 num_data_stripes
/= map
->num_domains
;
8945 set_num_data_stripes(map
, num_data_stripes
);
8950 if (u
->new_level
!= 5 || migr_map
->raid_level
!= 0 ||
8951 migr_map
->raid_level
== map
->raid_level
)
8954 if (u
->new_disks
[0] >= 0) {
8957 new_disk
= get_disk_super(super
,
8958 major(u
->new_disks
[0]),
8959 minor(u
->new_disks
[0]));
8960 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
8961 major(u
->new_disks
[0]),
8962 minor(u
->new_disks
[0]),
8963 new_disk
, new_disk
->index
);
8964 if (new_disk
== NULL
)
8965 goto error_disk_add
;
8967 new_disk
->index
= map
->num_members
- 1;
8968 /* slot to fill in autolayout
8970 new_disk
->raiddisk
= new_disk
->index
;
8971 new_disk
->disk
.status
|= CONFIGURED_DISK
;
8972 new_disk
->disk
.status
&= ~SPARE_DISK
;
8974 goto error_disk_add
;
8977 *tofree
= *space_list
;
8978 /* calculate new size
8980 imsm_set_array_size(new_dev
, -1);
8987 *space_list
= tofree
;
8991 dprintf("Error: imsm: Cannot find disk.\n");
8995 static int apply_size_change_update(struct imsm_update_size_change
*u
,
8996 struct intel_super
*super
)
8998 struct intel_dev
*id
;
9001 dprintf("(enter)\n");
9002 if (u
->subdev
< 0 || u
->subdev
> 1) {
9003 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
9007 for (id
= super
->devlist
; id
; id
= id
->next
) {
9008 if (id
->index
== (unsigned)u
->subdev
) {
9009 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
9010 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9011 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
9012 unsigned long long blocks_per_member
;
9013 unsigned long long num_data_stripes
;
9015 /* calculate new size
9017 blocks_per_member
= u
->new_size
/ used_disks
;
9018 num_data_stripes
= blocks_per_member
/
9019 map
->blocks_per_strip
;
9020 num_data_stripes
/= map
->num_domains
;
9021 dprintf("(size: %llu, blocks per member: %llu, num_data_stipes: %llu)\n",
9022 u
->new_size
, blocks_per_member
,
9024 set_blocks_per_member(map
, blocks_per_member
);
9025 set_num_data_stripes(map
, num_data_stripes
);
9026 imsm_set_array_size(dev
, u
->new_size
);
9036 static int apply_update_activate_spare(struct imsm_update_activate_spare
*u
,
9037 struct intel_super
*super
,
9038 struct active_array
*active_array
)
9040 struct imsm_super
*mpb
= super
->anchor
;
9041 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
9042 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9043 struct imsm_map
*migr_map
;
9044 struct active_array
*a
;
9045 struct imsm_disk
*disk
;
9052 int second_map_created
= 0;
9054 for (; u
; u
= u
->next
) {
9055 victim
= get_imsm_disk_idx(dev
, u
->slot
, MAP_X
);
9060 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
9065 pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n",
9070 /* count failures (excluding rebuilds and the victim)
9071 * to determine map[0] state
9074 for (i
= 0; i
< map
->num_members
; i
++) {
9077 disk
= get_imsm_disk(super
,
9078 get_imsm_disk_idx(dev
, i
, MAP_X
));
9079 if (!disk
|| is_failed(disk
))
9083 /* adding a pristine spare, assign a new index */
9084 if (dl
->index
< 0) {
9085 dl
->index
= super
->anchor
->num_disks
;
9086 super
->anchor
->num_disks
++;
9089 disk
->status
|= CONFIGURED_DISK
;
9090 disk
->status
&= ~SPARE_DISK
;
9093 to_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
9094 if (!second_map_created
) {
9095 second_map_created
= 1;
9096 map
->map_state
= IMSM_T_STATE_DEGRADED
;
9097 migrate(dev
, super
, to_state
, MIGR_REBUILD
);
9099 map
->map_state
= to_state
;
9100 migr_map
= get_imsm_map(dev
, MAP_1
);
9101 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
9102 set_imsm_ord_tbl_ent(migr_map
, u
->slot
,
9103 dl
->index
| IMSM_ORD_REBUILD
);
9105 /* update the family_num to mark a new container
9106 * generation, being careful to record the existing
9107 * family_num in orig_family_num to clean up after
9108 * earlier mdadm versions that neglected to set it.
9110 if (mpb
->orig_family_num
== 0)
9111 mpb
->orig_family_num
= mpb
->family_num
;
9112 mpb
->family_num
+= super
->random
;
9114 /* count arrays using the victim in the metadata */
9116 for (a
= active_array
; a
; a
= a
->next
) {
9117 dev
= get_imsm_dev(super
, a
->info
.container_member
);
9118 map
= get_imsm_map(dev
, MAP_0
);
9120 if (get_imsm_disk_slot(map
, victim
) >= 0)
9124 /* delete the victim if it is no longer being
9130 /* We know that 'manager' isn't touching anything,
9131 * so it is safe to delete
9133 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
9134 if ((*dlp
)->index
== victim
)
9137 /* victim may be on the missing list */
9139 for (dlp
= &super
->missing
; *dlp
;
9140 dlp
= &(*dlp
)->next
)
9141 if ((*dlp
)->index
== victim
)
9143 imsm_delete(super
, dlp
, victim
);
9150 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
9151 struct intel_super
*super
,
9154 struct dl
*new_disk
;
9155 struct intel_dev
*id
;
9157 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
9158 int disk_count
= u
->old_raid_disks
;
9159 void **tofree
= NULL
;
9160 int devices_to_reshape
= 1;
9161 struct imsm_super
*mpb
= super
->anchor
;
9163 unsigned int dev_id
;
9165 dprintf("(enter)\n");
9167 /* enable spares to use in array */
9168 for (i
= 0; i
< delta_disks
; i
++) {
9169 new_disk
= get_disk_super(super
,
9170 major(u
->new_disks
[i
]),
9171 minor(u
->new_disks
[i
]));
9172 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
9173 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
9174 new_disk
, new_disk
->index
);
9175 if (new_disk
== NULL
||
9176 (new_disk
->index
>= 0 &&
9177 new_disk
->index
< u
->old_raid_disks
))
9178 goto update_reshape_exit
;
9179 new_disk
->index
= disk_count
++;
9180 /* slot to fill in autolayout
9182 new_disk
->raiddisk
= new_disk
->index
;
9183 new_disk
->disk
.status
|=
9185 new_disk
->disk
.status
&= ~SPARE_DISK
;
9188 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
9189 mpb
->num_raid_devs
);
9190 /* manage changes in volume
9192 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
9193 void **sp
= *space_list
;
9194 struct imsm_dev
*newdev
;
9195 struct imsm_map
*newmap
, *oldmap
;
9197 for (id
= super
->devlist
; id
; id
= id
->next
) {
9198 if (id
->index
== dev_id
)
9207 /* Copy the dev, but not (all of) the map */
9208 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
9209 oldmap
= get_imsm_map(id
->dev
, MAP_0
);
9210 newmap
= get_imsm_map(newdev
, MAP_0
);
9211 /* Copy the current map */
9212 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
9213 /* update one device only
9215 if (devices_to_reshape
) {
9216 dprintf("imsm: modifying subdev: %i\n",
9218 devices_to_reshape
--;
9219 newdev
->vol
.migr_state
= 1;
9220 newdev
->vol
.curr_migr_unit
= 0;
9221 set_migr_type(newdev
, MIGR_GEN_MIGR
);
9222 newmap
->num_members
= u
->new_raid_disks
;
9223 for (i
= 0; i
< delta_disks
; i
++) {
9224 set_imsm_ord_tbl_ent(newmap
,
9225 u
->old_raid_disks
+ i
,
9226 u
->old_raid_disks
+ i
);
9228 /* New map is correct, now need to save old map
9230 newmap
= get_imsm_map(newdev
, MAP_1
);
9231 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
9233 imsm_set_array_size(newdev
, -1);
9236 sp
= (void **)id
->dev
;
9241 /* Clear migration record */
9242 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
9245 *space_list
= tofree
;
9248 update_reshape_exit
:
9253 static int apply_takeover_update(struct imsm_update_takeover
*u
,
9254 struct intel_super
*super
,
9257 struct imsm_dev
*dev
= NULL
;
9258 struct intel_dev
*dv
;
9259 struct imsm_dev
*dev_new
;
9260 struct imsm_map
*map
;
9264 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
9265 if (dv
->index
== (unsigned int)u
->subarray
) {
9273 map
= get_imsm_map(dev
, MAP_0
);
9275 if (u
->direction
== R10_TO_R0
) {
9276 unsigned long long num_data_stripes
;
9278 map
->num_domains
= 1;
9279 num_data_stripes
= blocks_per_member(map
);
9280 num_data_stripes
/= map
->blocks_per_strip
;
9281 num_data_stripes
/= map
->num_domains
;
9282 set_num_data_stripes(map
, num_data_stripes
);
9284 /* Number of failed disks must be half of initial disk number */
9285 if (imsm_count_failed(super
, dev
, MAP_0
) !=
9286 (map
->num_members
/ 2))
9289 /* iterate through devices to mark removed disks as spare */
9290 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
9291 if (dm
->disk
.status
& FAILED_DISK
) {
9292 int idx
= dm
->index
;
9293 /* update indexes on the disk list */
9294 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
9295 the index values will end up being correct.... NB */
9296 for (du
= super
->disks
; du
; du
= du
->next
)
9297 if (du
->index
> idx
)
9299 /* mark as spare disk */
9304 map
->num_members
= map
->num_members
/ 2;
9305 map
->map_state
= IMSM_T_STATE_NORMAL
;
9306 map
->num_domains
= 1;
9307 map
->raid_level
= 0;
9308 map
->failed_disk_num
= -1;
9311 if (u
->direction
== R0_TO_R10
) {
9313 /* update slots in current disk list */
9314 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
9318 /* create new *missing* disks */
9319 for (i
= 0; i
< map
->num_members
; i
++) {
9320 space
= *space_list
;
9323 *space_list
= *space
;
9325 memcpy(du
, super
->disks
, sizeof(*du
));
9329 du
->index
= (i
* 2) + 1;
9330 sprintf((char *)du
->disk
.serial
,
9331 " MISSING_%d", du
->index
);
9332 sprintf((char *)du
->serial
,
9333 "MISSING_%d", du
->index
);
9334 du
->next
= super
->missing
;
9335 super
->missing
= du
;
9337 /* create new dev and map */
9338 space
= *space_list
;
9341 *space_list
= *space
;
9342 dev_new
= (void *)space
;
9343 memcpy(dev_new
, dev
, sizeof(*dev
));
9344 /* update new map */
9345 map
= get_imsm_map(dev_new
, MAP_0
);
9346 map
->num_members
= map
->num_members
* 2;
9347 map
->map_state
= IMSM_T_STATE_DEGRADED
;
9348 map
->num_domains
= 2;
9349 map
->raid_level
= 1;
9350 /* replace dev<->dev_new */
9353 /* update disk order table */
9354 for (du
= super
->disks
; du
; du
= du
->next
)
9356 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
9357 for (du
= super
->missing
; du
; du
= du
->next
)
9358 if (du
->index
>= 0) {
9359 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
9360 mark_missing(super
, dv
->dev
, &du
->disk
, du
->index
);
9366 static void imsm_process_update(struct supertype
*st
,
9367 struct metadata_update
*update
)
9370 * crack open the metadata_update envelope to find the update record
9371 * update can be one of:
9372 * update_reshape_container_disks - all the arrays in the container
9373 * are being reshaped to have more devices. We need to mark
9374 * the arrays for general migration and convert selected spares
9375 * into active devices.
9376 * update_activate_spare - a spare device has replaced a failed
9377 * device in an array, update the disk_ord_tbl. If this disk is
9378 * present in all member arrays then also clear the SPARE_DISK
9380 * update_create_array
9382 * update_rename_array
9383 * update_add_remove_disk
9385 struct intel_super
*super
= st
->sb
;
9386 struct imsm_super
*mpb
;
9387 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
9389 /* update requires a larger buf but the allocation failed */
9390 if (super
->next_len
&& !super
->next_buf
) {
9391 super
->next_len
= 0;
9395 if (super
->next_buf
) {
9396 memcpy(super
->next_buf
, super
->buf
, super
->len
);
9398 super
->len
= super
->next_len
;
9399 super
->buf
= super
->next_buf
;
9401 super
->next_len
= 0;
9402 super
->next_buf
= NULL
;
9405 mpb
= super
->anchor
;
9408 case update_general_migration_checkpoint
: {
9409 struct intel_dev
*id
;
9410 struct imsm_update_general_migration_checkpoint
*u
=
9411 (void *)update
->buf
;
9413 dprintf("called for update_general_migration_checkpoint\n");
9415 /* find device under general migration */
9416 for (id
= super
->devlist
; id
; id
= id
->next
) {
9417 if (is_gen_migration(id
->dev
)) {
9418 id
->dev
->vol
.curr_migr_unit
=
9419 __cpu_to_le32(u
->curr_migr_unit
);
9420 super
->updates_pending
++;
9425 case update_takeover
: {
9426 struct imsm_update_takeover
*u
= (void *)update
->buf
;
9427 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
9428 imsm_update_version_info(super
);
9429 super
->updates_pending
++;
9434 case update_reshape_container_disks
: {
9435 struct imsm_update_reshape
*u
= (void *)update
->buf
;
9436 if (apply_reshape_container_disks_update(
9437 u
, super
, &update
->space_list
))
9438 super
->updates_pending
++;
9441 case update_reshape_migration
: {
9442 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
9443 if (apply_reshape_migration_update(
9444 u
, super
, &update
->space_list
))
9445 super
->updates_pending
++;
9448 case update_size_change
: {
9449 struct imsm_update_size_change
*u
= (void *)update
->buf
;
9450 if (apply_size_change_update(u
, super
))
9451 super
->updates_pending
++;
9454 case update_activate_spare
: {
9455 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
9456 if (apply_update_activate_spare(u
, super
, st
->arrays
))
9457 super
->updates_pending
++;
9460 case update_create_array
: {
9461 /* someone wants to create a new array, we need to be aware of
9462 * a few races/collisions:
9463 * 1/ 'Create' called by two separate instances of mdadm
9464 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
9465 * devices that have since been assimilated via
9467 * In the event this update can not be carried out mdadm will
9468 * (FIX ME) notice that its update did not take hold.
9470 struct imsm_update_create_array
*u
= (void *) update
->buf
;
9471 struct intel_dev
*dv
;
9472 struct imsm_dev
*dev
;
9473 struct imsm_map
*map
, *new_map
;
9474 unsigned long long start
, end
;
9475 unsigned long long new_start
, new_end
;
9477 struct disk_info
*inf
;
9480 /* handle racing creates: first come first serve */
9481 if (u
->dev_idx
< mpb
->num_raid_devs
) {
9482 dprintf("subarray %d already defined\n", u
->dev_idx
);
9486 /* check update is next in sequence */
9487 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
9488 dprintf("can not create array %d expected index %d\n",
9489 u
->dev_idx
, mpb
->num_raid_devs
);
9493 new_map
= get_imsm_map(&u
->dev
, MAP_0
);
9494 new_start
= pba_of_lba0(new_map
);
9495 new_end
= new_start
+ blocks_per_member(new_map
);
9496 inf
= get_disk_info(u
);
9498 /* handle activate_spare versus create race:
9499 * check to make sure that overlapping arrays do not include
9502 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
9503 dev
= get_imsm_dev(super
, i
);
9504 map
= get_imsm_map(dev
, MAP_0
);
9505 start
= pba_of_lba0(map
);
9506 end
= start
+ blocks_per_member(map
);
9507 if ((new_start
>= start
&& new_start
<= end
) ||
9508 (start
>= new_start
&& start
<= new_end
))
9513 if (disks_overlap(super
, i
, u
)) {
9514 dprintf("arrays overlap\n");
9519 /* check that prepare update was successful */
9520 if (!update
->space
) {
9521 dprintf("prepare update failed\n");
9525 /* check that all disks are still active before committing
9526 * changes. FIXME: could we instead handle this by creating a
9527 * degraded array? That's probably not what the user expects,
9528 * so better to drop this update on the floor.
9530 for (i
= 0; i
< new_map
->num_members
; i
++) {
9531 dl
= serial_to_dl(inf
[i
].serial
, super
);
9533 dprintf("disk disappeared\n");
9538 super
->updates_pending
++;
9540 /* convert spares to members and fixup ord_tbl */
9541 for (i
= 0; i
< new_map
->num_members
; i
++) {
9542 dl
= serial_to_dl(inf
[i
].serial
, super
);
9543 if (dl
->index
== -1) {
9544 dl
->index
= mpb
->num_disks
;
9546 dl
->disk
.status
|= CONFIGURED_DISK
;
9547 dl
->disk
.status
&= ~SPARE_DISK
;
9549 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
9554 update
->space
= NULL
;
9555 imsm_copy_dev(dev
, &u
->dev
);
9556 dv
->index
= u
->dev_idx
;
9557 dv
->next
= super
->devlist
;
9558 super
->devlist
= dv
;
9559 mpb
->num_raid_devs
++;
9561 imsm_update_version_info(super
);
9564 /* mdmon knows how to release update->space, but not
9565 * ((struct intel_dev *) update->space)->dev
9567 if (update
->space
) {
9573 case update_kill_array
: {
9574 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
9575 int victim
= u
->dev_idx
;
9576 struct active_array
*a
;
9577 struct intel_dev
**dp
;
9578 struct imsm_dev
*dev
;
9580 /* sanity check that we are not affecting the uuid of
9581 * active arrays, or deleting an active array
9583 * FIXME when immutable ids are available, but note that
9584 * we'll also need to fixup the invalidated/active
9585 * subarray indexes in mdstat
9587 for (a
= st
->arrays
; a
; a
= a
->next
)
9588 if (a
->info
.container_member
>= victim
)
9590 /* by definition if mdmon is running at least one array
9591 * is active in the container, so checking
9592 * mpb->num_raid_devs is just extra paranoia
9594 dev
= get_imsm_dev(super
, victim
);
9595 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
9596 dprintf("failed to delete subarray-%d\n", victim
);
9600 for (dp
= &super
->devlist
; *dp
;)
9601 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
9604 if ((*dp
)->index
> (unsigned)victim
)
9608 mpb
->num_raid_devs
--;
9609 super
->updates_pending
++;
9612 case update_rename_array
: {
9613 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
9614 char name
[MAX_RAID_SERIAL_LEN
+1];
9615 int target
= u
->dev_idx
;
9616 struct active_array
*a
;
9617 struct imsm_dev
*dev
;
9619 /* sanity check that we are not affecting the uuid of
9622 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
9623 name
[MAX_RAID_SERIAL_LEN
] = '\0';
9624 for (a
= st
->arrays
; a
; a
= a
->next
)
9625 if (a
->info
.container_member
== target
)
9627 dev
= get_imsm_dev(super
, u
->dev_idx
);
9628 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
9629 dprintf("failed to rename subarray-%d\n", target
);
9633 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
9634 super
->updates_pending
++;
9637 case update_add_remove_disk
: {
9638 /* we may be able to repair some arrays if disks are
9639 * being added, check the status of add_remove_disk
9640 * if discs has been added.
9642 if (add_remove_disk_update(super
)) {
9643 struct active_array
*a
;
9645 super
->updates_pending
++;
9646 for (a
= st
->arrays
; a
; a
= a
->next
)
9647 a
->check_degraded
= 1;
9651 case update_prealloc_badblocks_mem
:
9653 case update_rwh_policy
: {
9654 struct imsm_update_rwh_policy
*u
= (void *)update
->buf
;
9655 int target
= u
->dev_idx
;
9656 struct imsm_dev
*dev
= get_imsm_dev(super
, target
);
9658 dprintf("could not find subarray-%d\n", target
);
9662 if (dev
->rwh_policy
!= u
->new_policy
) {
9663 dev
->rwh_policy
= u
->new_policy
;
9664 super
->updates_pending
++;
9669 pr_err("error: unsuported process update type:(type: %d)\n", type
);
9673 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
);
9675 static int imsm_prepare_update(struct supertype
*st
,
9676 struct metadata_update
*update
)
9679 * Allocate space to hold new disk entries, raid-device entries or a new
9680 * mpb if necessary. The manager synchronously waits for updates to
9681 * complete in the monitor, so new mpb buffers allocated here can be
9682 * integrated by the monitor thread without worrying about live pointers
9683 * in the manager thread.
9685 enum imsm_update_type type
;
9686 struct intel_super
*super
= st
->sb
;
9687 unsigned int sector_size
= super
->sector_size
;
9688 struct imsm_super
*mpb
= super
->anchor
;
9692 if (update
->len
< (int)sizeof(type
))
9695 type
= *(enum imsm_update_type
*) update
->buf
;
9698 case update_general_migration_checkpoint
:
9699 if (update
->len
< (int)sizeof(struct imsm_update_general_migration_checkpoint
))
9701 dprintf("called for update_general_migration_checkpoint\n");
9703 case update_takeover
: {
9704 struct imsm_update_takeover
*u
= (void *)update
->buf
;
9705 if (update
->len
< (int)sizeof(*u
))
9707 if (u
->direction
== R0_TO_R10
) {
9708 void **tail
= (void **)&update
->space_list
;
9709 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
9710 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9711 int num_members
= map
->num_members
;
9714 /* allocate memory for added disks */
9715 for (i
= 0; i
< num_members
; i
++) {
9716 size
= sizeof(struct dl
);
9717 space
= xmalloc(size
);
9722 /* allocate memory for new device */
9723 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
9724 (num_members
* sizeof(__u32
));
9725 space
= xmalloc(size
);
9729 len
= disks_to_mpb_size(num_members
* 2);
9734 case update_reshape_container_disks
: {
9735 /* Every raid device in the container is about to
9736 * gain some more devices, and we will enter a
9738 * So each 'imsm_map' will be bigger, and the imsm_vol
9739 * will now hold 2 of them.
9740 * Thus we need new 'struct imsm_dev' allocations sized
9741 * as sizeof_imsm_dev but with more devices in both maps.
9743 struct imsm_update_reshape
*u
= (void *)update
->buf
;
9744 struct intel_dev
*dl
;
9745 void **space_tail
= (void**)&update
->space_list
;
9747 if (update
->len
< (int)sizeof(*u
))
9750 dprintf("for update_reshape\n");
9752 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
9753 int size
= sizeof_imsm_dev(dl
->dev
, 1);
9755 if (u
->new_raid_disks
> u
->old_raid_disks
)
9756 size
+= sizeof(__u32
)*2*
9757 (u
->new_raid_disks
- u
->old_raid_disks
);
9764 len
= disks_to_mpb_size(u
->new_raid_disks
);
9765 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
9768 case update_reshape_migration
: {
9769 /* for migration level 0->5 we need to add disks
9770 * so the same as for container operation we will copy
9771 * device to the bigger location.
9772 * in memory prepared device and new disk area are prepared
9773 * for usage in process update
9775 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
9776 struct intel_dev
*id
;
9777 void **space_tail
= (void **)&update
->space_list
;
9780 int current_level
= -1;
9782 if (update
->len
< (int)sizeof(*u
))
9785 dprintf("for update_reshape\n");
9787 /* add space for bigger array in update
9789 for (id
= super
->devlist
; id
; id
= id
->next
) {
9790 if (id
->index
== (unsigned)u
->subdev
) {
9791 size
= sizeof_imsm_dev(id
->dev
, 1);
9792 if (u
->new_raid_disks
> u
->old_raid_disks
)
9793 size
+= sizeof(__u32
)*2*
9794 (u
->new_raid_disks
- u
->old_raid_disks
);
9802 if (update
->space_list
== NULL
)
9805 /* add space for disk in update
9807 size
= sizeof(struct dl
);
9813 /* add spare device to update
9815 for (id
= super
->devlist
; id
; id
= id
->next
)
9816 if (id
->index
== (unsigned)u
->subdev
) {
9817 struct imsm_dev
*dev
;
9818 struct imsm_map
*map
;
9820 dev
= get_imsm_dev(super
, u
->subdev
);
9821 map
= get_imsm_map(dev
, MAP_0
);
9822 current_level
= map
->raid_level
;
9825 if (u
->new_level
== 5 && u
->new_level
!= current_level
) {
9826 struct mdinfo
*spares
;
9828 spares
= get_spares_for_grow(st
);
9836 makedev(dev
->disk
.major
,
9838 dl
= get_disk_super(super
,
9841 dl
->index
= u
->old_raid_disks
;
9847 len
= disks_to_mpb_size(u
->new_raid_disks
);
9848 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
9851 case update_size_change
: {
9852 if (update
->len
< (int)sizeof(struct imsm_update_size_change
))
9856 case update_activate_spare
: {
9857 if (update
->len
< (int)sizeof(struct imsm_update_activate_spare
))
9861 case update_create_array
: {
9862 struct imsm_update_create_array
*u
= (void *) update
->buf
;
9863 struct intel_dev
*dv
;
9864 struct imsm_dev
*dev
= &u
->dev
;
9865 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
9867 struct disk_info
*inf
;
9871 if (update
->len
< (int)sizeof(*u
))
9874 inf
= get_disk_info(u
);
9875 len
= sizeof_imsm_dev(dev
, 1);
9876 /* allocate a new super->devlist entry */
9877 dv
= xmalloc(sizeof(*dv
));
9878 dv
->dev
= xmalloc(len
);
9881 /* count how many spares will be converted to members */
9882 for (i
= 0; i
< map
->num_members
; i
++) {
9883 dl
= serial_to_dl(inf
[i
].serial
, super
);
9885 /* hmm maybe it failed?, nothing we can do about
9890 if (count_memberships(dl
, super
) == 0)
9893 len
+= activate
* sizeof(struct imsm_disk
);
9896 case update_kill_array
: {
9897 if (update
->len
< (int)sizeof(struct imsm_update_kill_array
))
9901 case update_rename_array
: {
9902 if (update
->len
< (int)sizeof(struct imsm_update_rename_array
))
9906 case update_add_remove_disk
:
9907 /* no update->len needed */
9909 case update_prealloc_badblocks_mem
:
9910 super
->extra_space
+= sizeof(struct bbm_log
) -
9911 get_imsm_bbm_log_size(super
->bbm_log
);
9913 case update_rwh_policy
: {
9914 if (update
->len
< (int)sizeof(struct imsm_update_rwh_policy
))
9922 /* check if we need a larger metadata buffer */
9923 if (super
->next_buf
)
9924 buf_len
= super
->next_len
;
9926 buf_len
= super
->len
;
9928 if (__le32_to_cpu(mpb
->mpb_size
) + super
->extra_space
+ len
> buf_len
) {
9929 /* ok we need a larger buf than what is currently allocated
9930 * if this allocation fails process_update will notice that
9931 * ->next_len is set and ->next_buf is NULL
9933 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) +
9934 super
->extra_space
+ len
, sector_size
);
9935 if (super
->next_buf
)
9936 free(super
->next_buf
);
9938 super
->next_len
= buf_len
;
9939 if (posix_memalign(&super
->next_buf
, sector_size
, buf_len
) == 0)
9940 memset(super
->next_buf
, 0, buf_len
);
9942 super
->next_buf
= NULL
;
9947 /* must be called while manager is quiesced */
9948 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
9950 struct imsm_super
*mpb
= super
->anchor
;
9952 struct imsm_dev
*dev
;
9953 struct imsm_map
*map
;
9954 unsigned int i
, j
, num_members
;
9956 struct bbm_log
*log
= super
->bbm_log
;
9958 dprintf("deleting device[%d] from imsm_super\n", index
);
9960 /* shift all indexes down one */
9961 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
9962 if (iter
->index
> (int)index
)
9964 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
9965 if (iter
->index
> (int)index
)
9968 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
9969 dev
= get_imsm_dev(super
, i
);
9970 map
= get_imsm_map(dev
, MAP_0
);
9971 num_members
= map
->num_members
;
9972 for (j
= 0; j
< num_members
; j
++) {
9973 /* update ord entries being careful not to propagate
9974 * ord-flags to the first map
9976 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_X
);
9978 if (ord_to_idx(ord
) <= index
)
9981 map
= get_imsm_map(dev
, MAP_0
);
9982 set_imsm_ord_tbl_ent(map
, j
, ord_to_idx(ord
- 1));
9983 map
= get_imsm_map(dev
, MAP_1
);
9985 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
9989 for (i
= 0; i
< log
->entry_count
; i
++) {
9990 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[i
];
9992 if (entry
->disk_ordinal
<= index
)
9994 entry
->disk_ordinal
--;
9998 super
->updates_pending
++;
10000 struct dl
*dl
= *dlp
;
10002 *dlp
= (*dlp
)->next
;
10003 __free_imsm_disk(dl
);
10006 #endif /* MDASSEMBLE */
10008 static void close_targets(int *targets
, int new_disks
)
10015 for (i
= 0; i
< new_disks
; i
++) {
10016 if (targets
[i
] >= 0) {
10023 static int imsm_get_allowed_degradation(int level
, int raid_disks
,
10024 struct intel_super
*super
,
10025 struct imsm_dev
*dev
)
10031 struct imsm_map
*map
;
10034 ret_val
= raid_disks
/2;
10035 /* check map if all disks pairs not failed
10038 map
= get_imsm_map(dev
, MAP_0
);
10039 for (i
= 0; i
< ret_val
; i
++) {
10040 int degradation
= 0;
10041 if (get_imsm_disk(super
, i
) == NULL
)
10043 if (get_imsm_disk(super
, i
+ 1) == NULL
)
10045 if (degradation
== 2)
10048 map
= get_imsm_map(dev
, MAP_1
);
10049 /* if there is no second map
10050 * result can be returned
10054 /* check degradation in second map
10056 for (i
= 0; i
< ret_val
; i
++) {
10057 int degradation
= 0;
10058 if (get_imsm_disk(super
, i
) == NULL
)
10060 if (get_imsm_disk(super
, i
+ 1) == NULL
)
10062 if (degradation
== 2)
10076 /*******************************************************************************
10077 * Function: open_backup_targets
10078 * Description: Function opens file descriptors for all devices given in
10081 * info : general array info
10082 * raid_disks : number of disks
10083 * raid_fds : table of device's file descriptors
10084 * super : intel super for raid10 degradation check
10085 * dev : intel device for raid10 degradation check
10089 ******************************************************************************/
10090 int open_backup_targets(struct mdinfo
*info
, int raid_disks
, int *raid_fds
,
10091 struct intel_super
*super
, struct imsm_dev
*dev
)
10097 for (i
= 0; i
< raid_disks
; i
++)
10100 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
10103 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
)) {
10104 dprintf("disk is faulty!!\n");
10108 if (sd
->disk
.raid_disk
>= raid_disks
|| sd
->disk
.raid_disk
< 0)
10111 dn
= map_dev(sd
->disk
.major
,
10112 sd
->disk
.minor
, 1);
10113 raid_fds
[sd
->disk
.raid_disk
] = dev_open(dn
, O_RDWR
);
10114 if (raid_fds
[sd
->disk
.raid_disk
] < 0) {
10115 pr_err("cannot open component\n");
10120 /* check if maximum array degradation level is not exceeded
10122 if ((raid_disks
- opened
) >
10123 imsm_get_allowed_degradation(info
->new_level
, raid_disks
,
10125 pr_err("Not enough disks can be opened.\n");
10126 close_targets(raid_fds
, raid_disks
);
10132 /*******************************************************************************
10133 * Function: validate_container_imsm
10134 * Description: This routine validates container after assemble,
10135 * eg. if devices in container are under the same controller.
10138 * info : linked list with info about devices used in array
10142 ******************************************************************************/
10143 int validate_container_imsm(struct mdinfo
*info
)
10145 if (check_env("IMSM_NO_PLATFORM"))
10148 struct sys_dev
*idev
;
10149 struct sys_dev
*hba
= NULL
;
10150 struct sys_dev
*intel_devices
= find_intel_devices();
10151 char *dev_path
= devt_to_devpath(makedev(info
->disk
.major
,
10152 info
->disk
.minor
));
10154 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
10155 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
10164 pr_err("WARNING - Cannot detect HBA for device %s!\n",
10165 devid2kname(makedev(info
->disk
.major
, info
->disk
.minor
)));
10169 const struct imsm_orom
*orom
= get_orom_by_device_id(hba
->dev_id
);
10170 struct mdinfo
*dev
;
10172 for (dev
= info
->next
; dev
; dev
= dev
->next
) {
10173 dev_path
= devt_to_devpath(makedev(dev
->disk
.major
, dev
->disk
.minor
));
10175 struct sys_dev
*hba2
= NULL
;
10176 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
10177 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
10185 const struct imsm_orom
*orom2
= hba2
== NULL
? NULL
:
10186 get_orom_by_device_id(hba2
->dev_id
);
10188 if (hba2
&& hba
->type
!= hba2
->type
) {
10189 pr_err("WARNING - HBAs of devices do not match %s != %s\n",
10190 get_sys_dev_type(hba
->type
), get_sys_dev_type(hba2
->type
));
10194 if (orom
!= orom2
) {
10195 pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n"
10196 " This operation is not supported and can lead to data loss.\n");
10201 pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n"
10202 " This operation is not supported and can lead to data loss.\n");
10210 /*******************************************************************************
10211 * Function: imsm_record_badblock
10212 * Description: This routine stores new bad block record in BBM log
10215 * a : array containing a bad block
10216 * slot : disk number containing a bad block
10217 * sector : bad block sector
10218 * length : bad block sectors range
10222 ******************************************************************************/
10223 static int imsm_record_badblock(struct active_array
*a
, int slot
,
10224 unsigned long long sector
, int length
)
10226 struct intel_super
*super
= a
->container
->sb
;
10230 ord
= imsm_disk_slot_to_ord(a
, slot
);
10234 ret
= record_new_badblock(super
->bbm_log
, ord_to_idx(ord
), sector
,
10237 super
->updates_pending
++;
10241 /*******************************************************************************
10242 * Function: imsm_clear_badblock
10243 * Description: This routine clears bad block record from BBM log
10246 * a : array containing a bad block
10247 * slot : disk number containing a bad block
10248 * sector : bad block sector
10249 * length : bad block sectors range
10253 ******************************************************************************/
10254 static int imsm_clear_badblock(struct active_array
*a
, int slot
,
10255 unsigned long long sector
, int length
)
10257 struct intel_super
*super
= a
->container
->sb
;
10261 ord
= imsm_disk_slot_to_ord(a
, slot
);
10265 ret
= clear_badblock(super
->bbm_log
, ord_to_idx(ord
), sector
, length
);
10267 super
->updates_pending
++;
10271 /*******************************************************************************
10272 * Function: imsm_get_badblocks
10273 * Description: This routine get list of bad blocks for an array
10277 * slot : disk number
10279 * bb : structure containing bad blocks
10281 ******************************************************************************/
10282 static struct md_bb
*imsm_get_badblocks(struct active_array
*a
, int slot
)
10284 int inst
= a
->info
.container_member
;
10285 struct intel_super
*super
= a
->container
->sb
;
10286 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
10287 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
10290 ord
= imsm_disk_slot_to_ord(a
, slot
);
10294 get_volume_badblocks(super
->bbm_log
, ord_to_idx(ord
), pba_of_lba0(map
),
10295 blocks_per_member(map
), &super
->bb
);
10299 /*******************************************************************************
10300 * Function: examine_badblocks_imsm
10301 * Description: Prints list of bad blocks on a disk to the standard output
10304 * st : metadata handler
10305 * fd : open file descriptor for device
10306 * devname : device name
10310 ******************************************************************************/
10311 static int examine_badblocks_imsm(struct supertype
*st
, int fd
, char *devname
)
10313 struct intel_super
*super
= st
->sb
;
10314 struct bbm_log
*log
= super
->bbm_log
;
10315 struct dl
*d
= NULL
;
10318 for (d
= super
->disks
; d
; d
= d
->next
) {
10319 if (strcmp(d
->devname
, devname
) == 0)
10323 if ((d
== NULL
) || (d
->index
< 0)) { /* serial mismatch probably */
10324 pr_err("%s doesn't appear to be part of a raid array\n",
10331 struct bbm_log_entry
*entry
= &log
->marked_block_entries
[0];
10333 for (i
= 0; i
< log
->entry_count
; i
++) {
10334 if (entry
[i
].disk_ordinal
== d
->index
) {
10335 unsigned long long sector
= __le48_to_cpu(
10336 &entry
[i
].defective_block_start
);
10337 int cnt
= entry
[i
].marked_count
+ 1;
10340 printf("Bad-blocks on %s:\n", devname
);
10344 printf("%20llu for %d sectors\n", sector
, cnt
);
10350 printf("No bad-blocks list configured on %s\n", devname
);
10354 /*******************************************************************************
10355 * Function: init_migr_record_imsm
10356 * Description: Function inits imsm migration record
10358 * super : imsm internal array info
10359 * dev : device under migration
10360 * info : general array info to find the smallest device
10363 ******************************************************************************/
10364 void init_migr_record_imsm(struct supertype
*st
, struct imsm_dev
*dev
,
10365 struct mdinfo
*info
)
10367 struct intel_super
*super
= st
->sb
;
10368 struct migr_record
*migr_rec
= super
->migr_rec
;
10369 int new_data_disks
;
10370 unsigned long long dsize
, dev_sectors
;
10371 long long unsigned min_dev_sectors
= -1LLU;
10375 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
10376 struct imsm_map
*map_src
= get_imsm_map(dev
, MAP_1
);
10377 unsigned long long num_migr_units
;
10378 unsigned long long array_blocks
;
10380 memset(migr_rec
, 0, sizeof(struct migr_record
));
10381 migr_rec
->family_num
= __cpu_to_le32(super
->anchor
->family_num
);
10383 /* only ascending reshape supported now */
10384 migr_rec
->ascending_migr
= __cpu_to_le32(1);
10386 migr_rec
->dest_depth_per_unit
= GEN_MIGR_AREA_SIZE
/
10387 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
10388 migr_rec
->dest_depth_per_unit
*=
10389 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
10390 new_data_disks
= imsm_num_data_members(dev
, MAP_0
);
10391 migr_rec
->blocks_per_unit
=
10392 __cpu_to_le32(migr_rec
->dest_depth_per_unit
* new_data_disks
);
10393 migr_rec
->dest_depth_per_unit
=
10394 __cpu_to_le32(migr_rec
->dest_depth_per_unit
);
10395 array_blocks
= info
->component_size
* new_data_disks
;
10397 array_blocks
/ __le32_to_cpu(migr_rec
->blocks_per_unit
);
10399 if (array_blocks
% __le32_to_cpu(migr_rec
->blocks_per_unit
))
10401 migr_rec
->num_migr_units
= __cpu_to_le32(num_migr_units
);
10403 migr_rec
->post_migr_vol_cap
= dev
->size_low
;
10404 migr_rec
->post_migr_vol_cap_hi
= dev
->size_high
;
10406 /* Find the smallest dev */
10407 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
10408 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
10409 fd
= dev_open(nm
, O_RDONLY
);
10412 get_dev_size(fd
, NULL
, &dsize
);
10413 dev_sectors
= dsize
/ 512;
10414 if (dev_sectors
< min_dev_sectors
)
10415 min_dev_sectors
= dev_sectors
;
10418 migr_rec
->ckpt_area_pba
= __cpu_to_le32(min_dev_sectors
-
10419 RAID_DISK_RESERVED_BLOCKS_IMSM_HI
);
10421 write_imsm_migr_rec(st
);
10426 /*******************************************************************************
10427 * Function: save_backup_imsm
10428 * Description: Function saves critical data stripes to Migration Copy Area
10429 * and updates the current migration unit status.
10430 * Use restore_stripes() to form a destination stripe,
10431 * and to write it to the Copy Area.
10433 * st : supertype information
10434 * dev : imsm device that backup is saved for
10435 * info : general array info
10436 * buf : input buffer
10437 * length : length of data to backup (blocks_per_unit)
10441 ******************************************************************************/
10442 int save_backup_imsm(struct supertype
*st
,
10443 struct imsm_dev
*dev
,
10444 struct mdinfo
*info
,
10449 struct intel_super
*super
= st
->sb
;
10450 unsigned long long *target_offsets
;
10453 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
10454 int new_disks
= map_dest
->num_members
;
10455 int dest_layout
= 0;
10457 unsigned long long start
;
10458 int data_disks
= imsm_num_data_members(dev
, MAP_0
);
10460 targets
= xmalloc(new_disks
* sizeof(int));
10462 for (i
= 0; i
< new_disks
; i
++)
10465 target_offsets
= xcalloc(new_disks
, sizeof(unsigned long long));
10467 start
= info
->reshape_progress
* 512;
10468 for (i
= 0; i
< new_disks
; i
++) {
10469 target_offsets
[i
] = (unsigned long long)
10470 __le32_to_cpu(super
->migr_rec
->ckpt_area_pba
) * 512;
10471 /* move back copy area adderss, it will be moved forward
10472 * in restore_stripes() using start input variable
10474 target_offsets
[i
] -= start
/data_disks
;
10477 if (open_backup_targets(info
, new_disks
, targets
,
10481 dest_layout
= imsm_level_to_layout(map_dest
->raid_level
);
10482 dest_chunk
= __le16_to_cpu(map_dest
->blocks_per_strip
) * 512;
10484 if (restore_stripes(targets
, /* list of dest devices */
10485 target_offsets
, /* migration record offsets */
10488 map_dest
->raid_level
,
10490 -1, /* source backup file descriptor */
10491 0, /* input buf offset
10492 * always 0 buf is already offseted */
10496 pr_err("Error restoring stripes\n");
10504 close_targets(targets
, new_disks
);
10507 free(target_offsets
);
10512 /*******************************************************************************
10513 * Function: save_checkpoint_imsm
10514 * Description: Function called for current unit status update
10515 * in the migration record. It writes it to disk.
10517 * super : imsm internal array info
10518 * info : general array info
10522 * 2: failure, means no valid migration record
10523 * / no general migration in progress /
10524 ******************************************************************************/
10525 int save_checkpoint_imsm(struct supertype
*st
, struct mdinfo
*info
, int state
)
10527 struct intel_super
*super
= st
->sb
;
10528 unsigned long long blocks_per_unit
;
10529 unsigned long long curr_migr_unit
;
10531 if (load_imsm_migr_rec(super
, info
) != 0) {
10532 dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n");
10536 blocks_per_unit
= __le32_to_cpu(super
->migr_rec
->blocks_per_unit
);
10537 if (blocks_per_unit
== 0) {
10538 dprintf("imsm: no migration in progress.\n");
10541 curr_migr_unit
= info
->reshape_progress
/ blocks_per_unit
;
10542 /* check if array is alligned to copy area
10543 * if it is not alligned, add one to current migration unit value
10544 * this can happend on array reshape finish only
10546 if (info
->reshape_progress
% blocks_per_unit
)
10549 super
->migr_rec
->curr_migr_unit
=
10550 __cpu_to_le32(curr_migr_unit
);
10551 super
->migr_rec
->rec_status
= __cpu_to_le32(state
);
10552 super
->migr_rec
->dest_1st_member_lba
=
10553 __cpu_to_le32(curr_migr_unit
*
10554 __le32_to_cpu(super
->migr_rec
->dest_depth_per_unit
));
10555 if (write_imsm_migr_rec(st
) < 0) {
10556 dprintf("imsm: Cannot write migration record outside backup area\n");
10563 /*******************************************************************************
10564 * Function: recover_backup_imsm
10565 * Description: Function recovers critical data from the Migration Copy Area
10566 * while assembling an array.
10568 * super : imsm internal array info
10569 * info : general array info
10571 * 0 : success (or there is no data to recover)
10573 ******************************************************************************/
10574 int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
)
10576 struct intel_super
*super
= st
->sb
;
10577 struct migr_record
*migr_rec
= super
->migr_rec
;
10578 struct imsm_map
*map_dest
;
10579 struct intel_dev
*id
= NULL
;
10580 unsigned long long read_offset
;
10581 unsigned long long write_offset
;
10583 int *targets
= NULL
;
10584 int new_disks
, i
, err
;
10587 unsigned int sector_size
= super
->sector_size
;
10588 unsigned long curr_migr_unit
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
10589 unsigned long num_migr_units
= __le32_to_cpu(migr_rec
->num_migr_units
);
10591 int skipped_disks
= 0;
10593 err
= sysfs_get_str(info
, NULL
, "array_state", (char *)buffer
, 20);
10597 /* recover data only during assemblation */
10598 if (strncmp(buffer
, "inactive", 8) != 0)
10600 /* no data to recover */
10601 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
10603 if (curr_migr_unit
>= num_migr_units
)
10606 /* find device during reshape */
10607 for (id
= super
->devlist
; id
; id
= id
->next
)
10608 if (is_gen_migration(id
->dev
))
10613 map_dest
= get_imsm_map(id
->dev
, MAP_0
);
10614 new_disks
= map_dest
->num_members
;
10616 read_offset
= (unsigned long long)
10617 __le32_to_cpu(migr_rec
->ckpt_area_pba
) * 512;
10619 write_offset
= ((unsigned long long)
10620 __le32_to_cpu(migr_rec
->dest_1st_member_lba
) +
10621 pba_of_lba0(map_dest
)) * 512;
10623 unit_len
= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
10624 if (posix_memalign((void **)&buf
, sector_size
, unit_len
) != 0)
10626 targets
= xcalloc(new_disks
, sizeof(int));
10628 if (open_backup_targets(info
, new_disks
, targets
, super
, id
->dev
)) {
10629 pr_err("Cannot open some devices belonging to array.\n");
10633 for (i
= 0; i
< new_disks
; i
++) {
10634 if (targets
[i
] < 0) {
10638 if (lseek64(targets
[i
], read_offset
, SEEK_SET
) < 0) {
10639 pr_err("Cannot seek to block: %s\n",
10644 if ((unsigned)read(targets
[i
], buf
, unit_len
) != unit_len
) {
10645 pr_err("Cannot read copy area block: %s\n",
10650 if (lseek64(targets
[i
], write_offset
, SEEK_SET
) < 0) {
10651 pr_err("Cannot seek to block: %s\n",
10656 if ((unsigned)write(targets
[i
], buf
, unit_len
) != unit_len
) {
10657 pr_err("Cannot restore block: %s\n",
10664 if (skipped_disks
> imsm_get_allowed_degradation(info
->new_level
,
10668 pr_err("Cannot restore data from backup. Too many failed disks\n");
10672 if (save_checkpoint_imsm(st
, info
, UNIT_SRC_NORMAL
)) {
10673 /* ignore error == 2, this can mean end of reshape here
10675 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n");
10681 for (i
= 0; i
< new_disks
; i
++)
10690 static char disk_by_path
[] = "/dev/disk/by-path/";
10692 static const char *imsm_get_disk_controller_domain(const char *path
)
10694 char disk_path
[PATH_MAX
];
10698 strcpy(disk_path
, disk_by_path
);
10699 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
10700 if (stat(disk_path
, &st
) == 0) {
10701 struct sys_dev
* hba
;
10704 path
= devt_to_devpath(st
.st_rdev
);
10707 hba
= find_disk_attached_hba(-1, path
);
10708 if (hba
&& hba
->type
== SYS_DEV_SAS
)
10710 else if (hba
&& hba
->type
== SYS_DEV_SATA
)
10714 dprintf("path: %s hba: %s attached: %s\n",
10715 path
, (hba
) ? hba
->path
: "NULL", drv
);
10721 static char *imsm_find_array_devnm_by_subdev(int subdev
, char *container
)
10723 static char devnm
[32];
10724 char subdev_name
[20];
10725 struct mdstat_ent
*mdstat
;
10727 sprintf(subdev_name
, "%d", subdev
);
10728 mdstat
= mdstat_by_subdev(subdev_name
, container
);
10732 strcpy(devnm
, mdstat
->devnm
);
10733 free_mdstat(mdstat
);
10737 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
10738 struct geo_params
*geo
,
10739 int *old_raid_disks
,
10742 /* currently we only support increasing the number of devices
10743 * for a container. This increases the number of device for each
10744 * member array. They must all be RAID0 or RAID5.
10747 struct mdinfo
*info
, *member
;
10748 int devices_that_can_grow
= 0;
10750 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st
->devnm
);
10752 if (geo
->size
> 0 ||
10753 geo
->level
!= UnSet
||
10754 geo
->layout
!= UnSet
||
10755 geo
->chunksize
!= 0 ||
10756 geo
->raid_disks
== UnSet
) {
10757 dprintf("imsm: Container operation is allowed for raid disks number change only.\n");
10761 if (direction
== ROLLBACK_METADATA_CHANGES
) {
10762 dprintf("imsm: Metadata changes rollback is not supported for container operation.\n");
10766 info
= container_content_imsm(st
, NULL
);
10767 for (member
= info
; member
; member
= member
->next
) {
10770 dprintf("imsm: checking device_num: %i\n",
10771 member
->container_member
);
10773 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
10774 /* we work on container for Online Capacity Expansion
10775 * only so raid_disks has to grow
10777 dprintf("imsm: for container operation raid disks increase is required\n");
10781 if (info
->array
.level
!= 0 && info
->array
.level
!= 5) {
10782 /* we cannot use this container with other raid level
10784 dprintf("imsm: for container operation wrong raid level (%i) detected\n",
10785 info
->array
.level
);
10788 /* check for platform support
10789 * for this raid level configuration
10791 struct intel_super
*super
= st
->sb
;
10792 if (!is_raid_level_supported(super
->orom
,
10793 member
->array
.level
,
10794 geo
->raid_disks
)) {
10795 dprintf("platform does not support raid%d with %d disk%s\n",
10798 geo
->raid_disks
> 1 ? "s" : "");
10801 /* check if component size is aligned to chunk size
10803 if (info
->component_size
%
10804 (info
->array
.chunk_size
/512)) {
10805 dprintf("Component size is not aligned to chunk size\n");
10810 if (*old_raid_disks
&&
10811 info
->array
.raid_disks
!= *old_raid_disks
)
10813 *old_raid_disks
= info
->array
.raid_disks
;
10815 /* All raid5 and raid0 volumes in container
10816 * have to be ready for Online Capacity Expansion
10817 * so they need to be assembled. We have already
10818 * checked that no recovery etc is happening.
10820 result
= imsm_find_array_devnm_by_subdev(member
->container_member
,
10821 st
->container_devnm
);
10822 if (result
== NULL
) {
10823 dprintf("imsm: cannot find array\n");
10826 devices_that_can_grow
++;
10829 if (!member
&& devices_that_can_grow
)
10833 dprintf("Container operation allowed\n");
10835 dprintf("Error: %i\n", ret_val
);
10840 /* Function: get_spares_for_grow
10841 * Description: Allocates memory and creates list of spare devices
10842 * avaliable in container. Checks if spare drive size is acceptable.
10843 * Parameters: Pointer to the supertype structure
10844 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
10847 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
10849 unsigned long long min_size
= min_acceptable_spare_size_imsm(st
);
10850 return container_choose_spares(st
, min_size
, NULL
, NULL
, NULL
, 0);
10853 /******************************************************************************
10854 * function: imsm_create_metadata_update_for_reshape
10855 * Function creates update for whole IMSM container.
10857 ******************************************************************************/
10858 static int imsm_create_metadata_update_for_reshape(
10859 struct supertype
*st
,
10860 struct geo_params
*geo
,
10861 int old_raid_disks
,
10862 struct imsm_update_reshape
**updatep
)
10864 struct intel_super
*super
= st
->sb
;
10865 struct imsm_super
*mpb
= super
->anchor
;
10866 int update_memory_size
;
10867 struct imsm_update_reshape
*u
;
10868 struct mdinfo
*spares
;
10871 struct mdinfo
*dev
;
10873 dprintf("(enter) raid_disks = %i\n", geo
->raid_disks
);
10875 delta_disks
= geo
->raid_disks
- old_raid_disks
;
10877 /* size of all update data without anchor */
10878 update_memory_size
= sizeof(struct imsm_update_reshape
);
10880 /* now add space for spare disks that we need to add. */
10881 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
10883 u
= xcalloc(1, update_memory_size
);
10884 u
->type
= update_reshape_container_disks
;
10885 u
->old_raid_disks
= old_raid_disks
;
10886 u
->new_raid_disks
= geo
->raid_disks
;
10888 /* now get spare disks list
10890 spares
= get_spares_for_grow(st
);
10893 || delta_disks
> spares
->array
.spare_disks
) {
10894 pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo
->dev_name
);
10899 /* we have got spares
10900 * update disk list in imsm_disk list table in anchor
10902 dprintf("imsm: %i spares are available.\n\n",
10903 spares
->array
.spare_disks
);
10905 dev
= spares
->devs
;
10906 for (i
= 0; i
< delta_disks
; i
++) {
10911 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
10913 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
10914 dl
->index
= mpb
->num_disks
;
10922 sysfs_free(spares
);
10924 dprintf("imsm: reshape update preparation :");
10925 if (i
== delta_disks
) {
10926 dprintf_cont(" OK\n");
10928 return update_memory_size
;
10931 dprintf_cont(" Error\n");
10936 /******************************************************************************
10937 * function: imsm_create_metadata_update_for_size_change()
10938 * Creates update for IMSM array for array size change.
10940 ******************************************************************************/
10941 static int imsm_create_metadata_update_for_size_change(
10942 struct supertype
*st
,
10943 struct geo_params
*geo
,
10944 struct imsm_update_size_change
**updatep
)
10946 struct intel_super
*super
= st
->sb
;
10947 int update_memory_size
;
10948 struct imsm_update_size_change
*u
;
10950 dprintf("(enter) New size = %llu\n", geo
->size
);
10952 /* size of all update data without anchor */
10953 update_memory_size
= sizeof(struct imsm_update_size_change
);
10955 u
= xcalloc(1, update_memory_size
);
10956 u
->type
= update_size_change
;
10957 u
->subdev
= super
->current_vol
;
10958 u
->new_size
= geo
->size
;
10960 dprintf("imsm: reshape update preparation : OK\n");
10963 return update_memory_size
;
10966 /******************************************************************************
10967 * function: imsm_create_metadata_update_for_migration()
10968 * Creates update for IMSM array.
10970 ******************************************************************************/
10971 static int imsm_create_metadata_update_for_migration(
10972 struct supertype
*st
,
10973 struct geo_params
*geo
,
10974 struct imsm_update_reshape_migration
**updatep
)
10976 struct intel_super
*super
= st
->sb
;
10977 int update_memory_size
;
10978 struct imsm_update_reshape_migration
*u
;
10979 struct imsm_dev
*dev
;
10980 int previous_level
= -1;
10982 dprintf("(enter) New Level = %i\n", geo
->level
);
10984 /* size of all update data without anchor */
10985 update_memory_size
= sizeof(struct imsm_update_reshape_migration
);
10987 u
= xcalloc(1, update_memory_size
);
10988 u
->type
= update_reshape_migration
;
10989 u
->subdev
= super
->current_vol
;
10990 u
->new_level
= geo
->level
;
10991 u
->new_layout
= geo
->layout
;
10992 u
->new_raid_disks
= u
->old_raid_disks
= geo
->raid_disks
;
10993 u
->new_disks
[0] = -1;
10994 u
->new_chunksize
= -1;
10996 dev
= get_imsm_dev(super
, u
->subdev
);
10998 struct imsm_map
*map
;
11000 map
= get_imsm_map(dev
, MAP_0
);
11002 int current_chunk_size
=
11003 __le16_to_cpu(map
->blocks_per_strip
) / 2;
11005 if (geo
->chunksize
!= current_chunk_size
) {
11006 u
->new_chunksize
= geo
->chunksize
/ 1024;
11007 dprintf("imsm: chunk size change from %i to %i\n",
11008 current_chunk_size
, u
->new_chunksize
);
11010 previous_level
= map
->raid_level
;
11013 if (geo
->level
== 5 && previous_level
== 0) {
11014 struct mdinfo
*spares
= NULL
;
11016 u
->new_raid_disks
++;
11017 spares
= get_spares_for_grow(st
);
11018 if (spares
== NULL
|| spares
->array
.spare_disks
< 1) {
11020 sysfs_free(spares
);
11021 update_memory_size
= 0;
11022 pr_err("cannot get spare device for requested migration\n");
11025 sysfs_free(spares
);
11027 dprintf("imsm: reshape update preparation : OK\n");
11030 return update_memory_size
;
11033 static void imsm_update_metadata_locally(struct supertype
*st
,
11034 void *buf
, int len
)
11036 struct metadata_update mu
;
11041 mu
.space_list
= NULL
;
11043 if (imsm_prepare_update(st
, &mu
))
11044 imsm_process_update(st
, &mu
);
11046 while (mu
.space_list
) {
11047 void **space
= mu
.space_list
;
11048 mu
.space_list
= *space
;
11053 /***************************************************************************
11054 * Function: imsm_analyze_change
11055 * Description: Function analyze change for single volume
11056 * and validate if transition is supported
11057 * Parameters: Geometry parameters, supertype structure,
11058 * metadata change direction (apply/rollback)
11059 * Returns: Operation type code on success, -1 if fail
11060 ****************************************************************************/
11061 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
11062 struct geo_params
*geo
,
11065 struct mdinfo info
;
11067 int check_devs
= 0;
11069 /* number of added/removed disks in operation result */
11070 int devNumChange
= 0;
11071 /* imsm compatible layout value for array geometry verification */
11072 int imsm_layout
= -1;
11074 struct imsm_dev
*dev
;
11075 struct intel_super
*super
;
11076 unsigned long long current_size
;
11077 unsigned long long free_size
;
11078 unsigned long long max_size
;
11081 getinfo_super_imsm_volume(st
, &info
, NULL
);
11082 if (geo
->level
!= info
.array
.level
&& geo
->level
>= 0 &&
11083 geo
->level
!= UnSet
) {
11084 switch (info
.array
.level
) {
11086 if (geo
->level
== 5) {
11087 change
= CH_MIGRATION
;
11088 if (geo
->layout
!= ALGORITHM_LEFT_ASYMMETRIC
) {
11089 pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n");
11091 goto analyse_change_exit
;
11093 imsm_layout
= geo
->layout
;
11095 devNumChange
= 1; /* parity disk added */
11096 } else if (geo
->level
== 10) {
11097 change
= CH_TAKEOVER
;
11099 devNumChange
= 2; /* two mirrors added */
11100 imsm_layout
= 0x102; /* imsm supported layout */
11105 if (geo
->level
== 0) {
11106 change
= CH_TAKEOVER
;
11108 devNumChange
= -(geo
->raid_disks
/2);
11109 imsm_layout
= 0; /* imsm raid0 layout */
11113 if (change
== -1) {
11114 pr_err("Error. Level Migration from %d to %d not supported!\n",
11115 info
.array
.level
, geo
->level
);
11116 goto analyse_change_exit
;
11119 geo
->level
= info
.array
.level
;
11121 if (geo
->layout
!= info
.array
.layout
&&
11122 (geo
->layout
!= UnSet
&& geo
->layout
!= -1)) {
11123 change
= CH_MIGRATION
;
11124 if (info
.array
.layout
== 0 && info
.array
.level
== 5 &&
11125 geo
->layout
== 5) {
11126 /* reshape 5 -> 4 */
11127 } else if (info
.array
.layout
== 5 && info
.array
.level
== 5 &&
11128 geo
->layout
== 0) {
11129 /* reshape 4 -> 5 */
11133 pr_err("Error. Layout Migration from %d to %d not supported!\n",
11134 info
.array
.layout
, geo
->layout
);
11136 goto analyse_change_exit
;
11139 geo
->layout
= info
.array
.layout
;
11140 if (imsm_layout
== -1)
11141 imsm_layout
= info
.array
.layout
;
11144 if (geo
->chunksize
> 0 && geo
->chunksize
!= UnSet
&&
11145 geo
->chunksize
!= info
.array
.chunk_size
) {
11146 if (info
.array
.level
== 10) {
11147 pr_err("Error. Chunk size change for RAID 10 is not supported.\n");
11149 goto analyse_change_exit
;
11150 } else if (info
.component_size
% (geo
->chunksize
/512)) {
11151 pr_err("New chunk size (%dK) does not evenly divide device size (%lluk). Aborting...\n",
11152 geo
->chunksize
/1024, info
.component_size
/2);
11154 goto analyse_change_exit
;
11156 change
= CH_MIGRATION
;
11158 geo
->chunksize
= info
.array
.chunk_size
;
11161 chunk
= geo
->chunksize
/ 1024;
11164 dev
= get_imsm_dev(super
, super
->current_vol
);
11165 data_disks
= imsm_num_data_members(dev
, MAP_0
);
11166 /* compute current size per disk member
11168 current_size
= info
.custom_array_size
/ data_disks
;
11170 if (geo
->size
> 0 && geo
->size
!= MAX_SIZE
) {
11171 /* align component size
11173 geo
->size
= imsm_component_size_aligment_check(
11174 get_imsm_raid_level(dev
->vol
.map
),
11175 chunk
* 1024, super
->sector_size
,
11177 if (geo
->size
== 0) {
11178 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is 0).\n",
11180 goto analyse_change_exit
;
11184 if (current_size
!= geo
->size
&& geo
->size
> 0) {
11185 if (change
!= -1) {
11186 pr_err("Error. Size change should be the only one at a time.\n");
11188 goto analyse_change_exit
;
11190 if ((super
->current_vol
+ 1) != super
->anchor
->num_raid_devs
) {
11191 pr_err("Error. The last volume in container can be expanded only (%i/%s).\n",
11192 super
->current_vol
, st
->devnm
);
11193 goto analyse_change_exit
;
11195 /* check the maximum available size
11197 rv
= imsm_get_free_size(st
, dev
->vol
.map
->num_members
,
11198 0, chunk
, &free_size
);
11200 /* Cannot find maximum available space
11204 max_size
= free_size
+ current_size
;
11205 /* align component size
11207 max_size
= imsm_component_size_aligment_check(
11208 get_imsm_raid_level(dev
->vol
.map
),
11209 chunk
* 1024, super
->sector_size
,
11212 if (geo
->size
== MAX_SIZE
) {
11213 /* requested size change to the maximum available size
11215 if (max_size
== 0) {
11216 pr_err("Error. Cannot find maximum available space.\n");
11218 goto analyse_change_exit
;
11220 geo
->size
= max_size
;
11223 if (direction
== ROLLBACK_METADATA_CHANGES
) {
11224 /* accept size for rollback only
11227 /* round size due to metadata compatibility
11229 geo
->size
= (geo
->size
>> SECT_PER_MB_SHIFT
)
11230 << SECT_PER_MB_SHIFT
;
11231 dprintf("Prepare update for size change to %llu\n",
11233 if (current_size
>= geo
->size
) {
11234 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is %llu).\n",
11235 current_size
, geo
->size
);
11236 goto analyse_change_exit
;
11238 if (max_size
&& geo
->size
> max_size
) {
11239 pr_err("Error. Requested size is larger than maximum available size (maximum available size is %llu, requested size /rounded/ is %llu).\n",
11240 max_size
, geo
->size
);
11241 goto analyse_change_exit
;
11244 geo
->size
*= data_disks
;
11245 geo
->raid_disks
= dev
->vol
.map
->num_members
;
11246 change
= CH_ARRAY_SIZE
;
11248 if (!validate_geometry_imsm(st
,
11251 geo
->raid_disks
+ devNumChange
,
11253 geo
->size
, INVALID_SECTORS
,
11254 0, 0, info
.consistency_policy
, 1))
11258 struct intel_super
*super
= st
->sb
;
11259 struct imsm_super
*mpb
= super
->anchor
;
11261 if (mpb
->num_raid_devs
> 1) {
11262 pr_err("Error. Cannot perform operation on %s- for this operation it MUST be single array in container\n",
11268 analyse_change_exit
:
11269 if (direction
== ROLLBACK_METADATA_CHANGES
&&
11270 (change
== CH_MIGRATION
|| change
== CH_TAKEOVER
)) {
11271 dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n");
11277 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
11279 struct intel_super
*super
= st
->sb
;
11280 struct imsm_update_takeover
*u
;
11282 u
= xmalloc(sizeof(struct imsm_update_takeover
));
11284 u
->type
= update_takeover
;
11285 u
->subarray
= super
->current_vol
;
11287 /* 10->0 transition */
11288 if (geo
->level
== 0)
11289 u
->direction
= R10_TO_R0
;
11291 /* 0->10 transition */
11292 if (geo
->level
== 10)
11293 u
->direction
= R0_TO_R10
;
11295 /* update metadata locally */
11296 imsm_update_metadata_locally(st
, u
,
11297 sizeof(struct imsm_update_takeover
));
11298 /* and possibly remotely */
11299 if (st
->update_tail
)
11300 append_metadata_update(st
, u
,
11301 sizeof(struct imsm_update_takeover
));
11308 static int imsm_reshape_super(struct supertype
*st
, unsigned long long size
,
11310 int layout
, int chunksize
, int raid_disks
,
11311 int delta_disks
, char *backup
, char *dev
,
11312 int direction
, int verbose
)
11315 struct geo_params geo
;
11317 dprintf("(enter)\n");
11319 memset(&geo
, 0, sizeof(struct geo_params
));
11321 geo
.dev_name
= dev
;
11322 strcpy(geo
.devnm
, st
->devnm
);
11325 geo
.layout
= layout
;
11326 geo
.chunksize
= chunksize
;
11327 geo
.raid_disks
= raid_disks
;
11328 if (delta_disks
!= UnSet
)
11329 geo
.raid_disks
+= delta_disks
;
11331 dprintf("for level : %i\n", geo
.level
);
11332 dprintf("for raid_disks : %i\n", geo
.raid_disks
);
11334 if (experimental() == 0)
11337 if (strcmp(st
->container_devnm
, st
->devnm
) == 0) {
11338 /* On container level we can only increase number of devices. */
11339 dprintf("imsm: info: Container operation\n");
11340 int old_raid_disks
= 0;
11342 if (imsm_reshape_is_allowed_on_container(
11343 st
, &geo
, &old_raid_disks
, direction
)) {
11344 struct imsm_update_reshape
*u
= NULL
;
11347 len
= imsm_create_metadata_update_for_reshape(
11348 st
, &geo
, old_raid_disks
, &u
);
11351 dprintf("imsm: Cannot prepare update\n");
11352 goto exit_imsm_reshape_super
;
11356 /* update metadata locally */
11357 imsm_update_metadata_locally(st
, u
, len
);
11358 /* and possibly remotely */
11359 if (st
->update_tail
)
11360 append_metadata_update(st
, u
, len
);
11365 pr_err("(imsm) Operation is not allowed on this container\n");
11368 /* On volume level we support following operations
11369 * - takeover: raid10 -> raid0; raid0 -> raid10
11370 * - chunk size migration
11371 * - migration: raid5 -> raid0; raid0 -> raid5
11373 struct intel_super
*super
= st
->sb
;
11374 struct intel_dev
*dev
= super
->devlist
;
11376 dprintf("imsm: info: Volume operation\n");
11377 /* find requested device */
11380 imsm_find_array_devnm_by_subdev(
11381 dev
->index
, st
->container_devnm
);
11382 if (devnm
&& strcmp(devnm
, geo
.devnm
) == 0)
11387 pr_err("Cannot find %s (%s) subarray\n",
11388 geo
.dev_name
, geo
.devnm
);
11389 goto exit_imsm_reshape_super
;
11391 super
->current_vol
= dev
->index
;
11392 change
= imsm_analyze_change(st
, &geo
, direction
);
11395 ret_val
= imsm_takeover(st
, &geo
);
11397 case CH_MIGRATION
: {
11398 struct imsm_update_reshape_migration
*u
= NULL
;
11400 imsm_create_metadata_update_for_migration(
11403 dprintf("imsm: Cannot prepare update\n");
11407 /* update metadata locally */
11408 imsm_update_metadata_locally(st
, u
, len
);
11409 /* and possibly remotely */
11410 if (st
->update_tail
)
11411 append_metadata_update(st
, u
, len
);
11416 case CH_ARRAY_SIZE
: {
11417 struct imsm_update_size_change
*u
= NULL
;
11419 imsm_create_metadata_update_for_size_change(
11422 dprintf("imsm: Cannot prepare update\n");
11426 /* update metadata locally */
11427 imsm_update_metadata_locally(st
, u
, len
);
11428 /* and possibly remotely */
11429 if (st
->update_tail
)
11430 append_metadata_update(st
, u
, len
);
11440 exit_imsm_reshape_super
:
11441 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
11445 #define COMPLETED_OK 0
11446 #define COMPLETED_NONE 1
11447 #define COMPLETED_DELAYED 2
11449 static int read_completed(int fd
, unsigned long long *val
)
11454 ret
= sysfs_fd_get_str(fd
, buf
, 50);
11458 ret
= COMPLETED_OK
;
11459 if (strncmp(buf
, "none", 4) == 0) {
11460 ret
= COMPLETED_NONE
;
11461 } else if (strncmp(buf
, "delayed", 7) == 0) {
11462 ret
= COMPLETED_DELAYED
;
11465 *val
= strtoull(buf
, &ep
, 0);
11466 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))
11472 /*******************************************************************************
11473 * Function: wait_for_reshape_imsm
11474 * Description: Function writes new sync_max value and waits until
11475 * reshape process reach new position
11477 * sra : general array info
11478 * ndata : number of disks in new array's layout
11481 * 1 : there is no reshape in progress,
11483 ******************************************************************************/
11484 int wait_for_reshape_imsm(struct mdinfo
*sra
, int ndata
)
11486 int fd
= sysfs_get_fd(sra
, NULL
, "sync_completed");
11488 unsigned long long completed
;
11489 /* to_complete : new sync_max position */
11490 unsigned long long to_complete
= sra
->reshape_progress
;
11491 unsigned long long position_to_set
= to_complete
/ ndata
;
11494 dprintf("cannot open reshape_position\n");
11499 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
11501 dprintf("cannot read reshape_position (no reshape in progres)\n");
11510 if (completed
> position_to_set
) {
11511 dprintf("wrong next position to set %llu (%llu)\n",
11512 to_complete
, position_to_set
);
11516 dprintf("Position set: %llu\n", position_to_set
);
11517 if (sysfs_set_num(sra
, NULL
, "sync_max",
11518 position_to_set
) != 0) {
11519 dprintf("cannot set reshape position to %llu\n",
11528 int timeout
= 3000;
11530 sysfs_wait(fd
, &timeout
);
11531 if (sysfs_get_str(sra
, NULL
, "sync_action",
11533 strncmp(action
, "reshape", 7) != 0) {
11534 if (strncmp(action
, "idle", 4) == 0)
11540 rc
= read_completed(fd
, &completed
);
11542 dprintf("cannot read reshape_position (in loop)\n");
11545 } else if (rc
== COMPLETED_NONE
)
11547 } while (completed
< position_to_set
);
11553 /*******************************************************************************
11554 * Function: check_degradation_change
11555 * Description: Check that array hasn't become failed.
11557 * info : for sysfs access
11558 * sources : source disks descriptors
11559 * degraded: previous degradation level
11561 * degradation level
11562 ******************************************************************************/
11563 int check_degradation_change(struct mdinfo
*info
,
11567 unsigned long long new_degraded
;
11570 rv
= sysfs_get_ll(info
, NULL
, "degraded", &new_degraded
);
11571 if (rv
== -1 || (new_degraded
!= (unsigned long long)degraded
)) {
11572 /* check each device to ensure it is still working */
11575 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
11576 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
11578 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
11581 if (sysfs_get_str(info
,
11582 sd
, "state", sbuf
, sizeof(sbuf
)) < 0 ||
11583 strstr(sbuf
, "faulty") ||
11584 strstr(sbuf
, "in_sync") == NULL
) {
11585 /* this device is dead */
11586 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
11587 if (sd
->disk
.raid_disk
>= 0 &&
11588 sources
[sd
->disk
.raid_disk
] >= 0) {
11590 sd
->disk
.raid_disk
]);
11591 sources
[sd
->disk
.raid_disk
] =
11600 return new_degraded
;
11603 /*******************************************************************************
11604 * Function: imsm_manage_reshape
11605 * Description: Function finds array under reshape and it manages reshape
11606 * process. It creates stripes backups (if required) and sets
11609 * afd : Backup handle (nattive) - not used
11610 * sra : general array info
11611 * reshape : reshape parameters - not used
11612 * st : supertype structure
11613 * blocks : size of critical section [blocks]
11614 * fds : table of source device descriptor
11615 * offsets : start of array (offest per devices)
11617 * destfd : table of destination device descriptor
11618 * destoffsets : table of destination offsets (per device)
11620 * 1 : success, reshape is done
11622 ******************************************************************************/
11623 static int imsm_manage_reshape(
11624 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
11625 struct supertype
*st
, unsigned long backup_blocks
,
11626 int *fds
, unsigned long long *offsets
,
11627 int dests
, int *destfd
, unsigned long long *destoffsets
)
11630 struct intel_super
*super
= st
->sb
;
11631 struct intel_dev
*dv
;
11632 unsigned int sector_size
= super
->sector_size
;
11633 struct imsm_dev
*dev
= NULL
;
11634 struct imsm_map
*map_src
;
11635 int migr_vol_qan
= 0;
11636 int ndata
, odata
; /* [bytes] */
11637 int chunk
; /* [bytes] */
11638 struct migr_record
*migr_rec
;
11640 unsigned int buf_size
; /* [bytes] */
11641 unsigned long long max_position
; /* array size [bytes] */
11642 unsigned long long next_step
; /* [blocks]/[bytes] */
11643 unsigned long long old_data_stripe_length
;
11644 unsigned long long start_src
; /* [bytes] */
11645 unsigned long long start
; /* [bytes] */
11646 unsigned long long start_buf_shift
; /* [bytes] */
11648 int source_layout
= 0;
11653 if (!fds
|| !offsets
)
11656 /* Find volume during the reshape */
11657 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
11658 if (dv
->dev
->vol
.migr_type
== MIGR_GEN_MIGR
11659 && dv
->dev
->vol
.migr_state
== 1) {
11664 /* Only one volume can migrate at the same time */
11665 if (migr_vol_qan
!= 1) {
11666 pr_err("%s", migr_vol_qan
?
11667 "Number of migrating volumes greater than 1\n" :
11668 "There is no volume during migrationg\n");
11672 map_src
= get_imsm_map(dev
, MAP_1
);
11673 if (map_src
== NULL
)
11676 ndata
= imsm_num_data_members(dev
, MAP_0
);
11677 odata
= imsm_num_data_members(dev
, MAP_1
);
11679 chunk
= __le16_to_cpu(map_src
->blocks_per_strip
) * 512;
11680 old_data_stripe_length
= odata
* chunk
;
11682 migr_rec
= super
->migr_rec
;
11684 /* initialize migration record for start condition */
11685 if (sra
->reshape_progress
== 0)
11686 init_migr_record_imsm(st
, dev
, sra
);
11688 if (__le32_to_cpu(migr_rec
->rec_status
) != UNIT_SRC_NORMAL
) {
11689 dprintf("imsm: cannot restart migration when data are present in copy area.\n");
11692 /* Save checkpoint to update migration record for current
11693 * reshape position (in md). It can be farther than current
11694 * reshape position in metadata.
11696 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
11697 /* ignore error == 2, this can mean end of reshape here
11699 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n");
11704 /* size for data */
11705 buf_size
= __le32_to_cpu(migr_rec
->blocks_per_unit
) * 512;
11706 /* extend buffer size for parity disk */
11707 buf_size
+= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
11708 /* add space for stripe aligment */
11709 buf_size
+= old_data_stripe_length
;
11710 if (posix_memalign((void **)&buf
, MAX_SECTOR_SIZE
, buf_size
)) {
11711 dprintf("imsm: Cannot allocate checkpoint buffer\n");
11715 max_position
= sra
->component_size
* ndata
;
11716 source_layout
= imsm_level_to_layout(map_src
->raid_level
);
11718 while (__le32_to_cpu(migr_rec
->curr_migr_unit
) <
11719 __le32_to_cpu(migr_rec
->num_migr_units
)) {
11720 /* current reshape position [blocks] */
11721 unsigned long long current_position
=
11722 __le32_to_cpu(migr_rec
->blocks_per_unit
)
11723 * __le32_to_cpu(migr_rec
->curr_migr_unit
);
11724 unsigned long long border
;
11726 /* Check that array hasn't become failed.
11728 degraded
= check_degradation_change(sra
, fds
, degraded
);
11729 if (degraded
> 1) {
11730 dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded
);
11734 next_step
= __le32_to_cpu(migr_rec
->blocks_per_unit
);
11736 if ((current_position
+ next_step
) > max_position
)
11737 next_step
= max_position
- current_position
;
11739 start
= current_position
* 512;
11741 /* align reading start to old geometry */
11742 start_buf_shift
= start
% old_data_stripe_length
;
11743 start_src
= start
- start_buf_shift
;
11745 border
= (start_src
/ odata
) - (start
/ ndata
);
11747 if (border
<= __le32_to_cpu(migr_rec
->dest_depth_per_unit
)) {
11748 /* save critical stripes to buf
11749 * start - start address of current unit
11750 * to backup [bytes]
11751 * start_src - start address of current unit
11752 * to backup alligned to source array
11755 unsigned long long next_step_filler
;
11756 unsigned long long copy_length
= next_step
* 512;
11758 /* allign copy area length to stripe in old geometry */
11759 next_step_filler
= ((copy_length
+ start_buf_shift
)
11760 % old_data_stripe_length
);
11761 if (next_step_filler
)
11762 next_step_filler
= (old_data_stripe_length
11763 - next_step_filler
);
11764 dprintf("save_stripes() parameters: start = %llu,\tstart_src = %llu,\tnext_step*512 = %llu,\tstart_in_buf_shift = %llu,\tnext_step_filler = %llu\n",
11765 start
, start_src
, copy_length
,
11766 start_buf_shift
, next_step_filler
);
11768 if (save_stripes(fds
, offsets
, map_src
->num_members
,
11769 chunk
, map_src
->raid_level
,
11770 source_layout
, 0, NULL
, start_src
,
11772 next_step_filler
+ start_buf_shift
,
11774 dprintf("imsm: Cannot save stripes to buffer\n");
11777 /* Convert data to destination format and store it
11778 * in backup general migration area
11780 if (save_backup_imsm(st
, dev
, sra
,
11781 buf
+ start_buf_shift
, copy_length
)) {
11782 dprintf("imsm: Cannot save stripes to target devices\n");
11785 if (save_checkpoint_imsm(st
, sra
,
11786 UNIT_SRC_IN_CP_AREA
)) {
11787 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n");
11791 /* set next step to use whole border area */
11792 border
/= next_step
;
11794 next_step
*= border
;
11796 /* When data backed up, checkpoint stored,
11797 * kick the kernel to reshape unit of data
11799 next_step
= next_step
+ sra
->reshape_progress
;
11800 /* limit next step to array max position */
11801 if (next_step
> max_position
)
11802 next_step
= max_position
;
11803 sysfs_set_num(sra
, NULL
, "suspend_lo", sra
->reshape_progress
);
11804 sysfs_set_num(sra
, NULL
, "suspend_hi", next_step
);
11805 sra
->reshape_progress
= next_step
;
11807 /* wait until reshape finish */
11808 if (wait_for_reshape_imsm(sra
, ndata
)) {
11809 dprintf("wait_for_reshape_imsm returned error!\n");
11815 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
11816 /* ignore error == 2, this can mean end of reshape here
11818 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n");
11824 /* clear migr_rec on disks after successful migration */
11827 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SECTORS
*sector_size
);
11828 for (d
= super
->disks
; d
; d
= d
->next
) {
11829 if (d
->index
< 0 || is_failed(&d
->disk
))
11831 unsigned long long dsize
;
11833 get_dev_size(d
->fd
, NULL
, &dsize
);
11834 if (lseek64(d
->fd
, dsize
- MIGR_REC_SECTOR_POSITION
*sector_size
,
11836 if ((unsigned int)write(d
->fd
, super
->migr_rec_buf
,
11837 MIGR_REC_BUF_SECTORS
*sector_size
) !=
11838 MIGR_REC_BUF_SECTORS
*sector_size
)
11839 perror("Write migr_rec failed");
11843 /* return '1' if done */
11847 /* See Grow.c: abort_reshape() for further explanation */
11848 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
11849 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
11850 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
11855 #endif /* MDASSEMBLE */
11857 struct superswitch super_imsm
= {
11859 .examine_super
= examine_super_imsm
,
11860 .brief_examine_super
= brief_examine_super_imsm
,
11861 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
11862 .export_examine_super
= export_examine_super_imsm
,
11863 .detail_super
= detail_super_imsm
,
11864 .brief_detail_super
= brief_detail_super_imsm
,
11865 .write_init_super
= write_init_super_imsm
,
11866 .validate_geometry
= validate_geometry_imsm
,
11867 .add_to_super
= add_to_super_imsm
,
11868 .remove_from_super
= remove_from_super_imsm
,
11869 .detail_platform
= detail_platform_imsm
,
11870 .export_detail_platform
= export_detail_platform_imsm
,
11871 .kill_subarray
= kill_subarray_imsm
,
11872 .update_subarray
= update_subarray_imsm
,
11873 .load_container
= load_container_imsm
,
11874 .default_geometry
= default_geometry_imsm
,
11875 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
11876 .reshape_super
= imsm_reshape_super
,
11877 .manage_reshape
= imsm_manage_reshape
,
11878 .recover_backup
= recover_backup_imsm
,
11879 .copy_metadata
= copy_metadata_imsm
,
11880 .examine_badblocks
= examine_badblocks_imsm
,
11882 .match_home
= match_home_imsm
,
11883 .uuid_from_super
= uuid_from_super_imsm
,
11884 .getinfo_super
= getinfo_super_imsm
,
11885 .getinfo_super_disks
= getinfo_super_disks_imsm
,
11886 .update_super
= update_super_imsm
,
11888 .avail_size
= avail_size_imsm
,
11889 .min_acceptable_spare_size
= min_acceptable_spare_size_imsm
,
11891 .compare_super
= compare_super_imsm
,
11893 .load_super
= load_super_imsm
,
11894 .init_super
= init_super_imsm
,
11895 .store_super
= store_super_imsm
,
11896 .free_super
= free_super_imsm
,
11897 .match_metadata_desc
= match_metadata_desc_imsm
,
11898 .container_content
= container_content_imsm
,
11899 .validate_container
= validate_container_imsm
,
11901 .write_init_ppl
= write_init_ppl_imsm
,
11902 .validate_ppl
= validate_ppl_imsm
,
11909 .open_new
= imsm_open_new
,
11910 .set_array_state
= imsm_set_array_state
,
11911 .set_disk
= imsm_set_disk
,
11912 .sync_metadata
= imsm_sync_metadata
,
11913 .activate_spare
= imsm_activate_spare
,
11914 .process_update
= imsm_process_update
,
11915 .prepare_update
= imsm_prepare_update
,
11916 .record_bad_block
= imsm_record_badblock
,
11917 .clear_bad_block
= imsm_clear_badblock
,
11918 .get_bad_blocks
= imsm_get_badblocks
,
11919 #endif /* MDASSEMBLE */