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