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