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