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