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