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__
));
240 static char *map_state_str
[] = { "normal", "uninitialized", "degraded", "failed" };
243 #define RAID_DISK_RESERVED_BLOCKS_IMSM_HI 2209
245 #define GEN_MIGR_AREA_SIZE 2048 /* General Migration Copy Area size in blocks */
247 #define MIGR_REC_BUF_SIZE 512 /* size of migr_record i/o buffer */
248 #define MIGR_REC_POSITION 512 /* migr_record position offset on disk,
249 * MIGR_REC_BUF_SIZE <= MIGR_REC_POSITION
253 #define UNIT_SRC_NORMAL 0 /* Source data for curr_migr_unit must
254 * be recovered using srcMap */
255 #define UNIT_SRC_IN_CP_AREA 1 /* Source data for curr_migr_unit has
256 * already been migrated and must
257 * be recovered from checkpoint area */
259 __u32 rec_status
; /* Status used to determine how to restart
260 * migration in case it aborts
262 __u32 curr_migr_unit
; /* 0..numMigrUnits-1 */
263 __u32 family_num
; /* Family number of MPB
264 * containing the RaidDev
265 * that is migrating */
266 __u32 ascending_migr
; /* True if migrating in increasing
268 __u32 blocks_per_unit
; /* Num disk blocks per unit of operation */
269 __u32 dest_depth_per_unit
; /* Num member blocks each destMap
271 * advances per unit-of-operation */
272 __u32 ckpt_area_pba
; /* Pba of first block of ckpt copy area */
273 __u32 dest_1st_member_lba
; /* First member lba on first
274 * stripe of destination */
275 __u32 num_migr_units
; /* Total num migration units-of-op */
276 __u32 post_migr_vol_cap
; /* Size of volume after
277 * migration completes */
278 __u32 post_migr_vol_cap_hi
; /* Expansion space for LBA64 */
279 __u32 ckpt_read_disk_num
; /* Which member disk in destSubMap[0] the
280 * migration ckpt record was read from
281 * (for recovered migrations) */
282 } __attribute__ ((__packed__
));
287 * 2: metadata does not match
295 struct md_list
*next
;
298 #define pr_vrb(fmt, arg...) (void) (verbose && pr_err(fmt, ##arg))
300 static __u8
migr_type(struct imsm_dev
*dev
)
302 if (dev
->vol
.migr_type
== MIGR_VERIFY
&&
303 dev
->status
& DEV_VERIFY_AND_FIX
)
306 return dev
->vol
.migr_type
;
309 static void set_migr_type(struct imsm_dev
*dev
, __u8 migr_type
)
311 /* for compatibility with older oroms convert MIGR_REPAIR, into
312 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
314 if (migr_type
== MIGR_REPAIR
) {
315 dev
->vol
.migr_type
= MIGR_VERIFY
;
316 dev
->status
|= DEV_VERIFY_AND_FIX
;
318 dev
->vol
.migr_type
= migr_type
;
319 dev
->status
&= ~DEV_VERIFY_AND_FIX
;
323 static unsigned int sector_count(__u32 bytes
)
325 return ROUND_UP(bytes
, 512) / 512;
328 static unsigned int mpb_sectors(struct imsm_super
*mpb
)
330 return sector_count(__le32_to_cpu(mpb
->mpb_size
));
334 struct imsm_dev
*dev
;
335 struct intel_dev
*next
;
340 enum sys_dev_type type
;
343 struct intel_hba
*next
;
350 /* internal representation of IMSM metadata */
353 void *buf
; /* O_DIRECT buffer for reading/writing metadata */
354 struct imsm_super
*anchor
; /* immovable parameters */
357 void *migr_rec_buf
; /* buffer for I/O operations */
358 struct migr_record
*migr_rec
; /* migration record */
360 int clean_migration_record_by_mdmon
; /* when reshape is switched to next
361 array, it indicates that mdmon is allowed to clean migration
363 size_t len
; /* size of the 'buf' allocation */
364 void *next_buf
; /* for realloc'ing buf from the manager */
366 int updates_pending
; /* count of pending updates for mdmon */
367 int current_vol
; /* index of raid device undergoing creation */
368 unsigned long long create_offset
; /* common start for 'current_vol' */
369 __u32 random
; /* random data for seeding new family numbers */
370 struct intel_dev
*devlist
;
374 __u8 serial
[MAX_RAID_SERIAL_LEN
];
377 struct imsm_disk disk
;
380 struct extent
*e
; /* for determining freespace @ create */
381 int raiddisk
; /* slot to fill in autolayout */
383 } *disks
, *current_disk
;
384 struct dl
*disk_mgmt_list
; /* list of disks to add/remove while mdmon
386 struct dl
*missing
; /* disks removed while we weren't looking */
387 struct bbm_log
*bbm_log
;
388 struct intel_hba
*hba
; /* device path of the raid controller for this metadata */
389 const struct imsm_orom
*orom
; /* platform firmware support */
390 struct intel_super
*next
; /* (temp) list for disambiguating family_num */
394 struct imsm_disk disk
;
395 #define IMSM_UNKNOWN_OWNER (-1)
397 struct intel_disk
*next
;
401 unsigned long long start
, size
;
404 /* definitions of reshape process types */
405 enum imsm_reshape_type
{
411 /* definition of messages passed to imsm_process_update */
412 enum imsm_update_type
{
413 update_activate_spare
,
417 update_add_remove_disk
,
418 update_reshape_container_disks
,
419 update_reshape_migration
,
421 update_general_migration_checkpoint
,
425 struct imsm_update_activate_spare
{
426 enum imsm_update_type type
;
430 struct imsm_update_activate_spare
*next
;
436 unsigned long long size
;
443 enum takeover_direction
{
447 struct imsm_update_takeover
{
448 enum imsm_update_type type
;
450 enum takeover_direction direction
;
453 struct imsm_update_reshape
{
454 enum imsm_update_type type
;
458 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
461 struct imsm_update_reshape_migration
{
462 enum imsm_update_type type
;
465 /* fields for array migration changes
472 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
475 struct imsm_update_size_change
{
476 enum imsm_update_type type
;
481 struct imsm_update_general_migration_checkpoint
{
482 enum imsm_update_type type
;
483 __u32 curr_migr_unit
;
487 __u8 serial
[MAX_RAID_SERIAL_LEN
];
490 struct imsm_update_create_array
{
491 enum imsm_update_type type
;
496 struct imsm_update_kill_array
{
497 enum imsm_update_type type
;
501 struct imsm_update_rename_array
{
502 enum imsm_update_type type
;
503 __u8 name
[MAX_RAID_SERIAL_LEN
];
507 struct imsm_update_add_remove_disk
{
508 enum imsm_update_type type
;
512 static const char *_sys_dev_type
[] = {
513 [SYS_DEV_UNKNOWN
] = "Unknown",
514 [SYS_DEV_SAS
] = "SAS",
515 [SYS_DEV_SATA
] = "SATA"
518 const char *get_sys_dev_type(enum sys_dev_type type
)
520 if (type
>= SYS_DEV_MAX
)
521 type
= SYS_DEV_UNKNOWN
;
523 return _sys_dev_type
[type
];
526 static struct intel_hba
* alloc_intel_hba(struct sys_dev
*device
)
528 struct intel_hba
*result
= xmalloc(sizeof(*result
));
530 result
->type
= device
->type
;
531 result
->path
= xstrdup(device
->path
);
533 if (result
->path
&& (result
->pci_id
= strrchr(result
->path
, '/')) != NULL
)
539 static struct intel_hba
* find_intel_hba(struct intel_hba
*hba
, struct sys_dev
*device
)
541 struct intel_hba
*result
=NULL
;
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 sypport odf HBA types mixing
567 if (device
->type
!= hba
->type
)
573 hba
->next
= alloc_intel_hba(device
);
577 static struct sys_dev
* find_disk_attached_hba(int fd
, const char *devname
)
579 struct sys_dev
*list
, *elem
, *prev
;
582 if ((list
= find_intel_devices()) == NULL
)
586 disk_path
= (char *) devname
;
588 disk_path
= diskfd_to_devpath(fd
);
595 for (prev
= NULL
, elem
= list
; elem
; prev
= elem
, elem
= elem
->next
) {
596 if (path_attached_to_hba(disk_path
, elem
->path
)) {
600 prev
->next
= elem
->next
;
602 if (disk_path
!= devname
)
608 if (disk_path
!= devname
)
616 static int find_intel_hba_capability(int fd
, struct intel_super
*super
,
619 static struct supertype
*match_metadata_desc_imsm(char *arg
)
621 struct supertype
*st
;
623 if (strcmp(arg
, "imsm") != 0 &&
624 strcmp(arg
, "default") != 0
628 st
= xcalloc(1, sizeof(*st
));
629 st
->container_dev
= NoMdDev
;
630 st
->ss
= &super_imsm
;
631 st
->max_devs
= IMSM_MAX_DEVICES
;
632 st
->minor_version
= 0;
638 static __u8
*get_imsm_version(struct imsm_super
*mpb
)
640 return &mpb
->sig
[MPB_SIG_LEN
];
644 /* retrieve a disk directly from the anchor when the anchor is known to be
645 * up-to-date, currently only at load time
647 static struct imsm_disk
*__get_imsm_disk(struct imsm_super
*mpb
, __u8 index
)
649 if (index
>= mpb
->num_disks
)
651 return &mpb
->disk
[index
];
654 /* retrieve the disk description based on a index of the disk
657 static struct dl
*get_imsm_dl_disk(struct intel_super
*super
, __u8 index
)
661 for (d
= super
->disks
; d
; d
= d
->next
)
662 if (d
->index
== index
)
667 /* retrieve a disk from the parsed metadata */
668 static struct imsm_disk
*get_imsm_disk(struct intel_super
*super
, __u8 index
)
672 dl
= get_imsm_dl_disk(super
, index
);
679 /* generate a checksum directly from the anchor when the anchor is known to be
680 * up-to-date, currently only at load or write_super after coalescing
682 static __u32
__gen_imsm_checksum(struct imsm_super
*mpb
)
684 __u32 end
= mpb
->mpb_size
/ sizeof(end
);
685 __u32
*p
= (__u32
*) mpb
;
689 sum
+= __le32_to_cpu(*p
);
693 return sum
- __le32_to_cpu(mpb
->check_sum
);
696 static size_t sizeof_imsm_map(struct imsm_map
*map
)
698 return sizeof(struct imsm_map
) + sizeof(__u32
) * (map
->num_members
- 1);
701 struct imsm_map
*get_imsm_map(struct imsm_dev
*dev
, int second_map
)
703 /* A device can have 2 maps if it is in the middle of a migration.
705 * MAP_0 - we return the first map
706 * MAP_1 - we return the second map if it exists, else NULL
707 * MAP_X - we return the second map if it exists, else the first
709 struct imsm_map
*map
= &dev
->vol
.map
[0];
710 struct imsm_map
*map2
= NULL
;
712 if (dev
->vol
.migr_state
)
713 map2
= (void *)map
+ sizeof_imsm_map(map
);
715 switch (second_map
) {
732 /* return the size of the device.
733 * migr_state increases the returned size if map[0] were to be duplicated
735 static size_t sizeof_imsm_dev(struct imsm_dev
*dev
, int migr_state
)
737 size_t size
= sizeof(*dev
) - sizeof(struct imsm_map
) +
738 sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
740 /* migrating means an additional map */
741 if (dev
->vol
.migr_state
)
742 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_1
));
744 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
750 /* retrieve disk serial number list from a metadata update */
751 static struct disk_info
*get_disk_info(struct imsm_update_create_array
*update
)
754 struct disk_info
*inf
;
756 inf
= u
+ sizeof(*update
) - sizeof(struct imsm_dev
) +
757 sizeof_imsm_dev(&update
->dev
, 0);
763 static struct imsm_dev
*__get_imsm_dev(struct imsm_super
*mpb
, __u8 index
)
769 if (index
>= mpb
->num_raid_devs
)
772 /* devices start after all disks */
773 offset
= ((void *) &mpb
->disk
[mpb
->num_disks
]) - _mpb
;
775 for (i
= 0; i
<= index
; i
++)
777 return _mpb
+ offset
;
779 offset
+= sizeof_imsm_dev(_mpb
+ offset
, 0);
784 static struct imsm_dev
*get_imsm_dev(struct intel_super
*super
, __u8 index
)
786 struct intel_dev
*dv
;
788 if (index
>= super
->anchor
->num_raid_devs
)
790 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
791 if (dv
->index
== index
)
798 * == MAP_0 get first map
799 * == MAP_1 get second map
800 * == MAP_X than get map according to the current migr_state
802 static __u32
get_imsm_ord_tbl_ent(struct imsm_dev
*dev
,
806 struct imsm_map
*map
;
808 map
= get_imsm_map(dev
, second_map
);
810 /* top byte identifies disk under rebuild */
811 return __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
814 #define ord_to_idx(ord) (((ord) << 8) >> 8)
815 static __u32
get_imsm_disk_idx(struct imsm_dev
*dev
, int slot
, int second_map
)
817 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, second_map
);
819 return ord_to_idx(ord
);
822 static void set_imsm_ord_tbl_ent(struct imsm_map
*map
, int slot
, __u32 ord
)
824 map
->disk_ord_tbl
[slot
] = __cpu_to_le32(ord
);
827 static int get_imsm_disk_slot(struct imsm_map
*map
, unsigned idx
)
832 for (slot
= 0; slot
< map
->num_members
; slot
++) {
833 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
834 if (ord_to_idx(ord
) == idx
)
841 static int get_imsm_raid_level(struct imsm_map
*map
)
843 if (map
->raid_level
== 1) {
844 if (map
->num_members
== 2)
850 return map
->raid_level
;
853 static int cmp_extent(const void *av
, const void *bv
)
855 const struct extent
*a
= av
;
856 const struct extent
*b
= bv
;
857 if (a
->start
< b
->start
)
859 if (a
->start
> b
->start
)
864 static int count_memberships(struct dl
*dl
, struct intel_super
*super
)
869 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
870 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
871 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
873 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
880 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
);
882 static int split_ull(unsigned long long n
, __u32
*lo
, __u32
*hi
)
884 if (lo
== 0 || hi
== 0)
886 *lo
= __le32_to_cpu((unsigned)n
);
887 *hi
= __le32_to_cpu((unsigned)(n
>> 32));
891 static unsigned long long join_u32(__u32 lo
, __u32 hi
)
893 return (unsigned long long)__le32_to_cpu(lo
) |
894 (((unsigned long long)__le32_to_cpu(hi
)) << 32);
897 static unsigned long long total_blocks(struct imsm_disk
*disk
)
901 return join_u32(disk
->total_blocks_lo
, disk
->total_blocks_hi
);
904 static unsigned long long pba_of_lba0(struct imsm_map
*map
)
908 return join_u32(map
->pba_of_lba0_lo
, map
->pba_of_lba0_hi
);
911 static unsigned long long blocks_per_member(struct imsm_map
*map
)
915 return join_u32(map
->blocks_per_member_lo
, map
->blocks_per_member_hi
);
919 static unsigned long long num_data_stripes(struct imsm_map
*map
)
923 return join_u32(map
->num_data_stripes_lo
, map
->num_data_stripes_hi
);
926 static void set_total_blocks(struct imsm_disk
*disk
, unsigned long long n
)
928 split_ull(n
, &disk
->total_blocks_lo
, &disk
->total_blocks_hi
);
932 static void set_pba_of_lba0(struct imsm_map
*map
, unsigned long long n
)
934 split_ull(n
, &map
->pba_of_lba0_lo
, &map
->pba_of_lba0_hi
);
937 static void set_blocks_per_member(struct imsm_map
*map
, unsigned long long n
)
939 split_ull(n
, &map
->blocks_per_member_lo
, &map
->blocks_per_member_hi
);
942 static void set_num_data_stripes(struct imsm_map
*map
, unsigned long long n
)
944 split_ull(n
, &map
->num_data_stripes_lo
, &map
->num_data_stripes_hi
);
947 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
)
949 /* find a list of used extents on the given physical device */
950 struct extent
*rv
, *e
;
952 int memberships
= count_memberships(dl
, super
);
955 /* trim the reserved area for spares, so they can join any array
956 * regardless of whether the OROM has assigned sectors from the
957 * IMSM_RESERVED_SECTORS region
960 reservation
= imsm_min_reserved_sectors(super
);
962 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
964 rv
= xcalloc(sizeof(struct extent
), (memberships
+ 1));
967 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
968 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
969 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
971 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
972 e
->start
= pba_of_lba0(map
);
973 e
->size
= blocks_per_member(map
);
977 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
979 /* determine the start of the metadata
980 * when no raid devices are defined use the default
981 * ...otherwise allow the metadata to truncate the value
982 * as is the case with older versions of imsm
985 struct extent
*last
= &rv
[memberships
- 1];
986 unsigned long long remainder
;
988 remainder
= total_blocks(&dl
->disk
) - (last
->start
+ last
->size
);
989 /* round down to 1k block to satisfy precision of the kernel
993 /* make sure remainder is still sane */
994 if (remainder
< (unsigned)ROUND_UP(super
->len
, 512) >> 9)
995 remainder
= ROUND_UP(super
->len
, 512) >> 9;
996 if (reservation
> remainder
)
997 reservation
= remainder
;
999 e
->start
= total_blocks(&dl
->disk
) - reservation
;
1004 /* try to determine how much space is reserved for metadata from
1005 * the last get_extents() entry, otherwise fallback to the
1008 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
1014 /* for spares just return a minimal reservation which will grow
1015 * once the spare is picked up by an array
1017 if (dl
->index
== -1)
1018 return MPB_SECTOR_CNT
;
1020 e
= get_extents(super
, dl
);
1022 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1024 /* scroll to last entry */
1025 for (i
= 0; e
[i
].size
; i
++)
1028 rv
= total_blocks(&dl
->disk
) - e
[i
].start
;
1035 static int is_spare(struct imsm_disk
*disk
)
1037 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
1040 static int is_configured(struct imsm_disk
*disk
)
1042 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
1045 static int is_failed(struct imsm_disk
*disk
)
1047 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
1050 /* try to determine how much space is reserved for metadata from
1051 * the last get_extents() entry on the smallest active disk,
1052 * otherwise fallback to the default
1054 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
)
1058 unsigned long long min_active
;
1060 __u32 rv
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1061 struct dl
*dl
, *dl_min
= NULL
;
1067 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
1070 unsigned long long blocks
= total_blocks(&dl
->disk
);
1071 if (blocks
< min_active
|| min_active
== 0) {
1073 min_active
= blocks
;
1079 /* find last lba used by subarrays on the smallest active disk */
1080 e
= get_extents(super
, dl_min
);
1083 for (i
= 0; e
[i
].size
; i
++)
1086 remainder
= min_active
- e
[i
].start
;
1089 /* to give priority to recovery we should not require full
1090 IMSM_RESERVED_SECTORS from the spare */
1091 rv
= MPB_SECTOR_CNT
+ NUM_BLOCKS_DIRTY_STRIPE_REGION
;
1093 /* if real reservation is smaller use that value */
1094 return (remainder
< rv
) ? remainder
: rv
;
1097 /* Return minimum size of a spare that can be used in this array*/
1098 static unsigned long long min_acceptable_spare_size_imsm(struct supertype
*st
)
1100 struct intel_super
*super
= st
->sb
;
1104 unsigned long long rv
= 0;
1108 /* find first active disk in array */
1110 while (dl
&& (is_failed(&dl
->disk
) || dl
->index
== -1))
1114 /* find last lba used by subarrays */
1115 e
= get_extents(super
, dl
);
1118 for (i
= 0; e
[i
].size
; i
++)
1121 rv
= e
[i
-1].start
+ e
[i
-1].size
;
1124 /* add the amount of space needed for metadata */
1125 rv
= rv
+ imsm_min_reserved_sectors(super
);
1130 static int is_gen_migration(struct imsm_dev
*dev
);
1133 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
1134 struct imsm_dev
*dev
);
1136 static void print_imsm_dev(struct intel_super
*super
,
1137 struct imsm_dev
*dev
,
1143 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1144 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
1148 printf("[%.16s]:\n", dev
->volume
);
1149 printf(" UUID : %s\n", uuid
);
1150 printf(" RAID Level : %d", get_imsm_raid_level(map
));
1152 printf(" <-- %d", get_imsm_raid_level(map2
));
1154 printf(" Members : %d", map
->num_members
);
1156 printf(" <-- %d", map2
->num_members
);
1158 printf(" Slots : [");
1159 for (i
= 0; i
< map
->num_members
; i
++) {
1160 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
1161 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1166 for (i
= 0; i
< map2
->num_members
; i
++) {
1167 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_1
);
1168 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1173 printf(" Failed disk : ");
1174 if (map
->failed_disk_num
== 0xff)
1177 printf("%i", map
->failed_disk_num
);
1179 slot
= get_imsm_disk_slot(map
, disk_idx
);
1181 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
1182 printf(" This Slot : %d%s\n", slot
,
1183 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
1185 printf(" This Slot : ?\n");
1186 sz
= __le32_to_cpu(dev
->size_high
);
1188 sz
+= __le32_to_cpu(dev
->size_low
);
1189 printf(" Array Size : %llu%s\n", (unsigned long long)sz
,
1190 human_size(sz
* 512));
1191 sz
= blocks_per_member(map
);
1192 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz
,
1193 human_size(sz
* 512));
1194 printf(" Sector Offset : %llu\n",
1196 printf(" Num Stripes : %llu\n",
1197 num_data_stripes(map
));
1198 printf(" Chunk Size : %u KiB",
1199 __le16_to_cpu(map
->blocks_per_strip
) / 2);
1201 printf(" <-- %u KiB",
1202 __le16_to_cpu(map2
->blocks_per_strip
) / 2);
1204 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
1205 printf(" Migrate State : ");
1206 if (dev
->vol
.migr_state
) {
1207 if (migr_type(dev
) == MIGR_INIT
)
1208 printf("initialize\n");
1209 else if (migr_type(dev
) == MIGR_REBUILD
)
1210 printf("rebuild\n");
1211 else if (migr_type(dev
) == MIGR_VERIFY
)
1213 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
1214 printf("general migration\n");
1215 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
1216 printf("state change\n");
1217 else if (migr_type(dev
) == MIGR_REPAIR
)
1220 printf("<unknown:%d>\n", migr_type(dev
));
1223 printf(" Map State : %s", map_state_str
[map
->map_state
]);
1224 if (dev
->vol
.migr_state
) {
1225 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1227 printf(" <-- %s", map_state_str
[map
->map_state
]);
1228 printf("\n Checkpoint : %u ",
1229 __le32_to_cpu(dev
->vol
.curr_migr_unit
));
1230 if ((is_gen_migration(dev
)) && ((slot
> 1) || (slot
< 0)))
1233 printf("(%llu)", (unsigned long long)
1234 blocks_per_migr_unit(super
, dev
));
1237 printf(" Dirty State : %s\n", dev
->vol
.dirty
? "dirty" : "clean");
1240 static void print_imsm_disk(struct imsm_disk
*disk
, int index
, __u32 reserved
)
1242 char str
[MAX_RAID_SERIAL_LEN
+ 1];
1245 if (index
< -1 || !disk
)
1249 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
1251 printf(" Disk%02d Serial : %s\n", index
, str
);
1253 printf(" Disk Serial : %s\n", str
);
1254 printf(" State :%s%s%s\n", is_spare(disk
) ? " spare" : "",
1255 is_configured(disk
) ? " active" : "",
1256 is_failed(disk
) ? " failed" : "");
1257 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
1258 sz
= total_blocks(disk
) - reserved
;
1259 printf(" Usable Size : %llu%s\n", (unsigned long long)sz
,
1260 human_size(sz
* 512));
1263 void examine_migr_rec_imsm(struct intel_super
*super
)
1265 struct migr_record
*migr_rec
= super
->migr_rec
;
1266 struct imsm_super
*mpb
= super
->anchor
;
1269 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1270 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1271 struct imsm_map
*map
;
1274 if (is_gen_migration(dev
) == 0)
1277 printf("\nMigration Record Information:");
1279 /* first map under migration */
1280 map
= get_imsm_map(dev
, MAP_0
);
1282 slot
= get_imsm_disk_slot(map
, super
->disks
->index
);
1283 if ((map
== NULL
) || (slot
> 1) || (slot
< 0)) {
1284 printf(" Empty\n ");
1285 printf("Examine one of first two disks in array\n");
1288 printf("\n Status : ");
1289 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
1292 printf("Contains Data\n");
1293 printf(" Current Unit : %u\n",
1294 __le32_to_cpu(migr_rec
->curr_migr_unit
));
1295 printf(" Family : %u\n",
1296 __le32_to_cpu(migr_rec
->family_num
));
1297 printf(" Ascending : %u\n",
1298 __le32_to_cpu(migr_rec
->ascending_migr
));
1299 printf(" Blocks Per Unit : %u\n",
1300 __le32_to_cpu(migr_rec
->blocks_per_unit
));
1301 printf(" Dest. Depth Per Unit : %u\n",
1302 __le32_to_cpu(migr_rec
->dest_depth_per_unit
));
1303 printf(" Checkpoint Area pba : %u\n",
1304 __le32_to_cpu(migr_rec
->ckpt_area_pba
));
1305 printf(" First member lba : %u\n",
1306 __le32_to_cpu(migr_rec
->dest_1st_member_lba
));
1307 printf(" Total Number of Units : %u\n",
1308 __le32_to_cpu(migr_rec
->num_migr_units
));
1309 printf(" Size of volume : %u\n",
1310 __le32_to_cpu(migr_rec
->post_migr_vol_cap
));
1311 printf(" Expansion space for LBA64 : %u\n",
1312 __le32_to_cpu(migr_rec
->post_migr_vol_cap_hi
));
1313 printf(" Record was read from : %u\n",
1314 __le32_to_cpu(migr_rec
->ckpt_read_disk_num
));
1319 #endif /* MDASSEMBLE */
1320 /*******************************************************************************
1321 * function: imsm_check_attributes
1322 * Description: Function checks if features represented by attributes flags
1323 * are supported by mdadm.
1325 * attributes - Attributes read from metadata
1327 * 0 - passed attributes contains unsupported features flags
1328 * 1 - all features are supported
1329 ******************************************************************************/
1330 static int imsm_check_attributes(__u32 attributes
)
1333 __u32 not_supported
= MPB_ATTRIB_SUPPORTED
^0xffffffff;
1335 not_supported
&= ~MPB_ATTRIB_IGNORED
;
1337 not_supported
&= attributes
;
1338 if (not_supported
) {
1339 pr_err("(IMSM): Unsupported attributes : %x\n",
1340 (unsigned)__le32_to_cpu(not_supported
));
1341 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1342 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
1343 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1345 if (not_supported
& MPB_ATTRIB_2TB
) {
1346 dprintf("\t\tMPB_ATTRIB_2TB\n");
1347 not_supported
^= MPB_ATTRIB_2TB
;
1349 if (not_supported
& MPB_ATTRIB_RAID0
) {
1350 dprintf("\t\tMPB_ATTRIB_RAID0\n");
1351 not_supported
^= MPB_ATTRIB_RAID0
;
1353 if (not_supported
& MPB_ATTRIB_RAID1
) {
1354 dprintf("\t\tMPB_ATTRIB_RAID1\n");
1355 not_supported
^= MPB_ATTRIB_RAID1
;
1357 if (not_supported
& MPB_ATTRIB_RAID10
) {
1358 dprintf("\t\tMPB_ATTRIB_RAID10\n");
1359 not_supported
^= MPB_ATTRIB_RAID10
;
1361 if (not_supported
& MPB_ATTRIB_RAID1E
) {
1362 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
1363 not_supported
^= MPB_ATTRIB_RAID1E
;
1365 if (not_supported
& MPB_ATTRIB_RAID5
) {
1366 dprintf("\t\tMPB_ATTRIB_RAID5\n");
1367 not_supported
^= MPB_ATTRIB_RAID5
;
1369 if (not_supported
& MPB_ATTRIB_RAIDCNG
) {
1370 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
1371 not_supported
^= MPB_ATTRIB_RAIDCNG
;
1373 if (not_supported
& MPB_ATTRIB_BBM
) {
1374 dprintf("\t\tMPB_ATTRIB_BBM\n");
1375 not_supported
^= MPB_ATTRIB_BBM
;
1377 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1378 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
1379 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1381 if (not_supported
& MPB_ATTRIB_EXP_STRIPE_SIZE
) {
1382 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
1383 not_supported
^= MPB_ATTRIB_EXP_STRIPE_SIZE
;
1385 if (not_supported
& MPB_ATTRIB_2TB_DISK
) {
1386 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
1387 not_supported
^= MPB_ATTRIB_2TB_DISK
;
1389 if (not_supported
& MPB_ATTRIB_NEVER_USE2
) {
1390 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
1391 not_supported
^= MPB_ATTRIB_NEVER_USE2
;
1393 if (not_supported
& MPB_ATTRIB_NEVER_USE
) {
1394 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
1395 not_supported
^= MPB_ATTRIB_NEVER_USE
;
1399 dprintf(Name
"(IMSM): Unknown attributes : %x\n", not_supported
);
1408 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
1410 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
1412 struct intel_super
*super
= st
->sb
;
1413 struct imsm_super
*mpb
= super
->anchor
;
1414 char str
[MAX_SIGNATURE_LENGTH
];
1419 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
1422 snprintf(str
, MPB_SIG_LEN
, "%s", mpb
->sig
);
1423 printf(" Magic : %s\n", str
);
1424 snprintf(str
, strlen(MPB_VERSION_RAID0
), "%s", get_imsm_version(mpb
));
1425 printf(" Version : %s\n", get_imsm_version(mpb
));
1426 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
1427 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
1428 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
1429 printf(" Attributes : ");
1430 if (imsm_check_attributes(mpb
->attributes
))
1431 printf("All supported\n");
1433 printf("not supported\n");
1434 getinfo_super_imsm(st
, &info
, NULL
);
1435 fname_from_uuid(st
, &info
, nbuf
, ':');
1436 printf(" UUID : %s\n", nbuf
+ 5);
1437 sum
= __le32_to_cpu(mpb
->check_sum
);
1438 printf(" Checksum : %08x %s\n", sum
,
1439 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
1440 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
));
1441 printf(" Disks : %d\n", mpb
->num_disks
);
1442 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
1443 print_imsm_disk(__get_imsm_disk(mpb
, super
->disks
->index
), super
->disks
->index
, reserved
);
1444 if (super
->bbm_log
) {
1445 struct bbm_log
*log
= super
->bbm_log
;
1448 printf("Bad Block Management Log:\n");
1449 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
1450 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
1451 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
1452 printf(" Spare Blocks : %d\n", __le32_to_cpu(log
->reserved_spare_block_count
));
1453 printf(" First Spare : %llx\n",
1454 (unsigned long long) __le64_to_cpu(log
->first_spare_lba
));
1456 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1458 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1460 super
->current_vol
= i
;
1461 getinfo_super_imsm(st
, &info
, NULL
);
1462 fname_from_uuid(st
, &info
, nbuf
, ':');
1463 print_imsm_dev(super
, dev
, nbuf
+ 5, super
->disks
->index
);
1465 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1466 if (i
== super
->disks
->index
)
1468 print_imsm_disk(__get_imsm_disk(mpb
, i
), i
, reserved
);
1471 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
1472 if (dl
->index
== -1)
1473 print_imsm_disk(&dl
->disk
, -1, reserved
);
1475 examine_migr_rec_imsm(super
);
1478 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
1480 /* We just write a generic IMSM ARRAY entry */
1483 struct intel_super
*super
= st
->sb
;
1485 if (!super
->anchor
->num_raid_devs
) {
1486 printf("ARRAY metadata=imsm\n");
1490 getinfo_super_imsm(st
, &info
, NULL
);
1491 fname_from_uuid(st
, &info
, nbuf
, ':');
1492 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
1495 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
1497 /* We just write a generic IMSM ARRAY entry */
1501 struct intel_super
*super
= st
->sb
;
1504 if (!super
->anchor
->num_raid_devs
)
1507 getinfo_super_imsm(st
, &info
, NULL
);
1508 fname_from_uuid(st
, &info
, nbuf
, ':');
1509 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1510 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1512 super
->current_vol
= i
;
1513 getinfo_super_imsm(st
, &info
, NULL
);
1514 fname_from_uuid(st
, &info
, nbuf1
, ':');
1515 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
1516 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
1520 static void export_examine_super_imsm(struct supertype
*st
)
1522 struct intel_super
*super
= st
->sb
;
1523 struct imsm_super
*mpb
= super
->anchor
;
1527 getinfo_super_imsm(st
, &info
, NULL
);
1528 fname_from_uuid(st
, &info
, nbuf
, ':');
1529 printf("MD_METADATA=imsm\n");
1530 printf("MD_LEVEL=container\n");
1531 printf("MD_UUID=%s\n", nbuf
+5);
1532 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
1535 static void detail_super_imsm(struct supertype
*st
, char *homehost
)
1540 getinfo_super_imsm(st
, &info
, NULL
);
1541 fname_from_uuid(st
, &info
, nbuf
, ':');
1542 printf("\n UUID : %s\n", nbuf
+ 5);
1545 static void brief_detail_super_imsm(struct supertype
*st
)
1549 getinfo_super_imsm(st
, &info
, NULL
);
1550 fname_from_uuid(st
, &info
, nbuf
, ':');
1551 printf(" UUID=%s", nbuf
+ 5);
1554 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
);
1555 static void fd2devname(int fd
, char *name
);
1557 static int ahci_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
1559 /* dump an unsorted list of devices attached to AHCI Intel storage
1560 * controller, as well as non-connected ports
1562 int hba_len
= strlen(hba_path
) + 1;
1567 unsigned long port_mask
= (1 << port_count
) - 1;
1569 if (port_count
> (int)sizeof(port_mask
) * 8) {
1571 pr_err("port_count %d out of range\n", port_count
);
1575 /* scroll through /sys/dev/block looking for devices attached to
1578 dir
= opendir("/sys/dev/block");
1579 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
1590 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
1592 path
= devt_to_devpath(makedev(major
, minor
));
1595 if (!path_attached_to_hba(path
, hba_path
)) {
1601 /* retrieve the scsi device type */
1602 if (asprintf(&device
, "/sys/dev/block/%d:%d/device/xxxxxxx", major
, minor
) < 0) {
1604 pr_err("failed to allocate 'device'\n");
1608 sprintf(device
, "/sys/dev/block/%d:%d/device/type", major
, minor
);
1609 if (load_sys(device
, buf
) != 0) {
1611 pr_err("failed to read device type for %s\n",
1617 type
= strtoul(buf
, NULL
, 10);
1619 /* if it's not a disk print the vendor and model */
1620 if (!(type
== 0 || type
== 7 || type
== 14)) {
1623 sprintf(device
, "/sys/dev/block/%d:%d/device/vendor", major
, minor
);
1624 if (load_sys(device
, buf
) == 0) {
1625 strncpy(vendor
, buf
, sizeof(vendor
));
1626 vendor
[sizeof(vendor
) - 1] = '\0';
1627 c
= (char *) &vendor
[sizeof(vendor
) - 1];
1628 while (isspace(*c
) || *c
== '\0')
1632 sprintf(device
, "/sys/dev/block/%d:%d/device/model", major
, minor
);
1633 if (load_sys(device
, buf
) == 0) {
1634 strncpy(model
, buf
, sizeof(model
));
1635 model
[sizeof(model
) - 1] = '\0';
1636 c
= (char *) &model
[sizeof(model
) - 1];
1637 while (isspace(*c
) || *c
== '\0')
1641 if (vendor
[0] && model
[0])
1642 sprintf(buf
, "%.64s %.64s", vendor
, model
);
1644 switch (type
) { /* numbers from hald/linux/device.c */
1645 case 1: sprintf(buf
, "tape"); break;
1646 case 2: sprintf(buf
, "printer"); break;
1647 case 3: sprintf(buf
, "processor"); break;
1649 case 5: sprintf(buf
, "cdrom"); break;
1650 case 6: sprintf(buf
, "scanner"); break;
1651 case 8: sprintf(buf
, "media_changer"); break;
1652 case 9: sprintf(buf
, "comm"); break;
1653 case 12: sprintf(buf
, "raid"); break;
1654 default: sprintf(buf
, "unknown");
1660 /* chop device path to 'host%d' and calculate the port number */
1661 c
= strchr(&path
[hba_len
], '/');
1664 pr_err("%s - invalid path name\n", path
+ hba_len
);
1669 if (sscanf(&path
[hba_len
], "host%d", &port
) == 1)
1673 *c
= '/'; /* repair the full string */
1674 pr_err("failed to determine port number for %s\n",
1681 /* mark this port as used */
1682 port_mask
&= ~(1 << port
);
1684 /* print out the device information */
1686 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
1690 fd
= dev_open(ent
->d_name
, O_RDONLY
);
1692 printf(" Port%d : - disk info unavailable -\n", port
);
1694 fd2devname(fd
, buf
);
1695 printf(" Port%d : %s", port
, buf
);
1696 if (imsm_read_serial(fd
, NULL
, (__u8
*) buf
) == 0)
1697 printf(" (%.*s)\n", MAX_RAID_SERIAL_LEN
, buf
);
1712 for (i
= 0; i
< port_count
; i
++)
1713 if (port_mask
& (1 << i
))
1714 printf(" Port%d : - no device attached -\n", i
);
1720 static void print_found_intel_controllers(struct sys_dev
*elem
)
1722 for (; elem
; elem
= elem
->next
) {
1723 pr_err("found Intel(R) ");
1724 if (elem
->type
== SYS_DEV_SATA
)
1725 fprintf(stderr
, "SATA ");
1726 else if (elem
->type
== SYS_DEV_SAS
)
1727 fprintf(stderr
, "SAS ");
1728 fprintf(stderr
, "RAID controller");
1730 fprintf(stderr
, " at %s", elem
->pci_id
);
1731 fprintf(stderr
, ".\n");
1736 static int ahci_get_port_count(const char *hba_path
, int *port_count
)
1743 if ((dir
= opendir(hba_path
)) == NULL
)
1746 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
1749 if (sscanf(ent
->d_name
, "host%d", &host
) != 1)
1751 if (*port_count
== 0)
1753 else if (host
< host_base
)
1756 if (host
+ 1 > *port_count
+ host_base
)
1757 *port_count
= host
+ 1 - host_base
;
1763 static void print_imsm_capability(const struct imsm_orom
*orom
)
1765 printf(" Platform : Intel(R) Matrix Storage Manager\n");
1766 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
1767 orom
->hotfix_ver
, orom
->build
);
1768 printf(" RAID Levels :%s%s%s%s%s\n",
1769 imsm_orom_has_raid0(orom
) ? " raid0" : "",
1770 imsm_orom_has_raid1(orom
) ? " raid1" : "",
1771 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
1772 imsm_orom_has_raid10(orom
) ? " raid10" : "",
1773 imsm_orom_has_raid5(orom
) ? " raid5" : "");
1774 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1775 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
1776 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
1777 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
1778 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
1779 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
1780 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
1781 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
1782 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
1783 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
1784 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
1785 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
1786 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
1787 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
1788 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
1789 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
1790 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
1791 printf(" 2TB volumes :%s supported\n",
1792 (orom
->attr
& IMSM_OROM_ATTR_2TB
)?"":" not");
1793 printf(" 2TB disks :%s supported\n",
1794 (orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
)?"":" not");
1795 printf(" Max Disks : %d\n", orom
->tds
);
1796 printf(" Max Volumes : %d per array, %d per controller\n",
1797 orom
->vpa
, orom
->vphba
);
1801 static void print_imsm_capability_export(const struct imsm_orom
*orom
)
1803 printf("MD_FIRMWARE_TYPE=imsm\n");
1804 printf("IMSM_VERSION=%d.%d.%d.%d\n",orom
->major_ver
, orom
->minor_ver
,
1805 orom
->hotfix_ver
, orom
->build
);
1806 printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n",
1807 imsm_orom_has_raid0(orom
) ? "raid0 " : "",
1808 imsm_orom_has_raid1(orom
) ? "raid1 " : "",
1809 imsm_orom_has_raid1e(orom
) ? "raid1e " : "",
1810 imsm_orom_has_raid5(orom
) ? "raid10 " : "",
1811 imsm_orom_has_raid10(orom
) ? "raid5 " : "");
1812 printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1813 imsm_orom_has_chunk(orom
, 2) ? "2k " : "",
1814 imsm_orom_has_chunk(orom
, 4) ? "4k " : "",
1815 imsm_orom_has_chunk(orom
, 8) ? "8k " : "",
1816 imsm_orom_has_chunk(orom
, 16) ? "16k " : "",
1817 imsm_orom_has_chunk(orom
, 32) ? "32k " : "",
1818 imsm_orom_has_chunk(orom
, 64) ? "64k " : "",
1819 imsm_orom_has_chunk(orom
, 128) ? "128k " : "",
1820 imsm_orom_has_chunk(orom
, 256) ? "256k " : "",
1821 imsm_orom_has_chunk(orom
, 512) ? "512k " : "",
1822 imsm_orom_has_chunk(orom
, 1024*1) ? "1M " : "",
1823 imsm_orom_has_chunk(orom
, 1024*2) ? "2M " : "",
1824 imsm_orom_has_chunk(orom
, 1024*4) ? "4M " : "",
1825 imsm_orom_has_chunk(orom
, 1024*8) ? "8M " : "",
1826 imsm_orom_has_chunk(orom
, 1024*16) ? "16M " : "",
1827 imsm_orom_has_chunk(orom
, 1024*32) ? "32M " : "",
1828 imsm_orom_has_chunk(orom
, 1024*64) ? "64M " : "");
1829 printf("IMSM_2TB_VOLUMES=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB
) ? "yes" : "no");
1830 printf("IMSM_2TB_DISKS=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) ? "yes" : "no");
1831 printf("IMSM_MAX_DISKS=%d\n",orom
->tds
);
1832 printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom
->vpa
);
1833 printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom
->vphba
);
1836 static int detail_platform_imsm(int verbose
, int enumerate_only
, char *controller_path
)
1838 /* There are two components to imsm platform support, the ahci SATA
1839 * controller and the option-rom. To find the SATA controller we
1840 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
1841 * controller with the Intel vendor id is present. This approach
1842 * allows mdadm to leverage the kernel's ahci detection logic, with the
1843 * caveat that if ahci.ko is not loaded mdadm will not be able to
1844 * detect platform raid capabilities. The option-rom resides in a
1845 * platform "Adapter ROM". We scan for its signature to retrieve the
1846 * platform capabilities. If raid support is disabled in the BIOS the
1847 * option-rom capability structure will not be available.
1849 const struct imsm_orom
*orom
;
1850 struct sys_dev
*list
, *hba
;
1855 if (enumerate_only
) {
1856 if (check_env("IMSM_NO_PLATFORM"))
1858 list
= find_intel_devices();
1861 for (hba
= list
; hba
; hba
= hba
->next
) {
1862 orom
= find_imsm_capability(hba
->type
);
1870 free_sys_dev(&list
);
1874 list
= find_intel_devices();
1877 pr_err("no active Intel(R) RAID "
1878 "controller found.\n");
1879 free_sys_dev(&list
);
1881 } else if (verbose
> 0)
1882 print_found_intel_controllers(list
);
1884 for (hba
= list
; hba
; hba
= hba
->next
) {
1885 if (controller_path
&& (compare_paths(hba
->path
,controller_path
) != 0))
1887 orom
= find_imsm_capability(hba
->type
);
1889 pr_err("imsm capabilities not found for controller: %s (type %s)\n",
1890 hba
->path
, get_sys_dev_type(hba
->type
));
1893 print_imsm_capability(orom
);
1894 printf(" I/O Controller : %s (%s)\n",
1895 hba
->path
, get_sys_dev_type(hba
->type
));
1896 if (hba
->type
== SYS_DEV_SATA
) {
1897 host_base
= ahci_get_port_count(hba
->path
, &port_count
);
1898 if (ahci_enumerate_ports(hba
->path
, port_count
, host_base
, verbose
)) {
1900 pr_err("failed to enumerate "
1901 "ports on SATA controller at %s.\n", hba
->pci_id
);
1908 if (controller_path
&& result
== 1)
1909 pr_err("no active Intel(R) RAID "
1910 "controller found under %s\n",controller_path
);
1912 free_sys_dev(&list
);
1916 static int export_detail_platform_imsm(int verbose
, char *controller_path
)
1918 const struct imsm_orom
*orom
;
1919 struct sys_dev
*list
, *hba
;
1922 list
= find_intel_devices();
1925 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n");
1927 free_sys_dev(&list
);
1931 for (hba
= list
; hba
; hba
= hba
->next
) {
1932 if (controller_path
&& (compare_paths(hba
->path
,controller_path
) != 0))
1934 orom
= find_imsm_capability(hba
->type
);
1937 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n",hba
->path
);
1940 print_imsm_capability_export(orom
);
1950 static int match_home_imsm(struct supertype
*st
, char *homehost
)
1952 /* the imsm metadata format does not specify any host
1953 * identification information. We return -1 since we can never
1954 * confirm nor deny whether a given array is "meant" for this
1955 * host. We rely on compare_super and the 'family_num' fields to
1956 * exclude member disks that do not belong, and we rely on
1957 * mdadm.conf to specify the arrays that should be assembled.
1958 * Auto-assembly may still pick up "foreign" arrays.
1964 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
1966 /* The uuid returned here is used for:
1967 * uuid to put into bitmap file (Create, Grow)
1968 * uuid for backup header when saving critical section (Grow)
1969 * comparing uuids when re-adding a device into an array
1970 * In these cases the uuid required is that of the data-array,
1971 * not the device-set.
1972 * uuid to recognise same set when adding a missing device back
1973 * to an array. This is a uuid for the device-set.
1975 * For each of these we can make do with a truncated
1976 * or hashed uuid rather than the original, as long as
1978 * In each case the uuid required is that of the data-array,
1979 * not the device-set.
1981 /* imsm does not track uuid's so we synthesis one using sha1 on
1982 * - The signature (Which is constant for all imsm array, but no matter)
1983 * - the orig_family_num of the container
1984 * - the index number of the volume
1985 * - the 'serial' number of the volume.
1986 * Hopefully these are all constant.
1988 struct intel_super
*super
= st
->sb
;
1991 struct sha1_ctx ctx
;
1992 struct imsm_dev
*dev
= NULL
;
1995 /* some mdadm versions failed to set ->orig_family_num, in which
1996 * case fall back to ->family_num. orig_family_num will be
1997 * fixed up with the first metadata update.
1999 family_num
= super
->anchor
->orig_family_num
;
2000 if (family_num
== 0)
2001 family_num
= super
->anchor
->family_num
;
2002 sha1_init_ctx(&ctx
);
2003 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
2004 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
2005 if (super
->current_vol
>= 0)
2006 dev
= get_imsm_dev(super
, super
->current_vol
);
2008 __u32 vol
= super
->current_vol
;
2009 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
2010 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
2012 sha1_finish_ctx(&ctx
, buf
);
2013 memcpy(uuid
, buf
, 4*4);
2018 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
2020 __u8
*v
= get_imsm_version(mpb
);
2021 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
2022 char major
[] = { 0, 0, 0 };
2023 char minor
[] = { 0 ,0, 0 };
2024 char patch
[] = { 0, 0, 0 };
2025 char *ver_parse
[] = { major
, minor
, patch
};
2029 while (*v
!= '\0' && v
< end
) {
2030 if (*v
!= '.' && j
< 2)
2031 ver_parse
[i
][j
++] = *v
;
2039 *m
= strtol(minor
, NULL
, 0);
2040 *p
= strtol(patch
, NULL
, 0);
2044 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
2046 /* migr_strip_size when repairing or initializing parity */
2047 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2048 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2050 switch (get_imsm_raid_level(map
)) {
2055 return 128*1024 >> 9;
2059 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
2061 /* migr_strip_size when rebuilding a degraded disk, no idea why
2062 * this is different than migr_strip_size_resync(), but it's good
2065 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2066 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2068 switch (get_imsm_raid_level(map
)) {
2071 if (map
->num_members
% map
->num_domains
== 0)
2072 return 128*1024 >> 9;
2076 return max((__u32
) 64*1024 >> 9, chunk
);
2078 return 128*1024 >> 9;
2082 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
2084 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2085 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2086 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
2087 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
2089 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
2092 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
2094 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2095 int level
= get_imsm_raid_level(lo
);
2097 if (level
== 1 || level
== 10) {
2098 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2100 return hi
->num_domains
;
2102 return num_stripes_per_unit_resync(dev
);
2105 static __u8
imsm_num_data_members(struct imsm_dev
*dev
, int second_map
)
2107 /* named 'imsm_' because raid0, raid1 and raid10
2108 * counter-intuitively have the same number of data disks
2110 struct imsm_map
*map
= get_imsm_map(dev
, second_map
);
2112 switch (get_imsm_raid_level(map
)) {
2114 return map
->num_members
;
2118 return map
->num_members
/2;
2120 return map
->num_members
- 1;
2122 dprintf("%s: unsupported raid level\n", __func__
);
2127 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
2129 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2130 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2132 switch(get_imsm_raid_level(map
)) {
2135 return chunk
* map
->num_domains
;
2137 return chunk
* map
->num_members
;
2143 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
2145 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2146 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2147 __u32 strip
= block
/ chunk
;
2149 switch (get_imsm_raid_level(map
)) {
2152 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
2153 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
2155 return vol_stripe
* chunk
+ block
% chunk
;
2157 __u32 stripe
= strip
/ (map
->num_members
- 1);
2159 return stripe
* chunk
+ block
% chunk
;
2166 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
2167 struct imsm_dev
*dev
)
2169 /* calculate the conversion factor between per member 'blocks'
2170 * (md/{resync,rebuild}_start) and imsm migration units, return
2171 * 0 for the 'not migrating' and 'unsupported migration' cases
2173 if (!dev
->vol
.migr_state
)
2176 switch (migr_type(dev
)) {
2177 case MIGR_GEN_MIGR
: {
2178 struct migr_record
*migr_rec
= super
->migr_rec
;
2179 return __le32_to_cpu(migr_rec
->blocks_per_unit
);
2184 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2185 __u32 stripes_per_unit
;
2186 __u32 blocks_per_unit
;
2195 /* yes, this is really the translation of migr_units to
2196 * per-member blocks in the 'resync' case
2198 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
2199 migr_chunk
= migr_strip_blocks_resync(dev
);
2200 disks
= imsm_num_data_members(dev
, MAP_0
);
2201 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
2202 stripe
= __le16_to_cpu(map
->blocks_per_strip
) * disks
;
2203 segment
= blocks_per_unit
/ stripe
;
2204 block_rel
= blocks_per_unit
- segment
* stripe
;
2205 parity_depth
= parity_segment_depth(dev
);
2206 block_map
= map_migr_block(dev
, block_rel
);
2207 return block_map
+ parity_depth
* segment
;
2209 case MIGR_REBUILD
: {
2210 __u32 stripes_per_unit
;
2213 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
2214 migr_chunk
= migr_strip_blocks_rebuild(dev
);
2215 return migr_chunk
* stripes_per_unit
;
2217 case MIGR_STATE_CHANGE
:
2223 static int imsm_level_to_layout(int level
)
2231 return ALGORITHM_LEFT_ASYMMETRIC
;
2238 /*******************************************************************************
2239 * Function: read_imsm_migr_rec
2240 * Description: Function reads imsm migration record from last sector of disk
2242 * fd : disk descriptor
2243 * super : metadata info
2247 ******************************************************************************/
2248 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
)
2251 unsigned long long dsize
;
2253 get_dev_size(fd
, NULL
, &dsize
);
2254 if (lseek64(fd
, dsize
- MIGR_REC_POSITION
, SEEK_SET
) < 0) {
2255 pr_err("Cannot seek to anchor block: %s\n",
2259 if (read(fd
, super
->migr_rec_buf
, MIGR_REC_BUF_SIZE
) !=
2260 MIGR_REC_BUF_SIZE
) {
2261 pr_err("Cannot read migr record block: %s\n",
2271 static struct imsm_dev
*imsm_get_device_during_migration(
2272 struct intel_super
*super
)
2275 struct intel_dev
*dv
;
2277 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
2278 if (is_gen_migration(dv
->dev
))
2284 /*******************************************************************************
2285 * Function: load_imsm_migr_rec
2286 * Description: Function reads imsm migration record (it is stored at the last
2289 * super : imsm internal array info
2290 * info : general array info
2294 * -2 : no migration in progress
2295 ******************************************************************************/
2296 static int load_imsm_migr_rec(struct intel_super
*super
, struct mdinfo
*info
)
2299 struct dl
*dl
= NULL
;
2303 struct imsm_dev
*dev
;
2304 struct imsm_map
*map
= NULL
;
2307 /* find map under migration */
2308 dev
= imsm_get_device_during_migration(super
);
2309 /* nothing to load,no migration in progress?
2313 map
= get_imsm_map(dev
, MAP_0
);
2316 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
2317 /* skip spare and failed disks
2319 if (sd
->disk
.raid_disk
< 0)
2321 /* read only from one of the first two slots */
2323 slot
= get_imsm_disk_slot(map
,
2324 sd
->disk
.raid_disk
);
2325 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2328 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
2329 fd
= dev_open(nm
, O_RDONLY
);
2335 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
2336 /* skip spare and failed disks
2340 /* read only from one of the first two slots */
2342 slot
= get_imsm_disk_slot(map
, dl
->index
);
2343 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2345 sprintf(nm
, "%d:%d", dl
->major
, dl
->minor
);
2346 fd
= dev_open(nm
, O_RDONLY
);
2353 retval
= read_imsm_migr_rec(fd
, super
);
2362 /*******************************************************************************
2363 * function: imsm_create_metadata_checkpoint_update
2364 * Description: It creates update for checkpoint change.
2366 * super : imsm internal array info
2367 * u : pointer to prepared update
2370 * If length is equal to 0, input pointer u contains no update
2371 ******************************************************************************/
2372 static int imsm_create_metadata_checkpoint_update(
2373 struct intel_super
*super
,
2374 struct imsm_update_general_migration_checkpoint
**u
)
2377 int update_memory_size
= 0;
2379 dprintf("imsm_create_metadata_checkpoint_update(enter)\n");
2385 /* size of all update data without anchor */
2386 update_memory_size
=
2387 sizeof(struct imsm_update_general_migration_checkpoint
);
2389 *u
= xcalloc(1, update_memory_size
);
2391 dprintf("error: cannot get memory for "
2392 "imsm_create_metadata_checkpoint_update update\n");
2395 (*u
)->type
= update_general_migration_checkpoint
;
2396 (*u
)->curr_migr_unit
= __le32_to_cpu(super
->migr_rec
->curr_migr_unit
);
2397 dprintf("imsm_create_metadata_checkpoint_update: prepared for %u\n",
2398 (*u
)->curr_migr_unit
);
2400 return update_memory_size
;
2404 static void imsm_update_metadata_locally(struct supertype
*st
,
2405 void *buf
, int len
);
2407 /*******************************************************************************
2408 * Function: write_imsm_migr_rec
2409 * Description: Function writes imsm migration record
2410 * (at the last sector of disk)
2412 * super : imsm internal array info
2416 ******************************************************************************/
2417 static int write_imsm_migr_rec(struct supertype
*st
)
2419 struct intel_super
*super
= st
->sb
;
2420 unsigned long long dsize
;
2426 struct imsm_update_general_migration_checkpoint
*u
;
2427 struct imsm_dev
*dev
;
2428 struct imsm_map
*map
= NULL
;
2430 /* find map under migration */
2431 dev
= imsm_get_device_during_migration(super
);
2432 /* if no migration, write buffer anyway to clear migr_record
2433 * on disk based on first available device
2436 dev
= get_imsm_dev(super
, super
->current_vol
< 0 ? 0 :
2437 super
->current_vol
);
2439 map
= get_imsm_map(dev
, MAP_0
);
2441 for (sd
= super
->disks
; sd
; sd
= sd
->next
) {
2444 /* skip failed and spare devices */
2447 /* write to 2 first slots only */
2449 slot
= get_imsm_disk_slot(map
, sd
->index
);
2450 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2453 sprintf(nm
, "%d:%d", sd
->major
, sd
->minor
);
2454 fd
= dev_open(nm
, O_RDWR
);
2457 get_dev_size(fd
, NULL
, &dsize
);
2458 if (lseek64(fd
, dsize
- MIGR_REC_POSITION
, SEEK_SET
) < 0) {
2459 pr_err("Cannot seek to anchor block: %s\n",
2463 if (write(fd
, super
->migr_rec_buf
, MIGR_REC_BUF_SIZE
) !=
2464 MIGR_REC_BUF_SIZE
) {
2465 pr_err("Cannot write migr record block: %s\n",
2472 /* update checkpoint information in metadata */
2473 len
= imsm_create_metadata_checkpoint_update(super
, &u
);
2476 dprintf("imsm: Cannot prepare update\n");
2479 /* update metadata locally */
2480 imsm_update_metadata_locally(st
, u
, len
);
2481 /* and possibly remotely */
2482 if (st
->update_tail
) {
2483 append_metadata_update(st
, u
, len
);
2484 /* during reshape we do all work inside metadata handler
2485 * manage_reshape(), so metadata update has to be triggered
2488 flush_metadata_updates(st
);
2489 st
->update_tail
= &st
->updates
;
2499 #endif /* MDASSEMBLE */
2501 /* spare/missing disks activations are not allowe when
2502 * array/container performs reshape operation, because
2503 * all arrays in container works on the same disks set
2505 int imsm_reshape_blocks_arrays_changes(struct intel_super
*super
)
2508 struct intel_dev
*i_dev
;
2509 struct imsm_dev
*dev
;
2511 /* check whole container
2513 for (i_dev
= super
->devlist
; i_dev
; i_dev
= i_dev
->next
) {
2515 if (is_gen_migration(dev
)) {
2516 /* No repair during any migration in container
2524 static unsigned long long imsm_component_size_aligment_check(int level
,
2526 unsigned long long component_size
)
2528 unsigned int component_size_alligment
;
2530 /* check component size aligment
2532 component_size_alligment
= component_size
% (chunk_size
/512);
2534 dprintf("imsm_component_size_aligment_check(Level: %i, "
2535 "chunk_size = %i, component_size = %llu), "
2536 "component_size_alligment = %u\n",
2537 level
, chunk_size
, component_size
,
2538 component_size_alligment
);
2540 if (component_size_alligment
&& (level
!= 1) && (level
!= UnSet
)) {
2541 dprintf("imsm: reported component size alligned from %llu ",
2543 component_size
-= component_size_alligment
;
2544 dprintf("to %llu (%i).\n",
2545 component_size
, component_size_alligment
);
2548 return component_size
;
2551 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
2553 struct intel_super
*super
= st
->sb
;
2554 struct migr_record
*migr_rec
= super
->migr_rec
;
2555 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
2556 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2557 struct imsm_map
*prev_map
= get_imsm_map(dev
, MAP_1
);
2558 struct imsm_map
*map_to_analyse
= map
;
2561 int map_disks
= info
->array
.raid_disks
;
2563 memset(info
, 0, sizeof(*info
));
2565 map_to_analyse
= prev_map
;
2567 dl
= super
->current_disk
;
2569 info
->container_member
= super
->current_vol
;
2570 info
->array
.raid_disks
= map
->num_members
;
2571 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
2572 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
2573 info
->array
.md_minor
= -1;
2574 info
->array
.ctime
= 0;
2575 info
->array
.utime
= 0;
2576 info
->array
.chunk_size
=
2577 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
2578 info
->array
.state
= !dev
->vol
.dirty
;
2579 info
->custom_array_size
= __le32_to_cpu(dev
->size_high
);
2580 info
->custom_array_size
<<= 32;
2581 info
->custom_array_size
|= __le32_to_cpu(dev
->size_low
);
2582 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
2584 if (is_gen_migration(dev
)) {
2585 info
->reshape_active
= 1;
2586 info
->new_level
= get_imsm_raid_level(map
);
2587 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
2588 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
2589 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
2590 if (info
->delta_disks
) {
2591 /* this needs to be applied to every array
2594 info
->reshape_active
= CONTAINER_RESHAPE
;
2596 /* We shape information that we give to md might have to be
2597 * modify to cope with md's requirement for reshaping arrays.
2598 * For example, when reshaping a RAID0, md requires it to be
2599 * presented as a degraded RAID4.
2600 * Also if a RAID0 is migrating to a RAID5 we need to specify
2601 * the array as already being RAID5, but the 'before' layout
2602 * is a RAID4-like layout.
2604 switch (info
->array
.level
) {
2606 switch(info
->new_level
) {
2608 /* conversion is happening as RAID4 */
2609 info
->array
.level
= 4;
2610 info
->array
.raid_disks
+= 1;
2613 /* conversion is happening as RAID5 */
2614 info
->array
.level
= 5;
2615 info
->array
.layout
= ALGORITHM_PARITY_N
;
2616 info
->delta_disks
-= 1;
2619 /* FIXME error message */
2620 info
->array
.level
= UnSet
;
2626 info
->new_level
= UnSet
;
2627 info
->new_layout
= UnSet
;
2628 info
->new_chunk
= info
->array
.chunk_size
;
2629 info
->delta_disks
= 0;
2633 info
->disk
.major
= dl
->major
;
2634 info
->disk
.minor
= dl
->minor
;
2635 info
->disk
.number
= dl
->index
;
2636 info
->disk
.raid_disk
= get_imsm_disk_slot(map_to_analyse
,
2640 info
->data_offset
= pba_of_lba0(map_to_analyse
);
2641 info
->component_size
= blocks_per_member(map_to_analyse
);
2643 info
->component_size
= imsm_component_size_aligment_check(
2645 info
->array
.chunk_size
,
2646 info
->component_size
);
2648 memset(info
->uuid
, 0, sizeof(info
->uuid
));
2649 info
->recovery_start
= MaxSector
;
2651 info
->reshape_progress
= 0;
2652 info
->resync_start
= MaxSector
;
2653 if ((map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
2655 imsm_reshape_blocks_arrays_changes(super
) == 0) {
2656 info
->resync_start
= 0;
2658 if (dev
->vol
.migr_state
) {
2659 switch (migr_type(dev
)) {
2662 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2664 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
2666 info
->resync_start
= blocks_per_unit
* units
;
2669 case MIGR_GEN_MIGR
: {
2670 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2672 __u64 units
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
2673 unsigned long long array_blocks
;
2676 if (__le32_to_cpu(migr_rec
->ascending_migr
) &&
2678 (__le32_to_cpu(migr_rec
->num_migr_units
)-1)) &&
2679 (super
->migr_rec
->rec_status
==
2680 __cpu_to_le32(UNIT_SRC_IN_CP_AREA
)))
2683 info
->reshape_progress
= blocks_per_unit
* units
;
2685 dprintf("IMSM: General Migration checkpoint : %llu "
2686 "(%llu) -> read reshape progress : %llu\n",
2687 (unsigned long long)units
,
2688 (unsigned long long)blocks_per_unit
,
2689 info
->reshape_progress
);
2691 used_disks
= imsm_num_data_members(dev
, MAP_1
);
2692 if (used_disks
> 0) {
2693 array_blocks
= blocks_per_member(map
) *
2695 /* round array size down to closest MB
2697 info
->custom_array_size
= (array_blocks
2698 >> SECT_PER_MB_SHIFT
)
2699 << SECT_PER_MB_SHIFT
;
2703 /* we could emulate the checkpointing of
2704 * 'sync_action=check' migrations, but for now
2705 * we just immediately complete them
2708 /* this is handled by container_content_imsm() */
2709 case MIGR_STATE_CHANGE
:
2710 /* FIXME handle other migrations */
2712 /* we are not dirty, so... */
2713 info
->resync_start
= MaxSector
;
2717 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
2718 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
2720 info
->array
.major_version
= -1;
2721 info
->array
.minor_version
= -2;
2722 devname
= devnum2devname(st
->container_dev
);
2723 *info
->text_version
= '\0';
2725 sprintf(info
->text_version
, "/%s/%d", devname
, info
->container_member
);
2727 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
2728 uuid_from_super_imsm(st
, info
->uuid
);
2732 for (i
=0; i
<map_disks
; i
++) {
2734 if (i
< info
->array
.raid_disks
) {
2735 struct imsm_disk
*dsk
;
2736 j
= get_imsm_disk_idx(dev
, i
, MAP_X
);
2737 dsk
= get_imsm_disk(super
, j
);
2738 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
2745 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
2746 int failed
, int look_in_map
);
2748 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
2753 static void manage_second_map(struct intel_super
*super
, struct imsm_dev
*dev
)
2755 if (is_gen_migration(dev
)) {
2758 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
2760 failed
= imsm_count_failed(super
, dev
, MAP_1
);
2761 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_1
);
2762 if (map2
->map_state
!= map_state
) {
2763 map2
->map_state
= map_state
;
2764 super
->updates_pending
++;
2770 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
2774 for (d
= super
->missing
; d
; d
= d
->next
)
2775 if (d
->index
== index
)
2780 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
2782 struct intel_super
*super
= st
->sb
;
2783 struct imsm_disk
*disk
;
2784 int map_disks
= info
->array
.raid_disks
;
2785 int max_enough
= -1;
2787 struct imsm_super
*mpb
;
2789 if (super
->current_vol
>= 0) {
2790 getinfo_super_imsm_volume(st
, info
, map
);
2793 memset(info
, 0, sizeof(*info
));
2795 /* Set raid_disks to zero so that Assemble will always pull in valid
2798 info
->array
.raid_disks
= 0;
2799 info
->array
.level
= LEVEL_CONTAINER
;
2800 info
->array
.layout
= 0;
2801 info
->array
.md_minor
= -1;
2802 info
->array
.ctime
= 0; /* N/A for imsm */
2803 info
->array
.utime
= 0;
2804 info
->array
.chunk_size
= 0;
2806 info
->disk
.major
= 0;
2807 info
->disk
.minor
= 0;
2808 info
->disk
.raid_disk
= -1;
2809 info
->reshape_active
= 0;
2810 info
->array
.major_version
= -1;
2811 info
->array
.minor_version
= -2;
2812 strcpy(info
->text_version
, "imsm");
2813 info
->safe_mode_delay
= 0;
2814 info
->disk
.number
= -1;
2815 info
->disk
.state
= 0;
2817 info
->recovery_start
= MaxSector
;
2818 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
2820 /* do we have the all the insync disks that we expect? */
2821 mpb
= super
->anchor
;
2823 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
2824 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
2825 int failed
, enough
, j
, missing
= 0;
2826 struct imsm_map
*map
;
2829 failed
= imsm_count_failed(super
, dev
, MAP_0
);
2830 state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
2831 map
= get_imsm_map(dev
, MAP_0
);
2833 /* any newly missing disks?
2834 * (catches single-degraded vs double-degraded)
2836 for (j
= 0; j
< map
->num_members
; j
++) {
2837 __u32 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
2838 __u32 idx
= ord_to_idx(ord
);
2840 if (!(ord
& IMSM_ORD_REBUILD
) &&
2841 get_imsm_missing(super
, idx
)) {
2847 if (state
== IMSM_T_STATE_FAILED
)
2849 else if (state
== IMSM_T_STATE_DEGRADED
&&
2850 (state
!= map
->map_state
|| missing
))
2852 else /* we're normal, or already degraded */
2854 if (is_gen_migration(dev
) && missing
) {
2855 /* during general migration we need all disks
2856 * that process is running on.
2857 * No new missing disk is allowed.
2861 /* no more checks necessary
2865 /* in the missing/failed disk case check to see
2866 * if at least one array is runnable
2868 max_enough
= max(max_enough
, enough
);
2870 dprintf("%s: enough: %d\n", __func__
, max_enough
);
2871 info
->container_enough
= max_enough
;
2874 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
2876 disk
= &super
->disks
->disk
;
2877 info
->data_offset
= total_blocks(&super
->disks
->disk
) - reserved
;
2878 info
->component_size
= reserved
;
2879 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
2880 /* we don't change info->disk.raid_disk here because
2881 * this state will be finalized in mdmon after we have
2882 * found the 'most fresh' version of the metadata
2884 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
2885 info
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
2888 /* only call uuid_from_super_imsm when this disk is part of a populated container,
2889 * ->compare_super may have updated the 'num_raid_devs' field for spares
2891 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
2892 uuid_from_super_imsm(st
, info
->uuid
);
2894 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
2896 /* I don't know how to compute 'map' on imsm, so use safe default */
2899 for (i
= 0; i
< map_disks
; i
++)
2905 /* allocates memory and fills disk in mdinfo structure
2906 * for each disk in array */
2907 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
2909 struct mdinfo
*mddev
= NULL
;
2910 struct intel_super
*super
= st
->sb
;
2911 struct imsm_disk
*disk
;
2914 if (!super
|| !super
->disks
)
2917 mddev
= xcalloc(1, sizeof(*mddev
));
2921 tmp
= xcalloc(1, sizeof(*tmp
));
2923 tmp
->next
= mddev
->devs
;
2925 tmp
->disk
.number
= count
++;
2926 tmp
->disk
.major
= dl
->major
;
2927 tmp
->disk
.minor
= dl
->minor
;
2928 tmp
->disk
.state
= is_configured(disk
) ?
2929 (1 << MD_DISK_ACTIVE
) : 0;
2930 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
2931 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
2932 tmp
->disk
.raid_disk
= -1;
2938 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
2939 char *update
, char *devname
, int verbose
,
2940 int uuid_set
, char *homehost
)
2942 /* For 'assemble' and 'force' we need to return non-zero if any
2943 * change was made. For others, the return value is ignored.
2944 * Update options are:
2945 * force-one : This device looks a bit old but needs to be included,
2946 * update age info appropriately.
2947 * assemble: clear any 'faulty' flag to allow this device to
2949 * force-array: Array is degraded but being forced, mark it clean
2950 * if that will be needed to assemble it.
2952 * newdev: not used ????
2953 * grow: Array has gained a new device - this is currently for
2955 * resync: mark as dirty so a resync will happen.
2956 * name: update the name - preserving the homehost
2957 * uuid: Change the uuid of the array to match watch is given
2959 * Following are not relevant for this imsm:
2960 * sparc2.2 : update from old dodgey metadata
2961 * super-minor: change the preferred_minor number
2962 * summaries: update redundant counters.
2963 * homehost: update the recorded homehost
2964 * _reshape_progress: record new reshape_progress position.
2967 struct intel_super
*super
= st
->sb
;
2968 struct imsm_super
*mpb
;
2970 /* we can only update container info */
2971 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
2974 mpb
= super
->anchor
;
2976 if (strcmp(update
, "uuid") == 0) {
2977 /* We take this to mean that the family_num should be updated.
2978 * However that is much smaller than the uuid so we cannot really
2979 * allow an explicit uuid to be given. And it is hard to reliably
2981 * So if !uuid_set we know the current uuid is random and just used
2982 * the first 'int' and copy it to the other 3 positions.
2983 * Otherwise we require the 4 'int's to be the same as would be the
2984 * case if we are using a random uuid. So an explicit uuid will be
2985 * accepted as long as all for ints are the same... which shouldn't hurt
2988 info
->uuid
[1] = info
->uuid
[2] = info
->uuid
[3] = info
->uuid
[0];
2991 if (info
->uuid
[0] != info
->uuid
[1] ||
2992 info
->uuid
[1] != info
->uuid
[2] ||
2993 info
->uuid
[2] != info
->uuid
[3])
2999 mpb
->orig_family_num
= info
->uuid
[0];
3000 } else if (strcmp(update
, "assemble") == 0)
3005 /* successful update? recompute checksum */
3007 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
3012 static size_t disks_to_mpb_size(int disks
)
3016 size
= sizeof(struct imsm_super
);
3017 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
3018 size
+= 2 * sizeof(struct imsm_dev
);
3019 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
3020 size
+= (4 - 2) * sizeof(struct imsm_map
);
3021 /* 4 possible disk_ord_tbl's */
3022 size
+= 4 * (disks
- 1) * sizeof(__u32
);
3027 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
,
3028 unsigned long long data_offset
)
3030 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
3033 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
3036 static void free_devlist(struct intel_super
*super
)
3038 struct intel_dev
*dv
;
3040 while (super
->devlist
) {
3041 dv
= super
->devlist
->next
;
3042 free(super
->devlist
->dev
);
3043 free(super
->devlist
);
3044 super
->devlist
= dv
;
3048 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
3050 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
3053 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
3057 * 0 same, or first was empty, and second was copied
3058 * 1 second had wrong number
3060 * 3 wrong other info
3062 struct intel_super
*first
= st
->sb
;
3063 struct intel_super
*sec
= tst
->sb
;
3070 /* in platform dependent environment test if the disks
3071 * use the same Intel hba
3073 if (!check_env("IMSM_NO_PLATFORM")) {
3074 if (!first
->hba
|| !sec
->hba
||
3075 (first
->hba
->type
!= sec
->hba
->type
)) {
3077 "HBAs of devices does not match %s != %s\n",
3078 first
->hba
? get_sys_dev_type(first
->hba
->type
) : NULL
,
3079 sec
->hba
? get_sys_dev_type(sec
->hba
->type
) : NULL
);
3084 /* if an anchor does not have num_raid_devs set then it is a free
3087 if (first
->anchor
->num_raid_devs
> 0 &&
3088 sec
->anchor
->num_raid_devs
> 0) {
3089 /* Determine if these disks might ever have been
3090 * related. Further disambiguation can only take place
3091 * in load_super_imsm_all
3093 __u32 first_family
= first
->anchor
->orig_family_num
;
3094 __u32 sec_family
= sec
->anchor
->orig_family_num
;
3096 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
3097 MAX_SIGNATURE_LENGTH
) != 0)
3100 if (first_family
== 0)
3101 first_family
= first
->anchor
->family_num
;
3102 if (sec_family
== 0)
3103 sec_family
= sec
->anchor
->family_num
;
3105 if (first_family
!= sec_family
)
3111 /* if 'first' is a spare promote it to a populated mpb with sec's
3114 if (first
->anchor
->num_raid_devs
== 0 &&
3115 sec
->anchor
->num_raid_devs
> 0) {
3117 struct intel_dev
*dv
;
3118 struct imsm_dev
*dev
;
3120 /* we need to copy raid device info from sec if an allocation
3121 * fails here we don't associate the spare
3123 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
3124 dv
= xmalloc(sizeof(*dv
));
3125 dev
= xmalloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
3128 dv
->next
= first
->devlist
;
3129 first
->devlist
= dv
;
3131 if (i
< sec
->anchor
->num_raid_devs
) {
3132 /* allocation failure */
3133 free_devlist(first
);
3134 fprintf(stderr
, "imsm: failed to associate spare\n");
3137 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
3138 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
3139 first
->anchor
->family_num
= sec
->anchor
->family_num
;
3140 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
3141 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
3142 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
3148 static void fd2devname(int fd
, char *name
)
3152 char dname
[PATH_MAX
];
3157 if (fstat(fd
, &st
) != 0)
3159 sprintf(path
, "/sys/dev/block/%d:%d",
3160 major(st
.st_rdev
), minor(st
.st_rdev
));
3162 rv
= readlink(path
, dname
, sizeof(dname
)-1);
3167 nm
= strrchr(dname
, '/');
3170 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
3174 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
3176 static int imsm_read_serial(int fd
, char *devname
,
3177 __u8 serial
[MAX_RAID_SERIAL_LEN
])
3179 unsigned char scsi_serial
[255];
3188 memset(scsi_serial
, 0, sizeof(scsi_serial
));
3190 rv
= scsi_get_serial(fd
, scsi_serial
, sizeof(scsi_serial
));
3192 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
3193 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3194 fd2devname(fd
, (char *) serial
);
3200 pr_err("Failed to retrieve serial for %s\n",
3205 rsp_len
= scsi_serial
[3];
3208 pr_err("Failed to retrieve serial for %s\n",
3212 rsp_buf
= (char *) &scsi_serial
[4];
3214 /* trim all whitespace and non-printable characters and convert
3217 for (i
= 0, dest
= rsp_buf
; i
< rsp_len
; i
++) {
3220 /* ':' is reserved for use in placeholder serial
3221 * numbers for missing disks
3229 len
= dest
- rsp_buf
;
3232 /* truncate leading characters */
3233 if (len
> MAX_RAID_SERIAL_LEN
) {
3234 dest
+= len
- MAX_RAID_SERIAL_LEN
;
3235 len
= MAX_RAID_SERIAL_LEN
;
3238 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3239 memcpy(serial
, dest
, len
);
3244 static int serialcmp(__u8
*s1
, __u8
*s2
)
3246 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
3249 static void serialcpy(__u8
*dest
, __u8
*src
)
3251 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
3254 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
3258 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3259 if (serialcmp(dl
->serial
, serial
) == 0)
3265 static struct imsm_disk
*
3266 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
3270 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3271 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3273 if (serialcmp(disk
->serial
, serial
) == 0) {
3284 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3286 struct imsm_disk
*disk
;
3291 __u8 serial
[MAX_RAID_SERIAL_LEN
];
3293 rv
= imsm_read_serial(fd
, devname
, serial
);
3298 dl
= xcalloc(1, sizeof(*dl
));
3301 dl
->major
= major(stb
.st_rdev
);
3302 dl
->minor
= minor(stb
.st_rdev
);
3303 dl
->next
= super
->disks
;
3304 dl
->fd
= keep_fd
? fd
: -1;
3305 assert(super
->disks
== NULL
);
3307 serialcpy(dl
->serial
, serial
);
3310 fd2devname(fd
, name
);
3312 dl
->devname
= xstrdup(devname
);
3314 dl
->devname
= xstrdup(name
);
3316 /* look up this disk's index in the current anchor */
3317 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
3320 /* only set index on disks that are a member of a
3321 * populated contianer, i.e. one with raid_devs
3323 if (is_failed(&dl
->disk
))
3325 else if (is_spare(&dl
->disk
))
3333 /* When migrating map0 contains the 'destination' state while map1
3334 * contains the current state. When not migrating map0 contains the
3335 * current state. This routine assumes that map[0].map_state is set to
3336 * the current array state before being called.
3338 * Migration is indicated by one of the following states
3339 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
3340 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
3341 * map1state=unitialized)
3342 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
3344 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
3345 * map1state=degraded)
3346 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
3349 static void migrate(struct imsm_dev
*dev
, struct intel_super
*super
,
3350 __u8 to_state
, int migr_type
)
3352 struct imsm_map
*dest
;
3353 struct imsm_map
*src
= get_imsm_map(dev
, MAP_0
);
3355 dev
->vol
.migr_state
= 1;
3356 set_migr_type(dev
, migr_type
);
3357 dev
->vol
.curr_migr_unit
= 0;
3358 dest
= get_imsm_map(dev
, MAP_1
);
3360 /* duplicate and then set the target end state in map[0] */
3361 memcpy(dest
, src
, sizeof_imsm_map(src
));
3362 if ((migr_type
== MIGR_REBUILD
) ||
3363 (migr_type
== MIGR_GEN_MIGR
)) {
3367 for (i
= 0; i
< src
->num_members
; i
++) {
3368 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
3369 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
3373 if (migr_type
== MIGR_GEN_MIGR
)
3374 /* Clear migration record */
3375 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
3377 src
->map_state
= to_state
;
3380 static void end_migration(struct imsm_dev
*dev
, struct intel_super
*super
,
3383 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3384 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
== 0 ?
3388 /* merge any IMSM_ORD_REBUILD bits that were not successfully
3389 * completed in the last migration.
3391 * FIXME add support for raid-level-migration
3393 if ((map_state
!= map
->map_state
) && (is_gen_migration(dev
) == 0) &&
3394 (prev
->map_state
!= IMSM_T_STATE_UNINITIALIZED
)) {
3395 /* when final map state is other than expected
3396 * merge maps (not for migration)
3400 for (i
= 0; i
< prev
->num_members
; i
++)
3401 for (j
= 0; j
< map
->num_members
; j
++)
3402 /* during online capacity expansion
3403 * disks position can be changed
3404 * if takeover is used
3406 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
3407 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
3408 map
->disk_ord_tbl
[j
] |=
3409 prev
->disk_ord_tbl
[i
];
3412 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3413 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3416 dev
->vol
.migr_state
= 0;
3417 set_migr_type(dev
, 0);
3418 dev
->vol
.curr_migr_unit
= 0;
3419 map
->map_state
= map_state
;
3423 static int parse_raid_devices(struct intel_super
*super
)
3426 struct imsm_dev
*dev_new
;
3427 size_t len
, len_migr
;
3429 size_t space_needed
= 0;
3430 struct imsm_super
*mpb
= super
->anchor
;
3432 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3433 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3434 struct intel_dev
*dv
;
3436 len
= sizeof_imsm_dev(dev_iter
, 0);
3437 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
3439 space_needed
+= len_migr
- len
;
3441 dv
= xmalloc(sizeof(*dv
));
3442 if (max_len
< len_migr
)
3444 if (max_len
> len_migr
)
3445 space_needed
+= max_len
- len_migr
;
3446 dev_new
= xmalloc(max_len
);
3447 imsm_copy_dev(dev_new
, dev_iter
);
3450 dv
->next
= super
->devlist
;
3451 super
->devlist
= dv
;
3454 /* ensure that super->buf is large enough when all raid devices
3457 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
3460 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
, 512);
3461 if (posix_memalign(&buf
, 512, len
) != 0)
3464 memcpy(buf
, super
->buf
, super
->len
);
3465 memset(buf
+ super
->len
, 0, len
- super
->len
);
3474 /* retrieve a pointer to the bbm log which starts after all raid devices */
3475 struct bbm_log
*__get_imsm_bbm_log(struct imsm_super
*mpb
)
3479 if (__le32_to_cpu(mpb
->bbm_log_size
)) {
3481 ptr
+= mpb
->mpb_size
- __le32_to_cpu(mpb
->bbm_log_size
);
3487 /*******************************************************************************
3488 * Function: check_mpb_migr_compatibility
3489 * Description: Function checks for unsupported migration features:
3490 * - migration optimization area (pba_of_lba0)
3491 * - descending reshape (ascending_migr)
3493 * super : imsm metadata information
3495 * 0 : migration is compatible
3496 * -1 : migration is not compatible
3497 ******************************************************************************/
3498 int check_mpb_migr_compatibility(struct intel_super
*super
)
3500 struct imsm_map
*map0
, *map1
;
3501 struct migr_record
*migr_rec
= super
->migr_rec
;
3504 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3505 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3508 dev_iter
->vol
.migr_state
== 1 &&
3509 dev_iter
->vol
.migr_type
== MIGR_GEN_MIGR
) {
3510 /* This device is migrating */
3511 map0
= get_imsm_map(dev_iter
, MAP_0
);
3512 map1
= get_imsm_map(dev_iter
, MAP_1
);
3513 if (pba_of_lba0(map0
) != pba_of_lba0(map1
))
3514 /* migration optimization area was used */
3516 if (migr_rec
->ascending_migr
== 0
3517 && migr_rec
->dest_depth_per_unit
> 0)
3518 /* descending reshape not supported yet */
3525 static void __free_imsm(struct intel_super
*super
, int free_disks
);
3527 /* load_imsm_mpb - read matrix metadata
3528 * allocates super->mpb to be freed by free_imsm
3530 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
3532 unsigned long long dsize
;
3533 unsigned long long sectors
;
3535 struct imsm_super
*anchor
;
3538 get_dev_size(fd
, NULL
, &dsize
);
3541 pr_err("%s: device to small for imsm\n",
3546 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0) {
3548 pr_err("Cannot seek to anchor block on %s: %s\n",
3549 devname
, strerror(errno
));
3553 if (posix_memalign((void**)&anchor
, 512, 512) != 0) {
3555 pr_err("Failed to allocate imsm anchor buffer"
3556 " on %s\n", devname
);
3559 if (read(fd
, anchor
, 512) != 512) {
3561 pr_err("Cannot read anchor block on %s: %s\n",
3562 devname
, strerror(errno
));
3567 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
3569 pr_err("no IMSM anchor on %s\n", devname
);
3574 __free_imsm(super
, 0);
3575 /* reload capability and hba */
3577 /* capability and hba must be updated with new super allocation */
3578 find_intel_hba_capability(fd
, super
, devname
);
3579 super
->len
= ROUND_UP(anchor
->mpb_size
, 512);
3580 if (posix_memalign(&super
->buf
, 512, super
->len
) != 0) {
3582 pr_err("unable to allocate %zu byte mpb buffer\n",
3587 memcpy(super
->buf
, anchor
, 512);
3589 sectors
= mpb_sectors(anchor
) - 1;
3592 if (posix_memalign(&super
->migr_rec_buf
, 512, MIGR_REC_BUF_SIZE
) != 0) {
3593 pr_err("%s could not allocate migr_rec buffer\n", __func__
);
3597 super
->clean_migration_record_by_mdmon
= 0;
3600 check_sum
= __gen_imsm_checksum(super
->anchor
);
3601 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3603 pr_err("IMSM checksum %x != %x on %s\n",
3605 __le32_to_cpu(super
->anchor
->check_sum
),
3613 /* read the extended mpb */
3614 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0) {
3616 pr_err("Cannot seek to extended mpb on %s: %s\n",
3617 devname
, strerror(errno
));
3621 if ((unsigned)read(fd
, super
->buf
+ 512, super
->len
- 512) != super
->len
- 512) {
3623 pr_err("Cannot read extended mpb on %s: %s\n",
3624 devname
, strerror(errno
));
3628 check_sum
= __gen_imsm_checksum(super
->anchor
);
3629 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3631 pr_err("IMSM checksum %x != %x on %s\n",
3632 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
3637 /* FIXME the BBM log is disk specific so we cannot use this global
3638 * buffer for all disks. Ok for now since we only look at the global
3639 * bbm_log_size parameter to gate assembly
3641 super
->bbm_log
= __get_imsm_bbm_log(super
->anchor
);
3646 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
);
3648 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
3649 static void clear_hi(struct intel_super
*super
)
3651 struct imsm_super
*mpb
= super
->anchor
;
3653 if (mpb
->attributes
& MPB_ATTRIB_2TB_DISK
)
3655 for (i
= 0; i
< mpb
->num_disks
; ++i
) {
3656 struct imsm_disk
*disk
= &mpb
->disk
[i
];
3657 disk
->total_blocks_hi
= 0;
3659 for (i
= 0; i
< mpb
->num_raid_devs
; ++i
) {
3660 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3663 for (n
= 0; n
< 2; ++n
) {
3664 struct imsm_map
*map
= get_imsm_map(dev
, n
);
3667 map
->pba_of_lba0_hi
= 0;
3668 map
->blocks_per_member_hi
= 0;
3669 map
->num_data_stripes_hi
= 0;
3675 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3679 err
= load_imsm_mpb(fd
, super
, devname
);
3682 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
3685 err
= parse_raid_devices(super
);
3690 static void __free_imsm_disk(struct dl
*d
)
3702 static void free_imsm_disks(struct intel_super
*super
)
3706 while (super
->disks
) {
3708 super
->disks
= d
->next
;
3709 __free_imsm_disk(d
);
3711 while (super
->disk_mgmt_list
) {
3712 d
= super
->disk_mgmt_list
;
3713 super
->disk_mgmt_list
= d
->next
;
3714 __free_imsm_disk(d
);
3716 while (super
->missing
) {
3718 super
->missing
= d
->next
;
3719 __free_imsm_disk(d
);
3724 /* free all the pieces hanging off of a super pointer */
3725 static void __free_imsm(struct intel_super
*super
, int free_disks
)
3727 struct intel_hba
*elem
, *next
;
3733 /* unlink capability description */
3735 if (super
->migr_rec_buf
) {
3736 free(super
->migr_rec_buf
);
3737 super
->migr_rec_buf
= NULL
;
3740 free_imsm_disks(super
);
3741 free_devlist(super
);
3745 free((void *)elem
->path
);
3753 static void free_imsm(struct intel_super
*super
)
3755 __free_imsm(super
, 1);
3759 static void free_super_imsm(struct supertype
*st
)
3761 struct intel_super
*super
= st
->sb
;
3770 static struct intel_super
*alloc_super(void)
3772 struct intel_super
*super
= xcalloc(1, sizeof(*super
));
3774 super
->current_vol
= -1;
3775 super
->create_offset
= ~((unsigned long long) 0);
3780 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
3782 static int find_intel_hba_capability(int fd
, struct intel_super
*super
, char *devname
)
3784 struct sys_dev
*hba_name
;
3787 if ((fd
< 0) || check_env("IMSM_NO_PLATFORM")) {
3792 hba_name
= find_disk_attached_hba(fd
, NULL
);
3795 pr_err("%s is not attached to Intel(R) RAID controller.\n",
3799 rv
= attach_hba_to_super(super
, hba_name
);
3802 struct intel_hba
*hba
= super
->hba
;
3804 pr_err("%s is attached to Intel(R) %s RAID "
3805 "controller (%s),\n"
3806 " but the container is assigned to Intel(R) "
3807 "%s RAID controller (",
3810 hba_name
->pci_id
? : "Err!",
3811 get_sys_dev_type(hba_name
->type
));
3814 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
3816 fprintf(stderr
, ", ");
3820 fprintf(stderr
, ").\n"
3821 " Mixing devices attached to different controllers "
3822 "is not allowed.\n");
3824 free_sys_dev(&hba_name
);
3827 super
->orom
= find_imsm_capability(hba_name
->type
);
3828 free_sys_dev(&hba_name
);
3834 /* find_missing - helper routine for load_super_imsm_all that identifies
3835 * disks that have disappeared from the system. This routine relies on
3836 * the mpb being uptodate, which it is at load time.
3838 static int find_missing(struct intel_super
*super
)
3841 struct imsm_super
*mpb
= super
->anchor
;
3843 struct imsm_disk
*disk
;
3845 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3846 disk
= __get_imsm_disk(mpb
, i
);
3847 dl
= serial_to_dl(disk
->serial
, super
);
3851 dl
= xmalloc(sizeof(*dl
));
3855 dl
->devname
= xstrdup("missing");
3857 serialcpy(dl
->serial
, disk
->serial
);
3860 dl
->next
= super
->missing
;
3861 super
->missing
= dl
;
3868 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
3870 struct intel_disk
*idisk
= disk_list
;
3873 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
3875 idisk
= idisk
->next
;
3881 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
3882 struct intel_super
*super
,
3883 struct intel_disk
**disk_list
)
3885 struct imsm_disk
*d
= &super
->disks
->disk
;
3886 struct imsm_super
*mpb
= super
->anchor
;
3889 for (i
= 0; i
< tbl_size
; i
++) {
3890 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
3891 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
3893 if (tbl_mpb
->family_num
== mpb
->family_num
) {
3894 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
3895 dprintf("%s: mpb from %d:%d matches %d:%d\n",
3896 __func__
, super
->disks
->major
,
3897 super
->disks
->minor
,
3898 table
[i
]->disks
->major
,
3899 table
[i
]->disks
->minor
);
3903 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
3904 is_configured(d
) == is_configured(tbl_d
)) &&
3905 tbl_mpb
->generation_num
< mpb
->generation_num
) {
3906 /* current version of the mpb is a
3907 * better candidate than the one in
3908 * super_table, but copy over "cross
3909 * generational" status
3911 struct intel_disk
*idisk
;
3913 dprintf("%s: mpb from %d:%d replaces %d:%d\n",
3914 __func__
, super
->disks
->major
,
3915 super
->disks
->minor
,
3916 table
[i
]->disks
->major
,
3917 table
[i
]->disks
->minor
);
3919 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
3920 if (idisk
&& is_failed(&idisk
->disk
))
3921 tbl_d
->status
|= FAILED_DISK
;
3924 struct intel_disk
*idisk
;
3925 struct imsm_disk
*disk
;
3927 /* tbl_mpb is more up to date, but copy
3928 * over cross generational status before
3931 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
3932 if (disk
&& is_failed(disk
))
3933 d
->status
|= FAILED_DISK
;
3935 idisk
= disk_list_get(d
->serial
, *disk_list
);
3938 if (disk
&& is_configured(disk
))
3939 idisk
->disk
.status
|= CONFIGURED_DISK
;
3942 dprintf("%s: mpb from %d:%d prefer %d:%d\n",
3943 __func__
, super
->disks
->major
,
3944 super
->disks
->minor
,
3945 table
[i
]->disks
->major
,
3946 table
[i
]->disks
->minor
);
3954 table
[tbl_size
++] = super
;
3958 /* update/extend the merged list of imsm_disk records */
3959 for (j
= 0; j
< mpb
->num_disks
; j
++) {
3960 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
3961 struct intel_disk
*idisk
;
3963 idisk
= disk_list_get(disk
->serial
, *disk_list
);
3965 idisk
->disk
.status
|= disk
->status
;
3966 if (is_configured(&idisk
->disk
) ||
3967 is_failed(&idisk
->disk
))
3968 idisk
->disk
.status
&= ~(SPARE_DISK
);
3970 idisk
= xcalloc(1, sizeof(*idisk
));
3971 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
3972 idisk
->disk
= *disk
;
3973 idisk
->next
= *disk_list
;
3977 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
3984 static struct intel_super
*
3985 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
3988 struct imsm_super
*mpb
= super
->anchor
;
3992 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3993 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3994 struct intel_disk
*idisk
;
3996 idisk
= disk_list_get(disk
->serial
, disk_list
);
3998 if (idisk
->owner
== owner
||
3999 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
4002 dprintf("%s: '%.16s' owner %d != %d\n",
4003 __func__
, disk
->serial
, idisk
->owner
,
4006 dprintf("%s: unknown disk %x [%d]: %.16s\n",
4007 __func__
, __le32_to_cpu(mpb
->family_num
), i
,
4013 if (ok_count
== mpb
->num_disks
)
4018 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
4020 struct intel_super
*s
;
4022 for (s
= super_list
; s
; s
= s
->next
) {
4023 if (family_num
!= s
->anchor
->family_num
)
4025 fprintf(stderr
, "Conflict, offlining family %#x on '%s'\n",
4026 __le32_to_cpu(family_num
), s
->disks
->devname
);
4030 static struct intel_super
*
4031 imsm_thunderdome(struct intel_super
**super_list
, int len
)
4033 struct intel_super
*super_table
[len
];
4034 struct intel_disk
*disk_list
= NULL
;
4035 struct intel_super
*champion
, *spare
;
4036 struct intel_super
*s
, **del
;
4041 memset(super_table
, 0, sizeof(super_table
));
4042 for (s
= *super_list
; s
; s
= s
->next
)
4043 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
4045 for (i
= 0; i
< tbl_size
; i
++) {
4046 struct imsm_disk
*d
;
4047 struct intel_disk
*idisk
;
4048 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
4051 d
= &s
->disks
->disk
;
4053 /* 'd' must appear in merged disk list for its
4054 * configuration to be valid
4056 idisk
= disk_list_get(d
->serial
, disk_list
);
4057 if (idisk
&& idisk
->owner
== i
)
4058 s
= validate_members(s
, disk_list
, i
);
4063 dprintf("%s: marking family: %#x from %d:%d offline\n",
4064 __func__
, mpb
->family_num
,
4065 super_table
[i
]->disks
->major
,
4066 super_table
[i
]->disks
->minor
);
4070 /* This is where the mdadm implementation differs from the Windows
4071 * driver which has no strict concept of a container. We can only
4072 * assemble one family from a container, so when returning a prodigal
4073 * array member to this system the code will not be able to disambiguate
4074 * the container contents that should be assembled ("foreign" versus
4075 * "local"). It requires user intervention to set the orig_family_num
4076 * to a new value to establish a new container. The Windows driver in
4077 * this situation fixes up the volume name in place and manages the
4078 * foreign array as an independent entity.
4083 for (i
= 0; i
< tbl_size
; i
++) {
4084 struct intel_super
*tbl_ent
= super_table
[i
];
4090 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
4095 if (s
&& !is_spare
) {
4096 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
4098 } else if (!s
&& !is_spare
)
4111 fprintf(stderr
, "Chose family %#x on '%s', "
4112 "assemble conflicts to new container with '--update=uuid'\n",
4113 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
4115 /* collect all dl's onto 'champion', and update them to
4116 * champion's version of the status
4118 for (s
= *super_list
; s
; s
= s
->next
) {
4119 struct imsm_super
*mpb
= champion
->anchor
;
4120 struct dl
*dl
= s
->disks
;
4125 mpb
->attributes
|= s
->anchor
->attributes
& MPB_ATTRIB_2TB_DISK
;
4127 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4128 struct imsm_disk
*disk
;
4130 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
4133 /* only set index on disks that are a member of
4134 * a populated contianer, i.e. one with
4137 if (is_failed(&dl
->disk
))
4139 else if (is_spare(&dl
->disk
))
4145 if (i
>= mpb
->num_disks
) {
4146 struct intel_disk
*idisk
;
4148 idisk
= disk_list_get(dl
->serial
, disk_list
);
4149 if (idisk
&& is_spare(&idisk
->disk
) &&
4150 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
4158 dl
->next
= champion
->disks
;
4159 champion
->disks
= dl
;
4163 /* delete 'champion' from super_list */
4164 for (del
= super_list
; *del
; ) {
4165 if (*del
== champion
) {
4166 *del
= (*del
)->next
;
4169 del
= &(*del
)->next
;
4171 champion
->next
= NULL
;
4175 struct intel_disk
*idisk
= disk_list
;
4177 disk_list
= disk_list
->next
;
4186 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
);
4187 static int get_super_block(struct intel_super
**super_list
, int devnum
, char *devname
,
4188 int major
, int minor
, int keep_fd
);
4190 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4191 int *max
, int keep_fd
);
4194 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
4195 char *devname
, struct md_list
*devlist
,
4198 struct intel_super
*super_list
= NULL
;
4199 struct intel_super
*super
= NULL
;
4204 /* 'fd' is an opened container */
4205 err
= get_sra_super_block(fd
, &super_list
, devname
, &i
, keep_fd
);
4207 /* get super block from devlist devices */
4208 err
= get_devlist_super_block(devlist
, &super_list
, &i
, keep_fd
);
4211 /* all mpbs enter, maybe one leaves */
4212 super
= imsm_thunderdome(&super_list
, i
);
4218 if (find_missing(super
) != 0) {
4224 /* load migration record */
4225 err
= load_imsm_migr_rec(super
, NULL
);
4227 /* migration is in progress,
4228 * but migr_rec cannot be loaded,
4234 /* Check migration compatibility */
4235 if ((err
== 0) && (check_mpb_migr_compatibility(super
) != 0)) {
4236 pr_err("Unsupported migration detected");
4238 fprintf(stderr
, " on %s\n", devname
);
4240 fprintf(stderr
, " (IMSM).\n");
4249 while (super_list
) {
4250 struct intel_super
*s
= super_list
;
4252 super_list
= super_list
->next
;
4262 st
->container_dev
= fd2devnum(fd
);
4264 st
->container_dev
= NoMdDev
;
4265 if (err
== 0 && st
->ss
== NULL
) {
4266 st
->ss
= &super_imsm
;
4267 st
->minor_version
= 0;
4268 st
->max_devs
= IMSM_MAX_DEVICES
;
4275 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4276 int *max
, int keep_fd
)
4278 struct md_list
*tmpdev
;
4282 for (i
= 0, tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
4283 if (tmpdev
->used
!= 1)
4285 if (tmpdev
->container
== 1) {
4287 int fd
= dev_open(tmpdev
->devname
, O_RDONLY
|O_EXCL
);
4289 pr_err("cannot open device %s: %s\n",
4290 tmpdev
->devname
, strerror(errno
));
4294 err
= get_sra_super_block(fd
, super_list
,
4295 tmpdev
->devname
, &lmax
,
4304 int major
= major(tmpdev
->st_rdev
);
4305 int minor
= minor(tmpdev
->st_rdev
);
4306 err
= get_super_block(super_list
,
4323 static int get_super_block(struct intel_super
**super_list
, int devnum
, char *devname
,
4324 int major
, int minor
, int keep_fd
)
4326 struct intel_super
*s
= NULL
;
4339 sprintf(nm
, "%d:%d", major
, minor
);
4340 dfd
= dev_open(nm
, O_RDWR
);
4346 rv
= find_intel_hba_capability(dfd
, s
, devname
);
4347 /* no orom/efi or non-intel hba of the disk */
4353 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4355 /* retry the load if we might have raced against mdmon */
4356 if (err
== 3 && (devnum
!= -1) && mdmon_running(devnum
))
4357 for (retry
= 0; retry
< 3; retry
++) {
4359 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4365 s
->next
= *super_list
;
4373 if ((dfd
>= 0) && (!keep_fd
))
4380 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
)
4387 sra
= sysfs_read(fd
, 0, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
4391 if (sra
->array
.major_version
!= -1 ||
4392 sra
->array
.minor_version
!= -2 ||
4393 strcmp(sra
->text_version
, "imsm") != 0) {
4398 devnum
= fd2devnum(fd
);
4399 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
4400 if (get_super_block(super_list
, devnum
, devname
,
4401 sd
->disk
.major
, sd
->disk
.minor
, keep_fd
) != 0) {
4412 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
4414 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
, NULL
, 1);
4418 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
4420 struct intel_super
*super
;
4423 if (test_partition(fd
))
4424 /* IMSM not allowed on partitions */
4427 free_super_imsm(st
);
4429 super
= alloc_super();
4430 /* Load hba and capabilities if they exist.
4431 * But do not preclude loading metadata in case capabilities or hba are
4432 * non-compliant and ignore_hw_compat is set.
4434 rv
= find_intel_hba_capability(fd
, super
, devname
);
4435 /* no orom/efi or non-intel hba of the disk */
4436 if ((rv
!= 0) && (st
->ignore_hw_compat
== 0)) {
4438 pr_err("No OROM/EFI properties for %s\n", devname
);
4442 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
4446 pr_err("Failed to load all information "
4447 "sections on %s\n", devname
);
4453 if (st
->ss
== NULL
) {
4454 st
->ss
= &super_imsm
;
4455 st
->minor_version
= 0;
4456 st
->max_devs
= IMSM_MAX_DEVICES
;
4459 /* load migration record */
4460 if (load_imsm_migr_rec(super
, NULL
) == 0) {
4461 /* Check for unsupported migration features */
4462 if (check_mpb_migr_compatibility(super
) != 0) {
4463 pr_err("Unsupported migration detected");
4465 fprintf(stderr
, " on %s\n", devname
);
4467 fprintf(stderr
, " (IMSM).\n");
4475 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
4477 if (info
->level
== 1)
4479 return info
->chunk_size
>> 9;
4482 static unsigned long long info_to_blocks_per_member(mdu_array_info_t
*info
,
4483 unsigned long long size
)
4485 if (info
->level
== 1)
4488 return (size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
4491 static void imsm_update_version_info(struct intel_super
*super
)
4493 /* update the version and attributes */
4494 struct imsm_super
*mpb
= super
->anchor
;
4496 struct imsm_dev
*dev
;
4497 struct imsm_map
*map
;
4500 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4501 dev
= get_imsm_dev(super
, i
);
4502 map
= get_imsm_map(dev
, MAP_0
);
4503 if (__le32_to_cpu(dev
->size_high
) > 0)
4504 mpb
->attributes
|= MPB_ATTRIB_2TB
;
4506 /* FIXME detect when an array spans a port multiplier */
4508 mpb
->attributes
|= MPB_ATTRIB_PM
;
4511 if (mpb
->num_raid_devs
> 1 ||
4512 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
4513 version
= MPB_VERSION_ATTRIBS
;
4514 switch (get_imsm_raid_level(map
)) {
4515 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
4516 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
4517 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
4518 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
4521 if (map
->num_members
>= 5)
4522 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
4523 else if (dev
->status
== DEV_CLONE_N_GO
)
4524 version
= MPB_VERSION_CNG
;
4525 else if (get_imsm_raid_level(map
) == 5)
4526 version
= MPB_VERSION_RAID5
;
4527 else if (map
->num_members
>= 3)
4528 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
4529 else if (get_imsm_raid_level(map
) == 1)
4530 version
= MPB_VERSION_RAID1
;
4532 version
= MPB_VERSION_RAID0
;
4534 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
4538 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
4540 struct imsm_super
*mpb
= super
->anchor
;
4541 char *reason
= NULL
;
4544 if (strlen(name
) > MAX_RAID_SERIAL_LEN
)
4545 reason
= "must be 16 characters or less";
4547 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4548 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4550 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
4551 reason
= "already exists";
4556 if (reason
&& !quiet
)
4557 pr_err("imsm volume name %s\n", reason
);
4562 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
4563 unsigned long long size
, char *name
,
4564 char *homehost
, int *uuid
,
4565 long long data_offset
)
4567 /* We are creating a volume inside a pre-existing container.
4568 * so st->sb is already set.
4570 struct intel_super
*super
= st
->sb
;
4571 struct imsm_super
*mpb
= super
->anchor
;
4572 struct intel_dev
*dv
;
4573 struct imsm_dev
*dev
;
4574 struct imsm_vol
*vol
;
4575 struct imsm_map
*map
;
4576 int idx
= mpb
->num_raid_devs
;
4578 unsigned long long array_blocks
;
4579 size_t size_old
, size_new
;
4580 unsigned long long num_data_stripes
;
4582 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
4583 pr_err("This imsm-container already has the "
4584 "maximum of %d volumes\n", super
->orom
->vpa
);
4588 /* ensure the mpb is large enough for the new data */
4589 size_old
= __le32_to_cpu(mpb
->mpb_size
);
4590 size_new
= disks_to_mpb_size(info
->nr_disks
);
4591 if (size_new
> size_old
) {
4593 size_t size_round
= ROUND_UP(size_new
, 512);
4595 if (posix_memalign(&mpb_new
, 512, size_round
) != 0) {
4596 pr_err("could not allocate new mpb\n");
4599 if (posix_memalign(&super
->migr_rec_buf
, 512,
4600 MIGR_REC_BUF_SIZE
) != 0) {
4601 pr_err("%s could not allocate migr_rec buffer\n",
4608 memcpy(mpb_new
, mpb
, size_old
);
4611 super
->anchor
= mpb_new
;
4612 mpb
->mpb_size
= __cpu_to_le32(size_new
);
4613 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
4615 super
->current_vol
= idx
;
4617 /* handle 'failed_disks' by either:
4618 * a) create dummy disk entries in the table if this the first
4619 * volume in the array. We add them here as this is the only
4620 * opportunity to add them. add_to_super_imsm_volume()
4621 * handles the non-failed disks and continues incrementing
4623 * b) validate that 'failed_disks' matches the current number
4624 * of missing disks if the container is populated
4626 if (super
->current_vol
== 0) {
4628 for (i
= 0; i
< info
->failed_disks
; i
++) {
4629 struct imsm_disk
*disk
;
4632 disk
= __get_imsm_disk(mpb
, i
);
4633 disk
->status
= CONFIGURED_DISK
| FAILED_DISK
;
4634 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
4635 snprintf((char *) disk
->serial
, MAX_RAID_SERIAL_LEN
,
4638 find_missing(super
);
4643 for (d
= super
->missing
; d
; d
= d
->next
)
4645 if (info
->failed_disks
> missing
) {
4646 pr_err("unable to add 'missing' disk to container\n");
4651 if (!check_name(super
, name
, 0))
4653 dv
= xmalloc(sizeof(*dv
));
4654 dev
= xcalloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
4655 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
4656 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
4657 info
->layout
, info
->chunk_size
,
4659 /* round array size down to closest MB */
4660 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
4662 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
4663 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
4664 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
4666 vol
->migr_state
= 0;
4667 set_migr_type(dev
, MIGR_INIT
);
4668 vol
->dirty
= !info
->state
;
4669 vol
->curr_migr_unit
= 0;
4670 map
= get_imsm_map(dev
, MAP_0
);
4671 set_pba_of_lba0(map
, super
->create_offset
);
4672 set_blocks_per_member(map
, info_to_blocks_per_member(info
, size
));
4673 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
4674 map
->failed_disk_num
= ~0;
4675 if (info
->level
> 0)
4676 map
->map_state
= (info
->state
? IMSM_T_STATE_NORMAL
4677 : IMSM_T_STATE_UNINITIALIZED
);
4679 map
->map_state
= info
->failed_disks
? IMSM_T_STATE_FAILED
:
4680 IMSM_T_STATE_NORMAL
;
4683 if (info
->level
== 1 && info
->raid_disks
> 2) {
4686 pr_err("imsm does not support more than 2 disks"
4687 "in a raid1 volume\n");
4691 map
->raid_level
= info
->level
;
4692 if (info
->level
== 10) {
4693 map
->raid_level
= 1;
4694 map
->num_domains
= info
->raid_disks
/ 2;
4695 } else if (info
->level
== 1)
4696 map
->num_domains
= info
->raid_disks
;
4698 map
->num_domains
= 1;
4700 /* info->size is only int so use the 'size' parameter instead */
4701 num_data_stripes
= (size
* 2) / info_to_blocks_per_strip(info
);
4702 num_data_stripes
/= map
->num_domains
;
4703 set_num_data_stripes(map
, num_data_stripes
);
4705 map
->num_members
= info
->raid_disks
;
4706 for (i
= 0; i
< map
->num_members
; i
++) {
4707 /* initialized in add_to_super */
4708 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
4710 mpb
->num_raid_devs
++;
4713 dv
->index
= super
->current_vol
;
4714 dv
->next
= super
->devlist
;
4715 super
->devlist
= dv
;
4717 imsm_update_version_info(super
);
4722 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
4723 unsigned long long size
, char *name
,
4724 char *homehost
, int *uuid
,
4725 unsigned long long data_offset
)
4727 /* This is primarily called by Create when creating a new array.
4728 * We will then get add_to_super called for each component, and then
4729 * write_init_super called to write it out to each device.
4730 * For IMSM, Create can create on fresh devices or on a pre-existing
4732 * To create on a pre-existing array a different method will be called.
4733 * This one is just for fresh drives.
4735 struct intel_super
*super
;
4736 struct imsm_super
*mpb
;
4740 if (data_offset
!= INVALID_SECTORS
) {
4741 fprintf(stderr
, Name
": data-offset not supported by imsm\n");
4746 return init_super_imsm_volume(st
, info
, size
, name
, homehost
, uuid
,
4750 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
4754 super
= alloc_super();
4755 if (super
&& posix_memalign(&super
->buf
, 512, mpb_size
) != 0) {
4760 pr_err("%s could not allocate superblock\n", __func__
);
4763 if (posix_memalign(&super
->migr_rec_buf
, 512, MIGR_REC_BUF_SIZE
) != 0) {
4764 pr_err("%s could not allocate migr_rec buffer\n", __func__
);
4769 memset(super
->buf
, 0, mpb_size
);
4771 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
4775 /* zeroing superblock */
4779 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
4781 version
= (char *) mpb
->sig
;
4782 strcpy(version
, MPB_SIGNATURE
);
4783 version
+= strlen(MPB_SIGNATURE
);
4784 strcpy(version
, MPB_VERSION_RAID0
);
4790 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
4791 int fd
, char *devname
)
4793 struct intel_super
*super
= st
->sb
;
4794 struct imsm_super
*mpb
= super
->anchor
;
4795 struct imsm_disk
*_disk
;
4796 struct imsm_dev
*dev
;
4797 struct imsm_map
*map
;
4801 dev
= get_imsm_dev(super
, super
->current_vol
);
4802 map
= get_imsm_map(dev
, MAP_0
);
4804 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
4805 pr_err("%s: Cannot add spare devices to IMSM volume\n",
4811 /* we're doing autolayout so grab the pre-marked (in
4812 * validate_geometry) raid_disk
4814 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4815 if (dl
->raiddisk
== dk
->raid_disk
)
4818 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4819 if (dl
->major
== dk
->major
&&
4820 dl
->minor
== dk
->minor
)
4825 pr_err("%s is not a member of the same container\n", devname
);
4829 /* add a pristine spare to the metadata */
4830 if (dl
->index
< 0) {
4831 dl
->index
= super
->anchor
->num_disks
;
4832 super
->anchor
->num_disks
++;
4834 /* Check the device has not already been added */
4835 slot
= get_imsm_disk_slot(map
, dl
->index
);
4837 (get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
) & IMSM_ORD_REBUILD
) == 0) {
4838 pr_err("%s has been included in this array twice\n",
4842 set_imsm_ord_tbl_ent(map
, dk
->raid_disk
, dl
->index
);
4843 dl
->disk
.status
= CONFIGURED_DISK
;
4845 /* update size of 'missing' disks to be at least as large as the
4846 * largest acitve member (we only have dummy missing disks when
4847 * creating the first volume)
4849 if (super
->current_vol
== 0) {
4850 for (df
= super
->missing
; df
; df
= df
->next
) {
4851 if (total_blocks(&dl
->disk
) > total_blocks(&df
->disk
))
4852 set_total_blocks(&df
->disk
, total_blocks(&dl
->disk
));
4853 _disk
= __get_imsm_disk(mpb
, df
->index
);
4858 /* refresh unset/failed slots to point to valid 'missing' entries */
4859 for (df
= super
->missing
; df
; df
= df
->next
)
4860 for (slot
= 0; slot
< mpb
->num_disks
; slot
++) {
4861 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
4863 if ((ord
& IMSM_ORD_REBUILD
) == 0)
4865 set_imsm_ord_tbl_ent(map
, slot
, df
->index
| IMSM_ORD_REBUILD
);
4866 if (is_gen_migration(dev
)) {
4867 struct imsm_map
*map2
= get_imsm_map(dev
,
4869 int slot2
= get_imsm_disk_slot(map2
, df
->index
);
4870 if ((slot2
< map2
->num_members
) &&
4872 __u32 ord2
= get_imsm_ord_tbl_ent(dev
,
4875 if ((unsigned)df
->index
==
4877 set_imsm_ord_tbl_ent(map2
,
4883 dprintf("set slot:%d to missing disk:%d\n", slot
, df
->index
);
4887 /* if we are creating the first raid device update the family number */
4888 if (super
->current_vol
== 0) {
4890 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
4892 _disk
= __get_imsm_disk(mpb
, dl
->index
);
4893 if (!_dev
|| !_disk
) {
4894 pr_err("BUG mpb setup error\n");
4900 sum
+= __gen_imsm_checksum(mpb
);
4901 mpb
->family_num
= __cpu_to_le32(sum
);
4902 mpb
->orig_family_num
= mpb
->family_num
;
4904 super
->current_disk
= dl
;
4909 * Function marks disk as spare and restores disk serial
4910 * in case it was previously marked as failed by takeover operation
4912 * -1 : critical error
4913 * 0 : disk is marked as spare but serial is not set
4916 int mark_spare(struct dl
*disk
)
4918 __u8 serial
[MAX_RAID_SERIAL_LEN
];
4925 if (!imsm_read_serial(disk
->fd
, NULL
, serial
)) {
4926 /* Restore disk serial number, because takeover marks disk
4927 * as failed and adds to serial ':0' before it becomes
4930 serialcpy(disk
->serial
, serial
);
4931 serialcpy(disk
->disk
.serial
, serial
);
4934 disk
->disk
.status
= SPARE_DISK
;
4940 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
4941 int fd
, char *devname
,
4942 unsigned long long data_offset
)
4944 struct intel_super
*super
= st
->sb
;
4946 unsigned long long size
;
4951 /* If we are on an RAID enabled platform check that the disk is
4952 * attached to the raid controller.
4953 * We do not need to test disks attachment for container based additions,
4954 * they shall be already tested when container was created/assembled.
4956 rv
= find_intel_hba_capability(fd
, super
, devname
);
4957 /* no orom/efi or non-intel hba of the disk */
4959 dprintf("capability: %p fd: %d ret: %d\n",
4960 super
->orom
, fd
, rv
);
4964 if (super
->current_vol
>= 0)
4965 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
4968 dd
= xcalloc(sizeof(*dd
), 1);
4969 dd
->major
= major(stb
.st_rdev
);
4970 dd
->minor
= minor(stb
.st_rdev
);
4971 dd
->devname
= devname
? xstrdup(devname
) : NULL
;
4974 dd
->action
= DISK_ADD
;
4975 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
4977 pr_err("failed to retrieve scsi serial, aborting\n");
4982 get_dev_size(fd
, NULL
, &size
);
4984 serialcpy(dd
->disk
.serial
, dd
->serial
);
4985 set_total_blocks(&dd
->disk
, size
);
4986 if (__le32_to_cpu(dd
->disk
.total_blocks_hi
) > 0) {
4987 struct imsm_super
*mpb
= super
->anchor
;
4988 mpb
->attributes
|= MPB_ATTRIB_2TB_DISK
;
4991 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
4992 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
4994 dd
->disk
.scsi_id
= __cpu_to_le32(0);
4996 if (st
->update_tail
) {
4997 dd
->next
= super
->disk_mgmt_list
;
4998 super
->disk_mgmt_list
= dd
;
5000 dd
->next
= super
->disks
;
5002 super
->updates_pending
++;
5009 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
5011 struct intel_super
*super
= st
->sb
;
5014 /* remove from super works only in mdmon - for communication
5015 * manager - monitor. Check if communication memory buffer
5018 if (!st
->update_tail
) {
5019 pr_err("%s shall be used in mdmon context only"
5020 "(line %d).\n", __func__
, __LINE__
);
5023 dd
= xcalloc(1, sizeof(*dd
));
5024 dd
->major
= dk
->major
;
5025 dd
->minor
= dk
->minor
;
5028 dd
->action
= DISK_REMOVE
;
5030 dd
->next
= super
->disk_mgmt_list
;
5031 super
->disk_mgmt_list
= dd
;
5037 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
5041 struct imsm_super anchor
;
5042 } spare_record
__attribute__ ((aligned(512)));
5044 /* spare records have their own family number and do not have any defined raid
5047 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
5049 struct imsm_super
*mpb
= super
->anchor
;
5050 struct imsm_super
*spare
= &spare_record
.anchor
;
5054 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
)),
5055 spare
->generation_num
= __cpu_to_le32(1UL),
5056 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5057 spare
->num_disks
= 1,
5058 spare
->num_raid_devs
= 0,
5059 spare
->cache_size
= mpb
->cache_size
,
5060 spare
->pwr_cycle_count
= __cpu_to_le32(1),
5062 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
5063 MPB_SIGNATURE MPB_VERSION_RAID0
);
5065 for (d
= super
->disks
; d
; d
= d
->next
) {
5069 spare
->disk
[0] = d
->disk
;
5070 if (__le32_to_cpu(d
->disk
.total_blocks_hi
) > 0)
5071 spare
->attributes
|= MPB_ATTRIB_2TB_DISK
;
5073 sum
= __gen_imsm_checksum(spare
);
5074 spare
->family_num
= __cpu_to_le32(sum
);
5075 spare
->orig_family_num
= 0;
5076 sum
= __gen_imsm_checksum(spare
);
5077 spare
->check_sum
= __cpu_to_le32(sum
);
5079 if (store_imsm_mpb(d
->fd
, spare
)) {
5080 fprintf(stderr
, "%s: failed for device %d:%d %s\n",
5081 __func__
, d
->major
, d
->minor
, strerror(errno
));
5093 static int write_super_imsm(struct supertype
*st
, int doclose
)
5095 struct intel_super
*super
= st
->sb
;
5096 struct imsm_super
*mpb
= super
->anchor
;
5102 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
5104 int clear_migration_record
= 1;
5106 /* 'generation' is incremented everytime the metadata is written */
5107 generation
= __le32_to_cpu(mpb
->generation_num
);
5109 mpb
->generation_num
= __cpu_to_le32(generation
);
5111 /* fix up cases where previous mdadm releases failed to set
5114 if (mpb
->orig_family_num
== 0)
5115 mpb
->orig_family_num
= mpb
->family_num
;
5117 for (d
= super
->disks
; d
; d
= d
->next
) {
5121 mpb
->disk
[d
->index
] = d
->disk
;
5125 for (d
= super
->missing
; d
; d
= d
->next
) {
5126 mpb
->disk
[d
->index
] = d
->disk
;
5129 mpb
->num_disks
= num_disks
;
5130 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
5132 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5133 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
5134 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
5136 imsm_copy_dev(dev
, dev2
);
5137 mpb_size
+= sizeof_imsm_dev(dev
, 0);
5139 if (is_gen_migration(dev2
))
5140 clear_migration_record
= 0;
5142 mpb_size
+= __le32_to_cpu(mpb
->bbm_log_size
);
5143 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5145 /* recalculate checksum */
5146 sum
= __gen_imsm_checksum(mpb
);
5147 mpb
->check_sum
= __cpu_to_le32(sum
);
5149 if (super
->clean_migration_record_by_mdmon
) {
5150 clear_migration_record
= 1;
5151 super
->clean_migration_record_by_mdmon
= 0;
5153 if (clear_migration_record
)
5154 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SIZE
);
5156 /* write the mpb for disks that compose raid devices */
5157 for (d
= super
->disks
; d
; d
= d
->next
) {
5158 if (d
->index
< 0 || is_failed(&d
->disk
))
5161 if (clear_migration_record
) {
5162 unsigned long long dsize
;
5164 get_dev_size(d
->fd
, NULL
, &dsize
);
5165 if (lseek64(d
->fd
, dsize
- 512, SEEK_SET
) >= 0) {
5166 if (write(d
->fd
, super
->migr_rec_buf
,
5167 MIGR_REC_BUF_SIZE
) != MIGR_REC_BUF_SIZE
)
5168 perror("Write migr_rec failed");
5172 if (store_imsm_mpb(d
->fd
, mpb
))
5174 "%s: failed for device %d:%d (fd: %d)%s\n",
5175 __func__
, d
->major
, d
->minor
,
5176 d
->fd
, strerror(errno
));
5185 return write_super_imsm_spares(super
, doclose
);
5191 static int create_array(struct supertype
*st
, int dev_idx
)
5194 struct imsm_update_create_array
*u
;
5195 struct intel_super
*super
= st
->sb
;
5196 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
5197 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
5198 struct disk_info
*inf
;
5199 struct imsm_disk
*disk
;
5202 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
5203 sizeof(*inf
) * map
->num_members
;
5205 u
->type
= update_create_array
;
5206 u
->dev_idx
= dev_idx
;
5207 imsm_copy_dev(&u
->dev
, dev
);
5208 inf
= get_disk_info(u
);
5209 for (i
= 0; i
< map
->num_members
; i
++) {
5210 int idx
= get_imsm_disk_idx(dev
, i
, MAP_X
);
5212 disk
= get_imsm_disk(super
, idx
);
5213 serialcpy(inf
[i
].serial
, disk
->serial
);
5215 append_metadata_update(st
, u
, len
);
5220 static int mgmt_disk(struct supertype
*st
)
5222 struct intel_super
*super
= st
->sb
;
5224 struct imsm_update_add_remove_disk
*u
;
5226 if (!super
->disk_mgmt_list
)
5231 u
->type
= update_add_remove_disk
;
5232 append_metadata_update(st
, u
, len
);
5237 static int write_init_super_imsm(struct supertype
*st
)
5239 struct intel_super
*super
= st
->sb
;
5240 int current_vol
= super
->current_vol
;
5242 /* we are done with current_vol reset it to point st at the container */
5243 super
->current_vol
= -1;
5245 if (st
->update_tail
) {
5246 /* queue the recently created array / added disk
5247 * as a metadata update */
5250 /* determine if we are creating a volume or adding a disk */
5251 if (current_vol
< 0) {
5252 /* in the mgmt (add/remove) disk case we are running
5253 * in mdmon context, so don't close fd's
5255 return mgmt_disk(st
);
5257 rv
= create_array(st
, current_vol
);
5262 for (d
= super
->disks
; d
; d
= d
->next
)
5263 Kill(d
->devname
, NULL
, 0, -1, 1);
5264 return write_super_imsm(st
, 1);
5269 static int store_super_imsm(struct supertype
*st
, int fd
)
5271 struct intel_super
*super
= st
->sb
;
5272 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
5278 return store_imsm_mpb(fd
, mpb
);
5284 static int imsm_bbm_log_size(struct imsm_super
*mpb
)
5286 return __le32_to_cpu(mpb
->bbm_log_size
);
5290 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
5291 int layout
, int raiddisks
, int chunk
,
5292 unsigned long long size
,
5293 unsigned long long data_offset
,
5295 unsigned long long *freesize
,
5299 unsigned long long ldsize
;
5300 struct intel_super
*super
=NULL
;
5303 if (level
!= LEVEL_CONTAINER
)
5308 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
5311 pr_err("imsm: Cannot open %s: %s\n",
5312 dev
, strerror(errno
));
5315 if (!get_dev_size(fd
, dev
, &ldsize
)) {
5320 /* capabilities retrieve could be possible
5321 * note that there is no fd for the disks in array.
5323 super
= alloc_super();
5324 rv
= find_intel_hba_capability(fd
, super
, verbose
> 0 ? dev
: NULL
);
5328 fd2devname(fd
, str
);
5329 dprintf("validate_geometry_imsm_container: fd: %d %s orom: %p rv: %d raiddisk: %d\n",
5330 fd
, str
, super
->orom
, rv
, raiddisks
);
5332 /* no orom/efi or non-intel hba of the disk */
5339 if (raiddisks
> super
->orom
->tds
) {
5341 pr_err("%d exceeds maximum number of"
5342 " platform supported disks: %d\n",
5343 raiddisks
, super
->orom
->tds
);
5347 if ((super
->orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) == 0 &&
5348 (ldsize
>> 9) >> 32 > 0) {
5350 pr_err("%s exceeds maximum platform supported size\n", dev
);
5356 *freesize
= avail_size_imsm(st
, ldsize
>> 9, data_offset
);
5362 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
5364 const unsigned long long base_start
= e
[*idx
].start
;
5365 unsigned long long end
= base_start
+ e
[*idx
].size
;
5368 if (base_start
== end
)
5372 for (i
= *idx
; i
< num_extents
; i
++) {
5373 /* extend overlapping extents */
5374 if (e
[i
].start
>= base_start
&&
5375 e
[i
].start
<= end
) {
5378 if (e
[i
].start
+ e
[i
].size
> end
)
5379 end
= e
[i
].start
+ e
[i
].size
;
5380 } else if (e
[i
].start
> end
) {
5386 return end
- base_start
;
5389 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
5391 /* build a composite disk with all known extents and generate a new
5392 * 'maxsize' given the "all disks in an array must share a common start
5393 * offset" constraint
5395 struct extent
*e
= xcalloc(sum_extents
, sizeof(*e
));
5399 unsigned long long pos
;
5400 unsigned long long start
= 0;
5401 unsigned long long maxsize
;
5402 unsigned long reserve
;
5404 /* coalesce and sort all extents. also, check to see if we need to
5405 * reserve space between member arrays
5408 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5411 for (i
= 0; i
< dl
->extent_cnt
; i
++)
5414 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
5419 while (i
< sum_extents
) {
5420 e
[j
].start
= e
[i
].start
;
5421 e
[j
].size
= find_size(e
, &i
, sum_extents
);
5423 if (e
[j
-1].size
== 0)
5432 unsigned long long esize
;
5434 esize
= e
[i
].start
- pos
;
5435 if (esize
>= maxsize
) {
5440 pos
= e
[i
].start
+ e
[i
].size
;
5442 } while (e
[i
-1].size
);
5448 /* FIXME assumes volume at offset 0 is the first volume in a
5451 if (start_extent
> 0)
5452 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
5456 if (maxsize
< reserve
)
5459 super
->create_offset
= ~((unsigned long long) 0);
5460 if (start
+ reserve
> super
->create_offset
)
5461 return 0; /* start overflows create_offset */
5462 super
->create_offset
= start
+ reserve
;
5464 return maxsize
- reserve
;
5467 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
5469 if (level
< 0 || level
== 6 || level
== 4)
5472 /* if we have an orom prevent invalid raid levels */
5475 case 0: return imsm_orom_has_raid0(orom
);
5478 return imsm_orom_has_raid1e(orom
);
5479 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
5480 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
5481 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
5484 return 1; /* not on an Intel RAID platform so anything goes */
5491 active_arrays_by_format(char *name
, char* hba
, struct md_list
**devlist
,
5492 int dpa
, int verbose
)
5494 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
5495 struct mdstat_ent
*memb
= NULL
;
5498 struct md_list
*dv
= NULL
;
5501 for (memb
= mdstat
; memb
; memb
= memb
->next
) {
5502 if (memb
->metadata_version
&&
5503 (strncmp(memb
->metadata_version
, "external:", 9) == 0) &&
5504 (strcmp(&memb
->metadata_version
[9], name
) == 0) &&
5505 !is_subarray(memb
->metadata_version
+9) &&
5507 struct dev_member
*dev
= memb
->members
;
5509 while(dev
&& (fd
< 0)) {
5510 char *path
= xmalloc(strlen(dev
->name
) + strlen("/dev/") + 1);
5511 num
= sprintf(path
, "%s%s", "/dev/", dev
->name
);
5513 fd
= open(path
, O_RDONLY
, 0);
5514 if ((num
<= 0) || (fd
< 0)) {
5515 pr_vrb(": Cannot open %s: %s\n",
5516 dev
->name
, strerror(errno
));
5522 if ((fd
>= 0) && disk_attached_to_hba(fd
, hba
)) {
5523 struct mdstat_ent
*vol
;
5524 for (vol
= mdstat
; vol
; vol
= vol
->next
) {
5525 if ((vol
->active
> 0) &&
5526 vol
->metadata_version
&&
5527 is_container_member(vol
, memb
->dev
)) {
5532 if (*devlist
&& (found
< dpa
)) {
5533 dv
= xcalloc(1, sizeof(*dv
));
5534 dv
->devname
= xmalloc(strlen(memb
->dev
) + strlen("/dev/") + 1);
5535 sprintf(dv
->devname
, "%s%s", "/dev/", memb
->dev
);
5538 dv
->next
= *devlist
;
5546 free_mdstat(mdstat
);
5551 static struct md_list
*
5552 get_loop_devices(void)
5555 struct md_list
*devlist
= NULL
;
5556 struct md_list
*dv
= NULL
;
5558 for(i
= 0; i
< 12; i
++) {
5559 dv
= xcalloc(1, sizeof(*dv
));
5560 dv
->devname
= xmalloc(40);
5561 sprintf(dv
->devname
, "/dev/loop%d", i
);
5569 static struct md_list
*
5570 get_devices(const char *hba_path
)
5572 struct md_list
*devlist
= NULL
;
5573 struct md_list
*dv
= NULL
;
5579 devlist
= get_loop_devices();
5582 /* scroll through /sys/dev/block looking for devices attached to
5585 dir
= opendir("/sys/dev/block");
5586 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
5591 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
5593 path
= devt_to_devpath(makedev(major
, minor
));
5596 if (!path_attached_to_hba(path
, hba_path
)) {
5603 fd
= dev_open(ent
->d_name
, O_RDONLY
);
5605 fd2devname(fd
, buf
);
5608 pr_err("cannot open device: %s\n",
5614 dv
= xcalloc(1, sizeof(*dv
));
5615 dv
->devname
= xstrdup(buf
);
5622 devlist
= devlist
->next
;
5632 count_volumes_list(struct md_list
*devlist
, char *homehost
,
5633 int verbose
, int *found
)
5635 struct md_list
*tmpdev
;
5637 struct supertype
*st
= NULL
;
5639 /* first walk the list of devices to find a consistent set
5640 * that match the criterea, if that is possible.
5641 * We flag the ones we like with 'used'.
5644 st
= match_metadata_desc_imsm("imsm");
5646 pr_vrb(": cannot allocate memory for imsm supertype\n");
5650 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5651 char *devname
= tmpdev
->devname
;
5653 struct supertype
*tst
;
5655 if (tmpdev
->used
> 1)
5657 tst
= dup_super(st
);
5659 pr_vrb(": cannot allocate memory for imsm supertype\n");
5662 tmpdev
->container
= 0;
5663 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
5665 dprintf(": cannot open device %s: %s\n",
5666 devname
, strerror(errno
));
5668 } else if (fstat(dfd
, &stb
)< 0) {
5670 dprintf(": fstat failed for %s: %s\n",
5671 devname
, strerror(errno
));
5673 } else if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
5674 dprintf(": %s is not a block device.\n",
5677 } else if (must_be_container(dfd
)) {
5678 struct supertype
*cst
;
5679 cst
= super_by_fd(dfd
, NULL
);
5681 dprintf(": cannot recognize container type %s\n",
5684 } else if (tst
->ss
!= st
->ss
) {
5685 dprintf(": non-imsm container - ignore it: %s\n",
5688 } else if (!tst
->ss
->load_container
||
5689 tst
->ss
->load_container(tst
, dfd
, NULL
))
5692 tmpdev
->container
= 1;
5695 cst
->ss
->free_super(cst
);
5697 tmpdev
->st_rdev
= stb
.st_rdev
;
5698 if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
5699 dprintf(": no RAID superblock on %s\n",
5702 } else if (tst
->ss
->compare_super
== NULL
) {
5703 dprintf(": Cannot assemble %s metadata on %s\n",
5704 tst
->ss
->name
, devname
);
5710 if (tmpdev
->used
== 2 || tmpdev
->used
== 4) {
5711 /* Ignore unrecognised devices during auto-assembly */
5716 tst
->ss
->getinfo_super(tst
, &info
, NULL
);
5718 if (st
->minor_version
== -1)
5719 st
->minor_version
= tst
->minor_version
;
5721 if (memcmp(info
.uuid
, uuid_zero
,
5722 sizeof(int[4])) == 0) {
5723 /* this is a floating spare. It cannot define
5724 * an array unless there are no more arrays of
5725 * this type to be found. It can be included
5726 * in an array of this type though.
5732 if (st
->ss
!= tst
->ss
||
5733 st
->minor_version
!= tst
->minor_version
||
5734 st
->ss
->compare_super(st
, tst
) != 0) {
5735 /* Some mismatch. If exactly one array matches this host,
5736 * we can resolve on that one.
5737 * Or, if we are auto assembling, we just ignore the second
5740 dprintf(": superblock on %s doesn't match others - assembly aborted\n",
5746 dprintf("found: devname: %s\n", devname
);
5750 tst
->ss
->free_super(tst
);
5754 if ((err
= load_super_imsm_all(st
, -1, &st
->sb
, NULL
, devlist
, 0)) == 0) {
5755 struct mdinfo
*iter
, *head
= st
->ss
->container_content(st
, NULL
);
5756 for (iter
= head
; iter
; iter
= iter
->next
) {
5757 dprintf("content->text_version: %s vol\n",
5758 iter
->text_version
);
5759 if (iter
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
5760 /* do not assemble arrays with unsupported
5762 dprintf(": Cannot activate member %s.\n",
5763 iter
->text_version
);
5770 dprintf(" no valid super block on device list: err: %d %p\n",
5774 dprintf(" no more devices to examin\n");
5777 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5778 if ((tmpdev
->used
== 1) && (tmpdev
->found
)) {
5780 if (count
< tmpdev
->found
)
5783 count
-= tmpdev
->found
;
5786 if (tmpdev
->used
== 1)
5791 st
->ss
->free_super(st
);
5797 count_volumes(char *hba
, int dpa
, int verbose
)
5799 struct md_list
*devlist
= NULL
;
5803 devlist
= get_devices(hba
);
5804 /* if no intel devices return zero volumes */
5805 if (devlist
== NULL
)
5808 count
= active_arrays_by_format("imsm", hba
, &devlist
, dpa
, verbose
);
5809 dprintf(" path: %s active arrays: %d\n", hba
, count
);
5810 if (devlist
== NULL
)
5814 count
+= count_volumes_list(devlist
,
5818 dprintf("found %d count: %d\n", found
, count
);
5821 dprintf("path: %s total number of volumes: %d\n", hba
, count
);
5824 struct md_list
*dv
= devlist
;
5825 devlist
= devlist
->next
;
5832 static int imsm_default_chunk(const struct imsm_orom
*orom
)
5834 /* up to 512 if the plaform supports it, otherwise the platform max.
5835 * 128 if no platform detected
5837 int fs
= max(7, orom
? fls(orom
->sss
) : 0);
5839 return min(512, (1 << fs
));
5843 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
5844 int raiddisks
, int *chunk
, unsigned long long size
, int verbose
)
5846 /* check/set platform and metadata limits/defaults */
5847 if (super
->orom
&& raiddisks
> super
->orom
->dpa
) {
5848 pr_vrb(": platform supports a maximum of %d disks per array\n",
5853 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
5854 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
5855 pr_vrb(": platform does not support raid%d with %d disk%s\n",
5856 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
5860 if (chunk
&& (*chunk
== 0 || *chunk
== UnSet
))
5861 *chunk
= imsm_default_chunk(super
->orom
);
5863 if (super
->orom
&& chunk
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
5864 pr_vrb(": platform does not support a chunk size of: "
5869 if (layout
!= imsm_level_to_layout(level
)) {
5871 pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
5872 else if (level
== 10)
5873 pr_vrb(": imsm raid 10 only supports the n2 layout\n");
5875 pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
5880 if (super
->orom
&& (super
->orom
->attr
& IMSM_OROM_ATTR_2TB
) == 0 && chunk
&&
5881 (calc_array_size(level
, raiddisks
, layout
, *chunk
, size
) >> 32) > 0) {
5882 pr_vrb(": platform does not support a volume size over 2TB\n");
5888 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
5889 * FIX ME add ahci details
5891 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
5892 int layout
, int raiddisks
, int *chunk
,
5893 unsigned long long size
,
5894 unsigned long long data_offset
,
5896 unsigned long long *freesize
,
5900 struct intel_super
*super
= st
->sb
;
5901 struct imsm_super
*mpb
;
5903 unsigned long long pos
= 0;
5904 unsigned long long maxsize
;
5908 /* We must have the container info already read in. */
5912 mpb
= super
->anchor
;
5914 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, size
, verbose
)) {
5915 pr_err("RAID gemetry validation failed. "
5916 "Cannot proceed with the action(s).\n");
5920 /* General test: make sure there is space for
5921 * 'raiddisks' device extents of size 'size' at a given
5924 unsigned long long minsize
= size
;
5925 unsigned long long start_offset
= MaxSector
;
5928 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
5929 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5934 e
= get_extents(super
, dl
);
5937 unsigned long long esize
;
5938 esize
= e
[i
].start
- pos
;
5939 if (esize
>= minsize
)
5941 if (found
&& start_offset
== MaxSector
) {
5944 } else if (found
&& pos
!= start_offset
) {
5948 pos
= e
[i
].start
+ e
[i
].size
;
5950 } while (e
[i
-1].size
);
5955 if (dcnt
< raiddisks
) {
5957 pr_err("imsm: Not enough "
5958 "devices with space for this array "
5966 /* This device must be a member of the set */
5967 if (stat(dev
, &stb
) < 0)
5969 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
5971 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5972 if (dl
->major
== (int)major(stb
.st_rdev
) &&
5973 dl
->minor
== (int)minor(stb
.st_rdev
))
5978 pr_err("%s is not in the "
5979 "same imsm set\n", dev
);
5981 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
5982 /* If a volume is present then the current creation attempt
5983 * cannot incorporate new spares because the orom may not
5984 * understand this configuration (all member disks must be
5985 * members of each array in the container).
5987 pr_err("%s is a spare and a volume"
5988 " is already defined for this container\n", dev
);
5989 pr_err("The option-rom requires all member"
5990 " disks to be a member of all volumes\n");
5992 } else if (super
->orom
&& mpb
->num_raid_devs
> 0 &&
5993 mpb
->num_disks
!= raiddisks
) {
5994 pr_err("The option-rom requires all member"
5995 " disks to be a member of all volumes\n");
5999 /* retrieve the largest free space block */
6000 e
= get_extents(super
, dl
);
6005 unsigned long long esize
;
6007 esize
= e
[i
].start
- pos
;
6008 if (esize
>= maxsize
)
6010 pos
= e
[i
].start
+ e
[i
].size
;
6012 } while (e
[i
-1].size
);
6017 pr_err("unable to determine free space for: %s\n",
6021 if (maxsize
< size
) {
6023 pr_err("%s not enough space (%llu < %llu)\n",
6024 dev
, maxsize
, size
);
6028 /* count total number of extents for merge */
6030 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6032 i
+= dl
->extent_cnt
;
6034 maxsize
= merge_extents(super
, i
);
6036 if (!check_env("IMSM_NO_PLATFORM") &&
6037 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6038 pr_err("attempting to create a second "
6039 "volume with size less then remaining space. "
6044 if (maxsize
< size
|| maxsize
== 0) {
6047 pr_err("no free space"
6048 " left on device. Aborting...\n");
6050 pr_err("not enough space"
6051 " to create volume of given size"
6052 " (%llu < %llu). Aborting...\n",
6058 *freesize
= maxsize
;
6061 int count
= count_volumes(super
->hba
->path
,
6062 super
->orom
->dpa
, verbose
);
6063 if (super
->orom
->vphba
<= count
) {
6064 pr_vrb(": platform does not support more than %d raid volumes.\n",
6065 super
->orom
->vphba
);
6072 static int imsm_get_free_size(struct supertype
*st
, int raiddisks
,
6073 unsigned long long size
, int chunk
,
6074 unsigned long long *freesize
)
6076 struct intel_super
*super
= st
->sb
;
6077 struct imsm_super
*mpb
= super
->anchor
;
6082 unsigned long long maxsize
;
6083 unsigned long long minsize
;
6087 /* find the largest common start free region of the possible disks */
6091 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6097 /* don't activate new spares if we are orom constrained
6098 * and there is already a volume active in the container
6100 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
6103 e
= get_extents(super
, dl
);
6106 for (i
= 1; e
[i
-1].size
; i
++)
6114 maxsize
= merge_extents(super
, extent_cnt
);
6118 minsize
= chunk
* 2;
6120 if (cnt
< raiddisks
||
6121 (super
->orom
&& used
&& used
!= raiddisks
) ||
6122 maxsize
< minsize
||
6124 pr_err("not enough devices with space to create array.\n");
6125 return 0; /* No enough free spaces large enough */
6136 if (!check_env("IMSM_NO_PLATFORM") &&
6137 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6138 pr_err("attempting to create a second "
6139 "volume with size less then remaining space. "
6144 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6146 dl
->raiddisk
= cnt
++;
6150 dprintf("imsm: imsm_get_free_size() returns : %llu\n", size
);
6155 static int reserve_space(struct supertype
*st
, int raiddisks
,
6156 unsigned long long size
, int chunk
,
6157 unsigned long long *freesize
)
6159 struct intel_super
*super
= st
->sb
;
6164 rv
= imsm_get_free_size(st
, raiddisks
, size
, chunk
, freesize
);
6167 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6169 dl
->raiddisk
= cnt
++;
6176 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
6177 int raiddisks
, int *chunk
, unsigned long long size
,
6178 unsigned long long data_offset
,
6179 char *dev
, unsigned long long *freesize
,
6187 * if given unused devices create a container
6188 * if given given devices in a container create a member volume
6190 if (level
== LEVEL_CONTAINER
) {
6191 /* Must be a fresh device to add to a container */
6192 return validate_geometry_imsm_container(st
, level
, layout
,
6202 struct intel_super
*super
= st
->sb
;
6203 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
6204 raiddisks
, chunk
, size
,
6207 /* we are being asked to automatically layout a
6208 * new volume based on the current contents of
6209 * the container. If the the parameters can be
6210 * satisfied reserve_space will record the disks,
6211 * start offset, and size of the volume to be
6212 * created. add_to_super and getinfo_super
6213 * detect when autolayout is in progress.
6215 /* assuming that freesize is always given when array is
6217 if (super
->orom
&& freesize
) {
6219 count
= count_volumes(super
->hba
->path
,
6220 super
->orom
->dpa
, verbose
);
6221 if (super
->orom
->vphba
<= count
) {
6222 pr_vrb(": platform does not support more"
6223 " than %d raid volumes.\n",
6224 super
->orom
->vphba
);
6229 return reserve_space(st
, raiddisks
, size
,
6230 chunk
?*chunk
:0, freesize
);
6235 /* creating in a given container */
6236 return validate_geometry_imsm_volume(st
, level
, layout
,
6237 raiddisks
, chunk
, size
,
6239 dev
, freesize
, verbose
);
6242 /* This device needs to be a device in an 'imsm' container */
6243 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
6246 pr_err("Cannot create this array on device %s\n",
6251 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
6253 pr_err("Cannot open %s: %s\n",
6254 dev
, strerror(errno
));
6257 /* Well, it is in use by someone, maybe an 'imsm' container. */
6258 cfd
= open_container(fd
);
6262 pr_err("Cannot use %s: It is busy\n",
6266 sra
= sysfs_read(cfd
, 0, GET_VERSION
);
6267 if (sra
&& sra
->array
.major_version
== -1 &&
6268 strcmp(sra
->text_version
, "imsm") == 0)
6272 /* This is a member of a imsm container. Load the container
6273 * and try to create a volume
6275 struct intel_super
*super
;
6277 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
, NULL
, 1) == 0) {
6279 st
->container_dev
= fd2devnum(cfd
);
6281 return validate_geometry_imsm_volume(st
, level
, layout
,
6283 size
, data_offset
, dev
,
6290 pr_err("failed container membership check\n");
6296 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
6298 struct intel_super
*super
= st
->sb
;
6300 if (level
&& *level
== UnSet
)
6301 *level
= LEVEL_CONTAINER
;
6303 if (level
&& layout
&& *layout
== UnSet
)
6304 *layout
= imsm_level_to_layout(*level
);
6306 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0))
6307 *chunk
= imsm_default_chunk(super
->orom
);
6310 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
6312 static int kill_subarray_imsm(struct supertype
*st
)
6314 /* remove the subarray currently referenced by ->current_vol */
6316 struct intel_dev
**dp
;
6317 struct intel_super
*super
= st
->sb
;
6318 __u8 current_vol
= super
->current_vol
;
6319 struct imsm_super
*mpb
= super
->anchor
;
6321 if (super
->current_vol
< 0)
6323 super
->current_vol
= -1; /* invalidate subarray cursor */
6325 /* block deletions that would change the uuid of active subarrays
6327 * FIXME when immutable ids are available, but note that we'll
6328 * also need to fixup the invalidated/active subarray indexes in
6331 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6334 if (i
< current_vol
)
6336 sprintf(subarray
, "%u", i
);
6337 if (is_subarray_active(subarray
, st
->devname
)) {
6338 pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
6345 if (st
->update_tail
) {
6346 struct imsm_update_kill_array
*u
= xmalloc(sizeof(*u
));
6348 u
->type
= update_kill_array
;
6349 u
->dev_idx
= current_vol
;
6350 append_metadata_update(st
, u
, sizeof(*u
));
6355 for (dp
= &super
->devlist
; *dp
;)
6356 if ((*dp
)->index
== current_vol
) {
6359 handle_missing(super
, (*dp
)->dev
);
6360 if ((*dp
)->index
> current_vol
)
6365 /* no more raid devices, all active components are now spares,
6366 * but of course failed are still failed
6368 if (--mpb
->num_raid_devs
== 0) {
6371 for (d
= super
->disks
; d
; d
= d
->next
)
6376 super
->updates_pending
++;
6381 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
6382 char *update
, struct mddev_ident
*ident
)
6384 /* update the subarray currently referenced by ->current_vol */
6385 struct intel_super
*super
= st
->sb
;
6386 struct imsm_super
*mpb
= super
->anchor
;
6388 if (strcmp(update
, "name") == 0) {
6389 char *name
= ident
->name
;
6393 if (is_subarray_active(subarray
, st
->devname
)) {
6394 pr_err("Unable to update name of active subarray\n");
6398 if (!check_name(super
, name
, 0))
6401 vol
= strtoul(subarray
, &ep
, 10);
6402 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
6405 if (st
->update_tail
) {
6406 struct imsm_update_rename_array
*u
= xmalloc(sizeof(*u
));
6408 u
->type
= update_rename_array
;
6410 snprintf((char *) u
->name
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6411 append_metadata_update(st
, u
, sizeof(*u
));
6413 struct imsm_dev
*dev
;
6416 dev
= get_imsm_dev(super
, vol
);
6417 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6418 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6419 dev
= get_imsm_dev(super
, i
);
6420 handle_missing(super
, dev
);
6422 super
->updates_pending
++;
6429 #endif /* MDASSEMBLE */
6431 static int is_gen_migration(struct imsm_dev
*dev
)
6436 if (!dev
->vol
.migr_state
)
6439 if (migr_type(dev
) == MIGR_GEN_MIGR
)
6445 static int is_rebuilding(struct imsm_dev
*dev
)
6447 struct imsm_map
*migr_map
;
6449 if (!dev
->vol
.migr_state
)
6452 if (migr_type(dev
) != MIGR_REBUILD
)
6455 migr_map
= get_imsm_map(dev
, MAP_1
);
6457 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
6464 static int is_initializing(struct imsm_dev
*dev
)
6466 struct imsm_map
*migr_map
;
6468 if (!dev
->vol
.migr_state
)
6471 if (migr_type(dev
) != MIGR_INIT
)
6474 migr_map
= get_imsm_map(dev
, MAP_1
);
6476 if (migr_map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
6483 static void update_recovery_start(struct intel_super
*super
,
6484 struct imsm_dev
*dev
,
6485 struct mdinfo
*array
)
6487 struct mdinfo
*rebuild
= NULL
;
6491 if (!is_rebuilding(dev
))
6494 /* Find the rebuild target, but punt on the dual rebuild case */
6495 for (d
= array
->devs
; d
; d
= d
->next
)
6496 if (d
->recovery_start
== 0) {
6503 /* (?) none of the disks are marked with
6504 * IMSM_ORD_REBUILD, so assume they are missing and the
6505 * disk_ord_tbl was not correctly updated
6507 dprintf("%s: failed to locate out-of-sync disk\n", __func__
);
6511 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
6512 rebuild
->recovery_start
= units
* blocks_per_migr_unit(super
, dev
);
6516 static int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
);
6519 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
6521 /* Given a container loaded by load_super_imsm_all,
6522 * extract information about all the arrays into
6524 * If 'subarray' is given, just extract info about that array.
6526 * For each imsm_dev create an mdinfo, fill it in,
6527 * then look for matching devices in super->disks
6528 * and create appropriate device mdinfo.
6530 struct intel_super
*super
= st
->sb
;
6531 struct imsm_super
*mpb
= super
->anchor
;
6532 struct mdinfo
*rest
= NULL
;
6536 int spare_disks
= 0;
6538 /* do not assemble arrays when not all attributes are supported */
6539 if (imsm_check_attributes(mpb
->attributes
) == 0) {
6541 pr_err("Unsupported attributes in IMSM metadata."
6542 "Arrays activation is blocked.\n");
6545 /* check for bad blocks */
6546 if (imsm_bbm_log_size(super
->anchor
)) {
6547 pr_err("BBM log found in IMSM metadata."
6548 "Arrays activation is blocked.\n");
6553 /* count spare devices, not used in maps
6555 for (d
= super
->disks
; d
; d
= d
->next
)
6559 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6560 struct imsm_dev
*dev
;
6561 struct imsm_map
*map
;
6562 struct imsm_map
*map2
;
6563 struct mdinfo
*this;
6571 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
6574 dev
= get_imsm_dev(super
, i
);
6575 map
= get_imsm_map(dev
, MAP_0
);
6576 map2
= get_imsm_map(dev
, MAP_1
);
6578 /* do not publish arrays that are in the middle of an
6579 * unsupported migration
6581 if (dev
->vol
.migr_state
&&
6582 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
6583 pr_err("cannot assemble volume '%.16s':"
6584 " unsupported migration in progress\n",
6588 /* do not publish arrays that are not support by controller's
6592 this = xmalloc(sizeof(*this));
6594 super
->current_vol
= i
;
6595 getinfo_super_imsm_volume(st
, this, NULL
);
6598 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
6599 /* mdadm does not support all metadata features- set the bit in all arrays state */
6600 if (!validate_geometry_imsm_orom(super
,
6601 get_imsm_raid_level(map
), /* RAID level */
6602 imsm_level_to_layout(get_imsm_raid_level(map
)),
6603 map
->num_members
, /* raid disks */
6604 &chunk
, join_u32(dev
->size_low
, dev
->size_high
),
6606 pr_err("IMSM RAID geometry validation"
6607 " failed. Array %s activation is blocked.\n",
6609 this->array
.state
|=
6610 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
6611 (1<<MD_SB_BLOCK_VOLUME
);
6615 /* if array has bad blocks, set suitable bit in all arrays state */
6617 this->array
.state
|=
6618 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
6619 (1<<MD_SB_BLOCK_VOLUME
);
6621 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
6622 unsigned long long recovery_start
;
6623 struct mdinfo
*info_d
;
6630 idx
= get_imsm_disk_idx(dev
, slot
, MAP_0
);
6631 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
6632 for (d
= super
->disks
; d
; d
= d
->next
)
6633 if (d
->index
== idx
)
6636 recovery_start
= MaxSector
;
6639 if (d
&& is_failed(&d
->disk
))
6641 if (ord
& IMSM_ORD_REBUILD
)
6645 * if we skip some disks the array will be assmebled degraded;
6646 * reset resync start to avoid a dirty-degraded
6647 * situation when performing the intial sync
6649 * FIXME handle dirty degraded
6651 if ((skip
|| recovery_start
== 0) && !dev
->vol
.dirty
)
6652 this->resync_start
= MaxSector
;
6656 info_d
= xcalloc(1, sizeof(*info_d
));
6657 info_d
->next
= this->devs
;
6658 this->devs
= info_d
;
6660 info_d
->disk
.number
= d
->index
;
6661 info_d
->disk
.major
= d
->major
;
6662 info_d
->disk
.minor
= d
->minor
;
6663 info_d
->disk
.raid_disk
= slot
;
6664 info_d
->recovery_start
= recovery_start
;
6666 if (slot
< map2
->num_members
)
6667 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
6669 this->array
.spare_disks
++;
6671 if (slot
< map
->num_members
)
6672 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
6674 this->array
.spare_disks
++;
6676 if (info_d
->recovery_start
== MaxSector
)
6677 this->array
.working_disks
++;
6679 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
6680 info_d
->data_offset
= pba_of_lba0(map
);
6681 info_d
->component_size
= blocks_per_member(map
);
6683 /* now that the disk list is up-to-date fixup recovery_start */
6684 update_recovery_start(super
, dev
, this);
6685 this->array
.spare_disks
+= spare_disks
;
6688 /* check for reshape */
6689 if (this->reshape_active
== 1)
6690 recover_backup_imsm(st
, this);
6699 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
6700 int failed
, int look_in_map
)
6702 struct imsm_map
*map
;
6704 map
= get_imsm_map(dev
, look_in_map
);
6707 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
6708 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
6710 switch (get_imsm_raid_level(map
)) {
6712 return IMSM_T_STATE_FAILED
;
6715 if (failed
< map
->num_members
)
6716 return IMSM_T_STATE_DEGRADED
;
6718 return IMSM_T_STATE_FAILED
;
6723 * check to see if any mirrors have failed, otherwise we
6724 * are degraded. Even numbered slots are mirrored on
6728 /* gcc -Os complains that this is unused */
6729 int insync
= insync
;
6731 for (i
= 0; i
< map
->num_members
; i
++) {
6732 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_X
);
6733 int idx
= ord_to_idx(ord
);
6734 struct imsm_disk
*disk
;
6736 /* reset the potential in-sync count on even-numbered
6737 * slots. num_copies is always 2 for imsm raid10
6742 disk
= get_imsm_disk(super
, idx
);
6743 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
6746 /* no in-sync disks left in this mirror the
6750 return IMSM_T_STATE_FAILED
;
6753 return IMSM_T_STATE_DEGRADED
;
6757 return IMSM_T_STATE_DEGRADED
;
6759 return IMSM_T_STATE_FAILED
;
6765 return map
->map_state
;
6768 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
6773 struct imsm_disk
*disk
;
6774 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6775 struct imsm_map
*prev
= get_imsm_map(dev
, MAP_1
);
6776 struct imsm_map
*map_for_loop
;
6781 /* at the beginning of migration we set IMSM_ORD_REBUILD on
6782 * disks that are being rebuilt. New failures are recorded to
6783 * map[0]. So we look through all the disks we started with and
6784 * see if any failures are still present, or if any new ones
6788 if (prev
&& (map
->num_members
< prev
->num_members
))
6789 map_for_loop
= prev
;
6791 for (i
= 0; i
< map_for_loop
->num_members
; i
++) {
6793 /* when MAP_X is passed both maps failures are counted
6796 ((look_in_map
== MAP_1
) || (look_in_map
== MAP_X
)) &&
6797 (i
< prev
->num_members
)) {
6798 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
6799 idx_1
= ord_to_idx(ord
);
6801 disk
= get_imsm_disk(super
, idx_1
);
6802 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
6805 if (((look_in_map
== MAP_0
) || (look_in_map
== MAP_X
)) &&
6806 (i
< map
->num_members
)) {
6807 ord
= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
6808 idx
= ord_to_idx(ord
);
6811 disk
= get_imsm_disk(super
, idx
);
6812 if (!disk
|| is_failed(disk
) ||
6813 ord
& IMSM_ORD_REBUILD
)
6823 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
6826 struct intel_super
*super
= c
->sb
;
6827 struct imsm_super
*mpb
= super
->anchor
;
6829 if (atoi(inst
) >= mpb
->num_raid_devs
) {
6830 fprintf(stderr
, "%s: subarry index %d, out of range\n",
6831 __func__
, atoi(inst
));
6835 dprintf("imsm: open_new %s\n", inst
);
6836 a
->info
.container_member
= atoi(inst
);
6840 static int is_resyncing(struct imsm_dev
*dev
)
6842 struct imsm_map
*migr_map
;
6844 if (!dev
->vol
.migr_state
)
6847 if (migr_type(dev
) == MIGR_INIT
||
6848 migr_type(dev
) == MIGR_REPAIR
)
6851 if (migr_type(dev
) == MIGR_GEN_MIGR
)
6854 migr_map
= get_imsm_map(dev
, MAP_1
);
6856 if ((migr_map
->map_state
== IMSM_T_STATE_NORMAL
) &&
6857 (dev
->vol
.migr_type
!= MIGR_GEN_MIGR
))
6863 /* return true if we recorded new information */
6864 static int mark_failure(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
6868 struct imsm_map
*map
;
6869 char buf
[MAX_RAID_SERIAL_LEN
+3];
6870 unsigned int len
, shift
= 0;
6872 /* new failures are always set in map[0] */
6873 map
= get_imsm_map(dev
, MAP_0
);
6875 slot
= get_imsm_disk_slot(map
, idx
);
6879 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
6880 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
6883 memcpy(buf
, disk
->serial
, MAX_RAID_SERIAL_LEN
);
6884 buf
[MAX_RAID_SERIAL_LEN
] = '\000';
6886 if ((len
= strlen(buf
)) >= MAX_RAID_SERIAL_LEN
)
6887 shift
= len
- MAX_RAID_SERIAL_LEN
+ 1;
6888 strncpy((char *)disk
->serial
, &buf
[shift
], MAX_RAID_SERIAL_LEN
);
6890 disk
->status
|= FAILED_DISK
;
6891 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
6892 /* mark failures in second map if second map exists and this disk
6894 * This is valid for migration, initialization and rebuild
6896 if (dev
->vol
.migr_state
) {
6897 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
6898 int slot2
= get_imsm_disk_slot(map2
, idx
);
6900 if ((slot2
< map2
->num_members
) &&
6902 set_imsm_ord_tbl_ent(map2
, slot2
,
6903 idx
| IMSM_ORD_REBUILD
);
6905 if (map
->failed_disk_num
== 0xff)
6906 map
->failed_disk_num
= slot
;
6910 static void mark_missing(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
6912 mark_failure(dev
, disk
, idx
);
6914 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
6917 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
6918 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
6921 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
6925 if (!super
->missing
)
6928 dprintf("imsm: mark missing\n");
6929 /* end process for initialization and rebuild only
6931 if (is_gen_migration(dev
) == 0) {
6935 failed
= imsm_count_failed(super
, dev
, MAP_0
);
6936 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
6938 end_migration(dev
, super
, map_state
);
6940 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
6941 mark_missing(dev
, &dl
->disk
, dl
->index
);
6942 super
->updates_pending
++;
6945 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
,
6948 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
6949 unsigned long long array_blocks
;
6950 struct imsm_map
*map
;
6952 if (used_disks
== 0) {
6953 /* when problems occures
6954 * return current array_blocks value
6956 array_blocks
= __le32_to_cpu(dev
->size_high
);
6957 array_blocks
= array_blocks
<< 32;
6958 array_blocks
+= __le32_to_cpu(dev
->size_low
);
6960 return array_blocks
;
6963 /* set array size in metadata
6965 if (new_size
<= 0) {
6966 /* OLCE size change is caused by added disks
6968 map
= get_imsm_map(dev
, MAP_0
);
6969 array_blocks
= blocks_per_member(map
) * used_disks
;
6971 /* Online Volume Size Change
6972 * Using available free space
6974 array_blocks
= new_size
;
6977 /* round array size down to closest MB
6979 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
6980 dev
->size_low
= __cpu_to_le32((__u32
)array_blocks
);
6981 dev
->size_high
= __cpu_to_le32((__u32
)(array_blocks
>> 32));
6983 return array_blocks
;
6986 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
6988 static void imsm_progress_container_reshape(struct intel_super
*super
)
6990 /* if no device has a migr_state, but some device has a
6991 * different number of members than the previous device, start
6992 * changing the number of devices in this device to match
6995 struct imsm_super
*mpb
= super
->anchor
;
6996 int prev_disks
= -1;
7000 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7001 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
7002 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7003 struct imsm_map
*map2
;
7004 int prev_num_members
;
7006 if (dev
->vol
.migr_state
)
7009 if (prev_disks
== -1)
7010 prev_disks
= map
->num_members
;
7011 if (prev_disks
== map
->num_members
)
7014 /* OK, this array needs to enter reshape mode.
7015 * i.e it needs a migr_state
7018 copy_map_size
= sizeof_imsm_map(map
);
7019 prev_num_members
= map
->num_members
;
7020 map
->num_members
= prev_disks
;
7021 dev
->vol
.migr_state
= 1;
7022 dev
->vol
.curr_migr_unit
= 0;
7023 set_migr_type(dev
, MIGR_GEN_MIGR
);
7024 for (i
= prev_num_members
;
7025 i
< map
->num_members
; i
++)
7026 set_imsm_ord_tbl_ent(map
, i
, i
);
7027 map2
= get_imsm_map(dev
, MAP_1
);
7028 /* Copy the current map */
7029 memcpy(map2
, map
, copy_map_size
);
7030 map2
->num_members
= prev_num_members
;
7032 imsm_set_array_size(dev
, -1);
7033 super
->clean_migration_record_by_mdmon
= 1;
7034 super
->updates_pending
++;
7038 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
7039 * states are handled in imsm_set_disk() with one exception, when a
7040 * resync is stopped due to a new failure this routine will set the
7041 * 'degraded' state for the array.
7043 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
7045 int inst
= a
->info
.container_member
;
7046 struct intel_super
*super
= a
->container
->sb
;
7047 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7048 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7049 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
7050 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7051 __u32 blocks_per_unit
;
7053 if (dev
->vol
.migr_state
&&
7054 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
7055 /* array state change is blocked due to reshape action
7057 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
7058 * - finish the reshape (if last_checkpoint is big and action != reshape)
7059 * - update curr_migr_unit
7061 if (a
->curr_action
== reshape
) {
7062 /* still reshaping, maybe update curr_migr_unit */
7063 goto mark_checkpoint
;
7065 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
7066 /* for some reason we aborted the reshape.
7068 * disable automatic metadata rollback
7069 * user action is required to recover process
7072 struct imsm_map
*map2
=
7073 get_imsm_map(dev
, MAP_1
);
7074 dev
->vol
.migr_state
= 0;
7075 set_migr_type(dev
, 0);
7076 dev
->vol
.curr_migr_unit
= 0;
7078 sizeof_imsm_map(map2
));
7079 super
->updates_pending
++;
7082 if (a
->last_checkpoint
>= a
->info
.component_size
) {
7083 unsigned long long array_blocks
;
7087 used_disks
= imsm_num_data_members(dev
, MAP_0
);
7088 if (used_disks
> 0) {
7090 blocks_per_member(map
) *
7092 /* round array size down to closest MB
7094 array_blocks
= (array_blocks
7095 >> SECT_PER_MB_SHIFT
)
7096 << SECT_PER_MB_SHIFT
;
7097 a
->info
.custom_array_size
= array_blocks
;
7098 /* encourage manager to update array
7102 a
->check_reshape
= 1;
7104 /* finalize online capacity expansion/reshape */
7105 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
7107 mdi
->disk
.raid_disk
,
7110 imsm_progress_container_reshape(super
);
7115 /* before we activate this array handle any missing disks */
7116 if (consistent
== 2)
7117 handle_missing(super
, dev
);
7119 if (consistent
== 2 &&
7120 (!is_resync_complete(&a
->info
) ||
7121 map_state
!= IMSM_T_STATE_NORMAL
||
7122 dev
->vol
.migr_state
))
7125 if (is_resync_complete(&a
->info
)) {
7126 /* complete intialization / resync,
7127 * recovery and interrupted recovery is completed in
7130 if (is_resyncing(dev
)) {
7131 dprintf("imsm: mark resync done\n");
7132 end_migration(dev
, super
, map_state
);
7133 super
->updates_pending
++;
7134 a
->last_checkpoint
= 0;
7136 } else if ((!is_resyncing(dev
) && !failed
) &&
7137 (imsm_reshape_blocks_arrays_changes(super
) == 0)) {
7138 /* mark the start of the init process if nothing is failed */
7139 dprintf("imsm: mark resync start\n");
7140 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
7141 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
7143 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
7144 super
->updates_pending
++;
7148 /* skip checkpointing for general migration,
7149 * it is controlled in mdadm
7151 if (is_gen_migration(dev
))
7152 goto skip_mark_checkpoint
;
7154 /* check if we can update curr_migr_unit from resync_start, recovery_start */
7155 blocks_per_unit
= blocks_per_migr_unit(super
, dev
);
7156 if (blocks_per_unit
) {
7160 units
= a
->last_checkpoint
/ blocks_per_unit
;
7163 /* check that we did not overflow 32-bits, and that
7164 * curr_migr_unit needs updating
7166 if (units32
== units
&&
7168 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
7169 dprintf("imsm: mark checkpoint (%u)\n", units32
);
7170 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
7171 super
->updates_pending
++;
7175 skip_mark_checkpoint
:
7176 /* mark dirty / clean */
7177 if (dev
->vol
.dirty
!= !consistent
) {
7178 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
7183 super
->updates_pending
++;
7189 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
7191 int inst
= a
->info
.container_member
;
7192 struct intel_super
*super
= a
->container
->sb
;
7193 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7194 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7195 struct imsm_disk
*disk
;
7197 int recovery_not_finished
= 0;
7202 if (n
> map
->num_members
)
7203 fprintf(stderr
, "imsm: set_disk %d out of range 0..%d\n",
7204 n
, map
->num_members
- 1);
7209 dprintf("imsm: set_disk %d:%x\n", n
, state
);
7211 ord
= get_imsm_ord_tbl_ent(dev
, n
, MAP_0
);
7212 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
7214 /* check for new failures */
7215 if (state
& DS_FAULTY
) {
7216 if (mark_failure(dev
, disk
, ord_to_idx(ord
)))
7217 super
->updates_pending
++;
7220 /* check if in_sync */
7221 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
7222 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
7224 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
7225 super
->updates_pending
++;
7228 failed
= imsm_count_failed(super
, dev
, MAP_0
);
7229 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7231 /* check if recovery complete, newly degraded, or failed */
7232 dprintf("imsm: Detected transition to state ");
7233 switch (map_state
) {
7234 case IMSM_T_STATE_NORMAL
: /* transition to normal state */
7235 dprintf("normal: ");
7236 if (is_rebuilding(dev
)) {
7237 dprintf("while rebuilding");
7238 /* check if recovery is really finished */
7239 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
7240 if (mdi
->recovery_start
!= MaxSector
) {
7241 recovery_not_finished
= 1;
7244 if (recovery_not_finished
) {
7245 dprintf("\nimsm: Rebuild has not finished yet, "
7246 "state not changed");
7247 if (a
->last_checkpoint
< mdi
->recovery_start
) {
7248 a
->last_checkpoint
= mdi
->recovery_start
;
7249 super
->updates_pending
++;
7253 end_migration(dev
, super
, map_state
);
7254 map
= get_imsm_map(dev
, MAP_0
);
7255 map
->failed_disk_num
= ~0;
7256 super
->updates_pending
++;
7257 a
->last_checkpoint
= 0;
7260 if (is_gen_migration(dev
)) {
7261 dprintf("while general migration");
7262 if (a
->last_checkpoint
>= a
->info
.component_size
)
7263 end_migration(dev
, super
, map_state
);
7265 map
->map_state
= map_state
;
7266 map
= get_imsm_map(dev
, MAP_0
);
7267 map
->failed_disk_num
= ~0;
7268 super
->updates_pending
++;
7272 case IMSM_T_STATE_DEGRADED
: /* transition to degraded state */
7273 dprintf("degraded: ");
7274 if ((map
->map_state
!= map_state
) &&
7275 !dev
->vol
.migr_state
) {
7276 dprintf("mark degraded");
7277 map
->map_state
= map_state
;
7278 super
->updates_pending
++;
7279 a
->last_checkpoint
= 0;
7282 if (is_rebuilding(dev
)) {
7283 dprintf("while rebuilding.");
7284 if (map
->map_state
!= map_state
) {
7285 dprintf(" Map state change");
7286 end_migration(dev
, super
, map_state
);
7287 super
->updates_pending
++;
7291 if (is_gen_migration(dev
)) {
7292 dprintf("while general migration");
7293 if (a
->last_checkpoint
>= a
->info
.component_size
)
7294 end_migration(dev
, super
, map_state
);
7296 map
->map_state
= map_state
;
7297 manage_second_map(super
, dev
);
7299 super
->updates_pending
++;
7302 if (is_initializing(dev
)) {
7303 dprintf("while initialization.");
7304 map
->map_state
= map_state
;
7305 super
->updates_pending
++;
7309 case IMSM_T_STATE_FAILED
: /* transition to failed state */
7310 dprintf("failed: ");
7311 if (is_gen_migration(dev
)) {
7312 dprintf("while general migration");
7313 map
->map_state
= map_state
;
7314 super
->updates_pending
++;
7317 if (map
->map_state
!= map_state
) {
7318 dprintf("mark failed");
7319 end_migration(dev
, super
, map_state
);
7320 super
->updates_pending
++;
7321 a
->last_checkpoint
= 0;
7326 dprintf("state %i\n", map_state
);
7332 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
7335 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
7336 unsigned long long dsize
;
7337 unsigned long long sectors
;
7339 get_dev_size(fd
, NULL
, &dsize
);
7341 if (mpb_size
> 512) {
7342 /* -1 to account for anchor */
7343 sectors
= mpb_sectors(mpb
) - 1;
7345 /* write the extended mpb to the sectors preceeding the anchor */
7346 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0)
7349 if ((unsigned long long)write(fd
, buf
+ 512, 512 * sectors
)
7354 /* first block is stored on second to last sector of the disk */
7355 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0)
7358 if (write(fd
, buf
, 512) != 512)
7364 static void imsm_sync_metadata(struct supertype
*container
)
7366 struct intel_super
*super
= container
->sb
;
7368 dprintf("sync metadata: %d\n", super
->updates_pending
);
7369 if (!super
->updates_pending
)
7372 write_super_imsm(container
, 0);
7374 super
->updates_pending
= 0;
7377 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
7379 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7380 int i
= get_imsm_disk_idx(dev
, idx
, MAP_X
);
7383 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7387 if (dl
&& is_failed(&dl
->disk
))
7391 dprintf("%s: found %x:%x\n", __func__
, dl
->major
, dl
->minor
);
7396 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
7397 struct active_array
*a
, int activate_new
,
7398 struct mdinfo
*additional_test_list
)
7400 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7401 int idx
= get_imsm_disk_idx(dev
, slot
, MAP_X
);
7402 struct imsm_super
*mpb
= super
->anchor
;
7403 struct imsm_map
*map
;
7404 unsigned long long pos
;
7409 __u32 array_start
= 0;
7410 __u32 array_end
= 0;
7412 struct mdinfo
*test_list
;
7414 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7415 /* If in this array, skip */
7416 for (d
= a
->info
.devs
; d
; d
= d
->next
)
7417 if (d
->state_fd
>= 0 &&
7418 d
->disk
.major
== dl
->major
&&
7419 d
->disk
.minor
== dl
->minor
) {
7420 dprintf("%x:%x already in array\n",
7421 dl
->major
, dl
->minor
);
7426 test_list
= additional_test_list
;
7428 if (test_list
->disk
.major
== dl
->major
&&
7429 test_list
->disk
.minor
== dl
->minor
) {
7430 dprintf("%x:%x already in additional test list\n",
7431 dl
->major
, dl
->minor
);
7434 test_list
= test_list
->next
;
7439 /* skip in use or failed drives */
7440 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
7442 dprintf("%x:%x status (failed: %d index: %d)\n",
7443 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
7447 /* skip pure spares when we are looking for partially
7448 * assimilated drives
7450 if (dl
->index
== -1 && !activate_new
)
7453 /* Does this unused device have the requisite free space?
7454 * It needs to be able to cover all member volumes
7456 ex
= get_extents(super
, dl
);
7458 dprintf("cannot get extents\n");
7461 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7462 dev
= get_imsm_dev(super
, i
);
7463 map
= get_imsm_map(dev
, MAP_0
);
7465 /* check if this disk is already a member of
7468 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
7474 array_start
= pba_of_lba0(map
);
7475 array_end
= array_start
+
7476 blocks_per_member(map
) - 1;
7479 /* check that we can start at pba_of_lba0 with
7480 * blocks_per_member of space
7482 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
7486 pos
= ex
[j
].start
+ ex
[j
].size
;
7488 } while (ex
[j
-1].size
);
7495 if (i
< mpb
->num_raid_devs
) {
7496 dprintf("%x:%x does not have %u to %u available\n",
7497 dl
->major
, dl
->minor
, array_start
, array_end
);
7508 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
7510 struct imsm_dev
*dev2
;
7511 struct imsm_map
*map
;
7517 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
7519 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
, MAP_0
);
7520 if (state
== IMSM_T_STATE_FAILED
) {
7521 map
= get_imsm_map(dev2
, MAP_0
);
7524 for (slot
= 0; slot
< map
->num_members
; slot
++) {
7526 * Check if failed disks are deleted from intel
7527 * disk list or are marked to be deleted
7529 idx
= get_imsm_disk_idx(dev2
, slot
, MAP_X
);
7530 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
7532 * Do not rebuild the array if failed disks
7533 * from failed sub-array are not removed from
7537 is_failed(&idisk
->disk
) &&
7538 (idisk
->action
!= DISK_REMOVE
))
7546 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
7547 struct metadata_update
**updates
)
7550 * Find a device with unused free space and use it to replace a
7551 * failed/vacant region in an array. We replace failed regions one a
7552 * array at a time. The result is that a new spare disk will be added
7553 * to the first failed array and after the monitor has finished
7554 * propagating failures the remainder will be consumed.
7556 * FIXME add a capability for mdmon to request spares from another
7560 struct intel_super
*super
= a
->container
->sb
;
7561 int inst
= a
->info
.container_member
;
7562 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7563 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7564 int failed
= a
->info
.array
.raid_disks
;
7565 struct mdinfo
*rv
= NULL
;
7568 struct metadata_update
*mu
;
7570 struct imsm_update_activate_spare
*u
;
7575 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
7576 if ((d
->curr_state
& DS_FAULTY
) &&
7578 /* wait for Removal to happen */
7580 if (d
->state_fd
>= 0)
7584 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
7585 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
7587 if (imsm_reshape_blocks_arrays_changes(super
))
7590 /* Cannot activate another spare if rebuild is in progress already
7592 if (is_rebuilding(dev
)) {
7593 dprintf("imsm: No spare activation allowed. "
7594 "Rebuild in progress already.\n");
7598 if (a
->info
.array
.level
== 4)
7599 /* No repair for takeovered array
7600 * imsm doesn't support raid4
7604 if (imsm_check_degraded(super
, dev
, failed
, MAP_0
) !=
7605 IMSM_T_STATE_DEGRADED
)
7609 * If there are any failed disks check state of the other volume.
7610 * Block rebuild if the another one is failed until failed disks
7611 * are removed from container.
7614 dprintf("found failed disks in %.*s, check if there another"
7615 "failed sub-array.\n",
7616 MAX_RAID_SERIAL_LEN
, dev
->volume
);
7617 /* check if states of the other volumes allow for rebuild */
7618 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
7620 allowed
= imsm_rebuild_allowed(a
->container
,
7628 /* For each slot, if it is not working, find a spare */
7629 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
7630 for (d
= a
->info
.devs
; d
; d
= d
->next
)
7631 if (d
->disk
.raid_disk
== i
)
7633 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
7634 if (d
&& (d
->state_fd
>= 0))
7638 * OK, this device needs recovery. Try to re-add the
7639 * previous occupant of this slot, if this fails see if
7640 * we can continue the assimilation of a spare that was
7641 * partially assimilated, finally try to activate a new
7644 dl
= imsm_readd(super
, i
, a
);
7646 dl
= imsm_add_spare(super
, i
, a
, 0, rv
);
7648 dl
= imsm_add_spare(super
, i
, a
, 1, rv
);
7652 /* found a usable disk with enough space */
7653 di
= xcalloc(1, sizeof(*di
));
7655 /* dl->index will be -1 in the case we are activating a
7656 * pristine spare. imsm_process_update() will create a
7657 * new index in this case. Once a disk is found to be
7658 * failed in all member arrays it is kicked from the
7661 di
->disk
.number
= dl
->index
;
7663 /* (ab)use di->devs to store a pointer to the device
7666 di
->devs
= (struct mdinfo
*) dl
;
7668 di
->disk
.raid_disk
= i
;
7669 di
->disk
.major
= dl
->major
;
7670 di
->disk
.minor
= dl
->minor
;
7672 di
->recovery_start
= 0;
7673 di
->data_offset
= pba_of_lba0(map
);
7674 di
->component_size
= a
->info
.component_size
;
7675 di
->container_member
= inst
;
7676 super
->random
= random32();
7680 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
7681 i
, di
->data_offset
);
7685 /* No spares found */
7687 /* Now 'rv' has a list of devices to return.
7688 * Create a metadata_update record to update the
7689 * disk_ord_tbl for the array
7691 mu
= xmalloc(sizeof(*mu
));
7692 mu
->buf
= xcalloc(num_spares
,
7693 sizeof(struct imsm_update_activate_spare
));
7695 mu
->space_list
= NULL
;
7696 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
7697 mu
->next
= *updates
;
7698 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
7700 for (di
= rv
; di
; di
= di
->next
) {
7701 u
->type
= update_activate_spare
;
7702 u
->dl
= (struct dl
*) di
->devs
;
7704 u
->slot
= di
->disk
.raid_disk
;
7715 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
7717 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
7718 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7719 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, MAP_0
);
7720 struct disk_info
*inf
= get_disk_info(u
);
7721 struct imsm_disk
*disk
;
7725 for (i
= 0; i
< map
->num_members
; i
++) {
7726 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, MAP_X
));
7727 for (j
= 0; j
< new_map
->num_members
; j
++)
7728 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
7736 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
7738 struct dl
*dl
= NULL
;
7739 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7740 if ((dl
->major
== major
) && (dl
->minor
== minor
))
7745 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
7747 struct dl
*prev
= NULL
;
7751 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7752 if ((dl
->major
== major
) && (dl
->minor
== minor
)) {
7755 prev
->next
= dl
->next
;
7757 super
->disks
= dl
->next
;
7759 __free_imsm_disk(dl
);
7760 dprintf("%s: removed %x:%x\n",
7761 __func__
, major
, minor
);
7769 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
7771 static int add_remove_disk_update(struct intel_super
*super
)
7773 int check_degraded
= 0;
7774 struct dl
*disk
= NULL
;
7775 /* add/remove some spares to/from the metadata/contrainer */
7776 while (super
->disk_mgmt_list
) {
7777 struct dl
*disk_cfg
;
7779 disk_cfg
= super
->disk_mgmt_list
;
7780 super
->disk_mgmt_list
= disk_cfg
->next
;
7781 disk_cfg
->next
= NULL
;
7783 if (disk_cfg
->action
== DISK_ADD
) {
7784 disk_cfg
->next
= super
->disks
;
7785 super
->disks
= disk_cfg
;
7787 dprintf("%s: added %x:%x\n",
7788 __func__
, disk_cfg
->major
,
7790 } else if (disk_cfg
->action
== DISK_REMOVE
) {
7791 dprintf("Disk remove action processed: %x.%x\n",
7792 disk_cfg
->major
, disk_cfg
->minor
);
7793 disk
= get_disk_super(super
,
7797 /* store action status */
7798 disk
->action
= DISK_REMOVE
;
7799 /* remove spare disks only */
7800 if (disk
->index
== -1) {
7801 remove_disk_super(super
,
7806 /* release allocate disk structure */
7807 __free_imsm_disk(disk_cfg
);
7810 return check_degraded
;
7814 static int apply_reshape_migration_update(struct imsm_update_reshape_migration
*u
,
7815 struct intel_super
*super
,
7818 struct intel_dev
*id
;
7819 void **tofree
= NULL
;
7822 dprintf("apply_reshape_migration_update()\n");
7823 if ((u
->subdev
< 0) ||
7825 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
7828 if ((space_list
== NULL
) || (*space_list
== NULL
)) {
7829 dprintf("imsm: Error: Memory is not allocated\n");
7833 for (id
= super
->devlist
; id
; id
= id
->next
) {
7834 if (id
->index
== (unsigned)u
->subdev
) {
7835 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
7836 struct imsm_map
*map
;
7837 struct imsm_dev
*new_dev
=
7838 (struct imsm_dev
*)*space_list
;
7839 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
7841 struct dl
*new_disk
;
7843 if (new_dev
== NULL
)
7845 *space_list
= **space_list
;
7846 memcpy(new_dev
, dev
, sizeof_imsm_dev(dev
, 0));
7847 map
= get_imsm_map(new_dev
, MAP_0
);
7849 dprintf("imsm: Error: migration in progress");
7853 to_state
= map
->map_state
;
7854 if ((u
->new_level
== 5) && (map
->raid_level
== 0)) {
7856 /* this should not happen */
7857 if (u
->new_disks
[0] < 0) {
7858 map
->failed_disk_num
=
7859 map
->num_members
- 1;
7860 to_state
= IMSM_T_STATE_DEGRADED
;
7862 to_state
= IMSM_T_STATE_NORMAL
;
7864 migrate(new_dev
, super
, to_state
, MIGR_GEN_MIGR
);
7865 if (u
->new_level
> -1)
7866 map
->raid_level
= u
->new_level
;
7867 migr_map
= get_imsm_map(new_dev
, MAP_1
);
7868 if ((u
->new_level
== 5) &&
7869 (migr_map
->raid_level
== 0)) {
7870 int ord
= map
->num_members
- 1;
7871 migr_map
->num_members
--;
7872 if (u
->new_disks
[0] < 0)
7873 ord
|= IMSM_ORD_REBUILD
;
7874 set_imsm_ord_tbl_ent(map
,
7875 map
->num_members
- 1,
7879 tofree
= (void **)dev
;
7881 /* update chunk size
7883 if (u
->new_chunksize
> 0)
7884 map
->blocks_per_strip
=
7885 __cpu_to_le16(u
->new_chunksize
* 2);
7889 if ((u
->new_level
!= 5) ||
7890 (migr_map
->raid_level
!= 0) ||
7891 (migr_map
->raid_level
== map
->raid_level
))
7894 if (u
->new_disks
[0] >= 0) {
7897 new_disk
= get_disk_super(super
,
7898 major(u
->new_disks
[0]),
7899 minor(u
->new_disks
[0]));
7900 dprintf("imsm: new disk for reshape is: %i:%i "
7901 "(%p, index = %i)\n",
7902 major(u
->new_disks
[0]),
7903 minor(u
->new_disks
[0]),
7904 new_disk
, new_disk
->index
);
7905 if (new_disk
== NULL
)
7906 goto error_disk_add
;
7908 new_disk
->index
= map
->num_members
- 1;
7909 /* slot to fill in autolayout
7911 new_disk
->raiddisk
= new_disk
->index
;
7912 new_disk
->disk
.status
|= CONFIGURED_DISK
;
7913 new_disk
->disk
.status
&= ~SPARE_DISK
;
7915 goto error_disk_add
;
7918 *tofree
= *space_list
;
7919 /* calculate new size
7921 imsm_set_array_size(new_dev
, -1);
7928 *space_list
= tofree
;
7932 dprintf("Error: imsm: Cannot find disk.\n");
7936 static int apply_size_change_update(struct imsm_update_size_change
*u
,
7937 struct intel_super
*super
)
7939 struct intel_dev
*id
;
7942 dprintf("apply_size_change_update()\n");
7943 if ((u
->subdev
< 0) ||
7945 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
7949 for (id
= super
->devlist
; id
; id
= id
->next
) {
7950 if (id
->index
== (unsigned)u
->subdev
) {
7951 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
7952 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7953 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
7954 unsigned long long blocks_per_member
;
7956 /* calculate new size
7958 blocks_per_member
= u
->new_size
/ used_disks
;
7959 dprintf("imsm: apply_size_change_update(size: %llu, "
7960 "blocks per member: %llu)\n",
7961 u
->new_size
, blocks_per_member
);
7962 set_blocks_per_member(map
, blocks_per_member
);
7963 imsm_set_array_size(dev
, u
->new_size
);
7974 static int apply_update_activate_spare(struct imsm_update_activate_spare
*u
,
7975 struct intel_super
*super
,
7976 struct active_array
*active_array
)
7978 struct imsm_super
*mpb
= super
->anchor
;
7979 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
7980 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7981 struct imsm_map
*migr_map
;
7982 struct active_array
*a
;
7983 struct imsm_disk
*disk
;
7990 int second_map_created
= 0;
7992 for (; u
; u
= u
->next
) {
7993 victim
= get_imsm_disk_idx(dev
, u
->slot
, MAP_X
);
7998 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
8003 fprintf(stderr
, "error: imsm_activate_spare passed "
8004 "an unknown disk (index: %d)\n",
8009 /* count failures (excluding rebuilds and the victim)
8010 * to determine map[0] state
8013 for (i
= 0; i
< map
->num_members
; i
++) {
8016 disk
= get_imsm_disk(super
,
8017 get_imsm_disk_idx(dev
, i
, MAP_X
));
8018 if (!disk
|| is_failed(disk
))
8022 /* adding a pristine spare, assign a new index */
8023 if (dl
->index
< 0) {
8024 dl
->index
= super
->anchor
->num_disks
;
8025 super
->anchor
->num_disks
++;
8028 disk
->status
|= CONFIGURED_DISK
;
8029 disk
->status
&= ~SPARE_DISK
;
8032 to_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8033 if (!second_map_created
) {
8034 second_map_created
= 1;
8035 map
->map_state
= IMSM_T_STATE_DEGRADED
;
8036 migrate(dev
, super
, to_state
, MIGR_REBUILD
);
8038 map
->map_state
= to_state
;
8039 migr_map
= get_imsm_map(dev
, MAP_1
);
8040 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
8041 set_imsm_ord_tbl_ent(migr_map
, u
->slot
,
8042 dl
->index
| IMSM_ORD_REBUILD
);
8044 /* update the family_num to mark a new container
8045 * generation, being careful to record the existing
8046 * family_num in orig_family_num to clean up after
8047 * earlier mdadm versions that neglected to set it.
8049 if (mpb
->orig_family_num
== 0)
8050 mpb
->orig_family_num
= mpb
->family_num
;
8051 mpb
->family_num
+= super
->random
;
8053 /* count arrays using the victim in the metadata */
8055 for (a
= active_array
; a
; a
= a
->next
) {
8056 dev
= get_imsm_dev(super
, a
->info
.container_member
);
8057 map
= get_imsm_map(dev
, MAP_0
);
8059 if (get_imsm_disk_slot(map
, victim
) >= 0)
8063 /* delete the victim if it is no longer being
8069 /* We know that 'manager' isn't touching anything,
8070 * so it is safe to delete
8072 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
8073 if ((*dlp
)->index
== victim
)
8076 /* victim may be on the missing list */
8078 for (dlp
= &super
->missing
; *dlp
;
8079 dlp
= &(*dlp
)->next
)
8080 if ((*dlp
)->index
== victim
)
8082 imsm_delete(super
, dlp
, victim
);
8089 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
8090 struct intel_super
*super
,
8093 struct dl
*new_disk
;
8094 struct intel_dev
*id
;
8096 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
8097 int disk_count
= u
->old_raid_disks
;
8098 void **tofree
= NULL
;
8099 int devices_to_reshape
= 1;
8100 struct imsm_super
*mpb
= super
->anchor
;
8102 unsigned int dev_id
;
8104 dprintf("imsm: apply_reshape_container_disks_update()\n");
8106 /* enable spares to use in array */
8107 for (i
= 0; i
< delta_disks
; i
++) {
8108 new_disk
= get_disk_super(super
,
8109 major(u
->new_disks
[i
]),
8110 minor(u
->new_disks
[i
]));
8111 dprintf("imsm: new disk for reshape is: %i:%i "
8112 "(%p, index = %i)\n",
8113 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
8114 new_disk
, new_disk
->index
);
8115 if ((new_disk
== NULL
) ||
8116 ((new_disk
->index
>= 0) &&
8117 (new_disk
->index
< u
->old_raid_disks
)))
8118 goto update_reshape_exit
;
8119 new_disk
->index
= disk_count
++;
8120 /* slot to fill in autolayout
8122 new_disk
->raiddisk
= new_disk
->index
;
8123 new_disk
->disk
.status
|=
8125 new_disk
->disk
.status
&= ~SPARE_DISK
;
8128 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
8129 mpb
->num_raid_devs
);
8130 /* manage changes in volume
8132 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
8133 void **sp
= *space_list
;
8134 struct imsm_dev
*newdev
;
8135 struct imsm_map
*newmap
, *oldmap
;
8137 for (id
= super
->devlist
; id
; id
= id
->next
) {
8138 if (id
->index
== dev_id
)
8147 /* Copy the dev, but not (all of) the map */
8148 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
8149 oldmap
= get_imsm_map(id
->dev
, MAP_0
);
8150 newmap
= get_imsm_map(newdev
, MAP_0
);
8151 /* Copy the current map */
8152 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8153 /* update one device only
8155 if (devices_to_reshape
) {
8156 dprintf("imsm: modifying subdev: %i\n",
8158 devices_to_reshape
--;
8159 newdev
->vol
.migr_state
= 1;
8160 newdev
->vol
.curr_migr_unit
= 0;
8161 set_migr_type(newdev
, MIGR_GEN_MIGR
);
8162 newmap
->num_members
= u
->new_raid_disks
;
8163 for (i
= 0; i
< delta_disks
; i
++) {
8164 set_imsm_ord_tbl_ent(newmap
,
8165 u
->old_raid_disks
+ i
,
8166 u
->old_raid_disks
+ i
);
8168 /* New map is correct, now need to save old map
8170 newmap
= get_imsm_map(newdev
, MAP_1
);
8171 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8173 imsm_set_array_size(newdev
, -1);
8176 sp
= (void **)id
->dev
;
8181 /* Clear migration record */
8182 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
8185 *space_list
= tofree
;
8188 update_reshape_exit
:
8193 static int apply_takeover_update(struct imsm_update_takeover
*u
,
8194 struct intel_super
*super
,
8197 struct imsm_dev
*dev
= NULL
;
8198 struct intel_dev
*dv
;
8199 struct imsm_dev
*dev_new
;
8200 struct imsm_map
*map
;
8204 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
8205 if (dv
->index
== (unsigned int)u
->subarray
) {
8213 map
= get_imsm_map(dev
, MAP_0
);
8215 if (u
->direction
== R10_TO_R0
) {
8216 /* Number of failed disks must be half of initial disk number */
8217 if (imsm_count_failed(super
, dev
, MAP_0
) !=
8218 (map
->num_members
/ 2))
8221 /* iterate through devices to mark removed disks as spare */
8222 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8223 if (dm
->disk
.status
& FAILED_DISK
) {
8224 int idx
= dm
->index
;
8225 /* update indexes on the disk list */
8226 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
8227 the index values will end up being correct.... NB */
8228 for (du
= super
->disks
; du
; du
= du
->next
)
8229 if (du
->index
> idx
)
8231 /* mark as spare disk */
8236 map
->num_members
= map
->num_members
/ 2;
8237 map
->map_state
= IMSM_T_STATE_NORMAL
;
8238 map
->num_domains
= 1;
8239 map
->raid_level
= 0;
8240 map
->failed_disk_num
= -1;
8243 if (u
->direction
== R0_TO_R10
) {
8245 /* update slots in current disk list */
8246 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8250 /* create new *missing* disks */
8251 for (i
= 0; i
< map
->num_members
; i
++) {
8252 space
= *space_list
;
8255 *space_list
= *space
;
8257 memcpy(du
, super
->disks
, sizeof(*du
));
8261 du
->index
= (i
* 2) + 1;
8262 sprintf((char *)du
->disk
.serial
,
8263 " MISSING_%d", du
->index
);
8264 sprintf((char *)du
->serial
,
8265 "MISSING_%d", du
->index
);
8266 du
->next
= super
->missing
;
8267 super
->missing
= du
;
8269 /* create new dev and map */
8270 space
= *space_list
;
8273 *space_list
= *space
;
8274 dev_new
= (void *)space
;
8275 memcpy(dev_new
, dev
, sizeof(*dev
));
8276 /* update new map */
8277 map
= get_imsm_map(dev_new
, MAP_0
);
8278 map
->num_members
= map
->num_members
* 2;
8279 map
->map_state
= IMSM_T_STATE_DEGRADED
;
8280 map
->num_domains
= 2;
8281 map
->raid_level
= 1;
8282 /* replace dev<->dev_new */
8285 /* update disk order table */
8286 for (du
= super
->disks
; du
; du
= du
->next
)
8288 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8289 for (du
= super
->missing
; du
; du
= du
->next
)
8290 if (du
->index
>= 0) {
8291 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8292 mark_missing(dv
->dev
, &du
->disk
, du
->index
);
8298 static void imsm_process_update(struct supertype
*st
,
8299 struct metadata_update
*update
)
8302 * crack open the metadata_update envelope to find the update record
8303 * update can be one of:
8304 * update_reshape_container_disks - all the arrays in the container
8305 * are being reshaped to have more devices. We need to mark
8306 * the arrays for general migration and convert selected spares
8307 * into active devices.
8308 * update_activate_spare - a spare device has replaced a failed
8309 * device in an array, update the disk_ord_tbl. If this disk is
8310 * present in all member arrays then also clear the SPARE_DISK
8312 * update_create_array
8314 * update_rename_array
8315 * update_add_remove_disk
8317 struct intel_super
*super
= st
->sb
;
8318 struct imsm_super
*mpb
;
8319 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
8321 /* update requires a larger buf but the allocation failed */
8322 if (super
->next_len
&& !super
->next_buf
) {
8323 super
->next_len
= 0;
8327 if (super
->next_buf
) {
8328 memcpy(super
->next_buf
, super
->buf
, super
->len
);
8330 super
->len
= super
->next_len
;
8331 super
->buf
= super
->next_buf
;
8333 super
->next_len
= 0;
8334 super
->next_buf
= NULL
;
8337 mpb
= super
->anchor
;
8340 case update_general_migration_checkpoint
: {
8341 struct intel_dev
*id
;
8342 struct imsm_update_general_migration_checkpoint
*u
=
8343 (void *)update
->buf
;
8345 dprintf("imsm: process_update() "
8346 "for update_general_migration_checkpoint called\n");
8348 /* find device under general migration */
8349 for (id
= super
->devlist
; id
; id
= id
->next
) {
8350 if (is_gen_migration(id
->dev
)) {
8351 id
->dev
->vol
.curr_migr_unit
=
8352 __cpu_to_le32(u
->curr_migr_unit
);
8353 super
->updates_pending
++;
8358 case update_takeover
: {
8359 struct imsm_update_takeover
*u
= (void *)update
->buf
;
8360 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
8361 imsm_update_version_info(super
);
8362 super
->updates_pending
++;
8367 case update_reshape_container_disks
: {
8368 struct imsm_update_reshape
*u
= (void *)update
->buf
;
8369 if (apply_reshape_container_disks_update(
8370 u
, super
, &update
->space_list
))
8371 super
->updates_pending
++;
8374 case update_reshape_migration
: {
8375 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
8376 if (apply_reshape_migration_update(
8377 u
, super
, &update
->space_list
))
8378 super
->updates_pending
++;
8381 case update_size_change
: {
8382 struct imsm_update_size_change
*u
= (void *)update
->buf
;
8383 if (apply_size_change_update(u
, super
))
8384 super
->updates_pending
++;
8387 case update_activate_spare
: {
8388 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
8389 if (apply_update_activate_spare(u
, super
, st
->arrays
))
8390 super
->updates_pending
++;
8393 case update_create_array
: {
8394 /* someone wants to create a new array, we need to be aware of
8395 * a few races/collisions:
8396 * 1/ 'Create' called by two separate instances of mdadm
8397 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
8398 * devices that have since been assimilated via
8400 * In the event this update can not be carried out mdadm will
8401 * (FIX ME) notice that its update did not take hold.
8403 struct imsm_update_create_array
*u
= (void *) update
->buf
;
8404 struct intel_dev
*dv
;
8405 struct imsm_dev
*dev
;
8406 struct imsm_map
*map
, *new_map
;
8407 unsigned long long start
, end
;
8408 unsigned long long new_start
, new_end
;
8410 struct disk_info
*inf
;
8413 /* handle racing creates: first come first serve */
8414 if (u
->dev_idx
< mpb
->num_raid_devs
) {
8415 dprintf("%s: subarray %d already defined\n",
8416 __func__
, u
->dev_idx
);
8420 /* check update is next in sequence */
8421 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
8422 dprintf("%s: can not create array %d expected index %d\n",
8423 __func__
, u
->dev_idx
, mpb
->num_raid_devs
);
8427 new_map
= get_imsm_map(&u
->dev
, MAP_0
);
8428 new_start
= pba_of_lba0(new_map
);
8429 new_end
= new_start
+ blocks_per_member(new_map
);
8430 inf
= get_disk_info(u
);
8432 /* handle activate_spare versus create race:
8433 * check to make sure that overlapping arrays do not include
8436 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8437 dev
= get_imsm_dev(super
, i
);
8438 map
= get_imsm_map(dev
, MAP_0
);
8439 start
= pba_of_lba0(map
);
8440 end
= start
+ blocks_per_member(map
);
8441 if ((new_start
>= start
&& new_start
<= end
) ||
8442 (start
>= new_start
&& start
<= new_end
))
8447 if (disks_overlap(super
, i
, u
)) {
8448 dprintf("%s: arrays overlap\n", __func__
);
8453 /* check that prepare update was successful */
8454 if (!update
->space
) {
8455 dprintf("%s: prepare update failed\n", __func__
);
8459 /* check that all disks are still active before committing
8460 * changes. FIXME: could we instead handle this by creating a
8461 * degraded array? That's probably not what the user expects,
8462 * so better to drop this update on the floor.
8464 for (i
= 0; i
< new_map
->num_members
; i
++) {
8465 dl
= serial_to_dl(inf
[i
].serial
, super
);
8467 dprintf("%s: disk disappeared\n", __func__
);
8472 super
->updates_pending
++;
8474 /* convert spares to members and fixup ord_tbl */
8475 for (i
= 0; i
< new_map
->num_members
; i
++) {
8476 dl
= serial_to_dl(inf
[i
].serial
, super
);
8477 if (dl
->index
== -1) {
8478 dl
->index
= mpb
->num_disks
;
8480 dl
->disk
.status
|= CONFIGURED_DISK
;
8481 dl
->disk
.status
&= ~SPARE_DISK
;
8483 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
8488 update
->space
= NULL
;
8489 imsm_copy_dev(dev
, &u
->dev
);
8490 dv
->index
= u
->dev_idx
;
8491 dv
->next
= super
->devlist
;
8492 super
->devlist
= dv
;
8493 mpb
->num_raid_devs
++;
8495 imsm_update_version_info(super
);
8498 /* mdmon knows how to release update->space, but not
8499 * ((struct intel_dev *) update->space)->dev
8501 if (update
->space
) {
8507 case update_kill_array
: {
8508 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
8509 int victim
= u
->dev_idx
;
8510 struct active_array
*a
;
8511 struct intel_dev
**dp
;
8512 struct imsm_dev
*dev
;
8514 /* sanity check that we are not affecting the uuid of
8515 * active arrays, or deleting an active array
8517 * FIXME when immutable ids are available, but note that
8518 * we'll also need to fixup the invalidated/active
8519 * subarray indexes in mdstat
8521 for (a
= st
->arrays
; a
; a
= a
->next
)
8522 if (a
->info
.container_member
>= victim
)
8524 /* by definition if mdmon is running at least one array
8525 * is active in the container, so checking
8526 * mpb->num_raid_devs is just extra paranoia
8528 dev
= get_imsm_dev(super
, victim
);
8529 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
8530 dprintf("failed to delete subarray-%d\n", victim
);
8534 for (dp
= &super
->devlist
; *dp
;)
8535 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
8538 if ((*dp
)->index
> (unsigned)victim
)
8542 mpb
->num_raid_devs
--;
8543 super
->updates_pending
++;
8546 case update_rename_array
: {
8547 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
8548 char name
[MAX_RAID_SERIAL_LEN
+1];
8549 int target
= u
->dev_idx
;
8550 struct active_array
*a
;
8551 struct imsm_dev
*dev
;
8553 /* sanity check that we are not affecting the uuid of
8556 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
8557 name
[MAX_RAID_SERIAL_LEN
] = '\0';
8558 for (a
= st
->arrays
; a
; a
= a
->next
)
8559 if (a
->info
.container_member
== target
)
8561 dev
= get_imsm_dev(super
, u
->dev_idx
);
8562 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
8563 dprintf("failed to rename subarray-%d\n", target
);
8567 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
8568 super
->updates_pending
++;
8571 case update_add_remove_disk
: {
8572 /* we may be able to repair some arrays if disks are
8573 * being added, check teh status of add_remove_disk
8574 * if discs has been added.
8576 if (add_remove_disk_update(super
)) {
8577 struct active_array
*a
;
8579 super
->updates_pending
++;
8580 for (a
= st
->arrays
; a
; a
= a
->next
)
8581 a
->check_degraded
= 1;
8586 fprintf(stderr
, "error: unsuported process update type:"
8587 "(type: %d)\n", type
);
8591 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
);
8593 static void imsm_prepare_update(struct supertype
*st
,
8594 struct metadata_update
*update
)
8597 * Allocate space to hold new disk entries, raid-device entries or a new
8598 * mpb if necessary. The manager synchronously waits for updates to
8599 * complete in the monitor, so new mpb buffers allocated here can be
8600 * integrated by the monitor thread without worrying about live pointers
8601 * in the manager thread.
8603 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
8604 struct intel_super
*super
= st
->sb
;
8605 struct imsm_super
*mpb
= super
->anchor
;
8610 case update_general_migration_checkpoint
:
8611 dprintf("imsm: prepare_update() "
8612 "for update_general_migration_checkpoint called\n");
8614 case update_takeover
: {
8615 struct imsm_update_takeover
*u
= (void *)update
->buf
;
8616 if (u
->direction
== R0_TO_R10
) {
8617 void **tail
= (void **)&update
->space_list
;
8618 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
8619 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8620 int num_members
= map
->num_members
;
8623 /* allocate memory for added disks */
8624 for (i
= 0; i
< num_members
; i
++) {
8625 size
= sizeof(struct dl
);
8626 space
= xmalloc(size
);
8631 /* allocate memory for new device */
8632 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
8633 (num_members
* sizeof(__u32
));
8634 space
= xmalloc(size
);
8638 len
= disks_to_mpb_size(num_members
* 2);
8643 case update_reshape_container_disks
: {
8644 /* Every raid device in the container is about to
8645 * gain some more devices, and we will enter a
8647 * So each 'imsm_map' will be bigger, and the imsm_vol
8648 * will now hold 2 of them.
8649 * Thus we need new 'struct imsm_dev' allocations sized
8650 * as sizeof_imsm_dev but with more devices in both maps.
8652 struct imsm_update_reshape
*u
= (void *)update
->buf
;
8653 struct intel_dev
*dl
;
8654 void **space_tail
= (void**)&update
->space_list
;
8656 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
8658 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
8659 int size
= sizeof_imsm_dev(dl
->dev
, 1);
8661 if (u
->new_raid_disks
> u
->old_raid_disks
)
8662 size
+= sizeof(__u32
)*2*
8663 (u
->new_raid_disks
- u
->old_raid_disks
);
8670 len
= disks_to_mpb_size(u
->new_raid_disks
);
8671 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
8674 case update_reshape_migration
: {
8675 /* for migration level 0->5 we need to add disks
8676 * so the same as for container operation we will copy
8677 * device to the bigger location.
8678 * in memory prepared device and new disk area are prepared
8679 * for usage in process update
8681 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
8682 struct intel_dev
*id
;
8683 void **space_tail
= (void **)&update
->space_list
;
8686 int current_level
= -1;
8688 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
8690 /* add space for bigger array in update
8692 for (id
= super
->devlist
; id
; id
= id
->next
) {
8693 if (id
->index
== (unsigned)u
->subdev
) {
8694 size
= sizeof_imsm_dev(id
->dev
, 1);
8695 if (u
->new_raid_disks
> u
->old_raid_disks
)
8696 size
+= sizeof(__u32
)*2*
8697 (u
->new_raid_disks
- u
->old_raid_disks
);
8705 if (update
->space_list
== NULL
)
8708 /* add space for disk in update
8710 size
= sizeof(struct dl
);
8716 /* add spare device to update
8718 for (id
= super
->devlist
; id
; id
= id
->next
)
8719 if (id
->index
== (unsigned)u
->subdev
) {
8720 struct imsm_dev
*dev
;
8721 struct imsm_map
*map
;
8723 dev
= get_imsm_dev(super
, u
->subdev
);
8724 map
= get_imsm_map(dev
, MAP_0
);
8725 current_level
= map
->raid_level
;
8728 if ((u
->new_level
== 5) && (u
->new_level
!= current_level
)) {
8729 struct mdinfo
*spares
;
8731 spares
= get_spares_for_grow(st
);
8739 makedev(dev
->disk
.major
,
8741 dl
= get_disk_super(super
,
8744 dl
->index
= u
->old_raid_disks
;
8750 len
= disks_to_mpb_size(u
->new_raid_disks
);
8751 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
8754 case update_size_change
: {
8757 case update_create_array
: {
8758 struct imsm_update_create_array
*u
= (void *) update
->buf
;
8759 struct intel_dev
*dv
;
8760 struct imsm_dev
*dev
= &u
->dev
;
8761 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8763 struct disk_info
*inf
;
8767 inf
= get_disk_info(u
);
8768 len
= sizeof_imsm_dev(dev
, 1);
8769 /* allocate a new super->devlist entry */
8770 dv
= xmalloc(sizeof(*dv
));
8771 dv
->dev
= xmalloc(len
);
8774 /* count how many spares will be converted to members */
8775 for (i
= 0; i
< map
->num_members
; i
++) {
8776 dl
= serial_to_dl(inf
[i
].serial
, super
);
8778 /* hmm maybe it failed?, nothing we can do about
8783 if (count_memberships(dl
, super
) == 0)
8786 len
+= activate
* sizeof(struct imsm_disk
);
8793 /* check if we need a larger metadata buffer */
8794 if (super
->next_buf
)
8795 buf_len
= super
->next_len
;
8797 buf_len
= super
->len
;
8799 if (__le32_to_cpu(mpb
->mpb_size
) + len
> buf_len
) {
8800 /* ok we need a larger buf than what is currently allocated
8801 * if this allocation fails process_update will notice that
8802 * ->next_len is set and ->next_buf is NULL
8804 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + len
, 512);
8805 if (super
->next_buf
)
8806 free(super
->next_buf
);
8808 super
->next_len
= buf_len
;
8809 if (posix_memalign(&super
->next_buf
, 512, buf_len
) == 0)
8810 memset(super
->next_buf
, 0, buf_len
);
8812 super
->next_buf
= NULL
;
8816 /* must be called while manager is quiesced */
8817 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
8819 struct imsm_super
*mpb
= super
->anchor
;
8821 struct imsm_dev
*dev
;
8822 struct imsm_map
*map
;
8823 int i
, j
, num_members
;
8826 dprintf("%s: deleting device[%d] from imsm_super\n",
8829 /* shift all indexes down one */
8830 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
8831 if (iter
->index
> (int)index
)
8833 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
8834 if (iter
->index
> (int)index
)
8837 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8838 dev
= get_imsm_dev(super
, i
);
8839 map
= get_imsm_map(dev
, MAP_0
);
8840 num_members
= map
->num_members
;
8841 for (j
= 0; j
< num_members
; j
++) {
8842 /* update ord entries being careful not to propagate
8843 * ord-flags to the first map
8845 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_X
);
8847 if (ord_to_idx(ord
) <= index
)
8850 map
= get_imsm_map(dev
, MAP_0
);
8851 set_imsm_ord_tbl_ent(map
, j
, ord_to_idx(ord
- 1));
8852 map
= get_imsm_map(dev
, MAP_1
);
8854 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
8859 super
->updates_pending
++;
8861 struct dl
*dl
= *dlp
;
8863 *dlp
= (*dlp
)->next
;
8864 __free_imsm_disk(dl
);
8867 #endif /* MDASSEMBLE */
8869 static void close_targets(int *targets
, int new_disks
)
8876 for (i
= 0; i
< new_disks
; i
++) {
8877 if (targets
[i
] >= 0) {
8884 static int imsm_get_allowed_degradation(int level
, int raid_disks
,
8885 struct intel_super
*super
,
8886 struct imsm_dev
*dev
)
8892 struct imsm_map
*map
;
8895 ret_val
= raid_disks
/2;
8896 /* check map if all disks pairs not failed
8899 map
= get_imsm_map(dev
, MAP_0
);
8900 for (i
= 0; i
< ret_val
; i
++) {
8901 int degradation
= 0;
8902 if (get_imsm_disk(super
, i
) == NULL
)
8904 if (get_imsm_disk(super
, i
+ 1) == NULL
)
8906 if (degradation
== 2)
8909 map
= get_imsm_map(dev
, MAP_1
);
8910 /* if there is no second map
8911 * result can be returned
8915 /* check degradation in second map
8917 for (i
= 0; i
< ret_val
; i
++) {
8918 int degradation
= 0;
8919 if (get_imsm_disk(super
, i
) == NULL
)
8921 if (get_imsm_disk(super
, i
+ 1) == NULL
)
8923 if (degradation
== 2)
8938 /*******************************************************************************
8939 * Function: open_backup_targets
8940 * Description: Function opens file descriptors for all devices given in
8943 * info : general array info
8944 * raid_disks : number of disks
8945 * raid_fds : table of device's file descriptors
8946 * super : intel super for raid10 degradation check
8947 * dev : intel device for raid10 degradation check
8951 ******************************************************************************/
8952 int open_backup_targets(struct mdinfo
*info
, int raid_disks
, int *raid_fds
,
8953 struct intel_super
*super
, struct imsm_dev
*dev
)
8959 for (i
= 0; i
< raid_disks
; i
++)
8962 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
8965 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
)) {
8966 dprintf("disk is faulty!!\n");
8970 if ((sd
->disk
.raid_disk
>= raid_disks
) ||
8971 (sd
->disk
.raid_disk
< 0))
8974 dn
= map_dev(sd
->disk
.major
,
8976 raid_fds
[sd
->disk
.raid_disk
] = dev_open(dn
, O_RDWR
);
8977 if (raid_fds
[sd
->disk
.raid_disk
] < 0) {
8978 fprintf(stderr
, "cannot open component\n");
8983 /* check if maximum array degradation level is not exceeded
8985 if ((raid_disks
- opened
) >
8986 imsm_get_allowed_degradation(info
->new_level
,
8989 fprintf(stderr
, "Not enough disks can be opened.\n");
8990 close_targets(raid_fds
, raid_disks
);
8997 /*******************************************************************************
8998 * Function: init_migr_record_imsm
8999 * Description: Function inits imsm migration record
9001 * super : imsm internal array info
9002 * dev : device under migration
9003 * info : general array info to find the smallest device
9006 ******************************************************************************/
9007 void init_migr_record_imsm(struct supertype
*st
, struct imsm_dev
*dev
,
9008 struct mdinfo
*info
)
9010 struct intel_super
*super
= st
->sb
;
9011 struct migr_record
*migr_rec
= super
->migr_rec
;
9013 unsigned long long dsize
, dev_sectors
;
9014 long long unsigned min_dev_sectors
= -1LLU;
9018 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
9019 struct imsm_map
*map_src
= get_imsm_map(dev
, MAP_1
);
9020 unsigned long long num_migr_units
;
9021 unsigned long long array_blocks
;
9023 memset(migr_rec
, 0, sizeof(struct migr_record
));
9024 migr_rec
->family_num
= __cpu_to_le32(super
->anchor
->family_num
);
9026 /* only ascending reshape supported now */
9027 migr_rec
->ascending_migr
= __cpu_to_le32(1);
9029 migr_rec
->dest_depth_per_unit
= GEN_MIGR_AREA_SIZE
/
9030 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
9031 migr_rec
->dest_depth_per_unit
*=
9032 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
9033 new_data_disks
= imsm_num_data_members(dev
, MAP_0
);
9034 migr_rec
->blocks_per_unit
=
9035 __cpu_to_le32(migr_rec
->dest_depth_per_unit
* new_data_disks
);
9036 migr_rec
->dest_depth_per_unit
=
9037 __cpu_to_le32(migr_rec
->dest_depth_per_unit
);
9038 array_blocks
= info
->component_size
* new_data_disks
;
9040 array_blocks
/ __le32_to_cpu(migr_rec
->blocks_per_unit
);
9042 if (array_blocks
% __le32_to_cpu(migr_rec
->blocks_per_unit
))
9044 migr_rec
->num_migr_units
= __cpu_to_le32(num_migr_units
);
9046 migr_rec
->post_migr_vol_cap
= dev
->size_low
;
9047 migr_rec
->post_migr_vol_cap_hi
= dev
->size_high
;
9050 /* Find the smallest dev */
9051 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
9052 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
9053 fd
= dev_open(nm
, O_RDONLY
);
9056 get_dev_size(fd
, NULL
, &dsize
);
9057 dev_sectors
= dsize
/ 512;
9058 if (dev_sectors
< min_dev_sectors
)
9059 min_dev_sectors
= dev_sectors
;
9062 migr_rec
->ckpt_area_pba
= __cpu_to_le32(min_dev_sectors
-
9063 RAID_DISK_RESERVED_BLOCKS_IMSM_HI
);
9065 write_imsm_migr_rec(st
);
9070 /*******************************************************************************
9071 * Function: save_backup_imsm
9072 * Description: Function saves critical data stripes to Migration Copy Area
9073 * and updates the current migration unit status.
9074 * Use restore_stripes() to form a destination stripe,
9075 * and to write it to the Copy Area.
9077 * st : supertype information
9078 * dev : imsm device that backup is saved for
9079 * info : general array info
9080 * buf : input buffer
9081 * length : length of data to backup (blocks_per_unit)
9085 ******************************************************************************/
9086 int save_backup_imsm(struct supertype
*st
,
9087 struct imsm_dev
*dev
,
9088 struct mdinfo
*info
,
9093 struct intel_super
*super
= st
->sb
;
9094 unsigned long long *target_offsets
= NULL
;
9095 int *targets
= NULL
;
9097 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
9098 int new_disks
= map_dest
->num_members
;
9099 int dest_layout
= 0;
9101 unsigned long long start
;
9102 int data_disks
= imsm_num_data_members(dev
, MAP_0
);
9104 targets
= xmalloc(new_disks
* sizeof(int));
9106 for (i
= 0; i
< new_disks
; i
++)
9109 target_offsets
= xcalloc(new_disks
, sizeof(unsigned long long));
9111 start
= info
->reshape_progress
* 512;
9112 for (i
= 0; i
< new_disks
; i
++) {
9113 target_offsets
[i
] = (unsigned long long)
9114 __le32_to_cpu(super
->migr_rec
->ckpt_area_pba
) * 512;
9115 /* move back copy area adderss, it will be moved forward
9116 * in restore_stripes() using start input variable
9118 target_offsets
[i
] -= start
/data_disks
;
9121 if (open_backup_targets(info
, new_disks
, targets
,
9125 dest_layout
= imsm_level_to_layout(map_dest
->raid_level
);
9126 dest_chunk
= __le16_to_cpu(map_dest
->blocks_per_strip
) * 512;
9128 if (restore_stripes(targets
, /* list of dest devices */
9129 target_offsets
, /* migration record offsets */
9132 map_dest
->raid_level
,
9134 -1, /* source backup file descriptor */
9135 0, /* input buf offset
9136 * always 0 buf is already offseted */
9140 pr_err("Error restoring stripes\n");
9148 close_targets(targets
, new_disks
);
9151 free(target_offsets
);
9156 /*******************************************************************************
9157 * Function: save_checkpoint_imsm
9158 * Description: Function called for current unit status update
9159 * in the migration record. It writes it to disk.
9161 * super : imsm internal array info
9162 * info : general array info
9166 * 2: failure, means no valid migration record
9167 * / no general migration in progress /
9168 ******************************************************************************/
9169 int save_checkpoint_imsm(struct supertype
*st
, struct mdinfo
*info
, int state
)
9171 struct intel_super
*super
= st
->sb
;
9172 unsigned long long blocks_per_unit
;
9173 unsigned long long curr_migr_unit
;
9175 if (load_imsm_migr_rec(super
, info
) != 0) {
9176 dprintf("imsm: ERROR: Cannot read migration record "
9177 "for checkpoint save.\n");
9181 blocks_per_unit
= __le32_to_cpu(super
->migr_rec
->blocks_per_unit
);
9182 if (blocks_per_unit
== 0) {
9183 dprintf("imsm: no migration in progress.\n");
9186 curr_migr_unit
= info
->reshape_progress
/ blocks_per_unit
;
9187 /* check if array is alligned to copy area
9188 * if it is not alligned, add one to current migration unit value
9189 * this can happend on array reshape finish only
9191 if (info
->reshape_progress
% blocks_per_unit
)
9194 super
->migr_rec
->curr_migr_unit
=
9195 __cpu_to_le32(curr_migr_unit
);
9196 super
->migr_rec
->rec_status
= __cpu_to_le32(state
);
9197 super
->migr_rec
->dest_1st_member_lba
=
9198 __cpu_to_le32(curr_migr_unit
*
9199 __le32_to_cpu(super
->migr_rec
->dest_depth_per_unit
));
9200 if (write_imsm_migr_rec(st
) < 0) {
9201 dprintf("imsm: Cannot write migration record "
9202 "outside backup area\n");
9209 /*******************************************************************************
9210 * Function: recover_backup_imsm
9211 * Description: Function recovers critical data from the Migration Copy Area
9212 * while assembling an array.
9214 * super : imsm internal array info
9215 * info : general array info
9217 * 0 : success (or there is no data to recover)
9219 ******************************************************************************/
9220 int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
)
9222 struct intel_super
*super
= st
->sb
;
9223 struct migr_record
*migr_rec
= super
->migr_rec
;
9224 struct imsm_map
*map_dest
= NULL
;
9225 struct intel_dev
*id
= NULL
;
9226 unsigned long long read_offset
;
9227 unsigned long long write_offset
;
9229 int *targets
= NULL
;
9230 int new_disks
, i
, err
;
9233 unsigned long curr_migr_unit
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
9234 unsigned long num_migr_units
= __le32_to_cpu(migr_rec
->num_migr_units
);
9236 int skipped_disks
= 0;
9238 err
= sysfs_get_str(info
, NULL
, "array_state", (char *)buffer
, 20);
9242 /* recover data only during assemblation */
9243 if (strncmp(buffer
, "inactive", 8) != 0)
9245 /* no data to recover */
9246 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
9248 if (curr_migr_unit
>= num_migr_units
)
9251 /* find device during reshape */
9252 for (id
= super
->devlist
; id
; id
= id
->next
)
9253 if (is_gen_migration(id
->dev
))
9258 map_dest
= get_imsm_map(id
->dev
, MAP_0
);
9259 new_disks
= map_dest
->num_members
;
9261 read_offset
= (unsigned long long)
9262 __le32_to_cpu(migr_rec
->ckpt_area_pba
) * 512;
9264 write_offset
= ((unsigned long long)
9265 __le32_to_cpu(migr_rec
->dest_1st_member_lba
) +
9266 pba_of_lba0(map_dest
)) * 512;
9268 unit_len
= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
9269 if (posix_memalign((void **)&buf
, 512, unit_len
) != 0)
9271 targets
= xcalloc(new_disks
, sizeof(int));
9273 if (open_backup_targets(info
, new_disks
, targets
, super
, id
->dev
)) {
9274 pr_err("Cannot open some devices belonging to array.\n");
9278 for (i
= 0; i
< new_disks
; i
++) {
9279 if (targets
[i
] < 0) {
9283 if (lseek64(targets
[i
], read_offset
, SEEK_SET
) < 0) {
9284 pr_err("Cannot seek to block: %s\n",
9289 if ((unsigned)read(targets
[i
], buf
, unit_len
) != unit_len
) {
9290 pr_err("Cannot read copy area block: %s\n",
9295 if (lseek64(targets
[i
], write_offset
, SEEK_SET
) < 0) {
9296 pr_err("Cannot seek to block: %s\n",
9301 if ((unsigned)write(targets
[i
], buf
, unit_len
) != unit_len
) {
9302 pr_err("Cannot restore block: %s\n",
9309 if (skipped_disks
> imsm_get_allowed_degradation(info
->new_level
,
9313 pr_err("Cannot restore data from backup."
9314 " Too many failed disks\n");
9318 if (save_checkpoint_imsm(st
, info
, UNIT_SRC_NORMAL
)) {
9319 /* ignore error == 2, this can mean end of reshape here
9321 dprintf("imsm: Cannot write checkpoint to "
9322 "migration record (UNIT_SRC_NORMAL) during restart\n");
9328 for (i
= 0; i
< new_disks
; i
++)
9337 static char disk_by_path
[] = "/dev/disk/by-path/";
9339 static const char *imsm_get_disk_controller_domain(const char *path
)
9341 char disk_path
[PATH_MAX
];
9345 strncpy(disk_path
, disk_by_path
, PATH_MAX
- 1);
9346 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
9347 if (stat(disk_path
, &st
) == 0) {
9348 struct sys_dev
* hba
;
9351 path
= devt_to_devpath(st
.st_rdev
);
9354 hba
= find_disk_attached_hba(-1, path
);
9355 if (hba
&& hba
->type
== SYS_DEV_SAS
)
9357 else if (hba
&& hba
->type
== SYS_DEV_SATA
)
9361 dprintf("path: %s hba: %s attached: %s\n",
9362 path
, (hba
) ? hba
->path
: "NULL", drv
);
9370 static int imsm_find_array_minor_by_subdev(int subdev
, int container
, int *minor
)
9372 char subdev_name
[20];
9373 struct mdstat_ent
*mdstat
;
9375 sprintf(subdev_name
, "%d", subdev
);
9376 mdstat
= mdstat_by_subdev(subdev_name
, container
);
9380 *minor
= mdstat
->devnum
;
9381 free_mdstat(mdstat
);
9385 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
9386 struct geo_params
*geo
,
9387 int *old_raid_disks
,
9390 /* currently we only support increasing the number of devices
9391 * for a container. This increases the number of device for each
9392 * member array. They must all be RAID0 or RAID5.
9395 struct mdinfo
*info
, *member
;
9396 int devices_that_can_grow
= 0;
9398 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): "
9399 "st->devnum = (%i)\n",
9402 if (geo
->size
> 0 ||
9403 geo
->level
!= UnSet
||
9404 geo
->layout
!= UnSet
||
9405 geo
->chunksize
!= 0 ||
9406 geo
->raid_disks
== UnSet
) {
9407 dprintf("imsm: Container operation is allowed for "
9408 "raid disks number change only.\n");
9412 if (direction
== ROLLBACK_METADATA_CHANGES
) {
9413 dprintf("imsm: Metadata changes rollback is not supported for "
9414 "container operation.\n");
9418 info
= container_content_imsm(st
, NULL
);
9419 for (member
= info
; member
; member
= member
->next
) {
9423 dprintf("imsm: checking device_num: %i\n",
9424 member
->container_member
);
9426 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
9427 /* we work on container for Online Capacity Expansion
9428 * only so raid_disks has to grow
9430 dprintf("imsm: for container operation raid disks "
9431 "increase is required\n");
9435 if ((info
->array
.level
!= 0) &&
9436 (info
->array
.level
!= 5)) {
9437 /* we cannot use this container with other raid level
9439 dprintf("imsm: for container operation wrong"
9440 " raid level (%i) detected\n",
9444 /* check for platform support
9445 * for this raid level configuration
9447 struct intel_super
*super
= st
->sb
;
9448 if (!is_raid_level_supported(super
->orom
,
9449 member
->array
.level
,
9451 dprintf("platform does not support raid%d with"
9455 geo
->raid_disks
> 1 ? "s" : "");
9458 /* check if component size is aligned to chunk size
9460 if (info
->component_size
%
9461 (info
->array
.chunk_size
/512)) {
9462 dprintf("Component size is not aligned to "
9468 if (*old_raid_disks
&&
9469 info
->array
.raid_disks
!= *old_raid_disks
)
9471 *old_raid_disks
= info
->array
.raid_disks
;
9473 /* All raid5 and raid0 volumes in container
9474 * have to be ready for Online Capacity Expansion
9475 * so they need to be assembled. We have already
9476 * checked that no recovery etc is happening.
9478 result
= imsm_find_array_minor_by_subdev(member
->container_member
,
9482 dprintf("imsm: cannot find array\n");
9485 devices_that_can_grow
++;
9488 if (!member
&& devices_that_can_grow
)
9492 dprintf("\tContainer operation allowed\n");
9494 dprintf("\tError: %i\n", ret_val
);
9499 /* Function: get_spares_for_grow
9500 * Description: Allocates memory and creates list of spare devices
9501 * avaliable in container. Checks if spare drive size is acceptable.
9502 * Parameters: Pointer to the supertype structure
9503 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
9506 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
9508 unsigned long long min_size
= min_acceptable_spare_size_imsm(st
);
9509 return container_choose_spares(st
, min_size
, NULL
, NULL
, NULL
, 0);
9512 /******************************************************************************
9513 * function: imsm_create_metadata_update_for_reshape
9514 * Function creates update for whole IMSM container.
9516 ******************************************************************************/
9517 static int imsm_create_metadata_update_for_reshape(
9518 struct supertype
*st
,
9519 struct geo_params
*geo
,
9521 struct imsm_update_reshape
**updatep
)
9523 struct intel_super
*super
= st
->sb
;
9524 struct imsm_super
*mpb
= super
->anchor
;
9525 int update_memory_size
= 0;
9526 struct imsm_update_reshape
*u
= NULL
;
9527 struct mdinfo
*spares
= NULL
;
9529 int delta_disks
= 0;
9532 dprintf("imsm_update_metadata_for_reshape(enter) raid_disks = %i\n",
9535 delta_disks
= geo
->raid_disks
- old_raid_disks
;
9537 /* size of all update data without anchor */
9538 update_memory_size
= sizeof(struct imsm_update_reshape
);
9540 /* now add space for spare disks that we need to add. */
9541 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
9543 u
= xcalloc(1, update_memory_size
);
9544 u
->type
= update_reshape_container_disks
;
9545 u
->old_raid_disks
= old_raid_disks
;
9546 u
->new_raid_disks
= geo
->raid_disks
;
9548 /* now get spare disks list
9550 spares
= get_spares_for_grow(st
);
9553 || delta_disks
> spares
->array
.spare_disks
) {
9554 pr_err("imsm: ERROR: Cannot get spare devices "
9555 "for %s.\n", geo
->dev_name
);
9560 /* we have got spares
9561 * update disk list in imsm_disk list table in anchor
9563 dprintf("imsm: %i spares are available.\n\n",
9564 spares
->array
.spare_disks
);
9567 for (i
= 0; i
< delta_disks
; i
++) {
9572 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
9574 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
9575 dl
->index
= mpb
->num_disks
;
9585 dprintf("imsm: reshape update preparation :");
9586 if (i
== delta_disks
) {
9589 return update_memory_size
;
9592 dprintf(" Error\n");
9598 /******************************************************************************
9599 * function: imsm_create_metadata_update_for_size_change()
9600 * Creates update for IMSM array for array size change.
9602 ******************************************************************************/
9603 static int imsm_create_metadata_update_for_size_change(
9604 struct supertype
*st
,
9605 struct geo_params
*geo
,
9606 struct imsm_update_size_change
**updatep
)
9608 struct intel_super
*super
= st
->sb
;
9609 int update_memory_size
= 0;
9610 struct imsm_update_size_change
*u
= NULL
;
9612 dprintf("imsm_create_metadata_update_for_size_change(enter)"
9613 " New size = %llu\n", geo
->size
);
9615 /* size of all update data without anchor */
9616 update_memory_size
= sizeof(struct imsm_update_size_change
);
9618 u
= xcalloc(1, update_memory_size
);
9619 u
->type
= update_size_change
;
9620 u
->subdev
= super
->current_vol
;
9621 u
->new_size
= geo
->size
;
9623 dprintf("imsm: reshape update preparation : OK\n");
9626 return update_memory_size
;
9629 /******************************************************************************
9630 * function: imsm_create_metadata_update_for_migration()
9631 * Creates update for IMSM array.
9633 ******************************************************************************/
9634 static int imsm_create_metadata_update_for_migration(
9635 struct supertype
*st
,
9636 struct geo_params
*geo
,
9637 struct imsm_update_reshape_migration
**updatep
)
9639 struct intel_super
*super
= st
->sb
;
9640 int update_memory_size
= 0;
9641 struct imsm_update_reshape_migration
*u
= NULL
;
9642 struct imsm_dev
*dev
;
9643 int previous_level
= -1;
9645 dprintf("imsm_create_metadata_update_for_migration(enter)"
9646 " New Level = %i\n", geo
->level
);
9648 /* size of all update data without anchor */
9649 update_memory_size
= sizeof(struct imsm_update_reshape_migration
);
9651 u
= xcalloc(1, update_memory_size
);
9652 u
->type
= update_reshape_migration
;
9653 u
->subdev
= super
->current_vol
;
9654 u
->new_level
= geo
->level
;
9655 u
->new_layout
= geo
->layout
;
9656 u
->new_raid_disks
= u
->old_raid_disks
= geo
->raid_disks
;
9657 u
->new_disks
[0] = -1;
9658 u
->new_chunksize
= -1;
9660 dev
= get_imsm_dev(super
, u
->subdev
);
9662 struct imsm_map
*map
;
9664 map
= get_imsm_map(dev
, MAP_0
);
9666 int current_chunk_size
=
9667 __le16_to_cpu(map
->blocks_per_strip
) / 2;
9669 if (geo
->chunksize
!= current_chunk_size
) {
9670 u
->new_chunksize
= geo
->chunksize
/ 1024;
9672 "chunk size change from %i to %i\n",
9673 current_chunk_size
, u
->new_chunksize
);
9675 previous_level
= map
->raid_level
;
9678 if ((geo
->level
== 5) && (previous_level
== 0)) {
9679 struct mdinfo
*spares
= NULL
;
9681 u
->new_raid_disks
++;
9682 spares
= get_spares_for_grow(st
);
9683 if ((spares
== NULL
) || (spares
->array
.spare_disks
< 1)) {
9686 update_memory_size
= 0;
9687 dprintf("error: cannot get spare device "
9688 "for requested migration");
9693 dprintf("imsm: reshape update preparation : OK\n");
9696 return update_memory_size
;
9699 static void imsm_update_metadata_locally(struct supertype
*st
,
9702 struct metadata_update mu
;
9707 mu
.space_list
= NULL
;
9709 imsm_prepare_update(st
, &mu
);
9710 imsm_process_update(st
, &mu
);
9712 while (mu
.space_list
) {
9713 void **space
= mu
.space_list
;
9714 mu
.space_list
= *space
;
9719 /***************************************************************************
9720 * Function: imsm_analyze_change
9721 * Description: Function analyze change for single volume
9722 * and validate if transition is supported
9723 * Parameters: Geometry parameters, supertype structure,
9724 * metadata change direction (apply/rollback)
9725 * Returns: Operation type code on success, -1 if fail
9726 ****************************************************************************/
9727 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
9728 struct geo_params
*geo
,
9735 /* number of added/removed disks in operation result */
9736 int devNumChange
= 0;
9737 /* imsm compatible layout value for array geometry verification */
9738 int imsm_layout
= -1;
9740 struct imsm_dev
*dev
;
9741 struct intel_super
*super
;
9742 unsigned long long current_size
;
9743 unsigned long long free_size
;
9744 unsigned long long max_size
;
9747 getinfo_super_imsm_volume(st
, &info
, NULL
);
9748 if ((geo
->level
!= info
.array
.level
) &&
9749 (geo
->level
>= 0) &&
9750 (geo
->level
!= UnSet
)) {
9751 switch (info
.array
.level
) {
9753 if (geo
->level
== 5) {
9754 change
= CH_MIGRATION
;
9755 if (geo
->layout
!= ALGORITHM_LEFT_ASYMMETRIC
) {
9756 pr_err("Error. Requested Layout "
9757 "not supported (left-asymmetric layout "
9758 "is supported only)!\n");
9760 goto analyse_change_exit
;
9762 imsm_layout
= geo
->layout
;
9764 devNumChange
= 1; /* parity disk added */
9765 } else if (geo
->level
== 10) {
9766 change
= CH_TAKEOVER
;
9768 devNumChange
= 2; /* two mirrors added */
9769 imsm_layout
= 0x102; /* imsm supported layout */
9774 if (geo
->level
== 0) {
9775 change
= CH_TAKEOVER
;
9777 devNumChange
= -(geo
->raid_disks
/2);
9778 imsm_layout
= 0; /* imsm raid0 layout */
9783 pr_err("Error. Level Migration from %d to %d "
9785 info
.array
.level
, geo
->level
);
9786 goto analyse_change_exit
;
9789 geo
->level
= info
.array
.level
;
9791 if ((geo
->layout
!= info
.array
.layout
)
9792 && ((geo
->layout
!= UnSet
) && (geo
->layout
!= -1))) {
9793 change
= CH_MIGRATION
;
9794 if ((info
.array
.layout
== 0)
9795 && (info
.array
.level
== 5)
9796 && (geo
->layout
== 5)) {
9797 /* reshape 5 -> 4 */
9798 } else if ((info
.array
.layout
== 5)
9799 && (info
.array
.level
== 5)
9800 && (geo
->layout
== 0)) {
9801 /* reshape 4 -> 5 */
9805 pr_err("Error. Layout Migration from %d to %d "
9807 info
.array
.layout
, geo
->layout
);
9809 goto analyse_change_exit
;
9812 geo
->layout
= info
.array
.layout
;
9813 if (imsm_layout
== -1)
9814 imsm_layout
= info
.array
.layout
;
9817 if ((geo
->chunksize
> 0) && (geo
->chunksize
!= UnSet
)
9818 && (geo
->chunksize
!= info
.array
.chunk_size
))
9819 change
= CH_MIGRATION
;
9821 geo
->chunksize
= info
.array
.chunk_size
;
9823 chunk
= geo
->chunksize
/ 1024;
9826 dev
= get_imsm_dev(super
, super
->current_vol
);
9827 data_disks
= imsm_num_data_members(dev
, MAP_0
);
9828 /* compute current size per disk member
9830 current_size
= info
.custom_array_size
/ data_disks
;
9832 if ((geo
->size
> 0) && (geo
->size
!= MAX_SIZE
)) {
9833 /* align component size
9835 geo
->size
= imsm_component_size_aligment_check(
9836 get_imsm_raid_level(dev
->vol
.map
),
9839 if (geo
->size
== 0) {
9840 pr_err("Error. Size expansion is " \
9841 "supported only (current size is %llu, " \
9842 "requested size /rounded/ is 0).\n",
9844 goto analyse_change_exit
;
9848 if ((current_size
!= geo
->size
) && (geo
->size
> 0)) {
9850 pr_err("Error. Size change should be the only "
9851 "one at a time.\n");
9853 goto analyse_change_exit
;
9855 if ((super
->current_vol
+ 1) != super
->anchor
->num_raid_devs
) {
9856 pr_err("Error. The last volume in container "
9857 "can be expanded only (%i/%i).\n",
9858 super
->current_vol
, st
->devnum
);
9859 goto analyse_change_exit
;
9861 /* check the maximum available size
9863 rv
= imsm_get_free_size(st
, dev
->vol
.map
->num_members
,
9864 0, chunk
, &free_size
);
9866 /* Cannot find maximum available space
9870 max_size
= free_size
+ current_size
;
9871 /* align component size
9873 max_size
= imsm_component_size_aligment_check(
9874 get_imsm_raid_level(dev
->vol
.map
),
9878 if (geo
->size
== MAX_SIZE
) {
9879 /* requested size change to the maximum available size
9881 if (max_size
== 0) {
9882 pr_err("Error. Cannot find "
9883 "maximum available space.\n");
9885 goto analyse_change_exit
;
9887 geo
->size
= max_size
;
9890 if ((direction
== ROLLBACK_METADATA_CHANGES
)) {
9891 /* accept size for rollback only
9894 /* round size due to metadata compatibility
9896 geo
->size
= (geo
->size
>> SECT_PER_MB_SHIFT
)
9897 << SECT_PER_MB_SHIFT
;
9898 dprintf("Prepare update for size change to %llu\n",
9900 if (current_size
>= geo
->size
) {
9901 pr_err("Error. Size expansion is "
9902 "supported only (current size is %llu, "
9903 "requested size /rounded/ is %llu).\n",
9904 current_size
, geo
->size
);
9905 goto analyse_change_exit
;
9907 if (max_size
&& geo
->size
> max_size
) {
9908 pr_err("Error. Requested size is larger "
9909 "than maximum available size (maximum "
9910 "available size is %llu, "
9911 "requested size /rounded/ is %llu).\n",
9912 max_size
, geo
->size
);
9913 goto analyse_change_exit
;
9916 geo
->size
*= data_disks
;
9917 geo
->raid_disks
= dev
->vol
.map
->num_members
;
9918 change
= CH_ARRAY_SIZE
;
9920 if (!validate_geometry_imsm(st
,
9923 geo
->raid_disks
+ devNumChange
,
9925 geo
->size
, INVALID_SECTORS
,
9930 struct intel_super
*super
= st
->sb
;
9931 struct imsm_super
*mpb
= super
->anchor
;
9933 if (mpb
->num_raid_devs
> 1) {
9934 pr_err("Error. Cannot perform operation on %s"
9935 "- for this operation it MUST be single "
9936 "array in container\n",
9942 analyse_change_exit
:
9943 if ((direction
== ROLLBACK_METADATA_CHANGES
) &&
9944 ((change
== CH_MIGRATION
) || (change
== CH_TAKEOVER
))) {
9945 dprintf("imsm: Metadata changes rollback is not supported for "
9946 "migration and takeover operations.\n");
9952 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
9954 struct intel_super
*super
= st
->sb
;
9955 struct imsm_update_takeover
*u
;
9957 u
= xmalloc(sizeof(struct imsm_update_takeover
));
9959 u
->type
= update_takeover
;
9960 u
->subarray
= super
->current_vol
;
9962 /* 10->0 transition */
9963 if (geo
->level
== 0)
9964 u
->direction
= R10_TO_R0
;
9966 /* 0->10 transition */
9967 if (geo
->level
== 10)
9968 u
->direction
= R0_TO_R10
;
9970 /* update metadata locally */
9971 imsm_update_metadata_locally(st
, u
,
9972 sizeof(struct imsm_update_takeover
));
9973 /* and possibly remotely */
9974 if (st
->update_tail
)
9975 append_metadata_update(st
, u
,
9976 sizeof(struct imsm_update_takeover
));
9983 static int imsm_reshape_super(struct supertype
*st
, unsigned long long size
,
9985 int layout
, int chunksize
, int raid_disks
,
9986 int delta_disks
, char *backup
, char *dev
,
9987 int direction
, int verbose
)
9990 struct geo_params geo
;
9992 dprintf("imsm: reshape_super called.\n");
9994 memset(&geo
, 0, sizeof(struct geo_params
));
9997 geo
.dev_id
= st
->devnum
;
10000 geo
.layout
= layout
;
10001 geo
.chunksize
= chunksize
;
10002 geo
.raid_disks
= raid_disks
;
10003 if (delta_disks
!= UnSet
)
10004 geo
.raid_disks
+= delta_disks
;
10006 dprintf("\tfor level : %i\n", geo
.level
);
10007 dprintf("\tfor raid_disks : %i\n", geo
.raid_disks
);
10009 if (experimental() == 0)
10012 if (st
->container_dev
== st
->devnum
) {
10013 /* On container level we can only increase number of devices. */
10014 dprintf("imsm: info: Container operation\n");
10015 int old_raid_disks
= 0;
10017 if (imsm_reshape_is_allowed_on_container(
10018 st
, &geo
, &old_raid_disks
, direction
)) {
10019 struct imsm_update_reshape
*u
= NULL
;
10022 len
= imsm_create_metadata_update_for_reshape(
10023 st
, &geo
, old_raid_disks
, &u
);
10026 dprintf("imsm: Cannot prepare update\n");
10027 goto exit_imsm_reshape_super
;
10031 /* update metadata locally */
10032 imsm_update_metadata_locally(st
, u
, len
);
10033 /* and possibly remotely */
10034 if (st
->update_tail
)
10035 append_metadata_update(st
, u
, len
);
10040 pr_err("(imsm) Operation "
10041 "is not allowed on this container\n");
10044 /* On volume level we support following operations
10045 * - takeover: raid10 -> raid0; raid0 -> raid10
10046 * - chunk size migration
10047 * - migration: raid5 -> raid0; raid0 -> raid5
10049 struct intel_super
*super
= st
->sb
;
10050 struct intel_dev
*dev
= super
->devlist
;
10051 int change
, devnum
;
10052 dprintf("imsm: info: Volume operation\n");
10053 /* find requested device */
10055 if (imsm_find_array_minor_by_subdev(
10056 dev
->index
, st
->container_dev
, &devnum
) == 0
10057 && devnum
== geo
.dev_id
)
10062 pr_err("Cannot find %s (%i) subarray\n",
10063 geo
.dev_name
, geo
.dev_id
);
10064 goto exit_imsm_reshape_super
;
10066 super
->current_vol
= dev
->index
;
10067 change
= imsm_analyze_change(st
, &geo
, direction
);
10070 ret_val
= imsm_takeover(st
, &geo
);
10072 case CH_MIGRATION
: {
10073 struct imsm_update_reshape_migration
*u
= NULL
;
10075 imsm_create_metadata_update_for_migration(
10079 "Cannot prepare update\n");
10083 /* update metadata locally */
10084 imsm_update_metadata_locally(st
, u
, len
);
10085 /* and possibly remotely */
10086 if (st
->update_tail
)
10087 append_metadata_update(st
, u
, len
);
10092 case CH_ARRAY_SIZE
: {
10093 struct imsm_update_size_change
*u
= NULL
;
10095 imsm_create_metadata_update_for_size_change(
10099 "Cannot prepare update\n");
10103 /* update metadata locally */
10104 imsm_update_metadata_locally(st
, u
, len
);
10105 /* and possibly remotely */
10106 if (st
->update_tail
)
10107 append_metadata_update(st
, u
, len
);
10117 exit_imsm_reshape_super
:
10118 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
10122 /*******************************************************************************
10123 * Function: wait_for_reshape_imsm
10124 * Description: Function writes new sync_max value and waits until
10125 * reshape process reach new position
10127 * sra : general array info
10128 * ndata : number of disks in new array's layout
10131 * 1 : there is no reshape in progress,
10133 ******************************************************************************/
10134 int wait_for_reshape_imsm(struct mdinfo
*sra
, int ndata
)
10136 int fd
= sysfs_get_fd(sra
, NULL
, "reshape_position");
10137 unsigned long long completed
;
10138 /* to_complete : new sync_max position */
10139 unsigned long long to_complete
= sra
->reshape_progress
;
10140 unsigned long long position_to_set
= to_complete
/ ndata
;
10143 dprintf("imsm: wait_for_reshape_imsm() "
10144 "cannot open reshape_position\n");
10148 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
10149 dprintf("imsm: wait_for_reshape_imsm() "
10150 "cannot read reshape_position (no reshape in progres)\n");
10155 if (completed
> to_complete
) {
10156 dprintf("imsm: wait_for_reshape_imsm() "
10157 "wrong next position to set %llu (%llu)\n",
10158 to_complete
, completed
);
10162 dprintf("Position set: %llu\n", position_to_set
);
10163 if (sysfs_set_num(sra
, NULL
, "sync_max",
10164 position_to_set
) != 0) {
10165 dprintf("imsm: wait_for_reshape_imsm() "
10166 "cannot set reshape position to %llu\n",
10177 select(fd
+1, &rfds
, NULL
, NULL
, NULL
);
10178 if (sysfs_get_str(sra
, NULL
, "sync_action",
10180 strncmp(action
, "reshape", 7) != 0)
10182 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
10183 dprintf("imsm: wait_for_reshape_imsm() "
10184 "cannot read reshape_position (in loop)\n");
10188 } while (completed
< to_complete
);
10194 /*******************************************************************************
10195 * Function: check_degradation_change
10196 * Description: Check that array hasn't become failed.
10198 * info : for sysfs access
10199 * sources : source disks descriptors
10200 * degraded: previous degradation level
10202 * degradation level
10203 ******************************************************************************/
10204 int check_degradation_change(struct mdinfo
*info
,
10208 unsigned long long new_degraded
;
10211 rv
= sysfs_get_ll(info
, NULL
, "degraded", &new_degraded
);
10212 if ((rv
== -1) || (new_degraded
!= (unsigned long long)degraded
)) {
10213 /* check each device to ensure it is still working */
10216 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
10217 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
10219 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
10221 if (sysfs_get_str(info
,
10222 sd
, "state", sbuf
, 20) < 0 ||
10223 strstr(sbuf
, "faulty") ||
10224 strstr(sbuf
, "in_sync") == NULL
) {
10225 /* this device is dead */
10226 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
10227 if (sd
->disk
.raid_disk
>= 0 &&
10228 sources
[sd
->disk
.raid_disk
] >= 0) {
10230 sd
->disk
.raid_disk
]);
10231 sources
[sd
->disk
.raid_disk
] =
10240 return new_degraded
;
10243 /*******************************************************************************
10244 * Function: imsm_manage_reshape
10245 * Description: Function finds array under reshape and it manages reshape
10246 * process. It creates stripes backups (if required) and sets
10249 * afd : Backup handle (nattive) - not used
10250 * sra : general array info
10251 * reshape : reshape parameters - not used
10252 * st : supertype structure
10253 * blocks : size of critical section [blocks]
10254 * fds : table of source device descriptor
10255 * offsets : start of array (offest per devices)
10257 * destfd : table of destination device descriptor
10258 * destoffsets : table of destination offsets (per device)
10260 * 1 : success, reshape is done
10262 ******************************************************************************/
10263 static int imsm_manage_reshape(
10264 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
10265 struct supertype
*st
, unsigned long backup_blocks
,
10266 int *fds
, unsigned long long *offsets
,
10267 int dests
, int *destfd
, unsigned long long *destoffsets
)
10270 struct intel_super
*super
= st
->sb
;
10271 struct intel_dev
*dv
= NULL
;
10272 struct imsm_dev
*dev
= NULL
;
10273 struct imsm_map
*map_src
;
10274 int migr_vol_qan
= 0;
10275 int ndata
, odata
; /* [bytes] */
10276 int chunk
; /* [bytes] */
10277 struct migr_record
*migr_rec
;
10279 unsigned int buf_size
; /* [bytes] */
10280 unsigned long long max_position
; /* array size [bytes] */
10281 unsigned long long next_step
; /* [blocks]/[bytes] */
10282 unsigned long long old_data_stripe_length
;
10283 unsigned long long start_src
; /* [bytes] */
10284 unsigned long long start
; /* [bytes] */
10285 unsigned long long start_buf_shift
; /* [bytes] */
10287 int source_layout
= 0;
10289 if (!fds
|| !offsets
|| !sra
)
10292 /* Find volume during the reshape */
10293 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
10294 if (dv
->dev
->vol
.migr_type
== MIGR_GEN_MIGR
10295 && dv
->dev
->vol
.migr_state
== 1) {
10300 /* Only one volume can migrate at the same time */
10301 if (migr_vol_qan
!= 1) {
10302 pr_err(": %s", migr_vol_qan
?
10303 "Number of migrating volumes greater than 1\n" :
10304 "There is no volume during migrationg\n");
10308 map_src
= get_imsm_map(dev
, MAP_1
);
10309 if (map_src
== NULL
)
10312 ndata
= imsm_num_data_members(dev
, MAP_0
);
10313 odata
= imsm_num_data_members(dev
, MAP_1
);
10315 chunk
= __le16_to_cpu(map_src
->blocks_per_strip
) * 512;
10316 old_data_stripe_length
= odata
* chunk
;
10318 migr_rec
= super
->migr_rec
;
10320 /* initialize migration record for start condition */
10321 if (sra
->reshape_progress
== 0)
10322 init_migr_record_imsm(st
, dev
, sra
);
10324 if (__le32_to_cpu(migr_rec
->rec_status
) != UNIT_SRC_NORMAL
) {
10325 dprintf("imsm: cannot restart migration when data "
10326 "are present in copy area.\n");
10329 /* Save checkpoint to update migration record for current
10330 * reshape position (in md). It can be farther than current
10331 * reshape position in metadata.
10333 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
10334 /* ignore error == 2, this can mean end of reshape here
10336 dprintf("imsm: Cannot write checkpoint to "
10337 "migration record (UNIT_SRC_NORMAL, "
10338 "initial save)\n");
10343 /* size for data */
10344 buf_size
= __le32_to_cpu(migr_rec
->blocks_per_unit
) * 512;
10345 /* extend buffer size for parity disk */
10346 buf_size
+= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
10347 /* add space for stripe aligment */
10348 buf_size
+= old_data_stripe_length
;
10349 if (posix_memalign((void **)&buf
, 4096, buf_size
)) {
10350 dprintf("imsm: Cannot allocate checpoint buffer\n");
10354 max_position
= sra
->component_size
* ndata
;
10355 source_layout
= imsm_level_to_layout(map_src
->raid_level
);
10357 while (__le32_to_cpu(migr_rec
->curr_migr_unit
) <
10358 __le32_to_cpu(migr_rec
->num_migr_units
)) {
10359 /* current reshape position [blocks] */
10360 unsigned long long current_position
=
10361 __le32_to_cpu(migr_rec
->blocks_per_unit
)
10362 * __le32_to_cpu(migr_rec
->curr_migr_unit
);
10363 unsigned long long border
;
10365 /* Check that array hasn't become failed.
10367 degraded
= check_degradation_change(sra
, fds
, degraded
);
10368 if (degraded
> 1) {
10369 dprintf("imsm: Abort reshape due to degradation"
10370 " level (%i)\n", degraded
);
10374 next_step
= __le32_to_cpu(migr_rec
->blocks_per_unit
);
10376 if ((current_position
+ next_step
) > max_position
)
10377 next_step
= max_position
- current_position
;
10379 start
= current_position
* 512;
10381 /* allign reading start to old geometry */
10382 start_buf_shift
= start
% old_data_stripe_length
;
10383 start_src
= start
- start_buf_shift
;
10385 border
= (start_src
/ odata
) - (start
/ ndata
);
10387 if (border
<= __le32_to_cpu(migr_rec
->dest_depth_per_unit
)) {
10388 /* save critical stripes to buf
10389 * start - start address of current unit
10390 * to backup [bytes]
10391 * start_src - start address of current unit
10392 * to backup alligned to source array
10395 unsigned long long next_step_filler
= 0;
10396 unsigned long long copy_length
= next_step
* 512;
10398 /* allign copy area length to stripe in old geometry */
10399 next_step_filler
= ((copy_length
+ start_buf_shift
)
10400 % old_data_stripe_length
);
10401 if (next_step_filler
)
10402 next_step_filler
= (old_data_stripe_length
10403 - next_step_filler
);
10404 dprintf("save_stripes() parameters: start = %llu,"
10405 "\tstart_src = %llu,\tnext_step*512 = %llu,"
10406 "\tstart_in_buf_shift = %llu,"
10407 "\tnext_step_filler = %llu\n",
10408 start
, start_src
, copy_length
,
10409 start_buf_shift
, next_step_filler
);
10411 if (save_stripes(fds
, offsets
, map_src
->num_members
,
10412 chunk
, map_src
->raid_level
,
10413 source_layout
, 0, NULL
, start_src
,
10415 next_step_filler
+ start_buf_shift
,
10417 dprintf("imsm: Cannot save stripes"
10421 /* Convert data to destination format and store it
10422 * in backup general migration area
10424 if (save_backup_imsm(st
, dev
, sra
,
10425 buf
+ start_buf_shift
, copy_length
)) {
10426 dprintf("imsm: Cannot save stripes to "
10427 "target devices\n");
10430 if (save_checkpoint_imsm(st
, sra
,
10431 UNIT_SRC_IN_CP_AREA
)) {
10432 dprintf("imsm: Cannot write checkpoint to "
10433 "migration record (UNIT_SRC_IN_CP_AREA)\n");
10437 /* set next step to use whole border area */
10438 border
/= next_step
;
10440 next_step
*= border
;
10442 /* When data backed up, checkpoint stored,
10443 * kick the kernel to reshape unit of data
10445 next_step
= next_step
+ sra
->reshape_progress
;
10446 /* limit next step to array max position */
10447 if (next_step
> max_position
)
10448 next_step
= max_position
;
10449 sysfs_set_num(sra
, NULL
, "suspend_lo", sra
->reshape_progress
);
10450 sysfs_set_num(sra
, NULL
, "suspend_hi", next_step
);
10451 sra
->reshape_progress
= next_step
;
10453 /* wait until reshape finish */
10454 if (wait_for_reshape_imsm(sra
, ndata
) < 0) {
10455 dprintf("wait_for_reshape_imsm returned error!\n");
10459 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
10460 /* ignore error == 2, this can mean end of reshape here
10462 dprintf("imsm: Cannot write checkpoint to "
10463 "migration record (UNIT_SRC_NORMAL)\n");
10469 /* return '1' if done */
10473 abort_reshape(sra
);
10477 #endif /* MDASSEMBLE */
10479 struct superswitch super_imsm
= {
10481 .examine_super
= examine_super_imsm
,
10482 .brief_examine_super
= brief_examine_super_imsm
,
10483 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
10484 .export_examine_super
= export_examine_super_imsm
,
10485 .detail_super
= detail_super_imsm
,
10486 .brief_detail_super
= brief_detail_super_imsm
,
10487 .write_init_super
= write_init_super_imsm
,
10488 .validate_geometry
= validate_geometry_imsm
,
10489 .add_to_super
= add_to_super_imsm
,
10490 .remove_from_super
= remove_from_super_imsm
,
10491 .detail_platform
= detail_platform_imsm
,
10492 .export_detail_platform
= export_detail_platform_imsm
,
10493 .kill_subarray
= kill_subarray_imsm
,
10494 .update_subarray
= update_subarray_imsm
,
10495 .load_container
= load_container_imsm
,
10496 .default_geometry
= default_geometry_imsm
,
10497 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
10498 .reshape_super
= imsm_reshape_super
,
10499 .manage_reshape
= imsm_manage_reshape
,
10500 .recover_backup
= recover_backup_imsm
,
10502 .match_home
= match_home_imsm
,
10503 .uuid_from_super
= uuid_from_super_imsm
,
10504 .getinfo_super
= getinfo_super_imsm
,
10505 .getinfo_super_disks
= getinfo_super_disks_imsm
,
10506 .update_super
= update_super_imsm
,
10508 .avail_size
= avail_size_imsm
,
10509 .min_acceptable_spare_size
= min_acceptable_spare_size_imsm
,
10511 .compare_super
= compare_super_imsm
,
10513 .load_super
= load_super_imsm
,
10514 .init_super
= init_super_imsm
,
10515 .store_super
= store_super_imsm
,
10516 .free_super
= free_super_imsm
,
10517 .match_metadata_desc
= match_metadata_desc_imsm
,
10518 .container_content
= container_content_imsm
,
10526 .open_new
= imsm_open_new
,
10527 .set_array_state
= imsm_set_array_state
,
10528 .set_disk
= imsm_set_disk
,
10529 .sync_metadata
= imsm_sync_metadata
,
10530 .activate_spare
= imsm_activate_spare
,
10531 .process_update
= imsm_process_update
,
10532 .prepare_update
= imsm_prepare_update
,
10533 #endif /* MDASSEMBLE */