2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2006-2007 Neil Brown <neilb@suse.de>
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.
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.
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
22 * Email: <neil@brown.name>
24 * Specifications for DDF takes from Common RAID DDF Specification Revision 1.2
25 * (July 28 2006). Reused by permission of SNIA.
28 #define HAVE_STDINT_H 1
33 static inline int ROUND_UP(int a
, int base
)
35 return ((a
+base
-1)/base
)*base
;
38 /* a non-official T10 name for creation GUIDs */
39 static char T10
[] = "Linux-MD";
41 /* DDF timestamps are 1980 based, so we need to add
42 * second-in-decade-of-seventies to convert to linux timestamps.
43 * 10 years with 2 leap years.
45 #define DECADE (3600*24*(365*10+2))
48 const unsigned char *buf
,
51 /* The DDF metadata handling.
52 * DDF metadata lives at the end of the device.
53 * The last 512 byte block provides an 'anchor' which is used to locate
54 * the rest of the metadata which usually lives immediately behind the anchor.
57 * - all multibyte numeric fields are bigendian.
58 * - all strings are space padded.
62 /* Primary Raid Level (PRL) */
63 #define DDF_RAID0 0x00
64 #define DDF_RAID1 0x01
65 #define DDF_RAID3 0x03
66 #define DDF_RAID4 0x04
67 #define DDF_RAID5 0x05
68 #define DDF_RAID1E 0x11
70 #define DDF_CONCAT 0x1f
71 #define DDF_RAID5E 0x15
72 #define DDF_RAID5EE 0x25
73 #define DDF_RAID6 0x16 /* Vendor unique layout */
75 /* Raid Level Qualifier (RLQ) */
76 #define DDF_RAID0_SIMPLE 0x00
77 #define DDF_RAID1_SIMPLE 0x00 /* just 2 devices in this plex */
78 #define DDF_RAID1_MULTI 0x01 /* exactly 3 devices in this plex */
79 #define DDF_RAID3_0 0x00 /* parity in first extent */
80 #define DDF_RAID3_N 0x01 /* parity in last extent */
81 #define DDF_RAID4_0 0x00 /* parity in first extent */
82 #define DDF_RAID4_N 0x01 /* parity in last extent */
83 /* these apply to raid5e and raid5ee as well */
84 #define DDF_RAID5_0_RESTART 0x00 /* same as 'right asymmetric' - layout 1 */
85 #define DDF_RAID5_N_RESTART 0x02 /* same as 'left asymmetric' - layout 0 */
86 #define DDF_RAID5_N_CONTINUE 0x03 /* same as 'left symmetric' - layout 2 */
88 #define DDF_RAID1E_ADJACENT 0x00 /* raid10 nearcopies==2 */
89 #define DDF_RAID1E_OFFSET 0x01 /* raid10 offsetcopies==2 */
91 /* Secondary RAID Level (SRL) */
92 #define DDF_2STRIPED 0x00 /* This is weirder than RAID0 !! */
93 #define DDF_2MIRRORED 0x01
94 #define DDF_2CONCAT 0x02
95 #define DDF_2SPANNED 0x03 /* This is also weird - be careful */
98 #define DDF_HEADER_MAGIC __cpu_to_be32(0xDE11DE11)
99 #define DDF_CONTROLLER_MAGIC __cpu_to_be32(0xAD111111)
100 #define DDF_PHYS_RECORDS_MAGIC __cpu_to_be32(0x22222222)
101 #define DDF_PHYS_DATA_MAGIC __cpu_to_be32(0x33333333)
102 #define DDF_VIRT_RECORDS_MAGIC __cpu_to_be32(0xDDDDDDDD)
103 #define DDF_VD_CONF_MAGIC __cpu_to_be32(0xEEEEEEEE)
104 #define DDF_SPARE_ASSIGN_MAGIC __cpu_to_be32(0x55555555)
105 #define DDF_VU_CONF_MAGIC __cpu_to_be32(0x88888888)
106 #define DDF_VENDOR_LOG_MAGIC __cpu_to_be32(0x01dBEEF0)
107 #define DDF_BBM_LOG_MAGIC __cpu_to_be32(0xABADB10C)
109 #define DDF_GUID_LEN 24
110 #define DDF_REVISION "01.00.00"
115 char guid
[DDF_GUID_LEN
];
116 char revision
[8]; /* 01.00.00 */
117 __u32 seq
; /* starts at '1' */
122 __u8 pad0
; /* 0xff */
123 __u8 pad1
[12]; /* 12 * 0xff */
124 /* 64 bytes so far */
125 __u8 header_ext
[32]; /* reserved: fill with 0xff */
129 __u8 pad2
[3]; /* 0xff */
130 __u32 workspace_len
; /* sectors for vendor space -
131 * at least 32768(sectors) */
133 __u16 max_pd_entries
; /* one of 15, 63, 255, 1023, 4095 */
134 __u16 max_vd_entries
; /* 2^(4,6,8,10,12)-1 : i.e. as above */
135 __u16 max_partitions
; /* i.e. max num of configuration
136 record entries per disk */
137 __u16 config_record_len
; /* 1 +ROUNDUP(max_primary_element_entries
139 __u16 max_primary_element_entries
; /* 16, 64, 256, 1024, or 4096 */
140 __u8 pad3
[54]; /* 0xff */
141 /* 192 bytes so far */
142 __u32 controller_section_offset
;
143 __u32 controller_section_length
;
144 __u32 phys_section_offset
;
145 __u32 phys_section_length
;
146 __u32 virt_section_offset
;
147 __u32 virt_section_length
;
148 __u32 config_section_offset
;
149 __u32 config_section_length
;
150 __u32 data_section_offset
;
151 __u32 data_section_length
;
152 __u32 bbm_section_offset
;
153 __u32 bbm_section_length
;
154 __u32 diag_space_offset
;
155 __u32 diag_space_length
;
158 /* 256 bytes so far */
159 __u8 pad4
[256]; /* 0xff */
163 #define DDF_HEADER_ANCHOR 0x00
164 #define DDF_HEADER_PRIMARY 0x01
165 #define DDF_HEADER_SECONDARY 0x02
167 /* The content of the 'controller section' - global scope */
168 struct ddf_controller_data
{
171 char guid
[DDF_GUID_LEN
];
172 struct controller_type
{
179 __u8 pad
[8]; /* 0xff */
180 __u8 vendor_data
[448];
183 /* The content of phys_section - global scope */
190 struct phys_disk_entry
{
191 char guid
[DDF_GUID_LEN
];
195 __u64 config_size
; /* DDF structures must be after here */
196 char path
[18]; /* another horrible structure really */
201 /* phys_disk_entry.type is a bitmap - bigendian remember */
202 #define DDF_Forced_PD_GUID 1
203 #define DDF_Active_in_VD 2
204 #define DDF_Global_Spare 4
205 #define DDF_Spare 8 /* overrides Global_spare */
206 #define DDF_Foreign 16
207 #define DDF_Legacy 32 /* no DDF on this device */
209 #define DDF_Interface_mask 0xf00
210 #define DDF_Interface_SCSI 0x100
211 #define DDF_Interface_SAS 0x200
212 #define DDF_Interface_SATA 0x300
213 #define DDF_Interface_FC 0x400
215 /* phys_disk_entry.state is a bigendian bitmap */
217 #define DDF_Failed 2 /* overrides 1,4,8 */
218 #define DDF_Rebuilding 4
219 #define DDF_Transition 8
221 #define DDF_ReadErrors 32
222 #define DDF_Missing 64
224 /* The content of the virt_section global scope */
225 struct virtual_disk
{
228 __u16 populated_vdes
;
231 struct virtual_entry
{
232 char guid
[DDF_GUID_LEN
];
234 __u16 pad0
; /* 0xffff */
244 /* virtual_entry.type is a bitmap - bigendian */
246 #define DDF_Enforce_Groups 2
247 #define DDF_Unicode 4
248 #define DDF_Owner_Valid 8
250 /* virtual_entry.state is a bigendian bitmap */
251 #define DDF_state_mask 0x7
252 #define DDF_state_optimal 0x0
253 #define DDF_state_degraded 0x1
254 #define DDF_state_deleted 0x2
255 #define DDF_state_missing 0x3
256 #define DDF_state_failed 0x4
258 #define DDF_state_morphing 0x8
259 #define DDF_state_inconsistent 0x10
261 /* virtual_entry.init_state is a bigendian bitmap */
262 #define DDF_initstate_mask 0x03
263 #define DDF_init_not 0x00
264 #define DDF_init_quick 0x01
265 #define DDF_init_full 0x02
267 #define DDF_access_mask 0xc0
268 #define DDF_access_rw 0x00
269 #define DDF_access_ro 0x80
270 #define DDF_access_blocked 0xc0
272 /* The content of the config_section - local scope
273 * It has multiple records each config_record_len sectors
274 * They can be vd_config or spare_assign
280 char guid
[DDF_GUID_LEN
];
284 __u16 prim_elmnt_count
;
285 __u8 chunk_shift
; /* 0 == 512, 1==1024 etc */
288 __u8 sec_elmnt_count
;
300 __u8 v0
[32]; /* reserved- 0xff */
301 __u8 v1
[32]; /* reserved- 0xff */
302 __u8 v2
[16]; /* reserved- 0xff */
303 __u8 v3
[16]; /* reserved- 0xff */
305 __u32 phys_refnum
[0]; /* refnum of each disk in sequence */
306 /*__u64 lba_offset[0]; LBA offset in each phys. Note extents in a
307 bvd are always the same size */
310 /* vd_config.cache_pol[7] is a bitmap */
311 #define DDF_cache_writeback 1 /* else writethrough */
312 #define DDF_cache_wadaptive 2 /* only applies if writeback */
313 #define DDF_cache_readahead 4
314 #define DDF_cache_radaptive 8 /* only if doing read-ahead */
315 #define DDF_cache_ifnobatt 16 /* even to write cache if battery is poor */
316 #define DDF_cache_wallowed 32 /* enable write caching */
317 #define DDF_cache_rallowed 64 /* enable read caching */
319 struct spare_assign
{
325 __u16 populated
; /* SAEs used */
326 __u16 max
; /* max SAEs */
328 struct spare_assign_entry
{
329 char guid
[DDF_GUID_LEN
];
330 __u16 secondary_element
;
334 /* spare_assign.type is a bitmap */
335 #define DDF_spare_dedicated 0x1 /* else global */
336 #define DDF_spare_revertible 0x2 /* else committable */
337 #define DDF_spare_active 0x4 /* else not active */
338 #define DDF_spare_affinity 0x8 /* enclosure affinity */
340 /* The data_section contents - local scope */
344 char guid
[DDF_GUID_LEN
];
345 __u32 refnum
; /* crc of some magic drive data ... */
346 __u8 forced_ref
; /* set when above was not result of magic */
347 __u8 forced_guid
; /* set if guid was forced rather than magic */
352 /* bbm_section content */
353 struct bad_block_log
{
360 struct mapped_block
{
361 __u64 defective_start
;
362 __u32 replacement_start
;
368 /* Struct for internally holding ddf structures */
369 /* The DDF structure stored on each device is potentially
370 * quite different, as some data is global and some is local.
371 * The global data is:
374 * - Physical disk records
375 * - Virtual disk records
377 * - Configuration records
378 * - Physical Disk data section
379 * ( and Bad block and vendor which I don't care about yet).
381 * The local data is parsed into separate lists as it is read
382 * and reconstructed for writing. This means that we only need
383 * to make config changes once and they are automatically
384 * propagated to all devices.
385 * Note that the ddf_super has space of the conf and disk data
386 * for this disk and also for a list of all such data.
387 * The list is only used for the superblock that is being
388 * built in Create or Assemble to describe the whole array.
391 struct ddf_header anchor
, primary
, secondary
, *active
;
392 struct ddf_controller_data controller
;
393 struct phys_disk
*phys
;
394 struct virtual_disk
*virt
;
399 __u64
*lba_offset
; /* location in 'conf' of
401 struct vd_config conf
;
402 } *conflist
, *newconf
;
405 struct disk_data disk
;
409 struct vcl
*vlist
[0]; /* max_part+1 in size */
414 #define offsetof(t,f) ((size_t)&(((t*)0)->f))
417 struct superswitch super_ddf_container
, super_ddf_bvd
;
419 static int calc_crc(void *buf
, int len
)
421 /* crcs are always at the same place as in the ddf_header */
422 struct ddf_header
*ddf
= buf
;
423 __u32 oldcrc
= ddf
->crc
;
425 ddf
->crc
= 0xffffffff;
427 newcrc
= crc32(0, buf
, len
);
432 static int load_ddf_header(int fd
, unsigned long long lba
,
433 unsigned long long size
,
435 struct ddf_header
*hdr
, struct ddf_header
*anchor
)
437 /* read a ddf header (primary or secondary) from fd/lba
438 * and check that it is consistent with anchor
440 * magic, crc, guid, rev, and LBA's header_type, and
441 * everything after header_type must be the same
446 if (lseek64(fd
, lba
<<9, 0) < 0)
449 if (read(fd
, hdr
, 512) != 512)
452 if (hdr
->magic
!= DDF_HEADER_MAGIC
)
454 if (calc_crc(hdr
, 512) != hdr
->crc
)
456 if (memcmp(anchor
->guid
, hdr
->guid
, DDF_GUID_LEN
) != 0 ||
457 memcmp(anchor
->revision
, hdr
->revision
, 8) != 0 ||
458 anchor
->primary_lba
!= hdr
->primary_lba
||
459 anchor
->secondary_lba
!= hdr
->secondary_lba
||
461 memcmp(anchor
->pad2
, hdr
->pad2
, 512 -
462 offsetof(struct ddf_header
, pad2
)) != 0)
465 /* Looks good enough to me... */
469 static void *load_section(int fd
, struct ddf_super
*super
, void *buf
,
470 __u32 offset_be
, __u32 len_be
, int check
)
472 unsigned long long offset
= __be32_to_cpu(offset_be
);
473 unsigned long long len
= __be32_to_cpu(len_be
);
474 int dofree
= (buf
== NULL
);
477 if (len
!= 2 && len
!= 8 && len
!= 32
478 && len
!= 128 && len
!= 512)
484 /* All pre-allocated sections are a single block */
488 buf
= malloc(len
<<9);
492 if (super
->active
->type
== 1)
493 offset
+= __be64_to_cpu(super
->active
->primary_lba
);
495 offset
+= __be64_to_cpu(super
->active
->secondary_lba
);
497 if (lseek64(fd
, offset
<<9, 0) != (offset
<<9)) {
502 if (read(fd
, buf
, len
<<9) != (len
<<9)) {
510 static int load_ddf_headers(int fd
, struct ddf_super
*super
, char *devname
)
512 unsigned long long dsize
;
514 get_dev_size(fd
, NULL
, &dsize
);
516 if (lseek64(fd
, dsize
-512, 0) < 0) {
519 Name
": Cannot seek to anchor block on %s: %s\n",
520 devname
, strerror(errno
));
523 if (read(fd
, &super
->anchor
, 512) != 512) {
526 Name
": Cannot read anchor block on %s: %s\n",
527 devname
, strerror(errno
));
530 if (super
->anchor
.magic
!= DDF_HEADER_MAGIC
) {
532 fprintf(stderr
, Name
": no DDF anchor found on %s\n",
536 if (calc_crc(&super
->anchor
, 512) != super
->anchor
.crc
) {
538 fprintf(stderr
, Name
": bad CRC on anchor on %s\n",
542 if (memcmp(super
->anchor
.revision
, DDF_REVISION
, 8) != 0) {
544 fprintf(stderr
, Name
": can only support super revision"
545 " %.8s, not %.8s on %s\n",
546 DDF_REVISION
, super
->anchor
.revision
, devname
);
549 if (load_ddf_header(fd
, __be64_to_cpu(super
->anchor
.primary_lba
),
551 &super
->primary
, &super
->anchor
) == 0) {
554 Name
": Failed to load primary DDF header "
558 super
->active
= &super
->primary
;
559 if (load_ddf_header(fd
, __be64_to_cpu(super
->anchor
.secondary_lba
),
561 &super
->secondary
, &super
->anchor
)) {
562 if ((__be32_to_cpu(super
->primary
.seq
)
563 < __be32_to_cpu(super
->secondary
.seq
) &&
564 !super
->secondary
.openflag
)
565 || (__be32_to_cpu(super
->primary
.seq
)
566 == __be32_to_cpu(super
->secondary
.seq
) &&
567 super
->primary
.openflag
&& !super
->secondary
.openflag
)
569 super
->active
= &super
->secondary
;
574 static int load_ddf_global(int fd
, struct ddf_super
*super
, char *devname
)
577 ok
= load_section(fd
, super
, &super
->controller
,
578 super
->active
->controller_section_offset
,
579 super
->active
->controller_section_length
,
581 super
->phys
= load_section(fd
, super
, NULL
,
582 super
->active
->phys_section_offset
,
583 super
->active
->phys_section_length
,
585 super
->pdsize
= __be32_to_cpu(super
->active
->phys_section_length
) * 512;
587 super
->virt
= load_section(fd
, super
, NULL
,
588 super
->active
->virt_section_offset
,
589 super
->active
->virt_section_length
,
591 super
->vdsize
= __be32_to_cpu(super
->active
->virt_section_length
) * 512;
599 super
->conflist
= NULL
;
604 static int load_ddf_local(int fd
, struct ddf_super
*super
,
605 char *devname
, int keep
)
613 /* First the local disk info */
614 super
->max_part
= __be16_to_cpu(super
->active
->max_partitions
);
615 dl
= malloc(sizeof(*dl
) +
616 (super
->max_part
+1) * sizeof(dl
->vlist
[0]));
618 load_section(fd
, super
, &dl
->disk
,
619 super
->active
->data_section_offset
,
620 super
->active
->data_section_length
,
622 dl
->devname
= devname
? strdup(devname
) : NULL
;
624 dl
->major
= major(stb
.st_rdev
);
625 dl
->minor
= minor(stb
.st_rdev
);
626 dl
->next
= super
->dlist
;
627 dl
->fd
= keep
? fd
: -1;
628 for (i
=0 ; i
< super
->max_part
+ 1 ; i
++)
632 /* Now the config list. */
633 /* 'conf' is an array of config entries, some of which are
634 * probably invalid. Those which are good need to be copied into
637 conflen
= __be16_to_cpu(super
->active
->config_record_len
);
639 conf
= load_section(fd
, super
, NULL
,
640 super
->active
->config_section_offset
,
641 super
->active
->config_section_length
,
645 i
< __be32_to_cpu(super
->active
->config_section_length
);
647 struct vd_config
*vd
=
648 (struct vd_config
*)((char*)conf
+ i
*512);
651 if (vd
->magic
!= DDF_VD_CONF_MAGIC
)
653 for (vcl
= super
->conflist
; vcl
; vcl
= vcl
->next
) {
654 if (memcmp(vcl
->conf
.guid
,
655 vd
->guid
, DDF_GUID_LEN
) == 0)
660 dl
->vlist
[i
/conflen
] = vcl
;
661 if (__be32_to_cpu(vd
->seqnum
) <=
662 __be32_to_cpu(vcl
->conf
.seqnum
))
665 vcl
= malloc(conflen
*512 + offsetof(struct vcl
, conf
));
666 vcl
->next
= super
->conflist
;
667 super
->conflist
= vcl
;
669 memcpy(&vcl
->conf
, vd
, conflen
*512);
670 vcl
->lba_offset
= (__u64
*)
671 &vcl
->conf
.phys_refnum
[super
->max_part
+1];
672 dl
->vlist
[i
/conflen
] = vcl
;
680 static int load_super_ddf_all(struct supertype
*st
, int fd
,
681 void **sbp
, char *devname
, int keep_fd
);
683 static int load_super_ddf(struct supertype
*st
, int fd
,
686 unsigned long long dsize
;
687 struct ddf_super
*super
;
691 if (load_super_ddf_all(st
, fd
, &st
->sb
, devname
, 0) == 0)
695 if (get_dev_size(fd
, devname
, &dsize
) == 0)
698 /* 32M is a lower bound */
699 if (dsize
<= 32*1024*1024) {
702 Name
": %s is too small for ddf: "
703 "size is %llu sectors.\n",
711 Name
": %s is an odd size for ddf: "
712 "size is %llu bytes.\n",
718 super
= malloc(sizeof(*super
));
720 fprintf(stderr
, Name
": malloc of %zu failed.\n",
725 rv
= load_ddf_headers(fd
, super
, devname
);
731 /* Have valid headers and have chosen the best. Let's read in the rest*/
733 rv
= load_ddf_global(fd
, super
, devname
);
738 Name
": Failed to load all information "
739 "sections on %s\n", devname
);
744 load_ddf_local(fd
, super
, devname
, 0);
746 /* Should possibly check the sections .... */
749 if (st
->ss
== NULL
) {
751 st
->minor_version
= 0;
758 static void free_super_ddf(struct supertype
*st
)
760 struct ddf_super
*ddf
= st
->sb
;
765 while (ddf
->conflist
) {
766 struct vcl
*v
= ddf
->conflist
;
767 ddf
->conflist
= v
->next
;
771 struct dl
*d
= ddf
->dlist
;
772 ddf
->dlist
= d
->next
;
781 static struct supertype
*match_metadata_desc_ddf(char *arg
)
783 /* 'ddf' only support containers */
784 struct supertype
*st
;
785 if (strcmp(arg
, "ddf") != 0 &&
786 strcmp(arg
, "default") != 0
790 st
= malloc(sizeof(*st
));
793 st
->minor_version
= 0;
798 static struct supertype
*match_metadata_desc_ddf_bvd(char *arg
)
800 struct supertype
*st
;
801 if (strcmp(arg
, "ddf/bvd") != 0 &&
802 strcmp(arg
, "bvd") != 0 &&
803 strcmp(arg
, "default") != 0
807 st
= malloc(sizeof(*st
));
808 st
->ss
= &super_ddf_bvd
;
810 st
->minor_version
= 0;
814 static struct supertype
*match_metadata_desc_ddf_svd(char *arg
)
816 struct supertype
*st
;
817 if (strcmp(arg
, "ddf/svd") != 0 &&
818 strcmp(arg
, "svd") != 0 &&
819 strcmp(arg
, "default") != 0
823 st
= malloc(sizeof(*st
));
824 st
->ss
= &super_ddf_svd
;
826 st
->minor_version
= 0;
833 static mapping_t ddf_state
[] = {
839 { "Partially Optimal", 5},
845 static mapping_t ddf_init_state
[] = {
846 { "Not Initialised", 0},
847 { "QuickInit in Progress", 1},
848 { "Fully Initialised", 2},
852 static mapping_t ddf_access
[] = {
856 { "Blocked (no access)", 3},
860 static mapping_t ddf_level
[] = {
861 { "RAID0", DDF_RAID0
},
862 { "RAID1", DDF_RAID1
},
863 { "RAID3", DDF_RAID3
},
864 { "RAID4", DDF_RAID4
},
865 { "RAID5", DDF_RAID5
},
866 { "RAID1E",DDF_RAID1E
},
868 { "CONCAT",DDF_CONCAT
},
869 { "RAID5E",DDF_RAID5E
},
870 { "RAID5EE",DDF_RAID5EE
},
871 { "RAID6", DDF_RAID6
},
874 static mapping_t ddf_sec_level
[] = {
875 { "Striped", DDF_2STRIPED
},
876 { "Mirrored", DDF_2MIRRORED
},
877 { "Concat", DDF_2CONCAT
},
878 { "Spanned", DDF_2SPANNED
},
886 static struct num_mapping ddf_level_num
[] = {
889 { DDF_RAID3
, LEVEL_UNSUPPORTED
},
891 { DDF_RAID1E
, LEVEL_UNSUPPORTED
},
892 { DDF_JBOD
, LEVEL_UNSUPPORTED
},
893 { DDF_CONCAT
, LEVEL_LINEAR
},
894 { DDF_RAID5E
, LEVEL_UNSUPPORTED
},
895 { DDF_RAID5EE
, LEVEL_UNSUPPORTED
},
900 static int map_num1(struct num_mapping
*map
, int num
)
903 for (i
=0 ; map
[i
].num1
!= MAXINT
; i
++)
904 if (map
[i
].num1
== num
)
910 static void print_guid(char *guid
, int tstamp
)
912 /* A GUIDs are part (or all) ASCII and part binary.
913 * They tend to be space padded.
914 * We ignore trailing spaces and print numbers
915 * <0x20 and >=0x7f as \xXX
916 * Some GUIDs have a time stamp in bytes 16-19.
917 * We print that if appropriate
919 int l
= DDF_GUID_LEN
;
921 while (l
&& guid
[l
-1] == ' ')
923 for (i
=0 ; i
<l
; i
++) {
924 if (guid
[i
] >= 0x20 && guid
[i
] < 0x7f)
925 fputc(guid
[i
], stdout
);
927 fprintf(stdout
, "\\x%02x", guid
[i
]&255);
930 time_t then
= __be32_to_cpu(*(__u32
*)(guid
+16)) + DECADE
;
933 tm
= localtime(&then
);
934 strftime(tbuf
, 100, " (%D %T)",tm
);
939 static void examine_vd(int n
, struct ddf_super
*sb
, char *guid
)
941 int crl
= __be16_to_cpu(sb
->anchor
.config_record_len
);
944 for (vcl
= sb
->conflist
; vcl
; vcl
= vcl
->next
) {
945 struct vd_config
*vc
= &vcl
->conf
;
947 if (calc_crc(vc
, crl
*512) != vc
->crc
)
949 if (memcmp(vc
->guid
, guid
, DDF_GUID_LEN
) != 0)
952 /* Ok, we know about this VD, let's give more details */
953 printf(" Raid Devices[%d] : %d\n", n
,
954 __be16_to_cpu(vc
->prim_elmnt_count
));
955 printf(" Chunk Size[%d] : %d sectors\n", n
,
956 1 << vc
->chunk_shift
);
957 printf(" Raid Level[%d] : %s\n", n
,
958 map_num(ddf_level
, vc
->prl
)?:"-unknown-");
959 if (vc
->sec_elmnt_count
!= 1) {
960 printf(" Secondary Position[%d] : %d of %d\n", n
,
961 vc
->sec_elmnt_seq
, vc
->sec_elmnt_count
);
962 printf(" Secondary Level[%d] : %s\n", n
,
963 map_num(ddf_sec_level
, vc
->srl
) ?: "-unknown-");
965 printf(" Device Size[%d] : %llu\n", n
,
966 __be64_to_cpu(vc
->blocks
)/2);
967 printf(" Array Size[%d] : %llu\n", n
,
968 __be64_to_cpu(vc
->array_blocks
)/2);
972 static void examine_vds(struct ddf_super
*sb
)
974 int cnt
= __be16_to_cpu(sb
->virt
->populated_vdes
);
976 printf(" Virtual Disks : %d\n", cnt
);
978 for (i
=0; i
<cnt
; i
++) {
979 struct virtual_entry
*ve
= &sb
->virt
->entries
[i
];
980 printf(" VD GUID[%d] : ", i
); print_guid(ve
->guid
, 1);
982 printf(" unit[%d] : %d\n", i
, __be16_to_cpu(ve
->unit
));
983 printf(" state[%d] : %s, %s%s\n", i
,
984 map_num(ddf_state
, ve
->state
& 7),
985 (ve
->state
& 8) ? "Morphing, ": "",
986 (ve
->state
& 16)? "Not Consistent" : "Consistent");
987 printf(" init state[%d] : %s\n", i
,
988 map_num(ddf_init_state
, ve
->init_state
&3));
989 printf(" access[%d] : %s\n", i
,
990 map_num(ddf_access
, (ve
->init_state
>>6) & 3));
991 printf(" Name[%d] : %.16s\n", i
, ve
->name
);
992 examine_vd(i
, sb
, ve
->guid
);
994 if (cnt
) printf("\n");
997 static void examine_pds(struct ddf_super
*sb
)
999 int cnt
= __be16_to_cpu(sb
->phys
->used_pdes
);
1002 printf(" Physical Disks : %d\n", cnt
);
1004 for (i
=0 ; i
<cnt
; i
++) {
1005 struct phys_disk_entry
*pd
= &sb
->phys
->entries
[i
];
1006 int type
= __be16_to_cpu(pd
->type
);
1007 int state
= __be16_to_cpu(pd
->state
);
1009 printf(" PD GUID[%d] : ", i
); print_guid(pd
->guid
, 0);
1011 printf(" ref[%d] : %08x\n", i
,
1012 __be32_to_cpu(pd
->refnum
));
1013 printf(" mode[%d] : %s%s%s%s%s\n", i
,
1014 (type
&2) ? "active":"",
1015 (type
&4) ? "Global Spare":"",
1016 (type
&8) ? "spare" : "",
1017 (type
&16)? ", foreign" : "",
1018 (type
&32)? "pass-through" : "");
1019 printf(" state[%d] : %s%s%s%s%s%s%s\n", i
,
1020 (state
&1)? "Online": "Offline",
1021 (state
&2)? ", Failed": "",
1022 (state
&4)? ", Rebuilding": "",
1023 (state
&8)? ", in-transition": "",
1024 (state
&16)? ", SMART errors": "",
1025 (state
&32)? ", Unrecovered Read Errors": "",
1026 (state
&64)? ", Missing" : "");
1027 printf(" Avail Size[%d] : %llu K\n", i
,
1028 __be64_to_cpu(pd
->config_size
)>>1);
1029 for (dl
= sb
->dlist
; dl
; dl
= dl
->next
) {
1030 if (dl
->disk
.refnum
== pd
->refnum
) {
1031 char *dv
= map_dev(dl
->major
, dl
->minor
, 0);
1033 printf(" Device[%d] : %s\n",
1041 static void examine_super_ddf(struct supertype
*st
, char *homehost
)
1043 struct ddf_super
*sb
= st
->sb
;
1045 printf(" Magic : %08x\n", __be32_to_cpu(sb
->anchor
.magic
));
1046 printf(" Version : %.8s\n", sb
->anchor
.revision
);
1047 printf("Controller GUID : "); print_guid(sb
->anchor
.guid
, 1);
1049 printf(" Seq : %08x\n", __be32_to_cpu(sb
->active
->seq
));
1050 printf(" Redundant hdr : %s\n", sb
->secondary
.magic
== DDF_HEADER_MAGIC
1056 static void brief_examine_super_ddf(struct supertype
*st
)
1058 /* We just write a generic DDF ARRAY entry
1059 * The uuid is all hex, 6 groups of 4 bytes
1061 struct ddf_super
*ddf
= st
->sb
;
1063 printf("ARRAY /dev/ddf UUID=");
1064 for (i
= 0; i
< DDF_GUID_LEN
; i
++) {
1065 printf("%02x", ddf
->anchor
.guid
[i
]);
1066 if ((i
&3) == 0 && i
!= 0)
1072 static void detail_super_ddf(struct supertype
*st
, char *homehost
)
1075 * Could print DDF GUID
1076 * Need to find which array
1077 * If whole, briefly list all arrays
1082 static void brief_detail_super_ddf(struct supertype
*st
)
1084 /* FIXME I really need to know which array we are detailing.
1085 * Can that be stored in ddf_super??
1087 // struct ddf_super *ddf = st->sb;
1093 static int match_home_ddf(struct supertype
*st
, char *homehost
)
1095 /* It matches 'this' host if the controller is a
1096 * Linux-MD controller with vendor_data matching
1099 struct ddf_super
*ddf
= st
->sb
;
1100 int len
= strlen(homehost
);
1102 return (memcmp(ddf
->controller
.guid
, T10
, 8) == 0 &&
1103 len
< sizeof(ddf
->controller
.vendor_data
) &&
1104 memcmp(ddf
->controller
.vendor_data
, homehost
,len
) == 0 &&
1105 ddf
->controller
.vendor_data
[len
] == 0);
1108 static struct vd_config
*find_vdcr(struct ddf_super
*ddf
)
1110 /* FIXME this just picks off the first one */
1111 return &ddf
->conflist
->conf
;
1114 static void uuid_from_super_ddf(struct supertype
*st
, int uuid
[4])
1116 /* The uuid returned here is used for:
1117 * uuid to put into bitmap file (Create, Grow)
1118 * uuid for backup header when saving critical section (Grow)
1119 * comparing uuids when re-adding a device into an array
1120 * For each of these we can make do with a truncated
1121 * or hashed uuid rather than the original, as long as
1123 * In each case the uuid required is that of the data-array,
1124 * not the device-set.
1125 * In the case of SVD we assume the BVD is of interest,
1126 * though that might be the case if a bitmap were made for
1127 * a mirrored SVD - worry about that later.
1128 * So we need to find the VD configuration record for the
1129 * relevant BVD and extract the GUID and Secondary_Element_Seq.
1130 * The first 16 bytes of the sha1 of these is used.
1132 struct ddf_super
*ddf
= st
->sb
;
1133 struct vd_config
*vd
= find_vdcr(ddf
);
1136 memset(uuid
, 0, sizeof (uuid
));
1139 struct sha1_ctx ctx
;
1140 sha1_init_ctx(&ctx
);
1141 sha1_process_bytes(&vd
->guid
, DDF_GUID_LEN
, &ctx
);
1142 if (vd
->sec_elmnt_count
> 1)
1143 sha1_process_bytes(&vd
->sec_elmnt_seq
, 1, &ctx
);
1144 sha1_finish_ctx(&ctx
, buf
);
1145 memcpy(uuid
, buf
, sizeof(uuid
));
1149 static void getinfo_super_ddf(struct supertype
*st
, struct mdinfo
*info
)
1151 struct ddf_super
*ddf
= st
->sb
;
1153 info
->array
.major_version
= 1000;
1154 info
->array
.minor_version
= 0; /* FIXME use ddf->revision somehow */
1155 info
->array
.patch_version
= 0;
1156 info
->array
.raid_disks
= __be16_to_cpu(ddf
->phys
->used_pdes
);
1157 info
->array
.level
= LEVEL_CONTAINER
;
1158 info
->array
.layout
= 0;
1159 info
->array
.md_minor
= -1;
1160 info
->array
.ctime
= DECADE
+ __be32_to_cpu(*(__u32
*)
1161 (ddf
->anchor
.guid
+16));
1162 info
->array
.utime
= 0;
1163 info
->array
.chunk_size
= 0;
1165 // info->data_offset = ???;
1166 // info->component_size = ???;
1168 info
->disk
.major
= 0;
1169 info
->disk
.minor
= 0;
1170 // info->disk.number = __be32_to_cpu(ddf->disk.refnum);
1171 // info->disk.raid_disk = find refnum in the table and use index;
1172 // info->disk.state = ???;
1174 // uuid_from_super_ddf(info->uuid, sbv);
1176 // info->name[] ?? ;
1179 static void getinfo_super_ddf_bvd(struct supertype
*st
, struct mdinfo
*info
)
1181 struct ddf_super
*ddf
= st
->sb
;
1182 struct vd_config
*vd
= find_vdcr(ddf
);
1184 /* FIXME this returns BVD info - what if we want SVD ?? */
1186 info
->array
.major_version
= 1000;
1187 info
->array
.minor_version
= 0; /* FIXME use ddf->revision somehow */
1188 info
->array
.patch_version
= 0;
1189 info
->array
.raid_disks
= __be16_to_cpu(vd
->prim_elmnt_count
);
1190 info
->array
.level
= map_num1(ddf_level_num
, vd
->prl
);
1191 info
->array
.layout
= vd
->rlq
; /* FIXME should this be mapped */
1192 info
->array
.md_minor
= -1;
1193 info
->array
.ctime
= DECADE
+ __be32_to_cpu(*(__u32
*)(vd
->guid
+16));
1194 info
->array
.utime
= DECADE
+ __be32_to_cpu(vd
->timestamp
);
1195 info
->array
.chunk_size
= 512 << vd
->chunk_shift
;
1197 // info->data_offset = ???;
1198 // info->component_size = ???;
1200 info
->disk
.major
= 0;
1201 info
->disk
.minor
= 0;
1202 // info->disk.number = __be32_to_cpu(ddf->disk.refnum);
1203 // info->disk.raid_disk = find refnum in the table and use index;
1204 // info->disk.state = ???;
1206 uuid_from_super_ddf(st
, info
->uuid
);
1208 // info->name[] ?? ;
1211 static int update_super_ddf(struct supertype
*st
, struct mdinfo
*info
,
1213 char *devname
, int verbose
,
1214 int uuid_set
, char *homehost
)
1216 /* For 'assemble' and 'force' we need to return non-zero if any
1217 * change was made. For others, the return value is ignored.
1218 * Update options are:
1219 * force-one : This device looks a bit old but needs to be included,
1220 * update age info appropriately.
1221 * assemble: clear any 'faulty' flag to allow this device to
1223 * force-array: Array is degraded but being forced, mark it clean
1224 * if that will be needed to assemble it.
1226 * newdev: not used ????
1227 * grow: Array has gained a new device - this is currently for
1229 * resync: mark as dirty so a resync will happen.
1230 * uuid: Change the uuid of the array to match watch is given
1231 * homehost: update the recorded homehost
1232 * name: update the name - preserving the homehost
1233 * _reshape_progress: record new reshape_progress position.
1235 * Following are not relevant for this version:
1236 * sparc2.2 : update from old dodgey metadata
1237 * super-minor: change the preferred_minor number
1238 * summaries: update redundant counters.
1241 // struct ddf_super *ddf = st->sb;
1242 // struct vd_config *vd = find_vdcr(ddf);
1243 // struct virtual_entry *ve = find_ve(ddf);
1246 /* we don't need to handle "force-*" or "assemble" as
1247 * there is no need to 'trick' the kernel. We the metadata is
1248 * first updated to activate the array, all the implied modifications
1252 if (strcmp(update
, "grow") == 0) {
1255 if (strcmp(update
, "resync") == 0) {
1256 // info->resync_checkpoint = 0;
1258 /* We ignore UUID updates as they make even less sense
1261 if (strcmp(update
, "homehost") == 0) {
1262 /* homehost is stored in controller->vendor_data,
1263 * or it is when we are the vendor
1265 // if (info->vendor_is_local)
1266 // strcpy(ddf->controller.vendor_data, homehost);
1268 if (strcmp(update
, "name") == 0) {
1269 /* name is stored in virtual_entry->name */
1270 // memset(ve->name, ' ', 16);
1271 // strncpy(ve->name, info->name, 16);
1273 if (strcmp(update
, "_reshape_progress") == 0) {
1274 /* We don't support reshape yet */
1277 // update_all_csum(ddf);
1282 static int init_super_ddf(struct supertype
*st
,
1283 mdu_array_info_t
*info
,
1284 unsigned long long size
, char *name
, char *homehost
,
1287 /* This is primarily called by Create when creating a new array.
1288 * We will then get add_to_super called for each component, and then
1289 * write_init_super called to write it out to each device.
1290 * For DDF, Create can create on fresh devices or on a pre-existing
1292 * To create on a pre-existing array a different method will be called.
1293 * This one is just for fresh drives.
1295 * We need to create the entire 'ddf' structure which includes:
1296 * DDF headers - these are easy.
1297 * Controller data - a Sector describing this controller .. not that
1298 * this is a controller exactly.
1299 * Physical Disk Record - one entry per device, so
1300 * leave plenty of space.
1301 * Virtual Disk Records - again, just leave plenty of space.
1302 * This just lists VDs, doesn't give details
1303 * Config records - describes the VDs that use this disk
1304 * DiskData - describes 'this' device.
1305 * BadBlockManagement - empty
1306 * Diag Space - empty
1307 * Vendor Logs - Could we put bitmaps here?
1310 struct ddf_super
*ddf
;
1315 int max_phys_disks
, max_virt_disks
;
1316 unsigned long long sector
;
1320 struct phys_disk
*pd
;
1321 struct virtual_disk
*vd
;
1323 ddf
= malloc(sizeof(*ddf
));
1324 ddf
->dlist
= NULL
; /* no physical disks yet */
1325 ddf
->conflist
= NULL
; /* No virtual disks yet */
1327 /* At least 32MB *must* be reserved for the ddf. So let's just
1328 * start 32MB from the end, and put the primary header there.
1329 * Don't do secondary for now.
1330 * We don't know exactly where that will be yet as it could be
1331 * different on each device. To just set up the lengths.
1335 ddf
->anchor
.magic
= DDF_HEADER_MAGIC
;
1336 /* 24 bytes of fiction required.
1337 * first 8 are a 'vendor-id' - "Linux-MD"
1338 * next 8 are controller type.. how about 0X DEAD BEEF 0000 0000
1339 * Remaining 16 are serial number.... maybe a hostname would do?
1341 memcpy(ddf
->anchor
.guid
, T10
, sizeof(T10
));
1342 stamp
= __cpu_to_be32(0xdeadbeef);
1343 memcpy(ddf
->anchor
.guid
+8, &stamp
, 4);
1344 stamp
= __cpu_to_be32(0);
1345 memcpy(ddf
->anchor
.guid
+12, &stamp
, 4);
1346 stamp
= __cpu_to_be32(time(0) - DECADE
);
1347 memcpy(ddf
->anchor
.guid
+16, &stamp
, 4);
1348 rfd
= open("/dev/urandom", O_RDONLY
);
1349 if (rfd
< 0 || read(rfd
, &stamp
, 4) != 4)
1351 memcpy(ddf
->anchor
.guid
+20, &stamp
, 4);
1352 if (rfd
>= 0) close(rfd
);
1354 memcpy(ddf
->anchor
.revision
, DDF_REVISION
, 8);
1355 ddf
->anchor
.seq
= __cpu_to_be32(1);
1356 ddf
->anchor
.timestamp
= __cpu_to_be32(time(0) - DECADE
);
1357 ddf
->anchor
.openflag
= 0xFF;
1358 ddf
->anchor
.foreignflag
= 0;
1359 ddf
->anchor
.enforcegroups
= 0; /* Is this best?? */
1360 ddf
->anchor
.pad0
= 0xff;
1361 memset(ddf
->anchor
.pad1
, 0xff, 12);
1362 memset(ddf
->anchor
.header_ext
, 0xff, 32);
1363 ddf
->anchor
.primary_lba
= ~(__u64
)0;
1364 ddf
->anchor
.secondary_lba
= ~(__u64
)0;
1365 ddf
->anchor
.type
= DDF_HEADER_ANCHOR
;
1366 memset(ddf
->anchor
.pad2
, 0xff, 3);
1367 ddf
->anchor
.workspace_len
= __cpu_to_be32(32768); /* Must be reserved */
1368 ddf
->anchor
.workspace_lba
= ~(__u64
)0; /* Put this at bottom
1369 of 32M reserved.. */
1370 max_phys_disks
= 1023; /* Should be enough */
1371 ddf
->anchor
.max_pd_entries
= __cpu_to_be16(max_phys_disks
);
1372 max_virt_disks
= 255;
1373 ddf
->anchor
.max_vd_entries
= __cpu_to_be16(max_virt_disks
); /* ?? */
1374 ddf
->anchor
.max_partitions
= __cpu_to_be16(64); /* ?? */
1376 ddf
->anchor
.config_record_len
= __cpu_to_be16(1 + 256*12/512);
1377 ddf
->anchor
.max_primary_element_entries
= __cpu_to_be16(256);
1378 memset(ddf
->anchor
.pad3
, 0xff, 54);
1380 /* controller sections is one sector long immediately
1381 * after the ddf header */
1383 ddf
->anchor
.controller_section_offset
= __cpu_to_be32(sector
);
1384 ddf
->anchor
.controller_section_length
= __cpu_to_be32(1);
1387 /* phys is 8 sectors after that */
1388 pdsize
= ROUND_UP(sizeof(struct phys_disk
) +
1389 sizeof(struct phys_disk_entry
)*max_phys_disks
,
1391 switch(pdsize
/512) {
1392 case 2: case 8: case 32: case 128: case 512: break;
1395 ddf
->anchor
.phys_section_offset
= __cpu_to_be32(sector
);
1396 ddf
->anchor
.phys_section_length
=
1397 __cpu_to_be32(pdsize
/512); /* max_primary_element_entries/8 */
1398 sector
+= pdsize
/512;
1400 /* virt is another 32 sectors */
1401 vdsize
= ROUND_UP(sizeof(struct virtual_disk
) +
1402 sizeof(struct virtual_entry
) * max_virt_disks
,
1404 switch(vdsize
/512) {
1405 case 2: case 8: case 32: case 128: case 512: break;
1408 ddf
->anchor
.virt_section_offset
= __cpu_to_be32(sector
);
1409 ddf
->anchor
.virt_section_length
=
1410 __cpu_to_be32(vdsize
/512); /* max_vd_entries/8 */
1411 sector
+= vdsize
/512;
1413 clen
= (1 + 256*12/512) * (64+1);
1414 ddf
->anchor
.config_section_offset
= __cpu_to_be32(sector
);
1415 ddf
->anchor
.config_section_length
= __cpu_to_be32(clen
);
1418 ddf
->anchor
.data_section_offset
= __cpu_to_be32(sector
);
1419 ddf
->anchor
.data_section_length
= __cpu_to_be32(1);
1422 ddf
->anchor
.bbm_section_length
= __cpu_to_be32(0);
1423 ddf
->anchor
.bbm_section_offset
= __cpu_to_be32(0xFFFFFFFF);
1424 ddf
->anchor
.diag_space_length
= __cpu_to_be32(0);
1425 ddf
->anchor
.diag_space_offset
= __cpu_to_be32(0xFFFFFFFF);
1426 ddf
->anchor
.vendor_length
= __cpu_to_be32(0);
1427 ddf
->anchor
.vendor_offset
= __cpu_to_be32(0xFFFFFFFF);
1429 memset(ddf
->anchor
.pad4
, 0xff, 256);
1431 memcpy(&ddf
->primary
, &ddf
->anchor
, 512);
1432 memcpy(&ddf
->secondary
, &ddf
->anchor
, 512);
1434 ddf
->primary
.openflag
= 1; /* I guess.. */
1435 ddf
->primary
.type
= DDF_HEADER_PRIMARY
;
1437 ddf
->secondary
.openflag
= 1; /* I guess.. */
1438 ddf
->secondary
.type
= DDF_HEADER_SECONDARY
;
1440 ddf
->active
= &ddf
->primary
;
1442 ddf
->controller
.magic
= DDF_CONTROLLER_MAGIC
;
1444 /* 24 more bytes of fiction required.
1445 * first 8 are a 'vendor-id' - "Linux-MD"
1446 * Remaining 16 are serial number.... maybe a hostname would do?
1448 memcpy(ddf
->controller
.guid
, T10
, sizeof(T10
));
1449 gethostname(hostname
, 17);
1451 hostlen
= strlen(hostname
);
1452 memcpy(ddf
->controller
.guid
+ 24 - hostlen
, hostname
, hostlen
);
1453 for (i
= strlen(T10
) ; i
+hostlen
< 24; i
++)
1454 ddf
->controller
.guid
[i
] = ' ';
1456 ddf
->controller
.type
.vendor_id
= __cpu_to_be16(0xDEAD);
1457 ddf
->controller
.type
.device_id
= __cpu_to_be16(0xBEEF);
1458 ddf
->controller
.type
.sub_vendor_id
= 0;
1459 ddf
->controller
.type
.sub_device_id
= 0;
1460 memcpy(ddf
->controller
.product_id
, "What Is My PID??", 16);
1461 memset(ddf
->controller
.pad
, 0xff, 8);
1462 memset(ddf
->controller
.vendor_data
, 0xff, 448);
1464 pd
= ddf
->phys
= malloc(pdsize
);
1465 ddf
->pdsize
= pdsize
;
1467 memset(pd
, 0xff, pdsize
);
1468 memset(pd
, 0, sizeof(*pd
));
1469 pd
->magic
= DDF_PHYS_DATA_MAGIC
;
1470 pd
->used_pdes
= __cpu_to_be16(0);
1471 pd
->max_pdes
= __cpu_to_be16(max_phys_disks
);
1472 memset(pd
->pad
, 0xff, 52);
1474 vd
= ddf
->virt
= malloc(vdsize
);
1475 ddf
->vdsize
= vdsize
;
1476 memset(vd
, 0, vdsize
);
1477 vd
->magic
= DDF_VIRT_RECORDS_MAGIC
;
1478 vd
->populated_vdes
= __cpu_to_be16(0);
1479 vd
->max_vdes
= __cpu_to_be16(max_virt_disks
);
1480 memset(vd
->pad
, 0xff, 52);
1486 /* add a device to a container, either while creating it or while
1487 * expanding a pre-existing container
1489 static void add_to_super_ddf(struct supertype
*st
,
1490 mdu_disk_info_t
*dk
, int fd
, char *devname
)
1492 struct ddf_super
*ddf
= st
->sb
;
1496 unsigned long long size
;
1497 struct phys_disk_entry
*pde
;
1501 /* This is device numbered dk->number. We need to create
1502 * a phys_disk entry and a more detailed disk_data entry.
1505 dd
= malloc(sizeof(*dd
) + sizeof(dd
->vlist
[0]) * (ddf
->max_part
+1));
1506 dd
->major
= major(stb
.st_rdev
);
1507 dd
->minor
= minor(stb
.st_rdev
);
1508 dd
->devname
= devname
;
1509 dd
->next
= ddf
->dlist
;
1512 dd
->disk
.magic
= DDF_PHYS_DATA_MAGIC
;
1514 tm
= localtime(&now
);
1515 sprintf(dd
->disk
.guid
, "%8s%04d%02d%02d",
1516 T10
, tm
->tm_year
+1900, tm
->tm_mon
+1, tm
->tm_mday
);
1517 *(__u32
*)(dd
->disk
.guid
+ 16) = random();
1518 *(__u32
*)(dd
->disk
.guid
+ 20) = random();
1520 dd
->disk
.refnum
= random(); /* and hope for the best */
1521 dd
->disk
.forced_ref
= 1;
1522 dd
->disk
.forced_guid
= 1;
1523 memset(dd
->disk
.vendor
, ' ', 32);
1524 memcpy(dd
->disk
.vendor
, "Linux", 5);
1525 memset(dd
->disk
.pad
, 0xff, 442);
1526 for (i
= 0; i
< ddf
->max_part
+1 ; i
++)
1527 dd
->vlist
[i
] = NULL
;
1529 n
= __be16_to_cpu(ddf
->phys
->used_pdes
);
1530 pde
= &ddf
->phys
->entries
[n
];
1532 ddf
->phys
->used_pdes
= __cpu_to_be16(n
);
1534 memcpy(pde
->guid
, dd
->disk
.guid
, DDF_GUID_LEN
);
1535 pde
->refnum
= dd
->disk
.refnum
;
1536 pde
->type
= __cpu_to_be16(DDF_Forced_PD_GUID
|DDF_Global_Spare
);
1537 pde
->state
= __cpu_to_be16(DDF_Online
);
1538 get_dev_size(fd
, NULL
, &size
);
1539 /* We are required to reserve 32Meg, and record the size in sectors */
1540 pde
->config_size
= __cpu_to_be64( (size
- 32*1024*1024) / 512);
1541 sprintf(pde
->path
, "%17.17s","Information: nil") ;
1542 memset(pde
->pad
, 0xff, 6);
1548 * This is the write_init_super method for a ddf container. It is
1549 * called when creating a container or adding another device to a
1554 static int write_init_super_ddf(struct supertype
*st
)
1557 struct ddf_super
*ddf
= st
->sb
;
1563 unsigned long long size
, sector
;
1565 for (d
= ddf
->dlist
; d
; d
=d
->next
) {
1571 /* We need to fill in the primary, (secondary) and workspace
1572 * lba's in the headers, set their checksums,
1573 * Also checksum phys, virt....
1575 * Then write everything out, finally the anchor is written.
1577 get_dev_size(fd
, NULL
, &size
);
1579 ddf
->anchor
.workspace_lba
= __cpu_to_be64(size
- 32*1024*2);
1580 ddf
->anchor
.primary_lba
= __cpu_to_be64(size
- 16*1024*2);
1581 ddf
->anchor
.seq
= __cpu_to_be32(1);
1582 memcpy(&ddf
->primary
, &ddf
->anchor
, 512);
1583 memcpy(&ddf
->secondary
, &ddf
->anchor
, 512);
1585 ddf
->anchor
.openflag
= 0xFF; /* 'open' means nothing */
1586 ddf
->anchor
.seq
= 0xFFFFFFFF; /* no sequencing in anchor */
1587 ddf
->anchor
.crc
= calc_crc(&ddf
->anchor
, 512);
1589 ddf
->primary
.openflag
= 0;
1590 ddf
->primary
.type
= DDF_HEADER_PRIMARY
;
1592 ddf
->secondary
.openflag
= 0;
1593 ddf
->secondary
.type
= DDF_HEADER_SECONDARY
;
1595 ddf
->primary
.crc
= calc_crc(&ddf
->primary
, 512);
1596 ddf
->secondary
.crc
= calc_crc(&ddf
->secondary
, 512);
1598 sector
= size
- 16*1024*2;
1599 lseek64(fd
, sector
<<9, 0);
1600 write(fd
, &ddf
->primary
, 512);
1602 ddf
->controller
.crc
= calc_crc(&ddf
->controller
, 512);
1603 write(fd
, &ddf
->controller
, 512);
1605 ddf
->phys
->crc
= calc_crc(ddf
->phys
, ddf
->pdsize
);
1607 write(fd
, ddf
->phys
, ddf
->pdsize
);
1609 ddf
->virt
->crc
= calc_crc(ddf
->virt
, ddf
->vdsize
);
1610 write(fd
, ddf
->virt
, ddf
->vdsize
);
1612 /* Now write lots of config records. */
1613 n_config
= __be16_to_cpu(ddf
->active
->max_partitions
);
1614 conf_size
= __be16_to_cpu(ddf
->active
->config_record_len
) * 512;
1615 for (i
= 0 ; i
<= n_config
; i
++) {
1616 struct vcl
*c
= d
->vlist
[i
];
1619 c
->conf
.crc
= calc_crc(&c
->conf
, conf_size
);
1620 write(fd
, &c
->conf
, conf_size
);
1622 __u32 sig
= 0xffffffff;
1624 lseek64(fd
, conf_size
-4, SEEK_CUR
);
1627 d
->disk
.crc
= calc_crc(&d
->disk
, 512);
1628 write(fd
, &d
->disk
, 512);
1630 /* Maybe do the same for secondary */
1632 lseek64(fd
, (size
-1)*512, SEEK_SET
);
1633 write(fd
, &ddf
->anchor
, 512);
1640 static __u64
avail_size_ddf(struct supertype
*st
, __u64 devsize
)
1642 /* We must reserve the last 32Meg */
1643 if (devsize
<= 32*1024*2)
1645 return devsize
- 32*1024*2;
1649 int validate_geometry_ddf(struct supertype
*st
,
1650 int level
, int layout
, int raiddisks
,
1651 int chunk
, unsigned long long size
,
1652 char *dev
, unsigned long long *freesize
)
1658 /* ddf potentially supports lots of things, but it depends on
1659 * what devices are offered (and maybe kernel version?)
1660 * If given unused devices, we will make a container.
1661 * If given devices in a container, we will make a BVD.
1662 * If given BVDs, we make an SVD, changing all the GUIDs in the process.
1665 if (level
== LEVEL_CONTAINER
) {
1666 st
->ss
= &super_ddf_container
;
1667 return st
->ss
->validate_geometry(st
, level
, layout
, raiddisks
,
1668 chunk
, size
, dev
, freesize
);
1673 /* This device needs to be either a device in a 'ddf' container,
1674 * or it needs to be a 'ddf-bvd' array. Test the first first.
1677 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
1679 sra
= sysfs_read(fd
, 0, GET_VERSION
);
1681 if (sra
&& sra
->array
.major_version
== -1 &&
1682 strcmp(sra
->text_version
, "ddf-bvd") == 0) {
1683 st
->ss
= &super_ddf_svd
;
1684 return st
->ss
->validate_geometry(st
, level
, layout
,
1685 raiddisks
, chunk
, size
,
1690 Name
": Cannot create this array on device %s\n",
1694 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
1695 fprintf(stderr
, Name
": Cannot open %s: %s\n",
1696 dev
, strerror(errno
));
1699 /* Well, it is in use by someone, maybe a 'ddf' container. */
1700 cfd
= open_container(fd
);
1703 fprintf(stderr
, Name
": Cannot use %s: It is busy\n",
1707 sra
= sysfs_read(cfd
, 0, GET_VERSION
);
1709 if (sra
&& sra
->array
.major_version
== -1 &&
1710 strcmp(sra
->text_version
, "ddf") == 0) {
1711 /* This is a member of a ddf container. Load the container
1712 * and try to create a bvd
1714 struct ddf_super
*ddf
;
1715 st
->ss
= &super_ddf_bvd
;
1716 if (load_super_ddf_all(st
, cfd
, (void **)&ddf
, NULL
, 1) == 0) {
1719 return st
->ss
->validate_geometry(st
, level
, layout
,
1720 raiddisks
, chunk
, size
,
1725 fprintf(stderr
, Name
": Cannot use %s: Already in use\n",
1730 int validate_geometry_ddf_container(struct supertype
*st
,
1731 int level
, int layout
, int raiddisks
,
1732 int chunk
, unsigned long long size
,
1733 char *dev
, unsigned long long *freesize
)
1736 unsigned long long ldsize
;
1738 if (level
!= LEVEL_CONTAINER
)
1743 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
1745 fprintf(stderr
, Name
": Cannot open %s: %s\n",
1746 dev
, strerror(errno
));
1749 if (!get_dev_size(fd
, dev
, &ldsize
)) {
1755 *freesize
= avail_size_ddf(st
, ldsize
);
1760 int validate_geometry_ddf_bvd(struct supertype
*st
,
1761 int level
, int layout
, int raiddisks
,
1762 int chunk
, unsigned long long size
,
1763 char *dev
, unsigned long long *freesize
)
1766 struct ddf_super
*ddf
= st
->sb
;
1768 /* ddf/bvd supports lots of things, but not containers */
1769 if (level
== LEVEL_CONTAINER
)
1771 /* We must have the container info already read in. */
1775 /* This device must be a member of the set */
1776 if (stat(dev
, &stb
) < 0)
1778 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
1780 for (dl
= ddf
->dlist
; dl
; dl
= dl
->next
) {
1781 if (dl
->major
== major(stb
.st_rdev
) &&
1782 dl
->minor
== minor(stb
.st_rdev
))
1789 int validate_geometry_ddf_svd(struct supertype
*st
,
1790 int level
, int layout
, int raiddisks
,
1791 int chunk
, unsigned long long size
,
1792 char *dev
, unsigned long long *freesize
)
1794 /* dd/svd only supports striped, mirrored, concat, spanned... */
1795 if (level
!= LEVEL_LINEAR
&&
1803 static int load_super_ddf_all(struct supertype
*st
, int fd
,
1804 void **sbp
, char *devname
, int keep_fd
)
1807 struct ddf_super
*super
;
1808 struct mdinfo
*sd
, *best
= NULL
;
1814 sra
= sysfs_read(fd
, 0, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
1817 if (sra
->array
.major_version
!= -1 ||
1818 sra
->array
.minor_version
!= -2 ||
1819 strcmp(sra
->text_version
, "ddf") != 0)
1822 super
= malloc(sizeof(*super
));
1826 /* first, try each device, and choose the best ddf */
1827 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
1829 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
1830 dfd
= dev_open(nm
, keep_fd
? O_RDWR
: O_RDONLY
);
1833 rv
= load_ddf_headers(dfd
, super
, NULL
);
1834 if (!keep_fd
) close(dfd
);
1836 seq
= __be32_to_cpu(super
->active
->seq
);
1837 if (super
->active
->openflag
)
1839 if (!best
|| seq
> bestseq
) {
1847 /* OK, load this ddf */
1848 sprintf(nm
, "%d:%d", best
->disk
.major
, best
->disk
.minor
);
1849 dfd
= dev_open(nm
, O_RDONLY
);
1852 load_ddf_headers(dfd
, super
, NULL
);
1853 load_ddf_global(dfd
, super
, NULL
);
1855 /* Now we need the device-local bits */
1856 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
1857 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
1858 dfd
= dev_open(nm
, O_RDONLY
);
1861 seq
= load_ddf_local(dfd
, super
, NULL
, keep_fd
);
1865 if (st
->ss
== NULL
) {
1866 st
->ss
= &super_ddf
;
1867 st
->minor_version
= 0;
1876 static int init_zero_ddf(struct supertype
*st
,
1877 mdu_array_info_t
*info
,
1878 unsigned long long size
, char *name
,
1879 char *homehost
, int *uuid
)
1885 static int store_zero_ddf(struct supertype
*st
, int fd
)
1887 unsigned long long dsize
;
1889 memset(buf
, 0, 512);
1892 if (!get_dev_size(fd
, NULL
, &dsize
))
1895 lseek64(fd
, dsize
-512, 0);
1896 write(fd
, buf
, 512);
1900 struct superswitch super_ddf
= {
1902 .examine_super
= examine_super_ddf
,
1903 .brief_examine_super
= brief_examine_super_ddf
,
1904 .detail_super
= detail_super_ddf
,
1905 .brief_detail_super
= brief_detail_super_ddf
,
1906 .validate_geometry
= validate_geometry_ddf
,
1908 .match_home
= match_home_ddf
,
1909 .uuid_from_super
= uuid_from_super_ddf
,
1910 .getinfo_super
= getinfo_super_ddf
,
1911 .update_super
= update_super_ddf
,
1913 .avail_size
= avail_size_ddf
,
1915 .load_super
= load_super_ddf
,
1916 .init_super
= init_zero_ddf
,
1917 .store_super
= store_zero_ddf
,
1918 .free_super
= free_super_ddf
,
1919 .match_metadata_desc
= match_metadata_desc_ddf
,
1925 .text_version
= "ddf",
1928 /* Super_ddf_container is set by validate_geometry_ddf when given a
1929 * device that is not part of any array
1931 struct superswitch super_ddf_container
= {
1933 .validate_geometry
= validate_geometry_ddf_container
,
1934 .write_init_super
= write_init_super_ddf
,
1937 .init_super
= init_super_ddf
,
1938 .add_to_super
= add_to_super_ddf
,
1940 .free_super
= free_super_ddf
,
1945 .text_version
= "ddf",
1948 struct superswitch super_ddf_bvd
= {
1950 // .detail_super = detail_super_ddf_bvd,
1951 // .brief_detail_super = brief_detail_super_ddf_bvd,
1952 .validate_geometry
= validate_geometry_ddf_bvd
,
1954 .update_super
= update_super_ddf
,
1955 .init_super
= init_super_ddf
,
1956 .getinfo_super
= getinfo_super_ddf_bvd
,
1958 .load_super
= load_super_ddf
,
1959 .free_super
= free_super_ddf
,
1960 .match_metadata_desc
= match_metadata_desc_ddf_bvd
,
1966 .text_version
= "ddf",
1969 struct superswitch super_ddf_svd
= {
1971 // .detail_super = detail_super_ddf_svd,
1972 // .brief_detail_super = brief_detail_super_ddf_svd,
1973 .validate_geometry
= validate_geometry_ddf_bvd
,
1975 .update_super
= update_super_ddf
,
1976 .init_super
= init_super_ddf
,
1978 .load_super
= load_super_ddf
,
1979 .free_super
= free_super_ddf
,
1980 .match_metadata_desc
= match_metadata_desc_ddf_svd
,
1986 .text_version
= "ddf",