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 /* definition of messages passed to imsm_process_update */
288 enum imsm_update_type
{
289 update_activate_spare
,
293 update_add_remove_disk
,
294 update_reshape_container_disks
,
297 struct imsm_update_activate_spare
{
298 enum imsm_update_type type
;
302 struct imsm_update_activate_spare
*next
;
316 struct imsm_update_reshape
{
317 enum imsm_update_type type
;
320 int new_disks
[1]; /* new_raid_disk - old_raid_disks makedev number */
324 __u8 serial
[MAX_RAID_SERIAL_LEN
];
327 struct imsm_update_create_array
{
328 enum imsm_update_type type
;
333 struct imsm_update_kill_array
{
334 enum imsm_update_type type
;
338 struct imsm_update_rename_array
{
339 enum imsm_update_type type
;
340 __u8 name
[MAX_RAID_SERIAL_LEN
];
344 struct imsm_update_add_remove_disk
{
345 enum imsm_update_type type
;
348 static struct supertype
*match_metadata_desc_imsm(char *arg
)
350 struct supertype
*st
;
352 if (strcmp(arg
, "imsm") != 0 &&
353 strcmp(arg
, "default") != 0
357 st
= malloc(sizeof(*st
));
360 memset(st
, 0, sizeof(*st
));
361 st
->container_dev
= NoMdDev
;
362 st
->ss
= &super_imsm
;
363 st
->max_devs
= IMSM_MAX_DEVICES
;
364 st
->minor_version
= 0;
370 static __u8
*get_imsm_version(struct imsm_super
*mpb
)
372 return &mpb
->sig
[MPB_SIG_LEN
];
376 /* retrieve a disk directly from the anchor when the anchor is known to be
377 * up-to-date, currently only at load time
379 static struct imsm_disk
*__get_imsm_disk(struct imsm_super
*mpb
, __u8 index
)
381 if (index
>= mpb
->num_disks
)
383 return &mpb
->disk
[index
];
386 /* retrieve the disk description based on a index of the disk
389 static struct dl
*get_imsm_dl_disk(struct intel_super
*super
, __u8 index
)
393 for (d
= super
->disks
; d
; d
= d
->next
)
394 if (d
->index
== index
)
399 /* retrieve a disk from the parsed metadata */
400 static struct imsm_disk
*get_imsm_disk(struct intel_super
*super
, __u8 index
)
404 dl
= get_imsm_dl_disk(super
, index
);
411 /* generate a checksum directly from the anchor when the anchor is known to be
412 * up-to-date, currently only at load or write_super after coalescing
414 static __u32
__gen_imsm_checksum(struct imsm_super
*mpb
)
416 __u32 end
= mpb
->mpb_size
/ sizeof(end
);
417 __u32
*p
= (__u32
*) mpb
;
421 sum
+= __le32_to_cpu(*p
);
425 return sum
- __le32_to_cpu(mpb
->check_sum
);
428 static size_t sizeof_imsm_map(struct imsm_map
*map
)
430 return sizeof(struct imsm_map
) + sizeof(__u32
) * (map
->num_members
- 1);
433 struct imsm_map
*get_imsm_map(struct imsm_dev
*dev
, int second_map
)
435 struct imsm_map
*map
= &dev
->vol
.map
[0];
437 if (second_map
&& !dev
->vol
.migr_state
)
439 else if (second_map
) {
442 return ptr
+ sizeof_imsm_map(map
);
448 /* return the size of the device.
449 * migr_state increases the returned size if map[0] were to be duplicated
451 static size_t sizeof_imsm_dev(struct imsm_dev
*dev
, int migr_state
)
453 size_t size
= sizeof(*dev
) - sizeof(struct imsm_map
) +
454 sizeof_imsm_map(get_imsm_map(dev
, 0));
456 /* migrating means an additional map */
457 if (dev
->vol
.migr_state
)
458 size
+= sizeof_imsm_map(get_imsm_map(dev
, 1));
460 size
+= sizeof_imsm_map(get_imsm_map(dev
, 0));
466 /* retrieve disk serial number list from a metadata update */
467 static struct disk_info
*get_disk_info(struct imsm_update_create_array
*update
)
470 struct disk_info
*inf
;
472 inf
= u
+ sizeof(*update
) - sizeof(struct imsm_dev
) +
473 sizeof_imsm_dev(&update
->dev
, 0);
479 static struct imsm_dev
*__get_imsm_dev(struct imsm_super
*mpb
, __u8 index
)
485 if (index
>= mpb
->num_raid_devs
)
488 /* devices start after all disks */
489 offset
= ((void *) &mpb
->disk
[mpb
->num_disks
]) - _mpb
;
491 for (i
= 0; i
<= index
; i
++)
493 return _mpb
+ offset
;
495 offset
+= sizeof_imsm_dev(_mpb
+ offset
, 0);
500 static struct imsm_dev
*get_imsm_dev(struct intel_super
*super
, __u8 index
)
502 struct intel_dev
*dv
;
504 if (index
>= super
->anchor
->num_raid_devs
)
506 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
507 if (dv
->index
== index
)
512 static __u32
get_imsm_ord_tbl_ent(struct imsm_dev
*dev
, int slot
)
514 struct imsm_map
*map
;
516 if (dev
->vol
.migr_state
)
517 map
= get_imsm_map(dev
, 1);
519 map
= get_imsm_map(dev
, 0);
521 /* top byte identifies disk under rebuild */
522 return __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
525 #define ord_to_idx(ord) (((ord) << 8) >> 8)
526 static __u32
get_imsm_disk_idx(struct imsm_dev
*dev
, int slot
)
528 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
);
530 return ord_to_idx(ord
);
533 static void set_imsm_ord_tbl_ent(struct imsm_map
*map
, int slot
, __u32 ord
)
535 map
->disk_ord_tbl
[slot
] = __cpu_to_le32(ord
);
538 static int get_imsm_disk_slot(struct imsm_map
*map
, unsigned idx
)
543 for (slot
= 0; slot
< map
->num_members
; slot
++) {
544 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
545 if (ord_to_idx(ord
) == idx
)
552 static int get_imsm_raid_level(struct imsm_map
*map
)
554 if (map
->raid_level
== 1) {
555 if (map
->num_members
== 2)
561 return map
->raid_level
;
564 static int cmp_extent(const void *av
, const void *bv
)
566 const struct extent
*a
= av
;
567 const struct extent
*b
= bv
;
568 if (a
->start
< b
->start
)
570 if (a
->start
> b
->start
)
575 static int count_memberships(struct dl
*dl
, struct intel_super
*super
)
580 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
581 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
582 struct imsm_map
*map
= get_imsm_map(dev
, 0);
584 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
591 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
)
593 /* find a list of used extents on the given physical device */
594 struct extent
*rv
, *e
;
596 int memberships
= count_memberships(dl
, super
);
597 __u32 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
599 rv
= malloc(sizeof(struct extent
) * (memberships
+ 1));
604 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
605 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
606 struct imsm_map
*map
= get_imsm_map(dev
, 0);
608 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
609 e
->start
= __le32_to_cpu(map
->pba_of_lba0
);
610 e
->size
= __le32_to_cpu(map
->blocks_per_member
);
614 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
616 /* determine the start of the metadata
617 * when no raid devices are defined use the default
618 * ...otherwise allow the metadata to truncate the value
619 * as is the case with older versions of imsm
622 struct extent
*last
= &rv
[memberships
- 1];
625 remainder
= __le32_to_cpu(dl
->disk
.total_blocks
) -
626 (last
->start
+ last
->size
);
627 /* round down to 1k block to satisfy precision of the kernel
631 /* make sure remainder is still sane */
632 if (remainder
< (unsigned)ROUND_UP(super
->len
, 512) >> 9)
633 remainder
= ROUND_UP(super
->len
, 512) >> 9;
634 if (reservation
> remainder
)
635 reservation
= remainder
;
637 e
->start
= __le32_to_cpu(dl
->disk
.total_blocks
) - reservation
;
642 /* try to determine how much space is reserved for metadata from
643 * the last get_extents() entry, otherwise fallback to the
646 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
652 /* for spares just return a minimal reservation which will grow
653 * once the spare is picked up by an array
656 return MPB_SECTOR_CNT
;
658 e
= get_extents(super
, dl
);
660 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
662 /* scroll to last entry */
663 for (i
= 0; e
[i
].size
; i
++)
666 rv
= __le32_to_cpu(dl
->disk
.total_blocks
) - e
[i
].start
;
673 static int is_spare(struct imsm_disk
*disk
)
675 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
678 static int is_configured(struct imsm_disk
*disk
)
680 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
683 static int is_failed(struct imsm_disk
*disk
)
685 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
688 /* Return minimum size of a spare that can be used in this array*/
689 static unsigned long long min_acceptable_spare_size_imsm(struct supertype
*st
)
691 struct intel_super
*super
= st
->sb
;
695 unsigned long long rv
= 0;
699 /* find first active disk in array */
701 while (dl
&& (is_failed(&dl
->disk
) || dl
->index
== -1))
705 /* find last lba used by subarrays */
706 e
= get_extents(super
, dl
);
709 for (i
= 0; e
[i
].size
; i
++)
712 rv
= e
[i
-1].start
+ e
[i
-1].size
;
714 /* add the amount of space needed for metadata */
715 rv
= rv
+ MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
720 static __u64
blocks_per_migr_unit(struct imsm_dev
*dev
);
722 static void print_imsm_dev(struct imsm_dev
*dev
, char *uuid
, int disk_idx
)
726 struct imsm_map
*map
= get_imsm_map(dev
, 0);
730 printf("[%.16s]:\n", dev
->volume
);
731 printf(" UUID : %s\n", uuid
);
732 printf(" RAID Level : %d\n", get_imsm_raid_level(map
));
733 printf(" Members : %d\n", map
->num_members
);
734 printf(" Slots : [");
735 for (i
= 0; i
< map
->num_members
; i
++) {
736 ord
= get_imsm_ord_tbl_ent(dev
, i
);
737 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
740 slot
= get_imsm_disk_slot(map
, disk_idx
);
742 ord
= get_imsm_ord_tbl_ent(dev
, slot
);
743 printf(" This Slot : %d%s\n", slot
,
744 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
746 printf(" This Slot : ?\n");
747 sz
= __le32_to_cpu(dev
->size_high
);
749 sz
+= __le32_to_cpu(dev
->size_low
);
750 printf(" Array Size : %llu%s\n", (unsigned long long)sz
,
751 human_size(sz
* 512));
752 sz
= __le32_to_cpu(map
->blocks_per_member
);
753 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz
,
754 human_size(sz
* 512));
755 printf(" Sector Offset : %u\n",
756 __le32_to_cpu(map
->pba_of_lba0
));
757 printf(" Num Stripes : %u\n",
758 __le32_to_cpu(map
->num_data_stripes
));
759 printf(" Chunk Size : %u KiB\n",
760 __le16_to_cpu(map
->blocks_per_strip
) / 2);
761 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
762 printf(" Migrate State : ");
763 if (dev
->vol
.migr_state
) {
764 if (migr_type(dev
) == MIGR_INIT
)
765 printf("initialize\n");
766 else if (migr_type(dev
) == MIGR_REBUILD
)
768 else if (migr_type(dev
) == MIGR_VERIFY
)
770 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
771 printf("general migration\n");
772 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
773 printf("state change\n");
774 else if (migr_type(dev
) == MIGR_REPAIR
)
777 printf("<unknown:%d>\n", migr_type(dev
));
780 printf(" Map State : %s", map_state_str
[map
->map_state
]);
781 if (dev
->vol
.migr_state
) {
782 struct imsm_map
*map
= get_imsm_map(dev
, 1);
784 printf(" <-- %s", map_state_str
[map
->map_state
]);
785 printf("\n Checkpoint : %u (%llu)",
786 __le32_to_cpu(dev
->vol
.curr_migr_unit
),
787 (unsigned long long)blocks_per_migr_unit(dev
));
790 printf(" Dirty State : %s\n", dev
->vol
.dirty
? "dirty" : "clean");
793 static void print_imsm_disk(struct imsm_super
*mpb
, int index
, __u32 reserved
)
795 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, index
);
796 char str
[MAX_RAID_SERIAL_LEN
+ 1];
799 if (index
< 0 || !disk
)
803 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
804 printf(" Disk%02d Serial : %s\n", index
, str
);
805 printf(" State :%s%s%s\n", is_spare(disk
) ? " spare" : "",
806 is_configured(disk
) ? " active" : "",
807 is_failed(disk
) ? " failed" : "");
808 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
809 sz
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
810 printf(" Usable Size : %llu%s\n", (unsigned long long)sz
,
811 human_size(sz
* 512));
814 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
816 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
818 struct intel_super
*super
= st
->sb
;
819 struct imsm_super
*mpb
= super
->anchor
;
820 char str
[MAX_SIGNATURE_LENGTH
];
825 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
828 snprintf(str
, MPB_SIG_LEN
, "%s", mpb
->sig
);
829 printf(" Magic : %s\n", str
);
830 snprintf(str
, strlen(MPB_VERSION_RAID0
), "%s", get_imsm_version(mpb
));
831 printf(" Version : %s\n", get_imsm_version(mpb
));
832 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
833 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
834 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
835 getinfo_super_imsm(st
, &info
, NULL
);
836 fname_from_uuid(st
, &info
, nbuf
, ':');
837 printf(" UUID : %s\n", nbuf
+ 5);
838 sum
= __le32_to_cpu(mpb
->check_sum
);
839 printf(" Checksum : %08x %s\n", sum
,
840 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
841 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
));
842 printf(" Disks : %d\n", mpb
->num_disks
);
843 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
844 print_imsm_disk(mpb
, super
->disks
->index
, reserved
);
845 if (super
->bbm_log
) {
846 struct bbm_log
*log
= super
->bbm_log
;
849 printf("Bad Block Management Log:\n");
850 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
851 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
852 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
853 printf(" Spare Blocks : %d\n", __le32_to_cpu(log
->reserved_spare_block_count
));
854 printf(" First Spare : %llx\n",
855 (unsigned long long) __le64_to_cpu(log
->first_spare_lba
));
857 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
859 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
861 super
->current_vol
= i
;
862 getinfo_super_imsm(st
, &info
, NULL
);
863 fname_from_uuid(st
, &info
, nbuf
, ':');
864 print_imsm_dev(dev
, nbuf
+ 5, super
->disks
->index
);
866 for (i
= 0; i
< mpb
->num_disks
; i
++) {
867 if (i
== super
->disks
->index
)
869 print_imsm_disk(mpb
, i
, reserved
);
871 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
872 struct imsm_disk
*disk
;
873 char str
[MAX_RAID_SERIAL_LEN
+ 1];
881 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
882 printf(" Disk Serial : %s\n", str
);
883 printf(" State :%s%s%s\n", is_spare(disk
) ? " spare" : "",
884 is_configured(disk
) ? " active" : "",
885 is_failed(disk
) ? " failed" : "");
886 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
887 sz
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
888 printf(" Usable Size : %llu%s\n", (unsigned long long)sz
,
889 human_size(sz
* 512));
893 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
895 /* We just write a generic IMSM ARRAY entry */
898 struct intel_super
*super
= st
->sb
;
900 if (!super
->anchor
->num_raid_devs
) {
901 printf("ARRAY metadata=imsm\n");
905 getinfo_super_imsm(st
, &info
, NULL
);
906 fname_from_uuid(st
, &info
, nbuf
, ':');
907 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
910 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
912 /* We just write a generic IMSM ARRAY entry */
916 struct intel_super
*super
= st
->sb
;
919 if (!super
->anchor
->num_raid_devs
)
922 getinfo_super_imsm(st
, &info
, NULL
);
923 fname_from_uuid(st
, &info
, nbuf
, ':');
924 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
925 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
927 super
->current_vol
= i
;
928 getinfo_super_imsm(st
, &info
, NULL
);
929 fname_from_uuid(st
, &info
, nbuf1
, ':');
930 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
931 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
935 static void export_examine_super_imsm(struct supertype
*st
)
937 struct intel_super
*super
= st
->sb
;
938 struct imsm_super
*mpb
= super
->anchor
;
942 getinfo_super_imsm(st
, &info
, NULL
);
943 fname_from_uuid(st
, &info
, nbuf
, ':');
944 printf("MD_METADATA=imsm\n");
945 printf("MD_LEVEL=container\n");
946 printf("MD_UUID=%s\n", nbuf
+5);
947 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
950 static void detail_super_imsm(struct supertype
*st
, char *homehost
)
955 getinfo_super_imsm(st
, &info
, NULL
);
956 fname_from_uuid(st
, &info
, nbuf
, ':');
957 printf("\n UUID : %s\n", nbuf
+ 5);
960 static void brief_detail_super_imsm(struct supertype
*st
)
964 getinfo_super_imsm(st
, &info
, NULL
);
965 fname_from_uuid(st
, &info
, nbuf
, ':');
966 printf(" UUID=%s", nbuf
+ 5);
969 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
);
970 static void fd2devname(int fd
, char *name
);
972 static int imsm_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
974 /* dump an unsorted list of devices attached to ahci, as well as
975 * non-connected ports
977 int hba_len
= strlen(hba_path
) + 1;
982 unsigned long port_mask
= (1 << port_count
) - 1;
984 if (port_count
> (int)sizeof(port_mask
) * 8) {
986 fprintf(stderr
, Name
": port_count %d out of range\n", port_count
);
990 /* scroll through /sys/dev/block looking for devices attached to
993 dir
= opendir("/sys/dev/block");
994 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
1005 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
1007 path
= devt_to_devpath(makedev(major
, minor
));
1010 if (!path_attached_to_hba(path
, hba_path
)) {
1016 /* retrieve the scsi device type */
1017 if (asprintf(&device
, "/sys/dev/block/%d:%d/device/xxxxxxx", major
, minor
) < 0) {
1019 fprintf(stderr
, Name
": failed to allocate 'device'\n");
1023 sprintf(device
, "/sys/dev/block/%d:%d/device/type", major
, minor
);
1024 if (load_sys(device
, buf
) != 0) {
1026 fprintf(stderr
, Name
": failed to read device type for %s\n",
1032 type
= strtoul(buf
, NULL
, 10);
1034 /* if it's not a disk print the vendor and model */
1035 if (!(type
== 0 || type
== 7 || type
== 14)) {
1038 sprintf(device
, "/sys/dev/block/%d:%d/device/vendor", major
, minor
);
1039 if (load_sys(device
, buf
) == 0) {
1040 strncpy(vendor
, buf
, sizeof(vendor
));
1041 vendor
[sizeof(vendor
) - 1] = '\0';
1042 c
= (char *) &vendor
[sizeof(vendor
) - 1];
1043 while (isspace(*c
) || *c
== '\0')
1047 sprintf(device
, "/sys/dev/block/%d:%d/device/model", major
, minor
);
1048 if (load_sys(device
, buf
) == 0) {
1049 strncpy(model
, buf
, sizeof(model
));
1050 model
[sizeof(model
) - 1] = '\0';
1051 c
= (char *) &model
[sizeof(model
) - 1];
1052 while (isspace(*c
) || *c
== '\0')
1056 if (vendor
[0] && model
[0])
1057 sprintf(buf
, "%.64s %.64s", vendor
, model
);
1059 switch (type
) { /* numbers from hald/linux/device.c */
1060 case 1: sprintf(buf
, "tape"); break;
1061 case 2: sprintf(buf
, "printer"); break;
1062 case 3: sprintf(buf
, "processor"); break;
1064 case 5: sprintf(buf
, "cdrom"); break;
1065 case 6: sprintf(buf
, "scanner"); break;
1066 case 8: sprintf(buf
, "media_changer"); break;
1067 case 9: sprintf(buf
, "comm"); break;
1068 case 12: sprintf(buf
, "raid"); break;
1069 default: sprintf(buf
, "unknown");
1075 /* chop device path to 'host%d' and calculate the port number */
1076 c
= strchr(&path
[hba_len
], '/');
1079 fprintf(stderr
, Name
": %s - invalid path name\n", path
+ hba_len
);
1084 if (sscanf(&path
[hba_len
], "host%d", &port
) == 1)
1088 *c
= '/'; /* repair the full string */
1089 fprintf(stderr
, Name
": failed to determine port number for %s\n",
1096 /* mark this port as used */
1097 port_mask
&= ~(1 << port
);
1099 /* print out the device information */
1101 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
1105 fd
= dev_open(ent
->d_name
, O_RDONLY
);
1107 printf(" Port%d : - disk info unavailable -\n", port
);
1109 fd2devname(fd
, buf
);
1110 printf(" Port%d : %s", port
, buf
);
1111 if (imsm_read_serial(fd
, NULL
, (__u8
*) buf
) == 0)
1112 printf(" (%s)\n", buf
);
1127 for (i
= 0; i
< port_count
; i
++)
1128 if (port_mask
& (1 << i
))
1129 printf(" Port%d : - no device attached -\n", i
);
1135 static int detail_platform_imsm(int verbose
, int enumerate_only
)
1137 /* There are two components to imsm platform support, the ahci SATA
1138 * controller and the option-rom. To find the SATA controller we
1139 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
1140 * controller with the Intel vendor id is present. This approach
1141 * allows mdadm to leverage the kernel's ahci detection logic, with the
1142 * caveat that if ahci.ko is not loaded mdadm will not be able to
1143 * detect platform raid capabilities. The option-rom resides in a
1144 * platform "Adapter ROM". We scan for its signature to retrieve the
1145 * platform capabilities. If raid support is disabled in the BIOS the
1146 * option-rom capability structure will not be available.
1148 const struct imsm_orom
*orom
;
1149 struct sys_dev
*list
, *hba
;
1152 const char *hba_path
;
1156 if (enumerate_only
) {
1157 if (check_env("IMSM_NO_PLATFORM") || find_imsm_orom())
1162 list
= find_driver_devices("pci", "ahci");
1163 for (hba
= list
; hba
; hba
= hba
->next
)
1164 if (devpath_to_vendor(hba
->path
) == 0x8086)
1169 fprintf(stderr
, Name
": unable to find active ahci controller\n");
1170 free_sys_dev(&list
);
1173 fprintf(stderr
, Name
": found Intel SATA AHCI Controller\n");
1174 hba_path
= hba
->path
;
1176 free_sys_dev(&list
);
1178 orom
= find_imsm_orom();
1181 fprintf(stderr
, Name
": imsm option-rom not found\n");
1185 printf(" Platform : Intel(R) Matrix Storage Manager\n");
1186 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
1187 orom
->hotfix_ver
, orom
->build
);
1188 printf(" RAID Levels :%s%s%s%s%s\n",
1189 imsm_orom_has_raid0(orom
) ? " raid0" : "",
1190 imsm_orom_has_raid1(orom
) ? " raid1" : "",
1191 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
1192 imsm_orom_has_raid10(orom
) ? " raid10" : "",
1193 imsm_orom_has_raid5(orom
) ? " raid5" : "");
1194 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1195 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
1196 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
1197 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
1198 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
1199 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
1200 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
1201 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
1202 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
1203 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
1204 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
1205 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
1206 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
1207 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
1208 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
1209 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
1210 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
1211 printf(" Max Disks : %d\n", orom
->tds
);
1212 printf(" Max Volumes : %d\n", orom
->vpa
);
1213 printf(" I/O Controller : %s\n", hba_path
);
1215 /* find the smallest scsi host number to determine a port number base */
1216 dir
= opendir(hba_path
);
1217 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
1220 if (sscanf(ent
->d_name
, "host%d", &host
) != 1)
1222 if (port_count
== 0)
1224 else if (host
< host_base
)
1227 if (host
+ 1 > port_count
+ host_base
)
1228 port_count
= host
+ 1 - host_base
;
1234 if (!port_count
|| imsm_enumerate_ports(hba_path
, port_count
,
1235 host_base
, verbose
) != 0) {
1237 fprintf(stderr
, Name
": failed to enumerate ports\n");
1245 static int match_home_imsm(struct supertype
*st
, char *homehost
)
1247 /* the imsm metadata format does not specify any host
1248 * identification information. We return -1 since we can never
1249 * confirm nor deny whether a given array is "meant" for this
1250 * host. We rely on compare_super and the 'family_num' fields to
1251 * exclude member disks that do not belong, and we rely on
1252 * mdadm.conf to specify the arrays that should be assembled.
1253 * Auto-assembly may still pick up "foreign" arrays.
1259 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
1261 /* The uuid returned here is used for:
1262 * uuid to put into bitmap file (Create, Grow)
1263 * uuid for backup header when saving critical section (Grow)
1264 * comparing uuids when re-adding a device into an array
1265 * In these cases the uuid required is that of the data-array,
1266 * not the device-set.
1267 * uuid to recognise same set when adding a missing device back
1268 * to an array. This is a uuid for the device-set.
1270 * For each of these we can make do with a truncated
1271 * or hashed uuid rather than the original, as long as
1273 * In each case the uuid required is that of the data-array,
1274 * not the device-set.
1276 /* imsm does not track uuid's so we synthesis one using sha1 on
1277 * - The signature (Which is constant for all imsm array, but no matter)
1278 * - the orig_family_num of the container
1279 * - the index number of the volume
1280 * - the 'serial' number of the volume.
1281 * Hopefully these are all constant.
1283 struct intel_super
*super
= st
->sb
;
1286 struct sha1_ctx ctx
;
1287 struct imsm_dev
*dev
= NULL
;
1290 /* some mdadm versions failed to set ->orig_family_num, in which
1291 * case fall back to ->family_num. orig_family_num will be
1292 * fixed up with the first metadata update.
1294 family_num
= super
->anchor
->orig_family_num
;
1295 if (family_num
== 0)
1296 family_num
= super
->anchor
->family_num
;
1297 sha1_init_ctx(&ctx
);
1298 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
1299 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
1300 if (super
->current_vol
>= 0)
1301 dev
= get_imsm_dev(super
, super
->current_vol
);
1303 __u32 vol
= super
->current_vol
;
1304 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
1305 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
1307 sha1_finish_ctx(&ctx
, buf
);
1308 memcpy(uuid
, buf
, 4*4);
1313 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
1315 __u8
*v
= get_imsm_version(mpb
);
1316 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
1317 char major
[] = { 0, 0, 0 };
1318 char minor
[] = { 0 ,0, 0 };
1319 char patch
[] = { 0, 0, 0 };
1320 char *ver_parse
[] = { major
, minor
, patch
};
1324 while (*v
!= '\0' && v
< end
) {
1325 if (*v
!= '.' && j
< 2)
1326 ver_parse
[i
][j
++] = *v
;
1334 *m
= strtol(minor
, NULL
, 0);
1335 *p
= strtol(patch
, NULL
, 0);
1339 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
1341 /* migr_strip_size when repairing or initializing parity */
1342 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1343 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1345 switch (get_imsm_raid_level(map
)) {
1350 return 128*1024 >> 9;
1354 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
1356 /* migr_strip_size when rebuilding a degraded disk, no idea why
1357 * this is different than migr_strip_size_resync(), but it's good
1360 struct imsm_map
*map
= get_imsm_map(dev
, 1);
1361 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1363 switch (get_imsm_raid_level(map
)) {
1366 if (map
->num_members
% map
->num_domains
== 0)
1367 return 128*1024 >> 9;
1371 return max((__u32
) 64*1024 >> 9, chunk
);
1373 return 128*1024 >> 9;
1377 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
1379 struct imsm_map
*lo
= get_imsm_map(dev
, 0);
1380 struct imsm_map
*hi
= get_imsm_map(dev
, 1);
1381 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
1382 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
1384 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
1387 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
1389 struct imsm_map
*lo
= get_imsm_map(dev
, 0);
1390 int level
= get_imsm_raid_level(lo
);
1392 if (level
== 1 || level
== 10) {
1393 struct imsm_map
*hi
= get_imsm_map(dev
, 1);
1395 return hi
->num_domains
;
1397 return num_stripes_per_unit_resync(dev
);
1400 static __u8
imsm_num_data_members(struct imsm_dev
*dev
)
1402 /* named 'imsm_' because raid0, raid1 and raid10
1403 * counter-intuitively have the same number of data disks
1405 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1407 switch (get_imsm_raid_level(map
)) {
1411 return map
->num_members
;
1413 return map
->num_members
- 1;
1415 dprintf("%s: unsupported raid level\n", __func__
);
1420 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
1422 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1423 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1425 switch(get_imsm_raid_level(map
)) {
1428 return chunk
* map
->num_domains
;
1430 return chunk
* map
->num_members
;
1436 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
1438 struct imsm_map
*map
= get_imsm_map(dev
, 1);
1439 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1440 __u32 strip
= block
/ chunk
;
1442 switch (get_imsm_raid_level(map
)) {
1445 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
1446 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
1448 return vol_stripe
* chunk
+ block
% chunk
;
1450 __u32 stripe
= strip
/ (map
->num_members
- 1);
1452 return stripe
* chunk
+ block
% chunk
;
1459 static __u64
blocks_per_migr_unit(struct imsm_dev
*dev
)
1461 /* calculate the conversion factor between per member 'blocks'
1462 * (md/{resync,rebuild}_start) and imsm migration units, return
1463 * 0 for the 'not migrating' and 'unsupported migration' cases
1465 if (!dev
->vol
.migr_state
)
1468 switch (migr_type(dev
)) {
1472 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1473 __u32 stripes_per_unit
;
1474 __u32 blocks_per_unit
;
1483 /* yes, this is really the translation of migr_units to
1484 * per-member blocks in the 'resync' case
1486 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
1487 migr_chunk
= migr_strip_blocks_resync(dev
);
1488 disks
= imsm_num_data_members(dev
);
1489 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
1490 stripe
= __le32_to_cpu(map
->blocks_per_strip
) * disks
;
1491 segment
= blocks_per_unit
/ stripe
;
1492 block_rel
= blocks_per_unit
- segment
* stripe
;
1493 parity_depth
= parity_segment_depth(dev
);
1494 block_map
= map_migr_block(dev
, block_rel
);
1495 return block_map
+ parity_depth
* segment
;
1497 case MIGR_REBUILD
: {
1498 __u32 stripes_per_unit
;
1501 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
1502 migr_chunk
= migr_strip_blocks_rebuild(dev
);
1503 return migr_chunk
* stripes_per_unit
;
1506 case MIGR_STATE_CHANGE
:
1512 static int imsm_level_to_layout(int level
)
1520 return ALGORITHM_LEFT_ASYMMETRIC
;
1527 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
1529 struct intel_super
*super
= st
->sb
;
1530 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
1531 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1534 int map_disks
= info
->array
.raid_disks
;
1536 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
1537 if (dl
->raiddisk
== info
->disk
.raid_disk
)
1539 info
->container_member
= super
->current_vol
;
1540 info
->array
.raid_disks
= map
->num_members
;
1541 info
->array
.level
= get_imsm_raid_level(map
);
1542 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
1543 info
->array
.md_minor
= -1;
1544 info
->array
.ctime
= 0;
1545 info
->array
.utime
= 0;
1546 info
->array
.chunk_size
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
1547 info
->array
.state
= !dev
->vol
.dirty
;
1548 info
->custom_array_size
= __le32_to_cpu(dev
->size_high
);
1549 info
->custom_array_size
<<= 32;
1550 info
->custom_array_size
|= __le32_to_cpu(dev
->size_low
);
1552 info
->disk
.major
= 0;
1553 info
->disk
.minor
= 0;
1555 info
->disk
.major
= dl
->major
;
1556 info
->disk
.minor
= dl
->minor
;
1559 info
->data_offset
= __le32_to_cpu(map
->pba_of_lba0
);
1560 info
->component_size
= __le32_to_cpu(map
->blocks_per_member
);
1561 memset(info
->uuid
, 0, sizeof(info
->uuid
));
1562 info
->recovery_start
= MaxSector
;
1563 info
->reshape_active
= 0;
1565 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
|| dev
->vol
.dirty
) {
1566 info
->resync_start
= 0;
1567 } else if (dev
->vol
.migr_state
) {
1568 switch (migr_type(dev
)) {
1571 __u64 blocks_per_unit
= blocks_per_migr_unit(dev
);
1572 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
1574 info
->resync_start
= blocks_per_unit
* units
;
1578 /* we could emulate the checkpointing of
1579 * 'sync_action=check' migrations, but for now
1580 * we just immediately complete them
1583 /* this is handled by container_content_imsm() */
1585 case MIGR_STATE_CHANGE
:
1586 /* FIXME handle other migrations */
1588 /* we are not dirty, so... */
1589 info
->resync_start
= MaxSector
;
1592 info
->resync_start
= MaxSector
;
1594 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
1595 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
1597 info
->array
.major_version
= -1;
1598 info
->array
.minor_version
= -2;
1599 devname
= devnum2devname(st
->container_dev
);
1600 *info
->text_version
= '\0';
1602 sprintf(info
->text_version
, "/%s/%d", devname
, info
->container_member
);
1604 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
1605 uuid_from_super_imsm(st
, info
->uuid
);
1609 for (i
=0; i
<map_disks
; i
++) {
1611 if (i
< info
->array
.raid_disks
) {
1612 struct imsm_disk
*dsk
;
1613 j
= get_imsm_disk_idx(dev
, i
);
1614 dsk
= get_imsm_disk(super
, j
);
1615 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
1622 /* check the config file to see if we can return a real uuid for this spare */
1623 static void fixup_container_spare_uuid(struct mdinfo
*inf
)
1625 struct mddev_ident
*array_list
;
1627 if (inf
->array
.level
!= LEVEL_CONTAINER
||
1628 memcmp(inf
->uuid
, uuid_match_any
, sizeof(int[4])) != 0)
1631 array_list
= conf_get_ident(NULL
);
1633 for (; array_list
; array_list
= array_list
->next
) {
1634 if (array_list
->uuid_set
) {
1635 struct supertype
*_sst
; /* spare supertype */
1636 struct supertype
*_cst
; /* container supertype */
1638 _cst
= array_list
->st
;
1640 _sst
= _cst
->ss
->match_metadata_desc(inf
->text_version
);
1645 memcpy(inf
->uuid
, array_list
->uuid
, sizeof(int[4]));
1654 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
, int failed
);
1655 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
);
1657 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
1661 for (d
= super
->missing
; d
; d
= d
->next
)
1662 if (d
->index
== index
)
1667 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
1669 struct intel_super
*super
= st
->sb
;
1670 struct imsm_disk
*disk
;
1671 int map_disks
= info
->array
.raid_disks
;
1672 int max_enough
= -1;
1674 struct imsm_super
*mpb
;
1676 if (super
->current_vol
>= 0) {
1677 getinfo_super_imsm_volume(st
, info
, map
);
1681 /* Set raid_disks to zero so that Assemble will always pull in valid
1684 info
->array
.raid_disks
= 0;
1685 info
->array
.level
= LEVEL_CONTAINER
;
1686 info
->array
.layout
= 0;
1687 info
->array
.md_minor
= -1;
1688 info
->array
.ctime
= 0; /* N/A for imsm */
1689 info
->array
.utime
= 0;
1690 info
->array
.chunk_size
= 0;
1692 info
->disk
.major
= 0;
1693 info
->disk
.minor
= 0;
1694 info
->disk
.raid_disk
= -1;
1695 info
->reshape_active
= 0;
1696 info
->array
.major_version
= -1;
1697 info
->array
.minor_version
= -2;
1698 strcpy(info
->text_version
, "imsm");
1699 info
->safe_mode_delay
= 0;
1700 info
->disk
.number
= -1;
1701 info
->disk
.state
= 0;
1703 info
->recovery_start
= MaxSector
;
1705 /* do we have the all the insync disks that we expect? */
1706 mpb
= super
->anchor
;
1708 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1709 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1710 int failed
, enough
, j
, missing
= 0;
1711 struct imsm_map
*map
;
1714 failed
= imsm_count_failed(super
, dev
);
1715 state
= imsm_check_degraded(super
, dev
, failed
);
1716 map
= get_imsm_map(dev
, dev
->vol
.migr_state
);
1718 /* any newly missing disks?
1719 * (catches single-degraded vs double-degraded)
1721 for (j
= 0; j
< map
->num_members
; j
++) {
1722 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
);
1723 __u32 idx
= ord_to_idx(ord
);
1725 if (!(ord
& IMSM_ORD_REBUILD
) &&
1726 get_imsm_missing(super
, idx
)) {
1732 if (state
== IMSM_T_STATE_FAILED
)
1734 else if (state
== IMSM_T_STATE_DEGRADED
&&
1735 (state
!= map
->map_state
|| missing
))
1737 else /* we're normal, or already degraded */
1740 /* in the missing/failed disk case check to see
1741 * if at least one array is runnable
1743 max_enough
= max(max_enough
, enough
);
1745 dprintf("%s: enough: %d\n", __func__
, max_enough
);
1746 info
->container_enough
= max_enough
;
1749 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
1751 disk
= &super
->disks
->disk
;
1752 info
->data_offset
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
1753 info
->component_size
= reserved
;
1754 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
1755 /* we don't change info->disk.raid_disk here because
1756 * this state will be finalized in mdmon after we have
1757 * found the 'most fresh' version of the metadata
1759 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
1760 info
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
1763 /* only call uuid_from_super_imsm when this disk is part of a populated container,
1764 * ->compare_super may have updated the 'num_raid_devs' field for spares
1766 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
1767 uuid_from_super_imsm(st
, info
->uuid
);
1769 memcpy(info
->uuid
, uuid_match_any
, sizeof(int[4]));
1770 fixup_container_spare_uuid(info
);
1773 /* I don't know how to compute 'map' on imsm, so use safe default */
1776 for (i
= 0; i
< map_disks
; i
++)
1782 /* allocates memory and fills disk in mdinfo structure
1783 * for each disk in array */
1784 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
1786 struct mdinfo
*mddev
= NULL
;
1787 struct intel_super
*super
= st
->sb
;
1788 struct imsm_disk
*disk
;
1791 if (!super
|| !super
->disks
)
1794 mddev
= malloc(sizeof(*mddev
));
1796 fprintf(stderr
, Name
": Failed to allocate memory.\n");
1799 memset(mddev
, 0, sizeof(*mddev
));
1803 tmp
= malloc(sizeof(*tmp
));
1805 fprintf(stderr
, Name
": Failed to allocate memory.\n");
1810 memset(tmp
, 0, sizeof(*tmp
));
1812 tmp
->next
= mddev
->devs
;
1814 tmp
->disk
.number
= count
++;
1815 tmp
->disk
.major
= dl
->major
;
1816 tmp
->disk
.minor
= dl
->minor
;
1817 tmp
->disk
.state
= is_configured(disk
) ?
1818 (1 << MD_DISK_ACTIVE
) : 0;
1819 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
1820 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
1821 tmp
->disk
.raid_disk
= -1;
1827 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
1828 char *update
, char *devname
, int verbose
,
1829 int uuid_set
, char *homehost
)
1831 /* For 'assemble' and 'force' we need to return non-zero if any
1832 * change was made. For others, the return value is ignored.
1833 * Update options are:
1834 * force-one : This device looks a bit old but needs to be included,
1835 * update age info appropriately.
1836 * assemble: clear any 'faulty' flag to allow this device to
1838 * force-array: Array is degraded but being forced, mark it clean
1839 * if that will be needed to assemble it.
1841 * newdev: not used ????
1842 * grow: Array has gained a new device - this is currently for
1844 * resync: mark as dirty so a resync will happen.
1845 * name: update the name - preserving the homehost
1846 * uuid: Change the uuid of the array to match watch is given
1848 * Following are not relevant for this imsm:
1849 * sparc2.2 : update from old dodgey metadata
1850 * super-minor: change the preferred_minor number
1851 * summaries: update redundant counters.
1852 * homehost: update the recorded homehost
1853 * _reshape_progress: record new reshape_progress position.
1856 struct intel_super
*super
= st
->sb
;
1857 struct imsm_super
*mpb
;
1859 /* we can only update container info */
1860 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
1863 mpb
= super
->anchor
;
1865 if (strcmp(update
, "uuid") == 0 && uuid_set
&& !info
->update_private
)
1867 else if (strcmp(update
, "uuid") == 0 && uuid_set
&& info
->update_private
) {
1868 mpb
->orig_family_num
= *((__u32
*) info
->update_private
);
1870 } else if (strcmp(update
, "uuid") == 0) {
1871 __u32
*new_family
= malloc(sizeof(*new_family
));
1873 /* update orig_family_number with the incoming random
1874 * data, report the new effective uuid, and store the
1875 * new orig_family_num for future updates.
1878 memcpy(&mpb
->orig_family_num
, info
->uuid
, sizeof(__u32
));
1879 uuid_from_super_imsm(st
, info
->uuid
);
1880 *new_family
= mpb
->orig_family_num
;
1881 info
->update_private
= new_family
;
1884 } else if (strcmp(update
, "assemble") == 0)
1889 /* successful update? recompute checksum */
1891 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
1896 static size_t disks_to_mpb_size(int disks
)
1900 size
= sizeof(struct imsm_super
);
1901 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
1902 size
+= 2 * sizeof(struct imsm_dev
);
1903 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
1904 size
+= (4 - 2) * sizeof(struct imsm_map
);
1905 /* 4 possible disk_ord_tbl's */
1906 size
+= 4 * (disks
- 1) * sizeof(__u32
);
1911 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
)
1913 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
1916 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
1919 static void free_devlist(struct intel_super
*super
)
1921 struct intel_dev
*dv
;
1923 while (super
->devlist
) {
1924 dv
= super
->devlist
->next
;
1925 free(super
->devlist
->dev
);
1926 free(super
->devlist
);
1927 super
->devlist
= dv
;
1931 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
1933 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
1936 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
1940 * 0 same, or first was empty, and second was copied
1941 * 1 second had wrong number
1943 * 3 wrong other info
1945 struct intel_super
*first
= st
->sb
;
1946 struct intel_super
*sec
= tst
->sb
;
1954 /* if an anchor does not have num_raid_devs set then it is a free
1957 if (first
->anchor
->num_raid_devs
> 0 &&
1958 sec
->anchor
->num_raid_devs
> 0) {
1959 /* Determine if these disks might ever have been
1960 * related. Further disambiguation can only take place
1961 * in load_super_imsm_all
1963 __u32 first_family
= first
->anchor
->orig_family_num
;
1964 __u32 sec_family
= sec
->anchor
->orig_family_num
;
1966 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
1967 MAX_SIGNATURE_LENGTH
) != 0)
1970 if (first_family
== 0)
1971 first_family
= first
->anchor
->family_num
;
1972 if (sec_family
== 0)
1973 sec_family
= sec
->anchor
->family_num
;
1975 if (first_family
!= sec_family
)
1981 /* if 'first' is a spare promote it to a populated mpb with sec's
1984 if (first
->anchor
->num_raid_devs
== 0 &&
1985 sec
->anchor
->num_raid_devs
> 0) {
1987 struct intel_dev
*dv
;
1988 struct imsm_dev
*dev
;
1990 /* we need to copy raid device info from sec if an allocation
1991 * fails here we don't associate the spare
1993 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
1994 dv
= malloc(sizeof(*dv
));
1997 dev
= malloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
2004 dv
->next
= first
->devlist
;
2005 first
->devlist
= dv
;
2007 if (i
< sec
->anchor
->num_raid_devs
) {
2008 /* allocation failure */
2009 free_devlist(first
);
2010 fprintf(stderr
, "imsm: failed to associate spare\n");
2013 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
2014 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
2015 first
->anchor
->family_num
= sec
->anchor
->family_num
;
2016 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
2017 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
2018 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
2024 static void fd2devname(int fd
, char *name
)
2028 char dname
[PATH_MAX
];
2033 if (fstat(fd
, &st
) != 0)
2035 sprintf(path
, "/sys/dev/block/%d:%d",
2036 major(st
.st_rdev
), minor(st
.st_rdev
));
2038 rv
= readlink(path
, dname
, sizeof(dname
));
2043 nm
= strrchr(dname
, '/');
2045 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
2048 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
2050 static int imsm_read_serial(int fd
, char *devname
,
2051 __u8 serial
[MAX_RAID_SERIAL_LEN
])
2053 unsigned char scsi_serial
[255];
2062 memset(scsi_serial
, 0, sizeof(scsi_serial
));
2064 rv
= scsi_get_serial(fd
, scsi_serial
, sizeof(scsi_serial
));
2066 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
2067 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
2068 fd2devname(fd
, (char *) serial
);
2075 Name
": Failed to retrieve serial for %s\n",
2080 rsp_len
= scsi_serial
[3];
2084 Name
": Failed to retrieve serial for %s\n",
2088 rsp_buf
= (char *) &scsi_serial
[4];
2090 /* trim all whitespace and non-printable characters and convert
2093 for (i
= 0, dest
= rsp_buf
; i
< rsp_len
; i
++) {
2096 /* ':' is reserved for use in placeholder serial
2097 * numbers for missing disks
2105 len
= dest
- rsp_buf
;
2108 /* truncate leading characters */
2109 if (len
> MAX_RAID_SERIAL_LEN
) {
2110 dest
+= len
- MAX_RAID_SERIAL_LEN
;
2111 len
= MAX_RAID_SERIAL_LEN
;
2114 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
2115 memcpy(serial
, dest
, len
);
2120 static int serialcmp(__u8
*s1
, __u8
*s2
)
2122 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
2125 static void serialcpy(__u8
*dest
, __u8
*src
)
2127 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
2131 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
2135 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
2136 if (serialcmp(dl
->serial
, serial
) == 0)
2143 static struct imsm_disk
*
2144 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
2148 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2149 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
2151 if (serialcmp(disk
->serial
, serial
) == 0) {
2162 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
2164 struct imsm_disk
*disk
;
2169 __u8 serial
[MAX_RAID_SERIAL_LEN
];
2171 rv
= imsm_read_serial(fd
, devname
, serial
);
2176 dl
= calloc(1, sizeof(*dl
));
2180 Name
": failed to allocate disk buffer for %s\n",
2186 dl
->major
= major(stb
.st_rdev
);
2187 dl
->minor
= minor(stb
.st_rdev
);
2188 dl
->next
= super
->disks
;
2189 dl
->fd
= keep_fd
? fd
: -1;
2190 assert(super
->disks
== NULL
);
2192 serialcpy(dl
->serial
, serial
);
2195 fd2devname(fd
, name
);
2197 dl
->devname
= strdup(devname
);
2199 dl
->devname
= strdup(name
);
2201 /* look up this disk's index in the current anchor */
2202 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
2205 /* only set index on disks that are a member of a
2206 * populated contianer, i.e. one with raid_devs
2208 if (is_failed(&dl
->disk
))
2210 else if (is_spare(&dl
->disk
))
2218 /* When migrating map0 contains the 'destination' state while map1
2219 * contains the current state. When not migrating map0 contains the
2220 * current state. This routine assumes that map[0].map_state is set to
2221 * the current array state before being called.
2223 * Migration is indicated by one of the following states
2224 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
2225 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
2226 * map1state=unitialized)
2227 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
2229 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
2230 * map1state=degraded)
2232 static void migrate(struct imsm_dev
*dev
, __u8 to_state
, int migr_type
)
2234 struct imsm_map
*dest
;
2235 struct imsm_map
*src
= get_imsm_map(dev
, 0);
2237 dev
->vol
.migr_state
= 1;
2238 set_migr_type(dev
, migr_type
);
2239 dev
->vol
.curr_migr_unit
= 0;
2240 dest
= get_imsm_map(dev
, 1);
2242 /* duplicate and then set the target end state in map[0] */
2243 memcpy(dest
, src
, sizeof_imsm_map(src
));
2244 if ((migr_type
== MIGR_REBUILD
) ||
2245 (migr_type
== MIGR_GEN_MIGR
)) {
2249 for (i
= 0; i
< src
->num_members
; i
++) {
2250 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
2251 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
2255 src
->map_state
= to_state
;
2258 static void end_migration(struct imsm_dev
*dev
, __u8 map_state
)
2260 struct imsm_map
*map
= get_imsm_map(dev
, 0);
2261 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
);
2264 /* merge any IMSM_ORD_REBUILD bits that were not successfully
2265 * completed in the last migration.
2267 * FIXME add support for raid-level-migration
2269 for (i
= 0; i
< prev
->num_members
; i
++)
2270 for (j
= 0; j
< map
->num_members
; j
++)
2271 /* during online capacity expansion
2272 * disks position can be changed if takeover is used
2274 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
2275 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
2276 map
->disk_ord_tbl
[j
] |= prev
->disk_ord_tbl
[i
];
2280 dev
->vol
.migr_state
= 0;
2281 dev
->vol
.migr_type
= 0;
2282 dev
->vol
.curr_migr_unit
= 0;
2283 map
->map_state
= map_state
;
2287 static int parse_raid_devices(struct intel_super
*super
)
2290 struct imsm_dev
*dev_new
;
2291 size_t len
, len_migr
;
2292 size_t space_needed
= 0;
2293 struct imsm_super
*mpb
= super
->anchor
;
2295 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
2296 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
2297 struct intel_dev
*dv
;
2299 len
= sizeof_imsm_dev(dev_iter
, 0);
2300 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
2302 space_needed
+= len_migr
- len
;
2304 dv
= malloc(sizeof(*dv
));
2307 dev_new
= malloc(len_migr
);
2312 imsm_copy_dev(dev_new
, dev_iter
);
2315 dv
->next
= super
->devlist
;
2316 super
->devlist
= dv
;
2319 /* ensure that super->buf is large enough when all raid devices
2322 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
2325 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
, 512);
2326 if (posix_memalign(&buf
, 512, len
) != 0)
2329 memcpy(buf
, super
->buf
, super
->len
);
2330 memset(buf
+ super
->len
, 0, len
- super
->len
);
2339 /* retrieve a pointer to the bbm log which starts after all raid devices */
2340 struct bbm_log
*__get_imsm_bbm_log(struct imsm_super
*mpb
)
2344 if (__le32_to_cpu(mpb
->bbm_log_size
)) {
2346 ptr
+= mpb
->mpb_size
- __le32_to_cpu(mpb
->bbm_log_size
);
2352 static void __free_imsm(struct intel_super
*super
, int free_disks
);
2354 /* load_imsm_mpb - read matrix metadata
2355 * allocates super->mpb to be freed by free_super
2357 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
2359 unsigned long long dsize
;
2360 unsigned long long sectors
;
2362 struct imsm_super
*anchor
;
2365 get_dev_size(fd
, NULL
, &dsize
);
2369 Name
": %s: device to small for imsm\n",
2374 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0) {
2377 Name
": Cannot seek to anchor block on %s: %s\n",
2378 devname
, strerror(errno
));
2382 if (posix_memalign((void**)&anchor
, 512, 512) != 0) {
2385 Name
": Failed to allocate imsm anchor buffer"
2386 " on %s\n", devname
);
2389 if (read(fd
, anchor
, 512) != 512) {
2392 Name
": Cannot read anchor block on %s: %s\n",
2393 devname
, strerror(errno
));
2398 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
2401 Name
": no IMSM anchor on %s\n", devname
);
2406 __free_imsm(super
, 0);
2407 super
->len
= ROUND_UP(anchor
->mpb_size
, 512);
2408 if (posix_memalign(&super
->buf
, 512, super
->len
) != 0) {
2411 Name
": unable to allocate %zu byte mpb buffer\n",
2416 memcpy(super
->buf
, anchor
, 512);
2418 sectors
= mpb_sectors(anchor
) - 1;
2421 check_sum
= __gen_imsm_checksum(super
->anchor
);
2422 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
2425 Name
": IMSM checksum %x != %x on %s\n",
2427 __le32_to_cpu(super
->anchor
->check_sum
),
2435 /* read the extended mpb */
2436 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0) {
2439 Name
": Cannot seek to extended mpb on %s: %s\n",
2440 devname
, strerror(errno
));
2444 if ((unsigned)read(fd
, super
->buf
+ 512, super
->len
- 512) != super
->len
- 512) {
2447 Name
": Cannot read extended mpb on %s: %s\n",
2448 devname
, strerror(errno
));
2452 check_sum
= __gen_imsm_checksum(super
->anchor
);
2453 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
2456 Name
": IMSM checksum %x != %x on %s\n",
2457 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
2462 /* FIXME the BBM log is disk specific so we cannot use this global
2463 * buffer for all disks. Ok for now since we only look at the global
2464 * bbm_log_size parameter to gate assembly
2466 super
->bbm_log
= __get_imsm_bbm_log(super
->anchor
);
2472 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
2476 err
= load_imsm_mpb(fd
, super
, devname
);
2479 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
2482 err
= parse_raid_devices(super
);
2487 static void __free_imsm_disk(struct dl
*d
)
2499 static void free_imsm_disks(struct intel_super
*super
)
2503 while (super
->disks
) {
2505 super
->disks
= d
->next
;
2506 __free_imsm_disk(d
);
2508 while (super
->missing
) {
2510 super
->missing
= d
->next
;
2511 __free_imsm_disk(d
);
2516 /* free all the pieces hanging off of a super pointer */
2517 static void __free_imsm(struct intel_super
*super
, int free_disks
)
2524 free_imsm_disks(super
);
2525 free_devlist(super
);
2527 free((void *) super
->hba
);
2532 static void free_imsm(struct intel_super
*super
)
2534 __free_imsm(super
, 1);
2538 static void free_super_imsm(struct supertype
*st
)
2540 struct intel_super
*super
= st
->sb
;
2549 static struct intel_super
*alloc_super(void)
2551 struct intel_super
*super
= malloc(sizeof(*super
));
2554 memset(super
, 0, sizeof(*super
));
2555 super
->current_vol
= -1;
2556 super
->create_offset
= ~((__u32
) 0);
2557 if (!check_env("IMSM_NO_PLATFORM"))
2558 super
->orom
= find_imsm_orom();
2559 if (super
->orom
&& !check_env("IMSM_TEST_OROM")) {
2560 struct sys_dev
*list
, *ent
;
2562 /* find the first intel ahci controller */
2563 list
= find_driver_devices("pci", "ahci");
2564 for (ent
= list
; ent
; ent
= ent
->next
)
2565 if (devpath_to_vendor(ent
->path
) == 0x8086)
2568 super
->hba
= ent
->path
;
2571 free_sys_dev(&list
);
2579 /* find_missing - helper routine for load_super_imsm_all that identifies
2580 * disks that have disappeared from the system. This routine relies on
2581 * the mpb being uptodate, which it is at load time.
2583 static int find_missing(struct intel_super
*super
)
2586 struct imsm_super
*mpb
= super
->anchor
;
2588 struct imsm_disk
*disk
;
2590 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2591 disk
= __get_imsm_disk(mpb
, i
);
2592 dl
= serial_to_dl(disk
->serial
, super
);
2596 dl
= malloc(sizeof(*dl
));
2602 dl
->devname
= strdup("missing");
2604 serialcpy(dl
->serial
, disk
->serial
);
2607 dl
->next
= super
->missing
;
2608 super
->missing
= dl
;
2614 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
2616 struct intel_disk
*idisk
= disk_list
;
2619 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
2621 idisk
= idisk
->next
;
2627 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
2628 struct intel_super
*super
,
2629 struct intel_disk
**disk_list
)
2631 struct imsm_disk
*d
= &super
->disks
->disk
;
2632 struct imsm_super
*mpb
= super
->anchor
;
2635 for (i
= 0; i
< tbl_size
; i
++) {
2636 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
2637 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
2639 if (tbl_mpb
->family_num
== mpb
->family_num
) {
2640 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
2641 dprintf("%s: mpb from %d:%d matches %d:%d\n",
2642 __func__
, super
->disks
->major
,
2643 super
->disks
->minor
,
2644 table
[i
]->disks
->major
,
2645 table
[i
]->disks
->minor
);
2649 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
2650 is_configured(d
) == is_configured(tbl_d
)) &&
2651 tbl_mpb
->generation_num
< mpb
->generation_num
) {
2652 /* current version of the mpb is a
2653 * better candidate than the one in
2654 * super_table, but copy over "cross
2655 * generational" status
2657 struct intel_disk
*idisk
;
2659 dprintf("%s: mpb from %d:%d replaces %d:%d\n",
2660 __func__
, super
->disks
->major
,
2661 super
->disks
->minor
,
2662 table
[i
]->disks
->major
,
2663 table
[i
]->disks
->minor
);
2665 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
2666 if (idisk
&& is_failed(&idisk
->disk
))
2667 tbl_d
->status
|= FAILED_DISK
;
2670 struct intel_disk
*idisk
;
2671 struct imsm_disk
*disk
;
2673 /* tbl_mpb is more up to date, but copy
2674 * over cross generational status before
2677 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
2678 if (disk
&& is_failed(disk
))
2679 d
->status
|= FAILED_DISK
;
2681 idisk
= disk_list_get(d
->serial
, *disk_list
);
2684 if (disk
&& is_configured(disk
))
2685 idisk
->disk
.status
|= CONFIGURED_DISK
;
2688 dprintf("%s: mpb from %d:%d prefer %d:%d\n",
2689 __func__
, super
->disks
->major
,
2690 super
->disks
->minor
,
2691 table
[i
]->disks
->major
,
2692 table
[i
]->disks
->minor
);
2700 table
[tbl_size
++] = super
;
2704 /* update/extend the merged list of imsm_disk records */
2705 for (j
= 0; j
< mpb
->num_disks
; j
++) {
2706 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
2707 struct intel_disk
*idisk
;
2709 idisk
= disk_list_get(disk
->serial
, *disk_list
);
2711 idisk
->disk
.status
|= disk
->status
;
2712 if (is_configured(&idisk
->disk
) ||
2713 is_failed(&idisk
->disk
))
2714 idisk
->disk
.status
&= ~(SPARE_DISK
);
2716 idisk
= calloc(1, sizeof(*idisk
));
2719 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
2720 idisk
->disk
= *disk
;
2721 idisk
->next
= *disk_list
;
2725 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
2732 static struct intel_super
*
2733 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
2736 struct imsm_super
*mpb
= super
->anchor
;
2740 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2741 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
2742 struct intel_disk
*idisk
;
2744 idisk
= disk_list_get(disk
->serial
, disk_list
);
2746 if (idisk
->owner
== owner
||
2747 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
2750 dprintf("%s: '%.16s' owner %d != %d\n",
2751 __func__
, disk
->serial
, idisk
->owner
,
2754 dprintf("%s: unknown disk %x [%d]: %.16s\n",
2755 __func__
, __le32_to_cpu(mpb
->family_num
), i
,
2761 if (ok_count
== mpb
->num_disks
)
2766 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
2768 struct intel_super
*s
;
2770 for (s
= super_list
; s
; s
= s
->next
) {
2771 if (family_num
!= s
->anchor
->family_num
)
2773 fprintf(stderr
, "Conflict, offlining family %#x on '%s'\n",
2774 __le32_to_cpu(family_num
), s
->disks
->devname
);
2778 static struct intel_super
*
2779 imsm_thunderdome(struct intel_super
**super_list
, int len
)
2781 struct intel_super
*super_table
[len
];
2782 struct intel_disk
*disk_list
= NULL
;
2783 struct intel_super
*champion
, *spare
;
2784 struct intel_super
*s
, **del
;
2789 memset(super_table
, 0, sizeof(super_table
));
2790 for (s
= *super_list
; s
; s
= s
->next
)
2791 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
2793 for (i
= 0; i
< tbl_size
; i
++) {
2794 struct imsm_disk
*d
;
2795 struct intel_disk
*idisk
;
2796 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
2799 d
= &s
->disks
->disk
;
2801 /* 'd' must appear in merged disk list for its
2802 * configuration to be valid
2804 idisk
= disk_list_get(d
->serial
, disk_list
);
2805 if (idisk
&& idisk
->owner
== i
)
2806 s
= validate_members(s
, disk_list
, i
);
2811 dprintf("%s: marking family: %#x from %d:%d offline\n",
2812 __func__
, mpb
->family_num
,
2813 super_table
[i
]->disks
->major
,
2814 super_table
[i
]->disks
->minor
);
2818 /* This is where the mdadm implementation differs from the Windows
2819 * driver which has no strict concept of a container. We can only
2820 * assemble one family from a container, so when returning a prodigal
2821 * array member to this system the code will not be able to disambiguate
2822 * the container contents that should be assembled ("foreign" versus
2823 * "local"). It requires user intervention to set the orig_family_num
2824 * to a new value to establish a new container. The Windows driver in
2825 * this situation fixes up the volume name in place and manages the
2826 * foreign array as an independent entity.
2831 for (i
= 0; i
< tbl_size
; i
++) {
2832 struct intel_super
*tbl_ent
= super_table
[i
];
2838 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
2843 if (s
&& !is_spare
) {
2844 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
2846 } else if (!s
&& !is_spare
)
2859 fprintf(stderr
, "Chose family %#x on '%s', "
2860 "assemble conflicts to new container with '--update=uuid'\n",
2861 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
2863 /* collect all dl's onto 'champion', and update them to
2864 * champion's version of the status
2866 for (s
= *super_list
; s
; s
= s
->next
) {
2867 struct imsm_super
*mpb
= champion
->anchor
;
2868 struct dl
*dl
= s
->disks
;
2873 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2874 struct imsm_disk
*disk
;
2876 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
2879 /* only set index on disks that are a member of
2880 * a populated contianer, i.e. one with
2883 if (is_failed(&dl
->disk
))
2885 else if (is_spare(&dl
->disk
))
2891 if (i
>= mpb
->num_disks
) {
2892 struct intel_disk
*idisk
;
2894 idisk
= disk_list_get(dl
->serial
, disk_list
);
2895 if (idisk
&& is_spare(&idisk
->disk
) &&
2896 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
2904 dl
->next
= champion
->disks
;
2905 champion
->disks
= dl
;
2909 /* delete 'champion' from super_list */
2910 for (del
= super_list
; *del
; ) {
2911 if (*del
== champion
) {
2912 *del
= (*del
)->next
;
2915 del
= &(*del
)->next
;
2917 champion
->next
= NULL
;
2921 struct intel_disk
*idisk
= disk_list
;
2923 disk_list
= disk_list
->next
;
2930 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
2934 struct intel_super
*super_list
= NULL
;
2935 struct intel_super
*super
= NULL
;
2936 int devnum
= fd2devnum(fd
);
2942 /* check if 'fd' an opened container */
2943 sra
= sysfs_read(fd
, 0, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
2947 if (sra
->array
.major_version
!= -1 ||
2948 sra
->array
.minor_version
!= -2 ||
2949 strcmp(sra
->text_version
, "imsm") != 0) {
2954 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
2955 struct intel_super
*s
= alloc_super();
2962 s
->next
= super_list
;
2966 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
2967 dfd
= dev_open(nm
, O_RDWR
);
2971 err
= load_and_parse_mpb(dfd
, s
, NULL
, 1);
2973 /* retry the load if we might have raced against mdmon */
2974 if (err
== 3 && mdmon_running(devnum
))
2975 for (retry
= 0; retry
< 3; retry
++) {
2977 err
= load_and_parse_mpb(dfd
, s
, NULL
, 1);
2985 /* all mpbs enter, maybe one leaves */
2986 super
= imsm_thunderdome(&super_list
, i
);
2992 if (find_missing(super
) != 0) {
3000 while (super_list
) {
3001 struct intel_super
*s
= super_list
;
3003 super_list
= super_list
->next
;
3012 st
->container_dev
= devnum
;
3013 if (err
== 0 && st
->ss
== NULL
) {
3014 st
->ss
= &super_imsm
;
3015 st
->minor_version
= 0;
3016 st
->max_devs
= IMSM_MAX_DEVICES
;
3021 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
3023 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
);
3027 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
3029 struct intel_super
*super
;
3033 if (load_super_imsm_all(st
, fd
, &st
->sb
, devname
) == 0)
3037 if (test_partition(fd
))
3038 /* IMSM not allowed on partitions */
3041 free_super_imsm(st
);
3043 super
= alloc_super();
3046 Name
": malloc of %zu failed.\n",
3051 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
3056 Name
": Failed to load all information "
3057 "sections on %s\n", devname
);
3063 if (st
->ss
== NULL
) {
3064 st
->ss
= &super_imsm
;
3065 st
->minor_version
= 0;
3066 st
->max_devs
= IMSM_MAX_DEVICES
;
3071 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
3073 if (info
->level
== 1)
3075 return info
->chunk_size
>> 9;
3078 static __u32
info_to_num_data_stripes(mdu_array_info_t
*info
, int num_domains
)
3082 num_stripes
= (info
->size
* 2) / info_to_blocks_per_strip(info
);
3083 num_stripes
/= num_domains
;
3088 static __u32
info_to_blocks_per_member(mdu_array_info_t
*info
)
3090 if (info
->level
== 1)
3091 return info
->size
* 2;
3093 return (info
->size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
3096 static void imsm_update_version_info(struct intel_super
*super
)
3098 /* update the version and attributes */
3099 struct imsm_super
*mpb
= super
->anchor
;
3101 struct imsm_dev
*dev
;
3102 struct imsm_map
*map
;
3105 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3106 dev
= get_imsm_dev(super
, i
);
3107 map
= get_imsm_map(dev
, 0);
3108 if (__le32_to_cpu(dev
->size_high
) > 0)
3109 mpb
->attributes
|= MPB_ATTRIB_2TB
;
3111 /* FIXME detect when an array spans a port multiplier */
3113 mpb
->attributes
|= MPB_ATTRIB_PM
;
3116 if (mpb
->num_raid_devs
> 1 ||
3117 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
3118 version
= MPB_VERSION_ATTRIBS
;
3119 switch (get_imsm_raid_level(map
)) {
3120 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
3121 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
3122 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
3123 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
3126 if (map
->num_members
>= 5)
3127 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
3128 else if (dev
->status
== DEV_CLONE_N_GO
)
3129 version
= MPB_VERSION_CNG
;
3130 else if (get_imsm_raid_level(map
) == 5)
3131 version
= MPB_VERSION_RAID5
;
3132 else if (map
->num_members
>= 3)
3133 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
3134 else if (get_imsm_raid_level(map
) == 1)
3135 version
= MPB_VERSION_RAID1
;
3137 version
= MPB_VERSION_RAID0
;
3139 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
3143 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
3145 struct imsm_super
*mpb
= super
->anchor
;
3146 char *reason
= NULL
;
3149 if (strlen(name
) > MAX_RAID_SERIAL_LEN
)
3150 reason
= "must be 16 characters or less";
3152 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3153 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3155 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
3156 reason
= "already exists";
3161 if (reason
&& !quiet
)
3162 fprintf(stderr
, Name
": imsm volume name %s\n", reason
);
3167 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
3168 unsigned long long size
, char *name
,
3169 char *homehost
, int *uuid
)
3171 /* We are creating a volume inside a pre-existing container.
3172 * so st->sb is already set.
3174 struct intel_super
*super
= st
->sb
;
3175 struct imsm_super
*mpb
= super
->anchor
;
3176 struct intel_dev
*dv
;
3177 struct imsm_dev
*dev
;
3178 struct imsm_vol
*vol
;
3179 struct imsm_map
*map
;
3180 int idx
= mpb
->num_raid_devs
;
3182 unsigned long long array_blocks
;
3183 size_t size_old
, size_new
;
3184 __u32 num_data_stripes
;
3186 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
3187 fprintf(stderr
, Name
": This imsm-container already has the "
3188 "maximum of %d volumes\n", super
->orom
->vpa
);
3192 /* ensure the mpb is large enough for the new data */
3193 size_old
= __le32_to_cpu(mpb
->mpb_size
);
3194 size_new
= disks_to_mpb_size(info
->nr_disks
);
3195 if (size_new
> size_old
) {
3197 size_t size_round
= ROUND_UP(size_new
, 512);
3199 if (posix_memalign(&mpb_new
, 512, size_round
) != 0) {
3200 fprintf(stderr
, Name
": could not allocate new mpb\n");
3203 memcpy(mpb_new
, mpb
, size_old
);
3206 super
->anchor
= mpb_new
;
3207 mpb
->mpb_size
= __cpu_to_le32(size_new
);
3208 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
3210 super
->current_vol
= idx
;
3211 /* when creating the first raid device in this container set num_disks
3212 * to zero, i.e. delete this spare and add raid member devices in
3213 * add_to_super_imsm_volume()
3215 if (super
->current_vol
== 0)
3218 if (!check_name(super
, name
, 0))
3220 dv
= malloc(sizeof(*dv
));
3222 fprintf(stderr
, Name
": failed to allocate device list entry\n");
3225 dev
= malloc(sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
3228 fprintf(stderr
, Name
": could not allocate raid device\n");
3231 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
3232 if (info
->level
== 1)
3233 array_blocks
= info_to_blocks_per_member(info
);
3235 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
3236 info
->layout
, info
->chunk_size
,
3238 /* round array size down to closest MB */
3239 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
3241 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
3242 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
3243 dev
->status
= __cpu_to_le32(0);
3244 dev
->reserved_blocks
= __cpu_to_le32(0);
3246 vol
->migr_state
= 0;
3247 set_migr_type(dev
, MIGR_INIT
);
3249 vol
->curr_migr_unit
= 0;
3250 map
= get_imsm_map(dev
, 0);
3251 map
->pba_of_lba0
= __cpu_to_le32(super
->create_offset
);
3252 map
->blocks_per_member
= __cpu_to_le32(info_to_blocks_per_member(info
));
3253 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
3254 map
->failed_disk_num
= ~0;
3255 map
->map_state
= info
->level
? IMSM_T_STATE_UNINITIALIZED
:
3256 IMSM_T_STATE_NORMAL
;
3259 if (info
->level
== 1 && info
->raid_disks
> 2) {
3262 fprintf(stderr
, Name
": imsm does not support more than 2 disks"
3263 "in a raid1 volume\n");
3267 map
->raid_level
= info
->level
;
3268 if (info
->level
== 10) {
3269 map
->raid_level
= 1;
3270 map
->num_domains
= info
->raid_disks
/ 2;
3271 } else if (info
->level
== 1)
3272 map
->num_domains
= info
->raid_disks
;
3274 map
->num_domains
= 1;
3276 num_data_stripes
= info_to_num_data_stripes(info
, map
->num_domains
);
3277 map
->num_data_stripes
= __cpu_to_le32(num_data_stripes
);
3279 map
->num_members
= info
->raid_disks
;
3280 for (i
= 0; i
< map
->num_members
; i
++) {
3281 /* initialized in add_to_super */
3282 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
3284 mpb
->num_raid_devs
++;
3287 dv
->index
= super
->current_vol
;
3288 dv
->next
= super
->devlist
;
3289 super
->devlist
= dv
;
3291 imsm_update_version_info(super
);
3296 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
3297 unsigned long long size
, char *name
,
3298 char *homehost
, int *uuid
)
3300 /* This is primarily called by Create when creating a new array.
3301 * We will then get add_to_super called for each component, and then
3302 * write_init_super called to write it out to each device.
3303 * For IMSM, Create can create on fresh devices or on a pre-existing
3305 * To create on a pre-existing array a different method will be called.
3306 * This one is just for fresh drives.
3308 struct intel_super
*super
;
3309 struct imsm_super
*mpb
;
3314 return init_super_imsm_volume(st
, info
, size
, name
, homehost
, uuid
);
3317 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
3321 super
= alloc_super();
3322 if (super
&& posix_memalign(&super
->buf
, 512, mpb_size
) != 0) {
3327 fprintf(stderr
, Name
3328 ": %s could not allocate superblock\n", __func__
);
3331 memset(super
->buf
, 0, mpb_size
);
3333 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
3337 /* zeroing superblock */
3341 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
3343 version
= (char *) mpb
->sig
;
3344 strcpy(version
, MPB_SIGNATURE
);
3345 version
+= strlen(MPB_SIGNATURE
);
3346 strcpy(version
, MPB_VERSION_RAID0
);
3352 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
3353 int fd
, char *devname
)
3355 struct intel_super
*super
= st
->sb
;
3356 struct imsm_super
*mpb
= super
->anchor
;
3358 struct imsm_dev
*dev
;
3359 struct imsm_map
*map
;
3362 dev
= get_imsm_dev(super
, super
->current_vol
);
3363 map
= get_imsm_map(dev
, 0);
3365 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
3366 fprintf(stderr
, Name
": %s: Cannot add spare devices to IMSM volume\n",
3372 /* we're doing autolayout so grab the pre-marked (in
3373 * validate_geometry) raid_disk
3375 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3376 if (dl
->raiddisk
== dk
->raid_disk
)
3379 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3380 if (dl
->major
== dk
->major
&&
3381 dl
->minor
== dk
->minor
)
3386 fprintf(stderr
, Name
": %s is not a member of the same container\n", devname
);
3390 /* add a pristine spare to the metadata */
3391 if (dl
->index
< 0) {
3392 dl
->index
= super
->anchor
->num_disks
;
3393 super
->anchor
->num_disks
++;
3395 /* Check the device has not already been added */
3396 slot
= get_imsm_disk_slot(map
, dl
->index
);
3398 (get_imsm_ord_tbl_ent(dev
, slot
) & IMSM_ORD_REBUILD
) == 0) {
3399 fprintf(stderr
, Name
": %s has been included in this array twice\n",
3403 set_imsm_ord_tbl_ent(map
, dk
->number
, dl
->index
);
3404 dl
->disk
.status
= CONFIGURED_DISK
;
3406 /* if we are creating the first raid device update the family number */
3407 if (super
->current_vol
== 0) {
3409 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
3410 struct imsm_disk
*_disk
= __get_imsm_disk(mpb
, dl
->index
);
3412 if (!_dev
|| !_disk
) {
3413 fprintf(stderr
, Name
": BUG mpb setup error\n");
3419 sum
+= __gen_imsm_checksum(mpb
);
3420 mpb
->family_num
= __cpu_to_le32(sum
);
3421 mpb
->orig_family_num
= mpb
->family_num
;
3427 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
3428 int fd
, char *devname
)
3430 struct intel_super
*super
= st
->sb
;
3432 unsigned long long size
;
3437 /* if we are on an RAID enabled platform check that the disk is
3438 * attached to the raid controller
3440 if (super
->hba
&& !disk_attached_to_hba(fd
, super
->hba
)) {
3442 Name
": %s is not attached to the raid controller: %s\n",
3443 devname
? : "disk", super
->hba
);
3447 if (super
->current_vol
>= 0)
3448 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
3451 dd
= malloc(sizeof(*dd
));
3454 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
3457 memset(dd
, 0, sizeof(*dd
));
3458 dd
->major
= major(stb
.st_rdev
);
3459 dd
->minor
= minor(stb
.st_rdev
);
3461 dd
->devname
= devname
? strdup(devname
) : NULL
;
3464 dd
->action
= DISK_ADD
;
3465 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
3468 Name
": failed to retrieve scsi serial, aborting\n");
3473 get_dev_size(fd
, NULL
, &size
);
3475 serialcpy(dd
->disk
.serial
, dd
->serial
);
3476 dd
->disk
.total_blocks
= __cpu_to_le32(size
);
3477 dd
->disk
.status
= SPARE_DISK
;
3478 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
3479 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
3481 dd
->disk
.scsi_id
= __cpu_to_le32(0);
3483 if (st
->update_tail
) {
3484 dd
->next
= super
->disk_mgmt_list
;
3485 super
->disk_mgmt_list
= dd
;
3487 dd
->next
= super
->disks
;
3495 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
3497 struct intel_super
*super
= st
->sb
;
3500 /* remove from super works only in mdmon - for communication
3501 * manager - monitor. Check if communication memory buffer
3504 if (!st
->update_tail
) {
3506 Name
": %s shall be used in mdmon context only"
3507 "(line %d).\n", __func__
, __LINE__
);
3510 dd
= malloc(sizeof(*dd
));
3513 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
3516 memset(dd
, 0, sizeof(*dd
));
3517 dd
->major
= dk
->major
;
3518 dd
->minor
= dk
->minor
;
3521 dd
->disk
.status
= SPARE_DISK
;
3522 dd
->action
= DISK_REMOVE
;
3524 dd
->next
= super
->disk_mgmt_list
;
3525 super
->disk_mgmt_list
= dd
;
3531 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
3535 struct imsm_super anchor
;
3536 } spare_record
__attribute__ ((aligned(512)));
3538 /* spare records have their own family number and do not have any defined raid
3541 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
3543 struct imsm_super
*mpb
= super
->anchor
;
3544 struct imsm_super
*spare
= &spare_record
.anchor
;
3548 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
)),
3549 spare
->generation_num
= __cpu_to_le32(1UL),
3550 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
3551 spare
->num_disks
= 1,
3552 spare
->num_raid_devs
= 0,
3553 spare
->cache_size
= mpb
->cache_size
,
3554 spare
->pwr_cycle_count
= __cpu_to_le32(1),
3556 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
3557 MPB_SIGNATURE MPB_VERSION_RAID0
);
3559 for (d
= super
->disks
; d
; d
= d
->next
) {
3563 spare
->disk
[0] = d
->disk
;
3564 sum
= __gen_imsm_checksum(spare
);
3565 spare
->family_num
= __cpu_to_le32(sum
);
3566 spare
->orig_family_num
= 0;
3567 sum
= __gen_imsm_checksum(spare
);
3568 spare
->check_sum
= __cpu_to_le32(sum
);
3570 if (store_imsm_mpb(d
->fd
, spare
)) {
3571 fprintf(stderr
, "%s: failed for device %d:%d %s\n",
3572 __func__
, d
->major
, d
->minor
, strerror(errno
));
3584 static int write_super_imsm(struct supertype
*st
, int doclose
)
3586 struct intel_super
*super
= st
->sb
;
3587 struct imsm_super
*mpb
= super
->anchor
;
3593 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
3596 /* 'generation' is incremented everytime the metadata is written */
3597 generation
= __le32_to_cpu(mpb
->generation_num
);
3599 mpb
->generation_num
= __cpu_to_le32(generation
);
3601 /* fix up cases where previous mdadm releases failed to set
3604 if (mpb
->orig_family_num
== 0)
3605 mpb
->orig_family_num
= mpb
->family_num
;
3607 for (d
= super
->disks
; d
; d
= d
->next
) {
3611 mpb
->disk
[d
->index
] = d
->disk
;
3615 for (d
= super
->missing
; d
; d
= d
->next
) {
3616 mpb
->disk
[d
->index
] = d
->disk
;
3619 mpb
->num_disks
= num_disks
;
3620 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
3622 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3623 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
3624 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
3626 imsm_copy_dev(dev
, dev2
);
3627 mpb_size
+= sizeof_imsm_dev(dev
, 0);
3630 mpb_size
+= __le32_to_cpu(mpb
->bbm_log_size
);
3631 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
3633 /* recalculate checksum */
3634 sum
= __gen_imsm_checksum(mpb
);
3635 mpb
->check_sum
= __cpu_to_le32(sum
);
3637 /* write the mpb for disks that compose raid devices */
3638 for (d
= super
->disks
; d
; d
= d
->next
) {
3641 if (store_imsm_mpb(d
->fd
, mpb
))
3642 fprintf(stderr
, "%s: failed for device %d:%d %s\n",
3643 __func__
, d
->major
, d
->minor
, strerror(errno
));
3651 return write_super_imsm_spares(super
, doclose
);
3657 static int create_array(struct supertype
*st
, int dev_idx
)
3660 struct imsm_update_create_array
*u
;
3661 struct intel_super
*super
= st
->sb
;
3662 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
3663 struct imsm_map
*map
= get_imsm_map(dev
, 0);
3664 struct disk_info
*inf
;
3665 struct imsm_disk
*disk
;
3668 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
3669 sizeof(*inf
) * map
->num_members
;
3672 fprintf(stderr
, "%s: failed to allocate update buffer\n",
3677 u
->type
= update_create_array
;
3678 u
->dev_idx
= dev_idx
;
3679 imsm_copy_dev(&u
->dev
, dev
);
3680 inf
= get_disk_info(u
);
3681 for (i
= 0; i
< map
->num_members
; i
++) {
3682 int idx
= get_imsm_disk_idx(dev
, i
);
3684 disk
= get_imsm_disk(super
, idx
);
3685 serialcpy(inf
[i
].serial
, disk
->serial
);
3687 append_metadata_update(st
, u
, len
);
3692 static int mgmt_disk(struct supertype
*st
)
3694 struct intel_super
*super
= st
->sb
;
3696 struct imsm_update_add_remove_disk
*u
;
3698 if (!super
->disk_mgmt_list
)
3704 fprintf(stderr
, "%s: failed to allocate update buffer\n",
3709 u
->type
= update_add_remove_disk
;
3710 append_metadata_update(st
, u
, len
);
3715 static int write_init_super_imsm(struct supertype
*st
)
3717 struct intel_super
*super
= st
->sb
;
3718 int current_vol
= super
->current_vol
;
3720 /* we are done with current_vol reset it to point st at the container */
3721 super
->current_vol
= -1;
3723 if (st
->update_tail
) {
3724 /* queue the recently created array / added disk
3725 * as a metadata update */
3729 /* determine if we are creating a volume or adding a disk */
3730 if (current_vol
< 0) {
3731 /* in the mgmt (add/remove) disk case we are running
3732 * in mdmon context, so don't close fd's
3734 return mgmt_disk(st
);
3736 rv
= create_array(st
, current_vol
);
3738 for (d
= super
->disks
; d
; d
= d
->next
) {
3746 for (d
= super
->disks
; d
; d
= d
->next
)
3747 Kill(d
->devname
, NULL
, 0, 1, 1);
3748 return write_super_imsm(st
, 1);
3753 static int store_super_imsm(struct supertype
*st
, int fd
)
3755 struct intel_super
*super
= st
->sb
;
3756 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
3762 return store_imsm_mpb(fd
, mpb
);
3768 static int imsm_bbm_log_size(struct imsm_super
*mpb
)
3770 return __le32_to_cpu(mpb
->bbm_log_size
);
3774 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
3775 int layout
, int raiddisks
, int chunk
,
3776 unsigned long long size
, char *dev
,
3777 unsigned long long *freesize
,
3781 unsigned long long ldsize
;
3782 const struct imsm_orom
*orom
;
3784 if (level
!= LEVEL_CONTAINER
)
3789 if (check_env("IMSM_NO_PLATFORM"))
3792 orom
= find_imsm_orom();
3793 if (orom
&& raiddisks
> orom
->tds
) {
3795 fprintf(stderr
, Name
": %d exceeds maximum number of"
3796 " platform supported disks: %d\n",
3797 raiddisks
, orom
->tds
);
3801 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
3804 fprintf(stderr
, Name
": imsm: Cannot open %s: %s\n",
3805 dev
, strerror(errno
));
3808 if (!get_dev_size(fd
, dev
, &ldsize
)) {
3814 *freesize
= avail_size_imsm(st
, ldsize
>> 9);
3819 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
3821 const unsigned long long base_start
= e
[*idx
].start
;
3822 unsigned long long end
= base_start
+ e
[*idx
].size
;
3825 if (base_start
== end
)
3829 for (i
= *idx
; i
< num_extents
; i
++) {
3830 /* extend overlapping extents */
3831 if (e
[i
].start
>= base_start
&&
3832 e
[i
].start
<= end
) {
3835 if (e
[i
].start
+ e
[i
].size
> end
)
3836 end
= e
[i
].start
+ e
[i
].size
;
3837 } else if (e
[i
].start
> end
) {
3843 return end
- base_start
;
3846 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
3848 /* build a composite disk with all known extents and generate a new
3849 * 'maxsize' given the "all disks in an array must share a common start
3850 * offset" constraint
3852 struct extent
*e
= calloc(sum_extents
, sizeof(*e
));
3856 unsigned long long pos
;
3857 unsigned long long start
= 0;
3858 unsigned long long maxsize
;
3859 unsigned long reserve
;
3864 /* coalesce and sort all extents. also, check to see if we need to
3865 * reserve space between member arrays
3868 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
3871 for (i
= 0; i
< dl
->extent_cnt
; i
++)
3874 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
3879 while (i
< sum_extents
) {
3880 e
[j
].start
= e
[i
].start
;
3881 e
[j
].size
= find_size(e
, &i
, sum_extents
);
3883 if (e
[j
-1].size
== 0)
3892 unsigned long long esize
;
3894 esize
= e
[i
].start
- pos
;
3895 if (esize
>= maxsize
) {
3900 pos
= e
[i
].start
+ e
[i
].size
;
3902 } while (e
[i
-1].size
);
3908 /* FIXME assumes volume at offset 0 is the first volume in a
3911 if (start_extent
> 0)
3912 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
3916 if (maxsize
< reserve
)
3919 super
->create_offset
= ~((__u32
) 0);
3920 if (start
+ reserve
> super
->create_offset
)
3921 return 0; /* start overflows create_offset */
3922 super
->create_offset
= start
+ reserve
;
3924 return maxsize
- reserve
;
3927 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
3929 if (level
< 0 || level
== 6 || level
== 4)
3932 /* if we have an orom prevent invalid raid levels */
3935 case 0: return imsm_orom_has_raid0(orom
);
3938 return imsm_orom_has_raid1e(orom
);
3939 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
3940 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
3941 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
3944 return 1; /* not on an Intel RAID platform so anything goes */
3949 #define pr_vrb(fmt, arg...) (void) (verbose && fprintf(stderr, Name fmt, ##arg))
3951 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
3952 int raiddisks
, int chunk
, int verbose
)
3954 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
3955 pr_vrb(": platform does not support raid%d with %d disk%s\n",
3956 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
3959 if (super
->orom
&& level
!= 1 &&
3960 !imsm_orom_has_chunk(super
->orom
, chunk
)) {
3961 pr_vrb(": platform does not support a chunk size of: %d\n", chunk
);
3964 if (layout
!= imsm_level_to_layout(level
)) {
3966 pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
3967 else if (level
== 10)
3968 pr_vrb(": imsm raid 10 only supports the n2 layout\n");
3970 pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
3978 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
3979 * FIX ME add ahci details
3981 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
3982 int layout
, int raiddisks
, int chunk
,
3983 unsigned long long size
, char *dev
,
3984 unsigned long long *freesize
,
3988 struct intel_super
*super
= st
->sb
;
3989 struct imsm_super
*mpb
= super
->anchor
;
3991 unsigned long long pos
= 0;
3992 unsigned long long maxsize
;
3996 /* We must have the container info already read in. */
4000 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, verbose
))
4004 /* General test: make sure there is space for
4005 * 'raiddisks' device extents of size 'size' at a given
4008 unsigned long long minsize
= size
;
4009 unsigned long long start_offset
= MaxSector
;
4012 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
4013 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
4018 e
= get_extents(super
, dl
);
4021 unsigned long long esize
;
4022 esize
= e
[i
].start
- pos
;
4023 if (esize
>= minsize
)
4025 if (found
&& start_offset
== MaxSector
) {
4028 } else if (found
&& pos
!= start_offset
) {
4032 pos
= e
[i
].start
+ e
[i
].size
;
4034 } while (e
[i
-1].size
);
4039 if (dcnt
< raiddisks
) {
4041 fprintf(stderr
, Name
": imsm: Not enough "
4042 "devices with space for this array "
4050 /* This device must be a member of the set */
4051 if (stat(dev
, &stb
) < 0)
4053 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
4055 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
4056 if (dl
->major
== (int)major(stb
.st_rdev
) &&
4057 dl
->minor
== (int)minor(stb
.st_rdev
))
4062 fprintf(stderr
, Name
": %s is not in the "
4063 "same imsm set\n", dev
);
4065 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
4066 /* If a volume is present then the current creation attempt
4067 * cannot incorporate new spares because the orom may not
4068 * understand this configuration (all member disks must be
4069 * members of each array in the container).
4071 fprintf(stderr
, Name
": %s is a spare and a volume"
4072 " is already defined for this container\n", dev
);
4073 fprintf(stderr
, Name
": The option-rom requires all member"
4074 " disks to be a member of all volumes\n");
4078 /* retrieve the largest free space block */
4079 e
= get_extents(super
, dl
);
4084 unsigned long long esize
;
4086 esize
= e
[i
].start
- pos
;
4087 if (esize
>= maxsize
)
4089 pos
= e
[i
].start
+ e
[i
].size
;
4091 } while (e
[i
-1].size
);
4096 fprintf(stderr
, Name
": unable to determine free space for: %s\n",
4100 if (maxsize
< size
) {
4102 fprintf(stderr
, Name
": %s not enough space (%llu < %llu)\n",
4103 dev
, maxsize
, size
);
4107 /* count total number of extents for merge */
4109 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4111 i
+= dl
->extent_cnt
;
4113 maxsize
= merge_extents(super
, i
);
4114 if (maxsize
< size
|| maxsize
== 0) {
4116 fprintf(stderr
, Name
": not enough space after merge (%llu < %llu)\n",
4121 *freesize
= maxsize
;
4126 static int reserve_space(struct supertype
*st
, int raiddisks
,
4127 unsigned long long size
, int chunk
,
4128 unsigned long long *freesize
)
4130 struct intel_super
*super
= st
->sb
;
4131 struct imsm_super
*mpb
= super
->anchor
;
4136 unsigned long long maxsize
;
4137 unsigned long long minsize
;
4141 /* find the largest common start free region of the possible disks */
4145 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
4151 /* don't activate new spares if we are orom constrained
4152 * and there is already a volume active in the container
4154 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
4157 e
= get_extents(super
, dl
);
4160 for (i
= 1; e
[i
-1].size
; i
++)
4168 maxsize
= merge_extents(super
, extent_cnt
);
4173 if (cnt
< raiddisks
||
4174 (super
->orom
&& used
&& used
!= raiddisks
) ||
4175 maxsize
< minsize
||
4177 fprintf(stderr
, Name
": not enough devices with space to create array.\n");
4178 return 0; /* No enough free spaces large enough */
4190 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4192 dl
->raiddisk
= cnt
++;
4199 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
4200 int raiddisks
, int chunk
, unsigned long long size
,
4201 char *dev
, unsigned long long *freesize
,
4208 /* if given unused devices create a container
4209 * if given given devices in a container create a member volume
4211 if (level
== LEVEL_CONTAINER
) {
4212 /* Must be a fresh device to add to a container */
4213 return validate_geometry_imsm_container(st
, level
, layout
,
4214 raiddisks
, chunk
, size
,
4220 if (st
->sb
&& freesize
) {
4221 /* we are being asked to automatically layout a
4222 * new volume based on the current contents of
4223 * the container. If the the parameters can be
4224 * satisfied reserve_space will record the disks,
4225 * start offset, and size of the volume to be
4226 * created. add_to_super and getinfo_super
4227 * detect when autolayout is in progress.
4229 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
4233 return reserve_space(st
, raiddisks
, size
, chunk
, freesize
);
4238 /* creating in a given container */
4239 return validate_geometry_imsm_volume(st
, level
, layout
,
4240 raiddisks
, chunk
, size
,
4241 dev
, freesize
, verbose
);
4244 /* This device needs to be a device in an 'imsm' container */
4245 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
4249 Name
": Cannot create this array on device %s\n",
4254 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
4256 fprintf(stderr
, Name
": Cannot open %s: %s\n",
4257 dev
, strerror(errno
));
4260 /* Well, it is in use by someone, maybe an 'imsm' container. */
4261 cfd
= open_container(fd
);
4265 fprintf(stderr
, Name
": Cannot use %s: It is busy\n",
4269 sra
= sysfs_read(cfd
, 0, GET_VERSION
);
4270 if (sra
&& sra
->array
.major_version
== -1 &&
4271 strcmp(sra
->text_version
, "imsm") == 0)
4275 /* This is a member of a imsm container. Load the container
4276 * and try to create a volume
4278 struct intel_super
*super
;
4280 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
) == 0) {
4282 st
->container_dev
= fd2devnum(cfd
);
4284 return validate_geometry_imsm_volume(st
, level
, layout
,
4292 fprintf(stderr
, Name
": failed container membership check\n");
4298 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
4300 struct intel_super
*super
= st
->sb
;
4302 if (level
&& *level
== UnSet
)
4303 *level
= LEVEL_CONTAINER
;
4305 if (level
&& layout
&& *layout
== UnSet
)
4306 *layout
= imsm_level_to_layout(*level
);
4308 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0) &&
4309 super
&& super
->orom
)
4310 *chunk
= imsm_orom_default_chunk(super
->orom
);
4313 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
4315 static int kill_subarray_imsm(struct supertype
*st
)
4317 /* remove the subarray currently referenced by ->current_vol */
4319 struct intel_dev
**dp
;
4320 struct intel_super
*super
= st
->sb
;
4321 __u8 current_vol
= super
->current_vol
;
4322 struct imsm_super
*mpb
= super
->anchor
;
4324 if (super
->current_vol
< 0)
4326 super
->current_vol
= -1; /* invalidate subarray cursor */
4328 /* block deletions that would change the uuid of active subarrays
4330 * FIXME when immutable ids are available, but note that we'll
4331 * also need to fixup the invalidated/active subarray indexes in
4334 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4337 if (i
< current_vol
)
4339 sprintf(subarray
, "%u", i
);
4340 if (is_subarray_active(subarray
, st
->devname
)) {
4342 Name
": deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
4349 if (st
->update_tail
) {
4350 struct imsm_update_kill_array
*u
= malloc(sizeof(*u
));
4354 u
->type
= update_kill_array
;
4355 u
->dev_idx
= current_vol
;
4356 append_metadata_update(st
, u
, sizeof(*u
));
4361 for (dp
= &super
->devlist
; *dp
;)
4362 if ((*dp
)->index
== current_vol
) {
4365 handle_missing(super
, (*dp
)->dev
);
4366 if ((*dp
)->index
> current_vol
)
4371 /* no more raid devices, all active components are now spares,
4372 * but of course failed are still failed
4374 if (--mpb
->num_raid_devs
== 0) {
4377 for (d
= super
->disks
; d
; d
= d
->next
)
4378 if (d
->index
> -2) {
4380 d
->disk
.status
= SPARE_DISK
;
4384 super
->updates_pending
++;
4389 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
4390 char *update
, struct mddev_ident
*ident
)
4392 /* update the subarray currently referenced by ->current_vol */
4393 struct intel_super
*super
= st
->sb
;
4394 struct imsm_super
*mpb
= super
->anchor
;
4396 if (strcmp(update
, "name") == 0) {
4397 char *name
= ident
->name
;
4401 if (is_subarray_active(subarray
, st
->devname
)) {
4403 Name
": Unable to update name of active subarray\n");
4407 if (!check_name(super
, name
, 0))
4410 vol
= strtoul(subarray
, &ep
, 10);
4411 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
4414 if (st
->update_tail
) {
4415 struct imsm_update_rename_array
*u
= malloc(sizeof(*u
));
4419 u
->type
= update_rename_array
;
4421 snprintf((char *) u
->name
, MAX_RAID_SERIAL_LEN
, "%s", name
);
4422 append_metadata_update(st
, u
, sizeof(*u
));
4424 struct imsm_dev
*dev
;
4427 dev
= get_imsm_dev(super
, vol
);
4428 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
4429 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4430 dev
= get_imsm_dev(super
, i
);
4431 handle_missing(super
, dev
);
4433 super
->updates_pending
++;
4440 #endif /* MDASSEMBLE */
4442 static int is_gen_migration(struct imsm_dev
*dev
)
4444 if (!dev
->vol
.migr_state
)
4447 if (migr_type(dev
) == MIGR_GEN_MIGR
)
4453 static int is_rebuilding(struct imsm_dev
*dev
)
4455 struct imsm_map
*migr_map
;
4457 if (!dev
->vol
.migr_state
)
4460 if (migr_type(dev
) != MIGR_REBUILD
)
4463 migr_map
= get_imsm_map(dev
, 1);
4465 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
4471 static void update_recovery_start(struct imsm_dev
*dev
, struct mdinfo
*array
)
4473 struct mdinfo
*rebuild
= NULL
;
4477 if (!is_rebuilding(dev
))
4480 /* Find the rebuild target, but punt on the dual rebuild case */
4481 for (d
= array
->devs
; d
; d
= d
->next
)
4482 if (d
->recovery_start
== 0) {
4489 /* (?) none of the disks are marked with
4490 * IMSM_ORD_REBUILD, so assume they are missing and the
4491 * disk_ord_tbl was not correctly updated
4493 dprintf("%s: failed to locate out-of-sync disk\n", __func__
);
4497 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
4498 rebuild
->recovery_start
= units
* blocks_per_migr_unit(dev
);
4502 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
4504 /* Given a container loaded by load_super_imsm_all,
4505 * extract information about all the arrays into
4507 * If 'subarray' is given, just extract info about that array.
4509 * For each imsm_dev create an mdinfo, fill it in,
4510 * then look for matching devices in super->disks
4511 * and create appropriate device mdinfo.
4513 struct intel_super
*super
= st
->sb
;
4514 struct imsm_super
*mpb
= super
->anchor
;
4515 struct mdinfo
*rest
= NULL
;
4518 /* do not assemble arrays that might have bad blocks */
4519 if (imsm_bbm_log_size(super
->anchor
)) {
4520 fprintf(stderr
, Name
": BBM log found in metadata. "
4521 "Cannot activate array(s).\n");
4525 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4526 struct imsm_dev
*dev
;
4527 struct imsm_map
*map
;
4528 struct mdinfo
*this;
4533 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
4536 dev
= get_imsm_dev(super
, i
);
4537 map
= get_imsm_map(dev
, 0);
4539 /* do not publish arrays that are in the middle of an
4540 * unsupported migration
4542 if (dev
->vol
.migr_state
&&
4543 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
4544 fprintf(stderr
, Name
": cannot assemble volume '%.16s':"
4545 " unsupported migration in progress\n",
4550 this = malloc(sizeof(*this));
4552 fprintf(stderr
, Name
": failed to allocate %zu bytes\n",
4556 memset(this, 0, sizeof(*this));
4559 super
->current_vol
= i
;
4560 getinfo_super_imsm_volume(st
, this, NULL
);
4561 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
4562 unsigned long long recovery_start
;
4563 struct mdinfo
*info_d
;
4570 idx
= get_imsm_disk_idx(dev
, slot
);
4571 ord
= get_imsm_ord_tbl_ent(dev
, slot
);
4572 for (d
= super
->disks
; d
; d
= d
->next
)
4573 if (d
->index
== idx
)
4576 recovery_start
= MaxSector
;
4579 if (d
&& is_failed(&d
->disk
))
4581 if (ord
& IMSM_ORD_REBUILD
)
4585 * if we skip some disks the array will be assmebled degraded;
4586 * reset resync start to avoid a dirty-degraded
4587 * situation when performing the intial sync
4589 * FIXME handle dirty degraded
4591 if ((skip
|| recovery_start
== 0) && !dev
->vol
.dirty
)
4592 this->resync_start
= MaxSector
;
4596 info_d
= calloc(1, sizeof(*info_d
));
4598 fprintf(stderr
, Name
": failed to allocate disk"
4599 " for volume %.16s\n", dev
->volume
);
4600 info_d
= this->devs
;
4602 struct mdinfo
*d
= info_d
->next
;
4611 info_d
->next
= this->devs
;
4612 this->devs
= info_d
;
4614 info_d
->disk
.number
= d
->index
;
4615 info_d
->disk
.major
= d
->major
;
4616 info_d
->disk
.minor
= d
->minor
;
4617 info_d
->disk
.raid_disk
= slot
;
4618 info_d
->recovery_start
= recovery_start
;
4620 if (info_d
->recovery_start
== MaxSector
)
4621 this->array
.working_disks
++;
4623 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
4624 info_d
->data_offset
= __le32_to_cpu(map
->pba_of_lba0
);
4625 info_d
->component_size
= __le32_to_cpu(map
->blocks_per_member
);
4627 /* now that the disk list is up-to-date fixup recovery_start */
4628 update_recovery_start(dev
, this);
4636 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
, int failed
)
4638 struct imsm_map
*map
= get_imsm_map(dev
, 0);
4641 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
4642 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
4644 switch (get_imsm_raid_level(map
)) {
4646 return IMSM_T_STATE_FAILED
;
4649 if (failed
< map
->num_members
)
4650 return IMSM_T_STATE_DEGRADED
;
4652 return IMSM_T_STATE_FAILED
;
4657 * check to see if any mirrors have failed, otherwise we
4658 * are degraded. Even numbered slots are mirrored on
4662 /* gcc -Os complains that this is unused */
4663 int insync
= insync
;
4665 for (i
= 0; i
< map
->num_members
; i
++) {
4666 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
);
4667 int idx
= ord_to_idx(ord
);
4668 struct imsm_disk
*disk
;
4670 /* reset the potential in-sync count on even-numbered
4671 * slots. num_copies is always 2 for imsm raid10
4676 disk
= get_imsm_disk(super
, idx
);
4677 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
4680 /* no in-sync disks left in this mirror the
4684 return IMSM_T_STATE_FAILED
;
4687 return IMSM_T_STATE_DEGRADED
;
4691 return IMSM_T_STATE_DEGRADED
;
4693 return IMSM_T_STATE_FAILED
;
4699 return map
->map_state
;
4702 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
)
4706 struct imsm_disk
*disk
;
4707 struct imsm_map
*map
= get_imsm_map(dev
, 0);
4708 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
);
4712 /* at the beginning of migration we set IMSM_ORD_REBUILD on
4713 * disks that are being rebuilt. New failures are recorded to
4714 * map[0]. So we look through all the disks we started with and
4715 * see if any failures are still present, or if any new ones
4718 * FIXME add support for online capacity expansion and
4719 * raid-level-migration
4721 for (i
= 0; i
< prev
->num_members
; i
++) {
4722 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
4723 ord
|= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
4724 idx
= ord_to_idx(ord
);
4726 disk
= get_imsm_disk(super
, idx
);
4727 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
4735 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
4738 struct intel_super
*super
= c
->sb
;
4739 struct imsm_super
*mpb
= super
->anchor
;
4741 if (atoi(inst
) >= mpb
->num_raid_devs
) {
4742 fprintf(stderr
, "%s: subarry index %d, out of range\n",
4743 __func__
, atoi(inst
));
4747 dprintf("imsm: open_new %s\n", inst
);
4748 a
->info
.container_member
= atoi(inst
);
4752 static int is_resyncing(struct imsm_dev
*dev
)
4754 struct imsm_map
*migr_map
;
4756 if (!dev
->vol
.migr_state
)
4759 if (migr_type(dev
) == MIGR_INIT
||
4760 migr_type(dev
) == MIGR_REPAIR
)
4763 migr_map
= get_imsm_map(dev
, 1);
4765 if (migr_map
->map_state
== IMSM_T_STATE_NORMAL
)
4771 /* return true if we recorded new information */
4772 static int mark_failure(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
4776 struct imsm_map
*map
;
4778 /* new failures are always set in map[0] */
4779 map
= get_imsm_map(dev
, 0);
4781 slot
= get_imsm_disk_slot(map
, idx
);
4785 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
4786 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
4789 disk
->status
|= FAILED_DISK
;
4790 disk
->status
&= ~CONFIGURED_DISK
;
4791 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
4792 if (map
->failed_disk_num
== 0xff)
4793 map
->failed_disk_num
= slot
;
4797 static void mark_missing(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
4799 mark_failure(dev
, disk
, idx
);
4801 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
4804 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
4805 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
4808 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
4814 if (!super
->missing
)
4816 failed
= imsm_count_failed(super
, dev
);
4817 map_state
= imsm_check_degraded(super
, dev
, failed
);
4819 dprintf("imsm: mark missing\n");
4820 end_migration(dev
, map_state
);
4821 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
4822 mark_missing(dev
, &dl
->disk
, dl
->index
);
4823 super
->updates_pending
++;
4826 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
4828 /* Handle dirty -> clean transititions and resync. Degraded and rebuild
4829 * states are handled in imsm_set_disk() with one exception, when a
4830 * resync is stopped due to a new failure this routine will set the
4831 * 'degraded' state for the array.
4833 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
4835 int inst
= a
->info
.container_member
;
4836 struct intel_super
*super
= a
->container
->sb
;
4837 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
4838 struct imsm_map
*map
= get_imsm_map(dev
, 0);
4839 int failed
= imsm_count_failed(super
, dev
);
4840 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
);
4841 __u32 blocks_per_unit
;
4843 /* before we activate this array handle any missing disks */
4844 if (consistent
== 2)
4845 handle_missing(super
, dev
);
4847 if (consistent
== 2 &&
4848 (!is_resync_complete(&a
->info
) ||
4849 map_state
!= IMSM_T_STATE_NORMAL
||
4850 dev
->vol
.migr_state
))
4853 if (is_resync_complete(&a
->info
)) {
4854 /* complete intialization / resync,
4855 * recovery and interrupted recovery is completed in
4858 if (is_resyncing(dev
)) {
4859 dprintf("imsm: mark resync done\n");
4860 end_migration(dev
, map_state
);
4861 super
->updates_pending
++;
4862 a
->last_checkpoint
= 0;
4864 } else if (!is_resyncing(dev
) && !failed
) {
4865 /* mark the start of the init process if nothing is failed */
4866 dprintf("imsm: mark resync start\n");
4867 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
4868 migrate(dev
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
4870 migrate(dev
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
4871 super
->updates_pending
++;
4874 /* check if we can update curr_migr_unit from resync_start, recovery_start */
4875 blocks_per_unit
= blocks_per_migr_unit(dev
);
4876 if (blocks_per_unit
) {
4880 units
= a
->last_checkpoint
/ blocks_per_unit
;
4883 /* check that we did not overflow 32-bits, and that
4884 * curr_migr_unit needs updating
4886 if (units32
== units
&&
4887 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
4888 dprintf("imsm: mark checkpoint (%u)\n", units32
);
4889 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
4890 super
->updates_pending
++;
4894 /* mark dirty / clean */
4895 if (dev
->vol
.dirty
!= !consistent
) {
4896 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
4901 super
->updates_pending
++;
4904 /* finalize online capacity expansion/reshape */
4905 if ((a
->curr_action
!= reshape
) &&
4906 (a
->prev_action
== reshape
)) {
4909 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
4910 imsm_set_disk(a
, mdi
->disk
.raid_disk
, mdi
->curr_state
);
4916 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
4918 int inst
= a
->info
.container_member
;
4919 struct intel_super
*super
= a
->container
->sb
;
4920 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
4921 struct imsm_map
*map
= get_imsm_map(dev
, 0);
4922 struct imsm_disk
*disk
;
4927 if (n
> map
->num_members
)
4928 fprintf(stderr
, "imsm: set_disk %d out of range 0..%d\n",
4929 n
, map
->num_members
- 1);
4934 dprintf("imsm: set_disk %d:%x\n", n
, state
);
4936 ord
= get_imsm_ord_tbl_ent(dev
, n
);
4937 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
4939 /* check for new failures */
4940 if (state
& DS_FAULTY
) {
4941 if (mark_failure(dev
, disk
, ord_to_idx(ord
)))
4942 super
->updates_pending
++;
4945 /* check if in_sync */
4946 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
4947 struct imsm_map
*migr_map
= get_imsm_map(dev
, 1);
4949 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
4950 super
->updates_pending
++;
4953 failed
= imsm_count_failed(super
, dev
);
4954 map_state
= imsm_check_degraded(super
, dev
, failed
);
4956 /* check if recovery complete, newly degraded, or failed */
4957 if (map_state
== IMSM_T_STATE_NORMAL
&& is_rebuilding(dev
)) {
4958 end_migration(dev
, map_state
);
4959 map
= get_imsm_map(dev
, 0);
4960 map
->failed_disk_num
= ~0;
4961 super
->updates_pending
++;
4962 a
->last_checkpoint
= 0;
4963 } else if (map_state
== IMSM_T_STATE_DEGRADED
&&
4964 map
->map_state
!= map_state
&&
4965 !dev
->vol
.migr_state
) {
4966 dprintf("imsm: mark degraded\n");
4967 map
->map_state
= map_state
;
4968 super
->updates_pending
++;
4969 a
->last_checkpoint
= 0;
4970 } else if (map_state
== IMSM_T_STATE_FAILED
&&
4971 map
->map_state
!= map_state
) {
4972 dprintf("imsm: mark failed\n");
4973 end_migration(dev
, map_state
);
4974 super
->updates_pending
++;
4975 a
->last_checkpoint
= 0;
4976 } else if (is_gen_migration(dev
)) {
4977 dprintf("imsm: Detected General Migration in state: ");
4978 if (map_state
== IMSM_T_STATE_NORMAL
) {
4979 end_migration(dev
, map_state
);
4980 map
= get_imsm_map(dev
, 0);
4981 map
->failed_disk_num
= ~0;
4982 dprintf("normal\n");
4984 if (map_state
== IMSM_T_STATE_DEGRADED
) {
4985 printf("degraded\n");
4986 end_migration(dev
, map_state
);
4988 dprintf("failed\n");
4990 map
->map_state
= map_state
;
4992 super
->updates_pending
++;
4996 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
4999 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
5000 unsigned long long dsize
;
5001 unsigned long long sectors
;
5003 get_dev_size(fd
, NULL
, &dsize
);
5005 if (mpb_size
> 512) {
5006 /* -1 to account for anchor */
5007 sectors
= mpb_sectors(mpb
) - 1;
5009 /* write the extended mpb to the sectors preceeding the anchor */
5010 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0)
5013 if ((unsigned long long)write(fd
, buf
+ 512, 512 * sectors
)
5018 /* first block is stored on second to last sector of the disk */
5019 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0)
5022 if (write(fd
, buf
, 512) != 512)
5028 static void imsm_sync_metadata(struct supertype
*container
)
5030 struct intel_super
*super
= container
->sb
;
5032 dprintf("sync metadata: %d\n", super
->updates_pending
);
5033 if (!super
->updates_pending
)
5036 write_super_imsm(container
, 0);
5038 super
->updates_pending
= 0;
5041 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
5043 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
5044 int i
= get_imsm_disk_idx(dev
, idx
);
5047 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5051 if (dl
&& is_failed(&dl
->disk
))
5055 dprintf("%s: found %x:%x\n", __func__
, dl
->major
, dl
->minor
);
5060 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
5061 struct active_array
*a
, int activate_new
,
5062 struct mdinfo
*additional_test_list
)
5064 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
5065 int idx
= get_imsm_disk_idx(dev
, slot
);
5066 struct imsm_super
*mpb
= super
->anchor
;
5067 struct imsm_map
*map
;
5068 unsigned long long pos
;
5073 __u32 array_start
= 0;
5074 __u32 array_end
= 0;
5076 struct mdinfo
*test_list
;
5078 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5079 /* If in this array, skip */
5080 for (d
= a
->info
.devs
; d
; d
= d
->next
)
5081 if (d
->state_fd
>= 0 &&
5082 d
->disk
.major
== dl
->major
&&
5083 d
->disk
.minor
== dl
->minor
) {
5084 dprintf("%x:%x already in array\n",
5085 dl
->major
, dl
->minor
);
5090 test_list
= additional_test_list
;
5092 if (test_list
->disk
.major
== dl
->major
&&
5093 test_list
->disk
.minor
== dl
->minor
) {
5094 dprintf("%x:%x already in additional test list\n",
5095 dl
->major
, dl
->minor
);
5098 test_list
= test_list
->next
;
5103 /* skip in use or failed drives */
5104 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
5106 dprintf("%x:%x status (failed: %d index: %d)\n",
5107 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
5111 /* skip pure spares when we are looking for partially
5112 * assimilated drives
5114 if (dl
->index
== -1 && !activate_new
)
5117 /* Does this unused device have the requisite free space?
5118 * It needs to be able to cover all member volumes
5120 ex
= get_extents(super
, dl
);
5122 dprintf("cannot get extents\n");
5125 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5126 dev
= get_imsm_dev(super
, i
);
5127 map
= get_imsm_map(dev
, 0);
5129 /* check if this disk is already a member of
5132 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
5138 array_start
= __le32_to_cpu(map
->pba_of_lba0
);
5139 array_end
= array_start
+
5140 __le32_to_cpu(map
->blocks_per_member
) - 1;
5143 /* check that we can start at pba_of_lba0 with
5144 * blocks_per_member of space
5146 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
5150 pos
= ex
[j
].start
+ ex
[j
].size
;
5152 } while (ex
[j
-1].size
);
5159 if (i
< mpb
->num_raid_devs
) {
5160 dprintf("%x:%x does not have %u to %u available\n",
5161 dl
->major
, dl
->minor
, array_start
, array_end
);
5172 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
5174 struct imsm_dev
*dev2
;
5175 struct imsm_map
*map
;
5181 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
5183 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
);
5184 if (state
== IMSM_T_STATE_FAILED
) {
5185 map
= get_imsm_map(dev2
, 0);
5188 for (slot
= 0; slot
< map
->num_members
; slot
++) {
5190 * Check if failed disks are deleted from intel
5191 * disk list or are marked to be deleted
5193 idx
= get_imsm_disk_idx(dev2
, slot
);
5194 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
5196 * Do not rebuild the array if failed disks
5197 * from failed sub-array are not removed from
5201 is_failed(&idisk
->disk
) &&
5202 (idisk
->action
!= DISK_REMOVE
))
5210 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
5211 struct metadata_update
**updates
)
5214 * Find a device with unused free space and use it to replace a
5215 * failed/vacant region in an array. We replace failed regions one a
5216 * array at a time. The result is that a new spare disk will be added
5217 * to the first failed array and after the monitor has finished
5218 * propagating failures the remainder will be consumed.
5220 * FIXME add a capability for mdmon to request spares from another
5224 struct intel_super
*super
= a
->container
->sb
;
5225 int inst
= a
->info
.container_member
;
5226 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
5227 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5228 int failed
= a
->info
.array
.raid_disks
;
5229 struct mdinfo
*rv
= NULL
;
5232 struct metadata_update
*mu
;
5234 struct imsm_update_activate_spare
*u
;
5239 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
5240 if ((d
->curr_state
& DS_FAULTY
) &&
5242 /* wait for Removal to happen */
5244 if (d
->state_fd
>= 0)
5248 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
5249 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
5250 if (imsm_check_degraded(super
, dev
, failed
) != IMSM_T_STATE_DEGRADED
)
5254 * If there are any failed disks check state of the other volume.
5255 * Block rebuild if the another one is failed until failed disks
5256 * are removed from container.
5259 dprintf("found failed disks in %s, check if there another"
5260 "failed sub-array.\n",
5262 /* check if states of the other volumes allow for rebuild */
5263 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
5265 allowed
= imsm_rebuild_allowed(a
->container
,
5273 /* For each slot, if it is not working, find a spare */
5274 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
5275 for (d
= a
->info
.devs
; d
; d
= d
->next
)
5276 if (d
->disk
.raid_disk
== i
)
5278 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
5279 if (d
&& (d
->state_fd
>= 0))
5283 * OK, this device needs recovery. Try to re-add the
5284 * previous occupant of this slot, if this fails see if
5285 * we can continue the assimilation of a spare that was
5286 * partially assimilated, finally try to activate a new
5289 dl
= imsm_readd(super
, i
, a
);
5291 dl
= imsm_add_spare(super
, i
, a
, 0, NULL
);
5293 dl
= imsm_add_spare(super
, i
, a
, 1, NULL
);
5297 /* found a usable disk with enough space */
5298 di
= malloc(sizeof(*di
));
5301 memset(di
, 0, sizeof(*di
));
5303 /* dl->index will be -1 in the case we are activating a
5304 * pristine spare. imsm_process_update() will create a
5305 * new index in this case. Once a disk is found to be
5306 * failed in all member arrays it is kicked from the
5309 di
->disk
.number
= dl
->index
;
5311 /* (ab)use di->devs to store a pointer to the device
5314 di
->devs
= (struct mdinfo
*) dl
;
5316 di
->disk
.raid_disk
= i
;
5317 di
->disk
.major
= dl
->major
;
5318 di
->disk
.minor
= dl
->minor
;
5320 di
->recovery_start
= 0;
5321 di
->data_offset
= __le32_to_cpu(map
->pba_of_lba0
);
5322 di
->component_size
= a
->info
.component_size
;
5323 di
->container_member
= inst
;
5324 super
->random
= random32();
5328 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
5329 i
, di
->data_offset
);
5335 /* No spares found */
5337 /* Now 'rv' has a list of devices to return.
5338 * Create a metadata_update record to update the
5339 * disk_ord_tbl for the array
5341 mu
= malloc(sizeof(*mu
));
5343 mu
->buf
= malloc(sizeof(struct imsm_update_activate_spare
) * num_spares
);
5344 if (mu
->buf
== NULL
) {
5351 struct mdinfo
*n
= rv
->next
;
5360 mu
->space_list
= NULL
;
5361 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
5362 mu
->next
= *updates
;
5363 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
5365 for (di
= rv
; di
; di
= di
->next
) {
5366 u
->type
= update_activate_spare
;
5367 u
->dl
= (struct dl
*) di
->devs
;
5369 u
->slot
= di
->disk
.raid_disk
;
5380 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
5382 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
5383 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5384 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, 0);
5385 struct disk_info
*inf
= get_disk_info(u
);
5386 struct imsm_disk
*disk
;
5390 for (i
= 0; i
< map
->num_members
; i
++) {
5391 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
));
5392 for (j
= 0; j
< new_map
->num_members
; j
++)
5393 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
5401 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
5403 struct dl
*dl
= NULL
;
5404 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5405 if ((dl
->major
== major
) && (dl
->minor
== minor
))
5410 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
5412 struct dl
*prev
= NULL
;
5416 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5417 if ((dl
->major
== major
) && (dl
->minor
== minor
)) {
5420 prev
->next
= dl
->next
;
5422 super
->disks
= dl
->next
;
5424 __free_imsm_disk(dl
);
5425 dprintf("%s: removed %x:%x\n",
5426 __func__
, major
, minor
);
5434 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
5436 static int add_remove_disk_update(struct intel_super
*super
)
5438 int check_degraded
= 0;
5439 struct dl
*disk
= NULL
;
5440 /* add/remove some spares to/from the metadata/contrainer */
5441 while (super
->disk_mgmt_list
) {
5442 struct dl
*disk_cfg
;
5444 disk_cfg
= super
->disk_mgmt_list
;
5445 super
->disk_mgmt_list
= disk_cfg
->next
;
5446 disk_cfg
->next
= NULL
;
5448 if (disk_cfg
->action
== DISK_ADD
) {
5449 disk_cfg
->next
= super
->disks
;
5450 super
->disks
= disk_cfg
;
5452 dprintf("%s: added %x:%x\n",
5453 __func__
, disk_cfg
->major
,
5455 } else if (disk_cfg
->action
== DISK_REMOVE
) {
5456 dprintf("Disk remove action processed: %x.%x\n",
5457 disk_cfg
->major
, disk_cfg
->minor
);
5458 disk
= get_disk_super(super
,
5462 /* store action status */
5463 disk
->action
= DISK_REMOVE
;
5464 /* remove spare disks only */
5465 if (disk
->index
== -1) {
5466 remove_disk_super(super
,
5471 /* release allocate disk structure */
5472 __free_imsm_disk(disk_cfg
);
5475 return check_degraded
;
5478 static void imsm_process_update(struct supertype
*st
,
5479 struct metadata_update
*update
)
5482 * crack open the metadata_update envelope to find the update record
5483 * update can be one of:
5484 * update_activate_spare - a spare device has replaced a failed
5485 * device in an array, update the disk_ord_tbl. If this disk is
5486 * present in all member arrays then also clear the SPARE_DISK
5489 struct intel_super
*super
= st
->sb
;
5490 struct imsm_super
*mpb
;
5491 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
5493 /* update requires a larger buf but the allocation failed */
5494 if (super
->next_len
&& !super
->next_buf
) {
5495 super
->next_len
= 0;
5499 if (super
->next_buf
) {
5500 memcpy(super
->next_buf
, super
->buf
, super
->len
);
5502 super
->len
= super
->next_len
;
5503 super
->buf
= super
->next_buf
;
5505 super
->next_len
= 0;
5506 super
->next_buf
= NULL
;
5509 mpb
= super
->anchor
;
5512 case update_reshape_container_disks
: {
5515 case update_activate_spare
: {
5516 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
5517 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
5518 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5519 struct imsm_map
*migr_map
;
5520 struct active_array
*a
;
5521 struct imsm_disk
*disk
;
5526 int victim
= get_imsm_disk_idx(dev
, u
->slot
);
5529 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5534 fprintf(stderr
, "error: imsm_activate_spare passed "
5535 "an unknown disk (index: %d)\n",
5540 super
->updates_pending
++;
5542 /* count failures (excluding rebuilds and the victim)
5543 * to determine map[0] state
5546 for (i
= 0; i
< map
->num_members
; i
++) {
5549 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
));
5550 if (!disk
|| is_failed(disk
))
5554 /* adding a pristine spare, assign a new index */
5555 if (dl
->index
< 0) {
5556 dl
->index
= super
->anchor
->num_disks
;
5557 super
->anchor
->num_disks
++;
5560 disk
->status
|= CONFIGURED_DISK
;
5561 disk
->status
&= ~SPARE_DISK
;
5564 to_state
= imsm_check_degraded(super
, dev
, failed
);
5565 map
->map_state
= IMSM_T_STATE_DEGRADED
;
5566 migrate(dev
, to_state
, MIGR_REBUILD
);
5567 migr_map
= get_imsm_map(dev
, 1);
5568 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
5569 set_imsm_ord_tbl_ent(migr_map
, u
->slot
, dl
->index
| IMSM_ORD_REBUILD
);
5571 /* update the family_num to mark a new container
5572 * generation, being careful to record the existing
5573 * family_num in orig_family_num to clean up after
5574 * earlier mdadm versions that neglected to set it.
5576 if (mpb
->orig_family_num
== 0)
5577 mpb
->orig_family_num
= mpb
->family_num
;
5578 mpb
->family_num
+= super
->random
;
5580 /* count arrays using the victim in the metadata */
5582 for (a
= st
->arrays
; a
; a
= a
->next
) {
5583 dev
= get_imsm_dev(super
, a
->info
.container_member
);
5584 map
= get_imsm_map(dev
, 0);
5586 if (get_imsm_disk_slot(map
, victim
) >= 0)
5590 /* delete the victim if it is no longer being
5596 /* We know that 'manager' isn't touching anything,
5597 * so it is safe to delete
5599 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
5600 if ((*dlp
)->index
== victim
)
5603 /* victim may be on the missing list */
5605 for (dlp
= &super
->missing
; *dlp
; dlp
= &(*dlp
)->next
)
5606 if ((*dlp
)->index
== victim
)
5608 imsm_delete(super
, dlp
, victim
);
5612 case update_create_array
: {
5613 /* someone wants to create a new array, we need to be aware of
5614 * a few races/collisions:
5615 * 1/ 'Create' called by two separate instances of mdadm
5616 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
5617 * devices that have since been assimilated via
5619 * In the event this update can not be carried out mdadm will
5620 * (FIX ME) notice that its update did not take hold.
5622 struct imsm_update_create_array
*u
= (void *) update
->buf
;
5623 struct intel_dev
*dv
;
5624 struct imsm_dev
*dev
;
5625 struct imsm_map
*map
, *new_map
;
5626 unsigned long long start
, end
;
5627 unsigned long long new_start
, new_end
;
5629 struct disk_info
*inf
;
5632 /* handle racing creates: first come first serve */
5633 if (u
->dev_idx
< mpb
->num_raid_devs
) {
5634 dprintf("%s: subarray %d already defined\n",
5635 __func__
, u
->dev_idx
);
5639 /* check update is next in sequence */
5640 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
5641 dprintf("%s: can not create array %d expected index %d\n",
5642 __func__
, u
->dev_idx
, mpb
->num_raid_devs
);
5646 new_map
= get_imsm_map(&u
->dev
, 0);
5647 new_start
= __le32_to_cpu(new_map
->pba_of_lba0
);
5648 new_end
= new_start
+ __le32_to_cpu(new_map
->blocks_per_member
);
5649 inf
= get_disk_info(u
);
5651 /* handle activate_spare versus create race:
5652 * check to make sure that overlapping arrays do not include
5655 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5656 dev
= get_imsm_dev(super
, i
);
5657 map
= get_imsm_map(dev
, 0);
5658 start
= __le32_to_cpu(map
->pba_of_lba0
);
5659 end
= start
+ __le32_to_cpu(map
->blocks_per_member
);
5660 if ((new_start
>= start
&& new_start
<= end
) ||
5661 (start
>= new_start
&& start
<= new_end
))
5666 if (disks_overlap(super
, i
, u
)) {
5667 dprintf("%s: arrays overlap\n", __func__
);
5672 /* check that prepare update was successful */
5673 if (!update
->space
) {
5674 dprintf("%s: prepare update failed\n", __func__
);
5678 /* check that all disks are still active before committing
5679 * changes. FIXME: could we instead handle this by creating a
5680 * degraded array? That's probably not what the user expects,
5681 * so better to drop this update on the floor.
5683 for (i
= 0; i
< new_map
->num_members
; i
++) {
5684 dl
= serial_to_dl(inf
[i
].serial
, super
);
5686 dprintf("%s: disk disappeared\n", __func__
);
5691 super
->updates_pending
++;
5693 /* convert spares to members and fixup ord_tbl */
5694 for (i
= 0; i
< new_map
->num_members
; i
++) {
5695 dl
= serial_to_dl(inf
[i
].serial
, super
);
5696 if (dl
->index
== -1) {
5697 dl
->index
= mpb
->num_disks
;
5699 dl
->disk
.status
|= CONFIGURED_DISK
;
5700 dl
->disk
.status
&= ~SPARE_DISK
;
5702 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
5707 update
->space
= NULL
;
5708 imsm_copy_dev(dev
, &u
->dev
);
5709 dv
->index
= u
->dev_idx
;
5710 dv
->next
= super
->devlist
;
5711 super
->devlist
= dv
;
5712 mpb
->num_raid_devs
++;
5714 imsm_update_version_info(super
);
5717 /* mdmon knows how to release update->space, but not
5718 * ((struct intel_dev *) update->space)->dev
5720 if (update
->space
) {
5726 case update_kill_array
: {
5727 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
5728 int victim
= u
->dev_idx
;
5729 struct active_array
*a
;
5730 struct intel_dev
**dp
;
5731 struct imsm_dev
*dev
;
5733 /* sanity check that we are not affecting the uuid of
5734 * active arrays, or deleting an active array
5736 * FIXME when immutable ids are available, but note that
5737 * we'll also need to fixup the invalidated/active
5738 * subarray indexes in mdstat
5740 for (a
= st
->arrays
; a
; a
= a
->next
)
5741 if (a
->info
.container_member
>= victim
)
5743 /* by definition if mdmon is running at least one array
5744 * is active in the container, so checking
5745 * mpb->num_raid_devs is just extra paranoia
5747 dev
= get_imsm_dev(super
, victim
);
5748 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
5749 dprintf("failed to delete subarray-%d\n", victim
);
5753 for (dp
= &super
->devlist
; *dp
;)
5754 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
5757 if ((*dp
)->index
> (unsigned)victim
)
5761 mpb
->num_raid_devs
--;
5762 super
->updates_pending
++;
5765 case update_rename_array
: {
5766 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
5767 char name
[MAX_RAID_SERIAL_LEN
+1];
5768 int target
= u
->dev_idx
;
5769 struct active_array
*a
;
5770 struct imsm_dev
*dev
;
5772 /* sanity check that we are not affecting the uuid of
5775 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
5776 name
[MAX_RAID_SERIAL_LEN
] = '\0';
5777 for (a
= st
->arrays
; a
; a
= a
->next
)
5778 if (a
->info
.container_member
== target
)
5780 dev
= get_imsm_dev(super
, u
->dev_idx
);
5781 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
5782 dprintf("failed to rename subarray-%d\n", target
);
5786 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
5787 super
->updates_pending
++;
5790 case update_add_remove_disk
: {
5791 /* we may be able to repair some arrays if disks are
5792 * being added, check teh status of add_remove_disk
5793 * if discs has been added.
5795 if (add_remove_disk_update(super
)) {
5796 struct active_array
*a
;
5798 super
->updates_pending
++;
5799 for (a
= st
->arrays
; a
; a
= a
->next
)
5800 a
->check_degraded
= 1;
5805 fprintf(stderr
, "error: unsuported process update type:"
5806 "(type: %d)\n", type
);
5810 static void imsm_prepare_update(struct supertype
*st
,
5811 struct metadata_update
*update
)
5814 * Allocate space to hold new disk entries, raid-device entries or a new
5815 * mpb if necessary. The manager synchronously waits for updates to
5816 * complete in the monitor, so new mpb buffers allocated here can be
5817 * integrated by the monitor thread without worrying about live pointers
5818 * in the manager thread.
5820 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
5821 struct intel_super
*super
= st
->sb
;
5822 struct imsm_super
*mpb
= super
->anchor
;
5827 case update_reshape_container_disks
: {
5830 case update_create_array
: {
5831 struct imsm_update_create_array
*u
= (void *) update
->buf
;
5832 struct intel_dev
*dv
;
5833 struct imsm_dev
*dev
= &u
->dev
;
5834 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5836 struct disk_info
*inf
;
5840 inf
= get_disk_info(u
);
5841 len
= sizeof_imsm_dev(dev
, 1);
5842 /* allocate a new super->devlist entry */
5843 dv
= malloc(sizeof(*dv
));
5845 dv
->dev
= malloc(len
);
5850 update
->space
= NULL
;
5854 /* count how many spares will be converted to members */
5855 for (i
= 0; i
< map
->num_members
; i
++) {
5856 dl
= serial_to_dl(inf
[i
].serial
, super
);
5858 /* hmm maybe it failed?, nothing we can do about
5863 if (count_memberships(dl
, super
) == 0)
5866 len
+= activate
* sizeof(struct imsm_disk
);
5873 /* check if we need a larger metadata buffer */
5874 if (super
->next_buf
)
5875 buf_len
= super
->next_len
;
5877 buf_len
= super
->len
;
5879 if (__le32_to_cpu(mpb
->mpb_size
) + len
> buf_len
) {
5880 /* ok we need a larger buf than what is currently allocated
5881 * if this allocation fails process_update will notice that
5882 * ->next_len is set and ->next_buf is NULL
5884 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + len
, 512);
5885 if (super
->next_buf
)
5886 free(super
->next_buf
);
5888 super
->next_len
= buf_len
;
5889 if (posix_memalign(&super
->next_buf
, 512, buf_len
) == 0)
5890 memset(super
->next_buf
, 0, buf_len
);
5892 super
->next_buf
= NULL
;
5896 /* must be called while manager is quiesced */
5897 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
5899 struct imsm_super
*mpb
= super
->anchor
;
5901 struct imsm_dev
*dev
;
5902 struct imsm_map
*map
;
5903 int i
, j
, num_members
;
5906 dprintf("%s: deleting device[%d] from imsm_super\n",
5909 /* shift all indexes down one */
5910 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
5911 if (iter
->index
> (int)index
)
5913 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
5914 if (iter
->index
> (int)index
)
5917 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5918 dev
= get_imsm_dev(super
, i
);
5919 map
= get_imsm_map(dev
, 0);
5920 num_members
= map
->num_members
;
5921 for (j
= 0; j
< num_members
; j
++) {
5922 /* update ord entries being careful not to propagate
5923 * ord-flags to the first map
5925 ord
= get_imsm_ord_tbl_ent(dev
, j
);
5927 if (ord_to_idx(ord
) <= index
)
5930 map
= get_imsm_map(dev
, 0);
5931 set_imsm_ord_tbl_ent(map
, j
, ord_to_idx(ord
- 1));
5932 map
= get_imsm_map(dev
, 1);
5934 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
5939 super
->updates_pending
++;
5941 struct dl
*dl
= *dlp
;
5943 *dlp
= (*dlp
)->next
;
5944 __free_imsm_disk(dl
);
5947 #endif /* MDASSEMBLE */
5949 static char disk_by_path
[] = "/dev/disk/by-path/";
5951 static const char *imsm_get_disk_controller_domain(const char *path
)
5953 struct sys_dev
*list
, *hba
= NULL
;
5954 char disk_path
[PATH_MAX
];
5958 list
= find_driver_devices("pci", "ahci");
5959 for (hba
= list
; hba
; hba
= hba
->next
)
5960 if (devpath_to_vendor(hba
->path
) == 0x8086)
5966 strncpy(disk_path
, disk_by_path
, PATH_MAX
- 1);
5967 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
5968 if (stat(disk_path
, &st
) == 0) {
5969 dpath
= devt_to_devpath(st
.st_rdev
);
5971 ahci
= path_attached_to_hba(dpath
, hba
->path
);
5974 dprintf("path: %s(%s) hba: %s attached: %d\n",
5975 path
, dpath
, (hba
) ? hba
->path
: "NULL", ahci
);
5976 free_sys_dev(&list
);
5983 static int imsm_find_array_minor_by_subdev(int subdev
, int container
, int *minor
)
5985 char subdev_name
[20];
5986 struct mdstat_ent
*mdstat
;
5988 sprintf(subdev_name
, "%d", subdev
);
5989 mdstat
= mdstat_by_subdev(subdev_name
, container
);
5993 *minor
= mdstat
->devnum
;
5994 free_mdstat(mdstat
);
5998 static int imsm_reshape_is_allowed_on_container(struct supertype
*st
,
5999 struct geo_params
*geo
,
6000 int *old_raid_disks
)
6003 struct mdinfo
*info
, *member
;
6004 int devices_that_can_grow
= 0;
6006 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): "
6007 "st->devnum = (%i)\n",
6010 if (geo
->size
!= -1 ||
6011 geo
->level
!= UnSet
||
6012 geo
->layout
!= UnSet
||
6013 geo
->chunksize
!= 0 ||
6014 geo
->raid_disks
== UnSet
) {
6015 dprintf("imsm: Container operation is allowed for "
6016 "raid disks number change only.\n");
6020 info
= container_content_imsm(st
, NULL
);
6021 for (member
= info
; member
; member
= member
->next
) {
6025 dprintf("imsm: checking device_num: %i\n",
6026 member
->container_member
);
6028 if (geo
->raid_disks
< member
->array
.raid_disks
) {
6029 /* we work on container for Online Capacity Expansion
6030 * only so raid_disks has to grow
6032 dprintf("imsm: for container operation raid disks "
6033 "increase is required\n");
6037 if ((info
->array
.level
!= 0) &&
6038 (info
->array
.level
!= 5)) {
6039 /* we cannot use this container with other raid level
6041 dprintf("imsm: for container operation wrong"\
6042 " raid level (%i) detected\n",
6046 /* check for platform support
6047 * for this raid level configuration
6049 struct intel_super
*super
= st
->sb
;
6050 if (!is_raid_level_supported(super
->orom
,
6051 member
->array
.level
,
6053 dprintf("platform does not support raid%d with"\
6057 geo
->raid_disks
> 1 ? "s" : "");
6062 if (*old_raid_disks
&&
6063 info
->array
.raid_disks
!= *old_raid_disks
)
6065 *old_raid_disks
= info
->array
.raid_disks
;
6067 /* All raid5 and raid0 volumes in container
6068 * have to be ready for Online Capacity Expansion
6069 * so they need to be assembled. We have already
6070 * checked that no recovery etc is happening.
6072 result
= imsm_find_array_minor_by_subdev(member
->container_member
,
6076 dprintf("imsm: cannot find array\n");
6079 devices_that_can_grow
++;
6082 if (!member
&& devices_that_can_grow
)
6086 dprintf("\tContainer operation allowed\n");
6088 dprintf("\tError: %i\n", ret_val
);
6093 /* Function: get_spares_for_grow
6094 * Description: Allocates memory and creates list of spare devices
6095 * avaliable in container. Checks if spare drive size is acceptable.
6096 * Parameters: Pointer to the supertype structure
6097 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
6100 static struct mdinfo
*get_spares_for_grow(struct supertype
*st
)
6103 struct mdinfo
*disks
, *d
, **dp
;
6104 unsigned long long min_size
= min_acceptable_spare_size_imsm(st
);
6106 /* get list of alldisks in container */
6107 disks
= getinfo_super_disks_imsm(st
);
6111 /* find spare devices on the list */
6113 disks
->array
.spare_disks
= 0;
6117 if (d
->disk
.state
== 0) {
6118 /* check if size is acceptable */
6119 unsigned long long dev_size
;
6120 dev
= makedev(d
->disk
.major
,d
->disk
.minor
);
6122 dev_size_from_id(dev
, &dev_size
) &&
6123 dev_size
>= min_size
) {
6130 disks
->array
.spare_disks
++;
6140 /******************************************************************************
6141 * function: imsm_create_metadata_update_for_reshape
6142 * Function creates update for whole IMSM container.
6144 ******************************************************************************/
6145 static int imsm_create_metadata_update_for_reshape(
6146 struct supertype
*st
,
6147 struct geo_params
*geo
,
6149 struct imsm_update_reshape
**updatep
)
6151 struct intel_super
*super
= st
->sb
;
6152 struct imsm_super
*mpb
= super
->anchor
;
6153 int update_memory_size
= 0;
6154 struct imsm_update_reshape
*u
= NULL
;
6155 struct mdinfo
*spares
= NULL
;
6157 int delta_disks
= 0;
6159 dprintf("imsm_update_metadata_for_reshape(enter) raid_disks = %i\n",
6162 delta_disks
= geo
->raid_disks
- old_raid_disks
;
6164 /* size of all update data without anchor */
6165 update_memory_size
= sizeof(struct imsm_update_reshape
);
6167 /* now add space for spare disks that we need to add. */
6168 update_memory_size
+= sizeof(u
->new_disks
[0]) * (delta_disks
- 1);
6170 u
= calloc(1, update_memory_size
);
6173 "cannot get memory for imsm_update_reshape update\n");
6176 u
->type
= update_reshape_container_disks
;
6177 u
->old_raid_disks
= old_raid_disks
;
6178 u
->new_raid_disks
= geo
->raid_disks
;
6180 /* now get spare disks list
6182 spares
= get_spares_for_grow(st
);
6185 || delta_disks
> spares
->array
.spare_disks
) {
6186 dprintf("imsm: ERROR: Cannot get spare devices.\n");
6190 /* we have got spares
6191 * update disk list in imsm_disk list table in anchor
6193 dprintf("imsm: %i spares are available.\n\n",
6194 spares
->array
.spare_disks
);
6196 for (i
= 0; i
< delta_disks
; i
++) {
6197 struct mdinfo
*dev
= spares
->devs
;
6200 u
->new_disks
[i
] = makedev(dev
->disk
.major
,
6202 dl
= get_disk_super(super
, dev
->disk
.major
, dev
->disk
.minor
);
6203 dl
->index
= mpb
->num_disks
++;
6205 /* Now update the metadata so that container_content will find
6208 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
6210 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
6211 struct imsm_map
*map
= get_imsm_map(dev
, 0);
6212 map
->num_members
= geo
->raid_disks
;
6213 for (d
= 0; d
< delta_disks
; d
++) {
6214 set_imsm_ord_tbl_ent(map
, old_raid_disks
+ d
,
6215 mpb
->num_disks
- delta_disks
+ d
);
6224 if (i
== delta_disks
) {
6226 return update_memory_size
;
6234 static int imsm_reshape_super(struct supertype
*st
, long long size
, int level
,
6235 int layout
, int chunksize
, int raid_disks
,
6236 char *backup
, char *dev
, int verbouse
)
6238 /* currently we only support increasing the number of devices
6239 * for a container. This increases the number of device for each
6240 * member array. They must all be RAID0 or RAID5.
6244 struct geo_params geo
;
6246 dprintf("imsm: reshape_super called.\n");
6248 memset(&geo
, sizeof(struct geo_params
), 0);
6253 geo
.layout
= layout
;
6254 geo
.chunksize
= chunksize
;
6255 geo
.raid_disks
= raid_disks
;
6257 dprintf("\tfor level : %i\n", geo
.level
);
6258 dprintf("\tfor raid_disks : %i\n", geo
.raid_disks
);
6260 if (experimental() == 0)
6263 /* verify reshape conditions
6264 * on container level we can only increase number of devices. */
6265 if (st
->container_dev
== st
->devnum
) {
6266 /* check for delta_disks > 0
6267 *and supported raid levels 0 and 5 only in container */
6268 int old_raid_disks
= 0;
6269 if (imsm_reshape_is_allowed_on_container(
6270 st
, &geo
, &old_raid_disks
)) {
6271 struct imsm_update_reshape
*u
= NULL
;
6274 len
= imsm_create_metadata_update_for_reshape(
6275 st
, &geo
, old_raid_disks
, &u
);
6279 append_metadata_update(st
, u
, len
);
6281 dprintf("imsm: Cannot prepare "\
6284 dprintf("imsm: Operation is not allowed "\
6285 "on this container\n");
6287 dprintf("imsm: not a container operation\n");
6289 dprintf("imsm: reshape_super Exit code = %i\n", ret_val
);
6293 struct superswitch super_imsm
= {
6295 .examine_super
= examine_super_imsm
,
6296 .brief_examine_super
= brief_examine_super_imsm
,
6297 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
6298 .export_examine_super
= export_examine_super_imsm
,
6299 .detail_super
= detail_super_imsm
,
6300 .brief_detail_super
= brief_detail_super_imsm
,
6301 .write_init_super
= write_init_super_imsm
,
6302 .validate_geometry
= validate_geometry_imsm
,
6303 .add_to_super
= add_to_super_imsm
,
6304 .remove_from_super
= remove_from_super_imsm
,
6305 .detail_platform
= detail_platform_imsm
,
6306 .kill_subarray
= kill_subarray_imsm
,
6307 .update_subarray
= update_subarray_imsm
,
6308 .load_container
= load_container_imsm
,
6310 .match_home
= match_home_imsm
,
6311 .uuid_from_super
= uuid_from_super_imsm
,
6312 .getinfo_super
= getinfo_super_imsm
,
6313 .getinfo_super_disks
= getinfo_super_disks_imsm
,
6314 .update_super
= update_super_imsm
,
6316 .avail_size
= avail_size_imsm
,
6317 .min_acceptable_spare_size
= min_acceptable_spare_size_imsm
,
6319 .compare_super
= compare_super_imsm
,
6321 .load_super
= load_super_imsm
,
6322 .init_super
= init_super_imsm
,
6323 .store_super
= store_super_imsm
,
6324 .free_super
= free_super_imsm
,
6325 .match_metadata_desc
= match_metadata_desc_imsm
,
6326 .container_content
= container_content_imsm
,
6327 .default_geometry
= default_geometry_imsm
,
6328 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
6329 .reshape_super
= imsm_reshape_super
,
6336 .open_new
= imsm_open_new
,
6337 .set_array_state
= imsm_set_array_state
,
6338 .set_disk
= imsm_set_disk
,
6339 .sync_metadata
= imsm_sync_metadata
,
6340 .activate_spare
= imsm_activate_spare
,
6341 .process_update
= imsm_process_update
,
6342 .prepare_update
= imsm_prepare_update
,
6343 #endif /* MDASSEMBLE */