]>
Commit | Line | Data |
---|---|---|
cdddbdbc DW |
1 | /* |
2 | * mdadm - Intel(R) Matrix Storage Manager Support | |
3 | * | |
a54d5262 | 4 | * Copyright (C) 2002-2008 Intel Corporation |
cdddbdbc DW |
5 | * |
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. | |
9 | * | |
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 | |
13 | * more details. | |
14 | * | |
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. | |
18 | */ | |
19 | ||
51006d85 | 20 | #define HAVE_STDINT_H 1 |
cdddbdbc | 21 | #include "mdadm.h" |
c2a1e7da | 22 | #include "mdmon.h" |
51006d85 | 23 | #include "sha1.h" |
88c32bb1 | 24 | #include "platform-intel.h" |
cdddbdbc DW |
25 | #include <values.h> |
26 | #include <scsi/sg.h> | |
27 | #include <ctype.h> | |
d665cc31 | 28 | #include <dirent.h> |
cdddbdbc DW |
29 | |
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" | |
fe7ed8cb DW |
35 | #define MPB_VERSION_MANY_VOLUMES_PER_ARRAY "1.2.00" |
36 | #define MPB_VERSION_3OR4_DISK_ARRAY "1.2.01" | |
cdddbdbc | 37 | #define MPB_VERSION_RAID5 "1.2.02" |
fe7ed8cb DW |
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" | |
cdddbdbc DW |
41 | #define MAX_SIGNATURE_LENGTH 32 |
42 | #define MAX_RAID_SERIAL_LEN 16 | |
fe7ed8cb DW |
43 | |
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) | |
53 | ||
c2c087e6 DW |
54 | #define MPB_SECTOR_CNT 418 |
55 | #define IMSM_RESERVED_SECTORS 4096 | |
979d38be | 56 | #define SECT_PER_MB_SHIFT 11 |
cdddbdbc DW |
57 | |
58 | /* Disk configuration info. */ | |
59 | #define IMSM_MAX_DEVICES 255 | |
60 | struct imsm_disk { | |
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 */ | |
f2f27e63 DW |
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 | #define USABLE_DISK __cpu_to_le32(0x08) /* Fully usable unless FAILED_DISK is set */ | |
cdddbdbc | 68 | __u32 status; /* 0xF0 - 0xF3 */ |
fe7ed8cb DW |
69 | __u32 owner_cfg_num; /* which config 0,1,2... owns this disk */ |
70 | #define IMSM_DISK_FILLERS 4 | |
cdddbdbc DW |
71 | __u32 filler[IMSM_DISK_FILLERS]; /* 0xF4 - 0x107 MPB_DISK_FILLERS for future expansion */ |
72 | }; | |
73 | ||
74 | /* RAID map configuration infos. */ | |
75 | struct imsm_map { | |
76 | __u32 pba_of_lba0; /* start address of partition */ | |
77 | __u32 blocks_per_member;/* blocks per member */ | |
78 | __u32 num_data_stripes; /* number of data stripes */ | |
79 | __u16 blocks_per_strip; | |
80 | __u8 map_state; /* Normal, Uninitialized, Degraded, Failed */ | |
81 | #define IMSM_T_STATE_NORMAL 0 | |
82 | #define IMSM_T_STATE_UNINITIALIZED 1 | |
e3bba0e0 DW |
83 | #define IMSM_T_STATE_DEGRADED 2 |
84 | #define IMSM_T_STATE_FAILED 3 | |
cdddbdbc DW |
85 | __u8 raid_level; |
86 | #define IMSM_T_RAID0 0 | |
87 | #define IMSM_T_RAID1 1 | |
88 | #define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */ | |
89 | __u8 num_members; /* number of member disks */ | |
fe7ed8cb DW |
90 | __u8 num_domains; /* number of parity domains */ |
91 | __u8 failed_disk_num; /* valid only when state is degraded */ | |
252d23c0 | 92 | __u8 ddf; |
cdddbdbc | 93 | __u32 filler[7]; /* expansion area */ |
7eef0453 | 94 | #define IMSM_ORD_REBUILD (1 << 24) |
cdddbdbc | 95 | __u32 disk_ord_tbl[1]; /* disk_ord_tbl[num_members], |
7eef0453 DW |
96 | * top byte contains some flags |
97 | */ | |
cdddbdbc DW |
98 | } __attribute__ ((packed)); |
99 | ||
100 | struct imsm_vol { | |
f8f603f1 | 101 | __u32 curr_migr_unit; |
fe7ed8cb | 102 | __u32 checkpoint_id; /* id to access curr_migr_unit */ |
cdddbdbc | 103 | __u8 migr_state; /* Normal or Migrating */ |
e3bba0e0 DW |
104 | #define MIGR_INIT 0 |
105 | #define MIGR_REBUILD 1 | |
106 | #define MIGR_VERIFY 2 /* analagous to echo check > sync_action */ | |
107 | #define MIGR_GEN_MIGR 3 | |
108 | #define MIGR_STATE_CHANGE 4 | |
1484e727 | 109 | #define MIGR_REPAIR 5 |
cdddbdbc DW |
110 | __u8 migr_type; /* Initializing, Rebuilding, ... */ |
111 | __u8 dirty; | |
fe7ed8cb DW |
112 | __u8 fs_state; /* fast-sync state for CnG (0xff == disabled) */ |
113 | __u16 verify_errors; /* number of mismatches */ | |
114 | __u16 bad_blocks; /* number of bad blocks during verify */ | |
115 | __u32 filler[4]; | |
cdddbdbc DW |
116 | struct imsm_map map[1]; |
117 | /* here comes another one if migr_state */ | |
118 | } __attribute__ ((packed)); | |
119 | ||
120 | struct imsm_dev { | |
fe7ed8cb | 121 | __u8 volume[MAX_RAID_SERIAL_LEN]; |
cdddbdbc DW |
122 | __u32 size_low; |
123 | __u32 size_high; | |
fe7ed8cb DW |
124 | #define DEV_BOOTABLE __cpu_to_le32(0x01) |
125 | #define DEV_BOOT_DEVICE __cpu_to_le32(0x02) | |
126 | #define DEV_READ_COALESCING __cpu_to_le32(0x04) | |
127 | #define DEV_WRITE_COALESCING __cpu_to_le32(0x08) | |
128 | #define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10) | |
129 | #define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20) | |
130 | #define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40) | |
131 | #define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80) | |
132 | #define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100) | |
133 | #define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200) | |
134 | #define DEV_CLONE_N_GO __cpu_to_le32(0x400) | |
135 | #define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800) | |
136 | #define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000) | |
cdddbdbc DW |
137 | __u32 status; /* Persistent RaidDev status */ |
138 | __u32 reserved_blocks; /* Reserved blocks at beginning of volume */ | |
fe7ed8cb DW |
139 | __u8 migr_priority; |
140 | __u8 num_sub_vols; | |
141 | __u8 tid; | |
142 | __u8 cng_master_disk; | |
143 | __u16 cache_policy; | |
144 | __u8 cng_state; | |
145 | __u8 cng_sub_state; | |
146 | #define IMSM_DEV_FILLERS 10 | |
cdddbdbc DW |
147 | __u32 filler[IMSM_DEV_FILLERS]; |
148 | struct imsm_vol vol; | |
149 | } __attribute__ ((packed)); | |
150 | ||
151 | struct imsm_super { | |
152 | __u8 sig[MAX_SIGNATURE_LENGTH]; /* 0x00 - 0x1F */ | |
153 | __u32 check_sum; /* 0x20 - 0x23 MPB Checksum */ | |
154 | __u32 mpb_size; /* 0x24 - 0x27 Size of MPB */ | |
155 | __u32 family_num; /* 0x28 - 0x2B Checksum from first time this config was written */ | |
156 | __u32 generation_num; /* 0x2C - 0x2F Incremented each time this array's MPB is written */ | |
604b746f JD |
157 | __u32 error_log_size; /* 0x30 - 0x33 in bytes */ |
158 | __u32 attributes; /* 0x34 - 0x37 */ | |
cdddbdbc DW |
159 | __u8 num_disks; /* 0x38 Number of configured disks */ |
160 | __u8 num_raid_devs; /* 0x39 Number of configured volumes */ | |
604b746f JD |
161 | __u8 error_log_pos; /* 0x3A */ |
162 | __u8 fill[1]; /* 0x3B */ | |
163 | __u32 cache_size; /* 0x3c - 0x40 in mb */ | |
164 | __u32 orig_family_num; /* 0x40 - 0x43 original family num */ | |
165 | __u32 pwr_cycle_count; /* 0x44 - 0x47 simulated power cycle count for array */ | |
166 | __u32 bbm_log_size; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */ | |
167 | #define IMSM_FILLERS 35 | |
168 | __u32 filler[IMSM_FILLERS]; /* 0x4C - 0xD7 RAID_MPB_FILLERS */ | |
cdddbdbc DW |
169 | struct imsm_disk disk[1]; /* 0xD8 diskTbl[numDisks] */ |
170 | /* here comes imsm_dev[num_raid_devs] */ | |
604b746f | 171 | /* here comes BBM logs */ |
cdddbdbc DW |
172 | } __attribute__ ((packed)); |
173 | ||
604b746f JD |
174 | #define BBM_LOG_MAX_ENTRIES 254 |
175 | ||
176 | struct bbm_log_entry { | |
177 | __u64 defective_block_start; | |
178 | #define UNREADABLE 0xFFFFFFFF | |
179 | __u32 spare_block_offset; | |
180 | __u16 remapped_marked_count; | |
181 | __u16 disk_ordinal; | |
182 | } __attribute__ ((__packed__)); | |
183 | ||
184 | struct bbm_log { | |
185 | __u32 signature; /* 0xABADB10C */ | |
186 | __u32 entry_count; | |
187 | __u32 reserved_spare_block_count; /* 0 */ | |
188 | __u32 reserved; /* 0xFFFF */ | |
189 | __u64 first_spare_lba; | |
190 | struct bbm_log_entry mapped_block_entries[BBM_LOG_MAX_ENTRIES]; | |
191 | } __attribute__ ((__packed__)); | |
192 | ||
193 | ||
cdddbdbc DW |
194 | #ifndef MDASSEMBLE |
195 | static char *map_state_str[] = { "normal", "uninitialized", "degraded", "failed" }; | |
196 | #endif | |
197 | ||
1484e727 DW |
198 | static __u8 migr_type(struct imsm_dev *dev) |
199 | { | |
200 | if (dev->vol.migr_type == MIGR_VERIFY && | |
201 | dev->status & DEV_VERIFY_AND_FIX) | |
202 | return MIGR_REPAIR; | |
203 | else | |
204 | return dev->vol.migr_type; | |
205 | } | |
206 | ||
207 | static void set_migr_type(struct imsm_dev *dev, __u8 migr_type) | |
208 | { | |
209 | /* for compatibility with older oroms convert MIGR_REPAIR, into | |
210 | * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status | |
211 | */ | |
212 | if (migr_type == MIGR_REPAIR) { | |
213 | dev->vol.migr_type = MIGR_VERIFY; | |
214 | dev->status |= DEV_VERIFY_AND_FIX; | |
215 | } else { | |
216 | dev->vol.migr_type = migr_type; | |
217 | dev->status &= ~DEV_VERIFY_AND_FIX; | |
218 | } | |
219 | } | |
220 | ||
87eb16df | 221 | static unsigned int sector_count(__u32 bytes) |
cdddbdbc | 222 | { |
87eb16df DW |
223 | return ((bytes + (512-1)) & (~(512-1))) / 512; |
224 | } | |
cdddbdbc | 225 | |
87eb16df DW |
226 | static unsigned int mpb_sectors(struct imsm_super *mpb) |
227 | { | |
228 | return sector_count(__le32_to_cpu(mpb->mpb_size)); | |
cdddbdbc DW |
229 | } |
230 | ||
ba2de7ba DW |
231 | struct intel_dev { |
232 | struct imsm_dev *dev; | |
233 | struct intel_dev *next; | |
234 | int index; | |
235 | }; | |
236 | ||
cdddbdbc DW |
237 | /* internal representation of IMSM metadata */ |
238 | struct intel_super { | |
239 | union { | |
949c47a0 DW |
240 | void *buf; /* O_DIRECT buffer for reading/writing metadata */ |
241 | struct imsm_super *anchor; /* immovable parameters */ | |
cdddbdbc | 242 | }; |
949c47a0 | 243 | size_t len; /* size of the 'buf' allocation */ |
4d7b1503 DW |
244 | void *next_buf; /* for realloc'ing buf from the manager */ |
245 | size_t next_len; | |
c2c087e6 DW |
246 | int updates_pending; /* count of pending updates for mdmon */ |
247 | int creating_imsm; /* flag to indicate container creation */ | |
bf5a934a | 248 | int current_vol; /* index of raid device undergoing creation */ |
0dcecb2e | 249 | __u32 create_offset; /* common start for 'current_vol' */ |
148acb7b | 250 | __u32 random; /* random data for seeding new family numbers */ |
ba2de7ba | 251 | struct intel_dev *devlist; |
cdddbdbc DW |
252 | struct dl { |
253 | struct dl *next; | |
254 | int index; | |
255 | __u8 serial[MAX_RAID_SERIAL_LEN]; | |
256 | int major, minor; | |
257 | char *devname; | |
b9f594fe | 258 | struct imsm_disk disk; |
cdddbdbc | 259 | int fd; |
0dcecb2e DW |
260 | int extent_cnt; |
261 | struct extent *e; /* for determining freespace @ create */ | |
efb30e7f | 262 | int raiddisk; /* slot to fill in autolayout */ |
cdddbdbc | 263 | } *disks; |
43dad3d6 | 264 | struct dl *add; /* list of disks to add while mdmon active */ |
47ee5a45 | 265 | struct dl *missing; /* disks removed while we weren't looking */ |
43dad3d6 | 266 | struct bbm_log *bbm_log; |
88c32bb1 DW |
267 | const char *hba; /* device path of the raid controller for this metadata */ |
268 | const struct imsm_orom *orom; /* platform firmware support */ | |
cdddbdbc DW |
269 | }; |
270 | ||
c2c087e6 DW |
271 | struct extent { |
272 | unsigned long long start, size; | |
273 | }; | |
274 | ||
88758e9d DW |
275 | /* definition of messages passed to imsm_process_update */ |
276 | enum imsm_update_type { | |
277 | update_activate_spare, | |
8273f55e | 278 | update_create_array, |
43dad3d6 | 279 | update_add_disk, |
88758e9d DW |
280 | }; |
281 | ||
282 | struct imsm_update_activate_spare { | |
283 | enum imsm_update_type type; | |
d23fe947 | 284 | struct dl *dl; |
88758e9d DW |
285 | int slot; |
286 | int array; | |
287 | struct imsm_update_activate_spare *next; | |
288 | }; | |
289 | ||
54c2c1ea DW |
290 | struct disk_info { |
291 | __u8 serial[MAX_RAID_SERIAL_LEN]; | |
292 | }; | |
293 | ||
8273f55e DW |
294 | struct imsm_update_create_array { |
295 | enum imsm_update_type type; | |
8273f55e | 296 | int dev_idx; |
6a3e913e | 297 | struct imsm_dev dev; |
8273f55e DW |
298 | }; |
299 | ||
43dad3d6 DW |
300 | struct imsm_update_add_disk { |
301 | enum imsm_update_type type; | |
302 | }; | |
303 | ||
cdddbdbc DW |
304 | static struct supertype *match_metadata_desc_imsm(char *arg) |
305 | { | |
306 | struct supertype *st; | |
307 | ||
308 | if (strcmp(arg, "imsm") != 0 && | |
309 | strcmp(arg, "default") != 0 | |
310 | ) | |
311 | return NULL; | |
312 | ||
313 | st = malloc(sizeof(*st)); | |
ef609477 | 314 | memset(st, 0, sizeof(*st)); |
cdddbdbc DW |
315 | st->ss = &super_imsm; |
316 | st->max_devs = IMSM_MAX_DEVICES; | |
317 | st->minor_version = 0; | |
318 | st->sb = NULL; | |
319 | return st; | |
320 | } | |
321 | ||
0e600426 | 322 | #ifndef MDASSEMBLE |
cdddbdbc DW |
323 | static __u8 *get_imsm_version(struct imsm_super *mpb) |
324 | { | |
325 | return &mpb->sig[MPB_SIG_LEN]; | |
326 | } | |
0e600426 | 327 | #endif |
cdddbdbc | 328 | |
949c47a0 DW |
329 | /* retrieve a disk directly from the anchor when the anchor is known to be |
330 | * up-to-date, currently only at load time | |
331 | */ | |
332 | static struct imsm_disk *__get_imsm_disk(struct imsm_super *mpb, __u8 index) | |
cdddbdbc | 333 | { |
949c47a0 | 334 | if (index >= mpb->num_disks) |
cdddbdbc DW |
335 | return NULL; |
336 | return &mpb->disk[index]; | |
337 | } | |
338 | ||
0e600426 | 339 | #ifndef MDASSEMBLE |
b9f594fe | 340 | /* retrieve a disk from the parsed metadata */ |
949c47a0 DW |
341 | static struct imsm_disk *get_imsm_disk(struct intel_super *super, __u8 index) |
342 | { | |
b9f594fe DW |
343 | struct dl *d; |
344 | ||
345 | for (d = super->disks; d; d = d->next) | |
346 | if (d->index == index) | |
347 | return &d->disk; | |
348 | ||
349 | return NULL; | |
949c47a0 | 350 | } |
0e600426 | 351 | #endif |
949c47a0 DW |
352 | |
353 | /* generate a checksum directly from the anchor when the anchor is known to be | |
354 | * up-to-date, currently only at load or write_super after coalescing | |
355 | */ | |
356 | static __u32 __gen_imsm_checksum(struct imsm_super *mpb) | |
cdddbdbc DW |
357 | { |
358 | __u32 end = mpb->mpb_size / sizeof(end); | |
359 | __u32 *p = (__u32 *) mpb; | |
360 | __u32 sum = 0; | |
361 | ||
97f734fd N |
362 | while (end--) { |
363 | sum += __le32_to_cpu(*p); | |
364 | p++; | |
365 | } | |
cdddbdbc DW |
366 | |
367 | return sum - __le32_to_cpu(mpb->check_sum); | |
368 | } | |
369 | ||
a965f303 DW |
370 | static size_t sizeof_imsm_map(struct imsm_map *map) |
371 | { | |
372 | return sizeof(struct imsm_map) + sizeof(__u32) * (map->num_members - 1); | |
373 | } | |
374 | ||
375 | struct imsm_map *get_imsm_map(struct imsm_dev *dev, int second_map) | |
cdddbdbc | 376 | { |
a965f303 DW |
377 | struct imsm_map *map = &dev->vol.map[0]; |
378 | ||
379 | if (second_map && !dev->vol.migr_state) | |
380 | return NULL; | |
381 | else if (second_map) { | |
382 | void *ptr = map; | |
383 | ||
384 | return ptr + sizeof_imsm_map(map); | |
385 | } else | |
386 | return map; | |
387 | ||
388 | } | |
cdddbdbc | 389 | |
3393c6af DW |
390 | /* return the size of the device. |
391 | * migr_state increases the returned size if map[0] were to be duplicated | |
392 | */ | |
393 | static size_t sizeof_imsm_dev(struct imsm_dev *dev, int migr_state) | |
a965f303 DW |
394 | { |
395 | size_t size = sizeof(*dev) - sizeof(struct imsm_map) + | |
396 | sizeof_imsm_map(get_imsm_map(dev, 0)); | |
cdddbdbc DW |
397 | |
398 | /* migrating means an additional map */ | |
a965f303 DW |
399 | if (dev->vol.migr_state) |
400 | size += sizeof_imsm_map(get_imsm_map(dev, 1)); | |
3393c6af DW |
401 | else if (migr_state) |
402 | size += sizeof_imsm_map(get_imsm_map(dev, 0)); | |
cdddbdbc DW |
403 | |
404 | return size; | |
405 | } | |
406 | ||
54c2c1ea DW |
407 | #ifndef MDASSEMBLE |
408 | /* retrieve disk serial number list from a metadata update */ | |
409 | static struct disk_info *get_disk_info(struct imsm_update_create_array *update) | |
410 | { | |
411 | void *u = update; | |
412 | struct disk_info *inf; | |
413 | ||
414 | inf = u + sizeof(*update) - sizeof(struct imsm_dev) + | |
415 | sizeof_imsm_dev(&update->dev, 0); | |
416 | ||
417 | return inf; | |
418 | } | |
419 | #endif | |
420 | ||
949c47a0 | 421 | static struct imsm_dev *__get_imsm_dev(struct imsm_super *mpb, __u8 index) |
cdddbdbc DW |
422 | { |
423 | int offset; | |
424 | int i; | |
425 | void *_mpb = mpb; | |
426 | ||
949c47a0 | 427 | if (index >= mpb->num_raid_devs) |
cdddbdbc DW |
428 | return NULL; |
429 | ||
430 | /* devices start after all disks */ | |
431 | offset = ((void *) &mpb->disk[mpb->num_disks]) - _mpb; | |
432 | ||
433 | for (i = 0; i <= index; i++) | |
434 | if (i == index) | |
435 | return _mpb + offset; | |
436 | else | |
3393c6af | 437 | offset += sizeof_imsm_dev(_mpb + offset, 0); |
cdddbdbc DW |
438 | |
439 | return NULL; | |
440 | } | |
441 | ||
949c47a0 DW |
442 | static struct imsm_dev *get_imsm_dev(struct intel_super *super, __u8 index) |
443 | { | |
ba2de7ba DW |
444 | struct intel_dev *dv; |
445 | ||
949c47a0 DW |
446 | if (index >= super->anchor->num_raid_devs) |
447 | return NULL; | |
ba2de7ba DW |
448 | for (dv = super->devlist; dv; dv = dv->next) |
449 | if (dv->index == index) | |
450 | return dv->dev; | |
451 | return NULL; | |
949c47a0 DW |
452 | } |
453 | ||
7eef0453 DW |
454 | static __u32 get_imsm_ord_tbl_ent(struct imsm_dev *dev, int slot) |
455 | { | |
456 | struct imsm_map *map; | |
457 | ||
458 | if (dev->vol.migr_state) | |
7eef0453 | 459 | map = get_imsm_map(dev, 1); |
fb9bf0d3 DW |
460 | else |
461 | map = get_imsm_map(dev, 0); | |
7eef0453 | 462 | |
ff077194 DW |
463 | /* top byte identifies disk under rebuild */ |
464 | return __le32_to_cpu(map->disk_ord_tbl[slot]); | |
465 | } | |
466 | ||
467 | #define ord_to_idx(ord) (((ord) << 8) >> 8) | |
468 | static __u32 get_imsm_disk_idx(struct imsm_dev *dev, int slot) | |
469 | { | |
470 | __u32 ord = get_imsm_ord_tbl_ent(dev, slot); | |
471 | ||
472 | return ord_to_idx(ord); | |
7eef0453 DW |
473 | } |
474 | ||
be73972f DW |
475 | static void set_imsm_ord_tbl_ent(struct imsm_map *map, int slot, __u32 ord) |
476 | { | |
477 | map->disk_ord_tbl[slot] = __cpu_to_le32(ord); | |
478 | } | |
479 | ||
620b1713 DW |
480 | static int get_imsm_disk_slot(struct imsm_map *map, int idx) |
481 | { | |
482 | int slot; | |
483 | __u32 ord; | |
484 | ||
485 | for (slot = 0; slot < map->num_members; slot++) { | |
486 | ord = __le32_to_cpu(map->disk_ord_tbl[slot]); | |
487 | if (ord_to_idx(ord) == idx) | |
488 | return slot; | |
489 | } | |
490 | ||
491 | return -1; | |
492 | } | |
493 | ||
cdddbdbc DW |
494 | static int get_imsm_raid_level(struct imsm_map *map) |
495 | { | |
496 | if (map->raid_level == 1) { | |
497 | if (map->num_members == 2) | |
498 | return 1; | |
499 | else | |
500 | return 10; | |
501 | } | |
502 | ||
503 | return map->raid_level; | |
504 | } | |
505 | ||
c2c087e6 DW |
506 | static int cmp_extent(const void *av, const void *bv) |
507 | { | |
508 | const struct extent *a = av; | |
509 | const struct extent *b = bv; | |
510 | if (a->start < b->start) | |
511 | return -1; | |
512 | if (a->start > b->start) | |
513 | return 1; | |
514 | return 0; | |
515 | } | |
516 | ||
0dcecb2e | 517 | static int count_memberships(struct dl *dl, struct intel_super *super) |
c2c087e6 | 518 | { |
c2c087e6 | 519 | int memberships = 0; |
620b1713 | 520 | int i; |
c2c087e6 | 521 | |
949c47a0 DW |
522 | for (i = 0; i < super->anchor->num_raid_devs; i++) { |
523 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
a965f303 | 524 | struct imsm_map *map = get_imsm_map(dev, 0); |
c2c087e6 | 525 | |
620b1713 DW |
526 | if (get_imsm_disk_slot(map, dl->index) >= 0) |
527 | memberships++; | |
c2c087e6 | 528 | } |
0dcecb2e DW |
529 | |
530 | return memberships; | |
531 | } | |
532 | ||
533 | static struct extent *get_extents(struct intel_super *super, struct dl *dl) | |
534 | { | |
535 | /* find a list of used extents on the given physical device */ | |
536 | struct extent *rv, *e; | |
620b1713 | 537 | int i; |
0dcecb2e DW |
538 | int memberships = count_memberships(dl, super); |
539 | __u32 reservation = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; | |
540 | ||
c2c087e6 DW |
541 | rv = malloc(sizeof(struct extent) * (memberships + 1)); |
542 | if (!rv) | |
543 | return NULL; | |
544 | e = rv; | |
545 | ||
949c47a0 DW |
546 | for (i = 0; i < super->anchor->num_raid_devs; i++) { |
547 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
a965f303 | 548 | struct imsm_map *map = get_imsm_map(dev, 0); |
c2c087e6 | 549 | |
620b1713 DW |
550 | if (get_imsm_disk_slot(map, dl->index) >= 0) { |
551 | e->start = __le32_to_cpu(map->pba_of_lba0); | |
552 | e->size = __le32_to_cpu(map->blocks_per_member); | |
553 | e++; | |
c2c087e6 DW |
554 | } |
555 | } | |
556 | qsort(rv, memberships, sizeof(*rv), cmp_extent); | |
557 | ||
14e8215b DW |
558 | /* determine the start of the metadata |
559 | * when no raid devices are defined use the default | |
560 | * ...otherwise allow the metadata to truncate the value | |
561 | * as is the case with older versions of imsm | |
562 | */ | |
563 | if (memberships) { | |
564 | struct extent *last = &rv[memberships - 1]; | |
565 | __u32 remainder; | |
566 | ||
567 | remainder = __le32_to_cpu(dl->disk.total_blocks) - | |
568 | (last->start + last->size); | |
dda5855f DW |
569 | /* round down to 1k block to satisfy precision of the kernel |
570 | * 'size' interface | |
571 | */ | |
572 | remainder &= ~1UL; | |
573 | /* make sure remainder is still sane */ | |
574 | if (remainder < ROUND_UP(super->len, 512) >> 9) | |
575 | remainder = ROUND_UP(super->len, 512) >> 9; | |
14e8215b DW |
576 | if (reservation > remainder) |
577 | reservation = remainder; | |
578 | } | |
579 | e->start = __le32_to_cpu(dl->disk.total_blocks) - reservation; | |
c2c087e6 DW |
580 | e->size = 0; |
581 | return rv; | |
582 | } | |
583 | ||
14e8215b DW |
584 | /* try to determine how much space is reserved for metadata from |
585 | * the last get_extents() entry, otherwise fallback to the | |
586 | * default | |
587 | */ | |
588 | static __u32 imsm_reserved_sectors(struct intel_super *super, struct dl *dl) | |
589 | { | |
590 | struct extent *e; | |
591 | int i; | |
592 | __u32 rv; | |
593 | ||
594 | /* for spares just return a minimal reservation which will grow | |
595 | * once the spare is picked up by an array | |
596 | */ | |
597 | if (dl->index == -1) | |
598 | return MPB_SECTOR_CNT; | |
599 | ||
600 | e = get_extents(super, dl); | |
601 | if (!e) | |
602 | return MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; | |
603 | ||
604 | /* scroll to last entry */ | |
605 | for (i = 0; e[i].size; i++) | |
606 | continue; | |
607 | ||
608 | rv = __le32_to_cpu(dl->disk.total_blocks) - e[i].start; | |
609 | ||
610 | free(e); | |
611 | ||
612 | return rv; | |
613 | } | |
614 | ||
615 | #ifndef MDASSEMBLE | |
44470971 | 616 | static void print_imsm_dev(struct imsm_dev *dev, char *uuid, int disk_idx) |
cdddbdbc DW |
617 | { |
618 | __u64 sz; | |
619 | int slot; | |
a965f303 | 620 | struct imsm_map *map = get_imsm_map(dev, 0); |
b10b37b8 | 621 | __u32 ord; |
cdddbdbc DW |
622 | |
623 | printf("\n"); | |
1e7bc0ed | 624 | printf("[%.16s]:\n", dev->volume); |
44470971 | 625 | printf(" UUID : %s\n", uuid); |
cdddbdbc DW |
626 | printf(" RAID Level : %d\n", get_imsm_raid_level(map)); |
627 | printf(" Members : %d\n", map->num_members); | |
620b1713 DW |
628 | slot = get_imsm_disk_slot(map, disk_idx); |
629 | if (slot >= 0) { | |
b10b37b8 DW |
630 | ord = get_imsm_ord_tbl_ent(dev, slot); |
631 | printf(" This Slot : %d%s\n", slot, | |
632 | ord & IMSM_ORD_REBUILD ? " (out-of-sync)" : ""); | |
633 | } else | |
cdddbdbc DW |
634 | printf(" This Slot : ?\n"); |
635 | sz = __le32_to_cpu(dev->size_high); | |
636 | sz <<= 32; | |
637 | sz += __le32_to_cpu(dev->size_low); | |
638 | printf(" Array Size : %llu%s\n", (unsigned long long)sz, | |
639 | human_size(sz * 512)); | |
640 | sz = __le32_to_cpu(map->blocks_per_member); | |
641 | printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz, | |
642 | human_size(sz * 512)); | |
643 | printf(" Sector Offset : %u\n", | |
644 | __le32_to_cpu(map->pba_of_lba0)); | |
645 | printf(" Num Stripes : %u\n", | |
646 | __le32_to_cpu(map->num_data_stripes)); | |
647 | printf(" Chunk Size : %u KiB\n", | |
648 | __le16_to_cpu(map->blocks_per_strip) / 2); | |
649 | printf(" Reserved : %d\n", __le32_to_cpu(dev->reserved_blocks)); | |
1484e727 DW |
650 | printf(" Migrate State : %s", dev->vol.migr_state ? "migrating" : "idle\n"); |
651 | if (dev->vol.migr_state) { | |
652 | if (migr_type(dev) == MIGR_INIT) | |
653 | printf(": initializing\n"); | |
654 | else if (migr_type(dev) == MIGR_REBUILD) | |
655 | printf(": rebuilding\n"); | |
656 | else if (migr_type(dev) == MIGR_VERIFY) | |
657 | printf(": check\n"); | |
658 | else if (migr_type(dev) == MIGR_GEN_MIGR) | |
659 | printf(": general migration\n"); | |
660 | else if (migr_type(dev) == MIGR_STATE_CHANGE) | |
661 | printf(": state change\n"); | |
662 | else if (migr_type(dev) == MIGR_REPAIR) | |
663 | printf(": repair\n"); | |
664 | else | |
665 | printf(": <unknown:%d>\n", migr_type(dev)); | |
666 | } | |
3393c6af DW |
667 | printf(" Map State : %s", map_state_str[map->map_state]); |
668 | if (dev->vol.migr_state) { | |
669 | struct imsm_map *map = get_imsm_map(dev, 1); | |
b10b37b8 | 670 | printf(" <-- %s", map_state_str[map->map_state]); |
3393c6af DW |
671 | } |
672 | printf("\n"); | |
cdddbdbc | 673 | printf(" Dirty State : %s\n", dev->vol.dirty ? "dirty" : "clean"); |
cdddbdbc DW |
674 | } |
675 | ||
14e8215b | 676 | static void print_imsm_disk(struct imsm_super *mpb, int index, __u32 reserved) |
cdddbdbc | 677 | { |
949c47a0 | 678 | struct imsm_disk *disk = __get_imsm_disk(mpb, index); |
1f24f035 | 679 | char str[MAX_RAID_SERIAL_LEN + 1]; |
cdddbdbc DW |
680 | __u32 s; |
681 | __u64 sz; | |
682 | ||
e9d82038 DW |
683 | if (index < 0) |
684 | return; | |
685 | ||
cdddbdbc | 686 | printf("\n"); |
1f24f035 | 687 | snprintf(str, MAX_RAID_SERIAL_LEN + 1, "%s", disk->serial); |
cdddbdbc | 688 | printf(" Disk%02d Serial : %s\n", index, str); |
f2f27e63 | 689 | s = disk->status; |
cdddbdbc DW |
690 | printf(" State :%s%s%s%s\n", s&SPARE_DISK ? " spare" : "", |
691 | s&CONFIGURED_DISK ? " active" : "", | |
692 | s&FAILED_DISK ? " failed" : "", | |
693 | s&USABLE_DISK ? " usable" : ""); | |
694 | printf(" Id : %08x\n", __le32_to_cpu(disk->scsi_id)); | |
14e8215b | 695 | sz = __le32_to_cpu(disk->total_blocks) - reserved; |
cdddbdbc DW |
696 | printf(" Usable Size : %llu%s\n", (unsigned long long)sz, |
697 | human_size(sz * 512)); | |
698 | } | |
699 | ||
44470971 DW |
700 | static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info); |
701 | ||
cdddbdbc DW |
702 | static void examine_super_imsm(struct supertype *st, char *homehost) |
703 | { | |
704 | struct intel_super *super = st->sb; | |
949c47a0 | 705 | struct imsm_super *mpb = super->anchor; |
cdddbdbc DW |
706 | char str[MAX_SIGNATURE_LENGTH]; |
707 | int i; | |
27fd6274 DW |
708 | struct mdinfo info; |
709 | char nbuf[64]; | |
cdddbdbc | 710 | __u32 sum; |
14e8215b | 711 | __u32 reserved = imsm_reserved_sectors(super, super->disks); |
cdddbdbc | 712 | |
27fd6274 | 713 | |
cdddbdbc DW |
714 | snprintf(str, MPB_SIG_LEN, "%s", mpb->sig); |
715 | printf(" Magic : %s\n", str); | |
716 | snprintf(str, strlen(MPB_VERSION_RAID0), "%s", get_imsm_version(mpb)); | |
717 | printf(" Version : %s\n", get_imsm_version(mpb)); | |
148acb7b | 718 | printf(" Orig Family : %08x\n", __le32_to_cpu(mpb->orig_family_num)); |
cdddbdbc DW |
719 | printf(" Family : %08x\n", __le32_to_cpu(mpb->family_num)); |
720 | printf(" Generation : %08x\n", __le32_to_cpu(mpb->generation_num)); | |
27fd6274 | 721 | getinfo_super_imsm(st, &info); |
ae2bfd4e | 722 | fname_from_uuid(st, &info, nbuf, ':'); |
27fd6274 | 723 | printf(" UUID : %s\n", nbuf + 5); |
cdddbdbc DW |
724 | sum = __le32_to_cpu(mpb->check_sum); |
725 | printf(" Checksum : %08x %s\n", sum, | |
949c47a0 | 726 | __gen_imsm_checksum(mpb) == sum ? "correct" : "incorrect"); |
87eb16df | 727 | printf(" MPB Sectors : %d\n", mpb_sectors(mpb)); |
cdddbdbc DW |
728 | printf(" Disks : %d\n", mpb->num_disks); |
729 | printf(" RAID Devices : %d\n", mpb->num_raid_devs); | |
14e8215b | 730 | print_imsm_disk(mpb, super->disks->index, reserved); |
604b746f JD |
731 | if (super->bbm_log) { |
732 | struct bbm_log *log = super->bbm_log; | |
733 | ||
734 | printf("\n"); | |
735 | printf("Bad Block Management Log:\n"); | |
736 | printf(" Log Size : %d\n", __le32_to_cpu(mpb->bbm_log_size)); | |
737 | printf(" Signature : %x\n", __le32_to_cpu(log->signature)); | |
738 | printf(" Entry Count : %d\n", __le32_to_cpu(log->entry_count)); | |
739 | printf(" Spare Blocks : %d\n", __le32_to_cpu(log->reserved_spare_block_count)); | |
13a3b65d N |
740 | printf(" First Spare : %llx\n", |
741 | (unsigned long long) __le64_to_cpu(log->first_spare_lba)); | |
604b746f | 742 | } |
44470971 DW |
743 | for (i = 0; i < mpb->num_raid_devs; i++) { |
744 | struct mdinfo info; | |
745 | struct imsm_dev *dev = __get_imsm_dev(mpb, i); | |
746 | ||
747 | super->current_vol = i; | |
748 | getinfo_super_imsm(st, &info); | |
ae2bfd4e | 749 | fname_from_uuid(st, &info, nbuf, ':'); |
44470971 DW |
750 | print_imsm_dev(dev, nbuf + 5, super->disks->index); |
751 | } | |
cdddbdbc DW |
752 | for (i = 0; i < mpb->num_disks; i++) { |
753 | if (i == super->disks->index) | |
754 | continue; | |
14e8215b | 755 | print_imsm_disk(mpb, i, reserved); |
cdddbdbc DW |
756 | } |
757 | } | |
758 | ||
061f2c6a | 759 | static void brief_examine_super_imsm(struct supertype *st, int verbose) |
cdddbdbc | 760 | { |
27fd6274 | 761 | /* We just write a generic IMSM ARRAY entry */ |
ff54de6e N |
762 | struct mdinfo info; |
763 | char nbuf[64]; | |
1e7bc0ed | 764 | struct intel_super *super = st->sb; |
1e7bc0ed | 765 | |
0d5a423f DW |
766 | if (!super->anchor->num_raid_devs) { |
767 | printf("ARRAY metadata=imsm\n"); | |
1e7bc0ed | 768 | return; |
0d5a423f | 769 | } |
ff54de6e | 770 | |
4737ae25 N |
771 | getinfo_super_imsm(st, &info); |
772 | fname_from_uuid(st, &info, nbuf, ':'); | |
773 | printf("ARRAY metadata=imsm UUID=%s\n", nbuf + 5); | |
774 | } | |
775 | ||
776 | static void brief_examine_subarrays_imsm(struct supertype *st, int verbose) | |
777 | { | |
778 | /* We just write a generic IMSM ARRAY entry */ | |
779 | struct mdinfo info; | |
780 | char nbuf[64]; | |
781 | char nbuf1[64]; | |
782 | struct intel_super *super = st->sb; | |
783 | int i; | |
784 | ||
785 | if (!super->anchor->num_raid_devs) | |
786 | return; | |
787 | ||
ff54de6e | 788 | getinfo_super_imsm(st, &info); |
ae2bfd4e | 789 | fname_from_uuid(st, &info, nbuf, ':'); |
1e7bc0ed DW |
790 | for (i = 0; i < super->anchor->num_raid_devs; i++) { |
791 | struct imsm_dev *dev = get_imsm_dev(super, i); | |
792 | ||
793 | super->current_vol = i; | |
794 | getinfo_super_imsm(st, &info); | |
ae2bfd4e | 795 | fname_from_uuid(st, &info, nbuf1, ':'); |
1124b3cf | 796 | printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n", |
cf8de691 | 797 | dev->volume, nbuf + 5, i, nbuf1 + 5); |
1e7bc0ed | 798 | } |
cdddbdbc DW |
799 | } |
800 | ||
9d84c8ea DW |
801 | static void export_examine_super_imsm(struct supertype *st) |
802 | { | |
803 | struct intel_super *super = st->sb; | |
804 | struct imsm_super *mpb = super->anchor; | |
805 | struct mdinfo info; | |
806 | char nbuf[64]; | |
807 | ||
808 | getinfo_super_imsm(st, &info); | |
809 | fname_from_uuid(st, &info, nbuf, ':'); | |
810 | printf("MD_METADATA=imsm\n"); | |
811 | printf("MD_LEVEL=container\n"); | |
812 | printf("MD_UUID=%s\n", nbuf+5); | |
813 | printf("MD_DEVICES=%u\n", mpb->num_disks); | |
814 | } | |
815 | ||
cdddbdbc DW |
816 | static void detail_super_imsm(struct supertype *st, char *homehost) |
817 | { | |
3ebe00a1 DW |
818 | struct mdinfo info; |
819 | char nbuf[64]; | |
820 | ||
821 | getinfo_super_imsm(st, &info); | |
ae2bfd4e | 822 | fname_from_uuid(st, &info, nbuf, ':'); |
3ebe00a1 | 823 | printf("\n UUID : %s\n", nbuf + 5); |
cdddbdbc DW |
824 | } |
825 | ||
826 | static void brief_detail_super_imsm(struct supertype *st) | |
827 | { | |
ff54de6e N |
828 | struct mdinfo info; |
829 | char nbuf[64]; | |
830 | getinfo_super_imsm(st, &info); | |
ae2bfd4e | 831 | fname_from_uuid(st, &info, nbuf, ':'); |
ff54de6e | 832 | printf(" UUID=%s", nbuf + 5); |
cdddbdbc | 833 | } |
d665cc31 DW |
834 | |
835 | static int imsm_read_serial(int fd, char *devname, __u8 *serial); | |
836 | static void fd2devname(int fd, char *name); | |
837 | ||
838 | static int imsm_enumerate_ports(const char *hba_path, int port_count, int host_base, int verbose) | |
839 | { | |
840 | /* dump an unsorted list of devices attached to ahci, as well as | |
841 | * non-connected ports | |
842 | */ | |
843 | int hba_len = strlen(hba_path) + 1; | |
844 | struct dirent *ent; | |
845 | DIR *dir; | |
846 | char *path = NULL; | |
847 | int err = 0; | |
848 | unsigned long port_mask = (1 << port_count) - 1; | |
849 | ||
850 | if (port_count > sizeof(port_mask) * 8) { | |
851 | if (verbose) | |
852 | fprintf(stderr, Name ": port_count %d out of range\n", port_count); | |
853 | return 2; | |
854 | } | |
855 | ||
856 | /* scroll through /sys/dev/block looking for devices attached to | |
857 | * this hba | |
858 | */ | |
859 | dir = opendir("/sys/dev/block"); | |
860 | for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) { | |
861 | int fd; | |
862 | char model[64]; | |
863 | char vendor[64]; | |
864 | char buf[1024]; | |
865 | int major, minor; | |
866 | char *device; | |
867 | char *c; | |
868 | int port; | |
869 | int type; | |
870 | ||
871 | if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2) | |
872 | continue; | |
873 | path = devt_to_devpath(makedev(major, minor)); | |
874 | if (!path) | |
875 | continue; | |
876 | if (!path_attached_to_hba(path, hba_path)) { | |
877 | free(path); | |
878 | path = NULL; | |
879 | continue; | |
880 | } | |
881 | ||
882 | /* retrieve the scsi device type */ | |
883 | if (asprintf(&device, "/sys/dev/block/%d:%d/device/xxxxxxx", major, minor) < 0) { | |
884 | if (verbose) | |
885 | fprintf(stderr, Name ": failed to allocate 'device'\n"); | |
886 | err = 2; | |
887 | break; | |
888 | } | |
889 | sprintf(device, "/sys/dev/block/%d:%d/device/type", major, minor); | |
890 | if (load_sys(device, buf) != 0) { | |
891 | if (verbose) | |
892 | fprintf(stderr, Name ": failed to read device type for %s\n", | |
893 | path); | |
894 | err = 2; | |
895 | free(device); | |
896 | break; | |
897 | } | |
898 | type = strtoul(buf, NULL, 10); | |
899 | ||
900 | /* if it's not a disk print the vendor and model */ | |
901 | if (!(type == 0 || type == 7 || type == 14)) { | |
902 | vendor[0] = '\0'; | |
903 | model[0] = '\0'; | |
904 | sprintf(device, "/sys/dev/block/%d:%d/device/vendor", major, minor); | |
905 | if (load_sys(device, buf) == 0) { | |
906 | strncpy(vendor, buf, sizeof(vendor)); | |
907 | vendor[sizeof(vendor) - 1] = '\0'; | |
908 | c = (char *) &vendor[sizeof(vendor) - 1]; | |
909 | while (isspace(*c) || *c == '\0') | |
910 | *c-- = '\0'; | |
911 | ||
912 | } | |
913 | sprintf(device, "/sys/dev/block/%d:%d/device/model", major, minor); | |
914 | if (load_sys(device, buf) == 0) { | |
915 | strncpy(model, buf, sizeof(model)); | |
916 | model[sizeof(model) - 1] = '\0'; | |
917 | c = (char *) &model[sizeof(model) - 1]; | |
918 | while (isspace(*c) || *c == '\0') | |
919 | *c-- = '\0'; | |
920 | } | |
921 | ||
922 | if (vendor[0] && model[0]) | |
923 | sprintf(buf, "%.64s %.64s", vendor, model); | |
924 | else | |
925 | switch (type) { /* numbers from hald/linux/device.c */ | |
926 | case 1: sprintf(buf, "tape"); break; | |
927 | case 2: sprintf(buf, "printer"); break; | |
928 | case 3: sprintf(buf, "processor"); break; | |
929 | case 4: | |
930 | case 5: sprintf(buf, "cdrom"); break; | |
931 | case 6: sprintf(buf, "scanner"); break; | |
932 | case 8: sprintf(buf, "media_changer"); break; | |
933 | case 9: sprintf(buf, "comm"); break; | |
934 | case 12: sprintf(buf, "raid"); break; | |
935 | default: sprintf(buf, "unknown"); | |
936 | } | |
937 | } else | |
938 | buf[0] = '\0'; | |
939 | free(device); | |
940 | ||
941 | /* chop device path to 'host%d' and calculate the port number */ | |
942 | c = strchr(&path[hba_len], '/'); | |
943 | *c = '\0'; | |
944 | if (sscanf(&path[hba_len], "host%d", &port) == 1) | |
945 | port -= host_base; | |
946 | else { | |
947 | if (verbose) { | |
948 | *c = '/'; /* repair the full string */ | |
949 | fprintf(stderr, Name ": failed to determine port number for %s\n", | |
950 | path); | |
951 | } | |
952 | err = 2; | |
953 | break; | |
954 | } | |
955 | ||
956 | /* mark this port as used */ | |
957 | port_mask &= ~(1 << port); | |
958 | ||
959 | /* print out the device information */ | |
960 | if (buf[0]) { | |
961 | printf(" Port%d : - non-disk device (%s) -\n", port, buf); | |
962 | continue; | |
963 | } | |
964 | ||
965 | fd = dev_open(ent->d_name, O_RDONLY); | |
966 | if (fd < 0) | |
967 | printf(" Port%d : - disk info unavailable -\n", port); | |
968 | else { | |
969 | fd2devname(fd, buf); | |
970 | printf(" Port%d : %s", port, buf); | |
971 | if (imsm_read_serial(fd, NULL, (__u8 *) buf) == 0) | |
972 | printf(" (%s)\n", buf); | |
973 | else | |
974 | printf("()\n"); | |
975 | } | |
976 | close(fd); | |
977 | free(path); | |
978 | path = NULL; | |
979 | } | |
980 | if (path) | |
981 | free(path); | |
982 | if (dir) | |
983 | closedir(dir); | |
984 | if (err == 0) { | |
985 | int i; | |
986 | ||
987 | for (i = 0; i < port_count; i++) | |
988 | if (port_mask & (1 << i)) | |
989 | printf(" Port%d : - no device attached -\n", i); | |
990 | } | |
991 | ||
992 | return err; | |
993 | } | |
994 | ||
5615172f | 995 | static int detail_platform_imsm(int verbose, int enumerate_only) |
d665cc31 DW |
996 | { |
997 | /* There are two components to imsm platform support, the ahci SATA | |
998 | * controller and the option-rom. To find the SATA controller we | |
999 | * simply look in /sys/bus/pci/drivers/ahci to see if an ahci | |
1000 | * controller with the Intel vendor id is present. This approach | |
1001 | * allows mdadm to leverage the kernel's ahci detection logic, with the | |
1002 | * caveat that if ahci.ko is not loaded mdadm will not be able to | |
1003 | * detect platform raid capabilities. The option-rom resides in a | |
1004 | * platform "Adapter ROM". We scan for its signature to retrieve the | |
1005 | * platform capabilities. If raid support is disabled in the BIOS the | |
1006 | * option-rom capability structure will not be available. | |
1007 | */ | |
1008 | const struct imsm_orom *orom; | |
1009 | struct sys_dev *list, *hba; | |
1010 | DIR *dir; | |
1011 | struct dirent *ent; | |
1012 | const char *hba_path; | |
1013 | int host_base = 0; | |
1014 | int port_count = 0; | |
1015 | ||
5615172f DW |
1016 | if (enumerate_only) { |
1017 | if (check_env("IMSM_NO_PLATFORM") || find_imsm_orom()) | |
1018 | return 0; | |
1019 | return 2; | |
1020 | } | |
1021 | ||
d665cc31 DW |
1022 | list = find_driver_devices("pci", "ahci"); |
1023 | for (hba = list; hba; hba = hba->next) | |
1024 | if (devpath_to_vendor(hba->path) == 0x8086) | |
1025 | break; | |
1026 | ||
1027 | if (!hba) { | |
1028 | if (verbose) | |
1029 | fprintf(stderr, Name ": unable to find active ahci controller\n"); | |
1030 | free_sys_dev(&list); | |
1031 | return 2; | |
1032 | } else if (verbose) | |
1033 | fprintf(stderr, Name ": found Intel SATA AHCI Controller\n"); | |
1034 | hba_path = hba->path; | |
1035 | hba->path = NULL; | |
1036 | free_sys_dev(&list); | |
1037 | ||
1038 | orom = find_imsm_orom(); | |
1039 | if (!orom) { | |
1040 | if (verbose) | |
1041 | fprintf(stderr, Name ": imsm option-rom not found\n"); | |
1042 | return 2; | |
1043 | } | |
1044 | ||
1045 | printf(" Platform : Intel(R) Matrix Storage Manager\n"); | |
1046 | printf(" Version : %d.%d.%d.%d\n", orom->major_ver, orom->minor_ver, | |
1047 | orom->hotfix_ver, orom->build); | |
1048 | printf(" RAID Levels :%s%s%s%s%s\n", | |
1049 | imsm_orom_has_raid0(orom) ? " raid0" : "", | |
1050 | imsm_orom_has_raid1(orom) ? " raid1" : "", | |
1051 | imsm_orom_has_raid1e(orom) ? " raid1e" : "", | |
1052 | imsm_orom_has_raid10(orom) ? " raid10" : "", | |
1053 | imsm_orom_has_raid5(orom) ? " raid5" : ""); | |
8be094f0 DW |
1054 | printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n", |
1055 | imsm_orom_has_chunk(orom, 2) ? " 2k" : "", | |
1056 | imsm_orom_has_chunk(orom, 4) ? " 4k" : "", | |
1057 | imsm_orom_has_chunk(orom, 8) ? " 8k" : "", | |
1058 | imsm_orom_has_chunk(orom, 16) ? " 16k" : "", | |
1059 | imsm_orom_has_chunk(orom, 32) ? " 32k" : "", | |
1060 | imsm_orom_has_chunk(orom, 64) ? " 64k" : "", | |
1061 | imsm_orom_has_chunk(orom, 128) ? " 128k" : "", | |
1062 | imsm_orom_has_chunk(orom, 256) ? " 256k" : "", | |
1063 | imsm_orom_has_chunk(orom, 512) ? " 512k" : "", | |
1064 | imsm_orom_has_chunk(orom, 1024*1) ? " 1M" : "", | |
1065 | imsm_orom_has_chunk(orom, 1024*2) ? " 2M" : "", | |
1066 | imsm_orom_has_chunk(orom, 1024*4) ? " 4M" : "", | |
1067 | imsm_orom_has_chunk(orom, 1024*8) ? " 8M" : "", | |
1068 | imsm_orom_has_chunk(orom, 1024*16) ? " 16M" : "", | |
1069 | imsm_orom_has_chunk(orom, 1024*32) ? " 32M" : "", | |
1070 | imsm_orom_has_chunk(orom, 1024*64) ? " 64M" : ""); | |
d665cc31 DW |
1071 | printf(" Max Disks : %d\n", orom->tds); |
1072 | printf(" Max Volumes : %d\n", orom->vpa); | |
1073 | printf(" I/O Controller : %s\n", hba_path); | |
1074 | ||
1075 | /* find the smallest scsi host number to determine a port number base */ | |
1076 | dir = opendir(hba_path); | |
1077 | for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) { | |
1078 | int host; | |
1079 | ||
1080 | if (sscanf(ent->d_name, "host%d", &host) != 1) | |
1081 | continue; | |
1082 | if (port_count == 0) | |
1083 | host_base = host; | |
1084 | else if (host < host_base) | |
1085 | host_base = host; | |
1086 | ||
1087 | if (host + 1 > port_count + host_base) | |
1088 | port_count = host + 1 - host_base; | |
1089 | ||
1090 | } | |
1091 | if (dir) | |
1092 | closedir(dir); | |
1093 | ||
1094 | if (!port_count || imsm_enumerate_ports(hba_path, port_count, | |
1095 | host_base, verbose) != 0) { | |
1096 | if (verbose) | |
1097 | fprintf(stderr, Name ": failed to enumerate ports\n"); | |
1098 | return 2; | |
1099 | } | |
1100 | ||
1101 | return 0; | |
1102 | } | |
cdddbdbc DW |
1103 | #endif |
1104 | ||
1105 | static int match_home_imsm(struct supertype *st, char *homehost) | |
1106 | { | |
5115ca67 DW |
1107 | /* the imsm metadata format does not specify any host |
1108 | * identification information. We return -1 since we can never | |
1109 | * confirm nor deny whether a given array is "meant" for this | |
148acb7b | 1110 | * host. We rely on compare_super and the 'family_num' fields to |
5115ca67 DW |
1111 | * exclude member disks that do not belong, and we rely on |
1112 | * mdadm.conf to specify the arrays that should be assembled. | |
1113 | * Auto-assembly may still pick up "foreign" arrays. | |
1114 | */ | |
cdddbdbc | 1115 | |
9362c1c8 | 1116 | return -1; |
cdddbdbc DW |
1117 | } |
1118 | ||
1119 | static void uuid_from_super_imsm(struct supertype *st, int uuid[4]) | |
1120 | { | |
51006d85 N |
1121 | /* The uuid returned here is used for: |
1122 | * uuid to put into bitmap file (Create, Grow) | |
1123 | * uuid for backup header when saving critical section (Grow) | |
1124 | * comparing uuids when re-adding a device into an array | |
1125 | * In these cases the uuid required is that of the data-array, | |
1126 | * not the device-set. | |
1127 | * uuid to recognise same set when adding a missing device back | |
1128 | * to an array. This is a uuid for the device-set. | |
1129 | * | |
1130 | * For each of these we can make do with a truncated | |
1131 | * or hashed uuid rather than the original, as long as | |
1132 | * everyone agrees. | |
1133 | * In each case the uuid required is that of the data-array, | |
1134 | * not the device-set. | |
43dad3d6 | 1135 | */ |
51006d85 N |
1136 | /* imsm does not track uuid's so we synthesis one using sha1 on |
1137 | * - The signature (Which is constant for all imsm array, but no matter) | |
148acb7b | 1138 | * - the orig_family_num of the container |
51006d85 N |
1139 | * - the index number of the volume |
1140 | * - the 'serial' number of the volume. | |
1141 | * Hopefully these are all constant. | |
1142 | */ | |
1143 | struct intel_super *super = st->sb; | |
43dad3d6 | 1144 | |
51006d85 N |
1145 | char buf[20]; |
1146 | struct sha1_ctx ctx; | |
1147 | struct imsm_dev *dev = NULL; | |
148acb7b | 1148 | __u32 family_num; |
51006d85 | 1149 | |
148acb7b DW |
1150 | /* some mdadm versions failed to set ->orig_family_num, in which |
1151 | * case fall back to ->family_num. orig_family_num will be | |
1152 | * fixed up with the first metadata update. | |
1153 | */ | |
1154 | family_num = super->anchor->orig_family_num; | |
1155 | if (family_num == 0) | |
1156 | family_num = super->anchor->family_num; | |
51006d85 | 1157 | sha1_init_ctx(&ctx); |
92bd8f8d | 1158 | sha1_process_bytes(super->anchor->sig, MPB_SIG_LEN, &ctx); |
148acb7b | 1159 | sha1_process_bytes(&family_num, sizeof(__u32), &ctx); |
51006d85 N |
1160 | if (super->current_vol >= 0) |
1161 | dev = get_imsm_dev(super, super->current_vol); | |
1162 | if (dev) { | |
1163 | __u32 vol = super->current_vol; | |
1164 | sha1_process_bytes(&vol, sizeof(vol), &ctx); | |
1165 | sha1_process_bytes(dev->volume, MAX_RAID_SERIAL_LEN, &ctx); | |
1166 | } | |
1167 | sha1_finish_ctx(&ctx, buf); | |
1168 | memcpy(uuid, buf, 4*4); | |
cdddbdbc DW |
1169 | } |
1170 | ||
0d481d37 | 1171 | #if 0 |
4f5bc454 DW |
1172 | static void |
1173 | get_imsm_numerical_version(struct imsm_super *mpb, int *m, int *p) | |
cdddbdbc | 1174 | { |
cdddbdbc DW |
1175 | __u8 *v = get_imsm_version(mpb); |
1176 | __u8 *end = mpb->sig + MAX_SIGNATURE_LENGTH; | |
1177 | char major[] = { 0, 0, 0 }; | |
1178 | char minor[] = { 0 ,0, 0 }; | |
1179 | char patch[] = { 0, 0, 0 }; | |
1180 | char *ver_parse[] = { major, minor, patch }; | |
1181 | int i, j; | |
1182 | ||
1183 | i = j = 0; | |
1184 | while (*v != '\0' && v < end) { | |
1185 | if (*v != '.' && j < 2) | |
1186 | ver_parse[i][j++] = *v; | |
1187 | else { | |
1188 | i++; | |
1189 | j = 0; | |
1190 | } | |
1191 | v++; | |
1192 | } | |
1193 | ||
4f5bc454 DW |
1194 | *m = strtol(minor, NULL, 0); |
1195 | *p = strtol(patch, NULL, 0); | |
1196 | } | |
0d481d37 | 1197 | #endif |
4f5bc454 | 1198 | |
c2c087e6 DW |
1199 | static int imsm_level_to_layout(int level) |
1200 | { | |
1201 | switch (level) { | |
1202 | case 0: | |
1203 | case 1: | |
1204 | return 0; | |
1205 | case 5: | |
1206 | case 6: | |
a380c027 | 1207 | return ALGORITHM_LEFT_ASYMMETRIC; |
c2c087e6 | 1208 | case 10: |
c92a2527 | 1209 | return 0x102; |
c2c087e6 | 1210 | } |
a18a888e | 1211 | return UnSet; |
c2c087e6 DW |
1212 | } |
1213 | ||
bf5a934a DW |
1214 | static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info) |
1215 | { | |
1216 | struct intel_super *super = st->sb; | |
949c47a0 | 1217 | struct imsm_dev *dev = get_imsm_dev(super, super->current_vol); |
a965f303 | 1218 | struct imsm_map *map = get_imsm_map(dev, 0); |
efb30e7f | 1219 | struct dl *dl; |
bf5a934a | 1220 | |
efb30e7f DW |
1221 | for (dl = super->disks; dl; dl = dl->next) |
1222 | if (dl->raiddisk == info->disk.raid_disk) | |
1223 | break; | |
bf5a934a DW |
1224 | info->container_member = super->current_vol; |
1225 | info->array.raid_disks = map->num_members; | |
1226 | info->array.level = get_imsm_raid_level(map); | |
1227 | info->array.layout = imsm_level_to_layout(info->array.level); | |
1228 | info->array.md_minor = -1; | |
1229 | info->array.ctime = 0; | |
1230 | info->array.utime = 0; | |
301406c9 DW |
1231 | info->array.chunk_size = __le16_to_cpu(map->blocks_per_strip) << 9; |
1232 | info->array.state = !dev->vol.dirty; | |
da9b4a62 DW |
1233 | info->custom_array_size = __le32_to_cpu(dev->size_high); |
1234 | info->custom_array_size <<= 32; | |
1235 | info->custom_array_size |= __le32_to_cpu(dev->size_low); | |
301406c9 DW |
1236 | |
1237 | info->disk.major = 0; | |
1238 | info->disk.minor = 0; | |
efb30e7f DW |
1239 | if (dl) { |
1240 | info->disk.major = dl->major; | |
1241 | info->disk.minor = dl->minor; | |
1242 | } | |
bf5a934a DW |
1243 | |
1244 | info->data_offset = __le32_to_cpu(map->pba_of_lba0); | |
1245 | info->component_size = __le32_to_cpu(map->blocks_per_member); | |
301406c9 | 1246 | memset(info->uuid, 0, sizeof(info->uuid)); |
bf5a934a | 1247 | |
f8f603f1 | 1248 | if (map->map_state == IMSM_T_STATE_UNINITIALIZED || dev->vol.dirty) |
301406c9 | 1249 | info->resync_start = 0; |
f8f603f1 | 1250 | else if (dev->vol.migr_state) |
da188789 DW |
1251 | /* FIXME add curr_migr_unit to resync_start conversion */ |
1252 | info->resync_start = 0; | |
301406c9 DW |
1253 | else |
1254 | info->resync_start = ~0ULL; | |
1255 | ||
1256 | strncpy(info->name, (char *) dev->volume, MAX_RAID_SERIAL_LEN); | |
1257 | info->name[MAX_RAID_SERIAL_LEN] = 0; | |
bf5a934a | 1258 | |
f35f2525 N |
1259 | info->array.major_version = -1; |
1260 | info->array.minor_version = -2; | |
bf5a934a DW |
1261 | sprintf(info->text_version, "/%s/%d", |
1262 | devnum2devname(st->container_dev), | |
1263 | info->container_member); | |
a67dd8cc | 1264 | info->safe_mode_delay = 4000; /* 4 secs like the Matrix driver */ |
51006d85 | 1265 | uuid_from_super_imsm(st, info->uuid); |
bf5a934a DW |
1266 | } |
1267 | ||
7a70e8aa DW |
1268 | /* check the config file to see if we can return a real uuid for this spare */ |
1269 | static void fixup_container_spare_uuid(struct mdinfo *inf) | |
1270 | { | |
1271 | struct mddev_ident_s *array_list; | |
1272 | ||
1273 | if (inf->array.level != LEVEL_CONTAINER || | |
1274 | memcmp(inf->uuid, uuid_match_any, sizeof(int[4])) != 0) | |
1275 | return; | |
1276 | ||
1277 | array_list = conf_get_ident(NULL); | |
1278 | ||
1279 | for (; array_list; array_list = array_list->next) { | |
1280 | if (array_list->uuid_set) { | |
1281 | struct supertype *_sst; /* spare supertype */ | |
1282 | struct supertype *_cst; /* container supertype */ | |
1283 | ||
1284 | _cst = array_list->st; | |
7e8545e9 DW |
1285 | if (_cst) |
1286 | _sst = _cst->ss->match_metadata_desc(inf->text_version); | |
1287 | else | |
1288 | _sst = NULL; | |
1289 | ||
7a70e8aa DW |
1290 | if (_sst) { |
1291 | memcpy(inf->uuid, array_list->uuid, sizeof(int[4])); | |
1292 | free(_sst); | |
1293 | break; | |
1294 | } | |
1295 | } | |
1296 | } | |
1297 | } | |
bf5a934a | 1298 | |
4f5bc454 DW |
1299 | static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info) |
1300 | { | |
1301 | struct intel_super *super = st->sb; | |
4f5bc454 DW |
1302 | struct imsm_disk *disk; |
1303 | __u32 s; | |
4f5bc454 | 1304 | |
bf5a934a DW |
1305 | if (super->current_vol >= 0) { |
1306 | getinfo_super_imsm_volume(st, info); | |
1307 | return; | |
1308 | } | |
d23fe947 DW |
1309 | |
1310 | /* Set raid_disks to zero so that Assemble will always pull in valid | |
1311 | * spares | |
1312 | */ | |
1313 | info->array.raid_disks = 0; | |
cdddbdbc DW |
1314 | info->array.level = LEVEL_CONTAINER; |
1315 | info->array.layout = 0; | |
1316 | info->array.md_minor = -1; | |
c2c087e6 | 1317 | info->array.ctime = 0; /* N/A for imsm */ |
cdddbdbc DW |
1318 | info->array.utime = 0; |
1319 | info->array.chunk_size = 0; | |
1320 | ||
1321 | info->disk.major = 0; | |
1322 | info->disk.minor = 0; | |
cdddbdbc | 1323 | info->disk.raid_disk = -1; |
c2c087e6 | 1324 | info->reshape_active = 0; |
f35f2525 N |
1325 | info->array.major_version = -1; |
1326 | info->array.minor_version = -2; | |
c2c087e6 | 1327 | strcpy(info->text_version, "imsm"); |
a67dd8cc | 1328 | info->safe_mode_delay = 0; |
c2c087e6 DW |
1329 | info->disk.number = -1; |
1330 | info->disk.state = 0; | |
c5afc314 | 1331 | info->name[0] = 0; |
c2c087e6 | 1332 | |
4a04ec6c | 1333 | if (super->disks) { |
14e8215b DW |
1334 | __u32 reserved = imsm_reserved_sectors(super, super->disks); |
1335 | ||
b9f594fe | 1336 | disk = &super->disks->disk; |
14e8215b DW |
1337 | info->data_offset = __le32_to_cpu(disk->total_blocks) - reserved; |
1338 | info->component_size = reserved; | |
f2f27e63 | 1339 | s = disk->status; |
4a04ec6c | 1340 | info->disk.state = s & CONFIGURED_DISK ? (1 << MD_DISK_ACTIVE) : 0; |
df474657 DW |
1341 | /* we don't change info->disk.raid_disk here because |
1342 | * this state will be finalized in mdmon after we have | |
1343 | * found the 'most fresh' version of the metadata | |
1344 | */ | |
1345 | info->disk.state |= s & FAILED_DISK ? (1 << MD_DISK_FAULTY) : 0; | |
032e9e29 | 1346 | info->disk.state |= s & SPARE_DISK ? 0 : (1 << MD_DISK_SYNC); |
cdddbdbc | 1347 | } |
a575e2a7 DW |
1348 | |
1349 | /* only call uuid_from_super_imsm when this disk is part of a populated container, | |
1350 | * ->compare_super may have updated the 'num_raid_devs' field for spares | |
1351 | */ | |
1352 | if (info->disk.state & (1 << MD_DISK_SYNC) || super->anchor->num_raid_devs) | |
36ba7d48 | 1353 | uuid_from_super_imsm(st, info->uuid); |
7a70e8aa | 1354 | else { |
032e9e29 | 1355 | memcpy(info->uuid, uuid_match_any, sizeof(int[4])); |
7a70e8aa DW |
1356 | fixup_container_spare_uuid(info); |
1357 | } | |
cdddbdbc DW |
1358 | } |
1359 | ||
cdddbdbc DW |
1360 | static int update_super_imsm(struct supertype *st, struct mdinfo *info, |
1361 | char *update, char *devname, int verbose, | |
1362 | int uuid_set, char *homehost) | |
1363 | { | |
f352c545 DW |
1364 | /* FIXME */ |
1365 | ||
1366 | /* For 'assemble' and 'force' we need to return non-zero if any | |
1367 | * change was made. For others, the return value is ignored. | |
1368 | * Update options are: | |
1369 | * force-one : This device looks a bit old but needs to be included, | |
1370 | * update age info appropriately. | |
1371 | * assemble: clear any 'faulty' flag to allow this device to | |
1372 | * be assembled. | |
1373 | * force-array: Array is degraded but being forced, mark it clean | |
1374 | * if that will be needed to assemble it. | |
1375 | * | |
1376 | * newdev: not used ???? | |
1377 | * grow: Array has gained a new device - this is currently for | |
1378 | * linear only | |
1379 | * resync: mark as dirty so a resync will happen. | |
1380 | * name: update the name - preserving the homehost | |
1381 | * | |
1382 | * Following are not relevant for this imsm: | |
1383 | * sparc2.2 : update from old dodgey metadata | |
1384 | * super-minor: change the preferred_minor number | |
1385 | * summaries: update redundant counters. | |
1386 | * uuid: Change the uuid of the array to match watch is given | |
1387 | * homehost: update the recorded homehost | |
1388 | * _reshape_progress: record new reshape_progress position. | |
1389 | */ | |
1390 | int rv = 0; | |
1391 | //struct intel_super *super = st->sb; | |
1392 | //struct imsm_super *mpb = super->mpb; | |
1393 | ||
1394 | if (strcmp(update, "grow") == 0) { | |
1395 | } | |
1396 | if (strcmp(update, "resync") == 0) { | |
1397 | /* dev->vol.dirty = 1; */ | |
1398 | } | |
1399 | ||
1400 | /* IMSM has no concept of UUID or homehost */ | |
1401 | ||
1402 | return rv; | |
cdddbdbc DW |
1403 | } |
1404 | ||
c2c087e6 | 1405 | static size_t disks_to_mpb_size(int disks) |
cdddbdbc | 1406 | { |
c2c087e6 | 1407 | size_t size; |
cdddbdbc | 1408 | |
c2c087e6 DW |
1409 | size = sizeof(struct imsm_super); |
1410 | size += (disks - 1) * sizeof(struct imsm_disk); | |
1411 | size += 2 * sizeof(struct imsm_dev); | |
1412 | /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */ | |
1413 | size += (4 - 2) * sizeof(struct imsm_map); | |
1414 | /* 4 possible disk_ord_tbl's */ | |
1415 | size += 4 * (disks - 1) * sizeof(__u32); | |
1416 | ||
1417 | return size; | |
1418 | } | |
1419 | ||
1420 | static __u64 avail_size_imsm(struct supertype *st, __u64 devsize) | |
1421 | { | |
1422 | if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS)) | |
1423 | return 0; | |
1424 | ||
1425 | return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS); | |
cdddbdbc DW |
1426 | } |
1427 | ||
ba2de7ba DW |
1428 | static void free_devlist(struct intel_super *super) |
1429 | { | |
1430 | struct intel_dev *dv; | |
1431 | ||
1432 | while (super->devlist) { | |
1433 | dv = super->devlist->next; | |
1434 | free(super->devlist->dev); | |
1435 | free(super->devlist); | |
1436 | super->devlist = dv; | |
1437 | } | |
1438 | } | |
1439 | ||
1440 | static void imsm_copy_dev(struct imsm_dev *dest, struct imsm_dev *src) | |
1441 | { | |
1442 | memcpy(dest, src, sizeof_imsm_dev(src, 0)); | |
1443 | } | |
1444 | ||
cdddbdbc DW |
1445 | static int compare_super_imsm(struct supertype *st, struct supertype *tst) |
1446 | { | |
1447 | /* | |
1448 | * return: | |
1449 | * 0 same, or first was empty, and second was copied | |
1450 | * 1 second had wrong number | |
1451 | * 2 wrong uuid | |
1452 | * 3 wrong other info | |
1453 | */ | |
1454 | struct intel_super *first = st->sb; | |
1455 | struct intel_super *sec = tst->sb; | |
1456 | ||
1457 | if (!first) { | |
1458 | st->sb = tst->sb; | |
1459 | tst->sb = NULL; | |
1460 | return 0; | |
1461 | } | |
1462 | ||
949c47a0 | 1463 | if (memcmp(first->anchor->sig, sec->anchor->sig, MAX_SIGNATURE_LENGTH) != 0) |
cdddbdbc | 1464 | return 3; |
d23fe947 DW |
1465 | |
1466 | /* if an anchor does not have num_raid_devs set then it is a free | |
1467 | * floating spare | |
1468 | */ | |
1469 | if (first->anchor->num_raid_devs > 0 && | |
1470 | sec->anchor->num_raid_devs > 0) { | |
148acb7b DW |
1471 | if (first->anchor->orig_family_num != sec->anchor->orig_family_num || |
1472 | first->anchor->family_num != sec->anchor->family_num) | |
d23fe947 | 1473 | return 3; |
d23fe947 | 1474 | } |
cdddbdbc | 1475 | |
3e372e5a DW |
1476 | /* if 'first' is a spare promote it to a populated mpb with sec's |
1477 | * family number | |
1478 | */ | |
1479 | if (first->anchor->num_raid_devs == 0 && | |
1480 | sec->anchor->num_raid_devs > 0) { | |
78d30f94 | 1481 | int i; |
ba2de7ba DW |
1482 | struct intel_dev *dv; |
1483 | struct imsm_dev *dev; | |
78d30f94 DW |
1484 | |
1485 | /* we need to copy raid device info from sec if an allocation | |
1486 | * fails here we don't associate the spare | |
1487 | */ | |
1488 | for (i = 0; i < sec->anchor->num_raid_devs; i++) { | |
ba2de7ba DW |
1489 | dv = malloc(sizeof(*dv)); |
1490 | if (!dv) | |
1491 | break; | |
1492 | dev = malloc(sizeof_imsm_dev(get_imsm_dev(sec, i), 1)); | |
1493 | if (!dev) { | |
1494 | free(dv); | |
1495 | break; | |
78d30f94 | 1496 | } |
ba2de7ba DW |
1497 | dv->dev = dev; |
1498 | dv->index = i; | |
1499 | dv->next = first->devlist; | |
1500 | first->devlist = dv; | |
78d30f94 | 1501 | } |
ba2de7ba DW |
1502 | if (i <= sec->anchor->num_raid_devs) { |
1503 | /* allocation failure */ | |
1504 | free_devlist(first); | |
1505 | fprintf(stderr, "imsm: failed to associate spare\n"); | |
1506 | return 3; | |
78d30f94 | 1507 | } |
ba2de7ba DW |
1508 | for (i = 0; i < sec->anchor->num_raid_devs; i++) |
1509 | imsm_copy_dev(get_imsm_dev(first, i), get_imsm_dev(sec, i)); | |
78d30f94 | 1510 | |
3e372e5a | 1511 | first->anchor->num_raid_devs = sec->anchor->num_raid_devs; |
148acb7b | 1512 | first->anchor->orig_family_num = sec->anchor->orig_family_num; |
3e372e5a DW |
1513 | first->anchor->family_num = sec->anchor->family_num; |
1514 | } | |
1515 | ||
cdddbdbc DW |
1516 | return 0; |
1517 | } | |
1518 | ||
0030e8d6 DW |
1519 | static void fd2devname(int fd, char *name) |
1520 | { | |
1521 | struct stat st; | |
1522 | char path[256]; | |
1523 | char dname[100]; | |
1524 | char *nm; | |
1525 | int rv; | |
1526 | ||
1527 | name[0] = '\0'; | |
1528 | if (fstat(fd, &st) != 0) | |
1529 | return; | |
1530 | sprintf(path, "/sys/dev/block/%d:%d", | |
1531 | major(st.st_rdev), minor(st.st_rdev)); | |
1532 | ||
1533 | rv = readlink(path, dname, sizeof(dname)); | |
1534 | if (rv <= 0) | |
1535 | return; | |
1536 | ||
1537 | dname[rv] = '\0'; | |
1538 | nm = strrchr(dname, '/'); | |
1539 | nm++; | |
1540 | snprintf(name, MAX_RAID_SERIAL_LEN, "/dev/%s", nm); | |
1541 | } | |
1542 | ||
1543 | ||
cdddbdbc DW |
1544 | extern int scsi_get_serial(int fd, void *buf, size_t buf_len); |
1545 | ||
1546 | static int imsm_read_serial(int fd, char *devname, | |
1547 | __u8 serial[MAX_RAID_SERIAL_LEN]) | |
1548 | { | |
1549 | unsigned char scsi_serial[255]; | |
cdddbdbc DW |
1550 | int rv; |
1551 | int rsp_len; | |
1f24f035 | 1552 | int len; |
316e2bf4 DW |
1553 | char *dest; |
1554 | char *src; | |
1555 | char *rsp_buf; | |
1556 | int i; | |
cdddbdbc DW |
1557 | |
1558 | memset(scsi_serial, 0, sizeof(scsi_serial)); | |
cdddbdbc | 1559 | |
f9ba0ff1 DW |
1560 | rv = scsi_get_serial(fd, scsi_serial, sizeof(scsi_serial)); |
1561 | ||
40ebbb9c | 1562 | if (rv && check_env("IMSM_DEVNAME_AS_SERIAL")) { |
f9ba0ff1 DW |
1563 | memset(serial, 0, MAX_RAID_SERIAL_LEN); |
1564 | fd2devname(fd, (char *) serial); | |
0030e8d6 DW |
1565 | return 0; |
1566 | } | |
1567 | ||
cdddbdbc DW |
1568 | if (rv != 0) { |
1569 | if (devname) | |
1570 | fprintf(stderr, | |
1571 | Name ": Failed to retrieve serial for %s\n", | |
1572 | devname); | |
1573 | return rv; | |
1574 | } | |
1575 | ||
1576 | rsp_len = scsi_serial[3]; | |
03cd4cc8 DW |
1577 | if (!rsp_len) { |
1578 | if (devname) | |
1579 | fprintf(stderr, | |
1580 | Name ": Failed to retrieve serial for %s\n", | |
1581 | devname); | |
1582 | return 2; | |
1583 | } | |
1f24f035 | 1584 | rsp_buf = (char *) &scsi_serial[4]; |
5c3db629 | 1585 | |
316e2bf4 DW |
1586 | /* trim all whitespace and non-printable characters and convert |
1587 | * ':' to ';' | |
1588 | */ | |
1589 | for (i = 0, dest = rsp_buf; i < rsp_len; i++) { | |
1590 | src = &rsp_buf[i]; | |
1591 | if (*src > 0x20) { | |
1592 | /* ':' is reserved for use in placeholder serial | |
1593 | * numbers for missing disks | |
1594 | */ | |
1595 | if (*src == ':') | |
1596 | *dest++ = ';'; | |
1597 | else | |
1598 | *dest++ = *src; | |
1599 | } | |
1600 | } | |
1601 | len = dest - rsp_buf; | |
1602 | dest = rsp_buf; | |
1603 | ||
1604 | /* truncate leading characters */ | |
1605 | if (len > MAX_RAID_SERIAL_LEN) { | |
1606 | dest += len - MAX_RAID_SERIAL_LEN; | |
1f24f035 | 1607 | len = MAX_RAID_SERIAL_LEN; |
316e2bf4 | 1608 | } |
5c3db629 | 1609 | |
5c3db629 | 1610 | memset(serial, 0, MAX_RAID_SERIAL_LEN); |
316e2bf4 | 1611 | memcpy(serial, dest, len); |
cdddbdbc DW |
1612 | |
1613 | return 0; | |
1614 | } | |
1615 | ||
1f24f035 DW |
1616 | static int serialcmp(__u8 *s1, __u8 *s2) |
1617 | { | |
1618 | return strncmp((char *) s1, (char *) s2, MAX_RAID_SERIAL_LEN); | |
1619 | } | |
1620 | ||
1621 | static void serialcpy(__u8 *dest, __u8 *src) | |
1622 | { | |
1623 | strncpy((char *) dest, (char *) src, MAX_RAID_SERIAL_LEN); | |
1624 | } | |
1625 | ||
54c2c1ea DW |
1626 | static struct dl *serial_to_dl(__u8 *serial, struct intel_super *super) |
1627 | { | |
1628 | struct dl *dl; | |
1629 | ||
1630 | for (dl = super->disks; dl; dl = dl->next) | |
1631 | if (serialcmp(dl->serial, serial) == 0) | |
1632 | break; | |
1633 | ||
1634 | return dl; | |
1635 | } | |
1636 | ||
cdddbdbc DW |
1637 | static int |
1638 | load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd) | |
1639 | { | |
cdddbdbc DW |
1640 | struct dl *dl; |
1641 | struct stat stb; | |
cdddbdbc DW |
1642 | int rv; |
1643 | int i; | |
d23fe947 DW |
1644 | int alloc = 1; |
1645 | __u8 serial[MAX_RAID_SERIAL_LEN]; | |
1646 | ||
1647 | rv = imsm_read_serial(fd, devname, serial); | |
1648 | ||
1649 | if (rv != 0) | |
1650 | return 2; | |
1651 | ||
1652 | /* check if this is a disk we have seen before. it may be a spare in | |
1653 | * super->disks while the current anchor believes it is a raid member, | |
1654 | * check if we need to update dl->index | |
1655 | */ | |
54c2c1ea | 1656 | dl = serial_to_dl(serial, super); |
d23fe947 DW |
1657 | if (!dl) |
1658 | dl = malloc(sizeof(*dl)); | |
1659 | else | |
1660 | alloc = 0; | |
cdddbdbc | 1661 | |
b9f594fe | 1662 | if (!dl) { |
cdddbdbc DW |
1663 | if (devname) |
1664 | fprintf(stderr, | |
1665 | Name ": failed to allocate disk buffer for %s\n", | |
1666 | devname); | |
1667 | return 2; | |
1668 | } | |
cdddbdbc | 1669 | |
d23fe947 DW |
1670 | if (alloc) { |
1671 | fstat(fd, &stb); | |
1672 | dl->major = major(stb.st_rdev); | |
1673 | dl->minor = minor(stb.st_rdev); | |
1674 | dl->next = super->disks; | |
1675 | dl->fd = keep_fd ? fd : -1; | |
1676 | dl->devname = devname ? strdup(devname) : NULL; | |
1f24f035 | 1677 | serialcpy(dl->serial, serial); |
8796fdc4 | 1678 | dl->index = -2; |
0dcecb2e | 1679 | dl->e = NULL; |
d23fe947 DW |
1680 | } else if (keep_fd) { |
1681 | close(dl->fd); | |
1682 | dl->fd = fd; | |
1683 | } | |
cdddbdbc | 1684 | |
d23fe947 | 1685 | /* look up this disk's index in the current anchor */ |
949c47a0 DW |
1686 | for (i = 0; i < super->anchor->num_disks; i++) { |
1687 | struct imsm_disk *disk_iter; | |
1688 | ||
1689 | disk_iter = __get_imsm_disk(super->anchor, i); | |
cdddbdbc | 1690 | |
1f24f035 | 1691 | if (serialcmp(disk_iter->serial, dl->serial) == 0) { |
b9f594fe | 1692 | dl->disk = *disk_iter; |
d23fe947 DW |
1693 | /* only set index on disks that are a member of a |
1694 | * populated contianer, i.e. one with raid_devs | |
1695 | */ | |
f2f27e63 | 1696 | if (dl->disk.status & FAILED_DISK) |
6c386dd3 | 1697 | dl->index = -2; |
f2f27e63 | 1698 | else if (dl->disk.status & SPARE_DISK) |
d23fe947 DW |
1699 | dl->index = -1; |
1700 | else | |
1701 | dl->index = i; | |
8796fdc4 | 1702 | |
cdddbdbc | 1703 | break; |
949c47a0 | 1704 | } |
cdddbdbc DW |
1705 | } |
1706 | ||
3f6efecc DW |
1707 | /* no match, maybe a stale failed drive */ |
1708 | if (i == super->anchor->num_disks && dl->index >= 0) { | |
1709 | dl->disk = *__get_imsm_disk(super->anchor, dl->index); | |
f2f27e63 | 1710 | if (dl->disk.status & FAILED_DISK) |
3f6efecc DW |
1711 | dl->index = -2; |
1712 | } | |
1713 | ||
d23fe947 DW |
1714 | if (alloc) |
1715 | super->disks = dl; | |
6c386dd3 | 1716 | |
949c47a0 DW |
1717 | return 0; |
1718 | } | |
1719 | ||
0e600426 | 1720 | #ifndef MDASSEMBLE |
0c046afd DW |
1721 | /* When migrating map0 contains the 'destination' state while map1 |
1722 | * contains the current state. When not migrating map0 contains the | |
1723 | * current state. This routine assumes that map[0].map_state is set to | |
1724 | * the current array state before being called. | |
1725 | * | |
1726 | * Migration is indicated by one of the following states | |
1727 | * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed) | |
e3bba0e0 | 1728 | * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal |
0c046afd | 1729 | * map1state=unitialized) |
1484e727 | 1730 | * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal |
0c046afd | 1731 | * map1state=normal) |
e3bba0e0 | 1732 | * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal |
0c046afd DW |
1733 | * map1state=degraded) |
1734 | */ | |
0556e1a2 | 1735 | static void migrate(struct imsm_dev *dev, __u8 to_state, int migr_type) |
3393c6af | 1736 | { |
0c046afd | 1737 | struct imsm_map *dest; |
3393c6af DW |
1738 | struct imsm_map *src = get_imsm_map(dev, 0); |
1739 | ||
0c046afd | 1740 | dev->vol.migr_state = 1; |
1484e727 | 1741 | set_migr_type(dev, migr_type); |
f8f603f1 | 1742 | dev->vol.curr_migr_unit = 0; |
0c046afd DW |
1743 | dest = get_imsm_map(dev, 1); |
1744 | ||
0556e1a2 | 1745 | /* duplicate and then set the target end state in map[0] */ |
3393c6af | 1746 | memcpy(dest, src, sizeof_imsm_map(src)); |
0556e1a2 DW |
1747 | if (migr_type == MIGR_REBUILD) { |
1748 | __u32 ord; | |
1749 | int i; | |
1750 | ||
1751 | for (i = 0; i < src->num_members; i++) { | |
1752 | ord = __le32_to_cpu(src->disk_ord_tbl[i]); | |
1753 | set_imsm_ord_tbl_ent(src, i, ord_to_idx(ord)); | |
1754 | } | |
1755 | } | |
1756 | ||
0c046afd | 1757 | src->map_state = to_state; |
949c47a0 | 1758 | } |
f8f603f1 DW |
1759 | |
1760 | static void end_migration(struct imsm_dev *dev, __u8 map_state) | |
1761 | { | |
1762 | struct imsm_map *map = get_imsm_map(dev, 0); | |
0556e1a2 DW |
1763 | struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state); |
1764 | int i; | |
1765 | ||
1766 | /* merge any IMSM_ORD_REBUILD bits that were not successfully | |
1767 | * completed in the last migration. | |
1768 | * | |
1769 | * FIXME add support for online capacity expansion and | |
1770 | * raid-level-migration | |
1771 | */ | |
1772 | for (i = 0; i < prev->num_members; i++) | |
1773 | map->disk_ord_tbl[i] |= prev->disk_ord_tbl[i]; | |
f8f603f1 DW |
1774 | |
1775 | dev->vol.migr_state = 0; | |
1776 | dev->vol.curr_migr_unit = 0; | |
1777 | map->map_state = map_state; | |
1778 | } | |
0e600426 | 1779 | #endif |
949c47a0 DW |
1780 | |
1781 | static int parse_raid_devices(struct intel_super *super) | |
1782 | { | |
1783 | int i; | |
1784 | struct imsm_dev *dev_new; | |
4d7b1503 DW |
1785 | size_t len, len_migr; |
1786 | size_t space_needed = 0; | |
1787 | struct imsm_super *mpb = super->anchor; | |
949c47a0 DW |
1788 | |
1789 | for (i = 0; i < super->anchor->num_raid_devs; i++) { | |
1790 | struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i); | |
ba2de7ba | 1791 | struct intel_dev *dv; |
949c47a0 | 1792 | |
4d7b1503 DW |
1793 | len = sizeof_imsm_dev(dev_iter, 0); |
1794 | len_migr = sizeof_imsm_dev(dev_iter, 1); | |
1795 | if (len_migr > len) | |
1796 | space_needed += len_migr - len; | |
1797 | ||
ba2de7ba DW |
1798 | dv = malloc(sizeof(*dv)); |
1799 | if (!dv) | |
1800 | return 1; | |
4d7b1503 | 1801 | dev_new = malloc(len_migr); |
ba2de7ba DW |
1802 | if (!dev_new) { |
1803 | free(dv); | |
949c47a0 | 1804 | return 1; |
ba2de7ba | 1805 | } |
949c47a0 | 1806 | imsm_copy_dev(dev_new, dev_iter); |
ba2de7ba DW |
1807 | dv->dev = dev_new; |
1808 | dv->index = i; | |
1809 | dv->next = super->devlist; | |
1810 | super->devlist = dv; | |
949c47a0 | 1811 | } |
cdddbdbc | 1812 | |
4d7b1503 DW |
1813 | /* ensure that super->buf is large enough when all raid devices |
1814 | * are migrating | |
1815 | */ | |
1816 | if (__le32_to_cpu(mpb->mpb_size) + space_needed > super->len) { | |
1817 | void *buf; | |
1818 | ||
1819 | len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + space_needed, 512); | |
1820 | if (posix_memalign(&buf, 512, len) != 0) | |
1821 | return 1; | |
1822 | ||
1f45a8ad DW |
1823 | memcpy(buf, super->buf, super->len); |
1824 | memset(buf + super->len, 0, len - super->len); | |
4d7b1503 DW |
1825 | free(super->buf); |
1826 | super->buf = buf; | |
1827 | super->len = len; | |
1828 | } | |
1829 | ||
cdddbdbc DW |
1830 | return 0; |
1831 | } | |
1832 | ||
604b746f JD |
1833 | /* retrieve a pointer to the bbm log which starts after all raid devices */ |
1834 | struct bbm_log *__get_imsm_bbm_log(struct imsm_super *mpb) | |
1835 | { | |
1836 | void *ptr = NULL; | |
1837 | ||
1838 | if (__le32_to_cpu(mpb->bbm_log_size)) { | |
1839 | ptr = mpb; | |
1840 | ptr += mpb->mpb_size - __le32_to_cpu(mpb->bbm_log_size); | |
1841 | } | |
1842 | ||
1843 | return ptr; | |
1844 | } | |
1845 | ||
d23fe947 | 1846 | static void __free_imsm(struct intel_super *super, int free_disks); |
9ca2c81c | 1847 | |
cdddbdbc DW |
1848 | /* load_imsm_mpb - read matrix metadata |
1849 | * allocates super->mpb to be freed by free_super | |
1850 | */ | |
1851 | static int load_imsm_mpb(int fd, struct intel_super *super, char *devname) | |
1852 | { | |
1853 | unsigned long long dsize; | |
cdddbdbc DW |
1854 | unsigned long long sectors; |
1855 | struct stat; | |
6416d527 | 1856 | struct imsm_super *anchor; |
cdddbdbc | 1857 | __u32 check_sum; |
949c47a0 | 1858 | int rc; |
cdddbdbc | 1859 | |
cdddbdbc DW |
1860 | get_dev_size(fd, NULL, &dsize); |
1861 | ||
1862 | if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0) { | |
1863 | if (devname) | |
1864 | fprintf(stderr, | |
1865 | Name ": Cannot seek to anchor block on %s: %s\n", | |
1866 | devname, strerror(errno)); | |
1867 | return 1; | |
1868 | } | |
1869 | ||
949c47a0 | 1870 | if (posix_memalign((void**)&anchor, 512, 512) != 0) { |
ad97895e DW |
1871 | if (devname) |
1872 | fprintf(stderr, | |
1873 | Name ": Failed to allocate imsm anchor buffer" | |
1874 | " on %s\n", devname); | |
1875 | return 1; | |
1876 | } | |
949c47a0 | 1877 | if (read(fd, anchor, 512) != 512) { |
cdddbdbc DW |
1878 | if (devname) |
1879 | fprintf(stderr, | |
1880 | Name ": Cannot read anchor block on %s: %s\n", | |
1881 | devname, strerror(errno)); | |
6416d527 | 1882 | free(anchor); |
cdddbdbc DW |
1883 | return 1; |
1884 | } | |
1885 | ||
6416d527 | 1886 | if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) { |
cdddbdbc DW |
1887 | if (devname) |
1888 | fprintf(stderr, | |
1889 | Name ": no IMSM anchor on %s\n", devname); | |
6416d527 | 1890 | free(anchor); |
cdddbdbc DW |
1891 | return 2; |
1892 | } | |
1893 | ||
d23fe947 | 1894 | __free_imsm(super, 0); |
949c47a0 DW |
1895 | super->len = ROUND_UP(anchor->mpb_size, 512); |
1896 | if (posix_memalign(&super->buf, 512, super->len) != 0) { | |
cdddbdbc DW |
1897 | if (devname) |
1898 | fprintf(stderr, | |
1899 | Name ": unable to allocate %zu byte mpb buffer\n", | |
949c47a0 | 1900 | super->len); |
6416d527 | 1901 | free(anchor); |
cdddbdbc DW |
1902 | return 2; |
1903 | } | |
949c47a0 | 1904 | memcpy(super->buf, anchor, 512); |
cdddbdbc | 1905 | |
6416d527 NB |
1906 | sectors = mpb_sectors(anchor) - 1; |
1907 | free(anchor); | |
949c47a0 | 1908 | if (!sectors) { |
ecf45690 DW |
1909 | check_sum = __gen_imsm_checksum(super->anchor); |
1910 | if (check_sum != __le32_to_cpu(super->anchor->check_sum)) { | |
1911 | if (devname) | |
1912 | fprintf(stderr, | |
1913 | Name ": IMSM checksum %x != %x on %s\n", | |
1914 | check_sum, | |
1915 | __le32_to_cpu(super->anchor->check_sum), | |
1916 | devname); | |
1917 | return 2; | |
1918 | } | |
1919 | ||
949c47a0 DW |
1920 | rc = load_imsm_disk(fd, super, devname, 0); |
1921 | if (rc == 0) | |
1922 | rc = parse_raid_devices(super); | |
1923 | return rc; | |
1924 | } | |
cdddbdbc DW |
1925 | |
1926 | /* read the extended mpb */ | |
1927 | if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0) { | |
1928 | if (devname) | |
1929 | fprintf(stderr, | |
1930 | Name ": Cannot seek to extended mpb on %s: %s\n", | |
1931 | devname, strerror(errno)); | |
1932 | return 1; | |
1933 | } | |
1934 | ||
949c47a0 | 1935 | if (read(fd, super->buf + 512, super->len - 512) != super->len - 512) { |
cdddbdbc DW |
1936 | if (devname) |
1937 | fprintf(stderr, | |
1938 | Name ": Cannot read extended mpb on %s: %s\n", | |
1939 | devname, strerror(errno)); | |
1940 | return 2; | |
1941 | } | |
1942 | ||
949c47a0 DW |
1943 | check_sum = __gen_imsm_checksum(super->anchor); |
1944 | if (check_sum != __le32_to_cpu(super->anchor->check_sum)) { | |
cdddbdbc DW |
1945 | if (devname) |
1946 | fprintf(stderr, | |
1947 | Name ": IMSM checksum %x != %x on %s\n", | |
949c47a0 | 1948 | check_sum, __le32_to_cpu(super->anchor->check_sum), |
cdddbdbc | 1949 | devname); |
db575f3b | 1950 | return 3; |
cdddbdbc DW |
1951 | } |
1952 | ||
604b746f JD |
1953 | /* FIXME the BBM log is disk specific so we cannot use this global |
1954 | * buffer for all disks. Ok for now since we only look at the global | |
1955 | * bbm_log_size parameter to gate assembly | |
1956 | */ | |
1957 | super->bbm_log = __get_imsm_bbm_log(super->anchor); | |
1958 | ||
949c47a0 DW |
1959 | rc = load_imsm_disk(fd, super, devname, 0); |
1960 | if (rc == 0) | |
1961 | rc = parse_raid_devices(super); | |
4d7b1503 | 1962 | |
949c47a0 | 1963 | return rc; |
cdddbdbc DW |
1964 | } |
1965 | ||
ae6aad82 DW |
1966 | static void __free_imsm_disk(struct dl *d) |
1967 | { | |
1968 | if (d->fd >= 0) | |
1969 | close(d->fd); | |
1970 | if (d->devname) | |
1971 | free(d->devname); | |
0dcecb2e DW |
1972 | if (d->e) |
1973 | free(d->e); | |
ae6aad82 DW |
1974 | free(d); |
1975 | ||
1976 | } | |
cdddbdbc DW |
1977 | static void free_imsm_disks(struct intel_super *super) |
1978 | { | |
47ee5a45 | 1979 | struct dl *d; |
cdddbdbc | 1980 | |
47ee5a45 DW |
1981 | while (super->disks) { |
1982 | d = super->disks; | |
cdddbdbc | 1983 | super->disks = d->next; |
ae6aad82 | 1984 | __free_imsm_disk(d); |
cdddbdbc | 1985 | } |
47ee5a45 DW |
1986 | while (super->missing) { |
1987 | d = super->missing; | |
1988 | super->missing = d->next; | |
1989 | __free_imsm_disk(d); | |
1990 | } | |
1991 | ||
cdddbdbc DW |
1992 | } |
1993 | ||
9ca2c81c | 1994 | /* free all the pieces hanging off of a super pointer */ |
d23fe947 | 1995 | static void __free_imsm(struct intel_super *super, int free_disks) |
cdddbdbc | 1996 | { |
9ca2c81c | 1997 | if (super->buf) { |
949c47a0 | 1998 | free(super->buf); |
9ca2c81c DW |
1999 | super->buf = NULL; |
2000 | } | |
d23fe947 DW |
2001 | if (free_disks) |
2002 | free_imsm_disks(super); | |
ba2de7ba | 2003 | free_devlist(super); |
88c32bb1 DW |
2004 | if (super->hba) { |
2005 | free((void *) super->hba); | |
2006 | super->hba = NULL; | |
2007 | } | |
cdddbdbc DW |
2008 | } |
2009 | ||
9ca2c81c DW |
2010 | static void free_imsm(struct intel_super *super) |
2011 | { | |
d23fe947 | 2012 | __free_imsm(super, 1); |
9ca2c81c DW |
2013 | free(super); |
2014 | } | |
cdddbdbc DW |
2015 | |
2016 | static void free_super_imsm(struct supertype *st) | |
2017 | { | |
2018 | struct intel_super *super = st->sb; | |
2019 | ||
2020 | if (!super) | |
2021 | return; | |
2022 | ||
2023 | free_imsm(super); | |
2024 | st->sb = NULL; | |
2025 | } | |
2026 | ||
c2c087e6 DW |
2027 | static struct intel_super *alloc_super(int creating_imsm) |
2028 | { | |
2029 | struct intel_super *super = malloc(sizeof(*super)); | |
2030 | ||
2031 | if (super) { | |
2032 | memset(super, 0, sizeof(*super)); | |
2033 | super->creating_imsm = creating_imsm; | |
bf5a934a | 2034 | super->current_vol = -1; |
0dcecb2e | 2035 | super->create_offset = ~((__u32 ) 0); |
88c32bb1 DW |
2036 | if (!check_env("IMSM_NO_PLATFORM")) |
2037 | super->orom = find_imsm_orom(); | |
cceebc67 | 2038 | if (super->orom && !check_env("IMSM_TEST_OROM")) { |
88c32bb1 DW |
2039 | struct sys_dev *list, *ent; |
2040 | ||
2041 | /* find the first intel ahci controller */ | |
2042 | list = find_driver_devices("pci", "ahci"); | |
2043 | for (ent = list; ent; ent = ent->next) | |
2044 | if (devpath_to_vendor(ent->path) == 0x8086) | |
2045 | break; | |
2046 | if (ent) { | |
2047 | super->hba = ent->path; | |
2048 | ent->path = NULL; | |
2049 | } | |
2050 | free_sys_dev(&list); | |
2051 | } | |
c2c087e6 DW |
2052 | } |
2053 | ||
2054 | return super; | |
2055 | } | |
2056 | ||
cdddbdbc | 2057 | #ifndef MDASSEMBLE |
47ee5a45 DW |
2058 | /* find_missing - helper routine for load_super_imsm_all that identifies |
2059 | * disks that have disappeared from the system. This routine relies on | |
2060 | * the mpb being uptodate, which it is at load time. | |
2061 | */ | |
2062 | static int find_missing(struct intel_super *super) | |
2063 | { | |
2064 | int i; | |
2065 | struct imsm_super *mpb = super->anchor; | |
2066 | struct dl *dl; | |
2067 | struct imsm_disk *disk; | |
47ee5a45 DW |
2068 | |
2069 | for (i = 0; i < mpb->num_disks; i++) { | |
2070 | disk = __get_imsm_disk(mpb, i); | |
54c2c1ea | 2071 | dl = serial_to_dl(disk->serial, super); |
47ee5a45 DW |
2072 | if (dl) |
2073 | continue; | |
47ee5a45 DW |
2074 | |
2075 | dl = malloc(sizeof(*dl)); | |
2076 | if (!dl) | |
2077 | return 1; | |
2078 | dl->major = 0; | |
2079 | dl->minor = 0; | |
2080 | dl->fd = -1; | |
2081 | dl->devname = strdup("missing"); | |
2082 | dl->index = i; | |
2083 | serialcpy(dl->serial, disk->serial); | |
2084 | dl->disk = *disk; | |
689c9bf3 | 2085 | dl->e = NULL; |
47ee5a45 DW |
2086 | dl->next = super->missing; |
2087 | super->missing = dl; | |
2088 | } | |
2089 | ||
2090 | return 0; | |
2091 | } | |
2092 | ||
cdddbdbc DW |
2093 | static int load_super_imsm_all(struct supertype *st, int fd, void **sbp, |
2094 | char *devname, int keep_fd) | |
2095 | { | |
2096 | struct mdinfo *sra; | |
2097 | struct intel_super *super; | |
2098 | struct mdinfo *sd, *best = NULL; | |
2099 | __u32 bestgen = 0; | |
2100 | __u32 gen; | |
2101 | char nm[20]; | |
2102 | int dfd; | |
2103 | int rv; | |
db575f3b DW |
2104 | int devnum = fd2devnum(fd); |
2105 | int retry; | |
dab4a513 | 2106 | enum sysfs_read_flags flags; |
cdddbdbc | 2107 | |
dab4a513 DW |
2108 | flags = GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE; |
2109 | if (mdmon_running(devnum)) | |
2110 | flags |= SKIP_GONE_DEVS; | |
2111 | ||
2112 | /* check if 'fd' an opened container */ | |
2113 | sra = sysfs_read(fd, 0, flags); | |
cdddbdbc DW |
2114 | if (!sra) |
2115 | return 1; | |
2116 | ||
2117 | if (sra->array.major_version != -1 || | |
2118 | sra->array.minor_version != -2 || | |
2119 | strcmp(sra->text_version, "imsm") != 0) | |
2120 | return 1; | |
2121 | ||
c2c087e6 | 2122 | super = alloc_super(0); |
cdddbdbc DW |
2123 | if (!super) |
2124 | return 1; | |
2125 | ||
d23fe947 | 2126 | /* find the most up to date disk in this array, skipping spares */ |
cdddbdbc DW |
2127 | for (sd = sra->devs; sd; sd = sd->next) { |
2128 | sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor); | |
2129 | dfd = dev_open(nm, keep_fd ? O_RDWR : O_RDONLY); | |
be2c0e38 | 2130 | if (dfd < 0) { |
cdddbdbc DW |
2131 | free_imsm(super); |
2132 | return 2; | |
2133 | } | |
2134 | rv = load_imsm_mpb(dfd, super, NULL); | |
db575f3b DW |
2135 | |
2136 | /* retry the load if we might have raced against mdmon */ | |
2137 | if (rv == 3 && mdmon_running(devnum)) | |
2138 | for (retry = 0; retry < 3; retry++) { | |
2139 | usleep(3000); | |
2140 | rv = load_imsm_mpb(dfd, super, NULL); | |
2141 | if (rv != 3) | |
2142 | break; | |
2143 | } | |
cdddbdbc DW |
2144 | if (!keep_fd) |
2145 | close(dfd); | |
2146 | if (rv == 0) { | |
d23fe947 DW |
2147 | if (super->anchor->num_raid_devs == 0) |
2148 | gen = 0; | |
2149 | else | |
2150 | gen = __le32_to_cpu(super->anchor->generation_num); | |
cdddbdbc DW |
2151 | if (!best || gen > bestgen) { |
2152 | bestgen = gen; | |
2153 | best = sd; | |
2154 | } | |
2155 | } else { | |
2156 | free_imsm(super); | |
db575f3b | 2157 | return rv; |
cdddbdbc DW |
2158 | } |
2159 | } | |
2160 | ||
2161 | if (!best) { | |
2162 | free_imsm(super); | |
2163 | return 1; | |
2164 | } | |
2165 | ||
2166 | /* load the most up to date anchor */ | |
2167 | sprintf(nm, "%d:%d", best->disk.major, best->disk.minor); | |
2168 | dfd = dev_open(nm, O_RDONLY); | |
be2c0e38 | 2169 | if (dfd < 0) { |
cdddbdbc DW |
2170 | free_imsm(super); |
2171 | return 1; | |
2172 | } | |
2173 | rv = load_imsm_mpb(dfd, super, NULL); | |
2174 | close(dfd); | |
2175 | if (rv != 0) { | |
2176 | free_imsm(super); | |
2177 | return 2; | |
2178 | } | |
2179 | ||
d23fe947 | 2180 | /* re-parse the disk list with the current anchor */ |
cdddbdbc DW |
2181 | for (sd = sra->devs ; sd ; sd = sd->next) { |
2182 | sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor); | |
2183 | dfd = dev_open(nm, keep_fd? O_RDWR : O_RDONLY); | |
be2c0e38 | 2184 | if (dfd < 0) { |
cdddbdbc DW |
2185 | free_imsm(super); |
2186 | return 2; | |
2187 | } | |
2188 | load_imsm_disk(dfd, super, NULL, keep_fd); | |
2189 | if (!keep_fd) | |
2190 | close(dfd); | |
2191 | } | |
2192 | ||
47ee5a45 DW |
2193 | |
2194 | if (find_missing(super) != 0) { | |
2195 | free_imsm(super); | |
2196 | return 2; | |
2197 | } | |
2198 | ||
f7e7067b | 2199 | if (st->subarray[0]) { |
949c47a0 | 2200 | if (atoi(st->subarray) <= super->anchor->num_raid_devs) |
bf5a934a | 2201 | super->current_vol = atoi(st->subarray); |
af99d9ca DW |
2202 | else { |
2203 | free_imsm(super); | |
bf5a934a | 2204 | return 1; |
af99d9ca | 2205 | } |
f7e7067b NB |
2206 | } |
2207 | ||
cdddbdbc | 2208 | *sbp = super; |
db575f3b | 2209 | st->container_dev = devnum; |
cdddbdbc | 2210 | if (st->ss == NULL) { |
bf5a934a | 2211 | st->ss = &super_imsm; |
cdddbdbc DW |
2212 | st->minor_version = 0; |
2213 | st->max_devs = IMSM_MAX_DEVICES; | |
2214 | } | |
352452c3 | 2215 | st->loaded_container = 1; |
cdddbdbc DW |
2216 | |
2217 | return 0; | |
2218 | } | |
2219 | #endif | |
2220 | ||
2221 | static int load_super_imsm(struct supertype *st, int fd, char *devname) | |
2222 | { | |
2223 | struct intel_super *super; | |
2224 | int rv; | |
2225 | ||
2226 | #ifndef MDASSEMBLE | |
3dbccbcf | 2227 | if (load_super_imsm_all(st, fd, &st->sb, devname, 1) == 0) |
cdddbdbc DW |
2228 | return 0; |
2229 | #endif | |
2230 | ||
37424f13 DW |
2231 | free_super_imsm(st); |
2232 | ||
c2c087e6 | 2233 | super = alloc_super(0); |
cdddbdbc DW |
2234 | if (!super) { |
2235 | fprintf(stderr, | |
2236 | Name ": malloc of %zu failed.\n", | |
2237 | sizeof(*super)); | |
2238 | return 1; | |
2239 | } | |
2240 | ||
2241 | rv = load_imsm_mpb(fd, super, devname); | |
2242 | ||
2243 | if (rv) { | |
2244 | if (devname) | |
2245 | fprintf(stderr, | |
2246 | Name ": Failed to load all information " | |
2247 | "sections on %s\n", devname); | |
2248 | free_imsm(super); | |
2249 | return rv; | |
2250 | } | |
2251 | ||
af99d9ca DW |
2252 | if (st->subarray[0]) { |
2253 | if (atoi(st->subarray) <= super->anchor->num_raid_devs) | |
2254 | super->current_vol = atoi(st->subarray); | |
2255 | else { | |
2256 | free_imsm(super); | |
2257 | return 1; | |
2258 | } | |
2259 | } | |
2260 | ||
cdddbdbc DW |
2261 | st->sb = super; |
2262 | if (st->ss == NULL) { | |
2263 | st->ss = &super_imsm; | |
2264 | st->minor_version = 0; | |
2265 | st->max_devs = IMSM_MAX_DEVICES; | |
2266 | } | |
352452c3 | 2267 | st->loaded_container = 0; |
cdddbdbc DW |
2268 | |
2269 | return 0; | |
2270 | } | |
2271 | ||
ef6ffade DW |
2272 | static __u16 info_to_blocks_per_strip(mdu_array_info_t *info) |
2273 | { | |
2274 | if (info->level == 1) | |
2275 | return 128; | |
2276 | return info->chunk_size >> 9; | |
2277 | } | |
2278 | ||
ff596308 | 2279 | static __u32 info_to_num_data_stripes(mdu_array_info_t *info, int num_domains) |
ef6ffade DW |
2280 | { |
2281 | __u32 num_stripes; | |
2282 | ||
2283 | num_stripes = (info->size * 2) / info_to_blocks_per_strip(info); | |
ff596308 | 2284 | num_stripes /= num_domains; |
ef6ffade DW |
2285 | |
2286 | return num_stripes; | |
2287 | } | |
2288 | ||
fcfd9599 DW |
2289 | static __u32 info_to_blocks_per_member(mdu_array_info_t *info) |
2290 | { | |
4025c288 DW |
2291 | if (info->level == 1) |
2292 | return info->size * 2; | |
2293 | else | |
2294 | return (info->size * 2) & ~(info_to_blocks_per_strip(info) - 1); | |
fcfd9599 DW |
2295 | } |
2296 | ||
4d1313e9 DW |
2297 | static void imsm_update_version_info(struct intel_super *super) |
2298 | { | |
2299 | /* update the version and attributes */ | |
2300 | struct imsm_super *mpb = super->anchor; | |
2301 | char *version; | |
2302 | struct imsm_dev *dev; | |
2303 | struct imsm_map *map; | |
2304 | int i; | |
2305 | ||
2306 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
2307 | dev = get_imsm_dev(super, i); | |
2308 | map = get_imsm_map(dev, 0); | |
2309 | if (__le32_to_cpu(dev->size_high) > 0) | |
2310 | mpb->attributes |= MPB_ATTRIB_2TB; | |
2311 | ||
2312 | /* FIXME detect when an array spans a port multiplier */ | |
2313 | #if 0 | |
2314 | mpb->attributes |= MPB_ATTRIB_PM; | |
2315 | #endif | |
2316 | ||
2317 | if (mpb->num_raid_devs > 1 || | |
2318 | mpb->attributes != MPB_ATTRIB_CHECKSUM_VERIFY) { | |
2319 | version = MPB_VERSION_ATTRIBS; | |
2320 | switch (get_imsm_raid_level(map)) { | |
2321 | case 0: mpb->attributes |= MPB_ATTRIB_RAID0; break; | |
2322 | case 1: mpb->attributes |= MPB_ATTRIB_RAID1; break; | |
2323 | case 10: mpb->attributes |= MPB_ATTRIB_RAID10; break; | |
2324 | case 5: mpb->attributes |= MPB_ATTRIB_RAID5; break; | |
2325 | } | |
2326 | } else { | |
2327 | if (map->num_members >= 5) | |
2328 | version = MPB_VERSION_5OR6_DISK_ARRAY; | |
2329 | else if (dev->status == DEV_CLONE_N_GO) | |
2330 | version = MPB_VERSION_CNG; | |
2331 | else if (get_imsm_raid_level(map) == 5) | |
2332 | version = MPB_VERSION_RAID5; | |
2333 | else if (map->num_members >= 3) | |
2334 | version = MPB_VERSION_3OR4_DISK_ARRAY; | |
2335 | else if (get_imsm_raid_level(map) == 1) | |
2336 | version = MPB_VERSION_RAID1; | |
2337 | else | |
2338 | version = MPB_VERSION_RAID0; | |
2339 | } | |
2340 | strcpy(((char *) mpb->sig) + strlen(MPB_SIGNATURE), version); | |
2341 | } | |
2342 | } | |
2343 | ||
8b353278 DW |
2344 | static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info, |
2345 | unsigned long long size, char *name, | |
2346 | char *homehost, int *uuid) | |
cdddbdbc | 2347 | { |
c2c087e6 DW |
2348 | /* We are creating a volume inside a pre-existing container. |
2349 | * so st->sb is already set. | |
2350 | */ | |
2351 | struct intel_super *super = st->sb; | |
949c47a0 | 2352 | struct imsm_super *mpb = super->anchor; |
ba2de7ba | 2353 | struct intel_dev *dv; |
c2c087e6 DW |
2354 | struct imsm_dev *dev; |
2355 | struct imsm_vol *vol; | |
2356 | struct imsm_map *map; | |
2357 | int idx = mpb->num_raid_devs; | |
2358 | int i; | |
2359 | unsigned long long array_blocks; | |
2c092cad | 2360 | size_t size_old, size_new; |
ff596308 | 2361 | __u32 num_data_stripes; |
cdddbdbc | 2362 | |
88c32bb1 | 2363 | if (super->orom && mpb->num_raid_devs >= super->orom->vpa) { |
c2c087e6 | 2364 | fprintf(stderr, Name": This imsm-container already has the " |
88c32bb1 | 2365 | "maximum of %d volumes\n", super->orom->vpa); |
c2c087e6 DW |
2366 | return 0; |
2367 | } | |
2368 | ||
2c092cad DW |
2369 | /* ensure the mpb is large enough for the new data */ |
2370 | size_old = __le32_to_cpu(mpb->mpb_size); | |
2371 | size_new = disks_to_mpb_size(info->nr_disks); | |
2372 | if (size_new > size_old) { | |
2373 | void *mpb_new; | |
2374 | size_t size_round = ROUND_UP(size_new, 512); | |
2375 | ||
2376 | if (posix_memalign(&mpb_new, 512, size_round) != 0) { | |
2377 | fprintf(stderr, Name": could not allocate new mpb\n"); | |
2378 | return 0; | |
2379 | } | |
2380 | memcpy(mpb_new, mpb, size_old); | |
2381 | free(mpb); | |
2382 | mpb = mpb_new; | |
949c47a0 | 2383 | super->anchor = mpb_new; |
2c092cad DW |
2384 | mpb->mpb_size = __cpu_to_le32(size_new); |
2385 | memset(mpb_new + size_old, 0, size_round - size_old); | |
2386 | } | |
bf5a934a | 2387 | super->current_vol = idx; |
d23fe947 DW |
2388 | /* when creating the first raid device in this container set num_disks |
2389 | * to zero, i.e. delete this spare and add raid member devices in | |
2390 | * add_to_super_imsm_volume() | |
2391 | */ | |
2392 | if (super->current_vol == 0) | |
2393 | mpb->num_disks = 0; | |
5a038140 DW |
2394 | |
2395 | for (i = 0; i < super->current_vol; i++) { | |
2396 | dev = get_imsm_dev(super, i); | |
2397 | if (strncmp((char *) dev->volume, name, | |
2398 | MAX_RAID_SERIAL_LEN) == 0) { | |
2399 | fprintf(stderr, Name": '%s' is already defined for this container\n", | |
2400 | name); | |
2401 | return 0; | |
2402 | } | |
2403 | } | |
2404 | ||
bf5a934a | 2405 | sprintf(st->subarray, "%d", idx); |
ba2de7ba DW |
2406 | dv = malloc(sizeof(*dv)); |
2407 | if (!dv) { | |
2408 | fprintf(stderr, Name ": failed to allocate device list entry\n"); | |
2409 | return 0; | |
2410 | } | |
949c47a0 DW |
2411 | dev = malloc(sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1)); |
2412 | if (!dev) { | |
ba2de7ba | 2413 | free(dv); |
949c47a0 DW |
2414 | fprintf(stderr, Name": could not allocate raid device\n"); |
2415 | return 0; | |
2416 | } | |
c2c087e6 | 2417 | strncpy((char *) dev->volume, name, MAX_RAID_SERIAL_LEN); |
03bcbc65 DW |
2418 | if (info->level == 1) |
2419 | array_blocks = info_to_blocks_per_member(info); | |
2420 | else | |
2421 | array_blocks = calc_array_size(info->level, info->raid_disks, | |
2422 | info->layout, info->chunk_size, | |
2423 | info->size*2); | |
979d38be DW |
2424 | /* round array size down to closest MB */ |
2425 | array_blocks = (array_blocks >> SECT_PER_MB_SHIFT) << SECT_PER_MB_SHIFT; | |
2426 | ||
c2c087e6 DW |
2427 | dev->size_low = __cpu_to_le32((__u32) array_blocks); |
2428 | dev->size_high = __cpu_to_le32((__u32) (array_blocks >> 32)); | |
2429 | dev->status = __cpu_to_le32(0); | |
2430 | dev->reserved_blocks = __cpu_to_le32(0); | |
2431 | vol = &dev->vol; | |
2432 | vol->migr_state = 0; | |
1484e727 | 2433 | set_migr_type(dev, MIGR_INIT); |
c2c087e6 | 2434 | vol->dirty = 0; |
f8f603f1 | 2435 | vol->curr_migr_unit = 0; |
a965f303 | 2436 | map = get_imsm_map(dev, 0); |
0dcecb2e | 2437 | map->pba_of_lba0 = __cpu_to_le32(super->create_offset); |
fcfd9599 | 2438 | map->blocks_per_member = __cpu_to_le32(info_to_blocks_per_member(info)); |
ef6ffade | 2439 | map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info)); |
0556e1a2 | 2440 | map->failed_disk_num = ~0; |
c2c087e6 DW |
2441 | map->map_state = info->level ? IMSM_T_STATE_UNINITIALIZED : |
2442 | IMSM_T_STATE_NORMAL; | |
252d23c0 | 2443 | map->ddf = 1; |
ef6ffade DW |
2444 | |
2445 | if (info->level == 1 && info->raid_disks > 2) { | |
2446 | fprintf(stderr, Name": imsm does not support more than 2 disks" | |
2447 | "in a raid1 volume\n"); | |
2448 | return 0; | |
2449 | } | |
81062a36 DW |
2450 | |
2451 | map->raid_level = info->level; | |
4d1313e9 | 2452 | if (info->level == 10) { |
c2c087e6 | 2453 | map->raid_level = 1; |
4d1313e9 | 2454 | map->num_domains = info->raid_disks / 2; |
81062a36 DW |
2455 | } else if (info->level == 1) |
2456 | map->num_domains = info->raid_disks; | |
2457 | else | |
ff596308 | 2458 | map->num_domains = 1; |
81062a36 | 2459 | |
ff596308 DW |
2460 | num_data_stripes = info_to_num_data_stripes(info, map->num_domains); |
2461 | map->num_data_stripes = __cpu_to_le32(num_data_stripes); | |
ef6ffade | 2462 | |
c2c087e6 DW |
2463 | map->num_members = info->raid_disks; |
2464 | for (i = 0; i < map->num_members; i++) { | |
2465 | /* initialized in add_to_super */ | |
be73972f | 2466 | set_imsm_ord_tbl_ent(map, i, 0); |
c2c087e6 | 2467 | } |
949c47a0 | 2468 | mpb->num_raid_devs++; |
ba2de7ba DW |
2469 | |
2470 | dv->dev = dev; | |
2471 | dv->index = super->current_vol; | |
2472 | dv->next = super->devlist; | |
2473 | super->devlist = dv; | |
c2c087e6 | 2474 | |
4d1313e9 DW |
2475 | imsm_update_version_info(super); |
2476 | ||
c2c087e6 | 2477 | return 1; |
cdddbdbc DW |
2478 | } |
2479 | ||
bf5a934a DW |
2480 | static int init_super_imsm(struct supertype *st, mdu_array_info_t *info, |
2481 | unsigned long long size, char *name, | |
2482 | char *homehost, int *uuid) | |
2483 | { | |
2484 | /* This is primarily called by Create when creating a new array. | |
2485 | * We will then get add_to_super called for each component, and then | |
2486 | * write_init_super called to write it out to each device. | |
2487 | * For IMSM, Create can create on fresh devices or on a pre-existing | |
2488 | * array. | |
2489 | * To create on a pre-existing array a different method will be called. | |
2490 | * This one is just for fresh drives. | |
2491 | */ | |
2492 | struct intel_super *super; | |
2493 | struct imsm_super *mpb; | |
2494 | size_t mpb_size; | |
4d1313e9 | 2495 | char *version; |
bf5a934a DW |
2496 | |
2497 | if (!info) { | |
2498 | st->sb = NULL; | |
2499 | return 0; | |
2500 | } | |
2501 | if (st->sb) | |
2502 | return init_super_imsm_volume(st, info, size, name, homehost, | |
2503 | uuid); | |
2504 | ||
2505 | super = alloc_super(1); | |
2506 | if (!super) | |
2507 | return 0; | |
2508 | mpb_size = disks_to_mpb_size(info->nr_disks); | |
ef649044 | 2509 | if (posix_memalign(&super->buf, 512, mpb_size) != 0) { |
bf5a934a DW |
2510 | free(super); |
2511 | return 0; | |
2512 | } | |
ef649044 | 2513 | mpb = super->buf; |
bf5a934a DW |
2514 | memset(mpb, 0, mpb_size); |
2515 | ||
4d1313e9 DW |
2516 | mpb->attributes = MPB_ATTRIB_CHECKSUM_VERIFY; |
2517 | ||
2518 | version = (char *) mpb->sig; | |
2519 | strcpy(version, MPB_SIGNATURE); | |
2520 | version += strlen(MPB_SIGNATURE); | |
2521 | strcpy(version, MPB_VERSION_RAID0); | |
bf5a934a DW |
2522 | mpb->mpb_size = mpb_size; |
2523 | ||
bf5a934a DW |
2524 | st->sb = super; |
2525 | return 1; | |
2526 | } | |
2527 | ||
0e600426 | 2528 | #ifndef MDASSEMBLE |
f20c3968 | 2529 | static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk, |
bf5a934a DW |
2530 | int fd, char *devname) |
2531 | { | |
2532 | struct intel_super *super = st->sb; | |
d23fe947 | 2533 | struct imsm_super *mpb = super->anchor; |
bf5a934a DW |
2534 | struct dl *dl; |
2535 | struct imsm_dev *dev; | |
2536 | struct imsm_map *map; | |
bf5a934a | 2537 | |
949c47a0 | 2538 | dev = get_imsm_dev(super, super->current_vol); |
a965f303 | 2539 | map = get_imsm_map(dev, 0); |
bf5a934a | 2540 | |
208933a7 N |
2541 | if (! (dk->state & (1<<MD_DISK_SYNC))) { |
2542 | fprintf(stderr, Name ": %s: Cannot add spare devices to IMSM volume\n", | |
2543 | devname); | |
2544 | return 1; | |
2545 | } | |
2546 | ||
efb30e7f DW |
2547 | if (fd == -1) { |
2548 | /* we're doing autolayout so grab the pre-marked (in | |
2549 | * validate_geometry) raid_disk | |
2550 | */ | |
2551 | for (dl = super->disks; dl; dl = dl->next) | |
2552 | if (dl->raiddisk == dk->raid_disk) | |
2553 | break; | |
2554 | } else { | |
2555 | for (dl = super->disks; dl ; dl = dl->next) | |
2556 | if (dl->major == dk->major && | |
2557 | dl->minor == dk->minor) | |
2558 | break; | |
2559 | } | |
d23fe947 | 2560 | |
208933a7 N |
2561 | if (!dl) { |
2562 | fprintf(stderr, Name ": %s is not a member of the same container\n", devname); | |
f20c3968 | 2563 | return 1; |
208933a7 | 2564 | } |
bf5a934a | 2565 | |
d23fe947 DW |
2566 | /* add a pristine spare to the metadata */ |
2567 | if (dl->index < 0) { | |
2568 | dl->index = super->anchor->num_disks; | |
2569 | super->anchor->num_disks++; | |
2570 | } | |
be73972f | 2571 | set_imsm_ord_tbl_ent(map, dk->number, dl->index); |
f2f27e63 | 2572 | dl->disk.status = CONFIGURED_DISK | USABLE_DISK; |
d23fe947 DW |
2573 | |
2574 | /* if we are creating the first raid device update the family number */ | |
2575 | if (super->current_vol == 0) { | |
2576 | __u32 sum; | |
2577 | struct imsm_dev *_dev = __get_imsm_dev(mpb, 0); | |
2578 | struct imsm_disk *_disk = __get_imsm_disk(mpb, dl->index); | |
2579 | ||
2580 | *_dev = *dev; | |
2581 | *_disk = dl->disk; | |
148acb7b DW |
2582 | sum = random32(); |
2583 | sum += __gen_imsm_checksum(mpb); | |
d23fe947 | 2584 | mpb->family_num = __cpu_to_le32(sum); |
148acb7b | 2585 | mpb->orig_family_num = mpb->family_num; |
d23fe947 | 2586 | } |
f20c3968 DW |
2587 | |
2588 | return 0; | |
bf5a934a DW |
2589 | } |
2590 | ||
f20c3968 | 2591 | static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk, |
cdddbdbc DW |
2592 | int fd, char *devname) |
2593 | { | |
c2c087e6 | 2594 | struct intel_super *super = st->sb; |
c2c087e6 DW |
2595 | struct dl *dd; |
2596 | unsigned long long size; | |
f2f27e63 | 2597 | __u32 id; |
c2c087e6 DW |
2598 | int rv; |
2599 | struct stat stb; | |
2600 | ||
88c32bb1 DW |
2601 | /* if we are on an RAID enabled platform check that the disk is |
2602 | * attached to the raid controller | |
2603 | */ | |
2604 | if (super->hba && !disk_attached_to_hba(fd, super->hba)) { | |
2605 | fprintf(stderr, | |
2606 | Name ": %s is not attached to the raid controller: %s\n", | |
2607 | devname ? : "disk", super->hba); | |
2608 | return 1; | |
2609 | } | |
2610 | ||
f20c3968 DW |
2611 | if (super->current_vol >= 0) |
2612 | return add_to_super_imsm_volume(st, dk, fd, devname); | |
bf5a934a | 2613 | |
c2c087e6 DW |
2614 | fstat(fd, &stb); |
2615 | dd = malloc(sizeof(*dd)); | |
b9f594fe | 2616 | if (!dd) { |
c2c087e6 DW |
2617 | fprintf(stderr, |
2618 | Name ": malloc failed %s:%d.\n", __func__, __LINE__); | |
f20c3968 | 2619 | return 1; |
c2c087e6 DW |
2620 | } |
2621 | memset(dd, 0, sizeof(*dd)); | |
2622 | dd->major = major(stb.st_rdev); | |
2623 | dd->minor = minor(stb.st_rdev); | |
b9f594fe | 2624 | dd->index = -1; |
c2c087e6 | 2625 | dd->devname = devname ? strdup(devname) : NULL; |
c2c087e6 | 2626 | dd->fd = fd; |
689c9bf3 | 2627 | dd->e = NULL; |
c2c087e6 DW |
2628 | rv = imsm_read_serial(fd, devname, dd->serial); |
2629 | if (rv) { | |
2630 | fprintf(stderr, | |
0030e8d6 | 2631 | Name ": failed to retrieve scsi serial, aborting\n"); |
949c47a0 | 2632 | free(dd); |
0030e8d6 | 2633 | abort(); |
c2c087e6 DW |
2634 | } |
2635 | ||
c2c087e6 DW |
2636 | get_dev_size(fd, NULL, &size); |
2637 | size /= 512; | |
1f24f035 | 2638 | serialcpy(dd->disk.serial, dd->serial); |
b9f594fe | 2639 | dd->disk.total_blocks = __cpu_to_le32(size); |
f2f27e63 | 2640 | dd->disk.status = USABLE_DISK | SPARE_DISK; |
c2c087e6 | 2641 | if (sysfs_disk_to_scsi_id(fd, &id) == 0) |
b9f594fe | 2642 | dd->disk.scsi_id = __cpu_to_le32(id); |
c2c087e6 | 2643 | else |
b9f594fe | 2644 | dd->disk.scsi_id = __cpu_to_le32(0); |
43dad3d6 DW |
2645 | |
2646 | if (st->update_tail) { | |
2647 | dd->next = super->add; | |
2648 | super->add = dd; | |
2649 | } else { | |
2650 | dd->next = super->disks; | |
2651 | super->disks = dd; | |
2652 | } | |
f20c3968 DW |
2653 | |
2654 | return 0; | |
cdddbdbc DW |
2655 | } |
2656 | ||
c2c087e6 DW |
2657 | static int store_imsm_mpb(int fd, struct intel_super *super); |
2658 | ||
d23fe947 DW |
2659 | /* spare records have their own family number and do not have any defined raid |
2660 | * devices | |
2661 | */ | |
2662 | static int write_super_imsm_spares(struct intel_super *super, int doclose) | |
2663 | { | |
2664 | struct imsm_super mpb_save; | |
2665 | struct imsm_super *mpb = super->anchor; | |
2666 | __u32 sum; | |
2667 | struct dl *d; | |
2668 | ||
2669 | mpb_save = *mpb; | |
2670 | mpb->num_raid_devs = 0; | |
2671 | mpb->num_disks = 1; | |
2672 | mpb->mpb_size = sizeof(struct imsm_super); | |
2673 | mpb->generation_num = __cpu_to_le32(1UL); | |
2674 | ||
2675 | for (d = super->disks; d; d = d->next) { | |
8796fdc4 | 2676 | if (d->index != -1) |
d23fe947 DW |
2677 | continue; |
2678 | ||
2679 | mpb->disk[0] = d->disk; | |
2680 | sum = __gen_imsm_checksum(mpb); | |
2681 | mpb->family_num = __cpu_to_le32(sum); | |
148acb7b | 2682 | mpb->orig_family_num = 0; |
d23fe947 DW |
2683 | sum = __gen_imsm_checksum(mpb); |
2684 | mpb->check_sum = __cpu_to_le32(sum); | |
2685 | ||
2686 | if (store_imsm_mpb(d->fd, super)) { | |
2687 | fprintf(stderr, "%s: failed for device %d:%d %s\n", | |
2688 | __func__, d->major, d->minor, strerror(errno)); | |
2689 | *mpb = mpb_save; | |
e74255d9 | 2690 | return 1; |
d23fe947 DW |
2691 | } |
2692 | if (doclose) { | |
2693 | close(d->fd); | |
2694 | d->fd = -1; | |
2695 | } | |
2696 | } | |
2697 | ||
2698 | *mpb = mpb_save; | |
e74255d9 | 2699 | return 0; |
d23fe947 DW |
2700 | } |
2701 | ||
c2c087e6 | 2702 | static int write_super_imsm(struct intel_super *super, int doclose) |
cdddbdbc | 2703 | { |
949c47a0 | 2704 | struct imsm_super *mpb = super->anchor; |
c2c087e6 DW |
2705 | struct dl *d; |
2706 | __u32 generation; | |
2707 | __u32 sum; | |
d23fe947 | 2708 | int spares = 0; |
949c47a0 | 2709 | int i; |
a48ac0a8 | 2710 | __u32 mpb_size = sizeof(struct imsm_super) - sizeof(struct imsm_disk); |
cdddbdbc | 2711 | |
c2c087e6 DW |
2712 | /* 'generation' is incremented everytime the metadata is written */ |
2713 | generation = __le32_to_cpu(mpb->generation_num); | |
2714 | generation++; | |
2715 | mpb->generation_num = __cpu_to_le32(generation); | |
2716 | ||
148acb7b DW |
2717 | /* fix up cases where previous mdadm releases failed to set |
2718 | * orig_family_num | |
2719 | */ | |
2720 | if (mpb->orig_family_num == 0) | |
2721 | mpb->orig_family_num = mpb->family_num; | |
2722 | ||
1ee1e9fc | 2723 | mpb_size += sizeof(struct imsm_disk) * mpb->num_disks; |
d23fe947 | 2724 | for (d = super->disks; d; d = d->next) { |
8796fdc4 | 2725 | if (d->index == -1) |
d23fe947 | 2726 | spares++; |
1ee1e9fc | 2727 | else |
d23fe947 | 2728 | mpb->disk[d->index] = d->disk; |
d23fe947 | 2729 | } |
47ee5a45 DW |
2730 | for (d = super->missing; d; d = d->next) |
2731 | mpb->disk[d->index] = d->disk; | |
b9f594fe | 2732 | |
949c47a0 DW |
2733 | for (i = 0; i < mpb->num_raid_devs; i++) { |
2734 | struct imsm_dev *dev = __get_imsm_dev(mpb, i); | |
2735 | ||
ba2de7ba | 2736 | imsm_copy_dev(dev, get_imsm_dev(super, i)); |
a48ac0a8 | 2737 | mpb_size += sizeof_imsm_dev(dev, 0); |
949c47a0 | 2738 | } |
a48ac0a8 DW |
2739 | mpb_size += __le32_to_cpu(mpb->bbm_log_size); |
2740 | mpb->mpb_size = __cpu_to_le32(mpb_size); | |
949c47a0 | 2741 | |
c2c087e6 | 2742 | /* recalculate checksum */ |
949c47a0 | 2743 | sum = __gen_imsm_checksum(mpb); |
c2c087e6 DW |
2744 | mpb->check_sum = __cpu_to_le32(sum); |
2745 | ||
d23fe947 | 2746 | /* write the mpb for disks that compose raid devices */ |
c2c087e6 | 2747 | for (d = super->disks; d ; d = d->next) { |
d23fe947 DW |
2748 | if (d->index < 0) |
2749 | continue; | |
8796fdc4 | 2750 | if (store_imsm_mpb(d->fd, super)) |
c2c087e6 DW |
2751 | fprintf(stderr, "%s: failed for device %d:%d %s\n", |
2752 | __func__, d->major, d->minor, strerror(errno)); | |
c2c087e6 DW |
2753 | if (doclose) { |
2754 | close(d->fd); | |
2755 | d->fd = -1; | |
2756 | } | |
2757 | } | |
2758 | ||
d23fe947 DW |
2759 | if (spares) |
2760 | return write_super_imsm_spares(super, doclose); | |
2761 | ||
e74255d9 | 2762 | return 0; |
c2c087e6 DW |
2763 | } |
2764 | ||
0e600426 | 2765 | |
9b1fb677 | 2766 | static int create_array(struct supertype *st, int dev_idx) |
43dad3d6 DW |
2767 | { |
2768 | size_t len; | |
2769 | struct imsm_update_create_array *u; | |
2770 | struct intel_super *super = st->sb; | |
9b1fb677 | 2771 | struct imsm_dev *dev = get_imsm_dev(super, dev_idx); |
54c2c1ea DW |
2772 | struct imsm_map *map = get_imsm_map(dev, 0); |
2773 | struct disk_info *inf; | |
2774 | struct imsm_disk *disk; | |
2775 | int i; | |
43dad3d6 | 2776 | |
54c2c1ea DW |
2777 | len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0) + |
2778 | sizeof(*inf) * map->num_members; | |
43dad3d6 DW |
2779 | u = malloc(len); |
2780 | if (!u) { | |
2781 | fprintf(stderr, "%s: failed to allocate update buffer\n", | |
2782 | __func__); | |
2783 | return 1; | |
2784 | } | |
2785 | ||
2786 | u->type = update_create_array; | |
9b1fb677 | 2787 | u->dev_idx = dev_idx; |
43dad3d6 | 2788 | imsm_copy_dev(&u->dev, dev); |
54c2c1ea DW |
2789 | inf = get_disk_info(u); |
2790 | for (i = 0; i < map->num_members; i++) { | |
9b1fb677 DW |
2791 | int idx = get_imsm_disk_idx(dev, i); |
2792 | ||
54c2c1ea DW |
2793 | disk = get_imsm_disk(super, idx); |
2794 | serialcpy(inf[i].serial, disk->serial); | |
2795 | } | |
43dad3d6 DW |
2796 | append_metadata_update(st, u, len); |
2797 | ||
2798 | return 0; | |
2799 | } | |
2800 | ||
7801ac20 | 2801 | static int _add_disk(struct supertype *st) |
43dad3d6 DW |
2802 | { |
2803 | struct intel_super *super = st->sb; | |
2804 | size_t len; | |
2805 | struct imsm_update_add_disk *u; | |
2806 | ||
2807 | if (!super->add) | |
2808 | return 0; | |
2809 | ||
2810 | len = sizeof(*u); | |
2811 | u = malloc(len); | |
2812 | if (!u) { | |
2813 | fprintf(stderr, "%s: failed to allocate update buffer\n", | |
2814 | __func__); | |
2815 | return 1; | |
2816 | } | |
2817 | ||
2818 | u->type = update_add_disk; | |
2819 | append_metadata_update(st, u, len); | |
2820 | ||
2821 | return 0; | |
2822 | } | |
2823 | ||
c2c087e6 DW |
2824 | static int write_init_super_imsm(struct supertype *st) |
2825 | { | |
9b1fb677 DW |
2826 | struct intel_super *super = st->sb; |
2827 | int current_vol = super->current_vol; | |
2828 | ||
2829 | /* we are done with current_vol reset it to point st at the container */ | |
2830 | super->current_vol = -1; | |
2831 | ||
8273f55e | 2832 | if (st->update_tail) { |
43dad3d6 DW |
2833 | /* queue the recently created array / added disk |
2834 | * as a metadata update */ | |
8273f55e | 2835 | struct dl *d; |
43dad3d6 | 2836 | int rv; |
8273f55e | 2837 | |
43dad3d6 | 2838 | /* determine if we are creating a volume or adding a disk */ |
9b1fb677 | 2839 | if (current_vol < 0) { |
43dad3d6 DW |
2840 | /* in the add disk case we are running in mdmon |
2841 | * context, so don't close fd's | |
2842 | */ | |
7801ac20 | 2843 | return _add_disk(st); |
43dad3d6 | 2844 | } else |
9b1fb677 | 2845 | rv = create_array(st, current_vol); |
8273f55e DW |
2846 | |
2847 | for (d = super->disks; d ; d = d->next) { | |
2848 | close(d->fd); | |
2849 | d->fd = -1; | |
2850 | } | |
2851 | ||
43dad3d6 | 2852 | return rv; |
8273f55e DW |
2853 | } else |
2854 | return write_super_imsm(st->sb, 1); | |
cdddbdbc | 2855 | } |
0e600426 | 2856 | #endif |
cdddbdbc DW |
2857 | |
2858 | static int store_zero_imsm(struct supertype *st, int fd) | |
2859 | { | |
551c80c1 | 2860 | unsigned long long dsize; |
6416d527 | 2861 | void *buf; |
551c80c1 DW |
2862 | |
2863 | get_dev_size(fd, NULL, &dsize); | |
2864 | ||
2865 | /* first block is stored on second to last sector of the disk */ | |
2866 | if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0) | |
2867 | return 1; | |
2868 | ||
ad97895e DW |
2869 | if (posix_memalign(&buf, 512, 512) != 0) |
2870 | return 1; | |
2871 | ||
eb7ea463 DW |
2872 | memset(buf, 0, 512); |
2873 | if (write(fd, buf, 512) != 512) | |
551c80c1 | 2874 | return 1; |
cdddbdbc DW |
2875 | return 0; |
2876 | } | |
2877 | ||
0e600426 N |
2878 | static int imsm_bbm_log_size(struct imsm_super *mpb) |
2879 | { | |
2880 | return __le32_to_cpu(mpb->bbm_log_size); | |
2881 | } | |
2882 | ||
2883 | #ifndef MDASSEMBLE | |
cdddbdbc DW |
2884 | static int validate_geometry_imsm_container(struct supertype *st, int level, |
2885 | int layout, int raiddisks, int chunk, | |
c2c087e6 | 2886 | unsigned long long size, char *dev, |
2c514b71 NB |
2887 | unsigned long long *freesize, |
2888 | int verbose) | |
cdddbdbc | 2889 | { |
c2c087e6 DW |
2890 | int fd; |
2891 | unsigned long long ldsize; | |
88c32bb1 | 2892 | const struct imsm_orom *orom; |
cdddbdbc | 2893 | |
c2c087e6 DW |
2894 | if (level != LEVEL_CONTAINER) |
2895 | return 0; | |
2896 | if (!dev) | |
2897 | return 1; | |
2898 | ||
88c32bb1 DW |
2899 | if (check_env("IMSM_NO_PLATFORM")) |
2900 | orom = NULL; | |
2901 | else | |
2902 | orom = find_imsm_orom(); | |
2903 | if (orom && raiddisks > orom->tds) { | |
2904 | if (verbose) | |
2905 | fprintf(stderr, Name ": %d exceeds maximum number of" | |
2906 | " platform supported disks: %d\n", | |
2907 | raiddisks, orom->tds); | |
2908 | return 0; | |
2909 | } | |
2910 | ||
c2c087e6 DW |
2911 | fd = open(dev, O_RDONLY|O_EXCL, 0); |
2912 | if (fd < 0) { | |
2c514b71 NB |
2913 | if (verbose) |
2914 | fprintf(stderr, Name ": imsm: Cannot open %s: %s\n", | |
2915 | dev, strerror(errno)); | |
c2c087e6 DW |
2916 | return 0; |
2917 | } | |
2918 | if (!get_dev_size(fd, dev, &ldsize)) { | |
2919 | close(fd); | |
2920 | return 0; | |
2921 | } | |
2922 | close(fd); | |
2923 | ||
2924 | *freesize = avail_size_imsm(st, ldsize >> 9); | |
2925 | ||
2926 | return 1; | |
cdddbdbc DW |
2927 | } |
2928 | ||
0dcecb2e DW |
2929 | static unsigned long long find_size(struct extent *e, int *idx, int num_extents) |
2930 | { | |
2931 | const unsigned long long base_start = e[*idx].start; | |
2932 | unsigned long long end = base_start + e[*idx].size; | |
2933 | int i; | |
2934 | ||
2935 | if (base_start == end) | |
2936 | return 0; | |
2937 | ||
2938 | *idx = *idx + 1; | |
2939 | for (i = *idx; i < num_extents; i++) { | |
2940 | /* extend overlapping extents */ | |
2941 | if (e[i].start >= base_start && | |
2942 | e[i].start <= end) { | |
2943 | if (e[i].size == 0) | |
2944 | return 0; | |
2945 | if (e[i].start + e[i].size > end) | |
2946 | end = e[i].start + e[i].size; | |
2947 | } else if (e[i].start > end) { | |
2948 | *idx = i; | |
2949 | break; | |
2950 | } | |
2951 | } | |
2952 | ||
2953 | return end - base_start; | |
2954 | } | |
2955 | ||
2956 | static unsigned long long merge_extents(struct intel_super *super, int sum_extents) | |
2957 | { | |
2958 | /* build a composite disk with all known extents and generate a new | |
2959 | * 'maxsize' given the "all disks in an array must share a common start | |
2960 | * offset" constraint | |
2961 | */ | |
2962 | struct extent *e = calloc(sum_extents, sizeof(*e)); | |
2963 | struct dl *dl; | |
2964 | int i, j; | |
2965 | int start_extent; | |
2966 | unsigned long long pos; | |
b9d77223 | 2967 | unsigned long long start = 0; |
0dcecb2e DW |
2968 | unsigned long long maxsize; |
2969 | unsigned long reserve; | |
2970 | ||
2971 | if (!e) | |
2972 | return ~0ULL; /* error */ | |
2973 | ||
2974 | /* coalesce and sort all extents. also, check to see if we need to | |
2975 | * reserve space between member arrays | |
2976 | */ | |
2977 | j = 0; | |
2978 | for (dl = super->disks; dl; dl = dl->next) { | |
2979 | if (!dl->e) | |
2980 | continue; | |
2981 | for (i = 0; i < dl->extent_cnt; i++) | |
2982 | e[j++] = dl->e[i]; | |
2983 | } | |
2984 | qsort(e, sum_extents, sizeof(*e), cmp_extent); | |
2985 | ||
2986 | /* merge extents */ | |
2987 | i = 0; | |
2988 | j = 0; | |
2989 | while (i < sum_extents) { | |
2990 | e[j].start = e[i].start; | |
2991 | e[j].size = find_size(e, &i, sum_extents); | |
2992 | j++; | |
2993 | if (e[j-1].size == 0) | |
2994 | break; | |
2995 | } | |
2996 | ||
2997 | pos = 0; | |
2998 | maxsize = 0; | |
2999 | start_extent = 0; | |
3000 | i = 0; | |
3001 | do { | |
3002 | unsigned long long esize; | |
3003 | ||
3004 | esize = e[i].start - pos; | |
3005 | if (esize >= maxsize) { | |
3006 | maxsize = esize; | |
3007 | start = pos; | |
3008 | start_extent = i; | |
3009 | } | |
3010 | pos = e[i].start + e[i].size; | |
3011 | i++; | |
3012 | } while (e[i-1].size); | |
3013 | free(e); | |
3014 | ||
3015 | if (start_extent > 0) | |
3016 | reserve = IMSM_RESERVED_SECTORS; /* gap between raid regions */ | |
3017 | else | |
3018 | reserve = 0; | |
3019 | ||
3020 | if (maxsize < reserve) | |
3021 | return ~0ULL; | |
3022 | ||
3023 | super->create_offset = ~((__u32) 0); | |
3024 | if (start + reserve > super->create_offset) | |
3025 | return ~0ULL; /* start overflows create_offset */ | |
3026 | super->create_offset = start + reserve; | |
3027 | ||
3028 | return maxsize - reserve; | |
3029 | } | |
3030 | ||
88c32bb1 DW |
3031 | static int is_raid_level_supported(const struct imsm_orom *orom, int level, int raiddisks) |
3032 | { | |
3033 | if (level < 0 || level == 6 || level == 4) | |
3034 | return 0; | |
3035 | ||
3036 | /* if we have an orom prevent invalid raid levels */ | |
3037 | if (orom) | |
3038 | switch (level) { | |
3039 | case 0: return imsm_orom_has_raid0(orom); | |
3040 | case 1: | |
3041 | if (raiddisks > 2) | |
3042 | return imsm_orom_has_raid1e(orom); | |
1c556e92 DW |
3043 | return imsm_orom_has_raid1(orom) && raiddisks == 2; |
3044 | case 10: return imsm_orom_has_raid10(orom) && raiddisks == 4; | |
3045 | case 5: return imsm_orom_has_raid5(orom) && raiddisks > 2; | |
88c32bb1 DW |
3046 | } |
3047 | else | |
3048 | return 1; /* not on an Intel RAID platform so anything goes */ | |
3049 | ||
3050 | return 0; | |
3051 | } | |
3052 | ||
35f81cbb | 3053 | #define pr_vrb(fmt, arg...) (void) (verbose && fprintf(stderr, Name fmt, ##arg)) |
c2c087e6 DW |
3054 | /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd |
3055 | * FIX ME add ahci details | |
3056 | */ | |
8b353278 DW |
3057 | static int validate_geometry_imsm_volume(struct supertype *st, int level, |
3058 | int layout, int raiddisks, int chunk, | |
c2c087e6 | 3059 | unsigned long long size, char *dev, |
2c514b71 NB |
3060 | unsigned long long *freesize, |
3061 | int verbose) | |
cdddbdbc | 3062 | { |
c2c087e6 DW |
3063 | struct stat stb; |
3064 | struct intel_super *super = st->sb; | |
a20d2ba5 | 3065 | struct imsm_super *mpb = super->anchor; |
c2c087e6 DW |
3066 | struct dl *dl; |
3067 | unsigned long long pos = 0; | |
3068 | unsigned long long maxsize; | |
3069 | struct extent *e; | |
3070 | int i; | |
cdddbdbc | 3071 | |
88c32bb1 DW |
3072 | /* We must have the container info already read in. */ |
3073 | if (!super) | |
c2c087e6 DW |
3074 | return 0; |
3075 | ||
88c32bb1 | 3076 | if (!is_raid_level_supported(super->orom, level, raiddisks)) { |
1c556e92 DW |
3077 | pr_vrb(": platform does not support raid%d with %d disk%s\n", |
3078 | level, raiddisks, raiddisks > 1 ? "s" : ""); | |
c2c087e6 DW |
3079 | return 0; |
3080 | } | |
78757ce8 DW |
3081 | if (super->orom && level != 1 && |
3082 | !imsm_orom_has_chunk(super->orom, chunk)) { | |
35f81cbb | 3083 | pr_vrb(": platform does not support a chunk size of: %d\n", chunk); |
c2c087e6 | 3084 | return 0; |
88c32bb1 DW |
3085 | } |
3086 | if (layout != imsm_level_to_layout(level)) { | |
3087 | if (level == 5) | |
35f81cbb | 3088 | pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n"); |
88c32bb1 | 3089 | else if (level == 10) |
35f81cbb | 3090 | pr_vrb(": imsm raid 10 only supports the n2 layout\n"); |
88c32bb1 | 3091 | else |
35f81cbb | 3092 | pr_vrb(": imsm unknown layout %#x for this raid level %d\n", |
88c32bb1 | 3093 | layout, level); |
c2c087e6 | 3094 | return 0; |
88c32bb1 | 3095 | } |
c2c087e6 DW |
3096 | |
3097 | if (!dev) { | |
3098 | /* General test: make sure there is space for | |
2da8544a DW |
3099 | * 'raiddisks' device extents of size 'size' at a given |
3100 | * offset | |
c2c087e6 | 3101 | */ |
e46273eb | 3102 | unsigned long long minsize = size; |
2da8544a | 3103 | unsigned long long start_offset = ~0ULL; |
c2c087e6 DW |
3104 | int dcnt = 0; |
3105 | if (minsize == 0) | |
3106 | minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS; | |
3107 | for (dl = super->disks; dl ; dl = dl->next) { | |
3108 | int found = 0; | |
3109 | ||
bf5a934a | 3110 | pos = 0; |
c2c087e6 DW |
3111 | i = 0; |
3112 | e = get_extents(super, dl); | |
3113 | if (!e) continue; | |
3114 | do { | |
3115 | unsigned long long esize; | |
3116 | esize = e[i].start - pos; | |
3117 | if (esize >= minsize) | |
3118 | found = 1; | |
2da8544a DW |
3119 | if (found && start_offset == ~0ULL) { |
3120 | start_offset = pos; | |
3121 | break; | |
3122 | } else if (found && pos != start_offset) { | |
3123 | found = 0; | |
3124 | break; | |
3125 | } | |
c2c087e6 DW |
3126 | pos = e[i].start + e[i].size; |
3127 | i++; | |
3128 | } while (e[i-1].size); | |
3129 | if (found) | |
3130 | dcnt++; | |
3131 | free(e); | |
3132 | } | |
3133 | if (dcnt < raiddisks) { | |
2c514b71 NB |
3134 | if (verbose) |
3135 | fprintf(stderr, Name ": imsm: Not enough " | |
3136 | "devices with space for this array " | |
3137 | "(%d < %d)\n", | |
3138 | dcnt, raiddisks); | |
c2c087e6 DW |
3139 | return 0; |
3140 | } | |
3141 | return 1; | |
3142 | } | |
0dcecb2e | 3143 | |
c2c087e6 DW |
3144 | /* This device must be a member of the set */ |
3145 | if (stat(dev, &stb) < 0) | |
3146 | return 0; | |
3147 | if ((S_IFMT & stb.st_mode) != S_IFBLK) | |
3148 | return 0; | |
3149 | for (dl = super->disks ; dl ; dl = dl->next) { | |
3150 | if (dl->major == major(stb.st_rdev) && | |
3151 | dl->minor == minor(stb.st_rdev)) | |
3152 | break; | |
3153 | } | |
3154 | if (!dl) { | |
2c514b71 NB |
3155 | if (verbose) |
3156 | fprintf(stderr, Name ": %s is not in the " | |
3157 | "same imsm set\n", dev); | |
c2c087e6 | 3158 | return 0; |
a20d2ba5 DW |
3159 | } else if (super->orom && dl->index < 0 && mpb->num_raid_devs) { |
3160 | /* If a volume is present then the current creation attempt | |
3161 | * cannot incorporate new spares because the orom may not | |
3162 | * understand this configuration (all member disks must be | |
3163 | * members of each array in the container). | |
3164 | */ | |
3165 | fprintf(stderr, Name ": %s is a spare and a volume" | |
3166 | " is already defined for this container\n", dev); | |
3167 | fprintf(stderr, Name ": The option-rom requires all member" | |
3168 | " disks to be a member of all volumes\n"); | |
3169 | return 0; | |
c2c087e6 | 3170 | } |
0dcecb2e DW |
3171 | |
3172 | /* retrieve the largest free space block */ | |
c2c087e6 DW |
3173 | e = get_extents(super, dl); |
3174 | maxsize = 0; | |
3175 | i = 0; | |
0dcecb2e DW |
3176 | if (e) { |
3177 | do { | |
3178 | unsigned long long esize; | |
3179 | ||
3180 | esize = e[i].start - pos; | |
3181 | if (esize >= maxsize) | |
3182 | maxsize = esize; | |
3183 | pos = e[i].start + e[i].size; | |
3184 | i++; | |
3185 | } while (e[i-1].size); | |
3186 | dl->e = e; | |
3187 | dl->extent_cnt = i; | |
3188 | } else { | |
3189 | if (verbose) | |
3190 | fprintf(stderr, Name ": unable to determine free space for: %s\n", | |
3191 | dev); | |
3192 | return 0; | |
3193 | } | |
3194 | if (maxsize < size) { | |
3195 | if (verbose) | |
3196 | fprintf(stderr, Name ": %s not enough space (%llu < %llu)\n", | |
3197 | dev, maxsize, size); | |
3198 | return 0; | |
3199 | } | |
3200 | ||
3201 | /* count total number of extents for merge */ | |
3202 | i = 0; | |
3203 | for (dl = super->disks; dl; dl = dl->next) | |
3204 | if (dl->e) | |
3205 | i += dl->extent_cnt; | |
3206 | ||
3207 | maxsize = merge_extents(super, i); | |
3208 | if (maxsize < size) { | |
3209 | if (verbose) | |
3210 | fprintf(stderr, Name ": not enough space after merge (%llu < %llu)\n", | |
3211 | maxsize, size); | |
3212 | return 0; | |
3213 | } else if (maxsize == ~0ULL) { | |
3214 | if (verbose) | |
3215 | fprintf(stderr, Name ": failed to merge %d extents\n", i); | |
3216 | return 0; | |
3217 | } | |
3218 | ||
c2c087e6 DW |
3219 | *freesize = maxsize; |
3220 | ||
3221 | return 1; | |
cdddbdbc DW |
3222 | } |
3223 | ||
efb30e7f DW |
3224 | static int reserve_space(struct supertype *st, int raiddisks, |
3225 | unsigned long long size, int chunk, | |
3226 | unsigned long long *freesize) | |
3227 | { | |
3228 | struct intel_super *super = st->sb; | |
3229 | struct imsm_super *mpb = super->anchor; | |
3230 | struct dl *dl; | |
3231 | int i; | |
3232 | int extent_cnt; | |
3233 | struct extent *e; | |
3234 | unsigned long long maxsize; | |
3235 | unsigned long long minsize; | |
3236 | int cnt; | |
3237 | int used; | |
3238 | ||
3239 | /* find the largest common start free region of the possible disks */ | |
3240 | used = 0; | |
3241 | extent_cnt = 0; | |
3242 | cnt = 0; | |
3243 | for (dl = super->disks; dl; dl = dl->next) { | |
3244 | dl->raiddisk = -1; | |
3245 | ||
3246 | if (dl->index >= 0) | |
3247 | used++; | |
3248 | ||
3249 | /* don't activate new spares if we are orom constrained | |
3250 | * and there is already a volume active in the container | |
3251 | */ | |
3252 | if (super->orom && dl->index < 0 && mpb->num_raid_devs) | |
3253 | continue; | |
3254 | ||
3255 | e = get_extents(super, dl); | |
3256 | if (!e) | |
3257 | continue; | |
3258 | for (i = 1; e[i-1].size; i++) | |
3259 | ; | |
3260 | dl->e = e; | |
3261 | dl->extent_cnt = i; | |
3262 | extent_cnt += i; | |
3263 | cnt++; | |
3264 | } | |
3265 | ||
3266 | maxsize = merge_extents(super, extent_cnt); | |
3267 | minsize = size; | |
3268 | if (size == 0) | |
3269 | minsize = chunk; | |
3270 | ||
3271 | if (cnt < raiddisks || | |
3272 | (super->orom && used && used != raiddisks) || | |
3273 | maxsize < minsize) { | |
3274 | fprintf(stderr, Name ": not enough devices with space to create array.\n"); | |
3275 | return 0; /* No enough free spaces large enough */ | |
3276 | } | |
3277 | ||
3278 | if (size == 0) { | |
3279 | size = maxsize; | |
3280 | if (chunk) { | |
3281 | size /= chunk; | |
3282 | size *= chunk; | |
3283 | } | |
3284 | } | |
3285 | ||
3286 | cnt = 0; | |
3287 | for (dl = super->disks; dl; dl = dl->next) | |
3288 | if (dl->e) | |
3289 | dl->raiddisk = cnt++; | |
3290 | ||
3291 | *freesize = size; | |
3292 | ||
3293 | return 1; | |
3294 | } | |
3295 | ||
bf5a934a DW |
3296 | static int validate_geometry_imsm(struct supertype *st, int level, int layout, |
3297 | int raiddisks, int chunk, unsigned long long size, | |
3298 | char *dev, unsigned long long *freesize, | |
3299 | int verbose) | |
3300 | { | |
3301 | int fd, cfd; | |
3302 | struct mdinfo *sra; | |
3303 | ||
3304 | /* if given unused devices create a container | |
3305 | * if given given devices in a container create a member volume | |
3306 | */ | |
3307 | if (level == LEVEL_CONTAINER) { | |
3308 | /* Must be a fresh device to add to a container */ | |
3309 | return validate_geometry_imsm_container(st, level, layout, | |
3310 | raiddisks, chunk, size, | |
3311 | dev, freesize, | |
3312 | verbose); | |
3313 | } | |
3314 | ||
8592f29d N |
3315 | if (!dev) { |
3316 | if (st->sb && freesize) { | |
efb30e7f DW |
3317 | /* we are being asked to automatically layout a |
3318 | * new volume based on the current contents of | |
3319 | * the container. If the the parameters can be | |
3320 | * satisfied reserve_space will record the disks, | |
3321 | * start offset, and size of the volume to be | |
3322 | * created. add_to_super and getinfo_super | |
3323 | * detect when autolayout is in progress. | |
3324 | */ | |
3325 | return reserve_space(st, raiddisks, size, chunk, freesize); | |
8592f29d N |
3326 | } |
3327 | return 1; | |
3328 | } | |
bf5a934a DW |
3329 | if (st->sb) { |
3330 | /* creating in a given container */ | |
3331 | return validate_geometry_imsm_volume(st, level, layout, | |
3332 | raiddisks, chunk, size, | |
3333 | dev, freesize, verbose); | |
3334 | } | |
3335 | ||
3336 | /* limit creation to the following levels */ | |
3337 | if (!dev) | |
3338 | switch (level) { | |
3339 | case 0: | |
3340 | case 1: | |
3341 | case 10: | |
3342 | case 5: | |
3343 | break; | |
3344 | default: | |
3345 | return 1; | |
3346 | } | |
3347 | ||
3348 | /* This device needs to be a device in an 'imsm' container */ | |
3349 | fd = open(dev, O_RDONLY|O_EXCL, 0); | |
3350 | if (fd >= 0) { | |
3351 | if (verbose) | |
3352 | fprintf(stderr, | |
3353 | Name ": Cannot create this array on device %s\n", | |
3354 | dev); | |
3355 | close(fd); | |
3356 | return 0; | |
3357 | } | |
3358 | if (errno != EBUSY || (fd = open(dev, O_RDONLY, 0)) < 0) { | |
3359 | if (verbose) | |
3360 | fprintf(stderr, Name ": Cannot open %s: %s\n", | |
3361 | dev, strerror(errno)); | |
3362 | return 0; | |
3363 | } | |
3364 | /* Well, it is in use by someone, maybe an 'imsm' container. */ | |
3365 | cfd = open_container(fd); | |
3366 | if (cfd < 0) { | |
3367 | close(fd); | |
3368 | if (verbose) | |
3369 | fprintf(stderr, Name ": Cannot use %s: It is busy\n", | |
3370 | dev); | |
3371 | return 0; | |
3372 | } | |
3373 | sra = sysfs_read(cfd, 0, GET_VERSION); | |
3374 | close(fd); | |
3375 | if (sra && sra->array.major_version == -1 && | |
3376 | strcmp(sra->text_version, "imsm") == 0) { | |
3377 | /* This is a member of a imsm container. Load the container | |
3378 | * and try to create a volume | |
3379 | */ | |
3380 | struct intel_super *super; | |
3381 | ||
3382 | if (load_super_imsm_all(st, cfd, (void **) &super, NULL, 1) == 0) { | |
3383 | st->sb = super; | |
3384 | st->container_dev = fd2devnum(cfd); | |
3385 | close(cfd); | |
3386 | return validate_geometry_imsm_volume(st, level, layout, | |
3387 | raiddisks, chunk, | |
3388 | size, dev, | |
3389 | freesize, verbose); | |
3390 | } | |
3391 | close(cfd); | |
3392 | } else /* may belong to another container */ | |
3393 | return 0; | |
3394 | ||
3395 | return 1; | |
3396 | } | |
0e600426 | 3397 | #endif /* MDASSEMBLE */ |
bf5a934a | 3398 | |
cdddbdbc DW |
3399 | static struct mdinfo *container_content_imsm(struct supertype *st) |
3400 | { | |
4f5bc454 DW |
3401 | /* Given a container loaded by load_super_imsm_all, |
3402 | * extract information about all the arrays into | |
3403 | * an mdinfo tree. | |
3404 | * | |
3405 | * For each imsm_dev create an mdinfo, fill it in, | |
3406 | * then look for matching devices in super->disks | |
3407 | * and create appropriate device mdinfo. | |
3408 | */ | |
3409 | struct intel_super *super = st->sb; | |
949c47a0 | 3410 | struct imsm_super *mpb = super->anchor; |
4f5bc454 DW |
3411 | struct mdinfo *rest = NULL; |
3412 | int i; | |
cdddbdbc | 3413 | |
604b746f JD |
3414 | /* do not assemble arrays that might have bad blocks */ |
3415 | if (imsm_bbm_log_size(super->anchor)) { | |
3416 | fprintf(stderr, Name ": BBM log found in metadata. " | |
3417 | "Cannot activate array(s).\n"); | |
3418 | return NULL; | |
3419 | } | |
3420 | ||
4f5bc454 | 3421 | for (i = 0; i < mpb->num_raid_devs; i++) { |
949c47a0 | 3422 | struct imsm_dev *dev = get_imsm_dev(super, i); |
a965f303 | 3423 | struct imsm_map *map = get_imsm_map(dev, 0); |
4f5bc454 | 3424 | struct mdinfo *this; |
4f5bc454 DW |
3425 | int slot; |
3426 | ||
1ce0101c DW |
3427 | /* do not publish arrays that are in the middle of an |
3428 | * unsupported migration | |
3429 | */ | |
3430 | if (dev->vol.migr_state && | |
3431 | (migr_type(dev) == MIGR_GEN_MIGR || | |
3432 | migr_type(dev) == MIGR_STATE_CHANGE)) { | |
3433 | fprintf(stderr, Name ": cannot assemble volume '%.16s':" | |
3434 | " unsupported migration in progress\n", | |
3435 | dev->volume); | |
3436 | continue; | |
3437 | } | |
3438 | ||
4f5bc454 DW |
3439 | this = malloc(sizeof(*this)); |
3440 | memset(this, 0, sizeof(*this)); | |
3441 | this->next = rest; | |
4f5bc454 | 3442 | |
301406c9 DW |
3443 | super->current_vol = i; |
3444 | getinfo_super_imsm_volume(st, this); | |
4f5bc454 | 3445 | for (slot = 0 ; slot < map->num_members; slot++) { |
4f5bc454 DW |
3446 | struct mdinfo *info_d; |
3447 | struct dl *d; | |
3448 | int idx; | |
9a1608e5 | 3449 | int skip; |
4f5bc454 | 3450 | __u32 s; |
7eef0453 | 3451 | __u32 ord; |
4f5bc454 | 3452 | |
9a1608e5 | 3453 | skip = 0; |
ff077194 | 3454 | idx = get_imsm_disk_idx(dev, slot); |
7eef0453 | 3455 | ord = get_imsm_ord_tbl_ent(dev, slot); |
4f5bc454 DW |
3456 | for (d = super->disks; d ; d = d->next) |
3457 | if (d->index == idx) | |
3458 | break; | |
3459 | ||
3460 | if (d == NULL) | |
9a1608e5 DW |
3461 | skip = 1; |
3462 | ||
f2f27e63 | 3463 | s = d ? d->disk.status : 0; |
9a1608e5 DW |
3464 | if (s & FAILED_DISK) |
3465 | skip = 1; | |
3466 | if (!(s & USABLE_DISK)) | |
3467 | skip = 1; | |
7eef0453 DW |
3468 | if (ord & IMSM_ORD_REBUILD) |
3469 | skip = 1; | |
9a1608e5 DW |
3470 | |
3471 | /* | |
3472 | * if we skip some disks the array will be assmebled degraded; | |
3473 | * reset resync start to avoid a dirty-degraded situation | |
3474 | * | |
3475 | * FIXME handle dirty degraded | |
3476 | */ | |
3477 | if (skip && !dev->vol.dirty) | |
3478 | this->resync_start = ~0ULL; | |
3479 | if (skip) | |
3480 | continue; | |
4f5bc454 DW |
3481 | |
3482 | info_d = malloc(sizeof(*info_d)); | |
9a1608e5 DW |
3483 | if (!info_d) { |
3484 | fprintf(stderr, Name ": failed to allocate disk" | |
1ce0101c | 3485 | " for volume %.16s\n", dev->volume); |
9a1608e5 DW |
3486 | free(this); |
3487 | this = rest; | |
3488 | break; | |
3489 | } | |
4f5bc454 DW |
3490 | memset(info_d, 0, sizeof(*info_d)); |
3491 | info_d->next = this->devs; | |
3492 | this->devs = info_d; | |
3493 | ||
4f5bc454 DW |
3494 | info_d->disk.number = d->index; |
3495 | info_d->disk.major = d->major; | |
3496 | info_d->disk.minor = d->minor; | |
3497 | info_d->disk.raid_disk = slot; | |
4f5bc454 DW |
3498 | |
3499 | this->array.working_disks++; | |
3500 | ||
3501 | info_d->events = __le32_to_cpu(mpb->generation_num); | |
3502 | info_d->data_offset = __le32_to_cpu(map->pba_of_lba0); | |
3503 | info_d->component_size = __le32_to_cpu(map->blocks_per_member); | |
3504 | if (d->devname) | |
3505 | strcpy(info_d->name, d->devname); | |
3506 | } | |
9a1608e5 | 3507 | rest = this; |
4f5bc454 DW |
3508 | } |
3509 | ||
3510 | return rest; | |
cdddbdbc DW |
3511 | } |
3512 | ||
845dea95 | 3513 | |
0e600426 | 3514 | #ifndef MDASSEMBLE |
cba0191b NB |
3515 | static int imsm_open_new(struct supertype *c, struct active_array *a, |
3516 | char *inst) | |
845dea95 | 3517 | { |
0372d5a2 | 3518 | struct intel_super *super = c->sb; |
949c47a0 | 3519 | struct imsm_super *mpb = super->anchor; |
0372d5a2 | 3520 | |
949c47a0 | 3521 | if (atoi(inst) >= mpb->num_raid_devs) { |
0372d5a2 DW |
3522 | fprintf(stderr, "%s: subarry index %d, out of range\n", |
3523 | __func__, atoi(inst)); | |
3524 | return -ENODEV; | |
3525 | } | |
3526 | ||
4e6e574a | 3527 | dprintf("imsm: open_new %s\n", inst); |
cba0191b | 3528 | a->info.container_member = atoi(inst); |
845dea95 NB |
3529 | return 0; |
3530 | } | |
3531 | ||
fb49eef2 | 3532 | static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev, int failed) |
c2a1e7da | 3533 | { |
a965f303 | 3534 | struct imsm_map *map = get_imsm_map(dev, 0); |
c2a1e7da DW |
3535 | |
3536 | if (!failed) | |
3393c6af DW |
3537 | return map->map_state == IMSM_T_STATE_UNINITIALIZED ? |
3538 | IMSM_T_STATE_UNINITIALIZED : IMSM_T_STATE_NORMAL; | |
c2a1e7da DW |
3539 | |
3540 | switch (get_imsm_raid_level(map)) { | |
3541 | case 0: | |
3542 | return IMSM_T_STATE_FAILED; | |
3543 | break; | |
3544 | case 1: | |
3545 | if (failed < map->num_members) | |
3546 | return IMSM_T_STATE_DEGRADED; | |
3547 | else | |
3548 | return IMSM_T_STATE_FAILED; | |
3549 | break; | |
3550 | case 10: | |
3551 | { | |
3552 | /** | |
c92a2527 DW |
3553 | * check to see if any mirrors have failed, otherwise we |
3554 | * are degraded. Even numbered slots are mirrored on | |
3555 | * slot+1 | |
c2a1e7da | 3556 | */ |
c2a1e7da | 3557 | int i; |
d9b420a5 N |
3558 | /* gcc -Os complains that this is unused */ |
3559 | int insync = insync; | |
c2a1e7da DW |
3560 | |
3561 | for (i = 0; i < map->num_members; i++) { | |
c92a2527 DW |
3562 | __u32 ord = get_imsm_ord_tbl_ent(dev, i); |
3563 | int idx = ord_to_idx(ord); | |
3564 | struct imsm_disk *disk; | |
c2a1e7da | 3565 | |
c92a2527 DW |
3566 | /* reset the potential in-sync count on even-numbered |
3567 | * slots. num_copies is always 2 for imsm raid10 | |
3568 | */ | |
3569 | if ((i & 1) == 0) | |
3570 | insync = 2; | |
c2a1e7da | 3571 | |
c92a2527 | 3572 | disk = get_imsm_disk(super, idx); |
f2f27e63 | 3573 | if (!disk || disk->status & FAILED_DISK || |
c92a2527 DW |
3574 | ord & IMSM_ORD_REBUILD) |
3575 | insync--; | |
c2a1e7da | 3576 | |
c92a2527 DW |
3577 | /* no in-sync disks left in this mirror the |
3578 | * array has failed | |
3579 | */ | |
3580 | if (insync == 0) | |
3581 | return IMSM_T_STATE_FAILED; | |
c2a1e7da DW |
3582 | } |
3583 | ||
3584 | return IMSM_T_STATE_DEGRADED; | |
3585 | } | |
3586 | case 5: | |
3587 | if (failed < 2) | |
3588 | return IMSM_T_STATE_DEGRADED; | |
3589 | else | |
3590 | return IMSM_T_STATE_FAILED; | |
3591 | break; | |
3592 | default: | |
3593 | break; | |
3594 | } | |
3595 | ||
3596 | return map->map_state; | |
3597 | } | |
3598 | ||
ff077194 | 3599 | static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev) |
c2a1e7da DW |
3600 | { |
3601 | int i; | |
3602 | int failed = 0; | |
3603 | struct imsm_disk *disk; | |
ff077194 | 3604 | struct imsm_map *map = get_imsm_map(dev, 0); |
0556e1a2 DW |
3605 | struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state); |
3606 | __u32 ord; | |
3607 | int idx; | |
c2a1e7da | 3608 | |
0556e1a2 DW |
3609 | /* at the beginning of migration we set IMSM_ORD_REBUILD on |
3610 | * disks that are being rebuilt. New failures are recorded to | |
3611 | * map[0]. So we look through all the disks we started with and | |
3612 | * see if any failures are still present, or if any new ones | |
3613 | * have arrived | |
3614 | * | |
3615 | * FIXME add support for online capacity expansion and | |
3616 | * raid-level-migration | |
3617 | */ | |
3618 | for (i = 0; i < prev->num_members; i++) { | |
3619 | ord = __le32_to_cpu(prev->disk_ord_tbl[i]); | |
3620 | ord |= __le32_to_cpu(map->disk_ord_tbl[i]); | |
3621 | idx = ord_to_idx(ord); | |
c2a1e7da | 3622 | |
949c47a0 | 3623 | disk = get_imsm_disk(super, idx); |
f2f27e63 | 3624 | if (!disk || disk->status & FAILED_DISK || |
b10b37b8 | 3625 | ord & IMSM_ORD_REBUILD) |
fcb84475 | 3626 | failed++; |
c2a1e7da DW |
3627 | } |
3628 | ||
3629 | return failed; | |
845dea95 NB |
3630 | } |
3631 | ||
0c046afd DW |
3632 | static int is_resyncing(struct imsm_dev *dev) |
3633 | { | |
3634 | struct imsm_map *migr_map; | |
3635 | ||
3636 | if (!dev->vol.migr_state) | |
3637 | return 0; | |
3638 | ||
1484e727 DW |
3639 | if (migr_type(dev) == MIGR_INIT || |
3640 | migr_type(dev) == MIGR_REPAIR) | |
0c046afd DW |
3641 | return 1; |
3642 | ||
3643 | migr_map = get_imsm_map(dev, 1); | |
3644 | ||
3645 | if (migr_map->map_state == IMSM_T_STATE_NORMAL) | |
3646 | return 1; | |
3647 | else | |
3648 | return 0; | |
3649 | } | |
3650 | ||
3651 | static int is_rebuilding(struct imsm_dev *dev) | |
3652 | { | |
3653 | struct imsm_map *migr_map; | |
3654 | ||
3655 | if (!dev->vol.migr_state) | |
3656 | return 0; | |
3657 | ||
1484e727 | 3658 | if (migr_type(dev) != MIGR_REBUILD) |
0c046afd DW |
3659 | return 0; |
3660 | ||
3661 | migr_map = get_imsm_map(dev, 1); | |
3662 | ||
3663 | if (migr_map->map_state == IMSM_T_STATE_DEGRADED) | |
3664 | return 1; | |
3665 | else | |
3666 | return 0; | |
3667 | } | |
3668 | ||
0556e1a2 DW |
3669 | /* return true if we recorded new information */ |
3670 | static int mark_failure(struct imsm_dev *dev, struct imsm_disk *disk, int idx) | |
47ee5a45 | 3671 | { |
0556e1a2 DW |
3672 | __u32 ord; |
3673 | int slot; | |
3674 | struct imsm_map *map; | |
3675 | ||
3676 | /* new failures are always set in map[0] */ | |
3677 | map = get_imsm_map(dev, 0); | |
3678 | ||
3679 | slot = get_imsm_disk_slot(map, idx); | |
3680 | if (slot < 0) | |
3681 | return 0; | |
3682 | ||
3683 | ord = __le32_to_cpu(map->disk_ord_tbl[slot]); | |
3684 | if ((disk->status & FAILED_DISK) && (ord & IMSM_ORD_REBUILD)) | |
3685 | return 0; | |
3686 | ||
f2f27e63 | 3687 | disk->status |= FAILED_DISK; |
0556e1a2 | 3688 | set_imsm_ord_tbl_ent(map, slot, idx | IMSM_ORD_REBUILD); |
4291d691 | 3689 | if (~map->failed_disk_num == 0) |
0556e1a2 DW |
3690 | map->failed_disk_num = slot; |
3691 | return 1; | |
3692 | } | |
3693 | ||
3694 | static void mark_missing(struct imsm_dev *dev, struct imsm_disk *disk, int idx) | |
3695 | { | |
3696 | mark_failure(dev, disk, idx); | |
3697 | ||
3698 | if (disk->scsi_id == __cpu_to_le32(~(__u32)0)) | |
3699 | return; | |
3700 | ||
47ee5a45 DW |
3701 | disk->scsi_id = __cpu_to_le32(~(__u32)0); |
3702 | memmove(&disk->serial[0], &disk->serial[1], MAX_RAID_SERIAL_LEN - 1); | |
3703 | } | |
3704 | ||
0c046afd DW |
3705 | /* Handle dirty -> clean transititions and resync. Degraded and rebuild |
3706 | * states are handled in imsm_set_disk() with one exception, when a | |
3707 | * resync is stopped due to a new failure this routine will set the | |
3708 | * 'degraded' state for the array. | |
3709 | */ | |
01f157d7 | 3710 | static int imsm_set_array_state(struct active_array *a, int consistent) |
a862209d DW |
3711 | { |
3712 | int inst = a->info.container_member; | |
3713 | struct intel_super *super = a->container->sb; | |
949c47a0 | 3714 | struct imsm_dev *dev = get_imsm_dev(super, inst); |
a965f303 | 3715 | struct imsm_map *map = get_imsm_map(dev, 0); |
0c046afd DW |
3716 | int failed = imsm_count_failed(super, dev); |
3717 | __u8 map_state = imsm_check_degraded(super, dev, failed); | |
a862209d | 3718 | |
47ee5a45 DW |
3719 | /* before we activate this array handle any missing disks */ |
3720 | if (consistent == 2 && super->missing) { | |
3721 | struct dl *dl; | |
3722 | ||
3723 | dprintf("imsm: mark missing\n"); | |
3724 | end_migration(dev, map_state); | |
3725 | for (dl = super->missing; dl; dl = dl->next) | |
0556e1a2 | 3726 | mark_missing(dev, &dl->disk, dl->index); |
47ee5a45 DW |
3727 | super->updates_pending++; |
3728 | } | |
3729 | ||
0c046afd | 3730 | if (consistent == 2 && |
593add1b | 3731 | (!is_resync_complete(a) || |
0c046afd DW |
3732 | map_state != IMSM_T_STATE_NORMAL || |
3733 | dev->vol.migr_state)) | |
01f157d7 | 3734 | consistent = 0; |
272906ef | 3735 | |
593add1b | 3736 | if (is_resync_complete(a)) { |
0c046afd | 3737 | /* complete intialization / resync, |
0556e1a2 DW |
3738 | * recovery and interrupted recovery is completed in |
3739 | * ->set_disk | |
0c046afd DW |
3740 | */ |
3741 | if (is_resyncing(dev)) { | |
3742 | dprintf("imsm: mark resync done\n"); | |
f8f603f1 | 3743 | end_migration(dev, map_state); |
115c3803 | 3744 | super->updates_pending++; |
115c3803 | 3745 | } |
0c046afd DW |
3746 | } else if (!is_resyncing(dev) && !failed) { |
3747 | /* mark the start of the init process if nothing is failed */ | |
3748 | dprintf("imsm: mark resync start (%llu)\n", a->resync_start); | |
1484e727 | 3749 | if (map->map_state == IMSM_T_STATE_UNINITIALIZED) |
e3bba0e0 | 3750 | migrate(dev, IMSM_T_STATE_NORMAL, MIGR_INIT); |
1484e727 DW |
3751 | else |
3752 | migrate(dev, IMSM_T_STATE_NORMAL, MIGR_REPAIR); | |
3393c6af | 3753 | super->updates_pending++; |
115c3803 | 3754 | } |
a862209d | 3755 | |
da188789 | 3756 | /* FIXME check if we can update curr_migr_unit from resync_start */ |
f8f603f1 | 3757 | |
3393c6af | 3758 | /* mark dirty / clean */ |
0c046afd | 3759 | if (dev->vol.dirty != !consistent) { |
3393c6af | 3760 | dprintf("imsm: mark '%s' (%llu)\n", |
0c046afd DW |
3761 | consistent ? "clean" : "dirty", a->resync_start); |
3762 | if (consistent) | |
3763 | dev->vol.dirty = 0; | |
3764 | else | |
3765 | dev->vol.dirty = 1; | |
a862209d DW |
3766 | super->updates_pending++; |
3767 | } | |
01f157d7 | 3768 | return consistent; |
a862209d DW |
3769 | } |
3770 | ||
8d45d196 | 3771 | static void imsm_set_disk(struct active_array *a, int n, int state) |
845dea95 | 3772 | { |
8d45d196 DW |
3773 | int inst = a->info.container_member; |
3774 | struct intel_super *super = a->container->sb; | |
949c47a0 | 3775 | struct imsm_dev *dev = get_imsm_dev(super, inst); |
a965f303 | 3776 | struct imsm_map *map = get_imsm_map(dev, 0); |
8d45d196 | 3777 | struct imsm_disk *disk; |
0c046afd | 3778 | int failed; |
b10b37b8 | 3779 | __u32 ord; |
0c046afd | 3780 | __u8 map_state; |
8d45d196 DW |
3781 | |
3782 | if (n > map->num_members) | |
3783 | fprintf(stderr, "imsm: set_disk %d out of range 0..%d\n", | |
3784 | n, map->num_members - 1); | |
3785 | ||
3786 | if (n < 0) | |
3787 | return; | |
3788 | ||
4e6e574a | 3789 | dprintf("imsm: set_disk %d:%x\n", n, state); |
8d45d196 | 3790 | |
b10b37b8 DW |
3791 | ord = get_imsm_ord_tbl_ent(dev, n); |
3792 | disk = get_imsm_disk(super, ord_to_idx(ord)); | |
8d45d196 | 3793 | |
5802a811 | 3794 | /* check for new failures */ |
0556e1a2 DW |
3795 | if (state & DS_FAULTY) { |
3796 | if (mark_failure(dev, disk, ord_to_idx(ord))) | |
3797 | super->updates_pending++; | |
8d45d196 | 3798 | } |
47ee5a45 | 3799 | |
19859edc | 3800 | /* check if in_sync */ |
0556e1a2 | 3801 | if (state & DS_INSYNC && ord & IMSM_ORD_REBUILD && is_rebuilding(dev)) { |
b10b37b8 DW |
3802 | struct imsm_map *migr_map = get_imsm_map(dev, 1); |
3803 | ||
3804 | set_imsm_ord_tbl_ent(migr_map, n, ord_to_idx(ord)); | |
19859edc DW |
3805 | super->updates_pending++; |
3806 | } | |
8d45d196 | 3807 | |
0c046afd DW |
3808 | failed = imsm_count_failed(super, dev); |
3809 | map_state = imsm_check_degraded(super, dev, failed); | |
5802a811 | 3810 | |
0c046afd DW |
3811 | /* check if recovery complete, newly degraded, or failed */ |
3812 | if (map_state == IMSM_T_STATE_NORMAL && is_rebuilding(dev)) { | |
f8f603f1 | 3813 | end_migration(dev, map_state); |
0556e1a2 DW |
3814 | map = get_imsm_map(dev, 0); |
3815 | map->failed_disk_num = ~0; | |
0c046afd DW |
3816 | super->updates_pending++; |
3817 | } else if (map_state == IMSM_T_STATE_DEGRADED && | |
3818 | map->map_state != map_state && | |
3819 | !dev->vol.migr_state) { | |
3820 | dprintf("imsm: mark degraded\n"); | |
3821 | map->map_state = map_state; | |
3822 | super->updates_pending++; | |
3823 | } else if (map_state == IMSM_T_STATE_FAILED && | |
3824 | map->map_state != map_state) { | |
3825 | dprintf("imsm: mark failed\n"); | |
f8f603f1 | 3826 | end_migration(dev, map_state); |
0c046afd | 3827 | super->updates_pending++; |
5802a811 | 3828 | } |
845dea95 NB |
3829 | } |
3830 | ||
c2a1e7da DW |
3831 | static int store_imsm_mpb(int fd, struct intel_super *super) |
3832 | { | |
949c47a0 | 3833 | struct imsm_super *mpb = super->anchor; |
c2a1e7da DW |
3834 | __u32 mpb_size = __le32_to_cpu(mpb->mpb_size); |
3835 | unsigned long long dsize; | |
3836 | unsigned long long sectors; | |
3837 | ||
3838 | get_dev_size(fd, NULL, &dsize); | |
3839 | ||
272f648f DW |
3840 | if (mpb_size > 512) { |
3841 | /* -1 to account for anchor */ | |
3842 | sectors = mpb_sectors(mpb) - 1; | |
c2a1e7da | 3843 | |
272f648f DW |
3844 | /* write the extended mpb to the sectors preceeding the anchor */ |
3845 | if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0) | |
3846 | return 1; | |
c2a1e7da | 3847 | |
99e29264 | 3848 | if (write(fd, super->buf + 512, 512 * sectors) != 512 * sectors) |
272f648f DW |
3849 | return 1; |
3850 | } | |
c2a1e7da | 3851 | |
272f648f DW |
3852 | /* first block is stored on second to last sector of the disk */ |
3853 | if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0) | |
c2a1e7da DW |
3854 | return 1; |
3855 | ||
272f648f | 3856 | if (write(fd, super->buf, 512) != 512) |
c2a1e7da DW |
3857 | return 1; |
3858 | ||
c2a1e7da DW |
3859 | return 0; |
3860 | } | |
3861 | ||
2e735d19 | 3862 | static void imsm_sync_metadata(struct supertype *container) |
845dea95 | 3863 | { |
2e735d19 | 3864 | struct intel_super *super = container->sb; |
c2a1e7da DW |
3865 | |
3866 | if (!super->updates_pending) | |
3867 | return; | |
3868 | ||
c2c087e6 | 3869 | write_super_imsm(super, 0); |
c2a1e7da DW |
3870 | |
3871 | super->updates_pending = 0; | |
845dea95 NB |
3872 | } |
3873 | ||
272906ef DW |
3874 | static struct dl *imsm_readd(struct intel_super *super, int idx, struct active_array *a) |
3875 | { | |
3876 | struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member); | |
ff077194 | 3877 | int i = get_imsm_disk_idx(dev, idx); |
272906ef DW |
3878 | struct dl *dl; |
3879 | ||
3880 | for (dl = super->disks; dl; dl = dl->next) | |
3881 | if (dl->index == i) | |
3882 | break; | |
3883 | ||
f2f27e63 | 3884 | if (dl && dl->disk.status & FAILED_DISK) |
272906ef DW |
3885 | dl = NULL; |
3886 | ||
3887 | if (dl) | |
3888 | dprintf("%s: found %x:%x\n", __func__, dl->major, dl->minor); | |
3889 | ||
3890 | return dl; | |
3891 | } | |
3892 | ||
a20d2ba5 DW |
3893 | static struct dl *imsm_add_spare(struct intel_super *super, int slot, |
3894 | struct active_array *a, int activate_new) | |
272906ef DW |
3895 | { |
3896 | struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member); | |
e553d2a4 | 3897 | int idx = get_imsm_disk_idx(dev, slot); |
a20d2ba5 DW |
3898 | struct imsm_super *mpb = super->anchor; |
3899 | struct imsm_map *map; | |
272906ef DW |
3900 | unsigned long long pos; |
3901 | struct mdinfo *d; | |
3902 | struct extent *ex; | |
a20d2ba5 | 3903 | int i, j; |
272906ef DW |
3904 | int found; |
3905 | __u32 array_start; | |
329c8278 | 3906 | __u32 array_end; |
272906ef DW |
3907 | struct dl *dl; |
3908 | ||
3909 | for (dl = super->disks; dl; dl = dl->next) { | |
3910 | /* If in this array, skip */ | |
3911 | for (d = a->info.devs ; d ; d = d->next) | |
e553d2a4 DW |
3912 | if (d->state_fd >= 0 && |
3913 | d->disk.major == dl->major && | |
272906ef DW |
3914 | d->disk.minor == dl->minor) { |
3915 | dprintf("%x:%x already in array\n", dl->major, dl->minor); | |
3916 | break; | |
3917 | } | |
3918 | if (d) | |
3919 | continue; | |
3920 | ||
e553d2a4 | 3921 | /* skip in use or failed drives */ |
df474657 DW |
3922 | if (dl->disk.status & FAILED_DISK || idx == dl->index || |
3923 | dl->index == -2) { | |
3924 | dprintf("%x:%x status (failed: %d index: %d)\n", | |
3925 | dl->major, dl->minor, | |
3926 | (dl->disk.status & FAILED_DISK) == FAILED_DISK, idx); | |
9a1608e5 DW |
3927 | continue; |
3928 | } | |
3929 | ||
a20d2ba5 DW |
3930 | /* skip pure spares when we are looking for partially |
3931 | * assimilated drives | |
3932 | */ | |
3933 | if (dl->index == -1 && !activate_new) | |
3934 | continue; | |
3935 | ||
272906ef | 3936 | /* Does this unused device have the requisite free space? |
a20d2ba5 | 3937 | * It needs to be able to cover all member volumes |
272906ef DW |
3938 | */ |
3939 | ex = get_extents(super, dl); | |
3940 | if (!ex) { | |
3941 | dprintf("cannot get extents\n"); | |
3942 | continue; | |
3943 | } | |
a20d2ba5 DW |
3944 | for (i = 0; i < mpb->num_raid_devs; i++) { |
3945 | dev = get_imsm_dev(super, i); | |
3946 | map = get_imsm_map(dev, 0); | |
272906ef | 3947 | |
a20d2ba5 DW |
3948 | /* check if this disk is already a member of |
3949 | * this array | |
272906ef | 3950 | */ |
620b1713 | 3951 | if (get_imsm_disk_slot(map, dl->index) >= 0) |
a20d2ba5 DW |
3952 | continue; |
3953 | ||
3954 | found = 0; | |
3955 | j = 0; | |
3956 | pos = 0; | |
3957 | array_start = __le32_to_cpu(map->pba_of_lba0); | |
329c8278 DW |
3958 | array_end = array_start + |
3959 | __le32_to_cpu(map->blocks_per_member) - 1; | |
a20d2ba5 DW |
3960 | |
3961 | do { | |
3962 | /* check that we can start at pba_of_lba0 with | |
3963 | * blocks_per_member of space | |
3964 | */ | |
329c8278 | 3965 | if (array_start >= pos && array_end < ex[j].start) { |
a20d2ba5 DW |
3966 | found = 1; |
3967 | break; | |
3968 | } | |
3969 | pos = ex[j].start + ex[j].size; | |
3970 | j++; | |
3971 | } while (ex[j-1].size); | |
3972 | ||
3973 | if (!found) | |
272906ef | 3974 | break; |
a20d2ba5 | 3975 | } |
272906ef DW |
3976 | |
3977 | free(ex); | |
a20d2ba5 | 3978 | if (i < mpb->num_raid_devs) { |
329c8278 DW |
3979 | dprintf("%x:%x does not have %u to %u available\n", |
3980 | dl->major, dl->minor, array_start, array_end); | |
272906ef DW |
3981 | /* No room */ |
3982 | continue; | |
a20d2ba5 DW |
3983 | } |
3984 | return dl; | |
272906ef DW |
3985 | } |
3986 | ||
3987 | return dl; | |
3988 | } | |
3989 | ||
88758e9d DW |
3990 | static struct mdinfo *imsm_activate_spare(struct active_array *a, |
3991 | struct metadata_update **updates) | |
3992 | { | |
3993 | /** | |
d23fe947 DW |
3994 | * Find a device with unused free space and use it to replace a |
3995 | * failed/vacant region in an array. We replace failed regions one a | |
3996 | * array at a time. The result is that a new spare disk will be added | |
3997 | * to the first failed array and after the monitor has finished | |
3998 | * propagating failures the remainder will be consumed. | |
88758e9d | 3999 | * |
d23fe947 DW |
4000 | * FIXME add a capability for mdmon to request spares from another |
4001 | * container. | |
88758e9d DW |
4002 | */ |
4003 | ||
4004 | struct intel_super *super = a->container->sb; | |
88758e9d | 4005 | int inst = a->info.container_member; |
949c47a0 | 4006 | struct imsm_dev *dev = get_imsm_dev(super, inst); |
a965f303 | 4007 | struct imsm_map *map = get_imsm_map(dev, 0); |
88758e9d DW |
4008 | int failed = a->info.array.raid_disks; |
4009 | struct mdinfo *rv = NULL; | |
4010 | struct mdinfo *d; | |
4011 | struct mdinfo *di; | |
4012 | struct metadata_update *mu; | |
4013 | struct dl *dl; | |
4014 | struct imsm_update_activate_spare *u; | |
4015 | int num_spares = 0; | |
4016 | int i; | |
4017 | ||
4018 | for (d = a->info.devs ; d ; d = d->next) { | |
4019 | if ((d->curr_state & DS_FAULTY) && | |
4020 | d->state_fd >= 0) | |
4021 | /* wait for Removal to happen */ | |
4022 | return NULL; | |
4023 | if (d->state_fd >= 0) | |
4024 | failed--; | |
4025 | } | |
4026 | ||
4027 | dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n", | |
4028 | inst, failed, a->info.array.raid_disks, a->info.array.level); | |
fb49eef2 | 4029 | if (imsm_check_degraded(super, dev, failed) != IMSM_T_STATE_DEGRADED) |
88758e9d DW |
4030 | return NULL; |
4031 | ||
4032 | /* For each slot, if it is not working, find a spare */ | |
88758e9d DW |
4033 | for (i = 0; i < a->info.array.raid_disks; i++) { |
4034 | for (d = a->info.devs ; d ; d = d->next) | |
4035 | if (d->disk.raid_disk == i) | |
4036 | break; | |
4037 | dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0); | |
4038 | if (d && (d->state_fd >= 0)) | |
4039 | continue; | |
4040 | ||
272906ef | 4041 | /* |
a20d2ba5 DW |
4042 | * OK, this device needs recovery. Try to re-add the |
4043 | * previous occupant of this slot, if this fails see if | |
4044 | * we can continue the assimilation of a spare that was | |
4045 | * partially assimilated, finally try to activate a new | |
4046 | * spare. | |
272906ef DW |
4047 | */ |
4048 | dl = imsm_readd(super, i, a); | |
4049 | if (!dl) | |
a20d2ba5 DW |
4050 | dl = imsm_add_spare(super, i, a, 0); |
4051 | if (!dl) | |
4052 | dl = imsm_add_spare(super, i, a, 1); | |
272906ef DW |
4053 | if (!dl) |
4054 | continue; | |
4055 | ||
4056 | /* found a usable disk with enough space */ | |
4057 | di = malloc(sizeof(*di)); | |
79244939 DW |
4058 | if (!di) |
4059 | continue; | |
272906ef DW |
4060 | memset(di, 0, sizeof(*di)); |
4061 | ||
4062 | /* dl->index will be -1 in the case we are activating a | |
4063 | * pristine spare. imsm_process_update() will create a | |
4064 | * new index in this case. Once a disk is found to be | |
4065 | * failed in all member arrays it is kicked from the | |
4066 | * metadata | |
4067 | */ | |
4068 | di->disk.number = dl->index; | |
d23fe947 | 4069 | |
272906ef DW |
4070 | /* (ab)use di->devs to store a pointer to the device |
4071 | * we chose | |
4072 | */ | |
4073 | di->devs = (struct mdinfo *) dl; | |
4074 | ||
4075 | di->disk.raid_disk = i; | |
4076 | di->disk.major = dl->major; | |
4077 | di->disk.minor = dl->minor; | |
4078 | di->disk.state = 0; | |
4079 | di->data_offset = __le32_to_cpu(map->pba_of_lba0); | |
4080 | di->component_size = a->info.component_size; | |
4081 | di->container_member = inst; | |
148acb7b | 4082 | super->random = random32(); |
272906ef DW |
4083 | di->next = rv; |
4084 | rv = di; | |
4085 | num_spares++; | |
4086 | dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor, | |
4087 | i, di->data_offset); | |
88758e9d | 4088 | |
272906ef | 4089 | break; |
88758e9d DW |
4090 | } |
4091 | ||
4092 | if (!rv) | |
4093 | /* No spares found */ | |
4094 | return rv; | |
4095 | /* Now 'rv' has a list of devices to return. | |
4096 | * Create a metadata_update record to update the | |
4097 | * disk_ord_tbl for the array | |
4098 | */ | |
4099 | mu = malloc(sizeof(*mu)); | |
79244939 DW |
4100 | if (mu) { |
4101 | mu->buf = malloc(sizeof(struct imsm_update_activate_spare) * num_spares); | |
4102 | if (mu->buf == NULL) { | |
4103 | free(mu); | |
4104 | mu = NULL; | |
4105 | } | |
4106 | } | |
4107 | if (!mu) { | |
4108 | while (rv) { | |
4109 | struct mdinfo *n = rv->next; | |
4110 | ||
4111 | free(rv); | |
4112 | rv = n; | |
4113 | } | |
4114 | return NULL; | |
4115 | } | |
4116 | ||
88758e9d DW |
4117 | mu->space = NULL; |
4118 | mu->len = sizeof(struct imsm_update_activate_spare) * num_spares; | |
4119 | mu->next = *updates; | |
4120 | u = (struct imsm_update_activate_spare *) mu->buf; | |
4121 | ||
4122 | for (di = rv ; di ; di = di->next) { | |
4123 | u->type = update_activate_spare; | |
d23fe947 DW |
4124 | u->dl = (struct dl *) di->devs; |
4125 | di->devs = NULL; | |
88758e9d DW |
4126 | u->slot = di->disk.raid_disk; |
4127 | u->array = inst; | |
4128 | u->next = u + 1; | |
4129 | u++; | |
4130 | } | |
4131 | (u-1)->next = NULL; | |
4132 | *updates = mu; | |
4133 | ||
4134 | return rv; | |
4135 | } | |
4136 | ||
54c2c1ea | 4137 | static int disks_overlap(struct intel_super *super, int idx, struct imsm_update_create_array *u) |
8273f55e | 4138 | { |
54c2c1ea DW |
4139 | struct imsm_dev *dev = get_imsm_dev(super, idx); |
4140 | struct imsm_map *map = get_imsm_map(dev, 0); | |
4141 | struct imsm_map *new_map = get_imsm_map(&u->dev, 0); | |
4142 | struct disk_info *inf = get_disk_info(u); | |
4143 | struct imsm_disk *disk; | |
8273f55e DW |
4144 | int i; |
4145 | int j; | |
8273f55e | 4146 | |
54c2c1ea DW |
4147 | for (i = 0; i < map->num_members; i++) { |
4148 | disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i)); | |
4149 | for (j = 0; j < new_map->num_members; j++) | |
4150 | if (serialcmp(disk->serial, inf[j].serial) == 0) | |
8273f55e DW |
4151 | return 1; |
4152 | } | |
4153 | ||
4154 | return 0; | |
4155 | } | |
4156 | ||
24565c9a | 4157 | static void imsm_delete(struct intel_super *super, struct dl **dlp, int index); |
ae6aad82 | 4158 | |
e8319a19 DW |
4159 | static void imsm_process_update(struct supertype *st, |
4160 | struct metadata_update *update) | |
4161 | { | |
4162 | /** | |
4163 | * crack open the metadata_update envelope to find the update record | |
4164 | * update can be one of: | |
4165 | * update_activate_spare - a spare device has replaced a failed | |
4166 | * device in an array, update the disk_ord_tbl. If this disk is | |
4167 | * present in all member arrays then also clear the SPARE_DISK | |
4168 | * flag | |
4169 | */ | |
4170 | struct intel_super *super = st->sb; | |
4d7b1503 | 4171 | struct imsm_super *mpb; |
e8319a19 DW |
4172 | enum imsm_update_type type = *(enum imsm_update_type *) update->buf; |
4173 | ||
4d7b1503 DW |
4174 | /* update requires a larger buf but the allocation failed */ |
4175 | if (super->next_len && !super->next_buf) { | |
4176 | super->next_len = 0; | |
4177 | return; | |
4178 | } | |
4179 | ||
4180 | if (super->next_buf) { | |
4181 | memcpy(super->next_buf, super->buf, super->len); | |
4182 | free(super->buf); | |
4183 | super->len = super->next_len; | |
4184 | super->buf = super->next_buf; | |
4185 | ||
4186 | super->next_len = 0; | |
4187 | super->next_buf = NULL; | |
4188 | } | |
4189 | ||
4190 | mpb = super->anchor; | |
4191 | ||
e8319a19 DW |
4192 | switch (type) { |
4193 | case update_activate_spare: { | |
4194 | struct imsm_update_activate_spare *u = (void *) update->buf; | |
949c47a0 | 4195 | struct imsm_dev *dev = get_imsm_dev(super, u->array); |
a965f303 | 4196 | struct imsm_map *map = get_imsm_map(dev, 0); |
0c046afd | 4197 | struct imsm_map *migr_map; |
e8319a19 DW |
4198 | struct active_array *a; |
4199 | struct imsm_disk *disk; | |
0c046afd | 4200 | __u8 to_state; |
e8319a19 | 4201 | struct dl *dl; |
e8319a19 | 4202 | unsigned int found; |
0c046afd DW |
4203 | int failed; |
4204 | int victim = get_imsm_disk_idx(dev, u->slot); | |
e8319a19 DW |
4205 | int i; |
4206 | ||
4207 | for (dl = super->disks; dl; dl = dl->next) | |
d23fe947 | 4208 | if (dl == u->dl) |
e8319a19 DW |
4209 | break; |
4210 | ||
4211 | if (!dl) { | |
4212 | fprintf(stderr, "error: imsm_activate_spare passed " | |
1f24f035 DW |
4213 | "an unknown disk (index: %d)\n", |
4214 | u->dl->index); | |
e8319a19 DW |
4215 | return; |
4216 | } | |
4217 | ||
4218 | super->updates_pending++; | |
4219 | ||
0c046afd DW |
4220 | /* count failures (excluding rebuilds and the victim) |
4221 | * to determine map[0] state | |
4222 | */ | |
4223 | failed = 0; | |
4224 | for (i = 0; i < map->num_members; i++) { | |
4225 | if (i == u->slot) | |
4226 | continue; | |
4227 | disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i)); | |
f2f27e63 | 4228 | if (!disk || disk->status & FAILED_DISK) |
0c046afd DW |
4229 | failed++; |
4230 | } | |
4231 | ||
d23fe947 DW |
4232 | /* adding a pristine spare, assign a new index */ |
4233 | if (dl->index < 0) { | |
4234 | dl->index = super->anchor->num_disks; | |
4235 | super->anchor->num_disks++; | |
4236 | } | |
d23fe947 | 4237 | disk = &dl->disk; |
f2f27e63 DW |
4238 | disk->status |= CONFIGURED_DISK; |
4239 | disk->status &= ~SPARE_DISK; | |
e8319a19 | 4240 | |
0c046afd DW |
4241 | /* mark rebuild */ |
4242 | to_state = imsm_check_degraded(super, dev, failed); | |
4243 | map->map_state = IMSM_T_STATE_DEGRADED; | |
e3bba0e0 | 4244 | migrate(dev, to_state, MIGR_REBUILD); |
0c046afd DW |
4245 | migr_map = get_imsm_map(dev, 1); |
4246 | set_imsm_ord_tbl_ent(map, u->slot, dl->index); | |
4247 | set_imsm_ord_tbl_ent(migr_map, u->slot, dl->index | IMSM_ORD_REBUILD); | |
4248 | ||
148acb7b DW |
4249 | /* update the family_num to mark a new container |
4250 | * generation, being careful to record the existing | |
4251 | * family_num in orig_family_num to clean up after | |
4252 | * earlier mdadm versions that neglected to set it. | |
4253 | */ | |
4254 | if (mpb->orig_family_num == 0) | |
4255 | mpb->orig_family_num = mpb->family_num; | |
4256 | mpb->family_num += super->random; | |
4257 | ||
e8319a19 DW |
4258 | /* count arrays using the victim in the metadata */ |
4259 | found = 0; | |
4260 | for (a = st->arrays; a ; a = a->next) { | |
949c47a0 | 4261 | dev = get_imsm_dev(super, a->info.container_member); |
620b1713 DW |
4262 | map = get_imsm_map(dev, 0); |
4263 | ||
4264 | if (get_imsm_disk_slot(map, victim) >= 0) | |
4265 | found++; | |
e8319a19 DW |
4266 | } |
4267 | ||
24565c9a | 4268 | /* delete the victim if it is no longer being |
e8319a19 DW |
4269 | * utilized anywhere |
4270 | */ | |
e8319a19 | 4271 | if (!found) { |
ae6aad82 | 4272 | struct dl **dlp; |
24565c9a | 4273 | |
47ee5a45 DW |
4274 | /* We know that 'manager' isn't touching anything, |
4275 | * so it is safe to delete | |
4276 | */ | |
24565c9a | 4277 | for (dlp = &super->disks; *dlp; dlp = &(*dlp)->next) |
ae6aad82 DW |
4278 | if ((*dlp)->index == victim) |
4279 | break; | |
47ee5a45 DW |
4280 | |
4281 | /* victim may be on the missing list */ | |
4282 | if (!*dlp) | |
4283 | for (dlp = &super->missing; *dlp; dlp = &(*dlp)->next) | |
4284 | if ((*dlp)->index == victim) | |
4285 | break; | |
24565c9a | 4286 | imsm_delete(super, dlp, victim); |
e8319a19 | 4287 | } |
8273f55e DW |
4288 | break; |
4289 | } | |
4290 | case update_create_array: { | |
4291 | /* someone wants to create a new array, we need to be aware of | |
4292 | * a few races/collisions: | |
4293 | * 1/ 'Create' called by two separate instances of mdadm | |
4294 | * 2/ 'Create' versus 'activate_spare': mdadm has chosen | |
4295 | * devices that have since been assimilated via | |
4296 | * activate_spare. | |
4297 | * In the event this update can not be carried out mdadm will | |
4298 | * (FIX ME) notice that its update did not take hold. | |
4299 | */ | |
4300 | struct imsm_update_create_array *u = (void *) update->buf; | |
ba2de7ba | 4301 | struct intel_dev *dv; |
8273f55e DW |
4302 | struct imsm_dev *dev; |
4303 | struct imsm_map *map, *new_map; | |
4304 | unsigned long long start, end; | |
4305 | unsigned long long new_start, new_end; | |
4306 | int i; | |
54c2c1ea DW |
4307 | struct disk_info *inf; |
4308 | struct dl *dl; | |
8273f55e DW |
4309 | |
4310 | /* handle racing creates: first come first serve */ | |
4311 | if (u->dev_idx < mpb->num_raid_devs) { | |
4312 | dprintf("%s: subarray %d already defined\n", | |
4313 | __func__, u->dev_idx); | |
ba2de7ba | 4314 | goto create_error; |
8273f55e DW |
4315 | } |
4316 | ||
4317 | /* check update is next in sequence */ | |
4318 | if (u->dev_idx != mpb->num_raid_devs) { | |
6a3e913e DW |
4319 | dprintf("%s: can not create array %d expected index %d\n", |
4320 | __func__, u->dev_idx, mpb->num_raid_devs); | |
ba2de7ba | 4321 | goto create_error; |
8273f55e DW |
4322 | } |
4323 | ||
a965f303 | 4324 | new_map = get_imsm_map(&u->dev, 0); |
8273f55e DW |
4325 | new_start = __le32_to_cpu(new_map->pba_of_lba0); |
4326 | new_end = new_start + __le32_to_cpu(new_map->blocks_per_member); | |
54c2c1ea | 4327 | inf = get_disk_info(u); |
8273f55e DW |
4328 | |
4329 | /* handle activate_spare versus create race: | |
4330 | * check to make sure that overlapping arrays do not include | |
4331 | * overalpping disks | |
4332 | */ | |
4333 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
949c47a0 | 4334 | dev = get_imsm_dev(super, i); |
a965f303 | 4335 | map = get_imsm_map(dev, 0); |
8273f55e DW |
4336 | start = __le32_to_cpu(map->pba_of_lba0); |
4337 | end = start + __le32_to_cpu(map->blocks_per_member); | |
4338 | if ((new_start >= start && new_start <= end) || | |
4339 | (start >= new_start && start <= new_end)) | |
54c2c1ea DW |
4340 | /* overlap */; |
4341 | else | |
4342 | continue; | |
4343 | ||
4344 | if (disks_overlap(super, i, u)) { | |
8273f55e | 4345 | dprintf("%s: arrays overlap\n", __func__); |
ba2de7ba | 4346 | goto create_error; |
8273f55e DW |
4347 | } |
4348 | } | |
8273f55e | 4349 | |
949c47a0 DW |
4350 | /* check that prepare update was successful */ |
4351 | if (!update->space) { | |
4352 | dprintf("%s: prepare update failed\n", __func__); | |
ba2de7ba | 4353 | goto create_error; |
949c47a0 DW |
4354 | } |
4355 | ||
54c2c1ea DW |
4356 | /* check that all disks are still active before committing |
4357 | * changes. FIXME: could we instead handle this by creating a | |
4358 | * degraded array? That's probably not what the user expects, | |
4359 | * so better to drop this update on the floor. | |
4360 | */ | |
4361 | for (i = 0; i < new_map->num_members; i++) { | |
4362 | dl = serial_to_dl(inf[i].serial, super); | |
4363 | if (!dl) { | |
4364 | dprintf("%s: disk disappeared\n", __func__); | |
ba2de7ba | 4365 | goto create_error; |
54c2c1ea | 4366 | } |
949c47a0 DW |
4367 | } |
4368 | ||
8273f55e | 4369 | super->updates_pending++; |
54c2c1ea DW |
4370 | |
4371 | /* convert spares to members and fixup ord_tbl */ | |
4372 | for (i = 0; i < new_map->num_members; i++) { | |
4373 | dl = serial_to_dl(inf[i].serial, super); | |
4374 | if (dl->index == -1) { | |
4375 | dl->index = mpb->num_disks; | |
4376 | mpb->num_disks++; | |
4377 | dl->disk.status |= CONFIGURED_DISK; | |
4378 | dl->disk.status &= ~SPARE_DISK; | |
4379 | } | |
4380 | set_imsm_ord_tbl_ent(new_map, i, dl->index); | |
4381 | } | |
4382 | ||
ba2de7ba DW |
4383 | dv = update->space; |
4384 | dev = dv->dev; | |
949c47a0 DW |
4385 | update->space = NULL; |
4386 | imsm_copy_dev(dev, &u->dev); | |
ba2de7ba DW |
4387 | dv->index = u->dev_idx; |
4388 | dv->next = super->devlist; | |
4389 | super->devlist = dv; | |
8273f55e | 4390 | mpb->num_raid_devs++; |
8273f55e | 4391 | |
4d1313e9 | 4392 | imsm_update_version_info(super); |
8273f55e | 4393 | break; |
ba2de7ba DW |
4394 | create_error: |
4395 | /* mdmon knows how to release update->space, but not | |
4396 | * ((struct intel_dev *) update->space)->dev | |
4397 | */ | |
4398 | if (update->space) { | |
4399 | dv = update->space; | |
4400 | free(dv->dev); | |
4401 | } | |
8273f55e | 4402 | break; |
e8319a19 | 4403 | } |
43dad3d6 DW |
4404 | case update_add_disk: |
4405 | ||
4406 | /* we may be able to repair some arrays if disks are | |
4407 | * being added */ | |
4408 | if (super->add) { | |
4409 | struct active_array *a; | |
072b727f DW |
4410 | |
4411 | super->updates_pending++; | |
43dad3d6 DW |
4412 | for (a = st->arrays; a; a = a->next) |
4413 | a->check_degraded = 1; | |
4414 | } | |
e553d2a4 | 4415 | /* add some spares to the metadata */ |
43dad3d6 | 4416 | while (super->add) { |
e553d2a4 DW |
4417 | struct dl *al; |
4418 | ||
43dad3d6 DW |
4419 | al = super->add; |
4420 | super->add = al->next; | |
43dad3d6 DW |
4421 | al->next = super->disks; |
4422 | super->disks = al; | |
e553d2a4 DW |
4423 | dprintf("%s: added %x:%x\n", |
4424 | __func__, al->major, al->minor); | |
43dad3d6 DW |
4425 | } |
4426 | ||
4427 | break; | |
e8319a19 DW |
4428 | } |
4429 | } | |
88758e9d | 4430 | |
8273f55e DW |
4431 | static void imsm_prepare_update(struct supertype *st, |
4432 | struct metadata_update *update) | |
4433 | { | |
949c47a0 | 4434 | /** |
4d7b1503 DW |
4435 | * Allocate space to hold new disk entries, raid-device entries or a new |
4436 | * mpb if necessary. The manager synchronously waits for updates to | |
4437 | * complete in the monitor, so new mpb buffers allocated here can be | |
4438 | * integrated by the monitor thread without worrying about live pointers | |
4439 | * in the manager thread. | |
8273f55e | 4440 | */ |
949c47a0 | 4441 | enum imsm_update_type type = *(enum imsm_update_type *) update->buf; |
4d7b1503 DW |
4442 | struct intel_super *super = st->sb; |
4443 | struct imsm_super *mpb = super->anchor; | |
4444 | size_t buf_len; | |
4445 | size_t len = 0; | |
949c47a0 DW |
4446 | |
4447 | switch (type) { | |
4448 | case update_create_array: { | |
4449 | struct imsm_update_create_array *u = (void *) update->buf; | |
ba2de7ba | 4450 | struct intel_dev *dv; |
54c2c1ea DW |
4451 | struct imsm_dev *dev = &u->dev; |
4452 | struct imsm_map *map = get_imsm_map(dev, 0); | |
4453 | struct dl *dl; | |
4454 | struct disk_info *inf; | |
4455 | int i; | |
4456 | int activate = 0; | |
949c47a0 | 4457 | |
54c2c1ea DW |
4458 | inf = get_disk_info(u); |
4459 | len = sizeof_imsm_dev(dev, 1); | |
ba2de7ba DW |
4460 | /* allocate a new super->devlist entry */ |
4461 | dv = malloc(sizeof(*dv)); | |
4462 | if (dv) { | |
4463 | dv->dev = malloc(len); | |
4464 | if (dv->dev) | |
4465 | update->space = dv; | |
4466 | else { | |
4467 | free(dv); | |
4468 | update->space = NULL; | |
4469 | } | |
4470 | } | |
949c47a0 | 4471 | |
54c2c1ea DW |
4472 | /* count how many spares will be converted to members */ |
4473 | for (i = 0; i < map->num_members; i++) { | |
4474 | dl = serial_to_dl(inf[i].serial, super); | |
4475 | if (!dl) { | |
4476 | /* hmm maybe it failed?, nothing we can do about | |
4477 | * it here | |
4478 | */ | |
4479 | continue; | |
4480 | } | |
4481 | if (count_memberships(dl, super) == 0) | |
4482 | activate++; | |
4483 | } | |
4484 | len += activate * sizeof(struct imsm_disk); | |
949c47a0 DW |
4485 | break; |
4486 | default: | |
4487 | break; | |
4488 | } | |
4489 | } | |
8273f55e | 4490 | |
4d7b1503 DW |
4491 | /* check if we need a larger metadata buffer */ |
4492 | if (super->next_buf) | |
4493 | buf_len = super->next_len; | |
4494 | else | |
4495 | buf_len = super->len; | |
4496 | ||
4497 | if (__le32_to_cpu(mpb->mpb_size) + len > buf_len) { | |
4498 | /* ok we need a larger buf than what is currently allocated | |
4499 | * if this allocation fails process_update will notice that | |
4500 | * ->next_len is set and ->next_buf is NULL | |
4501 | */ | |
4502 | buf_len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + len, 512); | |
4503 | if (super->next_buf) | |
4504 | free(super->next_buf); | |
4505 | ||
4506 | super->next_len = buf_len; | |
1f45a8ad DW |
4507 | if (posix_memalign(&super->next_buf, 512, buf_len) == 0) |
4508 | memset(super->next_buf, 0, buf_len); | |
4509 | else | |
4d7b1503 DW |
4510 | super->next_buf = NULL; |
4511 | } | |
8273f55e DW |
4512 | } |
4513 | ||
ae6aad82 | 4514 | /* must be called while manager is quiesced */ |
24565c9a | 4515 | static void imsm_delete(struct intel_super *super, struct dl **dlp, int index) |
ae6aad82 DW |
4516 | { |
4517 | struct imsm_super *mpb = super->anchor; | |
ae6aad82 DW |
4518 | struct dl *iter; |
4519 | struct imsm_dev *dev; | |
4520 | struct imsm_map *map; | |
24565c9a DW |
4521 | int i, j, num_members; |
4522 | __u32 ord; | |
ae6aad82 | 4523 | |
24565c9a DW |
4524 | dprintf("%s: deleting device[%d] from imsm_super\n", |
4525 | __func__, index); | |
ae6aad82 DW |
4526 | |
4527 | /* shift all indexes down one */ | |
4528 | for (iter = super->disks; iter; iter = iter->next) | |
24565c9a | 4529 | if (iter->index > index) |
ae6aad82 | 4530 | iter->index--; |
47ee5a45 DW |
4531 | for (iter = super->missing; iter; iter = iter->next) |
4532 | if (iter->index > index) | |
4533 | iter->index--; | |
ae6aad82 DW |
4534 | |
4535 | for (i = 0; i < mpb->num_raid_devs; i++) { | |
4536 | dev = get_imsm_dev(super, i); | |
4537 | map = get_imsm_map(dev, 0); | |
24565c9a DW |
4538 | num_members = map->num_members; |
4539 | for (j = 0; j < num_members; j++) { | |
4540 | /* update ord entries being careful not to propagate | |
4541 | * ord-flags to the first map | |
4542 | */ | |
4543 | ord = get_imsm_ord_tbl_ent(dev, j); | |
ae6aad82 | 4544 | |
24565c9a DW |
4545 | if (ord_to_idx(ord) <= index) |
4546 | continue; | |
ae6aad82 | 4547 | |
24565c9a DW |
4548 | map = get_imsm_map(dev, 0); |
4549 | set_imsm_ord_tbl_ent(map, j, ord_to_idx(ord - 1)); | |
4550 | map = get_imsm_map(dev, 1); | |
4551 | if (map) | |
4552 | set_imsm_ord_tbl_ent(map, j, ord - 1); | |
ae6aad82 DW |
4553 | } |
4554 | } | |
4555 | ||
4556 | mpb->num_disks--; | |
4557 | super->updates_pending++; | |
24565c9a DW |
4558 | if (*dlp) { |
4559 | struct dl *dl = *dlp; | |
4560 | ||
4561 | *dlp = (*dlp)->next; | |
4562 | __free_imsm_disk(dl); | |
4563 | } | |
ae6aad82 | 4564 | } |
0e600426 | 4565 | #endif /* MDASSEMBLE */ |
ae6aad82 | 4566 | |
cdddbdbc DW |
4567 | struct superswitch super_imsm = { |
4568 | #ifndef MDASSEMBLE | |
4569 | .examine_super = examine_super_imsm, | |
4570 | .brief_examine_super = brief_examine_super_imsm, | |
4737ae25 | 4571 | .brief_examine_subarrays = brief_examine_subarrays_imsm, |
9d84c8ea | 4572 | .export_examine_super = export_examine_super_imsm, |
cdddbdbc DW |
4573 | .detail_super = detail_super_imsm, |
4574 | .brief_detail_super = brief_detail_super_imsm, | |
bf5a934a | 4575 | .write_init_super = write_init_super_imsm, |
0e600426 N |
4576 | .validate_geometry = validate_geometry_imsm, |
4577 | .add_to_super = add_to_super_imsm, | |
d665cc31 | 4578 | .detail_platform = detail_platform_imsm, |
cdddbdbc DW |
4579 | #endif |
4580 | .match_home = match_home_imsm, | |
4581 | .uuid_from_super= uuid_from_super_imsm, | |
4582 | .getinfo_super = getinfo_super_imsm, | |
4583 | .update_super = update_super_imsm, | |
4584 | ||
4585 | .avail_size = avail_size_imsm, | |
4586 | ||
4587 | .compare_super = compare_super_imsm, | |
4588 | ||
4589 | .load_super = load_super_imsm, | |
bf5a934a | 4590 | .init_super = init_super_imsm, |
cdddbdbc DW |
4591 | .store_super = store_zero_imsm, |
4592 | .free_super = free_super_imsm, | |
4593 | .match_metadata_desc = match_metadata_desc_imsm, | |
bf5a934a | 4594 | .container_content = container_content_imsm, |
a18a888e | 4595 | .default_layout = imsm_level_to_layout, |
cdddbdbc | 4596 | |
cdddbdbc | 4597 | .external = 1, |
4cce4069 | 4598 | .name = "imsm", |
845dea95 | 4599 | |
0e600426 | 4600 | #ifndef MDASSEMBLE |
845dea95 NB |
4601 | /* for mdmon */ |
4602 | .open_new = imsm_open_new, | |
4603 | .load_super = load_super_imsm, | |
ed9d66aa | 4604 | .set_array_state= imsm_set_array_state, |
845dea95 NB |
4605 | .set_disk = imsm_set_disk, |
4606 | .sync_metadata = imsm_sync_metadata, | |
88758e9d | 4607 | .activate_spare = imsm_activate_spare, |
e8319a19 | 4608 | .process_update = imsm_process_update, |
8273f55e | 4609 | .prepare_update = imsm_prepare_update, |
0e600426 | 4610 | #endif /* MDASSEMBLE */ |
cdddbdbc | 4611 | }; |