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