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