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
,
296 struct imsm_update_activate_spare
{
297 enum imsm_update_type type
;
301 struct imsm_update_activate_spare
*next
;
305 __u8 serial
[MAX_RAID_SERIAL_LEN
];
308 struct imsm_update_create_array
{
309 enum imsm_update_type type
;
314 struct imsm_update_kill_array
{
315 enum imsm_update_type type
;
319 struct imsm_update_rename_array
{
320 enum imsm_update_type type
;
321 __u8 name
[MAX_RAID_SERIAL_LEN
];
325 struct imsm_update_add_remove_disk
{
326 enum imsm_update_type type
;
329 static struct supertype
*match_metadata_desc_imsm(char *arg
)
331 struct supertype
*st
;
333 if (strcmp(arg
, "imsm") != 0 &&
334 strcmp(arg
, "default") != 0
338 st
= malloc(sizeof(*st
));
341 memset(st
, 0, sizeof(*st
));
342 st
->container_dev
= NoMdDev
;
343 st
->ss
= &super_imsm
;
344 st
->max_devs
= IMSM_MAX_DEVICES
;
345 st
->minor_version
= 0;
351 static __u8
*get_imsm_version(struct imsm_super
*mpb
)
353 return &mpb
->sig
[MPB_SIG_LEN
];
357 /* retrieve a disk directly from the anchor when the anchor is known to be
358 * up-to-date, currently only at load time
360 static struct imsm_disk
*__get_imsm_disk(struct imsm_super
*mpb
, __u8 index
)
362 if (index
>= mpb
->num_disks
)
364 return &mpb
->disk
[index
];
367 /* retrieve the disk description based on a index of the disk
370 static struct dl
*get_imsm_dl_disk(struct intel_super
*super
, __u8 index
)
374 for (d
= super
->disks
; d
; d
= d
->next
)
375 if (d
->index
== index
)
380 /* retrieve a disk from the parsed metadata */
381 static struct imsm_disk
*get_imsm_disk(struct intel_super
*super
, __u8 index
)
385 dl
= get_imsm_dl_disk(super
, index
);
392 /* generate a checksum directly from the anchor when the anchor is known to be
393 * up-to-date, currently only at load or write_super after coalescing
395 static __u32
__gen_imsm_checksum(struct imsm_super
*mpb
)
397 __u32 end
= mpb
->mpb_size
/ sizeof(end
);
398 __u32
*p
= (__u32
*) mpb
;
402 sum
+= __le32_to_cpu(*p
);
406 return sum
- __le32_to_cpu(mpb
->check_sum
);
409 static size_t sizeof_imsm_map(struct imsm_map
*map
)
411 return sizeof(struct imsm_map
) + sizeof(__u32
) * (map
->num_members
- 1);
414 struct imsm_map
*get_imsm_map(struct imsm_dev
*dev
, int second_map
)
416 struct imsm_map
*map
= &dev
->vol
.map
[0];
418 if (second_map
&& !dev
->vol
.migr_state
)
420 else if (second_map
) {
423 return ptr
+ sizeof_imsm_map(map
);
429 /* return the size of the device.
430 * migr_state increases the returned size if map[0] were to be duplicated
432 static size_t sizeof_imsm_dev(struct imsm_dev
*dev
, int migr_state
)
434 size_t size
= sizeof(*dev
) - sizeof(struct imsm_map
) +
435 sizeof_imsm_map(get_imsm_map(dev
, 0));
437 /* migrating means an additional map */
438 if (dev
->vol
.migr_state
)
439 size
+= sizeof_imsm_map(get_imsm_map(dev
, 1));
441 size
+= sizeof_imsm_map(get_imsm_map(dev
, 0));
447 /* retrieve disk serial number list from a metadata update */
448 static struct disk_info
*get_disk_info(struct imsm_update_create_array
*update
)
451 struct disk_info
*inf
;
453 inf
= u
+ sizeof(*update
) - sizeof(struct imsm_dev
) +
454 sizeof_imsm_dev(&update
->dev
, 0);
460 static struct imsm_dev
*__get_imsm_dev(struct imsm_super
*mpb
, __u8 index
)
466 if (index
>= mpb
->num_raid_devs
)
469 /* devices start after all disks */
470 offset
= ((void *) &mpb
->disk
[mpb
->num_disks
]) - _mpb
;
472 for (i
= 0; i
<= index
; i
++)
474 return _mpb
+ offset
;
476 offset
+= sizeof_imsm_dev(_mpb
+ offset
, 0);
481 static struct imsm_dev
*get_imsm_dev(struct intel_super
*super
, __u8 index
)
483 struct intel_dev
*dv
;
485 if (index
>= super
->anchor
->num_raid_devs
)
487 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
488 if (dv
->index
== index
)
493 static __u32
get_imsm_ord_tbl_ent(struct imsm_dev
*dev
, int slot
)
495 struct imsm_map
*map
;
497 if (dev
->vol
.migr_state
)
498 map
= get_imsm_map(dev
, 1);
500 map
= get_imsm_map(dev
, 0);
502 /* top byte identifies disk under rebuild */
503 return __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
506 #define ord_to_idx(ord) (((ord) << 8) >> 8)
507 static __u32
get_imsm_disk_idx(struct imsm_dev
*dev
, int slot
)
509 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
);
511 return ord_to_idx(ord
);
514 static void set_imsm_ord_tbl_ent(struct imsm_map
*map
, int slot
, __u32 ord
)
516 map
->disk_ord_tbl
[slot
] = __cpu_to_le32(ord
);
519 static int get_imsm_disk_slot(struct imsm_map
*map
, unsigned idx
)
524 for (slot
= 0; slot
< map
->num_members
; slot
++) {
525 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
526 if (ord_to_idx(ord
) == idx
)
533 static int get_imsm_raid_level(struct imsm_map
*map
)
535 if (map
->raid_level
== 1) {
536 if (map
->num_members
== 2)
542 return map
->raid_level
;
545 static int cmp_extent(const void *av
, const void *bv
)
547 const struct extent
*a
= av
;
548 const struct extent
*b
= bv
;
549 if (a
->start
< b
->start
)
551 if (a
->start
> b
->start
)
556 static int count_memberships(struct dl
*dl
, struct intel_super
*super
)
561 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
562 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
563 struct imsm_map
*map
= get_imsm_map(dev
, 0);
565 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
572 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
)
574 /* find a list of used extents on the given physical device */
575 struct extent
*rv
, *e
;
577 int memberships
= count_memberships(dl
, super
);
578 __u32 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
580 rv
= malloc(sizeof(struct extent
) * (memberships
+ 1));
585 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
586 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
587 struct imsm_map
*map
= get_imsm_map(dev
, 0);
589 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
590 e
->start
= __le32_to_cpu(map
->pba_of_lba0
);
591 e
->size
= __le32_to_cpu(map
->blocks_per_member
);
595 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
597 /* determine the start of the metadata
598 * when no raid devices are defined use the default
599 * ...otherwise allow the metadata to truncate the value
600 * as is the case with older versions of imsm
603 struct extent
*last
= &rv
[memberships
- 1];
606 remainder
= __le32_to_cpu(dl
->disk
.total_blocks
) -
607 (last
->start
+ last
->size
);
608 /* round down to 1k block to satisfy precision of the kernel
612 /* make sure remainder is still sane */
613 if (remainder
< (unsigned)ROUND_UP(super
->len
, 512) >> 9)
614 remainder
= ROUND_UP(super
->len
, 512) >> 9;
615 if (reservation
> remainder
)
616 reservation
= remainder
;
618 e
->start
= __le32_to_cpu(dl
->disk
.total_blocks
) - reservation
;
623 /* try to determine how much space is reserved for metadata from
624 * the last get_extents() entry, otherwise fallback to the
627 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
633 /* for spares just return a minimal reservation which will grow
634 * once the spare is picked up by an array
637 return MPB_SECTOR_CNT
;
639 e
= get_extents(super
, dl
);
641 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
643 /* scroll to last entry */
644 for (i
= 0; e
[i
].size
; i
++)
647 rv
= __le32_to_cpu(dl
->disk
.total_blocks
) - e
[i
].start
;
654 static int is_spare(struct imsm_disk
*disk
)
656 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
659 static int is_configured(struct imsm_disk
*disk
)
661 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
664 static int is_failed(struct imsm_disk
*disk
)
666 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
669 /* Return minimum size of a spare that can be used in this array*/
670 static unsigned long long min_acceptable_spare_size_imsm(struct supertype
*st
)
672 struct intel_super
*super
= st
->sb
;
676 unsigned long long rv
= 0;
680 /* find first active disk in array */
682 while (dl
&& (is_failed(&dl
->disk
) || dl
->index
== -1))
686 /* find last lba used by subarrays */
687 e
= get_extents(super
, dl
);
690 for (i
= 0; e
[i
].size
; i
++)
693 rv
= e
[i
-1].start
+ e
[i
-1].size
;
695 /* add the amount of space needed for metadata */
696 rv
= rv
+ MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
701 static __u64
blocks_per_migr_unit(struct imsm_dev
*dev
);
703 static void print_imsm_dev(struct imsm_dev
*dev
, char *uuid
, int disk_idx
)
707 struct imsm_map
*map
= get_imsm_map(dev
, 0);
711 printf("[%.16s]:\n", dev
->volume
);
712 printf(" UUID : %s\n", uuid
);
713 printf(" RAID Level : %d\n", get_imsm_raid_level(map
));
714 printf(" Members : %d\n", map
->num_members
);
715 printf(" Slots : [");
716 for (i
= 0; i
< map
->num_members
; i
++) {
717 ord
= get_imsm_ord_tbl_ent(dev
, i
);
718 printf("%s", ord
& IMSM_ORD_REBUILD
? "_" : "U");
721 slot
= get_imsm_disk_slot(map
, disk_idx
);
723 ord
= get_imsm_ord_tbl_ent(dev
, slot
);
724 printf(" This Slot : %d%s\n", slot
,
725 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
727 printf(" This Slot : ?\n");
728 sz
= __le32_to_cpu(dev
->size_high
);
730 sz
+= __le32_to_cpu(dev
->size_low
);
731 printf(" Array Size : %llu%s\n", (unsigned long long)sz
,
732 human_size(sz
* 512));
733 sz
= __le32_to_cpu(map
->blocks_per_member
);
734 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz
,
735 human_size(sz
* 512));
736 printf(" Sector Offset : %u\n",
737 __le32_to_cpu(map
->pba_of_lba0
));
738 printf(" Num Stripes : %u\n",
739 __le32_to_cpu(map
->num_data_stripes
));
740 printf(" Chunk Size : %u KiB\n",
741 __le16_to_cpu(map
->blocks_per_strip
) / 2);
742 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
743 printf(" Migrate State : ");
744 if (dev
->vol
.migr_state
) {
745 if (migr_type(dev
) == MIGR_INIT
)
746 printf("initialize\n");
747 else if (migr_type(dev
) == MIGR_REBUILD
)
749 else if (migr_type(dev
) == MIGR_VERIFY
)
751 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
752 printf("general migration\n");
753 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
754 printf("state change\n");
755 else if (migr_type(dev
) == MIGR_REPAIR
)
758 printf("<unknown:%d>\n", migr_type(dev
));
761 printf(" Map State : %s", map_state_str
[map
->map_state
]);
762 if (dev
->vol
.migr_state
) {
763 struct imsm_map
*map
= get_imsm_map(dev
, 1);
765 printf(" <-- %s", map_state_str
[map
->map_state
]);
766 printf("\n Checkpoint : %u (%llu)",
767 __le32_to_cpu(dev
->vol
.curr_migr_unit
),
768 (unsigned long long)blocks_per_migr_unit(dev
));
771 printf(" Dirty State : %s\n", dev
->vol
.dirty
? "dirty" : "clean");
774 static void print_imsm_disk(struct imsm_super
*mpb
, int index
, __u32 reserved
)
776 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, index
);
777 char str
[MAX_RAID_SERIAL_LEN
+ 1];
780 if (index
< 0 || !disk
)
784 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
785 printf(" Disk%02d Serial : %s\n", index
, str
);
786 printf(" State :%s%s%s\n", is_spare(disk
) ? " spare" : "",
787 is_configured(disk
) ? " active" : "",
788 is_failed(disk
) ? " failed" : "");
789 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
790 sz
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
791 printf(" Usable Size : %llu%s\n", (unsigned long long)sz
,
792 human_size(sz
* 512));
795 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
);
797 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
799 struct intel_super
*super
= st
->sb
;
800 struct imsm_super
*mpb
= super
->anchor
;
801 char str
[MAX_SIGNATURE_LENGTH
];
806 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
809 snprintf(str
, MPB_SIG_LEN
, "%s", mpb
->sig
);
810 printf(" Magic : %s\n", str
);
811 snprintf(str
, strlen(MPB_VERSION_RAID0
), "%s", get_imsm_version(mpb
));
812 printf(" Version : %s\n", get_imsm_version(mpb
));
813 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
814 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
815 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
816 getinfo_super_imsm(st
, &info
, NULL
);
817 fname_from_uuid(st
, &info
, nbuf
, ':');
818 printf(" UUID : %s\n", nbuf
+ 5);
819 sum
= __le32_to_cpu(mpb
->check_sum
);
820 printf(" Checksum : %08x %s\n", sum
,
821 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
822 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
));
823 printf(" Disks : %d\n", mpb
->num_disks
);
824 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
825 print_imsm_disk(mpb
, super
->disks
->index
, reserved
);
826 if (super
->bbm_log
) {
827 struct bbm_log
*log
= super
->bbm_log
;
830 printf("Bad Block Management Log:\n");
831 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
832 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
833 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
834 printf(" Spare Blocks : %d\n", __le32_to_cpu(log
->reserved_spare_block_count
));
835 printf(" First Spare : %llx\n",
836 (unsigned long long) __le64_to_cpu(log
->first_spare_lba
));
838 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
840 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
842 super
->current_vol
= i
;
843 getinfo_super_imsm(st
, &info
, NULL
);
844 fname_from_uuid(st
, &info
, nbuf
, ':');
845 print_imsm_dev(dev
, nbuf
+ 5, super
->disks
->index
);
847 for (i
= 0; i
< mpb
->num_disks
; i
++) {
848 if (i
== super
->disks
->index
)
850 print_imsm_disk(mpb
, i
, reserved
);
852 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
853 struct imsm_disk
*disk
;
854 char str
[MAX_RAID_SERIAL_LEN
+ 1];
862 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
863 printf(" Disk Serial : %s\n", str
);
864 printf(" State :%s%s%s\n", is_spare(disk
) ? " spare" : "",
865 is_configured(disk
) ? " active" : "",
866 is_failed(disk
) ? " failed" : "");
867 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
868 sz
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
869 printf(" Usable Size : %llu%s\n", (unsigned long long)sz
,
870 human_size(sz
* 512));
874 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
876 /* We just write a generic IMSM ARRAY entry */
879 struct intel_super
*super
= st
->sb
;
881 if (!super
->anchor
->num_raid_devs
) {
882 printf("ARRAY metadata=imsm\n");
886 getinfo_super_imsm(st
, &info
, NULL
);
887 fname_from_uuid(st
, &info
, nbuf
, ':');
888 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
891 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
893 /* We just write a generic IMSM ARRAY entry */
897 struct intel_super
*super
= st
->sb
;
900 if (!super
->anchor
->num_raid_devs
)
903 getinfo_super_imsm(st
, &info
, NULL
);
904 fname_from_uuid(st
, &info
, nbuf
, ':');
905 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
906 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
908 super
->current_vol
= i
;
909 getinfo_super_imsm(st
, &info
, NULL
);
910 fname_from_uuid(st
, &info
, nbuf1
, ':');
911 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
912 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
916 static void export_examine_super_imsm(struct supertype
*st
)
918 struct intel_super
*super
= st
->sb
;
919 struct imsm_super
*mpb
= super
->anchor
;
923 getinfo_super_imsm(st
, &info
, NULL
);
924 fname_from_uuid(st
, &info
, nbuf
, ':');
925 printf("MD_METADATA=imsm\n");
926 printf("MD_LEVEL=container\n");
927 printf("MD_UUID=%s\n", nbuf
+5);
928 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
931 static void detail_super_imsm(struct supertype
*st
, char *homehost
)
936 getinfo_super_imsm(st
, &info
, NULL
);
937 fname_from_uuid(st
, &info
, nbuf
, ':');
938 printf("\n UUID : %s\n", nbuf
+ 5);
941 static void brief_detail_super_imsm(struct supertype
*st
)
945 getinfo_super_imsm(st
, &info
, NULL
);
946 fname_from_uuid(st
, &info
, nbuf
, ':');
947 printf(" UUID=%s", nbuf
+ 5);
950 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
);
951 static void fd2devname(int fd
, char *name
);
953 static int imsm_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
955 /* dump an unsorted list of devices attached to ahci, as well as
956 * non-connected ports
958 int hba_len
= strlen(hba_path
) + 1;
963 unsigned long port_mask
= (1 << port_count
) - 1;
965 if (port_count
> (int)sizeof(port_mask
) * 8) {
967 fprintf(stderr
, Name
": port_count %d out of range\n", port_count
);
971 /* scroll through /sys/dev/block looking for devices attached to
974 dir
= opendir("/sys/dev/block");
975 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
986 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
988 path
= devt_to_devpath(makedev(major
, minor
));
991 if (!path_attached_to_hba(path
, hba_path
)) {
997 /* retrieve the scsi device type */
998 if (asprintf(&device
, "/sys/dev/block/%d:%d/device/xxxxxxx", major
, minor
) < 0) {
1000 fprintf(stderr
, Name
": failed to allocate 'device'\n");
1004 sprintf(device
, "/sys/dev/block/%d:%d/device/type", major
, minor
);
1005 if (load_sys(device
, buf
) != 0) {
1007 fprintf(stderr
, Name
": failed to read device type for %s\n",
1013 type
= strtoul(buf
, NULL
, 10);
1015 /* if it's not a disk print the vendor and model */
1016 if (!(type
== 0 || type
== 7 || type
== 14)) {
1019 sprintf(device
, "/sys/dev/block/%d:%d/device/vendor", major
, minor
);
1020 if (load_sys(device
, buf
) == 0) {
1021 strncpy(vendor
, buf
, sizeof(vendor
));
1022 vendor
[sizeof(vendor
) - 1] = '\0';
1023 c
= (char *) &vendor
[sizeof(vendor
) - 1];
1024 while (isspace(*c
) || *c
== '\0')
1028 sprintf(device
, "/sys/dev/block/%d:%d/device/model", major
, minor
);
1029 if (load_sys(device
, buf
) == 0) {
1030 strncpy(model
, buf
, sizeof(model
));
1031 model
[sizeof(model
) - 1] = '\0';
1032 c
= (char *) &model
[sizeof(model
) - 1];
1033 while (isspace(*c
) || *c
== '\0')
1037 if (vendor
[0] && model
[0])
1038 sprintf(buf
, "%.64s %.64s", vendor
, model
);
1040 switch (type
) { /* numbers from hald/linux/device.c */
1041 case 1: sprintf(buf
, "tape"); break;
1042 case 2: sprintf(buf
, "printer"); break;
1043 case 3: sprintf(buf
, "processor"); break;
1045 case 5: sprintf(buf
, "cdrom"); break;
1046 case 6: sprintf(buf
, "scanner"); break;
1047 case 8: sprintf(buf
, "media_changer"); break;
1048 case 9: sprintf(buf
, "comm"); break;
1049 case 12: sprintf(buf
, "raid"); break;
1050 default: sprintf(buf
, "unknown");
1056 /* chop device path to 'host%d' and calculate the port number */
1057 c
= strchr(&path
[hba_len
], '/');
1060 fprintf(stderr
, Name
": %s - invalid path name\n", path
+ hba_len
);
1065 if (sscanf(&path
[hba_len
], "host%d", &port
) == 1)
1069 *c
= '/'; /* repair the full string */
1070 fprintf(stderr
, Name
": failed to determine port number for %s\n",
1077 /* mark this port as used */
1078 port_mask
&= ~(1 << port
);
1080 /* print out the device information */
1082 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
1086 fd
= dev_open(ent
->d_name
, O_RDONLY
);
1088 printf(" Port%d : - disk info unavailable -\n", port
);
1090 fd2devname(fd
, buf
);
1091 printf(" Port%d : %s", port
, buf
);
1092 if (imsm_read_serial(fd
, NULL
, (__u8
*) buf
) == 0)
1093 printf(" (%s)\n", buf
);
1108 for (i
= 0; i
< port_count
; i
++)
1109 if (port_mask
& (1 << i
))
1110 printf(" Port%d : - no device attached -\n", i
);
1116 static int detail_platform_imsm(int verbose
, int enumerate_only
)
1118 /* There are two components to imsm platform support, the ahci SATA
1119 * controller and the option-rom. To find the SATA controller we
1120 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
1121 * controller with the Intel vendor id is present. This approach
1122 * allows mdadm to leverage the kernel's ahci detection logic, with the
1123 * caveat that if ahci.ko is not loaded mdadm will not be able to
1124 * detect platform raid capabilities. The option-rom resides in a
1125 * platform "Adapter ROM". We scan for its signature to retrieve the
1126 * platform capabilities. If raid support is disabled in the BIOS the
1127 * option-rom capability structure will not be available.
1129 const struct imsm_orom
*orom
;
1130 struct sys_dev
*list
, *hba
;
1133 const char *hba_path
;
1137 if (enumerate_only
) {
1138 if (check_env("IMSM_NO_PLATFORM") || find_imsm_orom())
1143 list
= find_driver_devices("pci", "ahci");
1144 for (hba
= list
; hba
; hba
= hba
->next
)
1145 if (devpath_to_vendor(hba
->path
) == 0x8086)
1150 fprintf(stderr
, Name
": unable to find active ahci controller\n");
1151 free_sys_dev(&list
);
1154 fprintf(stderr
, Name
": found Intel SATA AHCI Controller\n");
1155 hba_path
= hba
->path
;
1157 free_sys_dev(&list
);
1159 orom
= find_imsm_orom();
1162 fprintf(stderr
, Name
": imsm option-rom not found\n");
1166 printf(" Platform : Intel(R) Matrix Storage Manager\n");
1167 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
1168 orom
->hotfix_ver
, orom
->build
);
1169 printf(" RAID Levels :%s%s%s%s%s\n",
1170 imsm_orom_has_raid0(orom
) ? " raid0" : "",
1171 imsm_orom_has_raid1(orom
) ? " raid1" : "",
1172 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
1173 imsm_orom_has_raid10(orom
) ? " raid10" : "",
1174 imsm_orom_has_raid5(orom
) ? " raid5" : "");
1175 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1176 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
1177 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
1178 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
1179 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
1180 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
1181 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
1182 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
1183 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
1184 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
1185 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
1186 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
1187 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
1188 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
1189 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
1190 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
1191 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
1192 printf(" Max Disks : %d\n", orom
->tds
);
1193 printf(" Max Volumes : %d\n", orom
->vpa
);
1194 printf(" I/O Controller : %s\n", hba_path
);
1196 /* find the smallest scsi host number to determine a port number base */
1197 dir
= opendir(hba_path
);
1198 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
1201 if (sscanf(ent
->d_name
, "host%d", &host
) != 1)
1203 if (port_count
== 0)
1205 else if (host
< host_base
)
1208 if (host
+ 1 > port_count
+ host_base
)
1209 port_count
= host
+ 1 - host_base
;
1215 if (!port_count
|| imsm_enumerate_ports(hba_path
, port_count
,
1216 host_base
, verbose
) != 0) {
1218 fprintf(stderr
, Name
": failed to enumerate ports\n");
1226 static int match_home_imsm(struct supertype
*st
, char *homehost
)
1228 /* the imsm metadata format does not specify any host
1229 * identification information. We return -1 since we can never
1230 * confirm nor deny whether a given array is "meant" for this
1231 * host. We rely on compare_super and the 'family_num' fields to
1232 * exclude member disks that do not belong, and we rely on
1233 * mdadm.conf to specify the arrays that should be assembled.
1234 * Auto-assembly may still pick up "foreign" arrays.
1240 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
1242 /* The uuid returned here is used for:
1243 * uuid to put into bitmap file (Create, Grow)
1244 * uuid for backup header when saving critical section (Grow)
1245 * comparing uuids when re-adding a device into an array
1246 * In these cases the uuid required is that of the data-array,
1247 * not the device-set.
1248 * uuid to recognise same set when adding a missing device back
1249 * to an array. This is a uuid for the device-set.
1251 * For each of these we can make do with a truncated
1252 * or hashed uuid rather than the original, as long as
1254 * In each case the uuid required is that of the data-array,
1255 * not the device-set.
1257 /* imsm does not track uuid's so we synthesis one using sha1 on
1258 * - The signature (Which is constant for all imsm array, but no matter)
1259 * - the orig_family_num of the container
1260 * - the index number of the volume
1261 * - the 'serial' number of the volume.
1262 * Hopefully these are all constant.
1264 struct intel_super
*super
= st
->sb
;
1267 struct sha1_ctx ctx
;
1268 struct imsm_dev
*dev
= NULL
;
1271 /* some mdadm versions failed to set ->orig_family_num, in which
1272 * case fall back to ->family_num. orig_family_num will be
1273 * fixed up with the first metadata update.
1275 family_num
= super
->anchor
->orig_family_num
;
1276 if (family_num
== 0)
1277 family_num
= super
->anchor
->family_num
;
1278 sha1_init_ctx(&ctx
);
1279 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
1280 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
1281 if (super
->current_vol
>= 0)
1282 dev
= get_imsm_dev(super
, super
->current_vol
);
1284 __u32 vol
= super
->current_vol
;
1285 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
1286 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
1288 sha1_finish_ctx(&ctx
, buf
);
1289 memcpy(uuid
, buf
, 4*4);
1294 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
1296 __u8
*v
= get_imsm_version(mpb
);
1297 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
1298 char major
[] = { 0, 0, 0 };
1299 char minor
[] = { 0 ,0, 0 };
1300 char patch
[] = { 0, 0, 0 };
1301 char *ver_parse
[] = { major
, minor
, patch
};
1305 while (*v
!= '\0' && v
< end
) {
1306 if (*v
!= '.' && j
< 2)
1307 ver_parse
[i
][j
++] = *v
;
1315 *m
= strtol(minor
, NULL
, 0);
1316 *p
= strtol(patch
, NULL
, 0);
1320 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
1322 /* migr_strip_size when repairing or initializing parity */
1323 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1324 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1326 switch (get_imsm_raid_level(map
)) {
1331 return 128*1024 >> 9;
1335 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
1337 /* migr_strip_size when rebuilding a degraded disk, no idea why
1338 * this is different than migr_strip_size_resync(), but it's good
1341 struct imsm_map
*map
= get_imsm_map(dev
, 1);
1342 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1344 switch (get_imsm_raid_level(map
)) {
1347 if (map
->num_members
% map
->num_domains
== 0)
1348 return 128*1024 >> 9;
1352 return max((__u32
) 64*1024 >> 9, chunk
);
1354 return 128*1024 >> 9;
1358 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
1360 struct imsm_map
*lo
= get_imsm_map(dev
, 0);
1361 struct imsm_map
*hi
= get_imsm_map(dev
, 1);
1362 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
1363 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
1365 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
1368 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
1370 struct imsm_map
*lo
= get_imsm_map(dev
, 0);
1371 int level
= get_imsm_raid_level(lo
);
1373 if (level
== 1 || level
== 10) {
1374 struct imsm_map
*hi
= get_imsm_map(dev
, 1);
1376 return hi
->num_domains
;
1378 return num_stripes_per_unit_resync(dev
);
1381 static __u8
imsm_num_data_members(struct imsm_dev
*dev
)
1383 /* named 'imsm_' because raid0, raid1 and raid10
1384 * counter-intuitively have the same number of data disks
1386 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1388 switch (get_imsm_raid_level(map
)) {
1392 return map
->num_members
;
1394 return map
->num_members
- 1;
1396 dprintf("%s: unsupported raid level\n", __func__
);
1401 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
1403 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1404 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1406 switch(get_imsm_raid_level(map
)) {
1409 return chunk
* map
->num_domains
;
1411 return chunk
* map
->num_members
;
1417 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
1419 struct imsm_map
*map
= get_imsm_map(dev
, 1);
1420 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1421 __u32 strip
= block
/ chunk
;
1423 switch (get_imsm_raid_level(map
)) {
1426 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
1427 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
1429 return vol_stripe
* chunk
+ block
% chunk
;
1431 __u32 stripe
= strip
/ (map
->num_members
- 1);
1433 return stripe
* chunk
+ block
% chunk
;
1440 static __u64
blocks_per_migr_unit(struct imsm_dev
*dev
)
1442 /* calculate the conversion factor between per member 'blocks'
1443 * (md/{resync,rebuild}_start) and imsm migration units, return
1444 * 0 for the 'not migrating' and 'unsupported migration' cases
1446 if (!dev
->vol
.migr_state
)
1449 switch (migr_type(dev
)) {
1453 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1454 __u32 stripes_per_unit
;
1455 __u32 blocks_per_unit
;
1464 /* yes, this is really the translation of migr_units to
1465 * per-member blocks in the 'resync' case
1467 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
1468 migr_chunk
= migr_strip_blocks_resync(dev
);
1469 disks
= imsm_num_data_members(dev
);
1470 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
1471 stripe
= __le32_to_cpu(map
->blocks_per_strip
) * disks
;
1472 segment
= blocks_per_unit
/ stripe
;
1473 block_rel
= blocks_per_unit
- segment
* stripe
;
1474 parity_depth
= parity_segment_depth(dev
);
1475 block_map
= map_migr_block(dev
, block_rel
);
1476 return block_map
+ parity_depth
* segment
;
1478 case MIGR_REBUILD
: {
1479 __u32 stripes_per_unit
;
1482 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
1483 migr_chunk
= migr_strip_blocks_rebuild(dev
);
1484 return migr_chunk
* stripes_per_unit
;
1487 case MIGR_STATE_CHANGE
:
1493 static int imsm_level_to_layout(int level
)
1501 return ALGORITHM_LEFT_ASYMMETRIC
;
1508 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
, char *dmap
)
1510 struct intel_super
*super
= st
->sb
;
1511 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
1512 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1515 int map_disks
= info
->array
.raid_disks
;
1517 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
1518 if (dl
->raiddisk
== info
->disk
.raid_disk
)
1520 info
->container_member
= super
->current_vol
;
1521 info
->array
.raid_disks
= map
->num_members
;
1522 info
->array
.level
= get_imsm_raid_level(map
);
1523 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
1524 info
->array
.md_minor
= -1;
1525 info
->array
.ctime
= 0;
1526 info
->array
.utime
= 0;
1527 info
->array
.chunk_size
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
1528 info
->array
.state
= !dev
->vol
.dirty
;
1529 info
->custom_array_size
= __le32_to_cpu(dev
->size_high
);
1530 info
->custom_array_size
<<= 32;
1531 info
->custom_array_size
|= __le32_to_cpu(dev
->size_low
);
1533 info
->disk
.major
= 0;
1534 info
->disk
.minor
= 0;
1536 info
->disk
.major
= dl
->major
;
1537 info
->disk
.minor
= dl
->minor
;
1540 info
->data_offset
= __le32_to_cpu(map
->pba_of_lba0
);
1541 info
->component_size
= __le32_to_cpu(map
->blocks_per_member
);
1542 memset(info
->uuid
, 0, sizeof(info
->uuid
));
1543 info
->recovery_start
= MaxSector
;
1544 info
->reshape_active
= 0;
1546 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
|| dev
->vol
.dirty
) {
1547 info
->resync_start
= 0;
1548 } else if (dev
->vol
.migr_state
) {
1549 switch (migr_type(dev
)) {
1552 __u64 blocks_per_unit
= blocks_per_migr_unit(dev
);
1553 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
1555 info
->resync_start
= blocks_per_unit
* units
;
1559 /* we could emulate the checkpointing of
1560 * 'sync_action=check' migrations, but for now
1561 * we just immediately complete them
1564 /* this is handled by container_content_imsm() */
1566 case MIGR_STATE_CHANGE
:
1567 /* FIXME handle other migrations */
1569 /* we are not dirty, so... */
1570 info
->resync_start
= MaxSector
;
1573 info
->resync_start
= MaxSector
;
1575 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
1576 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
1578 info
->array
.major_version
= -1;
1579 info
->array
.minor_version
= -2;
1580 devname
= devnum2devname(st
->container_dev
);
1581 *info
->text_version
= '\0';
1583 sprintf(info
->text_version
, "/%s/%d", devname
, info
->container_member
);
1585 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
1586 uuid_from_super_imsm(st
, info
->uuid
);
1590 for (i
=0; i
<map_disks
; i
++) {
1592 if (i
< info
->array
.raid_disks
) {
1593 struct imsm_disk
*dsk
;
1594 j
= get_imsm_disk_idx(dev
, i
);
1595 dsk
= get_imsm_disk(super
, j
);
1596 if (dsk
&& (dsk
->status
& CONFIGURED_DISK
))
1603 /* check the config file to see if we can return a real uuid for this spare */
1604 static void fixup_container_spare_uuid(struct mdinfo
*inf
)
1606 struct mddev_ident
*array_list
;
1608 if (inf
->array
.level
!= LEVEL_CONTAINER
||
1609 memcmp(inf
->uuid
, uuid_match_any
, sizeof(int[4])) != 0)
1612 array_list
= conf_get_ident(NULL
);
1614 for (; array_list
; array_list
= array_list
->next
) {
1615 if (array_list
->uuid_set
) {
1616 struct supertype
*_sst
; /* spare supertype */
1617 struct supertype
*_cst
; /* container supertype */
1619 _cst
= array_list
->st
;
1621 _sst
= _cst
->ss
->match_metadata_desc(inf
->text_version
);
1626 memcpy(inf
->uuid
, array_list
->uuid
, sizeof(int[4]));
1635 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
, int failed
);
1636 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
);
1638 static struct imsm_disk
*get_imsm_missing(struct intel_super
*super
, __u8 index
)
1642 for (d
= super
->missing
; d
; d
= d
->next
)
1643 if (d
->index
== index
)
1648 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
, char *map
)
1650 struct intel_super
*super
= st
->sb
;
1651 struct imsm_disk
*disk
;
1652 int map_disks
= info
->array
.raid_disks
;
1653 int max_enough
= -1;
1655 struct imsm_super
*mpb
;
1657 if (super
->current_vol
>= 0) {
1658 getinfo_super_imsm_volume(st
, info
, map
);
1662 /* Set raid_disks to zero so that Assemble will always pull in valid
1665 info
->array
.raid_disks
= 0;
1666 info
->array
.level
= LEVEL_CONTAINER
;
1667 info
->array
.layout
= 0;
1668 info
->array
.md_minor
= -1;
1669 info
->array
.ctime
= 0; /* N/A for imsm */
1670 info
->array
.utime
= 0;
1671 info
->array
.chunk_size
= 0;
1673 info
->disk
.major
= 0;
1674 info
->disk
.minor
= 0;
1675 info
->disk
.raid_disk
= -1;
1676 info
->reshape_active
= 0;
1677 info
->array
.major_version
= -1;
1678 info
->array
.minor_version
= -2;
1679 strcpy(info
->text_version
, "imsm");
1680 info
->safe_mode_delay
= 0;
1681 info
->disk
.number
= -1;
1682 info
->disk
.state
= 0;
1684 info
->recovery_start
= MaxSector
;
1686 /* do we have the all the insync disks that we expect? */
1687 mpb
= super
->anchor
;
1689 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
1690 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
1691 int failed
, enough
, j
, missing
= 0;
1692 struct imsm_map
*map
;
1695 failed
= imsm_count_failed(super
, dev
);
1696 state
= imsm_check_degraded(super
, dev
, failed
);
1697 map
= get_imsm_map(dev
, dev
->vol
.migr_state
);
1699 /* any newly missing disks?
1700 * (catches single-degraded vs double-degraded)
1702 for (j
= 0; j
< map
->num_members
; j
++) {
1703 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
);
1704 __u32 idx
= ord_to_idx(ord
);
1706 if (!(ord
& IMSM_ORD_REBUILD
) &&
1707 get_imsm_missing(super
, idx
)) {
1713 if (state
== IMSM_T_STATE_FAILED
)
1715 else if (state
== IMSM_T_STATE_DEGRADED
&&
1716 (state
!= map
->map_state
|| missing
))
1718 else /* we're normal, or already degraded */
1721 /* in the missing/failed disk case check to see
1722 * if at least one array is runnable
1724 max_enough
= max(max_enough
, enough
);
1726 dprintf("%s: enough: %d\n", __func__
, max_enough
);
1727 info
->container_enough
= max_enough
;
1730 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
1732 disk
= &super
->disks
->disk
;
1733 info
->data_offset
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
1734 info
->component_size
= reserved
;
1735 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
1736 /* we don't change info->disk.raid_disk here because
1737 * this state will be finalized in mdmon after we have
1738 * found the 'most fresh' version of the metadata
1740 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
1741 info
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
1744 /* only call uuid_from_super_imsm when this disk is part of a populated container,
1745 * ->compare_super may have updated the 'num_raid_devs' field for spares
1747 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
1748 uuid_from_super_imsm(st
, info
->uuid
);
1750 memcpy(info
->uuid
, uuid_match_any
, sizeof(int[4]));
1751 fixup_container_spare_uuid(info
);
1754 /* I don't know how to compute 'map' on imsm, so use safe default */
1757 for (i
= 0; i
< map_disks
; i
++)
1763 /* allocates memory and fills disk in mdinfo structure
1764 * for each disk in array */
1765 struct mdinfo
*getinfo_super_disks_imsm(struct supertype
*st
)
1767 struct mdinfo
*mddev
= NULL
;
1768 struct intel_super
*super
= st
->sb
;
1769 struct imsm_disk
*disk
;
1772 if (!super
|| !super
->disks
)
1775 mddev
= malloc(sizeof(*mddev
));
1777 fprintf(stderr
, Name
": Failed to allocate memory.\n");
1780 memset(mddev
, 0, sizeof(*mddev
));
1784 tmp
= malloc(sizeof(*tmp
));
1786 fprintf(stderr
, Name
": Failed to allocate memory.\n");
1791 memset(tmp
, 0, sizeof(*tmp
));
1793 tmp
->next
= mddev
->devs
;
1795 tmp
->disk
.number
= count
++;
1796 tmp
->disk
.major
= dl
->major
;
1797 tmp
->disk
.minor
= dl
->minor
;
1798 tmp
->disk
.state
= is_configured(disk
) ?
1799 (1 << MD_DISK_ACTIVE
) : 0;
1800 tmp
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
1801 tmp
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
1802 tmp
->disk
.raid_disk
= -1;
1808 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
1809 char *update
, char *devname
, int verbose
,
1810 int uuid_set
, char *homehost
)
1812 /* For 'assemble' and 'force' we need to return non-zero if any
1813 * change was made. For others, the return value is ignored.
1814 * Update options are:
1815 * force-one : This device looks a bit old but needs to be included,
1816 * update age info appropriately.
1817 * assemble: clear any 'faulty' flag to allow this device to
1819 * force-array: Array is degraded but being forced, mark it clean
1820 * if that will be needed to assemble it.
1822 * newdev: not used ????
1823 * grow: Array has gained a new device - this is currently for
1825 * resync: mark as dirty so a resync will happen.
1826 * name: update the name - preserving the homehost
1827 * uuid: Change the uuid of the array to match watch is given
1829 * Following are not relevant for this imsm:
1830 * sparc2.2 : update from old dodgey metadata
1831 * super-minor: change the preferred_minor number
1832 * summaries: update redundant counters.
1833 * homehost: update the recorded homehost
1834 * _reshape_progress: record new reshape_progress position.
1837 struct intel_super
*super
= st
->sb
;
1838 struct imsm_super
*mpb
;
1840 /* we can only update container info */
1841 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
1844 mpb
= super
->anchor
;
1846 if (strcmp(update
, "uuid") == 0 && uuid_set
&& !info
->update_private
)
1848 else if (strcmp(update
, "uuid") == 0 && uuid_set
&& info
->update_private
) {
1849 mpb
->orig_family_num
= *((__u32
*) info
->update_private
);
1851 } else if (strcmp(update
, "uuid") == 0) {
1852 __u32
*new_family
= malloc(sizeof(*new_family
));
1854 /* update orig_family_number with the incoming random
1855 * data, report the new effective uuid, and store the
1856 * new orig_family_num for future updates.
1859 memcpy(&mpb
->orig_family_num
, info
->uuid
, sizeof(__u32
));
1860 uuid_from_super_imsm(st
, info
->uuid
);
1861 *new_family
= mpb
->orig_family_num
;
1862 info
->update_private
= new_family
;
1865 } else if (strcmp(update
, "assemble") == 0)
1870 /* successful update? recompute checksum */
1872 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
1877 static size_t disks_to_mpb_size(int disks
)
1881 size
= sizeof(struct imsm_super
);
1882 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
1883 size
+= 2 * sizeof(struct imsm_dev
);
1884 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
1885 size
+= (4 - 2) * sizeof(struct imsm_map
);
1886 /* 4 possible disk_ord_tbl's */
1887 size
+= 4 * (disks
- 1) * sizeof(__u32
);
1892 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
)
1894 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
1897 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
1900 static void free_devlist(struct intel_super
*super
)
1902 struct intel_dev
*dv
;
1904 while (super
->devlist
) {
1905 dv
= super
->devlist
->next
;
1906 free(super
->devlist
->dev
);
1907 free(super
->devlist
);
1908 super
->devlist
= dv
;
1912 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
1914 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
1917 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
1921 * 0 same, or first was empty, and second was copied
1922 * 1 second had wrong number
1924 * 3 wrong other info
1926 struct intel_super
*first
= st
->sb
;
1927 struct intel_super
*sec
= tst
->sb
;
1935 /* if an anchor does not have num_raid_devs set then it is a free
1938 if (first
->anchor
->num_raid_devs
> 0 &&
1939 sec
->anchor
->num_raid_devs
> 0) {
1940 /* Determine if these disks might ever have been
1941 * related. Further disambiguation can only take place
1942 * in load_super_imsm_all
1944 __u32 first_family
= first
->anchor
->orig_family_num
;
1945 __u32 sec_family
= sec
->anchor
->orig_family_num
;
1947 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
1948 MAX_SIGNATURE_LENGTH
) != 0)
1951 if (first_family
== 0)
1952 first_family
= first
->anchor
->family_num
;
1953 if (sec_family
== 0)
1954 sec_family
= sec
->anchor
->family_num
;
1956 if (first_family
!= sec_family
)
1962 /* if 'first' is a spare promote it to a populated mpb with sec's
1965 if (first
->anchor
->num_raid_devs
== 0 &&
1966 sec
->anchor
->num_raid_devs
> 0) {
1968 struct intel_dev
*dv
;
1969 struct imsm_dev
*dev
;
1971 /* we need to copy raid device info from sec if an allocation
1972 * fails here we don't associate the spare
1974 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
1975 dv
= malloc(sizeof(*dv
));
1978 dev
= malloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
1985 dv
->next
= first
->devlist
;
1986 first
->devlist
= dv
;
1988 if (i
< sec
->anchor
->num_raid_devs
) {
1989 /* allocation failure */
1990 free_devlist(first
);
1991 fprintf(stderr
, "imsm: failed to associate spare\n");
1994 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
1995 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
1996 first
->anchor
->family_num
= sec
->anchor
->family_num
;
1997 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
1998 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
1999 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
2005 static void fd2devname(int fd
, char *name
)
2009 char dname
[PATH_MAX
];
2014 if (fstat(fd
, &st
) != 0)
2016 sprintf(path
, "/sys/dev/block/%d:%d",
2017 major(st
.st_rdev
), minor(st
.st_rdev
));
2019 rv
= readlink(path
, dname
, sizeof(dname
));
2024 nm
= strrchr(dname
, '/');
2026 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
2029 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
2031 static int imsm_read_serial(int fd
, char *devname
,
2032 __u8 serial
[MAX_RAID_SERIAL_LEN
])
2034 unsigned char scsi_serial
[255];
2043 memset(scsi_serial
, 0, sizeof(scsi_serial
));
2045 rv
= scsi_get_serial(fd
, scsi_serial
, sizeof(scsi_serial
));
2047 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
2048 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
2049 fd2devname(fd
, (char *) serial
);
2056 Name
": Failed to retrieve serial for %s\n",
2061 rsp_len
= scsi_serial
[3];
2065 Name
": Failed to retrieve serial for %s\n",
2069 rsp_buf
= (char *) &scsi_serial
[4];
2071 /* trim all whitespace and non-printable characters and convert
2074 for (i
= 0, dest
= rsp_buf
; i
< rsp_len
; i
++) {
2077 /* ':' is reserved for use in placeholder serial
2078 * numbers for missing disks
2086 len
= dest
- rsp_buf
;
2089 /* truncate leading characters */
2090 if (len
> MAX_RAID_SERIAL_LEN
) {
2091 dest
+= len
- MAX_RAID_SERIAL_LEN
;
2092 len
= MAX_RAID_SERIAL_LEN
;
2095 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
2096 memcpy(serial
, dest
, len
);
2101 static int serialcmp(__u8
*s1
, __u8
*s2
)
2103 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
2106 static void serialcpy(__u8
*dest
, __u8
*src
)
2108 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
2112 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
2116 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
2117 if (serialcmp(dl
->serial
, serial
) == 0)
2124 static struct imsm_disk
*
2125 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
2129 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2130 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
2132 if (serialcmp(disk
->serial
, serial
) == 0) {
2143 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
2145 struct imsm_disk
*disk
;
2150 __u8 serial
[MAX_RAID_SERIAL_LEN
];
2152 rv
= imsm_read_serial(fd
, devname
, serial
);
2157 dl
= calloc(1, sizeof(*dl
));
2161 Name
": failed to allocate disk buffer for %s\n",
2167 dl
->major
= major(stb
.st_rdev
);
2168 dl
->minor
= minor(stb
.st_rdev
);
2169 dl
->next
= super
->disks
;
2170 dl
->fd
= keep_fd
? fd
: -1;
2171 assert(super
->disks
== NULL
);
2173 serialcpy(dl
->serial
, serial
);
2176 fd2devname(fd
, name
);
2178 dl
->devname
= strdup(devname
);
2180 dl
->devname
= strdup(name
);
2182 /* look up this disk's index in the current anchor */
2183 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
2186 /* only set index on disks that are a member of a
2187 * populated contianer, i.e. one with raid_devs
2189 if (is_failed(&dl
->disk
))
2191 else if (is_spare(&dl
->disk
))
2199 /* When migrating map0 contains the 'destination' state while map1
2200 * contains the current state. When not migrating map0 contains the
2201 * current state. This routine assumes that map[0].map_state is set to
2202 * the current array state before being called.
2204 * Migration is indicated by one of the following states
2205 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
2206 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
2207 * map1state=unitialized)
2208 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
2210 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
2211 * map1state=degraded)
2213 static void migrate(struct imsm_dev
*dev
, __u8 to_state
, int migr_type
)
2215 struct imsm_map
*dest
;
2216 struct imsm_map
*src
= get_imsm_map(dev
, 0);
2218 dev
->vol
.migr_state
= 1;
2219 set_migr_type(dev
, migr_type
);
2220 dev
->vol
.curr_migr_unit
= 0;
2221 dest
= get_imsm_map(dev
, 1);
2223 /* duplicate and then set the target end state in map[0] */
2224 memcpy(dest
, src
, sizeof_imsm_map(src
));
2225 if ((migr_type
== MIGR_REBUILD
) ||
2226 (migr_type
== MIGR_GEN_MIGR
)) {
2230 for (i
= 0; i
< src
->num_members
; i
++) {
2231 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
2232 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
2236 src
->map_state
= to_state
;
2239 static void end_migration(struct imsm_dev
*dev
, __u8 map_state
)
2241 struct imsm_map
*map
= get_imsm_map(dev
, 0);
2242 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
);
2245 /* merge any IMSM_ORD_REBUILD bits that were not successfully
2246 * completed in the last migration.
2248 * FIXME add support for raid-level-migration
2250 for (i
= 0; i
< prev
->num_members
; i
++)
2251 for (j
= 0; j
< map
->num_members
; j
++)
2252 /* during online capacity expansion
2253 * disks position can be changed if takeover is used
2255 if (ord_to_idx(map
->disk_ord_tbl
[j
]) ==
2256 ord_to_idx(prev
->disk_ord_tbl
[i
])) {
2257 map
->disk_ord_tbl
[j
] |= prev
->disk_ord_tbl
[i
];
2261 dev
->vol
.migr_state
= 0;
2262 dev
->vol
.migr_type
= 0;
2263 dev
->vol
.curr_migr_unit
= 0;
2264 map
->map_state
= map_state
;
2268 static int parse_raid_devices(struct intel_super
*super
)
2271 struct imsm_dev
*dev_new
;
2272 size_t len
, len_migr
;
2273 size_t space_needed
= 0;
2274 struct imsm_super
*mpb
= super
->anchor
;
2276 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
2277 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
2278 struct intel_dev
*dv
;
2280 len
= sizeof_imsm_dev(dev_iter
, 0);
2281 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
2283 space_needed
+= len_migr
- len
;
2285 dv
= malloc(sizeof(*dv
));
2288 dev_new
= malloc(len_migr
);
2293 imsm_copy_dev(dev_new
, dev_iter
);
2296 dv
->next
= super
->devlist
;
2297 super
->devlist
= dv
;
2300 /* ensure that super->buf is large enough when all raid devices
2303 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
2306 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
, 512);
2307 if (posix_memalign(&buf
, 512, len
) != 0)
2310 memcpy(buf
, super
->buf
, super
->len
);
2311 memset(buf
+ super
->len
, 0, len
- super
->len
);
2320 /* retrieve a pointer to the bbm log which starts after all raid devices */
2321 struct bbm_log
*__get_imsm_bbm_log(struct imsm_super
*mpb
)
2325 if (__le32_to_cpu(mpb
->bbm_log_size
)) {
2327 ptr
+= mpb
->mpb_size
- __le32_to_cpu(mpb
->bbm_log_size
);
2333 static void __free_imsm(struct intel_super
*super
, int free_disks
);
2335 /* load_imsm_mpb - read matrix metadata
2336 * allocates super->mpb to be freed by free_super
2338 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
2340 unsigned long long dsize
;
2341 unsigned long long sectors
;
2343 struct imsm_super
*anchor
;
2346 get_dev_size(fd
, NULL
, &dsize
);
2350 Name
": %s: device to small for imsm\n",
2355 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0) {
2358 Name
": Cannot seek to anchor block on %s: %s\n",
2359 devname
, strerror(errno
));
2363 if (posix_memalign((void**)&anchor
, 512, 512) != 0) {
2366 Name
": Failed to allocate imsm anchor buffer"
2367 " on %s\n", devname
);
2370 if (read(fd
, anchor
, 512) != 512) {
2373 Name
": Cannot read anchor block on %s: %s\n",
2374 devname
, strerror(errno
));
2379 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
2382 Name
": no IMSM anchor on %s\n", devname
);
2387 __free_imsm(super
, 0);
2388 super
->len
= ROUND_UP(anchor
->mpb_size
, 512);
2389 if (posix_memalign(&super
->buf
, 512, super
->len
) != 0) {
2392 Name
": unable to allocate %zu byte mpb buffer\n",
2397 memcpy(super
->buf
, anchor
, 512);
2399 sectors
= mpb_sectors(anchor
) - 1;
2402 check_sum
= __gen_imsm_checksum(super
->anchor
);
2403 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
2406 Name
": IMSM checksum %x != %x on %s\n",
2408 __le32_to_cpu(super
->anchor
->check_sum
),
2416 /* read the extended mpb */
2417 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0) {
2420 Name
": Cannot seek to extended mpb on %s: %s\n",
2421 devname
, strerror(errno
));
2425 if ((unsigned)read(fd
, super
->buf
+ 512, super
->len
- 512) != super
->len
- 512) {
2428 Name
": Cannot read extended mpb on %s: %s\n",
2429 devname
, strerror(errno
));
2433 check_sum
= __gen_imsm_checksum(super
->anchor
);
2434 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
2437 Name
": IMSM checksum %x != %x on %s\n",
2438 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
2443 /* FIXME the BBM log is disk specific so we cannot use this global
2444 * buffer for all disks. Ok for now since we only look at the global
2445 * bbm_log_size parameter to gate assembly
2447 super
->bbm_log
= __get_imsm_bbm_log(super
->anchor
);
2453 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
2457 err
= load_imsm_mpb(fd
, super
, devname
);
2460 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
2463 err
= parse_raid_devices(super
);
2468 static void __free_imsm_disk(struct dl
*d
)
2480 static void free_imsm_disks(struct intel_super
*super
)
2484 while (super
->disks
) {
2486 super
->disks
= d
->next
;
2487 __free_imsm_disk(d
);
2489 while (super
->missing
) {
2491 super
->missing
= d
->next
;
2492 __free_imsm_disk(d
);
2497 /* free all the pieces hanging off of a super pointer */
2498 static void __free_imsm(struct intel_super
*super
, int free_disks
)
2505 free_imsm_disks(super
);
2506 free_devlist(super
);
2508 free((void *) super
->hba
);
2513 static void free_imsm(struct intel_super
*super
)
2515 __free_imsm(super
, 1);
2519 static void free_super_imsm(struct supertype
*st
)
2521 struct intel_super
*super
= st
->sb
;
2530 static struct intel_super
*alloc_super(void)
2532 struct intel_super
*super
= malloc(sizeof(*super
));
2535 memset(super
, 0, sizeof(*super
));
2536 super
->current_vol
= -1;
2537 super
->create_offset
= ~((__u32
) 0);
2538 if (!check_env("IMSM_NO_PLATFORM"))
2539 super
->orom
= find_imsm_orom();
2540 if (super
->orom
&& !check_env("IMSM_TEST_OROM")) {
2541 struct sys_dev
*list
, *ent
;
2543 /* find the first intel ahci controller */
2544 list
= find_driver_devices("pci", "ahci");
2545 for (ent
= list
; ent
; ent
= ent
->next
)
2546 if (devpath_to_vendor(ent
->path
) == 0x8086)
2549 super
->hba
= ent
->path
;
2552 free_sys_dev(&list
);
2560 /* find_missing - helper routine for load_super_imsm_all that identifies
2561 * disks that have disappeared from the system. This routine relies on
2562 * the mpb being uptodate, which it is at load time.
2564 static int find_missing(struct intel_super
*super
)
2567 struct imsm_super
*mpb
= super
->anchor
;
2569 struct imsm_disk
*disk
;
2571 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2572 disk
= __get_imsm_disk(mpb
, i
);
2573 dl
= serial_to_dl(disk
->serial
, super
);
2577 dl
= malloc(sizeof(*dl
));
2583 dl
->devname
= strdup("missing");
2585 serialcpy(dl
->serial
, disk
->serial
);
2588 dl
->next
= super
->missing
;
2589 super
->missing
= dl
;
2595 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
2597 struct intel_disk
*idisk
= disk_list
;
2600 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
2602 idisk
= idisk
->next
;
2608 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
2609 struct intel_super
*super
,
2610 struct intel_disk
**disk_list
)
2612 struct imsm_disk
*d
= &super
->disks
->disk
;
2613 struct imsm_super
*mpb
= super
->anchor
;
2616 for (i
= 0; i
< tbl_size
; i
++) {
2617 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
2618 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
2620 if (tbl_mpb
->family_num
== mpb
->family_num
) {
2621 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
2622 dprintf("%s: mpb from %d:%d matches %d:%d\n",
2623 __func__
, super
->disks
->major
,
2624 super
->disks
->minor
,
2625 table
[i
]->disks
->major
,
2626 table
[i
]->disks
->minor
);
2630 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
2631 is_configured(d
) == is_configured(tbl_d
)) &&
2632 tbl_mpb
->generation_num
< mpb
->generation_num
) {
2633 /* current version of the mpb is a
2634 * better candidate than the one in
2635 * super_table, but copy over "cross
2636 * generational" status
2638 struct intel_disk
*idisk
;
2640 dprintf("%s: mpb from %d:%d replaces %d:%d\n",
2641 __func__
, super
->disks
->major
,
2642 super
->disks
->minor
,
2643 table
[i
]->disks
->major
,
2644 table
[i
]->disks
->minor
);
2646 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
2647 if (idisk
&& is_failed(&idisk
->disk
))
2648 tbl_d
->status
|= FAILED_DISK
;
2651 struct intel_disk
*idisk
;
2652 struct imsm_disk
*disk
;
2654 /* tbl_mpb is more up to date, but copy
2655 * over cross generational status before
2658 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
2659 if (disk
&& is_failed(disk
))
2660 d
->status
|= FAILED_DISK
;
2662 idisk
= disk_list_get(d
->serial
, *disk_list
);
2665 if (disk
&& is_configured(disk
))
2666 idisk
->disk
.status
|= CONFIGURED_DISK
;
2669 dprintf("%s: mpb from %d:%d prefer %d:%d\n",
2670 __func__
, super
->disks
->major
,
2671 super
->disks
->minor
,
2672 table
[i
]->disks
->major
,
2673 table
[i
]->disks
->minor
);
2681 table
[tbl_size
++] = super
;
2685 /* update/extend the merged list of imsm_disk records */
2686 for (j
= 0; j
< mpb
->num_disks
; j
++) {
2687 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
2688 struct intel_disk
*idisk
;
2690 idisk
= disk_list_get(disk
->serial
, *disk_list
);
2692 idisk
->disk
.status
|= disk
->status
;
2693 if (is_configured(&idisk
->disk
) ||
2694 is_failed(&idisk
->disk
))
2695 idisk
->disk
.status
&= ~(SPARE_DISK
);
2697 idisk
= calloc(1, sizeof(*idisk
));
2700 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
2701 idisk
->disk
= *disk
;
2702 idisk
->next
= *disk_list
;
2706 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
2713 static struct intel_super
*
2714 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
2717 struct imsm_super
*mpb
= super
->anchor
;
2721 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2722 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
2723 struct intel_disk
*idisk
;
2725 idisk
= disk_list_get(disk
->serial
, disk_list
);
2727 if (idisk
->owner
== owner
||
2728 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
2731 dprintf("%s: '%.16s' owner %d != %d\n",
2732 __func__
, disk
->serial
, idisk
->owner
,
2735 dprintf("%s: unknown disk %x [%d]: %.16s\n",
2736 __func__
, __le32_to_cpu(mpb
->family_num
), i
,
2742 if (ok_count
== mpb
->num_disks
)
2747 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
2749 struct intel_super
*s
;
2751 for (s
= super_list
; s
; s
= s
->next
) {
2752 if (family_num
!= s
->anchor
->family_num
)
2754 fprintf(stderr
, "Conflict, offlining family %#x on '%s'\n",
2755 __le32_to_cpu(family_num
), s
->disks
->devname
);
2759 static struct intel_super
*
2760 imsm_thunderdome(struct intel_super
**super_list
, int len
)
2762 struct intel_super
*super_table
[len
];
2763 struct intel_disk
*disk_list
= NULL
;
2764 struct intel_super
*champion
, *spare
;
2765 struct intel_super
*s
, **del
;
2770 memset(super_table
, 0, sizeof(super_table
));
2771 for (s
= *super_list
; s
; s
= s
->next
)
2772 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
2774 for (i
= 0; i
< tbl_size
; i
++) {
2775 struct imsm_disk
*d
;
2776 struct intel_disk
*idisk
;
2777 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
2780 d
= &s
->disks
->disk
;
2782 /* 'd' must appear in merged disk list for its
2783 * configuration to be valid
2785 idisk
= disk_list_get(d
->serial
, disk_list
);
2786 if (idisk
&& idisk
->owner
== i
)
2787 s
= validate_members(s
, disk_list
, i
);
2792 dprintf("%s: marking family: %#x from %d:%d offline\n",
2793 __func__
, mpb
->family_num
,
2794 super_table
[i
]->disks
->major
,
2795 super_table
[i
]->disks
->minor
);
2799 /* This is where the mdadm implementation differs from the Windows
2800 * driver which has no strict concept of a container. We can only
2801 * assemble one family from a container, so when returning a prodigal
2802 * array member to this system the code will not be able to disambiguate
2803 * the container contents that should be assembled ("foreign" versus
2804 * "local"). It requires user intervention to set the orig_family_num
2805 * to a new value to establish a new container. The Windows driver in
2806 * this situation fixes up the volume name in place and manages the
2807 * foreign array as an independent entity.
2812 for (i
= 0; i
< tbl_size
; i
++) {
2813 struct intel_super
*tbl_ent
= super_table
[i
];
2819 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
2824 if (s
&& !is_spare
) {
2825 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
2827 } else if (!s
&& !is_spare
)
2840 fprintf(stderr
, "Chose family %#x on '%s', "
2841 "assemble conflicts to new container with '--update=uuid'\n",
2842 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
2844 /* collect all dl's onto 'champion', and update them to
2845 * champion's version of the status
2847 for (s
= *super_list
; s
; s
= s
->next
) {
2848 struct imsm_super
*mpb
= champion
->anchor
;
2849 struct dl
*dl
= s
->disks
;
2854 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2855 struct imsm_disk
*disk
;
2857 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
2860 /* only set index on disks that are a member of
2861 * a populated contianer, i.e. one with
2864 if (is_failed(&dl
->disk
))
2866 else if (is_spare(&dl
->disk
))
2872 if (i
>= mpb
->num_disks
) {
2873 struct intel_disk
*idisk
;
2875 idisk
= disk_list_get(dl
->serial
, disk_list
);
2876 if (idisk
&& is_spare(&idisk
->disk
) &&
2877 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
2885 dl
->next
= champion
->disks
;
2886 champion
->disks
= dl
;
2890 /* delete 'champion' from super_list */
2891 for (del
= super_list
; *del
; ) {
2892 if (*del
== champion
) {
2893 *del
= (*del
)->next
;
2896 del
= &(*del
)->next
;
2898 champion
->next
= NULL
;
2902 struct intel_disk
*idisk
= disk_list
;
2904 disk_list
= disk_list
->next
;
2911 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
2915 struct intel_super
*super_list
= NULL
;
2916 struct intel_super
*super
= NULL
;
2917 int devnum
= fd2devnum(fd
);
2923 /* check if 'fd' an opened container */
2924 sra
= sysfs_read(fd
, 0, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
2928 if (sra
->array
.major_version
!= -1 ||
2929 sra
->array
.minor_version
!= -2 ||
2930 strcmp(sra
->text_version
, "imsm") != 0) {
2935 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
2936 struct intel_super
*s
= alloc_super();
2943 s
->next
= super_list
;
2947 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
2948 dfd
= dev_open(nm
, O_RDWR
);
2952 err
= load_and_parse_mpb(dfd
, s
, NULL
, 1);
2954 /* retry the load if we might have raced against mdmon */
2955 if (err
== 3 && mdmon_running(devnum
))
2956 for (retry
= 0; retry
< 3; retry
++) {
2958 err
= load_and_parse_mpb(dfd
, s
, NULL
, 1);
2966 /* all mpbs enter, maybe one leaves */
2967 super
= imsm_thunderdome(&super_list
, i
);
2973 if (find_missing(super
) != 0) {
2981 while (super_list
) {
2982 struct intel_super
*s
= super_list
;
2984 super_list
= super_list
->next
;
2993 st
->container_dev
= devnum
;
2994 if (err
== 0 && st
->ss
== NULL
) {
2995 st
->ss
= &super_imsm
;
2996 st
->minor_version
= 0;
2997 st
->max_devs
= IMSM_MAX_DEVICES
;
3002 static int load_container_imsm(struct supertype
*st
, int fd
, char *devname
)
3004 return load_super_imsm_all(st
, fd
, &st
->sb
, devname
);
3008 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
3010 struct intel_super
*super
;
3014 if (load_super_imsm_all(st
, fd
, &st
->sb
, devname
) == 0)
3018 if (test_partition(fd
))
3019 /* IMSM not allowed on partitions */
3022 free_super_imsm(st
);
3024 super
= alloc_super();
3027 Name
": malloc of %zu failed.\n",
3032 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
3037 Name
": Failed to load all information "
3038 "sections on %s\n", devname
);
3044 if (st
->ss
== NULL
) {
3045 st
->ss
= &super_imsm
;
3046 st
->minor_version
= 0;
3047 st
->max_devs
= IMSM_MAX_DEVICES
;
3052 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
3054 if (info
->level
== 1)
3056 return info
->chunk_size
>> 9;
3059 static __u32
info_to_num_data_stripes(mdu_array_info_t
*info
, int num_domains
)
3063 num_stripes
= (info
->size
* 2) / info_to_blocks_per_strip(info
);
3064 num_stripes
/= num_domains
;
3069 static __u32
info_to_blocks_per_member(mdu_array_info_t
*info
)
3071 if (info
->level
== 1)
3072 return info
->size
* 2;
3074 return (info
->size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
3077 static void imsm_update_version_info(struct intel_super
*super
)
3079 /* update the version and attributes */
3080 struct imsm_super
*mpb
= super
->anchor
;
3082 struct imsm_dev
*dev
;
3083 struct imsm_map
*map
;
3086 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3087 dev
= get_imsm_dev(super
, i
);
3088 map
= get_imsm_map(dev
, 0);
3089 if (__le32_to_cpu(dev
->size_high
) > 0)
3090 mpb
->attributes
|= MPB_ATTRIB_2TB
;
3092 /* FIXME detect when an array spans a port multiplier */
3094 mpb
->attributes
|= MPB_ATTRIB_PM
;
3097 if (mpb
->num_raid_devs
> 1 ||
3098 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
3099 version
= MPB_VERSION_ATTRIBS
;
3100 switch (get_imsm_raid_level(map
)) {
3101 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
3102 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
3103 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
3104 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
3107 if (map
->num_members
>= 5)
3108 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
3109 else if (dev
->status
== DEV_CLONE_N_GO
)
3110 version
= MPB_VERSION_CNG
;
3111 else if (get_imsm_raid_level(map
) == 5)
3112 version
= MPB_VERSION_RAID5
;
3113 else if (map
->num_members
>= 3)
3114 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
3115 else if (get_imsm_raid_level(map
) == 1)
3116 version
= MPB_VERSION_RAID1
;
3118 version
= MPB_VERSION_RAID0
;
3120 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
3124 static int check_name(struct intel_super
*super
, char *name
, int quiet
)
3126 struct imsm_super
*mpb
= super
->anchor
;
3127 char *reason
= NULL
;
3130 if (strlen(name
) > MAX_RAID_SERIAL_LEN
)
3131 reason
= "must be 16 characters or less";
3133 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3134 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
3136 if (strncmp((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
) == 0) {
3137 reason
= "already exists";
3142 if (reason
&& !quiet
)
3143 fprintf(stderr
, Name
": imsm volume name %s\n", reason
);
3148 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
3149 unsigned long long size
, char *name
,
3150 char *homehost
, int *uuid
)
3152 /* We are creating a volume inside a pre-existing container.
3153 * so st->sb is already set.
3155 struct intel_super
*super
= st
->sb
;
3156 struct imsm_super
*mpb
= super
->anchor
;
3157 struct intel_dev
*dv
;
3158 struct imsm_dev
*dev
;
3159 struct imsm_vol
*vol
;
3160 struct imsm_map
*map
;
3161 int idx
= mpb
->num_raid_devs
;
3163 unsigned long long array_blocks
;
3164 size_t size_old
, size_new
;
3165 __u32 num_data_stripes
;
3167 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
3168 fprintf(stderr
, Name
": This imsm-container already has the "
3169 "maximum of %d volumes\n", super
->orom
->vpa
);
3173 /* ensure the mpb is large enough for the new data */
3174 size_old
= __le32_to_cpu(mpb
->mpb_size
);
3175 size_new
= disks_to_mpb_size(info
->nr_disks
);
3176 if (size_new
> size_old
) {
3178 size_t size_round
= ROUND_UP(size_new
, 512);
3180 if (posix_memalign(&mpb_new
, 512, size_round
) != 0) {
3181 fprintf(stderr
, Name
": could not allocate new mpb\n");
3184 memcpy(mpb_new
, mpb
, size_old
);
3187 super
->anchor
= mpb_new
;
3188 mpb
->mpb_size
= __cpu_to_le32(size_new
);
3189 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
3191 super
->current_vol
= idx
;
3192 /* when creating the first raid device in this container set num_disks
3193 * to zero, i.e. delete this spare and add raid member devices in
3194 * add_to_super_imsm_volume()
3196 if (super
->current_vol
== 0)
3199 if (!check_name(super
, name
, 0))
3201 dv
= malloc(sizeof(*dv
));
3203 fprintf(stderr
, Name
": failed to allocate device list entry\n");
3206 dev
= malloc(sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
3209 fprintf(stderr
, Name
": could not allocate raid device\n");
3212 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
3213 if (info
->level
== 1)
3214 array_blocks
= info_to_blocks_per_member(info
);
3216 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
3217 info
->layout
, info
->chunk_size
,
3219 /* round array size down to closest MB */
3220 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
3222 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
3223 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
3224 dev
->status
= __cpu_to_le32(0);
3225 dev
->reserved_blocks
= __cpu_to_le32(0);
3227 vol
->migr_state
= 0;
3228 set_migr_type(dev
, MIGR_INIT
);
3230 vol
->curr_migr_unit
= 0;
3231 map
= get_imsm_map(dev
, 0);
3232 map
->pba_of_lba0
= __cpu_to_le32(super
->create_offset
);
3233 map
->blocks_per_member
= __cpu_to_le32(info_to_blocks_per_member(info
));
3234 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
3235 map
->failed_disk_num
= ~0;
3236 map
->map_state
= info
->level
? IMSM_T_STATE_UNINITIALIZED
:
3237 IMSM_T_STATE_NORMAL
;
3240 if (info
->level
== 1 && info
->raid_disks
> 2) {
3243 fprintf(stderr
, Name
": imsm does not support more than 2 disks"
3244 "in a raid1 volume\n");
3248 map
->raid_level
= info
->level
;
3249 if (info
->level
== 10) {
3250 map
->raid_level
= 1;
3251 map
->num_domains
= info
->raid_disks
/ 2;
3252 } else if (info
->level
== 1)
3253 map
->num_domains
= info
->raid_disks
;
3255 map
->num_domains
= 1;
3257 num_data_stripes
= info_to_num_data_stripes(info
, map
->num_domains
);
3258 map
->num_data_stripes
= __cpu_to_le32(num_data_stripes
);
3260 map
->num_members
= info
->raid_disks
;
3261 for (i
= 0; i
< map
->num_members
; i
++) {
3262 /* initialized in add_to_super */
3263 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
3265 mpb
->num_raid_devs
++;
3268 dv
->index
= super
->current_vol
;
3269 dv
->next
= super
->devlist
;
3270 super
->devlist
= dv
;
3272 imsm_update_version_info(super
);
3277 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
3278 unsigned long long size
, char *name
,
3279 char *homehost
, int *uuid
)
3281 /* This is primarily called by Create when creating a new array.
3282 * We will then get add_to_super called for each component, and then
3283 * write_init_super called to write it out to each device.
3284 * For IMSM, Create can create on fresh devices or on a pre-existing
3286 * To create on a pre-existing array a different method will be called.
3287 * This one is just for fresh drives.
3289 struct intel_super
*super
;
3290 struct imsm_super
*mpb
;
3295 return init_super_imsm_volume(st
, info
, size
, name
, homehost
, uuid
);
3298 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
3302 super
= alloc_super();
3303 if (super
&& posix_memalign(&super
->buf
, 512, mpb_size
) != 0) {
3308 fprintf(stderr
, Name
3309 ": %s could not allocate superblock\n", __func__
);
3312 memset(super
->buf
, 0, mpb_size
);
3314 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
3318 /* zeroing superblock */
3322 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
3324 version
= (char *) mpb
->sig
;
3325 strcpy(version
, MPB_SIGNATURE
);
3326 version
+= strlen(MPB_SIGNATURE
);
3327 strcpy(version
, MPB_VERSION_RAID0
);
3333 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
3334 int fd
, char *devname
)
3336 struct intel_super
*super
= st
->sb
;
3337 struct imsm_super
*mpb
= super
->anchor
;
3339 struct imsm_dev
*dev
;
3340 struct imsm_map
*map
;
3343 dev
= get_imsm_dev(super
, super
->current_vol
);
3344 map
= get_imsm_map(dev
, 0);
3346 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
3347 fprintf(stderr
, Name
": %s: Cannot add spare devices to IMSM volume\n",
3353 /* we're doing autolayout so grab the pre-marked (in
3354 * validate_geometry) raid_disk
3356 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3357 if (dl
->raiddisk
== dk
->raid_disk
)
3360 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3361 if (dl
->major
== dk
->major
&&
3362 dl
->minor
== dk
->minor
)
3367 fprintf(stderr
, Name
": %s is not a member of the same container\n", devname
);
3371 /* add a pristine spare to the metadata */
3372 if (dl
->index
< 0) {
3373 dl
->index
= super
->anchor
->num_disks
;
3374 super
->anchor
->num_disks
++;
3376 /* Check the device has not already been added */
3377 slot
= get_imsm_disk_slot(map
, dl
->index
);
3379 (get_imsm_ord_tbl_ent(dev
, slot
) & IMSM_ORD_REBUILD
) == 0) {
3380 fprintf(stderr
, Name
": %s has been included in this array twice\n",
3384 set_imsm_ord_tbl_ent(map
, dk
->number
, dl
->index
);
3385 dl
->disk
.status
= CONFIGURED_DISK
;
3387 /* if we are creating the first raid device update the family number */
3388 if (super
->current_vol
== 0) {
3390 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
3391 struct imsm_disk
*_disk
= __get_imsm_disk(mpb
, dl
->index
);
3393 if (!_dev
|| !_disk
) {
3394 fprintf(stderr
, Name
": BUG mpb setup error\n");
3400 sum
+= __gen_imsm_checksum(mpb
);
3401 mpb
->family_num
= __cpu_to_le32(sum
);
3402 mpb
->orig_family_num
= mpb
->family_num
;
3408 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
3409 int fd
, char *devname
)
3411 struct intel_super
*super
= st
->sb
;
3413 unsigned long long size
;
3418 /* if we are on an RAID enabled platform check that the disk is
3419 * attached to the raid controller
3421 if (super
->hba
&& !disk_attached_to_hba(fd
, super
->hba
)) {
3423 Name
": %s is not attached to the raid controller: %s\n",
3424 devname
? : "disk", super
->hba
);
3428 if (super
->current_vol
>= 0)
3429 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
3432 dd
= malloc(sizeof(*dd
));
3435 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
3438 memset(dd
, 0, sizeof(*dd
));
3439 dd
->major
= major(stb
.st_rdev
);
3440 dd
->minor
= minor(stb
.st_rdev
);
3442 dd
->devname
= devname
? strdup(devname
) : NULL
;
3445 dd
->action
= DISK_ADD
;
3446 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
3449 Name
": failed to retrieve scsi serial, aborting\n");
3454 get_dev_size(fd
, NULL
, &size
);
3456 serialcpy(dd
->disk
.serial
, dd
->serial
);
3457 dd
->disk
.total_blocks
= __cpu_to_le32(size
);
3458 dd
->disk
.status
= SPARE_DISK
;
3459 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
3460 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
3462 dd
->disk
.scsi_id
= __cpu_to_le32(0);
3464 if (st
->update_tail
) {
3465 dd
->next
= super
->disk_mgmt_list
;
3466 super
->disk_mgmt_list
= dd
;
3468 dd
->next
= super
->disks
;
3476 static int remove_from_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
)
3478 struct intel_super
*super
= st
->sb
;
3481 /* remove from super works only in mdmon - for communication
3482 * manager - monitor. Check if communication memory buffer
3485 if (!st
->update_tail
) {
3487 Name
": %s shall be used in mdmon context only"
3488 "(line %d).\n", __func__
, __LINE__
);
3491 dd
= malloc(sizeof(*dd
));
3494 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
3497 memset(dd
, 0, sizeof(*dd
));
3498 dd
->major
= dk
->major
;
3499 dd
->minor
= dk
->minor
;
3502 dd
->disk
.status
= SPARE_DISK
;
3503 dd
->action
= DISK_REMOVE
;
3505 dd
->next
= super
->disk_mgmt_list
;
3506 super
->disk_mgmt_list
= dd
;
3512 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
3516 struct imsm_super anchor
;
3517 } spare_record
__attribute__ ((aligned(512)));
3519 /* spare records have their own family number and do not have any defined raid
3522 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
3524 struct imsm_super
*mpb
= super
->anchor
;
3525 struct imsm_super
*spare
= &spare_record
.anchor
;
3529 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
)),
3530 spare
->generation_num
= __cpu_to_le32(1UL),
3531 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
3532 spare
->num_disks
= 1,
3533 spare
->num_raid_devs
= 0,
3534 spare
->cache_size
= mpb
->cache_size
,
3535 spare
->pwr_cycle_count
= __cpu_to_le32(1),
3537 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
3538 MPB_SIGNATURE MPB_VERSION_RAID0
);
3540 for (d
= super
->disks
; d
; d
= d
->next
) {
3544 spare
->disk
[0] = d
->disk
;
3545 sum
= __gen_imsm_checksum(spare
);
3546 spare
->family_num
= __cpu_to_le32(sum
);
3547 spare
->orig_family_num
= 0;
3548 sum
= __gen_imsm_checksum(spare
);
3549 spare
->check_sum
= __cpu_to_le32(sum
);
3551 if (store_imsm_mpb(d
->fd
, spare
)) {
3552 fprintf(stderr
, "%s: failed for device %d:%d %s\n",
3553 __func__
, d
->major
, d
->minor
, strerror(errno
));
3565 static int write_super_imsm(struct supertype
*st
, int doclose
)
3567 struct intel_super
*super
= st
->sb
;
3568 struct imsm_super
*mpb
= super
->anchor
;
3574 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
3577 /* 'generation' is incremented everytime the metadata is written */
3578 generation
= __le32_to_cpu(mpb
->generation_num
);
3580 mpb
->generation_num
= __cpu_to_le32(generation
);
3582 /* fix up cases where previous mdadm releases failed to set
3585 if (mpb
->orig_family_num
== 0)
3586 mpb
->orig_family_num
= mpb
->family_num
;
3588 for (d
= super
->disks
; d
; d
= d
->next
) {
3592 mpb
->disk
[d
->index
] = d
->disk
;
3596 for (d
= super
->missing
; d
; d
= d
->next
) {
3597 mpb
->disk
[d
->index
] = d
->disk
;
3600 mpb
->num_disks
= num_disks
;
3601 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
3603 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3604 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
3605 struct imsm_dev
*dev2
= get_imsm_dev(super
, i
);
3607 imsm_copy_dev(dev
, dev2
);
3608 mpb_size
+= sizeof_imsm_dev(dev
, 0);
3611 mpb_size
+= __le32_to_cpu(mpb
->bbm_log_size
);
3612 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
3614 /* recalculate checksum */
3615 sum
= __gen_imsm_checksum(mpb
);
3616 mpb
->check_sum
= __cpu_to_le32(sum
);
3618 /* write the mpb for disks that compose raid devices */
3619 for (d
= super
->disks
; d
; d
= d
->next
) {
3622 if (store_imsm_mpb(d
->fd
, mpb
))
3623 fprintf(stderr
, "%s: failed for device %d:%d %s\n",
3624 __func__
, d
->major
, d
->minor
, strerror(errno
));
3632 return write_super_imsm_spares(super
, doclose
);
3638 static int create_array(struct supertype
*st
, int dev_idx
)
3641 struct imsm_update_create_array
*u
;
3642 struct intel_super
*super
= st
->sb
;
3643 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
3644 struct imsm_map
*map
= get_imsm_map(dev
, 0);
3645 struct disk_info
*inf
;
3646 struct imsm_disk
*disk
;
3649 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
3650 sizeof(*inf
) * map
->num_members
;
3653 fprintf(stderr
, "%s: failed to allocate update buffer\n",
3658 u
->type
= update_create_array
;
3659 u
->dev_idx
= dev_idx
;
3660 imsm_copy_dev(&u
->dev
, dev
);
3661 inf
= get_disk_info(u
);
3662 for (i
= 0; i
< map
->num_members
; i
++) {
3663 int idx
= get_imsm_disk_idx(dev
, i
);
3665 disk
= get_imsm_disk(super
, idx
);
3666 serialcpy(inf
[i
].serial
, disk
->serial
);
3668 append_metadata_update(st
, u
, len
);
3673 static int mgmt_disk(struct supertype
*st
)
3675 struct intel_super
*super
= st
->sb
;
3677 struct imsm_update_add_remove_disk
*u
;
3679 if (!super
->disk_mgmt_list
)
3685 fprintf(stderr
, "%s: failed to allocate update buffer\n",
3690 u
->type
= update_add_remove_disk
;
3691 append_metadata_update(st
, u
, len
);
3696 static int write_init_super_imsm(struct supertype
*st
)
3698 struct intel_super
*super
= st
->sb
;
3699 int current_vol
= super
->current_vol
;
3701 /* we are done with current_vol reset it to point st at the container */
3702 super
->current_vol
= -1;
3704 if (st
->update_tail
) {
3705 /* queue the recently created array / added disk
3706 * as a metadata update */
3710 /* determine if we are creating a volume or adding a disk */
3711 if (current_vol
< 0) {
3712 /* in the mgmt (add/remove) disk case we are running
3713 * in mdmon context, so don't close fd's
3715 return mgmt_disk(st
);
3717 rv
= create_array(st
, current_vol
);
3719 for (d
= super
->disks
; d
; d
= d
->next
) {
3727 for (d
= super
->disks
; d
; d
= d
->next
)
3728 Kill(d
->devname
, NULL
, 0, 1, 1);
3729 return write_super_imsm(st
, 1);
3734 static int store_super_imsm(struct supertype
*st
, int fd
)
3736 struct intel_super
*super
= st
->sb
;
3737 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
3743 return store_imsm_mpb(fd
, mpb
);
3749 static int imsm_bbm_log_size(struct imsm_super
*mpb
)
3751 return __le32_to_cpu(mpb
->bbm_log_size
);
3755 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
3756 int layout
, int raiddisks
, int chunk
,
3757 unsigned long long size
, char *dev
,
3758 unsigned long long *freesize
,
3762 unsigned long long ldsize
;
3763 const struct imsm_orom
*orom
;
3765 if (level
!= LEVEL_CONTAINER
)
3770 if (check_env("IMSM_NO_PLATFORM"))
3773 orom
= find_imsm_orom();
3774 if (orom
&& raiddisks
> orom
->tds
) {
3776 fprintf(stderr
, Name
": %d exceeds maximum number of"
3777 " platform supported disks: %d\n",
3778 raiddisks
, orom
->tds
);
3782 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
3785 fprintf(stderr
, Name
": imsm: Cannot open %s: %s\n",
3786 dev
, strerror(errno
));
3789 if (!get_dev_size(fd
, dev
, &ldsize
)) {
3795 *freesize
= avail_size_imsm(st
, ldsize
>> 9);
3800 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
3802 const unsigned long long base_start
= e
[*idx
].start
;
3803 unsigned long long end
= base_start
+ e
[*idx
].size
;
3806 if (base_start
== end
)
3810 for (i
= *idx
; i
< num_extents
; i
++) {
3811 /* extend overlapping extents */
3812 if (e
[i
].start
>= base_start
&&
3813 e
[i
].start
<= end
) {
3816 if (e
[i
].start
+ e
[i
].size
> end
)
3817 end
= e
[i
].start
+ e
[i
].size
;
3818 } else if (e
[i
].start
> end
) {
3824 return end
- base_start
;
3827 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
3829 /* build a composite disk with all known extents and generate a new
3830 * 'maxsize' given the "all disks in an array must share a common start
3831 * offset" constraint
3833 struct extent
*e
= calloc(sum_extents
, sizeof(*e
));
3837 unsigned long long pos
;
3838 unsigned long long start
= 0;
3839 unsigned long long maxsize
;
3840 unsigned long reserve
;
3845 /* coalesce and sort all extents. also, check to see if we need to
3846 * reserve space between member arrays
3849 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
3852 for (i
= 0; i
< dl
->extent_cnt
; i
++)
3855 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
3860 while (i
< sum_extents
) {
3861 e
[j
].start
= e
[i
].start
;
3862 e
[j
].size
= find_size(e
, &i
, sum_extents
);
3864 if (e
[j
-1].size
== 0)
3873 unsigned long long esize
;
3875 esize
= e
[i
].start
- pos
;
3876 if (esize
>= maxsize
) {
3881 pos
= e
[i
].start
+ e
[i
].size
;
3883 } while (e
[i
-1].size
);
3889 /* FIXME assumes volume at offset 0 is the first volume in a
3892 if (start_extent
> 0)
3893 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
3897 if (maxsize
< reserve
)
3900 super
->create_offset
= ~((__u32
) 0);
3901 if (start
+ reserve
> super
->create_offset
)
3902 return 0; /* start overflows create_offset */
3903 super
->create_offset
= start
+ reserve
;
3905 return maxsize
- reserve
;
3908 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
3910 if (level
< 0 || level
== 6 || level
== 4)
3913 /* if we have an orom prevent invalid raid levels */
3916 case 0: return imsm_orom_has_raid0(orom
);
3919 return imsm_orom_has_raid1e(orom
);
3920 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
3921 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
3922 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
3925 return 1; /* not on an Intel RAID platform so anything goes */
3930 #define pr_vrb(fmt, arg...) (void) (verbose && fprintf(stderr, Name fmt, ##arg))
3932 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
3933 int raiddisks
, int chunk
, int verbose
)
3935 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
3936 pr_vrb(": platform does not support raid%d with %d disk%s\n",
3937 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
3940 if (super
->orom
&& level
!= 1 &&
3941 !imsm_orom_has_chunk(super
->orom
, chunk
)) {
3942 pr_vrb(": platform does not support a chunk size of: %d\n", chunk
);
3945 if (layout
!= imsm_level_to_layout(level
)) {
3947 pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
3948 else if (level
== 10)
3949 pr_vrb(": imsm raid 10 only supports the n2 layout\n");
3951 pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
3959 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
3960 * FIX ME add ahci details
3962 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
3963 int layout
, int raiddisks
, int chunk
,
3964 unsigned long long size
, char *dev
,
3965 unsigned long long *freesize
,
3969 struct intel_super
*super
= st
->sb
;
3970 struct imsm_super
*mpb
= super
->anchor
;
3972 unsigned long long pos
= 0;
3973 unsigned long long maxsize
;
3977 /* We must have the container info already read in. */
3981 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, verbose
))
3985 /* General test: make sure there is space for
3986 * 'raiddisks' device extents of size 'size' at a given
3989 unsigned long long minsize
= size
;
3990 unsigned long long start_offset
= MaxSector
;
3993 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
3994 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
3999 e
= get_extents(super
, dl
);
4002 unsigned long long esize
;
4003 esize
= e
[i
].start
- pos
;
4004 if (esize
>= minsize
)
4006 if (found
&& start_offset
== MaxSector
) {
4009 } else if (found
&& pos
!= start_offset
) {
4013 pos
= e
[i
].start
+ e
[i
].size
;
4015 } while (e
[i
-1].size
);
4020 if (dcnt
< raiddisks
) {
4022 fprintf(stderr
, Name
": imsm: Not enough "
4023 "devices with space for this array "
4031 /* This device must be a member of the set */
4032 if (stat(dev
, &stb
) < 0)
4034 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
4036 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
4037 if (dl
->major
== (int)major(stb
.st_rdev
) &&
4038 dl
->minor
== (int)minor(stb
.st_rdev
))
4043 fprintf(stderr
, Name
": %s is not in the "
4044 "same imsm set\n", dev
);
4046 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
4047 /* If a volume is present then the current creation attempt
4048 * cannot incorporate new spares because the orom may not
4049 * understand this configuration (all member disks must be
4050 * members of each array in the container).
4052 fprintf(stderr
, Name
": %s is a spare and a volume"
4053 " is already defined for this container\n", dev
);
4054 fprintf(stderr
, Name
": The option-rom requires all member"
4055 " disks to be a member of all volumes\n");
4059 /* retrieve the largest free space block */
4060 e
= get_extents(super
, dl
);
4065 unsigned long long esize
;
4067 esize
= e
[i
].start
- pos
;
4068 if (esize
>= maxsize
)
4070 pos
= e
[i
].start
+ e
[i
].size
;
4072 } while (e
[i
-1].size
);
4077 fprintf(stderr
, Name
": unable to determine free space for: %s\n",
4081 if (maxsize
< size
) {
4083 fprintf(stderr
, Name
": %s not enough space (%llu < %llu)\n",
4084 dev
, maxsize
, size
);
4088 /* count total number of extents for merge */
4090 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4092 i
+= dl
->extent_cnt
;
4094 maxsize
= merge_extents(super
, i
);
4095 if (maxsize
< size
|| maxsize
== 0) {
4097 fprintf(stderr
, Name
": not enough space after merge (%llu < %llu)\n",
4102 *freesize
= maxsize
;
4107 static int reserve_space(struct supertype
*st
, int raiddisks
,
4108 unsigned long long size
, int chunk
,
4109 unsigned long long *freesize
)
4111 struct intel_super
*super
= st
->sb
;
4112 struct imsm_super
*mpb
= super
->anchor
;
4117 unsigned long long maxsize
;
4118 unsigned long long minsize
;
4122 /* find the largest common start free region of the possible disks */
4126 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
4132 /* don't activate new spares if we are orom constrained
4133 * and there is already a volume active in the container
4135 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
4138 e
= get_extents(super
, dl
);
4141 for (i
= 1; e
[i
-1].size
; i
++)
4149 maxsize
= merge_extents(super
, extent_cnt
);
4154 if (cnt
< raiddisks
||
4155 (super
->orom
&& used
&& used
!= raiddisks
) ||
4156 maxsize
< minsize
||
4158 fprintf(stderr
, Name
": not enough devices with space to create array.\n");
4159 return 0; /* No enough free spaces large enough */
4171 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4173 dl
->raiddisk
= cnt
++;
4180 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
4181 int raiddisks
, int chunk
, unsigned long long size
,
4182 char *dev
, unsigned long long *freesize
,
4189 /* if given unused devices create a container
4190 * if given given devices in a container create a member volume
4192 if (level
== LEVEL_CONTAINER
) {
4193 /* Must be a fresh device to add to a container */
4194 return validate_geometry_imsm_container(st
, level
, layout
,
4195 raiddisks
, chunk
, size
,
4201 if (st
->sb
&& freesize
) {
4202 /* we are being asked to automatically layout a
4203 * new volume based on the current contents of
4204 * the container. If the the parameters can be
4205 * satisfied reserve_space will record the disks,
4206 * start offset, and size of the volume to be
4207 * created. add_to_super and getinfo_super
4208 * detect when autolayout is in progress.
4210 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
4214 return reserve_space(st
, raiddisks
, size
, chunk
, freesize
);
4219 /* creating in a given container */
4220 return validate_geometry_imsm_volume(st
, level
, layout
,
4221 raiddisks
, chunk
, size
,
4222 dev
, freesize
, verbose
);
4225 /* This device needs to be a device in an 'imsm' container */
4226 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
4230 Name
": Cannot create this array on device %s\n",
4235 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
4237 fprintf(stderr
, Name
": Cannot open %s: %s\n",
4238 dev
, strerror(errno
));
4241 /* Well, it is in use by someone, maybe an 'imsm' container. */
4242 cfd
= open_container(fd
);
4246 fprintf(stderr
, Name
": Cannot use %s: It is busy\n",
4250 sra
= sysfs_read(cfd
, 0, GET_VERSION
);
4251 if (sra
&& sra
->array
.major_version
== -1 &&
4252 strcmp(sra
->text_version
, "imsm") == 0)
4256 /* This is a member of a imsm container. Load the container
4257 * and try to create a volume
4259 struct intel_super
*super
;
4261 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
) == 0) {
4263 st
->container_dev
= fd2devnum(cfd
);
4265 return validate_geometry_imsm_volume(st
, level
, layout
,
4273 fprintf(stderr
, Name
": failed container membership check\n");
4279 static void default_geometry_imsm(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
4281 struct intel_super
*super
= st
->sb
;
4283 if (level
&& *level
== UnSet
)
4284 *level
= LEVEL_CONTAINER
;
4286 if (level
&& layout
&& *layout
== UnSet
)
4287 *layout
= imsm_level_to_layout(*level
);
4289 if (chunk
&& (*chunk
== UnSet
|| *chunk
== 0) &&
4290 super
&& super
->orom
)
4291 *chunk
= imsm_orom_default_chunk(super
->orom
);
4294 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
);
4296 static int kill_subarray_imsm(struct supertype
*st
)
4298 /* remove the subarray currently referenced by ->current_vol */
4300 struct intel_dev
**dp
;
4301 struct intel_super
*super
= st
->sb
;
4302 __u8 current_vol
= super
->current_vol
;
4303 struct imsm_super
*mpb
= super
->anchor
;
4305 if (super
->current_vol
< 0)
4307 super
->current_vol
= -1; /* invalidate subarray cursor */
4309 /* block deletions that would change the uuid of active subarrays
4311 * FIXME when immutable ids are available, but note that we'll
4312 * also need to fixup the invalidated/active subarray indexes in
4315 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4318 if (i
< current_vol
)
4320 sprintf(subarray
, "%u", i
);
4321 if (is_subarray_active(subarray
, st
->devname
)) {
4323 Name
": deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
4330 if (st
->update_tail
) {
4331 struct imsm_update_kill_array
*u
= malloc(sizeof(*u
));
4335 u
->type
= update_kill_array
;
4336 u
->dev_idx
= current_vol
;
4337 append_metadata_update(st
, u
, sizeof(*u
));
4342 for (dp
= &super
->devlist
; *dp
;)
4343 if ((*dp
)->index
== current_vol
) {
4346 handle_missing(super
, (*dp
)->dev
);
4347 if ((*dp
)->index
> current_vol
)
4352 /* no more raid devices, all active components are now spares,
4353 * but of course failed are still failed
4355 if (--mpb
->num_raid_devs
== 0) {
4358 for (d
= super
->disks
; d
; d
= d
->next
)
4359 if (d
->index
> -2) {
4361 d
->disk
.status
= SPARE_DISK
;
4365 super
->updates_pending
++;
4370 static int update_subarray_imsm(struct supertype
*st
, char *subarray
,
4371 char *update
, struct mddev_ident
*ident
)
4373 /* update the subarray currently referenced by ->current_vol */
4374 struct intel_super
*super
= st
->sb
;
4375 struct imsm_super
*mpb
= super
->anchor
;
4377 if (strcmp(update
, "name") == 0) {
4378 char *name
= ident
->name
;
4382 if (is_subarray_active(subarray
, st
->devname
)) {
4384 Name
": Unable to update name of active subarray\n");
4388 if (!check_name(super
, name
, 0))
4391 vol
= strtoul(subarray
, &ep
, 10);
4392 if (*ep
!= '\0' || vol
>= super
->anchor
->num_raid_devs
)
4395 if (st
->update_tail
) {
4396 struct imsm_update_rename_array
*u
= malloc(sizeof(*u
));
4400 u
->type
= update_rename_array
;
4402 snprintf((char *) u
->name
, MAX_RAID_SERIAL_LEN
, "%s", name
);
4403 append_metadata_update(st
, u
, sizeof(*u
));
4405 struct imsm_dev
*dev
;
4408 dev
= get_imsm_dev(super
, vol
);
4409 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
4410 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4411 dev
= get_imsm_dev(super
, i
);
4412 handle_missing(super
, dev
);
4414 super
->updates_pending
++;
4421 #endif /* MDASSEMBLE */
4423 static int is_gen_migration(struct imsm_dev
*dev
)
4425 if (!dev
->vol
.migr_state
)
4428 if (migr_type(dev
) == MIGR_GEN_MIGR
)
4434 static int is_rebuilding(struct imsm_dev
*dev
)
4436 struct imsm_map
*migr_map
;
4438 if (!dev
->vol
.migr_state
)
4441 if (migr_type(dev
) != MIGR_REBUILD
)
4444 migr_map
= get_imsm_map(dev
, 1);
4446 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
4452 static void update_recovery_start(struct imsm_dev
*dev
, struct mdinfo
*array
)
4454 struct mdinfo
*rebuild
= NULL
;
4458 if (!is_rebuilding(dev
))
4461 /* Find the rebuild target, but punt on the dual rebuild case */
4462 for (d
= array
->devs
; d
; d
= d
->next
)
4463 if (d
->recovery_start
== 0) {
4470 /* (?) none of the disks are marked with
4471 * IMSM_ORD_REBUILD, so assume they are missing and the
4472 * disk_ord_tbl was not correctly updated
4474 dprintf("%s: failed to locate out-of-sync disk\n", __func__
);
4478 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
4479 rebuild
->recovery_start
= units
* blocks_per_migr_unit(dev
);
4483 static struct mdinfo
*container_content_imsm(struct supertype
*st
, char *subarray
)
4485 /* Given a container loaded by load_super_imsm_all,
4486 * extract information about all the arrays into
4488 * If 'subarray' is given, just extract info about that array.
4490 * For each imsm_dev create an mdinfo, fill it in,
4491 * then look for matching devices in super->disks
4492 * and create appropriate device mdinfo.
4494 struct intel_super
*super
= st
->sb
;
4495 struct imsm_super
*mpb
= super
->anchor
;
4496 struct mdinfo
*rest
= NULL
;
4499 /* do not assemble arrays that might have bad blocks */
4500 if (imsm_bbm_log_size(super
->anchor
)) {
4501 fprintf(stderr
, Name
": BBM log found in metadata. "
4502 "Cannot activate array(s).\n");
4506 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4507 struct imsm_dev
*dev
;
4508 struct imsm_map
*map
;
4509 struct mdinfo
*this;
4514 (i
!= strtoul(subarray
, &ep
, 10) || *ep
!= '\0'))
4517 dev
= get_imsm_dev(super
, i
);
4518 map
= get_imsm_map(dev
, 0);
4520 /* do not publish arrays that are in the middle of an
4521 * unsupported migration
4523 if (dev
->vol
.migr_state
&&
4524 (migr_type(dev
) == MIGR_STATE_CHANGE
)) {
4525 fprintf(stderr
, Name
": cannot assemble volume '%.16s':"
4526 " unsupported migration in progress\n",
4531 this = malloc(sizeof(*this));
4533 fprintf(stderr
, Name
": failed to allocate %zu bytes\n",
4537 memset(this, 0, sizeof(*this));
4540 super
->current_vol
= i
;
4541 getinfo_super_imsm_volume(st
, this, NULL
);
4542 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
4543 unsigned long long recovery_start
;
4544 struct mdinfo
*info_d
;
4551 idx
= get_imsm_disk_idx(dev
, slot
);
4552 ord
= get_imsm_ord_tbl_ent(dev
, slot
);
4553 for (d
= super
->disks
; d
; d
= d
->next
)
4554 if (d
->index
== idx
)
4557 recovery_start
= MaxSector
;
4560 if (d
&& is_failed(&d
->disk
))
4562 if (ord
& IMSM_ORD_REBUILD
)
4566 * if we skip some disks the array will be assmebled degraded;
4567 * reset resync start to avoid a dirty-degraded
4568 * situation when performing the intial sync
4570 * FIXME handle dirty degraded
4572 if ((skip
|| recovery_start
== 0) && !dev
->vol
.dirty
)
4573 this->resync_start
= MaxSector
;
4577 info_d
= calloc(1, sizeof(*info_d
));
4579 fprintf(stderr
, Name
": failed to allocate disk"
4580 " for volume %.16s\n", dev
->volume
);
4581 info_d
= this->devs
;
4583 struct mdinfo
*d
= info_d
->next
;
4592 info_d
->next
= this->devs
;
4593 this->devs
= info_d
;
4595 info_d
->disk
.number
= d
->index
;
4596 info_d
->disk
.major
= d
->major
;
4597 info_d
->disk
.minor
= d
->minor
;
4598 info_d
->disk
.raid_disk
= slot
;
4599 info_d
->recovery_start
= recovery_start
;
4601 if (info_d
->recovery_start
== MaxSector
)
4602 this->array
.working_disks
++;
4604 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
4605 info_d
->data_offset
= __le32_to_cpu(map
->pba_of_lba0
);
4606 info_d
->component_size
= __le32_to_cpu(map
->blocks_per_member
);
4608 /* now that the disk list is up-to-date fixup recovery_start */
4609 update_recovery_start(dev
, this);
4617 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
, int failed
)
4619 struct imsm_map
*map
= get_imsm_map(dev
, 0);
4622 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
4623 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
4625 switch (get_imsm_raid_level(map
)) {
4627 return IMSM_T_STATE_FAILED
;
4630 if (failed
< map
->num_members
)
4631 return IMSM_T_STATE_DEGRADED
;
4633 return IMSM_T_STATE_FAILED
;
4638 * check to see if any mirrors have failed, otherwise we
4639 * are degraded. Even numbered slots are mirrored on
4643 /* gcc -Os complains that this is unused */
4644 int insync
= insync
;
4646 for (i
= 0; i
< map
->num_members
; i
++) {
4647 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
);
4648 int idx
= ord_to_idx(ord
);
4649 struct imsm_disk
*disk
;
4651 /* reset the potential in-sync count on even-numbered
4652 * slots. num_copies is always 2 for imsm raid10
4657 disk
= get_imsm_disk(super
, idx
);
4658 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
4661 /* no in-sync disks left in this mirror the
4665 return IMSM_T_STATE_FAILED
;
4668 return IMSM_T_STATE_DEGRADED
;
4672 return IMSM_T_STATE_DEGRADED
;
4674 return IMSM_T_STATE_FAILED
;
4680 return map
->map_state
;
4683 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
)
4687 struct imsm_disk
*disk
;
4688 struct imsm_map
*map
= get_imsm_map(dev
, 0);
4689 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
);
4693 /* at the beginning of migration we set IMSM_ORD_REBUILD on
4694 * disks that are being rebuilt. New failures are recorded to
4695 * map[0]. So we look through all the disks we started with and
4696 * see if any failures are still present, or if any new ones
4699 * FIXME add support for online capacity expansion and
4700 * raid-level-migration
4702 for (i
= 0; i
< prev
->num_members
; i
++) {
4703 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
4704 ord
|= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
4705 idx
= ord_to_idx(ord
);
4707 disk
= get_imsm_disk(super
, idx
);
4708 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
4716 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
4719 struct intel_super
*super
= c
->sb
;
4720 struct imsm_super
*mpb
= super
->anchor
;
4722 if (atoi(inst
) >= mpb
->num_raid_devs
) {
4723 fprintf(stderr
, "%s: subarry index %d, out of range\n",
4724 __func__
, atoi(inst
));
4728 dprintf("imsm: open_new %s\n", inst
);
4729 a
->info
.container_member
= atoi(inst
);
4733 static int is_resyncing(struct imsm_dev
*dev
)
4735 struct imsm_map
*migr_map
;
4737 if (!dev
->vol
.migr_state
)
4740 if (migr_type(dev
) == MIGR_INIT
||
4741 migr_type(dev
) == MIGR_REPAIR
)
4744 migr_map
= get_imsm_map(dev
, 1);
4746 if (migr_map
->map_state
== IMSM_T_STATE_NORMAL
)
4752 /* return true if we recorded new information */
4753 static int mark_failure(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
4757 struct imsm_map
*map
;
4759 /* new failures are always set in map[0] */
4760 map
= get_imsm_map(dev
, 0);
4762 slot
= get_imsm_disk_slot(map
, idx
);
4766 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
4767 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
4770 disk
->status
|= FAILED_DISK
;
4771 disk
->status
&= ~CONFIGURED_DISK
;
4772 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
4773 if (map
->failed_disk_num
== 0xff)
4774 map
->failed_disk_num
= slot
;
4778 static void mark_missing(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
4780 mark_failure(dev
, disk
, idx
);
4782 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
4785 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
4786 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
4789 static void handle_missing(struct intel_super
*super
, struct imsm_dev
*dev
)
4795 if (!super
->missing
)
4797 failed
= imsm_count_failed(super
, dev
);
4798 map_state
= imsm_check_degraded(super
, dev
, failed
);
4800 dprintf("imsm: mark missing\n");
4801 end_migration(dev
, map_state
);
4802 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
4803 mark_missing(dev
, &dl
->disk
, dl
->index
);
4804 super
->updates_pending
++;
4807 static void imsm_set_disk(struct active_array
*a
, int n
, int state
);
4809 /* Handle dirty -> clean transititions and resync. Degraded and rebuild
4810 * states are handled in imsm_set_disk() with one exception, when a
4811 * resync is stopped due to a new failure this routine will set the
4812 * 'degraded' state for the array.
4814 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
4816 int inst
= a
->info
.container_member
;
4817 struct intel_super
*super
= a
->container
->sb
;
4818 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
4819 struct imsm_map
*map
= get_imsm_map(dev
, 0);
4820 int failed
= imsm_count_failed(super
, dev
);
4821 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
);
4822 __u32 blocks_per_unit
;
4824 /* before we activate this array handle any missing disks */
4825 if (consistent
== 2)
4826 handle_missing(super
, dev
);
4828 if (consistent
== 2 &&
4829 (!is_resync_complete(&a
->info
) ||
4830 map_state
!= IMSM_T_STATE_NORMAL
||
4831 dev
->vol
.migr_state
))
4834 if (is_resync_complete(&a
->info
)) {
4835 /* complete intialization / resync,
4836 * recovery and interrupted recovery is completed in
4839 if (is_resyncing(dev
)) {
4840 dprintf("imsm: mark resync done\n");
4841 end_migration(dev
, map_state
);
4842 super
->updates_pending
++;
4843 a
->last_checkpoint
= 0;
4845 } else if (!is_resyncing(dev
) && !failed
) {
4846 /* mark the start of the init process if nothing is failed */
4847 dprintf("imsm: mark resync start\n");
4848 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
4849 migrate(dev
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
4851 migrate(dev
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
4852 super
->updates_pending
++;
4855 /* check if we can update curr_migr_unit from resync_start, recovery_start */
4856 blocks_per_unit
= blocks_per_migr_unit(dev
);
4857 if (blocks_per_unit
) {
4861 units
= a
->last_checkpoint
/ blocks_per_unit
;
4864 /* check that we did not overflow 32-bits, and that
4865 * curr_migr_unit needs updating
4867 if (units32
== units
&&
4868 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
4869 dprintf("imsm: mark checkpoint (%u)\n", units32
);
4870 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
4871 super
->updates_pending
++;
4875 /* mark dirty / clean */
4876 if (dev
->vol
.dirty
!= !consistent
) {
4877 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
4882 super
->updates_pending
++;
4885 /* finalize online capacity expansion/reshape */
4886 if ((a
->curr_action
!= reshape
) &&
4887 (a
->prev_action
== reshape
)) {
4890 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
4891 imsm_set_disk(a
, mdi
->disk
.raid_disk
, mdi
->curr_state
);
4897 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
4899 int inst
= a
->info
.container_member
;
4900 struct intel_super
*super
= a
->container
->sb
;
4901 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
4902 struct imsm_map
*map
= get_imsm_map(dev
, 0);
4903 struct imsm_disk
*disk
;
4908 if (n
> map
->num_members
)
4909 fprintf(stderr
, "imsm: set_disk %d out of range 0..%d\n",
4910 n
, map
->num_members
- 1);
4915 dprintf("imsm: set_disk %d:%x\n", n
, state
);
4917 ord
= get_imsm_ord_tbl_ent(dev
, n
);
4918 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
4920 /* check for new failures */
4921 if (state
& DS_FAULTY
) {
4922 if (mark_failure(dev
, disk
, ord_to_idx(ord
)))
4923 super
->updates_pending
++;
4926 /* check if in_sync */
4927 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
4928 struct imsm_map
*migr_map
= get_imsm_map(dev
, 1);
4930 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
4931 super
->updates_pending
++;
4934 failed
= imsm_count_failed(super
, dev
);
4935 map_state
= imsm_check_degraded(super
, dev
, failed
);
4937 /* check if recovery complete, newly degraded, or failed */
4938 if (map_state
== IMSM_T_STATE_NORMAL
&& is_rebuilding(dev
)) {
4939 end_migration(dev
, map_state
);
4940 map
= get_imsm_map(dev
, 0);
4941 map
->failed_disk_num
= ~0;
4942 super
->updates_pending
++;
4943 a
->last_checkpoint
= 0;
4944 } else if (map_state
== IMSM_T_STATE_DEGRADED
&&
4945 map
->map_state
!= map_state
&&
4946 !dev
->vol
.migr_state
) {
4947 dprintf("imsm: mark degraded\n");
4948 map
->map_state
= map_state
;
4949 super
->updates_pending
++;
4950 a
->last_checkpoint
= 0;
4951 } else if (map_state
== IMSM_T_STATE_FAILED
&&
4952 map
->map_state
!= map_state
) {
4953 dprintf("imsm: mark failed\n");
4954 end_migration(dev
, map_state
);
4955 super
->updates_pending
++;
4956 a
->last_checkpoint
= 0;
4957 } else if (is_gen_migration(dev
)) {
4958 dprintf("imsm: Detected General Migration in state: ");
4959 if (map_state
== IMSM_T_STATE_NORMAL
) {
4960 end_migration(dev
, map_state
);
4961 map
= get_imsm_map(dev
, 0);
4962 map
->failed_disk_num
= ~0;
4963 dprintf("normal\n");
4965 if (map_state
== IMSM_T_STATE_DEGRADED
) {
4966 printf("degraded\n");
4967 end_migration(dev
, map_state
);
4969 dprintf("failed\n");
4971 map
->map_state
= map_state
;
4973 super
->updates_pending
++;
4977 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
4980 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
4981 unsigned long long dsize
;
4982 unsigned long long sectors
;
4984 get_dev_size(fd
, NULL
, &dsize
);
4986 if (mpb_size
> 512) {
4987 /* -1 to account for anchor */
4988 sectors
= mpb_sectors(mpb
) - 1;
4990 /* write the extended mpb to the sectors preceeding the anchor */
4991 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0)
4994 if ((unsigned long long)write(fd
, buf
+ 512, 512 * sectors
)
4999 /* first block is stored on second to last sector of the disk */
5000 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0)
5003 if (write(fd
, buf
, 512) != 512)
5009 static void imsm_sync_metadata(struct supertype
*container
)
5011 struct intel_super
*super
= container
->sb
;
5013 dprintf("sync metadata: %d\n", super
->updates_pending
);
5014 if (!super
->updates_pending
)
5017 write_super_imsm(container
, 0);
5019 super
->updates_pending
= 0;
5022 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
5024 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
5025 int i
= get_imsm_disk_idx(dev
, idx
);
5028 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5032 if (dl
&& is_failed(&dl
->disk
))
5036 dprintf("%s: found %x:%x\n", __func__
, dl
->major
, dl
->minor
);
5041 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
5042 struct active_array
*a
, int activate_new
,
5043 struct mdinfo
*additional_test_list
)
5045 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
5046 int idx
= get_imsm_disk_idx(dev
, slot
);
5047 struct imsm_super
*mpb
= super
->anchor
;
5048 struct imsm_map
*map
;
5049 unsigned long long pos
;
5054 __u32 array_start
= 0;
5055 __u32 array_end
= 0;
5057 struct mdinfo
*test_list
;
5059 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5060 /* If in this array, skip */
5061 for (d
= a
->info
.devs
; d
; d
= d
->next
)
5062 if (d
->state_fd
>= 0 &&
5063 d
->disk
.major
== dl
->major
&&
5064 d
->disk
.minor
== dl
->minor
) {
5065 dprintf("%x:%x already in array\n",
5066 dl
->major
, dl
->minor
);
5071 test_list
= additional_test_list
;
5073 if (test_list
->disk
.major
== dl
->major
&&
5074 test_list
->disk
.minor
== dl
->minor
) {
5075 dprintf("%x:%x already in additional test list\n",
5076 dl
->major
, dl
->minor
);
5079 test_list
= test_list
->next
;
5084 /* skip in use or failed drives */
5085 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
5087 dprintf("%x:%x status (failed: %d index: %d)\n",
5088 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
5092 /* skip pure spares when we are looking for partially
5093 * assimilated drives
5095 if (dl
->index
== -1 && !activate_new
)
5098 /* Does this unused device have the requisite free space?
5099 * It needs to be able to cover all member volumes
5101 ex
= get_extents(super
, dl
);
5103 dprintf("cannot get extents\n");
5106 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5107 dev
= get_imsm_dev(super
, i
);
5108 map
= get_imsm_map(dev
, 0);
5110 /* check if this disk is already a member of
5113 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
5119 array_start
= __le32_to_cpu(map
->pba_of_lba0
);
5120 array_end
= array_start
+
5121 __le32_to_cpu(map
->blocks_per_member
) - 1;
5124 /* check that we can start at pba_of_lba0 with
5125 * blocks_per_member of space
5127 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
5131 pos
= ex
[j
].start
+ ex
[j
].size
;
5133 } while (ex
[j
-1].size
);
5140 if (i
< mpb
->num_raid_devs
) {
5141 dprintf("%x:%x does not have %u to %u available\n",
5142 dl
->major
, dl
->minor
, array_start
, array_end
);
5153 static int imsm_rebuild_allowed(struct supertype
*cont
, int dev_idx
, int failed
)
5155 struct imsm_dev
*dev2
;
5156 struct imsm_map
*map
;
5162 dev2
= get_imsm_dev(cont
->sb
, dev_idx
);
5164 state
= imsm_check_degraded(cont
->sb
, dev2
, failed
);
5165 if (state
== IMSM_T_STATE_FAILED
) {
5166 map
= get_imsm_map(dev2
, 0);
5169 for (slot
= 0; slot
< map
->num_members
; slot
++) {
5171 * Check if failed disks are deleted from intel
5172 * disk list or are marked to be deleted
5174 idx
= get_imsm_disk_idx(dev2
, slot
);
5175 idisk
= get_imsm_dl_disk(cont
->sb
, idx
);
5177 * Do not rebuild the array if failed disks
5178 * from failed sub-array are not removed from
5182 is_failed(&idisk
->disk
) &&
5183 (idisk
->action
!= DISK_REMOVE
))
5191 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
5192 struct metadata_update
**updates
)
5195 * Find a device with unused free space and use it to replace a
5196 * failed/vacant region in an array. We replace failed regions one a
5197 * array at a time. The result is that a new spare disk will be added
5198 * to the first failed array and after the monitor has finished
5199 * propagating failures the remainder will be consumed.
5201 * FIXME add a capability for mdmon to request spares from another
5205 struct intel_super
*super
= a
->container
->sb
;
5206 int inst
= a
->info
.container_member
;
5207 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
5208 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5209 int failed
= a
->info
.array
.raid_disks
;
5210 struct mdinfo
*rv
= NULL
;
5213 struct metadata_update
*mu
;
5215 struct imsm_update_activate_spare
*u
;
5220 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
5221 if ((d
->curr_state
& DS_FAULTY
) &&
5223 /* wait for Removal to happen */
5225 if (d
->state_fd
>= 0)
5229 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
5230 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
5231 if (imsm_check_degraded(super
, dev
, failed
) != IMSM_T_STATE_DEGRADED
)
5235 * If there are any failed disks check state of the other volume.
5236 * Block rebuild if the another one is failed until failed disks
5237 * are removed from container.
5240 dprintf("found failed disks in %s, check if there another"
5241 "failed sub-array.\n",
5243 /* check if states of the other volumes allow for rebuild */
5244 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
5246 allowed
= imsm_rebuild_allowed(a
->container
,
5254 /* For each slot, if it is not working, find a spare */
5255 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
5256 for (d
= a
->info
.devs
; d
; d
= d
->next
)
5257 if (d
->disk
.raid_disk
== i
)
5259 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
5260 if (d
&& (d
->state_fd
>= 0))
5264 * OK, this device needs recovery. Try to re-add the
5265 * previous occupant of this slot, if this fails see if
5266 * we can continue the assimilation of a spare that was
5267 * partially assimilated, finally try to activate a new
5270 dl
= imsm_readd(super
, i
, a
);
5272 dl
= imsm_add_spare(super
, i
, a
, 0, NULL
);
5274 dl
= imsm_add_spare(super
, i
, a
, 1, NULL
);
5278 /* found a usable disk with enough space */
5279 di
= malloc(sizeof(*di
));
5282 memset(di
, 0, sizeof(*di
));
5284 /* dl->index will be -1 in the case we are activating a
5285 * pristine spare. imsm_process_update() will create a
5286 * new index in this case. Once a disk is found to be
5287 * failed in all member arrays it is kicked from the
5290 di
->disk
.number
= dl
->index
;
5292 /* (ab)use di->devs to store a pointer to the device
5295 di
->devs
= (struct mdinfo
*) dl
;
5297 di
->disk
.raid_disk
= i
;
5298 di
->disk
.major
= dl
->major
;
5299 di
->disk
.minor
= dl
->minor
;
5301 di
->recovery_start
= 0;
5302 di
->data_offset
= __le32_to_cpu(map
->pba_of_lba0
);
5303 di
->component_size
= a
->info
.component_size
;
5304 di
->container_member
= inst
;
5305 super
->random
= random32();
5309 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
5310 i
, di
->data_offset
);
5316 /* No spares found */
5318 /* Now 'rv' has a list of devices to return.
5319 * Create a metadata_update record to update the
5320 * disk_ord_tbl for the array
5322 mu
= malloc(sizeof(*mu
));
5324 mu
->buf
= malloc(sizeof(struct imsm_update_activate_spare
) * num_spares
);
5325 if (mu
->buf
== NULL
) {
5332 struct mdinfo
*n
= rv
->next
;
5341 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
5342 mu
->next
= *updates
;
5343 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
5345 for (di
= rv
; di
; di
= di
->next
) {
5346 u
->type
= update_activate_spare
;
5347 u
->dl
= (struct dl
*) di
->devs
;
5349 u
->slot
= di
->disk
.raid_disk
;
5360 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
5362 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
5363 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5364 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, 0);
5365 struct disk_info
*inf
= get_disk_info(u
);
5366 struct imsm_disk
*disk
;
5370 for (i
= 0; i
< map
->num_members
; i
++) {
5371 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
));
5372 for (j
= 0; j
< new_map
->num_members
; j
++)
5373 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
5381 static struct dl
*get_disk_super(struct intel_super
*super
, int major
, int minor
)
5383 struct dl
*dl
= NULL
;
5384 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5385 if ((dl
->major
== major
) && (dl
->minor
== minor
))
5390 static int remove_disk_super(struct intel_super
*super
, int major
, int minor
)
5392 struct dl
*prev
= NULL
;
5396 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
5397 if ((dl
->major
== major
) && (dl
->minor
== minor
)) {
5400 prev
->next
= dl
->next
;
5402 super
->disks
= dl
->next
;
5404 __free_imsm_disk(dl
);
5405 dprintf("%s: removed %x:%x\n",
5406 __func__
, major
, minor
);
5414 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
);
5416 static int add_remove_disk_update(struct intel_super
*super
)
5418 int check_degraded
= 0;
5419 struct dl
*disk
= NULL
;
5420 /* add/remove some spares to/from the metadata/contrainer */
5421 while (super
->disk_mgmt_list
) {
5422 struct dl
*disk_cfg
;
5424 disk_cfg
= super
->disk_mgmt_list
;
5425 super
->disk_mgmt_list
= disk_cfg
->next
;
5426 disk_cfg
->next
= NULL
;
5428 if (disk_cfg
->action
== DISK_ADD
) {
5429 disk_cfg
->next
= super
->disks
;
5430 super
->disks
= disk_cfg
;
5432 dprintf("%s: added %x:%x\n",
5433 __func__
, disk_cfg
->major
,
5435 } else if (disk_cfg
->action
== DISK_REMOVE
) {
5436 dprintf("Disk remove action processed: %x.%x\n",
5437 disk_cfg
->major
, disk_cfg
->minor
);
5438 disk
= get_disk_super(super
,
5442 /* store action status */
5443 disk
->action
= DISK_REMOVE
;
5444 /* remove spare disks only */
5445 if (disk
->index
== -1) {
5446 remove_disk_super(super
,
5451 /* release allocate disk structure */
5452 __free_imsm_disk(disk_cfg
);
5455 return check_degraded
;
5458 static void imsm_process_update(struct supertype
*st
,
5459 struct metadata_update
*update
)
5462 * crack open the metadata_update envelope to find the update record
5463 * update can be one of:
5464 * update_activate_spare - a spare device has replaced a failed
5465 * device in an array, update the disk_ord_tbl. If this disk is
5466 * present in all member arrays then also clear the SPARE_DISK
5469 struct intel_super
*super
= st
->sb
;
5470 struct imsm_super
*mpb
;
5471 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
5473 /* update requires a larger buf but the allocation failed */
5474 if (super
->next_len
&& !super
->next_buf
) {
5475 super
->next_len
= 0;
5479 if (super
->next_buf
) {
5480 memcpy(super
->next_buf
, super
->buf
, super
->len
);
5482 super
->len
= super
->next_len
;
5483 super
->buf
= super
->next_buf
;
5485 super
->next_len
= 0;
5486 super
->next_buf
= NULL
;
5489 mpb
= super
->anchor
;
5492 case update_activate_spare
: {
5493 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
5494 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
5495 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5496 struct imsm_map
*migr_map
;
5497 struct active_array
*a
;
5498 struct imsm_disk
*disk
;
5503 int victim
= get_imsm_disk_idx(dev
, u
->slot
);
5506 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
5511 fprintf(stderr
, "error: imsm_activate_spare passed "
5512 "an unknown disk (index: %d)\n",
5517 super
->updates_pending
++;
5519 /* count failures (excluding rebuilds and the victim)
5520 * to determine map[0] state
5523 for (i
= 0; i
< map
->num_members
; i
++) {
5526 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
));
5527 if (!disk
|| is_failed(disk
))
5531 /* adding a pristine spare, assign a new index */
5532 if (dl
->index
< 0) {
5533 dl
->index
= super
->anchor
->num_disks
;
5534 super
->anchor
->num_disks
++;
5537 disk
->status
|= CONFIGURED_DISK
;
5538 disk
->status
&= ~SPARE_DISK
;
5541 to_state
= imsm_check_degraded(super
, dev
, failed
);
5542 map
->map_state
= IMSM_T_STATE_DEGRADED
;
5543 migrate(dev
, to_state
, MIGR_REBUILD
);
5544 migr_map
= get_imsm_map(dev
, 1);
5545 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
5546 set_imsm_ord_tbl_ent(migr_map
, u
->slot
, dl
->index
| IMSM_ORD_REBUILD
);
5548 /* update the family_num to mark a new container
5549 * generation, being careful to record the existing
5550 * family_num in orig_family_num to clean up after
5551 * earlier mdadm versions that neglected to set it.
5553 if (mpb
->orig_family_num
== 0)
5554 mpb
->orig_family_num
= mpb
->family_num
;
5555 mpb
->family_num
+= super
->random
;
5557 /* count arrays using the victim in the metadata */
5559 for (a
= st
->arrays
; a
; a
= a
->next
) {
5560 dev
= get_imsm_dev(super
, a
->info
.container_member
);
5561 map
= get_imsm_map(dev
, 0);
5563 if (get_imsm_disk_slot(map
, victim
) >= 0)
5567 /* delete the victim if it is no longer being
5573 /* We know that 'manager' isn't touching anything,
5574 * so it is safe to delete
5576 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
5577 if ((*dlp
)->index
== victim
)
5580 /* victim may be on the missing list */
5582 for (dlp
= &super
->missing
; *dlp
; dlp
= &(*dlp
)->next
)
5583 if ((*dlp
)->index
== victim
)
5585 imsm_delete(super
, dlp
, victim
);
5589 case update_create_array
: {
5590 /* someone wants to create a new array, we need to be aware of
5591 * a few races/collisions:
5592 * 1/ 'Create' called by two separate instances of mdadm
5593 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
5594 * devices that have since been assimilated via
5596 * In the event this update can not be carried out mdadm will
5597 * (FIX ME) notice that its update did not take hold.
5599 struct imsm_update_create_array
*u
= (void *) update
->buf
;
5600 struct intel_dev
*dv
;
5601 struct imsm_dev
*dev
;
5602 struct imsm_map
*map
, *new_map
;
5603 unsigned long long start
, end
;
5604 unsigned long long new_start
, new_end
;
5606 struct disk_info
*inf
;
5609 /* handle racing creates: first come first serve */
5610 if (u
->dev_idx
< mpb
->num_raid_devs
) {
5611 dprintf("%s: subarray %d already defined\n",
5612 __func__
, u
->dev_idx
);
5616 /* check update is next in sequence */
5617 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
5618 dprintf("%s: can not create array %d expected index %d\n",
5619 __func__
, u
->dev_idx
, mpb
->num_raid_devs
);
5623 new_map
= get_imsm_map(&u
->dev
, 0);
5624 new_start
= __le32_to_cpu(new_map
->pba_of_lba0
);
5625 new_end
= new_start
+ __le32_to_cpu(new_map
->blocks_per_member
);
5626 inf
= get_disk_info(u
);
5628 /* handle activate_spare versus create race:
5629 * check to make sure that overlapping arrays do not include
5632 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5633 dev
= get_imsm_dev(super
, i
);
5634 map
= get_imsm_map(dev
, 0);
5635 start
= __le32_to_cpu(map
->pba_of_lba0
);
5636 end
= start
+ __le32_to_cpu(map
->blocks_per_member
);
5637 if ((new_start
>= start
&& new_start
<= end
) ||
5638 (start
>= new_start
&& start
<= new_end
))
5643 if (disks_overlap(super
, i
, u
)) {
5644 dprintf("%s: arrays overlap\n", __func__
);
5649 /* check that prepare update was successful */
5650 if (!update
->space
) {
5651 dprintf("%s: prepare update failed\n", __func__
);
5655 /* check that all disks are still active before committing
5656 * changes. FIXME: could we instead handle this by creating a
5657 * degraded array? That's probably not what the user expects,
5658 * so better to drop this update on the floor.
5660 for (i
= 0; i
< new_map
->num_members
; i
++) {
5661 dl
= serial_to_dl(inf
[i
].serial
, super
);
5663 dprintf("%s: disk disappeared\n", __func__
);
5668 super
->updates_pending
++;
5670 /* convert spares to members and fixup ord_tbl */
5671 for (i
= 0; i
< new_map
->num_members
; i
++) {
5672 dl
= serial_to_dl(inf
[i
].serial
, super
);
5673 if (dl
->index
== -1) {
5674 dl
->index
= mpb
->num_disks
;
5676 dl
->disk
.status
|= CONFIGURED_DISK
;
5677 dl
->disk
.status
&= ~SPARE_DISK
;
5679 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
5684 update
->space
= NULL
;
5685 imsm_copy_dev(dev
, &u
->dev
);
5686 dv
->index
= u
->dev_idx
;
5687 dv
->next
= super
->devlist
;
5688 super
->devlist
= dv
;
5689 mpb
->num_raid_devs
++;
5691 imsm_update_version_info(super
);
5694 /* mdmon knows how to release update->space, but not
5695 * ((struct intel_dev *) update->space)->dev
5697 if (update
->space
) {
5703 case update_kill_array
: {
5704 struct imsm_update_kill_array
*u
= (void *) update
->buf
;
5705 int victim
= u
->dev_idx
;
5706 struct active_array
*a
;
5707 struct intel_dev
**dp
;
5708 struct imsm_dev
*dev
;
5710 /* sanity check that we are not affecting the uuid of
5711 * active arrays, or deleting an active array
5713 * FIXME when immutable ids are available, but note that
5714 * we'll also need to fixup the invalidated/active
5715 * subarray indexes in mdstat
5717 for (a
= st
->arrays
; a
; a
= a
->next
)
5718 if (a
->info
.container_member
>= victim
)
5720 /* by definition if mdmon is running at least one array
5721 * is active in the container, so checking
5722 * mpb->num_raid_devs is just extra paranoia
5724 dev
= get_imsm_dev(super
, victim
);
5725 if (a
|| !dev
|| mpb
->num_raid_devs
== 1) {
5726 dprintf("failed to delete subarray-%d\n", victim
);
5730 for (dp
= &super
->devlist
; *dp
;)
5731 if ((*dp
)->index
== (unsigned)super
->current_vol
) {
5734 if ((*dp
)->index
> (unsigned)victim
)
5738 mpb
->num_raid_devs
--;
5739 super
->updates_pending
++;
5742 case update_rename_array
: {
5743 struct imsm_update_rename_array
*u
= (void *) update
->buf
;
5744 char name
[MAX_RAID_SERIAL_LEN
+1];
5745 int target
= u
->dev_idx
;
5746 struct active_array
*a
;
5747 struct imsm_dev
*dev
;
5749 /* sanity check that we are not affecting the uuid of
5752 snprintf(name
, MAX_RAID_SERIAL_LEN
, "%s", (char *) u
->name
);
5753 name
[MAX_RAID_SERIAL_LEN
] = '\0';
5754 for (a
= st
->arrays
; a
; a
= a
->next
)
5755 if (a
->info
.container_member
== target
)
5757 dev
= get_imsm_dev(super
, u
->dev_idx
);
5758 if (a
|| !dev
|| !check_name(super
, name
, 1)) {
5759 dprintf("failed to rename subarray-%d\n", target
);
5763 snprintf((char *) dev
->volume
, MAX_RAID_SERIAL_LEN
, "%s", name
);
5764 super
->updates_pending
++;
5767 case update_add_remove_disk
: {
5768 /* we may be able to repair some arrays if disks are
5769 * being added, check teh status of add_remove_disk
5770 * if discs has been added.
5772 if (add_remove_disk_update(super
)) {
5773 struct active_array
*a
;
5775 super
->updates_pending
++;
5776 for (a
= st
->arrays
; a
; a
= a
->next
)
5777 a
->check_degraded
= 1;
5782 fprintf(stderr
, "error: unsuported process update type:"
5783 "(type: %d)\n", type
);
5787 static void imsm_prepare_update(struct supertype
*st
,
5788 struct metadata_update
*update
)
5791 * Allocate space to hold new disk entries, raid-device entries or a new
5792 * mpb if necessary. The manager synchronously waits for updates to
5793 * complete in the monitor, so new mpb buffers allocated here can be
5794 * integrated by the monitor thread without worrying about live pointers
5795 * in the manager thread.
5797 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
5798 struct intel_super
*super
= st
->sb
;
5799 struct imsm_super
*mpb
= super
->anchor
;
5804 case update_create_array
: {
5805 struct imsm_update_create_array
*u
= (void *) update
->buf
;
5806 struct intel_dev
*dv
;
5807 struct imsm_dev
*dev
= &u
->dev
;
5808 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5810 struct disk_info
*inf
;
5814 inf
= get_disk_info(u
);
5815 len
= sizeof_imsm_dev(dev
, 1);
5816 /* allocate a new super->devlist entry */
5817 dv
= malloc(sizeof(*dv
));
5819 dv
->dev
= malloc(len
);
5824 update
->space
= NULL
;
5828 /* count how many spares will be converted to members */
5829 for (i
= 0; i
< map
->num_members
; i
++) {
5830 dl
= serial_to_dl(inf
[i
].serial
, super
);
5832 /* hmm maybe it failed?, nothing we can do about
5837 if (count_memberships(dl
, super
) == 0)
5840 len
+= activate
* sizeof(struct imsm_disk
);
5847 /* check if we need a larger metadata buffer */
5848 if (super
->next_buf
)
5849 buf_len
= super
->next_len
;
5851 buf_len
= super
->len
;
5853 if (__le32_to_cpu(mpb
->mpb_size
) + len
> buf_len
) {
5854 /* ok we need a larger buf than what is currently allocated
5855 * if this allocation fails process_update will notice that
5856 * ->next_len is set and ->next_buf is NULL
5858 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + len
, 512);
5859 if (super
->next_buf
)
5860 free(super
->next_buf
);
5862 super
->next_len
= buf_len
;
5863 if (posix_memalign(&super
->next_buf
, 512, buf_len
) == 0)
5864 memset(super
->next_buf
, 0, buf_len
);
5866 super
->next_buf
= NULL
;
5870 /* must be called while manager is quiesced */
5871 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, unsigned index
)
5873 struct imsm_super
*mpb
= super
->anchor
;
5875 struct imsm_dev
*dev
;
5876 struct imsm_map
*map
;
5877 int i
, j
, num_members
;
5880 dprintf("%s: deleting device[%d] from imsm_super\n",
5883 /* shift all indexes down one */
5884 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
5885 if (iter
->index
> (int)index
)
5887 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
5888 if (iter
->index
> (int)index
)
5891 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5892 dev
= get_imsm_dev(super
, i
);
5893 map
= get_imsm_map(dev
, 0);
5894 num_members
= map
->num_members
;
5895 for (j
= 0; j
< num_members
; j
++) {
5896 /* update ord entries being careful not to propagate
5897 * ord-flags to the first map
5899 ord
= get_imsm_ord_tbl_ent(dev
, j
);
5901 if (ord_to_idx(ord
) <= index
)
5904 map
= get_imsm_map(dev
, 0);
5905 set_imsm_ord_tbl_ent(map
, j
, ord_to_idx(ord
- 1));
5906 map
= get_imsm_map(dev
, 1);
5908 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
5913 super
->updates_pending
++;
5915 struct dl
*dl
= *dlp
;
5917 *dlp
= (*dlp
)->next
;
5918 __free_imsm_disk(dl
);
5921 #endif /* MDASSEMBLE */
5923 static char disk_by_path
[] = "/dev/disk/by-path/";
5925 static const char *imsm_get_disk_controller_domain(const char *path
)
5927 struct sys_dev
*list
, *hba
= NULL
;
5928 char disk_path
[PATH_MAX
];
5932 list
= find_driver_devices("pci", "ahci");
5933 for (hba
= list
; hba
; hba
= hba
->next
)
5934 if (devpath_to_vendor(hba
->path
) == 0x8086)
5940 strncpy(disk_path
, disk_by_path
, PATH_MAX
- 1);
5941 strncat(disk_path
, path
, PATH_MAX
- strlen(disk_path
) - 1);
5942 if (stat(disk_path
, &st
) == 0) {
5943 dpath
= devt_to_devpath(st
.st_rdev
);
5945 ahci
= path_attached_to_hba(dpath
, hba
->path
);
5948 dprintf("path: %s(%s) hba: %s attached: %d\n",
5949 path
, dpath
, (hba
) ? hba
->path
: "NULL", ahci
);
5950 free_sys_dev(&list
);
5958 struct superswitch super_imsm
= {
5960 .examine_super
= examine_super_imsm
,
5961 .brief_examine_super
= brief_examine_super_imsm
,
5962 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
5963 .export_examine_super
= export_examine_super_imsm
,
5964 .detail_super
= detail_super_imsm
,
5965 .brief_detail_super
= brief_detail_super_imsm
,
5966 .write_init_super
= write_init_super_imsm
,
5967 .validate_geometry
= validate_geometry_imsm
,
5968 .add_to_super
= add_to_super_imsm
,
5969 .remove_from_super
= remove_from_super_imsm
,
5970 .detail_platform
= detail_platform_imsm
,
5971 .kill_subarray
= kill_subarray_imsm
,
5972 .update_subarray
= update_subarray_imsm
,
5973 .load_container
= load_container_imsm
,
5975 .match_home
= match_home_imsm
,
5976 .uuid_from_super
= uuid_from_super_imsm
,
5977 .getinfo_super
= getinfo_super_imsm
,
5978 .getinfo_super_disks
= getinfo_super_disks_imsm
,
5979 .update_super
= update_super_imsm
,
5981 .avail_size
= avail_size_imsm
,
5982 .min_acceptable_spare_size
= min_acceptable_spare_size_imsm
,
5984 .compare_super
= compare_super_imsm
,
5986 .load_super
= load_super_imsm
,
5987 .init_super
= init_super_imsm
,
5988 .store_super
= store_super_imsm
,
5989 .free_super
= free_super_imsm
,
5990 .match_metadata_desc
= match_metadata_desc_imsm
,
5991 .container_content
= container_content_imsm
,
5992 .default_geometry
= default_geometry_imsm
,
5993 .get_disk_controller_domain
= imsm_get_disk_controller_domain
,
6000 .open_new
= imsm_open_new
,
6001 .set_array_state
= imsm_set_array_state
,
6002 .set_disk
= imsm_set_disk
,
6003 .sync_metadata
= imsm_sync_metadata
,
6004 .activate_spare
= imsm_activate_spare
,
6005 .process_update
= imsm_process_update
,
6006 .prepare_update
= imsm_prepare_update
,
6007 #endif /* MDASSEMBLE */