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
44 #define MPB_ATTRIB_CHECKSUM_VERIFY __cpu_to_le32(0x80000000)
45 #define MPB_ATTRIB_PM __cpu_to_le32(0x40000000)
46 #define MPB_ATTRIB_2TB __cpu_to_le32(0x20000000)
47 #define MPB_ATTRIB_RAID0 __cpu_to_le32(0x00000001)
48 #define MPB_ATTRIB_RAID1 __cpu_to_le32(0x00000002)
49 #define MPB_ATTRIB_RAID10 __cpu_to_le32(0x00000004)
50 #define MPB_ATTRIB_RAID1E __cpu_to_le32(0x00000008)
51 #define MPB_ATTRIB_RAID5 __cpu_to_le32(0x00000010)
52 #define MPB_ATTRIB_RAIDCNG __cpu_to_le32(0x00000020)
54 #define MPB_SECTOR_CNT 2210
55 #define IMSM_RESERVED_SECTORS 4096
56 #define SECT_PER_MB_SHIFT 11
58 /* Disk configuration info. */
59 #define IMSM_MAX_DEVICES 255
61 __u8 serial
[MAX_RAID_SERIAL_LEN
];/* 0xD8 - 0xE7 ascii serial number */
62 __u32 total_blocks
; /* 0xE8 - 0xEB total blocks */
63 __u32 scsi_id
; /* 0xEC - 0xEF scsi ID */
64 #define SPARE_DISK __cpu_to_le32(0x01) /* Spare */
65 #define CONFIGURED_DISK __cpu_to_le32(0x02) /* Member of some RaidDev */
66 #define FAILED_DISK __cpu_to_le32(0x04) /* Permanent failure */
67 __u32 status
; /* 0xF0 - 0xF3 */
68 __u32 owner_cfg_num
; /* which config 0,1,2... owns this disk */
69 #define IMSM_DISK_FILLERS 4
70 __u32 filler
[IMSM_DISK_FILLERS
]; /* 0xF4 - 0x107 MPB_DISK_FILLERS for future expansion */
73 /* RAID map configuration infos. */
75 __u32 pba_of_lba0
; /* start address of partition */
76 __u32 blocks_per_member
;/* blocks per member */
77 __u32 num_data_stripes
; /* number of data stripes */
78 __u16 blocks_per_strip
;
79 __u8 map_state
; /* Normal, Uninitialized, Degraded, Failed */
80 #define IMSM_T_STATE_NORMAL 0
81 #define IMSM_T_STATE_UNINITIALIZED 1
82 #define IMSM_T_STATE_DEGRADED 2
83 #define IMSM_T_STATE_FAILED 3
85 #define IMSM_T_RAID0 0
86 #define IMSM_T_RAID1 1
87 #define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */
88 __u8 num_members
; /* number of member disks */
89 __u8 num_domains
; /* number of parity domains */
90 __u8 failed_disk_num
; /* valid only when state is degraded */
92 __u32 filler
[7]; /* expansion area */
93 #define IMSM_ORD_REBUILD (1 << 24)
94 __u32 disk_ord_tbl
[1]; /* disk_ord_tbl[num_members],
95 * top byte contains some flags
97 } __attribute__ ((packed
));
100 __u32 curr_migr_unit
;
101 __u32 checkpoint_id
; /* id to access curr_migr_unit */
102 __u8 migr_state
; /* Normal or Migrating */
104 #define MIGR_REBUILD 1
105 #define MIGR_VERIFY 2 /* analagous to echo check > sync_action */
106 #define MIGR_GEN_MIGR 3
107 #define MIGR_STATE_CHANGE 4
108 #define MIGR_REPAIR 5
109 __u8 migr_type
; /* Initializing, Rebuilding, ... */
111 __u8 fs_state
; /* fast-sync state for CnG (0xff == disabled) */
112 __u16 verify_errors
; /* number of mismatches */
113 __u16 bad_blocks
; /* number of bad blocks during verify */
115 struct imsm_map map
[1];
116 /* here comes another one if migr_state */
117 } __attribute__ ((packed
));
120 __u8 volume
[MAX_RAID_SERIAL_LEN
];
123 #define DEV_BOOTABLE __cpu_to_le32(0x01)
124 #define DEV_BOOT_DEVICE __cpu_to_le32(0x02)
125 #define DEV_READ_COALESCING __cpu_to_le32(0x04)
126 #define DEV_WRITE_COALESCING __cpu_to_le32(0x08)
127 #define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10)
128 #define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20)
129 #define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40)
130 #define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80)
131 #define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100)
132 #define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200)
133 #define DEV_CLONE_N_GO __cpu_to_le32(0x400)
134 #define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800)
135 #define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000)
136 __u32 status
; /* Persistent RaidDev status */
137 __u32 reserved_blocks
; /* Reserved blocks at beginning of volume */
141 __u8 cng_master_disk
;
145 #define IMSM_DEV_FILLERS 10
146 __u32 filler
[IMSM_DEV_FILLERS
];
148 } __attribute__ ((packed
));
151 __u8 sig
[MAX_SIGNATURE_LENGTH
]; /* 0x00 - 0x1F */
152 __u32 check_sum
; /* 0x20 - 0x23 MPB Checksum */
153 __u32 mpb_size
; /* 0x24 - 0x27 Size of MPB */
154 __u32 family_num
; /* 0x28 - 0x2B Checksum from first time this config was written */
155 __u32 generation_num
; /* 0x2C - 0x2F Incremented each time this array's MPB is written */
156 __u32 error_log_size
; /* 0x30 - 0x33 in bytes */
157 __u32 attributes
; /* 0x34 - 0x37 */
158 __u8 num_disks
; /* 0x38 Number of configured disks */
159 __u8 num_raid_devs
; /* 0x39 Number of configured volumes */
160 __u8 error_log_pos
; /* 0x3A */
161 __u8 fill
[1]; /* 0x3B */
162 __u32 cache_size
; /* 0x3c - 0x40 in mb */
163 __u32 orig_family_num
; /* 0x40 - 0x43 original family num */
164 __u32 pwr_cycle_count
; /* 0x44 - 0x47 simulated power cycle count for array */
165 __u32 bbm_log_size
; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */
166 #define IMSM_FILLERS 35
167 __u32 filler
[IMSM_FILLERS
]; /* 0x4C - 0xD7 RAID_MPB_FILLERS */
168 struct imsm_disk disk
[1]; /* 0xD8 diskTbl[numDisks] */
169 /* here comes imsm_dev[num_raid_devs] */
170 /* here comes BBM logs */
171 } __attribute__ ((packed
));
173 #define BBM_LOG_MAX_ENTRIES 254
175 struct bbm_log_entry
{
176 __u64 defective_block_start
;
177 #define UNREADABLE 0xFFFFFFFF
178 __u32 spare_block_offset
;
179 __u16 remapped_marked_count
;
181 } __attribute__ ((__packed__
));
184 __u32 signature
; /* 0xABADB10C */
186 __u32 reserved_spare_block_count
; /* 0 */
187 __u32 reserved
; /* 0xFFFF */
188 __u64 first_spare_lba
;
189 struct bbm_log_entry mapped_block_entries
[BBM_LOG_MAX_ENTRIES
];
190 } __attribute__ ((__packed__
));
194 static char *map_state_str
[] = { "normal", "uninitialized", "degraded", "failed" };
197 #define RAID_DISK_RESERVED_BLOCKS_IMSM_HI 2209
199 #define GEN_MIGR_AREA_SIZE 2048 /* General Migration Copy Area size in blocks */
201 #define UNIT_SRC_NORMAL 0 /* Source data for curr_migr_unit must
202 * be recovered using srcMap */
203 #define UNIT_SRC_IN_CP_AREA 1 /* Source data for curr_migr_unit has
204 * already been migrated and must
205 * be recovered from checkpoint area */
207 __u32 rec_status
; /* Status used to determine how to restart
208 * migration in case it aborts
210 __u32 curr_migr_unit
; /* 0..numMigrUnits-1 */
211 __u32 family_num
; /* Family number of MPB
212 * containing the RaidDev
213 * that is migrating */
214 __u32 ascending_migr
; /* True if migrating in increasing
216 __u32 blocks_per_unit
; /* Num disk blocks per unit of operation */
217 __u32 dest_depth_per_unit
; /* Num member blocks each destMap
219 * advances per unit-of-operation */
220 __u32 ckpt_area_pba
; /* Pba of first block of ckpt copy area */
221 __u32 dest_1st_member_lba
; /* First member lba on first
222 * stripe of destination */
223 __u32 num_migr_units
; /* Total num migration units-of-op */
224 __u32 post_migr_vol_cap
; /* Size of volume after
225 * migration completes */
226 __u32 post_migr_vol_cap_hi
; /* Expansion space for LBA64 */
227 __u32 ckpt_read_disk_num
; /* Which member disk in destSubMap[0] the
228 * migration ckpt record was read from
229 * (for recovered migrations) */
230 } __attribute__ ((__packed__
));
232 static __u8
migr_type(struct imsm_dev
*dev
)
234 if (dev
->vol
.migr_type
== MIGR_VERIFY
&&
235 dev
->status
& DEV_VERIFY_AND_FIX
)
238 return dev
->vol
.migr_type
;
241 static void set_migr_type(struct imsm_dev
*dev
, __u8 migr_type
)
243 /* for compatibility with older oroms convert MIGR_REPAIR, into
244 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
246 if (migr_type
== MIGR_REPAIR
) {
247 dev
->vol
.migr_type
= MIGR_VERIFY
;
248 dev
->status
|= DEV_VERIFY_AND_FIX
;
250 dev
->vol
.migr_type
= migr_type
;
251 dev
->status
&= ~DEV_VERIFY_AND_FIX
;
255 static unsigned int sector_count(__u32 bytes
)
257 return ((bytes
+ (512-1)) & (~(512-1))) / 512;
260 static unsigned int mpb_sectors(struct imsm_super
*mpb
)
262 return sector_count(__le32_to_cpu(mpb
->mpb_size
));
266 struct imsm_dev
*dev
;
267 struct intel_dev
*next
;
272 enum sys_dev_type type
;
275 struct intel_hba
*next
;
282 /* internal representation of IMSM metadata */
285 void *buf
; /* O_DIRECT buffer for reading/writing metadata */
286 struct imsm_super
*anchor
; /* immovable parameters */
289 void *migr_rec_buf
; /* buffer for I/O operations */
290 struct migr_record
*migr_rec
; /* migration record */
292 size_t len
; /* size of the 'buf' allocation */
293 void *next_buf
; /* for realloc'ing buf from the manager */
295 int updates_pending
; /* count of pending updates for mdmon */
296 int current_vol
; /* index of raid device undergoing creation */
297 __u32 create_offset
; /* common start for 'current_vol' */
298 __u32 random
; /* random data for seeding new family numbers */
299 struct intel_dev
*devlist
;
303 __u8 serial
[MAX_RAID_SERIAL_LEN
];
306 struct imsm_disk disk
;
309 struct extent
*e
; /* for determining freespace @ create */
310 int raiddisk
; /* slot to fill in autolayout */
313 struct dl
*disk_mgmt_list
; /* list of disks to add/remove while mdmon
315 struct dl
*missing
; /* disks removed while we weren't looking */
316 struct bbm_log
*bbm_log
;
317 struct intel_hba
*hba
; /* device path of the raid controller for this metadata */
318 const struct imsm_orom
*orom
; /* platform firmware support */
319 struct intel_super
*next
; /* (temp) list for disambiguating family_num */
323 struct imsm_disk disk
;
324 #define IMSM_UNKNOWN_OWNER (-1)
326 struct intel_disk
*next
;
330 unsigned long long start
, size
;
333 /* definitions of reshape process types */
334 enum imsm_reshape_type
{
339 /* definition of messages passed to imsm_process_update */
340 enum imsm_update_type
{
341 update_activate_spare
,
345 update_add_remove_disk
,
346 update_reshape_container_disks
,
347 update_reshape_migration
,
349 update_general_migration_checkpoint
,
352 struct imsm_update_activate_spare
{
353 enum imsm_update_type type
;
357 struct imsm_update_activate_spare
*next
;
370 enum takeover_direction
{
374 struct imsm_update_takeover
{
375 enum imsm_update_type type
;
377 enum takeover_direction direction
;
380 struct imsm_update_reshape
{
381 enum imsm_update_type type
;
385 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
388 struct imsm_update_reshape_migration
{
389 enum imsm_update_type type
;
392 /* fields for array migration changes
399 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
402 struct imsm_update_general_migration_checkpoint
{
403 enum imsm_update_type type
;
404 __u32 curr_migr_unit
;
408 __u8 serial
[MAX_RAID_SERIAL_LEN
];
411 struct imsm_update_create_array
{
412 enum imsm_update_type type
;
417 struct imsm_update_kill_array
{
418 enum imsm_update_type type
;
422 struct imsm_update_rename_array
{
423 enum imsm_update_type type
;
424 __u8 name
[MAX_RAID_SERIAL_LEN
];
428 struct imsm_update_add_remove_disk
{
429 enum imsm_update_type type
;
433 static const char *_sys_dev_type
[] = {
434 [SYS_DEV_UNKNOWN
] = "Unknown",
435 [SYS_DEV_SAS
] = "SAS",
436 [SYS_DEV_SATA
] = "SATA"
439 const char *get_sys_dev_type(enum sys_dev_type type
)
441 if (type
>= SYS_DEV_MAX
)
442 type
= SYS_DEV_UNKNOWN
;
444 return _sys_dev_type
[type
];
447 static struct intel_hba
* alloc_intel_hba(struct sys_dev
*device
)
449 struct intel_hba
*result
= malloc(sizeof(*result
));
451 result
->type
= device
->type
;
452 result
->path
= strdup(device
->path
);
454 if (result
->path
&& (result
->pci_id
= strrchr(result
->path
, '/')) != NULL
)
460 static struct intel_hba
* find_intel_hba(struct intel_hba
*hba
, struct sys_dev
*device
)
462 struct intel_hba
*result
=NULL
;
463 for (result
= hba
; result
; result
= result
->next
) {
464 if (result
->type
== device
->type
&& strcmp(result
->path
, device
->path
) == 0)
470 static int attach_hba_to_super(struct intel_super
*super
, struct sys_dev
*device
)
472 struct intel_hba
*hba
;
474 /* check if disk attached to Intel HBA */
475 hba
= find_intel_hba(super
->hba
, device
);
478 /* Check if HBA is already attached to super */
479 if (super
->hba
== NULL
) {
480 super
->hba
= alloc_intel_hba(device
);
485 /* Intel metadata allows for all disks attached to the same type HBA.
486 * Do not sypport odf HBA types mixing
488 if (device
->type
!= hba
->type
)
494 hba
->next
= alloc_intel_hba(device
);
498 static struct sys_dev
* find_disk_attached_hba(int fd
, const char *devname
)
500 struct sys_dev
*list
, *elem
, *prev
;
503 if ((list
= find_intel_devices()) == NULL
)
507 disk_path
= (char *) devname
;
509 disk_path
= diskfd_to_devpath(fd
);
516 for (prev
= NULL
, elem
= list
; elem
; prev
= elem
, elem
= elem
->next
) {
517 if (path_attached_to_hba(disk_path
, elem
->path
)) {
521 prev
->next
= elem
->next
;
523 if (disk_path
!= devname
)
529 if (disk_path
!= devname
)
537 static int find_intel_hba_capability(int fd
, struct intel_super
*super
,
540 static struct supertype
*match_metadata_desc_imsm(char *arg
)
542 struct supertype
*st
;
544 if (strcmp(arg
, "imsm") != 0 &&
545 strcmp(arg
, "default") != 0
549 st
= malloc(sizeof(*st
));
552 memset(st
, 0, sizeof(*st
));
553 st
->container_dev
= NoMdDev
;
554 st
->ss
= &super_imsm
;
555 st
->max_devs
= IMSM_MAX_DEVICES
;
556 st
->minor_version
= 0;
562 static __u8
*get_imsm_version(struct imsm_super
*mpb
)
564 return &mpb
->sig
[MPB_SIG_LEN
];
568 /* retrieve a disk directly from the anchor when the anchor is known to be
569 * up-to-date, currently only at load time
571 static struct imsm_disk
*__get_imsm_disk(struct imsm_super
*mpb
, __u8 index
)
573 if (index
>= mpb
->num_disks
)
575 return &mpb
->disk
[index
];
578 /* retrieve the disk description based on a index of the disk
581 static struct dl
*get_imsm_dl_disk(struct intel_super
*super
, __u8 index
)
585 for (d
= super
->disks
; d
; d
= d
->next
)
586 if (d
->index
== index
)
591 /* retrieve a disk from the parsed metadata */
592 static struct imsm_disk
*get_imsm_disk(struct intel_super
*super
, __u8 index
)
596 dl
= get_imsm_dl_disk(super
, index
);
603 /* generate a checksum directly from the anchor when the anchor is known to be
604 * up-to-date, currently only at load or write_super after coalescing
606 static __u32
__gen_imsm_checksum(struct imsm_super
*mpb
)
608 __u32 end
= mpb
->mpb_size
/ sizeof(end
);
609 __u32
*p
= (__u32
*) mpb
;
613 sum
+= __le32_to_cpu(*p
);
617 return sum
- __le32_to_cpu(mpb
->check_sum
);
620 static size_t sizeof_imsm_map(struct imsm_map
*map
)
622 return sizeof(struct imsm_map
) + sizeof(__u32
) * (map
->num_members
- 1);
625 struct imsm_map
*get_imsm_map(struct imsm_dev
*dev
, int second_map
)
627 /* A device can have 2 maps if it is in the middle of a migration.
629 * 0 - we return the first map
630 * 1 - we return the second map if it exists, else NULL
631 * -1 - we return the second map if it exists, else the first
633 struct imsm_map
*map
= &dev
->vol
.map
[0];
635 if (second_map
== 1 && !dev
->vol
.migr_state
)
637 else if (second_map
== 1 ||
638 (second_map
< 0 && dev
->vol
.migr_state
)) {
641 return ptr
+ sizeof_imsm_map(map
);
647 /* return the size of the device.
648 * migr_state increases the returned size if map[0] were to be duplicated
650 static size_t sizeof_imsm_dev(struct imsm_dev
*dev
, int migr_state
)
652 size_t size
= sizeof(*dev
) - sizeof(struct imsm_map
) +
653 sizeof_imsm_map(get_imsm_map(dev
, 0));
655 /* migrating means an additional map */
656 if (dev
->vol
.migr_state
)
657 size
+= sizeof_imsm_map(get_imsm_map(dev
, 1));
659 size
+= sizeof_imsm_map(get_imsm_map(dev
, 0));
665 /* retrieve disk serial number list from a metadata update */
666 static struct disk_info
*get_disk_info(struct imsm_update_create_array
*update
)
669 struct disk_info
*inf
;
671 inf
= u
+ sizeof(*update
) - sizeof(struct imsm_dev
) +
672 sizeof_imsm_dev(&update
->dev
, 0);
678 static struct imsm_dev
*__get_imsm_dev(struct imsm_super
*mpb
, __u8 index
)
684 if (index
>= mpb
->num_raid_devs
)
687 /* devices start after all disks */
688 offset
= ((void *) &mpb
->disk
[mpb
->num_disks
]) - _mpb
;
690 for (i
= 0; i
<= index
; i
++)
692 return _mpb
+ offset
;
694 offset
+= sizeof_imsm_dev(_mpb
+ offset
, 0);
699 static struct imsm_dev
*get_imsm_dev(struct intel_super
*super
, __u8 index
)
701 struct intel_dev
*dv
;
703 if (index
>= super
->anchor
->num_raid_devs
)
705 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
706 if (dv
->index
== index
)
714 * == 1 get second map
715 * == -1 than get map according to the current migr_state
717 static __u32
get_imsm_ord_tbl_ent(struct imsm_dev
*dev
,
721 struct imsm_map
*map
;
723 map
= get_imsm_map(dev
, second_map
);
725 /* top byte identifies disk under rebuild */
726 return __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
729 #define ord_to_idx(ord) (((ord) << 8) >> 8)
730 static __u32
get_imsm_disk_idx(struct imsm_dev
*dev
, int slot
, int second_map
)
732 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, second_map
);
734 return ord_to_idx(ord
);
737 static void set_imsm_ord_tbl_ent(struct imsm_map
*map
, int slot
, __u32 ord
)
739 map
->disk_ord_tbl
[slot
] = __cpu_to_le32(ord
);
742 static int get_imsm_disk_slot(struct imsm_map
*map
, unsigned idx
)
747 for (slot
= 0; slot
< map
->num_members
; slot
++) {
748 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
749 if (ord_to_idx(ord
) == idx
)
756 static int get_imsm_raid_level(struct imsm_map
*map
)
758 if (map
->raid_level
== 1) {
759 if (map
->num_members
== 2)
765 return map
->raid_level
;
768 static int cmp_extent(const void *av
, const void *bv
)
770 const struct extent
*a
= av
;
771 const struct extent
*b
= bv
;
772 if (a
->start
< b
->start
)
774 if (a
->start
> b
->start
)
779 static int count_memberships(struct dl
*dl
, struct intel_super
*super
)
784 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
785 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
786 struct imsm_map
*map
= get_imsm_map(dev
, 0);
788 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
795 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
)
797 /* find a list of used extents on the given physical device */
798 struct extent
*rv
, *e
;
800 int memberships
= count_memberships(dl
, super
);
801 __u32 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
803 rv
= malloc(sizeof(struct extent
) * (memberships
+ 1));
808 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
809 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
810 struct imsm_map
*map
= get_imsm_map(dev
, 0);
812 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
813 e
->start
= __le32_to_cpu(map
->pba_of_lba0
);
814 e
->size
= __le32_to_cpu(map
->blocks_per_member
);
818 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
820 /* determine the start of the metadata
821 * when no raid devices are defined use the default
822 * ...otherwise allow the metadata to truncate the value
823 * as is the case with older versions of imsm
826 struct extent
*last
= &rv
[memberships
- 1];
829 remainder
= __le32_to_cpu(dl
->disk
.total_blocks
) -
830 (last
->start
+ last
->size
);
831 /* round down to 1k block to satisfy precision of the kernel
835 /* make sure remainder is still sane */
836 if (remainder
< (unsigned)ROUND_UP(super
->len
, 512) >> 9)
837 remainder
= ROUND_UP(super
->len
, 512) >> 9;
838 if (reservation
> remainder
)
839 reservation
= remainder
;
841 e
->start
= __le32_to_cpu(dl
->disk
.total_blocks
) - reservation
;
846 /* try to determine how much space is reserved for metadata from
847 * the last get_extents() entry, otherwise fallback to the
850 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
856 /* for spares just return a minimal reservation which will grow
857 * once the spare is picked up by an array
860 return MPB_SECTOR_CNT
;
862 e
= get_extents(super
, dl
);
864 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
866 /* scroll to last entry */
867 for (i
= 0; e
[i
].size
; i
++)
870 rv
= __le32_to_cpu(dl
->disk
.total_blocks
) - e
[i
].start
;
877 static int is_spare(struct imsm_disk
*disk
)
879 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
882 static int is_configured(struct imsm_disk
*disk
)
884 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
887 static int is_failed(struct imsm_disk
*disk
)
889 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
892 /* Return minimum size of a spare that can be used in this array*/
893 static unsigned long long min_acceptable_spare_size_imsm(struct supertype
*st
)
895 struct intel_super
*super
= st
->sb
;
899 unsigned long long rv
= 0;
903 /* find first active disk in array */
905 while (dl
&& (is_failed(&dl
->disk
) || dl
->index
== -1))
909 /* find last lba used by subarrays */
910 e
= get_extents(super
, dl
);
913 for (i
= 0; e
[i
].size
; i
++)
916 rv
= e
[i
-1].start
+ e
[i
-1].size
;
918 /* add the amount of space needed for metadata */
919 rv
= rv
+ MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
924 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
925 struct imsm_dev
*dev
);
927 static void print_imsm_dev(struct intel_super
*super
,
928 struct imsm_dev
*dev
,
934 struct imsm_map
*map
= get_imsm_map(dev
, 0);
935 struct imsm_map
*map2
= get_imsm_map(dev
, 1);
939 printf("[%.16s]:\n", dev
->volume
);
940 printf(" UUID : %s\n", uuid
);
941 printf(" RAID Level : %d", get_imsm_raid_level(map
));
943 printf(" <-- %d", get_imsm_raid_level(map2
));
945 printf(" Members : %d", map
->num_members
);
947 printf(" <-- %d", map2
->num_members
);
949 printf(" Slots : [");
950 for (i
= 0; i
< map
->num_members
; i
++) {
951 ord
= get_imsm_ord_tbl_ent(dev
, i
, 0);
952 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
957 for (i
= 0; i
< map2
->num_members
; i
++) {
958 ord
= get_imsm_ord_tbl_ent(dev
, i
, 1);
959 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
964 printf(" Failed disk : ");
965 if (map
->failed_disk_num
== 0xff)
968 printf("%i", map
->failed_disk_num
);
970 slot
= get_imsm_disk_slot(map
, disk_idx
);
972 ord
= get_imsm_ord_tbl_ent(dev
, slot
, -1);
973 printf(" This Slot : %d%s\n", slot
,
974 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
976 printf(" This Slot : ?\n");
977 sz
= __le32_to_cpu(dev
->size_high
);
979 sz
+= __le32_to_cpu(dev
->size_low
);
980 printf(" Array Size : %llu%s\n", (unsigned long long)sz
,
981 human_size(sz
* 512));
982 sz
= __le32_to_cpu(map
->blocks_per_member
);
983 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz
,
984 human_size(sz
* 512));
985 printf(" Sector Offset : %u\n",
986 __le32_to_cpu(map
->pba_of_lba0
));
987 printf(" Num Stripes : %u\n",
988 __le32_to_cpu(map
->num_data_stripes
));
989 printf(" Chunk Size : %u KiB",
990 __le16_to_cpu(map
->blocks_per_strip
) / 2);
992 printf(" <-- %u KiB",
993 __le16_to_cpu(map2
->blocks_per_strip
) / 2);
995 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
996 printf(" Migrate State : ");
997 if (dev
->vol
.migr_state
) {
998 if (migr_type(dev
) == MIGR_INIT
)
999 printf("initialize\n");
1000 else if (migr_type(dev
) == MIGR_REBUILD
)
1001 printf("rebuild\n");
1002 else if (migr_type(dev
) == MIGR_VERIFY
)
1004 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
1005 printf("general migration\n");
1006 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
1007 printf("state change\n");
1008 else if (migr_type(dev
) == MIGR_REPAIR
)
1011 printf("<unknown:%d>\n", migr_type(dev
));
1014 printf(" Map State : %s", map_state_str
[map
->map_state
]);
1015 if (dev
->vol
.migr_state
) {
1016 struct imsm_map
*map
= get_imsm_map(dev
, 1);
1018 printf(" <-- %s", map_state_str
[map
->map_state
]);
1019 printf("\n Checkpoint : %u (%llu)",
1020 __le32_to_cpu(dev
->vol
.curr_migr_unit
),
1021 (unsigned long long)blocks_per_migr_unit(super
, dev
));
1024 printf(" Dirty State : %s\n", dev
->vol
.dirty
? "dirty" : "clean");
1027 static void print_imsm_disk(struct imsm_super
*mpb
, int index
, __u32 reserved
)
1029 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, index
);
1030 char str
[MAX_RAID_SERIAL_LEN
+ 1];
1033 if (index
< 0 || !disk
)
1037 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
1038 printf(" Disk%02d Serial : %s\n", index
, str
);
1039 printf(" State :%s%s%s\n", is_spare(disk
) ? " spare" : "",
1040 is_configured(disk
) ? " active" : "",
1041 is_failed(disk
) ? " failed" : "");
1042 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
1043 sz
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
1044 printf(" Usable Size : %llu%s\n", (unsigned long long)sz
,
1045 human_size(sz
* 512));
1048 static int is_gen_migration(struct imsm_dev
*dev
);
1050 void examine_migr_rec_imsm(struct intel_super
*super
)
1052 struct migr_record
*migr_rec
= super
->migr_rec
;
1053 struct imsm_super
*mpb
= super
->anchor
;
1056 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1057 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1058 if (is_gen_migration(dev
) == 0)
1061 printf("\nMigration Record Information:");
1062 if (super
->disks
->index
> 1) {
1063 printf(" Empty\n ");
1064 printf("Examine one of first two disks in array\n");
1067 printf("\n Status : ");
1068 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
1071 printf("Contains Data\n");
1072 printf(" Current Unit : %u\n",
1073 __le32_to_cpu(migr_rec
->curr_migr_unit
));
1074 printf(" Family : %u\n",
1075 __le32_to_cpu(migr_rec
->family_num
));
1076 printf(" Ascending : %u\n",
1077 __le32_to_cpu(migr_rec
->ascending_migr
));
1078 printf(" Blocks Per Unit : %u\n",
1079 __le32_to_cpu(migr_rec
->blocks_per_unit
));
1080 printf(" Dest. Depth Per Unit : %u\n",
1081 __le32_to_cpu(migr_rec
->dest_depth_per_unit
));
1082 printf(" Checkpoint Area pba : %u\n",
1083 __le32_to_cpu(migr_rec
->ckpt_area_pba
));
1084 printf(" First member lba : %u\n",
1085 __le32_to_cpu(migr_rec
->dest_1st_member_lba
));
1086 printf(" Total Number of Units : %u\n",
1087 __le32_to_cpu(migr_rec
->num_migr_units
));
1088 printf(" Size of volume : %u\n",
1089 __le32_to_cpu(migr_rec
->post_migr_vol_cap
));
1090 printf(" Expansion space for LBA64 : %u\n",
1091 __le32_to_cpu(migr_rec
->post_migr_vol_cap_hi
));
1092 printf(" Record was read from : %u\n",
1093 __le32_to_cpu(migr_rec
->ckpt_read_disk_num
));
1099 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
1101 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
1103 struct intel_super
*super
= st
->sb
;
1104 struct imsm_super
*mpb
= super
->anchor
;
1105 char str
[MAX_SIGNATURE_LENGTH
];
1110 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
1113 snprintf(str
, MPB_SIG_LEN
, "%s", mpb
->sig
);
1114 printf(" Magic : %s\n", str
);
1115 snprintf(str
, strlen(MPB_VERSION_RAID0
), "%s", get_imsm_version(mpb
));
1116 printf(" Version : %s\n", get_imsm_version(mpb
));
1117 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
1118 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
1119 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
1120 getinfo_super_imsm(st
, &info
, NULL
);
1121 fname_from_uuid(st
, &info
, nbuf
, ':');
1122 printf(" UUID : %s\n", nbuf
+ 5);
1123 sum
= __le32_to_cpu(mpb
->check_sum
);
1124 printf(" Checksum : %08x %s\n", sum
,
1125 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
1126 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
));
1127 printf(" Disks : %d\n", mpb
->num_disks
);
1128 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
1129 print_imsm_disk(mpb
, super
->disks
->index
, reserved
);
1130 if (super
->bbm_log
) {
1131 struct bbm_log
*log
= super
->bbm_log
;
1134 printf("Bad Block Management Log:\n");
1135 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
1136 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
1137 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
1138 printf(" Spare Blocks : %d\n", __le32_to_cpu(log
->reserved_spare_block_count
));
1139 printf(" First Spare : %llx\n",
1140 (unsigned long long) __le64_to_cpu(log
->first_spare_lba
));
1142 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1144 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1146 super
->current_vol
= i
;
1147 getinfo_super_imsm(st
, &info
, NULL
);
1148 fname_from_uuid(st
, &info
, nbuf
, ':');
1149 print_imsm_dev(super
, dev
, nbuf
+ 5, super
->disks
->index
);
1151 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1152 if (i
== super
->disks
->index
)
1154 print_imsm_disk(mpb
, i
, reserved
);
1156 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
1157 struct imsm_disk
*disk
;
1158 char str
[MAX_RAID_SERIAL_LEN
+ 1];
1166 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
1167 printf(" Disk Serial : %s\n", str
);
1168 printf(" State :%s%s%s\n", is_spare(disk
) ? " spare" : "",
1169 is_configured(disk
) ? " active" : "",
1170 is_failed(disk
) ? " failed" : "");
1171 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
1172 sz
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
1173 printf(" Usable Size : %llu%s\n", (unsigned long long)sz
,
1174 human_size(sz
* 512));
1177 examine_migr_rec_imsm(super
);
1180 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
1182 /* We just write a generic IMSM ARRAY entry */
1185 struct intel_super
*super
= st
->sb
;
1187 if (!super
->anchor
->num_raid_devs
) {
1188 printf("ARRAY metadata=imsm\n");
1192 getinfo_super_imsm(st
, &info
, NULL
);
1193 fname_from_uuid(st
, &info
, nbuf
, ':');
1194 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
1197 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
1199 /* We just write a generic IMSM ARRAY entry */
1203 struct intel_super
*super
= st
->sb
;
1206 if (!super
->anchor
->num_raid_devs
)
1209 getinfo_super_imsm(st
, &info
, NULL
);
1210 fname_from_uuid(st
, &info
, nbuf
, ':');
1211 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1212 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1214 super
->current_vol
= i
;
1215 getinfo_super_imsm(st
, &info
, NULL
);
1216 fname_from_uuid(st
, &info
, nbuf1
, ':');
1217 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
1218 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
1222 static void export_examine_super_imsm(struct supertype
*st
)
1224 struct intel_super
*super
= st
->sb
;
1225 struct imsm_super
*mpb
= super
->anchor
;
1229 getinfo_super_imsm(st
, &info
, NULL
);
1230 fname_from_uuid(st
, &info
, nbuf
, ':');
1231 printf("MD_METADATA=imsm\n");
1232 printf("MD_LEVEL=container\n");
1233 printf("MD_UUID=%s\n", nbuf
+5);
1234 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
1237 static void detail_super_imsm(struct supertype
*st
, char *homehost
)
1242 getinfo_super_imsm(st
, &info
, NULL
);
1243 fname_from_uuid(st
, &info
, nbuf
, ':');
1244 printf("\n UUID : %s\n", nbuf
+ 5);
1247 static void brief_detail_super_imsm(struct supertype
*st
)
1251 getinfo_super_imsm(st
, &info
, NULL
);
1252 fname_from_uuid(st
, &info
, nbuf
, ':');
1253 printf(" UUID=%s", nbuf
+ 5);
1256 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
);
1257 static void fd2devname(int fd
, char *name
);
1259 static int ahci_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
1261 /* dump an unsorted list of devices attached to AHCI Intel storage
1262 * controller, as well as non-connected ports
1264 int hba_len
= strlen(hba_path
) + 1;
1269 unsigned long port_mask
= (1 << port_count
) - 1;
1271 if (port_count
> (int)sizeof(port_mask
) * 8) {
1273 fprintf(stderr
, Name
": port_count %d out of range\n", port_count
);
1277 /* scroll through /sys/dev/block looking for devices attached to
1280 dir
= opendir("/sys/dev/block");
1281 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
1292 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
1294 path
= devt_to_devpath(makedev(major
, minor
));
1297 if (!path_attached_to_hba(path
, hba_path
)) {
1303 /* retrieve the scsi device type */
1304 if (asprintf(&device
, "/sys/dev/block/%d:%d/device/xxxxxxx", major
, minor
) < 0) {
1306 fprintf(stderr
, Name
": failed to allocate 'device'\n");
1310 sprintf(device
, "/sys/dev/block/%d:%d/device/type", major
, minor
);
1311 if (load_sys(device
, buf
) != 0) {
1313 fprintf(stderr
, Name
": failed to read device type for %s\n",
1319 type
= strtoul(buf
, NULL
, 10);
1321 /* if it's not a disk print the vendor and model */
1322 if (!(type
== 0 || type
== 7 || type
== 14)) {
1325 sprintf(device
, "/sys/dev/block/%d:%d/device/vendor", major
, minor
);
1326 if (load_sys(device
, buf
) == 0) {
1327 strncpy(vendor
, buf
, sizeof(vendor
));
1328 vendor
[sizeof(vendor
) - 1] = '\0';
1329 c
= (char *) &vendor
[sizeof(vendor
) - 1];
1330 while (isspace(*c
) || *c
== '\0')
1334 sprintf(device
, "/sys/dev/block/%d:%d/device/model", major
, minor
);
1335 if (load_sys(device
, buf
) == 0) {
1336 strncpy(model
, buf
, sizeof(model
));
1337 model
[sizeof(model
) - 1] = '\0';
1338 c
= (char *) &model
[sizeof(model
) - 1];
1339 while (isspace(*c
) || *c
== '\0')
1343 if (vendor
[0] && model
[0])
1344 sprintf(buf
, "%.64s %.64s", vendor
, model
);
1346 switch (type
) { /* numbers from hald/linux/device.c */
1347 case 1: sprintf(buf
, "tape"); break;
1348 case 2: sprintf(buf
, "printer"); break;
1349 case 3: sprintf(buf
, "processor"); break;
1351 case 5: sprintf(buf
, "cdrom"); break;
1352 case 6: sprintf(buf
, "scanner"); break;
1353 case 8: sprintf(buf
, "media_changer"); break;
1354 case 9: sprintf(buf
, "comm"); break;
1355 case 12: sprintf(buf
, "raid"); break;
1356 default: sprintf(buf
, "unknown");
1362 /* chop device path to 'host%d' and calculate the port number */
1363 c
= strchr(&path
[hba_len
], '/');
1366 fprintf(stderr
, Name
": %s - invalid path name\n", path
+ hba_len
);
1371 if (sscanf(&path
[hba_len
], "host%d", &port
) == 1)
1375 *c
= '/'; /* repair the full string */
1376 fprintf(stderr
, Name
": failed to determine port number for %s\n",
1383 /* mark this port as used */
1384 port_mask
&= ~(1 << port
);
1386 /* print out the device information */
1388 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
1392 fd
= dev_open(ent
->d_name
, O_RDONLY
);
1394 printf(" Port%d : - disk info unavailable -\n", port
);
1396 fd2devname(fd
, buf
);
1397 printf(" Port%d : %s", port
, buf
);
1398 if (imsm_read_serial(fd
, NULL
, (__u8
*) buf
) == 0)
1399 printf(" (%s)\n", buf
);
1414 for (i
= 0; i
< port_count
; i
++)
1415 if (port_mask
& (1 << i
))
1416 printf(" Port%d : - no device attached -\n", i
);
1424 static void print_found_intel_controllers(struct sys_dev
*elem
)
1426 for (; elem
; elem
= elem
->next
) {
1427 fprintf(stderr
, Name
": found Intel(R) ");
1428 if (elem
->type
== SYS_DEV_SATA
)
1429 fprintf(stderr
, "SATA ");
1430 else if (elem
->type
== SYS_DEV_SAS
)
1431 fprintf(stderr
, "SAS ");
1432 fprintf(stderr
, "RAID controller");
1434 fprintf(stderr
, " at %s", elem
->pci_id
);
1435 fprintf(stderr
, ".\n");
1440 static int ahci_get_port_count(const char *hba_path
, int *port_count
)
1447 if ((dir
= opendir(hba_path
)) == NULL
)
1450 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
1453 if (sscanf(ent
->d_name
, "host%d", &host
) != 1)
1455 if (*port_count
== 0)
1457 else if (host
< host_base
)
1460 if (host
+ 1 > *port_count
+ host_base
)
1461 *port_count
= host
+ 1 - host_base
;
1467 static void print_imsm_capability(const struct imsm_orom
*orom
)
1469 printf(" Platform : Intel(R) Matrix Storage Manager\n");
1470 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
1471 orom
->hotfix_ver
, orom
->build
);
1472 printf(" RAID Levels :%s%s%s%s%s\n",
1473 imsm_orom_has_raid0(orom
) ? " raid0" : "",
1474 imsm_orom_has_raid1(orom
) ? " raid1" : "",
1475 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
1476 imsm_orom_has_raid10(orom
) ? " raid10" : "",
1477 imsm_orom_has_raid5(orom
) ? " raid5" : "");
1478 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1479 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
1480 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
1481 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
1482 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
1483 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
1484 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
1485 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
1486 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
1487 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
1488 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
1489 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
1490 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
1491 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
1492 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
1493 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
1494 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
1495 printf(" Max Disks : %d\n", orom
->tds
);
1496 printf(" Max Volumes : %d\n", orom
->vpa
);
1500 static int detail_platform_imsm(int verbose
, int enumerate_only
)
1502 /* There are two components to imsm platform support, the ahci SATA
1503 * controller and the option-rom. To find the SATA controller we
1504 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
1505 * controller with the Intel vendor id is present. This approach
1506 * allows mdadm to leverage the kernel's ahci detection logic, with the
1507 * caveat that if ahci.ko is not loaded mdadm will not be able to
1508 * detect platform raid capabilities. The option-rom resides in a
1509 * platform "Adapter ROM". We scan for its signature to retrieve the
1510 * platform capabilities. If raid support is disabled in the BIOS the
1511 * option-rom capability structure will not be available.
1513 const struct imsm_orom
*orom
;
1514 struct sys_dev
*list
, *hba
;
1519 if (enumerate_only
) {
1520 if (check_env("IMSM_NO_PLATFORM"))
1522 list
= find_intel_devices();
1525 for (hba
= list
; hba
; hba
= hba
->next
) {
1526 orom
= find_imsm_capability(hba
->type
);
1532 free_sys_dev(&list
);
1536 list
= find_intel_devices();
1539 fprintf(stderr
, Name
": no active Intel(R) RAID "
1540 "controller found.\n");
1541 free_sys_dev(&list
);
1544 print_found_intel_controllers(list
);
1546 for (hba
= list
; hba
; hba
= hba
->next
) {
1547 orom
= find_imsm_capability(hba
->type
);
1549 fprintf(stderr
, Name
": imsm capabilities not found for controller: %s (type %s)\n",
1550 hba
->path
, get_sys_dev_type(hba
->type
));
1552 print_imsm_capability(orom
);
1555 for (hba
= list
; hba
; hba
= hba
->next
) {
1556 printf(" I/O Controller : %s (%s)\n",
1557 hba
->path
, get_sys_dev_type(hba
->type
));
1559 if (hba
->type
== SYS_DEV_SATA
) {
1560 host_base
= ahci_get_port_count(hba
->path
, &port_count
);
1561 if (ahci_enumerate_ports(hba
->path
, port_count
, host_base
, verbose
)) {
1563 fprintf(stderr
, Name
": failed to enumerate "
1564 "ports on SATA controller at %s.", hba
->pci_id
);
1570 free_sys_dev(&list
);
1575 static int match_home_imsm(struct supertype
*st
, char *homehost
)
1577 /* the imsm metadata format does not specify any host
1578 * identification information. We return -1 since we can never
1579 * confirm nor deny whether a given array is "meant" for this
1580 * host. We rely on compare_super and the 'family_num' fields to
1581 * exclude member disks that do not belong, and we rely on
1582 * mdadm.conf to specify the arrays that should be assembled.
1583 * Auto-assembly may still pick up "foreign" arrays.
1589 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
1591 /* The uuid returned here is used for:
1592 * uuid to put into bitmap file (Create, Grow)
1593 * uuid for backup header when saving critical section (Grow)
1594 * comparing uuids when re-adding a device into an array
1595 * In these cases the uuid required is that of the data-array,
1596 * not the device-set.
1597 * uuid to recognise same set when adding a missing device back
1598 * to an array. This is a uuid for the device-set.
1600 * For each of these we can make do with a truncated
1601 * or hashed uuid rather than the original, as long as
1603 * In each case the uuid required is that of the data-array,
1604 * not the device-set.
1606 /* imsm does not track uuid's so we synthesis one using sha1 on
1607 * - The signature (Which is constant for all imsm array, but no matter)
1608 * - the orig_family_num of the container
1609 * - the index number of the volume
1610 * - the 'serial' number of the volume.
1611 * Hopefully these are all constant.
1613 struct intel_super
*super
= st
->sb
;
1616 struct sha1_ctx ctx
;
1617 struct imsm_dev
*dev
= NULL
;
1620 /* some mdadm versions failed to set ->orig_family_num, in which
1621 * case fall back to ->family_num. orig_family_num will be
1622 * fixed up with the first metadata update.
1624 family_num
= super
->anchor
->orig_family_num
;
1625 if (family_num
== 0)
1626 family_num
= super
->anchor
->family_num
;
1627 sha1_init_ctx(&ctx
);
1628 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
1629 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
1630 if (super
->current_vol
>= 0)
1631 dev
= get_imsm_dev(super
, super
->current_vol
);
1633 __u32 vol
= super
->current_vol
;
1634 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
1635 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
1637 sha1_finish_ctx(&ctx
, buf
);
1638 memcpy(uuid
, buf
, 4*4);
1643 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
1645 __u8
*v
= get_imsm_version(mpb
);
1646 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
1647 char major
[] = { 0, 0, 0 };
1648 char minor
[] = { 0 ,0, 0 };
1649 char patch
[] = { 0, 0, 0 };
1650 char *ver_parse
[] = { major
, minor
, patch
};
1654 while (*v
!= '\0' && v
< end
) {
1655 if (*v
!= '.' && j
< 2)
1656 ver_parse
[i
][j
++] = *v
;
1664 *m
= strtol(minor
, NULL
, 0);
1665 *p
= strtol(patch
, NULL
, 0);
1669 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
1671 /* migr_strip_size when repairing or initializing parity */
1672 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1673 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1675 switch (get_imsm_raid_level(map
)) {
1680 return 128*1024 >> 9;
1684 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
1686 /* migr_strip_size when rebuilding a degraded disk, no idea why
1687 * this is different than migr_strip_size_resync(), but it's good
1690 struct imsm_map
*map
= get_imsm_map(dev
, 1);
1691 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1693 switch (get_imsm_raid_level(map
)) {
1696 if (map
->num_members
% map
->num_domains
== 0)
1697 return 128*1024 >> 9;
1701 return max((__u32
) 64*1024 >> 9, chunk
);
1703 return 128*1024 >> 9;
1707 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
1709 struct imsm_map
*lo
= get_imsm_map(dev
, 0);
1710 struct imsm_map
*hi
= get_imsm_map(dev
, 1);
1711 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
1712 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
1714 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
1717 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
1719 struct imsm_map
*lo
= get_imsm_map(dev
, 0);
1720 int level
= get_imsm_raid_level(lo
);
1722 if (level
== 1 || level
== 10) {
1723 struct imsm_map
*hi
= get_imsm_map(dev
, 1);
1725 return hi
->num_domains
;
1727 return num_stripes_per_unit_resync(dev
);
1730 static __u8
imsm_num_data_members(struct imsm_dev
*dev
, int second_map
)
1732 /* named 'imsm_' because raid0, raid1 and raid10
1733 * counter-intuitively have the same number of data disks
1735 struct imsm_map
*map
= get_imsm_map(dev
, second_map
);
1737 switch (get_imsm_raid_level(map
)) {
1741 return map
->num_members
;
1743 return map
->num_members
- 1;
1745 dprintf("%s: unsupported raid level\n", __func__
);
1750 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
1752 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1753 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1755 switch(get_imsm_raid_level(map
)) {
1758 return chunk
* map
->num_domains
;
1760 return chunk
* map
->num_members
;
1766 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
1768 struct imsm_map
*map
= get_imsm_map(dev
, 1);
1769 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1770 __u32 strip
= block
/ chunk
;
1772 switch (get_imsm_raid_level(map
)) {
1775 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
1776 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
1778 return vol_stripe
* chunk
+ block
% chunk
;
1780 __u32 stripe
= strip
/ (map
->num_members
- 1);
1782 return stripe
* chunk
+ block
% chunk
;
1789 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
1790 struct imsm_dev
*dev
)
1792 /* calculate the conversion factor between per member 'blocks'
1793 * (md/{resync,rebuild}_start) and imsm migration units, return
1794 * 0 for the 'not migrating' and 'unsupported migration' cases
1796 if (!dev
->vol
.migr_state
)
1799 switch (migr_type(dev
)) {
1800 case MIGR_GEN_MIGR
: {
1801 struct migr_record
*migr_rec
= super
->migr_rec
;
1802 return __le32_to_cpu(migr_rec
->blocks_per_unit
);
1807 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1808 __u32 stripes_per_unit
;
1809 __u32 blocks_per_unit
;
1818 /* yes, this is really the translation of migr_units to
1819 * per-member blocks in the 'resync' case
1821 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
1822 migr_chunk
= migr_strip_blocks_resync(dev
);
1823 disks
= imsm_num_data_members(dev
, 0);
1824 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
1825 stripe
= __le16_to_cpu(map
->blocks_per_strip
) * disks
;
1826 segment
= blocks_per_unit
/ stripe
;
1827 block_rel
= blocks_per_unit
- segment
* stripe
;
1828 parity_depth
= parity_segment_depth(dev
);
1829 block_map
= map_migr_block(dev
, block_rel
);
1830 return block_map
+ parity_depth
* segment
;
1832 case MIGR_REBUILD
: {
1833 __u32 stripes_per_unit
;
1836 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
1837 migr_chunk
= migr_strip_blocks_rebuild(dev
);
1838 return migr_chunk
* stripes_per_unit
;
1840 case MIGR_STATE_CHANGE
:
1846 static int imsm_level_to_layout(int level
)
1854 return ALGORITHM_LEFT_ASYMMETRIC
;
1861 /*******************************************************************************
1862 * Function: read_imsm_migr_rec
1863 * Description: Function reads imsm migration record from last sector of disk
1865 * fd : disk descriptor
1866 * super : metadata info
1870 ******************************************************************************/
1871 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
)
1874 unsigned long long dsize
;
1876 get_dev_size(fd
, NULL
, &dsize
);
1877 if (lseek64(fd
, dsize
- 512, SEEK_SET
) < 0) {
1879 Name
": Cannot seek to anchor block: %s\n",
1883 if (read(fd
, super
->migr_rec_buf
, 512) != 512) {
1885 Name
": Cannot read migr record block: %s\n",
1895 /*******************************************************************************
1896 * Function: load_imsm_migr_rec
1897 * Description: Function reads imsm migration record (it is stored at the last
1900 * super : imsm internal array info
1901 * info : general array info
1905 ******************************************************************************/
1906 static int load_imsm_migr_rec(struct intel_super
*super
, struct mdinfo
*info
)
1909 struct dl
*dl
= NULL
;
1915 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
1916 /* read only from one of the first two slots */
1917 if ((sd
->disk
.raid_disk
> 1) ||
1918 (sd
->disk
.raid_disk
< 0))
1920 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
1921 fd
= dev_open(nm
, O_RDONLY
);
1927 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
1928 /* read only from one of the first two slots */
1931 sprintf(nm
, "%d:%d", dl
->major
, dl
->minor
);
1932 fd
= dev_open(nm
, O_RDONLY
);
1939 retval
= read_imsm_migr_rec(fd
, super
);
1947 /*******************************************************************************
1948 * function: imsm_create_metadata_checkpoint_update
1949 * Description: It creates update for checkpoint change.
1951 * super : imsm internal array info
1952 * u : pointer to prepared update
1955 * If length is equal to 0, input pointer u contains no update
1956 ******************************************************************************/
1957 static int imsm_create_metadata_checkpoint_update(
1958 struct intel_super
*super
,
1959 struct imsm_update_general_migration_checkpoint
**u
)
1962 int update_memory_size
= 0;
1964 dprintf("imsm_create_metadata_checkpoint_update(enter)\n");
1970 /* size of all update data without anchor */
1971 update_memory_size
=
1972 sizeof(struct imsm_update_general_migration_checkpoint
);
1974 *u
= calloc(1, update_memory_size
);
1976 dprintf("error: cannot get memory for "
1977 "imsm_create_metadata_checkpoint_update update\n");
1980 (*u
)->type
= update_general_migration_checkpoint
;
1981 (*u
)->curr_migr_unit
= __le32_to_cpu(super
->migr_rec
->curr_migr_unit
);
1982 dprintf("imsm_create_metadata_checkpoint_update: prepared for %u\n",
1983 (*u
)->curr_migr_unit
);
1985 return update_memory_size
;
1989 static void imsm_update_metadata_locally(struct supertype
*st
,
1990 void *buf
, int len
);
1992 /*******************************************************************************
1993 * Function: write_imsm_migr_rec
1994 * Description: Function writes imsm migration record
1995 * (at the last sector of disk)
1997 * super : imsm internal array info
2001 ******************************************************************************/
2002 static int write_imsm_migr_rec(struct supertype
*st
)
2004 struct intel_super
*super
= st
->sb
;
2005 unsigned long long dsize
;
2011 struct imsm_update_general_migration_checkpoint
*u
;
2013 for (sd
= super
->disks
; sd
; sd
= sd
->next
) {
2014 /* write to 2 first slots only */
2015 if ((sd
->index
< 0) || (sd
->index
> 1))
2017 sprintf(nm
, "%d:%d", sd
->major
, sd
->minor
);
2018 fd
= dev_open(nm
, O_RDWR
);
2021 get_dev_size(fd
, NULL
, &dsize
);
2022 if (lseek64(fd
, dsize
- 512, SEEK_SET
) < 0) {
2024 Name
": Cannot seek to anchor block: %s\n",
2028 if (write(fd
, super
->migr_rec_buf
, 512) != 512) {
2030 Name
": Cannot write migr record block: %s\n",
2037 /* update checkpoint information in metadata */
2038 len
= imsm_create_metadata_checkpoint_update(super
, &u
);
2041 dprintf("imsm: Cannot prepare update\n");
2044 /* update metadata locally */
2045 imsm_update_metadata_locally(st
, u
, len
);
2046 /* and possibly remotely */
2047 if (st
->update_tail
) {
2048 append_metadata_update(st
, u
, len
);
2049 /* during reshape we do all work inside metadata handler
2050 * manage_reshape(), so metadata update has to be triggered
2053 flush_metadata_updates(st
);
2054 st
->update_tail
= &st
->updates
;
2065 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
2067 struct intel_super
*super
= st
->sb
;
2068 struct migr_record
*migr_rec
= super
->migr_rec
;
2069 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
2070 struct imsm_map
*map
= get_imsm_map(dev
, 0);
2071 struct imsm_map
*prev_map
= get_imsm_map(dev
, 1);
2072 struct imsm_map
*map_to_analyse
= map
;
2075 unsigned int component_size_alligment
;
2076 int map_disks
= info
->array
.raid_disks
;
2078 memset(info
, 0, sizeof(*info
));
2080 map_to_analyse
= prev_map
;
2084 info
->container_member
= super
->current_vol
;
2085 info
->array
.raid_disks
= map
->num_members
;
2086 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
2087 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
2088 info
->array
.md_minor
= -1;
2089 info
->array
.ctime
= 0;
2090 info
->array
.utime
= 0;
2091 info
->array
.chunk_size
=
2092 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
2093 info
->array
.state
= !dev
->vol
.dirty
;
2094 info
->custom_array_size
= __le32_to_cpu(dev
->size_high
);
2095 info
->custom_array_size
<<= 32;
2096 info
->custom_array_size
|= __le32_to_cpu(dev
->size_low
);
2097 if (prev_map
&& map
->map_state
== prev_map
->map_state
) {
2098 info
->reshape_active
= 1;
2099 info
->new_level
= get_imsm_raid_level(map
);
2100 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
2101 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
2102 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
2103 if (info
->delta_disks
) {
2104 /* this needs to be applied to every array
2107 info
->reshape_active
= 2;
2109 /* We shape information that we give to md might have to be
2110 * modify to cope with md's requirement for reshaping arrays.
2111 * For example, when reshaping a RAID0, md requires it to be
2112 * presented as a degraded RAID4.
2113 * Also if a RAID0 is migrating to a RAID5 we need to specify
2114 * the array as already being RAID5, but the 'before' layout
2115 * is a RAID4-like layout.
2117 switch (info
->array
.level
) {
2119 switch(info
->new_level
) {
2121 /* conversion is happening as RAID4 */
2122 info
->array
.level
= 4;
2123 info
->array
.raid_disks
+= 1;
2126 /* conversion is happening as RAID5 */
2127 info
->array
.level
= 5;
2128 info
->array
.layout
= ALGORITHM_PARITY_N
;
2129 info
->delta_disks
-= 1;
2132 /* FIXME error message */
2133 info
->array
.level
= UnSet
;
2139 info
->new_level
= UnSet
;
2140 info
->new_layout
= UnSet
;
2141 info
->new_chunk
= info
->array
.chunk_size
;
2142 info
->delta_disks
= 0;
2144 info
->disk
.major
= 0;
2145 info
->disk
.minor
= 0;
2147 info
->disk
.major
= dl
->major
;
2148 info
->disk
.minor
= dl
->minor
;
2151 info
->data_offset
= __le32_to_cpu(map_to_analyse
->pba_of_lba0
);
2152 info
->component_size
=
2153 __le32_to_cpu(map_to_analyse
->blocks_per_member
);
2155 /* check component size aligment
2157 component_size_alligment
=
2158 info
->component_size
% (info
->array
.chunk_size
/512);
2160 if (component_size_alligment
&&
2161 (info
->array
.level
!= 1) && (info
->array
.level
!= UnSet
)) {
2162 dprintf("imsm: reported component size alligned from %llu ",
2163 info
->component_size
);
2164 info
->component_size
-= component_size_alligment
;
2165 dprintf("to %llu (%i).\n",
2166 info
->component_size
, component_size_alligment
);
2169 memset(info
->uuid
, 0, sizeof(info
->uuid
));
2170 info
->recovery_start
= MaxSector
;
2172 info
->reshape_progress
= 0;
2173 info
->resync_start
= MaxSector
;
2174 if (map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
2176 info
->resync_start
= 0;
2178 if (dev
->vol
.migr_state
) {
2179 switch (migr_type(dev
)) {
2182 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2184 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
2186 info
->resync_start
= blocks_per_unit
* units
;
2189 case MIGR_GEN_MIGR
: {
2190 __u64 blocks_per_unit
= blocks_per_migr_unit(super
,
2192 __u64 units
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
2193 unsigned long long array_blocks
;
2196 if (__le32_to_cpu(migr_rec
->ascending_migr
) &&
2198 (__le32_to_cpu(migr_rec
->num_migr_units
)-1)) &&
2199 (super
->migr_rec
->rec_status
==
2200 __cpu_to_le32(UNIT_SRC_IN_CP_AREA
)))
2203 info
->reshape_progress
= blocks_per_unit
* units
;
2205 dprintf("IMSM: General Migration checkpoint : %llu "
2206 "(%llu) -> read reshape progress : %llu\n",
2207 units
, blocks_per_unit
, info
->reshape_progress
);
2209 used_disks
= imsm_num_data_members(dev
, 1);
2210 if (used_disks
> 0) {
2211 array_blocks
= map
->blocks_per_member
*
2213 /* round array size down to closest MB
2215 info
->custom_array_size
= (array_blocks
2216 >> SECT_PER_MB_SHIFT
)
2217 << SECT_PER_MB_SHIFT
;
2221 /* we could emulate the checkpointing of
2222 * 'sync_action=check' migrations, but for now
2223 * we just immediately complete them
2226 /* this is handled by container_content_imsm() */
2227 case MIGR_STATE_CHANGE
:
2228 /* FIXME handle other migrations */
2230 /* we are not dirty, so... */
2231 info
->resync_start
= MaxSector
;
2235 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
2236 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
2238 info
->array
.major_version
= -1;
2239 info
->array
.minor_version
= -2;
2240 devname
= devnum2devname(st
->container_dev
);
2241 *info
->text_version
= '\0';
2243 sprintf(info
->text_version
, "/%s/%d", devname
, info
->container_member
);
2245 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
2246 uuid_from_super_imsm(st
, info
->uuid
);
2250 for (i
=0; i
<map_disks
; i
++) {
2252 if (i
< info
->array
.raid_disks
) {
2253 struct imsm_disk
*dsk
;
2254 j
= get_imsm_disk_idx(dev
, i
, -1);
2255 dsk
= get_imsm_disk(super
, j
);
2256 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
2263 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
, int failed
);
2264 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
);
2266 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
2270 for (d
= super
->missing
; d
; d
= d
->next
)
2271 if (d
->index
== index
)
2276 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
2278 struct intel_super
*super
= st
->sb
;
2279 struct imsm_disk
*disk
;
2280 int map_disks
= info
->array
.raid_disks
;
2281 int max_enough
= -1;
2283 struct imsm_super
*mpb
;
2285 if (super
->current_vol
>= 0) {
2286 getinfo_super_imsm_volume(st
, info
, map
);
2289 memset(info
, 0, sizeof(*info
));
2291 /* Set raid_disks to zero so that Assemble will always pull in valid
2294 info
->array
.raid_disks
= 0;
2295 info
->array
.level
= LEVEL_CONTAINER
;
2296 info
->array
.layout
= 0;
2297 info
->array
.md_minor
= -1;
2298 info
->array
.ctime
= 0; /* N/A for imsm */
2299 info
->array
.utime
= 0;
2300 info
->array
.chunk_size
= 0;
2302 info
->disk
.major
= 0;
2303 info
->disk
.minor
= 0;
2304 info
->disk
.raid_disk
= -1;
2305 info
->reshape_active
= 0;
2306 info
->array
.major_version
= -1;
2307 info
->array
.minor_version
= -2;
2308 strcpy(info
->text_version
, "imsm");
2309 info
->safe_mode_delay
= 0;
2310 info
->disk
.number
= -1;
2311 info
->disk
.state
= 0;
2313 info
->recovery_start
= MaxSector
;
2315 /* do we have the all the insync disks that we expect? */
2316 mpb
= super
->anchor
;
2318 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
2319 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
2320 int failed
, enough
, j
, missing
= 0;
2321 struct imsm_map
*map
;
2324 failed
= imsm_count_failed(super
, dev
);
2325 state
= imsm_check_degraded(super
, dev
, failed
);
2326 map
= get_imsm_map(dev
, dev
->vol
.migr_state
);
2328 /* any newly missing disks?
2329 * (catches single-degraded vs double-degraded)
2331 for (j
= 0; j
< map
->num_members
; j
++) {
2332 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, -1);
2333 __u32 idx
= ord_to_idx(ord
);
2335 if (!(ord
& IMSM_ORD_REBUILD
) &&
2336 get_imsm_missing(super
, idx
)) {
2342 if (state
== IMSM_T_STATE_FAILED
)
2344 else if (state
== IMSM_T_STATE_DEGRADED
&&
2345 (state
!= map
->map_state
|| missing
))
2347 else /* we're normal, or already degraded */
2350 /* in the missing/failed disk case check to see
2351 * if at least one array is runnable
2353 max_enough
= max(max_enough
, enough
);
2355 dprintf("%s: enough: %d\n", __func__
, max_enough
);
2356 info
->container_enough
= max_enough
;
2359 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
2361 disk
= &super
->disks
->disk
;
2362 info
->data_offset
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
2363 info
->component_size
= reserved
;
2364 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
2365 /* we don't change info->disk.raid_disk here because
2366 * this state will be finalized in mdmon after we have
2367 * found the 'most fresh' version of the metadata
2369 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
2370 info
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
2373 /* only call uuid_from_super_imsm when this disk is part of a populated container,
2374 * ->compare_super may have updated the 'num_raid_devs' field for spares
2376 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
2377 uuid_from_super_imsm(st
, info
->uuid
);
2379 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
2381 /* I don't know how to compute 'map' on imsm, so use safe default */
2384 for (i
= 0; i
< map_disks
; i
++)
2390 /* allocates memory and fills disk in mdinfo structure
2391 * for each disk in array */
2392 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
2394 struct mdinfo
*mddev
= NULL
;
2395 struct intel_super
*super
= st
->sb
;
2396 struct imsm_disk
*disk
;
2399 if (!super
|| !super
->disks
)
2402 mddev
= malloc(sizeof(*mddev
));
2404 fprintf(stderr
, Name
": Failed to allocate memory.\n");
2407 memset(mddev
, 0, sizeof(*mddev
));
2411 tmp
= malloc(sizeof(*tmp
));
2413 fprintf(stderr
, Name
": Failed to allocate memory.\n");
2418 memset(tmp
, 0, sizeof(*tmp
));
2420 tmp
->next
= mddev
->devs
;
2422 tmp
->disk
.number
= count
++;
2423 tmp
->disk
.major
= dl
->major
;
2424 tmp
->disk
.minor
= dl
->minor
;
2425 tmp
->disk
.state
= is_configured(disk
) ?
2426 (1 << MD_DISK_ACTIVE
) : 0;
2427 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
2428 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
2429 tmp
->disk
.raid_disk
= -1;
2435 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
2436 char *update
, char *devname
, int verbose
,
2437 int uuid_set
, char *homehost
)
2439 /* For 'assemble' and 'force' we need to return non-zero if any
2440 * change was made. For others, the return value is ignored.
2441 * Update options are:
2442 * force-one : This device looks a bit old but needs to be included,
2443 * update age info appropriately.
2444 * assemble: clear any 'faulty' flag to allow this device to
2446 * force-array: Array is degraded but being forced, mark it clean
2447 * if that will be needed to assemble it.
2449 * newdev: not used ????
2450 * grow: Array has gained a new device - this is currently for
2452 * resync: mark as dirty so a resync will happen.
2453 * name: update the name - preserving the homehost
2454 * uuid: Change the uuid of the array to match watch is given
2456 * Following are not relevant for this imsm:
2457 * sparc2.2 : update from old dodgey metadata
2458 * super-minor: change the preferred_minor number
2459 * summaries: update redundant counters.
2460 * homehost: update the recorded homehost
2461 * _reshape_progress: record new reshape_progress position.
2464 struct intel_super
*super
= st
->sb
;
2465 struct imsm_super
*mpb
;
2467 /* we can only update container info */
2468 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
2471 mpb
= super
->anchor
;
2473 if (strcmp(update
, "uuid") == 0 && uuid_set
&& !info
->update_private
)
2475 else if (strcmp(update
, "uuid") == 0 && uuid_set
&& info
->update_private
) {
2476 mpb
->orig_family_num
= *((__u32
*) info
->update_private
);
2478 } else if (strcmp(update
, "uuid") == 0) {
2479 __u32
*new_family
= malloc(sizeof(*new_family
));
2481 /* update orig_family_number with the incoming random
2482 * data, report the new effective uuid, and store the
2483 * new orig_family_num for future updates.
2486 memcpy(&mpb
->orig_family_num
, info
->uuid
, sizeof(__u32
));
2487 uuid_from_super_imsm(st
, info
->uuid
);
2488 *new_family
= mpb
->orig_family_num
;
2489 info
->update_private
= new_family
;
2492 } else if (strcmp(update
, "assemble") == 0)
2497 /* successful update? recompute checksum */
2499 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
2504 static size_t disks_to_mpb_size(int disks
)
2508 size
= sizeof(struct imsm_super
);
2509 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
2510 size
+= 2 * sizeof(struct imsm_dev
);
2511 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
2512 size
+= (4 - 2) * sizeof(struct imsm_map
);
2513 /* 4 possible disk_ord_tbl's */
2514 size
+= 4 * (disks
- 1) * sizeof(__u32
);
2519 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
)
2521 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
2524 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
2527 static void free_devlist(struct intel_super
*super
)
2529 struct intel_dev
*dv
;
2531 while (super
->devlist
) {
2532 dv
= super
->devlist
->next
;
2533 free(super
->devlist
->dev
);
2534 free(super
->devlist
);
2535 super
->devlist
= dv
;
2539 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
2541 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
2544 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
2548 * 0 same, or first was empty, and second was copied
2549 * 1 second had wrong number
2551 * 3 wrong other info
2553 struct intel_super
*first
= st
->sb
;
2554 struct intel_super
*sec
= tst
->sb
;
2561 /* in platform dependent environment test if the disks
2562 * use the same Intel hba
2564 if (!check_env("IMSM_NO_PLATFORM")) {
2565 if (!first
->hba
|| !sec
->hba
||
2566 (first
->hba
->type
!= sec
->hba
->type
)) {
2568 "HBAs of devices does not match %s != %s\n",
2569 first
->hba
? get_sys_dev_type(first
->hba
->type
) : NULL
,
2570 sec
->hba
? get_sys_dev_type(sec
->hba
->type
) : NULL
);
2575 /* if an anchor does not have num_raid_devs set then it is a free
2578 if (first
->anchor
->num_raid_devs
> 0 &&
2579 sec
->anchor
->num_raid_devs
> 0) {
2580 /* Determine if these disks might ever have been
2581 * related. Further disambiguation can only take place
2582 * in load_super_imsm_all
2584 __u32 first_family
= first
->anchor
->orig_family_num
;
2585 __u32 sec_family
= sec
->anchor
->orig_family_num
;
2587 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
2588 MAX_SIGNATURE_LENGTH
) != 0)
2591 if (first_family
== 0)
2592 first_family
= first
->anchor
->family_num
;
2593 if (sec_family
== 0)
2594 sec_family
= sec
->anchor
->family_num
;
2596 if (first_family
!= sec_family
)
2602 /* if 'first' is a spare promote it to a populated mpb with sec's
2605 if (first
->anchor
->num_raid_devs
== 0 &&
2606 sec
->anchor
->num_raid_devs
> 0) {
2608 struct intel_dev
*dv
;
2609 struct imsm_dev
*dev
;
2611 /* we need to copy raid device info from sec if an allocation
2612 * fails here we don't associate the spare
2614 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
2615 dv
= malloc(sizeof(*dv
));
2618 dev
= malloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
2625 dv
->next
= first
->devlist
;
2626 first
->devlist
= dv
;
2628 if (i
< sec
->anchor
->num_raid_devs
) {
2629 /* allocation failure */
2630 free_devlist(first
);
2631 fprintf(stderr
, "imsm: failed to associate spare\n");
2634 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
2635 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
2636 first
->anchor
->family_num
= sec
->anchor
->family_num
;
2637 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
2638 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
2639 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
2645 static void fd2devname(int fd
, char *name
)
2649 char dname
[PATH_MAX
];
2654 if (fstat(fd
, &st
) != 0)
2656 sprintf(path
, "/sys/dev/block/%d:%d",
2657 major(st
.st_rdev
), minor(st
.st_rdev
));
2659 rv
= readlink(path
, dname
, sizeof(dname
));
2664 nm
= strrchr(dname
, '/');
2666 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
2669 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
2671 static int imsm_read_serial(int fd
, char *devname
,
2672 __u8 serial
[MAX_RAID_SERIAL_LEN
])
2674 unsigned char scsi_serial
[255];
2683 memset(scsi_serial
, 0, sizeof(scsi_serial
));
2685 rv
= scsi_get_serial(fd
, scsi_serial
, sizeof(scsi_serial
));
2687 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
2688 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
2689 fd2devname(fd
, (char *) serial
);
2696 Name
": Failed to retrieve serial for %s\n",
2701 rsp_len
= scsi_serial
[3];
2705 Name
": Failed to retrieve serial for %s\n",
2709 rsp_buf
= (char *) &scsi_serial
[4];
2711 /* trim all whitespace and non-printable characters and convert
2714 for (i
= 0, dest
= rsp_buf
; i
< rsp_len
; i
++) {
2717 /* ':' is reserved for use in placeholder serial
2718 * numbers for missing disks
2726 len
= dest
- rsp_buf
;
2729 /* truncate leading characters */
2730 if (len
> MAX_RAID_SERIAL_LEN
) {
2731 dest
+= len
- MAX_RAID_SERIAL_LEN
;
2732 len
= MAX_RAID_SERIAL_LEN
;
2735 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
2736 memcpy(serial
, dest
, len
);
2741 static int serialcmp(__u8
*s1
, __u8
*s2
)
2743 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
2746 static void serialcpy(__u8
*dest
, __u8
*src
)
2748 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
2752 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
2756 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
2757 if (serialcmp(dl
->serial
, serial
) == 0)
2764 static struct imsm_disk
*
2765 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
2769 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2770 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
2772 if (serialcmp(disk
->serial
, serial
) == 0) {
2783 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
2785 struct imsm_disk
*disk
;
2790 __u8 serial
[MAX_RAID_SERIAL_LEN
];
2792 rv
= imsm_read_serial(fd
, devname
, serial
);
2797 dl
= calloc(1, sizeof(*dl
));
2801 Name
": failed to allocate disk buffer for %s\n",
2807 dl
->major
= major(stb
.st_rdev
);
2808 dl
->minor
= minor(stb
.st_rdev
);
2809 dl
->next
= super
->disks
;
2810 dl
->fd
= keep_fd
? fd
: -1;
2811 assert(super
->disks
== NULL
);
2813 serialcpy(dl
->serial
, serial
);
2816 fd2devname(fd
, name
);
2818 dl
->devname
= strdup(devname
);
2820 dl
->devname
= strdup(name
);
2822 /* look up this disk's index in the current anchor */
2823 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
2826 /* only set index on disks that are a member of a
2827 * populated contianer, i.e. one with raid_devs
2829 if (is_failed(&dl
->disk
))
2831 else if (is_spare(&dl
->disk
))
2839 /* When migrating map0 contains the 'destination' state while map1
2840 * contains the current state. When not migrating map0 contains the
2841 * current state. This routine assumes that map[0].map_state is set to
2842 * the current array state before being called.
2844 * Migration is indicated by one of the following states
2845 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
2846 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
2847 * map1state=unitialized)
2848 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
2850 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
2851 * map1state=degraded)
2852 * 5/ Migration (mig_state=1 migr_type=MIGR_GEN_MIGR map0state=normal
2855 static void migrate(struct imsm_dev
*dev
, struct intel_super
*super
,
2856 __u8 to_state
, int migr_type
)
2858 struct imsm_map
*dest
;
2859 struct imsm_map
*src
= get_imsm_map(dev
, 0);
2861 dev
->vol
.migr_state
= 1;
2862 set_migr_type(dev
, migr_type
);
2863 dev
->vol
.curr_migr_unit
= 0;
2864 dest
= get_imsm_map(dev
, 1);
2866 /* duplicate and then set the target end state in map[0] */
2867 memcpy(dest
, src
, sizeof_imsm_map(src
));
2868 if ((migr_type
== MIGR_REBUILD
) ||
2869 (migr_type
== MIGR_GEN_MIGR
)) {
2873 for (i
= 0; i
< src
->num_members
; i
++) {
2874 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
2875 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
2879 if (migr_type
== MIGR_GEN_MIGR
)
2880 /* Clear migration record */
2881 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
2883 src
->map_state
= to_state
;
2886 static void end_migration(struct imsm_dev
*dev
, __u8 map_state
)
2888 struct imsm_map
*map
= get_imsm_map(dev
, 0);
2889 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
);
2892 /* merge any IMSM_ORD_REBUILD bits that were not successfully
2893 * completed in the last migration.
2895 * FIXME add support for raid-level-migration
2897 for (i
= 0; i
< prev
->num_members
; i
++)
2898 for (j
= 0; j
< map
->num_members
; j
++)
2899 /* during online capacity expansion
2900 * disks position can be changed if takeover is used
2902 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
2903 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
2904 map
->disk_ord_tbl
[j
] |= prev
->disk_ord_tbl
[i
];
2908 dev
->vol
.migr_state
= 0;
2909 dev
->vol
.migr_type
= 0;
2910 dev
->vol
.curr_migr_unit
= 0;
2911 map
->map_state
= map_state
;
2915 static int parse_raid_devices(struct intel_super
*super
)
2918 struct imsm_dev
*dev_new
;
2919 size_t len
, len_migr
;
2921 size_t space_needed
= 0;
2922 struct imsm_super
*mpb
= super
->anchor
;
2924 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
2925 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
2926 struct intel_dev
*dv
;
2928 len
= sizeof_imsm_dev(dev_iter
, 0);
2929 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
2931 space_needed
+= len_migr
- len
;
2933 dv
= malloc(sizeof(*dv
));
2936 if (max_len
< len_migr
)
2938 if (max_len
> len_migr
)
2939 space_needed
+= max_len
- len_migr
;
2940 dev_new
= malloc(max_len
);
2945 imsm_copy_dev(dev_new
, dev_iter
);
2948 dv
->next
= super
->devlist
;
2949 super
->devlist
= dv
;
2952 /* ensure that super->buf is large enough when all raid devices
2955 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
2958 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
, 512);
2959 if (posix_memalign(&buf
, 512, len
) != 0)
2962 memcpy(buf
, super
->buf
, super
->len
);
2963 memset(buf
+ super
->len
, 0, len
- super
->len
);
2972 /* retrieve a pointer to the bbm log which starts after all raid devices */
2973 struct bbm_log
*__get_imsm_bbm_log(struct imsm_super
*mpb
)
2977 if (__le32_to_cpu(mpb
->bbm_log_size
)) {
2979 ptr
+= mpb
->mpb_size
- __le32_to_cpu(mpb
->bbm_log_size
);
2985 /*******************************************************************************
2986 * Function: check_mpb_migr_compatibility
2987 * Description: Function checks for unsupported migration features:
2988 * - migration optimization area (pba_of_lba0)
2989 * - descending reshape (ascending_migr)
2991 * super : imsm metadata information
2993 * 0 : migration is compatible
2994 * -1 : migration is not compatible
2995 ******************************************************************************/
2996 int check_mpb_migr_compatibility(struct intel_super
*super
)
2998 struct imsm_map
*map0
, *map1
;
2999 struct migr_record
*migr_rec
= super
->migr_rec
;
3002 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
3003 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
3006 dev_iter
->vol
.migr_state
== 1 &&
3007 dev_iter
->vol
.migr_type
== MIGR_GEN_MIGR
) {
3008 /* This device is migrating */
3009 map0
= get_imsm_map(dev_iter
, 0);
3010 map1
= get_imsm_map(dev_iter
, 1);
3011 if (map0
->pba_of_lba0
!= map1
->pba_of_lba0
)
3012 /* migration optimization area was used */
3014 if (migr_rec
->ascending_migr
== 0
3015 && migr_rec
->dest_depth_per_unit
> 0)
3016 /* descending reshape not supported yet */
3023 static void __free_imsm(struct intel_super
*super
, int free_disks
);
3025 /* load_imsm_mpb - read matrix metadata
3026 * allocates super->mpb to be freed by free_imsm
3028 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
3030 unsigned long long dsize
;
3031 unsigned long long sectors
;
3033 struct imsm_super
*anchor
;
3036 get_dev_size(fd
, NULL
, &dsize
);
3040 Name
": %s: device to small for imsm\n",
3045 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0) {
3047 fprintf(stderr
, Name
3048 ": Cannot seek to anchor block on %s: %s\n",
3049 devname
, strerror(errno
));
3053 if (posix_memalign((void**)&anchor
, 512, 512) != 0) {
3056 Name
": Failed to allocate imsm anchor buffer"
3057 " on %s\n", devname
);
3060 if (read(fd
, anchor
, 512) != 512) {
3063 Name
": Cannot read anchor block on %s: %s\n",
3064 devname
, strerror(errno
));
3069 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
3072 Name
": no IMSM anchor on %s\n", devname
);
3077 __free_imsm(super
, 0);
3078 /* reload capability and hba */
3080 /* capability and hba must be updated with new super allocation */
3081 find_intel_hba_capability(fd
, super
, devname
);
3082 super
->len
= ROUND_UP(anchor
->mpb_size
, 512);
3083 if (posix_memalign(&super
->buf
, 512, super
->len
) != 0) {
3086 Name
": unable to allocate %zu byte mpb buffer\n",
3091 memcpy(super
->buf
, anchor
, 512);
3093 sectors
= mpb_sectors(anchor
) - 1;
3096 if (posix_memalign(&super
->migr_rec_buf
, 512, 512) != 0) {
3097 fprintf(stderr
, Name
3098 ": %s could not allocate migr_rec buffer\n", __func__
);
3104 check_sum
= __gen_imsm_checksum(super
->anchor
);
3105 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3108 Name
": IMSM checksum %x != %x on %s\n",
3110 __le32_to_cpu(super
->anchor
->check_sum
),
3118 /* read the extended mpb */
3119 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0) {
3122 Name
": Cannot seek to extended mpb on %s: %s\n",
3123 devname
, strerror(errno
));
3127 if ((unsigned)read(fd
, super
->buf
+ 512, super
->len
- 512) != super
->len
- 512) {
3130 Name
": Cannot read extended mpb on %s: %s\n",
3131 devname
, strerror(errno
));
3135 check_sum
= __gen_imsm_checksum(super
->anchor
);
3136 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
3139 Name
": IMSM checksum %x != %x on %s\n",
3140 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
3145 /* FIXME the BBM log is disk specific so we cannot use this global
3146 * buffer for all disks. Ok for now since we only look at the global
3147 * bbm_log_size parameter to gate assembly
3149 super
->bbm_log
= __get_imsm_bbm_log(super
->anchor
);
3154 static int read_imsm_migr_rec(int fd
, struct intel_super
*super
);
3157 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
3161 err
= load_imsm_mpb(fd
, super
, devname
);
3164 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
3167 err
= parse_raid_devices(super
);
3172 static void __free_imsm_disk(struct dl
*d
)
3184 static void free_imsm_disks(struct intel_super
*super
)
3188 while (super
->disks
) {
3190 super
->disks
= d
->next
;
3191 __free_imsm_disk(d
);
3193 while (super
->disk_mgmt_list
) {
3194 d
= super
->disk_mgmt_list
;
3195 super
->disk_mgmt_list
= d
->next
;
3196 __free_imsm_disk(d
);
3198 while (super
->missing
) {
3200 super
->missing
= d
->next
;
3201 __free_imsm_disk(d
);
3206 /* free all the pieces hanging off of a super pointer */
3207 static void __free_imsm(struct intel_super
*super
, int free_disks
)
3209 struct intel_hba
*elem
, *next
;
3215 /* unlink capability description */
3217 if (super
->migr_rec_buf
) {
3218 free(super
->migr_rec_buf
);
3219 super
->migr_rec_buf
= NULL
;
3222 free_imsm_disks(super
);
3223 free_devlist(super
);
3227 free((void *)elem
->path
);
3235 static void free_imsm(struct intel_super
*super
)
3237 __free_imsm(super
, 1);
3241 static void free_super_imsm(struct supertype
*st
)
3243 struct intel_super
*super
= st
->sb
;
3252 static struct intel_super
*alloc_super(void)
3254 struct intel_super
*super
= malloc(sizeof(*super
));
3257 memset(super
, 0, sizeof(*super
));
3258 super
->current_vol
= -1;
3259 super
->create_offset
= ~((__u32
) 0);
3265 * find and allocate hba and OROM/EFI based on valid fd of RAID component device
3267 static int find_intel_hba_capability(int fd
, struct intel_super
*super
, char *devname
)
3269 struct sys_dev
*hba_name
;
3272 if ((fd
< 0) || check_env("IMSM_NO_PLATFORM")) {
3277 hba_name
= find_disk_attached_hba(fd
, NULL
);
3281 Name
": %s is not attached to Intel(R) RAID controller.\n",
3285 rv
= attach_hba_to_super(super
, hba_name
);
3288 struct intel_hba
*hba
= super
->hba
;
3290 fprintf(stderr
, Name
": %s is attached to Intel(R) %s RAID "
3291 "controller (%s),\n"
3292 " but the container is assigned to Intel(R) "
3293 "%s RAID controller (",
3296 hba_name
->pci_id
? : "Err!",
3297 get_sys_dev_type(hba_name
->type
));
3300 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
3302 fprintf(stderr
, ", ");
3306 fprintf(stderr
, ").\n"
3307 " Mixing devices attached to different controllers "
3308 "is not allowed.\n");
3310 free_sys_dev(&hba_name
);
3313 super
->orom
= find_imsm_capability(hba_name
->type
);
3314 free_sys_dev(&hba_name
);
3321 /* find_missing - helper routine for load_super_imsm_all that identifies
3322 * disks that have disappeared from the system. This routine relies on
3323 * the mpb being uptodate, which it is at load time.
3325 static int find_missing(struct intel_super
*super
)
3328 struct imsm_super
*mpb
= super
->anchor
;
3330 struct imsm_disk
*disk
;
3332 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3333 disk
= __get_imsm_disk(mpb
, i
);
3334 dl
= serial_to_dl(disk
->serial
, super
);
3338 dl
= malloc(sizeof(*dl
));
3344 dl
->devname
= strdup("missing");
3346 serialcpy(dl
->serial
, disk
->serial
);
3349 dl
->next
= super
->missing
;
3350 super
->missing
= dl
;
3356 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
3358 struct intel_disk
*idisk
= disk_list
;
3361 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
3363 idisk
= idisk
->next
;
3369 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
3370 struct intel_super
*super
,
3371 struct intel_disk
**disk_list
)
3373 struct imsm_disk
*d
= &super
->disks
->disk
;
3374 struct imsm_super
*mpb
= super
->anchor
;
3377 for (i
= 0; i
< tbl_size
; i
++) {
3378 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
3379 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
3381 if (tbl_mpb
->family_num
== mpb
->family_num
) {
3382 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
3383 dprintf("%s: mpb from %d:%d matches %d:%d\n",
3384 __func__
, super
->disks
->major
,
3385 super
->disks
->minor
,
3386 table
[i
]->disks
->major
,
3387 table
[i
]->disks
->minor
);
3391 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
3392 is_configured(d
) == is_configured(tbl_d
)) &&
3393 tbl_mpb
->generation_num
< mpb
->generation_num
) {
3394 /* current version of the mpb is a
3395 * better candidate than the one in
3396 * super_table, but copy over "cross
3397 * generational" status
3399 struct intel_disk
*idisk
;
3401 dprintf("%s: mpb from %d:%d replaces %d:%d\n",
3402 __func__
, super
->disks
->major
,
3403 super
->disks
->minor
,
3404 table
[i
]->disks
->major
,
3405 table
[i
]->disks
->minor
);
3407 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
3408 if (idisk
&& is_failed(&idisk
->disk
))
3409 tbl_d
->status
|= FAILED_DISK
;
3412 struct intel_disk
*idisk
;
3413 struct imsm_disk
*disk
;
3415 /* tbl_mpb is more up to date, but copy
3416 * over cross generational status before
3419 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
3420 if (disk
&& is_failed(disk
))
3421 d
->status
|= FAILED_DISK
;
3423 idisk
= disk_list_get(d
->serial
, *disk_list
);
3426 if (disk
&& is_configured(disk
))
3427 idisk
->disk
.status
|= CONFIGURED_DISK
;
3430 dprintf("%s: mpb from %d:%d prefer %d:%d\n",
3431 __func__
, super
->disks
->major
,
3432 super
->disks
->minor
,
3433 table
[i
]->disks
->major
,
3434 table
[i
]->disks
->minor
);
3442 table
[tbl_size
++] = super
;
3446 /* update/extend the merged list of imsm_disk records */
3447 for (j
= 0; j
< mpb
->num_disks
; j
++) {
3448 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
3449 struct intel_disk
*idisk
;
3451 idisk
= disk_list_get(disk
->serial
, *disk_list
);
3453 idisk
->disk
.status
|= disk
->status
;
3454 if (is_configured(&idisk
->disk
) ||
3455 is_failed(&idisk
->disk
))
3456 idisk
->disk
.status
&= ~(SPARE_DISK
);
3458 idisk
= calloc(1, sizeof(*idisk
));
3461 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
3462 idisk
->disk
= *disk
;
3463 idisk
->next
= *disk_list
;
3467 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
3474 static struct intel_super
*
3475 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
3478 struct imsm_super
*mpb
= super
->anchor
;
3482 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3483 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3484 struct intel_disk
*idisk
;
3486 idisk
= disk_list_get(disk
->serial
, disk_list
);
3488 if (idisk
->owner
== owner
||
3489 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
3492 dprintf("%s: '%.16s' owner %d != %d\n",
3493 __func__
, disk
->serial
, idisk
->owner
,
3496 dprintf("%s: unknown disk %x [%d]: %.16s\n",
3497 __func__
, __le32_to_cpu(mpb
->family_num
), i
,
3503 if (ok_count
== mpb
->num_disks
)
3508 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
3510 struct intel_super
*s
;
3512 for (s
= super_list
; s
; s
= s
->next
) {
3513 if (family_num
!= s
->anchor
->family_num
)
3515 fprintf(stderr
, "Conflict, offlining family %#x on '%s'\n",
3516 __le32_to_cpu(family_num
), s
->disks
->devname
);
3520 static struct intel_super
*
3521 imsm_thunderdome(struct intel_super
**super_list
, int len
)
3523 struct intel_super
*super_table
[len
];
3524 struct intel_disk
*disk_list
= NULL
;
3525 struct intel_super
*champion
, *spare
;
3526 struct intel_super
*s
, **del
;
3531 memset(super_table
, 0, sizeof(super_table
));
3532 for (s
= *super_list
; s
; s
= s
->next
)
3533 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
3535 for (i
= 0; i
< tbl_size
; i
++) {
3536 struct imsm_disk
*d
;
3537 struct intel_disk
*idisk
;
3538 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
3541 d
= &s
->disks
->disk
;
3543 /* 'd' must appear in merged disk list for its
3544 * configuration to be valid
3546 idisk
= disk_list_get(d
->serial
, disk_list
);
3547 if (idisk
&& idisk
->owner
== i
)
3548 s
= validate_members(s
, disk_list
, i
);
3553 dprintf("%s: marking family: %#x from %d:%d offline\n",
3554 __func__
, mpb
->family_num
,
3555 super_table
[i
]->disks
->major
,
3556 super_table
[i
]->disks
->minor
);
3560 /* This is where the mdadm implementation differs from the Windows
3561 * driver which has no strict concept of a container. We can only
3562 * assemble one family from a container, so when returning a prodigal
3563 * array member to this system the code will not be able to disambiguate
3564 * the container contents that should be assembled ("foreign" versus
3565 * "local"). It requires user intervention to set the orig_family_num
3566 * to a new value to establish a new container. The Windows driver in
3567 * this situation fixes up the volume name in place and manages the
3568 * foreign array as an independent entity.
3573 for (i
= 0; i
< tbl_size
; i
++) {
3574 struct intel_super
*tbl_ent
= super_table
[i
];
3580 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
3585 if (s
&& !is_spare
) {
3586 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
3588 } else if (!s
&& !is_spare
)
3601 fprintf(stderr
, "Chose family %#x on '%s', "
3602 "assemble conflicts to new container with '--update=uuid'\n",
3603 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
3605 /* collect all dl's onto 'champion', and update them to
3606 * champion's version of the status
3608 for (s
= *super_list
; s
; s
= s
->next
) {
3609 struct imsm_super
*mpb
= champion
->anchor
;
3610 struct dl
*dl
= s
->disks
;
3615 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3616 struct imsm_disk
*disk
;
3618 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
3621 /* only set index on disks that are a member of
3622 * a populated contianer, i.e. one with
3625 if (is_failed(&dl
->disk
))
3627 else if (is_spare(&dl
->disk
))
3633 if (i
>= mpb
->num_disks
) {
3634 struct intel_disk
*idisk
;
3636 idisk
= disk_list_get(dl
->serial
, disk_list
);
3637 if (idisk
&& is_spare(&idisk
->disk
) &&
3638 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
3646 dl
->next
= champion
->disks
;
3647 champion
->disks
= dl
;
3651 /* delete 'champion' from super_list */
3652 for (del
= super_list
; *del
; ) {
3653 if (*del
== champion
) {
3654 *del
= (*del
)->next
;
3657 del
= &(*del
)->next
;
3659 champion
->next
= NULL
;
3663 struct intel_disk
*idisk
= disk_list
;
3665 disk_list
= disk_list
->next
;
3672 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
3676 struct intel_super
*super_list
= NULL
;
3677 struct intel_super
*super
= NULL
;
3678 int devnum
= fd2devnum(fd
);
3684 /* check if 'fd' an opened container */
3685 sra
= sysfs_read(fd
, 0, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
3689 if (sra
->array
.major_version
!= -1 ||
3690 sra
->array
.minor_version
!= -2 ||
3691 strcmp(sra
->text_version
, "imsm") != 0) {
3696 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
3697 struct intel_super
*s
= alloc_super();
3705 s
->next
= super_list
;
3709 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
3710 dfd
= dev_open(nm
, O_RDWR
);
3714 rv
= find_intel_hba_capability(dfd
, s
, devname
);
3715 /* no orom/efi or non-intel hba of the disk */
3719 err
= load_and_parse_mpb(dfd
, s
, NULL
, 1);
3721 /* retry the load if we might have raced against mdmon */
3722 if (err
== 3 && mdmon_running(devnum
))
3723 for (retry
= 0; retry
< 3; retry
++) {
3725 err
= load_and_parse_mpb(dfd
, s
, NULL
, 1);
3733 /* all mpbs enter, maybe one leaves */
3734 super
= imsm_thunderdome(&super_list
, i
);
3740 if (find_missing(super
) != 0) {
3746 /* load migration record */
3747 err
= load_imsm_migr_rec(super
, NULL
);
3753 /* Check migration compatibility */
3754 if (check_mpb_migr_compatibility(super
) != 0) {
3755 fprintf(stderr
, Name
": Unsupported migration detected");
3757 fprintf(stderr
, " on %s\n", devname
);
3759 fprintf(stderr
, " (IMSM).\n");
3768 while (super_list
) {
3769 struct intel_super
*s
= super_list
;
3771 super_list
= super_list
->next
;
3780 st
->container_dev
= devnum
;
3781 if (err
== 0 && st
->ss
== NULL
) {
3782 st
->ss
= &super_imsm
;
3783 st
->minor_version
= 0;
3784 st
->max_devs
= IMSM_MAX_DEVICES
;
3789 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
3791 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
);
3795 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
3797 struct intel_super
*super
;
3800 if (test_partition(fd
))
3801 /* IMSM not allowed on partitions */
3804 free_super_imsm(st
);
3806 super
= alloc_super();
3809 Name
": malloc of %zu failed.\n",
3813 /* Load hba and capabilities if they exist.
3814 * But do not preclude loading metadata in case capabilities or hba are
3815 * non-compliant and ignore_hw_compat is set.
3817 rv
= find_intel_hba_capability(fd
, super
, devname
);
3818 /* no orom/efi or non-intel hba of the disk */
3819 if ((rv
!= 0) && (st
->ignore_hw_compat
== 0)) {
3822 Name
": No OROM/EFI properties for %s\n", devname
);
3826 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
3831 Name
": Failed to load all information "
3832 "sections on %s\n", devname
);
3838 if (st
->ss
== NULL
) {
3839 st
->ss
= &super_imsm
;
3840 st
->minor_version
= 0;
3841 st
->max_devs
= IMSM_MAX_DEVICES
;
3844 /* load migration record */
3845 if (load_imsm_migr_rec(super
, NULL
) == 0) {
3846 /* Check for unsupported migration features */
3847 if (check_mpb_migr_compatibility(super
) != 0) {
3849 Name
": Unsupported migration detected");
3851 fprintf(stderr
, " on %s\n", devname
);
3853 fprintf(stderr
, " (IMSM).\n");
3861 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
3863 if (info
->level
== 1)
3865 return info
->chunk_size
>> 9;
3868 static __u32
info_to_num_data_stripes(mdu_array_info_t
*info
, int num_domains
)
3872 num_stripes
= (info
->size
* 2) / info_to_blocks_per_strip(info
);
3873 num_stripes
/= num_domains
;
3878 static __u32
info_to_blocks_per_member(mdu_array_info_t
*info
)
3880 if (info
->level
== 1)
3881 return info
->size
* 2;
3883 return (info
->size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
3886 static void imsm_update_version_info(struct intel_super
*super
)
3888 /* update the version and attributes */
3889 struct imsm_super
*mpb
= super
->anchor
;
3891 struct imsm_dev
*dev
;
3892 struct imsm_map
*map
;
3895 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3896 dev
= get_imsm_dev(super
, i
);
3897 map
= get_imsm_map(dev
, 0);
3898 if (__le32_to_cpu(dev
->size_high
) > 0)
3899 mpb
->attributes
|= MPB_ATTRIB_2TB
;
3901 /* FIXME detect when an array spans a port multiplier */
3903 mpb
->attributes
|= MPB_ATTRIB_PM
;
3906 if (mpb
->num_raid_devs
> 1 ||
3907 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
3908 version
= MPB_VERSION_ATTRIBS
;
3909 switch (get_imsm_raid_level(map
)) {
3910 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
3911 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
3912 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
3913 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
3916 if (map
->num_members
>= 5)
3917 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
3918 else if (dev
->status
== DEV_CLONE_N_GO
)
3919 version
= MPB_VERSION_CNG
;
3920 else if (get_imsm_raid_level(map
) == 5)
3921 version
= MPB_VERSION_RAID5
;
3922 else if (map
->num_members
>= 3)
3923 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
3924 else if (get_imsm_raid_level(map
) == 1)
3925 version
= MPB_VERSION_RAID1
;
3927 version
= MPB_VERSION_RAID0
;
3929 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
3933 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
3935 struct imsm_super
*mpb
= super
->anchor
;
3936 char *reason
= NULL
;
3939 if (strlen(name
) > MAX_RAID_SERIAL_LEN
)
3940 reason
= "must be 16 characters or less";
3942 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3943 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3945 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
3946 reason
= "already exists";
3951 if (reason
&& !quiet
)
3952 fprintf(stderr
, Name
": imsm volume name %s\n", reason
);
3957 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
3958 unsigned long long size
, char *name
,
3959 char *homehost
, int *uuid
)
3961 /* We are creating a volume inside a pre-existing container.
3962 * so st->sb is already set.
3964 struct intel_super
*super
= st
->sb
;
3965 struct imsm_super
*mpb
= super
->anchor
;
3966 struct intel_dev
*dv
;
3967 struct imsm_dev
*dev
;
3968 struct imsm_vol
*vol
;
3969 struct imsm_map
*map
;
3970 int idx
= mpb
->num_raid_devs
;
3972 unsigned long long array_blocks
;
3973 size_t size_old
, size_new
;
3974 __u32 num_data_stripes
;
3976 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
3977 fprintf(stderr
, Name
": This imsm-container already has the "
3978 "maximum of %d volumes\n", super
->orom
->vpa
);
3982 /* ensure the mpb is large enough for the new data */
3983 size_old
= __le32_to_cpu(mpb
->mpb_size
);
3984 size_new
= disks_to_mpb_size(info
->nr_disks
);
3985 if (size_new
> size_old
) {
3987 size_t size_round
= ROUND_UP(size_new
, 512);
3989 if (posix_memalign(&mpb_new
, 512, size_round
) != 0) {
3990 fprintf(stderr
, Name
": could not allocate new mpb\n");
3993 if (posix_memalign(&super
->migr_rec_buf
, 512, 512) != 0) {
3994 fprintf(stderr
, Name
3995 ": %s could not allocate migr_rec buffer\n",
4001 memcpy(mpb_new
, mpb
, size_old
);
4004 super
->anchor
= mpb_new
;
4005 mpb
->mpb_size
= __cpu_to_le32(size_new
);
4006 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
4008 super
->current_vol
= idx
;
4009 /* when creating the first raid device in this container set num_disks
4010 * to zero, i.e. delete this spare and add raid member devices in
4011 * add_to_super_imsm_volume()
4013 if (super
->current_vol
== 0)
4016 if (!check_name(super
, name
, 0))
4018 dv
= malloc(sizeof(*dv
));
4020 fprintf(stderr
, Name
": failed to allocate device list entry\n");
4023 dev
= calloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
4026 fprintf(stderr
, Name
": could not allocate raid device\n");
4030 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
4031 if (info
->level
== 1)
4032 array_blocks
= info_to_blocks_per_member(info
);
4034 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
4035 info
->layout
, info
->chunk_size
,
4037 /* round array size down to closest MB */
4038 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
4040 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
4041 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
4042 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
4044 vol
->migr_state
= 0;
4045 set_migr_type(dev
, MIGR_INIT
);
4047 vol
->curr_migr_unit
= 0;
4048 map
= get_imsm_map(dev
, 0);
4049 map
->pba_of_lba0
= __cpu_to_le32(super
->create_offset
);
4050 map
->blocks_per_member
= __cpu_to_le32(info_to_blocks_per_member(info
));
4051 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
4052 map
->failed_disk_num
= ~0;
4053 map
->map_state
= info
->level
? IMSM_T_STATE_UNINITIALIZED
:
4054 IMSM_T_STATE_NORMAL
;
4057 if (info
->level
== 1 && info
->raid_disks
> 2) {
4060 fprintf(stderr
, Name
": imsm does not support more than 2 disks"
4061 "in a raid1 volume\n");
4065 map
->raid_level
= info
->level
;
4066 if (info
->level
== 10) {
4067 map
->raid_level
= 1;
4068 map
->num_domains
= info
->raid_disks
/ 2;
4069 } else if (info
->level
== 1)
4070 map
->num_domains
= info
->raid_disks
;
4072 map
->num_domains
= 1;
4074 num_data_stripes
= info_to_num_data_stripes(info
, map
->num_domains
);
4075 map
->num_data_stripes
= __cpu_to_le32(num_data_stripes
);
4077 map
->num_members
= info
->raid_disks
;
4078 for (i
= 0; i
< map
->num_members
; i
++) {
4079 /* initialized in add_to_super */
4080 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
4082 mpb
->num_raid_devs
++;
4085 dv
->index
= super
->current_vol
;
4086 dv
->next
= super
->devlist
;
4087 super
->devlist
= dv
;
4089 imsm_update_version_info(super
);
4094 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
4095 unsigned long long size
, char *name
,
4096 char *homehost
, int *uuid
)
4098 /* This is primarily called by Create when creating a new array.
4099 * We will then get add_to_super called for each component, and then
4100 * write_init_super called to write it out to each device.
4101 * For IMSM, Create can create on fresh devices or on a pre-existing
4103 * To create on a pre-existing array a different method will be called.
4104 * This one is just for fresh drives.
4106 struct intel_super
*super
;
4107 struct imsm_super
*mpb
;
4112 return init_super_imsm_volume(st
, info
, size
, name
, homehost
, uuid
);
4115 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
4119 super
= alloc_super();
4120 if (super
&& posix_memalign(&super
->buf
, 512, mpb_size
) != 0) {
4125 fprintf(stderr
, Name
4126 ": %s could not allocate superblock\n", __func__
);
4129 if (posix_memalign(&super
->migr_rec_buf
, 512, 512) != 0) {
4130 fprintf(stderr
, Name
4131 ": %s could not allocate migr_rec buffer\n", __func__
);
4136 memset(super
->buf
, 0, mpb_size
);
4138 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
4142 /* zeroing superblock */
4146 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
4148 version
= (char *) mpb
->sig
;
4149 strcpy(version
, MPB_SIGNATURE
);
4150 version
+= strlen(MPB_SIGNATURE
);
4151 strcpy(version
, MPB_VERSION_RAID0
);
4157 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
4158 int fd
, char *devname
)
4160 struct intel_super
*super
= st
->sb
;
4161 struct imsm_super
*mpb
= super
->anchor
;
4163 struct imsm_dev
*dev
;
4164 struct imsm_map
*map
;
4167 dev
= get_imsm_dev(super
, super
->current_vol
);
4168 map
= get_imsm_map(dev
, 0);
4170 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
4171 fprintf(stderr
, Name
": %s: Cannot add spare devices to IMSM volume\n",
4177 /* we're doing autolayout so grab the pre-marked (in
4178 * validate_geometry) raid_disk
4180 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4181 if (dl
->raiddisk
== dk
->raid_disk
)
4184 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4185 if (dl
->major
== dk
->major
&&
4186 dl
->minor
== dk
->minor
)
4191 fprintf(stderr
, Name
": %s is not a member of the same container\n", devname
);
4195 /* add a pristine spare to the metadata */
4196 if (dl
->index
< 0) {
4197 dl
->index
= super
->anchor
->num_disks
;
4198 super
->anchor
->num_disks
++;
4200 /* Check the device has not already been added */
4201 slot
= get_imsm_disk_slot(map
, dl
->index
);
4203 (get_imsm_ord_tbl_ent(dev
, slot
, -1) & IMSM_ORD_REBUILD
) == 0) {
4204 fprintf(stderr
, Name
": %s has been included in this array twice\n",
4208 set_imsm_ord_tbl_ent(map
, dk
->number
, dl
->index
);
4209 dl
->disk
.status
= CONFIGURED_DISK
;
4211 /* if we are creating the first raid device update the family number */
4212 if (super
->current_vol
== 0) {
4214 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
4215 struct imsm_disk
*_disk
= __get_imsm_disk(mpb
, dl
->index
);
4217 if (!_dev
|| !_disk
) {
4218 fprintf(stderr
, Name
": BUG mpb setup error\n");
4224 sum
+= __gen_imsm_checksum(mpb
);
4225 mpb
->family_num
= __cpu_to_le32(sum
);
4226 mpb
->orig_family_num
= mpb
->family_num
;
4233 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
4234 int fd
, char *devname
)
4236 struct intel_super
*super
= st
->sb
;
4238 unsigned long long size
;
4243 /* If we are on an RAID enabled platform check that the disk is
4244 * attached to the raid controller.
4245 * We do not need to test disks attachment for container based additions,
4246 * they shall be already tested when container was created/assembled.
4248 rv
= find_intel_hba_capability(fd
, super
, devname
);
4249 /* no orom/efi or non-intel hba of the disk */
4251 dprintf("capability: %p fd: %d ret: %d\n",
4252 super
->orom
, fd
, rv
);
4256 if (super
->current_vol
>= 0)
4257 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
4260 dd
= malloc(sizeof(*dd
));
4263 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
4266 memset(dd
, 0, sizeof(*dd
));
4267 dd
->major
= major(stb
.st_rdev
);
4268 dd
->minor
= minor(stb
.st_rdev
);
4270 dd
->devname
= devname
? strdup(devname
) : NULL
;
4273 dd
->action
= DISK_ADD
;
4274 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
4277 Name
": failed to retrieve scsi serial, aborting\n");
4282 get_dev_size(fd
, NULL
, &size
);
4284 serialcpy(dd
->disk
.serial
, dd
->serial
);
4285 dd
->disk
.total_blocks
= __cpu_to_le32(size
);
4286 dd
->disk
.status
= SPARE_DISK
;
4287 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
4288 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
4290 dd
->disk
.scsi_id
= __cpu_to_le32(0);
4292 if (st
->update_tail
) {
4293 dd
->next
= super
->disk_mgmt_list
;
4294 super
->disk_mgmt_list
= dd
;
4296 dd
->next
= super
->disks
;
4298 super
->updates_pending
++;
4305 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
4307 struct intel_super
*super
= st
->sb
;
4310 /* remove from super works only in mdmon - for communication
4311 * manager - monitor. Check if communication memory buffer
4314 if (!st
->update_tail
) {
4316 Name
": %s shall be used in mdmon context only"
4317 "(line %d).\n", __func__
, __LINE__
);
4320 dd
= malloc(sizeof(*dd
));
4323 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
4326 memset(dd
, 0, sizeof(*dd
));
4327 dd
->major
= dk
->major
;
4328 dd
->minor
= dk
->minor
;
4331 dd
->disk
.status
= SPARE_DISK
;
4332 dd
->action
= DISK_REMOVE
;
4334 dd
->next
= super
->disk_mgmt_list
;
4335 super
->disk_mgmt_list
= dd
;
4341 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
4345 struct imsm_super anchor
;
4346 } spare_record
__attribute__ ((aligned(512)));
4348 /* spare records have their own family number and do not have any defined raid
4351 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
4353 struct imsm_super
*mpb
= super
->anchor
;
4354 struct imsm_super
*spare
= &spare_record
.anchor
;
4358 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
)),
4359 spare
->generation_num
= __cpu_to_le32(1UL),
4360 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
4361 spare
->num_disks
= 1,
4362 spare
->num_raid_devs
= 0,
4363 spare
->cache_size
= mpb
->cache_size
,
4364 spare
->pwr_cycle_count
= __cpu_to_le32(1),
4366 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
4367 MPB_SIGNATURE MPB_VERSION_RAID0
);
4369 for (d
= super
->disks
; d
; d
= d
->next
) {
4373 spare
->disk
[0] = d
->disk
;
4374 sum
= __gen_imsm_checksum(spare
);
4375 spare
->family_num
= __cpu_to_le32(sum
);
4376 spare
->orig_family_num
= 0;
4377 sum
= __gen_imsm_checksum(spare
);
4378 spare
->check_sum
= __cpu_to_le32(sum
);
4380 if (store_imsm_mpb(d
->fd
, spare
)) {
4381 fprintf(stderr
, "%s: failed for device %d:%d %s\n",
4382 __func__
, d
->major
, d
->minor
, strerror(errno
));
4394 static int is_gen_migration(struct imsm_dev
*dev
);
4396 static int write_super_imsm(struct supertype
*st
, int doclose
)
4398 struct intel_super
*super
= st
->sb
;
4399 struct imsm_super
*mpb
= super
->anchor
;
4405 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
4407 int clear_migration_record
= 1;
4409 /* 'generation' is incremented everytime the metadata is written */
4410 generation
= __le32_to_cpu(mpb
->generation_num
);
4412 mpb
->generation_num
= __cpu_to_le32(generation
);
4414 /* fix up cases where previous mdadm releases failed to set
4417 if (mpb
->orig_family_num
== 0)
4418 mpb
->orig_family_num
= mpb
->family_num
;
4420 for (d
= super
->disks
; d
; d
= d
->next
) {
4424 mpb
->disk
[d
->index
] = d
->disk
;
4428 for (d
= super
->missing
; d
; d
= d
->next
) {
4429 mpb
->disk
[d
->index
] = d
->disk
;
4432 mpb
->num_disks
= num_disks
;
4433 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
4435 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4436 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
4437 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
4439 imsm_copy_dev(dev
, dev2
);
4440 mpb_size
+= sizeof_imsm_dev(dev
, 0);
4442 if (is_gen_migration(dev2
))
4443 clear_migration_record
= 0;
4445 mpb_size
+= __le32_to_cpu(mpb
->bbm_log_size
);
4446 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
4448 /* recalculate checksum */
4449 sum
= __gen_imsm_checksum(mpb
);
4450 mpb
->check_sum
= __cpu_to_le32(sum
);
4452 if (clear_migration_record
)
4453 memset(super
->migr_rec_buf
, 0, 512);
4455 /* write the mpb for disks that compose raid devices */
4456 for (d
= super
->disks
; d
; d
= d
->next
) {
4459 if (store_imsm_mpb(d
->fd
, mpb
))
4460 fprintf(stderr
, "%s: failed for device %d:%d %s\n",
4461 __func__
, d
->major
, d
->minor
, strerror(errno
));
4462 if (clear_migration_record
) {
4463 unsigned long long dsize
;
4465 get_dev_size(d
->fd
, NULL
, &dsize
);
4466 if (lseek64(d
->fd
, dsize
- 512, SEEK_SET
) >= 0) {
4467 write(d
->fd
, super
->migr_rec_buf
, 512);
4477 return write_super_imsm_spares(super
, doclose
);
4483 static int create_array(struct supertype
*st
, int dev_idx
)
4486 struct imsm_update_create_array
*u
;
4487 struct intel_super
*super
= st
->sb
;
4488 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
4489 struct imsm_map
*map
= get_imsm_map(dev
, 0);
4490 struct disk_info
*inf
;
4491 struct imsm_disk
*disk
;
4494 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
4495 sizeof(*inf
) * map
->num_members
;
4498 fprintf(stderr
, "%s: failed to allocate update buffer\n",
4503 u
->type
= update_create_array
;
4504 u
->dev_idx
= dev_idx
;
4505 imsm_copy_dev(&u
->dev
, dev
);
4506 inf
= get_disk_info(u
);
4507 for (i
= 0; i
< map
->num_members
; i
++) {
4508 int idx
= get_imsm_disk_idx(dev
, i
, -1);
4510 disk
= get_imsm_disk(super
, idx
);
4511 serialcpy(inf
[i
].serial
, disk
->serial
);
4513 append_metadata_update(st
, u
, len
);
4518 static int mgmt_disk(struct supertype
*st
)
4520 struct intel_super
*super
= st
->sb
;
4522 struct imsm_update_add_remove_disk
*u
;
4524 if (!super
->disk_mgmt_list
)
4530 fprintf(stderr
, "%s: failed to allocate update buffer\n",
4535 u
->type
= update_add_remove_disk
;
4536 append_metadata_update(st
, u
, len
);
4541 static int write_init_super_imsm(struct supertype
*st
)
4543 struct intel_super
*super
= st
->sb
;
4544 int current_vol
= super
->current_vol
;
4546 /* we are done with current_vol reset it to point st at the container */
4547 super
->current_vol
= -1;
4549 if (st
->update_tail
) {
4550 /* queue the recently created array / added disk
4551 * as a metadata update */
4554 /* determine if we are creating a volume or adding a disk */
4555 if (current_vol
< 0) {
4556 /* in the mgmt (add/remove) disk case we are running
4557 * in mdmon context, so don't close fd's
4559 return mgmt_disk(st
);
4561 rv
= create_array(st
, current_vol
);
4566 for (d
= super
->disks
; d
; d
= d
->next
)
4567 Kill(d
->devname
, NULL
, 0, 1, 1);
4568 return write_super_imsm(st
, 1);
4573 static int store_super_imsm(struct supertype
*st
, int fd
)
4575 struct intel_super
*super
= st
->sb
;
4576 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
4582 return store_imsm_mpb(fd
, mpb
);
4588 static int imsm_bbm_log_size(struct imsm_super
*mpb
)
4590 return __le32_to_cpu(mpb
->bbm_log_size
);
4594 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
4595 int layout
, int raiddisks
, int chunk
,
4596 unsigned long long size
, char *dev
,
4597 unsigned long long *freesize
,
4601 unsigned long long ldsize
;
4602 struct intel_super
*super
=NULL
;
4605 if (level
!= LEVEL_CONTAINER
)
4610 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
4613 fprintf(stderr
, Name
": imsm: Cannot open %s: %s\n",
4614 dev
, strerror(errno
));
4617 if (!get_dev_size(fd
, dev
, &ldsize
)) {
4622 /* capabilities retrieve could be possible
4623 * note that there is no fd for the disks in array.
4625 super
= alloc_super();
4628 Name
": malloc of %zu failed.\n",
4634 rv
= find_intel_hba_capability(fd
, super
, verbose
? dev
: NULL
);
4638 fd2devname(fd
, str
);
4639 dprintf("validate_geometry_imsm_container: fd: %d %s orom: %p rv: %d raiddisk: %d\n",
4640 fd
, str
, super
->orom
, rv
, raiddisks
);
4642 /* no orom/efi or non-intel hba of the disk */
4648 if (super
->orom
&& raiddisks
> super
->orom
->tds
) {
4650 fprintf(stderr
, Name
": %d exceeds maximum number of"
4651 " platform supported disks: %d\n",
4652 raiddisks
, super
->orom
->tds
);
4658 *freesize
= avail_size_imsm(st
, ldsize
>> 9);
4664 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
4666 const unsigned long long base_start
= e
[*idx
].start
;
4667 unsigned long long end
= base_start
+ e
[*idx
].size
;
4670 if (base_start
== end
)
4674 for (i
= *idx
; i
< num_extents
; i
++) {
4675 /* extend overlapping extents */
4676 if (e
[i
].start
>= base_start
&&
4677 e
[i
].start
<= end
) {
4680 if (e
[i
].start
+ e
[i
].size
> end
)
4681 end
= e
[i
].start
+ e
[i
].size
;
4682 } else if (e
[i
].start
> end
) {
4688 return end
- base_start
;
4691 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
4693 /* build a composite disk with all known extents and generate a new
4694 * 'maxsize' given the "all disks in an array must share a common start
4695 * offset" constraint
4697 struct extent
*e
= calloc(sum_extents
, sizeof(*e
));
4701 unsigned long long pos
;
4702 unsigned long long start
= 0;
4703 unsigned long long maxsize
;
4704 unsigned long reserve
;
4709 /* coalesce and sort all extents. also, check to see if we need to
4710 * reserve space between member arrays
4713 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
4716 for (i
= 0; i
< dl
->extent_cnt
; i
++)
4719 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
4724 while (i
< sum_extents
) {
4725 e
[j
].start
= e
[i
].start
;
4726 e
[j
].size
= find_size(e
, &i
, sum_extents
);
4728 if (e
[j
-1].size
== 0)
4737 unsigned long long esize
;
4739 esize
= e
[i
].start
- pos
;
4740 if (esize
>= maxsize
) {
4745 pos
= e
[i
].start
+ e
[i
].size
;
4747 } while (e
[i
-1].size
);
4753 /* FIXME assumes volume at offset 0 is the first volume in a
4756 if (start_extent
> 0)
4757 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
4761 if (maxsize
< reserve
)
4764 super
->create_offset
= ~((__u32
) 0);
4765 if (start
+ reserve
> super
->create_offset
)
4766 return 0; /* start overflows create_offset */
4767 super
->create_offset
= start
+ reserve
;
4769 return maxsize
- reserve
;
4772 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
4774 if (level
< 0 || level
== 6 || level
== 4)
4777 /* if we have an orom prevent invalid raid levels */
4780 case 0: return imsm_orom_has_raid0(orom
);
4783 return imsm_orom_has_raid1e(orom
);
4784 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
4785 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
4786 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
4789 return 1; /* not on an Intel RAID platform so anything goes */
4795 #define pr_vrb(fmt, arg...) (void) (verbose && fprintf(stderr, Name fmt, ##arg))
4797 * validate volume parameters with OROM/EFI capabilities
4800 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
4801 int raiddisks
, int *chunk
, int verbose
)
4806 /* validate container capabilities */
4807 if (super
->orom
&& raiddisks
> super
->orom
->tds
) {
4809 fprintf(stderr
, Name
": %d exceeds maximum number of"
4810 " platform supported disks: %d\n",
4811 raiddisks
, super
->orom
->tds
);
4815 /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
4816 if (super
->orom
&& (!is_raid_level_supported(super
->orom
, level
,
4818 pr_vrb(": platform does not support raid%d with %d disk%s\n",
4819 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
4822 if (super
->orom
&& level
!= 1) {
4823 if (chunk
&& (*chunk
== 0 || *chunk
== UnSet
))
4824 *chunk
= imsm_orom_default_chunk(super
->orom
);
4825 else if (chunk
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
4826 pr_vrb(": platform does not support a chunk size of: "
4831 if (layout
!= imsm_level_to_layout(level
)) {
4833 pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
4834 else if (level
== 10)
4835 pr_vrb(": imsm raid 10 only supports the n2 layout\n");
4837 pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
4844 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
4845 * FIX ME add ahci details
4847 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
4848 int layout
, int raiddisks
, int *chunk
,
4849 unsigned long long size
, char *dev
,
4850 unsigned long long *freesize
,
4854 struct intel_super
*super
= st
->sb
;
4855 struct imsm_super
*mpb
= super
->anchor
;
4857 unsigned long long pos
= 0;
4858 unsigned long long maxsize
;
4862 /* We must have the container info already read in. */
4866 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, verbose
)) {
4867 fprintf(stderr
, Name
": RAID gemetry validation failed. "
4868 "Cannot proceed with the action(s).\n");
4872 /* General test: make sure there is space for
4873 * 'raiddisks' device extents of size 'size' at a given
4876 unsigned long long minsize
= size
;
4877 unsigned long long start_offset
= MaxSector
;
4880 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
4881 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
4886 e
= get_extents(super
, dl
);
4889 unsigned long long esize
;
4890 esize
= e
[i
].start
- pos
;
4891 if (esize
>= minsize
)
4893 if (found
&& start_offset
== MaxSector
) {
4896 } else if (found
&& pos
!= start_offset
) {
4900 pos
= e
[i
].start
+ e
[i
].size
;
4902 } while (e
[i
-1].size
);
4907 if (dcnt
< raiddisks
) {
4909 fprintf(stderr
, Name
": imsm: Not enough "
4910 "devices with space for this array "
4918 /* This device must be a member of the set */
4919 if (stat(dev
, &stb
) < 0)
4921 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
4923 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
4924 if (dl
->major
== (int)major(stb
.st_rdev
) &&
4925 dl
->minor
== (int)minor(stb
.st_rdev
))
4930 fprintf(stderr
, Name
": %s is not in the "
4931 "same imsm set\n", dev
);
4933 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
4934 /* If a volume is present then the current creation attempt
4935 * cannot incorporate new spares because the orom may not
4936 * understand this configuration (all member disks must be
4937 * members of each array in the container).
4939 fprintf(stderr
, Name
": %s is a spare and a volume"
4940 " is already defined for this container\n", dev
);
4941 fprintf(stderr
, Name
": The option-rom requires all member"
4942 " disks to be a member of all volumes\n");
4946 /* retrieve the largest free space block */
4947 e
= get_extents(super
, dl
);
4952 unsigned long long esize
;
4954 esize
= e
[i
].start
- pos
;
4955 if (esize
>= maxsize
)
4957 pos
= e
[i
].start
+ e
[i
].size
;
4959 } while (e
[i
-1].size
);
4964 fprintf(stderr
, Name
": unable to determine free space for: %s\n",
4968 if (maxsize
< size
) {
4970 fprintf(stderr
, Name
": %s not enough space (%llu < %llu)\n",
4971 dev
, maxsize
, size
);
4975 /* count total number of extents for merge */
4977 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4979 i
+= dl
->extent_cnt
;
4981 maxsize
= merge_extents(super
, i
);
4982 if (maxsize
< size
|| maxsize
== 0) {
4984 fprintf(stderr
, Name
": not enough space after merge (%llu < %llu)\n",
4989 *freesize
= maxsize
;
4994 static int reserve_space(struct supertype
*st
, int raiddisks
,
4995 unsigned long long size
, int chunk
,
4996 unsigned long long *freesize
)
4998 struct intel_super
*super
= st
->sb
;
4999 struct imsm_super
*mpb
= super
->anchor
;
5004 unsigned long long maxsize
;
5005 unsigned long long minsize
;
5009 /* find the largest common start free region of the possible disks */
5013 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5019 /* don't activate new spares if we are orom constrained
5020 * and there is already a volume active in the container
5022 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
5025 e
= get_extents(super
, dl
);
5028 for (i
= 1; e
[i
-1].size
; i
++)
5036 maxsize
= merge_extents(super
, extent_cnt
);
5040 minsize
= chunk
* 2;
5042 if (cnt
< raiddisks
||
5043 (super
->orom
&& used
&& used
!= raiddisks
) ||
5044 maxsize
< minsize
||
5046 fprintf(stderr
, Name
": not enough devices with space to create array.\n");
5047 return 0; /* No enough free spaces large enough */
5059 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5061 dl
->raiddisk
= cnt
++;
5068 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
5069 int raiddisks
, int *chunk
, unsigned long long size
,
5070 char *dev
, unsigned long long *freesize
,
5078 * if given unused devices create a container
5079 * if given given devices in a container create a member volume
5081 if (level
== LEVEL_CONTAINER
) {
5082 /* Must be a fresh device to add to a container */
5083 return validate_geometry_imsm_container(st
, level
, layout
,
5085 chunk
?*chunk
:0, size
,
5091 if (st
->sb
&& freesize
) {
5092 /* we are being asked to automatically layout a
5093 * new volume based on the current contents of
5094 * the container. If the the parameters can be
5095 * satisfied reserve_space will record the disks,
5096 * start offset, and size of the volume to be
5097 * created. add_to_super and getinfo_super
5098 * detect when autolayout is in progress.
5100 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
5104 return reserve_space(st
, raiddisks
, size
,
5105 chunk
?*chunk
:0, freesize
);
5110 /* creating in a given container */
5111 return validate_geometry_imsm_volume(st
, level
, layout
,
5112 raiddisks
, chunk
, size
,
5113 dev
, freesize
, verbose
);
5116 /* This device needs to be a device in an 'imsm' container */
5117 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
5121 Name
": Cannot create this array on device %s\n",
5126 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
5128 fprintf(stderr
, Name
": Cannot open %s: %s\n",
5129 dev
, strerror(errno
));
5132 /* Well, it is in use by someone, maybe an 'imsm' container. */
5133 cfd
= open_container(fd
);
5137 fprintf(stderr
, Name
": Cannot use %s: It is busy\n",
5141 sra
= sysfs_read(cfd
, 0, GET_VERSION
);
5142 if (sra
&& sra
->array
.major_version
== -1 &&
5143 strcmp(sra
->text_version
, "imsm") == 0)
5147 /* This is a member of a imsm container. Load the container
5148 * and try to create a volume
5150 struct intel_super
*super
;
5152 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
) == 0) {
5154 st
->container_dev
= fd2devnum(cfd
);
5156 return validate_geometry_imsm_volume(st
, level
, layout
,
5164 fprintf(stderr
, Name
": failed container membership check\n");
5170 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
5172 struct intel_super
*super
= st
->sb
;
5174 if (level
&& *level
== UnSet
)
5175 *level
= LEVEL_CONTAINER
;
5177 if (level
&& layout
&& *layout
== UnSet
)
5178 *layout
= imsm_level_to_layout(*level
);
5180 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0) &&
5181 super
&& super
->orom
)
5182 *chunk
= imsm_orom_default_chunk(super
->orom
);
5185 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
5187 static int kill_subarray_imsm(struct supertype
*st
)
5189 /* remove the subarray currently referenced by ->current_vol */
5191 struct intel_dev
**dp
;
5192 struct intel_super
*super
= st
->sb
;
5193 __u8 current_vol
= super
->current_vol
;
5194 struct imsm_super
*mpb
= super
->anchor
;
5196 if (super
->current_vol
< 0)
5198 super
->current_vol
= -1; /* invalidate subarray cursor */
5200 /* block deletions that would change the uuid of active subarrays
5202 * FIXME when immutable ids are available, but note that we'll
5203 * also need to fixup the invalidated/active subarray indexes in
5206 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5209 if (i
< current_vol
)
5211 sprintf(subarray
, "%u", i
);
5212 if (is_subarray_active(subarray
, st
->devname
)) {
5214 Name
": deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
5221 if (st
->update_tail
) {
5222 struct imsm_update_kill_array
*u
= malloc(sizeof(*u
));
5226 u
->type
= update_kill_array
;
5227 u
->dev_idx
= current_vol
;
5228 append_metadata_update(st
, u
, sizeof(*u
));
5233 for (dp
= &super
->devlist
; *dp
;)
5234 if ((*dp
)->index
== current_vol
) {
5237 handle_missing(super
, (*dp
)->dev
);
5238 if ((*dp
)->index
> current_vol
)
5243 /* no more raid devices, all active components are now spares,
5244 * but of course failed are still failed
5246 if (--mpb
->num_raid_devs
== 0) {
5249 for (d
= super
->disks
; d
; d
= d
->next
)
5250 if (d
->index
> -2) {
5252 d
->disk
.status
= SPARE_DISK
;
5256 super
->updates_pending
++;
5261 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
5262 char *update
, struct mddev_ident
*ident
)
5264 /* update the subarray currently referenced by ->current_vol */
5265 struct intel_super
*super
= st
->sb
;
5266 struct imsm_super
*mpb
= super
->anchor
;
5268 if (strcmp(update
, "name") == 0) {
5269 char *name
= ident
->name
;
5273 if (is_subarray_active(subarray
, st
->devname
)) {
5275 Name
": Unable to update name of active subarray\n");
5279 if (!check_name(super
, name
, 0))
5282 vol
= strtoul(subarray
, &ep
, 10);
5283 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
5286 if (st
->update_tail
) {
5287 struct imsm_update_rename_array
*u
= malloc(sizeof(*u
));
5291 u
->type
= update_rename_array
;
5293 snprintf((char *) u
->name
, MAX_RAID_SERIAL_LEN
, "%s", name
);
5294 append_metadata_update(st
, u
, sizeof(*u
));
5296 struct imsm_dev
*dev
;
5299 dev
= get_imsm_dev(super
, vol
);
5300 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
5301 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5302 dev
= get_imsm_dev(super
, i
);
5303 handle_missing(super
, dev
);
5305 super
->updates_pending
++;
5313 static int is_gen_migration(struct imsm_dev
*dev
)
5318 if (!dev
->vol
.migr_state
)
5321 if (migr_type(dev
) == MIGR_GEN_MIGR
)
5326 #endif /* MDASSEMBLE */
5328 static int is_rebuilding(struct imsm_dev
*dev
)
5330 struct imsm_map
*migr_map
;
5332 if (!dev
->vol
.migr_state
)
5335 if (migr_type(dev
) != MIGR_REBUILD
)
5338 migr_map
= get_imsm_map(dev
, 1);
5340 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
5346 static void update_recovery_start(struct intel_super
*super
,
5347 struct imsm_dev
*dev
,
5348 struct mdinfo
*array
)
5350 struct mdinfo
*rebuild
= NULL
;
5354 if (!is_rebuilding(dev
))
5357 /* Find the rebuild target, but punt on the dual rebuild case */
5358 for (d
= array
->devs
; d
; d
= d
->next
)
5359 if (d
->recovery_start
== 0) {
5366 /* (?) none of the disks are marked with
5367 * IMSM_ORD_REBUILD, so assume they are missing and the
5368 * disk_ord_tbl was not correctly updated
5370 dprintf("%s: failed to locate out-of-sync disk\n", __func__
);
5374 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
5375 rebuild
->recovery_start
= units
* blocks_per_migr_unit(super
, dev
);
5378 static int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
);
5380 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
5382 /* Given a container loaded by load_super_imsm_all,
5383 * extract information about all the arrays into
5385 * If 'subarray' is given, just extract info about that array.
5387 * For each imsm_dev create an mdinfo, fill it in,
5388 * then look for matching devices in super->disks
5389 * and create appropriate device mdinfo.
5391 struct intel_super
*super
= st
->sb
;
5392 struct imsm_super
*mpb
= super
->anchor
;
5393 struct mdinfo
*rest
= NULL
;
5397 int spare_disks
= 0;
5399 /* check for bad blocks */
5400 if (imsm_bbm_log_size(super
->anchor
))
5403 /* count spare devices, not used in maps
5405 for (d
= super
->disks
; d
; d
= d
->next
)
5409 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5410 struct imsm_dev
*dev
;
5411 struct imsm_map
*map
;
5412 struct imsm_map
*map2
;
5413 struct mdinfo
*this;
5418 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
5421 dev
= get_imsm_dev(super
, i
);
5422 map
= get_imsm_map(dev
, 0);
5423 map2
= get_imsm_map(dev
, 1);
5425 /* do not publish arrays that are in the middle of an
5426 * unsupported migration
5428 if (dev
->vol
.migr_state
&&
5429 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
5430 fprintf(stderr
, Name
": cannot assemble volume '%.16s':"
5431 " unsupported migration in progress\n",
5435 /* do not publish arrays that are not support by controller's
5439 chunk
= __le16_to_cpu(map
->blocks_per_strip
) >> 1;
5441 if (!validate_geometry_imsm_orom(super
,
5442 get_imsm_raid_level(map
), /* RAID level */
5443 imsm_level_to_layout(get_imsm_raid_level(map
)),
5444 map
->num_members
, /* raid disks */
5447 fprintf(stderr
, Name
": RAID gemetry validation failed. "
5448 "Cannot proceed with the action(s).\n");
5451 #endif /* MDASSEMBLE */
5452 this = malloc(sizeof(*this));
5454 fprintf(stderr
, Name
": failed to allocate %zu bytes\n",
5459 super
->current_vol
= i
;
5460 getinfo_super_imsm_volume(st
, this, NULL
);
5462 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
5463 unsigned long long recovery_start
;
5464 struct mdinfo
*info_d
;
5471 idx
= get_imsm_disk_idx(dev
, slot
, 0);
5472 ord
= get_imsm_ord_tbl_ent(dev
, slot
, -1);
5473 for (d
= super
->disks
; d
; d
= d
->next
)
5474 if (d
->index
== idx
)
5477 recovery_start
= MaxSector
;
5480 if (d
&& is_failed(&d
->disk
))
5482 if (ord
& IMSM_ORD_REBUILD
)
5486 * if we skip some disks the array will be assmebled degraded;
5487 * reset resync start to avoid a dirty-degraded
5488 * situation when performing the intial sync
5490 * FIXME handle dirty degraded
5492 if ((skip
|| recovery_start
== 0) && !dev
->vol
.dirty
)
5493 this->resync_start
= MaxSector
;
5497 info_d
= calloc(1, sizeof(*info_d
));
5499 fprintf(stderr
, Name
": failed to allocate disk"
5500 " for volume %.16s\n", dev
->volume
);
5501 info_d
= this->devs
;
5503 struct mdinfo
*d
= info_d
->next
;
5512 info_d
->next
= this->devs
;
5513 this->devs
= info_d
;
5515 info_d
->disk
.number
= d
->index
;
5516 info_d
->disk
.major
= d
->major
;
5517 info_d
->disk
.minor
= d
->minor
;
5518 info_d
->disk
.raid_disk
= slot
;
5519 info_d
->recovery_start
= recovery_start
;
5521 if (slot
< map2
->num_members
)
5522 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
5524 this->array
.spare_disks
++;
5526 if (slot
< map
->num_members
)
5527 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
5529 this->array
.spare_disks
++;
5531 if (info_d
->recovery_start
== MaxSector
)
5532 this->array
.working_disks
++;
5534 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
5535 info_d
->data_offset
= __le32_to_cpu(map
->pba_of_lba0
);
5536 info_d
->component_size
= __le32_to_cpu(map
->blocks_per_member
);
5538 /* now that the disk list is up-to-date fixup recovery_start */
5539 update_recovery_start(super
, dev
, this);
5540 this->array
.spare_disks
+= spare_disks
;
5542 /* check for reshape */
5543 if (this->reshape_active
== 1)
5544 recover_backup_imsm(st
, this);
5549 /* if array has bad blocks, set suitable bit in array status */
5551 rest
->array
.state
|= (1<<MD_SB_BBM_ERRORS
);
5557 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
, int failed
)
5559 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5562 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
5563 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
5565 switch (get_imsm_raid_level(map
)) {
5567 return IMSM_T_STATE_FAILED
;
5570 if (failed
< map
->num_members
)
5571 return IMSM_T_STATE_DEGRADED
;
5573 return IMSM_T_STATE_FAILED
;
5578 * check to see if any mirrors have failed, otherwise we
5579 * are degraded. Even numbered slots are mirrored on
5583 /* gcc -Os complains that this is unused */
5584 int insync
= insync
;
5586 for (i
= 0; i
< map
->num_members
; i
++) {
5587 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, -1);
5588 int idx
= ord_to_idx(ord
);
5589 struct imsm_disk
*disk
;
5591 /* reset the potential in-sync count on even-numbered
5592 * slots. num_copies is always 2 for imsm raid10
5597 disk
= get_imsm_disk(super
, idx
);
5598 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
5601 /* no in-sync disks left in this mirror the
5605 return IMSM_T_STATE_FAILED
;
5608 return IMSM_T_STATE_DEGRADED
;
5612 return IMSM_T_STATE_DEGRADED
;
5614 return IMSM_T_STATE_FAILED
;
5620 return map
->map_state
;
5623 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
)
5627 struct imsm_disk
*disk
;
5628 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5629 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
);
5633 /* at the beginning of migration we set IMSM_ORD_REBUILD on
5634 * disks that are being rebuilt. New failures are recorded to
5635 * map[0]. So we look through all the disks we started with and
5636 * see if any failures are still present, or if any new ones
5639 * FIXME add support for online capacity expansion and
5640 * raid-level-migration
5642 for (i
= 0; i
< prev
->num_members
; i
++) {
5643 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
5644 ord
|= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
5645 idx
= ord_to_idx(ord
);
5647 disk
= get_imsm_disk(super
, idx
);
5648 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
5656 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
5659 struct intel_super
*super
= c
->sb
;
5660 struct imsm_super
*mpb
= super
->anchor
;
5662 if (atoi(inst
) >= mpb
->num_raid_devs
) {
5663 fprintf(stderr
, "%s: subarry index %d, out of range\n",
5664 __func__
, atoi(inst
));
5668 dprintf("imsm: open_new %s\n", inst
);
5669 a
->info
.container_member
= atoi(inst
);
5673 static int is_resyncing(struct imsm_dev
*dev
)
5675 struct imsm_map
*migr_map
;
5677 if (!dev
->vol
.migr_state
)
5680 if (migr_type(dev
) == MIGR_INIT
||
5681 migr_type(dev
) == MIGR_REPAIR
)
5684 if (migr_type(dev
) == MIGR_GEN_MIGR
)
5687 migr_map
= get_imsm_map(dev
, 1);
5689 if ((migr_map
->map_state
== IMSM_T_STATE_NORMAL
) &&
5690 (dev
->vol
.migr_type
!= MIGR_GEN_MIGR
))
5696 /* return true if we recorded new information */
5697 static int mark_failure(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
5701 struct imsm_map
*map
;
5703 /* new failures are always set in map[0] */
5704 map
= get_imsm_map(dev
, 0);
5706 slot
= get_imsm_disk_slot(map
, idx
);
5710 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
5711 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
5714 disk
->status
|= FAILED_DISK
;
5715 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
5716 if (map
->failed_disk_num
== 0xff)
5717 map
->failed_disk_num
= slot
;
5721 static void mark_missing(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
5723 mark_failure(dev
, disk
, idx
);
5725 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
5728 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
5729 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
5732 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
5738 if (!super
->missing
)
5740 failed
= imsm_count_failed(super
, dev
);
5741 map_state
= imsm_check_degraded(super
, dev
, failed
);
5743 dprintf("imsm: mark missing\n");
5744 end_migration(dev
, map_state
);
5745 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
5746 mark_missing(dev
, &dl
->disk
, dl
->index
);
5747 super
->updates_pending
++;
5750 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
)
5752 int used_disks
= imsm_num_data_members(dev
, 0);
5753 unsigned long long array_blocks
;
5754 struct imsm_map
*map
;
5756 if (used_disks
== 0) {
5757 /* when problems occures
5758 * return current array_blocks value
5760 array_blocks
= __le32_to_cpu(dev
->size_high
);
5761 array_blocks
= array_blocks
<< 32;
5762 array_blocks
+= __le32_to_cpu(dev
->size_low
);
5764 return array_blocks
;
5767 /* set array size in metadata
5769 map
= get_imsm_map(dev
, 0);
5770 array_blocks
= map
->blocks_per_member
* used_disks
;
5772 /* round array size down to closest MB
5774 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
5775 dev
->size_low
= __cpu_to_le32((__u32
)array_blocks
);
5776 dev
->size_high
= __cpu_to_le32((__u32
)(array_blocks
>> 32));
5778 return array_blocks
;
5781 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
5783 static void imsm_progress_container_reshape(struct intel_super
*super
)
5785 /* if no device has a migr_state, but some device has a
5786 * different number of members than the previous device, start
5787 * changing the number of devices in this device to match
5790 struct imsm_super
*mpb
= super
->anchor
;
5791 int prev_disks
= -1;
5795 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5796 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
5797 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5798 struct imsm_map
*map2
;
5799 int prev_num_members
;
5801 if (dev
->vol
.migr_state
)
5804 if (prev_disks
== -1)
5805 prev_disks
= map
->num_members
;
5806 if (prev_disks
== map
->num_members
)
5809 /* OK, this array needs to enter reshape mode.
5810 * i.e it needs a migr_state
5813 copy_map_size
= sizeof_imsm_map(map
);
5814 prev_num_members
= map
->num_members
;
5815 map
->num_members
= prev_disks
;
5816 dev
->vol
.migr_state
= 1;
5817 dev
->vol
.curr_migr_unit
= 0;
5818 dev
->vol
.migr_type
= MIGR_GEN_MIGR
;
5819 for (i
= prev_num_members
;
5820 i
< map
->num_members
; i
++)
5821 set_imsm_ord_tbl_ent(map
, i
, i
);
5822 map2
= get_imsm_map(dev
, 1);
5823 /* Copy the current map */
5824 memcpy(map2
, map
, copy_map_size
);
5825 map2
->num_members
= prev_num_members
;
5827 imsm_set_array_size(dev
);
5828 super
->updates_pending
++;
5832 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
5833 * states are handled in imsm_set_disk() with one exception, when a
5834 * resync is stopped due to a new failure this routine will set the
5835 * 'degraded' state for the array.
5837 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
5839 int inst
= a
->info
.container_member
;
5840 struct intel_super
*super
= a
->container
->sb
;
5841 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
5842 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5843 int failed
= imsm_count_failed(super
, dev
);
5844 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
);
5845 __u32 blocks_per_unit
;
5847 if (dev
->vol
.migr_state
&&
5848 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
5849 /* array state change is blocked due to reshape action
5851 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
5852 * - finish the reshape (if last_checkpoint is big and action != reshape)
5853 * - update curr_migr_unit
5855 if (a
->curr_action
== reshape
) {
5856 /* still reshaping, maybe update curr_migr_unit */
5857 goto mark_checkpoint
;
5859 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
5860 /* for some reason we aborted the reshape.
5862 * disable automatic metadata rollback
5863 * user action is required to recover process
5866 struct imsm_map
*map2
= get_imsm_map(dev
, 1);
5867 dev
->vol
.migr_state
= 0;
5868 dev
->vol
.migr_type
= 0;
5869 dev
->vol
.curr_migr_unit
= 0;
5870 memcpy(map
, map2
, sizeof_imsm_map(map2
));
5871 super
->updates_pending
++;
5874 if (a
->last_checkpoint
>= a
->info
.component_size
) {
5875 unsigned long long array_blocks
;
5879 used_disks
= imsm_num_data_members(dev
, 0);
5880 if (used_disks
> 0) {
5882 map
->blocks_per_member
*
5884 /* round array size down to closest MB
5886 array_blocks
= (array_blocks
5887 >> SECT_PER_MB_SHIFT
)
5888 << SECT_PER_MB_SHIFT
;
5889 a
->info
.custom_array_size
= array_blocks
;
5890 /* encourage manager to update array
5894 a
->check_reshape
= 1;
5896 /* finalize online capacity expansion/reshape */
5897 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
5899 mdi
->disk
.raid_disk
,
5902 imsm_progress_container_reshape(super
);
5907 /* before we activate this array handle any missing disks */
5908 if (consistent
== 2)
5909 handle_missing(super
, dev
);
5911 if (consistent
== 2 &&
5912 (!is_resync_complete(&a
->info
) ||
5913 map_state
!= IMSM_T_STATE_NORMAL
||
5914 dev
->vol
.migr_state
))
5917 if (is_resync_complete(&a
->info
)) {
5918 /* complete intialization / resync,
5919 * recovery and interrupted recovery is completed in
5922 if (is_resyncing(dev
)) {
5923 dprintf("imsm: mark resync done\n");
5924 end_migration(dev
, map_state
);
5925 super
->updates_pending
++;
5926 a
->last_checkpoint
= 0;
5928 } else if (!is_resyncing(dev
) && !failed
) {
5929 /* mark the start of the init process if nothing is failed */
5930 dprintf("imsm: mark resync start\n");
5931 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
5932 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
5934 migrate(dev
, super
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
5935 super
->updates_pending
++;
5939 /* skip checkpointing for general migration,
5940 * it is controlled in mdadm
5942 if (is_gen_migration(dev
))
5943 goto skip_mark_checkpoint
;
5945 /* check if we can update curr_migr_unit from resync_start, recovery_start */
5946 blocks_per_unit
= blocks_per_migr_unit(super
, dev
);
5947 if (blocks_per_unit
) {
5951 units
= a
->last_checkpoint
/ blocks_per_unit
;
5954 /* check that we did not overflow 32-bits, and that
5955 * curr_migr_unit needs updating
5957 if (units32
== units
&&
5959 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
5960 dprintf("imsm: mark checkpoint (%u)\n", units32
);
5961 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
5962 super
->updates_pending
++;
5966 skip_mark_checkpoint
:
5967 /* mark dirty / clean */
5968 if (dev
->vol
.dirty
!= !consistent
) {
5969 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
5974 super
->updates_pending
++;
5980 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
5982 int inst
= a
->info
.container_member
;
5983 struct intel_super
*super
= a
->container
->sb
;
5984 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
5985 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5986 struct imsm_disk
*disk
;
5991 if (n
> map
->num_members
)
5992 fprintf(stderr
, "imsm: set_disk %d out of range 0..%d\n",
5993 n
, map
->num_members
- 1);
5998 dprintf("imsm: set_disk %d:%x\n", n
, state
);
6000 ord
= get_imsm_ord_tbl_ent(dev
, n
, -1);
6001 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
6003 /* check for new failures */
6004 if (state
& DS_FAULTY
) {
6005 if (mark_failure(dev
, disk
, ord_to_idx(ord
)))
6006 super
->updates_pending
++;
6009 /* check if in_sync */
6010 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
6011 struct imsm_map
*migr_map
= get_imsm_map(dev
, 1);
6013 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
6014 super
->updates_pending
++;
6017 failed
= imsm_count_failed(super
, dev
);
6018 map_state
= imsm_check_degraded(super
, dev
, failed
);
6020 /* check if recovery complete, newly degraded, or failed */
6021 if (map_state
== IMSM_T_STATE_NORMAL
&& is_rebuilding(dev
)) {
6022 end_migration(dev
, map_state
);
6023 map
= get_imsm_map(dev
, 0);
6024 map
->failed_disk_num
= ~0;
6025 super
->updates_pending
++;
6026 a
->last_checkpoint
= 0;
6027 } else if (map_state
== IMSM_T_STATE_DEGRADED
&&
6028 map
->map_state
!= map_state
&&
6029 !dev
->vol
.migr_state
) {
6030 dprintf("imsm: mark degraded\n");
6031 map
->map_state
= map_state
;
6032 super
->updates_pending
++;
6033 a
->last_checkpoint
= 0;
6034 } else if (map_state
== IMSM_T_STATE_FAILED
&&
6035 map
->map_state
!= map_state
) {
6036 dprintf("imsm: mark failed\n");
6037 end_migration(dev
, map_state
);
6038 super
->updates_pending
++;
6039 a
->last_checkpoint
= 0;
6040 } else if (is_gen_migration(dev
)) {
6041 dprintf("imsm: Detected General Migration in state: ");
6042 if (map_state
== IMSM_T_STATE_NORMAL
) {
6043 end_migration(dev
, map_state
);
6044 map
= get_imsm_map(dev
, 0);
6045 map
->failed_disk_num
= ~0;
6046 dprintf("normal\n");
6048 if (map_state
== IMSM_T_STATE_DEGRADED
) {
6049 printf("degraded\n");
6050 end_migration(dev
, map_state
);
6052 dprintf("failed\n");
6054 map
->map_state
= map_state
;
6056 super
->updates_pending
++;
6060 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
6063 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
6064 unsigned long long dsize
;
6065 unsigned long long sectors
;
6067 get_dev_size(fd
, NULL
, &dsize
);
6069 if (mpb_size
> 512) {
6070 /* -1 to account for anchor */
6071 sectors
= mpb_sectors(mpb
) - 1;
6073 /* write the extended mpb to the sectors preceeding the anchor */
6074 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0)
6077 if ((unsigned long long)write(fd
, buf
+ 512, 512 * sectors
)
6082 /* first block is stored on second to last sector of the disk */
6083 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0)
6086 if (write(fd
, buf
, 512) != 512)
6092 static void imsm_sync_metadata(struct supertype
*container
)
6094 struct intel_super
*super
= container
->sb
;
6096 dprintf("sync metadata: %d\n", super
->updates_pending
);
6097 if (!super
->updates_pending
)
6100 write_super_imsm(container
, 0);
6102 super
->updates_pending
= 0;
6105 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
6107 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
6108 int i
= get_imsm_disk_idx(dev
, idx
, -1);
6111 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6115 if (dl
&& is_failed(&dl
->disk
))
6119 dprintf("%s: found %x:%x\n", __func__
, dl
->major
, dl
->minor
);
6124 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
6125 struct active_array
*a
, int activate_new
,
6126 struct mdinfo
*additional_test_list
)
6128 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
6129 int idx
= get_imsm_disk_idx(dev
, slot
, -1);
6130 struct imsm_super
*mpb
= super
->anchor
;
6131 struct imsm_map
*map
;
6132 unsigned long long pos
;
6137 __u32 array_start
= 0;
6138 __u32 array_end
= 0;
6140 struct mdinfo
*test_list
;
6142 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6143 /* If in this array, skip */
6144 for (d
= a
->info
.devs
; d
; d
= d
->next
)
6145 if (d
->state_fd
>= 0 &&
6146 d
->disk
.major
== dl
->major
&&
6147 d
->disk
.minor
== dl
->minor
) {
6148 dprintf("%x:%x already in array\n",
6149 dl
->major
, dl
->minor
);
6154 test_list
= additional_test_list
;
6156 if (test_list
->disk
.major
== dl
->major
&&
6157 test_list
->disk
.minor
== dl
->minor
) {
6158 dprintf("%x:%x already in additional test list\n",
6159 dl
->major
, dl
->minor
);
6162 test_list
= test_list
->next
;
6167 /* skip in use or failed drives */
6168 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
6170 dprintf("%x:%x status (failed: %d index: %d)\n",
6171 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
6175 /* skip pure spares when we are looking for partially
6176 * assimilated drives
6178 if (dl
->index
== -1 && !activate_new
)
6181 /* Does this unused device have the requisite free space?
6182 * It needs to be able to cover all member volumes
6184 ex
= get_extents(super
, dl
);
6186 dprintf("cannot get extents\n");
6189 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6190 dev
= get_imsm_dev(super
, i
);
6191 map
= get_imsm_map(dev
, 0);
6193 /* check if this disk is already a member of
6196 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
6202 array_start
= __le32_to_cpu(map
->pba_of_lba0
);
6203 array_end
= array_start
+
6204 __le32_to_cpu(map
->blocks_per_member
) - 1;
6207 /* check that we can start at pba_of_lba0 with
6208 * blocks_per_member of space
6210 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
6214 pos
= ex
[j
].start
+ ex
[j
].size
;
6216 } while (ex
[j
-1].size
);
6223 if (i
< mpb
->num_raid_devs
) {
6224 dprintf("%x:%x does not have %u to %u available\n",
6225 dl
->major
, dl
->minor
, array_start
, array_end
);
6236 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
6238 struct imsm_dev
*dev2
;
6239 struct imsm_map
*map
;
6245 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
6247 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
);
6248 if (state
== IMSM_T_STATE_FAILED
) {
6249 map
= get_imsm_map(dev2
, 0);
6252 for (slot
= 0; slot
< map
->num_members
; slot
++) {
6254 * Check if failed disks are deleted from intel
6255 * disk list or are marked to be deleted
6257 idx
= get_imsm_disk_idx(dev2
, slot
, -1);
6258 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
6260 * Do not rebuild the array if failed disks
6261 * from failed sub-array are not removed from
6265 is_failed(&idisk
->disk
) &&
6266 (idisk
->action
!= DISK_REMOVE
))
6274 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
6275 struct metadata_update
**updates
)
6278 * Find a device with unused free space and use it to replace a
6279 * failed/vacant region in an array. We replace failed regions one a
6280 * array at a time. The result is that a new spare disk will be added
6281 * to the first failed array and after the monitor has finished
6282 * propagating failures the remainder will be consumed.
6284 * FIXME add a capability for mdmon to request spares from another
6288 struct intel_super
*super
= a
->container
->sb
;
6289 int inst
= a
->info
.container_member
;
6290 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
6291 struct imsm_map
*map
= get_imsm_map(dev
, 0);
6292 int failed
= a
->info
.array
.raid_disks
;
6293 struct mdinfo
*rv
= NULL
;
6296 struct metadata_update
*mu
;
6298 struct imsm_update_activate_spare
*u
;
6303 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
6304 if ((d
->curr_state
& DS_FAULTY
) &&
6306 /* wait for Removal to happen */
6308 if (d
->state_fd
>= 0)
6312 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
6313 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
6315 if (dev
->vol
.migr_state
&&
6316 dev
->vol
.migr_type
== MIGR_GEN_MIGR
)
6317 /* No repair during migration */
6320 if (a
->info
.array
.level
== 4)
6321 /* No repair for takeovered array
6322 * imsm doesn't support raid4
6326 if (imsm_check_degraded(super
, dev
, failed
) != IMSM_T_STATE_DEGRADED
)
6330 * If there are any failed disks check state of the other volume.
6331 * Block rebuild if the another one is failed until failed disks
6332 * are removed from container.
6335 dprintf("found failed disks in %s, check if there another"
6336 "failed sub-array.\n",
6338 /* check if states of the other volumes allow for rebuild */
6339 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
6341 allowed
= imsm_rebuild_allowed(a
->container
,
6349 /* For each slot, if it is not working, find a spare */
6350 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
6351 for (d
= a
->info
.devs
; d
; d
= d
->next
)
6352 if (d
->disk
.raid_disk
== i
)
6354 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
6355 if (d
&& (d
->state_fd
>= 0))
6359 * OK, this device needs recovery. Try to re-add the
6360 * previous occupant of this slot, if this fails see if
6361 * we can continue the assimilation of a spare that was
6362 * partially assimilated, finally try to activate a new
6365 dl
= imsm_readd(super
, i
, a
);
6367 dl
= imsm_add_spare(super
, i
, a
, 0, NULL
);
6369 dl
= imsm_add_spare(super
, i
, a
, 1, NULL
);
6373 /* found a usable disk with enough space */
6374 di
= malloc(sizeof(*di
));
6377 memset(di
, 0, sizeof(*di
));
6379 /* dl->index will be -1 in the case we are activating a
6380 * pristine spare. imsm_process_update() will create a
6381 * new index in this case. Once a disk is found to be
6382 * failed in all member arrays it is kicked from the
6385 di
->disk
.number
= dl
->index
;
6387 /* (ab)use di->devs to store a pointer to the device
6390 di
->devs
= (struct mdinfo
*) dl
;
6392 di
->disk
.raid_disk
= i
;
6393 di
->disk
.major
= dl
->major
;
6394 di
->disk
.minor
= dl
->minor
;
6396 di
->recovery_start
= 0;
6397 di
->data_offset
= __le32_to_cpu(map
->pba_of_lba0
);
6398 di
->component_size
= a
->info
.component_size
;
6399 di
->container_member
= inst
;
6400 super
->random
= random32();
6404 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
6405 i
, di
->data_offset
);
6411 /* No spares found */
6413 /* Now 'rv' has a list of devices to return.
6414 * Create a metadata_update record to update the
6415 * disk_ord_tbl for the array
6417 mu
= malloc(sizeof(*mu
));
6419 mu
->buf
= malloc(sizeof(struct imsm_update_activate_spare
) * num_spares
);
6420 if (mu
->buf
== NULL
) {
6427 struct mdinfo
*n
= rv
->next
;
6436 mu
->space_list
= NULL
;
6437 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
6438 mu
->next
= *updates
;
6439 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
6441 for (di
= rv
; di
; di
= di
->next
) {
6442 u
->type
= update_activate_spare
;
6443 u
->dl
= (struct dl
*) di
->devs
;
6445 u
->slot
= di
->disk
.raid_disk
;
6456 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
6458 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
6459 struct imsm_map
*map
= get_imsm_map(dev
, 0);
6460 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, 0);
6461 struct disk_info
*inf
= get_disk_info(u
);
6462 struct imsm_disk
*disk
;
6466 for (i
= 0; i
< map
->num_members
; i
++) {
6467 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, -1));
6468 for (j
= 0; j
< new_map
->num_members
; j
++)
6469 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
6477 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
6479 struct dl
*dl
= NULL
;
6480 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6481 if ((dl
->major
== major
) && (dl
->minor
== minor
))
6486 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
6488 struct dl
*prev
= NULL
;
6492 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
6493 if ((dl
->major
== major
) && (dl
->minor
== minor
)) {
6496 prev
->next
= dl
->next
;
6498 super
->disks
= dl
->next
;
6500 __free_imsm_disk(dl
);
6501 dprintf("%s: removed %x:%x\n",
6502 __func__
, major
, minor
);
6510 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
6512 static int add_remove_disk_update(struct intel_super
*super
)
6514 int check_degraded
= 0;
6515 struct dl
*disk
= NULL
;
6516 /* add/remove some spares to/from the metadata/contrainer */
6517 while (super
->disk_mgmt_list
) {
6518 struct dl
*disk_cfg
;
6520 disk_cfg
= super
->disk_mgmt_list
;
6521 super
->disk_mgmt_list
= disk_cfg
->next
;
6522 disk_cfg
->next
= NULL
;
6524 if (disk_cfg
->action
== DISK_ADD
) {
6525 disk_cfg
->next
= super
->disks
;
6526 super
->disks
= disk_cfg
;
6528 dprintf("%s: added %x:%x\n",
6529 __func__
, disk_cfg
->major
,
6531 } else if (disk_cfg
->action
== DISK_REMOVE
) {
6532 dprintf("Disk remove action processed: %x.%x\n",
6533 disk_cfg
->major
, disk_cfg
->minor
);
6534 disk
= get_disk_super(super
,
6538 /* store action status */
6539 disk
->action
= DISK_REMOVE
;
6540 /* remove spare disks only */
6541 if (disk
->index
== -1) {
6542 remove_disk_super(super
,
6547 /* release allocate disk structure */
6548 __free_imsm_disk(disk_cfg
);
6551 return check_degraded
;
6555 static int apply_reshape_migration_update(struct imsm_update_reshape_migration
*u
,
6556 struct intel_super
*super
,
6559 struct intel_dev
*id
;
6560 void **tofree
= NULL
;
6563 dprintf("apply_reshape_migration_update()\n");
6564 if ((u
->subdev
< 0) ||
6566 dprintf("imsm: Error: Wrong subdev: %i\n", u
->subdev
);
6569 if ((space_list
== NULL
) || (*space_list
== NULL
)) {
6570 dprintf("imsm: Error: Memory is not allocated\n");
6574 for (id
= super
->devlist
; id
; id
= id
->next
) {
6575 if (id
->index
== (unsigned)u
->subdev
) {
6576 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subdev
);
6577 struct imsm_map
*map
;
6578 struct imsm_dev
*new_dev
=
6579 (struct imsm_dev
*)*space_list
;
6580 struct imsm_map
*migr_map
= get_imsm_map(dev
, 1);
6582 struct dl
*new_disk
;
6584 if (new_dev
== NULL
)
6586 *space_list
= **space_list
;
6587 memcpy(new_dev
, dev
, sizeof_imsm_dev(dev
, 0));
6588 map
= get_imsm_map(new_dev
, 0);
6590 dprintf("imsm: Error: migration in progress");
6594 to_state
= map
->map_state
;
6595 if ((u
->new_level
== 5) && (map
->raid_level
== 0)) {
6597 /* this should not happen */
6598 if (u
->new_disks
[0] < 0) {
6599 map
->failed_disk_num
=
6600 map
->num_members
- 1;
6601 to_state
= IMSM_T_STATE_DEGRADED
;
6603 to_state
= IMSM_T_STATE_NORMAL
;
6605 migrate(new_dev
, super
, to_state
, MIGR_GEN_MIGR
);
6606 if (u
->new_level
> -1)
6607 map
->raid_level
= u
->new_level
;
6608 migr_map
= get_imsm_map(new_dev
, 1);
6609 if ((u
->new_level
== 5) &&
6610 (migr_map
->raid_level
== 0)) {
6611 int ord
= map
->num_members
- 1;
6612 migr_map
->num_members
--;
6613 if (u
->new_disks
[0] < 0)
6614 ord
|= IMSM_ORD_REBUILD
;
6615 set_imsm_ord_tbl_ent(map
,
6616 map
->num_members
- 1,
6620 tofree
= (void **)dev
;
6622 /* update chunk size
6624 if (u
->new_chunksize
> 0)
6625 map
->blocks_per_strip
=
6626 __cpu_to_le16(u
->new_chunksize
* 2);
6630 if ((u
->new_level
!= 5) ||
6631 (migr_map
->raid_level
!= 0) ||
6632 (migr_map
->raid_level
== map
->raid_level
))
6635 if (u
->new_disks
[0] >= 0) {
6638 new_disk
= get_disk_super(super
,
6639 major(u
->new_disks
[0]),
6640 minor(u
->new_disks
[0]));
6641 dprintf("imsm: new disk for reshape is: %i:%i "
6642 "(%p, index = %i)\n",
6643 major(u
->new_disks
[0]),
6644 minor(u
->new_disks
[0]),
6645 new_disk
, new_disk
->index
);
6646 if (new_disk
== NULL
)
6647 goto error_disk_add
;
6649 new_disk
->index
= map
->num_members
- 1;
6650 /* slot to fill in autolayout
6652 new_disk
->raiddisk
= new_disk
->index
;
6653 new_disk
->disk
.status
|= CONFIGURED_DISK
;
6654 new_disk
->disk
.status
&= ~SPARE_DISK
;
6656 goto error_disk_add
;
6659 *tofree
= *space_list
;
6660 /* calculate new size
6662 imsm_set_array_size(new_dev
);
6669 *space_list
= tofree
;
6673 dprintf("Error: imsm: Cannot find disk.\n");
6678 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
6679 struct intel_super
*super
,
6682 struct dl
*new_disk
;
6683 struct intel_dev
*id
;
6685 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
6686 int disk_count
= u
->old_raid_disks
;
6687 void **tofree
= NULL
;
6688 int devices_to_reshape
= 1;
6689 struct imsm_super
*mpb
= super
->anchor
;
6691 unsigned int dev_id
;
6693 dprintf("imsm: apply_reshape_container_disks_update()\n");
6695 /* enable spares to use in array */
6696 for (i
= 0; i
< delta_disks
; i
++) {
6697 new_disk
= get_disk_super(super
,
6698 major(u
->new_disks
[i
]),
6699 minor(u
->new_disks
[i
]));
6700 dprintf("imsm: new disk for reshape is: %i:%i "
6701 "(%p, index = %i)\n",
6702 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
6703 new_disk
, new_disk
->index
);
6704 if ((new_disk
== NULL
) ||
6705 ((new_disk
->index
>= 0) &&
6706 (new_disk
->index
< u
->old_raid_disks
)))
6707 goto update_reshape_exit
;
6708 new_disk
->index
= disk_count
++;
6709 /* slot to fill in autolayout
6711 new_disk
->raiddisk
= new_disk
->index
;
6712 new_disk
->disk
.status
|=
6714 new_disk
->disk
.status
&= ~SPARE_DISK
;
6717 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
6718 mpb
->num_raid_devs
);
6719 /* manage changes in volume
6721 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
6722 void **sp
= *space_list
;
6723 struct imsm_dev
*newdev
;
6724 struct imsm_map
*newmap
, *oldmap
;
6726 for (id
= super
->devlist
; id
; id
= id
->next
) {
6727 if (id
->index
== dev_id
)
6736 /* Copy the dev, but not (all of) the map */
6737 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
6738 oldmap
= get_imsm_map(id
->dev
, 0);
6739 newmap
= get_imsm_map(newdev
, 0);
6740 /* Copy the current map */
6741 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
6742 /* update one device only
6744 if (devices_to_reshape
) {
6745 dprintf("imsm: modifying subdev: %i\n",
6747 devices_to_reshape
--;
6748 newdev
->vol
.migr_state
= 1;
6749 newdev
->vol
.curr_migr_unit
= 0;
6750 newdev
->vol
.migr_type
= MIGR_GEN_MIGR
;
6751 newmap
->num_members
= u
->new_raid_disks
;
6752 for (i
= 0; i
< delta_disks
; i
++) {
6753 set_imsm_ord_tbl_ent(newmap
,
6754 u
->old_raid_disks
+ i
,
6755 u
->old_raid_disks
+ i
);
6757 /* New map is correct, now need to save old map
6759 newmap
= get_imsm_map(newdev
, 1);
6760 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
6762 imsm_set_array_size(newdev
);
6765 sp
= (void **)id
->dev
;
6770 /* Clear migration record */
6771 memset(super
->migr_rec
, 0, sizeof(struct migr_record
));
6774 *space_list
= tofree
;
6777 update_reshape_exit
:
6782 static int apply_takeover_update(struct imsm_update_takeover
*u
,
6783 struct intel_super
*super
,
6786 struct imsm_dev
*dev
= NULL
;
6787 struct intel_dev
*dv
;
6788 struct imsm_dev
*dev_new
;
6789 struct imsm_map
*map
;
6793 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
6794 if (dv
->index
== (unsigned int)u
->subarray
) {
6802 map
= get_imsm_map(dev
, 0);
6804 if (u
->direction
== R10_TO_R0
) {
6805 /* Number of failed disks must be half of initial disk number */
6806 if (imsm_count_failed(super
, dev
) != (map
->num_members
/ 2))
6809 /* iterate through devices to mark removed disks as spare */
6810 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
6811 if (dm
->disk
.status
& FAILED_DISK
) {
6812 int idx
= dm
->index
;
6813 /* update indexes on the disk list */
6814 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
6815 the index values will end up being correct.... NB */
6816 for (du
= super
->disks
; du
; du
= du
->next
)
6817 if (du
->index
> idx
)
6819 /* mark as spare disk */
6820 dm
->disk
.status
= SPARE_DISK
;
6825 map
->num_members
= map
->num_members
/ 2;
6826 map
->map_state
= IMSM_T_STATE_NORMAL
;
6827 map
->num_domains
= 1;
6828 map
->raid_level
= 0;
6829 map
->failed_disk_num
= -1;
6832 if (u
->direction
== R0_TO_R10
) {
6834 /* update slots in current disk list */
6835 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
6839 /* create new *missing* disks */
6840 for (i
= 0; i
< map
->num_members
; i
++) {
6841 space
= *space_list
;
6844 *space_list
= *space
;
6846 memcpy(du
, super
->disks
, sizeof(*du
));
6850 du
->index
= (i
* 2) + 1;
6851 sprintf((char *)du
->disk
.serial
,
6852 " MISSING_%d", du
->index
);
6853 sprintf((char *)du
->serial
,
6854 "MISSING_%d", du
->index
);
6855 du
->next
= super
->missing
;
6856 super
->missing
= du
;
6858 /* create new dev and map */
6859 space
= *space_list
;
6862 *space_list
= *space
;
6863 dev_new
= (void *)space
;
6864 memcpy(dev_new
, dev
, sizeof(*dev
));
6865 /* update new map */
6866 map
= get_imsm_map(dev_new
, 0);
6867 map
->num_members
= map
->num_members
* 2;
6868 map
->map_state
= IMSM_T_STATE_DEGRADED
;
6869 map
->num_domains
= 2;
6870 map
->raid_level
= 1;
6871 /* replace dev<->dev_new */
6874 /* update disk order table */
6875 for (du
= super
->disks
; du
; du
= du
->next
)
6877 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
6878 for (du
= super
->missing
; du
; du
= du
->next
)
6879 if (du
->index
>= 0) {
6880 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
6881 mark_missing(dev_new
, &du
->disk
, du
->index
);
6887 static void imsm_process_update(struct supertype
*st
,
6888 struct metadata_update
*update
)
6891 * crack open the metadata_update envelope to find the update record
6892 * update can be one of:
6893 * update_reshape_container_disks - all the arrays in the container
6894 * are being reshaped to have more devices. We need to mark
6895 * the arrays for general migration and convert selected spares
6896 * into active devices.
6897 * update_activate_spare - a spare device has replaced a failed
6898 * device in an array, update the disk_ord_tbl. If this disk is
6899 * present in all member arrays then also clear the SPARE_DISK
6901 * update_create_array
6903 * update_rename_array
6904 * update_add_remove_disk
6906 struct intel_super
*super
= st
->sb
;
6907 struct imsm_super
*mpb
;
6908 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
6910 /* update requires a larger buf but the allocation failed */
6911 if (super
->next_len
&& !super
->next_buf
) {
6912 super
->next_len
= 0;
6916 if (super
->next_buf
) {
6917 memcpy(super
->next_buf
, super
->buf
, super
->len
);
6919 super
->len
= super
->next_len
;
6920 super
->buf
= super
->next_buf
;
6922 super
->next_len
= 0;
6923 super
->next_buf
= NULL
;
6926 mpb
= super
->anchor
;
6929 case update_general_migration_checkpoint
: {
6930 struct intel_dev
*id
;
6931 struct imsm_update_general_migration_checkpoint
*u
=
6932 (void *)update
->buf
;
6934 dprintf("imsm: process_update() "
6935 "for update_general_migration_checkpoint called\n");
6937 /* find device under general migration */
6938 for (id
= super
->devlist
; id
; id
= id
->next
) {
6939 if (is_gen_migration(id
->dev
)) {
6940 id
->dev
->vol
.curr_migr_unit
=
6941 __cpu_to_le32(u
->curr_migr_unit
);
6942 super
->updates_pending
++;
6947 case update_takeover
: {
6948 struct imsm_update_takeover
*u
= (void *)update
->buf
;
6949 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
6950 imsm_update_version_info(super
);
6951 super
->updates_pending
++;
6956 case update_reshape_container_disks
: {
6957 struct imsm_update_reshape
*u
= (void *)update
->buf
;
6958 if (apply_reshape_container_disks_update(
6959 u
, super
, &update
->space_list
))
6960 super
->updates_pending
++;
6963 case update_reshape_migration
: {
6964 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
6965 if (apply_reshape_migration_update(
6966 u
, super
, &update
->space_list
))
6967 super
->updates_pending
++;
6970 case update_activate_spare
: {
6971 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
6972 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
6973 struct imsm_map
*map
= get_imsm_map(dev
, 0);
6974 struct imsm_map
*migr_map
;
6975 struct active_array
*a
;
6976 struct imsm_disk
*disk
;
6981 int victim
= get_imsm_disk_idx(dev
, u
->slot
, -1);
6984 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6989 fprintf(stderr
, "error: imsm_activate_spare passed "
6990 "an unknown disk (index: %d)\n",
6995 super
->updates_pending
++;
6996 /* count failures (excluding rebuilds and the victim)
6997 * to determine map[0] state
7000 for (i
= 0; i
< map
->num_members
; i
++) {
7003 disk
= get_imsm_disk(super
,
7004 get_imsm_disk_idx(dev
, i
, -1));
7005 if (!disk
|| is_failed(disk
))
7009 /* adding a pristine spare, assign a new index */
7010 if (dl
->index
< 0) {
7011 dl
->index
= super
->anchor
->num_disks
;
7012 super
->anchor
->num_disks
++;
7015 disk
->status
|= CONFIGURED_DISK
;
7016 disk
->status
&= ~SPARE_DISK
;
7019 to_state
= imsm_check_degraded(super
, dev
, failed
);
7020 map
->map_state
= IMSM_T_STATE_DEGRADED
;
7021 migrate(dev
, super
, to_state
, MIGR_REBUILD
);
7022 migr_map
= get_imsm_map(dev
, 1);
7023 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
7024 set_imsm_ord_tbl_ent(migr_map
, u
->slot
, dl
->index
| IMSM_ORD_REBUILD
);
7026 /* update the family_num to mark a new container
7027 * generation, being careful to record the existing
7028 * family_num in orig_family_num to clean up after
7029 * earlier mdadm versions that neglected to set it.
7031 if (mpb
->orig_family_num
== 0)
7032 mpb
->orig_family_num
= mpb
->family_num
;
7033 mpb
->family_num
+= super
->random
;
7035 /* count arrays using the victim in the metadata */
7037 for (a
= st
->arrays
; a
; a
= a
->next
) {
7038 dev
= get_imsm_dev(super
, a
->info
.container_member
);
7039 map
= get_imsm_map(dev
, 0);
7041 if (get_imsm_disk_slot(map
, victim
) >= 0)
7045 /* delete the victim if it is no longer being
7051 /* We know that 'manager' isn't touching anything,
7052 * so it is safe to delete
7054 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
7055 if ((*dlp
)->index
== victim
)
7058 /* victim may be on the missing list */
7060 for (dlp
= &super
->missing
; *dlp
; dlp
= &(*dlp
)->next
)
7061 if ((*dlp
)->index
== victim
)
7063 imsm_delete(super
, dlp
, victim
);
7067 case update_create_array
: {
7068 /* someone wants to create a new array, we need to be aware of
7069 * a few races/collisions:
7070 * 1/ 'Create' called by two separate instances of mdadm
7071 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
7072 * devices that have since been assimilated via
7074 * In the event this update can not be carried out mdadm will
7075 * (FIX ME) notice that its update did not take hold.
7077 struct imsm_update_create_array
*u
= (void *) update
->buf
;
7078 struct intel_dev
*dv
;
7079 struct imsm_dev
*dev
;
7080 struct imsm_map
*map
, *new_map
;
7081 unsigned long long start
, end
;
7082 unsigned long long new_start
, new_end
;
7084 struct disk_info
*inf
;
7087 /* handle racing creates: first come first serve */
7088 if (u
->dev_idx
< mpb
->num_raid_devs
) {
7089 dprintf("%s: subarray %d already defined\n",
7090 __func__
, u
->dev_idx
);
7094 /* check update is next in sequence */
7095 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
7096 dprintf("%s: can not create array %d expected index %d\n",
7097 __func__
, u
->dev_idx
, mpb
->num_raid_devs
);
7101 new_map
= get_imsm_map(&u
->dev
, 0);
7102 new_start
= __le32_to_cpu(new_map
->pba_of_lba0
);
7103 new_end
= new_start
+ __le32_to_cpu(new_map
->blocks_per_member
);
7104 inf
= get_disk_info(u
);
7106 /* handle activate_spare versus create race:
7107 * check to make sure that overlapping arrays do not include
7110 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7111 dev
= get_imsm_dev(super
, i
);
7112 map
= get_imsm_map(dev
, 0);
7113 start
= __le32_to_cpu(map
->pba_of_lba0
);
7114 end
= start
+ __le32_to_cpu(map
->blocks_per_member
);
7115 if ((new_start
>= start
&& new_start
<= end
) ||
7116 (start
>= new_start
&& start
<= new_end
))
7121 if (disks_overlap(super
, i
, u
)) {
7122 dprintf("%s: arrays overlap\n", __func__
);
7127 /* check that prepare update was successful */
7128 if (!update
->space
) {
7129 dprintf("%s: prepare update failed\n", __func__
);
7133 /* check that all disks are still active before committing
7134 * changes. FIXME: could we instead handle this by creating a
7135 * degraded array? That's probably not what the user expects,
7136 * so better to drop this update on the floor.
7138 for (i
= 0; i
< new_map
->num_members
; i
++) {
7139 dl
= serial_to_dl(inf
[i
].serial
, super
);
7141 dprintf("%s: disk disappeared\n", __func__
);
7146 super
->updates_pending
++;
7148 /* convert spares to members and fixup ord_tbl */
7149 for (i
= 0; i
< new_map
->num_members
; i
++) {
7150 dl
= serial_to_dl(inf
[i
].serial
, super
);
7151 if (dl
->index
== -1) {
7152 dl
->index
= mpb
->num_disks
;
7154 dl
->disk
.status
|= CONFIGURED_DISK
;
7155 dl
->disk
.status
&= ~SPARE_DISK
;
7157 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
7162 update
->space
= NULL
;
7163 imsm_copy_dev(dev
, &u
->dev
);
7164 dv
->index
= u
->dev_idx
;
7165 dv
->next
= super
->devlist
;
7166 super
->devlist
= dv
;
7167 mpb
->num_raid_devs
++;
7169 imsm_update_version_info(super
);
7172 /* mdmon knows how to release update->space, but not
7173 * ((struct intel_dev *) update->space)->dev
7175 if (update
->space
) {
7181 case update_kill_array
: {
7182 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
7183 int victim
= u
->dev_idx
;
7184 struct active_array
*a
;
7185 struct intel_dev
**dp
;
7186 struct imsm_dev
*dev
;
7188 /* sanity check that we are not affecting the uuid of
7189 * active arrays, or deleting an active array
7191 * FIXME when immutable ids are available, but note that
7192 * we'll also need to fixup the invalidated/active
7193 * subarray indexes in mdstat
7195 for (a
= st
->arrays
; a
; a
= a
->next
)
7196 if (a
->info
.container_member
>= victim
)
7198 /* by definition if mdmon is running at least one array
7199 * is active in the container, so checking
7200 * mpb->num_raid_devs is just extra paranoia
7202 dev
= get_imsm_dev(super
, victim
);
7203 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
7204 dprintf("failed to delete subarray-%d\n", victim
);
7208 for (dp
= &super
->devlist
; *dp
;)
7209 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
7212 if ((*dp
)->index
> (unsigned)victim
)
7216 mpb
->num_raid_devs
--;
7217 super
->updates_pending
++;
7220 case update_rename_array
: {
7221 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
7222 char name
[MAX_RAID_SERIAL_LEN
+1];
7223 int target
= u
->dev_idx
;
7224 struct active_array
*a
;
7225 struct imsm_dev
*dev
;
7227 /* sanity check that we are not affecting the uuid of
7230 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
7231 name
[MAX_RAID_SERIAL_LEN
] = '\0';
7232 for (a
= st
->arrays
; a
; a
= a
->next
)
7233 if (a
->info
.container_member
== target
)
7235 dev
= get_imsm_dev(super
, u
->dev_idx
);
7236 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
7237 dprintf("failed to rename subarray-%d\n", target
);
7241 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
7242 super
->updates_pending
++;
7245 case update_add_remove_disk
: {
7246 /* we may be able to repair some arrays if disks are
7247 * being added, check teh status of add_remove_disk
7248 * if discs has been added.
7250 if (add_remove_disk_update(super
)) {
7251 struct active_array
*a
;
7253 super
->updates_pending
++;
7254 for (a
= st
->arrays
; a
; a
= a
->next
)
7255 a
->check_degraded
= 1;
7260 fprintf(stderr
, "error: unsuported process update type:"
7261 "(type: %d)\n", type
);
7265 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
);
7267 static void imsm_prepare_update(struct supertype
*st
,
7268 struct metadata_update
*update
)
7271 * Allocate space to hold new disk entries, raid-device entries or a new
7272 * mpb if necessary. The manager synchronously waits for updates to
7273 * complete in the monitor, so new mpb buffers allocated here can be
7274 * integrated by the monitor thread without worrying about live pointers
7275 * in the manager thread.
7277 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
7278 struct intel_super
*super
= st
->sb
;
7279 struct imsm_super
*mpb
= super
->anchor
;
7284 case update_general_migration_checkpoint
:
7285 dprintf("imsm: prepare_update() "
7286 "for update_general_migration_checkpoint called\n");
7288 case update_takeover
: {
7289 struct imsm_update_takeover
*u
= (void *)update
->buf
;
7290 if (u
->direction
== R0_TO_R10
) {
7291 void **tail
= (void **)&update
->space_list
;
7292 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
7293 struct imsm_map
*map
= get_imsm_map(dev
, 0);
7294 int num_members
= map
->num_members
;
7298 /* allocate memory for added disks */
7299 for (i
= 0; i
< num_members
; i
++) {
7300 size
= sizeof(struct dl
);
7301 space
= malloc(size
);
7310 /* allocate memory for new device */
7311 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
7312 (num_members
* sizeof(__u32
));
7313 space
= malloc(size
);
7322 len
= disks_to_mpb_size(num_members
* 2);
7324 /* if allocation didn't success, free buffer */
7325 while (update
->space_list
) {
7326 void **sp
= update
->space_list
;
7327 update
->space_list
= *sp
;
7335 case update_reshape_container_disks
: {
7336 /* Every raid device in the container is about to
7337 * gain some more devices, and we will enter a
7339 * So each 'imsm_map' will be bigger, and the imsm_vol
7340 * will now hold 2 of them.
7341 * Thus we need new 'struct imsm_dev' allocations sized
7342 * as sizeof_imsm_dev but with more devices in both maps.
7344 struct imsm_update_reshape
*u
= (void *)update
->buf
;
7345 struct intel_dev
*dl
;
7346 void **space_tail
= (void**)&update
->space_list
;
7348 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
7350 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
7351 int size
= sizeof_imsm_dev(dl
->dev
, 1);
7353 if (u
->new_raid_disks
> u
->old_raid_disks
)
7354 size
+= sizeof(__u32
)*2*
7355 (u
->new_raid_disks
- u
->old_raid_disks
);
7364 len
= disks_to_mpb_size(u
->new_raid_disks
);
7365 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
7368 case update_reshape_migration
: {
7369 /* for migration level 0->5 we need to add disks
7370 * so the same as for container operation we will copy
7371 * device to the bigger location.
7372 * in memory prepared device and new disk area are prepared
7373 * for usage in process update
7375 struct imsm_update_reshape_migration
*u
= (void *)update
->buf
;
7376 struct intel_dev
*id
;
7377 void **space_tail
= (void **)&update
->space_list
;
7380 int current_level
= -1;
7382 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
7384 /* add space for bigger array in update
7386 for (id
= super
->devlist
; id
; id
= id
->next
) {
7387 if (id
->index
== (unsigned)u
->subdev
) {
7388 size
= sizeof_imsm_dev(id
->dev
, 1);
7389 if (u
->new_raid_disks
> u
->old_raid_disks
)
7390 size
+= sizeof(__u32
)*2*
7391 (u
->new_raid_disks
- u
->old_raid_disks
);
7401 if (update
->space_list
== NULL
)
7404 /* add space for disk in update
7406 size
= sizeof(struct dl
);
7409 free(update
->space_list
);
7410 update
->space_list
= NULL
;
7417 /* add spare device to update
7419 for (id
= super
->devlist
; id
; id
= id
->next
)
7420 if (id
->index
== (unsigned)u
->subdev
) {
7421 struct imsm_dev
*dev
;
7422 struct imsm_map
*map
;
7424 dev
= get_imsm_dev(super
, u
->subdev
);
7425 map
= get_imsm_map(dev
, 0);
7426 current_level
= map
->raid_level
;
7429 if ((u
->new_level
== 5) && (u
->new_level
!= current_level
)) {
7430 struct mdinfo
*spares
;
7432 spares
= get_spares_for_grow(st
);
7440 makedev(dev
->disk
.major
,
7442 dl
= get_disk_super(super
,
7445 dl
->index
= u
->old_raid_disks
;
7451 len
= disks_to_mpb_size(u
->new_raid_disks
);
7452 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
7455 case update_create_array
: {
7456 struct imsm_update_create_array
*u
= (void *) update
->buf
;
7457 struct intel_dev
*dv
;
7458 struct imsm_dev
*dev
= &u
->dev
;
7459 struct imsm_map
*map
= get_imsm_map(dev
, 0);
7461 struct disk_info
*inf
;
7465 inf
= get_disk_info(u
);
7466 len
= sizeof_imsm_dev(dev
, 1);
7467 /* allocate a new super->devlist entry */
7468 dv
= malloc(sizeof(*dv
));
7470 dv
->dev
= malloc(len
);
7475 update
->space
= NULL
;
7479 /* count how many spares will be converted to members */
7480 for (i
= 0; i
< map
->num_members
; i
++) {
7481 dl
= serial_to_dl(inf
[i
].serial
, super
);
7483 /* hmm maybe it failed?, nothing we can do about
7488 if (count_memberships(dl
, super
) == 0)
7491 len
+= activate
* sizeof(struct imsm_disk
);
7498 /* check if we need a larger metadata buffer */
7499 if (super
->next_buf
)
7500 buf_len
= super
->next_len
;
7502 buf_len
= super
->len
;
7504 if (__le32_to_cpu(mpb
->mpb_size
) + len
> buf_len
) {
7505 /* ok we need a larger buf than what is currently allocated
7506 * if this allocation fails process_update will notice that
7507 * ->next_len is set and ->next_buf is NULL
7509 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + len
, 512);
7510 if (super
->next_buf
)
7511 free(super
->next_buf
);
7513 super
->next_len
= buf_len
;
7514 if (posix_memalign(&super
->next_buf
, 512, buf_len
) == 0)
7515 memset(super
->next_buf
, 0, buf_len
);
7517 super
->next_buf
= NULL
;
7521 /* must be called while manager is quiesced */
7522 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
7524 struct imsm_super
*mpb
= super
->anchor
;
7526 struct imsm_dev
*dev
;
7527 struct imsm_map
*map
;
7528 int i
, j
, num_members
;
7531 dprintf("%s: deleting device[%d] from imsm_super\n",
7534 /* shift all indexes down one */
7535 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
7536 if (iter
->index
> (int)index
)
7538 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
7539 if (iter
->index
> (int)index
)
7542 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
7543 dev
= get_imsm_dev(super
, i
);
7544 map
= get_imsm_map(dev
, 0);
7545 num_members
= map
->num_members
;
7546 for (j
= 0; j
< num_members
; j
++) {
7547 /* update ord entries being careful not to propagate
7548 * ord-flags to the first map
7550 ord
= get_imsm_ord_tbl_ent(dev
, j
, -1);
7552 if (ord_to_idx(ord
) <= index
)
7555 map
= get_imsm_map(dev
, 0);
7556 set_imsm_ord_tbl_ent(map
, j
, ord_to_idx(ord
- 1));
7557 map
= get_imsm_map(dev
, 1);
7559 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
7564 super
->updates_pending
++;
7566 struct dl
*dl
= *dlp
;
7568 *dlp
= (*dlp
)->next
;
7569 __free_imsm_disk(dl
);
7573 /*******************************************************************************
7574 * Function: open_backup_targets
7575 * Description: Function opens file descriptors for all devices given in
7578 * info : general array info
7579 * raid_disks : number of disks
7580 * raid_fds : table of device's file descriptors
7584 ******************************************************************************/
7585 int open_backup_targets(struct mdinfo
*info
, int raid_disks
, int *raid_fds
)
7589 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
7592 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
)) {
7593 dprintf("disk is faulty!!\n");
7597 if ((sd
->disk
.raid_disk
>= raid_disks
) ||
7598 (sd
->disk
.raid_disk
< 0))
7601 dn
= map_dev(sd
->disk
.major
,
7603 raid_fds
[sd
->disk
.raid_disk
] = dev_open(dn
, O_RDWR
);
7604 if (raid_fds
[sd
->disk
.raid_disk
] < 0) {
7605 fprintf(stderr
, "cannot open component\n");
7612 /*******************************************************************************
7613 * Function: init_migr_record_imsm
7614 * Description: Function inits imsm migration record
7616 * super : imsm internal array info
7617 * dev : device under migration
7618 * info : general array info to find the smallest device
7621 ******************************************************************************/
7622 void init_migr_record_imsm(struct supertype
*st
, struct imsm_dev
*dev
,
7623 struct mdinfo
*info
)
7625 struct intel_super
*super
= st
->sb
;
7626 struct migr_record
*migr_rec
= super
->migr_rec
;
7628 unsigned long long dsize
, dev_sectors
;
7629 long long unsigned min_dev_sectors
= -1LLU;
7633 struct imsm_map
*map_dest
= get_imsm_map(dev
, 0);
7634 struct imsm_map
*map_src
= get_imsm_map(dev
, 1);
7635 unsigned long long num_migr_units
;
7636 unsigned long long array_blocks
;
7638 memset(migr_rec
, 0, sizeof(struct migr_record
));
7639 migr_rec
->family_num
= __cpu_to_le32(super
->anchor
->family_num
);
7641 /* only ascending reshape supported now */
7642 migr_rec
->ascending_migr
= __cpu_to_le32(1);
7644 migr_rec
->dest_depth_per_unit
= GEN_MIGR_AREA_SIZE
/
7645 max(map_dest
->blocks_per_strip
, map_src
->blocks_per_strip
);
7646 migr_rec
->dest_depth_per_unit
*= map_dest
->blocks_per_strip
;
7647 new_data_disks
= imsm_num_data_members(dev
, 0);
7648 migr_rec
->blocks_per_unit
=
7649 __cpu_to_le32(migr_rec
->dest_depth_per_unit
* new_data_disks
);
7650 migr_rec
->dest_depth_per_unit
=
7651 __cpu_to_le32(migr_rec
->dest_depth_per_unit
);
7652 array_blocks
= info
->component_size
* new_data_disks
;
7654 array_blocks
/ __le32_to_cpu(migr_rec
->blocks_per_unit
);
7656 if (array_blocks
% __le32_to_cpu(migr_rec
->blocks_per_unit
))
7658 migr_rec
->num_migr_units
= __cpu_to_le32(num_migr_units
);
7660 migr_rec
->post_migr_vol_cap
= dev
->size_low
;
7661 migr_rec
->post_migr_vol_cap_hi
= dev
->size_high
;
7664 /* Find the smallest dev */
7665 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
7666 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
7667 fd
= dev_open(nm
, O_RDONLY
);
7670 get_dev_size(fd
, NULL
, &dsize
);
7671 dev_sectors
= dsize
/ 512;
7672 if (dev_sectors
< min_dev_sectors
)
7673 min_dev_sectors
= dev_sectors
;
7676 migr_rec
->ckpt_area_pba
= __cpu_to_le32(min_dev_sectors
-
7677 RAID_DISK_RESERVED_BLOCKS_IMSM_HI
);
7679 write_imsm_migr_rec(st
);
7684 /*******************************************************************************
7685 * Function: save_backup_imsm
7686 * Description: Function saves critical data stripes to Migration Copy Area
7687 * and updates the current migration unit status.
7688 * Use restore_stripes() to form a destination stripe,
7689 * and to write it to the Copy Area.
7691 * st : supertype information
7692 * dev : imsm device that backup is saved for
7693 * info : general array info
7694 * buf : input buffer
7695 * length : length of data to backup (blocks_per_unit)
7699 ******************************************************************************/
7700 int save_backup_imsm(struct supertype
*st
,
7701 struct imsm_dev
*dev
,
7702 struct mdinfo
*info
,
7707 struct intel_super
*super
= st
->sb
;
7708 unsigned long long *target_offsets
= NULL
;
7709 int *targets
= NULL
;
7711 struct imsm_map
*map_dest
= get_imsm_map(dev
, 0);
7712 int new_disks
= map_dest
->num_members
;
7713 int dest_layout
= 0;
7715 unsigned long long start
;
7716 int data_disks
= imsm_num_data_members(dev
, 0);
7718 targets
= malloc(new_disks
* sizeof(int));
7722 for (i
= 0; i
< new_disks
; i
++)
7725 target_offsets
= malloc(new_disks
* sizeof(unsigned long long));
7726 if (!target_offsets
)
7729 start
= info
->reshape_progress
* 512;
7730 for (i
= 0; i
< new_disks
; i
++) {
7731 target_offsets
[i
] = (unsigned long long)
7732 __le32_to_cpu(super
->migr_rec
->ckpt_area_pba
) * 512;
7733 /* move back copy area adderss, it will be moved forward
7734 * in restore_stripes() using start input variable
7736 target_offsets
[i
] -= start
/data_disks
;
7739 if (open_backup_targets(info
, new_disks
, targets
))
7742 dest_layout
= imsm_level_to_layout(map_dest
->raid_level
);
7743 dest_chunk
= __le16_to_cpu(map_dest
->blocks_per_strip
) * 512;
7745 if (restore_stripes(targets
, /* list of dest devices */
7746 target_offsets
, /* migration record offsets */
7749 map_dest
->raid_level
,
7751 -1, /* source backup file descriptor */
7752 0, /* input buf offset
7753 * always 0 buf is already offseted */
7757 fprintf(stderr
, Name
": Error restoring stripes\n");
7765 for (i
= 0; i
< new_disks
; i
++)
7766 if (targets
[i
] >= 0)
7770 free(target_offsets
);
7775 /*******************************************************************************
7776 * Function: save_checkpoint_imsm
7777 * Description: Function called for current unit status update
7778 * in the migration record. It writes it to disk.
7780 * super : imsm internal array info
7781 * info : general array info
7785 * 2: failure, means no valid migration record
7786 * / no general migration in progress /
7787 ******************************************************************************/
7788 int save_checkpoint_imsm(struct supertype
*st
, struct mdinfo
*info
, int state
)
7790 struct intel_super
*super
= st
->sb
;
7791 unsigned long long blocks_per_unit
;
7792 unsigned long long curr_migr_unit
;
7794 if (load_imsm_migr_rec(super
, info
) != 0) {
7795 dprintf("imsm: ERROR: Cannot read migration record "
7796 "for checkpoint save.\n");
7800 blocks_per_unit
= __le32_to_cpu(super
->migr_rec
->blocks_per_unit
);
7801 if (blocks_per_unit
== 0) {
7802 dprintf("imsm: no migration in progress.\n");
7805 curr_migr_unit
= info
->reshape_progress
/ blocks_per_unit
;
7806 /* check if array is alligned to copy area
7807 * if it is not alligned, add one to current migration unit value
7808 * this can happend on array reshape finish only
7810 if (info
->reshape_progress
% blocks_per_unit
)
7813 super
->migr_rec
->curr_migr_unit
=
7814 __cpu_to_le32(curr_migr_unit
);
7815 super
->migr_rec
->rec_status
= __cpu_to_le32(state
);
7816 super
->migr_rec
->dest_1st_member_lba
=
7817 __cpu_to_le32(curr_migr_unit
*
7818 __le32_to_cpu(super
->migr_rec
->dest_depth_per_unit
));
7819 if (write_imsm_migr_rec(st
) < 0) {
7820 dprintf("imsm: Cannot write migration record "
7821 "outside backup area\n");
7828 static __u64
blocks_per_migr_unit(struct intel_super
*super
,
7829 struct imsm_dev
*dev
);
7831 /*******************************************************************************
7832 * Function: recover_backup_imsm
7833 * Description: Function recovers critical data from the Migration Copy Area
7834 * while assembling an array.
7836 * super : imsm internal array info
7837 * info : general array info
7839 * 0 : success (or there is no data to recover)
7841 ******************************************************************************/
7842 int recover_backup_imsm(struct supertype
*st
, struct mdinfo
*info
)
7844 struct intel_super
*super
= st
->sb
;
7845 struct migr_record
*migr_rec
= super
->migr_rec
;
7846 struct imsm_map
*map_dest
= NULL
;
7847 struct intel_dev
*id
= NULL
;
7848 unsigned long long read_offset
;
7849 unsigned long long write_offset
;
7851 int *targets
= NULL
;
7852 int new_disks
, i
, err
;
7855 unsigned long curr_migr_unit
= __le32_to_cpu(migr_rec
->curr_migr_unit
);
7856 unsigned long num_migr_units
= __le32_to_cpu(migr_rec
->num_migr_units
);
7858 int skipped_disks
= 0;
7859 int max_degradation
;
7861 err
= sysfs_get_str(info
, NULL
, "array_state", (char *)buffer
, 20);
7865 /* recover data only during assemblation */
7866 if (strncmp(buffer
, "inactive", 8) != 0)
7868 /* no data to recover */
7869 if (__le32_to_cpu(migr_rec
->rec_status
) == UNIT_SRC_NORMAL
)
7871 if (curr_migr_unit
>= num_migr_units
)
7874 /* find device during reshape */
7875 for (id
= super
->devlist
; id
; id
= id
->next
)
7876 if (is_gen_migration(id
->dev
))
7881 map_dest
= get_imsm_map(id
->dev
, 0);
7882 new_disks
= map_dest
->num_members
;
7883 max_degradation
= new_disks
- imsm_num_data_members(id
->dev
, 0);
7885 read_offset
= (unsigned long long)
7886 __le32_to_cpu(migr_rec
->ckpt_area_pba
) * 512;
7888 write_offset
= ((unsigned long long)
7889 __le32_to_cpu(migr_rec
->dest_1st_member_lba
) +
7890 __le32_to_cpu(map_dest
->pba_of_lba0
)) * 512;
7892 unit_len
= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
7893 if (posix_memalign((void **)&buf
, 512, unit_len
) != 0)
7895 targets
= malloc(new_disks
* sizeof(int));
7899 open_backup_targets(info
, new_disks
, targets
);
7901 for (i
= 0; i
< new_disks
; i
++) {
7902 if (targets
[i
] < 0) {
7906 if (lseek64(targets
[i
], read_offset
, SEEK_SET
) < 0) {
7908 Name
": Cannot seek to block: %s\n",
7912 if (read(targets
[i
], buf
, unit_len
) != unit_len
) {
7914 Name
": Cannot read copy area block: %s\n",
7918 if (lseek64(targets
[i
], write_offset
, SEEK_SET
) < 0) {
7920 Name
": Cannot seek to block: %s\n",
7924 if (write(targets
[i
], buf
, unit_len
) != unit_len
) {
7926 Name
": Cannot restore block: %s\n",
7932 if (skipped_disks
> max_degradation
) {
7934 Name
": Cannot restore data from backup."
7935 " Too many failed disks\n");
7939 if (save_checkpoint_imsm(st
, info
, UNIT_SRC_NORMAL
)) {
7940 /* ignore error == 2, this can mean end of reshape here
7942 dprintf("imsm: Cannot write checkpoint to "
7943 "migration record (UNIT_SRC_NORMAL) during restart\n");
7949 for (i
= 0; i
< new_disks
; i
++)
7958 static char disk_by_path
[] = "/dev/disk/by-path/";
7960 static const char *imsm_get_disk_controller_domain(const char *path
)
7962 char disk_path
[PATH_MAX
];
7966 strncpy(disk_path
, disk_by_path
, PATH_MAX
- 1);
7967 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
7968 if (stat(disk_path
, &st
) == 0) {
7969 struct sys_dev
* hba
;
7972 path
= devt_to_devpath(st
.st_rdev
);
7975 hba
= find_disk_attached_hba(-1, path
);
7976 if (hba
&& hba
->type
== SYS_DEV_SAS
)
7978 else if (hba
&& hba
->type
== SYS_DEV_SATA
)
7982 dprintf("path: %s hba: %s attached: %s\n",
7983 path
, (hba
) ? hba
->path
: "NULL", drv
);
7991 static int imsm_find_array_minor_by_subdev(int subdev
, int container
, int *minor
)
7993 char subdev_name
[20];
7994 struct mdstat_ent
*mdstat
;
7996 sprintf(subdev_name
, "%d", subdev
);
7997 mdstat
= mdstat_by_subdev(subdev_name
, container
);
8001 *minor
= mdstat
->devnum
;
8002 free_mdstat(mdstat
);
8006 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
8007 struct geo_params
*geo
,
8008 int *old_raid_disks
)
8010 /* currently we only support increasing the number of devices
8011 * for a container. This increases the number of device for each
8012 * member array. They must all be RAID0 or RAID5.
8015 struct mdinfo
*info
, *member
;
8016 int devices_that_can_grow
= 0;
8018 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): "
8019 "st->devnum = (%i)\n",
8022 if (geo
->size
!= -1 ||
8023 geo
->level
!= UnSet
||
8024 geo
->layout
!= UnSet
||
8025 geo
->chunksize
!= 0 ||
8026 geo
->raid_disks
== UnSet
) {
8027 dprintf("imsm: Container operation is allowed for "
8028 "raid disks number change only.\n");
8032 info
= container_content_imsm(st
, NULL
);
8033 for (member
= info
; member
; member
= member
->next
) {
8037 dprintf("imsm: checking device_num: %i\n",
8038 member
->container_member
);
8040 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
8041 /* we work on container for Online Capacity Expansion
8042 * only so raid_disks has to grow
8044 dprintf("imsm: for container operation raid disks "
8045 "increase is required\n");
8049 if ((info
->array
.level
!= 0) &&
8050 (info
->array
.level
!= 5)) {
8051 /* we cannot use this container with other raid level
8053 dprintf("imsm: for container operation wrong"
8054 " raid level (%i) detected\n",
8058 /* check for platform support
8059 * for this raid level configuration
8061 struct intel_super
*super
= st
->sb
;
8062 if (!is_raid_level_supported(super
->orom
,
8063 member
->array
.level
,
8065 dprintf("platform does not support raid%d with"
8069 geo
->raid_disks
> 1 ? "s" : "");
8072 /* check if component size is aligned to chunk size
8074 if (info
->component_size
%
8075 (info
->array
.chunk_size
/512)) {
8076 dprintf("Component size is not aligned to "
8082 if (*old_raid_disks
&&
8083 info
->array
.raid_disks
!= *old_raid_disks
)
8085 *old_raid_disks
= info
->array
.raid_disks
;
8087 /* All raid5 and raid0 volumes in container
8088 * have to be ready for Online Capacity Expansion
8089 * so they need to be assembled. We have already
8090 * checked that no recovery etc is happening.
8092 result
= imsm_find_array_minor_by_subdev(member
->container_member
,
8096 dprintf("imsm: cannot find array\n");
8099 devices_that_can_grow
++;
8102 if (!member
&& devices_that_can_grow
)
8106 dprintf("\tContainer operation allowed\n");
8108 dprintf("\tError: %i\n", ret_val
);
8113 /* Function: get_spares_for_grow
8114 * Description: Allocates memory and creates list of spare devices
8115 * avaliable in container. Checks if spare drive size is acceptable.
8116 * Parameters: Pointer to the supertype structure
8117 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
8120 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
8122 unsigned long long min_size
= min_acceptable_spare_size_imsm(st
);
8123 return container_choose_spares(st
, min_size
, NULL
, NULL
, NULL
, 0);
8126 /******************************************************************************
8127 * function: imsm_create_metadata_update_for_reshape
8128 * Function creates update for whole IMSM container.
8130 ******************************************************************************/
8131 static int imsm_create_metadata_update_for_reshape(
8132 struct supertype
*st
,
8133 struct geo_params
*geo
,
8135 struct imsm_update_reshape
**updatep
)
8137 struct intel_super
*super
= st
->sb
;
8138 struct imsm_super
*mpb
= super
->anchor
;
8139 int update_memory_size
= 0;
8140 struct imsm_update_reshape
*u
= NULL
;
8141 struct mdinfo
*spares
= NULL
;
8143 int delta_disks
= 0;
8146 dprintf("imsm_update_metadata_for_reshape(enter) raid_disks = %i\n",
8149 delta_disks
= geo
->raid_disks
- old_raid_disks
;
8151 /* size of all update data without anchor */
8152 update_memory_size
= sizeof(struct imsm_update_reshape
);
8154 /* now add space for spare disks that we need to add. */
8155 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
8157 u
= calloc(1, update_memory_size
);
8160 "cannot get memory for imsm_update_reshape update\n");
8163 u
->type
= update_reshape_container_disks
;
8164 u
->old_raid_disks
= old_raid_disks
;
8165 u
->new_raid_disks
= geo
->raid_disks
;
8167 /* now get spare disks list
8169 spares
= get_spares_for_grow(st
);
8172 || delta_disks
> spares
->array
.spare_disks
) {
8173 fprintf(stderr
, Name
": imsm: ERROR: Cannot get spare devices "
8174 "for %s.\n", geo
->dev_name
);
8178 /* we have got spares
8179 * update disk list in imsm_disk list table in anchor
8181 dprintf("imsm: %i spares are available.\n\n",
8182 spares
->array
.spare_disks
);
8185 for (i
= 0; i
< delta_disks
; i
++) {
8190 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
8192 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
8193 dl
->index
= mpb
->num_disks
;
8203 dprintf("imsm: reshape update preparation :");
8204 if (i
== delta_disks
) {
8207 return update_memory_size
;
8210 dprintf(" Error\n");
8215 /******************************************************************************
8216 * function: imsm_create_metadata_update_for_migration()
8217 * Creates update for IMSM array.
8219 ******************************************************************************/
8220 static int imsm_create_metadata_update_for_migration(
8221 struct supertype
*st
,
8222 struct geo_params
*geo
,
8223 struct imsm_update_reshape_migration
**updatep
)
8225 struct intel_super
*super
= st
->sb
;
8226 int update_memory_size
= 0;
8227 struct imsm_update_reshape_migration
*u
= NULL
;
8228 struct imsm_dev
*dev
;
8229 int previous_level
= -1;
8231 dprintf("imsm_create_metadata_update_for_migration(enter)"
8232 " New Level = %i\n", geo
->level
);
8234 /* size of all update data without anchor */
8235 update_memory_size
= sizeof(struct imsm_update_reshape_migration
);
8237 u
= calloc(1, update_memory_size
);
8239 dprintf("error: cannot get memory for "
8240 "imsm_create_metadata_update_for_migration\n");
8243 u
->type
= update_reshape_migration
;
8244 u
->subdev
= super
->current_vol
;
8245 u
->new_level
= geo
->level
;
8246 u
->new_layout
= geo
->layout
;
8247 u
->new_raid_disks
= u
->old_raid_disks
= geo
->raid_disks
;
8248 u
->new_disks
[0] = -1;
8249 u
->new_chunksize
= -1;
8251 dev
= get_imsm_dev(super
, u
->subdev
);
8253 struct imsm_map
*map
;
8255 map
= get_imsm_map(dev
, 0);
8257 int current_chunk_size
=
8258 __le16_to_cpu(map
->blocks_per_strip
) / 2;
8260 if (geo
->chunksize
!= current_chunk_size
) {
8261 u
->new_chunksize
= geo
->chunksize
/ 1024;
8263 "chunk size change from %i to %i\n",
8264 current_chunk_size
, u
->new_chunksize
);
8266 previous_level
= map
->raid_level
;
8269 if ((geo
->level
== 5) && (previous_level
== 0)) {
8270 struct mdinfo
*spares
= NULL
;
8272 u
->new_raid_disks
++;
8273 spares
= get_spares_for_grow(st
);
8274 if ((spares
== NULL
) || (spares
->array
.spare_disks
< 1)) {
8277 update_memory_size
= 0;
8278 dprintf("error: cannot get spare device "
8279 "for requested migration");
8284 dprintf("imsm: reshape update preparation : OK\n");
8287 return update_memory_size
;
8290 static void imsm_update_metadata_locally(struct supertype
*st
,
8293 struct metadata_update mu
;
8298 mu
.space_list
= NULL
;
8300 imsm_prepare_update(st
, &mu
);
8301 imsm_process_update(st
, &mu
);
8303 while (mu
.space_list
) {
8304 void **space
= mu
.space_list
;
8305 mu
.space_list
= *space
;
8310 /***************************************************************************
8311 * Function: imsm_analyze_change
8312 * Description: Function analyze change for single volume
8313 * and validate if transition is supported
8314 * Parameters: Geometry parameters, supertype structure
8315 * Returns: Operation type code on success, -1 if fail
8316 ****************************************************************************/
8317 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
8318 struct geo_params
*geo
)
8325 getinfo_super_imsm_volume(st
, &info
, NULL
);
8326 if ((geo
->level
!= info
.array
.level
) &&
8327 (geo
->level
>= 0) &&
8328 (geo
->level
!= UnSet
)) {
8329 switch (info
.array
.level
) {
8331 if (geo
->level
== 5) {
8332 change
= CH_MIGRATION
;
8333 if (geo
->layout
!= ALGORITHM_LEFT_ASYMMETRIC
) {
8335 Name
" Error. Requested Layout "
8336 "not supported (left-asymmetric layout "
8337 "is supported only)!\n");
8339 goto analyse_change_exit
;
8343 if (geo
->level
== 10) {
8344 change
= CH_TAKEOVER
;
8349 if (geo
->level
== 0) {
8350 change
= CH_TAKEOVER
;
8355 if (geo
->level
== 0) {
8356 change
= CH_TAKEOVER
;
8363 Name
" Error. Level Migration from %d to %d "
8365 info
.array
.level
, geo
->level
);
8366 goto analyse_change_exit
;
8369 geo
->level
= info
.array
.level
;
8371 if ((geo
->layout
!= info
.array
.layout
)
8372 && ((geo
->layout
!= UnSet
) && (geo
->layout
!= -1))) {
8373 change
= CH_MIGRATION
;
8374 if ((info
.array
.layout
== 0)
8375 && (info
.array
.level
== 5)
8376 && (geo
->layout
== 5)) {
8377 /* reshape 5 -> 4 */
8378 } else if ((info
.array
.layout
== 5)
8379 && (info
.array
.level
== 5)
8380 && (geo
->layout
== 0)) {
8381 /* reshape 4 -> 5 */
8386 Name
" Error. Layout Migration from %d to %d "
8388 info
.array
.layout
, geo
->layout
);
8390 goto analyse_change_exit
;
8393 geo
->layout
= info
.array
.layout
;
8395 if ((geo
->chunksize
> 0) && (geo
->chunksize
!= UnSet
)
8396 && (geo
->chunksize
!= info
.array
.chunk_size
))
8397 change
= CH_MIGRATION
;
8399 geo
->chunksize
= info
.array
.chunk_size
;
8401 chunk
= geo
->chunksize
/ 1024;
8402 if (!validate_geometry_imsm(st
,
8412 struct intel_super
*super
= st
->sb
;
8413 struct imsm_super
*mpb
= super
->anchor
;
8415 if (mpb
->num_raid_devs
> 1) {
8417 Name
" Error. Cannot perform operation on %s"
8418 "- for this operation it MUST be single "
8419 "array in container\n",
8425 analyse_change_exit
:
8430 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
8432 struct intel_super
*super
= st
->sb
;
8433 struct imsm_update_takeover
*u
;
8435 u
= malloc(sizeof(struct imsm_update_takeover
));
8439 u
->type
= update_takeover
;
8440 u
->subarray
= super
->current_vol
;
8442 /* 10->0 transition */
8443 if (geo
->level
== 0)
8444 u
->direction
= R10_TO_R0
;
8446 /* 0->10 transition */
8447 if (geo
->level
== 10)
8448 u
->direction
= R0_TO_R10
;
8450 /* update metadata locally */
8451 imsm_update_metadata_locally(st
, u
,
8452 sizeof(struct imsm_update_takeover
));
8453 /* and possibly remotely */
8454 if (st
->update_tail
)
8455 append_metadata_update(st
, u
,
8456 sizeof(struct imsm_update_takeover
));
8463 static int imsm_reshape_super(struct supertype
*st
, long long size
, int level
,
8464 int layout
, int chunksize
, int raid_disks
,
8465 int delta_disks
, char *backup
, char *dev
,
8469 struct geo_params geo
;
8471 dprintf("imsm: reshape_super called.\n");
8473 memset(&geo
, 0, sizeof(struct geo_params
));
8476 geo
.dev_id
= st
->devnum
;
8479 geo
.layout
= layout
;
8480 geo
.chunksize
= chunksize
;
8481 geo
.raid_disks
= raid_disks
;
8482 if (delta_disks
!= UnSet
)
8483 geo
.raid_disks
+= delta_disks
;
8485 dprintf("\tfor level : %i\n", geo
.level
);
8486 dprintf("\tfor raid_disks : %i\n", geo
.raid_disks
);
8488 if (experimental() == 0)
8491 if (st
->container_dev
== st
->devnum
) {
8492 /* On container level we can only increase number of devices. */
8493 dprintf("imsm: info: Container operation\n");
8494 int old_raid_disks
= 0;
8496 if (imsm_reshape_is_allowed_on_container(
8497 st
, &geo
, &old_raid_disks
)) {
8498 struct imsm_update_reshape
*u
= NULL
;
8501 len
= imsm_create_metadata_update_for_reshape(
8502 st
, &geo
, old_raid_disks
, &u
);
8505 dprintf("imsm: Cannot prepare update\n");
8506 goto exit_imsm_reshape_super
;
8510 /* update metadata locally */
8511 imsm_update_metadata_locally(st
, u
, len
);
8512 /* and possibly remotely */
8513 if (st
->update_tail
)
8514 append_metadata_update(st
, u
, len
);
8519 fprintf(stderr
, Name
": (imsm) Operation "
8520 "is not allowed on this container\n");
8523 /* On volume level we support following operations
8524 * - takeover: raid10 -> raid0; raid0 -> raid10
8525 * - chunk size migration
8526 * - migration: raid5 -> raid0; raid0 -> raid5
8528 struct intel_super
*super
= st
->sb
;
8529 struct intel_dev
*dev
= super
->devlist
;
8531 dprintf("imsm: info: Volume operation\n");
8532 /* find requested device */
8534 imsm_find_array_minor_by_subdev(dev
->index
, st
->container_dev
, &devnum
);
8535 if (devnum
== geo
.dev_id
)
8540 fprintf(stderr
, Name
" Cannot find %s (%i) subarray\n",
8541 geo
.dev_name
, geo
.dev_id
);
8542 goto exit_imsm_reshape_super
;
8544 super
->current_vol
= dev
->index
;
8545 change
= imsm_analyze_change(st
, &geo
);
8548 ret_val
= imsm_takeover(st
, &geo
);
8550 case CH_MIGRATION
: {
8551 struct imsm_update_reshape_migration
*u
= NULL
;
8553 imsm_create_metadata_update_for_migration(
8557 "Cannot prepare update\n");
8561 /* update metadata locally */
8562 imsm_update_metadata_locally(st
, u
, len
);
8563 /* and possibly remotely */
8564 if (st
->update_tail
)
8565 append_metadata_update(st
, u
, len
);
8575 exit_imsm_reshape_super
:
8576 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
8580 /*******************************************************************************
8581 * Function: wait_for_reshape_imsm
8582 * Description: Function writes new sync_max value and waits until
8583 * reshape process reach new position
8585 * sra : general array info
8586 * ndata : number of disks in new array's layout
8589 * 1 : there is no reshape in progress,
8591 ******************************************************************************/
8592 int wait_for_reshape_imsm(struct mdinfo
*sra
, int ndata
)
8594 int fd
= sysfs_get_fd(sra
, NULL
, "reshape_position");
8595 unsigned long long completed
;
8596 /* to_complete : new sync_max position */
8597 unsigned long long to_complete
= sra
->reshape_progress
;
8598 unsigned long long position_to_set
= to_complete
/ ndata
;
8601 dprintf("imsm: wait_for_reshape_imsm() "
8602 "cannot open reshape_position\n");
8606 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
8607 dprintf("imsm: wait_for_reshape_imsm() "
8608 "cannot read reshape_position (no reshape in progres)\n");
8613 if (completed
> to_complete
) {
8614 dprintf("imsm: wait_for_reshape_imsm() "
8615 "wrong next position to set %llu (%llu)\n",
8616 to_complete
, completed
);
8620 dprintf("Position set: %llu\n", position_to_set
);
8621 if (sysfs_set_num(sra
, NULL
, "sync_max",
8622 position_to_set
) != 0) {
8623 dprintf("imsm: wait_for_reshape_imsm() "
8624 "cannot set reshape position to %llu\n",
8635 select(fd
+1, &rfds
, NULL
, NULL
, NULL
);
8636 if (sysfs_get_str(sra
, NULL
, "sync_action",
8638 strncmp(action
, "reshape", 7) != 0)
8640 if (sysfs_fd_get_ll(fd
, &completed
) < 0) {
8641 dprintf("imsm: wait_for_reshape_imsm() "
8642 "cannot read reshape_position (in loop)\n");
8646 } while (completed
< to_complete
);
8652 /*******************************************************************************
8653 * Function: check_degradation_change
8654 * Description: Check that array hasn't become failed.
8656 * info : for sysfs access
8657 * sources : source disks descriptors
8658 * degraded: previous degradation level
8661 ******************************************************************************/
8662 int check_degradation_change(struct mdinfo
*info
,
8666 unsigned long long new_degraded
;
8667 sysfs_get_ll(info
, NULL
, "degraded", &new_degraded
);
8668 if (new_degraded
!= (unsigned long long)degraded
) {
8669 /* check each device to ensure it is still working */
8672 for (sd
= info
->devs
; sd
; sd
= sd
->next
) {
8673 if (sd
->disk
.state
& (1<<MD_DISK_FAULTY
))
8675 if (sd
->disk
.state
& (1<<MD_DISK_SYNC
)) {
8677 if (sysfs_get_str(info
,
8678 sd
, "state", sbuf
, 20) < 0 ||
8679 strstr(sbuf
, "faulty") ||
8680 strstr(sbuf
, "in_sync") == NULL
) {
8681 /* this device is dead */
8682 sd
->disk
.state
= (1<<MD_DISK_FAULTY
);
8683 if (sd
->disk
.raid_disk
>= 0 &&
8684 sources
[sd
->disk
.raid_disk
] >= 0) {
8686 sd
->disk
.raid_disk
]);
8687 sources
[sd
->disk
.raid_disk
] =
8696 return new_degraded
;
8699 /*******************************************************************************
8700 * Function: imsm_manage_reshape
8701 * Description: Function finds array under reshape and it manages reshape
8702 * process. It creates stripes backups (if required) and sets
8705 * afd : Backup handle (nattive) - not used
8706 * sra : general array info
8707 * reshape : reshape parameters - not used
8708 * st : supertype structure
8709 * blocks : size of critical section [blocks]
8710 * fds : table of source device descriptor
8711 * offsets : start of array (offest per devices)
8713 * destfd : table of destination device descriptor
8714 * destoffsets : table of destination offsets (per device)
8716 * 1 : success, reshape is done
8718 ******************************************************************************/
8719 static int imsm_manage_reshape(
8720 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
8721 struct supertype
*st
, unsigned long backup_blocks
,
8722 int *fds
, unsigned long long *offsets
,
8723 int dests
, int *destfd
, unsigned long long *destoffsets
)
8726 struct intel_super
*super
= st
->sb
;
8727 struct intel_dev
*dv
= NULL
;
8728 struct imsm_dev
*dev
= NULL
;
8729 struct imsm_map
*map_src
;
8730 int migr_vol_qan
= 0;
8731 int ndata
, odata
; /* [bytes] */
8732 int chunk
; /* [bytes] */
8733 struct migr_record
*migr_rec
;
8735 unsigned int buf_size
; /* [bytes] */
8736 unsigned long long max_position
; /* array size [bytes] */
8737 unsigned long long next_step
; /* [blocks]/[bytes] */
8738 unsigned long long old_data_stripe_length
;
8739 unsigned long long start_src
; /* [bytes] */
8740 unsigned long long start
; /* [bytes] */
8741 unsigned long long start_buf_shift
; /* [bytes] */
8743 int source_layout
= 0;
8745 if (!fds
|| !offsets
|| !sra
)
8748 /* Find volume during the reshape */
8749 for (dv
= super
->devlist
; dv
; dv
= dv
->next
) {
8750 if (dv
->dev
->vol
.migr_type
== MIGR_GEN_MIGR
8751 && dv
->dev
->vol
.migr_state
== 1) {
8756 /* Only one volume can migrate at the same time */
8757 if (migr_vol_qan
!= 1) {
8758 fprintf(stderr
, Name
" : %s", migr_vol_qan
?
8759 "Number of migrating volumes greater than 1\n" :
8760 "There is no volume during migrationg\n");
8764 map_src
= get_imsm_map(dev
, 1);
8765 if (map_src
== NULL
)
8768 ndata
= imsm_num_data_members(dev
, 0);
8769 odata
= imsm_num_data_members(dev
, 1);
8771 chunk
= __le16_to_cpu(map_src
->blocks_per_strip
) * 512;
8772 old_data_stripe_length
= odata
* chunk
;
8774 migr_rec
= super
->migr_rec
;
8776 /* initialize migration record for start condition */
8777 if (sra
->reshape_progress
== 0)
8778 init_migr_record_imsm(st
, dev
, sra
);
8780 if (__le32_to_cpu(migr_rec
->rec_status
) != UNIT_SRC_NORMAL
) {
8781 dprintf("imsm: cannot restart migration when data "
8782 "are present in copy area.\n");
8788 buf_size
= __le32_to_cpu(migr_rec
->blocks_per_unit
) * 512;
8789 /* extend buffer size for parity disk */
8790 buf_size
+= __le32_to_cpu(migr_rec
->dest_depth_per_unit
) * 512;
8791 /* add space for stripe aligment */
8792 buf_size
+= old_data_stripe_length
;
8793 if (posix_memalign((void **)&buf
, 4096, buf_size
)) {
8794 dprintf("imsm: Cannot allocate checpoint buffer\n");
8798 max_position
= sra
->component_size
* ndata
;
8799 source_layout
= imsm_level_to_layout(map_src
->raid_level
);
8801 while (__le32_to_cpu(migr_rec
->curr_migr_unit
) <
8802 __le32_to_cpu(migr_rec
->num_migr_units
)) {
8803 /* current reshape position [blocks] */
8804 unsigned long long current_position
=
8805 __le32_to_cpu(migr_rec
->blocks_per_unit
)
8806 * __le32_to_cpu(migr_rec
->curr_migr_unit
);
8807 unsigned long long border
;
8809 /* Check that array hasn't become failed.
8811 degraded
= check_degradation_change(sra
, fds
, degraded
);
8813 dprintf("imsm: Abort reshape due to degradation"
8814 " level (%i)\n", degraded
);
8818 next_step
= __le32_to_cpu(migr_rec
->blocks_per_unit
);
8820 if ((current_position
+ next_step
) > max_position
)
8821 next_step
= max_position
- current_position
;
8823 start
= current_position
* 512;
8825 /* allign reading start to old geometry */
8826 start_buf_shift
= start
% old_data_stripe_length
;
8827 start_src
= start
- start_buf_shift
;
8829 border
= (start_src
/ odata
) - (start
/ ndata
);
8831 if (border
<= __le32_to_cpu(migr_rec
->dest_depth_per_unit
)) {
8832 /* save critical stripes to buf
8833 * start - start address of current unit
8835 * start_src - start address of current unit
8836 * to backup alligned to source array
8839 unsigned long long next_step_filler
= 0;
8840 unsigned long long copy_length
= next_step
* 512;
8842 /* allign copy area length to stripe in old geometry */
8843 next_step_filler
= ((copy_length
+ start_buf_shift
)
8844 % old_data_stripe_length
);
8845 if (next_step_filler
)
8846 next_step_filler
= (old_data_stripe_length
8847 - next_step_filler
);
8848 dprintf("save_stripes() parameters: start = %llu,"
8849 "\tstart_src = %llu,\tnext_step*512 = %llu,"
8850 "\tstart_in_buf_shift = %llu,"
8851 "\tnext_step_filler = %llu\n",
8852 start
, start_src
, copy_length
,
8853 start_buf_shift
, next_step_filler
);
8855 if (save_stripes(fds
, offsets
, map_src
->num_members
,
8856 chunk
, map_src
->raid_level
,
8857 source_layout
, 0, NULL
, start_src
,
8859 next_step_filler
+ start_buf_shift
,
8861 dprintf("imsm: Cannot save stripes"
8865 /* Convert data to destination format and store it
8866 * in backup general migration area
8868 if (save_backup_imsm(st
, dev
, sra
,
8869 buf
+ start_buf_shift
, copy_length
)) {
8870 dprintf("imsm: Cannot save stripes to "
8871 "target devices\n");
8874 if (save_checkpoint_imsm(st
, sra
,
8875 UNIT_SRC_IN_CP_AREA
)) {
8876 dprintf("imsm: Cannot write checkpoint to "
8877 "migration record (UNIT_SRC_IN_CP_AREA)\n");
8881 /* set next step to use whole border area */
8882 border
/= next_step
;
8884 next_step
*= border
;
8886 /* When data backed up, checkpoint stored,
8887 * kick the kernel to reshape unit of data
8889 next_step
= next_step
+ sra
->reshape_progress
;
8890 /* limit next step to array max position */
8891 if (next_step
> max_position
)
8892 next_step
= max_position
;
8893 sysfs_set_num(sra
, NULL
, "suspend_lo", sra
->reshape_progress
);
8894 sysfs_set_num(sra
, NULL
, "suspend_hi", next_step
);
8895 sra
->reshape_progress
= next_step
;
8897 /* wait until reshape finish */
8898 if (wait_for_reshape_imsm(sra
, ndata
) < 0) {
8899 dprintf("wait_for_reshape_imsm returned error!\n");
8903 if (save_checkpoint_imsm(st
, sra
, UNIT_SRC_NORMAL
) == 1) {
8904 /* ignore error == 2, this can mean end of reshape here
8906 dprintf("imsm: Cannot write checkpoint to "
8907 "migration record (UNIT_SRC_NORMAL)\n");
8913 /* return '1' if done */
8921 #endif /* MDASSEMBLE */
8923 struct superswitch super_imsm
= {
8925 .examine_super
= examine_super_imsm
,
8926 .brief_examine_super
= brief_examine_super_imsm
,
8927 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
8928 .export_examine_super
= export_examine_super_imsm
,
8929 .detail_super
= detail_super_imsm
,
8930 .brief_detail_super
= brief_detail_super_imsm
,
8931 .write_init_super
= write_init_super_imsm
,
8932 .validate_geometry
= validate_geometry_imsm
,
8933 .add_to_super
= add_to_super_imsm
,
8934 .remove_from_super
= remove_from_super_imsm
,
8935 .detail_platform
= detail_platform_imsm
,
8936 .kill_subarray
= kill_subarray_imsm
,
8937 .update_subarray
= update_subarray_imsm
,
8938 .load_container
= load_container_imsm
,
8939 .default_geometry
= default_geometry_imsm
,
8940 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
8941 .reshape_super
= imsm_reshape_super
,
8942 .manage_reshape
= imsm_manage_reshape
,
8944 .match_home
= match_home_imsm
,
8945 .uuid_from_super
= uuid_from_super_imsm
,
8946 .getinfo_super
= getinfo_super_imsm
,
8947 .getinfo_super_disks
= getinfo_super_disks_imsm
,
8948 .update_super
= update_super_imsm
,
8950 .avail_size
= avail_size_imsm
,
8951 .min_acceptable_spare_size
= min_acceptable_spare_size_imsm
,
8953 .compare_super
= compare_super_imsm
,
8955 .load_super
= load_super_imsm
,
8956 .init_super
= init_super_imsm
,
8957 .store_super
= store_super_imsm
,
8958 .free_super
= free_super_imsm
,
8959 .match_metadata_desc
= match_metadata_desc_imsm
,
8960 .container_content
= container_content_imsm
,
8962 .recover_backup
= recover_backup_imsm
,
8969 .open_new
= imsm_open_new
,
8970 .set_array_state
= imsm_set_array_state
,
8971 .set_disk
= imsm_set_disk
,
8972 .sync_metadata
= imsm_sync_metadata
,
8973 .activate_spare
= imsm_activate_spare
,
8974 .process_update
= imsm_process_update
,
8975 .prepare_update
= imsm_prepare_update
,
8976 #endif /* MDASSEMBLE */