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