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