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