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