2 * mdadm - Intel(R) Matrix Storage Manager Support
4 * Copyright (C) 2002-2008 Intel Corporation
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20 #define HAVE_STDINT_H 1
24 #include "platform-intel.h"
30 /* MPB == Metadata Parameter Block */
31 #define MPB_SIGNATURE "Intel Raid ISM Cfg Sig. "
32 #define MPB_SIG_LEN (strlen(MPB_SIGNATURE))
33 #define MPB_VERSION_RAID0 "1.0.00"
34 #define MPB_VERSION_RAID1 "1.1.00"
35 #define MPB_VERSION_MANY_VOLUMES_PER_ARRAY "1.2.00"
36 #define MPB_VERSION_3OR4_DISK_ARRAY "1.2.01"
37 #define MPB_VERSION_RAID5 "1.2.02"
38 #define MPB_VERSION_5OR6_DISK_ARRAY "1.2.04"
39 #define MPB_VERSION_CNG "1.2.06"
40 #define MPB_VERSION_ATTRIBS "1.3.00"
41 #define MAX_SIGNATURE_LENGTH 32
42 #define MAX_RAID_SERIAL_LEN 16
45 #define MPB_ATTRIB_RAID0 __cpu_to_le32(0x00000001)
47 #define MPB_ATTRIB_RAID1 __cpu_to_le32(0x00000002)
49 #define MPB_ATTRIB_RAID10 __cpu_to_le32(0x00000004)
51 #define MPB_ATTRIB_RAID1E __cpu_to_le32(0x00000008)
53 #define MPB_ATTRIB_RAID5 __cpu_to_le32(0x00000010)
54 /* supports RAID CNG */
55 #define MPB_ATTRIB_RAIDCNG __cpu_to_le32(0x00000020)
56 /* supports expanded stripe sizes of 256K, 512K and 1MB */
57 #define MPB_ATTRIB_EXP_STRIPE_SIZE __cpu_to_le32(0x00000040)
59 /* The OROM Support RST Caching of Volumes */
60 #define MPB_ATTRIB_NVM __cpu_to_le32(0x02000000)
61 /* The OROM supports creating disks greater than 2TB */
62 #define MPB_ATTRIB_2TB_DISK __cpu_to_le32(0x04000000)
63 /* The OROM supports Bad Block Management */
64 #define MPB_ATTRIB_BBM __cpu_to_le32(0x08000000)
66 /* THe OROM Supports NVM Caching of Volumes */
67 #define MPB_ATTRIB_NEVER_USE2 __cpu_to_le32(0x10000000)
68 /* The OROM supports creating volumes greater than 2TB */
69 #define MPB_ATTRIB_2TB __cpu_to_le32(0x20000000)
70 /* originally for PMP, now it's wasted b/c. Never use this bit! */
71 #define MPB_ATTRIB_NEVER_USE __cpu_to_le32(0x40000000)
72 /* Verify MPB contents against checksum after reading MPB */
73 #define MPB_ATTRIB_CHECKSUM_VERIFY __cpu_to_le32(0x80000000)
75 /* Define all supported attributes that have to be accepted by mdadm
77 #define MPB_ATTRIB_SUPPORTED (MPB_ATTRIB_CHECKSUM_VERIFY | \
79 MPB_ATTRIB_2TB_DISK | \
84 MPB_ATTRIB_EXP_STRIPE_SIZE)
86 /* Define attributes that are unused but not harmful */
87 #define MPB_ATTRIB_IGNORED (MPB_ATTRIB_NEVER_USE)
89 #define MPB_SECTOR_CNT 2210
90 #define IMSM_RESERVED_SECTORS 4096
91 #define NUM_BLOCKS_DIRTY_STRIPE_REGION 2056
92 #define SECT_PER_MB_SHIFT 11
94 /* Disk configuration info. */
95 #define IMSM_MAX_DEVICES 255
97 __u8 serial
[MAX_RAID_SERIAL_LEN
];/* 0xD8 - 0xE7 ascii serial number */
98 __u32 total_blocks_lo
; /* 0xE8 - 0xEB total blocks lo */
99 __u32 scsi_id
; /* 0xEC - 0xEF scsi ID */
100 #define SPARE_DISK __cpu_to_le32(0x01) /* Spare */
101 #define CONFIGURED_DISK __cpu_to_le32(0x02) /* Member of some RaidDev */
102 #define FAILED_DISK __cpu_to_le32(0x04) /* Permanent failure */
103 __u32 status
; /* 0xF0 - 0xF3 */
104 __u32 owner_cfg_num
; /* which config 0,1,2... owns this disk */
105 __u32 total_blocks_hi
; /* 0xF4 - 0xF5 total blocks hi */
106 #define IMSM_DISK_FILLERS 3
107 __u32 filler
[IMSM_DISK_FILLERS
]; /* 0xF5 - 0x107 MPB_DISK_FILLERS for future expansion */
110 /* map selector for map managment
116 /* RAID map configuration infos. */
118 __u32 pba_of_lba0_lo
; /* start address of partition */
119 __u32 blocks_per_member_lo
;/* blocks per member */
120 __u32 num_data_stripes_lo
; /* number of data stripes */
121 __u16 blocks_per_strip
;
122 __u8 map_state
; /* Normal, Uninitialized, Degraded, Failed */
123 #define IMSM_T_STATE_NORMAL 0
124 #define IMSM_T_STATE_UNINITIALIZED 1
125 #define IMSM_T_STATE_DEGRADED 2
126 #define IMSM_T_STATE_FAILED 3
128 #define IMSM_T_RAID0 0
129 #define IMSM_T_RAID1 1
130 #define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */
131 __u8 num_members
; /* number of member disks */
132 __u8 num_domains
; /* number of parity domains */
133 __u8 failed_disk_num
; /* valid only when state is degraded */
135 __u32 pba_of_lba0_hi
;
136 __u32 blocks_per_member_hi
;
137 __u32 num_data_stripes_hi
;
138 __u32 filler
[4]; /* expansion area */
139 #define IMSM_ORD_REBUILD (1 << 24)
140 __u32 disk_ord_tbl
[1]; /* disk_ord_tbl[num_members],
141 * top byte contains some flags
143 } __attribute__ ((packed
));
146 __u32 curr_migr_unit
;
147 __u32 checkpoint_id
; /* id to access curr_migr_unit */
148 __u8 migr_state
; /* Normal or Migrating */
150 #define MIGR_REBUILD 1
151 #define MIGR_VERIFY 2 /* analagous to echo check > sync_action */
152 #define MIGR_GEN_MIGR 3
153 #define MIGR_STATE_CHANGE 4
154 #define MIGR_REPAIR 5
155 __u8 migr_type
; /* Initializing, Rebuilding, ... */
157 __u8 fs_state
; /* fast-sync state for CnG (0xff == disabled) */
158 __u16 verify_errors
; /* number of mismatches */
159 __u16 bad_blocks
; /* number of bad blocks during verify */
161 struct imsm_map map
[1];
162 /* here comes another one if migr_state */
163 } __attribute__ ((packed
));
166 __u8 volume
[MAX_RAID_SERIAL_LEN
];
169 #define DEV_BOOTABLE __cpu_to_le32(0x01)
170 #define DEV_BOOT_DEVICE __cpu_to_le32(0x02)
171 #define DEV_READ_COALESCING __cpu_to_le32(0x04)
172 #define DEV_WRITE_COALESCING __cpu_to_le32(0x08)
173 #define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10)
174 #define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20)
175 #define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40)
176 #define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80)
177 #define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100)
178 #define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200)
179 #define DEV_CLONE_N_GO __cpu_to_le32(0x400)
180 #define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800)
181 #define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000)
182 __u32 status
; /* Persistent RaidDev status */
183 __u32 reserved_blocks
; /* Reserved blocks at beginning of volume */
187 __u8 cng_master_disk
;
191 #define IMSM_DEV_FILLERS 10
192 __u32 filler
[IMSM_DEV_FILLERS
];
194 } __attribute__ ((packed
));
197 __u8 sig
[MAX_SIGNATURE_LENGTH
]; /* 0x00 - 0x1F */
198 __u32 check_sum
; /* 0x20 - 0x23 MPB Checksum */
199 __u32 mpb_size
; /* 0x24 - 0x27 Size of MPB */
200 __u32 family_num
; /* 0x28 - 0x2B Checksum from first time this config was written */
201 __u32 generation_num
; /* 0x2C - 0x2F Incremented each time this array's MPB is written */
202 __u32 error_log_size
; /* 0x30 - 0x33 in bytes */
203 __u32 attributes
; /* 0x34 - 0x37 */
204 __u8 num_disks
; /* 0x38 Number of configured disks */
205 __u8 num_raid_devs
; /* 0x39 Number of configured volumes */
206 __u8 error_log_pos
; /* 0x3A */
207 __u8 fill
[1]; /* 0x3B */
208 __u32 cache_size
; /* 0x3c - 0x40 in mb */
209 __u32 orig_family_num
; /* 0x40 - 0x43 original family num */
210 __u32 pwr_cycle_count
; /* 0x44 - 0x47 simulated power cycle count for array */
211 __u32 bbm_log_size
; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */
212 #define IMSM_FILLERS 35
213 __u32 filler
[IMSM_FILLERS
]; /* 0x4C - 0xD7 RAID_MPB_FILLERS */
214 struct imsm_disk disk
[1]; /* 0xD8 diskTbl[numDisks] */
215 /* here comes imsm_dev[num_raid_devs] */
216 /* here comes BBM logs */
217 } __attribute__ ((packed
));
219 #define BBM_LOG_MAX_ENTRIES 254
221 struct bbm_log_entry
{
222 __u64 defective_block_start
;
223 #define UNREADABLE 0xFFFFFFFF
224 __u32 spare_block_offset
;
225 __u16 remapped_marked_count
;
227 } __attribute__ ((__packed__
));
230 __u32 signature
; /* 0xABADB10C */
232 __u32 reserved_spare_block_count
; /* 0 */
233 __u32 reserved
; /* 0xFFFF */
234 __u64 first_spare_lba
;
235 struct bbm_log_entry mapped_block_entries
[BBM_LOG_MAX_ENTRIES
];
236 } __attribute__ ((__packed__
));
239 static char *map_state_str
[] = { "normal", "uninitialized", "degraded", "failed" };
242 #define RAID_DISK_RESERVED_BLOCKS_IMSM_HI 2209
244 #define GEN_MIGR_AREA_SIZE 2048 /* General Migration Copy Area size in blocks */
246 #define MIGR_REC_BUF_SIZE 512 /* size of migr_record i/o buffer */
247 #define MIGR_REC_POSITION 512 /* migr_record position offset on disk,
248 * MIGR_REC_BUF_SIZE <= MIGR_REC_POSITION
251 #define UNIT_SRC_NORMAL 0 /* Source data for curr_migr_unit must
252 * be recovered using srcMap */
253 #define UNIT_SRC_IN_CP_AREA 1 /* Source data for curr_migr_unit has
254 * already been migrated and must
255 * be recovered from checkpoint area */
257 __u32 rec_status
; /* Status used to determine how to restart
258 * migration in case it aborts
260 __u32 curr_migr_unit
; /* 0..numMigrUnits-1 */
261 __u32 family_num
; /* Family number of MPB
262 * containing the RaidDev
263 * that is migrating */
264 __u32 ascending_migr
; /* True if migrating in increasing
266 __u32 blocks_per_unit
; /* Num disk blocks per unit of operation */
267 __u32 dest_depth_per_unit
; /* Num member blocks each destMap
269 * advances per unit-of-operation */
270 __u32 ckpt_area_pba
; /* Pba of first block of ckpt copy area */
271 __u32 dest_1st_member_lba
; /* First member lba on first
272 * stripe of destination */
273 __u32 num_migr_units
; /* Total num migration units-of-op */
274 __u32 post_migr_vol_cap
; /* Size of volume after
275 * migration completes */
276 __u32 post_migr_vol_cap_hi
; /* Expansion space for LBA64 */
277 __u32 ckpt_read_disk_num
; /* Which member disk in destSubMap[0] the
278 * migration ckpt record was read from
279 * (for recovered migrations) */
280 } __attribute__ ((__packed__
));
285 * 2: metadata does not match
293 struct md_list
*next
;
296 #define pr_vrb(fmt, arg...) (void) (verbose && pr_err(fmt, ##arg))
298 static __u8
migr_type(struct imsm_dev
*dev
)
300 if (dev
->vol
.migr_type
== MIGR_VERIFY
&&
301 dev
->status
& DEV_VERIFY_AND_FIX
)
304 return dev
->vol
.migr_type
;
307 static void set_migr_type(struct imsm_dev
*dev
, __u8 migr_type
)
309 /* for compatibility with older oroms convert MIGR_REPAIR, into
310 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
312 if (migr_type
== MIGR_REPAIR
) {
313 dev
->vol
.migr_type
= MIGR_VERIFY
;
314 dev
->status
|= DEV_VERIFY_AND_FIX
;
316 dev
->vol
.migr_type
= migr_type
;
317 dev
->status
&= ~DEV_VERIFY_AND_FIX
;
321 static unsigned int sector_count(__u32 bytes
)
323 return ROUND_UP(bytes
, 512) / 512;
326 static unsigned int mpb_sectors(struct imsm_super
*mpb
)
328 return sector_count(__le32_to_cpu(mpb
->mpb_size
));
332 struct imsm_dev
*dev
;
333 struct intel_dev
*next
;
338 enum sys_dev_type type
;
341 struct intel_hba
*next
;
348 /* internal representation of IMSM metadata */
351 void *buf
; /* O_DIRECT buffer for reading/writing metadata */
352 struct imsm_super
*anchor
; /* immovable parameters */
355 void *migr_rec_buf
; /* buffer for I/O operations */
356 struct migr_record
*migr_rec
; /* migration record */
358 int clean_migration_record_by_mdmon
; /* when reshape is switched to next
359 array, it indicates that mdmon is allowed to clean migration
361 size_t len
; /* size of the 'buf' allocation */
362 void *next_buf
; /* for realloc'ing buf from the manager */
364 int updates_pending
; /* count of pending updates for mdmon */
365 int current_vol
; /* index of raid device undergoing creation */
366 unsigned long long create_offset
; /* common start for 'current_vol' */
367 __u32 random
; /* random data for seeding new family numbers */
368 struct intel_dev
*devlist
;
372 __u8 serial
[MAX_RAID_SERIAL_LEN
];
375 struct imsm_disk disk
;
378 struct extent
*e
; /* for determining freespace @ create */
379 int raiddisk
; /* slot to fill in autolayout */
381 } *disks
, *current_disk
;
382 struct dl
*disk_mgmt_list
; /* list of disks to add/remove while mdmon
384 struct dl
*missing
; /* disks removed while we weren't looking */
385 struct bbm_log
*bbm_log
;
386 struct intel_hba
*hba
; /* device path of the raid controller for this metadata */
387 const struct imsm_orom
*orom
; /* platform firmware support */
388 struct intel_super
*next
; /* (temp) list for disambiguating family_num */
392 struct imsm_disk disk
;
393 #define IMSM_UNKNOWN_OWNER (-1)
395 struct intel_disk
*next
;
399 unsigned long long start
, size
;
402 /* definitions of reshape process types */
403 enum imsm_reshape_type
{
409 /* definition of messages passed to imsm_process_update */
410 enum imsm_update_type
{
411 update_activate_spare
,
415 update_add_remove_disk
,
416 update_reshape_container_disks
,
417 update_reshape_migration
,
419 update_general_migration_checkpoint
,
423 struct imsm_update_activate_spare
{
424 enum imsm_update_type type
;
428 struct imsm_update_activate_spare
*next
;
434 unsigned long long size
;
441 enum takeover_direction
{
445 struct imsm_update_takeover
{
446 enum imsm_update_type type
;
448 enum takeover_direction direction
;
451 struct imsm_update_reshape
{
452 enum imsm_update_type type
;
456 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
459 struct imsm_update_reshape_migration
{
460 enum imsm_update_type type
;
463 /* fields for array migration changes
470 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
473 struct imsm_update_size_change
{
474 enum imsm_update_type type
;
479 struct imsm_update_general_migration_checkpoint
{
480 enum imsm_update_type type
;
481 __u32 curr_migr_unit
;
485 __u8 serial
[MAX_RAID_SERIAL_LEN
];
488 struct imsm_update_create_array
{
489 enum imsm_update_type type
;
494 struct imsm_update_kill_array
{
495 enum imsm_update_type type
;
499 struct imsm_update_rename_array
{
500 enum imsm_update_type type
;
501 __u8 name
[MAX_RAID_SERIAL_LEN
];
505 struct imsm_update_add_remove_disk
{
506 enum imsm_update_type type
;
509 static const char *_sys_dev_type
[] = {
510 [SYS_DEV_UNKNOWN
] = "Unknown",
511 [SYS_DEV_SAS
] = "SAS",
512 [SYS_DEV_SATA
] = "SATA",
513 [SYS_DEV_NVME
] = "NVMe",
514 [SYS_DEV_VMD
] = "VMD"
517 const char *get_sys_dev_type(enum sys_dev_type type
)
519 if (type
>= SYS_DEV_MAX
)
520 type
= SYS_DEV_UNKNOWN
;
522 return _sys_dev_type
[type
];
525 static struct intel_hba
* alloc_intel_hba(struct sys_dev
*device
)
527 struct intel_hba
*result
= xmalloc(sizeof(*result
));
529 result
->type
= device
->type
;
530 result
->path
= xstrdup(device
->path
);
532 if (result
->path
&& (result
->pci_id
= strrchr(result
->path
, '/')) != NULL
)
538 static struct intel_hba
* find_intel_hba(struct intel_hba
*hba
, struct sys_dev
*device
)
540 struct intel_hba
*result
;
542 for (result
= hba
; result
; result
= result
->next
) {
543 if (result
->type
== device
->type
&& strcmp(result
->path
, device
->path
) == 0)
549 static int attach_hba_to_super(struct intel_super
*super
, struct sys_dev
*device
)
551 struct intel_hba
*hba
;
553 /* check if disk attached to Intel HBA */
554 hba
= find_intel_hba(super
->hba
, device
);
557 /* Check if HBA is already attached to super */
558 if (super
->hba
== NULL
) {
559 super
->hba
= alloc_intel_hba(device
);
564 /* Intel metadata allows for all disks attached to the same type HBA.
565 * Do not support HBA types mixing
567 if (device
->type
!= hba
->type
)
570 /* Always forbid spanning between VMD domains (seen as different controllers by mdadm) */
571 if (device
->type
== SYS_DEV_VMD
&& !path_attached_to_hba(device
->path
, hba
->path
))
574 /* Multiple same type HBAs can be used if they share the same OROM */
575 const struct imsm_orom
*device_orom
= get_orom_by_device_id(device
->dev_id
);
577 if (device_orom
!= super
->orom
)
583 hba
->next
= alloc_intel_hba(device
);
587 static struct sys_dev
* find_disk_attached_hba(int fd
, const char *devname
)
589 struct sys_dev
*list
, *elem
;
592 if ((list
= find_intel_devices()) == NULL
)
596 disk_path
= (char *) devname
;
598 disk_path
= diskfd_to_devpath(fd
);
603 for (elem
= list
; elem
; elem
= elem
->next
)
604 if (path_attached_to_hba(disk_path
, elem
->path
))
607 if (disk_path
!= devname
)
613 static int find_intel_hba_capability(int fd
, struct intel_super
*super
,
616 static struct supertype
*match_metadata_desc_imsm(char *arg
)
618 struct supertype
*st
;
620 if (strcmp(arg
, "imsm") != 0 &&
621 strcmp(arg
, "default") != 0
625 st
= xcalloc(1, sizeof(*st
));
626 st
->ss
= &super_imsm
;
627 st
->max_devs
= IMSM_MAX_DEVICES
;
628 st
->minor_version
= 0;
634 static __u8
*get_imsm_version(struct imsm_super
*mpb
)
636 return &mpb
->sig
[MPB_SIG_LEN
];
640 /* retrieve a disk directly from the anchor when the anchor is known to be
641 * up-to-date, currently only at load time
643 static struct imsm_disk
*__get_imsm_disk(struct imsm_super
*mpb
, __u8 index
)
645 if (index
>= mpb
->num_disks
)
647 return &mpb
->disk
[index
];
650 /* retrieve the disk description based on a index of the disk
653 static struct dl
*get_imsm_dl_disk(struct intel_super
*super
, __u8 index
)
657 for (d
= super
->disks
; d
; d
= d
->next
)
658 if (d
->index
== index
)
663 /* retrieve a disk from the parsed metadata */
664 static struct imsm_disk
*get_imsm_disk(struct intel_super
*super
, __u8 index
)
668 dl
= get_imsm_dl_disk(super
, index
);
675 /* generate a checksum directly from the anchor when the anchor is known to be
676 * up-to-date, currently only at load or write_super after coalescing
678 static __u32
__gen_imsm_checksum(struct imsm_super
*mpb
)
680 __u32 end
= mpb
->mpb_size
/ sizeof(end
);
681 __u32
*p
= (__u32
*) mpb
;
685 sum
+= __le32_to_cpu(*p
);
689 return sum
- __le32_to_cpu(mpb
->check_sum
);
692 static size_t sizeof_imsm_map(struct imsm_map
*map
)
694 return sizeof(struct imsm_map
) + sizeof(__u32
) * (map
->num_members
- 1);
697 struct imsm_map
*get_imsm_map(struct imsm_dev
*dev
, int second_map
)
699 /* A device can have 2 maps if it is in the middle of a migration.
701 * MAP_0 - we return the first map
702 * MAP_1 - we return the second map if it exists, else NULL
703 * MAP_X - we return the second map if it exists, else the first
705 struct imsm_map
*map
= &dev
->vol
.map
[0];
706 struct imsm_map
*map2
= NULL
;
708 if (dev
->vol
.migr_state
)
709 map2
= (void *)map
+ sizeof_imsm_map(map
);
711 switch (second_map
) {
728 /* return the size of the device.
729 * migr_state increases the returned size if map[0] were to be duplicated
731 static size_t sizeof_imsm_dev(struct imsm_dev
*dev
, int migr_state
)
733 size_t size
= sizeof(*dev
) - sizeof(struct imsm_map
) +
734 sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
736 /* migrating means an additional map */
737 if (dev
->vol
.migr_state
)
738 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_1
));
740 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
746 /* retrieve disk serial number list from a metadata update */
747 static struct disk_info
*get_disk_info(struct imsm_update_create_array
*update
)
750 struct disk_info
*inf
;
752 inf
= u
+ sizeof(*update
) - sizeof(struct imsm_dev
) +
753 sizeof_imsm_dev(&update
->dev
, 0);
759 static struct imsm_dev
*__get_imsm_dev(struct imsm_super
*mpb
, __u8 index
)
765 if (index
>= mpb
->num_raid_devs
)
768 /* devices start after all disks */
769 offset
= ((void *) &mpb
->disk
[mpb
->num_disks
]) - _mpb
;
771 for (i
= 0; i
<= index
; i
++)
773 return _mpb
+ offset
;
775 offset
+= sizeof_imsm_dev(_mpb
+ offset
, 0);
780 static struct imsm_dev
*get_imsm_dev(struct intel_super
*super
, __u8 index
)
782 struct intel_dev
*dv
;
784 if (index
>= super
->anchor
->num_raid_devs
)
786 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
787 if (dv
->index
== index
)
794 * == MAP_0 get first map
795 * == MAP_1 get second map
796 * == MAP_X than get map according to the current migr_state
798 static __u32
get_imsm_ord_tbl_ent(struct imsm_dev
*dev
,
802 struct imsm_map
*map
;
804 map
= get_imsm_map(dev
, second_map
);
806 /* top byte identifies disk under rebuild */
807 return __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
810 #define ord_to_idx(ord) (((ord) << 8) >> 8)
811 static __u32
get_imsm_disk_idx(struct imsm_dev
*dev
, int slot
, int second_map
)
813 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, second_map
);
815 return ord_to_idx(ord
);
818 static void set_imsm_ord_tbl_ent(struct imsm_map
*map
, int slot
, __u32 ord
)
820 map
->disk_ord_tbl
[slot
] = __cpu_to_le32(ord
);
823 static int get_imsm_disk_slot(struct imsm_map
*map
, unsigned idx
)
828 for (slot
= 0; slot
< map
->num_members
; slot
++) {
829 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
830 if (ord_to_idx(ord
) == idx
)
837 static int get_imsm_raid_level(struct imsm_map
*map
)
839 if (map
->raid_level
== 1) {
840 if (map
->num_members
== 2)
846 return map
->raid_level
;
849 static int cmp_extent(const void *av
, const void *bv
)
851 const struct extent
*a
= av
;
852 const struct extent
*b
= bv
;
853 if (a
->start
< b
->start
)
855 if (a
->start
> b
->start
)
860 static int count_memberships(struct dl
*dl
, struct intel_super
*super
)
865 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
866 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
867 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
869 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
876 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
);
878 static int split_ull(unsigned long long n
, __u32
*lo
, __u32
*hi
)
880 if (lo
== 0 || hi
== 0)
882 *lo
= __le32_to_cpu((unsigned)n
);
883 *hi
= __le32_to_cpu((unsigned)(n
>> 32));
887 static unsigned long long join_u32(__u32 lo
, __u32 hi
)
889 return (unsigned long long)__le32_to_cpu(lo
) |
890 (((unsigned long long)__le32_to_cpu(hi
)) << 32);
893 static unsigned long long total_blocks(struct imsm_disk
*disk
)
897 return join_u32(disk
->total_blocks_lo
, disk
->total_blocks_hi
);
900 static unsigned long long pba_of_lba0(struct imsm_map
*map
)
904 return join_u32(map
->pba_of_lba0_lo
, map
->pba_of_lba0_hi
);
907 static unsigned long long blocks_per_member(struct imsm_map
*map
)
911 return join_u32(map
->blocks_per_member_lo
, map
->blocks_per_member_hi
);
915 static unsigned long long num_data_stripes(struct imsm_map
*map
)
919 return join_u32(map
->num_data_stripes_lo
, map
->num_data_stripes_hi
);
922 static void set_total_blocks(struct imsm_disk
*disk
, unsigned long long n
)
924 split_ull(n
, &disk
->total_blocks_lo
, &disk
->total_blocks_hi
);
928 static void set_pba_of_lba0(struct imsm_map
*map
, unsigned long long n
)
930 split_ull(n
, &map
->pba_of_lba0_lo
, &map
->pba_of_lba0_hi
);
933 static void set_blocks_per_member(struct imsm_map
*map
, unsigned long long n
)
935 split_ull(n
, &map
->blocks_per_member_lo
, &map
->blocks_per_member_hi
);
938 static void set_num_data_stripes(struct imsm_map
*map
, unsigned long long n
)
940 split_ull(n
, &map
->num_data_stripes_lo
, &map
->num_data_stripes_hi
);
943 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
)
945 /* find a list of used extents on the given physical device */
946 struct extent
*rv
, *e
;
948 int memberships
= count_memberships(dl
, super
);
951 /* trim the reserved area for spares, so they can join any array
952 * regardless of whether the OROM has assigned sectors from the
953 * IMSM_RESERVED_SECTORS region
956 reservation
= imsm_min_reserved_sectors(super
);
958 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
960 rv
= xcalloc(sizeof(struct extent
), (memberships
+ 1));
963 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
964 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
965 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
967 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
968 e
->start
= pba_of_lba0(map
);
969 e
->size
= blocks_per_member(map
);
973 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
975 /* determine the start of the metadata
976 * when no raid devices are defined use the default
977 * ...otherwise allow the metadata to truncate the value
978 * as is the case with older versions of imsm
981 struct extent
*last
= &rv
[memberships
- 1];
982 unsigned long long remainder
;
984 remainder
= total_blocks(&dl
->disk
) - (last
->start
+ last
->size
);
985 /* round down to 1k block to satisfy precision of the kernel
989 /* make sure remainder is still sane */
990 if (remainder
< (unsigned)ROUND_UP(super
->len
, 512) >> 9)
991 remainder
= ROUND_UP(super
->len
, 512) >> 9;
992 if (reservation
> remainder
)
993 reservation
= remainder
;
995 e
->start
= total_blocks(&dl
->disk
) - reservation
;
1000 /* try to determine how much space is reserved for metadata from
1001 * the last get_extents() entry, otherwise fallback to the
1004 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
1010 /* for spares just return a minimal reservation which will grow
1011 * once the spare is picked up by an array
1013 if (dl
->index
== -1)
1014 return MPB_SECTOR_CNT
;
1016 e
= get_extents(super
, dl
);
1018 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1020 /* scroll to last entry */
1021 for (i
= 0; e
[i
].size
; i
++)
1024 rv
= total_blocks(&dl
->disk
) - e
[i
].start
;
1031 static int is_spare(struct imsm_disk
*disk
)
1033 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
1036 static int is_configured(struct imsm_disk
*disk
)
1038 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
1041 static int is_failed(struct imsm_disk
*disk
)
1043 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
1046 /* try to determine how much space is reserved for metadata from
1047 * the last get_extents() entry on the smallest active disk,
1048 * otherwise fallback to the default
1050 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
)
1054 unsigned long long min_active
;
1056 __u32 rv
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1057 struct dl
*dl
, *dl_min
= NULL
;
1063 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
1066 unsigned long long blocks
= total_blocks(&dl
->disk
);
1067 if (blocks
< min_active
|| min_active
== 0) {
1069 min_active
= blocks
;
1075 /* find last lba used by subarrays on the smallest active disk */
1076 e
= get_extents(super
, dl_min
);
1079 for (i
= 0; e
[i
].size
; i
++)
1082 remainder
= min_active
- e
[i
].start
;
1085 /* to give priority to recovery we should not require full
1086 IMSM_RESERVED_SECTORS from the spare */
1087 rv
= MPB_SECTOR_CNT
+ NUM_BLOCKS_DIRTY_STRIPE_REGION
;
1089 /* if real reservation is smaller use that value */
1090 return (remainder
< rv
) ? remainder
: rv
;
1093 /* Return minimum size of a spare that can be used in this array*/
1094 static unsigned long long min_acceptable_spare_size_imsm(struct supertype
*st
)
1096 struct intel_super
*super
= st
->sb
;
1100 unsigned long long rv
= 0;
1104 /* find first active disk in array */
1106 while (dl
&& (is_failed(&dl
->disk
) || dl
->index
== -1))
1110 /* find last lba used by subarrays */
1111 e
= get_extents(super
, dl
);
1114 for (i
= 0; e
[i
].size
; i
++)
1117 rv
= e
[i
-1].start
+ e
[i
-1].size
;
1120 /* add the amount of space needed for metadata */
1121 rv
= rv
+ imsm_min_reserved_sectors(super
);
1126 static int is_gen_migration(struct imsm_dev
*dev
);
1129 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
1130 struct imsm_dev
*dev
);
1132 static void print_imsm_dev(struct intel_super
*super
,
1133 struct imsm_dev
*dev
,
1139 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1140 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
1144 printf("[%.16s]:\n", dev
->volume
);
1145 printf(" UUID : %s\n", uuid
);
1146 printf(" RAID Level : %d", get_imsm_raid_level(map
));
1148 printf(" <-- %d", get_imsm_raid_level(map2
));
1150 printf(" Members : %d", map
->num_members
);
1152 printf(" <-- %d", map2
->num_members
);
1154 printf(" Slots : [");
1155 for (i
= 0; i
< map
->num_members
; i
++) {
1156 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
1157 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1162 for (i
= 0; i
< map2
->num_members
; i
++) {
1163 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_1
);
1164 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1169 printf(" Failed disk : ");
1170 if (map
->failed_disk_num
== 0xff)
1173 printf("%i", map
->failed_disk_num
);
1175 slot
= get_imsm_disk_slot(map
, disk_idx
);
1177 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
1178 printf(" This Slot : %d%s\n", slot
,
1179 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
1181 printf(" This Slot : ?\n");
1182 sz
= __le32_to_cpu(dev
->size_high
);
1184 sz
+= __le32_to_cpu(dev
->size_low
);
1185 printf(" Array Size : %llu%s\n", (unsigned long long)sz
,
1186 human_size(sz
* 512));
1187 sz
= blocks_per_member(map
);
1188 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz
,
1189 human_size(sz
* 512));
1190 printf(" Sector Offset : %llu\n",
1192 printf(" Num Stripes : %llu\n",
1193 num_data_stripes(map
));
1194 printf(" Chunk Size : %u KiB",
1195 __le16_to_cpu(map
->blocks_per_strip
) / 2);
1197 printf(" <-- %u KiB",
1198 __le16_to_cpu(map2
->blocks_per_strip
) / 2);
1200 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
1201 printf(" Migrate State : ");
1202 if (dev
->vol
.migr_state
) {
1203 if (migr_type(dev
) == MIGR_INIT
)
1204 printf("initialize\n");
1205 else if (migr_type(dev
) == MIGR_REBUILD
)
1206 printf("rebuild\n");
1207 else if (migr_type(dev
) == MIGR_VERIFY
)
1209 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
1210 printf("general migration\n");
1211 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
1212 printf("state change\n");
1213 else if (migr_type(dev
) == MIGR_REPAIR
)
1216 printf("<unknown:%d>\n", migr_type(dev
));
1219 printf(" Map State : %s", map_state_str
[map
->map_state
]);
1220 if (dev
->vol
.migr_state
) {
1221 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1223 printf(" <-- %s", map_state_str
[map
->map_state
]);
1224 printf("\n Checkpoint : %u ",
1225 __le32_to_cpu(dev
->vol
.curr_migr_unit
));
1226 if (is_gen_migration(dev
) && (slot
> 1 || slot
< 0))
1229 printf("(%llu)", (unsigned long long)
1230 blocks_per_migr_unit(super
, dev
));
1233 printf(" Dirty State : %s\n", dev
->vol
.dirty
? "dirty" : "clean");
1236 static void print_imsm_disk(struct imsm_disk
*disk
, int index
, __u32 reserved
)
1238 char str
[MAX_RAID_SERIAL_LEN
+ 1];
1241 if (index
< -1 || !disk
)
1245 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
1247 printf(" Disk%02d Serial : %s\n", index
, str
);
1249 printf(" Disk Serial : %s\n", str
);
1250 printf(" State :%s%s%s\n", is_spare(disk
) ? " spare" : "",
1251 is_configured(disk
) ? " active" : "",
1252 is_failed(disk
) ? " failed" : "");
1253 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
1254 sz
= total_blocks(disk
) - reserved
;
1255 printf(" Usable Size : %llu%s\n", (unsigned long long)sz
,
1256 human_size(sz
* 512));
1259 void examine_migr_rec_imsm(struct intel_super
*super
)
1261 struct migr_record
*migr_rec
= super
->migr_rec
;
1262 struct imsm_super
*mpb
= super
->anchor
;
1265 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1266 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1267 struct imsm_map
*map
;
1270 if (is_gen_migration(dev
) == 0)
1273 printf("\nMigration Record Information:");
1275 /* first map under migration */
1276 map
= get_imsm_map(dev
, MAP_0
);
1278 slot
= get_imsm_disk_slot(map
, super
->disks
->index
);
1279 if (map
== NULL
|| slot
> 1 || slot
< 0) {
1280 printf(" Empty\n ");
1281 printf("Examine one of first two disks in array\n");
1284 printf("\n Status : ");
1285 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
1288 printf("Contains Data\n");
1289 printf(" Current Unit : %u\n",
1290 __le32_to_cpu(migr_rec
->curr_migr_unit
));
1291 printf(" Family : %u\n",
1292 __le32_to_cpu(migr_rec
->family_num
));
1293 printf(" Ascending : %u\n",
1294 __le32_to_cpu(migr_rec
->ascending_migr
));
1295 printf(" Blocks Per Unit : %u\n",
1296 __le32_to_cpu(migr_rec
->blocks_per_unit
));
1297 printf(" Dest. Depth Per Unit : %u\n",
1298 __le32_to_cpu(migr_rec
->dest_depth_per_unit
));
1299 printf(" Checkpoint Area pba : %u\n",
1300 __le32_to_cpu(migr_rec
->ckpt_area_pba
));
1301 printf(" First member lba : %u\n",
1302 __le32_to_cpu(migr_rec
->dest_1st_member_lba
));
1303 printf(" Total Number of Units : %u\n",
1304 __le32_to_cpu(migr_rec
->num_migr_units
));
1305 printf(" Size of volume : %u\n",
1306 __le32_to_cpu(migr_rec
->post_migr_vol_cap
));
1307 printf(" Expansion space for LBA64 : %u\n",
1308 __le32_to_cpu(migr_rec
->post_migr_vol_cap_hi
));
1309 printf(" Record was read from : %u\n",
1310 __le32_to_cpu(migr_rec
->ckpt_read_disk_num
));
1315 #endif /* MDASSEMBLE */
1316 /*******************************************************************************
1317 * function: imsm_check_attributes
1318 * Description: Function checks if features represented by attributes flags
1319 * are supported by mdadm.
1321 * attributes - Attributes read from metadata
1323 * 0 - passed attributes contains unsupported features flags
1324 * 1 - all features are supported
1325 ******************************************************************************/
1326 static int imsm_check_attributes(__u32 attributes
)
1329 __u32 not_supported
= MPB_ATTRIB_SUPPORTED
^0xffffffff;
1331 not_supported
&= ~MPB_ATTRIB_IGNORED
;
1333 not_supported
&= attributes
;
1334 if (not_supported
) {
1335 pr_err("(IMSM): Unsupported attributes : %x\n",
1336 (unsigned)__le32_to_cpu(not_supported
));
1337 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1338 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
1339 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1341 if (not_supported
& MPB_ATTRIB_2TB
) {
1342 dprintf("\t\tMPB_ATTRIB_2TB\n");
1343 not_supported
^= MPB_ATTRIB_2TB
;
1345 if (not_supported
& MPB_ATTRIB_RAID0
) {
1346 dprintf("\t\tMPB_ATTRIB_RAID0\n");
1347 not_supported
^= MPB_ATTRIB_RAID0
;
1349 if (not_supported
& MPB_ATTRIB_RAID1
) {
1350 dprintf("\t\tMPB_ATTRIB_RAID1\n");
1351 not_supported
^= MPB_ATTRIB_RAID1
;
1353 if (not_supported
& MPB_ATTRIB_RAID10
) {
1354 dprintf("\t\tMPB_ATTRIB_RAID10\n");
1355 not_supported
^= MPB_ATTRIB_RAID10
;
1357 if (not_supported
& MPB_ATTRIB_RAID1E
) {
1358 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
1359 not_supported
^= MPB_ATTRIB_RAID1E
;
1361 if (not_supported
& MPB_ATTRIB_RAID5
) {
1362 dprintf("\t\tMPB_ATTRIB_RAID5\n");
1363 not_supported
^= MPB_ATTRIB_RAID5
;
1365 if (not_supported
& MPB_ATTRIB_RAIDCNG
) {
1366 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
1367 not_supported
^= MPB_ATTRIB_RAIDCNG
;
1369 if (not_supported
& MPB_ATTRIB_BBM
) {
1370 dprintf("\t\tMPB_ATTRIB_BBM\n");
1371 not_supported
^= MPB_ATTRIB_BBM
;
1373 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1374 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
1375 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1377 if (not_supported
& MPB_ATTRIB_EXP_STRIPE_SIZE
) {
1378 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
1379 not_supported
^= MPB_ATTRIB_EXP_STRIPE_SIZE
;
1381 if (not_supported
& MPB_ATTRIB_2TB_DISK
) {
1382 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
1383 not_supported
^= MPB_ATTRIB_2TB_DISK
;
1385 if (not_supported
& MPB_ATTRIB_NEVER_USE2
) {
1386 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
1387 not_supported
^= MPB_ATTRIB_NEVER_USE2
;
1389 if (not_supported
& MPB_ATTRIB_NEVER_USE
) {
1390 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
1391 not_supported
^= MPB_ATTRIB_NEVER_USE
;
1395 dprintf("(IMSM): Unknown attributes : %x\n", not_supported
);
1404 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
1406 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
1408 struct intel_super
*super
= st
->sb
;
1409 struct imsm_super
*mpb
= super
->anchor
;
1410 char str
[MAX_SIGNATURE_LENGTH
];
1415 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
1418 snprintf(str
, MPB_SIG_LEN
, "%s", mpb
->sig
);
1419 printf(" Magic : %s\n", str
);
1420 snprintf(str
, strlen(MPB_VERSION_RAID0
), "%s", get_imsm_version(mpb
));
1421 printf(" Version : %s\n", get_imsm_version(mpb
));
1422 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
1423 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
1424 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
1425 printf(" Attributes : ");
1426 if (imsm_check_attributes(mpb
->attributes
))
1427 printf("All supported\n");
1429 printf("not supported\n");
1430 getinfo_super_imsm(st
, &info
, NULL
);
1431 fname_from_uuid(st
, &info
, nbuf
, ':');
1432 printf(" UUID : %s\n", nbuf
+ 5);
1433 sum
= __le32_to_cpu(mpb
->check_sum
);
1434 printf(" Checksum : %08x %s\n", sum
,
1435 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
1436 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
));
1437 printf(" Disks : %d\n", mpb
->num_disks
);
1438 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
1439 print_imsm_disk(__get_imsm_disk(mpb
, super
->disks
->index
), super
->disks
->index
, reserved
);
1440 if (super
->bbm_log
) {
1441 struct bbm_log
*log
= super
->bbm_log
;
1444 printf("Bad Block Management Log:\n");
1445 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
1446 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
1447 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
1448 printf(" Spare Blocks : %d\n", __le32_to_cpu(log
->reserved_spare_block_count
));
1449 printf(" First Spare : %llx\n",
1450 (unsigned long long) __le64_to_cpu(log
->first_spare_lba
));
1452 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1454 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1456 super
->current_vol
= i
;
1457 getinfo_super_imsm(st
, &info
, NULL
);
1458 fname_from_uuid(st
, &info
, nbuf
, ':');
1459 print_imsm_dev(super
, dev
, nbuf
+ 5, super
->disks
->index
);
1461 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1462 if (i
== super
->disks
->index
)
1464 print_imsm_disk(__get_imsm_disk(mpb
, i
), i
, reserved
);
1467 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
1468 if (dl
->index
== -1)
1469 print_imsm_disk(&dl
->disk
, -1, reserved
);
1471 examine_migr_rec_imsm(super
);
1474 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
1476 /* We just write a generic IMSM ARRAY entry */
1479 struct intel_super
*super
= st
->sb
;
1481 if (!super
->anchor
->num_raid_devs
) {
1482 printf("ARRAY metadata=imsm\n");
1486 getinfo_super_imsm(st
, &info
, NULL
);
1487 fname_from_uuid(st
, &info
, nbuf
, ':');
1488 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
1491 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
1493 /* We just write a generic IMSM ARRAY entry */
1497 struct intel_super
*super
= st
->sb
;
1500 if (!super
->anchor
->num_raid_devs
)
1503 getinfo_super_imsm(st
, &info
, NULL
);
1504 fname_from_uuid(st
, &info
, nbuf
, ':');
1505 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1506 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1508 super
->current_vol
= i
;
1509 getinfo_super_imsm(st
, &info
, NULL
);
1510 fname_from_uuid(st
, &info
, nbuf1
, ':');
1511 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
1512 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
1516 static void export_examine_super_imsm(struct supertype
*st
)
1518 struct intel_super
*super
= st
->sb
;
1519 struct imsm_super
*mpb
= super
->anchor
;
1523 getinfo_super_imsm(st
, &info
, NULL
);
1524 fname_from_uuid(st
, &info
, nbuf
, ':');
1525 printf("MD_METADATA=imsm\n");
1526 printf("MD_LEVEL=container\n");
1527 printf("MD_UUID=%s\n", nbuf
+5);
1528 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
1531 static int copy_metadata_imsm(struct supertype
*st
, int from
, int to
)
1533 /* The second last 512byte sector of the device contains
1534 * the "struct imsm_super" metadata.
1535 * This contains mpb_size which is the size in bytes of the
1536 * extended metadata. This is located immediately before
1538 * We want to read all that, plus the last sector which
1539 * may contain a migration record, and write it all
1543 unsigned long long dsize
, offset
;
1545 struct imsm_super
*sb
;
1548 if (posix_memalign(&buf
, 4096, 4096) != 0)
1551 if (!get_dev_size(from
, NULL
, &dsize
))
1554 if (lseek64(from
, dsize
-1024, 0) < 0)
1556 if (read(from
, buf
, 512) != 512)
1559 if (strncmp((char*)sb
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0)
1562 sectors
= mpb_sectors(sb
) + 2;
1563 offset
= dsize
- sectors
* 512;
1564 if (lseek64(from
, offset
, 0) < 0 ||
1565 lseek64(to
, offset
, 0) < 0)
1567 while (written
< sectors
* 512) {
1568 int n
= sectors
*512 - written
;
1571 if (read(from
, buf
, n
) != n
)
1573 if (write(to
, buf
, n
) != n
)
1584 static void detail_super_imsm(struct supertype
*st
, char *homehost
)
1589 getinfo_super_imsm(st
, &info
, NULL
);
1590 fname_from_uuid(st
, &info
, nbuf
, ':');
1591 printf("\n UUID : %s\n", nbuf
+ 5);
1594 static void brief_detail_super_imsm(struct supertype
*st
)
1598 getinfo_super_imsm(st
, &info
, NULL
);
1599 fname_from_uuid(st
, &info
, nbuf
, ':');
1600 printf(" UUID=%s", nbuf
+ 5);
1603 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
);
1604 static void fd2devname(int fd
, char *name
);
1606 static int ahci_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
1608 /* dump an unsorted list of devices attached to AHCI Intel storage
1609 * controller, as well as non-connected ports
1611 int hba_len
= strlen(hba_path
) + 1;
1616 unsigned long port_mask
= (1 << port_count
) - 1;
1618 if (port_count
> (int)sizeof(port_mask
) * 8) {
1620 pr_err("port_count %d out of range\n", port_count
);
1624 /* scroll through /sys/dev/block looking for devices attached to
1627 dir
= opendir("/sys/dev/block");
1631 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
1642 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
1644 path
= devt_to_devpath(makedev(major
, minor
));
1647 if (!path_attached_to_hba(path
, hba_path
)) {
1653 /* retrieve the scsi device type */
1654 if (asprintf(&device
, "/sys/dev/block/%d:%d/device/xxxxxxx", major
, minor
) < 0) {
1656 pr_err("failed to allocate 'device'\n");
1660 sprintf(device
, "/sys/dev/block/%d:%d/device/type", major
, minor
);
1661 if (load_sys(device
, buf
, sizeof(buf
)) != 0) {
1663 pr_err("failed to read device type for %s\n",
1669 type
= strtoul(buf
, NULL
, 10);
1671 /* if it's not a disk print the vendor and model */
1672 if (!(type
== 0 || type
== 7 || type
== 14)) {
1675 sprintf(device
, "/sys/dev/block/%d:%d/device/vendor", major
, minor
);
1676 if (load_sys(device
, buf
, sizeof(buf
)) == 0) {
1677 strncpy(vendor
, buf
, sizeof(vendor
));
1678 vendor
[sizeof(vendor
) - 1] = '\0';
1679 c
= (char *) &vendor
[sizeof(vendor
) - 1];
1680 while (isspace(*c
) || *c
== '\0')
1684 sprintf(device
, "/sys/dev/block/%d:%d/device/model", major
, minor
);
1685 if (load_sys(device
, buf
, sizeof(buf
)) == 0) {
1686 strncpy(model
, buf
, sizeof(model
));
1687 model
[sizeof(model
) - 1] = '\0';
1688 c
= (char *) &model
[sizeof(model
) - 1];
1689 while (isspace(*c
) || *c
== '\0')
1693 if (vendor
[0] && model
[0])
1694 sprintf(buf
, "%.64s %.64s", vendor
, model
);
1696 switch (type
) { /* numbers from hald/linux/device.c */
1697 case 1: sprintf(buf
, "tape"); break;
1698 case 2: sprintf(buf
, "printer"); break;
1699 case 3: sprintf(buf
, "processor"); break;
1701 case 5: sprintf(buf
, "cdrom"); break;
1702 case 6: sprintf(buf
, "scanner"); break;
1703 case 8: sprintf(buf
, "media_changer"); break;
1704 case 9: sprintf(buf
, "comm"); break;
1705 case 12: sprintf(buf
, "raid"); break;
1706 default: sprintf(buf
, "unknown");
1712 /* chop device path to 'host%d' and calculate the port number */
1713 c
= strchr(&path
[hba_len
], '/');
1716 pr_err("%s - invalid path name\n", path
+ hba_len
);
1721 if ((sscanf(&path
[hba_len
], "ata%d", &port
) == 1) ||
1722 ((sscanf(&path
[hba_len
], "host%d", &port
) == 1)))
1726 *c
= '/'; /* repair the full string */
1727 pr_err("failed to determine port number for %s\n",
1734 /* mark this port as used */
1735 port_mask
&= ~(1 << port
);
1737 /* print out the device information */
1739 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
1743 fd
= dev_open(ent
->d_name
, O_RDONLY
);
1745 printf(" Port%d : - disk info unavailable -\n", port
);
1747 fd2devname(fd
, buf
);
1748 printf(" Port%d : %s", port
, buf
);
1749 if (imsm_read_serial(fd
, NULL
, (__u8
*) buf
) == 0)
1750 printf(" (%.*s)\n", MAX_RAID_SERIAL_LEN
, buf
);
1765 for (i
= 0; i
< port_count
; i
++)
1766 if (port_mask
& (1 << i
))
1767 printf(" Port%d : - no device attached -\n", i
);
1773 static int print_vmd_attached_devs(struct sys_dev
*hba
)
1781 if (hba
->type
!= SYS_DEV_VMD
)
1784 /* scroll through /sys/dev/block looking for devices attached to
1787 dir
= opendir("/sys/bus/pci/drivers/nvme");
1791 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
1794 /* is 'ent' a device? check that the 'subsystem' link exists and
1795 * that its target matches 'bus'
1797 sprintf(path
, "/sys/bus/pci/drivers/nvme/%s/subsystem",
1799 n
= readlink(path
, link
, sizeof(link
));
1800 if (n
< 0 || n
>= (int)sizeof(link
))
1803 c
= strrchr(link
, '/');
1806 if (strncmp("pci", c
+1, strlen("pci")) != 0)
1809 sprintf(path
, "/sys/bus/pci/drivers/nvme/%s", ent
->d_name
);
1810 /* if not a intel NVMe - skip it*/
1811 if (devpath_to_vendor(path
) != 0x8086)
1814 rp
= realpath(path
, NULL
);
1818 if (path_attached_to_hba(rp
, hba
->path
)) {
1819 printf(" NVMe under VMD : %s\n", rp
);
1828 static void print_found_intel_controllers(struct sys_dev
*elem
)
1830 for (; elem
; elem
= elem
->next
) {
1831 pr_err("found Intel(R) ");
1832 if (elem
->type
== SYS_DEV_SATA
)
1833 fprintf(stderr
, "SATA ");
1834 else if (elem
->type
== SYS_DEV_SAS
)
1835 fprintf(stderr
, "SAS ");
1836 else if (elem
->type
== SYS_DEV_NVME
)
1837 fprintf(stderr
, "NVMe ");
1839 if (elem
->type
== SYS_DEV_VMD
)
1840 fprintf(stderr
, "VMD domain");
1842 fprintf(stderr
, "RAID controller");
1845 fprintf(stderr
, " at %s", elem
->pci_id
);
1846 fprintf(stderr
, ".\n");
1851 static int ahci_get_port_count(const char *hba_path
, int *port_count
)
1858 if ((dir
= opendir(hba_path
)) == NULL
)
1861 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
1864 if ((sscanf(ent
->d_name
, "ata%d", &host
) != 1) &&
1865 ((sscanf(ent
->d_name
, "host%d", &host
) != 1)))
1867 if (*port_count
== 0)
1869 else if (host
< host_base
)
1872 if (host
+ 1 > *port_count
+ host_base
)
1873 *port_count
= host
+ 1 - host_base
;
1879 static void print_imsm_capability(const struct imsm_orom
*orom
)
1881 printf(" Platform : Intel(R) ");
1882 if (orom
->capabilities
== 0 && orom
->driver_features
== 0)
1883 printf("Matrix Storage Manager\n");
1885 printf("Rapid Storage Technology%s\n",
1886 imsm_orom_is_enterprise(orom
) ? " enterprise" : "");
1887 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
)
1888 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
,
1889 orom
->minor_ver
, orom
->hotfix_ver
, orom
->build
);
1890 printf(" RAID Levels :%s%s%s%s%s\n",
1891 imsm_orom_has_raid0(orom
) ? " raid0" : "",
1892 imsm_orom_has_raid1(orom
) ? " raid1" : "",
1893 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
1894 imsm_orom_has_raid10(orom
) ? " raid10" : "",
1895 imsm_orom_has_raid5(orom
) ? " raid5" : "");
1896 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1897 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
1898 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
1899 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
1900 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
1901 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
1902 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
1903 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
1904 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
1905 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
1906 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
1907 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
1908 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
1909 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
1910 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
1911 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
1912 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
1913 printf(" 2TB volumes :%s supported\n",
1914 (orom
->attr
& IMSM_OROM_ATTR_2TB
)?"":" not");
1915 printf(" 2TB disks :%s supported\n",
1916 (orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
)?"":" not");
1917 printf(" Max Disks : %d\n", orom
->tds
);
1918 printf(" Max Volumes : %d per array, %d per %s\n",
1919 orom
->vpa
, orom
->vphba
,
1920 imsm_orom_is_nvme(orom
) ? "platform" : "controller");
1924 static void print_imsm_capability_export(const struct imsm_orom
*orom
)
1926 printf("MD_FIRMWARE_TYPE=imsm\n");
1927 if (orom
->major_ver
|| orom
->minor_ver
|| orom
->hotfix_ver
|| orom
->build
)
1928 printf("IMSM_VERSION=%d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
1929 orom
->hotfix_ver
, orom
->build
);
1930 printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n",
1931 imsm_orom_has_raid0(orom
) ? "raid0 " : "",
1932 imsm_orom_has_raid1(orom
) ? "raid1 " : "",
1933 imsm_orom_has_raid1e(orom
) ? "raid1e " : "",
1934 imsm_orom_has_raid5(orom
) ? "raid10 " : "",
1935 imsm_orom_has_raid10(orom
) ? "raid5 " : "");
1936 printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1937 imsm_orom_has_chunk(orom
, 2) ? "2k " : "",
1938 imsm_orom_has_chunk(orom
, 4) ? "4k " : "",
1939 imsm_orom_has_chunk(orom
, 8) ? "8k " : "",
1940 imsm_orom_has_chunk(orom
, 16) ? "16k " : "",
1941 imsm_orom_has_chunk(orom
, 32) ? "32k " : "",
1942 imsm_orom_has_chunk(orom
, 64) ? "64k " : "",
1943 imsm_orom_has_chunk(orom
, 128) ? "128k " : "",
1944 imsm_orom_has_chunk(orom
, 256) ? "256k " : "",
1945 imsm_orom_has_chunk(orom
, 512) ? "512k " : "",
1946 imsm_orom_has_chunk(orom
, 1024*1) ? "1M " : "",
1947 imsm_orom_has_chunk(orom
, 1024*2) ? "2M " : "",
1948 imsm_orom_has_chunk(orom
, 1024*4) ? "4M " : "",
1949 imsm_orom_has_chunk(orom
, 1024*8) ? "8M " : "",
1950 imsm_orom_has_chunk(orom
, 1024*16) ? "16M " : "",
1951 imsm_orom_has_chunk(orom
, 1024*32) ? "32M " : "",
1952 imsm_orom_has_chunk(orom
, 1024*64) ? "64M " : "");
1953 printf("IMSM_2TB_VOLUMES=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB
) ? "yes" : "no");
1954 printf("IMSM_2TB_DISKS=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) ? "yes" : "no");
1955 printf("IMSM_MAX_DISKS=%d\n",orom
->tds
);
1956 printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom
->vpa
);
1957 printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom
->vphba
);
1960 static int detail_platform_imsm(int verbose
, int enumerate_only
, char *controller_path
)
1962 /* There are two components to imsm platform support, the ahci SATA
1963 * controller and the option-rom. To find the SATA controller we
1964 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
1965 * controller with the Intel vendor id is present. This approach
1966 * allows mdadm to leverage the kernel's ahci detection logic, with the
1967 * caveat that if ahci.ko is not loaded mdadm will not be able to
1968 * detect platform raid capabilities. The option-rom resides in a
1969 * platform "Adapter ROM". We scan for its signature to retrieve the
1970 * platform capabilities. If raid support is disabled in the BIOS the
1971 * option-rom capability structure will not be available.
1973 struct sys_dev
*list
, *hba
;
1978 if (enumerate_only
) {
1979 if (check_env("IMSM_NO_PLATFORM"))
1981 list
= find_intel_devices();
1984 for (hba
= list
; hba
; hba
= hba
->next
) {
1985 if (find_imsm_capability(hba
)) {
1995 list
= find_intel_devices();
1998 pr_err("no active Intel(R) RAID controller found.\n");
2000 } else if (verbose
> 0)
2001 print_found_intel_controllers(list
);
2003 for (hba
= list
; hba
; hba
= hba
->next
) {
2004 if (controller_path
&& (compare_paths(hba
->path
, controller_path
) != 0))
2006 if (!find_imsm_capability(hba
)) {
2008 pr_err("imsm capabilities not found for controller: %s (type %s)\n",
2009 hba
->type
== SYS_DEV_VMD
? vmd_domain_to_controller(hba
, buf
) : hba
->path
,
2010 get_sys_dev_type(hba
->type
));
2016 if (controller_path
&& result
== 1) {
2017 pr_err("no active Intel(R) RAID controller found under %s\n",
2022 const struct orom_entry
*entry
;
2024 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2025 if (entry
->type
== SYS_DEV_VMD
) {
2026 for (hba
= list
; hba
; hba
= hba
->next
) {
2027 if (hba
->type
== SYS_DEV_VMD
) {
2029 print_imsm_capability(&entry
->orom
);
2030 printf(" I/O Controller : %s (%s)\n",
2031 vmd_domain_to_controller(hba
, buf
), get_sys_dev_type(hba
->type
));
2032 if (print_vmd_attached_devs(hba
)) {
2034 pr_err("failed to get devices attached to VMD domain.\n");
2043 print_imsm_capability(&entry
->orom
);
2044 if (entry
->type
== SYS_DEV_NVME
) {
2045 for (hba
= list
; hba
; hba
= hba
->next
) {
2046 if (hba
->type
== SYS_DEV_NVME
)
2047 printf(" NVMe Device : %s\n", hba
->path
);
2053 struct devid_list
*devid
;
2054 for (devid
= entry
->devid_list
; devid
; devid
= devid
->next
) {
2055 hba
= device_by_id(devid
->devid
);
2059 printf(" I/O Controller : %s (%s)\n",
2060 hba
->path
, get_sys_dev_type(hba
->type
));
2061 if (hba
->type
== SYS_DEV_SATA
) {
2062 host_base
= ahci_get_port_count(hba
->path
, &port_count
);
2063 if (ahci_enumerate_ports(hba
->path
, port_count
, host_base
, verbose
)) {
2065 pr_err("failed to enumerate ports on SATA controller at %s.\n", hba
->pci_id
);
2076 static int export_detail_platform_imsm(int verbose
, char *controller_path
)
2078 struct sys_dev
*list
, *hba
;
2081 list
= find_intel_devices();
2084 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n");
2089 for (hba
= list
; hba
; hba
= hba
->next
) {
2090 if (controller_path
&& (compare_paths(hba
->path
,controller_path
) != 0))
2092 if (!find_imsm_capability(hba
) && verbose
> 0) {
2094 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n",
2095 hba
->type
== SYS_DEV_VMD
? vmd_domain_to_controller(hba
, buf
) : hba
->path
);
2101 const struct orom_entry
*entry
;
2103 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
2104 if (entry
->type
== SYS_DEV_VMD
) {
2105 for (hba
= list
; hba
; hba
= hba
->next
)
2106 print_imsm_capability_export(&entry
->orom
);
2109 print_imsm_capability_export(&entry
->orom
);
2117 static int match_home_imsm(struct supertype
*st
, char *homehost
)
2119 /* the imsm metadata format does not specify any host
2120 * identification information. We return -1 since we can never
2121 * confirm nor deny whether a given array is "meant" for this
2122 * host. We rely on compare_super and the 'family_num' fields to
2123 * exclude member disks that do not belong, and we rely on
2124 * mdadm.conf to specify the arrays that should be assembled.
2125 * Auto-assembly may still pick up "foreign" arrays.
2131 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
2133 /* The uuid returned here is used for:
2134 * uuid to put into bitmap file (Create, Grow)
2135 * uuid for backup header when saving critical section (Grow)
2136 * comparing uuids when re-adding a device into an array
2137 * In these cases the uuid required is that of the data-array,
2138 * not the device-set.
2139 * uuid to recognise same set when adding a missing device back
2140 * to an array. This is a uuid for the device-set.
2142 * For each of these we can make do with a truncated
2143 * or hashed uuid rather than the original, as long as
2145 * In each case the uuid required is that of the data-array,
2146 * not the device-set.
2148 /* imsm does not track uuid's so we synthesis one using sha1 on
2149 * - The signature (Which is constant for all imsm array, but no matter)
2150 * - the orig_family_num of the container
2151 * - the index number of the volume
2152 * - the 'serial' number of the volume.
2153 * Hopefully these are all constant.
2155 struct intel_super
*super
= st
->sb
;
2158 struct sha1_ctx ctx
;
2159 struct imsm_dev
*dev
= NULL
;
2162 /* some mdadm versions failed to set ->orig_family_num, in which
2163 * case fall back to ->family_num. orig_family_num will be
2164 * fixed up with the first metadata update.
2166 family_num
= super
->anchor
->orig_family_num
;
2167 if (family_num
== 0)
2168 family_num
= super
->anchor
->family_num
;
2169 sha1_init_ctx(&ctx
);
2170 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
2171 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
2172 if (super
->current_vol
>= 0)
2173 dev
= get_imsm_dev(super
, super
->current_vol
);
2175 __u32 vol
= super
->current_vol
;
2176 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
2177 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
2179 sha1_finish_ctx(&ctx
, buf
);
2180 memcpy(uuid
, buf
, 4*4);
2185 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
2187 __u8
*v
= get_imsm_version(mpb
);
2188 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
2189 char major
[] = { 0, 0, 0 };
2190 char minor
[] = { 0 ,0, 0 };
2191 char patch
[] = { 0, 0, 0 };
2192 char *ver_parse
[] = { major
, minor
, patch
};
2196 while (*v
!= '\0' && v
< end
) {
2197 if (*v
!= '.' && j
< 2)
2198 ver_parse
[i
][j
++] = *v
;
2206 *m
= strtol(minor
, NULL
, 0);
2207 *p
= strtol(patch
, NULL
, 0);
2211 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
2213 /* migr_strip_size when repairing or initializing parity */
2214 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2215 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2217 switch (get_imsm_raid_level(map
)) {
2222 return 128*1024 >> 9;
2226 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
2228 /* migr_strip_size when rebuilding a degraded disk, no idea why
2229 * this is different than migr_strip_size_resync(), but it's good
2232 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2233 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2235 switch (get_imsm_raid_level(map
)) {
2238 if (map
->num_members
% map
->num_domains
== 0)
2239 return 128*1024 >> 9;
2243 return max((__u32
) 64*1024 >> 9, chunk
);
2245 return 128*1024 >> 9;
2249 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
2251 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2252 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2253 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
2254 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
2256 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
2259 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
2261 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2262 int level
= get_imsm_raid_level(lo
);
2264 if (level
== 1 || level
== 10) {
2265 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2267 return hi
->num_domains
;
2269 return num_stripes_per_unit_resync(dev
);
2272 static __u8
imsm_num_data_members(struct imsm_dev
*dev
, int second_map
)
2274 /* named 'imsm_' because raid0, raid1 and raid10
2275 * counter-intuitively have the same number of data disks
2277 struct imsm_map
*map
= get_imsm_map(dev
, second_map
);
2279 switch (get_imsm_raid_level(map
)) {
2281 return map
->num_members
;
2285 return map
->num_members
/2;
2287 return map
->num_members
- 1;
2289 dprintf("unsupported raid level\n");
2294 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
2296 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2297 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2299 switch(get_imsm_raid_level(map
)) {
2302 return chunk
* map
->num_domains
;
2304 return chunk
* map
->num_members
;
2310 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
2312 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2313 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2314 __u32 strip
= block
/ chunk
;
2316 switch (get_imsm_raid_level(map
)) {
2319 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
2320 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
2322 return vol_stripe
* chunk
+ block
% chunk
;
2324 __u32 stripe
= strip
/ (map
->num_members
- 1);
2326 return stripe
* chunk
+ block
% chunk
;
2333 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
2334 struct imsm_dev
*dev
)
2336 /* calculate the conversion factor between per member 'blocks'
2337 * (md/{resync,rebuild}_start) and imsm migration units, return
2338 * 0 for the 'not migrating' and 'unsupported migration' cases
2340 if (!dev
->vol
.migr_state
)
2343 switch (migr_type(dev
)) {
2344 case MIGR_GEN_MIGR
: {
2345 struct migr_record
*migr_rec
= super
->migr_rec
;
2346 return __le32_to_cpu(migr_rec
->blocks_per_unit
);
2351 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2352 __u32 stripes_per_unit
;
2353 __u32 blocks_per_unit
;
2362 /* yes, this is really the translation of migr_units to
2363 * per-member blocks in the 'resync' case
2365 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
2366 migr_chunk
= migr_strip_blocks_resync(dev
);
2367 disks
= imsm_num_data_members(dev
, MAP_0
);
2368 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
2369 stripe
= __le16_to_cpu(map
->blocks_per_strip
) * disks
;
2370 segment
= blocks_per_unit
/ stripe
;
2371 block_rel
= blocks_per_unit
- segment
* stripe
;
2372 parity_depth
= parity_segment_depth(dev
);
2373 block_map
= map_migr_block(dev
, block_rel
);
2374 return block_map
+ parity_depth
* segment
;
2376 case MIGR_REBUILD
: {
2377 __u32 stripes_per_unit
;
2380 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
2381 migr_chunk
= migr_strip_blocks_rebuild(dev
);
2382 return migr_chunk
* stripes_per_unit
;
2384 case MIGR_STATE_CHANGE
:
2390 static int imsm_level_to_layout(int level
)
2398 return ALGORITHM_LEFT_ASYMMETRIC
;
2405 /*******************************************************************************
2406 * Function: read_imsm_migr_rec
2407 * Description: Function reads imsm migration record from last sector of disk
2409 * fd : disk descriptor
2410 * super : metadata info
2414 ******************************************************************************/
2415 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
)
2418 unsigned long long dsize
;
2420 get_dev_size(fd
, NULL
, &dsize
);
2421 if (lseek64(fd
, dsize
- MIGR_REC_POSITION
, SEEK_SET
) < 0) {
2422 pr_err("Cannot seek to anchor block: %s\n",
2426 if (read(fd
, super
->migr_rec_buf
, MIGR_REC_BUF_SIZE
) !=
2427 MIGR_REC_BUF_SIZE
) {
2428 pr_err("Cannot read migr record block: %s\n",
2438 static struct imsm_dev
*imsm_get_device_during_migration(
2439 struct intel_super
*super
)
2442 struct intel_dev
*dv
;
2444 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
2445 if (is_gen_migration(dv
->dev
))
2451 /*******************************************************************************
2452 * Function: load_imsm_migr_rec
2453 * Description: Function reads imsm migration record (it is stored at the last
2456 * super : imsm internal array info
2457 * info : general array info
2461 * -2 : no migration in progress
2462 ******************************************************************************/
2463 static int load_imsm_migr_rec(struct intel_super
*super
, struct mdinfo
*info
)
2470 struct imsm_dev
*dev
;
2471 struct imsm_map
*map
;
2474 /* find map under migration */
2475 dev
= imsm_get_device_during_migration(super
);
2476 /* nothing to load,no migration in progress?
2480 map
= get_imsm_map(dev
, MAP_0
);
2483 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
2484 /* skip spare and failed disks
2486 if (sd
->disk
.raid_disk
< 0)
2488 /* read only from one of the first two slots */
2490 slot
= get_imsm_disk_slot(map
,
2491 sd
->disk
.raid_disk
);
2492 if (map
== NULL
|| slot
> 1 || slot
< 0)
2495 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
2496 fd
= dev_open(nm
, O_RDONLY
);
2502 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
2503 /* skip spare and failed disks
2507 /* read only from one of the first two slots */
2509 slot
= get_imsm_disk_slot(map
, dl
->index
);
2510 if (map
== NULL
|| slot
> 1 || slot
< 0)
2512 sprintf(nm
, "%d:%d", dl
->major
, dl
->minor
);
2513 fd
= dev_open(nm
, O_RDONLY
);
2520 retval
= read_imsm_migr_rec(fd
, super
);
2529 /*******************************************************************************
2530 * function: imsm_create_metadata_checkpoint_update
2531 * Description: It creates update for checkpoint change.
2533 * super : imsm internal array info
2534 * u : pointer to prepared update
2537 * If length is equal to 0, input pointer u contains no update
2538 ******************************************************************************/
2539 static int imsm_create_metadata_checkpoint_update(
2540 struct intel_super
*super
,
2541 struct imsm_update_general_migration_checkpoint
**u
)
2544 int update_memory_size
= 0;
2546 dprintf("(enter)\n");
2552 /* size of all update data without anchor */
2553 update_memory_size
=
2554 sizeof(struct imsm_update_general_migration_checkpoint
);
2556 *u
= xcalloc(1, update_memory_size
);
2558 dprintf("error: cannot get memory\n");
2561 (*u
)->type
= update_general_migration_checkpoint
;
2562 (*u
)->curr_migr_unit
= __le32_to_cpu(super
->migr_rec
->curr_migr_unit
);
2563 dprintf("prepared for %u\n", (*u
)->curr_migr_unit
);
2565 return update_memory_size
;
2568 static void imsm_update_metadata_locally(struct supertype
*st
,
2569 void *buf
, int len
);
2571 /*******************************************************************************
2572 * Function: write_imsm_migr_rec
2573 * Description: Function writes imsm migration record
2574 * (at the last sector of disk)
2576 * super : imsm internal array info
2580 ******************************************************************************/
2581 static int write_imsm_migr_rec(struct supertype
*st
)
2583 struct intel_super
*super
= st
->sb
;
2584 unsigned long long dsize
;
2590 struct imsm_update_general_migration_checkpoint
*u
;
2591 struct imsm_dev
*dev
;
2592 struct imsm_map
*map
;
2594 /* find map under migration */
2595 dev
= imsm_get_device_during_migration(super
);
2596 /* if no migration, write buffer anyway to clear migr_record
2597 * on disk based on first available device
2600 dev
= get_imsm_dev(super
, super
->current_vol
< 0 ? 0 :
2601 super
->current_vol
);
2603 map
= get_imsm_map(dev
, MAP_0
);
2605 for (sd
= super
->disks
; sd
; sd
= sd
->next
) {
2608 /* skip failed and spare devices */
2611 /* write to 2 first slots only */
2613 slot
= get_imsm_disk_slot(map
, sd
->index
);
2614 if (map
== NULL
|| slot
> 1 || slot
< 0)
2617 sprintf(nm
, "%d:%d", sd
->major
, sd
->minor
);
2618 fd
= dev_open(nm
, O_RDWR
);
2621 get_dev_size(fd
, NULL
, &dsize
);
2622 if (lseek64(fd
, dsize
- MIGR_REC_POSITION
, SEEK_SET
) < 0) {
2623 pr_err("Cannot seek to anchor block: %s\n",
2627 if (write(fd
, super
->migr_rec_buf
, MIGR_REC_BUF_SIZE
) !=
2628 MIGR_REC_BUF_SIZE
) {
2629 pr_err("Cannot write migr record block: %s\n",
2636 /* update checkpoint information in metadata */
2637 len
= imsm_create_metadata_checkpoint_update(super
, &u
);
2640 dprintf("imsm: Cannot prepare update\n");
2643 /* update metadata locally */
2644 imsm_update_metadata_locally(st
, u
, len
);
2645 /* and possibly remotely */
2646 if (st
->update_tail
) {
2647 append_metadata_update(st
, u
, len
);
2648 /* during reshape we do all work inside metadata handler
2649 * manage_reshape(), so metadata update has to be triggered
2652 flush_metadata_updates(st
);
2653 st
->update_tail
= &st
->updates
;
2663 #endif /* MDASSEMBLE */
2665 /* spare/missing disks activations are not allowe when
2666 * array/container performs reshape operation, because
2667 * all arrays in container works on the same disks set
2669 int imsm_reshape_blocks_arrays_changes(struct intel_super
*super
)
2672 struct intel_dev
*i_dev
;
2673 struct imsm_dev
*dev
;
2675 /* check whole container
2677 for (i_dev
= super
->devlist
; i_dev
; i_dev
= i_dev
->next
) {
2679 if (is_gen_migration(dev
)) {
2680 /* No repair during any migration in container
2688 static unsigned long long imsm_component_size_aligment_check(int level
,
2690 unsigned long long component_size
)
2692 unsigned int component_size_alligment
;
2694 /* check component size aligment
2696 component_size_alligment
= component_size
% (chunk_size
/512);
2698 dprintf("(Level: %i, chunk_size = %i, component_size = %llu), component_size_alligment = %u\n",
2699 level
, chunk_size
, component_size
,
2700 component_size_alligment
);
2702 if (component_size_alligment
&& (level
!= 1) && (level
!= UnSet
)) {
2703 dprintf("imsm: reported component size alligned from %llu ",
2705 component_size
-= component_size_alligment
;
2706 dprintf_cont("to %llu (%i).\n",
2707 component_size
, component_size_alligment
);
2710 return component_size
;
2713 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
2715 struct intel_super
*super
= st
->sb
;
2716 struct migr_record
*migr_rec
= super
->migr_rec
;
2717 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
2718 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2719 struct imsm_map
*prev_map
= get_imsm_map(dev
, MAP_1
);
2720 struct imsm_map
*map_to_analyse
= map
;
2722 int map_disks
= info
->array
.raid_disks
;
2724 memset(info
, 0, sizeof(*info
));
2726 map_to_analyse
= prev_map
;
2728 dl
= super
->current_disk
;
2730 info
->container_member
= super
->current_vol
;
2731 info
->array
.raid_disks
= map
->num_members
;
2732 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
2733 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
2734 info
->array
.md_minor
= -1;
2735 info
->array
.ctime
= 0;
2736 info
->array
.utime
= 0;
2737 info
->array
.chunk_size
=
2738 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
2739 info
->array
.state
= !dev
->vol
.dirty
;
2740 info
->custom_array_size
= __le32_to_cpu(dev
->size_high
);
2741 info
->custom_array_size
<<= 32;
2742 info
->custom_array_size
|= __le32_to_cpu(dev
->size_low
);
2743 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
2745 if (is_gen_migration(dev
)) {
2746 info
->reshape_active
= 1;
2747 info
->new_level
= get_imsm_raid_level(map
);
2748 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
2749 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
2750 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
2751 if (info
->delta_disks
) {
2752 /* this needs to be applied to every array
2755 info
->reshape_active
= CONTAINER_RESHAPE
;
2757 /* We shape information that we give to md might have to be
2758 * modify to cope with md's requirement for reshaping arrays.
2759 * For example, when reshaping a RAID0, md requires it to be
2760 * presented as a degraded RAID4.
2761 * Also if a RAID0 is migrating to a RAID5 we need to specify
2762 * the array as already being RAID5, but the 'before' layout
2763 * is a RAID4-like layout.
2765 switch (info
->array
.level
) {
2767 switch(info
->new_level
) {
2769 /* conversion is happening as RAID4 */
2770 info
->array
.level
= 4;
2771 info
->array
.raid_disks
+= 1;
2774 /* conversion is happening as RAID5 */
2775 info
->array
.level
= 5;
2776 info
->array
.layout
= ALGORITHM_PARITY_N
;
2777 info
->delta_disks
-= 1;
2780 /* FIXME error message */
2781 info
->array
.level
= UnSet
;
2787 info
->new_level
= UnSet
;
2788 info
->new_layout
= UnSet
;
2789 info
->new_chunk
= info
->array
.chunk_size
;
2790 info
->delta_disks
= 0;
2794 info
->disk
.major
= dl
->major
;
2795 info
->disk
.minor
= dl
->minor
;
2796 info
->disk
.number
= dl
->index
;
2797 info
->disk
.raid_disk
= get_imsm_disk_slot(map_to_analyse
,
2801 info
->data_offset
= pba_of_lba0(map_to_analyse
);
2802 info
->component_size
= blocks_per_member(map_to_analyse
);
2804 info
->component_size
= imsm_component_size_aligment_check(
2806 info
->array
.chunk_size
,
2807 info
->component_size
);
2809 memset(info
->uuid
, 0, sizeof(info
->uuid
));
2810 info
->recovery_start
= MaxSector
;
2812 info
->reshape_progress
= 0;
2813 info
->resync_start
= MaxSector
;
2814 if ((map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
2816 imsm_reshape_blocks_arrays_changes(super
) == 0) {
2817 info
->resync_start
= 0;
2819 if (dev
->vol
.migr_state
) {
2820 switch (migr_type(dev
)) {
2823 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2825 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
2827 info
->resync_start
= blocks_per_unit
* units
;
2830 case MIGR_GEN_MIGR
: {
2831 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2833 __u64 units
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
2834 unsigned long long array_blocks
;
2837 if (__le32_to_cpu(migr_rec
->ascending_migr
) &&
2839 (__le32_to_cpu(migr_rec
->num_migr_units
)-1)) &&
2840 (super
->migr_rec
->rec_status
==
2841 __cpu_to_le32(UNIT_SRC_IN_CP_AREA
)))
2844 info
->reshape_progress
= blocks_per_unit
* units
;
2846 dprintf("IMSM: General Migration checkpoint : %llu (%llu) -> read reshape progress : %llu\n",
2847 (unsigned long long)units
,
2848 (unsigned long long)blocks_per_unit
,
2849 info
->reshape_progress
);
2851 used_disks
= imsm_num_data_members(dev
, MAP_1
);
2852 if (used_disks
> 0) {
2853 array_blocks
= blocks_per_member(map
) *
2855 /* round array size down to closest MB
2857 info
->custom_array_size
= (array_blocks
2858 >> SECT_PER_MB_SHIFT
)
2859 << SECT_PER_MB_SHIFT
;
2863 /* we could emulate the checkpointing of
2864 * 'sync_action=check' migrations, but for now
2865 * we just immediately complete them
2868 /* this is handled by container_content_imsm() */
2869 case MIGR_STATE_CHANGE
:
2870 /* FIXME handle other migrations */
2872 /* we are not dirty, so... */
2873 info
->resync_start
= MaxSector
;
2877 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
2878 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
2880 info
->array
.major_version
= -1;
2881 info
->array
.minor_version
= -2;
2882 sprintf(info
->text_version
, "/%s/%d", st
->container_devnm
, info
->container_member
);
2883 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
2884 uuid_from_super_imsm(st
, info
->uuid
);
2888 for (i
=0; i
<map_disks
; i
++) {
2890 if (i
< info
->array
.raid_disks
) {
2891 struct imsm_disk
*dsk
;
2892 j
= get_imsm_disk_idx(dev
, i
, MAP_X
);
2893 dsk
= get_imsm_disk(super
, j
);
2894 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
2901 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
2902 int failed
, int look_in_map
);
2904 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
2908 static void manage_second_map(struct intel_super
*super
, struct imsm_dev
*dev
)
2910 if (is_gen_migration(dev
)) {
2913 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
2915 failed
= imsm_count_failed(super
, dev
, MAP_1
);
2916 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_1
);
2917 if (map2
->map_state
!= map_state
) {
2918 map2
->map_state
= map_state
;
2919 super
->updates_pending
++;
2925 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
2929 for (d
= super
->missing
; d
; d
= d
->next
)
2930 if (d
->index
== index
)
2935 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
2937 struct intel_super
*super
= st
->sb
;
2938 struct imsm_disk
*disk
;
2939 int map_disks
= info
->array
.raid_disks
;
2940 int max_enough
= -1;
2942 struct imsm_super
*mpb
;
2944 if (super
->current_vol
>= 0) {
2945 getinfo_super_imsm_volume(st
, info
, map
);
2948 memset(info
, 0, sizeof(*info
));
2950 /* Set raid_disks to zero so that Assemble will always pull in valid
2953 info
->array
.raid_disks
= 0;
2954 info
->array
.level
= LEVEL_CONTAINER
;
2955 info
->array
.layout
= 0;
2956 info
->array
.md_minor
= -1;
2957 info
->array
.ctime
= 0; /* N/A for imsm */
2958 info
->array
.utime
= 0;
2959 info
->array
.chunk_size
= 0;
2961 info
->disk
.major
= 0;
2962 info
->disk
.minor
= 0;
2963 info
->disk
.raid_disk
= -1;
2964 info
->reshape_active
= 0;
2965 info
->array
.major_version
= -1;
2966 info
->array
.minor_version
= -2;
2967 strcpy(info
->text_version
, "imsm");
2968 info
->safe_mode_delay
= 0;
2969 info
->disk
.number
= -1;
2970 info
->disk
.state
= 0;
2972 info
->recovery_start
= MaxSector
;
2973 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
2975 /* do we have the all the insync disks that we expect? */
2976 mpb
= super
->anchor
;
2978 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
2979 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
2980 int failed
, enough
, j
, missing
= 0;
2981 struct imsm_map
*map
;
2984 failed
= imsm_count_failed(super
, dev
, MAP_0
);
2985 state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
2986 map
= get_imsm_map(dev
, MAP_0
);
2988 /* any newly missing disks?
2989 * (catches single-degraded vs double-degraded)
2991 for (j
= 0; j
< map
->num_members
; j
++) {
2992 __u32 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
2993 __u32 idx
= ord_to_idx(ord
);
2995 if (!(ord
& IMSM_ORD_REBUILD
) &&
2996 get_imsm_missing(super
, idx
)) {
3002 if (state
== IMSM_T_STATE_FAILED
)
3004 else if (state
== IMSM_T_STATE_DEGRADED
&&
3005 (state
!= map
->map_state
|| missing
))
3007 else /* we're normal, or already degraded */
3009 if (is_gen_migration(dev
) && missing
) {
3010 /* during general migration we need all disks
3011 * that process is running on.
3012 * No new missing disk is allowed.
3016 /* no more checks necessary
3020 /* in the missing/failed disk case check to see
3021 * if at least one array is runnable
3023 max_enough
= max(max_enough
, enough
);
3025 dprintf("enough: %d\n", max_enough
);
3026 info
->container_enough
= max_enough
;
3029 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
3031 disk
= &super
->disks
->disk
;
3032 info
->data_offset
= total_blocks(&super
->disks
->disk
) - reserved
;
3033 info
->component_size
= reserved
;
3034 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
3035 /* we don't change info->disk.raid_disk here because
3036 * this state will be finalized in mdmon after we have
3037 * found the 'most fresh' version of the metadata
3039 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3040 info
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
3043 /* only call uuid_from_super_imsm when this disk is part of a populated container,
3044 * ->compare_super may have updated the 'num_raid_devs' field for spares
3046 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
3047 uuid_from_super_imsm(st
, info
->uuid
);
3049 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
3051 /* I don't know how to compute 'map' on imsm, so use safe default */
3054 for (i
= 0; i
< map_disks
; i
++)
3060 /* allocates memory and fills disk in mdinfo structure
3061 * for each disk in array */
3062 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
3064 struct mdinfo
*mddev
;
3065 struct intel_super
*super
= st
->sb
;
3066 struct imsm_disk
*disk
;
3069 if (!super
|| !super
->disks
)
3072 mddev
= xcalloc(1, sizeof(*mddev
));
3076 tmp
= xcalloc(1, sizeof(*tmp
));
3078 tmp
->next
= mddev
->devs
;
3080 tmp
->disk
.number
= count
++;
3081 tmp
->disk
.major
= dl
->major
;
3082 tmp
->disk
.minor
= dl
->minor
;
3083 tmp
->disk
.state
= is_configured(disk
) ?
3084 (1 << MD_DISK_ACTIVE
) : 0;
3085 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
3086 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
3087 tmp
->disk
.raid_disk
= -1;
3093 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
3094 char *update
, char *devname
, int verbose
,
3095 int uuid_set
, char *homehost
)
3097 /* For 'assemble' and 'force' we need to return non-zero if any
3098 * change was made. For others, the return value is ignored.
3099 * Update options are:
3100 * force-one : This device looks a bit old but needs to be included,
3101 * update age info appropriately.
3102 * assemble: clear any 'faulty' flag to allow this device to
3104 * force-array: Array is degraded but being forced, mark it clean
3105 * if that will be needed to assemble it.
3107 * newdev: not used ????
3108 * grow: Array has gained a new device - this is currently for
3110 * resync: mark as dirty so a resync will happen.
3111 * name: update the name - preserving the homehost
3112 * uuid: Change the uuid of the array to match watch is given
3114 * Following are not relevant for this imsm:
3115 * sparc2.2 : update from old dodgey metadata
3116 * super-minor: change the preferred_minor number
3117 * summaries: update redundant counters.
3118 * homehost: update the recorded homehost
3119 * _reshape_progress: record new reshape_progress position.
3122 struct intel_super
*super
= st
->sb
;
3123 struct imsm_super
*mpb
;
3125 /* we can only update container info */
3126 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
3129 mpb
= super
->anchor
;
3131 if (strcmp(update
, "uuid") == 0) {
3132 /* We take this to mean that the family_num should be updated.
3133 * However that is much smaller than the uuid so we cannot really
3134 * allow an explicit uuid to be given. And it is hard to reliably
3136 * So if !uuid_set we know the current uuid is random and just used
3137 * the first 'int' and copy it to the other 3 positions.
3138 * Otherwise we require the 4 'int's to be the same as would be the
3139 * case if we are using a random uuid. So an explicit uuid will be
3140 * accepted as long as all for ints are the same... which shouldn't hurt
3143 info
->uuid
[1] = info
->uuid
[2] = info
->uuid
[3] = info
->uuid
[0];
3146 if (info
->uuid
[0] != info
->uuid
[1] ||
3147 info
->uuid
[1] != info
->uuid
[2] ||
3148 info
->uuid
[2] != info
->uuid
[3])
3154 mpb
->orig_family_num
= info
->uuid
[0];
3155 } else if (strcmp(update
, "assemble") == 0)
3160 /* successful update? recompute checksum */
3162 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
3167 static size_t disks_to_mpb_size(int disks
)
3171 size
= sizeof(struct imsm_super
);
3172 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
3173 size
+= 2 * sizeof(struct imsm_dev
);
3174 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
3175 size
+= (4 - 2) * sizeof(struct imsm_map
);
3176 /* 4 possible disk_ord_tbl's */
3177 size
+= 4 * (disks
- 1) * sizeof(__u32
);
3182 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
,
3183 unsigned long long data_offset
)
3185 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
3188 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
3191 static void free_devlist(struct intel_super
*super
)
3193 struct intel_dev
*dv
;
3195 while (super
->devlist
) {
3196 dv
= super
->devlist
->next
;
3197 free(super
->devlist
->dev
);
3198 free(super
->devlist
);
3199 super
->devlist
= dv
;
3203 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
3205 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
3208 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
3212 * 0 same, or first was empty, and second was copied
3213 * 1 second had wrong number
3215 * 3 wrong other info
3217 struct intel_super
*first
= st
->sb
;
3218 struct intel_super
*sec
= tst
->sb
;
3225 /* in platform dependent environment test if the disks
3226 * use the same Intel hba
3227 * If not on Intel hba at all, allow anything.
3229 if (!check_env("IMSM_NO_PLATFORM") && first
->hba
&& sec
->hba
) {
3230 if (first
->hba
->type
!= sec
->hba
->type
) {
3232 "HBAs of devices do not match %s != %s\n",
3233 get_sys_dev_type(first
->hba
->type
),
3234 get_sys_dev_type(sec
->hba
->type
));
3237 if (first
->orom
!= sec
->orom
) {
3239 "HBAs of devices do not match %s != %s\n",
3240 first
->hba
->pci_id
, sec
->hba
->pci_id
);
3245 /* if an anchor does not have num_raid_devs set then it is a free
3248 if (first
->anchor
->num_raid_devs
> 0 &&
3249 sec
->anchor
->num_raid_devs
> 0) {
3250 /* Determine if these disks might ever have been
3251 * related. Further disambiguation can only take place
3252 * in load_super_imsm_all
3254 __u32 first_family
= first
->anchor
->orig_family_num
;
3255 __u32 sec_family
= sec
->anchor
->orig_family_num
;
3257 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
3258 MAX_SIGNATURE_LENGTH
) != 0)
3261 if (first_family
== 0)
3262 first_family
= first
->anchor
->family_num
;
3263 if (sec_family
== 0)
3264 sec_family
= sec
->anchor
->family_num
;
3266 if (first_family
!= sec_family
)
3271 /* if 'first' is a spare promote it to a populated mpb with sec's
3274 if (first
->anchor
->num_raid_devs
== 0 &&
3275 sec
->anchor
->num_raid_devs
> 0) {
3277 struct intel_dev
*dv
;
3278 struct imsm_dev
*dev
;
3280 /* we need to copy raid device info from sec if an allocation
3281 * fails here we don't associate the spare
3283 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
3284 dv
= xmalloc(sizeof(*dv
));
3285 dev
= xmalloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
3288 dv
->next
= first
->devlist
;
3289 first
->devlist
= dv
;
3291 if (i
< sec
->anchor
->num_raid_devs
) {
3292 /* allocation failure */
3293 free_devlist(first
);
3294 pr_err("imsm: failed to associate spare\n");
3297 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
3298 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
3299 first
->anchor
->family_num
= sec
->anchor
->family_num
;
3300 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
3301 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
3302 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
3308 static void fd2devname(int fd
, char *name
)
3312 char dname
[PATH_MAX
];
3317 if (fstat(fd
, &st
) != 0)
3319 sprintf(path
, "/sys/dev/block/%d:%d",
3320 major(st
.st_rdev
), minor(st
.st_rdev
));
3322 rv
= readlink(path
, dname
, sizeof(dname
)-1);
3327 nm
= strrchr(dname
, '/');
3330 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
3334 static int nvme_get_serial(int fd
, void *buf
, size_t buf_len
)
3337 char *name
= fd2kname(fd
);
3342 if (strncmp(name
, "nvme", 4) != 0)
3345 snprintf(path
, sizeof(path
) - 1, "/sys/block/%s/device/serial", name
);
3347 return load_sys(path
, buf
, buf_len
);
3350 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
3352 static int imsm_read_serial(int fd
, char *devname
,
3353 __u8 serial
[MAX_RAID_SERIAL_LEN
])
3362 memset(buf
, 0, sizeof(buf
));
3364 rv
= nvme_get_serial(fd
, buf
, sizeof(buf
));
3367 rv
= scsi_get_serial(fd
, buf
, sizeof(buf
));
3369 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
3370 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3371 fd2devname(fd
, (char *) serial
);
3377 pr_err("Failed to retrieve serial for %s\n",
3382 /* trim all whitespace and non-printable characters and convert
3385 for (i
= 0, dest
= buf
; i
< sizeof(buf
) && buf
[i
]; i
++) {
3388 /* ':' is reserved for use in placeholder serial
3389 * numbers for missing disks
3400 /* truncate leading characters */
3401 if (len
> MAX_RAID_SERIAL_LEN
) {
3402 dest
+= len
- MAX_RAID_SERIAL_LEN
;
3403 len
= MAX_RAID_SERIAL_LEN
;
3406 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3407 memcpy(serial
, dest
, len
);
3412 static int serialcmp(__u8
*s1
, __u8
*s2
)
3414 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
3417 static void serialcpy(__u8
*dest
, __u8
*src
)
3419 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
3422 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
3426 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3427 if (serialcmp(dl
->serial
, serial
) == 0)
3433 static struct imsm_disk
*
3434 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
3438 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3439 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3441 if (serialcmp(disk
->serial
, serial
) == 0) {
3452 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3454 struct imsm_disk
*disk
;
3459 __u8 serial
[MAX_RAID_SERIAL_LEN
];
3461 rv
= imsm_read_serial(fd
, devname
, serial
);
3466 dl
= xcalloc(1, sizeof(*dl
));
3469 dl
->major
= major(stb
.st_rdev
);
3470 dl
->minor
= minor(stb
.st_rdev
);
3471 dl
->next
= super
->disks
;
3472 dl
->fd
= keep_fd
? fd
: -1;
3473 assert(super
->disks
== NULL
);
3475 serialcpy(dl
->serial
, serial
);
3478 fd2devname(fd
, name
);
3480 dl
->devname
= xstrdup(devname
);
3482 dl
->devname
= xstrdup(name
);
3484 /* look up this disk's index in the current anchor */
3485 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
3488 /* only set index on disks that are a member of a
3489 * populated contianer, i.e. one with raid_devs
3491 if (is_failed(&dl
->disk
))
3493 else if (is_spare(&dl
->disk
))
3501 /* When migrating map0 contains the 'destination' state while map1
3502 * contains the current state. When not migrating map0 contains the
3503 * current state. This routine assumes that map[0].map_state is set to
3504 * the current array state before being called.
3506 * Migration is indicated by one of the following states
3507 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
3508 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
3509 * map1state=unitialized)
3510 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
3512 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
3513 * map1state=degraded)
3514 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
3517 static void migrate(struct imsm_dev
*dev
, struct intel_super
*super
,
3518 __u8 to_state
, int migr_type
)
3520 struct imsm_map
*dest
;
3521 struct imsm_map
*src
= get_imsm_map(dev
, MAP_0
);
3523 dev
->vol
.migr_state
= 1;
3524 set_migr_type(dev
, migr_type
);
3525 dev
->vol
.curr_migr_unit
= 0;
3526 dest
= get_imsm_map(dev
, MAP_1
);
3528 /* duplicate and then set the target end state in map[0] */
3529 memcpy(dest
, src
, sizeof_imsm_map(src
));
3530 if (migr_type
== MIGR_REBUILD
|| migr_type
== MIGR_GEN_MIGR
) {
3534 for (i
= 0; i
< src
->num_members
; i
++) {
3535 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
3536 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
3540 if (migr_type
== MIGR_GEN_MIGR
)
3541 /* Clear migration record */
3542 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
3544 src
->map_state
= to_state
;
3547 static void end_migration(struct imsm_dev
*dev
, struct intel_super
*super
,
3550 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3551 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
== 0 ?
3555 /* merge any IMSM_ORD_REBUILD bits that were not successfully
3556 * completed in the last migration.
3558 * FIXME add support for raid-level-migration
3560 if (map_state
!= map
->map_state
&& (is_gen_migration(dev
) == 0) &&
3561 prev
->map_state
!= IMSM_T_STATE_UNINITIALIZED
) {
3562 /* when final map state is other than expected
3563 * merge maps (not for migration)
3567 for (i
= 0; i
< prev
->num_members
; i
++)
3568 for (j
= 0; j
< map
->num_members
; j
++)
3569 /* during online capacity expansion
3570 * disks position can be changed
3571 * if takeover is used
3573 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
3574 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
3575 map
->disk_ord_tbl
[j
] |=
3576 prev
->disk_ord_tbl
[i
];
3579 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3580 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3583 dev
->vol
.migr_state
= 0;
3584 set_migr_type(dev
, 0);
3585 dev
->vol
.curr_migr_unit
= 0;
3586 map
->map_state
= map_state
;
3590 static int parse_raid_devices(struct intel_super
*super
)
3593 struct imsm_dev
*dev_new
;
3594 size_t len
, len_migr
;
3596 size_t space_needed
= 0;
3597 struct imsm_super
*mpb
= super
->anchor
;
3599 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3600 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3601 struct intel_dev
*dv
;
3603 len
= sizeof_imsm_dev(dev_iter
, 0);
3604 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
3606 space_needed
+= len_migr
- len
;
3608 dv
= xmalloc(sizeof(*dv
));
3609 if (max_len
< len_migr
)
3611 if (max_len
> len_migr
)
3612 space_needed
+= max_len
- len_migr
;
3613 dev_new
= xmalloc(max_len
);
3614 imsm_copy_dev(dev_new
, dev_iter
);
3617 dv
->next
= super
->devlist
;
3618 super
->devlist
= dv
;
3621 /* ensure that super->buf is large enough when all raid devices
3624 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
3627 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
, 512);
3628 if (posix_memalign(&buf
, 512, len
) != 0)
3631 memcpy(buf
, super
->buf
, super
->len
);
3632 memset(buf
+ super
->len
, 0, len
- super
->len
);
3641 /* retrieve a pointer to the bbm log which starts after all raid devices */
3642 struct bbm_log
*__get_imsm_bbm_log(struct imsm_super
*mpb
)
3646 if (__le32_to_cpu(mpb
->bbm_log_size
)) {
3648 ptr
+= mpb
->mpb_size
- __le32_to_cpu(mpb
->bbm_log_size
);
3654 /*******************************************************************************
3655 * Function: check_mpb_migr_compatibility
3656 * Description: Function checks for unsupported migration features:
3657 * - migration optimization area (pba_of_lba0)
3658 * - descending reshape (ascending_migr)
3660 * super : imsm metadata information
3662 * 0 : migration is compatible
3663 * -1 : migration is not compatible
3664 ******************************************************************************/
3665 int check_mpb_migr_compatibility(struct intel_super
*super
)
3667 struct imsm_map
*map0
, *map1
;
3668 struct migr_record
*migr_rec
= super
->migr_rec
;
3671 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3672 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3675 dev_iter
->vol
.migr_state
== 1 &&
3676 dev_iter
->vol
.migr_type
== MIGR_GEN_MIGR
) {
3677 /* This device is migrating */
3678 map0
= get_imsm_map(dev_iter
, MAP_0
);
3679 map1
= get_imsm_map(dev_iter
, MAP_1
);
3680 if (pba_of_lba0(map0
) != pba_of_lba0(map1
))
3681 /* migration optimization area was used */
3683 if (migr_rec
->ascending_migr
== 0
3684 && migr_rec
->dest_depth_per_unit
> 0)
3685 /* descending reshape not supported yet */
3692 static void __free_imsm(struct intel_super
*super
, int free_disks
);
3694 /* load_imsm_mpb - read matrix metadata
3695 * allocates super->mpb to be freed by free_imsm
3697 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
3699 unsigned long long dsize
;
3700 unsigned long long sectors
;
3702 struct imsm_super
*anchor
;
3705 get_dev_size(fd
, NULL
, &dsize
);
3708 pr_err("%s: device to small for imsm\n",
3713 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0) {
3715 pr_err("Cannot seek to anchor block on %s: %s\n",
3716 devname
, strerror(errno
));
3720 if (posix_memalign((void**)&anchor
, 512, 512) != 0) {
3722 pr_err("Failed to allocate imsm anchor buffer on %s\n", devname
);
3725 if (read(fd
, anchor
, 512) != 512) {
3727 pr_err("Cannot read anchor block on %s: %s\n",
3728 devname
, strerror(errno
));
3733 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
3735 pr_err("no IMSM anchor on %s\n", devname
);
3740 __free_imsm(super
, 0);
3741 /* reload capability and hba */
3743 /* capability and hba must be updated with new super allocation */
3744 find_intel_hba_capability(fd
, super
, devname
);
3745 super
->len
= ROUND_UP(anchor
->mpb_size
, 512);
3746 if (posix_memalign(&super
->buf
, 512, super
->len
) != 0) {
3748 pr_err("unable to allocate %zu byte mpb buffer\n",
3753 memcpy(super
->buf
, anchor
, 512);
3755 sectors
= mpb_sectors(anchor
) - 1;
3758 if (posix_memalign(&super
->migr_rec_buf
, 512, MIGR_REC_BUF_SIZE
) != 0) {
3759 pr_err("could not allocate migr_rec buffer\n");
3763 super
->clean_migration_record_by_mdmon
= 0;
3766 check_sum
= __gen_imsm_checksum(super
->anchor
);
3767 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3769 pr_err("IMSM checksum %x != %x on %s\n",
3771 __le32_to_cpu(super
->anchor
->check_sum
),
3779 /* read the extended mpb */
3780 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0) {
3782 pr_err("Cannot seek to extended mpb on %s: %s\n",
3783 devname
, strerror(errno
));
3787 if ((unsigned)read(fd
, super
->buf
+ 512, super
->len
- 512) != super
->len
- 512) {
3789 pr_err("Cannot read extended mpb on %s: %s\n",
3790 devname
, strerror(errno
));
3794 check_sum
= __gen_imsm_checksum(super
->anchor
);
3795 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3797 pr_err("IMSM checksum %x != %x on %s\n",
3798 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
3803 /* FIXME the BBM log is disk specific so we cannot use this global
3804 * buffer for all disks. Ok for now since we only look at the global
3805 * bbm_log_size parameter to gate assembly
3807 super
->bbm_log
= __get_imsm_bbm_log(super
->anchor
);
3812 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
);
3814 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
3815 static void clear_hi(struct intel_super
*super
)
3817 struct imsm_super
*mpb
= super
->anchor
;
3819 if (mpb
->attributes
& MPB_ATTRIB_2TB_DISK
)
3821 for (i
= 0; i
< mpb
->num_disks
; ++i
) {
3822 struct imsm_disk
*disk
= &mpb
->disk
[i
];
3823 disk
->total_blocks_hi
= 0;
3825 for (i
= 0; i
< mpb
->num_raid_devs
; ++i
) {
3826 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3829 for (n
= 0; n
< 2; ++n
) {
3830 struct imsm_map
*map
= get_imsm_map(dev
, n
);
3833 map
->pba_of_lba0_hi
= 0;
3834 map
->blocks_per_member_hi
= 0;
3835 map
->num_data_stripes_hi
= 0;
3841 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3845 err
= load_imsm_mpb(fd
, super
, devname
);
3848 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
3851 err
= parse_raid_devices(super
);
3856 static void __free_imsm_disk(struct dl
*d
)
3868 static void free_imsm_disks(struct intel_super
*super
)
3872 while (super
->disks
) {
3874 super
->disks
= d
->next
;
3875 __free_imsm_disk(d
);
3877 while (super
->disk_mgmt_list
) {
3878 d
= super
->disk_mgmt_list
;
3879 super
->disk_mgmt_list
= d
->next
;
3880 __free_imsm_disk(d
);
3882 while (super
->missing
) {
3884 super
->missing
= d
->next
;
3885 __free_imsm_disk(d
);
3890 /* free all the pieces hanging off of a super pointer */
3891 static void __free_imsm(struct intel_super
*super
, int free_disks
)
3893 struct intel_hba
*elem
, *next
;
3899 /* unlink capability description */
3901 if (super
->migr_rec_buf
) {
3902 free(super
->migr_rec_buf
);
3903 super
->migr_rec_buf
= NULL
;
3906 free_imsm_disks(super
);
3907 free_devlist(super
);
3911 free((void *)elem
->path
);
3919 static void free_imsm(struct intel_super
*super
)
3921 __free_imsm(super
, 1);
3925 static void free_super_imsm(struct supertype
*st
)
3927 struct intel_super
*super
= st
->sb
;
3936 static struct intel_super
*alloc_super(void)
3938 struct intel_super
*super
= xcalloc(1, sizeof(*super
));
3940 super
->current_vol
= -1;
3941 super
->create_offset
= ~((unsigned long long) 0);
3946 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
3948 static int find_intel_hba_capability(int fd
, struct intel_super
*super
, char *devname
)
3950 struct sys_dev
*hba_name
;
3953 if (fd
< 0 || check_env("IMSM_NO_PLATFORM")) {
3958 hba_name
= find_disk_attached_hba(fd
, NULL
);
3961 pr_err("%s is not attached to Intel(R) RAID controller.\n",
3965 rv
= attach_hba_to_super(super
, hba_name
);
3968 struct intel_hba
*hba
= super
->hba
;
3970 pr_err("%s is attached to Intel(R) %s %s (%s),\n"
3971 " but the container is assigned to Intel(R) %s %s (",
3973 get_sys_dev_type(hba_name
->type
),
3974 hba_name
->type
== SYS_DEV_VMD
? "domain" : "RAID controller",
3975 hba_name
->pci_id
? : "Err!",
3976 get_sys_dev_type(super
->hba
->type
),
3977 hba
->type
== SYS_DEV_VMD
? "domain" : "RAID controller");
3980 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
3982 fprintf(stderr
, ", ");
3985 fprintf(stderr
, ").\n"
3986 " Mixing devices attached to different %s is not allowed.\n",
3987 hba_name
->type
== SYS_DEV_VMD
? "VMD domains" : "controllers");
3991 super
->orom
= find_imsm_capability(hba_name
);
3998 /* find_missing - helper routine for load_super_imsm_all that identifies
3999 * disks that have disappeared from the system. This routine relies on
4000 * the mpb being uptodate, which it is at load time.
4002 static int find_missing(struct intel_super
*super
)
4005 struct imsm_super
*mpb
= super
->anchor
;
4007 struct imsm_disk
*disk
;
4009 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4010 disk
= __get_imsm_disk(mpb
, i
);
4011 dl
= serial_to_dl(disk
->serial
, super
);
4015 dl
= xmalloc(sizeof(*dl
));
4019 dl
->devname
= xstrdup("missing");
4021 serialcpy(dl
->serial
, disk
->serial
);
4024 dl
->next
= super
->missing
;
4025 super
->missing
= dl
;
4032 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
4034 struct intel_disk
*idisk
= disk_list
;
4037 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
4039 idisk
= idisk
->next
;
4045 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
4046 struct intel_super
*super
,
4047 struct intel_disk
**disk_list
)
4049 struct imsm_disk
*d
= &super
->disks
->disk
;
4050 struct imsm_super
*mpb
= super
->anchor
;
4053 for (i
= 0; i
< tbl_size
; i
++) {
4054 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
4055 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
4057 if (tbl_mpb
->family_num
== mpb
->family_num
) {
4058 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
4059 dprintf("mpb from %d:%d matches %d:%d\n",
4060 super
->disks
->major
,
4061 super
->disks
->minor
,
4062 table
[i
]->disks
->major
,
4063 table
[i
]->disks
->minor
);
4067 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
4068 is_configured(d
) == is_configured(tbl_d
)) &&
4069 tbl_mpb
->generation_num
< mpb
->generation_num
) {
4070 /* current version of the mpb is a
4071 * better candidate than the one in
4072 * super_table, but copy over "cross
4073 * generational" status
4075 struct intel_disk
*idisk
;
4077 dprintf("mpb from %d:%d replaces %d:%d\n",
4078 super
->disks
->major
,
4079 super
->disks
->minor
,
4080 table
[i
]->disks
->major
,
4081 table
[i
]->disks
->minor
);
4083 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
4084 if (idisk
&& is_failed(&idisk
->disk
))
4085 tbl_d
->status
|= FAILED_DISK
;
4088 struct intel_disk
*idisk
;
4089 struct imsm_disk
*disk
;
4091 /* tbl_mpb is more up to date, but copy
4092 * over cross generational status before
4095 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
4096 if (disk
&& is_failed(disk
))
4097 d
->status
|= FAILED_DISK
;
4099 idisk
= disk_list_get(d
->serial
, *disk_list
);
4102 if (disk
&& is_configured(disk
))
4103 idisk
->disk
.status
|= CONFIGURED_DISK
;
4106 dprintf("mpb from %d:%d prefer %d:%d\n",
4107 super
->disks
->major
,
4108 super
->disks
->minor
,
4109 table
[i
]->disks
->major
,
4110 table
[i
]->disks
->minor
);
4118 table
[tbl_size
++] = super
;
4122 /* update/extend the merged list of imsm_disk records */
4123 for (j
= 0; j
< mpb
->num_disks
; j
++) {
4124 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
4125 struct intel_disk
*idisk
;
4127 idisk
= disk_list_get(disk
->serial
, *disk_list
);
4129 idisk
->disk
.status
|= disk
->status
;
4130 if (is_configured(&idisk
->disk
) ||
4131 is_failed(&idisk
->disk
))
4132 idisk
->disk
.status
&= ~(SPARE_DISK
);
4134 idisk
= xcalloc(1, sizeof(*idisk
));
4135 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
4136 idisk
->disk
= *disk
;
4137 idisk
->next
= *disk_list
;
4141 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
4148 static struct intel_super
*
4149 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
4152 struct imsm_super
*mpb
= super
->anchor
;
4156 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4157 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
4158 struct intel_disk
*idisk
;
4160 idisk
= disk_list_get(disk
->serial
, disk_list
);
4162 if (idisk
->owner
== owner
||
4163 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
4166 dprintf("'%.16s' owner %d != %d\n",
4167 disk
->serial
, idisk
->owner
,
4170 dprintf("unknown disk %x [%d]: %.16s\n",
4171 __le32_to_cpu(mpb
->family_num
), i
,
4177 if (ok_count
== mpb
->num_disks
)
4182 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
4184 struct intel_super
*s
;
4186 for (s
= super_list
; s
; s
= s
->next
) {
4187 if (family_num
!= s
->anchor
->family_num
)
4189 pr_err("Conflict, offlining family %#x on '%s'\n",
4190 __le32_to_cpu(family_num
), s
->disks
->devname
);
4194 static struct intel_super
*
4195 imsm_thunderdome(struct intel_super
**super_list
, int len
)
4197 struct intel_super
*super_table
[len
];
4198 struct intel_disk
*disk_list
= NULL
;
4199 struct intel_super
*champion
, *spare
;
4200 struct intel_super
*s
, **del
;
4205 memset(super_table
, 0, sizeof(super_table
));
4206 for (s
= *super_list
; s
; s
= s
->next
)
4207 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
4209 for (i
= 0; i
< tbl_size
; i
++) {
4210 struct imsm_disk
*d
;
4211 struct intel_disk
*idisk
;
4212 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
4215 d
= &s
->disks
->disk
;
4217 /* 'd' must appear in merged disk list for its
4218 * configuration to be valid
4220 idisk
= disk_list_get(d
->serial
, disk_list
);
4221 if (idisk
&& idisk
->owner
== i
)
4222 s
= validate_members(s
, disk_list
, i
);
4227 dprintf("marking family: %#x from %d:%d offline\n",
4229 super_table
[i
]->disks
->major
,
4230 super_table
[i
]->disks
->minor
);
4234 /* This is where the mdadm implementation differs from the Windows
4235 * driver which has no strict concept of a container. We can only
4236 * assemble one family from a container, so when returning a prodigal
4237 * array member to this system the code will not be able to disambiguate
4238 * the container contents that should be assembled ("foreign" versus
4239 * "local"). It requires user intervention to set the orig_family_num
4240 * to a new value to establish a new container. The Windows driver in
4241 * this situation fixes up the volume name in place and manages the
4242 * foreign array as an independent entity.
4247 for (i
= 0; i
< tbl_size
; i
++) {
4248 struct intel_super
*tbl_ent
= super_table
[i
];
4254 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
4259 if (s
&& !is_spare
) {
4260 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
4262 } else if (!s
&& !is_spare
)
4275 pr_err("Chose family %#x on '%s', assemble conflicts to new container with '--update=uuid'\n",
4276 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
4278 /* collect all dl's onto 'champion', and update them to
4279 * champion's version of the status
4281 for (s
= *super_list
; s
; s
= s
->next
) {
4282 struct imsm_super
*mpb
= champion
->anchor
;
4283 struct dl
*dl
= s
->disks
;
4288 mpb
->attributes
|= s
->anchor
->attributes
& MPB_ATTRIB_2TB_DISK
;
4290 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4291 struct imsm_disk
*disk
;
4293 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
4296 /* only set index on disks that are a member of
4297 * a populated contianer, i.e. one with
4300 if (is_failed(&dl
->disk
))
4302 else if (is_spare(&dl
->disk
))
4308 if (i
>= mpb
->num_disks
) {
4309 struct intel_disk
*idisk
;
4311 idisk
= disk_list_get(dl
->serial
, disk_list
);
4312 if (idisk
&& is_spare(&idisk
->disk
) &&
4313 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
4321 dl
->next
= champion
->disks
;
4322 champion
->disks
= dl
;
4326 /* delete 'champion' from super_list */
4327 for (del
= super_list
; *del
; ) {
4328 if (*del
== champion
) {
4329 *del
= (*del
)->next
;
4332 del
= &(*del
)->next
;
4334 champion
->next
= NULL
;
4338 struct intel_disk
*idisk
= disk_list
;
4340 disk_list
= disk_list
->next
;
4348 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
);
4349 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
4350 int major
, int minor
, int keep_fd
);
4352 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4353 int *max
, int keep_fd
);
4355 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
4356 char *devname
, struct md_list
*devlist
,
4359 struct intel_super
*super_list
= NULL
;
4360 struct intel_super
*super
= NULL
;
4365 /* 'fd' is an opened container */
4366 err
= get_sra_super_block(fd
, &super_list
, devname
, &i
, keep_fd
);
4368 /* get super block from devlist devices */
4369 err
= get_devlist_super_block(devlist
, &super_list
, &i
, keep_fd
);
4372 /* all mpbs enter, maybe one leaves */
4373 super
= imsm_thunderdome(&super_list
, i
);
4379 if (find_missing(super
) != 0) {
4385 /* load migration record */
4386 err
= load_imsm_migr_rec(super
, NULL
);
4388 /* migration is in progress,
4389 * but migr_rec cannot be loaded,
4395 /* Check migration compatibility */
4396 if (err
== 0 && check_mpb_migr_compatibility(super
) != 0) {
4397 pr_err("Unsupported migration detected");
4399 fprintf(stderr
, " on %s\n", devname
);
4401 fprintf(stderr
, " (IMSM).\n");
4410 while (super_list
) {
4411 struct intel_super
*s
= super_list
;
4413 super_list
= super_list
->next
;
4422 strcpy(st
->container_devnm
, fd2devnm(fd
));
4424 st
->container_devnm
[0] = 0;
4425 if (err
== 0 && st
->ss
== NULL
) {
4426 st
->ss
= &super_imsm
;
4427 st
->minor_version
= 0;
4428 st
->max_devs
= IMSM_MAX_DEVICES
;
4434 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4435 int *max
, int keep_fd
)
4437 struct md_list
*tmpdev
;
4441 for (i
= 0, tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
4442 if (tmpdev
->used
!= 1)
4444 if (tmpdev
->container
== 1) {
4446 int fd
= dev_open(tmpdev
->devname
, O_RDONLY
|O_EXCL
);
4448 pr_err("cannot open device %s: %s\n",
4449 tmpdev
->devname
, strerror(errno
));
4453 err
= get_sra_super_block(fd
, super_list
,
4454 tmpdev
->devname
, &lmax
,
4463 int major
= major(tmpdev
->st_rdev
);
4464 int minor
= minor(tmpdev
->st_rdev
);
4465 err
= get_super_block(super_list
,
4482 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
4483 int major
, int minor
, int keep_fd
)
4485 struct intel_super
*s
;
4497 sprintf(nm
, "%d:%d", major
, minor
);
4498 dfd
= dev_open(nm
, O_RDWR
);
4504 find_intel_hba_capability(dfd
, s
, devname
);
4505 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4507 /* retry the load if we might have raced against mdmon */
4508 if (err
== 3 && devnm
&& mdmon_running(devnm
))
4509 for (retry
= 0; retry
< 3; retry
++) {
4511 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4517 s
->next
= *super_list
;
4525 if (dfd
>= 0 && !keep_fd
)
4532 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
)
4539 sra
= sysfs_read(fd
, NULL
, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
4543 if (sra
->array
.major_version
!= -1 ||
4544 sra
->array
.minor_version
!= -2 ||
4545 strcmp(sra
->text_version
, "imsm") != 0) {
4550 devnm
= fd2devnm(fd
);
4551 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
4552 if (get_super_block(super_list
, devnm
, devname
,
4553 sd
->disk
.major
, sd
->disk
.minor
, keep_fd
) != 0) {
4564 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
4566 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
, NULL
, 1);
4570 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
4572 struct intel_super
*super
;
4576 if (test_partition(fd
))
4577 /* IMSM not allowed on partitions */
4580 free_super_imsm(st
);
4582 super
= alloc_super();
4583 /* Load hba and capabilities if they exist.
4584 * But do not preclude loading metadata in case capabilities or hba are
4585 * non-compliant and ignore_hw_compat is set.
4587 rv
= find_intel_hba_capability(fd
, super
, devname
);
4588 /* no orom/efi or non-intel hba of the disk */
4589 if (rv
!= 0 && st
->ignore_hw_compat
== 0) {
4591 pr_err("No OROM/EFI properties for %s\n", devname
);
4595 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
4597 /* retry the load if we might have raced against mdmon */
4599 struct mdstat_ent
*mdstat
= NULL
;
4600 char *name
= fd2kname(fd
);
4603 mdstat
= mdstat_by_component(name
);
4605 if (mdstat
&& mdmon_running(mdstat
->devnm
) && getpid() != mdmon_pid(mdstat
->devnm
)) {
4606 for (retry
= 0; retry
< 3; retry
++) {
4608 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
4614 free_mdstat(mdstat
);
4619 pr_err("Failed to load all information sections on %s\n", devname
);
4625 if (st
->ss
== NULL
) {
4626 st
->ss
= &super_imsm
;
4627 st
->minor_version
= 0;
4628 st
->max_devs
= IMSM_MAX_DEVICES
;
4631 /* load migration record */
4632 if (load_imsm_migr_rec(super
, NULL
) == 0) {
4633 /* Check for unsupported migration features */
4634 if (check_mpb_migr_compatibility(super
) != 0) {
4635 pr_err("Unsupported migration detected");
4637 fprintf(stderr
, " on %s\n", devname
);
4639 fprintf(stderr
, " (IMSM).\n");
4647 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
4649 if (info
->level
== 1)
4651 return info
->chunk_size
>> 9;
4654 static unsigned long long info_to_blocks_per_member(mdu_array_info_t
*info
,
4655 unsigned long long size
)
4657 if (info
->level
== 1)
4660 return (size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
4663 static void imsm_update_version_info(struct intel_super
*super
)
4665 /* update the version and attributes */
4666 struct imsm_super
*mpb
= super
->anchor
;
4668 struct imsm_dev
*dev
;
4669 struct imsm_map
*map
;
4672 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4673 dev
= get_imsm_dev(super
, i
);
4674 map
= get_imsm_map(dev
, MAP_0
);
4675 if (__le32_to_cpu(dev
->size_high
) > 0)
4676 mpb
->attributes
|= MPB_ATTRIB_2TB
;
4678 /* FIXME detect when an array spans a port multiplier */
4680 mpb
->attributes
|= MPB_ATTRIB_PM
;
4683 if (mpb
->num_raid_devs
> 1 ||
4684 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
4685 version
= MPB_VERSION_ATTRIBS
;
4686 switch (get_imsm_raid_level(map
)) {
4687 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
4688 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
4689 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
4690 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
4693 if (map
->num_members
>= 5)
4694 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
4695 else if (dev
->status
== DEV_CLONE_N_GO
)
4696 version
= MPB_VERSION_CNG
;
4697 else if (get_imsm_raid_level(map
) == 5)
4698 version
= MPB_VERSION_RAID5
;
4699 else if (map
->num_members
>= 3)
4700 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
4701 else if (get_imsm_raid_level(map
) == 1)
4702 version
= MPB_VERSION_RAID1
;
4704 version
= MPB_VERSION_RAID0
;
4706 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
4710 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
4712 struct imsm_super
*mpb
= super
->anchor
;
4713 char *reason
= NULL
;
4716 if (strlen(name
) > MAX_RAID_SERIAL_LEN
)
4717 reason
= "must be 16 characters or less";
4719 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4720 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4722 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
4723 reason
= "already exists";
4728 if (reason
&& !quiet
)
4729 pr_err("imsm volume name %s\n", reason
);
4734 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
4735 unsigned long long size
, char *name
,
4736 char *homehost
, int *uuid
,
4737 long long data_offset
)
4739 /* We are creating a volume inside a pre-existing container.
4740 * so st->sb is already set.
4742 struct intel_super
*super
= st
->sb
;
4743 struct imsm_super
*mpb
= super
->anchor
;
4744 struct intel_dev
*dv
;
4745 struct imsm_dev
*dev
;
4746 struct imsm_vol
*vol
;
4747 struct imsm_map
*map
;
4748 int idx
= mpb
->num_raid_devs
;
4750 unsigned long long array_blocks
;
4751 size_t size_old
, size_new
;
4752 unsigned long long num_data_stripes
;
4754 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
4755 pr_err("This imsm-container already has the maximum of %d volumes\n", super
->orom
->vpa
);
4759 /* ensure the mpb is large enough for the new data */
4760 size_old
= __le32_to_cpu(mpb
->mpb_size
);
4761 size_new
= disks_to_mpb_size(info
->nr_disks
);
4762 if (size_new
> size_old
) {
4764 size_t size_round
= ROUND_UP(size_new
, 512);
4766 if (posix_memalign(&mpb_new
, 512, size_round
) != 0) {
4767 pr_err("could not allocate new mpb\n");
4770 if (posix_memalign(&super
->migr_rec_buf
, 512,
4771 MIGR_REC_BUF_SIZE
) != 0) {
4772 pr_err("could not allocate migr_rec buffer\n");
4778 memcpy(mpb_new
, mpb
, size_old
);
4781 super
->anchor
= mpb_new
;
4782 mpb
->mpb_size
= __cpu_to_le32(size_new
);
4783 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
4785 super
->current_vol
= idx
;
4787 /* handle 'failed_disks' by either:
4788 * a) create dummy disk entries in the table if this the first
4789 * volume in the array. We add them here as this is the only
4790 * opportunity to add them. add_to_super_imsm_volume()
4791 * handles the non-failed disks and continues incrementing
4793 * b) validate that 'failed_disks' matches the current number
4794 * of missing disks if the container is populated
4796 if (super
->current_vol
== 0) {
4798 for (i
= 0; i
< info
->failed_disks
; i
++) {
4799 struct imsm_disk
*disk
;
4802 disk
= __get_imsm_disk(mpb
, i
);
4803 disk
->status
= CONFIGURED_DISK
| FAILED_DISK
;
4804 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
4805 snprintf((char *) disk
->serial
, MAX_RAID_SERIAL_LEN
,
4808 find_missing(super
);
4813 for (d
= super
->missing
; d
; d
= d
->next
)
4815 if (info
->failed_disks
> missing
) {
4816 pr_err("unable to add 'missing' disk to container\n");
4821 if (!check_name(super
, name
, 0))
4823 dv
= xmalloc(sizeof(*dv
));
4824 dev
= xcalloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
4825 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
4826 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
4827 info
->layout
, info
->chunk_size
,
4829 /* round array size down to closest MB */
4830 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
4832 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
4833 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
4834 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
4836 vol
->migr_state
= 0;
4837 set_migr_type(dev
, MIGR_INIT
);
4838 vol
->dirty
= !info
->state
;
4839 vol
->curr_migr_unit
= 0;
4840 map
= get_imsm_map(dev
, MAP_0
);
4841 set_pba_of_lba0(map
, super
->create_offset
);
4842 set_blocks_per_member(map
, info_to_blocks_per_member(info
, size
));
4843 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
4844 map
->failed_disk_num
= ~0;
4845 if (info
->level
> 0)
4846 map
->map_state
= (info
->state
? IMSM_T_STATE_NORMAL
4847 : IMSM_T_STATE_UNINITIALIZED
);
4849 map
->map_state
= info
->failed_disks
? IMSM_T_STATE_FAILED
:
4850 IMSM_T_STATE_NORMAL
;
4853 if (info
->level
== 1 && info
->raid_disks
> 2) {
4856 pr_err("imsm does not support more than 2 disksin a raid1 volume\n");
4860 map
->raid_level
= info
->level
;
4861 if (info
->level
== 10) {
4862 map
->raid_level
= 1;
4863 map
->num_domains
= info
->raid_disks
/ 2;
4864 } else if (info
->level
== 1)
4865 map
->num_domains
= info
->raid_disks
;
4867 map
->num_domains
= 1;
4869 /* info->size is only int so use the 'size' parameter instead */
4870 num_data_stripes
= (size
* 2) / info_to_blocks_per_strip(info
);
4871 num_data_stripes
/= map
->num_domains
;
4872 set_num_data_stripes(map
, num_data_stripes
);
4874 map
->num_members
= info
->raid_disks
;
4875 for (i
= 0; i
< map
->num_members
; i
++) {
4876 /* initialized in add_to_super */
4877 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
4879 mpb
->num_raid_devs
++;
4882 dv
->index
= super
->current_vol
;
4883 dv
->next
= super
->devlist
;
4884 super
->devlist
= dv
;
4886 imsm_update_version_info(super
);
4891 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
4892 unsigned long long size
, char *name
,
4893 char *homehost
, int *uuid
,
4894 unsigned long long data_offset
)
4896 /* This is primarily called by Create when creating a new array.
4897 * We will then get add_to_super called for each component, and then
4898 * write_init_super called to write it out to each device.
4899 * For IMSM, Create can create on fresh devices or on a pre-existing
4901 * To create on a pre-existing array a different method will be called.
4902 * This one is just for fresh drives.
4904 struct intel_super
*super
;
4905 struct imsm_super
*mpb
;
4909 if (data_offset
!= INVALID_SECTORS
) {
4910 pr_err("data-offset not supported by imsm\n");
4915 return init_super_imsm_volume(st
, info
, size
, name
, homehost
, uuid
,
4919 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
4923 super
= alloc_super();
4924 if (super
&& posix_memalign(&super
->buf
, 512, mpb_size
) != 0) {
4929 pr_err("could not allocate superblock\n");
4932 if (posix_memalign(&super
->migr_rec_buf
, 512, MIGR_REC_BUF_SIZE
) != 0) {
4933 pr_err("could not allocate migr_rec buffer\n");
4938 memset(super
->buf
, 0, mpb_size
);
4940 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
4944 /* zeroing superblock */
4948 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
4950 version
= (char *) mpb
->sig
;
4951 strcpy(version
, MPB_SIGNATURE
);
4952 version
+= strlen(MPB_SIGNATURE
);
4953 strcpy(version
, MPB_VERSION_RAID0
);
4959 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
4960 int fd
, char *devname
)
4962 struct intel_super
*super
= st
->sb
;
4963 struct imsm_super
*mpb
= super
->anchor
;
4964 struct imsm_disk
*_disk
;
4965 struct imsm_dev
*dev
;
4966 struct imsm_map
*map
;
4970 dev
= get_imsm_dev(super
, super
->current_vol
);
4971 map
= get_imsm_map(dev
, MAP_0
);
4973 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
4974 pr_err("%s: Cannot add spare devices to IMSM volume\n",
4980 /* we're doing autolayout so grab the pre-marked (in
4981 * validate_geometry) raid_disk
4983 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4984 if (dl
->raiddisk
== dk
->raid_disk
)
4987 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4988 if (dl
->major
== dk
->major
&&
4989 dl
->minor
== dk
->minor
)
4994 pr_err("%s is not a member of the same container\n", devname
);
4998 /* add a pristine spare to the metadata */
4999 if (dl
->index
< 0) {
5000 dl
->index
= super
->anchor
->num_disks
;
5001 super
->anchor
->num_disks
++;
5003 /* Check the device has not already been added */
5004 slot
= get_imsm_disk_slot(map
, dl
->index
);
5006 (get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
) & IMSM_ORD_REBUILD
) == 0) {
5007 pr_err("%s has been included in this array twice\n",
5011 set_imsm_ord_tbl_ent(map
, dk
->raid_disk
, dl
->index
);
5012 dl
->disk
.status
= CONFIGURED_DISK
;
5014 /* update size of 'missing' disks to be at least as large as the
5015 * largest acitve member (we only have dummy missing disks when
5016 * creating the first volume)
5018 if (super
->current_vol
== 0) {
5019 for (df
= super
->missing
; df
; df
= df
->next
) {
5020 if (total_blocks(&dl
->disk
) > total_blocks(&df
->disk
))
5021 set_total_blocks(&df
->disk
, total_blocks(&dl
->disk
));
5022 _disk
= __get_imsm_disk(mpb
, df
->index
);
5027 /* refresh unset/failed slots to point to valid 'missing' entries */
5028 for (df
= super
->missing
; df
; df
= df
->next
)
5029 for (slot
= 0; slot
< mpb
->num_disks
; slot
++) {
5030 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
5032 if ((ord
& IMSM_ORD_REBUILD
) == 0)
5034 set_imsm_ord_tbl_ent(map
, slot
, df
->index
| IMSM_ORD_REBUILD
);
5035 if (is_gen_migration(dev
)) {
5036 struct imsm_map
*map2
= get_imsm_map(dev
,
5038 int slot2
= get_imsm_disk_slot(map2
, df
->index
);
5039 if (slot2
< map2
->num_members
&& slot2
>= 0) {
5040 __u32 ord2
= get_imsm_ord_tbl_ent(dev
,
5043 if ((unsigned)df
->index
==
5045 set_imsm_ord_tbl_ent(map2
,
5051 dprintf("set slot:%d to missing disk:%d\n", slot
, df
->index
);
5055 /* if we are creating the first raid device update the family number */
5056 if (super
->current_vol
== 0) {
5058 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
5060 _disk
= __get_imsm_disk(mpb
, dl
->index
);
5061 if (!_dev
|| !_disk
) {
5062 pr_err("BUG mpb setup error\n");
5068 sum
+= __gen_imsm_checksum(mpb
);
5069 mpb
->family_num
= __cpu_to_le32(sum
);
5070 mpb
->orig_family_num
= mpb
->family_num
;
5072 super
->current_disk
= dl
;
5077 * Function marks disk as spare and restores disk serial
5078 * in case it was previously marked as failed by takeover operation
5080 * -1 : critical error
5081 * 0 : disk is marked as spare but serial is not set
5084 int mark_spare(struct dl
*disk
)
5086 __u8 serial
[MAX_RAID_SERIAL_LEN
];
5093 if (!imsm_read_serial(disk
->fd
, NULL
, serial
)) {
5094 /* Restore disk serial number, because takeover marks disk
5095 * as failed and adds to serial ':0' before it becomes
5098 serialcpy(disk
->serial
, serial
);
5099 serialcpy(disk
->disk
.serial
, serial
);
5102 disk
->disk
.status
= SPARE_DISK
;
5108 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
5109 int fd
, char *devname
,
5110 unsigned long long data_offset
)
5112 struct intel_super
*super
= st
->sb
;
5114 unsigned long long size
;
5119 /* If we are on an RAID enabled platform check that the disk is
5120 * attached to the raid controller.
5121 * We do not need to test disks attachment for container based additions,
5122 * they shall be already tested when container was created/assembled.
5124 rv
= find_intel_hba_capability(fd
, super
, devname
);
5125 /* no orom/efi or non-intel hba of the disk */
5127 dprintf("capability: %p fd: %d ret: %d\n",
5128 super
->orom
, fd
, rv
);
5132 if (super
->current_vol
>= 0)
5133 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
5136 dd
= xcalloc(sizeof(*dd
), 1);
5137 dd
->major
= major(stb
.st_rdev
);
5138 dd
->minor
= minor(stb
.st_rdev
);
5139 dd
->devname
= devname
? xstrdup(devname
) : NULL
;
5142 dd
->action
= DISK_ADD
;
5143 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
5145 pr_err("failed to retrieve scsi serial, aborting\n");
5150 get_dev_size(fd
, NULL
, &size
);
5151 /* clear migr_rec when adding disk to container */
5152 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SIZE
);
5153 if (lseek64(fd
, size
- MIGR_REC_POSITION
, SEEK_SET
) >= 0) {
5154 if (write(fd
, super
->migr_rec_buf
,
5155 MIGR_REC_BUF_SIZE
) != MIGR_REC_BUF_SIZE
)
5156 perror("Write migr_rec failed");
5160 serialcpy(dd
->disk
.serial
, dd
->serial
);
5161 set_total_blocks(&dd
->disk
, size
);
5162 if (__le32_to_cpu(dd
->disk
.total_blocks_hi
) > 0) {
5163 struct imsm_super
*mpb
= super
->anchor
;
5164 mpb
->attributes
|= MPB_ATTRIB_2TB_DISK
;
5167 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
5168 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
5170 dd
->disk
.scsi_id
= __cpu_to_le32(0);
5172 if (st
->update_tail
) {
5173 dd
->next
= super
->disk_mgmt_list
;
5174 super
->disk_mgmt_list
= dd
;
5176 dd
->next
= super
->disks
;
5178 super
->updates_pending
++;
5184 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
5186 struct intel_super
*super
= st
->sb
;
5189 /* remove from super works only in mdmon - for communication
5190 * manager - monitor. Check if communication memory buffer
5193 if (!st
->update_tail
) {
5194 pr_err("shall be used in mdmon context only\n");
5197 dd
= xcalloc(1, sizeof(*dd
));
5198 dd
->major
= dk
->major
;
5199 dd
->minor
= dk
->minor
;
5202 dd
->action
= DISK_REMOVE
;
5204 dd
->next
= super
->disk_mgmt_list
;
5205 super
->disk_mgmt_list
= dd
;
5210 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
5214 struct imsm_super anchor
;
5215 } spare_record
__attribute__ ((aligned(512)));
5217 /* spare records have their own family number and do not have any defined raid
5220 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
5222 struct imsm_super
*mpb
= super
->anchor
;
5223 struct imsm_super
*spare
= &spare_record
.anchor
;
5227 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
));
5228 spare
->generation_num
= __cpu_to_le32(1UL);
5229 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5230 spare
->num_disks
= 1;
5231 spare
->num_raid_devs
= 0;
5232 spare
->cache_size
= mpb
->cache_size
;
5233 spare
->pwr_cycle_count
= __cpu_to_le32(1);
5235 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
5236 MPB_SIGNATURE MPB_VERSION_RAID0
);
5238 for (d
= super
->disks
; d
; d
= d
->next
) {
5242 spare
->disk
[0] = d
->disk
;
5243 if (__le32_to_cpu(d
->disk
.total_blocks_hi
) > 0)
5244 spare
->attributes
|= MPB_ATTRIB_2TB_DISK
;
5246 sum
= __gen_imsm_checksum(spare
);
5247 spare
->family_num
= __cpu_to_le32(sum
);
5248 spare
->orig_family_num
= 0;
5249 sum
= __gen_imsm_checksum(spare
);
5250 spare
->check_sum
= __cpu_to_le32(sum
);
5252 if (store_imsm_mpb(d
->fd
, spare
)) {
5253 pr_err("failed for device %d:%d %s\n",
5254 d
->major
, d
->minor
, strerror(errno
));
5266 static int write_super_imsm(struct supertype
*st
, int doclose
)
5268 struct intel_super
*super
= st
->sb
;
5269 struct imsm_super
*mpb
= super
->anchor
;
5275 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
5277 int clear_migration_record
= 1;
5279 /* 'generation' is incremented everytime the metadata is written */
5280 generation
= __le32_to_cpu(mpb
->generation_num
);
5282 mpb
->generation_num
= __cpu_to_le32(generation
);
5284 /* fix up cases where previous mdadm releases failed to set
5287 if (mpb
->orig_family_num
== 0)
5288 mpb
->orig_family_num
= mpb
->family_num
;
5290 for (d
= super
->disks
; d
; d
= d
->next
) {
5294 mpb
->disk
[d
->index
] = d
->disk
;
5298 for (d
= super
->missing
; d
; d
= d
->next
) {
5299 mpb
->disk
[d
->index
] = d
->disk
;
5302 mpb
->num_disks
= num_disks
;
5303 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
5305 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5306 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
5307 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
5309 imsm_copy_dev(dev
, dev2
);
5310 mpb_size
+= sizeof_imsm_dev(dev
, 0);
5312 if (is_gen_migration(dev2
))
5313 clear_migration_record
= 0;
5315 mpb_size
+= __le32_to_cpu(mpb
->bbm_log_size
);
5316 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5318 /* recalculate checksum */
5319 sum
= __gen_imsm_checksum(mpb
);
5320 mpb
->check_sum
= __cpu_to_le32(sum
);
5322 if (super
->clean_migration_record_by_mdmon
) {
5323 clear_migration_record
= 1;
5324 super
->clean_migration_record_by_mdmon
= 0;
5326 if (clear_migration_record
)
5327 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SIZE
);
5329 /* write the mpb for disks that compose raid devices */
5330 for (d
= super
->disks
; d
; d
= d
->next
) {
5331 if (d
->index
< 0 || is_failed(&d
->disk
))
5334 if (clear_migration_record
) {
5335 unsigned long long dsize
;
5337 get_dev_size(d
->fd
, NULL
, &dsize
);
5338 if (lseek64(d
->fd
, dsize
- 512, SEEK_SET
) >= 0) {
5339 if (write(d
->fd
, super
->migr_rec_buf
,
5340 MIGR_REC_BUF_SIZE
) != MIGR_REC_BUF_SIZE
)
5341 perror("Write migr_rec failed");
5345 if (store_imsm_mpb(d
->fd
, mpb
))
5347 "failed for device %d:%d (fd: %d)%s\n",
5349 d
->fd
, strerror(errno
));
5358 return write_super_imsm_spares(super
, doclose
);
5363 static int create_array(struct supertype
*st
, int dev_idx
)
5366 struct imsm_update_create_array
*u
;
5367 struct intel_super
*super
= st
->sb
;
5368 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
5369 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
5370 struct disk_info
*inf
;
5371 struct imsm_disk
*disk
;
5374 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
5375 sizeof(*inf
) * map
->num_members
;
5377 u
->type
= update_create_array
;
5378 u
->dev_idx
= dev_idx
;
5379 imsm_copy_dev(&u
->dev
, dev
);
5380 inf
= get_disk_info(u
);
5381 for (i
= 0; i
< map
->num_members
; i
++) {
5382 int idx
= get_imsm_disk_idx(dev
, i
, MAP_X
);
5384 disk
= get_imsm_disk(super
, idx
);
5386 disk
= get_imsm_missing(super
, idx
);
5387 serialcpy(inf
[i
].serial
, disk
->serial
);
5389 append_metadata_update(st
, u
, len
);
5394 static int mgmt_disk(struct supertype
*st
)
5396 struct intel_super
*super
= st
->sb
;
5398 struct imsm_update_add_remove_disk
*u
;
5400 if (!super
->disk_mgmt_list
)
5405 u
->type
= update_add_remove_disk
;
5406 append_metadata_update(st
, u
, len
);
5411 static int write_init_super_imsm(struct supertype
*st
)
5413 struct intel_super
*super
= st
->sb
;
5414 int current_vol
= super
->current_vol
;
5416 /* we are done with current_vol reset it to point st at the container */
5417 super
->current_vol
= -1;
5419 if (st
->update_tail
) {
5420 /* queue the recently created array / added disk
5421 * as a metadata update */
5424 /* determine if we are creating a volume or adding a disk */
5425 if (current_vol
< 0) {
5426 /* in the mgmt (add/remove) disk case we are running
5427 * in mdmon context, so don't close fd's
5429 return mgmt_disk(st
);
5431 rv
= create_array(st
, current_vol
);
5436 for (d
= super
->disks
; d
; d
= d
->next
)
5437 Kill(d
->devname
, NULL
, 0, -1, 1);
5438 return write_super_imsm(st
, 1);
5443 static int store_super_imsm(struct supertype
*st
, int fd
)
5445 struct intel_super
*super
= st
->sb
;
5446 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
5452 return store_imsm_mpb(fd
, mpb
);
5458 static int imsm_bbm_log_size(struct imsm_super
*mpb
)
5460 return __le32_to_cpu(mpb
->bbm_log_size
);
5464 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
5465 int layout
, int raiddisks
, int chunk
,
5466 unsigned long long size
,
5467 unsigned long long data_offset
,
5469 unsigned long long *freesize
,
5473 unsigned long long ldsize
;
5474 struct intel_super
*super
;
5477 if (level
!= LEVEL_CONTAINER
)
5482 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
5485 pr_err("imsm: Cannot open %s: %s\n",
5486 dev
, strerror(errno
));
5489 if (!get_dev_size(fd
, dev
, &ldsize
)) {
5494 /* capabilities retrieve could be possible
5495 * note that there is no fd for the disks in array.
5497 super
= alloc_super();
5498 rv
= find_intel_hba_capability(fd
, super
, verbose
> 0 ? dev
: NULL
);
5502 fd2devname(fd
, str
);
5503 dprintf("fd: %d %s orom: %p rv: %d raiddisk: %d\n",
5504 fd
, str
, super
->orom
, rv
, raiddisks
);
5506 /* no orom/efi or non-intel hba of the disk */
5513 if (raiddisks
> super
->orom
->tds
) {
5515 pr_err("%d exceeds maximum number of platform supported disks: %d\n",
5516 raiddisks
, super
->orom
->tds
);
5520 if ((super
->orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) == 0 &&
5521 (ldsize
>> 9) >> 32 > 0) {
5523 pr_err("%s exceeds maximum platform supported size\n", dev
);
5529 *freesize
= avail_size_imsm(st
, ldsize
>> 9, data_offset
);
5535 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
5537 const unsigned long long base_start
= e
[*idx
].start
;
5538 unsigned long long end
= base_start
+ e
[*idx
].size
;
5541 if (base_start
== end
)
5545 for (i
= *idx
; i
< num_extents
; i
++) {
5546 /* extend overlapping extents */
5547 if (e
[i
].start
>= base_start
&&
5548 e
[i
].start
<= end
) {
5551 if (e
[i
].start
+ e
[i
].size
> end
)
5552 end
= e
[i
].start
+ e
[i
].size
;
5553 } else if (e
[i
].start
> end
) {
5559 return end
- base_start
;
5562 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
5564 /* build a composite disk with all known extents and generate a new
5565 * 'maxsize' given the "all disks in an array must share a common start
5566 * offset" constraint
5568 struct extent
*e
= xcalloc(sum_extents
, sizeof(*e
));
5572 unsigned long long pos
;
5573 unsigned long long start
= 0;
5574 unsigned long long maxsize
;
5575 unsigned long reserve
;
5577 /* coalesce and sort all extents. also, check to see if we need to
5578 * reserve space between member arrays
5581 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5584 for (i
= 0; i
< dl
->extent_cnt
; i
++)
5587 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
5592 while (i
< sum_extents
) {
5593 e
[j
].start
= e
[i
].start
;
5594 e
[j
].size
= find_size(e
, &i
, sum_extents
);
5596 if (e
[j
-1].size
== 0)
5605 unsigned long long esize
;
5607 esize
= e
[i
].start
- pos
;
5608 if (esize
>= maxsize
) {
5613 pos
= e
[i
].start
+ e
[i
].size
;
5615 } while (e
[i
-1].size
);
5621 /* FIXME assumes volume at offset 0 is the first volume in a
5624 if (start_extent
> 0)
5625 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
5629 if (maxsize
< reserve
)
5632 super
->create_offset
= ~((unsigned long long) 0);
5633 if (start
+ reserve
> super
->create_offset
)
5634 return 0; /* start overflows create_offset */
5635 super
->create_offset
= start
+ reserve
;
5637 return maxsize
- reserve
;
5640 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
5642 if (level
< 0 || level
== 6 || level
== 4)
5645 /* if we have an orom prevent invalid raid levels */
5648 case 0: return imsm_orom_has_raid0(orom
);
5651 return imsm_orom_has_raid1e(orom
);
5652 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
5653 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
5654 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
5657 return 1; /* not on an Intel RAID platform so anything goes */
5663 active_arrays_by_format(char *name
, char* hba
, struct md_list
**devlist
,
5664 int dpa
, int verbose
)
5666 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
5667 struct mdstat_ent
*memb
;
5673 for (memb
= mdstat
; memb
; memb
= memb
->next
) {
5674 if (memb
->metadata_version
&&
5675 (strncmp(memb
->metadata_version
, "external:", 9) == 0) &&
5676 (strcmp(&memb
->metadata_version
[9], name
) == 0) &&
5677 !is_subarray(memb
->metadata_version
+9) &&
5679 struct dev_member
*dev
= memb
->members
;
5681 while(dev
&& (fd
< 0)) {
5682 char *path
= xmalloc(strlen(dev
->name
) + strlen("/dev/") + 1);
5683 num
= sprintf(path
, "%s%s", "/dev/", dev
->name
);
5685 fd
= open(path
, O_RDONLY
, 0);
5686 if (num
<= 0 || fd
< 0) {
5687 pr_vrb("Cannot open %s: %s\n",
5688 dev
->name
, strerror(errno
));
5694 if (fd
>= 0 && disk_attached_to_hba(fd
, hba
)) {
5695 struct mdstat_ent
*vol
;
5696 for (vol
= mdstat
; vol
; vol
= vol
->next
) {
5697 if (vol
->active
> 0 &&
5698 vol
->metadata_version
&&
5699 is_container_member(vol
, memb
->devnm
)) {
5704 if (*devlist
&& (found
< dpa
)) {
5705 dv
= xcalloc(1, sizeof(*dv
));
5706 dv
->devname
= xmalloc(strlen(memb
->devnm
) + strlen("/dev/") + 1);
5707 sprintf(dv
->devname
, "%s%s", "/dev/", memb
->devnm
);
5710 dv
->next
= *devlist
;
5718 free_mdstat(mdstat
);
5723 static struct md_list
*
5724 get_loop_devices(void)
5727 struct md_list
*devlist
= NULL
;
5730 for(i
= 0; i
< 12; i
++) {
5731 dv
= xcalloc(1, sizeof(*dv
));
5732 dv
->devname
= xmalloc(40);
5733 sprintf(dv
->devname
, "/dev/loop%d", i
);
5741 static struct md_list
*
5742 get_devices(const char *hba_path
)
5744 struct md_list
*devlist
= NULL
;
5751 devlist
= get_loop_devices();
5754 /* scroll through /sys/dev/block looking for devices attached to
5757 dir
= opendir("/sys/dev/block");
5758 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
5763 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
5765 path
= devt_to_devpath(makedev(major
, minor
));
5768 if (!path_attached_to_hba(path
, hba_path
)) {
5775 fd
= dev_open(ent
->d_name
, O_RDONLY
);
5777 fd2devname(fd
, buf
);
5780 pr_err("cannot open device: %s\n",
5785 dv
= xcalloc(1, sizeof(*dv
));
5786 dv
->devname
= xstrdup(buf
);
5793 devlist
= devlist
->next
;
5803 count_volumes_list(struct md_list
*devlist
, char *homehost
,
5804 int verbose
, int *found
)
5806 struct md_list
*tmpdev
;
5808 struct supertype
*st
;
5810 /* first walk the list of devices to find a consistent set
5811 * that match the criterea, if that is possible.
5812 * We flag the ones we like with 'used'.
5815 st
= match_metadata_desc_imsm("imsm");
5817 pr_vrb("cannot allocate memory for imsm supertype\n");
5821 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5822 char *devname
= tmpdev
->devname
;
5824 struct supertype
*tst
;
5826 if (tmpdev
->used
> 1)
5828 tst
= dup_super(st
);
5830 pr_vrb("cannot allocate memory for imsm supertype\n");
5833 tmpdev
->container
= 0;
5834 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
5836 dprintf("cannot open device %s: %s\n",
5837 devname
, strerror(errno
));
5839 } else if (fstat(dfd
, &stb
)< 0) {
5841 dprintf("fstat failed for %s: %s\n",
5842 devname
, strerror(errno
));
5844 } else if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
5845 dprintf("%s is not a block device.\n",
5848 } else if (must_be_container(dfd
)) {
5849 struct supertype
*cst
;
5850 cst
= super_by_fd(dfd
, NULL
);
5852 dprintf("cannot recognize container type %s\n",
5855 } else if (tst
->ss
!= st
->ss
) {
5856 dprintf("non-imsm container - ignore it: %s\n",
5859 } else if (!tst
->ss
->load_container
||
5860 tst
->ss
->load_container(tst
, dfd
, NULL
))
5863 tmpdev
->container
= 1;
5866 cst
->ss
->free_super(cst
);
5868 tmpdev
->st_rdev
= stb
.st_rdev
;
5869 if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
5870 dprintf("no RAID superblock on %s\n",
5873 } else if (tst
->ss
->compare_super
== NULL
) {
5874 dprintf("Cannot assemble %s metadata on %s\n",
5875 tst
->ss
->name
, devname
);
5881 if (tmpdev
->used
== 2 || tmpdev
->used
== 4) {
5882 /* Ignore unrecognised devices during auto-assembly */
5887 tst
->ss
->getinfo_super(tst
, &info
, NULL
);
5889 if (st
->minor_version
== -1)
5890 st
->minor_version
= tst
->minor_version
;
5892 if (memcmp(info
.uuid
, uuid_zero
,
5893 sizeof(int[4])) == 0) {
5894 /* this is a floating spare. It cannot define
5895 * an array unless there are no more arrays of
5896 * this type to be found. It can be included
5897 * in an array of this type though.
5903 if (st
->ss
!= tst
->ss
||
5904 st
->minor_version
!= tst
->minor_version
||
5905 st
->ss
->compare_super(st
, tst
) != 0) {
5906 /* Some mismatch. If exactly one array matches this host,
5907 * we can resolve on that one.
5908 * Or, if we are auto assembling, we just ignore the second
5911 dprintf("superblock on %s doesn't match others - assembly aborted\n",
5917 dprintf("found: devname: %s\n", devname
);
5921 tst
->ss
->free_super(tst
);
5925 if ((err
= load_super_imsm_all(st
, -1, &st
->sb
, NULL
, devlist
, 0)) == 0) {
5926 struct mdinfo
*iter
, *head
= st
->ss
->container_content(st
, NULL
);
5927 for (iter
= head
; iter
; iter
= iter
->next
) {
5928 dprintf("content->text_version: %s vol\n",
5929 iter
->text_version
);
5930 if (iter
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
5931 /* do not assemble arrays with unsupported
5933 dprintf("Cannot activate member %s.\n",
5934 iter
->text_version
);
5941 dprintf("No valid super block on device list: err: %d %p\n",
5945 dprintf("no more devices to examine\n");
5948 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5949 if (tmpdev
->used
== 1 && tmpdev
->found
) {
5951 if (count
< tmpdev
->found
)
5954 count
-= tmpdev
->found
;
5957 if (tmpdev
->used
== 1)
5962 st
->ss
->free_super(st
);
5967 count_volumes(struct intel_hba
*hba
, int dpa
, int verbose
)
5969 struct sys_dev
*idev
, *intel_devices
= find_intel_devices();
5971 const struct orom_entry
*entry
;
5972 struct devid_list
*dv
, *devid_list
;
5974 if (!hba
|| !hba
->path
)
5977 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
5978 if (strstr(idev
->path
, hba
->path
))
5982 if (!idev
|| !idev
->dev_id
)
5985 entry
= get_orom_entry_by_device_id(idev
->dev_id
);
5987 if (!entry
|| !entry
->devid_list
)
5990 devid_list
= entry
->devid_list
;
5991 for (dv
= devid_list
; dv
; dv
= dv
->next
) {
5992 struct md_list
*devlist
;
5993 struct sys_dev
*device
= device_by_id(dv
->devid
);
5998 hba_path
= device
->path
;
6002 /* VMD has one orom entry for all domain, but spanning is not allowed.
6003 * VMD arrays should be counted per domain (controller), so skip
6004 * domains that are not the given one.
6006 if (hba
->type
== SYS_DEV_VMD
&&
6007 (strncmp(device
->path
, hba
->path
, strlen(device
->path
)) != 0))
6010 devlist
= get_devices(hba_path
);
6011 /* if no intel devices return zero volumes */
6012 if (devlist
== NULL
)
6015 count
+= active_arrays_by_format("imsm", hba_path
, &devlist
, dpa
, verbose
);
6016 dprintf("path: %s active arrays: %d\n", hba_path
, count
);
6017 if (devlist
== NULL
)
6021 count
+= count_volumes_list(devlist
,
6025 dprintf("found %d count: %d\n", found
, count
);
6028 dprintf("path: %s total number of volumes: %d\n", hba_path
, count
);
6031 struct md_list
*dv
= devlist
;
6032 devlist
= devlist
->next
;
6040 static int imsm_default_chunk(const struct imsm_orom
*orom
)
6042 /* up to 512 if the plaform supports it, otherwise the platform max.
6043 * 128 if no platform detected
6045 int fs
= max(7, orom
? fls(orom
->sss
) : 0);
6047 return min(512, (1 << fs
));
6051 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
6052 int raiddisks
, int *chunk
, unsigned long long size
, int verbose
)
6054 /* check/set platform and metadata limits/defaults */
6055 if (super
->orom
&& raiddisks
> super
->orom
->dpa
) {
6056 pr_vrb("platform supports a maximum of %d disks per array\n",
6061 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
6062 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
6063 pr_vrb("platform does not support raid%d with %d disk%s\n",
6064 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
6068 if (*chunk
== 0 || *chunk
== UnSet
)
6069 *chunk
= imsm_default_chunk(super
->orom
);
6071 if (super
->orom
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
6072 pr_vrb("platform does not support a chunk size of: %d\n", *chunk
);
6076 if (layout
!= imsm_level_to_layout(level
)) {
6078 pr_vrb("imsm raid 5 only supports the left-asymmetric layout\n");
6079 else if (level
== 10)
6080 pr_vrb("imsm raid 10 only supports the n2 layout\n");
6082 pr_vrb("imsm unknown layout %#x for this raid level %d\n",
6087 if (super
->orom
&& (super
->orom
->attr
& IMSM_OROM_ATTR_2TB
) == 0 &&
6088 (calc_array_size(level
, raiddisks
, layout
, *chunk
, size
) >> 32) > 0) {
6089 pr_vrb("platform does not support a volume size over 2TB\n");
6096 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
6097 * FIX ME add ahci details
6099 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
6100 int layout
, int raiddisks
, int *chunk
,
6101 unsigned long long size
,
6102 unsigned long long data_offset
,
6104 unsigned long long *freesize
,
6108 struct intel_super
*super
= st
->sb
;
6109 struct imsm_super
*mpb
;
6111 unsigned long long pos
= 0;
6112 unsigned long long maxsize
;
6116 /* We must have the container info already read in. */
6120 mpb
= super
->anchor
;
6122 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, size
, verbose
)) {
6123 pr_err("RAID gemetry validation failed. Cannot proceed with the action(s).\n");
6127 /* General test: make sure there is space for
6128 * 'raiddisks' device extents of size 'size' at a given
6131 unsigned long long minsize
= size
;
6132 unsigned long long start_offset
= MaxSector
;
6135 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
6136 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6141 e
= get_extents(super
, dl
);
6144 unsigned long long esize
;
6145 esize
= e
[i
].start
- pos
;
6146 if (esize
>= minsize
)
6148 if (found
&& start_offset
== MaxSector
) {
6151 } else if (found
&& pos
!= start_offset
) {
6155 pos
= e
[i
].start
+ e
[i
].size
;
6157 } while (e
[i
-1].size
);
6162 if (dcnt
< raiddisks
) {
6164 pr_err("imsm: Not enough devices with space for this array (%d < %d)\n",
6171 /* This device must be a member of the set */
6172 if (stat(dev
, &stb
) < 0)
6174 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
6176 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6177 if (dl
->major
== (int)major(stb
.st_rdev
) &&
6178 dl
->minor
== (int)minor(stb
.st_rdev
))
6183 pr_err("%s is not in the same imsm set\n", dev
);
6185 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
6186 /* If a volume is present then the current creation attempt
6187 * cannot incorporate new spares because the orom may not
6188 * understand this configuration (all member disks must be
6189 * members of each array in the container).
6191 pr_err("%s is a spare and a volume is already defined for this container\n", dev
);
6192 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
6194 } else if (super
->orom
&& mpb
->num_raid_devs
> 0 &&
6195 mpb
->num_disks
!= raiddisks
) {
6196 pr_err("The option-rom requires all member disks to be a member of all volumes\n");
6200 /* retrieve the largest free space block */
6201 e
= get_extents(super
, dl
);
6206 unsigned long long esize
;
6208 esize
= e
[i
].start
- pos
;
6209 if (esize
>= maxsize
)
6211 pos
= e
[i
].start
+ e
[i
].size
;
6213 } while (e
[i
-1].size
);
6218 pr_err("unable to determine free space for: %s\n",
6222 if (maxsize
< size
) {
6224 pr_err("%s not enough space (%llu < %llu)\n",
6225 dev
, maxsize
, size
);
6229 /* count total number of extents for merge */
6231 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6233 i
+= dl
->extent_cnt
;
6235 maxsize
= merge_extents(super
, i
);
6237 if (!check_env("IMSM_NO_PLATFORM") &&
6238 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6239 pr_err("attempting to create a second volume with size less then remaining space. Aborting...\n");
6243 if (maxsize
< size
|| maxsize
== 0) {
6246 pr_err("no free space left on device. Aborting...\n");
6248 pr_err("not enough space to create volume of given size (%llu < %llu). Aborting...\n",
6254 *freesize
= maxsize
;
6257 int count
= count_volumes(super
->hba
,
6258 super
->orom
->dpa
, verbose
);
6259 if (super
->orom
->vphba
<= count
) {
6260 pr_vrb("platform does not support more than %d raid volumes.\n",
6261 super
->orom
->vphba
);
6268 static int imsm_get_free_size(struct supertype
*st
, int raiddisks
,
6269 unsigned long long size
, int chunk
,
6270 unsigned long long *freesize
)
6272 struct intel_super
*super
= st
->sb
;
6273 struct imsm_super
*mpb
= super
->anchor
;
6278 unsigned long long maxsize
;
6279 unsigned long long minsize
;
6283 /* find the largest common start free region of the possible disks */
6287 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6293 /* don't activate new spares if we are orom constrained
6294 * and there is already a volume active in the container
6296 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
6299 e
= get_extents(super
, dl
);
6302 for (i
= 1; e
[i
-1].size
; i
++)
6310 maxsize
= merge_extents(super
, extent_cnt
);
6314 minsize
= chunk
* 2;
6316 if (cnt
< raiddisks
||
6317 (super
->orom
&& used
&& used
!= raiddisks
) ||
6318 maxsize
< minsize
||
6320 pr_err("not enough devices with space to create array.\n");
6321 return 0; /* No enough free spaces large enough */
6332 if (!check_env("IMSM_NO_PLATFORM") &&
6333 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6334 pr_err("attempting to create a second volume with size less then remaining space. Aborting...\n");
6338 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6340 dl
->raiddisk
= cnt
++;
6344 dprintf("imsm: imsm_get_free_size() returns : %llu\n", size
);
6349 static int reserve_space(struct supertype
*st
, int raiddisks
,
6350 unsigned long long size
, int chunk
,
6351 unsigned long long *freesize
)
6353 struct intel_super
*super
= st
->sb
;
6358 rv
= imsm_get_free_size(st
, raiddisks
, size
, chunk
, freesize
);
6361 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6363 dl
->raiddisk
= cnt
++;
6370 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
6371 int raiddisks
, int *chunk
, unsigned long long size
,
6372 unsigned long long data_offset
,
6373 char *dev
, unsigned long long *freesize
,
6381 * if given unused devices create a container
6382 * if given given devices in a container create a member volume
6384 if (level
== LEVEL_CONTAINER
) {
6385 /* Must be a fresh device to add to a container */
6386 return validate_geometry_imsm_container(st
, level
, layout
,
6396 struct intel_super
*super
= st
->sb
;
6397 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
6398 raiddisks
, chunk
, size
,
6401 /* we are being asked to automatically layout a
6402 * new volume based on the current contents of
6403 * the container. If the the parameters can be
6404 * satisfied reserve_space will record the disks,
6405 * start offset, and size of the volume to be
6406 * created. add_to_super and getinfo_super
6407 * detect when autolayout is in progress.
6409 /* assuming that freesize is always given when array is
6411 if (super
->orom
&& freesize
) {
6413 count
= count_volumes(super
->hba
,
6414 super
->orom
->dpa
, verbose
);
6415 if (super
->orom
->vphba
<= count
) {
6416 pr_vrb("platform does not support more than %d raid volumes.\n",
6417 super
->orom
->vphba
);
6422 return reserve_space(st
, raiddisks
, size
,
6428 /* creating in a given container */
6429 return validate_geometry_imsm_volume(st
, level
, layout
,
6430 raiddisks
, chunk
, size
,
6432 dev
, freesize
, verbose
);
6435 /* This device needs to be a device in an 'imsm' container */
6436 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
6439 pr_err("Cannot create this array on device %s\n",
6444 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
6446 pr_err("Cannot open %s: %s\n",
6447 dev
, strerror(errno
));
6450 /* Well, it is in use by someone, maybe an 'imsm' container. */
6451 cfd
= open_container(fd
);
6455 pr_err("Cannot use %s: It is busy\n",
6459 sra
= sysfs_read(cfd
, NULL
, GET_VERSION
);
6460 if (sra
&& sra
->array
.major_version
== -1 &&
6461 strcmp(sra
->text_version
, "imsm") == 0)
6465 /* This is a member of a imsm container. Load the container
6466 * and try to create a volume
6468 struct intel_super
*super
;
6470 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
, NULL
, 1) == 0) {
6472 strcpy(st
->container_devnm
, fd2devnm(cfd
));
6474 return validate_geometry_imsm_volume(st
, level
, layout
,
6476 size
, data_offset
, dev
,
6483 pr_err("failed container membership check\n");
6489 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
6491 struct intel_super
*super
= st
->sb
;
6493 if (level
&& *level
== UnSet
)
6494 *level
= LEVEL_CONTAINER
;
6496 if (level
&& layout
&& *layout
== UnSet
)
6497 *layout
= imsm_level_to_layout(*level
);
6499 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0))
6500 *chunk
= imsm_default_chunk(super
->orom
);
6503 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
6505 static int kill_subarray_imsm(struct supertype
*st
)
6507 /* remove the subarray currently referenced by ->current_vol */
6509 struct intel_dev
**dp
;
6510 struct intel_super
*super
= st
->sb
;
6511 __u8 current_vol
= super
->current_vol
;
6512 struct imsm_super
*mpb
= super
->anchor
;
6514 if (super
->current_vol
< 0)
6516 super
->current_vol
= -1; /* invalidate subarray cursor */
6518 /* block deletions that would change the uuid of active subarrays
6520 * FIXME when immutable ids are available, but note that we'll
6521 * also need to fixup the invalidated/active subarray indexes in
6524 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6527 if (i
< current_vol
)
6529 sprintf(subarray
, "%u", i
);
6530 if (is_subarray_active(subarray
, st
->devnm
)) {
6531 pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
6538 if (st
->update_tail
) {
6539 struct imsm_update_kill_array
*u
= xmalloc(sizeof(*u
));
6541 u
->type
= update_kill_array
;
6542 u
->dev_idx
= current_vol
;
6543 append_metadata_update(st
, u
, sizeof(*u
));
6548 for (dp
= &super
->devlist
; *dp
;)
6549 if ((*dp
)->index
== current_vol
) {
6552 handle_missing(super
, (*dp
)->dev
);
6553 if ((*dp
)->index
> current_vol
)
6558 /* no more raid devices, all active components are now spares,
6559 * but of course failed are still failed
6561 if (--mpb
->num_raid_devs
== 0) {
6564 for (d
= super
->disks
; d
; d
= d
->next
)
6569 super
->updates_pending
++;
6574 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
6575 char *update
, struct mddev_ident
*ident
)
6577 /* update the subarray currently referenced by ->current_vol */
6578 struct intel_super
*super
= st
->sb
;
6579 struct imsm_super
*mpb
= super
->anchor
;
6581 if (strcmp(update
, "name") == 0) {
6582 char *name
= ident
->name
;
6586 if (is_subarray_active(subarray
, st
->devnm
)) {
6587 pr_err("Unable to update name of active subarray\n");
6591 if (!check_name(super
, name
, 0))
6594 vol
= strtoul(subarray
, &ep
, 10);
6595 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
6598 if (st
->update_tail
) {
6599 struct imsm_update_rename_array
*u
= xmalloc(sizeof(*u
));
6601 u
->type
= update_rename_array
;
6603 snprintf((char *) u
->name
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6604 append_metadata_update(st
, u
, sizeof(*u
));
6606 struct imsm_dev
*dev
;
6609 dev
= get_imsm_dev(super
, vol
);
6610 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6611 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6612 dev
= get_imsm_dev(super
, i
);
6613 handle_missing(super
, dev
);
6615 super
->updates_pending
++;
6622 #endif /* MDASSEMBLE */
6624 static int is_gen_migration(struct imsm_dev
*dev
)
6629 if (!dev
->vol
.migr_state
)
6632 if (migr_type(dev
) == MIGR_GEN_MIGR
)
6638 static int is_rebuilding(struct imsm_dev
*dev
)
6640 struct imsm_map
*migr_map
;
6642 if (!dev
->vol
.migr_state
)
6645 if (migr_type(dev
) != MIGR_REBUILD
)
6648 migr_map
= get_imsm_map(dev
, MAP_1
);
6650 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
6657 static int is_initializing(struct imsm_dev
*dev
)
6659 struct imsm_map
*migr_map
;
6661 if (!dev
->vol
.migr_state
)
6664 if (migr_type(dev
) != MIGR_INIT
)
6667 migr_map
= get_imsm_map(dev
, MAP_1
);
6669 if (migr_map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
6676 static void update_recovery_start(struct intel_super
*super
,
6677 struct imsm_dev
*dev
,
6678 struct mdinfo
*array
)
6680 struct mdinfo
*rebuild
= NULL
;
6684 if (!is_rebuilding(dev
))
6687 /* Find the rebuild target, but punt on the dual rebuild case */
6688 for (d
= array
->devs
; d
; d
= d
->next
)
6689 if (d
->recovery_start
== 0) {
6696 /* (?) none of the disks are marked with
6697 * IMSM_ORD_REBUILD, so assume they are missing and the
6698 * disk_ord_tbl was not correctly updated
6700 dprintf("failed to locate out-of-sync disk\n");
6704 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
6705 rebuild
->recovery_start
= units
* blocks_per_migr_unit(super
, dev
);
6709 static int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
);
6712 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
6714 /* Given a container loaded by load_super_imsm_all,
6715 * extract information about all the arrays into
6717 * If 'subarray' is given, just extract info about that array.
6719 * For each imsm_dev create an mdinfo, fill it in,
6720 * then look for matching devices in super->disks
6721 * and create appropriate device mdinfo.
6723 struct intel_super
*super
= st
->sb
;
6724 struct imsm_super
*mpb
= super
->anchor
;
6725 struct mdinfo
*rest
= NULL
;
6729 int spare_disks
= 0;
6731 /* do not assemble arrays when not all attributes are supported */
6732 if (imsm_check_attributes(mpb
->attributes
) == 0) {
6734 pr_err("Unsupported attributes in IMSM metadata.Arrays activation is blocked.\n");
6737 /* check for bad blocks */
6738 if (imsm_bbm_log_size(super
->anchor
)) {
6739 pr_err("BBM log found in IMSM metadata.Arrays activation is blocked.\n");
6743 /* count spare devices, not used in maps
6745 for (d
= super
->disks
; d
; d
= d
->next
)
6749 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6750 struct imsm_dev
*dev
;
6751 struct imsm_map
*map
;
6752 struct imsm_map
*map2
;
6753 struct mdinfo
*this;
6761 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
6764 dev
= get_imsm_dev(super
, i
);
6765 map
= get_imsm_map(dev
, MAP_0
);
6766 map2
= get_imsm_map(dev
, MAP_1
);
6768 /* do not publish arrays that are in the middle of an
6769 * unsupported migration
6771 if (dev
->vol
.migr_state
&&
6772 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
6773 pr_err("cannot assemble volume '%.16s': unsupported migration in progress\n",
6777 /* do not publish arrays that are not support by controller's
6781 this = xmalloc(sizeof(*this));
6783 super
->current_vol
= i
;
6784 getinfo_super_imsm_volume(st
, this, NULL
);
6787 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
6788 /* mdadm does not support all metadata features- set the bit in all arrays state */
6789 if (!validate_geometry_imsm_orom(super
,
6790 get_imsm_raid_level(map
), /* RAID level */
6791 imsm_level_to_layout(get_imsm_raid_level(map
)),
6792 map
->num_members
, /* raid disks */
6793 &chunk
, join_u32(dev
->size_low
, dev
->size_high
),
6795 pr_err("IMSM RAID geometry validation failed. Array %s activation is blocked.\n",
6797 this->array
.state
|=
6798 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
6799 (1<<MD_SB_BLOCK_VOLUME
);
6803 /* if array has bad blocks, set suitable bit in all arrays state */
6805 this->array
.state
|=
6806 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
6807 (1<<MD_SB_BLOCK_VOLUME
);
6809 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
6810 unsigned long long recovery_start
;
6811 struct mdinfo
*info_d
;
6818 idx
= get_imsm_disk_idx(dev
, slot
, MAP_0
);
6819 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
6820 for (d
= super
->disks
; d
; d
= d
->next
)
6821 if (d
->index
== idx
)
6824 recovery_start
= MaxSector
;
6827 if (d
&& is_failed(&d
->disk
))
6829 if (ord
& IMSM_ORD_REBUILD
)
6833 * if we skip some disks the array will be assmebled degraded;
6834 * reset resync start to avoid a dirty-degraded
6835 * situation when performing the intial sync
6837 * FIXME handle dirty degraded
6839 if ((skip
|| recovery_start
== 0) && !dev
->vol
.dirty
)
6840 this->resync_start
= MaxSector
;
6844 info_d
= xcalloc(1, sizeof(*info_d
));
6845 info_d
->next
= this->devs
;
6846 this->devs
= info_d
;
6848 info_d
->disk
.number
= d
->index
;
6849 info_d
->disk
.major
= d
->major
;
6850 info_d
->disk
.minor
= d
->minor
;
6851 info_d
->disk
.raid_disk
= slot
;
6852 info_d
->recovery_start
= recovery_start
;
6854 if (slot
< map2
->num_members
)
6855 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
6857 this->array
.spare_disks
++;
6859 if (slot
< map
->num_members
)
6860 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
6862 this->array
.spare_disks
++;
6864 if (info_d
->recovery_start
== MaxSector
)
6865 this->array
.working_disks
++;
6867 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
6868 info_d
->data_offset
= pba_of_lba0(map
);
6869 info_d
->component_size
= blocks_per_member(map
);
6871 /* now that the disk list is up-to-date fixup recovery_start */
6872 update_recovery_start(super
, dev
, this);
6873 this->array
.spare_disks
+= spare_disks
;
6876 /* check for reshape */
6877 if (this->reshape_active
== 1)
6878 recover_backup_imsm(st
, this);
6886 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
6887 int failed
, int look_in_map
)
6889 struct imsm_map
*map
;
6891 map
= get_imsm_map(dev
, look_in_map
);
6894 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
6895 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
6897 switch (get_imsm_raid_level(map
)) {
6899 return IMSM_T_STATE_FAILED
;
6902 if (failed
< map
->num_members
)
6903 return IMSM_T_STATE_DEGRADED
;
6905 return IMSM_T_STATE_FAILED
;
6910 * check to see if any mirrors have failed, otherwise we
6911 * are degraded. Even numbered slots are mirrored on
6915 /* gcc -Os complains that this is unused */
6916 int insync
= insync
;
6918 for (i
= 0; i
< map
->num_members
; i
++) {
6919 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_X
);
6920 int idx
= ord_to_idx(ord
);
6921 struct imsm_disk
*disk
;
6923 /* reset the potential in-sync count on even-numbered
6924 * slots. num_copies is always 2 for imsm raid10
6929 disk
= get_imsm_disk(super
, idx
);
6930 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
6933 /* no in-sync disks left in this mirror the
6937 return IMSM_T_STATE_FAILED
;
6940 return IMSM_T_STATE_DEGRADED
;
6944 return IMSM_T_STATE_DEGRADED
;
6946 return IMSM_T_STATE_FAILED
;
6952 return map
->map_state
;
6955 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
6960 struct imsm_disk
*disk
;
6961 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6962 struct imsm_map
*prev
= get_imsm_map(dev
, MAP_1
);
6963 struct imsm_map
*map_for_loop
;
6968 /* at the beginning of migration we set IMSM_ORD_REBUILD on
6969 * disks that are being rebuilt. New failures are recorded to
6970 * map[0]. So we look through all the disks we started with and
6971 * see if any failures are still present, or if any new ones
6975 if (prev
&& (map
->num_members
< prev
->num_members
))
6976 map_for_loop
= prev
;
6978 for (i
= 0; i
< map_for_loop
->num_members
; i
++) {
6980 /* when MAP_X is passed both maps failures are counted
6983 (look_in_map
== MAP_1
|| look_in_map
== MAP_X
) &&
6984 i
< prev
->num_members
) {
6985 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
6986 idx_1
= ord_to_idx(ord
);
6988 disk
= get_imsm_disk(super
, idx_1
);
6989 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
6992 if ((look_in_map
== MAP_0
|| look_in_map
== MAP_X
) &&
6993 i
< map
->num_members
) {
6994 ord
= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
6995 idx
= ord_to_idx(ord
);
6998 disk
= get_imsm_disk(super
, idx
);
6999 if (!disk
|| is_failed(disk
) ||
7000 ord
& IMSM_ORD_REBUILD
)
7010 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
7013 struct intel_super
*super
= c
->sb
;
7014 struct imsm_super
*mpb
= super
->anchor
;
7016 if (atoi(inst
) >= mpb
->num_raid_devs
) {
7017 pr_err("subarry index %d, out of range\n", atoi(inst
));
7021 dprintf("imsm: open_new %s\n", inst
);
7022 a
->info
.container_member
= atoi(inst
);
7026 static int is_resyncing(struct imsm_dev
*dev
)
7028 struct imsm_map
*migr_map
;
7030 if (!dev
->vol
.migr_state
)
7033 if (migr_type(dev
) == MIGR_INIT
||
7034 migr_type(dev
) == MIGR_REPAIR
)
7037 if (migr_type(dev
) == MIGR_GEN_MIGR
)
7040 migr_map
= get_imsm_map(dev
, MAP_1
);
7042 if (migr_map
->map_state
== IMSM_T_STATE_NORMAL
&&
7043 dev
->vol
.migr_type
!= MIGR_GEN_MIGR
)
7049 /* return true if we recorded new information */
7050 static int mark_failure(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
7054 struct imsm_map
*map
;
7055 char buf
[MAX_RAID_SERIAL_LEN
+3];
7056 unsigned int len
, shift
= 0;
7058 /* new failures are always set in map[0] */
7059 map
= get_imsm_map(dev
, MAP_0
);
7061 slot
= get_imsm_disk_slot(map
, idx
);
7065 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
7066 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
7069 memcpy(buf
, disk
->serial
, MAX_RAID_SERIAL_LEN
);
7070 buf
[MAX_RAID_SERIAL_LEN
] = '\000';
7072 if ((len
= strlen(buf
)) >= MAX_RAID_SERIAL_LEN
)
7073 shift
= len
- MAX_RAID_SERIAL_LEN
+ 1;
7074 strncpy((char *)disk
->serial
, &buf
[shift
], MAX_RAID_SERIAL_LEN
);
7076 disk
->status
|= FAILED_DISK
;
7077 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
7078 /* mark failures in second map if second map exists and this disk
7080 * This is valid for migration, initialization and rebuild
7082 if (dev
->vol
.migr_state
) {
7083 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
7084 int slot2
= get_imsm_disk_slot(map2
, idx
);
7086 if (slot2
< map2
->num_members
&& slot2
>= 0)
7087 set_imsm_ord_tbl_ent(map2
, slot2
,
7088 idx
| IMSM_ORD_REBUILD
);
7090 if (map
->failed_disk_num
== 0xff)
7091 map
->failed_disk_num
= slot
;
7095 static void mark_missing(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
7097 mark_failure(dev
, disk
, idx
);
7099 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
7102 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
7103 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
7106 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
7110 if (!super
->missing
)
7113 /* When orom adds replacement for missing disk it does
7114 * not remove entry of missing disk, but just updates map with
7115 * new added disk. So it is not enough just to test if there is
7116 * any missing disk, we have to look if there are any failed disks
7117 * in map to stop migration */
7119 dprintf("imsm: mark missing\n");
7120 /* end process for initialization and rebuild only
7122 if (is_gen_migration(dev
) == 0) {
7126 failed
= imsm_count_failed(super
, dev
, MAP_0
);
7127 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7130 end_migration(dev
, super
, map_state
);
7132 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
7133 mark_missing(dev
, &dl
->disk
, dl
->index
);
7134 super
->updates_pending
++;
7137 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
,
7140 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
7141 unsigned long long array_blocks
;
7142 struct imsm_map
*map
;
7144 if (used_disks
== 0) {
7145 /* when problems occures
7146 * return current array_blocks value
7148 array_blocks
= __le32_to_cpu(dev
->size_high
);
7149 array_blocks
= array_blocks
<< 32;
7150 array_blocks
+= __le32_to_cpu(dev
->size_low
);
7152 return array_blocks
;
7155 /* set array size in metadata
7157 if (new_size
<= 0) {
7158 /* OLCE size change is caused by added disks
7160 map
= get_imsm_map(dev
, MAP_0
);
7161 array_blocks
= blocks_per_member(map
) * used_disks
;
7163 /* Online Volume Size Change
7164 * Using available free space
7166 array_blocks
= new_size
;
7169 /* round array size down to closest MB
7171 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
7172 dev
->size_low
= __cpu_to_le32((__u32
)array_blocks
);
7173 dev
->size_high
= __cpu_to_le32((__u32
)(array_blocks
>> 32));
7175 return array_blocks
;
7178 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
7180 static void imsm_progress_container_reshape(struct intel_super
*super
)
7182 /* if no device has a migr_state, but some device has a
7183 * different number of members than the previous device, start
7184 * changing the number of devices in this device to match
7187 struct imsm_super
*mpb
= super
->anchor
;
7188 int prev_disks
= -1;
7192 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7193 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
7194 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7195 struct imsm_map
*map2
;
7196 int prev_num_members
;
7198 if (dev
->vol
.migr_state
)
7201 if (prev_disks
== -1)
7202 prev_disks
= map
->num_members
;
7203 if (prev_disks
== map
->num_members
)
7206 /* OK, this array needs to enter reshape mode.
7207 * i.e it needs a migr_state
7210 copy_map_size
= sizeof_imsm_map(map
);
7211 prev_num_members
= map
->num_members
;
7212 map
->num_members
= prev_disks
;
7213 dev
->vol
.migr_state
= 1;
7214 dev
->vol
.curr_migr_unit
= 0;
7215 set_migr_type(dev
, MIGR_GEN_MIGR
);
7216 for (i
= prev_num_members
;
7217 i
< map
->num_members
; i
++)
7218 set_imsm_ord_tbl_ent(map
, i
, i
);
7219 map2
= get_imsm_map(dev
, MAP_1
);
7220 /* Copy the current map */
7221 memcpy(map2
, map
, copy_map_size
);
7222 map2
->num_members
= prev_num_members
;
7224 imsm_set_array_size(dev
, -1);
7225 super
->clean_migration_record_by_mdmon
= 1;
7226 super
->updates_pending
++;
7230 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
7231 * states are handled in imsm_set_disk() with one exception, when a
7232 * resync is stopped due to a new failure this routine will set the
7233 * 'degraded' state for the array.
7235 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
7237 int inst
= a
->info
.container_member
;
7238 struct intel_super
*super
= a
->container
->sb
;
7239 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7240 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7241 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
7242 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7243 __u32 blocks_per_unit
;
7245 if (dev
->vol
.migr_state
&&
7246 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
7247 /* array state change is blocked due to reshape action
7249 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
7250 * - finish the reshape (if last_checkpoint is big and action != reshape)
7251 * - update curr_migr_unit
7253 if (a
->curr_action
== reshape
) {
7254 /* still reshaping, maybe update curr_migr_unit */
7255 goto mark_checkpoint
;
7257 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
7258 /* for some reason we aborted the reshape.
7260 * disable automatic metadata rollback
7261 * user action is required to recover process
7264 struct imsm_map
*map2
=
7265 get_imsm_map(dev
, MAP_1
);
7266 dev
->vol
.migr_state
= 0;
7267 set_migr_type(dev
, 0);
7268 dev
->vol
.curr_migr_unit
= 0;
7270 sizeof_imsm_map(map2
));
7271 super
->updates_pending
++;
7274 if (a
->last_checkpoint
>= a
->info
.component_size
) {
7275 unsigned long long array_blocks
;
7279 used_disks
= imsm_num_data_members(dev
, MAP_0
);
7280 if (used_disks
> 0) {
7282 blocks_per_member(map
) *
7284 /* round array size down to closest MB
7286 array_blocks
= (array_blocks
7287 >> SECT_PER_MB_SHIFT
)
7288 << SECT_PER_MB_SHIFT
;
7289 a
->info
.custom_array_size
= array_blocks
;
7290 /* encourage manager to update array
7294 a
->check_reshape
= 1;
7296 /* finalize online capacity expansion/reshape */
7297 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
7299 mdi
->disk
.raid_disk
,
7302 imsm_progress_container_reshape(super
);
7307 /* before we activate this array handle any missing disks */
7308 if (consistent
== 2)
7309 handle_missing(super
, dev
);
7311 if (consistent
== 2 &&
7312 (!is_resync_complete(&a
->info
) ||
7313 map_state
!= IMSM_T_STATE_NORMAL
||
7314 dev
->vol
.migr_state
))
7317 if (is_resync_complete(&a
->info
)) {
7318 /* complete intialization / resync,
7319 * recovery and interrupted recovery is completed in
7322 if (is_resyncing(dev
)) {
7323 dprintf("imsm: mark resync done\n");
7324 end_migration(dev
, super
, map_state
);
7325 super
->updates_pending
++;
7326 a
->last_checkpoint
= 0;
7328 } else if ((!is_resyncing(dev
) && !failed
) &&
7329 (imsm_reshape_blocks_arrays_changes(super
) == 0)) {
7330 /* mark the start of the init process if nothing is failed */
7331 dprintf("imsm: mark resync start\n");
7332 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
7333 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
7335 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
7336 super
->updates_pending
++;
7340 /* skip checkpointing for general migration,
7341 * it is controlled in mdadm
7343 if (is_gen_migration(dev
))
7344 goto skip_mark_checkpoint
;
7346 /* check if we can update curr_migr_unit from resync_start, recovery_start */
7347 blocks_per_unit
= blocks_per_migr_unit(super
, dev
);
7348 if (blocks_per_unit
) {
7352 units
= a
->last_checkpoint
/ blocks_per_unit
;
7355 /* check that we did not overflow 32-bits, and that
7356 * curr_migr_unit needs updating
7358 if (units32
== units
&&
7360 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
7361 dprintf("imsm: mark checkpoint (%u)\n", units32
);
7362 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
7363 super
->updates_pending
++;
7367 skip_mark_checkpoint
:
7368 /* mark dirty / clean */
7369 if (dev
->vol
.dirty
!= !consistent
) {
7370 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
7375 super
->updates_pending
++;
7381 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
7383 int inst
= a
->info
.container_member
;
7384 struct intel_super
*super
= a
->container
->sb
;
7385 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7386 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7387 struct imsm_disk
*disk
;
7389 int recovery_not_finished
= 0;
7394 if (n
> map
->num_members
)
7395 pr_err("imsm: set_disk %d out of range 0..%d\n",
7396 n
, map
->num_members
- 1);
7401 dprintf("imsm: set_disk %d:%x\n", n
, state
);
7403 ord
= get_imsm_ord_tbl_ent(dev
, n
, MAP_0
);
7404 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
7406 /* check for new failures */
7407 if (state
& DS_FAULTY
) {
7408 if (mark_failure(dev
, disk
, ord_to_idx(ord
)))
7409 super
->updates_pending
++;
7412 /* check if in_sync */
7413 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
7414 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
7416 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
7417 super
->updates_pending
++;
7420 failed
= imsm_count_failed(super
, dev
, MAP_0
);
7421 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7423 /* check if recovery complete, newly degraded, or failed */
7424 dprintf("imsm: Detected transition to state ");
7425 switch (map_state
) {
7426 case IMSM_T_STATE_NORMAL
: /* transition to normal state */
7427 dprintf("normal: ");
7428 if (is_rebuilding(dev
)) {
7429 dprintf_cont("while rebuilding");
7430 /* check if recovery is really finished */
7431 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
7432 if (mdi
->recovery_start
!= MaxSector
) {
7433 recovery_not_finished
= 1;
7436 if (recovery_not_finished
) {
7438 dprintf("Rebuild has not finished yet, state not changed");
7439 if (a
->last_checkpoint
< mdi
->recovery_start
) {
7440 a
->last_checkpoint
= mdi
->recovery_start
;
7441 super
->updates_pending
++;
7445 end_migration(dev
, super
, map_state
);
7446 map
= get_imsm_map(dev
, MAP_0
);
7447 map
->failed_disk_num
= ~0;
7448 super
->updates_pending
++;
7449 a
->last_checkpoint
= 0;
7452 if (is_gen_migration(dev
)) {
7453 dprintf_cont("while general migration");
7454 if (a
->last_checkpoint
>= a
->info
.component_size
)
7455 end_migration(dev
, super
, map_state
);
7457 map
->map_state
= map_state
;
7458 map
= get_imsm_map(dev
, MAP_0
);
7459 map
->failed_disk_num
= ~0;
7460 super
->updates_pending
++;
7464 case IMSM_T_STATE_DEGRADED
: /* transition to degraded state */
7465 dprintf_cont("degraded: ");
7466 if (map
->map_state
!= map_state
&& !dev
->vol
.migr_state
) {
7467 dprintf_cont("mark degraded");
7468 map
->map_state
= map_state
;
7469 super
->updates_pending
++;
7470 a
->last_checkpoint
= 0;
7473 if (is_rebuilding(dev
)) {
7474 dprintf_cont("while rebuilding.");
7475 if (map
->map_state
!= map_state
) {
7476 dprintf_cont(" Map state change");
7477 end_migration(dev
, super
, map_state
);
7478 super
->updates_pending
++;
7482 if (is_gen_migration(dev
)) {
7483 dprintf_cont("while general migration");
7484 if (a
->last_checkpoint
>= a
->info
.component_size
)
7485 end_migration(dev
, super
, map_state
);
7487 map
->map_state
= map_state
;
7488 manage_second_map(super
, dev
);
7490 super
->updates_pending
++;
7493 if (is_initializing(dev
)) {
7494 dprintf_cont("while initialization.");
7495 map
->map_state
= map_state
;
7496 super
->updates_pending
++;
7500 case IMSM_T_STATE_FAILED
: /* transition to failed state */
7501 dprintf_cont("failed: ");
7502 if (is_gen_migration(dev
)) {
7503 dprintf_cont("while general migration");
7504 map
->map_state
= map_state
;
7505 super
->updates_pending
++;
7508 if (map
->map_state
!= map_state
) {
7509 dprintf_cont("mark failed");
7510 end_migration(dev
, super
, map_state
);
7511 super
->updates_pending
++;
7512 a
->last_checkpoint
= 0;
7517 dprintf_cont("state %i\n", map_state
);
7522 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
7525 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
7526 unsigned long long dsize
;
7527 unsigned long long sectors
;
7529 get_dev_size(fd
, NULL
, &dsize
);
7531 if (mpb_size
> 512) {
7532 /* -1 to account for anchor */
7533 sectors
= mpb_sectors(mpb
) - 1;
7535 /* write the extended mpb to the sectors preceeding the anchor */
7536 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0)
7539 if ((unsigned long long)write(fd
, buf
+ 512, 512 * sectors
)
7544 /* first block is stored on second to last sector of the disk */
7545 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0)
7548 if (write(fd
, buf
, 512) != 512)
7554 static void imsm_sync_metadata(struct supertype
*container
)
7556 struct intel_super
*super
= container
->sb
;
7558 dprintf("sync metadata: %d\n", super
->updates_pending
);
7559 if (!super
->updates_pending
)
7562 write_super_imsm(container
, 0);
7564 super
->updates_pending
= 0;
7567 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
7569 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7570 int i
= get_imsm_disk_idx(dev
, idx
, MAP_X
);
7573 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7577 if (dl
&& is_failed(&dl
->disk
))
7581 dprintf("found %x:%x\n", dl
->major
, dl
->minor
);
7586 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
7587 struct active_array
*a
, int activate_new
,
7588 struct mdinfo
*additional_test_list
)
7590 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7591 int idx
= get_imsm_disk_idx(dev
, slot
, MAP_X
);
7592 struct imsm_super
*mpb
= super
->anchor
;
7593 struct imsm_map
*map
;
7594 unsigned long long pos
;
7599 __u32 array_start
= 0;
7600 __u32 array_end
= 0;
7602 struct mdinfo
*test_list
;
7604 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7605 /* If in this array, skip */
7606 for (d
= a
->info
.devs
; d
; d
= d
->next
)
7607 if (d
->state_fd
>= 0 &&
7608 d
->disk
.major
== dl
->major
&&
7609 d
->disk
.minor
== dl
->minor
) {
7610 dprintf("%x:%x already in array\n",
7611 dl
->major
, dl
->minor
);
7616 test_list
= additional_test_list
;
7618 if (test_list
->disk
.major
== dl
->major
&&
7619 test_list
->disk
.minor
== dl
->minor
) {
7620 dprintf("%x:%x already in additional test list\n",
7621 dl
->major
, dl
->minor
);
7624 test_list
= test_list
->next
;
7629 /* skip in use or failed drives */
7630 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
7632 dprintf("%x:%x status (failed: %d index: %d)\n",
7633 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
7637 /* skip pure spares when we are looking for partially
7638 * assimilated drives
7640 if (dl
->index
== -1 && !activate_new
)
7643 /* Does this unused device have the requisite free space?
7644 * It needs to be able to cover all member volumes
7646 ex
= get_extents(super
, dl
);
7648 dprintf("cannot get extents\n");
7651 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7652 dev
= get_imsm_dev(super
, i
);
7653 map
= get_imsm_map(dev
, MAP_0
);
7655 /* check if this disk is already a member of
7658 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
7664 array_start
= pba_of_lba0(map
);
7665 array_end
= array_start
+
7666 blocks_per_member(map
) - 1;
7669 /* check that we can start at pba_of_lba0 with
7670 * blocks_per_member of space
7672 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
7676 pos
= ex
[j
].start
+ ex
[j
].size
;
7678 } while (ex
[j
-1].size
);
7685 if (i
< mpb
->num_raid_devs
) {
7686 dprintf("%x:%x does not have %u to %u available\n",
7687 dl
->major
, dl
->minor
, array_start
, array_end
);
7697 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
7699 struct imsm_dev
*dev2
;
7700 struct imsm_map
*map
;
7706 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
7708 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
, MAP_0
);
7709 if (state
== IMSM_T_STATE_FAILED
) {
7710 map
= get_imsm_map(dev2
, MAP_0
);
7713 for (slot
= 0; slot
< map
->num_members
; slot
++) {
7715 * Check if failed disks are deleted from intel
7716 * disk list or are marked to be deleted
7718 idx
= get_imsm_disk_idx(dev2
, slot
, MAP_X
);
7719 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
7721 * Do not rebuild the array if failed disks
7722 * from failed sub-array are not removed from
7726 is_failed(&idisk
->disk
) &&
7727 (idisk
->action
!= DISK_REMOVE
))
7735 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
7736 struct metadata_update
**updates
)
7739 * Find a device with unused free space and use it to replace a
7740 * failed/vacant region in an array. We replace failed regions one a
7741 * array at a time. The result is that a new spare disk will be added
7742 * to the first failed array and after the monitor has finished
7743 * propagating failures the remainder will be consumed.
7745 * FIXME add a capability for mdmon to request spares from another
7749 struct intel_super
*super
= a
->container
->sb
;
7750 int inst
= a
->info
.container_member
;
7751 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7752 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7753 int failed
= a
->info
.array
.raid_disks
;
7754 struct mdinfo
*rv
= NULL
;
7757 struct metadata_update
*mu
;
7759 struct imsm_update_activate_spare
*u
;
7764 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
7765 if ((d
->curr_state
& DS_FAULTY
) &&
7767 /* wait for Removal to happen */
7769 if (d
->state_fd
>= 0)
7773 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
7774 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
7776 if (imsm_reshape_blocks_arrays_changes(super
))
7779 /* Cannot activate another spare if rebuild is in progress already
7781 if (is_rebuilding(dev
)) {
7782 dprintf("imsm: No spare activation allowed. Rebuild in progress already.\n");
7786 if (a
->info
.array
.level
== 4)
7787 /* No repair for takeovered array
7788 * imsm doesn't support raid4
7792 if (imsm_check_degraded(super
, dev
, failed
, MAP_0
) !=
7793 IMSM_T_STATE_DEGRADED
)
7796 if (get_imsm_map(dev
, MAP_0
)->map_state
== IMSM_T_STATE_UNINITIALIZED
) {
7797 dprintf("imsm: No spare activation allowed. Volume is not initialized.\n");
7802 * If there are any failed disks check state of the other volume.
7803 * Block rebuild if the another one is failed until failed disks
7804 * are removed from container.
7807 dprintf("found failed disks in %.*s, check if there anotherfailed sub-array.\n",
7808 MAX_RAID_SERIAL_LEN
, dev
->volume
);
7809 /* check if states of the other volumes allow for rebuild */
7810 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
7812 allowed
= imsm_rebuild_allowed(a
->container
,
7820 /* For each slot, if it is not working, find a spare */
7821 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
7822 for (d
= a
->info
.devs
; d
; d
= d
->next
)
7823 if (d
->disk
.raid_disk
== i
)
7825 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
7826 if (d
&& (d
->state_fd
>= 0))
7830 * OK, this device needs recovery. Try to re-add the
7831 * previous occupant of this slot, if this fails see if
7832 * we can continue the assimilation of a spare that was
7833 * partially assimilated, finally try to activate a new
7836 dl
= imsm_readd(super
, i
, a
);
7838 dl
= imsm_add_spare(super
, i
, a
, 0, rv
);
7840 dl
= imsm_add_spare(super
, i
, a
, 1, rv
);
7844 /* found a usable disk with enough space */
7845 di
= xcalloc(1, sizeof(*di
));
7847 /* dl->index will be -1 in the case we are activating a
7848 * pristine spare. imsm_process_update() will create a
7849 * new index in this case. Once a disk is found to be
7850 * failed in all member arrays it is kicked from the
7853 di
->disk
.number
= dl
->index
;
7855 /* (ab)use di->devs to store a pointer to the device
7858 di
->devs
= (struct mdinfo
*) dl
;
7860 di
->disk
.raid_disk
= i
;
7861 di
->disk
.major
= dl
->major
;
7862 di
->disk
.minor
= dl
->minor
;
7864 di
->recovery_start
= 0;
7865 di
->data_offset
= pba_of_lba0(map
);
7866 di
->component_size
= a
->info
.component_size
;
7867 di
->container_member
= inst
;
7868 super
->random
= random32();
7872 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
7873 i
, di
->data_offset
);
7877 /* No spares found */
7879 /* Now 'rv' has a list of devices to return.
7880 * Create a metadata_update record to update the
7881 * disk_ord_tbl for the array
7883 mu
= xmalloc(sizeof(*mu
));
7884 mu
->buf
= xcalloc(num_spares
,
7885 sizeof(struct imsm_update_activate_spare
));
7887 mu
->space_list
= NULL
;
7888 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
7889 mu
->next
= *updates
;
7890 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
7892 for (di
= rv
; di
; di
= di
->next
) {
7893 u
->type
= update_activate_spare
;
7894 u
->dl
= (struct dl
*) di
->devs
;
7896 u
->slot
= di
->disk
.raid_disk
;
7907 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
7909 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
7910 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7911 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, MAP_0
);
7912 struct disk_info
*inf
= get_disk_info(u
);
7913 struct imsm_disk
*disk
;
7917 for (i
= 0; i
< map
->num_members
; i
++) {
7918 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, MAP_X
));
7919 for (j
= 0; j
< new_map
->num_members
; j
++)
7920 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
7927 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
7931 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7932 if (dl
->major
== major
&& dl
->minor
== minor
)
7937 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
7943 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7944 if (dl
->major
== major
&& dl
->minor
== minor
) {
7947 prev
->next
= dl
->next
;
7949 super
->disks
= dl
->next
;
7951 __free_imsm_disk(dl
);
7952 dprintf("removed %x:%x\n", major
, minor
);
7960 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
7962 static int add_remove_disk_update(struct intel_super
*super
)
7964 int check_degraded
= 0;
7967 /* add/remove some spares to/from the metadata/contrainer */
7968 while (super
->disk_mgmt_list
) {
7969 struct dl
*disk_cfg
;
7971 disk_cfg
= super
->disk_mgmt_list
;
7972 super
->disk_mgmt_list
= disk_cfg
->next
;
7973 disk_cfg
->next
= NULL
;
7975 if (disk_cfg
->action
== DISK_ADD
) {
7976 disk_cfg
->next
= super
->disks
;
7977 super
->disks
= disk_cfg
;
7979 dprintf("added %x:%x\n",
7980 disk_cfg
->major
, disk_cfg
->minor
);
7981 } else if (disk_cfg
->action
== DISK_REMOVE
) {
7982 dprintf("Disk remove action processed: %x.%x\n",
7983 disk_cfg
->major
, disk_cfg
->minor
);
7984 disk
= get_disk_super(super
,
7988 /* store action status */
7989 disk
->action
= DISK_REMOVE
;
7990 /* remove spare disks only */
7991 if (disk
->index
== -1) {
7992 remove_disk_super(super
,
7997 /* release allocate disk structure */
7998 __free_imsm_disk(disk_cfg
);
8001 return check_degraded
;
8004 static int apply_reshape_migration_update(struct imsm_update_reshape_migration
*u
,
8005 struct intel_super
*super
,
8008 struct intel_dev
*id
;
8009 void **tofree
= NULL
;
8012 dprintf("(enter)\n");
8013 if (u
->subdev
< 0 || u
->subdev
> 1) {
8014 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
8017 if (space_list
== NULL
|| *space_list
== NULL
) {
8018 dprintf("imsm: Error: Memory is not allocated\n");
8022 for (id
= super
->devlist
; id
; id
= id
->next
) {
8023 if (id
->index
== (unsigned)u
->subdev
) {
8024 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
8025 struct imsm_map
*map
;
8026 struct imsm_dev
*new_dev
=
8027 (struct imsm_dev
*)*space_list
;
8028 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
8030 struct dl
*new_disk
;
8032 if (new_dev
== NULL
)
8034 *space_list
= **space_list
;
8035 memcpy(new_dev
, dev
, sizeof_imsm_dev(dev
, 0));
8036 map
= get_imsm_map(new_dev
, MAP_0
);
8038 dprintf("imsm: Error: migration in progress");
8042 to_state
= map
->map_state
;
8043 if ((u
->new_level
== 5) && (map
->raid_level
== 0)) {
8045 /* this should not happen */
8046 if (u
->new_disks
[0] < 0) {
8047 map
->failed_disk_num
=
8048 map
->num_members
- 1;
8049 to_state
= IMSM_T_STATE_DEGRADED
;
8051 to_state
= IMSM_T_STATE_NORMAL
;
8053 migrate(new_dev
, super
, to_state
, MIGR_GEN_MIGR
);
8054 if (u
->new_level
> -1)
8055 map
->raid_level
= u
->new_level
;
8056 migr_map
= get_imsm_map(new_dev
, MAP_1
);
8057 if ((u
->new_level
== 5) &&
8058 (migr_map
->raid_level
== 0)) {
8059 int ord
= map
->num_members
- 1;
8060 migr_map
->num_members
--;
8061 if (u
->new_disks
[0] < 0)
8062 ord
|= IMSM_ORD_REBUILD
;
8063 set_imsm_ord_tbl_ent(map
,
8064 map
->num_members
- 1,
8068 tofree
= (void **)dev
;
8070 /* update chunk size
8072 if (u
->new_chunksize
> 0)
8073 map
->blocks_per_strip
=
8074 __cpu_to_le16(u
->new_chunksize
* 2);
8078 if (u
->new_level
!= 5 || migr_map
->raid_level
!= 0 ||
8079 migr_map
->raid_level
== map
->raid_level
)
8082 if (u
->new_disks
[0] >= 0) {
8085 new_disk
= get_disk_super(super
,
8086 major(u
->new_disks
[0]),
8087 minor(u
->new_disks
[0]));
8088 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
8089 major(u
->new_disks
[0]),
8090 minor(u
->new_disks
[0]),
8091 new_disk
, new_disk
->index
);
8092 if (new_disk
== NULL
)
8093 goto error_disk_add
;
8095 new_disk
->index
= map
->num_members
- 1;
8096 /* slot to fill in autolayout
8098 new_disk
->raiddisk
= new_disk
->index
;
8099 new_disk
->disk
.status
|= CONFIGURED_DISK
;
8100 new_disk
->disk
.status
&= ~SPARE_DISK
;
8102 goto error_disk_add
;
8105 *tofree
= *space_list
;
8106 /* calculate new size
8108 imsm_set_array_size(new_dev
, -1);
8115 *space_list
= tofree
;
8119 dprintf("Error: imsm: Cannot find disk.\n");
8123 static int apply_size_change_update(struct imsm_update_size_change
*u
,
8124 struct intel_super
*super
)
8126 struct intel_dev
*id
;
8129 dprintf("(enter)\n");
8130 if (u
->subdev
< 0 || u
->subdev
> 1) {
8131 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
8135 for (id
= super
->devlist
; id
; id
= id
->next
) {
8136 if (id
->index
== (unsigned)u
->subdev
) {
8137 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
8138 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8139 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
8140 unsigned long long blocks_per_member
;
8142 /* calculate new size
8144 blocks_per_member
= u
->new_size
/ used_disks
;
8145 dprintf("(size: %llu, blocks per member: %llu)\n",
8146 u
->new_size
, blocks_per_member
);
8147 set_blocks_per_member(map
, blocks_per_member
);
8148 imsm_set_array_size(dev
, u
->new_size
);
8158 static int apply_update_activate_spare(struct imsm_update_activate_spare
*u
,
8159 struct intel_super
*super
,
8160 struct active_array
*active_array
)
8162 struct imsm_super
*mpb
= super
->anchor
;
8163 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
8164 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8165 struct imsm_map
*migr_map
;
8166 struct active_array
*a
;
8167 struct imsm_disk
*disk
;
8174 int second_map_created
= 0;
8176 for (; u
; u
= u
->next
) {
8177 victim
= get_imsm_disk_idx(dev
, u
->slot
, MAP_X
);
8182 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
8187 pr_err("error: imsm_activate_spare passed an unknown disk (index: %d)\n",
8192 /* count failures (excluding rebuilds and the victim)
8193 * to determine map[0] state
8196 for (i
= 0; i
< map
->num_members
; i
++) {
8199 disk
= get_imsm_disk(super
,
8200 get_imsm_disk_idx(dev
, i
, MAP_X
));
8201 if (!disk
|| is_failed(disk
))
8205 /* adding a pristine spare, assign a new index */
8206 if (dl
->index
< 0) {
8207 dl
->index
= super
->anchor
->num_disks
;
8208 super
->anchor
->num_disks
++;
8211 disk
->status
|= CONFIGURED_DISK
;
8212 disk
->status
&= ~SPARE_DISK
;
8215 to_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8216 if (!second_map_created
) {
8217 second_map_created
= 1;
8218 map
->map_state
= IMSM_T_STATE_DEGRADED
;
8219 migrate(dev
, super
, to_state
, MIGR_REBUILD
);
8221 map
->map_state
= to_state
;
8222 migr_map
= get_imsm_map(dev
, MAP_1
);
8223 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
8224 set_imsm_ord_tbl_ent(migr_map
, u
->slot
,
8225 dl
->index
| IMSM_ORD_REBUILD
);
8227 /* update the family_num to mark a new container
8228 * generation, being careful to record the existing
8229 * family_num in orig_family_num to clean up after
8230 * earlier mdadm versions that neglected to set it.
8232 if (mpb
->orig_family_num
== 0)
8233 mpb
->orig_family_num
= mpb
->family_num
;
8234 mpb
->family_num
+= super
->random
;
8236 /* count arrays using the victim in the metadata */
8238 for (a
= active_array
; a
; a
= a
->next
) {
8239 dev
= get_imsm_dev(super
, a
->info
.container_member
);
8240 map
= get_imsm_map(dev
, MAP_0
);
8242 if (get_imsm_disk_slot(map
, victim
) >= 0)
8246 /* delete the victim if it is no longer being
8252 /* We know that 'manager' isn't touching anything,
8253 * so it is safe to delete
8255 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
8256 if ((*dlp
)->index
== victim
)
8259 /* victim may be on the missing list */
8261 for (dlp
= &super
->missing
; *dlp
;
8262 dlp
= &(*dlp
)->next
)
8263 if ((*dlp
)->index
== victim
)
8265 imsm_delete(super
, dlp
, victim
);
8272 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
8273 struct intel_super
*super
,
8276 struct dl
*new_disk
;
8277 struct intel_dev
*id
;
8279 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
8280 int disk_count
= u
->old_raid_disks
;
8281 void **tofree
= NULL
;
8282 int devices_to_reshape
= 1;
8283 struct imsm_super
*mpb
= super
->anchor
;
8285 unsigned int dev_id
;
8287 dprintf("(enter)\n");
8289 /* enable spares to use in array */
8290 for (i
= 0; i
< delta_disks
; i
++) {
8291 new_disk
= get_disk_super(super
,
8292 major(u
->new_disks
[i
]),
8293 minor(u
->new_disks
[i
]));
8294 dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
8295 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
8296 new_disk
, new_disk
->index
);
8297 if (new_disk
== NULL
||
8298 (new_disk
->index
>= 0 &&
8299 new_disk
->index
< u
->old_raid_disks
))
8300 goto update_reshape_exit
;
8301 new_disk
->index
= disk_count
++;
8302 /* slot to fill in autolayout
8304 new_disk
->raiddisk
= new_disk
->index
;
8305 new_disk
->disk
.status
|=
8307 new_disk
->disk
.status
&= ~SPARE_DISK
;
8310 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
8311 mpb
->num_raid_devs
);
8312 /* manage changes in volume
8314 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
8315 void **sp
= *space_list
;
8316 struct imsm_dev
*newdev
;
8317 struct imsm_map
*newmap
, *oldmap
;
8319 for (id
= super
->devlist
; id
; id
= id
->next
) {
8320 if (id
->index
== dev_id
)
8329 /* Copy the dev, but not (all of) the map */
8330 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
8331 oldmap
= get_imsm_map(id
->dev
, MAP_0
);
8332 newmap
= get_imsm_map(newdev
, MAP_0
);
8333 /* Copy the current map */
8334 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8335 /* update one device only
8337 if (devices_to_reshape
) {
8338 dprintf("imsm: modifying subdev: %i\n",
8340 devices_to_reshape
--;
8341 newdev
->vol
.migr_state
= 1;
8342 newdev
->vol
.curr_migr_unit
= 0;
8343 set_migr_type(newdev
, MIGR_GEN_MIGR
);
8344 newmap
->num_members
= u
->new_raid_disks
;
8345 for (i
= 0; i
< delta_disks
; i
++) {
8346 set_imsm_ord_tbl_ent(newmap
,
8347 u
->old_raid_disks
+ i
,
8348 u
->old_raid_disks
+ i
);
8350 /* New map is correct, now need to save old map
8352 newmap
= get_imsm_map(newdev
, MAP_1
);
8353 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8355 imsm_set_array_size(newdev
, -1);
8358 sp
= (void **)id
->dev
;
8363 /* Clear migration record */
8364 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
8367 *space_list
= tofree
;
8370 update_reshape_exit
:
8375 static int apply_takeover_update(struct imsm_update_takeover
*u
,
8376 struct intel_super
*super
,
8379 struct imsm_dev
*dev
= NULL
;
8380 struct intel_dev
*dv
;
8381 struct imsm_dev
*dev_new
;
8382 struct imsm_map
*map
;
8386 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
8387 if (dv
->index
== (unsigned int)u
->subarray
) {
8395 map
= get_imsm_map(dev
, MAP_0
);
8397 if (u
->direction
== R10_TO_R0
) {
8398 /* Number of failed disks must be half of initial disk number */
8399 if (imsm_count_failed(super
, dev
, MAP_0
) !=
8400 (map
->num_members
/ 2))
8403 /* iterate through devices to mark removed disks as spare */
8404 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8405 if (dm
->disk
.status
& FAILED_DISK
) {
8406 int idx
= dm
->index
;
8407 /* update indexes on the disk list */
8408 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
8409 the index values will end up being correct.... NB */
8410 for (du
= super
->disks
; du
; du
= du
->next
)
8411 if (du
->index
> idx
)
8413 /* mark as spare disk */
8418 map
->num_members
= map
->num_members
/ 2;
8419 map
->map_state
= IMSM_T_STATE_NORMAL
;
8420 map
->num_domains
= 1;
8421 map
->raid_level
= 0;
8422 map
->failed_disk_num
= -1;
8425 if (u
->direction
== R0_TO_R10
) {
8427 /* update slots in current disk list */
8428 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8432 /* create new *missing* disks */
8433 for (i
= 0; i
< map
->num_members
; i
++) {
8434 space
= *space_list
;
8437 *space_list
= *space
;
8439 memcpy(du
, super
->disks
, sizeof(*du
));
8443 du
->index
= (i
* 2) + 1;
8444 sprintf((char *)du
->disk
.serial
,
8445 " MISSING_%d", du
->index
);
8446 sprintf((char *)du
->serial
,
8447 "MISSING_%d", du
->index
);
8448 du
->next
= super
->missing
;
8449 super
->missing
= du
;
8451 /* create new dev and map */
8452 space
= *space_list
;
8455 *space_list
= *space
;
8456 dev_new
= (void *)space
;
8457 memcpy(dev_new
, dev
, sizeof(*dev
));
8458 /* update new map */
8459 map
= get_imsm_map(dev_new
, MAP_0
);
8460 map
->num_members
= map
->num_members
* 2;
8461 map
->map_state
= IMSM_T_STATE_DEGRADED
;
8462 map
->num_domains
= 2;
8463 map
->raid_level
= 1;
8464 /* replace dev<->dev_new */
8467 /* update disk order table */
8468 for (du
= super
->disks
; du
; du
= du
->next
)
8470 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8471 for (du
= super
->missing
; du
; du
= du
->next
)
8472 if (du
->index
>= 0) {
8473 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8474 mark_missing(dv
->dev
, &du
->disk
, du
->index
);
8480 static void imsm_process_update(struct supertype
*st
,
8481 struct metadata_update
*update
)
8484 * crack open the metadata_update envelope to find the update record
8485 * update can be one of:
8486 * update_reshape_container_disks - all the arrays in the container
8487 * are being reshaped to have more devices. We need to mark
8488 * the arrays for general migration and convert selected spares
8489 * into active devices.
8490 * update_activate_spare - a spare device has replaced a failed
8491 * device in an array, update the disk_ord_tbl. If this disk is
8492 * present in all member arrays then also clear the SPARE_DISK
8494 * update_create_array
8496 * update_rename_array
8497 * update_add_remove_disk
8499 struct intel_super
*super
= st
->sb
;
8500 struct imsm_super
*mpb
;
8501 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
8503 /* update requires a larger buf but the allocation failed */
8504 if (super
->next_len
&& !super
->next_buf
) {
8505 super
->next_len
= 0;
8509 if (super
->next_buf
) {
8510 memcpy(super
->next_buf
, super
->buf
, super
->len
);
8512 super
->len
= super
->next_len
;
8513 super
->buf
= super
->next_buf
;
8515 super
->next_len
= 0;
8516 super
->next_buf
= NULL
;
8519 mpb
= super
->anchor
;
8522 case update_general_migration_checkpoint
: {
8523 struct intel_dev
*id
;
8524 struct imsm_update_general_migration_checkpoint
*u
=
8525 (void *)update
->buf
;
8527 dprintf("called for update_general_migration_checkpoint\n");
8529 /* find device under general migration */
8530 for (id
= super
->devlist
; id
; id
= id
->next
) {
8531 if (is_gen_migration(id
->dev
)) {
8532 id
->dev
->vol
.curr_migr_unit
=
8533 __cpu_to_le32(u
->curr_migr_unit
);
8534 super
->updates_pending
++;
8539 case update_takeover
: {
8540 struct imsm_update_takeover
*u
= (void *)update
->buf
;
8541 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
8542 imsm_update_version_info(super
);
8543 super
->updates_pending
++;
8548 case update_reshape_container_disks
: {
8549 struct imsm_update_reshape
*u
= (void *)update
->buf
;
8550 if (apply_reshape_container_disks_update(
8551 u
, super
, &update
->space_list
))
8552 super
->updates_pending
++;
8555 case update_reshape_migration
: {
8556 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
8557 if (apply_reshape_migration_update(
8558 u
, super
, &update
->space_list
))
8559 super
->updates_pending
++;
8562 case update_size_change
: {
8563 struct imsm_update_size_change
*u
= (void *)update
->buf
;
8564 if (apply_size_change_update(u
, super
))
8565 super
->updates_pending
++;
8568 case update_activate_spare
: {
8569 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
8570 if (apply_update_activate_spare(u
, super
, st
->arrays
))
8571 super
->updates_pending
++;
8574 case update_create_array
: {
8575 /* someone wants to create a new array, we need to be aware of
8576 * a few races/collisions:
8577 * 1/ 'Create' called by two separate instances of mdadm
8578 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
8579 * devices that have since been assimilated via
8581 * In the event this update can not be carried out mdadm will
8582 * (FIX ME) notice that its update did not take hold.
8584 struct imsm_update_create_array
*u
= (void *) update
->buf
;
8585 struct intel_dev
*dv
;
8586 struct imsm_dev
*dev
;
8587 struct imsm_map
*map
, *new_map
;
8588 unsigned long long start
, end
;
8589 unsigned long long new_start
, new_end
;
8591 struct disk_info
*inf
;
8594 /* handle racing creates: first come first serve */
8595 if (u
->dev_idx
< mpb
->num_raid_devs
) {
8596 dprintf("subarray %d already defined\n", u
->dev_idx
);
8600 /* check update is next in sequence */
8601 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
8602 dprintf("can not create array %d expected index %d\n",
8603 u
->dev_idx
, mpb
->num_raid_devs
);
8607 new_map
= get_imsm_map(&u
->dev
, MAP_0
);
8608 new_start
= pba_of_lba0(new_map
);
8609 new_end
= new_start
+ blocks_per_member(new_map
);
8610 inf
= get_disk_info(u
);
8612 /* handle activate_spare versus create race:
8613 * check to make sure that overlapping arrays do not include
8616 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8617 dev
= get_imsm_dev(super
, i
);
8618 map
= get_imsm_map(dev
, MAP_0
);
8619 start
= pba_of_lba0(map
);
8620 end
= start
+ blocks_per_member(map
);
8621 if ((new_start
>= start
&& new_start
<= end
) ||
8622 (start
>= new_start
&& start
<= new_end
))
8627 if (disks_overlap(super
, i
, u
)) {
8628 dprintf("arrays overlap\n");
8633 /* check that prepare update was successful */
8634 if (!update
->space
) {
8635 dprintf("prepare update failed\n");
8639 /* check that all disks are still active before committing
8640 * changes. FIXME: could we instead handle this by creating a
8641 * degraded array? That's probably not what the user expects,
8642 * so better to drop this update on the floor.
8644 for (i
= 0; i
< new_map
->num_members
; i
++) {
8645 dl
= serial_to_dl(inf
[i
].serial
, super
);
8647 dprintf("disk disappeared\n");
8652 super
->updates_pending
++;
8654 /* convert spares to members and fixup ord_tbl */
8655 for (i
= 0; i
< new_map
->num_members
; i
++) {
8656 dl
= serial_to_dl(inf
[i
].serial
, super
);
8657 if (dl
->index
== -1) {
8658 dl
->index
= mpb
->num_disks
;
8660 dl
->disk
.status
|= CONFIGURED_DISK
;
8661 dl
->disk
.status
&= ~SPARE_DISK
;
8663 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
8668 update
->space
= NULL
;
8669 imsm_copy_dev(dev
, &u
->dev
);
8670 dv
->index
= u
->dev_idx
;
8671 dv
->next
= super
->devlist
;
8672 super
->devlist
= dv
;
8673 mpb
->num_raid_devs
++;
8675 imsm_update_version_info(super
);
8678 /* mdmon knows how to release update->space, but not
8679 * ((struct intel_dev *) update->space)->dev
8681 if (update
->space
) {
8687 case update_kill_array
: {
8688 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
8689 int victim
= u
->dev_idx
;
8690 struct active_array
*a
;
8691 struct intel_dev
**dp
;
8692 struct imsm_dev
*dev
;
8694 /* sanity check that we are not affecting the uuid of
8695 * active arrays, or deleting an active array
8697 * FIXME when immutable ids are available, but note that
8698 * we'll also need to fixup the invalidated/active
8699 * subarray indexes in mdstat
8701 for (a
= st
->arrays
; a
; a
= a
->next
)
8702 if (a
->info
.container_member
>= victim
)
8704 /* by definition if mdmon is running at least one array
8705 * is active in the container, so checking
8706 * mpb->num_raid_devs is just extra paranoia
8708 dev
= get_imsm_dev(super
, victim
);
8709 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
8710 dprintf("failed to delete subarray-%d\n", victim
);
8714 for (dp
= &super
->devlist
; *dp
;)
8715 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
8718 if ((*dp
)->index
> (unsigned)victim
)
8722 mpb
->num_raid_devs
--;
8723 super
->updates_pending
++;
8726 case update_rename_array
: {
8727 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
8728 char name
[MAX_RAID_SERIAL_LEN
+1];
8729 int target
= u
->dev_idx
;
8730 struct active_array
*a
;
8731 struct imsm_dev
*dev
;
8733 /* sanity check that we are not affecting the uuid of
8736 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
8737 name
[MAX_RAID_SERIAL_LEN
] = '\0';
8738 for (a
= st
->arrays
; a
; a
= a
->next
)
8739 if (a
->info
.container_member
== target
)
8741 dev
= get_imsm_dev(super
, u
->dev_idx
);
8742 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
8743 dprintf("failed to rename subarray-%d\n", target
);
8747 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
8748 super
->updates_pending
++;
8751 case update_add_remove_disk
: {
8752 /* we may be able to repair some arrays if disks are
8753 * being added, check the status of add_remove_disk
8754 * if discs has been added.
8756 if (add_remove_disk_update(super
)) {
8757 struct active_array
*a
;
8759 super
->updates_pending
++;
8760 for (a
= st
->arrays
; a
; a
= a
->next
)
8761 a
->check_degraded
= 1;
8766 pr_err("error: unsuported process update type:(type: %d)\n", type
);
8770 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
);
8772 static int imsm_prepare_update(struct supertype
*st
,
8773 struct metadata_update
*update
)
8776 * Allocate space to hold new disk entries, raid-device entries or a new
8777 * mpb if necessary. The manager synchronously waits for updates to
8778 * complete in the monitor, so new mpb buffers allocated here can be
8779 * integrated by the monitor thread without worrying about live pointers
8780 * in the manager thread.
8782 enum imsm_update_type type
;
8783 struct intel_super
*super
= st
->sb
;
8784 struct imsm_super
*mpb
= super
->anchor
;
8788 if (update
->len
< (int)sizeof(type
))
8791 type
= *(enum imsm_update_type
*) update
->buf
;
8794 case update_general_migration_checkpoint
:
8795 if (update
->len
< (int)sizeof(struct imsm_update_general_migration_checkpoint
))
8797 dprintf("called for update_general_migration_checkpoint\n");
8799 case update_takeover
: {
8800 struct imsm_update_takeover
*u
= (void *)update
->buf
;
8801 if (update
->len
< (int)sizeof(*u
))
8803 if (u
->direction
== R0_TO_R10
) {
8804 void **tail
= (void **)&update
->space_list
;
8805 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
8806 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8807 int num_members
= map
->num_members
;
8810 /* allocate memory for added disks */
8811 for (i
= 0; i
< num_members
; i
++) {
8812 size
= sizeof(struct dl
);
8813 space
= xmalloc(size
);
8818 /* allocate memory for new device */
8819 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
8820 (num_members
* sizeof(__u32
));
8821 space
= xmalloc(size
);
8825 len
= disks_to_mpb_size(num_members
* 2);
8830 case update_reshape_container_disks
: {
8831 /* Every raid device in the container is about to
8832 * gain some more devices, and we will enter a
8834 * So each 'imsm_map' will be bigger, and the imsm_vol
8835 * will now hold 2 of them.
8836 * Thus we need new 'struct imsm_dev' allocations sized
8837 * as sizeof_imsm_dev but with more devices in both maps.
8839 struct imsm_update_reshape
*u
= (void *)update
->buf
;
8840 struct intel_dev
*dl
;
8841 void **space_tail
= (void**)&update
->space_list
;
8843 if (update
->len
< (int)sizeof(*u
))
8846 dprintf("for update_reshape\n");
8848 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
8849 int size
= sizeof_imsm_dev(dl
->dev
, 1);
8851 if (u
->new_raid_disks
> u
->old_raid_disks
)
8852 size
+= sizeof(__u32
)*2*
8853 (u
->new_raid_disks
- u
->old_raid_disks
);
8860 len
= disks_to_mpb_size(u
->new_raid_disks
);
8861 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
8864 case update_reshape_migration
: {
8865 /* for migration level 0->5 we need to add disks
8866 * so the same as for container operation we will copy
8867 * device to the bigger location.
8868 * in memory prepared device and new disk area are prepared
8869 * for usage in process update
8871 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
8872 struct intel_dev
*id
;
8873 void **space_tail
= (void **)&update
->space_list
;
8876 int current_level
= -1;
8878 if (update
->len
< (int)sizeof(*u
))
8881 dprintf("for update_reshape\n");
8883 /* add space for bigger array in update
8885 for (id
= super
->devlist
; id
; id
= id
->next
) {
8886 if (id
->index
== (unsigned)u
->subdev
) {
8887 size
= sizeof_imsm_dev(id
->dev
, 1);
8888 if (u
->new_raid_disks
> u
->old_raid_disks
)
8889 size
+= sizeof(__u32
)*2*
8890 (u
->new_raid_disks
- u
->old_raid_disks
);
8898 if (update
->space_list
== NULL
)
8901 /* add space for disk in update
8903 size
= sizeof(struct dl
);
8909 /* add spare device to update
8911 for (id
= super
->devlist
; id
; id
= id
->next
)
8912 if (id
->index
== (unsigned)u
->subdev
) {
8913 struct imsm_dev
*dev
;
8914 struct imsm_map
*map
;
8916 dev
= get_imsm_dev(super
, u
->subdev
);
8917 map
= get_imsm_map(dev
, MAP_0
);
8918 current_level
= map
->raid_level
;
8921 if (u
->new_level
== 5 && u
->new_level
!= current_level
) {
8922 struct mdinfo
*spares
;
8924 spares
= get_spares_for_grow(st
);
8932 makedev(dev
->disk
.major
,
8934 dl
= get_disk_super(super
,
8937 dl
->index
= u
->old_raid_disks
;
8943 len
= disks_to_mpb_size(u
->new_raid_disks
);
8944 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
8947 case update_size_change
: {
8948 if (update
->len
< (int)sizeof(struct imsm_update_size_change
))
8952 case update_activate_spare
: {
8953 if (update
->len
< (int)sizeof(struct imsm_update_activate_spare
))
8957 case update_create_array
: {
8958 struct imsm_update_create_array
*u
= (void *) update
->buf
;
8959 struct intel_dev
*dv
;
8960 struct imsm_dev
*dev
= &u
->dev
;
8961 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8963 struct disk_info
*inf
;
8967 if (update
->len
< (int)sizeof(*u
))
8970 inf
= get_disk_info(u
);
8971 len
= sizeof_imsm_dev(dev
, 1);
8972 /* allocate a new super->devlist entry */
8973 dv
= xmalloc(sizeof(*dv
));
8974 dv
->dev
= xmalloc(len
);
8977 /* count how many spares will be converted to members */
8978 for (i
= 0; i
< map
->num_members
; i
++) {
8979 dl
= serial_to_dl(inf
[i
].serial
, super
);
8981 /* hmm maybe it failed?, nothing we can do about
8986 if (count_memberships(dl
, super
) == 0)
8989 len
+= activate
* sizeof(struct imsm_disk
);
8992 case update_kill_array
: {
8993 if (update
->len
< (int)sizeof(struct imsm_update_kill_array
))
8997 case update_rename_array
: {
8998 if (update
->len
< (int)sizeof(struct imsm_update_rename_array
))
9002 case update_add_remove_disk
:
9003 /* no update->len needed */
9009 /* check if we need a larger metadata buffer */
9010 if (super
->next_buf
)
9011 buf_len
= super
->next_len
;
9013 buf_len
= super
->len
;
9015 if (__le32_to_cpu(mpb
->mpb_size
) + len
> buf_len
) {
9016 /* ok we need a larger buf than what is currently allocated
9017 * if this allocation fails process_update will notice that
9018 * ->next_len is set and ->next_buf is NULL
9020 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + len
, 512);
9021 if (super
->next_buf
)
9022 free(super
->next_buf
);
9024 super
->next_len
= buf_len
;
9025 if (posix_memalign(&super
->next_buf
, 512, buf_len
) == 0)
9026 memset(super
->next_buf
, 0, buf_len
);
9028 super
->next_buf
= NULL
;
9033 /* must be called while manager is quiesced */
9034 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
9036 struct imsm_super
*mpb
= super
->anchor
;
9038 struct imsm_dev
*dev
;
9039 struct imsm_map
*map
;
9040 int i
, j
, num_members
;
9043 dprintf("deleting device[%d] from imsm_super\n", index
);
9045 /* shift all indexes down one */
9046 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
9047 if (iter
->index
> (int)index
)
9049 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
9050 if (iter
->index
> (int)index
)
9053 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
9054 dev
= get_imsm_dev(super
, i
);
9055 map
= get_imsm_map(dev
, MAP_0
);
9056 num_members
= map
->num_members
;
9057 for (j
= 0; j
< num_members
; j
++) {
9058 /* update ord entries being careful not to propagate
9059 * ord-flags to the first map
9061 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_X
);
9063 if (ord_to_idx(ord
) <= index
)
9066 map
= get_imsm_map(dev
, MAP_0
);
9067 set_imsm_ord_tbl_ent(map
, j
, ord_to_idx(ord
- 1));
9068 map
= get_imsm_map(dev
, MAP_1
);
9070 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
9075 super
->updates_pending
++;
9077 struct dl
*dl
= *dlp
;
9079 *dlp
= (*dlp
)->next
;
9080 __free_imsm_disk(dl
);
9083 #endif /* MDASSEMBLE */
9085 static void close_targets(int *targets
, int new_disks
)
9092 for (i
= 0; i
< new_disks
; i
++) {
9093 if (targets
[i
] >= 0) {
9100 static int imsm_get_allowed_degradation(int level
, int raid_disks
,
9101 struct intel_super
*super
,
9102 struct imsm_dev
*dev
)
9108 struct imsm_map
*map
;
9111 ret_val
= raid_disks
/2;
9112 /* check map if all disks pairs not failed
9115 map
= get_imsm_map(dev
, MAP_0
);
9116 for (i
= 0; i
< ret_val
; i
++) {
9117 int degradation
= 0;
9118 if (get_imsm_disk(super
, i
) == NULL
)
9120 if (get_imsm_disk(super
, i
+ 1) == NULL
)
9122 if (degradation
== 2)
9125 map
= get_imsm_map(dev
, MAP_1
);
9126 /* if there is no second map
9127 * result can be returned
9131 /* check degradation in second map
9133 for (i
= 0; i
< ret_val
; i
++) {
9134 int degradation
= 0;
9135 if (get_imsm_disk(super
, i
) == NULL
)
9137 if (get_imsm_disk(super
, i
+ 1) == NULL
)
9139 if (degradation
== 2)
9153 /*******************************************************************************
9154 * Function: open_backup_targets
9155 * Description: Function opens file descriptors for all devices given in
9158 * info : general array info
9159 * raid_disks : number of disks
9160 * raid_fds : table of device's file descriptors
9161 * super : intel super for raid10 degradation check
9162 * dev : intel device for raid10 degradation check
9166 ******************************************************************************/
9167 int open_backup_targets(struct mdinfo
*info
, int raid_disks
, int *raid_fds
,
9168 struct intel_super
*super
, struct imsm_dev
*dev
)
9174 for (i
= 0; i
< raid_disks
; i
++)
9177 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
9180 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
)) {
9181 dprintf("disk is faulty!!\n");
9185 if (sd
->disk
.raid_disk
>= raid_disks
|| sd
->disk
.raid_disk
< 0)
9188 dn
= map_dev(sd
->disk
.major
,
9190 raid_fds
[sd
->disk
.raid_disk
] = dev_open(dn
, O_RDWR
);
9191 if (raid_fds
[sd
->disk
.raid_disk
] < 0) {
9192 pr_err("cannot open component\n");
9197 /* check if maximum array degradation level is not exceeded
9199 if ((raid_disks
- opened
) >
9200 imsm_get_allowed_degradation(info
->new_level
, raid_disks
,
9202 pr_err("Not enough disks can be opened.\n");
9203 close_targets(raid_fds
, raid_disks
);
9209 /*******************************************************************************
9210 * Function: validate_container_imsm
9211 * Description: This routine validates container after assemble,
9212 * eg. if devices in container are under the same controller.
9215 * info : linked list with info about devices used in array
9219 ******************************************************************************/
9220 int validate_container_imsm(struct mdinfo
*info
)
9222 if (check_env("IMSM_NO_PLATFORM"))
9225 struct sys_dev
*idev
;
9226 struct sys_dev
*hba
= NULL
;
9227 struct sys_dev
*intel_devices
= find_intel_devices();
9228 char *dev_path
= devt_to_devpath(makedev(info
->disk
.major
,
9231 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
9232 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
9241 pr_err("WARNING - Cannot detect HBA for device %s!\n",
9242 devid2kname(makedev(info
->disk
.major
, info
->disk
.minor
)));
9246 const struct imsm_orom
*orom
= get_orom_by_device_id(hba
->dev_id
);
9249 for (dev
= info
->next
; dev
; dev
= dev
->next
) {
9250 dev_path
= devt_to_devpath(makedev(dev
->disk
.major
, dev
->disk
.minor
));
9252 struct sys_dev
*hba2
= NULL
;
9253 for (idev
= intel_devices
; idev
; idev
= idev
->next
) {
9254 if (dev_path
&& strstr(dev_path
, idev
->path
)) {
9262 const struct imsm_orom
*orom2
= hba2
== NULL
? NULL
:
9263 get_orom_by_device_id(hba2
->dev_id
);
9265 if (hba2
&& hba
->type
!= hba2
->type
) {
9266 pr_err("WARNING - HBAs of devices do not match %s != %s\n",
9267 get_sys_dev_type(hba
->type
), get_sys_dev_type(hba2
->type
));
9271 if (orom
!= orom2
||
9272 (hba
->type
== SYS_DEV_VMD
&& hba
!= hba2
)) {
9273 pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n"
9274 " This operation is not supported and can lead to data loss.\n");
9279 pr_err("WARNING - IMSM container assembled with disks under HBAs without IMSM platform support!\n"
9280 " This operation is not supported and can lead to data loss.\n");
9288 /*******************************************************************************
9289 * Function: init_migr_record_imsm
9290 * Description: Function inits imsm migration record
9292 * super : imsm internal array info
9293 * dev : device under migration
9294 * info : general array info to find the smallest device
9297 ******************************************************************************/
9298 void init_migr_record_imsm(struct supertype
*st
, struct imsm_dev
*dev
,
9299 struct mdinfo
*info
)
9301 struct intel_super
*super
= st
->sb
;
9302 struct migr_record
*migr_rec
= super
->migr_rec
;
9304 unsigned long long dsize
, dev_sectors
;
9305 long long unsigned min_dev_sectors
= -1LLU;
9309 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
9310 struct imsm_map
*map_src
= get_imsm_map(dev
, MAP_1
);
9311 unsigned long long num_migr_units
;
9312 unsigned long long array_blocks
;
9314 memset(migr_rec
, 0, sizeof(struct migr_record
));
9315 migr_rec
->family_num
= __cpu_to_le32(super
->anchor
->family_num
);
9317 /* only ascending reshape supported now */
9318 migr_rec
->ascending_migr
= __cpu_to_le32(1);
9320 migr_rec
->dest_depth_per_unit
= GEN_MIGR_AREA_SIZE
/
9321 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
9322 migr_rec
->dest_depth_per_unit
*=
9323 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
9324 new_data_disks
= imsm_num_data_members(dev
, MAP_0
);
9325 migr_rec
->blocks_per_unit
=
9326 __cpu_to_le32(migr_rec
->dest_depth_per_unit
* new_data_disks
);
9327 migr_rec
->dest_depth_per_unit
=
9328 __cpu_to_le32(migr_rec
->dest_depth_per_unit
);
9329 array_blocks
= info
->component_size
* new_data_disks
;
9331 array_blocks
/ __le32_to_cpu(migr_rec
->blocks_per_unit
);
9333 if (array_blocks
% __le32_to_cpu(migr_rec
->blocks_per_unit
))
9335 migr_rec
->num_migr_units
= __cpu_to_le32(num_migr_units
);
9337 migr_rec
->post_migr_vol_cap
= dev
->size_low
;
9338 migr_rec
->post_migr_vol_cap_hi
= dev
->size_high
;
9340 /* Find the smallest dev */
9341 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
9342 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
9343 fd
= dev_open(nm
, O_RDONLY
);
9346 get_dev_size(fd
, NULL
, &dsize
);
9347 dev_sectors
= dsize
/ 512;
9348 if (dev_sectors
< min_dev_sectors
)
9349 min_dev_sectors
= dev_sectors
;
9352 migr_rec
->ckpt_area_pba
= __cpu_to_le32(min_dev_sectors
-
9353 RAID_DISK_RESERVED_BLOCKS_IMSM_HI
);
9355 write_imsm_migr_rec(st
);
9360 /*******************************************************************************
9361 * Function: save_backup_imsm
9362 * Description: Function saves critical data stripes to Migration Copy Area
9363 * and updates the current migration unit status.
9364 * Use restore_stripes() to form a destination stripe,
9365 * and to write it to the Copy Area.
9367 * st : supertype information
9368 * dev : imsm device that backup is saved for
9369 * info : general array info
9370 * buf : input buffer
9371 * length : length of data to backup (blocks_per_unit)
9375 ******************************************************************************/
9376 int save_backup_imsm(struct supertype
*st
,
9377 struct imsm_dev
*dev
,
9378 struct mdinfo
*info
,
9383 struct intel_super
*super
= st
->sb
;
9384 unsigned long long *target_offsets
;
9387 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
9388 int new_disks
= map_dest
->num_members
;
9389 int dest_layout
= 0;
9391 unsigned long long start
;
9392 int data_disks
= imsm_num_data_members(dev
, MAP_0
);
9394 targets
= xmalloc(new_disks
* sizeof(int));
9396 for (i
= 0; i
< new_disks
; i
++)
9399 target_offsets
= xcalloc(new_disks
, sizeof(unsigned long long));
9401 start
= info
->reshape_progress
* 512;
9402 for (i
= 0; i
< new_disks
; i
++) {
9403 target_offsets
[i
] = (unsigned long long)
9404 __le32_to_cpu(super
->migr_rec
->ckpt_area_pba
) * 512;
9405 /* move back copy area adderss, it will be moved forward
9406 * in restore_stripes() using start input variable
9408 target_offsets
[i
] -= start
/data_disks
;
9411 if (open_backup_targets(info
, new_disks
, targets
,
9415 dest_layout
= imsm_level_to_layout(map_dest
->raid_level
);
9416 dest_chunk
= __le16_to_cpu(map_dest
->blocks_per_strip
) * 512;
9418 if (restore_stripes(targets
, /* list of dest devices */
9419 target_offsets
, /* migration record offsets */
9422 map_dest
->raid_level
,
9424 -1, /* source backup file descriptor */
9425 0, /* input buf offset
9426 * always 0 buf is already offseted */
9430 pr_err("Error restoring stripes\n");
9438 close_targets(targets
, new_disks
);
9441 free(target_offsets
);
9446 /*******************************************************************************
9447 * Function: save_checkpoint_imsm
9448 * Description: Function called for current unit status update
9449 * in the migration record. It writes it to disk.
9451 * super : imsm internal array info
9452 * info : general array info
9456 * 2: failure, means no valid migration record
9457 * / no general migration in progress /
9458 ******************************************************************************/
9459 int save_checkpoint_imsm(struct supertype
*st
, struct mdinfo
*info
, int state
)
9461 struct intel_super
*super
= st
->sb
;
9462 unsigned long long blocks_per_unit
;
9463 unsigned long long curr_migr_unit
;
9465 if (load_imsm_migr_rec(super
, info
) != 0) {
9466 dprintf("imsm: ERROR: Cannot read migration record for checkpoint save.\n");
9470 blocks_per_unit
= __le32_to_cpu(super
->migr_rec
->blocks_per_unit
);
9471 if (blocks_per_unit
== 0) {
9472 dprintf("imsm: no migration in progress.\n");
9475 curr_migr_unit
= info
->reshape_progress
/ blocks_per_unit
;
9476 /* check if array is alligned to copy area
9477 * if it is not alligned, add one to current migration unit value
9478 * this can happend on array reshape finish only
9480 if (info
->reshape_progress
% blocks_per_unit
)
9483 super
->migr_rec
->curr_migr_unit
=
9484 __cpu_to_le32(curr_migr_unit
);
9485 super
->migr_rec
->rec_status
= __cpu_to_le32(state
);
9486 super
->migr_rec
->dest_1st_member_lba
=
9487 __cpu_to_le32(curr_migr_unit
*
9488 __le32_to_cpu(super
->migr_rec
->dest_depth_per_unit
));
9489 if (write_imsm_migr_rec(st
) < 0) {
9490 dprintf("imsm: Cannot write migration record outside backup area\n");
9497 /*******************************************************************************
9498 * Function: recover_backup_imsm
9499 * Description: Function recovers critical data from the Migration Copy Area
9500 * while assembling an array.
9502 * super : imsm internal array info
9503 * info : general array info
9505 * 0 : success (or there is no data to recover)
9507 ******************************************************************************/
9508 int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
)
9510 struct intel_super
*super
= st
->sb
;
9511 struct migr_record
*migr_rec
= super
->migr_rec
;
9512 struct imsm_map
*map_dest
;
9513 struct intel_dev
*id
= NULL
;
9514 unsigned long long read_offset
;
9515 unsigned long long write_offset
;
9517 int *targets
= NULL
;
9518 int new_disks
, i
, err
;
9521 unsigned long curr_migr_unit
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
9522 unsigned long num_migr_units
= __le32_to_cpu(migr_rec
->num_migr_units
);
9524 int skipped_disks
= 0;
9526 err
= sysfs_get_str(info
, NULL
, "array_state", (char *)buffer
, 20);
9530 /* recover data only during assemblation */
9531 if (strncmp(buffer
, "inactive", 8) != 0)
9533 /* no data to recover */
9534 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
9536 if (curr_migr_unit
>= num_migr_units
)
9539 /* find device during reshape */
9540 for (id
= super
->devlist
; id
; id
= id
->next
)
9541 if (is_gen_migration(id
->dev
))
9546 map_dest
= get_imsm_map(id
->dev
, MAP_0
);
9547 new_disks
= map_dest
->num_members
;
9549 read_offset
= (unsigned long long)
9550 __le32_to_cpu(migr_rec
->ckpt_area_pba
) * 512;
9552 write_offset
= ((unsigned long long)
9553 __le32_to_cpu(migr_rec
->dest_1st_member_lba
) +
9554 pba_of_lba0(map_dest
)) * 512;
9556 unit_len
= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
9557 if (posix_memalign((void **)&buf
, 512, unit_len
) != 0)
9559 targets
= xcalloc(new_disks
, sizeof(int));
9561 if (open_backup_targets(info
, new_disks
, targets
, super
, id
->dev
)) {
9562 pr_err("Cannot open some devices belonging to array.\n");
9566 for (i
= 0; i
< new_disks
; i
++) {
9567 if (targets
[i
] < 0) {
9571 if (lseek64(targets
[i
], read_offset
, SEEK_SET
) < 0) {
9572 pr_err("Cannot seek to block: %s\n",
9577 if ((unsigned)read(targets
[i
], buf
, unit_len
) != unit_len
) {
9578 pr_err("Cannot read copy area block: %s\n",
9583 if (lseek64(targets
[i
], write_offset
, SEEK_SET
) < 0) {
9584 pr_err("Cannot seek to block: %s\n",
9589 if ((unsigned)write(targets
[i
], buf
, unit_len
) != unit_len
) {
9590 pr_err("Cannot restore block: %s\n",
9597 if (skipped_disks
> imsm_get_allowed_degradation(info
->new_level
,
9601 pr_err("Cannot restore data from backup. Too many failed disks\n");
9605 if (save_checkpoint_imsm(st
, info
, UNIT_SRC_NORMAL
)) {
9606 /* ignore error == 2, this can mean end of reshape here
9608 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL) during restart\n");
9614 for (i
= 0; i
< new_disks
; i
++)
9623 static char disk_by_path
[] = "/dev/disk/by-path/";
9625 static const char *imsm_get_disk_controller_domain(const char *path
)
9627 char disk_path
[PATH_MAX
];
9631 strcpy(disk_path
, disk_by_path
);
9632 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
9633 if (stat(disk_path
, &st
) == 0) {
9634 struct sys_dev
* hba
;
9637 path
= devt_to_devpath(st
.st_rdev
);
9640 hba
= find_disk_attached_hba(-1, path
);
9641 if (hba
&& hba
->type
== SYS_DEV_SAS
)
9643 else if (hba
&& hba
->type
== SYS_DEV_SATA
)
9647 dprintf("path: %s hba: %s attached: %s\n",
9648 path
, (hba
) ? hba
->path
: "NULL", drv
);
9654 static char *imsm_find_array_devnm_by_subdev(int subdev
, char *container
)
9656 static char devnm
[32];
9657 char subdev_name
[20];
9658 struct mdstat_ent
*mdstat
;
9660 sprintf(subdev_name
, "%d", subdev
);
9661 mdstat
= mdstat_by_subdev(subdev_name
, container
);
9665 strcpy(devnm
, mdstat
->devnm
);
9666 free_mdstat(mdstat
);
9670 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
9671 struct geo_params
*geo
,
9672 int *old_raid_disks
,
9675 /* currently we only support increasing the number of devices
9676 * for a container. This increases the number of device for each
9677 * member array. They must all be RAID0 or RAID5.
9680 struct mdinfo
*info
, *member
;
9681 int devices_that_can_grow
= 0;
9683 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): st->devnm = (%s)\n", st
->devnm
);
9685 if (geo
->size
> 0 ||
9686 geo
->level
!= UnSet
||
9687 geo
->layout
!= UnSet
||
9688 geo
->chunksize
!= 0 ||
9689 geo
->raid_disks
== UnSet
) {
9690 dprintf("imsm: Container operation is allowed for raid disks number change only.\n");
9694 if (direction
== ROLLBACK_METADATA_CHANGES
) {
9695 dprintf("imsm: Metadata changes rollback is not supported for container operation.\n");
9699 info
= container_content_imsm(st
, NULL
);
9700 for (member
= info
; member
; member
= member
->next
) {
9703 dprintf("imsm: checking device_num: %i\n",
9704 member
->container_member
);
9706 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
9707 /* we work on container for Online Capacity Expansion
9708 * only so raid_disks has to grow
9710 dprintf("imsm: for container operation raid disks increase is required\n");
9714 if (info
->array
.level
!= 0 && info
->array
.level
!= 5) {
9715 /* we cannot use this container with other raid level
9717 dprintf("imsm: for container operation wrong raid level (%i) detected\n",
9721 /* check for platform support
9722 * for this raid level configuration
9724 struct intel_super
*super
= st
->sb
;
9725 if (!is_raid_level_supported(super
->orom
,
9726 member
->array
.level
,
9728 dprintf("platform does not support raid%d with %d disk%s\n",
9731 geo
->raid_disks
> 1 ? "s" : "");
9734 /* check if component size is aligned to chunk size
9736 if (info
->component_size
%
9737 (info
->array
.chunk_size
/512)) {
9738 dprintf("Component size is not aligned to chunk size\n");
9743 if (*old_raid_disks
&&
9744 info
->array
.raid_disks
!= *old_raid_disks
)
9746 *old_raid_disks
= info
->array
.raid_disks
;
9748 /* All raid5 and raid0 volumes in container
9749 * have to be ready for Online Capacity Expansion
9750 * so they need to be assembled. We have already
9751 * checked that no recovery etc is happening.
9753 result
= imsm_find_array_devnm_by_subdev(member
->container_member
,
9754 st
->container_devnm
);
9755 if (result
== NULL
) {
9756 dprintf("imsm: cannot find array\n");
9759 devices_that_can_grow
++;
9762 if (!member
&& devices_that_can_grow
)
9766 dprintf("Container operation allowed\n");
9768 dprintf("Error: %i\n", ret_val
);
9773 /* Function: get_spares_for_grow
9774 * Description: Allocates memory and creates list of spare devices
9775 * avaliable in container. Checks if spare drive size is acceptable.
9776 * Parameters: Pointer to the supertype structure
9777 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
9780 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
9782 unsigned long long min_size
= min_acceptable_spare_size_imsm(st
);
9783 return container_choose_spares(st
, min_size
, NULL
, NULL
, NULL
, 0);
9786 /******************************************************************************
9787 * function: imsm_create_metadata_update_for_reshape
9788 * Function creates update for whole IMSM container.
9790 ******************************************************************************/
9791 static int imsm_create_metadata_update_for_reshape(
9792 struct supertype
*st
,
9793 struct geo_params
*geo
,
9795 struct imsm_update_reshape
**updatep
)
9797 struct intel_super
*super
= st
->sb
;
9798 struct imsm_super
*mpb
= super
->anchor
;
9799 int update_memory_size
;
9800 struct imsm_update_reshape
*u
;
9801 struct mdinfo
*spares
;
9806 dprintf("(enter) raid_disks = %i\n", geo
->raid_disks
);
9808 delta_disks
= geo
->raid_disks
- old_raid_disks
;
9810 /* size of all update data without anchor */
9811 update_memory_size
= sizeof(struct imsm_update_reshape
);
9813 /* now add space for spare disks that we need to add. */
9814 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
9816 u
= xcalloc(1, update_memory_size
);
9817 u
->type
= update_reshape_container_disks
;
9818 u
->old_raid_disks
= old_raid_disks
;
9819 u
->new_raid_disks
= geo
->raid_disks
;
9821 /* now get spare disks list
9823 spares
= get_spares_for_grow(st
);
9826 || delta_disks
> spares
->array
.spare_disks
) {
9827 pr_err("imsm: ERROR: Cannot get spare devices for %s.\n", geo
->dev_name
);
9832 /* we have got spares
9833 * update disk list in imsm_disk list table in anchor
9835 dprintf("imsm: %i spares are available.\n\n",
9836 spares
->array
.spare_disks
);
9839 for (i
= 0; i
< delta_disks
; i
++) {
9844 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
9846 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
9847 dl
->index
= mpb
->num_disks
;
9857 dprintf("imsm: reshape update preparation :");
9858 if (i
== delta_disks
) {
9859 dprintf_cont(" OK\n");
9861 return update_memory_size
;
9864 dprintf_cont(" Error\n");
9869 /******************************************************************************
9870 * function: imsm_create_metadata_update_for_size_change()
9871 * Creates update for IMSM array for array size change.
9873 ******************************************************************************/
9874 static int imsm_create_metadata_update_for_size_change(
9875 struct supertype
*st
,
9876 struct geo_params
*geo
,
9877 struct imsm_update_size_change
**updatep
)
9879 struct intel_super
*super
= st
->sb
;
9880 int update_memory_size
;
9881 struct imsm_update_size_change
*u
;
9883 dprintf("(enter) New size = %llu\n", geo
->size
);
9885 /* size of all update data without anchor */
9886 update_memory_size
= sizeof(struct imsm_update_size_change
);
9888 u
= xcalloc(1, update_memory_size
);
9889 u
->type
= update_size_change
;
9890 u
->subdev
= super
->current_vol
;
9891 u
->new_size
= geo
->size
;
9893 dprintf("imsm: reshape update preparation : OK\n");
9896 return update_memory_size
;
9899 /******************************************************************************
9900 * function: imsm_create_metadata_update_for_migration()
9901 * Creates update for IMSM array.
9903 ******************************************************************************/
9904 static int imsm_create_metadata_update_for_migration(
9905 struct supertype
*st
,
9906 struct geo_params
*geo
,
9907 struct imsm_update_reshape_migration
**updatep
)
9909 struct intel_super
*super
= st
->sb
;
9910 int update_memory_size
;
9911 struct imsm_update_reshape_migration
*u
;
9912 struct imsm_dev
*dev
;
9913 int previous_level
= -1;
9915 dprintf("(enter) New Level = %i\n", geo
->level
);
9917 /* size of all update data without anchor */
9918 update_memory_size
= sizeof(struct imsm_update_reshape_migration
);
9920 u
= xcalloc(1, update_memory_size
);
9921 u
->type
= update_reshape_migration
;
9922 u
->subdev
= super
->current_vol
;
9923 u
->new_level
= geo
->level
;
9924 u
->new_layout
= geo
->layout
;
9925 u
->new_raid_disks
= u
->old_raid_disks
= geo
->raid_disks
;
9926 u
->new_disks
[0] = -1;
9927 u
->new_chunksize
= -1;
9929 dev
= get_imsm_dev(super
, u
->subdev
);
9931 struct imsm_map
*map
;
9933 map
= get_imsm_map(dev
, MAP_0
);
9935 int current_chunk_size
=
9936 __le16_to_cpu(map
->blocks_per_strip
) / 2;
9938 if (geo
->chunksize
!= current_chunk_size
) {
9939 u
->new_chunksize
= geo
->chunksize
/ 1024;
9940 dprintf("imsm: chunk size change from %i to %i\n",
9941 current_chunk_size
, u
->new_chunksize
);
9943 previous_level
= map
->raid_level
;
9946 if (geo
->level
== 5 && previous_level
== 0) {
9947 struct mdinfo
*spares
= NULL
;
9949 u
->new_raid_disks
++;
9950 spares
= get_spares_for_grow(st
);
9951 if (spares
== NULL
|| spares
->array
.spare_disks
< 1) {
9954 update_memory_size
= 0;
9955 dprintf("error: cannot get spare device for requested migration");
9960 dprintf("imsm: reshape update preparation : OK\n");
9963 return update_memory_size
;
9966 static void imsm_update_metadata_locally(struct supertype
*st
,
9969 struct metadata_update mu
;
9974 mu
.space_list
= NULL
;
9976 if (imsm_prepare_update(st
, &mu
))
9977 imsm_process_update(st
, &mu
);
9979 while (mu
.space_list
) {
9980 void **space
= mu
.space_list
;
9981 mu
.space_list
= *space
;
9986 /***************************************************************************
9987 * Function: imsm_analyze_change
9988 * Description: Function analyze change for single volume
9989 * and validate if transition is supported
9990 * Parameters: Geometry parameters, supertype structure,
9991 * metadata change direction (apply/rollback)
9992 * Returns: Operation type code on success, -1 if fail
9993 ****************************************************************************/
9994 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
9995 struct geo_params
*geo
,
10000 int check_devs
= 0;
10002 /* number of added/removed disks in operation result */
10003 int devNumChange
= 0;
10004 /* imsm compatible layout value for array geometry verification */
10005 int imsm_layout
= -1;
10007 struct imsm_dev
*dev
;
10008 struct intel_super
*super
;
10009 unsigned long long current_size
;
10010 unsigned long long free_size
;
10011 unsigned long long max_size
;
10014 getinfo_super_imsm_volume(st
, &info
, NULL
);
10015 if (geo
->level
!= info
.array
.level
&& geo
->level
>= 0 &&
10016 geo
->level
!= UnSet
) {
10017 switch (info
.array
.level
) {
10019 if (geo
->level
== 5) {
10020 change
= CH_MIGRATION
;
10021 if (geo
->layout
!= ALGORITHM_LEFT_ASYMMETRIC
) {
10022 pr_err("Error. Requested Layout not supported (left-asymmetric layout is supported only)!\n");
10024 goto analyse_change_exit
;
10026 imsm_layout
= geo
->layout
;
10028 devNumChange
= 1; /* parity disk added */
10029 } else if (geo
->level
== 10) {
10030 change
= CH_TAKEOVER
;
10032 devNumChange
= 2; /* two mirrors added */
10033 imsm_layout
= 0x102; /* imsm supported layout */
10038 if (geo
->level
== 0) {
10039 change
= CH_TAKEOVER
;
10041 devNumChange
= -(geo
->raid_disks
/2);
10042 imsm_layout
= 0; /* imsm raid0 layout */
10046 if (change
== -1) {
10047 pr_err("Error. Level Migration from %d to %d not supported!\n",
10048 info
.array
.level
, geo
->level
);
10049 goto analyse_change_exit
;
10052 geo
->level
= info
.array
.level
;
10054 if (geo
->layout
!= info
.array
.layout
&&
10055 (geo
->layout
!= UnSet
&& geo
->layout
!= -1)) {
10056 change
= CH_MIGRATION
;
10057 if (info
.array
.layout
== 0 && info
.array
.level
== 5 &&
10058 geo
->layout
== 5) {
10059 /* reshape 5 -> 4 */
10060 } else if (info
.array
.layout
== 5 && info
.array
.level
== 5 &&
10061 geo
->layout
== 0) {
10062 /* reshape 4 -> 5 */
10066 pr_err("Error. Layout Migration from %d to %d not supported!\n",
10067 info
.array
.layout
, geo
->layout
);
10069 goto analyse_change_exit
;
10072 geo
->layout
= info
.array
.layout
;
10073 if (imsm_layout
== -1)
10074 imsm_layout
= info
.array
.layout
;
10077 if (geo
->chunksize
> 0 && geo
->chunksize
!= UnSet
&&
10078 geo
->chunksize
!= info
.array
.chunk_size
) {
10079 if (info
.array
.level
== 10) {
10080 pr_err("Error. Chunk size change for RAID 10 is not supported.\n");
10082 goto analyse_change_exit
;
10084 change
= CH_MIGRATION
;
10086 geo
->chunksize
= info
.array
.chunk_size
;
10089 chunk
= geo
->chunksize
/ 1024;
10092 dev
= get_imsm_dev(super
, super
->current_vol
);
10093 data_disks
= imsm_num_data_members(dev
, MAP_0
);
10094 /* compute current size per disk member
10096 current_size
= info
.custom_array_size
/ data_disks
;
10098 if (geo
->size
> 0 && geo
->size
!= MAX_SIZE
) {
10099 /* align component size
10101 geo
->size
= imsm_component_size_aligment_check(
10102 get_imsm_raid_level(dev
->vol
.map
),
10105 if (geo
->size
== 0) {
10106 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is 0).\n",
10108 goto analyse_change_exit
;
10112 if (current_size
!= geo
->size
&& geo
->size
> 0) {
10113 if (change
!= -1) {
10114 pr_err("Error. Size change should be the only one at a time.\n");
10116 goto analyse_change_exit
;
10118 if ((super
->current_vol
+ 1) != super
->anchor
->num_raid_devs
) {
10119 pr_err("Error. The last volume in container can be expanded only (%i/%s).\n",
10120 super
->current_vol
, st
->devnm
);
10121 goto analyse_change_exit
;
10123 /* check the maximum available size
10125 rv
= imsm_get_free_size(st
, dev
->vol
.map
->num_members
,
10126 0, chunk
, &free_size
);
10128 /* Cannot find maximum available space
10132 max_size
= free_size
+ current_size
;
10133 /* align component size
10135 max_size
= imsm_component_size_aligment_check(
10136 get_imsm_raid_level(dev
->vol
.map
),
10140 if (geo
->size
== MAX_SIZE
) {
10141 /* requested size change to the maximum available size
10143 if (max_size
== 0) {
10144 pr_err("Error. Cannot find maximum available space.\n");
10146 goto analyse_change_exit
;
10148 geo
->size
= max_size
;
10151 if (direction
== ROLLBACK_METADATA_CHANGES
) {
10152 /* accept size for rollback only
10155 /* round size due to metadata compatibility
10157 geo
->size
= (geo
->size
>> SECT_PER_MB_SHIFT
)
10158 << SECT_PER_MB_SHIFT
;
10159 dprintf("Prepare update for size change to %llu\n",
10161 if (current_size
>= geo
->size
) {
10162 pr_err("Error. Size expansion is supported only (current size is %llu, requested size /rounded/ is %llu).\n",
10163 current_size
, geo
->size
);
10164 goto analyse_change_exit
;
10166 if (max_size
&& geo
->size
> max_size
) {
10167 pr_err("Error. Requested size is larger than maximum available size (maximum available size is %llu, requested size /rounded/ is %llu).\n",
10168 max_size
, geo
->size
);
10169 goto analyse_change_exit
;
10172 geo
->size
*= data_disks
;
10173 geo
->raid_disks
= dev
->vol
.map
->num_members
;
10174 change
= CH_ARRAY_SIZE
;
10176 if (!validate_geometry_imsm(st
,
10179 geo
->raid_disks
+ devNumChange
,
10181 geo
->size
, INVALID_SECTORS
,
10186 struct intel_super
*super
= st
->sb
;
10187 struct imsm_super
*mpb
= super
->anchor
;
10189 if (mpb
->num_raid_devs
> 1) {
10190 pr_err("Error. Cannot perform operation on %s- for this operation it MUST be single array in container\n",
10196 analyse_change_exit
:
10197 if (direction
== ROLLBACK_METADATA_CHANGES
&&
10198 (change
== CH_MIGRATION
|| change
== CH_TAKEOVER
)) {
10199 dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n");
10205 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
10207 struct intel_super
*super
= st
->sb
;
10208 struct imsm_update_takeover
*u
;
10210 u
= xmalloc(sizeof(struct imsm_update_takeover
));
10212 u
->type
= update_takeover
;
10213 u
->subarray
= super
->current_vol
;
10215 /* 10->0 transition */
10216 if (geo
->level
== 0)
10217 u
->direction
= R10_TO_R0
;
10219 /* 0->10 transition */
10220 if (geo
->level
== 10)
10221 u
->direction
= R0_TO_R10
;
10223 /* update metadata locally */
10224 imsm_update_metadata_locally(st
, u
,
10225 sizeof(struct imsm_update_takeover
));
10226 /* and possibly remotely */
10227 if (st
->update_tail
)
10228 append_metadata_update(st
, u
,
10229 sizeof(struct imsm_update_takeover
));
10236 static int imsm_reshape_super(struct supertype
*st
, unsigned long long size
,
10238 int layout
, int chunksize
, int raid_disks
,
10239 int delta_disks
, char *backup
, char *dev
,
10240 int direction
, int verbose
)
10243 struct geo_params geo
;
10245 dprintf("(enter)\n");
10247 memset(&geo
, 0, sizeof(struct geo_params
));
10249 geo
.dev_name
= dev
;
10250 strcpy(geo
.devnm
, st
->devnm
);
10253 geo
.layout
= layout
;
10254 geo
.chunksize
= chunksize
;
10255 geo
.raid_disks
= raid_disks
;
10256 if (delta_disks
!= UnSet
)
10257 geo
.raid_disks
+= delta_disks
;
10259 dprintf("for level : %i\n", geo
.level
);
10260 dprintf("for raid_disks : %i\n", geo
.raid_disks
);
10262 if (experimental() == 0)
10265 if (strcmp(st
->container_devnm
, st
->devnm
) == 0) {
10266 /* On container level we can only increase number of devices. */
10267 dprintf("imsm: info: Container operation\n");
10268 int old_raid_disks
= 0;
10270 if (imsm_reshape_is_allowed_on_container(
10271 st
, &geo
, &old_raid_disks
, direction
)) {
10272 struct imsm_update_reshape
*u
= NULL
;
10275 len
= imsm_create_metadata_update_for_reshape(
10276 st
, &geo
, old_raid_disks
, &u
);
10279 dprintf("imsm: Cannot prepare update\n");
10280 goto exit_imsm_reshape_super
;
10284 /* update metadata locally */
10285 imsm_update_metadata_locally(st
, u
, len
);
10286 /* and possibly remotely */
10287 if (st
->update_tail
)
10288 append_metadata_update(st
, u
, len
);
10293 pr_err("(imsm) Operation is not allowed on this container\n");
10296 /* On volume level we support following operations
10297 * - takeover: raid10 -> raid0; raid0 -> raid10
10298 * - chunk size migration
10299 * - migration: raid5 -> raid0; raid0 -> raid5
10301 struct intel_super
*super
= st
->sb
;
10302 struct intel_dev
*dev
= super
->devlist
;
10304 dprintf("imsm: info: Volume operation\n");
10305 /* find requested device */
10308 imsm_find_array_devnm_by_subdev(
10309 dev
->index
, st
->container_devnm
);
10310 if (devnm
&& strcmp(devnm
, geo
.devnm
) == 0)
10315 pr_err("Cannot find %s (%s) subarray\n",
10316 geo
.dev_name
, geo
.devnm
);
10317 goto exit_imsm_reshape_super
;
10319 super
->current_vol
= dev
->index
;
10320 change
= imsm_analyze_change(st
, &geo
, direction
);
10323 ret_val
= imsm_takeover(st
, &geo
);
10325 case CH_MIGRATION
: {
10326 struct imsm_update_reshape_migration
*u
= NULL
;
10328 imsm_create_metadata_update_for_migration(
10331 dprintf("imsm: Cannot prepare update\n");
10335 /* update metadata locally */
10336 imsm_update_metadata_locally(st
, u
, len
);
10337 /* and possibly remotely */
10338 if (st
->update_tail
)
10339 append_metadata_update(st
, u
, len
);
10344 case CH_ARRAY_SIZE
: {
10345 struct imsm_update_size_change
*u
= NULL
;
10347 imsm_create_metadata_update_for_size_change(
10350 dprintf("imsm: Cannot prepare update\n");
10354 /* update metadata locally */
10355 imsm_update_metadata_locally(st
, u
, len
);
10356 /* and possibly remotely */
10357 if (st
->update_tail
)
10358 append_metadata_update(st
, u
, len
);
10368 exit_imsm_reshape_super
:
10369 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
10373 #define COMPLETED_OK 0
10374 #define COMPLETED_NONE 1
10375 #define COMPLETED_DELAYED 2
10377 static int read_completed(int fd
, unsigned long long *val
)
10382 ret
= sysfs_fd_get_str(fd
, buf
, 50);
10386 ret
= COMPLETED_OK
;
10387 if (strncmp(buf
, "none", 4) == 0) {
10388 ret
= COMPLETED_NONE
;
10389 } else if (strncmp(buf
, "delayed", 7) == 0) {
10390 ret
= COMPLETED_DELAYED
;
10393 *val
= strtoull(buf
, &ep
, 0);
10394 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))
10400 /*******************************************************************************
10401 * Function: wait_for_reshape_imsm
10402 * Description: Function writes new sync_max value and waits until
10403 * reshape process reach new position
10405 * sra : general array info
10406 * ndata : number of disks in new array's layout
10409 * 1 : there is no reshape in progress,
10411 ******************************************************************************/
10412 int wait_for_reshape_imsm(struct mdinfo
*sra
, int ndata
)
10414 int fd
= sysfs_get_fd(sra
, NULL
, "sync_completed");
10416 unsigned long long completed
;
10417 /* to_complete : new sync_max position */
10418 unsigned long long to_complete
= sra
->reshape_progress
;
10419 unsigned long long position_to_set
= to_complete
/ ndata
;
10422 dprintf("cannot open reshape_position\n");
10427 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
10429 dprintf("cannot read reshape_position (no reshape in progres)\n");
10438 if (completed
> position_to_set
) {
10439 dprintf("wrong next position to set %llu (%llu)\n",
10440 to_complete
, position_to_set
);
10444 dprintf("Position set: %llu\n", position_to_set
);
10445 if (sysfs_set_num(sra
, NULL
, "sync_max",
10446 position_to_set
) != 0) {
10447 dprintf("cannot set reshape position to %llu\n",
10456 int timeout
= 3000;
10458 sysfs_wait(fd
, &timeout
);
10459 if (sysfs_get_str(sra
, NULL
, "sync_action",
10461 strncmp(action
, "reshape", 7) != 0) {
10462 if (strncmp(action
, "idle", 4) == 0)
10468 rc
= read_completed(fd
, &completed
);
10470 dprintf("cannot read reshape_position (in loop)\n");
10473 } else if (rc
== COMPLETED_NONE
)
10475 } while (completed
< position_to_set
);
10481 /*******************************************************************************
10482 * Function: check_degradation_change
10483 * Description: Check that array hasn't become failed.
10485 * info : for sysfs access
10486 * sources : source disks descriptors
10487 * degraded: previous degradation level
10489 * degradation level
10490 ******************************************************************************/
10491 int check_degradation_change(struct mdinfo
*info
,
10495 unsigned long long new_degraded
;
10498 rv
= sysfs_get_ll(info
, NULL
, "degraded", &new_degraded
);
10499 if (rv
== -1 || (new_degraded
!= (unsigned long long)degraded
)) {
10500 /* check each device to ensure it is still working */
10503 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
10504 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
10506 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
10508 if (sysfs_get_str(info
,
10509 sd
, "state", sbuf
, 20) < 0 ||
10510 strstr(sbuf
, "faulty") ||
10511 strstr(sbuf
, "in_sync") == NULL
) {
10512 /* this device is dead */
10513 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
10514 if (sd
->disk
.raid_disk
>= 0 &&
10515 sources
[sd
->disk
.raid_disk
] >= 0) {
10517 sd
->disk
.raid_disk
]);
10518 sources
[sd
->disk
.raid_disk
] =
10527 return new_degraded
;
10530 /*******************************************************************************
10531 * Function: imsm_manage_reshape
10532 * Description: Function finds array under reshape and it manages reshape
10533 * process. It creates stripes backups (if required) and sets
10536 * afd : Backup handle (nattive) - not used
10537 * sra : general array info
10538 * reshape : reshape parameters - not used
10539 * st : supertype structure
10540 * blocks : size of critical section [blocks]
10541 * fds : table of source device descriptor
10542 * offsets : start of array (offest per devices)
10544 * destfd : table of destination device descriptor
10545 * destoffsets : table of destination offsets (per device)
10547 * 1 : success, reshape is done
10549 ******************************************************************************/
10550 static int imsm_manage_reshape(
10551 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
10552 struct supertype
*st
, unsigned long backup_blocks
,
10553 int *fds
, unsigned long long *offsets
,
10554 int dests
, int *destfd
, unsigned long long *destoffsets
)
10557 struct intel_super
*super
= st
->sb
;
10558 struct intel_dev
*dv
;
10559 struct imsm_dev
*dev
= NULL
;
10560 struct imsm_map
*map_src
;
10561 int migr_vol_qan
= 0;
10562 int ndata
, odata
; /* [bytes] */
10563 int chunk
; /* [bytes] */
10564 struct migr_record
*migr_rec
;
10566 unsigned int buf_size
; /* [bytes] */
10567 unsigned long long max_position
; /* array size [bytes] */
10568 unsigned long long next_step
; /* [blocks]/[bytes] */
10569 unsigned long long old_data_stripe_length
;
10570 unsigned long long start_src
; /* [bytes] */
10571 unsigned long long start
; /* [bytes] */
10572 unsigned long long start_buf_shift
; /* [bytes] */
10574 int source_layout
= 0;
10579 if (!fds
|| !offsets
)
10582 /* Find volume during the reshape */
10583 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
10584 if (dv
->dev
->vol
.migr_type
== MIGR_GEN_MIGR
10585 && dv
->dev
->vol
.migr_state
== 1) {
10590 /* Only one volume can migrate at the same time */
10591 if (migr_vol_qan
!= 1) {
10592 pr_err("%s", migr_vol_qan
?
10593 "Number of migrating volumes greater than 1\n" :
10594 "There is no volume during migrationg\n");
10598 map_src
= get_imsm_map(dev
, MAP_1
);
10599 if (map_src
== NULL
)
10602 ndata
= imsm_num_data_members(dev
, MAP_0
);
10603 odata
= imsm_num_data_members(dev
, MAP_1
);
10605 chunk
= __le16_to_cpu(map_src
->blocks_per_strip
) * 512;
10606 old_data_stripe_length
= odata
* chunk
;
10608 migr_rec
= super
->migr_rec
;
10610 /* initialize migration record for start condition */
10611 if (sra
->reshape_progress
== 0)
10612 init_migr_record_imsm(st
, dev
, sra
);
10614 if (__le32_to_cpu(migr_rec
->rec_status
) != UNIT_SRC_NORMAL
) {
10615 dprintf("imsm: cannot restart migration when data are present in copy area.\n");
10618 /* Save checkpoint to update migration record for current
10619 * reshape position (in md). It can be farther than current
10620 * reshape position in metadata.
10622 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
10623 /* ignore error == 2, this can mean end of reshape here
10625 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL, initial save)\n");
10630 /* size for data */
10631 buf_size
= __le32_to_cpu(migr_rec
->blocks_per_unit
) * 512;
10632 /* extend buffer size for parity disk */
10633 buf_size
+= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
10634 /* add space for stripe aligment */
10635 buf_size
+= old_data_stripe_length
;
10636 if (posix_memalign((void **)&buf
, 4096, buf_size
)) {
10637 dprintf("imsm: Cannot allocate checpoint buffer\n");
10641 max_position
= sra
->component_size
* ndata
;
10642 source_layout
= imsm_level_to_layout(map_src
->raid_level
);
10644 while (__le32_to_cpu(migr_rec
->curr_migr_unit
) <
10645 __le32_to_cpu(migr_rec
->num_migr_units
)) {
10646 /* current reshape position [blocks] */
10647 unsigned long long current_position
=
10648 __le32_to_cpu(migr_rec
->blocks_per_unit
)
10649 * __le32_to_cpu(migr_rec
->curr_migr_unit
);
10650 unsigned long long border
;
10652 /* Check that array hasn't become failed.
10654 degraded
= check_degradation_change(sra
, fds
, degraded
);
10655 if (degraded
> 1) {
10656 dprintf("imsm: Abort reshape due to degradation level (%i)\n", degraded
);
10660 next_step
= __le32_to_cpu(migr_rec
->blocks_per_unit
);
10662 if ((current_position
+ next_step
) > max_position
)
10663 next_step
= max_position
- current_position
;
10665 start
= current_position
* 512;
10667 /* align reading start to old geometry */
10668 start_buf_shift
= start
% old_data_stripe_length
;
10669 start_src
= start
- start_buf_shift
;
10671 border
= (start_src
/ odata
) - (start
/ ndata
);
10673 if (border
<= __le32_to_cpu(migr_rec
->dest_depth_per_unit
)) {
10674 /* save critical stripes to buf
10675 * start - start address of current unit
10676 * to backup [bytes]
10677 * start_src - start address of current unit
10678 * to backup alligned to source array
10681 unsigned long long next_step_filler
;
10682 unsigned long long copy_length
= next_step
* 512;
10684 /* allign copy area length to stripe in old geometry */
10685 next_step_filler
= ((copy_length
+ start_buf_shift
)
10686 % old_data_stripe_length
);
10687 if (next_step_filler
)
10688 next_step_filler
= (old_data_stripe_length
10689 - next_step_filler
);
10690 dprintf("save_stripes() parameters: start = %llu,\tstart_src = %llu,\tnext_step*512 = %llu,\tstart_in_buf_shift = %llu,\tnext_step_filler = %llu\n",
10691 start
, start_src
, copy_length
,
10692 start_buf_shift
, next_step_filler
);
10694 if (save_stripes(fds
, offsets
, map_src
->num_members
,
10695 chunk
, map_src
->raid_level
,
10696 source_layout
, 0, NULL
, start_src
,
10698 next_step_filler
+ start_buf_shift
,
10700 dprintf("imsm: Cannot save stripes to buffer\n");
10703 /* Convert data to destination format and store it
10704 * in backup general migration area
10706 if (save_backup_imsm(st
, dev
, sra
,
10707 buf
+ start_buf_shift
, copy_length
)) {
10708 dprintf("imsm: Cannot save stripes to target devices\n");
10711 if (save_checkpoint_imsm(st
, sra
,
10712 UNIT_SRC_IN_CP_AREA
)) {
10713 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_IN_CP_AREA)\n");
10717 /* set next step to use whole border area */
10718 border
/= next_step
;
10720 next_step
*= border
;
10722 /* When data backed up, checkpoint stored,
10723 * kick the kernel to reshape unit of data
10725 next_step
= next_step
+ sra
->reshape_progress
;
10726 /* limit next step to array max position */
10727 if (next_step
> max_position
)
10728 next_step
= max_position
;
10729 sysfs_set_num(sra
, NULL
, "suspend_lo", sra
->reshape_progress
);
10730 sysfs_set_num(sra
, NULL
, "suspend_hi", next_step
);
10731 sra
->reshape_progress
= next_step
;
10733 /* wait until reshape finish */
10734 if (wait_for_reshape_imsm(sra
, ndata
)) {
10735 dprintf("wait_for_reshape_imsm returned error!\n");
10741 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
10742 /* ignore error == 2, this can mean end of reshape here
10744 dprintf("imsm: Cannot write checkpoint to migration record (UNIT_SRC_NORMAL)\n");
10750 /* clear migr_rec on disks after successful migration */
10753 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SIZE
);
10754 for (d
= super
->disks
; d
; d
= d
->next
) {
10755 if (d
->index
< 0 || is_failed(&d
->disk
))
10757 unsigned long long dsize
;
10759 get_dev_size(d
->fd
, NULL
, &dsize
);
10760 if (lseek64(d
->fd
, dsize
- MIGR_REC_POSITION
,
10762 if (write(d
->fd
, super
->migr_rec_buf
,
10763 MIGR_REC_BUF_SIZE
) != MIGR_REC_BUF_SIZE
)
10764 perror("Write migr_rec failed");
10768 /* return '1' if done */
10772 /* See Grow.c: abort_reshape() for further explanation */
10773 sysfs_set_num(sra
, NULL
, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL
);
10774 sysfs_set_num(sra
, NULL
, "suspend_hi", 0);
10775 sysfs_set_num(sra
, NULL
, "suspend_lo", 0);
10780 #endif /* MDASSEMBLE */
10782 struct superswitch super_imsm
= {
10784 .examine_super
= examine_super_imsm
,
10785 .brief_examine_super
= brief_examine_super_imsm
,
10786 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
10787 .export_examine_super
= export_examine_super_imsm
,
10788 .detail_super
= detail_super_imsm
,
10789 .brief_detail_super
= brief_detail_super_imsm
,
10790 .write_init_super
= write_init_super_imsm
,
10791 .validate_geometry
= validate_geometry_imsm
,
10792 .add_to_super
= add_to_super_imsm
,
10793 .remove_from_super
= remove_from_super_imsm
,
10794 .detail_platform
= detail_platform_imsm
,
10795 .export_detail_platform
= export_detail_platform_imsm
,
10796 .kill_subarray
= kill_subarray_imsm
,
10797 .update_subarray
= update_subarray_imsm
,
10798 .load_container
= load_container_imsm
,
10799 .default_geometry
= default_geometry_imsm
,
10800 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
10801 .reshape_super
= imsm_reshape_super
,
10802 .manage_reshape
= imsm_manage_reshape
,
10803 .recover_backup
= recover_backup_imsm
,
10804 .copy_metadata
= copy_metadata_imsm
,
10806 .match_home
= match_home_imsm
,
10807 .uuid_from_super
= uuid_from_super_imsm
,
10808 .getinfo_super
= getinfo_super_imsm
,
10809 .getinfo_super_disks
= getinfo_super_disks_imsm
,
10810 .update_super
= update_super_imsm
,
10812 .avail_size
= avail_size_imsm
,
10813 .min_acceptable_spare_size
= min_acceptable_spare_size_imsm
,
10815 .compare_super
= compare_super_imsm
,
10817 .load_super
= load_super_imsm
,
10818 .init_super
= init_super_imsm
,
10819 .store_super
= store_super_imsm
,
10820 .free_super
= free_super_imsm
,
10821 .match_metadata_desc
= match_metadata_desc_imsm
,
10822 .container_content
= container_content_imsm
,
10823 .validate_container
= validate_container_imsm
,
10830 .open_new
= imsm_open_new
,
10831 .set_array_state
= imsm_set_array_state
,
10832 .set_disk
= imsm_set_disk
,
10833 .sync_metadata
= imsm_sync_metadata
,
10834 .activate_spare
= imsm_activate_spare
,
10835 .process_update
= imsm_process_update
,
10836 .prepare_update
= imsm_prepare_update
,
10837 #endif /* MDASSEMBLE */