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
;
236 /* internal representation of IMSM metadata */
239 void *buf
; /* O_DIRECT buffer for reading/writing metadata */
240 struct imsm_super
*anchor
; /* immovable parameters */
242 size_t len
; /* size of the 'buf' allocation */
243 void *next_buf
; /* for realloc'ing buf from the manager */
245 int updates_pending
; /* count of pending updates for mdmon */
246 int creating_imsm
; /* flag to indicate container creation */
247 int current_vol
; /* index of raid device undergoing creation */
248 __u32 create_offset
; /* common start for 'current_vol' */
249 __u32 random
; /* random data for seeding new family numbers */
250 struct intel_dev
*devlist
;
254 __u8 serial
[MAX_RAID_SERIAL_LEN
];
257 struct imsm_disk disk
;
260 struct extent
*e
; /* for determining freespace @ create */
261 int raiddisk
; /* slot to fill in autolayout */
263 struct dl
*add
; /* list of disks to add while mdmon active */
264 struct dl
*missing
; /* disks removed while we weren't looking */
265 struct bbm_log
*bbm_log
;
266 const char *hba
; /* device path of the raid controller for this metadata */
267 const struct imsm_orom
*orom
; /* platform firmware support */
268 struct intel_super
*next
; /* (temp) list for disambiguating family_num */
272 struct imsm_disk disk
;
273 #define IMSM_UNKNOWN_OWNER (-1)
275 struct intel_disk
*next
;
279 unsigned long long start
, size
;
282 /* definition of messages passed to imsm_process_update */
283 enum imsm_update_type
{
284 update_activate_spare
,
289 struct imsm_update_activate_spare
{
290 enum imsm_update_type type
;
294 struct imsm_update_activate_spare
*next
;
298 __u8 serial
[MAX_RAID_SERIAL_LEN
];
301 struct imsm_update_create_array
{
302 enum imsm_update_type type
;
307 struct imsm_update_add_disk
{
308 enum imsm_update_type type
;
311 static struct supertype
*match_metadata_desc_imsm(char *arg
)
313 struct supertype
*st
;
315 if (strcmp(arg
, "imsm") != 0 &&
316 strcmp(arg
, "default") != 0
320 st
= malloc(sizeof(*st
));
323 memset(st
, 0, sizeof(*st
));
324 st
->ss
= &super_imsm
;
325 st
->max_devs
= IMSM_MAX_DEVICES
;
326 st
->minor_version
= 0;
332 static __u8
*get_imsm_version(struct imsm_super
*mpb
)
334 return &mpb
->sig
[MPB_SIG_LEN
];
338 /* retrieve a disk directly from the anchor when the anchor is known to be
339 * up-to-date, currently only at load time
341 static struct imsm_disk
*__get_imsm_disk(struct imsm_super
*mpb
, __u8 index
)
343 if (index
>= mpb
->num_disks
)
345 return &mpb
->disk
[index
];
349 /* retrieve a disk from the parsed metadata */
350 static struct imsm_disk
*get_imsm_disk(struct intel_super
*super
, __u8 index
)
354 for (d
= super
->disks
; d
; d
= d
->next
)
355 if (d
->index
== index
)
362 /* generate a checksum directly from the anchor when the anchor is known to be
363 * up-to-date, currently only at load or write_super after coalescing
365 static __u32
__gen_imsm_checksum(struct imsm_super
*mpb
)
367 __u32 end
= mpb
->mpb_size
/ sizeof(end
);
368 __u32
*p
= (__u32
*) mpb
;
372 sum
+= __le32_to_cpu(*p
);
376 return sum
- __le32_to_cpu(mpb
->check_sum
);
379 static size_t sizeof_imsm_map(struct imsm_map
*map
)
381 return sizeof(struct imsm_map
) + sizeof(__u32
) * (map
->num_members
- 1);
384 struct imsm_map
*get_imsm_map(struct imsm_dev
*dev
, int second_map
)
386 struct imsm_map
*map
= &dev
->vol
.map
[0];
388 if (second_map
&& !dev
->vol
.migr_state
)
390 else if (second_map
) {
393 return ptr
+ sizeof_imsm_map(map
);
399 /* return the size of the device.
400 * migr_state increases the returned size if map[0] were to be duplicated
402 static size_t sizeof_imsm_dev(struct imsm_dev
*dev
, int migr_state
)
404 size_t size
= sizeof(*dev
) - sizeof(struct imsm_map
) +
405 sizeof_imsm_map(get_imsm_map(dev
, 0));
407 /* migrating means an additional map */
408 if (dev
->vol
.migr_state
)
409 size
+= sizeof_imsm_map(get_imsm_map(dev
, 1));
411 size
+= sizeof_imsm_map(get_imsm_map(dev
, 0));
417 /* retrieve disk serial number list from a metadata update */
418 static struct disk_info
*get_disk_info(struct imsm_update_create_array
*update
)
421 struct disk_info
*inf
;
423 inf
= u
+ sizeof(*update
) - sizeof(struct imsm_dev
) +
424 sizeof_imsm_dev(&update
->dev
, 0);
430 static struct imsm_dev
*__get_imsm_dev(struct imsm_super
*mpb
, __u8 index
)
436 if (index
>= mpb
->num_raid_devs
)
439 /* devices start after all disks */
440 offset
= ((void *) &mpb
->disk
[mpb
->num_disks
]) - _mpb
;
442 for (i
= 0; i
<= index
; i
++)
444 return _mpb
+ offset
;
446 offset
+= sizeof_imsm_dev(_mpb
+ offset
, 0);
451 static struct imsm_dev
*get_imsm_dev(struct intel_super
*super
, __u8 index
)
453 struct intel_dev
*dv
;
455 if (index
>= super
->anchor
->num_raid_devs
)
457 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
458 if (dv
->index
== index
)
463 static __u32
get_imsm_ord_tbl_ent(struct imsm_dev
*dev
, int slot
)
465 struct imsm_map
*map
;
467 if (dev
->vol
.migr_state
)
468 map
= get_imsm_map(dev
, 1);
470 map
= get_imsm_map(dev
, 0);
472 /* top byte identifies disk under rebuild */
473 return __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
476 #define ord_to_idx(ord) (((ord) << 8) >> 8)
477 static __u32
get_imsm_disk_idx(struct imsm_dev
*dev
, int slot
)
479 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
);
481 return ord_to_idx(ord
);
484 static void set_imsm_ord_tbl_ent(struct imsm_map
*map
, int slot
, __u32 ord
)
486 map
->disk_ord_tbl
[slot
] = __cpu_to_le32(ord
);
489 static int get_imsm_disk_slot(struct imsm_map
*map
, int idx
)
494 for (slot
= 0; slot
< map
->num_members
; slot
++) {
495 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
496 if (ord_to_idx(ord
) == idx
)
503 static int get_imsm_raid_level(struct imsm_map
*map
)
505 if (map
->raid_level
== 1) {
506 if (map
->num_members
== 2)
512 return map
->raid_level
;
515 static int cmp_extent(const void *av
, const void *bv
)
517 const struct extent
*a
= av
;
518 const struct extent
*b
= bv
;
519 if (a
->start
< b
->start
)
521 if (a
->start
> b
->start
)
526 static int count_memberships(struct dl
*dl
, struct intel_super
*super
)
531 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
532 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
533 struct imsm_map
*map
= get_imsm_map(dev
, 0);
535 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
542 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
)
544 /* find a list of used extents on the given physical device */
545 struct extent
*rv
, *e
;
547 int memberships
= count_memberships(dl
, super
);
548 __u32 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
550 rv
= malloc(sizeof(struct extent
) * (memberships
+ 1));
555 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
556 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
557 struct imsm_map
*map
= get_imsm_map(dev
, 0);
559 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
560 e
->start
= __le32_to_cpu(map
->pba_of_lba0
);
561 e
->size
= __le32_to_cpu(map
->blocks_per_member
);
565 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
567 /* determine the start of the metadata
568 * when no raid devices are defined use the default
569 * ...otherwise allow the metadata to truncate the value
570 * as is the case with older versions of imsm
573 struct extent
*last
= &rv
[memberships
- 1];
576 remainder
= __le32_to_cpu(dl
->disk
.total_blocks
) -
577 (last
->start
+ last
->size
);
578 /* round down to 1k block to satisfy precision of the kernel
582 /* make sure remainder is still sane */
583 if (remainder
< ROUND_UP(super
->len
, 512) >> 9)
584 remainder
= ROUND_UP(super
->len
, 512) >> 9;
585 if (reservation
> remainder
)
586 reservation
= remainder
;
588 e
->start
= __le32_to_cpu(dl
->disk
.total_blocks
) - reservation
;
593 /* try to determine how much space is reserved for metadata from
594 * the last get_extents() entry, otherwise fallback to the
597 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
603 /* for spares just return a minimal reservation which will grow
604 * once the spare is picked up by an array
607 return MPB_SECTOR_CNT
;
609 e
= get_extents(super
, dl
);
611 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
613 /* scroll to last entry */
614 for (i
= 0; e
[i
].size
; i
++)
617 rv
= __le32_to_cpu(dl
->disk
.total_blocks
) - e
[i
].start
;
624 static int is_spare(struct imsm_disk
*disk
)
626 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
629 static int is_configured(struct imsm_disk
*disk
)
631 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
634 static int is_failed(struct imsm_disk
*disk
)
636 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
640 static __u64
blocks_per_migr_unit(struct imsm_dev
*dev
);
642 static void print_imsm_dev(struct imsm_dev
*dev
, char *uuid
, int disk_idx
)
646 struct imsm_map
*map
= get_imsm_map(dev
, 0);
650 printf("[%.16s]:\n", dev
->volume
);
651 printf(" UUID : %s\n", uuid
);
652 printf(" RAID Level : %d\n", get_imsm_raid_level(map
));
653 printf(" Members : %d\n", map
->num_members
);
654 slot
= get_imsm_disk_slot(map
, disk_idx
);
656 ord
= get_imsm_ord_tbl_ent(dev
, slot
);
657 printf(" This Slot : %d%s\n", slot
,
658 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
660 printf(" This Slot : ?\n");
661 sz
= __le32_to_cpu(dev
->size_high
);
663 sz
+= __le32_to_cpu(dev
->size_low
);
664 printf(" Array Size : %llu%s\n", (unsigned long long)sz
,
665 human_size(sz
* 512));
666 sz
= __le32_to_cpu(map
->blocks_per_member
);
667 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz
,
668 human_size(sz
* 512));
669 printf(" Sector Offset : %u\n",
670 __le32_to_cpu(map
->pba_of_lba0
));
671 printf(" Num Stripes : %u\n",
672 __le32_to_cpu(map
->num_data_stripes
));
673 printf(" Chunk Size : %u KiB\n",
674 __le16_to_cpu(map
->blocks_per_strip
) / 2);
675 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
676 printf(" Migrate State : ");
677 if (dev
->vol
.migr_state
) {
678 if (migr_type(dev
) == MIGR_INIT
)
679 printf("initialize\n");
680 else if (migr_type(dev
) == MIGR_REBUILD
)
682 else if (migr_type(dev
) == MIGR_VERIFY
)
684 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
685 printf("general migration\n");
686 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
687 printf("state change\n");
688 else if (migr_type(dev
) == MIGR_REPAIR
)
691 printf("<unknown:%d>\n", migr_type(dev
));
694 printf(" Map State : %s", map_state_str
[map
->map_state
]);
695 if (dev
->vol
.migr_state
) {
696 struct imsm_map
*map
= get_imsm_map(dev
, 1);
698 printf(" <-- %s", map_state_str
[map
->map_state
]);
699 printf("\n Checkpoint : %u (%llu)",
700 __le32_to_cpu(dev
->vol
.curr_migr_unit
),
701 blocks_per_migr_unit(dev
));
704 printf(" Dirty State : %s\n", dev
->vol
.dirty
? "dirty" : "clean");
707 static void print_imsm_disk(struct imsm_super
*mpb
, int index
, __u32 reserved
)
709 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, index
);
710 char str
[MAX_RAID_SERIAL_LEN
+ 1];
713 if (index
< 0 || !disk
)
717 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
718 printf(" Disk%02d Serial : %s\n", index
, str
);
719 printf(" State :%s%s%s\n", is_spare(disk
) ? " spare" : "",
720 is_configured(disk
) ? " active" : "",
721 is_failed(disk
) ? " failed" : "");
722 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
723 sz
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
724 printf(" Usable Size : %llu%s\n", (unsigned long long)sz
,
725 human_size(sz
* 512));
728 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
);
730 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
732 struct intel_super
*super
= st
->sb
;
733 struct imsm_super
*mpb
= super
->anchor
;
734 char str
[MAX_SIGNATURE_LENGTH
];
739 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
742 snprintf(str
, MPB_SIG_LEN
, "%s", mpb
->sig
);
743 printf(" Magic : %s\n", str
);
744 snprintf(str
, strlen(MPB_VERSION_RAID0
), "%s", get_imsm_version(mpb
));
745 printf(" Version : %s\n", get_imsm_version(mpb
));
746 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
747 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
748 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
749 getinfo_super_imsm(st
, &info
);
750 fname_from_uuid(st
, &info
, nbuf
, ':');
751 printf(" UUID : %s\n", nbuf
+ 5);
752 sum
= __le32_to_cpu(mpb
->check_sum
);
753 printf(" Checksum : %08x %s\n", sum
,
754 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
755 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
));
756 printf(" Disks : %d\n", mpb
->num_disks
);
757 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
758 print_imsm_disk(mpb
, super
->disks
->index
, reserved
);
759 if (super
->bbm_log
) {
760 struct bbm_log
*log
= super
->bbm_log
;
763 printf("Bad Block Management Log:\n");
764 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
765 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
766 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
767 printf(" Spare Blocks : %d\n", __le32_to_cpu(log
->reserved_spare_block_count
));
768 printf(" First Spare : %llx\n",
769 (unsigned long long) __le64_to_cpu(log
->first_spare_lba
));
771 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
773 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
775 super
->current_vol
= i
;
776 getinfo_super_imsm(st
, &info
);
777 fname_from_uuid(st
, &info
, nbuf
, ':');
778 print_imsm_dev(dev
, nbuf
+ 5, super
->disks
->index
);
780 for (i
= 0; i
< mpb
->num_disks
; i
++) {
781 if (i
== super
->disks
->index
)
783 print_imsm_disk(mpb
, i
, reserved
);
787 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
789 /* We just write a generic IMSM ARRAY entry */
792 struct intel_super
*super
= st
->sb
;
794 if (!super
->anchor
->num_raid_devs
) {
795 printf("ARRAY metadata=imsm\n");
799 getinfo_super_imsm(st
, &info
);
800 fname_from_uuid(st
, &info
, nbuf
, ':');
801 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
804 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
806 /* We just write a generic IMSM ARRAY entry */
810 struct intel_super
*super
= st
->sb
;
813 if (!super
->anchor
->num_raid_devs
)
816 getinfo_super_imsm(st
, &info
);
817 fname_from_uuid(st
, &info
, nbuf
, ':');
818 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
819 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
821 super
->current_vol
= i
;
822 getinfo_super_imsm(st
, &info
);
823 fname_from_uuid(st
, &info
, nbuf1
, ':');
824 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
825 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
829 static void export_examine_super_imsm(struct supertype
*st
)
831 struct intel_super
*super
= st
->sb
;
832 struct imsm_super
*mpb
= super
->anchor
;
836 getinfo_super_imsm(st
, &info
);
837 fname_from_uuid(st
, &info
, nbuf
, ':');
838 printf("MD_METADATA=imsm\n");
839 printf("MD_LEVEL=container\n");
840 printf("MD_UUID=%s\n", nbuf
+5);
841 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
844 static void detail_super_imsm(struct supertype
*st
, char *homehost
)
849 getinfo_super_imsm(st
, &info
);
850 fname_from_uuid(st
, &info
, nbuf
, ':');
851 printf("\n UUID : %s\n", nbuf
+ 5);
854 static void brief_detail_super_imsm(struct supertype
*st
)
858 getinfo_super_imsm(st
, &info
);
859 fname_from_uuid(st
, &info
, nbuf
, ':');
860 printf(" UUID=%s", nbuf
+ 5);
863 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
);
864 static void fd2devname(int fd
, char *name
);
866 static int imsm_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
868 /* dump an unsorted list of devices attached to ahci, as well as
869 * non-connected ports
871 int hba_len
= strlen(hba_path
) + 1;
876 unsigned long port_mask
= (1 << port_count
) - 1;
878 if (port_count
> sizeof(port_mask
) * 8) {
880 fprintf(stderr
, Name
": port_count %d out of range\n", port_count
);
884 /* scroll through /sys/dev/block looking for devices attached to
887 dir
= opendir("/sys/dev/block");
888 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
899 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
901 path
= devt_to_devpath(makedev(major
, minor
));
904 if (!path_attached_to_hba(path
, hba_path
)) {
910 /* retrieve the scsi device type */
911 if (asprintf(&device
, "/sys/dev/block/%d:%d/device/xxxxxxx", major
, minor
) < 0) {
913 fprintf(stderr
, Name
": failed to allocate 'device'\n");
917 sprintf(device
, "/sys/dev/block/%d:%d/device/type", major
, minor
);
918 if (load_sys(device
, buf
) != 0) {
920 fprintf(stderr
, Name
": failed to read device type for %s\n",
926 type
= strtoul(buf
, NULL
, 10);
928 /* if it's not a disk print the vendor and model */
929 if (!(type
== 0 || type
== 7 || type
== 14)) {
932 sprintf(device
, "/sys/dev/block/%d:%d/device/vendor", major
, minor
);
933 if (load_sys(device
, buf
) == 0) {
934 strncpy(vendor
, buf
, sizeof(vendor
));
935 vendor
[sizeof(vendor
) - 1] = '\0';
936 c
= (char *) &vendor
[sizeof(vendor
) - 1];
937 while (isspace(*c
) || *c
== '\0')
941 sprintf(device
, "/sys/dev/block/%d:%d/device/model", major
, minor
);
942 if (load_sys(device
, buf
) == 0) {
943 strncpy(model
, buf
, sizeof(model
));
944 model
[sizeof(model
) - 1] = '\0';
945 c
= (char *) &model
[sizeof(model
) - 1];
946 while (isspace(*c
) || *c
== '\0')
950 if (vendor
[0] && model
[0])
951 sprintf(buf
, "%.64s %.64s", vendor
, model
);
953 switch (type
) { /* numbers from hald/linux/device.c */
954 case 1: sprintf(buf
, "tape"); break;
955 case 2: sprintf(buf
, "printer"); break;
956 case 3: sprintf(buf
, "processor"); break;
958 case 5: sprintf(buf
, "cdrom"); break;
959 case 6: sprintf(buf
, "scanner"); break;
960 case 8: sprintf(buf
, "media_changer"); break;
961 case 9: sprintf(buf
, "comm"); break;
962 case 12: sprintf(buf
, "raid"); break;
963 default: sprintf(buf
, "unknown");
969 /* chop device path to 'host%d' and calculate the port number */
970 c
= strchr(&path
[hba_len
], '/');
973 fprintf(stderr
, Name
": %s - invalid path name\n", path
+ hba_len
);
978 if (sscanf(&path
[hba_len
], "host%d", &port
) == 1)
982 *c
= '/'; /* repair the full string */
983 fprintf(stderr
, Name
": failed to determine port number for %s\n",
990 /* mark this port as used */
991 port_mask
&= ~(1 << port
);
993 /* print out the device information */
995 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
999 fd
= dev_open(ent
->d_name
, O_RDONLY
);
1001 printf(" Port%d : - disk info unavailable -\n", port
);
1003 fd2devname(fd
, buf
);
1004 printf(" Port%d : %s", port
, buf
);
1005 if (imsm_read_serial(fd
, NULL
, (__u8
*) buf
) == 0)
1006 printf(" (%s)\n", buf
);
1021 for (i
= 0; i
< port_count
; i
++)
1022 if (port_mask
& (1 << i
))
1023 printf(" Port%d : - no device attached -\n", i
);
1029 static int detail_platform_imsm(int verbose
, int enumerate_only
)
1031 /* There are two components to imsm platform support, the ahci SATA
1032 * controller and the option-rom. To find the SATA controller we
1033 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
1034 * controller with the Intel vendor id is present. This approach
1035 * allows mdadm to leverage the kernel's ahci detection logic, with the
1036 * caveat that if ahci.ko is not loaded mdadm will not be able to
1037 * detect platform raid capabilities. The option-rom resides in a
1038 * platform "Adapter ROM". We scan for its signature to retrieve the
1039 * platform capabilities. If raid support is disabled in the BIOS the
1040 * option-rom capability structure will not be available.
1042 const struct imsm_orom
*orom
;
1043 struct sys_dev
*list
, *hba
;
1046 const char *hba_path
;
1050 if (enumerate_only
) {
1051 if (check_env("IMSM_NO_PLATFORM") || find_imsm_orom())
1056 list
= find_driver_devices("pci", "ahci");
1057 for (hba
= list
; hba
; hba
= hba
->next
)
1058 if (devpath_to_vendor(hba
->path
) == 0x8086)
1063 fprintf(stderr
, Name
": unable to find active ahci controller\n");
1064 free_sys_dev(&list
);
1067 fprintf(stderr
, Name
": found Intel SATA AHCI Controller\n");
1068 hba_path
= hba
->path
;
1070 free_sys_dev(&list
);
1072 orom
= find_imsm_orom();
1075 fprintf(stderr
, Name
": imsm option-rom not found\n");
1079 printf(" Platform : Intel(R) Matrix Storage Manager\n");
1080 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
1081 orom
->hotfix_ver
, orom
->build
);
1082 printf(" RAID Levels :%s%s%s%s%s\n",
1083 imsm_orom_has_raid0(orom
) ? " raid0" : "",
1084 imsm_orom_has_raid1(orom
) ? " raid1" : "",
1085 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
1086 imsm_orom_has_raid10(orom
) ? " raid10" : "",
1087 imsm_orom_has_raid5(orom
) ? " raid5" : "");
1088 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1089 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
1090 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
1091 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
1092 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
1093 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
1094 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
1095 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
1096 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
1097 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
1098 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
1099 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
1100 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
1101 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
1102 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
1103 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
1104 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
1105 printf(" Max Disks : %d\n", orom
->tds
);
1106 printf(" Max Volumes : %d\n", orom
->vpa
);
1107 printf(" I/O Controller : %s\n", hba_path
);
1109 /* find the smallest scsi host number to determine a port number base */
1110 dir
= opendir(hba_path
);
1111 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
1114 if (sscanf(ent
->d_name
, "host%d", &host
) != 1)
1116 if (port_count
== 0)
1118 else if (host
< host_base
)
1121 if (host
+ 1 > port_count
+ host_base
)
1122 port_count
= host
+ 1 - host_base
;
1128 if (!port_count
|| imsm_enumerate_ports(hba_path
, port_count
,
1129 host_base
, verbose
) != 0) {
1131 fprintf(stderr
, Name
": failed to enumerate ports\n");
1139 static int match_home_imsm(struct supertype
*st
, char *homehost
)
1141 /* the imsm metadata format does not specify any host
1142 * identification information. We return -1 since we can never
1143 * confirm nor deny whether a given array is "meant" for this
1144 * host. We rely on compare_super and the 'family_num' fields to
1145 * exclude member disks that do not belong, and we rely on
1146 * mdadm.conf to specify the arrays that should be assembled.
1147 * Auto-assembly may still pick up "foreign" arrays.
1153 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
1155 /* The uuid returned here is used for:
1156 * uuid to put into bitmap file (Create, Grow)
1157 * uuid for backup header when saving critical section (Grow)
1158 * comparing uuids when re-adding a device into an array
1159 * In these cases the uuid required is that of the data-array,
1160 * not the device-set.
1161 * uuid to recognise same set when adding a missing device back
1162 * to an array. This is a uuid for the device-set.
1164 * For each of these we can make do with a truncated
1165 * or hashed uuid rather than the original, as long as
1167 * In each case the uuid required is that of the data-array,
1168 * not the device-set.
1170 /* imsm does not track uuid's so we synthesis one using sha1 on
1171 * - The signature (Which is constant for all imsm array, but no matter)
1172 * - the orig_family_num of the container
1173 * - the index number of the volume
1174 * - the 'serial' number of the volume.
1175 * Hopefully these are all constant.
1177 struct intel_super
*super
= st
->sb
;
1180 struct sha1_ctx ctx
;
1181 struct imsm_dev
*dev
= NULL
;
1184 /* some mdadm versions failed to set ->orig_family_num, in which
1185 * case fall back to ->family_num. orig_family_num will be
1186 * fixed up with the first metadata update.
1188 family_num
= super
->anchor
->orig_family_num
;
1189 if (family_num
== 0)
1190 family_num
= super
->anchor
->family_num
;
1191 sha1_init_ctx(&ctx
);
1192 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
1193 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
1194 if (super
->current_vol
>= 0)
1195 dev
= get_imsm_dev(super
, super
->current_vol
);
1197 __u32 vol
= super
->current_vol
;
1198 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
1199 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
1201 sha1_finish_ctx(&ctx
, buf
);
1202 memcpy(uuid
, buf
, 4*4);
1207 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
1209 __u8
*v
= get_imsm_version(mpb
);
1210 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
1211 char major
[] = { 0, 0, 0 };
1212 char minor
[] = { 0 ,0, 0 };
1213 char patch
[] = { 0, 0, 0 };
1214 char *ver_parse
[] = { major
, minor
, patch
};
1218 while (*v
!= '\0' && v
< end
) {
1219 if (*v
!= '.' && j
< 2)
1220 ver_parse
[i
][j
++] = *v
;
1228 *m
= strtol(minor
, NULL
, 0);
1229 *p
= strtol(patch
, NULL
, 0);
1233 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
1235 /* migr_strip_size when repairing or initializing parity */
1236 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1237 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1239 switch (get_imsm_raid_level(map
)) {
1244 return 128*1024 >> 9;
1248 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
1250 /* migr_strip_size when rebuilding a degraded disk, no idea why
1251 * this is different than migr_strip_size_resync(), but it's good
1254 struct imsm_map
*map
= get_imsm_map(dev
, 1);
1255 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1257 switch (get_imsm_raid_level(map
)) {
1260 if (map
->num_members
% map
->num_domains
== 0)
1261 return 128*1024 >> 9;
1265 return max((__u32
) 64*1024 >> 9, chunk
);
1267 return 128*1024 >> 9;
1271 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
1273 struct imsm_map
*lo
= get_imsm_map(dev
, 0);
1274 struct imsm_map
*hi
= get_imsm_map(dev
, 1);
1275 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
1276 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
1278 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
1281 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
1283 struct imsm_map
*lo
= get_imsm_map(dev
, 0);
1284 int level
= get_imsm_raid_level(lo
);
1286 if (level
== 1 || level
== 10) {
1287 struct imsm_map
*hi
= get_imsm_map(dev
, 1);
1289 return hi
->num_domains
;
1291 return num_stripes_per_unit_resync(dev
);
1294 static __u8
imsm_num_data_members(struct imsm_dev
*dev
)
1296 /* named 'imsm_' because raid0, raid1 and raid10
1297 * counter-intuitively have the same number of data disks
1299 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1301 switch (get_imsm_raid_level(map
)) {
1305 return map
->num_members
;
1307 return map
->num_members
- 1;
1309 dprintf("%s: unsupported raid level\n", __func__
);
1314 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
1316 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1317 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1319 switch(get_imsm_raid_level(map
)) {
1322 return chunk
* map
->num_domains
;
1324 return chunk
* map
->num_members
;
1330 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
1332 struct imsm_map
*map
= get_imsm_map(dev
, 1);
1333 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1334 __u32 strip
= block
/ chunk
;
1336 switch (get_imsm_raid_level(map
)) {
1339 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
1340 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
1342 return vol_stripe
* chunk
+ block
% chunk
;
1344 __u32 stripe
= strip
/ (map
->num_members
- 1);
1346 return stripe
* chunk
+ block
% chunk
;
1353 static __u64
blocks_per_migr_unit(struct imsm_dev
*dev
)
1355 /* calculate the conversion factor between per member 'blocks'
1356 * (md/{resync,rebuild}_start) and imsm migration units, return
1357 * 0 for the 'not migrating' and 'unsupported migration' cases
1359 if (!dev
->vol
.migr_state
)
1362 switch (migr_type(dev
)) {
1366 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1367 __u32 stripes_per_unit
;
1368 __u32 blocks_per_unit
;
1377 /* yes, this is really the translation of migr_units to
1378 * per-member blocks in the 'resync' case
1380 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
1381 migr_chunk
= migr_strip_blocks_resync(dev
);
1382 disks
= imsm_num_data_members(dev
);
1383 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
1384 stripe
= __le32_to_cpu(map
->blocks_per_strip
) * disks
;
1385 segment
= blocks_per_unit
/ stripe
;
1386 block_rel
= blocks_per_unit
- segment
* stripe
;
1387 parity_depth
= parity_segment_depth(dev
);
1388 block_map
= map_migr_block(dev
, block_rel
);
1389 return block_map
+ parity_depth
* segment
;
1391 case MIGR_REBUILD
: {
1392 __u32 stripes_per_unit
;
1395 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
1396 migr_chunk
= migr_strip_blocks_rebuild(dev
);
1397 return migr_chunk
* stripes_per_unit
;
1400 case MIGR_STATE_CHANGE
:
1406 static int imsm_level_to_layout(int level
)
1414 return ALGORITHM_LEFT_ASYMMETRIC
;
1421 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
)
1423 struct intel_super
*super
= st
->sb
;
1424 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
1425 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1429 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
1430 if (dl
->raiddisk
== info
->disk
.raid_disk
)
1432 info
->container_member
= super
->current_vol
;
1433 info
->array
.raid_disks
= map
->num_members
;
1434 info
->array
.level
= get_imsm_raid_level(map
);
1435 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
1436 info
->array
.md_minor
= -1;
1437 info
->array
.ctime
= 0;
1438 info
->array
.utime
= 0;
1439 info
->array
.chunk_size
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
1440 info
->array
.state
= !dev
->vol
.dirty
;
1441 info
->custom_array_size
= __le32_to_cpu(dev
->size_high
);
1442 info
->custom_array_size
<<= 32;
1443 info
->custom_array_size
|= __le32_to_cpu(dev
->size_low
);
1445 info
->disk
.major
= 0;
1446 info
->disk
.minor
= 0;
1448 info
->disk
.major
= dl
->major
;
1449 info
->disk
.minor
= dl
->minor
;
1452 info
->data_offset
= __le32_to_cpu(map
->pba_of_lba0
);
1453 info
->component_size
= __le32_to_cpu(map
->blocks_per_member
);
1454 memset(info
->uuid
, 0, sizeof(info
->uuid
));
1456 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
|| dev
->vol
.dirty
) {
1457 info
->resync_start
= 0;
1458 } else if (dev
->vol
.migr_state
) {
1459 switch (migr_type(dev
)) {
1462 __u64 blocks_per_unit
= blocks_per_migr_unit(dev
);
1463 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
1465 info
->resync_start
= blocks_per_unit
* units
;
1469 /* we could emulate the checkpointing of
1470 * 'sync_action=check' migrations, but for now
1471 * we just immediately complete them
1474 /* this is handled by container_content_imsm() */
1476 case MIGR_STATE_CHANGE
:
1477 /* FIXME handle other migrations */
1479 /* we are not dirty, so... */
1480 info
->resync_start
= MaxSector
;
1483 info
->resync_start
= MaxSector
;
1485 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
1486 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
1488 info
->array
.major_version
= -1;
1489 info
->array
.minor_version
= -2;
1490 devname
= devnum2devname(st
->container_dev
);
1491 *info
->text_version
= '\0';
1493 sprintf(info
->text_version
, "/%s/%d", devname
, info
->container_member
);
1495 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
1496 uuid_from_super_imsm(st
, info
->uuid
);
1499 /* check the config file to see if we can return a real uuid for this spare */
1500 static void fixup_container_spare_uuid(struct mdinfo
*inf
)
1502 struct mddev_ident_s
*array_list
;
1504 if (inf
->array
.level
!= LEVEL_CONTAINER
||
1505 memcmp(inf
->uuid
, uuid_match_any
, sizeof(int[4])) != 0)
1508 array_list
= conf_get_ident(NULL
);
1510 for (; array_list
; array_list
= array_list
->next
) {
1511 if (array_list
->uuid_set
) {
1512 struct supertype
*_sst
; /* spare supertype */
1513 struct supertype
*_cst
; /* container supertype */
1515 _cst
= array_list
->st
;
1517 _sst
= _cst
->ss
->match_metadata_desc(inf
->text_version
);
1522 memcpy(inf
->uuid
, array_list
->uuid
, sizeof(int[4]));
1530 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
)
1532 struct intel_super
*super
= st
->sb
;
1533 struct imsm_disk
*disk
;
1535 if (super
->current_vol
>= 0) {
1536 getinfo_super_imsm_volume(st
, info
);
1540 /* Set raid_disks to zero so that Assemble will always pull in valid
1543 info
->array
.raid_disks
= 0;
1544 info
->array
.level
= LEVEL_CONTAINER
;
1545 info
->array
.layout
= 0;
1546 info
->array
.md_minor
= -1;
1547 info
->array
.ctime
= 0; /* N/A for imsm */
1548 info
->array
.utime
= 0;
1549 info
->array
.chunk_size
= 0;
1551 info
->disk
.major
= 0;
1552 info
->disk
.minor
= 0;
1553 info
->disk
.raid_disk
= -1;
1554 info
->reshape_active
= 0;
1555 info
->array
.major_version
= -1;
1556 info
->array
.minor_version
= -2;
1557 strcpy(info
->text_version
, "imsm");
1558 info
->safe_mode_delay
= 0;
1559 info
->disk
.number
= -1;
1560 info
->disk
.state
= 0;
1564 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
1566 disk
= &super
->disks
->disk
;
1567 info
->data_offset
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
1568 info
->component_size
= reserved
;
1569 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
1570 /* we don't change info->disk.raid_disk here because
1571 * this state will be finalized in mdmon after we have
1572 * found the 'most fresh' version of the metadata
1574 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
1575 info
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
1578 /* only call uuid_from_super_imsm when this disk is part of a populated container,
1579 * ->compare_super may have updated the 'num_raid_devs' field for spares
1581 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
1582 uuid_from_super_imsm(st
, info
->uuid
);
1584 memcpy(info
->uuid
, uuid_match_any
, sizeof(int[4]));
1585 fixup_container_spare_uuid(info
);
1589 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
1590 char *update
, char *devname
, int verbose
,
1591 int uuid_set
, char *homehost
)
1593 /* For 'assemble' and 'force' we need to return non-zero if any
1594 * change was made. For others, the return value is ignored.
1595 * Update options are:
1596 * force-one : This device looks a bit old but needs to be included,
1597 * update age info appropriately.
1598 * assemble: clear any 'faulty' flag to allow this device to
1600 * force-array: Array is degraded but being forced, mark it clean
1601 * if that will be needed to assemble it.
1603 * newdev: not used ????
1604 * grow: Array has gained a new device - this is currently for
1606 * resync: mark as dirty so a resync will happen.
1607 * name: update the name - preserving the homehost
1608 * uuid: Change the uuid of the array to match watch is given
1610 * Following are not relevant for this imsm:
1611 * sparc2.2 : update from old dodgey metadata
1612 * super-minor: change the preferred_minor number
1613 * summaries: update redundant counters.
1614 * homehost: update the recorded homehost
1615 * _reshape_progress: record new reshape_progress position.
1618 struct intel_super
*super
= st
->sb
;
1619 struct imsm_super
*mpb
;
1621 /* we can only update container info */
1622 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
1625 mpb
= super
->anchor
;
1627 if (strcmp(update
, "uuid") == 0 && uuid_set
&& !info
->update_private
)
1629 Name
": '--uuid' not supported for imsm metadata\n");
1630 else if (strcmp(update
, "uuid") == 0 && uuid_set
&& info
->update_private
) {
1631 mpb
->orig_family_num
= *((__u32
*) info
->update_private
);
1633 } else if (strcmp(update
, "uuid") == 0) {
1634 __u32
*new_family
= malloc(sizeof(*new_family
));
1636 /* update orig_family_number with the incoming random
1637 * data, report the new effective uuid, and store the
1638 * new orig_family_num for future updates.
1641 memcpy(&mpb
->orig_family_num
, info
->uuid
, sizeof(__u32
));
1642 uuid_from_super_imsm(st
, info
->uuid
);
1643 *new_family
= mpb
->orig_family_num
;
1644 info
->update_private
= new_family
;
1647 } else if (strcmp(update
, "assemble") == 0)
1651 Name
": '--update=%s' not supported for imsm metadata\n",
1654 /* successful update? recompute checksum */
1656 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
1661 static size_t disks_to_mpb_size(int disks
)
1665 size
= sizeof(struct imsm_super
);
1666 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
1667 size
+= 2 * sizeof(struct imsm_dev
);
1668 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
1669 size
+= (4 - 2) * sizeof(struct imsm_map
);
1670 /* 4 possible disk_ord_tbl's */
1671 size
+= 4 * (disks
- 1) * sizeof(__u32
);
1676 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
)
1678 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
1681 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
1684 static void free_devlist(struct intel_super
*super
)
1686 struct intel_dev
*dv
;
1688 while (super
->devlist
) {
1689 dv
= super
->devlist
->next
;
1690 free(super
->devlist
->dev
);
1691 free(super
->devlist
);
1692 super
->devlist
= dv
;
1696 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
1698 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
1701 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
1705 * 0 same, or first was empty, and second was copied
1706 * 1 second had wrong number
1708 * 3 wrong other info
1710 struct intel_super
*first
= st
->sb
;
1711 struct intel_super
*sec
= tst
->sb
;
1719 /* if an anchor does not have num_raid_devs set then it is a free
1722 if (first
->anchor
->num_raid_devs
> 0 &&
1723 sec
->anchor
->num_raid_devs
> 0) {
1724 /* Determine if these disks might ever have been
1725 * related. Further disambiguation can only take place
1726 * in load_super_imsm_all
1728 __u32 first_family
= first
->anchor
->orig_family_num
;
1729 __u32 sec_family
= sec
->anchor
->orig_family_num
;
1731 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
1732 MAX_SIGNATURE_LENGTH
) != 0)
1735 if (first_family
== 0)
1736 first_family
= first
->anchor
->family_num
;
1737 if (sec_family
== 0)
1738 sec_family
= sec
->anchor
->family_num
;
1740 if (first_family
!= sec_family
)
1746 /* if 'first' is a spare promote it to a populated mpb with sec's
1749 if (first
->anchor
->num_raid_devs
== 0 &&
1750 sec
->anchor
->num_raid_devs
> 0) {
1752 struct intel_dev
*dv
;
1753 struct imsm_dev
*dev
;
1755 /* we need to copy raid device info from sec if an allocation
1756 * fails here we don't associate the spare
1758 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
1759 dv
= malloc(sizeof(*dv
));
1762 dev
= malloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
1769 dv
->next
= first
->devlist
;
1770 first
->devlist
= dv
;
1772 if (i
< sec
->anchor
->num_raid_devs
) {
1773 /* allocation failure */
1774 free_devlist(first
);
1775 fprintf(stderr
, "imsm: failed to associate spare\n");
1778 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
1779 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
1780 first
->anchor
->family_num
= sec
->anchor
->family_num
;
1781 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
1782 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
1783 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
1789 static void fd2devname(int fd
, char *name
)
1793 char dname
[PATH_MAX
];
1798 if (fstat(fd
, &st
) != 0)
1800 sprintf(path
, "/sys/dev/block/%d:%d",
1801 major(st
.st_rdev
), minor(st
.st_rdev
));
1803 rv
= readlink(path
, dname
, sizeof(dname
));
1808 nm
= strrchr(dname
, '/');
1810 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
1813 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
1815 static int imsm_read_serial(int fd
, char *devname
,
1816 __u8 serial
[MAX_RAID_SERIAL_LEN
])
1818 unsigned char scsi_serial
[255];
1827 memset(scsi_serial
, 0, sizeof(scsi_serial
));
1829 rv
= scsi_get_serial(fd
, scsi_serial
, sizeof(scsi_serial
));
1831 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
1832 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
1833 fd2devname(fd
, (char *) serial
);
1840 Name
": Failed to retrieve serial for %s\n",
1845 rsp_len
= scsi_serial
[3];
1849 Name
": Failed to retrieve serial for %s\n",
1853 rsp_buf
= (char *) &scsi_serial
[4];
1855 /* trim all whitespace and non-printable characters and convert
1858 for (i
= 0, dest
= rsp_buf
; i
< rsp_len
; i
++) {
1861 /* ':' is reserved for use in placeholder serial
1862 * numbers for missing disks
1870 len
= dest
- rsp_buf
;
1873 /* truncate leading characters */
1874 if (len
> MAX_RAID_SERIAL_LEN
) {
1875 dest
+= len
- MAX_RAID_SERIAL_LEN
;
1876 len
= MAX_RAID_SERIAL_LEN
;
1879 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
1880 memcpy(serial
, dest
, len
);
1885 static int serialcmp(__u8
*s1
, __u8
*s2
)
1887 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
1890 static void serialcpy(__u8
*dest
, __u8
*src
)
1892 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
1896 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
1900 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
1901 if (serialcmp(dl
->serial
, serial
) == 0)
1908 static struct imsm_disk
*
1909 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
1913 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1914 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
1916 if (serialcmp(disk
->serial
, serial
) == 0) {
1927 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
1929 struct imsm_disk
*disk
;
1934 __u8 serial
[MAX_RAID_SERIAL_LEN
];
1936 rv
= imsm_read_serial(fd
, devname
, serial
);
1941 dl
= calloc(1, sizeof(*dl
));
1945 Name
": failed to allocate disk buffer for %s\n",
1951 dl
->major
= major(stb
.st_rdev
);
1952 dl
->minor
= minor(stb
.st_rdev
);
1953 dl
->next
= super
->disks
;
1954 dl
->fd
= keep_fd
? fd
: -1;
1955 assert(super
->disks
== NULL
);
1957 serialcpy(dl
->serial
, serial
);
1960 fd2devname(fd
, name
);
1962 dl
->devname
= strdup(devname
);
1964 dl
->devname
= strdup(name
);
1966 /* look up this disk's index in the current anchor */
1967 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
1970 /* only set index on disks that are a member of a
1971 * populated contianer, i.e. one with raid_devs
1973 if (is_failed(&dl
->disk
))
1975 else if (is_spare(&dl
->disk
))
1983 /* When migrating map0 contains the 'destination' state while map1
1984 * contains the current state. When not migrating map0 contains the
1985 * current state. This routine assumes that map[0].map_state is set to
1986 * the current array state before being called.
1988 * Migration is indicated by one of the following states
1989 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
1990 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
1991 * map1state=unitialized)
1992 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
1994 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
1995 * map1state=degraded)
1997 static void migrate(struct imsm_dev
*dev
, __u8 to_state
, int migr_type
)
1999 struct imsm_map
*dest
;
2000 struct imsm_map
*src
= get_imsm_map(dev
, 0);
2002 dev
->vol
.migr_state
= 1;
2003 set_migr_type(dev
, migr_type
);
2004 dev
->vol
.curr_migr_unit
= 0;
2005 dest
= get_imsm_map(dev
, 1);
2007 /* duplicate and then set the target end state in map[0] */
2008 memcpy(dest
, src
, sizeof_imsm_map(src
));
2009 if (migr_type
== MIGR_REBUILD
) {
2013 for (i
= 0; i
< src
->num_members
; i
++) {
2014 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
2015 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
2019 src
->map_state
= to_state
;
2022 static void end_migration(struct imsm_dev
*dev
, __u8 map_state
)
2024 struct imsm_map
*map
= get_imsm_map(dev
, 0);
2025 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
);
2028 /* merge any IMSM_ORD_REBUILD bits that were not successfully
2029 * completed in the last migration.
2031 * FIXME add support for online capacity expansion and
2032 * raid-level-migration
2034 for (i
= 0; i
< prev
->num_members
; i
++)
2035 map
->disk_ord_tbl
[i
] |= prev
->disk_ord_tbl
[i
];
2037 dev
->vol
.migr_state
= 0;
2038 dev
->vol
.curr_migr_unit
= 0;
2039 map
->map_state
= map_state
;
2043 static int parse_raid_devices(struct intel_super
*super
)
2046 struct imsm_dev
*dev_new
;
2047 size_t len
, len_migr
;
2048 size_t space_needed
= 0;
2049 struct imsm_super
*mpb
= super
->anchor
;
2051 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
2052 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
2053 struct intel_dev
*dv
;
2055 len
= sizeof_imsm_dev(dev_iter
, 0);
2056 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
2058 space_needed
+= len_migr
- len
;
2060 dv
= malloc(sizeof(*dv
));
2063 dev_new
= malloc(len_migr
);
2068 imsm_copy_dev(dev_new
, dev_iter
);
2071 dv
->next
= super
->devlist
;
2072 super
->devlist
= dv
;
2075 /* ensure that super->buf is large enough when all raid devices
2078 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
2081 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
, 512);
2082 if (posix_memalign(&buf
, 512, len
) != 0)
2085 memcpy(buf
, super
->buf
, super
->len
);
2086 memset(buf
+ super
->len
, 0, len
- super
->len
);
2095 /* retrieve a pointer to the bbm log which starts after all raid devices */
2096 struct bbm_log
*__get_imsm_bbm_log(struct imsm_super
*mpb
)
2100 if (__le32_to_cpu(mpb
->bbm_log_size
)) {
2102 ptr
+= mpb
->mpb_size
- __le32_to_cpu(mpb
->bbm_log_size
);
2108 static void __free_imsm(struct intel_super
*super
, int free_disks
);
2110 /* load_imsm_mpb - read matrix metadata
2111 * allocates super->mpb to be freed by free_super
2113 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
2115 unsigned long long dsize
;
2116 unsigned long long sectors
;
2118 struct imsm_super
*anchor
;
2121 get_dev_size(fd
, NULL
, &dsize
);
2123 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0) {
2126 Name
": Cannot seek to anchor block on %s: %s\n",
2127 devname
, strerror(errno
));
2131 if (posix_memalign((void**)&anchor
, 512, 512) != 0) {
2134 Name
": Failed to allocate imsm anchor buffer"
2135 " on %s\n", devname
);
2138 if (read(fd
, anchor
, 512) != 512) {
2141 Name
": Cannot read anchor block on %s: %s\n",
2142 devname
, strerror(errno
));
2147 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
2150 Name
": no IMSM anchor on %s\n", devname
);
2155 __free_imsm(super
, 0);
2156 super
->len
= ROUND_UP(anchor
->mpb_size
, 512);
2157 if (posix_memalign(&super
->buf
, 512, super
->len
) != 0) {
2160 Name
": unable to allocate %zu byte mpb buffer\n",
2165 memcpy(super
->buf
, anchor
, 512);
2167 sectors
= mpb_sectors(anchor
) - 1;
2170 check_sum
= __gen_imsm_checksum(super
->anchor
);
2171 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
2174 Name
": IMSM checksum %x != %x on %s\n",
2176 __le32_to_cpu(super
->anchor
->check_sum
),
2184 /* read the extended mpb */
2185 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0) {
2188 Name
": Cannot seek to extended mpb on %s: %s\n",
2189 devname
, strerror(errno
));
2193 if (read(fd
, super
->buf
+ 512, super
->len
- 512) != super
->len
- 512) {
2196 Name
": Cannot read extended mpb on %s: %s\n",
2197 devname
, strerror(errno
));
2201 check_sum
= __gen_imsm_checksum(super
->anchor
);
2202 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
2205 Name
": IMSM checksum %x != %x on %s\n",
2206 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
2211 /* FIXME the BBM log is disk specific so we cannot use this global
2212 * buffer for all disks. Ok for now since we only look at the global
2213 * bbm_log_size parameter to gate assembly
2215 super
->bbm_log
= __get_imsm_bbm_log(super
->anchor
);
2221 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
2225 err
= load_imsm_mpb(fd
, super
, devname
);
2228 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
2231 err
= parse_raid_devices(super
);
2236 static void __free_imsm_disk(struct dl
*d
)
2247 static void free_imsm_disks(struct intel_super
*super
)
2251 while (super
->disks
) {
2253 super
->disks
= d
->next
;
2254 __free_imsm_disk(d
);
2256 while (super
->missing
) {
2258 super
->missing
= d
->next
;
2259 __free_imsm_disk(d
);
2264 /* free all the pieces hanging off of a super pointer */
2265 static void __free_imsm(struct intel_super
*super
, int free_disks
)
2272 free_imsm_disks(super
);
2273 free_devlist(super
);
2275 free((void *) super
->hba
);
2280 static void free_imsm(struct intel_super
*super
)
2282 __free_imsm(super
, 1);
2286 static void free_super_imsm(struct supertype
*st
)
2288 struct intel_super
*super
= st
->sb
;
2297 static struct intel_super
*alloc_super(int creating_imsm
)
2299 struct intel_super
*super
= malloc(sizeof(*super
));
2302 memset(super
, 0, sizeof(*super
));
2303 super
->creating_imsm
= creating_imsm
;
2304 super
->current_vol
= -1;
2305 super
->create_offset
= ~((__u32
) 0);
2306 if (!check_env("IMSM_NO_PLATFORM"))
2307 super
->orom
= find_imsm_orom();
2308 if (super
->orom
&& !check_env("IMSM_TEST_OROM")) {
2309 struct sys_dev
*list
, *ent
;
2311 /* find the first intel ahci controller */
2312 list
= find_driver_devices("pci", "ahci");
2313 for (ent
= list
; ent
; ent
= ent
->next
)
2314 if (devpath_to_vendor(ent
->path
) == 0x8086)
2317 super
->hba
= ent
->path
;
2320 free_sys_dev(&list
);
2328 /* find_missing - helper routine for load_super_imsm_all that identifies
2329 * disks that have disappeared from the system. This routine relies on
2330 * the mpb being uptodate, which it is at load time.
2332 static int find_missing(struct intel_super
*super
)
2335 struct imsm_super
*mpb
= super
->anchor
;
2337 struct imsm_disk
*disk
;
2339 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2340 disk
= __get_imsm_disk(mpb
, i
);
2341 dl
= serial_to_dl(disk
->serial
, super
);
2345 dl
= malloc(sizeof(*dl
));
2351 dl
->devname
= strdup("missing");
2353 serialcpy(dl
->serial
, disk
->serial
);
2356 dl
->next
= super
->missing
;
2357 super
->missing
= dl
;
2363 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
2365 struct intel_disk
*idisk
= disk_list
;
2368 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
2370 idisk
= idisk
->next
;
2376 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
2377 struct intel_super
*super
,
2378 struct intel_disk
**disk_list
)
2380 struct imsm_disk
*d
= &super
->disks
->disk
;
2381 struct imsm_super
*mpb
= super
->anchor
;
2384 for (i
= 0; i
< tbl_size
; i
++) {
2385 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
2386 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
2388 if (tbl_mpb
->family_num
== mpb
->family_num
) {
2389 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
2390 dprintf("%s: mpb from %d:%d matches %d:%d\n",
2391 __func__
, super
->disks
->major
,
2392 super
->disks
->minor
,
2393 table
[i
]->disks
->major
,
2394 table
[i
]->disks
->minor
);
2398 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
2399 is_configured(d
) == is_configured(tbl_d
)) &&
2400 tbl_mpb
->generation_num
< mpb
->generation_num
) {
2401 /* current version of the mpb is a
2402 * better candidate than the one in
2403 * super_table, but copy over "cross
2404 * generational" status
2406 struct intel_disk
*idisk
;
2408 dprintf("%s: mpb from %d:%d replaces %d:%d\n",
2409 __func__
, super
->disks
->major
,
2410 super
->disks
->minor
,
2411 table
[i
]->disks
->major
,
2412 table
[i
]->disks
->minor
);
2414 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
2415 if (idisk
&& is_failed(&idisk
->disk
))
2416 tbl_d
->status
|= FAILED_DISK
;
2419 struct intel_disk
*idisk
;
2420 struct imsm_disk
*disk
;
2422 /* tbl_mpb is more up to date, but copy
2423 * over cross generational status before
2426 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
2427 if (disk
&& is_failed(disk
))
2428 d
->status
|= FAILED_DISK
;
2430 idisk
= disk_list_get(d
->serial
, *disk_list
);
2433 if (disk
&& is_configured(disk
))
2434 idisk
->disk
.status
|= CONFIGURED_DISK
;
2437 dprintf("%s: mpb from %d:%d prefer %d:%d\n",
2438 __func__
, super
->disks
->major
,
2439 super
->disks
->minor
,
2440 table
[i
]->disks
->major
,
2441 table
[i
]->disks
->minor
);
2449 table
[tbl_size
++] = super
;
2453 /* update/extend the merged list of imsm_disk records */
2454 for (j
= 0; j
< mpb
->num_disks
; j
++) {
2455 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
2456 struct intel_disk
*idisk
;
2458 idisk
= disk_list_get(disk
->serial
, *disk_list
);
2460 idisk
->disk
.status
|= disk
->status
;
2461 if (is_configured(&idisk
->disk
) ||
2462 is_failed(&idisk
->disk
))
2463 idisk
->disk
.status
&= ~(SPARE_DISK
);
2465 idisk
= calloc(1, sizeof(*idisk
));
2468 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
2469 idisk
->disk
= *disk
;
2470 idisk
->next
= *disk_list
;
2474 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
2481 static struct intel_super
*
2482 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
2485 struct imsm_super
*mpb
= super
->anchor
;
2489 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2490 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
2491 struct intel_disk
*idisk
;
2493 idisk
= disk_list_get(disk
->serial
, disk_list
);
2495 if (idisk
->owner
== owner
||
2496 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
2499 dprintf("%s: '%.16s' owner %d != %d\n",
2500 __func__
, disk
->serial
, idisk
->owner
,
2503 dprintf("%s: unknown disk %x [%d]: %.16s\n",
2504 __func__
, __le32_to_cpu(mpb
->family_num
), i
,
2510 if (ok_count
== mpb
->num_disks
)
2515 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
2517 struct intel_super
*s
;
2519 for (s
= super_list
; s
; s
= s
->next
) {
2520 if (family_num
!= s
->anchor
->family_num
)
2522 fprintf(stderr
, "Conflict, offlining family %#x on '%s'\n",
2523 __le32_to_cpu(family_num
), s
->disks
->devname
);
2527 static struct intel_super
*
2528 imsm_thunderdome(struct intel_super
**super_list
, int len
)
2530 struct intel_super
*super_table
[len
];
2531 struct intel_disk
*disk_list
= NULL
;
2532 struct intel_super
*champion
, *spare
;
2533 struct intel_super
*s
, **del
;
2538 memset(super_table
, 0, sizeof(super_table
));
2539 for (s
= *super_list
; s
; s
= s
->next
)
2540 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
2542 for (i
= 0; i
< tbl_size
; i
++) {
2543 struct imsm_disk
*d
;
2544 struct intel_disk
*idisk
;
2545 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
2548 d
= &s
->disks
->disk
;
2550 /* 'd' must appear in merged disk list for its
2551 * configuration to be valid
2553 idisk
= disk_list_get(d
->serial
, disk_list
);
2554 if (idisk
&& idisk
->owner
== i
)
2555 s
= validate_members(s
, disk_list
, i
);
2560 dprintf("%s: marking family: %#x from %d:%d offline\n",
2561 __func__
, mpb
->family_num
,
2562 super_table
[i
]->disks
->major
,
2563 super_table
[i
]->disks
->minor
);
2567 /* This is where the mdadm implementation differs from the Windows
2568 * driver which has no strict concept of a container. We can only
2569 * assemble one family from a container, so when returning a prodigal
2570 * array member to this system the code will not be able to disambiguate
2571 * the container contents that should be assembled ("foreign" versus
2572 * "local"). It requires user intervention to set the orig_family_num
2573 * to a new value to establish a new container. The Windows driver in
2574 * this situation fixes up the volume name in place and manages the
2575 * foreign array as an independent entity.
2580 for (i
= 0; i
< tbl_size
; i
++) {
2581 struct intel_super
*tbl_ent
= super_table
[i
];
2587 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
2592 if (s
&& !is_spare
) {
2593 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
2595 } else if (!s
&& !is_spare
)
2608 fprintf(stderr
, "Chose family %#x on '%s', "
2609 "assemble conflicts to new container with '--update=uuid'\n",
2610 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
2612 /* collect all dl's onto 'champion', and update them to
2613 * champion's version of the status
2615 for (s
= *super_list
; s
; s
= s
->next
) {
2616 struct imsm_super
*mpb
= champion
->anchor
;
2617 struct dl
*dl
= s
->disks
;
2622 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2623 struct imsm_disk
*disk
;
2625 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
2628 /* only set index on disks that are a member of
2629 * a populated contianer, i.e. one with
2632 if (is_failed(&dl
->disk
))
2634 else if (is_spare(&dl
->disk
))
2640 if (i
>= mpb
->num_disks
) {
2641 struct intel_disk
*idisk
;
2643 idisk
= disk_list_get(dl
->serial
, disk_list
);
2644 if (idisk
&& is_spare(&idisk
->disk
) &&
2645 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
2653 dl
->next
= champion
->disks
;
2654 champion
->disks
= dl
;
2658 /* delete 'champion' from super_list */
2659 for (del
= super_list
; *del
; ) {
2660 if (*del
== champion
) {
2661 *del
= (*del
)->next
;
2664 del
= &(*del
)->next
;
2666 champion
->next
= NULL
;
2670 struct intel_disk
*idisk
= disk_list
;
2672 disk_list
= disk_list
->next
;
2679 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
2680 char *devname
, int keep_fd
)
2683 struct intel_super
*super_list
= NULL
;
2684 struct intel_super
*super
= NULL
;
2685 int devnum
= fd2devnum(fd
);
2690 enum sysfs_read_flags flags
;
2692 flags
= GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
;
2693 if (mdmon_running(devnum
))
2694 flags
|= SKIP_GONE_DEVS
;
2696 /* check if 'fd' an opened container */
2697 sra
= sysfs_read(fd
, 0, flags
);
2701 if (sra
->array
.major_version
!= -1 ||
2702 sra
->array
.minor_version
!= -2 ||
2703 strcmp(sra
->text_version
, "imsm") != 0) {
2708 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
2709 struct intel_super
*s
= alloc_super(0);
2716 s
->next
= super_list
;
2720 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
2721 dfd
= dev_open(nm
, keep_fd
? O_RDWR
: O_RDONLY
);
2725 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
2727 /* retry the load if we might have raced against mdmon */
2728 if (err
== 3 && mdmon_running(devnum
))
2729 for (retry
= 0; retry
< 3; retry
++) {
2731 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
2741 /* all mpbs enter, maybe one leaves */
2742 super
= imsm_thunderdome(&super_list
, i
);
2748 if (find_missing(super
) != 0) {
2754 if (st
->subarray
[0]) {
2755 if (atoi(st
->subarray
) <= super
->anchor
->num_raid_devs
)
2756 super
->current_vol
= atoi(st
->subarray
);
2766 while (super_list
) {
2767 struct intel_super
*s
= super_list
;
2769 super_list
= super_list
->next
;
2778 st
->container_dev
= devnum
;
2779 if (err
== 0 && st
->ss
== NULL
) {
2780 st
->ss
= &super_imsm
;
2781 st
->minor_version
= 0;
2782 st
->max_devs
= IMSM_MAX_DEVICES
;
2784 st
->loaded_container
= 1;
2790 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
2792 struct intel_super
*super
;
2796 if (load_super_imsm_all(st
, fd
, &st
->sb
, devname
, 1) == 0)
2800 free_super_imsm(st
);
2802 super
= alloc_super(0);
2805 Name
": malloc of %zu failed.\n",
2810 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
2815 Name
": Failed to load all information "
2816 "sections on %s\n", devname
);
2821 if (st
->subarray
[0]) {
2822 if (atoi(st
->subarray
) <= super
->anchor
->num_raid_devs
)
2823 super
->current_vol
= atoi(st
->subarray
);
2831 if (st
->ss
== NULL
) {
2832 st
->ss
= &super_imsm
;
2833 st
->minor_version
= 0;
2834 st
->max_devs
= IMSM_MAX_DEVICES
;
2836 st
->loaded_container
= 0;
2841 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
2843 if (info
->level
== 1)
2845 return info
->chunk_size
>> 9;
2848 static __u32
info_to_num_data_stripes(mdu_array_info_t
*info
, int num_domains
)
2852 num_stripes
= (info
->size
* 2) / info_to_blocks_per_strip(info
);
2853 num_stripes
/= num_domains
;
2858 static __u32
info_to_blocks_per_member(mdu_array_info_t
*info
)
2860 if (info
->level
== 1)
2861 return info
->size
* 2;
2863 return (info
->size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
2866 static void imsm_update_version_info(struct intel_super
*super
)
2868 /* update the version and attributes */
2869 struct imsm_super
*mpb
= super
->anchor
;
2871 struct imsm_dev
*dev
;
2872 struct imsm_map
*map
;
2875 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
2876 dev
= get_imsm_dev(super
, i
);
2877 map
= get_imsm_map(dev
, 0);
2878 if (__le32_to_cpu(dev
->size_high
) > 0)
2879 mpb
->attributes
|= MPB_ATTRIB_2TB
;
2881 /* FIXME detect when an array spans a port multiplier */
2883 mpb
->attributes
|= MPB_ATTRIB_PM
;
2886 if (mpb
->num_raid_devs
> 1 ||
2887 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
2888 version
= MPB_VERSION_ATTRIBS
;
2889 switch (get_imsm_raid_level(map
)) {
2890 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
2891 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
2892 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
2893 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
2896 if (map
->num_members
>= 5)
2897 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
2898 else if (dev
->status
== DEV_CLONE_N_GO
)
2899 version
= MPB_VERSION_CNG
;
2900 else if (get_imsm_raid_level(map
) == 5)
2901 version
= MPB_VERSION_RAID5
;
2902 else if (map
->num_members
>= 3)
2903 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
2904 else if (get_imsm_raid_level(map
) == 1)
2905 version
= MPB_VERSION_RAID1
;
2907 version
= MPB_VERSION_RAID0
;
2909 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
2913 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
2914 unsigned long long size
, char *name
,
2915 char *homehost
, int *uuid
)
2917 /* We are creating a volume inside a pre-existing container.
2918 * so st->sb is already set.
2920 struct intel_super
*super
= st
->sb
;
2921 struct imsm_super
*mpb
= super
->anchor
;
2922 struct intel_dev
*dv
;
2923 struct imsm_dev
*dev
;
2924 struct imsm_vol
*vol
;
2925 struct imsm_map
*map
;
2926 int idx
= mpb
->num_raid_devs
;
2928 unsigned long long array_blocks
;
2929 size_t size_old
, size_new
;
2930 __u32 num_data_stripes
;
2932 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
2933 fprintf(stderr
, Name
": This imsm-container already has the "
2934 "maximum of %d volumes\n", super
->orom
->vpa
);
2938 /* ensure the mpb is large enough for the new data */
2939 size_old
= __le32_to_cpu(mpb
->mpb_size
);
2940 size_new
= disks_to_mpb_size(info
->nr_disks
);
2941 if (size_new
> size_old
) {
2943 size_t size_round
= ROUND_UP(size_new
, 512);
2945 if (posix_memalign(&mpb_new
, 512, size_round
) != 0) {
2946 fprintf(stderr
, Name
": could not allocate new mpb\n");
2949 memcpy(mpb_new
, mpb
, size_old
);
2952 super
->anchor
= mpb_new
;
2953 mpb
->mpb_size
= __cpu_to_le32(size_new
);
2954 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
2956 super
->current_vol
= idx
;
2957 /* when creating the first raid device in this container set num_disks
2958 * to zero, i.e. delete this spare and add raid member devices in
2959 * add_to_super_imsm_volume()
2961 if (super
->current_vol
== 0)
2964 for (i
= 0; i
< super
->current_vol
; i
++) {
2965 dev
= get_imsm_dev(super
, i
);
2966 if (strncmp((char *) dev
->volume
, name
,
2967 MAX_RAID_SERIAL_LEN
) == 0) {
2968 fprintf(stderr
, Name
": '%s' is already defined for this container\n",
2974 sprintf(st
->subarray
, "%d", idx
);
2975 dv
= malloc(sizeof(*dv
));
2977 fprintf(stderr
, Name
": failed to allocate device list entry\n");
2980 dev
= malloc(sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
2983 fprintf(stderr
, Name
": could not allocate raid device\n");
2986 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
2987 if (info
->level
== 1)
2988 array_blocks
= info_to_blocks_per_member(info
);
2990 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
2991 info
->layout
, info
->chunk_size
,
2993 /* round array size down to closest MB */
2994 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
2996 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
2997 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
2998 dev
->status
= __cpu_to_le32(0);
2999 dev
->reserved_blocks
= __cpu_to_le32(0);
3001 vol
->migr_state
= 0;
3002 set_migr_type(dev
, MIGR_INIT
);
3004 vol
->curr_migr_unit
= 0;
3005 map
= get_imsm_map(dev
, 0);
3006 map
->pba_of_lba0
= __cpu_to_le32(super
->create_offset
);
3007 map
->blocks_per_member
= __cpu_to_le32(info_to_blocks_per_member(info
));
3008 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
3009 map
->failed_disk_num
= ~0;
3010 map
->map_state
= info
->level
? IMSM_T_STATE_UNINITIALIZED
:
3011 IMSM_T_STATE_NORMAL
;
3014 if (info
->level
== 1 && info
->raid_disks
> 2) {
3017 fprintf(stderr
, Name
": imsm does not support more than 2 disks"
3018 "in a raid1 volume\n");
3022 map
->raid_level
= info
->level
;
3023 if (info
->level
== 10) {
3024 map
->raid_level
= 1;
3025 map
->num_domains
= info
->raid_disks
/ 2;
3026 } else if (info
->level
== 1)
3027 map
->num_domains
= info
->raid_disks
;
3029 map
->num_domains
= 1;
3031 num_data_stripes
= info_to_num_data_stripes(info
, map
->num_domains
);
3032 map
->num_data_stripes
= __cpu_to_le32(num_data_stripes
);
3034 map
->num_members
= info
->raid_disks
;
3035 for (i
= 0; i
< map
->num_members
; i
++) {
3036 /* initialized in add_to_super */
3037 set_imsm_ord_tbl_ent(map
, i
, 0);
3039 mpb
->num_raid_devs
++;
3042 dv
->index
= super
->current_vol
;
3043 dv
->next
= super
->devlist
;
3044 super
->devlist
= dv
;
3046 imsm_update_version_info(super
);
3051 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
3052 unsigned long long size
, char *name
,
3053 char *homehost
, int *uuid
)
3055 /* This is primarily called by Create when creating a new array.
3056 * We will then get add_to_super called for each component, and then
3057 * write_init_super called to write it out to each device.
3058 * For IMSM, Create can create on fresh devices or on a pre-existing
3060 * To create on a pre-existing array a different method will be called.
3061 * This one is just for fresh drives.
3063 struct intel_super
*super
;
3064 struct imsm_super
*mpb
;
3069 return init_super_imsm_volume(st
, info
, size
, name
, homehost
, uuid
);
3072 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
3076 super
= alloc_super(1);
3077 if (super
&& posix_memalign(&super
->buf
, 512, mpb_size
) != 0) {
3082 fprintf(stderr
, Name
3083 ": %s could not allocate superblock\n", __func__
);
3086 memset(super
->buf
, 0, mpb_size
);
3088 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
3092 /* zeroing superblock */
3096 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
3098 version
= (char *) mpb
->sig
;
3099 strcpy(version
, MPB_SIGNATURE
);
3100 version
+= strlen(MPB_SIGNATURE
);
3101 strcpy(version
, MPB_VERSION_RAID0
);
3107 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
3108 int fd
, char *devname
)
3110 struct intel_super
*super
= st
->sb
;
3111 struct imsm_super
*mpb
= super
->anchor
;
3113 struct imsm_dev
*dev
;
3114 struct imsm_map
*map
;
3116 dev
= get_imsm_dev(super
, super
->current_vol
);
3117 map
= get_imsm_map(dev
, 0);
3119 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
3120 fprintf(stderr
, Name
": %s: Cannot add spare devices to IMSM volume\n",
3126 /* we're doing autolayout so grab the pre-marked (in
3127 * validate_geometry) raid_disk
3129 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3130 if (dl
->raiddisk
== dk
->raid_disk
)
3133 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3134 if (dl
->major
== dk
->major
&&
3135 dl
->minor
== dk
->minor
)
3140 fprintf(stderr
, Name
": %s is not a member of the same container\n", devname
);
3144 /* add a pristine spare to the metadata */
3145 if (dl
->index
< 0) {
3146 dl
->index
= super
->anchor
->num_disks
;
3147 super
->anchor
->num_disks
++;
3149 set_imsm_ord_tbl_ent(map
, dk
->number
, dl
->index
);
3150 dl
->disk
.status
= CONFIGURED_DISK
;
3152 /* if we are creating the first raid device update the family number */
3153 if (super
->current_vol
== 0) {
3155 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
3156 struct imsm_disk
*_disk
= __get_imsm_disk(mpb
, dl
->index
);
3158 if (!_dev
|| !_disk
) {
3159 fprintf(stderr
, Name
": BUG mpb setup error\n");
3165 sum
+= __gen_imsm_checksum(mpb
);
3166 mpb
->family_num
= __cpu_to_le32(sum
);
3167 mpb
->orig_family_num
= mpb
->family_num
;
3173 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
3174 int fd
, char *devname
)
3176 struct intel_super
*super
= st
->sb
;
3178 unsigned long long size
;
3183 /* if we are on an RAID enabled platform check that the disk is
3184 * attached to the raid controller
3186 if (super
->hba
&& !disk_attached_to_hba(fd
, super
->hba
)) {
3188 Name
": %s is not attached to the raid controller: %s\n",
3189 devname
? : "disk", super
->hba
);
3193 if (super
->current_vol
>= 0)
3194 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
3197 dd
= malloc(sizeof(*dd
));
3200 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
3203 memset(dd
, 0, sizeof(*dd
));
3204 dd
->major
= major(stb
.st_rdev
);
3205 dd
->minor
= minor(stb
.st_rdev
);
3207 dd
->devname
= devname
? strdup(devname
) : NULL
;
3210 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
3213 Name
": failed to retrieve scsi serial, aborting\n");
3218 get_dev_size(fd
, NULL
, &size
);
3220 serialcpy(dd
->disk
.serial
, dd
->serial
);
3221 dd
->disk
.total_blocks
= __cpu_to_le32(size
);
3222 dd
->disk
.status
= SPARE_DISK
;
3223 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
3224 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
3226 dd
->disk
.scsi_id
= __cpu_to_le32(0);
3228 if (st
->update_tail
) {
3229 dd
->next
= super
->add
;
3232 dd
->next
= super
->disks
;
3239 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
3243 struct imsm_super anchor
;
3244 } spare_record
__attribute__ ((aligned(512)));
3246 /* spare records have their own family number and do not have any defined raid
3249 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
3251 struct imsm_super
*mpb
= super
->anchor
;
3252 struct imsm_super
*spare
= &spare_record
.anchor
;
3256 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
)),
3257 spare
->generation_num
= __cpu_to_le32(1UL),
3258 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
3259 spare
->num_disks
= 1,
3260 spare
->num_raid_devs
= 0,
3261 spare
->cache_size
= mpb
->cache_size
,
3262 spare
->pwr_cycle_count
= __cpu_to_le32(1),
3264 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
3265 MPB_SIGNATURE MPB_VERSION_RAID0
);
3267 for (d
= super
->disks
; d
; d
= d
->next
) {
3271 spare
->disk
[0] = d
->disk
;
3272 sum
= __gen_imsm_checksum(spare
);
3273 spare
->family_num
= __cpu_to_le32(sum
);
3274 spare
->orig_family_num
= 0;
3275 sum
= __gen_imsm_checksum(spare
);
3276 spare
->check_sum
= __cpu_to_le32(sum
);
3278 if (store_imsm_mpb(d
->fd
, spare
)) {
3279 fprintf(stderr
, "%s: failed for device %d:%d %s\n",
3280 __func__
, d
->major
, d
->minor
, strerror(errno
));
3292 static int write_super_imsm(struct intel_super
*super
, int doclose
)
3294 struct imsm_super
*mpb
= super
->anchor
;
3300 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
3302 /* 'generation' is incremented everytime the metadata is written */
3303 generation
= __le32_to_cpu(mpb
->generation_num
);
3305 mpb
->generation_num
= __cpu_to_le32(generation
);
3307 /* fix up cases where previous mdadm releases failed to set
3310 if (mpb
->orig_family_num
== 0)
3311 mpb
->orig_family_num
= mpb
->family_num
;
3313 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
3314 for (d
= super
->disks
; d
; d
= d
->next
) {
3318 mpb
->disk
[d
->index
] = d
->disk
;
3320 for (d
= super
->missing
; d
; d
= d
->next
)
3321 mpb
->disk
[d
->index
] = d
->disk
;
3323 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3324 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
3326 imsm_copy_dev(dev
, get_imsm_dev(super
, i
));
3327 mpb_size
+= sizeof_imsm_dev(dev
, 0);
3329 mpb_size
+= __le32_to_cpu(mpb
->bbm_log_size
);
3330 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
3332 /* recalculate checksum */
3333 sum
= __gen_imsm_checksum(mpb
);
3334 mpb
->check_sum
= __cpu_to_le32(sum
);
3336 /* write the mpb for disks that compose raid devices */
3337 for (d
= super
->disks
; d
; d
= d
->next
) {
3340 if (store_imsm_mpb(d
->fd
, mpb
))
3341 fprintf(stderr
, "%s: failed for device %d:%d %s\n",
3342 __func__
, d
->major
, d
->minor
, strerror(errno
));
3350 return write_super_imsm_spares(super
, doclose
);
3356 static int create_array(struct supertype
*st
, int dev_idx
)
3359 struct imsm_update_create_array
*u
;
3360 struct intel_super
*super
= st
->sb
;
3361 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
3362 struct imsm_map
*map
= get_imsm_map(dev
, 0);
3363 struct disk_info
*inf
;
3364 struct imsm_disk
*disk
;
3367 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
3368 sizeof(*inf
) * map
->num_members
;
3371 fprintf(stderr
, "%s: failed to allocate update buffer\n",
3376 u
->type
= update_create_array
;
3377 u
->dev_idx
= dev_idx
;
3378 imsm_copy_dev(&u
->dev
, dev
);
3379 inf
= get_disk_info(u
);
3380 for (i
= 0; i
< map
->num_members
; i
++) {
3381 int idx
= get_imsm_disk_idx(dev
, i
);
3383 disk
= get_imsm_disk(super
, idx
);
3384 serialcpy(inf
[i
].serial
, disk
->serial
);
3386 append_metadata_update(st
, u
, len
);
3391 static int _add_disk(struct supertype
*st
)
3393 struct intel_super
*super
= st
->sb
;
3395 struct imsm_update_add_disk
*u
;
3403 fprintf(stderr
, "%s: failed to allocate update buffer\n",
3408 u
->type
= update_add_disk
;
3409 append_metadata_update(st
, u
, len
);
3414 static int write_init_super_imsm(struct supertype
*st
)
3416 struct intel_super
*super
= st
->sb
;
3417 int current_vol
= super
->current_vol
;
3419 /* we are done with current_vol reset it to point st at the container */
3420 super
->current_vol
= -1;
3422 if (st
->update_tail
) {
3423 /* queue the recently created array / added disk
3424 * as a metadata update */
3428 /* determine if we are creating a volume or adding a disk */
3429 if (current_vol
< 0) {
3430 /* in the add disk case we are running in mdmon
3431 * context, so don't close fd's
3433 return _add_disk(st
);
3435 rv
= create_array(st
, current_vol
);
3437 for (d
= super
->disks
; d
; d
= d
->next
) {
3444 return write_super_imsm(st
->sb
, 1);
3448 static int store_super_imsm(struct supertype
*st
, int fd
)
3450 struct intel_super
*super
= st
->sb
;
3451 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
3457 return store_imsm_mpb(fd
, mpb
);
3463 static int imsm_bbm_log_size(struct imsm_super
*mpb
)
3465 return __le32_to_cpu(mpb
->bbm_log_size
);
3469 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
3470 int layout
, int raiddisks
, int chunk
,
3471 unsigned long long size
, char *dev
,
3472 unsigned long long *freesize
,
3476 unsigned long long ldsize
;
3477 const struct imsm_orom
*orom
;
3479 if (level
!= LEVEL_CONTAINER
)
3484 if (check_env("IMSM_NO_PLATFORM"))
3487 orom
= find_imsm_orom();
3488 if (orom
&& raiddisks
> orom
->tds
) {
3490 fprintf(stderr
, Name
": %d exceeds maximum number of"
3491 " platform supported disks: %d\n",
3492 raiddisks
, orom
->tds
);
3496 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
3499 fprintf(stderr
, Name
": imsm: Cannot open %s: %s\n",
3500 dev
, strerror(errno
));
3503 if (!get_dev_size(fd
, dev
, &ldsize
)) {
3509 *freesize
= avail_size_imsm(st
, ldsize
>> 9);
3514 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
3516 const unsigned long long base_start
= e
[*idx
].start
;
3517 unsigned long long end
= base_start
+ e
[*idx
].size
;
3520 if (base_start
== end
)
3524 for (i
= *idx
; i
< num_extents
; i
++) {
3525 /* extend overlapping extents */
3526 if (e
[i
].start
>= base_start
&&
3527 e
[i
].start
<= end
) {
3530 if (e
[i
].start
+ e
[i
].size
> end
)
3531 end
= e
[i
].start
+ e
[i
].size
;
3532 } else if (e
[i
].start
> end
) {
3538 return end
- base_start
;
3541 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
3543 /* build a composite disk with all known extents and generate a new
3544 * 'maxsize' given the "all disks in an array must share a common start
3545 * offset" constraint
3547 struct extent
*e
= calloc(sum_extents
, sizeof(*e
));
3551 unsigned long long pos
;
3552 unsigned long long start
= 0;
3553 unsigned long long maxsize
;
3554 unsigned long reserve
;
3559 /* coalesce and sort all extents. also, check to see if we need to
3560 * reserve space between member arrays
3563 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
3566 for (i
= 0; i
< dl
->extent_cnt
; i
++)
3569 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
3574 while (i
< sum_extents
) {
3575 e
[j
].start
= e
[i
].start
;
3576 e
[j
].size
= find_size(e
, &i
, sum_extents
);
3578 if (e
[j
-1].size
== 0)
3587 unsigned long long esize
;
3589 esize
= e
[i
].start
- pos
;
3590 if (esize
>= maxsize
) {
3595 pos
= e
[i
].start
+ e
[i
].size
;
3597 } while (e
[i
-1].size
);
3603 /* FIXME assumes volume at offset 0 is the first volume in a
3606 if (start_extent
> 0)
3607 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
3611 if (maxsize
< reserve
)
3614 super
->create_offset
= ~((__u32
) 0);
3615 if (start
+ reserve
> super
->create_offset
)
3616 return 0; /* start overflows create_offset */
3617 super
->create_offset
= start
+ reserve
;
3619 return maxsize
- reserve
;
3622 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
3624 if (level
< 0 || level
== 6 || level
== 4)
3627 /* if we have an orom prevent invalid raid levels */
3630 case 0: return imsm_orom_has_raid0(orom
);
3633 return imsm_orom_has_raid1e(orom
);
3634 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
3635 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
3636 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
3639 return 1; /* not on an Intel RAID platform so anything goes */
3644 #define pr_vrb(fmt, arg...) (void) (verbose && fprintf(stderr, Name fmt, ##arg))
3646 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
3647 int raiddisks
, int chunk
, int verbose
)
3649 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
3650 pr_vrb(": platform does not support raid%d with %d disk%s\n",
3651 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
3654 if (super
->orom
&& level
!= 1 &&
3655 !imsm_orom_has_chunk(super
->orom
, chunk
)) {
3656 pr_vrb(": platform does not support a chunk size of: %d\n", chunk
);
3659 if (layout
!= imsm_level_to_layout(level
)) {
3661 pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
3662 else if (level
== 10)
3663 pr_vrb(": imsm raid 10 only supports the n2 layout\n");
3665 pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
3673 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
3674 * FIX ME add ahci details
3676 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
3677 int layout
, int raiddisks
, int chunk
,
3678 unsigned long long size
, char *dev
,
3679 unsigned long long *freesize
,
3683 struct intel_super
*super
= st
->sb
;
3684 struct imsm_super
*mpb
= super
->anchor
;
3686 unsigned long long pos
= 0;
3687 unsigned long long maxsize
;
3691 /* We must have the container info already read in. */
3695 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, verbose
))
3699 /* General test: make sure there is space for
3700 * 'raiddisks' device extents of size 'size' at a given
3703 unsigned long long minsize
= size
;
3704 unsigned long long start_offset
= MaxSector
;
3707 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
3708 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
3713 e
= get_extents(super
, dl
);
3716 unsigned long long esize
;
3717 esize
= e
[i
].start
- pos
;
3718 if (esize
>= minsize
)
3720 if (found
&& start_offset
== MaxSector
) {
3723 } else if (found
&& pos
!= start_offset
) {
3727 pos
= e
[i
].start
+ e
[i
].size
;
3729 } while (e
[i
-1].size
);
3734 if (dcnt
< raiddisks
) {
3736 fprintf(stderr
, Name
": imsm: Not enough "
3737 "devices with space for this array "
3745 /* This device must be a member of the set */
3746 if (stat(dev
, &stb
) < 0)
3748 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
3750 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
3751 if (dl
->major
== major(stb
.st_rdev
) &&
3752 dl
->minor
== minor(stb
.st_rdev
))
3757 fprintf(stderr
, Name
": %s is not in the "
3758 "same imsm set\n", dev
);
3760 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
3761 /* If a volume is present then the current creation attempt
3762 * cannot incorporate new spares because the orom may not
3763 * understand this configuration (all member disks must be
3764 * members of each array in the container).
3766 fprintf(stderr
, Name
": %s is a spare and a volume"
3767 " is already defined for this container\n", dev
);
3768 fprintf(stderr
, Name
": The option-rom requires all member"
3769 " disks to be a member of all volumes\n");
3773 /* retrieve the largest free space block */
3774 e
= get_extents(super
, dl
);
3779 unsigned long long esize
;
3781 esize
= e
[i
].start
- pos
;
3782 if (esize
>= maxsize
)
3784 pos
= e
[i
].start
+ e
[i
].size
;
3786 } while (e
[i
-1].size
);
3791 fprintf(stderr
, Name
": unable to determine free space for: %s\n",
3795 if (maxsize
< size
) {
3797 fprintf(stderr
, Name
": %s not enough space (%llu < %llu)\n",
3798 dev
, maxsize
, size
);
3802 /* count total number of extents for merge */
3804 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3806 i
+= dl
->extent_cnt
;
3808 maxsize
= merge_extents(super
, i
);
3809 if (maxsize
< size
|| maxsize
== 0) {
3811 fprintf(stderr
, Name
": not enough space after merge (%llu < %llu)\n",
3816 *freesize
= maxsize
;
3821 static int reserve_space(struct supertype
*st
, int raiddisks
,
3822 unsigned long long size
, int chunk
,
3823 unsigned long long *freesize
)
3825 struct intel_super
*super
= st
->sb
;
3826 struct imsm_super
*mpb
= super
->anchor
;
3831 unsigned long long maxsize
;
3832 unsigned long long minsize
;
3836 /* find the largest common start free region of the possible disks */
3840 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
3846 /* don't activate new spares if we are orom constrained
3847 * and there is already a volume active in the container
3849 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
3852 e
= get_extents(super
, dl
);
3855 for (i
= 1; e
[i
-1].size
; i
++)
3863 maxsize
= merge_extents(super
, extent_cnt
);
3868 if (cnt
< raiddisks
||
3869 (super
->orom
&& used
&& used
!= raiddisks
) ||
3870 maxsize
< minsize
||
3872 fprintf(stderr
, Name
": not enough devices with space to create array.\n");
3873 return 0; /* No enough free spaces large enough */
3885 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3887 dl
->raiddisk
= cnt
++;
3894 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
3895 int raiddisks
, int chunk
, unsigned long long size
,
3896 char *dev
, unsigned long long *freesize
,
3903 /* if given unused devices create a container
3904 * if given given devices in a container create a member volume
3906 if (level
== LEVEL_CONTAINER
) {
3907 /* Must be a fresh device to add to a container */
3908 return validate_geometry_imsm_container(st
, level
, layout
,
3909 raiddisks
, chunk
, size
,
3915 if (st
->sb
&& freesize
) {
3916 /* we are being asked to automatically layout a
3917 * new volume based on the current contents of
3918 * the container. If the the parameters can be
3919 * satisfied reserve_space will record the disks,
3920 * start offset, and size of the volume to be
3921 * created. add_to_super and getinfo_super
3922 * detect when autolayout is in progress.
3924 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
3928 return reserve_space(st
, raiddisks
, size
, chunk
, freesize
);
3933 /* creating in a given container */
3934 return validate_geometry_imsm_volume(st
, level
, layout
,
3935 raiddisks
, chunk
, size
,
3936 dev
, freesize
, verbose
);
3939 /* This device needs to be a device in an 'imsm' container */
3940 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
3944 Name
": Cannot create this array on device %s\n",
3949 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
3951 fprintf(stderr
, Name
": Cannot open %s: %s\n",
3952 dev
, strerror(errno
));
3955 /* Well, it is in use by someone, maybe an 'imsm' container. */
3956 cfd
= open_container(fd
);
3960 fprintf(stderr
, Name
": Cannot use %s: It is busy\n",
3964 sra
= sysfs_read(cfd
, 0, GET_VERSION
);
3965 if (sra
&& sra
->array
.major_version
== -1 &&
3966 strcmp(sra
->text_version
, "imsm") == 0)
3970 /* This is a member of a imsm container. Load the container
3971 * and try to create a volume
3973 struct intel_super
*super
;
3975 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
, 1) == 0) {
3977 st
->container_dev
= fd2devnum(cfd
);
3979 return validate_geometry_imsm_volume(st
, level
, layout
,
3987 fprintf(stderr
, Name
": failed container membership check\n");
3992 #endif /* MDASSEMBLE */
3994 static int is_rebuilding(struct imsm_dev
*dev
)
3996 struct imsm_map
*migr_map
;
3998 if (!dev
->vol
.migr_state
)
4001 if (migr_type(dev
) != MIGR_REBUILD
)
4004 migr_map
= get_imsm_map(dev
, 1);
4006 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
4012 static void update_recovery_start(struct imsm_dev
*dev
, struct mdinfo
*array
)
4014 struct mdinfo
*rebuild
= NULL
;
4018 if (!is_rebuilding(dev
))
4021 /* Find the rebuild target, but punt on the dual rebuild case */
4022 for (d
= array
->devs
; d
; d
= d
->next
)
4023 if (d
->recovery_start
== 0) {
4029 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
4030 rebuild
->recovery_start
= units
* blocks_per_migr_unit(dev
);
4034 static struct mdinfo
*container_content_imsm(struct supertype
*st
)
4036 /* Given a container loaded by load_super_imsm_all,
4037 * extract information about all the arrays into
4040 * For each imsm_dev create an mdinfo, fill it in,
4041 * then look for matching devices in super->disks
4042 * and create appropriate device mdinfo.
4044 struct intel_super
*super
= st
->sb
;
4045 struct imsm_super
*mpb
= super
->anchor
;
4046 struct mdinfo
*rest
= NULL
;
4049 /* do not assemble arrays that might have bad blocks */
4050 if (imsm_bbm_log_size(super
->anchor
)) {
4051 fprintf(stderr
, Name
": BBM log found in metadata. "
4052 "Cannot activate array(s).\n");
4056 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4057 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4058 struct imsm_map
*map
= get_imsm_map(dev
, 0);
4059 struct mdinfo
*this;
4062 /* do not publish arrays that are in the middle of an
4063 * unsupported migration
4065 if (dev
->vol
.migr_state
&&
4066 (migr_type(dev
) == MIGR_GEN_MIGR
||
4067 migr_type(dev
) == MIGR_STATE_CHANGE
)) {
4068 fprintf(stderr
, Name
": cannot assemble volume '%.16s':"
4069 " unsupported migration in progress\n",
4074 this = malloc(sizeof(*this));
4076 fprintf(stderr
, Name
": failed to allocate %lu bytes\n",
4080 memset(this, 0, sizeof(*this));
4083 super
->current_vol
= i
;
4084 getinfo_super_imsm_volume(st
, this);
4085 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
4086 unsigned long long recovery_start
;
4087 struct mdinfo
*info_d
;
4094 idx
= get_imsm_disk_idx(dev
, slot
);
4095 ord
= get_imsm_ord_tbl_ent(dev
, slot
);
4096 for (d
= super
->disks
; d
; d
= d
->next
)
4097 if (d
->index
== idx
)
4100 recovery_start
= MaxSector
;
4103 if (d
&& is_failed(&d
->disk
))
4105 if (ord
& IMSM_ORD_REBUILD
)
4109 * if we skip some disks the array will be assmebled degraded;
4110 * reset resync start to avoid a dirty-degraded
4111 * situation when performing the intial sync
4113 * FIXME handle dirty degraded
4115 if ((skip
|| recovery_start
== 0) && !dev
->vol
.dirty
)
4116 this->resync_start
= MaxSector
;
4120 info_d
= calloc(1, sizeof(*info_d
));
4122 fprintf(stderr
, Name
": failed to allocate disk"
4123 " for volume %.16s\n", dev
->volume
);
4124 info_d
= this->devs
;
4126 struct mdinfo
*d
= info_d
->next
;
4135 info_d
->next
= this->devs
;
4136 this->devs
= info_d
;
4138 info_d
->disk
.number
= d
->index
;
4139 info_d
->disk
.major
= d
->major
;
4140 info_d
->disk
.minor
= d
->minor
;
4141 info_d
->disk
.raid_disk
= slot
;
4142 info_d
->recovery_start
= recovery_start
;
4144 if (info_d
->recovery_start
== MaxSector
)
4145 this->array
.working_disks
++;
4147 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
4148 info_d
->data_offset
= __le32_to_cpu(map
->pba_of_lba0
);
4149 info_d
->component_size
= __le32_to_cpu(map
->blocks_per_member
);
4151 /* now that the disk list is up-to-date fixup recovery_start */
4152 update_recovery_start(dev
, this);
4161 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
4164 struct intel_super
*super
= c
->sb
;
4165 struct imsm_super
*mpb
= super
->anchor
;
4167 if (atoi(inst
) >= mpb
->num_raid_devs
) {
4168 fprintf(stderr
, "%s: subarry index %d, out of range\n",
4169 __func__
, atoi(inst
));
4173 dprintf("imsm: open_new %s\n", inst
);
4174 a
->info
.container_member
= atoi(inst
);
4178 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
, int failed
)
4180 struct imsm_map
*map
= get_imsm_map(dev
, 0);
4183 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
4184 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
4186 switch (get_imsm_raid_level(map
)) {
4188 return IMSM_T_STATE_FAILED
;
4191 if (failed
< map
->num_members
)
4192 return IMSM_T_STATE_DEGRADED
;
4194 return IMSM_T_STATE_FAILED
;
4199 * check to see if any mirrors have failed, otherwise we
4200 * are degraded. Even numbered slots are mirrored on
4204 /* gcc -Os complains that this is unused */
4205 int insync
= insync
;
4207 for (i
= 0; i
< map
->num_members
; i
++) {
4208 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
);
4209 int idx
= ord_to_idx(ord
);
4210 struct imsm_disk
*disk
;
4212 /* reset the potential in-sync count on even-numbered
4213 * slots. num_copies is always 2 for imsm raid10
4218 disk
= get_imsm_disk(super
, idx
);
4219 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
4222 /* no in-sync disks left in this mirror the
4226 return IMSM_T_STATE_FAILED
;
4229 return IMSM_T_STATE_DEGRADED
;
4233 return IMSM_T_STATE_DEGRADED
;
4235 return IMSM_T_STATE_FAILED
;
4241 return map
->map_state
;
4244 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
)
4248 struct imsm_disk
*disk
;
4249 struct imsm_map
*map
= get_imsm_map(dev
, 0);
4250 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
);
4254 /* at the beginning of migration we set IMSM_ORD_REBUILD on
4255 * disks that are being rebuilt. New failures are recorded to
4256 * map[0]. So we look through all the disks we started with and
4257 * see if any failures are still present, or if any new ones
4260 * FIXME add support for online capacity expansion and
4261 * raid-level-migration
4263 for (i
= 0; i
< prev
->num_members
; i
++) {
4264 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
4265 ord
|= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
4266 idx
= ord_to_idx(ord
);
4268 disk
= get_imsm_disk(super
, idx
);
4269 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
4276 static int is_resyncing(struct imsm_dev
*dev
)
4278 struct imsm_map
*migr_map
;
4280 if (!dev
->vol
.migr_state
)
4283 if (migr_type(dev
) == MIGR_INIT
||
4284 migr_type(dev
) == MIGR_REPAIR
)
4287 migr_map
= get_imsm_map(dev
, 1);
4289 if (migr_map
->map_state
== IMSM_T_STATE_NORMAL
)
4295 /* return true if we recorded new information */
4296 static int mark_failure(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
4300 struct imsm_map
*map
;
4302 /* new failures are always set in map[0] */
4303 map
= get_imsm_map(dev
, 0);
4305 slot
= get_imsm_disk_slot(map
, idx
);
4309 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
4310 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
4313 disk
->status
|= FAILED_DISK
;
4314 disk
->status
&= ~CONFIGURED_DISK
;
4315 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
4316 if (~map
->failed_disk_num
== 0)
4317 map
->failed_disk_num
= slot
;
4321 static void mark_missing(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
4323 mark_failure(dev
, disk
, idx
);
4325 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
4328 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
4329 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
4332 /* Handle dirty -> clean transititions and resync. Degraded and rebuild
4333 * states are handled in imsm_set_disk() with one exception, when a
4334 * resync is stopped due to a new failure this routine will set the
4335 * 'degraded' state for the array.
4337 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
4339 int inst
= a
->info
.container_member
;
4340 struct intel_super
*super
= a
->container
->sb
;
4341 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
4342 struct imsm_map
*map
= get_imsm_map(dev
, 0);
4343 int failed
= imsm_count_failed(super
, dev
);
4344 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
);
4345 __u32 blocks_per_unit
;
4347 /* before we activate this array handle any missing disks */
4348 if (consistent
== 2 && super
->missing
) {
4351 dprintf("imsm: mark missing\n");
4352 end_migration(dev
, map_state
);
4353 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
4354 mark_missing(dev
, &dl
->disk
, dl
->index
);
4355 super
->updates_pending
++;
4358 if (consistent
== 2 &&
4359 (!is_resync_complete(&a
->info
) ||
4360 map_state
!= IMSM_T_STATE_NORMAL
||
4361 dev
->vol
.migr_state
))
4364 if (is_resync_complete(&a
->info
)) {
4365 /* complete intialization / resync,
4366 * recovery and interrupted recovery is completed in
4369 if (is_resyncing(dev
)) {
4370 dprintf("imsm: mark resync done\n");
4371 end_migration(dev
, map_state
);
4372 super
->updates_pending
++;
4374 } else if (!is_resyncing(dev
) && !failed
) {
4375 /* mark the start of the init process if nothing is failed */
4376 dprintf("imsm: mark resync start\n");
4377 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
4378 migrate(dev
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
4380 migrate(dev
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
4381 super
->updates_pending
++;
4384 /* check if we can update curr_migr_unit from resync_start, recovery_start */
4385 blocks_per_unit
= blocks_per_migr_unit(dev
);
4386 if (blocks_per_unit
&& failed
<= 1) {
4390 if (migr_type(dev
) == MIGR_REBUILD
)
4391 units
= min_recovery_start(&a
->info
) / blocks_per_unit
;
4393 units
= a
->info
.resync_start
/ blocks_per_unit
;
4396 /* check that we did not overflow 32-bits, and that
4397 * curr_migr_unit needs updating
4399 if (units32
== units
&&
4400 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
4401 dprintf("imsm: mark checkpoint (%u)\n", units32
);
4402 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
4403 super
->updates_pending
++;
4407 /* mark dirty / clean */
4408 if (dev
->vol
.dirty
!= !consistent
) {
4409 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
4414 super
->updates_pending
++;
4419 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
4421 int inst
= a
->info
.container_member
;
4422 struct intel_super
*super
= a
->container
->sb
;
4423 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
4424 struct imsm_map
*map
= get_imsm_map(dev
, 0);
4425 struct imsm_disk
*disk
;
4430 if (n
> map
->num_members
)
4431 fprintf(stderr
, "imsm: set_disk %d out of range 0..%d\n",
4432 n
, map
->num_members
- 1);
4437 dprintf("imsm: set_disk %d:%x\n", n
, state
);
4439 ord
= get_imsm_ord_tbl_ent(dev
, n
);
4440 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
4442 /* check for new failures */
4443 if (state
& DS_FAULTY
) {
4444 if (mark_failure(dev
, disk
, ord_to_idx(ord
)))
4445 super
->updates_pending
++;
4448 /* check if in_sync */
4449 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
4450 struct imsm_map
*migr_map
= get_imsm_map(dev
, 1);
4452 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
4453 super
->updates_pending
++;
4456 failed
= imsm_count_failed(super
, dev
);
4457 map_state
= imsm_check_degraded(super
, dev
, failed
);
4459 /* check if recovery complete, newly degraded, or failed */
4460 if (map_state
== IMSM_T_STATE_NORMAL
&& is_rebuilding(dev
)) {
4461 end_migration(dev
, map_state
);
4462 map
= get_imsm_map(dev
, 0);
4463 map
->failed_disk_num
= ~0;
4464 super
->updates_pending
++;
4465 } else if (map_state
== IMSM_T_STATE_DEGRADED
&&
4466 map
->map_state
!= map_state
&&
4467 !dev
->vol
.migr_state
) {
4468 dprintf("imsm: mark degraded\n");
4469 map
->map_state
= map_state
;
4470 super
->updates_pending
++;
4471 } else if (map_state
== IMSM_T_STATE_FAILED
&&
4472 map
->map_state
!= map_state
) {
4473 dprintf("imsm: mark failed\n");
4474 end_migration(dev
, map_state
);
4475 super
->updates_pending
++;
4479 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
4482 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
4483 unsigned long long dsize
;
4484 unsigned long long sectors
;
4486 get_dev_size(fd
, NULL
, &dsize
);
4488 if (mpb_size
> 512) {
4489 /* -1 to account for anchor */
4490 sectors
= mpb_sectors(mpb
) - 1;
4492 /* write the extended mpb to the sectors preceeding the anchor */
4493 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0)
4496 if (write(fd
, buf
+ 512, 512 * sectors
) != 512 * sectors
)
4500 /* first block is stored on second to last sector of the disk */
4501 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0)
4504 if (write(fd
, buf
, 512) != 512)
4510 static void imsm_sync_metadata(struct supertype
*container
)
4512 struct intel_super
*super
= container
->sb
;
4514 if (!super
->updates_pending
)
4517 write_super_imsm(super
, 0);
4519 super
->updates_pending
= 0;
4522 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
4524 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
4525 int i
= get_imsm_disk_idx(dev
, idx
);
4528 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4532 if (dl
&& is_failed(&dl
->disk
))
4536 dprintf("%s: found %x:%x\n", __func__
, dl
->major
, dl
->minor
);
4541 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
4542 struct active_array
*a
, int activate_new
)
4544 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
4545 int idx
= get_imsm_disk_idx(dev
, slot
);
4546 struct imsm_super
*mpb
= super
->anchor
;
4547 struct imsm_map
*map
;
4548 unsigned long long pos
;
4557 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
4558 /* If in this array, skip */
4559 for (d
= a
->info
.devs
; d
; d
= d
->next
)
4560 if (d
->state_fd
>= 0 &&
4561 d
->disk
.major
== dl
->major
&&
4562 d
->disk
.minor
== dl
->minor
) {
4563 dprintf("%x:%x already in array\n", dl
->major
, dl
->minor
);
4569 /* skip in use or failed drives */
4570 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
4572 dprintf("%x:%x status (failed: %d index: %d)\n",
4573 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
4577 /* skip pure spares when we are looking for partially
4578 * assimilated drives
4580 if (dl
->index
== -1 && !activate_new
)
4583 /* Does this unused device have the requisite free space?
4584 * It needs to be able to cover all member volumes
4586 ex
= get_extents(super
, dl
);
4588 dprintf("cannot get extents\n");
4591 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4592 dev
= get_imsm_dev(super
, i
);
4593 map
= get_imsm_map(dev
, 0);
4595 /* check if this disk is already a member of
4598 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
4604 array_start
= __le32_to_cpu(map
->pba_of_lba0
);
4605 array_end
= array_start
+
4606 __le32_to_cpu(map
->blocks_per_member
) - 1;
4609 /* check that we can start at pba_of_lba0 with
4610 * blocks_per_member of space
4612 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
4616 pos
= ex
[j
].start
+ ex
[j
].size
;
4618 } while (ex
[j
-1].size
);
4625 if (i
< mpb
->num_raid_devs
) {
4626 dprintf("%x:%x does not have %u to %u available\n",
4627 dl
->major
, dl
->minor
, array_start
, array_end
);
4637 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
4638 struct metadata_update
**updates
)
4641 * Find a device with unused free space and use it to replace a
4642 * failed/vacant region in an array. We replace failed regions one a
4643 * array at a time. The result is that a new spare disk will be added
4644 * to the first failed array and after the monitor has finished
4645 * propagating failures the remainder will be consumed.
4647 * FIXME add a capability for mdmon to request spares from another
4651 struct intel_super
*super
= a
->container
->sb
;
4652 int inst
= a
->info
.container_member
;
4653 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
4654 struct imsm_map
*map
= get_imsm_map(dev
, 0);
4655 int failed
= a
->info
.array
.raid_disks
;
4656 struct mdinfo
*rv
= NULL
;
4659 struct metadata_update
*mu
;
4661 struct imsm_update_activate_spare
*u
;
4665 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
4666 if ((d
->curr_state
& DS_FAULTY
) &&
4668 /* wait for Removal to happen */
4670 if (d
->state_fd
>= 0)
4674 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
4675 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
4676 if (imsm_check_degraded(super
, dev
, failed
) != IMSM_T_STATE_DEGRADED
)
4679 /* For each slot, if it is not working, find a spare */
4680 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
4681 for (d
= a
->info
.devs
; d
; d
= d
->next
)
4682 if (d
->disk
.raid_disk
== i
)
4684 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
4685 if (d
&& (d
->state_fd
>= 0))
4689 * OK, this device needs recovery. Try to re-add the
4690 * previous occupant of this slot, if this fails see if
4691 * we can continue the assimilation of a spare that was
4692 * partially assimilated, finally try to activate a new
4695 dl
= imsm_readd(super
, i
, a
);
4697 dl
= imsm_add_spare(super
, i
, a
, 0);
4699 dl
= imsm_add_spare(super
, i
, a
, 1);
4703 /* found a usable disk with enough space */
4704 di
= malloc(sizeof(*di
));
4707 memset(di
, 0, sizeof(*di
));
4709 /* dl->index will be -1 in the case we are activating a
4710 * pristine spare. imsm_process_update() will create a
4711 * new index in this case. Once a disk is found to be
4712 * failed in all member arrays it is kicked from the
4715 di
->disk
.number
= dl
->index
;
4717 /* (ab)use di->devs to store a pointer to the device
4720 di
->devs
= (struct mdinfo
*) dl
;
4722 di
->disk
.raid_disk
= i
;
4723 di
->disk
.major
= dl
->major
;
4724 di
->disk
.minor
= dl
->minor
;
4726 di
->recovery_start
= 0;
4727 di
->data_offset
= __le32_to_cpu(map
->pba_of_lba0
);
4728 di
->component_size
= a
->info
.component_size
;
4729 di
->container_member
= inst
;
4730 super
->random
= random32();
4734 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
4735 i
, di
->data_offset
);
4741 /* No spares found */
4743 /* Now 'rv' has a list of devices to return.
4744 * Create a metadata_update record to update the
4745 * disk_ord_tbl for the array
4747 mu
= malloc(sizeof(*mu
));
4749 mu
->buf
= malloc(sizeof(struct imsm_update_activate_spare
) * num_spares
);
4750 if (mu
->buf
== NULL
) {
4757 struct mdinfo
*n
= rv
->next
;
4766 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
4767 mu
->next
= *updates
;
4768 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
4770 for (di
= rv
; di
; di
= di
->next
) {
4771 u
->type
= update_activate_spare
;
4772 u
->dl
= (struct dl
*) di
->devs
;
4774 u
->slot
= di
->disk
.raid_disk
;
4785 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
4787 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
4788 struct imsm_map
*map
= get_imsm_map(dev
, 0);
4789 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, 0);
4790 struct disk_info
*inf
= get_disk_info(u
);
4791 struct imsm_disk
*disk
;
4795 for (i
= 0; i
< map
->num_members
; i
++) {
4796 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
));
4797 for (j
= 0; j
< new_map
->num_members
; j
++)
4798 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
4805 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, int index
);
4807 static void imsm_process_update(struct supertype
*st
,
4808 struct metadata_update
*update
)
4811 * crack open the metadata_update envelope to find the update record
4812 * update can be one of:
4813 * update_activate_spare - a spare device has replaced a failed
4814 * device in an array, update the disk_ord_tbl. If this disk is
4815 * present in all member arrays then also clear the SPARE_DISK
4818 struct intel_super
*super
= st
->sb
;
4819 struct imsm_super
*mpb
;
4820 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
4822 /* update requires a larger buf but the allocation failed */
4823 if (super
->next_len
&& !super
->next_buf
) {
4824 super
->next_len
= 0;
4828 if (super
->next_buf
) {
4829 memcpy(super
->next_buf
, super
->buf
, super
->len
);
4831 super
->len
= super
->next_len
;
4832 super
->buf
= super
->next_buf
;
4834 super
->next_len
= 0;
4835 super
->next_buf
= NULL
;
4838 mpb
= super
->anchor
;
4841 case update_activate_spare
: {
4842 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
4843 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
4844 struct imsm_map
*map
= get_imsm_map(dev
, 0);
4845 struct imsm_map
*migr_map
;
4846 struct active_array
*a
;
4847 struct imsm_disk
*disk
;
4852 int victim
= get_imsm_disk_idx(dev
, u
->slot
);
4855 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4860 fprintf(stderr
, "error: imsm_activate_spare passed "
4861 "an unknown disk (index: %d)\n",
4866 super
->updates_pending
++;
4868 /* count failures (excluding rebuilds and the victim)
4869 * to determine map[0] state
4872 for (i
= 0; i
< map
->num_members
; i
++) {
4875 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
));
4876 if (!disk
|| is_failed(disk
))
4880 /* adding a pristine spare, assign a new index */
4881 if (dl
->index
< 0) {
4882 dl
->index
= super
->anchor
->num_disks
;
4883 super
->anchor
->num_disks
++;
4886 disk
->status
|= CONFIGURED_DISK
;
4887 disk
->status
&= ~SPARE_DISK
;
4890 to_state
= imsm_check_degraded(super
, dev
, failed
);
4891 map
->map_state
= IMSM_T_STATE_DEGRADED
;
4892 migrate(dev
, to_state
, MIGR_REBUILD
);
4893 migr_map
= get_imsm_map(dev
, 1);
4894 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
4895 set_imsm_ord_tbl_ent(migr_map
, u
->slot
, dl
->index
| IMSM_ORD_REBUILD
);
4897 /* update the family_num to mark a new container
4898 * generation, being careful to record the existing
4899 * family_num in orig_family_num to clean up after
4900 * earlier mdadm versions that neglected to set it.
4902 if (mpb
->orig_family_num
== 0)
4903 mpb
->orig_family_num
= mpb
->family_num
;
4904 mpb
->family_num
+= super
->random
;
4906 /* count arrays using the victim in the metadata */
4908 for (a
= st
->arrays
; a
; a
= a
->next
) {
4909 dev
= get_imsm_dev(super
, a
->info
.container_member
);
4910 map
= get_imsm_map(dev
, 0);
4912 if (get_imsm_disk_slot(map
, victim
) >= 0)
4916 /* delete the victim if it is no longer being
4922 /* We know that 'manager' isn't touching anything,
4923 * so it is safe to delete
4925 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
4926 if ((*dlp
)->index
== victim
)
4929 /* victim may be on the missing list */
4931 for (dlp
= &super
->missing
; *dlp
; dlp
= &(*dlp
)->next
)
4932 if ((*dlp
)->index
== victim
)
4934 imsm_delete(super
, dlp
, victim
);
4938 case update_create_array
: {
4939 /* someone wants to create a new array, we need to be aware of
4940 * a few races/collisions:
4941 * 1/ 'Create' called by two separate instances of mdadm
4942 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
4943 * devices that have since been assimilated via
4945 * In the event this update can not be carried out mdadm will
4946 * (FIX ME) notice that its update did not take hold.
4948 struct imsm_update_create_array
*u
= (void *) update
->buf
;
4949 struct intel_dev
*dv
;
4950 struct imsm_dev
*dev
;
4951 struct imsm_map
*map
, *new_map
;
4952 unsigned long long start
, end
;
4953 unsigned long long new_start
, new_end
;
4955 struct disk_info
*inf
;
4958 /* handle racing creates: first come first serve */
4959 if (u
->dev_idx
< mpb
->num_raid_devs
) {
4960 dprintf("%s: subarray %d already defined\n",
4961 __func__
, u
->dev_idx
);
4965 /* check update is next in sequence */
4966 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
4967 dprintf("%s: can not create array %d expected index %d\n",
4968 __func__
, u
->dev_idx
, mpb
->num_raid_devs
);
4972 new_map
= get_imsm_map(&u
->dev
, 0);
4973 new_start
= __le32_to_cpu(new_map
->pba_of_lba0
);
4974 new_end
= new_start
+ __le32_to_cpu(new_map
->blocks_per_member
);
4975 inf
= get_disk_info(u
);
4977 /* handle activate_spare versus create race:
4978 * check to make sure that overlapping arrays do not include
4981 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4982 dev
= get_imsm_dev(super
, i
);
4983 map
= get_imsm_map(dev
, 0);
4984 start
= __le32_to_cpu(map
->pba_of_lba0
);
4985 end
= start
+ __le32_to_cpu(map
->blocks_per_member
);
4986 if ((new_start
>= start
&& new_start
<= end
) ||
4987 (start
>= new_start
&& start
<= new_end
))
4992 if (disks_overlap(super
, i
, u
)) {
4993 dprintf("%s: arrays overlap\n", __func__
);
4998 /* check that prepare update was successful */
4999 if (!update
->space
) {
5000 dprintf("%s: prepare update failed\n", __func__
);
5004 /* check that all disks are still active before committing
5005 * changes. FIXME: could we instead handle this by creating a
5006 * degraded array? That's probably not what the user expects,
5007 * so better to drop this update on the floor.
5009 for (i
= 0; i
< new_map
->num_members
; i
++) {
5010 dl
= serial_to_dl(inf
[i
].serial
, super
);
5012 dprintf("%s: disk disappeared\n", __func__
);
5017 super
->updates_pending
++;
5019 /* convert spares to members and fixup ord_tbl */
5020 for (i
= 0; i
< new_map
->num_members
; i
++) {
5021 dl
= serial_to_dl(inf
[i
].serial
, super
);
5022 if (dl
->index
== -1) {
5023 dl
->index
= mpb
->num_disks
;
5025 dl
->disk
.status
|= CONFIGURED_DISK
;
5026 dl
->disk
.status
&= ~SPARE_DISK
;
5028 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
5033 update
->space
= NULL
;
5034 imsm_copy_dev(dev
, &u
->dev
);
5035 dv
->index
= u
->dev_idx
;
5036 dv
->next
= super
->devlist
;
5037 super
->devlist
= dv
;
5038 mpb
->num_raid_devs
++;
5040 imsm_update_version_info(super
);
5043 /* mdmon knows how to release update->space, but not
5044 * ((struct intel_dev *) update->space)->dev
5046 if (update
->space
) {
5052 case update_add_disk
:
5054 /* we may be able to repair some arrays if disks are
5057 struct active_array
*a
;
5059 super
->updates_pending
++;
5060 for (a
= st
->arrays
; a
; a
= a
->next
)
5061 a
->check_degraded
= 1;
5063 /* add some spares to the metadata */
5064 while (super
->add
) {
5068 super
->add
= al
->next
;
5069 al
->next
= super
->disks
;
5071 dprintf("%s: added %x:%x\n",
5072 __func__
, al
->major
, al
->minor
);
5079 static void imsm_prepare_update(struct supertype
*st
,
5080 struct metadata_update
*update
)
5083 * Allocate space to hold new disk entries, raid-device entries or a new
5084 * mpb if necessary. The manager synchronously waits for updates to
5085 * complete in the monitor, so new mpb buffers allocated here can be
5086 * integrated by the monitor thread without worrying about live pointers
5087 * in the manager thread.
5089 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
5090 struct intel_super
*super
= st
->sb
;
5091 struct imsm_super
*mpb
= super
->anchor
;
5096 case update_create_array
: {
5097 struct imsm_update_create_array
*u
= (void *) update
->buf
;
5098 struct intel_dev
*dv
;
5099 struct imsm_dev
*dev
= &u
->dev
;
5100 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5102 struct disk_info
*inf
;
5106 inf
= get_disk_info(u
);
5107 len
= sizeof_imsm_dev(dev
, 1);
5108 /* allocate a new super->devlist entry */
5109 dv
= malloc(sizeof(*dv
));
5111 dv
->dev
= malloc(len
);
5116 update
->space
= NULL
;
5120 /* count how many spares will be converted to members */
5121 for (i
= 0; i
< map
->num_members
; i
++) {
5122 dl
= serial_to_dl(inf
[i
].serial
, super
);
5124 /* hmm maybe it failed?, nothing we can do about
5129 if (count_memberships(dl
, super
) == 0)
5132 len
+= activate
* sizeof(struct imsm_disk
);
5139 /* check if we need a larger metadata buffer */
5140 if (super
->next_buf
)
5141 buf_len
= super
->next_len
;
5143 buf_len
= super
->len
;
5145 if (__le32_to_cpu(mpb
->mpb_size
) + len
> buf_len
) {
5146 /* ok we need a larger buf than what is currently allocated
5147 * if this allocation fails process_update will notice that
5148 * ->next_len is set and ->next_buf is NULL
5150 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + len
, 512);
5151 if (super
->next_buf
)
5152 free(super
->next_buf
);
5154 super
->next_len
= buf_len
;
5155 if (posix_memalign(&super
->next_buf
, 512, buf_len
) == 0)
5156 memset(super
->next_buf
, 0, buf_len
);
5158 super
->next_buf
= NULL
;
5162 /* must be called while manager is quiesced */
5163 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, int index
)
5165 struct imsm_super
*mpb
= super
->anchor
;
5167 struct imsm_dev
*dev
;
5168 struct imsm_map
*map
;
5169 int i
, j
, num_members
;
5172 dprintf("%s: deleting device[%d] from imsm_super\n",
5175 /* shift all indexes down one */
5176 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
5177 if (iter
->index
> index
)
5179 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
5180 if (iter
->index
> index
)
5183 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5184 dev
= get_imsm_dev(super
, i
);
5185 map
= get_imsm_map(dev
, 0);
5186 num_members
= map
->num_members
;
5187 for (j
= 0; j
< num_members
; j
++) {
5188 /* update ord entries being careful not to propagate
5189 * ord-flags to the first map
5191 ord
= get_imsm_ord_tbl_ent(dev
, j
);
5193 if (ord_to_idx(ord
) <= index
)
5196 map
= get_imsm_map(dev
, 0);
5197 set_imsm_ord_tbl_ent(map
, j
, ord_to_idx(ord
- 1));
5198 map
= get_imsm_map(dev
, 1);
5200 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
5205 super
->updates_pending
++;
5207 struct dl
*dl
= *dlp
;
5209 *dlp
= (*dlp
)->next
;
5210 __free_imsm_disk(dl
);
5213 #endif /* MDASSEMBLE */
5215 struct superswitch super_imsm
= {
5217 .examine_super
= examine_super_imsm
,
5218 .brief_examine_super
= brief_examine_super_imsm
,
5219 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
5220 .export_examine_super
= export_examine_super_imsm
,
5221 .detail_super
= detail_super_imsm
,
5222 .brief_detail_super
= brief_detail_super_imsm
,
5223 .write_init_super
= write_init_super_imsm
,
5224 .validate_geometry
= validate_geometry_imsm
,
5225 .add_to_super
= add_to_super_imsm
,
5226 .detail_platform
= detail_platform_imsm
,
5228 .match_home
= match_home_imsm
,
5229 .uuid_from_super
= uuid_from_super_imsm
,
5230 .getinfo_super
= getinfo_super_imsm
,
5231 .update_super
= update_super_imsm
,
5233 .avail_size
= avail_size_imsm
,
5235 .compare_super
= compare_super_imsm
,
5237 .load_super
= load_super_imsm
,
5238 .init_super
= init_super_imsm
,
5239 .store_super
= store_super_imsm
,
5240 .free_super
= free_super_imsm
,
5241 .match_metadata_desc
= match_metadata_desc_imsm
,
5242 .container_content
= container_content_imsm
,
5243 .default_layout
= imsm_level_to_layout
,
5250 .open_new
= imsm_open_new
,
5251 .load_super
= load_super_imsm
,
5252 .set_array_state
= imsm_set_array_state
,
5253 .set_disk
= imsm_set_disk
,
5254 .sync_metadata
= imsm_sync_metadata
,
5255 .activate_spare
= imsm_activate_spare
,
5256 .process_update
= imsm_process_update
,
5257 .prepare_update
= imsm_prepare_update
,
5258 #endif /* MDASSEMBLE */