]> git.ipfire.org Git - thirdparty/mdadm.git/blob - super-ddf.c
c7dcc1ae1abe6749a27b01d93d9e8aa1791f7655
[thirdparty/mdadm.git] / super-ddf.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2006-2007 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 static inline int ROUND_UP(int a, int base)
35 {
36 return ((a+base-1)/base)*base;
37 }
38
39 /* a non-official T10 name for creation GUIDs */
40 static char T10[] = "Linux-MD";
41
42 /* DDF timestamps are 1980 based, so we need to add
43 * second-in-decade-of-seventies to convert to linux timestamps.
44 * 10 years with 2 leap years.
45 */
46 #define DECADE (3600*24*(365*10+2))
47 unsigned long crc32(
48 unsigned long crc,
49 const unsigned char *buf,
50 unsigned len);
51
52 /* The DDF metadata handling.
53 * DDF metadata lives at the end of the device.
54 * The last 512 byte block provides an 'anchor' which is used to locate
55 * the rest of the metadata which usually lives immediately behind the anchor.
56 *
57 * Note:
58 * - all multibyte numeric fields are bigendian.
59 * - all strings are space padded.
60 *
61 */
62
63 /* Primary Raid Level (PRL) */
64 #define DDF_RAID0 0x00
65 #define DDF_RAID1 0x01
66 #define DDF_RAID3 0x03
67 #define DDF_RAID4 0x04
68 #define DDF_RAID5 0x05
69 #define DDF_RAID1E 0x11
70 #define DDF_JBOD 0x0f
71 #define DDF_CONCAT 0x1f
72 #define DDF_RAID5E 0x15
73 #define DDF_RAID5EE 0x25
74 #define DDF_RAID6 0x16 /* Vendor unique layout */
75
76 /* Raid Level Qualifier (RLQ) */
77 #define DDF_RAID0_SIMPLE 0x00
78 #define DDF_RAID1_SIMPLE 0x00 /* just 2 devices in this plex */
79 #define DDF_RAID1_MULTI 0x01 /* exactly 3 devices in this plex */
80 #define DDF_RAID3_0 0x00 /* parity in first extent */
81 #define DDF_RAID3_N 0x01 /* parity in last extent */
82 #define DDF_RAID4_0 0x00 /* parity in first extent */
83 #define DDF_RAID4_N 0x01 /* parity in last extent */
84 /* these apply to raid5e and raid5ee as well */
85 #define DDF_RAID5_0_RESTART 0x00 /* same as 'right asymmetric' - layout 1 */
86 #define DDF_RAID5_N_RESTART 0x02 /* same as 'left asymmetric' - layout 0 */
87 #define DDF_RAID5_N_CONTINUE 0x03 /* same as 'left symmetric' - layout 2 */
88
89 #define DDF_RAID1E_ADJACENT 0x00 /* raid10 nearcopies==2 */
90 #define DDF_RAID1E_OFFSET 0x01 /* raid10 offsetcopies==2 */
91
92 /* Secondary RAID Level (SRL) */
93 #define DDF_2STRIPED 0x00 /* This is weirder than RAID0 !! */
94 #define DDF_2MIRRORED 0x01
95 #define DDF_2CONCAT 0x02
96 #define DDF_2SPANNED 0x03 /* This is also weird - be careful */
97
98 /* Magic numbers */
99 #define DDF_HEADER_MAGIC __cpu_to_be32(0xDE11DE11)
100 #define DDF_CONTROLLER_MAGIC __cpu_to_be32(0xAD111111)
101 #define DDF_PHYS_RECORDS_MAGIC __cpu_to_be32(0x22222222)
102 #define DDF_PHYS_DATA_MAGIC __cpu_to_be32(0x33333333)
103 #define DDF_VIRT_RECORDS_MAGIC __cpu_to_be32(0xDDDDDDDD)
104 #define DDF_VD_CONF_MAGIC __cpu_to_be32(0xEEEEEEEE)
105 #define DDF_SPARE_ASSIGN_MAGIC __cpu_to_be32(0x55555555)
106 #define DDF_VU_CONF_MAGIC __cpu_to_be32(0x88888888)
107 #define DDF_VENDOR_LOG_MAGIC __cpu_to_be32(0x01dBEEF0)
108 #define DDF_BBM_LOG_MAGIC __cpu_to_be32(0xABADB10C)
109
110 #define DDF_GUID_LEN 24
111 #define DDF_REVISION "01.00.00"
112
113 struct ddf_header {
114 __u32 magic;
115 __u32 crc;
116 char guid[DDF_GUID_LEN];
117 char revision[8]; /* 01.00.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;
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;
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
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;
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;
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
315 /* vd_config.cache_pol[7] is a bitmap */
316 #define DDF_cache_writeback 1 /* else writethrough */
317 #define DDF_cache_wadaptive 2 /* only applies if writeback */
318 #define DDF_cache_readahead 4
319 #define DDF_cache_radaptive 8 /* only if doing read-ahead */
320 #define DDF_cache_ifnobatt 16 /* even to write cache if battery is poor */
321 #define DDF_cache_wallowed 32 /* enable write caching */
322 #define DDF_cache_rallowed 64 /* enable read caching */
323
324 struct spare_assign {
325 __u32 magic;
326 __u32 crc;
327 __u32 timestamp;
328 __u8 reserved[7];
329 __u8 type;
330 __u16 populated; /* SAEs used */
331 __u16 max; /* max SAEs */
332 __u8 pad[8];
333 struct spare_assign_entry {
334 char guid[DDF_GUID_LEN];
335 __u16 secondary_element;
336 __u8 pad[6];
337 } spare_ents[0];
338 };
339 /* spare_assign.type is a bitmap */
340 #define DDF_spare_dedicated 0x1 /* else global */
341 #define DDF_spare_revertible 0x2 /* else committable */
342 #define DDF_spare_active 0x4 /* else not active */
343 #define DDF_spare_affinity 0x8 /* enclosure affinity */
344
345 /* The data_section contents - local scope */
346 struct disk_data {
347 __u32 magic;
348 __u32 crc;
349 char guid[DDF_GUID_LEN];
350 __u32 refnum; /* crc of some magic drive data ... */
351 __u8 forced_ref; /* set when above was not result of magic */
352 __u8 forced_guid; /* set if guid was forced rather than magic */
353 __u8 vendor[32];
354 __u8 pad[442];
355 };
356
357 /* bbm_section content */
358 struct bad_block_log {
359 __u32 magic;
360 __u32 crc;
361 __u16 entry_count;
362 __u32 spare_count;
363 __u8 pad[10];
364 __u64 first_spare;
365 struct mapped_block {
366 __u64 defective_start;
367 __u32 replacement_start;
368 __u16 remap_count;
369 __u8 pad[2];
370 } entries[0];
371 };
372
373 /* Struct for internally holding ddf structures */
374 /* The DDF structure stored on each device is potentially
375 * quite different, as some data is global and some is local.
376 * The global data is:
377 * - ddf header
378 * - controller_data
379 * - Physical disk records
380 * - Virtual disk records
381 * The local data is:
382 * - Configuration records
383 * - Physical Disk data section
384 * ( and Bad block and vendor which I don't care about yet).
385 *
386 * The local data is parsed into separate lists as it is read
387 * and reconstructed for writing. This means that we only need
388 * to make config changes once and they are automatically
389 * propagated to all devices.
390 * Note that the ddf_super has space of the conf and disk data
391 * for this disk and also for a list of all such data.
392 * The list is only used for the superblock that is being
393 * built in Create or Assemble to describe the whole array.
394 */
395 struct ddf_super {
396 struct ddf_header anchor, primary, secondary, *active;
397 struct ddf_controller_data controller;
398 struct phys_disk *phys;
399 struct virtual_disk *virt;
400 int pdsize, vdsize;
401 int max_part;
402 struct vcl {
403 struct vcl *next;
404 __u64 *lba_offset; /* location in 'conf' of
405 * the lba table */
406 struct vd_config conf;
407 } *conflist, *newconf;
408 struct dl {
409 struct dl *next;
410 struct disk_data disk;
411 int major, minor;
412 char *devname;
413 int fd;
414 struct vcl *vlist[0]; /* max_part+1 in size */
415 } *dlist;
416 };
417
418 #ifndef offsetof
419 #define offsetof(t,f) ((size_t)&(((t*)0)->f))
420 #endif
421
422 extern struct superswitch super_ddf_container, super_ddf_bvd, super_ddf;
423
424 static int calc_crc(void *buf, int len)
425 {
426 /* crcs are always at the same place as in the ddf_header */
427 struct ddf_header *ddf = buf;
428 __u32 oldcrc = ddf->crc;
429 __u32 newcrc;
430 ddf->crc = 0xffffffff;
431
432 newcrc = crc32(0, buf, len);
433 ddf->crc = oldcrc;
434 return newcrc;
435 }
436
437 static int load_ddf_header(int fd, unsigned long long lba,
438 unsigned long long size,
439 int type,
440 struct ddf_header *hdr, struct ddf_header *anchor)
441 {
442 /* read a ddf header (primary or secondary) from fd/lba
443 * and check that it is consistent with anchor
444 * Need to check:
445 * magic, crc, guid, rev, and LBA's header_type, and
446 * everything after header_type must be the same
447 */
448 if (lba >= size-1)
449 return 0;
450
451 if (lseek64(fd, lba<<9, 0) < 0)
452 return 0;
453
454 if (read(fd, hdr, 512) != 512)
455 return 0;
456
457 if (hdr->magic != DDF_HEADER_MAGIC)
458 return 0;
459 if (calc_crc(hdr, 512) != hdr->crc)
460 return 0;
461 if (memcmp(anchor->guid, hdr->guid, DDF_GUID_LEN) != 0 ||
462 memcmp(anchor->revision, hdr->revision, 8) != 0 ||
463 anchor->primary_lba != hdr->primary_lba ||
464 anchor->secondary_lba != hdr->secondary_lba ||
465 hdr->type != type ||
466 memcmp(anchor->pad2, hdr->pad2, 512 -
467 offsetof(struct ddf_header, pad2)) != 0)
468 return 0;
469
470 /* Looks good enough to me... */
471 return 1;
472 }
473
474 static void *load_section(int fd, struct ddf_super *super, void *buf,
475 __u32 offset_be, __u32 len_be, int check)
476 {
477 unsigned long long offset = __be32_to_cpu(offset_be);
478 unsigned long long len = __be32_to_cpu(len_be);
479 int dofree = (buf == NULL);
480
481 if (check)
482 if (len != 2 && len != 8 && len != 32
483 && len != 128 && len != 512)
484 return NULL;
485
486 if (len > 1024)
487 return NULL;
488 if (buf) {
489 /* All pre-allocated sections are a single block */
490 if (len != 1)
491 return NULL;
492 } else
493 buf = malloc(len<<9);
494 if (!buf)
495 return NULL;
496
497 if (super->active->type == 1)
498 offset += __be64_to_cpu(super->active->primary_lba);
499 else
500 offset += __be64_to_cpu(super->active->secondary_lba);
501
502 if (lseek64(fd, offset<<9, 0) != (offset<<9)) {
503 if (dofree)
504 free(buf);
505 return NULL;
506 }
507 if (read(fd, buf, len<<9) != (len<<9)) {
508 if (dofree)
509 free(buf);
510 return NULL;
511 }
512 return buf;
513 }
514
515 static int load_ddf_headers(int fd, struct ddf_super *super, char *devname)
516 {
517 unsigned long long dsize;
518
519 get_dev_size(fd, NULL, &dsize);
520
521 if (lseek64(fd, dsize-512, 0) < 0) {
522 if (devname)
523 fprintf(stderr,
524 Name": Cannot seek to anchor block on %s: %s\n",
525 devname, strerror(errno));
526 return 1;
527 }
528 if (read(fd, &super->anchor, 512) != 512) {
529 if (devname)
530 fprintf(stderr,
531 Name ": Cannot read anchor block on %s: %s\n",
532 devname, strerror(errno));
533 return 1;
534 }
535 if (super->anchor.magic != DDF_HEADER_MAGIC) {
536 if (devname)
537 fprintf(stderr, Name ": no DDF anchor found on %s\n",
538 devname);
539 return 2;
540 }
541 if (calc_crc(&super->anchor, 512) != super->anchor.crc) {
542 if (devname)
543 fprintf(stderr, Name ": bad CRC on anchor on %s\n",
544 devname);
545 return 2;
546 }
547 if (memcmp(super->anchor.revision, DDF_REVISION, 8) != 0) {
548 if (devname)
549 fprintf(stderr, Name ": can only support super revision"
550 " %.8s, not %.8s on %s\n",
551 DDF_REVISION, super->anchor.revision, devname);
552 return 2;
553 }
554 if (load_ddf_header(fd, __be64_to_cpu(super->anchor.primary_lba),
555 dsize >> 9, 1,
556 &super->primary, &super->anchor) == 0) {
557 if (devname)
558 fprintf(stderr,
559 Name ": Failed to load primary DDF header "
560 "on %s\n", devname);
561 return 2;
562 }
563 super->active = &super->primary;
564 if (load_ddf_header(fd, __be64_to_cpu(super->anchor.secondary_lba),
565 dsize >> 9, 2,
566 &super->secondary, &super->anchor)) {
567 if ((__be32_to_cpu(super->primary.seq)
568 < __be32_to_cpu(super->secondary.seq) &&
569 !super->secondary.openflag)
570 || (__be32_to_cpu(super->primary.seq)
571 == __be32_to_cpu(super->secondary.seq) &&
572 super->primary.openflag && !super->secondary.openflag)
573 )
574 super->active = &super->secondary;
575 }
576 return 0;
577 }
578
579 static int load_ddf_global(int fd, struct ddf_super *super, char *devname)
580 {
581 void *ok;
582 ok = load_section(fd, super, &super->controller,
583 super->active->controller_section_offset,
584 super->active->controller_section_length,
585 0);
586 super->phys = load_section(fd, super, NULL,
587 super->active->phys_section_offset,
588 super->active->phys_section_length,
589 1);
590 super->pdsize = __be32_to_cpu(super->active->phys_section_length) * 512;
591
592 super->virt = load_section(fd, super, NULL,
593 super->active->virt_section_offset,
594 super->active->virt_section_length,
595 1);
596 super->vdsize = __be32_to_cpu(super->active->virt_section_length) * 512;
597 if (!ok ||
598 !super->phys ||
599 !super->virt) {
600 free(super->phys);
601 free(super->virt);
602 super->phys = NULL;
603 super->virt = NULL;
604 return 2;
605 }
606 super->conflist = NULL;
607 super->dlist = NULL;
608 return 0;
609 }
610
611 static int load_ddf_local(int fd, struct ddf_super *super,
612 char *devname, int keep)
613 {
614 struct dl *dl;
615 struct stat stb;
616 char *conf;
617 int i;
618 int conflen;
619 int mppe;
620
621 /* First the local disk info */
622 super->max_part = __be16_to_cpu(super->active->max_partitions);
623 dl = malloc(sizeof(*dl) +
624 (super->max_part+1) * sizeof(dl->vlist[0]));
625
626 load_section(fd, super, &dl->disk,
627 super->active->data_section_offset,
628 super->active->data_section_length,
629 0);
630 dl->devname = devname ? strdup(devname) : NULL;
631
632 fstat(fd, &stb);
633 dl->major = major(stb.st_rdev);
634 dl->minor = minor(stb.st_rdev);
635 dl->next = super->dlist;
636 dl->fd = keep ? fd : -1;
637 for (i=0 ; i < super->max_part + 1 ; i++)
638 dl->vlist[i] = NULL;
639 super->dlist = dl;
640
641 /* Now the config list. */
642 /* 'conf' is an array of config entries, some of which are
643 * probably invalid. Those which are good need to be copied into
644 * the conflist
645 */
646 conflen = __be16_to_cpu(super->active->config_record_len);
647
648 conf = load_section(fd, super, NULL,
649 super->active->config_section_offset,
650 super->active->config_section_length,
651 0);
652
653 for (i = 0;
654 i < __be32_to_cpu(super->active->config_section_length);
655 i += conflen) {
656 struct vd_config *vd =
657 (struct vd_config *)((char*)conf + i*512);
658 struct vcl *vcl;
659
660 if (vd->magic != DDF_VD_CONF_MAGIC)
661 continue;
662 for (vcl = super->conflist; vcl; vcl = vcl->next) {
663 if (memcmp(vcl->conf.guid,
664 vd->guid, DDF_GUID_LEN) == 0)
665 break;
666 }
667
668 if (vcl) {
669 dl->vlist[i/conflen] = vcl;
670 if (__be32_to_cpu(vd->seqnum) <=
671 __be32_to_cpu(vcl->conf.seqnum))
672 continue;
673 } else {
674 vcl = malloc(conflen*512 + offsetof(struct vcl, conf));
675 vcl->next = super->conflist;
676 super->conflist = vcl;
677 }
678 memcpy(&vcl->conf, vd, conflen*512);
679 mppe = __be16_to_cpu(super->anchor.max_primary_element_entries);
680 vcl->lba_offset = (__u64*)
681 &vcl->conf.phys_refnum[mppe];
682 dl->vlist[i/conflen] = vcl;
683 }
684 free(conf);
685
686 return 0;
687 }
688
689 #ifndef MDASSEMBLE
690 static int load_super_ddf_all(struct supertype *st, int fd,
691 void **sbp, char *devname, int keep_fd);
692 #endif
693 static int load_super_ddf(struct supertype *st, int fd,
694 char *devname)
695 {
696 unsigned long long dsize;
697 struct ddf_super *super;
698 int rv;
699
700 #ifndef MDASSEMBLE
701 if (load_super_ddf_all(st, fd, &st->sb, devname, 1) == 0)
702 return 0;
703 #endif
704
705 if (get_dev_size(fd, devname, &dsize) == 0)
706 return 1;
707
708 /* 32M is a lower bound */
709 if (dsize <= 32*1024*1024) {
710 if (devname) {
711 fprintf(stderr,
712 Name ": %s is too small for ddf: "
713 "size is %llu sectors.\n",
714 devname, dsize>>9);
715 return 1;
716 }
717 }
718 if (dsize & 511) {
719 if (devname) {
720 fprintf(stderr,
721 Name ": %s is an odd size for ddf: "
722 "size is %llu bytes.\n",
723 devname, dsize);
724 return 1;
725 }
726 }
727
728 super = malloc(sizeof(*super));
729 if (!super) {
730 fprintf(stderr, Name ": malloc of %zu failed.\n",
731 sizeof(*super));
732 return 1;
733 }
734 memset(super, 0, sizeof(*super));
735
736 rv = load_ddf_headers(fd, super, devname);
737 if (rv) {
738 free(super);
739 return rv;
740 }
741
742 /* Have valid headers and have chosen the best. Let's read in the rest*/
743
744 rv = load_ddf_global(fd, super, devname);
745
746 if (rv) {
747 if (devname)
748 fprintf(stderr,
749 Name ": Failed to load all information "
750 "sections on %s\n", devname);
751 free(super);
752 return rv;
753 }
754
755 load_ddf_local(fd, super, devname, 0);
756
757 /* Should possibly check the sections .... */
758
759 st->sb = super;
760 if (st->ss == NULL) {
761 st->ss = &super_ddf;
762 st->minor_version = 0;
763 st->max_devs = 512;
764 }
765 return 0;
766
767 }
768
769 static void free_super_ddf(struct supertype *st)
770 {
771 struct ddf_super *ddf = st->sb;
772 if (ddf == NULL)
773 return;
774 free(ddf->phys);
775 free(ddf->virt);
776 while (ddf->conflist) {
777 struct vcl *v = ddf->conflist;
778 ddf->conflist = v->next;
779 free(v);
780 }
781 while (ddf->dlist) {
782 struct dl *d = ddf->dlist;
783 ddf->dlist = d->next;
784 if (d->fd >= 0)
785 close(d->fd);
786 free(d);
787 }
788 free(ddf);
789 st->sb = NULL;
790 }
791
792 static struct supertype *match_metadata_desc_ddf(char *arg)
793 {
794 /* 'ddf' only support containers */
795 struct supertype *st;
796 if (strcmp(arg, "ddf") != 0 &&
797 strcmp(arg, "default") != 0
798 )
799 return NULL;
800
801 st = malloc(sizeof(*st));
802 st->ss = &super_ddf;
803 st->max_devs = 512;
804 st->minor_version = 0;
805 st->sb = NULL;
806 return st;
807 }
808
809 static struct supertype *match_metadata_desc_ddf_bvd(char *arg)
810 {
811 struct supertype *st;
812 if (strcmp(arg, "ddf/bvd") != 0 &&
813 strcmp(arg, "bvd") != 0 &&
814 strcmp(arg, "default") != 0
815 )
816 return NULL;
817
818 st = malloc(sizeof(*st));
819 st->ss = &super_ddf_bvd;
820 st->max_devs = 512;
821 st->minor_version = 0;
822 st->sb = NULL;
823 return st;
824 }
825 static struct supertype *match_metadata_desc_ddf_svd(char *arg)
826 {
827 struct supertype *st;
828 if (strcmp(arg, "ddf/svd") != 0 &&
829 strcmp(arg, "svd") != 0 &&
830 strcmp(arg, "default") != 0
831 )
832 return NULL;
833
834 st = malloc(sizeof(*st));
835 st->ss = &super_ddf_svd;
836 st->max_devs = 512;
837 st->minor_version = 0;
838 st->sb = NULL;
839 return st;
840 }
841
842 #ifndef MDASSEMBLE
843
844 static mapping_t ddf_state[] = {
845 { "Optimal", 0},
846 { "Degraded", 1},
847 { "Deleted", 2},
848 { "Missing", 3},
849 { "Failed", 4},
850 { "Partially Optimal", 5},
851 { "-reserved-", 6},
852 { "-reserved-", 7},
853 { NULL, 0}
854 };
855
856 static mapping_t ddf_init_state[] = {
857 { "Not Initialised", 0},
858 { "QuickInit in Progress", 1},
859 { "Fully Initialised", 2},
860 { "*UNKNOWN*", 3},
861 { NULL, 0}
862 };
863 static mapping_t ddf_access[] = {
864 { "Read/Write", 0},
865 { "Reserved", 1},
866 { "Read Only", 2},
867 { "Blocked (no access)", 3},
868 { NULL ,0}
869 };
870
871 static mapping_t ddf_level[] = {
872 { "RAID0", DDF_RAID0},
873 { "RAID1", DDF_RAID1},
874 { "RAID3", DDF_RAID3},
875 { "RAID4", DDF_RAID4},
876 { "RAID5", DDF_RAID5},
877 { "RAID1E",DDF_RAID1E},
878 { "JBOD", DDF_JBOD},
879 { "CONCAT",DDF_CONCAT},
880 { "RAID5E",DDF_RAID5E},
881 { "RAID5EE",DDF_RAID5EE},
882 { "RAID6", DDF_RAID6},
883 { NULL, 0}
884 };
885 static mapping_t ddf_sec_level[] = {
886 { "Striped", DDF_2STRIPED},
887 { "Mirrored", DDF_2MIRRORED},
888 { "Concat", DDF_2CONCAT},
889 { "Spanned", DDF_2SPANNED},
890 { NULL, 0}
891 };
892 #endif
893
894 struct num_mapping {
895 int num1, num2;
896 };
897 static struct num_mapping ddf_level_num[] = {
898 { DDF_RAID0, 0 },
899 { DDF_RAID1, 1 },
900 { DDF_RAID3, LEVEL_UNSUPPORTED },
901 { DDF_RAID5, 4 },
902 { DDF_RAID1E, LEVEL_UNSUPPORTED },
903 { DDF_JBOD, LEVEL_UNSUPPORTED },
904 { DDF_CONCAT, LEVEL_LINEAR },
905 { DDF_RAID5E, LEVEL_UNSUPPORTED },
906 { DDF_RAID5EE, LEVEL_UNSUPPORTED },
907 { DDF_RAID6, 6},
908 { MAXINT, MAXINT }
909 };
910
911 static int map_num1(struct num_mapping *map, int num)
912 {
913 int i;
914 for (i=0 ; map[i].num1 != MAXINT; i++)
915 if (map[i].num1 == num)
916 break;
917 return map[i].num2;
918 }
919
920 #ifndef MDASSEMBLE
921 static void print_guid(char *guid, int tstamp)
922 {
923 /* A GUIDs are part (or all) ASCII and part binary.
924 * They tend to be space padded.
925 * We ignore trailing spaces and print numbers
926 * <0x20 and >=0x7f as \xXX
927 * Some GUIDs have a time stamp in bytes 16-19.
928 * We print that if appropriate
929 */
930 int l = DDF_GUID_LEN;
931 int i;
932 while (l && guid[l-1] == ' ')
933 l--;
934 for (i=0 ; i<l ; i++) {
935 if (guid[i] >= 0x20 && guid[i] < 0x7f)
936 fputc(guid[i], stdout);
937 else
938 fprintf(stdout, "\\x%02x", guid[i]&255);
939 }
940 if (tstamp) {
941 time_t then = __be32_to_cpu(*(__u32*)(guid+16)) + DECADE;
942 char tbuf[100];
943 struct tm *tm;
944 tm = localtime(&then);
945 strftime(tbuf, 100, " (%D %T)",tm);
946 fputs(tbuf, stdout);
947 }
948 }
949
950 static void examine_vd(int n, struct ddf_super *sb, char *guid)
951 {
952 int crl = __be16_to_cpu(sb->anchor.config_record_len);
953 struct vcl *vcl;
954
955 for (vcl = sb->conflist ; vcl ; vcl = vcl->next) {
956 struct vd_config *vc = &vcl->conf;
957
958 if (calc_crc(vc, crl*512) != vc->crc)
959 continue;
960 if (memcmp(vc->guid, guid, DDF_GUID_LEN) != 0)
961 continue;
962
963 /* Ok, we know about this VD, let's give more details */
964 printf(" Raid Devices[%d] : %d\n", n,
965 __be16_to_cpu(vc->prim_elmnt_count));
966 printf(" Chunk Size[%d] : %d sectors\n", n,
967 1 << vc->chunk_shift);
968 printf(" Raid Level[%d] : %s\n", n,
969 map_num(ddf_level, vc->prl)?:"-unknown-");
970 if (vc->sec_elmnt_count != 1) {
971 printf(" Secondary Position[%d] : %d of %d\n", n,
972 vc->sec_elmnt_seq, vc->sec_elmnt_count);
973 printf(" Secondary Level[%d] : %s\n", n,
974 map_num(ddf_sec_level, vc->srl) ?: "-unknown-");
975 }
976 printf(" Device Size[%d] : %llu\n", n,
977 __be64_to_cpu(vc->blocks)/2);
978 printf(" Array Size[%d] : %llu\n", n,
979 __be64_to_cpu(vc->array_blocks)/2);
980 }
981 }
982
983 static void examine_vds(struct ddf_super *sb)
984 {
985 int cnt = __be16_to_cpu(sb->virt->populated_vdes);
986 int i;
987 printf(" Virtual Disks : %d\n", cnt);
988
989 for (i=0; i<cnt; i++) {
990 struct virtual_entry *ve = &sb->virt->entries[i];
991 printf(" VD GUID[%d] : ", i); print_guid(ve->guid, 1);
992 printf("\n");
993 printf(" unit[%d] : %d\n", i, __be16_to_cpu(ve->unit));
994 printf(" state[%d] : %s, %s%s\n", i,
995 map_num(ddf_state, ve->state & 7),
996 (ve->state & 8) ? "Morphing, ": "",
997 (ve->state & 16)? "Not Consistent" : "Consistent");
998 printf(" init state[%d] : %s\n", i,
999 map_num(ddf_init_state, ve->init_state&3));
1000 printf(" access[%d] : %s\n", i,
1001 map_num(ddf_access, (ve->init_state>>6) & 3));
1002 printf(" Name[%d] : %.16s\n", i, ve->name);
1003 examine_vd(i, sb, ve->guid);
1004 }
1005 if (cnt) printf("\n");
1006 }
1007
1008 static void examine_pds(struct ddf_super *sb)
1009 {
1010 int cnt = __be16_to_cpu(sb->phys->used_pdes);
1011 int i;
1012 struct dl *dl;
1013 printf(" Physical Disks : %d\n", cnt);
1014
1015 for (i=0 ; i<cnt ; i++) {
1016 struct phys_disk_entry *pd = &sb->phys->entries[i];
1017 int type = __be16_to_cpu(pd->type);
1018 int state = __be16_to_cpu(pd->state);
1019
1020 printf(" PD GUID[%d] : ", i); print_guid(pd->guid, 0);
1021 printf("\n");
1022 printf(" ref[%d] : %08x\n", i,
1023 __be32_to_cpu(pd->refnum));
1024 printf(" mode[%d] : %s%s%s%s%s\n", i,
1025 (type&2) ? "active":"",
1026 (type&4) ? "Global Spare":"",
1027 (type&8) ? "spare" : "",
1028 (type&16)? ", foreign" : "",
1029 (type&32)? "pass-through" : "");
1030 printf(" state[%d] : %s%s%s%s%s%s%s\n", i,
1031 (state&1)? "Online": "Offline",
1032 (state&2)? ", Failed": "",
1033 (state&4)? ", Rebuilding": "",
1034 (state&8)? ", in-transition": "",
1035 (state&16)? ", SMART errors": "",
1036 (state&32)? ", Unrecovered Read Errors": "",
1037 (state&64)? ", Missing" : "");
1038 printf(" Avail Size[%d] : %llu K\n", i,
1039 __be64_to_cpu(pd->config_size)>>1);
1040 for (dl = sb->dlist; dl ; dl = dl->next) {
1041 if (dl->disk.refnum == pd->refnum) {
1042 char *dv = map_dev(dl->major, dl->minor, 0);
1043 if (dv)
1044 printf(" Device[%d] : %s\n",
1045 i, dv);
1046 }
1047 }
1048 printf("\n");
1049 }
1050 }
1051
1052 static void examine_super_ddf(struct supertype *st, char *homehost)
1053 {
1054 struct ddf_super *sb = st->sb;
1055
1056 printf(" Magic : %08x\n", __be32_to_cpu(sb->anchor.magic));
1057 printf(" Version : %.8s\n", sb->anchor.revision);
1058 printf("Controller GUID : "); print_guid(sb->controller.guid, 0);
1059 printf("\n");
1060 printf(" Container GUID : "); print_guid(sb->anchor.guid, 1);
1061 printf("\n");
1062 printf(" Seq : %08x\n", __be32_to_cpu(sb->active->seq));
1063 printf(" Redundant hdr : %s\n", sb->secondary.magic == DDF_HEADER_MAGIC
1064 ?"yes" : "no");
1065 examine_vds(sb);
1066 examine_pds(sb);
1067 }
1068
1069 static void brief_examine_super_ddf(struct supertype *st)
1070 {
1071 /* We just write a generic DDF ARRAY entry
1072 * The uuid is all hex, 6 groups of 4 bytes
1073 */
1074 struct ddf_super *ddf = st->sb;
1075 int i;
1076 printf("ARRAY /dev/ddf UUID=");
1077 for (i = 0; i < DDF_GUID_LEN; i++) {
1078 printf("%02x", ddf->anchor.guid[i]);
1079 if ((i&3) == 0 && i != 0)
1080 printf(":");
1081 }
1082 printf("\n");
1083 }
1084
1085 static void detail_super_ddf(struct supertype *st, char *homehost)
1086 {
1087 /* FIXME later
1088 * Could print DDF GUID
1089 * Need to find which array
1090 * If whole, briefly list all arrays
1091 * If one, give name
1092 */
1093 }
1094
1095 static void brief_detail_super_ddf(struct supertype *st)
1096 {
1097 /* FIXME I really need to know which array we are detailing.
1098 * Can that be stored in ddf_super??
1099 */
1100 // struct ddf_super *ddf = st->sb;
1101 }
1102
1103
1104 #endif
1105
1106 static int match_home_ddf(struct supertype *st, char *homehost)
1107 {
1108 /* It matches 'this' host if the controller is a
1109 * Linux-MD controller with vendor_data matching
1110 * the hostname
1111 */
1112 struct ddf_super *ddf = st->sb;
1113 int len = strlen(homehost);
1114
1115 return (memcmp(ddf->controller.guid, T10, 8) == 0 &&
1116 len < sizeof(ddf->controller.vendor_data) &&
1117 memcmp(ddf->controller.vendor_data, homehost,len) == 0 &&
1118 ddf->controller.vendor_data[len] == 0);
1119 }
1120
1121 static struct vd_config *find_vdcr(struct ddf_super *ddf, int inst)
1122 {
1123 struct vcl *v;
1124 if (inst < 0 || inst > __be16_to_cpu(ddf->virt->populated_vdes))
1125 return NULL;
1126 for (v = ddf->conflist; v; v = v->next)
1127 if (memcmp(v->conf.guid,
1128 ddf->virt->entries[inst].guid,
1129 DDF_GUID_LEN) == 0)
1130 return &v->conf;
1131 return NULL;
1132 }
1133
1134 static int find_phys(struct ddf_super *ddf, __u32 phys_refnum)
1135 {
1136 /* Find the entry in phys_disk which has the given refnum
1137 * and return it's index
1138 */
1139 int i;
1140 for (i=0; i < __be16_to_cpu(ddf->phys->max_pdes); i++)
1141 if (ddf->phys->entries[i].refnum == phys_refnum)
1142 return i;
1143 return -1;
1144 }
1145
1146 static void uuid_from_super_ddf(struct supertype *st, int uuid[4])
1147 {
1148 /* The uuid returned here is used for:
1149 * uuid to put into bitmap file (Create, Grow)
1150 * uuid for backup header when saving critical section (Grow)
1151 * comparing uuids when re-adding a device into an array
1152 * For each of these we can make do with a truncated
1153 * or hashed uuid rather than the original, as long as
1154 * everyone agrees.
1155 * In each case the uuid required is that of the data-array,
1156 * not the device-set.
1157 * In the case of SVD we assume the BVD is of interest,
1158 * though that might be the case if a bitmap were made for
1159 * a mirrored SVD - worry about that later.
1160 * So we need to find the VD configuration record for the
1161 * relevant BVD and extract the GUID and Secondary_Element_Seq.
1162 * The first 16 bytes of the sha1 of these is used.
1163 */
1164 struct ddf_super *ddf = st->sb;
1165 struct vd_config *vd = find_vdcr(ddf, st->container_member);
1166
1167 if (!vd)
1168 memset(uuid, 0, sizeof (uuid));
1169 else {
1170 char buf[20];
1171 struct sha1_ctx ctx;
1172 sha1_init_ctx(&ctx);
1173 sha1_process_bytes(&vd->guid, DDF_GUID_LEN, &ctx);
1174 if (vd->sec_elmnt_count > 1)
1175 sha1_process_bytes(&vd->sec_elmnt_seq, 1, &ctx);
1176 sha1_finish_ctx(&ctx, buf);
1177 memcpy(uuid, buf, sizeof(uuid));
1178 }
1179 }
1180
1181 static void getinfo_super_ddf(struct supertype *st, struct mdinfo *info)
1182 {
1183 struct ddf_super *ddf = st->sb;
1184 int i;
1185
1186 info->array.major_version = 1000;
1187 info->array.minor_version = 0; /* FIXME use ddf->revision somehow */
1188 info->array.patch_version = 0;
1189 info->array.raid_disks = __be16_to_cpu(ddf->phys->used_pdes);
1190 info->array.level = LEVEL_CONTAINER;
1191 info->array.layout = 0;
1192 info->array.md_minor = -1;
1193 info->array.ctime = DECADE + __be32_to_cpu(*(__u32*)
1194 (ddf->anchor.guid+16));
1195 info->array.utime = 0;
1196 info->array.chunk_size = 0;
1197
1198 // info->data_offset = ???;
1199 // info->component_size = ???;
1200
1201 info->disk.major = 0;
1202 info->disk.minor = 0;
1203 info->disk.number = __be32_to_cpu(ddf->dlist->disk.refnum);
1204 // info->disk.raid_disk = find refnum in the table and use index;
1205 info->disk.raid_disk = -1;
1206 for (i = 0; i < __be16_to_cpu(ddf->phys->max_pdes) ; i++)
1207 if (ddf->phys->entries[i].refnum == ddf->dlist->disk.refnum) {
1208 info->disk.raid_disk = i;
1209 break;
1210 }
1211 info->disk.state = (1 << MD_DISK_SYNC);
1212
1213 info->reshape_active = 0;
1214
1215 // uuid_from_super_ddf(info->uuid, sbv);
1216
1217 // info->name[] ?? ;
1218 }
1219
1220 static void getinfo_super_n_container(struct supertype *st, struct mdinfo *info)
1221 {
1222 /* just need offset and size */
1223 struct ddf_super *ddf = st->sb;
1224 int n = info->disk.number;
1225
1226 info->data_offset = __be64_to_cpu(ddf->phys->entries[n].config_size);
1227 info->component_size = 32*1024*1024 / 512;
1228 }
1229
1230 static int rlq_to_layout(int rlq, int prl, int raiddisks);
1231
1232 static void getinfo_super_ddf_bvd(struct supertype *st, struct mdinfo *info)
1233 {
1234 struct ddf_super *ddf = st->sb;
1235 struct vd_config *vd = find_vdcr(ddf, info->container_member);
1236
1237 /* FIXME this returns BVD info - what if we want SVD ?? */
1238
1239 info->array.major_version = 1000;
1240 info->array.minor_version = 0; /* FIXME use ddf->revision somehow */
1241 info->array.patch_version = 0;
1242 info->array.raid_disks = __be16_to_cpu(vd->prim_elmnt_count);
1243 info->array.level = map_num1(ddf_level_num, vd->prl);
1244 info->array.layout = rlq_to_layout(vd->rlq, vd->prl,
1245 info->array.raid_disks);
1246 info->array.md_minor = -1;
1247 info->array.ctime = DECADE + __be32_to_cpu(*(__u32*)(vd->guid+16));
1248 info->array.utime = DECADE + __be32_to_cpu(vd->timestamp);
1249 info->array.chunk_size = 512 << vd->chunk_shift;
1250
1251 // info->data_offset = ???;
1252 // info->component_size = ???;
1253
1254 info->disk.major = 0;
1255 info->disk.minor = 0;
1256 // info->disk.number = __be32_to_cpu(ddf->disk.refnum);
1257 // info->disk.raid_disk = find refnum in the table and use index;
1258 // info->disk.state = ???;
1259
1260 uuid_from_super_ddf(st, info->uuid);
1261
1262 // info->name[] ?? ;
1263 }
1264
1265 static void getinfo_super_n_bvd(struct supertype *st, struct mdinfo *info)
1266 {
1267 /* Find the particular details for info->disk.raid_disk.
1268 * This includes data_offset, component_size,
1269 */
1270 struct ddf_super *ddf = st->sb;
1271 __u64 *lba_offset = ddf->newconf->lba_offset;
1272 struct vd_config *conf = &ddf->newconf->conf;
1273 info->data_offset = __be64_to_cpu(lba_offset[info->disk.raid_disk]);
1274 info->component_size = __be64_to_cpu(conf->blocks);
1275 }
1276
1277 static int update_super_ddf(struct supertype *st, struct mdinfo *info,
1278 char *update,
1279 char *devname, int verbose,
1280 int uuid_set, char *homehost)
1281 {
1282 /* For 'assemble' and 'force' we need to return non-zero if any
1283 * change was made. For others, the return value is ignored.
1284 * Update options are:
1285 * force-one : This device looks a bit old but needs to be included,
1286 * update age info appropriately.
1287 * assemble: clear any 'faulty' flag to allow this device to
1288 * be assembled.
1289 * force-array: Array is degraded but being forced, mark it clean
1290 * if that will be needed to assemble it.
1291 *
1292 * newdev: not used ????
1293 * grow: Array has gained a new device - this is currently for
1294 * linear only
1295 * resync: mark as dirty so a resync will happen.
1296 * uuid: Change the uuid of the array to match watch is given
1297 * homehost: update the recorded homehost
1298 * name: update the name - preserving the homehost
1299 * _reshape_progress: record new reshape_progress position.
1300 *
1301 * Following are not relevant for this version:
1302 * sparc2.2 : update from old dodgey metadata
1303 * super-minor: change the preferred_minor number
1304 * summaries: update redundant counters.
1305 */
1306 int rv = 0;
1307 // struct ddf_super *ddf = st->sb;
1308 // struct vd_config *vd = find_vdcr(ddf, info->container_member);
1309 // struct virtual_entry *ve = find_ve(ddf);
1310
1311
1312 /* we don't need to handle "force-*" or "assemble" as
1313 * there is no need to 'trick' the kernel. We the metadata is
1314 * first updated to activate the array, all the implied modifications
1315 * will just happen.
1316 */
1317
1318 if (strcmp(update, "grow") == 0) {
1319 /* FIXME */
1320 }
1321 if (strcmp(update, "resync") == 0) {
1322 // info->resync_checkpoint = 0;
1323 }
1324 /* We ignore UUID updates as they make even less sense
1325 * with DDF
1326 */
1327 if (strcmp(update, "homehost") == 0) {
1328 /* homehost is stored in controller->vendor_data,
1329 * or it is when we are the vendor
1330 */
1331 // if (info->vendor_is_local)
1332 // strcpy(ddf->controller.vendor_data, homehost);
1333 }
1334 if (strcmp(update, "name") == 0) {
1335 /* name is stored in virtual_entry->name */
1336 // memset(ve->name, ' ', 16);
1337 // strncpy(ve->name, info->name, 16);
1338 }
1339 if (strcmp(update, "_reshape_progress") == 0) {
1340 /* We don't support reshape yet */
1341 }
1342
1343 // update_all_csum(ddf);
1344
1345 return rv;
1346 }
1347
1348 static void make_header_guid(char *guid)
1349 {
1350 __u32 stamp;
1351 int rfd;
1352 /* Create a DDF Header of Virtual Disk GUID */
1353
1354 /* 24 bytes of fiction required.
1355 * first 8 are a 'vendor-id' - "Linux-MD"
1356 * next 8 are controller type.. how about 0X DEAD BEEF 0000 0000
1357 * Remaining 8 random number plus timestamp
1358 */
1359 memcpy(guid, T10, sizeof(T10));
1360 stamp = __cpu_to_be32(0xdeadbeef);
1361 memcpy(guid+8, &stamp, 4);
1362 stamp = __cpu_to_be32(0);
1363 memcpy(guid+12, &stamp, 4);
1364 stamp = __cpu_to_be32(time(0) - DECADE);
1365 memcpy(guid+16, &stamp, 4);
1366 rfd = open("/dev/urandom", O_RDONLY);
1367 if (rfd < 0 || read(rfd, &stamp, 4) != 4)
1368 stamp = random();
1369 memcpy(guid+20, &stamp, 4);
1370 if (rfd >= 0) close(rfd);
1371 }
1372 static int init_super_ddf(struct supertype *st,
1373 mdu_array_info_t *info,
1374 unsigned long long size, char *name, char *homehost,
1375 int *uuid)
1376 {
1377 /* This is primarily called by Create when creating a new array.
1378 * We will then get add_to_super called for each component, and then
1379 * write_init_super called to write it out to each device.
1380 * For DDF, Create can create on fresh devices or on a pre-existing
1381 * array.
1382 * To create on a pre-existing array a different method will be called.
1383 * This one is just for fresh drives.
1384 *
1385 * We need to create the entire 'ddf' structure which includes:
1386 * DDF headers - these are easy.
1387 * Controller data - a Sector describing this controller .. not that
1388 * this is a controller exactly.
1389 * Physical Disk Record - one entry per device, so
1390 * leave plenty of space.
1391 * Virtual Disk Records - again, just leave plenty of space.
1392 * This just lists VDs, doesn't give details
1393 * Config records - describes the VDs that use this disk
1394 * DiskData - describes 'this' device.
1395 * BadBlockManagement - empty
1396 * Diag Space - empty
1397 * Vendor Logs - Could we put bitmaps here?
1398 *
1399 */
1400 struct ddf_super *ddf;
1401 char hostname[17];
1402 int hostlen;
1403 int max_phys_disks, max_virt_disks;
1404 unsigned long long sector;
1405 int clen;
1406 int i;
1407 int pdsize, vdsize;
1408 struct phys_disk *pd;
1409 struct virtual_disk *vd;
1410
1411 ddf = malloc(sizeof(*ddf));
1412 ddf->dlist = NULL; /* no physical disks yet */
1413 ddf->conflist = NULL; /* No virtual disks yet */
1414
1415 /* At least 32MB *must* be reserved for the ddf. So let's just
1416 * start 32MB from the end, and put the primary header there.
1417 * Don't do secondary for now.
1418 * We don't know exactly where that will be yet as it could be
1419 * different on each device. To just set up the lengths.
1420 *
1421 */
1422
1423 ddf->anchor.magic = DDF_HEADER_MAGIC;
1424 make_header_guid(ddf->anchor.guid);
1425
1426 memcpy(ddf->anchor.revision, DDF_REVISION, 8);
1427 ddf->anchor.seq = __cpu_to_be32(1);
1428 ddf->anchor.timestamp = __cpu_to_be32(time(0) - DECADE);
1429 ddf->anchor.openflag = 0xFF;
1430 ddf->anchor.foreignflag = 0;
1431 ddf->anchor.enforcegroups = 0; /* Is this best?? */
1432 ddf->anchor.pad0 = 0xff;
1433 memset(ddf->anchor.pad1, 0xff, 12);
1434 memset(ddf->anchor.header_ext, 0xff, 32);
1435 ddf->anchor.primary_lba = ~(__u64)0;
1436 ddf->anchor.secondary_lba = ~(__u64)0;
1437 ddf->anchor.type = DDF_HEADER_ANCHOR;
1438 memset(ddf->anchor.pad2, 0xff, 3);
1439 ddf->anchor.workspace_len = __cpu_to_be32(32768); /* Must be reserved */
1440 ddf->anchor.workspace_lba = ~(__u64)0; /* Put this at bottom
1441 of 32M reserved.. */
1442 max_phys_disks = 1023; /* Should be enough */
1443 ddf->anchor.max_pd_entries = __cpu_to_be16(max_phys_disks);
1444 max_virt_disks = 255;
1445 ddf->anchor.max_vd_entries = __cpu_to_be16(max_virt_disks); /* ?? */
1446 ddf->anchor.max_partitions = __cpu_to_be16(64); /* ?? */
1447 ddf->max_part = 64;
1448 ddf->anchor.config_record_len = __cpu_to_be16(1 + 256*12/512);
1449 ddf->anchor.max_primary_element_entries = __cpu_to_be16(256);
1450 memset(ddf->anchor.pad3, 0xff, 54);
1451
1452 /* controller sections is one sector long immediately
1453 * after the ddf header */
1454 sector = 1;
1455 ddf->anchor.controller_section_offset = __cpu_to_be32(sector);
1456 ddf->anchor.controller_section_length = __cpu_to_be32(1);
1457 sector += 1;
1458
1459 /* phys is 8 sectors after that */
1460 pdsize = ROUND_UP(sizeof(struct phys_disk) +
1461 sizeof(struct phys_disk_entry)*max_phys_disks,
1462 512);
1463 switch(pdsize/512) {
1464 case 2: case 8: case 32: case 128: case 512: break;
1465 default: abort();
1466 }
1467 ddf->anchor.phys_section_offset = __cpu_to_be32(sector);
1468 ddf->anchor.phys_section_length =
1469 __cpu_to_be32(pdsize/512); /* max_primary_element_entries/8 */
1470 sector += pdsize/512;
1471
1472 /* virt is another 32 sectors */
1473 vdsize = ROUND_UP(sizeof(struct virtual_disk) +
1474 sizeof(struct virtual_entry) * max_virt_disks,
1475 512);
1476 switch(vdsize/512) {
1477 case 2: case 8: case 32: case 128: case 512: break;
1478 default: abort();
1479 }
1480 ddf->anchor.virt_section_offset = __cpu_to_be32(sector);
1481 ddf->anchor.virt_section_length =
1482 __cpu_to_be32(vdsize/512); /* max_vd_entries/8 */
1483 sector += vdsize/512;
1484
1485 clen = (1 + 256*12/512) * (64+1);
1486 ddf->anchor.config_section_offset = __cpu_to_be32(sector);
1487 ddf->anchor.config_section_length = __cpu_to_be32(clen);
1488 sector += clen;
1489
1490 ddf->anchor.data_section_offset = __cpu_to_be32(sector);
1491 ddf->anchor.data_section_length = __cpu_to_be32(1);
1492 sector += 1;
1493
1494 ddf->anchor.bbm_section_length = __cpu_to_be32(0);
1495 ddf->anchor.bbm_section_offset = __cpu_to_be32(0xFFFFFFFF);
1496 ddf->anchor.diag_space_length = __cpu_to_be32(0);
1497 ddf->anchor.diag_space_offset = __cpu_to_be32(0xFFFFFFFF);
1498 ddf->anchor.vendor_length = __cpu_to_be32(0);
1499 ddf->anchor.vendor_offset = __cpu_to_be32(0xFFFFFFFF);
1500
1501 memset(ddf->anchor.pad4, 0xff, 256);
1502
1503 memcpy(&ddf->primary, &ddf->anchor, 512);
1504 memcpy(&ddf->secondary, &ddf->anchor, 512);
1505
1506 ddf->primary.openflag = 1; /* I guess.. */
1507 ddf->primary.type = DDF_HEADER_PRIMARY;
1508
1509 ddf->secondary.openflag = 1; /* I guess.. */
1510 ddf->secondary.type = DDF_HEADER_SECONDARY;
1511
1512 ddf->active = &ddf->primary;
1513
1514 ddf->controller.magic = DDF_CONTROLLER_MAGIC;
1515
1516 /* 24 more bytes of fiction required.
1517 * first 8 are a 'vendor-id' - "Linux-MD"
1518 * Remaining 16 are serial number.... maybe a hostname would do?
1519 */
1520 memcpy(ddf->controller.guid, T10, sizeof(T10));
1521 gethostname(hostname, 17);
1522 hostname[17] = 0;
1523 hostlen = strlen(hostname);
1524 memcpy(ddf->controller.guid + 24 - hostlen, hostname, hostlen);
1525 for (i = strlen(T10) ; i+hostlen < 24; i++)
1526 ddf->controller.guid[i] = ' ';
1527
1528 ddf->controller.type.vendor_id = __cpu_to_be16(0xDEAD);
1529 ddf->controller.type.device_id = __cpu_to_be16(0xBEEF);
1530 ddf->controller.type.sub_vendor_id = 0;
1531 ddf->controller.type.sub_device_id = 0;
1532 memcpy(ddf->controller.product_id, "What Is My PID??", 16);
1533 memset(ddf->controller.pad, 0xff, 8);
1534 memset(ddf->controller.vendor_data, 0xff, 448);
1535
1536 pd = ddf->phys = malloc(pdsize);
1537 ddf->pdsize = pdsize;
1538
1539 memset(pd, 0xff, pdsize);
1540 memset(pd, 0, sizeof(*pd));
1541 pd->magic = DDF_PHYS_DATA_MAGIC;
1542 pd->used_pdes = __cpu_to_be16(0);
1543 pd->max_pdes = __cpu_to_be16(max_phys_disks);
1544 memset(pd->pad, 0xff, 52);
1545
1546 vd = ddf->virt = malloc(vdsize);
1547 ddf->vdsize = vdsize;
1548 memset(vd, 0, vdsize);
1549 vd->magic = DDF_VIRT_RECORDS_MAGIC;
1550 vd->populated_vdes = __cpu_to_be16(0);
1551 vd->max_vdes = __cpu_to_be16(max_virt_disks);
1552 memset(vd->pad, 0xff, 52);
1553
1554 for (i=0; i<max_virt_disks; i++)
1555 memset(&vd->entries[i], 0xff, sizeof(struct virtual_entry));
1556
1557 st->sb = ddf;
1558 return 1;
1559 }
1560
1561 static int all_ff(char *guid)
1562 {
1563 int i;
1564 for (i = 0; i < DDF_GUID_LEN; i++)
1565 if (guid[i] != (char)0xff)
1566 return 0;
1567 return 1;
1568 }
1569 static int chunk_to_shift(int chunksize)
1570 {
1571 return ffs(chunksize/512)-1;
1572 }
1573
1574 static int level_to_prl(int level)
1575 {
1576 switch (level) {
1577 case LEVEL_LINEAR: return DDF_CONCAT;
1578 case 0: return DDF_RAID0;
1579 case 1: return DDF_RAID1;
1580 case 4: return DDF_RAID4;
1581 case 5: return DDF_RAID5;
1582 case 6: return DDF_RAID6;
1583 default: return -1;
1584 }
1585 }
1586 static int layout_to_rlq(int level, int layout, int raiddisks)
1587 {
1588 switch(level) {
1589 case 0:
1590 return DDF_RAID0_SIMPLE;
1591 case 1:
1592 switch(raiddisks) {
1593 case 2: return DDF_RAID1_SIMPLE;
1594 case 3: return DDF_RAID1_MULTI;
1595 default: return -1;
1596 }
1597 case 4:
1598 switch(layout) {
1599 case 0: return DDF_RAID4_N;
1600 }
1601 break;
1602 case 5:
1603 case 6:
1604 switch(layout) {
1605 case ALGORITHM_LEFT_ASYMMETRIC:
1606 return DDF_RAID5_N_RESTART;
1607 case ALGORITHM_RIGHT_ASYMMETRIC:
1608 return DDF_RAID5_0_RESTART;
1609 case ALGORITHM_LEFT_SYMMETRIC:
1610 return DDF_RAID5_N_CONTINUE;
1611 case ALGORITHM_RIGHT_SYMMETRIC:
1612 return -1; /* not mentioned in standard */
1613 }
1614 }
1615 return -1;
1616 }
1617
1618 static int rlq_to_layout(int rlq, int prl, int raiddisks)
1619 {
1620 switch(prl) {
1621 case DDF_RAID0:
1622 return 0; /* hopefully rlq == DDF_RAID0_SIMPLE */
1623 case DDF_RAID1:
1624 return 0; /* hopefully rlq == SIMPLE or MULTI depending
1625 on raiddisks*/
1626 case DDF_RAID4:
1627 switch(rlq) {
1628 case DDF_RAID4_N:
1629 return 0;
1630 default:
1631 /* not supported */
1632 return -1; /* FIXME this isn't checked */
1633 }
1634 case DDF_RAID5:
1635 case DDF_RAID6:
1636 switch(rlq) {
1637 case DDF_RAID5_N_RESTART:
1638 return ALGORITHM_LEFT_ASYMMETRIC;
1639 case DDF_RAID5_0_RESTART:
1640 return ALGORITHM_RIGHT_ASYMMETRIC;
1641 case DDF_RAID5_N_CONTINUE:
1642 return ALGORITHM_LEFT_SYMMETRIC;
1643 default:
1644 return -1;
1645 }
1646 }
1647 return -1;
1648 }
1649
1650 static int init_super_ddf_bvd(struct supertype *st,
1651 mdu_array_info_t *info,
1652 unsigned long long size,
1653 char *name, char *homehost,
1654 int *uuid)
1655 {
1656 /* We are creating a BVD inside a pre-existing container.
1657 * so st->sb is already set.
1658 * We need to create a new vd_config and a new virtual_entry
1659 */
1660 struct ddf_super *ddf = st->sb;
1661 int venum;
1662 struct virtual_entry *ve;
1663 struct vcl *vcl;
1664 struct vd_config *vc;
1665 int mppe;
1666 int conflen;
1667
1668 if (__be16_to_cpu(ddf->virt->populated_vdes)
1669 >= __be16_to_cpu(ddf->virt->max_vdes)) {
1670 fprintf(stderr, Name": This ddf already has the "
1671 "maximum of %d virtual devices\n",
1672 __be16_to_cpu(ddf->virt->max_vdes));
1673 return 0;
1674 }
1675
1676 for (venum = 0; venum < __be16_to_cpu(ddf->virt->max_vdes); venum++)
1677 if (all_ff(ddf->virt->entries[venum].guid))
1678 break;
1679 if (venum == __be16_to_cpu(ddf->virt->max_vdes)) {
1680 fprintf(stderr, Name ": Cannot find spare slot for "
1681 "virtual disk - DDF is corrupt\n");
1682 return 0;
1683 }
1684 ve = &ddf->virt->entries[venum];
1685 st->container_member = venum;
1686
1687 /* A Virtual Disk GUID contains the T10 Vendor ID, controller type,
1688 * timestamp, random number
1689 */
1690 make_header_guid(ve->guid);
1691 ve->unit = __cpu_to_be16(info->md_minor);
1692 ve->pad0 = 0xFFFF;
1693 ve->guid_crc = crc32(0, (unsigned char*)ddf->anchor.guid, DDF_GUID_LEN);
1694 ve->type = 0;
1695 ve->state = DDF_state_degraded; /* Will be modified as devices are added */
1696 if (info->state & 1) /* clean */
1697 ve->init_state = DDF_init_full;
1698 else
1699 ve->init_state = DDF_init_not;
1700
1701 memset(ve->pad1, 0xff, 14);
1702 memset(ve->name, ' ', 16);
1703 if (name)
1704 strncpy(ve->name, name, 16);
1705 ddf->virt->populated_vdes =
1706 __cpu_to_be16(__be16_to_cpu(ddf->virt->populated_vdes)+1);
1707
1708 /* Now create a new vd_config */
1709 conflen = __be16_to_cpu(ddf->active->config_record_len);
1710 vcl = malloc(offsetof(struct vcl, conf) + conflen * 512);
1711 mppe = __be16_to_cpu(ddf->anchor.max_primary_element_entries);
1712 vcl->lba_offset = (__u64*) &vcl->conf.phys_refnum[mppe];
1713
1714 vc = &vcl->conf;
1715
1716 vc->magic = DDF_VD_CONF_MAGIC;
1717 memcpy(vc->guid, ve->guid, DDF_GUID_LEN);
1718 vc->timestamp = __cpu_to_be32(time(0)-DECADE);
1719 vc->seqnum = __cpu_to_be32(1);
1720 memset(vc->pad0, 0xff, 24);
1721 vc->prim_elmnt_count = __cpu_to_be16(info->raid_disks);
1722 vc->chunk_shift = chunk_to_shift(info->chunk_size);
1723 vc->prl = level_to_prl(info->level);
1724 vc->rlq = layout_to_rlq(info->level, info->layout, info->raid_disks);
1725 vc->sec_elmnt_count = 1;
1726 vc->sec_elmnt_seq = 0;
1727 vc->srl = 0;
1728 vc->blocks = __cpu_to_be64(info->size * 2);
1729 vc->array_blocks = __cpu_to_be64(
1730 calc_array_size(info->level, info->raid_disks, info->layout,
1731 info->chunk_size, info->size*2));
1732 memset(vc->pad1, 0xff, 8);
1733 vc->spare_refs[0] = 0xffffffff;
1734 vc->spare_refs[1] = 0xffffffff;
1735 vc->spare_refs[2] = 0xffffffff;
1736 vc->spare_refs[3] = 0xffffffff;
1737 vc->spare_refs[4] = 0xffffffff;
1738 vc->spare_refs[5] = 0xffffffff;
1739 vc->spare_refs[6] = 0xffffffff;
1740 vc->spare_refs[7] = 0xffffffff;
1741 memset(vc->cache_pol, 0, 8);
1742 vc->bg_rate = 0x80;
1743 memset(vc->pad2, 0xff, 3);
1744 memset(vc->pad3, 0xff, 52);
1745 memset(vc->pad4, 0xff, 192);
1746 memset(vc->v0, 0xff, 32);
1747 memset(vc->v1, 0xff, 32);
1748 memset(vc->v2, 0xff, 16);
1749 memset(vc->v3, 0xff, 16);
1750 memset(vc->vendor, 0xff, 32);
1751
1752 memset(vc->phys_refnum, 0xff, 4*mppe);
1753 memset(vc->phys_refnum+mppe, 0x00, 8*mppe);
1754
1755 vcl->next = ddf->conflist;
1756 ddf->conflist = vcl;
1757 ddf->newconf = vcl;
1758 return 1;
1759 }
1760
1761 static void add_to_super_ddf_bvd(struct supertype *st,
1762 mdu_disk_info_t *dk, int fd, char *devname)
1763 {
1764 /* fd and devname identify a device with-in the ddf container (st).
1765 * dk identifies a location in the new BVD.
1766 * We need to find suitable free space in that device and update
1767 * the phys_refnum and lba_offset for the newly created vd_config.
1768 * We might also want to update the type in the phys_disk
1769 * section. FIXME
1770 */
1771 struct dl *dl;
1772 struct ddf_super *ddf = st->sb;
1773 struct vd_config *vc;
1774 __u64 *lba_offset;
1775 int mppe;
1776 int working;
1777
1778 for (dl = ddf->dlist; dl ; dl = dl->next)
1779 if (dl->major == dk->major &&
1780 dl->minor == dk->minor)
1781 break;
1782 if (!dl || ! (dk->state & (1<<MD_DISK_SYNC)))
1783 return;
1784
1785 vc = &ddf->newconf->conf;
1786 vc->phys_refnum[dk->raid_disk] = dl->disk.refnum;
1787 mppe = __be16_to_cpu(ddf->anchor.max_primary_element_entries);
1788 lba_offset = (__u64*)(vc->phys_refnum + mppe);
1789 lba_offset[dk->raid_disk] = 0; /* FIXME */
1790
1791 dl->vlist[0] = ddf->newconf; /* FIXME */
1792
1793 dl->fd = fd;
1794 dl->devname = devname;
1795
1796 /* Check how many working raid_disks, and if we can mark
1797 * array as optimal yet
1798 */
1799 working = 0;
1800 #if 0
1801 for (i=0; i < __be16_to_cpu(vc->prim_elmnt_count); i++)
1802 if (vc->phys_refnum[i] != 0xffffffff)
1803 working++;
1804 if (working == __be16_to_cpu(vc->prim_elmnt_count))
1805 ->entries[xx].state = (->entries[xx].state & ~DDF_state_mask)
1806 | DDF_state_optimal;
1807
1808 if (vc->prl == DDF_RAID6 &&
1809 working+1 == __be16_to_cpu(vc->prim_elmnt_count))
1810 ->entries[xx].state = (->entries[xx].state & ~DDF_state_mask)
1811 | DDF_state_part_optimal;
1812 #endif
1813 }
1814
1815 /* add a device to a container, either while creating it or while
1816 * expanding a pre-existing container
1817 */
1818 static void add_to_super_ddf(struct supertype *st,
1819 mdu_disk_info_t *dk, int fd, char *devname)
1820 {
1821 struct ddf_super *ddf = st->sb;
1822 struct dl *dd;
1823 time_t now;
1824 struct tm *tm;
1825 unsigned long long size;
1826 struct phys_disk_entry *pde;
1827 int n, i;
1828 struct stat stb;
1829
1830 /* This is device numbered dk->number. We need to create
1831 * a phys_disk entry and a more detailed disk_data entry.
1832 */
1833 fstat(fd, &stb);
1834 dd = malloc(sizeof(*dd) + sizeof(dd->vlist[0]) * (ddf->max_part+1));
1835 dd->major = major(stb.st_rdev);
1836 dd->minor = minor(stb.st_rdev);
1837 dd->devname = devname;
1838 dd->next = ddf->dlist;
1839 dd->fd = fd;
1840
1841 dd->disk.magic = DDF_PHYS_DATA_MAGIC;
1842 now = time(0);
1843 tm = localtime(&now);
1844 sprintf(dd->disk.guid, "%8s%04d%02d%02d",
1845 T10, tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
1846 *(__u32*)(dd->disk.guid + 16) = random();
1847 *(__u32*)(dd->disk.guid + 20) = random();
1848
1849 dd->disk.refnum = random(); /* and hope for the best FIXME check this is unique!!*/
1850 dd->disk.forced_ref = 1;
1851 dd->disk.forced_guid = 1;
1852 memset(dd->disk.vendor, ' ', 32);
1853 memcpy(dd->disk.vendor, "Linux", 5);
1854 memset(dd->disk.pad, 0xff, 442);
1855 for (i = 0; i < ddf->max_part+1 ; i++)
1856 dd->vlist[i] = NULL;
1857
1858 n = __be16_to_cpu(ddf->phys->used_pdes);
1859 pde = &ddf->phys->entries[n];
1860 n++;
1861 ddf->phys->used_pdes = __cpu_to_be16(n);
1862
1863 memcpy(pde->guid, dd->disk.guid, DDF_GUID_LEN);
1864 pde->refnum = dd->disk.refnum;
1865 pde->type = __cpu_to_be16(DDF_Forced_PD_GUID |DDF_Global_Spare);
1866 pde->state = __cpu_to_be16(DDF_Online);
1867 get_dev_size(fd, NULL, &size);
1868 /* We are required to reserve 32Meg, and record the size in sectors */
1869 pde->config_size = __cpu_to_be64( (size - 32*1024*1024) / 512);
1870 sprintf(pde->path, "%17.17s","Information: nil") ;
1871 memset(pde->pad, 0xff, 6);
1872
1873 ddf->dlist = dd;
1874 }
1875
1876 /*
1877 * This is the write_init_super method for a ddf container. It is
1878 * called when creating a container or adding another device to a
1879 * container.
1880 */
1881
1882 #ifndef MDASSEMBLE
1883 static int __write_init_super_ddf(struct supertype *st, int do_close)
1884 {
1885
1886 struct ddf_super *ddf = st->sb;
1887 int i;
1888 struct dl *d;
1889 int n_config;
1890 int conf_size;
1891
1892 unsigned long long size, sector;
1893
1894 for (d = ddf->dlist; d; d=d->next) {
1895 int fd = d->fd;
1896
1897 if (fd < 0)
1898 continue;
1899
1900 /* We need to fill in the primary, (secondary) and workspace
1901 * lba's in the headers, set their checksums,
1902 * Also checksum phys, virt....
1903 *
1904 * Then write everything out, finally the anchor is written.
1905 */
1906 get_dev_size(fd, NULL, &size);
1907 size /= 512;
1908 ddf->anchor.workspace_lba = __cpu_to_be64(size - 32*1024*2);
1909 ddf->anchor.primary_lba = __cpu_to_be64(size - 16*1024*2);
1910 ddf->anchor.seq = __cpu_to_be32(1);
1911 memcpy(&ddf->primary, &ddf->anchor, 512);
1912 memcpy(&ddf->secondary, &ddf->anchor, 512);
1913
1914 ddf->anchor.openflag = 0xFF; /* 'open' means nothing */
1915 ddf->anchor.seq = 0xFFFFFFFF; /* no sequencing in anchor */
1916 ddf->anchor.crc = calc_crc(&ddf->anchor, 512);
1917
1918 ddf->primary.openflag = 0;
1919 ddf->primary.type = DDF_HEADER_PRIMARY;
1920
1921 ddf->secondary.openflag = 0;
1922 ddf->secondary.type = DDF_HEADER_SECONDARY;
1923
1924 ddf->primary.crc = calc_crc(&ddf->primary, 512);
1925 ddf->secondary.crc = calc_crc(&ddf->secondary, 512);
1926
1927 sector = size - 16*1024*2;
1928 lseek64(fd, sector<<9, 0);
1929 write(fd, &ddf->primary, 512);
1930
1931 ddf->controller.crc = calc_crc(&ddf->controller, 512);
1932 write(fd, &ddf->controller, 512);
1933
1934 ddf->phys->crc = calc_crc(ddf->phys, ddf->pdsize);
1935
1936 write(fd, ddf->phys, ddf->pdsize);
1937
1938 ddf->virt->crc = calc_crc(ddf->virt, ddf->vdsize);
1939 write(fd, ddf->virt, ddf->vdsize);
1940
1941 /* Now write lots of config records. */
1942 n_config = __be16_to_cpu(ddf->active->max_partitions);
1943 conf_size = __be16_to_cpu(ddf->active->config_record_len) * 512;
1944 for (i = 0 ; i <= n_config ; i++) {
1945 struct vcl *c = d->vlist[i];
1946
1947 if (c) {
1948 c->conf.crc = calc_crc(&c->conf, conf_size);
1949 write(fd, &c->conf, conf_size);
1950 } else {
1951 __u32 sig = 0xffffffff;
1952 write(fd, &sig, 4);
1953 lseek64(fd, conf_size-4, SEEK_CUR);
1954 }
1955 }
1956 d->disk.crc = calc_crc(&d->disk, 512);
1957 write(fd, &d->disk, 512);
1958
1959 /* Maybe do the same for secondary */
1960
1961 lseek64(fd, (size-1)*512, SEEK_SET);
1962 write(fd, &ddf->anchor, 512);
1963 if (do_close) {
1964 close(fd);
1965 d->fd = -1;
1966 }
1967 }
1968 return 1;
1969 }
1970
1971 static int write_init_super_ddf(struct supertype *st)
1972 {
1973 return __write_init_super_ddf(st, 1);
1974 }
1975
1976 #endif
1977
1978 static __u64 avail_size_ddf(struct supertype *st, __u64 devsize)
1979 {
1980 /* We must reserve the last 32Meg */
1981 if (devsize <= 32*1024*2)
1982 return 0;
1983 return devsize - 32*1024*2;
1984 }
1985
1986 #ifndef MDASSEMBLE
1987 int validate_geometry_ddf(struct supertype *st,
1988 int level, int layout, int raiddisks,
1989 int chunk, unsigned long long size,
1990 char *dev, unsigned long long *freesize)
1991 {
1992 int fd;
1993 struct mdinfo *sra;
1994 int cfd;
1995
1996 /* ddf potentially supports lots of things, but it depends on
1997 * what devices are offered (and maybe kernel version?)
1998 * If given unused devices, we will make a container.
1999 * If given devices in a container, we will make a BVD.
2000 * If given BVDs, we make an SVD, changing all the GUIDs in the process.
2001 */
2002
2003 if (level == LEVEL_CONTAINER) {
2004 st->ss = &super_ddf_container;
2005 if (dev) {
2006 int rv =st->ss->validate_geometry(st, level, layout,
2007 raiddisks, chunk,
2008 size,
2009 NULL, freesize);
2010 if (rv)
2011 return rv;
2012 }
2013 return st->ss->validate_geometry(st, level, layout, raiddisks,
2014 chunk, size, dev, freesize);
2015 }
2016
2017 if (st->sb) {
2018 /* creating in a given container */
2019 st->ss = &super_ddf_bvd;
2020 if (dev) {
2021 int rv =st->ss->validate_geometry(st, level, layout,
2022 raiddisks, chunk,
2023 size,
2024 NULL, freesize);
2025 if (rv)
2026 return rv;
2027 }
2028 return st->ss->validate_geometry(st, level, layout, raiddisks,
2029 chunk, size, dev, freesize);
2030 }
2031 /* FIXME should exclude MULTIPATH, or more appropriately, allow
2032 * only known levels.
2033 */
2034 if (!dev)
2035 return 1;
2036
2037 /* This device needs to be either a device in a 'ddf' container,
2038 * or it needs to be a 'ddf-bvd' array.
2039 */
2040
2041 fd = open(dev, O_RDONLY|O_EXCL, 0);
2042 if (fd >= 0) {
2043 sra = sysfs_read(fd, 0, GET_VERSION);
2044 close(fd);
2045 if (sra && sra->array.major_version == -1 &&
2046 strcmp(sra->text_version, "ddf-bvd") == 0) {
2047 st->ss = &super_ddf_svd;
2048 return st->ss->validate_geometry(st, level, layout,
2049 raiddisks, chunk, size,
2050 dev, freesize);
2051 }
2052
2053 fprintf(stderr,
2054 Name ": Cannot create this array on device %s\n",
2055 dev);
2056 return 0;
2057 }
2058 if (errno != EBUSY || (fd = open(dev, O_RDONLY, 0)) < 0) {
2059 fprintf(stderr, Name ": Cannot open %s: %s\n",
2060 dev, strerror(errno));
2061 return 0;
2062 }
2063 /* Well, it is in use by someone, maybe a 'ddf' container. */
2064 cfd = open_container(fd);
2065 if (cfd < 0) {
2066 close(fd);
2067 fprintf(stderr, Name ": Cannot use %s: It is busy\n",
2068 dev);
2069 return 0;
2070 }
2071 sra = sysfs_read(cfd, 0, GET_VERSION);
2072 close(fd);
2073 if (sra && sra->array.major_version == -1 &&
2074 strcmp(sra->text_version, "ddf") == 0) {
2075 /* This is a member of a ddf container. Load the container
2076 * and try to create a bvd
2077 */
2078 struct ddf_super *ddf;
2079 st->ss = &super_ddf_bvd;
2080 if (load_super_ddf_all(st, cfd, (void **)&ddf, NULL, 1) == 0) {
2081 st->sb = ddf;
2082 st->container_dev = fd2devnum(cfd);
2083 st->container_member = 27; // FIXME
2084 close(cfd);
2085 return st->ss->validate_geometry(st, level, layout,
2086 raiddisks, chunk, size,
2087 dev, freesize);
2088 }
2089 close(cfd);
2090 }
2091 fprintf(stderr, Name ": Cannot use %s: Already in use\n",
2092 dev);
2093 return 1;
2094 }
2095
2096 int validate_geometry_ddf_container(struct supertype *st,
2097 int level, int layout, int raiddisks,
2098 int chunk, unsigned long long size,
2099 char *dev, unsigned long long *freesize)
2100 {
2101 int fd;
2102 unsigned long long ldsize;
2103
2104 if (level != LEVEL_CONTAINER)
2105 return 0;
2106 if (!dev)
2107 return 1;
2108
2109 fd = open(dev, O_RDONLY|O_EXCL, 0);
2110 if (fd < 0) {
2111 fprintf(stderr, Name ": Cannot open %s: %s\n",
2112 dev, strerror(errno));
2113 return 0;
2114 }
2115 if (!get_dev_size(fd, dev, &ldsize)) {
2116 close(fd);
2117 return 0;
2118 }
2119 close(fd);
2120
2121 *freesize = avail_size_ddf(st, ldsize);
2122
2123 return 1;
2124 }
2125
2126 struct extent {
2127 unsigned long long start, size;
2128 };
2129 int cmp_extent(const void *av, const void *bv)
2130 {
2131 const struct extent *a = av;
2132 const struct extent *b = bv;
2133 if (a->start < b->start)
2134 return -1;
2135 if (a->start > b->start)
2136 return 1;
2137 return 0;
2138 }
2139
2140 struct extent *get_extents(struct ddf_super *ddf, struct dl *dl)
2141 {
2142 /* find a list of used extents on the give physical device
2143 * (dnum) or the given ddf.
2144 * Return a malloced array of 'struct extent'
2145
2146 FIXME ignore DDF_Legacy devices?
2147
2148 */
2149 struct extent *rv;
2150 int n = 0;
2151 int dnum;
2152 int i, j;
2153
2154 for (dnum = 0; dnum < ddf->phys->used_pdes; dnum++)
2155 if (memcmp(dl->disk.guid,
2156 ddf->phys->entries[dnum].guid,
2157 DDF_GUID_LEN) == 0)
2158 break;
2159
2160 if (dnum == ddf->phys->used_pdes)
2161 return NULL;
2162
2163 rv = malloc(sizeof(struct extent) * (ddf->max_part + 2));
2164 if (!rv)
2165 return NULL;
2166
2167 for (i = 0; i < ddf->max_part+1; i++) {
2168 struct vcl *v = dl->vlist[i];
2169 if (v == NULL)
2170 continue;
2171 for (j=0; j < v->conf.prim_elmnt_count; j++)
2172 if (v->conf.phys_refnum[j] == dl->disk.refnum) {
2173 /* This device plays role 'j' in 'v'. */
2174 rv[n].start = __be64_to_cpu(v->lba_offset[j]);
2175 rv[n].size = __be64_to_cpu(v->conf.blocks);
2176 n++;
2177 break;
2178 }
2179 }
2180 qsort(rv, n, sizeof(*rv), cmp_extent);
2181
2182 rv[n].start = __be64_to_cpu(ddf->phys->entries[dnum].config_size);
2183 rv[n].size = 0;
2184 return rv;
2185 }
2186
2187 int validate_geometry_ddf_bvd(struct supertype *st,
2188 int level, int layout, int raiddisks,
2189 int chunk, unsigned long long size,
2190 char *dev, unsigned long long *freesize)
2191 {
2192 struct stat stb;
2193 struct ddf_super *ddf = st->sb;
2194 struct dl *dl;
2195 unsigned long long pos = 0;
2196 unsigned long long maxsize;
2197 struct extent *e;
2198 int i;
2199 /* ddf/bvd supports lots of things, but not containers */
2200 if (level == LEVEL_CONTAINER)
2201 return 0;
2202 /* We must have the container info already read in. */
2203 if (!ddf)
2204 return 0;
2205
2206 if (!dev) {
2207 /* General test: make sure there is space for
2208 * 'raiddisks' device extents of size 'size'.
2209 */
2210 unsigned long long minsize = size;
2211 int dcnt = 0;
2212 if (minsize == 0)
2213 minsize = 8;
2214 for (dl = ddf->dlist; dl ; dl = dl->next)
2215 {
2216 int found = 0;
2217
2218 i = 0;
2219 e = get_extents(ddf, dl);
2220 if (!e) continue;
2221 do {
2222 unsigned long long esize;
2223 esize = e[i].start - pos;
2224 if (esize >= minsize)
2225 found = 1;
2226 pos = e[i].start + e[i].size;
2227 i++;
2228 } while (e[i-1].size);
2229 if (found)
2230 dcnt++;
2231 free(e);
2232 }
2233 if (dcnt < raiddisks) {
2234 fprintf(stderr, Name ": Not enough devices with space "
2235 "for this array (%d < %d)\n",
2236 dcnt, raiddisks);
2237 return 0;
2238 }
2239 return 1;
2240 }
2241 /* This device must be a member of the set */
2242 if (stat(dev, &stb) < 0)
2243 return 0;
2244 if ((S_IFMT & stb.st_mode) != S_IFBLK)
2245 return 0;
2246 for (dl = ddf->dlist ; dl ; dl = dl->next) {
2247 if (dl->major == major(stb.st_rdev) &&
2248 dl->minor == minor(stb.st_rdev))
2249 break;
2250 }
2251 if (!dl) {
2252 fprintf(stderr, Name ": %s is not in the same DDF set\n",
2253 dev);
2254 return 0;
2255 }
2256 e = get_extents(ddf, dl);
2257 maxsize = 0;
2258 i = 0;
2259 if (e) do {
2260 unsigned long long esize;
2261 esize = e[i].start - pos;
2262 if (esize >= maxsize)
2263 maxsize = esize;
2264 pos = e[i].start + e[i].size;
2265 i++;
2266 } while (e[i-1].size);
2267 *freesize = maxsize;
2268 // FIXME here I am
2269
2270 return 1;
2271 }
2272 int validate_geometry_ddf_svd(struct supertype *st,
2273 int level, int layout, int raiddisks,
2274 int chunk, unsigned long long size,
2275 char *dev, unsigned long long *freesize)
2276 {
2277 /* dd/svd only supports striped, mirrored, concat, spanned... */
2278 if (level != LEVEL_LINEAR &&
2279 level != 0 &&
2280 level != 1)
2281 return 0;
2282 return 1;
2283 }
2284
2285
2286 static int load_super_ddf_all(struct supertype *st, int fd,
2287 void **sbp, char *devname, int keep_fd)
2288 {
2289 struct mdinfo *sra;
2290 struct ddf_super *super;
2291 struct mdinfo *sd, *best = NULL;
2292 int bestseq = 0;
2293 int seq;
2294 char nm[20];
2295 int dfd;
2296
2297 sra = sysfs_read(fd, 0, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE);
2298 if (!sra)
2299 return 1;
2300 if (sra->array.major_version != -1 ||
2301 sra->array.minor_version != -2 ||
2302 strcmp(sra->text_version, "ddf") != 0)
2303 return 1;
2304
2305 super = malloc(sizeof(*super));
2306 if (!super)
2307 return 1;
2308 memset(super, 0, sizeof(*super));
2309
2310 /* first, try each device, and choose the best ddf */
2311 for (sd = sra->devs ; sd ; sd = sd->next) {
2312 int rv;
2313 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
2314 dfd = dev_open(nm, O_RDONLY);
2315 if (dfd < 0)
2316 return 2;
2317 rv = load_ddf_headers(dfd, super, NULL);
2318 close(dfd);
2319 if (rv == 0) {
2320 seq = __be32_to_cpu(super->active->seq);
2321 if (super->active->openflag)
2322 seq--;
2323 if (!best || seq > bestseq) {
2324 bestseq = seq;
2325 best = sd;
2326 }
2327 }
2328 }
2329 if (!best)
2330 return 1;
2331 /* OK, load this ddf */
2332 sprintf(nm, "%d:%d", best->disk.major, best->disk.minor);
2333 dfd = dev_open(nm, O_RDONLY);
2334 if (dfd < 0)
2335 return 1;
2336 load_ddf_headers(dfd, super, NULL);
2337 load_ddf_global(dfd, super, NULL);
2338 close(dfd);
2339 /* Now we need the device-local bits */
2340 for (sd = sra->devs ; sd ; sd = sd->next) {
2341 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
2342 dfd = dev_open(nm, keep_fd? O_RDWR : O_RDONLY);
2343 if (dfd < 0)
2344 return 2;
2345 seq = load_ddf_local(dfd, super, NULL, keep_fd);
2346 if (!keep_fd) close(dfd);
2347 }
2348 *sbp = super;
2349 if (st->ss == NULL) {
2350 st->ss = &super_ddf_container;
2351 st->minor_version = 0;
2352 st->max_devs = 512;
2353 st->container_dev = fd2devnum(fd);
2354 }
2355 return 0;
2356 }
2357 #endif
2358
2359
2360
2361 static struct mdinfo *container_content_ddf(struct supertype *st)
2362 {
2363 /* Given a container loaded by load_super_ddf_all,
2364 * extract information about all the arrays into
2365 * an mdinfo tree.
2366 *
2367 * For each vcl in conflist: create an mdinfo, fill it in,
2368 * then look for matching devices (phys_refnum) in dlist
2369 * and create appropriate device mdinfo.
2370 */
2371 struct ddf_super *ddf = st->sb;
2372 struct mdinfo *rest = NULL;
2373 struct vcl *vc;
2374
2375 for (vc = ddf->conflist ; vc ; vc=vc->next)
2376 {
2377 int mppe;
2378 int i;
2379 struct mdinfo *this;
2380 this = malloc(sizeof(*this));
2381 memset(this, 0, sizeof(*this));
2382 this->next = rest;
2383 rest = this;
2384
2385 this->array.major_version = 1000;
2386 this->array.minor_version = 0;
2387 this->array.patch_version = 0;
2388 this->array.level = map_num1(ddf_level_num, vc->conf.prl);
2389 this->array.raid_disks =
2390 __be16_to_cpu(vc->conf.prim_elmnt_count);
2391 /* FIXME this should be mapped */
2392 this->array.layout = vc->conf.rlq;
2393 this->array.md_minor = -1;
2394 this->array.ctime = DECADE +
2395 __be32_to_cpu(*(__u32*)(vc->conf.guid+16));
2396 this->array.utime = DECADE +
2397 __be32_to_cpu(vc->conf.timestamp);
2398 this->array.chunk_size = 512 << vc->conf.chunk_shift;
2399
2400 for (i=0; i < __be16_to_cpu(ddf->virt->populated_vdes); i++)
2401 if (memcmp(ddf->virt->entries[i].guid,
2402 vc->conf.guid, DDF_GUID_LEN) == 0)
2403 break;
2404 if ((ddf->virt->entries[i].state & DDF_state_inconsistent) ||
2405 (ddf->virt->entries[i].init_state & DDF_initstate_mask) !=
2406 DDF_init_full) {
2407 this->array.state = 0;
2408 this->resync_start = 0;
2409 } else {
2410 this->array.state = 1;
2411 this->resync_start = ~0ULL;
2412 }
2413 memcpy(this->name, ddf->virt->entries[i].name, 32);
2414 this->name[33]=0;
2415
2416 memset(this->uuid, 0, sizeof(this->uuid));
2417 this->component_size = __be64_to_cpu(vc->conf.blocks);
2418 this->array.size = this->component_size / 2;
2419 this->container_member = i;
2420
2421 mppe = __be16_to_cpu(ddf->anchor.max_primary_element_entries);
2422 for (i=0 ; i < mppe ; i++) {
2423 struct mdinfo *dev;
2424 struct dl *d;
2425
2426 if (vc->conf.phys_refnum[i] == 0xFFFFFFFF)
2427 continue;
2428
2429 this->array.working_disks++;
2430
2431 for (d = ddf->dlist; d ; d=d->next)
2432 if (d->disk.refnum == vc->conf.phys_refnum[i])
2433 break;
2434 if (d == NULL)
2435 break;
2436
2437 dev = malloc(sizeof(*dev));
2438 memset(dev, 0, sizeof(*dev));
2439 dev->next = this->devs;
2440 this->devs = dev;
2441
2442 dev->disk.number = __be32_to_cpu(d->disk.refnum);
2443 dev->disk.major = d->major;
2444 dev->disk.minor = d->minor;
2445 dev->disk.raid_disk = i;
2446 dev->disk.state = (1<<MD_DISK_SYNC)|(1<<MD_DISK_ACTIVE);
2447
2448 dev->events = __le32_to_cpu(ddf->primary.seq);
2449 dev->data_offset = vc->lba_offset[i];
2450 dev->component_size = __be64_to_cpu(vc->conf.blocks);
2451 if (d->devname)
2452 strcpy(dev->name, d->devname);
2453 }
2454 }
2455 return rest;
2456 }
2457
2458 static int init_zero_ddf(struct supertype *st,
2459 mdu_array_info_t *info,
2460 unsigned long long size, char *name,
2461 char *homehost, int *uuid)
2462 {
2463 st->sb = NULL;
2464 return 0;
2465 }
2466
2467 static int store_zero_ddf(struct supertype *st, int fd)
2468 {
2469 unsigned long long dsize;
2470 char buf[512];
2471 memset(buf, 0, 512);
2472
2473
2474 if (!get_dev_size(fd, NULL, &dsize))
2475 return 1;
2476
2477 lseek64(fd, dsize-512, 0);
2478 write(fd, buf, 512);
2479 return 0;
2480 }
2481
2482 static int compare_super_ddf(struct supertype *st, struct supertype *tst)
2483 {
2484 /*
2485 * return:
2486 * 0 same, or first was empty, and second was copied
2487 * 1 second had wrong number
2488 * 2 wrong uuid
2489 * 3 wrong other info
2490 */
2491 struct ddf_super *first = st->sb;
2492 struct ddf_super *second = tst->sb;
2493
2494 if (!first) {
2495 st->sb = tst->sb;
2496 tst->sb = NULL;
2497 return 0;
2498 }
2499
2500 if (memcmp(first->anchor.guid, second->anchor.guid, DDF_GUID_LEN) != 0)
2501 return 2;
2502
2503 /* FIXME should I look at anything else? */
2504 return 0;
2505 }
2506
2507 /*
2508 * A new array 'a' has been started which claims to be instance 'inst'
2509 * within container 'c'.
2510 * We need to confirm that the array matches the metadata in 'c' so
2511 * that we don't corrupt any metadata.
2512 */
2513 static int ddf_open_new(struct supertype *c, struct active_array *a, int inst)
2514 {
2515 fprintf(stderr, "ddf: open_new %d\n", inst);
2516 return 0;
2517 }
2518
2519 /*
2520 * The array 'a' is to be marked clean in the metadata.
2521 * If '->resync_start' is not ~(unsigned long long)0, then the array is only
2522 * clean up to the point (in sectors). If that cannot be recorded in the
2523 * metadata, then leave it as dirty.
2524 *
2525 * For DDF, we need to clear the DDF_state_inconsistent bit in the
2526 * !global! virtual_disk.virtual_entry structure.
2527 */
2528 static void ddf_set_array_state(struct active_array *a, int consistent)
2529 {
2530 struct ddf_super *ddf = a->container->sb;
2531 int inst = a->info.container_member;
2532 if (consistent)
2533 ddf->virt->entries[inst].state &= ~DDF_state_inconsistent;
2534 else
2535 ddf->virt->entries[inst].state |= DDF_state_inconsistent;
2536 ddf->virt->entries[inst].init_state &= ~DDF_initstate_mask;
2537 if (a->resync_start == ~0ULL)
2538 ddf->virt->entries[inst].init_state |= DDF_init_full;
2539 else if (a->resync_start == 0)
2540 ddf->virt->entries[inst].init_state |= DDF_init_not;
2541 else
2542 ddf->virt->entries[inst].init_state |= DDF_init_quick;
2543
2544 printf("ddf mark %s %llu\n", consistent?"clean":"dirty",
2545 a->resync_start);
2546 }
2547
2548 /*
2549 * The state of each disk is stored in the global phys_disk structure
2550 * in phys_disk.entries[n].state.
2551 * This makes various combinations awkward.
2552 * - When a device fails in any array, it must be failed in all arrays
2553 * that include a part of this device.
2554 * - When a component is rebuilding, we cannot include it officially in the
2555 * array unless this is the only array that uses the device.
2556 *
2557 * So: when transitioning:
2558 * Online -> failed, just set failed flag. monitor will propagate
2559 * spare -> online, the device might need to be added to the array.
2560 * spare -> failed, just set failed. Don't worry if in array or not.
2561 */
2562 static void ddf_set_disk(struct active_array *a, int n, int state)
2563 {
2564 struct ddf_super *ddf = a->container->sb;
2565 int inst = a->info.container_member;
2566 struct vd_config *vc = find_vdcr(ddf, inst);
2567 int pd = find_phys(ddf, vc->phys_refnum[n]);
2568 int i, st, working;
2569
2570 if (vc == NULL) {
2571 fprintf(stderr, "ddf: cannot find instance %d!!\n", inst);
2572 return;
2573 }
2574 if (pd < 0) {
2575 /* disk doesn't currently exist. If it is now in_sync,
2576 * insert it. */
2577 if ((state & DS_INSYNC) && ! (state & DS_FAULTY)) {
2578 /* Find dev 'n' in a->info->devs, determine the
2579 * ddf refnum, and set vc->phys_refnum and update
2580 * phys->entries[]
2581 */
2582 /* FIXME */
2583 }
2584 } else {
2585 if (state & DS_FAULTY)
2586 ddf->phys->entries[pd].state |= __cpu_to_be16(DDF_Failed);
2587 if (state & DS_INSYNC) {
2588 ddf->phys->entries[pd].state |= __cpu_to_be16(DDF_Online);
2589 ddf->phys->entries[pd].state &= __cpu_to_be16(~DDF_Rebuilding);
2590 }
2591 }
2592
2593 /* Now we need to check the state of the array and update
2594 * virtual_disk.entries[n].state.
2595 * It needs to be one of "optimal", "degraded", "failed".
2596 * I don't understand 'deleted' or 'missing'.
2597 */
2598 working = 0;
2599 for (i=0; i < a->info.array.raid_disks; i++) {
2600 pd = find_phys(ddf, vc->phys_refnum[i]);
2601 if (pd < 0)
2602 continue;
2603 st = ddf->phys->entries[pd].state;
2604 if ((state & (DDF_Online|DDF_Failed|DDF_Rebuilding))
2605 == DDF_Online)
2606 working++;
2607 }
2608 state = DDF_state_degraded;
2609 if (working == a->info.array.raid_disks)
2610 state = DDF_state_optimal;
2611 else switch(vc->prl) {
2612 case DDF_RAID0:
2613 case DDF_CONCAT:
2614 case DDF_JBOD:
2615 state = DDF_state_failed;
2616 break;
2617 case DDF_RAID1:
2618 if (working == 0)
2619 state = DDF_state_failed;
2620 break;
2621 case DDF_RAID4:
2622 case DDF_RAID5:
2623 if (working < a->info.array.raid_disks-1)
2624 state = DDF_state_failed;
2625 break;
2626 case DDF_RAID6:
2627 if (working < a->info.array.raid_disks-2)
2628 state = DDF_state_failed;
2629 else if (working == a->info.array.raid_disks-1)
2630 state = DDF_state_part_optimal;
2631 break;
2632 }
2633
2634 ddf->virt->entries[inst].state =
2635 (ddf->virt->entries[inst].state & ~DDF_state_mask)
2636 | state;
2637
2638 fprintf(stderr, "ddf: set_disk %d\n", n);
2639 }
2640
2641 static void ddf_sync_metadata(struct active_array *a)
2642 {
2643
2644 /*
2645 * Write all data to all devices.
2646 * Later, we might be able to track whether only local changes
2647 * have been made, or whether any global data has been changed,
2648 * but ddf is sufficiently weird that it probably always
2649 * changes global data ....
2650 */
2651 __write_init_super_ddf(a->container, 0);
2652 fprintf(stderr, "ddf: sync_metadata\n");
2653 }
2654
2655 struct superswitch super_ddf = {
2656 #ifndef MDASSEMBLE
2657 .examine_super = examine_super_ddf,
2658 .brief_examine_super = brief_examine_super_ddf,
2659 .detail_super = detail_super_ddf,
2660 .brief_detail_super = brief_detail_super_ddf,
2661 .validate_geometry = validate_geometry_ddf,
2662 #endif
2663 .match_home = match_home_ddf,
2664 .uuid_from_super= uuid_from_super_ddf,
2665 .getinfo_super = getinfo_super_ddf,
2666 .update_super = update_super_ddf,
2667
2668 .avail_size = avail_size_ddf,
2669
2670 .compare_super = compare_super_ddf,
2671
2672 .load_super = load_super_ddf,
2673 .init_super = init_zero_ddf,
2674 .store_super = store_zero_ddf,
2675 .free_super = free_super_ddf,
2676 .match_metadata_desc = match_metadata_desc_ddf,
2677 .getinfo_super_n = getinfo_super_n_container,
2678
2679
2680 .major = 1000,
2681 .swapuuid = 0,
2682 .external = 1,
2683 .text_version = "ddf",
2684
2685 /* for mdmon */
2686 .open_new = ddf_open_new,
2687 .set_array_state= ddf_set_array_state,
2688 .set_disk = ddf_set_disk,
2689 .sync_metadata = ddf_sync_metadata,
2690
2691
2692 };
2693
2694 /* Super_ddf_container is set by validate_geometry_ddf when given a
2695 * device that is not part of any array
2696 */
2697 struct superswitch super_ddf_container = {
2698 #ifndef MDASSEMBLE
2699 .validate_geometry = validate_geometry_ddf_container,
2700 .write_init_super = write_init_super_ddf,
2701 #endif
2702
2703 .load_super = load_super_ddf,
2704 .init_super = init_super_ddf,
2705 .add_to_super = add_to_super_ddf,
2706 .getinfo_super = getinfo_super_ddf,
2707
2708 .free_super = free_super_ddf,
2709
2710 .container_content = container_content_ddf,
2711 .getinfo_super_n = getinfo_super_n_container,
2712
2713 .major = 1000,
2714 .swapuuid = 0,
2715 .external = 1,
2716 .text_version = "ddf",
2717 };
2718
2719 struct superswitch super_ddf_bvd = {
2720 #ifndef MDASSEMBLE
2721 // .detail_super = detail_super_ddf_bvd,
2722 // .brief_detail_super = brief_detail_super_ddf_bvd,
2723 .validate_geometry = validate_geometry_ddf_bvd,
2724 .write_init_super = write_init_super_ddf,
2725 #endif
2726 .update_super = update_super_ddf,
2727 .init_super = init_super_ddf_bvd,
2728 .add_to_super = add_to_super_ddf_bvd,
2729 .getinfo_super = getinfo_super_ddf_bvd,
2730 .getinfo_super_n = getinfo_super_n_bvd,
2731
2732 .load_super = load_super_ddf,
2733 .free_super = free_super_ddf,
2734 .match_metadata_desc = match_metadata_desc_ddf_bvd,
2735
2736
2737 .major = 1001,
2738 .swapuuid = 0,
2739 .external = 2,
2740 .text_version = "ddf",
2741 };
2742
2743 struct superswitch super_ddf_svd = {
2744 #ifndef MDASSEMBLE
2745 // .detail_super = detail_super_ddf_svd,
2746 // .brief_detail_super = brief_detail_super_ddf_svd,
2747 .validate_geometry = validate_geometry_ddf_svd,
2748 #endif
2749 .update_super = update_super_ddf,
2750 .init_super = init_super_ddf,
2751
2752 .load_super = load_super_ddf,
2753 .free_super = free_super_ddf,
2754 .match_metadata_desc = match_metadata_desc_ddf_svd,
2755
2756 .major = 1002,
2757 .swapuuid = 0,
2758 .external = 2,
2759 .text_version = "ddf",
2760 };