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