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