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