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
;
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 int detail_platform_imsm(int verbose
, int enumerate_only
)
1803 /* There are two components to imsm platform support, the ahci SATA
1804 * controller and the option-rom. To find the SATA controller we
1805 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
1806 * controller with the Intel vendor id is present. This approach
1807 * allows mdadm to leverage the kernel's ahci detection logic, with the
1808 * caveat that if ahci.ko is not loaded mdadm will not be able to
1809 * detect platform raid capabilities. The option-rom resides in a
1810 * platform "Adapter ROM". We scan for its signature to retrieve the
1811 * platform capabilities. If raid support is disabled in the BIOS the
1812 * option-rom capability structure will not be available.
1814 const struct imsm_orom
*orom
;
1815 struct sys_dev
*list
, *hba
;
1820 if (enumerate_only
) {
1821 if (check_env("IMSM_NO_PLATFORM"))
1823 list
= find_intel_devices();
1826 for (hba
= list
; hba
; hba
= hba
->next
) {
1827 orom
= find_imsm_capability(hba
->type
);
1833 free_sys_dev(&list
);
1837 list
= find_intel_devices();
1840 pr_err("no active Intel(R) RAID "
1841 "controller found.\n");
1842 free_sys_dev(&list
);
1844 } else if (verbose
> 0)
1845 print_found_intel_controllers(list
);
1847 for (hba
= list
; hba
; hba
= hba
->next
) {
1848 orom
= find_imsm_capability(hba
->type
);
1850 pr_err("imsm capabilities not found for controller: %s (type %s)\n",
1851 hba
->path
, get_sys_dev_type(hba
->type
));
1853 print_imsm_capability(orom
);
1856 for (hba
= list
; hba
; hba
= hba
->next
) {
1857 printf(" I/O Controller : %s (%s)\n",
1858 hba
->path
, get_sys_dev_type(hba
->type
));
1860 if (hba
->type
== SYS_DEV_SATA
) {
1861 host_base
= ahci_get_port_count(hba
->path
, &port_count
);
1862 if (ahci_enumerate_ports(hba
->path
, port_count
, host_base
, verbose
)) {
1864 pr_err("failed to enumerate "
1865 "ports on SATA controller at %s.", hba
->pci_id
);
1871 free_sys_dev(&list
);
1876 static int match_home_imsm(struct supertype
*st
, char *homehost
)
1878 /* the imsm metadata format does not specify any host
1879 * identification information. We return -1 since we can never
1880 * confirm nor deny whether a given array is "meant" for this
1881 * host. We rely on compare_super and the 'family_num' fields to
1882 * exclude member disks that do not belong, and we rely on
1883 * mdadm.conf to specify the arrays that should be assembled.
1884 * Auto-assembly may still pick up "foreign" arrays.
1890 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
1892 /* The uuid returned here is used for:
1893 * uuid to put into bitmap file (Create, Grow)
1894 * uuid for backup header when saving critical section (Grow)
1895 * comparing uuids when re-adding a device into an array
1896 * In these cases the uuid required is that of the data-array,
1897 * not the device-set.
1898 * uuid to recognise same set when adding a missing device back
1899 * to an array. This is a uuid for the device-set.
1901 * For each of these we can make do with a truncated
1902 * or hashed uuid rather than the original, as long as
1904 * In each case the uuid required is that of the data-array,
1905 * not the device-set.
1907 /* imsm does not track uuid's so we synthesis one using sha1 on
1908 * - The signature (Which is constant for all imsm array, but no matter)
1909 * - the orig_family_num of the container
1910 * - the index number of the volume
1911 * - the 'serial' number of the volume.
1912 * Hopefully these are all constant.
1914 struct intel_super
*super
= st
->sb
;
1917 struct sha1_ctx ctx
;
1918 struct imsm_dev
*dev
= NULL
;
1921 /* some mdadm versions failed to set ->orig_family_num, in which
1922 * case fall back to ->family_num. orig_family_num will be
1923 * fixed up with the first metadata update.
1925 family_num
= super
->anchor
->orig_family_num
;
1926 if (family_num
== 0)
1927 family_num
= super
->anchor
->family_num
;
1928 sha1_init_ctx(&ctx
);
1929 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
1930 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
1931 if (super
->current_vol
>= 0)
1932 dev
= get_imsm_dev(super
, super
->current_vol
);
1934 __u32 vol
= super
->current_vol
;
1935 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
1936 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
1938 sha1_finish_ctx(&ctx
, buf
);
1939 memcpy(uuid
, buf
, 4*4);
1944 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
1946 __u8
*v
= get_imsm_version(mpb
);
1947 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
1948 char major
[] = { 0, 0, 0 };
1949 char minor
[] = { 0 ,0, 0 };
1950 char patch
[] = { 0, 0, 0 };
1951 char *ver_parse
[] = { major
, minor
, patch
};
1955 while (*v
!= '\0' && v
< end
) {
1956 if (*v
!= '.' && j
< 2)
1957 ver_parse
[i
][j
++] = *v
;
1965 *m
= strtol(minor
, NULL
, 0);
1966 *p
= strtol(patch
, NULL
, 0);
1970 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
1972 /* migr_strip_size when repairing or initializing parity */
1973 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1974 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1976 switch (get_imsm_raid_level(map
)) {
1981 return 128*1024 >> 9;
1985 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
1987 /* migr_strip_size when rebuilding a degraded disk, no idea why
1988 * this is different than migr_strip_size_resync(), but it's good
1991 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1992 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1994 switch (get_imsm_raid_level(map
)) {
1997 if (map
->num_members
% map
->num_domains
== 0)
1998 return 128*1024 >> 9;
2002 return max((__u32
) 64*1024 >> 9, chunk
);
2004 return 128*1024 >> 9;
2008 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
2010 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2011 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2012 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
2013 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
2015 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
2018 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
2020 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2021 int level
= get_imsm_raid_level(lo
);
2023 if (level
== 1 || level
== 10) {
2024 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2026 return hi
->num_domains
;
2028 return num_stripes_per_unit_resync(dev
);
2031 static __u8
imsm_num_data_members(struct imsm_dev
*dev
, int second_map
)
2033 /* named 'imsm_' because raid0, raid1 and raid10
2034 * counter-intuitively have the same number of data disks
2036 struct imsm_map
*map
= get_imsm_map(dev
, second_map
);
2038 switch (get_imsm_raid_level(map
)) {
2040 return map
->num_members
;
2044 return map
->num_members
/2;
2046 return map
->num_members
- 1;
2048 dprintf("%s: unsupported raid level\n", __func__
);
2053 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
2055 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2056 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2058 switch(get_imsm_raid_level(map
)) {
2061 return chunk
* map
->num_domains
;
2063 return chunk
* map
->num_members
;
2069 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
2071 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2072 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2073 __u32 strip
= block
/ chunk
;
2075 switch (get_imsm_raid_level(map
)) {
2078 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
2079 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
2081 return vol_stripe
* chunk
+ block
% chunk
;
2083 __u32 stripe
= strip
/ (map
->num_members
- 1);
2085 return stripe
* chunk
+ block
% chunk
;
2092 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
2093 struct imsm_dev
*dev
)
2095 /* calculate the conversion factor between per member 'blocks'
2096 * (md/{resync,rebuild}_start) and imsm migration units, return
2097 * 0 for the 'not migrating' and 'unsupported migration' cases
2099 if (!dev
->vol
.migr_state
)
2102 switch (migr_type(dev
)) {
2103 case MIGR_GEN_MIGR
: {
2104 struct migr_record
*migr_rec
= super
->migr_rec
;
2105 return __le32_to_cpu(migr_rec
->blocks_per_unit
);
2110 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2111 __u32 stripes_per_unit
;
2112 __u32 blocks_per_unit
;
2121 /* yes, this is really the translation of migr_units to
2122 * per-member blocks in the 'resync' case
2124 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
2125 migr_chunk
= migr_strip_blocks_resync(dev
);
2126 disks
= imsm_num_data_members(dev
, MAP_0
);
2127 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
2128 stripe
= __le16_to_cpu(map
->blocks_per_strip
) * disks
;
2129 segment
= blocks_per_unit
/ stripe
;
2130 block_rel
= blocks_per_unit
- segment
* stripe
;
2131 parity_depth
= parity_segment_depth(dev
);
2132 block_map
= map_migr_block(dev
, block_rel
);
2133 return block_map
+ parity_depth
* segment
;
2135 case MIGR_REBUILD
: {
2136 __u32 stripes_per_unit
;
2139 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
2140 migr_chunk
= migr_strip_blocks_rebuild(dev
);
2141 return migr_chunk
* stripes_per_unit
;
2143 case MIGR_STATE_CHANGE
:
2149 static int imsm_level_to_layout(int level
)
2157 return ALGORITHM_LEFT_ASYMMETRIC
;
2164 /*******************************************************************************
2165 * Function: read_imsm_migr_rec
2166 * Description: Function reads imsm migration record from last sector of disk
2168 * fd : disk descriptor
2169 * super : metadata info
2173 ******************************************************************************/
2174 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
)
2177 unsigned long long dsize
;
2179 get_dev_size(fd
, NULL
, &dsize
);
2180 if (lseek64(fd
, dsize
- MIGR_REC_POSITION
, SEEK_SET
) < 0) {
2181 pr_err("Cannot seek to anchor block: %s\n",
2185 if (read(fd
, super
->migr_rec_buf
, MIGR_REC_BUF_SIZE
) !=
2186 MIGR_REC_BUF_SIZE
) {
2187 pr_err("Cannot read migr record block: %s\n",
2197 static struct imsm_dev
*imsm_get_device_during_migration(
2198 struct intel_super
*super
)
2201 struct intel_dev
*dv
;
2203 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
2204 if (is_gen_migration(dv
->dev
))
2210 /*******************************************************************************
2211 * Function: load_imsm_migr_rec
2212 * Description: Function reads imsm migration record (it is stored at the last
2215 * super : imsm internal array info
2216 * info : general array info
2220 * -2 : no migration in progress
2221 ******************************************************************************/
2222 static int load_imsm_migr_rec(struct intel_super
*super
, struct mdinfo
*info
)
2225 struct dl
*dl
= NULL
;
2229 struct imsm_dev
*dev
;
2230 struct imsm_map
*map
= NULL
;
2233 /* find map under migration */
2234 dev
= imsm_get_device_during_migration(super
);
2235 /* nothing to load,no migration in progress?
2239 map
= get_imsm_map(dev
, MAP_0
);
2242 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
2243 /* skip spare and failed disks
2245 if (sd
->disk
.raid_disk
< 0)
2247 /* read only from one of the first two slots */
2249 slot
= get_imsm_disk_slot(map
,
2250 sd
->disk
.raid_disk
);
2251 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2254 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
2255 fd
= dev_open(nm
, O_RDONLY
);
2261 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
2262 /* skip spare and failed disks
2266 /* read only from one of the first two slots */
2268 slot
= get_imsm_disk_slot(map
, dl
->index
);
2269 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2271 sprintf(nm
, "%d:%d", dl
->major
, dl
->minor
);
2272 fd
= dev_open(nm
, O_RDONLY
);
2279 retval
= read_imsm_migr_rec(fd
, super
);
2288 /*******************************************************************************
2289 * function: imsm_create_metadata_checkpoint_update
2290 * Description: It creates update for checkpoint change.
2292 * super : imsm internal array info
2293 * u : pointer to prepared update
2296 * If length is equal to 0, input pointer u contains no update
2297 ******************************************************************************/
2298 static int imsm_create_metadata_checkpoint_update(
2299 struct intel_super
*super
,
2300 struct imsm_update_general_migration_checkpoint
**u
)
2303 int update_memory_size
= 0;
2305 dprintf("imsm_create_metadata_checkpoint_update(enter)\n");
2311 /* size of all update data without anchor */
2312 update_memory_size
=
2313 sizeof(struct imsm_update_general_migration_checkpoint
);
2315 *u
= xcalloc(1, update_memory_size
);
2317 dprintf("error: cannot get memory for "
2318 "imsm_create_metadata_checkpoint_update update\n");
2321 (*u
)->type
= update_general_migration_checkpoint
;
2322 (*u
)->curr_migr_unit
= __le32_to_cpu(super
->migr_rec
->curr_migr_unit
);
2323 dprintf("imsm_create_metadata_checkpoint_update: prepared for %u\n",
2324 (*u
)->curr_migr_unit
);
2326 return update_memory_size
;
2330 static void imsm_update_metadata_locally(struct supertype
*st
,
2331 void *buf
, int len
);
2333 /*******************************************************************************
2334 * Function: write_imsm_migr_rec
2335 * Description: Function writes imsm migration record
2336 * (at the last sector of disk)
2338 * super : imsm internal array info
2342 ******************************************************************************/
2343 static int write_imsm_migr_rec(struct supertype
*st
)
2345 struct intel_super
*super
= st
->sb
;
2346 unsigned long long dsize
;
2352 struct imsm_update_general_migration_checkpoint
*u
;
2353 struct imsm_dev
*dev
;
2354 struct imsm_map
*map
= NULL
;
2356 /* find map under migration */
2357 dev
= imsm_get_device_during_migration(super
);
2358 /* if no migration, write buffer anyway to clear migr_record
2359 * on disk based on first available device
2362 dev
= get_imsm_dev(super
, super
->current_vol
< 0 ? 0 :
2363 super
->current_vol
);
2365 map
= get_imsm_map(dev
, MAP_0
);
2367 for (sd
= super
->disks
; sd
; sd
= sd
->next
) {
2370 /* skip failed and spare devices */
2373 /* write to 2 first slots only */
2375 slot
= get_imsm_disk_slot(map
, sd
->index
);
2376 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2379 sprintf(nm
, "%d:%d", sd
->major
, sd
->minor
);
2380 fd
= dev_open(nm
, O_RDWR
);
2383 get_dev_size(fd
, NULL
, &dsize
);
2384 if (lseek64(fd
, dsize
- MIGR_REC_POSITION
, SEEK_SET
) < 0) {
2385 pr_err("Cannot seek to anchor block: %s\n",
2389 if (write(fd
, super
->migr_rec_buf
, MIGR_REC_BUF_SIZE
) !=
2390 MIGR_REC_BUF_SIZE
) {
2391 pr_err("Cannot write migr record block: %s\n",
2398 /* update checkpoint information in metadata */
2399 len
= imsm_create_metadata_checkpoint_update(super
, &u
);
2402 dprintf("imsm: Cannot prepare update\n");
2405 /* update metadata locally */
2406 imsm_update_metadata_locally(st
, u
, len
);
2407 /* and possibly remotely */
2408 if (st
->update_tail
) {
2409 append_metadata_update(st
, u
, len
);
2410 /* during reshape we do all work inside metadata handler
2411 * manage_reshape(), so metadata update has to be triggered
2414 flush_metadata_updates(st
);
2415 st
->update_tail
= &st
->updates
;
2425 #endif /* MDASSEMBLE */
2427 /* spare/missing disks activations are not allowe when
2428 * array/container performs reshape operation, because
2429 * all arrays in container works on the same disks set
2431 int imsm_reshape_blocks_arrays_changes(struct intel_super
*super
)
2434 struct intel_dev
*i_dev
;
2435 struct imsm_dev
*dev
;
2437 /* check whole container
2439 for (i_dev
= super
->devlist
; i_dev
; i_dev
= i_dev
->next
) {
2441 if (is_gen_migration(dev
)) {
2442 /* No repair during any migration in container
2450 static unsigned long long imsm_component_size_aligment_check(int level
,
2452 unsigned long long component_size
)
2454 unsigned int component_size_alligment
;
2456 /* check component size aligment
2458 component_size_alligment
= component_size
% (chunk_size
/512);
2460 dprintf("imsm_component_size_aligment_check(Level: %i, "
2461 "chunk_size = %i, component_size = %llu), "
2462 "component_size_alligment = %u\n",
2463 level
, chunk_size
, component_size
,
2464 component_size_alligment
);
2466 if (component_size_alligment
&& (level
!= 1) && (level
!= UnSet
)) {
2467 dprintf("imsm: reported component size alligned from %llu ",
2469 component_size
-= component_size_alligment
;
2470 dprintf("to %llu (%i).\n",
2471 component_size
, component_size_alligment
);
2474 return component_size
;
2477 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
2479 struct intel_super
*super
= st
->sb
;
2480 struct migr_record
*migr_rec
= super
->migr_rec
;
2481 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
2482 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2483 struct imsm_map
*prev_map
= get_imsm_map(dev
, MAP_1
);
2484 struct imsm_map
*map_to_analyse
= map
;
2487 int map_disks
= info
->array
.raid_disks
;
2489 memset(info
, 0, sizeof(*info
));
2491 map_to_analyse
= prev_map
;
2493 dl
= super
->current_disk
;
2495 info
->container_member
= super
->current_vol
;
2496 info
->array
.raid_disks
= map
->num_members
;
2497 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
2498 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
2499 info
->array
.md_minor
= -1;
2500 info
->array
.ctime
= 0;
2501 info
->array
.utime
= 0;
2502 info
->array
.chunk_size
=
2503 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
2504 info
->array
.state
= !dev
->vol
.dirty
;
2505 info
->custom_array_size
= __le32_to_cpu(dev
->size_high
);
2506 info
->custom_array_size
<<= 32;
2507 info
->custom_array_size
|= __le32_to_cpu(dev
->size_low
);
2508 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
2510 if (is_gen_migration(dev
)) {
2511 info
->reshape_active
= 1;
2512 info
->new_level
= get_imsm_raid_level(map
);
2513 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
2514 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
2515 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
2516 if (info
->delta_disks
) {
2517 /* this needs to be applied to every array
2520 info
->reshape_active
= CONTAINER_RESHAPE
;
2522 /* We shape information that we give to md might have to be
2523 * modify to cope with md's requirement for reshaping arrays.
2524 * For example, when reshaping a RAID0, md requires it to be
2525 * presented as a degraded RAID4.
2526 * Also if a RAID0 is migrating to a RAID5 we need to specify
2527 * the array as already being RAID5, but the 'before' layout
2528 * is a RAID4-like layout.
2530 switch (info
->array
.level
) {
2532 switch(info
->new_level
) {
2534 /* conversion is happening as RAID4 */
2535 info
->array
.level
= 4;
2536 info
->array
.raid_disks
+= 1;
2539 /* conversion is happening as RAID5 */
2540 info
->array
.level
= 5;
2541 info
->array
.layout
= ALGORITHM_PARITY_N
;
2542 info
->delta_disks
-= 1;
2545 /* FIXME error message */
2546 info
->array
.level
= UnSet
;
2552 info
->new_level
= UnSet
;
2553 info
->new_layout
= UnSet
;
2554 info
->new_chunk
= info
->array
.chunk_size
;
2555 info
->delta_disks
= 0;
2559 info
->disk
.major
= dl
->major
;
2560 info
->disk
.minor
= dl
->minor
;
2561 info
->disk
.number
= dl
->index
;
2562 info
->disk
.raid_disk
= get_imsm_disk_slot(map_to_analyse
,
2566 info
->data_offset
= pba_of_lba0(map_to_analyse
);
2567 info
->component_size
= blocks_per_member(map_to_analyse
);
2569 info
->component_size
= imsm_component_size_aligment_check(
2571 info
->array
.chunk_size
,
2572 info
->component_size
);
2574 memset(info
->uuid
, 0, sizeof(info
->uuid
));
2575 info
->recovery_start
= MaxSector
;
2577 info
->reshape_progress
= 0;
2578 info
->resync_start
= MaxSector
;
2579 if ((map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
2581 imsm_reshape_blocks_arrays_changes(super
) == 0) {
2582 info
->resync_start
= 0;
2584 if (dev
->vol
.migr_state
) {
2585 switch (migr_type(dev
)) {
2588 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2590 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
2592 info
->resync_start
= blocks_per_unit
* units
;
2595 case MIGR_GEN_MIGR
: {
2596 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2598 __u64 units
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
2599 unsigned long long array_blocks
;
2602 if (__le32_to_cpu(migr_rec
->ascending_migr
) &&
2604 (__le32_to_cpu(migr_rec
->num_migr_units
)-1)) &&
2605 (super
->migr_rec
->rec_status
==
2606 __cpu_to_le32(UNIT_SRC_IN_CP_AREA
)))
2609 info
->reshape_progress
= blocks_per_unit
* units
;
2611 dprintf("IMSM: General Migration checkpoint : %llu "
2612 "(%llu) -> read reshape progress : %llu\n",
2613 (unsigned long long)units
,
2614 (unsigned long long)blocks_per_unit
,
2615 info
->reshape_progress
);
2617 used_disks
= imsm_num_data_members(dev
, MAP_1
);
2618 if (used_disks
> 0) {
2619 array_blocks
= blocks_per_member(map
) *
2621 /* round array size down to closest MB
2623 info
->custom_array_size
= (array_blocks
2624 >> SECT_PER_MB_SHIFT
)
2625 << SECT_PER_MB_SHIFT
;
2629 /* we could emulate the checkpointing of
2630 * 'sync_action=check' migrations, but for now
2631 * we just immediately complete them
2634 /* this is handled by container_content_imsm() */
2635 case MIGR_STATE_CHANGE
:
2636 /* FIXME handle other migrations */
2638 /* we are not dirty, so... */
2639 info
->resync_start
= MaxSector
;
2643 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
2644 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
2646 info
->array
.major_version
= -1;
2647 info
->array
.minor_version
= -2;
2648 devname
= devnum2devname(st
->container_dev
);
2649 *info
->text_version
= '\0';
2651 sprintf(info
->text_version
, "/%s/%d", devname
, info
->container_member
);
2653 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
2654 uuid_from_super_imsm(st
, info
->uuid
);
2658 for (i
=0; i
<map_disks
; i
++) {
2660 if (i
< info
->array
.raid_disks
) {
2661 struct imsm_disk
*dsk
;
2662 j
= get_imsm_disk_idx(dev
, i
, MAP_X
);
2663 dsk
= get_imsm_disk(super
, j
);
2664 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
2671 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
2672 int failed
, int look_in_map
);
2674 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
2679 static void manage_second_map(struct intel_super
*super
, struct imsm_dev
*dev
)
2681 if (is_gen_migration(dev
)) {
2684 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
2686 failed
= imsm_count_failed(super
, dev
, MAP_1
);
2687 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_1
);
2688 if (map2
->map_state
!= map_state
) {
2689 map2
->map_state
= map_state
;
2690 super
->updates_pending
++;
2696 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
2700 for (d
= super
->missing
; d
; d
= d
->next
)
2701 if (d
->index
== index
)
2706 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
2708 struct intel_super
*super
= st
->sb
;
2709 struct imsm_disk
*disk
;
2710 int map_disks
= info
->array
.raid_disks
;
2711 int max_enough
= -1;
2713 struct imsm_super
*mpb
;
2715 if (super
->current_vol
>= 0) {
2716 getinfo_super_imsm_volume(st
, info
, map
);
2719 memset(info
, 0, sizeof(*info
));
2721 /* Set raid_disks to zero so that Assemble will always pull in valid
2724 info
->array
.raid_disks
= 0;
2725 info
->array
.level
= LEVEL_CONTAINER
;
2726 info
->array
.layout
= 0;
2727 info
->array
.md_minor
= -1;
2728 info
->array
.ctime
= 0; /* N/A for imsm */
2729 info
->array
.utime
= 0;
2730 info
->array
.chunk_size
= 0;
2732 info
->disk
.major
= 0;
2733 info
->disk
.minor
= 0;
2734 info
->disk
.raid_disk
= -1;
2735 info
->reshape_active
= 0;
2736 info
->array
.major_version
= -1;
2737 info
->array
.minor_version
= -2;
2738 strcpy(info
->text_version
, "imsm");
2739 info
->safe_mode_delay
= 0;
2740 info
->disk
.number
= -1;
2741 info
->disk
.state
= 0;
2743 info
->recovery_start
= MaxSector
;
2744 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
2746 /* do we have the all the insync disks that we expect? */
2747 mpb
= super
->anchor
;
2749 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
2750 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
2751 int failed
, enough
, j
, missing
= 0;
2752 struct imsm_map
*map
;
2755 failed
= imsm_count_failed(super
, dev
, MAP_0
);
2756 state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
2757 map
= get_imsm_map(dev
, MAP_0
);
2759 /* any newly missing disks?
2760 * (catches single-degraded vs double-degraded)
2762 for (j
= 0; j
< map
->num_members
; j
++) {
2763 __u32 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
2764 __u32 idx
= ord_to_idx(ord
);
2766 if (!(ord
& IMSM_ORD_REBUILD
) &&
2767 get_imsm_missing(super
, idx
)) {
2773 if (state
== IMSM_T_STATE_FAILED
)
2775 else if (state
== IMSM_T_STATE_DEGRADED
&&
2776 (state
!= map
->map_state
|| missing
))
2778 else /* we're normal, or already degraded */
2780 if (is_gen_migration(dev
) && missing
) {
2781 /* during general migration we need all disks
2782 * that process is running on.
2783 * No new missing disk is allowed.
2787 /* no more checks necessary
2791 /* in the missing/failed disk case check to see
2792 * if at least one array is runnable
2794 max_enough
= max(max_enough
, enough
);
2796 dprintf("%s: enough: %d\n", __func__
, max_enough
);
2797 info
->container_enough
= max_enough
;
2800 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
2802 disk
= &super
->disks
->disk
;
2803 info
->data_offset
= total_blocks(&super
->disks
->disk
) - reserved
;
2804 info
->component_size
= reserved
;
2805 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
2806 /* we don't change info->disk.raid_disk here because
2807 * this state will be finalized in mdmon after we have
2808 * found the 'most fresh' version of the metadata
2810 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
2811 info
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
2814 /* only call uuid_from_super_imsm when this disk is part of a populated container,
2815 * ->compare_super may have updated the 'num_raid_devs' field for spares
2817 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
2818 uuid_from_super_imsm(st
, info
->uuid
);
2820 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
2822 /* I don't know how to compute 'map' on imsm, so use safe default */
2825 for (i
= 0; i
< map_disks
; i
++)
2831 /* allocates memory and fills disk in mdinfo structure
2832 * for each disk in array */
2833 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
2835 struct mdinfo
*mddev
= NULL
;
2836 struct intel_super
*super
= st
->sb
;
2837 struct imsm_disk
*disk
;
2840 if (!super
|| !super
->disks
)
2843 mddev
= xcalloc(1, sizeof(*mddev
));
2847 tmp
= xcalloc(1, sizeof(*tmp
));
2849 tmp
->next
= mddev
->devs
;
2851 tmp
->disk
.number
= count
++;
2852 tmp
->disk
.major
= dl
->major
;
2853 tmp
->disk
.minor
= dl
->minor
;
2854 tmp
->disk
.state
= is_configured(disk
) ?
2855 (1 << MD_DISK_ACTIVE
) : 0;
2856 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
2857 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
2858 tmp
->disk
.raid_disk
= -1;
2864 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
2865 char *update
, char *devname
, int verbose
,
2866 int uuid_set
, char *homehost
)
2868 /* For 'assemble' and 'force' we need to return non-zero if any
2869 * change was made. For others, the return value is ignored.
2870 * Update options are:
2871 * force-one : This device looks a bit old but needs to be included,
2872 * update age info appropriately.
2873 * assemble: clear any 'faulty' flag to allow this device to
2875 * force-array: Array is degraded but being forced, mark it clean
2876 * if that will be needed to assemble it.
2878 * newdev: not used ????
2879 * grow: Array has gained a new device - this is currently for
2881 * resync: mark as dirty so a resync will happen.
2882 * name: update the name - preserving the homehost
2883 * uuid: Change the uuid of the array to match watch is given
2885 * Following are not relevant for this imsm:
2886 * sparc2.2 : update from old dodgey metadata
2887 * super-minor: change the preferred_minor number
2888 * summaries: update redundant counters.
2889 * homehost: update the recorded homehost
2890 * _reshape_progress: record new reshape_progress position.
2893 struct intel_super
*super
= st
->sb
;
2894 struct imsm_super
*mpb
;
2896 /* we can only update container info */
2897 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
2900 mpb
= super
->anchor
;
2902 if (strcmp(update
, "uuid") == 0) {
2903 /* We take this to mean that the family_num should be updated.
2904 * However that is much smaller than the uuid so we cannot really
2905 * allow an explicit uuid to be given. And it is hard to reliably
2907 * So if !uuid_set we know the current uuid is random and just used
2908 * the first 'int' and copy it to the other 3 positions.
2909 * Otherwise we require the 4 'int's to be the same as would be the
2910 * case if we are using a random uuid. So an explicit uuid will be
2911 * accepted as long as all for ints are the same... which shouldn't hurt
2914 info
->uuid
[1] = info
->uuid
[2] = info
->uuid
[3] = info
->uuid
[0];
2917 if (info
->uuid
[0] != info
->uuid
[1] ||
2918 info
->uuid
[1] != info
->uuid
[2] ||
2919 info
->uuid
[2] != info
->uuid
[3])
2925 mpb
->orig_family_num
= info
->uuid
[0];
2926 } else if (strcmp(update
, "assemble") == 0)
2931 /* successful update? recompute checksum */
2933 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
2938 static size_t disks_to_mpb_size(int disks
)
2942 size
= sizeof(struct imsm_super
);
2943 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
2944 size
+= 2 * sizeof(struct imsm_dev
);
2945 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
2946 size
+= (4 - 2) * sizeof(struct imsm_map
);
2947 /* 4 possible disk_ord_tbl's */
2948 size
+= 4 * (disks
- 1) * sizeof(__u32
);
2953 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
)
2955 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
2958 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
2961 static void free_devlist(struct intel_super
*super
)
2963 struct intel_dev
*dv
;
2965 while (super
->devlist
) {
2966 dv
= super
->devlist
->next
;
2967 free(super
->devlist
->dev
);
2968 free(super
->devlist
);
2969 super
->devlist
= dv
;
2973 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
2975 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
2978 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
2982 * 0 same, or first was empty, and second was copied
2983 * 1 second had wrong number
2985 * 3 wrong other info
2987 struct intel_super
*first
= st
->sb
;
2988 struct intel_super
*sec
= tst
->sb
;
2995 /* in platform dependent environment test if the disks
2996 * use the same Intel hba
2998 if (!check_env("IMSM_NO_PLATFORM")) {
2999 if (!first
->hba
|| !sec
->hba
||
3000 (first
->hba
->type
!= sec
->hba
->type
)) {
3002 "HBAs of devices does not match %s != %s\n",
3003 first
->hba
? get_sys_dev_type(first
->hba
->type
) : NULL
,
3004 sec
->hba
? get_sys_dev_type(sec
->hba
->type
) : NULL
);
3009 /* if an anchor does not have num_raid_devs set then it is a free
3012 if (first
->anchor
->num_raid_devs
> 0 &&
3013 sec
->anchor
->num_raid_devs
> 0) {
3014 /* Determine if these disks might ever have been
3015 * related. Further disambiguation can only take place
3016 * in load_super_imsm_all
3018 __u32 first_family
= first
->anchor
->orig_family_num
;
3019 __u32 sec_family
= sec
->anchor
->orig_family_num
;
3021 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
3022 MAX_SIGNATURE_LENGTH
) != 0)
3025 if (first_family
== 0)
3026 first_family
= first
->anchor
->family_num
;
3027 if (sec_family
== 0)
3028 sec_family
= sec
->anchor
->family_num
;
3030 if (first_family
!= sec_family
)
3036 /* if 'first' is a spare promote it to a populated mpb with sec's
3039 if (first
->anchor
->num_raid_devs
== 0 &&
3040 sec
->anchor
->num_raid_devs
> 0) {
3042 struct intel_dev
*dv
;
3043 struct imsm_dev
*dev
;
3045 /* we need to copy raid device info from sec if an allocation
3046 * fails here we don't associate the spare
3048 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
3049 dv
= xmalloc(sizeof(*dv
));
3050 dev
= xmalloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
3053 dv
->next
= first
->devlist
;
3054 first
->devlist
= dv
;
3056 if (i
< sec
->anchor
->num_raid_devs
) {
3057 /* allocation failure */
3058 free_devlist(first
);
3059 fprintf(stderr
, "imsm: failed to associate spare\n");
3062 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
3063 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
3064 first
->anchor
->family_num
= sec
->anchor
->family_num
;
3065 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
3066 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
3067 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
3073 static void fd2devname(int fd
, char *name
)
3077 char dname
[PATH_MAX
];
3082 if (fstat(fd
, &st
) != 0)
3084 sprintf(path
, "/sys/dev/block/%d:%d",
3085 major(st
.st_rdev
), minor(st
.st_rdev
));
3087 rv
= readlink(path
, dname
, sizeof(dname
)-1);
3092 nm
= strrchr(dname
, '/');
3095 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
3099 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
3101 static int imsm_read_serial(int fd
, char *devname
,
3102 __u8 serial
[MAX_RAID_SERIAL_LEN
])
3104 unsigned char scsi_serial
[255];
3113 memset(scsi_serial
, 0, sizeof(scsi_serial
));
3115 rv
= scsi_get_serial(fd
, scsi_serial
, sizeof(scsi_serial
));
3117 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
3118 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3119 fd2devname(fd
, (char *) serial
);
3125 pr_err("Failed to retrieve serial for %s\n",
3130 rsp_len
= scsi_serial
[3];
3133 pr_err("Failed to retrieve serial for %s\n",
3137 rsp_buf
= (char *) &scsi_serial
[4];
3139 /* trim all whitespace and non-printable characters and convert
3142 for (i
= 0, dest
= rsp_buf
; i
< rsp_len
; i
++) {
3145 /* ':' is reserved for use in placeholder serial
3146 * numbers for missing disks
3154 len
= dest
- rsp_buf
;
3157 /* truncate leading characters */
3158 if (len
> MAX_RAID_SERIAL_LEN
) {
3159 dest
+= len
- MAX_RAID_SERIAL_LEN
;
3160 len
= MAX_RAID_SERIAL_LEN
;
3163 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3164 memcpy(serial
, dest
, len
);
3169 static int serialcmp(__u8
*s1
, __u8
*s2
)
3171 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
3174 static void serialcpy(__u8
*dest
, __u8
*src
)
3176 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
3179 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
3183 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3184 if (serialcmp(dl
->serial
, serial
) == 0)
3190 static struct imsm_disk
*
3191 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
3195 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3196 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3198 if (serialcmp(disk
->serial
, serial
) == 0) {
3209 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3211 struct imsm_disk
*disk
;
3216 __u8 serial
[MAX_RAID_SERIAL_LEN
];
3218 rv
= imsm_read_serial(fd
, devname
, serial
);
3223 dl
= xcalloc(1, sizeof(*dl
));
3226 dl
->major
= major(stb
.st_rdev
);
3227 dl
->minor
= minor(stb
.st_rdev
);
3228 dl
->next
= super
->disks
;
3229 dl
->fd
= keep_fd
? fd
: -1;
3230 assert(super
->disks
== NULL
);
3232 serialcpy(dl
->serial
, serial
);
3235 fd2devname(fd
, name
);
3237 dl
->devname
= xstrdup(devname
);
3239 dl
->devname
= xstrdup(name
);
3241 /* look up this disk's index in the current anchor */
3242 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
3245 /* only set index on disks that are a member of a
3246 * populated contianer, i.e. one with raid_devs
3248 if (is_failed(&dl
->disk
))
3250 else if (is_spare(&dl
->disk
))
3258 /* When migrating map0 contains the 'destination' state while map1
3259 * contains the current state. When not migrating map0 contains the
3260 * current state. This routine assumes that map[0].map_state is set to
3261 * the current array state before being called.
3263 * Migration is indicated by one of the following states
3264 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
3265 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
3266 * map1state=unitialized)
3267 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
3269 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
3270 * map1state=degraded)
3271 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
3274 static void migrate(struct imsm_dev
*dev
, struct intel_super
*super
,
3275 __u8 to_state
, int migr_type
)
3277 struct imsm_map
*dest
;
3278 struct imsm_map
*src
= get_imsm_map(dev
, MAP_0
);
3280 dev
->vol
.migr_state
= 1;
3281 set_migr_type(dev
, migr_type
);
3282 dev
->vol
.curr_migr_unit
= 0;
3283 dest
= get_imsm_map(dev
, MAP_1
);
3285 /* duplicate and then set the target end state in map[0] */
3286 memcpy(dest
, src
, sizeof_imsm_map(src
));
3287 if ((migr_type
== MIGR_REBUILD
) ||
3288 (migr_type
== MIGR_GEN_MIGR
)) {
3292 for (i
= 0; i
< src
->num_members
; i
++) {
3293 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
3294 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
3298 if (migr_type
== MIGR_GEN_MIGR
)
3299 /* Clear migration record */
3300 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
3302 src
->map_state
= to_state
;
3305 static void end_migration(struct imsm_dev
*dev
, struct intel_super
*super
,
3308 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3309 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
== 0 ?
3313 /* merge any IMSM_ORD_REBUILD bits that were not successfully
3314 * completed in the last migration.
3316 * FIXME add support for raid-level-migration
3318 if ((map_state
!= map
->map_state
) && (is_gen_migration(dev
) == 0) &&
3319 (prev
->map_state
!= IMSM_T_STATE_UNINITIALIZED
)) {
3320 /* when final map state is other than expected
3321 * merge maps (not for migration)
3325 for (i
= 0; i
< prev
->num_members
; i
++)
3326 for (j
= 0; j
< map
->num_members
; j
++)
3327 /* during online capacity expansion
3328 * disks position can be changed
3329 * if takeover is used
3331 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
3332 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
3333 map
->disk_ord_tbl
[j
] |=
3334 prev
->disk_ord_tbl
[i
];
3337 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3338 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3341 dev
->vol
.migr_state
= 0;
3342 set_migr_type(dev
, 0);
3343 dev
->vol
.curr_migr_unit
= 0;
3344 map
->map_state
= map_state
;
3348 static int parse_raid_devices(struct intel_super
*super
)
3351 struct imsm_dev
*dev_new
;
3352 size_t len
, len_migr
;
3354 size_t space_needed
= 0;
3355 struct imsm_super
*mpb
= super
->anchor
;
3357 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3358 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3359 struct intel_dev
*dv
;
3361 len
= sizeof_imsm_dev(dev_iter
, 0);
3362 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
3364 space_needed
+= len_migr
- len
;
3366 dv
= xmalloc(sizeof(*dv
));
3367 if (max_len
< len_migr
)
3369 if (max_len
> len_migr
)
3370 space_needed
+= max_len
- len_migr
;
3371 dev_new
= xmalloc(max_len
);
3372 imsm_copy_dev(dev_new
, dev_iter
);
3375 dv
->next
= super
->devlist
;
3376 super
->devlist
= dv
;
3379 /* ensure that super->buf is large enough when all raid devices
3382 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
3385 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
, 512);
3386 if (posix_memalign(&buf
, 512, len
) != 0)
3389 memcpy(buf
, super
->buf
, super
->len
);
3390 memset(buf
+ super
->len
, 0, len
- super
->len
);
3399 /* retrieve a pointer to the bbm log which starts after all raid devices */
3400 struct bbm_log
*__get_imsm_bbm_log(struct imsm_super
*mpb
)
3404 if (__le32_to_cpu(mpb
->bbm_log_size
)) {
3406 ptr
+= mpb
->mpb_size
- __le32_to_cpu(mpb
->bbm_log_size
);
3412 /*******************************************************************************
3413 * Function: check_mpb_migr_compatibility
3414 * Description: Function checks for unsupported migration features:
3415 * - migration optimization area (pba_of_lba0)
3416 * - descending reshape (ascending_migr)
3418 * super : imsm metadata information
3420 * 0 : migration is compatible
3421 * -1 : migration is not compatible
3422 ******************************************************************************/
3423 int check_mpb_migr_compatibility(struct intel_super
*super
)
3425 struct imsm_map
*map0
, *map1
;
3426 struct migr_record
*migr_rec
= super
->migr_rec
;
3429 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3430 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3433 dev_iter
->vol
.migr_state
== 1 &&
3434 dev_iter
->vol
.migr_type
== MIGR_GEN_MIGR
) {
3435 /* This device is migrating */
3436 map0
= get_imsm_map(dev_iter
, MAP_0
);
3437 map1
= get_imsm_map(dev_iter
, MAP_1
);
3438 if (pba_of_lba0(map0
) != pba_of_lba0(map1
))
3439 /* migration optimization area was used */
3441 if (migr_rec
->ascending_migr
== 0
3442 && migr_rec
->dest_depth_per_unit
> 0)
3443 /* descending reshape not supported yet */
3450 static void __free_imsm(struct intel_super
*super
, int free_disks
);
3452 /* load_imsm_mpb - read matrix metadata
3453 * allocates super->mpb to be freed by free_imsm
3455 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
3457 unsigned long long dsize
;
3458 unsigned long long sectors
;
3460 struct imsm_super
*anchor
;
3463 get_dev_size(fd
, NULL
, &dsize
);
3466 pr_err("%s: device to small for imsm\n",
3471 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0) {
3473 pr_err("Cannot seek to anchor block on %s: %s\n",
3474 devname
, strerror(errno
));
3478 if (posix_memalign((void**)&anchor
, 512, 512) != 0) {
3480 pr_err("Failed to allocate imsm anchor buffer"
3481 " on %s\n", devname
);
3484 if (read(fd
, anchor
, 512) != 512) {
3486 pr_err("Cannot read anchor block on %s: %s\n",
3487 devname
, strerror(errno
));
3492 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
3494 pr_err("no IMSM anchor on %s\n", devname
);
3499 __free_imsm(super
, 0);
3500 /* reload capability and hba */
3502 /* capability and hba must be updated with new super allocation */
3503 find_intel_hba_capability(fd
, super
, devname
);
3504 super
->len
= ROUND_UP(anchor
->mpb_size
, 512);
3505 if (posix_memalign(&super
->buf
, 512, super
->len
) != 0) {
3507 pr_err("unable to allocate %zu byte mpb buffer\n",
3512 memcpy(super
->buf
, anchor
, 512);
3514 sectors
= mpb_sectors(anchor
) - 1;
3517 if (posix_memalign(&super
->migr_rec_buf
, 512, MIGR_REC_BUF_SIZE
) != 0) {
3518 pr_err("%s could not allocate migr_rec buffer\n", __func__
);
3522 super
->clean_migration_record_by_mdmon
= 0;
3525 check_sum
= __gen_imsm_checksum(super
->anchor
);
3526 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3528 pr_err("IMSM checksum %x != %x on %s\n",
3530 __le32_to_cpu(super
->anchor
->check_sum
),
3538 /* read the extended mpb */
3539 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0) {
3541 pr_err("Cannot seek to extended mpb on %s: %s\n",
3542 devname
, strerror(errno
));
3546 if ((unsigned)read(fd
, super
->buf
+ 512, super
->len
- 512) != super
->len
- 512) {
3548 pr_err("Cannot read extended mpb on %s: %s\n",
3549 devname
, strerror(errno
));
3553 check_sum
= __gen_imsm_checksum(super
->anchor
);
3554 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3556 pr_err("IMSM checksum %x != %x on %s\n",
3557 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
3562 /* FIXME the BBM log is disk specific so we cannot use this global
3563 * buffer for all disks. Ok for now since we only look at the global
3564 * bbm_log_size parameter to gate assembly
3566 super
->bbm_log
= __get_imsm_bbm_log(super
->anchor
);
3571 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
);
3573 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
3574 static void clear_hi(struct intel_super
*super
)
3576 struct imsm_super
*mpb
= super
->anchor
;
3578 if (mpb
->attributes
& MPB_ATTRIB_2TB_DISK
)
3580 for (i
= 0; i
< mpb
->num_disks
; ++i
) {
3581 struct imsm_disk
*disk
= &mpb
->disk
[i
];
3582 disk
->total_blocks_hi
= 0;
3584 for (i
= 0; i
< mpb
->num_raid_devs
; ++i
) {
3585 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3588 for (n
= 0; n
< 2; ++n
) {
3589 struct imsm_map
*map
= get_imsm_map(dev
, n
);
3592 map
->pba_of_lba0_hi
= 0;
3593 map
->blocks_per_member_hi
= 0;
3594 map
->num_data_stripes_hi
= 0;
3600 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3604 err
= load_imsm_mpb(fd
, super
, devname
);
3607 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
3610 err
= parse_raid_devices(super
);
3615 static void __free_imsm_disk(struct dl
*d
)
3627 static void free_imsm_disks(struct intel_super
*super
)
3631 while (super
->disks
) {
3633 super
->disks
= d
->next
;
3634 __free_imsm_disk(d
);
3636 while (super
->disk_mgmt_list
) {
3637 d
= super
->disk_mgmt_list
;
3638 super
->disk_mgmt_list
= d
->next
;
3639 __free_imsm_disk(d
);
3641 while (super
->missing
) {
3643 super
->missing
= d
->next
;
3644 __free_imsm_disk(d
);
3649 /* free all the pieces hanging off of a super pointer */
3650 static void __free_imsm(struct intel_super
*super
, int free_disks
)
3652 struct intel_hba
*elem
, *next
;
3658 /* unlink capability description */
3660 if (super
->migr_rec_buf
) {
3661 free(super
->migr_rec_buf
);
3662 super
->migr_rec_buf
= NULL
;
3665 free_imsm_disks(super
);
3666 free_devlist(super
);
3670 free((void *)elem
->path
);
3678 static void free_imsm(struct intel_super
*super
)
3680 __free_imsm(super
, 1);
3684 static void free_super_imsm(struct supertype
*st
)
3686 struct intel_super
*super
= st
->sb
;
3695 static struct intel_super
*alloc_super(void)
3697 struct intel_super
*super
= xcalloc(1, sizeof(*super
));
3699 super
->current_vol
= -1;
3700 super
->create_offset
= ~((unsigned long long) 0);
3705 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
3707 static int find_intel_hba_capability(int fd
, struct intel_super
*super
, char *devname
)
3709 struct sys_dev
*hba_name
;
3712 if ((fd
< 0) || check_env("IMSM_NO_PLATFORM")) {
3717 hba_name
= find_disk_attached_hba(fd
, NULL
);
3720 pr_err("%s is not attached to Intel(R) RAID controller.\n",
3724 rv
= attach_hba_to_super(super
, hba_name
);
3727 struct intel_hba
*hba
= super
->hba
;
3729 pr_err("%s is attached to Intel(R) %s RAID "
3730 "controller (%s),\n"
3731 " but the container is assigned to Intel(R) "
3732 "%s RAID controller (",
3735 hba_name
->pci_id
? : "Err!",
3736 get_sys_dev_type(hba_name
->type
));
3739 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
3741 fprintf(stderr
, ", ");
3745 fprintf(stderr
, ").\n"
3746 " Mixing devices attached to different controllers "
3747 "is not allowed.\n");
3749 free_sys_dev(&hba_name
);
3752 super
->orom
= find_imsm_capability(hba_name
->type
);
3753 free_sys_dev(&hba_name
);
3759 /* find_missing - helper routine for load_super_imsm_all that identifies
3760 * disks that have disappeared from the system. This routine relies on
3761 * the mpb being uptodate, which it is at load time.
3763 static int find_missing(struct intel_super
*super
)
3766 struct imsm_super
*mpb
= super
->anchor
;
3768 struct imsm_disk
*disk
;
3770 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3771 disk
= __get_imsm_disk(mpb
, i
);
3772 dl
= serial_to_dl(disk
->serial
, super
);
3776 dl
= xmalloc(sizeof(*dl
));
3780 dl
->devname
= xstrdup("missing");
3782 serialcpy(dl
->serial
, disk
->serial
);
3785 dl
->next
= super
->missing
;
3786 super
->missing
= dl
;
3793 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
3795 struct intel_disk
*idisk
= disk_list
;
3798 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
3800 idisk
= idisk
->next
;
3806 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
3807 struct intel_super
*super
,
3808 struct intel_disk
**disk_list
)
3810 struct imsm_disk
*d
= &super
->disks
->disk
;
3811 struct imsm_super
*mpb
= super
->anchor
;
3814 for (i
= 0; i
< tbl_size
; i
++) {
3815 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
3816 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
3818 if (tbl_mpb
->family_num
== mpb
->family_num
) {
3819 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
3820 dprintf("%s: mpb from %d:%d matches %d:%d\n",
3821 __func__
, super
->disks
->major
,
3822 super
->disks
->minor
,
3823 table
[i
]->disks
->major
,
3824 table
[i
]->disks
->minor
);
3828 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
3829 is_configured(d
) == is_configured(tbl_d
)) &&
3830 tbl_mpb
->generation_num
< mpb
->generation_num
) {
3831 /* current version of the mpb is a
3832 * better candidate than the one in
3833 * super_table, but copy over "cross
3834 * generational" status
3836 struct intel_disk
*idisk
;
3838 dprintf("%s: mpb from %d:%d replaces %d:%d\n",
3839 __func__
, super
->disks
->major
,
3840 super
->disks
->minor
,
3841 table
[i
]->disks
->major
,
3842 table
[i
]->disks
->minor
);
3844 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
3845 if (idisk
&& is_failed(&idisk
->disk
))
3846 tbl_d
->status
|= FAILED_DISK
;
3849 struct intel_disk
*idisk
;
3850 struct imsm_disk
*disk
;
3852 /* tbl_mpb is more up to date, but copy
3853 * over cross generational status before
3856 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
3857 if (disk
&& is_failed(disk
))
3858 d
->status
|= FAILED_DISK
;
3860 idisk
= disk_list_get(d
->serial
, *disk_list
);
3863 if (disk
&& is_configured(disk
))
3864 idisk
->disk
.status
|= CONFIGURED_DISK
;
3867 dprintf("%s: mpb from %d:%d prefer %d:%d\n",
3868 __func__
, super
->disks
->major
,
3869 super
->disks
->minor
,
3870 table
[i
]->disks
->major
,
3871 table
[i
]->disks
->minor
);
3879 table
[tbl_size
++] = super
;
3883 /* update/extend the merged list of imsm_disk records */
3884 for (j
= 0; j
< mpb
->num_disks
; j
++) {
3885 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
3886 struct intel_disk
*idisk
;
3888 idisk
= disk_list_get(disk
->serial
, *disk_list
);
3890 idisk
->disk
.status
|= disk
->status
;
3891 if (is_configured(&idisk
->disk
) ||
3892 is_failed(&idisk
->disk
))
3893 idisk
->disk
.status
&= ~(SPARE_DISK
);
3895 idisk
= xcalloc(1, sizeof(*idisk
));
3896 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
3897 idisk
->disk
= *disk
;
3898 idisk
->next
= *disk_list
;
3902 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
3909 static struct intel_super
*
3910 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
3913 struct imsm_super
*mpb
= super
->anchor
;
3917 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3918 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3919 struct intel_disk
*idisk
;
3921 idisk
= disk_list_get(disk
->serial
, disk_list
);
3923 if (idisk
->owner
== owner
||
3924 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
3927 dprintf("%s: '%.16s' owner %d != %d\n",
3928 __func__
, disk
->serial
, idisk
->owner
,
3931 dprintf("%s: unknown disk %x [%d]: %.16s\n",
3932 __func__
, __le32_to_cpu(mpb
->family_num
), i
,
3938 if (ok_count
== mpb
->num_disks
)
3943 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
3945 struct intel_super
*s
;
3947 for (s
= super_list
; s
; s
= s
->next
) {
3948 if (family_num
!= s
->anchor
->family_num
)
3950 fprintf(stderr
, "Conflict, offlining family %#x on '%s'\n",
3951 __le32_to_cpu(family_num
), s
->disks
->devname
);
3955 static struct intel_super
*
3956 imsm_thunderdome(struct intel_super
**super_list
, int len
)
3958 struct intel_super
*super_table
[len
];
3959 struct intel_disk
*disk_list
= NULL
;
3960 struct intel_super
*champion
, *spare
;
3961 struct intel_super
*s
, **del
;
3966 memset(super_table
, 0, sizeof(super_table
));
3967 for (s
= *super_list
; s
; s
= s
->next
)
3968 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
3970 for (i
= 0; i
< tbl_size
; i
++) {
3971 struct imsm_disk
*d
;
3972 struct intel_disk
*idisk
;
3973 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
3976 d
= &s
->disks
->disk
;
3978 /* 'd' must appear in merged disk list for its
3979 * configuration to be valid
3981 idisk
= disk_list_get(d
->serial
, disk_list
);
3982 if (idisk
&& idisk
->owner
== i
)
3983 s
= validate_members(s
, disk_list
, i
);
3988 dprintf("%s: marking family: %#x from %d:%d offline\n",
3989 __func__
, mpb
->family_num
,
3990 super_table
[i
]->disks
->major
,
3991 super_table
[i
]->disks
->minor
);
3995 /* This is where the mdadm implementation differs from the Windows
3996 * driver which has no strict concept of a container. We can only
3997 * assemble one family from a container, so when returning a prodigal
3998 * array member to this system the code will not be able to disambiguate
3999 * the container contents that should be assembled ("foreign" versus
4000 * "local"). It requires user intervention to set the orig_family_num
4001 * to a new value to establish a new container. The Windows driver in
4002 * this situation fixes up the volume name in place and manages the
4003 * foreign array as an independent entity.
4008 for (i
= 0; i
< tbl_size
; i
++) {
4009 struct intel_super
*tbl_ent
= super_table
[i
];
4015 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
4020 if (s
&& !is_spare
) {
4021 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
4023 } else if (!s
&& !is_spare
)
4036 fprintf(stderr
, "Chose family %#x on '%s', "
4037 "assemble conflicts to new container with '--update=uuid'\n",
4038 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
4040 /* collect all dl's onto 'champion', and update them to
4041 * champion's version of the status
4043 for (s
= *super_list
; s
; s
= s
->next
) {
4044 struct imsm_super
*mpb
= champion
->anchor
;
4045 struct dl
*dl
= s
->disks
;
4050 mpb
->attributes
|= s
->anchor
->attributes
& MPB_ATTRIB_2TB_DISK
;
4052 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4053 struct imsm_disk
*disk
;
4055 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
4058 /* only set index on disks that are a member of
4059 * a populated contianer, i.e. one with
4062 if (is_failed(&dl
->disk
))
4064 else if (is_spare(&dl
->disk
))
4070 if (i
>= mpb
->num_disks
) {
4071 struct intel_disk
*idisk
;
4073 idisk
= disk_list_get(dl
->serial
, disk_list
);
4074 if (idisk
&& is_spare(&idisk
->disk
) &&
4075 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
4083 dl
->next
= champion
->disks
;
4084 champion
->disks
= dl
;
4088 /* delete 'champion' from super_list */
4089 for (del
= super_list
; *del
; ) {
4090 if (*del
== champion
) {
4091 *del
= (*del
)->next
;
4094 del
= &(*del
)->next
;
4096 champion
->next
= NULL
;
4100 struct intel_disk
*idisk
= disk_list
;
4102 disk_list
= disk_list
->next
;
4111 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
);
4112 static int get_super_block(struct intel_super
**super_list
, int devnum
, char *devname
,
4113 int major
, int minor
, int keep_fd
);
4115 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4116 int *max
, int keep_fd
);
4119 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
4120 char *devname
, struct md_list
*devlist
,
4123 struct intel_super
*super_list
= NULL
;
4124 struct intel_super
*super
= NULL
;
4129 /* 'fd' is an opened container */
4130 err
= get_sra_super_block(fd
, &super_list
, devname
, &i
, keep_fd
);
4132 /* get super block from devlist devices */
4133 err
= get_devlist_super_block(devlist
, &super_list
, &i
, keep_fd
);
4136 /* all mpbs enter, maybe one leaves */
4137 super
= imsm_thunderdome(&super_list
, i
);
4143 if (find_missing(super
) != 0) {
4149 /* load migration record */
4150 err
= load_imsm_migr_rec(super
, NULL
);
4152 /* migration is in progress,
4153 * but migr_rec cannot be loaded,
4159 /* Check migration compatibility */
4160 if ((err
== 0) && (check_mpb_migr_compatibility(super
) != 0)) {
4161 pr_err("Unsupported migration detected");
4163 fprintf(stderr
, " on %s\n", devname
);
4165 fprintf(stderr
, " (IMSM).\n");
4174 while (super_list
) {
4175 struct intel_super
*s
= super_list
;
4177 super_list
= super_list
->next
;
4187 st
->container_dev
= fd2devnum(fd
);
4189 st
->container_dev
= NoMdDev
;
4190 if (err
== 0 && st
->ss
== NULL
) {
4191 st
->ss
= &super_imsm
;
4192 st
->minor_version
= 0;
4193 st
->max_devs
= IMSM_MAX_DEVICES
;
4200 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4201 int *max
, int keep_fd
)
4203 struct md_list
*tmpdev
;
4207 for (i
= 0, tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
4208 if (tmpdev
->used
!= 1)
4210 if (tmpdev
->container
== 1) {
4212 int fd
= dev_open(tmpdev
->devname
, O_RDONLY
|O_EXCL
);
4214 pr_err("cannot open device %s: %s\n",
4215 tmpdev
->devname
, strerror(errno
));
4219 err
= get_sra_super_block(fd
, super_list
,
4220 tmpdev
->devname
, &lmax
,
4229 int major
= major(tmpdev
->st_rdev
);
4230 int minor
= minor(tmpdev
->st_rdev
);
4231 err
= get_super_block(super_list
,
4248 static int get_super_block(struct intel_super
**super_list
, int devnum
, char *devname
,
4249 int major
, int minor
, int keep_fd
)
4251 struct intel_super
*s
= NULL
;
4264 sprintf(nm
, "%d:%d", major
, minor
);
4265 dfd
= dev_open(nm
, O_RDWR
);
4271 rv
= find_intel_hba_capability(dfd
, s
, devname
);
4272 /* no orom/efi or non-intel hba of the disk */
4278 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4280 /* retry the load if we might have raced against mdmon */
4281 if (err
== 3 && (devnum
!= -1) && mdmon_running(devnum
))
4282 for (retry
= 0; retry
< 3; retry
++) {
4284 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4290 s
->next
= *super_list
;
4298 if ((dfd
>= 0) && (!keep_fd
))
4305 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
)
4312 sra
= sysfs_read(fd
, 0, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
4316 if (sra
->array
.major_version
!= -1 ||
4317 sra
->array
.minor_version
!= -2 ||
4318 strcmp(sra
->text_version
, "imsm") != 0) {
4323 devnum
= fd2devnum(fd
);
4324 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
4325 if (get_super_block(super_list
, devnum
, devname
,
4326 sd
->disk
.major
, sd
->disk
.minor
, keep_fd
) != 0) {
4337 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
4339 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
, NULL
, 1);
4343 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
4345 struct intel_super
*super
;
4348 if (test_partition(fd
))
4349 /* IMSM not allowed on partitions */
4352 free_super_imsm(st
);
4354 super
= alloc_super();
4355 /* Load hba and capabilities if they exist.
4356 * But do not preclude loading metadata in case capabilities or hba are
4357 * non-compliant and ignore_hw_compat is set.
4359 rv
= find_intel_hba_capability(fd
, super
, devname
);
4360 /* no orom/efi or non-intel hba of the disk */
4361 if ((rv
!= 0) && (st
->ignore_hw_compat
== 0)) {
4363 pr_err("No OROM/EFI properties for %s\n", devname
);
4367 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
4371 pr_err("Failed to load all information "
4372 "sections on %s\n", devname
);
4378 if (st
->ss
== NULL
) {
4379 st
->ss
= &super_imsm
;
4380 st
->minor_version
= 0;
4381 st
->max_devs
= IMSM_MAX_DEVICES
;
4384 /* load migration record */
4385 if (load_imsm_migr_rec(super
, NULL
) == 0) {
4386 /* Check for unsupported migration features */
4387 if (check_mpb_migr_compatibility(super
) != 0) {
4388 pr_err("Unsupported migration detected");
4390 fprintf(stderr
, " on %s\n", devname
);
4392 fprintf(stderr
, " (IMSM).\n");
4400 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
4402 if (info
->level
== 1)
4404 return info
->chunk_size
>> 9;
4407 static unsigned long long info_to_blocks_per_member(mdu_array_info_t
*info
,
4408 unsigned long long size
)
4410 if (info
->level
== 1)
4413 return (size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
4416 static void imsm_update_version_info(struct intel_super
*super
)
4418 /* update the version and attributes */
4419 struct imsm_super
*mpb
= super
->anchor
;
4421 struct imsm_dev
*dev
;
4422 struct imsm_map
*map
;
4425 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4426 dev
= get_imsm_dev(super
, i
);
4427 map
= get_imsm_map(dev
, MAP_0
);
4428 if (__le32_to_cpu(dev
->size_high
) > 0)
4429 mpb
->attributes
|= MPB_ATTRIB_2TB
;
4431 /* FIXME detect when an array spans a port multiplier */
4433 mpb
->attributes
|= MPB_ATTRIB_PM
;
4436 if (mpb
->num_raid_devs
> 1 ||
4437 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
4438 version
= MPB_VERSION_ATTRIBS
;
4439 switch (get_imsm_raid_level(map
)) {
4440 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
4441 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
4442 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
4443 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
4446 if (map
->num_members
>= 5)
4447 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
4448 else if (dev
->status
== DEV_CLONE_N_GO
)
4449 version
= MPB_VERSION_CNG
;
4450 else if (get_imsm_raid_level(map
) == 5)
4451 version
= MPB_VERSION_RAID5
;
4452 else if (map
->num_members
>= 3)
4453 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
4454 else if (get_imsm_raid_level(map
) == 1)
4455 version
= MPB_VERSION_RAID1
;
4457 version
= MPB_VERSION_RAID0
;
4459 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
4463 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
4465 struct imsm_super
*mpb
= super
->anchor
;
4466 char *reason
= NULL
;
4469 if (strlen(name
) > MAX_RAID_SERIAL_LEN
)
4470 reason
= "must be 16 characters or less";
4472 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4473 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4475 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
4476 reason
= "already exists";
4481 if (reason
&& !quiet
)
4482 pr_err("imsm volume name %s\n", reason
);
4487 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
4488 unsigned long long size
, char *name
,
4489 char *homehost
, int *uuid
)
4491 /* We are creating a volume inside a pre-existing container.
4492 * so st->sb is already set.
4494 struct intel_super
*super
= st
->sb
;
4495 struct imsm_super
*mpb
= super
->anchor
;
4496 struct intel_dev
*dv
;
4497 struct imsm_dev
*dev
;
4498 struct imsm_vol
*vol
;
4499 struct imsm_map
*map
;
4500 int idx
= mpb
->num_raid_devs
;
4502 unsigned long long array_blocks
;
4503 size_t size_old
, size_new
;
4504 unsigned long long num_data_stripes
;
4506 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
4507 pr_err("This imsm-container already has the "
4508 "maximum of %d volumes\n", super
->orom
->vpa
);
4512 /* ensure the mpb is large enough for the new data */
4513 size_old
= __le32_to_cpu(mpb
->mpb_size
);
4514 size_new
= disks_to_mpb_size(info
->nr_disks
);
4515 if (size_new
> size_old
) {
4517 size_t size_round
= ROUND_UP(size_new
, 512);
4519 if (posix_memalign(&mpb_new
, 512, size_round
) != 0) {
4520 pr_err("could not allocate new mpb\n");
4523 if (posix_memalign(&super
->migr_rec_buf
, 512,
4524 MIGR_REC_BUF_SIZE
) != 0) {
4525 pr_err("%s could not allocate migr_rec buffer\n",
4532 memcpy(mpb_new
, mpb
, size_old
);
4535 super
->anchor
= mpb_new
;
4536 mpb
->mpb_size
= __cpu_to_le32(size_new
);
4537 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
4539 super
->current_vol
= idx
;
4541 /* handle 'failed_disks' by either:
4542 * a) create dummy disk entries in the table if this the first
4543 * volume in the array. We add them here as this is the only
4544 * opportunity to add them. add_to_super_imsm_volume()
4545 * handles the non-failed disks and continues incrementing
4547 * b) validate that 'failed_disks' matches the current number
4548 * of missing disks if the container is populated
4550 if (super
->current_vol
== 0) {
4552 for (i
= 0; i
< info
->failed_disks
; i
++) {
4553 struct imsm_disk
*disk
;
4556 disk
= __get_imsm_disk(mpb
, i
);
4557 disk
->status
= CONFIGURED_DISK
| FAILED_DISK
;
4558 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
4559 snprintf((char *) disk
->serial
, MAX_RAID_SERIAL_LEN
,
4562 find_missing(super
);
4567 for (d
= super
->missing
; d
; d
= d
->next
)
4569 if (info
->failed_disks
> missing
) {
4570 pr_err("unable to add 'missing' disk to container\n");
4575 if (!check_name(super
, name
, 0))
4577 dv
= xmalloc(sizeof(*dv
));
4578 dev
= xcalloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
4579 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
4580 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
4581 info
->layout
, info
->chunk_size
,
4583 /* round array size down to closest MB */
4584 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
4586 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
4587 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
4588 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
4590 vol
->migr_state
= 0;
4591 set_migr_type(dev
, MIGR_INIT
);
4592 vol
->dirty
= !info
->state
;
4593 vol
->curr_migr_unit
= 0;
4594 map
= get_imsm_map(dev
, MAP_0
);
4595 set_pba_of_lba0(map
, super
->create_offset
);
4596 set_blocks_per_member(map
, info_to_blocks_per_member(info
, size
));
4597 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
4598 map
->failed_disk_num
= ~0;
4599 if (info
->level
> 0)
4600 map
->map_state
= IMSM_T_STATE_UNINITIALIZED
;
4602 map
->map_state
= info
->failed_disks
? IMSM_T_STATE_FAILED
:
4603 IMSM_T_STATE_NORMAL
;
4606 if (info
->level
== 1 && info
->raid_disks
> 2) {
4609 pr_err("imsm does not support more than 2 disks"
4610 "in a raid1 volume\n");
4614 map
->raid_level
= info
->level
;
4615 if (info
->level
== 10) {
4616 map
->raid_level
= 1;
4617 map
->num_domains
= info
->raid_disks
/ 2;
4618 } else if (info
->level
== 1)
4619 map
->num_domains
= info
->raid_disks
;
4621 map
->num_domains
= 1;
4623 /* info->size is only int so use the 'size' parameter instead */
4624 num_data_stripes
= (size
* 2) / info_to_blocks_per_strip(info
);
4625 num_data_stripes
/= map
->num_domains
;
4626 set_num_data_stripes(map
, num_data_stripes
);
4628 map
->num_members
= info
->raid_disks
;
4629 for (i
= 0; i
< map
->num_members
; i
++) {
4630 /* initialized in add_to_super */
4631 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
4633 mpb
->num_raid_devs
++;
4636 dv
->index
= super
->current_vol
;
4637 dv
->next
= super
->devlist
;
4638 super
->devlist
= dv
;
4640 imsm_update_version_info(super
);
4645 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
4646 unsigned long long size
, char *name
,
4647 char *homehost
, int *uuid
)
4649 /* This is primarily called by Create when creating a new array.
4650 * We will then get add_to_super called for each component, and then
4651 * write_init_super called to write it out to each device.
4652 * For IMSM, Create can create on fresh devices or on a pre-existing
4654 * To create on a pre-existing array a different method will be called.
4655 * This one is just for fresh drives.
4657 struct intel_super
*super
;
4658 struct imsm_super
*mpb
;
4663 return init_super_imsm_volume(st
, info
, size
, name
, homehost
, uuid
);
4666 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
4670 super
= alloc_super();
4671 if (super
&& posix_memalign(&super
->buf
, 512, mpb_size
) != 0) {
4676 pr_err("%s could not allocate superblock\n", __func__
);
4679 if (posix_memalign(&super
->migr_rec_buf
, 512, MIGR_REC_BUF_SIZE
) != 0) {
4680 pr_err("%s could not allocate migr_rec buffer\n", __func__
);
4685 memset(super
->buf
, 0, mpb_size
);
4687 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
4691 /* zeroing superblock */
4695 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
4697 version
= (char *) mpb
->sig
;
4698 strcpy(version
, MPB_SIGNATURE
);
4699 version
+= strlen(MPB_SIGNATURE
);
4700 strcpy(version
, MPB_VERSION_RAID0
);
4706 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
4707 int fd
, char *devname
)
4709 struct intel_super
*super
= st
->sb
;
4710 struct imsm_super
*mpb
= super
->anchor
;
4711 struct imsm_disk
*_disk
;
4712 struct imsm_dev
*dev
;
4713 struct imsm_map
*map
;
4717 dev
= get_imsm_dev(super
, super
->current_vol
);
4718 map
= get_imsm_map(dev
, MAP_0
);
4720 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
4721 pr_err("%s: Cannot add spare devices to IMSM volume\n",
4727 /* we're doing autolayout so grab the pre-marked (in
4728 * validate_geometry) raid_disk
4730 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4731 if (dl
->raiddisk
== dk
->raid_disk
)
4734 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4735 if (dl
->major
== dk
->major
&&
4736 dl
->minor
== dk
->minor
)
4741 pr_err("%s is not a member of the same container\n", devname
);
4745 /* add a pristine spare to the metadata */
4746 if (dl
->index
< 0) {
4747 dl
->index
= super
->anchor
->num_disks
;
4748 super
->anchor
->num_disks
++;
4750 /* Check the device has not already been added */
4751 slot
= get_imsm_disk_slot(map
, dl
->index
);
4753 (get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
) & IMSM_ORD_REBUILD
) == 0) {
4754 pr_err("%s has been included in this array twice\n",
4758 set_imsm_ord_tbl_ent(map
, dk
->raid_disk
, dl
->index
);
4759 dl
->disk
.status
= CONFIGURED_DISK
;
4761 /* update size of 'missing' disks to be at least as large as the
4762 * largest acitve member (we only have dummy missing disks when
4763 * creating the first volume)
4765 if (super
->current_vol
== 0) {
4766 for (df
= super
->missing
; df
; df
= df
->next
) {
4767 if (total_blocks(&dl
->disk
) > total_blocks(&df
->disk
))
4768 set_total_blocks(&df
->disk
, total_blocks(&dl
->disk
));
4769 _disk
= __get_imsm_disk(mpb
, df
->index
);
4774 /* refresh unset/failed slots to point to valid 'missing' entries */
4775 for (df
= super
->missing
; df
; df
= df
->next
)
4776 for (slot
= 0; slot
< mpb
->num_disks
; slot
++) {
4777 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
4779 if ((ord
& IMSM_ORD_REBUILD
) == 0)
4781 set_imsm_ord_tbl_ent(map
, slot
, df
->index
| IMSM_ORD_REBUILD
);
4782 if (is_gen_migration(dev
)) {
4783 struct imsm_map
*map2
= get_imsm_map(dev
,
4785 int slot2
= get_imsm_disk_slot(map2
, df
->index
);
4786 if ((slot2
< map2
->num_members
) &&
4788 __u32 ord2
= get_imsm_ord_tbl_ent(dev
,
4791 if ((unsigned)df
->index
==
4793 set_imsm_ord_tbl_ent(map2
,
4799 dprintf("set slot:%d to missing disk:%d\n", slot
, df
->index
);
4803 /* if we are creating the first raid device update the family number */
4804 if (super
->current_vol
== 0) {
4806 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
4808 _disk
= __get_imsm_disk(mpb
, dl
->index
);
4809 if (!_dev
|| !_disk
) {
4810 pr_err("BUG mpb setup error\n");
4816 sum
+= __gen_imsm_checksum(mpb
);
4817 mpb
->family_num
= __cpu_to_le32(sum
);
4818 mpb
->orig_family_num
= mpb
->family_num
;
4820 super
->current_disk
= dl
;
4825 * Function marks disk as spare and restores disk serial
4826 * in case it was previously marked as failed by takeover operation
4828 * -1 : critical error
4829 * 0 : disk is marked as spare but serial is not set
4832 int mark_spare(struct dl
*disk
)
4834 __u8 serial
[MAX_RAID_SERIAL_LEN
];
4841 if (!imsm_read_serial(disk
->fd
, NULL
, serial
)) {
4842 /* Restore disk serial number, because takeover marks disk
4843 * as failed and adds to serial ':0' before it becomes
4846 serialcpy(disk
->serial
, serial
);
4847 serialcpy(disk
->disk
.serial
, serial
);
4850 disk
->disk
.status
= SPARE_DISK
;
4856 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
4857 int fd
, char *devname
)
4859 struct intel_super
*super
= st
->sb
;
4861 unsigned long long size
;
4866 /* If we are on an RAID enabled platform check that the disk is
4867 * attached to the raid controller.
4868 * We do not need to test disks attachment for container based additions,
4869 * they shall be already tested when container was created/assembled.
4871 rv
= find_intel_hba_capability(fd
, super
, devname
);
4872 /* no orom/efi or non-intel hba of the disk */
4874 dprintf("capability: %p fd: %d ret: %d\n",
4875 super
->orom
, fd
, rv
);
4879 if (super
->current_vol
>= 0)
4880 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
4883 dd
= xcalloc(sizeof(*dd
), 1);
4884 dd
->major
= major(stb
.st_rdev
);
4885 dd
->minor
= minor(stb
.st_rdev
);
4886 dd
->devname
= devname
? xstrdup(devname
) : NULL
;
4889 dd
->action
= DISK_ADD
;
4890 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
4892 pr_err("failed to retrieve scsi serial, aborting\n");
4897 get_dev_size(fd
, NULL
, &size
);
4899 serialcpy(dd
->disk
.serial
, dd
->serial
);
4900 set_total_blocks(&dd
->disk
, size
);
4901 if (__le32_to_cpu(dd
->disk
.total_blocks_hi
) > 0) {
4902 struct imsm_super
*mpb
= super
->anchor
;
4903 mpb
->attributes
|= MPB_ATTRIB_2TB_DISK
;
4906 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
4907 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
4909 dd
->disk
.scsi_id
= __cpu_to_le32(0);
4911 if (st
->update_tail
) {
4912 dd
->next
= super
->disk_mgmt_list
;
4913 super
->disk_mgmt_list
= dd
;
4915 dd
->next
= super
->disks
;
4917 super
->updates_pending
++;
4924 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
4926 struct intel_super
*super
= st
->sb
;
4929 /* remove from super works only in mdmon - for communication
4930 * manager - monitor. Check if communication memory buffer
4933 if (!st
->update_tail
) {
4934 pr_err("%s shall be used in mdmon context only"
4935 "(line %d).\n", __func__
, __LINE__
);
4938 dd
= xcalloc(1, sizeof(*dd
));
4939 dd
->major
= dk
->major
;
4940 dd
->minor
= dk
->minor
;
4943 dd
->action
= DISK_REMOVE
;
4945 dd
->next
= super
->disk_mgmt_list
;
4946 super
->disk_mgmt_list
= dd
;
4952 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
4956 struct imsm_super anchor
;
4957 } spare_record
__attribute__ ((aligned(512)));
4959 /* spare records have their own family number and do not have any defined raid
4962 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
4964 struct imsm_super
*mpb
= super
->anchor
;
4965 struct imsm_super
*spare
= &spare_record
.anchor
;
4969 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
)),
4970 spare
->generation_num
= __cpu_to_le32(1UL),
4971 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
4972 spare
->num_disks
= 1,
4973 spare
->num_raid_devs
= 0,
4974 spare
->cache_size
= mpb
->cache_size
,
4975 spare
->pwr_cycle_count
= __cpu_to_le32(1),
4977 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
4978 MPB_SIGNATURE MPB_VERSION_RAID0
);
4980 for (d
= super
->disks
; d
; d
= d
->next
) {
4984 spare
->disk
[0] = d
->disk
;
4985 if (__le32_to_cpu(d
->disk
.total_blocks_hi
) > 0)
4986 spare
->attributes
|= MPB_ATTRIB_2TB_DISK
;
4988 sum
= __gen_imsm_checksum(spare
);
4989 spare
->family_num
= __cpu_to_le32(sum
);
4990 spare
->orig_family_num
= 0;
4991 sum
= __gen_imsm_checksum(spare
);
4992 spare
->check_sum
= __cpu_to_le32(sum
);
4994 if (store_imsm_mpb(d
->fd
, spare
)) {
4995 fprintf(stderr
, "%s: failed for device %d:%d %s\n",
4996 __func__
, d
->major
, d
->minor
, strerror(errno
));
5008 static int write_super_imsm(struct supertype
*st
, int doclose
)
5010 struct intel_super
*super
= st
->sb
;
5011 struct imsm_super
*mpb
= super
->anchor
;
5017 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
5019 int clear_migration_record
= 1;
5021 /* 'generation' is incremented everytime the metadata is written */
5022 generation
= __le32_to_cpu(mpb
->generation_num
);
5024 mpb
->generation_num
= __cpu_to_le32(generation
);
5026 /* fix up cases where previous mdadm releases failed to set
5029 if (mpb
->orig_family_num
== 0)
5030 mpb
->orig_family_num
= mpb
->family_num
;
5032 for (d
= super
->disks
; d
; d
= d
->next
) {
5036 mpb
->disk
[d
->index
] = d
->disk
;
5040 for (d
= super
->missing
; d
; d
= d
->next
) {
5041 mpb
->disk
[d
->index
] = d
->disk
;
5044 mpb
->num_disks
= num_disks
;
5045 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
5047 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5048 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
5049 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
5051 imsm_copy_dev(dev
, dev2
);
5052 mpb_size
+= sizeof_imsm_dev(dev
, 0);
5054 if (is_gen_migration(dev2
))
5055 clear_migration_record
= 0;
5057 mpb_size
+= __le32_to_cpu(mpb
->bbm_log_size
);
5058 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5060 /* recalculate checksum */
5061 sum
= __gen_imsm_checksum(mpb
);
5062 mpb
->check_sum
= __cpu_to_le32(sum
);
5064 if (super
->clean_migration_record_by_mdmon
) {
5065 clear_migration_record
= 1;
5066 super
->clean_migration_record_by_mdmon
= 0;
5068 if (clear_migration_record
)
5069 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SIZE
);
5071 /* write the mpb for disks that compose raid devices */
5072 for (d
= super
->disks
; d
; d
= d
->next
) {
5073 if (d
->index
< 0 || is_failed(&d
->disk
))
5076 if (clear_migration_record
) {
5077 unsigned long long dsize
;
5079 get_dev_size(d
->fd
, NULL
, &dsize
);
5080 if (lseek64(d
->fd
, dsize
- 512, SEEK_SET
) >= 0) {
5081 if (write(d
->fd
, super
->migr_rec_buf
,
5082 MIGR_REC_BUF_SIZE
) != MIGR_REC_BUF_SIZE
)
5083 perror("Write migr_rec failed");
5087 if (store_imsm_mpb(d
->fd
, mpb
))
5089 "%s: failed for device %d:%d (fd: %d)%s\n",
5090 __func__
, d
->major
, d
->minor
,
5091 d
->fd
, strerror(errno
));
5100 return write_super_imsm_spares(super
, doclose
);
5106 static int create_array(struct supertype
*st
, int dev_idx
)
5109 struct imsm_update_create_array
*u
;
5110 struct intel_super
*super
= st
->sb
;
5111 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
5112 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
5113 struct disk_info
*inf
;
5114 struct imsm_disk
*disk
;
5117 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
5118 sizeof(*inf
) * map
->num_members
;
5120 u
->type
= update_create_array
;
5121 u
->dev_idx
= dev_idx
;
5122 imsm_copy_dev(&u
->dev
, dev
);
5123 inf
= get_disk_info(u
);
5124 for (i
= 0; i
< map
->num_members
; i
++) {
5125 int idx
= get_imsm_disk_idx(dev
, i
, MAP_X
);
5127 disk
= get_imsm_disk(super
, idx
);
5128 serialcpy(inf
[i
].serial
, disk
->serial
);
5130 append_metadata_update(st
, u
, len
);
5135 static int mgmt_disk(struct supertype
*st
)
5137 struct intel_super
*super
= st
->sb
;
5139 struct imsm_update_add_remove_disk
*u
;
5141 if (!super
->disk_mgmt_list
)
5146 u
->type
= update_add_remove_disk
;
5147 append_metadata_update(st
, u
, len
);
5152 static int write_init_super_imsm(struct supertype
*st
)
5154 struct intel_super
*super
= st
->sb
;
5155 int current_vol
= super
->current_vol
;
5157 /* we are done with current_vol reset it to point st at the container */
5158 super
->current_vol
= -1;
5160 if (st
->update_tail
) {
5161 /* queue the recently created array / added disk
5162 * as a metadata update */
5165 /* determine if we are creating a volume or adding a disk */
5166 if (current_vol
< 0) {
5167 /* in the mgmt (add/remove) disk case we are running
5168 * in mdmon context, so don't close fd's
5170 return mgmt_disk(st
);
5172 rv
= create_array(st
, current_vol
);
5177 for (d
= super
->disks
; d
; d
= d
->next
)
5178 Kill(d
->devname
, NULL
, 0, -1, 1);
5179 return write_super_imsm(st
, 1);
5184 static int store_super_imsm(struct supertype
*st
, int fd
)
5186 struct intel_super
*super
= st
->sb
;
5187 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
5193 return store_imsm_mpb(fd
, mpb
);
5199 static int imsm_bbm_log_size(struct imsm_super
*mpb
)
5201 return __le32_to_cpu(mpb
->bbm_log_size
);
5205 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
5206 int layout
, int raiddisks
, int chunk
,
5207 unsigned long long size
, char *dev
,
5208 unsigned long long *freesize
,
5212 unsigned long long ldsize
;
5213 struct intel_super
*super
=NULL
;
5216 if (level
!= LEVEL_CONTAINER
)
5221 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
5224 pr_err("imsm: Cannot open %s: %s\n",
5225 dev
, strerror(errno
));
5228 if (!get_dev_size(fd
, dev
, &ldsize
)) {
5233 /* capabilities retrieve could be possible
5234 * note that there is no fd for the disks in array.
5236 super
= alloc_super();
5237 rv
= find_intel_hba_capability(fd
, super
, verbose
> 0 ? dev
: NULL
);
5241 fd2devname(fd
, str
);
5242 dprintf("validate_geometry_imsm_container: fd: %d %s orom: %p rv: %d raiddisk: %d\n",
5243 fd
, str
, super
->orom
, rv
, raiddisks
);
5245 /* no orom/efi or non-intel hba of the disk */
5252 if (raiddisks
> super
->orom
->tds
) {
5254 pr_err("%d exceeds maximum number of"
5255 " platform supported disks: %d\n",
5256 raiddisks
, super
->orom
->tds
);
5260 if ((super
->orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) == 0 &&
5261 (ldsize
>> 9) >> 32 > 0) {
5263 pr_err("%s exceeds maximum platform supported size\n", dev
);
5269 *freesize
= avail_size_imsm(st
, ldsize
>> 9);
5275 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
5277 const unsigned long long base_start
= e
[*idx
].start
;
5278 unsigned long long end
= base_start
+ e
[*idx
].size
;
5281 if (base_start
== end
)
5285 for (i
= *idx
; i
< num_extents
; i
++) {
5286 /* extend overlapping extents */
5287 if (e
[i
].start
>= base_start
&&
5288 e
[i
].start
<= end
) {
5291 if (e
[i
].start
+ e
[i
].size
> end
)
5292 end
= e
[i
].start
+ e
[i
].size
;
5293 } else if (e
[i
].start
> end
) {
5299 return end
- base_start
;
5302 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
5304 /* build a composite disk with all known extents and generate a new
5305 * 'maxsize' given the "all disks in an array must share a common start
5306 * offset" constraint
5308 struct extent
*e
= xcalloc(sum_extents
, sizeof(*e
));
5312 unsigned long long pos
;
5313 unsigned long long start
= 0;
5314 unsigned long long maxsize
;
5315 unsigned long reserve
;
5317 /* coalesce and sort all extents. also, check to see if we need to
5318 * reserve space between member arrays
5321 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5324 for (i
= 0; i
< dl
->extent_cnt
; i
++)
5327 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
5332 while (i
< sum_extents
) {
5333 e
[j
].start
= e
[i
].start
;
5334 e
[j
].size
= find_size(e
, &i
, sum_extents
);
5336 if (e
[j
-1].size
== 0)
5345 unsigned long long esize
;
5347 esize
= e
[i
].start
- pos
;
5348 if (esize
>= maxsize
) {
5353 pos
= e
[i
].start
+ e
[i
].size
;
5355 } while (e
[i
-1].size
);
5361 /* FIXME assumes volume at offset 0 is the first volume in a
5364 if (start_extent
> 0)
5365 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
5369 if (maxsize
< reserve
)
5372 super
->create_offset
= ~((unsigned long long) 0);
5373 if (start
+ reserve
> super
->create_offset
)
5374 return 0; /* start overflows create_offset */
5375 super
->create_offset
= start
+ reserve
;
5377 return maxsize
- reserve
;
5380 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
5382 if (level
< 0 || level
== 6 || level
== 4)
5385 /* if we have an orom prevent invalid raid levels */
5388 case 0: return imsm_orom_has_raid0(orom
);
5391 return imsm_orom_has_raid1e(orom
);
5392 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
5393 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
5394 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
5397 return 1; /* not on an Intel RAID platform so anything goes */
5404 active_arrays_by_format(char *name
, char* hba
, struct md_list
**devlist
,
5405 int dpa
, int verbose
)
5407 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
5408 struct mdstat_ent
*memb
= NULL
;
5411 struct md_list
*dv
= NULL
;
5414 for (memb
= mdstat
; memb
; memb
= memb
->next
) {
5415 if (memb
->metadata_version
&&
5416 (strncmp(memb
->metadata_version
, "external:", 9) == 0) &&
5417 (strcmp(&memb
->metadata_version
[9], name
) == 0) &&
5418 !is_subarray(memb
->metadata_version
+9) &&
5420 struct dev_member
*dev
= memb
->members
;
5422 while(dev
&& (fd
< 0)) {
5423 char *path
= xmalloc(strlen(dev
->name
) + strlen("/dev/") + 1);
5424 num
= sprintf(path
, "%s%s", "/dev/", dev
->name
);
5426 fd
= open(path
, O_RDONLY
, 0);
5427 if ((num
<= 0) || (fd
< 0)) {
5428 pr_vrb(": Cannot open %s: %s\n",
5429 dev
->name
, strerror(errno
));
5435 if ((fd
>= 0) && disk_attached_to_hba(fd
, hba
)) {
5436 struct mdstat_ent
*vol
;
5437 for (vol
= mdstat
; vol
; vol
= vol
->next
) {
5438 if ((vol
->active
> 0) &&
5439 vol
->metadata_version
&&
5440 is_container_member(vol
, memb
->dev
)) {
5445 if (*devlist
&& (found
< dpa
)) {
5446 dv
= xcalloc(1, sizeof(*dv
));
5447 dv
->devname
= xmalloc(strlen(memb
->dev
) + strlen("/dev/") + 1);
5448 sprintf(dv
->devname
, "%s%s", "/dev/", memb
->dev
);
5451 dv
->next
= *devlist
;
5459 free_mdstat(mdstat
);
5464 static struct md_list
*
5465 get_loop_devices(void)
5468 struct md_list
*devlist
= NULL
;
5469 struct md_list
*dv
= NULL
;
5471 for(i
= 0; i
< 12; i
++) {
5472 dv
= xcalloc(1, sizeof(*dv
));
5473 dv
->devname
= xmalloc(40);
5474 sprintf(dv
->devname
, "/dev/loop%d", i
);
5482 static struct md_list
*
5483 get_devices(const char *hba_path
)
5485 struct md_list
*devlist
= NULL
;
5486 struct md_list
*dv
= NULL
;
5492 devlist
= get_loop_devices();
5495 /* scroll through /sys/dev/block looking for devices attached to
5498 dir
= opendir("/sys/dev/block");
5499 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
5504 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
5506 path
= devt_to_devpath(makedev(major
, minor
));
5509 if (!path_attached_to_hba(path
, hba_path
)) {
5516 fd
= dev_open(ent
->d_name
, O_RDONLY
);
5518 fd2devname(fd
, buf
);
5521 pr_err("cannot open device: %s\n",
5527 dv
= xcalloc(1, sizeof(*dv
));
5528 dv
->devname
= xstrdup(buf
);
5535 devlist
= devlist
->next
;
5545 count_volumes_list(struct md_list
*devlist
, char *homehost
,
5546 int verbose
, int *found
)
5548 struct md_list
*tmpdev
;
5550 struct supertype
*st
= NULL
;
5552 /* first walk the list of devices to find a consistent set
5553 * that match the criterea, if that is possible.
5554 * We flag the ones we like with 'used'.
5557 st
= match_metadata_desc_imsm("imsm");
5559 pr_vrb(": cannot allocate memory for imsm supertype\n");
5563 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5564 char *devname
= tmpdev
->devname
;
5566 struct supertype
*tst
;
5568 if (tmpdev
->used
> 1)
5570 tst
= dup_super(st
);
5572 pr_vrb(": cannot allocate memory for imsm supertype\n");
5575 tmpdev
->container
= 0;
5576 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
5578 dprintf(": cannot open device %s: %s\n",
5579 devname
, strerror(errno
));
5581 } else if (fstat(dfd
, &stb
)< 0) {
5583 dprintf(": fstat failed for %s: %s\n",
5584 devname
, strerror(errno
));
5586 } else if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
5587 dprintf(": %s is not a block device.\n",
5590 } else if (must_be_container(dfd
)) {
5591 struct supertype
*cst
;
5592 cst
= super_by_fd(dfd
, NULL
);
5594 dprintf(": cannot recognize container type %s\n",
5597 } else if (tst
->ss
!= st
->ss
) {
5598 dprintf(": non-imsm container - ignore it: %s\n",
5601 } else if (!tst
->ss
->load_container
||
5602 tst
->ss
->load_container(tst
, dfd
, NULL
))
5605 tmpdev
->container
= 1;
5608 cst
->ss
->free_super(cst
);
5610 tmpdev
->st_rdev
= stb
.st_rdev
;
5611 if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
5612 dprintf(": no RAID superblock on %s\n",
5615 } else if (tst
->ss
->compare_super
== NULL
) {
5616 dprintf(": Cannot assemble %s metadata on %s\n",
5617 tst
->ss
->name
, devname
);
5623 if (tmpdev
->used
== 2 || tmpdev
->used
== 4) {
5624 /* Ignore unrecognised devices during auto-assembly */
5629 tst
->ss
->getinfo_super(tst
, &info
, NULL
);
5631 if (st
->minor_version
== -1)
5632 st
->minor_version
= tst
->minor_version
;
5634 if (memcmp(info
.uuid
, uuid_zero
,
5635 sizeof(int[4])) == 0) {
5636 /* this is a floating spare. It cannot define
5637 * an array unless there are no more arrays of
5638 * this type to be found. It can be included
5639 * in an array of this type though.
5645 if (st
->ss
!= tst
->ss
||
5646 st
->minor_version
!= tst
->minor_version
||
5647 st
->ss
->compare_super(st
, tst
) != 0) {
5648 /* Some mismatch. If exactly one array matches this host,
5649 * we can resolve on that one.
5650 * Or, if we are auto assembling, we just ignore the second
5653 dprintf(": superblock on %s doesn't match others - assembly aborted\n",
5659 dprintf("found: devname: %s\n", devname
);
5663 tst
->ss
->free_super(tst
);
5667 if ((err
= load_super_imsm_all(st
, -1, &st
->sb
, NULL
, devlist
, 0)) == 0) {
5668 struct mdinfo
*iter
, *head
= st
->ss
->container_content(st
, NULL
);
5669 for (iter
= head
; iter
; iter
= iter
->next
) {
5670 dprintf("content->text_version: %s vol\n",
5671 iter
->text_version
);
5672 if (iter
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
5673 /* do not assemble arrays with unsupported
5675 dprintf(": Cannot activate member %s.\n",
5676 iter
->text_version
);
5683 dprintf(" no valid super block on device list: err: %d %p\n",
5687 dprintf(" no more devices to examin\n");
5690 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5691 if ((tmpdev
->used
== 1) && (tmpdev
->found
)) {
5693 if (count
< tmpdev
->found
)
5696 count
-= tmpdev
->found
;
5699 if (tmpdev
->used
== 1)
5704 st
->ss
->free_super(st
);
5710 count_volumes(char *hba
, int dpa
, int verbose
)
5712 struct md_list
*devlist
= NULL
;
5716 devlist
= get_devices(hba
);
5717 /* if no intel devices return zero volumes */
5718 if (devlist
== NULL
)
5721 count
= active_arrays_by_format("imsm", hba
, &devlist
, dpa
, verbose
);
5722 dprintf(" path: %s active arrays: %d\n", hba
, count
);
5723 if (devlist
== NULL
)
5727 count
+= count_volumes_list(devlist
,
5731 dprintf("found %d count: %d\n", found
, count
);
5734 dprintf("path: %s total number of volumes: %d\n", hba
, count
);
5737 struct md_list
*dv
= devlist
;
5738 devlist
= devlist
->next
;
5745 static int imsm_default_chunk(const struct imsm_orom
*orom
)
5747 /* up to 512 if the plaform supports it, otherwise the platform max.
5748 * 128 if no platform detected
5750 int fs
= max(7, orom
? fls(orom
->sss
) : 0);
5752 return min(512, (1 << fs
));
5756 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
5757 int raiddisks
, int *chunk
, unsigned long long size
, int verbose
)
5759 /* check/set platform and metadata limits/defaults */
5760 if (super
->orom
&& raiddisks
> super
->orom
->dpa
) {
5761 pr_vrb(": platform supports a maximum of %d disks per array\n",
5766 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
5767 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
5768 pr_vrb(": platform does not support raid%d with %d disk%s\n",
5769 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
5773 if (chunk
&& (*chunk
== 0 || *chunk
== UnSet
))
5774 *chunk
= imsm_default_chunk(super
->orom
);
5776 if (super
->orom
&& chunk
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
5777 pr_vrb(": platform does not support a chunk size of: "
5782 if (layout
!= imsm_level_to_layout(level
)) {
5784 pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
5785 else if (level
== 10)
5786 pr_vrb(": imsm raid 10 only supports the n2 layout\n");
5788 pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
5793 if (super
->orom
&& (super
->orom
->attr
& IMSM_OROM_ATTR_2TB
) == 0 && chunk
&&
5794 (calc_array_size(level
, raiddisks
, layout
, *chunk
, size
) >> 32) > 0) {
5795 pr_vrb(": platform does not support a volume size over 2TB\n");
5801 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
5802 * FIX ME add ahci details
5804 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
5805 int layout
, int raiddisks
, int *chunk
,
5806 unsigned long long size
, char *dev
,
5807 unsigned long long *freesize
,
5811 struct intel_super
*super
= st
->sb
;
5812 struct imsm_super
*mpb
;
5814 unsigned long long pos
= 0;
5815 unsigned long long maxsize
;
5819 /* We must have the container info already read in. */
5823 mpb
= super
->anchor
;
5825 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, size
, verbose
)) {
5826 pr_err("RAID gemetry validation failed. "
5827 "Cannot proceed with the action(s).\n");
5831 /* General test: make sure there is space for
5832 * 'raiddisks' device extents of size 'size' at a given
5835 unsigned long long minsize
= size
;
5836 unsigned long long start_offset
= MaxSector
;
5839 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
5840 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5845 e
= get_extents(super
, dl
);
5848 unsigned long long esize
;
5849 esize
= e
[i
].start
- pos
;
5850 if (esize
>= minsize
)
5852 if (found
&& start_offset
== MaxSector
) {
5855 } else if (found
&& pos
!= start_offset
) {
5859 pos
= e
[i
].start
+ e
[i
].size
;
5861 } while (e
[i
-1].size
);
5866 if (dcnt
< raiddisks
) {
5868 pr_err("imsm: Not enough "
5869 "devices with space for this array "
5877 /* This device must be a member of the set */
5878 if (stat(dev
, &stb
) < 0)
5880 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
5882 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5883 if (dl
->major
== (int)major(stb
.st_rdev
) &&
5884 dl
->minor
== (int)minor(stb
.st_rdev
))
5889 pr_err("%s is not in the "
5890 "same imsm set\n", dev
);
5892 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
5893 /* If a volume is present then the current creation attempt
5894 * cannot incorporate new spares because the orom may not
5895 * understand this configuration (all member disks must be
5896 * members of each array in the container).
5898 pr_err("%s is a spare and a volume"
5899 " is already defined for this container\n", dev
);
5900 pr_err("The option-rom requires all member"
5901 " disks to be a member of all volumes\n");
5903 } else if (super
->orom
&& mpb
->num_raid_devs
> 0 &&
5904 mpb
->num_disks
!= raiddisks
) {
5905 pr_err("The option-rom requires all member"
5906 " disks to be a member of all volumes\n");
5910 /* retrieve the largest free space block */
5911 e
= get_extents(super
, dl
);
5916 unsigned long long esize
;
5918 esize
= e
[i
].start
- pos
;
5919 if (esize
>= maxsize
)
5921 pos
= e
[i
].start
+ e
[i
].size
;
5923 } while (e
[i
-1].size
);
5928 pr_err("unable to determine free space for: %s\n",
5932 if (maxsize
< size
) {
5934 pr_err("%s not enough space (%llu < %llu)\n",
5935 dev
, maxsize
, size
);
5939 /* count total number of extents for merge */
5941 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5943 i
+= dl
->extent_cnt
;
5945 maxsize
= merge_extents(super
, i
);
5947 if (!check_env("IMSM_NO_PLATFORM") &&
5948 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
5949 pr_err("attempting to create a second "
5950 "volume with size less then remaining space. "
5955 if (maxsize
< size
|| maxsize
== 0) {
5958 pr_err("no free space"
5959 " left on device. Aborting...\n");
5961 pr_err("not enough space"
5962 " to create volume of given size"
5963 " (%llu < %llu). Aborting...\n",
5969 *freesize
= maxsize
;
5972 int count
= count_volumes(super
->hba
->path
,
5973 super
->orom
->dpa
, verbose
);
5974 if (super
->orom
->vphba
<= count
) {
5975 pr_vrb(": platform does not support more than %d raid volumes.\n",
5976 super
->orom
->vphba
);
5983 static int imsm_get_free_size(struct supertype
*st
, int raiddisks
,
5984 unsigned long long size
, int chunk
,
5985 unsigned long long *freesize
)
5987 struct intel_super
*super
= st
->sb
;
5988 struct imsm_super
*mpb
= super
->anchor
;
5993 unsigned long long maxsize
;
5994 unsigned long long minsize
;
5998 /* find the largest common start free region of the possible disks */
6002 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6008 /* don't activate new spares if we are orom constrained
6009 * and there is already a volume active in the container
6011 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
6014 e
= get_extents(super
, dl
);
6017 for (i
= 1; e
[i
-1].size
; i
++)
6025 maxsize
= merge_extents(super
, extent_cnt
);
6029 minsize
= chunk
* 2;
6031 if (cnt
< raiddisks
||
6032 (super
->orom
&& used
&& used
!= raiddisks
) ||
6033 maxsize
< minsize
||
6035 pr_err("not enough devices with space to create array.\n");
6036 return 0; /* No enough free spaces large enough */
6047 if (!check_env("IMSM_NO_PLATFORM") &&
6048 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6049 pr_err("attempting to create a second "
6050 "volume with size less then remaining space. "
6055 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6057 dl
->raiddisk
= cnt
++;
6061 dprintf("imsm: imsm_get_free_size() returns : %llu\n", size
);
6066 static int reserve_space(struct supertype
*st
, int raiddisks
,
6067 unsigned long long size
, int chunk
,
6068 unsigned long long *freesize
)
6070 struct intel_super
*super
= st
->sb
;
6075 rv
= imsm_get_free_size(st
, raiddisks
, size
, chunk
, freesize
);
6078 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6080 dl
->raiddisk
= cnt
++;
6087 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
6088 int raiddisks
, int *chunk
, unsigned long long size
,
6089 char *dev
, unsigned long long *freesize
,
6097 * if given unused devices create a container
6098 * if given given devices in a container create a member volume
6100 if (level
== LEVEL_CONTAINER
) {
6101 /* Must be a fresh device to add to a container */
6102 return validate_geometry_imsm_container(st
, level
, layout
,
6104 chunk
?*chunk
:0, size
,
6111 struct intel_super
*super
= st
->sb
;
6112 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
6113 raiddisks
, chunk
, size
,
6116 /* we are being asked to automatically layout a
6117 * new volume based on the current contents of
6118 * the container. If the the parameters can be
6119 * satisfied reserve_space will record the disks,
6120 * start offset, and size of the volume to be
6121 * created. add_to_super and getinfo_super
6122 * detect when autolayout is in progress.
6124 /* assuming that freesize is always given when array is
6126 if (super
->orom
&& freesize
) {
6128 count
= count_volumes(super
->hba
->path
,
6129 super
->orom
->dpa
, verbose
);
6130 if (super
->orom
->vphba
<= count
) {
6131 pr_vrb(": platform does not support more"
6132 " than %d raid volumes.\n",
6133 super
->orom
->vphba
);
6138 return reserve_space(st
, raiddisks
, size
,
6139 chunk
?*chunk
:0, freesize
);
6144 /* creating in a given container */
6145 return validate_geometry_imsm_volume(st
, level
, layout
,
6146 raiddisks
, chunk
, size
,
6147 dev
, freesize
, verbose
);
6150 /* This device needs to be a device in an 'imsm' container */
6151 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
6154 pr_err("Cannot create this array on device %s\n",
6159 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
6161 pr_err("Cannot open %s: %s\n",
6162 dev
, strerror(errno
));
6165 /* Well, it is in use by someone, maybe an 'imsm' container. */
6166 cfd
= open_container(fd
);
6170 pr_err("Cannot use %s: It is busy\n",
6174 sra
= sysfs_read(cfd
, 0, GET_VERSION
);
6175 if (sra
&& sra
->array
.major_version
== -1 &&
6176 strcmp(sra
->text_version
, "imsm") == 0)
6180 /* This is a member of a imsm container. Load the container
6181 * and try to create a volume
6183 struct intel_super
*super
;
6185 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
, NULL
, 1) == 0) {
6187 st
->container_dev
= fd2devnum(cfd
);
6189 return validate_geometry_imsm_volume(st
, level
, layout
,
6198 pr_err("failed container membership check\n");
6204 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
6206 struct intel_super
*super
= st
->sb
;
6208 if (level
&& *level
== UnSet
)
6209 *level
= LEVEL_CONTAINER
;
6211 if (level
&& layout
&& *layout
== UnSet
)
6212 *layout
= imsm_level_to_layout(*level
);
6214 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0))
6215 *chunk
= imsm_default_chunk(super
->orom
);
6218 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
6220 static int kill_subarray_imsm(struct supertype
*st
)
6222 /* remove the subarray currently referenced by ->current_vol */
6224 struct intel_dev
**dp
;
6225 struct intel_super
*super
= st
->sb
;
6226 __u8 current_vol
= super
->current_vol
;
6227 struct imsm_super
*mpb
= super
->anchor
;
6229 if (super
->current_vol
< 0)
6231 super
->current_vol
= -1; /* invalidate subarray cursor */
6233 /* block deletions that would change the uuid of active subarrays
6235 * FIXME when immutable ids are available, but note that we'll
6236 * also need to fixup the invalidated/active subarray indexes in
6239 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6242 if (i
< current_vol
)
6244 sprintf(subarray
, "%u", i
);
6245 if (is_subarray_active(subarray
, st
->devname
)) {
6246 pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
6253 if (st
->update_tail
) {
6254 struct imsm_update_kill_array
*u
= xmalloc(sizeof(*u
));
6256 u
->type
= update_kill_array
;
6257 u
->dev_idx
= current_vol
;
6258 append_metadata_update(st
, u
, sizeof(*u
));
6263 for (dp
= &super
->devlist
; *dp
;)
6264 if ((*dp
)->index
== current_vol
) {
6267 handle_missing(super
, (*dp
)->dev
);
6268 if ((*dp
)->index
> current_vol
)
6273 /* no more raid devices, all active components are now spares,
6274 * but of course failed are still failed
6276 if (--mpb
->num_raid_devs
== 0) {
6279 for (d
= super
->disks
; d
; d
= d
->next
)
6284 super
->updates_pending
++;
6289 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
6290 char *update
, struct mddev_ident
*ident
)
6292 /* update the subarray currently referenced by ->current_vol */
6293 struct intel_super
*super
= st
->sb
;
6294 struct imsm_super
*mpb
= super
->anchor
;
6296 if (strcmp(update
, "name") == 0) {
6297 char *name
= ident
->name
;
6301 if (is_subarray_active(subarray
, st
->devname
)) {
6302 pr_err("Unable to update name of active subarray\n");
6306 if (!check_name(super
, name
, 0))
6309 vol
= strtoul(subarray
, &ep
, 10);
6310 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
6313 if (st
->update_tail
) {
6314 struct imsm_update_rename_array
*u
= xmalloc(sizeof(*u
));
6316 u
->type
= update_rename_array
;
6318 snprintf((char *) u
->name
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6319 append_metadata_update(st
, u
, sizeof(*u
));
6321 struct imsm_dev
*dev
;
6324 dev
= get_imsm_dev(super
, vol
);
6325 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6326 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6327 dev
= get_imsm_dev(super
, i
);
6328 handle_missing(super
, dev
);
6330 super
->updates_pending
++;
6337 #endif /* MDASSEMBLE */
6339 static int is_gen_migration(struct imsm_dev
*dev
)
6344 if (!dev
->vol
.migr_state
)
6347 if (migr_type(dev
) == MIGR_GEN_MIGR
)
6353 static int is_rebuilding(struct imsm_dev
*dev
)
6355 struct imsm_map
*migr_map
;
6357 if (!dev
->vol
.migr_state
)
6360 if (migr_type(dev
) != MIGR_REBUILD
)
6363 migr_map
= get_imsm_map(dev
, MAP_1
);
6365 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
6372 static int is_initializing(struct imsm_dev
*dev
)
6374 struct imsm_map
*migr_map
;
6376 if (!dev
->vol
.migr_state
)
6379 if (migr_type(dev
) != MIGR_INIT
)
6382 migr_map
= get_imsm_map(dev
, MAP_1
);
6384 if (migr_map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
6391 static void update_recovery_start(struct intel_super
*super
,
6392 struct imsm_dev
*dev
,
6393 struct mdinfo
*array
)
6395 struct mdinfo
*rebuild
= NULL
;
6399 if (!is_rebuilding(dev
))
6402 /* Find the rebuild target, but punt on the dual rebuild case */
6403 for (d
= array
->devs
; d
; d
= d
->next
)
6404 if (d
->recovery_start
== 0) {
6411 /* (?) none of the disks are marked with
6412 * IMSM_ORD_REBUILD, so assume they are missing and the
6413 * disk_ord_tbl was not correctly updated
6415 dprintf("%s: failed to locate out-of-sync disk\n", __func__
);
6419 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
6420 rebuild
->recovery_start
= units
* blocks_per_migr_unit(super
, dev
);
6424 static int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
);
6427 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
6429 /* Given a container loaded by load_super_imsm_all,
6430 * extract information about all the arrays into
6432 * If 'subarray' is given, just extract info about that array.
6434 * For each imsm_dev create an mdinfo, fill it in,
6435 * then look for matching devices in super->disks
6436 * and create appropriate device mdinfo.
6438 struct intel_super
*super
= st
->sb
;
6439 struct imsm_super
*mpb
= super
->anchor
;
6440 struct mdinfo
*rest
= NULL
;
6444 int spare_disks
= 0;
6446 /* do not assemble arrays when not all attributes are supported */
6447 if (imsm_check_attributes(mpb
->attributes
) == 0) {
6449 pr_err("Unsupported attributes in IMSM metadata."
6450 "Arrays activation is blocked.\n");
6453 /* check for bad blocks */
6454 if (imsm_bbm_log_size(super
->anchor
)) {
6455 pr_err("BBM log found in IMSM metadata."
6456 "Arrays activation is blocked.\n");
6461 /* count spare devices, not used in maps
6463 for (d
= super
->disks
; d
; d
= d
->next
)
6467 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6468 struct imsm_dev
*dev
;
6469 struct imsm_map
*map
;
6470 struct imsm_map
*map2
;
6471 struct mdinfo
*this;
6479 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
6482 dev
= get_imsm_dev(super
, i
);
6483 map
= get_imsm_map(dev
, MAP_0
);
6484 map2
= get_imsm_map(dev
, MAP_1
);
6486 /* do not publish arrays that are in the middle of an
6487 * unsupported migration
6489 if (dev
->vol
.migr_state
&&
6490 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
6491 pr_err("cannot assemble volume '%.16s':"
6492 " unsupported migration in progress\n",
6496 /* do not publish arrays that are not support by controller's
6500 this = xmalloc(sizeof(*this));
6502 super
->current_vol
= i
;
6503 getinfo_super_imsm_volume(st
, this, NULL
);
6506 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
6507 /* mdadm does not support all metadata features- set the bit in all arrays state */
6508 if (!validate_geometry_imsm_orom(super
,
6509 get_imsm_raid_level(map
), /* RAID level */
6510 imsm_level_to_layout(get_imsm_raid_level(map
)),
6511 map
->num_members
, /* raid disks */
6512 &chunk
, join_u32(dev
->size_low
, dev
->size_high
),
6514 pr_err("IMSM RAID geometry validation"
6515 " failed. Array %s activation is blocked.\n",
6517 this->array
.state
|=
6518 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
6519 (1<<MD_SB_BLOCK_VOLUME
);
6523 /* if array has bad blocks, set suitable bit in all arrays state */
6525 this->array
.state
|=
6526 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
6527 (1<<MD_SB_BLOCK_VOLUME
);
6529 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
6530 unsigned long long recovery_start
;
6531 struct mdinfo
*info_d
;
6538 idx
= get_imsm_disk_idx(dev
, slot
, MAP_0
);
6539 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
6540 for (d
= super
->disks
; d
; d
= d
->next
)
6541 if (d
->index
== idx
)
6544 recovery_start
= MaxSector
;
6547 if (d
&& is_failed(&d
->disk
))
6549 if (ord
& IMSM_ORD_REBUILD
)
6553 * if we skip some disks the array will be assmebled degraded;
6554 * reset resync start to avoid a dirty-degraded
6555 * situation when performing the intial sync
6557 * FIXME handle dirty degraded
6559 if ((skip
|| recovery_start
== 0) && !dev
->vol
.dirty
)
6560 this->resync_start
= MaxSector
;
6564 info_d
= xcalloc(1, sizeof(*info_d
));
6565 info_d
->next
= this->devs
;
6566 this->devs
= info_d
;
6568 info_d
->disk
.number
= d
->index
;
6569 info_d
->disk
.major
= d
->major
;
6570 info_d
->disk
.minor
= d
->minor
;
6571 info_d
->disk
.raid_disk
= slot
;
6572 info_d
->recovery_start
= recovery_start
;
6574 if (slot
< map2
->num_members
)
6575 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
6577 this->array
.spare_disks
++;
6579 if (slot
< map
->num_members
)
6580 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
6582 this->array
.spare_disks
++;
6584 if (info_d
->recovery_start
== MaxSector
)
6585 this->array
.working_disks
++;
6587 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
6588 info_d
->data_offset
= pba_of_lba0(map
);
6589 info_d
->component_size
= blocks_per_member(map
);
6591 /* now that the disk list is up-to-date fixup recovery_start */
6592 update_recovery_start(super
, dev
, this);
6593 this->array
.spare_disks
+= spare_disks
;
6596 /* check for reshape */
6597 if (this->reshape_active
== 1)
6598 recover_backup_imsm(st
, this);
6607 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
6608 int failed
, int look_in_map
)
6610 struct imsm_map
*map
;
6612 map
= get_imsm_map(dev
, look_in_map
);
6615 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
6616 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
6618 switch (get_imsm_raid_level(map
)) {
6620 return IMSM_T_STATE_FAILED
;
6623 if (failed
< map
->num_members
)
6624 return IMSM_T_STATE_DEGRADED
;
6626 return IMSM_T_STATE_FAILED
;
6631 * check to see if any mirrors have failed, otherwise we
6632 * are degraded. Even numbered slots are mirrored on
6636 /* gcc -Os complains that this is unused */
6637 int insync
= insync
;
6639 for (i
= 0; i
< map
->num_members
; i
++) {
6640 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_X
);
6641 int idx
= ord_to_idx(ord
);
6642 struct imsm_disk
*disk
;
6644 /* reset the potential in-sync count on even-numbered
6645 * slots. num_copies is always 2 for imsm raid10
6650 disk
= get_imsm_disk(super
, idx
);
6651 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
6654 /* no in-sync disks left in this mirror the
6658 return IMSM_T_STATE_FAILED
;
6661 return IMSM_T_STATE_DEGRADED
;
6665 return IMSM_T_STATE_DEGRADED
;
6667 return IMSM_T_STATE_FAILED
;
6673 return map
->map_state
;
6676 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
6681 struct imsm_disk
*disk
;
6682 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6683 struct imsm_map
*prev
= get_imsm_map(dev
, MAP_1
);
6684 struct imsm_map
*map_for_loop
;
6689 /* at the beginning of migration we set IMSM_ORD_REBUILD on
6690 * disks that are being rebuilt. New failures are recorded to
6691 * map[0]. So we look through all the disks we started with and
6692 * see if any failures are still present, or if any new ones
6696 if (prev
&& (map
->num_members
< prev
->num_members
))
6697 map_for_loop
= prev
;
6699 for (i
= 0; i
< map_for_loop
->num_members
; i
++) {
6701 /* when MAP_X is passed both maps failures are counted
6704 ((look_in_map
== MAP_1
) || (look_in_map
== MAP_X
)) &&
6705 (i
< prev
->num_members
)) {
6706 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
6707 idx_1
= ord_to_idx(ord
);
6709 disk
= get_imsm_disk(super
, idx_1
);
6710 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
6713 if (((look_in_map
== MAP_0
) || (look_in_map
== MAP_X
)) &&
6714 (i
< map
->num_members
)) {
6715 ord
= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
6716 idx
= ord_to_idx(ord
);
6719 disk
= get_imsm_disk(super
, idx
);
6720 if (!disk
|| is_failed(disk
) ||
6721 ord
& IMSM_ORD_REBUILD
)
6731 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
6734 struct intel_super
*super
= c
->sb
;
6735 struct imsm_super
*mpb
= super
->anchor
;
6737 if (atoi(inst
) >= mpb
->num_raid_devs
) {
6738 fprintf(stderr
, "%s: subarry index %d, out of range\n",
6739 __func__
, atoi(inst
));
6743 dprintf("imsm: open_new %s\n", inst
);
6744 a
->info
.container_member
= atoi(inst
);
6748 static int is_resyncing(struct imsm_dev
*dev
)
6750 struct imsm_map
*migr_map
;
6752 if (!dev
->vol
.migr_state
)
6755 if (migr_type(dev
) == MIGR_INIT
||
6756 migr_type(dev
) == MIGR_REPAIR
)
6759 if (migr_type(dev
) == MIGR_GEN_MIGR
)
6762 migr_map
= get_imsm_map(dev
, MAP_1
);
6764 if ((migr_map
->map_state
== IMSM_T_STATE_NORMAL
) &&
6765 (dev
->vol
.migr_type
!= MIGR_GEN_MIGR
))
6771 /* return true if we recorded new information */
6772 static int mark_failure(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
6776 struct imsm_map
*map
;
6777 char buf
[MAX_RAID_SERIAL_LEN
+3];
6778 unsigned int len
, shift
= 0;
6780 /* new failures are always set in map[0] */
6781 map
= get_imsm_map(dev
, MAP_0
);
6783 slot
= get_imsm_disk_slot(map
, idx
);
6787 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
6788 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
6791 memcpy(buf
, disk
->serial
, MAX_RAID_SERIAL_LEN
);
6792 buf
[MAX_RAID_SERIAL_LEN
] = '\000';
6794 if ((len
= strlen(buf
)) >= MAX_RAID_SERIAL_LEN
)
6795 shift
= len
- MAX_RAID_SERIAL_LEN
+ 1;
6796 strncpy((char *)disk
->serial
, &buf
[shift
], MAX_RAID_SERIAL_LEN
);
6798 disk
->status
|= FAILED_DISK
;
6799 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
6800 /* mark failures in second map if second map exists and this disk
6802 * This is valid for migration, initialization and rebuild
6804 if (dev
->vol
.migr_state
) {
6805 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
6806 int slot2
= get_imsm_disk_slot(map2
, idx
);
6808 if ((slot2
< map2
->num_members
) &&
6810 set_imsm_ord_tbl_ent(map2
, slot2
,
6811 idx
| IMSM_ORD_REBUILD
);
6813 if (map
->failed_disk_num
== 0xff)
6814 map
->failed_disk_num
= slot
;
6818 static void mark_missing(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
6820 mark_failure(dev
, disk
, idx
);
6822 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
6825 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
6826 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
6829 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
6833 if (!super
->missing
)
6836 dprintf("imsm: mark missing\n");
6837 /* end process for initialization and rebuild only
6839 if (is_gen_migration(dev
) == 0) {
6843 failed
= imsm_count_failed(super
, dev
, MAP_0
);
6844 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
6846 end_migration(dev
, super
, map_state
);
6848 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
6849 mark_missing(dev
, &dl
->disk
, dl
->index
);
6850 super
->updates_pending
++;
6853 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
,
6856 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
6857 unsigned long long array_blocks
;
6858 struct imsm_map
*map
;
6860 if (used_disks
== 0) {
6861 /* when problems occures
6862 * return current array_blocks value
6864 array_blocks
= __le32_to_cpu(dev
->size_high
);
6865 array_blocks
= array_blocks
<< 32;
6866 array_blocks
+= __le32_to_cpu(dev
->size_low
);
6868 return array_blocks
;
6871 /* set array size in metadata
6873 if (new_size
<= 0) {
6874 /* OLCE size change is caused by added disks
6876 map
= get_imsm_map(dev
, MAP_0
);
6877 array_blocks
= blocks_per_member(map
) * used_disks
;
6879 /* Online Volume Size Change
6880 * Using available free space
6882 array_blocks
= new_size
;
6885 /* round array size down to closest MB
6887 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
6888 dev
->size_low
= __cpu_to_le32((__u32
)array_blocks
);
6889 dev
->size_high
= __cpu_to_le32((__u32
)(array_blocks
>> 32));
6891 return array_blocks
;
6894 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
6896 static void imsm_progress_container_reshape(struct intel_super
*super
)
6898 /* if no device has a migr_state, but some device has a
6899 * different number of members than the previous device, start
6900 * changing the number of devices in this device to match
6903 struct imsm_super
*mpb
= super
->anchor
;
6904 int prev_disks
= -1;
6908 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6909 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
6910 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6911 struct imsm_map
*map2
;
6912 int prev_num_members
;
6914 if (dev
->vol
.migr_state
)
6917 if (prev_disks
== -1)
6918 prev_disks
= map
->num_members
;
6919 if (prev_disks
== map
->num_members
)
6922 /* OK, this array needs to enter reshape mode.
6923 * i.e it needs a migr_state
6926 copy_map_size
= sizeof_imsm_map(map
);
6927 prev_num_members
= map
->num_members
;
6928 map
->num_members
= prev_disks
;
6929 dev
->vol
.migr_state
= 1;
6930 dev
->vol
.curr_migr_unit
= 0;
6931 set_migr_type(dev
, MIGR_GEN_MIGR
);
6932 for (i
= prev_num_members
;
6933 i
< map
->num_members
; i
++)
6934 set_imsm_ord_tbl_ent(map
, i
, i
);
6935 map2
= get_imsm_map(dev
, MAP_1
);
6936 /* Copy the current map */
6937 memcpy(map2
, map
, copy_map_size
);
6938 map2
->num_members
= prev_num_members
;
6940 imsm_set_array_size(dev
, -1);
6941 super
->clean_migration_record_by_mdmon
= 1;
6942 super
->updates_pending
++;
6946 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
6947 * states are handled in imsm_set_disk() with one exception, when a
6948 * resync is stopped due to a new failure this routine will set the
6949 * 'degraded' state for the array.
6951 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
6953 int inst
= a
->info
.container_member
;
6954 struct intel_super
*super
= a
->container
->sb
;
6955 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
6956 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6957 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
6958 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
6959 __u32 blocks_per_unit
;
6961 if (dev
->vol
.migr_state
&&
6962 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
6963 /* array state change is blocked due to reshape action
6965 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
6966 * - finish the reshape (if last_checkpoint is big and action != reshape)
6967 * - update curr_migr_unit
6969 if (a
->curr_action
== reshape
) {
6970 /* still reshaping, maybe update curr_migr_unit */
6971 goto mark_checkpoint
;
6973 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
6974 /* for some reason we aborted the reshape.
6976 * disable automatic metadata rollback
6977 * user action is required to recover process
6980 struct imsm_map
*map2
=
6981 get_imsm_map(dev
, MAP_1
);
6982 dev
->vol
.migr_state
= 0;
6983 set_migr_type(dev
, 0);
6984 dev
->vol
.curr_migr_unit
= 0;
6986 sizeof_imsm_map(map2
));
6987 super
->updates_pending
++;
6990 if (a
->last_checkpoint
>= a
->info
.component_size
) {
6991 unsigned long long array_blocks
;
6995 used_disks
= imsm_num_data_members(dev
, MAP_0
);
6996 if (used_disks
> 0) {
6998 blocks_per_member(map
) *
7000 /* round array size down to closest MB
7002 array_blocks
= (array_blocks
7003 >> SECT_PER_MB_SHIFT
)
7004 << SECT_PER_MB_SHIFT
;
7005 a
->info
.custom_array_size
= array_blocks
;
7006 /* encourage manager to update array
7010 a
->check_reshape
= 1;
7012 /* finalize online capacity expansion/reshape */
7013 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
7015 mdi
->disk
.raid_disk
,
7018 imsm_progress_container_reshape(super
);
7023 /* before we activate this array handle any missing disks */
7024 if (consistent
== 2)
7025 handle_missing(super
, dev
);
7027 if (consistent
== 2 &&
7028 (!is_resync_complete(&a
->info
) ||
7029 map_state
!= IMSM_T_STATE_NORMAL
||
7030 dev
->vol
.migr_state
))
7033 if (is_resync_complete(&a
->info
)) {
7034 /* complete intialization / resync,
7035 * recovery and interrupted recovery is completed in
7038 if (is_resyncing(dev
)) {
7039 dprintf("imsm: mark resync done\n");
7040 end_migration(dev
, super
, map_state
);
7041 super
->updates_pending
++;
7042 a
->last_checkpoint
= 0;
7044 } else if ((!is_resyncing(dev
) && !failed
) &&
7045 (imsm_reshape_blocks_arrays_changes(super
) == 0)) {
7046 /* mark the start of the init process if nothing is failed */
7047 dprintf("imsm: mark resync start\n");
7048 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
7049 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
7051 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
7052 super
->updates_pending
++;
7056 /* skip checkpointing for general migration,
7057 * it is controlled in mdadm
7059 if (is_gen_migration(dev
))
7060 goto skip_mark_checkpoint
;
7062 /* check if we can update curr_migr_unit from resync_start, recovery_start */
7063 blocks_per_unit
= blocks_per_migr_unit(super
, dev
);
7064 if (blocks_per_unit
) {
7068 units
= a
->last_checkpoint
/ blocks_per_unit
;
7071 /* check that we did not overflow 32-bits, and that
7072 * curr_migr_unit needs updating
7074 if (units32
== units
&&
7076 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
7077 dprintf("imsm: mark checkpoint (%u)\n", units32
);
7078 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
7079 super
->updates_pending
++;
7083 skip_mark_checkpoint
:
7084 /* mark dirty / clean */
7085 if (dev
->vol
.dirty
!= !consistent
) {
7086 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
7091 super
->updates_pending
++;
7097 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
7099 int inst
= a
->info
.container_member
;
7100 struct intel_super
*super
= a
->container
->sb
;
7101 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7102 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7103 struct imsm_disk
*disk
;
7105 int recovery_not_finished
= 0;
7110 if (n
> map
->num_members
)
7111 fprintf(stderr
, "imsm: set_disk %d out of range 0..%d\n",
7112 n
, map
->num_members
- 1);
7117 dprintf("imsm: set_disk %d:%x\n", n
, state
);
7119 ord
= get_imsm_ord_tbl_ent(dev
, n
, MAP_0
);
7120 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
7122 /* check for new failures */
7123 if (state
& DS_FAULTY
) {
7124 if (mark_failure(dev
, disk
, ord_to_idx(ord
)))
7125 super
->updates_pending
++;
7128 /* check if in_sync */
7129 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
7130 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
7132 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
7133 super
->updates_pending
++;
7136 failed
= imsm_count_failed(super
, dev
, MAP_0
);
7137 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7139 /* check if recovery complete, newly degraded, or failed */
7140 dprintf("imsm: Detected transition to state ");
7141 switch (map_state
) {
7142 case IMSM_T_STATE_NORMAL
: /* transition to normal state */
7143 dprintf("normal: ");
7144 if (is_rebuilding(dev
)) {
7145 dprintf("while rebuilding");
7146 /* check if recovery is really finished */
7147 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
7148 if (mdi
->recovery_start
!= MaxSector
) {
7149 recovery_not_finished
= 1;
7152 if (recovery_not_finished
) {
7153 dprintf("\nimsm: Rebuild has not finished yet, "
7154 "state not changed");
7155 if (a
->last_checkpoint
< mdi
->recovery_start
) {
7156 a
->last_checkpoint
= mdi
->recovery_start
;
7157 super
->updates_pending
++;
7161 end_migration(dev
, super
, map_state
);
7162 map
= get_imsm_map(dev
, MAP_0
);
7163 map
->failed_disk_num
= ~0;
7164 super
->updates_pending
++;
7165 a
->last_checkpoint
= 0;
7168 if (is_gen_migration(dev
)) {
7169 dprintf("while general migration");
7170 if (a
->last_checkpoint
>= a
->info
.component_size
)
7171 end_migration(dev
, super
, map_state
);
7173 map
->map_state
= map_state
;
7174 map
= get_imsm_map(dev
, MAP_0
);
7175 map
->failed_disk_num
= ~0;
7176 super
->updates_pending
++;
7180 case IMSM_T_STATE_DEGRADED
: /* transition to degraded state */
7181 dprintf("degraded: ");
7182 if ((map
->map_state
!= map_state
) &&
7183 !dev
->vol
.migr_state
) {
7184 dprintf("mark degraded");
7185 map
->map_state
= map_state
;
7186 super
->updates_pending
++;
7187 a
->last_checkpoint
= 0;
7190 if (is_rebuilding(dev
)) {
7191 dprintf("while rebuilding.");
7192 if (map
->map_state
!= map_state
) {
7193 dprintf(" Map state change");
7194 end_migration(dev
, super
, map_state
);
7195 super
->updates_pending
++;
7199 if (is_gen_migration(dev
)) {
7200 dprintf("while general migration");
7201 if (a
->last_checkpoint
>= a
->info
.component_size
)
7202 end_migration(dev
, super
, map_state
);
7204 map
->map_state
= map_state
;
7205 manage_second_map(super
, dev
);
7207 super
->updates_pending
++;
7210 if (is_initializing(dev
)) {
7211 dprintf("while initialization.");
7212 map
->map_state
= map_state
;
7213 super
->updates_pending
++;
7217 case IMSM_T_STATE_FAILED
: /* transition to failed state */
7218 dprintf("failed: ");
7219 if (is_gen_migration(dev
)) {
7220 dprintf("while general migration");
7221 map
->map_state
= map_state
;
7222 super
->updates_pending
++;
7225 if (map
->map_state
!= map_state
) {
7226 dprintf("mark failed");
7227 end_migration(dev
, super
, map_state
);
7228 super
->updates_pending
++;
7229 a
->last_checkpoint
= 0;
7234 dprintf("state %i\n", map_state
);
7240 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
7243 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
7244 unsigned long long dsize
;
7245 unsigned long long sectors
;
7247 get_dev_size(fd
, NULL
, &dsize
);
7249 if (mpb_size
> 512) {
7250 /* -1 to account for anchor */
7251 sectors
= mpb_sectors(mpb
) - 1;
7253 /* write the extended mpb to the sectors preceeding the anchor */
7254 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0)
7257 if ((unsigned long long)write(fd
, buf
+ 512, 512 * sectors
)
7262 /* first block is stored on second to last sector of the disk */
7263 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0)
7266 if (write(fd
, buf
, 512) != 512)
7272 static void imsm_sync_metadata(struct supertype
*container
)
7274 struct intel_super
*super
= container
->sb
;
7276 dprintf("sync metadata: %d\n", super
->updates_pending
);
7277 if (!super
->updates_pending
)
7280 write_super_imsm(container
, 0);
7282 super
->updates_pending
= 0;
7285 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
7287 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7288 int i
= get_imsm_disk_idx(dev
, idx
, MAP_X
);
7291 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7295 if (dl
&& is_failed(&dl
->disk
))
7299 dprintf("%s: found %x:%x\n", __func__
, dl
->major
, dl
->minor
);
7304 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
7305 struct active_array
*a
, int activate_new
,
7306 struct mdinfo
*additional_test_list
)
7308 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7309 int idx
= get_imsm_disk_idx(dev
, slot
, MAP_X
);
7310 struct imsm_super
*mpb
= super
->anchor
;
7311 struct imsm_map
*map
;
7312 unsigned long long pos
;
7317 __u32 array_start
= 0;
7318 __u32 array_end
= 0;
7320 struct mdinfo
*test_list
;
7322 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7323 /* If in this array, skip */
7324 for (d
= a
->info
.devs
; d
; d
= d
->next
)
7325 if (d
->state_fd
>= 0 &&
7326 d
->disk
.major
== dl
->major
&&
7327 d
->disk
.minor
== dl
->minor
) {
7328 dprintf("%x:%x already in array\n",
7329 dl
->major
, dl
->minor
);
7334 test_list
= additional_test_list
;
7336 if (test_list
->disk
.major
== dl
->major
&&
7337 test_list
->disk
.minor
== dl
->minor
) {
7338 dprintf("%x:%x already in additional test list\n",
7339 dl
->major
, dl
->minor
);
7342 test_list
= test_list
->next
;
7347 /* skip in use or failed drives */
7348 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
7350 dprintf("%x:%x status (failed: %d index: %d)\n",
7351 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
7355 /* skip pure spares when we are looking for partially
7356 * assimilated drives
7358 if (dl
->index
== -1 && !activate_new
)
7361 /* Does this unused device have the requisite free space?
7362 * It needs to be able to cover all member volumes
7364 ex
= get_extents(super
, dl
);
7366 dprintf("cannot get extents\n");
7369 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7370 dev
= get_imsm_dev(super
, i
);
7371 map
= get_imsm_map(dev
, MAP_0
);
7373 /* check if this disk is already a member of
7376 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
7382 array_start
= pba_of_lba0(map
);
7383 array_end
= array_start
+
7384 blocks_per_member(map
) - 1;
7387 /* check that we can start at pba_of_lba0 with
7388 * blocks_per_member of space
7390 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
7394 pos
= ex
[j
].start
+ ex
[j
].size
;
7396 } while (ex
[j
-1].size
);
7403 if (i
< mpb
->num_raid_devs
) {
7404 dprintf("%x:%x does not have %u to %u available\n",
7405 dl
->major
, dl
->minor
, array_start
, array_end
);
7416 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
7418 struct imsm_dev
*dev2
;
7419 struct imsm_map
*map
;
7425 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
7427 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
, MAP_0
);
7428 if (state
== IMSM_T_STATE_FAILED
) {
7429 map
= get_imsm_map(dev2
, MAP_0
);
7432 for (slot
= 0; slot
< map
->num_members
; slot
++) {
7434 * Check if failed disks are deleted from intel
7435 * disk list or are marked to be deleted
7437 idx
= get_imsm_disk_idx(dev2
, slot
, MAP_X
);
7438 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
7440 * Do not rebuild the array if failed disks
7441 * from failed sub-array are not removed from
7445 is_failed(&idisk
->disk
) &&
7446 (idisk
->action
!= DISK_REMOVE
))
7454 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
7455 struct metadata_update
**updates
)
7458 * Find a device with unused free space and use it to replace a
7459 * failed/vacant region in an array. We replace failed regions one a
7460 * array at a time. The result is that a new spare disk will be added
7461 * to the first failed array and after the monitor has finished
7462 * propagating failures the remainder will be consumed.
7464 * FIXME add a capability for mdmon to request spares from another
7468 struct intel_super
*super
= a
->container
->sb
;
7469 int inst
= a
->info
.container_member
;
7470 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7471 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7472 int failed
= a
->info
.array
.raid_disks
;
7473 struct mdinfo
*rv
= NULL
;
7476 struct metadata_update
*mu
;
7478 struct imsm_update_activate_spare
*u
;
7483 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
7484 if ((d
->curr_state
& DS_FAULTY
) &&
7486 /* wait for Removal to happen */
7488 if (d
->state_fd
>= 0)
7492 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
7493 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
7495 if (imsm_reshape_blocks_arrays_changes(super
))
7498 /* Cannot activate another spare if rebuild is in progress already
7500 if (is_rebuilding(dev
)) {
7501 dprintf("imsm: No spare activation allowed. "
7502 "Rebuild in progress already.\n");
7506 if (a
->info
.array
.level
== 4)
7507 /* No repair for takeovered array
7508 * imsm doesn't support raid4
7512 if (imsm_check_degraded(super
, dev
, failed
, MAP_0
) !=
7513 IMSM_T_STATE_DEGRADED
)
7517 * If there are any failed disks check state of the other volume.
7518 * Block rebuild if the another one is failed until failed disks
7519 * are removed from container.
7522 dprintf("found failed disks in %.*s, check if there another"
7523 "failed sub-array.\n",
7524 MAX_RAID_SERIAL_LEN
, dev
->volume
);
7525 /* check if states of the other volumes allow for rebuild */
7526 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
7528 allowed
= imsm_rebuild_allowed(a
->container
,
7536 /* For each slot, if it is not working, find a spare */
7537 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
7538 for (d
= a
->info
.devs
; d
; d
= d
->next
)
7539 if (d
->disk
.raid_disk
== i
)
7541 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
7542 if (d
&& (d
->state_fd
>= 0))
7546 * OK, this device needs recovery. Try to re-add the
7547 * previous occupant of this slot, if this fails see if
7548 * we can continue the assimilation of a spare that was
7549 * partially assimilated, finally try to activate a new
7552 dl
= imsm_readd(super
, i
, a
);
7554 dl
= imsm_add_spare(super
, i
, a
, 0, rv
);
7556 dl
= imsm_add_spare(super
, i
, a
, 1, rv
);
7560 /* found a usable disk with enough space */
7561 di
= xcalloc(1, sizeof(*di
));
7563 /* dl->index will be -1 in the case we are activating a
7564 * pristine spare. imsm_process_update() will create a
7565 * new index in this case. Once a disk is found to be
7566 * failed in all member arrays it is kicked from the
7569 di
->disk
.number
= dl
->index
;
7571 /* (ab)use di->devs to store a pointer to the device
7574 di
->devs
= (struct mdinfo
*) dl
;
7576 di
->disk
.raid_disk
= i
;
7577 di
->disk
.major
= dl
->major
;
7578 di
->disk
.minor
= dl
->minor
;
7580 di
->recovery_start
= 0;
7581 di
->data_offset
= pba_of_lba0(map
);
7582 di
->component_size
= a
->info
.component_size
;
7583 di
->container_member
= inst
;
7584 super
->random
= random32();
7588 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
7589 i
, di
->data_offset
);
7593 /* No spares found */
7595 /* Now 'rv' has a list of devices to return.
7596 * Create a metadata_update record to update the
7597 * disk_ord_tbl for the array
7599 mu
= xmalloc(sizeof(*mu
));
7600 mu
->buf
= xcalloc(num_spares
,
7601 sizeof(struct imsm_update_activate_spare
));
7603 mu
->space_list
= NULL
;
7604 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
7605 mu
->next
= *updates
;
7606 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
7608 for (di
= rv
; di
; di
= di
->next
) {
7609 u
->type
= update_activate_spare
;
7610 u
->dl
= (struct dl
*) di
->devs
;
7612 u
->slot
= di
->disk
.raid_disk
;
7623 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
7625 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
7626 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7627 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, MAP_0
);
7628 struct disk_info
*inf
= get_disk_info(u
);
7629 struct imsm_disk
*disk
;
7633 for (i
= 0; i
< map
->num_members
; i
++) {
7634 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, MAP_X
));
7635 for (j
= 0; j
< new_map
->num_members
; j
++)
7636 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
7644 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
7646 struct dl
*dl
= NULL
;
7647 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7648 if ((dl
->major
== major
) && (dl
->minor
== minor
))
7653 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
7655 struct dl
*prev
= NULL
;
7659 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7660 if ((dl
->major
== major
) && (dl
->minor
== minor
)) {
7663 prev
->next
= dl
->next
;
7665 super
->disks
= dl
->next
;
7667 __free_imsm_disk(dl
);
7668 dprintf("%s: removed %x:%x\n",
7669 __func__
, major
, minor
);
7677 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
7679 static int add_remove_disk_update(struct intel_super
*super
)
7681 int check_degraded
= 0;
7682 struct dl
*disk
= NULL
;
7683 /* add/remove some spares to/from the metadata/contrainer */
7684 while (super
->disk_mgmt_list
) {
7685 struct dl
*disk_cfg
;
7687 disk_cfg
= super
->disk_mgmt_list
;
7688 super
->disk_mgmt_list
= disk_cfg
->next
;
7689 disk_cfg
->next
= NULL
;
7691 if (disk_cfg
->action
== DISK_ADD
) {
7692 disk_cfg
->next
= super
->disks
;
7693 super
->disks
= disk_cfg
;
7695 dprintf("%s: added %x:%x\n",
7696 __func__
, disk_cfg
->major
,
7698 } else if (disk_cfg
->action
== DISK_REMOVE
) {
7699 dprintf("Disk remove action processed: %x.%x\n",
7700 disk_cfg
->major
, disk_cfg
->minor
);
7701 disk
= get_disk_super(super
,
7705 /* store action status */
7706 disk
->action
= DISK_REMOVE
;
7707 /* remove spare disks only */
7708 if (disk
->index
== -1) {
7709 remove_disk_super(super
,
7714 /* release allocate disk structure */
7715 __free_imsm_disk(disk_cfg
);
7718 return check_degraded
;
7722 static int apply_reshape_migration_update(struct imsm_update_reshape_migration
*u
,
7723 struct intel_super
*super
,
7726 struct intel_dev
*id
;
7727 void **tofree
= NULL
;
7730 dprintf("apply_reshape_migration_update()\n");
7731 if ((u
->subdev
< 0) ||
7733 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
7736 if ((space_list
== NULL
) || (*space_list
== NULL
)) {
7737 dprintf("imsm: Error: Memory is not allocated\n");
7741 for (id
= super
->devlist
; id
; id
= id
->next
) {
7742 if (id
->index
== (unsigned)u
->subdev
) {
7743 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
7744 struct imsm_map
*map
;
7745 struct imsm_dev
*new_dev
=
7746 (struct imsm_dev
*)*space_list
;
7747 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
7749 struct dl
*new_disk
;
7751 if (new_dev
== NULL
)
7753 *space_list
= **space_list
;
7754 memcpy(new_dev
, dev
, sizeof_imsm_dev(dev
, 0));
7755 map
= get_imsm_map(new_dev
, MAP_0
);
7757 dprintf("imsm: Error: migration in progress");
7761 to_state
= map
->map_state
;
7762 if ((u
->new_level
== 5) && (map
->raid_level
== 0)) {
7764 /* this should not happen */
7765 if (u
->new_disks
[0] < 0) {
7766 map
->failed_disk_num
=
7767 map
->num_members
- 1;
7768 to_state
= IMSM_T_STATE_DEGRADED
;
7770 to_state
= IMSM_T_STATE_NORMAL
;
7772 migrate(new_dev
, super
, to_state
, MIGR_GEN_MIGR
);
7773 if (u
->new_level
> -1)
7774 map
->raid_level
= u
->new_level
;
7775 migr_map
= get_imsm_map(new_dev
, MAP_1
);
7776 if ((u
->new_level
== 5) &&
7777 (migr_map
->raid_level
== 0)) {
7778 int ord
= map
->num_members
- 1;
7779 migr_map
->num_members
--;
7780 if (u
->new_disks
[0] < 0)
7781 ord
|= IMSM_ORD_REBUILD
;
7782 set_imsm_ord_tbl_ent(map
,
7783 map
->num_members
- 1,
7787 tofree
= (void **)dev
;
7789 /* update chunk size
7791 if (u
->new_chunksize
> 0)
7792 map
->blocks_per_strip
=
7793 __cpu_to_le16(u
->new_chunksize
* 2);
7797 if ((u
->new_level
!= 5) ||
7798 (migr_map
->raid_level
!= 0) ||
7799 (migr_map
->raid_level
== map
->raid_level
))
7802 if (u
->new_disks
[0] >= 0) {
7805 new_disk
= get_disk_super(super
,
7806 major(u
->new_disks
[0]),
7807 minor(u
->new_disks
[0]));
7808 dprintf("imsm: new disk for reshape is: %i:%i "
7809 "(%p, index = %i)\n",
7810 major(u
->new_disks
[0]),
7811 minor(u
->new_disks
[0]),
7812 new_disk
, new_disk
->index
);
7813 if (new_disk
== NULL
)
7814 goto error_disk_add
;
7816 new_disk
->index
= map
->num_members
- 1;
7817 /* slot to fill in autolayout
7819 new_disk
->raiddisk
= new_disk
->index
;
7820 new_disk
->disk
.status
|= CONFIGURED_DISK
;
7821 new_disk
->disk
.status
&= ~SPARE_DISK
;
7823 goto error_disk_add
;
7826 *tofree
= *space_list
;
7827 /* calculate new size
7829 imsm_set_array_size(new_dev
, -1);
7836 *space_list
= tofree
;
7840 dprintf("Error: imsm: Cannot find disk.\n");
7844 static int apply_size_change_update(struct imsm_update_size_change
*u
,
7845 struct intel_super
*super
)
7847 struct intel_dev
*id
;
7850 dprintf("apply_size_change_update()\n");
7851 if ((u
->subdev
< 0) ||
7853 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
7857 for (id
= super
->devlist
; id
; id
= id
->next
) {
7858 if (id
->index
== (unsigned)u
->subdev
) {
7859 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
7860 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7861 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
7862 unsigned long long blocks_per_member
;
7864 /* calculate new size
7866 blocks_per_member
= u
->new_size
/ used_disks
;
7867 dprintf("imsm: apply_size_change_update(size: %llu, "
7868 "blocks per member: %llu)\n",
7869 u
->new_size
, blocks_per_member
);
7870 set_blocks_per_member(map
, blocks_per_member
);
7871 imsm_set_array_size(dev
, u
->new_size
);
7882 static int apply_update_activate_spare(struct imsm_update_activate_spare
*u
,
7883 struct intel_super
*super
,
7884 struct active_array
*active_array
)
7886 struct imsm_super
*mpb
= super
->anchor
;
7887 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
7888 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7889 struct imsm_map
*migr_map
;
7890 struct active_array
*a
;
7891 struct imsm_disk
*disk
;
7898 int second_map_created
= 0;
7900 for (; u
; u
= u
->next
) {
7901 victim
= get_imsm_disk_idx(dev
, u
->slot
, MAP_X
);
7906 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7911 fprintf(stderr
, "error: imsm_activate_spare passed "
7912 "an unknown disk (index: %d)\n",
7917 /* count failures (excluding rebuilds and the victim)
7918 * to determine map[0] state
7921 for (i
= 0; i
< map
->num_members
; i
++) {
7924 disk
= get_imsm_disk(super
,
7925 get_imsm_disk_idx(dev
, i
, MAP_X
));
7926 if (!disk
|| is_failed(disk
))
7930 /* adding a pristine spare, assign a new index */
7931 if (dl
->index
< 0) {
7932 dl
->index
= super
->anchor
->num_disks
;
7933 super
->anchor
->num_disks
++;
7936 disk
->status
|= CONFIGURED_DISK
;
7937 disk
->status
&= ~SPARE_DISK
;
7940 to_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7941 if (!second_map_created
) {
7942 second_map_created
= 1;
7943 map
->map_state
= IMSM_T_STATE_DEGRADED
;
7944 migrate(dev
, super
, to_state
, MIGR_REBUILD
);
7946 map
->map_state
= to_state
;
7947 migr_map
= get_imsm_map(dev
, MAP_1
);
7948 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
7949 set_imsm_ord_tbl_ent(migr_map
, u
->slot
,
7950 dl
->index
| IMSM_ORD_REBUILD
);
7952 /* update the family_num to mark a new container
7953 * generation, being careful to record the existing
7954 * family_num in orig_family_num to clean up after
7955 * earlier mdadm versions that neglected to set it.
7957 if (mpb
->orig_family_num
== 0)
7958 mpb
->orig_family_num
= mpb
->family_num
;
7959 mpb
->family_num
+= super
->random
;
7961 /* count arrays using the victim in the metadata */
7963 for (a
= active_array
; a
; a
= a
->next
) {
7964 dev
= get_imsm_dev(super
, a
->info
.container_member
);
7965 map
= get_imsm_map(dev
, MAP_0
);
7967 if (get_imsm_disk_slot(map
, victim
) >= 0)
7971 /* delete the victim if it is no longer being
7977 /* We know that 'manager' isn't touching anything,
7978 * so it is safe to delete
7980 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
7981 if ((*dlp
)->index
== victim
)
7984 /* victim may be on the missing list */
7986 for (dlp
= &super
->missing
; *dlp
;
7987 dlp
= &(*dlp
)->next
)
7988 if ((*dlp
)->index
== victim
)
7990 imsm_delete(super
, dlp
, victim
);
7997 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
7998 struct intel_super
*super
,
8001 struct dl
*new_disk
;
8002 struct intel_dev
*id
;
8004 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
8005 int disk_count
= u
->old_raid_disks
;
8006 void **tofree
= NULL
;
8007 int devices_to_reshape
= 1;
8008 struct imsm_super
*mpb
= super
->anchor
;
8010 unsigned int dev_id
;
8012 dprintf("imsm: apply_reshape_container_disks_update()\n");
8014 /* enable spares to use in array */
8015 for (i
= 0; i
< delta_disks
; i
++) {
8016 new_disk
= get_disk_super(super
,
8017 major(u
->new_disks
[i
]),
8018 minor(u
->new_disks
[i
]));
8019 dprintf("imsm: new disk for reshape is: %i:%i "
8020 "(%p, index = %i)\n",
8021 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
8022 new_disk
, new_disk
->index
);
8023 if ((new_disk
== NULL
) ||
8024 ((new_disk
->index
>= 0) &&
8025 (new_disk
->index
< u
->old_raid_disks
)))
8026 goto update_reshape_exit
;
8027 new_disk
->index
= disk_count
++;
8028 /* slot to fill in autolayout
8030 new_disk
->raiddisk
= new_disk
->index
;
8031 new_disk
->disk
.status
|=
8033 new_disk
->disk
.status
&= ~SPARE_DISK
;
8036 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
8037 mpb
->num_raid_devs
);
8038 /* manage changes in volume
8040 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
8041 void **sp
= *space_list
;
8042 struct imsm_dev
*newdev
;
8043 struct imsm_map
*newmap
, *oldmap
;
8045 for (id
= super
->devlist
; id
; id
= id
->next
) {
8046 if (id
->index
== dev_id
)
8055 /* Copy the dev, but not (all of) the map */
8056 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
8057 oldmap
= get_imsm_map(id
->dev
, MAP_0
);
8058 newmap
= get_imsm_map(newdev
, MAP_0
);
8059 /* Copy the current map */
8060 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8061 /* update one device only
8063 if (devices_to_reshape
) {
8064 dprintf("imsm: modifying subdev: %i\n",
8066 devices_to_reshape
--;
8067 newdev
->vol
.migr_state
= 1;
8068 newdev
->vol
.curr_migr_unit
= 0;
8069 set_migr_type(newdev
, MIGR_GEN_MIGR
);
8070 newmap
->num_members
= u
->new_raid_disks
;
8071 for (i
= 0; i
< delta_disks
; i
++) {
8072 set_imsm_ord_tbl_ent(newmap
,
8073 u
->old_raid_disks
+ i
,
8074 u
->old_raid_disks
+ i
);
8076 /* New map is correct, now need to save old map
8078 newmap
= get_imsm_map(newdev
, MAP_1
);
8079 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8081 imsm_set_array_size(newdev
, -1);
8084 sp
= (void **)id
->dev
;
8089 /* Clear migration record */
8090 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
8093 *space_list
= tofree
;
8096 update_reshape_exit
:
8101 static int apply_takeover_update(struct imsm_update_takeover
*u
,
8102 struct intel_super
*super
,
8105 struct imsm_dev
*dev
= NULL
;
8106 struct intel_dev
*dv
;
8107 struct imsm_dev
*dev_new
;
8108 struct imsm_map
*map
;
8112 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
8113 if (dv
->index
== (unsigned int)u
->subarray
) {
8121 map
= get_imsm_map(dev
, MAP_0
);
8123 if (u
->direction
== R10_TO_R0
) {
8124 /* Number of failed disks must be half of initial disk number */
8125 if (imsm_count_failed(super
, dev
, MAP_0
) !=
8126 (map
->num_members
/ 2))
8129 /* iterate through devices to mark removed disks as spare */
8130 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8131 if (dm
->disk
.status
& FAILED_DISK
) {
8132 int idx
= dm
->index
;
8133 /* update indexes on the disk list */
8134 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
8135 the index values will end up being correct.... NB */
8136 for (du
= super
->disks
; du
; du
= du
->next
)
8137 if (du
->index
> idx
)
8139 /* mark as spare disk */
8144 map
->num_members
= map
->num_members
/ 2;
8145 map
->map_state
= IMSM_T_STATE_NORMAL
;
8146 map
->num_domains
= 1;
8147 map
->raid_level
= 0;
8148 map
->failed_disk_num
= -1;
8151 if (u
->direction
== R0_TO_R10
) {
8153 /* update slots in current disk list */
8154 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8158 /* create new *missing* disks */
8159 for (i
= 0; i
< map
->num_members
; i
++) {
8160 space
= *space_list
;
8163 *space_list
= *space
;
8165 memcpy(du
, super
->disks
, sizeof(*du
));
8169 du
->index
= (i
* 2) + 1;
8170 sprintf((char *)du
->disk
.serial
,
8171 " MISSING_%d", du
->index
);
8172 sprintf((char *)du
->serial
,
8173 "MISSING_%d", du
->index
);
8174 du
->next
= super
->missing
;
8175 super
->missing
= du
;
8177 /* create new dev and map */
8178 space
= *space_list
;
8181 *space_list
= *space
;
8182 dev_new
= (void *)space
;
8183 memcpy(dev_new
, dev
, sizeof(*dev
));
8184 /* update new map */
8185 map
= get_imsm_map(dev_new
, MAP_0
);
8186 map
->num_members
= map
->num_members
* 2;
8187 map
->map_state
= IMSM_T_STATE_DEGRADED
;
8188 map
->num_domains
= 2;
8189 map
->raid_level
= 1;
8190 /* replace dev<->dev_new */
8193 /* update disk order table */
8194 for (du
= super
->disks
; du
; du
= du
->next
)
8196 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8197 for (du
= super
->missing
; du
; du
= du
->next
)
8198 if (du
->index
>= 0) {
8199 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8200 mark_missing(dv
->dev
, &du
->disk
, du
->index
);
8206 static void imsm_process_update(struct supertype
*st
,
8207 struct metadata_update
*update
)
8210 * crack open the metadata_update envelope to find the update record
8211 * update can be one of:
8212 * update_reshape_container_disks - all the arrays in the container
8213 * are being reshaped to have more devices. We need to mark
8214 * the arrays for general migration and convert selected spares
8215 * into active devices.
8216 * update_activate_spare - a spare device has replaced a failed
8217 * device in an array, update the disk_ord_tbl. If this disk is
8218 * present in all member arrays then also clear the SPARE_DISK
8220 * update_create_array
8222 * update_rename_array
8223 * update_add_remove_disk
8225 struct intel_super
*super
= st
->sb
;
8226 struct imsm_super
*mpb
;
8227 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
8229 /* update requires a larger buf but the allocation failed */
8230 if (super
->next_len
&& !super
->next_buf
) {
8231 super
->next_len
= 0;
8235 if (super
->next_buf
) {
8236 memcpy(super
->next_buf
, super
->buf
, super
->len
);
8238 super
->len
= super
->next_len
;
8239 super
->buf
= super
->next_buf
;
8241 super
->next_len
= 0;
8242 super
->next_buf
= NULL
;
8245 mpb
= super
->anchor
;
8248 case update_general_migration_checkpoint
: {
8249 struct intel_dev
*id
;
8250 struct imsm_update_general_migration_checkpoint
*u
=
8251 (void *)update
->buf
;
8253 dprintf("imsm: process_update() "
8254 "for update_general_migration_checkpoint called\n");
8256 /* find device under general migration */
8257 for (id
= super
->devlist
; id
; id
= id
->next
) {
8258 if (is_gen_migration(id
->dev
)) {
8259 id
->dev
->vol
.curr_migr_unit
=
8260 __cpu_to_le32(u
->curr_migr_unit
);
8261 super
->updates_pending
++;
8266 case update_takeover
: {
8267 struct imsm_update_takeover
*u
= (void *)update
->buf
;
8268 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
8269 imsm_update_version_info(super
);
8270 super
->updates_pending
++;
8275 case update_reshape_container_disks
: {
8276 struct imsm_update_reshape
*u
= (void *)update
->buf
;
8277 if (apply_reshape_container_disks_update(
8278 u
, super
, &update
->space_list
))
8279 super
->updates_pending
++;
8282 case update_reshape_migration
: {
8283 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
8284 if (apply_reshape_migration_update(
8285 u
, super
, &update
->space_list
))
8286 super
->updates_pending
++;
8289 case update_size_change
: {
8290 struct imsm_update_size_change
*u
= (void *)update
->buf
;
8291 if (apply_size_change_update(u
, super
))
8292 super
->updates_pending
++;
8295 case update_activate_spare
: {
8296 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
8297 if (apply_update_activate_spare(u
, super
, st
->arrays
))
8298 super
->updates_pending
++;
8301 case update_create_array
: {
8302 /* someone wants to create a new array, we need to be aware of
8303 * a few races/collisions:
8304 * 1/ 'Create' called by two separate instances of mdadm
8305 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
8306 * devices that have since been assimilated via
8308 * In the event this update can not be carried out mdadm will
8309 * (FIX ME) notice that its update did not take hold.
8311 struct imsm_update_create_array
*u
= (void *) update
->buf
;
8312 struct intel_dev
*dv
;
8313 struct imsm_dev
*dev
;
8314 struct imsm_map
*map
, *new_map
;
8315 unsigned long long start
, end
;
8316 unsigned long long new_start
, new_end
;
8318 struct disk_info
*inf
;
8321 /* handle racing creates: first come first serve */
8322 if (u
->dev_idx
< mpb
->num_raid_devs
) {
8323 dprintf("%s: subarray %d already defined\n",
8324 __func__
, u
->dev_idx
);
8328 /* check update is next in sequence */
8329 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
8330 dprintf("%s: can not create array %d expected index %d\n",
8331 __func__
, u
->dev_idx
, mpb
->num_raid_devs
);
8335 new_map
= get_imsm_map(&u
->dev
, MAP_0
);
8336 new_start
= pba_of_lba0(new_map
);
8337 new_end
= new_start
+ blocks_per_member(new_map
);
8338 inf
= get_disk_info(u
);
8340 /* handle activate_spare versus create race:
8341 * check to make sure that overlapping arrays do not include
8344 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8345 dev
= get_imsm_dev(super
, i
);
8346 map
= get_imsm_map(dev
, MAP_0
);
8347 start
= pba_of_lba0(map
);
8348 end
= start
+ blocks_per_member(map
);
8349 if ((new_start
>= start
&& new_start
<= end
) ||
8350 (start
>= new_start
&& start
<= new_end
))
8355 if (disks_overlap(super
, i
, u
)) {
8356 dprintf("%s: arrays overlap\n", __func__
);
8361 /* check that prepare update was successful */
8362 if (!update
->space
) {
8363 dprintf("%s: prepare update failed\n", __func__
);
8367 /* check that all disks are still active before committing
8368 * changes. FIXME: could we instead handle this by creating a
8369 * degraded array? That's probably not what the user expects,
8370 * so better to drop this update on the floor.
8372 for (i
= 0; i
< new_map
->num_members
; i
++) {
8373 dl
= serial_to_dl(inf
[i
].serial
, super
);
8375 dprintf("%s: disk disappeared\n", __func__
);
8380 super
->updates_pending
++;
8382 /* convert spares to members and fixup ord_tbl */
8383 for (i
= 0; i
< new_map
->num_members
; i
++) {
8384 dl
= serial_to_dl(inf
[i
].serial
, super
);
8385 if (dl
->index
== -1) {
8386 dl
->index
= mpb
->num_disks
;
8388 dl
->disk
.status
|= CONFIGURED_DISK
;
8389 dl
->disk
.status
&= ~SPARE_DISK
;
8391 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
8396 update
->space
= NULL
;
8397 imsm_copy_dev(dev
, &u
->dev
);
8398 dv
->index
= u
->dev_idx
;
8399 dv
->next
= super
->devlist
;
8400 super
->devlist
= dv
;
8401 mpb
->num_raid_devs
++;
8403 imsm_update_version_info(super
);
8406 /* mdmon knows how to release update->space, but not
8407 * ((struct intel_dev *) update->space)->dev
8409 if (update
->space
) {
8415 case update_kill_array
: {
8416 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
8417 int victim
= u
->dev_idx
;
8418 struct active_array
*a
;
8419 struct intel_dev
**dp
;
8420 struct imsm_dev
*dev
;
8422 /* sanity check that we are not affecting the uuid of
8423 * active arrays, or deleting an active array
8425 * FIXME when immutable ids are available, but note that
8426 * we'll also need to fixup the invalidated/active
8427 * subarray indexes in mdstat
8429 for (a
= st
->arrays
; a
; a
= a
->next
)
8430 if (a
->info
.container_member
>= victim
)
8432 /* by definition if mdmon is running at least one array
8433 * is active in the container, so checking
8434 * mpb->num_raid_devs is just extra paranoia
8436 dev
= get_imsm_dev(super
, victim
);
8437 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
8438 dprintf("failed to delete subarray-%d\n", victim
);
8442 for (dp
= &super
->devlist
; *dp
;)
8443 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
8446 if ((*dp
)->index
> (unsigned)victim
)
8450 mpb
->num_raid_devs
--;
8451 super
->updates_pending
++;
8454 case update_rename_array
: {
8455 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
8456 char name
[MAX_RAID_SERIAL_LEN
+1];
8457 int target
= u
->dev_idx
;
8458 struct active_array
*a
;
8459 struct imsm_dev
*dev
;
8461 /* sanity check that we are not affecting the uuid of
8464 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
8465 name
[MAX_RAID_SERIAL_LEN
] = '\0';
8466 for (a
= st
->arrays
; a
; a
= a
->next
)
8467 if (a
->info
.container_member
== target
)
8469 dev
= get_imsm_dev(super
, u
->dev_idx
);
8470 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
8471 dprintf("failed to rename subarray-%d\n", target
);
8475 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
8476 super
->updates_pending
++;
8479 case update_add_remove_disk
: {
8480 /* we may be able to repair some arrays if disks are
8481 * being added, check teh status of add_remove_disk
8482 * if discs has been added.
8484 if (add_remove_disk_update(super
)) {
8485 struct active_array
*a
;
8487 super
->updates_pending
++;
8488 for (a
= st
->arrays
; a
; a
= a
->next
)
8489 a
->check_degraded
= 1;
8494 fprintf(stderr
, "error: unsuported process update type:"
8495 "(type: %d)\n", type
);
8499 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
);
8501 static void imsm_prepare_update(struct supertype
*st
,
8502 struct metadata_update
*update
)
8505 * Allocate space to hold new disk entries, raid-device entries or a new
8506 * mpb if necessary. The manager synchronously waits for updates to
8507 * complete in the monitor, so new mpb buffers allocated here can be
8508 * integrated by the monitor thread without worrying about live pointers
8509 * in the manager thread.
8511 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
8512 struct intel_super
*super
= st
->sb
;
8513 struct imsm_super
*mpb
= super
->anchor
;
8518 case update_general_migration_checkpoint
:
8519 dprintf("imsm: prepare_update() "
8520 "for update_general_migration_checkpoint called\n");
8522 case update_takeover
: {
8523 struct imsm_update_takeover
*u
= (void *)update
->buf
;
8524 if (u
->direction
== R0_TO_R10
) {
8525 void **tail
= (void **)&update
->space_list
;
8526 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
8527 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8528 int num_members
= map
->num_members
;
8531 /* allocate memory for added disks */
8532 for (i
= 0; i
< num_members
; i
++) {
8533 size
= sizeof(struct dl
);
8534 space
= xmalloc(size
);
8539 /* allocate memory for new device */
8540 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
8541 (num_members
* sizeof(__u32
));
8542 space
= xmalloc(size
);
8546 len
= disks_to_mpb_size(num_members
* 2);
8551 case update_reshape_container_disks
: {
8552 /* Every raid device in the container is about to
8553 * gain some more devices, and we will enter a
8555 * So each 'imsm_map' will be bigger, and the imsm_vol
8556 * will now hold 2 of them.
8557 * Thus we need new 'struct imsm_dev' allocations sized
8558 * as sizeof_imsm_dev but with more devices in both maps.
8560 struct imsm_update_reshape
*u
= (void *)update
->buf
;
8561 struct intel_dev
*dl
;
8562 void **space_tail
= (void**)&update
->space_list
;
8564 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
8566 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
8567 int size
= sizeof_imsm_dev(dl
->dev
, 1);
8569 if (u
->new_raid_disks
> u
->old_raid_disks
)
8570 size
+= sizeof(__u32
)*2*
8571 (u
->new_raid_disks
- u
->old_raid_disks
);
8578 len
= disks_to_mpb_size(u
->new_raid_disks
);
8579 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
8582 case update_reshape_migration
: {
8583 /* for migration level 0->5 we need to add disks
8584 * so the same as for container operation we will copy
8585 * device to the bigger location.
8586 * in memory prepared device and new disk area are prepared
8587 * for usage in process update
8589 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
8590 struct intel_dev
*id
;
8591 void **space_tail
= (void **)&update
->space_list
;
8594 int current_level
= -1;
8596 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
8598 /* add space for bigger array in update
8600 for (id
= super
->devlist
; id
; id
= id
->next
) {
8601 if (id
->index
== (unsigned)u
->subdev
) {
8602 size
= sizeof_imsm_dev(id
->dev
, 1);
8603 if (u
->new_raid_disks
> u
->old_raid_disks
)
8604 size
+= sizeof(__u32
)*2*
8605 (u
->new_raid_disks
- u
->old_raid_disks
);
8613 if (update
->space_list
== NULL
)
8616 /* add space for disk in update
8618 size
= sizeof(struct dl
);
8624 /* add spare device to update
8626 for (id
= super
->devlist
; id
; id
= id
->next
)
8627 if (id
->index
== (unsigned)u
->subdev
) {
8628 struct imsm_dev
*dev
;
8629 struct imsm_map
*map
;
8631 dev
= get_imsm_dev(super
, u
->subdev
);
8632 map
= get_imsm_map(dev
, MAP_0
);
8633 current_level
= map
->raid_level
;
8636 if ((u
->new_level
== 5) && (u
->new_level
!= current_level
)) {
8637 struct mdinfo
*spares
;
8639 spares
= get_spares_for_grow(st
);
8647 makedev(dev
->disk
.major
,
8649 dl
= get_disk_super(super
,
8652 dl
->index
= u
->old_raid_disks
;
8658 len
= disks_to_mpb_size(u
->new_raid_disks
);
8659 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
8662 case update_size_change
: {
8665 case update_create_array
: {
8666 struct imsm_update_create_array
*u
= (void *) update
->buf
;
8667 struct intel_dev
*dv
;
8668 struct imsm_dev
*dev
= &u
->dev
;
8669 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8671 struct disk_info
*inf
;
8675 inf
= get_disk_info(u
);
8676 len
= sizeof_imsm_dev(dev
, 1);
8677 /* allocate a new super->devlist entry */
8678 dv
= xmalloc(sizeof(*dv
));
8679 dv
->dev
= xmalloc(len
);
8682 /* count how many spares will be converted to members */
8683 for (i
= 0; i
< map
->num_members
; i
++) {
8684 dl
= serial_to_dl(inf
[i
].serial
, super
);
8686 /* hmm maybe it failed?, nothing we can do about
8691 if (count_memberships(dl
, super
) == 0)
8694 len
+= activate
* sizeof(struct imsm_disk
);
8701 /* check if we need a larger metadata buffer */
8702 if (super
->next_buf
)
8703 buf_len
= super
->next_len
;
8705 buf_len
= super
->len
;
8707 if (__le32_to_cpu(mpb
->mpb_size
) + len
> buf_len
) {
8708 /* ok we need a larger buf than what is currently allocated
8709 * if this allocation fails process_update will notice that
8710 * ->next_len is set and ->next_buf is NULL
8712 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + len
, 512);
8713 if (super
->next_buf
)
8714 free(super
->next_buf
);
8716 super
->next_len
= buf_len
;
8717 if (posix_memalign(&super
->next_buf
, 512, buf_len
) == 0)
8718 memset(super
->next_buf
, 0, buf_len
);
8720 super
->next_buf
= NULL
;
8724 /* must be called while manager is quiesced */
8725 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
8727 struct imsm_super
*mpb
= super
->anchor
;
8729 struct imsm_dev
*dev
;
8730 struct imsm_map
*map
;
8731 int i
, j
, num_members
;
8734 dprintf("%s: deleting device[%d] from imsm_super\n",
8737 /* shift all indexes down one */
8738 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
8739 if (iter
->index
> (int)index
)
8741 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
8742 if (iter
->index
> (int)index
)
8745 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8746 dev
= get_imsm_dev(super
, i
);
8747 map
= get_imsm_map(dev
, MAP_0
);
8748 num_members
= map
->num_members
;
8749 for (j
= 0; j
< num_members
; j
++) {
8750 /* update ord entries being careful not to propagate
8751 * ord-flags to the first map
8753 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_X
);
8755 if (ord_to_idx(ord
) <= index
)
8758 map
= get_imsm_map(dev
, MAP_0
);
8759 set_imsm_ord_tbl_ent(map
, j
, ord_to_idx(ord
- 1));
8760 map
= get_imsm_map(dev
, MAP_1
);
8762 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
8767 super
->updates_pending
++;
8769 struct dl
*dl
= *dlp
;
8771 *dlp
= (*dlp
)->next
;
8772 __free_imsm_disk(dl
);
8775 #endif /* MDASSEMBLE */
8777 static void close_targets(int *targets
, int new_disks
)
8784 for (i
= 0; i
< new_disks
; i
++) {
8785 if (targets
[i
] >= 0) {
8792 static int imsm_get_allowed_degradation(int level
, int raid_disks
,
8793 struct intel_super
*super
,
8794 struct imsm_dev
*dev
)
8800 struct imsm_map
*map
;
8803 ret_val
= raid_disks
/2;
8804 /* check map if all disks pairs not failed
8807 map
= get_imsm_map(dev
, MAP_0
);
8808 for (i
= 0; i
< ret_val
; i
++) {
8809 int degradation
= 0;
8810 if (get_imsm_disk(super
, i
) == NULL
)
8812 if (get_imsm_disk(super
, i
+ 1) == NULL
)
8814 if (degradation
== 2)
8817 map
= get_imsm_map(dev
, MAP_1
);
8818 /* if there is no second map
8819 * result can be returned
8823 /* check degradation in second map
8825 for (i
= 0; i
< ret_val
; i
++) {
8826 int degradation
= 0;
8827 if (get_imsm_disk(super
, i
) == NULL
)
8829 if (get_imsm_disk(super
, i
+ 1) == NULL
)
8831 if (degradation
== 2)
8846 /*******************************************************************************
8847 * Function: open_backup_targets
8848 * Description: Function opens file descriptors for all devices given in
8851 * info : general array info
8852 * raid_disks : number of disks
8853 * raid_fds : table of device's file descriptors
8854 * super : intel super for raid10 degradation check
8855 * dev : intel device for raid10 degradation check
8859 ******************************************************************************/
8860 int open_backup_targets(struct mdinfo
*info
, int raid_disks
, int *raid_fds
,
8861 struct intel_super
*super
, struct imsm_dev
*dev
)
8867 for (i
= 0; i
< raid_disks
; i
++)
8870 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
8873 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
)) {
8874 dprintf("disk is faulty!!\n");
8878 if ((sd
->disk
.raid_disk
>= raid_disks
) ||
8879 (sd
->disk
.raid_disk
< 0))
8882 dn
= map_dev(sd
->disk
.major
,
8884 raid_fds
[sd
->disk
.raid_disk
] = dev_open(dn
, O_RDWR
);
8885 if (raid_fds
[sd
->disk
.raid_disk
] < 0) {
8886 fprintf(stderr
, "cannot open component\n");
8891 /* check if maximum array degradation level is not exceeded
8893 if ((raid_disks
- opened
) >
8894 imsm_get_allowed_degradation(info
->new_level
,
8897 fprintf(stderr
, "Not enough disks can be opened.\n");
8898 close_targets(raid_fds
, raid_disks
);
8905 /*******************************************************************************
8906 * Function: init_migr_record_imsm
8907 * Description: Function inits imsm migration record
8909 * super : imsm internal array info
8910 * dev : device under migration
8911 * info : general array info to find the smallest device
8914 ******************************************************************************/
8915 void init_migr_record_imsm(struct supertype
*st
, struct imsm_dev
*dev
,
8916 struct mdinfo
*info
)
8918 struct intel_super
*super
= st
->sb
;
8919 struct migr_record
*migr_rec
= super
->migr_rec
;
8921 unsigned long long dsize
, dev_sectors
;
8922 long long unsigned min_dev_sectors
= -1LLU;
8926 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
8927 struct imsm_map
*map_src
= get_imsm_map(dev
, MAP_1
);
8928 unsigned long long num_migr_units
;
8929 unsigned long long array_blocks
;
8931 memset(migr_rec
, 0, sizeof(struct migr_record
));
8932 migr_rec
->family_num
= __cpu_to_le32(super
->anchor
->family_num
);
8934 /* only ascending reshape supported now */
8935 migr_rec
->ascending_migr
= __cpu_to_le32(1);
8937 migr_rec
->dest_depth_per_unit
= GEN_MIGR_AREA_SIZE
/
8938 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
8939 migr_rec
->dest_depth_per_unit
*=
8940 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
8941 new_data_disks
= imsm_num_data_members(dev
, MAP_0
);
8942 migr_rec
->blocks_per_unit
=
8943 __cpu_to_le32(migr_rec
->dest_depth_per_unit
* new_data_disks
);
8944 migr_rec
->dest_depth_per_unit
=
8945 __cpu_to_le32(migr_rec
->dest_depth_per_unit
);
8946 array_blocks
= info
->component_size
* new_data_disks
;
8948 array_blocks
/ __le32_to_cpu(migr_rec
->blocks_per_unit
);
8950 if (array_blocks
% __le32_to_cpu(migr_rec
->blocks_per_unit
))
8952 migr_rec
->num_migr_units
= __cpu_to_le32(num_migr_units
);
8954 migr_rec
->post_migr_vol_cap
= dev
->size_low
;
8955 migr_rec
->post_migr_vol_cap_hi
= dev
->size_high
;
8958 /* Find the smallest dev */
8959 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
8960 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
8961 fd
= dev_open(nm
, O_RDONLY
);
8964 get_dev_size(fd
, NULL
, &dsize
);
8965 dev_sectors
= dsize
/ 512;
8966 if (dev_sectors
< min_dev_sectors
)
8967 min_dev_sectors
= dev_sectors
;
8970 migr_rec
->ckpt_area_pba
= __cpu_to_le32(min_dev_sectors
-
8971 RAID_DISK_RESERVED_BLOCKS_IMSM_HI
);
8973 write_imsm_migr_rec(st
);
8978 /*******************************************************************************
8979 * Function: save_backup_imsm
8980 * Description: Function saves critical data stripes to Migration Copy Area
8981 * and updates the current migration unit status.
8982 * Use restore_stripes() to form a destination stripe,
8983 * and to write it to the Copy Area.
8985 * st : supertype information
8986 * dev : imsm device that backup is saved for
8987 * info : general array info
8988 * buf : input buffer
8989 * length : length of data to backup (blocks_per_unit)
8993 ******************************************************************************/
8994 int save_backup_imsm(struct supertype
*st
,
8995 struct imsm_dev
*dev
,
8996 struct mdinfo
*info
,
9001 struct intel_super
*super
= st
->sb
;
9002 unsigned long long *target_offsets
= NULL
;
9003 int *targets
= NULL
;
9005 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
9006 int new_disks
= map_dest
->num_members
;
9007 int dest_layout
= 0;
9009 unsigned long long start
;
9010 int data_disks
= imsm_num_data_members(dev
, MAP_0
);
9012 targets
= xmalloc(new_disks
* sizeof(int));
9014 for (i
= 0; i
< new_disks
; i
++)
9017 target_offsets
= xcalloc(new_disks
, sizeof(unsigned long long));
9019 start
= info
->reshape_progress
* 512;
9020 for (i
= 0; i
< new_disks
; i
++) {
9021 target_offsets
[i
] = (unsigned long long)
9022 __le32_to_cpu(super
->migr_rec
->ckpt_area_pba
) * 512;
9023 /* move back copy area adderss, it will be moved forward
9024 * in restore_stripes() using start input variable
9026 target_offsets
[i
] -= start
/data_disks
;
9029 if (open_backup_targets(info
, new_disks
, targets
,
9033 dest_layout
= imsm_level_to_layout(map_dest
->raid_level
);
9034 dest_chunk
= __le16_to_cpu(map_dest
->blocks_per_strip
) * 512;
9036 if (restore_stripes(targets
, /* list of dest devices */
9037 target_offsets
, /* migration record offsets */
9040 map_dest
->raid_level
,
9042 -1, /* source backup file descriptor */
9043 0, /* input buf offset
9044 * always 0 buf is already offseted */
9048 pr_err("Error restoring stripes\n");
9056 close_targets(targets
, new_disks
);
9059 free(target_offsets
);
9064 /*******************************************************************************
9065 * Function: save_checkpoint_imsm
9066 * Description: Function called for current unit status update
9067 * in the migration record. It writes it to disk.
9069 * super : imsm internal array info
9070 * info : general array info
9074 * 2: failure, means no valid migration record
9075 * / no general migration in progress /
9076 ******************************************************************************/
9077 int save_checkpoint_imsm(struct supertype
*st
, struct mdinfo
*info
, int state
)
9079 struct intel_super
*super
= st
->sb
;
9080 unsigned long long blocks_per_unit
;
9081 unsigned long long curr_migr_unit
;
9083 if (load_imsm_migr_rec(super
, info
) != 0) {
9084 dprintf("imsm: ERROR: Cannot read migration record "
9085 "for checkpoint save.\n");
9089 blocks_per_unit
= __le32_to_cpu(super
->migr_rec
->blocks_per_unit
);
9090 if (blocks_per_unit
== 0) {
9091 dprintf("imsm: no migration in progress.\n");
9094 curr_migr_unit
= info
->reshape_progress
/ blocks_per_unit
;
9095 /* check if array is alligned to copy area
9096 * if it is not alligned, add one to current migration unit value
9097 * this can happend on array reshape finish only
9099 if (info
->reshape_progress
% blocks_per_unit
)
9102 super
->migr_rec
->curr_migr_unit
=
9103 __cpu_to_le32(curr_migr_unit
);
9104 super
->migr_rec
->rec_status
= __cpu_to_le32(state
);
9105 super
->migr_rec
->dest_1st_member_lba
=
9106 __cpu_to_le32(curr_migr_unit
*
9107 __le32_to_cpu(super
->migr_rec
->dest_depth_per_unit
));
9108 if (write_imsm_migr_rec(st
) < 0) {
9109 dprintf("imsm: Cannot write migration record "
9110 "outside backup area\n");
9117 /*******************************************************************************
9118 * Function: recover_backup_imsm
9119 * Description: Function recovers critical data from the Migration Copy Area
9120 * while assembling an array.
9122 * super : imsm internal array info
9123 * info : general array info
9125 * 0 : success (or there is no data to recover)
9127 ******************************************************************************/
9128 int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
)
9130 struct intel_super
*super
= st
->sb
;
9131 struct migr_record
*migr_rec
= super
->migr_rec
;
9132 struct imsm_map
*map_dest
= NULL
;
9133 struct intel_dev
*id
= NULL
;
9134 unsigned long long read_offset
;
9135 unsigned long long write_offset
;
9137 int *targets
= NULL
;
9138 int new_disks
, i
, err
;
9141 unsigned long curr_migr_unit
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
9142 unsigned long num_migr_units
= __le32_to_cpu(migr_rec
->num_migr_units
);
9144 int skipped_disks
= 0;
9146 err
= sysfs_get_str(info
, NULL
, "array_state", (char *)buffer
, 20);
9150 /* recover data only during assemblation */
9151 if (strncmp(buffer
, "inactive", 8) != 0)
9153 /* no data to recover */
9154 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
9156 if (curr_migr_unit
>= num_migr_units
)
9159 /* find device during reshape */
9160 for (id
= super
->devlist
; id
; id
= id
->next
)
9161 if (is_gen_migration(id
->dev
))
9166 map_dest
= get_imsm_map(id
->dev
, MAP_0
);
9167 new_disks
= map_dest
->num_members
;
9169 read_offset
= (unsigned long long)
9170 __le32_to_cpu(migr_rec
->ckpt_area_pba
) * 512;
9172 write_offset
= ((unsigned long long)
9173 __le32_to_cpu(migr_rec
->dest_1st_member_lba
) +
9174 pba_of_lba0(map_dest
)) * 512;
9176 unit_len
= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
9177 if (posix_memalign((void **)&buf
, 512, unit_len
) != 0)
9179 targets
= xcalloc(new_disks
, sizeof(int));
9181 if (open_backup_targets(info
, new_disks
, targets
, super
, id
->dev
)) {
9182 pr_err("Cannot open some devices belonging to array.\n");
9186 for (i
= 0; i
< new_disks
; i
++) {
9187 if (targets
[i
] < 0) {
9191 if (lseek64(targets
[i
], read_offset
, SEEK_SET
) < 0) {
9192 pr_err("Cannot seek to block: %s\n",
9197 if ((unsigned)read(targets
[i
], buf
, unit_len
) != unit_len
) {
9198 pr_err("Cannot read copy area block: %s\n",
9203 if (lseek64(targets
[i
], write_offset
, SEEK_SET
) < 0) {
9204 pr_err("Cannot seek to block: %s\n",
9209 if ((unsigned)write(targets
[i
], buf
, unit_len
) != unit_len
) {
9210 pr_err("Cannot restore block: %s\n",
9217 if (skipped_disks
> imsm_get_allowed_degradation(info
->new_level
,
9221 pr_err("Cannot restore data from backup."
9222 " Too many failed disks\n");
9226 if (save_checkpoint_imsm(st
, info
, UNIT_SRC_NORMAL
)) {
9227 /* ignore error == 2, this can mean end of reshape here
9229 dprintf("imsm: Cannot write checkpoint to "
9230 "migration record (UNIT_SRC_NORMAL) during restart\n");
9236 for (i
= 0; i
< new_disks
; i
++)
9245 static char disk_by_path
[] = "/dev/disk/by-path/";
9247 static const char *imsm_get_disk_controller_domain(const char *path
)
9249 char disk_path
[PATH_MAX
];
9253 strncpy(disk_path
, disk_by_path
, PATH_MAX
- 1);
9254 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
9255 if (stat(disk_path
, &st
) == 0) {
9256 struct sys_dev
* hba
;
9259 path
= devt_to_devpath(st
.st_rdev
);
9262 hba
= find_disk_attached_hba(-1, path
);
9263 if (hba
&& hba
->type
== SYS_DEV_SAS
)
9265 else if (hba
&& hba
->type
== SYS_DEV_SATA
)
9269 dprintf("path: %s hba: %s attached: %s\n",
9270 path
, (hba
) ? hba
->path
: "NULL", drv
);
9278 static int imsm_find_array_minor_by_subdev(int subdev
, int container
, int *minor
)
9280 char subdev_name
[20];
9281 struct mdstat_ent
*mdstat
;
9283 sprintf(subdev_name
, "%d", subdev
);
9284 mdstat
= mdstat_by_subdev(subdev_name
, container
);
9288 *minor
= mdstat
->devnum
;
9289 free_mdstat(mdstat
);
9293 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
9294 struct geo_params
*geo
,
9295 int *old_raid_disks
,
9298 /* currently we only support increasing the number of devices
9299 * for a container. This increases the number of device for each
9300 * member array. They must all be RAID0 or RAID5.
9303 struct mdinfo
*info
, *member
;
9304 int devices_that_can_grow
= 0;
9306 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): "
9307 "st->devnum = (%i)\n",
9310 if (geo
->size
!= -1 ||
9311 geo
->level
!= UnSet
||
9312 geo
->layout
!= UnSet
||
9313 geo
->chunksize
!= 0 ||
9314 geo
->raid_disks
== UnSet
) {
9315 dprintf("imsm: Container operation is allowed for "
9316 "raid disks number change only.\n");
9320 if (direction
== ROLLBACK_METADATA_CHANGES
) {
9321 dprintf("imsm: Metadata changes rollback is not supported for "
9322 "container operation.\n");
9326 info
= container_content_imsm(st
, NULL
);
9327 for (member
= info
; member
; member
= member
->next
) {
9331 dprintf("imsm: checking device_num: %i\n",
9332 member
->container_member
);
9334 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
9335 /* we work on container for Online Capacity Expansion
9336 * only so raid_disks has to grow
9338 dprintf("imsm: for container operation raid disks "
9339 "increase is required\n");
9343 if ((info
->array
.level
!= 0) &&
9344 (info
->array
.level
!= 5)) {
9345 /* we cannot use this container with other raid level
9347 dprintf("imsm: for container operation wrong"
9348 " raid level (%i) detected\n",
9352 /* check for platform support
9353 * for this raid level configuration
9355 struct intel_super
*super
= st
->sb
;
9356 if (!is_raid_level_supported(super
->orom
,
9357 member
->array
.level
,
9359 dprintf("platform does not support raid%d with"
9363 geo
->raid_disks
> 1 ? "s" : "");
9366 /* check if component size is aligned to chunk size
9368 if (info
->component_size
%
9369 (info
->array
.chunk_size
/512)) {
9370 dprintf("Component size is not aligned to "
9376 if (*old_raid_disks
&&
9377 info
->array
.raid_disks
!= *old_raid_disks
)
9379 *old_raid_disks
= info
->array
.raid_disks
;
9381 /* All raid5 and raid0 volumes in container
9382 * have to be ready for Online Capacity Expansion
9383 * so they need to be assembled. We have already
9384 * checked that no recovery etc is happening.
9386 result
= imsm_find_array_minor_by_subdev(member
->container_member
,
9390 dprintf("imsm: cannot find array\n");
9393 devices_that_can_grow
++;
9396 if (!member
&& devices_that_can_grow
)
9400 dprintf("\tContainer operation allowed\n");
9402 dprintf("\tError: %i\n", ret_val
);
9407 /* Function: get_spares_for_grow
9408 * Description: Allocates memory and creates list of spare devices
9409 * avaliable in container. Checks if spare drive size is acceptable.
9410 * Parameters: Pointer to the supertype structure
9411 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
9414 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
9416 unsigned long long min_size
= min_acceptable_spare_size_imsm(st
);
9417 return container_choose_spares(st
, min_size
, NULL
, NULL
, NULL
, 0);
9420 /******************************************************************************
9421 * function: imsm_create_metadata_update_for_reshape
9422 * Function creates update for whole IMSM container.
9424 ******************************************************************************/
9425 static int imsm_create_metadata_update_for_reshape(
9426 struct supertype
*st
,
9427 struct geo_params
*geo
,
9429 struct imsm_update_reshape
**updatep
)
9431 struct intel_super
*super
= st
->sb
;
9432 struct imsm_super
*mpb
= super
->anchor
;
9433 int update_memory_size
= 0;
9434 struct imsm_update_reshape
*u
= NULL
;
9435 struct mdinfo
*spares
= NULL
;
9437 int delta_disks
= 0;
9440 dprintf("imsm_update_metadata_for_reshape(enter) raid_disks = %i\n",
9443 delta_disks
= geo
->raid_disks
- old_raid_disks
;
9445 /* size of all update data without anchor */
9446 update_memory_size
= sizeof(struct imsm_update_reshape
);
9448 /* now add space for spare disks that we need to add. */
9449 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
9451 u
= xcalloc(1, update_memory_size
);
9452 u
->type
= update_reshape_container_disks
;
9453 u
->old_raid_disks
= old_raid_disks
;
9454 u
->new_raid_disks
= geo
->raid_disks
;
9456 /* now get spare disks list
9458 spares
= get_spares_for_grow(st
);
9461 || delta_disks
> spares
->array
.spare_disks
) {
9462 pr_err("imsm: ERROR: Cannot get spare devices "
9463 "for %s.\n", geo
->dev_name
);
9468 /* we have got spares
9469 * update disk list in imsm_disk list table in anchor
9471 dprintf("imsm: %i spares are available.\n\n",
9472 spares
->array
.spare_disks
);
9475 for (i
= 0; i
< delta_disks
; i
++) {
9480 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
9482 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
9483 dl
->index
= mpb
->num_disks
;
9493 dprintf("imsm: reshape update preparation :");
9494 if (i
== delta_disks
) {
9497 return update_memory_size
;
9500 dprintf(" Error\n");
9506 /******************************************************************************
9507 * function: imsm_create_metadata_update_for_size_change()
9508 * Creates update for IMSM array for array size change.
9510 ******************************************************************************/
9511 static int imsm_create_metadata_update_for_size_change(
9512 struct supertype
*st
,
9513 struct geo_params
*geo
,
9514 struct imsm_update_size_change
**updatep
)
9516 struct intel_super
*super
= st
->sb
;
9517 int update_memory_size
= 0;
9518 struct imsm_update_size_change
*u
= NULL
;
9520 dprintf("imsm_create_metadata_update_for_size_change(enter)"
9521 " New size = %llu\n", geo
->size
);
9523 /* size of all update data without anchor */
9524 update_memory_size
= sizeof(struct imsm_update_size_change
);
9526 u
= xcalloc(1, update_memory_size
);
9527 u
->type
= update_size_change
;
9528 u
->subdev
= super
->current_vol
;
9529 u
->new_size
= geo
->size
;
9531 dprintf("imsm: reshape update preparation : OK\n");
9534 return update_memory_size
;
9537 /******************************************************************************
9538 * function: imsm_create_metadata_update_for_migration()
9539 * Creates update for IMSM array.
9541 ******************************************************************************/
9542 static int imsm_create_metadata_update_for_migration(
9543 struct supertype
*st
,
9544 struct geo_params
*geo
,
9545 struct imsm_update_reshape_migration
**updatep
)
9547 struct intel_super
*super
= st
->sb
;
9548 int update_memory_size
= 0;
9549 struct imsm_update_reshape_migration
*u
= NULL
;
9550 struct imsm_dev
*dev
;
9551 int previous_level
= -1;
9553 dprintf("imsm_create_metadata_update_for_migration(enter)"
9554 " New Level = %i\n", geo
->level
);
9556 /* size of all update data without anchor */
9557 update_memory_size
= sizeof(struct imsm_update_reshape_migration
);
9559 u
= xcalloc(1, update_memory_size
);
9560 u
->type
= update_reshape_migration
;
9561 u
->subdev
= super
->current_vol
;
9562 u
->new_level
= geo
->level
;
9563 u
->new_layout
= geo
->layout
;
9564 u
->new_raid_disks
= u
->old_raid_disks
= geo
->raid_disks
;
9565 u
->new_disks
[0] = -1;
9566 u
->new_chunksize
= -1;
9568 dev
= get_imsm_dev(super
, u
->subdev
);
9570 struct imsm_map
*map
;
9572 map
= get_imsm_map(dev
, MAP_0
);
9574 int current_chunk_size
=
9575 __le16_to_cpu(map
->blocks_per_strip
) / 2;
9577 if (geo
->chunksize
!= current_chunk_size
) {
9578 u
->new_chunksize
= geo
->chunksize
/ 1024;
9580 "chunk size change from %i to %i\n",
9581 current_chunk_size
, u
->new_chunksize
);
9583 previous_level
= map
->raid_level
;
9586 if ((geo
->level
== 5) && (previous_level
== 0)) {
9587 struct mdinfo
*spares
= NULL
;
9589 u
->new_raid_disks
++;
9590 spares
= get_spares_for_grow(st
);
9591 if ((spares
== NULL
) || (spares
->array
.spare_disks
< 1)) {
9594 update_memory_size
= 0;
9595 dprintf("error: cannot get spare device "
9596 "for requested migration");
9601 dprintf("imsm: reshape update preparation : OK\n");
9604 return update_memory_size
;
9607 static void imsm_update_metadata_locally(struct supertype
*st
,
9610 struct metadata_update mu
;
9615 mu
.space_list
= NULL
;
9617 imsm_prepare_update(st
, &mu
);
9618 imsm_process_update(st
, &mu
);
9620 while (mu
.space_list
) {
9621 void **space
= mu
.space_list
;
9622 mu
.space_list
= *space
;
9627 /***************************************************************************
9628 * Function: imsm_analyze_change
9629 * Description: Function analyze change for single volume
9630 * and validate if transition is supported
9631 * Parameters: Geometry parameters, supertype structure,
9632 * metadata change direction (apply/rollback)
9633 * Returns: Operation type code on success, -1 if fail
9634 ****************************************************************************/
9635 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
9636 struct geo_params
*geo
,
9643 /* number of added/removed disks in operation result */
9644 int devNumChange
= 0;
9645 /* imsm compatible layout value for array geometry verification */
9646 int imsm_layout
= -1;
9648 struct imsm_dev
*dev
;
9649 struct intel_super
*super
;
9650 long long current_size
;
9651 unsigned long long free_size
;
9655 getinfo_super_imsm_volume(st
, &info
, NULL
);
9656 if ((geo
->level
!= info
.array
.level
) &&
9657 (geo
->level
>= 0) &&
9658 (geo
->level
!= UnSet
)) {
9659 switch (info
.array
.level
) {
9661 if (geo
->level
== 5) {
9662 change
= CH_MIGRATION
;
9663 if (geo
->layout
!= ALGORITHM_LEFT_ASYMMETRIC
) {
9664 pr_err("Error. Requested Layout "
9665 "not supported (left-asymmetric layout "
9666 "is supported only)!\n");
9668 goto analyse_change_exit
;
9670 imsm_layout
= geo
->layout
;
9672 devNumChange
= 1; /* parity disk added */
9673 } else if (geo
->level
== 10) {
9674 change
= CH_TAKEOVER
;
9676 devNumChange
= 2; /* two mirrors added */
9677 imsm_layout
= 0x102; /* imsm supported layout */
9682 if (geo
->level
== 0) {
9683 change
= CH_TAKEOVER
;
9685 devNumChange
= -(geo
->raid_disks
/2);
9686 imsm_layout
= 0; /* imsm raid0 layout */
9691 pr_err("Error. Level Migration from %d to %d "
9693 info
.array
.level
, geo
->level
);
9694 goto analyse_change_exit
;
9697 geo
->level
= info
.array
.level
;
9699 if ((geo
->layout
!= info
.array
.layout
)
9700 && ((geo
->layout
!= UnSet
) && (geo
->layout
!= -1))) {
9701 change
= CH_MIGRATION
;
9702 if ((info
.array
.layout
== 0)
9703 && (info
.array
.level
== 5)
9704 && (geo
->layout
== 5)) {
9705 /* reshape 5 -> 4 */
9706 } else if ((info
.array
.layout
== 5)
9707 && (info
.array
.level
== 5)
9708 && (geo
->layout
== 0)) {
9709 /* reshape 4 -> 5 */
9713 pr_err("Error. Layout Migration from %d to %d "
9715 info
.array
.layout
, geo
->layout
);
9717 goto analyse_change_exit
;
9720 geo
->layout
= info
.array
.layout
;
9721 if (imsm_layout
== -1)
9722 imsm_layout
= info
.array
.layout
;
9725 if ((geo
->chunksize
> 0) && (geo
->chunksize
!= UnSet
)
9726 && (geo
->chunksize
!= info
.array
.chunk_size
))
9727 change
= CH_MIGRATION
;
9729 geo
->chunksize
= info
.array
.chunk_size
;
9731 chunk
= geo
->chunksize
/ 1024;
9734 dev
= get_imsm_dev(super
, super
->current_vol
);
9735 data_disks
= imsm_num_data_members(dev
, MAP_0
);
9736 /* compute current size per disk member
9738 current_size
= info
.custom_array_size
/ data_disks
;
9740 if (geo
->size
> 0) {
9741 /* align component size
9743 geo
->size
= imsm_component_size_aligment_check(
9744 get_imsm_raid_level(dev
->vol
.map
),
9749 if ((current_size
!= geo
->size
) && (geo
->size
>= 0)) {
9751 pr_err("Error. Size change should be the only "
9752 "one at a time.\n");
9754 goto analyse_change_exit
;
9756 if ((super
->current_vol
+ 1) != super
->anchor
->num_raid_devs
) {
9757 pr_err("Error. The last volume in container "
9758 "can be expanded only (%i/%i).\n",
9759 super
->current_vol
, st
->devnum
);
9760 goto analyse_change_exit
;
9762 /* check the maximum available size
9764 rv
= imsm_get_free_size(st
, dev
->vol
.map
->num_members
,
9765 0, chunk
, &free_size
);
9767 /* Cannot find maximum available space
9771 max_size
= free_size
+ current_size
;
9772 /* align component size
9774 max_size
= imsm_component_size_aligment_check(
9775 get_imsm_raid_level(dev
->vol
.map
),
9779 if (geo
->size
== 0) {
9780 /* requested size change to the maximum available size
9782 if (max_size
== 0) {
9783 pr_err("Error. Cannot find "
9784 "maximum available space.\n");
9786 goto analyse_change_exit
;
9788 geo
->size
= max_size
;
9791 if ((direction
== ROLLBACK_METADATA_CHANGES
)) {
9792 /* accept size for rollback only
9795 /* round size due to metadata compatibility
9797 geo
->size
= (geo
->size
>> SECT_PER_MB_SHIFT
)
9798 << SECT_PER_MB_SHIFT
;
9799 dprintf("Prepare update for size change to %llu\n",
9801 if (current_size
>= geo
->size
) {
9802 pr_err("Error. Size expansion is "
9803 "supported only (current size is %llu, "
9804 "requested size /rounded/ is %llu).\n",
9805 current_size
, geo
->size
);
9806 goto analyse_change_exit
;
9808 if (max_size
&& geo
->size
> max_size
) {
9809 pr_err("Error. Requested size is larger "
9810 "than maximum available size (maximum "
9811 "available size is %llu, "
9812 "requested size /rounded/ is %llu).\n",
9813 max_size
, geo
->size
);
9814 goto analyse_change_exit
;
9817 geo
->size
*= data_disks
;
9818 geo
->raid_disks
= dev
->vol
.map
->num_members
;
9819 change
= CH_ARRAY_SIZE
;
9821 if (!validate_geometry_imsm(st
,
9824 geo
->raid_disks
+ devNumChange
,
9831 struct intel_super
*super
= st
->sb
;
9832 struct imsm_super
*mpb
= super
->anchor
;
9834 if (mpb
->num_raid_devs
> 1) {
9835 pr_err("Error. Cannot perform operation on %s"
9836 "- for this operation it MUST be single "
9837 "array in container\n",
9843 analyse_change_exit
:
9844 if ((direction
== ROLLBACK_METADATA_CHANGES
) &&
9845 ((change
== CH_MIGRATION
) || (change
== CH_TAKEOVER
))) {
9846 dprintf("imsm: Metadata changes rollback is not supported for "
9847 "migration and takeover operations.\n");
9853 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
9855 struct intel_super
*super
= st
->sb
;
9856 struct imsm_update_takeover
*u
;
9858 u
= xmalloc(sizeof(struct imsm_update_takeover
));
9860 u
->type
= update_takeover
;
9861 u
->subarray
= super
->current_vol
;
9863 /* 10->0 transition */
9864 if (geo
->level
== 0)
9865 u
->direction
= R10_TO_R0
;
9867 /* 0->10 transition */
9868 if (geo
->level
== 10)
9869 u
->direction
= R0_TO_R10
;
9871 /* update metadata locally */
9872 imsm_update_metadata_locally(st
, u
,
9873 sizeof(struct imsm_update_takeover
));
9874 /* and possibly remotely */
9875 if (st
->update_tail
)
9876 append_metadata_update(st
, u
,
9877 sizeof(struct imsm_update_takeover
));
9884 static int imsm_reshape_super(struct supertype
*st
, long long size
, int level
,
9885 int layout
, int chunksize
, int raid_disks
,
9886 int delta_disks
, char *backup
, char *dev
,
9887 int direction
, int verbose
)
9890 struct geo_params geo
;
9892 dprintf("imsm: reshape_super called.\n");
9894 memset(&geo
, 0, sizeof(struct geo_params
));
9897 geo
.dev_id
= st
->devnum
;
9900 geo
.layout
= layout
;
9901 geo
.chunksize
= chunksize
;
9902 geo
.raid_disks
= raid_disks
;
9903 if (delta_disks
!= UnSet
)
9904 geo
.raid_disks
+= delta_disks
;
9906 dprintf("\tfor level : %i\n", geo
.level
);
9907 dprintf("\tfor raid_disks : %i\n", geo
.raid_disks
);
9909 if (experimental() == 0)
9912 if (st
->container_dev
== st
->devnum
) {
9913 /* On container level we can only increase number of devices. */
9914 dprintf("imsm: info: Container operation\n");
9915 int old_raid_disks
= 0;
9917 if (imsm_reshape_is_allowed_on_container(
9918 st
, &geo
, &old_raid_disks
, direction
)) {
9919 struct imsm_update_reshape
*u
= NULL
;
9922 len
= imsm_create_metadata_update_for_reshape(
9923 st
, &geo
, old_raid_disks
, &u
);
9926 dprintf("imsm: Cannot prepare update\n");
9927 goto exit_imsm_reshape_super
;
9931 /* update metadata locally */
9932 imsm_update_metadata_locally(st
, u
, len
);
9933 /* and possibly remotely */
9934 if (st
->update_tail
)
9935 append_metadata_update(st
, u
, len
);
9940 pr_err("(imsm) Operation "
9941 "is not allowed on this container\n");
9944 /* On volume level we support following operations
9945 * - takeover: raid10 -> raid0; raid0 -> raid10
9946 * - chunk size migration
9947 * - migration: raid5 -> raid0; raid0 -> raid5
9949 struct intel_super
*super
= st
->sb
;
9950 struct intel_dev
*dev
= super
->devlist
;
9952 dprintf("imsm: info: Volume operation\n");
9953 /* find requested device */
9955 if (imsm_find_array_minor_by_subdev(
9956 dev
->index
, st
->container_dev
, &devnum
) == 0
9957 && devnum
== geo
.dev_id
)
9962 pr_err("Cannot find %s (%i) subarray\n",
9963 geo
.dev_name
, geo
.dev_id
);
9964 goto exit_imsm_reshape_super
;
9966 super
->current_vol
= dev
->index
;
9967 change
= imsm_analyze_change(st
, &geo
, direction
);
9970 ret_val
= imsm_takeover(st
, &geo
);
9972 case CH_MIGRATION
: {
9973 struct imsm_update_reshape_migration
*u
= NULL
;
9975 imsm_create_metadata_update_for_migration(
9979 "Cannot prepare update\n");
9983 /* update metadata locally */
9984 imsm_update_metadata_locally(st
, u
, len
);
9985 /* and possibly remotely */
9986 if (st
->update_tail
)
9987 append_metadata_update(st
, u
, len
);
9992 case CH_ARRAY_SIZE
: {
9993 struct imsm_update_size_change
*u
= NULL
;
9995 imsm_create_metadata_update_for_size_change(
9999 "Cannot prepare update\n");
10003 /* update metadata locally */
10004 imsm_update_metadata_locally(st
, u
, len
);
10005 /* and possibly remotely */
10006 if (st
->update_tail
)
10007 append_metadata_update(st
, u
, len
);
10017 exit_imsm_reshape_super
:
10018 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
10022 /*******************************************************************************
10023 * Function: wait_for_reshape_imsm
10024 * Description: Function writes new sync_max value and waits until
10025 * reshape process reach new position
10027 * sra : general array info
10028 * ndata : number of disks in new array's layout
10031 * 1 : there is no reshape in progress,
10033 ******************************************************************************/
10034 int wait_for_reshape_imsm(struct mdinfo
*sra
, int ndata
)
10036 int fd
= sysfs_get_fd(sra
, NULL
, "reshape_position");
10037 unsigned long long completed
;
10038 /* to_complete : new sync_max position */
10039 unsigned long long to_complete
= sra
->reshape_progress
;
10040 unsigned long long position_to_set
= to_complete
/ ndata
;
10043 dprintf("imsm: wait_for_reshape_imsm() "
10044 "cannot open reshape_position\n");
10048 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
10049 dprintf("imsm: wait_for_reshape_imsm() "
10050 "cannot read reshape_position (no reshape in progres)\n");
10055 if (completed
> to_complete
) {
10056 dprintf("imsm: wait_for_reshape_imsm() "
10057 "wrong next position to set %llu (%llu)\n",
10058 to_complete
, completed
);
10062 dprintf("Position set: %llu\n", position_to_set
);
10063 if (sysfs_set_num(sra
, NULL
, "sync_max",
10064 position_to_set
) != 0) {
10065 dprintf("imsm: wait_for_reshape_imsm() "
10066 "cannot set reshape position to %llu\n",
10077 select(fd
+1, &rfds
, NULL
, NULL
, NULL
);
10078 if (sysfs_get_str(sra
, NULL
, "sync_action",
10080 strncmp(action
, "reshape", 7) != 0)
10082 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
10083 dprintf("imsm: wait_for_reshape_imsm() "
10084 "cannot read reshape_position (in loop)\n");
10088 } while (completed
< to_complete
);
10094 /*******************************************************************************
10095 * Function: check_degradation_change
10096 * Description: Check that array hasn't become failed.
10098 * info : for sysfs access
10099 * sources : source disks descriptors
10100 * degraded: previous degradation level
10102 * degradation level
10103 ******************************************************************************/
10104 int check_degradation_change(struct mdinfo
*info
,
10108 unsigned long long new_degraded
;
10111 rv
= sysfs_get_ll(info
, NULL
, "degraded", &new_degraded
);
10112 if ((rv
== -1) || (new_degraded
!= (unsigned long long)degraded
)) {
10113 /* check each device to ensure it is still working */
10116 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
10117 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
10119 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
10121 if (sysfs_get_str(info
,
10122 sd
, "state", sbuf
, 20) < 0 ||
10123 strstr(sbuf
, "faulty") ||
10124 strstr(sbuf
, "in_sync") == NULL
) {
10125 /* this device is dead */
10126 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
10127 if (sd
->disk
.raid_disk
>= 0 &&
10128 sources
[sd
->disk
.raid_disk
] >= 0) {
10130 sd
->disk
.raid_disk
]);
10131 sources
[sd
->disk
.raid_disk
] =
10140 return new_degraded
;
10143 /*******************************************************************************
10144 * Function: imsm_manage_reshape
10145 * Description: Function finds array under reshape and it manages reshape
10146 * process. It creates stripes backups (if required) and sets
10149 * afd : Backup handle (nattive) - not used
10150 * sra : general array info
10151 * reshape : reshape parameters - not used
10152 * st : supertype structure
10153 * blocks : size of critical section [blocks]
10154 * fds : table of source device descriptor
10155 * offsets : start of array (offest per devices)
10157 * destfd : table of destination device descriptor
10158 * destoffsets : table of destination offsets (per device)
10160 * 1 : success, reshape is done
10162 ******************************************************************************/
10163 static int imsm_manage_reshape(
10164 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
10165 struct supertype
*st
, unsigned long backup_blocks
,
10166 int *fds
, unsigned long long *offsets
,
10167 int dests
, int *destfd
, unsigned long long *destoffsets
)
10170 struct intel_super
*super
= st
->sb
;
10171 struct intel_dev
*dv
= NULL
;
10172 struct imsm_dev
*dev
= NULL
;
10173 struct imsm_map
*map_src
;
10174 int migr_vol_qan
= 0;
10175 int ndata
, odata
; /* [bytes] */
10176 int chunk
; /* [bytes] */
10177 struct migr_record
*migr_rec
;
10179 unsigned int buf_size
; /* [bytes] */
10180 unsigned long long max_position
; /* array size [bytes] */
10181 unsigned long long next_step
; /* [blocks]/[bytes] */
10182 unsigned long long old_data_stripe_length
;
10183 unsigned long long start_src
; /* [bytes] */
10184 unsigned long long start
; /* [bytes] */
10185 unsigned long long start_buf_shift
; /* [bytes] */
10187 int source_layout
= 0;
10189 if (!fds
|| !offsets
|| !sra
)
10192 /* Find volume during the reshape */
10193 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
10194 if (dv
->dev
->vol
.migr_type
== MIGR_GEN_MIGR
10195 && dv
->dev
->vol
.migr_state
== 1) {
10200 /* Only one volume can migrate at the same time */
10201 if (migr_vol_qan
!= 1) {
10202 pr_err(": %s", migr_vol_qan
?
10203 "Number of migrating volumes greater than 1\n" :
10204 "There is no volume during migrationg\n");
10208 map_src
= get_imsm_map(dev
, MAP_1
);
10209 if (map_src
== NULL
)
10212 ndata
= imsm_num_data_members(dev
, MAP_0
);
10213 odata
= imsm_num_data_members(dev
, MAP_1
);
10215 chunk
= __le16_to_cpu(map_src
->blocks_per_strip
) * 512;
10216 old_data_stripe_length
= odata
* chunk
;
10218 migr_rec
= super
->migr_rec
;
10220 /* initialize migration record for start condition */
10221 if (sra
->reshape_progress
== 0)
10222 init_migr_record_imsm(st
, dev
, sra
);
10224 if (__le32_to_cpu(migr_rec
->rec_status
) != UNIT_SRC_NORMAL
) {
10225 dprintf("imsm: cannot restart migration when data "
10226 "are present in copy area.\n");
10229 /* Save checkpoint to update migration record for current
10230 * reshape position (in md). It can be farther than current
10231 * reshape position in metadata.
10233 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
10234 /* ignore error == 2, this can mean end of reshape here
10236 dprintf("imsm: Cannot write checkpoint to "
10237 "migration record (UNIT_SRC_NORMAL, "
10238 "initial save)\n");
10243 /* size for data */
10244 buf_size
= __le32_to_cpu(migr_rec
->blocks_per_unit
) * 512;
10245 /* extend buffer size for parity disk */
10246 buf_size
+= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
10247 /* add space for stripe aligment */
10248 buf_size
+= old_data_stripe_length
;
10249 if (posix_memalign((void **)&buf
, 4096, buf_size
)) {
10250 dprintf("imsm: Cannot allocate checpoint buffer\n");
10254 max_position
= sra
->component_size
* ndata
;
10255 source_layout
= imsm_level_to_layout(map_src
->raid_level
);
10257 while (__le32_to_cpu(migr_rec
->curr_migr_unit
) <
10258 __le32_to_cpu(migr_rec
->num_migr_units
)) {
10259 /* current reshape position [blocks] */
10260 unsigned long long current_position
=
10261 __le32_to_cpu(migr_rec
->blocks_per_unit
)
10262 * __le32_to_cpu(migr_rec
->curr_migr_unit
);
10263 unsigned long long border
;
10265 /* Check that array hasn't become failed.
10267 degraded
= check_degradation_change(sra
, fds
, degraded
);
10268 if (degraded
> 1) {
10269 dprintf("imsm: Abort reshape due to degradation"
10270 " level (%i)\n", degraded
);
10274 next_step
= __le32_to_cpu(migr_rec
->blocks_per_unit
);
10276 if ((current_position
+ next_step
) > max_position
)
10277 next_step
= max_position
- current_position
;
10279 start
= current_position
* 512;
10281 /* allign reading start to old geometry */
10282 start_buf_shift
= start
% old_data_stripe_length
;
10283 start_src
= start
- start_buf_shift
;
10285 border
= (start_src
/ odata
) - (start
/ ndata
);
10287 if (border
<= __le32_to_cpu(migr_rec
->dest_depth_per_unit
)) {
10288 /* save critical stripes to buf
10289 * start - start address of current unit
10290 * to backup [bytes]
10291 * start_src - start address of current unit
10292 * to backup alligned to source array
10295 unsigned long long next_step_filler
= 0;
10296 unsigned long long copy_length
= next_step
* 512;
10298 /* allign copy area length to stripe in old geometry */
10299 next_step_filler
= ((copy_length
+ start_buf_shift
)
10300 % old_data_stripe_length
);
10301 if (next_step_filler
)
10302 next_step_filler
= (old_data_stripe_length
10303 - next_step_filler
);
10304 dprintf("save_stripes() parameters: start = %llu,"
10305 "\tstart_src = %llu,\tnext_step*512 = %llu,"
10306 "\tstart_in_buf_shift = %llu,"
10307 "\tnext_step_filler = %llu\n",
10308 start
, start_src
, copy_length
,
10309 start_buf_shift
, next_step_filler
);
10311 if (save_stripes(fds
, offsets
, map_src
->num_members
,
10312 chunk
, map_src
->raid_level
,
10313 source_layout
, 0, NULL
, start_src
,
10315 next_step_filler
+ start_buf_shift
,
10317 dprintf("imsm: Cannot save stripes"
10321 /* Convert data to destination format and store it
10322 * in backup general migration area
10324 if (save_backup_imsm(st
, dev
, sra
,
10325 buf
+ start_buf_shift
, copy_length
)) {
10326 dprintf("imsm: Cannot save stripes to "
10327 "target devices\n");
10330 if (save_checkpoint_imsm(st
, sra
,
10331 UNIT_SRC_IN_CP_AREA
)) {
10332 dprintf("imsm: Cannot write checkpoint to "
10333 "migration record (UNIT_SRC_IN_CP_AREA)\n");
10337 /* set next step to use whole border area */
10338 border
/= next_step
;
10340 next_step
*= border
;
10342 /* When data backed up, checkpoint stored,
10343 * kick the kernel to reshape unit of data
10345 next_step
= next_step
+ sra
->reshape_progress
;
10346 /* limit next step to array max position */
10347 if (next_step
> max_position
)
10348 next_step
= max_position
;
10349 sysfs_set_num(sra
, NULL
, "suspend_lo", sra
->reshape_progress
);
10350 sysfs_set_num(sra
, NULL
, "suspend_hi", next_step
);
10351 sra
->reshape_progress
= next_step
;
10353 /* wait until reshape finish */
10354 if (wait_for_reshape_imsm(sra
, ndata
) < 0) {
10355 dprintf("wait_for_reshape_imsm returned error!\n");
10359 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
10360 /* ignore error == 2, this can mean end of reshape here
10362 dprintf("imsm: Cannot write checkpoint to "
10363 "migration record (UNIT_SRC_NORMAL)\n");
10369 /* return '1' if done */
10373 abort_reshape(sra
);
10377 #endif /* MDASSEMBLE */
10379 struct superswitch super_imsm
= {
10381 .examine_super
= examine_super_imsm
,
10382 .brief_examine_super
= brief_examine_super_imsm
,
10383 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
10384 .export_examine_super
= export_examine_super_imsm
,
10385 .detail_super
= detail_super_imsm
,
10386 .brief_detail_super
= brief_detail_super_imsm
,
10387 .write_init_super
= write_init_super_imsm
,
10388 .validate_geometry
= validate_geometry_imsm
,
10389 .add_to_super
= add_to_super_imsm
,
10390 .remove_from_super
= remove_from_super_imsm
,
10391 .detail_platform
= detail_platform_imsm
,
10392 .kill_subarray
= kill_subarray_imsm
,
10393 .update_subarray
= update_subarray_imsm
,
10394 .load_container
= load_container_imsm
,
10395 .default_geometry
= default_geometry_imsm
,
10396 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
10397 .reshape_super
= imsm_reshape_super
,
10398 .manage_reshape
= imsm_manage_reshape
,
10399 .recover_backup
= recover_backup_imsm
,
10401 .match_home
= match_home_imsm
,
10402 .uuid_from_super
= uuid_from_super_imsm
,
10403 .getinfo_super
= getinfo_super_imsm
,
10404 .getinfo_super_disks
= getinfo_super_disks_imsm
,
10405 .update_super
= update_super_imsm
,
10407 .avail_size
= avail_size_imsm
,
10408 .min_acceptable_spare_size
= min_acceptable_spare_size_imsm
,
10410 .compare_super
= compare_super_imsm
,
10412 .load_super
= load_super_imsm
,
10413 .init_super
= init_super_imsm
,
10414 .store_super
= store_super_imsm
,
10415 .free_super
= free_super_imsm
,
10416 .match_metadata_desc
= match_metadata_desc_imsm
,
10417 .container_content
= container_content_imsm
,
10425 .open_new
= imsm_open_new
,
10426 .set_array_state
= imsm_set_array_state
,
10427 .set_disk
= imsm_set_disk
,
10428 .sync_metadata
= imsm_sync_metadata
,
10429 .activate_spare
= imsm_activate_spare
,
10430 .process_update
= imsm_process_update
,
10431 .prepare_update
= imsm_prepare_update
,
10432 #endif /* MDASSEMBLE */