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 current_vol
; /* index of raid device undergoing creation */
247 __u32 create_offset
; /* common start for 'current_vol' */
248 __u32 random
; /* random data for seeding new family numbers */
249 struct intel_dev
*devlist
;
253 __u8 serial
[MAX_RAID_SERIAL_LEN
];
256 struct imsm_disk disk
;
259 struct extent
*e
; /* for determining freespace @ create */
260 int raiddisk
; /* slot to fill in autolayout */
262 struct dl
*add
; /* list of disks to add while mdmon active */
263 struct dl
*missing
; /* disks removed while we weren't looking */
264 struct bbm_log
*bbm_log
;
265 const char *hba
; /* device path of the raid controller for this metadata */
266 const struct imsm_orom
*orom
; /* platform firmware support */
267 struct intel_super
*next
; /* (temp) list for disambiguating family_num */
271 struct imsm_disk disk
;
272 #define IMSM_UNKNOWN_OWNER (-1)
274 struct intel_disk
*next
;
278 unsigned long long start
, size
;
281 /* definition of messages passed to imsm_process_update */
282 enum imsm_update_type
{
283 update_activate_spare
,
288 struct imsm_update_activate_spare
{
289 enum imsm_update_type type
;
293 struct imsm_update_activate_spare
*next
;
297 __u8 serial
[MAX_RAID_SERIAL_LEN
];
300 struct imsm_update_create_array
{
301 enum imsm_update_type type
;
306 struct imsm_update_add_disk
{
307 enum imsm_update_type type
;
310 static struct supertype
*match_metadata_desc_imsm(char *arg
)
312 struct supertype
*st
;
314 if (strcmp(arg
, "imsm") != 0 &&
315 strcmp(arg
, "default") != 0
319 st
= malloc(sizeof(*st
));
322 memset(st
, 0, sizeof(*st
));
323 st
->ss
= &super_imsm
;
324 st
->max_devs
= IMSM_MAX_DEVICES
;
325 st
->minor_version
= 0;
331 static __u8
*get_imsm_version(struct imsm_super
*mpb
)
333 return &mpb
->sig
[MPB_SIG_LEN
];
337 /* retrieve a disk directly from the anchor when the anchor is known to be
338 * up-to-date, currently only at load time
340 static struct imsm_disk
*__get_imsm_disk(struct imsm_super
*mpb
, __u8 index
)
342 if (index
>= mpb
->num_disks
)
344 return &mpb
->disk
[index
];
348 /* retrieve a disk from the parsed metadata */
349 static struct imsm_disk
*get_imsm_disk(struct intel_super
*super
, __u8 index
)
353 for (d
= super
->disks
; d
; d
= d
->next
)
354 if (d
->index
== index
)
361 /* generate a checksum directly from the anchor when the anchor is known to be
362 * up-to-date, currently only at load or write_super after coalescing
364 static __u32
__gen_imsm_checksum(struct imsm_super
*mpb
)
366 __u32 end
= mpb
->mpb_size
/ sizeof(end
);
367 __u32
*p
= (__u32
*) mpb
;
371 sum
+= __le32_to_cpu(*p
);
375 return sum
- __le32_to_cpu(mpb
->check_sum
);
378 static size_t sizeof_imsm_map(struct imsm_map
*map
)
380 return sizeof(struct imsm_map
) + sizeof(__u32
) * (map
->num_members
- 1);
383 struct imsm_map
*get_imsm_map(struct imsm_dev
*dev
, int second_map
)
385 struct imsm_map
*map
= &dev
->vol
.map
[0];
387 if (second_map
&& !dev
->vol
.migr_state
)
389 else if (second_map
) {
392 return ptr
+ sizeof_imsm_map(map
);
398 /* return the size of the device.
399 * migr_state increases the returned size if map[0] were to be duplicated
401 static size_t sizeof_imsm_dev(struct imsm_dev
*dev
, int migr_state
)
403 size_t size
= sizeof(*dev
) - sizeof(struct imsm_map
) +
404 sizeof_imsm_map(get_imsm_map(dev
, 0));
406 /* migrating means an additional map */
407 if (dev
->vol
.migr_state
)
408 size
+= sizeof_imsm_map(get_imsm_map(dev
, 1));
410 size
+= sizeof_imsm_map(get_imsm_map(dev
, 0));
416 /* retrieve disk serial number list from a metadata update */
417 static struct disk_info
*get_disk_info(struct imsm_update_create_array
*update
)
420 struct disk_info
*inf
;
422 inf
= u
+ sizeof(*update
) - sizeof(struct imsm_dev
) +
423 sizeof_imsm_dev(&update
->dev
, 0);
429 static struct imsm_dev
*__get_imsm_dev(struct imsm_super
*mpb
, __u8 index
)
435 if (index
>= mpb
->num_raid_devs
)
438 /* devices start after all disks */
439 offset
= ((void *) &mpb
->disk
[mpb
->num_disks
]) - _mpb
;
441 for (i
= 0; i
<= index
; i
++)
443 return _mpb
+ offset
;
445 offset
+= sizeof_imsm_dev(_mpb
+ offset
, 0);
450 static struct imsm_dev
*get_imsm_dev(struct intel_super
*super
, __u8 index
)
452 struct intel_dev
*dv
;
454 if (index
>= super
->anchor
->num_raid_devs
)
456 for (dv
= super
->devlist
; dv
; dv
= dv
->next
)
457 if (dv
->index
== index
)
462 static __u32
get_imsm_ord_tbl_ent(struct imsm_dev
*dev
, int slot
)
464 struct imsm_map
*map
;
466 if (dev
->vol
.migr_state
)
467 map
= get_imsm_map(dev
, 1);
469 map
= get_imsm_map(dev
, 0);
471 /* top byte identifies disk under rebuild */
472 return __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
475 #define ord_to_idx(ord) (((ord) << 8) >> 8)
476 static __u32
get_imsm_disk_idx(struct imsm_dev
*dev
, int slot
)
478 __u32 ord
= get_imsm_ord_tbl_ent(dev
, slot
);
480 return ord_to_idx(ord
);
483 static void set_imsm_ord_tbl_ent(struct imsm_map
*map
, int slot
, __u32 ord
)
485 map
->disk_ord_tbl
[slot
] = __cpu_to_le32(ord
);
488 static int get_imsm_disk_slot(struct imsm_map
*map
, int idx
)
493 for (slot
= 0; slot
< map
->num_members
; slot
++) {
494 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
495 if (ord_to_idx(ord
) == idx
)
502 static int get_imsm_raid_level(struct imsm_map
*map
)
504 if (map
->raid_level
== 1) {
505 if (map
->num_members
== 2)
511 return map
->raid_level
;
514 static int cmp_extent(const void *av
, const void *bv
)
516 const struct extent
*a
= av
;
517 const struct extent
*b
= bv
;
518 if (a
->start
< b
->start
)
520 if (a
->start
> b
->start
)
525 static int count_memberships(struct dl
*dl
, struct intel_super
*super
)
530 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
531 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
532 struct imsm_map
*map
= get_imsm_map(dev
, 0);
534 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
541 static struct extent
*get_extents(struct intel_super
*super
, struct dl
*dl
)
543 /* find a list of used extents on the given physical device */
544 struct extent
*rv
, *e
;
546 int memberships
= count_memberships(dl
, super
);
547 __u32 reservation
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
549 rv
= malloc(sizeof(struct extent
) * (memberships
+ 1));
554 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
555 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
556 struct imsm_map
*map
= get_imsm_map(dev
, 0);
558 if (get_imsm_disk_slot(map
, dl
->index
) >= 0) {
559 e
->start
= __le32_to_cpu(map
->pba_of_lba0
);
560 e
->size
= __le32_to_cpu(map
->blocks_per_member
);
564 qsort(rv
, memberships
, sizeof(*rv
), cmp_extent
);
566 /* determine the start of the metadata
567 * when no raid devices are defined use the default
568 * ...otherwise allow the metadata to truncate the value
569 * as is the case with older versions of imsm
572 struct extent
*last
= &rv
[memberships
- 1];
575 remainder
= __le32_to_cpu(dl
->disk
.total_blocks
) -
576 (last
->start
+ last
->size
);
577 /* round down to 1k block to satisfy precision of the kernel
581 /* make sure remainder is still sane */
582 if (remainder
< ROUND_UP(super
->len
, 512) >> 9)
583 remainder
= ROUND_UP(super
->len
, 512) >> 9;
584 if (reservation
> remainder
)
585 reservation
= remainder
;
587 e
->start
= __le32_to_cpu(dl
->disk
.total_blocks
) - reservation
;
592 /* try to determine how much space is reserved for metadata from
593 * the last get_extents() entry, otherwise fallback to the
596 static __u32
imsm_reserved_sectors(struct intel_super
*super
, struct dl
*dl
)
602 /* for spares just return a minimal reservation which will grow
603 * once the spare is picked up by an array
606 return MPB_SECTOR_CNT
;
608 e
= get_extents(super
, dl
);
610 return MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
612 /* scroll to last entry */
613 for (i
= 0; e
[i
].size
; i
++)
616 rv
= __le32_to_cpu(dl
->disk
.total_blocks
) - e
[i
].start
;
623 static int is_spare(struct imsm_disk
*disk
)
625 return (disk
->status
& SPARE_DISK
) == SPARE_DISK
;
628 static int is_configured(struct imsm_disk
*disk
)
630 return (disk
->status
& CONFIGURED_DISK
) == CONFIGURED_DISK
;
633 static int is_failed(struct imsm_disk
*disk
)
635 return (disk
->status
& FAILED_DISK
) == FAILED_DISK
;
639 static __u64
blocks_per_migr_unit(struct imsm_dev
*dev
);
641 static void print_imsm_dev(struct imsm_dev
*dev
, char *uuid
, int disk_idx
)
645 struct imsm_map
*map
= get_imsm_map(dev
, 0);
649 printf("[%.16s]:\n", dev
->volume
);
650 printf(" UUID : %s\n", uuid
);
651 printf(" RAID Level : %d\n", get_imsm_raid_level(map
));
652 printf(" Members : %d\n", map
->num_members
);
653 slot
= get_imsm_disk_slot(map
, disk_idx
);
655 ord
= get_imsm_ord_tbl_ent(dev
, slot
);
656 printf(" This Slot : %d%s\n", slot
,
657 ord
& IMSM_ORD_REBUILD
? " (out-of-sync)" : "");
659 printf(" This Slot : ?\n");
660 sz
= __le32_to_cpu(dev
->size_high
);
662 sz
+= __le32_to_cpu(dev
->size_low
);
663 printf(" Array Size : %llu%s\n", (unsigned long long)sz
,
664 human_size(sz
* 512));
665 sz
= __le32_to_cpu(map
->blocks_per_member
);
666 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz
,
667 human_size(sz
* 512));
668 printf(" Sector Offset : %u\n",
669 __le32_to_cpu(map
->pba_of_lba0
));
670 printf(" Num Stripes : %u\n",
671 __le32_to_cpu(map
->num_data_stripes
));
672 printf(" Chunk Size : %u KiB\n",
673 __le16_to_cpu(map
->blocks_per_strip
) / 2);
674 printf(" Reserved : %d\n", __le32_to_cpu(dev
->reserved_blocks
));
675 printf(" Migrate State : ");
676 if (dev
->vol
.migr_state
) {
677 if (migr_type(dev
) == MIGR_INIT
)
678 printf("initialize\n");
679 else if (migr_type(dev
) == MIGR_REBUILD
)
681 else if (migr_type(dev
) == MIGR_VERIFY
)
683 else if (migr_type(dev
) == MIGR_GEN_MIGR
)
684 printf("general migration\n");
685 else if (migr_type(dev
) == MIGR_STATE_CHANGE
)
686 printf("state change\n");
687 else if (migr_type(dev
) == MIGR_REPAIR
)
690 printf("<unknown:%d>\n", migr_type(dev
));
693 printf(" Map State : %s", map_state_str
[map
->map_state
]);
694 if (dev
->vol
.migr_state
) {
695 struct imsm_map
*map
= get_imsm_map(dev
, 1);
697 printf(" <-- %s", map_state_str
[map
->map_state
]);
698 printf("\n Checkpoint : %u (%llu)",
699 __le32_to_cpu(dev
->vol
.curr_migr_unit
),
700 (unsigned long long)blocks_per_migr_unit(dev
));
703 printf(" Dirty State : %s\n", dev
->vol
.dirty
? "dirty" : "clean");
706 static void print_imsm_disk(struct imsm_super
*mpb
, int index
, __u32 reserved
)
708 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, index
);
709 char str
[MAX_RAID_SERIAL_LEN
+ 1];
712 if (index
< 0 || !disk
)
716 snprintf(str
, MAX_RAID_SERIAL_LEN
+ 1, "%s", disk
->serial
);
717 printf(" Disk%02d Serial : %s\n", index
, str
);
718 printf(" State :%s%s%s\n", is_spare(disk
) ? " spare" : "",
719 is_configured(disk
) ? " active" : "",
720 is_failed(disk
) ? " failed" : "");
721 printf(" Id : %08x\n", __le32_to_cpu(disk
->scsi_id
));
722 sz
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
723 printf(" Usable Size : %llu%s\n", (unsigned long long)sz
,
724 human_size(sz
* 512));
727 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
);
729 static void examine_super_imsm(struct supertype
*st
, char *homehost
)
731 struct intel_super
*super
= st
->sb
;
732 struct imsm_super
*mpb
= super
->anchor
;
733 char str
[MAX_SIGNATURE_LENGTH
];
738 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
741 snprintf(str
, MPB_SIG_LEN
, "%s", mpb
->sig
);
742 printf(" Magic : %s\n", str
);
743 snprintf(str
, strlen(MPB_VERSION_RAID0
), "%s", get_imsm_version(mpb
));
744 printf(" Version : %s\n", get_imsm_version(mpb
));
745 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb
->orig_family_num
));
746 printf(" Family : %08x\n", __le32_to_cpu(mpb
->family_num
));
747 printf(" Generation : %08x\n", __le32_to_cpu(mpb
->generation_num
));
748 getinfo_super_imsm(st
, &info
);
749 fname_from_uuid(st
, &info
, nbuf
, ':');
750 printf(" UUID : %s\n", nbuf
+ 5);
751 sum
= __le32_to_cpu(mpb
->check_sum
);
752 printf(" Checksum : %08x %s\n", sum
,
753 __gen_imsm_checksum(mpb
) == sum
? "correct" : "incorrect");
754 printf(" MPB Sectors : %d\n", mpb_sectors(mpb
));
755 printf(" Disks : %d\n", mpb
->num_disks
);
756 printf(" RAID Devices : %d\n", mpb
->num_raid_devs
);
757 print_imsm_disk(mpb
, super
->disks
->index
, reserved
);
758 if (super
->bbm_log
) {
759 struct bbm_log
*log
= super
->bbm_log
;
762 printf("Bad Block Management Log:\n");
763 printf(" Log Size : %d\n", __le32_to_cpu(mpb
->bbm_log_size
));
764 printf(" Signature : %x\n", __le32_to_cpu(log
->signature
));
765 printf(" Entry Count : %d\n", __le32_to_cpu(log
->entry_count
));
766 printf(" Spare Blocks : %d\n", __le32_to_cpu(log
->reserved_spare_block_count
));
767 printf(" First Spare : %llx\n",
768 (unsigned long long) __le64_to_cpu(log
->first_spare_lba
));
770 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
772 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
774 super
->current_vol
= i
;
775 getinfo_super_imsm(st
, &info
);
776 fname_from_uuid(st
, &info
, nbuf
, ':');
777 print_imsm_dev(dev
, nbuf
+ 5, super
->disks
->index
);
779 for (i
= 0; i
< mpb
->num_disks
; i
++) {
780 if (i
== super
->disks
->index
)
782 print_imsm_disk(mpb
, i
, reserved
);
786 static void brief_examine_super_imsm(struct supertype
*st
, int verbose
)
788 /* We just write a generic IMSM ARRAY entry */
791 struct intel_super
*super
= st
->sb
;
793 if (!super
->anchor
->num_raid_devs
) {
794 printf("ARRAY metadata=imsm\n");
798 getinfo_super_imsm(st
, &info
);
799 fname_from_uuid(st
, &info
, nbuf
, ':');
800 printf("ARRAY metadata=imsm UUID=%s\n", nbuf
+ 5);
803 static void brief_examine_subarrays_imsm(struct supertype
*st
, int verbose
)
805 /* We just write a generic IMSM ARRAY entry */
809 struct intel_super
*super
= st
->sb
;
812 if (!super
->anchor
->num_raid_devs
)
815 getinfo_super_imsm(st
, &info
);
816 fname_from_uuid(st
, &info
, nbuf
, ':');
817 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
818 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
820 super
->current_vol
= i
;
821 getinfo_super_imsm(st
, &info
);
822 fname_from_uuid(st
, &info
, nbuf1
, ':');
823 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
824 dev
->volume
, nbuf
+ 5, i
, nbuf1
+ 5);
828 static void export_examine_super_imsm(struct supertype
*st
)
830 struct intel_super
*super
= st
->sb
;
831 struct imsm_super
*mpb
= super
->anchor
;
835 getinfo_super_imsm(st
, &info
);
836 fname_from_uuid(st
, &info
, nbuf
, ':');
837 printf("MD_METADATA=imsm\n");
838 printf("MD_LEVEL=container\n");
839 printf("MD_UUID=%s\n", nbuf
+5);
840 printf("MD_DEVICES=%u\n", mpb
->num_disks
);
843 static void detail_super_imsm(struct supertype
*st
, char *homehost
)
848 getinfo_super_imsm(st
, &info
);
849 fname_from_uuid(st
, &info
, nbuf
, ':');
850 printf("\n UUID : %s\n", nbuf
+ 5);
853 static void brief_detail_super_imsm(struct supertype
*st
)
857 getinfo_super_imsm(st
, &info
);
858 fname_from_uuid(st
, &info
, nbuf
, ':');
859 printf(" UUID=%s", nbuf
+ 5);
862 static int imsm_read_serial(int fd
, char *devname
, __u8
*serial
);
863 static void fd2devname(int fd
, char *name
);
865 static int imsm_enumerate_ports(const char *hba_path
, int port_count
, int host_base
, int verbose
)
867 /* dump an unsorted list of devices attached to ahci, as well as
868 * non-connected ports
870 int hba_len
= strlen(hba_path
) + 1;
875 unsigned long port_mask
= (1 << port_count
) - 1;
877 if (port_count
> sizeof(port_mask
) * 8) {
879 fprintf(stderr
, Name
": port_count %d out of range\n", port_count
);
883 /* scroll through /sys/dev/block looking for devices attached to
886 dir
= opendir("/sys/dev/block");
887 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
898 if (sscanf(ent
->d_name
, "%d:%d", &major
, &minor
) != 2)
900 path
= devt_to_devpath(makedev(major
, minor
));
903 if (!path_attached_to_hba(path
, hba_path
)) {
909 /* retrieve the scsi device type */
910 if (asprintf(&device
, "/sys/dev/block/%d:%d/device/xxxxxxx", major
, minor
) < 0) {
912 fprintf(stderr
, Name
": failed to allocate 'device'\n");
916 sprintf(device
, "/sys/dev/block/%d:%d/device/type", major
, minor
);
917 if (load_sys(device
, buf
) != 0) {
919 fprintf(stderr
, Name
": failed to read device type for %s\n",
925 type
= strtoul(buf
, NULL
, 10);
927 /* if it's not a disk print the vendor and model */
928 if (!(type
== 0 || type
== 7 || type
== 14)) {
931 sprintf(device
, "/sys/dev/block/%d:%d/device/vendor", major
, minor
);
932 if (load_sys(device
, buf
) == 0) {
933 strncpy(vendor
, buf
, sizeof(vendor
));
934 vendor
[sizeof(vendor
) - 1] = '\0';
935 c
= (char *) &vendor
[sizeof(vendor
) - 1];
936 while (isspace(*c
) || *c
== '\0')
940 sprintf(device
, "/sys/dev/block/%d:%d/device/model", major
, minor
);
941 if (load_sys(device
, buf
) == 0) {
942 strncpy(model
, buf
, sizeof(model
));
943 model
[sizeof(model
) - 1] = '\0';
944 c
= (char *) &model
[sizeof(model
) - 1];
945 while (isspace(*c
) || *c
== '\0')
949 if (vendor
[0] && model
[0])
950 sprintf(buf
, "%.64s %.64s", vendor
, model
);
952 switch (type
) { /* numbers from hald/linux/device.c */
953 case 1: sprintf(buf
, "tape"); break;
954 case 2: sprintf(buf
, "printer"); break;
955 case 3: sprintf(buf
, "processor"); break;
957 case 5: sprintf(buf
, "cdrom"); break;
958 case 6: sprintf(buf
, "scanner"); break;
959 case 8: sprintf(buf
, "media_changer"); break;
960 case 9: sprintf(buf
, "comm"); break;
961 case 12: sprintf(buf
, "raid"); break;
962 default: sprintf(buf
, "unknown");
968 /* chop device path to 'host%d' and calculate the port number */
969 c
= strchr(&path
[hba_len
], '/');
972 fprintf(stderr
, Name
": %s - invalid path name\n", path
+ hba_len
);
977 if (sscanf(&path
[hba_len
], "host%d", &port
) == 1)
981 *c
= '/'; /* repair the full string */
982 fprintf(stderr
, Name
": failed to determine port number for %s\n",
989 /* mark this port as used */
990 port_mask
&= ~(1 << port
);
992 /* print out the device information */
994 printf(" Port%d : - non-disk device (%s) -\n", port
, buf
);
998 fd
= dev_open(ent
->d_name
, O_RDONLY
);
1000 printf(" Port%d : - disk info unavailable -\n", port
);
1002 fd2devname(fd
, buf
);
1003 printf(" Port%d : %s", port
, buf
);
1004 if (imsm_read_serial(fd
, NULL
, (__u8
*) buf
) == 0)
1005 printf(" (%s)\n", buf
);
1020 for (i
= 0; i
< port_count
; i
++)
1021 if (port_mask
& (1 << i
))
1022 printf(" Port%d : - no device attached -\n", i
);
1028 static int detail_platform_imsm(int verbose
, int enumerate_only
)
1030 /* There are two components to imsm platform support, the ahci SATA
1031 * controller and the option-rom. To find the SATA controller we
1032 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
1033 * controller with the Intel vendor id is present. This approach
1034 * allows mdadm to leverage the kernel's ahci detection logic, with the
1035 * caveat that if ahci.ko is not loaded mdadm will not be able to
1036 * detect platform raid capabilities. The option-rom resides in a
1037 * platform "Adapter ROM". We scan for its signature to retrieve the
1038 * platform capabilities. If raid support is disabled in the BIOS the
1039 * option-rom capability structure will not be available.
1041 const struct imsm_orom
*orom
;
1042 struct sys_dev
*list
, *hba
;
1045 const char *hba_path
;
1049 if (enumerate_only
) {
1050 if (check_env("IMSM_NO_PLATFORM") || find_imsm_orom())
1055 list
= find_driver_devices("pci", "ahci");
1056 for (hba
= list
; hba
; hba
= hba
->next
)
1057 if (devpath_to_vendor(hba
->path
) == 0x8086)
1062 fprintf(stderr
, Name
": unable to find active ahci controller\n");
1063 free_sys_dev(&list
);
1066 fprintf(stderr
, Name
": found Intel SATA AHCI Controller\n");
1067 hba_path
= hba
->path
;
1069 free_sys_dev(&list
);
1071 orom
= find_imsm_orom();
1074 fprintf(stderr
, Name
": imsm option-rom not found\n");
1078 printf(" Platform : Intel(R) Matrix Storage Manager\n");
1079 printf(" Version : %d.%d.%d.%d\n", orom
->major_ver
, orom
->minor_ver
,
1080 orom
->hotfix_ver
, orom
->build
);
1081 printf(" RAID Levels :%s%s%s%s%s\n",
1082 imsm_orom_has_raid0(orom
) ? " raid0" : "",
1083 imsm_orom_has_raid1(orom
) ? " raid1" : "",
1084 imsm_orom_has_raid1e(orom
) ? " raid1e" : "",
1085 imsm_orom_has_raid10(orom
) ? " raid10" : "",
1086 imsm_orom_has_raid5(orom
) ? " raid5" : "");
1087 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1088 imsm_orom_has_chunk(orom
, 2) ? " 2k" : "",
1089 imsm_orom_has_chunk(orom
, 4) ? " 4k" : "",
1090 imsm_orom_has_chunk(orom
, 8) ? " 8k" : "",
1091 imsm_orom_has_chunk(orom
, 16) ? " 16k" : "",
1092 imsm_orom_has_chunk(orom
, 32) ? " 32k" : "",
1093 imsm_orom_has_chunk(orom
, 64) ? " 64k" : "",
1094 imsm_orom_has_chunk(orom
, 128) ? " 128k" : "",
1095 imsm_orom_has_chunk(orom
, 256) ? " 256k" : "",
1096 imsm_orom_has_chunk(orom
, 512) ? " 512k" : "",
1097 imsm_orom_has_chunk(orom
, 1024*1) ? " 1M" : "",
1098 imsm_orom_has_chunk(orom
, 1024*2) ? " 2M" : "",
1099 imsm_orom_has_chunk(orom
, 1024*4) ? " 4M" : "",
1100 imsm_orom_has_chunk(orom
, 1024*8) ? " 8M" : "",
1101 imsm_orom_has_chunk(orom
, 1024*16) ? " 16M" : "",
1102 imsm_orom_has_chunk(orom
, 1024*32) ? " 32M" : "",
1103 imsm_orom_has_chunk(orom
, 1024*64) ? " 64M" : "");
1104 printf(" Max Disks : %d\n", orom
->tds
);
1105 printf(" Max Volumes : %d\n", orom
->vpa
);
1106 printf(" I/O Controller : %s\n", hba_path
);
1108 /* find the smallest scsi host number to determine a port number base */
1109 dir
= opendir(hba_path
);
1110 for (ent
= dir
? readdir(dir
) : NULL
; ent
; ent
= readdir(dir
)) {
1113 if (sscanf(ent
->d_name
, "host%d", &host
) != 1)
1115 if (port_count
== 0)
1117 else if (host
< host_base
)
1120 if (host
+ 1 > port_count
+ host_base
)
1121 port_count
= host
+ 1 - host_base
;
1127 if (!port_count
|| imsm_enumerate_ports(hba_path
, port_count
,
1128 host_base
, verbose
) != 0) {
1130 fprintf(stderr
, Name
": failed to enumerate ports\n");
1138 static int match_home_imsm(struct supertype
*st
, char *homehost
)
1140 /* the imsm metadata format does not specify any host
1141 * identification information. We return -1 since we can never
1142 * confirm nor deny whether a given array is "meant" for this
1143 * host. We rely on compare_super and the 'family_num' fields to
1144 * exclude member disks that do not belong, and we rely on
1145 * mdadm.conf to specify the arrays that should be assembled.
1146 * Auto-assembly may still pick up "foreign" arrays.
1152 static void uuid_from_super_imsm(struct supertype
*st
, int uuid
[4])
1154 /* The uuid returned here is used for:
1155 * uuid to put into bitmap file (Create, Grow)
1156 * uuid for backup header when saving critical section (Grow)
1157 * comparing uuids when re-adding a device into an array
1158 * In these cases the uuid required is that of the data-array,
1159 * not the device-set.
1160 * uuid to recognise same set when adding a missing device back
1161 * to an array. This is a uuid for the device-set.
1163 * For each of these we can make do with a truncated
1164 * or hashed uuid rather than the original, as long as
1166 * In each case the uuid required is that of the data-array,
1167 * not the device-set.
1169 /* imsm does not track uuid's so we synthesis one using sha1 on
1170 * - The signature (Which is constant for all imsm array, but no matter)
1171 * - the orig_family_num of the container
1172 * - the index number of the volume
1173 * - the 'serial' number of the volume.
1174 * Hopefully these are all constant.
1176 struct intel_super
*super
= st
->sb
;
1179 struct sha1_ctx ctx
;
1180 struct imsm_dev
*dev
= NULL
;
1183 /* some mdadm versions failed to set ->orig_family_num, in which
1184 * case fall back to ->family_num. orig_family_num will be
1185 * fixed up with the first metadata update.
1187 family_num
= super
->anchor
->orig_family_num
;
1188 if (family_num
== 0)
1189 family_num
= super
->anchor
->family_num
;
1190 sha1_init_ctx(&ctx
);
1191 sha1_process_bytes(super
->anchor
->sig
, MPB_SIG_LEN
, &ctx
);
1192 sha1_process_bytes(&family_num
, sizeof(__u32
), &ctx
);
1193 if (super
->current_vol
>= 0)
1194 dev
= get_imsm_dev(super
, super
->current_vol
);
1196 __u32 vol
= super
->current_vol
;
1197 sha1_process_bytes(&vol
, sizeof(vol
), &ctx
);
1198 sha1_process_bytes(dev
->volume
, MAX_RAID_SERIAL_LEN
, &ctx
);
1200 sha1_finish_ctx(&ctx
, buf
);
1201 memcpy(uuid
, buf
, 4*4);
1206 get_imsm_numerical_version(struct imsm_super
*mpb
, int *m
, int *p
)
1208 __u8
*v
= get_imsm_version(mpb
);
1209 __u8
*end
= mpb
->sig
+ MAX_SIGNATURE_LENGTH
;
1210 char major
[] = { 0, 0, 0 };
1211 char minor
[] = { 0 ,0, 0 };
1212 char patch
[] = { 0, 0, 0 };
1213 char *ver_parse
[] = { major
, minor
, patch
};
1217 while (*v
!= '\0' && v
< end
) {
1218 if (*v
!= '.' && j
< 2)
1219 ver_parse
[i
][j
++] = *v
;
1227 *m
= strtol(minor
, NULL
, 0);
1228 *p
= strtol(patch
, NULL
, 0);
1232 static __u32
migr_strip_blocks_resync(struct imsm_dev
*dev
)
1234 /* migr_strip_size when repairing or initializing parity */
1235 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1236 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1238 switch (get_imsm_raid_level(map
)) {
1243 return 128*1024 >> 9;
1247 static __u32
migr_strip_blocks_rebuild(struct imsm_dev
*dev
)
1249 /* migr_strip_size when rebuilding a degraded disk, no idea why
1250 * this is different than migr_strip_size_resync(), but it's good
1253 struct imsm_map
*map
= get_imsm_map(dev
, 1);
1254 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1256 switch (get_imsm_raid_level(map
)) {
1259 if (map
->num_members
% map
->num_domains
== 0)
1260 return 128*1024 >> 9;
1264 return max((__u32
) 64*1024 >> 9, chunk
);
1266 return 128*1024 >> 9;
1270 static __u32
num_stripes_per_unit_resync(struct imsm_dev
*dev
)
1272 struct imsm_map
*lo
= get_imsm_map(dev
, 0);
1273 struct imsm_map
*hi
= get_imsm_map(dev
, 1);
1274 __u32 lo_chunk
= __le32_to_cpu(lo
->blocks_per_strip
);
1275 __u32 hi_chunk
= __le32_to_cpu(hi
->blocks_per_strip
);
1277 return max((__u32
) 1, hi_chunk
/ lo_chunk
);
1280 static __u32
num_stripes_per_unit_rebuild(struct imsm_dev
*dev
)
1282 struct imsm_map
*lo
= get_imsm_map(dev
, 0);
1283 int level
= get_imsm_raid_level(lo
);
1285 if (level
== 1 || level
== 10) {
1286 struct imsm_map
*hi
= get_imsm_map(dev
, 1);
1288 return hi
->num_domains
;
1290 return num_stripes_per_unit_resync(dev
);
1293 static __u8
imsm_num_data_members(struct imsm_dev
*dev
)
1295 /* named 'imsm_' because raid0, raid1 and raid10
1296 * counter-intuitively have the same number of data disks
1298 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1300 switch (get_imsm_raid_level(map
)) {
1304 return map
->num_members
;
1306 return map
->num_members
- 1;
1308 dprintf("%s: unsupported raid level\n", __func__
);
1313 static __u32
parity_segment_depth(struct imsm_dev
*dev
)
1315 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1316 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1318 switch(get_imsm_raid_level(map
)) {
1321 return chunk
* map
->num_domains
;
1323 return chunk
* map
->num_members
;
1329 static __u32
map_migr_block(struct imsm_dev
*dev
, __u32 block
)
1331 struct imsm_map
*map
= get_imsm_map(dev
, 1);
1332 __u32 chunk
= __le32_to_cpu(map
->blocks_per_strip
);
1333 __u32 strip
= block
/ chunk
;
1335 switch (get_imsm_raid_level(map
)) {
1338 __u32 vol_strip
= (strip
* map
->num_domains
) + 1;
1339 __u32 vol_stripe
= vol_strip
/ map
->num_members
;
1341 return vol_stripe
* chunk
+ block
% chunk
;
1343 __u32 stripe
= strip
/ (map
->num_members
- 1);
1345 return stripe
* chunk
+ block
% chunk
;
1352 static __u64
blocks_per_migr_unit(struct imsm_dev
*dev
)
1354 /* calculate the conversion factor between per member 'blocks'
1355 * (md/{resync,rebuild}_start) and imsm migration units, return
1356 * 0 for the 'not migrating' and 'unsupported migration' cases
1358 if (!dev
->vol
.migr_state
)
1361 switch (migr_type(dev
)) {
1365 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1366 __u32 stripes_per_unit
;
1367 __u32 blocks_per_unit
;
1376 /* yes, this is really the translation of migr_units to
1377 * per-member blocks in the 'resync' case
1379 stripes_per_unit
= num_stripes_per_unit_resync(dev
);
1380 migr_chunk
= migr_strip_blocks_resync(dev
);
1381 disks
= imsm_num_data_members(dev
);
1382 blocks_per_unit
= stripes_per_unit
* migr_chunk
* disks
;
1383 stripe
= __le32_to_cpu(map
->blocks_per_strip
) * disks
;
1384 segment
= blocks_per_unit
/ stripe
;
1385 block_rel
= blocks_per_unit
- segment
* stripe
;
1386 parity_depth
= parity_segment_depth(dev
);
1387 block_map
= map_migr_block(dev
, block_rel
);
1388 return block_map
+ parity_depth
* segment
;
1390 case MIGR_REBUILD
: {
1391 __u32 stripes_per_unit
;
1394 stripes_per_unit
= num_stripes_per_unit_rebuild(dev
);
1395 migr_chunk
= migr_strip_blocks_rebuild(dev
);
1396 return migr_chunk
* stripes_per_unit
;
1399 case MIGR_STATE_CHANGE
:
1405 static int imsm_level_to_layout(int level
)
1413 return ALGORITHM_LEFT_ASYMMETRIC
;
1420 static void getinfo_super_imsm_volume(struct supertype
*st
, struct mdinfo
*info
)
1422 struct intel_super
*super
= st
->sb
;
1423 struct imsm_dev
*dev
= get_imsm_dev(super
, super
->current_vol
);
1424 struct imsm_map
*map
= get_imsm_map(dev
, 0);
1428 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
1429 if (dl
->raiddisk
== info
->disk
.raid_disk
)
1431 info
->container_member
= super
->current_vol
;
1432 info
->array
.raid_disks
= map
->num_members
;
1433 info
->array
.level
= get_imsm_raid_level(map
);
1434 info
->array
.layout
= imsm_level_to_layout(info
->array
.level
);
1435 info
->array
.md_minor
= -1;
1436 info
->array
.ctime
= 0;
1437 info
->array
.utime
= 0;
1438 info
->array
.chunk_size
= __le16_to_cpu(map
->blocks_per_strip
) << 9;
1439 info
->array
.state
= !dev
->vol
.dirty
;
1440 info
->custom_array_size
= __le32_to_cpu(dev
->size_high
);
1441 info
->custom_array_size
<<= 32;
1442 info
->custom_array_size
|= __le32_to_cpu(dev
->size_low
);
1444 info
->disk
.major
= 0;
1445 info
->disk
.minor
= 0;
1447 info
->disk
.major
= dl
->major
;
1448 info
->disk
.minor
= dl
->minor
;
1451 info
->data_offset
= __le32_to_cpu(map
->pba_of_lba0
);
1452 info
->component_size
= __le32_to_cpu(map
->blocks_per_member
);
1453 memset(info
->uuid
, 0, sizeof(info
->uuid
));
1454 info
->recovery_start
= MaxSector
;
1455 info
->reshape_active
= 0;
1457 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
|| dev
->vol
.dirty
) {
1458 info
->resync_start
= 0;
1459 } else if (dev
->vol
.migr_state
) {
1460 switch (migr_type(dev
)) {
1463 __u64 blocks_per_unit
= blocks_per_migr_unit(dev
);
1464 __u64 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
1466 info
->resync_start
= blocks_per_unit
* units
;
1470 /* we could emulate the checkpointing of
1471 * 'sync_action=check' migrations, but for now
1472 * we just immediately complete them
1475 /* this is handled by container_content_imsm() */
1477 case MIGR_STATE_CHANGE
:
1478 /* FIXME handle other migrations */
1480 /* we are not dirty, so... */
1481 info
->resync_start
= MaxSector
;
1484 info
->resync_start
= MaxSector
;
1486 strncpy(info
->name
, (char *) dev
->volume
, MAX_RAID_SERIAL_LEN
);
1487 info
->name
[MAX_RAID_SERIAL_LEN
] = 0;
1489 info
->array
.major_version
= -1;
1490 info
->array
.minor_version
= -2;
1491 devname
= devnum2devname(st
->container_dev
);
1492 *info
->text_version
= '\0';
1494 sprintf(info
->text_version
, "/%s/%d", devname
, info
->container_member
);
1496 info
->safe_mode_delay
= 4000; /* 4 secs like the Matrix driver */
1497 uuid_from_super_imsm(st
, info
->uuid
);
1500 /* check the config file to see if we can return a real uuid for this spare */
1501 static void fixup_container_spare_uuid(struct mdinfo
*inf
)
1503 struct mddev_ident_s
*array_list
;
1505 if (inf
->array
.level
!= LEVEL_CONTAINER
||
1506 memcmp(inf
->uuid
, uuid_match_any
, sizeof(int[4])) != 0)
1509 array_list
= conf_get_ident(NULL
);
1511 for (; array_list
; array_list
= array_list
->next
) {
1512 if (array_list
->uuid_set
) {
1513 struct supertype
*_sst
; /* spare supertype */
1514 struct supertype
*_cst
; /* container supertype */
1516 _cst
= array_list
->st
;
1518 _sst
= _cst
->ss
->match_metadata_desc(inf
->text_version
);
1523 memcpy(inf
->uuid
, array_list
->uuid
, sizeof(int[4]));
1531 static void getinfo_super_imsm(struct supertype
*st
, struct mdinfo
*info
)
1533 struct intel_super
*super
= st
->sb
;
1534 struct imsm_disk
*disk
;
1536 if (super
->current_vol
>= 0) {
1537 getinfo_super_imsm_volume(st
, info
);
1541 /* Set raid_disks to zero so that Assemble will always pull in valid
1544 info
->array
.raid_disks
= 0;
1545 info
->array
.level
= LEVEL_CONTAINER
;
1546 info
->array
.layout
= 0;
1547 info
->array
.md_minor
= -1;
1548 info
->array
.ctime
= 0; /* N/A for imsm */
1549 info
->array
.utime
= 0;
1550 info
->array
.chunk_size
= 0;
1552 info
->disk
.major
= 0;
1553 info
->disk
.minor
= 0;
1554 info
->disk
.raid_disk
= -1;
1555 info
->reshape_active
= 0;
1556 info
->array
.major_version
= -1;
1557 info
->array
.minor_version
= -2;
1558 strcpy(info
->text_version
, "imsm");
1559 info
->safe_mode_delay
= 0;
1560 info
->disk
.number
= -1;
1561 info
->disk
.state
= 0;
1563 info
->recovery_start
= MaxSector
;
1566 __u32 reserved
= imsm_reserved_sectors(super
, super
->disks
);
1568 disk
= &super
->disks
->disk
;
1569 info
->data_offset
= __le32_to_cpu(disk
->total_blocks
) - reserved
;
1570 info
->component_size
= reserved
;
1571 info
->disk
.state
= is_configured(disk
) ? (1 << MD_DISK_ACTIVE
) : 0;
1572 /* we don't change info->disk.raid_disk here because
1573 * this state will be finalized in mdmon after we have
1574 * found the 'most fresh' version of the metadata
1576 info
->disk
.state
|= is_failed(disk
) ? (1 << MD_DISK_FAULTY
) : 0;
1577 info
->disk
.state
|= is_spare(disk
) ? 0 : (1 << MD_DISK_SYNC
);
1580 /* only call uuid_from_super_imsm when this disk is part of a populated container,
1581 * ->compare_super may have updated the 'num_raid_devs' field for spares
1583 if (info
->disk
.state
& (1 << MD_DISK_SYNC
) || super
->anchor
->num_raid_devs
)
1584 uuid_from_super_imsm(st
, info
->uuid
);
1586 memcpy(info
->uuid
, uuid_match_any
, sizeof(int[4]));
1587 fixup_container_spare_uuid(info
);
1591 static int update_super_imsm(struct supertype
*st
, struct mdinfo
*info
,
1592 char *update
, char *devname
, int verbose
,
1593 int uuid_set
, char *homehost
)
1595 /* For 'assemble' and 'force' we need to return non-zero if any
1596 * change was made. For others, the return value is ignored.
1597 * Update options are:
1598 * force-one : This device looks a bit old but needs to be included,
1599 * update age info appropriately.
1600 * assemble: clear any 'faulty' flag to allow this device to
1602 * force-array: Array is degraded but being forced, mark it clean
1603 * if that will be needed to assemble it.
1605 * newdev: not used ????
1606 * grow: Array has gained a new device - this is currently for
1608 * resync: mark as dirty so a resync will happen.
1609 * name: update the name - preserving the homehost
1610 * uuid: Change the uuid of the array to match watch is given
1612 * Following are not relevant for this imsm:
1613 * sparc2.2 : update from old dodgey metadata
1614 * super-minor: change the preferred_minor number
1615 * summaries: update redundant counters.
1616 * homehost: update the recorded homehost
1617 * _reshape_progress: record new reshape_progress position.
1620 struct intel_super
*super
= st
->sb
;
1621 struct imsm_super
*mpb
;
1623 /* we can only update container info */
1624 if (!super
|| super
->current_vol
>= 0 || !super
->anchor
)
1627 mpb
= super
->anchor
;
1629 if (strcmp(update
, "uuid") == 0 && uuid_set
&& !info
->update_private
)
1631 Name
": '--uuid' not supported for imsm metadata\n");
1632 else if (strcmp(update
, "uuid") == 0 && uuid_set
&& info
->update_private
) {
1633 mpb
->orig_family_num
= *((__u32
*) info
->update_private
);
1635 } else if (strcmp(update
, "uuid") == 0) {
1636 __u32
*new_family
= malloc(sizeof(*new_family
));
1638 /* update orig_family_number with the incoming random
1639 * data, report the new effective uuid, and store the
1640 * new orig_family_num for future updates.
1643 memcpy(&mpb
->orig_family_num
, info
->uuid
, sizeof(__u32
));
1644 uuid_from_super_imsm(st
, info
->uuid
);
1645 *new_family
= mpb
->orig_family_num
;
1646 info
->update_private
= new_family
;
1649 } else if (strcmp(update
, "assemble") == 0)
1653 Name
": '--update=%s' not supported for imsm metadata\n",
1656 /* successful update? recompute checksum */
1658 mpb
->check_sum
= __le32_to_cpu(__gen_imsm_checksum(mpb
));
1663 static size_t disks_to_mpb_size(int disks
)
1667 size
= sizeof(struct imsm_super
);
1668 size
+= (disks
- 1) * sizeof(struct imsm_disk
);
1669 size
+= 2 * sizeof(struct imsm_dev
);
1670 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
1671 size
+= (4 - 2) * sizeof(struct imsm_map
);
1672 /* 4 possible disk_ord_tbl's */
1673 size
+= 4 * (disks
- 1) * sizeof(__u32
);
1678 static __u64
avail_size_imsm(struct supertype
*st
, __u64 devsize
)
1680 if (devsize
< (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
))
1683 return devsize
- (MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
);
1686 static void free_devlist(struct intel_super
*super
)
1688 struct intel_dev
*dv
;
1690 while (super
->devlist
) {
1691 dv
= super
->devlist
->next
;
1692 free(super
->devlist
->dev
);
1693 free(super
->devlist
);
1694 super
->devlist
= dv
;
1698 static void imsm_copy_dev(struct imsm_dev
*dest
, struct imsm_dev
*src
)
1700 memcpy(dest
, src
, sizeof_imsm_dev(src
, 0));
1703 static int compare_super_imsm(struct supertype
*st
, struct supertype
*tst
)
1707 * 0 same, or first was empty, and second was copied
1708 * 1 second had wrong number
1710 * 3 wrong other info
1712 struct intel_super
*first
= st
->sb
;
1713 struct intel_super
*sec
= tst
->sb
;
1721 /* if an anchor does not have num_raid_devs set then it is a free
1724 if (first
->anchor
->num_raid_devs
> 0 &&
1725 sec
->anchor
->num_raid_devs
> 0) {
1726 /* Determine if these disks might ever have been
1727 * related. Further disambiguation can only take place
1728 * in load_super_imsm_all
1730 __u32 first_family
= first
->anchor
->orig_family_num
;
1731 __u32 sec_family
= sec
->anchor
->orig_family_num
;
1733 if (memcmp(first
->anchor
->sig
, sec
->anchor
->sig
,
1734 MAX_SIGNATURE_LENGTH
) != 0)
1737 if (first_family
== 0)
1738 first_family
= first
->anchor
->family_num
;
1739 if (sec_family
== 0)
1740 sec_family
= sec
->anchor
->family_num
;
1742 if (first_family
!= sec_family
)
1748 /* if 'first' is a spare promote it to a populated mpb with sec's
1751 if (first
->anchor
->num_raid_devs
== 0 &&
1752 sec
->anchor
->num_raid_devs
> 0) {
1754 struct intel_dev
*dv
;
1755 struct imsm_dev
*dev
;
1757 /* we need to copy raid device info from sec if an allocation
1758 * fails here we don't associate the spare
1760 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++) {
1761 dv
= malloc(sizeof(*dv
));
1764 dev
= malloc(sizeof_imsm_dev(get_imsm_dev(sec
, i
), 1));
1771 dv
->next
= first
->devlist
;
1772 first
->devlist
= dv
;
1774 if (i
< sec
->anchor
->num_raid_devs
) {
1775 /* allocation failure */
1776 free_devlist(first
);
1777 fprintf(stderr
, "imsm: failed to associate spare\n");
1780 first
->anchor
->num_raid_devs
= sec
->anchor
->num_raid_devs
;
1781 first
->anchor
->orig_family_num
= sec
->anchor
->orig_family_num
;
1782 first
->anchor
->family_num
= sec
->anchor
->family_num
;
1783 memcpy(first
->anchor
->sig
, sec
->anchor
->sig
, MAX_SIGNATURE_LENGTH
);
1784 for (i
= 0; i
< sec
->anchor
->num_raid_devs
; i
++)
1785 imsm_copy_dev(get_imsm_dev(first
, i
), get_imsm_dev(sec
, i
));
1791 static void fd2devname(int fd
, char *name
)
1795 char dname
[PATH_MAX
];
1800 if (fstat(fd
, &st
) != 0)
1802 sprintf(path
, "/sys/dev/block/%d:%d",
1803 major(st
.st_rdev
), minor(st
.st_rdev
));
1805 rv
= readlink(path
, dname
, sizeof(dname
));
1810 nm
= strrchr(dname
, '/');
1812 snprintf(name
, MAX_RAID_SERIAL_LEN
, "/dev/%s", nm
);
1815 extern int scsi_get_serial(int fd
, void *buf
, size_t buf_len
);
1817 static int imsm_read_serial(int fd
, char *devname
,
1818 __u8 serial
[MAX_RAID_SERIAL_LEN
])
1820 unsigned char scsi_serial
[255];
1829 memset(scsi_serial
, 0, sizeof(scsi_serial
));
1831 rv
= scsi_get_serial(fd
, scsi_serial
, sizeof(scsi_serial
));
1833 if (rv
&& check_env("IMSM_DEVNAME_AS_SERIAL")) {
1834 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
1835 fd2devname(fd
, (char *) serial
);
1842 Name
": Failed to retrieve serial for %s\n",
1847 rsp_len
= scsi_serial
[3];
1851 Name
": Failed to retrieve serial for %s\n",
1855 rsp_buf
= (char *) &scsi_serial
[4];
1857 /* trim all whitespace and non-printable characters and convert
1860 for (i
= 0, dest
= rsp_buf
; i
< rsp_len
; i
++) {
1863 /* ':' is reserved for use in placeholder serial
1864 * numbers for missing disks
1872 len
= dest
- rsp_buf
;
1875 /* truncate leading characters */
1876 if (len
> MAX_RAID_SERIAL_LEN
) {
1877 dest
+= len
- MAX_RAID_SERIAL_LEN
;
1878 len
= MAX_RAID_SERIAL_LEN
;
1881 memset(serial
, 0, MAX_RAID_SERIAL_LEN
);
1882 memcpy(serial
, dest
, len
);
1887 static int serialcmp(__u8
*s1
, __u8
*s2
)
1889 return strncmp((char *) s1
, (char *) s2
, MAX_RAID_SERIAL_LEN
);
1892 static void serialcpy(__u8
*dest
, __u8
*src
)
1894 strncpy((char *) dest
, (char *) src
, MAX_RAID_SERIAL_LEN
);
1898 static struct dl
*serial_to_dl(__u8
*serial
, struct intel_super
*super
)
1902 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
1903 if (serialcmp(dl
->serial
, serial
) == 0)
1910 static struct imsm_disk
*
1911 __serial_to_disk(__u8
*serial
, struct imsm_super
*mpb
, int *idx
)
1915 for (i
= 0; i
< mpb
->num_disks
; i
++) {
1916 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
1918 if (serialcmp(disk
->serial
, serial
) == 0) {
1929 load_imsm_disk(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
1931 struct imsm_disk
*disk
;
1936 __u8 serial
[MAX_RAID_SERIAL_LEN
];
1938 rv
= imsm_read_serial(fd
, devname
, serial
);
1943 dl
= calloc(1, sizeof(*dl
));
1947 Name
": failed to allocate disk buffer for %s\n",
1953 dl
->major
= major(stb
.st_rdev
);
1954 dl
->minor
= minor(stb
.st_rdev
);
1955 dl
->next
= super
->disks
;
1956 dl
->fd
= keep_fd
? fd
: -1;
1957 assert(super
->disks
== NULL
);
1959 serialcpy(dl
->serial
, serial
);
1962 fd2devname(fd
, name
);
1964 dl
->devname
= strdup(devname
);
1966 dl
->devname
= strdup(name
);
1968 /* look up this disk's index in the current anchor */
1969 disk
= __serial_to_disk(dl
->serial
, super
->anchor
, &dl
->index
);
1972 /* only set index on disks that are a member of a
1973 * populated contianer, i.e. one with raid_devs
1975 if (is_failed(&dl
->disk
))
1977 else if (is_spare(&dl
->disk
))
1985 /* When migrating map0 contains the 'destination' state while map1
1986 * contains the current state. When not migrating map0 contains the
1987 * current state. This routine assumes that map[0].map_state is set to
1988 * the current array state before being called.
1990 * Migration is indicated by one of the following states
1991 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
1992 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
1993 * map1state=unitialized)
1994 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
1996 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
1997 * map1state=degraded)
1999 static void migrate(struct imsm_dev
*dev
, __u8 to_state
, int migr_type
)
2001 struct imsm_map
*dest
;
2002 struct imsm_map
*src
= get_imsm_map(dev
, 0);
2004 dev
->vol
.migr_state
= 1;
2005 set_migr_type(dev
, migr_type
);
2006 dev
->vol
.curr_migr_unit
= 0;
2007 dest
= get_imsm_map(dev
, 1);
2009 /* duplicate and then set the target end state in map[0] */
2010 memcpy(dest
, src
, sizeof_imsm_map(src
));
2011 if (migr_type
== MIGR_REBUILD
) {
2015 for (i
= 0; i
< src
->num_members
; i
++) {
2016 ord
= __le32_to_cpu(src
->disk_ord_tbl
[i
]);
2017 set_imsm_ord_tbl_ent(src
, i
, ord_to_idx(ord
));
2021 src
->map_state
= to_state
;
2024 static void end_migration(struct imsm_dev
*dev
, __u8 map_state
)
2026 struct imsm_map
*map
= get_imsm_map(dev
, 0);
2027 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
);
2030 /* merge any IMSM_ORD_REBUILD bits that were not successfully
2031 * completed in the last migration.
2033 * FIXME add support for online capacity expansion and
2034 * raid-level-migration
2036 for (i
= 0; i
< prev
->num_members
; i
++)
2037 map
->disk_ord_tbl
[i
] |= prev
->disk_ord_tbl
[i
];
2039 dev
->vol
.migr_state
= 0;
2040 dev
->vol
.curr_migr_unit
= 0;
2041 map
->map_state
= map_state
;
2045 static int parse_raid_devices(struct intel_super
*super
)
2048 struct imsm_dev
*dev_new
;
2049 size_t len
, len_migr
;
2050 size_t space_needed
= 0;
2051 struct imsm_super
*mpb
= super
->anchor
;
2053 for (i
= 0; i
< super
->anchor
->num_raid_devs
; i
++) {
2054 struct imsm_dev
*dev_iter
= __get_imsm_dev(super
->anchor
, i
);
2055 struct intel_dev
*dv
;
2057 len
= sizeof_imsm_dev(dev_iter
, 0);
2058 len_migr
= sizeof_imsm_dev(dev_iter
, 1);
2060 space_needed
+= len_migr
- len
;
2062 dv
= malloc(sizeof(*dv
));
2065 dev_new
= malloc(len_migr
);
2070 imsm_copy_dev(dev_new
, dev_iter
);
2073 dv
->next
= super
->devlist
;
2074 super
->devlist
= dv
;
2077 /* ensure that super->buf is large enough when all raid devices
2080 if (__le32_to_cpu(mpb
->mpb_size
) + space_needed
> super
->len
) {
2083 len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + space_needed
, 512);
2084 if (posix_memalign(&buf
, 512, len
) != 0)
2087 memcpy(buf
, super
->buf
, super
->len
);
2088 memset(buf
+ super
->len
, 0, len
- super
->len
);
2097 /* retrieve a pointer to the bbm log which starts after all raid devices */
2098 struct bbm_log
*__get_imsm_bbm_log(struct imsm_super
*mpb
)
2102 if (__le32_to_cpu(mpb
->bbm_log_size
)) {
2104 ptr
+= mpb
->mpb_size
- __le32_to_cpu(mpb
->bbm_log_size
);
2110 static void __free_imsm(struct intel_super
*super
, int free_disks
);
2112 /* load_imsm_mpb - read matrix metadata
2113 * allocates super->mpb to be freed by free_super
2115 static int load_imsm_mpb(int fd
, struct intel_super
*super
, char *devname
)
2117 unsigned long long dsize
;
2118 unsigned long long sectors
;
2120 struct imsm_super
*anchor
;
2123 get_dev_size(fd
, NULL
, &dsize
);
2125 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0) {
2128 Name
": Cannot seek to anchor block on %s: %s\n",
2129 devname
, strerror(errno
));
2133 if (posix_memalign((void**)&anchor
, 512, 512) != 0) {
2136 Name
": Failed to allocate imsm anchor buffer"
2137 " on %s\n", devname
);
2140 if (read(fd
, anchor
, 512) != 512) {
2143 Name
": Cannot read anchor block on %s: %s\n",
2144 devname
, strerror(errno
));
2149 if (strncmp((char *) anchor
->sig
, MPB_SIGNATURE
, MPB_SIG_LEN
) != 0) {
2152 Name
": no IMSM anchor on %s\n", devname
);
2157 __free_imsm(super
, 0);
2158 super
->len
= ROUND_UP(anchor
->mpb_size
, 512);
2159 if (posix_memalign(&super
->buf
, 512, super
->len
) != 0) {
2162 Name
": unable to allocate %zu byte mpb buffer\n",
2167 memcpy(super
->buf
, anchor
, 512);
2169 sectors
= mpb_sectors(anchor
) - 1;
2172 check_sum
= __gen_imsm_checksum(super
->anchor
);
2173 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
2176 Name
": IMSM checksum %x != %x on %s\n",
2178 __le32_to_cpu(super
->anchor
->check_sum
),
2186 /* read the extended mpb */
2187 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0) {
2190 Name
": Cannot seek to extended mpb on %s: %s\n",
2191 devname
, strerror(errno
));
2195 if (read(fd
, super
->buf
+ 512, super
->len
- 512) != super
->len
- 512) {
2198 Name
": Cannot read extended mpb on %s: %s\n",
2199 devname
, strerror(errno
));
2203 check_sum
= __gen_imsm_checksum(super
->anchor
);
2204 if (check_sum
!= __le32_to_cpu(super
->anchor
->check_sum
)) {
2207 Name
": IMSM checksum %x != %x on %s\n",
2208 check_sum
, __le32_to_cpu(super
->anchor
->check_sum
),
2213 /* FIXME the BBM log is disk specific so we cannot use this global
2214 * buffer for all disks. Ok for now since we only look at the global
2215 * bbm_log_size parameter to gate assembly
2217 super
->bbm_log
= __get_imsm_bbm_log(super
->anchor
);
2223 load_and_parse_mpb(int fd
, struct intel_super
*super
, char *devname
, int keep_fd
)
2227 err
= load_imsm_mpb(fd
, super
, devname
);
2230 err
= load_imsm_disk(fd
, super
, devname
, keep_fd
);
2233 err
= parse_raid_devices(super
);
2238 static void __free_imsm_disk(struct dl
*d
)
2249 static void free_imsm_disks(struct intel_super
*super
)
2253 while (super
->disks
) {
2255 super
->disks
= d
->next
;
2256 __free_imsm_disk(d
);
2258 while (super
->missing
) {
2260 super
->missing
= d
->next
;
2261 __free_imsm_disk(d
);
2266 /* free all the pieces hanging off of a super pointer */
2267 static void __free_imsm(struct intel_super
*super
, int free_disks
)
2274 free_imsm_disks(super
);
2275 free_devlist(super
);
2277 free((void *) super
->hba
);
2282 static void free_imsm(struct intel_super
*super
)
2284 __free_imsm(super
, 1);
2288 static void free_super_imsm(struct supertype
*st
)
2290 struct intel_super
*super
= st
->sb
;
2299 static struct intel_super
*alloc_super(void)
2301 struct intel_super
*super
= malloc(sizeof(*super
));
2304 memset(super
, 0, sizeof(*super
));
2305 super
->current_vol
= -1;
2306 super
->create_offset
= ~((__u32
) 0);
2307 if (!check_env("IMSM_NO_PLATFORM"))
2308 super
->orom
= find_imsm_orom();
2309 if (super
->orom
&& !check_env("IMSM_TEST_OROM")) {
2310 struct sys_dev
*list
, *ent
;
2312 /* find the first intel ahci controller */
2313 list
= find_driver_devices("pci", "ahci");
2314 for (ent
= list
; ent
; ent
= ent
->next
)
2315 if (devpath_to_vendor(ent
->path
) == 0x8086)
2318 super
->hba
= ent
->path
;
2321 free_sys_dev(&list
);
2329 /* find_missing - helper routine for load_super_imsm_all that identifies
2330 * disks that have disappeared from the system. This routine relies on
2331 * the mpb being uptodate, which it is at load time.
2333 static int find_missing(struct intel_super
*super
)
2336 struct imsm_super
*mpb
= super
->anchor
;
2338 struct imsm_disk
*disk
;
2340 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2341 disk
= __get_imsm_disk(mpb
, i
);
2342 dl
= serial_to_dl(disk
->serial
, super
);
2346 dl
= malloc(sizeof(*dl
));
2352 dl
->devname
= strdup("missing");
2354 serialcpy(dl
->serial
, disk
->serial
);
2357 dl
->next
= super
->missing
;
2358 super
->missing
= dl
;
2364 static struct intel_disk
*disk_list_get(__u8
*serial
, struct intel_disk
*disk_list
)
2366 struct intel_disk
*idisk
= disk_list
;
2369 if (serialcmp(idisk
->disk
.serial
, serial
) == 0)
2371 idisk
= idisk
->next
;
2377 static int __prep_thunderdome(struct intel_super
**table
, int tbl_size
,
2378 struct intel_super
*super
,
2379 struct intel_disk
**disk_list
)
2381 struct imsm_disk
*d
= &super
->disks
->disk
;
2382 struct imsm_super
*mpb
= super
->anchor
;
2385 for (i
= 0; i
< tbl_size
; i
++) {
2386 struct imsm_super
*tbl_mpb
= table
[i
]->anchor
;
2387 struct imsm_disk
*tbl_d
= &table
[i
]->disks
->disk
;
2389 if (tbl_mpb
->family_num
== mpb
->family_num
) {
2390 if (tbl_mpb
->check_sum
== mpb
->check_sum
) {
2391 dprintf("%s: mpb from %d:%d matches %d:%d\n",
2392 __func__
, super
->disks
->major
,
2393 super
->disks
->minor
,
2394 table
[i
]->disks
->major
,
2395 table
[i
]->disks
->minor
);
2399 if (((is_configured(d
) && !is_configured(tbl_d
)) ||
2400 is_configured(d
) == is_configured(tbl_d
)) &&
2401 tbl_mpb
->generation_num
< mpb
->generation_num
) {
2402 /* current version of the mpb is a
2403 * better candidate than the one in
2404 * super_table, but copy over "cross
2405 * generational" status
2407 struct intel_disk
*idisk
;
2409 dprintf("%s: mpb from %d:%d replaces %d:%d\n",
2410 __func__
, super
->disks
->major
,
2411 super
->disks
->minor
,
2412 table
[i
]->disks
->major
,
2413 table
[i
]->disks
->minor
);
2415 idisk
= disk_list_get(tbl_d
->serial
, *disk_list
);
2416 if (idisk
&& is_failed(&idisk
->disk
))
2417 tbl_d
->status
|= FAILED_DISK
;
2420 struct intel_disk
*idisk
;
2421 struct imsm_disk
*disk
;
2423 /* tbl_mpb is more up to date, but copy
2424 * over cross generational status before
2427 disk
= __serial_to_disk(d
->serial
, mpb
, NULL
);
2428 if (disk
&& is_failed(disk
))
2429 d
->status
|= FAILED_DISK
;
2431 idisk
= disk_list_get(d
->serial
, *disk_list
);
2434 if (disk
&& is_configured(disk
))
2435 idisk
->disk
.status
|= CONFIGURED_DISK
;
2438 dprintf("%s: mpb from %d:%d prefer %d:%d\n",
2439 __func__
, super
->disks
->major
,
2440 super
->disks
->minor
,
2441 table
[i
]->disks
->major
,
2442 table
[i
]->disks
->minor
);
2450 table
[tbl_size
++] = super
;
2454 /* update/extend the merged list of imsm_disk records */
2455 for (j
= 0; j
< mpb
->num_disks
; j
++) {
2456 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, j
);
2457 struct intel_disk
*idisk
;
2459 idisk
= disk_list_get(disk
->serial
, *disk_list
);
2461 idisk
->disk
.status
|= disk
->status
;
2462 if (is_configured(&idisk
->disk
) ||
2463 is_failed(&idisk
->disk
))
2464 idisk
->disk
.status
&= ~(SPARE_DISK
);
2466 idisk
= calloc(1, sizeof(*idisk
));
2469 idisk
->owner
= IMSM_UNKNOWN_OWNER
;
2470 idisk
->disk
= *disk
;
2471 idisk
->next
= *disk_list
;
2475 if (serialcmp(idisk
->disk
.serial
, d
->serial
) == 0)
2482 static struct intel_super
*
2483 validate_members(struct intel_super
*super
, struct intel_disk
*disk_list
,
2486 struct imsm_super
*mpb
= super
->anchor
;
2490 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2491 struct imsm_disk
*disk
= __get_imsm_disk(mpb
, i
);
2492 struct intel_disk
*idisk
;
2494 idisk
= disk_list_get(disk
->serial
, disk_list
);
2496 if (idisk
->owner
== owner
||
2497 idisk
->owner
== IMSM_UNKNOWN_OWNER
)
2500 dprintf("%s: '%.16s' owner %d != %d\n",
2501 __func__
, disk
->serial
, idisk
->owner
,
2504 dprintf("%s: unknown disk %x [%d]: %.16s\n",
2505 __func__
, __le32_to_cpu(mpb
->family_num
), i
,
2511 if (ok_count
== mpb
->num_disks
)
2516 static void show_conflicts(__u32 family_num
, struct intel_super
*super_list
)
2518 struct intel_super
*s
;
2520 for (s
= super_list
; s
; s
= s
->next
) {
2521 if (family_num
!= s
->anchor
->family_num
)
2523 fprintf(stderr
, "Conflict, offlining family %#x on '%s'\n",
2524 __le32_to_cpu(family_num
), s
->disks
->devname
);
2528 static struct intel_super
*
2529 imsm_thunderdome(struct intel_super
**super_list
, int len
)
2531 struct intel_super
*super_table
[len
];
2532 struct intel_disk
*disk_list
= NULL
;
2533 struct intel_super
*champion
, *spare
;
2534 struct intel_super
*s
, **del
;
2539 memset(super_table
, 0, sizeof(super_table
));
2540 for (s
= *super_list
; s
; s
= s
->next
)
2541 tbl_size
= __prep_thunderdome(super_table
, tbl_size
, s
, &disk_list
);
2543 for (i
= 0; i
< tbl_size
; i
++) {
2544 struct imsm_disk
*d
;
2545 struct intel_disk
*idisk
;
2546 struct imsm_super
*mpb
= super_table
[i
]->anchor
;
2549 d
= &s
->disks
->disk
;
2551 /* 'd' must appear in merged disk list for its
2552 * configuration to be valid
2554 idisk
= disk_list_get(d
->serial
, disk_list
);
2555 if (idisk
&& idisk
->owner
== i
)
2556 s
= validate_members(s
, disk_list
, i
);
2561 dprintf("%s: marking family: %#x from %d:%d offline\n",
2562 __func__
, mpb
->family_num
,
2563 super_table
[i
]->disks
->major
,
2564 super_table
[i
]->disks
->minor
);
2568 /* This is where the mdadm implementation differs from the Windows
2569 * driver which has no strict concept of a container. We can only
2570 * assemble one family from a container, so when returning a prodigal
2571 * array member to this system the code will not be able to disambiguate
2572 * the container contents that should be assembled ("foreign" versus
2573 * "local"). It requires user intervention to set the orig_family_num
2574 * to a new value to establish a new container. The Windows driver in
2575 * this situation fixes up the volume name in place and manages the
2576 * foreign array as an independent entity.
2581 for (i
= 0; i
< tbl_size
; i
++) {
2582 struct intel_super
*tbl_ent
= super_table
[i
];
2588 if (tbl_ent
->anchor
->num_raid_devs
== 0) {
2593 if (s
&& !is_spare
) {
2594 show_conflicts(tbl_ent
->anchor
->family_num
, *super_list
);
2596 } else if (!s
&& !is_spare
)
2609 fprintf(stderr
, "Chose family %#x on '%s', "
2610 "assemble conflicts to new container with '--update=uuid'\n",
2611 __le32_to_cpu(s
->anchor
->family_num
), s
->disks
->devname
);
2613 /* collect all dl's onto 'champion', and update them to
2614 * champion's version of the status
2616 for (s
= *super_list
; s
; s
= s
->next
) {
2617 struct imsm_super
*mpb
= champion
->anchor
;
2618 struct dl
*dl
= s
->disks
;
2623 for (i
= 0; i
< mpb
->num_disks
; i
++) {
2624 struct imsm_disk
*disk
;
2626 disk
= __serial_to_disk(dl
->serial
, mpb
, &dl
->index
);
2629 /* only set index on disks that are a member of
2630 * a populated contianer, i.e. one with
2633 if (is_failed(&dl
->disk
))
2635 else if (is_spare(&dl
->disk
))
2641 if (i
>= mpb
->num_disks
) {
2642 struct intel_disk
*idisk
;
2644 idisk
= disk_list_get(dl
->serial
, disk_list
);
2645 if (idisk
&& is_spare(&idisk
->disk
) &&
2646 !is_failed(&idisk
->disk
) && !is_configured(&idisk
->disk
))
2654 dl
->next
= champion
->disks
;
2655 champion
->disks
= dl
;
2659 /* delete 'champion' from super_list */
2660 for (del
= super_list
; *del
; ) {
2661 if (*del
== champion
) {
2662 *del
= (*del
)->next
;
2665 del
= &(*del
)->next
;
2667 champion
->next
= NULL
;
2671 struct intel_disk
*idisk
= disk_list
;
2673 disk_list
= disk_list
->next
;
2680 static int load_super_imsm_all(struct supertype
*st
, int fd
, void **sbp
,
2681 char *devname
, int keep_fd
)
2684 struct intel_super
*super_list
= NULL
;
2685 struct intel_super
*super
= NULL
;
2686 int devnum
= fd2devnum(fd
);
2691 enum sysfs_read_flags flags
;
2693 flags
= GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
;
2694 if (mdmon_running(devnum
))
2695 flags
|= SKIP_GONE_DEVS
;
2697 /* check if 'fd' an opened container */
2698 sra
= sysfs_read(fd
, 0, flags
);
2702 if (sra
->array
.major_version
!= -1 ||
2703 sra
->array
.minor_version
!= -2 ||
2704 strcmp(sra
->text_version
, "imsm") != 0) {
2709 for (sd
= sra
->devs
, i
= 0; sd
; sd
= sd
->next
, i
++) {
2710 struct intel_super
*s
= alloc_super();
2717 s
->next
= super_list
;
2721 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
2722 dfd
= dev_open(nm
, keep_fd
? O_RDWR
: O_RDONLY
);
2726 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
2728 /* retry the load if we might have raced against mdmon */
2729 if (err
== 3 && mdmon_running(devnum
))
2730 for (retry
= 0; retry
< 3; retry
++) {
2732 err
= load_and_parse_mpb(dfd
, s
, NULL
, keep_fd
);
2742 /* all mpbs enter, maybe one leaves */
2743 super
= imsm_thunderdome(&super_list
, i
);
2749 if (find_missing(super
) != 0) {
2755 if (st
->subarray
[0]) {
2756 if (atoi(st
->subarray
) <= super
->anchor
->num_raid_devs
)
2757 super
->current_vol
= atoi(st
->subarray
);
2767 while (super_list
) {
2768 struct intel_super
*s
= super_list
;
2770 super_list
= super_list
->next
;
2779 st
->container_dev
= devnum
;
2780 if (err
== 0 && st
->ss
== NULL
) {
2781 st
->ss
= &super_imsm
;
2782 st
->minor_version
= 0;
2783 st
->max_devs
= IMSM_MAX_DEVICES
;
2785 st
->loaded_container
= 1;
2791 static int load_super_imsm(struct supertype
*st
, int fd
, char *devname
)
2793 struct intel_super
*super
;
2797 if (load_super_imsm_all(st
, fd
, &st
->sb
, devname
, 1) == 0)
2801 free_super_imsm(st
);
2803 super
= alloc_super();
2806 Name
": malloc of %zu failed.\n",
2811 rv
= load_and_parse_mpb(fd
, super
, devname
, 0);
2816 Name
": Failed to load all information "
2817 "sections on %s\n", devname
);
2822 if (st
->subarray
[0]) {
2823 if (atoi(st
->subarray
) <= super
->anchor
->num_raid_devs
)
2824 super
->current_vol
= atoi(st
->subarray
);
2832 if (st
->ss
== NULL
) {
2833 st
->ss
= &super_imsm
;
2834 st
->minor_version
= 0;
2835 st
->max_devs
= IMSM_MAX_DEVICES
;
2837 st
->loaded_container
= 0;
2842 static __u16
info_to_blocks_per_strip(mdu_array_info_t
*info
)
2844 if (info
->level
== 1)
2846 return info
->chunk_size
>> 9;
2849 static __u32
info_to_num_data_stripes(mdu_array_info_t
*info
, int num_domains
)
2853 num_stripes
= (info
->size
* 2) / info_to_blocks_per_strip(info
);
2854 num_stripes
/= num_domains
;
2859 static __u32
info_to_blocks_per_member(mdu_array_info_t
*info
)
2861 if (info
->level
== 1)
2862 return info
->size
* 2;
2864 return (info
->size
* 2) & ~(info_to_blocks_per_strip(info
) - 1);
2867 static void imsm_update_version_info(struct intel_super
*super
)
2869 /* update the version and attributes */
2870 struct imsm_super
*mpb
= super
->anchor
;
2872 struct imsm_dev
*dev
;
2873 struct imsm_map
*map
;
2876 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
2877 dev
= get_imsm_dev(super
, i
);
2878 map
= get_imsm_map(dev
, 0);
2879 if (__le32_to_cpu(dev
->size_high
) > 0)
2880 mpb
->attributes
|= MPB_ATTRIB_2TB
;
2882 /* FIXME detect when an array spans a port multiplier */
2884 mpb
->attributes
|= MPB_ATTRIB_PM
;
2887 if (mpb
->num_raid_devs
> 1 ||
2888 mpb
->attributes
!= MPB_ATTRIB_CHECKSUM_VERIFY
) {
2889 version
= MPB_VERSION_ATTRIBS
;
2890 switch (get_imsm_raid_level(map
)) {
2891 case 0: mpb
->attributes
|= MPB_ATTRIB_RAID0
; break;
2892 case 1: mpb
->attributes
|= MPB_ATTRIB_RAID1
; break;
2893 case 10: mpb
->attributes
|= MPB_ATTRIB_RAID10
; break;
2894 case 5: mpb
->attributes
|= MPB_ATTRIB_RAID5
; break;
2897 if (map
->num_members
>= 5)
2898 version
= MPB_VERSION_5OR6_DISK_ARRAY
;
2899 else if (dev
->status
== DEV_CLONE_N_GO
)
2900 version
= MPB_VERSION_CNG
;
2901 else if (get_imsm_raid_level(map
) == 5)
2902 version
= MPB_VERSION_RAID5
;
2903 else if (map
->num_members
>= 3)
2904 version
= MPB_VERSION_3OR4_DISK_ARRAY
;
2905 else if (get_imsm_raid_level(map
) == 1)
2906 version
= MPB_VERSION_RAID1
;
2908 version
= MPB_VERSION_RAID0
;
2910 strcpy(((char *) mpb
->sig
) + strlen(MPB_SIGNATURE
), version
);
2914 static int init_super_imsm_volume(struct supertype
*st
, mdu_array_info_t
*info
,
2915 unsigned long long size
, char *name
,
2916 char *homehost
, int *uuid
)
2918 /* We are creating a volume inside a pre-existing container.
2919 * so st->sb is already set.
2921 struct intel_super
*super
= st
->sb
;
2922 struct imsm_super
*mpb
= super
->anchor
;
2923 struct intel_dev
*dv
;
2924 struct imsm_dev
*dev
;
2925 struct imsm_vol
*vol
;
2926 struct imsm_map
*map
;
2927 int idx
= mpb
->num_raid_devs
;
2929 unsigned long long array_blocks
;
2930 size_t size_old
, size_new
;
2931 __u32 num_data_stripes
;
2933 if (super
->orom
&& mpb
->num_raid_devs
>= super
->orom
->vpa
) {
2934 fprintf(stderr
, Name
": This imsm-container already has the "
2935 "maximum of %d volumes\n", super
->orom
->vpa
);
2939 /* ensure the mpb is large enough for the new data */
2940 size_old
= __le32_to_cpu(mpb
->mpb_size
);
2941 size_new
= disks_to_mpb_size(info
->nr_disks
);
2942 if (size_new
> size_old
) {
2944 size_t size_round
= ROUND_UP(size_new
, 512);
2946 if (posix_memalign(&mpb_new
, 512, size_round
) != 0) {
2947 fprintf(stderr
, Name
": could not allocate new mpb\n");
2950 memcpy(mpb_new
, mpb
, size_old
);
2953 super
->anchor
= mpb_new
;
2954 mpb
->mpb_size
= __cpu_to_le32(size_new
);
2955 memset(mpb_new
+ size_old
, 0, size_round
- size_old
);
2957 super
->current_vol
= idx
;
2958 /* when creating the first raid device in this container set num_disks
2959 * to zero, i.e. delete this spare and add raid member devices in
2960 * add_to_super_imsm_volume()
2962 if (super
->current_vol
== 0)
2965 for (i
= 0; i
< super
->current_vol
; i
++) {
2966 dev
= get_imsm_dev(super
, i
);
2967 if (strncmp((char *) dev
->volume
, name
,
2968 MAX_RAID_SERIAL_LEN
) == 0) {
2969 fprintf(stderr
, Name
": '%s' is already defined for this container\n",
2975 sprintf(st
->subarray
, "%d", idx
);
2976 dv
= malloc(sizeof(*dv
));
2978 fprintf(stderr
, Name
": failed to allocate device list entry\n");
2981 dev
= malloc(sizeof(*dev
) + sizeof(__u32
) * (info
->raid_disks
- 1));
2984 fprintf(stderr
, Name
": could not allocate raid device\n");
2987 strncpy((char *) dev
->volume
, name
, MAX_RAID_SERIAL_LEN
);
2988 if (info
->level
== 1)
2989 array_blocks
= info_to_blocks_per_member(info
);
2991 array_blocks
= calc_array_size(info
->level
, info
->raid_disks
,
2992 info
->layout
, info
->chunk_size
,
2994 /* round array size down to closest MB */
2995 array_blocks
= (array_blocks
>> SECT_PER_MB_SHIFT
) << SECT_PER_MB_SHIFT
;
2997 dev
->size_low
= __cpu_to_le32((__u32
) array_blocks
);
2998 dev
->size_high
= __cpu_to_le32((__u32
) (array_blocks
>> 32));
2999 dev
->status
= __cpu_to_le32(0);
3000 dev
->reserved_blocks
= __cpu_to_le32(0);
3002 vol
->migr_state
= 0;
3003 set_migr_type(dev
, MIGR_INIT
);
3005 vol
->curr_migr_unit
= 0;
3006 map
= get_imsm_map(dev
, 0);
3007 map
->pba_of_lba0
= __cpu_to_le32(super
->create_offset
);
3008 map
->blocks_per_member
= __cpu_to_le32(info_to_blocks_per_member(info
));
3009 map
->blocks_per_strip
= __cpu_to_le16(info_to_blocks_per_strip(info
));
3010 map
->failed_disk_num
= ~0;
3011 map
->map_state
= info
->level
? IMSM_T_STATE_UNINITIALIZED
:
3012 IMSM_T_STATE_NORMAL
;
3015 if (info
->level
== 1 && info
->raid_disks
> 2) {
3018 fprintf(stderr
, Name
": imsm does not support more than 2 disks"
3019 "in a raid1 volume\n");
3023 map
->raid_level
= info
->level
;
3024 if (info
->level
== 10) {
3025 map
->raid_level
= 1;
3026 map
->num_domains
= info
->raid_disks
/ 2;
3027 } else if (info
->level
== 1)
3028 map
->num_domains
= info
->raid_disks
;
3030 map
->num_domains
= 1;
3032 num_data_stripes
= info_to_num_data_stripes(info
, map
->num_domains
);
3033 map
->num_data_stripes
= __cpu_to_le32(num_data_stripes
);
3035 map
->num_members
= info
->raid_disks
;
3036 for (i
= 0; i
< map
->num_members
; i
++) {
3037 /* initialized in add_to_super */
3038 set_imsm_ord_tbl_ent(map
, i
, IMSM_ORD_REBUILD
);
3040 mpb
->num_raid_devs
++;
3043 dv
->index
= super
->current_vol
;
3044 dv
->next
= super
->devlist
;
3045 super
->devlist
= dv
;
3047 imsm_update_version_info(super
);
3052 static int init_super_imsm(struct supertype
*st
, mdu_array_info_t
*info
,
3053 unsigned long long size
, char *name
,
3054 char *homehost
, int *uuid
)
3056 /* This is primarily called by Create when creating a new array.
3057 * We will then get add_to_super called for each component, and then
3058 * write_init_super called to write it out to each device.
3059 * For IMSM, Create can create on fresh devices or on a pre-existing
3061 * To create on a pre-existing array a different method will be called.
3062 * This one is just for fresh drives.
3064 struct intel_super
*super
;
3065 struct imsm_super
*mpb
;
3070 return init_super_imsm_volume(st
, info
, size
, name
, homehost
, uuid
);
3073 mpb_size
= disks_to_mpb_size(info
->nr_disks
);
3077 super
= alloc_super();
3078 if (super
&& posix_memalign(&super
->buf
, 512, mpb_size
) != 0) {
3083 fprintf(stderr
, Name
3084 ": %s could not allocate superblock\n", __func__
);
3087 memset(super
->buf
, 0, mpb_size
);
3089 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
3093 /* zeroing superblock */
3097 mpb
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
3099 version
= (char *) mpb
->sig
;
3100 strcpy(version
, MPB_SIGNATURE
);
3101 version
+= strlen(MPB_SIGNATURE
);
3102 strcpy(version
, MPB_VERSION_RAID0
);
3108 static int add_to_super_imsm_volume(struct supertype
*st
, mdu_disk_info_t
*dk
,
3109 int fd
, char *devname
)
3111 struct intel_super
*super
= st
->sb
;
3112 struct imsm_super
*mpb
= super
->anchor
;
3114 struct imsm_dev
*dev
;
3115 struct imsm_map
*map
;
3118 dev
= get_imsm_dev(super
, super
->current_vol
);
3119 map
= get_imsm_map(dev
, 0);
3121 if (! (dk
->state
& (1<<MD_DISK_SYNC
))) {
3122 fprintf(stderr
, Name
": %s: Cannot add spare devices to IMSM volume\n",
3128 /* we're doing autolayout so grab the pre-marked (in
3129 * validate_geometry) raid_disk
3131 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3132 if (dl
->raiddisk
== dk
->raid_disk
)
3135 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3136 if (dl
->major
== dk
->major
&&
3137 dl
->minor
== dk
->minor
)
3142 fprintf(stderr
, Name
": %s is not a member of the same container\n", devname
);
3146 /* add a pristine spare to the metadata */
3147 if (dl
->index
< 0) {
3148 dl
->index
= super
->anchor
->num_disks
;
3149 super
->anchor
->num_disks
++;
3151 /* Check the device has not already been added */
3152 slot
= get_imsm_disk_slot(map
, dl
->index
);
3154 (get_imsm_ord_tbl_ent(dev
, slot
) & IMSM_ORD_REBUILD
) == 0) {
3155 fprintf(stderr
, Name
": %s has been included in this array twice\n",
3159 set_imsm_ord_tbl_ent(map
, dk
->number
, dl
->index
);
3160 dl
->disk
.status
= CONFIGURED_DISK
;
3162 /* if we are creating the first raid device update the family number */
3163 if (super
->current_vol
== 0) {
3165 struct imsm_dev
*_dev
= __get_imsm_dev(mpb
, 0);
3166 struct imsm_disk
*_disk
= __get_imsm_disk(mpb
, dl
->index
);
3168 if (!_dev
|| !_disk
) {
3169 fprintf(stderr
, Name
": BUG mpb setup error\n");
3175 sum
+= __gen_imsm_checksum(mpb
);
3176 mpb
->family_num
= __cpu_to_le32(sum
);
3177 mpb
->orig_family_num
= mpb
->family_num
;
3183 static int add_to_super_imsm(struct supertype
*st
, mdu_disk_info_t
*dk
,
3184 int fd
, char *devname
)
3186 struct intel_super
*super
= st
->sb
;
3188 unsigned long long size
;
3193 /* if we are on an RAID enabled platform check that the disk is
3194 * attached to the raid controller
3196 if (super
->hba
&& !disk_attached_to_hba(fd
, super
->hba
)) {
3198 Name
": %s is not attached to the raid controller: %s\n",
3199 devname
? : "disk", super
->hba
);
3203 if (super
->current_vol
>= 0)
3204 return add_to_super_imsm_volume(st
, dk
, fd
, devname
);
3207 dd
= malloc(sizeof(*dd
));
3210 Name
": malloc failed %s:%d.\n", __func__
, __LINE__
);
3213 memset(dd
, 0, sizeof(*dd
));
3214 dd
->major
= major(stb
.st_rdev
);
3215 dd
->minor
= minor(stb
.st_rdev
);
3217 dd
->devname
= devname
? strdup(devname
) : NULL
;
3220 rv
= imsm_read_serial(fd
, devname
, dd
->serial
);
3223 Name
": failed to retrieve scsi serial, aborting\n");
3228 get_dev_size(fd
, NULL
, &size
);
3230 serialcpy(dd
->disk
.serial
, dd
->serial
);
3231 dd
->disk
.total_blocks
= __cpu_to_le32(size
);
3232 dd
->disk
.status
= SPARE_DISK
;
3233 if (sysfs_disk_to_scsi_id(fd
, &id
) == 0)
3234 dd
->disk
.scsi_id
= __cpu_to_le32(id
);
3236 dd
->disk
.scsi_id
= __cpu_to_le32(0);
3238 if (st
->update_tail
) {
3239 dd
->next
= super
->add
;
3242 dd
->next
= super
->disks
;
3249 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
);
3253 struct imsm_super anchor
;
3254 } spare_record
__attribute__ ((aligned(512)));
3256 /* spare records have their own family number and do not have any defined raid
3259 static int write_super_imsm_spares(struct intel_super
*super
, int doclose
)
3261 struct imsm_super
*mpb
= super
->anchor
;
3262 struct imsm_super
*spare
= &spare_record
.anchor
;
3266 spare
->mpb_size
= __cpu_to_le32(sizeof(struct imsm_super
)),
3267 spare
->generation_num
= __cpu_to_le32(1UL),
3268 spare
->attributes
= MPB_ATTRIB_CHECKSUM_VERIFY
;
3269 spare
->num_disks
= 1,
3270 spare
->num_raid_devs
= 0,
3271 spare
->cache_size
= mpb
->cache_size
,
3272 spare
->pwr_cycle_count
= __cpu_to_le32(1),
3274 snprintf((char *) spare
->sig
, MAX_SIGNATURE_LENGTH
,
3275 MPB_SIGNATURE MPB_VERSION_RAID0
);
3277 for (d
= super
->disks
; d
; d
= d
->next
) {
3281 spare
->disk
[0] = d
->disk
;
3282 sum
= __gen_imsm_checksum(spare
);
3283 spare
->family_num
= __cpu_to_le32(sum
);
3284 spare
->orig_family_num
= 0;
3285 sum
= __gen_imsm_checksum(spare
);
3286 spare
->check_sum
= __cpu_to_le32(sum
);
3288 if (store_imsm_mpb(d
->fd
, spare
)) {
3289 fprintf(stderr
, "%s: failed for device %d:%d %s\n",
3290 __func__
, d
->major
, d
->minor
, strerror(errno
));
3302 static int write_super_imsm(struct intel_super
*super
, int doclose
)
3304 struct imsm_super
*mpb
= super
->anchor
;
3310 __u32 mpb_size
= sizeof(struct imsm_super
) - sizeof(struct imsm_disk
);
3312 /* 'generation' is incremented everytime the metadata is written */
3313 generation
= __le32_to_cpu(mpb
->generation_num
);
3315 mpb
->generation_num
= __cpu_to_le32(generation
);
3317 /* fix up cases where previous mdadm releases failed to set
3320 if (mpb
->orig_family_num
== 0)
3321 mpb
->orig_family_num
= mpb
->family_num
;
3323 mpb_size
+= sizeof(struct imsm_disk
) * mpb
->num_disks
;
3324 for (d
= super
->disks
; d
; d
= d
->next
) {
3328 mpb
->disk
[d
->index
] = d
->disk
;
3330 for (d
= super
->missing
; d
; d
= d
->next
)
3331 mpb
->disk
[d
->index
] = d
->disk
;
3333 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
3334 struct imsm_dev
*dev
= __get_imsm_dev(mpb
, i
);
3336 imsm_copy_dev(dev
, get_imsm_dev(super
, i
));
3337 mpb_size
+= sizeof_imsm_dev(dev
, 0);
3339 mpb_size
+= __le32_to_cpu(mpb
->bbm_log_size
);
3340 mpb
->mpb_size
= __cpu_to_le32(mpb_size
);
3342 /* recalculate checksum */
3343 sum
= __gen_imsm_checksum(mpb
);
3344 mpb
->check_sum
= __cpu_to_le32(sum
);
3346 /* write the mpb for disks that compose raid devices */
3347 for (d
= super
->disks
; d
; d
= d
->next
) {
3350 if (store_imsm_mpb(d
->fd
, mpb
))
3351 fprintf(stderr
, "%s: failed for device %d:%d %s\n",
3352 __func__
, d
->major
, d
->minor
, strerror(errno
));
3360 return write_super_imsm_spares(super
, doclose
);
3366 static int create_array(struct supertype
*st
, int dev_idx
)
3369 struct imsm_update_create_array
*u
;
3370 struct intel_super
*super
= st
->sb
;
3371 struct imsm_dev
*dev
= get_imsm_dev(super
, dev_idx
);
3372 struct imsm_map
*map
= get_imsm_map(dev
, 0);
3373 struct disk_info
*inf
;
3374 struct imsm_disk
*disk
;
3377 len
= sizeof(*u
) - sizeof(*dev
) + sizeof_imsm_dev(dev
, 0) +
3378 sizeof(*inf
) * map
->num_members
;
3381 fprintf(stderr
, "%s: failed to allocate update buffer\n",
3386 u
->type
= update_create_array
;
3387 u
->dev_idx
= dev_idx
;
3388 imsm_copy_dev(&u
->dev
, dev
);
3389 inf
= get_disk_info(u
);
3390 for (i
= 0; i
< map
->num_members
; i
++) {
3391 int idx
= get_imsm_disk_idx(dev
, i
);
3393 disk
= get_imsm_disk(super
, idx
);
3394 serialcpy(inf
[i
].serial
, disk
->serial
);
3396 append_metadata_update(st
, u
, len
);
3401 static int _add_disk(struct supertype
*st
)
3403 struct intel_super
*super
= st
->sb
;
3405 struct imsm_update_add_disk
*u
;
3413 fprintf(stderr
, "%s: failed to allocate update buffer\n",
3418 u
->type
= update_add_disk
;
3419 append_metadata_update(st
, u
, len
);
3424 static int write_init_super_imsm(struct supertype
*st
)
3426 struct intel_super
*super
= st
->sb
;
3427 int current_vol
= super
->current_vol
;
3429 /* we are done with current_vol reset it to point st at the container */
3430 super
->current_vol
= -1;
3432 if (st
->update_tail
) {
3433 /* queue the recently created array / added disk
3434 * as a metadata update */
3438 /* determine if we are creating a volume or adding a disk */
3439 if (current_vol
< 0) {
3440 /* in the add disk case we are running in mdmon
3441 * context, so don't close fd's
3443 return _add_disk(st
);
3445 rv
= create_array(st
, current_vol
);
3447 for (d
= super
->disks
; d
; d
= d
->next
) {
3455 for (d
= super
->disks
; d
; d
= d
->next
)
3456 Kill(d
->devname
, NULL
, 0, 1, 1);
3457 return write_super_imsm(st
->sb
, 1);
3462 static int store_super_imsm(struct supertype
*st
, int fd
)
3464 struct intel_super
*super
= st
->sb
;
3465 struct imsm_super
*mpb
= super
? super
->anchor
: NULL
;
3471 return store_imsm_mpb(fd
, mpb
);
3477 static int imsm_bbm_log_size(struct imsm_super
*mpb
)
3479 return __le32_to_cpu(mpb
->bbm_log_size
);
3483 static int validate_geometry_imsm_container(struct supertype
*st
, int level
,
3484 int layout
, int raiddisks
, int chunk
,
3485 unsigned long long size
, char *dev
,
3486 unsigned long long *freesize
,
3490 unsigned long long ldsize
;
3491 const struct imsm_orom
*orom
;
3493 if (level
!= LEVEL_CONTAINER
)
3498 if (check_env("IMSM_NO_PLATFORM"))
3501 orom
= find_imsm_orom();
3502 if (orom
&& raiddisks
> orom
->tds
) {
3504 fprintf(stderr
, Name
": %d exceeds maximum number of"
3505 " platform supported disks: %d\n",
3506 raiddisks
, orom
->tds
);
3510 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
3513 fprintf(stderr
, Name
": imsm: Cannot open %s: %s\n",
3514 dev
, strerror(errno
));
3517 if (!get_dev_size(fd
, dev
, &ldsize
)) {
3523 *freesize
= avail_size_imsm(st
, ldsize
>> 9);
3528 static unsigned long long find_size(struct extent
*e
, int *idx
, int num_extents
)
3530 const unsigned long long base_start
= e
[*idx
].start
;
3531 unsigned long long end
= base_start
+ e
[*idx
].size
;
3534 if (base_start
== end
)
3538 for (i
= *idx
; i
< num_extents
; i
++) {
3539 /* extend overlapping extents */
3540 if (e
[i
].start
>= base_start
&&
3541 e
[i
].start
<= end
) {
3544 if (e
[i
].start
+ e
[i
].size
> end
)
3545 end
= e
[i
].start
+ e
[i
].size
;
3546 } else if (e
[i
].start
> end
) {
3552 return end
- base_start
;
3555 static unsigned long long merge_extents(struct intel_super
*super
, int sum_extents
)
3557 /* build a composite disk with all known extents and generate a new
3558 * 'maxsize' given the "all disks in an array must share a common start
3559 * offset" constraint
3561 struct extent
*e
= calloc(sum_extents
, sizeof(*e
));
3565 unsigned long long pos
;
3566 unsigned long long start
= 0;
3567 unsigned long long maxsize
;
3568 unsigned long reserve
;
3573 /* coalesce and sort all extents. also, check to see if we need to
3574 * reserve space between member arrays
3577 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
3580 for (i
= 0; i
< dl
->extent_cnt
; i
++)
3583 qsort(e
, sum_extents
, sizeof(*e
), cmp_extent
);
3588 while (i
< sum_extents
) {
3589 e
[j
].start
= e
[i
].start
;
3590 e
[j
].size
= find_size(e
, &i
, sum_extents
);
3592 if (e
[j
-1].size
== 0)
3601 unsigned long long esize
;
3603 esize
= e
[i
].start
- pos
;
3604 if (esize
>= maxsize
) {
3609 pos
= e
[i
].start
+ e
[i
].size
;
3611 } while (e
[i
-1].size
);
3617 /* FIXME assumes volume at offset 0 is the first volume in a
3620 if (start_extent
> 0)
3621 reserve
= IMSM_RESERVED_SECTORS
; /* gap between raid regions */
3625 if (maxsize
< reserve
)
3628 super
->create_offset
= ~((__u32
) 0);
3629 if (start
+ reserve
> super
->create_offset
)
3630 return 0; /* start overflows create_offset */
3631 super
->create_offset
= start
+ reserve
;
3633 return maxsize
- reserve
;
3636 static int is_raid_level_supported(const struct imsm_orom
*orom
, int level
, int raiddisks
)
3638 if (level
< 0 || level
== 6 || level
== 4)
3641 /* if we have an orom prevent invalid raid levels */
3644 case 0: return imsm_orom_has_raid0(orom
);
3647 return imsm_orom_has_raid1e(orom
);
3648 return imsm_orom_has_raid1(orom
) && raiddisks
== 2;
3649 case 10: return imsm_orom_has_raid10(orom
) && raiddisks
== 4;
3650 case 5: return imsm_orom_has_raid5(orom
) && raiddisks
> 2;
3653 return 1; /* not on an Intel RAID platform so anything goes */
3658 #define pr_vrb(fmt, arg...) (void) (verbose && fprintf(stderr, Name fmt, ##arg))
3660 validate_geometry_imsm_orom(struct intel_super
*super
, int level
, int layout
,
3661 int raiddisks
, int chunk
, int verbose
)
3663 if (!is_raid_level_supported(super
->orom
, level
, raiddisks
)) {
3664 pr_vrb(": platform does not support raid%d with %d disk%s\n",
3665 level
, raiddisks
, raiddisks
> 1 ? "s" : "");
3668 if (super
->orom
&& level
!= 1 &&
3669 !imsm_orom_has_chunk(super
->orom
, chunk
)) {
3670 pr_vrb(": platform does not support a chunk size of: %d\n", chunk
);
3673 if (layout
!= imsm_level_to_layout(level
)) {
3675 pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
3676 else if (level
== 10)
3677 pr_vrb(": imsm raid 10 only supports the n2 layout\n");
3679 pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
3687 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
3688 * FIX ME add ahci details
3690 static int validate_geometry_imsm_volume(struct supertype
*st
, int level
,
3691 int layout
, int raiddisks
, int chunk
,
3692 unsigned long long size
, char *dev
,
3693 unsigned long long *freesize
,
3697 struct intel_super
*super
= st
->sb
;
3698 struct imsm_super
*mpb
= super
->anchor
;
3700 unsigned long long pos
= 0;
3701 unsigned long long maxsize
;
3705 /* We must have the container info already read in. */
3709 if (!validate_geometry_imsm_orom(super
, level
, layout
, raiddisks
, chunk
, verbose
))
3713 /* General test: make sure there is space for
3714 * 'raiddisks' device extents of size 'size' at a given
3717 unsigned long long minsize
= size
;
3718 unsigned long long start_offset
= MaxSector
;
3721 minsize
= MPB_SECTOR_CNT
+ IMSM_RESERVED_SECTORS
;
3722 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
3727 e
= get_extents(super
, dl
);
3730 unsigned long long esize
;
3731 esize
= e
[i
].start
- pos
;
3732 if (esize
>= minsize
)
3734 if (found
&& start_offset
== MaxSector
) {
3737 } else if (found
&& pos
!= start_offset
) {
3741 pos
= e
[i
].start
+ e
[i
].size
;
3743 } while (e
[i
-1].size
);
3748 if (dcnt
< raiddisks
) {
3750 fprintf(stderr
, Name
": imsm: Not enough "
3751 "devices with space for this array "
3759 /* This device must be a member of the set */
3760 if (stat(dev
, &stb
) < 0)
3762 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
3764 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
3765 if (dl
->major
== major(stb
.st_rdev
) &&
3766 dl
->minor
== minor(stb
.st_rdev
))
3771 fprintf(stderr
, Name
": %s is not in the "
3772 "same imsm set\n", dev
);
3774 } else if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
) {
3775 /* If a volume is present then the current creation attempt
3776 * cannot incorporate new spares because the orom may not
3777 * understand this configuration (all member disks must be
3778 * members of each array in the container).
3780 fprintf(stderr
, Name
": %s is a spare and a volume"
3781 " is already defined for this container\n", dev
);
3782 fprintf(stderr
, Name
": The option-rom requires all member"
3783 " disks to be a member of all volumes\n");
3787 /* retrieve the largest free space block */
3788 e
= get_extents(super
, dl
);
3793 unsigned long long esize
;
3795 esize
= e
[i
].start
- pos
;
3796 if (esize
>= maxsize
)
3798 pos
= e
[i
].start
+ e
[i
].size
;
3800 } while (e
[i
-1].size
);
3805 fprintf(stderr
, Name
": unable to determine free space for: %s\n",
3809 if (maxsize
< size
) {
3811 fprintf(stderr
, Name
": %s not enough space (%llu < %llu)\n",
3812 dev
, maxsize
, size
);
3816 /* count total number of extents for merge */
3818 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3820 i
+= dl
->extent_cnt
;
3822 maxsize
= merge_extents(super
, i
);
3823 if (maxsize
< size
|| maxsize
== 0) {
3825 fprintf(stderr
, Name
": not enough space after merge (%llu < %llu)\n",
3830 *freesize
= maxsize
;
3835 static int reserve_space(struct supertype
*st
, int raiddisks
,
3836 unsigned long long size
, int chunk
,
3837 unsigned long long *freesize
)
3839 struct intel_super
*super
= st
->sb
;
3840 struct imsm_super
*mpb
= super
->anchor
;
3845 unsigned long long maxsize
;
3846 unsigned long long minsize
;
3850 /* find the largest common start free region of the possible disks */
3854 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
3860 /* don't activate new spares if we are orom constrained
3861 * and there is already a volume active in the container
3863 if (super
->orom
&& dl
->index
< 0 && mpb
->num_raid_devs
)
3866 e
= get_extents(super
, dl
);
3869 for (i
= 1; e
[i
-1].size
; i
++)
3877 maxsize
= merge_extents(super
, extent_cnt
);
3882 if (cnt
< raiddisks
||
3883 (super
->orom
&& used
&& used
!= raiddisks
) ||
3884 maxsize
< minsize
||
3886 fprintf(stderr
, Name
": not enough devices with space to create array.\n");
3887 return 0; /* No enough free spaces large enough */
3899 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
3901 dl
->raiddisk
= cnt
++;
3908 static int validate_geometry_imsm(struct supertype
*st
, int level
, int layout
,
3909 int raiddisks
, int chunk
, unsigned long long size
,
3910 char *dev
, unsigned long long *freesize
,
3917 /* if given unused devices create a container
3918 * if given given devices in a container create a member volume
3920 if (level
== LEVEL_CONTAINER
) {
3921 /* Must be a fresh device to add to a container */
3922 return validate_geometry_imsm_container(st
, level
, layout
,
3923 raiddisks
, chunk
, size
,
3929 if (st
->sb
&& freesize
) {
3930 /* we are being asked to automatically layout a
3931 * new volume based on the current contents of
3932 * the container. If the the parameters can be
3933 * satisfied reserve_space will record the disks,
3934 * start offset, and size of the volume to be
3935 * created. add_to_super and getinfo_super
3936 * detect when autolayout is in progress.
3938 if (!validate_geometry_imsm_orom(st
->sb
, level
, layout
,
3942 return reserve_space(st
, raiddisks
, size
, chunk
, freesize
);
3947 /* creating in a given container */
3948 return validate_geometry_imsm_volume(st
, level
, layout
,
3949 raiddisks
, chunk
, size
,
3950 dev
, freesize
, verbose
);
3953 /* This device needs to be a device in an 'imsm' container */
3954 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
3958 Name
": Cannot create this array on device %s\n",
3963 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
3965 fprintf(stderr
, Name
": Cannot open %s: %s\n",
3966 dev
, strerror(errno
));
3969 /* Well, it is in use by someone, maybe an 'imsm' container. */
3970 cfd
= open_container(fd
);
3974 fprintf(stderr
, Name
": Cannot use %s: It is busy\n",
3978 sra
= sysfs_read(cfd
, 0, GET_VERSION
);
3979 if (sra
&& sra
->array
.major_version
== -1 &&
3980 strcmp(sra
->text_version
, "imsm") == 0)
3984 /* This is a member of a imsm container. Load the container
3985 * and try to create a volume
3987 struct intel_super
*super
;
3989 if (load_super_imsm_all(st
, cfd
, (void **) &super
, NULL
, 1) == 0) {
3991 st
->container_dev
= fd2devnum(cfd
);
3993 return validate_geometry_imsm_volume(st
, level
, layout
,
4001 fprintf(stderr
, Name
": failed container membership check\n");
4006 #endif /* MDASSEMBLE */
4008 static int is_rebuilding(struct imsm_dev
*dev
)
4010 struct imsm_map
*migr_map
;
4012 if (!dev
->vol
.migr_state
)
4015 if (migr_type(dev
) != MIGR_REBUILD
)
4018 migr_map
= get_imsm_map(dev
, 1);
4020 if (migr_map
->map_state
== IMSM_T_STATE_DEGRADED
)
4026 static void update_recovery_start(struct imsm_dev
*dev
, struct mdinfo
*array
)
4028 struct mdinfo
*rebuild
= NULL
;
4032 if (!is_rebuilding(dev
))
4035 /* Find the rebuild target, but punt on the dual rebuild case */
4036 for (d
= array
->devs
; d
; d
= d
->next
)
4037 if (d
->recovery_start
== 0) {
4043 units
= __le32_to_cpu(dev
->vol
.curr_migr_unit
);
4044 rebuild
->recovery_start
= units
* blocks_per_migr_unit(dev
);
4048 static struct mdinfo
*container_content_imsm(struct supertype
*st
)
4050 /* Given a container loaded by load_super_imsm_all,
4051 * extract information about all the arrays into
4054 * For each imsm_dev create an mdinfo, fill it in,
4055 * then look for matching devices in super->disks
4056 * and create appropriate device mdinfo.
4058 struct intel_super
*super
= st
->sb
;
4059 struct imsm_super
*mpb
= super
->anchor
;
4060 struct mdinfo
*rest
= NULL
;
4063 /* do not assemble arrays that might have bad blocks */
4064 if (imsm_bbm_log_size(super
->anchor
)) {
4065 fprintf(stderr
, Name
": BBM log found in metadata. "
4066 "Cannot activate array(s).\n");
4070 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4071 struct imsm_dev
*dev
= get_imsm_dev(super
, i
);
4072 struct imsm_map
*map
= get_imsm_map(dev
, 0);
4073 struct mdinfo
*this;
4076 /* do not publish arrays that are in the middle of an
4077 * unsupported migration
4079 if (dev
->vol
.migr_state
&&
4080 (migr_type(dev
) == MIGR_GEN_MIGR
||
4081 migr_type(dev
) == MIGR_STATE_CHANGE
)) {
4082 fprintf(stderr
, Name
": cannot assemble volume '%.16s':"
4083 " unsupported migration in progress\n",
4088 this = malloc(sizeof(*this));
4090 fprintf(stderr
, Name
": failed to allocate %zu bytes\n",
4094 memset(this, 0, sizeof(*this));
4097 super
->current_vol
= i
;
4098 getinfo_super_imsm_volume(st
, this);
4099 for (slot
= 0 ; slot
< map
->num_members
; slot
++) {
4100 unsigned long long recovery_start
;
4101 struct mdinfo
*info_d
;
4108 idx
= get_imsm_disk_idx(dev
, slot
);
4109 ord
= get_imsm_ord_tbl_ent(dev
, slot
);
4110 for (d
= super
->disks
; d
; d
= d
->next
)
4111 if (d
->index
== idx
)
4114 recovery_start
= MaxSector
;
4117 if (d
&& is_failed(&d
->disk
))
4119 if (ord
& IMSM_ORD_REBUILD
)
4123 * if we skip some disks the array will be assmebled degraded;
4124 * reset resync start to avoid a dirty-degraded
4125 * situation when performing the intial sync
4127 * FIXME handle dirty degraded
4129 if ((skip
|| recovery_start
== 0) && !dev
->vol
.dirty
)
4130 this->resync_start
= MaxSector
;
4134 info_d
= calloc(1, sizeof(*info_d
));
4136 fprintf(stderr
, Name
": failed to allocate disk"
4137 " for volume %.16s\n", dev
->volume
);
4138 info_d
= this->devs
;
4140 struct mdinfo
*d
= info_d
->next
;
4149 info_d
->next
= this->devs
;
4150 this->devs
= info_d
;
4152 info_d
->disk
.number
= d
->index
;
4153 info_d
->disk
.major
= d
->major
;
4154 info_d
->disk
.minor
= d
->minor
;
4155 info_d
->disk
.raid_disk
= slot
;
4156 info_d
->recovery_start
= recovery_start
;
4158 if (info_d
->recovery_start
== MaxSector
)
4159 this->array
.working_disks
++;
4161 info_d
->events
= __le32_to_cpu(mpb
->generation_num
);
4162 info_d
->data_offset
= __le32_to_cpu(map
->pba_of_lba0
);
4163 info_d
->component_size
= __le32_to_cpu(map
->blocks_per_member
);
4165 /* now that the disk list is up-to-date fixup recovery_start */
4166 update_recovery_start(dev
, this);
4175 static int imsm_open_new(struct supertype
*c
, struct active_array
*a
,
4178 struct intel_super
*super
= c
->sb
;
4179 struct imsm_super
*mpb
= super
->anchor
;
4181 if (atoi(inst
) >= mpb
->num_raid_devs
) {
4182 fprintf(stderr
, "%s: subarry index %d, out of range\n",
4183 __func__
, atoi(inst
));
4187 dprintf("imsm: open_new %s\n", inst
);
4188 a
->info
.container_member
= atoi(inst
);
4192 static __u8
imsm_check_degraded(struct intel_super
*super
, struct imsm_dev
*dev
, int failed
)
4194 struct imsm_map
*map
= get_imsm_map(dev
, 0);
4197 return map
->map_state
== IMSM_T_STATE_UNINITIALIZED
?
4198 IMSM_T_STATE_UNINITIALIZED
: IMSM_T_STATE_NORMAL
;
4200 switch (get_imsm_raid_level(map
)) {
4202 return IMSM_T_STATE_FAILED
;
4205 if (failed
< map
->num_members
)
4206 return IMSM_T_STATE_DEGRADED
;
4208 return IMSM_T_STATE_FAILED
;
4213 * check to see if any mirrors have failed, otherwise we
4214 * are degraded. Even numbered slots are mirrored on
4218 /* gcc -Os complains that this is unused */
4219 int insync
= insync
;
4221 for (i
= 0; i
< map
->num_members
; i
++) {
4222 __u32 ord
= get_imsm_ord_tbl_ent(dev
, i
);
4223 int idx
= ord_to_idx(ord
);
4224 struct imsm_disk
*disk
;
4226 /* reset the potential in-sync count on even-numbered
4227 * slots. num_copies is always 2 for imsm raid10
4232 disk
= get_imsm_disk(super
, idx
);
4233 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
4236 /* no in-sync disks left in this mirror the
4240 return IMSM_T_STATE_FAILED
;
4243 return IMSM_T_STATE_DEGRADED
;
4247 return IMSM_T_STATE_DEGRADED
;
4249 return IMSM_T_STATE_FAILED
;
4255 return map
->map_state
;
4258 static int imsm_count_failed(struct intel_super
*super
, struct imsm_dev
*dev
)
4262 struct imsm_disk
*disk
;
4263 struct imsm_map
*map
= get_imsm_map(dev
, 0);
4264 struct imsm_map
*prev
= get_imsm_map(dev
, dev
->vol
.migr_state
);
4268 /* at the beginning of migration we set IMSM_ORD_REBUILD on
4269 * disks that are being rebuilt. New failures are recorded to
4270 * map[0]. So we look through all the disks we started with and
4271 * see if any failures are still present, or if any new ones
4274 * FIXME add support for online capacity expansion and
4275 * raid-level-migration
4277 for (i
= 0; i
< prev
->num_members
; i
++) {
4278 ord
= __le32_to_cpu(prev
->disk_ord_tbl
[i
]);
4279 ord
|= __le32_to_cpu(map
->disk_ord_tbl
[i
]);
4280 idx
= ord_to_idx(ord
);
4282 disk
= get_imsm_disk(super
, idx
);
4283 if (!disk
|| is_failed(disk
) || ord
& IMSM_ORD_REBUILD
)
4290 static int is_resyncing(struct imsm_dev
*dev
)
4292 struct imsm_map
*migr_map
;
4294 if (!dev
->vol
.migr_state
)
4297 if (migr_type(dev
) == MIGR_INIT
||
4298 migr_type(dev
) == MIGR_REPAIR
)
4301 migr_map
= get_imsm_map(dev
, 1);
4303 if (migr_map
->map_state
== IMSM_T_STATE_NORMAL
)
4309 /* return true if we recorded new information */
4310 static int mark_failure(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
4314 struct imsm_map
*map
;
4316 /* new failures are always set in map[0] */
4317 map
= get_imsm_map(dev
, 0);
4319 slot
= get_imsm_disk_slot(map
, idx
);
4323 ord
= __le32_to_cpu(map
->disk_ord_tbl
[slot
]);
4324 if (is_failed(disk
) && (ord
& IMSM_ORD_REBUILD
))
4327 disk
->status
|= FAILED_DISK
;
4328 disk
->status
&= ~CONFIGURED_DISK
;
4329 set_imsm_ord_tbl_ent(map
, slot
, idx
| IMSM_ORD_REBUILD
);
4330 if (~map
->failed_disk_num
== 0)
4331 map
->failed_disk_num
= slot
;
4335 static void mark_missing(struct imsm_dev
*dev
, struct imsm_disk
*disk
, int idx
)
4337 mark_failure(dev
, disk
, idx
);
4339 if (disk
->scsi_id
== __cpu_to_le32(~(__u32
)0))
4342 disk
->scsi_id
= __cpu_to_le32(~(__u32
)0);
4343 memmove(&disk
->serial
[0], &disk
->serial
[1], MAX_RAID_SERIAL_LEN
- 1);
4346 /* Handle dirty -> clean transititions and resync. Degraded and rebuild
4347 * states are handled in imsm_set_disk() with one exception, when a
4348 * resync is stopped due to a new failure this routine will set the
4349 * 'degraded' state for the array.
4351 static int imsm_set_array_state(struct active_array
*a
, int consistent
)
4353 int inst
= a
->info
.container_member
;
4354 struct intel_super
*super
= a
->container
->sb
;
4355 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
4356 struct imsm_map
*map
= get_imsm_map(dev
, 0);
4357 int failed
= imsm_count_failed(super
, dev
);
4358 __u8 map_state
= imsm_check_degraded(super
, dev
, failed
);
4359 __u32 blocks_per_unit
;
4361 /* before we activate this array handle any missing disks */
4362 if (consistent
== 2 && super
->missing
) {
4365 dprintf("imsm: mark missing\n");
4366 end_migration(dev
, map_state
);
4367 for (dl
= super
->missing
; dl
; dl
= dl
->next
)
4368 mark_missing(dev
, &dl
->disk
, dl
->index
);
4369 super
->updates_pending
++;
4372 if (consistent
== 2 &&
4373 (!is_resync_complete(&a
->info
) ||
4374 map_state
!= IMSM_T_STATE_NORMAL
||
4375 dev
->vol
.migr_state
))
4378 if (is_resync_complete(&a
->info
)) {
4379 /* complete intialization / resync,
4380 * recovery and interrupted recovery is completed in
4383 if (is_resyncing(dev
)) {
4384 dprintf("imsm: mark resync done\n");
4385 end_migration(dev
, map_state
);
4386 super
->updates_pending
++;
4387 a
->last_checkpoint
= 0;
4389 } else if (!is_resyncing(dev
) && !failed
) {
4390 /* mark the start of the init process if nothing is failed */
4391 dprintf("imsm: mark resync start\n");
4392 if (map
->map_state
== IMSM_T_STATE_UNINITIALIZED
)
4393 migrate(dev
, IMSM_T_STATE_NORMAL
, MIGR_INIT
);
4395 migrate(dev
, IMSM_T_STATE_NORMAL
, MIGR_REPAIR
);
4396 super
->updates_pending
++;
4399 /* check if we can update curr_migr_unit from resync_start, recovery_start */
4400 blocks_per_unit
= blocks_per_migr_unit(dev
);
4401 if (blocks_per_unit
&& failed
<= 1) {
4405 if (migr_type(dev
) == MIGR_REBUILD
)
4406 units
= min_recovery_start(&a
->info
) / blocks_per_unit
;
4408 units
= a
->info
.resync_start
/ blocks_per_unit
;
4411 /* check that we did not overflow 32-bits, and that
4412 * curr_migr_unit needs updating
4414 if (units32
== units
&&
4415 __le32_to_cpu(dev
->vol
.curr_migr_unit
) != units32
) {
4416 dprintf("imsm: mark checkpoint (%u)\n", units32
);
4417 dev
->vol
.curr_migr_unit
= __cpu_to_le32(units32
);
4418 super
->updates_pending
++;
4422 /* mark dirty / clean */
4423 if (dev
->vol
.dirty
!= !consistent
) {
4424 dprintf("imsm: mark '%s'\n", consistent
? "clean" : "dirty");
4429 super
->updates_pending
++;
4434 static void imsm_set_disk(struct active_array
*a
, int n
, int state
)
4436 int inst
= a
->info
.container_member
;
4437 struct intel_super
*super
= a
->container
->sb
;
4438 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
4439 struct imsm_map
*map
= get_imsm_map(dev
, 0);
4440 struct imsm_disk
*disk
;
4445 if (n
> map
->num_members
)
4446 fprintf(stderr
, "imsm: set_disk %d out of range 0..%d\n",
4447 n
, map
->num_members
- 1);
4452 dprintf("imsm: set_disk %d:%x\n", n
, state
);
4454 ord
= get_imsm_ord_tbl_ent(dev
, n
);
4455 disk
= get_imsm_disk(super
, ord_to_idx(ord
));
4457 /* check for new failures */
4458 if (state
& DS_FAULTY
) {
4459 if (mark_failure(dev
, disk
, ord_to_idx(ord
)))
4460 super
->updates_pending
++;
4463 /* check if in_sync */
4464 if (state
& DS_INSYNC
&& ord
& IMSM_ORD_REBUILD
&& is_rebuilding(dev
)) {
4465 struct imsm_map
*migr_map
= get_imsm_map(dev
, 1);
4467 set_imsm_ord_tbl_ent(migr_map
, n
, ord_to_idx(ord
));
4468 super
->updates_pending
++;
4471 failed
= imsm_count_failed(super
, dev
);
4472 map_state
= imsm_check_degraded(super
, dev
, failed
);
4474 /* check if recovery complete, newly degraded, or failed */
4475 if (map_state
== IMSM_T_STATE_NORMAL
&& is_rebuilding(dev
)) {
4476 end_migration(dev
, map_state
);
4477 map
= get_imsm_map(dev
, 0);
4478 map
->failed_disk_num
= ~0;
4479 super
->updates_pending
++;
4480 a
->last_checkpoint
= 0;
4481 } else if (map_state
== IMSM_T_STATE_DEGRADED
&&
4482 map
->map_state
!= map_state
&&
4483 !dev
->vol
.migr_state
) {
4484 dprintf("imsm: mark degraded\n");
4485 map
->map_state
= map_state
;
4486 super
->updates_pending
++;
4487 a
->last_checkpoint
= 0;
4488 } else if (map_state
== IMSM_T_STATE_FAILED
&&
4489 map
->map_state
!= map_state
) {
4490 dprintf("imsm: mark failed\n");
4491 end_migration(dev
, map_state
);
4492 super
->updates_pending
++;
4493 a
->last_checkpoint
= 0;
4497 static int store_imsm_mpb(int fd
, struct imsm_super
*mpb
)
4500 __u32 mpb_size
= __le32_to_cpu(mpb
->mpb_size
);
4501 unsigned long long dsize
;
4502 unsigned long long sectors
;
4504 get_dev_size(fd
, NULL
, &dsize
);
4506 if (mpb_size
> 512) {
4507 /* -1 to account for anchor */
4508 sectors
= mpb_sectors(mpb
) - 1;
4510 /* write the extended mpb to the sectors preceeding the anchor */
4511 if (lseek64(fd
, dsize
- (512 * (2 + sectors
)), SEEK_SET
) < 0)
4514 if (write(fd
, buf
+ 512, 512 * sectors
) != 512 * sectors
)
4518 /* first block is stored on second to last sector of the disk */
4519 if (lseek64(fd
, dsize
- (512 * 2), SEEK_SET
) < 0)
4522 if (write(fd
, buf
, 512) != 512)
4528 static void imsm_sync_metadata(struct supertype
*container
)
4530 struct intel_super
*super
= container
->sb
;
4532 if (!super
->updates_pending
)
4535 write_super_imsm(super
, 0);
4537 super
->updates_pending
= 0;
4540 static struct dl
*imsm_readd(struct intel_super
*super
, int idx
, struct active_array
*a
)
4542 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
4543 int i
= get_imsm_disk_idx(dev
, idx
);
4546 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4550 if (dl
&& is_failed(&dl
->disk
))
4554 dprintf("%s: found %x:%x\n", __func__
, dl
->major
, dl
->minor
);
4559 static struct dl
*imsm_add_spare(struct intel_super
*super
, int slot
,
4560 struct active_array
*a
, int activate_new
)
4562 struct imsm_dev
*dev
= get_imsm_dev(super
, a
->info
.container_member
);
4563 int idx
= get_imsm_disk_idx(dev
, slot
);
4564 struct imsm_super
*mpb
= super
->anchor
;
4565 struct imsm_map
*map
;
4566 unsigned long long pos
;
4575 for (dl
= super
->disks
; dl
; dl
= dl
->next
) {
4576 /* If in this array, skip */
4577 for (d
= a
->info
.devs
; d
; d
= d
->next
)
4578 if (d
->state_fd
>= 0 &&
4579 d
->disk
.major
== dl
->major
&&
4580 d
->disk
.minor
== dl
->minor
) {
4581 dprintf("%x:%x already in array\n", dl
->major
, dl
->minor
);
4587 /* skip in use or failed drives */
4588 if (is_failed(&dl
->disk
) || idx
== dl
->index
||
4590 dprintf("%x:%x status (failed: %d index: %d)\n",
4591 dl
->major
, dl
->minor
, is_failed(&dl
->disk
), idx
);
4595 /* skip pure spares when we are looking for partially
4596 * assimilated drives
4598 if (dl
->index
== -1 && !activate_new
)
4601 /* Does this unused device have the requisite free space?
4602 * It needs to be able to cover all member volumes
4604 ex
= get_extents(super
, dl
);
4606 dprintf("cannot get extents\n");
4609 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
4610 dev
= get_imsm_dev(super
, i
);
4611 map
= get_imsm_map(dev
, 0);
4613 /* check if this disk is already a member of
4616 if (get_imsm_disk_slot(map
, dl
->index
) >= 0)
4622 array_start
= __le32_to_cpu(map
->pba_of_lba0
);
4623 array_end
= array_start
+
4624 __le32_to_cpu(map
->blocks_per_member
) - 1;
4627 /* check that we can start at pba_of_lba0 with
4628 * blocks_per_member of space
4630 if (array_start
>= pos
&& array_end
< ex
[j
].start
) {
4634 pos
= ex
[j
].start
+ ex
[j
].size
;
4636 } while (ex
[j
-1].size
);
4643 if (i
< mpb
->num_raid_devs
) {
4644 dprintf("%x:%x does not have %u to %u available\n",
4645 dl
->major
, dl
->minor
, array_start
, array_end
);
4655 static struct mdinfo
*imsm_activate_spare(struct active_array
*a
,
4656 struct metadata_update
**updates
)
4659 * Find a device with unused free space and use it to replace a
4660 * failed/vacant region in an array. We replace failed regions one a
4661 * array at a time. The result is that a new spare disk will be added
4662 * to the first failed array and after the monitor has finished
4663 * propagating failures the remainder will be consumed.
4665 * FIXME add a capability for mdmon to request spares from another
4669 struct intel_super
*super
= a
->container
->sb
;
4670 int inst
= a
->info
.container_member
;
4671 struct imsm_dev
*dev
= get_imsm_dev(super
, inst
);
4672 struct imsm_map
*map
= get_imsm_map(dev
, 0);
4673 int failed
= a
->info
.array
.raid_disks
;
4674 struct mdinfo
*rv
= NULL
;
4677 struct metadata_update
*mu
;
4679 struct imsm_update_activate_spare
*u
;
4683 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
4684 if ((d
->curr_state
& DS_FAULTY
) &&
4686 /* wait for Removal to happen */
4688 if (d
->state_fd
>= 0)
4692 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
4693 inst
, failed
, a
->info
.array
.raid_disks
, a
->info
.array
.level
);
4694 if (imsm_check_degraded(super
, dev
, failed
) != IMSM_T_STATE_DEGRADED
)
4697 /* For each slot, if it is not working, find a spare */
4698 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
4699 for (d
= a
->info
.devs
; d
; d
= d
->next
)
4700 if (d
->disk
.raid_disk
== i
)
4702 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
4703 if (d
&& (d
->state_fd
>= 0))
4707 * OK, this device needs recovery. Try to re-add the
4708 * previous occupant of this slot, if this fails see if
4709 * we can continue the assimilation of a spare that was
4710 * partially assimilated, finally try to activate a new
4713 dl
= imsm_readd(super
, i
, a
);
4715 dl
= imsm_add_spare(super
, i
, a
, 0);
4717 dl
= imsm_add_spare(super
, i
, a
, 1);
4721 /* found a usable disk with enough space */
4722 di
= malloc(sizeof(*di
));
4725 memset(di
, 0, sizeof(*di
));
4727 /* dl->index will be -1 in the case we are activating a
4728 * pristine spare. imsm_process_update() will create a
4729 * new index in this case. Once a disk is found to be
4730 * failed in all member arrays it is kicked from the
4733 di
->disk
.number
= dl
->index
;
4735 /* (ab)use di->devs to store a pointer to the device
4738 di
->devs
= (struct mdinfo
*) dl
;
4740 di
->disk
.raid_disk
= i
;
4741 di
->disk
.major
= dl
->major
;
4742 di
->disk
.minor
= dl
->minor
;
4744 di
->recovery_start
= 0;
4745 di
->data_offset
= __le32_to_cpu(map
->pba_of_lba0
);
4746 di
->component_size
= a
->info
.component_size
;
4747 di
->container_member
= inst
;
4748 super
->random
= random32();
4752 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
4753 i
, di
->data_offset
);
4759 /* No spares found */
4761 /* Now 'rv' has a list of devices to return.
4762 * Create a metadata_update record to update the
4763 * disk_ord_tbl for the array
4765 mu
= malloc(sizeof(*mu
));
4767 mu
->buf
= malloc(sizeof(struct imsm_update_activate_spare
) * num_spares
);
4768 if (mu
->buf
== NULL
) {
4775 struct mdinfo
*n
= rv
->next
;
4784 mu
->len
= sizeof(struct imsm_update_activate_spare
) * num_spares
;
4785 mu
->next
= *updates
;
4786 u
= (struct imsm_update_activate_spare
*) mu
->buf
;
4788 for (di
= rv
; di
; di
= di
->next
) {
4789 u
->type
= update_activate_spare
;
4790 u
->dl
= (struct dl
*) di
->devs
;
4792 u
->slot
= di
->disk
.raid_disk
;
4803 static int disks_overlap(struct intel_super
*super
, int idx
, struct imsm_update_create_array
*u
)
4805 struct imsm_dev
*dev
= get_imsm_dev(super
, idx
);
4806 struct imsm_map
*map
= get_imsm_map(dev
, 0);
4807 struct imsm_map
*new_map
= get_imsm_map(&u
->dev
, 0);
4808 struct disk_info
*inf
= get_disk_info(u
);
4809 struct imsm_disk
*disk
;
4813 for (i
= 0; i
< map
->num_members
; i
++) {
4814 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
));
4815 for (j
= 0; j
< new_map
->num_members
; j
++)
4816 if (serialcmp(disk
->serial
, inf
[j
].serial
) == 0)
4823 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, int index
);
4825 static void imsm_process_update(struct supertype
*st
,
4826 struct metadata_update
*update
)
4829 * crack open the metadata_update envelope to find the update record
4830 * update can be one of:
4831 * update_activate_spare - a spare device has replaced a failed
4832 * device in an array, update the disk_ord_tbl. If this disk is
4833 * present in all member arrays then also clear the SPARE_DISK
4836 struct intel_super
*super
= st
->sb
;
4837 struct imsm_super
*mpb
;
4838 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
4840 /* update requires a larger buf but the allocation failed */
4841 if (super
->next_len
&& !super
->next_buf
) {
4842 super
->next_len
= 0;
4846 if (super
->next_buf
) {
4847 memcpy(super
->next_buf
, super
->buf
, super
->len
);
4849 super
->len
= super
->next_len
;
4850 super
->buf
= super
->next_buf
;
4852 super
->next_len
= 0;
4853 super
->next_buf
= NULL
;
4856 mpb
= super
->anchor
;
4859 case update_activate_spare
: {
4860 struct imsm_update_activate_spare
*u
= (void *) update
->buf
;
4861 struct imsm_dev
*dev
= get_imsm_dev(super
, u
->array
);
4862 struct imsm_map
*map
= get_imsm_map(dev
, 0);
4863 struct imsm_map
*migr_map
;
4864 struct active_array
*a
;
4865 struct imsm_disk
*disk
;
4870 int victim
= get_imsm_disk_idx(dev
, u
->slot
);
4873 for (dl
= super
->disks
; dl
; dl
= dl
->next
)
4878 fprintf(stderr
, "error: imsm_activate_spare passed "
4879 "an unknown disk (index: %d)\n",
4884 super
->updates_pending
++;
4886 /* count failures (excluding rebuilds and the victim)
4887 * to determine map[0] state
4890 for (i
= 0; i
< map
->num_members
; i
++) {
4893 disk
= get_imsm_disk(super
, get_imsm_disk_idx(dev
, i
));
4894 if (!disk
|| is_failed(disk
))
4898 /* adding a pristine spare, assign a new index */
4899 if (dl
->index
< 0) {
4900 dl
->index
= super
->anchor
->num_disks
;
4901 super
->anchor
->num_disks
++;
4904 disk
->status
|= CONFIGURED_DISK
;
4905 disk
->status
&= ~SPARE_DISK
;
4908 to_state
= imsm_check_degraded(super
, dev
, failed
);
4909 map
->map_state
= IMSM_T_STATE_DEGRADED
;
4910 migrate(dev
, to_state
, MIGR_REBUILD
);
4911 migr_map
= get_imsm_map(dev
, 1);
4912 set_imsm_ord_tbl_ent(map
, u
->slot
, dl
->index
);
4913 set_imsm_ord_tbl_ent(migr_map
, u
->slot
, dl
->index
| IMSM_ORD_REBUILD
);
4915 /* update the family_num to mark a new container
4916 * generation, being careful to record the existing
4917 * family_num in orig_family_num to clean up after
4918 * earlier mdadm versions that neglected to set it.
4920 if (mpb
->orig_family_num
== 0)
4921 mpb
->orig_family_num
= mpb
->family_num
;
4922 mpb
->family_num
+= super
->random
;
4924 /* count arrays using the victim in the metadata */
4926 for (a
= st
->arrays
; a
; a
= a
->next
) {
4927 dev
= get_imsm_dev(super
, a
->info
.container_member
);
4928 map
= get_imsm_map(dev
, 0);
4930 if (get_imsm_disk_slot(map
, victim
) >= 0)
4934 /* delete the victim if it is no longer being
4940 /* We know that 'manager' isn't touching anything,
4941 * so it is safe to delete
4943 for (dlp
= &super
->disks
; *dlp
; dlp
= &(*dlp
)->next
)
4944 if ((*dlp
)->index
== victim
)
4947 /* victim may be on the missing list */
4949 for (dlp
= &super
->missing
; *dlp
; dlp
= &(*dlp
)->next
)
4950 if ((*dlp
)->index
== victim
)
4952 imsm_delete(super
, dlp
, victim
);
4956 case update_create_array
: {
4957 /* someone wants to create a new array, we need to be aware of
4958 * a few races/collisions:
4959 * 1/ 'Create' called by two separate instances of mdadm
4960 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
4961 * devices that have since been assimilated via
4963 * In the event this update can not be carried out mdadm will
4964 * (FIX ME) notice that its update did not take hold.
4966 struct imsm_update_create_array
*u
= (void *) update
->buf
;
4967 struct intel_dev
*dv
;
4968 struct imsm_dev
*dev
;
4969 struct imsm_map
*map
, *new_map
;
4970 unsigned long long start
, end
;
4971 unsigned long long new_start
, new_end
;
4973 struct disk_info
*inf
;
4976 /* handle racing creates: first come first serve */
4977 if (u
->dev_idx
< mpb
->num_raid_devs
) {
4978 dprintf("%s: subarray %d already defined\n",
4979 __func__
, u
->dev_idx
);
4983 /* check update is next in sequence */
4984 if (u
->dev_idx
!= mpb
->num_raid_devs
) {
4985 dprintf("%s: can not create array %d expected index %d\n",
4986 __func__
, u
->dev_idx
, mpb
->num_raid_devs
);
4990 new_map
= get_imsm_map(&u
->dev
, 0);
4991 new_start
= __le32_to_cpu(new_map
->pba_of_lba0
);
4992 new_end
= new_start
+ __le32_to_cpu(new_map
->blocks_per_member
);
4993 inf
= get_disk_info(u
);
4995 /* handle activate_spare versus create race:
4996 * check to make sure that overlapping arrays do not include
4999 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5000 dev
= get_imsm_dev(super
, i
);
5001 map
= get_imsm_map(dev
, 0);
5002 start
= __le32_to_cpu(map
->pba_of_lba0
);
5003 end
= start
+ __le32_to_cpu(map
->blocks_per_member
);
5004 if ((new_start
>= start
&& new_start
<= end
) ||
5005 (start
>= new_start
&& start
<= new_end
))
5010 if (disks_overlap(super
, i
, u
)) {
5011 dprintf("%s: arrays overlap\n", __func__
);
5016 /* check that prepare update was successful */
5017 if (!update
->space
) {
5018 dprintf("%s: prepare update failed\n", __func__
);
5022 /* check that all disks are still active before committing
5023 * changes. FIXME: could we instead handle this by creating a
5024 * degraded array? That's probably not what the user expects,
5025 * so better to drop this update on the floor.
5027 for (i
= 0; i
< new_map
->num_members
; i
++) {
5028 dl
= serial_to_dl(inf
[i
].serial
, super
);
5030 dprintf("%s: disk disappeared\n", __func__
);
5035 super
->updates_pending
++;
5037 /* convert spares to members and fixup ord_tbl */
5038 for (i
= 0; i
< new_map
->num_members
; i
++) {
5039 dl
= serial_to_dl(inf
[i
].serial
, super
);
5040 if (dl
->index
== -1) {
5041 dl
->index
= mpb
->num_disks
;
5043 dl
->disk
.status
|= CONFIGURED_DISK
;
5044 dl
->disk
.status
&= ~SPARE_DISK
;
5046 set_imsm_ord_tbl_ent(new_map
, i
, dl
->index
);
5051 update
->space
= NULL
;
5052 imsm_copy_dev(dev
, &u
->dev
);
5053 dv
->index
= u
->dev_idx
;
5054 dv
->next
= super
->devlist
;
5055 super
->devlist
= dv
;
5056 mpb
->num_raid_devs
++;
5058 imsm_update_version_info(super
);
5061 /* mdmon knows how to release update->space, but not
5062 * ((struct intel_dev *) update->space)->dev
5064 if (update
->space
) {
5070 case update_add_disk
:
5072 /* we may be able to repair some arrays if disks are
5075 struct active_array
*a
;
5077 super
->updates_pending
++;
5078 for (a
= st
->arrays
; a
; a
= a
->next
)
5079 a
->check_degraded
= 1;
5081 /* add some spares to the metadata */
5082 while (super
->add
) {
5086 super
->add
= al
->next
;
5087 al
->next
= super
->disks
;
5089 dprintf("%s: added %x:%x\n",
5090 __func__
, al
->major
, al
->minor
);
5097 static void imsm_prepare_update(struct supertype
*st
,
5098 struct metadata_update
*update
)
5101 * Allocate space to hold new disk entries, raid-device entries or a new
5102 * mpb if necessary. The manager synchronously waits for updates to
5103 * complete in the monitor, so new mpb buffers allocated here can be
5104 * integrated by the monitor thread without worrying about live pointers
5105 * in the manager thread.
5107 enum imsm_update_type type
= *(enum imsm_update_type
*) update
->buf
;
5108 struct intel_super
*super
= st
->sb
;
5109 struct imsm_super
*mpb
= super
->anchor
;
5114 case update_create_array
: {
5115 struct imsm_update_create_array
*u
= (void *) update
->buf
;
5116 struct intel_dev
*dv
;
5117 struct imsm_dev
*dev
= &u
->dev
;
5118 struct imsm_map
*map
= get_imsm_map(dev
, 0);
5120 struct disk_info
*inf
;
5124 inf
= get_disk_info(u
);
5125 len
= sizeof_imsm_dev(dev
, 1);
5126 /* allocate a new super->devlist entry */
5127 dv
= malloc(sizeof(*dv
));
5129 dv
->dev
= malloc(len
);
5134 update
->space
= NULL
;
5138 /* count how many spares will be converted to members */
5139 for (i
= 0; i
< map
->num_members
; i
++) {
5140 dl
= serial_to_dl(inf
[i
].serial
, super
);
5142 /* hmm maybe it failed?, nothing we can do about
5147 if (count_memberships(dl
, super
) == 0)
5150 len
+= activate
* sizeof(struct imsm_disk
);
5157 /* check if we need a larger metadata buffer */
5158 if (super
->next_buf
)
5159 buf_len
= super
->next_len
;
5161 buf_len
= super
->len
;
5163 if (__le32_to_cpu(mpb
->mpb_size
) + len
> buf_len
) {
5164 /* ok we need a larger buf than what is currently allocated
5165 * if this allocation fails process_update will notice that
5166 * ->next_len is set and ->next_buf is NULL
5168 buf_len
= ROUND_UP(__le32_to_cpu(mpb
->mpb_size
) + len
, 512);
5169 if (super
->next_buf
)
5170 free(super
->next_buf
);
5172 super
->next_len
= buf_len
;
5173 if (posix_memalign(&super
->next_buf
, 512, buf_len
) == 0)
5174 memset(super
->next_buf
, 0, buf_len
);
5176 super
->next_buf
= NULL
;
5180 /* must be called while manager is quiesced */
5181 static void imsm_delete(struct intel_super
*super
, struct dl
**dlp
, int index
)
5183 struct imsm_super
*mpb
= super
->anchor
;
5185 struct imsm_dev
*dev
;
5186 struct imsm_map
*map
;
5187 int i
, j
, num_members
;
5190 dprintf("%s: deleting device[%d] from imsm_super\n",
5193 /* shift all indexes down one */
5194 for (iter
= super
->disks
; iter
; iter
= iter
->next
)
5195 if (iter
->index
> index
)
5197 for (iter
= super
->missing
; iter
; iter
= iter
->next
)
5198 if (iter
->index
> index
)
5201 for (i
= 0; i
< mpb
->num_raid_devs
; i
++) {
5202 dev
= get_imsm_dev(super
, i
);
5203 map
= get_imsm_map(dev
, 0);
5204 num_members
= map
->num_members
;
5205 for (j
= 0; j
< num_members
; j
++) {
5206 /* update ord entries being careful not to propagate
5207 * ord-flags to the first map
5209 ord
= get_imsm_ord_tbl_ent(dev
, j
);
5211 if (ord_to_idx(ord
) <= index
)
5214 map
= get_imsm_map(dev
, 0);
5215 set_imsm_ord_tbl_ent(map
, j
, ord_to_idx(ord
- 1));
5216 map
= get_imsm_map(dev
, 1);
5218 set_imsm_ord_tbl_ent(map
, j
, ord
- 1);
5223 super
->updates_pending
++;
5225 struct dl
*dl
= *dlp
;
5227 *dlp
= (*dlp
)->next
;
5228 __free_imsm_disk(dl
);
5231 #endif /* MDASSEMBLE */
5233 struct superswitch super_imsm
= {
5235 .examine_super
= examine_super_imsm
,
5236 .brief_examine_super
= brief_examine_super_imsm
,
5237 .brief_examine_subarrays
= brief_examine_subarrays_imsm
,
5238 .export_examine_super
= export_examine_super_imsm
,
5239 .detail_super
= detail_super_imsm
,
5240 .brief_detail_super
= brief_detail_super_imsm
,
5241 .write_init_super
= write_init_super_imsm
,
5242 .validate_geometry
= validate_geometry_imsm
,
5243 .add_to_super
= add_to_super_imsm
,
5244 .detail_platform
= detail_platform_imsm
,
5246 .match_home
= match_home_imsm
,
5247 .uuid_from_super
= uuid_from_super_imsm
,
5248 .getinfo_super
= getinfo_super_imsm
,
5249 .update_super
= update_super_imsm
,
5251 .avail_size
= avail_size_imsm
,
5253 .compare_super
= compare_super_imsm
,
5255 .load_super
= load_super_imsm
,
5256 .init_super
= init_super_imsm
,
5257 .store_super
= store_super_imsm
,
5258 .free_super
= free_super_imsm
,
5259 .match_metadata_desc
= match_metadata_desc_imsm
,
5260 .container_content
= container_content_imsm
,
5261 .default_layout
= imsm_level_to_layout
,
5268 .open_new
= imsm_open_new
,
5269 .load_super
= load_super_imsm
,
5270 .set_array_state
= imsm_set_array_state
,
5271 .set_disk
= imsm_set_disk
,
5272 .sync_metadata
= imsm_sync_metadata
,
5273 .activate_spare
= imsm_activate_spare
,
5274 .process_update
= imsm_process_update
,
5275 .prepare_update
= imsm_prepare_update
,
5276 #endif /* MDASSEMBLE */