2 * mdadm - Intel(R) Matrix Storage Manager Support
4 * Copyright (C) 2002-2008 Intel Corporation
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20 #define HAVE_STDINT_H 1
24 #include "platform-intel.h"
30 /* MPB == Metadata Parameter Block */
31 #define MPB_SIGNATURE "Intel Raid ISM Cfg Sig. "
32 #define MPB_SIG_LEN (strlen(MPB_SIGNATURE))
33 #define MPB_VERSION_RAID0 "1.0.00"
34 #define MPB_VERSION_RAID1 "1.1.00"
35 #define MPB_VERSION_MANY_VOLUMES_PER_ARRAY "1.2.00"
36 #define MPB_VERSION_3OR4_DISK_ARRAY "1.2.01"
37 #define MPB_VERSION_RAID5 "1.2.02"
38 #define MPB_VERSION_5OR6_DISK_ARRAY "1.2.04"
39 #define MPB_VERSION_CNG "1.2.06"
40 #define MPB_VERSION_ATTRIBS "1.3.00"
41 #define MAX_SIGNATURE_LENGTH 32
42 #define MAX_RAID_SERIAL_LEN 16
45 #define MPB_ATTRIB_RAID0 __cpu_to_le32(0x00000001)
47 #define MPB_ATTRIB_RAID1 __cpu_to_le32(0x00000002)
49 #define MPB_ATTRIB_RAID10 __cpu_to_le32(0x00000004)
51 #define MPB_ATTRIB_RAID1E __cpu_to_le32(0x00000008)
53 #define MPB_ATTRIB_RAID5 __cpu_to_le32(0x00000010)
54 /* supports RAID CNG */
55 #define MPB_ATTRIB_RAIDCNG __cpu_to_le32(0x00000020)
56 /* supports expanded stripe sizes of 256K, 512K and 1MB */
57 #define MPB_ATTRIB_EXP_STRIPE_SIZE __cpu_to_le32(0x00000040)
59 /* The OROM Support RST Caching of Volumes */
60 #define MPB_ATTRIB_NVM __cpu_to_le32(0x02000000)
61 /* The OROM supports creating disks greater than 2TB */
62 #define MPB_ATTRIB_2TB_DISK __cpu_to_le32(0x04000000)
63 /* The OROM supports Bad Block Management */
64 #define MPB_ATTRIB_BBM __cpu_to_le32(0x08000000)
66 /* THe OROM Supports NVM Caching of Volumes */
67 #define MPB_ATTRIB_NEVER_USE2 __cpu_to_le32(0x10000000)
68 /* The OROM supports creating volumes greater than 2TB */
69 #define MPB_ATTRIB_2TB __cpu_to_le32(0x20000000)
70 /* originally for PMP, now it's wasted b/c. Never use this bit! */
71 #define MPB_ATTRIB_NEVER_USE __cpu_to_le32(0x40000000)
72 /* Verify MPB contents against checksum after reading MPB */
73 #define MPB_ATTRIB_CHECKSUM_VERIFY __cpu_to_le32(0x80000000)
75 /* Define all supported attributes that have to be accepted by mdadm
77 #define MPB_ATTRIB_SUPPORTED (MPB_ATTRIB_CHECKSUM_VERIFY | \
79 MPB_ATTRIB_2TB_DISK | \
84 MPB_ATTRIB_EXP_STRIPE_SIZE)
86 /* Define attributes that are unused but not harmful */
87 #define MPB_ATTRIB_IGNORED (MPB_ATTRIB_NEVER_USE)
89 #define MPB_SECTOR_CNT 2210
90 #define IMSM_RESERVED_SECTORS 4096
91 #define NUM_BLOCKS_DIRTY_STRIPE_REGION 2056
92 #define SECT_PER_MB_SHIFT 11
94 /* Disk configuration info. */
95 #define IMSM_MAX_DEVICES 255
97 __u8 serial
[MAX_RAID_SERIAL_LEN
];/* 0xD8 - 0xE7 ascii serial number */
98 __u32 total_blocks_lo
; /* 0xE8 - 0xEB total blocks lo */
99 __u32 scsi_id
; /* 0xEC - 0xEF scsi ID */
100 #define SPARE_DISK __cpu_to_le32(0x01) /* Spare */
101 #define CONFIGURED_DISK __cpu_to_le32(0x02) /* Member of some RaidDev */
102 #define FAILED_DISK __cpu_to_le32(0x04) /* Permanent failure */
103 __u32 status
; /* 0xF0 - 0xF3 */
104 __u32 owner_cfg_num
; /* which config 0,1,2... owns this disk */
105 __u32 total_blocks_hi
; /* 0xF4 - 0xF5 total blocks hi */
106 #define IMSM_DISK_FILLERS 3
107 __u32 filler
[IMSM_DISK_FILLERS
]; /* 0xF5 - 0x107 MPB_DISK_FILLERS for future expansion */
110 /* map selector for map managment
116 /* RAID map configuration infos. */
118 __u32 pba_of_lba0_lo
; /* start address of partition */
119 __u32 blocks_per_member_lo
;/* blocks per member */
120 __u32 num_data_stripes_lo
; /* number of data stripes */
121 __u16 blocks_per_strip
;
122 __u8 map_state
; /* Normal, Uninitialized, Degraded, Failed */
123 #define IMSM_T_STATE_NORMAL 0
124 #define IMSM_T_STATE_UNINITIALIZED 1
125 #define IMSM_T_STATE_DEGRADED 2
126 #define IMSM_T_STATE_FAILED 3
128 #define IMSM_T_RAID0 0
129 #define IMSM_T_RAID1 1
130 #define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */
131 __u8 num_members
; /* number of member disks */
132 __u8 num_domains
; /* number of parity domains */
133 __u8 failed_disk_num
; /* valid only when state is degraded */
135 __u32 pba_of_lba0_hi
;
136 __u32 blocks_per_member_hi
;
137 __u32 num_data_stripes_hi
;
138 __u32 filler
[4]; /* expansion area */
139 #define IMSM_ORD_REBUILD (1 << 24)
140 __u32 disk_ord_tbl
[1]; /* disk_ord_tbl[num_members],
141 * top byte contains some flags
143 } __attribute__ ((packed
));
146 __u32 curr_migr_unit
;
147 __u32 checkpoint_id
; /* id to access curr_migr_unit */
148 __u8 migr_state
; /* Normal or Migrating */
150 #define MIGR_REBUILD 1
151 #define MIGR_VERIFY 2 /* analagous to echo check > sync_action */
152 #define MIGR_GEN_MIGR 3
153 #define MIGR_STATE_CHANGE 4
154 #define MIGR_REPAIR 5
155 __u8 migr_type
; /* Initializing, Rebuilding, ... */
157 __u8 fs_state
; /* fast-sync state for CnG (0xff == disabled) */
158 __u16 verify_errors
; /* number of mismatches */
159 __u16 bad_blocks
; /* number of bad blocks during verify */
161 struct imsm_map map
[1];
162 /* here comes another one if migr_state */
163 } __attribute__ ((packed
));
166 __u8 volume
[MAX_RAID_SERIAL_LEN
];
169 #define DEV_BOOTABLE __cpu_to_le32(0x01)
170 #define DEV_BOOT_DEVICE __cpu_to_le32(0x02)
171 #define DEV_READ_COALESCING __cpu_to_le32(0x04)
172 #define DEV_WRITE_COALESCING __cpu_to_le32(0x08)
173 #define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10)
174 #define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20)
175 #define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40)
176 #define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80)
177 #define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100)
178 #define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200)
179 #define DEV_CLONE_N_GO __cpu_to_le32(0x400)
180 #define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800)
181 #define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000)
182 __u32 status
; /* Persistent RaidDev status */
183 __u32 reserved_blocks
; /* Reserved blocks at beginning of volume */
187 __u8 cng_master_disk
;
191 #define IMSM_DEV_FILLERS 10
192 __u32 filler
[IMSM_DEV_FILLERS
];
194 } __attribute__ ((packed
));
197 __u8 sig
[MAX_SIGNATURE_LENGTH
]; /* 0x00 - 0x1F */
198 __u32 check_sum
; /* 0x20 - 0x23 MPB Checksum */
199 __u32 mpb_size
; /* 0x24 - 0x27 Size of MPB */
200 __u32 family_num
; /* 0x28 - 0x2B Checksum from first time this config was written */
201 __u32 generation_num
; /* 0x2C - 0x2F Incremented each time this array's MPB is written */
202 __u32 error_log_size
; /* 0x30 - 0x33 in bytes */
203 __u32 attributes
; /* 0x34 - 0x37 */
204 __u8 num_disks
; /* 0x38 Number of configured disks */
205 __u8 num_raid_devs
; /* 0x39 Number of configured volumes */
206 __u8 error_log_pos
; /* 0x3A */
207 __u8 fill
[1]; /* 0x3B */
208 __u32 cache_size
; /* 0x3c - 0x40 in mb */
209 __u32 orig_family_num
; /* 0x40 - 0x43 original family num */
210 __u32 pwr_cycle_count
; /* 0x44 - 0x47 simulated power cycle count for array */
211 __u32 bbm_log_size
; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */
212 #define IMSM_FILLERS 35
213 __u32 filler
[IMSM_FILLERS
]; /* 0x4C - 0xD7 RAID_MPB_FILLERS */
214 struct imsm_disk disk
[1]; /* 0xD8 diskTbl[numDisks] */
215 /* here comes imsm_dev[num_raid_devs] */
216 /* here comes BBM logs */
217 } __attribute__ ((packed
));
219 #define BBM_LOG_MAX_ENTRIES 254
221 struct bbm_log_entry
{
222 __u64 defective_block_start
;
223 #define UNREADABLE 0xFFFFFFFF
224 __u32 spare_block_offset
;
225 __u16 remapped_marked_count
;
227 } __attribute__ ((__packed__
));
230 __u32 signature
; /* 0xABADB10C */
232 __u32 reserved_spare_block_count
; /* 0 */
233 __u32 reserved
; /* 0xFFFF */
234 __u64 first_spare_lba
;
235 struct bbm_log_entry mapped_block_entries
[BBM_LOG_MAX_ENTRIES
];
236 } __attribute__ ((__packed__
));
240 static char *map_state_str
[] = { "normal", "uninitialized", "degraded", "failed" };
243 #define RAID_DISK_RESERVED_BLOCKS_IMSM_HI 2209
245 #define GEN_MIGR_AREA_SIZE 2048 /* General Migration Copy Area size in blocks */
247 #define MIGR_REC_BUF_SIZE 512 /* size of migr_record i/o buffer */
248 #define MIGR_REC_POSITION 512 /* migr_record position offset on disk,
249 * MIGR_REC_BUF_SIZE <= MIGR_REC_POSITION
253 #define UNIT_SRC_NORMAL 0 /* Source data for curr_migr_unit must
254 * be recovered using srcMap */
255 #define UNIT_SRC_IN_CP_AREA 1 /* Source data for curr_migr_unit has
256 * already been migrated and must
257 * be recovered from checkpoint area */
259 __u32 rec_status
; /* Status used to determine how to restart
260 * migration in case it aborts
262 __u32 curr_migr_unit
; /* 0..numMigrUnits-1 */
263 __u32 family_num
; /* Family number of MPB
264 * containing the RaidDev
265 * that is migrating */
266 __u32 ascending_migr
; /* True if migrating in increasing
268 __u32 blocks_per_unit
; /* Num disk blocks per unit of operation */
269 __u32 dest_depth_per_unit
; /* Num member blocks each destMap
271 * advances per unit-of-operation */
272 __u32 ckpt_area_pba
; /* Pba of first block of ckpt copy area */
273 __u32 dest_1st_member_lba
; /* First member lba on first
274 * stripe of destination */
275 __u32 num_migr_units
; /* Total num migration units-of-op */
276 __u32 post_migr_vol_cap
; /* Size of volume after
277 * migration completes */
278 __u32 post_migr_vol_cap_hi
; /* Expansion space for LBA64 */
279 __u32 ckpt_read_disk_num
; /* Which member disk in destSubMap[0] the
280 * migration ckpt record was read from
281 * (for recovered migrations) */
282 } __attribute__ ((__packed__
));
287 * 2: metadata does not match
295 struct md_list
*next
;
298 #define pr_vrb(fmt, arg...) (void) (verbose && pr_err(fmt, ##arg))
300 static __u8
migr_type(struct imsm_dev
*dev
)
302 if (dev
->vol
.migr_type
== MIGR_VERIFY
&&
303 dev
->status
& DEV_VERIFY_AND_FIX
)
306 return dev
->vol
.migr_type
;
309 static void set_migr_type(struct imsm_dev
*dev
, __u8 migr_type
)
311 /* for compatibility with older oroms convert MIGR_REPAIR, into
312 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
314 if (migr_type
== MIGR_REPAIR
) {
315 dev
->vol
.migr_type
= MIGR_VERIFY
;
316 dev
->status
|= DEV_VERIFY_AND_FIX
;
318 dev
->vol
.migr_type
= migr_type
;
319 dev
->status
&= ~DEV_VERIFY_AND_FIX
;
323 static unsigned int sector_count(__u32 bytes
)
325 return ROUND_UP(bytes
, 512) / 512;
328 static unsigned int mpb_sectors(struct imsm_super
*mpb
)
330 return sector_count(__le32_to_cpu(mpb
->mpb_size
));
334 struct imsm_dev
*dev
;
335 struct intel_dev
*next
;
340 enum sys_dev_type type
;
343 struct intel_hba
*next
;
350 /* internal representation of IMSM metadata */
353 void *buf
; /* O_DIRECT buffer for reading/writing metadata */
354 struct imsm_super
*anchor
; /* immovable parameters */
357 void *migr_rec_buf
; /* buffer for I/O operations */
358 struct migr_record
*migr_rec
; /* migration record */
360 int clean_migration_record_by_mdmon
; /* when reshape is switched to next
361 array, it indicates that mdmon is allowed to clean migration
363 size_t len
; /* size of the 'buf' allocation */
364 void *next_buf
; /* for realloc'ing buf from the manager */
366 int updates_pending
; /* count of pending updates for mdmon */
367 int current_vol
; /* index of raid device undergoing creation */
368 unsigned long long create_offset
; /* common start for 'current_vol' */
369 __u32 random
; /* random data for seeding new family numbers */
370 struct intel_dev
*devlist
;
374 __u8 serial
[MAX_RAID_SERIAL_LEN
];
377 struct imsm_disk disk
;
380 struct extent
*e
; /* for determining freespace @ create */
381 int raiddisk
; /* slot to fill in autolayout */
383 } *disks
, *current_disk
;
384 struct dl
*disk_mgmt_list
; /* list of disks to add/remove while mdmon
386 struct dl
*missing
; /* disks removed while we weren't looking */
387 struct bbm_log
*bbm_log
;
388 struct intel_hba
*hba
; /* device path of the raid controller for this metadata */
389 const struct imsm_orom
*orom
; /* platform firmware support */
390 struct intel_super
*next
; /* (temp) list for disambiguating family_num */
394 struct imsm_disk disk
;
395 #define IMSM_UNKNOWN_OWNER (-1)
397 struct intel_disk
*next
;
401 unsigned long long start
, size
;
404 /* definitions of reshape process types */
405 enum imsm_reshape_type
{
411 /* definition of messages passed to imsm_process_update */
412 enum imsm_update_type
{
413 update_activate_spare
,
417 update_add_remove_disk
,
418 update_reshape_container_disks
,
419 update_reshape_migration
,
421 update_general_migration_checkpoint
,
425 struct imsm_update_activate_spare
{
426 enum imsm_update_type type
;
430 struct imsm_update_activate_spare
*next
;
436 unsigned long long size
;
443 enum takeover_direction
{
447 struct imsm_update_takeover
{
448 enum imsm_update_type type
;
450 enum takeover_direction direction
;
453 struct imsm_update_reshape
{
454 enum imsm_update_type type
;
458 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
461 struct imsm_update_reshape_migration
{
462 enum imsm_update_type type
;
465 /* fields for array migration changes
472 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
475 struct imsm_update_size_change
{
476 enum imsm_update_type type
;
481 struct imsm_update_general_migration_checkpoint
{
482 enum imsm_update_type type
;
483 __u32 curr_migr_unit
;
487 __u8 serial
[MAX_RAID_SERIAL_LEN
];
490 struct imsm_update_create_array
{
491 enum imsm_update_type type
;
496 struct imsm_update_kill_array
{
497 enum imsm_update_type type
;
501 struct imsm_update_rename_array
{
502 enum imsm_update_type type
;
503 __u8 name
[MAX_RAID_SERIAL_LEN
];
507 struct imsm_update_add_remove_disk
{
508 enum imsm_update_type type
;
512 static const char *_sys_dev_type
[] = {
513 [SYS_DEV_UNKNOWN
] = "Unknown",
514 [SYS_DEV_SAS
] = "SAS",
515 [SYS_DEV_SATA
] = "SATA"
518 const char *get_sys_dev_type(enum sys_dev_type type
)
520 if (type
>= SYS_DEV_MAX
)
521 type
= SYS_DEV_UNKNOWN
;
523 return _sys_dev_type
[type
];
526 static struct intel_hba
* alloc_intel_hba(struct sys_dev
*device
)
528 struct intel_hba
*result
= xmalloc(sizeof(*result
));
530 result
->type
= device
->type
;
531 result
->path
= xstrdup(device
->path
);
533 if (result
->path
&& (result
->pci_id
= strrchr(result
->path
, '/')) != NULL
)
539 static struct intel_hba
* find_intel_hba(struct intel_hba
*hba
, struct sys_dev
*device
)
541 struct intel_hba
*result
=NULL
;
542 for (result
= hba
; result
; result
= result
->next
) {
543 if (result
->type
== device
->type
&& strcmp(result
->path
, device
->path
) == 0)
549 static int attach_hba_to_super(struct intel_super
*super
, struct sys_dev
*device
)
551 struct intel_hba
*hba
;
553 /* check if disk attached to Intel HBA */
554 hba
= find_intel_hba(super
->hba
, device
);
557 /* Check if HBA is already attached to super */
558 if (super
->hba
== NULL
) {
559 super
->hba
= alloc_intel_hba(device
);
562 /* IMSM metadata disallows to attach disks to multiple
568 static struct sys_dev
* find_disk_attached_hba(int fd
, const char *devname
)
570 struct sys_dev
*list
, *elem
;
573 if ((list
= find_intel_devices()) == NULL
)
577 disk_path
= (char *) devname
;
579 disk_path
= diskfd_to_devpath(fd
);
584 for (elem
= list
; elem
; elem
= elem
->next
)
585 if (path_attached_to_hba(disk_path
, elem
->path
))
588 if (disk_path
!= devname
)
595 static int find_intel_hba_capability(int fd
, struct intel_super
*super
,
598 static struct supertype
*match_metadata_desc_imsm(char *arg
)
600 struct supertype
*st
;
602 if (strcmp(arg
, "imsm") != 0 &&
603 strcmp(arg
, "default") != 0
607 st
= xcalloc(1, sizeof(*st
));
608 st
->ss
= &super_imsm
;
609 st
->max_devs
= IMSM_MAX_DEVICES
;
610 st
->minor_version
= 0;
616 static __u8
*get_imsm_version(struct imsm_super
*mpb
)
618 return &mpb
->sig
[MPB_SIG_LEN
];
622 /* retrieve a disk directly from the anchor when the anchor is known to be
623 * up-to-date, currently only at load time
625 static struct imsm_disk
*__get_imsm_disk(struct imsm_super
*mpb
, __u8 index
)
627 if (index
>= mpb
->num_disks
)
629 return &mpb
->disk
[index
];
632 /* retrieve the disk description based on a index of the disk
635 static struct dl
*get_imsm_dl_disk(struct intel_super
*super
, __u8 index
)
639 for (d
= super
->disks
; d
; d
= d
->next
)
640 if (d
->index
== index
)
645 /* retrieve a disk from the parsed metadata */
646 static struct imsm_disk
*get_imsm_disk(struct intel_super
*super
, __u8 index
)
650 dl
= get_imsm_dl_disk(super
, index
);
657 /* generate a checksum directly from the anchor when the anchor is known to be
658 * up-to-date, currently only at load or write_super after coalescing
660 static __u32
__gen_imsm_checksum(struct imsm_super
*mpb
)
662 __u32 end
= mpb
->mpb_size
/ sizeof(end
);
663 __u32
*p
= (__u32
*) mpb
;
667 sum
+= __le32_to_cpu(*p
);
671 return sum
- __le32_to_cpu(mpb
->check_sum
);
674 static size_t sizeof_imsm_map(struct imsm_map
*map
)
676 return sizeof(struct imsm_map
) + sizeof(__u32
) * (map
->num_members
- 1);
679 struct imsm_map
*get_imsm_map(struct imsm_dev
*dev
, int second_map
)
681 /* A device can have 2 maps if it is in the middle of a migration.
683 * MAP_0 - we return the first map
684 * MAP_1 - we return the second map if it exists, else NULL
685 * MAP_X - we return the second map if it exists, else the first
687 struct imsm_map
*map
= &dev
->vol
.map
[0];
688 struct imsm_map
*map2
= NULL
;
690 if (dev
->vol
.migr_state
)
691 map2
= (void *)map
+ sizeof_imsm_map(map
);
693 switch (second_map
) {
710 /* return the size of the device.
711 * migr_state increases the returned size if map[0] were to be duplicated
713 static size_t sizeof_imsm_dev(struct imsm_dev
*dev
, int migr_state
)
715 size_t size
= sizeof(*dev
) - sizeof(struct imsm_map
) +
716 sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
718 /* migrating means an additional map */
719 if (dev
->vol
.migr_state
)
720 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_1
));
722 size
+= sizeof_imsm_map(get_imsm_map(dev
, MAP_0
));
728 /* retrieve disk serial number list from a metadata update */
729 static struct disk_info
*get_disk_info(struct imsm_update_create_array
*update
)
732 struct disk_info
*inf
;
734 inf
= u
+ sizeof(*update
) - sizeof(struct imsm_dev
) +
735 sizeof_imsm_dev(&update
->dev
, 0);
741 static struct imsm_dev
*__get_imsm_dev(struct imsm_super
*mpb
, __u8 index
)
747 if (index
>= mpb
->num_raid_devs
)
750 /* devices start after all disks */
751 offset
= ((void *) &mpb
->disk
[mpb
->num_disks
]) - _mpb
;
753 for (i
= 0; i
<= index
; i
++)
755 return _mpb
+ offset
;
757 offset
+= sizeof_imsm_dev(_mpb
+ offset
, 0);
762 static struct imsm_dev
*get_imsm_dev(struct intel_super
*super
, __u8 index
)
764 struct intel_dev
*dv
;
766 if (index
>= super
->anchor
->num_raid_devs
)
768 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
769 if (dv
->index
== index
)
776 * == MAP_0 get first map
777 * == MAP_1 get second map
778 * == MAP_X than get map according to the current migr_state
780 static __u32
get_imsm_ord_tbl_ent(struct imsm_dev
*dev
,
784 struct imsm_map
*map
;
786 map
= get_imsm_map(dev
, second_map
);
788 /* top byte identifies disk under rebuild */
789 return __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
792 #define ord_to_idx(ord) (((ord) << 8) >> 8)
793 static __u32
get_imsm_disk_idx(struct imsm_dev
*dev
, int slot
, int second_map
)
795 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, second_map
);
797 return ord_to_idx(ord
);
800 static void set_imsm_ord_tbl_ent(struct imsm_map
*map
, int slot
, __u32 ord
)
802 map
->disk_ord_tbl
[slot
] = __cpu_to_le32(ord
);
805 static int get_imsm_disk_slot(struct imsm_map
*map
, unsigned idx
)
810 for (slot
= 0; slot
< map
->num_members
; slot
++) {
811 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
812 if (ord_to_idx(ord
) == idx
)
819 static int get_imsm_raid_level(struct imsm_map
*map
)
821 if (map
->raid_level
== 1) {
822 if (map
->num_members
== 2)
828 return map
->raid_level
;
831 static int cmp_extent(const void *av
, const void *bv
)
833 const struct extent
*a
= av
;
834 const struct extent
*b
= bv
;
835 if (a
->start
< b
->start
)
837 if (a
->start
> b
->start
)
842 static int count_memberships(struct dl
*dl
, struct intel_super
*super
)
847 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
848 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
849 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
851 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
858 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
);
860 static int split_ull(unsigned long long n
, __u32
*lo
, __u32
*hi
)
862 if (lo
== 0 || hi
== 0)
864 *lo
= __le32_to_cpu((unsigned)n
);
865 *hi
= __le32_to_cpu((unsigned)(n
>> 32));
869 static unsigned long long join_u32(__u32 lo
, __u32 hi
)
871 return (unsigned long long)__le32_to_cpu(lo
) |
872 (((unsigned long long)__le32_to_cpu(hi
)) << 32);
875 static unsigned long long total_blocks(struct imsm_disk
*disk
)
879 return join_u32(disk
->total_blocks_lo
, disk
->total_blocks_hi
);
882 static unsigned long long pba_of_lba0(struct imsm_map
*map
)
886 return join_u32(map
->pba_of_lba0_lo
, map
->pba_of_lba0_hi
);
889 static unsigned long long blocks_per_member(struct imsm_map
*map
)
893 return join_u32(map
->blocks_per_member_lo
, map
->blocks_per_member_hi
);
897 static unsigned long long num_data_stripes(struct imsm_map
*map
)
901 return join_u32(map
->num_data_stripes_lo
, map
->num_data_stripes_hi
);
904 static void set_total_blocks(struct imsm_disk
*disk
, unsigned long long n
)
906 split_ull(n
, &disk
->total_blocks_lo
, &disk
->total_blocks_hi
);
910 static void set_pba_of_lba0(struct imsm_map
*map
, unsigned long long n
)
912 split_ull(n
, &map
->pba_of_lba0_lo
, &map
->pba_of_lba0_hi
);
915 static void set_blocks_per_member(struct imsm_map
*map
, unsigned long long n
)
917 split_ull(n
, &map
->blocks_per_member_lo
, &map
->blocks_per_member_hi
);
920 static void set_num_data_stripes(struct imsm_map
*map
, unsigned long long n
)
922 split_ull(n
, &map
->num_data_stripes_lo
, &map
->num_data_stripes_hi
);
925 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
)
927 /* find a list of used extents on the given physical device */
928 struct extent
*rv
, *e
;
930 int memberships
= count_memberships(dl
, super
);
933 /* trim the reserved area for spares, so they can join any array
934 * regardless of whether the OROM has assigned sectors from the
935 * IMSM_RESERVED_SECTORS region
938 reservation
= imsm_min_reserved_sectors(super
);
940 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
942 rv
= xcalloc(sizeof(struct extent
), (memberships
+ 1));
945 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
946 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
947 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
949 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
950 e
->start
= pba_of_lba0(map
);
951 e
->size
= blocks_per_member(map
);
955 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
957 /* determine the start of the metadata
958 * when no raid devices are defined use the default
959 * ...otherwise allow the metadata to truncate the value
960 * as is the case with older versions of imsm
963 struct extent
*last
= &rv
[memberships
- 1];
964 unsigned long long remainder
;
966 remainder
= total_blocks(&dl
->disk
) - (last
->start
+ last
->size
);
967 /* round down to 1k block to satisfy precision of the kernel
971 /* make sure remainder is still sane */
972 if (remainder
< (unsigned)ROUND_UP(super
->len
, 512) >> 9)
973 remainder
= ROUND_UP(super
->len
, 512) >> 9;
974 if (reservation
> remainder
)
975 reservation
= remainder
;
977 e
->start
= total_blocks(&dl
->disk
) - reservation
;
982 /* try to determine how much space is reserved for metadata from
983 * the last get_extents() entry, otherwise fallback to the
986 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
992 /* for spares just return a minimal reservation which will grow
993 * once the spare is picked up by an array
996 return MPB_SECTOR_CNT
;
998 e
= get_extents(super
, dl
);
1000 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1002 /* scroll to last entry */
1003 for (i
= 0; e
[i
].size
; i
++)
1006 rv
= total_blocks(&dl
->disk
) - e
[i
].start
;
1013 static int is_spare(struct imsm_disk
*disk
)
1015 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
1018 static int is_configured(struct imsm_disk
*disk
)
1020 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
1023 static int is_failed(struct imsm_disk
*disk
)
1025 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
1028 /* try to determine how much space is reserved for metadata from
1029 * the last get_extents() entry on the smallest active disk,
1030 * otherwise fallback to the default
1032 static __u32
imsm_min_reserved_sectors(struct intel_super
*super
)
1036 unsigned long long min_active
;
1038 __u32 rv
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
1039 struct dl
*dl
, *dl_min
= NULL
;
1045 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
1048 unsigned long long blocks
= total_blocks(&dl
->disk
);
1049 if (blocks
< min_active
|| min_active
== 0) {
1051 min_active
= blocks
;
1057 /* find last lba used by subarrays on the smallest active disk */
1058 e
= get_extents(super
, dl_min
);
1061 for (i
= 0; e
[i
].size
; i
++)
1064 remainder
= min_active
- e
[i
].start
;
1067 /* to give priority to recovery we should not require full
1068 IMSM_RESERVED_SECTORS from the spare */
1069 rv
= MPB_SECTOR_CNT
+ NUM_BLOCKS_DIRTY_STRIPE_REGION
;
1071 /* if real reservation is smaller use that value */
1072 return (remainder
< rv
) ? remainder
: rv
;
1075 /* Return minimum size of a spare that can be used in this array*/
1076 static unsigned long long min_acceptable_spare_size_imsm(struct supertype
*st
)
1078 struct intel_super
*super
= st
->sb
;
1082 unsigned long long rv
= 0;
1086 /* find first active disk in array */
1088 while (dl
&& (is_failed(&dl
->disk
) || dl
->index
== -1))
1092 /* find last lba used by subarrays */
1093 e
= get_extents(super
, dl
);
1096 for (i
= 0; e
[i
].size
; i
++)
1099 rv
= e
[i
-1].start
+ e
[i
-1].size
;
1102 /* add the amount of space needed for metadata */
1103 rv
= rv
+ imsm_min_reserved_sectors(super
);
1108 static int is_gen_migration(struct imsm_dev
*dev
);
1111 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
1112 struct imsm_dev
*dev
);
1114 static void print_imsm_dev(struct intel_super
*super
,
1115 struct imsm_dev
*dev
,
1121 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
1122 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
1126 printf("[%.16s]:\n", dev
->volume
);
1127 printf(" UUID : %s\n", uuid
);
1128 printf(" RAID Level : %d", get_imsm_raid_level(map
));
1130 printf(" <-- %d", get_imsm_raid_level(map2
));
1132 printf(" Members : %d", map
->num_members
);
1134 printf(" <-- %d", map2
->num_members
);
1136 printf(" Slots : [");
1137 for (i
= 0; i
< map
->num_members
; i
++) {
1138 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_0
);
1139 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1144 for (i
= 0; i
< map2
->num_members
; i
++) {
1145 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_1
);
1146 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
1151 printf(" Failed disk : ");
1152 if (map
->failed_disk_num
== 0xff)
1155 printf("%i", map
->failed_disk_num
);
1157 slot
= get_imsm_disk_slot(map
, disk_idx
);
1159 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
1160 printf(" This Slot : %d%s\n", slot
,
1161 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
1163 printf(" This Slot : ?\n");
1164 sz
= __le32_to_cpu(dev
->size_high
);
1166 sz
+= __le32_to_cpu(dev
->size_low
);
1167 printf(" Array Size : %llu%s\n", (unsigned long long)sz
,
1168 human_size(sz
* 512));
1169 sz
= blocks_per_member(map
);
1170 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz
,
1171 human_size(sz
* 512));
1172 printf(" Sector Offset : %llu\n",
1174 printf(" Num Stripes : %llu\n",
1175 num_data_stripes(map
));
1176 printf(" Chunk Size : %u KiB",
1177 __le16_to_cpu(map
->blocks_per_strip
) / 2);
1179 printf(" <-- %u KiB",
1180 __le16_to_cpu(map2
->blocks_per_strip
) / 2);
1182 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
1183 printf(" Migrate State : ");
1184 if (dev
->vol
.migr_state
) {
1185 if (migr_type(dev
) == MIGR_INIT
)
1186 printf("initialize\n");
1187 else if (migr_type(dev
) == MIGR_REBUILD
)
1188 printf("rebuild\n");
1189 else if (migr_type(dev
) == MIGR_VERIFY
)
1191 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
1192 printf("general migration\n");
1193 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
1194 printf("state change\n");
1195 else if (migr_type(dev
) == MIGR_REPAIR
)
1198 printf("<unknown:%d>\n", migr_type(dev
));
1201 printf(" Map State : %s", map_state_str
[map
->map_state
]);
1202 if (dev
->vol
.migr_state
) {
1203 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
1205 printf(" <-- %s", map_state_str
[map
->map_state
]);
1206 printf("\n Checkpoint : %u ",
1207 __le32_to_cpu(dev
->vol
.curr_migr_unit
));
1208 if ((is_gen_migration(dev
)) && ((slot
> 1) || (slot
< 0)))
1211 printf("(%llu)", (unsigned long long)
1212 blocks_per_migr_unit(super
, dev
));
1215 printf(" Dirty State : %s\n", dev
->vol
.dirty
? "dirty" : "clean");
1218 static void print_imsm_disk(struct imsm_disk
*disk
, int index
, __u32 reserved
)
1220 char str
[MAX_RAID_SERIAL_LEN
+ 1];
1223 if (index
< -1 || !disk
)
1227 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
1229 printf(" Disk%02d Serial : %s\n", index
, str
);
1231 printf(" Disk Serial : %s\n", str
);
1232 printf(" State :%s%s%s\n", is_spare(disk
) ? " spare" : "",
1233 is_configured(disk
) ? " active" : "",
1234 is_failed(disk
) ? " failed" : "");
1235 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
1236 sz
= total_blocks(disk
) - reserved
;
1237 printf(" Usable Size : %llu%s\n", (unsigned long long)sz
,
1238 human_size(sz
* 512));
1241 void examine_migr_rec_imsm(struct intel_super
*super
)
1243 struct migr_record
*migr_rec
= super
->migr_rec
;
1244 struct imsm_super
*mpb
= super
->anchor
;
1247 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1248 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1249 struct imsm_map
*map
;
1252 if (is_gen_migration(dev
) == 0)
1255 printf("\nMigration Record Information:");
1257 /* first map under migration */
1258 map
= get_imsm_map(dev
, MAP_0
);
1260 slot
= get_imsm_disk_slot(map
, super
->disks
->index
);
1261 if ((map
== NULL
) || (slot
> 1) || (slot
< 0)) {
1262 printf(" Empty\n ");
1263 printf("Examine one of first two disks in array\n");
1266 printf("\n Status : ");
1267 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
1270 printf("Contains Data\n");
1271 printf(" Current Unit : %u\n",
1272 __le32_to_cpu(migr_rec
->curr_migr_unit
));
1273 printf(" Family : %u\n",
1274 __le32_to_cpu(migr_rec
->family_num
));
1275 printf(" Ascending : %u\n",
1276 __le32_to_cpu(migr_rec
->ascending_migr
));
1277 printf(" Blocks Per Unit : %u\n",
1278 __le32_to_cpu(migr_rec
->blocks_per_unit
));
1279 printf(" Dest. Depth Per Unit : %u\n",
1280 __le32_to_cpu(migr_rec
->dest_depth_per_unit
));
1281 printf(" Checkpoint Area pba : %u\n",
1282 __le32_to_cpu(migr_rec
->ckpt_area_pba
));
1283 printf(" First member lba : %u\n",
1284 __le32_to_cpu(migr_rec
->dest_1st_member_lba
));
1285 printf(" Total Number of Units : %u\n",
1286 __le32_to_cpu(migr_rec
->num_migr_units
));
1287 printf(" Size of volume : %u\n",
1288 __le32_to_cpu(migr_rec
->post_migr_vol_cap
));
1289 printf(" Expansion space for LBA64 : %u\n",
1290 __le32_to_cpu(migr_rec
->post_migr_vol_cap_hi
));
1291 printf(" Record was read from : %u\n",
1292 __le32_to_cpu(migr_rec
->ckpt_read_disk_num
));
1297 #endif /* MDASSEMBLE */
1298 /*******************************************************************************
1299 * function: imsm_check_attributes
1300 * Description: Function checks if features represented by attributes flags
1301 * are supported by mdadm.
1303 * attributes - Attributes read from metadata
1305 * 0 - passed attributes contains unsupported features flags
1306 * 1 - all features are supported
1307 ******************************************************************************/
1308 static int imsm_check_attributes(__u32 attributes
)
1311 __u32 not_supported
= MPB_ATTRIB_SUPPORTED
^0xffffffff;
1313 not_supported
&= ~MPB_ATTRIB_IGNORED
;
1315 not_supported
&= attributes
;
1316 if (not_supported
) {
1317 pr_err("(IMSM): Unsupported attributes : %x\n",
1318 (unsigned)__le32_to_cpu(not_supported
));
1319 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1320 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
1321 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1323 if (not_supported
& MPB_ATTRIB_2TB
) {
1324 dprintf("\t\tMPB_ATTRIB_2TB\n");
1325 not_supported
^= MPB_ATTRIB_2TB
;
1327 if (not_supported
& MPB_ATTRIB_RAID0
) {
1328 dprintf("\t\tMPB_ATTRIB_RAID0\n");
1329 not_supported
^= MPB_ATTRIB_RAID0
;
1331 if (not_supported
& MPB_ATTRIB_RAID1
) {
1332 dprintf("\t\tMPB_ATTRIB_RAID1\n");
1333 not_supported
^= MPB_ATTRIB_RAID1
;
1335 if (not_supported
& MPB_ATTRIB_RAID10
) {
1336 dprintf("\t\tMPB_ATTRIB_RAID10\n");
1337 not_supported
^= MPB_ATTRIB_RAID10
;
1339 if (not_supported
& MPB_ATTRIB_RAID1E
) {
1340 dprintf("\t\tMPB_ATTRIB_RAID1E\n");
1341 not_supported
^= MPB_ATTRIB_RAID1E
;
1343 if (not_supported
& MPB_ATTRIB_RAID5
) {
1344 dprintf("\t\tMPB_ATTRIB_RAID5\n");
1345 not_supported
^= MPB_ATTRIB_RAID5
;
1347 if (not_supported
& MPB_ATTRIB_RAIDCNG
) {
1348 dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
1349 not_supported
^= MPB_ATTRIB_RAIDCNG
;
1351 if (not_supported
& MPB_ATTRIB_BBM
) {
1352 dprintf("\t\tMPB_ATTRIB_BBM\n");
1353 not_supported
^= MPB_ATTRIB_BBM
;
1355 if (not_supported
& MPB_ATTRIB_CHECKSUM_VERIFY
) {
1356 dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
1357 not_supported
^= MPB_ATTRIB_CHECKSUM_VERIFY
;
1359 if (not_supported
& MPB_ATTRIB_EXP_STRIPE_SIZE
) {
1360 dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
1361 not_supported
^= MPB_ATTRIB_EXP_STRIPE_SIZE
;
1363 if (not_supported
& MPB_ATTRIB_2TB_DISK
) {
1364 dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
1365 not_supported
^= MPB_ATTRIB_2TB_DISK
;
1367 if (not_supported
& MPB_ATTRIB_NEVER_USE2
) {
1368 dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
1369 not_supported
^= MPB_ATTRIB_NEVER_USE2
;
1371 if (not_supported
& MPB_ATTRIB_NEVER_USE
) {
1372 dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
1373 not_supported
^= MPB_ATTRIB_NEVER_USE
;
1377 dprintf(Name
"(IMSM): Unknown attributes : %x\n", not_supported
);
1386 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
1388 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
1390 struct intel_super
*super
= st
->sb
;
1391 struct imsm_super
*mpb
= super
->anchor
;
1392 char str
[MAX_SIGNATURE_LENGTH
];
1397 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
1400 snprintf(str
, MPB_SIG_LEN
, "%s", mpb
->sig
);
1401 printf(" Magic : %s\n", str
);
1402 snprintf(str
, strlen(MPB_VERSION_RAID0
), "%s", get_imsm_version(mpb
));
1403 printf(" Version : %s\n", get_imsm_version(mpb
));
1404 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
1405 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
1406 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
1407 printf(" Attributes : ");
1408 if (imsm_check_attributes(mpb
->attributes
))
1409 printf("All supported\n");
1411 printf("not supported\n");
1412 getinfo_super_imsm(st
, &info
, NULL
);
1413 fname_from_uuid(st
, &info
, nbuf
, ':');
1414 printf(" UUID : %s\n", nbuf
+ 5);
1415 sum
= __le32_to_cpu(mpb
->check_sum
);
1416 printf(" Checksum : %08x %s\n", sum
,
1417 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
1418 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
));
1419 printf(" Disks : %d\n", mpb
->num_disks
);
1420 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
1421 print_imsm_disk(__get_imsm_disk(mpb
, super
->disks
->index
), super
->disks
->index
, reserved
);
1422 if (super
->bbm_log
) {
1423 struct bbm_log
*log
= super
->bbm_log
;
1426 printf("Bad Block Management Log:\n");
1427 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
1428 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
1429 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
1430 printf(" Spare Blocks : %d\n", __le32_to_cpu(log
->reserved_spare_block_count
));
1431 printf(" First Spare : %llx\n",
1432 (unsigned long long) __le64_to_cpu(log
->first_spare_lba
));
1434 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1436 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1438 super
->current_vol
= i
;
1439 getinfo_super_imsm(st
, &info
, NULL
);
1440 fname_from_uuid(st
, &info
, nbuf
, ':');
1441 print_imsm_dev(super
, dev
, nbuf
+ 5, super
->disks
->index
);
1443 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1444 if (i
== super
->disks
->index
)
1446 print_imsm_disk(__get_imsm_disk(mpb
, i
), i
, reserved
);
1449 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
1450 if (dl
->index
== -1)
1451 print_imsm_disk(&dl
->disk
, -1, reserved
);
1453 examine_migr_rec_imsm(super
);
1456 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
1458 /* We just write a generic IMSM ARRAY entry */
1461 struct intel_super
*super
= st
->sb
;
1463 if (!super
->anchor
->num_raid_devs
) {
1464 printf("ARRAY metadata=imsm\n");
1468 getinfo_super_imsm(st
, &info
, NULL
);
1469 fname_from_uuid(st
, &info
, nbuf
, ':');
1470 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
1473 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
1475 /* We just write a generic IMSM ARRAY entry */
1479 struct intel_super
*super
= st
->sb
;
1482 if (!super
->anchor
->num_raid_devs
)
1485 getinfo_super_imsm(st
, &info
, NULL
);
1486 fname_from_uuid(st
, &info
, nbuf
, ':');
1487 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1488 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1490 super
->current_vol
= i
;
1491 getinfo_super_imsm(st
, &info
, NULL
);
1492 fname_from_uuid(st
, &info
, nbuf1
, ':');
1493 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
1494 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
1498 static void export_examine_super_imsm(struct supertype
*st
)
1500 struct intel_super
*super
= st
->sb
;
1501 struct imsm_super
*mpb
= super
->anchor
;
1505 getinfo_super_imsm(st
, &info
, NULL
);
1506 fname_from_uuid(st
, &info
, nbuf
, ':');
1507 printf("MD_METADATA=imsm\n");
1508 printf("MD_LEVEL=container\n");
1509 printf("MD_UUID=%s\n", nbuf
+5);
1510 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
1513 static int copy_metadata_imsm(struct supertype
*st
, int from
, int to
)
1515 /* The second last 512byte sector of the device contains
1516 * the "struct imsm_super" metadata.
1517 * This contains mpb_size which is the size in bytes of the
1518 * extended metadata. This is located immediately before
1520 * We want to read all that, plus the last sector which
1521 * may contain a migration record, and write it all
1525 unsigned long long dsize
, offset
;
1527 struct imsm_super
*sb
;
1530 if (posix_memalign(&buf
, 4096, 4096) != 0)
1533 if (!get_dev_size(from
, NULL
, &dsize
))
1536 if (lseek64(from
, dsize
-1024, 0) < 0)
1538 if (read(from
, buf
, 512) != 512)
1541 if (strncmp((char*)sb
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0)
1544 sectors
= mpb_sectors(sb
) + 2;
1545 offset
= dsize
- sectors
* 512;
1546 if (lseek64(from
, offset
, 0) < 0 ||
1547 lseek64(to
, offset
, 0) < 0)
1549 while (written
< sectors
* 512) {
1550 int n
= sectors
*512 - written
;
1553 if (read(from
, buf
, n
) != n
)
1555 if (write(to
, buf
, n
) != n
)
1566 static void detail_super_imsm(struct supertype
*st
, char *homehost
)
1571 getinfo_super_imsm(st
, &info
, NULL
);
1572 fname_from_uuid(st
, &info
, nbuf
, ':');
1573 printf("\n UUID : %s\n", nbuf
+ 5);
1576 static void brief_detail_super_imsm(struct supertype
*st
)
1580 getinfo_super_imsm(st
, &info
, NULL
);
1581 fname_from_uuid(st
, &info
, nbuf
, ':');
1582 printf(" UUID=%s", nbuf
+ 5);
1585 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
);
1586 static void fd2devname(int fd
, char *name
);
1588 static int ahci_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
1590 /* dump an unsorted list of devices attached to AHCI Intel storage
1591 * controller, as well as non-connected ports
1593 int hba_len
= strlen(hba_path
) + 1;
1598 unsigned long port_mask
= (1 << port_count
) - 1;
1600 if (port_count
> (int)sizeof(port_mask
) * 8) {
1602 pr_err("port_count %d out of range\n", port_count
);
1606 /* scroll through /sys/dev/block looking for devices attached to
1609 dir
= opendir("/sys/dev/block");
1610 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
1621 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
1623 path
= devt_to_devpath(makedev(major
, minor
));
1626 if (!path_attached_to_hba(path
, hba_path
)) {
1632 /* retrieve the scsi device type */
1633 if (asprintf(&device
, "/sys/dev/block/%d:%d/device/xxxxxxx", major
, minor
) < 0) {
1635 pr_err("failed to allocate 'device'\n");
1639 sprintf(device
, "/sys/dev/block/%d:%d/device/type", major
, minor
);
1640 if (load_sys(device
, buf
) != 0) {
1642 pr_err("failed to read device type for %s\n",
1648 type
= strtoul(buf
, NULL
, 10);
1650 /* if it's not a disk print the vendor and model */
1651 if (!(type
== 0 || type
== 7 || type
== 14)) {
1654 sprintf(device
, "/sys/dev/block/%d:%d/device/vendor", major
, minor
);
1655 if (load_sys(device
, buf
) == 0) {
1656 strncpy(vendor
, buf
, sizeof(vendor
));
1657 vendor
[sizeof(vendor
) - 1] = '\0';
1658 c
= (char *) &vendor
[sizeof(vendor
) - 1];
1659 while (isspace(*c
) || *c
== '\0')
1663 sprintf(device
, "/sys/dev/block/%d:%d/device/model", major
, minor
);
1664 if (load_sys(device
, buf
) == 0) {
1665 strncpy(model
, buf
, sizeof(model
));
1666 model
[sizeof(model
) - 1] = '\0';
1667 c
= (char *) &model
[sizeof(model
) - 1];
1668 while (isspace(*c
) || *c
== '\0')
1672 if (vendor
[0] && model
[0])
1673 sprintf(buf
, "%.64s %.64s", vendor
, model
);
1675 switch (type
) { /* numbers from hald/linux/device.c */
1676 case 1: sprintf(buf
, "tape"); break;
1677 case 2: sprintf(buf
, "printer"); break;
1678 case 3: sprintf(buf
, "processor"); break;
1680 case 5: sprintf(buf
, "cdrom"); break;
1681 case 6: sprintf(buf
, "scanner"); break;
1682 case 8: sprintf(buf
, "media_changer"); break;
1683 case 9: sprintf(buf
, "comm"); break;
1684 case 12: sprintf(buf
, "raid"); break;
1685 default: sprintf(buf
, "unknown");
1691 /* chop device path to 'host%d' and calculate the port number */
1692 c
= strchr(&path
[hba_len
], '/');
1695 pr_err("%s - invalid path name\n", path
+ hba_len
);
1700 if (sscanf(&path
[hba_len
], "host%d", &port
) == 1)
1704 *c
= '/'; /* repair the full string */
1705 pr_err("failed to determine port number for %s\n",
1712 /* mark this port as used */
1713 port_mask
&= ~(1 << port
);
1715 /* print out the device information */
1717 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
1721 fd
= dev_open(ent
->d_name
, O_RDONLY
);
1723 printf(" Port%d : - disk info unavailable -\n", port
);
1725 fd2devname(fd
, buf
);
1726 printf(" Port%d : %s", port
, buf
);
1727 if (imsm_read_serial(fd
, NULL
, (__u8
*) buf
) == 0)
1728 printf(" (%.*s)\n", MAX_RAID_SERIAL_LEN
, buf
);
1743 for (i
= 0; i
< port_count
; i
++)
1744 if (port_mask
& (1 << i
))
1745 printf(" Port%d : - no device attached -\n", i
);
1751 static void print_found_intel_controllers(struct sys_dev
*elem
)
1753 for (; elem
; elem
= elem
->next
) {
1754 pr_err("found Intel(R) ");
1755 if (elem
->type
== SYS_DEV_SATA
)
1756 fprintf(stderr
, "SATA ");
1757 else if (elem
->type
== SYS_DEV_SAS
)
1758 fprintf(stderr
, "SAS ");
1759 fprintf(stderr
, "RAID controller");
1761 fprintf(stderr
, " at %s", elem
->pci_id
);
1762 fprintf(stderr
, ".\n");
1767 static int ahci_get_port_count(const char *hba_path
, int *port_count
)
1774 if ((dir
= opendir(hba_path
)) == NULL
)
1777 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
1780 if (sscanf(ent
->d_name
, "host%d", &host
) != 1)
1782 if (*port_count
== 0)
1784 else if (host
< host_base
)
1787 if (host
+ 1 > *port_count
+ host_base
)
1788 *port_count
= host
+ 1 - host_base
;
1794 static void print_imsm_capability(const struct imsm_orom
*orom
)
1796 printf(" Platform : Intel(R) Matrix Storage Manager\n");
1797 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
1798 orom
->hotfix_ver
, orom
->build
);
1799 printf(" RAID Levels :%s%s%s%s%s\n",
1800 imsm_orom_has_raid0(orom
) ? " raid0" : "",
1801 imsm_orom_has_raid1(orom
) ? " raid1" : "",
1802 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
1803 imsm_orom_has_raid10(orom
) ? " raid10" : "",
1804 imsm_orom_has_raid5(orom
) ? " raid5" : "");
1805 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1806 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
1807 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
1808 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
1809 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
1810 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
1811 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
1812 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
1813 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
1814 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
1815 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
1816 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
1817 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
1818 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
1819 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
1820 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
1821 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
1822 printf(" 2TB volumes :%s supported\n",
1823 (orom
->attr
& IMSM_OROM_ATTR_2TB
)?"":" not");
1824 printf(" 2TB disks :%s supported\n",
1825 (orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
)?"":" not");
1826 printf(" Max Disks : %d\n", orom
->tds
);
1827 printf(" Max Volumes : %d per array, %d per controller\n",
1828 orom
->vpa
, orom
->vphba
);
1832 static void print_imsm_capability_export(const struct imsm_orom
*orom
)
1834 printf("MD_FIRMWARE_TYPE=imsm\n");
1835 printf("IMSM_VERSION=%d.%d.%d.%d\n",orom
->major_ver
, orom
->minor_ver
,
1836 orom
->hotfix_ver
, orom
->build
);
1837 printf("IMSM_SUPPORTED_RAID_LEVELS=%s%s%s%s%s\n",
1838 imsm_orom_has_raid0(orom
) ? "raid0 " : "",
1839 imsm_orom_has_raid1(orom
) ? "raid1 " : "",
1840 imsm_orom_has_raid1e(orom
) ? "raid1e " : "",
1841 imsm_orom_has_raid5(orom
) ? "raid10 " : "",
1842 imsm_orom_has_raid10(orom
) ? "raid5 " : "");
1843 printf("IMSM_SUPPORTED_CHUNK_SIZES=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1844 imsm_orom_has_chunk(orom
, 2) ? "2k " : "",
1845 imsm_orom_has_chunk(orom
, 4) ? "4k " : "",
1846 imsm_orom_has_chunk(orom
, 8) ? "8k " : "",
1847 imsm_orom_has_chunk(orom
, 16) ? "16k " : "",
1848 imsm_orom_has_chunk(orom
, 32) ? "32k " : "",
1849 imsm_orom_has_chunk(orom
, 64) ? "64k " : "",
1850 imsm_orom_has_chunk(orom
, 128) ? "128k " : "",
1851 imsm_orom_has_chunk(orom
, 256) ? "256k " : "",
1852 imsm_orom_has_chunk(orom
, 512) ? "512k " : "",
1853 imsm_orom_has_chunk(orom
, 1024*1) ? "1M " : "",
1854 imsm_orom_has_chunk(orom
, 1024*2) ? "2M " : "",
1855 imsm_orom_has_chunk(orom
, 1024*4) ? "4M " : "",
1856 imsm_orom_has_chunk(orom
, 1024*8) ? "8M " : "",
1857 imsm_orom_has_chunk(orom
, 1024*16) ? "16M " : "",
1858 imsm_orom_has_chunk(orom
, 1024*32) ? "32M " : "",
1859 imsm_orom_has_chunk(orom
, 1024*64) ? "64M " : "");
1860 printf("IMSM_2TB_VOLUMES=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB
) ? "yes" : "no");
1861 printf("IMSM_2TB_DISKS=%s\n",(orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) ? "yes" : "no");
1862 printf("IMSM_MAX_DISKS=%d\n",orom
->tds
);
1863 printf("IMSM_MAX_VOLUMES_PER_ARRAY=%d\n",orom
->vpa
);
1864 printf("IMSM_MAX_VOLUMES_PER_CONTROLLER=%d\n",orom
->vphba
);
1867 static int detail_platform_imsm(int verbose
, int enumerate_only
, char *controller_path
)
1869 /* There are two components to imsm platform support, the ahci SATA
1870 * controller and the option-rom. To find the SATA controller we
1871 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
1872 * controller with the Intel vendor id is present. This approach
1873 * allows mdadm to leverage the kernel's ahci detection logic, with the
1874 * caveat that if ahci.ko is not loaded mdadm will not be able to
1875 * detect platform raid capabilities. The option-rom resides in a
1876 * platform "Adapter ROM". We scan for its signature to retrieve the
1877 * platform capabilities. If raid support is disabled in the BIOS the
1878 * option-rom capability structure will not be available.
1880 const struct imsm_orom
*orom
;
1881 struct sys_dev
*list
, *hba
;
1886 if (enumerate_only
) {
1887 if (check_env("IMSM_NO_PLATFORM"))
1889 list
= find_intel_devices();
1892 for (hba
= list
; hba
; hba
= hba
->next
) {
1893 orom
= find_imsm_capability(hba
->type
);
1904 list
= find_intel_devices();
1907 pr_err("no active Intel(R) RAID "
1908 "controller found.\n");
1910 } else if (verbose
> 0)
1911 print_found_intel_controllers(list
);
1913 for (hba
= list
; hba
; hba
= hba
->next
) {
1914 if (controller_path
&& (compare_paths(hba
->path
,controller_path
) != 0))
1916 orom
= find_imsm_capability(hba
->type
);
1918 pr_err("imsm capabilities not found for controller: %s (type %s)\n",
1919 hba
->path
, get_sys_dev_type(hba
->type
));
1922 print_imsm_capability(orom
);
1923 printf(" I/O Controller : %s (%s)\n",
1924 hba
->path
, get_sys_dev_type(hba
->type
));
1925 if (hba
->type
== SYS_DEV_SATA
) {
1926 host_base
= ahci_get_port_count(hba
->path
, &port_count
);
1927 if (ahci_enumerate_ports(hba
->path
, port_count
, host_base
, verbose
)) {
1929 pr_err("failed to enumerate "
1930 "ports on SATA controller at %s.\n", hba
->pci_id
);
1937 if (controller_path
&& result
== 1)
1938 pr_err("no active Intel(R) RAID "
1939 "controller found under %s\n",controller_path
);
1944 static int export_detail_platform_imsm(int verbose
, char *controller_path
)
1946 const struct imsm_orom
*orom
;
1947 struct sys_dev
*list
, *hba
;
1950 list
= find_intel_devices();
1953 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_INTEL_DEVICES\n");
1958 for (hba
= list
; hba
; hba
= hba
->next
) {
1959 if (controller_path
&& (compare_paths(hba
->path
,controller_path
) != 0))
1961 orom
= find_imsm_capability(hba
->type
);
1964 pr_err("IMSM_DETAIL_PLATFORM_ERROR=NO_IMSM_CAPABLE_DEVICE_UNDER_%s\n",hba
->path
);
1967 print_imsm_capability_export(orom
);
1977 static int match_home_imsm(struct supertype
*st
, char *homehost
)
1979 /* the imsm metadata format does not specify any host
1980 * identification information. We return -1 since we can never
1981 * confirm nor deny whether a given array is "meant" for this
1982 * host. We rely on compare_super and the 'family_num' fields to
1983 * exclude member disks that do not belong, and we rely on
1984 * mdadm.conf to specify the arrays that should be assembled.
1985 * Auto-assembly may still pick up "foreign" arrays.
1991 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
1993 /* The uuid returned here is used for:
1994 * uuid to put into bitmap file (Create, Grow)
1995 * uuid for backup header when saving critical section (Grow)
1996 * comparing uuids when re-adding a device into an array
1997 * In these cases the uuid required is that of the data-array,
1998 * not the device-set.
1999 * uuid to recognise same set when adding a missing device back
2000 * to an array. This is a uuid for the device-set.
2002 * For each of these we can make do with a truncated
2003 * or hashed uuid rather than the original, as long as
2005 * In each case the uuid required is that of the data-array,
2006 * not the device-set.
2008 /* imsm does not track uuid's so we synthesis one using sha1 on
2009 * - The signature (Which is constant for all imsm array, but no matter)
2010 * - the orig_family_num of the container
2011 * - the index number of the volume
2012 * - the 'serial' number of the volume.
2013 * Hopefully these are all constant.
2015 struct intel_super
*super
= st
->sb
;
2018 struct sha1_ctx ctx
;
2019 struct imsm_dev
*dev
= NULL
;
2022 /* some mdadm versions failed to set ->orig_family_num, in which
2023 * case fall back to ->family_num. orig_family_num will be
2024 * fixed up with the first metadata update.
2026 family_num
= super
->anchor
->orig_family_num
;
2027 if (family_num
== 0)
2028 family_num
= super
->anchor
->family_num
;
2029 sha1_init_ctx(&ctx
);
2030 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
2031 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
2032 if (super
->current_vol
>= 0)
2033 dev
= get_imsm_dev(super
, super
->current_vol
);
2035 __u32 vol
= super
->current_vol
;
2036 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
2037 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
2039 sha1_finish_ctx(&ctx
, buf
);
2040 memcpy(uuid
, buf
, 4*4);
2045 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
2047 __u8
*v
= get_imsm_version(mpb
);
2048 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
2049 char major
[] = { 0, 0, 0 };
2050 char minor
[] = { 0 ,0, 0 };
2051 char patch
[] = { 0, 0, 0 };
2052 char *ver_parse
[] = { major
, minor
, patch
};
2056 while (*v
!= '\0' && v
< end
) {
2057 if (*v
!= '.' && j
< 2)
2058 ver_parse
[i
][j
++] = *v
;
2066 *m
= strtol(minor
, NULL
, 0);
2067 *p
= strtol(patch
, NULL
, 0);
2071 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
2073 /* migr_strip_size when repairing or initializing parity */
2074 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2075 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2077 switch (get_imsm_raid_level(map
)) {
2082 return 128*1024 >> 9;
2086 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
2088 /* migr_strip_size when rebuilding a degraded disk, no idea why
2089 * this is different than migr_strip_size_resync(), but it's good
2092 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2093 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2095 switch (get_imsm_raid_level(map
)) {
2098 if (map
->num_members
% map
->num_domains
== 0)
2099 return 128*1024 >> 9;
2103 return max((__u32
) 64*1024 >> 9, chunk
);
2105 return 128*1024 >> 9;
2109 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
2111 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2112 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2113 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
2114 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
2116 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
2119 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
2121 struct imsm_map
*lo
= get_imsm_map(dev
, MAP_0
);
2122 int level
= get_imsm_raid_level(lo
);
2124 if (level
== 1 || level
== 10) {
2125 struct imsm_map
*hi
= get_imsm_map(dev
, MAP_1
);
2127 return hi
->num_domains
;
2129 return num_stripes_per_unit_resync(dev
);
2132 static __u8
imsm_num_data_members(struct imsm_dev
*dev
, int second_map
)
2134 /* named 'imsm_' because raid0, raid1 and raid10
2135 * counter-intuitively have the same number of data disks
2137 struct imsm_map
*map
= get_imsm_map(dev
, second_map
);
2139 switch (get_imsm_raid_level(map
)) {
2141 return map
->num_members
;
2145 return map
->num_members
/2;
2147 return map
->num_members
- 1;
2149 dprintf("%s: unsupported raid level\n", __func__
);
2154 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
2156 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2157 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2159 switch(get_imsm_raid_level(map
)) {
2162 return chunk
* map
->num_domains
;
2164 return chunk
* map
->num_members
;
2170 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
2172 struct imsm_map
*map
= get_imsm_map(dev
, MAP_1
);
2173 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
2174 __u32 strip
= block
/ chunk
;
2176 switch (get_imsm_raid_level(map
)) {
2179 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
2180 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
2182 return vol_stripe
* chunk
+ block
% chunk
;
2184 __u32 stripe
= strip
/ (map
->num_members
- 1);
2186 return stripe
* chunk
+ block
% chunk
;
2193 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
2194 struct imsm_dev
*dev
)
2196 /* calculate the conversion factor between per member 'blocks'
2197 * (md/{resync,rebuild}_start) and imsm migration units, return
2198 * 0 for the 'not migrating' and 'unsupported migration' cases
2200 if (!dev
->vol
.migr_state
)
2203 switch (migr_type(dev
)) {
2204 case MIGR_GEN_MIGR
: {
2205 struct migr_record
*migr_rec
= super
->migr_rec
;
2206 return __le32_to_cpu(migr_rec
->blocks_per_unit
);
2211 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2212 __u32 stripes_per_unit
;
2213 __u32 blocks_per_unit
;
2222 /* yes, this is really the translation of migr_units to
2223 * per-member blocks in the 'resync' case
2225 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
2226 migr_chunk
= migr_strip_blocks_resync(dev
);
2227 disks
= imsm_num_data_members(dev
, MAP_0
);
2228 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
2229 stripe
= __le16_to_cpu(map
->blocks_per_strip
) * disks
;
2230 segment
= blocks_per_unit
/ stripe
;
2231 block_rel
= blocks_per_unit
- segment
* stripe
;
2232 parity_depth
= parity_segment_depth(dev
);
2233 block_map
= map_migr_block(dev
, block_rel
);
2234 return block_map
+ parity_depth
* segment
;
2236 case MIGR_REBUILD
: {
2237 __u32 stripes_per_unit
;
2240 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
2241 migr_chunk
= migr_strip_blocks_rebuild(dev
);
2242 return migr_chunk
* stripes_per_unit
;
2244 case MIGR_STATE_CHANGE
:
2250 static int imsm_level_to_layout(int level
)
2258 return ALGORITHM_LEFT_ASYMMETRIC
;
2265 /*******************************************************************************
2266 * Function: read_imsm_migr_rec
2267 * Description: Function reads imsm migration record from last sector of disk
2269 * fd : disk descriptor
2270 * super : metadata info
2274 ******************************************************************************/
2275 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
)
2278 unsigned long long dsize
;
2280 get_dev_size(fd
, NULL
, &dsize
);
2281 if (lseek64(fd
, dsize
- MIGR_REC_POSITION
, SEEK_SET
) < 0) {
2282 pr_err("Cannot seek to anchor block: %s\n",
2286 if (read(fd
, super
->migr_rec_buf
, MIGR_REC_BUF_SIZE
) !=
2287 MIGR_REC_BUF_SIZE
) {
2288 pr_err("Cannot read migr record block: %s\n",
2298 static struct imsm_dev
*imsm_get_device_during_migration(
2299 struct intel_super
*super
)
2302 struct intel_dev
*dv
;
2304 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
2305 if (is_gen_migration(dv
->dev
))
2311 /*******************************************************************************
2312 * Function: load_imsm_migr_rec
2313 * Description: Function reads imsm migration record (it is stored at the last
2316 * super : imsm internal array info
2317 * info : general array info
2321 * -2 : no migration in progress
2322 ******************************************************************************/
2323 static int load_imsm_migr_rec(struct intel_super
*super
, struct mdinfo
*info
)
2326 struct dl
*dl
= NULL
;
2330 struct imsm_dev
*dev
;
2331 struct imsm_map
*map
= NULL
;
2334 /* find map under migration */
2335 dev
= imsm_get_device_during_migration(super
);
2336 /* nothing to load,no migration in progress?
2340 map
= get_imsm_map(dev
, MAP_0
);
2343 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
2344 /* skip spare and failed disks
2346 if (sd
->disk
.raid_disk
< 0)
2348 /* read only from one of the first two slots */
2350 slot
= get_imsm_disk_slot(map
,
2351 sd
->disk
.raid_disk
);
2352 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2355 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
2356 fd
= dev_open(nm
, O_RDONLY
);
2362 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
2363 /* skip spare and failed disks
2367 /* read only from one of the first two slots */
2369 slot
= get_imsm_disk_slot(map
, dl
->index
);
2370 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2372 sprintf(nm
, "%d:%d", dl
->major
, dl
->minor
);
2373 fd
= dev_open(nm
, O_RDONLY
);
2380 retval
= read_imsm_migr_rec(fd
, super
);
2389 /*******************************************************************************
2390 * function: imsm_create_metadata_checkpoint_update
2391 * Description: It creates update for checkpoint change.
2393 * super : imsm internal array info
2394 * u : pointer to prepared update
2397 * If length is equal to 0, input pointer u contains no update
2398 ******************************************************************************/
2399 static int imsm_create_metadata_checkpoint_update(
2400 struct intel_super
*super
,
2401 struct imsm_update_general_migration_checkpoint
**u
)
2404 int update_memory_size
= 0;
2406 dprintf("imsm_create_metadata_checkpoint_update(enter)\n");
2412 /* size of all update data without anchor */
2413 update_memory_size
=
2414 sizeof(struct imsm_update_general_migration_checkpoint
);
2416 *u
= xcalloc(1, update_memory_size
);
2418 dprintf("error: cannot get memory for "
2419 "imsm_create_metadata_checkpoint_update update\n");
2422 (*u
)->type
= update_general_migration_checkpoint
;
2423 (*u
)->curr_migr_unit
= __le32_to_cpu(super
->migr_rec
->curr_migr_unit
);
2424 dprintf("imsm_create_metadata_checkpoint_update: prepared for %u\n",
2425 (*u
)->curr_migr_unit
);
2427 return update_memory_size
;
2431 static void imsm_update_metadata_locally(struct supertype
*st
,
2432 void *buf
, int len
);
2434 /*******************************************************************************
2435 * Function: write_imsm_migr_rec
2436 * Description: Function writes imsm migration record
2437 * (at the last sector of disk)
2439 * super : imsm internal array info
2443 ******************************************************************************/
2444 static int write_imsm_migr_rec(struct supertype
*st
)
2446 struct intel_super
*super
= st
->sb
;
2447 unsigned long long dsize
;
2453 struct imsm_update_general_migration_checkpoint
*u
;
2454 struct imsm_dev
*dev
;
2455 struct imsm_map
*map
= NULL
;
2457 /* find map under migration */
2458 dev
= imsm_get_device_during_migration(super
);
2459 /* if no migration, write buffer anyway to clear migr_record
2460 * on disk based on first available device
2463 dev
= get_imsm_dev(super
, super
->current_vol
< 0 ? 0 :
2464 super
->current_vol
);
2466 map
= get_imsm_map(dev
, MAP_0
);
2468 for (sd
= super
->disks
; sd
; sd
= sd
->next
) {
2471 /* skip failed and spare devices */
2474 /* write to 2 first slots only */
2476 slot
= get_imsm_disk_slot(map
, sd
->index
);
2477 if ((map
== NULL
) || (slot
> 1) || (slot
< 0))
2480 sprintf(nm
, "%d:%d", sd
->major
, sd
->minor
);
2481 fd
= dev_open(nm
, O_RDWR
);
2484 get_dev_size(fd
, NULL
, &dsize
);
2485 if (lseek64(fd
, dsize
- MIGR_REC_POSITION
, SEEK_SET
) < 0) {
2486 pr_err("Cannot seek to anchor block: %s\n",
2490 if (write(fd
, super
->migr_rec_buf
, MIGR_REC_BUF_SIZE
) !=
2491 MIGR_REC_BUF_SIZE
) {
2492 pr_err("Cannot write migr record block: %s\n",
2499 /* update checkpoint information in metadata */
2500 len
= imsm_create_metadata_checkpoint_update(super
, &u
);
2503 dprintf("imsm: Cannot prepare update\n");
2506 /* update metadata locally */
2507 imsm_update_metadata_locally(st
, u
, len
);
2508 /* and possibly remotely */
2509 if (st
->update_tail
) {
2510 append_metadata_update(st
, u
, len
);
2511 /* during reshape we do all work inside metadata handler
2512 * manage_reshape(), so metadata update has to be triggered
2515 flush_metadata_updates(st
);
2516 st
->update_tail
= &st
->updates
;
2526 #endif /* MDASSEMBLE */
2528 /* spare/missing disks activations are not allowe when
2529 * array/container performs reshape operation, because
2530 * all arrays in container works on the same disks set
2532 int imsm_reshape_blocks_arrays_changes(struct intel_super
*super
)
2535 struct intel_dev
*i_dev
;
2536 struct imsm_dev
*dev
;
2538 /* check whole container
2540 for (i_dev
= super
->devlist
; i_dev
; i_dev
= i_dev
->next
) {
2542 if (is_gen_migration(dev
)) {
2543 /* No repair during any migration in container
2551 static unsigned long long imsm_component_size_aligment_check(int level
,
2553 unsigned long long component_size
)
2555 unsigned int component_size_alligment
;
2557 /* check component size aligment
2559 component_size_alligment
= component_size
% (chunk_size
/512);
2561 dprintf("imsm_component_size_aligment_check(Level: %i, "
2562 "chunk_size = %i, component_size = %llu), "
2563 "component_size_alligment = %u\n",
2564 level
, chunk_size
, component_size
,
2565 component_size_alligment
);
2567 if (component_size_alligment
&& (level
!= 1) && (level
!= UnSet
)) {
2568 dprintf("imsm: reported component size alligned from %llu ",
2570 component_size
-= component_size_alligment
;
2571 dprintf("to %llu (%i).\n",
2572 component_size
, component_size_alligment
);
2575 return component_size
;
2578 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
2580 struct intel_super
*super
= st
->sb
;
2581 struct migr_record
*migr_rec
= super
->migr_rec
;
2582 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
2583 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
2584 struct imsm_map
*prev_map
= get_imsm_map(dev
, MAP_1
);
2585 struct imsm_map
*map_to_analyse
= map
;
2587 int map_disks
= info
->array
.raid_disks
;
2589 memset(info
, 0, sizeof(*info
));
2591 map_to_analyse
= prev_map
;
2593 dl
= super
->current_disk
;
2595 info
->container_member
= super
->current_vol
;
2596 info
->array
.raid_disks
= map
->num_members
;
2597 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
2598 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
2599 info
->array
.md_minor
= -1;
2600 info
->array
.ctime
= 0;
2601 info
->array
.utime
= 0;
2602 info
->array
.chunk_size
=
2603 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
2604 info
->array
.state
= !dev
->vol
.dirty
;
2605 info
->custom_array_size
= __le32_to_cpu(dev
->size_high
);
2606 info
->custom_array_size
<<= 32;
2607 info
->custom_array_size
|= __le32_to_cpu(dev
->size_low
);
2608 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
2610 if (is_gen_migration(dev
)) {
2611 info
->reshape_active
= 1;
2612 info
->new_level
= get_imsm_raid_level(map
);
2613 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
2614 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
2615 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
2616 if (info
->delta_disks
) {
2617 /* this needs to be applied to every array
2620 info
->reshape_active
= CONTAINER_RESHAPE
;
2622 /* We shape information that we give to md might have to be
2623 * modify to cope with md's requirement for reshaping arrays.
2624 * For example, when reshaping a RAID0, md requires it to be
2625 * presented as a degraded RAID4.
2626 * Also if a RAID0 is migrating to a RAID5 we need to specify
2627 * the array as already being RAID5, but the 'before' layout
2628 * is a RAID4-like layout.
2630 switch (info
->array
.level
) {
2632 switch(info
->new_level
) {
2634 /* conversion is happening as RAID4 */
2635 info
->array
.level
= 4;
2636 info
->array
.raid_disks
+= 1;
2639 /* conversion is happening as RAID5 */
2640 info
->array
.level
= 5;
2641 info
->array
.layout
= ALGORITHM_PARITY_N
;
2642 info
->delta_disks
-= 1;
2645 /* FIXME error message */
2646 info
->array
.level
= UnSet
;
2652 info
->new_level
= UnSet
;
2653 info
->new_layout
= UnSet
;
2654 info
->new_chunk
= info
->array
.chunk_size
;
2655 info
->delta_disks
= 0;
2659 info
->disk
.major
= dl
->major
;
2660 info
->disk
.minor
= dl
->minor
;
2661 info
->disk
.number
= dl
->index
;
2662 info
->disk
.raid_disk
= get_imsm_disk_slot(map_to_analyse
,
2666 info
->data_offset
= pba_of_lba0(map_to_analyse
);
2667 info
->component_size
= blocks_per_member(map_to_analyse
);
2669 info
->component_size
= imsm_component_size_aligment_check(
2671 info
->array
.chunk_size
,
2672 info
->component_size
);
2674 memset(info
->uuid
, 0, sizeof(info
->uuid
));
2675 info
->recovery_start
= MaxSector
;
2677 info
->reshape_progress
= 0;
2678 info
->resync_start
= MaxSector
;
2679 if ((map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
2681 imsm_reshape_blocks_arrays_changes(super
) == 0) {
2682 info
->resync_start
= 0;
2684 if (dev
->vol
.migr_state
) {
2685 switch (migr_type(dev
)) {
2688 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2690 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
2692 info
->resync_start
= blocks_per_unit
* units
;
2695 case MIGR_GEN_MIGR
: {
2696 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2698 __u64 units
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
2699 unsigned long long array_blocks
;
2702 if (__le32_to_cpu(migr_rec
->ascending_migr
) &&
2704 (__le32_to_cpu(migr_rec
->num_migr_units
)-1)) &&
2705 (super
->migr_rec
->rec_status
==
2706 __cpu_to_le32(UNIT_SRC_IN_CP_AREA
)))
2709 info
->reshape_progress
= blocks_per_unit
* units
;
2711 dprintf("IMSM: General Migration checkpoint : %llu "
2712 "(%llu) -> read reshape progress : %llu\n",
2713 (unsigned long long)units
,
2714 (unsigned long long)blocks_per_unit
,
2715 info
->reshape_progress
);
2717 used_disks
= imsm_num_data_members(dev
, MAP_1
);
2718 if (used_disks
> 0) {
2719 array_blocks
= blocks_per_member(map
) *
2721 /* round array size down to closest MB
2723 info
->custom_array_size
= (array_blocks
2724 >> SECT_PER_MB_SHIFT
)
2725 << SECT_PER_MB_SHIFT
;
2729 /* we could emulate the checkpointing of
2730 * 'sync_action=check' migrations, but for now
2731 * we just immediately complete them
2734 /* this is handled by container_content_imsm() */
2735 case MIGR_STATE_CHANGE
:
2736 /* FIXME handle other migrations */
2738 /* we are not dirty, so... */
2739 info
->resync_start
= MaxSector
;
2743 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
2744 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
2746 info
->array
.major_version
= -1;
2747 info
->array
.minor_version
= -2;
2748 sprintf(info
->text_version
, "/%s/%d", st
->container_devnm
, info
->container_member
);
2749 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
2750 uuid_from_super_imsm(st
, info
->uuid
);
2754 for (i
=0; i
<map_disks
; i
++) {
2756 if (i
< info
->array
.raid_disks
) {
2757 struct imsm_disk
*dsk
;
2758 j
= get_imsm_disk_idx(dev
, i
, MAP_X
);
2759 dsk
= get_imsm_disk(super
, j
);
2760 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
2767 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
2768 int failed
, int look_in_map
);
2770 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
2775 static void manage_second_map(struct intel_super
*super
, struct imsm_dev
*dev
)
2777 if (is_gen_migration(dev
)) {
2780 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
2782 failed
= imsm_count_failed(super
, dev
, MAP_1
);
2783 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_1
);
2784 if (map2
->map_state
!= map_state
) {
2785 map2
->map_state
= map_state
;
2786 super
->updates_pending
++;
2792 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
2796 for (d
= super
->missing
; d
; d
= d
->next
)
2797 if (d
->index
== index
)
2802 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
2804 struct intel_super
*super
= st
->sb
;
2805 struct imsm_disk
*disk
;
2806 int map_disks
= info
->array
.raid_disks
;
2807 int max_enough
= -1;
2809 struct imsm_super
*mpb
;
2811 if (super
->current_vol
>= 0) {
2812 getinfo_super_imsm_volume(st
, info
, map
);
2815 memset(info
, 0, sizeof(*info
));
2817 /* Set raid_disks to zero so that Assemble will always pull in valid
2820 info
->array
.raid_disks
= 0;
2821 info
->array
.level
= LEVEL_CONTAINER
;
2822 info
->array
.layout
= 0;
2823 info
->array
.md_minor
= -1;
2824 info
->array
.ctime
= 0; /* N/A for imsm */
2825 info
->array
.utime
= 0;
2826 info
->array
.chunk_size
= 0;
2828 info
->disk
.major
= 0;
2829 info
->disk
.minor
= 0;
2830 info
->disk
.raid_disk
= -1;
2831 info
->reshape_active
= 0;
2832 info
->array
.major_version
= -1;
2833 info
->array
.minor_version
= -2;
2834 strcpy(info
->text_version
, "imsm");
2835 info
->safe_mode_delay
= 0;
2836 info
->disk
.number
= -1;
2837 info
->disk
.state
= 0;
2839 info
->recovery_start
= MaxSector
;
2840 info
->recovery_blocked
= imsm_reshape_blocks_arrays_changes(st
->sb
);
2842 /* do we have the all the insync disks that we expect? */
2843 mpb
= super
->anchor
;
2845 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
2846 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
2847 int failed
, enough
, j
, missing
= 0;
2848 struct imsm_map
*map
;
2851 failed
= imsm_count_failed(super
, dev
, MAP_0
);
2852 state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
2853 map
= get_imsm_map(dev
, MAP_0
);
2855 /* any newly missing disks?
2856 * (catches single-degraded vs double-degraded)
2858 for (j
= 0; j
< map
->num_members
; j
++) {
2859 __u32 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_0
);
2860 __u32 idx
= ord_to_idx(ord
);
2862 if (!(ord
& IMSM_ORD_REBUILD
) &&
2863 get_imsm_missing(super
, idx
)) {
2869 if (state
== IMSM_T_STATE_FAILED
)
2871 else if (state
== IMSM_T_STATE_DEGRADED
&&
2872 (state
!= map
->map_state
|| missing
))
2874 else /* we're normal, or already degraded */
2876 if (is_gen_migration(dev
) && missing
) {
2877 /* during general migration we need all disks
2878 * that process is running on.
2879 * No new missing disk is allowed.
2883 /* no more checks necessary
2887 /* in the missing/failed disk case check to see
2888 * if at least one array is runnable
2890 max_enough
= max(max_enough
, enough
);
2892 dprintf("%s: enough: %d\n", __func__
, max_enough
);
2893 info
->container_enough
= max_enough
;
2896 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
2898 disk
= &super
->disks
->disk
;
2899 info
->data_offset
= total_blocks(&super
->disks
->disk
) - reserved
;
2900 info
->component_size
= reserved
;
2901 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
2902 /* we don't change info->disk.raid_disk here because
2903 * this state will be finalized in mdmon after we have
2904 * found the 'most fresh' version of the metadata
2906 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
2907 info
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
2910 /* only call uuid_from_super_imsm when this disk is part of a populated container,
2911 * ->compare_super may have updated the 'num_raid_devs' field for spares
2913 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
2914 uuid_from_super_imsm(st
, info
->uuid
);
2916 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
2918 /* I don't know how to compute 'map' on imsm, so use safe default */
2921 for (i
= 0; i
< map_disks
; i
++)
2927 /* allocates memory and fills disk in mdinfo structure
2928 * for each disk in array */
2929 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
2931 struct mdinfo
*mddev
= NULL
;
2932 struct intel_super
*super
= st
->sb
;
2933 struct imsm_disk
*disk
;
2936 if (!super
|| !super
->disks
)
2939 mddev
= xcalloc(1, sizeof(*mddev
));
2943 tmp
= xcalloc(1, sizeof(*tmp
));
2945 tmp
->next
= mddev
->devs
;
2947 tmp
->disk
.number
= count
++;
2948 tmp
->disk
.major
= dl
->major
;
2949 tmp
->disk
.minor
= dl
->minor
;
2950 tmp
->disk
.state
= is_configured(disk
) ?
2951 (1 << MD_DISK_ACTIVE
) : 0;
2952 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
2953 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
2954 tmp
->disk
.raid_disk
= -1;
2960 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
2961 char *update
, char *devname
, int verbose
,
2962 int uuid_set
, char *homehost
)
2964 /* For 'assemble' and 'force' we need to return non-zero if any
2965 * change was made. For others, the return value is ignored.
2966 * Update options are:
2967 * force-one : This device looks a bit old but needs to be included,
2968 * update age info appropriately.
2969 * assemble: clear any 'faulty' flag to allow this device to
2971 * force-array: Array is degraded but being forced, mark it clean
2972 * if that will be needed to assemble it.
2974 * newdev: not used ????
2975 * grow: Array has gained a new device - this is currently for
2977 * resync: mark as dirty so a resync will happen.
2978 * name: update the name - preserving the homehost
2979 * uuid: Change the uuid of the array to match watch is given
2981 * Following are not relevant for this imsm:
2982 * sparc2.2 : update from old dodgey metadata
2983 * super-minor: change the preferred_minor number
2984 * summaries: update redundant counters.
2985 * homehost: update the recorded homehost
2986 * _reshape_progress: record new reshape_progress position.
2989 struct intel_super
*super
= st
->sb
;
2990 struct imsm_super
*mpb
;
2992 /* we can only update container info */
2993 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
2996 mpb
= super
->anchor
;
2998 if (strcmp(update
, "uuid") == 0) {
2999 /* We take this to mean that the family_num should be updated.
3000 * However that is much smaller than the uuid so we cannot really
3001 * allow an explicit uuid to be given. And it is hard to reliably
3003 * So if !uuid_set we know the current uuid is random and just used
3004 * the first 'int' and copy it to the other 3 positions.
3005 * Otherwise we require the 4 'int's to be the same as would be the
3006 * case if we are using a random uuid. So an explicit uuid will be
3007 * accepted as long as all for ints are the same... which shouldn't hurt
3010 info
->uuid
[1] = info
->uuid
[2] = info
->uuid
[3] = info
->uuid
[0];
3013 if (info
->uuid
[0] != info
->uuid
[1] ||
3014 info
->uuid
[1] != info
->uuid
[2] ||
3015 info
->uuid
[2] != info
->uuid
[3])
3021 mpb
->orig_family_num
= info
->uuid
[0];
3022 } else if (strcmp(update
, "assemble") == 0)
3027 /* successful update? recompute checksum */
3029 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
3034 static size_t disks_to_mpb_size(int disks
)
3038 size
= sizeof(struct imsm_super
);
3039 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
3040 size
+= 2 * sizeof(struct imsm_dev
);
3041 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
3042 size
+= (4 - 2) * sizeof(struct imsm_map
);
3043 /* 4 possible disk_ord_tbl's */
3044 size
+= 4 * (disks
- 1) * sizeof(__u32
);
3049 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
,
3050 unsigned long long data_offset
)
3052 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
3055 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
3058 static void free_devlist(struct intel_super
*super
)
3060 struct intel_dev
*dv
;
3062 while (super
->devlist
) {
3063 dv
= super
->devlist
->next
;
3064 free(super
->devlist
->dev
);
3065 free(super
->devlist
);
3066 super
->devlist
= dv
;
3070 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
3072 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
3075 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
3079 * 0 same, or first was empty, and second was copied
3080 * 1 second had wrong number
3082 * 3 wrong other info
3084 struct intel_super
*first
= st
->sb
;
3085 struct intel_super
*sec
= tst
->sb
;
3092 /* in platform dependent environment test if the disks
3093 * use the same Intel hba
3094 * If not on Intel hba at all, allow anything.
3096 if (!check_env("IMSM_NO_PLATFORM")) {
3097 if (first
->hba
&& sec
->hba
&&
3098 strcmp(first
->hba
->path
, sec
->hba
->path
) != 0) {
3100 "HBAs of devices does not match %s != %s\n",
3101 first
->hba
? first
->hba
->path
: NULL
,
3102 sec
->hba
? sec
->hba
->path
: NULL
);
3107 /* if an anchor does not have num_raid_devs set then it is a free
3110 if (first
->anchor
->num_raid_devs
> 0 &&
3111 sec
->anchor
->num_raid_devs
> 0) {
3112 /* Determine if these disks might ever have been
3113 * related. Further disambiguation can only take place
3114 * in load_super_imsm_all
3116 __u32 first_family
= first
->anchor
->orig_family_num
;
3117 __u32 sec_family
= sec
->anchor
->orig_family_num
;
3119 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
3120 MAX_SIGNATURE_LENGTH
) != 0)
3123 if (first_family
== 0)
3124 first_family
= first
->anchor
->family_num
;
3125 if (sec_family
== 0)
3126 sec_family
= sec
->anchor
->family_num
;
3128 if (first_family
!= sec_family
)
3134 /* if 'first' is a spare promote it to a populated mpb with sec's
3137 if (first
->anchor
->num_raid_devs
== 0 &&
3138 sec
->anchor
->num_raid_devs
> 0) {
3140 struct intel_dev
*dv
;
3141 struct imsm_dev
*dev
;
3143 /* we need to copy raid device info from sec if an allocation
3144 * fails here we don't associate the spare
3146 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
3147 dv
= xmalloc(sizeof(*dv
));
3148 dev
= xmalloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
3151 dv
->next
= first
->devlist
;
3152 first
->devlist
= dv
;
3154 if (i
< sec
->anchor
->num_raid_devs
) {
3155 /* allocation failure */
3156 free_devlist(first
);
3157 fprintf(stderr
, "imsm: failed to associate spare\n");
3160 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
3161 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
3162 first
->anchor
->family_num
= sec
->anchor
->family_num
;
3163 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
3164 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
3165 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
3171 static void fd2devname(int fd
, char *name
)
3175 char dname
[PATH_MAX
];
3180 if (fstat(fd
, &st
) != 0)
3182 sprintf(path
, "/sys/dev/block/%d:%d",
3183 major(st
.st_rdev
), minor(st
.st_rdev
));
3185 rv
= readlink(path
, dname
, sizeof(dname
)-1);
3190 nm
= strrchr(dname
, '/');
3193 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
3197 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
3199 static int imsm_read_serial(int fd
, char *devname
,
3200 __u8 serial
[MAX_RAID_SERIAL_LEN
])
3202 unsigned char scsi_serial
[255];
3211 memset(scsi_serial
, 0, sizeof(scsi_serial
));
3213 rv
= scsi_get_serial(fd
, scsi_serial
, sizeof(scsi_serial
));
3215 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
3216 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3217 fd2devname(fd
, (char *) serial
);
3223 pr_err("Failed to retrieve serial for %s\n",
3228 rsp_len
= scsi_serial
[3];
3231 pr_err("Failed to retrieve serial for %s\n",
3235 rsp_buf
= (char *) &scsi_serial
[4];
3237 /* trim all whitespace and non-printable characters and convert
3240 for (i
= 0, dest
= rsp_buf
; i
< rsp_len
; i
++) {
3243 /* ':' is reserved for use in placeholder serial
3244 * numbers for missing disks
3252 len
= dest
- rsp_buf
;
3255 /* truncate leading characters */
3256 if (len
> MAX_RAID_SERIAL_LEN
) {
3257 dest
+= len
- MAX_RAID_SERIAL_LEN
;
3258 len
= MAX_RAID_SERIAL_LEN
;
3261 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
3262 memcpy(serial
, dest
, len
);
3267 static int serialcmp(__u8
*s1
, __u8
*s2
)
3269 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
3272 static void serialcpy(__u8
*dest
, __u8
*src
)
3274 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
3277 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
3281 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3282 if (serialcmp(dl
->serial
, serial
) == 0)
3288 static struct imsm_disk
*
3289 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
3293 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3294 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3296 if (serialcmp(disk
->serial
, serial
) == 0) {
3307 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3309 struct imsm_disk
*disk
;
3314 __u8 serial
[MAX_RAID_SERIAL_LEN
];
3316 rv
= imsm_read_serial(fd
, devname
, serial
);
3321 dl
= xcalloc(1, sizeof(*dl
));
3324 dl
->major
= major(stb
.st_rdev
);
3325 dl
->minor
= minor(stb
.st_rdev
);
3326 dl
->next
= super
->disks
;
3327 dl
->fd
= keep_fd
? fd
: -1;
3328 assert(super
->disks
== NULL
);
3330 serialcpy(dl
->serial
, serial
);
3333 fd2devname(fd
, name
);
3335 dl
->devname
= xstrdup(devname
);
3337 dl
->devname
= xstrdup(name
);
3339 /* look up this disk's index in the current anchor */
3340 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
3343 /* only set index on disks that are a member of a
3344 * populated contianer, i.e. one with raid_devs
3346 if (is_failed(&dl
->disk
))
3348 else if (is_spare(&dl
->disk
))
3356 /* When migrating map0 contains the 'destination' state while map1
3357 * contains the current state. When not migrating map0 contains the
3358 * current state. This routine assumes that map[0].map_state is set to
3359 * the current array state before being called.
3361 * Migration is indicated by one of the following states
3362 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
3363 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
3364 * map1state=unitialized)
3365 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
3367 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
3368 * map1state=degraded)
3369 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
3372 static void migrate(struct imsm_dev
*dev
, struct intel_super
*super
,
3373 __u8 to_state
, int migr_type
)
3375 struct imsm_map
*dest
;
3376 struct imsm_map
*src
= get_imsm_map(dev
, MAP_0
);
3378 dev
->vol
.migr_state
= 1;
3379 set_migr_type(dev
, migr_type
);
3380 dev
->vol
.curr_migr_unit
= 0;
3381 dest
= get_imsm_map(dev
, MAP_1
);
3383 /* duplicate and then set the target end state in map[0] */
3384 memcpy(dest
, src
, sizeof_imsm_map(src
));
3385 if ((migr_type
== MIGR_REBUILD
) ||
3386 (migr_type
== MIGR_GEN_MIGR
)) {
3390 for (i
= 0; i
< src
->num_members
; i
++) {
3391 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
3392 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
3396 if (migr_type
== MIGR_GEN_MIGR
)
3397 /* Clear migration record */
3398 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
3400 src
->map_state
= to_state
;
3403 static void end_migration(struct imsm_dev
*dev
, struct intel_super
*super
,
3406 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
3407 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
== 0 ?
3411 /* merge any IMSM_ORD_REBUILD bits that were not successfully
3412 * completed in the last migration.
3414 * FIXME add support for raid-level-migration
3416 if ((map_state
!= map
->map_state
) && (is_gen_migration(dev
) == 0) &&
3417 (prev
->map_state
!= IMSM_T_STATE_UNINITIALIZED
)) {
3418 /* when final map state is other than expected
3419 * merge maps (not for migration)
3423 for (i
= 0; i
< prev
->num_members
; i
++)
3424 for (j
= 0; j
< map
->num_members
; j
++)
3425 /* during online capacity expansion
3426 * disks position can be changed
3427 * if takeover is used
3429 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
3430 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
3431 map
->disk_ord_tbl
[j
] |=
3432 prev
->disk_ord_tbl
[i
];
3435 failed
= imsm_count_failed(super
, dev
, MAP_0
);
3436 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
3439 dev
->vol
.migr_state
= 0;
3440 set_migr_type(dev
, 0);
3441 dev
->vol
.curr_migr_unit
= 0;
3442 map
->map_state
= map_state
;
3446 static int parse_raid_devices(struct intel_super
*super
)
3449 struct imsm_dev
*dev_new
;
3450 size_t len
, len_migr
;
3452 size_t space_needed
= 0;
3453 struct imsm_super
*mpb
= super
->anchor
;
3455 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3456 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3457 struct intel_dev
*dv
;
3459 len
= sizeof_imsm_dev(dev_iter
, 0);
3460 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
3462 space_needed
+= len_migr
- len
;
3464 dv
= xmalloc(sizeof(*dv
));
3465 if (max_len
< len_migr
)
3467 if (max_len
> len_migr
)
3468 space_needed
+= max_len
- len_migr
;
3469 dev_new
= xmalloc(max_len
);
3470 imsm_copy_dev(dev_new
, dev_iter
);
3473 dv
->next
= super
->devlist
;
3474 super
->devlist
= dv
;
3477 /* ensure that super->buf is large enough when all raid devices
3480 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
3483 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
, 512);
3484 if (posix_memalign(&buf
, 512, len
) != 0)
3487 memcpy(buf
, super
->buf
, super
->len
);
3488 memset(buf
+ super
->len
, 0, len
- super
->len
);
3497 /* retrieve a pointer to the bbm log which starts after all raid devices */
3498 struct bbm_log
*__get_imsm_bbm_log(struct imsm_super
*mpb
)
3502 if (__le32_to_cpu(mpb
->bbm_log_size
)) {
3504 ptr
+= mpb
->mpb_size
- __le32_to_cpu(mpb
->bbm_log_size
);
3510 /*******************************************************************************
3511 * Function: check_mpb_migr_compatibility
3512 * Description: Function checks for unsupported migration features:
3513 * - migration optimization area (pba_of_lba0)
3514 * - descending reshape (ascending_migr)
3516 * super : imsm metadata information
3518 * 0 : migration is compatible
3519 * -1 : migration is not compatible
3520 ******************************************************************************/
3521 int check_mpb_migr_compatibility(struct intel_super
*super
)
3523 struct imsm_map
*map0
, *map1
;
3524 struct migr_record
*migr_rec
= super
->migr_rec
;
3527 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3528 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3531 dev_iter
->vol
.migr_state
== 1 &&
3532 dev_iter
->vol
.migr_type
== MIGR_GEN_MIGR
) {
3533 /* This device is migrating */
3534 map0
= get_imsm_map(dev_iter
, MAP_0
);
3535 map1
= get_imsm_map(dev_iter
, MAP_1
);
3536 if (pba_of_lba0(map0
) != pba_of_lba0(map1
))
3537 /* migration optimization area was used */
3539 if (migr_rec
->ascending_migr
== 0
3540 && migr_rec
->dest_depth_per_unit
> 0)
3541 /* descending reshape not supported yet */
3548 static void __free_imsm(struct intel_super
*super
, int free_disks
);
3550 /* load_imsm_mpb - read matrix metadata
3551 * allocates super->mpb to be freed by free_imsm
3553 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
3555 unsigned long long dsize
;
3556 unsigned long long sectors
;
3558 struct imsm_super
*anchor
;
3561 get_dev_size(fd
, NULL
, &dsize
);
3564 pr_err("%s: device to small for imsm\n",
3569 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0) {
3571 pr_err("Cannot seek to anchor block on %s: %s\n",
3572 devname
, strerror(errno
));
3576 if (posix_memalign((void**)&anchor
, 512, 512) != 0) {
3578 pr_err("Failed to allocate imsm anchor buffer"
3579 " on %s\n", devname
);
3582 if (read(fd
, anchor
, 512) != 512) {
3584 pr_err("Cannot read anchor block on %s: %s\n",
3585 devname
, strerror(errno
));
3590 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
3592 pr_err("no IMSM anchor on %s\n", devname
);
3597 __free_imsm(super
, 0);
3598 /* reload capability and hba */
3600 /* capability and hba must be updated with new super allocation */
3601 find_intel_hba_capability(fd
, super
, devname
);
3602 super
->len
= ROUND_UP(anchor
->mpb_size
, 512);
3603 if (posix_memalign(&super
->buf
, 512, super
->len
) != 0) {
3605 pr_err("unable to allocate %zu byte mpb buffer\n",
3610 memcpy(super
->buf
, anchor
, 512);
3612 sectors
= mpb_sectors(anchor
) - 1;
3615 if (posix_memalign(&super
->migr_rec_buf
, 512, MIGR_REC_BUF_SIZE
) != 0) {
3616 pr_err("%s could not allocate migr_rec buffer\n", __func__
);
3620 super
->clean_migration_record_by_mdmon
= 0;
3623 check_sum
= __gen_imsm_checksum(super
->anchor
);
3624 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3626 pr_err("IMSM checksum %x != %x on %s\n",
3628 __le32_to_cpu(super
->anchor
->check_sum
),
3636 /* read the extended mpb */
3637 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0) {
3639 pr_err("Cannot seek to extended mpb on %s: %s\n",
3640 devname
, strerror(errno
));
3644 if ((unsigned)read(fd
, super
->buf
+ 512, super
->len
- 512) != super
->len
- 512) {
3646 pr_err("Cannot read extended mpb on %s: %s\n",
3647 devname
, strerror(errno
));
3651 check_sum
= __gen_imsm_checksum(super
->anchor
);
3652 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3654 pr_err("IMSM checksum %x != %x on %s\n",
3655 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
3660 /* FIXME the BBM log is disk specific so we cannot use this global
3661 * buffer for all disks. Ok for now since we only look at the global
3662 * bbm_log_size parameter to gate assembly
3664 super
->bbm_log
= __get_imsm_bbm_log(super
->anchor
);
3669 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
);
3671 /* clears hi bits in metadata if MPB_ATTRIB_2TB_DISK not set */
3672 static void clear_hi(struct intel_super
*super
)
3674 struct imsm_super
*mpb
= super
->anchor
;
3676 if (mpb
->attributes
& MPB_ATTRIB_2TB_DISK
)
3678 for (i
= 0; i
< mpb
->num_disks
; ++i
) {
3679 struct imsm_disk
*disk
= &mpb
->disk
[i
];
3680 disk
->total_blocks_hi
= 0;
3682 for (i
= 0; i
< mpb
->num_raid_devs
; ++i
) {
3683 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3686 for (n
= 0; n
< 2; ++n
) {
3687 struct imsm_map
*map
= get_imsm_map(dev
, n
);
3690 map
->pba_of_lba0_hi
= 0;
3691 map
->blocks_per_member_hi
= 0;
3692 map
->num_data_stripes_hi
= 0;
3698 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3702 err
= load_imsm_mpb(fd
, super
, devname
);
3705 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
3708 err
= parse_raid_devices(super
);
3713 static void __free_imsm_disk(struct dl
*d
)
3725 static void free_imsm_disks(struct intel_super
*super
)
3729 while (super
->disks
) {
3731 super
->disks
= d
->next
;
3732 __free_imsm_disk(d
);
3734 while (super
->disk_mgmt_list
) {
3735 d
= super
->disk_mgmt_list
;
3736 super
->disk_mgmt_list
= d
->next
;
3737 __free_imsm_disk(d
);
3739 while (super
->missing
) {
3741 super
->missing
= d
->next
;
3742 __free_imsm_disk(d
);
3747 /* free all the pieces hanging off of a super pointer */
3748 static void __free_imsm(struct intel_super
*super
, int free_disks
)
3750 struct intel_hba
*elem
, *next
;
3756 /* unlink capability description */
3758 if (super
->migr_rec_buf
) {
3759 free(super
->migr_rec_buf
);
3760 super
->migr_rec_buf
= NULL
;
3763 free_imsm_disks(super
);
3764 free_devlist(super
);
3768 free((void *)elem
->path
);
3776 static void free_imsm(struct intel_super
*super
)
3778 __free_imsm(super
, 1);
3782 static void free_super_imsm(struct supertype
*st
)
3784 struct intel_super
*super
= st
->sb
;
3793 static struct intel_super
*alloc_super(void)
3795 struct intel_super
*super
= xcalloc(1, sizeof(*super
));
3797 super
->current_vol
= -1;
3798 super
->create_offset
= ~((unsigned long long) 0);
3803 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
3805 static int find_intel_hba_capability(int fd
, struct intel_super
*super
, char *devname
)
3807 struct sys_dev
*hba_name
;
3810 if ((fd
< 0) || check_env("IMSM_NO_PLATFORM")) {
3815 hba_name
= find_disk_attached_hba(fd
, NULL
);
3818 pr_err("%s is not attached to Intel(R) RAID controller.\n",
3822 rv
= attach_hba_to_super(super
, hba_name
);
3825 struct intel_hba
*hba
= super
->hba
;
3827 pr_err("%s is attached to Intel(R) %s RAID "
3828 "controller (%s),\n"
3829 " but the container is assigned to Intel(R) "
3830 "%s RAID controller (",
3833 hba_name
->pci_id
? : "Err!",
3834 get_sys_dev_type(hba_name
->type
));
3837 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
3839 fprintf(stderr
, ", ");
3843 fprintf(stderr
, ").\n"
3844 " Mixing devices attached to multiple controllers "
3845 "is not allowed.\n");
3849 super
->orom
= find_imsm_capability(hba_name
->type
);
3855 /* find_missing - helper routine for load_super_imsm_all that identifies
3856 * disks that have disappeared from the system. This routine relies on
3857 * the mpb being uptodate, which it is at load time.
3859 static int find_missing(struct intel_super
*super
)
3862 struct imsm_super
*mpb
= super
->anchor
;
3864 struct imsm_disk
*disk
;
3866 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3867 disk
= __get_imsm_disk(mpb
, i
);
3868 dl
= serial_to_dl(disk
->serial
, super
);
3872 dl
= xmalloc(sizeof(*dl
));
3876 dl
->devname
= xstrdup("missing");
3878 serialcpy(dl
->serial
, disk
->serial
);
3881 dl
->next
= super
->missing
;
3882 super
->missing
= dl
;
3889 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
3891 struct intel_disk
*idisk
= disk_list
;
3894 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
3896 idisk
= idisk
->next
;
3902 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
3903 struct intel_super
*super
,
3904 struct intel_disk
**disk_list
)
3906 struct imsm_disk
*d
= &super
->disks
->disk
;
3907 struct imsm_super
*mpb
= super
->anchor
;
3910 for (i
= 0; i
< tbl_size
; i
++) {
3911 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
3912 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
3914 if (tbl_mpb
->family_num
== mpb
->family_num
) {
3915 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
3916 dprintf("%s: mpb from %d:%d matches %d:%d\n",
3917 __func__
, super
->disks
->major
,
3918 super
->disks
->minor
,
3919 table
[i
]->disks
->major
,
3920 table
[i
]->disks
->minor
);
3924 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
3925 is_configured(d
) == is_configured(tbl_d
)) &&
3926 tbl_mpb
->generation_num
< mpb
->generation_num
) {
3927 /* current version of the mpb is a
3928 * better candidate than the one in
3929 * super_table, but copy over "cross
3930 * generational" status
3932 struct intel_disk
*idisk
;
3934 dprintf("%s: mpb from %d:%d replaces %d:%d\n",
3935 __func__
, super
->disks
->major
,
3936 super
->disks
->minor
,
3937 table
[i
]->disks
->major
,
3938 table
[i
]->disks
->minor
);
3940 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
3941 if (idisk
&& is_failed(&idisk
->disk
))
3942 tbl_d
->status
|= FAILED_DISK
;
3945 struct intel_disk
*idisk
;
3946 struct imsm_disk
*disk
;
3948 /* tbl_mpb is more up to date, but copy
3949 * over cross generational status before
3952 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
3953 if (disk
&& is_failed(disk
))
3954 d
->status
|= FAILED_DISK
;
3956 idisk
= disk_list_get(d
->serial
, *disk_list
);
3959 if (disk
&& is_configured(disk
))
3960 idisk
->disk
.status
|= CONFIGURED_DISK
;
3963 dprintf("%s: mpb from %d:%d prefer %d:%d\n",
3964 __func__
, super
->disks
->major
,
3965 super
->disks
->minor
,
3966 table
[i
]->disks
->major
,
3967 table
[i
]->disks
->minor
);
3975 table
[tbl_size
++] = super
;
3979 /* update/extend the merged list of imsm_disk records */
3980 for (j
= 0; j
< mpb
->num_disks
; j
++) {
3981 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
3982 struct intel_disk
*idisk
;
3984 idisk
= disk_list_get(disk
->serial
, *disk_list
);
3986 idisk
->disk
.status
|= disk
->status
;
3987 if (is_configured(&idisk
->disk
) ||
3988 is_failed(&idisk
->disk
))
3989 idisk
->disk
.status
&= ~(SPARE_DISK
);
3991 idisk
= xcalloc(1, sizeof(*idisk
));
3992 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
3993 idisk
->disk
= *disk
;
3994 idisk
->next
= *disk_list
;
3998 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
4005 static struct intel_super
*
4006 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
4009 struct imsm_super
*mpb
= super
->anchor
;
4013 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4014 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
4015 struct intel_disk
*idisk
;
4017 idisk
= disk_list_get(disk
->serial
, disk_list
);
4019 if (idisk
->owner
== owner
||
4020 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
4023 dprintf("%s: '%.16s' owner %d != %d\n",
4024 __func__
, disk
->serial
, idisk
->owner
,
4027 dprintf("%s: unknown disk %x [%d]: %.16s\n",
4028 __func__
, __le32_to_cpu(mpb
->family_num
), i
,
4034 if (ok_count
== mpb
->num_disks
)
4039 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
4041 struct intel_super
*s
;
4043 for (s
= super_list
; s
; s
= s
->next
) {
4044 if (family_num
!= s
->anchor
->family_num
)
4046 fprintf(stderr
, "Conflict, offlining family %#x on '%s'\n",
4047 __le32_to_cpu(family_num
), s
->disks
->devname
);
4051 static struct intel_super
*
4052 imsm_thunderdome(struct intel_super
**super_list
, int len
)
4054 struct intel_super
*super_table
[len
];
4055 struct intel_disk
*disk_list
= NULL
;
4056 struct intel_super
*champion
, *spare
;
4057 struct intel_super
*s
, **del
;
4062 memset(super_table
, 0, sizeof(super_table
));
4063 for (s
= *super_list
; s
; s
= s
->next
)
4064 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
4066 for (i
= 0; i
< tbl_size
; i
++) {
4067 struct imsm_disk
*d
;
4068 struct intel_disk
*idisk
;
4069 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
4072 d
= &s
->disks
->disk
;
4074 /* 'd' must appear in merged disk list for its
4075 * configuration to be valid
4077 idisk
= disk_list_get(d
->serial
, disk_list
);
4078 if (idisk
&& idisk
->owner
== i
)
4079 s
= validate_members(s
, disk_list
, i
);
4084 dprintf("%s: marking family: %#x from %d:%d offline\n",
4085 __func__
, mpb
->family_num
,
4086 super_table
[i
]->disks
->major
,
4087 super_table
[i
]->disks
->minor
);
4091 /* This is where the mdadm implementation differs from the Windows
4092 * driver which has no strict concept of a container. We can only
4093 * assemble one family from a container, so when returning a prodigal
4094 * array member to this system the code will not be able to disambiguate
4095 * the container contents that should be assembled ("foreign" versus
4096 * "local"). It requires user intervention to set the orig_family_num
4097 * to a new value to establish a new container. The Windows driver in
4098 * this situation fixes up the volume name in place and manages the
4099 * foreign array as an independent entity.
4104 for (i
= 0; i
< tbl_size
; i
++) {
4105 struct intel_super
*tbl_ent
= super_table
[i
];
4111 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
4116 if (s
&& !is_spare
) {
4117 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
4119 } else if (!s
&& !is_spare
)
4132 fprintf(stderr
, "Chose family %#x on '%s', "
4133 "assemble conflicts to new container with '--update=uuid'\n",
4134 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
4136 /* collect all dl's onto 'champion', and update them to
4137 * champion's version of the status
4139 for (s
= *super_list
; s
; s
= s
->next
) {
4140 struct imsm_super
*mpb
= champion
->anchor
;
4141 struct dl
*dl
= s
->disks
;
4146 mpb
->attributes
|= s
->anchor
->attributes
& MPB_ATTRIB_2TB_DISK
;
4148 for (i
= 0; i
< mpb
->num_disks
; i
++) {
4149 struct imsm_disk
*disk
;
4151 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
4154 /* only set index on disks that are a member of
4155 * a populated contianer, i.e. one with
4158 if (is_failed(&dl
->disk
))
4160 else if (is_spare(&dl
->disk
))
4166 if (i
>= mpb
->num_disks
) {
4167 struct intel_disk
*idisk
;
4169 idisk
= disk_list_get(dl
->serial
, disk_list
);
4170 if (idisk
&& is_spare(&idisk
->disk
) &&
4171 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
4179 dl
->next
= champion
->disks
;
4180 champion
->disks
= dl
;
4184 /* delete 'champion' from super_list */
4185 for (del
= super_list
; *del
; ) {
4186 if (*del
== champion
) {
4187 *del
= (*del
)->next
;
4190 del
= &(*del
)->next
;
4192 champion
->next
= NULL
;
4196 struct intel_disk
*idisk
= disk_list
;
4198 disk_list
= disk_list
->next
;
4207 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
);
4208 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
4209 int major
, int minor
, int keep_fd
);
4211 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4212 int *max
, int keep_fd
);
4215 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
4216 char *devname
, struct md_list
*devlist
,
4219 struct intel_super
*super_list
= NULL
;
4220 struct intel_super
*super
= NULL
;
4225 /* 'fd' is an opened container */
4226 err
= get_sra_super_block(fd
, &super_list
, devname
, &i
, keep_fd
);
4228 /* get super block from devlist devices */
4229 err
= get_devlist_super_block(devlist
, &super_list
, &i
, keep_fd
);
4232 /* all mpbs enter, maybe one leaves */
4233 super
= imsm_thunderdome(&super_list
, i
);
4239 if (find_missing(super
) != 0) {
4245 /* load migration record */
4246 err
= load_imsm_migr_rec(super
, NULL
);
4248 /* migration is in progress,
4249 * but migr_rec cannot be loaded,
4255 /* Check migration compatibility */
4256 if ((err
== 0) && (check_mpb_migr_compatibility(super
) != 0)) {
4257 pr_err("Unsupported migration detected");
4259 fprintf(stderr
, " on %s\n", devname
);
4261 fprintf(stderr
, " (IMSM).\n");
4270 while (super_list
) {
4271 struct intel_super
*s
= super_list
;
4273 super_list
= super_list
->next
;
4283 strcpy(st
->container_devnm
, fd2devnm(fd
));
4285 st
->container_devnm
[0] = 0;
4286 if (err
== 0 && st
->ss
== NULL
) {
4287 st
->ss
= &super_imsm
;
4288 st
->minor_version
= 0;
4289 st
->max_devs
= IMSM_MAX_DEVICES
;
4296 get_devlist_super_block(struct md_list
*devlist
, struct intel_super
**super_list
,
4297 int *max
, int keep_fd
)
4299 struct md_list
*tmpdev
;
4303 for (i
= 0, tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
4304 if (tmpdev
->used
!= 1)
4306 if (tmpdev
->container
== 1) {
4308 int fd
= dev_open(tmpdev
->devname
, O_RDONLY
|O_EXCL
);
4310 pr_err("cannot open device %s: %s\n",
4311 tmpdev
->devname
, strerror(errno
));
4315 err
= get_sra_super_block(fd
, super_list
,
4316 tmpdev
->devname
, &lmax
,
4325 int major
= major(tmpdev
->st_rdev
);
4326 int minor
= minor(tmpdev
->st_rdev
);
4327 err
= get_super_block(super_list
,
4344 static int get_super_block(struct intel_super
**super_list
, char *devnm
, char *devname
,
4345 int major
, int minor
, int keep_fd
)
4347 struct intel_super
*s
= NULL
;
4359 sprintf(nm
, "%d:%d", major
, minor
);
4360 dfd
= dev_open(nm
, O_RDWR
);
4366 find_intel_hba_capability(dfd
, s
, devname
);
4367 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4369 /* retry the load if we might have raced against mdmon */
4370 if (err
== 3 && devnm
&& mdmon_running(devnm
))
4371 for (retry
= 0; retry
< 3; retry
++) {
4373 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
4379 s
->next
= *super_list
;
4387 if ((dfd
>= 0) && (!keep_fd
))
4394 get_sra_super_block(int fd
, struct intel_super
**super_list
, char *devname
, int *max
, int keep_fd
)
4401 sra
= sysfs_read(fd
, NULL
, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
4405 if (sra
->array
.major_version
!= -1 ||
4406 sra
->array
.minor_version
!= -2 ||
4407 strcmp(sra
->text_version
, "imsm") != 0) {
4412 devnm
= fd2devnm(fd
);
4413 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
4414 if (get_super_block(super_list
, devnm
, devname
,
4415 sd
->disk
.major
, sd
->disk
.minor
, keep_fd
) != 0) {
4426 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
4428 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
, NULL
, 1);
4432 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
4434 struct intel_super
*super
;
4437 if (!st
->ignore_hw_compat
&& test_partition(fd
))
4438 /* IMSM not allowed on partitions */
4441 free_super_imsm(st
);
4443 super
= alloc_super();
4444 /* Load hba and capabilities if they exist.
4445 * But do not preclude loading metadata in case capabilities or hba are
4446 * non-compliant and ignore_hw_compat is set.
4448 rv
= find_intel_hba_capability(fd
, super
, devname
);
4449 /* no orom/efi or non-intel hba of the disk */
4450 if ((rv
!= 0) && (st
->ignore_hw_compat
== 0)) {
4452 pr_err("No OROM/EFI properties for %s\n", devname
);
4456 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
4460 pr_err("Failed to load all information "
4461 "sections on %s\n", devname
);
4467 if (st
->ss
== NULL
) {
4468 st
->ss
= &super_imsm
;
4469 st
->minor_version
= 0;
4470 st
->max_devs
= IMSM_MAX_DEVICES
;
4473 /* load migration record */
4474 if (load_imsm_migr_rec(super
, NULL
) == 0) {
4475 /* Check for unsupported migration features */
4476 if (check_mpb_migr_compatibility(super
) != 0) {
4477 pr_err("Unsupported migration detected");
4479 fprintf(stderr
, " on %s\n", devname
);
4481 fprintf(stderr
, " (IMSM).\n");
4489 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
4491 if (info
->level
== 1)
4493 return info
->chunk_size
>> 9;
4496 static unsigned long long info_to_blocks_per_member(mdu_array_info_t
*info
,
4497 unsigned long long size
)
4499 if (info
->level
== 1)
4502 return (size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
4505 static void imsm_update_version_info(struct intel_super
*super
)
4507 /* update the version and attributes */
4508 struct imsm_super
*mpb
= super
->anchor
;
4510 struct imsm_dev
*dev
;
4511 struct imsm_map
*map
;
4514 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4515 dev
= get_imsm_dev(super
, i
);
4516 map
= get_imsm_map(dev
, MAP_0
);
4517 if (__le32_to_cpu(dev
->size_high
) > 0)
4518 mpb
->attributes
|= MPB_ATTRIB_2TB
;
4520 /* FIXME detect when an array spans a port multiplier */
4522 mpb
->attributes
|= MPB_ATTRIB_PM
;
4525 if (mpb
->num_raid_devs
> 1 ||
4526 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
4527 version
= MPB_VERSION_ATTRIBS
;
4528 switch (get_imsm_raid_level(map
)) {
4529 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
4530 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
4531 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
4532 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
4535 if (map
->num_members
>= 5)
4536 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
4537 else if (dev
->status
== DEV_CLONE_N_GO
)
4538 version
= MPB_VERSION_CNG
;
4539 else if (get_imsm_raid_level(map
) == 5)
4540 version
= MPB_VERSION_RAID5
;
4541 else if (map
->num_members
>= 3)
4542 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
4543 else if (get_imsm_raid_level(map
) == 1)
4544 version
= MPB_VERSION_RAID1
;
4546 version
= MPB_VERSION_RAID0
;
4548 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
4552 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
4554 struct imsm_super
*mpb
= super
->anchor
;
4555 char *reason
= NULL
;
4558 if (strlen(name
) > MAX_RAID_SERIAL_LEN
)
4559 reason
= "must be 16 characters or less";
4561 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4562 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4564 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
4565 reason
= "already exists";
4570 if (reason
&& !quiet
)
4571 pr_err("imsm volume name %s\n", reason
);
4576 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
4577 unsigned long long size
, char *name
,
4578 char *homehost
, int *uuid
,
4579 long long data_offset
)
4581 /* We are creating a volume inside a pre-existing container.
4582 * so st->sb is already set.
4584 struct intel_super
*super
= st
->sb
;
4585 struct imsm_super
*mpb
= super
->anchor
;
4586 struct intel_dev
*dv
;
4587 struct imsm_dev
*dev
;
4588 struct imsm_vol
*vol
;
4589 struct imsm_map
*map
;
4590 int idx
= mpb
->num_raid_devs
;
4592 unsigned long long array_blocks
;
4593 size_t size_old
, size_new
;
4594 unsigned long long num_data_stripes
;
4596 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
4597 pr_err("This imsm-container already has the "
4598 "maximum of %d volumes\n", super
->orom
->vpa
);
4602 /* ensure the mpb is large enough for the new data */
4603 size_old
= __le32_to_cpu(mpb
->mpb_size
);
4604 size_new
= disks_to_mpb_size(info
->nr_disks
);
4605 if (size_new
> size_old
) {
4607 size_t size_round
= ROUND_UP(size_new
, 512);
4609 if (posix_memalign(&mpb_new
, 512, size_round
) != 0) {
4610 pr_err("could not allocate new mpb\n");
4613 if (posix_memalign(&super
->migr_rec_buf
, 512,
4614 MIGR_REC_BUF_SIZE
) != 0) {
4615 pr_err("%s could not allocate migr_rec buffer\n",
4622 memcpy(mpb_new
, mpb
, size_old
);
4625 super
->anchor
= mpb_new
;
4626 mpb
->mpb_size
= __cpu_to_le32(size_new
);
4627 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
4629 super
->current_vol
= idx
;
4631 /* handle 'failed_disks' by either:
4632 * a) create dummy disk entries in the table if this the first
4633 * volume in the array. We add them here as this is the only
4634 * opportunity to add them. add_to_super_imsm_volume()
4635 * handles the non-failed disks and continues incrementing
4637 * b) validate that 'failed_disks' matches the current number
4638 * of missing disks if the container is populated
4640 if (super
->current_vol
== 0) {
4642 for (i
= 0; i
< info
->failed_disks
; i
++) {
4643 struct imsm_disk
*disk
;
4646 disk
= __get_imsm_disk(mpb
, i
);
4647 disk
->status
= CONFIGURED_DISK
| FAILED_DISK
;
4648 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
4649 snprintf((char *) disk
->serial
, MAX_RAID_SERIAL_LEN
,
4652 find_missing(super
);
4657 for (d
= super
->missing
; d
; d
= d
->next
)
4659 if (info
->failed_disks
> missing
) {
4660 pr_err("unable to add 'missing' disk to container\n");
4665 if (!check_name(super
, name
, 0))
4667 dv
= xmalloc(sizeof(*dv
));
4668 dev
= xcalloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
4669 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
4670 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
4671 info
->layout
, info
->chunk_size
,
4673 /* round array size down to closest MB */
4674 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
4676 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
4677 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
4678 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
4680 vol
->migr_state
= 0;
4681 set_migr_type(dev
, MIGR_INIT
);
4682 vol
->dirty
= !info
->state
;
4683 vol
->curr_migr_unit
= 0;
4684 map
= get_imsm_map(dev
, MAP_0
);
4685 set_pba_of_lba0(map
, super
->create_offset
);
4686 set_blocks_per_member(map
, info_to_blocks_per_member(info
, size
));
4687 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
4688 map
->failed_disk_num
= ~0;
4689 if (info
->level
> 0)
4690 map
->map_state
= (info
->state
? IMSM_T_STATE_NORMAL
4691 : IMSM_T_STATE_UNINITIALIZED
);
4693 map
->map_state
= info
->failed_disks
? IMSM_T_STATE_FAILED
:
4694 IMSM_T_STATE_NORMAL
;
4697 if (info
->level
== 1 && info
->raid_disks
> 2) {
4700 pr_err("imsm does not support more than 2 disks"
4701 "in a raid1 volume\n");
4705 map
->raid_level
= info
->level
;
4706 if (info
->level
== 10) {
4707 map
->raid_level
= 1;
4708 map
->num_domains
= info
->raid_disks
/ 2;
4709 } else if (info
->level
== 1)
4710 map
->num_domains
= info
->raid_disks
;
4712 map
->num_domains
= 1;
4714 /* info->size is only int so use the 'size' parameter instead */
4715 num_data_stripes
= (size
* 2) / info_to_blocks_per_strip(info
);
4716 num_data_stripes
/= map
->num_domains
;
4717 set_num_data_stripes(map
, num_data_stripes
);
4719 map
->num_members
= info
->raid_disks
;
4720 for (i
= 0; i
< map
->num_members
; i
++) {
4721 /* initialized in add_to_super */
4722 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
4724 mpb
->num_raid_devs
++;
4727 dv
->index
= super
->current_vol
;
4728 dv
->next
= super
->devlist
;
4729 super
->devlist
= dv
;
4731 imsm_update_version_info(super
);
4736 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
4737 unsigned long long size
, char *name
,
4738 char *homehost
, int *uuid
,
4739 unsigned long long data_offset
)
4741 /* This is primarily called by Create when creating a new array.
4742 * We will then get add_to_super called for each component, and then
4743 * write_init_super called to write it out to each device.
4744 * For IMSM, Create can create on fresh devices or on a pre-existing
4746 * To create on a pre-existing array a different method will be called.
4747 * This one is just for fresh drives.
4749 struct intel_super
*super
;
4750 struct imsm_super
*mpb
;
4754 if (data_offset
!= INVALID_SECTORS
) {
4755 pr_err("data-offset not supported by imsm\n");
4760 return init_super_imsm_volume(st
, info
, size
, name
, homehost
, uuid
,
4764 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
4768 super
= alloc_super();
4769 if (super
&& posix_memalign(&super
->buf
, 512, mpb_size
) != 0) {
4774 pr_err("%s could not allocate superblock\n", __func__
);
4777 if (posix_memalign(&super
->migr_rec_buf
, 512, MIGR_REC_BUF_SIZE
) != 0) {
4778 pr_err("%s could not allocate migr_rec buffer\n", __func__
);
4783 memset(super
->buf
, 0, mpb_size
);
4785 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
4789 /* zeroing superblock */
4793 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
4795 version
= (char *) mpb
->sig
;
4796 strcpy(version
, MPB_SIGNATURE
);
4797 version
+= strlen(MPB_SIGNATURE
);
4798 strcpy(version
, MPB_VERSION_RAID0
);
4804 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
4805 int fd
, char *devname
)
4807 struct intel_super
*super
= st
->sb
;
4808 struct imsm_super
*mpb
= super
->anchor
;
4809 struct imsm_disk
*_disk
;
4810 struct imsm_dev
*dev
;
4811 struct imsm_map
*map
;
4815 dev
= get_imsm_dev(super
, super
->current_vol
);
4816 map
= get_imsm_map(dev
, MAP_0
);
4818 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
4819 pr_err("%s: Cannot add spare devices to IMSM volume\n",
4825 /* we're doing autolayout so grab the pre-marked (in
4826 * validate_geometry) raid_disk
4828 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4829 if (dl
->raiddisk
== dk
->raid_disk
)
4832 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4833 if (dl
->major
== dk
->major
&&
4834 dl
->minor
== dk
->minor
)
4839 pr_err("%s is not a member of the same container\n", devname
);
4843 /* add a pristine spare to the metadata */
4844 if (dl
->index
< 0) {
4845 dl
->index
= super
->anchor
->num_disks
;
4846 super
->anchor
->num_disks
++;
4848 /* Check the device has not already been added */
4849 slot
= get_imsm_disk_slot(map
, dl
->index
);
4851 (get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
) & IMSM_ORD_REBUILD
) == 0) {
4852 pr_err("%s has been included in this array twice\n",
4856 set_imsm_ord_tbl_ent(map
, dk
->raid_disk
, dl
->index
);
4857 dl
->disk
.status
= CONFIGURED_DISK
;
4859 /* update size of 'missing' disks to be at least as large as the
4860 * largest acitve member (we only have dummy missing disks when
4861 * creating the first volume)
4863 if (super
->current_vol
== 0) {
4864 for (df
= super
->missing
; df
; df
= df
->next
) {
4865 if (total_blocks(&dl
->disk
) > total_blocks(&df
->disk
))
4866 set_total_blocks(&df
->disk
, total_blocks(&dl
->disk
));
4867 _disk
= __get_imsm_disk(mpb
, df
->index
);
4872 /* refresh unset/failed slots to point to valid 'missing' entries */
4873 for (df
= super
->missing
; df
; df
= df
->next
)
4874 for (slot
= 0; slot
< mpb
->num_disks
; slot
++) {
4875 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
4877 if ((ord
& IMSM_ORD_REBUILD
) == 0)
4879 set_imsm_ord_tbl_ent(map
, slot
, df
->index
| IMSM_ORD_REBUILD
);
4880 if (is_gen_migration(dev
)) {
4881 struct imsm_map
*map2
= get_imsm_map(dev
,
4883 int slot2
= get_imsm_disk_slot(map2
, df
->index
);
4884 if ((slot2
< map2
->num_members
) &&
4886 __u32 ord2
= get_imsm_ord_tbl_ent(dev
,
4889 if ((unsigned)df
->index
==
4891 set_imsm_ord_tbl_ent(map2
,
4897 dprintf("set slot:%d to missing disk:%d\n", slot
, df
->index
);
4901 /* if we are creating the first raid device update the family number */
4902 if (super
->current_vol
== 0) {
4904 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
4906 _disk
= __get_imsm_disk(mpb
, dl
->index
);
4907 if (!_dev
|| !_disk
) {
4908 pr_err("BUG mpb setup error\n");
4914 sum
+= __gen_imsm_checksum(mpb
);
4915 mpb
->family_num
= __cpu_to_le32(sum
);
4916 mpb
->orig_family_num
= mpb
->family_num
;
4918 super
->current_disk
= dl
;
4923 * Function marks disk as spare and restores disk serial
4924 * in case it was previously marked as failed by takeover operation
4926 * -1 : critical error
4927 * 0 : disk is marked as spare but serial is not set
4930 int mark_spare(struct dl
*disk
)
4932 __u8 serial
[MAX_RAID_SERIAL_LEN
];
4939 if (!imsm_read_serial(disk
->fd
, NULL
, serial
)) {
4940 /* Restore disk serial number, because takeover marks disk
4941 * as failed and adds to serial ':0' before it becomes
4944 serialcpy(disk
->serial
, serial
);
4945 serialcpy(disk
->disk
.serial
, serial
);
4948 disk
->disk
.status
= SPARE_DISK
;
4954 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
4955 int fd
, char *devname
,
4956 unsigned long long data_offset
)
4958 struct intel_super
*super
= st
->sb
;
4960 unsigned long long size
;
4965 /* If we are on an RAID enabled platform check that the disk is
4966 * attached to the raid controller.
4967 * We do not need to test disks attachment for container based additions,
4968 * they shall be already tested when container was created/assembled.
4970 rv
= find_intel_hba_capability(fd
, super
, devname
);
4971 /* no orom/efi or non-intel hba of the disk */
4973 dprintf("capability: %p fd: %d ret: %d\n",
4974 super
->orom
, fd
, rv
);
4978 if (super
->current_vol
>= 0)
4979 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
4982 dd
= xcalloc(sizeof(*dd
), 1);
4983 dd
->major
= major(stb
.st_rdev
);
4984 dd
->minor
= minor(stb
.st_rdev
);
4985 dd
->devname
= devname
? xstrdup(devname
) : NULL
;
4988 dd
->action
= DISK_ADD
;
4989 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
4991 pr_err("failed to retrieve scsi serial, aborting\n");
4996 get_dev_size(fd
, NULL
, &size
);
4998 serialcpy(dd
->disk
.serial
, dd
->serial
);
4999 set_total_blocks(&dd
->disk
, size
);
5000 if (__le32_to_cpu(dd
->disk
.total_blocks_hi
) > 0) {
5001 struct imsm_super
*mpb
= super
->anchor
;
5002 mpb
->attributes
|= MPB_ATTRIB_2TB_DISK
;
5005 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
5006 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
5008 dd
->disk
.scsi_id
= __cpu_to_le32(0);
5010 if (st
->update_tail
) {
5011 dd
->next
= super
->disk_mgmt_list
;
5012 super
->disk_mgmt_list
= dd
;
5014 dd
->next
= super
->disks
;
5016 super
->updates_pending
++;
5023 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
5025 struct intel_super
*super
= st
->sb
;
5028 /* remove from super works only in mdmon - for communication
5029 * manager - monitor. Check if communication memory buffer
5032 if (!st
->update_tail
) {
5033 pr_err("%s shall be used in mdmon context only"
5034 "(line %d).\n", __func__
, __LINE__
);
5037 dd
= xcalloc(1, sizeof(*dd
));
5038 dd
->major
= dk
->major
;
5039 dd
->minor
= dk
->minor
;
5042 dd
->action
= DISK_REMOVE
;
5044 dd
->next
= super
->disk_mgmt_list
;
5045 super
->disk_mgmt_list
= dd
;
5051 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
5055 struct imsm_super anchor
;
5056 } spare_record
__attribute__ ((aligned(512)));
5058 /* spare records have their own family number and do not have any defined raid
5061 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
5063 struct imsm_super
*mpb
= super
->anchor
;
5064 struct imsm_super
*spare
= &spare_record
.anchor
;
5068 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
)),
5069 spare
->generation_num
= __cpu_to_le32(1UL),
5070 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
5071 spare
->num_disks
= 1,
5072 spare
->num_raid_devs
= 0,
5073 spare
->cache_size
= mpb
->cache_size
,
5074 spare
->pwr_cycle_count
= __cpu_to_le32(1),
5076 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
5077 MPB_SIGNATURE MPB_VERSION_RAID0
);
5079 for (d
= super
->disks
; d
; d
= d
->next
) {
5083 spare
->disk
[0] = d
->disk
;
5084 if (__le32_to_cpu(d
->disk
.total_blocks_hi
) > 0)
5085 spare
->attributes
|= MPB_ATTRIB_2TB_DISK
;
5087 sum
= __gen_imsm_checksum(spare
);
5088 spare
->family_num
= __cpu_to_le32(sum
);
5089 spare
->orig_family_num
= 0;
5090 sum
= __gen_imsm_checksum(spare
);
5091 spare
->check_sum
= __cpu_to_le32(sum
);
5093 if (store_imsm_mpb(d
->fd
, spare
)) {
5094 fprintf(stderr
, "%s: failed for device %d:%d %s\n",
5095 __func__
, d
->major
, d
->minor
, strerror(errno
));
5107 static int write_super_imsm(struct supertype
*st
, int doclose
)
5109 struct intel_super
*super
= st
->sb
;
5110 struct imsm_super
*mpb
= super
->anchor
;
5116 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
5118 int clear_migration_record
= 1;
5120 /* 'generation' is incremented everytime the metadata is written */
5121 generation
= __le32_to_cpu(mpb
->generation_num
);
5123 mpb
->generation_num
= __cpu_to_le32(generation
);
5125 /* fix up cases where previous mdadm releases failed to set
5128 if (mpb
->orig_family_num
== 0)
5129 mpb
->orig_family_num
= mpb
->family_num
;
5131 for (d
= super
->disks
; d
; d
= d
->next
) {
5135 mpb
->disk
[d
->index
] = d
->disk
;
5139 for (d
= super
->missing
; d
; d
= d
->next
) {
5140 mpb
->disk
[d
->index
] = d
->disk
;
5143 mpb
->num_disks
= num_disks
;
5144 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
5146 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5147 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
5148 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
5150 imsm_copy_dev(dev
, dev2
);
5151 mpb_size
+= sizeof_imsm_dev(dev
, 0);
5153 if (is_gen_migration(dev2
))
5154 clear_migration_record
= 0;
5156 mpb_size
+= __le32_to_cpu(mpb
->bbm_log_size
);
5157 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
5159 /* recalculate checksum */
5160 sum
= __gen_imsm_checksum(mpb
);
5161 mpb
->check_sum
= __cpu_to_le32(sum
);
5163 if (super
->clean_migration_record_by_mdmon
) {
5164 clear_migration_record
= 1;
5165 super
->clean_migration_record_by_mdmon
= 0;
5167 if (clear_migration_record
)
5168 memset(super
->migr_rec_buf
, 0, MIGR_REC_BUF_SIZE
);
5170 /* write the mpb for disks that compose raid devices */
5171 for (d
= super
->disks
; d
; d
= d
->next
) {
5172 if (d
->index
< 0 || is_failed(&d
->disk
))
5175 if (clear_migration_record
) {
5176 unsigned long long dsize
;
5178 get_dev_size(d
->fd
, NULL
, &dsize
);
5179 if (lseek64(d
->fd
, dsize
- 512, SEEK_SET
) >= 0) {
5180 if (write(d
->fd
, super
->migr_rec_buf
,
5181 MIGR_REC_BUF_SIZE
) != MIGR_REC_BUF_SIZE
)
5182 perror("Write migr_rec failed");
5186 if (store_imsm_mpb(d
->fd
, mpb
))
5188 "%s: failed for device %d:%d (fd: %d)%s\n",
5189 __func__
, d
->major
, d
->minor
,
5190 d
->fd
, strerror(errno
));
5199 return write_super_imsm_spares(super
, doclose
);
5205 static int create_array(struct supertype
*st
, int dev_idx
)
5208 struct imsm_update_create_array
*u
;
5209 struct intel_super
*super
= st
->sb
;
5210 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
5211 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
5212 struct disk_info
*inf
;
5213 struct imsm_disk
*disk
;
5216 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
5217 sizeof(*inf
) * map
->num_members
;
5219 u
->type
= update_create_array
;
5220 u
->dev_idx
= dev_idx
;
5221 imsm_copy_dev(&u
->dev
, dev
);
5222 inf
= get_disk_info(u
);
5223 for (i
= 0; i
< map
->num_members
; i
++) {
5224 int idx
= get_imsm_disk_idx(dev
, i
, MAP_X
);
5226 disk
= get_imsm_disk(super
, idx
);
5227 serialcpy(inf
[i
].serial
, disk
->serial
);
5229 append_metadata_update(st
, u
, len
);
5234 static int mgmt_disk(struct supertype
*st
)
5236 struct intel_super
*super
= st
->sb
;
5238 struct imsm_update_add_remove_disk
*u
;
5240 if (!super
->disk_mgmt_list
)
5245 u
->type
= update_add_remove_disk
;
5246 append_metadata_update(st
, u
, len
);
5251 static int write_init_super_imsm(struct supertype
*st
)
5253 struct intel_super
*super
= st
->sb
;
5254 int current_vol
= super
->current_vol
;
5256 /* we are done with current_vol reset it to point st at the container */
5257 super
->current_vol
= -1;
5259 if (st
->update_tail
) {
5260 /* queue the recently created array / added disk
5261 * as a metadata update */
5264 /* determine if we are creating a volume or adding a disk */
5265 if (current_vol
< 0) {
5266 /* in the mgmt (add/remove) disk case we are running
5267 * in mdmon context, so don't close fd's
5269 return mgmt_disk(st
);
5271 rv
= create_array(st
, current_vol
);
5276 for (d
= super
->disks
; d
; d
= d
->next
)
5277 Kill(d
->devname
, NULL
, 0, -1, 1);
5278 return write_super_imsm(st
, 1);
5283 static int store_super_imsm(struct supertype
*st
, int fd
)
5285 struct intel_super
*super
= st
->sb
;
5286 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
5292 return store_imsm_mpb(fd
, mpb
);
5298 static int imsm_bbm_log_size(struct imsm_super
*mpb
)
5300 return __le32_to_cpu(mpb
->bbm_log_size
);
5304 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
5305 int layout
, int raiddisks
, int chunk
,
5306 unsigned long long size
,
5307 unsigned long long data_offset
,
5309 unsigned long long *freesize
,
5313 unsigned long long ldsize
;
5314 struct intel_super
*super
=NULL
;
5317 if (level
!= LEVEL_CONTAINER
)
5322 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
5325 pr_err("imsm: Cannot open %s: %s\n",
5326 dev
, strerror(errno
));
5329 if (!get_dev_size(fd
, dev
, &ldsize
)) {
5334 /* capabilities retrieve could be possible
5335 * note that there is no fd for the disks in array.
5337 super
= alloc_super();
5338 rv
= find_intel_hba_capability(fd
, super
, verbose
> 0 ? dev
: NULL
);
5342 fd2devname(fd
, str
);
5343 dprintf("validate_geometry_imsm_container: fd: %d %s orom: %p rv: %d raiddisk: %d\n",
5344 fd
, str
, super
->orom
, rv
, raiddisks
);
5346 /* no orom/efi or non-intel hba of the disk */
5353 if (raiddisks
> super
->orom
->tds
) {
5355 pr_err("%d exceeds maximum number of"
5356 " platform supported disks: %d\n",
5357 raiddisks
, super
->orom
->tds
);
5361 if ((super
->orom
->attr
& IMSM_OROM_ATTR_2TB_DISK
) == 0 &&
5362 (ldsize
>> 9) >> 32 > 0) {
5364 pr_err("%s exceeds maximum platform supported size\n", dev
);
5370 *freesize
= avail_size_imsm(st
, ldsize
>> 9, data_offset
);
5376 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
5378 const unsigned long long base_start
= e
[*idx
].start
;
5379 unsigned long long end
= base_start
+ e
[*idx
].size
;
5382 if (base_start
== end
)
5386 for (i
= *idx
; i
< num_extents
; i
++) {
5387 /* extend overlapping extents */
5388 if (e
[i
].start
>= base_start
&&
5389 e
[i
].start
<= end
) {
5392 if (e
[i
].start
+ e
[i
].size
> end
)
5393 end
= e
[i
].start
+ e
[i
].size
;
5394 } else if (e
[i
].start
> end
) {
5400 return end
- base_start
;
5403 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
5405 /* build a composite disk with all known extents and generate a new
5406 * 'maxsize' given the "all disks in an array must share a common start
5407 * offset" constraint
5409 struct extent
*e
= xcalloc(sum_extents
, sizeof(*e
));
5413 unsigned long long pos
;
5414 unsigned long long start
= 0;
5415 unsigned long long maxsize
;
5416 unsigned long reserve
;
5418 /* coalesce and sort all extents. also, check to see if we need to
5419 * reserve space between member arrays
5422 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5425 for (i
= 0; i
< dl
->extent_cnt
; i
++)
5428 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
5433 while (i
< sum_extents
) {
5434 e
[j
].start
= e
[i
].start
;
5435 e
[j
].size
= find_size(e
, &i
, sum_extents
);
5437 if (e
[j
-1].size
== 0)
5446 unsigned long long esize
;
5448 esize
= e
[i
].start
- pos
;
5449 if (esize
>= maxsize
) {
5454 pos
= e
[i
].start
+ e
[i
].size
;
5456 } while (e
[i
-1].size
);
5462 /* FIXME assumes volume at offset 0 is the first volume in a
5465 if (start_extent
> 0)
5466 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
5470 if (maxsize
< reserve
)
5473 super
->create_offset
= ~((unsigned long long) 0);
5474 if (start
+ reserve
> super
->create_offset
)
5475 return 0; /* start overflows create_offset */
5476 super
->create_offset
= start
+ reserve
;
5478 return maxsize
- reserve
;
5481 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
5483 if (level
< 0 || level
== 6 || level
== 4)
5486 /* if we have an orom prevent invalid raid levels */
5489 case 0: return imsm_orom_has_raid0(orom
);
5492 return imsm_orom_has_raid1e(orom
);
5493 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
5494 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
5495 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
5498 return 1; /* not on an Intel RAID platform so anything goes */
5505 active_arrays_by_format(char *name
, char* hba
, struct md_list
**devlist
,
5506 int dpa
, int verbose
)
5508 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
5509 struct mdstat_ent
*memb
= NULL
;
5512 struct md_list
*dv
= NULL
;
5515 for (memb
= mdstat
; memb
; memb
= memb
->next
) {
5516 if (memb
->metadata_version
&&
5517 (strncmp(memb
->metadata_version
, "external:", 9) == 0) &&
5518 (strcmp(&memb
->metadata_version
[9], name
) == 0) &&
5519 !is_subarray(memb
->metadata_version
+9) &&
5521 struct dev_member
*dev
= memb
->members
;
5523 while(dev
&& (fd
< 0)) {
5524 char *path
= xmalloc(strlen(dev
->name
) + strlen("/dev/") + 1);
5525 num
= sprintf(path
, "%s%s", "/dev/", dev
->name
);
5527 fd
= open(path
, O_RDONLY
, 0);
5528 if ((num
<= 0) || (fd
< 0)) {
5529 pr_vrb(": Cannot open %s: %s\n",
5530 dev
->name
, strerror(errno
));
5536 if ((fd
>= 0) && disk_attached_to_hba(fd
, hba
)) {
5537 struct mdstat_ent
*vol
;
5538 for (vol
= mdstat
; vol
; vol
= vol
->next
) {
5539 if ((vol
->active
> 0) &&
5540 vol
->metadata_version
&&
5541 is_container_member(vol
, memb
->dev
)) {
5546 if (*devlist
&& (found
< dpa
)) {
5547 dv
= xcalloc(1, sizeof(*dv
));
5548 dv
->devname
= xmalloc(strlen(memb
->dev
) + strlen("/dev/") + 1);
5549 sprintf(dv
->devname
, "%s%s", "/dev/", memb
->dev
);
5552 dv
->next
= *devlist
;
5560 free_mdstat(mdstat
);
5565 static struct md_list
*
5566 get_loop_devices(void)
5569 struct md_list
*devlist
= NULL
;
5570 struct md_list
*dv
= NULL
;
5572 for(i
= 0; i
< 12; i
++) {
5573 dv
= xcalloc(1, sizeof(*dv
));
5574 dv
->devname
= xmalloc(40);
5575 sprintf(dv
->devname
, "/dev/loop%d", i
);
5583 static struct md_list
*
5584 get_devices(const char *hba_path
)
5586 struct md_list
*devlist
= NULL
;
5587 struct md_list
*dv
= NULL
;
5593 devlist
= get_loop_devices();
5596 /* scroll through /sys/dev/block looking for devices attached to
5599 dir
= opendir("/sys/dev/block");
5600 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
5605 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
5607 path
= devt_to_devpath(makedev(major
, minor
));
5610 if (!path_attached_to_hba(path
, hba_path
)) {
5617 fd
= dev_open(ent
->d_name
, O_RDONLY
);
5619 fd2devname(fd
, buf
);
5622 pr_err("cannot open device: %s\n",
5628 dv
= xcalloc(1, sizeof(*dv
));
5629 dv
->devname
= xstrdup(buf
);
5636 devlist
= devlist
->next
;
5646 count_volumes_list(struct md_list
*devlist
, char *homehost
,
5647 int verbose
, int *found
)
5649 struct md_list
*tmpdev
;
5651 struct supertype
*st
= NULL
;
5653 /* first walk the list of devices to find a consistent set
5654 * that match the criterea, if that is possible.
5655 * We flag the ones we like with 'used'.
5658 st
= match_metadata_desc_imsm("imsm");
5660 pr_vrb(": cannot allocate memory for imsm supertype\n");
5664 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5665 char *devname
= tmpdev
->devname
;
5667 struct supertype
*tst
;
5669 if (tmpdev
->used
> 1)
5671 tst
= dup_super(st
);
5673 pr_vrb(": cannot allocate memory for imsm supertype\n");
5676 tmpdev
->container
= 0;
5677 dfd
= dev_open(devname
, O_RDONLY
|O_EXCL
);
5679 dprintf(": cannot open device %s: %s\n",
5680 devname
, strerror(errno
));
5682 } else if (fstat(dfd
, &stb
)< 0) {
5684 dprintf(": fstat failed for %s: %s\n",
5685 devname
, strerror(errno
));
5687 } else if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
5688 dprintf(": %s is not a block device.\n",
5691 } else if (must_be_container(dfd
)) {
5692 struct supertype
*cst
;
5693 cst
= super_by_fd(dfd
, NULL
);
5695 dprintf(": cannot recognize container type %s\n",
5698 } else if (tst
->ss
!= st
->ss
) {
5699 dprintf(": non-imsm container - ignore it: %s\n",
5702 } else if (!tst
->ss
->load_container
||
5703 tst
->ss
->load_container(tst
, dfd
, NULL
))
5706 tmpdev
->container
= 1;
5709 cst
->ss
->free_super(cst
);
5711 tmpdev
->st_rdev
= stb
.st_rdev
;
5712 if (tst
->ss
->load_super(tst
,dfd
, NULL
)) {
5713 dprintf(": no RAID superblock on %s\n",
5716 } else if (tst
->ss
->compare_super
== NULL
) {
5717 dprintf(": Cannot assemble %s metadata on %s\n",
5718 tst
->ss
->name
, devname
);
5724 if (tmpdev
->used
== 2 || tmpdev
->used
== 4) {
5725 /* Ignore unrecognised devices during auto-assembly */
5730 tst
->ss
->getinfo_super(tst
, &info
, NULL
);
5732 if (st
->minor_version
== -1)
5733 st
->minor_version
= tst
->minor_version
;
5735 if (memcmp(info
.uuid
, uuid_zero
,
5736 sizeof(int[4])) == 0) {
5737 /* this is a floating spare. It cannot define
5738 * an array unless there are no more arrays of
5739 * this type to be found. It can be included
5740 * in an array of this type though.
5746 if (st
->ss
!= tst
->ss
||
5747 st
->minor_version
!= tst
->minor_version
||
5748 st
->ss
->compare_super(st
, tst
) != 0) {
5749 /* Some mismatch. If exactly one array matches this host,
5750 * we can resolve on that one.
5751 * Or, if we are auto assembling, we just ignore the second
5754 dprintf(": superblock on %s doesn't match others - assembly aborted\n",
5760 dprintf("found: devname: %s\n", devname
);
5764 tst
->ss
->free_super(tst
);
5768 if ((err
= load_super_imsm_all(st
, -1, &st
->sb
, NULL
, devlist
, 0)) == 0) {
5769 struct mdinfo
*iter
, *head
= st
->ss
->container_content(st
, NULL
);
5770 for (iter
= head
; iter
; iter
= iter
->next
) {
5771 dprintf("content->text_version: %s vol\n",
5772 iter
->text_version
);
5773 if (iter
->array
.state
& (1<<MD_SB_BLOCK_VOLUME
)) {
5774 /* do not assemble arrays with unsupported
5776 dprintf(": Cannot activate member %s.\n",
5777 iter
->text_version
);
5784 dprintf(" no valid super block on device list: err: %d %p\n",
5788 dprintf(" no more devices to examin\n");
5791 for (tmpdev
= devlist
; tmpdev
; tmpdev
= tmpdev
->next
) {
5792 if ((tmpdev
->used
== 1) && (tmpdev
->found
)) {
5794 if (count
< tmpdev
->found
)
5797 count
-= tmpdev
->found
;
5800 if (tmpdev
->used
== 1)
5805 st
->ss
->free_super(st
);
5811 count_volumes(char *hba
, int dpa
, int verbose
)
5813 struct md_list
*devlist
= NULL
;
5817 devlist
= get_devices(hba
);
5818 /* if no intel devices return zero volumes */
5819 if (devlist
== NULL
)
5822 count
= active_arrays_by_format("imsm", hba
, &devlist
, dpa
, verbose
);
5823 dprintf(" path: %s active arrays: %d\n", hba
, count
);
5824 if (devlist
== NULL
)
5828 count
+= count_volumes_list(devlist
,
5832 dprintf("found %d count: %d\n", found
, count
);
5835 dprintf("path: %s total number of volumes: %d\n", hba
, count
);
5838 struct md_list
*dv
= devlist
;
5839 devlist
= devlist
->next
;
5846 static int imsm_default_chunk(const struct imsm_orom
*orom
)
5848 /* up to 512 if the plaform supports it, otherwise the platform max.
5849 * 128 if no platform detected
5851 int fs
= max(7, orom
? fls(orom
->sss
) : 0);
5853 return min(512, (1 << fs
));
5857 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
5858 int raiddisks
, int *chunk
, unsigned long long size
, int verbose
)
5860 /* check/set platform and metadata limits/defaults */
5861 if (super
->orom
&& raiddisks
> super
->orom
->dpa
) {
5862 pr_vrb(": platform supports a maximum of %d disks per array\n",
5867 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
5868 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
5869 pr_vrb(": platform does not support raid%d with %d disk%s\n",
5870 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
5874 if (chunk
&& (*chunk
== 0 || *chunk
== UnSet
))
5875 *chunk
= imsm_default_chunk(super
->orom
);
5877 if (super
->orom
&& chunk
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
5878 pr_vrb(": platform does not support a chunk size of: "
5883 if (layout
!= imsm_level_to_layout(level
)) {
5885 pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
5886 else if (level
== 10)
5887 pr_vrb(": imsm raid 10 only supports the n2 layout\n");
5889 pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
5894 if (super
->orom
&& (super
->orom
->attr
& IMSM_OROM_ATTR_2TB
) == 0 && chunk
&&
5895 (calc_array_size(level
, raiddisks
, layout
, *chunk
, size
) >> 32) > 0) {
5896 pr_vrb(": platform does not support a volume size over 2TB\n");
5902 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
5903 * FIX ME add ahci details
5905 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
5906 int layout
, int raiddisks
, int *chunk
,
5907 unsigned long long size
,
5908 unsigned long long data_offset
,
5910 unsigned long long *freesize
,
5914 struct intel_super
*super
= st
->sb
;
5915 struct imsm_super
*mpb
;
5917 unsigned long long pos
= 0;
5918 unsigned long long maxsize
;
5922 /* We must have the container info already read in. */
5926 mpb
= super
->anchor
;
5928 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, size
, verbose
)) {
5929 pr_err("RAID gemetry validation failed. "
5930 "Cannot proceed with the action(s).\n");
5934 /* General test: make sure there is space for
5935 * 'raiddisks' device extents of size 'size' at a given
5938 unsigned long long minsize
= size
;
5939 unsigned long long start_offset
= MaxSector
;
5942 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
5943 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5948 e
= get_extents(super
, dl
);
5951 unsigned long long esize
;
5952 esize
= e
[i
].start
- pos
;
5953 if (esize
>= minsize
)
5955 if (found
&& start_offset
== MaxSector
) {
5958 } else if (found
&& pos
!= start_offset
) {
5962 pos
= e
[i
].start
+ e
[i
].size
;
5964 } while (e
[i
-1].size
);
5969 if (dcnt
< raiddisks
) {
5971 pr_err("imsm: Not enough "
5972 "devices with space for this array "
5980 /* This device must be a member of the set */
5981 if (stat(dev
, &stb
) < 0)
5983 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
5985 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5986 if (dl
->major
== (int)major(stb
.st_rdev
) &&
5987 dl
->minor
== (int)minor(stb
.st_rdev
))
5992 pr_err("%s is not in the "
5993 "same imsm set\n", dev
);
5995 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
5996 /* If a volume is present then the current creation attempt
5997 * cannot incorporate new spares because the orom may not
5998 * understand this configuration (all member disks must be
5999 * members of each array in the container).
6001 pr_err("%s is a spare and a volume"
6002 " is already defined for this container\n", dev
);
6003 pr_err("The option-rom requires all member"
6004 " disks to be a member of all volumes\n");
6006 } else if (super
->orom
&& mpb
->num_raid_devs
> 0 &&
6007 mpb
->num_disks
!= raiddisks
) {
6008 pr_err("The option-rom requires all member"
6009 " disks to be a member of all volumes\n");
6013 /* retrieve the largest free space block */
6014 e
= get_extents(super
, dl
);
6019 unsigned long long esize
;
6021 esize
= e
[i
].start
- pos
;
6022 if (esize
>= maxsize
)
6024 pos
= e
[i
].start
+ e
[i
].size
;
6026 } while (e
[i
-1].size
);
6031 pr_err("unable to determine free space for: %s\n",
6035 if (maxsize
< size
) {
6037 pr_err("%s not enough space (%llu < %llu)\n",
6038 dev
, maxsize
, size
);
6042 /* count total number of extents for merge */
6044 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6046 i
+= dl
->extent_cnt
;
6048 maxsize
= merge_extents(super
, i
);
6050 if (!check_env("IMSM_NO_PLATFORM") &&
6051 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6052 pr_err("attempting to create a second "
6053 "volume with size less then remaining space. "
6058 if (maxsize
< size
|| maxsize
== 0) {
6061 pr_err("no free space"
6062 " left on device. Aborting...\n");
6064 pr_err("not enough space"
6065 " to create volume of given size"
6066 " (%llu < %llu). Aborting...\n",
6072 *freesize
= maxsize
;
6075 int count
= count_volumes(super
->hba
->path
,
6076 super
->orom
->dpa
, verbose
);
6077 if (super
->orom
->vphba
<= count
) {
6078 pr_vrb(": platform does not support more than %d raid volumes.\n",
6079 super
->orom
->vphba
);
6086 static int imsm_get_free_size(struct supertype
*st
, int raiddisks
,
6087 unsigned long long size
, int chunk
,
6088 unsigned long long *freesize
)
6090 struct intel_super
*super
= st
->sb
;
6091 struct imsm_super
*mpb
= super
->anchor
;
6096 unsigned long long maxsize
;
6097 unsigned long long minsize
;
6101 /* find the largest common start free region of the possible disks */
6105 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6111 /* don't activate new spares if we are orom constrained
6112 * and there is already a volume active in the container
6114 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
6117 e
= get_extents(super
, dl
);
6120 for (i
= 1; e
[i
-1].size
; i
++)
6128 maxsize
= merge_extents(super
, extent_cnt
);
6132 minsize
= chunk
* 2;
6134 if (cnt
< raiddisks
||
6135 (super
->orom
&& used
&& used
!= raiddisks
) ||
6136 maxsize
< minsize
||
6138 pr_err("not enough devices with space to create array.\n");
6139 return 0; /* No enough free spaces large enough */
6150 if (!check_env("IMSM_NO_PLATFORM") &&
6151 mpb
->num_raid_devs
> 0 && size
&& size
!= maxsize
) {
6152 pr_err("attempting to create a second "
6153 "volume with size less then remaining space. "
6158 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6160 dl
->raiddisk
= cnt
++;
6164 dprintf("imsm: imsm_get_free_size() returns : %llu\n", size
);
6169 static int reserve_space(struct supertype
*st
, int raiddisks
,
6170 unsigned long long size
, int chunk
,
6171 unsigned long long *freesize
)
6173 struct intel_super
*super
= st
->sb
;
6178 rv
= imsm_get_free_size(st
, raiddisks
, size
, chunk
, freesize
);
6181 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6183 dl
->raiddisk
= cnt
++;
6190 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
6191 int raiddisks
, int *chunk
, unsigned long long size
,
6192 unsigned long long data_offset
,
6193 char *dev
, unsigned long long *freesize
,
6201 * if given unused devices create a container
6202 * if given given devices in a container create a member volume
6204 if (level
== LEVEL_CONTAINER
) {
6205 /* Must be a fresh device to add to a container */
6206 return validate_geometry_imsm_container(st
, level
, layout
,
6216 struct intel_super
*super
= st
->sb
;
6217 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
6218 raiddisks
, chunk
, size
,
6221 /* we are being asked to automatically layout a
6222 * new volume based on the current contents of
6223 * the container. If the the parameters can be
6224 * satisfied reserve_space will record the disks,
6225 * start offset, and size of the volume to be
6226 * created. add_to_super and getinfo_super
6227 * detect when autolayout is in progress.
6229 /* assuming that freesize is always given when array is
6231 if (super
->orom
&& freesize
) {
6233 count
= count_volumes(super
->hba
->path
,
6234 super
->orom
->dpa
, verbose
);
6235 if (super
->orom
->vphba
<= count
) {
6236 pr_vrb(": platform does not support more"
6237 " than %d raid volumes.\n",
6238 super
->orom
->vphba
);
6243 return reserve_space(st
, raiddisks
, size
,
6244 chunk
?*chunk
:0, freesize
);
6249 /* creating in a given container */
6250 return validate_geometry_imsm_volume(st
, level
, layout
,
6251 raiddisks
, chunk
, size
,
6253 dev
, freesize
, verbose
);
6256 /* This device needs to be a device in an 'imsm' container */
6257 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
6260 pr_err("Cannot create this array on device %s\n",
6265 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
6267 pr_err("Cannot open %s: %s\n",
6268 dev
, strerror(errno
));
6271 /* Well, it is in use by someone, maybe an 'imsm' container. */
6272 cfd
= open_container(fd
);
6276 pr_err("Cannot use %s: It is busy\n",
6280 sra
= sysfs_read(cfd
, NULL
, GET_VERSION
);
6281 if (sra
&& sra
->array
.major_version
== -1 &&
6282 strcmp(sra
->text_version
, "imsm") == 0)
6286 /* This is a member of a imsm container. Load the container
6287 * and try to create a volume
6289 struct intel_super
*super
;
6291 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
, NULL
, 1) == 0) {
6293 strcpy(st
->container_devnm
, fd2devnm(cfd
));
6295 return validate_geometry_imsm_volume(st
, level
, layout
,
6297 size
, data_offset
, dev
,
6304 pr_err("failed container membership check\n");
6310 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
6312 struct intel_super
*super
= st
->sb
;
6314 if (level
&& *level
== UnSet
)
6315 *level
= LEVEL_CONTAINER
;
6317 if (level
&& layout
&& *layout
== UnSet
)
6318 *layout
= imsm_level_to_layout(*level
);
6320 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0))
6321 *chunk
= imsm_default_chunk(super
->orom
);
6324 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
6326 static int kill_subarray_imsm(struct supertype
*st
)
6328 /* remove the subarray currently referenced by ->current_vol */
6330 struct intel_dev
**dp
;
6331 struct intel_super
*super
= st
->sb
;
6332 __u8 current_vol
= super
->current_vol
;
6333 struct imsm_super
*mpb
= super
->anchor
;
6335 if (super
->current_vol
< 0)
6337 super
->current_vol
= -1; /* invalidate subarray cursor */
6339 /* block deletions that would change the uuid of active subarrays
6341 * FIXME when immutable ids are available, but note that we'll
6342 * also need to fixup the invalidated/active subarray indexes in
6345 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6348 if (i
< current_vol
)
6350 sprintf(subarray
, "%u", i
);
6351 if (is_subarray_active(subarray
, st
->devnm
)) {
6352 pr_err("deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
6359 if (st
->update_tail
) {
6360 struct imsm_update_kill_array
*u
= xmalloc(sizeof(*u
));
6362 u
->type
= update_kill_array
;
6363 u
->dev_idx
= current_vol
;
6364 append_metadata_update(st
, u
, sizeof(*u
));
6369 for (dp
= &super
->devlist
; *dp
;)
6370 if ((*dp
)->index
== current_vol
) {
6373 handle_missing(super
, (*dp
)->dev
);
6374 if ((*dp
)->index
> current_vol
)
6379 /* no more raid devices, all active components are now spares,
6380 * but of course failed are still failed
6382 if (--mpb
->num_raid_devs
== 0) {
6385 for (d
= super
->disks
; d
; d
= d
->next
)
6390 super
->updates_pending
++;
6395 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
6396 char *update
, struct mddev_ident
*ident
)
6398 /* update the subarray currently referenced by ->current_vol */
6399 struct intel_super
*super
= st
->sb
;
6400 struct imsm_super
*mpb
= super
->anchor
;
6402 if (strcmp(update
, "name") == 0) {
6403 char *name
= ident
->name
;
6407 if (is_subarray_active(subarray
, st
->devnm
)) {
6408 pr_err("Unable to update name of active subarray\n");
6412 if (!check_name(super
, name
, 0))
6415 vol
= strtoul(subarray
, &ep
, 10);
6416 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
6419 if (st
->update_tail
) {
6420 struct imsm_update_rename_array
*u
= xmalloc(sizeof(*u
));
6422 u
->type
= update_rename_array
;
6424 snprintf((char *) u
->name
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6425 append_metadata_update(st
, u
, sizeof(*u
));
6427 struct imsm_dev
*dev
;
6430 dev
= get_imsm_dev(super
, vol
);
6431 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6432 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6433 dev
= get_imsm_dev(super
, i
);
6434 handle_missing(super
, dev
);
6436 super
->updates_pending
++;
6443 #endif /* MDASSEMBLE */
6445 static int is_gen_migration(struct imsm_dev
*dev
)
6450 if (!dev
->vol
.migr_state
)
6453 if (migr_type(dev
) == MIGR_GEN_MIGR
)
6459 static int is_rebuilding(struct imsm_dev
*dev
)
6461 struct imsm_map
*migr_map
;
6463 if (!dev
->vol
.migr_state
)
6466 if (migr_type(dev
) != MIGR_REBUILD
)
6469 migr_map
= get_imsm_map(dev
, MAP_1
);
6471 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
6478 static int is_initializing(struct imsm_dev
*dev
)
6480 struct imsm_map
*migr_map
;
6482 if (!dev
->vol
.migr_state
)
6485 if (migr_type(dev
) != MIGR_INIT
)
6488 migr_map
= get_imsm_map(dev
, MAP_1
);
6490 if (migr_map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
6497 static void update_recovery_start(struct intel_super
*super
,
6498 struct imsm_dev
*dev
,
6499 struct mdinfo
*array
)
6501 struct mdinfo
*rebuild
= NULL
;
6505 if (!is_rebuilding(dev
))
6508 /* Find the rebuild target, but punt on the dual rebuild case */
6509 for (d
= array
->devs
; d
; d
= d
->next
)
6510 if (d
->recovery_start
== 0) {
6517 /* (?) none of the disks are marked with
6518 * IMSM_ORD_REBUILD, so assume they are missing and the
6519 * disk_ord_tbl was not correctly updated
6521 dprintf("%s: failed to locate out-of-sync disk\n", __func__
);
6525 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
6526 rebuild
->recovery_start
= units
* blocks_per_migr_unit(super
, dev
);
6530 static int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
);
6533 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
6535 /* Given a container loaded by load_super_imsm_all,
6536 * extract information about all the arrays into
6538 * If 'subarray' is given, just extract info about that array.
6540 * For each imsm_dev create an mdinfo, fill it in,
6541 * then look for matching devices in super->disks
6542 * and create appropriate device mdinfo.
6544 struct intel_super
*super
= st
->sb
;
6545 struct imsm_super
*mpb
= super
->anchor
;
6546 struct mdinfo
*rest
= NULL
;
6550 int spare_disks
= 0;
6552 /* do not assemble arrays when not all attributes are supported */
6553 if (imsm_check_attributes(mpb
->attributes
) == 0) {
6555 pr_err("Unsupported attributes in IMSM metadata."
6556 "Arrays activation is blocked.\n");
6559 /* check for bad blocks */
6560 if (imsm_bbm_log_size(super
->anchor
)) {
6561 pr_err("BBM log found in IMSM metadata."
6562 "Arrays activation is blocked.\n");
6567 /* count spare devices, not used in maps
6569 for (d
= super
->disks
; d
; d
= d
->next
)
6573 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6574 struct imsm_dev
*dev
;
6575 struct imsm_map
*map
;
6576 struct imsm_map
*map2
;
6577 struct mdinfo
*this;
6585 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
6588 dev
= get_imsm_dev(super
, i
);
6589 map
= get_imsm_map(dev
, MAP_0
);
6590 map2
= get_imsm_map(dev
, MAP_1
);
6592 /* do not publish arrays that are in the middle of an
6593 * unsupported migration
6595 if (dev
->vol
.migr_state
&&
6596 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
6597 pr_err("cannot assemble volume '%.16s':"
6598 " unsupported migration in progress\n",
6602 /* do not publish arrays that are not support by controller's
6606 this = xmalloc(sizeof(*this));
6608 super
->current_vol
= i
;
6609 getinfo_super_imsm_volume(st
, this, NULL
);
6612 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
6613 /* mdadm does not support all metadata features- set the bit in all arrays state */
6614 if (!validate_geometry_imsm_orom(super
,
6615 get_imsm_raid_level(map
), /* RAID level */
6616 imsm_level_to_layout(get_imsm_raid_level(map
)),
6617 map
->num_members
, /* raid disks */
6618 &chunk
, join_u32(dev
->size_low
, dev
->size_high
),
6620 pr_err("IMSM RAID geometry validation"
6621 " failed. Array %s activation is blocked.\n",
6623 this->array
.state
|=
6624 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
6625 (1<<MD_SB_BLOCK_VOLUME
);
6629 /* if array has bad blocks, set suitable bit in all arrays state */
6631 this->array
.state
|=
6632 (1<<MD_SB_BLOCK_CONTAINER_RESHAPE
) |
6633 (1<<MD_SB_BLOCK_VOLUME
);
6635 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
6636 unsigned long long recovery_start
;
6637 struct mdinfo
*info_d
;
6644 idx
= get_imsm_disk_idx(dev
, slot
, MAP_0
);
6645 ord
= get_imsm_ord_tbl_ent(dev
, slot
, MAP_X
);
6646 for (d
= super
->disks
; d
; d
= d
->next
)
6647 if (d
->index
== idx
)
6650 recovery_start
= MaxSector
;
6653 if (d
&& is_failed(&d
->disk
))
6655 if (ord
& IMSM_ORD_REBUILD
)
6659 * if we skip some disks the array will be assmebled degraded;
6660 * reset resync start to avoid a dirty-degraded
6661 * situation when performing the intial sync
6663 * FIXME handle dirty degraded
6665 if ((skip
|| recovery_start
== 0) && !dev
->vol
.dirty
)
6666 this->resync_start
= MaxSector
;
6670 info_d
= xcalloc(1, sizeof(*info_d
));
6671 info_d
->next
= this->devs
;
6672 this->devs
= info_d
;
6674 info_d
->disk
.number
= d
->index
;
6675 info_d
->disk
.major
= d
->major
;
6676 info_d
->disk
.minor
= d
->minor
;
6677 info_d
->disk
.raid_disk
= slot
;
6678 info_d
->recovery_start
= recovery_start
;
6680 if (slot
< map2
->num_members
)
6681 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
6683 this->array
.spare_disks
++;
6685 if (slot
< map
->num_members
)
6686 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
6688 this->array
.spare_disks
++;
6690 if (info_d
->recovery_start
== MaxSector
)
6691 this->array
.working_disks
++;
6693 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
6694 info_d
->data_offset
= pba_of_lba0(map
);
6695 info_d
->component_size
= blocks_per_member(map
);
6697 /* now that the disk list is up-to-date fixup recovery_start */
6698 update_recovery_start(super
, dev
, this);
6699 this->array
.spare_disks
+= spare_disks
;
6702 /* check for reshape */
6703 if (this->reshape_active
== 1)
6704 recover_backup_imsm(st
, this);
6713 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
,
6714 int failed
, int look_in_map
)
6716 struct imsm_map
*map
;
6718 map
= get_imsm_map(dev
, look_in_map
);
6721 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
6722 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
6724 switch (get_imsm_raid_level(map
)) {
6726 return IMSM_T_STATE_FAILED
;
6729 if (failed
< map
->num_members
)
6730 return IMSM_T_STATE_DEGRADED
;
6732 return IMSM_T_STATE_FAILED
;
6737 * check to see if any mirrors have failed, otherwise we
6738 * are degraded. Even numbered slots are mirrored on
6742 /* gcc -Os complains that this is unused */
6743 int insync
= insync
;
6745 for (i
= 0; i
< map
->num_members
; i
++) {
6746 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, MAP_X
);
6747 int idx
= ord_to_idx(ord
);
6748 struct imsm_disk
*disk
;
6750 /* reset the potential in-sync count on even-numbered
6751 * slots. num_copies is always 2 for imsm raid10
6756 disk
= get_imsm_disk(super
, idx
);
6757 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
6760 /* no in-sync disks left in this mirror the
6764 return IMSM_T_STATE_FAILED
;
6767 return IMSM_T_STATE_DEGRADED
;
6771 return IMSM_T_STATE_DEGRADED
;
6773 return IMSM_T_STATE_FAILED
;
6779 return map
->map_state
;
6782 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
,
6787 struct imsm_disk
*disk
;
6788 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
6789 struct imsm_map
*prev
= get_imsm_map(dev
, MAP_1
);
6790 struct imsm_map
*map_for_loop
;
6795 /* at the beginning of migration we set IMSM_ORD_REBUILD on
6796 * disks that are being rebuilt. New failures are recorded to
6797 * map[0]. So we look through all the disks we started with and
6798 * see if any failures are still present, or if any new ones
6802 if (prev
&& (map
->num_members
< prev
->num_members
))
6803 map_for_loop
= prev
;
6805 for (i
= 0; i
< map_for_loop
->num_members
; i
++) {
6807 /* when MAP_X is passed both maps failures are counted
6810 ((look_in_map
== MAP_1
) || (look_in_map
== MAP_X
)) &&
6811 (i
< prev
->num_members
)) {
6812 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
6813 idx_1
= ord_to_idx(ord
);
6815 disk
= get_imsm_disk(super
, idx_1
);
6816 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
6819 if (((look_in_map
== MAP_0
) || (look_in_map
== MAP_X
)) &&
6820 (i
< map
->num_members
)) {
6821 ord
= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
6822 idx
= ord_to_idx(ord
);
6825 disk
= get_imsm_disk(super
, idx
);
6826 if (!disk
|| is_failed(disk
) ||
6827 ord
& IMSM_ORD_REBUILD
)
6837 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
6840 struct intel_super
*super
= c
->sb
;
6841 struct imsm_super
*mpb
= super
->anchor
;
6843 if (atoi(inst
) >= mpb
->num_raid_devs
) {
6844 fprintf(stderr
, "%s: subarry index %d, out of range\n",
6845 __func__
, atoi(inst
));
6849 dprintf("imsm: open_new %s\n", inst
);
6850 a
->info
.container_member
= atoi(inst
);
6854 static int is_resyncing(struct imsm_dev
*dev
)
6856 struct imsm_map
*migr_map
;
6858 if (!dev
->vol
.migr_state
)
6861 if (migr_type(dev
) == MIGR_INIT
||
6862 migr_type(dev
) == MIGR_REPAIR
)
6865 if (migr_type(dev
) == MIGR_GEN_MIGR
)
6868 migr_map
= get_imsm_map(dev
, MAP_1
);
6870 if ((migr_map
->map_state
== IMSM_T_STATE_NORMAL
) &&
6871 (dev
->vol
.migr_type
!= MIGR_GEN_MIGR
))
6877 /* return true if we recorded new information */
6878 static int mark_failure(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
6882 struct imsm_map
*map
;
6883 char buf
[MAX_RAID_SERIAL_LEN
+3];
6884 unsigned int len
, shift
= 0;
6886 /* new failures are always set in map[0] */
6887 map
= get_imsm_map(dev
, MAP_0
);
6889 slot
= get_imsm_disk_slot(map
, idx
);
6893 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
6894 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
6897 memcpy(buf
, disk
->serial
, MAX_RAID_SERIAL_LEN
);
6898 buf
[MAX_RAID_SERIAL_LEN
] = '\000';
6900 if ((len
= strlen(buf
)) >= MAX_RAID_SERIAL_LEN
)
6901 shift
= len
- MAX_RAID_SERIAL_LEN
+ 1;
6902 strncpy((char *)disk
->serial
, &buf
[shift
], MAX_RAID_SERIAL_LEN
);
6904 disk
->status
|= FAILED_DISK
;
6905 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
6906 /* mark failures in second map if second map exists and this disk
6908 * This is valid for migration, initialization and rebuild
6910 if (dev
->vol
.migr_state
) {
6911 struct imsm_map
*map2
= get_imsm_map(dev
, MAP_1
);
6912 int slot2
= get_imsm_disk_slot(map2
, idx
);
6914 if ((slot2
< map2
->num_members
) &&
6916 set_imsm_ord_tbl_ent(map2
, slot2
,
6917 idx
| IMSM_ORD_REBUILD
);
6919 if (map
->failed_disk_num
== 0xff)
6920 map
->failed_disk_num
= slot
;
6924 static void mark_missing(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
6926 mark_failure(dev
, disk
, idx
);
6928 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
6931 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
6932 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
6935 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
6939 if (!super
->missing
)
6942 /* When orom adds replacement for missing disk it does
6943 * not remove entry of missing disk, but just updates map with
6944 * new added disk. So it is not enough just to test if there is
6945 * any missing disk, we have to look if there are any failed disks
6946 * in map to stop migration */
6948 dprintf("imsm: mark missing\n");
6949 /* end process for initialization and rebuild only
6951 if (is_gen_migration(dev
) == 0) {
6955 failed
= imsm_count_failed(super
, dev
, MAP_0
);
6956 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
6959 end_migration(dev
, super
, map_state
);
6961 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
6962 mark_missing(dev
, &dl
->disk
, dl
->index
);
6963 super
->updates_pending
++;
6966 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
,
6969 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
6970 unsigned long long array_blocks
;
6971 struct imsm_map
*map
;
6973 if (used_disks
== 0) {
6974 /* when problems occures
6975 * return current array_blocks value
6977 array_blocks
= __le32_to_cpu(dev
->size_high
);
6978 array_blocks
= array_blocks
<< 32;
6979 array_blocks
+= __le32_to_cpu(dev
->size_low
);
6981 return array_blocks
;
6984 /* set array size in metadata
6986 if (new_size
<= 0) {
6987 /* OLCE size change is caused by added disks
6989 map
= get_imsm_map(dev
, MAP_0
);
6990 array_blocks
= blocks_per_member(map
) * used_disks
;
6992 /* Online Volume Size Change
6993 * Using available free space
6995 array_blocks
= new_size
;
6998 /* round array size down to closest MB
7000 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
7001 dev
->size_low
= __cpu_to_le32((__u32
)array_blocks
);
7002 dev
->size_high
= __cpu_to_le32((__u32
)(array_blocks
>> 32));
7004 return array_blocks
;
7007 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
7009 static void imsm_progress_container_reshape(struct intel_super
*super
)
7011 /* if no device has a migr_state, but some device has a
7012 * different number of members than the previous device, start
7013 * changing the number of devices in this device to match
7016 struct imsm_super
*mpb
= super
->anchor
;
7017 int prev_disks
= -1;
7021 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7022 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
7023 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7024 struct imsm_map
*map2
;
7025 int prev_num_members
;
7027 if (dev
->vol
.migr_state
)
7030 if (prev_disks
== -1)
7031 prev_disks
= map
->num_members
;
7032 if (prev_disks
== map
->num_members
)
7035 /* OK, this array needs to enter reshape mode.
7036 * i.e it needs a migr_state
7039 copy_map_size
= sizeof_imsm_map(map
);
7040 prev_num_members
= map
->num_members
;
7041 map
->num_members
= prev_disks
;
7042 dev
->vol
.migr_state
= 1;
7043 dev
->vol
.curr_migr_unit
= 0;
7044 set_migr_type(dev
, MIGR_GEN_MIGR
);
7045 for (i
= prev_num_members
;
7046 i
< map
->num_members
; i
++)
7047 set_imsm_ord_tbl_ent(map
, i
, i
);
7048 map2
= get_imsm_map(dev
, MAP_1
);
7049 /* Copy the current map */
7050 memcpy(map2
, map
, copy_map_size
);
7051 map2
->num_members
= prev_num_members
;
7053 imsm_set_array_size(dev
, -1);
7054 super
->clean_migration_record_by_mdmon
= 1;
7055 super
->updates_pending
++;
7059 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
7060 * states are handled in imsm_set_disk() with one exception, when a
7061 * resync is stopped due to a new failure this routine will set the
7062 * 'degraded' state for the array.
7064 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
7066 int inst
= a
->info
.container_member
;
7067 struct intel_super
*super
= a
->container
->sb
;
7068 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7069 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7070 int failed
= imsm_count_failed(super
, dev
, MAP_0
);
7071 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7072 __u32 blocks_per_unit
;
7074 if (dev
->vol
.migr_state
&&
7075 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
7076 /* array state change is blocked due to reshape action
7078 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
7079 * - finish the reshape (if last_checkpoint is big and action != reshape)
7080 * - update curr_migr_unit
7082 if (a
->curr_action
== reshape
) {
7083 /* still reshaping, maybe update curr_migr_unit */
7084 goto mark_checkpoint
;
7086 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
7087 /* for some reason we aborted the reshape.
7089 * disable automatic metadata rollback
7090 * user action is required to recover process
7093 struct imsm_map
*map2
=
7094 get_imsm_map(dev
, MAP_1
);
7095 dev
->vol
.migr_state
= 0;
7096 set_migr_type(dev
, 0);
7097 dev
->vol
.curr_migr_unit
= 0;
7099 sizeof_imsm_map(map2
));
7100 super
->updates_pending
++;
7103 if (a
->last_checkpoint
>= a
->info
.component_size
) {
7104 unsigned long long array_blocks
;
7108 used_disks
= imsm_num_data_members(dev
, MAP_0
);
7109 if (used_disks
> 0) {
7111 blocks_per_member(map
) *
7113 /* round array size down to closest MB
7115 array_blocks
= (array_blocks
7116 >> SECT_PER_MB_SHIFT
)
7117 << SECT_PER_MB_SHIFT
;
7118 a
->info
.custom_array_size
= array_blocks
;
7119 /* encourage manager to update array
7123 a
->check_reshape
= 1;
7125 /* finalize online capacity expansion/reshape */
7126 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
7128 mdi
->disk
.raid_disk
,
7131 imsm_progress_container_reshape(super
);
7136 /* before we activate this array handle any missing disks */
7137 if (consistent
== 2)
7138 handle_missing(super
, dev
);
7140 if (consistent
== 2 &&
7141 (!is_resync_complete(&a
->info
) ||
7142 map_state
!= IMSM_T_STATE_NORMAL
||
7143 dev
->vol
.migr_state
))
7146 if (is_resync_complete(&a
->info
)) {
7147 /* complete intialization / resync,
7148 * recovery and interrupted recovery is completed in
7151 if (is_resyncing(dev
)) {
7152 dprintf("imsm: mark resync done\n");
7153 end_migration(dev
, super
, map_state
);
7154 super
->updates_pending
++;
7155 a
->last_checkpoint
= 0;
7157 } else if ((!is_resyncing(dev
) && !failed
) &&
7158 (imsm_reshape_blocks_arrays_changes(super
) == 0)) {
7159 /* mark the start of the init process if nothing is failed */
7160 dprintf("imsm: mark resync start\n");
7161 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
7162 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
7164 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
7165 super
->updates_pending
++;
7169 /* skip checkpointing for general migration,
7170 * it is controlled in mdadm
7172 if (is_gen_migration(dev
))
7173 goto skip_mark_checkpoint
;
7175 /* check if we can update curr_migr_unit from resync_start, recovery_start */
7176 blocks_per_unit
= blocks_per_migr_unit(super
, dev
);
7177 if (blocks_per_unit
) {
7181 units
= a
->last_checkpoint
/ blocks_per_unit
;
7184 /* check that we did not overflow 32-bits, and that
7185 * curr_migr_unit needs updating
7187 if (units32
== units
&&
7189 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
7190 dprintf("imsm: mark checkpoint (%u)\n", units32
);
7191 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
7192 super
->updates_pending
++;
7196 skip_mark_checkpoint
:
7197 /* mark dirty / clean */
7198 if (dev
->vol
.dirty
!= !consistent
) {
7199 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
7204 super
->updates_pending
++;
7210 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
7212 int inst
= a
->info
.container_member
;
7213 struct intel_super
*super
= a
->container
->sb
;
7214 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7215 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7216 struct imsm_disk
*disk
;
7218 int recovery_not_finished
= 0;
7223 if (n
> map
->num_members
)
7224 fprintf(stderr
, "imsm: set_disk %d out of range 0..%d\n",
7225 n
, map
->num_members
- 1);
7230 dprintf("imsm: set_disk %d:%x\n", n
, state
);
7232 ord
= get_imsm_ord_tbl_ent(dev
, n
, MAP_0
);
7233 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
7235 /* check for new failures */
7236 if (state
& DS_FAULTY
) {
7237 if (mark_failure(dev
, disk
, ord_to_idx(ord
)))
7238 super
->updates_pending
++;
7241 /* check if in_sync */
7242 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
7243 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
7245 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
7246 super
->updates_pending
++;
7249 failed
= imsm_count_failed(super
, dev
, MAP_0
);
7250 map_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
7252 /* check if recovery complete, newly degraded, or failed */
7253 dprintf("imsm: Detected transition to state ");
7254 switch (map_state
) {
7255 case IMSM_T_STATE_NORMAL
: /* transition to normal state */
7256 dprintf("normal: ");
7257 if (is_rebuilding(dev
)) {
7258 dprintf("while rebuilding");
7259 /* check if recovery is really finished */
7260 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
7261 if (mdi
->recovery_start
!= MaxSector
) {
7262 recovery_not_finished
= 1;
7265 if (recovery_not_finished
) {
7266 dprintf("\nimsm: Rebuild has not finished yet, "
7267 "state not changed");
7268 if (a
->last_checkpoint
< mdi
->recovery_start
) {
7269 a
->last_checkpoint
= mdi
->recovery_start
;
7270 super
->updates_pending
++;
7274 end_migration(dev
, super
, map_state
);
7275 map
= get_imsm_map(dev
, MAP_0
);
7276 map
->failed_disk_num
= ~0;
7277 super
->updates_pending
++;
7278 a
->last_checkpoint
= 0;
7281 if (is_gen_migration(dev
)) {
7282 dprintf("while general migration");
7283 if (a
->last_checkpoint
>= a
->info
.component_size
)
7284 end_migration(dev
, super
, map_state
);
7286 map
->map_state
= map_state
;
7287 map
= get_imsm_map(dev
, MAP_0
);
7288 map
->failed_disk_num
= ~0;
7289 super
->updates_pending
++;
7293 case IMSM_T_STATE_DEGRADED
: /* transition to degraded state */
7294 dprintf("degraded: ");
7295 if ((map
->map_state
!= map_state
) &&
7296 !dev
->vol
.migr_state
) {
7297 dprintf("mark degraded");
7298 map
->map_state
= map_state
;
7299 super
->updates_pending
++;
7300 a
->last_checkpoint
= 0;
7303 if (is_rebuilding(dev
)) {
7304 dprintf("while rebuilding.");
7305 if (map
->map_state
!= map_state
) {
7306 dprintf(" Map state change");
7307 end_migration(dev
, super
, map_state
);
7308 super
->updates_pending
++;
7312 if (is_gen_migration(dev
)) {
7313 dprintf("while general migration");
7314 if (a
->last_checkpoint
>= a
->info
.component_size
)
7315 end_migration(dev
, super
, map_state
);
7317 map
->map_state
= map_state
;
7318 manage_second_map(super
, dev
);
7320 super
->updates_pending
++;
7323 if (is_initializing(dev
)) {
7324 dprintf("while initialization.");
7325 map
->map_state
= map_state
;
7326 super
->updates_pending
++;
7330 case IMSM_T_STATE_FAILED
: /* transition to failed state */
7331 dprintf("failed: ");
7332 if (is_gen_migration(dev
)) {
7333 dprintf("while general migration");
7334 map
->map_state
= map_state
;
7335 super
->updates_pending
++;
7338 if (map
->map_state
!= map_state
) {
7339 dprintf("mark failed");
7340 end_migration(dev
, super
, map_state
);
7341 super
->updates_pending
++;
7342 a
->last_checkpoint
= 0;
7347 dprintf("state %i\n", map_state
);
7353 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
7356 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
7357 unsigned long long dsize
;
7358 unsigned long long sectors
;
7360 get_dev_size(fd
, NULL
, &dsize
);
7362 if (mpb_size
> 512) {
7363 /* -1 to account for anchor */
7364 sectors
= mpb_sectors(mpb
) - 1;
7366 /* write the extended mpb to the sectors preceeding the anchor */
7367 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0)
7370 if ((unsigned long long)write(fd
, buf
+ 512, 512 * sectors
)
7375 /* first block is stored on second to last sector of the disk */
7376 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0)
7379 if (write(fd
, buf
, 512) != 512)
7385 static void imsm_sync_metadata(struct supertype
*container
)
7387 struct intel_super
*super
= container
->sb
;
7389 dprintf("sync metadata: %d\n", super
->updates_pending
);
7390 if (!super
->updates_pending
)
7393 write_super_imsm(container
, 0);
7395 super
->updates_pending
= 0;
7398 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
7400 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7401 int i
= get_imsm_disk_idx(dev
, idx
, MAP_X
);
7404 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7408 if (dl
&& is_failed(&dl
->disk
))
7412 dprintf("%s: found %x:%x\n", __func__
, dl
->major
, dl
->minor
);
7417 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
7418 struct active_array
*a
, int activate_new
,
7419 struct mdinfo
*additional_test_list
)
7421 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
7422 int idx
= get_imsm_disk_idx(dev
, slot
, MAP_X
);
7423 struct imsm_super
*mpb
= super
->anchor
;
7424 struct imsm_map
*map
;
7425 unsigned long long pos
;
7430 __u32 array_start
= 0;
7431 __u32 array_end
= 0;
7433 struct mdinfo
*test_list
;
7435 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7436 /* If in this array, skip */
7437 for (d
= a
->info
.devs
; d
; d
= d
->next
)
7438 if (d
->state_fd
>= 0 &&
7439 d
->disk
.major
== dl
->major
&&
7440 d
->disk
.minor
== dl
->minor
) {
7441 dprintf("%x:%x already in array\n",
7442 dl
->major
, dl
->minor
);
7447 test_list
= additional_test_list
;
7449 if (test_list
->disk
.major
== dl
->major
&&
7450 test_list
->disk
.minor
== dl
->minor
) {
7451 dprintf("%x:%x already in additional test list\n",
7452 dl
->major
, dl
->minor
);
7455 test_list
= test_list
->next
;
7460 /* skip in use or failed drives */
7461 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
7463 dprintf("%x:%x status (failed: %d index: %d)\n",
7464 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
7468 /* skip pure spares when we are looking for partially
7469 * assimilated drives
7471 if (dl
->index
== -1 && !activate_new
)
7474 /* Does this unused device have the requisite free space?
7475 * It needs to be able to cover all member volumes
7477 ex
= get_extents(super
, dl
);
7479 dprintf("cannot get extents\n");
7482 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7483 dev
= get_imsm_dev(super
, i
);
7484 map
= get_imsm_map(dev
, MAP_0
);
7486 /* check if this disk is already a member of
7489 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
7495 array_start
= pba_of_lba0(map
);
7496 array_end
= array_start
+
7497 blocks_per_member(map
) - 1;
7500 /* check that we can start at pba_of_lba0 with
7501 * blocks_per_member of space
7503 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
7507 pos
= ex
[j
].start
+ ex
[j
].size
;
7509 } while (ex
[j
-1].size
);
7516 if (i
< mpb
->num_raid_devs
) {
7517 dprintf("%x:%x does not have %u to %u available\n",
7518 dl
->major
, dl
->minor
, array_start
, array_end
);
7529 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
7531 struct imsm_dev
*dev2
;
7532 struct imsm_map
*map
;
7538 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
7540 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
, MAP_0
);
7541 if (state
== IMSM_T_STATE_FAILED
) {
7542 map
= get_imsm_map(dev2
, MAP_0
);
7545 for (slot
= 0; slot
< map
->num_members
; slot
++) {
7547 * Check if failed disks are deleted from intel
7548 * disk list or are marked to be deleted
7550 idx
= get_imsm_disk_idx(dev2
, slot
, MAP_X
);
7551 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
7553 * Do not rebuild the array if failed disks
7554 * from failed sub-array are not removed from
7558 is_failed(&idisk
->disk
) &&
7559 (idisk
->action
!= DISK_REMOVE
))
7567 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
7568 struct metadata_update
**updates
)
7571 * Find a device with unused free space and use it to replace a
7572 * failed/vacant region in an array. We replace failed regions one a
7573 * array at a time. The result is that a new spare disk will be added
7574 * to the first failed array and after the monitor has finished
7575 * propagating failures the remainder will be consumed.
7577 * FIXME add a capability for mdmon to request spares from another
7581 struct intel_super
*super
= a
->container
->sb
;
7582 int inst
= a
->info
.container_member
;
7583 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
7584 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7585 int failed
= a
->info
.array
.raid_disks
;
7586 struct mdinfo
*rv
= NULL
;
7589 struct metadata_update
*mu
;
7591 struct imsm_update_activate_spare
*u
;
7596 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
7597 if ((d
->curr_state
& DS_FAULTY
) &&
7599 /* wait for Removal to happen */
7601 if (d
->state_fd
>= 0)
7605 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
7606 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
7608 if (imsm_reshape_blocks_arrays_changes(super
))
7611 /* Cannot activate another spare if rebuild is in progress already
7613 if (is_rebuilding(dev
)) {
7614 dprintf("imsm: No spare activation allowed. "
7615 "Rebuild in progress already.\n");
7619 if (a
->info
.array
.level
== 4)
7620 /* No repair for takeovered array
7621 * imsm doesn't support raid4
7625 if (imsm_check_degraded(super
, dev
, failed
, MAP_0
) !=
7626 IMSM_T_STATE_DEGRADED
)
7630 * If there are any failed disks check state of the other volume.
7631 * Block rebuild if the another one is failed until failed disks
7632 * are removed from container.
7635 dprintf("found failed disks in %.*s, check if there another"
7636 "failed sub-array.\n",
7637 MAX_RAID_SERIAL_LEN
, dev
->volume
);
7638 /* check if states of the other volumes allow for rebuild */
7639 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
7641 allowed
= imsm_rebuild_allowed(a
->container
,
7649 /* For each slot, if it is not working, find a spare */
7650 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
7651 for (d
= a
->info
.devs
; d
; d
= d
->next
)
7652 if (d
->disk
.raid_disk
== i
)
7654 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
7655 if (d
&& (d
->state_fd
>= 0))
7659 * OK, this device needs recovery. Try to re-add the
7660 * previous occupant of this slot, if this fails see if
7661 * we can continue the assimilation of a spare that was
7662 * partially assimilated, finally try to activate a new
7665 dl
= imsm_readd(super
, i
, a
);
7667 dl
= imsm_add_spare(super
, i
, a
, 0, rv
);
7669 dl
= imsm_add_spare(super
, i
, a
, 1, rv
);
7673 /* found a usable disk with enough space */
7674 di
= xcalloc(1, sizeof(*di
));
7676 /* dl->index will be -1 in the case we are activating a
7677 * pristine spare. imsm_process_update() will create a
7678 * new index in this case. Once a disk is found to be
7679 * failed in all member arrays it is kicked from the
7682 di
->disk
.number
= dl
->index
;
7684 /* (ab)use di->devs to store a pointer to the device
7687 di
->devs
= (struct mdinfo
*) dl
;
7689 di
->disk
.raid_disk
= i
;
7690 di
->disk
.major
= dl
->major
;
7691 di
->disk
.minor
= dl
->minor
;
7693 di
->recovery_start
= 0;
7694 di
->data_offset
= pba_of_lba0(map
);
7695 di
->component_size
= a
->info
.component_size
;
7696 di
->container_member
= inst
;
7697 super
->random
= random32();
7701 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
7702 i
, di
->data_offset
);
7706 /* No spares found */
7708 /* Now 'rv' has a list of devices to return.
7709 * Create a metadata_update record to update the
7710 * disk_ord_tbl for the array
7712 mu
= xmalloc(sizeof(*mu
));
7713 mu
->buf
= xcalloc(num_spares
,
7714 sizeof(struct imsm_update_activate_spare
));
7716 mu
->space_list
= NULL
;
7717 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
7718 mu
->next
= *updates
;
7719 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
7721 for (di
= rv
; di
; di
= di
->next
) {
7722 u
->type
= update_activate_spare
;
7723 u
->dl
= (struct dl
*) di
->devs
;
7725 u
->slot
= di
->disk
.raid_disk
;
7736 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
7738 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
7739 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7740 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, MAP_0
);
7741 struct disk_info
*inf
= get_disk_info(u
);
7742 struct imsm_disk
*disk
;
7746 for (i
= 0; i
< map
->num_members
; i
++) {
7747 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, MAP_X
));
7748 for (j
= 0; j
< new_map
->num_members
; j
++)
7749 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
7757 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
7759 struct dl
*dl
= NULL
;
7760 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
7761 if ((dl
->major
== major
) && (dl
->minor
== minor
))
7766 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
7768 struct dl
*prev
= NULL
;
7772 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
7773 if ((dl
->major
== major
) && (dl
->minor
== minor
)) {
7776 prev
->next
= dl
->next
;
7778 super
->disks
= dl
->next
;
7780 __free_imsm_disk(dl
);
7781 dprintf("%s: removed %x:%x\n",
7782 __func__
, major
, minor
);
7790 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
7792 static int add_remove_disk_update(struct intel_super
*super
)
7794 int check_degraded
= 0;
7795 struct dl
*disk
= NULL
;
7796 /* add/remove some spares to/from the metadata/contrainer */
7797 while (super
->disk_mgmt_list
) {
7798 struct dl
*disk_cfg
;
7800 disk_cfg
= super
->disk_mgmt_list
;
7801 super
->disk_mgmt_list
= disk_cfg
->next
;
7802 disk_cfg
->next
= NULL
;
7804 if (disk_cfg
->action
== DISK_ADD
) {
7805 disk_cfg
->next
= super
->disks
;
7806 super
->disks
= disk_cfg
;
7808 dprintf("%s: added %x:%x\n",
7809 __func__
, disk_cfg
->major
,
7811 } else if (disk_cfg
->action
== DISK_REMOVE
) {
7812 dprintf("Disk remove action processed: %x.%x\n",
7813 disk_cfg
->major
, disk_cfg
->minor
);
7814 disk
= get_disk_super(super
,
7818 /* store action status */
7819 disk
->action
= DISK_REMOVE
;
7820 /* remove spare disks only */
7821 if (disk
->index
== -1) {
7822 remove_disk_super(super
,
7827 /* release allocate disk structure */
7828 __free_imsm_disk(disk_cfg
);
7831 return check_degraded
;
7835 static int apply_reshape_migration_update(struct imsm_update_reshape_migration
*u
,
7836 struct intel_super
*super
,
7839 struct intel_dev
*id
;
7840 void **tofree
= NULL
;
7843 dprintf("apply_reshape_migration_update()\n");
7844 if ((u
->subdev
< 0) ||
7846 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
7849 if ((space_list
== NULL
) || (*space_list
== NULL
)) {
7850 dprintf("imsm: Error: Memory is not allocated\n");
7854 for (id
= super
->devlist
; id
; id
= id
->next
) {
7855 if (id
->index
== (unsigned)u
->subdev
) {
7856 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
7857 struct imsm_map
*map
;
7858 struct imsm_dev
*new_dev
=
7859 (struct imsm_dev
*)*space_list
;
7860 struct imsm_map
*migr_map
= get_imsm_map(dev
, MAP_1
);
7862 struct dl
*new_disk
;
7864 if (new_dev
== NULL
)
7866 *space_list
= **space_list
;
7867 memcpy(new_dev
, dev
, sizeof_imsm_dev(dev
, 0));
7868 map
= get_imsm_map(new_dev
, MAP_0
);
7870 dprintf("imsm: Error: migration in progress");
7874 to_state
= map
->map_state
;
7875 if ((u
->new_level
== 5) && (map
->raid_level
== 0)) {
7877 /* this should not happen */
7878 if (u
->new_disks
[0] < 0) {
7879 map
->failed_disk_num
=
7880 map
->num_members
- 1;
7881 to_state
= IMSM_T_STATE_DEGRADED
;
7883 to_state
= IMSM_T_STATE_NORMAL
;
7885 migrate(new_dev
, super
, to_state
, MIGR_GEN_MIGR
);
7886 if (u
->new_level
> -1)
7887 map
->raid_level
= u
->new_level
;
7888 migr_map
= get_imsm_map(new_dev
, MAP_1
);
7889 if ((u
->new_level
== 5) &&
7890 (migr_map
->raid_level
== 0)) {
7891 int ord
= map
->num_members
- 1;
7892 migr_map
->num_members
--;
7893 if (u
->new_disks
[0] < 0)
7894 ord
|= IMSM_ORD_REBUILD
;
7895 set_imsm_ord_tbl_ent(map
,
7896 map
->num_members
- 1,
7900 tofree
= (void **)dev
;
7902 /* update chunk size
7904 if (u
->new_chunksize
> 0)
7905 map
->blocks_per_strip
=
7906 __cpu_to_le16(u
->new_chunksize
* 2);
7910 if ((u
->new_level
!= 5) ||
7911 (migr_map
->raid_level
!= 0) ||
7912 (migr_map
->raid_level
== map
->raid_level
))
7915 if (u
->new_disks
[0] >= 0) {
7918 new_disk
= get_disk_super(super
,
7919 major(u
->new_disks
[0]),
7920 minor(u
->new_disks
[0]));
7921 dprintf("imsm: new disk for reshape is: %i:%i "
7922 "(%p, index = %i)\n",
7923 major(u
->new_disks
[0]),
7924 minor(u
->new_disks
[0]),
7925 new_disk
, new_disk
->index
);
7926 if (new_disk
== NULL
)
7927 goto error_disk_add
;
7929 new_disk
->index
= map
->num_members
- 1;
7930 /* slot to fill in autolayout
7932 new_disk
->raiddisk
= new_disk
->index
;
7933 new_disk
->disk
.status
|= CONFIGURED_DISK
;
7934 new_disk
->disk
.status
&= ~SPARE_DISK
;
7936 goto error_disk_add
;
7939 *tofree
= *space_list
;
7940 /* calculate new size
7942 imsm_set_array_size(new_dev
, -1);
7949 *space_list
= tofree
;
7953 dprintf("Error: imsm: Cannot find disk.\n");
7957 static int apply_size_change_update(struct imsm_update_size_change
*u
,
7958 struct intel_super
*super
)
7960 struct intel_dev
*id
;
7963 dprintf("apply_size_change_update()\n");
7964 if ((u
->subdev
< 0) ||
7966 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
7970 for (id
= super
->devlist
; id
; id
= id
->next
) {
7971 if (id
->index
== (unsigned)u
->subdev
) {
7972 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
7973 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
7974 int used_disks
= imsm_num_data_members(dev
, MAP_0
);
7975 unsigned long long blocks_per_member
;
7977 /* calculate new size
7979 blocks_per_member
= u
->new_size
/ used_disks
;
7980 dprintf("imsm: apply_size_change_update(size: %llu, "
7981 "blocks per member: %llu)\n",
7982 u
->new_size
, blocks_per_member
);
7983 set_blocks_per_member(map
, blocks_per_member
);
7984 imsm_set_array_size(dev
, u
->new_size
);
7995 static int apply_update_activate_spare(struct imsm_update_activate_spare
*u
,
7996 struct intel_super
*super
,
7997 struct active_array
*active_array
)
7999 struct imsm_super
*mpb
= super
->anchor
;
8000 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
8001 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8002 struct imsm_map
*migr_map
;
8003 struct active_array
*a
;
8004 struct imsm_disk
*disk
;
8011 int second_map_created
= 0;
8013 for (; u
; u
= u
->next
) {
8014 victim
= get_imsm_disk_idx(dev
, u
->slot
, MAP_X
);
8019 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
8024 fprintf(stderr
, "error: imsm_activate_spare passed "
8025 "an unknown disk (index: %d)\n",
8030 /* count failures (excluding rebuilds and the victim)
8031 * to determine map[0] state
8034 for (i
= 0; i
< map
->num_members
; i
++) {
8037 disk
= get_imsm_disk(super
,
8038 get_imsm_disk_idx(dev
, i
, MAP_X
));
8039 if (!disk
|| is_failed(disk
))
8043 /* adding a pristine spare, assign a new index */
8044 if (dl
->index
< 0) {
8045 dl
->index
= super
->anchor
->num_disks
;
8046 super
->anchor
->num_disks
++;
8049 disk
->status
|= CONFIGURED_DISK
;
8050 disk
->status
&= ~SPARE_DISK
;
8053 to_state
= imsm_check_degraded(super
, dev
, failed
, MAP_0
);
8054 if (!second_map_created
) {
8055 second_map_created
= 1;
8056 map
->map_state
= IMSM_T_STATE_DEGRADED
;
8057 migrate(dev
, super
, to_state
, MIGR_REBUILD
);
8059 map
->map_state
= to_state
;
8060 migr_map
= get_imsm_map(dev
, MAP_1
);
8061 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
8062 set_imsm_ord_tbl_ent(migr_map
, u
->slot
,
8063 dl
->index
| IMSM_ORD_REBUILD
);
8065 /* update the family_num to mark a new container
8066 * generation, being careful to record the existing
8067 * family_num in orig_family_num to clean up after
8068 * earlier mdadm versions that neglected to set it.
8070 if (mpb
->orig_family_num
== 0)
8071 mpb
->orig_family_num
= mpb
->family_num
;
8072 mpb
->family_num
+= super
->random
;
8074 /* count arrays using the victim in the metadata */
8076 for (a
= active_array
; a
; a
= a
->next
) {
8077 dev
= get_imsm_dev(super
, a
->info
.container_member
);
8078 map
= get_imsm_map(dev
, MAP_0
);
8080 if (get_imsm_disk_slot(map
, victim
) >= 0)
8084 /* delete the victim if it is no longer being
8090 /* We know that 'manager' isn't touching anything,
8091 * so it is safe to delete
8093 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
8094 if ((*dlp
)->index
== victim
)
8097 /* victim may be on the missing list */
8099 for (dlp
= &super
->missing
; *dlp
;
8100 dlp
= &(*dlp
)->next
)
8101 if ((*dlp
)->index
== victim
)
8103 imsm_delete(super
, dlp
, victim
);
8110 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
8111 struct intel_super
*super
,
8114 struct dl
*new_disk
;
8115 struct intel_dev
*id
;
8117 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
8118 int disk_count
= u
->old_raid_disks
;
8119 void **tofree
= NULL
;
8120 int devices_to_reshape
= 1;
8121 struct imsm_super
*mpb
= super
->anchor
;
8123 unsigned int dev_id
;
8125 dprintf("imsm: apply_reshape_container_disks_update()\n");
8127 /* enable spares to use in array */
8128 for (i
= 0; i
< delta_disks
; i
++) {
8129 new_disk
= get_disk_super(super
,
8130 major(u
->new_disks
[i
]),
8131 minor(u
->new_disks
[i
]));
8132 dprintf("imsm: new disk for reshape is: %i:%i "
8133 "(%p, index = %i)\n",
8134 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
8135 new_disk
, new_disk
->index
);
8136 if ((new_disk
== NULL
) ||
8137 ((new_disk
->index
>= 0) &&
8138 (new_disk
->index
< u
->old_raid_disks
)))
8139 goto update_reshape_exit
;
8140 new_disk
->index
= disk_count
++;
8141 /* slot to fill in autolayout
8143 new_disk
->raiddisk
= new_disk
->index
;
8144 new_disk
->disk
.status
|=
8146 new_disk
->disk
.status
&= ~SPARE_DISK
;
8149 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
8150 mpb
->num_raid_devs
);
8151 /* manage changes in volume
8153 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
8154 void **sp
= *space_list
;
8155 struct imsm_dev
*newdev
;
8156 struct imsm_map
*newmap
, *oldmap
;
8158 for (id
= super
->devlist
; id
; id
= id
->next
) {
8159 if (id
->index
== dev_id
)
8168 /* Copy the dev, but not (all of) the map */
8169 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
8170 oldmap
= get_imsm_map(id
->dev
, MAP_0
);
8171 newmap
= get_imsm_map(newdev
, MAP_0
);
8172 /* Copy the current map */
8173 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8174 /* update one device only
8176 if (devices_to_reshape
) {
8177 dprintf("imsm: modifying subdev: %i\n",
8179 devices_to_reshape
--;
8180 newdev
->vol
.migr_state
= 1;
8181 newdev
->vol
.curr_migr_unit
= 0;
8182 set_migr_type(newdev
, MIGR_GEN_MIGR
);
8183 newmap
->num_members
= u
->new_raid_disks
;
8184 for (i
= 0; i
< delta_disks
; i
++) {
8185 set_imsm_ord_tbl_ent(newmap
,
8186 u
->old_raid_disks
+ i
,
8187 u
->old_raid_disks
+ i
);
8189 /* New map is correct, now need to save old map
8191 newmap
= get_imsm_map(newdev
, MAP_1
);
8192 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
8194 imsm_set_array_size(newdev
, -1);
8197 sp
= (void **)id
->dev
;
8202 /* Clear migration record */
8203 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
8206 *space_list
= tofree
;
8209 update_reshape_exit
:
8214 static int apply_takeover_update(struct imsm_update_takeover
*u
,
8215 struct intel_super
*super
,
8218 struct imsm_dev
*dev
= NULL
;
8219 struct intel_dev
*dv
;
8220 struct imsm_dev
*dev_new
;
8221 struct imsm_map
*map
;
8225 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
8226 if (dv
->index
== (unsigned int)u
->subarray
) {
8234 map
= get_imsm_map(dev
, MAP_0
);
8236 if (u
->direction
== R10_TO_R0
) {
8237 /* Number of failed disks must be half of initial disk number */
8238 if (imsm_count_failed(super
, dev
, MAP_0
) !=
8239 (map
->num_members
/ 2))
8242 /* iterate through devices to mark removed disks as spare */
8243 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8244 if (dm
->disk
.status
& FAILED_DISK
) {
8245 int idx
= dm
->index
;
8246 /* update indexes on the disk list */
8247 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
8248 the index values will end up being correct.... NB */
8249 for (du
= super
->disks
; du
; du
= du
->next
)
8250 if (du
->index
> idx
)
8252 /* mark as spare disk */
8257 map
->num_members
= map
->num_members
/ 2;
8258 map
->map_state
= IMSM_T_STATE_NORMAL
;
8259 map
->num_domains
= 1;
8260 map
->raid_level
= 0;
8261 map
->failed_disk_num
= -1;
8264 if (u
->direction
== R0_TO_R10
) {
8266 /* update slots in current disk list */
8267 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
8271 /* create new *missing* disks */
8272 for (i
= 0; i
< map
->num_members
; i
++) {
8273 space
= *space_list
;
8276 *space_list
= *space
;
8278 memcpy(du
, super
->disks
, sizeof(*du
));
8282 du
->index
= (i
* 2) + 1;
8283 sprintf((char *)du
->disk
.serial
,
8284 " MISSING_%d", du
->index
);
8285 sprintf((char *)du
->serial
,
8286 "MISSING_%d", du
->index
);
8287 du
->next
= super
->missing
;
8288 super
->missing
= du
;
8290 /* create new dev and map */
8291 space
= *space_list
;
8294 *space_list
= *space
;
8295 dev_new
= (void *)space
;
8296 memcpy(dev_new
, dev
, sizeof(*dev
));
8297 /* update new map */
8298 map
= get_imsm_map(dev_new
, MAP_0
);
8299 map
->num_members
= map
->num_members
* 2;
8300 map
->map_state
= IMSM_T_STATE_DEGRADED
;
8301 map
->num_domains
= 2;
8302 map
->raid_level
= 1;
8303 /* replace dev<->dev_new */
8306 /* update disk order table */
8307 for (du
= super
->disks
; du
; du
= du
->next
)
8309 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8310 for (du
= super
->missing
; du
; du
= du
->next
)
8311 if (du
->index
>= 0) {
8312 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
8313 mark_missing(dv
->dev
, &du
->disk
, du
->index
);
8319 static void imsm_process_update(struct supertype
*st
,
8320 struct metadata_update
*update
)
8323 * crack open the metadata_update envelope to find the update record
8324 * update can be one of:
8325 * update_reshape_container_disks - all the arrays in the container
8326 * are being reshaped to have more devices. We need to mark
8327 * the arrays for general migration and convert selected spares
8328 * into active devices.
8329 * update_activate_spare - a spare device has replaced a failed
8330 * device in an array, update the disk_ord_tbl. If this disk is
8331 * present in all member arrays then also clear the SPARE_DISK
8333 * update_create_array
8335 * update_rename_array
8336 * update_add_remove_disk
8338 struct intel_super
*super
= st
->sb
;
8339 struct imsm_super
*mpb
;
8340 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
8342 /* update requires a larger buf but the allocation failed */
8343 if (super
->next_len
&& !super
->next_buf
) {
8344 super
->next_len
= 0;
8348 if (super
->next_buf
) {
8349 memcpy(super
->next_buf
, super
->buf
, super
->len
);
8351 super
->len
= super
->next_len
;
8352 super
->buf
= super
->next_buf
;
8354 super
->next_len
= 0;
8355 super
->next_buf
= NULL
;
8358 mpb
= super
->anchor
;
8361 case update_general_migration_checkpoint
: {
8362 struct intel_dev
*id
;
8363 struct imsm_update_general_migration_checkpoint
*u
=
8364 (void *)update
->buf
;
8366 dprintf("imsm: process_update() "
8367 "for update_general_migration_checkpoint called\n");
8369 /* find device under general migration */
8370 for (id
= super
->devlist
; id
; id
= id
->next
) {
8371 if (is_gen_migration(id
->dev
)) {
8372 id
->dev
->vol
.curr_migr_unit
=
8373 __cpu_to_le32(u
->curr_migr_unit
);
8374 super
->updates_pending
++;
8379 case update_takeover
: {
8380 struct imsm_update_takeover
*u
= (void *)update
->buf
;
8381 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
8382 imsm_update_version_info(super
);
8383 super
->updates_pending
++;
8388 case update_reshape_container_disks
: {
8389 struct imsm_update_reshape
*u
= (void *)update
->buf
;
8390 if (apply_reshape_container_disks_update(
8391 u
, super
, &update
->space_list
))
8392 super
->updates_pending
++;
8395 case update_reshape_migration
: {
8396 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
8397 if (apply_reshape_migration_update(
8398 u
, super
, &update
->space_list
))
8399 super
->updates_pending
++;
8402 case update_size_change
: {
8403 struct imsm_update_size_change
*u
= (void *)update
->buf
;
8404 if (apply_size_change_update(u
, super
))
8405 super
->updates_pending
++;
8408 case update_activate_spare
: {
8409 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
8410 if (apply_update_activate_spare(u
, super
, st
->arrays
))
8411 super
->updates_pending
++;
8414 case update_create_array
: {
8415 /* someone wants to create a new array, we need to be aware of
8416 * a few races/collisions:
8417 * 1/ 'Create' called by two separate instances of mdadm
8418 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
8419 * devices that have since been assimilated via
8421 * In the event this update can not be carried out mdadm will
8422 * (FIX ME) notice that its update did not take hold.
8424 struct imsm_update_create_array
*u
= (void *) update
->buf
;
8425 struct intel_dev
*dv
;
8426 struct imsm_dev
*dev
;
8427 struct imsm_map
*map
, *new_map
;
8428 unsigned long long start
, end
;
8429 unsigned long long new_start
, new_end
;
8431 struct disk_info
*inf
;
8434 /* handle racing creates: first come first serve */
8435 if (u
->dev_idx
< mpb
->num_raid_devs
) {
8436 dprintf("%s: subarray %d already defined\n",
8437 __func__
, u
->dev_idx
);
8441 /* check update is next in sequence */
8442 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
8443 dprintf("%s: can not create array %d expected index %d\n",
8444 __func__
, u
->dev_idx
, mpb
->num_raid_devs
);
8448 new_map
= get_imsm_map(&u
->dev
, MAP_0
);
8449 new_start
= pba_of_lba0(new_map
);
8450 new_end
= new_start
+ blocks_per_member(new_map
);
8451 inf
= get_disk_info(u
);
8453 /* handle activate_spare versus create race:
8454 * check to make sure that overlapping arrays do not include
8457 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8458 dev
= get_imsm_dev(super
, i
);
8459 map
= get_imsm_map(dev
, MAP_0
);
8460 start
= pba_of_lba0(map
);
8461 end
= start
+ blocks_per_member(map
);
8462 if ((new_start
>= start
&& new_start
<= end
) ||
8463 (start
>= new_start
&& start
<= new_end
))
8468 if (disks_overlap(super
, i
, u
)) {
8469 dprintf("%s: arrays overlap\n", __func__
);
8474 /* check that prepare update was successful */
8475 if (!update
->space
) {
8476 dprintf("%s: prepare update failed\n", __func__
);
8480 /* check that all disks are still active before committing
8481 * changes. FIXME: could we instead handle this by creating a
8482 * degraded array? That's probably not what the user expects,
8483 * so better to drop this update on the floor.
8485 for (i
= 0; i
< new_map
->num_members
; i
++) {
8486 dl
= serial_to_dl(inf
[i
].serial
, super
);
8488 dprintf("%s: disk disappeared\n", __func__
);
8493 super
->updates_pending
++;
8495 /* convert spares to members and fixup ord_tbl */
8496 for (i
= 0; i
< new_map
->num_members
; i
++) {
8497 dl
= serial_to_dl(inf
[i
].serial
, super
);
8498 if (dl
->index
== -1) {
8499 dl
->index
= mpb
->num_disks
;
8501 dl
->disk
.status
|= CONFIGURED_DISK
;
8502 dl
->disk
.status
&= ~SPARE_DISK
;
8504 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
8509 update
->space
= NULL
;
8510 imsm_copy_dev(dev
, &u
->dev
);
8511 dv
->index
= u
->dev_idx
;
8512 dv
->next
= super
->devlist
;
8513 super
->devlist
= dv
;
8514 mpb
->num_raid_devs
++;
8516 imsm_update_version_info(super
);
8519 /* mdmon knows how to release update->space, but not
8520 * ((struct intel_dev *) update->space)->dev
8522 if (update
->space
) {
8528 case update_kill_array
: {
8529 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
8530 int victim
= u
->dev_idx
;
8531 struct active_array
*a
;
8532 struct intel_dev
**dp
;
8533 struct imsm_dev
*dev
;
8535 /* sanity check that we are not affecting the uuid of
8536 * active arrays, or deleting an active array
8538 * FIXME when immutable ids are available, but note that
8539 * we'll also need to fixup the invalidated/active
8540 * subarray indexes in mdstat
8542 for (a
= st
->arrays
; a
; a
= a
->next
)
8543 if (a
->info
.container_member
>= victim
)
8545 /* by definition if mdmon is running at least one array
8546 * is active in the container, so checking
8547 * mpb->num_raid_devs is just extra paranoia
8549 dev
= get_imsm_dev(super
, victim
);
8550 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
8551 dprintf("failed to delete subarray-%d\n", victim
);
8555 for (dp
= &super
->devlist
; *dp
;)
8556 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
8559 if ((*dp
)->index
> (unsigned)victim
)
8563 mpb
->num_raid_devs
--;
8564 super
->updates_pending
++;
8567 case update_rename_array
: {
8568 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
8569 char name
[MAX_RAID_SERIAL_LEN
+1];
8570 int target
= u
->dev_idx
;
8571 struct active_array
*a
;
8572 struct imsm_dev
*dev
;
8574 /* sanity check that we are not affecting the uuid of
8577 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
8578 name
[MAX_RAID_SERIAL_LEN
] = '\0';
8579 for (a
= st
->arrays
; a
; a
= a
->next
)
8580 if (a
->info
.container_member
== target
)
8582 dev
= get_imsm_dev(super
, u
->dev_idx
);
8583 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
8584 dprintf("failed to rename subarray-%d\n", target
);
8588 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
8589 super
->updates_pending
++;
8592 case update_add_remove_disk
: {
8593 /* we may be able to repair some arrays if disks are
8594 * being added, check teh status of add_remove_disk
8595 * if discs has been added.
8597 if (add_remove_disk_update(super
)) {
8598 struct active_array
*a
;
8600 super
->updates_pending
++;
8601 for (a
= st
->arrays
; a
; a
= a
->next
)
8602 a
->check_degraded
= 1;
8607 fprintf(stderr
, "error: unsuported process update type:"
8608 "(type: %d)\n", type
);
8612 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
);
8614 static void imsm_prepare_update(struct supertype
*st
,
8615 struct metadata_update
*update
)
8618 * Allocate space to hold new disk entries, raid-device entries or a new
8619 * mpb if necessary. The manager synchronously waits for updates to
8620 * complete in the monitor, so new mpb buffers allocated here can be
8621 * integrated by the monitor thread without worrying about live pointers
8622 * in the manager thread.
8624 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
8625 struct intel_super
*super
= st
->sb
;
8626 struct imsm_super
*mpb
= super
->anchor
;
8631 case update_general_migration_checkpoint
:
8632 dprintf("imsm: prepare_update() "
8633 "for update_general_migration_checkpoint called\n");
8635 case update_takeover
: {
8636 struct imsm_update_takeover
*u
= (void *)update
->buf
;
8637 if (u
->direction
== R0_TO_R10
) {
8638 void **tail
= (void **)&update
->space_list
;
8639 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
8640 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8641 int num_members
= map
->num_members
;
8644 /* allocate memory for added disks */
8645 for (i
= 0; i
< num_members
; i
++) {
8646 size
= sizeof(struct dl
);
8647 space
= xmalloc(size
);
8652 /* allocate memory for new device */
8653 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
8654 (num_members
* sizeof(__u32
));
8655 space
= xmalloc(size
);
8659 len
= disks_to_mpb_size(num_members
* 2);
8664 case update_reshape_container_disks
: {
8665 /* Every raid device in the container is about to
8666 * gain some more devices, and we will enter a
8668 * So each 'imsm_map' will be bigger, and the imsm_vol
8669 * will now hold 2 of them.
8670 * Thus we need new 'struct imsm_dev' allocations sized
8671 * as sizeof_imsm_dev but with more devices in both maps.
8673 struct imsm_update_reshape
*u
= (void *)update
->buf
;
8674 struct intel_dev
*dl
;
8675 void **space_tail
= (void**)&update
->space_list
;
8677 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
8679 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
8680 int size
= sizeof_imsm_dev(dl
->dev
, 1);
8682 if (u
->new_raid_disks
> u
->old_raid_disks
)
8683 size
+= sizeof(__u32
)*2*
8684 (u
->new_raid_disks
- u
->old_raid_disks
);
8691 len
= disks_to_mpb_size(u
->new_raid_disks
);
8692 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
8695 case update_reshape_migration
: {
8696 /* for migration level 0->5 we need to add disks
8697 * so the same as for container operation we will copy
8698 * device to the bigger location.
8699 * in memory prepared device and new disk area are prepared
8700 * for usage in process update
8702 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
8703 struct intel_dev
*id
;
8704 void **space_tail
= (void **)&update
->space_list
;
8707 int current_level
= -1;
8709 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
8711 /* add space for bigger array in update
8713 for (id
= super
->devlist
; id
; id
= id
->next
) {
8714 if (id
->index
== (unsigned)u
->subdev
) {
8715 size
= sizeof_imsm_dev(id
->dev
, 1);
8716 if (u
->new_raid_disks
> u
->old_raid_disks
)
8717 size
+= sizeof(__u32
)*2*
8718 (u
->new_raid_disks
- u
->old_raid_disks
);
8726 if (update
->space_list
== NULL
)
8729 /* add space for disk in update
8731 size
= sizeof(struct dl
);
8737 /* add spare device to update
8739 for (id
= super
->devlist
; id
; id
= id
->next
)
8740 if (id
->index
== (unsigned)u
->subdev
) {
8741 struct imsm_dev
*dev
;
8742 struct imsm_map
*map
;
8744 dev
= get_imsm_dev(super
, u
->subdev
);
8745 map
= get_imsm_map(dev
, MAP_0
);
8746 current_level
= map
->raid_level
;
8749 if ((u
->new_level
== 5) && (u
->new_level
!= current_level
)) {
8750 struct mdinfo
*spares
;
8752 spares
= get_spares_for_grow(st
);
8760 makedev(dev
->disk
.major
,
8762 dl
= get_disk_super(super
,
8765 dl
->index
= u
->old_raid_disks
;
8771 len
= disks_to_mpb_size(u
->new_raid_disks
);
8772 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
8775 case update_size_change
: {
8778 case update_create_array
: {
8779 struct imsm_update_create_array
*u
= (void *) update
->buf
;
8780 struct intel_dev
*dv
;
8781 struct imsm_dev
*dev
= &u
->dev
;
8782 struct imsm_map
*map
= get_imsm_map(dev
, MAP_0
);
8784 struct disk_info
*inf
;
8788 inf
= get_disk_info(u
);
8789 len
= sizeof_imsm_dev(dev
, 1);
8790 /* allocate a new super->devlist entry */
8791 dv
= xmalloc(sizeof(*dv
));
8792 dv
->dev
= xmalloc(len
);
8795 /* count how many spares will be converted to members */
8796 for (i
= 0; i
< map
->num_members
; i
++) {
8797 dl
= serial_to_dl(inf
[i
].serial
, super
);
8799 /* hmm maybe it failed?, nothing we can do about
8804 if (count_memberships(dl
, super
) == 0)
8807 len
+= activate
* sizeof(struct imsm_disk
);
8814 /* check if we need a larger metadata buffer */
8815 if (super
->next_buf
)
8816 buf_len
= super
->next_len
;
8818 buf_len
= super
->len
;
8820 if (__le32_to_cpu(mpb
->mpb_size
) + len
> buf_len
) {
8821 /* ok we need a larger buf than what is currently allocated
8822 * if this allocation fails process_update will notice that
8823 * ->next_len is set and ->next_buf is NULL
8825 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + len
, 512);
8826 if (super
->next_buf
)
8827 free(super
->next_buf
);
8829 super
->next_len
= buf_len
;
8830 if (posix_memalign(&super
->next_buf
, 512, buf_len
) == 0)
8831 memset(super
->next_buf
, 0, buf_len
);
8833 super
->next_buf
= NULL
;
8837 /* must be called while manager is quiesced */
8838 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
8840 struct imsm_super
*mpb
= super
->anchor
;
8842 struct imsm_dev
*dev
;
8843 struct imsm_map
*map
;
8844 int i
, j
, num_members
;
8847 dprintf("%s: deleting device[%d] from imsm_super\n",
8850 /* shift all indexes down one */
8851 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
8852 if (iter
->index
> (int)index
)
8854 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
8855 if (iter
->index
> (int)index
)
8858 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
8859 dev
= get_imsm_dev(super
, i
);
8860 map
= get_imsm_map(dev
, MAP_0
);
8861 num_members
= map
->num_members
;
8862 for (j
= 0; j
< num_members
; j
++) {
8863 /* update ord entries being careful not to propagate
8864 * ord-flags to the first map
8866 ord
= get_imsm_ord_tbl_ent(dev
, j
, MAP_X
);
8868 if (ord_to_idx(ord
) <= index
)
8871 map
= get_imsm_map(dev
, MAP_0
);
8872 set_imsm_ord_tbl_ent(map
, j
, ord_to_idx(ord
- 1));
8873 map
= get_imsm_map(dev
, MAP_1
);
8875 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
8880 super
->updates_pending
++;
8882 struct dl
*dl
= *dlp
;
8884 *dlp
= (*dlp
)->next
;
8885 __free_imsm_disk(dl
);
8888 #endif /* MDASSEMBLE */
8890 static void close_targets(int *targets
, int new_disks
)
8897 for (i
= 0; i
< new_disks
; i
++) {
8898 if (targets
[i
] >= 0) {
8905 static int imsm_get_allowed_degradation(int level
, int raid_disks
,
8906 struct intel_super
*super
,
8907 struct imsm_dev
*dev
)
8913 struct imsm_map
*map
;
8916 ret_val
= raid_disks
/2;
8917 /* check map if all disks pairs not failed
8920 map
= get_imsm_map(dev
, MAP_0
);
8921 for (i
= 0; i
< ret_val
; i
++) {
8922 int degradation
= 0;
8923 if (get_imsm_disk(super
, i
) == NULL
)
8925 if (get_imsm_disk(super
, i
+ 1) == NULL
)
8927 if (degradation
== 2)
8930 map
= get_imsm_map(dev
, MAP_1
);
8931 /* if there is no second map
8932 * result can be returned
8936 /* check degradation in second map
8938 for (i
= 0; i
< ret_val
; i
++) {
8939 int degradation
= 0;
8940 if (get_imsm_disk(super
, i
) == NULL
)
8942 if (get_imsm_disk(super
, i
+ 1) == NULL
)
8944 if (degradation
== 2)
8959 /*******************************************************************************
8960 * Function: open_backup_targets
8961 * Description: Function opens file descriptors for all devices given in
8964 * info : general array info
8965 * raid_disks : number of disks
8966 * raid_fds : table of device's file descriptors
8967 * super : intel super for raid10 degradation check
8968 * dev : intel device for raid10 degradation check
8972 ******************************************************************************/
8973 int open_backup_targets(struct mdinfo
*info
, int raid_disks
, int *raid_fds
,
8974 struct intel_super
*super
, struct imsm_dev
*dev
)
8980 for (i
= 0; i
< raid_disks
; i
++)
8983 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
8986 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
)) {
8987 dprintf("disk is faulty!!\n");
8991 if ((sd
->disk
.raid_disk
>= raid_disks
) ||
8992 (sd
->disk
.raid_disk
< 0))
8995 dn
= map_dev(sd
->disk
.major
,
8997 raid_fds
[sd
->disk
.raid_disk
] = dev_open(dn
, O_RDWR
);
8998 if (raid_fds
[sd
->disk
.raid_disk
] < 0) {
8999 fprintf(stderr
, "cannot open component\n");
9004 /* check if maximum array degradation level is not exceeded
9006 if ((raid_disks
- opened
) >
9007 imsm_get_allowed_degradation(info
->new_level
,
9010 fprintf(stderr
, "Not enough disks can be opened.\n");
9011 close_targets(raid_fds
, raid_disks
);
9018 /*******************************************************************************
9019 * Function: init_migr_record_imsm
9020 * Description: Function inits imsm migration record
9022 * super : imsm internal array info
9023 * dev : device under migration
9024 * info : general array info to find the smallest device
9027 ******************************************************************************/
9028 void init_migr_record_imsm(struct supertype
*st
, struct imsm_dev
*dev
,
9029 struct mdinfo
*info
)
9031 struct intel_super
*super
= st
->sb
;
9032 struct migr_record
*migr_rec
= super
->migr_rec
;
9034 unsigned long long dsize
, dev_sectors
;
9035 long long unsigned min_dev_sectors
= -1LLU;
9039 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
9040 struct imsm_map
*map_src
= get_imsm_map(dev
, MAP_1
);
9041 unsigned long long num_migr_units
;
9042 unsigned long long array_blocks
;
9044 memset(migr_rec
, 0, sizeof(struct migr_record
));
9045 migr_rec
->family_num
= __cpu_to_le32(super
->anchor
->family_num
);
9047 /* only ascending reshape supported now */
9048 migr_rec
->ascending_migr
= __cpu_to_le32(1);
9050 migr_rec
->dest_depth_per_unit
= GEN_MIGR_AREA_SIZE
/
9051 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
9052 migr_rec
->dest_depth_per_unit
*=
9053 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
9054 new_data_disks
= imsm_num_data_members(dev
, MAP_0
);
9055 migr_rec
->blocks_per_unit
=
9056 __cpu_to_le32(migr_rec
->dest_depth_per_unit
* new_data_disks
);
9057 migr_rec
->dest_depth_per_unit
=
9058 __cpu_to_le32(migr_rec
->dest_depth_per_unit
);
9059 array_blocks
= info
->component_size
* new_data_disks
;
9061 array_blocks
/ __le32_to_cpu(migr_rec
->blocks_per_unit
);
9063 if (array_blocks
% __le32_to_cpu(migr_rec
->blocks_per_unit
))
9065 migr_rec
->num_migr_units
= __cpu_to_le32(num_migr_units
);
9067 migr_rec
->post_migr_vol_cap
= dev
->size_low
;
9068 migr_rec
->post_migr_vol_cap_hi
= dev
->size_high
;
9071 /* Find the smallest dev */
9072 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
9073 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
9074 fd
= dev_open(nm
, O_RDONLY
);
9077 get_dev_size(fd
, NULL
, &dsize
);
9078 dev_sectors
= dsize
/ 512;
9079 if (dev_sectors
< min_dev_sectors
)
9080 min_dev_sectors
= dev_sectors
;
9083 migr_rec
->ckpt_area_pba
= __cpu_to_le32(min_dev_sectors
-
9084 RAID_DISK_RESERVED_BLOCKS_IMSM_HI
);
9086 write_imsm_migr_rec(st
);
9091 /*******************************************************************************
9092 * Function: save_backup_imsm
9093 * Description: Function saves critical data stripes to Migration Copy Area
9094 * and updates the current migration unit status.
9095 * Use restore_stripes() to form a destination stripe,
9096 * and to write it to the Copy Area.
9098 * st : supertype information
9099 * dev : imsm device that backup is saved for
9100 * info : general array info
9101 * buf : input buffer
9102 * length : length of data to backup (blocks_per_unit)
9106 ******************************************************************************/
9107 int save_backup_imsm(struct supertype
*st
,
9108 struct imsm_dev
*dev
,
9109 struct mdinfo
*info
,
9114 struct intel_super
*super
= st
->sb
;
9115 unsigned long long *target_offsets
= NULL
;
9116 int *targets
= NULL
;
9118 struct imsm_map
*map_dest
= get_imsm_map(dev
, MAP_0
);
9119 int new_disks
= map_dest
->num_members
;
9120 int dest_layout
= 0;
9122 unsigned long long start
;
9123 int data_disks
= imsm_num_data_members(dev
, MAP_0
);
9125 targets
= xmalloc(new_disks
* sizeof(int));
9127 for (i
= 0; i
< new_disks
; i
++)
9130 target_offsets
= xcalloc(new_disks
, sizeof(unsigned long long));
9132 start
= info
->reshape_progress
* 512;
9133 for (i
= 0; i
< new_disks
; i
++) {
9134 target_offsets
[i
] = (unsigned long long)
9135 __le32_to_cpu(super
->migr_rec
->ckpt_area_pba
) * 512;
9136 /* move back copy area adderss, it will be moved forward
9137 * in restore_stripes() using start input variable
9139 target_offsets
[i
] -= start
/data_disks
;
9142 if (open_backup_targets(info
, new_disks
, targets
,
9146 dest_layout
= imsm_level_to_layout(map_dest
->raid_level
);
9147 dest_chunk
= __le16_to_cpu(map_dest
->blocks_per_strip
) * 512;
9149 if (restore_stripes(targets
, /* list of dest devices */
9150 target_offsets
, /* migration record offsets */
9153 map_dest
->raid_level
,
9155 -1, /* source backup file descriptor */
9156 0, /* input buf offset
9157 * always 0 buf is already offseted */
9161 pr_err("Error restoring stripes\n");
9169 close_targets(targets
, new_disks
);
9172 free(target_offsets
);
9177 /*******************************************************************************
9178 * Function: save_checkpoint_imsm
9179 * Description: Function called for current unit status update
9180 * in the migration record. It writes it to disk.
9182 * super : imsm internal array info
9183 * info : general array info
9187 * 2: failure, means no valid migration record
9188 * / no general migration in progress /
9189 ******************************************************************************/
9190 int save_checkpoint_imsm(struct supertype
*st
, struct mdinfo
*info
, int state
)
9192 struct intel_super
*super
= st
->sb
;
9193 unsigned long long blocks_per_unit
;
9194 unsigned long long curr_migr_unit
;
9196 if (load_imsm_migr_rec(super
, info
) != 0) {
9197 dprintf("imsm: ERROR: Cannot read migration record "
9198 "for checkpoint save.\n");
9202 blocks_per_unit
= __le32_to_cpu(super
->migr_rec
->blocks_per_unit
);
9203 if (blocks_per_unit
== 0) {
9204 dprintf("imsm: no migration in progress.\n");
9207 curr_migr_unit
= info
->reshape_progress
/ blocks_per_unit
;
9208 /* check if array is alligned to copy area
9209 * if it is not alligned, add one to current migration unit value
9210 * this can happend on array reshape finish only
9212 if (info
->reshape_progress
% blocks_per_unit
)
9215 super
->migr_rec
->curr_migr_unit
=
9216 __cpu_to_le32(curr_migr_unit
);
9217 super
->migr_rec
->rec_status
= __cpu_to_le32(state
);
9218 super
->migr_rec
->dest_1st_member_lba
=
9219 __cpu_to_le32(curr_migr_unit
*
9220 __le32_to_cpu(super
->migr_rec
->dest_depth_per_unit
));
9221 if (write_imsm_migr_rec(st
) < 0) {
9222 dprintf("imsm: Cannot write migration record "
9223 "outside backup area\n");
9230 /*******************************************************************************
9231 * Function: recover_backup_imsm
9232 * Description: Function recovers critical data from the Migration Copy Area
9233 * while assembling an array.
9235 * super : imsm internal array info
9236 * info : general array info
9238 * 0 : success (or there is no data to recover)
9240 ******************************************************************************/
9241 int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
)
9243 struct intel_super
*super
= st
->sb
;
9244 struct migr_record
*migr_rec
= super
->migr_rec
;
9245 struct imsm_map
*map_dest
= NULL
;
9246 struct intel_dev
*id
= NULL
;
9247 unsigned long long read_offset
;
9248 unsigned long long write_offset
;
9250 int *targets
= NULL
;
9251 int new_disks
, i
, err
;
9254 unsigned long curr_migr_unit
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
9255 unsigned long num_migr_units
= __le32_to_cpu(migr_rec
->num_migr_units
);
9257 int skipped_disks
= 0;
9259 err
= sysfs_get_str(info
, NULL
, "array_state", (char *)buffer
, 20);
9263 /* recover data only during assemblation */
9264 if (strncmp(buffer
, "inactive", 8) != 0)
9266 /* no data to recover */
9267 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
9269 if (curr_migr_unit
>= num_migr_units
)
9272 /* find device during reshape */
9273 for (id
= super
->devlist
; id
; id
= id
->next
)
9274 if (is_gen_migration(id
->dev
))
9279 map_dest
= get_imsm_map(id
->dev
, MAP_0
);
9280 new_disks
= map_dest
->num_members
;
9282 read_offset
= (unsigned long long)
9283 __le32_to_cpu(migr_rec
->ckpt_area_pba
) * 512;
9285 write_offset
= ((unsigned long long)
9286 __le32_to_cpu(migr_rec
->dest_1st_member_lba
) +
9287 pba_of_lba0(map_dest
)) * 512;
9289 unit_len
= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
9290 if (posix_memalign((void **)&buf
, 512, unit_len
) != 0)
9292 targets
= xcalloc(new_disks
, sizeof(int));
9294 if (open_backup_targets(info
, new_disks
, targets
, super
, id
->dev
)) {
9295 pr_err("Cannot open some devices belonging to array.\n");
9299 for (i
= 0; i
< new_disks
; i
++) {
9300 if (targets
[i
] < 0) {
9304 if (lseek64(targets
[i
], read_offset
, SEEK_SET
) < 0) {
9305 pr_err("Cannot seek to block: %s\n",
9310 if ((unsigned)read(targets
[i
], buf
, unit_len
) != unit_len
) {
9311 pr_err("Cannot read copy area block: %s\n",
9316 if (lseek64(targets
[i
], write_offset
, SEEK_SET
) < 0) {
9317 pr_err("Cannot seek to block: %s\n",
9322 if ((unsigned)write(targets
[i
], buf
, unit_len
) != unit_len
) {
9323 pr_err("Cannot restore block: %s\n",
9330 if (skipped_disks
> imsm_get_allowed_degradation(info
->new_level
,
9334 pr_err("Cannot restore data from backup."
9335 " Too many failed disks\n");
9339 if (save_checkpoint_imsm(st
, info
, UNIT_SRC_NORMAL
)) {
9340 /* ignore error == 2, this can mean end of reshape here
9342 dprintf("imsm: Cannot write checkpoint to "
9343 "migration record (UNIT_SRC_NORMAL) during restart\n");
9349 for (i
= 0; i
< new_disks
; i
++)
9358 static char disk_by_path
[] = "/dev/disk/by-path/";
9360 static const char *imsm_get_disk_controller_domain(const char *path
)
9362 char disk_path
[PATH_MAX
];
9366 strncpy(disk_path
, disk_by_path
, PATH_MAX
- 1);
9367 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
9368 if (stat(disk_path
, &st
) == 0) {
9369 struct sys_dev
* hba
;
9372 path
= devt_to_devpath(st
.st_rdev
);
9375 hba
= find_disk_attached_hba(-1, path
);
9376 if (hba
&& hba
->type
== SYS_DEV_SAS
)
9378 else if (hba
&& hba
->type
== SYS_DEV_SATA
)
9382 dprintf("path: %s hba: %s attached: %s\n",
9383 path
, (hba
) ? hba
->path
: "NULL", drv
);
9389 static char *imsm_find_array_devnm_by_subdev(int subdev
, char *container
)
9391 static char devnm
[32];
9392 char subdev_name
[20];
9393 struct mdstat_ent
*mdstat
;
9395 sprintf(subdev_name
, "%d", subdev
);
9396 mdstat
= mdstat_by_subdev(subdev_name
, container
);
9400 strcpy(devnm
, mdstat
->devnm
);
9401 free_mdstat(mdstat
);
9405 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
9406 struct geo_params
*geo
,
9407 int *old_raid_disks
,
9410 /* currently we only support increasing the number of devices
9411 * for a container. This increases the number of device for each
9412 * member array. They must all be RAID0 or RAID5.
9415 struct mdinfo
*info
, *member
;
9416 int devices_that_can_grow
= 0;
9418 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): "
9419 "st->devnm = (%s)\n", st
->devnm
);
9421 if (geo
->size
> 0 ||
9422 geo
->level
!= UnSet
||
9423 geo
->layout
!= UnSet
||
9424 geo
->chunksize
!= 0 ||
9425 geo
->raid_disks
== UnSet
) {
9426 dprintf("imsm: Container operation is allowed for "
9427 "raid disks number change only.\n");
9431 if (direction
== ROLLBACK_METADATA_CHANGES
) {
9432 dprintf("imsm: Metadata changes rollback is not supported for "
9433 "container operation.\n");
9437 info
= container_content_imsm(st
, NULL
);
9438 for (member
= info
; member
; member
= member
->next
) {
9441 dprintf("imsm: checking device_num: %i\n",
9442 member
->container_member
);
9444 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
9445 /* we work on container for Online Capacity Expansion
9446 * only so raid_disks has to grow
9448 dprintf("imsm: for container operation raid disks "
9449 "increase is required\n");
9453 if ((info
->array
.level
!= 0) &&
9454 (info
->array
.level
!= 5)) {
9455 /* we cannot use this container with other raid level
9457 dprintf("imsm: for container operation wrong"
9458 " raid level (%i) detected\n",
9462 /* check for platform support
9463 * for this raid level configuration
9465 struct intel_super
*super
= st
->sb
;
9466 if (!is_raid_level_supported(super
->orom
,
9467 member
->array
.level
,
9469 dprintf("platform does not support raid%d with"
9473 geo
->raid_disks
> 1 ? "s" : "");
9476 /* check if component size is aligned to chunk size
9478 if (info
->component_size
%
9479 (info
->array
.chunk_size
/512)) {
9480 dprintf("Component size is not aligned to "
9486 if (*old_raid_disks
&&
9487 info
->array
.raid_disks
!= *old_raid_disks
)
9489 *old_raid_disks
= info
->array
.raid_disks
;
9491 /* All raid5 and raid0 volumes in container
9492 * have to be ready for Online Capacity Expansion
9493 * so they need to be assembled. We have already
9494 * checked that no recovery etc is happening.
9496 result
= imsm_find_array_devnm_by_subdev(member
->container_member
,
9497 st
->container_devnm
);
9498 if (result
== NULL
) {
9499 dprintf("imsm: cannot find array\n");
9502 devices_that_can_grow
++;
9505 if (!member
&& devices_that_can_grow
)
9509 dprintf("\tContainer operation allowed\n");
9511 dprintf("\tError: %i\n", ret_val
);
9516 /* Function: get_spares_for_grow
9517 * Description: Allocates memory and creates list of spare devices
9518 * avaliable in container. Checks if spare drive size is acceptable.
9519 * Parameters: Pointer to the supertype structure
9520 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
9523 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
9525 unsigned long long min_size
= min_acceptable_spare_size_imsm(st
);
9526 return container_choose_spares(st
, min_size
, NULL
, NULL
, NULL
, 0);
9529 /******************************************************************************
9530 * function: imsm_create_metadata_update_for_reshape
9531 * Function creates update for whole IMSM container.
9533 ******************************************************************************/
9534 static int imsm_create_metadata_update_for_reshape(
9535 struct supertype
*st
,
9536 struct geo_params
*geo
,
9538 struct imsm_update_reshape
**updatep
)
9540 struct intel_super
*super
= st
->sb
;
9541 struct imsm_super
*mpb
= super
->anchor
;
9542 int update_memory_size
= 0;
9543 struct imsm_update_reshape
*u
= NULL
;
9544 struct mdinfo
*spares
= NULL
;
9546 int delta_disks
= 0;
9549 dprintf("imsm_update_metadata_for_reshape(enter) raid_disks = %i\n",
9552 delta_disks
= geo
->raid_disks
- old_raid_disks
;
9554 /* size of all update data without anchor */
9555 update_memory_size
= sizeof(struct imsm_update_reshape
);
9557 /* now add space for spare disks that we need to add. */
9558 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
9560 u
= xcalloc(1, update_memory_size
);
9561 u
->type
= update_reshape_container_disks
;
9562 u
->old_raid_disks
= old_raid_disks
;
9563 u
->new_raid_disks
= geo
->raid_disks
;
9565 /* now get spare disks list
9567 spares
= get_spares_for_grow(st
);
9570 || delta_disks
> spares
->array
.spare_disks
) {
9571 pr_err("imsm: ERROR: Cannot get spare devices "
9572 "for %s.\n", geo
->dev_name
);
9577 /* we have got spares
9578 * update disk list in imsm_disk list table in anchor
9580 dprintf("imsm: %i spares are available.\n\n",
9581 spares
->array
.spare_disks
);
9584 for (i
= 0; i
< delta_disks
; i
++) {
9589 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
9591 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
9592 dl
->index
= mpb
->num_disks
;
9602 dprintf("imsm: reshape update preparation :");
9603 if (i
== delta_disks
) {
9606 return update_memory_size
;
9609 dprintf(" Error\n");
9615 /******************************************************************************
9616 * function: imsm_create_metadata_update_for_size_change()
9617 * Creates update for IMSM array for array size change.
9619 ******************************************************************************/
9620 static int imsm_create_metadata_update_for_size_change(
9621 struct supertype
*st
,
9622 struct geo_params
*geo
,
9623 struct imsm_update_size_change
**updatep
)
9625 struct intel_super
*super
= st
->sb
;
9626 int update_memory_size
= 0;
9627 struct imsm_update_size_change
*u
= NULL
;
9629 dprintf("imsm_create_metadata_update_for_size_change(enter)"
9630 " New size = %llu\n", geo
->size
);
9632 /* size of all update data without anchor */
9633 update_memory_size
= sizeof(struct imsm_update_size_change
);
9635 u
= xcalloc(1, update_memory_size
);
9636 u
->type
= update_size_change
;
9637 u
->subdev
= super
->current_vol
;
9638 u
->new_size
= geo
->size
;
9640 dprintf("imsm: reshape update preparation : OK\n");
9643 return update_memory_size
;
9646 /******************************************************************************
9647 * function: imsm_create_metadata_update_for_migration()
9648 * Creates update for IMSM array.
9650 ******************************************************************************/
9651 static int imsm_create_metadata_update_for_migration(
9652 struct supertype
*st
,
9653 struct geo_params
*geo
,
9654 struct imsm_update_reshape_migration
**updatep
)
9656 struct intel_super
*super
= st
->sb
;
9657 int update_memory_size
= 0;
9658 struct imsm_update_reshape_migration
*u
= NULL
;
9659 struct imsm_dev
*dev
;
9660 int previous_level
= -1;
9662 dprintf("imsm_create_metadata_update_for_migration(enter)"
9663 " New Level = %i\n", geo
->level
);
9665 /* size of all update data without anchor */
9666 update_memory_size
= sizeof(struct imsm_update_reshape_migration
);
9668 u
= xcalloc(1, update_memory_size
);
9669 u
->type
= update_reshape_migration
;
9670 u
->subdev
= super
->current_vol
;
9671 u
->new_level
= geo
->level
;
9672 u
->new_layout
= geo
->layout
;
9673 u
->new_raid_disks
= u
->old_raid_disks
= geo
->raid_disks
;
9674 u
->new_disks
[0] = -1;
9675 u
->new_chunksize
= -1;
9677 dev
= get_imsm_dev(super
, u
->subdev
);
9679 struct imsm_map
*map
;
9681 map
= get_imsm_map(dev
, MAP_0
);
9683 int current_chunk_size
=
9684 __le16_to_cpu(map
->blocks_per_strip
) / 2;
9686 if (geo
->chunksize
!= current_chunk_size
) {
9687 u
->new_chunksize
= geo
->chunksize
/ 1024;
9689 "chunk size change from %i to %i\n",
9690 current_chunk_size
, u
->new_chunksize
);
9692 previous_level
= map
->raid_level
;
9695 if ((geo
->level
== 5) && (previous_level
== 0)) {
9696 struct mdinfo
*spares
= NULL
;
9698 u
->new_raid_disks
++;
9699 spares
= get_spares_for_grow(st
);
9700 if ((spares
== NULL
) || (spares
->array
.spare_disks
< 1)) {
9703 update_memory_size
= 0;
9704 dprintf("error: cannot get spare device "
9705 "for requested migration");
9710 dprintf("imsm: reshape update preparation : OK\n");
9713 return update_memory_size
;
9716 static void imsm_update_metadata_locally(struct supertype
*st
,
9719 struct metadata_update mu
;
9724 mu
.space_list
= NULL
;
9726 imsm_prepare_update(st
, &mu
);
9727 imsm_process_update(st
, &mu
);
9729 while (mu
.space_list
) {
9730 void **space
= mu
.space_list
;
9731 mu
.space_list
= *space
;
9736 /***************************************************************************
9737 * Function: imsm_analyze_change
9738 * Description: Function analyze change for single volume
9739 * and validate if transition is supported
9740 * Parameters: Geometry parameters, supertype structure,
9741 * metadata change direction (apply/rollback)
9742 * Returns: Operation type code on success, -1 if fail
9743 ****************************************************************************/
9744 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
9745 struct geo_params
*geo
,
9752 /* number of added/removed disks in operation result */
9753 int devNumChange
= 0;
9754 /* imsm compatible layout value for array geometry verification */
9755 int imsm_layout
= -1;
9757 struct imsm_dev
*dev
;
9758 struct intel_super
*super
;
9759 unsigned long long current_size
;
9760 unsigned long long free_size
;
9761 unsigned long long max_size
;
9764 getinfo_super_imsm_volume(st
, &info
, NULL
);
9765 if ((geo
->level
!= info
.array
.level
) &&
9766 (geo
->level
>= 0) &&
9767 (geo
->level
!= UnSet
)) {
9768 switch (info
.array
.level
) {
9770 if (geo
->level
== 5) {
9771 change
= CH_MIGRATION
;
9772 if (geo
->layout
!= ALGORITHM_LEFT_ASYMMETRIC
) {
9773 pr_err("Error. Requested Layout "
9774 "not supported (left-asymmetric layout "
9775 "is supported only)!\n");
9777 goto analyse_change_exit
;
9779 imsm_layout
= geo
->layout
;
9781 devNumChange
= 1; /* parity disk added */
9782 } else if (geo
->level
== 10) {
9783 change
= CH_TAKEOVER
;
9785 devNumChange
= 2; /* two mirrors added */
9786 imsm_layout
= 0x102; /* imsm supported layout */
9791 if (geo
->level
== 0) {
9792 change
= CH_TAKEOVER
;
9794 devNumChange
= -(geo
->raid_disks
/2);
9795 imsm_layout
= 0; /* imsm raid0 layout */
9800 pr_err("Error. Level Migration from %d to %d "
9802 info
.array
.level
, geo
->level
);
9803 goto analyse_change_exit
;
9806 geo
->level
= info
.array
.level
;
9808 if ((geo
->layout
!= info
.array
.layout
)
9809 && ((geo
->layout
!= UnSet
) && (geo
->layout
!= -1))) {
9810 change
= CH_MIGRATION
;
9811 if ((info
.array
.layout
== 0)
9812 && (info
.array
.level
== 5)
9813 && (geo
->layout
== 5)) {
9814 /* reshape 5 -> 4 */
9815 } else if ((info
.array
.layout
== 5)
9816 && (info
.array
.level
== 5)
9817 && (geo
->layout
== 0)) {
9818 /* reshape 4 -> 5 */
9822 pr_err("Error. Layout Migration from %d to %d "
9824 info
.array
.layout
, geo
->layout
);
9826 goto analyse_change_exit
;
9829 geo
->layout
= info
.array
.layout
;
9830 if (imsm_layout
== -1)
9831 imsm_layout
= info
.array
.layout
;
9834 if ((geo
->chunksize
> 0) && (geo
->chunksize
!= UnSet
)
9835 && (geo
->chunksize
!= info
.array
.chunk_size
))
9836 change
= CH_MIGRATION
;
9838 geo
->chunksize
= info
.array
.chunk_size
;
9840 chunk
= geo
->chunksize
/ 1024;
9843 dev
= get_imsm_dev(super
, super
->current_vol
);
9844 data_disks
= imsm_num_data_members(dev
, MAP_0
);
9845 /* compute current size per disk member
9847 current_size
= info
.custom_array_size
/ data_disks
;
9849 if ((geo
->size
> 0) && (geo
->size
!= MAX_SIZE
)) {
9850 /* align component size
9852 geo
->size
= imsm_component_size_aligment_check(
9853 get_imsm_raid_level(dev
->vol
.map
),
9856 if (geo
->size
== 0) {
9857 pr_err("Error. Size expansion is " \
9858 "supported only (current size is %llu, " \
9859 "requested size /rounded/ is 0).\n",
9861 goto analyse_change_exit
;
9865 if ((current_size
!= geo
->size
) && (geo
->size
> 0)) {
9867 pr_err("Error. Size change should be the only "
9868 "one at a time.\n");
9870 goto analyse_change_exit
;
9872 if ((super
->current_vol
+ 1) != super
->anchor
->num_raid_devs
) {
9873 pr_err("Error. The last volume in container "
9874 "can be expanded only (%i/%s).\n",
9875 super
->current_vol
, st
->devnm
);
9876 goto analyse_change_exit
;
9878 /* check the maximum available size
9880 rv
= imsm_get_free_size(st
, dev
->vol
.map
->num_members
,
9881 0, chunk
, &free_size
);
9883 /* Cannot find maximum available space
9887 max_size
= free_size
+ current_size
;
9888 /* align component size
9890 max_size
= imsm_component_size_aligment_check(
9891 get_imsm_raid_level(dev
->vol
.map
),
9895 if (geo
->size
== MAX_SIZE
) {
9896 /* requested size change to the maximum available size
9898 if (max_size
== 0) {
9899 pr_err("Error. Cannot find "
9900 "maximum available space.\n");
9902 goto analyse_change_exit
;
9904 geo
->size
= max_size
;
9907 if ((direction
== ROLLBACK_METADATA_CHANGES
)) {
9908 /* accept size for rollback only
9911 /* round size due to metadata compatibility
9913 geo
->size
= (geo
->size
>> SECT_PER_MB_SHIFT
)
9914 << SECT_PER_MB_SHIFT
;
9915 dprintf("Prepare update for size change to %llu\n",
9917 if (current_size
>= geo
->size
) {
9918 pr_err("Error. Size expansion is "
9919 "supported only (current size is %llu, "
9920 "requested size /rounded/ is %llu).\n",
9921 current_size
, geo
->size
);
9922 goto analyse_change_exit
;
9924 if (max_size
&& geo
->size
> max_size
) {
9925 pr_err("Error. Requested size is larger "
9926 "than maximum available size (maximum "
9927 "available size is %llu, "
9928 "requested size /rounded/ is %llu).\n",
9929 max_size
, geo
->size
);
9930 goto analyse_change_exit
;
9933 geo
->size
*= data_disks
;
9934 geo
->raid_disks
= dev
->vol
.map
->num_members
;
9935 change
= CH_ARRAY_SIZE
;
9937 if (!validate_geometry_imsm(st
,
9940 geo
->raid_disks
+ devNumChange
,
9942 geo
->size
, INVALID_SECTORS
,
9947 struct intel_super
*super
= st
->sb
;
9948 struct imsm_super
*mpb
= super
->anchor
;
9950 if (mpb
->num_raid_devs
> 1) {
9951 pr_err("Error. Cannot perform operation on %s"
9952 "- for this operation it MUST be single "
9953 "array in container\n",
9959 analyse_change_exit
:
9960 if ((direction
== ROLLBACK_METADATA_CHANGES
) &&
9961 ((change
== CH_MIGRATION
) || (change
== CH_TAKEOVER
))) {
9962 dprintf("imsm: Metadata changes rollback is not supported for "
9963 "migration and takeover operations.\n");
9969 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
9971 struct intel_super
*super
= st
->sb
;
9972 struct imsm_update_takeover
*u
;
9974 u
= xmalloc(sizeof(struct imsm_update_takeover
));
9976 u
->type
= update_takeover
;
9977 u
->subarray
= super
->current_vol
;
9979 /* 10->0 transition */
9980 if (geo
->level
== 0)
9981 u
->direction
= R10_TO_R0
;
9983 /* 0->10 transition */
9984 if (geo
->level
== 10)
9985 u
->direction
= R0_TO_R10
;
9987 /* update metadata locally */
9988 imsm_update_metadata_locally(st
, u
,
9989 sizeof(struct imsm_update_takeover
));
9990 /* and possibly remotely */
9991 if (st
->update_tail
)
9992 append_metadata_update(st
, u
,
9993 sizeof(struct imsm_update_takeover
));
10000 static int imsm_reshape_super(struct supertype
*st
, unsigned long long size
,
10002 int layout
, int chunksize
, int raid_disks
,
10003 int delta_disks
, char *backup
, char *dev
,
10004 int direction
, int verbose
)
10007 struct geo_params geo
;
10009 dprintf("imsm: reshape_super called.\n");
10011 memset(&geo
, 0, sizeof(struct geo_params
));
10013 geo
.dev_name
= dev
;
10014 strcpy(geo
.devnm
, st
->devnm
);
10017 geo
.layout
= layout
;
10018 geo
.chunksize
= chunksize
;
10019 geo
.raid_disks
= raid_disks
;
10020 if (delta_disks
!= UnSet
)
10021 geo
.raid_disks
+= delta_disks
;
10023 dprintf("\tfor level : %i\n", geo
.level
);
10024 dprintf("\tfor raid_disks : %i\n", geo
.raid_disks
);
10026 if (experimental() == 0)
10029 if (strcmp(st
->container_devnm
, st
->devnm
) == 0) {
10030 /* On container level we can only increase number of devices. */
10031 dprintf("imsm: info: Container operation\n");
10032 int old_raid_disks
= 0;
10034 if (imsm_reshape_is_allowed_on_container(
10035 st
, &geo
, &old_raid_disks
, direction
)) {
10036 struct imsm_update_reshape
*u
= NULL
;
10039 len
= imsm_create_metadata_update_for_reshape(
10040 st
, &geo
, old_raid_disks
, &u
);
10043 dprintf("imsm: Cannot prepare update\n");
10044 goto exit_imsm_reshape_super
;
10048 /* update metadata locally */
10049 imsm_update_metadata_locally(st
, u
, len
);
10050 /* and possibly remotely */
10051 if (st
->update_tail
)
10052 append_metadata_update(st
, u
, len
);
10057 pr_err("(imsm) Operation "
10058 "is not allowed on this container\n");
10061 /* On volume level we support following operations
10062 * - takeover: raid10 -> raid0; raid0 -> raid10
10063 * - chunk size migration
10064 * - migration: raid5 -> raid0; raid0 -> raid5
10066 struct intel_super
*super
= st
->sb
;
10067 struct intel_dev
*dev
= super
->devlist
;
10069 dprintf("imsm: info: Volume operation\n");
10070 /* find requested device */
10073 imsm_find_array_devnm_by_subdev(
10074 dev
->index
, st
->container_devnm
);
10075 if (devnm
&& strcmp(devnm
, geo
.devnm
) == 0)
10080 pr_err("Cannot find %s (%s) subarray\n",
10081 geo
.dev_name
, geo
.devnm
);
10082 goto exit_imsm_reshape_super
;
10084 super
->current_vol
= dev
->index
;
10085 change
= imsm_analyze_change(st
, &geo
, direction
);
10088 ret_val
= imsm_takeover(st
, &geo
);
10090 case CH_MIGRATION
: {
10091 struct imsm_update_reshape_migration
*u
= NULL
;
10093 imsm_create_metadata_update_for_migration(
10097 "Cannot prepare update\n");
10101 /* update metadata locally */
10102 imsm_update_metadata_locally(st
, u
, len
);
10103 /* and possibly remotely */
10104 if (st
->update_tail
)
10105 append_metadata_update(st
, u
, len
);
10110 case CH_ARRAY_SIZE
: {
10111 struct imsm_update_size_change
*u
= NULL
;
10113 imsm_create_metadata_update_for_size_change(
10117 "Cannot prepare update\n");
10121 /* update metadata locally */
10122 imsm_update_metadata_locally(st
, u
, len
);
10123 /* and possibly remotely */
10124 if (st
->update_tail
)
10125 append_metadata_update(st
, u
, len
);
10135 exit_imsm_reshape_super
:
10136 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
10140 /*******************************************************************************
10141 * Function: wait_for_reshape_imsm
10142 * Description: Function writes new sync_max value and waits until
10143 * reshape process reach new position
10145 * sra : general array info
10146 * ndata : number of disks in new array's layout
10149 * 1 : there is no reshape in progress,
10151 ******************************************************************************/
10152 int wait_for_reshape_imsm(struct mdinfo
*sra
, int ndata
)
10154 int fd
= sysfs_get_fd(sra
, NULL
, "reshape_position");
10155 unsigned long long completed
;
10156 /* to_complete : new sync_max position */
10157 unsigned long long to_complete
= sra
->reshape_progress
;
10158 unsigned long long position_to_set
= to_complete
/ ndata
;
10161 dprintf("imsm: wait_for_reshape_imsm() "
10162 "cannot open reshape_position\n");
10166 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
10167 dprintf("imsm: wait_for_reshape_imsm() "
10168 "cannot read reshape_position (no reshape in progres)\n");
10173 if (completed
> to_complete
) {
10174 dprintf("imsm: wait_for_reshape_imsm() "
10175 "wrong next position to set %llu (%llu)\n",
10176 to_complete
, completed
);
10180 dprintf("Position set: %llu\n", position_to_set
);
10181 if (sysfs_set_num(sra
, NULL
, "sync_max",
10182 position_to_set
) != 0) {
10183 dprintf("imsm: wait_for_reshape_imsm() "
10184 "cannot set reshape position to %llu\n",
10195 select(fd
+1, &rfds
, NULL
, NULL
, NULL
);
10196 if (sysfs_get_str(sra
, NULL
, "sync_action",
10198 strncmp(action
, "reshape", 7) != 0)
10200 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
10201 dprintf("imsm: wait_for_reshape_imsm() "
10202 "cannot read reshape_position (in loop)\n");
10206 } while (completed
< to_complete
);
10212 /*******************************************************************************
10213 * Function: check_degradation_change
10214 * Description: Check that array hasn't become failed.
10216 * info : for sysfs access
10217 * sources : source disks descriptors
10218 * degraded: previous degradation level
10220 * degradation level
10221 ******************************************************************************/
10222 int check_degradation_change(struct mdinfo
*info
,
10226 unsigned long long new_degraded
;
10229 rv
= sysfs_get_ll(info
, NULL
, "degraded", &new_degraded
);
10230 if ((rv
== -1) || (new_degraded
!= (unsigned long long)degraded
)) {
10231 /* check each device to ensure it is still working */
10234 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
10235 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
10237 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
10239 if (sysfs_get_str(info
,
10240 sd
, "state", sbuf
, 20) < 0 ||
10241 strstr(sbuf
, "faulty") ||
10242 strstr(sbuf
, "in_sync") == NULL
) {
10243 /* this device is dead */
10244 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
10245 if (sd
->disk
.raid_disk
>= 0 &&
10246 sources
[sd
->disk
.raid_disk
] >= 0) {
10248 sd
->disk
.raid_disk
]);
10249 sources
[sd
->disk
.raid_disk
] =
10258 return new_degraded
;
10261 /*******************************************************************************
10262 * Function: imsm_manage_reshape
10263 * Description: Function finds array under reshape and it manages reshape
10264 * process. It creates stripes backups (if required) and sets
10267 * afd : Backup handle (nattive) - not used
10268 * sra : general array info
10269 * reshape : reshape parameters - not used
10270 * st : supertype structure
10271 * blocks : size of critical section [blocks]
10272 * fds : table of source device descriptor
10273 * offsets : start of array (offest per devices)
10275 * destfd : table of destination device descriptor
10276 * destoffsets : table of destination offsets (per device)
10278 * 1 : success, reshape is done
10280 ******************************************************************************/
10281 static int imsm_manage_reshape(
10282 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
10283 struct supertype
*st
, unsigned long backup_blocks
,
10284 int *fds
, unsigned long long *offsets
,
10285 int dests
, int *destfd
, unsigned long long *destoffsets
)
10288 struct intel_super
*super
= st
->sb
;
10289 struct intel_dev
*dv
= NULL
;
10290 struct imsm_dev
*dev
= NULL
;
10291 struct imsm_map
*map_src
;
10292 int migr_vol_qan
= 0;
10293 int ndata
, odata
; /* [bytes] */
10294 int chunk
; /* [bytes] */
10295 struct migr_record
*migr_rec
;
10297 unsigned int buf_size
; /* [bytes] */
10298 unsigned long long max_position
; /* array size [bytes] */
10299 unsigned long long next_step
; /* [blocks]/[bytes] */
10300 unsigned long long old_data_stripe_length
;
10301 unsigned long long start_src
; /* [bytes] */
10302 unsigned long long start
; /* [bytes] */
10303 unsigned long long start_buf_shift
; /* [bytes] */
10305 int source_layout
= 0;
10307 if (!fds
|| !offsets
|| !sra
)
10310 /* Find volume during the reshape */
10311 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
10312 if (dv
->dev
->vol
.migr_type
== MIGR_GEN_MIGR
10313 && dv
->dev
->vol
.migr_state
== 1) {
10318 /* Only one volume can migrate at the same time */
10319 if (migr_vol_qan
!= 1) {
10320 pr_err(": %s", migr_vol_qan
?
10321 "Number of migrating volumes greater than 1\n" :
10322 "There is no volume during migrationg\n");
10326 map_src
= get_imsm_map(dev
, MAP_1
);
10327 if (map_src
== NULL
)
10330 ndata
= imsm_num_data_members(dev
, MAP_0
);
10331 odata
= imsm_num_data_members(dev
, MAP_1
);
10333 chunk
= __le16_to_cpu(map_src
->blocks_per_strip
) * 512;
10334 old_data_stripe_length
= odata
* chunk
;
10336 migr_rec
= super
->migr_rec
;
10338 /* initialize migration record for start condition */
10339 if (sra
->reshape_progress
== 0)
10340 init_migr_record_imsm(st
, dev
, sra
);
10342 if (__le32_to_cpu(migr_rec
->rec_status
) != UNIT_SRC_NORMAL
) {
10343 dprintf("imsm: cannot restart migration when data "
10344 "are present in copy area.\n");
10347 /* Save checkpoint to update migration record for current
10348 * reshape position (in md). It can be farther than current
10349 * reshape position in metadata.
10351 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
10352 /* ignore error == 2, this can mean end of reshape here
10354 dprintf("imsm: Cannot write checkpoint to "
10355 "migration record (UNIT_SRC_NORMAL, "
10356 "initial save)\n");
10361 /* size for data */
10362 buf_size
= __le32_to_cpu(migr_rec
->blocks_per_unit
) * 512;
10363 /* extend buffer size for parity disk */
10364 buf_size
+= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
10365 /* add space for stripe aligment */
10366 buf_size
+= old_data_stripe_length
;
10367 if (posix_memalign((void **)&buf
, 4096, buf_size
)) {
10368 dprintf("imsm: Cannot allocate checpoint buffer\n");
10372 max_position
= sra
->component_size
* ndata
;
10373 source_layout
= imsm_level_to_layout(map_src
->raid_level
);
10375 while (__le32_to_cpu(migr_rec
->curr_migr_unit
) <
10376 __le32_to_cpu(migr_rec
->num_migr_units
)) {
10377 /* current reshape position [blocks] */
10378 unsigned long long current_position
=
10379 __le32_to_cpu(migr_rec
->blocks_per_unit
)
10380 * __le32_to_cpu(migr_rec
->curr_migr_unit
);
10381 unsigned long long border
;
10383 /* Check that array hasn't become failed.
10385 degraded
= check_degradation_change(sra
, fds
, degraded
);
10386 if (degraded
> 1) {
10387 dprintf("imsm: Abort reshape due to degradation"
10388 " level (%i)\n", degraded
);
10392 next_step
= __le32_to_cpu(migr_rec
->blocks_per_unit
);
10394 if ((current_position
+ next_step
) > max_position
)
10395 next_step
= max_position
- current_position
;
10397 start
= current_position
* 512;
10399 /* allign reading start to old geometry */
10400 start_buf_shift
= start
% old_data_stripe_length
;
10401 start_src
= start
- start_buf_shift
;
10403 border
= (start_src
/ odata
) - (start
/ ndata
);
10405 if (border
<= __le32_to_cpu(migr_rec
->dest_depth_per_unit
)) {
10406 /* save critical stripes to buf
10407 * start - start address of current unit
10408 * to backup [bytes]
10409 * start_src - start address of current unit
10410 * to backup alligned to source array
10413 unsigned long long next_step_filler
= 0;
10414 unsigned long long copy_length
= next_step
* 512;
10416 /* allign copy area length to stripe in old geometry */
10417 next_step_filler
= ((copy_length
+ start_buf_shift
)
10418 % old_data_stripe_length
);
10419 if (next_step_filler
)
10420 next_step_filler
= (old_data_stripe_length
10421 - next_step_filler
);
10422 dprintf("save_stripes() parameters: start = %llu,"
10423 "\tstart_src = %llu,\tnext_step*512 = %llu,"
10424 "\tstart_in_buf_shift = %llu,"
10425 "\tnext_step_filler = %llu\n",
10426 start
, start_src
, copy_length
,
10427 start_buf_shift
, next_step_filler
);
10429 if (save_stripes(fds
, offsets
, map_src
->num_members
,
10430 chunk
, map_src
->raid_level
,
10431 source_layout
, 0, NULL
, start_src
,
10433 next_step_filler
+ start_buf_shift
,
10435 dprintf("imsm: Cannot save stripes"
10439 /* Convert data to destination format and store it
10440 * in backup general migration area
10442 if (save_backup_imsm(st
, dev
, sra
,
10443 buf
+ start_buf_shift
, copy_length
)) {
10444 dprintf("imsm: Cannot save stripes to "
10445 "target devices\n");
10448 if (save_checkpoint_imsm(st
, sra
,
10449 UNIT_SRC_IN_CP_AREA
)) {
10450 dprintf("imsm: Cannot write checkpoint to "
10451 "migration record (UNIT_SRC_IN_CP_AREA)\n");
10455 /* set next step to use whole border area */
10456 border
/= next_step
;
10458 next_step
*= border
;
10460 /* When data backed up, checkpoint stored,
10461 * kick the kernel to reshape unit of data
10463 next_step
= next_step
+ sra
->reshape_progress
;
10464 /* limit next step to array max position */
10465 if (next_step
> max_position
)
10466 next_step
= max_position
;
10467 sysfs_set_num(sra
, NULL
, "suspend_lo", sra
->reshape_progress
);
10468 sysfs_set_num(sra
, NULL
, "suspend_hi", next_step
);
10469 sra
->reshape_progress
= next_step
;
10471 /* wait until reshape finish */
10472 if (wait_for_reshape_imsm(sra
, ndata
) < 0) {
10473 dprintf("wait_for_reshape_imsm returned error!\n");
10477 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
10478 /* ignore error == 2, this can mean end of reshape here
10480 dprintf("imsm: Cannot write checkpoint to "
10481 "migration record (UNIT_SRC_NORMAL)\n");
10487 /* return '1' if done */
10491 abort_reshape(sra
);
10495 #endif /* MDASSEMBLE */
10497 struct superswitch super_imsm
= {
10499 .examine_super
= examine_super_imsm
,
10500 .brief_examine_super
= brief_examine_super_imsm
,
10501 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
10502 .export_examine_super
= export_examine_super_imsm
,
10503 .detail_super
= detail_super_imsm
,
10504 .brief_detail_super
= brief_detail_super_imsm
,
10505 .write_init_super
= write_init_super_imsm
,
10506 .validate_geometry
= validate_geometry_imsm
,
10507 .add_to_super
= add_to_super_imsm
,
10508 .remove_from_super
= remove_from_super_imsm
,
10509 .detail_platform
= detail_platform_imsm
,
10510 .export_detail_platform
= export_detail_platform_imsm
,
10511 .kill_subarray
= kill_subarray_imsm
,
10512 .update_subarray
= update_subarray_imsm
,
10513 .load_container
= load_container_imsm
,
10514 .default_geometry
= default_geometry_imsm
,
10515 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
10516 .reshape_super
= imsm_reshape_super
,
10517 .manage_reshape
= imsm_manage_reshape
,
10518 .recover_backup
= recover_backup_imsm
,
10519 .copy_metadata
= copy_metadata_imsm
,
10521 .match_home
= match_home_imsm
,
10522 .uuid_from_super
= uuid_from_super_imsm
,
10523 .getinfo_super
= getinfo_super_imsm
,
10524 .getinfo_super_disks
= getinfo_super_disks_imsm
,
10525 .update_super
= update_super_imsm
,
10527 .avail_size
= avail_size_imsm
,
10528 .min_acceptable_spare_size
= min_acceptable_spare_size_imsm
,
10530 .compare_super
= compare_super_imsm
,
10532 .load_super
= load_super_imsm
,
10533 .init_super
= init_super_imsm
,
10534 .store_super
= store_super_imsm
,
10535 .free_super
= free_super_imsm
,
10536 .match_metadata_desc
= match_metadata_desc_imsm
,
10537 .container_content
= container_content_imsm
,
10545 .open_new
= imsm_open_new
,
10546 .set_array_state
= imsm_set_array_state
,
10547 .set_disk
= imsm_set_disk
,
10548 .sync_metadata
= imsm_sync_metadata
,
10549 .activate_spare
= imsm_activate_spare
,
10550 .process_update
= imsm_process_update
,
10551 .prepare_update
= imsm_prepare_update
,
10552 #endif /* MDASSEMBLE */