]>
Commit | Line | Data |
---|---|---|
a322f70c DW |
1 | /* |
2 | * mdadm - manage Linux "md" devices aka RAID arrays. | |
3 | * | |
e736b623 | 4 | * Copyright (C) 2006-2009 Neil Brown <neilb@suse.de> |
a322f70c DW |
5 | * |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | * | |
21 | * Author: Neil Brown | |
22 | * Email: <neil@brown.name> | |
23 | * | |
24 | * Specifications for DDF takes from Common RAID DDF Specification Revision 1.2 | |
25 | * (July 28 2006). Reused by permission of SNIA. | |
26 | */ | |
27 | ||
28 | #define HAVE_STDINT_H 1 | |
29 | #include "mdadm.h" | |
549e9569 | 30 | #include "mdmon.h" |
a322f70c DW |
31 | #include "sha1.h" |
32 | #include <values.h> | |
33 | ||
a322f70c DW |
34 | /* a non-official T10 name for creation GUIDs */ |
35 | static char T10[] = "Linux-MD"; | |
36 | ||
37 | /* DDF timestamps are 1980 based, so we need to add | |
38 | * second-in-decade-of-seventies to convert to linux timestamps. | |
39 | * 10 years with 2 leap years. | |
40 | */ | |
41 | #define DECADE (3600*24*(365*10+2)) | |
42 | unsigned long crc32( | |
43 | unsigned long crc, | |
44 | const unsigned char *buf, | |
45 | unsigned len); | |
46 | ||
bedbf68a | 47 | #define DDF_NOTFOUND (~0U) |
48 | #define DDF_CONTAINER (DDF_NOTFOUND-1) | |
49 | ||
a322f70c DW |
50 | /* The DDF metadata handling. |
51 | * DDF metadata lives at the end of the device. | |
52 | * The last 512 byte block provides an 'anchor' which is used to locate | |
53 | * the rest of the metadata which usually lives immediately behind the anchor. | |
54 | * | |
55 | * Note: | |
56 | * - all multibyte numeric fields are bigendian. | |
57 | * - all strings are space padded. | |
58 | * | |
59 | */ | |
60 | ||
61 | /* Primary Raid Level (PRL) */ | |
62 | #define DDF_RAID0 0x00 | |
63 | #define DDF_RAID1 0x01 | |
64 | #define DDF_RAID3 0x03 | |
65 | #define DDF_RAID4 0x04 | |
66 | #define DDF_RAID5 0x05 | |
67 | #define DDF_RAID1E 0x11 | |
68 | #define DDF_JBOD 0x0f | |
69 | #define DDF_CONCAT 0x1f | |
70 | #define DDF_RAID5E 0x15 | |
71 | #define DDF_RAID5EE 0x25 | |
59e36268 | 72 | #define DDF_RAID6 0x06 |
a322f70c DW |
73 | |
74 | /* Raid Level Qualifier (RLQ) */ | |
75 | #define DDF_RAID0_SIMPLE 0x00 | |
76 | #define DDF_RAID1_SIMPLE 0x00 /* just 2 devices in this plex */ | |
77 | #define DDF_RAID1_MULTI 0x01 /* exactly 3 devices in this plex */ | |
78 | #define DDF_RAID3_0 0x00 /* parity in first extent */ | |
79 | #define DDF_RAID3_N 0x01 /* parity in last extent */ | |
80 | #define DDF_RAID4_0 0x00 /* parity in first extent */ | |
81 | #define DDF_RAID4_N 0x01 /* parity in last extent */ | |
82 | /* these apply to raid5e and raid5ee as well */ | |
83 | #define DDF_RAID5_0_RESTART 0x00 /* same as 'right asymmetric' - layout 1 */ | |
59e36268 | 84 | #define DDF_RAID6_0_RESTART 0x01 /* raid6 different from raid5 here!!! */ |
a322f70c DW |
85 | #define DDF_RAID5_N_RESTART 0x02 /* same as 'left asymmetric' - layout 0 */ |
86 | #define DDF_RAID5_N_CONTINUE 0x03 /* same as 'left symmetric' - layout 2 */ | |
87 | ||
88 | #define DDF_RAID1E_ADJACENT 0x00 /* raid10 nearcopies==2 */ | |
89 | #define DDF_RAID1E_OFFSET 0x01 /* raid10 offsetcopies==2 */ | |
90 | ||
91 | /* Secondary RAID Level (SRL) */ | |
92 | #define DDF_2STRIPED 0x00 /* This is weirder than RAID0 !! */ | |
93 | #define DDF_2MIRRORED 0x01 | |
94 | #define DDF_2CONCAT 0x02 | |
95 | #define DDF_2SPANNED 0x03 /* This is also weird - be careful */ | |
96 | ||
97 | /* Magic numbers */ | |
98 | #define DDF_HEADER_MAGIC __cpu_to_be32(0xDE11DE11) | |
99 | #define DDF_CONTROLLER_MAGIC __cpu_to_be32(0xAD111111) | |
100 | #define DDF_PHYS_RECORDS_MAGIC __cpu_to_be32(0x22222222) | |
101 | #define DDF_PHYS_DATA_MAGIC __cpu_to_be32(0x33333333) | |
102 | #define DDF_VIRT_RECORDS_MAGIC __cpu_to_be32(0xDDDDDDDD) | |
103 | #define DDF_VD_CONF_MAGIC __cpu_to_be32(0xEEEEEEEE) | |
104 | #define DDF_SPARE_ASSIGN_MAGIC __cpu_to_be32(0x55555555) | |
105 | #define DDF_VU_CONF_MAGIC __cpu_to_be32(0x88888888) | |
106 | #define DDF_VENDOR_LOG_MAGIC __cpu_to_be32(0x01dBEEF0) | |
107 | #define DDF_BBM_LOG_MAGIC __cpu_to_be32(0xABADB10C) | |
108 | ||
109 | #define DDF_GUID_LEN 24 | |
59e36268 NB |
110 | #define DDF_REVISION_0 "01.00.00" |
111 | #define DDF_REVISION_2 "01.02.00" | |
a322f70c DW |
112 | |
113 | struct ddf_header { | |
88c164f4 | 114 | __u32 magic; /* DDF_HEADER_MAGIC */ |
a322f70c DW |
115 | __u32 crc; |
116 | char guid[DDF_GUID_LEN]; | |
59e36268 | 117 | char revision[8]; /* 01.02.00 */ |
a322f70c DW |
118 | __u32 seq; /* starts at '1' */ |
119 | __u32 timestamp; | |
120 | __u8 openflag; | |
121 | __u8 foreignflag; | |
122 | __u8 enforcegroups; | |
123 | __u8 pad0; /* 0xff */ | |
124 | __u8 pad1[12]; /* 12 * 0xff */ | |
125 | /* 64 bytes so far */ | |
126 | __u8 header_ext[32]; /* reserved: fill with 0xff */ | |
127 | __u64 primary_lba; | |
128 | __u64 secondary_lba; | |
129 | __u8 type; | |
130 | __u8 pad2[3]; /* 0xff */ | |
131 | __u32 workspace_len; /* sectors for vendor space - | |
132 | * at least 32768(sectors) */ | |
133 | __u64 workspace_lba; | |
134 | __u16 max_pd_entries; /* one of 15, 63, 255, 1023, 4095 */ | |
135 | __u16 max_vd_entries; /* 2^(4,6,8,10,12)-1 : i.e. as above */ | |
136 | __u16 max_partitions; /* i.e. max num of configuration | |
137 | record entries per disk */ | |
138 | __u16 config_record_len; /* 1 +ROUNDUP(max_primary_element_entries | |
139 | *12/512) */ | |
140 | __u16 max_primary_element_entries; /* 16, 64, 256, 1024, or 4096 */ | |
141 | __u8 pad3[54]; /* 0xff */ | |
142 | /* 192 bytes so far */ | |
143 | __u32 controller_section_offset; | |
144 | __u32 controller_section_length; | |
145 | __u32 phys_section_offset; | |
146 | __u32 phys_section_length; | |
147 | __u32 virt_section_offset; | |
148 | __u32 virt_section_length; | |
149 | __u32 config_section_offset; | |
150 | __u32 config_section_length; | |
151 | __u32 data_section_offset; | |
152 | __u32 data_section_length; | |
153 | __u32 bbm_section_offset; | |
154 | __u32 bbm_section_length; | |
155 | __u32 diag_space_offset; | |
156 | __u32 diag_space_length; | |
157 | __u32 vendor_offset; | |
158 | __u32 vendor_length; | |
159 | /* 256 bytes so far */ | |
160 | __u8 pad4[256]; /* 0xff */ | |
161 | }; | |
162 | ||
163 | /* type field */ | |
164 | #define DDF_HEADER_ANCHOR 0x00 | |
165 | #define DDF_HEADER_PRIMARY 0x01 | |
166 | #define DDF_HEADER_SECONDARY 0x02 | |
167 | ||
168 | /* The content of the 'controller section' - global scope */ | |
169 | struct ddf_controller_data { | |
88c164f4 | 170 | __u32 magic; /* DDF_CONTROLLER_MAGIC */ |
a322f70c DW |
171 | __u32 crc; |
172 | char guid[DDF_GUID_LEN]; | |
173 | struct controller_type { | |
174 | __u16 vendor_id; | |
175 | __u16 device_id; | |
176 | __u16 sub_vendor_id; | |
177 | __u16 sub_device_id; | |
178 | } type; | |
179 | char product_id[16]; | |
180 | __u8 pad[8]; /* 0xff */ | |
181 | __u8 vendor_data[448]; | |
182 | }; | |
183 | ||
184 | /* The content of phys_section - global scope */ | |
185 | struct phys_disk { | |
88c164f4 | 186 | __u32 magic; /* DDF_PHYS_RECORDS_MAGIC */ |
a322f70c DW |
187 | __u32 crc; |
188 | __u16 used_pdes; | |
189 | __u16 max_pdes; | |
190 | __u8 pad[52]; | |
191 | struct phys_disk_entry { | |
192 | char guid[DDF_GUID_LEN]; | |
193 | __u32 refnum; | |
194 | __u16 type; | |
195 | __u16 state; | |
196 | __u64 config_size; /* DDF structures must be after here */ | |
197 | char path[18]; /* another horrible structure really */ | |
198 | __u8 pad[6]; | |
199 | } entries[0]; | |
200 | }; | |
201 | ||
202 | /* phys_disk_entry.type is a bitmap - bigendian remember */ | |
203 | #define DDF_Forced_PD_GUID 1 | |
204 | #define DDF_Active_in_VD 2 | |
88c164f4 | 205 | #define DDF_Global_Spare 4 /* VD_CONF records are ignored */ |
a322f70c DW |
206 | #define DDF_Spare 8 /* overrides Global_spare */ |
207 | #define DDF_Foreign 16 | |
208 | #define DDF_Legacy 32 /* no DDF on this device */ | |
209 | ||
210 | #define DDF_Interface_mask 0xf00 | |
211 | #define DDF_Interface_SCSI 0x100 | |
212 | #define DDF_Interface_SAS 0x200 | |
213 | #define DDF_Interface_SATA 0x300 | |
214 | #define DDF_Interface_FC 0x400 | |
215 | ||
216 | /* phys_disk_entry.state is a bigendian bitmap */ | |
217 | #define DDF_Online 1 | |
218 | #define DDF_Failed 2 /* overrides 1,4,8 */ | |
219 | #define DDF_Rebuilding 4 | |
220 | #define DDF_Transition 8 | |
221 | #define DDF_SMART 16 | |
222 | #define DDF_ReadErrors 32 | |
223 | #define DDF_Missing 64 | |
224 | ||
225 | /* The content of the virt_section global scope */ | |
226 | struct virtual_disk { | |
88c164f4 | 227 | __u32 magic; /* DDF_VIRT_RECORDS_MAGIC */ |
a322f70c DW |
228 | __u32 crc; |
229 | __u16 populated_vdes; | |
230 | __u16 max_vdes; | |
231 | __u8 pad[52]; | |
232 | struct virtual_entry { | |
233 | char guid[DDF_GUID_LEN]; | |
234 | __u16 unit; | |
235 | __u16 pad0; /* 0xffff */ | |
236 | __u16 guid_crc; | |
237 | __u16 type; | |
238 | __u8 state; | |
239 | __u8 init_state; | |
240 | __u8 pad1[14]; | |
241 | char name[16]; | |
242 | } entries[0]; | |
243 | }; | |
244 | ||
245 | /* virtual_entry.type is a bitmap - bigendian */ | |
246 | #define DDF_Shared 1 | |
247 | #define DDF_Enforce_Groups 2 | |
248 | #define DDF_Unicode 4 | |
249 | #define DDF_Owner_Valid 8 | |
250 | ||
251 | /* virtual_entry.state is a bigendian bitmap */ | |
252 | #define DDF_state_mask 0x7 | |
253 | #define DDF_state_optimal 0x0 | |
254 | #define DDF_state_degraded 0x1 | |
255 | #define DDF_state_deleted 0x2 | |
256 | #define DDF_state_missing 0x3 | |
257 | #define DDF_state_failed 0x4 | |
7a7cc504 | 258 | #define DDF_state_part_optimal 0x5 |
a322f70c DW |
259 | |
260 | #define DDF_state_morphing 0x8 | |
261 | #define DDF_state_inconsistent 0x10 | |
262 | ||
263 | /* virtual_entry.init_state is a bigendian bitmap */ | |
264 | #define DDF_initstate_mask 0x03 | |
265 | #define DDF_init_not 0x00 | |
7a7cc504 NB |
266 | #define DDF_init_quick 0x01 /* initialisation is progress. |
267 | * i.e. 'state_inconsistent' */ | |
a322f70c DW |
268 | #define DDF_init_full 0x02 |
269 | ||
270 | #define DDF_access_mask 0xc0 | |
271 | #define DDF_access_rw 0x00 | |
272 | #define DDF_access_ro 0x80 | |
273 | #define DDF_access_blocked 0xc0 | |
274 | ||
275 | /* The content of the config_section - local scope | |
276 | * It has multiple records each config_record_len sectors | |
277 | * They can be vd_config or spare_assign | |
278 | */ | |
279 | ||
280 | struct vd_config { | |
88c164f4 | 281 | __u32 magic; /* DDF_VD_CONF_MAGIC */ |
a322f70c DW |
282 | __u32 crc; |
283 | char guid[DDF_GUID_LEN]; | |
284 | __u32 timestamp; | |
285 | __u32 seqnum; | |
286 | __u8 pad0[24]; | |
287 | __u16 prim_elmnt_count; | |
288 | __u8 chunk_shift; /* 0 == 512, 1==1024 etc */ | |
289 | __u8 prl; | |
290 | __u8 rlq; | |
291 | __u8 sec_elmnt_count; | |
292 | __u8 sec_elmnt_seq; | |
293 | __u8 srl; | |
598f0d58 NB |
294 | __u64 blocks; /* blocks per component could be different |
295 | * on different component devices...(only | |
296 | * for concat I hope) */ | |
297 | __u64 array_blocks; /* blocks in array */ | |
a322f70c DW |
298 | __u8 pad1[8]; |
299 | __u32 spare_refs[8]; | |
300 | __u8 cache_pol[8]; | |
301 | __u8 bg_rate; | |
302 | __u8 pad2[3]; | |
303 | __u8 pad3[52]; | |
304 | __u8 pad4[192]; | |
305 | __u8 v0[32]; /* reserved- 0xff */ | |
306 | __u8 v1[32]; /* reserved- 0xff */ | |
307 | __u8 v2[16]; /* reserved- 0xff */ | |
308 | __u8 v3[16]; /* reserved- 0xff */ | |
309 | __u8 vendor[32]; | |
310 | __u32 phys_refnum[0]; /* refnum of each disk in sequence */ | |
311 | /*__u64 lba_offset[0]; LBA offset in each phys. Note extents in a | |
312 | bvd are always the same size */ | |
313 | }; | |
314 | ||
315 | /* vd_config.cache_pol[7] is a bitmap */ | |
316 | #define DDF_cache_writeback 1 /* else writethrough */ | |
317 | #define DDF_cache_wadaptive 2 /* only applies if writeback */ | |
318 | #define DDF_cache_readahead 4 | |
319 | #define DDF_cache_radaptive 8 /* only if doing read-ahead */ | |
320 | #define DDF_cache_ifnobatt 16 /* even to write cache if battery is poor */ | |
321 | #define DDF_cache_wallowed 32 /* enable write caching */ | |
322 | #define DDF_cache_rallowed 64 /* enable read caching */ | |
323 | ||
324 | struct spare_assign { | |
88c164f4 | 325 | __u32 magic; /* DDF_SPARE_ASSIGN_MAGIC */ |
a322f70c DW |
326 | __u32 crc; |
327 | __u32 timestamp; | |
328 | __u8 reserved[7]; | |
329 | __u8 type; | |
330 | __u16 populated; /* SAEs used */ | |
331 | __u16 max; /* max SAEs */ | |
332 | __u8 pad[8]; | |
333 | struct spare_assign_entry { | |
334 | char guid[DDF_GUID_LEN]; | |
335 | __u16 secondary_element; | |
336 | __u8 pad[6]; | |
337 | } spare_ents[0]; | |
338 | }; | |
339 | /* spare_assign.type is a bitmap */ | |
340 | #define DDF_spare_dedicated 0x1 /* else global */ | |
341 | #define DDF_spare_revertible 0x2 /* else committable */ | |
342 | #define DDF_spare_active 0x4 /* else not active */ | |
343 | #define DDF_spare_affinity 0x8 /* enclosure affinity */ | |
344 | ||
345 | /* The data_section contents - local scope */ | |
346 | struct disk_data { | |
88c164f4 | 347 | __u32 magic; /* DDF_PHYS_DATA_MAGIC */ |
a322f70c DW |
348 | __u32 crc; |
349 | char guid[DDF_GUID_LEN]; | |
350 | __u32 refnum; /* crc of some magic drive data ... */ | |
351 | __u8 forced_ref; /* set when above was not result of magic */ | |
352 | __u8 forced_guid; /* set if guid was forced rather than magic */ | |
353 | __u8 vendor[32]; | |
354 | __u8 pad[442]; | |
355 | }; | |
356 | ||
357 | /* bbm_section content */ | |
358 | struct bad_block_log { | |
359 | __u32 magic; | |
360 | __u32 crc; | |
361 | __u16 entry_count; | |
362 | __u32 spare_count; | |
363 | __u8 pad[10]; | |
364 | __u64 first_spare; | |
365 | struct mapped_block { | |
366 | __u64 defective_start; | |
367 | __u32 replacement_start; | |
368 | __u16 remap_count; | |
369 | __u8 pad[2]; | |
370 | } entries[0]; | |
371 | }; | |
372 | ||
373 | /* Struct for internally holding ddf structures */ | |
374 | /* The DDF structure stored on each device is potentially | |
375 | * quite different, as some data is global and some is local. | |
376 | * The global data is: | |
377 | * - ddf header | |
378 | * - controller_data | |
379 | * - Physical disk records | |
380 | * - Virtual disk records | |
381 | * The local data is: | |
382 | * - Configuration records | |
383 | * - Physical Disk data section | |
384 | * ( and Bad block and vendor which I don't care about yet). | |
385 | * | |
386 | * The local data is parsed into separate lists as it is read | |
387 | * and reconstructed for writing. This means that we only need | |
388 | * to make config changes once and they are automatically | |
389 | * propagated to all devices. | |
390 | * Note that the ddf_super has space of the conf and disk data | |
391 | * for this disk and also for a list of all such data. | |
392 | * The list is only used for the superblock that is being | |
393 | * built in Create or Assemble to describe the whole array. | |
394 | */ | |
395 | struct ddf_super { | |
6416d527 | 396 | struct ddf_header anchor, primary, secondary; |
a322f70c | 397 | struct ddf_controller_data controller; |
6416d527 | 398 | struct ddf_header *active; |
a322f70c DW |
399 | struct phys_disk *phys; |
400 | struct virtual_disk *virt; | |
401 | int pdsize, vdsize; | |
f21e18ca | 402 | unsigned int max_part, mppe, conf_rec_len; |
d2ca6449 | 403 | int currentdev; |
18a2f463 | 404 | int updates_pending; |
a322f70c | 405 | struct vcl { |
6416d527 NB |
406 | union { |
407 | char space[512]; | |
408 | struct { | |
409 | struct vcl *next; | |
410 | __u64 *lba_offset; /* location in 'conf' of | |
411 | * the lba table */ | |
f21e18ca | 412 | unsigned int vcnum; /* index into ->virt */ |
8ec5d685 | 413 | struct vd_config **other_bvds; |
6416d527 NB |
414 | __u64 *block_sizes; /* NULL if all the same */ |
415 | }; | |
416 | }; | |
a322f70c | 417 | struct vd_config conf; |
d2ca6449 | 418 | } *conflist, *currentconf; |
a322f70c | 419 | struct dl { |
6416d527 NB |
420 | union { |
421 | char space[512]; | |
422 | struct { | |
423 | struct dl *next; | |
424 | int major, minor; | |
425 | char *devname; | |
426 | int fd; | |
427 | unsigned long long size; /* sectors */ | |
097bcf00 | 428 | unsigned long long primary_lba; /* sectors */ |
429 | unsigned long long secondary_lba; /* sectors */ | |
430 | unsigned long long workspace_lba; /* sectors */ | |
6416d527 NB |
431 | int pdnum; /* index in ->phys */ |
432 | struct spare_assign *spare; | |
8592f29d N |
433 | void *mdupdate; /* hold metadata update */ |
434 | ||
435 | /* These fields used by auto-layout */ | |
436 | int raiddisk; /* slot to fill in autolayout */ | |
437 | __u64 esize; | |
6416d527 NB |
438 | }; |
439 | }; | |
a322f70c | 440 | struct disk_data disk; |
b2280677 | 441 | struct vcl *vlist[0]; /* max_part in size */ |
2cc2983d | 442 | } *dlist, *add_list; |
a322f70c DW |
443 | }; |
444 | ||
445 | #ifndef offsetof | |
446 | #define offsetof(t,f) ((size_t)&(((t*)0)->f)) | |
447 | #endif | |
448 | ||
7d5a7ff3 | 449 | #if DEBUG |
fb9d0acb | 450 | static int all_ff(const char *guid); |
7d5a7ff3 | 451 | static void pr_state(struct ddf_super *ddf, const char *msg) |
452 | { | |
453 | unsigned int i; | |
454 | dprintf("%s/%s: ", __func__, msg); | |
455 | for (i = 0; i < __be16_to_cpu(ddf->active->max_vd_entries); i++) { | |
456 | if (all_ff(ddf->virt->entries[i].guid)) | |
457 | continue; | |
458 | dprintf("%u(s=%02x i=%02x) ", i, | |
459 | ddf->virt->entries[i].state, | |
460 | ddf->virt->entries[i].init_state); | |
461 | } | |
462 | dprintf("\n"); | |
463 | } | |
464 | #else | |
465 | static void pr_state(const struct ddf_super *ddf, const char *msg) {} | |
466 | #endif | |
467 | ||
468 | #define ddf_set_updates_pending(x) \ | |
469 | do { (x)->updates_pending = 1; pr_state(x, __func__); } while (0) | |
470 | ||
f21e18ca | 471 | static unsigned int calc_crc(void *buf, int len) |
a322f70c DW |
472 | { |
473 | /* crcs are always at the same place as in the ddf_header */ | |
474 | struct ddf_header *ddf = buf; | |
475 | __u32 oldcrc = ddf->crc; | |
476 | __u32 newcrc; | |
477 | ddf->crc = 0xffffffff; | |
478 | ||
479 | newcrc = crc32(0, buf, len); | |
480 | ddf->crc = oldcrc; | |
4abe6b70 N |
481 | /* The crc is store (like everything) bigendian, so convert |
482 | * here for simplicity | |
483 | */ | |
484 | return __cpu_to_be32(newcrc); | |
a322f70c DW |
485 | } |
486 | ||
487 | static int load_ddf_header(int fd, unsigned long long lba, | |
488 | unsigned long long size, | |
489 | int type, | |
490 | struct ddf_header *hdr, struct ddf_header *anchor) | |
491 | { | |
492 | /* read a ddf header (primary or secondary) from fd/lba | |
493 | * and check that it is consistent with anchor | |
494 | * Need to check: | |
495 | * magic, crc, guid, rev, and LBA's header_type, and | |
496 | * everything after header_type must be the same | |
497 | */ | |
498 | if (lba >= size-1) | |
499 | return 0; | |
500 | ||
501 | if (lseek64(fd, lba<<9, 0) < 0) | |
502 | return 0; | |
503 | ||
504 | if (read(fd, hdr, 512) != 512) | |
505 | return 0; | |
506 | ||
507 | if (hdr->magic != DDF_HEADER_MAGIC) | |
508 | return 0; | |
509 | if (calc_crc(hdr, 512) != hdr->crc) | |
510 | return 0; | |
511 | if (memcmp(anchor->guid, hdr->guid, DDF_GUID_LEN) != 0 || | |
512 | memcmp(anchor->revision, hdr->revision, 8) != 0 || | |
513 | anchor->primary_lba != hdr->primary_lba || | |
514 | anchor->secondary_lba != hdr->secondary_lba || | |
515 | hdr->type != type || | |
516 | memcmp(anchor->pad2, hdr->pad2, 512 - | |
517 | offsetof(struct ddf_header, pad2)) != 0) | |
518 | return 0; | |
519 | ||
520 | /* Looks good enough to me... */ | |
521 | return 1; | |
522 | } | |
523 | ||
524 | static void *load_section(int fd, struct ddf_super *super, void *buf, | |
525 | __u32 offset_be, __u32 len_be, int check) | |
526 | { | |
527 | unsigned long long offset = __be32_to_cpu(offset_be); | |
528 | unsigned long long len = __be32_to_cpu(len_be); | |
529 | int dofree = (buf == NULL); | |
530 | ||
531 | if (check) | |
532 | if (len != 2 && len != 8 && len != 32 | |
533 | && len != 128 && len != 512) | |
534 | return NULL; | |
535 | ||
536 | if (len > 1024) | |
537 | return NULL; | |
538 | if (buf) { | |
539 | /* All pre-allocated sections are a single block */ | |
540 | if (len != 1) | |
541 | return NULL; | |
3d2c4fc7 DW |
542 | } else if (posix_memalign(&buf, 512, len<<9) != 0) |
543 | buf = NULL; | |
6416d527 | 544 | |
a322f70c DW |
545 | if (!buf) |
546 | return NULL; | |
547 | ||
548 | if (super->active->type == 1) | |
549 | offset += __be64_to_cpu(super->active->primary_lba); | |
550 | else | |
551 | offset += __be64_to_cpu(super->active->secondary_lba); | |
552 | ||
f21e18ca | 553 | if ((unsigned long long)lseek64(fd, offset<<9, 0) != (offset<<9)) { |
a322f70c DW |
554 | if (dofree) |
555 | free(buf); | |
556 | return NULL; | |
557 | } | |
f21e18ca | 558 | if ((unsigned long long)read(fd, buf, len<<9) != (len<<9)) { |
a322f70c DW |
559 | if (dofree) |
560 | free(buf); | |
561 | return NULL; | |
562 | } | |
563 | return buf; | |
564 | } | |
565 | ||
566 | static int load_ddf_headers(int fd, struct ddf_super *super, char *devname) | |
567 | { | |
568 | unsigned long long dsize; | |
569 | ||
570 | get_dev_size(fd, NULL, &dsize); | |
571 | ||
572 | if (lseek64(fd, dsize-512, 0) < 0) { | |
573 | if (devname) | |
e7b84f9d N |
574 | pr_err("Cannot seek to anchor block on %s: %s\n", |
575 | devname, strerror(errno)); | |
a322f70c DW |
576 | return 1; |
577 | } | |
578 | if (read(fd, &super->anchor, 512) != 512) { | |
579 | if (devname) | |
e7b84f9d N |
580 | pr_err("Cannot read anchor block on %s: %s\n", |
581 | devname, strerror(errno)); | |
a322f70c DW |
582 | return 1; |
583 | } | |
584 | if (super->anchor.magic != DDF_HEADER_MAGIC) { | |
585 | if (devname) | |
e7b84f9d | 586 | pr_err("no DDF anchor found on %s\n", |
a322f70c DW |
587 | devname); |
588 | return 2; | |
589 | } | |
590 | if (calc_crc(&super->anchor, 512) != super->anchor.crc) { | |
591 | if (devname) | |
e7b84f9d | 592 | pr_err("bad CRC on anchor on %s\n", |
a322f70c DW |
593 | devname); |
594 | return 2; | |
595 | } | |
59e36268 NB |
596 | if (memcmp(super->anchor.revision, DDF_REVISION_0, 8) != 0 && |
597 | memcmp(super->anchor.revision, DDF_REVISION_2, 8) != 0) { | |
a322f70c | 598 | if (devname) |
e7b84f9d | 599 | pr_err("can only support super revision" |
59e36268 NB |
600 | " %.8s and earlier, not %.8s on %s\n", |
601 | DDF_REVISION_2, super->anchor.revision,devname); | |
a322f70c DW |
602 | return 2; |
603 | } | |
dbeb699a | 604 | super->active = NULL; |
a322f70c DW |
605 | if (load_ddf_header(fd, __be64_to_cpu(super->anchor.primary_lba), |
606 | dsize >> 9, 1, | |
607 | &super->primary, &super->anchor) == 0) { | |
608 | if (devname) | |
e7b84f9d N |
609 | pr_err("Failed to load primary DDF header " |
610 | "on %s\n", devname); | |
dbeb699a | 611 | } else |
612 | super->active = &super->primary; | |
a322f70c DW |
613 | if (load_ddf_header(fd, __be64_to_cpu(super->anchor.secondary_lba), |
614 | dsize >> 9, 2, | |
615 | &super->secondary, &super->anchor)) { | |
616 | if ((__be32_to_cpu(super->primary.seq) | |
617 | < __be32_to_cpu(super->secondary.seq) && | |
618 | !super->secondary.openflag) | |
619 | || (__be32_to_cpu(super->primary.seq) | |
620 | == __be32_to_cpu(super->secondary.seq) && | |
621 | super->primary.openflag && !super->secondary.openflag) | |
dbeb699a | 622 | || super->active == NULL |
a322f70c DW |
623 | ) |
624 | super->active = &super->secondary; | |
dbeb699a | 625 | } else if (devname) |
626 | pr_err("Failed to load secondary DDF header on %s\n", | |
627 | devname); | |
628 | if (super->active == NULL) | |
629 | return 2; | |
a322f70c DW |
630 | return 0; |
631 | } | |
632 | ||
633 | static int load_ddf_global(int fd, struct ddf_super *super, char *devname) | |
634 | { | |
635 | void *ok; | |
636 | ok = load_section(fd, super, &super->controller, | |
637 | super->active->controller_section_offset, | |
638 | super->active->controller_section_length, | |
639 | 0); | |
640 | super->phys = load_section(fd, super, NULL, | |
641 | super->active->phys_section_offset, | |
642 | super->active->phys_section_length, | |
643 | 1); | |
644 | super->pdsize = __be32_to_cpu(super->active->phys_section_length) * 512; | |
645 | ||
646 | super->virt = load_section(fd, super, NULL, | |
647 | super->active->virt_section_offset, | |
648 | super->active->virt_section_length, | |
649 | 1); | |
650 | super->vdsize = __be32_to_cpu(super->active->virt_section_length) * 512; | |
651 | if (!ok || | |
652 | !super->phys || | |
653 | !super->virt) { | |
654 | free(super->phys); | |
655 | free(super->virt); | |
a2349791 NB |
656 | super->phys = NULL; |
657 | super->virt = NULL; | |
a322f70c DW |
658 | return 2; |
659 | } | |
660 | super->conflist = NULL; | |
661 | super->dlist = NULL; | |
8c3b8c2c NB |
662 | |
663 | super->max_part = __be16_to_cpu(super->active->max_partitions); | |
664 | super->mppe = __be16_to_cpu(super->active->max_primary_element_entries); | |
665 | super->conf_rec_len = __be16_to_cpu(super->active->config_record_len); | |
a322f70c DW |
666 | return 0; |
667 | } | |
668 | ||
3dc821b0 | 669 | static void add_other_bvd(struct vcl *vcl, struct vd_config *vd, |
670 | unsigned int len) | |
671 | { | |
672 | int i; | |
673 | for (i = 0; i < vcl->conf.sec_elmnt_count-1; i++) | |
674 | if (vcl->other_bvds[i] != NULL && | |
675 | vcl->other_bvds[i]->sec_elmnt_seq == vd->sec_elmnt_seq) | |
676 | break; | |
677 | ||
678 | if (i < vcl->conf.sec_elmnt_count-1) { | |
679 | if (vd->seqnum <= vcl->other_bvds[i]->seqnum) | |
680 | return; | |
681 | } else { | |
682 | for (i = 0; i < vcl->conf.sec_elmnt_count-1; i++) | |
683 | if (vcl->other_bvds[i] == NULL) | |
684 | break; | |
685 | if (i == vcl->conf.sec_elmnt_count-1) { | |
686 | pr_err("no space for sec level config %u, count is %u\n", | |
687 | vd->sec_elmnt_seq, vcl->conf.sec_elmnt_count); | |
688 | return; | |
689 | } | |
690 | if (posix_memalign((void **)&vcl->other_bvds[i], 512, len) | |
691 | != 0) { | |
692 | pr_err("%s could not allocate vd buf\n", __func__); | |
693 | return; | |
694 | } | |
695 | } | |
696 | memcpy(vcl->other_bvds[i], vd, len); | |
697 | } | |
698 | ||
a322f70c DW |
699 | static int load_ddf_local(int fd, struct ddf_super *super, |
700 | char *devname, int keep) | |
701 | { | |
702 | struct dl *dl; | |
703 | struct stat stb; | |
704 | char *conf; | |
f21e18ca N |
705 | unsigned int i; |
706 | unsigned int confsec; | |
b2280677 | 707 | int vnum; |
f21e18ca | 708 | unsigned int max_virt_disks = __be16_to_cpu(super->active->max_vd_entries); |
d2ca6449 | 709 | unsigned long long dsize; |
a322f70c DW |
710 | |
711 | /* First the local disk info */ | |
3d2c4fc7 | 712 | if (posix_memalign((void**)&dl, 512, |
6416d527 | 713 | sizeof(*dl) + |
3d2c4fc7 | 714 | (super->max_part) * sizeof(dl->vlist[0])) != 0) { |
e7b84f9d | 715 | pr_err("%s could not allocate disk info buffer\n", |
3d2c4fc7 DW |
716 | __func__); |
717 | return 1; | |
718 | } | |
a322f70c DW |
719 | |
720 | load_section(fd, super, &dl->disk, | |
721 | super->active->data_section_offset, | |
722 | super->active->data_section_length, | |
723 | 0); | |
503975b9 | 724 | dl->devname = devname ? xstrdup(devname) : NULL; |
598f0d58 | 725 | |
a322f70c DW |
726 | fstat(fd, &stb); |
727 | dl->major = major(stb.st_rdev); | |
728 | dl->minor = minor(stb.st_rdev); | |
729 | dl->next = super->dlist; | |
730 | dl->fd = keep ? fd : -1; | |
d2ca6449 NB |
731 | |
732 | dl->size = 0; | |
733 | if (get_dev_size(fd, devname, &dsize)) | |
734 | dl->size = dsize >> 9; | |
097bcf00 | 735 | /* If the disks have different sizes, the LBAs will differ |
736 | * between phys disks. | |
737 | * At this point here, the values in super->active must be valid | |
738 | * for this phys disk. */ | |
739 | dl->primary_lba = super->active->primary_lba; | |
740 | dl->secondary_lba = super->active->secondary_lba; | |
741 | dl->workspace_lba = super->active->workspace_lba; | |
b2280677 | 742 | dl->spare = NULL; |
f21e18ca | 743 | for (i = 0 ; i < super->max_part ; i++) |
a322f70c DW |
744 | dl->vlist[i] = NULL; |
745 | super->dlist = dl; | |
59e36268 | 746 | dl->pdnum = -1; |
f21e18ca | 747 | for (i = 0; i < __be16_to_cpu(super->active->max_pd_entries); i++) |
5575e7d9 NB |
748 | if (memcmp(super->phys->entries[i].guid, |
749 | dl->disk.guid, DDF_GUID_LEN) == 0) | |
750 | dl->pdnum = i; | |
751 | ||
a322f70c DW |
752 | /* Now the config list. */ |
753 | /* 'conf' is an array of config entries, some of which are | |
754 | * probably invalid. Those which are good need to be copied into | |
755 | * the conflist | |
756 | */ | |
a322f70c DW |
757 | |
758 | conf = load_section(fd, super, NULL, | |
759 | super->active->config_section_offset, | |
760 | super->active->config_section_length, | |
761 | 0); | |
762 | ||
b2280677 | 763 | vnum = 0; |
e223334f N |
764 | for (confsec = 0; |
765 | confsec < __be32_to_cpu(super->active->config_section_length); | |
766 | confsec += super->conf_rec_len) { | |
a322f70c | 767 | struct vd_config *vd = |
e223334f | 768 | (struct vd_config *)((char*)conf + confsec*512); |
a322f70c DW |
769 | struct vcl *vcl; |
770 | ||
b2280677 NB |
771 | if (vd->magic == DDF_SPARE_ASSIGN_MAGIC) { |
772 | if (dl->spare) | |
773 | continue; | |
3d2c4fc7 DW |
774 | if (posix_memalign((void**)&dl->spare, 512, |
775 | super->conf_rec_len*512) != 0) { | |
e7b84f9d N |
776 | pr_err("%s could not allocate spare info buf\n", |
777 | __func__); | |
3d2c4fc7 DW |
778 | return 1; |
779 | } | |
613b0d17 | 780 | |
b2280677 NB |
781 | memcpy(dl->spare, vd, super->conf_rec_len*512); |
782 | continue; | |
783 | } | |
a322f70c DW |
784 | if (vd->magic != DDF_VD_CONF_MAGIC) |
785 | continue; | |
786 | for (vcl = super->conflist; vcl; vcl = vcl->next) { | |
787 | if (memcmp(vcl->conf.guid, | |
788 | vd->guid, DDF_GUID_LEN) == 0) | |
789 | break; | |
790 | } | |
791 | ||
792 | if (vcl) { | |
b2280677 | 793 | dl->vlist[vnum++] = vcl; |
3dc821b0 | 794 | if (vcl->other_bvds != NULL && |
795 | vcl->conf.sec_elmnt_seq != vd->sec_elmnt_seq) { | |
796 | add_other_bvd(vcl, vd, super->conf_rec_len*512); | |
797 | continue; | |
798 | } | |
a322f70c DW |
799 | if (__be32_to_cpu(vd->seqnum) <= |
800 | __be32_to_cpu(vcl->conf.seqnum)) | |
801 | continue; | |
59e36268 | 802 | } else { |
3d2c4fc7 | 803 | if (posix_memalign((void**)&vcl, 512, |
6416d527 | 804 | (super->conf_rec_len*512 + |
3d2c4fc7 | 805 | offsetof(struct vcl, conf))) != 0) { |
e7b84f9d N |
806 | pr_err("%s could not allocate vcl buf\n", |
807 | __func__); | |
3d2c4fc7 DW |
808 | return 1; |
809 | } | |
a322f70c | 810 | vcl->next = super->conflist; |
59e36268 | 811 | vcl->block_sizes = NULL; /* FIXME not for CONCAT */ |
8ec5d685 | 812 | if (vd->sec_elmnt_count > 1) |
813 | vcl->other_bvds = | |
814 | xcalloc(vd->sec_elmnt_count - 1, | |
815 | sizeof(struct vd_config *)); | |
816 | else | |
817 | vcl->other_bvds = NULL; | |
a322f70c | 818 | super->conflist = vcl; |
b2280677 | 819 | dl->vlist[vnum++] = vcl; |
a322f70c | 820 | } |
8c3b8c2c | 821 | memcpy(&vcl->conf, vd, super->conf_rec_len*512); |
a322f70c | 822 | vcl->lba_offset = (__u64*) |
8c3b8c2c | 823 | &vcl->conf.phys_refnum[super->mppe]; |
59e36268 NB |
824 | |
825 | for (i=0; i < max_virt_disks ; i++) | |
826 | if (memcmp(super->virt->entries[i].guid, | |
827 | vcl->conf.guid, DDF_GUID_LEN)==0) | |
828 | break; | |
829 | if (i < max_virt_disks) | |
830 | vcl->vcnum = i; | |
a322f70c DW |
831 | } |
832 | free(conf); | |
833 | ||
834 | return 0; | |
835 | } | |
836 | ||
837 | #ifndef MDASSEMBLE | |
838 | static int load_super_ddf_all(struct supertype *st, int fd, | |
e1902a7b | 839 | void **sbp, char *devname); |
a322f70c | 840 | #endif |
37424f13 DW |
841 | |
842 | static void free_super_ddf(struct supertype *st); | |
843 | ||
a322f70c DW |
844 | static int load_super_ddf(struct supertype *st, int fd, |
845 | char *devname) | |
846 | { | |
847 | unsigned long long dsize; | |
848 | struct ddf_super *super; | |
849 | int rv; | |
850 | ||
a322f70c DW |
851 | if (get_dev_size(fd, devname, &dsize) == 0) |
852 | return 1; | |
853 | ||
b31df436 | 854 | if (!st->ignore_hw_compat && test_partition(fd)) |
691c6ee1 N |
855 | /* DDF is not allowed on partitions */ |
856 | return 1; | |
857 | ||
a322f70c DW |
858 | /* 32M is a lower bound */ |
859 | if (dsize <= 32*1024*1024) { | |
97320d7c | 860 | if (devname) |
e7b84f9d N |
861 | pr_err("%s is too small for ddf: " |
862 | "size is %llu sectors.\n", | |
863 | devname, dsize>>9); | |
97320d7c | 864 | return 1; |
a322f70c DW |
865 | } |
866 | if (dsize & 511) { | |
97320d7c | 867 | if (devname) |
e7b84f9d N |
868 | pr_err("%s is an odd size for ddf: " |
869 | "size is %llu bytes.\n", | |
870 | devname, dsize); | |
97320d7c | 871 | return 1; |
a322f70c DW |
872 | } |
873 | ||
37424f13 DW |
874 | free_super_ddf(st); |
875 | ||
6416d527 | 876 | if (posix_memalign((void**)&super, 512, sizeof(*super))!= 0) { |
e7b84f9d | 877 | pr_err("malloc of %zu failed.\n", |
a322f70c DW |
878 | sizeof(*super)); |
879 | return 1; | |
880 | } | |
a2349791 | 881 | memset(super, 0, sizeof(*super)); |
a322f70c DW |
882 | |
883 | rv = load_ddf_headers(fd, super, devname); | |
884 | if (rv) { | |
885 | free(super); | |
886 | return rv; | |
887 | } | |
888 | ||
889 | /* Have valid headers and have chosen the best. Let's read in the rest*/ | |
890 | ||
891 | rv = load_ddf_global(fd, super, devname); | |
892 | ||
893 | if (rv) { | |
894 | if (devname) | |
e7b84f9d N |
895 | pr_err("Failed to load all information " |
896 | "sections on %s\n", devname); | |
a322f70c DW |
897 | free(super); |
898 | return rv; | |
899 | } | |
900 | ||
3d2c4fc7 DW |
901 | rv = load_ddf_local(fd, super, devname, 0); |
902 | ||
903 | if (rv) { | |
904 | if (devname) | |
e7b84f9d N |
905 | pr_err("Failed to load all information " |
906 | "sections on %s\n", devname); | |
3d2c4fc7 DW |
907 | free(super); |
908 | return rv; | |
909 | } | |
a322f70c DW |
910 | |
911 | /* Should possibly check the sections .... */ | |
912 | ||
913 | st->sb = super; | |
914 | if (st->ss == NULL) { | |
915 | st->ss = &super_ddf; | |
916 | st->minor_version = 0; | |
917 | st->max_devs = 512; | |
918 | } | |
919 | return 0; | |
920 | ||
921 | } | |
922 | ||
923 | static void free_super_ddf(struct supertype *st) | |
924 | { | |
925 | struct ddf_super *ddf = st->sb; | |
926 | if (ddf == NULL) | |
927 | return; | |
928 | free(ddf->phys); | |
929 | free(ddf->virt); | |
930 | while (ddf->conflist) { | |
931 | struct vcl *v = ddf->conflist; | |
932 | ddf->conflist = v->next; | |
59e36268 NB |
933 | if (v->block_sizes) |
934 | free(v->block_sizes); | |
3dc821b0 | 935 | if (v->other_bvds) { |
936 | int i; | |
937 | for (i = 0; i < v->conf.sec_elmnt_count-1; i++) | |
938 | if (v->other_bvds[i] != NULL) | |
939 | free(v->other_bvds[i]); | |
8ec5d685 | 940 | free(v->other_bvds); |
3dc821b0 | 941 | } |
a322f70c DW |
942 | free(v); |
943 | } | |
944 | while (ddf->dlist) { | |
945 | struct dl *d = ddf->dlist; | |
946 | ddf->dlist = d->next; | |
947 | if (d->fd >= 0) | |
948 | close(d->fd); | |
b2280677 NB |
949 | if (d->spare) |
950 | free(d->spare); | |
a322f70c DW |
951 | free(d); |
952 | } | |
8a38cb04 N |
953 | while (ddf->add_list) { |
954 | struct dl *d = ddf->add_list; | |
955 | ddf->add_list = d->next; | |
956 | if (d->fd >= 0) | |
957 | close(d->fd); | |
958 | if (d->spare) | |
959 | free(d->spare); | |
960 | free(d); | |
961 | } | |
a322f70c DW |
962 | free(ddf); |
963 | st->sb = NULL; | |
964 | } | |
965 | ||
966 | static struct supertype *match_metadata_desc_ddf(char *arg) | |
967 | { | |
968 | /* 'ddf' only support containers */ | |
969 | struct supertype *st; | |
970 | if (strcmp(arg, "ddf") != 0 && | |
971 | strcmp(arg, "default") != 0 | |
972 | ) | |
973 | return NULL; | |
974 | ||
503975b9 | 975 | st = xcalloc(1, sizeof(*st)); |
a322f70c DW |
976 | st->ss = &super_ddf; |
977 | st->max_devs = 512; | |
978 | st->minor_version = 0; | |
979 | st->sb = NULL; | |
980 | return st; | |
981 | } | |
982 | ||
a322f70c DW |
983 | #ifndef MDASSEMBLE |
984 | ||
985 | static mapping_t ddf_state[] = { | |
986 | { "Optimal", 0}, | |
987 | { "Degraded", 1}, | |
988 | { "Deleted", 2}, | |
989 | { "Missing", 3}, | |
990 | { "Failed", 4}, | |
991 | { "Partially Optimal", 5}, | |
992 | { "-reserved-", 6}, | |
993 | { "-reserved-", 7}, | |
994 | { NULL, 0} | |
995 | }; | |
996 | ||
997 | static mapping_t ddf_init_state[] = { | |
998 | { "Not Initialised", 0}, | |
999 | { "QuickInit in Progress", 1}, | |
1000 | { "Fully Initialised", 2}, | |
1001 | { "*UNKNOWN*", 3}, | |
1002 | { NULL, 0} | |
1003 | }; | |
1004 | static mapping_t ddf_access[] = { | |
1005 | { "Read/Write", 0}, | |
1006 | { "Reserved", 1}, | |
1007 | { "Read Only", 2}, | |
1008 | { "Blocked (no access)", 3}, | |
1009 | { NULL ,0} | |
1010 | }; | |
1011 | ||
1012 | static mapping_t ddf_level[] = { | |
1013 | { "RAID0", DDF_RAID0}, | |
1014 | { "RAID1", DDF_RAID1}, | |
1015 | { "RAID3", DDF_RAID3}, | |
1016 | { "RAID4", DDF_RAID4}, | |
1017 | { "RAID5", DDF_RAID5}, | |
1018 | { "RAID1E",DDF_RAID1E}, | |
1019 | { "JBOD", DDF_JBOD}, | |
1020 | { "CONCAT",DDF_CONCAT}, | |
1021 | { "RAID5E",DDF_RAID5E}, | |
1022 | { "RAID5EE",DDF_RAID5EE}, | |
1023 | { "RAID6", DDF_RAID6}, | |
1024 | { NULL, 0} | |
1025 | }; | |
1026 | static mapping_t ddf_sec_level[] = { | |
1027 | { "Striped", DDF_2STRIPED}, | |
1028 | { "Mirrored", DDF_2MIRRORED}, | |
1029 | { "Concat", DDF_2CONCAT}, | |
1030 | { "Spanned", DDF_2SPANNED}, | |
1031 | { NULL, 0} | |
1032 | }; | |
1033 | #endif | |
1034 | ||
1035 | struct num_mapping { | |
1036 | int num1, num2; | |
1037 | }; | |
1038 | static struct num_mapping ddf_level_num[] = { | |
1039 | { DDF_RAID0, 0 }, | |
1040 | { DDF_RAID1, 1 }, | |
1041 | { DDF_RAID3, LEVEL_UNSUPPORTED }, | |
60f18132 NB |
1042 | { DDF_RAID4, 4 }, |
1043 | { DDF_RAID5, 5 }, | |
a322f70c DW |
1044 | { DDF_RAID1E, LEVEL_UNSUPPORTED }, |
1045 | { DDF_JBOD, LEVEL_UNSUPPORTED }, | |
1046 | { DDF_CONCAT, LEVEL_LINEAR }, | |
1047 | { DDF_RAID5E, LEVEL_UNSUPPORTED }, | |
1048 | { DDF_RAID5EE, LEVEL_UNSUPPORTED }, | |
1049 | { DDF_RAID6, 6}, | |
1050 | { MAXINT, MAXINT } | |
1051 | }; | |
1052 | ||
1053 | static int map_num1(struct num_mapping *map, int num) | |
1054 | { | |
1055 | int i; | |
1056 | for (i=0 ; map[i].num1 != MAXINT; i++) | |
1057 | if (map[i].num1 == num) | |
1058 | break; | |
1059 | return map[i].num2; | |
1060 | } | |
1061 | ||
fb9d0acb | 1062 | static int all_ff(const char *guid) |
42dc2744 N |
1063 | { |
1064 | int i; | |
1065 | for (i = 0; i < DDF_GUID_LEN; i++) | |
1066 | if (guid[i] != (char)0xff) | |
1067 | return 0; | |
1068 | return 1; | |
1069 | } | |
1070 | ||
a322f70c DW |
1071 | #ifndef MDASSEMBLE |
1072 | static void print_guid(char *guid, int tstamp) | |
1073 | { | |
1074 | /* A GUIDs are part (or all) ASCII and part binary. | |
1075 | * They tend to be space padded. | |
59e36268 NB |
1076 | * We print the GUID in HEX, then in parentheses add |
1077 | * any initial ASCII sequence, and a possible | |
1078 | * time stamp from bytes 16-19 | |
a322f70c DW |
1079 | */ |
1080 | int l = DDF_GUID_LEN; | |
1081 | int i; | |
59e36268 NB |
1082 | |
1083 | for (i=0 ; i<DDF_GUID_LEN ; i++) { | |
1084 | if ((i&3)==0 && i != 0) printf(":"); | |
1085 | printf("%02X", guid[i]&255); | |
1086 | } | |
1087 | ||
cfccea8c | 1088 | printf("\n ("); |
a322f70c DW |
1089 | while (l && guid[l-1] == ' ') |
1090 | l--; | |
1091 | for (i=0 ; i<l ; i++) { | |
1092 | if (guid[i] >= 0x20 && guid[i] < 0x7f) | |
1093 | fputc(guid[i], stdout); | |
1094 | else | |
59e36268 | 1095 | break; |
a322f70c DW |
1096 | } |
1097 | if (tstamp) { | |
1098 | time_t then = __be32_to_cpu(*(__u32*)(guid+16)) + DECADE; | |
1099 | char tbuf[100]; | |
1100 | struct tm *tm; | |
1101 | tm = localtime(&then); | |
59e36268 | 1102 | strftime(tbuf, 100, " %D %T",tm); |
a322f70c DW |
1103 | fputs(tbuf, stdout); |
1104 | } | |
59e36268 | 1105 | printf(")"); |
a322f70c DW |
1106 | } |
1107 | ||
1108 | static void examine_vd(int n, struct ddf_super *sb, char *guid) | |
1109 | { | |
8c3b8c2c | 1110 | int crl = sb->conf_rec_len; |
a322f70c DW |
1111 | struct vcl *vcl; |
1112 | ||
1113 | for (vcl = sb->conflist ; vcl ; vcl = vcl->next) { | |
f21e18ca | 1114 | unsigned int i; |
a322f70c DW |
1115 | struct vd_config *vc = &vcl->conf; |
1116 | ||
1117 | if (calc_crc(vc, crl*512) != vc->crc) | |
1118 | continue; | |
1119 | if (memcmp(vc->guid, guid, DDF_GUID_LEN) != 0) | |
1120 | continue; | |
1121 | ||
1122 | /* Ok, we know about this VD, let's give more details */ | |
b06e3095 | 1123 | printf(" Raid Devices[%d] : %d (", n, |
a322f70c | 1124 | __be16_to_cpu(vc->prim_elmnt_count)); |
f21e18ca | 1125 | for (i = 0; i < __be16_to_cpu(vc->prim_elmnt_count); i++) { |
b06e3095 N |
1126 | int j; |
1127 | int cnt = __be16_to_cpu(sb->phys->used_pdes); | |
1128 | for (j=0; j<cnt; j++) | |
1129 | if (vc->phys_refnum[i] == sb->phys->entries[j].refnum) | |
1130 | break; | |
1131 | if (i) printf(" "); | |
1132 | if (j < cnt) | |
1133 | printf("%d", j); | |
1134 | else | |
1135 | printf("--"); | |
1136 | } | |
1137 | printf(")\n"); | |
1138 | if (vc->chunk_shift != 255) | |
613b0d17 N |
1139 | printf(" Chunk Size[%d] : %d sectors\n", n, |
1140 | 1 << vc->chunk_shift); | |
a322f70c DW |
1141 | printf(" Raid Level[%d] : %s\n", n, |
1142 | map_num(ddf_level, vc->prl)?:"-unknown-"); | |
1143 | if (vc->sec_elmnt_count != 1) { | |
1144 | printf(" Secondary Position[%d] : %d of %d\n", n, | |
1145 | vc->sec_elmnt_seq, vc->sec_elmnt_count); | |
1146 | printf(" Secondary Level[%d] : %s\n", n, | |
1147 | map_num(ddf_sec_level, vc->srl) ?: "-unknown-"); | |
1148 | } | |
1149 | printf(" Device Size[%d] : %llu\n", n, | |
c9b6907b | 1150 | (unsigned long long)__be64_to_cpu(vc->blocks)/2); |
a322f70c | 1151 | printf(" Array Size[%d] : %llu\n", n, |
c9b6907b | 1152 | (unsigned long long)__be64_to_cpu(vc->array_blocks)/2); |
a322f70c DW |
1153 | } |
1154 | } | |
1155 | ||
1156 | static void examine_vds(struct ddf_super *sb) | |
1157 | { | |
1158 | int cnt = __be16_to_cpu(sb->virt->populated_vdes); | |
fb9d0acb | 1159 | unsigned int i; |
a322f70c DW |
1160 | printf(" Virtual Disks : %d\n", cnt); |
1161 | ||
fb9d0acb | 1162 | for (i = 0; i < __be16_to_cpu(sb->virt->max_vdes); i++) { |
a322f70c | 1163 | struct virtual_entry *ve = &sb->virt->entries[i]; |
fb9d0acb | 1164 | if (all_ff(ve->guid)) |
1165 | continue; | |
b06e3095 | 1166 | printf("\n"); |
a322f70c DW |
1167 | printf(" VD GUID[%d] : ", i); print_guid(ve->guid, 1); |
1168 | printf("\n"); | |
1169 | printf(" unit[%d] : %d\n", i, __be16_to_cpu(ve->unit)); | |
1170 | printf(" state[%d] : %s, %s%s\n", i, | |
1171 | map_num(ddf_state, ve->state & 7), | |
1172 | (ve->state & 8) ? "Morphing, ": "", | |
1173 | (ve->state & 16)? "Not Consistent" : "Consistent"); | |
1174 | printf(" init state[%d] : %s\n", i, | |
1175 | map_num(ddf_init_state, ve->init_state&3)); | |
1176 | printf(" access[%d] : %s\n", i, | |
1177 | map_num(ddf_access, (ve->init_state>>6) & 3)); | |
1178 | printf(" Name[%d] : %.16s\n", i, ve->name); | |
1179 | examine_vd(i, sb, ve->guid); | |
1180 | } | |
1181 | if (cnt) printf("\n"); | |
1182 | } | |
1183 | ||
1184 | static void examine_pds(struct ddf_super *sb) | |
1185 | { | |
1186 | int cnt = __be16_to_cpu(sb->phys->used_pdes); | |
1187 | int i; | |
1188 | struct dl *dl; | |
1189 | printf(" Physical Disks : %d\n", cnt); | |
962371a5 | 1190 | printf(" Number RefNo Size Device Type/State\n"); |
a322f70c DW |
1191 | |
1192 | for (i=0 ; i<cnt ; i++) { | |
1193 | struct phys_disk_entry *pd = &sb->phys->entries[i]; | |
1194 | int type = __be16_to_cpu(pd->type); | |
1195 | int state = __be16_to_cpu(pd->state); | |
1196 | ||
b06e3095 N |
1197 | //printf(" PD GUID[%d] : ", i); print_guid(pd->guid, 0); |
1198 | //printf("\n"); | |
1199 | printf(" %3d %08x ", i, | |
a322f70c | 1200 | __be32_to_cpu(pd->refnum)); |
613b0d17 | 1201 | printf("%8lluK ", |
c9b6907b | 1202 | (unsigned long long)__be64_to_cpu(pd->config_size)>>1); |
b06e3095 N |
1203 | for (dl = sb->dlist; dl ; dl = dl->next) { |
1204 | if (dl->disk.refnum == pd->refnum) { | |
1205 | char *dv = map_dev(dl->major, dl->minor, 0); | |
1206 | if (dv) { | |
962371a5 | 1207 | printf("%-15s", dv); |
b06e3095 N |
1208 | break; |
1209 | } | |
1210 | } | |
1211 | } | |
1212 | if (!dl) | |
962371a5 | 1213 | printf("%15s",""); |
b06e3095 | 1214 | printf(" %s%s%s%s%s", |
a322f70c | 1215 | (type&2) ? "active":"", |
b06e3095 | 1216 | (type&4) ? "Global-Spare":"", |
a322f70c DW |
1217 | (type&8) ? "spare" : "", |
1218 | (type&16)? ", foreign" : "", | |
1219 | (type&32)? "pass-through" : ""); | |
18cb4496 N |
1220 | if (state & DDF_Failed) |
1221 | /* This over-rides these three */ | |
1222 | state &= ~(DDF_Online|DDF_Rebuilding|DDF_Transition); | |
b06e3095 | 1223 | printf("/%s%s%s%s%s%s%s", |
a322f70c DW |
1224 | (state&1)? "Online": "Offline", |
1225 | (state&2)? ", Failed": "", | |
1226 | (state&4)? ", Rebuilding": "", | |
1227 | (state&8)? ", in-transition": "", | |
b06e3095 N |
1228 | (state&16)? ", SMART-errors": "", |
1229 | (state&32)? ", Unrecovered-Read-Errors": "", | |
a322f70c | 1230 | (state&64)? ", Missing" : ""); |
a322f70c DW |
1231 | printf("\n"); |
1232 | } | |
1233 | } | |
1234 | ||
1235 | static void examine_super_ddf(struct supertype *st, char *homehost) | |
1236 | { | |
1237 | struct ddf_super *sb = st->sb; | |
1238 | ||
1239 | printf(" Magic : %08x\n", __be32_to_cpu(sb->anchor.magic)); | |
1240 | printf(" Version : %.8s\n", sb->anchor.revision); | |
598f0d58 NB |
1241 | printf("Controller GUID : "); print_guid(sb->controller.guid, 0); |
1242 | printf("\n"); | |
1243 | printf(" Container GUID : "); print_guid(sb->anchor.guid, 1); | |
a322f70c DW |
1244 | printf("\n"); |
1245 | printf(" Seq : %08x\n", __be32_to_cpu(sb->active->seq)); | |
1246 | printf(" Redundant hdr : %s\n", sb->secondary.magic == DDF_HEADER_MAGIC | |
1247 | ?"yes" : "no"); | |
1248 | examine_vds(sb); | |
1249 | examine_pds(sb); | |
1250 | } | |
1251 | ||
a5d85af7 | 1252 | static void getinfo_super_ddf(struct supertype *st, struct mdinfo *info, char *map); |
ff54de6e | 1253 | |
bedbf68a | 1254 | static void uuid_from_ddf_guid(const char *guid, int uuid[4]); |
42dc2744 | 1255 | static void uuid_from_super_ddf(struct supertype *st, int uuid[4]); |
ff54de6e | 1256 | |
bedbf68a | 1257 | static unsigned int get_vd_num_of_subarray(struct supertype *st) |
1258 | { | |
1259 | /* | |
1260 | * Figure out the VD number for this supertype. | |
1261 | * Returns DDF_CONTAINER for the container itself, | |
1262 | * and DDF_NOTFOUND on error. | |
1263 | */ | |
1264 | struct ddf_super *ddf = st->sb; | |
1265 | struct mdinfo *sra; | |
1266 | char *sub, *end; | |
1267 | unsigned int vcnum; | |
1268 | ||
1269 | if (*st->container_devnm == '\0') | |
1270 | return DDF_CONTAINER; | |
1271 | ||
1272 | sra = sysfs_read(-1, st->devnm, GET_VERSION); | |
1273 | if (!sra || sra->array.major_version != -1 || | |
1274 | sra->array.minor_version != -2 || | |
1275 | !is_subarray(sra->text_version)) | |
1276 | return DDF_NOTFOUND; | |
1277 | ||
1278 | sub = strchr(sra->text_version + 1, '/'); | |
1279 | if (sub != NULL) | |
1280 | vcnum = strtoul(sub + 1, &end, 10); | |
1281 | if (sub == NULL || *sub == '\0' || *end != '\0' || | |
1282 | vcnum >= __be16_to_cpu(ddf->active->max_vd_entries)) | |
1283 | return DDF_NOTFOUND; | |
1284 | ||
1285 | return vcnum; | |
1286 | } | |
1287 | ||
061f2c6a | 1288 | static void brief_examine_super_ddf(struct supertype *st, int verbose) |
4737ae25 N |
1289 | { |
1290 | /* We just write a generic DDF ARRAY entry | |
1291 | */ | |
1292 | struct mdinfo info; | |
1293 | char nbuf[64]; | |
a5d85af7 | 1294 | getinfo_super_ddf(st, &info, NULL); |
4737ae25 N |
1295 | fname_from_uuid(st, &info, nbuf, ':'); |
1296 | ||
1297 | printf("ARRAY metadata=ddf UUID=%s\n", nbuf + 5); | |
1298 | } | |
1299 | ||
1300 | static void brief_examine_subarrays_ddf(struct supertype *st, int verbose) | |
a322f70c DW |
1301 | { |
1302 | /* We just write a generic DDF ARRAY entry | |
a322f70c | 1303 | */ |
42dc2744 | 1304 | struct ddf_super *ddf = st->sb; |
ff54de6e | 1305 | struct mdinfo info; |
f21e18ca | 1306 | unsigned int i; |
ff54de6e | 1307 | char nbuf[64]; |
a5d85af7 | 1308 | getinfo_super_ddf(st, &info, NULL); |
ff54de6e | 1309 | fname_from_uuid(st, &info, nbuf, ':'); |
42dc2744 | 1310 | |
f21e18ca | 1311 | for (i = 0; i < __be16_to_cpu(ddf->virt->max_vdes); i++) { |
42dc2744 N |
1312 | struct virtual_entry *ve = &ddf->virt->entries[i]; |
1313 | struct vcl vcl; | |
1314 | char nbuf1[64]; | |
1315 | if (all_ff(ve->guid)) | |
1316 | continue; | |
1317 | memcpy(vcl.conf.guid, ve->guid, DDF_GUID_LEN); | |
1318 | ddf->currentconf =&vcl; | |
1319 | uuid_from_super_ddf(st, info.uuid); | |
1320 | fname_from_uuid(st, &info, nbuf1, ':'); | |
1321 | printf("ARRAY container=%s member=%d UUID=%s\n", | |
1322 | nbuf+5, i, nbuf1+5); | |
1323 | } | |
a322f70c DW |
1324 | } |
1325 | ||
bceedeec N |
1326 | static void export_examine_super_ddf(struct supertype *st) |
1327 | { | |
1328 | struct mdinfo info; | |
1329 | char nbuf[64]; | |
a5d85af7 | 1330 | getinfo_super_ddf(st, &info, NULL); |
bceedeec N |
1331 | fname_from_uuid(st, &info, nbuf, ':'); |
1332 | printf("MD_METADATA=ddf\n"); | |
1333 | printf("MD_LEVEL=container\n"); | |
1334 | printf("MD_UUID=%s\n", nbuf+5); | |
1335 | } | |
bceedeec | 1336 | |
74db60b0 N |
1337 | static int copy_metadata_ddf(struct supertype *st, int from, int to) |
1338 | { | |
1339 | void *buf; | |
1340 | unsigned long long dsize, offset; | |
1341 | int bytes; | |
1342 | struct ddf_header *ddf; | |
1343 | int written = 0; | |
1344 | ||
1345 | /* The meta consists of an anchor, a primary, and a secondary. | |
1346 | * This all lives at the end of the device. | |
1347 | * So it is easiest to find the earliest of primary and | |
1348 | * secondary, and copy everything from there. | |
1349 | * | |
1350 | * Anchor is 512 from end It contains primary_lba and secondary_lba | |
1351 | * we choose one of those | |
1352 | */ | |
1353 | ||
1354 | if (posix_memalign(&buf, 4096, 4096) != 0) | |
1355 | return 1; | |
1356 | ||
1357 | if (!get_dev_size(from, NULL, &dsize)) | |
1358 | goto err; | |
1359 | ||
1360 | if (lseek64(from, dsize-512, 0) < 0) | |
1361 | goto err; | |
1362 | if (read(from, buf, 512) != 512) | |
1363 | goto err; | |
1364 | ddf = buf; | |
1365 | if (ddf->magic != DDF_HEADER_MAGIC || | |
1366 | calc_crc(ddf, 512) != ddf->crc || | |
1367 | (memcmp(ddf->revision, DDF_REVISION_0, 8) != 0 && | |
1368 | memcmp(ddf->revision, DDF_REVISION_2, 8) != 0)) | |
1369 | goto err; | |
1370 | ||
1371 | offset = dsize - 512; | |
1372 | if ((__be64_to_cpu(ddf->primary_lba) << 9) < offset) | |
1373 | offset = __be64_to_cpu(ddf->primary_lba) << 9; | |
1374 | if ((__be64_to_cpu(ddf->secondary_lba) << 9) < offset) | |
1375 | offset = __be64_to_cpu(ddf->secondary_lba) << 9; | |
1376 | ||
1377 | bytes = dsize - offset; | |
1378 | ||
1379 | if (lseek64(from, offset, 0) < 0 || | |
1380 | lseek64(to, offset, 0) < 0) | |
1381 | goto err; | |
1382 | while (written < bytes) { | |
1383 | int n = bytes - written; | |
1384 | if (n > 4096) | |
1385 | n = 4096; | |
1386 | if (read(from, buf, n) != n) | |
1387 | goto err; | |
1388 | if (write(to, buf, n) != n) | |
1389 | goto err; | |
1390 | written += n; | |
1391 | } | |
1392 | free(buf); | |
1393 | return 0; | |
1394 | err: | |
1395 | free(buf); | |
1396 | return 1; | |
1397 | } | |
1398 | ||
a322f70c DW |
1399 | static void detail_super_ddf(struct supertype *st, char *homehost) |
1400 | { | |
1401 | /* FIXME later | |
1402 | * Could print DDF GUID | |
1403 | * Need to find which array | |
1404 | * If whole, briefly list all arrays | |
1405 | * If one, give name | |
1406 | */ | |
1407 | } | |
1408 | ||
1409 | static void brief_detail_super_ddf(struct supertype *st) | |
1410 | { | |
ff54de6e N |
1411 | struct mdinfo info; |
1412 | char nbuf[64]; | |
bedbf68a | 1413 | struct ddf_super *ddf = st->sb; |
1414 | unsigned int vcnum = get_vd_num_of_subarray(st); | |
1415 | if (vcnum == DDF_CONTAINER) | |
1416 | uuid_from_super_ddf(st, info.uuid); | |
1417 | else if (vcnum == DDF_NOTFOUND) | |
1418 | return; | |
1419 | else | |
1420 | uuid_from_ddf_guid(ddf->virt->entries[vcnum].guid, info.uuid); | |
ff54de6e N |
1421 | fname_from_uuid(st, &info, nbuf,':'); |
1422 | printf(" UUID=%s", nbuf + 5); | |
a322f70c | 1423 | } |
a322f70c DW |
1424 | #endif |
1425 | ||
1426 | static int match_home_ddf(struct supertype *st, char *homehost) | |
1427 | { | |
1428 | /* It matches 'this' host if the controller is a | |
1429 | * Linux-MD controller with vendor_data matching | |
1430 | * the hostname | |
1431 | */ | |
1432 | struct ddf_super *ddf = st->sb; | |
f21e18ca | 1433 | unsigned int len; |
d1d3482b N |
1434 | |
1435 | if (!homehost) | |
1436 | return 0; | |
1437 | len = strlen(homehost); | |
a322f70c DW |
1438 | |
1439 | return (memcmp(ddf->controller.guid, T10, 8) == 0 && | |
1440 | len < sizeof(ddf->controller.vendor_data) && | |
1441 | memcmp(ddf->controller.vendor_data, homehost,len) == 0 && | |
1442 | ddf->controller.vendor_data[len] == 0); | |
1443 | } | |
1444 | ||
0e600426 | 1445 | #ifndef MDASSEMBLE |
baba3f4e | 1446 | static int find_index_in_bvd(const struct ddf_super *ddf, |
1447 | const struct vd_config *conf, unsigned int n, | |
1448 | unsigned int *n_bvd) | |
1449 | { | |
1450 | /* | |
1451 | * Find the index of the n-th valid physical disk in this BVD | |
1452 | */ | |
1453 | unsigned int i, j; | |
1454 | for (i = 0, j = 0; i < ddf->mppe && | |
1455 | j < __be16_to_cpu(conf->prim_elmnt_count); i++) { | |
1456 | if (conf->phys_refnum[i] != 0xffffffff) { | |
1457 | if (n == j) { | |
1458 | *n_bvd = i; | |
1459 | return 1; | |
1460 | } | |
1461 | j++; | |
1462 | } | |
1463 | } | |
1464 | dprintf("%s: couldn't find BVD member %u (total %u)\n", | |
1465 | __func__, n, __be16_to_cpu(conf->prim_elmnt_count)); | |
1466 | return 0; | |
1467 | } | |
1468 | ||
1469 | static struct vd_config *find_vdcr(struct ddf_super *ddf, unsigned int inst, | |
1470 | unsigned int n, | |
1471 | unsigned int *n_bvd, struct vcl **vcl) | |
a322f70c | 1472 | { |
7a7cc504 | 1473 | struct vcl *v; |
59e36268 | 1474 | |
baba3f4e | 1475 | for (v = ddf->conflist; v; v = v->next) { |
1476 | unsigned int nsec, ibvd; | |
1477 | struct vd_config *conf; | |
1478 | if (inst != v->vcnum) | |
1479 | continue; | |
1480 | conf = &v->conf; | |
1481 | if (conf->sec_elmnt_count == 1) { | |
1482 | if (find_index_in_bvd(ddf, conf, n, n_bvd)) { | |
1483 | *vcl = v; | |
1484 | return conf; | |
1485 | } else | |
1486 | goto bad; | |
1487 | } | |
1488 | if (v->other_bvds == NULL) { | |
1489 | pr_err("%s: BUG: other_bvds is NULL, nsec=%u\n", | |
1490 | __func__, conf->sec_elmnt_count); | |
1491 | goto bad; | |
1492 | } | |
1493 | nsec = n / __be16_to_cpu(conf->prim_elmnt_count); | |
1494 | if (conf->sec_elmnt_seq != nsec) { | |
1495 | for (ibvd = 1; ibvd < conf->sec_elmnt_count; ibvd++) { | |
1496 | if (v->other_bvds[ibvd-1] == NULL) | |
1497 | continue; | |
1498 | if (v->other_bvds[ibvd-1]->sec_elmnt_seq | |
1499 | == nsec) | |
1500 | break; | |
1501 | } | |
1502 | if (ibvd == conf->sec_elmnt_count) | |
1503 | goto bad; | |
1504 | conf = v->other_bvds[ibvd-1]; | |
1505 | } | |
1506 | if (!find_index_in_bvd(ddf, conf, | |
1507 | n - nsec*conf->sec_elmnt_count, n_bvd)) | |
1508 | goto bad; | |
1509 | dprintf("%s: found disk %u as member %u in bvd %d of array %u\n" | |
1510 | , __func__, n, *n_bvd, ibvd-1, inst); | |
1511 | *vcl = v; | |
1512 | return conf; | |
1513 | } | |
1514 | bad: | |
1515 | pr_err("%s: Could't find disk %d in array %u\n", __func__, n, inst); | |
7a7cc504 NB |
1516 | return NULL; |
1517 | } | |
0e600426 | 1518 | #endif |
7a7cc504 | 1519 | |
5ec636b7 | 1520 | static int find_phys(const struct ddf_super *ddf, __u32 phys_refnum) |
7a7cc504 NB |
1521 | { |
1522 | /* Find the entry in phys_disk which has the given refnum | |
1523 | * and return it's index | |
1524 | */ | |
f21e18ca N |
1525 | unsigned int i; |
1526 | for (i = 0; i < __be16_to_cpu(ddf->phys->max_pdes); i++) | |
7a7cc504 NB |
1527 | if (ddf->phys->entries[i].refnum == phys_refnum) |
1528 | return i; | |
1529 | return -1; | |
a322f70c DW |
1530 | } |
1531 | ||
bedbf68a | 1532 | static void uuid_from_ddf_guid(const char *guid, int uuid[4]) |
1533 | { | |
1534 | char buf[20]; | |
1535 | struct sha1_ctx ctx; | |
1536 | sha1_init_ctx(&ctx); | |
1537 | sha1_process_bytes(guid, DDF_GUID_LEN, &ctx); | |
1538 | sha1_finish_ctx(&ctx, buf); | |
1539 | memcpy(uuid, buf, 4*4); | |
1540 | } | |
1541 | ||
a322f70c DW |
1542 | static void uuid_from_super_ddf(struct supertype *st, int uuid[4]) |
1543 | { | |
1544 | /* The uuid returned here is used for: | |
1545 | * uuid to put into bitmap file (Create, Grow) | |
1546 | * uuid for backup header when saving critical section (Grow) | |
1547 | * comparing uuids when re-adding a device into an array | |
51006d85 N |
1548 | * In these cases the uuid required is that of the data-array, |
1549 | * not the device-set. | |
1550 | * uuid to recognise same set when adding a missing device back | |
1551 | * to an array. This is a uuid for the device-set. | |
613b0d17 | 1552 | * |
a322f70c DW |
1553 | * For each of these we can make do with a truncated |
1554 | * or hashed uuid rather than the original, as long as | |
1555 | * everyone agrees. | |
a322f70c DW |
1556 | * In the case of SVD we assume the BVD is of interest, |
1557 | * though that might be the case if a bitmap were made for | |
1558 | * a mirrored SVD - worry about that later. | |
1559 | * So we need to find the VD configuration record for the | |
1560 | * relevant BVD and extract the GUID and Secondary_Element_Seq. | |
1561 | * The first 16 bytes of the sha1 of these is used. | |
1562 | */ | |
1563 | struct ddf_super *ddf = st->sb; | |
d2ca6449 | 1564 | struct vcl *vcl = ddf->currentconf; |
c5afc314 | 1565 | char *guid; |
a322f70c | 1566 | |
c5afc314 N |
1567 | if (vcl) |
1568 | guid = vcl->conf.guid; | |
1569 | else | |
1570 | guid = ddf->anchor.guid; | |
bedbf68a | 1571 | uuid_from_ddf_guid(guid, uuid); |
a322f70c DW |
1572 | } |
1573 | ||
a5d85af7 | 1574 | static void getinfo_super_ddf_bvd(struct supertype *st, struct mdinfo *info, char *map); |
78e44928 | 1575 | |
a5d85af7 | 1576 | static void getinfo_super_ddf(struct supertype *st, struct mdinfo *info, char *map) |
a322f70c DW |
1577 | { |
1578 | struct ddf_super *ddf = st->sb; | |
a5d85af7 | 1579 | int map_disks = info->array.raid_disks; |
90fa1a29 | 1580 | __u32 *cptr; |
a322f70c | 1581 | |
78e44928 | 1582 | if (ddf->currentconf) { |
a5d85af7 | 1583 | getinfo_super_ddf_bvd(st, info, map); |
78e44928 NB |
1584 | return; |
1585 | } | |
95eeceeb | 1586 | memset(info, 0, sizeof(*info)); |
78e44928 | 1587 | |
a322f70c DW |
1588 | info->array.raid_disks = __be16_to_cpu(ddf->phys->used_pdes); |
1589 | info->array.level = LEVEL_CONTAINER; | |
1590 | info->array.layout = 0; | |
1591 | info->array.md_minor = -1; | |
90fa1a29 JS |
1592 | cptr = (__u32 *)(ddf->anchor.guid + 16); |
1593 | info->array.ctime = DECADE + __be32_to_cpu(*cptr); | |
1594 | ||
a322f70c DW |
1595 | info->array.utime = 0; |
1596 | info->array.chunk_size = 0; | |
510242aa | 1597 | info->container_enough = 1; |
a322f70c | 1598 | |
a322f70c DW |
1599 | info->disk.major = 0; |
1600 | info->disk.minor = 0; | |
cba0191b NB |
1601 | if (ddf->dlist) { |
1602 | info->disk.number = __be32_to_cpu(ddf->dlist->disk.refnum); | |
59e36268 | 1603 | info->disk.raid_disk = find_phys(ddf, ddf->dlist->disk.refnum); |
d2ca6449 NB |
1604 | |
1605 | info->data_offset = __be64_to_cpu(ddf->phys-> | |
613b0d17 N |
1606 | entries[info->disk.raid_disk]. |
1607 | config_size); | |
d2ca6449 | 1608 | info->component_size = ddf->dlist->size - info->data_offset; |
cba0191b NB |
1609 | } else { |
1610 | info->disk.number = -1; | |
661dce36 | 1611 | info->disk.raid_disk = -1; |
cba0191b NB |
1612 | // info->disk.raid_disk = find refnum in the table and use index; |
1613 | } | |
f22385f9 | 1614 | info->disk.state = (1 << MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE); |
a19c88b8 | 1615 | |
921d9e16 | 1616 | info->recovery_start = MaxSector; |
a19c88b8 | 1617 | info->reshape_active = 0; |
6e75048b | 1618 | info->recovery_blocked = 0; |
c5afc314 | 1619 | info->name[0] = 0; |
a322f70c | 1620 | |
f35f2525 N |
1621 | info->array.major_version = -1; |
1622 | info->array.minor_version = -2; | |
159c3a1a | 1623 | strcpy(info->text_version, "ddf"); |
a67dd8cc | 1624 | info->safe_mode_delay = 0; |
159c3a1a | 1625 | |
c5afc314 | 1626 | uuid_from_super_ddf(st, info->uuid); |
a322f70c | 1627 | |
a5d85af7 N |
1628 | if (map) { |
1629 | int i; | |
1630 | for (i = 0 ; i < map_disks; i++) { | |
1631 | if (i < info->array.raid_disks && | |
1632 | (__be16_to_cpu(ddf->phys->entries[i].state) & DDF_Online) && | |
1633 | !(__be16_to_cpu(ddf->phys->entries[i].state) & DDF_Failed)) | |
1634 | map[i] = 1; | |
1635 | else | |
1636 | map[i] = 0; | |
1637 | } | |
1638 | } | |
a322f70c DW |
1639 | } |
1640 | ||
598f0d58 NB |
1641 | static int rlq_to_layout(int rlq, int prl, int raiddisks); |
1642 | ||
a5d85af7 | 1643 | static void getinfo_super_ddf_bvd(struct supertype *st, struct mdinfo *info, char *map) |
a322f70c DW |
1644 | { |
1645 | struct ddf_super *ddf = st->sb; | |
d2ca6449 NB |
1646 | struct vcl *vc = ddf->currentconf; |
1647 | int cd = ddf->currentdev; | |
db42fa9b | 1648 | int j; |
8592f29d | 1649 | struct dl *dl; |
a5d85af7 | 1650 | int map_disks = info->array.raid_disks; |
90fa1a29 | 1651 | __u32 *cptr; |
a322f70c | 1652 | |
95eeceeb | 1653 | memset(info, 0, sizeof(*info)); |
a322f70c DW |
1654 | /* FIXME this returns BVD info - what if we want SVD ?? */ |
1655 | ||
d2ca6449 NB |
1656 | info->array.raid_disks = __be16_to_cpu(vc->conf.prim_elmnt_count); |
1657 | info->array.level = map_num1(ddf_level_num, vc->conf.prl); | |
1658 | info->array.layout = rlq_to_layout(vc->conf.rlq, vc->conf.prl, | |
598f0d58 | 1659 | info->array.raid_disks); |
a322f70c | 1660 | info->array.md_minor = -1; |
90fa1a29 JS |
1661 | cptr = (__u32 *)(vc->conf.guid + 16); |
1662 | info->array.ctime = DECADE + __be32_to_cpu(*cptr); | |
d2ca6449 NB |
1663 | info->array.utime = DECADE + __be32_to_cpu(vc->conf.timestamp); |
1664 | info->array.chunk_size = 512 << vc->conf.chunk_shift; | |
da9b4a62 | 1665 | info->custom_array_size = 0; |
d2ca6449 | 1666 | |
f21e18ca | 1667 | if (cd >= 0 && (unsigned)cd < ddf->mppe) { |
d2ca6449 NB |
1668 | info->data_offset = __be64_to_cpu(vc->lba_offset[cd]); |
1669 | if (vc->block_sizes) | |
1670 | info->component_size = vc->block_sizes[cd]; | |
1671 | else | |
1672 | info->component_size = __be64_to_cpu(vc->conf.blocks); | |
1673 | } | |
a322f70c | 1674 | |
fb204fb2 N |
1675 | for (dl = ddf->dlist; dl ; dl = dl->next) |
1676 | if (dl->raiddisk == ddf->currentdev) | |
1677 | break; | |
1678 | ||
a322f70c DW |
1679 | info->disk.major = 0; |
1680 | info->disk.minor = 0; | |
fb204fb2 | 1681 | info->disk.state = 0; |
8592f29d N |
1682 | if (dl) { |
1683 | info->disk.major = dl->major; | |
1684 | info->disk.minor = dl->minor; | |
fb204fb2 N |
1685 | info->disk.raid_disk = dl->raiddisk; |
1686 | info->disk.number = dl->pdnum; | |
1687 | info->disk.state = (1<<MD_DISK_SYNC)|(1<<MD_DISK_ACTIVE); | |
8592f29d | 1688 | } |
a322f70c | 1689 | |
103f2410 NB |
1690 | info->container_member = ddf->currentconf->vcnum; |
1691 | ||
921d9e16 | 1692 | info->recovery_start = MaxSector; |
80d26cb2 | 1693 | info->resync_start = 0; |
624c5ad4 | 1694 | info->reshape_active = 0; |
6e75048b | 1695 | info->recovery_blocked = 0; |
80d26cb2 NB |
1696 | if (!(ddf->virt->entries[info->container_member].state |
1697 | & DDF_state_inconsistent) && | |
1698 | (ddf->virt->entries[info->container_member].init_state | |
1699 | & DDF_initstate_mask) | |
1700 | == DDF_init_full) | |
b7528a20 | 1701 | info->resync_start = MaxSector; |
80d26cb2 | 1702 | |
a322f70c DW |
1703 | uuid_from_super_ddf(st, info->uuid); |
1704 | ||
f35f2525 N |
1705 | info->array.major_version = -1; |
1706 | info->array.minor_version = -2; | |
9b63e648 | 1707 | sprintf(info->text_version, "/%s/%d", |
4dd2df09 | 1708 | st->container_devnm, |
9b63e648 | 1709 | info->container_member); |
a67dd8cc | 1710 | info->safe_mode_delay = 200; |
159c3a1a | 1711 | |
db42fa9b N |
1712 | memcpy(info->name, ddf->virt->entries[info->container_member].name, 16); |
1713 | info->name[16]=0; | |
1714 | for(j=0; j<16; j++) | |
1715 | if (info->name[j] == ' ') | |
1716 | info->name[j] = 0; | |
a5d85af7 N |
1717 | |
1718 | if (map) | |
1719 | for (j = 0; j < map_disks; j++) { | |
1720 | map[j] = 0; | |
1721 | if (j < info->array.raid_disks) { | |
1722 | int i = find_phys(ddf, vc->conf.phys_refnum[j]); | |
613b0d17 | 1723 | if (i >= 0 && |
a5d85af7 N |
1724 | (__be16_to_cpu(ddf->phys->entries[i].state) & DDF_Online) && |
1725 | !(__be16_to_cpu(ddf->phys->entries[i].state) & DDF_Failed)) | |
1726 | map[i] = 1; | |
1727 | } | |
1728 | } | |
a322f70c DW |
1729 | } |
1730 | ||
1731 | static int update_super_ddf(struct supertype *st, struct mdinfo *info, | |
1732 | char *update, | |
1733 | char *devname, int verbose, | |
1734 | int uuid_set, char *homehost) | |
1735 | { | |
1736 | /* For 'assemble' and 'force' we need to return non-zero if any | |
1737 | * change was made. For others, the return value is ignored. | |
1738 | * Update options are: | |
1739 | * force-one : This device looks a bit old but needs to be included, | |
1740 | * update age info appropriately. | |
1741 | * assemble: clear any 'faulty' flag to allow this device to | |
1742 | * be assembled. | |
1743 | * force-array: Array is degraded but being forced, mark it clean | |
1744 | * if that will be needed to assemble it. | |
1745 | * | |
1746 | * newdev: not used ???? | |
1747 | * grow: Array has gained a new device - this is currently for | |
1748 | * linear only | |
1749 | * resync: mark as dirty so a resync will happen. | |
59e36268 | 1750 | * uuid: Change the uuid of the array to match what is given |
a322f70c DW |
1751 | * homehost: update the recorded homehost |
1752 | * name: update the name - preserving the homehost | |
1753 | * _reshape_progress: record new reshape_progress position. | |
1754 | * | |
1755 | * Following are not relevant for this version: | |
1756 | * sparc2.2 : update from old dodgey metadata | |
1757 | * super-minor: change the preferred_minor number | |
1758 | * summaries: update redundant counters. | |
1759 | */ | |
1760 | int rv = 0; | |
1761 | // struct ddf_super *ddf = st->sb; | |
7a7cc504 | 1762 | // struct vd_config *vd = find_vdcr(ddf, info->container_member); |
a322f70c DW |
1763 | // struct virtual_entry *ve = find_ve(ddf); |
1764 | ||
a322f70c DW |
1765 | /* we don't need to handle "force-*" or "assemble" as |
1766 | * there is no need to 'trick' the kernel. We the metadata is | |
1767 | * first updated to activate the array, all the implied modifications | |
1768 | * will just happen. | |
1769 | */ | |
1770 | ||
1771 | if (strcmp(update, "grow") == 0) { | |
1772 | /* FIXME */ | |
1e2b2765 | 1773 | } else if (strcmp(update, "resync") == 0) { |
a322f70c | 1774 | // info->resync_checkpoint = 0; |
1e2b2765 | 1775 | } else if (strcmp(update, "homehost") == 0) { |
a322f70c DW |
1776 | /* homehost is stored in controller->vendor_data, |
1777 | * or it is when we are the vendor | |
1778 | */ | |
1779 | // if (info->vendor_is_local) | |
1780 | // strcpy(ddf->controller.vendor_data, homehost); | |
1e2b2765 | 1781 | rv = -1; |
f49208ec | 1782 | } else if (strcmp(update, "name") == 0) { |
a322f70c DW |
1783 | /* name is stored in virtual_entry->name */ |
1784 | // memset(ve->name, ' ', 16); | |
1785 | // strncpy(ve->name, info->name, 16); | |
1e2b2765 | 1786 | rv = -1; |
f49208ec | 1787 | } else if (strcmp(update, "_reshape_progress") == 0) { |
a322f70c | 1788 | /* We don't support reshape yet */ |
f49208ec N |
1789 | } else if (strcmp(update, "assemble") == 0 ) { |
1790 | /* Do nothing, just succeed */ | |
1791 | rv = 0; | |
1e2b2765 N |
1792 | } else |
1793 | rv = -1; | |
a322f70c DW |
1794 | |
1795 | // update_all_csum(ddf); | |
1796 | ||
1797 | return rv; | |
1798 | } | |
1799 | ||
5f8097be NB |
1800 | static void make_header_guid(char *guid) |
1801 | { | |
1802 | __u32 stamp; | |
5f8097be NB |
1803 | /* Create a DDF Header of Virtual Disk GUID */ |
1804 | ||
1805 | /* 24 bytes of fiction required. | |
1806 | * first 8 are a 'vendor-id' - "Linux-MD" | |
1807 | * next 8 are controller type.. how about 0X DEAD BEEF 0000 0000 | |
1808 | * Remaining 8 random number plus timestamp | |
1809 | */ | |
1810 | memcpy(guid, T10, sizeof(T10)); | |
1811 | stamp = __cpu_to_be32(0xdeadbeef); | |
1812 | memcpy(guid+8, &stamp, 4); | |
1813 | stamp = __cpu_to_be32(0); | |
1814 | memcpy(guid+12, &stamp, 4); | |
1815 | stamp = __cpu_to_be32(time(0) - DECADE); | |
1816 | memcpy(guid+16, &stamp, 4); | |
bfb7ea78 | 1817 | stamp = random32(); |
5f8097be | 1818 | memcpy(guid+20, &stamp, 4); |
5f8097be | 1819 | } |
59e36268 | 1820 | |
fb9d0acb | 1821 | static unsigned int find_unused_vde(const struct ddf_super *ddf) |
1822 | { | |
1823 | unsigned int i; | |
1824 | for (i = 0; i < __be16_to_cpu(ddf->virt->max_vdes); i++) { | |
1825 | if (all_ff(ddf->virt->entries[i].guid)) | |
1826 | return i; | |
1827 | } | |
1828 | return DDF_NOTFOUND; | |
1829 | } | |
1830 | ||
1831 | static unsigned int find_vde_by_name(const struct ddf_super *ddf, | |
1832 | const char *name) | |
1833 | { | |
1834 | unsigned int i; | |
1835 | if (name == NULL) | |
1836 | return DDF_NOTFOUND; | |
1837 | for (i = 0; i < __be16_to_cpu(ddf->virt->max_vdes); i++) { | |
1838 | if (all_ff(ddf->virt->entries[i].guid)) | |
1839 | continue; | |
1840 | if (!strncmp(name, ddf->virt->entries[i].name, | |
1841 | sizeof(ddf->virt->entries[i].name))) | |
1842 | return i; | |
1843 | } | |
1844 | return DDF_NOTFOUND; | |
1845 | } | |
1846 | ||
1847 | static unsigned int find_vde_by_guid(const struct ddf_super *ddf, | |
1848 | const char *guid) | |
1849 | { | |
1850 | unsigned int i; | |
1851 | if (guid == NULL || all_ff(guid)) | |
1852 | return DDF_NOTFOUND; | |
1853 | for (i = 0; i < __be16_to_cpu(ddf->virt->max_vdes); i++) | |
1854 | if (!memcmp(ddf->virt->entries[i].guid, guid, DDF_GUID_LEN)) | |
1855 | return i; | |
1856 | return DDF_NOTFOUND; | |
1857 | } | |
1858 | ||
78e44928 NB |
1859 | static int init_super_ddf_bvd(struct supertype *st, |
1860 | mdu_array_info_t *info, | |
1861 | unsigned long long size, | |
1862 | char *name, char *homehost, | |
83cd1e97 | 1863 | int *uuid, unsigned long long data_offset); |
78e44928 | 1864 | |
a322f70c DW |
1865 | static int init_super_ddf(struct supertype *st, |
1866 | mdu_array_info_t *info, | |
1867 | unsigned long long size, char *name, char *homehost, | |
83cd1e97 | 1868 | int *uuid, unsigned long long data_offset) |
a322f70c DW |
1869 | { |
1870 | /* This is primarily called by Create when creating a new array. | |
1871 | * We will then get add_to_super called for each component, and then | |
1872 | * write_init_super called to write it out to each device. | |
1873 | * For DDF, Create can create on fresh devices or on a pre-existing | |
1874 | * array. | |
1875 | * To create on a pre-existing array a different method will be called. | |
1876 | * This one is just for fresh drives. | |
1877 | * | |
1878 | * We need to create the entire 'ddf' structure which includes: | |
1879 | * DDF headers - these are easy. | |
1880 | * Controller data - a Sector describing this controller .. not that | |
1881 | * this is a controller exactly. | |
1882 | * Physical Disk Record - one entry per device, so | |
1883 | * leave plenty of space. | |
1884 | * Virtual Disk Records - again, just leave plenty of space. | |
1885 | * This just lists VDs, doesn't give details | |
1886 | * Config records - describes the VDs that use this disk | |
1887 | * DiskData - describes 'this' device. | |
1888 | * BadBlockManagement - empty | |
1889 | * Diag Space - empty | |
1890 | * Vendor Logs - Could we put bitmaps here? | |
1891 | * | |
1892 | */ | |
1893 | struct ddf_super *ddf; | |
1894 | char hostname[17]; | |
1895 | int hostlen; | |
a322f70c DW |
1896 | int max_phys_disks, max_virt_disks; |
1897 | unsigned long long sector; | |
1898 | int clen; | |
1899 | int i; | |
1900 | int pdsize, vdsize; | |
1901 | struct phys_disk *pd; | |
1902 | struct virtual_disk *vd; | |
1903 | ||
83cd1e97 | 1904 | if (data_offset != INVALID_SECTORS) { |
ed503f89 | 1905 | pr_err("data-offset not supported by DDF\n"); |
83cd1e97 N |
1906 | return 0; |
1907 | } | |
1908 | ||
78e44928 | 1909 | if (st->sb) |
83cd1e97 N |
1910 | return init_super_ddf_bvd(st, info, size, name, homehost, uuid, |
1911 | data_offset); | |
ba7eb04f | 1912 | |
3d2c4fc7 | 1913 | if (posix_memalign((void**)&ddf, 512, sizeof(*ddf)) != 0) { |
e7b84f9d | 1914 | pr_err("%s could not allocate superblock\n", __func__); |
3d2c4fc7 DW |
1915 | return 0; |
1916 | } | |
6264b437 | 1917 | memset(ddf, 0, sizeof(*ddf)); |
a322f70c DW |
1918 | ddf->dlist = NULL; /* no physical disks yet */ |
1919 | ddf->conflist = NULL; /* No virtual disks yet */ | |
955e9ea1 DW |
1920 | st->sb = ddf; |
1921 | ||
1922 | if (info == NULL) { | |
1923 | /* zeroing superblock */ | |
1924 | return 0; | |
1925 | } | |
a322f70c DW |
1926 | |
1927 | /* At least 32MB *must* be reserved for the ddf. So let's just | |
1928 | * start 32MB from the end, and put the primary header there. | |
1929 | * Don't do secondary for now. | |
1930 | * We don't know exactly where that will be yet as it could be | |
1931 | * different on each device. To just set up the lengths. | |
1932 | * | |
1933 | */ | |
1934 | ||
1935 | ddf->anchor.magic = DDF_HEADER_MAGIC; | |
5f8097be | 1936 | make_header_guid(ddf->anchor.guid); |
a322f70c | 1937 | |
59e36268 | 1938 | memcpy(ddf->anchor.revision, DDF_REVISION_2, 8); |
a322f70c DW |
1939 | ddf->anchor.seq = __cpu_to_be32(1); |
1940 | ddf->anchor.timestamp = __cpu_to_be32(time(0) - DECADE); | |
1941 | ddf->anchor.openflag = 0xFF; | |
1942 | ddf->anchor.foreignflag = 0; | |
1943 | ddf->anchor.enforcegroups = 0; /* Is this best?? */ | |
1944 | ddf->anchor.pad0 = 0xff; | |
1945 | memset(ddf->anchor.pad1, 0xff, 12); | |
1946 | memset(ddf->anchor.header_ext, 0xff, 32); | |
1947 | ddf->anchor.primary_lba = ~(__u64)0; | |
1948 | ddf->anchor.secondary_lba = ~(__u64)0; | |
1949 | ddf->anchor.type = DDF_HEADER_ANCHOR; | |
1950 | memset(ddf->anchor.pad2, 0xff, 3); | |
1951 | ddf->anchor.workspace_len = __cpu_to_be32(32768); /* Must be reserved */ | |
1952 | ddf->anchor.workspace_lba = ~(__u64)0; /* Put this at bottom | |
1953 | of 32M reserved.. */ | |
1954 | max_phys_disks = 1023; /* Should be enough */ | |
1955 | ddf->anchor.max_pd_entries = __cpu_to_be16(max_phys_disks); | |
1956 | max_virt_disks = 255; | |
1957 | ddf->anchor.max_vd_entries = __cpu_to_be16(max_virt_disks); /* ?? */ | |
1958 | ddf->anchor.max_partitions = __cpu_to_be16(64); /* ?? */ | |
1959 | ddf->max_part = 64; | |
8c3b8c2c | 1960 | ddf->mppe = 256; |
59e36268 NB |
1961 | ddf->conf_rec_len = 1 + ROUND_UP(ddf->mppe * (4+8), 512)/512; |
1962 | ddf->anchor.config_record_len = __cpu_to_be16(ddf->conf_rec_len); | |
1963 | ddf->anchor.max_primary_element_entries = __cpu_to_be16(ddf->mppe); | |
a322f70c | 1964 | memset(ddf->anchor.pad3, 0xff, 54); |
a322f70c DW |
1965 | /* controller sections is one sector long immediately |
1966 | * after the ddf header */ | |
1967 | sector = 1; | |
1968 | ddf->anchor.controller_section_offset = __cpu_to_be32(sector); | |
1969 | ddf->anchor.controller_section_length = __cpu_to_be32(1); | |
1970 | sector += 1; | |
1971 | ||
1972 | /* phys is 8 sectors after that */ | |
1973 | pdsize = ROUND_UP(sizeof(struct phys_disk) + | |
1974 | sizeof(struct phys_disk_entry)*max_phys_disks, | |
1975 | 512); | |
1976 | switch(pdsize/512) { | |
1977 | case 2: case 8: case 32: case 128: case 512: break; | |
1978 | default: abort(); | |
1979 | } | |
1980 | ddf->anchor.phys_section_offset = __cpu_to_be32(sector); | |
1981 | ddf->anchor.phys_section_length = | |
1982 | __cpu_to_be32(pdsize/512); /* max_primary_element_entries/8 */ | |
1983 | sector += pdsize/512; | |
1984 | ||
1985 | /* virt is another 32 sectors */ | |
1986 | vdsize = ROUND_UP(sizeof(struct virtual_disk) + | |
1987 | sizeof(struct virtual_entry) * max_virt_disks, | |
1988 | 512); | |
1989 | switch(vdsize/512) { | |
1990 | case 2: case 8: case 32: case 128: case 512: break; | |
1991 | default: abort(); | |
1992 | } | |
1993 | ddf->anchor.virt_section_offset = __cpu_to_be32(sector); | |
1994 | ddf->anchor.virt_section_length = | |
1995 | __cpu_to_be32(vdsize/512); /* max_vd_entries/8 */ | |
1996 | sector += vdsize/512; | |
1997 | ||
59e36268 | 1998 | clen = ddf->conf_rec_len * (ddf->max_part+1); |
a322f70c DW |
1999 | ddf->anchor.config_section_offset = __cpu_to_be32(sector); |
2000 | ddf->anchor.config_section_length = __cpu_to_be32(clen); | |
2001 | sector += clen; | |
2002 | ||
2003 | ddf->anchor.data_section_offset = __cpu_to_be32(sector); | |
2004 | ddf->anchor.data_section_length = __cpu_to_be32(1); | |
2005 | sector += 1; | |
2006 | ||
2007 | ddf->anchor.bbm_section_length = __cpu_to_be32(0); | |
2008 | ddf->anchor.bbm_section_offset = __cpu_to_be32(0xFFFFFFFF); | |
2009 | ddf->anchor.diag_space_length = __cpu_to_be32(0); | |
2010 | ddf->anchor.diag_space_offset = __cpu_to_be32(0xFFFFFFFF); | |
2011 | ddf->anchor.vendor_length = __cpu_to_be32(0); | |
2012 | ddf->anchor.vendor_offset = __cpu_to_be32(0xFFFFFFFF); | |
2013 | ||
2014 | memset(ddf->anchor.pad4, 0xff, 256); | |
2015 | ||
2016 | memcpy(&ddf->primary, &ddf->anchor, 512); | |
2017 | memcpy(&ddf->secondary, &ddf->anchor, 512); | |
2018 | ||
2019 | ddf->primary.openflag = 1; /* I guess.. */ | |
2020 | ddf->primary.type = DDF_HEADER_PRIMARY; | |
2021 | ||
2022 | ddf->secondary.openflag = 1; /* I guess.. */ | |
2023 | ddf->secondary.type = DDF_HEADER_SECONDARY; | |
2024 | ||
2025 | ddf->active = &ddf->primary; | |
2026 | ||
2027 | ddf->controller.magic = DDF_CONTROLLER_MAGIC; | |
2028 | ||
2029 | /* 24 more bytes of fiction required. | |
2030 | * first 8 are a 'vendor-id' - "Linux-MD" | |
2031 | * Remaining 16 are serial number.... maybe a hostname would do? | |
2032 | */ | |
2033 | memcpy(ddf->controller.guid, T10, sizeof(T10)); | |
1ba6bff9 DW |
2034 | gethostname(hostname, sizeof(hostname)); |
2035 | hostname[sizeof(hostname) - 1] = 0; | |
a322f70c DW |
2036 | hostlen = strlen(hostname); |
2037 | memcpy(ddf->controller.guid + 24 - hostlen, hostname, hostlen); | |
2038 | for (i = strlen(T10) ; i+hostlen < 24; i++) | |
2039 | ddf->controller.guid[i] = ' '; | |
2040 | ||
2041 | ddf->controller.type.vendor_id = __cpu_to_be16(0xDEAD); | |
2042 | ddf->controller.type.device_id = __cpu_to_be16(0xBEEF); | |
2043 | ddf->controller.type.sub_vendor_id = 0; | |
2044 | ddf->controller.type.sub_device_id = 0; | |
2045 | memcpy(ddf->controller.product_id, "What Is My PID??", 16); | |
2046 | memset(ddf->controller.pad, 0xff, 8); | |
2047 | memset(ddf->controller.vendor_data, 0xff, 448); | |
a9e1c11d N |
2048 | if (homehost && strlen(homehost) < 440) |
2049 | strcpy((char*)ddf->controller.vendor_data, homehost); | |
a322f70c | 2050 | |
3d2c4fc7 | 2051 | if (posix_memalign((void**)&pd, 512, pdsize) != 0) { |
e7b84f9d | 2052 | pr_err("%s could not allocate pd\n", __func__); |
3d2c4fc7 DW |
2053 | return 0; |
2054 | } | |
6416d527 | 2055 | ddf->phys = pd; |
a322f70c DW |
2056 | ddf->pdsize = pdsize; |
2057 | ||
2058 | memset(pd, 0xff, pdsize); | |
2059 | memset(pd, 0, sizeof(*pd)); | |
076515ba | 2060 | pd->magic = DDF_PHYS_RECORDS_MAGIC; |
a322f70c DW |
2061 | pd->used_pdes = __cpu_to_be16(0); |
2062 | pd->max_pdes = __cpu_to_be16(max_phys_disks); | |
2063 | memset(pd->pad, 0xff, 52); | |
2064 | ||
3d2c4fc7 | 2065 | if (posix_memalign((void**)&vd, 512, vdsize) != 0) { |
e7b84f9d | 2066 | pr_err("%s could not allocate vd\n", __func__); |
3d2c4fc7 DW |
2067 | return 0; |
2068 | } | |
6416d527 | 2069 | ddf->virt = vd; |
a322f70c DW |
2070 | ddf->vdsize = vdsize; |
2071 | memset(vd, 0, vdsize); | |
2072 | vd->magic = DDF_VIRT_RECORDS_MAGIC; | |
2073 | vd->populated_vdes = __cpu_to_be16(0); | |
2074 | vd->max_vdes = __cpu_to_be16(max_virt_disks); | |
2075 | memset(vd->pad, 0xff, 52); | |
2076 | ||
5f8097be NB |
2077 | for (i=0; i<max_virt_disks; i++) |
2078 | memset(&vd->entries[i], 0xff, sizeof(struct virtual_entry)); | |
2079 | ||
a322f70c | 2080 | st->sb = ddf; |
7d5a7ff3 | 2081 | ddf_set_updates_pending(ddf); |
a322f70c DW |
2082 | return 1; |
2083 | } | |
2084 | ||
5f8097be NB |
2085 | static int chunk_to_shift(int chunksize) |
2086 | { | |
2087 | return ffs(chunksize/512)-1; | |
2088 | } | |
2089 | ||
2090 | static int level_to_prl(int level) | |
2091 | { | |
2092 | switch (level) { | |
2093 | case LEVEL_LINEAR: return DDF_CONCAT; | |
2094 | case 0: return DDF_RAID0; | |
2095 | case 1: return DDF_RAID1; | |
2096 | case 4: return DDF_RAID4; | |
2097 | case 5: return DDF_RAID5; | |
2098 | case 6: return DDF_RAID6; | |
2099 | default: return -1; | |
2100 | } | |
2101 | } | |
613b0d17 | 2102 | |
5f8097be NB |
2103 | static int layout_to_rlq(int level, int layout, int raiddisks) |
2104 | { | |
2105 | switch(level) { | |
2106 | case 0: | |
2107 | return DDF_RAID0_SIMPLE; | |
2108 | case 1: | |
2109 | switch(raiddisks) { | |
2110 | case 2: return DDF_RAID1_SIMPLE; | |
2111 | case 3: return DDF_RAID1_MULTI; | |
2112 | default: return -1; | |
2113 | } | |
2114 | case 4: | |
2115 | switch(layout) { | |
2116 | case 0: return DDF_RAID4_N; | |
2117 | } | |
2118 | break; | |
2119 | case 5: | |
5f8097be NB |
2120 | switch(layout) { |
2121 | case ALGORITHM_LEFT_ASYMMETRIC: | |
2122 | return DDF_RAID5_N_RESTART; | |
2123 | case ALGORITHM_RIGHT_ASYMMETRIC: | |
b640a252 | 2124 | return DDF_RAID5_0_RESTART; |
5f8097be NB |
2125 | case ALGORITHM_LEFT_SYMMETRIC: |
2126 | return DDF_RAID5_N_CONTINUE; | |
2127 | case ALGORITHM_RIGHT_SYMMETRIC: | |
2128 | return -1; /* not mentioned in standard */ | |
2129 | } | |
b640a252 N |
2130 | case 6: |
2131 | switch(layout) { | |
2132 | case ALGORITHM_ROTATING_N_RESTART: | |
2133 | return DDF_RAID5_N_RESTART; | |
2134 | case ALGORITHM_ROTATING_ZERO_RESTART: | |
2135 | return DDF_RAID6_0_RESTART; | |
2136 | case ALGORITHM_ROTATING_N_CONTINUE: | |
2137 | return DDF_RAID5_N_CONTINUE; | |
2138 | } | |
5f8097be NB |
2139 | } |
2140 | return -1; | |
2141 | } | |
2142 | ||
598f0d58 NB |
2143 | static int rlq_to_layout(int rlq, int prl, int raiddisks) |
2144 | { | |
2145 | switch(prl) { | |
2146 | case DDF_RAID0: | |
2147 | return 0; /* hopefully rlq == DDF_RAID0_SIMPLE */ | |
2148 | case DDF_RAID1: | |
2149 | return 0; /* hopefully rlq == SIMPLE or MULTI depending | |
2150 | on raiddisks*/ | |
2151 | case DDF_RAID4: | |
2152 | switch(rlq) { | |
2153 | case DDF_RAID4_N: | |
2154 | return 0; | |
2155 | default: | |
2156 | /* not supported */ | |
2157 | return -1; /* FIXME this isn't checked */ | |
2158 | } | |
2159 | case DDF_RAID5: | |
598f0d58 NB |
2160 | switch(rlq) { |
2161 | case DDF_RAID5_N_RESTART: | |
2162 | return ALGORITHM_LEFT_ASYMMETRIC; | |
2163 | case DDF_RAID5_0_RESTART: | |
2164 | return ALGORITHM_RIGHT_ASYMMETRIC; | |
2165 | case DDF_RAID5_N_CONTINUE: | |
2166 | return ALGORITHM_LEFT_SYMMETRIC; | |
2167 | default: | |
2168 | return -1; | |
2169 | } | |
59e36268 NB |
2170 | case DDF_RAID6: |
2171 | switch(rlq) { | |
2172 | case DDF_RAID5_N_RESTART: | |
b640a252 | 2173 | return ALGORITHM_ROTATING_N_RESTART; |
59e36268 | 2174 | case DDF_RAID6_0_RESTART: |
b640a252 | 2175 | return ALGORITHM_ROTATING_ZERO_RESTART; |
59e36268 | 2176 | case DDF_RAID5_N_CONTINUE: |
b640a252 | 2177 | return ALGORITHM_ROTATING_N_CONTINUE; |
59e36268 NB |
2178 | default: |
2179 | return -1; | |
2180 | } | |
598f0d58 NB |
2181 | } |
2182 | return -1; | |
2183 | } | |
2184 | ||
0e600426 | 2185 | #ifndef MDASSEMBLE |
59e36268 NB |
2186 | struct extent { |
2187 | unsigned long long start, size; | |
2188 | }; | |
78e44928 | 2189 | static int cmp_extent(const void *av, const void *bv) |
59e36268 NB |
2190 | { |
2191 | const struct extent *a = av; | |
2192 | const struct extent *b = bv; | |
2193 | if (a->start < b->start) | |
2194 | return -1; | |
2195 | if (a->start > b->start) | |
2196 | return 1; | |
2197 | return 0; | |
2198 | } | |
2199 | ||
78e44928 | 2200 | static struct extent *get_extents(struct ddf_super *ddf, struct dl *dl) |
59e36268 NB |
2201 | { |
2202 | /* find a list of used extents on the give physical device | |
2203 | * (dnum) of the given ddf. | |
2204 | * Return a malloced array of 'struct extent' | |
2205 | ||
613b0d17 | 2206 | * FIXME ignore DDF_Legacy devices? |
59e36268 NB |
2207 | |
2208 | */ | |
2209 | struct extent *rv; | |
2210 | int n = 0; | |
f21e18ca | 2211 | unsigned int i, j; |
59e36268 | 2212 | |
503975b9 | 2213 | rv = xmalloc(sizeof(struct extent) * (ddf->max_part + 2)); |
59e36268 NB |
2214 | |
2215 | for (i = 0; i < ddf->max_part; i++) { | |
2216 | struct vcl *v = dl->vlist[i]; | |
2217 | if (v == NULL) | |
2218 | continue; | |
f21e18ca | 2219 | for (j = 0; j < v->conf.prim_elmnt_count; j++) |
59e36268 NB |
2220 | if (v->conf.phys_refnum[j] == dl->disk.refnum) { |
2221 | /* This device plays role 'j' in 'v'. */ | |
2222 | rv[n].start = __be64_to_cpu(v->lba_offset[j]); | |
2223 | rv[n].size = __be64_to_cpu(v->conf.blocks); | |
2224 | n++; | |
2225 | break; | |
2226 | } | |
2227 | } | |
2228 | qsort(rv, n, sizeof(*rv), cmp_extent); | |
2229 | ||
2230 | rv[n].start = __be64_to_cpu(ddf->phys->entries[dl->pdnum].config_size); | |
2231 | rv[n].size = 0; | |
2232 | return rv; | |
2233 | } | |
0e600426 | 2234 | #endif |
59e36268 | 2235 | |
5f8097be NB |
2236 | static int init_super_ddf_bvd(struct supertype *st, |
2237 | mdu_array_info_t *info, | |
2238 | unsigned long long size, | |
2239 | char *name, char *homehost, | |
83cd1e97 | 2240 | int *uuid, unsigned long long data_offset) |
5f8097be NB |
2241 | { |
2242 | /* We are creating a BVD inside a pre-existing container. | |
2243 | * so st->sb is already set. | |
2244 | * We need to create a new vd_config and a new virtual_entry | |
2245 | */ | |
2246 | struct ddf_super *ddf = st->sb; | |
f21e18ca | 2247 | unsigned int venum; |
5f8097be NB |
2248 | struct virtual_entry *ve; |
2249 | struct vcl *vcl; | |
2250 | struct vd_config *vc; | |
5f8097be | 2251 | |
fb9d0acb | 2252 | if (find_vde_by_name(ddf, name) != DDF_NOTFOUND) { |
2253 | pr_err("This ddf already has an array called %s\n", name); | |
5f8097be NB |
2254 | return 0; |
2255 | } | |
fb9d0acb | 2256 | venum = find_unused_vde(ddf); |
2257 | if (venum == DDF_NOTFOUND) { | |
2258 | pr_err("Cannot find spare slot for virtual disk\n"); | |
5f8097be NB |
2259 | return 0; |
2260 | } | |
2261 | ve = &ddf->virt->entries[venum]; | |
2262 | ||
2263 | /* A Virtual Disk GUID contains the T10 Vendor ID, controller type, | |
2264 | * timestamp, random number | |
2265 | */ | |
2266 | make_header_guid(ve->guid); | |
2267 | ve->unit = __cpu_to_be16(info->md_minor); | |
2268 | ve->pad0 = 0xFFFF; | |
2269 | ve->guid_crc = crc32(0, (unsigned char*)ddf->anchor.guid, DDF_GUID_LEN); | |
2270 | ve->type = 0; | |
7a7cc504 NB |
2271 | ve->state = DDF_state_degraded; /* Will be modified as devices are added */ |
2272 | if (info->state & 1) /* clean */ | |
2273 | ve->init_state = DDF_init_full; | |
2274 | else | |
2275 | ve->init_state = DDF_init_not; | |
2276 | ||
5f8097be NB |
2277 | memset(ve->pad1, 0xff, 14); |
2278 | memset(ve->name, ' ', 16); | |
2279 | if (name) | |
2280 | strncpy(ve->name, name, 16); | |
2281 | ddf->virt->populated_vdes = | |
2282 | __cpu_to_be16(__be16_to_cpu(ddf->virt->populated_vdes)+1); | |
2283 | ||
2284 | /* Now create a new vd_config */ | |
3d2c4fc7 DW |
2285 | if (posix_memalign((void**)&vcl, 512, |
2286 | (offsetof(struct vcl, conf) + ddf->conf_rec_len * 512)) != 0) { | |
e7b84f9d | 2287 | pr_err("%s could not allocate vd_config\n", __func__); |
3d2c4fc7 DW |
2288 | return 0; |
2289 | } | |
8c3b8c2c | 2290 | vcl->lba_offset = (__u64*) &vcl->conf.phys_refnum[ddf->mppe]; |
59e36268 NB |
2291 | vcl->vcnum = venum; |
2292 | vcl->block_sizes = NULL; /* FIXME not for CONCAT */ | |
8ec5d685 | 2293 | vcl->other_bvds = NULL; |
5f8097be NB |
2294 | |
2295 | vc = &vcl->conf; | |
2296 | ||
2297 | vc->magic = DDF_VD_CONF_MAGIC; | |
2298 | memcpy(vc->guid, ve->guid, DDF_GUID_LEN); | |
2299 | vc->timestamp = __cpu_to_be32(time(0)-DECADE); | |
2300 | vc->seqnum = __cpu_to_be32(1); | |
2301 | memset(vc->pad0, 0xff, 24); | |
2302 | vc->prim_elmnt_count = __cpu_to_be16(info->raid_disks); | |
2303 | vc->chunk_shift = chunk_to_shift(info->chunk_size); | |
2304 | vc->prl = level_to_prl(info->level); | |
2305 | vc->rlq = layout_to_rlq(info->level, info->layout, info->raid_disks); | |
2306 | vc->sec_elmnt_count = 1; | |
2307 | vc->sec_elmnt_seq = 0; | |
2308 | vc->srl = 0; | |
2309 | vc->blocks = __cpu_to_be64(info->size * 2); | |
2310 | vc->array_blocks = __cpu_to_be64( | |
2311 | calc_array_size(info->level, info->raid_disks, info->layout, | |
2312 | info->chunk_size, info->size*2)); | |
2313 | memset(vc->pad1, 0xff, 8); | |
2314 | vc->spare_refs[0] = 0xffffffff; | |
2315 | vc->spare_refs[1] = 0xffffffff; | |
2316 | vc->spare_refs[2] = 0xffffffff; | |
2317 | vc->spare_refs[3] = 0xffffffff; | |
2318 | vc->spare_refs[4] = 0xffffffff; | |
2319 | vc->spare_refs[5] = 0xffffffff; | |
2320 | vc->spare_refs[6] = 0xffffffff; | |
2321 | vc->spare_refs[7] = 0xffffffff; | |
2322 | memset(vc->cache_pol, 0, 8); | |
2323 | vc->bg_rate = 0x80; | |
2324 | memset(vc->pad2, 0xff, 3); | |
2325 | memset(vc->pad3, 0xff, 52); | |
2326 | memset(vc->pad4, 0xff, 192); | |
2327 | memset(vc->v0, 0xff, 32); | |
2328 | memset(vc->v1, 0xff, 32); | |
2329 | memset(vc->v2, 0xff, 16); | |
2330 | memset(vc->v3, 0xff, 16); | |
2331 | memset(vc->vendor, 0xff, 32); | |
598f0d58 | 2332 | |
8c3b8c2c | 2333 | memset(vc->phys_refnum, 0xff, 4*ddf->mppe); |
e5a2a3cf | 2334 | memset(vc->phys_refnum+ddf->mppe, 0x00, 8*ddf->mppe); |
5f8097be NB |
2335 | |
2336 | vcl->next = ddf->conflist; | |
2337 | ddf->conflist = vcl; | |
d2ca6449 | 2338 | ddf->currentconf = vcl; |
7d5a7ff3 | 2339 | ddf_set_updates_pending(ddf); |
5f8097be NB |
2340 | return 1; |
2341 | } | |
2342 | ||
0e600426 | 2343 | #ifndef MDASSEMBLE |
5f8097be NB |
2344 | static void add_to_super_ddf_bvd(struct supertype *st, |
2345 | mdu_disk_info_t *dk, int fd, char *devname) | |
2346 | { | |
2347 | /* fd and devname identify a device with-in the ddf container (st). | |
2348 | * dk identifies a location in the new BVD. | |
2349 | * We need to find suitable free space in that device and update | |
2350 | * the phys_refnum and lba_offset for the newly created vd_config. | |
2351 | * We might also want to update the type in the phys_disk | |
5575e7d9 | 2352 | * section. |
8592f29d N |
2353 | * |
2354 | * Alternately: fd == -1 and we have already chosen which device to | |
2355 | * use and recorded in dlist->raid_disk; | |
5f8097be NB |
2356 | */ |
2357 | struct dl *dl; | |
2358 | struct ddf_super *ddf = st->sb; | |
2359 | struct vd_config *vc; | |
2360 | __u64 *lba_offset; | |
f21e18ca N |
2361 | unsigned int working; |
2362 | unsigned int i; | |
59e36268 NB |
2363 | unsigned long long blocks, pos, esize; |
2364 | struct extent *ex; | |
5f8097be | 2365 | |
8592f29d N |
2366 | if (fd == -1) { |
2367 | for (dl = ddf->dlist; dl ; dl = dl->next) | |
2368 | if (dl->raiddisk == dk->raid_disk) | |
2369 | break; | |
2370 | } else { | |
2371 | for (dl = ddf->dlist; dl ; dl = dl->next) | |
2372 | if (dl->major == dk->major && | |
2373 | dl->minor == dk->minor) | |
2374 | break; | |
2375 | } | |
5f8097be NB |
2376 | if (!dl || ! (dk->state & (1<<MD_DISK_SYNC))) |
2377 | return; | |
2378 | ||
d2ca6449 NB |
2379 | vc = &ddf->currentconf->conf; |
2380 | lba_offset = ddf->currentconf->lba_offset; | |
59e36268 NB |
2381 | |
2382 | ex = get_extents(ddf, dl); | |
2383 | if (!ex) | |
2384 | return; | |
2385 | ||
2386 | i = 0; pos = 0; | |
2387 | blocks = __be64_to_cpu(vc->blocks); | |
d2ca6449 NB |
2388 | if (ddf->currentconf->block_sizes) |
2389 | blocks = ddf->currentconf->block_sizes[dk->raid_disk]; | |
59e36268 NB |
2390 | |
2391 | do { | |
2392 | esize = ex[i].start - pos; | |
2393 | if (esize >= blocks) | |
2394 | break; | |
2395 | pos = ex[i].start + ex[i].size; | |
2396 | i++; | |
2397 | } while (ex[i-1].size); | |
2398 | ||
2399 | free(ex); | |
2400 | if (esize < blocks) | |
2401 | return; | |
2402 | ||
d2ca6449 | 2403 | ddf->currentdev = dk->raid_disk; |
5f8097be | 2404 | vc->phys_refnum[dk->raid_disk] = dl->disk.refnum; |
59e36268 | 2405 | lba_offset[dk->raid_disk] = __cpu_to_be64(pos); |
5f8097be | 2406 | |
f21e18ca | 2407 | for (i = 0; i < ddf->max_part ; i++) |
5575e7d9 NB |
2408 | if (dl->vlist[i] == NULL) |
2409 | break; | |
2410 | if (i == ddf->max_part) | |
2411 | return; | |
d2ca6449 | 2412 | dl->vlist[i] = ddf->currentconf; |
5f8097be | 2413 | |
8592f29d N |
2414 | if (fd >= 0) |
2415 | dl->fd = fd; | |
2416 | if (devname) | |
2417 | dl->devname = devname; | |
7a7cc504 NB |
2418 | |
2419 | /* Check how many working raid_disks, and if we can mark | |
2420 | * array as optimal yet | |
2421 | */ | |
2422 | working = 0; | |
5575e7d9 | 2423 | |
f21e18ca | 2424 | for (i = 0; i < __be16_to_cpu(vc->prim_elmnt_count); i++) |
7a7cc504 NB |
2425 | if (vc->phys_refnum[i] != 0xffffffff) |
2426 | working++; | |
59e36268 | 2427 | |
5575e7d9 | 2428 | /* Find which virtual_entry */ |
d2ca6449 | 2429 | i = ddf->currentconf->vcnum; |
7a7cc504 | 2430 | if (working == __be16_to_cpu(vc->prim_elmnt_count)) |
5575e7d9 NB |
2431 | ddf->virt->entries[i].state = |
2432 | (ddf->virt->entries[i].state & ~DDF_state_mask) | |
7a7cc504 NB |
2433 | | DDF_state_optimal; |
2434 | ||
2435 | if (vc->prl == DDF_RAID6 && | |
2436 | working+1 == __be16_to_cpu(vc->prim_elmnt_count)) | |
5575e7d9 NB |
2437 | ddf->virt->entries[i].state = |
2438 | (ddf->virt->entries[i].state & ~DDF_state_mask) | |
7a7cc504 | 2439 | | DDF_state_part_optimal; |
5575e7d9 NB |
2440 | |
2441 | ddf->phys->entries[dl->pdnum].type &= ~__cpu_to_be16(DDF_Global_Spare); | |
2442 | ddf->phys->entries[dl->pdnum].type |= __cpu_to_be16(DDF_Active_in_VD); | |
7d5a7ff3 | 2443 | ddf_set_updates_pending(ddf); |
5f8097be NB |
2444 | } |
2445 | ||
a322f70c DW |
2446 | /* add a device to a container, either while creating it or while |
2447 | * expanding a pre-existing container | |
2448 | */ | |
f20c3968 | 2449 | static int add_to_super_ddf(struct supertype *st, |
72ca9bcf N |
2450 | mdu_disk_info_t *dk, int fd, char *devname, |
2451 | unsigned long long data_offset) | |
a322f70c DW |
2452 | { |
2453 | struct ddf_super *ddf = st->sb; | |
2454 | struct dl *dd; | |
2455 | time_t now; | |
2456 | struct tm *tm; | |
2457 | unsigned long long size; | |
2458 | struct phys_disk_entry *pde; | |
f21e18ca | 2459 | unsigned int n, i; |
a322f70c | 2460 | struct stat stb; |
90fa1a29 | 2461 | __u32 *tptr; |
a322f70c | 2462 | |
78e44928 NB |
2463 | if (ddf->currentconf) { |
2464 | add_to_super_ddf_bvd(st, dk, fd, devname); | |
f20c3968 | 2465 | return 0; |
78e44928 NB |
2466 | } |
2467 | ||
a322f70c DW |
2468 | /* This is device numbered dk->number. We need to create |
2469 | * a phys_disk entry and a more detailed disk_data entry. | |
2470 | */ | |
2471 | fstat(fd, &stb); | |
3d2c4fc7 DW |
2472 | if (posix_memalign((void**)&dd, 512, |
2473 | sizeof(*dd) + sizeof(dd->vlist[0]) * ddf->max_part) != 0) { | |
e7b84f9d N |
2474 | pr_err("%s could allocate buffer for new disk, aborting\n", |
2475 | __func__); | |
f20c3968 | 2476 | return 1; |
3d2c4fc7 | 2477 | } |
a322f70c DW |
2478 | dd->major = major(stb.st_rdev); |
2479 | dd->minor = minor(stb.st_rdev); | |
2480 | dd->devname = devname; | |
a322f70c | 2481 | dd->fd = fd; |
b2280677 | 2482 | dd->spare = NULL; |
a322f70c DW |
2483 | |
2484 | dd->disk.magic = DDF_PHYS_DATA_MAGIC; | |
2485 | now = time(0); | |
2486 | tm = localtime(&now); | |
2487 | sprintf(dd->disk.guid, "%8s%04d%02d%02d", | |
2488 | T10, tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday); | |
90fa1a29 JS |
2489 | tptr = (__u32 *)(dd->disk.guid + 16); |
2490 | *tptr++ = random32(); | |
2491 | *tptr = random32(); | |
a322f70c | 2492 | |
59e36268 NB |
2493 | do { |
2494 | /* Cannot be bothered finding a CRC of some irrelevant details*/ | |
bfb7ea78 | 2495 | dd->disk.refnum = random32(); |
f21e18ca N |
2496 | for (i = __be16_to_cpu(ddf->active->max_pd_entries); |
2497 | i > 0; i--) | |
2498 | if (ddf->phys->entries[i-1].refnum == dd->disk.refnum) | |
59e36268 | 2499 | break; |
f21e18ca | 2500 | } while (i > 0); |
59e36268 | 2501 | |
a322f70c DW |
2502 | dd->disk.forced_ref = 1; |
2503 | dd->disk.forced_guid = 1; | |
2504 | memset(dd->disk.vendor, ' ', 32); | |
2505 | memcpy(dd->disk.vendor, "Linux", 5); | |
2506 | memset(dd->disk.pad, 0xff, 442); | |
b2280677 | 2507 | for (i = 0; i < ddf->max_part ; i++) |
a322f70c DW |
2508 | dd->vlist[i] = NULL; |
2509 | ||
2510 | n = __be16_to_cpu(ddf->phys->used_pdes); | |
2511 | pde = &ddf->phys->entries[n]; | |
5575e7d9 NB |
2512 | dd->pdnum = n; |
2513 | ||
2cc2983d N |
2514 | if (st->update_tail) { |
2515 | int len = (sizeof(struct phys_disk) + | |
2516 | sizeof(struct phys_disk_entry)); | |
2517 | struct phys_disk *pd; | |
2518 | ||
503975b9 | 2519 | pd = xmalloc(len); |
2cc2983d N |
2520 | pd->magic = DDF_PHYS_RECORDS_MAGIC; |
2521 | pd->used_pdes = __cpu_to_be16(n); | |
2522 | pde = &pd->entries[0]; | |
2523 | dd->mdupdate = pd; | |
2524 | } else { | |
2525 | n++; | |
2526 | ddf->phys->used_pdes = __cpu_to_be16(n); | |
2527 | } | |
a322f70c DW |
2528 | |
2529 | memcpy(pde->guid, dd->disk.guid, DDF_GUID_LEN); | |
2530 | pde->refnum = dd->disk.refnum; | |
5575e7d9 | 2531 | pde->type = __cpu_to_be16(DDF_Forced_PD_GUID | DDF_Global_Spare); |
a322f70c DW |
2532 | pde->state = __cpu_to_be16(DDF_Online); |
2533 | get_dev_size(fd, NULL, &size); | |
2534 | /* We are required to reserve 32Meg, and record the size in sectors */ | |
2535 | pde->config_size = __cpu_to_be64( (size - 32*1024*1024) / 512); | |
2536 | sprintf(pde->path, "%17.17s","Information: nil") ; | |
2537 | memset(pde->pad, 0xff, 6); | |
2538 | ||
d2ca6449 | 2539 | dd->size = size >> 9; |
2cc2983d N |
2540 | if (st->update_tail) { |
2541 | dd->next = ddf->add_list; | |
2542 | ddf->add_list = dd; | |
2543 | } else { | |
2544 | dd->next = ddf->dlist; | |
2545 | ddf->dlist = dd; | |
7d5a7ff3 | 2546 | ddf_set_updates_pending(ddf); |
2cc2983d | 2547 | } |
f20c3968 DW |
2548 | |
2549 | return 0; | |
a322f70c DW |
2550 | } |
2551 | ||
4dd968cc N |
2552 | static int remove_from_super_ddf(struct supertype *st, mdu_disk_info_t *dk) |
2553 | { | |
2554 | struct ddf_super *ddf = st->sb; | |
2555 | struct dl *dl; | |
2556 | ||
2557 | /* mdmon has noticed that this disk (dk->major/dk->minor) has | |
2558 | * disappeared from the container. | |
2559 | * We need to arrange that it disappears from the metadata and | |
2560 | * internal data structures too. | |
2561 | * Most of the work is done by ddf_process_update which edits | |
2562 | * the metadata and closes the file handle and attaches the memory | |
2563 | * where free_updates will free it. | |
2564 | */ | |
2565 | for (dl = ddf->dlist; dl ; dl = dl->next) | |
2566 | if (dl->major == dk->major && | |
2567 | dl->minor == dk->minor) | |
2568 | break; | |
2569 | if (!dl) | |
2570 | return -1; | |
2571 | ||
2572 | if (st->update_tail) { | |
2573 | int len = (sizeof(struct phys_disk) + | |
2574 | sizeof(struct phys_disk_entry)); | |
2575 | struct phys_disk *pd; | |
2576 | ||
503975b9 | 2577 | pd = xmalloc(len); |
4dd968cc N |
2578 | pd->magic = DDF_PHYS_RECORDS_MAGIC; |
2579 | pd->used_pdes = __cpu_to_be16(dl->pdnum); | |
2580 | pd->entries[0].state = __cpu_to_be16(DDF_Missing); | |
2581 | append_metadata_update(st, pd, len); | |
2582 | } | |
2583 | return 0; | |
2584 | } | |
2585 | ||
a322f70c DW |
2586 | /* |
2587 | * This is the write_init_super method for a ddf container. It is | |
2588 | * called when creating a container or adding another device to a | |
2589 | * container. | |
2590 | */ | |
42d5dfd9 | 2591 | #define NULL_CONF_SZ 4096 |
18a2f463 | 2592 | |
e3c2a365 | 2593 | static unsigned int get_pd_index_from_refnum(const struct vcl *vc, |
2594 | __u32 refnum, unsigned int nmax, | |
2595 | const struct vd_config **bvd, | |
2596 | unsigned int *idx); | |
2597 | ||
7f798aca | 2598 | static int __write_ddf_structure(struct dl *d, struct ddf_super *ddf, __u8 type, |
2599 | char *null_aligned) | |
a322f70c | 2600 | { |
7f798aca | 2601 | unsigned long long sector; |
2602 | struct ddf_header *header; | |
2603 | int fd, i, n_config, conf_size; | |
a4057a88 | 2604 | int ret = 0; |
7f798aca | 2605 | |
2606 | fd = d->fd; | |
2607 | ||
2608 | switch (type) { | |
2609 | case DDF_HEADER_PRIMARY: | |
2610 | header = &ddf->primary; | |
2611 | sector = __be64_to_cpu(header->primary_lba); | |
2612 | break; | |
2613 | case DDF_HEADER_SECONDARY: | |
2614 | header = &ddf->secondary; | |
2615 | sector = __be64_to_cpu(header->secondary_lba); | |
2616 | break; | |
2617 | default: | |
2618 | return 0; | |
2619 | } | |
2620 | ||
2621 | header->type = type; | |
a4057a88 | 2622 | header->openflag = 1; |
7f798aca | 2623 | header->crc = calc_crc(header, 512); |
2624 | ||
2625 | lseek64(fd, sector<<9, 0); | |
2626 | if (write(fd, header, 512) < 0) | |
a4057a88 | 2627 | goto out; |
7f798aca | 2628 | |
2629 | ddf->controller.crc = calc_crc(&ddf->controller, 512); | |
2630 | if (write(fd, &ddf->controller, 512) < 0) | |
a4057a88 | 2631 | goto out; |
a322f70c | 2632 | |
7f798aca | 2633 | ddf->phys->crc = calc_crc(ddf->phys, ddf->pdsize); |
2634 | if (write(fd, ddf->phys, ddf->pdsize) < 0) | |
a4057a88 | 2635 | goto out; |
7f798aca | 2636 | ddf->virt->crc = calc_crc(ddf->virt, ddf->vdsize); |
2637 | if (write(fd, ddf->virt, ddf->vdsize) < 0) | |
a4057a88 | 2638 | goto out; |
7f798aca | 2639 | |
2640 | /* Now write lots of config records. */ | |
2641 | n_config = ddf->max_part; | |
2642 | conf_size = ddf->conf_rec_len * 512; | |
2643 | for (i = 0 ; i <= n_config ; i++) { | |
e3c2a365 | 2644 | struct vcl *c; |
2645 | struct vd_config *vdc = NULL; | |
2646 | if (i == n_config) { | |
7f798aca | 2647 | c = (struct vcl *)d->spare; |
e3c2a365 | 2648 | if (c) |
2649 | vdc = &c->conf; | |
2650 | } else { | |
2651 | unsigned int dummy; | |
2652 | c = d->vlist[i]; | |
2653 | if (c) | |
2654 | get_pd_index_from_refnum( | |
2655 | c, d->disk.refnum, | |
2656 | ddf->mppe, | |
2657 | (const struct vd_config **)&vdc, | |
2658 | &dummy); | |
2659 | } | |
7f798aca | 2660 | if (c) { |
dacf3dc5 | 2661 | vdc->seqnum = header->seq; |
e3c2a365 | 2662 | vdc->crc = calc_crc(vdc, conf_size); |
2663 | if (write(fd, vdc, conf_size) < 0) | |
7f798aca | 2664 | break; |
2665 | } else { | |
2666 | unsigned int togo = conf_size; | |
2667 | while (togo > NULL_CONF_SZ) { | |
2668 | if (write(fd, null_aligned, NULL_CONF_SZ) < 0) | |
2669 | break; | |
2670 | togo -= NULL_CONF_SZ; | |
2671 | } | |
2672 | if (write(fd, null_aligned, togo) < 0) | |
2673 | break; | |
2674 | } | |
2675 | } | |
2676 | if (i <= n_config) | |
a4057a88 | 2677 | goto out; |
7f798aca | 2678 | |
2679 | d->disk.crc = calc_crc(&d->disk, 512); | |
2680 | if (write(fd, &d->disk, 512) < 0) | |
a4057a88 | 2681 | goto out; |
7f798aca | 2682 | |
a4057a88 | 2683 | ret = 1; |
2684 | out: | |
2685 | header->openflag = 0; | |
2686 | header->crc = calc_crc(header, 512); | |
2687 | ||
2688 | lseek64(fd, sector<<9, 0); | |
2689 | if (write(fd, header, 512) < 0) | |
2690 | ret = 0; | |
2691 | ||
2692 | return ret; | |
7f798aca | 2693 | } |
2694 | ||
2695 | static int __write_init_super_ddf(struct supertype *st) | |
2696 | { | |
a322f70c | 2697 | struct ddf_super *ddf = st->sb; |
a322f70c | 2698 | struct dl *d; |
175593bf DW |
2699 | int attempts = 0; |
2700 | int successes = 0; | |
7f798aca | 2701 | unsigned long long size; |
42d5dfd9 | 2702 | char *null_aligned; |
0175cbf6 | 2703 | __u32 seq; |
42d5dfd9 | 2704 | |
7d5a7ff3 | 2705 | pr_state(ddf, __func__); |
42d5dfd9 JS |
2706 | if (posix_memalign((void**)&null_aligned, 4096, NULL_CONF_SZ) != 0) { |
2707 | return -ENOMEM; | |
2708 | } | |
2709 | memset(null_aligned, 0xff, NULL_CONF_SZ); | |
a322f70c | 2710 | |
dc9e279c | 2711 | seq = ddf->active->seq + 1; |
0175cbf6 | 2712 | |
175593bf DW |
2713 | /* try to write updated metadata, |
2714 | * if we catch a failure move on to the next disk | |
2715 | */ | |
a322f70c DW |
2716 | for (d = ddf->dlist; d; d=d->next) { |
2717 | int fd = d->fd; | |
2718 | ||
2719 | if (fd < 0) | |
2720 | continue; | |
2721 | ||
175593bf | 2722 | attempts++; |
a322f70c DW |
2723 | /* We need to fill in the primary, (secondary) and workspace |
2724 | * lba's in the headers, set their checksums, | |
2725 | * Also checksum phys, virt.... | |
2726 | * | |
2727 | * Then write everything out, finally the anchor is written. | |
2728 | */ | |
2729 | get_dev_size(fd, NULL, &size); | |
2730 | size /= 512; | |
097bcf00 | 2731 | if (d->workspace_lba != 0) |
2732 | ddf->anchor.workspace_lba = d->workspace_lba; | |
2733 | else | |
2734 | ddf->anchor.workspace_lba = | |
2735 | __cpu_to_be64(size - 32*1024*2); | |
2736 | if (d->primary_lba != 0) | |
2737 | ddf->anchor.primary_lba = d->primary_lba; | |
2738 | else | |
2739 | ddf->anchor.primary_lba = | |
2740 | __cpu_to_be64(size - 16*1024*2); | |
2741 | if (d->secondary_lba != 0) | |
2742 | ddf->anchor.secondary_lba = d->secondary_lba; | |
2743 | else | |
2744 | ddf->anchor.secondary_lba = | |
2745 | __cpu_to_be64(size - 32*1024*2); | |
0175cbf6 | 2746 | ddf->anchor.seq = seq; |
a322f70c DW |
2747 | memcpy(&ddf->primary, &ddf->anchor, 512); |
2748 | memcpy(&ddf->secondary, &ddf->anchor, 512); | |
2749 | ||
2750 | ddf->anchor.openflag = 0xFF; /* 'open' means nothing */ | |
2751 | ddf->anchor.seq = 0xFFFFFFFF; /* no sequencing in anchor */ | |
2752 | ddf->anchor.crc = calc_crc(&ddf->anchor, 512); | |
2753 | ||
7f798aca | 2754 | if (!__write_ddf_structure(d, ddf, DDF_HEADER_PRIMARY, |
2755 | null_aligned)) | |
175593bf | 2756 | continue; |
a322f70c | 2757 | |
7f798aca | 2758 | if (!__write_ddf_structure(d, ddf, DDF_HEADER_SECONDARY, |
2759 | null_aligned)) | |
175593bf | 2760 | continue; |
a322f70c | 2761 | |
a322f70c | 2762 | lseek64(fd, (size-1)*512, SEEK_SET); |
175593bf DW |
2763 | if (write(fd, &ddf->anchor, 512) < 0) |
2764 | continue; | |
2765 | successes++; | |
2766 | } | |
42d5dfd9 | 2767 | free(null_aligned); |
175593bf | 2768 | |
175593bf | 2769 | return attempts != successes; |
a322f70c | 2770 | } |
7a7cc504 NB |
2771 | |
2772 | static int write_init_super_ddf(struct supertype *st) | |
2773 | { | |
9b1fb677 DW |
2774 | struct ddf_super *ddf = st->sb; |
2775 | struct vcl *currentconf = ddf->currentconf; | |
2776 | ||
2777 | /* we are done with currentconf reset it to point st at the container */ | |
2778 | ddf->currentconf = NULL; | |
edd8d13c NB |
2779 | |
2780 | if (st->update_tail) { | |
2781 | /* queue the virtual_disk and vd_config as metadata updates */ | |
2782 | struct virtual_disk *vd; | |
2783 | struct vd_config *vc; | |
edd8d13c NB |
2784 | int len; |
2785 | ||
9b1fb677 | 2786 | if (!currentconf) { |
2cc2983d N |
2787 | int len = (sizeof(struct phys_disk) + |
2788 | sizeof(struct phys_disk_entry)); | |
2789 | ||
2790 | /* adding a disk to the container. */ | |
2791 | if (!ddf->add_list) | |
2792 | return 0; | |
2793 | ||
2794 | append_metadata_update(st, ddf->add_list->mdupdate, len); | |
2795 | ddf->add_list->mdupdate = NULL; | |
2796 | return 0; | |
2797 | } | |
2798 | ||
2799 | /* Newly created VD */ | |
2800 | ||
edd8d13c NB |
2801 | /* First the virtual disk. We have a slightly fake header */ |
2802 | len = sizeof(struct virtual_disk) + sizeof(struct virtual_entry); | |
503975b9 | 2803 | vd = xmalloc(len); |
edd8d13c | 2804 | *vd = *ddf->virt; |
9b1fb677 DW |
2805 | vd->entries[0] = ddf->virt->entries[currentconf->vcnum]; |
2806 | vd->populated_vdes = __cpu_to_be16(currentconf->vcnum); | |
edd8d13c NB |
2807 | append_metadata_update(st, vd, len); |
2808 | ||
2809 | /* Then the vd_config */ | |
2810 | len = ddf->conf_rec_len * 512; | |
503975b9 | 2811 | vc = xmalloc(len); |
9b1fb677 | 2812 | memcpy(vc, ¤tconf->conf, len); |
edd8d13c NB |
2813 | append_metadata_update(st, vc, len); |
2814 | ||
2815 | /* FIXME I need to close the fds! */ | |
2816 | return 0; | |
613b0d17 | 2817 | } else { |
d682f344 N |
2818 | struct dl *d; |
2819 | for (d = ddf->dlist; d; d=d->next) | |
ba728be7 | 2820 | while (Kill(d->devname, NULL, 0, -1, 1) == 0); |
1cc7f4fe | 2821 | return __write_init_super_ddf(st); |
d682f344 | 2822 | } |
7a7cc504 NB |
2823 | } |
2824 | ||
a322f70c DW |
2825 | #endif |
2826 | ||
387fcd59 N |
2827 | static __u64 avail_size_ddf(struct supertype *st, __u64 devsize, |
2828 | unsigned long long data_offset) | |
a322f70c DW |
2829 | { |
2830 | /* We must reserve the last 32Meg */ | |
2831 | if (devsize <= 32*1024*2) | |
2832 | return 0; | |
2833 | return devsize - 32*1024*2; | |
2834 | } | |
2835 | ||
2836 | #ifndef MDASSEMBLE | |
8592f29d N |
2837 | |
2838 | static int reserve_space(struct supertype *st, int raiddisks, | |
2839 | unsigned long long size, int chunk, | |
2840 | unsigned long long *freesize) | |
2841 | { | |
2842 | /* Find 'raiddisks' spare extents at least 'size' big (but | |
2843 | * only caring about multiples of 'chunk') and remember | |
2844 | * them. | |
2845 | * If the cannot be found, fail. | |
2846 | */ | |
2847 | struct dl *dl; | |
2848 | struct ddf_super *ddf = st->sb; | |
2849 | int cnt = 0; | |
2850 | ||
2851 | for (dl = ddf->dlist; dl ; dl=dl->next) { | |
613b0d17 | 2852 | dl->raiddisk = -1; |
8592f29d N |
2853 | dl->esize = 0; |
2854 | } | |
2855 | /* Now find largest extent on each device */ | |
2856 | for (dl = ddf->dlist ; dl ; dl=dl->next) { | |
2857 | struct extent *e = get_extents(ddf, dl); | |
2858 | unsigned long long pos = 0; | |
2859 | int i = 0; | |
2860 | int found = 0; | |
2861 | unsigned long long minsize = size; | |
2862 | ||
2863 | if (size == 0) | |
2864 | minsize = chunk; | |
2865 | ||
2866 | if (!e) | |
2867 | continue; | |
2868 | do { | |
2869 | unsigned long long esize; | |
2870 | esize = e[i].start - pos; | |
2871 | if (esize >= minsize) { | |
2872 | found = 1; | |
2873 | minsize = esize; | |
2874 | } | |
2875 | pos = e[i].start + e[i].size; | |
2876 | i++; | |
2877 | } while (e[i-1].size); | |
2878 | if (found) { | |
2879 | cnt++; | |
2880 | dl->esize = minsize; | |
2881 | } | |
2882 | free(e); | |
2883 | } | |
2884 | if (cnt < raiddisks) { | |
e7b84f9d | 2885 | pr_err("not enough devices with space to create array.\n"); |
8592f29d N |
2886 | return 0; /* No enough free spaces large enough */ |
2887 | } | |
2888 | if (size == 0) { | |
2889 | /* choose the largest size of which there are at least 'raiddisk' */ | |
2890 | for (dl = ddf->dlist ; dl ; dl=dl->next) { | |
2891 | struct dl *dl2; | |
2892 | if (dl->esize <= size) | |
2893 | continue; | |
2894 | /* This is bigger than 'size', see if there are enough */ | |
2895 | cnt = 0; | |
7b80ad6a | 2896 | for (dl2 = ddf->dlist; dl2 ; dl2=dl2->next) |
8592f29d N |
2897 | if (dl2->esize >= dl->esize) |
2898 | cnt++; | |
2899 | if (cnt >= raiddisks) | |
2900 | size = dl->esize; | |
2901 | } | |
2902 | if (chunk) { | |
2903 | size = size / chunk; | |
2904 | size *= chunk; | |
2905 | } | |
2906 | *freesize = size; | |
2907 | if (size < 32) { | |
e7b84f9d | 2908 | pr_err("not enough spare devices to create array.\n"); |
8592f29d N |
2909 | return 0; |
2910 | } | |
2911 | } | |
2912 | /* We have a 'size' of which there are enough spaces. | |
2913 | * We simply do a first-fit */ | |
2914 | cnt = 0; | |
2915 | for (dl = ddf->dlist ; dl && cnt < raiddisks ; dl=dl->next) { | |
2916 | if (dl->esize < size) | |
2917 | continue; | |
613b0d17 | 2918 | |
8592f29d N |
2919 | dl->raiddisk = cnt; |
2920 | cnt++; | |
2921 | } | |
2922 | return 1; | |
2923 | } | |
2924 | ||
2c514b71 NB |
2925 | static int |
2926 | validate_geometry_ddf_container(struct supertype *st, | |
2927 | int level, int layout, int raiddisks, | |
2928 | int chunk, unsigned long long size, | |
af4348dd | 2929 | unsigned long long data_offset, |
2c514b71 NB |
2930 | char *dev, unsigned long long *freesize, |
2931 | int verbose); | |
78e44928 NB |
2932 | |
2933 | static int validate_geometry_ddf_bvd(struct supertype *st, | |
2934 | int level, int layout, int raiddisks, | |
c21e737b | 2935 | int *chunk, unsigned long long size, |
af4348dd | 2936 | unsigned long long data_offset, |
2c514b71 NB |
2937 | char *dev, unsigned long long *freesize, |
2938 | int verbose); | |
78e44928 NB |
2939 | |
2940 | static int validate_geometry_ddf(struct supertype *st, | |
2c514b71 | 2941 | int level, int layout, int raiddisks, |
c21e737b | 2942 | int *chunk, unsigned long long size, |
af4348dd | 2943 | unsigned long long data_offset, |
2c514b71 NB |
2944 | char *dev, unsigned long long *freesize, |
2945 | int verbose) | |
a322f70c DW |
2946 | { |
2947 | int fd; | |
2948 | struct mdinfo *sra; | |
2949 | int cfd; | |
2950 | ||
2951 | /* ddf potentially supports lots of things, but it depends on | |
2952 | * what devices are offered (and maybe kernel version?) | |
2953 | * If given unused devices, we will make a container. | |
2954 | * If given devices in a container, we will make a BVD. | |
2955 | * If given BVDs, we make an SVD, changing all the GUIDs in the process. | |
2956 | */ | |
2957 | ||
bb7295f1 N |
2958 | if (chunk && *chunk == UnSet) |
2959 | *chunk = DEFAULT_CHUNK; | |
2960 | ||
542ef4ec | 2961 | if (level == -1000000) level = LEVEL_CONTAINER; |
a322f70c | 2962 | if (level == LEVEL_CONTAINER) { |
78e44928 NB |
2963 | /* Must be a fresh device to add to a container */ |
2964 | return validate_geometry_ddf_container(st, level, layout, | |
c21e737b | 2965 | raiddisks, chunk?*chunk:0, |
af4348dd N |
2966 | size, data_offset, dev, |
2967 | freesize, | |
2c514b71 | 2968 | verbose); |
5f8097be NB |
2969 | } |
2970 | ||
78e44928 NB |
2971 | if (!dev) { |
2972 | /* Initial sanity check. Exclude illegal levels. */ | |
2973 | int i; | |
2974 | for (i=0; ddf_level_num[i].num1 != MAXINT; i++) | |
2975 | if (ddf_level_num[i].num2 == level) | |
2976 | break; | |
b42f577a N |
2977 | if (ddf_level_num[i].num1 == MAXINT) { |
2978 | if (verbose) | |
e7b84f9d | 2979 | pr_err("DDF does not support level %d arrays\n", |
613b0d17 | 2980 | level); |
78e44928 | 2981 | return 0; |
b42f577a | 2982 | } |
78e44928 | 2983 | /* Should check layout? etc */ |
8592f29d N |
2984 | |
2985 | if (st->sb && freesize) { | |
2986 | /* --create was given a container to create in. | |
2987 | * So we need to check that there are enough | |
2988 | * free spaces and return the amount of space. | |
2989 | * We may as well remember which drives were | |
2990 | * chosen so that add_to_super/getinfo_super | |
2991 | * can return them. | |
2992 | */ | |
c21e737b | 2993 | return reserve_space(st, raiddisks, size, chunk?*chunk:0, freesize); |
8592f29d | 2994 | } |
a322f70c | 2995 | return 1; |
78e44928 | 2996 | } |
a322f70c | 2997 | |
8592f29d N |
2998 | if (st->sb) { |
2999 | /* A container has already been opened, so we are | |
3000 | * creating in there. Maybe a BVD, maybe an SVD. | |
3001 | * Should make a distinction one day. | |
3002 | */ | |
3003 | return validate_geometry_ddf_bvd(st, level, layout, raiddisks, | |
af4348dd N |
3004 | chunk, size, data_offset, dev, |
3005 | freesize, | |
8592f29d N |
3006 | verbose); |
3007 | } | |
78e44928 NB |
3008 | /* This is the first device for the array. |
3009 | * If it is a container, we read it in and do automagic allocations, | |
3010 | * no other devices should be given. | |
3011 | * Otherwise it must be a member device of a container, and we | |
3012 | * do manual allocation. | |
3013 | * Later we should check for a BVD and make an SVD. | |
a322f70c | 3014 | */ |
a322f70c DW |
3015 | fd = open(dev, O_RDONLY|O_EXCL, 0); |
3016 | if (fd >= 0) { | |
4dd2df09 | 3017 | sra = sysfs_read(fd, NULL, GET_VERSION); |
a322f70c DW |
3018 | close(fd); |
3019 | if (sra && sra->array.major_version == -1 && | |
78e44928 NB |
3020 | strcmp(sra->text_version, "ddf") == 0) { |
3021 | ||
3022 | /* load super */ | |
3023 | /* find space for 'n' devices. */ | |
3024 | /* remember the devices */ | |
3025 | /* Somehow return the fact that we have enough */ | |
a322f70c DW |
3026 | } |
3027 | ||
2c514b71 | 3028 | if (verbose) |
e7b84f9d N |
3029 | pr_err("ddf: Cannot create this array " |
3030 | "on device %s - a container is required.\n", | |
3031 | dev); | |
a322f70c DW |
3032 | return 0; |
3033 | } | |
3034 | if (errno != EBUSY || (fd = open(dev, O_RDONLY, 0)) < 0) { | |
2c514b71 | 3035 | if (verbose) |
e7b84f9d | 3036 | pr_err("ddf: Cannot open %s: %s\n", |
613b0d17 | 3037 | dev, strerror(errno)); |
a322f70c DW |
3038 | return 0; |
3039 | } | |
3040 | /* Well, it is in use by someone, maybe a 'ddf' container. */ | |
3041 | cfd = open_container(fd); | |
3042 | if (cfd < 0) { | |
3043 | close(fd); | |
2c514b71 | 3044 | if (verbose) |
e7b84f9d | 3045 | pr_err("ddf: Cannot use %s: %s\n", |
613b0d17 | 3046 | dev, strerror(EBUSY)); |
a322f70c DW |
3047 | return 0; |
3048 | } | |
4dd2df09 | 3049 | sra = sysfs_read(cfd, NULL, GET_VERSION); |
a322f70c DW |
3050 | close(fd); |
3051 | if (sra && sra->array.major_version == -1 && | |
3052 | strcmp(sra->text_version, "ddf") == 0) { | |
3053 | /* This is a member of a ddf container. Load the container | |
3054 | * and try to create a bvd | |
3055 | */ | |
3056 | struct ddf_super *ddf; | |
e1902a7b | 3057 | if (load_super_ddf_all(st, cfd, (void **)&ddf, NULL) == 0) { |
5f8097be | 3058 | st->sb = ddf; |
4dd2df09 | 3059 | strcpy(st->container_devnm, fd2devnm(cfd)); |
a322f70c | 3060 | close(cfd); |
78e44928 | 3061 | return validate_geometry_ddf_bvd(st, level, layout, |
a322f70c | 3062 | raiddisks, chunk, size, |
af4348dd | 3063 | data_offset, |
2c514b71 NB |
3064 | dev, freesize, |
3065 | verbose); | |
a322f70c DW |
3066 | } |
3067 | close(cfd); | |
c42ec1ed DW |
3068 | } else /* device may belong to a different container */ |
3069 | return 0; | |
3070 | ||
a322f70c DW |
3071 | return 1; |
3072 | } | |
3073 | ||
2c514b71 NB |
3074 | static int |
3075 | validate_geometry_ddf_container(struct supertype *st, | |
3076 | int level, int layout, int raiddisks, | |
3077 | int chunk, unsigned long long size, | |
af4348dd | 3078 | unsigned long long data_offset, |
2c514b71 NB |
3079 | char *dev, unsigned long long *freesize, |
3080 | int verbose) | |
a322f70c DW |
3081 | { |
3082 | int fd; | |
3083 | unsigned long long ldsize; | |
3084 | ||
3085 | if (level != LEVEL_CONTAINER) | |
3086 | return 0; | |
3087 | if (!dev) | |
3088 | return 1; | |
3089 | ||
3090 | fd = open(dev, O_RDONLY|O_EXCL, 0); | |
3091 | if (fd < 0) { | |
2c514b71 | 3092 | if (verbose) |
e7b84f9d | 3093 | pr_err("ddf: Cannot open %s: %s\n", |
613b0d17 | 3094 | dev, strerror(errno)); |
a322f70c DW |
3095 | return 0; |
3096 | } | |
3097 | if (!get_dev_size(fd, dev, &ldsize)) { | |
3098 | close(fd); | |
3099 | return 0; | |
3100 | } | |
3101 | close(fd); | |
3102 | ||
387fcd59 | 3103 | *freesize = avail_size_ddf(st, ldsize >> 9, INVALID_SECTORS); |
ea17e7aa N |
3104 | if (*freesize == 0) |
3105 | return 0; | |
a322f70c DW |
3106 | |
3107 | return 1; | |
3108 | } | |
3109 | ||
78e44928 NB |
3110 | static int validate_geometry_ddf_bvd(struct supertype *st, |
3111 | int level, int layout, int raiddisks, | |
c21e737b | 3112 | int *chunk, unsigned long long size, |
af4348dd | 3113 | unsigned long long data_offset, |
2c514b71 NB |
3114 | char *dev, unsigned long long *freesize, |
3115 | int verbose) | |
a322f70c DW |
3116 | { |
3117 | struct stat stb; | |
3118 | struct ddf_super *ddf = st->sb; | |
3119 | struct dl *dl; | |
5f8097be NB |
3120 | unsigned long long pos = 0; |
3121 | unsigned long long maxsize; | |
3122 | struct extent *e; | |
3123 | int i; | |
a322f70c | 3124 | /* ddf/bvd supports lots of things, but not containers */ |
b42f577a N |
3125 | if (level == LEVEL_CONTAINER) { |
3126 | if (verbose) | |
e7b84f9d | 3127 | pr_err("DDF cannot create a container within an container\n"); |
a322f70c | 3128 | return 0; |
b42f577a | 3129 | } |
a322f70c DW |
3130 | /* We must have the container info already read in. */ |
3131 | if (!ddf) | |
3132 | return 0; | |
3133 | ||
5f8097be NB |
3134 | if (!dev) { |
3135 | /* General test: make sure there is space for | |
3136 | * 'raiddisks' device extents of size 'size'. | |
3137 | */ | |
3138 | unsigned long long minsize = size; | |
3139 | int dcnt = 0; | |
3140 | if (minsize == 0) | |
3141 | minsize = 8; | |
3142 | for (dl = ddf->dlist; dl ; dl = dl->next) | |
3143 | { | |
3144 | int found = 0; | |
7e1432fb | 3145 | pos = 0; |
5f8097be NB |
3146 | |
3147 | i = 0; | |
3148 | e = get_extents(ddf, dl); | |
3149 | if (!e) continue; | |
3150 | do { | |
3151 | unsigned long long esize; | |
3152 | esize = e[i].start - pos; | |
3153 | if (esize >= minsize) | |
3154 | found = 1; | |
3155 | pos = e[i].start + e[i].size; | |
3156 | i++; | |
3157 | } while (e[i-1].size); | |
3158 | if (found) | |
3159 | dcnt++; | |
3160 | free(e); | |
3161 | } | |
3162 | if (dcnt < raiddisks) { | |
2c514b71 | 3163 | if (verbose) |
e7b84f9d N |
3164 | pr_err("ddf: Not enough devices with " |
3165 | "space for this array (%d < %d)\n", | |
3166 | dcnt, raiddisks); | |
5f8097be NB |
3167 | return 0; |
3168 | } | |
3169 | return 1; | |
3170 | } | |
a322f70c DW |
3171 | /* This device must be a member of the set */ |
3172 | if (stat(dev, &stb) < 0) | |
3173 | return 0; | |
3174 | if ((S_IFMT & stb.st_mode) != S_IFBLK) | |
3175 | return 0; | |
3176 | for (dl = ddf->dlist ; dl ; dl = dl->next) { | |
f21e18ca N |
3177 | if (dl->major == (int)major(stb.st_rdev) && |
3178 | dl->minor == (int)minor(stb.st_rdev)) | |
a322f70c DW |
3179 | break; |
3180 | } | |
5f8097be | 3181 | if (!dl) { |
2c514b71 | 3182 | if (verbose) |
e7b84f9d | 3183 | pr_err("ddf: %s is not in the " |
613b0d17 N |
3184 | "same DDF set\n", |
3185 | dev); | |
5f8097be NB |
3186 | return 0; |
3187 | } | |
3188 | e = get_extents(ddf, dl); | |
3189 | maxsize = 0; | |
3190 | i = 0; | |
3191 | if (e) do { | |
613b0d17 N |
3192 | unsigned long long esize; |
3193 | esize = e[i].start - pos; | |
3194 | if (esize >= maxsize) | |
3195 | maxsize = esize; | |
3196 | pos = e[i].start + e[i].size; | |
3197 | i++; | |
3198 | } while (e[i-1].size); | |
5f8097be | 3199 | *freesize = maxsize; |
a322f70c DW |
3200 | // FIXME here I am |
3201 | ||
3202 | return 1; | |
3203 | } | |
59e36268 | 3204 | |
a322f70c | 3205 | static int load_super_ddf_all(struct supertype *st, int fd, |
e1902a7b | 3206 | void **sbp, char *devname) |
a322f70c DW |
3207 | { |
3208 | struct mdinfo *sra; | |
3209 | struct ddf_super *super; | |
3210 | struct mdinfo *sd, *best = NULL; | |
3211 | int bestseq = 0; | |
3212 | int seq; | |
3213 | char nm[20]; | |
3214 | int dfd; | |
3215 | ||
b526e52d | 3216 | sra = sysfs_read(fd, 0, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE); |
a322f70c DW |
3217 | if (!sra) |
3218 | return 1; | |
3219 | if (sra->array.major_version != -1 || | |
3220 | sra->array.minor_version != -2 || | |
3221 | strcmp(sra->text_version, "ddf") != 0) | |
3222 | return 1; | |
3223 | ||
6416d527 | 3224 | if (posix_memalign((void**)&super, 512, sizeof(*super)) != 0) |
a322f70c | 3225 | return 1; |
a2349791 | 3226 | memset(super, 0, sizeof(*super)); |
a322f70c DW |
3227 | |
3228 | /* first, try each device, and choose the best ddf */ | |
3229 | for (sd = sra->devs ; sd ; sd = sd->next) { | |
3230 | int rv; | |
3231 | sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor); | |
7a7cc504 NB |
3232 | dfd = dev_open(nm, O_RDONLY); |
3233 | if (dfd < 0) | |
a322f70c DW |
3234 | return 2; |
3235 | rv = load_ddf_headers(dfd, super, NULL); | |
7a7cc504 | 3236 | close(dfd); |
a322f70c DW |
3237 | if (rv == 0) { |
3238 | seq = __be32_to_cpu(super->active->seq); | |
3239 | if (super->active->openflag) | |
3240 | seq--; | |
3241 | if (!best || seq > bestseq) { | |
3242 | bestseq = seq; | |
3243 | best = sd; | |
3244 | } | |
3245 | } | |
3246 | } | |
3247 | if (!best) | |
3248 | return 1; | |
3249 | /* OK, load this ddf */ | |
3250 | sprintf(nm, "%d:%d", best->disk.major, best->disk.minor); | |
3251 | dfd = dev_open(nm, O_RDONLY); | |
7a7cc504 | 3252 | if (dfd < 0) |
a322f70c DW |
3253 | return 1; |
3254 | load_ddf_headers(dfd, super, NULL); | |
3255 | load_ddf_global(dfd, super, NULL); | |
3256 | close(dfd); | |
3257 | /* Now we need the device-local bits */ | |
3258 | for (sd = sra->devs ; sd ; sd = sd->next) { | |
3d2c4fc7 DW |
3259 | int rv; |
3260 | ||
a322f70c | 3261 | sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor); |
e1902a7b | 3262 | dfd = dev_open(nm, O_RDWR); |
7a7cc504 | 3263 | if (dfd < 0) |
a322f70c | 3264 | return 2; |
3d2c4fc7 DW |
3265 | rv = load_ddf_headers(dfd, super, NULL); |
3266 | if (rv == 0) | |
e1902a7b | 3267 | rv = load_ddf_local(dfd, super, NULL, 1); |
3d2c4fc7 DW |
3268 | if (rv) |
3269 | return 1; | |
a322f70c | 3270 | } |
33414a01 | 3271 | |
a322f70c DW |
3272 | *sbp = super; |
3273 | if (st->ss == NULL) { | |
78e44928 | 3274 | st->ss = &super_ddf; |
a322f70c DW |
3275 | st->minor_version = 0; |
3276 | st->max_devs = 512; | |
3277 | } | |
4dd2df09 | 3278 | strcpy(st->container_devnm, fd2devnm(fd)); |
a322f70c DW |
3279 | return 0; |
3280 | } | |
2b959fbf N |
3281 | |
3282 | static int load_container_ddf(struct supertype *st, int fd, | |
3283 | char *devname) | |
3284 | { | |
3285 | return load_super_ddf_all(st, fd, &st->sb, devname); | |
3286 | } | |
3287 | ||
0e600426 | 3288 | #endif /* MDASSEMBLE */ |
a322f70c | 3289 | |
a5c7adb3 | 3290 | static int check_secondary(const struct vcl *vc) |
3291 | { | |
3292 | const struct vd_config *conf = &vc->conf; | |
3293 | int i; | |
3294 | ||
3295 | /* The only DDF secondary RAID level md can support is | |
3296 | * RAID 10, if the stripe sizes and Basic volume sizes | |
3297 | * are all equal. | |
3298 | * Other configurations could in theory be supported by exposing | |
3299 | * the BVDs to user space and using device mapper for the secondary | |
3300 | * mapping. So far we don't support that. | |
3301 | */ | |
3302 | ||
3303 | __u64 sec_elements[4] = {0, 0, 0, 0}; | |
3304 | #define __set_sec_seen(n) (sec_elements[(n)>>6] |= (1<<((n)&63))) | |
3305 | #define __was_sec_seen(n) ((sec_elements[(n)>>6] & (1<<((n)&63))) != 0) | |
3306 | ||
3307 | if (vc->other_bvds == NULL) { | |
3308 | pr_err("No BVDs for secondary RAID found\n"); | |
3309 | return -1; | |
3310 | } | |
3311 | if (conf->prl != DDF_RAID1) { | |
3312 | pr_err("Secondary RAID level only supported for mirrored BVD\n"); | |
3313 | return -1; | |
3314 | } | |
3315 | if (conf->srl != DDF_2STRIPED && conf->srl != DDF_2SPANNED) { | |
3316 | pr_err("Secondary RAID level %d is unsupported\n", | |
3317 | conf->srl); | |
3318 | return -1; | |
3319 | } | |
3320 | __set_sec_seen(conf->sec_elmnt_seq); | |
3321 | for (i = 0; i < conf->sec_elmnt_count-1; i++) { | |
3322 | const struct vd_config *bvd = vc->other_bvds[i]; | |
c98567ba | 3323 | if (bvd == NULL) |
3324 | continue; | |
a5c7adb3 | 3325 | if (bvd->srl != conf->srl) { |
3326 | pr_err("Inconsistent secondary RAID level across BVDs\n"); | |
3327 | return -1; | |
3328 | } | |
3329 | if (bvd->prl != conf->prl) { | |
3330 | pr_err("Different RAID levels for BVDs are unsupported\n"); | |
3331 | return -1; | |
3332 | } | |
3333 | if (bvd->prim_elmnt_count != conf->prim_elmnt_count) { | |
3334 | pr_err("All BVDs must have the same number of primary elements\n"); | |
3335 | return -1; | |
3336 | } | |
3337 | if (bvd->chunk_shift != conf->chunk_shift) { | |
3338 | pr_err("Different strip sizes for BVDs are unsupported\n"); | |
3339 | return -1; | |
3340 | } | |
3341 | if (bvd->array_blocks != conf->array_blocks) { | |
3342 | pr_err("Different BVD sizes are unsupported\n"); | |
3343 | return -1; | |
3344 | } | |
3345 | __set_sec_seen(bvd->sec_elmnt_seq); | |
3346 | } | |
3347 | for (i = 0; i < conf->sec_elmnt_count; i++) { | |
3348 | if (!__was_sec_seen(i)) { | |
3349 | pr_err("BVD %d is missing\n", i); | |
3350 | return -1; | |
3351 | } | |
3352 | } | |
3353 | return 0; | |
3354 | } | |
3355 | ||
8a38db86 | 3356 | static unsigned int get_pd_index_from_refnum(const struct vcl *vc, |
4e587018 | 3357 | __u32 refnum, unsigned int nmax, |
3358 | const struct vd_config **bvd, | |
3359 | unsigned int *idx) | |
8a38db86 | 3360 | { |
4e587018 | 3361 | unsigned int i, j, n, sec, cnt; |
3362 | ||
3363 | cnt = __be16_to_cpu(vc->conf.prim_elmnt_count); | |
3364 | sec = (vc->conf.sec_elmnt_count == 1 ? 0 : vc->conf.sec_elmnt_seq); | |
3365 | ||
3366 | for (i = 0, j = 0 ; i < nmax ; i++) { | |
3367 | /* j counts valid entries for this BVD */ | |
3368 | if (vc->conf.phys_refnum[i] != 0xffffffff) | |
3369 | j++; | |
3370 | if (vc->conf.phys_refnum[i] == refnum) { | |
3371 | *bvd = &vc->conf; | |
3372 | *idx = i; | |
3373 | return sec * cnt + j - 1; | |
3374 | } | |
3375 | } | |
3376 | if (vc->other_bvds == NULL) | |
3377 | goto bad; | |
3378 | ||
3379 | for (n = 1; n < vc->conf.sec_elmnt_count; n++) { | |
3380 | struct vd_config *vd = vc->other_bvds[n-1]; | |
3381 | if (vd == NULL) | |
3382 | continue; | |
3383 | sec = vd->sec_elmnt_seq; | |
3384 | for (i = 0, j = 0 ; i < nmax ; i++) { | |
3385 | if (vd->phys_refnum[i] != 0xffffffff) | |
3386 | j++; | |
3387 | if (vd->phys_refnum[i] == refnum) { | |
3388 | *bvd = vd; | |
3389 | *idx = i; | |
3390 | return sec * cnt + j - 1; | |
3391 | } | |
3392 | } | |
3393 | } | |
3394 | bad: | |
3395 | *bvd = NULL; | |
d6e7b083 | 3396 | return DDF_NOTFOUND; |
8a38db86 | 3397 | } |
3398 | ||
00bbdbda | 3399 | static struct mdinfo *container_content_ddf(struct supertype *st, char *subarray) |
598f0d58 NB |
3400 | { |
3401 | /* Given a container loaded by load_super_ddf_all, | |
3402 | * extract information about all the arrays into | |
3403 | * an mdinfo tree. | |
3404 | * | |
3405 | * For each vcl in conflist: create an mdinfo, fill it in, | |
3406 | * then look for matching devices (phys_refnum) in dlist | |
3407 | * and create appropriate device mdinfo. | |
3408 | */ | |
3409 | struct ddf_super *ddf = st->sb; | |
3410 | struct mdinfo *rest = NULL; | |
3411 | struct vcl *vc; | |
3412 | ||
3413 | for (vc = ddf->conflist ; vc ; vc=vc->next) | |
3414 | { | |
f21e18ca N |
3415 | unsigned int i; |
3416 | unsigned int j; | |
598f0d58 | 3417 | struct mdinfo *this; |
00bbdbda | 3418 | char *ep; |
90fa1a29 | 3419 | __u32 *cptr; |
8a38db86 | 3420 | unsigned int pd; |
00bbdbda N |
3421 | |
3422 | if (subarray && | |
3423 | (strtoul(subarray, &ep, 10) != vc->vcnum || | |
3424 | *ep != '\0')) | |
3425 | continue; | |
3426 | ||
a5c7adb3 | 3427 | if (vc->conf.sec_elmnt_count > 1) { |
3428 | if (check_secondary(vc) != 0) | |
3429 | continue; | |
3430 | } | |
3431 | ||
503975b9 | 3432 | this = xcalloc(1, sizeof(*this)); |
598f0d58 NB |
3433 | this->next = rest; |
3434 | rest = this; | |
3435 | ||
4e587018 | 3436 | if (vc->conf.sec_elmnt_count == 1) { |
3437 | this->array.level = map_num1(ddf_level_num, | |
3438 | vc->conf.prl); | |
3439 | this->array.raid_disks = | |
3440 | __be16_to_cpu(vc->conf.prim_elmnt_count); | |
3441 | this->array.layout = | |
3442 | rlq_to_layout(vc->conf.rlq, vc->conf.prl, | |
3443 | this->array.raid_disks); | |
3444 | } else { | |
3445 | /* The only supported layout is RAID 10. | |
3446 | * Compatibility has been checked in check_secondary() | |
3447 | * above. | |
3448 | */ | |
3449 | this->array.level = 10; | |
3450 | this->array.raid_disks = | |
3451 | __be16_to_cpu(vc->conf.prim_elmnt_count) | |
3452 | * vc->conf.sec_elmnt_count; | |
3453 | this->array.layout = 0x100 | | |
3454 | __be16_to_cpu(vc->conf.prim_elmnt_count); | |
3455 | } | |
598f0d58 | 3456 | this->array.md_minor = -1; |
f35f2525 N |
3457 | this->array.major_version = -1; |
3458 | this->array.minor_version = -2; | |
90fa1a29 JS |
3459 | cptr = (__u32 *)(vc->conf.guid + 16); |
3460 | this->array.ctime = DECADE + __be32_to_cpu(*cptr); | |
598f0d58 NB |
3461 | this->array.utime = DECADE + |
3462 | __be32_to_cpu(vc->conf.timestamp); | |
3463 | this->array.chunk_size = 512 << vc->conf.chunk_shift; | |
3464 | ||
59e36268 | 3465 | i = vc->vcnum; |
7a7cc504 NB |
3466 | if ((ddf->virt->entries[i].state & DDF_state_inconsistent) || |
3467 | (ddf->virt->entries[i].init_state & DDF_initstate_mask) != | |
ed9d66aa | 3468 | DDF_init_full) { |
598f0d58 | 3469 | this->array.state = 0; |
ed9d66aa NB |
3470 | this->resync_start = 0; |
3471 | } else { | |
598f0d58 | 3472 | this->array.state = 1; |
b7528a20 | 3473 | this->resync_start = MaxSector; |
ed9d66aa | 3474 | } |
db42fa9b N |
3475 | memcpy(this->name, ddf->virt->entries[i].name, 16); |
3476 | this->name[16]=0; | |
3477 | for(j=0; j<16; j++) | |
3478 | if (this->name[j] == ' ') | |
3479 | this->name[j] = 0; | |
598f0d58 NB |
3480 | |
3481 | memset(this->uuid, 0, sizeof(this->uuid)); | |
3482 | this->component_size = __be64_to_cpu(vc->conf.blocks); | |
3483 | this->array.size = this->component_size / 2; | |
5f2aace8 | 3484 | this->container_member = i; |
598f0d58 | 3485 | |
c5afc314 N |
3486 | ddf->currentconf = vc; |
3487 | uuid_from_super_ddf(st, this->uuid); | |
3488 | ddf->currentconf = NULL; | |
3489 | ||
60f18132 | 3490 | sprintf(this->text_version, "/%s/%d", |
4dd2df09 | 3491 | st->container_devnm, this->container_member); |
60f18132 | 3492 | |
8a38db86 | 3493 | for (pd = 0; pd < __be16_to_cpu(ddf->phys->used_pdes); pd++) { |
598f0d58 NB |
3494 | struct mdinfo *dev; |
3495 | struct dl *d; | |
4e587018 | 3496 | const struct vd_config *bvd; |
3497 | unsigned int iphys; | |
3498 | __u64 *lba_offset; | |
fa033bec | 3499 | int stt; |
598f0d58 | 3500 | |
8a38db86 | 3501 | if (ddf->phys->entries[pd].refnum == 0xFFFFFFFF) |
bc17324f | 3502 | continue; |
0cf5ef67 N |
3503 | |
3504 | stt = __be16_to_cpu(ddf->phys->entries[pd].state); | |
fa033bec N |
3505 | if ((stt & (DDF_Online|DDF_Failed|DDF_Rebuilding)) |
3506 | != DDF_Online) | |
3507 | continue; | |
3508 | ||
8a38db86 | 3509 | i = get_pd_index_from_refnum( |
4e587018 | 3510 | vc, ddf->phys->entries[pd].refnum, |
3511 | ddf->mppe, &bvd, &iphys); | |
d6e7b083 | 3512 | if (i == DDF_NOTFOUND) |
8a38db86 | 3513 | continue; |
3514 | ||
fa033bec | 3515 | this->array.working_disks++; |
bc17324f | 3516 | |
0cf5ef67 | 3517 | for (d = ddf->dlist; d ; d=d->next) |
8a38db86 | 3518 | if (d->disk.refnum == |
3519 | ddf->phys->entries[pd].refnum) | |
0cf5ef67 N |
3520 | break; |
3521 | if (d == NULL) | |
3522 | /* Haven't found that one yet, maybe there are others */ | |
3523 | continue; | |
3524 | ||
503975b9 | 3525 | dev = xcalloc(1, sizeof(*dev)); |
598f0d58 NB |
3526 | dev->next = this->devs; |
3527 | this->devs = dev; | |
3528 | ||
3529 | dev->disk.number = __be32_to_cpu(d->disk.refnum); | |
3530 | dev->disk.major = d->major; | |
3531 | dev->disk.minor = d->minor; | |
3532 | dev->disk.raid_disk = i; | |
3533 | dev->disk.state = (1<<MD_DISK_SYNC)|(1<<MD_DISK_ACTIVE); | |
d23534e4 | 3534 | dev->recovery_start = MaxSector; |
598f0d58 | 3535 | |
120f7677 | 3536 | dev->events = __be32_to_cpu(ddf->primary.seq); |
4e587018 | 3537 | lba_offset = (__u64 *)&bvd->phys_refnum[ddf->mppe]; |
3538 | dev->data_offset = __be64_to_cpu(lba_offset[iphys]); | |
3539 | dev->component_size = __be64_to_cpu(bvd->blocks); | |
598f0d58 NB |
3540 | if (d->devname) |
3541 | strcpy(dev->name, d->devname); | |
3542 | } | |
3543 | } | |
3544 | return rest; | |
3545 | } | |
3546 | ||
955e9ea1 | 3547 | static int store_super_ddf(struct supertype *st, int fd) |
a322f70c | 3548 | { |
955e9ea1 | 3549 | struct ddf_super *ddf = st->sb; |
a322f70c | 3550 | unsigned long long dsize; |
6416d527 | 3551 | void *buf; |
3d2c4fc7 | 3552 | int rc; |
a322f70c | 3553 | |
955e9ea1 DW |
3554 | if (!ddf) |
3555 | return 1; | |
3556 | ||
a322f70c DW |
3557 | if (!get_dev_size(fd, NULL, &dsize)) |
3558 | return 1; | |
3559 | ||
dbf98368 | 3560 | if (ddf->dlist || ddf->conflist) { |
3561 | struct stat sta; | |
3562 | struct dl *dl; | |
3563 | int ofd, ret; | |
3564 | ||
3565 | if (fstat(fd, &sta) == -1 || !S_ISBLK(sta.st_mode)) { | |
3566 | pr_err("%s: file descriptor for invalid device\n", | |
3567 | __func__); | |
3568 | return 1; | |
3569 | } | |
3570 | for (dl = ddf->dlist; dl; dl = dl->next) | |
3571 | if (dl->major == (int)major(sta.st_rdev) && | |
3572 | dl->minor == (int)minor(sta.st_rdev)) | |
3573 | break; | |
3574 | if (!dl) { | |
3575 | pr_err("%s: couldn't find disk %d/%d\n", __func__, | |
3576 | (int)major(sta.st_rdev), | |
3577 | (int)minor(sta.st_rdev)); | |
3578 | return 1; | |
3579 | } | |
3580 | /* | |
3581 | For DDF, writing to just one disk makes no sense. | |
3582 | We would run the risk of writing inconsistent meta data | |
3583 | to the devices. So just call __write_init_super_ddf and | |
3584 | write to all devices, including this one. | |
3585 | Use the fd passed to this function, just in case dl->fd | |
3586 | is invalid. | |
3587 | */ | |
3588 | ofd = dl->fd; | |
3589 | dl->fd = fd; | |
3590 | ret = __write_init_super_ddf(st); | |
3591 | dl->fd = ofd; | |
3592 | return ret; | |
3593 | } | |
3594 | ||
3d2c4fc7 DW |
3595 | if (posix_memalign(&buf, 512, 512) != 0) |
3596 | return 1; | |
6416d527 NB |
3597 | memset(buf, 0, 512); |
3598 | ||
a322f70c | 3599 | lseek64(fd, dsize-512, 0); |
3d2c4fc7 | 3600 | rc = write(fd, buf, 512); |
6416d527 | 3601 | free(buf); |
3d2c4fc7 DW |
3602 | if (rc < 0) |
3603 | return 1; | |
a322f70c DW |
3604 | return 0; |
3605 | } | |
3606 | ||
a19c88b8 NB |
3607 | static int compare_super_ddf(struct supertype *st, struct supertype *tst) |
3608 | { | |
3609 | /* | |
3610 | * return: | |
3611 | * 0 same, or first was empty, and second was copied | |
3612 | * 1 second had wrong number | |
3613 | * 2 wrong uuid | |
3614 | * 3 wrong other info | |
3615 | */ | |
3616 | struct ddf_super *first = st->sb; | |
3617 | struct ddf_super *second = tst->sb; | |
4eefd651 | 3618 | struct dl *dl1, *dl2; |
3619 | struct vcl *vl1, *vl2; | |
2d210697 | 3620 | unsigned int max_vds, max_pds, pd, vd; |
a19c88b8 NB |
3621 | |
3622 | if (!first) { | |
3623 | st->sb = tst->sb; | |
3624 | tst->sb = NULL; | |
3625 | return 0; | |
3626 | } | |
3627 | ||
3628 | if (memcmp(first->anchor.guid, second->anchor.guid, DDF_GUID_LEN) != 0) | |
3629 | return 2; | |
3630 | ||
2d210697 | 3631 | if (first->anchor.seq != second->anchor.seq) { |
3632 | dprintf("%s: sequence number mismatch %u/%u\n", __func__, | |
3633 | __be32_to_cpu(first->anchor.seq), | |
3634 | __be32_to_cpu(second->anchor.seq)); | |
3635 | return 3; | |
3636 | } | |
3637 | if (first->max_part != second->max_part || | |
3638 | first->phys->used_pdes != second->phys->used_pdes || | |
3639 | first->virt->populated_vdes != second->virt->populated_vdes) { | |
3640 | dprintf("%s: PD/VD number mismatch\n", __func__); | |
3641 | return 3; | |
3642 | } | |
3643 | ||
3644 | max_pds = __be16_to_cpu(first->phys->used_pdes); | |
3645 | for (dl2 = second->dlist; dl2; dl2 = dl2->next) { | |
3646 | for (pd = 0; pd < max_pds; pd++) | |
3647 | if (first->phys->entries[pd].refnum == dl2->disk.refnum) | |
3648 | break; | |
3649 | if (pd == max_pds) { | |
3650 | dprintf("%s: no match for disk %08x\n", __func__, | |
3651 | __be32_to_cpu(dl2->disk.refnum)); | |
3652 | return 3; | |
3653 | } | |
3654 | } | |
3655 | ||
3656 | max_vds = __be16_to_cpu(first->active->max_vd_entries); | |
3657 | for (vl2 = second->conflist; vl2; vl2 = vl2->next) { | |
3658 | if (vl2->conf.magic != DDF_VD_CONF_MAGIC) | |
3659 | continue; | |
3660 | for (vd = 0; vd < max_vds; vd++) | |
3661 | if (!memcmp(first->virt->entries[vd].guid, | |
3662 | vl2->conf.guid, DDF_GUID_LEN)) | |
3663 | break; | |
3664 | if (vd == max_vds) { | |
3665 | dprintf("%s: no match for VD config\n", __func__); | |
3666 | return 3; | |
3667 | } | |
3668 | } | |
a19c88b8 | 3669 | /* FIXME should I look at anything else? */ |
2d210697 | 3670 | |
4eefd651 | 3671 | /* |
3672 | At this point we are fairly sure that the meta data matches. | |
3673 | But the new disk may contain additional local data. | |
3674 | Add it to the super block. | |
3675 | */ | |
3676 | for (vl2 = second->conflist; vl2; vl2 = vl2->next) { | |
3677 | for (vl1 = first->conflist; vl1; vl1 = vl1->next) | |
3678 | if (!memcmp(vl1->conf.guid, vl2->conf.guid, | |
3679 | DDF_GUID_LEN)) | |
3680 | break; | |
3681 | if (vl1) { | |
3682 | if (vl1->other_bvds != NULL && | |
3683 | vl1->conf.sec_elmnt_seq != | |
3684 | vl2->conf.sec_elmnt_seq) { | |
3685 | dprintf("%s: adding BVD %u\n", __func__, | |
3686 | vl2->conf.sec_elmnt_seq); | |
3687 | add_other_bvd(vl1, &vl2->conf, | |
3688 | first->conf_rec_len*512); | |
3689 | } | |
3690 | continue; | |
3691 | } | |
3692 | ||
3693 | if (posix_memalign((void **)&vl1, 512, | |
3694 | (first->conf_rec_len*512 + | |
3695 | offsetof(struct vcl, conf))) != 0) { | |
3696 | pr_err("%s could not allocate vcl buf\n", | |
3697 | __func__); | |
3698 | return 3; | |
3699 | } | |
3700 | ||
3701 | vl1->next = first->conflist; | |
3702 | vl1->block_sizes = NULL; | |
3f188b10 | 3703 | if (vl2->conf.sec_elmnt_count > 1) { |
4eefd651 | 3704 | vl1->other_bvds = xcalloc(vl2->conf.sec_elmnt_count - 1, |
3705 | sizeof(struct vd_config *)); | |
3706 | } else | |
3707 | vl1->other_bvds = NULL; | |
3708 | memcpy(&vl1->conf, &vl2->conf, first->conf_rec_len*512); | |
3709 | vl1->lba_offset = (__u64 *) | |
3710 | &vl1->conf.phys_refnum[first->mppe]; | |
3711 | for (vd = 0; vd < max_vds; vd++) | |
3712 | if (!memcmp(first->virt->entries[vd].guid, | |
3713 | vl1->conf.guid, DDF_GUID_LEN)) | |
3714 | break; | |
3715 | vl1->vcnum = vd; | |
3716 | dprintf("%s: added config for VD %u\n", __func__, vl1->vcnum); | |
3717 | first->conflist = vl1; | |
3718 | } | |
3719 | ||
3720 | for (dl2 = second->dlist; dl2; dl2 = dl2->next) { | |
3721 | for (dl1 = first->dlist; dl1; dl1 = dl1->next) | |
3722 | if (dl1->disk.refnum == dl2->disk.refnum) | |
3723 | break; | |
3724 | if (dl1) | |
3725 | continue; | |
3726 | ||
3727 | if (posix_memalign((void **)&dl1, 512, | |
3728 | sizeof(*dl1) + (first->max_part) * sizeof(dl1->vlist[0])) | |
3729 | != 0) { | |
3730 | pr_err("%s could not allocate disk info buffer\n", | |
3731 | __func__); | |
3732 | return 3; | |
3733 | } | |
3734 | memcpy(dl1, dl2, sizeof(*dl1)); | |
3735 | dl1->mdupdate = NULL; | |
3736 | dl1->next = first->dlist; | |
3737 | dl1->fd = -1; | |
3738 | for (pd = 0; pd < max_pds; pd++) | |
3739 | if (first->phys->entries[pd].refnum == dl1->disk.refnum) | |
3740 | break; | |
3741 | dl1->pdnum = pd; | |
3742 | if (dl2->spare) { | |
3743 | if (posix_memalign((void **)&dl1->spare, 512, | |
3744 | first->conf_rec_len*512) != 0) { | |
3745 | pr_err("%s could not allocate spare info buf\n", | |
3746 | __func__); | |
3747 | return 3; | |
3748 | } | |
3749 | memcpy(dl1->spare, dl2->spare, first->conf_rec_len*512); | |
3750 | } | |
3751 | for (vd = 0 ; vd < first->max_part ; vd++) { | |
3752 | if (!dl2->vlist[vd]) { | |
3753 | dl1->vlist[vd] = NULL; | |
3754 | continue; | |
3755 | } | |
3756 | for (vl1 = first->conflist; vl1; vl1 = vl1->next) { | |
3757 | if (!memcmp(vl1->conf.guid, | |
3758 | dl2->vlist[vd]->conf.guid, | |
3759 | DDF_GUID_LEN)) | |
3760 | break; | |
3761 | dl1->vlist[vd] = vl1; | |
3762 | } | |
3763 | } | |
3764 | first->dlist = dl1; | |
3765 | dprintf("%s: added disk %d: %08x\n", __func__, dl1->pdnum, | |
3766 | dl1->disk.refnum); | |
3767 | } | |
3768 | ||
a19c88b8 NB |
3769 | return 0; |
3770 | } | |
3771 | ||
0e600426 | 3772 | #ifndef MDASSEMBLE |
4e5528c6 NB |
3773 | /* |
3774 | * A new array 'a' has been started which claims to be instance 'inst' | |
3775 | * within container 'c'. | |
3776 | * We need to confirm that the array matches the metadata in 'c' so | |
3777 | * that we don't corrupt any metadata. | |
3778 | */ | |
cba0191b | 3779 | static int ddf_open_new(struct supertype *c, struct active_array *a, char *inst) |
549e9569 | 3780 | { |
a2aa439e | 3781 | struct ddf_super *ddf = c->sb; |
3782 | int n = atoi(inst); | |
fb9d0acb | 3783 | if (all_ff(ddf->virt->entries[n].guid)) { |
3784 | pr_err("%s: subarray %d doesn't exist\n", __func__, n); | |
a2aa439e | 3785 | return -ENODEV; |
3786 | } | |
3787 | dprintf("ddf: open_new %d\n", n); | |
3788 | a->info.container_member = n; | |
549e9569 NB |
3789 | return 0; |
3790 | } | |
3791 | ||
4e5528c6 NB |
3792 | /* |
3793 | * The array 'a' is to be marked clean in the metadata. | |
ed9d66aa | 3794 | * If '->resync_start' is not ~(unsigned long long)0, then the array is only |
4e5528c6 NB |
3795 | * clean up to the point (in sectors). If that cannot be recorded in the |
3796 | * metadata, then leave it as dirty. | |
3797 | * | |
3798 | * For DDF, we need to clear the DDF_state_inconsistent bit in the | |
3799 | * !global! virtual_disk.virtual_entry structure. | |
3800 | */ | |
01f157d7 | 3801 | static int ddf_set_array_state(struct active_array *a, int consistent) |
549e9569 | 3802 | { |
4e5528c6 NB |
3803 | struct ddf_super *ddf = a->container->sb; |
3804 | int inst = a->info.container_member; | |
18a2f463 | 3805 | int old = ddf->virt->entries[inst].state; |
01f157d7 N |
3806 | if (consistent == 2) { |
3807 | /* Should check if a recovery should be started FIXME */ | |
3808 | consistent = 1; | |
b7941fd6 | 3809 | if (!is_resync_complete(&a->info)) |
01f157d7 N |
3810 | consistent = 0; |
3811 | } | |
ed9d66aa NB |
3812 | if (consistent) |
3813 | ddf->virt->entries[inst].state &= ~DDF_state_inconsistent; | |
3814 | else | |
4e5528c6 | 3815 | ddf->virt->entries[inst].state |= DDF_state_inconsistent; |
18a2f463 | 3816 | if (old != ddf->virt->entries[inst].state) |
7d5a7ff3 | 3817 | ddf_set_updates_pending(ddf); |
18a2f463 NB |
3818 | |
3819 | old = ddf->virt->entries[inst].init_state; | |
ed9d66aa | 3820 | ddf->virt->entries[inst].init_state &= ~DDF_initstate_mask; |
b7941fd6 | 3821 | if (is_resync_complete(&a->info)) |
ed9d66aa | 3822 | ddf->virt->entries[inst].init_state |= DDF_init_full; |
b7941fd6 | 3823 | else if (a->info.resync_start == 0) |
ed9d66aa | 3824 | ddf->virt->entries[inst].init_state |= DDF_init_not; |
4e5528c6 | 3825 | else |
ed9d66aa | 3826 | ddf->virt->entries[inst].init_state |= DDF_init_quick; |
18a2f463 | 3827 | if (old != ddf->virt->entries[inst].init_state) |
7d5a7ff3 | 3828 | ddf_set_updates_pending(ddf); |
ed9d66aa | 3829 | |
2c514b71 | 3830 | dprintf("ddf mark %d %s %llu\n", inst, consistent?"clean":"dirty", |
b7941fd6 | 3831 | a->info.resync_start); |
01f157d7 | 3832 | return consistent; |
fd7cde1b DW |
3833 | } |
3834 | ||
5ec636b7 | 3835 | static int get_bvd_state(const struct ddf_super *ddf, |
3836 | const struct vd_config *vc) | |
3837 | { | |
3838 | unsigned int i, n_bvd, working = 0; | |
3839 | unsigned int n_prim = __be16_to_cpu(vc->prim_elmnt_count); | |
3840 | int pd, st, state; | |
3841 | for (i = 0; i < n_prim; i++) { | |
3842 | if (!find_index_in_bvd(ddf, vc, i, &n_bvd)) | |
3843 | continue; | |
3844 | pd = find_phys(ddf, vc->phys_refnum[n_bvd]); | |
3845 | if (pd < 0) | |
3846 | continue; | |
3847 | st = __be16_to_cpu(ddf->phys->entries[pd].state); | |
3848 | if ((st & (DDF_Online|DDF_Failed|DDF_Rebuilding)) | |
3849 | == DDF_Online) | |
3850 | working++; | |
3851 | } | |
3852 | ||
3853 | state = DDF_state_degraded; | |
3854 | if (working == n_prim) | |
3855 | state = DDF_state_optimal; | |
3856 | else | |
3857 | switch (vc->prl) { | |
3858 | case DDF_RAID0: | |
3859 | case DDF_CONCAT: | |
3860 | case DDF_JBOD: | |
3861 | state = DDF_state_failed; | |
3862 | break; | |
3863 | case DDF_RAID1: | |
3864 | if (working == 0) | |
3865 | state = DDF_state_failed; | |
3866 | else if (working >= 2) | |
3867 | state = DDF_state_part_optimal; | |
3868 | break; | |
3869 | case DDF_RAID4: | |
3870 | case DDF_RAID5: | |
3871 | if (working < n_prim - 1) | |
3872 | state = DDF_state_failed; | |
3873 | break; | |
3874 | case DDF_RAID6: | |
3875 | if (working < n_prim - 2) | |
3876 | state = DDF_state_failed; | |
3877 | else if (working == n_prim - 1) | |
3878 | state = DDF_state_part_optimal; | |
3879 | break; | |
3880 | } | |
3881 | return state; | |
3882 | } | |
3883 | ||
0777d17d | 3884 | static int secondary_state(int state, int other, int seclevel) |
3885 | { | |
3886 | if (state == DDF_state_optimal && other == DDF_state_optimal) | |
3887 | return DDF_state_optimal; | |
3888 | if (seclevel == DDF_2MIRRORED) { | |
3889 | if (state == DDF_state_optimal || other == DDF_state_optimal) | |
3890 | return DDF_state_part_optimal; | |
3891 | if (state == DDF_state_failed && other == DDF_state_failed) | |
3892 | return DDF_state_failed; | |
3893 | return DDF_state_degraded; | |
3894 | } else { | |
3895 | if (state == DDF_state_failed || other == DDF_state_failed) | |
3896 | return DDF_state_failed; | |
3897 | if (state == DDF_state_degraded || other == DDF_state_degraded) | |
3898 | return DDF_state_degraded; | |
3899 | return DDF_state_part_optimal; | |
3900 | } | |
3901 | } | |
3902 | ||
3903 | static int get_svd_state(const struct ddf_super *ddf, const struct vcl *vcl) | |
3904 | { | |
3905 | int state = get_bvd_state(ddf, &vcl->conf); | |
3906 | unsigned int i; | |
3907 | for (i = 1; i < vcl->conf.sec_elmnt_count; i++) { | |
3908 | state = secondary_state( | |
3909 | state, | |
3910 | get_bvd_state(ddf, vcl->other_bvds[i-1]), | |
3911 | vcl->conf.srl); | |
3912 | } | |
3913 | return state; | |
3914 | } | |
3915 | ||
7a7cc504 NB |
3916 | /* |
3917 | * The state of each disk is stored in the global phys_disk structure | |
3918 | * in phys_disk.entries[n].state. | |
3919 | * This makes various combinations awkward. | |
3920 | * - When a device fails in any array, it must be failed in all arrays | |
3921 | * that include a part of this device. | |
3922 | * - When a component is rebuilding, we cannot include it officially in the | |
3923 | * array unless this is the only array that uses the device. | |
3924 | * | |
3925 | * So: when transitioning: | |
3926 | * Online -> failed, just set failed flag. monitor will propagate | |
3927 | * spare -> online, the device might need to be added to the array. | |
3928 | * spare -> failed, just set failed. Don't worry if in array or not. | |
3929 | */ | |
8d45d196 | 3930 | static void ddf_set_disk(struct active_array *a, int n, int state) |
549e9569 | 3931 | { |
7a7cc504 | 3932 | struct ddf_super *ddf = a->container->sb; |
baba3f4e | 3933 | unsigned int inst = a->info.container_member, n_bvd; |
3934 | struct vcl *vcl; | |
3935 | struct vd_config *vc = find_vdcr(ddf, inst, (unsigned int)n, | |
3936 | &n_bvd, &vcl); | |
3937 | int pd; | |
e1316fab N |
3938 | struct mdinfo *mdi; |
3939 | struct dl *dl; | |
7a7cc504 NB |
3940 | |
3941 | if (vc == NULL) { | |
2c514b71 | 3942 | dprintf("ddf: cannot find instance %d!!\n", inst); |
7a7cc504 NB |
3943 | return; |
3944 | } | |
e1316fab N |
3945 | /* Find the matching slot in 'info'. */ |
3946 | for (mdi = a->info.devs; mdi; mdi = mdi->next) | |
3947 | if (mdi->disk.raid_disk == n) | |
3948 | break; | |
3949 | if (!mdi) | |
3950 | return; | |
3951 | ||
3952 | /* and find the 'dl' entry corresponding to that. */ | |
3953 | for (dl = ddf->dlist; dl; dl = dl->next) | |
77632af9 N |
3954 | if (mdi->state_fd >= 0 && |
3955 | mdi->disk.major == dl->major && | |
e1316fab N |
3956 | mdi->disk.minor == dl->minor) |
3957 | break; | |
3958 | if (!dl) | |
3959 | return; | |
3960 | ||
baba3f4e | 3961 | pd = find_phys(ddf, vc->phys_refnum[n_bvd]); |
e1316fab N |
3962 | if (pd < 0 || pd != dl->pdnum) { |
3963 | /* disk doesn't currently exist or has changed. | |
3964 | * If it is now in_sync, insert it. */ | |
baba3f4e | 3965 | dprintf("%s: phys disk not found for %d: %d/%d ref %08x\n", |
3966 | __func__, dl->pdnum, dl->major, dl->minor, | |
3967 | dl->disk.refnum); | |
3968 | dprintf("%s: array %u disk %u ref %08x pd %d\n", | |
3969 | __func__, inst, n_bvd, vc->phys_refnum[n_bvd], pd); | |
7a7cc504 | 3970 | if ((state & DS_INSYNC) && ! (state & DS_FAULTY)) { |
baba3f4e | 3971 | __u64 *lba_offset; |
3972 | pd = dl->pdnum; /* FIXME: is this really correct ? */ | |
3973 | vc->phys_refnum[n_bvd] = dl->disk.refnum; | |
3974 | lba_offset = (__u64 *)&vc->phys_refnum[ddf->mppe]; | |
3975 | lba_offset[n_bvd] = mdi->data_offset; | |
e1316fab N |
3976 | ddf->phys->entries[pd].type &= |
3977 | ~__cpu_to_be16(DDF_Global_Spare); | |
3978 | ddf->phys->entries[pd].type |= | |
3979 | __cpu_to_be16(DDF_Active_in_VD); | |
7d5a7ff3 | 3980 | ddf_set_updates_pending(ddf); |
7a7cc504 NB |
3981 | } |
3982 | } else { | |
18a2f463 | 3983 | int old = ddf->phys->entries[pd].state; |
7a7cc504 NB |
3984 | if (state & DS_FAULTY) |
3985 | ddf->phys->entries[pd].state |= __cpu_to_be16(DDF_Failed); | |
3986 | if (state & DS_INSYNC) { | |
3987 | ddf->phys->entries[pd].state |= __cpu_to_be16(DDF_Online); | |
3988 | ddf->phys->entries[pd].state &= __cpu_to_be16(~DDF_Rebuilding); | |
3989 | } | |
18a2f463 | 3990 | if (old != ddf->phys->entries[pd].state) |
7d5a7ff3 | 3991 | ddf_set_updates_pending(ddf); |
7a7cc504 NB |
3992 | } |
3993 | ||
2c514b71 | 3994 | dprintf("ddf: set_disk %d to %x\n", n, state); |
7e1432fb | 3995 | |
7a7cc504 NB |
3996 | /* Now we need to check the state of the array and update |
3997 | * virtual_disk.entries[n].state. | |
3998 | * It needs to be one of "optimal", "degraded", "failed". | |
3999 | * I don't understand 'deleted' or 'missing'. | |
4000 | */ | |
0777d17d | 4001 | state = get_svd_state(ddf, vcl); |
7a7cc504 | 4002 | |
18a2f463 NB |
4003 | if (ddf->virt->entries[inst].state != |
4004 | ((ddf->virt->entries[inst].state & ~DDF_state_mask) | |
4005 | | state)) { | |
4006 | ||
4007 | ddf->virt->entries[inst].state = | |
4008 | (ddf->virt->entries[inst].state & ~DDF_state_mask) | |
4009 | | state; | |
7d5a7ff3 | 4010 | ddf_set_updates_pending(ddf); |
18a2f463 | 4011 | } |
7a7cc504 | 4012 | |
549e9569 NB |
4013 | } |
4014 | ||
2e735d19 | 4015 | static void ddf_sync_metadata(struct supertype *st) |
549e9569 | 4016 | { |
7a7cc504 NB |
4017 | |
4018 | /* | |
4019 | * Write all data to all devices. | |
4020 | * Later, we might be able to track whether only local changes | |
4021 | * have been made, or whether any global data has been changed, | |
4022 | * but ddf is sufficiently weird that it probably always | |
4023 | * changes global data .... | |
4024 | */ | |
18a2f463 NB |
4025 | struct ddf_super *ddf = st->sb; |
4026 | if (!ddf->updates_pending) | |
4027 | return; | |
4028 | ddf->updates_pending = 0; | |
1cc7f4fe | 4029 | __write_init_super_ddf(st); |
2c514b71 | 4030 | dprintf("ddf: sync_metadata\n"); |
549e9569 NB |
4031 | } |
4032 | ||
88c164f4 NB |
4033 | static void ddf_process_update(struct supertype *st, |
4034 | struct metadata_update *update) | |
4035 | { | |
4036 | /* Apply this update to the metadata. | |
4037 | * The first 4 bytes are a DDF_*_MAGIC which guides | |
4038 | * our actions. | |
4039 | * Possible update are: | |
4040 | * DDF_PHYS_RECORDS_MAGIC | |
4dd968cc N |
4041 | * Add a new physical device or remove an old one. |
4042 | * Changes to this record only happen implicitly. | |
88c164f4 NB |
4043 | * used_pdes is the device number. |
4044 | * DDF_VIRT_RECORDS_MAGIC | |
4045 | * Add a new VD. Possibly also change the 'access' bits. | |
4046 | * populated_vdes is the entry number. | |
4047 | * DDF_VD_CONF_MAGIC | |
4048 | * New or updated VD. the VIRT_RECORD must already | |
4049 | * exist. For an update, phys_refnum and lba_offset | |
4050 | * (at least) are updated, and the VD_CONF must | |
4051 | * be written to precisely those devices listed with | |
4052 | * a phys_refnum. | |
4053 | * DDF_SPARE_ASSIGN_MAGIC | |
4054 | * replacement Spare Assignment Record... but for which device? | |
4055 | * | |
4056 | * So, e.g.: | |
4057 | * - to create a new array, we send a VIRT_RECORD and | |
4058 | * a VD_CONF. Then assemble and start the array. | |
4059 | * - to activate a spare we send a VD_CONF to add the phys_refnum | |
4060 | * and offset. This will also mark the spare as active with | |
4061 | * a spare-assignment record. | |
4062 | */ | |
4063 | struct ddf_super *ddf = st->sb; | |
4064 | __u32 *magic = (__u32*)update->buf; | |
4065 | struct phys_disk *pd; | |
4066 | struct virtual_disk *vd; | |
4067 | struct vd_config *vc; | |
4068 | struct vcl *vcl; | |
4069 | struct dl *dl; | |
f21e18ca N |
4070 | unsigned int mppe; |
4071 | unsigned int ent; | |
c7079c84 | 4072 | unsigned int pdnum, pd2; |
88c164f4 | 4073 | |
2c514b71 | 4074 | dprintf("Process update %x\n", *magic); |
7e1432fb | 4075 | |
88c164f4 NB |
4076 | switch (*magic) { |
4077 | case DDF_PHYS_RECORDS_MAGIC: | |
4078 | ||
4079 | if (update->len != (sizeof(struct phys_disk) + | |
4080 | sizeof(struct phys_disk_entry))) | |
4081 | return; | |
4082 | pd = (struct phys_disk*)update->buf; | |
4083 | ||
4084 | ent = __be16_to_cpu(pd->used_pdes); | |
4085 | if (ent >= __be16_to_cpu(ddf->phys->max_pdes)) | |
4086 | return; | |
4dd968cc N |
4087 | if (pd->entries[0].state & __cpu_to_be16(DDF_Missing)) { |
4088 | struct dl **dlp; | |
4089 | /* removing this disk. */ | |
4090 | ddf->phys->entries[ent].state |= __cpu_to_be16(DDF_Missing); | |
4091 | for (dlp = &ddf->dlist; *dlp; dlp = &(*dlp)->next) { | |
4092 | struct dl *dl = *dlp; | |
4093 | if (dl->pdnum == (signed)ent) { | |
4094 | close(dl->fd); | |
4095 | dl->fd = -1; | |
4096 | /* FIXME this doesn't free | |
4097 | * dl->devname */ | |
4098 | update->space = dl; | |
4099 | *dlp = dl->next; | |
4100 | break; | |
4101 | } | |
4102 | } | |
7d5a7ff3 | 4103 | ddf_set_updates_pending(ddf); |
4dd968cc N |
4104 | return; |
4105 | } | |
88c164f4 NB |
4106 | if (!all_ff(ddf->phys->entries[ent].guid)) |
4107 | return; | |
4108 | ddf->phys->entries[ent] = pd->entries[0]; | |
4109 | ddf->phys->used_pdes = __cpu_to_be16(1 + | |
613b0d17 | 4110 | __be16_to_cpu(ddf->phys->used_pdes)); |
7d5a7ff3 | 4111 | ddf_set_updates_pending(ddf); |
2cc2983d N |
4112 | if (ddf->add_list) { |
4113 | struct active_array *a; | |
4114 | struct dl *al = ddf->add_list; | |
4115 | ddf->add_list = al->next; | |
4116 | ||
4117 | al->next = ddf->dlist; | |
4118 | ddf->dlist = al; | |
4119 | ||
4120 | /* As a device has been added, we should check | |
4121 | * for any degraded devices that might make | |
4122 | * use of this spare */ | |
4123 | for (a = st->arrays ; a; a=a->next) | |
4124 | a->check_degraded = 1; | |
4125 | } | |
88c164f4 NB |
4126 | break; |
4127 | ||
4128 | case DDF_VIRT_RECORDS_MAGIC: | |
4129 | ||
4130 | if (update->len != (sizeof(struct virtual_disk) + | |
4131 | sizeof(struct virtual_entry))) | |
4132 | return; | |
4133 | vd = (struct virtual_disk*)update->buf; | |
4134 | ||
fb9d0acb | 4135 | ent = find_unused_vde(ddf); |
4136 | if (ent == DDF_NOTFOUND) | |
88c164f4 NB |
4137 | return; |
4138 | ddf->virt->entries[ent] = vd->entries[0]; | |
4139 | ddf->virt->populated_vdes = __cpu_to_be16(1 + | |
613b0d17 | 4140 | __be16_to_cpu(ddf->virt->populated_vdes)); |
7d5a7ff3 | 4141 | ddf_set_updates_pending(ddf); |
88c164f4 NB |
4142 | break; |
4143 | ||
4144 | case DDF_VD_CONF_MAGIC: | |
2c514b71 | 4145 | dprintf("len %d %d\n", update->len, ddf->conf_rec_len); |
88c164f4 NB |
4146 | |
4147 | mppe = __be16_to_cpu(ddf->anchor.max_primary_element_entries); | |
f21e18ca | 4148 | if ((unsigned)update->len != ddf->conf_rec_len * 512) |
88c164f4 NB |
4149 | return; |
4150 | vc = (struct vd_config*)update->buf; | |
4151 | for (vcl = ddf->conflist; vcl ; vcl = vcl->next) | |
4152 | if (memcmp(vcl->conf.guid, vc->guid, DDF_GUID_LEN) == 0) | |
4153 | break; | |
2c514b71 | 4154 | dprintf("vcl = %p\n", vcl); |
88c164f4 NB |
4155 | if (vcl) { |
4156 | /* An update, just copy the phys_refnum and lba_offset | |
4157 | * fields | |
4158 | */ | |
4159 | memcpy(vcl->conf.phys_refnum, vc->phys_refnum, | |
4160 | mppe * (sizeof(__u32) + sizeof(__u64))); | |
4161 | } else { | |
4162 | /* A new VD_CONF */ | |
e6b9548d DW |
4163 | if (!update->space) |
4164 | return; | |
88c164f4 NB |
4165 | vcl = update->space; |
4166 | update->space = NULL; | |
4167 | vcl->next = ddf->conflist; | |
edd8d13c | 4168 | memcpy(&vcl->conf, vc, update->len); |
88c164f4 NB |
4169 | vcl->lba_offset = (__u64*) |
4170 | &vcl->conf.phys_refnum[mppe]; | |
fb9d0acb | 4171 | ent = find_vde_by_guid(ddf, vc->guid); |
4172 | if (ent == DDF_NOTFOUND) | |
4173 | return; | |
4174 | vcl->vcnum = ent; | |
88c164f4 NB |
4175 | ddf->conflist = vcl; |
4176 | } | |
c7079c84 N |
4177 | /* Set DDF_Transition on all Failed devices - to help |
4178 | * us detect those that are no longer in use | |
4179 | */ | |
4180 | for (pdnum = 0; pdnum < __be16_to_cpu(ddf->phys->used_pdes); pdnum++) | |
4181 | if (ddf->phys->entries[pdnum].state | |
4182 | & __be16_to_cpu(DDF_Failed)) | |
4183 | ddf->phys->entries[pdnum].state | |
4184 | |= __be16_to_cpu(DDF_Transition); | |
88c164f4 NB |
4185 | /* Now make sure vlist is correct for each dl. */ |
4186 | for (dl = ddf->dlist; dl; dl = dl->next) { | |
f21e18ca N |
4187 | unsigned int dn; |
4188 | unsigned int vn = 0; | |
8401644c | 4189 | int in_degraded = 0; |
88c164f4 NB |
4190 | for (vcl = ddf->conflist; vcl ; vcl = vcl->next) |
4191 | for (dn=0; dn < ddf->mppe ; dn++) | |
4192 | if (vcl->conf.phys_refnum[dn] == | |
4193 | dl->disk.refnum) { | |
8401644c | 4194 | int vstate; |
2c514b71 NB |
4195 | dprintf("dev %d has %p at %d\n", |
4196 | dl->pdnum, vcl, vn); | |
c7079c84 N |
4197 | /* Clear the Transition flag */ |
4198 | if (ddf->phys->entries[dl->pdnum].state | |
4199 | & __be16_to_cpu(DDF_Failed)) | |
4200 | ddf->phys->entries[dl->pdnum].state &= | |
4201 | ~__be16_to_cpu(DDF_Transition); | |
4202 | ||
88c164f4 | 4203 | dl->vlist[vn++] = vcl; |
8401644c N |
4204 | vstate = ddf->virt->entries[vcl->vcnum].state |
4205 | & DDF_state_mask; | |
4206 | if (vstate == DDF_state_degraded || | |
4207 | vstate == DDF_state_part_optimal) | |
4208 | in_degraded = 1; | |
88c164f4 NB |
4209 | break; |
4210 | } | |
4211 | while (vn < ddf->max_part) | |
4212 | dl->vlist[vn++] = NULL; | |
7e1432fb NB |
4213 | if (dl->vlist[0]) { |
4214 | ddf->phys->entries[dl->pdnum].type &= | |
4215 | ~__cpu_to_be16(DDF_Global_Spare); | |
8401644c N |
4216 | if (!(ddf->phys->entries[dl->pdnum].type & |
4217 | __cpu_to_be16(DDF_Active_in_VD))) { | |
613b0d17 N |
4218 | ddf->phys->entries[dl->pdnum].type |= |
4219 | __cpu_to_be16(DDF_Active_in_VD); | |
4220 | if (in_degraded) | |
4221 | ddf->phys->entries[dl->pdnum].state |= | |
4222 | __cpu_to_be16(DDF_Rebuilding); | |
4223 | } | |
7e1432fb NB |
4224 | } |
4225 | if (dl->spare) { | |
4226 | ddf->phys->entries[dl->pdnum].type &= | |
4227 | ~__cpu_to_be16(DDF_Global_Spare); | |
4228 | ddf->phys->entries[dl->pdnum].type |= | |
4229 | __cpu_to_be16(DDF_Spare); | |
4230 | } | |
4231 | if (!dl->vlist[0] && !dl->spare) { | |
4232 | ddf->phys->entries[dl->pdnum].type |= | |
4233 | __cpu_to_be16(DDF_Global_Spare); | |
4234 | ddf->phys->entries[dl->pdnum].type &= | |
4235 | ~__cpu_to_be16(DDF_Spare | | |
4236 | DDF_Active_in_VD); | |
4237 | } | |
88c164f4 | 4238 | } |
c7079c84 N |
4239 | |
4240 | /* Now remove any 'Failed' devices that are not part | |
4241 | * of any VD. They will have the Transition flag set. | |
4242 | * Once done, we need to update all dl->pdnum numbers. | |
4243 | */ | |
4244 | pd2 = 0; | |
4245 | for (pdnum = 0; pdnum < __be16_to_cpu(ddf->phys->used_pdes); pdnum++) | |
4246 | if ((ddf->phys->entries[pdnum].state | |
4247 | & __be16_to_cpu(DDF_Failed)) | |
4248 | && (ddf->phys->entries[pdnum].state | |
4249 | & __be16_to_cpu(DDF_Transition))) | |
4250 | /* skip this one */; | |
4251 | else if (pdnum == pd2) | |
4252 | pd2++; | |
4253 | else { | |
4254 | ddf->phys->entries[pd2] = ddf->phys->entries[pdnum]; | |
4255 | for (dl = ddf->dlist; dl; dl = dl->next) | |
4256 | if (dl->pdnum == (int)pdnum) | |
4257 | dl->pdnum = pd2; | |
4258 | pd2++; | |
4259 | } | |
4260 | ddf->phys->used_pdes = __cpu_to_be16(pd2); | |
4261 | while (pd2 < pdnum) { | |
4262 | memset(ddf->phys->entries[pd2].guid, 0xff, DDF_GUID_LEN); | |
4263 | pd2++; | |
4264 | } | |
4265 | ||
7d5a7ff3 | 4266 | ddf_set_updates_pending(ddf); |
88c164f4 NB |
4267 | break; |
4268 | case DDF_SPARE_ASSIGN_MAGIC: | |
4269 | default: break; | |
4270 | } | |
4271 | } | |
4272 | ||
edd8d13c NB |
4273 | static void ddf_prepare_update(struct supertype *st, |
4274 | struct metadata_update *update) | |
4275 | { | |
4276 | /* This update arrived at managemon. | |
4277 | * We are about to pass it to monitor. | |
4278 | * If a malloc is needed, do it here. | |
4279 | */ | |
4280 | struct ddf_super *ddf = st->sb; | |
4281 | __u32 *magic = (__u32*)update->buf; | |
4282 | if (*magic == DDF_VD_CONF_MAGIC) | |
e6b9548d | 4283 | if (posix_memalign(&update->space, 512, |
613b0d17 N |
4284 | offsetof(struct vcl, conf) |
4285 | + ddf->conf_rec_len * 512) != 0) | |
e6b9548d | 4286 | update->space = NULL; |
edd8d13c NB |
4287 | } |
4288 | ||
7e1432fb NB |
4289 | /* |
4290 | * Check if the array 'a' is degraded but not failed. | |
4291 | * If it is, find as many spares as are available and needed and | |
4292 | * arrange for their inclusion. | |
4293 | * We only choose devices which are not already in the array, | |
4294 | * and prefer those with a spare-assignment to this array. | |
4295 | * otherwise we choose global spares - assuming always that | |
4296 | * there is enough room. | |
4297 | * For each spare that we assign, we return an 'mdinfo' which | |
4298 | * describes the position for the device in the array. | |
4299 | * We also add to 'updates' a DDF_VD_CONF_MAGIC update with | |
4300 | * the new phys_refnum and lba_offset values. | |
4301 | * | |
4302 | * Only worry about BVDs at the moment. | |
4303 | */ | |
4304 | static struct mdinfo *ddf_activate_spare(struct active_array *a, | |
4305 | struct metadata_update **updates) | |
4306 | { | |
4307 | int working = 0; | |
4308 | struct mdinfo *d; | |
4309 | struct ddf_super *ddf = a->container->sb; | |
4310 | int global_ok = 0; | |
4311 | struct mdinfo *rv = NULL; | |
4312 | struct mdinfo *di; | |
4313 | struct metadata_update *mu; | |
4314 | struct dl *dl; | |
4315 | int i; | |
baba3f4e | 4316 | struct vcl *vcl; |
7e1432fb NB |
4317 | struct vd_config *vc; |
4318 | __u64 *lba; | |
baba3f4e | 4319 | unsigned int n_bvd; |
7e1432fb | 4320 | |
7e1432fb NB |
4321 | for (d = a->info.devs ; d ; d = d->next) { |
4322 | if ((d->curr_state & DS_FAULTY) && | |
613b0d17 | 4323 | d->state_fd >= 0) |
7e1432fb NB |
4324 | /* wait for Removal to happen */ |
4325 | return NULL; | |
4326 | if (d->state_fd >= 0) | |
4327 | working ++; | |
4328 | } | |
4329 | ||
2c514b71 NB |
4330 | dprintf("ddf_activate: working=%d (%d) level=%d\n", working, a->info.array.raid_disks, |
4331 | a->info.array.level); | |
7e1432fb NB |
4332 | if (working == a->info.array.raid_disks) |
4333 | return NULL; /* array not degraded */ | |
4334 | switch (a->info.array.level) { | |
4335 | case 1: | |
4336 | if (working == 0) | |
4337 | return NULL; /* failed */ | |
4338 | break; | |
4339 | case 4: | |
4340 | case 5: | |
4341 | if (working < a->info.array.raid_disks - 1) | |
4342 | return NULL; /* failed */ | |
4343 | break; | |
4344 | case 6: | |
4345 | if (working < a->info.array.raid_disks - 2) | |
4346 | return NULL; /* failed */ | |
4347 | break; | |
4348 | default: /* concat or stripe */ | |
4349 | return NULL; /* failed */ | |
4350 | } | |
4351 | ||
4352 | /* For each slot, if it is not working, find a spare */ | |
4353 | dl = ddf->dlist; | |
4354 | for (i = 0; i < a->info.array.raid_disks; i++) { | |
4355 | for (d = a->info.devs ; d ; d = d->next) | |
4356 | if (d->disk.raid_disk == i) | |
4357 | break; | |
2c514b71 | 4358 | dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0); |
7e1432fb NB |
4359 | if (d && (d->state_fd >= 0)) |
4360 | continue; | |
4361 | ||
4362 | /* OK, this device needs recovery. Find a spare */ | |
4363 | again: | |
4364 | for ( ; dl ; dl = dl->next) { | |
4365 | unsigned long long esize; | |
4366 | unsigned long long pos; | |
4367 | struct mdinfo *d2; | |
4368 | int is_global = 0; | |
4369 | int is_dedicated = 0; | |
4370 | struct extent *ex; | |
f21e18ca | 4371 | unsigned int j; |
7e1432fb NB |
4372 | /* If in this array, skip */ |
4373 | for (d2 = a->info.devs ; d2 ; d2 = d2->next) | |
7590d562 N |
4374 | if (d2->state_fd >= 0 && |
4375 | d2->disk.major == dl->major && | |
7e1432fb | 4376 | d2->disk.minor == dl->minor) { |
2c514b71 | 4377 | dprintf("%x:%x already in array\n", dl->major, dl->minor); |
7e1432fb NB |
4378 | break; |
4379 | } | |
4380 | if (d2) | |
4381 | continue; | |
4382 | if (ddf->phys->entries[dl->pdnum].type & | |
4383 | __cpu_to_be16(DDF_Spare)) { | |
4384 | /* Check spare assign record */ | |
4385 | if (dl->spare) { | |
4386 | if (dl->spare->type & DDF_spare_dedicated) { | |
4387 | /* check spare_ents for guid */ | |
4388 | for (j = 0 ; | |
4389 | j < __be16_to_cpu(dl->spare->populated); | |
4390 | j++) { | |
4391 | if (memcmp(dl->spare->spare_ents[j].guid, | |
4392 | ddf->virt->entries[a->info.container_member].guid, | |
4393 | DDF_GUID_LEN) == 0) | |
4394 | is_dedicated = 1; | |
4395 | } | |
4396 | } else | |
4397 | is_global = 1; | |
4398 | } | |
4399 | } else if (ddf->phys->entries[dl->pdnum].type & | |
4400 | __cpu_to_be16(DDF_Global_Spare)) { | |
4401 | is_global = 1; | |
e0e7aeaa N |
4402 | } else if (!(ddf->phys->entries[dl->pdnum].state & |
4403 | __cpu_to_be16(DDF_Failed))) { | |
4404 | /* we can possibly use some of this */ | |
4405 | is_global = 1; | |
7e1432fb NB |
4406 | } |
4407 | if ( ! (is_dedicated || | |
4408 | (is_global && global_ok))) { | |
2c514b71 | 4409 | dprintf("%x:%x not suitable: %d %d\n", dl->major, dl->minor, |
613b0d17 | 4410 | is_dedicated, is_global); |
7e1432fb NB |
4411 | continue; |
4412 | } | |
4413 | ||
4414 | /* We are allowed to use this device - is there space? | |
4415 | * We need a->info.component_size sectors */ | |
4416 | ex = get_extents(ddf, dl); | |
4417 | if (!ex) { | |
2c514b71 | 4418 | dprintf("cannot get extents\n"); |
7e1432fb NB |
4419 | continue; |
4420 | } | |
4421 | j = 0; pos = 0; | |
4422 | esize = 0; | |
4423 | ||
4424 | do { | |
4425 | esize = ex[j].start - pos; | |
4426 | if (esize >= a->info.component_size) | |
4427 | break; | |
e5cc7d46 N |
4428 | pos = ex[j].start + ex[j].size; |
4429 | j++; | |
4430 | } while (ex[j-1].size); | |
7e1432fb NB |
4431 | |
4432 | free(ex); | |
4433 | if (esize < a->info.component_size) { | |
e5cc7d46 N |
4434 | dprintf("%x:%x has no room: %llu %llu\n", |
4435 | dl->major, dl->minor, | |
2c514b71 | 4436 | esize, a->info.component_size); |
7e1432fb NB |
4437 | /* No room */ |
4438 | continue; | |
4439 | } | |
4440 | ||
4441 | /* Cool, we have a device with some space at pos */ | |
503975b9 | 4442 | di = xcalloc(1, sizeof(*di)); |
7e1432fb NB |
4443 | di->disk.number = i; |
4444 | di->disk.raid_disk = i; | |
4445 | di->disk.major = dl->major; | |
4446 | di->disk.minor = dl->minor; | |
4447 | di->disk.state = 0; | |
d23534e4 | 4448 | di->recovery_start = 0; |
7e1432fb NB |
4449 | di->data_offset = pos; |
4450 | di->component_size = a->info.component_size; | |
4451 | di->container_member = dl->pdnum; | |
4452 | di->next = rv; | |
4453 | rv = di; | |
2c514b71 NB |
4454 | dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor, |
4455 | i, pos); | |
7e1432fb NB |
4456 | |
4457 | break; | |
4458 | } | |
4459 | if (!dl && ! global_ok) { | |
4460 | /* not enough dedicated spares, try global */ | |
4461 | global_ok = 1; | |
4462 | dl = ddf->dlist; | |
4463 | goto again; | |
4464 | } | |
4465 | } | |
4466 | ||
4467 | if (!rv) | |
4468 | /* No spares found */ | |
4469 | return rv; | |
4470 | /* Now 'rv' has a list of devices to return. | |
4471 | * Create a metadata_update record to update the | |
4472 | * phys_refnum and lba_offset values | |
4473 | */ | |
503975b9 N |
4474 | mu = xmalloc(sizeof(*mu)); |
4475 | if (posix_memalign(&mu->space, 512, sizeof(struct vcl)) != 0) { | |
79244939 DW |
4476 | free(mu); |
4477 | mu = NULL; | |
4478 | } | |
503975b9 | 4479 | mu->buf = xmalloc(ddf->conf_rec_len * 512); |
7590d562 N |
4480 | mu->len = ddf->conf_rec_len * 512; |
4481 | mu->space = NULL; | |
f50ae22e | 4482 | mu->space_list = NULL; |
7e1432fb | 4483 | mu->next = *updates; |
baba3f4e | 4484 | vc = find_vdcr(ddf, a->info.container_member, di->disk.raid_disk, |
4485 | &n_bvd, &vcl); | |
7e1432fb NB |
4486 | memcpy(mu->buf, vc, ddf->conf_rec_len * 512); |
4487 | ||
4488 | vc = (struct vd_config*)mu->buf; | |
4489 | lba = (__u64*)&vc->phys_refnum[ddf->mppe]; | |
4490 | for (di = rv ; di ; di = di->next) { | |
4491 | vc->phys_refnum[di->disk.raid_disk] = | |
4492 | ddf->phys->entries[dl->pdnum].refnum; | |
4493 | lba[di->disk.raid_disk] = di->data_offset; | |
4494 | } | |
4495 | *updates = mu; | |
4496 | return rv; | |
4497 | } | |
0e600426 | 4498 | #endif /* MDASSEMBLE */ |
7e1432fb | 4499 | |
b640a252 N |
4500 | static int ddf_level_to_layout(int level) |
4501 | { | |
4502 | switch(level) { | |
4503 | case 0: | |
4504 | case 1: | |
4505 | return 0; | |
4506 | case 5: | |
4507 | return ALGORITHM_LEFT_SYMMETRIC; | |
4508 | case 6: | |
4509 | return ALGORITHM_ROTATING_N_CONTINUE; | |
4510 | case 10: | |
4511 | return 0x102; | |
4512 | default: | |
4513 | return UnSet; | |
4514 | } | |
4515 | } | |
4516 | ||
30f58b22 DW |
4517 | static void default_geometry_ddf(struct supertype *st, int *level, int *layout, int *chunk) |
4518 | { | |
4519 | if (level && *level == UnSet) | |
4520 | *level = LEVEL_CONTAINER; | |
4521 | ||
4522 | if (level && layout && *layout == UnSet) | |
4523 | *layout = ddf_level_to_layout(*level); | |
4524 | } | |
4525 | ||
a322f70c DW |
4526 | struct superswitch super_ddf = { |
4527 | #ifndef MDASSEMBLE | |
4528 | .examine_super = examine_super_ddf, | |
4529 | .brief_examine_super = brief_examine_super_ddf, | |
4737ae25 | 4530 | .brief_examine_subarrays = brief_examine_subarrays_ddf, |
bceedeec | 4531 | .export_examine_super = export_examine_super_ddf, |
a322f70c DW |
4532 | .detail_super = detail_super_ddf, |
4533 | .brief_detail_super = brief_detail_super_ddf, | |
4534 | .validate_geometry = validate_geometry_ddf, | |
78e44928 | 4535 | .write_init_super = write_init_super_ddf, |
0e600426 | 4536 | .add_to_super = add_to_super_ddf, |
4dd968cc | 4537 | .remove_from_super = remove_from_super_ddf, |
2b959fbf | 4538 | .load_container = load_container_ddf, |
74db60b0 | 4539 | .copy_metadata = copy_metadata_ddf, |
a322f70c DW |
4540 | #endif |
4541 | .match_home = match_home_ddf, | |
4542 | .uuid_from_super= uuid_from_super_ddf, | |
4543 | .getinfo_super = getinfo_super_ddf, | |
4544 | .update_super = update_super_ddf, | |
4545 | ||
4546 | .avail_size = avail_size_ddf, | |
4547 | ||
a19c88b8 NB |
4548 | .compare_super = compare_super_ddf, |
4549 | ||
a322f70c | 4550 | .load_super = load_super_ddf, |
ba7eb04f | 4551 | .init_super = init_super_ddf, |
955e9ea1 | 4552 | .store_super = store_super_ddf, |
a322f70c DW |
4553 | .free_super = free_super_ddf, |
4554 | .match_metadata_desc = match_metadata_desc_ddf, | |
78e44928 | 4555 | .container_content = container_content_ddf, |
30f58b22 | 4556 | .default_geometry = default_geometry_ddf, |
a322f70c | 4557 | |
a322f70c | 4558 | .external = 1, |
549e9569 | 4559 | |
0e600426 | 4560 | #ifndef MDASSEMBLE |
549e9569 NB |
4561 | /* for mdmon */ |
4562 | .open_new = ddf_open_new, | |
ed9d66aa | 4563 | .set_array_state= ddf_set_array_state, |
549e9569 NB |
4564 | .set_disk = ddf_set_disk, |
4565 | .sync_metadata = ddf_sync_metadata, | |
88c164f4 | 4566 | .process_update = ddf_process_update, |
edd8d13c | 4567 | .prepare_update = ddf_prepare_update, |
7e1432fb | 4568 | .activate_spare = ddf_activate_spare, |
0e600426 | 4569 | #endif |
4cce4069 | 4570 | .name = "ddf", |
a322f70c | 4571 | }; |