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