md_ddf.c revision 235080
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: head/sys/geom/raid/md_ddf.c 235080 2012-05-06 12:55:20Z mav $");
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
47static 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
57struct 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
69struct 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
79struct g_raid_md_ddf_perdisk {
80	struct ddf_meta	 pd_meta;
81};
82
83struct 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
89struct 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
99static g_raid_md_create_req_t g_raid_md_create_req_ddf;
100static g_raid_md_taste_t g_raid_md_taste_ddf;
101static g_raid_md_event_t g_raid_md_event_ddf;
102static g_raid_md_volume_event_t g_raid_md_volume_event_ddf;
103static g_raid_md_ctl_t g_raid_md_ctl_ddf;
104static g_raid_md_write_t g_raid_md_write_ddf;
105static g_raid_md_fail_disk_t g_raid_md_fail_disk_ddf;
106static g_raid_md_free_disk_t g_raid_md_free_disk_ddf;
107static g_raid_md_free_volume_t g_raid_md_free_volume_ddf;
108static g_raid_md_free_t g_raid_md_free_ddf;
109
110static 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
124static 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
187static int
188isff(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
198static void
199print_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
218static void
219g_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
424static int
425ddf_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
449static int
450ddf_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
471static struct ddf_vdc_record *
472ddf_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
492static int
493ddf_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
510static int
511ddf_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
537static struct ddf_sa_record *
538ddf_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
560static void
561ddf_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
754static void
755ddf_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
781static void
782ddf_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
806static void
807ddf_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
840static void
841ddf_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
870static void
871ddf_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
928static void
929ddf_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
957static int
958ddf_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
1010static void
1011ddf_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
1024static void
1025ddf_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
1034static int
1035ddf_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
1095doread:
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) {
1102readerror:
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) {
1123hdrerror:
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
1230done:
1231	g_free(abuf);
1232	if (error != 0)
1233		ddf_meta_free(meta);
1234	return (error);
1235}
1236
1237static int
1238ddf_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
1252next:
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) {
1259err:
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
1325done:
1326	if (lba == plba && slba != -1) {
1327		lba = slba;
1328		goto next;
1329	}
1330
1331	return (error);
1332}
1333
1334static int
1335ddf_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
1353static struct g_raid_volume *
1354g_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
1367static struct g_raid_disk *
1368g_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
1388static int
1389g_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
1412static int
1413g_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
1455static int
1456g_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
1575static int
1576g_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		size = INT64_MAX;
1643		for (i = 0; i < vol->v_disks_count; i++) {
1644			sd = &vol->v_subdisks[i];
1645			if (sd->sd_state != G_RAID_SUBDISK_S_NONE)
1646				size = sd->sd_size;
1647			if (sd->sd_state <= G_RAID_SUBDISK_S_FAILED &&
1648			    (disk_pos < 0 ||
1649			     vol->v_subdisks[i].sd_state < sd->sd_state))
1650				disk_pos = i;
1651		}
1652		if (disk_pos >= 0 &&
1653		    vol->v_raid_level != G_RAID_VOLUME_RL_CONCAT &&
1654		    (off_t)esize * 512 < size) {
1655			G_RAID_DEBUG1(1, sc, "Disk %s free space "
1656			    "is too small (%ju < %ju)",
1657			    g_raid_get_diskname(disk),
1658			    (off_t)esize * 512, size);
1659			disk_pos = -1;
1660		}
1661		if (disk_pos >= 0) {
1662			if (vol->v_raid_level != G_RAID_VOLUME_RL_CONCAT)
1663				esize = size / 512;
1664			md_disk_bvd = disk_pos / GET16(vmeta, vdc->Primary_Element_Count); // XXX
1665			md_disk_pos = disk_pos % GET16(vmeta, vdc->Primary_Element_Count); // XXX
1666		} else {
1667nofit:
1668			if (disk->d_state == G_RAID_DISK_S_NONE)
1669				g_raid_change_disk_state(disk,
1670				    G_RAID_DISK_S_STALE);
1671			return (0);
1672		}
1673
1674		/*
1675		 * If spare is committable, delete spare record.
1676		 * Othersize, mark it active and leave there.
1677		 */
1678		sa = ddf_meta_find_sa(&pd->pd_meta, 0);
1679		if (sa != NULL) {
1680			if ((GET8D(&pd->pd_meta, sa->Spare_Type) &
1681			    DDF_SAR_TYPE_REVERTIBLE) == 0) {
1682				SET32D(&pd->pd_meta, sa->Signature, 0xffffffff);
1683			} else {
1684				SET8D(&pd->pd_meta, sa->Spare_Type,
1685				    GET8D(&pd->pd_meta, sa->Spare_Type) |
1686				    DDF_SAR_TYPE_ACTIVE);
1687			}
1688		}
1689
1690		G_RAID_DEBUG1(1, sc, "Disk %s takes pos %d in the volume %s",
1691		    g_raid_get_diskname(disk), disk_pos, vol->v_name);
1692		resurrection = 1;
1693	}
1694
1695	sd = &vol->v_subdisks[disk_pos];
1696
1697	if (resurrection && sd->sd_disk != NULL) {
1698		g_raid_change_disk_state(sd->sd_disk,
1699		    G_RAID_DISK_S_STALE_FAILED);
1700		TAILQ_REMOVE(&sd->sd_disk->d_subdisks,
1701		    sd, sd_next);
1702	}
1703	vol->v_subdisks[disk_pos].sd_disk = disk;
1704	TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
1705
1706	/* Welcome the new disk. */
1707	if (resurrection)
1708		g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE);
1709	else if (GET8(gmeta, pdr->entry[md_pde_pos].PD_State) & DDF_PDE_PFA)
1710		g_raid_change_disk_state(disk, G_RAID_DISK_S_FAILED);
1711	else
1712		g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE);
1713
1714	if (resurrection) {
1715		sd->sd_offset = (off_t)eoff * 512;
1716		sd->sd_size = (off_t)esize * 512;
1717	} else if (pdmeta->cr != NULL &&
1718	    (vdc1 = ddf_meta_find_vdc(pdmeta, vmeta->vdc->VD_GUID)) != NULL) {
1719		val2 = (uint64_t *)&(vdc1->Physical_Disk_Sequence[GET16(vmeta, hdr->Max_Primary_Element_Entries)]);
1720		sd->sd_offset = (off_t)GET64P(pdmeta, val2 + md_disk_pos) * 512;
1721		sd->sd_size = (off_t)GET64D(pdmeta, vdc1->Block_Count) * 512;
1722	}
1723
1724	if (resurrection) {
1725		/* Stale disk, almost same as new. */
1726		g_raid_change_subdisk_state(sd,
1727		    G_RAID_SUBDISK_S_NEW);
1728	} else if (GET8(gmeta, pdr->entry[md_pde_pos].PD_State) & DDF_PDE_PFA) {
1729		/* Failed disk. */
1730		g_raid_change_subdisk_state(sd,
1731		    G_RAID_SUBDISK_S_FAILED);
1732	} else if ((GET8(gmeta, pdr->entry[md_pde_pos].PD_State) &
1733	     (DDF_PDE_FAILED | DDF_PDE_REBUILD)) != 0) {
1734		/* Rebuilding disk. */
1735		g_raid_change_subdisk_state(sd,
1736		    G_RAID_SUBDISK_S_REBUILD);
1737		sd->sd_rebuild_pos = 0;
1738	} else if ((GET8(vmeta, vde->VD_State) & DDF_VDE_DIRTY) != 0 ||
1739	    (GET8(vmeta, vde->Init_State) & DDF_VDE_INIT_MASK) !=
1740	     DDF_VDE_INIT_FULL) {
1741		/* Stale disk or dirty volume (unclean shutdown). */
1742		g_raid_change_subdisk_state(sd,
1743		    G_RAID_SUBDISK_S_STALE);
1744	} else {
1745		/* Up to date disk. */
1746		g_raid_change_subdisk_state(sd,
1747		    G_RAID_SUBDISK_S_ACTIVE);
1748	}
1749	g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW,
1750	    G_RAID_EVENT_SUBDISK);
1751
1752	return (resurrection);
1753}
1754
1755static void
1756g_raid_md_ddf_refill(struct g_raid_softc *sc)
1757{
1758	struct g_raid_volume *vol;
1759	struct g_raid_subdisk *sd;
1760	struct g_raid_disk *disk;
1761	struct g_raid_md_object *md;
1762	struct g_raid_md_ddf_perdisk *pd;
1763	struct g_raid_md_ddf_pervolume *pv;
1764	int update, updated, i, bad;
1765
1766	md = sc->sc_md;
1767restart:
1768	updated = 0;
1769	TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
1770		pv = vol->v_md_data;
1771		if (!pv->pv_started || vol->v_stopping)
1772			continue;
1773
1774		/* Search for subdisk that needs replacement. */
1775		bad = 0;
1776		for (i = 0; i < vol->v_disks_count; i++) {
1777			sd = &vol->v_subdisks[i];
1778			if (sd->sd_state == G_RAID_SUBDISK_S_NONE ||
1779			    sd->sd_state == G_RAID_SUBDISK_S_FAILED)
1780			        bad = 1;
1781		}
1782		if (!bad)
1783			continue;
1784
1785		G_RAID_DEBUG1(1, sc, "Volume %s is not complete, "
1786		    "trying to refill.", vol->v_name);
1787
1788		TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1789			/* Skip failed. */
1790			if (disk->d_state < G_RAID_DISK_S_SPARE)
1791				continue;
1792			/* Skip already used by this volume. */
1793			for (i = 0; i < vol->v_disks_count; i++) {
1794				sd = &vol->v_subdisks[i];
1795				if (sd->sd_disk == disk)
1796					break;
1797			}
1798			if (i < vol->v_disks_count)
1799				continue;
1800
1801			/* Try to use disk if it has empty extents. */
1802			pd = disk->d_md_data;
1803			if (ddf_meta_count_vdc(&pd->pd_meta, NULL) <
1804			    GET16(&pd->pd_meta, hdr->Max_Partitions)) {
1805				update = g_raid_md_ddf_start_disk(disk, vol);
1806			} else
1807				update = 0;
1808			if (update) {
1809				updated = 1;
1810				g_raid_md_write_ddf(md, vol, NULL, disk);
1811				break;
1812			}
1813		}
1814	}
1815	if (updated)
1816		goto restart;
1817}
1818
1819static void
1820g_raid_md_ddf_start(struct g_raid_volume *vol)
1821{
1822	struct g_raid_softc *sc;
1823	struct g_raid_subdisk *sd;
1824	struct g_raid_disk *disk;
1825	struct g_raid_md_object *md;
1826	struct g_raid_md_ddf_perdisk *pd;
1827	struct g_raid_md_ddf_pervolume *pv;
1828	struct g_raid_md_ddf_object *mdi;
1829	struct ddf_vol_meta *vmeta;
1830	struct ddf_vdc_record *vdc;
1831	uint64_t *val2;
1832	int i, j, bvd;
1833
1834	sc = vol->v_softc;
1835	md = sc->sc_md;
1836	mdi = (struct g_raid_md_ddf_object *)md;
1837	pv = vol->v_md_data;
1838	vmeta = &pv->pv_meta;
1839	vdc = vmeta->vdc;
1840
1841	vol->v_raid_level = GET8(vmeta, vdc->Primary_RAID_Level);
1842	vol->v_raid_level_qualifier = GET8(vmeta, vdc->RLQ);
1843	if (GET8(vmeta, vdc->Secondary_Element_Count) > 1 &&
1844	    vol->v_raid_level == G_RAID_VOLUME_RL_RAID1 &&
1845	    GET8(vmeta, vdc->Secondary_RAID_Level) == 0)
1846		vol->v_raid_level = G_RAID_VOLUME_RL_RAID1E;
1847	vol->v_sectorsize = GET16(vmeta, vdc->Block_Size);
1848	if (vol->v_sectorsize == 0xffff)
1849		vol->v_sectorsize = vmeta->sectorsize;
1850	vol->v_strip_size = vol->v_sectorsize << GET8(vmeta, vdc->Stripe_Size);
1851	vol->v_disks_count = GET16(vmeta, vdc->Primary_Element_Count) *
1852	    GET8(vmeta, vdc->Secondary_Element_Count);
1853	vol->v_mdf_pdisks = GET8(vmeta, vdc->MDF_Parity_Disks);
1854	vol->v_mdf_polynomial = GET16(vmeta, vdc->MDF_Parity_Generator_Polynomial);
1855	vol->v_mdf_method = GET8(vmeta, vdc->MDF_Constant_Generation_Method);
1856	if (GET8(vmeta, vdc->Rotate_Parity_count) > 31)
1857		vol->v_rotate_parity = 1;
1858	else
1859		vol->v_rotate_parity = 1 << GET8(vmeta, vdc->Rotate_Parity_count);
1860	vol->v_mediasize = GET64(vmeta, vdc->VD_Size) * vol->v_sectorsize;
1861	for (i = 0, j = 0, bvd = 0; i < vol->v_disks_count; i++, j++) {
1862		if (j == GET16(vmeta, vdc->Primary_Element_Count)) {
1863			j = 0;
1864			bvd++;
1865		}
1866		sd = &vol->v_subdisks[i];
1867		if (vmeta->bvdc[bvd] == NULL) {
1868			sd->sd_offset = 0;
1869			sd->sd_size = GET64(vmeta, vdc->Block_Count) *
1870			    vol->v_sectorsize;
1871			continue;
1872		}
1873		val2 = (uint64_t *)&(vmeta->bvdc[bvd]->Physical_Disk_Sequence[
1874		    GET16(vmeta, hdr->Max_Primary_Element_Entries)]);
1875		sd->sd_offset = GET64P(vmeta, val2 + j) * vol->v_sectorsize;
1876		sd->sd_size = GET64(vmeta, bvdc[bvd]->Block_Count) *
1877		    vol->v_sectorsize;
1878	}
1879	g_raid_start_volume(vol);
1880
1881	/* Make all disks found till the moment take their places. */
1882	TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1883		pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
1884		if (ddf_meta_find_vdc(&pd->pd_meta, vmeta->vdc->VD_GUID) != NULL)
1885			g_raid_md_ddf_start_disk(disk, vol);
1886	}
1887
1888	pv->pv_started = 1;
1889	mdi->mdio_starting--;
1890	callout_stop(&pv->pv_start_co);
1891	G_RAID_DEBUG1(0, sc, "Volume started.");
1892	g_raid_md_write_ddf(md, vol, NULL, NULL);
1893
1894	/* Pickup any STALE/SPARE disks to refill array if needed. */
1895	g_raid_md_ddf_refill(sc);
1896
1897	g_raid_event_send(vol, G_RAID_VOLUME_E_START, G_RAID_EVENT_VOLUME);
1898}
1899
1900static void
1901g_raid_ddf_go(void *arg)
1902{
1903	struct g_raid_volume *vol;
1904	struct g_raid_softc *sc;
1905	struct g_raid_md_ddf_pervolume *pv;
1906
1907	vol = arg;
1908	pv = vol->v_md_data;
1909	sc = vol->v_softc;
1910	if (!pv->pv_started) {
1911		G_RAID_DEBUG1(0, sc, "Force volume start due to timeout.");
1912		g_raid_event_send(vol, G_RAID_VOLUME_E_STARTMD,
1913		    G_RAID_EVENT_VOLUME);
1914	}
1915}
1916
1917static void
1918g_raid_md_ddf_new_disk(struct g_raid_disk *disk)
1919{
1920	struct g_raid_softc *sc;
1921	struct g_raid_md_object *md;
1922	struct g_raid_md_ddf_perdisk *pd;
1923	struct g_raid_md_ddf_pervolume *pv;
1924	struct g_raid_md_ddf_object *mdi;
1925	struct g_raid_volume *vol;
1926	struct ddf_meta *pdmeta;
1927	struct ddf_vol_meta *vmeta;
1928	struct ddf_vdc_record *vdc;
1929	struct ddf_vd_entry *vde;
1930	int i, j, k, num, have, need, cnt, spare;
1931	uint32_t val;
1932	char buf[17];
1933
1934	sc = disk->d_softc;
1935	md = sc->sc_md;
1936	mdi = (struct g_raid_md_ddf_object *)md;
1937	pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
1938	pdmeta = &pd->pd_meta;
1939	spare = -1;
1940
1941	if (mdi->mdio_meta.hdr == NULL)
1942		ddf_meta_copy(&mdi->mdio_meta, pdmeta);
1943	else
1944		ddf_meta_update(&mdi->mdio_meta, pdmeta);
1945
1946	num = GETCRNUM(pdmeta);
1947	for (j = 0; j < num; j++) {
1948		vdc = GETVDCPTR(pdmeta, j);
1949		val = GET32D(pdmeta, vdc->Signature);
1950
1951		if (val == DDF_SA_SIGNATURE && spare == -1)
1952			spare = 1;
1953
1954		if (val != DDF_VDCR_SIGNATURE)
1955			continue;
1956		spare = 0;
1957		k = ddf_meta_find_vd(pdmeta, vdc->VD_GUID);
1958		if (k < 0)
1959			continue;
1960		vde = &pdmeta->vdr->entry[k];
1961
1962		/* Look for volume with matching ID. */
1963		vol = g_raid_md_ddf_get_volume(sc, vdc->VD_GUID);
1964		if (vol == NULL) {
1965			ddf_meta_get_name(pdmeta, k, buf);
1966			vol = g_raid_create_volume(sc, buf,
1967			    GET16D(pdmeta, vde->VD_Number));
1968			pv = malloc(sizeof(*pv), M_MD_DDF, M_WAITOK | M_ZERO);
1969			vol->v_md_data = pv;
1970			callout_init(&pv->pv_start_co, 1);
1971			callout_reset(&pv->pv_start_co,
1972			    g_raid_start_timeout * hz,
1973			    g_raid_ddf_go, vol);
1974			mdi->mdio_starting++;
1975		} else
1976			pv = vol->v_md_data;
1977
1978		/* If we haven't started yet - check metadata freshness. */
1979		vmeta = &pv->pv_meta;
1980		ddf_vol_meta_update(vmeta, pdmeta, vdc->VD_GUID, pv->pv_started);
1981	}
1982
1983	if (spare == 1) {
1984		g_raid_change_disk_state(disk, G_RAID_DISK_S_SPARE);
1985		g_raid_md_ddf_refill(sc);
1986	}
1987
1988	TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
1989		pv = vol->v_md_data;
1990		vmeta = &pv->pv_meta;
1991
1992		if (ddf_meta_find_vdc(pdmeta, vmeta->vdc->VD_GUID) == NULL)
1993			continue;
1994
1995		if (pv->pv_started) {
1996			if (g_raid_md_ddf_start_disk(disk, vol))
1997				g_raid_md_write_ddf(md, vol, NULL, NULL);
1998			continue;
1999		}
2000
2001		/* If we collected all needed disks - start array. */
2002		need = 0;
2003		have = 0;
2004		for (k = 0; k < GET8(vmeta, vdc->Secondary_Element_Count); k++) {
2005			if (vmeta->bvdc[k] == NULL) {
2006				need += GET16(vmeta, vdc->Primary_Element_Count);
2007				continue;
2008			}
2009			cnt = GET16(vmeta, bvdc[k]->Primary_Element_Count);
2010			need += cnt;
2011			for (i = 0; i < cnt; i++) {
2012				val = GET32(vmeta, bvdc[k]->Physical_Disk_Sequence[i]);
2013				if (g_raid_md_ddf_get_disk(sc, NULL, val) != NULL)
2014					have++;
2015			}
2016		}
2017		G_RAID_DEBUG1(1, sc, "Volume %s now has %d of %d disks",
2018		    vol->v_name, have, need);
2019		if (have == need)
2020			g_raid_md_ddf_start(vol);
2021	}
2022}
2023
2024static int
2025g_raid_md_create_req_ddf(struct g_raid_md_object *md, struct g_class *mp,
2026    struct gctl_req *req, struct g_geom **gp)
2027{
2028	struct g_geom *geom;
2029	struct g_raid_softc *sc;
2030	struct g_raid_md_ddf_object *mdi, *mdi1;
2031	char name[16];
2032	const char *fmtopt;
2033	int be = 1;
2034
2035	mdi = (struct g_raid_md_ddf_object *)md;
2036	fmtopt = gctl_get_asciiparam(req, "fmtopt");
2037	if (fmtopt == NULL || strcasecmp(fmtopt, "BE") == 0)
2038		be = 1;
2039	else if (strcasecmp(fmtopt, "LE") == 0)
2040		be = 0;
2041	else {
2042		gctl_error(req, "Incorrect fmtopt argument.");
2043		return (G_RAID_MD_TASTE_FAIL);
2044	}
2045
2046	/* Search for existing node. */
2047	LIST_FOREACH(geom, &mp->geom, geom) {
2048		sc = geom->softc;
2049		if (sc == NULL)
2050			continue;
2051		if (sc->sc_stopping != 0)
2052			continue;
2053		if (sc->sc_md->mdo_class != md->mdo_class)
2054			continue;
2055		mdi1 = (struct g_raid_md_ddf_object *)sc->sc_md;
2056		if (mdi1->mdio_bigendian != be)
2057			continue;
2058		break;
2059	}
2060	if (geom != NULL) {
2061		*gp = geom;
2062		return (G_RAID_MD_TASTE_EXISTING);
2063	}
2064
2065	/* Create new one if not found. */
2066	mdi->mdio_bigendian = be;
2067	snprintf(name, sizeof(name), "DDF%s", be ? "" : "-LE");
2068	sc = g_raid_create_node(mp, name, md);
2069	if (sc == NULL)
2070		return (G_RAID_MD_TASTE_FAIL);
2071	md->mdo_softc = sc;
2072	*gp = sc->sc_geom;
2073	return (G_RAID_MD_TASTE_NEW);
2074}
2075
2076static int
2077g_raid_md_taste_ddf(struct g_raid_md_object *md, struct g_class *mp,
2078                              struct g_consumer *cp, struct g_geom **gp)
2079{
2080	struct g_consumer *rcp;
2081	struct g_provider *pp;
2082	struct g_raid_softc *sc;
2083	struct g_raid_disk *disk;
2084	struct ddf_meta meta;
2085	struct g_raid_md_ddf_perdisk *pd;
2086	struct g_raid_md_ddf_object *mdi;
2087	struct g_geom *geom;
2088	int error, result, len, be;
2089	char name[16];
2090
2091	G_RAID_DEBUG(1, "Tasting DDF on %s", cp->provider->name);
2092	mdi = (struct g_raid_md_ddf_object *)md;
2093	pp = cp->provider;
2094
2095	/* Read metadata from device. */
2096	if (g_access(cp, 1, 0, 0) != 0)
2097		return (G_RAID_MD_TASTE_FAIL);
2098	g_topology_unlock();
2099	bzero(&meta, sizeof(meta));
2100	error = ddf_meta_read(cp, &meta);
2101	g_topology_lock();
2102	g_access(cp, -1, 0, 0);
2103	if (error != 0)
2104		return (G_RAID_MD_TASTE_FAIL);
2105	be = meta.bigendian;
2106
2107	/* Metadata valid. Print it. */
2108	g_raid_md_ddf_print(&meta);
2109
2110	/* Search for matching node. */
2111	sc = NULL;
2112	LIST_FOREACH(geom, &mp->geom, geom) {
2113		sc = geom->softc;
2114		if (sc == NULL)
2115			continue;
2116		if (sc->sc_stopping != 0)
2117			continue;
2118		if (sc->sc_md->mdo_class != md->mdo_class)
2119			continue;
2120		mdi = (struct g_raid_md_ddf_object *)sc->sc_md;
2121		if (mdi->mdio_bigendian != be)
2122			continue;
2123		break;
2124	}
2125
2126	/* Found matching node. */
2127	if (geom != NULL) {
2128		G_RAID_DEBUG(1, "Found matching array %s", sc->sc_name);
2129		result = G_RAID_MD_TASTE_EXISTING;
2130
2131	} else { /* Not found matching node -- create one. */
2132		result = G_RAID_MD_TASTE_NEW;
2133		mdi->mdio_bigendian = be;
2134		snprintf(name, sizeof(name), "DDF%s", be ? "" : "-LE");
2135		sc = g_raid_create_node(mp, name, md);
2136		md->mdo_softc = sc;
2137		geom = sc->sc_geom;
2138	}
2139
2140	rcp = g_new_consumer(geom);
2141	g_attach(rcp, pp);
2142	if (g_access(rcp, 1, 1, 1) != 0)
2143		; //goto fail1;
2144
2145	g_topology_unlock();
2146	sx_xlock(&sc->sc_lock);
2147
2148	pd = malloc(sizeof(*pd), M_MD_DDF, M_WAITOK | M_ZERO);
2149	pd->pd_meta = meta;
2150	disk = g_raid_create_disk(sc);
2151	disk->d_md_data = (void *)pd;
2152	disk->d_consumer = rcp;
2153	rcp->private = disk;
2154
2155	/* Read kernel dumping information. */
2156	disk->d_kd.offset = 0;
2157	disk->d_kd.length = OFF_MAX;
2158	len = sizeof(disk->d_kd);
2159	error = g_io_getattr("GEOM::kerneldump", rcp, &len, &disk->d_kd);
2160	if (disk->d_kd.di.dumper == NULL)
2161		G_RAID_DEBUG1(2, sc, "Dumping not supported by %s: %d.",
2162		    rcp->provider->name, error);
2163
2164	g_raid_md_ddf_new_disk(disk);
2165
2166	sx_xunlock(&sc->sc_lock);
2167	g_topology_lock();
2168	*gp = geom;
2169	return (result);
2170}
2171
2172static int
2173g_raid_md_event_ddf(struct g_raid_md_object *md,
2174    struct g_raid_disk *disk, u_int event)
2175{
2176	struct g_raid_softc *sc;
2177
2178	sc = md->mdo_softc;
2179	if (disk == NULL)
2180		return (-1);
2181	switch (event) {
2182	case G_RAID_DISK_E_DISCONNECTED:
2183		/* Delete disk. */
2184		g_raid_change_disk_state(disk, G_RAID_DISK_S_NONE);
2185		g_raid_destroy_disk(disk);
2186		g_raid_md_ddf_purge_volumes(sc);
2187
2188		/* Write updated metadata to all disks. */
2189		g_raid_md_write_ddf(md, NULL, NULL, NULL);
2190
2191		/* Check if anything left. */
2192		if (g_raid_ndisks(sc, -1) == 0)
2193			g_raid_destroy_node(sc, 0);
2194		else
2195			g_raid_md_ddf_refill(sc);
2196		return (0);
2197	}
2198	return (-2);
2199}
2200
2201static int
2202g_raid_md_volume_event_ddf(struct g_raid_md_object *md,
2203    struct g_raid_volume *vol, u_int event)
2204{
2205	struct g_raid_md_ddf_pervolume *pv;
2206
2207	pv = (struct g_raid_md_ddf_pervolume *)vol->v_md_data;
2208	switch (event) {
2209	case G_RAID_VOLUME_E_STARTMD:
2210		if (!pv->pv_started)
2211			g_raid_md_ddf_start(vol);
2212		return (0);
2213	}
2214	return (-2);
2215}
2216
2217static int
2218g_raid_md_ctl_ddf(struct g_raid_md_object *md,
2219    struct gctl_req *req)
2220{
2221	struct g_raid_softc *sc;
2222	struct g_raid_volume *vol, *vol1;
2223	struct g_raid_subdisk *sd;
2224	struct g_raid_disk *disk, *disks[DDF_MAX_DISKS_HARD];
2225	struct g_raid_md_ddf_perdisk *pd;
2226	struct g_raid_md_ddf_pervolume *pv;
2227	struct g_raid_md_ddf_object *mdi;
2228	struct ddf_sa_record *sa;
2229	struct g_consumer *cp;
2230	struct g_provider *pp;
2231	char arg[16];
2232	const char *verb, *volname, *levelname, *diskname;
2233	char *tmp;
2234	int *nargs, *force;
2235	off_t size, sectorsize, strip, offs[DDF_MAX_DISKS_HARD], esize;
2236	intmax_t *sizearg, *striparg;
2237	int i, numdisks, len, level, qual;
2238	int error;
2239
2240	sc = md->mdo_softc;
2241	mdi = (struct g_raid_md_ddf_object *)md;
2242	verb = gctl_get_param(req, "verb", NULL);
2243	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
2244	error = 0;
2245
2246	if (strcmp(verb, "label") == 0) {
2247
2248		if (*nargs < 4) {
2249			gctl_error(req, "Invalid number of arguments.");
2250			return (-1);
2251		}
2252		volname = gctl_get_asciiparam(req, "arg1");
2253		if (volname == NULL) {
2254			gctl_error(req, "No volume name.");
2255			return (-2);
2256		}
2257		levelname = gctl_get_asciiparam(req, "arg2");
2258		if (levelname == NULL) {
2259			gctl_error(req, "No RAID level.");
2260			return (-3);
2261		}
2262		if (g_raid_volume_str2level(levelname, &level, &qual)) {
2263			gctl_error(req, "Unknown RAID level '%s'.", levelname);
2264			return (-4);
2265		}
2266		numdisks = *nargs - 3;
2267		force = gctl_get_paraml(req, "force", sizeof(*force));
2268		if (!g_raid_md_ddf_supported(level, qual, numdisks,
2269		    force ? *force : 0)) {
2270			gctl_error(req, "Unsupported RAID level "
2271			    "(0x%02x/0x%02x), or number of disks (%d).",
2272			    level, qual, numdisks);
2273			return (-5);
2274		}
2275
2276		/* Search for disks, connect them and probe. */
2277		size = INT64_MAX;
2278		sectorsize = 0;
2279		bzero(disks, sizeof(disks));
2280		bzero(offs, sizeof(offs));
2281		for (i = 0; i < numdisks; i++) {
2282			snprintf(arg, sizeof(arg), "arg%d", i + 3);
2283			diskname = gctl_get_asciiparam(req, arg);
2284			if (diskname == NULL) {
2285				gctl_error(req, "No disk name (%s).", arg);
2286				error = -6;
2287				break;
2288			}
2289			if (strcmp(diskname, "NONE") == 0)
2290				continue;
2291
2292			TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2293				if (disk->d_consumer != NULL &&
2294				    disk->d_consumer->provider != NULL &&
2295				    strcmp(disk->d_consumer->provider->name,
2296				     diskname) == 0)
2297					break;
2298			}
2299			if (disk != NULL) {
2300				if (disk->d_state != G_RAID_DISK_S_ACTIVE) {
2301					gctl_error(req, "Disk '%s' is in a "
2302					    "wrong state (%s).", diskname,
2303					    g_raid_disk_state2str(disk->d_state));
2304					error = -7;
2305					break;
2306				}
2307				pd = disk->d_md_data;
2308				if (ddf_meta_count_vdc(&pd->pd_meta, NULL) >=
2309				    GET16(&pd->pd_meta, hdr->Max_Partitions)) {
2310					gctl_error(req, "No free partitions "
2311					    "on disk '%s'.",
2312					    diskname);
2313					error = -7;
2314					break;
2315				}
2316				pp = disk->d_consumer->provider;
2317				disks[i] = disk;
2318				ddf_meta_unused_range(&pd->pd_meta,
2319				    &offs[i], &esize);
2320				size = MIN(size, (off_t)esize * pp->sectorsize);
2321				sectorsize = MAX(sectorsize, pp->sectorsize);
2322				continue;
2323			}
2324
2325			g_topology_lock();
2326			cp = g_raid_open_consumer(sc, diskname);
2327			if (cp == NULL) {
2328				gctl_error(req, "Can't open disk '%s'.",
2329				    diskname);
2330				g_topology_unlock();
2331				error = -8;
2332				break;
2333			}
2334			pp = cp->provider;
2335			pd = malloc(sizeof(*pd), M_MD_DDF, M_WAITOK | M_ZERO);
2336			disk = g_raid_create_disk(sc);
2337			disk->d_md_data = (void *)pd;
2338			disk->d_consumer = cp;
2339			disks[i] = disk;
2340			cp->private = disk;
2341			ddf_meta_create(disk, &mdi->mdio_meta);
2342			if (mdi->mdio_meta.hdr == NULL)
2343				ddf_meta_copy(&mdi->mdio_meta, &pd->pd_meta);
2344			else
2345				ddf_meta_update(&mdi->mdio_meta, &pd->pd_meta);
2346			g_topology_unlock();
2347
2348			/* Read kernel dumping information. */
2349			disk->d_kd.offset = 0;
2350			disk->d_kd.length = OFF_MAX;
2351			len = sizeof(disk->d_kd);
2352			g_io_getattr("GEOM::kerneldump", cp, &len, &disk->d_kd);
2353			if (disk->d_kd.di.dumper == NULL)
2354				G_RAID_DEBUG1(2, sc,
2355				    "Dumping not supported by %s.",
2356				    cp->provider->name);
2357
2358			/* Reserve some space for metadata. */
2359			size = MIN(size, pp->mediasize - 131072llu * pp->sectorsize);
2360			sectorsize = MAX(sectorsize, pp->sectorsize);
2361		}
2362		if (error != 0) {
2363			for (i = 0; i < numdisks; i++) {
2364				if (disks[i] != NULL &&
2365				    disks[i]->d_state == G_RAID_DISK_S_NONE)
2366					g_raid_destroy_disk(disks[i]);
2367			}
2368			return (error);
2369		}
2370
2371		if (sectorsize <= 0) {
2372			gctl_error(req, "Can't get sector size.");
2373			return (-8);
2374		}
2375
2376		/* Handle size argument. */
2377		len = sizeof(*sizearg);
2378		sizearg = gctl_get_param(req, "size", &len);
2379		if (sizearg != NULL && len == sizeof(*sizearg) &&
2380		    *sizearg > 0) {
2381			if (*sizearg > size) {
2382				gctl_error(req, "Size too big %lld > %lld.",
2383				    (long long)*sizearg, (long long)size);
2384				return (-9);
2385			}
2386			size = *sizearg;
2387		}
2388
2389		/* Handle strip argument. */
2390		strip = 131072;
2391		len = sizeof(*striparg);
2392		striparg = gctl_get_param(req, "strip", &len);
2393		if (striparg != NULL && len == sizeof(*striparg) &&
2394		    *striparg > 0) {
2395			if (*striparg < sectorsize) {
2396				gctl_error(req, "Strip size too small.");
2397				return (-10);
2398			}
2399			if (*striparg % sectorsize != 0) {
2400				gctl_error(req, "Incorrect strip size.");
2401				return (-11);
2402			}
2403			strip = *striparg;
2404		}
2405
2406		/* Round size down to strip or sector. */
2407		if (level == G_RAID_VOLUME_RL_RAID1 ||
2408		    level == G_RAID_VOLUME_RL_RAID3 ||
2409		    level == G_RAID_VOLUME_RL_SINGLE ||
2410		    level == G_RAID_VOLUME_RL_CONCAT)
2411			size -= (size % sectorsize);
2412		else if (level == G_RAID_VOLUME_RL_RAID1E &&
2413		    (numdisks & 1) != 0)
2414			size -= (size % (2 * strip));
2415		else
2416			size -= (size % strip);
2417		if (size <= 0) {
2418			gctl_error(req, "Size too small.");
2419			return (-13);
2420		}
2421
2422		/* We have all we need, create things: volume, ... */
2423		pv = malloc(sizeof(*pv), M_MD_DDF, M_WAITOK | M_ZERO);
2424		ddf_vol_meta_create(&pv->pv_meta, &mdi->mdio_meta);
2425		pv->pv_started = 1;
2426		vol = g_raid_create_volume(sc, volname, -1);
2427		vol->v_md_data = pv;
2428		vol->v_raid_level = level;
2429		vol->v_raid_level_qualifier = qual;
2430		vol->v_strip_size = strip;
2431		vol->v_disks_count = numdisks;
2432		if (level == G_RAID_VOLUME_RL_RAID0 ||
2433		    level == G_RAID_VOLUME_RL_CONCAT ||
2434		    level == G_RAID_VOLUME_RL_SINGLE)
2435			vol->v_mediasize = size * numdisks;
2436		else if (level == G_RAID_VOLUME_RL_RAID1)
2437			vol->v_mediasize = size;
2438		else if (level == G_RAID_VOLUME_RL_RAID3 ||
2439		    level == G_RAID_VOLUME_RL_RAID4 ||
2440		    level == G_RAID_VOLUME_RL_RAID5)
2441			vol->v_mediasize = size * (numdisks - 1);
2442		else if (level == G_RAID_VOLUME_RL_RAID5R) {
2443			vol->v_mediasize = size * (numdisks - 1);
2444			vol->v_rotate_parity = 1024;
2445		} else if (level == G_RAID_VOLUME_RL_RAID6 ||
2446		    level == G_RAID_VOLUME_RL_RAID5E ||
2447		    level == G_RAID_VOLUME_RL_RAID5EE)
2448			vol->v_mediasize = size * (numdisks - 2);
2449		else if (level == G_RAID_VOLUME_RL_RAIDMDF) {
2450			if (numdisks < 5)
2451				vol->v_mdf_pdisks = 2;
2452			else
2453				vol->v_mdf_pdisks = 3;
2454			vol->v_mdf_polynomial = 0x11d;
2455			vol->v_mdf_method = 0x00;
2456			vol->v_mediasize = size * (numdisks - vol->v_mdf_pdisks);
2457		} else { /* RAID1E */
2458			vol->v_mediasize = ((size * numdisks) / strip / 2) *
2459			    strip;
2460		}
2461		vol->v_sectorsize = sectorsize;
2462		g_raid_start_volume(vol);
2463
2464		/* , and subdisks. */
2465		for (i = 0; i < numdisks; i++) {
2466			disk = disks[i];
2467			sd = &vol->v_subdisks[i];
2468			sd->sd_disk = disk;
2469			sd->sd_offset = (off_t)offs[i] * 512;
2470			sd->sd_size = size;
2471			if (disk == NULL)
2472				continue;
2473			TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
2474			g_raid_change_disk_state(disk,
2475			    G_RAID_DISK_S_ACTIVE);
2476			g_raid_change_subdisk_state(sd,
2477			    G_RAID_SUBDISK_S_ACTIVE);
2478			g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW,
2479			    G_RAID_EVENT_SUBDISK);
2480		}
2481
2482		/* Write metadata based on created entities. */
2483		G_RAID_DEBUG1(0, sc, "Array started.");
2484		g_raid_md_write_ddf(md, vol, NULL, NULL);
2485
2486		/* Pickup any STALE/SPARE disks to refill array if needed. */
2487		g_raid_md_ddf_refill(sc);
2488
2489		g_raid_event_send(vol, G_RAID_VOLUME_E_START,
2490		    G_RAID_EVENT_VOLUME);
2491		return (0);
2492	}
2493	if (strcmp(verb, "add") == 0) {
2494
2495		gctl_error(req, "`add` command is not applicable, "
2496		    "use `label` instead.");
2497		return (-99);
2498	}
2499	if (strcmp(verb, "delete") == 0) {
2500
2501		/* Full node destruction. */
2502		if (*nargs == 1) {
2503			/* Check if some volume is still open. */
2504			force = gctl_get_paraml(req, "force", sizeof(*force));
2505			if (force != NULL && *force == 0 &&
2506			    g_raid_nopens(sc) != 0) {
2507				gctl_error(req, "Some volume is still open.");
2508				return (-4);
2509			}
2510
2511			TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2512				if (disk->d_consumer)
2513					ddf_meta_erase(disk->d_consumer);
2514			}
2515			g_raid_destroy_node(sc, 0);
2516			return (0);
2517		}
2518
2519		/* Destroy specified volume. If it was last - all node. */
2520		if (*nargs != 2) {
2521			gctl_error(req, "Invalid number of arguments.");
2522			return (-1);
2523		}
2524		volname = gctl_get_asciiparam(req, "arg1");
2525		if (volname == NULL) {
2526			gctl_error(req, "No volume name.");
2527			return (-2);
2528		}
2529
2530		/* Search for volume. */
2531		TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2532			if (strcmp(vol->v_name, volname) == 0)
2533				break;
2534		}
2535		if (vol == NULL) {
2536			i = strtol(volname, &tmp, 10);
2537			if (verb != volname && tmp[0] == 0) {
2538				TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2539					if (vol->v_global_id == i)
2540						break;
2541				}
2542			}
2543		}
2544		if (vol == NULL) {
2545			gctl_error(req, "Volume '%s' not found.", volname);
2546			return (-3);
2547		}
2548
2549		/* Check if volume is still open. */
2550		force = gctl_get_paraml(req, "force", sizeof(*force));
2551		if (force != NULL && *force == 0 &&
2552		    vol->v_provider_open != 0) {
2553			gctl_error(req, "Volume is still open.");
2554			return (-4);
2555		}
2556
2557		/* Destroy volume and potentially node. */
2558		i = 0;
2559		TAILQ_FOREACH(vol1, &sc->sc_volumes, v_next)
2560			i++;
2561		if (i >= 2) {
2562			g_raid_destroy_volume(vol);
2563			g_raid_md_ddf_purge_disks(sc);
2564			g_raid_md_write_ddf(md, NULL, NULL, NULL);
2565		} else {
2566			TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2567				if (disk->d_consumer)
2568					ddf_meta_erase(disk->d_consumer);
2569			}
2570			g_raid_destroy_node(sc, 0);
2571		}
2572		return (0);
2573	}
2574	if (strcmp(verb, "remove") == 0 ||
2575	    strcmp(verb, "fail") == 0) {
2576		if (*nargs < 2) {
2577			gctl_error(req, "Invalid number of arguments.");
2578			return (-1);
2579		}
2580		for (i = 1; i < *nargs; i++) {
2581			snprintf(arg, sizeof(arg), "arg%d", i);
2582			diskname = gctl_get_asciiparam(req, arg);
2583			if (diskname == NULL) {
2584				gctl_error(req, "No disk name (%s).", arg);
2585				error = -2;
2586				break;
2587			}
2588			if (strncmp(diskname, "/dev/", 5) == 0)
2589				diskname += 5;
2590
2591			TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2592				if (disk->d_consumer != NULL &&
2593				    disk->d_consumer->provider != NULL &&
2594				    strcmp(disk->d_consumer->provider->name,
2595				     diskname) == 0)
2596					break;
2597			}
2598			if (disk == NULL) {
2599				gctl_error(req, "Disk '%s' not found.",
2600				    diskname);
2601				error = -3;
2602				break;
2603			}
2604
2605			if (strcmp(verb, "fail") == 0) {
2606				g_raid_md_fail_disk_ddf(md, NULL, disk);
2607				continue;
2608			}
2609
2610			/* Erase metadata on deleting disk and destroy it. */
2611			ddf_meta_erase(disk->d_consumer);
2612			g_raid_destroy_disk(disk);
2613		}
2614		g_raid_md_ddf_purge_volumes(sc);
2615
2616		/* Write updated metadata to remaining disks. */
2617		g_raid_md_write_ddf(md, NULL, NULL, NULL);
2618
2619		/* Check if anything left. */
2620		if (g_raid_ndisks(sc, -1) == 0)
2621			g_raid_destroy_node(sc, 0);
2622		else
2623			g_raid_md_ddf_refill(sc);
2624		return (error);
2625	}
2626	if (strcmp(verb, "insert") == 0) {
2627		if (*nargs < 2) {
2628			gctl_error(req, "Invalid number of arguments.");
2629			return (-1);
2630		}
2631		for (i = 1; i < *nargs; i++) {
2632			/* Get disk name. */
2633			snprintf(arg, sizeof(arg), "arg%d", i);
2634			diskname = gctl_get_asciiparam(req, arg);
2635			if (diskname == NULL) {
2636				gctl_error(req, "No disk name (%s).", arg);
2637				error = -3;
2638				break;
2639			}
2640
2641			/* Try to find provider with specified name. */
2642			g_topology_lock();
2643			cp = g_raid_open_consumer(sc, diskname);
2644			if (cp == NULL) {
2645				gctl_error(req, "Can't open disk '%s'.",
2646				    diskname);
2647				g_topology_unlock();
2648				error = -4;
2649				break;
2650			}
2651			pp = cp->provider;
2652			g_topology_unlock();
2653
2654			pd = malloc(sizeof(*pd), M_MD_DDF, M_WAITOK | M_ZERO);
2655
2656			disk = g_raid_create_disk(sc);
2657			disk->d_consumer = cp;
2658			disk->d_md_data = (void *)pd;
2659			cp->private = disk;
2660
2661			/* Read kernel dumping information. */
2662			disk->d_kd.offset = 0;
2663			disk->d_kd.length = OFF_MAX;
2664			len = sizeof(disk->d_kd);
2665			g_io_getattr("GEOM::kerneldump", cp, &len, &disk->d_kd);
2666			if (disk->d_kd.di.dumper == NULL)
2667				G_RAID_DEBUG1(2, sc,
2668				    "Dumping not supported by %s.",
2669				    cp->provider->name);
2670
2671			/* Welcome the "new" disk. */
2672			g_raid_change_disk_state(disk, G_RAID_DISK_S_SPARE);
2673			ddf_meta_create(disk, &mdi->mdio_meta);
2674			sa = ddf_meta_find_sa(&pd->pd_meta, 1);
2675			if (sa != NULL) {
2676				SET32D(&pd->pd_meta, sa->Signature,
2677				    DDF_SA_SIGNATURE);
2678				SET8D(&pd->pd_meta, sa->Spare_Type, 0);
2679				SET16D(&pd->pd_meta, sa->Populated_SAEs, 0);
2680				SET16D(&pd->pd_meta, sa->MAX_SAE_Supported,
2681				    (GET16(&pd->pd_meta, hdr->Configuration_Record_Length) *
2682				     pd->pd_meta.sectorsize -
2683				     sizeof(struct ddf_sa_record)) /
2684				    sizeof(struct ddf_sa_entry));
2685			}
2686			if (mdi->mdio_meta.hdr == NULL)
2687				ddf_meta_copy(&mdi->mdio_meta, &pd->pd_meta);
2688			else
2689				ddf_meta_update(&mdi->mdio_meta, &pd->pd_meta);
2690			g_raid_md_write_ddf(md, NULL, NULL, NULL);
2691			g_raid_md_ddf_refill(sc);
2692		}
2693		return (error);
2694	}
2695	return (-100);
2696}
2697
2698static int
2699g_raid_md_write_ddf(struct g_raid_md_object *md, struct g_raid_volume *tvol,
2700    struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk)
2701{
2702	struct g_raid_softc *sc;
2703	struct g_raid_volume *vol;
2704	struct g_raid_subdisk *sd;
2705	struct g_raid_disk *disk;
2706	struct g_raid_md_ddf_perdisk *pd;
2707	struct g_raid_md_ddf_pervolume *pv;
2708	struct g_raid_md_ddf_object *mdi;
2709	struct ddf_meta *gmeta;
2710	struct ddf_vol_meta *vmeta;
2711	struct ddf_vdc_record *vdc;
2712	struct ddf_sa_record *sa;
2713	uint64_t *val2;
2714	int i, j, pos, bvd, size;
2715
2716	sc = md->mdo_softc;
2717	mdi = (struct g_raid_md_ddf_object *)md;
2718	gmeta = &mdi->mdio_meta;
2719
2720	if (sc->sc_stopping == G_RAID_DESTROY_HARD)
2721		return (0);
2722
2723	/*
2724	 * Clear disk flags to let only really needed ones to be reset.
2725	 * Do it only if there are no volumes in starting state now,
2726	 * as they can update disk statuses yet and we may kill innocent.
2727	 */
2728	if (mdi->mdio_starting == 0) {
2729		for (i = 0; i < GET16(gmeta, pdr->Populated_PDEs); i++) {
2730			if (isff(gmeta->pdr->entry[i].PD_GUID, 24))
2731				continue;
2732			SET16(gmeta, pdr->entry[i].PD_Type,
2733			    GET16(gmeta, pdr->entry[i].PD_Type) &
2734			    ~(DDF_PDE_PARTICIPATING |
2735			      DDF_PDE_GLOBAL_SPARE | DDF_PDE_CONFIG_SPARE));
2736			if ((GET16(gmeta, pdr->entry[i].PD_State) &
2737			    DDF_PDE_PFA) == 0)
2738				SET16(gmeta, pdr->entry[i].PD_State, 0);
2739		}
2740	}
2741
2742	/* Generate/update new per-volume metadata. */
2743	TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2744		pv = (struct g_raid_md_ddf_pervolume *)vol->v_md_data;
2745		if (vol->v_stopping || !pv->pv_started)
2746			continue;
2747		vmeta = &pv->pv_meta;
2748
2749		SET32(vmeta, vdc->Sequence_Number,
2750		    GET32(vmeta, vdc->Sequence_Number) + 1);
2751		if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E &&
2752		    vol->v_disks_count % 2 == 0)
2753			SET16(vmeta, vdc->Primary_Element_Count, 2);
2754		else
2755			SET16(vmeta, vdc->Primary_Element_Count,
2756			    vol->v_disks_count);
2757		SET8(vmeta, vdc->Stripe_Size,
2758		    ffs(vol->v_strip_size / vol->v_sectorsize) - 1);
2759		if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E &&
2760		    vol->v_disks_count % 2 == 0) {
2761			SET8(vmeta, vdc->Primary_RAID_Level,
2762			    DDF_VDCR_RAID1);
2763			SET8(vmeta, vdc->RLQ, 0);
2764			SET8(vmeta, vdc->Secondary_Element_Count,
2765			    vol->v_disks_count / 2);
2766			SET8(vmeta, vdc->Secondary_RAID_Level, 0);
2767		} else {
2768			SET8(vmeta, vdc->Primary_RAID_Level,
2769			    vol->v_raid_level);
2770			SET8(vmeta, vdc->RLQ,
2771			    vol->v_raid_level_qualifier);
2772			SET8(vmeta, vdc->Secondary_Element_Count, 1);
2773			SET8(vmeta, vdc->Secondary_RAID_Level, 0);
2774		}
2775		SET8(vmeta, vdc->Secondary_Element_Seq, 0);
2776		SET64(vmeta, vdc->Block_Count, 0);
2777		SET64(vmeta, vdc->VD_Size, vol->v_mediasize / vol->v_sectorsize);
2778		SET16(vmeta, vdc->Block_Size, vol->v_sectorsize);
2779		SET8(vmeta, vdc->Rotate_Parity_count,
2780		    fls(vol->v_rotate_parity) - 1);
2781		SET8(vmeta, vdc->MDF_Parity_Disks, vol->v_mdf_pdisks);
2782		SET16(vmeta, vdc->MDF_Parity_Generator_Polynomial,
2783		    vol->v_mdf_polynomial);
2784		SET8(vmeta, vdc->MDF_Constant_Generation_Method,
2785		    vol->v_mdf_method);
2786
2787		SET16(vmeta, vde->VD_Number, vol->v_global_id);
2788		if (vol->v_state <= G_RAID_VOLUME_S_BROKEN)
2789			SET8(vmeta, vde->VD_State, DDF_VDE_FAILED);
2790		else if (vol->v_state <= G_RAID_VOLUME_S_DEGRADED)
2791			SET8(vmeta, vde->VD_State, DDF_VDE_DEGRADED);
2792		else if (vol->v_state <= G_RAID_VOLUME_S_SUBOPTIMAL)
2793			SET8(vmeta, vde->VD_State, DDF_VDE_PARTIAL);
2794		else
2795			SET8(vmeta, vde->VD_State, DDF_VDE_OPTIMAL);
2796		if (vol->v_dirty ||
2797		    g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_STALE) > 0 ||
2798		    g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_RESYNC) > 0)
2799			SET8(vmeta, vde->VD_State,
2800			    GET8(vmeta, vde->VD_State) | DDF_VDE_DIRTY);
2801		SET8(vmeta, vde->Init_State, DDF_VDE_INIT_FULL); // XXX
2802		ddf_meta_put_name(vmeta, vol->v_name);
2803
2804		for (i = 0; i < vol->v_disks_count; i++) {
2805			sd = &vol->v_subdisks[i];
2806			bvd = i / GET16(vmeta, vdc->Primary_Element_Count);
2807			pos = i % GET16(vmeta, vdc->Primary_Element_Count);
2808			disk = sd->sd_disk;
2809			if (disk != NULL) {
2810				pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
2811				if (vmeta->bvdc[bvd] == NULL) {
2812					size = GET16(vmeta,
2813					    hdr->Configuration_Record_Length) *
2814					    vmeta->sectorsize;
2815					vmeta->bvdc[bvd] = malloc(size,
2816					    M_MD_DDF, M_WAITOK);
2817					memset(vmeta->bvdc[bvd], 0xff, size);
2818				}
2819				memcpy(vmeta->bvdc[bvd], vmeta->vdc,
2820				    sizeof(struct ddf_vdc_record));
2821				SET8(vmeta, bvdc[bvd]->Secondary_Element_Seq, bvd);
2822				SET64(vmeta, bvdc[bvd]->Block_Count,
2823				    sd->sd_size / vol->v_sectorsize);
2824				SET32(vmeta, bvdc[bvd]->Physical_Disk_Sequence[pos],
2825				    GET32(&pd->pd_meta, pdd->PD_Reference));
2826				val2 = (uint64_t *)&(vmeta->bvdc[bvd]->Physical_Disk_Sequence[
2827				    GET16(vmeta, hdr->Max_Primary_Element_Entries)]);
2828				SET64P(vmeta, val2 + pos,
2829				    sd->sd_offset / vol->v_sectorsize);
2830			}
2831			if (vmeta->bvdc[bvd] == NULL)
2832				continue;
2833
2834			j = ddf_meta_find_pd(gmeta, NULL,
2835			    GET32(vmeta, bvdc[bvd]->Physical_Disk_Sequence[pos]));
2836			if (j < 0)
2837				continue;
2838			SET32(gmeta, pdr->entry[j].PD_Type,
2839			    GET32(gmeta, pdr->entry[j].PD_Type) |
2840			    DDF_PDE_PARTICIPATING);
2841			if (sd->sd_state == G_RAID_SUBDISK_S_NONE)
2842				SET32(gmeta, pdr->entry[j].PD_State,
2843				    GET32(gmeta, pdr->entry[j].PD_State) |
2844				    (DDF_PDE_FAILED | DDF_PDE_MISSING));
2845			else if (sd->sd_state == G_RAID_SUBDISK_S_FAILED)
2846				SET32(gmeta, pdr->entry[j].PD_State,
2847				    GET32(gmeta, pdr->entry[j].PD_State) |
2848				    (DDF_PDE_FAILED | DDF_PDE_PFA));
2849			else if (sd->sd_state <= G_RAID_SUBDISK_S_REBUILD)
2850				SET32(gmeta, pdr->entry[j].PD_State,
2851				    GET32(gmeta, pdr->entry[j].PD_State) |
2852				    DDF_PDE_REBUILD);
2853			else
2854				SET32(gmeta, pdr->entry[j].PD_State,
2855				    GET32(gmeta, pdr->entry[j].PD_State) |
2856				    DDF_PDE_ONLINE);
2857		}
2858	}
2859
2860	/* Mark spare and failed disks as such. */
2861	TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2862		pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
2863		i = ddf_meta_find_pd(gmeta, NULL,
2864		    GET32(&pd->pd_meta, pdd->PD_Reference));
2865		if (i < 0)
2866			continue;
2867		if (disk->d_state == G_RAID_DISK_S_FAILED) {
2868			SET32(gmeta, pdr->entry[i].PD_State,
2869			    GET32(gmeta, pdr->entry[i].PD_State) |
2870			    (DDF_PDE_FAILED | DDF_PDE_PFA));
2871		}
2872		if (disk->d_state != G_RAID_DISK_S_SPARE)
2873			continue;
2874		sa = ddf_meta_find_sa(&pd->pd_meta, 0);
2875		if (sa == NULL ||
2876		    (GET8D(&pd->pd_meta, sa->Spare_Type) &
2877		     DDF_SAR_TYPE_DEDICATED) == 0) {
2878			SET16(gmeta, pdr->entry[i].PD_Type,
2879			    GET16(gmeta, pdr->entry[i].PD_Type) |
2880			    DDF_PDE_GLOBAL_SPARE);
2881		} else {
2882			SET16(gmeta, pdr->entry[i].PD_Type,
2883			    GET16(gmeta, pdr->entry[i].PD_Type) |
2884			    DDF_PDE_CONFIG_SPARE);
2885		}
2886		SET32(gmeta, pdr->entry[i].PD_State,
2887		    GET32(gmeta, pdr->entry[i].PD_State) |
2888		    DDF_PDE_ONLINE);
2889	}
2890
2891	/* Remove disks without "participating" flag (unused). */
2892	for (i = 0, j = -1; i < GET16(gmeta, pdr->Populated_PDEs); i++) {
2893		if (isff(gmeta->pdr->entry[i].PD_GUID, 24))
2894			continue;
2895		if ((GET16(gmeta, pdr->entry[i].PD_Type) &
2896		    (DDF_PDE_PARTICIPATING |
2897		     DDF_PDE_GLOBAL_SPARE | DDF_PDE_CONFIG_SPARE)) != 0 ||
2898		    g_raid_md_ddf_get_disk(sc,
2899		     NULL, GET32(gmeta, pdr->entry[i].PD_Reference)) != NULL)
2900			j = i;
2901		else
2902			memset(&gmeta->pdr->entry[i], 0xff,
2903			    sizeof(struct ddf_pd_entry));
2904	}
2905	SET16(gmeta, pdr->Populated_PDEs, j + 1);
2906
2907	/* Update per-disk metadata and write them. */
2908	TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2909		pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
2910		if (disk->d_state != G_RAID_DISK_S_ACTIVE &&
2911		    disk->d_state != G_RAID_DISK_S_SPARE)
2912			continue;
2913		/* Update PDR. */
2914		memcpy(pd->pd_meta.pdr, gmeta->pdr,
2915		    GET32(&pd->pd_meta, hdr->pdr_length) *
2916		    pd->pd_meta.sectorsize);
2917		/* Update VDR. */
2918		SET16(&pd->pd_meta, vdr->Populated_VDEs, 0);
2919		TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2920			if (vol->v_stopping)
2921				continue;
2922			pv = (struct g_raid_md_ddf_pervolume *)vol->v_md_data;
2923			i = ddf_meta_find_vd(&pd->pd_meta,
2924			    pv->pv_meta.vde->VD_GUID);
2925			if (i < 0)
2926				i = ddf_meta_find_vd(&pd->pd_meta, NULL);
2927			if (i >= 0)
2928				memcpy(&pd->pd_meta.vdr->entry[i],
2929				    pv->pv_meta.vde,
2930				    sizeof(struct ddf_vd_entry));
2931		}
2932		/* Update VDC. */
2933		if (mdi->mdio_starting == 0) {
2934			/* Remove all VDCs to restore needed later. */
2935			j = GETCRNUM(&pd->pd_meta);
2936			for (i = 0; i < j; i++) {
2937				vdc = GETVDCPTR(&pd->pd_meta, i);
2938				if (GET32D(&pd->pd_meta, vdc->Signature) !=
2939				    DDF_VDCR_SIGNATURE)
2940					continue;
2941				SET32D(&pd->pd_meta, vdc->Signature, 0xffffffff);
2942			}
2943		}
2944		TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
2945			vol = sd->sd_volume;
2946			if (vol->v_stopping)
2947				continue;
2948			pv = (struct g_raid_md_ddf_pervolume *)vol->v_md_data;
2949			vmeta = &pv->pv_meta;
2950			vdc = ddf_meta_find_vdc(&pd->pd_meta,
2951			    vmeta->vde->VD_GUID);
2952			if (vdc == NULL)
2953				vdc = ddf_meta_find_vdc(&pd->pd_meta, NULL);
2954			if (vdc != NULL) {
2955				bvd = sd->sd_pos / GET16(vmeta,
2956				    vdc->Primary_Element_Count);
2957				memcpy(vdc, vmeta->bvdc[bvd],
2958				    GET16(&pd->pd_meta,
2959				    hdr->Configuration_Record_Length) *
2960				    pd->pd_meta.sectorsize);
2961			}
2962		}
2963		G_RAID_DEBUG(1, "Writing DDF metadata to %s",
2964		    g_raid_get_diskname(disk));
2965		g_raid_md_ddf_print(&pd->pd_meta);
2966		ddf_meta_write(disk->d_consumer, &pd->pd_meta);
2967	}
2968	return (0);
2969}
2970
2971static int
2972g_raid_md_fail_disk_ddf(struct g_raid_md_object *md,
2973    struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk)
2974{
2975	struct g_raid_softc *sc;
2976	struct g_raid_md_ddf_perdisk *pd;
2977	struct g_raid_subdisk *sd;
2978	int i;
2979
2980	sc = md->mdo_softc;
2981	pd = (struct g_raid_md_ddf_perdisk *)tdisk->d_md_data;
2982
2983	/* We can't fail disk that is not a part of array now. */
2984	if (tdisk->d_state != G_RAID_DISK_S_ACTIVE)
2985		return (-1);
2986
2987	/*
2988	 * Mark disk as failed in metadata and try to write that metadata
2989	 * to the disk itself to prevent it's later resurrection as STALE.
2990	 */
2991	G_RAID_DEBUG(1, "Writing DDF metadata to %s",
2992	    g_raid_get_diskname(tdisk));
2993	i = ddf_meta_find_pd(&pd->pd_meta, NULL, GET32(&pd->pd_meta, pdd->PD_Reference));
2994	SET16(&pd->pd_meta, pdr->entry[i].PD_State, DDF_PDE_FAILED | DDF_PDE_PFA);
2995	if (tdisk->d_consumer != NULL)
2996		ddf_meta_write(tdisk->d_consumer, &pd->pd_meta);
2997
2998	/* Change states. */
2999	g_raid_change_disk_state(tdisk, G_RAID_DISK_S_FAILED);
3000	TAILQ_FOREACH(sd, &tdisk->d_subdisks, sd_next) {
3001		g_raid_change_subdisk_state(sd,
3002		    G_RAID_SUBDISK_S_FAILED);
3003		g_raid_event_send(sd, G_RAID_SUBDISK_E_FAILED,
3004		    G_RAID_EVENT_SUBDISK);
3005	}
3006
3007	/* Write updated metadata to remaining disks. */
3008	g_raid_md_write_ddf(md, NULL, NULL, tdisk);
3009
3010	g_raid_md_ddf_refill(sc);
3011	return (0);
3012}
3013
3014static int
3015g_raid_md_free_disk_ddf(struct g_raid_md_object *md,
3016    struct g_raid_disk *disk)
3017{
3018	struct g_raid_md_ddf_perdisk *pd;
3019
3020	pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
3021	ddf_meta_free(&pd->pd_meta);
3022	free(pd, M_MD_DDF);
3023	disk->d_md_data = NULL;
3024	return (0);
3025}
3026
3027static int
3028g_raid_md_free_volume_ddf(struct g_raid_md_object *md,
3029    struct g_raid_volume *vol)
3030{
3031	struct g_raid_md_ddf_object *mdi;
3032	struct g_raid_md_ddf_pervolume *pv;
3033
3034	mdi = (struct g_raid_md_ddf_object *)md;
3035	pv = (struct g_raid_md_ddf_pervolume *)vol->v_md_data;
3036	ddf_vol_meta_free(&pv->pv_meta);
3037	if (!pv->pv_started) {
3038		pv->pv_started = 1;
3039		mdi->mdio_starting--;
3040		callout_stop(&pv->pv_start_co);
3041	}
3042	free(pv, M_MD_DDF);
3043	vol->v_md_data = NULL;
3044	return (0);
3045}
3046
3047static int
3048g_raid_md_free_ddf(struct g_raid_md_object *md)
3049{
3050	struct g_raid_md_ddf_object *mdi;
3051
3052	mdi = (struct g_raid_md_ddf_object *)md;
3053	if (!mdi->mdio_started) {
3054		mdi->mdio_started = 0;
3055		callout_stop(&mdi->mdio_start_co);
3056		G_RAID_DEBUG1(1, md->mdo_softc,
3057		    "root_mount_rel %p", mdi->mdio_rootmount);
3058		root_mount_rel(mdi->mdio_rootmount);
3059		mdi->mdio_rootmount = NULL;
3060	}
3061	ddf_meta_free(&mdi->mdio_meta);
3062	return (0);
3063}
3064
3065G_RAID_MD_DECLARE(g_raid_md_ddf);
3066