xref: /freebsd/sys/geom/raid/md_ddf.c (revision edf8578117e8844e02c0121147f45e4609b30680)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2012 Alexander Motin <mav@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/bio.h>
32 #include <sys/gsb_crc32.h>
33 #include <sys/endian.h>
34 #include <sys/kernel.h>
35 #include <sys/kobj.h>
36 #include <sys/limits.h>
37 #include <sys/lock.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
40 #include <sys/systm.h>
41 #include <sys/time.h>
42 #include <sys/clock.h>
43 #include <sys/disk.h>
44 #include <geom/geom.h>
45 #include <geom/geom_dbg.h>
46 #include "geom/raid/g_raid.h"
47 #include "geom/raid/md_ddf.h"
48 #include "g_raid_md_if.h"
49 
50 static MALLOC_DEFINE(M_MD_DDF, "md_ddf_data", "GEOM_RAID DDF metadata");
51 
52 #define	DDF_MAX_DISKS_HARD	128
53 
54 #define	DDF_MAX_DISKS	16
55 #define	DDF_MAX_VDISKS	7
56 #define	DDF_MAX_PARTITIONS	1
57 
58 #define DECADE (3600*24*(365*10+2))	/* 10 years in seconds. */
59 
60 struct ddf_meta {
61 	u_int	sectorsize;
62 	u_int	bigendian;
63 	struct ddf_header *hdr;
64 	struct ddf_cd_record *cdr;
65 	struct ddf_pd_record *pdr;
66 	struct ddf_vd_record *vdr;
67 	void *cr;
68 	struct ddf_pdd_record *pdd;
69 	struct ddf_bbm_log *bbm;
70 };
71 
72 struct ddf_vol_meta {
73 	u_int	sectorsize;
74 	u_int	bigendian;
75 	struct ddf_header *hdr;
76 	struct ddf_cd_record *cdr;
77 	struct ddf_vd_entry *vde;
78 	struct ddf_vdc_record *vdc;
79 	struct ddf_vdc_record *bvdc[DDF_MAX_DISKS_HARD];
80 };
81 
82 struct g_raid_md_ddf_perdisk {
83 	struct ddf_meta	 pd_meta;
84 };
85 
86 struct g_raid_md_ddf_pervolume {
87 	struct ddf_vol_meta		 pv_meta;
88 	int				 pv_started;
89 	struct callout			 pv_start_co;	/* STARTING state timer. */
90 };
91 
92 struct g_raid_md_ddf_object {
93 	struct g_raid_md_object	 mdio_base;
94 	u_int			 mdio_bigendian;
95 	struct ddf_meta		 mdio_meta;
96 	int			 mdio_starting;
97 	struct callout		 mdio_start_co;	/* STARTING state timer. */
98 	int			 mdio_started;
99 	struct root_hold_token	*mdio_rootmount; /* Root mount delay token. */
100 };
101 
102 static g_raid_md_create_req_t g_raid_md_create_req_ddf;
103 static g_raid_md_taste_t g_raid_md_taste_ddf;
104 static g_raid_md_event_t g_raid_md_event_ddf;
105 static g_raid_md_volume_event_t g_raid_md_volume_event_ddf;
106 static g_raid_md_ctl_t g_raid_md_ctl_ddf;
107 static g_raid_md_write_t g_raid_md_write_ddf;
108 static g_raid_md_fail_disk_t g_raid_md_fail_disk_ddf;
109 static g_raid_md_free_disk_t g_raid_md_free_disk_ddf;
110 static g_raid_md_free_volume_t g_raid_md_free_volume_ddf;
111 static g_raid_md_free_t g_raid_md_free_ddf;
112 
113 static kobj_method_t g_raid_md_ddf_methods[] = {
114 	KOBJMETHOD(g_raid_md_create_req,	g_raid_md_create_req_ddf),
115 	KOBJMETHOD(g_raid_md_taste,	g_raid_md_taste_ddf),
116 	KOBJMETHOD(g_raid_md_event,	g_raid_md_event_ddf),
117 	KOBJMETHOD(g_raid_md_volume_event,	g_raid_md_volume_event_ddf),
118 	KOBJMETHOD(g_raid_md_ctl,	g_raid_md_ctl_ddf),
119 	KOBJMETHOD(g_raid_md_write,	g_raid_md_write_ddf),
120 	KOBJMETHOD(g_raid_md_fail_disk,	g_raid_md_fail_disk_ddf),
121 	KOBJMETHOD(g_raid_md_free_disk,	g_raid_md_free_disk_ddf),
122 	KOBJMETHOD(g_raid_md_free_volume,	g_raid_md_free_volume_ddf),
123 	KOBJMETHOD(g_raid_md_free,	g_raid_md_free_ddf),
124 	{ 0, 0 }
125 };
126 
127 static struct g_raid_md_class g_raid_md_ddf_class = {
128 	"DDF",
129 	g_raid_md_ddf_methods,
130 	sizeof(struct g_raid_md_ddf_object),
131 	.mdc_enable = 1,
132 	.mdc_priority = 100
133 };
134 
135 #define GET8(m, f)	((m)->f)
136 #define GET16(m, f)	((m)->bigendian ? be16dec(&(m)->f) : le16dec(&(m)->f))
137 #define GET32(m, f)	((m)->bigendian ? be32dec(&(m)->f) : le32dec(&(m)->f))
138 #define GET64(m, f)	((m)->bigendian ? be64dec(&(m)->f) : le64dec(&(m)->f))
139 #define GET8D(m, f)	(f)
140 #define GET16D(m, f)	((m)->bigendian ? be16dec(&f) : le16dec(&f))
141 #define GET32D(m, f)	((m)->bigendian ? be32dec(&f) : le32dec(&f))
142 #define GET64D(m, f)	((m)->bigendian ? be64dec(&f) : le64dec(&f))
143 #define GET8P(m, f)	(*(f))
144 #define GET16P(m, f)	((m)->bigendian ? be16dec(f) : le16dec(f))
145 #define GET32P(m, f)	((m)->bigendian ? be32dec(f) : le32dec(f))
146 #define GET64P(m, f)	((m)->bigendian ? be64dec(f) : le64dec(f))
147 
148 #define SET8P(m, f, v)							\
149 	(*(f) = (v))
150 #define SET16P(m, f, v)							\
151 	do {								\
152 		if ((m)->bigendian)					\
153 			be16enc((f), (v));				\
154 		else							\
155 			le16enc((f), (v));				\
156 	} while (0)
157 #define SET32P(m, f, v)							\
158 	do {								\
159 		if ((m)->bigendian)					\
160 			be32enc((f), (v));				\
161 		else							\
162 			le32enc((f), (v));				\
163 	} while (0)
164 #define SET64P(m, f, v)							\
165 	do {								\
166 		if ((m)->bigendian)					\
167 			be64enc((f), (v));				\
168 		else							\
169 			le64enc((f), (v));				\
170 	} while (0)
171 #define SET8(m, f, v)	SET8P((m), &((m)->f), (v))
172 #define SET16(m, f, v)	SET16P((m), &((m)->f), (v))
173 #define SET32(m, f, v)	SET32P((m), &((m)->f), (v))
174 #define SET64(m, f, v)	SET64P((m), &((m)->f), (v))
175 #define SET8D(m, f, v)	SET8P((m), &(f), (v))
176 #define SET16D(m, f, v)	SET16P((m), &(f), (v))
177 #define SET32D(m, f, v)	SET32P((m), &(f), (v))
178 #define SET64D(m, f, v)	SET64P((m), &(f), (v))
179 
180 #define GETCRNUM(m)	(GET32((m), hdr->cr_length) /			\
181 	GET16((m), hdr->Configuration_Record_Length))
182 
183 #define GETVDCPTR(m, n)	((struct ddf_vdc_record *)((uint8_t *)(m)->cr +	\
184 	(n) * GET16((m), hdr->Configuration_Record_Length) *		\
185 	(m)->sectorsize))
186 
187 #define GETSAPTR(m, n)	((struct ddf_sa_record *)((uint8_t *)(m)->cr +	\
188 	(n) * GET16((m), hdr->Configuration_Record_Length) *		\
189 	(m)->sectorsize))
190 
191 static int
192 isff(uint8_t *buf, int size)
193 {
194 	int i;
195 
196 	for (i = 0; i < size; i++)
197 		if (buf[i] != 0xff)
198 			return (0);
199 	return (1);
200 }
201 
202 static void
203 print_guid(uint8_t *buf)
204 {
205 	int i, ascii;
206 
207 	ascii = 1;
208 	for (i = 0; i < 24; i++) {
209 		if (buf[i] != 0 && (buf[i] < ' ' || buf[i] > 127)) {
210 			ascii = 0;
211 			break;
212 		}
213 	}
214 	if (ascii) {
215 		printf("'%.24s'", buf);
216 	} else {
217 		for (i = 0; i < 24; i++)
218 			printf("%02x", buf[i]);
219 	}
220 }
221 
222 static void
223 g_raid_md_ddf_print(struct ddf_meta *meta)
224 {
225 	struct ddf_vdc_record *vdc;
226 	struct ddf_vuc_record *vuc;
227 	struct ddf_sa_record *sa;
228 	uint64_t *val2;
229 	uint32_t val;
230 	int i, j, k, num, num2;
231 
232 	if (g_raid_debug < 1)
233 		return;
234 
235 	printf("********* DDF Metadata *********\n");
236 	printf("**** Header ****\n");
237 	printf("DDF_Header_GUID      ");
238 	print_guid(meta->hdr->DDF_Header_GUID);
239 	printf("\n");
240 	printf("DDF_rev              %8.8s\n", (char *)&meta->hdr->DDF_rev[0]);
241 	printf("Sequence_Number      0x%08x\n", GET32(meta, hdr->Sequence_Number));
242 	printf("TimeStamp            0x%08x\n", GET32(meta, hdr->TimeStamp));
243 	printf("Open_Flag            0x%02x\n", GET16(meta, hdr->Open_Flag));
244 	printf("Foreign_Flag         0x%02x\n", GET16(meta, hdr->Foreign_Flag));
245 	printf("Diskgrouping         0x%02x\n", GET16(meta, hdr->Diskgrouping));
246 	printf("Primary_Header_LBA   %ju\n", GET64(meta, hdr->Primary_Header_LBA));
247 	printf("Secondary_Header_LBA %ju\n", GET64(meta, hdr->Secondary_Header_LBA));
248 	printf("WorkSpace_Length     %u\n", GET32(meta, hdr->WorkSpace_Length));
249 	printf("WorkSpace_LBA        %ju\n", GET64(meta, hdr->WorkSpace_LBA));
250 	printf("Max_PD_Entries       %u\n", GET16(meta, hdr->Max_PD_Entries));
251 	printf("Max_VD_Entries       %u\n", GET16(meta, hdr->Max_VD_Entries));
252 	printf("Max_Partitions       %u\n", GET16(meta, hdr->Max_Partitions));
253 	printf("Configuration_Record_Length %u\n", GET16(meta, hdr->Configuration_Record_Length));
254 	printf("Max_Primary_Element_Entries %u\n", GET16(meta, hdr->Max_Primary_Element_Entries));
255 	printf("Controller Data      %u:%u\n", GET32(meta, hdr->cd_section), GET32(meta, hdr->cd_length));
256 	printf("Physical Disk        %u:%u\n", GET32(meta, hdr->pdr_section), GET32(meta, hdr->pdr_length));
257 	printf("Virtual Disk         %u:%u\n", GET32(meta, hdr->vdr_section), GET32(meta, hdr->vdr_length));
258 	printf("Configuration Recs   %u:%u\n", GET32(meta, hdr->cr_section), GET32(meta, hdr->cr_length));
259 	printf("Physical Disk Recs   %u:%u\n", GET32(meta, hdr->pdd_section), GET32(meta, hdr->pdd_length));
260 	printf("BBM Log              %u:%u\n", GET32(meta, hdr->bbmlog_section), GET32(meta, hdr->bbmlog_length));
261 	printf("Diagnostic Space     %u:%u\n", GET32(meta, hdr->Diagnostic_Space), GET32(meta, hdr->Diagnostic_Space_Length));
262 	printf("Vendor_Specific_Logs %u:%u\n", GET32(meta, hdr->Vendor_Specific_Logs), GET32(meta, hdr->Vendor_Specific_Logs_Length));
263 	printf("**** Controller Data ****\n");
264 	printf("Controller_GUID      ");
265 	print_guid(meta->cdr->Controller_GUID);
266 	printf("\n");
267 	printf("Controller_Type      0x%04x%04x 0x%04x%04x\n",
268 	    GET16(meta, cdr->Controller_Type.Vendor_ID),
269 	    GET16(meta, cdr->Controller_Type.Device_ID),
270 	    GET16(meta, cdr->Controller_Type.SubVendor_ID),
271 	    GET16(meta, cdr->Controller_Type.SubDevice_ID));
272 	printf("Product_ID           '%.16s'\n", (char *)&meta->cdr->Product_ID[0]);
273 	printf("**** Physical Disk Records ****\n");
274 	printf("Populated_PDEs       %u\n", GET16(meta, pdr->Populated_PDEs));
275 	printf("Max_PDE_Supported    %u\n", GET16(meta, pdr->Max_PDE_Supported));
276 	for (j = 0; j < GET16(meta, pdr->Populated_PDEs); j++) {
277 		if (isff(meta->pdr->entry[j].PD_GUID, 24))
278 			continue;
279 		if (GET32(meta, pdr->entry[j].PD_Reference) == 0xffffffff)
280 			continue;
281 		printf("PD_GUID              ");
282 		print_guid(meta->pdr->entry[j].PD_GUID);
283 		printf("\n");
284 		printf("PD_Reference         0x%08x\n",
285 		    GET32(meta, pdr->entry[j].PD_Reference));
286 		printf("PD_Type              0x%04x\n",
287 		    GET16(meta, pdr->entry[j].PD_Type));
288 		printf("PD_State             0x%04x\n",
289 		    GET16(meta, pdr->entry[j].PD_State));
290 		printf("Configured_Size      %ju\n",
291 		    GET64(meta, pdr->entry[j].Configured_Size));
292 		printf("Block_Size           %u\n",
293 		    GET16(meta, pdr->entry[j].Block_Size));
294 	}
295 	printf("**** Virtual Disk Records ****\n");
296 	printf("Populated_VDEs       %u\n", GET16(meta, vdr->Populated_VDEs));
297 	printf("Max_VDE_Supported    %u\n", GET16(meta, vdr->Max_VDE_Supported));
298 	for (j = 0; j < GET16(meta, vdr->Populated_VDEs); j++) {
299 		if (isff(meta->vdr->entry[j].VD_GUID, 24))
300 			continue;
301 		printf("VD_GUID              ");
302 		print_guid(meta->vdr->entry[j].VD_GUID);
303 		printf("\n");
304 		printf("VD_Number            0x%04x\n",
305 		    GET16(meta, vdr->entry[j].VD_Number));
306 		printf("VD_Type              0x%04x\n",
307 		    GET16(meta, vdr->entry[j].VD_Type));
308 		printf("VD_State             0x%02x\n",
309 		    GET8(meta, vdr->entry[j].VD_State));
310 		printf("Init_State           0x%02x\n",
311 		    GET8(meta, vdr->entry[j].Init_State));
312 		printf("Drive_Failures_Remaining %u\n",
313 		    GET8(meta, vdr->entry[j].Drive_Failures_Remaining));
314 		printf("VD_Name              '%.16s'\n",
315 		    (char *)&meta->vdr->entry[j].VD_Name);
316 	}
317 	printf("**** Configuration Records ****\n");
318 	num = GETCRNUM(meta);
319 	for (j = 0; j < num; j++) {
320 		vdc = GETVDCPTR(meta, j);
321 		val = GET32D(meta, vdc->Signature);
322 		switch (val) {
323 		case DDF_VDCR_SIGNATURE:
324 			printf("** Virtual Disk Configuration **\n");
325 			printf("VD_GUID              ");
326 			print_guid(vdc->VD_GUID);
327 			printf("\n");
328 			printf("Timestamp            0x%08x\n",
329 			    GET32D(meta, vdc->Timestamp));
330 			printf("Sequence_Number      0x%08x\n",
331 			    GET32D(meta, vdc->Sequence_Number));
332 			printf("Primary_Element_Count %u\n",
333 			    GET16D(meta, vdc->Primary_Element_Count));
334 			printf("Stripe_Size          %u\n",
335 			    GET8D(meta, vdc->Stripe_Size));
336 			printf("Primary_RAID_Level   0x%02x\n",
337 			    GET8D(meta, vdc->Primary_RAID_Level));
338 			printf("RLQ                  0x%02x\n",
339 			    GET8D(meta, vdc->RLQ));
340 			printf("Secondary_Element_Count %u\n",
341 			    GET8D(meta, vdc->Secondary_Element_Count));
342 			printf("Secondary_Element_Seq %u\n",
343 			    GET8D(meta, vdc->Secondary_Element_Seq));
344 			printf("Secondary_RAID_Level 0x%02x\n",
345 			    GET8D(meta, vdc->Secondary_RAID_Level));
346 			printf("Block_Count          %ju\n",
347 			    GET64D(meta, vdc->Block_Count));
348 			printf("VD_Size              %ju\n",
349 			    GET64D(meta, vdc->VD_Size));
350 			printf("Block_Size           %u\n",
351 			    GET16D(meta, vdc->Block_Size));
352 			printf("Rotate_Parity_count  %u\n",
353 			    GET8D(meta, vdc->Rotate_Parity_count));
354 			printf("Associated_Spare_Disks");
355 			for (i = 0; i < 8; i++) {
356 				if (GET32D(meta, vdc->Associated_Spares[i]) != 0xffffffff)
357 					printf(" 0x%08x", GET32D(meta, vdc->Associated_Spares[i]));
358 			}
359 			printf("\n");
360 			printf("Cache_Flags          %016jx\n",
361 			    GET64D(meta, vdc->Cache_Flags));
362 			printf("BG_Rate              %u\n",
363 			    GET8D(meta, vdc->BG_Rate));
364 			printf("MDF_Parity_Disks     %u\n",
365 			    GET8D(meta, vdc->MDF_Parity_Disks));
366 			printf("MDF_Parity_Generator_Polynomial 0x%04x\n",
367 			    GET16D(meta, vdc->MDF_Parity_Generator_Polynomial));
368 			printf("MDF_Constant_Generation_Method 0x%02x\n",
369 			    GET8D(meta, vdc->MDF_Constant_Generation_Method));
370 			printf("Physical_Disks      ");
371 			num2 = GET16D(meta, vdc->Primary_Element_Count);
372 			val2 = (uint64_t *)&(vdc->Physical_Disk_Sequence[GET16(meta, hdr->Max_Primary_Element_Entries)]);
373 			for (i = 0; i < num2; i++)
374 				printf(" 0x%08x @ %ju",
375 				    GET32D(meta, vdc->Physical_Disk_Sequence[i]),
376 				    GET64P(meta, val2 + i));
377 			printf("\n");
378 			break;
379 		case DDF_VUCR_SIGNATURE:
380 			printf("** Vendor Unique Configuration **\n");
381 			vuc = (struct ddf_vuc_record *)vdc;
382 			printf("VD_GUID              ");
383 			print_guid(vuc->VD_GUID);
384 			printf("\n");
385 			break;
386 		case DDF_SA_SIGNATURE:
387 			printf("** Spare Assignment Configuration **\n");
388 			sa = (struct ddf_sa_record *)vdc;
389 			printf("Timestamp            0x%08x\n",
390 			    GET32D(meta, sa->Timestamp));
391 			printf("Spare_Type           0x%02x\n",
392 			    GET8D(meta, sa->Spare_Type));
393 			printf("Populated_SAEs       %u\n",
394 			    GET16D(meta, sa->Populated_SAEs));
395 			printf("MAX_SAE_Supported    %u\n",
396 			    GET16D(meta, sa->MAX_SAE_Supported));
397 			for (i = 0; i < GET16D(meta, sa->Populated_SAEs); i++) {
398 				if (isff(sa->entry[i].VD_GUID, 24))
399 					continue;
400 				printf("VD_GUID             ");
401 				for (k = 0; k < 24; k++)
402 					printf("%02x", sa->entry[i].VD_GUID[k]);
403 				printf("\n");
404 				printf("Secondary_Element   %u\n",
405 				    GET16D(meta, sa->entry[i].Secondary_Element));
406 			}
407 			break;
408 		case 0x00000000:
409 		case 0xFFFFFFFF:
410 			break;
411 		default:
412 			printf("Unknown configuration signature %08x\n", val);
413 			break;
414 		}
415 	}
416 	printf("**** Physical Disk Data ****\n");
417 	printf("PD_GUID              ");
418 	print_guid(meta->pdd->PD_GUID);
419 	printf("\n");
420 	printf("PD_Reference         0x%08x\n",
421 	    GET32(meta, pdd->PD_Reference));
422 	printf("Forced_Ref_Flag      0x%02x\n",
423 	    GET8(meta, pdd->Forced_Ref_Flag));
424 	printf("Forced_PD_GUID_Flag  0x%02x\n",
425 	    GET8(meta, pdd->Forced_PD_GUID_Flag));
426 }
427 
428 static int
429 ddf_meta_find_pd(struct ddf_meta *meta, uint8_t *GUID, uint32_t PD_Reference)
430 {
431 	int i;
432 
433 	for (i = 0; i < GET16(meta, pdr->Populated_PDEs); i++) {
434 		if (GUID != NULL) {
435 			if (memcmp(meta->pdr->entry[i].PD_GUID, GUID, 24) == 0)
436 				return (i);
437 		} else if (PD_Reference != 0xffffffff) {
438 			if (GET32(meta, pdr->entry[i].PD_Reference) == PD_Reference)
439 				return (i);
440 		} else
441 			if (isff(meta->pdr->entry[i].PD_GUID, 24))
442 				return (i);
443 	}
444 	if (GUID == NULL && PD_Reference == 0xffffffff) {
445 		if (i >= GET16(meta, pdr->Max_PDE_Supported))
446 			return (-1);
447 		SET16(meta, pdr->Populated_PDEs, i + 1);
448 		return (i);
449 	}
450 	return (-1);
451 }
452 
453 static int
454 ddf_meta_find_vd(struct ddf_meta *meta, uint8_t *GUID)
455 {
456 	int i;
457 
458 	for (i = 0; i < GET16(meta, vdr->Populated_VDEs); i++) {
459 		if (GUID != NULL) {
460 			if (memcmp(meta->vdr->entry[i].VD_GUID, GUID, 24) == 0)
461 				return (i);
462 		} else
463 			if (isff(meta->vdr->entry[i].VD_GUID, 24))
464 				return (i);
465 	}
466 	if (GUID == NULL) {
467 		if (i >= GET16(meta, vdr->Max_VDE_Supported))
468 			return (-1);
469 		SET16(meta, vdr->Populated_VDEs, i + 1);
470 		return (i);
471 	}
472 	return (-1);
473 }
474 
475 static struct ddf_vdc_record *
476 ddf_meta_find_vdc(struct ddf_meta *meta, uint8_t *GUID)
477 {
478 	struct ddf_vdc_record *vdc;
479 	int i, num;
480 
481 	num = GETCRNUM(meta);
482 	for (i = 0; i < num; i++) {
483 		vdc = GETVDCPTR(meta, i);
484 		if (GUID != NULL) {
485 			if (GET32D(meta, vdc->Signature) == DDF_VDCR_SIGNATURE &&
486 			    memcmp(vdc->VD_GUID, GUID, 24) == 0)
487 				return (vdc);
488 		} else
489 			if (GET32D(meta, vdc->Signature) == 0xffffffff ||
490 			    GET32D(meta, vdc->Signature) == 0)
491 				return (vdc);
492 	}
493 	return (NULL);
494 }
495 
496 static int
497 ddf_meta_count_vdc(struct ddf_meta *meta, uint8_t *GUID)
498 {
499 	struct ddf_vdc_record *vdc;
500 	int i, num, cnt;
501 
502 	cnt = 0;
503 	num = GETCRNUM(meta);
504 	for (i = 0; i < num; i++) {
505 		vdc = GETVDCPTR(meta, i);
506 		if (GET32D(meta, vdc->Signature) != DDF_VDCR_SIGNATURE)
507 			continue;
508 		if (GUID == NULL || memcmp(vdc->VD_GUID, GUID, 24) == 0)
509 			cnt++;
510 	}
511 	return (cnt);
512 }
513 
514 static int
515 ddf_meta_find_disk(struct ddf_vol_meta *vmeta, uint32_t PD_Reference,
516     int *bvdp, int *posp)
517 {
518 	int i, bvd, pos;
519 
520 	i = 0;
521 	for (bvd = 0; bvd < GET8(vmeta, vdc->Secondary_Element_Count); bvd++) {
522 		if (vmeta->bvdc[bvd] == NULL) {
523 			i += GET16(vmeta, vdc->Primary_Element_Count); // XXX
524 			continue;
525 		}
526 		for (pos = 0; pos < GET16(vmeta, bvdc[bvd]->Primary_Element_Count);
527 		    pos++, i++) {
528 			if (GET32(vmeta, bvdc[bvd]->Physical_Disk_Sequence[pos]) ==
529 			    PD_Reference) {
530 				if (bvdp != NULL)
531 					*bvdp = bvd;
532 				if (posp != NULL)
533 					*posp = pos;
534 				return (i);
535 			}
536 		}
537 	}
538 	return (-1);
539 }
540 
541 static struct ddf_sa_record *
542 ddf_meta_find_sa(struct ddf_meta *meta, int create)
543 {
544 	struct ddf_sa_record *sa;
545 	int i, num;
546 
547 	num = GETCRNUM(meta);
548 	for (i = 0; i < num; i++) {
549 		sa = GETSAPTR(meta, i);
550 		if (GET32D(meta, sa->Signature) == DDF_SA_SIGNATURE)
551 			return (sa);
552 	}
553 	if (create) {
554 		for (i = 0; i < num; i++) {
555 			sa = GETSAPTR(meta, i);
556 			if (GET32D(meta, sa->Signature) == 0xffffffff ||
557 			    GET32D(meta, sa->Signature) == 0)
558 				return (sa);
559 		}
560 	}
561 	return (NULL);
562 }
563 
564 static void
565 ddf_meta_create(struct g_raid_disk *disk, struct ddf_meta *sample)
566 {
567 	struct timespec ts;
568 	struct clocktime ct;
569 	struct g_raid_md_ddf_perdisk *pd;
570 	struct g_raid_md_ddf_object *mdi;
571 	struct ddf_meta *meta;
572 	struct ddf_pd_entry *pde;
573 	off_t anchorlba;
574 	u_int ss, pos, size;
575 	int len, error;
576 	char serial_buffer[DISK_IDENT_SIZE];
577 
578 	if (sample->hdr == NULL)
579 		sample = NULL;
580 
581 	mdi = (struct g_raid_md_ddf_object *)disk->d_softc->sc_md;
582 	pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
583 	meta = &pd->pd_meta;
584 	ss = disk->d_consumer->provider->sectorsize;
585 	anchorlba = disk->d_consumer->provider->mediasize / ss - 1;
586 
587 	meta->sectorsize = ss;
588 	meta->bigendian = sample ? sample->bigendian : mdi->mdio_bigendian;
589 	getnanotime(&ts);
590 	clock_ts_to_ct(&ts, &ct);
591 
592 	/* Header */
593 	meta->hdr = malloc(ss, M_MD_DDF, M_WAITOK);
594 	memset(meta->hdr, 0xff, ss);
595 	if (sample) {
596 		memcpy(meta->hdr, sample->hdr, sizeof(struct ddf_header));
597 		if (ss != sample->sectorsize) {
598 			SET32(meta, hdr->WorkSpace_Length,
599 			    howmany(GET32(sample, hdr->WorkSpace_Length) *
600 			        sample->sectorsize, ss));
601 			SET16(meta, hdr->Configuration_Record_Length,
602 			    howmany(GET16(sample,
603 			        hdr->Configuration_Record_Length) *
604 				sample->sectorsize, ss));
605 			SET32(meta, hdr->cd_length,
606 			    howmany(GET32(sample, hdr->cd_length) *
607 			        sample->sectorsize, ss));
608 			SET32(meta, hdr->pdr_length,
609 			    howmany(GET32(sample, hdr->pdr_length) *
610 			        sample->sectorsize, ss));
611 			SET32(meta, hdr->vdr_length,
612 			    howmany(GET32(sample, hdr->vdr_length) *
613 			        sample->sectorsize, ss));
614 			SET32(meta, hdr->cr_length,
615 			    howmany(GET32(sample, hdr->cr_length) *
616 			        sample->sectorsize, ss));
617 			SET32(meta, hdr->pdd_length,
618 			    howmany(GET32(sample, hdr->pdd_length) *
619 			        sample->sectorsize, ss));
620 			SET32(meta, hdr->bbmlog_length,
621 			    howmany(GET32(sample, hdr->bbmlog_length) *
622 			        sample->sectorsize, ss));
623 			SET32(meta, hdr->Diagnostic_Space,
624 			    howmany(GET32(sample, hdr->bbmlog_length) *
625 			        sample->sectorsize, ss));
626 			SET32(meta, hdr->Vendor_Specific_Logs,
627 			    howmany(GET32(sample, hdr->bbmlog_length) *
628 			        sample->sectorsize, ss));
629 		}
630 	} else {
631 		SET32(meta, hdr->Signature, DDF_HEADER_SIGNATURE);
632 		snprintf(meta->hdr->DDF_Header_GUID, 25, "FreeBSD %08x%08x",
633 		    (u_int)(ts.tv_sec - DECADE), arc4random());
634 		memcpy(meta->hdr->DDF_rev, "02.00.00", 8);
635 		SET32(meta, hdr->TimeStamp, (ts.tv_sec - DECADE));
636 		SET32(meta, hdr->WorkSpace_Length, 16 * 1024 * 1024 / ss);
637 		SET16(meta, hdr->Max_PD_Entries, DDF_MAX_DISKS - 1);
638 		SET16(meta, hdr->Max_VD_Entries, DDF_MAX_VDISKS);
639 		SET16(meta, hdr->Max_Partitions, DDF_MAX_PARTITIONS);
640 		SET16(meta, hdr->Max_Primary_Element_Entries, DDF_MAX_DISKS);
641 		SET16(meta, hdr->Configuration_Record_Length,
642 		    howmany(sizeof(struct ddf_vdc_record) + (4 + 8) *
643 		        GET16(meta, hdr->Max_Primary_Element_Entries), ss));
644 		SET32(meta, hdr->cd_length,
645 		    howmany(sizeof(struct ddf_cd_record), ss));
646 		SET32(meta, hdr->pdr_length,
647 		    howmany(sizeof(struct ddf_pd_record) +
648 		        sizeof(struct ddf_pd_entry) * GET16(meta,
649 			hdr->Max_PD_Entries), ss));
650 		SET32(meta, hdr->vdr_length,
651 		    howmany(sizeof(struct ddf_vd_record) +
652 		        sizeof(struct ddf_vd_entry) *
653 			GET16(meta, hdr->Max_VD_Entries), ss));
654 		SET32(meta, hdr->cr_length,
655 		    GET16(meta, hdr->Configuration_Record_Length) *
656 		    (GET16(meta, hdr->Max_Partitions) + 1));
657 		SET32(meta, hdr->pdd_length,
658 		    howmany(sizeof(struct ddf_pdd_record), ss));
659 		SET32(meta, hdr->bbmlog_length, 0);
660 		SET32(meta, hdr->Diagnostic_Space_Length, 0);
661 		SET32(meta, hdr->Vendor_Specific_Logs_Length, 0);
662 	}
663 	pos = 1;
664 	SET32(meta, hdr->cd_section, pos);
665 	pos += GET32(meta, hdr->cd_length);
666 	SET32(meta, hdr->pdr_section, pos);
667 	pos += GET32(meta, hdr->pdr_length);
668 	SET32(meta, hdr->vdr_section, pos);
669 	pos += GET32(meta, hdr->vdr_length);
670 	SET32(meta, hdr->cr_section, pos);
671 	pos += GET32(meta, hdr->cr_length);
672 	SET32(meta, hdr->pdd_section, pos);
673 	pos += GET32(meta, hdr->pdd_length);
674 	SET32(meta, hdr->bbmlog_section,
675 	    GET32(meta, hdr->bbmlog_length) != 0 ? pos : 0xffffffff);
676 	pos += GET32(meta, hdr->bbmlog_length);
677 	SET32(meta, hdr->Diagnostic_Space,
678 	    GET32(meta, hdr->Diagnostic_Space_Length) != 0 ? pos : 0xffffffff);
679 	pos += GET32(meta, hdr->Diagnostic_Space_Length);
680 	SET32(meta, hdr->Vendor_Specific_Logs,
681 	    GET32(meta, hdr->Vendor_Specific_Logs_Length) != 0 ? pos : 0xffffffff);
682 	pos += min(GET32(meta, hdr->Vendor_Specific_Logs_Length), 1);
683 	SET64(meta, hdr->Primary_Header_LBA,
684 	    anchorlba - pos);
685 	SET64(meta, hdr->Secondary_Header_LBA,
686 	    0xffffffffffffffffULL);
687 	SET64(meta, hdr->WorkSpace_LBA,
688 	    anchorlba + 1 - 32 * 1024 * 1024 / ss);
689 
690 	/* Controller Data */
691 	size = GET32(meta, hdr->cd_length) * ss;
692 	meta->cdr = malloc(size, M_MD_DDF, M_WAITOK);
693 	memset(meta->cdr, 0xff, size);
694 	SET32(meta, cdr->Signature, DDF_CONTROLLER_DATA_SIGNATURE);
695 	memcpy(meta->cdr->Controller_GUID, "FreeBSD GEOM RAID SERIAL", 24);
696 	memcpy(meta->cdr->Product_ID, "FreeBSD GEOMRAID", 16);
697 
698 	/* Physical Drive Records. */
699 	size = GET32(meta, hdr->pdr_length) * ss;
700 	meta->pdr = malloc(size, M_MD_DDF, M_WAITOK);
701 	memset(meta->pdr, 0xff, size);
702 	SET32(meta, pdr->Signature, DDF_PDR_SIGNATURE);
703 	SET16(meta, pdr->Populated_PDEs, 1);
704 	SET16(meta, pdr->Max_PDE_Supported,
705 	    GET16(meta, hdr->Max_PD_Entries));
706 
707 	pde = &meta->pdr->entry[0];
708 	len = sizeof(serial_buffer);
709 	error = g_io_getattr("GEOM::ident", disk->d_consumer, &len, serial_buffer);
710 	if (error == 0 && (len = strlen (serial_buffer)) >= 6 && len <= 20)
711 		snprintf(pde->PD_GUID, 25, "DISK%20s", serial_buffer);
712 	else
713 		snprintf(pde->PD_GUID, 25, "DISK%04d%02d%02d%08x%04x",
714 		    ct.year, ct.mon, ct.day,
715 		    arc4random(), arc4random() & 0xffff);
716 	SET32D(meta, pde->PD_Reference, arc4random());
717 	SET16D(meta, pde->PD_Type, DDF_PDE_GUID_FORCE);
718 	SET16D(meta, pde->PD_State, 0);
719 	SET64D(meta, pde->Configured_Size,
720 	    anchorlba + 1 - 32 * 1024 * 1024 / ss);
721 	SET16D(meta, pde->Block_Size, ss);
722 
723 	/* Virtual Drive Records. */
724 	size = GET32(meta, hdr->vdr_length) * ss;
725 	meta->vdr = malloc(size, M_MD_DDF, M_WAITOK);
726 	memset(meta->vdr, 0xff, size);
727 	SET32(meta, vdr->Signature, DDF_VD_RECORD_SIGNATURE);
728 	SET32(meta, vdr->Populated_VDEs, 0);
729 	SET16(meta, vdr->Max_VDE_Supported,
730 	    GET16(meta, hdr->Max_VD_Entries));
731 
732 	/* Configuration Records. */
733 	size = GET32(meta, hdr->cr_length) * ss;
734 	meta->cr = malloc(size, M_MD_DDF, M_WAITOK);
735 	memset(meta->cr, 0xff, size);
736 
737 	/* Physical Disk Data. */
738 	size = GET32(meta, hdr->pdd_length) * ss;
739 	meta->pdd = malloc(size, M_MD_DDF, M_WAITOK);
740 	memset(meta->pdd, 0xff, size);
741 	SET32(meta, pdd->Signature, DDF_PDD_SIGNATURE);
742 	memcpy(meta->pdd->PD_GUID, pde->PD_GUID, 24);
743 	SET32(meta, pdd->PD_Reference, GET32D(meta, pde->PD_Reference));
744 	SET8(meta, pdd->Forced_Ref_Flag, DDF_PDD_FORCED_REF);
745 	SET8(meta, pdd->Forced_PD_GUID_Flag, DDF_PDD_FORCED_GUID);
746 
747 	/* Bad Block Management Log. */
748 	if (GET32(meta, hdr->bbmlog_length) != 0) {
749 		size = GET32(meta, hdr->bbmlog_length) * ss;
750 		meta->bbm = malloc(size, M_MD_DDF, M_WAITOK);
751 		memset(meta->bbm, 0xff, size);
752 		SET32(meta, bbm->Signature, DDF_BBML_SIGNATURE);
753 		SET32(meta, bbm->Entry_Count, 0);
754 		SET32(meta, bbm->Spare_Block_Count, 0);
755 	}
756 }
757 
758 static void
759 ddf_meta_copy(struct ddf_meta *dst, struct ddf_meta *src)
760 {
761 	u_int ss;
762 
763 	dst->bigendian = src->bigendian;
764 	ss = dst->sectorsize = src->sectorsize;
765 	dst->hdr = malloc(ss, M_MD_DDF, M_WAITOK);
766 	memcpy(dst->hdr, src->hdr, ss);
767 	dst->cdr = malloc(GET32(src, hdr->cd_length) * ss, M_MD_DDF, M_WAITOK);
768 	memcpy(dst->cdr, src->cdr, GET32(src, hdr->cd_length) * ss);
769 	dst->pdr = malloc(GET32(src, hdr->pdr_length) * ss, M_MD_DDF, M_WAITOK);
770 	memcpy(dst->pdr, src->pdr, GET32(src, hdr->pdr_length) * ss);
771 	dst->vdr = malloc(GET32(src, hdr->vdr_length) * ss, M_MD_DDF, M_WAITOK);
772 	memcpy(dst->vdr, src->vdr, GET32(src, hdr->vdr_length) * ss);
773 	dst->cr = malloc(GET32(src, hdr->cr_length) * ss, M_MD_DDF, M_WAITOK);
774 	memcpy(dst->cr, src->cr, GET32(src, hdr->cr_length) * ss);
775 	dst->pdd = malloc(GET32(src, hdr->pdd_length) * ss, M_MD_DDF, M_WAITOK);
776 	memcpy(dst->pdd, src->pdd, GET32(src, hdr->pdd_length) * ss);
777 	if (src->bbm != NULL) {
778 		dst->bbm = malloc(GET32(src, hdr->bbmlog_length) * ss, M_MD_DDF, M_WAITOK);
779 		memcpy(dst->bbm, src->bbm, GET32(src, hdr->bbmlog_length) * ss);
780 	}
781 }
782 
783 static void
784 ddf_meta_update(struct ddf_meta *meta, struct ddf_meta *src)
785 {
786 	struct ddf_pd_entry *pde, *spde;
787 	int i, j;
788 
789 	for (i = 0; i < GET16(src, pdr->Populated_PDEs); i++) {
790 		spde = &src->pdr->entry[i];
791 		if (isff(spde->PD_GUID, 24))
792 			continue;
793 		j = ddf_meta_find_pd(meta, NULL,
794 		    GET32(src, pdr->entry[i].PD_Reference));
795 		if (j < 0) {
796 			j = ddf_meta_find_pd(meta, NULL, 0xffffffff);
797 			pde = &meta->pdr->entry[j];
798 			memcpy(pde, spde, sizeof(*pde));
799 		} else {
800 			pde = &meta->pdr->entry[j];
801 			SET16D(meta, pde->PD_State,
802 			    GET16D(meta, pde->PD_State) |
803 			    GET16D(src, pde->PD_State));
804 		}
805 	}
806 }
807 
808 static void
809 ddf_meta_free(struct ddf_meta *meta)
810 {
811 
812 	if (meta->hdr != NULL) {
813 		free(meta->hdr, M_MD_DDF);
814 		meta->hdr = NULL;
815 	}
816 	if (meta->cdr != NULL) {
817 		free(meta->cdr, M_MD_DDF);
818 		meta->cdr = NULL;
819 	}
820 	if (meta->pdr != NULL) {
821 		free(meta->pdr, M_MD_DDF);
822 		meta->pdr = NULL;
823 	}
824 	if (meta->vdr != NULL) {
825 		free(meta->vdr, M_MD_DDF);
826 		meta->vdr = NULL;
827 	}
828 	if (meta->cr != NULL) {
829 		free(meta->cr, M_MD_DDF);
830 		meta->cr = NULL;
831 	}
832 	if (meta->pdd != NULL) {
833 		free(meta->pdd, M_MD_DDF);
834 		meta->pdd = NULL;
835 	}
836 	if (meta->bbm != NULL) {
837 		free(meta->bbm, M_MD_DDF);
838 		meta->bbm = NULL;
839 	}
840 }
841 
842 static void
843 ddf_vol_meta_create(struct ddf_vol_meta *meta, struct ddf_meta *sample)
844 {
845 	struct timespec ts;
846 	struct clocktime ct;
847 	u_int ss, size;
848 
849 	meta->bigendian = sample->bigendian;
850 	ss = meta->sectorsize = sample->sectorsize;
851 	meta->hdr = malloc(ss, M_MD_DDF, M_WAITOK);
852 	memcpy(meta->hdr, sample->hdr, ss);
853 	meta->cdr = malloc(GET32(sample, hdr->cd_length) * ss, M_MD_DDF, M_WAITOK);
854 	memcpy(meta->cdr, sample->cdr, GET32(sample, hdr->cd_length) * ss);
855 	meta->vde = malloc(sizeof(struct ddf_vd_entry), M_MD_DDF, M_WAITOK);
856 	memset(meta->vde, 0xff, sizeof(struct ddf_vd_entry));
857 	getnanotime(&ts);
858 	clock_ts_to_ct(&ts, &ct);
859 	snprintf(meta->vde->VD_GUID, 25, "FreeBSD%04d%02d%02d%08x%01x",
860 	    ct.year, ct.mon, ct.day,
861 	    arc4random(), arc4random() & 0xf);
862 	size = GET16(sample, hdr->Configuration_Record_Length) * ss;
863 	meta->vdc = malloc(size, M_MD_DDF, M_WAITOK);
864 	memset(meta->vdc, 0xff, size);
865 	SET32(meta, vdc->Signature, DDF_VDCR_SIGNATURE);
866 	memcpy(meta->vdc->VD_GUID, meta->vde->VD_GUID, 24);
867 	SET32(meta, vdc->Sequence_Number, 0);
868 }
869 
870 static void
871 ddf_vol_meta_update(struct ddf_vol_meta *dst, struct ddf_meta *src,
872     uint8_t *GUID, int started)
873 {
874 	struct ddf_vd_entry *vde;
875 	struct ddf_vdc_record *vdc;
876 	int vnew, bvnew, bvd, size;
877 	u_int ss;
878 
879 	vde = &src->vdr->entry[ddf_meta_find_vd(src, GUID)];
880 	vdc = ddf_meta_find_vdc(src, GUID);
881 	if (GET8D(src, vdc->Secondary_Element_Count) == 1)
882 		bvd = 0;
883 	else
884 		bvd = GET8D(src, vdc->Secondary_Element_Seq);
885 	size = GET16(src, hdr->Configuration_Record_Length) * src->sectorsize;
886 
887 	if (dst->vdc == NULL ||
888 	    (!started && ((int32_t)(GET32D(src, vdc->Sequence_Number) -
889 	    GET32(dst, vdc->Sequence_Number))) > 0))
890 		vnew = 1;
891 	else
892 		vnew = 0;
893 
894 	if (dst->bvdc[bvd] == NULL ||
895 	    (!started && ((int32_t)(GET32D(src, vdc->Sequence_Number) -
896 	    GET32(dst, bvdc[bvd]->Sequence_Number))) > 0))
897 		bvnew = 1;
898 	else
899 		bvnew = 0;
900 
901 	if (vnew) {
902 		dst->bigendian = src->bigendian;
903 		ss = dst->sectorsize = src->sectorsize;
904 		if (dst->hdr != NULL)
905 			free(dst->hdr, M_MD_DDF);
906 		dst->hdr = malloc(ss, M_MD_DDF, M_WAITOK);
907 		memcpy(dst->hdr, src->hdr, ss);
908 		if (dst->cdr != NULL)
909 			free(dst->cdr, M_MD_DDF);
910 		dst->cdr = malloc(GET32(src, hdr->cd_length) * ss, M_MD_DDF, M_WAITOK);
911 		memcpy(dst->cdr, src->cdr, GET32(src, hdr->cd_length) * ss);
912 		if (dst->vde != NULL)
913 			free(dst->vde, M_MD_DDF);
914 		dst->vde = malloc(sizeof(struct ddf_vd_entry), M_MD_DDF, M_WAITOK);
915 		memcpy(dst->vde, vde, sizeof(struct ddf_vd_entry));
916 		if (dst->vdc != NULL)
917 			free(dst->vdc, M_MD_DDF);
918 		dst->vdc = malloc(size, M_MD_DDF, M_WAITOK);
919 		memcpy(dst->vdc, vdc, size);
920 	}
921 	if (bvnew) {
922 		if (dst->bvdc[bvd] != NULL)
923 			free(dst->bvdc[bvd], M_MD_DDF);
924 		dst->bvdc[bvd] = malloc(size, M_MD_DDF, M_WAITOK);
925 		memcpy(dst->bvdc[bvd], vdc, size);
926 	}
927 }
928 
929 static void
930 ddf_vol_meta_free(struct ddf_vol_meta *meta)
931 {
932 	int i;
933 
934 	if (meta->hdr != NULL) {
935 		free(meta->hdr, M_MD_DDF);
936 		meta->hdr = NULL;
937 	}
938 	if (meta->cdr != NULL) {
939 		free(meta->cdr, M_MD_DDF);
940 		meta->cdr = NULL;
941 	}
942 	if (meta->vde != NULL) {
943 		free(meta->vde, M_MD_DDF);
944 		meta->vde = NULL;
945 	}
946 	if (meta->vdc != NULL) {
947 		free(meta->vdc, M_MD_DDF);
948 		meta->vdc = NULL;
949 	}
950 	for (i = 0; i < DDF_MAX_DISKS_HARD; i++) {
951 		if (meta->bvdc[i] != NULL) {
952 			free(meta->bvdc[i], M_MD_DDF);
953 			meta->bvdc[i] = NULL;
954 		}
955 	}
956 }
957 
958 static int
959 ddf_meta_unused_range(struct ddf_meta *meta, off_t *off, off_t *size)
960 {
961 	struct ddf_vdc_record *vdc;
962 	off_t beg[32], end[32], beg1, end1;
963 	uint64_t *offp;
964 	int i, j, n, num, pos;
965 	uint32_t ref;
966 
967 	*off = 0;
968 	*size = 0;
969 	ref = GET32(meta, pdd->PD_Reference);
970 	pos = ddf_meta_find_pd(meta, NULL, ref);
971 	beg[0] = 0;
972 	end[0] = GET64(meta, pdr->entry[pos].Configured_Size);
973 	n = 1;
974 	num = GETCRNUM(meta);
975 	for (i = 0; i < num; i++) {
976 		vdc = GETVDCPTR(meta, i);
977 		if (GET32D(meta, vdc->Signature) != DDF_VDCR_SIGNATURE)
978 			continue;
979 		for (pos = 0; pos < GET16D(meta, vdc->Primary_Element_Count); pos++)
980 			if (GET32D(meta, vdc->Physical_Disk_Sequence[pos]) == ref)
981 				break;
982 		if (pos == GET16D(meta, vdc->Primary_Element_Count))
983 			continue;
984 		offp = (uint64_t *)&(vdc->Physical_Disk_Sequence[
985 		    GET16(meta, hdr->Max_Primary_Element_Entries)]);
986 		beg1 = GET64P(meta, offp + pos);
987 		end1 = beg1 + GET64D(meta, vdc->Block_Count);
988 		for (j = 0; j < n; j++) {
989 			if (beg[j] >= end1 || end[j] <= beg1 )
990 				continue;
991 			if (beg[j] < beg1 && end[j] > end1) {
992 				beg[n] = end1;
993 				end[n] = end[j];
994 				end[j] = beg1;
995 				n++;
996 			} else if (beg[j] < beg1)
997 				end[j] = beg1;
998 			else
999 				beg[j] = end1;
1000 		}
1001 	}
1002 	for (j = 0; j < n; j++) {
1003 		if (end[j] - beg[j] > *size) {
1004 			*off = beg[j];
1005 			*size = end[j] - beg[j];
1006 		}
1007 	}
1008 	return ((*size > 0) ? 1 : 0);
1009 }
1010 
1011 static void
1012 ddf_meta_get_name(struct ddf_meta *meta, int num, char *buf)
1013 {
1014 	const char *b;
1015 	int i;
1016 
1017 	b = meta->vdr->entry[num].VD_Name;
1018 	for (i = 15; i >= 0; i--)
1019 		if (b[i] != 0x20)
1020 			break;
1021 	memcpy(buf, b, i + 1);
1022 	buf[i + 1] = 0;
1023 }
1024 
1025 static void
1026 ddf_meta_put_name(struct ddf_vol_meta *meta, char *buf)
1027 {
1028 	int len;
1029 
1030 	len = min(strlen(buf), 16);
1031 	memset(meta->vde->VD_Name, 0x20, 16);
1032 	memcpy(meta->vde->VD_Name, buf, len);
1033 }
1034 
1035 static int
1036 ddf_meta_read(struct g_consumer *cp, struct ddf_meta *meta)
1037 {
1038 	struct g_provider *pp;
1039 	struct ddf_header *ahdr, *hdr;
1040 	char *abuf, *buf;
1041 	off_t plba, slba, lba;
1042 	int error, len, i;
1043 	u_int ss;
1044 	uint32_t val;
1045 
1046 	ddf_meta_free(meta);
1047 
1048 	pp = cp->provider;
1049 	ss = meta->sectorsize = pp->sectorsize;
1050 	if (ss < sizeof(*hdr))
1051 		return (ENXIO);
1052 	/* Read anchor block. */
1053 	abuf = g_read_data(cp, pp->mediasize - ss, ss, &error);
1054 	if (abuf == NULL) {
1055 		G_RAID_DEBUG(1, "Cannot read metadata from %s (error=%d).",
1056 		    pp->name, error);
1057 		return (error);
1058 	}
1059 	ahdr = (struct ddf_header *)abuf;
1060 
1061 	/* Check if this is an DDF RAID struct */
1062 	if (be32dec(&ahdr->Signature) == DDF_HEADER_SIGNATURE)
1063 		meta->bigendian = 1;
1064 	else if (le32dec(&ahdr->Signature) == DDF_HEADER_SIGNATURE)
1065 		meta->bigendian = 0;
1066 	else {
1067 		G_RAID_DEBUG(1, "DDF signature check failed on %s", pp->name);
1068 		error = EINVAL;
1069 		goto done;
1070 	}
1071 	if (ahdr->Header_Type != DDF_HEADER_ANCHOR) {
1072 		G_RAID_DEBUG(1, "DDF header type check failed on %s", pp->name);
1073 		error = EINVAL;
1074 		goto done;
1075 	}
1076 	meta->hdr = ahdr;
1077 	plba = GET64(meta, hdr->Primary_Header_LBA);
1078 	slba = GET64(meta, hdr->Secondary_Header_LBA);
1079 	val = GET32(meta, hdr->CRC);
1080 	SET32(meta, hdr->CRC, 0xffffffff);
1081 	meta->hdr = NULL;
1082 	if (crc32(ahdr, ss) != val) {
1083 		G_RAID_DEBUG(1, "DDF CRC mismatch on %s", pp->name);
1084 		error = EINVAL;
1085 		goto done;
1086 	}
1087 	if ((plba + 6) * ss >= pp->mediasize) {
1088 		G_RAID_DEBUG(1, "DDF primary header LBA is wrong on %s", pp->name);
1089 		error = EINVAL;
1090 		goto done;
1091 	}
1092 	if (slba != -1 && (slba + 6) * ss >= pp->mediasize) {
1093 		G_RAID_DEBUG(1, "DDF secondary header LBA is wrong on %s", pp->name);
1094 		error = EINVAL;
1095 		goto done;
1096 	}
1097 	lba = plba;
1098 
1099 doread:
1100 	error = 0;
1101 	ddf_meta_free(meta);
1102 
1103 	/* Read header block. */
1104 	buf = g_read_data(cp, lba * ss, ss, &error);
1105 	if (buf == NULL) {
1106 readerror:
1107 		G_RAID_DEBUG(1, "DDF %s metadata read error on %s (error=%d).",
1108 		    (lba == plba) ? "primary" : "secondary", pp->name, error);
1109 		if (lba == plba && slba != -1) {
1110 			lba = slba;
1111 			goto doread;
1112 		}
1113 		G_RAID_DEBUG(1, "DDF metadata read error on %s.", pp->name);
1114 		goto done;
1115 	}
1116 	meta->hdr = malloc(ss, M_MD_DDF, M_WAITOK);
1117 	memcpy(meta->hdr, buf, ss);
1118 	g_free(buf);
1119 	hdr = meta->hdr;
1120 	val = GET32(meta, hdr->CRC);
1121 	SET32(meta, hdr->CRC, 0xffffffff);
1122 	if (hdr->Signature != ahdr->Signature ||
1123 	    crc32(meta->hdr, ss) != val ||
1124 	    memcmp(hdr->DDF_Header_GUID, ahdr->DDF_Header_GUID, 24) ||
1125 	    GET64(meta, hdr->Primary_Header_LBA) != plba ||
1126 	    GET64(meta, hdr->Secondary_Header_LBA) != slba) {
1127 hdrerror:
1128 		G_RAID_DEBUG(1, "DDF %s metadata check failed on %s",
1129 		    (lba == plba) ? "primary" : "secondary", pp->name);
1130 		if (lba == plba && slba != -1) {
1131 			lba = slba;
1132 			goto doread;
1133 		}
1134 		G_RAID_DEBUG(1, "DDF metadata check failed on %s", pp->name);
1135 		error = EINVAL;
1136 		goto done;
1137 	}
1138 	if ((lba == plba && hdr->Header_Type != DDF_HEADER_PRIMARY) ||
1139 	    (lba == slba && hdr->Header_Type != DDF_HEADER_SECONDARY))
1140 		goto hdrerror;
1141 	len = 1;
1142 	len = max(len, GET32(meta, hdr->cd_section) + GET32(meta, hdr->cd_length));
1143 	len = max(len, GET32(meta, hdr->pdr_section) + GET32(meta, hdr->pdr_length));
1144 	len = max(len, GET32(meta, hdr->vdr_section) + GET32(meta, hdr->vdr_length));
1145 	len = max(len, GET32(meta, hdr->cr_section) + GET32(meta, hdr->cr_length));
1146 	len = max(len, GET32(meta, hdr->pdd_section) + GET32(meta, hdr->pdd_length));
1147 	if ((val = GET32(meta, hdr->bbmlog_section)) != 0xffffffff)
1148 		len = max(len, val + GET32(meta, hdr->bbmlog_length));
1149 	if ((val = GET32(meta, hdr->Diagnostic_Space)) != 0xffffffff)
1150 		len = max(len, val + GET32(meta, hdr->Diagnostic_Space_Length));
1151 	if ((val = GET32(meta, hdr->Vendor_Specific_Logs)) != 0xffffffff)
1152 		len = max(len, val + GET32(meta, hdr->Vendor_Specific_Logs_Length));
1153 	if ((plba + len) * ss >= pp->mediasize)
1154 		goto hdrerror;
1155 	if (slba != -1 && (slba + len) * ss >= pp->mediasize)
1156 		goto hdrerror;
1157 	/* Workaround for Adaptec implementation. */
1158 	if (GET16(meta, hdr->Max_Primary_Element_Entries) == 0xffff) {
1159 		SET16(meta, hdr->Max_Primary_Element_Entries,
1160 		    min(GET16(meta, hdr->Max_PD_Entries),
1161 		    (GET16(meta, hdr->Configuration_Record_Length) * ss - 512) / 12));
1162 	}
1163 
1164 	if (GET32(meta, hdr->cd_length) * ss >= maxphys ||
1165 	    GET32(meta, hdr->pdr_length) * ss >= maxphys ||
1166 	    GET32(meta, hdr->vdr_length) * ss >= maxphys ||
1167 	    GET32(meta, hdr->cr_length) * ss >= maxphys ||
1168 	    GET32(meta, hdr->pdd_length) * ss >= maxphys ||
1169 	    GET32(meta, hdr->bbmlog_length) * ss >= maxphys) {
1170 		G_RAID_DEBUG(1, "%s: Blocksize is too big.", pp->name);
1171 		goto hdrerror;
1172 	}
1173 
1174 	/* Read controller data. */
1175 	buf = g_read_data(cp, (lba + GET32(meta, hdr->cd_section)) * ss,
1176 	    GET32(meta, hdr->cd_length) * ss, &error);
1177 	if (buf == NULL)
1178 		goto readerror;
1179 	meta->cdr = malloc(GET32(meta, hdr->cd_length) * ss, M_MD_DDF, M_WAITOK);
1180 	memcpy(meta->cdr, buf, GET32(meta, hdr->cd_length) * ss);
1181 	g_free(buf);
1182 	if (GET32(meta, cdr->Signature) != DDF_CONTROLLER_DATA_SIGNATURE)
1183 		goto hdrerror;
1184 
1185 	/* Read physical disk records. */
1186 	buf = g_read_data(cp, (lba + GET32(meta, hdr->pdr_section)) * ss,
1187 	    GET32(meta, hdr->pdr_length) * ss, &error);
1188 	if (buf == NULL)
1189 		goto readerror;
1190 	meta->pdr = malloc(GET32(meta, hdr->pdr_length) * ss, M_MD_DDF, M_WAITOK);
1191 	memcpy(meta->pdr, buf, GET32(meta, hdr->pdr_length) * ss);
1192 	g_free(buf);
1193 	if (GET32(meta, pdr->Signature) != DDF_PDR_SIGNATURE)
1194 		goto hdrerror;
1195 	/*
1196 	 * Workaround for reading metadata corrupted due to graid bug.
1197 	 * XXX: Remove this before we have disks above 128PB. :)
1198 	 */
1199 	if (meta->bigendian) {
1200 		for (i = 0; i < GET16(meta, pdr->Populated_PDEs); i++) {
1201 			if (isff(meta->pdr->entry[i].PD_GUID, 24))
1202 				continue;
1203 			if (GET32(meta, pdr->entry[i].PD_Reference) ==
1204 			    0xffffffff)
1205 				continue;
1206 			if (GET64(meta, pdr->entry[i].Configured_Size) >=
1207 			     (1ULL << 48)) {
1208 				SET16(meta, pdr->entry[i].PD_State,
1209 				    GET16(meta, pdr->entry[i].PD_State) &
1210 				    ~DDF_PDE_FAILED);
1211 				SET64(meta, pdr->entry[i].Configured_Size,
1212 				    GET64(meta, pdr->entry[i].Configured_Size) &
1213 				    ((1ULL << 48) - 1));
1214 			}
1215 		}
1216 	}
1217 
1218 	/* Read virtual disk records. */
1219 	buf = g_read_data(cp, (lba + GET32(meta, hdr->vdr_section)) * ss,
1220 	    GET32(meta, hdr->vdr_length) * ss, &error);
1221 	if (buf == NULL)
1222 		goto readerror;
1223 	meta->vdr = malloc(GET32(meta, hdr->vdr_length) * ss, M_MD_DDF, M_WAITOK);
1224 	memcpy(meta->vdr, buf, GET32(meta, hdr->vdr_length) * ss);
1225 	g_free(buf);
1226 	if (GET32(meta, vdr->Signature) != DDF_VD_RECORD_SIGNATURE)
1227 		goto hdrerror;
1228 
1229 	/* Read configuration records. */
1230 	buf = g_read_data(cp, (lba + GET32(meta, hdr->cr_section)) * ss,
1231 	    GET32(meta, hdr->cr_length) * ss, &error);
1232 	if (buf == NULL)
1233 		goto readerror;
1234 	meta->cr = malloc(GET32(meta, hdr->cr_length) * ss, M_MD_DDF, M_WAITOK);
1235 	memcpy(meta->cr, buf, GET32(meta, hdr->cr_length) * ss);
1236 	g_free(buf);
1237 
1238 	/* Read physical disk data. */
1239 	buf = g_read_data(cp, (lba + GET32(meta, hdr->pdd_section)) * ss,
1240 	    GET32(meta, hdr->pdd_length) * ss, &error);
1241 	if (buf == NULL)
1242 		goto readerror;
1243 	meta->pdd = malloc(GET32(meta, hdr->pdd_length) * ss, M_MD_DDF, M_WAITOK);
1244 	memcpy(meta->pdd, buf, GET32(meta, hdr->pdd_length) * ss);
1245 	g_free(buf);
1246 	if (GET32(meta, pdd->Signature) != DDF_PDD_SIGNATURE)
1247 		goto hdrerror;
1248 	i = ddf_meta_find_pd(meta, NULL, GET32(meta, pdd->PD_Reference));
1249 	if (i < 0)
1250 		goto hdrerror;
1251 
1252 	/* Read BBM Log. */
1253 	if (GET32(meta, hdr->bbmlog_section) != 0xffffffff &&
1254 	    GET32(meta, hdr->bbmlog_length) != 0) {
1255 		buf = g_read_data(cp, (lba + GET32(meta, hdr->bbmlog_section)) * ss,
1256 		    GET32(meta, hdr->bbmlog_length) * ss, &error);
1257 		if (buf == NULL)
1258 			goto readerror;
1259 		meta->bbm = malloc(GET32(meta, hdr->bbmlog_length) * ss, M_MD_DDF, M_WAITOK);
1260 		memcpy(meta->bbm, buf, GET32(meta, hdr->bbmlog_length) * ss);
1261 		g_free(buf);
1262 		if (GET32(meta, bbm->Signature) != DDF_BBML_SIGNATURE)
1263 			goto hdrerror;
1264 	}
1265 
1266 done:
1267 	g_free(abuf);
1268 	if (error != 0)
1269 		ddf_meta_free(meta);
1270 	return (error);
1271 }
1272 
1273 static int
1274 ddf_meta_write(struct g_consumer *cp, struct ddf_meta *meta)
1275 {
1276 	struct g_provider *pp;
1277 	struct ddf_vdc_record *vdc;
1278 	off_t alba, plba, slba, lba;
1279 	u_int ss, size;
1280 	int error, i, num;
1281 
1282 	pp = cp->provider;
1283 	ss = pp->sectorsize;
1284 	lba = alba = pp->mediasize / ss - 1;
1285 	plba = GET64(meta, hdr->Primary_Header_LBA);
1286 	slba = GET64(meta, hdr->Secondary_Header_LBA);
1287 
1288 next:
1289 	SET8(meta, hdr->Header_Type, (lba == alba) ? DDF_HEADER_ANCHOR :
1290 	    (lba == plba) ? DDF_HEADER_PRIMARY : DDF_HEADER_SECONDARY);
1291 	SET32(meta, hdr->CRC, 0xffffffff);
1292 	SET32(meta, hdr->CRC, crc32(meta->hdr, ss));
1293 	error = g_write_data(cp, lba * ss, meta->hdr, ss);
1294 	if (error != 0) {
1295 err:
1296 		G_RAID_DEBUG(1, "Cannot write metadata to %s (error=%d).",
1297 		    pp->name, error);
1298 		if (lba != alba)
1299 			goto done;
1300 	}
1301 	if (lba == alba) {
1302 		lba = plba;
1303 		goto next;
1304 	}
1305 
1306 	size = GET32(meta, hdr->cd_length) * ss;
1307 	SET32(meta, cdr->CRC, 0xffffffff);
1308 	SET32(meta, cdr->CRC, crc32(meta->cdr, size));
1309 	error = g_write_data(cp, (lba + GET32(meta, hdr->cd_section)) * ss,
1310 	    meta->cdr, size);
1311 	if (error != 0)
1312 		goto err;
1313 
1314 	size = GET32(meta, hdr->pdr_length) * ss;
1315 	SET32(meta, pdr->CRC, 0xffffffff);
1316 	SET32(meta, pdr->CRC, crc32(meta->pdr, size));
1317 	error = g_write_data(cp, (lba + GET32(meta, hdr->pdr_section)) * ss,
1318 	    meta->pdr, size);
1319 	if (error != 0)
1320 		goto err;
1321 
1322 	size = GET32(meta, hdr->vdr_length) * ss;
1323 	SET32(meta, vdr->CRC, 0xffffffff);
1324 	SET32(meta, vdr->CRC, crc32(meta->vdr, size));
1325 	error = g_write_data(cp, (lba + GET32(meta, hdr->vdr_section)) * ss,
1326 	    meta->vdr, size);
1327 	if (error != 0)
1328 		goto err;
1329 
1330 	size = GET16(meta, hdr->Configuration_Record_Length) * ss;
1331 	num = GETCRNUM(meta);
1332 	for (i = 0; i < num; i++) {
1333 		vdc = GETVDCPTR(meta, i);
1334 		SET32D(meta, vdc->CRC, 0xffffffff);
1335 		SET32D(meta, vdc->CRC, crc32(vdc, size));
1336 	}
1337 	error = g_write_data(cp, (lba + GET32(meta, hdr->cr_section)) * ss,
1338 	    meta->cr, size * num);
1339 	if (error != 0)
1340 		goto err;
1341 
1342 	size = GET32(meta, hdr->pdd_length) * ss;
1343 	SET32(meta, pdd->CRC, 0xffffffff);
1344 	SET32(meta, pdd->CRC, crc32(meta->pdd, size));
1345 	error = g_write_data(cp, (lba + GET32(meta, hdr->pdd_section)) * ss,
1346 	    meta->pdd, size);
1347 	if (error != 0)
1348 		goto err;
1349 
1350 	if (GET32(meta, hdr->bbmlog_length) != 0) {
1351 		size = GET32(meta, hdr->bbmlog_length) * ss;
1352 		SET32(meta, bbm->CRC, 0xffffffff);
1353 		SET32(meta, bbm->CRC, crc32(meta->bbm, size));
1354 		error = g_write_data(cp,
1355 		    (lba + GET32(meta, hdr->bbmlog_section)) * ss,
1356 		    meta->bbm, size);
1357 		if (error != 0)
1358 			goto err;
1359 	}
1360 
1361 done:
1362 	if (lba == plba && slba != -1) {
1363 		lba = slba;
1364 		goto next;
1365 	}
1366 
1367 	return (error);
1368 }
1369 
1370 static int
1371 ddf_meta_erase(struct g_consumer *cp)
1372 {
1373 	struct g_provider *pp;
1374 	char *buf;
1375 	int error;
1376 
1377 	pp = cp->provider;
1378 	buf = malloc(pp->sectorsize, M_MD_DDF, M_WAITOK | M_ZERO);
1379 	error = g_write_data(cp, pp->mediasize - pp->sectorsize,
1380 	    buf, pp->sectorsize);
1381 	if (error != 0) {
1382 		G_RAID_DEBUG(1, "Cannot erase metadata on %s (error=%d).",
1383 		    pp->name, error);
1384 	}
1385 	free(buf, M_MD_DDF);
1386 	return (error);
1387 }
1388 
1389 static struct g_raid_volume *
1390 g_raid_md_ddf_get_volume(struct g_raid_softc *sc, uint8_t *GUID)
1391 {
1392 	struct g_raid_volume	*vol;
1393 	struct g_raid_md_ddf_pervolume *pv;
1394 
1395 	TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
1396 		pv = vol->v_md_data;
1397 		if (memcmp(pv->pv_meta.vde->VD_GUID, GUID, 24) == 0)
1398 			break;
1399 	}
1400 	return (vol);
1401 }
1402 
1403 static struct g_raid_disk *
1404 g_raid_md_ddf_get_disk(struct g_raid_softc *sc, uint8_t *GUID, uint32_t id)
1405 {
1406 	struct g_raid_disk	*disk;
1407 	struct g_raid_md_ddf_perdisk *pd;
1408 	struct ddf_meta *meta;
1409 
1410 	TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1411 		pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
1412 		meta = &pd->pd_meta;
1413 		if (GUID != NULL) {
1414 			if (memcmp(meta->pdd->PD_GUID, GUID, 24) == 0)
1415 				break;
1416 		} else {
1417 			if (GET32(meta, pdd->PD_Reference) == id)
1418 				break;
1419 		}
1420 	}
1421 	return (disk);
1422 }
1423 
1424 static int
1425 g_raid_md_ddf_purge_volumes(struct g_raid_softc *sc)
1426 {
1427 	struct g_raid_volume	*vol, *tvol;
1428 	int i, res;
1429 
1430 	res = 0;
1431 	TAILQ_FOREACH_SAFE(vol, &sc->sc_volumes, v_next, tvol) {
1432 		if (vol->v_stopping)
1433 			continue;
1434 		for (i = 0; i < vol->v_disks_count; i++) {
1435 			if (vol->v_subdisks[i].sd_state != G_RAID_SUBDISK_S_NONE)
1436 				break;
1437 		}
1438 		if (i >= vol->v_disks_count) {
1439 			g_raid_destroy_volume(vol);
1440 			res = 1;
1441 		}
1442 	}
1443 	return (res);
1444 }
1445 
1446 static int
1447 g_raid_md_ddf_purge_disks(struct g_raid_softc *sc)
1448 {
1449 #if 0
1450 	struct g_raid_disk	*disk, *tdisk;
1451 	struct g_raid_volume	*vol;
1452 	struct g_raid_md_ddf_perdisk *pd;
1453 	int i, j, res;
1454 
1455 	res = 0;
1456 	TAILQ_FOREACH_SAFE(disk, &sc->sc_disks, d_next, tdisk) {
1457 		if (disk->d_state == G_RAID_DISK_S_SPARE)
1458 			continue;
1459 		pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
1460 
1461 		/* Scan for deleted volumes. */
1462 		for (i = 0; i < pd->pd_subdisks; ) {
1463 			vol = g_raid_md_ddf_get_volume(sc,
1464 			    pd->pd_meta[i]->volume_id);
1465 			if (vol != NULL && !vol->v_stopping) {
1466 				i++;
1467 				continue;
1468 			}
1469 			free(pd->pd_meta[i], M_MD_DDF);
1470 			for (j = i; j < pd->pd_subdisks - 1; j++)
1471 				pd->pd_meta[j] = pd->pd_meta[j + 1];
1472 			pd->pd_meta[DDF_MAX_SUBDISKS - 1] = NULL;
1473 			pd->pd_subdisks--;
1474 			pd->pd_updated = 1;
1475 		}
1476 
1477 		/* If there is no metadata left - erase and delete disk. */
1478 		if (pd->pd_subdisks == 0) {
1479 			ddf_meta_erase(disk->d_consumer);
1480 			g_raid_destroy_disk(disk);
1481 			res = 1;
1482 		}
1483 	}
1484 	return (res);
1485 #endif
1486 	return (0);
1487 }
1488 
1489 static int
1490 g_raid_md_ddf_supported(int level, int qual, int disks, int force)
1491 {
1492 
1493 	if (disks > DDF_MAX_DISKS_HARD)
1494 		return (0);
1495 	switch (level) {
1496 	case G_RAID_VOLUME_RL_RAID0:
1497 		if (qual != G_RAID_VOLUME_RLQ_NONE)
1498 			return (0);
1499 		if (disks < 1)
1500 			return (0);
1501 		if (!force && disks < 2)
1502 			return (0);
1503 		break;
1504 	case G_RAID_VOLUME_RL_RAID1:
1505 		if (disks < 1)
1506 			return (0);
1507 		if (qual == G_RAID_VOLUME_RLQ_R1SM) {
1508 			if (!force && disks != 2)
1509 				return (0);
1510 		} else if (qual == G_RAID_VOLUME_RLQ_R1MM) {
1511 			if (!force && disks != 3)
1512 				return (0);
1513 		} else
1514 			return (0);
1515 		break;
1516 	case G_RAID_VOLUME_RL_RAID3:
1517 		if (qual != G_RAID_VOLUME_RLQ_R3P0 &&
1518 		    qual != G_RAID_VOLUME_RLQ_R3PN)
1519 			return (0);
1520 		if (disks < 3)
1521 			return (0);
1522 		break;
1523 	case G_RAID_VOLUME_RL_RAID4:
1524 		if (qual != G_RAID_VOLUME_RLQ_R4P0 &&
1525 		    qual != G_RAID_VOLUME_RLQ_R4PN)
1526 			return (0);
1527 		if (disks < 3)
1528 			return (0);
1529 		break;
1530 	case G_RAID_VOLUME_RL_RAID5:
1531 		if (qual != G_RAID_VOLUME_RLQ_R5RA &&
1532 		    qual != G_RAID_VOLUME_RLQ_R5RS &&
1533 		    qual != G_RAID_VOLUME_RLQ_R5LA &&
1534 		    qual != G_RAID_VOLUME_RLQ_R5LS)
1535 			return (0);
1536 		if (disks < 3)
1537 			return (0);
1538 		break;
1539 	case G_RAID_VOLUME_RL_RAID6:
1540 		if (qual != G_RAID_VOLUME_RLQ_R6RA &&
1541 		    qual != G_RAID_VOLUME_RLQ_R6RS &&
1542 		    qual != G_RAID_VOLUME_RLQ_R6LA &&
1543 		    qual != G_RAID_VOLUME_RLQ_R6LS)
1544 			return (0);
1545 		if (disks < 4)
1546 			return (0);
1547 		break;
1548 	case G_RAID_VOLUME_RL_RAIDMDF:
1549 		if (qual != G_RAID_VOLUME_RLQ_RMDFRA &&
1550 		    qual != G_RAID_VOLUME_RLQ_RMDFRS &&
1551 		    qual != G_RAID_VOLUME_RLQ_RMDFLA &&
1552 		    qual != G_RAID_VOLUME_RLQ_RMDFLS)
1553 			return (0);
1554 		if (disks < 4)
1555 			return (0);
1556 		break;
1557 	case G_RAID_VOLUME_RL_RAID1E:
1558 		if (qual != G_RAID_VOLUME_RLQ_R1EA &&
1559 		    qual != G_RAID_VOLUME_RLQ_R1EO)
1560 			return (0);
1561 		if (disks < 3)
1562 			return (0);
1563 		break;
1564 	case G_RAID_VOLUME_RL_SINGLE:
1565 		if (qual != G_RAID_VOLUME_RLQ_NONE)
1566 			return (0);
1567 		if (disks != 1)
1568 			return (0);
1569 		break;
1570 	case G_RAID_VOLUME_RL_CONCAT:
1571 		if (qual != G_RAID_VOLUME_RLQ_NONE)
1572 			return (0);
1573 		if (disks < 2)
1574 			return (0);
1575 		break;
1576 	case G_RAID_VOLUME_RL_RAID5E:
1577 		if (qual != G_RAID_VOLUME_RLQ_R5ERA &&
1578 		    qual != G_RAID_VOLUME_RLQ_R5ERS &&
1579 		    qual != G_RAID_VOLUME_RLQ_R5ELA &&
1580 		    qual != G_RAID_VOLUME_RLQ_R5ELS)
1581 			return (0);
1582 		if (disks < 4)
1583 			return (0);
1584 		break;
1585 	case G_RAID_VOLUME_RL_RAID5EE:
1586 		if (qual != G_RAID_VOLUME_RLQ_R5EERA &&
1587 		    qual != G_RAID_VOLUME_RLQ_R5EERS &&
1588 		    qual != G_RAID_VOLUME_RLQ_R5EELA &&
1589 		    qual != G_RAID_VOLUME_RLQ_R5EELS)
1590 			return (0);
1591 		if (disks < 4)
1592 			return (0);
1593 		break;
1594 	case G_RAID_VOLUME_RL_RAID5R:
1595 		if (qual != G_RAID_VOLUME_RLQ_R5RRA &&
1596 		    qual != G_RAID_VOLUME_RLQ_R5RRS &&
1597 		    qual != G_RAID_VOLUME_RLQ_R5RLA &&
1598 		    qual != G_RAID_VOLUME_RLQ_R5RLS)
1599 			return (0);
1600 		if (disks < 3)
1601 			return (0);
1602 		break;
1603 	default:
1604 		return (0);
1605 	}
1606 	return (1);
1607 }
1608 
1609 static int
1610 g_raid_md_ddf_start_disk(struct g_raid_disk *disk, struct g_raid_volume *vol)
1611 {
1612 	struct g_raid_softc *sc;
1613 	struct g_raid_subdisk *sd;
1614 	struct g_raid_md_ddf_perdisk *pd;
1615 	struct g_raid_md_ddf_pervolume *pv;
1616 	struct g_raid_md_ddf_object *mdi;
1617 	struct ddf_vol_meta *vmeta;
1618 	struct ddf_meta *pdmeta, *gmeta;
1619 	struct ddf_vdc_record *vdc1;
1620 	struct ddf_sa_record *sa;
1621 	off_t size, eoff = 0, esize = 0;
1622 	uint64_t *val2;
1623 	int disk_pos, md_disk_bvd = -1, md_disk_pos = -1, md_pde_pos;
1624 	int i, resurrection = 0;
1625 	uint32_t reference;
1626 
1627 	sc = disk->d_softc;
1628 	mdi = (struct g_raid_md_ddf_object *)sc->sc_md;
1629 	pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
1630 	pdmeta = &pd->pd_meta;
1631 	reference = GET32(&pd->pd_meta, pdd->PD_Reference);
1632 
1633 	pv = vol->v_md_data;
1634 	vmeta = &pv->pv_meta;
1635 	gmeta = &mdi->mdio_meta;
1636 
1637 	/* Find disk position in metadata by its reference. */
1638 	disk_pos = ddf_meta_find_disk(vmeta, reference,
1639 	    &md_disk_bvd, &md_disk_pos);
1640 	md_pde_pos = ddf_meta_find_pd(gmeta, NULL, reference);
1641 
1642 	if (disk_pos < 0) {
1643 		G_RAID_DEBUG1(1, sc,
1644 		    "Disk %s is not a present part of the volume %s",
1645 		    g_raid_get_diskname(disk), vol->v_name);
1646 
1647 		/* Failed stale disk is useless for us. */
1648 		if ((GET16(gmeta, pdr->entry[md_pde_pos].PD_State) & DDF_PDE_PFA) != 0) {
1649 			g_raid_change_disk_state(disk, G_RAID_DISK_S_STALE_FAILED);
1650 			return (0);
1651 		}
1652 
1653 		/* If disk has some metadata for this volume - erase. */
1654 		if ((vdc1 = ddf_meta_find_vdc(pdmeta, vmeta->vdc->VD_GUID)) != NULL)
1655 			SET32D(pdmeta, vdc1->Signature, 0xffffffff);
1656 
1657 		/* If we are in the start process, that's all for now. */
1658 		if (!pv->pv_started)
1659 			goto nofit;
1660 		/*
1661 		 * If we have already started - try to get use of the disk.
1662 		 * Try to replace OFFLINE disks first, then FAILED.
1663 		 */
1664 		if (ddf_meta_count_vdc(&pd->pd_meta, NULL) >=
1665 			GET16(&pd->pd_meta, hdr->Max_Partitions)) {
1666 			G_RAID_DEBUG1(1, sc, "No free partitions on disk %s",
1667 			    g_raid_get_diskname(disk));
1668 			goto nofit;
1669 		}
1670 		ddf_meta_unused_range(&pd->pd_meta, &eoff, &esize);
1671 		if (esize == 0) {
1672 			G_RAID_DEBUG1(1, sc, "No free space on disk %s",
1673 			    g_raid_get_diskname(disk));
1674 			goto nofit;
1675 		}
1676 		eoff *= pd->pd_meta.sectorsize;
1677 		esize *= pd->pd_meta.sectorsize;
1678 		size = INT64_MAX;
1679 		for (i = 0; i < vol->v_disks_count; i++) {
1680 			sd = &vol->v_subdisks[i];
1681 			if (sd->sd_state != G_RAID_SUBDISK_S_NONE)
1682 				size = sd->sd_size;
1683 			if (sd->sd_state <= G_RAID_SUBDISK_S_FAILED &&
1684 			    (disk_pos < 0 ||
1685 			     vol->v_subdisks[i].sd_state < sd->sd_state))
1686 				disk_pos = i;
1687 		}
1688 		if (disk_pos >= 0 &&
1689 		    vol->v_raid_level != G_RAID_VOLUME_RL_CONCAT &&
1690 		    esize < size) {
1691 			G_RAID_DEBUG1(1, sc, "Disk %s free space "
1692 			    "is too small (%ju < %ju)",
1693 			    g_raid_get_diskname(disk), esize, size);
1694 			disk_pos = -1;
1695 		}
1696 		if (disk_pos >= 0) {
1697 			if (vol->v_raid_level != G_RAID_VOLUME_RL_CONCAT)
1698 				esize = size;
1699 			md_disk_bvd = disk_pos / GET16(vmeta, vdc->Primary_Element_Count); // XXX
1700 			md_disk_pos = disk_pos % GET16(vmeta, vdc->Primary_Element_Count); // XXX
1701 		} else {
1702 nofit:
1703 			if (disk->d_state == G_RAID_DISK_S_NONE)
1704 				g_raid_change_disk_state(disk,
1705 				    G_RAID_DISK_S_STALE);
1706 			return (0);
1707 		}
1708 
1709 		/*
1710 		 * If spare is committable, delete spare record.
1711 		 * Othersize, mark it active and leave there.
1712 		 */
1713 		sa = ddf_meta_find_sa(&pd->pd_meta, 0);
1714 		if (sa != NULL) {
1715 			if ((GET8D(&pd->pd_meta, sa->Spare_Type) &
1716 			    DDF_SAR_TYPE_REVERTIBLE) == 0) {
1717 				SET32D(&pd->pd_meta, sa->Signature, 0xffffffff);
1718 			} else {
1719 				SET8D(&pd->pd_meta, sa->Spare_Type,
1720 				    GET8D(&pd->pd_meta, sa->Spare_Type) |
1721 				    DDF_SAR_TYPE_ACTIVE);
1722 			}
1723 		}
1724 
1725 		G_RAID_DEBUG1(1, sc, "Disk %s takes pos %d in the volume %s",
1726 		    g_raid_get_diskname(disk), disk_pos, vol->v_name);
1727 		resurrection = 1;
1728 	}
1729 
1730 	sd = &vol->v_subdisks[disk_pos];
1731 
1732 	if (resurrection && sd->sd_disk != NULL) {
1733 		g_raid_change_disk_state(sd->sd_disk,
1734 		    G_RAID_DISK_S_STALE_FAILED);
1735 		TAILQ_REMOVE(&sd->sd_disk->d_subdisks,
1736 		    sd, sd_next);
1737 	}
1738 	vol->v_subdisks[disk_pos].sd_disk = disk;
1739 	TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
1740 
1741 	/* Welcome the new disk. */
1742 	if (resurrection)
1743 		g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE);
1744 	else if (GET16(gmeta, pdr->entry[md_pde_pos].PD_State) & DDF_PDE_PFA)
1745 		g_raid_change_disk_state(disk, G_RAID_DISK_S_FAILED);
1746 	else
1747 		g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE);
1748 
1749 	if (resurrection) {
1750 		sd->sd_offset = eoff;
1751 		sd->sd_size = esize;
1752 	} else if (pdmeta->cr != NULL &&
1753 	    (vdc1 = ddf_meta_find_vdc(pdmeta, vmeta->vdc->VD_GUID)) != NULL) {
1754 		val2 = (uint64_t *)&(vdc1->Physical_Disk_Sequence[GET16(vmeta, hdr->Max_Primary_Element_Entries)]);
1755 		sd->sd_offset = (off_t)GET64P(pdmeta, val2 + md_disk_pos) * 512;
1756 		sd->sd_size = (off_t)GET64D(pdmeta, vdc1->Block_Count) * 512;
1757 	}
1758 
1759 	if (resurrection) {
1760 		/* Stale disk, almost same as new. */
1761 		g_raid_change_subdisk_state(sd,
1762 		    G_RAID_SUBDISK_S_NEW);
1763 	} else if (GET16(gmeta, pdr->entry[md_pde_pos].PD_State) & DDF_PDE_PFA) {
1764 		/* Failed disk. */
1765 		g_raid_change_subdisk_state(sd,
1766 		    G_RAID_SUBDISK_S_FAILED);
1767 	} else if ((GET16(gmeta, pdr->entry[md_pde_pos].PD_State) &
1768 	     (DDF_PDE_FAILED | DDF_PDE_REBUILD)) != 0) {
1769 		/* Rebuilding disk. */
1770 		g_raid_change_subdisk_state(sd,
1771 		    G_RAID_SUBDISK_S_REBUILD);
1772 		sd->sd_rebuild_pos = 0;
1773 	} else if ((GET8(vmeta, vde->VD_State) & DDF_VDE_DIRTY) != 0 ||
1774 	    (GET8(vmeta, vde->Init_State) & DDF_VDE_INIT_MASK) !=
1775 	     DDF_VDE_INIT_FULL) {
1776 		/* Stale disk or dirty volume (unclean shutdown). */
1777 		g_raid_change_subdisk_state(sd,
1778 		    G_RAID_SUBDISK_S_STALE);
1779 	} else {
1780 		/* Up to date disk. */
1781 		g_raid_change_subdisk_state(sd,
1782 		    G_RAID_SUBDISK_S_ACTIVE);
1783 	}
1784 	g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW,
1785 	    G_RAID_EVENT_SUBDISK);
1786 
1787 	return (resurrection);
1788 }
1789 
1790 static void
1791 g_raid_md_ddf_refill(struct g_raid_softc *sc)
1792 {
1793 	struct g_raid_volume *vol;
1794 	struct g_raid_subdisk *sd;
1795 	struct g_raid_disk *disk;
1796 	struct g_raid_md_object *md;
1797 	struct g_raid_md_ddf_perdisk *pd;
1798 	struct g_raid_md_ddf_pervolume *pv;
1799 	int update, updated, i, bad;
1800 
1801 	md = sc->sc_md;
1802 restart:
1803 	updated = 0;
1804 	TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
1805 		pv = vol->v_md_data;
1806 		if (!pv->pv_started || vol->v_stopping)
1807 			continue;
1808 
1809 		/* Search for subdisk that needs replacement. */
1810 		bad = 0;
1811 		for (i = 0; i < vol->v_disks_count; i++) {
1812 			sd = &vol->v_subdisks[i];
1813 			if (sd->sd_state == G_RAID_SUBDISK_S_NONE ||
1814 			    sd->sd_state == G_RAID_SUBDISK_S_FAILED)
1815 			        bad = 1;
1816 		}
1817 		if (!bad)
1818 			continue;
1819 
1820 		G_RAID_DEBUG1(1, sc, "Volume %s is not complete, "
1821 		    "trying to refill.", vol->v_name);
1822 
1823 		TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1824 			/* Skip failed. */
1825 			if (disk->d_state < G_RAID_DISK_S_SPARE)
1826 				continue;
1827 			/* Skip already used by this volume. */
1828 			for (i = 0; i < vol->v_disks_count; i++) {
1829 				sd = &vol->v_subdisks[i];
1830 				if (sd->sd_disk == disk)
1831 					break;
1832 			}
1833 			if (i < vol->v_disks_count)
1834 				continue;
1835 
1836 			/* Try to use disk if it has empty extents. */
1837 			pd = disk->d_md_data;
1838 			if (ddf_meta_count_vdc(&pd->pd_meta, NULL) <
1839 			    GET16(&pd->pd_meta, hdr->Max_Partitions)) {
1840 				update = g_raid_md_ddf_start_disk(disk, vol);
1841 			} else
1842 				update = 0;
1843 			if (update) {
1844 				updated = 1;
1845 				g_raid_md_write_ddf(md, vol, NULL, disk);
1846 				break;
1847 			}
1848 		}
1849 	}
1850 	if (updated)
1851 		goto restart;
1852 }
1853 
1854 static void
1855 g_raid_md_ddf_start(struct g_raid_volume *vol)
1856 {
1857 	struct g_raid_softc *sc;
1858 	struct g_raid_subdisk *sd;
1859 	struct g_raid_disk *disk;
1860 	struct g_raid_md_object *md;
1861 	struct g_raid_md_ddf_perdisk *pd;
1862 	struct g_raid_md_ddf_pervolume *pv;
1863 	struct g_raid_md_ddf_object *mdi;
1864 	struct ddf_vol_meta *vmeta;
1865 	uint64_t *val2;
1866 	int i, j, bvd;
1867 
1868 	sc = vol->v_softc;
1869 	md = sc->sc_md;
1870 	mdi = (struct g_raid_md_ddf_object *)md;
1871 	pv = vol->v_md_data;
1872 	vmeta = &pv->pv_meta;
1873 
1874 	vol->v_raid_level = GET8(vmeta, vdc->Primary_RAID_Level);
1875 	vol->v_raid_level_qualifier = GET8(vmeta, vdc->RLQ);
1876 	if (GET8(vmeta, vdc->Secondary_Element_Count) > 1 &&
1877 	    vol->v_raid_level == G_RAID_VOLUME_RL_RAID1 &&
1878 	    GET8(vmeta, vdc->Secondary_RAID_Level) == 0)
1879 		vol->v_raid_level = G_RAID_VOLUME_RL_RAID1E;
1880 	vol->v_sectorsize = GET16(vmeta, vdc->Block_Size);
1881 	if (vol->v_sectorsize == 0xffff)
1882 		vol->v_sectorsize = vmeta->sectorsize;
1883 	vol->v_strip_size = vol->v_sectorsize << GET8(vmeta, vdc->Stripe_Size);
1884 	vol->v_disks_count = GET16(vmeta, vdc->Primary_Element_Count) *
1885 	    GET8(vmeta, vdc->Secondary_Element_Count);
1886 	vol->v_mdf_pdisks = GET8(vmeta, vdc->MDF_Parity_Disks);
1887 	vol->v_mdf_polynomial = GET16(vmeta, vdc->MDF_Parity_Generator_Polynomial);
1888 	vol->v_mdf_method = GET8(vmeta, vdc->MDF_Constant_Generation_Method);
1889 	if (GET8(vmeta, vdc->Rotate_Parity_count) > 31)
1890 		vol->v_rotate_parity = 1;
1891 	else
1892 		vol->v_rotate_parity = 1 << GET8(vmeta, vdc->Rotate_Parity_count);
1893 	vol->v_mediasize = GET64(vmeta, vdc->VD_Size) * vol->v_sectorsize;
1894 	for (i = 0, j = 0, bvd = 0; i < vol->v_disks_count; i++, j++) {
1895 		if (j == GET16(vmeta, vdc->Primary_Element_Count)) {
1896 			j = 0;
1897 			bvd++;
1898 		}
1899 		sd = &vol->v_subdisks[i];
1900 		if (vmeta->bvdc[bvd] == NULL) {
1901 			sd->sd_offset = 0;
1902 			sd->sd_size = GET64(vmeta, vdc->Block_Count) *
1903 			    vol->v_sectorsize;
1904 			continue;
1905 		}
1906 		val2 = (uint64_t *)&(vmeta->bvdc[bvd]->Physical_Disk_Sequence[
1907 		    GET16(vmeta, hdr->Max_Primary_Element_Entries)]);
1908 		sd->sd_offset = GET64P(vmeta, val2 + j) * vol->v_sectorsize;
1909 		sd->sd_size = GET64(vmeta, bvdc[bvd]->Block_Count) *
1910 		    vol->v_sectorsize;
1911 	}
1912 	g_raid_start_volume(vol);
1913 
1914 	/* Make all disks found till the moment take their places. */
1915 	TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1916 		pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
1917 		if (ddf_meta_find_vdc(&pd->pd_meta, vmeta->vdc->VD_GUID) != NULL)
1918 			g_raid_md_ddf_start_disk(disk, vol);
1919 	}
1920 
1921 	pv->pv_started = 1;
1922 	mdi->mdio_starting--;
1923 	callout_stop(&pv->pv_start_co);
1924 	G_RAID_DEBUG1(0, sc, "Volume started.");
1925 	g_raid_md_write_ddf(md, vol, NULL, NULL);
1926 
1927 	/* Pickup any STALE/SPARE disks to refill array if needed. */
1928 	g_raid_md_ddf_refill(sc);
1929 
1930 	g_raid_event_send(vol, G_RAID_VOLUME_E_START, G_RAID_EVENT_VOLUME);
1931 }
1932 
1933 static void
1934 g_raid_ddf_go(void *arg)
1935 {
1936 	struct g_raid_volume *vol;
1937 	struct g_raid_softc *sc;
1938 	struct g_raid_md_ddf_pervolume *pv;
1939 
1940 	vol = arg;
1941 	pv = vol->v_md_data;
1942 	sc = vol->v_softc;
1943 	if (!pv->pv_started) {
1944 		G_RAID_DEBUG1(0, sc, "Force volume start due to timeout.");
1945 		g_raid_event_send(vol, G_RAID_VOLUME_E_STARTMD,
1946 		    G_RAID_EVENT_VOLUME);
1947 	}
1948 }
1949 
1950 static void
1951 g_raid_md_ddf_new_disk(struct g_raid_disk *disk)
1952 {
1953 	struct g_raid_softc *sc;
1954 	struct g_raid_md_object *md;
1955 	struct g_raid_md_ddf_perdisk *pd;
1956 	struct g_raid_md_ddf_pervolume *pv;
1957 	struct g_raid_md_ddf_object *mdi;
1958 	struct g_raid_volume *vol;
1959 	struct ddf_meta *pdmeta;
1960 	struct ddf_vol_meta *vmeta;
1961 	struct ddf_vdc_record *vdc;
1962 	struct ddf_vd_entry *vde;
1963 	int i, j, k, num, have, need, cnt, spare;
1964 	uint32_t val;
1965 	char buf[17];
1966 
1967 	sc = disk->d_softc;
1968 	md = sc->sc_md;
1969 	mdi = (struct g_raid_md_ddf_object *)md;
1970 	pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
1971 	pdmeta = &pd->pd_meta;
1972 	spare = -1;
1973 
1974 	if (mdi->mdio_meta.hdr == NULL)
1975 		ddf_meta_copy(&mdi->mdio_meta, pdmeta);
1976 	else
1977 		ddf_meta_update(&mdi->mdio_meta, pdmeta);
1978 
1979 	num = GETCRNUM(pdmeta);
1980 	for (j = 0; j < num; j++) {
1981 		vdc = GETVDCPTR(pdmeta, j);
1982 		val = GET32D(pdmeta, vdc->Signature);
1983 
1984 		if (val == DDF_SA_SIGNATURE && spare == -1)
1985 			spare = 1;
1986 
1987 		if (val != DDF_VDCR_SIGNATURE)
1988 			continue;
1989 		spare = 0;
1990 		k = ddf_meta_find_vd(pdmeta, vdc->VD_GUID);
1991 		if (k < 0)
1992 			continue;
1993 		vde = &pdmeta->vdr->entry[k];
1994 
1995 		/* Look for volume with matching ID. */
1996 		vol = g_raid_md_ddf_get_volume(sc, vdc->VD_GUID);
1997 		if (vol == NULL) {
1998 			ddf_meta_get_name(pdmeta, k, buf);
1999 			vol = g_raid_create_volume(sc, buf,
2000 			    GET16D(pdmeta, vde->VD_Number));
2001 			pv = malloc(sizeof(*pv), M_MD_DDF, M_WAITOK | M_ZERO);
2002 			vol->v_md_data = pv;
2003 			callout_init(&pv->pv_start_co, 1);
2004 			callout_reset(&pv->pv_start_co,
2005 			    g_raid_start_timeout * hz,
2006 			    g_raid_ddf_go, vol);
2007 			mdi->mdio_starting++;
2008 		} else
2009 			pv = vol->v_md_data;
2010 
2011 		/* If we haven't started yet - check metadata freshness. */
2012 		vmeta = &pv->pv_meta;
2013 		ddf_vol_meta_update(vmeta, pdmeta, vdc->VD_GUID, pv->pv_started);
2014 	}
2015 
2016 	if (spare == 1) {
2017 		g_raid_change_disk_state(disk, G_RAID_DISK_S_SPARE);
2018 		g_raid_md_ddf_refill(sc);
2019 	}
2020 
2021 	TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2022 		pv = vol->v_md_data;
2023 		vmeta = &pv->pv_meta;
2024 
2025 		if (ddf_meta_find_vdc(pdmeta, vmeta->vdc->VD_GUID) == NULL)
2026 			continue;
2027 
2028 		if (pv->pv_started) {
2029 			if (g_raid_md_ddf_start_disk(disk, vol))
2030 				g_raid_md_write_ddf(md, vol, NULL, NULL);
2031 			continue;
2032 		}
2033 
2034 		/* If we collected all needed disks - start array. */
2035 		need = 0;
2036 		have = 0;
2037 		for (k = 0; k < GET8(vmeta, vdc->Secondary_Element_Count); k++) {
2038 			if (vmeta->bvdc[k] == NULL) {
2039 				need += GET16(vmeta, vdc->Primary_Element_Count);
2040 				continue;
2041 			}
2042 			cnt = GET16(vmeta, bvdc[k]->Primary_Element_Count);
2043 			need += cnt;
2044 			for (i = 0; i < cnt; i++) {
2045 				val = GET32(vmeta, bvdc[k]->Physical_Disk_Sequence[i]);
2046 				if (g_raid_md_ddf_get_disk(sc, NULL, val) != NULL)
2047 					have++;
2048 			}
2049 		}
2050 		G_RAID_DEBUG1(1, sc, "Volume %s now has %d of %d disks",
2051 		    vol->v_name, have, need);
2052 		if (have == need)
2053 			g_raid_md_ddf_start(vol);
2054 	}
2055 }
2056 
2057 static int
2058 g_raid_md_create_req_ddf(struct g_raid_md_object *md, struct g_class *mp,
2059     struct gctl_req *req, struct g_geom **gp)
2060 {
2061 	struct g_geom *geom;
2062 	struct g_raid_softc *sc;
2063 	struct g_raid_md_ddf_object *mdi, *mdi1;
2064 	char name[16];
2065 	const char *fmtopt;
2066 	int be = 1;
2067 
2068 	mdi = (struct g_raid_md_ddf_object *)md;
2069 	fmtopt = gctl_get_asciiparam(req, "fmtopt");
2070 	if (fmtopt == NULL || strcasecmp(fmtopt, "BE") == 0)
2071 		be = 1;
2072 	else if (strcasecmp(fmtopt, "LE") == 0)
2073 		be = 0;
2074 	else {
2075 		gctl_error(req, "Incorrect fmtopt argument.");
2076 		return (G_RAID_MD_TASTE_FAIL);
2077 	}
2078 
2079 	/* Search for existing node. */
2080 	LIST_FOREACH(geom, &mp->geom, geom) {
2081 		sc = geom->softc;
2082 		if (sc == NULL)
2083 			continue;
2084 		if (sc->sc_stopping != 0)
2085 			continue;
2086 		if (sc->sc_md->mdo_class != md->mdo_class)
2087 			continue;
2088 		mdi1 = (struct g_raid_md_ddf_object *)sc->sc_md;
2089 		if (mdi1->mdio_bigendian != be)
2090 			continue;
2091 		break;
2092 	}
2093 	if (geom != NULL) {
2094 		*gp = geom;
2095 		return (G_RAID_MD_TASTE_EXISTING);
2096 	}
2097 
2098 	/* Create new one if not found. */
2099 	mdi->mdio_bigendian = be;
2100 	snprintf(name, sizeof(name), "DDF%s", be ? "" : "-LE");
2101 	sc = g_raid_create_node(mp, name, md);
2102 	if (sc == NULL)
2103 		return (G_RAID_MD_TASTE_FAIL);
2104 	md->mdo_softc = sc;
2105 	*gp = sc->sc_geom;
2106 	return (G_RAID_MD_TASTE_NEW);
2107 }
2108 
2109 static int
2110 g_raid_md_taste_ddf(struct g_raid_md_object *md, struct g_class *mp,
2111                               struct g_consumer *cp, struct g_geom **gp)
2112 {
2113 	struct g_consumer *rcp;
2114 	struct g_provider *pp;
2115 	struct g_raid_softc *sc;
2116 	struct g_raid_disk *disk;
2117 	struct ddf_meta meta;
2118 	struct g_raid_md_ddf_perdisk *pd;
2119 	struct g_raid_md_ddf_object *mdi;
2120 	struct g_geom *geom;
2121 	int error, result, be;
2122 	char name[16];
2123 
2124 	G_RAID_DEBUG(1, "Tasting DDF on %s", cp->provider->name);
2125 	mdi = (struct g_raid_md_ddf_object *)md;
2126 	pp = cp->provider;
2127 
2128 	/* Read metadata from device. */
2129 	g_topology_unlock();
2130 	bzero(&meta, sizeof(meta));
2131 	error = ddf_meta_read(cp, &meta);
2132 	g_topology_lock();
2133 	if (error != 0)
2134 		return (G_RAID_MD_TASTE_FAIL);
2135 	be = meta.bigendian;
2136 
2137 	/* Metadata valid. Print it. */
2138 	g_raid_md_ddf_print(&meta);
2139 
2140 	/* Search for matching node. */
2141 	sc = NULL;
2142 	LIST_FOREACH(geom, &mp->geom, geom) {
2143 		sc = geom->softc;
2144 		if (sc == NULL)
2145 			continue;
2146 		if (sc->sc_stopping != 0)
2147 			continue;
2148 		if (sc->sc_md->mdo_class != md->mdo_class)
2149 			continue;
2150 		mdi = (struct g_raid_md_ddf_object *)sc->sc_md;
2151 		if (mdi->mdio_bigendian != be)
2152 			continue;
2153 		break;
2154 	}
2155 
2156 	/* Found matching node. */
2157 	if (geom != NULL) {
2158 		G_RAID_DEBUG(1, "Found matching array %s", sc->sc_name);
2159 		result = G_RAID_MD_TASTE_EXISTING;
2160 
2161 	} else { /* Not found matching node -- create one. */
2162 		result = G_RAID_MD_TASTE_NEW;
2163 		mdi->mdio_bigendian = be;
2164 		snprintf(name, sizeof(name), "DDF%s", be ? "" : "-LE");
2165 		sc = g_raid_create_node(mp, name, md);
2166 		md->mdo_softc = sc;
2167 		geom = sc->sc_geom;
2168 	}
2169 
2170 	/* There is no return after this point, so we close passed consumer. */
2171 	g_access(cp, -1, 0, 0);
2172 
2173 	rcp = g_new_consumer(geom);
2174 	rcp->flags |= G_CF_DIRECT_RECEIVE;
2175 	g_attach(rcp, pp);
2176 	if (g_access(rcp, 1, 1, 1) != 0)
2177 		; //goto fail1;
2178 
2179 	g_topology_unlock();
2180 	sx_xlock(&sc->sc_lock);
2181 
2182 	pd = malloc(sizeof(*pd), M_MD_DDF, M_WAITOK | M_ZERO);
2183 	pd->pd_meta = meta;
2184 	disk = g_raid_create_disk(sc);
2185 	disk->d_md_data = (void *)pd;
2186 	disk->d_consumer = rcp;
2187 	rcp->private = disk;
2188 
2189 	g_raid_get_disk_info(disk);
2190 
2191 	g_raid_md_ddf_new_disk(disk);
2192 
2193 	sx_xunlock(&sc->sc_lock);
2194 	g_topology_lock();
2195 	*gp = geom;
2196 	return (result);
2197 }
2198 
2199 static int
2200 g_raid_md_event_ddf(struct g_raid_md_object *md,
2201     struct g_raid_disk *disk, u_int event)
2202 {
2203 	struct g_raid_softc *sc;
2204 
2205 	sc = md->mdo_softc;
2206 	if (disk == NULL)
2207 		return (-1);
2208 	switch (event) {
2209 	case G_RAID_DISK_E_DISCONNECTED:
2210 		/* Delete disk. */
2211 		g_raid_change_disk_state(disk, G_RAID_DISK_S_NONE);
2212 		g_raid_destroy_disk(disk);
2213 		g_raid_md_ddf_purge_volumes(sc);
2214 
2215 		/* Write updated metadata to all disks. */
2216 		g_raid_md_write_ddf(md, NULL, NULL, NULL);
2217 
2218 		/* Check if anything left. */
2219 		if (g_raid_ndisks(sc, -1) == 0)
2220 			g_raid_destroy_node(sc, 0);
2221 		else
2222 			g_raid_md_ddf_refill(sc);
2223 		return (0);
2224 	}
2225 	return (-2);
2226 }
2227 
2228 static int
2229 g_raid_md_volume_event_ddf(struct g_raid_md_object *md,
2230     struct g_raid_volume *vol, u_int event)
2231 {
2232 	struct g_raid_md_ddf_pervolume *pv;
2233 
2234 	pv = (struct g_raid_md_ddf_pervolume *)vol->v_md_data;
2235 	switch (event) {
2236 	case G_RAID_VOLUME_E_STARTMD:
2237 		if (!pv->pv_started)
2238 			g_raid_md_ddf_start(vol);
2239 		return (0);
2240 	}
2241 	return (-2);
2242 }
2243 
2244 static int
2245 g_raid_md_ctl_ddf(struct g_raid_md_object *md,
2246     struct gctl_req *req)
2247 {
2248 	struct g_raid_softc *sc;
2249 	struct g_raid_volume *vol, *vol1;
2250 	struct g_raid_subdisk *sd;
2251 	struct g_raid_disk *disk, *disks[DDF_MAX_DISKS_HARD];
2252 	struct g_raid_md_ddf_perdisk *pd;
2253 	struct g_raid_md_ddf_pervolume *pv;
2254 	struct g_raid_md_ddf_object *mdi;
2255 	struct ddf_sa_record *sa;
2256 	struct g_consumer *cp;
2257 	struct g_provider *pp;
2258 	char arg[16];
2259 	const char *nodename, *verb, *volname, *levelname, *diskname;
2260 	char *tmp;
2261 	int *nargs, *force;
2262 	off_t size, sectorsize, strip, offs[DDF_MAX_DISKS_HARD], esize;
2263 	intmax_t *sizearg, *striparg;
2264 	int i, numdisks, len, level, qual;
2265 	int error;
2266 
2267 	sc = md->mdo_softc;
2268 	mdi = (struct g_raid_md_ddf_object *)md;
2269 	verb = gctl_get_param(req, "verb", NULL);
2270 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
2271 	error = 0;
2272 
2273 	if (strcmp(verb, "label") == 0) {
2274 		if (*nargs < 4) {
2275 			gctl_error(req, "Invalid number of arguments.");
2276 			return (-1);
2277 		}
2278 		volname = gctl_get_asciiparam(req, "arg1");
2279 		if (volname == NULL) {
2280 			gctl_error(req, "No volume name.");
2281 			return (-2);
2282 		}
2283 		levelname = gctl_get_asciiparam(req, "arg2");
2284 		if (levelname == NULL) {
2285 			gctl_error(req, "No RAID level.");
2286 			return (-3);
2287 		}
2288 		if (g_raid_volume_str2level(levelname, &level, &qual)) {
2289 			gctl_error(req, "Unknown RAID level '%s'.", levelname);
2290 			return (-4);
2291 		}
2292 		numdisks = *nargs - 3;
2293 		force = gctl_get_paraml(req, "force", sizeof(*force));
2294 		if (!g_raid_md_ddf_supported(level, qual, numdisks,
2295 		    force ? *force : 0)) {
2296 			gctl_error(req, "Unsupported RAID level "
2297 			    "(0x%02x/0x%02x), or number of disks (%d).",
2298 			    level, qual, numdisks);
2299 			return (-5);
2300 		}
2301 
2302 		/* Search for disks, connect them and probe. */
2303 		size = INT64_MAX;
2304 		sectorsize = 0;
2305 		bzero(disks, sizeof(disks));
2306 		bzero(offs, sizeof(offs));
2307 		for (i = 0; i < numdisks; i++) {
2308 			snprintf(arg, sizeof(arg), "arg%d", i + 3);
2309 			diskname = gctl_get_asciiparam(req, arg);
2310 			if (diskname == NULL) {
2311 				gctl_error(req, "No disk name (%s).", arg);
2312 				error = -6;
2313 				break;
2314 			}
2315 			if (strcmp(diskname, "NONE") == 0)
2316 				continue;
2317 
2318 			TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2319 				if (disk->d_consumer != NULL &&
2320 				    disk->d_consumer->provider != NULL &&
2321 				    strcmp(disk->d_consumer->provider->name,
2322 				     diskname) == 0)
2323 					break;
2324 			}
2325 			if (disk != NULL) {
2326 				if (disk->d_state != G_RAID_DISK_S_ACTIVE) {
2327 					gctl_error(req, "Disk '%s' is in a "
2328 					    "wrong state (%s).", diskname,
2329 					    g_raid_disk_state2str(disk->d_state));
2330 					error = -7;
2331 					break;
2332 				}
2333 				pd = disk->d_md_data;
2334 				if (ddf_meta_count_vdc(&pd->pd_meta, NULL) >=
2335 				    GET16(&pd->pd_meta, hdr->Max_Partitions)) {
2336 					gctl_error(req, "No free partitions "
2337 					    "on disk '%s'.",
2338 					    diskname);
2339 					error = -7;
2340 					break;
2341 				}
2342 				pp = disk->d_consumer->provider;
2343 				disks[i] = disk;
2344 				ddf_meta_unused_range(&pd->pd_meta,
2345 				    &offs[i], &esize);
2346 				offs[i] *= pp->sectorsize;
2347 				size = MIN(size, (off_t)esize * pp->sectorsize);
2348 				sectorsize = MAX(sectorsize, pp->sectorsize);
2349 				continue;
2350 			}
2351 
2352 			g_topology_lock();
2353 			cp = g_raid_open_consumer(sc, diskname);
2354 			if (cp == NULL) {
2355 				gctl_error(req, "Can't open disk '%s'.",
2356 				    diskname);
2357 				g_topology_unlock();
2358 				error = -8;
2359 				break;
2360 			}
2361 			pp = cp->provider;
2362 			pd = malloc(sizeof(*pd), M_MD_DDF, M_WAITOK | M_ZERO);
2363 			disk = g_raid_create_disk(sc);
2364 			disk->d_md_data = (void *)pd;
2365 			disk->d_consumer = cp;
2366 			disks[i] = disk;
2367 			cp->private = disk;
2368 			ddf_meta_create(disk, &mdi->mdio_meta);
2369 			if (mdi->mdio_meta.hdr == NULL)
2370 				ddf_meta_copy(&mdi->mdio_meta, &pd->pd_meta);
2371 			else
2372 				ddf_meta_update(&mdi->mdio_meta, &pd->pd_meta);
2373 			g_topology_unlock();
2374 
2375 			g_raid_get_disk_info(disk);
2376 
2377 			/* Reserve some space for metadata. */
2378 			size = MIN(size, GET64(&pd->pd_meta,
2379 			    pdr->entry[0].Configured_Size) * pp->sectorsize);
2380 			sectorsize = MAX(sectorsize, pp->sectorsize);
2381 		}
2382 		if (error != 0) {
2383 			for (i = 0; i < numdisks; i++) {
2384 				if (disks[i] != NULL &&
2385 				    disks[i]->d_state == G_RAID_DISK_S_NONE)
2386 					g_raid_destroy_disk(disks[i]);
2387 			}
2388 			return (error);
2389 		}
2390 
2391 		if (sectorsize <= 0) {
2392 			gctl_error(req, "Can't get sector size.");
2393 			return (-8);
2394 		}
2395 
2396 		/* Handle size argument. */
2397 		len = sizeof(*sizearg);
2398 		sizearg = gctl_get_param(req, "size", &len);
2399 		if (sizearg != NULL && len == sizeof(*sizearg) &&
2400 		    *sizearg > 0) {
2401 			if (*sizearg > size) {
2402 				gctl_error(req, "Size too big %lld > %lld.",
2403 				    (long long)*sizearg, (long long)size);
2404 				return (-9);
2405 			}
2406 			size = *sizearg;
2407 		}
2408 
2409 		/* Handle strip argument. */
2410 		strip = 131072;
2411 		len = sizeof(*striparg);
2412 		striparg = gctl_get_param(req, "strip", &len);
2413 		if (striparg != NULL && len == sizeof(*striparg) &&
2414 		    *striparg > 0) {
2415 			if (*striparg < sectorsize) {
2416 				gctl_error(req, "Strip size too small.");
2417 				return (-10);
2418 			}
2419 			if (*striparg % sectorsize != 0) {
2420 				gctl_error(req, "Incorrect strip size.");
2421 				return (-11);
2422 			}
2423 			strip = *striparg;
2424 		}
2425 
2426 		/* Round size down to strip or sector. */
2427 		if (level == G_RAID_VOLUME_RL_RAID1 ||
2428 		    level == G_RAID_VOLUME_RL_RAID3 ||
2429 		    level == G_RAID_VOLUME_RL_SINGLE ||
2430 		    level == G_RAID_VOLUME_RL_CONCAT)
2431 			size -= (size % sectorsize);
2432 		else if (level == G_RAID_VOLUME_RL_RAID1E &&
2433 		    (numdisks & 1) != 0)
2434 			size -= (size % (2 * strip));
2435 		else
2436 			size -= (size % strip);
2437 		if (size <= 0) {
2438 			gctl_error(req, "Size too small.");
2439 			return (-13);
2440 		}
2441 
2442 		/* We have all we need, create things: volume, ... */
2443 		pv = malloc(sizeof(*pv), M_MD_DDF, M_WAITOK | M_ZERO);
2444 		ddf_vol_meta_create(&pv->pv_meta, &mdi->mdio_meta);
2445 		pv->pv_started = 1;
2446 		vol = g_raid_create_volume(sc, volname, -1);
2447 		vol->v_md_data = pv;
2448 		vol->v_raid_level = level;
2449 		vol->v_raid_level_qualifier = qual;
2450 		vol->v_strip_size = strip;
2451 		vol->v_disks_count = numdisks;
2452 		if (level == G_RAID_VOLUME_RL_RAID0 ||
2453 		    level == G_RAID_VOLUME_RL_CONCAT ||
2454 		    level == G_RAID_VOLUME_RL_SINGLE)
2455 			vol->v_mediasize = size * numdisks;
2456 		else if (level == G_RAID_VOLUME_RL_RAID1)
2457 			vol->v_mediasize = size;
2458 		else if (level == G_RAID_VOLUME_RL_RAID3 ||
2459 		    level == G_RAID_VOLUME_RL_RAID4 ||
2460 		    level == G_RAID_VOLUME_RL_RAID5)
2461 			vol->v_mediasize = size * (numdisks - 1);
2462 		else if (level == G_RAID_VOLUME_RL_RAID5R) {
2463 			vol->v_mediasize = size * (numdisks - 1);
2464 			vol->v_rotate_parity = 1024;
2465 		} else if (level == G_RAID_VOLUME_RL_RAID6 ||
2466 		    level == G_RAID_VOLUME_RL_RAID5E ||
2467 		    level == G_RAID_VOLUME_RL_RAID5EE)
2468 			vol->v_mediasize = size * (numdisks - 2);
2469 		else if (level == G_RAID_VOLUME_RL_RAIDMDF) {
2470 			if (numdisks < 5)
2471 				vol->v_mdf_pdisks = 2;
2472 			else
2473 				vol->v_mdf_pdisks = 3;
2474 			vol->v_mdf_polynomial = 0x11d;
2475 			vol->v_mdf_method = 0x00;
2476 			vol->v_mediasize = size * (numdisks - vol->v_mdf_pdisks);
2477 		} else { /* RAID1E */
2478 			vol->v_mediasize = ((size * numdisks) / strip / 2) *
2479 			    strip;
2480 		}
2481 		vol->v_sectorsize = sectorsize;
2482 		g_raid_start_volume(vol);
2483 
2484 		/* , and subdisks. */
2485 		for (i = 0; i < numdisks; i++) {
2486 			disk = disks[i];
2487 			sd = &vol->v_subdisks[i];
2488 			sd->sd_disk = disk;
2489 			sd->sd_offset = offs[i];
2490 			sd->sd_size = size;
2491 			if (disk == NULL)
2492 				continue;
2493 			TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
2494 			g_raid_change_disk_state(disk,
2495 			    G_RAID_DISK_S_ACTIVE);
2496 			g_raid_change_subdisk_state(sd,
2497 			    G_RAID_SUBDISK_S_ACTIVE);
2498 			g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW,
2499 			    G_RAID_EVENT_SUBDISK);
2500 		}
2501 
2502 		/* Write metadata based on created entities. */
2503 		G_RAID_DEBUG1(0, sc, "Array started.");
2504 		g_raid_md_write_ddf(md, vol, NULL, NULL);
2505 
2506 		/* Pickup any STALE/SPARE disks to refill array if needed. */
2507 		g_raid_md_ddf_refill(sc);
2508 
2509 		g_raid_event_send(vol, G_RAID_VOLUME_E_START,
2510 		    G_RAID_EVENT_VOLUME);
2511 		return (0);
2512 	}
2513 	if (strcmp(verb, "add") == 0) {
2514 		gctl_error(req, "`add` command is not applicable, "
2515 		    "use `label` instead.");
2516 		return (-99);
2517 	}
2518 	if (strcmp(verb, "delete") == 0) {
2519 		nodename = gctl_get_asciiparam(req, "arg0");
2520 		if (nodename != NULL && strcasecmp(sc->sc_name, nodename) != 0)
2521 			nodename = NULL;
2522 
2523 		/* Full node destruction. */
2524 		if (*nargs == 1 && nodename != NULL) {
2525 			/* Check if some volume is still open. */
2526 			force = gctl_get_paraml(req, "force", sizeof(*force));
2527 			if (force != NULL && *force == 0 &&
2528 			    g_raid_nopens(sc) != 0) {
2529 				gctl_error(req, "Some volume is still open.");
2530 				return (-4);
2531 			}
2532 
2533 			TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2534 				if (disk->d_consumer)
2535 					ddf_meta_erase(disk->d_consumer);
2536 			}
2537 			g_raid_destroy_node(sc, 0);
2538 			return (0);
2539 		}
2540 
2541 		/* Destroy specified volume. If it was last - all node. */
2542 		if (*nargs > 2) {
2543 			gctl_error(req, "Invalid number of arguments.");
2544 			return (-1);
2545 		}
2546 		volname = gctl_get_asciiparam(req,
2547 		    nodename != NULL ? "arg1" : "arg0");
2548 		if (volname == NULL) {
2549 			gctl_error(req, "No volume name.");
2550 			return (-2);
2551 		}
2552 
2553 		/* Search for volume. */
2554 		TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2555 			if (strcmp(vol->v_name, volname) == 0)
2556 				break;
2557 			pp = vol->v_provider;
2558 			if (pp == NULL)
2559 				continue;
2560 			if (strcmp(pp->name, volname) == 0)
2561 				break;
2562 			if (strncmp(pp->name, "raid/", 5) == 0 &&
2563 			    strcmp(pp->name + 5, volname) == 0)
2564 				break;
2565 		}
2566 		if (vol == NULL) {
2567 			i = strtol(volname, &tmp, 10);
2568 			if (verb != volname && tmp[0] == 0) {
2569 				TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2570 					if (vol->v_global_id == i)
2571 						break;
2572 				}
2573 			}
2574 		}
2575 		if (vol == NULL) {
2576 			gctl_error(req, "Volume '%s' not found.", volname);
2577 			return (-3);
2578 		}
2579 
2580 		/* Check if volume is still open. */
2581 		force = gctl_get_paraml(req, "force", sizeof(*force));
2582 		if (force != NULL && *force == 0 &&
2583 		    vol->v_provider_open != 0) {
2584 			gctl_error(req, "Volume is still open.");
2585 			return (-4);
2586 		}
2587 
2588 		/* Destroy volume and potentially node. */
2589 		i = 0;
2590 		TAILQ_FOREACH(vol1, &sc->sc_volumes, v_next)
2591 			i++;
2592 		if (i >= 2) {
2593 			g_raid_destroy_volume(vol);
2594 			g_raid_md_ddf_purge_disks(sc);
2595 			g_raid_md_write_ddf(md, NULL, NULL, NULL);
2596 		} else {
2597 			TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2598 				if (disk->d_consumer)
2599 					ddf_meta_erase(disk->d_consumer);
2600 			}
2601 			g_raid_destroy_node(sc, 0);
2602 		}
2603 		return (0);
2604 	}
2605 	if (strcmp(verb, "remove") == 0 ||
2606 	    strcmp(verb, "fail") == 0) {
2607 		if (*nargs < 2) {
2608 			gctl_error(req, "Invalid number of arguments.");
2609 			return (-1);
2610 		}
2611 		for (i = 1; i < *nargs; i++) {
2612 			snprintf(arg, sizeof(arg), "arg%d", i);
2613 			diskname = gctl_get_asciiparam(req, arg);
2614 			if (diskname == NULL) {
2615 				gctl_error(req, "No disk name (%s).", arg);
2616 				error = -2;
2617 				break;
2618 			}
2619 			if (strncmp(diskname, _PATH_DEV, 5) == 0)
2620 				diskname += 5;
2621 
2622 			TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2623 				if (disk->d_consumer != NULL &&
2624 				    disk->d_consumer->provider != NULL &&
2625 				    strcmp(disk->d_consumer->provider->name,
2626 				     diskname) == 0)
2627 					break;
2628 			}
2629 			if (disk == NULL) {
2630 				gctl_error(req, "Disk '%s' not found.",
2631 				    diskname);
2632 				error = -3;
2633 				break;
2634 			}
2635 
2636 			if (strcmp(verb, "fail") == 0) {
2637 				g_raid_md_fail_disk_ddf(md, NULL, disk);
2638 				continue;
2639 			}
2640 
2641 			/* Erase metadata on deleting disk and destroy it. */
2642 			ddf_meta_erase(disk->d_consumer);
2643 			g_raid_destroy_disk(disk);
2644 		}
2645 		g_raid_md_ddf_purge_volumes(sc);
2646 
2647 		/* Write updated metadata to remaining disks. */
2648 		g_raid_md_write_ddf(md, NULL, NULL, NULL);
2649 
2650 		/* Check if anything left. */
2651 		if (g_raid_ndisks(sc, -1) == 0)
2652 			g_raid_destroy_node(sc, 0);
2653 		else
2654 			g_raid_md_ddf_refill(sc);
2655 		return (error);
2656 	}
2657 	if (strcmp(verb, "insert") == 0) {
2658 		if (*nargs < 2) {
2659 			gctl_error(req, "Invalid number of arguments.");
2660 			return (-1);
2661 		}
2662 		for (i = 1; i < *nargs; i++) {
2663 			/* Get disk name. */
2664 			snprintf(arg, sizeof(arg), "arg%d", i);
2665 			diskname = gctl_get_asciiparam(req, arg);
2666 			if (diskname == NULL) {
2667 				gctl_error(req, "No disk name (%s).", arg);
2668 				error = -3;
2669 				break;
2670 			}
2671 
2672 			/* Try to find provider with specified name. */
2673 			g_topology_lock();
2674 			cp = g_raid_open_consumer(sc, diskname);
2675 			if (cp == NULL) {
2676 				gctl_error(req, "Can't open disk '%s'.",
2677 				    diskname);
2678 				g_topology_unlock();
2679 				error = -4;
2680 				break;
2681 			}
2682 			pp = cp->provider;
2683 			g_topology_unlock();
2684 
2685 			pd = malloc(sizeof(*pd), M_MD_DDF, M_WAITOK | M_ZERO);
2686 
2687 			disk = g_raid_create_disk(sc);
2688 			disk->d_consumer = cp;
2689 			disk->d_md_data = (void *)pd;
2690 			cp->private = disk;
2691 
2692 			g_raid_get_disk_info(disk);
2693 
2694 			/* Welcome the "new" disk. */
2695 			g_raid_change_disk_state(disk, G_RAID_DISK_S_SPARE);
2696 			ddf_meta_create(disk, &mdi->mdio_meta);
2697 			sa = ddf_meta_find_sa(&pd->pd_meta, 1);
2698 			if (sa != NULL) {
2699 				SET32D(&pd->pd_meta, sa->Signature,
2700 				    DDF_SA_SIGNATURE);
2701 				SET8D(&pd->pd_meta, sa->Spare_Type, 0);
2702 				SET16D(&pd->pd_meta, sa->Populated_SAEs, 0);
2703 				SET16D(&pd->pd_meta, sa->MAX_SAE_Supported,
2704 				    (GET16(&pd->pd_meta, hdr->Configuration_Record_Length) *
2705 				     pd->pd_meta.sectorsize -
2706 				     sizeof(struct ddf_sa_record)) /
2707 				    sizeof(struct ddf_sa_entry));
2708 			}
2709 			if (mdi->mdio_meta.hdr == NULL)
2710 				ddf_meta_copy(&mdi->mdio_meta, &pd->pd_meta);
2711 			else
2712 				ddf_meta_update(&mdi->mdio_meta, &pd->pd_meta);
2713 			g_raid_md_write_ddf(md, NULL, NULL, NULL);
2714 			g_raid_md_ddf_refill(sc);
2715 		}
2716 		return (error);
2717 	}
2718 	return (-100);
2719 }
2720 
2721 static int
2722 g_raid_md_write_ddf(struct g_raid_md_object *md, struct g_raid_volume *tvol,
2723     struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk)
2724 {
2725 	struct g_raid_softc *sc;
2726 	struct g_raid_volume *vol;
2727 	struct g_raid_subdisk *sd;
2728 	struct g_raid_disk *disk;
2729 	struct g_raid_md_ddf_perdisk *pd;
2730 	struct g_raid_md_ddf_pervolume *pv;
2731 	struct g_raid_md_ddf_object *mdi;
2732 	struct ddf_meta *gmeta;
2733 	struct ddf_vol_meta *vmeta;
2734 	struct ddf_vdc_record *vdc;
2735 	struct ddf_sa_record *sa;
2736 	uint64_t *val2;
2737 	int i, j, pos, bvd, size;
2738 
2739 	sc = md->mdo_softc;
2740 	mdi = (struct g_raid_md_ddf_object *)md;
2741 	gmeta = &mdi->mdio_meta;
2742 
2743 	if (sc->sc_stopping == G_RAID_DESTROY_HARD)
2744 		return (0);
2745 
2746 	/*
2747 	 * Clear disk flags to let only really needed ones to be reset.
2748 	 * Do it only if there are no volumes in starting state now,
2749 	 * as they can update disk statuses yet and we may kill innocent.
2750 	 */
2751 	if (mdi->mdio_starting == 0) {
2752 		for (i = 0; i < GET16(gmeta, pdr->Populated_PDEs); i++) {
2753 			if (isff(gmeta->pdr->entry[i].PD_GUID, 24))
2754 				continue;
2755 			SET16(gmeta, pdr->entry[i].PD_Type,
2756 			    GET16(gmeta, pdr->entry[i].PD_Type) &
2757 			    ~(DDF_PDE_PARTICIPATING |
2758 			      DDF_PDE_GLOBAL_SPARE | DDF_PDE_CONFIG_SPARE));
2759 			if ((GET16(gmeta, pdr->entry[i].PD_State) &
2760 			    DDF_PDE_PFA) == 0)
2761 				SET16(gmeta, pdr->entry[i].PD_State, 0);
2762 		}
2763 	}
2764 
2765 	/* Generate/update new per-volume metadata. */
2766 	TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2767 		pv = (struct g_raid_md_ddf_pervolume *)vol->v_md_data;
2768 		if (vol->v_stopping || !pv->pv_started)
2769 			continue;
2770 		vmeta = &pv->pv_meta;
2771 
2772 		SET32(vmeta, vdc->Sequence_Number,
2773 		    GET32(vmeta, vdc->Sequence_Number) + 1);
2774 		if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E &&
2775 		    vol->v_disks_count % 2 == 0)
2776 			SET16(vmeta, vdc->Primary_Element_Count, 2);
2777 		else
2778 			SET16(vmeta, vdc->Primary_Element_Count,
2779 			    vol->v_disks_count);
2780 		SET8(vmeta, vdc->Stripe_Size,
2781 		    ffs(vol->v_strip_size / vol->v_sectorsize) - 1);
2782 		if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E &&
2783 		    vol->v_disks_count % 2 == 0) {
2784 			SET8(vmeta, vdc->Primary_RAID_Level,
2785 			    DDF_VDCR_RAID1);
2786 			SET8(vmeta, vdc->RLQ, 0);
2787 			SET8(vmeta, vdc->Secondary_Element_Count,
2788 			    vol->v_disks_count / 2);
2789 			SET8(vmeta, vdc->Secondary_RAID_Level, 0);
2790 		} else {
2791 			SET8(vmeta, vdc->Primary_RAID_Level,
2792 			    vol->v_raid_level);
2793 			SET8(vmeta, vdc->RLQ,
2794 			    vol->v_raid_level_qualifier);
2795 			SET8(vmeta, vdc->Secondary_Element_Count, 1);
2796 			SET8(vmeta, vdc->Secondary_RAID_Level, 0);
2797 		}
2798 		SET8(vmeta, vdc->Secondary_Element_Seq, 0);
2799 		SET64(vmeta, vdc->Block_Count, 0);
2800 		SET64(vmeta, vdc->VD_Size, vol->v_mediasize / vol->v_sectorsize);
2801 		SET16(vmeta, vdc->Block_Size, vol->v_sectorsize);
2802 		SET8(vmeta, vdc->Rotate_Parity_count,
2803 		    fls(vol->v_rotate_parity) - 1);
2804 		SET8(vmeta, vdc->MDF_Parity_Disks, vol->v_mdf_pdisks);
2805 		SET16(vmeta, vdc->MDF_Parity_Generator_Polynomial,
2806 		    vol->v_mdf_polynomial);
2807 		SET8(vmeta, vdc->MDF_Constant_Generation_Method,
2808 		    vol->v_mdf_method);
2809 
2810 		SET16(vmeta, vde->VD_Number, vol->v_global_id);
2811 		if (vol->v_state <= G_RAID_VOLUME_S_BROKEN)
2812 			SET8(vmeta, vde->VD_State, DDF_VDE_FAILED);
2813 		else if (vol->v_state <= G_RAID_VOLUME_S_DEGRADED)
2814 			SET8(vmeta, vde->VD_State, DDF_VDE_DEGRADED);
2815 		else if (vol->v_state <= G_RAID_VOLUME_S_SUBOPTIMAL)
2816 			SET8(vmeta, vde->VD_State, DDF_VDE_PARTIAL);
2817 		else
2818 			SET8(vmeta, vde->VD_State, DDF_VDE_OPTIMAL);
2819 		if (vol->v_dirty ||
2820 		    g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_STALE) > 0 ||
2821 		    g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_RESYNC) > 0)
2822 			SET8(vmeta, vde->VD_State,
2823 			    GET8(vmeta, vde->VD_State) | DDF_VDE_DIRTY);
2824 		SET8(vmeta, vde->Init_State, DDF_VDE_INIT_FULL); // XXX
2825 		ddf_meta_put_name(vmeta, vol->v_name);
2826 
2827 		for (i = 0; i < vol->v_disks_count; i++) {
2828 			sd = &vol->v_subdisks[i];
2829 			bvd = i / GET16(vmeta, vdc->Primary_Element_Count);
2830 			pos = i % GET16(vmeta, vdc->Primary_Element_Count);
2831 			disk = sd->sd_disk;
2832 			if (disk != NULL) {
2833 				pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
2834 				if (vmeta->bvdc[bvd] == NULL) {
2835 					size = GET16(vmeta,
2836 					    hdr->Configuration_Record_Length) *
2837 					    vmeta->sectorsize;
2838 					vmeta->bvdc[bvd] = malloc(size,
2839 					    M_MD_DDF, M_WAITOK);
2840 					memset(vmeta->bvdc[bvd], 0xff, size);
2841 				}
2842 				memcpy(vmeta->bvdc[bvd], vmeta->vdc,
2843 				    sizeof(struct ddf_vdc_record));
2844 				SET8(vmeta, bvdc[bvd]->Secondary_Element_Seq, bvd);
2845 				SET64(vmeta, bvdc[bvd]->Block_Count,
2846 				    sd->sd_size / vol->v_sectorsize);
2847 				SET32(vmeta, bvdc[bvd]->Physical_Disk_Sequence[pos],
2848 				    GET32(&pd->pd_meta, pdd->PD_Reference));
2849 				val2 = (uint64_t *)&(vmeta->bvdc[bvd]->Physical_Disk_Sequence[
2850 				    GET16(vmeta, hdr->Max_Primary_Element_Entries)]);
2851 				SET64P(vmeta, val2 + pos,
2852 				    sd->sd_offset / vol->v_sectorsize);
2853 			}
2854 			if (vmeta->bvdc[bvd] == NULL)
2855 				continue;
2856 
2857 			j = ddf_meta_find_pd(gmeta, NULL,
2858 			    GET32(vmeta, bvdc[bvd]->Physical_Disk_Sequence[pos]));
2859 			if (j < 0)
2860 				continue;
2861 			SET16(gmeta, pdr->entry[j].PD_Type,
2862 			    GET16(gmeta, pdr->entry[j].PD_Type) |
2863 			    DDF_PDE_PARTICIPATING);
2864 			if (sd->sd_state == G_RAID_SUBDISK_S_NONE)
2865 				SET16(gmeta, pdr->entry[j].PD_State,
2866 				    GET16(gmeta, pdr->entry[j].PD_State) |
2867 				    (DDF_PDE_FAILED | DDF_PDE_MISSING));
2868 			else if (sd->sd_state == G_RAID_SUBDISK_S_FAILED)
2869 				SET16(gmeta, pdr->entry[j].PD_State,
2870 				    GET16(gmeta, pdr->entry[j].PD_State) |
2871 				    (DDF_PDE_FAILED | DDF_PDE_PFA));
2872 			else if (sd->sd_state <= G_RAID_SUBDISK_S_REBUILD)
2873 				SET16(gmeta, pdr->entry[j].PD_State,
2874 				    GET16(gmeta, pdr->entry[j].PD_State) |
2875 				    DDF_PDE_REBUILD);
2876 			else
2877 				SET16(gmeta, pdr->entry[j].PD_State,
2878 				    GET16(gmeta, pdr->entry[j].PD_State) |
2879 				    DDF_PDE_ONLINE);
2880 		}
2881 	}
2882 
2883 	/* Mark spare and failed disks as such. */
2884 	TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2885 		pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
2886 		i = ddf_meta_find_pd(gmeta, NULL,
2887 		    GET32(&pd->pd_meta, pdd->PD_Reference));
2888 		if (i < 0)
2889 			continue;
2890 		if (disk->d_state == G_RAID_DISK_S_FAILED) {
2891 			SET16(gmeta, pdr->entry[i].PD_State,
2892 			    GET16(gmeta, pdr->entry[i].PD_State) |
2893 			    (DDF_PDE_FAILED | DDF_PDE_PFA));
2894 		}
2895 		if (disk->d_state != G_RAID_DISK_S_SPARE)
2896 			continue;
2897 		sa = ddf_meta_find_sa(&pd->pd_meta, 0);
2898 		if (sa == NULL ||
2899 		    (GET8D(&pd->pd_meta, sa->Spare_Type) &
2900 		     DDF_SAR_TYPE_DEDICATED) == 0) {
2901 			SET16(gmeta, pdr->entry[i].PD_Type,
2902 			    GET16(gmeta, pdr->entry[i].PD_Type) |
2903 			    DDF_PDE_GLOBAL_SPARE);
2904 		} else {
2905 			SET16(gmeta, pdr->entry[i].PD_Type,
2906 			    GET16(gmeta, pdr->entry[i].PD_Type) |
2907 			    DDF_PDE_CONFIG_SPARE);
2908 		}
2909 		SET16(gmeta, pdr->entry[i].PD_State,
2910 		    GET16(gmeta, pdr->entry[i].PD_State) |
2911 		    DDF_PDE_ONLINE);
2912 	}
2913 
2914 	/* Remove disks without "participating" flag (unused). */
2915 	for (i = 0, j = -1; i < GET16(gmeta, pdr->Populated_PDEs); i++) {
2916 		if (isff(gmeta->pdr->entry[i].PD_GUID, 24))
2917 			continue;
2918 		if ((GET16(gmeta, pdr->entry[i].PD_Type) &
2919 		    (DDF_PDE_PARTICIPATING |
2920 		     DDF_PDE_GLOBAL_SPARE | DDF_PDE_CONFIG_SPARE)) != 0 ||
2921 		    g_raid_md_ddf_get_disk(sc,
2922 		     NULL, GET32(gmeta, pdr->entry[i].PD_Reference)) != NULL)
2923 			j = i;
2924 		else
2925 			memset(&gmeta->pdr->entry[i], 0xff,
2926 			    sizeof(struct ddf_pd_entry));
2927 	}
2928 	SET16(gmeta, pdr->Populated_PDEs, j + 1);
2929 
2930 	/* Update per-disk metadata and write them. */
2931 	TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2932 		pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
2933 		if (disk->d_state != G_RAID_DISK_S_ACTIVE &&
2934 		    disk->d_state != G_RAID_DISK_S_SPARE)
2935 			continue;
2936 		/* Update PDR. */
2937 		memcpy(pd->pd_meta.pdr, gmeta->pdr,
2938 		    GET32(&pd->pd_meta, hdr->pdr_length) *
2939 		    pd->pd_meta.sectorsize);
2940 		/* Update VDR. */
2941 		SET16(&pd->pd_meta, vdr->Populated_VDEs, 0);
2942 		TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2943 			if (vol->v_stopping)
2944 				continue;
2945 			pv = (struct g_raid_md_ddf_pervolume *)vol->v_md_data;
2946 			i = ddf_meta_find_vd(&pd->pd_meta,
2947 			    pv->pv_meta.vde->VD_GUID);
2948 			if (i < 0)
2949 				i = ddf_meta_find_vd(&pd->pd_meta, NULL);
2950 			if (i >= 0)
2951 				memcpy(&pd->pd_meta.vdr->entry[i],
2952 				    pv->pv_meta.vde,
2953 				    sizeof(struct ddf_vd_entry));
2954 		}
2955 		/* Update VDC. */
2956 		if (mdi->mdio_starting == 0) {
2957 			/* Remove all VDCs to restore needed later. */
2958 			j = GETCRNUM(&pd->pd_meta);
2959 			for (i = 0; i < j; i++) {
2960 				vdc = GETVDCPTR(&pd->pd_meta, i);
2961 				if (GET32D(&pd->pd_meta, vdc->Signature) !=
2962 				    DDF_VDCR_SIGNATURE)
2963 					continue;
2964 				SET32D(&pd->pd_meta, vdc->Signature, 0xffffffff);
2965 			}
2966 		}
2967 		TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
2968 			vol = sd->sd_volume;
2969 			if (vol->v_stopping)
2970 				continue;
2971 			pv = (struct g_raid_md_ddf_pervolume *)vol->v_md_data;
2972 			vmeta = &pv->pv_meta;
2973 			vdc = ddf_meta_find_vdc(&pd->pd_meta,
2974 			    vmeta->vde->VD_GUID);
2975 			if (vdc == NULL)
2976 				vdc = ddf_meta_find_vdc(&pd->pd_meta, NULL);
2977 			if (vdc != NULL) {
2978 				bvd = sd->sd_pos / GET16(vmeta,
2979 				    vdc->Primary_Element_Count);
2980 				memcpy(vdc, vmeta->bvdc[bvd],
2981 				    GET16(&pd->pd_meta,
2982 				    hdr->Configuration_Record_Length) *
2983 				    pd->pd_meta.sectorsize);
2984 			}
2985 		}
2986 		G_RAID_DEBUG(1, "Writing DDF metadata to %s",
2987 		    g_raid_get_diskname(disk));
2988 		g_raid_md_ddf_print(&pd->pd_meta);
2989 		ddf_meta_write(disk->d_consumer, &pd->pd_meta);
2990 	}
2991 	return (0);
2992 }
2993 
2994 static int
2995 g_raid_md_fail_disk_ddf(struct g_raid_md_object *md,
2996     struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk)
2997 {
2998 	struct g_raid_softc *sc;
2999 	struct g_raid_md_ddf_perdisk *pd;
3000 	struct g_raid_subdisk *sd;
3001 	int i;
3002 
3003 	sc = md->mdo_softc;
3004 	pd = (struct g_raid_md_ddf_perdisk *)tdisk->d_md_data;
3005 
3006 	/* We can't fail disk that is not a part of array now. */
3007 	if (tdisk->d_state != G_RAID_DISK_S_ACTIVE)
3008 		return (-1);
3009 
3010 	/*
3011 	 * Mark disk as failed in metadata and try to write that metadata
3012 	 * to the disk itself to prevent it's later resurrection as STALE.
3013 	 */
3014 	G_RAID_DEBUG(1, "Writing DDF metadata to %s",
3015 	    g_raid_get_diskname(tdisk));
3016 	i = ddf_meta_find_pd(&pd->pd_meta, NULL, GET32(&pd->pd_meta, pdd->PD_Reference));
3017 	SET16(&pd->pd_meta, pdr->entry[i].PD_State, DDF_PDE_FAILED | DDF_PDE_PFA);
3018 	if (tdisk->d_consumer != NULL)
3019 		ddf_meta_write(tdisk->d_consumer, &pd->pd_meta);
3020 
3021 	/* Change states. */
3022 	g_raid_change_disk_state(tdisk, G_RAID_DISK_S_FAILED);
3023 	TAILQ_FOREACH(sd, &tdisk->d_subdisks, sd_next) {
3024 		g_raid_change_subdisk_state(sd,
3025 		    G_RAID_SUBDISK_S_FAILED);
3026 		g_raid_event_send(sd, G_RAID_SUBDISK_E_FAILED,
3027 		    G_RAID_EVENT_SUBDISK);
3028 	}
3029 
3030 	/* Write updated metadata to remaining disks. */
3031 	g_raid_md_write_ddf(md, NULL, NULL, tdisk);
3032 
3033 	g_raid_md_ddf_refill(sc);
3034 	return (0);
3035 }
3036 
3037 static int
3038 g_raid_md_free_disk_ddf(struct g_raid_md_object *md,
3039     struct g_raid_disk *disk)
3040 {
3041 	struct g_raid_md_ddf_perdisk *pd;
3042 
3043 	pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
3044 	ddf_meta_free(&pd->pd_meta);
3045 	free(pd, M_MD_DDF);
3046 	disk->d_md_data = NULL;
3047 	return (0);
3048 }
3049 
3050 static int
3051 g_raid_md_free_volume_ddf(struct g_raid_md_object *md,
3052     struct g_raid_volume *vol)
3053 {
3054 	struct g_raid_md_ddf_object *mdi;
3055 	struct g_raid_md_ddf_pervolume *pv;
3056 
3057 	mdi = (struct g_raid_md_ddf_object *)md;
3058 	pv = (struct g_raid_md_ddf_pervolume *)vol->v_md_data;
3059 	ddf_vol_meta_free(&pv->pv_meta);
3060 	if (!pv->pv_started) {
3061 		pv->pv_started = 1;
3062 		mdi->mdio_starting--;
3063 		callout_stop(&pv->pv_start_co);
3064 	}
3065 	free(pv, M_MD_DDF);
3066 	vol->v_md_data = NULL;
3067 	return (0);
3068 }
3069 
3070 static int
3071 g_raid_md_free_ddf(struct g_raid_md_object *md)
3072 {
3073 	struct g_raid_md_ddf_object *mdi;
3074 
3075 	mdi = (struct g_raid_md_ddf_object *)md;
3076 	if (!mdi->mdio_started) {
3077 		mdi->mdio_started = 0;
3078 		callout_stop(&mdi->mdio_start_co);
3079 		G_RAID_DEBUG1(1, md->mdo_softc,
3080 		    "root_mount_rel %p", mdi->mdio_rootmount);
3081 		root_mount_rel(mdi->mdio_rootmount);
3082 		mdi->mdio_rootmount = NULL;
3083 	}
3084 	ddf_meta_free(&mdi->mdio_meta);
3085 	return (0);
3086 }
3087 
3088 G_RAID_MD_DECLARE(ddf, "DDF");
3089