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
;
240 /* internal representation of IMSM metadata */
243 void *buf
; /* O_DIRECT buffer for reading/writing metadata */
244 struct imsm_super
*anchor
; /* immovable parameters */
246 size_t len
; /* size of the 'buf' allocation */
247 void *next_buf
; /* for realloc'ing buf from the manager */
249 int updates_pending
; /* count of pending updates for mdmon */
250 int current_vol
; /* index of raid device undergoing creation */
251 __u32 create_offset
; /* common start for 'current_vol' */
252 __u32 random
; /* random data for seeding new family numbers */
253 struct intel_dev
*devlist
;
257 __u8 serial
[MAX_RAID_SERIAL_LEN
];
260 struct imsm_disk disk
;
263 struct extent
*e
; /* for determining freespace @ create */
264 int raiddisk
; /* slot to fill in autolayout */
267 struct dl
*disk_mgmt_list
; /* list of disks to add/remove while mdmon
269 struct dl
*missing
; /* disks removed while we weren't looking */
270 struct bbm_log
*bbm_log
;
271 const char *hba
; /* device path of the raid controller for this metadata */
272 const struct imsm_orom
*orom
; /* platform firmware support */
273 struct intel_super
*next
; /* (temp) list for disambiguating family_num */
277 struct imsm_disk disk
;
278 #define IMSM_UNKNOWN_OWNER (-1)
280 struct intel_disk
*next
;
284 unsigned long long start
, size
;
287 /* definitions of reshape process types */
288 enum imsm_reshape_type
{
294 /* definition of messages passed to imsm_process_update */
295 enum imsm_update_type
{
296 update_activate_spare
,
300 update_add_remove_disk
,
301 update_reshape_container_disks
,
305 struct imsm_update_activate_spare
{
306 enum imsm_update_type type
;
310 struct imsm_update_activate_spare
*next
;
323 enum takeover_direction
{
327 struct imsm_update_takeover
{
328 enum imsm_update_type type
;
330 enum takeover_direction direction
;
333 struct imsm_update_reshape
{
334 enum imsm_update_type type
;
337 int new_disks
[1]; /* new_raid_disks - old_raid_disks makedev number */
341 __u8 serial
[MAX_RAID_SERIAL_LEN
];
344 struct imsm_update_create_array
{
345 enum imsm_update_type type
;
350 struct imsm_update_kill_array
{
351 enum imsm_update_type type
;
355 struct imsm_update_rename_array
{
356 enum imsm_update_type type
;
357 __u8 name
[MAX_RAID_SERIAL_LEN
];
361 struct imsm_update_add_remove_disk
{
362 enum imsm_update_type type
;
365 static struct supertype
*match_metadata_desc_imsm(char *arg
)
367 struct supertype
*st
;
369 if (strcmp(arg
, "imsm") != 0 &&
370 strcmp(arg
, "default") != 0
374 st
= malloc(sizeof(*st
));
377 memset(st
, 0, sizeof(*st
));
378 st
->container_dev
= NoMdDev
;
379 st
->ss
= &super_imsm
;
380 st
->max_devs
= IMSM_MAX_DEVICES
;
381 st
->minor_version
= 0;
387 static __u8
*get_imsm_version(struct imsm_super
*mpb
)
389 return &mpb
->sig
[MPB_SIG_LEN
];
393 /* retrieve a disk directly from the anchor when the anchor is known to be
394 * up-to-date, currently only at load time
396 static struct imsm_disk
*__get_imsm_disk(struct imsm_super
*mpb
, __u8 index
)
398 if (index
>= mpb
->num_disks
)
400 return &mpb
->disk
[index
];
403 /* retrieve the disk description based on a index of the disk
406 static struct dl
*get_imsm_dl_disk(struct intel_super
*super
, __u8 index
)
410 for (d
= super
->disks
; d
; d
= d
->next
)
411 if (d
->index
== index
)
416 /* retrieve a disk from the parsed metadata */
417 static struct imsm_disk
*get_imsm_disk(struct intel_super
*super
, __u8 index
)
421 dl
= get_imsm_dl_disk(super
, index
);
428 /* generate a checksum directly from the anchor when the anchor is known to be
429 * up-to-date, currently only at load or write_super after coalescing
431 static __u32
__gen_imsm_checksum(struct imsm_super
*mpb
)
433 __u32 end
= mpb
->mpb_size
/ sizeof(end
);
434 __u32
*p
= (__u32
*) mpb
;
438 sum
+= __le32_to_cpu(*p
);
442 return sum
- __le32_to_cpu(mpb
->check_sum
);
445 static size_t sizeof_imsm_map(struct imsm_map
*map
)
447 return sizeof(struct imsm_map
) + sizeof(__u32
) * (map
->num_members
- 1);
450 struct imsm_map
*get_imsm_map(struct imsm_dev
*dev
, int second_map
)
452 struct imsm_map
*map
= &dev
->vol
.map
[0];
454 if (second_map
&& !dev
->vol
.migr_state
)
456 else if (second_map
) {
459 return ptr
+ sizeof_imsm_map(map
);
465 /* return the size of the device.
466 * migr_state increases the returned size if map[0] were to be duplicated
468 static size_t sizeof_imsm_dev(struct imsm_dev
*dev
, int migr_state
)
470 size_t size
= sizeof(*dev
) - sizeof(struct imsm_map
) +
471 sizeof_imsm_map(get_imsm_map(dev
, 0));
473 /* migrating means an additional map */
474 if (dev
->vol
.migr_state
)
475 size
+= sizeof_imsm_map(get_imsm_map(dev
, 1));
477 size
+= sizeof_imsm_map(get_imsm_map(dev
, 0));
483 /* retrieve disk serial number list from a metadata update */
484 static struct disk_info
*get_disk_info(struct imsm_update_create_array
*update
)
487 struct disk_info
*inf
;
489 inf
= u
+ sizeof(*update
) - sizeof(struct imsm_dev
) +
490 sizeof_imsm_dev(&update
->dev
, 0);
496 static struct imsm_dev
*__get_imsm_dev(struct imsm_super
*mpb
, __u8 index
)
502 if (index
>= mpb
->num_raid_devs
)
505 /* devices start after all disks */
506 offset
= ((void *) &mpb
->disk
[mpb
->num_disks
]) - _mpb
;
508 for (i
= 0; i
<= index
; i
++)
510 return _mpb
+ offset
;
512 offset
+= sizeof_imsm_dev(_mpb
+ offset
, 0);
517 static struct imsm_dev
*get_imsm_dev(struct intel_super
*super
, __u8 index
)
519 struct intel_dev
*dv
;
521 if (index
>= super
->anchor
->num_raid_devs
)
523 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
524 if (dv
->index
== index
)
532 * == 1 get second map
533 * == -1 than get map according to the current migr_state
535 static __u32
get_imsm_ord_tbl_ent(struct imsm_dev
*dev
,
539 struct imsm_map
*map
;
541 if (second_map
== -1) {
542 if (dev
->vol
.migr_state
)
543 map
= get_imsm_map(dev
, 1);
545 map
= get_imsm_map(dev
, 0);
547 map
= get_imsm_map(dev
, second_map
);
550 /* top byte identifies disk under rebuild */
551 return __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
554 #define ord_to_idx(ord) (((ord) << 8) >> 8)
555 static __u32
get_imsm_disk_idx(struct imsm_dev
*dev
, int slot
, int second_map
)
557 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
, second_map
);
559 return ord_to_idx(ord
);
562 static void set_imsm_ord_tbl_ent(struct imsm_map
*map
, int slot
, __u32 ord
)
564 map
->disk_ord_tbl
[slot
] = __cpu_to_le32(ord
);
567 static int get_imsm_disk_slot(struct imsm_map
*map
, unsigned idx
)
572 for (slot
= 0; slot
< map
->num_members
; slot
++) {
573 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
574 if (ord_to_idx(ord
) == idx
)
581 static int get_imsm_raid_level(struct imsm_map
*map
)
583 if (map
->raid_level
== 1) {
584 if (map
->num_members
== 2)
590 return map
->raid_level
;
593 static int cmp_extent(const void *av
, const void *bv
)
595 const struct extent
*a
= av
;
596 const struct extent
*b
= bv
;
597 if (a
->start
< b
->start
)
599 if (a
->start
> b
->start
)
604 static int count_memberships(struct dl
*dl
, struct intel_super
*super
)
609 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
610 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
611 struct imsm_map
*map
= get_imsm_map(dev
, 0);
613 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
620 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
)
622 /* find a list of used extents on the given physical device */
623 struct extent
*rv
, *e
;
625 int memberships
= count_memberships(dl
, super
);
626 __u32 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
628 rv
= malloc(sizeof(struct extent
) * (memberships
+ 1));
633 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
634 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
635 struct imsm_map
*map
= get_imsm_map(dev
, 0);
637 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
638 e
->start
= __le32_to_cpu(map
->pba_of_lba0
);
639 e
->size
= __le32_to_cpu(map
->blocks_per_member
);
643 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
645 /* determine the start of the metadata
646 * when no raid devices are defined use the default
647 * ...otherwise allow the metadata to truncate the value
648 * as is the case with older versions of imsm
651 struct extent
*last
= &rv
[memberships
- 1];
654 remainder
= __le32_to_cpu(dl
->disk
.total_blocks
) -
655 (last
->start
+ last
->size
);
656 /* round down to 1k block to satisfy precision of the kernel
660 /* make sure remainder is still sane */
661 if (remainder
< (unsigned)ROUND_UP(super
->len
, 512) >> 9)
662 remainder
= ROUND_UP(super
->len
, 512) >> 9;
663 if (reservation
> remainder
)
664 reservation
= remainder
;
666 e
->start
= __le32_to_cpu(dl
->disk
.total_blocks
) - reservation
;
671 /* try to determine how much space is reserved for metadata from
672 * the last get_extents() entry, otherwise fallback to the
675 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
681 /* for spares just return a minimal reservation which will grow
682 * once the spare is picked up by an array
685 return MPB_SECTOR_CNT
;
687 e
= get_extents(super
, dl
);
689 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
691 /* scroll to last entry */
692 for (i
= 0; e
[i
].size
; i
++)
695 rv
= __le32_to_cpu(dl
->disk
.total_blocks
) - e
[i
].start
;
702 static int is_spare(struct imsm_disk
*disk
)
704 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
707 static int is_configured(struct imsm_disk
*disk
)
709 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
712 static int is_failed(struct imsm_disk
*disk
)
714 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
717 /* Return minimum size of a spare that can be used in this array*/
718 static unsigned long long min_acceptable_spare_size_imsm(struct supertype
*st
)
720 struct intel_super
*super
= st
->sb
;
724 unsigned long long rv
= 0;
728 /* find first active disk in array */
730 while (dl
&& (is_failed(&dl
->disk
) || dl
->index
== -1))
734 /* find last lba used by subarrays */
735 e
= get_extents(super
, dl
);
738 for (i
= 0; e
[i
].size
; i
++)
741 rv
= e
[i
-1].start
+ e
[i
-1].size
;
743 /* add the amount of space needed for metadata */
744 rv
= rv
+ MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
749 static __u64
blocks_per_migr_unit(struct imsm_dev
*dev
);
751 static void print_imsm_dev(struct imsm_dev
*dev
, char *uuid
, int disk_idx
)
755 struct imsm_map
*map
= get_imsm_map(dev
, 0);
756 struct imsm_map
*map2
= get_imsm_map(dev
, 1);
760 printf("[%.16s]:\n", dev
->volume
);
761 printf(" UUID : %s\n", uuid
);
762 printf(" RAID Level : %d", get_imsm_raid_level(map
));
764 printf(" <-- %d", get_imsm_raid_level(map2
));
766 printf(" Members : %d", map
->num_members
);
768 printf(" <-- %d", map2
->num_members
);
770 printf(" Slots : [");
771 for (i
= 0; i
< map
->num_members
; i
++) {
772 ord
= get_imsm_ord_tbl_ent(dev
, i
, 0);
773 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
778 for (i
= 0; i
< map2
->num_members
; i
++) {
779 ord
= get_imsm_ord_tbl_ent(dev
, i
, 1);
780 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
785 slot
= get_imsm_disk_slot(map
, disk_idx
);
787 ord
= get_imsm_ord_tbl_ent(dev
, slot
, -1);
788 printf(" This Slot : %d%s\n", slot
,
789 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
791 printf(" This Slot : ?\n");
792 sz
= __le32_to_cpu(dev
->size_high
);
794 sz
+= __le32_to_cpu(dev
->size_low
);
795 printf(" Array Size : %llu%s\n", (unsigned long long)sz
,
796 human_size(sz
* 512));
797 sz
= __le32_to_cpu(map
->blocks_per_member
);
798 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz
,
799 human_size(sz
* 512));
800 printf(" Sector Offset : %u\n",
801 __le32_to_cpu(map
->pba_of_lba0
));
802 printf(" Num Stripes : %u\n",
803 __le32_to_cpu(map
->num_data_stripes
));
804 printf(" Chunk Size : %u KiB",
805 __le16_to_cpu(map
->blocks_per_strip
) / 2);
807 printf(" <-- %u KiB",
808 __le16_to_cpu(map2
->blocks_per_strip
) / 2);
810 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
811 printf(" Migrate State : ");
812 if (dev
->vol
.migr_state
) {
813 if (migr_type(dev
) == MIGR_INIT
)
814 printf("initialize\n");
815 else if (migr_type(dev
) == MIGR_REBUILD
)
817 else if (migr_type(dev
) == MIGR_VERIFY
)
819 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
820 printf("general migration\n");
821 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
822 printf("state change\n");
823 else if (migr_type(dev
) == MIGR_REPAIR
)
826 printf("<unknown:%d>\n", migr_type(dev
));
829 printf(" Map State : %s", map_state_str
[map
->map_state
]);
830 if (dev
->vol
.migr_state
) {
831 struct imsm_map
*map
= get_imsm_map(dev
, 1);
833 printf(" <-- %s", map_state_str
[map
->map_state
]);
834 printf("\n Checkpoint : %u (%llu)",
835 __le32_to_cpu(dev
->vol
.curr_migr_unit
),
836 (unsigned long long)blocks_per_migr_unit(dev
));
839 printf(" Dirty State : %s\n", dev
->vol
.dirty
? "dirty" : "clean");
842 static void print_imsm_disk(struct imsm_super
*mpb
, int index
, __u32 reserved
)
844 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, index
);
845 char str
[MAX_RAID_SERIAL_LEN
+ 1];
848 if (index
< 0 || !disk
)
852 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
853 printf(" Disk%02d Serial : %s\n", index
, str
);
854 printf(" State :%s%s%s\n", is_spare(disk
) ? " spare" : "",
855 is_configured(disk
) ? " active" : "",
856 is_failed(disk
) ? " failed" : "");
857 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
858 sz
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
859 printf(" Usable Size : %llu%s\n", (unsigned long long)sz
,
860 human_size(sz
* 512));
863 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
865 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
867 struct intel_super
*super
= st
->sb
;
868 struct imsm_super
*mpb
= super
->anchor
;
869 char str
[MAX_SIGNATURE_LENGTH
];
874 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
877 snprintf(str
, MPB_SIG_LEN
, "%s", mpb
->sig
);
878 printf(" Magic : %s\n", str
);
879 snprintf(str
, strlen(MPB_VERSION_RAID0
), "%s", get_imsm_version(mpb
));
880 printf(" Version : %s\n", get_imsm_version(mpb
));
881 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
882 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
883 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
884 getinfo_super_imsm(st
, &info
, NULL
);
885 fname_from_uuid(st
, &info
, nbuf
, ':');
886 printf(" UUID : %s\n", nbuf
+ 5);
887 sum
= __le32_to_cpu(mpb
->check_sum
);
888 printf(" Checksum : %08x %s\n", sum
,
889 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
890 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
));
891 printf(" Disks : %d\n", mpb
->num_disks
);
892 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
893 print_imsm_disk(mpb
, super
->disks
->index
, reserved
);
894 if (super
->bbm_log
) {
895 struct bbm_log
*log
= super
->bbm_log
;
898 printf("Bad Block Management Log:\n");
899 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
900 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
901 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
902 printf(" Spare Blocks : %d\n", __le32_to_cpu(log
->reserved_spare_block_count
));
903 printf(" First Spare : %llx\n",
904 (unsigned long long) __le64_to_cpu(log
->first_spare_lba
));
906 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
908 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
910 super
->current_vol
= i
;
911 getinfo_super_imsm(st
, &info
, NULL
);
912 fname_from_uuid(st
, &info
, nbuf
, ':');
913 print_imsm_dev(dev
, nbuf
+ 5, super
->disks
->index
);
915 for (i
= 0; i
< mpb
->num_disks
; i
++) {
916 if (i
== super
->disks
->index
)
918 print_imsm_disk(mpb
, i
, reserved
);
920 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
921 struct imsm_disk
*disk
;
922 char str
[MAX_RAID_SERIAL_LEN
+ 1];
930 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
931 printf(" Disk Serial : %s\n", str
);
932 printf(" State :%s%s%s\n", is_spare(disk
) ? " spare" : "",
933 is_configured(disk
) ? " active" : "",
934 is_failed(disk
) ? " failed" : "");
935 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
936 sz
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
937 printf(" Usable Size : %llu%s\n", (unsigned long long)sz
,
938 human_size(sz
* 512));
942 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
944 /* We just write a generic IMSM ARRAY entry */
947 struct intel_super
*super
= st
->sb
;
949 if (!super
->anchor
->num_raid_devs
) {
950 printf("ARRAY metadata=imsm\n");
954 getinfo_super_imsm(st
, &info
, NULL
);
955 fname_from_uuid(st
, &info
, nbuf
, ':');
956 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
959 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
961 /* We just write a generic IMSM ARRAY entry */
965 struct intel_super
*super
= st
->sb
;
968 if (!super
->anchor
->num_raid_devs
)
971 getinfo_super_imsm(st
, &info
, NULL
);
972 fname_from_uuid(st
, &info
, nbuf
, ':');
973 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
974 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
976 super
->current_vol
= i
;
977 getinfo_super_imsm(st
, &info
, NULL
);
978 fname_from_uuid(st
, &info
, nbuf1
, ':');
979 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
980 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
984 static void export_examine_super_imsm(struct supertype
*st
)
986 struct intel_super
*super
= st
->sb
;
987 struct imsm_super
*mpb
= super
->anchor
;
991 getinfo_super_imsm(st
, &info
, NULL
);
992 fname_from_uuid(st
, &info
, nbuf
, ':');
993 printf("MD_METADATA=imsm\n");
994 printf("MD_LEVEL=container\n");
995 printf("MD_UUID=%s\n", nbuf
+5);
996 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
999 static void detail_super_imsm(struct supertype
*st
, char *homehost
)
1004 getinfo_super_imsm(st
, &info
, NULL
);
1005 fname_from_uuid(st
, &info
, nbuf
, ':');
1006 printf("\n UUID : %s\n", nbuf
+ 5);
1009 static void brief_detail_super_imsm(struct supertype
*st
)
1013 getinfo_super_imsm(st
, &info
, NULL
);
1014 fname_from_uuid(st
, &info
, nbuf
, ':');
1015 printf(" UUID=%s", nbuf
+ 5);
1018 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
);
1019 static void fd2devname(int fd
, char *name
);
1021 static int imsm_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
1023 /* dump an unsorted list of devices attached to ahci, as well as
1024 * non-connected ports
1026 int hba_len
= strlen(hba_path
) + 1;
1031 unsigned long port_mask
= (1 << port_count
) - 1;
1033 if (port_count
> (int)sizeof(port_mask
) * 8) {
1035 fprintf(stderr
, Name
": port_count %d out of range\n", port_count
);
1039 /* scroll through /sys/dev/block looking for devices attached to
1042 dir
= opendir("/sys/dev/block");
1043 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
1054 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
1056 path
= devt_to_devpath(makedev(major
, minor
));
1059 if (!path_attached_to_hba(path
, hba_path
)) {
1065 /* retrieve the scsi device type */
1066 if (asprintf(&device
, "/sys/dev/block/%d:%d/device/xxxxxxx", major
, minor
) < 0) {
1068 fprintf(stderr
, Name
": failed to allocate 'device'\n");
1072 sprintf(device
, "/sys/dev/block/%d:%d/device/type", major
, minor
);
1073 if (load_sys(device
, buf
) != 0) {
1075 fprintf(stderr
, Name
": failed to read device type for %s\n",
1081 type
= strtoul(buf
, NULL
, 10);
1083 /* if it's not a disk print the vendor and model */
1084 if (!(type
== 0 || type
== 7 || type
== 14)) {
1087 sprintf(device
, "/sys/dev/block/%d:%d/device/vendor", major
, minor
);
1088 if (load_sys(device
, buf
) == 0) {
1089 strncpy(vendor
, buf
, sizeof(vendor
));
1090 vendor
[sizeof(vendor
) - 1] = '\0';
1091 c
= (char *) &vendor
[sizeof(vendor
) - 1];
1092 while (isspace(*c
) || *c
== '\0')
1096 sprintf(device
, "/sys/dev/block/%d:%d/device/model", major
, minor
);
1097 if (load_sys(device
, buf
) == 0) {
1098 strncpy(model
, buf
, sizeof(model
));
1099 model
[sizeof(model
) - 1] = '\0';
1100 c
= (char *) &model
[sizeof(model
) - 1];
1101 while (isspace(*c
) || *c
== '\0')
1105 if (vendor
[0] && model
[0])
1106 sprintf(buf
, "%.64s %.64s", vendor
, model
);
1108 switch (type
) { /* numbers from hald/linux/device.c */
1109 case 1: sprintf(buf
, "tape"); break;
1110 case 2: sprintf(buf
, "printer"); break;
1111 case 3: sprintf(buf
, "processor"); break;
1113 case 5: sprintf(buf
, "cdrom"); break;
1114 case 6: sprintf(buf
, "scanner"); break;
1115 case 8: sprintf(buf
, "media_changer"); break;
1116 case 9: sprintf(buf
, "comm"); break;
1117 case 12: sprintf(buf
, "raid"); break;
1118 default: sprintf(buf
, "unknown");
1124 /* chop device path to 'host%d' and calculate the port number */
1125 c
= strchr(&path
[hba_len
], '/');
1128 fprintf(stderr
, Name
": %s - invalid path name\n", path
+ hba_len
);
1133 if (sscanf(&path
[hba_len
], "host%d", &port
) == 1)
1137 *c
= '/'; /* repair the full string */
1138 fprintf(stderr
, Name
": failed to determine port number for %s\n",
1145 /* mark this port as used */
1146 port_mask
&= ~(1 << port
);
1148 /* print out the device information */
1150 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
1154 fd
= dev_open(ent
->d_name
, O_RDONLY
);
1156 printf(" Port%d : - disk info unavailable -\n", port
);
1158 fd2devname(fd
, buf
);
1159 printf(" Port%d : %s", port
, buf
);
1160 if (imsm_read_serial(fd
, NULL
, (__u8
*) buf
) == 0)
1161 printf(" (%s)\n", buf
);
1176 for (i
= 0; i
< port_count
; i
++)
1177 if (port_mask
& (1 << i
))
1178 printf(" Port%d : - no device attached -\n", i
);
1184 static int detail_platform_imsm(int verbose
, int enumerate_only
)
1186 /* There are two components to imsm platform support, the ahci SATA
1187 * controller and the option-rom. To find the SATA controller we
1188 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
1189 * controller with the Intel vendor id is present. This approach
1190 * allows mdadm to leverage the kernel's ahci detection logic, with the
1191 * caveat that if ahci.ko is not loaded mdadm will not be able to
1192 * detect platform raid capabilities. The option-rom resides in a
1193 * platform "Adapter ROM". We scan for its signature to retrieve the
1194 * platform capabilities. If raid support is disabled in the BIOS the
1195 * option-rom capability structure will not be available.
1197 const struct imsm_orom
*orom
;
1198 struct sys_dev
*list
, *hba
;
1201 const char *hba_path
;
1205 if (enumerate_only
) {
1206 if (check_env("IMSM_NO_PLATFORM") || find_imsm_orom())
1211 list
= find_driver_devices("pci", "ahci");
1212 for (hba
= list
; hba
; hba
= hba
->next
)
1213 if (devpath_to_vendor(hba
->path
) == 0x8086)
1218 fprintf(stderr
, Name
": unable to find active ahci controller\n");
1219 free_sys_dev(&list
);
1222 fprintf(stderr
, Name
": found Intel SATA AHCI Controller\n");
1223 hba_path
= hba
->path
;
1225 free_sys_dev(&list
);
1227 orom
= find_imsm_orom();
1230 fprintf(stderr
, Name
": imsm option-rom not found\n");
1234 printf(" Platform : Intel(R) Matrix Storage Manager\n");
1235 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
1236 orom
->hotfix_ver
, orom
->build
);
1237 printf(" RAID Levels :%s%s%s%s%s\n",
1238 imsm_orom_has_raid0(orom
) ? " raid0" : "",
1239 imsm_orom_has_raid1(orom
) ? " raid1" : "",
1240 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
1241 imsm_orom_has_raid10(orom
) ? " raid10" : "",
1242 imsm_orom_has_raid5(orom
) ? " raid5" : "");
1243 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1244 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
1245 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
1246 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
1247 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
1248 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
1249 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
1250 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
1251 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
1252 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
1253 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
1254 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
1255 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
1256 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
1257 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
1258 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
1259 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
1260 printf(" Max Disks : %d\n", orom
->tds
);
1261 printf(" Max Volumes : %d\n", orom
->vpa
);
1262 printf(" I/O Controller : %s\n", hba_path
);
1264 /* find the smallest scsi host number to determine a port number base */
1265 dir
= opendir(hba_path
);
1266 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
1269 if (sscanf(ent
->d_name
, "host%d", &host
) != 1)
1271 if (port_count
== 0)
1273 else if (host
< host_base
)
1276 if (host
+ 1 > port_count
+ host_base
)
1277 port_count
= host
+ 1 - host_base
;
1283 if (!port_count
|| imsm_enumerate_ports(hba_path
, port_count
,
1284 host_base
, verbose
) != 0) {
1286 fprintf(stderr
, Name
": failed to enumerate ports\n");
1294 static int match_home_imsm(struct supertype
*st
, char *homehost
)
1296 /* the imsm metadata format does not specify any host
1297 * identification information. We return -1 since we can never
1298 * confirm nor deny whether a given array is "meant" for this
1299 * host. We rely on compare_super and the 'family_num' fields to
1300 * exclude member disks that do not belong, and we rely on
1301 * mdadm.conf to specify the arrays that should be assembled.
1302 * Auto-assembly may still pick up "foreign" arrays.
1308 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
1310 /* The uuid returned here is used for:
1311 * uuid to put into bitmap file (Create, Grow)
1312 * uuid for backup header when saving critical section (Grow)
1313 * comparing uuids when re-adding a device into an array
1314 * In these cases the uuid required is that of the data-array,
1315 * not the device-set.
1316 * uuid to recognise same set when adding a missing device back
1317 * to an array. This is a uuid for the device-set.
1319 * For each of these we can make do with a truncated
1320 * or hashed uuid rather than the original, as long as
1322 * In each case the uuid required is that of the data-array,
1323 * not the device-set.
1325 /* imsm does not track uuid's so we synthesis one using sha1 on
1326 * - The signature (Which is constant for all imsm array, but no matter)
1327 * - the orig_family_num of the container
1328 * - the index number of the volume
1329 * - the 'serial' number of the volume.
1330 * Hopefully these are all constant.
1332 struct intel_super
*super
= st
->sb
;
1335 struct sha1_ctx ctx
;
1336 struct imsm_dev
*dev
= NULL
;
1339 /* some mdadm versions failed to set ->orig_family_num, in which
1340 * case fall back to ->family_num. orig_family_num will be
1341 * fixed up with the first metadata update.
1343 family_num
= super
->anchor
->orig_family_num
;
1344 if (family_num
== 0)
1345 family_num
= super
->anchor
->family_num
;
1346 sha1_init_ctx(&ctx
);
1347 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
1348 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
1349 if (super
->current_vol
>= 0)
1350 dev
= get_imsm_dev(super
, super
->current_vol
);
1352 __u32 vol
= super
->current_vol
;
1353 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
1354 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
1356 sha1_finish_ctx(&ctx
, buf
);
1357 memcpy(uuid
, buf
, 4*4);
1362 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
1364 __u8
*v
= get_imsm_version(mpb
);
1365 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
1366 char major
[] = { 0, 0, 0 };
1367 char minor
[] = { 0 ,0, 0 };
1368 char patch
[] = { 0, 0, 0 };
1369 char *ver_parse
[] = { major
, minor
, patch
};
1373 while (*v
!= '\0' && v
< end
) {
1374 if (*v
!= '.' && j
< 2)
1375 ver_parse
[i
][j
++] = *v
;
1383 *m
= strtol(minor
, NULL
, 0);
1384 *p
= strtol(patch
, NULL
, 0);
1388 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
1390 /* migr_strip_size when repairing or initializing parity */
1391 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1392 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1394 switch (get_imsm_raid_level(map
)) {
1399 return 128*1024 >> 9;
1403 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
1405 /* migr_strip_size when rebuilding a degraded disk, no idea why
1406 * this is different than migr_strip_size_resync(), but it's good
1409 struct imsm_map
*map
= get_imsm_map(dev
, 1);
1410 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1412 switch (get_imsm_raid_level(map
)) {
1415 if (map
->num_members
% map
->num_domains
== 0)
1416 return 128*1024 >> 9;
1420 return max((__u32
) 64*1024 >> 9, chunk
);
1422 return 128*1024 >> 9;
1426 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
1428 struct imsm_map
*lo
= get_imsm_map(dev
, 0);
1429 struct imsm_map
*hi
= get_imsm_map(dev
, 1);
1430 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
1431 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
1433 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
1436 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
1438 struct imsm_map
*lo
= get_imsm_map(dev
, 0);
1439 int level
= get_imsm_raid_level(lo
);
1441 if (level
== 1 || level
== 10) {
1442 struct imsm_map
*hi
= get_imsm_map(dev
, 1);
1444 return hi
->num_domains
;
1446 return num_stripes_per_unit_resync(dev
);
1449 static __u8
imsm_num_data_members(struct imsm_dev
*dev
, int second_map
)
1451 /* named 'imsm_' because raid0, raid1 and raid10
1452 * counter-intuitively have the same number of data disks
1454 struct imsm_map
*map
= get_imsm_map(dev
, second_map
);
1456 switch (get_imsm_raid_level(map
)) {
1460 return map
->num_members
;
1462 return map
->num_members
- 1;
1464 dprintf("%s: unsupported raid level\n", __func__
);
1469 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
1471 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1472 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1474 switch(get_imsm_raid_level(map
)) {
1477 return chunk
* map
->num_domains
;
1479 return chunk
* map
->num_members
;
1485 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
1487 struct imsm_map
*map
= get_imsm_map(dev
, 1);
1488 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1489 __u32 strip
= block
/ chunk
;
1491 switch (get_imsm_raid_level(map
)) {
1494 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
1495 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
1497 return vol_stripe
* chunk
+ block
% chunk
;
1499 __u32 stripe
= strip
/ (map
->num_members
- 1);
1501 return stripe
* chunk
+ block
% chunk
;
1508 static __u64
blocks_per_migr_unit(struct imsm_dev
*dev
)
1510 /* calculate the conversion factor between per member 'blocks'
1511 * (md/{resync,rebuild}_start) and imsm migration units, return
1512 * 0 for the 'not migrating' and 'unsupported migration' cases
1514 if (!dev
->vol
.migr_state
)
1517 switch (migr_type(dev
)) {
1522 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1523 __u32 stripes_per_unit
;
1524 __u32 blocks_per_unit
;
1533 /* yes, this is really the translation of migr_units to
1534 * per-member blocks in the 'resync' case
1536 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
1537 migr_chunk
= migr_strip_blocks_resync(dev
);
1538 disks
= imsm_num_data_members(dev
, 0);
1539 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
1540 stripe
= __le32_to_cpu(map
->blocks_per_strip
) * disks
;
1541 segment
= blocks_per_unit
/ stripe
;
1542 block_rel
= blocks_per_unit
- segment
* stripe
;
1543 parity_depth
= parity_segment_depth(dev
);
1544 block_map
= map_migr_block(dev
, block_rel
);
1545 return block_map
+ parity_depth
* segment
;
1547 case MIGR_REBUILD
: {
1548 __u32 stripes_per_unit
;
1551 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
1552 migr_chunk
= migr_strip_blocks_rebuild(dev
);
1553 return migr_chunk
* stripes_per_unit
;
1555 case MIGR_STATE_CHANGE
:
1561 static int imsm_level_to_layout(int level
)
1569 return ALGORITHM_LEFT_ASYMMETRIC
;
1576 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
1578 struct intel_super
*super
= st
->sb
;
1579 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
1580 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1581 struct imsm_map
*prev_map
= get_imsm_map(dev
, 1);
1582 struct imsm_map
*map_to_analyse
= map
;
1585 int map_disks
= info
->array
.raid_disks
;
1588 map_to_analyse
= prev_map
;
1590 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
1591 if (dl
->raiddisk
== info
->disk
.raid_disk
)
1593 info
->container_member
= super
->current_vol
;
1594 info
->array
.raid_disks
= map_to_analyse
->num_members
;
1595 info
->array
.level
= get_imsm_raid_level(map_to_analyse
);
1596 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
1597 info
->array
.md_minor
= -1;
1598 info
->array
.ctime
= 0;
1599 info
->array
.utime
= 0;
1600 info
->array
.chunk_size
=
1601 __le16_to_cpu(map_to_analyse
->blocks_per_strip
) << 9;
1602 info
->array
.state
= !dev
->vol
.dirty
;
1603 info
->custom_array_size
= __le32_to_cpu(dev
->size_high
);
1604 info
->custom_array_size
<<= 32;
1605 info
->custom_array_size
|= __le32_to_cpu(dev
->size_low
);
1607 info
->new_level
= get_imsm_raid_level(map
);
1608 info
->new_layout
= imsm_level_to_layout(info
->new_level
);
1609 info
->new_chunk
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
1611 info
->new_level
= UnSet
;
1612 info
->new_layout
= UnSet
;
1613 info
->new_chunk
= info
->array
.chunk_size
;
1615 info
->disk
.major
= 0;
1616 info
->disk
.minor
= 0;
1618 info
->disk
.major
= dl
->major
;
1619 info
->disk
.minor
= dl
->minor
;
1622 info
->data_offset
= __le32_to_cpu(map_to_analyse
->pba_of_lba0
);
1623 info
->component_size
=
1624 __le32_to_cpu(map_to_analyse
->blocks_per_member
);
1625 memset(info
->uuid
, 0, sizeof(info
->uuid
));
1626 info
->recovery_start
= MaxSector
;
1627 info
->reshape_active
= (prev_map
!= NULL
);
1628 if (info
->reshape_active
)
1629 info
->delta_disks
= map
->num_members
- prev_map
->num_members
;
1631 info
->delta_disks
= 0;
1633 if (map_to_analyse
->map_state
== IMSM_T_STATE_UNINITIALIZED
||
1635 info
->resync_start
= 0;
1636 } else if (dev
->vol
.migr_state
) {
1637 switch (migr_type(dev
)) {
1640 __u64 blocks_per_unit
= blocks_per_migr_unit(dev
);
1641 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
1643 info
->resync_start
= blocks_per_unit
* units
;
1647 /* we could emulate the checkpointing of
1648 * 'sync_action=check' migrations, but for now
1649 * we just immediately complete them
1652 /* this is handled by container_content_imsm() */
1654 case MIGR_STATE_CHANGE
:
1655 /* FIXME handle other migrations */
1657 /* we are not dirty, so... */
1658 info
->resync_start
= MaxSector
;
1661 info
->resync_start
= MaxSector
;
1663 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
1664 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
1666 info
->array
.major_version
= -1;
1667 info
->array
.minor_version
= -2;
1668 devname
= devnum2devname(st
->container_dev
);
1669 *info
->text_version
= '\0';
1671 sprintf(info
->text_version
, "/%s/%d", devname
, info
->container_member
);
1673 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
1674 uuid_from_super_imsm(st
, info
->uuid
);
1678 for (i
=0; i
<map_disks
; i
++) {
1680 if (i
< info
->array
.raid_disks
) {
1681 struct imsm_disk
*dsk
;
1682 j
= get_imsm_disk_idx(dev
, i
, -1);
1683 dsk
= get_imsm_disk(super
, j
);
1684 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
1691 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
, int failed
);
1692 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
);
1694 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
1698 for (d
= super
->missing
; d
; d
= d
->next
)
1699 if (d
->index
== index
)
1704 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
1706 struct intel_super
*super
= st
->sb
;
1707 struct imsm_disk
*disk
;
1708 int map_disks
= info
->array
.raid_disks
;
1709 int max_enough
= -1;
1711 struct imsm_super
*mpb
;
1713 if (super
->current_vol
>= 0) {
1714 getinfo_super_imsm_volume(st
, info
, map
);
1718 /* Set raid_disks to zero so that Assemble will always pull in valid
1721 info
->array
.raid_disks
= 0;
1722 info
->array
.level
= LEVEL_CONTAINER
;
1723 info
->array
.layout
= 0;
1724 info
->array
.md_minor
= -1;
1725 info
->array
.ctime
= 0; /* N/A for imsm */
1726 info
->array
.utime
= 0;
1727 info
->array
.chunk_size
= 0;
1729 info
->disk
.major
= 0;
1730 info
->disk
.minor
= 0;
1731 info
->disk
.raid_disk
= -1;
1732 info
->reshape_active
= 0;
1733 info
->array
.major_version
= -1;
1734 info
->array
.minor_version
= -2;
1735 strcpy(info
->text_version
, "imsm");
1736 info
->safe_mode_delay
= 0;
1737 info
->disk
.number
= -1;
1738 info
->disk
.state
= 0;
1740 info
->recovery_start
= MaxSector
;
1742 /* do we have the all the insync disks that we expect? */
1743 mpb
= super
->anchor
;
1745 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1746 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1747 int failed
, enough
, j
, missing
= 0;
1748 struct imsm_map
*map
;
1751 failed
= imsm_count_failed(super
, dev
);
1752 state
= imsm_check_degraded(super
, dev
, failed
);
1753 map
= get_imsm_map(dev
, dev
->vol
.migr_state
);
1755 /* any newly missing disks?
1756 * (catches single-degraded vs double-degraded)
1758 for (j
= 0; j
< map
->num_members
; j
++) {
1759 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, -1);
1760 __u32 idx
= ord_to_idx(ord
);
1762 if (!(ord
& IMSM_ORD_REBUILD
) &&
1763 get_imsm_missing(super
, idx
)) {
1769 if (state
== IMSM_T_STATE_FAILED
)
1771 else if (state
== IMSM_T_STATE_DEGRADED
&&
1772 (state
!= map
->map_state
|| missing
))
1774 else /* we're normal, or already degraded */
1777 /* in the missing/failed disk case check to see
1778 * if at least one array is runnable
1780 max_enough
= max(max_enough
, enough
);
1782 dprintf("%s: enough: %d\n", __func__
, max_enough
);
1783 info
->container_enough
= max_enough
;
1786 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
1788 disk
= &super
->disks
->disk
;
1789 info
->data_offset
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
1790 info
->component_size
= reserved
;
1791 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
1792 /* we don't change info->disk.raid_disk here because
1793 * this state will be finalized in mdmon after we have
1794 * found the 'most fresh' version of the metadata
1796 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
1797 info
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
1800 /* only call uuid_from_super_imsm when this disk is part of a populated container,
1801 * ->compare_super may have updated the 'num_raid_devs' field for spares
1803 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
1804 uuid_from_super_imsm(st
, info
->uuid
);
1806 memcpy(info
->uuid
, uuid_zero
, sizeof(uuid_zero
));
1808 /* I don't know how to compute 'map' on imsm, so use safe default */
1811 for (i
= 0; i
< map_disks
; i
++)
1817 /* allocates memory and fills disk in mdinfo structure
1818 * for each disk in array */
1819 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
1821 struct mdinfo
*mddev
= NULL
;
1822 struct intel_super
*super
= st
->sb
;
1823 struct imsm_disk
*disk
;
1826 if (!super
|| !super
->disks
)
1829 mddev
= malloc(sizeof(*mddev
));
1831 fprintf(stderr
, Name
": Failed to allocate memory.\n");
1834 memset(mddev
, 0, sizeof(*mddev
));
1838 tmp
= malloc(sizeof(*tmp
));
1840 fprintf(stderr
, Name
": Failed to allocate memory.\n");
1845 memset(tmp
, 0, sizeof(*tmp
));
1847 tmp
->next
= mddev
->devs
;
1849 tmp
->disk
.number
= count
++;
1850 tmp
->disk
.major
= dl
->major
;
1851 tmp
->disk
.minor
= dl
->minor
;
1852 tmp
->disk
.state
= is_configured(disk
) ?
1853 (1 << MD_DISK_ACTIVE
) : 0;
1854 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
1855 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
1856 tmp
->disk
.raid_disk
= -1;
1862 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
1863 char *update
, char *devname
, int verbose
,
1864 int uuid_set
, char *homehost
)
1866 /* For 'assemble' and 'force' we need to return non-zero if any
1867 * change was made. For others, the return value is ignored.
1868 * Update options are:
1869 * force-one : This device looks a bit old but needs to be included,
1870 * update age info appropriately.
1871 * assemble: clear any 'faulty' flag to allow this device to
1873 * force-array: Array is degraded but being forced, mark it clean
1874 * if that will be needed to assemble it.
1876 * newdev: not used ????
1877 * grow: Array has gained a new device - this is currently for
1879 * resync: mark as dirty so a resync will happen.
1880 * name: update the name - preserving the homehost
1881 * uuid: Change the uuid of the array to match watch is given
1883 * Following are not relevant for this imsm:
1884 * sparc2.2 : update from old dodgey metadata
1885 * super-minor: change the preferred_minor number
1886 * summaries: update redundant counters.
1887 * homehost: update the recorded homehost
1888 * _reshape_progress: record new reshape_progress position.
1891 struct intel_super
*super
= st
->sb
;
1892 struct imsm_super
*mpb
;
1894 /* we can only update container info */
1895 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
1898 mpb
= super
->anchor
;
1900 if (strcmp(update
, "uuid") == 0 && uuid_set
&& !info
->update_private
)
1902 else if (strcmp(update
, "uuid") == 0 && uuid_set
&& info
->update_private
) {
1903 mpb
->orig_family_num
= *((__u32
*) info
->update_private
);
1905 } else if (strcmp(update
, "uuid") == 0) {
1906 __u32
*new_family
= malloc(sizeof(*new_family
));
1908 /* update orig_family_number with the incoming random
1909 * data, report the new effective uuid, and store the
1910 * new orig_family_num for future updates.
1913 memcpy(&mpb
->orig_family_num
, info
->uuid
, sizeof(__u32
));
1914 uuid_from_super_imsm(st
, info
->uuid
);
1915 *new_family
= mpb
->orig_family_num
;
1916 info
->update_private
= new_family
;
1919 } else if (strcmp(update
, "assemble") == 0)
1924 /* successful update? recompute checksum */
1926 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
1931 static size_t disks_to_mpb_size(int disks
)
1935 size
= sizeof(struct imsm_super
);
1936 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
1937 size
+= 2 * sizeof(struct imsm_dev
);
1938 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
1939 size
+= (4 - 2) * sizeof(struct imsm_map
);
1940 /* 4 possible disk_ord_tbl's */
1941 size
+= 4 * (disks
- 1) * sizeof(__u32
);
1946 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
)
1948 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
1951 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
1954 static void free_devlist(struct intel_super
*super
)
1956 struct intel_dev
*dv
;
1958 while (super
->devlist
) {
1959 dv
= super
->devlist
->next
;
1960 free(super
->devlist
->dev
);
1961 free(super
->devlist
);
1962 super
->devlist
= dv
;
1966 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
1968 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
1971 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
1975 * 0 same, or first was empty, and second was copied
1976 * 1 second had wrong number
1978 * 3 wrong other info
1980 struct intel_super
*first
= st
->sb
;
1981 struct intel_super
*sec
= tst
->sb
;
1989 /* if an anchor does not have num_raid_devs set then it is a free
1992 if (first
->anchor
->num_raid_devs
> 0 &&
1993 sec
->anchor
->num_raid_devs
> 0) {
1994 /* Determine if these disks might ever have been
1995 * related. Further disambiguation can only take place
1996 * in load_super_imsm_all
1998 __u32 first_family
= first
->anchor
->orig_family_num
;
1999 __u32 sec_family
= sec
->anchor
->orig_family_num
;
2001 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
2002 MAX_SIGNATURE_LENGTH
) != 0)
2005 if (first_family
== 0)
2006 first_family
= first
->anchor
->family_num
;
2007 if (sec_family
== 0)
2008 sec_family
= sec
->anchor
->family_num
;
2010 if (first_family
!= sec_family
)
2016 /* if 'first' is a spare promote it to a populated mpb with sec's
2019 if (first
->anchor
->num_raid_devs
== 0 &&
2020 sec
->anchor
->num_raid_devs
> 0) {
2022 struct intel_dev
*dv
;
2023 struct imsm_dev
*dev
;
2025 /* we need to copy raid device info from sec if an allocation
2026 * fails here we don't associate the spare
2028 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
2029 dv
= malloc(sizeof(*dv
));
2032 dev
= malloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
2039 dv
->next
= first
->devlist
;
2040 first
->devlist
= dv
;
2042 if (i
< sec
->anchor
->num_raid_devs
) {
2043 /* allocation failure */
2044 free_devlist(first
);
2045 fprintf(stderr
, "imsm: failed to associate spare\n");
2048 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
2049 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
2050 first
->anchor
->family_num
= sec
->anchor
->family_num
;
2051 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
2052 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
2053 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
2059 static void fd2devname(int fd
, char *name
)
2063 char dname
[PATH_MAX
];
2068 if (fstat(fd
, &st
) != 0)
2070 sprintf(path
, "/sys/dev/block/%d:%d",
2071 major(st
.st_rdev
), minor(st
.st_rdev
));
2073 rv
= readlink(path
, dname
, sizeof(dname
));
2078 nm
= strrchr(dname
, '/');
2080 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
2083 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
2085 static int imsm_read_serial(int fd
, char *devname
,
2086 __u8 serial
[MAX_RAID_SERIAL_LEN
])
2088 unsigned char scsi_serial
[255];
2097 memset(scsi_serial
, 0, sizeof(scsi_serial
));
2099 rv
= scsi_get_serial(fd
, scsi_serial
, sizeof(scsi_serial
));
2101 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
2102 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
2103 fd2devname(fd
, (char *) serial
);
2110 Name
": Failed to retrieve serial for %s\n",
2115 rsp_len
= scsi_serial
[3];
2119 Name
": Failed to retrieve serial for %s\n",
2123 rsp_buf
= (char *) &scsi_serial
[4];
2125 /* trim all whitespace and non-printable characters and convert
2128 for (i
= 0, dest
= rsp_buf
; i
< rsp_len
; i
++) {
2131 /* ':' is reserved for use in placeholder serial
2132 * numbers for missing disks
2140 len
= dest
- rsp_buf
;
2143 /* truncate leading characters */
2144 if (len
> MAX_RAID_SERIAL_LEN
) {
2145 dest
+= len
- MAX_RAID_SERIAL_LEN
;
2146 len
= MAX_RAID_SERIAL_LEN
;
2149 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
2150 memcpy(serial
, dest
, len
);
2155 static int serialcmp(__u8
*s1
, __u8
*s2
)
2157 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
2160 static void serialcpy(__u8
*dest
, __u8
*src
)
2162 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
2166 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
2170 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
2171 if (serialcmp(dl
->serial
, serial
) == 0)
2178 static struct imsm_disk
*
2179 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
2183 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2184 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
2186 if (serialcmp(disk
->serial
, serial
) == 0) {
2197 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
2199 struct imsm_disk
*disk
;
2204 __u8 serial
[MAX_RAID_SERIAL_LEN
];
2206 rv
= imsm_read_serial(fd
, devname
, serial
);
2211 dl
= calloc(1, sizeof(*dl
));
2215 Name
": failed to allocate disk buffer for %s\n",
2221 dl
->major
= major(stb
.st_rdev
);
2222 dl
->minor
= minor(stb
.st_rdev
);
2223 dl
->next
= super
->disks
;
2224 dl
->fd
= keep_fd
? fd
: -1;
2225 assert(super
->disks
== NULL
);
2227 serialcpy(dl
->serial
, serial
);
2230 fd2devname(fd
, name
);
2232 dl
->devname
= strdup(devname
);
2234 dl
->devname
= strdup(name
);
2236 /* look up this disk's index in the current anchor */
2237 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
2240 /* only set index on disks that are a member of a
2241 * populated contianer, i.e. one with raid_devs
2243 if (is_failed(&dl
->disk
))
2245 else if (is_spare(&dl
->disk
))
2253 /* When migrating map0 contains the 'destination' state while map1
2254 * contains the current state. When not migrating map0 contains the
2255 * current state. This routine assumes that map[0].map_state is set to
2256 * the current array state before being called.
2258 * Migration is indicated by one of the following states
2259 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
2260 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
2261 * map1state=unitialized)
2262 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
2264 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
2265 * map1state=degraded)
2267 static void migrate(struct imsm_dev
*dev
, __u8 to_state
, int migr_type
)
2269 struct imsm_map
*dest
;
2270 struct imsm_map
*src
= get_imsm_map(dev
, 0);
2272 dev
->vol
.migr_state
= 1;
2273 set_migr_type(dev
, migr_type
);
2274 dev
->vol
.curr_migr_unit
= 0;
2275 dest
= get_imsm_map(dev
, 1);
2277 /* duplicate and then set the target end state in map[0] */
2278 memcpy(dest
, src
, sizeof_imsm_map(src
));
2279 if ((migr_type
== MIGR_REBUILD
) ||
2280 (migr_type
== MIGR_GEN_MIGR
)) {
2284 for (i
= 0; i
< src
->num_members
; i
++) {
2285 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
2286 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
2290 src
->map_state
= to_state
;
2293 static void end_migration(struct imsm_dev
*dev
, __u8 map_state
)
2295 struct imsm_map
*map
= get_imsm_map(dev
, 0);
2296 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
);
2299 /* merge any IMSM_ORD_REBUILD bits that were not successfully
2300 * completed in the last migration.
2302 * FIXME add support for raid-level-migration
2304 for (i
= 0; i
< prev
->num_members
; i
++)
2305 for (j
= 0; j
< map
->num_members
; j
++)
2306 /* during online capacity expansion
2307 * disks position can be changed if takeover is used
2309 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
2310 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
2311 map
->disk_ord_tbl
[j
] |= prev
->disk_ord_tbl
[i
];
2315 dev
->vol
.migr_state
= 0;
2316 dev
->vol
.migr_type
= 0;
2317 dev
->vol
.curr_migr_unit
= 0;
2318 map
->map_state
= map_state
;
2322 static int parse_raid_devices(struct intel_super
*super
)
2325 struct imsm_dev
*dev_new
;
2326 size_t len
, len_migr
;
2327 size_t space_needed
= 0;
2328 struct imsm_super
*mpb
= super
->anchor
;
2330 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
2331 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
2332 struct intel_dev
*dv
;
2334 len
= sizeof_imsm_dev(dev_iter
, 0);
2335 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
2337 space_needed
+= len_migr
- len
;
2339 dv
= malloc(sizeof(*dv
));
2342 dev_new
= malloc(len_migr
);
2347 imsm_copy_dev(dev_new
, dev_iter
);
2350 dv
->next
= super
->devlist
;
2351 super
->devlist
= dv
;
2354 /* ensure that super->buf is large enough when all raid devices
2357 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
2360 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
, 512);
2361 if (posix_memalign(&buf
, 512, len
) != 0)
2364 memcpy(buf
, super
->buf
, super
->len
);
2365 memset(buf
+ super
->len
, 0, len
- super
->len
);
2374 /* retrieve a pointer to the bbm log which starts after all raid devices */
2375 struct bbm_log
*__get_imsm_bbm_log(struct imsm_super
*mpb
)
2379 if (__le32_to_cpu(mpb
->bbm_log_size
)) {
2381 ptr
+= mpb
->mpb_size
- __le32_to_cpu(mpb
->bbm_log_size
);
2387 static void __free_imsm(struct intel_super
*super
, int free_disks
);
2389 /* load_imsm_mpb - read matrix metadata
2390 * allocates super->mpb to be freed by free_super
2392 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
2394 unsigned long long dsize
;
2395 unsigned long long sectors
;
2397 struct imsm_super
*anchor
;
2400 get_dev_size(fd
, NULL
, &dsize
);
2404 Name
": %s: device to small for imsm\n",
2409 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0) {
2412 Name
": Cannot seek to anchor block on %s: %s\n",
2413 devname
, strerror(errno
));
2417 if (posix_memalign((void**)&anchor
, 512, 512) != 0) {
2420 Name
": Failed to allocate imsm anchor buffer"
2421 " on %s\n", devname
);
2424 if (read(fd
, anchor
, 512) != 512) {
2427 Name
": Cannot read anchor block on %s: %s\n",
2428 devname
, strerror(errno
));
2433 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
2436 Name
": no IMSM anchor on %s\n", devname
);
2441 __free_imsm(super
, 0);
2442 super
->len
= ROUND_UP(anchor
->mpb_size
, 512);
2443 if (posix_memalign(&super
->buf
, 512, super
->len
) != 0) {
2446 Name
": unable to allocate %zu byte mpb buffer\n",
2451 memcpy(super
->buf
, anchor
, 512);
2453 sectors
= mpb_sectors(anchor
) - 1;
2456 check_sum
= __gen_imsm_checksum(super
->anchor
);
2457 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
2460 Name
": IMSM checksum %x != %x on %s\n",
2462 __le32_to_cpu(super
->anchor
->check_sum
),
2470 /* read the extended mpb */
2471 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0) {
2474 Name
": Cannot seek to extended mpb on %s: %s\n",
2475 devname
, strerror(errno
));
2479 if ((unsigned)read(fd
, super
->buf
+ 512, super
->len
- 512) != super
->len
- 512) {
2482 Name
": Cannot read extended mpb on %s: %s\n",
2483 devname
, strerror(errno
));
2487 check_sum
= __gen_imsm_checksum(super
->anchor
);
2488 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
2491 Name
": IMSM checksum %x != %x on %s\n",
2492 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
2497 /* FIXME the BBM log is disk specific so we cannot use this global
2498 * buffer for all disks. Ok for now since we only look at the global
2499 * bbm_log_size parameter to gate assembly
2501 super
->bbm_log
= __get_imsm_bbm_log(super
->anchor
);
2507 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
2511 err
= load_imsm_mpb(fd
, super
, devname
);
2514 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
2517 err
= parse_raid_devices(super
);
2522 static void __free_imsm_disk(struct dl
*d
)
2534 static void free_imsm_disks(struct intel_super
*super
)
2538 while (super
->disks
) {
2540 super
->disks
= d
->next
;
2541 __free_imsm_disk(d
);
2543 while (super
->missing
) {
2545 super
->missing
= d
->next
;
2546 __free_imsm_disk(d
);
2551 /* free all the pieces hanging off of a super pointer */
2552 static void __free_imsm(struct intel_super
*super
, int free_disks
)
2559 free_imsm_disks(super
);
2560 free_devlist(super
);
2562 free((void *) super
->hba
);
2567 static void free_imsm(struct intel_super
*super
)
2569 __free_imsm(super
, 1);
2573 static void free_super_imsm(struct supertype
*st
)
2575 struct intel_super
*super
= st
->sb
;
2584 static struct intel_super
*alloc_super(void)
2586 struct intel_super
*super
= malloc(sizeof(*super
));
2589 memset(super
, 0, sizeof(*super
));
2590 super
->current_vol
= -1;
2591 super
->create_offset
= ~((__u32
) 0);
2592 if (!check_env("IMSM_NO_PLATFORM"))
2593 super
->orom
= find_imsm_orom();
2594 if (super
->orom
&& !check_env("IMSM_TEST_OROM")) {
2595 struct sys_dev
*list
, *ent
;
2597 /* find the first intel ahci controller */
2598 list
= find_driver_devices("pci", "ahci");
2599 for (ent
= list
; ent
; ent
= ent
->next
)
2600 if (devpath_to_vendor(ent
->path
) == 0x8086)
2603 super
->hba
= ent
->path
;
2606 free_sys_dev(&list
);
2614 /* find_missing - helper routine for load_super_imsm_all that identifies
2615 * disks that have disappeared from the system. This routine relies on
2616 * the mpb being uptodate, which it is at load time.
2618 static int find_missing(struct intel_super
*super
)
2621 struct imsm_super
*mpb
= super
->anchor
;
2623 struct imsm_disk
*disk
;
2625 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2626 disk
= __get_imsm_disk(mpb
, i
);
2627 dl
= serial_to_dl(disk
->serial
, super
);
2631 dl
= malloc(sizeof(*dl
));
2637 dl
->devname
= strdup("missing");
2639 serialcpy(dl
->serial
, disk
->serial
);
2642 dl
->next
= super
->missing
;
2643 super
->missing
= dl
;
2649 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
2651 struct intel_disk
*idisk
= disk_list
;
2654 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
2656 idisk
= idisk
->next
;
2662 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
2663 struct intel_super
*super
,
2664 struct intel_disk
**disk_list
)
2666 struct imsm_disk
*d
= &super
->disks
->disk
;
2667 struct imsm_super
*mpb
= super
->anchor
;
2670 for (i
= 0; i
< tbl_size
; i
++) {
2671 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
2672 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
2674 if (tbl_mpb
->family_num
== mpb
->family_num
) {
2675 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
2676 dprintf("%s: mpb from %d:%d matches %d:%d\n",
2677 __func__
, super
->disks
->major
,
2678 super
->disks
->minor
,
2679 table
[i
]->disks
->major
,
2680 table
[i
]->disks
->minor
);
2684 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
2685 is_configured(d
) == is_configured(tbl_d
)) &&
2686 tbl_mpb
->generation_num
< mpb
->generation_num
) {
2687 /* current version of the mpb is a
2688 * better candidate than the one in
2689 * super_table, but copy over "cross
2690 * generational" status
2692 struct intel_disk
*idisk
;
2694 dprintf("%s: mpb from %d:%d replaces %d:%d\n",
2695 __func__
, super
->disks
->major
,
2696 super
->disks
->minor
,
2697 table
[i
]->disks
->major
,
2698 table
[i
]->disks
->minor
);
2700 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
2701 if (idisk
&& is_failed(&idisk
->disk
))
2702 tbl_d
->status
|= FAILED_DISK
;
2705 struct intel_disk
*idisk
;
2706 struct imsm_disk
*disk
;
2708 /* tbl_mpb is more up to date, but copy
2709 * over cross generational status before
2712 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
2713 if (disk
&& is_failed(disk
))
2714 d
->status
|= FAILED_DISK
;
2716 idisk
= disk_list_get(d
->serial
, *disk_list
);
2719 if (disk
&& is_configured(disk
))
2720 idisk
->disk
.status
|= CONFIGURED_DISK
;
2723 dprintf("%s: mpb from %d:%d prefer %d:%d\n",
2724 __func__
, super
->disks
->major
,
2725 super
->disks
->minor
,
2726 table
[i
]->disks
->major
,
2727 table
[i
]->disks
->minor
);
2735 table
[tbl_size
++] = super
;
2739 /* update/extend the merged list of imsm_disk records */
2740 for (j
= 0; j
< mpb
->num_disks
; j
++) {
2741 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
2742 struct intel_disk
*idisk
;
2744 idisk
= disk_list_get(disk
->serial
, *disk_list
);
2746 idisk
->disk
.status
|= disk
->status
;
2747 if (is_configured(&idisk
->disk
) ||
2748 is_failed(&idisk
->disk
))
2749 idisk
->disk
.status
&= ~(SPARE_DISK
);
2751 idisk
= calloc(1, sizeof(*idisk
));
2754 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
2755 idisk
->disk
= *disk
;
2756 idisk
->next
= *disk_list
;
2760 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
2767 static struct intel_super
*
2768 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
2771 struct imsm_super
*mpb
= super
->anchor
;
2775 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2776 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
2777 struct intel_disk
*idisk
;
2779 idisk
= disk_list_get(disk
->serial
, disk_list
);
2781 if (idisk
->owner
== owner
||
2782 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
2785 dprintf("%s: '%.16s' owner %d != %d\n",
2786 __func__
, disk
->serial
, idisk
->owner
,
2789 dprintf("%s: unknown disk %x [%d]: %.16s\n",
2790 __func__
, __le32_to_cpu(mpb
->family_num
), i
,
2796 if (ok_count
== mpb
->num_disks
)
2801 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
2803 struct intel_super
*s
;
2805 for (s
= super_list
; s
; s
= s
->next
) {
2806 if (family_num
!= s
->anchor
->family_num
)
2808 fprintf(stderr
, "Conflict, offlining family %#x on '%s'\n",
2809 __le32_to_cpu(family_num
), s
->disks
->devname
);
2813 static struct intel_super
*
2814 imsm_thunderdome(struct intel_super
**super_list
, int len
)
2816 struct intel_super
*super_table
[len
];
2817 struct intel_disk
*disk_list
= NULL
;
2818 struct intel_super
*champion
, *spare
;
2819 struct intel_super
*s
, **del
;
2824 memset(super_table
, 0, sizeof(super_table
));
2825 for (s
= *super_list
; s
; s
= s
->next
)
2826 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
2828 for (i
= 0; i
< tbl_size
; i
++) {
2829 struct imsm_disk
*d
;
2830 struct intel_disk
*idisk
;
2831 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
2834 d
= &s
->disks
->disk
;
2836 /* 'd' must appear in merged disk list for its
2837 * configuration to be valid
2839 idisk
= disk_list_get(d
->serial
, disk_list
);
2840 if (idisk
&& idisk
->owner
== i
)
2841 s
= validate_members(s
, disk_list
, i
);
2846 dprintf("%s: marking family: %#x from %d:%d offline\n",
2847 __func__
, mpb
->family_num
,
2848 super_table
[i
]->disks
->major
,
2849 super_table
[i
]->disks
->minor
);
2853 /* This is where the mdadm implementation differs from the Windows
2854 * driver which has no strict concept of a container. We can only
2855 * assemble one family from a container, so when returning a prodigal
2856 * array member to this system the code will not be able to disambiguate
2857 * the container contents that should be assembled ("foreign" versus
2858 * "local"). It requires user intervention to set the orig_family_num
2859 * to a new value to establish a new container. The Windows driver in
2860 * this situation fixes up the volume name in place and manages the
2861 * foreign array as an independent entity.
2866 for (i
= 0; i
< tbl_size
; i
++) {
2867 struct intel_super
*tbl_ent
= super_table
[i
];
2873 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
2878 if (s
&& !is_spare
) {
2879 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
2881 } else if (!s
&& !is_spare
)
2894 fprintf(stderr
, "Chose family %#x on '%s', "
2895 "assemble conflicts to new container with '--update=uuid'\n",
2896 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
2898 /* collect all dl's onto 'champion', and update them to
2899 * champion's version of the status
2901 for (s
= *super_list
; s
; s
= s
->next
) {
2902 struct imsm_super
*mpb
= champion
->anchor
;
2903 struct dl
*dl
= s
->disks
;
2908 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2909 struct imsm_disk
*disk
;
2911 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
2914 /* only set index on disks that are a member of
2915 * a populated contianer, i.e. one with
2918 if (is_failed(&dl
->disk
))
2920 else if (is_spare(&dl
->disk
))
2926 if (i
>= mpb
->num_disks
) {
2927 struct intel_disk
*idisk
;
2929 idisk
= disk_list_get(dl
->serial
, disk_list
);
2930 if (idisk
&& is_spare(&idisk
->disk
) &&
2931 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
2939 dl
->next
= champion
->disks
;
2940 champion
->disks
= dl
;
2944 /* delete 'champion' from super_list */
2945 for (del
= super_list
; *del
; ) {
2946 if (*del
== champion
) {
2947 *del
= (*del
)->next
;
2950 del
= &(*del
)->next
;
2952 champion
->next
= NULL
;
2956 struct intel_disk
*idisk
= disk_list
;
2958 disk_list
= disk_list
->next
;
2965 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
2969 struct intel_super
*super_list
= NULL
;
2970 struct intel_super
*super
= NULL
;
2971 int devnum
= fd2devnum(fd
);
2977 /* check if 'fd' an opened container */
2978 sra
= sysfs_read(fd
, 0, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
2982 if (sra
->array
.major_version
!= -1 ||
2983 sra
->array
.minor_version
!= -2 ||
2984 strcmp(sra
->text_version
, "imsm") != 0) {
2989 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
2990 struct intel_super
*s
= alloc_super();
2997 s
->next
= super_list
;
3001 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
3002 dfd
= dev_open(nm
, O_RDWR
);
3006 err
= load_and_parse_mpb(dfd
, s
, NULL
, 1);
3008 /* retry the load if we might have raced against mdmon */
3009 if (err
== 3 && mdmon_running(devnum
))
3010 for (retry
= 0; retry
< 3; retry
++) {
3012 err
= load_and_parse_mpb(dfd
, s
, NULL
, 1);
3020 /* all mpbs enter, maybe one leaves */
3021 super
= imsm_thunderdome(&super_list
, i
);
3027 if (find_missing(super
) != 0) {
3035 while (super_list
) {
3036 struct intel_super
*s
= super_list
;
3038 super_list
= super_list
->next
;
3047 st
->container_dev
= devnum
;
3048 if (err
== 0 && st
->ss
== NULL
) {
3049 st
->ss
= &super_imsm
;
3050 st
->minor_version
= 0;
3051 st
->max_devs
= IMSM_MAX_DEVICES
;
3056 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
3058 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
);
3062 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
3064 struct intel_super
*super
;
3067 if (test_partition(fd
))
3068 /* IMSM not allowed on partitions */
3071 free_super_imsm(st
);
3073 super
= alloc_super();
3076 Name
": malloc of %zu failed.\n",
3081 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
3086 Name
": Failed to load all information "
3087 "sections on %s\n", devname
);
3093 if (st
->ss
== NULL
) {
3094 st
->ss
= &super_imsm
;
3095 st
->minor_version
= 0;
3096 st
->max_devs
= IMSM_MAX_DEVICES
;
3101 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
3103 if (info
->level
== 1)
3105 return info
->chunk_size
>> 9;
3108 static __u32
info_to_num_data_stripes(mdu_array_info_t
*info
, int num_domains
)
3112 num_stripes
= (info
->size
* 2) / info_to_blocks_per_strip(info
);
3113 num_stripes
/= num_domains
;
3118 static __u32
info_to_blocks_per_member(mdu_array_info_t
*info
)
3120 if (info
->level
== 1)
3121 return info
->size
* 2;
3123 return (info
->size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
3126 static void imsm_update_version_info(struct intel_super
*super
)
3128 /* update the version and attributes */
3129 struct imsm_super
*mpb
= super
->anchor
;
3131 struct imsm_dev
*dev
;
3132 struct imsm_map
*map
;
3135 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3136 dev
= get_imsm_dev(super
, i
);
3137 map
= get_imsm_map(dev
, 0);
3138 if (__le32_to_cpu(dev
->size_high
) > 0)
3139 mpb
->attributes
|= MPB_ATTRIB_2TB
;
3141 /* FIXME detect when an array spans a port multiplier */
3143 mpb
->attributes
|= MPB_ATTRIB_PM
;
3146 if (mpb
->num_raid_devs
> 1 ||
3147 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
3148 version
= MPB_VERSION_ATTRIBS
;
3149 switch (get_imsm_raid_level(map
)) {
3150 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
3151 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
3152 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
3153 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
3156 if (map
->num_members
>= 5)
3157 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
3158 else if (dev
->status
== DEV_CLONE_N_GO
)
3159 version
= MPB_VERSION_CNG
;
3160 else if (get_imsm_raid_level(map
) == 5)
3161 version
= MPB_VERSION_RAID5
;
3162 else if (map
->num_members
>= 3)
3163 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
3164 else if (get_imsm_raid_level(map
) == 1)
3165 version
= MPB_VERSION_RAID1
;
3167 version
= MPB_VERSION_RAID0
;
3169 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
3173 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
3175 struct imsm_super
*mpb
= super
->anchor
;
3176 char *reason
= NULL
;
3179 if (strlen(name
) > MAX_RAID_SERIAL_LEN
)
3180 reason
= "must be 16 characters or less";
3182 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3183 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3185 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
3186 reason
= "already exists";
3191 if (reason
&& !quiet
)
3192 fprintf(stderr
, Name
": imsm volume name %s\n", reason
);
3197 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
3198 unsigned long long size
, char *name
,
3199 char *homehost
, int *uuid
)
3201 /* We are creating a volume inside a pre-existing container.
3202 * so st->sb is already set.
3204 struct intel_super
*super
= st
->sb
;
3205 struct imsm_super
*mpb
= super
->anchor
;
3206 struct intel_dev
*dv
;
3207 struct imsm_dev
*dev
;
3208 struct imsm_vol
*vol
;
3209 struct imsm_map
*map
;
3210 int idx
= mpb
->num_raid_devs
;
3212 unsigned long long array_blocks
;
3213 size_t size_old
, size_new
;
3214 __u32 num_data_stripes
;
3216 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
3217 fprintf(stderr
, Name
": This imsm-container already has the "
3218 "maximum of %d volumes\n", super
->orom
->vpa
);
3222 /* ensure the mpb is large enough for the new data */
3223 size_old
= __le32_to_cpu(mpb
->mpb_size
);
3224 size_new
= disks_to_mpb_size(info
->nr_disks
);
3225 if (size_new
> size_old
) {
3227 size_t size_round
= ROUND_UP(size_new
, 512);
3229 if (posix_memalign(&mpb_new
, 512, size_round
) != 0) {
3230 fprintf(stderr
, Name
": could not allocate new mpb\n");
3233 memcpy(mpb_new
, mpb
, size_old
);
3236 super
->anchor
= mpb_new
;
3237 mpb
->mpb_size
= __cpu_to_le32(size_new
);
3238 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
3240 super
->current_vol
= idx
;
3241 /* when creating the first raid device in this container set num_disks
3242 * to zero, i.e. delete this spare and add raid member devices in
3243 * add_to_super_imsm_volume()
3245 if (super
->current_vol
== 0)
3248 if (!check_name(super
, name
, 0))
3250 dv
= malloc(sizeof(*dv
));
3252 fprintf(stderr
, Name
": failed to allocate device list entry\n");
3255 dev
= malloc(sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
3258 fprintf(stderr
, Name
": could not allocate raid device\n");
3261 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
3262 if (info
->level
== 1)
3263 array_blocks
= info_to_blocks_per_member(info
);
3265 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
3266 info
->layout
, info
->chunk_size
,
3268 /* round array size down to closest MB */
3269 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
3271 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
3272 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
3273 dev
->status
= __cpu_to_le32(0);
3274 dev
->reserved_blocks
= __cpu_to_le32(0);
3276 vol
->migr_state
= 0;
3277 set_migr_type(dev
, MIGR_INIT
);
3279 vol
->curr_migr_unit
= 0;
3280 map
= get_imsm_map(dev
, 0);
3281 map
->pba_of_lba0
= __cpu_to_le32(super
->create_offset
);
3282 map
->blocks_per_member
= __cpu_to_le32(info_to_blocks_per_member(info
));
3283 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
3284 map
->failed_disk_num
= ~0;
3285 map
->map_state
= info
->level
? IMSM_T_STATE_UNINITIALIZED
:
3286 IMSM_T_STATE_NORMAL
;
3289 if (info
->level
== 1 && info
->raid_disks
> 2) {
3292 fprintf(stderr
, Name
": imsm does not support more than 2 disks"
3293 "in a raid1 volume\n");
3297 map
->raid_level
= info
->level
;
3298 if (info
->level
== 10) {
3299 map
->raid_level
= 1;
3300 map
->num_domains
= info
->raid_disks
/ 2;
3301 } else if (info
->level
== 1)
3302 map
->num_domains
= info
->raid_disks
;
3304 map
->num_domains
= 1;
3306 num_data_stripes
= info_to_num_data_stripes(info
, map
->num_domains
);
3307 map
->num_data_stripes
= __cpu_to_le32(num_data_stripes
);
3309 map
->num_members
= info
->raid_disks
;
3310 for (i
= 0; i
< map
->num_members
; i
++) {
3311 /* initialized in add_to_super */
3312 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
3314 mpb
->num_raid_devs
++;
3317 dv
->index
= super
->current_vol
;
3318 dv
->next
= super
->devlist
;
3319 super
->devlist
= dv
;
3321 imsm_update_version_info(super
);
3326 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
3327 unsigned long long size
, char *name
,
3328 char *homehost
, int *uuid
)
3330 /* This is primarily called by Create when creating a new array.
3331 * We will then get add_to_super called for each component, and then
3332 * write_init_super called to write it out to each device.
3333 * For IMSM, Create can create on fresh devices or on a pre-existing
3335 * To create on a pre-existing array a different method will be called.
3336 * This one is just for fresh drives.
3338 struct intel_super
*super
;
3339 struct imsm_super
*mpb
;
3344 return init_super_imsm_volume(st
, info
, size
, name
, homehost
, uuid
);
3347 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
3351 super
= alloc_super();
3352 if (super
&& posix_memalign(&super
->buf
, 512, mpb_size
) != 0) {
3357 fprintf(stderr
, Name
3358 ": %s could not allocate superblock\n", __func__
);
3361 memset(super
->buf
, 0, mpb_size
);
3363 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
3367 /* zeroing superblock */
3371 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
3373 version
= (char *) mpb
->sig
;
3374 strcpy(version
, MPB_SIGNATURE
);
3375 version
+= strlen(MPB_SIGNATURE
);
3376 strcpy(version
, MPB_VERSION_RAID0
);
3382 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
3383 int fd
, char *devname
)
3385 struct intel_super
*super
= st
->sb
;
3386 struct imsm_super
*mpb
= super
->anchor
;
3388 struct imsm_dev
*dev
;
3389 struct imsm_map
*map
;
3392 dev
= get_imsm_dev(super
, super
->current_vol
);
3393 map
= get_imsm_map(dev
, 0);
3395 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
3396 fprintf(stderr
, Name
": %s: Cannot add spare devices to IMSM volume\n",
3402 /* we're doing autolayout so grab the pre-marked (in
3403 * validate_geometry) raid_disk
3405 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3406 if (dl
->raiddisk
== dk
->raid_disk
)
3409 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3410 if (dl
->major
== dk
->major
&&
3411 dl
->minor
== dk
->minor
)
3416 fprintf(stderr
, Name
": %s is not a member of the same container\n", devname
);
3420 /* add a pristine spare to the metadata */
3421 if (dl
->index
< 0) {
3422 dl
->index
= super
->anchor
->num_disks
;
3423 super
->anchor
->num_disks
++;
3425 /* Check the device has not already been added */
3426 slot
= get_imsm_disk_slot(map
, dl
->index
);
3428 (get_imsm_ord_tbl_ent(dev
, slot
, -1) & IMSM_ORD_REBUILD
) == 0) {
3429 fprintf(stderr
, Name
": %s has been included in this array twice\n",
3433 set_imsm_ord_tbl_ent(map
, dk
->number
, dl
->index
);
3434 dl
->disk
.status
= CONFIGURED_DISK
;
3436 /* if we are creating the first raid device update the family number */
3437 if (super
->current_vol
== 0) {
3439 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
3440 struct imsm_disk
*_disk
= __get_imsm_disk(mpb
, dl
->index
);
3442 if (!_dev
|| !_disk
) {
3443 fprintf(stderr
, Name
": BUG mpb setup error\n");
3449 sum
+= __gen_imsm_checksum(mpb
);
3450 mpb
->family_num
= __cpu_to_le32(sum
);
3451 mpb
->orig_family_num
= mpb
->family_num
;
3457 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
3458 int fd
, char *devname
)
3460 struct intel_super
*super
= st
->sb
;
3462 unsigned long long size
;
3467 /* if we are on an RAID enabled platform check that the disk is
3468 * attached to the raid controller
3470 if (super
->hba
&& !disk_attached_to_hba(fd
, super
->hba
)) {
3472 Name
": %s is not attached to the raid controller: %s\n",
3473 devname
? : "disk", super
->hba
);
3477 if (super
->current_vol
>= 0)
3478 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
3481 dd
= malloc(sizeof(*dd
));
3484 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
3487 memset(dd
, 0, sizeof(*dd
));
3488 dd
->major
= major(stb
.st_rdev
);
3489 dd
->minor
= minor(stb
.st_rdev
);
3491 dd
->devname
= devname
? strdup(devname
) : NULL
;
3494 dd
->action
= DISK_ADD
;
3495 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
3498 Name
": failed to retrieve scsi serial, aborting\n");
3503 get_dev_size(fd
, NULL
, &size
);
3505 serialcpy(dd
->disk
.serial
, dd
->serial
);
3506 dd
->disk
.total_blocks
= __cpu_to_le32(size
);
3507 dd
->disk
.status
= SPARE_DISK
;
3508 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
3509 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
3511 dd
->disk
.scsi_id
= __cpu_to_le32(0);
3513 if (st
->update_tail
) {
3514 dd
->next
= super
->disk_mgmt_list
;
3515 super
->disk_mgmt_list
= dd
;
3517 dd
->next
= super
->disks
;
3525 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
3527 struct intel_super
*super
= st
->sb
;
3530 /* remove from super works only in mdmon - for communication
3531 * manager - monitor. Check if communication memory buffer
3534 if (!st
->update_tail
) {
3536 Name
": %s shall be used in mdmon context only"
3537 "(line %d).\n", __func__
, __LINE__
);
3540 dd
= malloc(sizeof(*dd
));
3543 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
3546 memset(dd
, 0, sizeof(*dd
));
3547 dd
->major
= dk
->major
;
3548 dd
->minor
= dk
->minor
;
3551 dd
->disk
.status
= SPARE_DISK
;
3552 dd
->action
= DISK_REMOVE
;
3554 dd
->next
= super
->disk_mgmt_list
;
3555 super
->disk_mgmt_list
= dd
;
3561 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
3565 struct imsm_super anchor
;
3566 } spare_record
__attribute__ ((aligned(512)));
3568 /* spare records have their own family number and do not have any defined raid
3571 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
3573 struct imsm_super
*mpb
= super
->anchor
;
3574 struct imsm_super
*spare
= &spare_record
.anchor
;
3578 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
)),
3579 spare
->generation_num
= __cpu_to_le32(1UL),
3580 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
3581 spare
->num_disks
= 1,
3582 spare
->num_raid_devs
= 0,
3583 spare
->cache_size
= mpb
->cache_size
,
3584 spare
->pwr_cycle_count
= __cpu_to_le32(1),
3586 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
3587 MPB_SIGNATURE MPB_VERSION_RAID0
);
3589 for (d
= super
->disks
; d
; d
= d
->next
) {
3593 spare
->disk
[0] = d
->disk
;
3594 sum
= __gen_imsm_checksum(spare
);
3595 spare
->family_num
= __cpu_to_le32(sum
);
3596 spare
->orig_family_num
= 0;
3597 sum
= __gen_imsm_checksum(spare
);
3598 spare
->check_sum
= __cpu_to_le32(sum
);
3600 if (store_imsm_mpb(d
->fd
, spare
)) {
3601 fprintf(stderr
, "%s: failed for device %d:%d %s\n",
3602 __func__
, d
->major
, d
->minor
, strerror(errno
));
3614 static int write_super_imsm(struct supertype
*st
, int doclose
)
3616 struct intel_super
*super
= st
->sb
;
3617 struct imsm_super
*mpb
= super
->anchor
;
3623 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
3626 /* 'generation' is incremented everytime the metadata is written */
3627 generation
= __le32_to_cpu(mpb
->generation_num
);
3629 mpb
->generation_num
= __cpu_to_le32(generation
);
3631 /* fix up cases where previous mdadm releases failed to set
3634 if (mpb
->orig_family_num
== 0)
3635 mpb
->orig_family_num
= mpb
->family_num
;
3637 for (d
= super
->disks
; d
; d
= d
->next
) {
3641 mpb
->disk
[d
->index
] = d
->disk
;
3645 for (d
= super
->missing
; d
; d
= d
->next
) {
3646 mpb
->disk
[d
->index
] = d
->disk
;
3649 mpb
->num_disks
= num_disks
;
3650 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
3652 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3653 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
3654 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
3656 imsm_copy_dev(dev
, dev2
);
3657 mpb_size
+= sizeof_imsm_dev(dev
, 0);
3660 mpb_size
+= __le32_to_cpu(mpb
->bbm_log_size
);
3661 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
3663 /* recalculate checksum */
3664 sum
= __gen_imsm_checksum(mpb
);
3665 mpb
->check_sum
= __cpu_to_le32(sum
);
3667 /* write the mpb for disks that compose raid devices */
3668 for (d
= super
->disks
; d
; d
= d
->next
) {
3671 if (store_imsm_mpb(d
->fd
, mpb
))
3672 fprintf(stderr
, "%s: failed for device %d:%d %s\n",
3673 __func__
, d
->major
, d
->minor
, strerror(errno
));
3681 return write_super_imsm_spares(super
, doclose
);
3687 static int create_array(struct supertype
*st
, int dev_idx
)
3690 struct imsm_update_create_array
*u
;
3691 struct intel_super
*super
= st
->sb
;
3692 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
3693 struct imsm_map
*map
= get_imsm_map(dev
, 0);
3694 struct disk_info
*inf
;
3695 struct imsm_disk
*disk
;
3698 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
3699 sizeof(*inf
) * map
->num_members
;
3702 fprintf(stderr
, "%s: failed to allocate update buffer\n",
3707 u
->type
= update_create_array
;
3708 u
->dev_idx
= dev_idx
;
3709 imsm_copy_dev(&u
->dev
, dev
);
3710 inf
= get_disk_info(u
);
3711 for (i
= 0; i
< map
->num_members
; i
++) {
3712 int idx
= get_imsm_disk_idx(dev
, i
, -1);
3714 disk
= get_imsm_disk(super
, idx
);
3715 serialcpy(inf
[i
].serial
, disk
->serial
);
3717 append_metadata_update(st
, u
, len
);
3722 static int mgmt_disk(struct supertype
*st
)
3724 struct intel_super
*super
= st
->sb
;
3726 struct imsm_update_add_remove_disk
*u
;
3728 if (!super
->disk_mgmt_list
)
3734 fprintf(stderr
, "%s: failed to allocate update buffer\n",
3739 u
->type
= update_add_remove_disk
;
3740 append_metadata_update(st
, u
, len
);
3745 static int write_init_super_imsm(struct supertype
*st
)
3747 struct intel_super
*super
= st
->sb
;
3748 int current_vol
= super
->current_vol
;
3750 /* we are done with current_vol reset it to point st at the container */
3751 super
->current_vol
= -1;
3753 if (st
->update_tail
) {
3754 /* queue the recently created array / added disk
3755 * as a metadata update */
3758 /* determine if we are creating a volume or adding a disk */
3759 if (current_vol
< 0) {
3760 /* in the mgmt (add/remove) disk case we are running
3761 * in mdmon context, so don't close fd's
3763 return mgmt_disk(st
);
3765 rv
= create_array(st
, current_vol
);
3770 for (d
= super
->disks
; d
; d
= d
->next
)
3771 Kill(d
->devname
, NULL
, 0, 1, 1);
3772 return write_super_imsm(st
, 1);
3777 static int store_super_imsm(struct supertype
*st
, int fd
)
3779 struct intel_super
*super
= st
->sb
;
3780 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
3786 return store_imsm_mpb(fd
, mpb
);
3792 static int imsm_bbm_log_size(struct imsm_super
*mpb
)
3794 return __le32_to_cpu(mpb
->bbm_log_size
);
3798 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
3799 int layout
, int raiddisks
, int chunk
,
3800 unsigned long long size
, char *dev
,
3801 unsigned long long *freesize
,
3805 unsigned long long ldsize
;
3806 const struct imsm_orom
*orom
;
3808 if (level
!= LEVEL_CONTAINER
)
3813 if (check_env("IMSM_NO_PLATFORM"))
3816 orom
= find_imsm_orom();
3817 if (orom
&& raiddisks
> orom
->tds
) {
3819 fprintf(stderr
, Name
": %d exceeds maximum number of"
3820 " platform supported disks: %d\n",
3821 raiddisks
, orom
->tds
);
3825 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
3828 fprintf(stderr
, Name
": imsm: Cannot open %s: %s\n",
3829 dev
, strerror(errno
));
3832 if (!get_dev_size(fd
, dev
, &ldsize
)) {
3838 *freesize
= avail_size_imsm(st
, ldsize
>> 9);
3843 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
3845 const unsigned long long base_start
= e
[*idx
].start
;
3846 unsigned long long end
= base_start
+ e
[*idx
].size
;
3849 if (base_start
== end
)
3853 for (i
= *idx
; i
< num_extents
; i
++) {
3854 /* extend overlapping extents */
3855 if (e
[i
].start
>= base_start
&&
3856 e
[i
].start
<= end
) {
3859 if (e
[i
].start
+ e
[i
].size
> end
)
3860 end
= e
[i
].start
+ e
[i
].size
;
3861 } else if (e
[i
].start
> end
) {
3867 return end
- base_start
;
3870 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
3872 /* build a composite disk with all known extents and generate a new
3873 * 'maxsize' given the "all disks in an array must share a common start
3874 * offset" constraint
3876 struct extent
*e
= calloc(sum_extents
, sizeof(*e
));
3880 unsigned long long pos
;
3881 unsigned long long start
= 0;
3882 unsigned long long maxsize
;
3883 unsigned long reserve
;
3888 /* coalesce and sort all extents. also, check to see if we need to
3889 * reserve space between member arrays
3892 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
3895 for (i
= 0; i
< dl
->extent_cnt
; i
++)
3898 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
3903 while (i
< sum_extents
) {
3904 e
[j
].start
= e
[i
].start
;
3905 e
[j
].size
= find_size(e
, &i
, sum_extents
);
3907 if (e
[j
-1].size
== 0)
3916 unsigned long long esize
;
3918 esize
= e
[i
].start
- pos
;
3919 if (esize
>= maxsize
) {
3924 pos
= e
[i
].start
+ e
[i
].size
;
3926 } while (e
[i
-1].size
);
3932 /* FIXME assumes volume at offset 0 is the first volume in a
3935 if (start_extent
> 0)
3936 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
3940 if (maxsize
< reserve
)
3943 super
->create_offset
= ~((__u32
) 0);
3944 if (start
+ reserve
> super
->create_offset
)
3945 return 0; /* start overflows create_offset */
3946 super
->create_offset
= start
+ reserve
;
3948 return maxsize
- reserve
;
3951 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
3953 if (level
< 0 || level
== 6 || level
== 4)
3956 /* if we have an orom prevent invalid raid levels */
3959 case 0: return imsm_orom_has_raid0(orom
);
3962 return imsm_orom_has_raid1e(orom
);
3963 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
3964 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
3965 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
3968 return 1; /* not on an Intel RAID platform so anything goes */
3973 #define pr_vrb(fmt, arg...) (void) (verbose && fprintf(stderr, Name fmt, ##arg))
3975 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
3976 int raiddisks
, int chunk
, int verbose
)
3978 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
3979 pr_vrb(": platform does not support raid%d with %d disk%s\n",
3980 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
3983 if (super
->orom
&& level
!= 1 &&
3984 !imsm_orom_has_chunk(super
->orom
, chunk
)) {
3985 pr_vrb(": platform does not support a chunk size of: %d\n", chunk
);
3988 if (layout
!= imsm_level_to_layout(level
)) {
3990 pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
3991 else if (level
== 10)
3992 pr_vrb(": imsm raid 10 only supports the n2 layout\n");
3994 pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
4002 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
4003 * FIX ME add ahci details
4005 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
4006 int layout
, int raiddisks
, int chunk
,
4007 unsigned long long size
, char *dev
,
4008 unsigned long long *freesize
,
4012 struct intel_super
*super
= st
->sb
;
4013 struct imsm_super
*mpb
= super
->anchor
;
4015 unsigned long long pos
= 0;
4016 unsigned long long maxsize
;
4020 /* We must have the container info already read in. */
4024 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, verbose
))
4028 /* General test: make sure there is space for
4029 * 'raiddisks' device extents of size 'size' at a given
4032 unsigned long long minsize
= size
;
4033 unsigned long long start_offset
= MaxSector
;
4036 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
4037 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
4042 e
= get_extents(super
, dl
);
4045 unsigned long long esize
;
4046 esize
= e
[i
].start
- pos
;
4047 if (esize
>= minsize
)
4049 if (found
&& start_offset
== MaxSector
) {
4052 } else if (found
&& pos
!= start_offset
) {
4056 pos
= e
[i
].start
+ e
[i
].size
;
4058 } while (e
[i
-1].size
);
4063 if (dcnt
< raiddisks
) {
4065 fprintf(stderr
, Name
": imsm: Not enough "
4066 "devices with space for this array "
4074 /* This device must be a member of the set */
4075 if (stat(dev
, &stb
) < 0)
4077 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
4079 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
4080 if (dl
->major
== (int)major(stb
.st_rdev
) &&
4081 dl
->minor
== (int)minor(stb
.st_rdev
))
4086 fprintf(stderr
, Name
": %s is not in the "
4087 "same imsm set\n", dev
);
4089 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
4090 /* If a volume is present then the current creation attempt
4091 * cannot incorporate new spares because the orom may not
4092 * understand this configuration (all member disks must be
4093 * members of each array in the container).
4095 fprintf(stderr
, Name
": %s is a spare and a volume"
4096 " is already defined for this container\n", dev
);
4097 fprintf(stderr
, Name
": The option-rom requires all member"
4098 " disks to be a member of all volumes\n");
4102 /* retrieve the largest free space block */
4103 e
= get_extents(super
, dl
);
4108 unsigned long long esize
;
4110 esize
= e
[i
].start
- pos
;
4111 if (esize
>= maxsize
)
4113 pos
= e
[i
].start
+ e
[i
].size
;
4115 } while (e
[i
-1].size
);
4120 fprintf(stderr
, Name
": unable to determine free space for: %s\n",
4124 if (maxsize
< size
) {
4126 fprintf(stderr
, Name
": %s not enough space (%llu < %llu)\n",
4127 dev
, maxsize
, size
);
4131 /* count total number of extents for merge */
4133 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4135 i
+= dl
->extent_cnt
;
4137 maxsize
= merge_extents(super
, i
);
4138 if (maxsize
< size
|| maxsize
== 0) {
4140 fprintf(stderr
, Name
": not enough space after merge (%llu < %llu)\n",
4145 *freesize
= maxsize
;
4150 static int reserve_space(struct supertype
*st
, int raiddisks
,
4151 unsigned long long size
, int chunk
,
4152 unsigned long long *freesize
)
4154 struct intel_super
*super
= st
->sb
;
4155 struct imsm_super
*mpb
= super
->anchor
;
4160 unsigned long long maxsize
;
4161 unsigned long long minsize
;
4165 /* find the largest common start free region of the possible disks */
4169 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
4175 /* don't activate new spares if we are orom constrained
4176 * and there is already a volume active in the container
4178 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
4181 e
= get_extents(super
, dl
);
4184 for (i
= 1; e
[i
-1].size
; i
++)
4192 maxsize
= merge_extents(super
, extent_cnt
);
4197 if (cnt
< raiddisks
||
4198 (super
->orom
&& used
&& used
!= raiddisks
) ||
4199 maxsize
< minsize
||
4201 fprintf(stderr
, Name
": not enough devices with space to create array.\n");
4202 return 0; /* No enough free spaces large enough */
4214 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4216 dl
->raiddisk
= cnt
++;
4223 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
4224 int raiddisks
, int chunk
, unsigned long long size
,
4225 char *dev
, unsigned long long *freesize
,
4232 /* if given unused devices create a container
4233 * if given given devices in a container create a member volume
4235 if (level
== LEVEL_CONTAINER
) {
4236 /* Must be a fresh device to add to a container */
4237 return validate_geometry_imsm_container(st
, level
, layout
,
4238 raiddisks
, chunk
, size
,
4244 if (st
->sb
&& freesize
) {
4245 /* we are being asked to automatically layout a
4246 * new volume based on the current contents of
4247 * the container. If the the parameters can be
4248 * satisfied reserve_space will record the disks,
4249 * start offset, and size of the volume to be
4250 * created. add_to_super and getinfo_super
4251 * detect when autolayout is in progress.
4253 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
4257 return reserve_space(st
, raiddisks
, size
, chunk
, freesize
);
4262 /* creating in a given container */
4263 return validate_geometry_imsm_volume(st
, level
, layout
,
4264 raiddisks
, chunk
, size
,
4265 dev
, freesize
, verbose
);
4268 /* This device needs to be a device in an 'imsm' container */
4269 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
4273 Name
": Cannot create this array on device %s\n",
4278 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
4280 fprintf(stderr
, Name
": Cannot open %s: %s\n",
4281 dev
, strerror(errno
));
4284 /* Well, it is in use by someone, maybe an 'imsm' container. */
4285 cfd
= open_container(fd
);
4289 fprintf(stderr
, Name
": Cannot use %s: It is busy\n",
4293 sra
= sysfs_read(cfd
, 0, GET_VERSION
);
4294 if (sra
&& sra
->array
.major_version
== -1 &&
4295 strcmp(sra
->text_version
, "imsm") == 0)
4299 /* This is a member of a imsm container. Load the container
4300 * and try to create a volume
4302 struct intel_super
*super
;
4304 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
) == 0) {
4306 st
->container_dev
= fd2devnum(cfd
);
4308 return validate_geometry_imsm_volume(st
, level
, layout
,
4316 fprintf(stderr
, Name
": failed container membership check\n");
4322 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
4324 struct intel_super
*super
= st
->sb
;
4326 if (level
&& *level
== UnSet
)
4327 *level
= LEVEL_CONTAINER
;
4329 if (level
&& layout
&& *layout
== UnSet
)
4330 *layout
= imsm_level_to_layout(*level
);
4332 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0) &&
4333 super
&& super
->orom
)
4334 *chunk
= imsm_orom_default_chunk(super
->orom
);
4337 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
4339 static int kill_subarray_imsm(struct supertype
*st
)
4341 /* remove the subarray currently referenced by ->current_vol */
4343 struct intel_dev
**dp
;
4344 struct intel_super
*super
= st
->sb
;
4345 __u8 current_vol
= super
->current_vol
;
4346 struct imsm_super
*mpb
= super
->anchor
;
4348 if (super
->current_vol
< 0)
4350 super
->current_vol
= -1; /* invalidate subarray cursor */
4352 /* block deletions that would change the uuid of active subarrays
4354 * FIXME when immutable ids are available, but note that we'll
4355 * also need to fixup the invalidated/active subarray indexes in
4358 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4361 if (i
< current_vol
)
4363 sprintf(subarray
, "%u", i
);
4364 if (is_subarray_active(subarray
, st
->devname
)) {
4366 Name
": deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
4373 if (st
->update_tail
) {
4374 struct imsm_update_kill_array
*u
= malloc(sizeof(*u
));
4378 u
->type
= update_kill_array
;
4379 u
->dev_idx
= current_vol
;
4380 append_metadata_update(st
, u
, sizeof(*u
));
4385 for (dp
= &super
->devlist
; *dp
;)
4386 if ((*dp
)->index
== current_vol
) {
4389 handle_missing(super
, (*dp
)->dev
);
4390 if ((*dp
)->index
> current_vol
)
4395 /* no more raid devices, all active components are now spares,
4396 * but of course failed are still failed
4398 if (--mpb
->num_raid_devs
== 0) {
4401 for (d
= super
->disks
; d
; d
= d
->next
)
4402 if (d
->index
> -2) {
4404 d
->disk
.status
= SPARE_DISK
;
4408 super
->updates_pending
++;
4413 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
4414 char *update
, struct mddev_ident
*ident
)
4416 /* update the subarray currently referenced by ->current_vol */
4417 struct intel_super
*super
= st
->sb
;
4418 struct imsm_super
*mpb
= super
->anchor
;
4420 if (strcmp(update
, "name") == 0) {
4421 char *name
= ident
->name
;
4425 if (is_subarray_active(subarray
, st
->devname
)) {
4427 Name
": Unable to update name of active subarray\n");
4431 if (!check_name(super
, name
, 0))
4434 vol
= strtoul(subarray
, &ep
, 10);
4435 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
4438 if (st
->update_tail
) {
4439 struct imsm_update_rename_array
*u
= malloc(sizeof(*u
));
4443 u
->type
= update_rename_array
;
4445 snprintf((char *) u
->name
, MAX_RAID_SERIAL_LEN
, "%s", name
);
4446 append_metadata_update(st
, u
, sizeof(*u
));
4448 struct imsm_dev
*dev
;
4451 dev
= get_imsm_dev(super
, vol
);
4452 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
4453 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4454 dev
= get_imsm_dev(super
, i
);
4455 handle_missing(super
, dev
);
4457 super
->updates_pending
++;
4464 #endif /* MDASSEMBLE */
4466 static int is_gen_migration(struct imsm_dev
*dev
)
4468 if (!dev
->vol
.migr_state
)
4471 if (migr_type(dev
) == MIGR_GEN_MIGR
)
4477 static int is_rebuilding(struct imsm_dev
*dev
)
4479 struct imsm_map
*migr_map
;
4481 if (!dev
->vol
.migr_state
)
4484 if (migr_type(dev
) != MIGR_REBUILD
)
4487 migr_map
= get_imsm_map(dev
, 1);
4489 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
4495 static void update_recovery_start(struct imsm_dev
*dev
, struct mdinfo
*array
)
4497 struct mdinfo
*rebuild
= NULL
;
4501 if (!is_rebuilding(dev
))
4504 /* Find the rebuild target, but punt on the dual rebuild case */
4505 for (d
= array
->devs
; d
; d
= d
->next
)
4506 if (d
->recovery_start
== 0) {
4513 /* (?) none of the disks are marked with
4514 * IMSM_ORD_REBUILD, so assume they are missing and the
4515 * disk_ord_tbl was not correctly updated
4517 dprintf("%s: failed to locate out-of-sync disk\n", __func__
);
4521 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
4522 rebuild
->recovery_start
= units
* blocks_per_migr_unit(dev
);
4526 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
4528 /* Given a container loaded by load_super_imsm_all,
4529 * extract information about all the arrays into
4531 * If 'subarray' is given, just extract info about that array.
4533 * For each imsm_dev create an mdinfo, fill it in,
4534 * then look for matching devices in super->disks
4535 * and create appropriate device mdinfo.
4537 struct intel_super
*super
= st
->sb
;
4538 struct imsm_super
*mpb
= super
->anchor
;
4539 struct mdinfo
*rest
= NULL
;
4543 /* check for bad blocks */
4544 if (imsm_bbm_log_size(super
->anchor
))
4547 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4548 struct imsm_dev
*dev
;
4549 struct imsm_map
*map
;
4550 struct imsm_map
*map2
;
4551 struct mdinfo
*this;
4556 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
4559 dev
= get_imsm_dev(super
, i
);
4560 map
= get_imsm_map(dev
, 0);
4561 map2
= get_imsm_map(dev
, 1);
4563 /* do not publish arrays that are in the middle of an
4564 * unsupported migration
4566 if (dev
->vol
.migr_state
&&
4567 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
4568 fprintf(stderr
, Name
": cannot assemble volume '%.16s':"
4569 " unsupported migration in progress\n",
4574 this = malloc(sizeof(*this));
4576 fprintf(stderr
, Name
": failed to allocate %zu bytes\n",
4580 memset(this, 0, sizeof(*this));
4583 super
->current_vol
= i
;
4584 getinfo_super_imsm_volume(st
, this, NULL
);
4585 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
4586 unsigned long long recovery_start
;
4587 struct mdinfo
*info_d
;
4594 idx
= get_imsm_disk_idx(dev
, slot
, 0);
4595 ord
= get_imsm_ord_tbl_ent(dev
, slot
, 0);
4596 for (d
= super
->disks
; d
; d
= d
->next
)
4597 if (d
->index
== idx
)
4600 recovery_start
= MaxSector
;
4603 if (d
&& is_failed(&d
->disk
))
4605 if (ord
& IMSM_ORD_REBUILD
)
4609 * if we skip some disks the array will be assmebled degraded;
4610 * reset resync start to avoid a dirty-degraded
4611 * situation when performing the intial sync
4613 * FIXME handle dirty degraded
4615 if ((skip
|| recovery_start
== 0) && !dev
->vol
.dirty
)
4616 this->resync_start
= MaxSector
;
4620 info_d
= calloc(1, sizeof(*info_d
));
4622 fprintf(stderr
, Name
": failed to allocate disk"
4623 " for volume %.16s\n", dev
->volume
);
4624 info_d
= this->devs
;
4626 struct mdinfo
*d
= info_d
->next
;
4635 info_d
->next
= this->devs
;
4636 this->devs
= info_d
;
4638 info_d
->disk
.number
= d
->index
;
4639 info_d
->disk
.major
= d
->major
;
4640 info_d
->disk
.minor
= d
->minor
;
4641 info_d
->disk
.raid_disk
= slot
;
4642 info_d
->recovery_start
= recovery_start
;
4644 if (slot
< map2
->num_members
)
4645 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
4647 this->array
.spare_disks
++;
4649 if (slot
< map
->num_members
)
4650 info_d
->disk
.state
= (1 << MD_DISK_ACTIVE
);
4652 this->array
.spare_disks
++;
4654 if (info_d
->recovery_start
== MaxSector
)
4655 this->array
.working_disks
++;
4657 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
4658 info_d
->data_offset
= __le32_to_cpu(map
->pba_of_lba0
);
4659 info_d
->component_size
= __le32_to_cpu(map
->blocks_per_member
);
4661 /* now that the disk list is up-to-date fixup recovery_start */
4662 update_recovery_start(dev
, this);
4666 /* if array has bad blocks, set suitable bit in array status */
4668 rest
->array
.state
|= (1<<MD_SB_BBM_ERRORS
);
4674 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
, int failed
)
4676 struct imsm_map
*map
= get_imsm_map(dev
, 0);
4679 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
4680 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
4682 switch (get_imsm_raid_level(map
)) {
4684 return IMSM_T_STATE_FAILED
;
4687 if (failed
< map
->num_members
)
4688 return IMSM_T_STATE_DEGRADED
;
4690 return IMSM_T_STATE_FAILED
;
4695 * check to see if any mirrors have failed, otherwise we
4696 * are degraded. Even numbered slots are mirrored on
4700 /* gcc -Os complains that this is unused */
4701 int insync
= insync
;
4703 for (i
= 0; i
< map
->num_members
; i
++) {
4704 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
, -1);
4705 int idx
= ord_to_idx(ord
);
4706 struct imsm_disk
*disk
;
4708 /* reset the potential in-sync count on even-numbered
4709 * slots. num_copies is always 2 for imsm raid10
4714 disk
= get_imsm_disk(super
, idx
);
4715 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
4718 /* no in-sync disks left in this mirror the
4722 return IMSM_T_STATE_FAILED
;
4725 return IMSM_T_STATE_DEGRADED
;
4729 return IMSM_T_STATE_DEGRADED
;
4731 return IMSM_T_STATE_FAILED
;
4737 return map
->map_state
;
4740 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
)
4744 struct imsm_disk
*disk
;
4745 struct imsm_map
*map
= get_imsm_map(dev
, 0);
4746 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
);
4750 /* at the beginning of migration we set IMSM_ORD_REBUILD on
4751 * disks that are being rebuilt. New failures are recorded to
4752 * map[0]. So we look through all the disks we started with and
4753 * see if any failures are still present, or if any new ones
4756 * FIXME add support for online capacity expansion and
4757 * raid-level-migration
4759 for (i
= 0; i
< prev
->num_members
; i
++) {
4760 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
4761 ord
|= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
4762 idx
= ord_to_idx(ord
);
4764 disk
= get_imsm_disk(super
, idx
);
4765 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
4773 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
4776 struct intel_super
*super
= c
->sb
;
4777 struct imsm_super
*mpb
= super
->anchor
;
4779 if (atoi(inst
) >= mpb
->num_raid_devs
) {
4780 fprintf(stderr
, "%s: subarry index %d, out of range\n",
4781 __func__
, atoi(inst
));
4785 dprintf("imsm: open_new %s\n", inst
);
4786 a
->info
.container_member
= atoi(inst
);
4790 static int is_resyncing(struct imsm_dev
*dev
)
4792 struct imsm_map
*migr_map
;
4794 if (!dev
->vol
.migr_state
)
4797 if (migr_type(dev
) == MIGR_INIT
||
4798 migr_type(dev
) == MIGR_REPAIR
)
4801 if (migr_type(dev
) == MIGR_GEN_MIGR
)
4804 migr_map
= get_imsm_map(dev
, 1);
4806 if ((migr_map
->map_state
== IMSM_T_STATE_NORMAL
) &&
4807 (dev
->vol
.migr_type
!= MIGR_GEN_MIGR
))
4813 /* return true if we recorded new information */
4814 static int mark_failure(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
4818 struct imsm_map
*map
;
4820 /* new failures are always set in map[0] */
4821 map
= get_imsm_map(dev
, 0);
4823 slot
= get_imsm_disk_slot(map
, idx
);
4827 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
4828 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
4831 disk
->status
|= FAILED_DISK
;
4832 disk
->status
&= ~CONFIGURED_DISK
;
4833 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
4834 if (map
->failed_disk_num
== 0xff)
4835 map
->failed_disk_num
= slot
;
4839 static void mark_missing(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
4841 mark_failure(dev
, disk
, idx
);
4843 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
4846 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
4847 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
4850 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
4856 if (!super
->missing
)
4858 failed
= imsm_count_failed(super
, dev
);
4859 map_state
= imsm_check_degraded(super
, dev
, failed
);
4861 dprintf("imsm: mark missing\n");
4862 end_migration(dev
, map_state
);
4863 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
4864 mark_missing(dev
, &dl
->disk
, dl
->index
);
4865 super
->updates_pending
++;
4868 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
4870 static void imsm_progress_container_reshape(struct intel_super
*super
)
4872 /* if no device has a migr_state, but some device has a
4873 * different number of members than the previous device, start
4874 * changing the number of devices in this device to match
4877 struct imsm_super
*mpb
= super
->anchor
;
4878 int prev_disks
= -1;
4881 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4882 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4883 struct imsm_map
*map
= get_imsm_map(dev
, 0);
4884 struct imsm_map
*map2
;
4885 int prev_num_members
;
4888 if (dev
->vol
.migr_state
)
4891 if (prev_disks
== -1)
4892 prev_disks
= map
->num_members
;
4893 if (prev_disks
== map
->num_members
)
4896 /* OK, this array needs to enter reshape mode.
4897 * i.e it needs a migr_state
4900 prev_num_members
= map
->num_members
;
4901 map
->num_members
= prev_disks
;
4902 dev
->vol
.migr_state
= 1;
4903 dev
->vol
.curr_migr_unit
= 0;
4904 dev
->vol
.migr_type
= MIGR_GEN_MIGR
;
4905 for (i
= prev_num_members
;
4906 i
< map
->num_members
; i
++)
4907 set_imsm_ord_tbl_ent(map
, i
, i
);
4908 map2
= get_imsm_map(dev
, 1);
4909 /* Copy the current map */
4910 memcpy(map2
, map
, sizeof_imsm_map(map
));
4911 map2
->num_members
= prev_num_members
;
4913 /* calculate new size
4915 used_disks
= imsm_num_data_members(dev
, 0);
4917 unsigned long long array_blocks
;
4920 map
->blocks_per_member
4922 /* round array size down to closest MB
4924 array_blocks
= (array_blocks
4925 >> SECT_PER_MB_SHIFT
)
4926 << SECT_PER_MB_SHIFT
;
4928 __cpu_to_le32((__u32
)array_blocks
);
4931 (__u32
)(array_blocks
>> 32));
4933 super
->updates_pending
++;
4937 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
4938 * states are handled in imsm_set_disk() with one exception, when a
4939 * resync is stopped due to a new failure this routine will set the
4940 * 'degraded' state for the array.
4942 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
4944 int inst
= a
->info
.container_member
;
4945 struct intel_super
*super
= a
->container
->sb
;
4946 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
4947 struct imsm_map
*map
= get_imsm_map(dev
, 0);
4948 int failed
= imsm_count_failed(super
, dev
);
4949 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
);
4950 __u32 blocks_per_unit
;
4952 if (dev
->vol
.migr_state
&&
4953 dev
->vol
.migr_type
== MIGR_GEN_MIGR
) {
4954 /* array state change is blocked due to reshape action
4956 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
4957 * - finish the reshape (if last_checkpoint is big and action != reshape)
4958 * - update curr_migr_unit
4960 if (a
->curr_action
== reshape
) {
4961 /* still reshaping, maybe update curr_migr_unit */
4962 long long blocks_per_unit
= blocks_per_migr_unit(dev
);
4963 long long unit
= a
->last_checkpoint
;
4964 if (blocks_per_unit
) {
4965 unit
/= blocks_per_unit
;
4967 __le32_to_cpu(dev
->vol
.curr_migr_unit
)) {
4968 dev
->vol
.curr_migr_unit
=
4969 __cpu_to_le32(unit
);
4970 super
->updates_pending
++;
4975 if (a
->last_checkpoint
== 0 && a
->prev_action
== reshape
) {
4976 /* for some reason we aborted the reshape.
4979 struct imsm_map
*map2
= get_imsm_map(dev
, 1);
4980 dev
->vol
.migr_state
= 0;
4981 dev
->vol
.migr_type
= 0;
4982 dev
->vol
.curr_migr_unit
= 0;
4983 memcpy(map
, map2
, sizeof_imsm_map(map2
));
4984 super
->updates_pending
++;
4986 if (a
->last_checkpoint
>= a
->info
.component_size
) {
4987 unsigned long long array_blocks
;
4989 /* it seems the reshape is all done */
4990 dev
->vol
.migr_state
= 0;
4991 dev
->vol
.migr_type
= 0;
4992 dev
->vol
.curr_migr_unit
= 0;
4994 used_disks
= imsm_num_data_members(dev
, -1);
4995 array_blocks
= map
->blocks_per_member
* used_disks
;
4996 /* round array size down to closest MB */
4997 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
)
4998 << SECT_PER_MB_SHIFT
;
4999 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
5000 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
5001 a
->info
.custom_array_size
= array_blocks
;
5002 a
->check_reshape
= 1; /* encourage manager to update
5005 super
->updates_pending
++;
5006 imsm_progress_container_reshape(super
);
5011 /* before we activate this array handle any missing disks */
5012 if (consistent
== 2)
5013 handle_missing(super
, dev
);
5015 if (consistent
== 2 &&
5016 (!is_resync_complete(&a
->info
) ||
5017 map_state
!= IMSM_T_STATE_NORMAL
||
5018 dev
->vol
.migr_state
))
5021 if (is_resync_complete(&a
->info
)) {
5022 /* complete intialization / resync,
5023 * recovery and interrupted recovery is completed in
5026 if (is_resyncing(dev
)) {
5027 dprintf("imsm: mark resync done\n");
5028 end_migration(dev
, map_state
);
5029 super
->updates_pending
++;
5030 a
->last_checkpoint
= 0;
5032 } else if (!is_resyncing(dev
) && !failed
) {
5033 /* mark the start of the init process if nothing is failed */
5034 dprintf("imsm: mark resync start\n");
5035 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
5036 migrate(dev
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
5038 migrate(dev
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
5039 super
->updates_pending
++;
5042 /* check if we can update curr_migr_unit from resync_start, recovery_start */
5043 blocks_per_unit
= blocks_per_migr_unit(dev
);
5044 if (blocks_per_unit
) {
5048 units
= a
->last_checkpoint
/ blocks_per_unit
;
5051 /* check that we did not overflow 32-bits, and that
5052 * curr_migr_unit needs updating
5054 if (units32
== units
&&
5055 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
5056 dprintf("imsm: mark checkpoint (%u)\n", units32
);
5057 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
5058 super
->updates_pending
++;
5062 /* mark dirty / clean */
5063 if (dev
->vol
.dirty
!= !consistent
) {
5064 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
5069 super
->updates_pending
++;
5072 /* finalize online capacity expansion/reshape */
5073 if ((a
->curr_action
!= reshape
) &&
5074 (a
->prev_action
== reshape
)) {
5077 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
5078 imsm_set_disk(a
, mdi
->disk
.raid_disk
, mdi
->curr_state
);
5084 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
5086 int inst
= a
->info
.container_member
;
5087 struct intel_super
*super
= a
->container
->sb
;
5088 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
5089 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5090 struct imsm_disk
*disk
;
5095 if (n
> map
->num_members
)
5096 fprintf(stderr
, "imsm: set_disk %d out of range 0..%d\n",
5097 n
, map
->num_members
- 1);
5102 dprintf("imsm: set_disk %d:%x\n", n
, state
);
5104 ord
= get_imsm_ord_tbl_ent(dev
, n
, -1);
5105 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
5107 /* check for new failures */
5108 if (state
& DS_FAULTY
) {
5109 if (mark_failure(dev
, disk
, ord_to_idx(ord
)))
5110 super
->updates_pending
++;
5113 /* check if in_sync */
5114 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
5115 struct imsm_map
*migr_map
= get_imsm_map(dev
, 1);
5117 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
5118 super
->updates_pending
++;
5121 failed
= imsm_count_failed(super
, dev
);
5122 map_state
= imsm_check_degraded(super
, dev
, failed
);
5124 /* check if recovery complete, newly degraded, or failed */
5125 if (map_state
== IMSM_T_STATE_NORMAL
&& is_rebuilding(dev
)) {
5126 end_migration(dev
, map_state
);
5127 map
= get_imsm_map(dev
, 0);
5128 map
->failed_disk_num
= ~0;
5129 super
->updates_pending
++;
5130 a
->last_checkpoint
= 0;
5131 } else if (map_state
== IMSM_T_STATE_DEGRADED
&&
5132 map
->map_state
!= map_state
&&
5133 !dev
->vol
.migr_state
) {
5134 dprintf("imsm: mark degraded\n");
5135 map
->map_state
= map_state
;
5136 super
->updates_pending
++;
5137 a
->last_checkpoint
= 0;
5138 } else if (map_state
== IMSM_T_STATE_FAILED
&&
5139 map
->map_state
!= map_state
) {
5140 dprintf("imsm: mark failed\n");
5141 end_migration(dev
, map_state
);
5142 super
->updates_pending
++;
5143 a
->last_checkpoint
= 0;
5144 } else if (is_gen_migration(dev
)) {
5145 dprintf("imsm: Detected General Migration in state: ");
5146 if (map_state
== IMSM_T_STATE_NORMAL
) {
5147 end_migration(dev
, map_state
);
5148 map
= get_imsm_map(dev
, 0);
5149 map
->failed_disk_num
= ~0;
5150 dprintf("normal\n");
5152 if (map_state
== IMSM_T_STATE_DEGRADED
) {
5153 printf("degraded\n");
5154 end_migration(dev
, map_state
);
5156 dprintf("failed\n");
5158 map
->map_state
= map_state
;
5160 super
->updates_pending
++;
5164 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
5167 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
5168 unsigned long long dsize
;
5169 unsigned long long sectors
;
5171 get_dev_size(fd
, NULL
, &dsize
);
5173 if (mpb_size
> 512) {
5174 /* -1 to account for anchor */
5175 sectors
= mpb_sectors(mpb
) - 1;
5177 /* write the extended mpb to the sectors preceeding the anchor */
5178 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0)
5181 if ((unsigned long long)write(fd
, buf
+ 512, 512 * sectors
)
5186 /* first block is stored on second to last sector of the disk */
5187 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0)
5190 if (write(fd
, buf
, 512) != 512)
5196 static void imsm_sync_metadata(struct supertype
*container
)
5198 struct intel_super
*super
= container
->sb
;
5200 dprintf("sync metadata: %d\n", super
->updates_pending
);
5201 if (!super
->updates_pending
)
5204 write_super_imsm(container
, 0);
5206 super
->updates_pending
= 0;
5209 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
5211 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
5212 int i
= get_imsm_disk_idx(dev
, idx
, -1);
5215 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5219 if (dl
&& is_failed(&dl
->disk
))
5223 dprintf("%s: found %x:%x\n", __func__
, dl
->major
, dl
->minor
);
5228 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
5229 struct active_array
*a
, int activate_new
,
5230 struct mdinfo
*additional_test_list
)
5232 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
5233 int idx
= get_imsm_disk_idx(dev
, slot
, -1);
5234 struct imsm_super
*mpb
= super
->anchor
;
5235 struct imsm_map
*map
;
5236 unsigned long long pos
;
5241 __u32 array_start
= 0;
5242 __u32 array_end
= 0;
5244 struct mdinfo
*test_list
;
5246 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5247 /* If in this array, skip */
5248 for (d
= a
->info
.devs
; d
; d
= d
->next
)
5249 if (d
->state_fd
>= 0 &&
5250 d
->disk
.major
== dl
->major
&&
5251 d
->disk
.minor
== dl
->minor
) {
5252 dprintf("%x:%x already in array\n",
5253 dl
->major
, dl
->minor
);
5258 test_list
= additional_test_list
;
5260 if (test_list
->disk
.major
== dl
->major
&&
5261 test_list
->disk
.minor
== dl
->minor
) {
5262 dprintf("%x:%x already in additional test list\n",
5263 dl
->major
, dl
->minor
);
5266 test_list
= test_list
->next
;
5271 /* skip in use or failed drives */
5272 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
5274 dprintf("%x:%x status (failed: %d index: %d)\n",
5275 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
5279 /* skip pure spares when we are looking for partially
5280 * assimilated drives
5282 if (dl
->index
== -1 && !activate_new
)
5285 /* Does this unused device have the requisite free space?
5286 * It needs to be able to cover all member volumes
5288 ex
= get_extents(super
, dl
);
5290 dprintf("cannot get extents\n");
5293 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5294 dev
= get_imsm_dev(super
, i
);
5295 map
= get_imsm_map(dev
, 0);
5297 /* check if this disk is already a member of
5300 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
5306 array_start
= __le32_to_cpu(map
->pba_of_lba0
);
5307 array_end
= array_start
+
5308 __le32_to_cpu(map
->blocks_per_member
) - 1;
5311 /* check that we can start at pba_of_lba0 with
5312 * blocks_per_member of space
5314 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
5318 pos
= ex
[j
].start
+ ex
[j
].size
;
5320 } while (ex
[j
-1].size
);
5327 if (i
< mpb
->num_raid_devs
) {
5328 dprintf("%x:%x does not have %u to %u available\n",
5329 dl
->major
, dl
->minor
, array_start
, array_end
);
5340 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
5342 struct imsm_dev
*dev2
;
5343 struct imsm_map
*map
;
5349 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
5351 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
);
5352 if (state
== IMSM_T_STATE_FAILED
) {
5353 map
= get_imsm_map(dev2
, 0);
5356 for (slot
= 0; slot
< map
->num_members
; slot
++) {
5358 * Check if failed disks are deleted from intel
5359 * disk list or are marked to be deleted
5361 idx
= get_imsm_disk_idx(dev2
, slot
, -1);
5362 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
5364 * Do not rebuild the array if failed disks
5365 * from failed sub-array are not removed from
5369 is_failed(&idisk
->disk
) &&
5370 (idisk
->action
!= DISK_REMOVE
))
5378 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
5379 struct metadata_update
**updates
)
5382 * Find a device with unused free space and use it to replace a
5383 * failed/vacant region in an array. We replace failed regions one a
5384 * array at a time. The result is that a new spare disk will be added
5385 * to the first failed array and after the monitor has finished
5386 * propagating failures the remainder will be consumed.
5388 * FIXME add a capability for mdmon to request spares from another
5392 struct intel_super
*super
= a
->container
->sb
;
5393 int inst
= a
->info
.container_member
;
5394 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
5395 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5396 int failed
= a
->info
.array
.raid_disks
;
5397 struct mdinfo
*rv
= NULL
;
5400 struct metadata_update
*mu
;
5402 struct imsm_update_activate_spare
*u
;
5407 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
5408 if ((d
->curr_state
& DS_FAULTY
) &&
5410 /* wait for Removal to happen */
5412 if (d
->state_fd
>= 0)
5416 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
5417 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
5419 if (dev
->vol
.migr_state
&&
5420 dev
->vol
.migr_type
== MIGR_GEN_MIGR
)
5421 /* No repair during migration */
5424 if (a
->info
.array
.level
== 4)
5425 /* No repair for takeovered array
5426 * imsm doesn't support raid4
5430 if (imsm_check_degraded(super
, dev
, failed
) != IMSM_T_STATE_DEGRADED
)
5434 * If there are any failed disks check state of the other volume.
5435 * Block rebuild if the another one is failed until failed disks
5436 * are removed from container.
5439 dprintf("found failed disks in %s, check if there another"
5440 "failed sub-array.\n",
5442 /* check if states of the other volumes allow for rebuild */
5443 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
5445 allowed
= imsm_rebuild_allowed(a
->container
,
5453 /* For each slot, if it is not working, find a spare */
5454 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
5455 for (d
= a
->info
.devs
; d
; d
= d
->next
)
5456 if (d
->disk
.raid_disk
== i
)
5458 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
5459 if (d
&& (d
->state_fd
>= 0))
5463 * OK, this device needs recovery. Try to re-add the
5464 * previous occupant of this slot, if this fails see if
5465 * we can continue the assimilation of a spare that was
5466 * partially assimilated, finally try to activate a new
5469 dl
= imsm_readd(super
, i
, a
);
5471 dl
= imsm_add_spare(super
, i
, a
, 0, NULL
);
5473 dl
= imsm_add_spare(super
, i
, a
, 1, NULL
);
5477 /* found a usable disk with enough space */
5478 di
= malloc(sizeof(*di
));
5481 memset(di
, 0, sizeof(*di
));
5483 /* dl->index will be -1 in the case we are activating a
5484 * pristine spare. imsm_process_update() will create a
5485 * new index in this case. Once a disk is found to be
5486 * failed in all member arrays it is kicked from the
5489 di
->disk
.number
= dl
->index
;
5491 /* (ab)use di->devs to store a pointer to the device
5494 di
->devs
= (struct mdinfo
*) dl
;
5496 di
->disk
.raid_disk
= i
;
5497 di
->disk
.major
= dl
->major
;
5498 di
->disk
.minor
= dl
->minor
;
5500 di
->recovery_start
= 0;
5501 di
->data_offset
= __le32_to_cpu(map
->pba_of_lba0
);
5502 di
->component_size
= a
->info
.component_size
;
5503 di
->container_member
= inst
;
5504 super
->random
= random32();
5508 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
5509 i
, di
->data_offset
);
5515 /* No spares found */
5517 /* Now 'rv' has a list of devices to return.
5518 * Create a metadata_update record to update the
5519 * disk_ord_tbl for the array
5521 mu
= malloc(sizeof(*mu
));
5523 mu
->buf
= malloc(sizeof(struct imsm_update_activate_spare
) * num_spares
);
5524 if (mu
->buf
== NULL
) {
5531 struct mdinfo
*n
= rv
->next
;
5540 mu
->space_list
= NULL
;
5541 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
5542 mu
->next
= *updates
;
5543 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
5545 for (di
= rv
; di
; di
= di
->next
) {
5546 u
->type
= update_activate_spare
;
5547 u
->dl
= (struct dl
*) di
->devs
;
5549 u
->slot
= di
->disk
.raid_disk
;
5560 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
5562 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
5563 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5564 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, 0);
5565 struct disk_info
*inf
= get_disk_info(u
);
5566 struct imsm_disk
*disk
;
5570 for (i
= 0; i
< map
->num_members
; i
++) {
5571 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
, -1));
5572 for (j
= 0; j
< new_map
->num_members
; j
++)
5573 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
5581 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
5583 struct dl
*dl
= NULL
;
5584 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5585 if ((dl
->major
== major
) && (dl
->minor
== minor
))
5590 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
5592 struct dl
*prev
= NULL
;
5596 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5597 if ((dl
->major
== major
) && (dl
->minor
== minor
)) {
5600 prev
->next
= dl
->next
;
5602 super
->disks
= dl
->next
;
5604 __free_imsm_disk(dl
);
5605 dprintf("%s: removed %x:%x\n",
5606 __func__
, major
, minor
);
5614 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
5616 static int add_remove_disk_update(struct intel_super
*super
)
5618 int check_degraded
= 0;
5619 struct dl
*disk
= NULL
;
5620 /* add/remove some spares to/from the metadata/contrainer */
5621 while (super
->disk_mgmt_list
) {
5622 struct dl
*disk_cfg
;
5624 disk_cfg
= super
->disk_mgmt_list
;
5625 super
->disk_mgmt_list
= disk_cfg
->next
;
5626 disk_cfg
->next
= NULL
;
5628 if (disk_cfg
->action
== DISK_ADD
) {
5629 disk_cfg
->next
= super
->disks
;
5630 super
->disks
= disk_cfg
;
5632 dprintf("%s: added %x:%x\n",
5633 __func__
, disk_cfg
->major
,
5635 } else if (disk_cfg
->action
== DISK_REMOVE
) {
5636 dprintf("Disk remove action processed: %x.%x\n",
5637 disk_cfg
->major
, disk_cfg
->minor
);
5638 disk
= get_disk_super(super
,
5642 /* store action status */
5643 disk
->action
= DISK_REMOVE
;
5644 /* remove spare disks only */
5645 if (disk
->index
== -1) {
5646 remove_disk_super(super
,
5651 /* release allocate disk structure */
5652 __free_imsm_disk(disk_cfg
);
5655 return check_degraded
;
5658 static int apply_reshape_container_disks_update(struct imsm_update_reshape
*u
,
5659 struct intel_super
*super
,
5662 struct dl
*new_disk
;
5663 struct intel_dev
*id
;
5665 int delta_disks
= u
->new_raid_disks
- u
->old_raid_disks
;
5666 int disk_count
= u
->old_raid_disks
;
5667 void **tofree
= NULL
;
5668 int devices_to_reshape
= 1;
5669 struct imsm_super
*mpb
= super
->anchor
;
5672 dprintf("imsm: imsm_process_update() for update_reshape\n");
5674 /* enable spares to use in array */
5675 for (i
= 0; i
< delta_disks
; i
++) {
5676 new_disk
= get_disk_super(super
,
5677 major(u
->new_disks
[i
]),
5678 minor(u
->new_disks
[i
]));
5679 dprintf("imsm: imsm_process_update(): new disk "
5680 "for reshape is: %i:%i (%p, index = %i)\n",
5681 major(u
->new_disks
[i
]), minor(u
->new_disks
[i
]),
5682 new_disk
, new_disk
->index
);
5683 if ((new_disk
== NULL
) ||
5684 ((new_disk
->index
>= 0) &&
5685 (new_disk
->index
< u
->old_raid_disks
)))
5686 goto update_reshape_exit
;
5687 new_disk
->index
= disk_count
++;
5688 /* slot to fill in autolayout
5690 new_disk
->raiddisk
= new_disk
->index
;
5691 new_disk
->disk
.status
|=
5693 new_disk
->disk
.status
&= ~SPARE_DISK
;
5696 dprintf("imsm: process_update(): update_reshape: volume set"
5697 " mpb->num_raid_devs = %i\n", mpb
->num_raid_devs
);
5698 /* manage changes in volume
5700 for (id
= super
->devlist
; id
; id
= id
->next
) {
5701 void **sp
= *space_list
;
5702 struct imsm_dev
*newdev
;
5703 struct imsm_map
*newmap
, *oldmap
;
5709 /* Copy the dev, but not (all of) the map */
5710 memcpy(newdev
, id
->dev
, sizeof(*newdev
));
5711 oldmap
= get_imsm_map(id
->dev
, 0);
5712 newmap
= get_imsm_map(newdev
, 0);
5713 /* Copy the current map */
5714 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
5715 /* update one device only
5717 if (devices_to_reshape
) {
5720 dprintf("process_update(): modifying "
5721 "subdev: %i\n", id
->index
);
5722 devices_to_reshape
--;
5723 newdev
->vol
.migr_state
= 1;
5724 newdev
->vol
.curr_migr_unit
= 0;
5725 newdev
->vol
.migr_type
= MIGR_GEN_MIGR
;
5726 newmap
->num_members
= u
->new_raid_disks
;
5727 for (i
= 0; i
< delta_disks
; i
++) {
5728 set_imsm_ord_tbl_ent(newmap
,
5729 u
->old_raid_disks
+ i
,
5730 u
->old_raid_disks
+ i
);
5732 /* New map is correct, now need to save old map
5734 newmap
= get_imsm_map(newdev
, 1);
5735 memcpy(newmap
, oldmap
, sizeof_imsm_map(oldmap
));
5737 /* calculate new size
5739 used_disks
= imsm_num_data_members(newdev
, 0);
5741 unsigned long long array_blocks
;
5744 newmap
->blocks_per_member
* used_disks
;
5745 /* round array size down to closest MB
5747 array_blocks
= (array_blocks
5748 >> SECT_PER_MB_SHIFT
)
5749 << SECT_PER_MB_SHIFT
;
5751 __cpu_to_le32((__u32
)array_blocks
);
5753 __cpu_to_le32((__u32
)(array_blocks
>> 32));
5757 sp
= (void **)id
->dev
;
5763 *space_list
= tofree
;
5766 update_reshape_exit
:
5771 static int apply_takeover_update(struct imsm_update_takeover
*u
,
5772 struct intel_super
*super
)
5774 struct imsm_dev
*dev
= NULL
;
5775 struct imsm_map
*map
;
5777 struct intel_dev
*dv
;
5779 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
5780 if (dv
->index
== (unsigned int)u
->subarray
) {
5788 map
= get_imsm_map(dev
, 0);
5790 if (u
->direction
== R10_TO_R0
) {
5791 /* iterate through devices to mark removed disks as spare */
5792 for (dm
= super
->disks
; dm
; dm
= dm
->next
) {
5793 if (dm
->disk
.status
& FAILED_DISK
) {
5794 int idx
= dm
->index
;
5795 /* update indexes on the disk list */
5796 /* FIXME this loop-with-the-loop looks wrong, I'm not convinced
5797 the index values will end up being correct.... NB */
5798 for (du
= super
->disks
; du
; du
= du
->next
)
5799 if (du
->index
> idx
)
5801 /* mark as spare disk */
5802 dm
->disk
.status
= SPARE_DISK
;
5808 map
->num_members
= map
->num_members
/ 2;
5809 map
->map_state
= IMSM_T_STATE_NORMAL
;
5810 map
->num_domains
= 1;
5811 map
->raid_level
= 0;
5812 map
->failed_disk_num
= -1;
5815 /* update disk order table */
5816 for (du
= super
->disks
; du
; du
= du
->next
)
5818 set_imsm_ord_tbl_ent(map
, du
->index
, du
->index
);
5823 static void imsm_process_update(struct supertype
*st
,
5824 struct metadata_update
*update
)
5827 * crack open the metadata_update envelope to find the update record
5828 * update can be one of:
5829 * update_reshape_container_disks - all the arrays in the container
5830 * are being reshaped to have more devices. We need to mark
5831 * the arrays for general migration and convert selected spares
5832 * into active devices.
5833 * update_activate_spare - a spare device has replaced a failed
5834 * device in an array, update the disk_ord_tbl. If this disk is
5835 * present in all member arrays then also clear the SPARE_DISK
5837 * update_create_array
5839 * update_rename_array
5840 * update_add_remove_disk
5842 struct intel_super
*super
= st
->sb
;
5843 struct imsm_super
*mpb
;
5844 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
5846 /* update requires a larger buf but the allocation failed */
5847 if (super
->next_len
&& !super
->next_buf
) {
5848 super
->next_len
= 0;
5852 if (super
->next_buf
) {
5853 memcpy(super
->next_buf
, super
->buf
, super
->len
);
5855 super
->len
= super
->next_len
;
5856 super
->buf
= super
->next_buf
;
5858 super
->next_len
= 0;
5859 super
->next_buf
= NULL
;
5862 mpb
= super
->anchor
;
5865 case update_takeover
: {
5866 struct imsm_update_takeover
*u
= (void *)update
->buf
;
5867 if (apply_takeover_update(u
, super
))
5868 super
->updates_pending
++;
5872 case update_reshape_container_disks
: {
5873 struct imsm_update_reshape
*u
= (void *)update
->buf
;
5874 if (apply_reshape_container_disks_update(
5875 u
, super
, &update
->space_list
))
5876 super
->updates_pending
++;
5879 case update_activate_spare
: {
5880 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
5881 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
5882 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5883 struct imsm_map
*migr_map
;
5884 struct active_array
*a
;
5885 struct imsm_disk
*disk
;
5890 int victim
= get_imsm_disk_idx(dev
, u
->slot
, -1);
5893 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5898 fprintf(stderr
, "error: imsm_activate_spare passed "
5899 "an unknown disk (index: %d)\n",
5904 super
->updates_pending
++;
5906 /* count failures (excluding rebuilds and the victim)
5907 * to determine map[0] state
5910 for (i
= 0; i
< map
->num_members
; i
++) {
5913 disk
= get_imsm_disk(super
,
5914 get_imsm_disk_idx(dev
, i
, -1));
5915 if (!disk
|| is_failed(disk
))
5919 /* adding a pristine spare, assign a new index */
5920 if (dl
->index
< 0) {
5921 dl
->index
= super
->anchor
->num_disks
;
5922 super
->anchor
->num_disks
++;
5925 disk
->status
|= CONFIGURED_DISK
;
5926 disk
->status
&= ~SPARE_DISK
;
5929 to_state
= imsm_check_degraded(super
, dev
, failed
);
5930 map
->map_state
= IMSM_T_STATE_DEGRADED
;
5931 migrate(dev
, to_state
, MIGR_REBUILD
);
5932 migr_map
= get_imsm_map(dev
, 1);
5933 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
5934 set_imsm_ord_tbl_ent(migr_map
, u
->slot
, dl
->index
| IMSM_ORD_REBUILD
);
5936 /* update the family_num to mark a new container
5937 * generation, being careful to record the existing
5938 * family_num in orig_family_num to clean up after
5939 * earlier mdadm versions that neglected to set it.
5941 if (mpb
->orig_family_num
== 0)
5942 mpb
->orig_family_num
= mpb
->family_num
;
5943 mpb
->family_num
+= super
->random
;
5945 /* count arrays using the victim in the metadata */
5947 for (a
= st
->arrays
; a
; a
= a
->next
) {
5948 dev
= get_imsm_dev(super
, a
->info
.container_member
);
5949 map
= get_imsm_map(dev
, 0);
5951 if (get_imsm_disk_slot(map
, victim
) >= 0)
5955 /* delete the victim if it is no longer being
5961 /* We know that 'manager' isn't touching anything,
5962 * so it is safe to delete
5964 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
5965 if ((*dlp
)->index
== victim
)
5968 /* victim may be on the missing list */
5970 for (dlp
= &super
->missing
; *dlp
; dlp
= &(*dlp
)->next
)
5971 if ((*dlp
)->index
== victim
)
5973 imsm_delete(super
, dlp
, victim
);
5977 case update_create_array
: {
5978 /* someone wants to create a new array, we need to be aware of
5979 * a few races/collisions:
5980 * 1/ 'Create' called by two separate instances of mdadm
5981 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
5982 * devices that have since been assimilated via
5984 * In the event this update can not be carried out mdadm will
5985 * (FIX ME) notice that its update did not take hold.
5987 struct imsm_update_create_array
*u
= (void *) update
->buf
;
5988 struct intel_dev
*dv
;
5989 struct imsm_dev
*dev
;
5990 struct imsm_map
*map
, *new_map
;
5991 unsigned long long start
, end
;
5992 unsigned long long new_start
, new_end
;
5994 struct disk_info
*inf
;
5997 /* handle racing creates: first come first serve */
5998 if (u
->dev_idx
< mpb
->num_raid_devs
) {
5999 dprintf("%s: subarray %d already defined\n",
6000 __func__
, u
->dev_idx
);
6004 /* check update is next in sequence */
6005 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
6006 dprintf("%s: can not create array %d expected index %d\n",
6007 __func__
, u
->dev_idx
, mpb
->num_raid_devs
);
6011 new_map
= get_imsm_map(&u
->dev
, 0);
6012 new_start
= __le32_to_cpu(new_map
->pba_of_lba0
);
6013 new_end
= new_start
+ __le32_to_cpu(new_map
->blocks_per_member
);
6014 inf
= get_disk_info(u
);
6016 /* handle activate_spare versus create race:
6017 * check to make sure that overlapping arrays do not include
6020 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6021 dev
= get_imsm_dev(super
, i
);
6022 map
= get_imsm_map(dev
, 0);
6023 start
= __le32_to_cpu(map
->pba_of_lba0
);
6024 end
= start
+ __le32_to_cpu(map
->blocks_per_member
);
6025 if ((new_start
>= start
&& new_start
<= end
) ||
6026 (start
>= new_start
&& start
<= new_end
))
6031 if (disks_overlap(super
, i
, u
)) {
6032 dprintf("%s: arrays overlap\n", __func__
);
6037 /* check that prepare update was successful */
6038 if (!update
->space
) {
6039 dprintf("%s: prepare update failed\n", __func__
);
6043 /* check that all disks are still active before committing
6044 * changes. FIXME: could we instead handle this by creating a
6045 * degraded array? That's probably not what the user expects,
6046 * so better to drop this update on the floor.
6048 for (i
= 0; i
< new_map
->num_members
; i
++) {
6049 dl
= serial_to_dl(inf
[i
].serial
, super
);
6051 dprintf("%s: disk disappeared\n", __func__
);
6056 super
->updates_pending
++;
6058 /* convert spares to members and fixup ord_tbl */
6059 for (i
= 0; i
< new_map
->num_members
; i
++) {
6060 dl
= serial_to_dl(inf
[i
].serial
, super
);
6061 if (dl
->index
== -1) {
6062 dl
->index
= mpb
->num_disks
;
6064 dl
->disk
.status
|= CONFIGURED_DISK
;
6065 dl
->disk
.status
&= ~SPARE_DISK
;
6067 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
6072 update
->space
= NULL
;
6073 imsm_copy_dev(dev
, &u
->dev
);
6074 dv
->index
= u
->dev_idx
;
6075 dv
->next
= super
->devlist
;
6076 super
->devlist
= dv
;
6077 mpb
->num_raid_devs
++;
6079 imsm_update_version_info(super
);
6082 /* mdmon knows how to release update->space, but not
6083 * ((struct intel_dev *) update->space)->dev
6085 if (update
->space
) {
6091 case update_kill_array
: {
6092 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
6093 int victim
= u
->dev_idx
;
6094 struct active_array
*a
;
6095 struct intel_dev
**dp
;
6096 struct imsm_dev
*dev
;
6098 /* sanity check that we are not affecting the uuid of
6099 * active arrays, or deleting an active array
6101 * FIXME when immutable ids are available, but note that
6102 * we'll also need to fixup the invalidated/active
6103 * subarray indexes in mdstat
6105 for (a
= st
->arrays
; a
; a
= a
->next
)
6106 if (a
->info
.container_member
>= victim
)
6108 /* by definition if mdmon is running at least one array
6109 * is active in the container, so checking
6110 * mpb->num_raid_devs is just extra paranoia
6112 dev
= get_imsm_dev(super
, victim
);
6113 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
6114 dprintf("failed to delete subarray-%d\n", victim
);
6118 for (dp
= &super
->devlist
; *dp
;)
6119 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
6122 if ((*dp
)->index
> (unsigned)victim
)
6126 mpb
->num_raid_devs
--;
6127 super
->updates_pending
++;
6130 case update_rename_array
: {
6131 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
6132 char name
[MAX_RAID_SERIAL_LEN
+1];
6133 int target
= u
->dev_idx
;
6134 struct active_array
*a
;
6135 struct imsm_dev
*dev
;
6137 /* sanity check that we are not affecting the uuid of
6140 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
6141 name
[MAX_RAID_SERIAL_LEN
] = '\0';
6142 for (a
= st
->arrays
; a
; a
= a
->next
)
6143 if (a
->info
.container_member
== target
)
6145 dev
= get_imsm_dev(super
, u
->dev_idx
);
6146 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
6147 dprintf("failed to rename subarray-%d\n", target
);
6151 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
6152 super
->updates_pending
++;
6155 case update_add_remove_disk
: {
6156 /* we may be able to repair some arrays if disks are
6157 * being added, check teh status of add_remove_disk
6158 * if discs has been added.
6160 if (add_remove_disk_update(super
)) {
6161 struct active_array
*a
;
6163 super
->updates_pending
++;
6164 for (a
= st
->arrays
; a
; a
= a
->next
)
6165 a
->check_degraded
= 1;
6170 fprintf(stderr
, "error: unsuported process update type:"
6171 "(type: %d)\n", type
);
6175 static void imsm_prepare_update(struct supertype
*st
,
6176 struct metadata_update
*update
)
6179 * Allocate space to hold new disk entries, raid-device entries or a new
6180 * mpb if necessary. The manager synchronously waits for updates to
6181 * complete in the monitor, so new mpb buffers allocated here can be
6182 * integrated by the monitor thread without worrying about live pointers
6183 * in the manager thread.
6185 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
6186 struct intel_super
*super
= st
->sb
;
6187 struct imsm_super
*mpb
= super
->anchor
;
6192 case update_reshape_container_disks
: {
6193 /* Every raid device in the container is about to
6194 * gain some more devices, and we will enter a
6196 * So each 'imsm_map' will be bigger, and the imsm_vol
6197 * will now hold 2 of them.
6198 * Thus we need new 'struct imsm_dev' allocations sized
6199 * as sizeof_imsm_dev but with more devices in both maps.
6201 struct imsm_update_reshape
*u
= (void *)update
->buf
;
6202 struct intel_dev
*dl
;
6203 void **space_tail
= (void**)&update
->space_list
;
6205 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
6207 for (dl
= super
->devlist
; dl
; dl
= dl
->next
) {
6208 int size
= sizeof_imsm_dev(dl
->dev
, 1);
6210 if (u
->new_raid_disks
> u
->old_raid_disks
)
6211 size
+= sizeof(__u32
)*2*
6212 (u
->new_raid_disks
- u
->old_raid_disks
);
6221 len
= disks_to_mpb_size(u
->new_raid_disks
);
6222 dprintf("New anchor length is %llu\n", (unsigned long long)len
);
6225 case update_create_array
: {
6226 struct imsm_update_create_array
*u
= (void *) update
->buf
;
6227 struct intel_dev
*dv
;
6228 struct imsm_dev
*dev
= &u
->dev
;
6229 struct imsm_map
*map
= get_imsm_map(dev
, 0);
6231 struct disk_info
*inf
;
6235 inf
= get_disk_info(u
);
6236 len
= sizeof_imsm_dev(dev
, 1);
6237 /* allocate a new super->devlist entry */
6238 dv
= malloc(sizeof(*dv
));
6240 dv
->dev
= malloc(len
);
6245 update
->space
= NULL
;
6249 /* count how many spares will be converted to members */
6250 for (i
= 0; i
< map
->num_members
; i
++) {
6251 dl
= serial_to_dl(inf
[i
].serial
, super
);
6253 /* hmm maybe it failed?, nothing we can do about
6258 if (count_memberships(dl
, super
) == 0)
6261 len
+= activate
* sizeof(struct imsm_disk
);
6268 /* check if we need a larger metadata buffer */
6269 if (super
->next_buf
)
6270 buf_len
= super
->next_len
;
6272 buf_len
= super
->len
;
6274 if (__le32_to_cpu(mpb
->mpb_size
) + len
> buf_len
) {
6275 /* ok we need a larger buf than what is currently allocated
6276 * if this allocation fails process_update will notice that
6277 * ->next_len is set and ->next_buf is NULL
6279 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + len
, 512);
6280 if (super
->next_buf
)
6281 free(super
->next_buf
);
6283 super
->next_len
= buf_len
;
6284 if (posix_memalign(&super
->next_buf
, 512, buf_len
) == 0)
6285 memset(super
->next_buf
, 0, buf_len
);
6287 super
->next_buf
= NULL
;
6291 /* must be called while manager is quiesced */
6292 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
6294 struct imsm_super
*mpb
= super
->anchor
;
6296 struct imsm_dev
*dev
;
6297 struct imsm_map
*map
;
6298 int i
, j
, num_members
;
6301 dprintf("%s: deleting device[%d] from imsm_super\n",
6304 /* shift all indexes down one */
6305 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
6306 if (iter
->index
> (int)index
)
6308 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
6309 if (iter
->index
> (int)index
)
6312 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6313 dev
= get_imsm_dev(super
, i
);
6314 map
= get_imsm_map(dev
, 0);
6315 num_members
= map
->num_members
;
6316 for (j
= 0; j
< num_members
; j
++) {
6317 /* update ord entries being careful not to propagate
6318 * ord-flags to the first map
6320 ord
= get_imsm_ord_tbl_ent(dev
, j
, -1);
6322 if (ord_to_idx(ord
) <= index
)
6325 map
= get_imsm_map(dev
, 0);
6326 set_imsm_ord_tbl_ent(map
, j
, ord_to_idx(ord
- 1));
6327 map
= get_imsm_map(dev
, 1);
6329 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
6334 super
->updates_pending
++;
6336 struct dl
*dl
= *dlp
;
6338 *dlp
= (*dlp
)->next
;
6339 __free_imsm_disk(dl
);
6342 #endif /* MDASSEMBLE */
6344 static char disk_by_path
[] = "/dev/disk/by-path/";
6346 static const char *imsm_get_disk_controller_domain(const char *path
)
6348 struct sys_dev
*list
, *hba
= NULL
;
6349 char disk_path
[PATH_MAX
];
6353 list
= find_driver_devices("pci", "ahci");
6354 for (hba
= list
; hba
; hba
= hba
->next
)
6355 if (devpath_to_vendor(hba
->path
) == 0x8086)
6361 strncpy(disk_path
, disk_by_path
, PATH_MAX
- 1);
6362 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
6363 if (stat(disk_path
, &st
) == 0) {
6364 dpath
= devt_to_devpath(st
.st_rdev
);
6366 ahci
= path_attached_to_hba(dpath
, hba
->path
);
6369 dprintf("path: %s(%s) hba: %s attached: %d\n",
6370 path
, dpath
, (hba
) ? hba
->path
: "NULL", ahci
);
6371 free_sys_dev(&list
);
6378 static int imsm_find_array_minor_by_subdev(int subdev
, int container
, int *minor
)
6380 char subdev_name
[20];
6381 struct mdstat_ent
*mdstat
;
6383 sprintf(subdev_name
, "%d", subdev
);
6384 mdstat
= mdstat_by_subdev(subdev_name
, container
);
6388 *minor
= mdstat
->devnum
;
6389 free_mdstat(mdstat
);
6393 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
6394 struct geo_params
*geo
,
6395 int *old_raid_disks
)
6397 /* currently we only support increasing the number of devices
6398 * for a container. This increases the number of device for each
6399 * member array. They must all be RAID0 or RAID5.
6402 struct mdinfo
*info
, *member
;
6403 int devices_that_can_grow
= 0;
6405 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): "
6406 "st->devnum = (%i)\n",
6409 if (geo
->size
!= -1 ||
6410 geo
->level
!= UnSet
||
6411 geo
->layout
!= UnSet
||
6412 geo
->chunksize
!= 0 ||
6413 geo
->raid_disks
== UnSet
) {
6414 dprintf("imsm: Container operation is allowed for "
6415 "raid disks number change only.\n");
6419 info
= container_content_imsm(st
, NULL
);
6420 for (member
= info
; member
; member
= member
->next
) {
6424 dprintf("imsm: checking device_num: %i\n",
6425 member
->container_member
);
6427 if (geo
->raid_disks
< member
->array
.raid_disks
) {
6428 /* we work on container for Online Capacity Expansion
6429 * only so raid_disks has to grow
6431 dprintf("imsm: for container operation raid disks "
6432 "increase is required\n");
6436 if ((info
->array
.level
!= 0) &&
6437 (info
->array
.level
!= 5)) {
6438 /* we cannot use this container with other raid level
6440 dprintf("imsm: for container operation wrong"
6441 " raid level (%i) detected\n",
6445 /* check for platform support
6446 * for this raid level configuration
6448 struct intel_super
*super
= st
->sb
;
6449 if (!is_raid_level_supported(super
->orom
,
6450 member
->array
.level
,
6452 dprintf("platform does not support raid%d with"
6456 geo
->raid_disks
> 1 ? "s" : "");
6461 if (*old_raid_disks
&&
6462 info
->array
.raid_disks
!= *old_raid_disks
)
6464 *old_raid_disks
= info
->array
.raid_disks
;
6466 /* All raid5 and raid0 volumes in container
6467 * have to be ready for Online Capacity Expansion
6468 * so they need to be assembled. We have already
6469 * checked that no recovery etc is happening.
6471 result
= imsm_find_array_minor_by_subdev(member
->container_member
,
6475 dprintf("imsm: cannot find array\n");
6478 devices_that_can_grow
++;
6481 if (!member
&& devices_that_can_grow
)
6485 dprintf("\tContainer operation allowed\n");
6487 dprintf("\tError: %i\n", ret_val
);
6492 /* Function: get_spares_for_grow
6493 * Description: Allocates memory and creates list of spare devices
6494 * avaliable in container. Checks if spare drive size is acceptable.
6495 * Parameters: Pointer to the supertype structure
6496 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
6499 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
6501 unsigned long long min_size
= min_acceptable_spare_size_imsm(st
);
6502 return container_choose_spares(st
, min_size
, NULL
, NULL
, NULL
, 0);
6505 /******************************************************************************
6506 * function: imsm_create_metadata_update_for_reshape
6507 * Function creates update for whole IMSM container.
6509 ******************************************************************************/
6510 static int imsm_create_metadata_update_for_reshape(
6511 struct supertype
*st
,
6512 struct geo_params
*geo
,
6514 struct imsm_update_reshape
**updatep
)
6516 struct intel_super
*super
= st
->sb
;
6517 struct imsm_super
*mpb
= super
->anchor
;
6518 int update_memory_size
= 0;
6519 struct imsm_update_reshape
*u
= NULL
;
6520 struct mdinfo
*spares
= NULL
;
6522 int delta_disks
= 0;
6525 dprintf("imsm_update_metadata_for_reshape(enter) raid_disks = %i\n",
6528 delta_disks
= geo
->raid_disks
- old_raid_disks
;
6530 /* size of all update data without anchor */
6531 update_memory_size
= sizeof(struct imsm_update_reshape
);
6533 /* now add space for spare disks that we need to add. */
6534 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
6536 u
= calloc(1, update_memory_size
);
6539 "cannot get memory for imsm_update_reshape update\n");
6542 u
->type
= update_reshape_container_disks
;
6543 u
->old_raid_disks
= old_raid_disks
;
6544 u
->new_raid_disks
= geo
->raid_disks
;
6546 /* now get spare disks list
6548 spares
= get_spares_for_grow(st
);
6551 || delta_disks
> spares
->array
.spare_disks
) {
6552 dprintf("imsm: ERROR: Cannot get spare devices.\n");
6556 /* we have got spares
6557 * update disk list in imsm_disk list table in anchor
6559 dprintf("imsm: %i spares are available.\n\n",
6560 spares
->array
.spare_disks
);
6563 for (i
= 0; i
< delta_disks
; i
++) {
6568 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
6570 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
6571 dl
->index
= mpb
->num_disks
;
6581 dprintf("imsm: reshape update preparation :");
6582 if (i
== delta_disks
) {
6585 return update_memory_size
;
6588 dprintf(" Error\n");
6593 static void imsm_update_metadata_locally(struct supertype
*st
,
6596 struct metadata_update mu
;
6601 mu
.space_list
= NULL
;
6603 imsm_prepare_update(st
, &mu
);
6604 imsm_process_update(st
, &mu
);
6606 while (mu
.space_list
) {
6607 void **space
= mu
.space_list
;
6608 mu
.space_list
= *space
;
6613 /***************************************************************************
6614 * Function: imsm_analyze_change
6615 * Description: Function analyze change for single volume
6616 * and validate if transition is supported
6617 * Parameters: Geometry parameters, supertype structure
6618 * Returns: Operation type code on success, -1 if fail
6619 ****************************************************************************/
6620 enum imsm_reshape_type
imsm_analyze_change(struct supertype
*st
,
6621 struct geo_params
*geo
)
6627 getinfo_super_imsm_volume(st
, &info
, NULL
);
6629 if ((geo
->level
!= info
.array
.level
) &&
6630 (geo
->level
>= 0) &&
6631 (geo
->level
!= UnSet
)) {
6632 switch (info
.array
.level
) {
6634 if (geo
->level
== 5) {
6635 change
= CH_LEVEL_MIGRATION
;
6638 if (geo
->level
== 10) {
6639 change
= CH_TAKEOVER
;
6644 if (geo
->level
!= 0)
6645 change
= CH_LEVEL_MIGRATION
;
6648 if (geo
->level
== 0) {
6649 change
= CH_TAKEOVER
;
6656 Name
" Error. Level Migration from %d to %d "
6658 info
.array
.level
, geo
->level
);
6659 goto analyse_change_exit
;
6662 geo
->level
= info
.array
.level
;
6664 if ((geo
->layout
!= info
.array
.layout
)
6665 && ((geo
->layout
!= UnSet
) && (geo
->layout
!= -1))) {
6666 change
= CH_LEVEL_MIGRATION
;
6667 if ((info
.array
.layout
== 0)
6668 && (info
.array
.level
== 5)
6669 && (geo
->layout
== 5)) {
6670 /* reshape 5 -> 4 */
6671 } else if ((info
.array
.layout
== 5)
6672 && (info
.array
.level
== 5)
6673 && (geo
->layout
== 0)) {
6674 /* reshape 4 -> 5 */
6679 Name
" Error. Layout Migration from %d to %d "
6681 info
.array
.layout
, geo
->layout
);
6683 goto analyse_change_exit
;
6686 geo
->layout
= info
.array
.layout
;
6688 if ((geo
->chunksize
> 0) && (geo
->chunksize
!= UnSet
)
6689 && (geo
->chunksize
!= info
.array
.chunk_size
))
6690 change
= CH_CHUNK_MIGR
;
6692 geo
->chunksize
= info
.array
.chunk_size
;
6694 if (!validate_geometry_imsm(st
,
6698 (geo
->chunksize
/ 1024),
6704 struct intel_super
*super
= st
->sb
;
6705 struct imsm_super
*mpb
= super
->anchor
;
6707 if (mpb
->num_raid_devs
> 1) {
6709 Name
" Error. Cannot perform operation on %s"
6710 "- for this operation it MUST be single "
6711 "array in container\n",
6717 analyse_change_exit
:
6722 int imsm_takeover(struct supertype
*st
, struct geo_params
*geo
)
6724 struct intel_super
*super
= st
->sb
;
6725 struct imsm_update_takeover
*u
;
6727 u
= malloc(sizeof(struct imsm_update_takeover
));
6731 u
->type
= update_takeover
;
6732 u
->subarray
= super
->current_vol
;
6734 /* 10->0 transition */
6735 if (geo
->level
== 0)
6736 u
->direction
= R10_TO_R0
;
6738 /* 0->10 transition */
6739 if (geo
->level
== 10)
6740 u
->direction
= R0_TO_R10
;
6742 /* update metadata locally */
6743 imsm_update_metadata_locally(st
, u
,
6744 sizeof(struct imsm_update_takeover
));
6745 /* and possibly remotely */
6746 if (st
->update_tail
)
6747 append_metadata_update(st
, u
,
6748 sizeof(struct imsm_update_takeover
));
6755 static int imsm_reshape_super(struct supertype
*st
, long long size
, int level
,
6756 int layout
, int chunksize
, int raid_disks
,
6757 char *backup
, char *dev
, int verbose
)
6760 struct geo_params geo
;
6762 dprintf("imsm: reshape_super called.\n");
6764 memset(&geo
, sizeof(struct geo_params
), 0);
6767 geo
.dev_id
= st
->devnum
;
6770 geo
.layout
= layout
;
6771 geo
.chunksize
= chunksize
;
6772 geo
.raid_disks
= raid_disks
;
6774 dprintf("\tfor level : %i\n", geo
.level
);
6775 dprintf("\tfor raid_disks : %i\n", geo
.raid_disks
);
6777 if (experimental() == 0)
6780 if (st
->container_dev
== st
->devnum
) {
6781 /* On container level we can only increase number of devices. */
6782 dprintf("imsm: info: Container operation\n");
6783 int old_raid_disks
= 0;
6784 if (imsm_reshape_is_allowed_on_container(
6785 st
, &geo
, &old_raid_disks
)) {
6786 struct imsm_update_reshape
*u
= NULL
;
6789 len
= imsm_create_metadata_update_for_reshape(
6790 st
, &geo
, old_raid_disks
, &u
);
6793 dprintf("imsm: Cannot prepare update\n");
6794 goto exit_imsm_reshape_super
;
6798 /* update metadata locally */
6799 imsm_update_metadata_locally(st
, u
, len
);
6800 /* and possibly remotely */
6801 if (st
->update_tail
)
6802 append_metadata_update(st
, u
, len
);
6807 fprintf(stderr
, Name
"imsm: Operation is not allowed "
6808 "on this container\n");
6811 /* On volume level we support following operations
6812 * - takeover: raid10 -> raid0; raid0 -> raid10
6813 * - chunk size migration
6814 * - migration: raid5 -> raid0; raid0 -> raid5
6816 struct intel_super
*super
= st
->sb
;
6817 struct intel_dev
*dev
= super
->devlist
;
6819 dprintf("imsm: info: Volume operation\n");
6820 /* find requested device */
6822 imsm_find_array_minor_by_subdev(dev
->index
, st
->container_dev
, &devnum
);
6823 if (devnum
== geo
.dev_id
)
6828 fprintf(stderr
, Name
" Cannot find %s (%i) subarray\n",
6829 geo
.dev_name
, geo
.dev_id
);
6830 goto exit_imsm_reshape_super
;
6832 super
->current_vol
= dev
->index
;
6833 change
= imsm_analyze_change(st
, &geo
);
6836 ret_val
= imsm_takeover(st
, &geo
);
6841 case CH_LEVEL_MIGRATION
:
6849 exit_imsm_reshape_super
:
6850 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
6854 static int imsm_manage_reshape(
6855 int afd
, struct mdinfo
*sra
, struct reshape
*reshape
,
6856 struct supertype
*st
, unsigned long stripes
,
6857 int *fds
, unsigned long long *offsets
,
6858 int dests
, int *destfd
, unsigned long long *destoffsets
)
6860 /* Just use child_monitor for now */
6861 return child_monitor(
6862 afd
, sra
, reshape
, st
, stripes
,
6863 fds
, offsets
, dests
, destfd
, destoffsets
);
6866 struct superswitch super_imsm
= {
6868 .examine_super
= examine_super_imsm
,
6869 .brief_examine_super
= brief_examine_super_imsm
,
6870 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
6871 .export_examine_super
= export_examine_super_imsm
,
6872 .detail_super
= detail_super_imsm
,
6873 .brief_detail_super
= brief_detail_super_imsm
,
6874 .write_init_super
= write_init_super_imsm
,
6875 .validate_geometry
= validate_geometry_imsm
,
6876 .add_to_super
= add_to_super_imsm
,
6877 .remove_from_super
= remove_from_super_imsm
,
6878 .detail_platform
= detail_platform_imsm
,
6879 .kill_subarray
= kill_subarray_imsm
,
6880 .update_subarray
= update_subarray_imsm
,
6881 .load_container
= load_container_imsm
,
6883 .match_home
= match_home_imsm
,
6884 .uuid_from_super
= uuid_from_super_imsm
,
6885 .getinfo_super
= getinfo_super_imsm
,
6886 .getinfo_super_disks
= getinfo_super_disks_imsm
,
6887 .update_super
= update_super_imsm
,
6889 .avail_size
= avail_size_imsm
,
6890 .min_acceptable_spare_size
= min_acceptable_spare_size_imsm
,
6892 .compare_super
= compare_super_imsm
,
6894 .load_super
= load_super_imsm
,
6895 .init_super
= init_super_imsm
,
6896 .store_super
= store_super_imsm
,
6897 .free_super
= free_super_imsm
,
6898 .match_metadata_desc
= match_metadata_desc_imsm
,
6899 .container_content
= container_content_imsm
,
6900 .default_geometry
= default_geometry_imsm
,
6901 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
6902 .reshape_super
= imsm_reshape_super
,
6903 .manage_reshape
= imsm_manage_reshape
,
6910 .open_new
= imsm_open_new
,
6911 .set_array_state
= imsm_set_array_state
,
6912 .set_disk
= imsm_set_disk
,
6913 .sync_metadata
= imsm_sync_metadata
,
6914 .activate_spare
= imsm_activate_spare
,
6915 .process_update
= imsm_process_update
,
6916 .prepare_update
= imsm_prepare_update
,
6917 #endif /* MDASSEMBLE */