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