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 418
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 static __u8
migr_type(struct imsm_dev
*dev
)
199 if (dev
->vol
.migr_type
== MIGR_VERIFY
&&
200 dev
->status
& DEV_VERIFY_AND_FIX
)
203 return dev
->vol
.migr_type
;
206 static void set_migr_type(struct imsm_dev
*dev
, __u8 migr_type
)
208 /* for compatibility with older oroms convert MIGR_REPAIR, into
209 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
211 if (migr_type
== MIGR_REPAIR
) {
212 dev
->vol
.migr_type
= MIGR_VERIFY
;
213 dev
->status
|= DEV_VERIFY_AND_FIX
;
215 dev
->vol
.migr_type
= migr_type
;
216 dev
->status
&= ~DEV_VERIFY_AND_FIX
;
220 static unsigned int sector_count(__u32 bytes
)
222 return ((bytes
+ (512-1)) & (~(512-1))) / 512;
225 static unsigned int mpb_sectors(struct imsm_super
*mpb
)
227 return sector_count(__le32_to_cpu(mpb
->mpb_size
));
231 struct imsm_dev
*dev
;
232 struct intel_dev
*next
;
237 enum sys_dev_type type
;
240 struct intel_hba
*next
;
247 /* internal representation of IMSM metadata */
250 void *buf
; /* O_DIRECT buffer for reading/writing metadata */
251 struct imsm_super
*anchor
; /* immovable parameters */
253 size_t len
; /* size of the 'buf' allocation */
254 void *next_buf
; /* for realloc'ing buf from the manager */
256 int updates_pending
; /* count of pending updates for mdmon */
257 int current_vol
; /* index of raid device undergoing creation */
258 __u32 create_offset
; /* common start for 'current_vol' */
259 __u32 random
; /* random data for seeding new family numbers */
260 struct intel_dev
*devlist
;
264 __u8 serial
[MAX_RAID_SERIAL_LEN
];
267 struct imsm_disk disk
;
270 struct extent
*e
; /* for determining freespace @ create */
271 int raiddisk
; /* slot to fill in autolayout */
274 struct dl
*disk_mgmt_list
; /* list of disks to add/remove while mdmon
276 struct dl
*missing
; /* disks removed while we weren't looking */
277 struct bbm_log
*bbm_log
;
278 struct intel_hba
*hba
; /* device path of the raid controller for this metadata */
279 const struct imsm_orom
*orom
; /* platform firmware support */
280 struct intel_super
*next
; /* (temp) list for disambiguating family_num */
284 struct imsm_disk disk
;
285 #define IMSM_UNKNOWN_OWNER (-1)
287 struct intel_disk
*next
;
291 unsigned long long start
, size
;
294 /* definitions of reshape process types */
295 enum imsm_reshape_type
{
300 /* definition of messages passed to imsm_process_update */
301 enum imsm_update_type
{
302 update_activate_spare
,
306 update_add_remove_disk
,
307 update_reshape_container_disks
,
311 struct imsm_update_activate_spare
{
312 enum imsm_update_type type
;
316 struct imsm_update_activate_spare
*next
;
329 enum takeover_direction
{
333 struct imsm_update_takeover
{
334 enum imsm_update_type type
;
336 enum takeover_direction direction
;
339 struct imsm_update_reshape
{
340 enum imsm_update_type type
;
343 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
347 __u8 serial
[MAX_RAID_SERIAL_LEN
];
350 struct imsm_update_create_array
{
351 enum imsm_update_type type
;
356 struct imsm_update_kill_array
{
357 enum imsm_update_type type
;
361 struct imsm_update_rename_array
{
362 enum imsm_update_type type
;
363 __u8 name
[MAX_RAID_SERIAL_LEN
];
367 struct imsm_update_add_remove_disk
{
368 enum imsm_update_type type
;
372 static const char *_sys_dev_type
[] = {
373 [SYS_DEV_UNKNOWN
] = "Unknown",
374 [SYS_DEV_SAS
] = "SAS",
375 [SYS_DEV_SATA
] = "SATA"
378 const char *get_sys_dev_type(enum sys_dev_type type
)
380 if (type
>= SYS_DEV_MAX
)
381 type
= SYS_DEV_UNKNOWN
;
383 return _sys_dev_type
[type
];
387 static struct intel_hba
* alloc_intel_hba(struct sys_dev
*device
)
389 struct intel_hba
*result
= malloc(sizeof(*result
));
391 result
->type
= device
->type
;
392 result
->path
= strdup(device
->path
);
394 if (result
->path
&& (result
->pci_id
= strrchr(result
->path
, '/')) != NULL
)
400 static struct intel_hba
* find_intel_hba(struct intel_hba
*hba
, struct sys_dev
*device
)
402 struct intel_hba
*result
=NULL
;
403 for (result
= hba
; result
; result
= result
->next
) {
404 if (result
->type
== device
->type
&& strcmp(result
->path
, device
->path
) == 0)
411 static int attach_hba_to_super(struct intel_super
*super
, struct sys_dev
*device
)
413 struct intel_hba
*hba
;
415 /* check if disk attached to Intel HBA */
416 hba
= find_intel_hba(super
->hba
, device
);
419 /* Check if HBA is already attached to super */
420 if (super
->hba
== NULL
) {
421 super
->hba
= alloc_intel_hba(device
);
426 /* Intel metadata allows for all disks attached to the same type HBA.
427 * Do not sypport odf HBA types mixing
429 if (device
->type
!= hba
->type
)
435 hba
->next
= alloc_intel_hba(device
);
439 static struct sys_dev
* find_disk_attached_hba(int fd
, const char *devname
)
441 struct sys_dev
*list
, *elem
, *prev
;
444 if ((list
= find_intel_devices()) == NULL
)
448 disk_path
= (char *) devname
;
450 disk_path
= diskfd_to_devpath(fd
);
457 for (prev
= NULL
, elem
= list
; elem
; prev
= elem
, elem
= elem
->next
) {
458 if (path_attached_to_hba(disk_path
, elem
->path
)) {
462 prev
->next
= elem
->next
;
464 if (disk_path
!= devname
)
470 if (disk_path
!= devname
)
476 #endif /* MDASSEMBLE */
479 static struct supertype
*match_metadata_desc_imsm(char *arg
)
481 struct supertype
*st
;
483 if (strcmp(arg
, "imsm") != 0 &&
484 strcmp(arg
, "default") != 0
488 st
= malloc(sizeof(*st
));
491 memset(st
, 0, sizeof(*st
));
492 st
->container_dev
= NoMdDev
;
493 st
->ss
= &super_imsm
;
494 st
->max_devs
= IMSM_MAX_DEVICES
;
495 st
->minor_version
= 0;
501 static __u8
*get_imsm_version(struct imsm_super
*mpb
)
503 return &mpb
->sig
[MPB_SIG_LEN
];
507 /* retrieve a disk directly from the anchor when the anchor is known to be
508 * up-to-date, currently only at load time
510 static struct imsm_disk
*__get_imsm_disk(struct imsm_super
*mpb
, __u8 index
)
512 if (index
>= mpb
->num_disks
)
514 return &mpb
->disk
[index
];
517 /* retrieve the disk description based on a index of the disk
520 static struct dl
*get_imsm_dl_disk(struct intel_super
*super
, __u8 index
)
524 for (d
= super
->disks
; d
; d
= d
->next
)
525 if (d
->index
== index
)
530 /* retrieve a disk from the parsed metadata */
531 static struct imsm_disk
*get_imsm_disk(struct intel_super
*super
, __u8 index
)
535 dl
= get_imsm_dl_disk(super
, index
);
542 /* generate a checksum directly from the anchor when the anchor is known to be
543 * up-to-date, currently only at load or write_super after coalescing
545 static __u32
__gen_imsm_checksum(struct imsm_super
*mpb
)
547 __u32 end
= mpb
->mpb_size
/ sizeof(end
);
548 __u32
*p
= (__u32
*) mpb
;
552 sum
+= __le32_to_cpu(*p
);
556 return sum
- __le32_to_cpu(mpb
->check_sum
);
559 static size_t sizeof_imsm_map(struct imsm_map
*map
)
561 return sizeof(struct imsm_map
) + sizeof(__u32
) * (map
->num_members
- 1);
564 struct imsm_map
*get_imsm_map(struct imsm_dev
*dev
, int second_map
)
566 /* A device can have 2 maps if it is in the middle of a migration.
568 * 0 - we return the first map
569 * 1 - we return the second map if it exists, else NULL
570 * -1 - we return the second map if it exists, else the first
572 struct imsm_map
*map
= &dev
->vol
.map
[0];
574 if (second_map
== 1 && !dev
->vol
.migr_state
)
576 else if (second_map
== 1 ||
577 (second_map
< 0 && dev
->vol
.migr_state
)) {
580 return ptr
+ sizeof_imsm_map(map
);
586 /* return the size of the device.
587 * migr_state increases the returned size if map[0] were to be duplicated
589 static size_t sizeof_imsm_dev(struct imsm_dev
*dev
, int migr_state
)
591 size_t size
= sizeof(*dev
) - sizeof(struct imsm_map
) +
592 sizeof_imsm_map(get_imsm_map(dev
, 0));
594 /* migrating means an additional map */
595 if (dev
->vol
.migr_state
)
596 size
+= sizeof_imsm_map(get_imsm_map(dev
, 1));
598 size
+= sizeof_imsm_map(get_imsm_map(dev
, 0));
604 /* retrieve disk serial number list from a metadata update */
605 static struct disk_info
*get_disk_info(struct imsm_update_create_array
*update
)
608 struct disk_info
*inf
;
610 inf
= u
+ sizeof(*update
) - sizeof(struct imsm_dev
) +
611 sizeof_imsm_dev(&update
->dev
, 0);
617 static struct imsm_dev
*__get_imsm_dev(struct imsm_super
*mpb
, __u8 index
)
623 if (index
>= mpb
->num_raid_devs
)
626 /* devices start after all disks */
627 offset
= ((void *) &mpb
->disk
[mpb
->num_disks
]) - _mpb
;
629 for (i
= 0; i
<= index
; i
++)
631 return _mpb
+ offset
;
633 offset
+= sizeof_imsm_dev(_mpb
+ offset
, 0);
638 static struct imsm_dev
*get_imsm_dev(struct intel_super
*super
, __u8 index
)
640 struct intel_dev
*dv
;
642 if (index
>= super
->anchor
->num_raid_devs
)
644 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
645 if (dv
->index
== index
)
653 * == 1 get second map
654 * == -1 than get map according to the current migr_state
656 static __u32
get_imsm_ord_tbl_ent(struct imsm_dev
*dev
,
660 struct imsm_map
*map
;
662 map
= get_imsm_map(dev
, second_map
);
664 /* top byte identifies disk under rebuild */
665 return __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
668 #define ord_to_idx(ord) (((ord) << 8) >> 8)
669 static __u32
get_imsm_disk_idx(struct imsm_dev
*dev
, int slot
, int second_map
)
671 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, second_map
);
673 return ord_to_idx(ord
);
676 static void set_imsm_ord_tbl_ent(struct imsm_map
*map
, int slot
, __u32 ord
)
678 map
->disk_ord_tbl
[slot
] = __cpu_to_le32(ord
);
681 static int get_imsm_disk_slot(struct imsm_map
*map
, unsigned idx
)
686 for (slot
= 0; slot
< map
->num_members
; slot
++) {
687 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
688 if (ord_to_idx(ord
) == idx
)
695 static int get_imsm_raid_level(struct imsm_map
*map
)
697 if (map
->raid_level
== 1) {
698 if (map
->num_members
== 2)
704 return map
->raid_level
;
707 static int cmp_extent(const void *av
, const void *bv
)
709 const struct extent
*a
= av
;
710 const struct extent
*b
= bv
;
711 if (a
->start
< b
->start
)
713 if (a
->start
> b
->start
)
718 static int count_memberships(struct dl
*dl
, struct intel_super
*super
)
723 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
724 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
725 struct imsm_map
*map
= get_imsm_map(dev
, 0);
727 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
734 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
)
736 /* find a list of used extents on the given physical device */
737 struct extent
*rv
, *e
;
739 int memberships
= count_memberships(dl
, super
);
740 __u32 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
742 rv
= malloc(sizeof(struct extent
) * (memberships
+ 1));
747 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
748 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
749 struct imsm_map
*map
= get_imsm_map(dev
, 0);
751 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
752 e
->start
= __le32_to_cpu(map
->pba_of_lba0
);
753 e
->size
= __le32_to_cpu(map
->blocks_per_member
);
757 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
759 /* determine the start of the metadata
760 * when no raid devices are defined use the default
761 * ...otherwise allow the metadata to truncate the value
762 * as is the case with older versions of imsm
765 struct extent
*last
= &rv
[memberships
- 1];
768 remainder
= __le32_to_cpu(dl
->disk
.total_blocks
) -
769 (last
->start
+ last
->size
);
770 /* round down to 1k block to satisfy precision of the kernel
774 /* make sure remainder is still sane */
775 if (remainder
< (unsigned)ROUND_UP(super
->len
, 512) >> 9)
776 remainder
= ROUND_UP(super
->len
, 512) >> 9;
777 if (reservation
> remainder
)
778 reservation
= remainder
;
780 e
->start
= __le32_to_cpu(dl
->disk
.total_blocks
) - reservation
;
785 /* try to determine how much space is reserved for metadata from
786 * the last get_extents() entry, otherwise fallback to the
789 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
795 /* for spares just return a minimal reservation which will grow
796 * once the spare is picked up by an array
799 return MPB_SECTOR_CNT
;
801 e
= get_extents(super
, dl
);
803 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
805 /* scroll to last entry */
806 for (i
= 0; e
[i
].size
; i
++)
809 rv
= __le32_to_cpu(dl
->disk
.total_blocks
) - e
[i
].start
;
816 static int is_spare(struct imsm_disk
*disk
)
818 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
821 static int is_configured(struct imsm_disk
*disk
)
823 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
826 static int is_failed(struct imsm_disk
*disk
)
828 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
831 /* Return minimum size of a spare that can be used in this array*/
832 static unsigned long long min_acceptable_spare_size_imsm(struct supertype
*st
)
834 struct intel_super
*super
= st
->sb
;
838 unsigned long long rv
= 0;
842 /* find first active disk in array */
844 while (dl
&& (is_failed(&dl
->disk
) || dl
->index
== -1))
848 /* find last lba used by subarrays */
849 e
= get_extents(super
, dl
);
852 for (i
= 0; e
[i
].size
; i
++)
855 rv
= e
[i
-1].start
+ e
[i
-1].size
;
857 /* add the amount of space needed for metadata */
858 rv
= rv
+ MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
863 static __u64
blocks_per_migr_unit(struct imsm_dev
*dev
);
865 static void print_imsm_dev(struct imsm_dev
*dev
, char *uuid
, int disk_idx
)
869 struct imsm_map
*map
= get_imsm_map(dev
, 0);
870 struct imsm_map
*map2
= get_imsm_map(dev
, 1);
874 printf("[%.16s]:\n", dev
->volume
);
875 printf(" UUID : %s\n", uuid
);
876 printf(" RAID Level : %d", get_imsm_raid_level(map
));
878 printf(" <-- %d", get_imsm_raid_level(map2
));
880 printf(" Members : %d", map
->num_members
);
882 printf(" <-- %d", map2
->num_members
);
884 printf(" Slots : [");
885 for (i
= 0; i
< map
->num_members
; i
++) {
886 ord
= get_imsm_ord_tbl_ent(dev
, i
, 0);
887 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
892 for (i
= 0; i
< map2
->num_members
; i
++) {
893 ord
= get_imsm_ord_tbl_ent(dev
, i
, 1);
894 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
899 printf(" Failed disk : ");
900 if (map
->failed_disk_num
== 0xff)
903 printf("%i", map
->failed_disk_num
);
905 slot
= get_imsm_disk_slot(map
, disk_idx
);
907 ord
= get_imsm_ord_tbl_ent(dev
, slot
, -1);
908 printf(" This Slot : %d%s\n", slot
,
909 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
911 printf(" This Slot : ?\n");
912 sz
= __le32_to_cpu(dev
->size_high
);
914 sz
+= __le32_to_cpu(dev
->size_low
);
915 printf(" Array Size : %llu%s\n", (unsigned long long)sz
,
916 human_size(sz
* 512));
917 sz
= __le32_to_cpu(map
->blocks_per_member
);
918 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz
,
919 human_size(sz
* 512));
920 printf(" Sector Offset : %u\n",
921 __le32_to_cpu(map
->pba_of_lba0
));
922 printf(" Num Stripes : %u\n",
923 __le32_to_cpu(map
->num_data_stripes
));
924 printf(" Chunk Size : %u KiB",
925 __le16_to_cpu(map
->blocks_per_strip
) / 2);
927 printf(" <-- %u KiB",
928 __le16_to_cpu(map2
->blocks_per_strip
) / 2);
930 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
931 printf(" Migrate State : ");
932 if (dev
->vol
.migr_state
) {
933 if (migr_type(dev
) == MIGR_INIT
)
934 printf("initialize\n");
935 else if (migr_type(dev
) == MIGR_REBUILD
)
937 else if (migr_type(dev
) == MIGR_VERIFY
)
939 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
940 printf("general migration\n");
941 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
942 printf("state change\n");
943 else if (migr_type(dev
) == MIGR_REPAIR
)
946 printf("<unknown:%d>\n", migr_type(dev
));
949 printf(" Map State : %s", map_state_str
[map
->map_state
]);
950 if (dev
->vol
.migr_state
) {
951 struct imsm_map
*map
= get_imsm_map(dev
, 1);
953 printf(" <-- %s", map_state_str
[map
->map_state
]);
954 printf("\n Checkpoint : %u (%llu)",
955 __le32_to_cpu(dev
->vol
.curr_migr_unit
),
956 (unsigned long long)blocks_per_migr_unit(dev
));
959 printf(" Dirty State : %s\n", dev
->vol
.dirty
? "dirty" : "clean");
962 static void print_imsm_disk(struct imsm_super
*mpb
, int index
, __u32 reserved
)
964 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, index
);
965 char str
[MAX_RAID_SERIAL_LEN
+ 1];
968 if (index
< 0 || !disk
)
972 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
973 printf(" Disk%02d Serial : %s\n", index
, str
);
974 printf(" State :%s%s%s\n", is_spare(disk
) ? " spare" : "",
975 is_configured(disk
) ? " active" : "",
976 is_failed(disk
) ? " failed" : "");
977 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
978 sz
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
979 printf(" Usable Size : %llu%s\n", (unsigned long long)sz
,
980 human_size(sz
* 512));
983 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
985 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
987 struct intel_super
*super
= st
->sb
;
988 struct imsm_super
*mpb
= super
->anchor
;
989 char str
[MAX_SIGNATURE_LENGTH
];
994 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
997 snprintf(str
, MPB_SIG_LEN
, "%s", mpb
->sig
);
998 printf(" Magic : %s\n", str
);
999 snprintf(str
, strlen(MPB_VERSION_RAID0
), "%s", get_imsm_version(mpb
));
1000 printf(" Version : %s\n", get_imsm_version(mpb
));
1001 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
1002 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
1003 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
1004 getinfo_super_imsm(st
, &info
, NULL
);
1005 fname_from_uuid(st
, &info
, nbuf
, ':');
1006 printf(" UUID : %s\n", nbuf
+ 5);
1007 sum
= __le32_to_cpu(mpb
->check_sum
);
1008 printf(" Checksum : %08x %s\n", sum
,
1009 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
1010 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
));
1011 printf(" Disks : %d\n", mpb
->num_disks
);
1012 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
1013 print_imsm_disk(mpb
, super
->disks
->index
, reserved
);
1014 if (super
->bbm_log
) {
1015 struct bbm_log
*log
= super
->bbm_log
;
1018 printf("Bad Block Management Log:\n");
1019 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
1020 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
1021 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
1022 printf(" Spare Blocks : %d\n", __le32_to_cpu(log
->reserved_spare_block_count
));
1023 printf(" First Spare : %llx\n",
1024 (unsigned long long) __le64_to_cpu(log
->first_spare_lba
));
1026 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1028 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
1030 super
->current_vol
= i
;
1031 getinfo_super_imsm(st
, &info
, NULL
);
1032 fname_from_uuid(st
, &info
, nbuf
, ':');
1033 print_imsm_dev(dev
, nbuf
+ 5, super
->disks
->index
);
1035 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1036 if (i
== super
->disks
->index
)
1038 print_imsm_disk(mpb
, i
, reserved
);
1040 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
1041 struct imsm_disk
*disk
;
1042 char str
[MAX_RAID_SERIAL_LEN
+ 1];
1050 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
1051 printf(" Disk Serial : %s\n", str
);
1052 printf(" State :%s%s%s\n", is_spare(disk
) ? " spare" : "",
1053 is_configured(disk
) ? " active" : "",
1054 is_failed(disk
) ? " failed" : "");
1055 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
1056 sz
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
1057 printf(" Usable Size : %llu%s\n", (unsigned long long)sz
,
1058 human_size(sz
* 512));
1062 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
1064 /* We just write a generic IMSM ARRAY entry */
1067 struct intel_super
*super
= st
->sb
;
1069 if (!super
->anchor
->num_raid_devs
) {
1070 printf("ARRAY metadata=imsm\n");
1074 getinfo_super_imsm(st
, &info
, NULL
);
1075 fname_from_uuid(st
, &info
, nbuf
, ':');
1076 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
1079 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
1081 /* We just write a generic IMSM ARRAY entry */
1085 struct intel_super
*super
= st
->sb
;
1088 if (!super
->anchor
->num_raid_devs
)
1091 getinfo_super_imsm(st
, &info
, NULL
);
1092 fname_from_uuid(st
, &info
, nbuf
, ':');
1093 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
1094 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1096 super
->current_vol
= i
;
1097 getinfo_super_imsm(st
, &info
, NULL
);
1098 fname_from_uuid(st
, &info
, nbuf1
, ':');
1099 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
1100 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
1104 static void export_examine_super_imsm(struct supertype
*st
)
1106 struct intel_super
*super
= st
->sb
;
1107 struct imsm_super
*mpb
= super
->anchor
;
1111 getinfo_super_imsm(st
, &info
, NULL
);
1112 fname_from_uuid(st
, &info
, nbuf
, ':');
1113 printf("MD_METADATA=imsm\n");
1114 printf("MD_LEVEL=container\n");
1115 printf("MD_UUID=%s\n", nbuf
+5);
1116 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
1119 static void detail_super_imsm(struct supertype
*st
, char *homehost
)
1124 getinfo_super_imsm(st
, &info
, NULL
);
1125 fname_from_uuid(st
, &info
, nbuf
, ':');
1126 printf("\n UUID : %s\n", nbuf
+ 5);
1129 static void brief_detail_super_imsm(struct supertype
*st
)
1133 getinfo_super_imsm(st
, &info
, NULL
);
1134 fname_from_uuid(st
, &info
, nbuf
, ':');
1135 printf(" UUID=%s", nbuf
+ 5);
1138 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
);
1139 static void fd2devname(int fd
, char *name
);
1141 static int ahci_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
1143 /* dump an unsorted list of devices attached to AHCI Intel storage
1144 * controller, as well as non-connected ports
1146 int hba_len
= strlen(hba_path
) + 1;
1151 unsigned long port_mask
= (1 << port_count
) - 1;
1153 if (port_count
> (int)sizeof(port_mask
) * 8) {
1155 fprintf(stderr
, Name
": port_count %d out of range\n", port_count
);
1159 /* scroll through /sys/dev/block looking for devices attached to
1162 dir
= opendir("/sys/dev/block");
1163 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
1174 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
1176 path
= devt_to_devpath(makedev(major
, minor
));
1179 if (!path_attached_to_hba(path
, hba_path
)) {
1185 /* retrieve the scsi device type */
1186 if (asprintf(&device
, "/sys/dev/block/%d:%d/device/xxxxxxx", major
, minor
) < 0) {
1188 fprintf(stderr
, Name
": failed to allocate 'device'\n");
1192 sprintf(device
, "/sys/dev/block/%d:%d/device/type", major
, minor
);
1193 if (load_sys(device
, buf
) != 0) {
1195 fprintf(stderr
, Name
": failed to read device type for %s\n",
1201 type
= strtoul(buf
, NULL
, 10);
1203 /* if it's not a disk print the vendor and model */
1204 if (!(type
== 0 || type
== 7 || type
== 14)) {
1207 sprintf(device
, "/sys/dev/block/%d:%d/device/vendor", major
, minor
);
1208 if (load_sys(device
, buf
) == 0) {
1209 strncpy(vendor
, buf
, sizeof(vendor
));
1210 vendor
[sizeof(vendor
) - 1] = '\0';
1211 c
= (char *) &vendor
[sizeof(vendor
) - 1];
1212 while (isspace(*c
) || *c
== '\0')
1216 sprintf(device
, "/sys/dev/block/%d:%d/device/model", major
, minor
);
1217 if (load_sys(device
, buf
) == 0) {
1218 strncpy(model
, buf
, sizeof(model
));
1219 model
[sizeof(model
) - 1] = '\0';
1220 c
= (char *) &model
[sizeof(model
) - 1];
1221 while (isspace(*c
) || *c
== '\0')
1225 if (vendor
[0] && model
[0])
1226 sprintf(buf
, "%.64s %.64s", vendor
, model
);
1228 switch (type
) { /* numbers from hald/linux/device.c */
1229 case 1: sprintf(buf
, "tape"); break;
1230 case 2: sprintf(buf
, "printer"); break;
1231 case 3: sprintf(buf
, "processor"); break;
1233 case 5: sprintf(buf
, "cdrom"); break;
1234 case 6: sprintf(buf
, "scanner"); break;
1235 case 8: sprintf(buf
, "media_changer"); break;
1236 case 9: sprintf(buf
, "comm"); break;
1237 case 12: sprintf(buf
, "raid"); break;
1238 default: sprintf(buf
, "unknown");
1244 /* chop device path to 'host%d' and calculate the port number */
1245 c
= strchr(&path
[hba_len
], '/');
1248 fprintf(stderr
, Name
": %s - invalid path name\n", path
+ hba_len
);
1253 if (sscanf(&path
[hba_len
], "host%d", &port
) == 1)
1257 *c
= '/'; /* repair the full string */
1258 fprintf(stderr
, Name
": failed to determine port number for %s\n",
1265 /* mark this port as used */
1266 port_mask
&= ~(1 << port
);
1268 /* print out the device information */
1270 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
1274 fd
= dev_open(ent
->d_name
, O_RDONLY
);
1276 printf(" Port%d : - disk info unavailable -\n", port
);
1278 fd2devname(fd
, buf
);
1279 printf(" Port%d : %s", port
, buf
);
1280 if (imsm_read_serial(fd
, NULL
, (__u8
*) buf
) == 0)
1281 printf(" (%s)\n", buf
);
1296 for (i
= 0; i
< port_count
; i
++)
1297 if (port_mask
& (1 << i
))
1298 printf(" Port%d : - no device attached -\n", i
);
1306 static void print_found_intel_controllers(struct sys_dev
*elem
)
1308 for (; elem
; elem
= elem
->next
) {
1309 fprintf(stderr
, Name
": found Intel(R) ");
1310 if (elem
->type
== SYS_DEV_SATA
)
1311 fprintf(stderr
, "SATA ");
1312 else if (elem
->type
== SYS_DEV_SAS
)
1313 fprintf(stderr
, "SAS ");
1314 fprintf(stderr
, "RAID controller");
1316 fprintf(stderr
, " at %s", elem
->pci_id
);
1317 fprintf(stderr
, ".\n");
1322 static int ahci_get_port_count(const char *hba_path
, int *port_count
)
1329 if ((dir
= opendir(hba_path
)) == NULL
)
1332 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
1335 if (sscanf(ent
->d_name
, "host%d", &host
) != 1)
1337 if (*port_count
== 0)
1339 else if (host
< host_base
)
1342 if (host
+ 1 > *port_count
+ host_base
)
1343 *port_count
= host
+ 1 - host_base
;
1349 static void print_imsm_capability(const struct imsm_orom
*orom
)
1351 printf(" Platform : Intel(R) Matrix Storage Manager\n");
1352 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
1353 orom
->hotfix_ver
, orom
->build
);
1354 printf(" RAID Levels :%s%s%s%s%s\n",
1355 imsm_orom_has_raid0(orom
) ? " raid0" : "",
1356 imsm_orom_has_raid1(orom
) ? " raid1" : "",
1357 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
1358 imsm_orom_has_raid10(orom
) ? " raid10" : "",
1359 imsm_orom_has_raid5(orom
) ? " raid5" : "");
1360 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1361 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
1362 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
1363 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
1364 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
1365 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
1366 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
1367 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
1368 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
1369 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
1370 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
1371 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
1372 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
1373 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
1374 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
1375 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
1376 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
1377 printf(" Max Disks : %d\n", orom
->tds
);
1378 printf(" Max Volumes : %d\n", orom
->vpa
);
1382 static int detail_platform_imsm(int verbose
, int enumerate_only
)
1384 /* There are two components to imsm platform support, the ahci SATA
1385 * controller and the option-rom. To find the SATA controller we
1386 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
1387 * controller with the Intel vendor id is present. This approach
1388 * allows mdadm to leverage the kernel's ahci detection logic, with the
1389 * caveat that if ahci.ko is not loaded mdadm will not be able to
1390 * detect platform raid capabilities. The option-rom resides in a
1391 * platform "Adapter ROM". We scan for its signature to retrieve the
1392 * platform capabilities. If raid support is disabled in the BIOS the
1393 * option-rom capability structure will not be available.
1395 const struct imsm_orom
*orom
;
1396 struct sys_dev
*list
, *hba
;
1401 if (enumerate_only
) {
1402 if (check_env("IMSM_NO_PLATFORM"))
1404 list
= find_intel_devices();
1407 for (hba
= list
; hba
; hba
= hba
->next
) {
1408 orom
= find_imsm_capability(hba
->type
);
1414 free_sys_dev(&list
);
1418 list
= find_intel_devices();
1421 fprintf(stderr
, Name
": no active Intel(R) RAID "
1422 "controller found.\n");
1423 free_sys_dev(&list
);
1426 print_found_intel_controllers(list
);
1428 for (hba
= list
; hba
; hba
= hba
->next
) {
1429 orom
= find_imsm_capability(hba
->type
);
1431 fprintf(stderr
, Name
": imsm capabilities not found for controller: %s (type %s)\n",
1432 hba
->path
, get_sys_dev_type(hba
->type
));
1434 print_imsm_capability(orom
);
1437 for (hba
= list
; hba
; hba
= hba
->next
) {
1438 printf(" I/O Controller : %s (%s)\n",
1439 hba
->path
, get_sys_dev_type(hba
->type
));
1441 if (hba
->type
== SYS_DEV_SATA
) {
1442 host_base
= ahci_get_port_count(hba
->path
, &port_count
);
1443 if (ahci_enumerate_ports(hba
->path
, port_count
, host_base
, verbose
)) {
1445 fprintf(stderr
, Name
": failed to enumerate "
1446 "ports on SATA controller at %s.", hba
->pci_id
);
1452 free_sys_dev(&list
);
1457 static int match_home_imsm(struct supertype
*st
, char *homehost
)
1459 /* the imsm metadata format does not specify any host
1460 * identification information. We return -1 since we can never
1461 * confirm nor deny whether a given array is "meant" for this
1462 * host. We rely on compare_super and the 'family_num' fields to
1463 * exclude member disks that do not belong, and we rely on
1464 * mdadm.conf to specify the arrays that should be assembled.
1465 * Auto-assembly may still pick up "foreign" arrays.
1471 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
1473 /* The uuid returned here is used for:
1474 * uuid to put into bitmap file (Create, Grow)
1475 * uuid for backup header when saving critical section (Grow)
1476 * comparing uuids when re-adding a device into an array
1477 * In these cases the uuid required is that of the data-array,
1478 * not the device-set.
1479 * uuid to recognise same set when adding a missing device back
1480 * to an array. This is a uuid for the device-set.
1482 * For each of these we can make do with a truncated
1483 * or hashed uuid rather than the original, as long as
1485 * In each case the uuid required is that of the data-array,
1486 * not the device-set.
1488 /* imsm does not track uuid's so we synthesis one using sha1 on
1489 * - The signature (Which is constant for all imsm array, but no matter)
1490 * - the orig_family_num of the container
1491 * - the index number of the volume
1492 * - the 'serial' number of the volume.
1493 * Hopefully these are all constant.
1495 struct intel_super
*super
= st
->sb
;
1498 struct sha1_ctx ctx
;
1499 struct imsm_dev
*dev
= NULL
;
1502 /* some mdadm versions failed to set ->orig_family_num, in which
1503 * case fall back to ->family_num. orig_family_num will be
1504 * fixed up with the first metadata update.
1506 family_num
= super
->anchor
->orig_family_num
;
1507 if (family_num
== 0)
1508 family_num
= super
->anchor
->family_num
;
1509 sha1_init_ctx(&ctx
);
1510 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
1511 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
1512 if (super
->current_vol
>= 0)
1513 dev
= get_imsm_dev(super
, super
->current_vol
);
1515 __u32 vol
= super
->current_vol
;
1516 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
1517 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
1519 sha1_finish_ctx(&ctx
, buf
);
1520 memcpy(uuid
, buf
, 4*4);
1525 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
1527 __u8
*v
= get_imsm_version(mpb
);
1528 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
1529 char major
[] = { 0, 0, 0 };
1530 char minor
[] = { 0 ,0, 0 };
1531 char patch
[] = { 0, 0, 0 };
1532 char *ver_parse
[] = { major
, minor
, patch
};
1536 while (*v
!= '\0' && v
< end
) {
1537 if (*v
!= '.' && j
< 2)
1538 ver_parse
[i
][j
++] = *v
;
1546 *m
= strtol(minor
, NULL
, 0);
1547 *p
= strtol(patch
, NULL
, 0);
1551 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
1553 /* migr_strip_size when repairing or initializing parity */
1554 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1555 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1557 switch (get_imsm_raid_level(map
)) {
1562 return 128*1024 >> 9;
1566 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
1568 /* migr_strip_size when rebuilding a degraded disk, no idea why
1569 * this is different than migr_strip_size_resync(), but it's good
1572 struct imsm_map
*map
= get_imsm_map(dev
, 1);
1573 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1575 switch (get_imsm_raid_level(map
)) {
1578 if (map
->num_members
% map
->num_domains
== 0)
1579 return 128*1024 >> 9;
1583 return max((__u32
) 64*1024 >> 9, chunk
);
1585 return 128*1024 >> 9;
1589 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
1591 struct imsm_map
*lo
= get_imsm_map(dev
, 0);
1592 struct imsm_map
*hi
= get_imsm_map(dev
, 1);
1593 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
1594 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
1596 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
1599 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
1601 struct imsm_map
*lo
= get_imsm_map(dev
, 0);
1602 int level
= get_imsm_raid_level(lo
);
1604 if (level
== 1 || level
== 10) {
1605 struct imsm_map
*hi
= get_imsm_map(dev
, 1);
1607 return hi
->num_domains
;
1609 return num_stripes_per_unit_resync(dev
);
1612 static __u8
imsm_num_data_members(struct imsm_dev
*dev
, int second_map
)
1614 /* named 'imsm_' because raid0, raid1 and raid10
1615 * counter-intuitively have the same number of data disks
1617 struct imsm_map
*map
= get_imsm_map(dev
, second_map
);
1619 switch (get_imsm_raid_level(map
)) {
1623 return map
->num_members
;
1625 return map
->num_members
- 1;
1627 dprintf("%s: unsupported raid level\n", __func__
);
1632 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
1634 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1635 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1637 switch(get_imsm_raid_level(map
)) {
1640 return chunk
* map
->num_domains
;
1642 return chunk
* map
->num_members
;
1648 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
1650 struct imsm_map
*map
= get_imsm_map(dev
, 1);
1651 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1652 __u32 strip
= block
/ chunk
;
1654 switch (get_imsm_raid_level(map
)) {
1657 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
1658 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
1660 return vol_stripe
* chunk
+ block
% chunk
;
1662 __u32 stripe
= strip
/ (map
->num_members
- 1);
1664 return stripe
* chunk
+ block
% chunk
;
1671 static __u64
blocks_per_migr_unit(struct imsm_dev
*dev
)
1673 /* calculate the conversion factor between per member 'blocks'
1674 * (md/{resync,rebuild}_start) and imsm migration units, return
1675 * 0 for the 'not migrating' and 'unsupported migration' cases
1677 if (!dev
->vol
.migr_state
)
1680 switch (migr_type(dev
)) {
1685 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1686 __u32 stripes_per_unit
;
1687 __u32 blocks_per_unit
;
1696 /* yes, this is really the translation of migr_units to
1697 * per-member blocks in the 'resync' case
1699 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
1700 migr_chunk
= migr_strip_blocks_resync(dev
);
1701 disks
= imsm_num_data_members(dev
, 0);
1702 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
1703 if (migr_type(dev
) == MIGR_GEN_MIGR
)
1704 return blocks_per_unit
;
1705 stripe
= __le32_to_cpu(map
->blocks_per_strip
) * disks
;
1706 segment
= blocks_per_unit
/ stripe
;
1707 block_rel
= blocks_per_unit
- segment
* stripe
;
1708 parity_depth
= parity_segment_depth(dev
);
1709 block_map
= map_migr_block(dev
, block_rel
);
1710 return block_map
+ parity_depth
* segment
;
1712 case MIGR_REBUILD
: {
1713 __u32 stripes_per_unit
;
1716 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
1717 migr_chunk
= migr_strip_blocks_rebuild(dev
);
1718 return migr_chunk
* stripes_per_unit
;
1720 case MIGR_STATE_CHANGE
:
1726 static int imsm_level_to_layout(int level
)
1734 return ALGORITHM_LEFT_ASYMMETRIC
;
1741 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
1743 struct intel_super
*super
= st
->sb
;
1744 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
1745 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1746 struct imsm_map
*prev_map
= get_imsm_map(dev
, 1);
1747 struct imsm_map
*map_to_analyse
= map
;
1750 int map_disks
= info
->array
.raid_disks
;
1753 map_to_analyse
= prev_map
;
1755 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
1756 if (dl
->raiddisk
== info
->disk
.raid_disk
)
1758 info
->container_member
= super
->current_vol
;
1759 info
->array
.raid_disks
= map_to_analyse
->num_members
;
1760 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
1761 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
1762 info
->array
.md_minor
= -1;
1763 info
->array
.ctime
= 0;
1764 info
->array
.utime
= 0;
1765 info
->array
.chunk_size
=
1766 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
1767 info
->array
.state
= !dev
->vol
.dirty
;
1768 info
->custom_array_size
= __le32_to_cpu(dev
->size_high
);
1769 info
->custom_array_size
<<= 32;
1770 info
->custom_array_size
|= __le32_to_cpu(dev
->size_low
);
1771 if (prev_map
&& map
->map_state
== prev_map
->map_state
) {
1772 info
->reshape_active
= 1;
1773 info
->new_level
= get_imsm_raid_level(map
);
1774 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
1775 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
1776 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
1777 if (info
->delta_disks
) {
1778 /* this needs to be applied to every array
1781 info
->reshape_active
= 2;
1783 /* We shape information that we give to md might have to be
1784 * modify to cope with md's requirement for reshaping arrays.
1785 * For example, when reshaping a RAID0, md requires it to be
1786 * presented as a degraded RAID4.
1787 * Also if a RAID0 is migrating to a RAID5 we need to specify
1788 * the array as already being RAID5, but the 'before' layout
1789 * is a RAID4-like layout.
1791 switch (info
->array
.level
) {
1793 switch(info
->new_level
) {
1795 /* conversion is happening as RAID4 */
1796 info
->array
.level
= 4;
1797 info
->array
.raid_disks
+= 1;
1800 /* conversion is happening as RAID5 */
1801 info
->array
.level
= 5;
1802 info
->array
.layout
= ALGORITHM_PARITY_N
;
1803 info
->array
.raid_disks
+= 1;
1804 info
->delta_disks
-= 1;
1807 /* FIXME error message */
1808 info
->array
.level
= UnSet
;
1814 info
->new_level
= UnSet
;
1815 info
->new_layout
= UnSet
;
1816 info
->new_chunk
= info
->array
.chunk_size
;
1817 info
->delta_disks
= 0;
1819 info
->disk
.major
= 0;
1820 info
->disk
.minor
= 0;
1822 info
->disk
.major
= dl
->major
;
1823 info
->disk
.minor
= dl
->minor
;
1826 info
->data_offset
= __le32_to_cpu(map_to_analyse
->pba_of_lba0
);
1827 info
->component_size
=
1828 __le32_to_cpu(map_to_analyse
->blocks_per_member
);
1829 memset(info
->uuid
, 0, sizeof(info
->uuid
));
1830 info
->recovery_start
= MaxSector
;
1832 info
->reshape_progress
= 0;
1833 info
->resync_start
= MaxSector
;
1834 if (map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
1836 info
->resync_start
= 0;
1838 if (dev
->vol
.migr_state
) {
1839 switch (migr_type(dev
)) {
1842 __u64 blocks_per_unit
= blocks_per_migr_unit(dev
);
1843 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
1845 info
->resync_start
= blocks_per_unit
* units
;
1848 case MIGR_GEN_MIGR
: {
1849 __u64 blocks_per_unit
= blocks_per_migr_unit(dev
);
1850 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
1851 unsigned long long array_blocks
;
1854 info
->reshape_progress
= blocks_per_unit
* units
;
1855 dprintf("IMSM: General Migration checkpoint : %llu "
1856 "(%llu) -> read reshape progress : %llu\n",
1857 units
, blocks_per_unit
, info
->reshape_progress
);
1859 used_disks
= imsm_num_data_members(dev
, 1);
1860 if (used_disks
> 0) {
1861 array_blocks
= map
->blocks_per_member
*
1863 /* round array size down to closest MB
1865 info
->custom_array_size
= (array_blocks
1866 >> SECT_PER_MB_SHIFT
)
1867 << SECT_PER_MB_SHIFT
;
1871 /* we could emulate the checkpointing of
1872 * 'sync_action=check' migrations, but for now
1873 * we just immediately complete them
1876 /* this is handled by container_content_imsm() */
1877 case MIGR_STATE_CHANGE
:
1878 /* FIXME handle other migrations */
1880 /* we are not dirty, so... */
1881 info
->resync_start
= MaxSector
;
1885 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
1886 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
1888 info
->array
.major_version
= -1;
1889 info
->array
.minor_version
= -2;
1890 devname
= devnum2devname(st
->container_dev
);
1891 *info
->text_version
= '\0';
1893 sprintf(info
->text_version
, "/%s/%d", devname
, info
->container_member
);
1895 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
1896 uuid_from_super_imsm(st
, info
->uuid
);
1900 for (i
=0; i
<map_disks
; i
++) {
1902 if (i
< info
->array
.raid_disks
) {
1903 struct imsm_disk
*dsk
;
1904 j
= get_imsm_disk_idx(dev
, i
, -1);
1905 dsk
= get_imsm_disk(super
, j
);
1906 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
1913 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
, int failed
);
1914 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
);
1916 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
1920 for (d
= super
->missing
; d
; d
= d
->next
)
1921 if (d
->index
== index
)
1926 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
1928 struct intel_super
*super
= st
->sb
;
1929 struct imsm_disk
*disk
;
1930 int map_disks
= info
->array
.raid_disks
;
1931 int max_enough
= -1;
1933 struct imsm_super
*mpb
;
1935 if (super
->current_vol
>= 0) {
1936 getinfo_super_imsm_volume(st
, info
, map
);
1940 /* Set raid_disks to zero so that Assemble will always pull in valid
1943 info
->array
.raid_disks
= 0;
1944 info
->array
.level
= LEVEL_CONTAINER
;
1945 info
->array
.layout
= 0;
1946 info
->array
.md_minor
= -1;
1947 info
->array
.ctime
= 0; /* N/A for imsm */
1948 info
->array
.utime
= 0;
1949 info
->array
.chunk_size
= 0;
1951 info
->disk
.major
= 0;
1952 info
->disk
.minor
= 0;
1953 info
->disk
.raid_disk
= -1;
1954 info
->reshape_active
= 0;
1955 info
->array
.major_version
= -1;
1956 info
->array
.minor_version
= -2;
1957 strcpy(info
->text_version
, "imsm");
1958 info
->safe_mode_delay
= 0;
1959 info
->disk
.number
= -1;
1960 info
->disk
.state
= 0;
1962 info
->recovery_start
= MaxSector
;
1964 /* do we have the all the insync disks that we expect? */
1965 mpb
= super
->anchor
;
1967 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1968 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1969 int failed
, enough
, j
, missing
= 0;
1970 struct imsm_map
*map
;
1973 failed
= imsm_count_failed(super
, dev
);
1974 state
= imsm_check_degraded(super
, dev
, failed
);
1975 map
= get_imsm_map(dev
, dev
->vol
.migr_state
);
1977 /* any newly missing disks?
1978 * (catches single-degraded vs double-degraded)
1980 for (j
= 0; j
< map
->num_members
; j
++) {
1981 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, -1);
1982 __u32 idx
= ord_to_idx(ord
);
1984 if (!(ord
& IMSM_ORD_REBUILD
) &&
1985 get_imsm_missing(super
, idx
)) {
1991 if (state
== IMSM_T_STATE_FAILED
)
1993 else if (state
== IMSM_T_STATE_DEGRADED
&&
1994 (state
!= map
->map_state
|| missing
))
1996 else /* we're normal, or already degraded */
1999 /* in the missing/failed disk case check to see
2000 * if at least one array is runnable
2002 max_enough
= max(max_enough
, enough
);
2004 dprintf("%s: enough: %d\n", __func__
, max_enough
);
2005 info
->container_enough
= max_enough
;
2008 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
2010 disk
= &super
->disks
->disk
;
2011 info
->data_offset
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
2012 info
->component_size
= reserved
;
2013 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
2014 /* we don't change info->disk.raid_disk here because
2015 * this state will be finalized in mdmon after we have
2016 * found the 'most fresh' version of the metadata
2018 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
2019 info
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
2022 /* only call uuid_from_super_imsm when this disk is part of a populated container,
2023 * ->compare_super may have updated the 'num_raid_devs' field for spares
2025 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
2026 uuid_from_super_imsm(st
, info
->uuid
);
2028 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
2030 /* I don't know how to compute 'map' on imsm, so use safe default */
2033 for (i
= 0; i
< map_disks
; i
++)
2039 /* allocates memory and fills disk in mdinfo structure
2040 * for each disk in array */
2041 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
2043 struct mdinfo
*mddev
= NULL
;
2044 struct intel_super
*super
= st
->sb
;
2045 struct imsm_disk
*disk
;
2048 if (!super
|| !super
->disks
)
2051 mddev
= malloc(sizeof(*mddev
));
2053 fprintf(stderr
, Name
": Failed to allocate memory.\n");
2056 memset(mddev
, 0, sizeof(*mddev
));
2060 tmp
= malloc(sizeof(*tmp
));
2062 fprintf(stderr
, Name
": Failed to allocate memory.\n");
2067 memset(tmp
, 0, sizeof(*tmp
));
2069 tmp
->next
= mddev
->devs
;
2071 tmp
->disk
.number
= count
++;
2072 tmp
->disk
.major
= dl
->major
;
2073 tmp
->disk
.minor
= dl
->minor
;
2074 tmp
->disk
.state
= is_configured(disk
) ?
2075 (1 << MD_DISK_ACTIVE
) : 0;
2076 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
2077 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
2078 tmp
->disk
.raid_disk
= -1;
2084 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
2085 char *update
, char *devname
, int verbose
,
2086 int uuid_set
, char *homehost
)
2088 /* For 'assemble' and 'force' we need to return non-zero if any
2089 * change was made. For others, the return value is ignored.
2090 * Update options are:
2091 * force-one : This device looks a bit old but needs to be included,
2092 * update age info appropriately.
2093 * assemble: clear any 'faulty' flag to allow this device to
2095 * force-array: Array is degraded but being forced, mark it clean
2096 * if that will be needed to assemble it.
2098 * newdev: not used ????
2099 * grow: Array has gained a new device - this is currently for
2101 * resync: mark as dirty so a resync will happen.
2102 * name: update the name - preserving the homehost
2103 * uuid: Change the uuid of the array to match watch is given
2105 * Following are not relevant for this imsm:
2106 * sparc2.2 : update from old dodgey metadata
2107 * super-minor: change the preferred_minor number
2108 * summaries: update redundant counters.
2109 * homehost: update the recorded homehost
2110 * _reshape_progress: record new reshape_progress position.
2113 struct intel_super
*super
= st
->sb
;
2114 struct imsm_super
*mpb
;
2116 /* we can only update container info */
2117 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
2120 mpb
= super
->anchor
;
2122 if (strcmp(update
, "uuid") == 0 && uuid_set
&& !info
->update_private
)
2124 else if (strcmp(update
, "uuid") == 0 && uuid_set
&& info
->update_private
) {
2125 mpb
->orig_family_num
= *((__u32
*) info
->update_private
);
2127 } else if (strcmp(update
, "uuid") == 0) {
2128 __u32
*new_family
= malloc(sizeof(*new_family
));
2130 /* update orig_family_number with the incoming random
2131 * data, report the new effective uuid, and store the
2132 * new orig_family_num for future updates.
2135 memcpy(&mpb
->orig_family_num
, info
->uuid
, sizeof(__u32
));
2136 uuid_from_super_imsm(st
, info
->uuid
);
2137 *new_family
= mpb
->orig_family_num
;
2138 info
->update_private
= new_family
;
2141 } else if (strcmp(update
, "assemble") == 0)
2146 /* successful update? recompute checksum */
2148 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
2153 static size_t disks_to_mpb_size(int disks
)
2157 size
= sizeof(struct imsm_super
);
2158 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
2159 size
+= 2 * sizeof(struct imsm_dev
);
2160 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
2161 size
+= (4 - 2) * sizeof(struct imsm_map
);
2162 /* 4 possible disk_ord_tbl's */
2163 size
+= 4 * (disks
- 1) * sizeof(__u32
);
2168 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
)
2170 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
2173 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
2176 static void free_devlist(struct intel_super
*super
)
2178 struct intel_dev
*dv
;
2180 while (super
->devlist
) {
2181 dv
= super
->devlist
->next
;
2182 free(super
->devlist
->dev
);
2183 free(super
->devlist
);
2184 super
->devlist
= dv
;
2188 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
2190 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
2193 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
2197 * 0 same, or first was empty, and second was copied
2198 * 1 second had wrong number
2200 * 3 wrong other info
2202 struct intel_super
*first
= st
->sb
;
2203 struct intel_super
*sec
= tst
->sb
;
2211 /* if an anchor does not have num_raid_devs set then it is a free
2214 if (first
->anchor
->num_raid_devs
> 0 &&
2215 sec
->anchor
->num_raid_devs
> 0) {
2216 /* Determine if these disks might ever have been
2217 * related. Further disambiguation can only take place
2218 * in load_super_imsm_all
2220 __u32 first_family
= first
->anchor
->orig_family_num
;
2221 __u32 sec_family
= sec
->anchor
->orig_family_num
;
2223 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
2224 MAX_SIGNATURE_LENGTH
) != 0)
2227 if (first_family
== 0)
2228 first_family
= first
->anchor
->family_num
;
2229 if (sec_family
== 0)
2230 sec_family
= sec
->anchor
->family_num
;
2232 if (first_family
!= sec_family
)
2238 /* if 'first' is a spare promote it to a populated mpb with sec's
2241 if (first
->anchor
->num_raid_devs
== 0 &&
2242 sec
->anchor
->num_raid_devs
> 0) {
2244 struct intel_dev
*dv
;
2245 struct imsm_dev
*dev
;
2247 /* we need to copy raid device info from sec if an allocation
2248 * fails here we don't associate the spare
2250 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
2251 dv
= malloc(sizeof(*dv
));
2254 dev
= malloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
2261 dv
->next
= first
->devlist
;
2262 first
->devlist
= dv
;
2264 if (i
< sec
->anchor
->num_raid_devs
) {
2265 /* allocation failure */
2266 free_devlist(first
);
2267 fprintf(stderr
, "imsm: failed to associate spare\n");
2270 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
2271 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
2272 first
->anchor
->family_num
= sec
->anchor
->family_num
;
2273 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
2274 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
2275 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
2281 static void fd2devname(int fd
, char *name
)
2285 char dname
[PATH_MAX
];
2290 if (fstat(fd
, &st
) != 0)
2292 sprintf(path
, "/sys/dev/block/%d:%d",
2293 major(st
.st_rdev
), minor(st
.st_rdev
));
2295 rv
= readlink(path
, dname
, sizeof(dname
));
2300 nm
= strrchr(dname
, '/');
2302 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
2305 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
2307 static int imsm_read_serial(int fd
, char *devname
,
2308 __u8 serial
[MAX_RAID_SERIAL_LEN
])
2310 unsigned char scsi_serial
[255];
2319 memset(scsi_serial
, 0, sizeof(scsi_serial
));
2321 rv
= scsi_get_serial(fd
, scsi_serial
, sizeof(scsi_serial
));
2323 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
2324 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
2325 fd2devname(fd
, (char *) serial
);
2332 Name
": Failed to retrieve serial for %s\n",
2337 rsp_len
= scsi_serial
[3];
2341 Name
": Failed to retrieve serial for %s\n",
2345 rsp_buf
= (char *) &scsi_serial
[4];
2347 /* trim all whitespace and non-printable characters and convert
2350 for (i
= 0, dest
= rsp_buf
; i
< rsp_len
; i
++) {
2353 /* ':' is reserved for use in placeholder serial
2354 * numbers for missing disks
2362 len
= dest
- rsp_buf
;
2365 /* truncate leading characters */
2366 if (len
> MAX_RAID_SERIAL_LEN
) {
2367 dest
+= len
- MAX_RAID_SERIAL_LEN
;
2368 len
= MAX_RAID_SERIAL_LEN
;
2371 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
2372 memcpy(serial
, dest
, len
);
2377 static int serialcmp(__u8
*s1
, __u8
*s2
)
2379 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
2382 static void serialcpy(__u8
*dest
, __u8
*src
)
2384 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
2388 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
2392 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
2393 if (serialcmp(dl
->serial
, serial
) == 0)
2400 static struct imsm_disk
*
2401 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
2405 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2406 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
2408 if (serialcmp(disk
->serial
, serial
) == 0) {
2419 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
2421 struct imsm_disk
*disk
;
2426 __u8 serial
[MAX_RAID_SERIAL_LEN
];
2428 rv
= imsm_read_serial(fd
, devname
, serial
);
2433 dl
= calloc(1, sizeof(*dl
));
2437 Name
": failed to allocate disk buffer for %s\n",
2443 dl
->major
= major(stb
.st_rdev
);
2444 dl
->minor
= minor(stb
.st_rdev
);
2445 dl
->next
= super
->disks
;
2446 dl
->fd
= keep_fd
? fd
: -1;
2447 assert(super
->disks
== NULL
);
2449 serialcpy(dl
->serial
, serial
);
2452 fd2devname(fd
, name
);
2454 dl
->devname
= strdup(devname
);
2456 dl
->devname
= strdup(name
);
2458 /* look up this disk's index in the current anchor */
2459 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
2462 /* only set index on disks that are a member of a
2463 * populated contianer, i.e. one with raid_devs
2465 if (is_failed(&dl
->disk
))
2467 else if (is_spare(&dl
->disk
))
2475 /* When migrating map0 contains the 'destination' state while map1
2476 * contains the current state. When not migrating map0 contains the
2477 * current state. This routine assumes that map[0].map_state is set to
2478 * the current array state before being called.
2480 * Migration is indicated by one of the following states
2481 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
2482 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
2483 * map1state=unitialized)
2484 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
2486 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
2487 * map1state=degraded)
2489 static void migrate(struct imsm_dev
*dev
, __u8 to_state
, int migr_type
)
2491 struct imsm_map
*dest
;
2492 struct imsm_map
*src
= get_imsm_map(dev
, 0);
2494 dev
->vol
.migr_state
= 1;
2495 set_migr_type(dev
, migr_type
);
2496 dev
->vol
.curr_migr_unit
= 0;
2497 dest
= get_imsm_map(dev
, 1);
2499 /* duplicate and then set the target end state in map[0] */
2500 memcpy(dest
, src
, sizeof_imsm_map(src
));
2501 if ((migr_type
== MIGR_REBUILD
) ||
2502 (migr_type
== MIGR_GEN_MIGR
)) {
2506 for (i
= 0; i
< src
->num_members
; i
++) {
2507 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
2508 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
2512 src
->map_state
= to_state
;
2515 static void end_migration(struct imsm_dev
*dev
, __u8 map_state
)
2517 struct imsm_map
*map
= get_imsm_map(dev
, 0);
2518 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
);
2521 /* merge any IMSM_ORD_REBUILD bits that were not successfully
2522 * completed in the last migration.
2524 * FIXME add support for raid-level-migration
2526 for (i
= 0; i
< prev
->num_members
; i
++)
2527 for (j
= 0; j
< map
->num_members
; j
++)
2528 /* during online capacity expansion
2529 * disks position can be changed if takeover is used
2531 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
2532 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
2533 map
->disk_ord_tbl
[j
] |= prev
->disk_ord_tbl
[i
];
2537 dev
->vol
.migr_state
= 0;
2538 dev
->vol
.migr_type
= 0;
2539 dev
->vol
.curr_migr_unit
= 0;
2540 map
->map_state
= map_state
;
2544 static int parse_raid_devices(struct intel_super
*super
)
2547 struct imsm_dev
*dev_new
;
2548 size_t len
, len_migr
;
2550 size_t space_needed
= 0;
2551 struct imsm_super
*mpb
= super
->anchor
;
2553 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
2554 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
2555 struct intel_dev
*dv
;
2557 len
= sizeof_imsm_dev(dev_iter
, 0);
2558 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
2560 space_needed
+= len_migr
- len
;
2562 dv
= malloc(sizeof(*dv
));
2565 if (max_len
< len_migr
)
2567 if (max_len
> len_migr
)
2568 space_needed
+= max_len
- len_migr
;
2569 dev_new
= malloc(max_len
);
2574 imsm_copy_dev(dev_new
, dev_iter
);
2577 dv
->next
= super
->devlist
;
2578 super
->devlist
= dv
;
2581 /* ensure that super->buf is large enough when all raid devices
2584 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
2587 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
, 512);
2588 if (posix_memalign(&buf
, 512, len
) != 0)
2591 memcpy(buf
, super
->buf
, super
->len
);
2592 memset(buf
+ super
->len
, 0, len
- super
->len
);
2601 /* retrieve a pointer to the bbm log which starts after all raid devices */
2602 struct bbm_log
*__get_imsm_bbm_log(struct imsm_super
*mpb
)
2606 if (__le32_to_cpu(mpb
->bbm_log_size
)) {
2608 ptr
+= mpb
->mpb_size
- __le32_to_cpu(mpb
->bbm_log_size
);
2614 static void __free_imsm(struct intel_super
*super
, int free_disks
);
2616 /* load_imsm_mpb - read matrix metadata
2617 * allocates super->mpb to be freed by free_super
2619 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
2621 unsigned long long dsize
;
2622 unsigned long long sectors
;
2624 struct imsm_super
*anchor
;
2627 get_dev_size(fd
, NULL
, &dsize
);
2631 Name
": %s: device to small for imsm\n",
2636 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0) {
2639 Name
": Cannot seek to anchor block on %s: %s\n",
2640 devname
, strerror(errno
));
2644 if (posix_memalign((void**)&anchor
, 512, 512) != 0) {
2647 Name
": Failed to allocate imsm anchor buffer"
2648 " on %s\n", devname
);
2651 if (read(fd
, anchor
, 512) != 512) {
2654 Name
": Cannot read anchor block on %s: %s\n",
2655 devname
, strerror(errno
));
2660 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
2663 Name
": no IMSM anchor on %s\n", devname
);
2668 __free_imsm(super
, 0);
2669 super
->len
= ROUND_UP(anchor
->mpb_size
, 512);
2670 if (posix_memalign(&super
->buf
, 512, super
->len
) != 0) {
2673 Name
": unable to allocate %zu byte mpb buffer\n",
2678 memcpy(super
->buf
, anchor
, 512);
2680 sectors
= mpb_sectors(anchor
) - 1;
2683 check_sum
= __gen_imsm_checksum(super
->anchor
);
2684 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
2687 Name
": IMSM checksum %x != %x on %s\n",
2689 __le32_to_cpu(super
->anchor
->check_sum
),
2697 /* read the extended mpb */
2698 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0) {
2701 Name
": Cannot seek to extended mpb on %s: %s\n",
2702 devname
, strerror(errno
));
2706 if ((unsigned)read(fd
, super
->buf
+ 512, super
->len
- 512) != super
->len
- 512) {
2709 Name
": Cannot read extended mpb on %s: %s\n",
2710 devname
, strerror(errno
));
2714 check_sum
= __gen_imsm_checksum(super
->anchor
);
2715 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
2718 Name
": IMSM checksum %x != %x on %s\n",
2719 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
2724 /* FIXME the BBM log is disk specific so we cannot use this global
2725 * buffer for all disks. Ok for now since we only look at the global
2726 * bbm_log_size parameter to gate assembly
2728 super
->bbm_log
= __get_imsm_bbm_log(super
->anchor
);
2734 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
2738 err
= load_imsm_mpb(fd
, super
, devname
);
2741 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
2744 err
= parse_raid_devices(super
);
2749 static void __free_imsm_disk(struct dl
*d
)
2761 static void free_imsm_disks(struct intel_super
*super
)
2765 while (super
->disks
) {
2767 super
->disks
= d
->next
;
2768 __free_imsm_disk(d
);
2770 while (super
->disk_mgmt_list
) {
2771 d
= super
->disk_mgmt_list
;
2772 super
->disk_mgmt_list
= d
->next
;
2773 __free_imsm_disk(d
);
2775 while (super
->missing
) {
2777 super
->missing
= d
->next
;
2778 __free_imsm_disk(d
);
2783 /* free all the pieces hanging off of a super pointer */
2784 static void __free_imsm(struct intel_super
*super
, int free_disks
)
2786 struct intel_hba
*elem
, *next
;
2793 free_imsm_disks(super
);
2794 free_devlist(super
);
2798 free((void *)elem
->path
);
2806 static void free_imsm(struct intel_super
*super
)
2808 __free_imsm(super
, 1);
2812 static void free_super_imsm(struct supertype
*st
)
2814 struct intel_super
*super
= st
->sb
;
2823 static struct intel_super
*alloc_super(void)
2825 struct intel_super
*super
= malloc(sizeof(*super
));
2828 memset(super
, 0, sizeof(*super
));
2829 super
->current_vol
= -1;
2830 super
->create_offset
= ~((__u32
) 0);
2831 if (!check_env("IMSM_NO_PLATFORM"))
2832 super
->orom
= find_imsm_orom();
2839 /* find_missing - helper routine for load_super_imsm_all that identifies
2840 * disks that have disappeared from the system. This routine relies on
2841 * the mpb being uptodate, which it is at load time.
2843 static int find_missing(struct intel_super
*super
)
2846 struct imsm_super
*mpb
= super
->anchor
;
2848 struct imsm_disk
*disk
;
2850 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2851 disk
= __get_imsm_disk(mpb
, i
);
2852 dl
= serial_to_dl(disk
->serial
, super
);
2856 dl
= malloc(sizeof(*dl
));
2862 dl
->devname
= strdup("missing");
2864 serialcpy(dl
->serial
, disk
->serial
);
2867 dl
->next
= super
->missing
;
2868 super
->missing
= dl
;
2874 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
2876 struct intel_disk
*idisk
= disk_list
;
2879 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
2881 idisk
= idisk
->next
;
2887 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
2888 struct intel_super
*super
,
2889 struct intel_disk
**disk_list
)
2891 struct imsm_disk
*d
= &super
->disks
->disk
;
2892 struct imsm_super
*mpb
= super
->anchor
;
2895 for (i
= 0; i
< tbl_size
; i
++) {
2896 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
2897 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
2899 if (tbl_mpb
->family_num
== mpb
->family_num
) {
2900 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
2901 dprintf("%s: mpb from %d:%d matches %d:%d\n",
2902 __func__
, super
->disks
->major
,
2903 super
->disks
->minor
,
2904 table
[i
]->disks
->major
,
2905 table
[i
]->disks
->minor
);
2909 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
2910 is_configured(d
) == is_configured(tbl_d
)) &&
2911 tbl_mpb
->generation_num
< mpb
->generation_num
) {
2912 /* current version of the mpb is a
2913 * better candidate than the one in
2914 * super_table, but copy over "cross
2915 * generational" status
2917 struct intel_disk
*idisk
;
2919 dprintf("%s: mpb from %d:%d replaces %d:%d\n",
2920 __func__
, super
->disks
->major
,
2921 super
->disks
->minor
,
2922 table
[i
]->disks
->major
,
2923 table
[i
]->disks
->minor
);
2925 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
2926 if (idisk
&& is_failed(&idisk
->disk
))
2927 tbl_d
->status
|= FAILED_DISK
;
2930 struct intel_disk
*idisk
;
2931 struct imsm_disk
*disk
;
2933 /* tbl_mpb is more up to date, but copy
2934 * over cross generational status before
2937 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
2938 if (disk
&& is_failed(disk
))
2939 d
->status
|= FAILED_DISK
;
2941 idisk
= disk_list_get(d
->serial
, *disk_list
);
2944 if (disk
&& is_configured(disk
))
2945 idisk
->disk
.status
|= CONFIGURED_DISK
;
2948 dprintf("%s: mpb from %d:%d prefer %d:%d\n",
2949 __func__
, super
->disks
->major
,
2950 super
->disks
->minor
,
2951 table
[i
]->disks
->major
,
2952 table
[i
]->disks
->minor
);
2960 table
[tbl_size
++] = super
;
2964 /* update/extend the merged list of imsm_disk records */
2965 for (j
= 0; j
< mpb
->num_disks
; j
++) {
2966 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
2967 struct intel_disk
*idisk
;
2969 idisk
= disk_list_get(disk
->serial
, *disk_list
);
2971 idisk
->disk
.status
|= disk
->status
;
2972 if (is_configured(&idisk
->disk
) ||
2973 is_failed(&idisk
->disk
))
2974 idisk
->disk
.status
&= ~(SPARE_DISK
);
2976 idisk
= calloc(1, sizeof(*idisk
));
2979 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
2980 idisk
->disk
= *disk
;
2981 idisk
->next
= *disk_list
;
2985 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
2992 static struct intel_super
*
2993 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
2996 struct imsm_super
*mpb
= super
->anchor
;
3000 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3001 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
3002 struct intel_disk
*idisk
;
3004 idisk
= disk_list_get(disk
->serial
, disk_list
);
3006 if (idisk
->owner
== owner
||
3007 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
3010 dprintf("%s: '%.16s' owner %d != %d\n",
3011 __func__
, disk
->serial
, idisk
->owner
,
3014 dprintf("%s: unknown disk %x [%d]: %.16s\n",
3015 __func__
, __le32_to_cpu(mpb
->family_num
), i
,
3021 if (ok_count
== mpb
->num_disks
)
3026 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
3028 struct intel_super
*s
;
3030 for (s
= super_list
; s
; s
= s
->next
) {
3031 if (family_num
!= s
->anchor
->family_num
)
3033 fprintf(stderr
, "Conflict, offlining family %#x on '%s'\n",
3034 __le32_to_cpu(family_num
), s
->disks
->devname
);
3038 static struct intel_super
*
3039 imsm_thunderdome(struct intel_super
**super_list
, int len
)
3041 struct intel_super
*super_table
[len
];
3042 struct intel_disk
*disk_list
= NULL
;
3043 struct intel_super
*champion
, *spare
;
3044 struct intel_super
*s
, **del
;
3049 memset(super_table
, 0, sizeof(super_table
));
3050 for (s
= *super_list
; s
; s
= s
->next
)
3051 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
3053 for (i
= 0; i
< tbl_size
; i
++) {
3054 struct imsm_disk
*d
;
3055 struct intel_disk
*idisk
;
3056 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
3059 d
= &s
->disks
->disk
;
3061 /* 'd' must appear in merged disk list for its
3062 * configuration to be valid
3064 idisk
= disk_list_get(d
->serial
, disk_list
);
3065 if (idisk
&& idisk
->owner
== i
)
3066 s
= validate_members(s
, disk_list
, i
);
3071 dprintf("%s: marking family: %#x from %d:%d offline\n",
3072 __func__
, mpb
->family_num
,
3073 super_table
[i
]->disks
->major
,
3074 super_table
[i
]->disks
->minor
);
3078 /* This is where the mdadm implementation differs from the Windows
3079 * driver which has no strict concept of a container. We can only
3080 * assemble one family from a container, so when returning a prodigal
3081 * array member to this system the code will not be able to disambiguate
3082 * the container contents that should be assembled ("foreign" versus
3083 * "local"). It requires user intervention to set the orig_family_num
3084 * to a new value to establish a new container. The Windows driver in
3085 * this situation fixes up the volume name in place and manages the
3086 * foreign array as an independent entity.
3091 for (i
= 0; i
< tbl_size
; i
++) {
3092 struct intel_super
*tbl_ent
= super_table
[i
];
3098 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
3103 if (s
&& !is_spare
) {
3104 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
3106 } else if (!s
&& !is_spare
)
3119 fprintf(stderr
, "Chose family %#x on '%s', "
3120 "assemble conflicts to new container with '--update=uuid'\n",
3121 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
3123 /* collect all dl's onto 'champion', and update them to
3124 * champion's version of the status
3126 for (s
= *super_list
; s
; s
= s
->next
) {
3127 struct imsm_super
*mpb
= champion
->anchor
;
3128 struct dl
*dl
= s
->disks
;
3133 for (i
= 0; i
< mpb
->num_disks
; i
++) {
3134 struct imsm_disk
*disk
;
3136 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
3139 /* only set index on disks that are a member of
3140 * a populated contianer, i.e. one with
3143 if (is_failed(&dl
->disk
))
3145 else if (is_spare(&dl
->disk
))
3151 if (i
>= mpb
->num_disks
) {
3152 struct intel_disk
*idisk
;
3154 idisk
= disk_list_get(dl
->serial
, disk_list
);
3155 if (idisk
&& is_spare(&idisk
->disk
) &&
3156 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
3164 dl
->next
= champion
->disks
;
3165 champion
->disks
= dl
;
3169 /* delete 'champion' from super_list */
3170 for (del
= super_list
; *del
; ) {
3171 if (*del
== champion
) {
3172 *del
= (*del
)->next
;
3175 del
= &(*del
)->next
;
3177 champion
->next
= NULL
;
3181 struct intel_disk
*idisk
= disk_list
;
3183 disk_list
= disk_list
->next
;
3190 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
3194 struct intel_super
*super_list
= NULL
;
3195 struct intel_super
*super
= NULL
;
3196 int devnum
= fd2devnum(fd
);
3202 /* check if 'fd' an opened container */
3203 sra
= sysfs_read(fd
, 0, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
3207 if (sra
->array
.major_version
!= -1 ||
3208 sra
->array
.minor_version
!= -2 ||
3209 strcmp(sra
->text_version
, "imsm") != 0) {
3214 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
3215 struct intel_super
*s
= alloc_super();
3222 s
->next
= super_list
;
3226 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
3227 dfd
= dev_open(nm
, O_RDWR
);
3231 err
= load_and_parse_mpb(dfd
, s
, NULL
, 1);
3233 /* retry the load if we might have raced against mdmon */
3234 if (err
== 3 && mdmon_running(devnum
))
3235 for (retry
= 0; retry
< 3; retry
++) {
3237 err
= load_and_parse_mpb(dfd
, s
, NULL
, 1);
3245 /* all mpbs enter, maybe one leaves */
3246 super
= imsm_thunderdome(&super_list
, i
);
3252 if (find_missing(super
) != 0) {
3260 while (super_list
) {
3261 struct intel_super
*s
= super_list
;
3263 super_list
= super_list
->next
;
3272 st
->container_dev
= devnum
;
3273 if (err
== 0 && st
->ss
== NULL
) {
3274 st
->ss
= &super_imsm
;
3275 st
->minor_version
= 0;
3276 st
->max_devs
= IMSM_MAX_DEVICES
;
3281 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
3283 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
);
3287 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
3289 struct intel_super
*super
;
3292 if (test_partition(fd
))
3293 /* IMSM not allowed on partitions */
3296 free_super_imsm(st
);
3298 super
= alloc_super();
3301 Name
": malloc of %zu failed.\n",
3306 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
3311 Name
": Failed to load all information "
3312 "sections on %s\n", devname
);
3318 if (st
->ss
== NULL
) {
3319 st
->ss
= &super_imsm
;
3320 st
->minor_version
= 0;
3321 st
->max_devs
= IMSM_MAX_DEVICES
;
3326 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
3328 if (info
->level
== 1)
3330 return info
->chunk_size
>> 9;
3333 static __u32
info_to_num_data_stripes(mdu_array_info_t
*info
, int num_domains
)
3337 num_stripes
= (info
->size
* 2) / info_to_blocks_per_strip(info
);
3338 num_stripes
/= num_domains
;
3343 static __u32
info_to_blocks_per_member(mdu_array_info_t
*info
)
3345 if (info
->level
== 1)
3346 return info
->size
* 2;
3348 return (info
->size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
3351 static void imsm_update_version_info(struct intel_super
*super
)
3353 /* update the version and attributes */
3354 struct imsm_super
*mpb
= super
->anchor
;
3356 struct imsm_dev
*dev
;
3357 struct imsm_map
*map
;
3360 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3361 dev
= get_imsm_dev(super
, i
);
3362 map
= get_imsm_map(dev
, 0);
3363 if (__le32_to_cpu(dev
->size_high
) > 0)
3364 mpb
->attributes
|= MPB_ATTRIB_2TB
;
3366 /* FIXME detect when an array spans a port multiplier */
3368 mpb
->attributes
|= MPB_ATTRIB_PM
;
3371 if (mpb
->num_raid_devs
> 1 ||
3372 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
3373 version
= MPB_VERSION_ATTRIBS
;
3374 switch (get_imsm_raid_level(map
)) {
3375 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
3376 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
3377 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
3378 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
3381 if (map
->num_members
>= 5)
3382 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
3383 else if (dev
->status
== DEV_CLONE_N_GO
)
3384 version
= MPB_VERSION_CNG
;
3385 else if (get_imsm_raid_level(map
) == 5)
3386 version
= MPB_VERSION_RAID5
;
3387 else if (map
->num_members
>= 3)
3388 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
3389 else if (get_imsm_raid_level(map
) == 1)
3390 version
= MPB_VERSION_RAID1
;
3392 version
= MPB_VERSION_RAID0
;
3394 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
3398 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
3400 struct imsm_super
*mpb
= super
->anchor
;
3401 char *reason
= NULL
;
3404 if (strlen(name
) > MAX_RAID_SERIAL_LEN
)
3405 reason
= "must be 16 characters or less";
3407 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3408 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3410 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
3411 reason
= "already exists";
3416 if (reason
&& !quiet
)
3417 fprintf(stderr
, Name
": imsm volume name %s\n", reason
);
3422 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
3423 unsigned long long size
, char *name
,
3424 char *homehost
, int *uuid
)
3426 /* We are creating a volume inside a pre-existing container.
3427 * so st->sb is already set.
3429 struct intel_super
*super
= st
->sb
;
3430 struct imsm_super
*mpb
= super
->anchor
;
3431 struct intel_dev
*dv
;
3432 struct imsm_dev
*dev
;
3433 struct imsm_vol
*vol
;
3434 struct imsm_map
*map
;
3435 int idx
= mpb
->num_raid_devs
;
3437 unsigned long long array_blocks
;
3438 size_t size_old
, size_new
;
3439 __u32 num_data_stripes
;
3441 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
3442 fprintf(stderr
, Name
": This imsm-container already has the "
3443 "maximum of %d volumes\n", super
->orom
->vpa
);
3447 /* ensure the mpb is large enough for the new data */
3448 size_old
= __le32_to_cpu(mpb
->mpb_size
);
3449 size_new
= disks_to_mpb_size(info
->nr_disks
);
3450 if (size_new
> size_old
) {
3452 size_t size_round
= ROUND_UP(size_new
, 512);
3454 if (posix_memalign(&mpb_new
, 512, size_round
) != 0) {
3455 fprintf(stderr
, Name
": could not allocate new mpb\n");
3458 memcpy(mpb_new
, mpb
, size_old
);
3461 super
->anchor
= mpb_new
;
3462 mpb
->mpb_size
= __cpu_to_le32(size_new
);
3463 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
3465 super
->current_vol
= idx
;
3466 /* when creating the first raid device in this container set num_disks
3467 * to zero, i.e. delete this spare and add raid member devices in
3468 * add_to_super_imsm_volume()
3470 if (super
->current_vol
== 0)
3473 if (!check_name(super
, name
, 0))
3475 dv
= malloc(sizeof(*dv
));
3477 fprintf(stderr
, Name
": failed to allocate device list entry\n");
3480 dev
= calloc(1, sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
3483 fprintf(stderr
, Name
": could not allocate raid device\n");
3487 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
3488 if (info
->level
== 1)
3489 array_blocks
= info_to_blocks_per_member(info
);
3491 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
3492 info
->layout
, info
->chunk_size
,
3494 /* round array size down to closest MB */
3495 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
3497 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
3498 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
3499 dev
->status
= (DEV_READ_COALESCING
| DEV_WRITE_COALESCING
);
3501 vol
->migr_state
= 0;
3502 set_migr_type(dev
, MIGR_INIT
);
3504 vol
->curr_migr_unit
= 0;
3505 map
= get_imsm_map(dev
, 0);
3506 map
->pba_of_lba0
= __cpu_to_le32(super
->create_offset
);
3507 map
->blocks_per_member
= __cpu_to_le32(info_to_blocks_per_member(info
));
3508 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
3509 map
->failed_disk_num
= ~0;
3510 map
->map_state
= info
->level
? IMSM_T_STATE_UNINITIALIZED
:
3511 IMSM_T_STATE_NORMAL
;
3514 if (info
->level
== 1 && info
->raid_disks
> 2) {
3517 fprintf(stderr
, Name
": imsm does not support more than 2 disks"
3518 "in a raid1 volume\n");
3522 map
->raid_level
= info
->level
;
3523 if (info
->level
== 10) {
3524 map
->raid_level
= 1;
3525 map
->num_domains
= info
->raid_disks
/ 2;
3526 } else if (info
->level
== 1)
3527 map
->num_domains
= info
->raid_disks
;
3529 map
->num_domains
= 1;
3531 num_data_stripes
= info_to_num_data_stripes(info
, map
->num_domains
);
3532 map
->num_data_stripes
= __cpu_to_le32(num_data_stripes
);
3534 map
->num_members
= info
->raid_disks
;
3535 for (i
= 0; i
< map
->num_members
; i
++) {
3536 /* initialized in add_to_super */
3537 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
3539 mpb
->num_raid_devs
++;
3542 dv
->index
= super
->current_vol
;
3543 dv
->next
= super
->devlist
;
3544 super
->devlist
= dv
;
3546 imsm_update_version_info(super
);
3551 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
3552 unsigned long long size
, char *name
,
3553 char *homehost
, int *uuid
)
3555 /* This is primarily called by Create when creating a new array.
3556 * We will then get add_to_super called for each component, and then
3557 * write_init_super called to write it out to each device.
3558 * For IMSM, Create can create on fresh devices or on a pre-existing
3560 * To create on a pre-existing array a different method will be called.
3561 * This one is just for fresh drives.
3563 struct intel_super
*super
;
3564 struct imsm_super
*mpb
;
3569 return init_super_imsm_volume(st
, info
, size
, name
, homehost
, uuid
);
3572 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
3576 super
= alloc_super();
3577 if (super
&& posix_memalign(&super
->buf
, 512, mpb_size
) != 0) {
3582 fprintf(stderr
, Name
3583 ": %s could not allocate superblock\n", __func__
);
3586 memset(super
->buf
, 0, mpb_size
);
3588 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
3592 /* zeroing superblock */
3596 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
3598 version
= (char *) mpb
->sig
;
3599 strcpy(version
, MPB_SIGNATURE
);
3600 version
+= strlen(MPB_SIGNATURE
);
3601 strcpy(version
, MPB_VERSION_RAID0
);
3607 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
3608 int fd
, char *devname
)
3610 struct intel_super
*super
= st
->sb
;
3611 struct imsm_super
*mpb
= super
->anchor
;
3613 struct imsm_dev
*dev
;
3614 struct imsm_map
*map
;
3617 dev
= get_imsm_dev(super
, super
->current_vol
);
3618 map
= get_imsm_map(dev
, 0);
3620 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
3621 fprintf(stderr
, Name
": %s: Cannot add spare devices to IMSM volume\n",
3627 /* we're doing autolayout so grab the pre-marked (in
3628 * validate_geometry) raid_disk
3630 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3631 if (dl
->raiddisk
== dk
->raid_disk
)
3634 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3635 if (dl
->major
== dk
->major
&&
3636 dl
->minor
== dk
->minor
)
3641 fprintf(stderr
, Name
": %s is not a member of the same container\n", devname
);
3645 /* add a pristine spare to the metadata */
3646 if (dl
->index
< 0) {
3647 dl
->index
= super
->anchor
->num_disks
;
3648 super
->anchor
->num_disks
++;
3650 /* Check the device has not already been added */
3651 slot
= get_imsm_disk_slot(map
, dl
->index
);
3653 (get_imsm_ord_tbl_ent(dev
, slot
, -1) & IMSM_ORD_REBUILD
) == 0) {
3654 fprintf(stderr
, Name
": %s has been included in this array twice\n",
3658 set_imsm_ord_tbl_ent(map
, dk
->number
, dl
->index
);
3659 dl
->disk
.status
= CONFIGURED_DISK
;
3661 /* if we are creating the first raid device update the family number */
3662 if (super
->current_vol
== 0) {
3664 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
3665 struct imsm_disk
*_disk
= __get_imsm_disk(mpb
, dl
->index
);
3667 if (!_dev
|| !_disk
) {
3668 fprintf(stderr
, Name
": BUG mpb setup error\n");
3674 sum
+= __gen_imsm_checksum(mpb
);
3675 mpb
->family_num
= __cpu_to_le32(sum
);
3676 mpb
->orig_family_num
= mpb
->family_num
;
3683 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
3684 int fd
, char *devname
)
3686 struct intel_super
*super
= st
->sb
;
3688 unsigned long long size
;
3693 /* If we are on an RAID enabled platform check that the disk is
3694 * attached to the raid controller.
3695 * We do not need to test disks attachment for container based additions,
3696 * they shall be already tested when container was created/assembled.
3698 if ((fd
!= -1) && !check_env("IMSM_NO_PLATFORM")) {
3699 struct sys_dev
*hba_name
;
3700 struct intel_hba
*hba
;
3702 hba_name
= find_disk_attached_hba(fd
, NULL
);
3705 Name
": %s is not attached to Intel(R) RAID controller.\n",
3706 devname
? : "disk");
3709 rv
= attach_hba_to_super(super
, hba_name
);
3712 fprintf(stderr
, Name
": %s is attached to Intel(R) %s RAID "
3713 "controller (%s),\n but the container is assigned to Intel(R) "
3714 "%s RAID controller (",
3716 get_sys_dev_type(hba_name
->type
),
3717 hba_name
->pci_id
? : "Err!",
3718 get_sys_dev_type(hba_name
->type
));
3722 fprintf(stderr
, "%s", hba
->pci_id
? : "Err!");
3724 fprintf(stderr
, ", ");
3728 fprintf(stderr
, ").\n"
3729 " Mixing devices attached to different controllers "
3730 "is not allowed.\n");
3731 free_sys_dev(&hba_name
);
3734 free_sys_dev(&hba_name
);
3737 if (super
->current_vol
>= 0)
3738 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
3741 dd
= malloc(sizeof(*dd
));
3744 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
3747 memset(dd
, 0, sizeof(*dd
));
3748 dd
->major
= major(stb
.st_rdev
);
3749 dd
->minor
= minor(stb
.st_rdev
);
3751 dd
->devname
= devname
? strdup(devname
) : NULL
;
3754 dd
->action
= DISK_ADD
;
3755 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
3758 Name
": failed to retrieve scsi serial, aborting\n");
3763 get_dev_size(fd
, NULL
, &size
);
3765 serialcpy(dd
->disk
.serial
, dd
->serial
);
3766 dd
->disk
.total_blocks
= __cpu_to_le32(size
);
3767 dd
->disk
.status
= SPARE_DISK
;
3768 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
3769 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
3771 dd
->disk
.scsi_id
= __cpu_to_le32(0);
3773 if (st
->update_tail
) {
3774 dd
->next
= super
->disk_mgmt_list
;
3775 super
->disk_mgmt_list
= dd
;
3777 dd
->next
= super
->disks
;
3785 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
3787 struct intel_super
*super
= st
->sb
;
3790 /* remove from super works only in mdmon - for communication
3791 * manager - monitor. Check if communication memory buffer
3794 if (!st
->update_tail
) {
3796 Name
": %s shall be used in mdmon context only"
3797 "(line %d).\n", __func__
, __LINE__
);
3800 dd
= malloc(sizeof(*dd
));
3803 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
3806 memset(dd
, 0, sizeof(*dd
));
3807 dd
->major
= dk
->major
;
3808 dd
->minor
= dk
->minor
;
3811 dd
->disk
.status
= SPARE_DISK
;
3812 dd
->action
= DISK_REMOVE
;
3814 dd
->next
= super
->disk_mgmt_list
;
3815 super
->disk_mgmt_list
= dd
;
3821 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
3825 struct imsm_super anchor
;
3826 } spare_record
__attribute__ ((aligned(512)));
3828 /* spare records have their own family number and do not have any defined raid
3831 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
3833 struct imsm_super
*mpb
= super
->anchor
;
3834 struct imsm_super
*spare
= &spare_record
.anchor
;
3838 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
)),
3839 spare
->generation_num
= __cpu_to_le32(1UL),
3840 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
3841 spare
->num_disks
= 1,
3842 spare
->num_raid_devs
= 0,
3843 spare
->cache_size
= mpb
->cache_size
,
3844 spare
->pwr_cycle_count
= __cpu_to_le32(1),
3846 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
3847 MPB_SIGNATURE MPB_VERSION_RAID0
);
3849 for (d
= super
->disks
; d
; d
= d
->next
) {
3853 spare
->disk
[0] = d
->disk
;
3854 sum
= __gen_imsm_checksum(spare
);
3855 spare
->family_num
= __cpu_to_le32(sum
);
3856 spare
->orig_family_num
= 0;
3857 sum
= __gen_imsm_checksum(spare
);
3858 spare
->check_sum
= __cpu_to_le32(sum
);
3860 if (store_imsm_mpb(d
->fd
, spare
)) {
3861 fprintf(stderr
, "%s: failed for device %d:%d %s\n",
3862 __func__
, d
->major
, d
->minor
, strerror(errno
));
3874 static int write_super_imsm(struct supertype
*st
, int doclose
)
3876 struct intel_super
*super
= st
->sb
;
3877 struct imsm_super
*mpb
= super
->anchor
;
3883 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
3886 /* 'generation' is incremented everytime the metadata is written */
3887 generation
= __le32_to_cpu(mpb
->generation_num
);
3889 mpb
->generation_num
= __cpu_to_le32(generation
);
3891 /* fix up cases where previous mdadm releases failed to set
3894 if (mpb
->orig_family_num
== 0)
3895 mpb
->orig_family_num
= mpb
->family_num
;
3897 for (d
= super
->disks
; d
; d
= d
->next
) {
3901 mpb
->disk
[d
->index
] = d
->disk
;
3905 for (d
= super
->missing
; d
; d
= d
->next
) {
3906 mpb
->disk
[d
->index
] = d
->disk
;
3909 mpb
->num_disks
= num_disks
;
3910 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
3912 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3913 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
3914 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
3916 imsm_copy_dev(dev
, dev2
);
3917 mpb_size
+= sizeof_imsm_dev(dev
, 0);
3920 mpb_size
+= __le32_to_cpu(mpb
->bbm_log_size
);
3921 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
3923 /* recalculate checksum */
3924 sum
= __gen_imsm_checksum(mpb
);
3925 mpb
->check_sum
= __cpu_to_le32(sum
);
3927 /* write the mpb for disks that compose raid devices */
3928 for (d
= super
->disks
; d
; d
= d
->next
) {
3931 if (store_imsm_mpb(d
->fd
, mpb
))
3932 fprintf(stderr
, "%s: failed for device %d:%d %s\n",
3933 __func__
, d
->major
, d
->minor
, strerror(errno
));
3941 return write_super_imsm_spares(super
, doclose
);
3947 static int create_array(struct supertype
*st
, int dev_idx
)
3950 struct imsm_update_create_array
*u
;
3951 struct intel_super
*super
= st
->sb
;
3952 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
3953 struct imsm_map
*map
= get_imsm_map(dev
, 0);
3954 struct disk_info
*inf
;
3955 struct imsm_disk
*disk
;
3958 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
3959 sizeof(*inf
) * map
->num_members
;
3962 fprintf(stderr
, "%s: failed to allocate update buffer\n",
3967 u
->type
= update_create_array
;
3968 u
->dev_idx
= dev_idx
;
3969 imsm_copy_dev(&u
->dev
, dev
);
3970 inf
= get_disk_info(u
);
3971 for (i
= 0; i
< map
->num_members
; i
++) {
3972 int idx
= get_imsm_disk_idx(dev
, i
, -1);
3974 disk
= get_imsm_disk(super
, idx
);
3975 serialcpy(inf
[i
].serial
, disk
->serial
);
3977 append_metadata_update(st
, u
, len
);
3982 static int mgmt_disk(struct supertype
*st
)
3984 struct intel_super
*super
= st
->sb
;
3986 struct imsm_update_add_remove_disk
*u
;
3988 if (!super
->disk_mgmt_list
)
3994 fprintf(stderr
, "%s: failed to allocate update buffer\n",
3999 u
->type
= update_add_remove_disk
;
4000 append_metadata_update(st
, u
, len
);
4005 static int write_init_super_imsm(struct supertype
*st
)
4007 struct intel_super
*super
= st
->sb
;
4008 int current_vol
= super
->current_vol
;
4010 /* we are done with current_vol reset it to point st at the container */
4011 super
->current_vol
= -1;
4013 if (st
->update_tail
) {
4014 /* queue the recently created array / added disk
4015 * as a metadata update */
4018 /* determine if we are creating a volume or adding a disk */
4019 if (current_vol
< 0) {
4020 /* in the mgmt (add/remove) disk case we are running
4021 * in mdmon context, so don't close fd's
4023 return mgmt_disk(st
);
4025 rv
= create_array(st
, current_vol
);
4030 for (d
= super
->disks
; d
; d
= d
->next
)
4031 Kill(d
->devname
, NULL
, 0, 1, 1);
4032 return write_super_imsm(st
, 1);
4037 static int store_super_imsm(struct supertype
*st
, int fd
)
4039 struct intel_super
*super
= st
->sb
;
4040 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
4046 return store_imsm_mpb(fd
, mpb
);
4052 static int imsm_bbm_log_size(struct imsm_super
*mpb
)
4054 return __le32_to_cpu(mpb
->bbm_log_size
);
4058 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
4059 int layout
, int raiddisks
, int chunk
,
4060 unsigned long long size
, char *dev
,
4061 unsigned long long *freesize
,
4065 unsigned long long ldsize
;
4066 const struct imsm_orom
*orom
;
4068 if (level
!= LEVEL_CONTAINER
)
4073 if (check_env("IMSM_NO_PLATFORM"))
4076 orom
= find_imsm_orom();
4077 if (orom
&& raiddisks
> orom
->tds
) {
4079 fprintf(stderr
, Name
": %d exceeds maximum number of"
4080 " platform supported disks: %d\n",
4081 raiddisks
, orom
->tds
);
4085 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
4088 fprintf(stderr
, Name
": imsm: Cannot open %s: %s\n",
4089 dev
, strerror(errno
));
4092 if (!get_dev_size(fd
, dev
, &ldsize
)) {
4098 *freesize
= avail_size_imsm(st
, ldsize
>> 9);
4103 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
4105 const unsigned long long base_start
= e
[*idx
].start
;
4106 unsigned long long end
= base_start
+ e
[*idx
].size
;
4109 if (base_start
== end
)
4113 for (i
= *idx
; i
< num_extents
; i
++) {
4114 /* extend overlapping extents */
4115 if (e
[i
].start
>= base_start
&&
4116 e
[i
].start
<= end
) {
4119 if (e
[i
].start
+ e
[i
].size
> end
)
4120 end
= e
[i
].start
+ e
[i
].size
;
4121 } else if (e
[i
].start
> end
) {
4127 return end
- base_start
;
4130 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
4132 /* build a composite disk with all known extents and generate a new
4133 * 'maxsize' given the "all disks in an array must share a common start
4134 * offset" constraint
4136 struct extent
*e
= calloc(sum_extents
, sizeof(*e
));
4140 unsigned long long pos
;
4141 unsigned long long start
= 0;
4142 unsigned long long maxsize
;
4143 unsigned long reserve
;
4148 /* coalesce and sort all extents. also, check to see if we need to
4149 * reserve space between member arrays
4152 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
4155 for (i
= 0; i
< dl
->extent_cnt
; i
++)
4158 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
4163 while (i
< sum_extents
) {
4164 e
[j
].start
= e
[i
].start
;
4165 e
[j
].size
= find_size(e
, &i
, sum_extents
);
4167 if (e
[j
-1].size
== 0)
4176 unsigned long long esize
;
4178 esize
= e
[i
].start
- pos
;
4179 if (esize
>= maxsize
) {
4184 pos
= e
[i
].start
+ e
[i
].size
;
4186 } while (e
[i
-1].size
);
4192 /* FIXME assumes volume at offset 0 is the first volume in a
4195 if (start_extent
> 0)
4196 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
4200 if (maxsize
< reserve
)
4203 super
->create_offset
= ~((__u32
) 0);
4204 if (start
+ reserve
> super
->create_offset
)
4205 return 0; /* start overflows create_offset */
4206 super
->create_offset
= start
+ reserve
;
4208 return maxsize
- reserve
;
4211 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
4213 if (level
< 0 || level
== 6 || level
== 4)
4216 /* if we have an orom prevent invalid raid levels */
4219 case 0: return imsm_orom_has_raid0(orom
);
4222 return imsm_orom_has_raid1e(orom
);
4223 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
4224 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
4225 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
4228 return 1; /* not on an Intel RAID platform so anything goes */
4233 #define pr_vrb(fmt, arg...) (void) (verbose && fprintf(stderr, Name fmt, ##arg))
4235 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
4236 int raiddisks
, int *chunk
, int verbose
)
4238 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
4239 pr_vrb(": platform does not support raid%d with %d disk%s\n",
4240 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
4243 if (super
->orom
&& level
!= 1) {
4244 if (chunk
&& (*chunk
== 0 || *chunk
== UnSet
))
4245 *chunk
= imsm_orom_default_chunk(super
->orom
);
4246 else if (chunk
&& !imsm_orom_has_chunk(super
->orom
, *chunk
)) {
4247 pr_vrb(": platform does not support a chunk size of: "
4252 if (layout
!= imsm_level_to_layout(level
)) {
4254 pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
4255 else if (level
== 10)
4256 pr_vrb(": imsm raid 10 only supports the n2 layout\n");
4258 pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
4266 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
4267 * FIX ME add ahci details
4269 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
4270 int layout
, int raiddisks
, int *chunk
,
4271 unsigned long long size
, char *dev
,
4272 unsigned long long *freesize
,
4276 struct intel_super
*super
= st
->sb
;
4277 struct imsm_super
*mpb
= super
->anchor
;
4279 unsigned long long pos
= 0;
4280 unsigned long long maxsize
;
4284 /* We must have the container info already read in. */
4288 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, verbose
))
4292 /* General test: make sure there is space for
4293 * 'raiddisks' device extents of size 'size' at a given
4296 unsigned long long minsize
= size
;
4297 unsigned long long start_offset
= MaxSector
;
4300 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
4301 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
4306 e
= get_extents(super
, dl
);
4309 unsigned long long esize
;
4310 esize
= e
[i
].start
- pos
;
4311 if (esize
>= minsize
)
4313 if (found
&& start_offset
== MaxSector
) {
4316 } else if (found
&& pos
!= start_offset
) {
4320 pos
= e
[i
].start
+ e
[i
].size
;
4322 } while (e
[i
-1].size
);
4327 if (dcnt
< raiddisks
) {
4329 fprintf(stderr
, Name
": imsm: Not enough "
4330 "devices with space for this array "
4338 /* This device must be a member of the set */
4339 if (stat(dev
, &stb
) < 0)
4341 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
4343 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
4344 if (dl
->major
== (int)major(stb
.st_rdev
) &&
4345 dl
->minor
== (int)minor(stb
.st_rdev
))
4350 fprintf(stderr
, Name
": %s is not in the "
4351 "same imsm set\n", dev
);
4353 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
4354 /* If a volume is present then the current creation attempt
4355 * cannot incorporate new spares because the orom may not
4356 * understand this configuration (all member disks must be
4357 * members of each array in the container).
4359 fprintf(stderr
, Name
": %s is a spare and a volume"
4360 " is already defined for this container\n", dev
);
4361 fprintf(stderr
, Name
": The option-rom requires all member"
4362 " disks to be a member of all volumes\n");
4366 /* retrieve the largest free space block */
4367 e
= get_extents(super
, dl
);
4372 unsigned long long esize
;
4374 esize
= e
[i
].start
- pos
;
4375 if (esize
>= maxsize
)
4377 pos
= e
[i
].start
+ e
[i
].size
;
4379 } while (e
[i
-1].size
);
4384 fprintf(stderr
, Name
": unable to determine free space for: %s\n",
4388 if (maxsize
< size
) {
4390 fprintf(stderr
, Name
": %s not enough space (%llu < %llu)\n",
4391 dev
, maxsize
, size
);
4395 /* count total number of extents for merge */
4397 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4399 i
+= dl
->extent_cnt
;
4401 maxsize
= merge_extents(super
, i
);
4402 if (maxsize
< size
|| maxsize
== 0) {
4404 fprintf(stderr
, Name
": not enough space after merge (%llu < %llu)\n",
4409 *freesize
= maxsize
;
4414 static int reserve_space(struct supertype
*st
, int raiddisks
,
4415 unsigned long long size
, int chunk
,
4416 unsigned long long *freesize
)
4418 struct intel_super
*super
= st
->sb
;
4419 struct imsm_super
*mpb
= super
->anchor
;
4424 unsigned long long maxsize
;
4425 unsigned long long minsize
;
4429 /* find the largest common start free region of the possible disks */
4433 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
4439 /* don't activate new spares if we are orom constrained
4440 * and there is already a volume active in the container
4442 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
4445 e
= get_extents(super
, dl
);
4448 for (i
= 1; e
[i
-1].size
; i
++)
4456 maxsize
= merge_extents(super
, extent_cnt
);
4460 minsize
= chunk
* 2;
4462 if (cnt
< raiddisks
||
4463 (super
->orom
&& used
&& used
!= raiddisks
) ||
4464 maxsize
< minsize
||
4466 fprintf(stderr
, Name
": not enough devices with space to create array.\n");
4467 return 0; /* No enough free spaces large enough */
4479 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4481 dl
->raiddisk
= cnt
++;
4488 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
4489 int raiddisks
, int *chunk
, unsigned long long size
,
4490 char *dev
, unsigned long long *freesize
,
4497 /* if given unused devices create a container
4498 * if given given devices in a container create a member volume
4500 if (level
== LEVEL_CONTAINER
) {
4501 /* Must be a fresh device to add to a container */
4502 return validate_geometry_imsm_container(st
, level
, layout
,
4504 chunk
?*chunk
:0, size
,
4510 if (st
->sb
&& freesize
) {
4511 /* we are being asked to automatically layout a
4512 * new volume based on the current contents of
4513 * the container. If the the parameters can be
4514 * satisfied reserve_space will record the disks,
4515 * start offset, and size of the volume to be
4516 * created. add_to_super and getinfo_super
4517 * detect when autolayout is in progress.
4519 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
4523 return reserve_space(st
, raiddisks
, size
,
4524 chunk
?*chunk
:0, freesize
);
4529 /* creating in a given container */
4530 return validate_geometry_imsm_volume(st
, level
, layout
,
4531 raiddisks
, chunk
, size
,
4532 dev
, freesize
, verbose
);
4535 /* This device needs to be a device in an 'imsm' container */
4536 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
4540 Name
": Cannot create this array on device %s\n",
4545 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
4547 fprintf(stderr
, Name
": Cannot open %s: %s\n",
4548 dev
, strerror(errno
));
4551 /* Well, it is in use by someone, maybe an 'imsm' container. */
4552 cfd
= open_container(fd
);
4556 fprintf(stderr
, Name
": Cannot use %s: It is busy\n",
4560 sra
= sysfs_read(cfd
, 0, GET_VERSION
);
4561 if (sra
&& sra
->array
.major_version
== -1 &&
4562 strcmp(sra
->text_version
, "imsm") == 0)
4566 /* This is a member of a imsm container. Load the container
4567 * and try to create a volume
4569 struct intel_super
*super
;
4571 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
) == 0) {
4573 st
->container_dev
= fd2devnum(cfd
);
4575 return validate_geometry_imsm_volume(st
, level
, layout
,
4583 fprintf(stderr
, Name
": failed container membership check\n");
4589 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
4591 struct intel_super
*super
= st
->sb
;
4593 if (level
&& *level
== UnSet
)
4594 *level
= LEVEL_CONTAINER
;
4596 if (level
&& layout
&& *layout
== UnSet
)
4597 *layout
= imsm_level_to_layout(*level
);
4599 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0) &&
4600 super
&& super
->orom
)
4601 *chunk
= imsm_orom_default_chunk(super
->orom
);
4604 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
4606 static int kill_subarray_imsm(struct supertype
*st
)
4608 /* remove the subarray currently referenced by ->current_vol */
4610 struct intel_dev
**dp
;
4611 struct intel_super
*super
= st
->sb
;
4612 __u8 current_vol
= super
->current_vol
;
4613 struct imsm_super
*mpb
= super
->anchor
;
4615 if (super
->current_vol
< 0)
4617 super
->current_vol
= -1; /* invalidate subarray cursor */
4619 /* block deletions that would change the uuid of active subarrays
4621 * FIXME when immutable ids are available, but note that we'll
4622 * also need to fixup the invalidated/active subarray indexes in
4625 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4628 if (i
< current_vol
)
4630 sprintf(subarray
, "%u", i
);
4631 if (is_subarray_active(subarray
, st
->devname
)) {
4633 Name
": deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
4640 if (st
->update_tail
) {
4641 struct imsm_update_kill_array
*u
= malloc(sizeof(*u
));
4645 u
->type
= update_kill_array
;
4646 u
->dev_idx
= current_vol
;
4647 append_metadata_update(st
, u
, sizeof(*u
));
4652 for (dp
= &super
->devlist
; *dp
;)
4653 if ((*dp
)->index
== current_vol
) {
4656 handle_missing(super
, (*dp
)->dev
);
4657 if ((*dp
)->index
> current_vol
)
4662 /* no more raid devices, all active components are now spares,
4663 * but of course failed are still failed
4665 if (--mpb
->num_raid_devs
== 0) {
4668 for (d
= super
->disks
; d
; d
= d
->next
)
4669 if (d
->index
> -2) {
4671 d
->disk
.status
= SPARE_DISK
;
4675 super
->updates_pending
++;
4680 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
4681 char *update
, struct mddev_ident
*ident
)
4683 /* update the subarray currently referenced by ->current_vol */
4684 struct intel_super
*super
= st
->sb
;
4685 struct imsm_super
*mpb
= super
->anchor
;
4687 if (strcmp(update
, "name") == 0) {
4688 char *name
= ident
->name
;
4692 if (is_subarray_active(subarray
, st
->devname
)) {
4694 Name
": Unable to update name of active subarray\n");
4698 if (!check_name(super
, name
, 0))
4701 vol
= strtoul(subarray
, &ep
, 10);
4702 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
4705 if (st
->update_tail
) {
4706 struct imsm_update_rename_array
*u
= malloc(sizeof(*u
));
4710 u
->type
= update_rename_array
;
4712 snprintf((char *) u
->name
, MAX_RAID_SERIAL_LEN
, "%s", name
);
4713 append_metadata_update(st
, u
, sizeof(*u
));
4715 struct imsm_dev
*dev
;
4718 dev
= get_imsm_dev(super
, vol
);
4719 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
4720 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4721 dev
= get_imsm_dev(super
, i
);
4722 handle_missing(super
, dev
);
4724 super
->updates_pending
++;
4732 static int is_gen_migration(struct imsm_dev
*dev
)
4734 if (!dev
->vol
.migr_state
)
4737 if (migr_type(dev
) == MIGR_GEN_MIGR
)
4742 #endif /* MDASSEMBLE */
4744 static int is_rebuilding(struct imsm_dev
*dev
)
4746 struct imsm_map
*migr_map
;
4748 if (!dev
->vol
.migr_state
)
4751 if (migr_type(dev
) != MIGR_REBUILD
)
4754 migr_map
= get_imsm_map(dev
, 1);
4756 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
4762 static void update_recovery_start(struct imsm_dev
*dev
, struct mdinfo
*array
)
4764 struct mdinfo
*rebuild
= NULL
;
4768 if (!is_rebuilding(dev
))
4771 /* Find the rebuild target, but punt on the dual rebuild case */
4772 for (d
= array
->devs
; d
; d
= d
->next
)
4773 if (d
->recovery_start
== 0) {
4780 /* (?) none of the disks are marked with
4781 * IMSM_ORD_REBUILD, so assume they are missing and the
4782 * disk_ord_tbl was not correctly updated
4784 dprintf("%s: failed to locate out-of-sync disk\n", __func__
);
4788 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
4789 rebuild
->recovery_start
= units
* blocks_per_migr_unit(dev
);
4793 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
4795 /* Given a container loaded by load_super_imsm_all,
4796 * extract information about all the arrays into
4798 * If 'subarray' is given, just extract info about that array.
4800 * For each imsm_dev create an mdinfo, fill it in,
4801 * then look for matching devices in super->disks
4802 * and create appropriate device mdinfo.
4804 struct intel_super
*super
= st
->sb
;
4805 struct imsm_super
*mpb
= super
->anchor
;
4806 struct mdinfo
*rest
= NULL
;
4810 int spare_disks
= 0;
4812 /* check for bad blocks */
4813 if (imsm_bbm_log_size(super
->anchor
))
4816 /* count spare devices, not used in maps
4818 for (d
= super
->disks
; d
; d
= d
->next
)
4822 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4823 struct imsm_dev
*dev
;
4824 struct imsm_map
*map
;
4825 struct imsm_map
*map2
;
4826 struct mdinfo
*this;
4831 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
4834 dev
= get_imsm_dev(super
, i
);
4835 map
= get_imsm_map(dev
, 0);
4836 map2
= get_imsm_map(dev
, 1);
4838 /* do not publish arrays that are in the middle of an
4839 * unsupported migration
4841 if (dev
->vol
.migr_state
&&
4842 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
4843 fprintf(stderr
, Name
": cannot assemble volume '%.16s':"
4844 " unsupported migration in progress\n",
4849 this = malloc(sizeof(*this));
4851 fprintf(stderr
, Name
": failed to allocate %zu bytes\n",
4855 memset(this, 0, sizeof(*this));
4858 super
->current_vol
= i
;
4859 getinfo_super_imsm_volume(st
, this, NULL
);
4860 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
4861 unsigned long long recovery_start
;
4862 struct mdinfo
*info_d
;
4869 idx
= get_imsm_disk_idx(dev
, slot
, 0);
4870 ord
= get_imsm_ord_tbl_ent(dev
, slot
, -1);
4871 for (d
= super
->disks
; d
; d
= d
->next
)
4872 if (d
->index
== idx
)
4875 recovery_start
= MaxSector
;
4878 if (d
&& is_failed(&d
->disk
))
4880 if (ord
& IMSM_ORD_REBUILD
)
4884 * if we skip some disks the array will be assmebled degraded;
4885 * reset resync start to avoid a dirty-degraded
4886 * situation when performing the intial sync
4888 * FIXME handle dirty degraded
4890 if ((skip
|| recovery_start
== 0) && !dev
->vol
.dirty
)
4891 this->resync_start
= MaxSector
;
4895 info_d
= calloc(1, sizeof(*info_d
));
4897 fprintf(stderr
, Name
": failed to allocate disk"
4898 " for volume %.16s\n", dev
->volume
);
4899 info_d
= this->devs
;
4901 struct mdinfo
*d
= info_d
->next
;
4910 info_d
->next
= this->devs
;
4911 this->devs
= info_d
;
4913 info_d
->disk
.number
= d
->index
;
4914 info_d
->disk
.major
= d
->major
;
4915 info_d
->disk
.minor
= d
->minor
;
4916 info_d
->disk
.raid_disk
= slot
;
4917 info_d
->recovery_start
= recovery_start
;
4919 if (slot
< map2
->num_members
)
4920 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
4922 this->array
.spare_disks
++;
4924 if (slot
< map
->num_members
)
4925 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
4927 this->array
.spare_disks
++;
4929 if (info_d
->recovery_start
== MaxSector
)
4930 this->array
.working_disks
++;
4932 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
4933 info_d
->data_offset
= __le32_to_cpu(map
->pba_of_lba0
);
4934 info_d
->component_size
= __le32_to_cpu(map
->blocks_per_member
);
4936 /* now that the disk list is up-to-date fixup recovery_start */
4937 update_recovery_start(dev
, this);
4938 this->array
.spare_disks
+= spare_disks
;
4942 /* if array has bad blocks, set suitable bit in array status */
4944 rest
->array
.state
|= (1<<MD_SB_BBM_ERRORS
);
4950 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
, int failed
)
4952 struct imsm_map
*map
= get_imsm_map(dev
, 0);
4955 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
4956 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
4958 switch (get_imsm_raid_level(map
)) {
4960 return IMSM_T_STATE_FAILED
;
4963 if (failed
< map
->num_members
)
4964 return IMSM_T_STATE_DEGRADED
;
4966 return IMSM_T_STATE_FAILED
;
4971 * check to see if any mirrors have failed, otherwise we
4972 * are degraded. Even numbered slots are mirrored on
4976 /* gcc -Os complains that this is unused */
4977 int insync
= insync
;
4979 for (i
= 0; i
< map
->num_members
; i
++) {
4980 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, -1);
4981 int idx
= ord_to_idx(ord
);
4982 struct imsm_disk
*disk
;
4984 /* reset the potential in-sync count on even-numbered
4985 * slots. num_copies is always 2 for imsm raid10
4990 disk
= get_imsm_disk(super
, idx
);
4991 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
4994 /* no in-sync disks left in this mirror the
4998 return IMSM_T_STATE_FAILED
;
5001 return IMSM_T_STATE_DEGRADED
;
5005 return IMSM_T_STATE_DEGRADED
;
5007 return IMSM_T_STATE_FAILED
;
5013 return map
->map_state
;
5016 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
)
5020 struct imsm_disk
*disk
;
5021 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5022 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
);
5026 /* at the beginning of migration we set IMSM_ORD_REBUILD on
5027 * disks that are being rebuilt. New failures are recorded to
5028 * map[0]. So we look through all the disks we started with and
5029 * see if any failures are still present, or if any new ones
5032 * FIXME add support for online capacity expansion and
5033 * raid-level-migration
5035 for (i
= 0; i
< prev
->num_members
; i
++) {
5036 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
5037 ord
|= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
5038 idx
= ord_to_idx(ord
);
5040 disk
= get_imsm_disk(super
, idx
);
5041 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
5049 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
5052 struct intel_super
*super
= c
->sb
;
5053 struct imsm_super
*mpb
= super
->anchor
;
5055 if (atoi(inst
) >= mpb
->num_raid_devs
) {
5056 fprintf(stderr
, "%s: subarry index %d, out of range\n",
5057 __func__
, atoi(inst
));
5061 dprintf("imsm: open_new %s\n", inst
);
5062 a
->info
.container_member
= atoi(inst
);
5066 static int is_resyncing(struct imsm_dev
*dev
)
5068 struct imsm_map
*migr_map
;
5070 if (!dev
->vol
.migr_state
)
5073 if (migr_type(dev
) == MIGR_INIT
||
5074 migr_type(dev
) == MIGR_REPAIR
)
5077 if (migr_type(dev
) == MIGR_GEN_MIGR
)
5080 migr_map
= get_imsm_map(dev
, 1);
5082 if ((migr_map
->map_state
== IMSM_T_STATE_NORMAL
) &&
5083 (dev
->vol
.migr_type
!= MIGR_GEN_MIGR
))
5089 /* return true if we recorded new information */
5090 static int mark_failure(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
5094 struct imsm_map
*map
;
5095 char buf
[MAX_RAID_SERIAL_LEN
+3];
5096 unsigned int len
, shift
= 0;
5098 /* new failures are always set in map[0] */
5099 map
= get_imsm_map(dev
, 0);
5101 slot
= get_imsm_disk_slot(map
, idx
);
5105 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
5106 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
5109 sprintf(buf
, "%s:0", disk
->serial
);
5110 if ((len
= strlen(buf
)) >= MAX_RAID_SERIAL_LEN
)
5111 shift
= len
- MAX_RAID_SERIAL_LEN
+ 1;
5112 strncpy((char *)disk
->serial
, &buf
[shift
], MAX_RAID_SERIAL_LEN
);
5114 disk
->status
|= FAILED_DISK
;
5115 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
5116 if (map
->failed_disk_num
== 0xff)
5117 map
->failed_disk_num
= slot
;
5121 static void mark_missing(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
5123 mark_failure(dev
, disk
, idx
);
5125 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
5128 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
5129 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
5132 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
5138 if (!super
->missing
)
5140 failed
= imsm_count_failed(super
, dev
);
5141 map_state
= imsm_check_degraded(super
, dev
, failed
);
5143 dprintf("imsm: mark missing\n");
5144 end_migration(dev
, map_state
);
5145 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
5146 mark_missing(dev
, &dl
->disk
, dl
->index
);
5147 super
->updates_pending
++;
5150 static unsigned long long imsm_set_array_size(struct imsm_dev
*dev
)
5152 int used_disks
= imsm_num_data_members(dev
, 0);
5153 unsigned long long array_blocks
;
5154 struct imsm_map
*map
;
5156 if (used_disks
== 0) {
5157 /* when problems occures
5158 * return current array_blocks value
5160 array_blocks
= __le32_to_cpu(dev
->size_high
);
5161 array_blocks
= array_blocks
<< 32;
5162 array_blocks
+= __le32_to_cpu(dev
->size_low
);
5164 return array_blocks
;
5167 /* set array size in metadata
5169 map
= get_imsm_map(dev
, 0);
5170 array_blocks
= map
->blocks_per_member
* used_disks
;
5172 /* round array size down to closest MB
5174 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
5175 dev
->size_low
= __cpu_to_le32((__u32
)array_blocks
);
5176 dev
->size_high
= __cpu_to_le32((__u32
)(array_blocks
>> 32));
5178 return array_blocks
;
5181 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
5183 static void imsm_progress_container_reshape(struct intel_super
*super
)
5185 /* if no device has a migr_state, but some device has a
5186 * different number of members than the previous device, start
5187 * changing the number of devices in this device to match
5190 struct imsm_super
*mpb
= super
->anchor
;
5191 int prev_disks
= -1;
5195 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5196 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
5197 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5198 struct imsm_map
*map2
;
5199 int prev_num_members
;
5201 if (dev
->vol
.migr_state
)
5204 if (prev_disks
== -1)
5205 prev_disks
= map
->num_members
;
5206 if (prev_disks
== map
->num_members
)
5209 /* OK, this array needs to enter reshape mode.
5210 * i.e it needs a migr_state
5213 copy_map_size
= sizeof_imsm_map(map
);
5214 prev_num_members
= map
->num_members
;
5215 map
->num_members
= prev_disks
;
5216 dev
->vol
.migr_state
= 1;
5217 dev
->vol
.curr_migr_unit
= 0;
5218 dev
->vol
.migr_type
= MIGR_GEN_MIGR
;
5219 for (i
= prev_num_members
;
5220 i
< map
->num_members
; i
++)
5221 set_imsm_ord_tbl_ent(map
, i
, i
);
5222 map2
= get_imsm_map(dev
, 1);
5223 /* Copy the current map */
5224 memcpy(map2
, map
, copy_map_size
);
5225 map2
->num_members
= prev_num_members
;
5227 imsm_set_array_size(dev
);
5228 super
->updates_pending
++;
5232 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
5233 * states are handled in imsm_set_disk() with one exception, when a
5234 * resync is stopped due to a new failure this routine will set the
5235 * 'degraded' state for the array.
5237 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
5239 int inst
= a
->info
.container_member
;
5240 struct intel_super
*super
= a
->container
->sb
;
5241 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
5242 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5243 int failed
= imsm_count_failed(super
, dev
);
5244 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
);
5245 __u32 blocks_per_unit
;
5247 if (dev
->vol
.migr_state
&&
5248 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
5249 /* array state change is blocked due to reshape action
5251 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
5252 * - finish the reshape (if last_checkpoint is big and action != reshape)
5253 * - update curr_migr_unit
5255 if (a
->curr_action
== reshape
) {
5256 /* still reshaping, maybe update curr_migr_unit */
5257 goto mark_checkpoint
;
5259 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
5260 /* for some reason we aborted the reshape.
5263 struct imsm_map
*map2
= get_imsm_map(dev
, 1);
5264 dev
->vol
.migr_state
= 0;
5265 dev
->vol
.migr_type
= 0;
5266 dev
->vol
.curr_migr_unit
= 0;
5267 memcpy(map
, map2
, sizeof_imsm_map(map2
));
5268 super
->updates_pending
++;
5270 if (a
->last_checkpoint
>= a
->info
.component_size
) {
5271 unsigned long long array_blocks
;
5275 used_disks
= imsm_num_data_members(dev
, 0);
5276 if (used_disks
> 0) {
5278 map
->blocks_per_member
*
5280 /* round array size down to closest MB
5282 array_blocks
= (array_blocks
5283 >> SECT_PER_MB_SHIFT
)
5284 << SECT_PER_MB_SHIFT
;
5285 a
->info
.custom_array_size
= array_blocks
;
5286 /* encourage manager to update array
5290 a
->check_reshape
= 1;
5292 /* finalize online capacity expansion/reshape */
5293 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
5295 mdi
->disk
.raid_disk
,
5298 imsm_progress_container_reshape(super
);
5303 /* before we activate this array handle any missing disks */
5304 if (consistent
== 2)
5305 handle_missing(super
, dev
);
5307 if (consistent
== 2 &&
5308 (!is_resync_complete(&a
->info
) ||
5309 map_state
!= IMSM_T_STATE_NORMAL
||
5310 dev
->vol
.migr_state
))
5313 if (is_resync_complete(&a
->info
)) {
5314 /* complete intialization / resync,
5315 * recovery and interrupted recovery is completed in
5318 if (is_resyncing(dev
)) {
5319 dprintf("imsm: mark resync done\n");
5320 end_migration(dev
, map_state
);
5321 super
->updates_pending
++;
5322 a
->last_checkpoint
= 0;
5324 } else if (!is_resyncing(dev
) && !failed
) {
5325 /* mark the start of the init process if nothing is failed */
5326 dprintf("imsm: mark resync start\n");
5327 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
5328 migrate(dev
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
5330 migrate(dev
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
5331 super
->updates_pending
++;
5335 /* check if we can update curr_migr_unit from resync_start, recovery_start */
5336 blocks_per_unit
= blocks_per_migr_unit(dev
);
5337 if (blocks_per_unit
) {
5341 units
= a
->last_checkpoint
/ blocks_per_unit
;
5344 /* check that we did not overflow 32-bits, and that
5345 * curr_migr_unit needs updating
5347 if (units32
== units
&&
5348 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
5349 dprintf("imsm: mark checkpoint (%u)\n", units32
);
5350 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
5351 super
->updates_pending
++;
5355 /* mark dirty / clean */
5356 if (dev
->vol
.dirty
!= !consistent
) {
5357 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
5362 super
->updates_pending
++;
5368 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
5370 int inst
= a
->info
.container_member
;
5371 struct intel_super
*super
= a
->container
->sb
;
5372 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
5373 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5374 struct imsm_disk
*disk
;
5379 if (n
> map
->num_members
)
5380 fprintf(stderr
, "imsm: set_disk %d out of range 0..%d\n",
5381 n
, map
->num_members
- 1);
5386 dprintf("imsm: set_disk %d:%x\n", n
, state
);
5388 ord
= get_imsm_ord_tbl_ent(dev
, n
, -1);
5389 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
5391 /* check for new failures */
5392 if (state
& DS_FAULTY
) {
5393 if (mark_failure(dev
, disk
, ord_to_idx(ord
)))
5394 super
->updates_pending
++;
5397 /* check if in_sync */
5398 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
5399 struct imsm_map
*migr_map
= get_imsm_map(dev
, 1);
5401 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
5402 super
->updates_pending
++;
5405 failed
= imsm_count_failed(super
, dev
);
5406 map_state
= imsm_check_degraded(super
, dev
, failed
);
5408 /* check if recovery complete, newly degraded, or failed */
5409 if (map_state
== IMSM_T_STATE_NORMAL
&& is_rebuilding(dev
)) {
5410 end_migration(dev
, map_state
);
5411 map
= get_imsm_map(dev
, 0);
5412 map
->failed_disk_num
= ~0;
5413 super
->updates_pending
++;
5414 a
->last_checkpoint
= 0;
5415 } else if (map_state
== IMSM_T_STATE_DEGRADED
&&
5416 map
->map_state
!= map_state
&&
5417 !dev
->vol
.migr_state
) {
5418 dprintf("imsm: mark degraded\n");
5419 map
->map_state
= map_state
;
5420 super
->updates_pending
++;
5421 a
->last_checkpoint
= 0;
5422 } else if (map_state
== IMSM_T_STATE_FAILED
&&
5423 map
->map_state
!= map_state
) {
5424 dprintf("imsm: mark failed\n");
5425 end_migration(dev
, map_state
);
5426 super
->updates_pending
++;
5427 a
->last_checkpoint
= 0;
5428 } else if (is_gen_migration(dev
)) {
5429 dprintf("imsm: Detected General Migration in state: ");
5430 if (map_state
== IMSM_T_STATE_NORMAL
) {
5431 end_migration(dev
, map_state
);
5432 map
= get_imsm_map(dev
, 0);
5433 map
->failed_disk_num
= ~0;
5434 dprintf("normal\n");
5436 if (map_state
== IMSM_T_STATE_DEGRADED
) {
5437 printf("degraded\n");
5438 end_migration(dev
, map_state
);
5440 dprintf("failed\n");
5442 map
->map_state
= map_state
;
5444 super
->updates_pending
++;
5448 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
5451 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
5452 unsigned long long dsize
;
5453 unsigned long long sectors
;
5455 get_dev_size(fd
, NULL
, &dsize
);
5457 if (mpb_size
> 512) {
5458 /* -1 to account for anchor */
5459 sectors
= mpb_sectors(mpb
) - 1;
5461 /* write the extended mpb to the sectors preceeding the anchor */
5462 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0)
5465 if ((unsigned long long)write(fd
, buf
+ 512, 512 * sectors
)
5470 /* first block is stored on second to last sector of the disk */
5471 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0)
5474 if (write(fd
, buf
, 512) != 512)
5480 static void imsm_sync_metadata(struct supertype
*container
)
5482 struct intel_super
*super
= container
->sb
;
5484 dprintf("sync metadata: %d\n", super
->updates_pending
);
5485 if (!super
->updates_pending
)
5488 write_super_imsm(container
, 0);
5490 super
->updates_pending
= 0;
5493 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
5495 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
5496 int i
= get_imsm_disk_idx(dev
, idx
, -1);
5499 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5503 if (dl
&& is_failed(&dl
->disk
))
5507 dprintf("%s: found %x:%x\n", __func__
, dl
->major
, dl
->minor
);
5512 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
5513 struct active_array
*a
, int activate_new
,
5514 struct mdinfo
*additional_test_list
)
5516 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
5517 int idx
= get_imsm_disk_idx(dev
, slot
, -1);
5518 struct imsm_super
*mpb
= super
->anchor
;
5519 struct imsm_map
*map
;
5520 unsigned long long pos
;
5525 __u32 array_start
= 0;
5526 __u32 array_end
= 0;
5528 struct mdinfo
*test_list
;
5530 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5531 /* If in this array, skip */
5532 for (d
= a
->info
.devs
; d
; d
= d
->next
)
5533 if (d
->state_fd
>= 0 &&
5534 d
->disk
.major
== dl
->major
&&
5535 d
->disk
.minor
== dl
->minor
) {
5536 dprintf("%x:%x already in array\n",
5537 dl
->major
, dl
->minor
);
5542 test_list
= additional_test_list
;
5544 if (test_list
->disk
.major
== dl
->major
&&
5545 test_list
->disk
.minor
== dl
->minor
) {
5546 dprintf("%x:%x already in additional test list\n",
5547 dl
->major
, dl
->minor
);
5550 test_list
= test_list
->next
;
5555 /* skip in use or failed drives */
5556 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
5558 dprintf("%x:%x status (failed: %d index: %d)\n",
5559 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
5563 /* skip pure spares when we are looking for partially
5564 * assimilated drives
5566 if (dl
->index
== -1 && !activate_new
)
5569 /* Does this unused device have the requisite free space?
5570 * It needs to be able to cover all member volumes
5572 ex
= get_extents(super
, dl
);
5574 dprintf("cannot get extents\n");
5577 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5578 dev
= get_imsm_dev(super
, i
);
5579 map
= get_imsm_map(dev
, 0);
5581 /* check if this disk is already a member of
5584 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
5590 array_start
= __le32_to_cpu(map
->pba_of_lba0
);
5591 array_end
= array_start
+
5592 __le32_to_cpu(map
->blocks_per_member
) - 1;
5595 /* check that we can start at pba_of_lba0 with
5596 * blocks_per_member of space
5598 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
5602 pos
= ex
[j
].start
+ ex
[j
].size
;
5604 } while (ex
[j
-1].size
);
5611 if (i
< mpb
->num_raid_devs
) {
5612 dprintf("%x:%x does not have %u to %u available\n",
5613 dl
->major
, dl
->minor
, array_start
, array_end
);
5624 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
5626 struct imsm_dev
*dev2
;
5627 struct imsm_map
*map
;
5633 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
5635 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
);
5636 if (state
== IMSM_T_STATE_FAILED
) {
5637 map
= get_imsm_map(dev2
, 0);
5640 for (slot
= 0; slot
< map
->num_members
; slot
++) {
5642 * Check if failed disks are deleted from intel
5643 * disk list or are marked to be deleted
5645 idx
= get_imsm_disk_idx(dev2
, slot
, -1);
5646 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
5648 * Do not rebuild the array if failed disks
5649 * from failed sub-array are not removed from
5653 is_failed(&idisk
->disk
) &&
5654 (idisk
->action
!= DISK_REMOVE
))
5662 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
5663 struct metadata_update
**updates
)
5666 * Find a device with unused free space and use it to replace a
5667 * failed/vacant region in an array. We replace failed regions one a
5668 * array at a time. The result is that a new spare disk will be added
5669 * to the first failed array and after the monitor has finished
5670 * propagating failures the remainder will be consumed.
5672 * FIXME add a capability for mdmon to request spares from another
5676 struct intel_super
*super
= a
->container
->sb
;
5677 int inst
= a
->info
.container_member
;
5678 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
5679 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5680 int failed
= a
->info
.array
.raid_disks
;
5681 struct mdinfo
*rv
= NULL
;
5684 struct metadata_update
*mu
;
5686 struct imsm_update_activate_spare
*u
;
5691 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
5692 if ((d
->curr_state
& DS_FAULTY
) &&
5694 /* wait for Removal to happen */
5696 if (d
->state_fd
>= 0)
5700 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
5701 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
5703 if (dev
->vol
.migr_state
&&
5704 dev
->vol
.migr_type
== MIGR_GEN_MIGR
)
5705 /* No repair during migration */
5708 if (a
->info
.array
.level
== 4)
5709 /* No repair for takeovered array
5710 * imsm doesn't support raid4
5714 if (imsm_check_degraded(super
, dev
, failed
) != IMSM_T_STATE_DEGRADED
)
5718 * If there are any failed disks check state of the other volume.
5719 * Block rebuild if the another one is failed until failed disks
5720 * are removed from container.
5723 dprintf("found failed disks in %s, check if there another"
5724 "failed sub-array.\n",
5726 /* check if states of the other volumes allow for rebuild */
5727 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
5729 allowed
= imsm_rebuild_allowed(a
->container
,
5737 /* For each slot, if it is not working, find a spare */
5738 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
5739 for (d
= a
->info
.devs
; d
; d
= d
->next
)
5740 if (d
->disk
.raid_disk
== i
)
5742 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
5743 if (d
&& (d
->state_fd
>= 0))
5747 * OK, this device needs recovery. Try to re-add the
5748 * previous occupant of this slot, if this fails see if
5749 * we can continue the assimilation of a spare that was
5750 * partially assimilated, finally try to activate a new
5753 dl
= imsm_readd(super
, i
, a
);
5755 dl
= imsm_add_spare(super
, i
, a
, 0, NULL
);
5757 dl
= imsm_add_spare(super
, i
, a
, 1, NULL
);
5761 /* found a usable disk with enough space */
5762 di
= malloc(sizeof(*di
));
5765 memset(di
, 0, sizeof(*di
));
5767 /* dl->index will be -1 in the case we are activating a
5768 * pristine spare. imsm_process_update() will create a
5769 * new index in this case. Once a disk is found to be
5770 * failed in all member arrays it is kicked from the
5773 di
->disk
.number
= dl
->index
;
5775 /* (ab)use di->devs to store a pointer to the device
5778 di
->devs
= (struct mdinfo
*) dl
;
5780 di
->disk
.raid_disk
= i
;
5781 di
->disk
.major
= dl
->major
;
5782 di
->disk
.minor
= dl
->minor
;
5784 di
->recovery_start
= 0;
5785 di
->data_offset
= __le32_to_cpu(map
->pba_of_lba0
);
5786 di
->component_size
= a
->info
.component_size
;
5787 di
->container_member
= inst
;
5788 super
->random
= random32();
5792 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
5793 i
, di
->data_offset
);
5799 /* No spares found */
5801 /* Now 'rv' has a list of devices to return.
5802 * Create a metadata_update record to update the
5803 * disk_ord_tbl for the array
5805 mu
= malloc(sizeof(*mu
));
5807 mu
->buf
= malloc(sizeof(struct imsm_update_activate_spare
) * num_spares
);
5808 if (mu
->buf
== NULL
) {
5815 struct mdinfo
*n
= rv
->next
;
5824 mu
->space_list
= NULL
;
5825 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
5826 mu
->next
= *updates
;
5827 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
5829 for (di
= rv
; di
; di
= di
->next
) {
5830 u
->type
= update_activate_spare
;
5831 u
->dl
= (struct dl
*) di
->devs
;
5833 u
->slot
= di
->disk
.raid_disk
;
5844 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
5846 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
5847 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5848 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, 0);
5849 struct disk_info
*inf
= get_disk_info(u
);
5850 struct imsm_disk
*disk
;
5854 for (i
= 0; i
< map
->num_members
; i
++) {
5855 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, -1));
5856 for (j
= 0; j
< new_map
->num_members
; j
++)
5857 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
5865 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
5867 struct dl
*dl
= NULL
;
5868 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5869 if ((dl
->major
== major
) && (dl
->minor
== minor
))
5874 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
5876 struct dl
*prev
= NULL
;
5880 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5881 if ((dl
->major
== major
) && (dl
->minor
== minor
)) {
5884 prev
->next
= dl
->next
;
5886 super
->disks
= dl
->next
;
5888 __free_imsm_disk(dl
);
5889 dprintf("%s: removed %x:%x\n",
5890 __func__
, major
, minor
);
5898 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
5900 static int add_remove_disk_update(struct intel_super
*super
)
5902 int check_degraded
= 0;
5903 struct dl
*disk
= NULL
;
5904 /* add/remove some spares to/from the metadata/contrainer */
5905 while (super
->disk_mgmt_list
) {
5906 struct dl
*disk_cfg
;
5908 disk_cfg
= super
->disk_mgmt_list
;
5909 super
->disk_mgmt_list
= disk_cfg
->next
;
5910 disk_cfg
->next
= NULL
;
5912 if (disk_cfg
->action
== DISK_ADD
) {
5913 disk_cfg
->next
= super
->disks
;
5914 super
->disks
= disk_cfg
;
5916 dprintf("%s: added %x:%x\n",
5917 __func__
, disk_cfg
->major
,
5919 } else if (disk_cfg
->action
== DISK_REMOVE
) {
5920 dprintf("Disk remove action processed: %x.%x\n",
5921 disk_cfg
->major
, disk_cfg
->minor
);
5922 disk
= get_disk_super(super
,
5926 /* store action status */
5927 disk
->action
= DISK_REMOVE
;
5928 /* remove spare disks only */
5929 if (disk
->index
== -1) {
5930 remove_disk_super(super
,
5935 /* release allocate disk structure */
5936 __free_imsm_disk(disk_cfg
);
5939 return check_degraded
;
5942 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
5943 struct intel_super
*super
,
5946 struct dl
*new_disk
;
5947 struct intel_dev
*id
;
5949 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
5950 int disk_count
= u
->old_raid_disks
;
5951 void **tofree
= NULL
;
5952 int devices_to_reshape
= 1;
5953 struct imsm_super
*mpb
= super
->anchor
;
5955 unsigned int dev_id
;
5957 dprintf("imsm: apply_reshape_container_disks_update()\n");
5959 /* enable spares to use in array */
5960 for (i
= 0; i
< delta_disks
; i
++) {
5961 new_disk
= get_disk_super(super
,
5962 major(u
->new_disks
[i
]),
5963 minor(u
->new_disks
[i
]));
5964 dprintf("imsm: new disk for reshape is: %i:%i "
5965 "(%p, index = %i)\n",
5966 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
5967 new_disk
, new_disk
->index
);
5968 if ((new_disk
== NULL
) ||
5969 ((new_disk
->index
>= 0) &&
5970 (new_disk
->index
< u
->old_raid_disks
)))
5971 goto update_reshape_exit
;
5972 new_disk
->index
= disk_count
++;
5973 /* slot to fill in autolayout
5975 new_disk
->raiddisk
= new_disk
->index
;
5976 new_disk
->disk
.status
|=
5978 new_disk
->disk
.status
&= ~SPARE_DISK
;
5981 dprintf("imsm: volume set mpb->num_raid_devs = %i\n",
5982 mpb
->num_raid_devs
);
5983 /* manage changes in volume
5985 for (dev_id
= 0; dev_id
< mpb
->num_raid_devs
; dev_id
++) {
5986 void **sp
= *space_list
;
5987 struct imsm_dev
*newdev
;
5988 struct imsm_map
*newmap
, *oldmap
;
5990 for (id
= super
->devlist
; id
; id
= id
->next
) {
5991 if (id
->index
== dev_id
)
6000 /* Copy the dev, but not (all of) the map */
6001 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
6002 oldmap
= get_imsm_map(id
->dev
, 0);
6003 newmap
= get_imsm_map(newdev
, 0);
6004 /* Copy the current map */
6005 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
6006 /* update one device only
6008 if (devices_to_reshape
) {
6009 dprintf("imsm: modifying subdev: %i\n",
6011 devices_to_reshape
--;
6012 newdev
->vol
.migr_state
= 1;
6013 newdev
->vol
.curr_migr_unit
= 0;
6014 newdev
->vol
.migr_type
= MIGR_GEN_MIGR
;
6015 newmap
->num_members
= u
->new_raid_disks
;
6016 for (i
= 0; i
< delta_disks
; i
++) {
6017 set_imsm_ord_tbl_ent(newmap
,
6018 u
->old_raid_disks
+ i
,
6019 u
->old_raid_disks
+ i
);
6021 /* New map is correct, now need to save old map
6023 newmap
= get_imsm_map(newdev
, 1);
6024 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
6026 imsm_set_array_size(newdev
);
6029 sp
= (void **)id
->dev
;
6035 *space_list
= tofree
;
6038 update_reshape_exit
:
6043 static int apply_takeover_update(struct imsm_update_takeover
*u
,
6044 struct intel_super
*super
,
6047 struct imsm_dev
*dev
= NULL
;
6048 struct intel_dev
*dv
;
6049 struct imsm_dev
*dev_new
;
6050 struct imsm_map
*map
;
6054 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
6055 if (dv
->index
== (unsigned int)u
->subarray
) {
6063 map
= get_imsm_map(dev
, 0);
6065 if (u
->direction
== R10_TO_R0
) {
6066 /* Number of failed disks must be half of initial disk number */
6067 if (imsm_count_failed(super
, dev
) != (map
->num_members
/ 2))
6070 /* iterate through devices to mark removed disks as spare */
6071 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
6072 if (dm
->disk
.status
& FAILED_DISK
) {
6073 int idx
= dm
->index
;
6074 /* update indexes on the disk list */
6075 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
6076 the index values will end up being correct.... NB */
6077 for (du
= super
->disks
; du
; du
= du
->next
)
6078 if (du
->index
> idx
)
6080 /* mark as spare disk */
6081 dm
->disk
.status
= SPARE_DISK
;
6086 map
->num_members
= map
->num_members
/ 2;
6087 map
->map_state
= IMSM_T_STATE_NORMAL
;
6088 map
->num_domains
= 1;
6089 map
->raid_level
= 0;
6090 map
->failed_disk_num
= -1;
6093 if (u
->direction
== R0_TO_R10
) {
6095 /* update slots in current disk list */
6096 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
6100 /* create new *missing* disks */
6101 for (i
= 0; i
< map
->num_members
; i
++) {
6102 space
= *space_list
;
6105 *space_list
= *space
;
6107 memcpy(du
, super
->disks
, sizeof(*du
));
6111 du
->index
= (i
* 2) + 1;
6112 sprintf((char *)du
->disk
.serial
,
6113 " MISSING_%d", du
->index
);
6114 sprintf((char *)du
->serial
,
6115 "MISSING_%d", du
->index
);
6116 du
->next
= super
->missing
;
6117 super
->missing
= du
;
6119 /* create new dev and map */
6120 space
= *space_list
;
6123 *space_list
= *space
;
6124 dev_new
= (void *)space
;
6125 memcpy(dev_new
, dev
, sizeof(*dev
));
6126 /* update new map */
6127 map
= get_imsm_map(dev_new
, 0);
6128 map
->num_members
= map
->num_members
* 2;
6129 map
->map_state
= IMSM_T_STATE_DEGRADED
;
6130 map
->num_domains
= 2;
6131 map
->raid_level
= 1;
6132 /* replace dev<->dev_new */
6135 /* update disk order table */
6136 for (du
= super
->disks
; du
; du
= du
->next
)
6138 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
6139 for (du
= super
->missing
; du
; du
= du
->next
)
6140 if (du
->index
>= 0) {
6141 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
6142 mark_missing(dev_new
, &du
->disk
, du
->index
);
6148 static void imsm_process_update(struct supertype
*st
,
6149 struct metadata_update
*update
)
6152 * crack open the metadata_update envelope to find the update record
6153 * update can be one of:
6154 * update_reshape_container_disks - all the arrays in the container
6155 * are being reshaped to have more devices. We need to mark
6156 * the arrays for general migration and convert selected spares
6157 * into active devices.
6158 * update_activate_spare - a spare device has replaced a failed
6159 * device in an array, update the disk_ord_tbl. If this disk is
6160 * present in all member arrays then also clear the SPARE_DISK
6162 * update_create_array
6164 * update_rename_array
6165 * update_add_remove_disk
6167 struct intel_super
*super
= st
->sb
;
6168 struct imsm_super
*mpb
;
6169 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
6171 /* update requires a larger buf but the allocation failed */
6172 if (super
->next_len
&& !super
->next_buf
) {
6173 super
->next_len
= 0;
6177 if (super
->next_buf
) {
6178 memcpy(super
->next_buf
, super
->buf
, super
->len
);
6180 super
->len
= super
->next_len
;
6181 super
->buf
= super
->next_buf
;
6183 super
->next_len
= 0;
6184 super
->next_buf
= NULL
;
6187 mpb
= super
->anchor
;
6190 case update_takeover
: {
6191 struct imsm_update_takeover
*u
= (void *)update
->buf
;
6192 if (apply_takeover_update(u
, super
, &update
->space_list
)) {
6193 imsm_update_version_info(super
);
6194 super
->updates_pending
++;
6199 case update_reshape_container_disks
: {
6200 struct imsm_update_reshape
*u
= (void *)update
->buf
;
6201 if (apply_reshape_container_disks_update(
6202 u
, super
, &update
->space_list
))
6203 super
->updates_pending
++;
6206 case update_activate_spare
: {
6207 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
6208 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
6209 struct imsm_map
*map
= get_imsm_map(dev
, 0);
6210 struct imsm_map
*migr_map
;
6211 struct active_array
*a
;
6212 struct imsm_disk
*disk
;
6217 int victim
= get_imsm_disk_idx(dev
, u
->slot
, -1);
6220 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
6225 fprintf(stderr
, "error: imsm_activate_spare passed "
6226 "an unknown disk (index: %d)\n",
6231 super
->updates_pending
++;
6233 /* count failures (excluding rebuilds and the victim)
6234 * to determine map[0] state
6237 for (i
= 0; i
< map
->num_members
; i
++) {
6240 disk
= get_imsm_disk(super
,
6241 get_imsm_disk_idx(dev
, i
, -1));
6242 if (!disk
|| is_failed(disk
))
6246 /* adding a pristine spare, assign a new index */
6247 if (dl
->index
< 0) {
6248 dl
->index
= super
->anchor
->num_disks
;
6249 super
->anchor
->num_disks
++;
6252 disk
->status
|= CONFIGURED_DISK
;
6253 disk
->status
&= ~SPARE_DISK
;
6256 to_state
= imsm_check_degraded(super
, dev
, failed
);
6257 map
->map_state
= IMSM_T_STATE_DEGRADED
;
6258 migrate(dev
, to_state
, MIGR_REBUILD
);
6259 migr_map
= get_imsm_map(dev
, 1);
6260 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
6261 set_imsm_ord_tbl_ent(migr_map
, u
->slot
, dl
->index
| IMSM_ORD_REBUILD
);
6263 /* update the family_num to mark a new container
6264 * generation, being careful to record the existing
6265 * family_num in orig_family_num to clean up after
6266 * earlier mdadm versions that neglected to set it.
6268 if (mpb
->orig_family_num
== 0)
6269 mpb
->orig_family_num
= mpb
->family_num
;
6270 mpb
->family_num
+= super
->random
;
6272 /* count arrays using the victim in the metadata */
6274 for (a
= st
->arrays
; a
; a
= a
->next
) {
6275 dev
= get_imsm_dev(super
, a
->info
.container_member
);
6276 map
= get_imsm_map(dev
, 0);
6278 if (get_imsm_disk_slot(map
, victim
) >= 0)
6282 /* delete the victim if it is no longer being
6288 /* We know that 'manager' isn't touching anything,
6289 * so it is safe to delete
6291 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
6292 if ((*dlp
)->index
== victim
)
6295 /* victim may be on the missing list */
6297 for (dlp
= &super
->missing
; *dlp
; dlp
= &(*dlp
)->next
)
6298 if ((*dlp
)->index
== victim
)
6300 imsm_delete(super
, dlp
, victim
);
6304 case update_create_array
: {
6305 /* someone wants to create a new array, we need to be aware of
6306 * a few races/collisions:
6307 * 1/ 'Create' called by two separate instances of mdadm
6308 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
6309 * devices that have since been assimilated via
6311 * In the event this update can not be carried out mdadm will
6312 * (FIX ME) notice that its update did not take hold.
6314 struct imsm_update_create_array
*u
= (void *) update
->buf
;
6315 struct intel_dev
*dv
;
6316 struct imsm_dev
*dev
;
6317 struct imsm_map
*map
, *new_map
;
6318 unsigned long long start
, end
;
6319 unsigned long long new_start
, new_end
;
6321 struct disk_info
*inf
;
6324 /* handle racing creates: first come first serve */
6325 if (u
->dev_idx
< mpb
->num_raid_devs
) {
6326 dprintf("%s: subarray %d already defined\n",
6327 __func__
, u
->dev_idx
);
6331 /* check update is next in sequence */
6332 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
6333 dprintf("%s: can not create array %d expected index %d\n",
6334 __func__
, u
->dev_idx
, mpb
->num_raid_devs
);
6338 new_map
= get_imsm_map(&u
->dev
, 0);
6339 new_start
= __le32_to_cpu(new_map
->pba_of_lba0
);
6340 new_end
= new_start
+ __le32_to_cpu(new_map
->blocks_per_member
);
6341 inf
= get_disk_info(u
);
6343 /* handle activate_spare versus create race:
6344 * check to make sure that overlapping arrays do not include
6347 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6348 dev
= get_imsm_dev(super
, i
);
6349 map
= get_imsm_map(dev
, 0);
6350 start
= __le32_to_cpu(map
->pba_of_lba0
);
6351 end
= start
+ __le32_to_cpu(map
->blocks_per_member
);
6352 if ((new_start
>= start
&& new_start
<= end
) ||
6353 (start
>= new_start
&& start
<= new_end
))
6358 if (disks_overlap(super
, i
, u
)) {
6359 dprintf("%s: arrays overlap\n", __func__
);
6364 /* check that prepare update was successful */
6365 if (!update
->space
) {
6366 dprintf("%s: prepare update failed\n", __func__
);
6370 /* check that all disks are still active before committing
6371 * changes. FIXME: could we instead handle this by creating a
6372 * degraded array? That's probably not what the user expects,
6373 * so better to drop this update on the floor.
6375 for (i
= 0; i
< new_map
->num_members
; i
++) {
6376 dl
= serial_to_dl(inf
[i
].serial
, super
);
6378 dprintf("%s: disk disappeared\n", __func__
);
6383 super
->updates_pending
++;
6385 /* convert spares to members and fixup ord_tbl */
6386 for (i
= 0; i
< new_map
->num_members
; i
++) {
6387 dl
= serial_to_dl(inf
[i
].serial
, super
);
6388 if (dl
->index
== -1) {
6389 dl
->index
= mpb
->num_disks
;
6391 dl
->disk
.status
|= CONFIGURED_DISK
;
6392 dl
->disk
.status
&= ~SPARE_DISK
;
6394 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
6399 update
->space
= NULL
;
6400 imsm_copy_dev(dev
, &u
->dev
);
6401 dv
->index
= u
->dev_idx
;
6402 dv
->next
= super
->devlist
;
6403 super
->devlist
= dv
;
6404 mpb
->num_raid_devs
++;
6406 imsm_update_version_info(super
);
6409 /* mdmon knows how to release update->space, but not
6410 * ((struct intel_dev *) update->space)->dev
6412 if (update
->space
) {
6418 case update_kill_array
: {
6419 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
6420 int victim
= u
->dev_idx
;
6421 struct active_array
*a
;
6422 struct intel_dev
**dp
;
6423 struct imsm_dev
*dev
;
6425 /* sanity check that we are not affecting the uuid of
6426 * active arrays, or deleting an active array
6428 * FIXME when immutable ids are available, but note that
6429 * we'll also need to fixup the invalidated/active
6430 * subarray indexes in mdstat
6432 for (a
= st
->arrays
; a
; a
= a
->next
)
6433 if (a
->info
.container_member
>= victim
)
6435 /* by definition if mdmon is running at least one array
6436 * is active in the container, so checking
6437 * mpb->num_raid_devs is just extra paranoia
6439 dev
= get_imsm_dev(super
, victim
);
6440 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
6441 dprintf("failed to delete subarray-%d\n", victim
);
6445 for (dp
= &super
->devlist
; *dp
;)
6446 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
6449 if ((*dp
)->index
> (unsigned)victim
)
6453 mpb
->num_raid_devs
--;
6454 super
->updates_pending
++;
6457 case update_rename_array
: {
6458 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
6459 char name
[MAX_RAID_SERIAL_LEN
+1];
6460 int target
= u
->dev_idx
;
6461 struct active_array
*a
;
6462 struct imsm_dev
*dev
;
6464 /* sanity check that we are not affecting the uuid of
6467 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
6468 name
[MAX_RAID_SERIAL_LEN
] = '\0';
6469 for (a
= st
->arrays
; a
; a
= a
->next
)
6470 if (a
->info
.container_member
== target
)
6472 dev
= get_imsm_dev(super
, u
->dev_idx
);
6473 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
6474 dprintf("failed to rename subarray-%d\n", target
);
6478 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6479 super
->updates_pending
++;
6482 case update_add_remove_disk
: {
6483 /* we may be able to repair some arrays if disks are
6484 * being added, check teh status of add_remove_disk
6485 * if discs has been added.
6487 if (add_remove_disk_update(super
)) {
6488 struct active_array
*a
;
6490 super
->updates_pending
++;
6491 for (a
= st
->arrays
; a
; a
= a
->next
)
6492 a
->check_degraded
= 1;
6497 fprintf(stderr
, "error: unsuported process update type:"
6498 "(type: %d)\n", type
);
6502 static void imsm_prepare_update(struct supertype
*st
,
6503 struct metadata_update
*update
)
6506 * Allocate space to hold new disk entries, raid-device entries or a new
6507 * mpb if necessary. The manager synchronously waits for updates to
6508 * complete in the monitor, so new mpb buffers allocated here can be
6509 * integrated by the monitor thread without worrying about live pointers
6510 * in the manager thread.
6512 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
6513 struct intel_super
*super
= st
->sb
;
6514 struct imsm_super
*mpb
= super
->anchor
;
6519 case update_takeover
: {
6520 struct imsm_update_takeover
*u
= (void *)update
->buf
;
6521 if (u
->direction
== R0_TO_R10
) {
6522 void **tail
= (void **)&update
->space_list
;
6523 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->subarray
);
6524 struct imsm_map
*map
= get_imsm_map(dev
, 0);
6525 int num_members
= map
->num_members
;
6529 /* allocate memory for added disks */
6530 for (i
= 0; i
< num_members
; i
++) {
6531 size
= sizeof(struct dl
);
6532 space
= malloc(size
);
6541 /* allocate memory for new device */
6542 size
= sizeof_imsm_dev(super
->devlist
->dev
, 0) +
6543 (num_members
* sizeof(__u32
));
6544 space
= malloc(size
);
6553 len
= disks_to_mpb_size(num_members
* 2);
6555 /* if allocation didn't success, free buffer */
6556 while (update
->space_list
) {
6557 void **sp
= update
->space_list
;
6558 update
->space_list
= *sp
;
6566 case update_reshape_container_disks
: {
6567 /* Every raid device in the container is about to
6568 * gain some more devices, and we will enter a
6570 * So each 'imsm_map' will be bigger, and the imsm_vol
6571 * will now hold 2 of them.
6572 * Thus we need new 'struct imsm_dev' allocations sized
6573 * as sizeof_imsm_dev but with more devices in both maps.
6575 struct imsm_update_reshape
*u
= (void *)update
->buf
;
6576 struct intel_dev
*dl
;
6577 void **space_tail
= (void**)&update
->space_list
;
6579 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
6581 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
6582 int size
= sizeof_imsm_dev(dl
->dev
, 1);
6584 if (u
->new_raid_disks
> u
->old_raid_disks
)
6585 size
+= sizeof(__u32
)*2*
6586 (u
->new_raid_disks
- u
->old_raid_disks
);
6595 len
= disks_to_mpb_size(u
->new_raid_disks
);
6596 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
6599 case update_create_array
: {
6600 struct imsm_update_create_array
*u
= (void *) update
->buf
;
6601 struct intel_dev
*dv
;
6602 struct imsm_dev
*dev
= &u
->dev
;
6603 struct imsm_map
*map
= get_imsm_map(dev
, 0);
6605 struct disk_info
*inf
;
6609 inf
= get_disk_info(u
);
6610 len
= sizeof_imsm_dev(dev
, 1);
6611 /* allocate a new super->devlist entry */
6612 dv
= malloc(sizeof(*dv
));
6614 dv
->dev
= malloc(len
);
6619 update
->space
= NULL
;
6623 /* count how many spares will be converted to members */
6624 for (i
= 0; i
< map
->num_members
; i
++) {
6625 dl
= serial_to_dl(inf
[i
].serial
, super
);
6627 /* hmm maybe it failed?, nothing we can do about
6632 if (count_memberships(dl
, super
) == 0)
6635 len
+= activate
* sizeof(struct imsm_disk
);
6642 /* check if we need a larger metadata buffer */
6643 if (super
->next_buf
)
6644 buf_len
= super
->next_len
;
6646 buf_len
= super
->len
;
6648 if (__le32_to_cpu(mpb
->mpb_size
) + len
> buf_len
) {
6649 /* ok we need a larger buf than what is currently allocated
6650 * if this allocation fails process_update will notice that
6651 * ->next_len is set and ->next_buf is NULL
6653 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + len
, 512);
6654 if (super
->next_buf
)
6655 free(super
->next_buf
);
6657 super
->next_len
= buf_len
;
6658 if (posix_memalign(&super
->next_buf
, 512, buf_len
) == 0)
6659 memset(super
->next_buf
, 0, buf_len
);
6661 super
->next_buf
= NULL
;
6665 /* must be called while manager is quiesced */
6666 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
6668 struct imsm_super
*mpb
= super
->anchor
;
6670 struct imsm_dev
*dev
;
6671 struct imsm_map
*map
;
6672 int i
, j
, num_members
;
6675 dprintf("%s: deleting device[%d] from imsm_super\n",
6678 /* shift all indexes down one */
6679 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
6680 if (iter
->index
> (int)index
)
6682 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
6683 if (iter
->index
> (int)index
)
6686 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6687 dev
= get_imsm_dev(super
, i
);
6688 map
= get_imsm_map(dev
, 0);
6689 num_members
= map
->num_members
;
6690 for (j
= 0; j
< num_members
; j
++) {
6691 /* update ord entries being careful not to propagate
6692 * ord-flags to the first map
6694 ord
= get_imsm_ord_tbl_ent(dev
, j
, -1);
6696 if (ord_to_idx(ord
) <= index
)
6699 map
= get_imsm_map(dev
, 0);
6700 set_imsm_ord_tbl_ent(map
, j
, ord_to_idx(ord
- 1));
6701 map
= get_imsm_map(dev
, 1);
6703 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
6708 super
->updates_pending
++;
6710 struct dl
*dl
= *dlp
;
6712 *dlp
= (*dlp
)->next
;
6713 __free_imsm_disk(dl
);
6717 static char disk_by_path
[] = "/dev/disk/by-path/";
6719 static const char *imsm_get_disk_controller_domain(const char *path
)
6721 char disk_path
[PATH_MAX
];
6725 strncpy(disk_path
, disk_by_path
, PATH_MAX
- 1);
6726 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
6727 if (stat(disk_path
, &st
) == 0) {
6728 struct sys_dev
* hba
;
6731 path
= devt_to_devpath(st
.st_rdev
);
6734 hba
= find_disk_attached_hba(-1, path
);
6735 if (hba
&& hba
->type
== SYS_DEV_SAS
)
6737 else if (hba
&& hba
->type
== SYS_DEV_SATA
)
6741 dprintf("path: %s hba: %s attached: %s\n",
6742 path
, (hba
) ? hba
->path
: "NULL", drv
);
6750 static int imsm_find_array_minor_by_subdev(int subdev
, int container
, int *minor
)
6752 char subdev_name
[20];
6753 struct mdstat_ent
*mdstat
;
6755 sprintf(subdev_name
, "%d", subdev
);
6756 mdstat
= mdstat_by_subdev(subdev_name
, container
);
6760 *minor
= mdstat
->devnum
;
6761 free_mdstat(mdstat
);
6765 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
6766 struct geo_params
*geo
,
6767 int *old_raid_disks
)
6769 /* currently we only support increasing the number of devices
6770 * for a container. This increases the number of device for each
6771 * member array. They must all be RAID0 or RAID5.
6774 struct mdinfo
*info
, *member
;
6775 int devices_that_can_grow
= 0;
6777 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): "
6778 "st->devnum = (%i)\n",
6781 if (geo
->size
!= -1 ||
6782 geo
->level
!= UnSet
||
6783 geo
->layout
!= UnSet
||
6784 geo
->chunksize
!= 0 ||
6785 geo
->raid_disks
== UnSet
) {
6786 dprintf("imsm: Container operation is allowed for "
6787 "raid disks number change only.\n");
6791 info
= container_content_imsm(st
, NULL
);
6792 for (member
= info
; member
; member
= member
->next
) {
6796 dprintf("imsm: checking device_num: %i\n",
6797 member
->container_member
);
6799 if (geo
->raid_disks
<= member
->array
.raid_disks
) {
6800 /* we work on container for Online Capacity Expansion
6801 * only so raid_disks has to grow
6803 dprintf("imsm: for container operation raid disks "
6804 "increase is required\n");
6808 if ((info
->array
.level
!= 0) &&
6809 (info
->array
.level
!= 5)) {
6810 /* we cannot use this container with other raid level
6812 dprintf("imsm: for container operation wrong"
6813 " raid level (%i) detected\n",
6817 /* check for platform support
6818 * for this raid level configuration
6820 struct intel_super
*super
= st
->sb
;
6821 if (!is_raid_level_supported(super
->orom
,
6822 member
->array
.level
,
6824 dprintf("platform does not support raid%d with"
6828 geo
->raid_disks
> 1 ? "s" : "");
6833 if (*old_raid_disks
&&
6834 info
->array
.raid_disks
!= *old_raid_disks
)
6836 *old_raid_disks
= info
->array
.raid_disks
;
6838 /* All raid5 and raid0 volumes in container
6839 * have to be ready for Online Capacity Expansion
6840 * so they need to be assembled. We have already
6841 * checked that no recovery etc is happening.
6843 result
= imsm_find_array_minor_by_subdev(member
->container_member
,
6847 dprintf("imsm: cannot find array\n");
6850 devices_that_can_grow
++;
6853 if (!member
&& devices_that_can_grow
)
6857 dprintf("\tContainer operation allowed\n");
6859 dprintf("\tError: %i\n", ret_val
);
6864 /* Function: get_spares_for_grow
6865 * Description: Allocates memory and creates list of spare devices
6866 * avaliable in container. Checks if spare drive size is acceptable.
6867 * Parameters: Pointer to the supertype structure
6868 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
6871 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
6873 unsigned long long min_size
= min_acceptable_spare_size_imsm(st
);
6874 return container_choose_spares(st
, min_size
, NULL
, NULL
, NULL
, 0);
6877 /******************************************************************************
6878 * function: imsm_create_metadata_update_for_reshape
6879 * Function creates update for whole IMSM container.
6881 ******************************************************************************/
6882 static int imsm_create_metadata_update_for_reshape(
6883 struct supertype
*st
,
6884 struct geo_params
*geo
,
6886 struct imsm_update_reshape
**updatep
)
6888 struct intel_super
*super
= st
->sb
;
6889 struct imsm_super
*mpb
= super
->anchor
;
6890 int update_memory_size
= 0;
6891 struct imsm_update_reshape
*u
= NULL
;
6892 struct mdinfo
*spares
= NULL
;
6894 int delta_disks
= 0;
6897 dprintf("imsm_update_metadata_for_reshape(enter) raid_disks = %i\n",
6900 delta_disks
= geo
->raid_disks
- old_raid_disks
;
6902 /* size of all update data without anchor */
6903 update_memory_size
= sizeof(struct imsm_update_reshape
);
6905 /* now add space for spare disks that we need to add. */
6906 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
6908 u
= calloc(1, update_memory_size
);
6911 "cannot get memory for imsm_update_reshape update\n");
6914 u
->type
= update_reshape_container_disks
;
6915 u
->old_raid_disks
= old_raid_disks
;
6916 u
->new_raid_disks
= geo
->raid_disks
;
6918 /* now get spare disks list
6920 spares
= get_spares_for_grow(st
);
6923 || delta_disks
> spares
->array
.spare_disks
) {
6924 fprintf(stderr
, Name
": imsm: ERROR: Cannot get spare devices "
6925 "for %s.\n", geo
->dev_name
);
6929 /* we have got spares
6930 * update disk list in imsm_disk list table in anchor
6932 dprintf("imsm: %i spares are available.\n\n",
6933 spares
->array
.spare_disks
);
6936 for (i
= 0; i
< delta_disks
; i
++) {
6941 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
6943 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
6944 dl
->index
= mpb
->num_disks
;
6954 dprintf("imsm: reshape update preparation :");
6955 if (i
== delta_disks
) {
6958 return update_memory_size
;
6961 dprintf(" Error\n");
6966 static void imsm_update_metadata_locally(struct supertype
*st
,
6969 struct metadata_update mu
;
6974 mu
.space_list
= NULL
;
6976 imsm_prepare_update(st
, &mu
);
6977 imsm_process_update(st
, &mu
);
6979 while (mu
.space_list
) {
6980 void **space
= mu
.space_list
;
6981 mu
.space_list
= *space
;
6986 /***************************************************************************
6987 * Function: imsm_analyze_change
6988 * Description: Function analyze change for single volume
6989 * and validate if transition is supported
6990 * Parameters: Geometry parameters, supertype structure
6991 * Returns: Operation type code on success, -1 if fail
6992 ****************************************************************************/
6993 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
6994 struct geo_params
*geo
)
7001 getinfo_super_imsm_volume(st
, &info
, NULL
);
7003 if ((geo
->level
!= info
.array
.level
) &&
7004 (geo
->level
>= 0) &&
7005 (geo
->level
!= UnSet
)) {
7006 switch (info
.array
.level
) {
7008 if (geo
->level
== 5) {
7009 change
= CH_MIGRATION
;
7012 if (geo
->level
== 10) {
7013 change
= CH_TAKEOVER
;
7018 if (geo
->level
== 0) {
7019 change
= CH_TAKEOVER
;
7024 if (geo
->level
== 0)
7025 change
= CH_MIGRATION
;
7028 if (geo
->level
== 0) {
7029 change
= CH_TAKEOVER
;
7036 Name
" Error. Level Migration from %d to %d "
7038 info
.array
.level
, geo
->level
);
7039 goto analyse_change_exit
;
7042 geo
->level
= info
.array
.level
;
7044 if ((geo
->layout
!= info
.array
.layout
)
7045 && ((geo
->layout
!= UnSet
) && (geo
->layout
!= -1))) {
7046 change
= CH_MIGRATION
;
7047 if ((info
.array
.layout
== 0)
7048 && (info
.array
.level
== 5)
7049 && (geo
->layout
== 5)) {
7050 /* reshape 5 -> 4 */
7051 } else if ((info
.array
.layout
== 5)
7052 && (info
.array
.level
== 5)
7053 && (geo
->layout
== 0)) {
7054 /* reshape 4 -> 5 */
7059 Name
" Error. Layout Migration from %d to %d "
7061 info
.array
.layout
, geo
->layout
);
7063 goto analyse_change_exit
;
7066 geo
->layout
= info
.array
.layout
;
7068 if ((geo
->chunksize
> 0) && (geo
->chunksize
!= UnSet
)
7069 && (geo
->chunksize
!= info
.array
.chunk_size
))
7070 change
= CH_MIGRATION
;
7072 geo
->chunksize
= info
.array
.chunk_size
;
7074 chunk
= geo
->chunksize
/ 1024;
7075 if (!validate_geometry_imsm(st
,
7085 struct intel_super
*super
= st
->sb
;
7086 struct imsm_super
*mpb
= super
->anchor
;
7088 if (mpb
->num_raid_devs
> 1) {
7090 Name
" Error. Cannot perform operation on %s"
7091 "- for this operation it MUST be single "
7092 "array in container\n",
7098 analyse_change_exit
:
7103 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
7105 struct intel_super
*super
= st
->sb
;
7106 struct imsm_update_takeover
*u
;
7108 u
= malloc(sizeof(struct imsm_update_takeover
));
7112 u
->type
= update_takeover
;
7113 u
->subarray
= super
->current_vol
;
7115 /* 10->0 transition */
7116 if (geo
->level
== 0)
7117 u
->direction
= R10_TO_R0
;
7119 /* 0->10 transition */
7120 if (geo
->level
== 10)
7121 u
->direction
= R0_TO_R10
;
7123 /* update metadata locally */
7124 imsm_update_metadata_locally(st
, u
,
7125 sizeof(struct imsm_update_takeover
));
7126 /* and possibly remotely */
7127 if (st
->update_tail
)
7128 append_metadata_update(st
, u
,
7129 sizeof(struct imsm_update_takeover
));
7136 static int imsm_reshape_super(struct supertype
*st
, long long size
, int level
,
7137 int layout
, int chunksize
, int raid_disks
,
7138 int delta_disks
, char *backup
, char *dev
,
7142 struct geo_params geo
;
7144 dprintf("imsm: reshape_super called.\n");
7146 memset(&geo
, 0, sizeof(struct geo_params
));
7149 geo
.dev_id
= st
->devnum
;
7152 geo
.layout
= layout
;
7153 geo
.chunksize
= chunksize
;
7154 geo
.raid_disks
= raid_disks
;
7155 if (delta_disks
!= UnSet
)
7156 geo
.raid_disks
+= delta_disks
;
7158 dprintf("\tfor level : %i\n", geo
.level
);
7159 dprintf("\tfor raid_disks : %i\n", geo
.raid_disks
);
7161 if (experimental() == 0)
7164 if (st
->container_dev
== st
->devnum
) {
7165 /* On container level we can only increase number of devices. */
7166 dprintf("imsm: info: Container operation\n");
7167 int old_raid_disks
= 0;
7168 if (imsm_reshape_is_allowed_on_container(
7169 st
, &geo
, &old_raid_disks
)) {
7170 struct imsm_update_reshape
*u
= NULL
;
7173 len
= imsm_create_metadata_update_for_reshape(
7174 st
, &geo
, old_raid_disks
, &u
);
7177 dprintf("imsm: Cannot prepare update\n");
7178 goto exit_imsm_reshape_super
;
7182 /* update metadata locally */
7183 imsm_update_metadata_locally(st
, u
, len
);
7184 /* and possibly remotely */
7185 if (st
->update_tail
)
7186 append_metadata_update(st
, u
, len
);
7191 fprintf(stderr
, Name
": (imsm) Operation "
7192 "is not allowed on this container\n");
7195 /* On volume level we support following operations
7196 * - takeover: raid10 -> raid0; raid0 -> raid10
7197 * - chunk size migration
7198 * - migration: raid5 -> raid0; raid0 -> raid5
7200 struct intel_super
*super
= st
->sb
;
7201 struct intel_dev
*dev
= super
->devlist
;
7203 dprintf("imsm: info: Volume operation\n");
7204 /* find requested device */
7206 imsm_find_array_minor_by_subdev(dev
->index
, st
->container_dev
, &devnum
);
7207 if (devnum
== geo
.dev_id
)
7212 fprintf(stderr
, Name
" Cannot find %s (%i) subarray\n",
7213 geo
.dev_name
, geo
.dev_id
);
7214 goto exit_imsm_reshape_super
;
7216 super
->current_vol
= dev
->index
;
7217 change
= imsm_analyze_change(st
, &geo
);
7220 ret_val
= imsm_takeover(st
, &geo
);
7230 exit_imsm_reshape_super
:
7231 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
7235 static int imsm_manage_reshape(
7236 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
7237 struct supertype
*st
, unsigned long stripes
,
7238 int *fds
, unsigned long long *offsets
,
7239 int dests
, int *destfd
, unsigned long long *destoffsets
)
7241 /* Just use child_monitor for now */
7242 return child_monitor(
7243 afd
, sra
, reshape
, st
, stripes
,
7244 fds
, offsets
, dests
, destfd
, destoffsets
);
7246 #endif /* MDASSEMBLE */
7248 struct superswitch super_imsm
= {
7250 .examine_super
= examine_super_imsm
,
7251 .brief_examine_super
= brief_examine_super_imsm
,
7252 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
7253 .export_examine_super
= export_examine_super_imsm
,
7254 .detail_super
= detail_super_imsm
,
7255 .brief_detail_super
= brief_detail_super_imsm
,
7256 .write_init_super
= write_init_super_imsm
,
7257 .validate_geometry
= validate_geometry_imsm
,
7258 .add_to_super
= add_to_super_imsm
,
7259 .remove_from_super
= remove_from_super_imsm
,
7260 .detail_platform
= detail_platform_imsm
,
7261 .kill_subarray
= kill_subarray_imsm
,
7262 .update_subarray
= update_subarray_imsm
,
7263 .load_container
= load_container_imsm
,
7264 .default_geometry
= default_geometry_imsm
,
7265 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
7266 .reshape_super
= imsm_reshape_super
,
7267 .manage_reshape
= imsm_manage_reshape
,
7269 .match_home
= match_home_imsm
,
7270 .uuid_from_super
= uuid_from_super_imsm
,
7271 .getinfo_super
= getinfo_super_imsm
,
7272 .getinfo_super_disks
= getinfo_super_disks_imsm
,
7273 .update_super
= update_super_imsm
,
7275 .avail_size
= avail_size_imsm
,
7276 .min_acceptable_spare_size
= min_acceptable_spare_size_imsm
,
7278 .compare_super
= compare_super_imsm
,
7280 .load_super
= load_super_imsm
,
7281 .init_super
= init_super_imsm
,
7282 .store_super
= store_super_imsm
,
7283 .free_super
= free_super_imsm
,
7284 .match_metadata_desc
= match_metadata_desc_imsm
,
7285 .container_content
= container_content_imsm
,
7292 .open_new
= imsm_open_new
,
7293 .set_array_state
= imsm_set_array_state
,
7294 .set_disk
= imsm_set_disk
,
7295 .sync_metadata
= imsm_sync_metadata
,
7296 .activate_spare
= imsm_activate_spare
,
7297 .process_update
= imsm_process_update
,
7298 .prepare_update
= imsm_prepare_update
,
7299 #endif /* MDASSEMBLE */