2 * Copyright (c) 2012 Alexander Motin <mav@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/endian.h>
33 #include <sys/kernel.h>
35 #include <sys/limits.h>
37 #include <sys/malloc.h>
38 #include <sys/mutex.h>
39 #include <sys/systm.h>
41 #include <sys/clock.h>
42 #include <geom/geom.h>
43 #include "geom/raid/g_raid.h"
44 #include "geom/raid/md_ddf.h"
45 #include "g_raid_md_if.h"
47 static MALLOC_DEFINE(M_MD_DDF, "md_ddf_data", "GEOM_RAID DDF metadata");
49 #define DDF_MAX_DISKS_HARD 128
51 #define DDF_MAX_DISKS 16
52 #define DDF_MAX_VDISKS 7
53 #define DDF_MAX_PARTITIONS 1
55 #define DECADE (3600*24*(365*10+2)) /* 10 years in seconds. */
60 struct ddf_header *hdr;
61 struct ddf_cd_record *cdr;
62 struct ddf_pd_record *pdr;
63 struct ddf_vd_record *vdr;
65 struct ddf_pdd_record *pdd;
66 struct ddf_bbm_log *bbm;
72 struct ddf_header *hdr;
73 struct ddf_cd_record *cdr;
74 struct ddf_vd_entry *vde;
75 struct ddf_vdc_record *vdc;
76 struct ddf_vdc_record *bvdc[DDF_MAX_DISKS_HARD];
79 struct g_raid_md_ddf_perdisk {
80 struct ddf_meta pd_meta;
83 struct g_raid_md_ddf_pervolume {
84 struct ddf_vol_meta pv_meta;
86 struct callout pv_start_co; /* STARTING state timer. */
89 struct g_raid_md_ddf_object {
90 struct g_raid_md_object mdio_base;
92 struct ddf_meta mdio_meta;
94 struct callout mdio_start_co; /* STARTING state timer. */
96 struct root_hold_token *mdio_rootmount; /* Root mount delay token. */
99 static g_raid_md_create_req_t g_raid_md_create_req_ddf;
100 static g_raid_md_taste_t g_raid_md_taste_ddf;
101 static g_raid_md_event_t g_raid_md_event_ddf;
102 static g_raid_md_volume_event_t g_raid_md_volume_event_ddf;
103 static g_raid_md_ctl_t g_raid_md_ctl_ddf;
104 static g_raid_md_write_t g_raid_md_write_ddf;
105 static g_raid_md_fail_disk_t g_raid_md_fail_disk_ddf;
106 static g_raid_md_free_disk_t g_raid_md_free_disk_ddf;
107 static g_raid_md_free_volume_t g_raid_md_free_volume_ddf;
108 static g_raid_md_free_t g_raid_md_free_ddf;
110 static kobj_method_t g_raid_md_ddf_methods[] = {
111 KOBJMETHOD(g_raid_md_create_req, g_raid_md_create_req_ddf),
112 KOBJMETHOD(g_raid_md_taste, g_raid_md_taste_ddf),
113 KOBJMETHOD(g_raid_md_event, g_raid_md_event_ddf),
114 KOBJMETHOD(g_raid_md_volume_event, g_raid_md_volume_event_ddf),
115 KOBJMETHOD(g_raid_md_ctl, g_raid_md_ctl_ddf),
116 KOBJMETHOD(g_raid_md_write, g_raid_md_write_ddf),
117 KOBJMETHOD(g_raid_md_fail_disk, g_raid_md_fail_disk_ddf),
118 KOBJMETHOD(g_raid_md_free_disk, g_raid_md_free_disk_ddf),
119 KOBJMETHOD(g_raid_md_free_volume, g_raid_md_free_volume_ddf),
120 KOBJMETHOD(g_raid_md_free, g_raid_md_free_ddf),
124 static struct g_raid_md_class g_raid_md_ddf_class = {
126 g_raid_md_ddf_methods,
127 sizeof(struct g_raid_md_ddf_object),
132 #define GET8(m, f) ((m)->f)
133 #define GET16(m, f) ((m)->bigendian ? be16dec(&(m)->f) : le16dec(&(m)->f))
134 #define GET32(m, f) ((m)->bigendian ? be32dec(&(m)->f) : le32dec(&(m)->f))
135 #define GET64(m, f) ((m)->bigendian ? be64dec(&(m)->f) : le64dec(&(m)->f))
136 #define GET8D(m, f) (f)
137 #define GET16D(m, f) ((m)->bigendian ? be16dec(&f) : le16dec(&f))
138 #define GET32D(m, f) ((m)->bigendian ? be32dec(&f) : le32dec(&f))
139 #define GET64D(m, f) ((m)->bigendian ? be64dec(&f) : le64dec(&f))
140 #define GET8P(m, f) (*(f))
141 #define GET16P(m, f) ((m)->bigendian ? be16dec(f) : le16dec(f))
142 #define GET32P(m, f) ((m)->bigendian ? be32dec(f) : le32dec(f))
143 #define GET64P(m, f) ((m)->bigendian ? be64dec(f) : le64dec(f))
145 #define SET8P(m, f, v) \
147 #define SET16P(m, f, v) \
149 if ((m)->bigendian) \
154 #define SET32P(m, f, v) \
156 if ((m)->bigendian) \
161 #define SET64P(m, f, v) \
163 if ((m)->bigendian) \
168 #define SET8(m, f, v) SET8P((m), &((m)->f), (v))
169 #define SET16(m, f, v) SET16P((m), &((m)->f), (v))
170 #define SET32(m, f, v) SET32P((m), &((m)->f), (v))
171 #define SET64(m, f, v) SET64P((m), &((m)->f), (v))
172 #define SET8D(m, f, v) SET8P((m), &(f), (v))
173 #define SET16D(m, f, v) SET16P((m), &(f), (v))
174 #define SET32D(m, f, v) SET32P((m), &(f), (v))
175 #define SET64D(m, f, v) SET64P((m), &(f), (v))
177 #define GETCRNUM(m) (GET32((m), hdr->cr_length) / \
178 GET16((m), hdr->Configuration_Record_Length))
180 #define GETVDCPTR(m, n) ((struct ddf_vdc_record *)((uint8_t *)(m)->cr + \
181 (n) * GET16((m), hdr->Configuration_Record_Length) * \
184 #define GETSAPTR(m, n) ((struct ddf_sa_record *)((uint8_t *)(m)->cr + \
185 (n) * GET16((m), hdr->Configuration_Record_Length) * \
189 isff(uint8_t *buf, int size)
193 for (i = 0; i < size; i++)
200 print_guid(uint8_t *buf)
205 for (i = 0; i < 24; i++) {
206 if (buf[i] != 0 && (buf[i] < ' ' || buf[i] > 127)) {
212 printf("'%.24s'", buf);
214 for (i = 0; i < 24; i++)
215 printf("%02x", buf[i]);
220 g_raid_md_ddf_print(struct ddf_meta *meta)
222 struct ddf_vdc_record *vdc;
223 struct ddf_vuc_record *vuc;
224 struct ddf_sa_record *sa;
227 int i, j, k, num, num2;
229 if (g_raid_debug < 1)
232 printf("********* DDF Metadata *********\n");
233 printf("**** Header ****\n");
234 printf("DDF_Header_GUID ");
235 print_guid(meta->hdr->DDF_Header_GUID);
237 printf("DDF_rev %8.8s\n", (char *)&meta->hdr->DDF_rev[0]);
238 printf("Sequence_Number 0x%08x\n", GET32(meta, hdr->Sequence_Number));
239 printf("TimeStamp 0x%08x\n", GET32(meta, hdr->TimeStamp));
240 printf("Open_Flag 0x%02x\n", GET16(meta, hdr->Open_Flag));
241 printf("Foreign_Flag 0x%02x\n", GET16(meta, hdr->Foreign_Flag));
242 printf("Diskgrouping 0x%02x\n", GET16(meta, hdr->Diskgrouping));
243 printf("Primary_Header_LBA %ju\n", GET64(meta, hdr->Primary_Header_LBA));
244 printf("Secondary_Header_LBA %ju\n", GET64(meta, hdr->Secondary_Header_LBA));
245 printf("WorkSpace_Length %u\n", GET32(meta, hdr->WorkSpace_Length));
246 printf("WorkSpace_LBA %ju\n", GET64(meta, hdr->WorkSpace_LBA));
247 printf("Max_PD_Entries %u\n", GET16(meta, hdr->Max_PD_Entries));
248 printf("Max_VD_Entries %u\n", GET16(meta, hdr->Max_VD_Entries));
249 printf("Max_Partitions %u\n", GET16(meta, hdr->Max_Partitions));
250 printf("Configuration_Record_Length %u\n", GET16(meta, hdr->Configuration_Record_Length));
251 printf("Max_Primary_Element_Entries %u\n", GET16(meta, hdr->Max_Primary_Element_Entries));
252 printf("Controller Data %u:%u\n", GET32(meta, hdr->cd_section), GET32(meta, hdr->cd_length));
253 printf("Physical Disk %u:%u\n", GET32(meta, hdr->pdr_section), GET32(meta, hdr->pdr_length));
254 printf("Virtual Disk %u:%u\n", GET32(meta, hdr->vdr_section), GET32(meta, hdr->vdr_length));
255 printf("Configuration Recs %u:%u\n", GET32(meta, hdr->cr_section), GET32(meta, hdr->cr_length));
256 printf("Physical Disk Recs %u:%u\n", GET32(meta, hdr->pdd_section), GET32(meta, hdr->pdd_length));
257 printf("BBM Log %u:%u\n", GET32(meta, hdr->bbmlog_section), GET32(meta, hdr->bbmlog_length));
258 printf("Diagnostic Space %u:%u\n", GET32(meta, hdr->Diagnostic_Space), GET32(meta, hdr->Diagnostic_Space_Length));
259 printf("Vendor_Specific_Logs %u:%u\n", GET32(meta, hdr->Vendor_Specific_Logs), GET32(meta, hdr->Vendor_Specific_Logs_Length));
260 printf("**** Controller Data ****\n");
261 printf("Controller_GUID ");
262 print_guid(meta->cdr->Controller_GUID);
264 printf("Controller_Type 0x%04x%04x 0x%04x%04x\n",
265 GET16(meta, cdr->Controller_Type.Vendor_ID),
266 GET16(meta, cdr->Controller_Type.Device_ID),
267 GET16(meta, cdr->Controller_Type.SubVendor_ID),
268 GET16(meta, cdr->Controller_Type.SubDevice_ID));
269 printf("Product_ID '%.16s'\n", (char *)&meta->cdr->Product_ID[0]);
270 printf("**** Physical Disk Records ****\n");
271 printf("Populated_PDEs %u\n", GET16(meta, pdr->Populated_PDEs));
272 printf("Max_PDE_Supported %u\n", GET16(meta, pdr->Max_PDE_Supported));
273 for (j = 0; j < GET16(meta, pdr->Populated_PDEs); j++) {
274 if (isff(meta->pdr->entry[j].PD_GUID, 24))
276 if (GET32(meta, pdr->entry[j].PD_Reference) == 0xffffffff)
279 print_guid(meta->pdr->entry[j].PD_GUID);
281 printf("PD_Reference 0x%08x\n",
282 GET32(meta, pdr->entry[j].PD_Reference));
283 printf("PD_Type 0x%04x\n",
284 GET16(meta, pdr->entry[j].PD_Type));
285 printf("PD_State 0x%04x\n",
286 GET16(meta, pdr->entry[j].PD_State));
287 printf("Configured_Size %ju\n",
288 GET64(meta, pdr->entry[j].Configured_Size));
289 printf("Block_Size %u\n",
290 GET16(meta, pdr->entry[j].Block_Size));
292 printf("**** Virtual Disk Records ****\n");
293 printf("Populated_VDEs %u\n", GET16(meta, vdr->Populated_VDEs));
294 printf("Max_VDE_Supported %u\n", GET16(meta, vdr->Max_VDE_Supported));
295 for (j = 0; j < GET16(meta, vdr->Populated_VDEs); j++) {
296 if (isff(meta->vdr->entry[j].VD_GUID, 24))
299 print_guid(meta->vdr->entry[j].VD_GUID);
301 printf("VD_Number 0x%04x\n",
302 GET16(meta, vdr->entry[j].VD_Number));
303 printf("VD_Type 0x%04x\n",
304 GET16(meta, vdr->entry[j].VD_Type));
305 printf("VD_State 0x%02x\n",
306 GET8(meta, vdr->entry[j].VD_State));
307 printf("Init_State 0x%02x\n",
308 GET8(meta, vdr->entry[j].Init_State));
309 printf("Drive_Failures_Remaining %u\n",
310 GET8(meta, vdr->entry[j].Drive_Failures_Remaining));
311 printf("VD_Name '%.16s'\n",
312 (char *)&meta->vdr->entry[j].VD_Name);
314 printf("**** Configuration Records ****\n");
315 num = GETCRNUM(meta);
316 for (j = 0; j < num; j++) {
317 vdc = GETVDCPTR(meta, j);
318 val = GET32D(meta, vdc->Signature);
320 case DDF_VDCR_SIGNATURE:
321 printf("** Virtual Disk Configuration **\n");
323 print_guid(vdc->VD_GUID);
325 printf("Timestamp 0x%08x\n",
326 GET32D(meta, vdc->Timestamp));
327 printf("Sequence_Number 0x%08x\n",
328 GET32D(meta, vdc->Sequence_Number));
329 printf("Primary_Element_Count %u\n",
330 GET16D(meta, vdc->Primary_Element_Count));
331 printf("Stripe_Size %u\n",
332 GET8D(meta, vdc->Stripe_Size));
333 printf("Primary_RAID_Level 0x%02x\n",
334 GET8D(meta, vdc->Primary_RAID_Level));
335 printf("RLQ 0x%02x\n",
336 GET8D(meta, vdc->RLQ));
337 printf("Secondary_Element_Count %u\n",
338 GET8D(meta, vdc->Secondary_Element_Count));
339 printf("Secondary_Element_Seq %u\n",
340 GET8D(meta, vdc->Secondary_Element_Seq));
341 printf("Secondary_RAID_Level 0x%02x\n",
342 GET8D(meta, vdc->Secondary_RAID_Level));
343 printf("Block_Count %ju\n",
344 GET64D(meta, vdc->Block_Count));
345 printf("VD_Size %ju\n",
346 GET64D(meta, vdc->VD_Size));
347 printf("Block_Size %u\n",
348 GET16D(meta, vdc->Block_Size));
349 printf("Rotate_Parity_count %u\n",
350 GET8D(meta, vdc->Rotate_Parity_count));
351 printf("Associated_Spare_Disks");
352 for (i = 0; i < 8; i++) {
353 if (GET32D(meta, vdc->Associated_Spares[i]) != 0xffffffff)
354 printf(" 0x%08x", GET32D(meta, vdc->Associated_Spares[i]));
357 printf("Cache_Flags %016jx\n",
358 GET64D(meta, vdc->Cache_Flags));
359 printf("BG_Rate %u\n",
360 GET8D(meta, vdc->BG_Rate));
361 printf("MDF_Parity_Disks %u\n",
362 GET8D(meta, vdc->MDF_Parity_Disks));
363 printf("MDF_Parity_Generator_Polynomial 0x%04x\n",
364 GET16D(meta, vdc->MDF_Parity_Generator_Polynomial));
365 printf("MDF_Constant_Generation_Method 0x%02x\n",
366 GET8D(meta, vdc->MDF_Constant_Generation_Method));
367 printf("Physical_Disks ");
368 num2 = GET16D(meta, vdc->Primary_Element_Count);
369 val2 = (uint64_t *)&(vdc->Physical_Disk_Sequence[GET16(meta, hdr->Max_Primary_Element_Entries)]);
370 for (i = 0; i < num2; i++)
371 printf(" 0x%08x @ %ju",
372 GET32D(meta, vdc->Physical_Disk_Sequence[i]),
373 GET64P(meta, val2 + i));
376 case DDF_VUCR_SIGNATURE:
377 printf("** Vendor Unique Configuration **\n");
378 vuc = (struct ddf_vuc_record *)vdc;
380 print_guid(vuc->VD_GUID);
383 case DDF_SA_SIGNATURE:
384 printf("** Spare Assignment Configuration **\n");
385 sa = (struct ddf_sa_record *)vdc;
386 printf("Timestamp 0x%08x\n",
387 GET32D(meta, sa->Timestamp));
388 printf("Spare_Type 0x%02x\n",
389 GET8D(meta, sa->Spare_Type));
390 printf("Populated_SAEs %u\n",
391 GET16D(meta, sa->Populated_SAEs));
392 printf("MAX_SAE_Supported %u\n",
393 GET16D(meta, sa->MAX_SAE_Supported));
394 for (i = 0; i < GET16D(meta, sa->Populated_SAEs); i++) {
395 if (isff(sa->entry[i].VD_GUID, 24))
398 for (k = 0; k < 24; k++)
399 printf("%02x", sa->entry[i].VD_GUID[k]);
401 printf("Secondary_Element %u\n",
402 GET16D(meta, sa->entry[i].Secondary_Element));
409 printf("Unknown configuration signature %08x\n", val);
413 printf("**** Physical Disk Data ****\n");
415 print_guid(meta->pdd->PD_GUID);
417 printf("PD_Reference 0x%08x\n",
418 GET32(meta, pdd->PD_Reference));
419 printf("Forced_Ref_Flag 0x%02x\n",
420 GET8(meta, pdd->Forced_Ref_Flag));
421 printf("Forced_PD_GUID_Flag 0x%02x\n",
422 GET8(meta, pdd->Forced_PD_GUID_Flag));
426 ddf_meta_find_pd(struct ddf_meta *meta, uint8_t *GUID, uint32_t PD_Reference)
430 for (i = 0; i < GET16(meta, pdr->Populated_PDEs); i++) {
432 if (memcmp(meta->pdr->entry[i].PD_GUID, GUID, 24) == 0)
434 } else if (PD_Reference != 0xffffffff) {
435 if (GET32(meta, pdr->entry[i].PD_Reference) == PD_Reference)
438 if (isff(meta->pdr->entry[i].PD_GUID, 24))
441 if (GUID == NULL && PD_Reference == 0xffffffff) {
442 if (i >= GET16(meta, pdr->Max_PDE_Supported))
444 SET16(meta, pdr->Populated_PDEs, i + 1);
451 ddf_meta_find_vd(struct ddf_meta *meta, uint8_t *GUID)
455 for (i = 0; i < GET16(meta, vdr->Populated_VDEs); i++) {
457 if (memcmp(meta->vdr->entry[i].VD_GUID, GUID, 24) == 0)
460 if (isff(meta->vdr->entry[i].VD_GUID, 24))
464 if (i >= GET16(meta, vdr->Max_VDE_Supported))
466 SET16(meta, vdr->Populated_VDEs, i + 1);
472 static struct ddf_vdc_record *
473 ddf_meta_find_vdc(struct ddf_meta *meta, uint8_t *GUID)
475 struct ddf_vdc_record *vdc;
478 num = GETCRNUM(meta);
479 for (i = 0; i < num; i++) {
480 vdc = GETVDCPTR(meta, i);
482 if (GET32D(meta, vdc->Signature) == DDF_VDCR_SIGNATURE &&
483 memcmp(vdc->VD_GUID, GUID, 24) == 0)
486 if (GET32D(meta, vdc->Signature) == 0xffffffff ||
487 GET32D(meta, vdc->Signature) == 0)
494 ddf_meta_count_vdc(struct ddf_meta *meta, uint8_t *GUID)
496 struct ddf_vdc_record *vdc;
500 num = GETCRNUM(meta);
501 for (i = 0; i < num; i++) {
502 vdc = GETVDCPTR(meta, i);
503 if (GET32D(meta, vdc->Signature) != DDF_VDCR_SIGNATURE)
505 if (GUID == NULL || memcmp(vdc->VD_GUID, GUID, 24) == 0)
512 ddf_meta_find_disk(struct ddf_vol_meta *vmeta, uint32_t PD_Reference,
513 int *bvdp, int *posp)
518 for (bvd = 0; bvd < GET8(vmeta, vdc->Secondary_Element_Count); bvd++) {
519 if (vmeta->bvdc[bvd] == NULL) {
520 i += GET16(vmeta, vdc->Primary_Element_Count); // XXX
523 for (pos = 0; pos < GET16(vmeta, bvdc[bvd]->Primary_Element_Count);
525 if (GET32(vmeta, bvdc[bvd]->Physical_Disk_Sequence[pos]) ==
538 static struct ddf_sa_record *
539 ddf_meta_find_sa(struct ddf_meta *meta, int create)
541 struct ddf_sa_record *sa;
544 num = GETCRNUM(meta);
545 for (i = 0; i < num; i++) {
546 sa = GETSAPTR(meta, i);
547 if (GET32D(meta, sa->Signature) == DDF_SA_SIGNATURE)
551 for (i = 0; i < num; i++) {
552 sa = GETSAPTR(meta, i);
553 if (GET32D(meta, sa->Signature) == 0xffffffff ||
554 GET32D(meta, sa->Signature) == 0)
562 ddf_meta_create(struct g_raid_disk *disk, struct ddf_meta *sample)
566 struct g_raid_md_ddf_perdisk *pd;
567 struct g_raid_md_ddf_object *mdi;
568 struct ddf_meta *meta;
569 struct ddf_pd_entry *pde;
573 char serial_buffer[24];
575 if (sample->hdr == NULL)
578 mdi = (struct g_raid_md_ddf_object *)disk->d_softc->sc_md;
579 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
581 ss = disk->d_consumer->provider->sectorsize;
582 anchorlba = disk->d_consumer->provider->mediasize / ss - 1;
584 meta->sectorsize = ss;
585 meta->bigendian = sample ? sample->bigendian : mdi->mdio_bigendian;
587 clock_ts_to_ct(&ts, &ct);
590 meta->hdr = malloc(ss, M_MD_DDF, M_WAITOK);
591 memset(meta->hdr, 0xff, ss);
593 memcpy(meta->hdr, sample->hdr, sizeof(struct ddf_header));
594 if (ss != sample->sectorsize) {
595 SET32(meta, hdr->WorkSpace_Length,
596 howmany(GET32(sample, hdr->WorkSpace_Length) *
597 sample->sectorsize, ss));
598 SET16(meta, hdr->Configuration_Record_Length,
599 howmany(GET16(sample,
600 hdr->Configuration_Record_Length) *
601 sample->sectorsize, ss));
602 SET32(meta, hdr->cd_length,
603 howmany(GET32(sample, hdr->cd_length) *
604 sample->sectorsize, ss));
605 SET32(meta, hdr->pdr_length,
606 howmany(GET32(sample, hdr->pdr_length) *
607 sample->sectorsize, ss));
608 SET32(meta, hdr->vdr_length,
609 howmany(GET32(sample, hdr->vdr_length) *
610 sample->sectorsize, ss));
611 SET32(meta, hdr->cr_length,
612 howmany(GET32(sample, hdr->cr_length) *
613 sample->sectorsize, ss));
614 SET32(meta, hdr->pdd_length,
615 howmany(GET32(sample, hdr->pdd_length) *
616 sample->sectorsize, ss));
617 SET32(meta, hdr->bbmlog_length,
618 howmany(GET32(sample, hdr->bbmlog_length) *
619 sample->sectorsize, ss));
620 SET32(meta, hdr->Diagnostic_Space,
621 howmany(GET32(sample, hdr->bbmlog_length) *
622 sample->sectorsize, ss));
623 SET32(meta, hdr->Vendor_Specific_Logs,
624 howmany(GET32(sample, hdr->bbmlog_length) *
625 sample->sectorsize, ss));
628 SET32(meta, hdr->Signature, DDF_HEADER_SIGNATURE);
629 snprintf(meta->hdr->DDF_Header_GUID, 25, "FreeBSD %08x%08x",
630 (u_int)(ts.tv_sec - DECADE), arc4random());
631 memcpy(meta->hdr->DDF_rev, "02.00.00", 8);
632 SET32(meta, hdr->TimeStamp, (ts.tv_sec - DECADE));
633 SET32(meta, hdr->WorkSpace_Length, 16 * 1024 * 1024 / ss);
634 SET16(meta, hdr->Max_PD_Entries, DDF_MAX_DISKS - 1);
635 SET16(meta, hdr->Max_VD_Entries, DDF_MAX_VDISKS);
636 SET16(meta, hdr->Max_Partitions, DDF_MAX_PARTITIONS);
637 SET16(meta, hdr->Max_Primary_Element_Entries, DDF_MAX_DISKS);
638 SET16(meta, hdr->Configuration_Record_Length,
639 howmany(sizeof(struct ddf_vdc_record) + (4 + 8) *
640 GET16(meta, hdr->Max_Primary_Element_Entries), ss));
641 SET32(meta, hdr->cd_length,
642 howmany(sizeof(struct ddf_cd_record), ss));
643 SET32(meta, hdr->pdr_length,
644 howmany(sizeof(struct ddf_pd_record) +
645 sizeof(struct ddf_pd_entry) * GET16(meta,
646 hdr->Max_PD_Entries), ss));
647 SET32(meta, hdr->vdr_length,
648 howmany(sizeof(struct ddf_vd_record) +
649 sizeof(struct ddf_vd_entry) *
650 GET16(meta, hdr->Max_VD_Entries), ss));
651 SET32(meta, hdr->cr_length,
652 GET16(meta, hdr->Configuration_Record_Length) *
653 (GET16(meta, hdr->Max_Partitions) + 1));
654 SET32(meta, hdr->pdd_length,
655 howmany(sizeof(struct ddf_pdd_record), ss));
656 SET32(meta, hdr->bbmlog_length, 0);
657 SET32(meta, hdr->Diagnostic_Space_Length, 0);
658 SET32(meta, hdr->Vendor_Specific_Logs_Length, 0);
661 SET32(meta, hdr->cd_section, pos);
662 pos += GET32(meta, hdr->cd_length);
663 SET32(meta, hdr->pdr_section, pos);
664 pos += GET32(meta, hdr->pdr_length);
665 SET32(meta, hdr->vdr_section, pos);
666 pos += GET32(meta, hdr->vdr_length);
667 SET32(meta, hdr->cr_section, pos);
668 pos += GET32(meta, hdr->cr_length);
669 SET32(meta, hdr->pdd_section, pos);
670 pos += GET32(meta, hdr->pdd_length);
671 SET32(meta, hdr->bbmlog_section,
672 GET32(meta, hdr->bbmlog_length) != 0 ? pos : 0xffffffff);
673 pos += GET32(meta, hdr->bbmlog_length);
674 SET32(meta, hdr->Diagnostic_Space,
675 GET32(meta, hdr->Diagnostic_Space_Length) != 0 ? pos : 0xffffffff);
676 pos += GET32(meta, hdr->Diagnostic_Space_Length);
677 SET32(meta, hdr->Vendor_Specific_Logs,
678 GET32(meta, hdr->Vendor_Specific_Logs_Length) != 0 ? pos : 0xffffffff);
679 pos += min(GET32(meta, hdr->Vendor_Specific_Logs_Length), 1);
680 SET64(meta, hdr->Primary_Header_LBA,
682 SET64(meta, hdr->Secondary_Header_LBA,
683 0xffffffffffffffffULL);
684 SET64(meta, hdr->WorkSpace_LBA,
685 anchorlba + 1 - 32 * 1024 * 1024 / ss);
687 /* Controller Data */
688 size = GET32(meta, hdr->cd_length) * ss;
689 meta->cdr = malloc(size, M_MD_DDF, M_WAITOK);
690 memset(meta->cdr, 0xff, size);
691 SET32(meta, cdr->Signature, DDF_CONTROLLER_DATA_SIGNATURE);
692 memcpy(meta->cdr->Controller_GUID, "FreeBSD GEOM RAID SERIAL", 24);
693 memcpy(meta->cdr->Product_ID, "FreeBSD GEOMRAID", 16);
695 /* Physical Drive Records. */
696 size = GET32(meta, hdr->pdr_length) * ss;
697 meta->pdr = malloc(size, M_MD_DDF, M_WAITOK);
698 memset(meta->pdr, 0xff, size);
699 SET32(meta, pdr->Signature, DDF_PDR_SIGNATURE);
700 SET16(meta, pdr->Populated_PDEs, 1);
701 SET16(meta, pdr->Max_PDE_Supported,
702 GET16(meta, hdr->Max_PD_Entries));
704 pde = &meta->pdr->entry[0];
705 len = sizeof(serial_buffer);
706 error = g_io_getattr("GEOM::ident", disk->d_consumer, &len, serial_buffer);
707 if (error == 0 && (len = strlen (serial_buffer)) >= 6 && len <= 20)
708 snprintf(pde->PD_GUID, 25, "DISK%20s", serial_buffer);
710 snprintf(pde->PD_GUID, 25, "DISK%04d%02d%02d%08x%04x",
711 ct.year, ct.mon, ct.day,
712 arc4random(), arc4random() & 0xffff);
713 SET32D(meta, pde->PD_Reference, arc4random());
714 SET16D(meta, pde->PD_Type, DDF_PDE_GUID_FORCE);
715 SET16D(meta, pde->PD_State, 0);
716 SET64D(meta, pde->Configured_Size,
717 anchorlba + 1 - 32 * 1024 * 1024 / ss);
718 SET16D(meta, pde->Block_Size, ss);
720 /* Virtual Drive Records. */
721 size = GET32(meta, hdr->vdr_length) * ss;
722 meta->vdr = malloc(size, M_MD_DDF, M_WAITOK);
723 memset(meta->vdr, 0xff, size);
724 SET32(meta, vdr->Signature, DDF_VD_RECORD_SIGNATURE);
725 SET32(meta, vdr->Populated_VDEs, 0);
726 SET16(meta, vdr->Max_VDE_Supported,
727 GET16(meta, hdr->Max_VD_Entries));
729 /* Configuration Records. */
730 size = GET32(meta, hdr->cr_length) * ss;
731 meta->cr = malloc(size, M_MD_DDF, M_WAITOK);
732 memset(meta->cr, 0xff, size);
734 /* Physical Disk Data. */
735 size = GET32(meta, hdr->pdd_length) * ss;
736 meta->pdd = malloc(size, M_MD_DDF, M_WAITOK);
737 memset(meta->pdd, 0xff, size);
738 SET32(meta, pdd->Signature, DDF_PDD_SIGNATURE);
739 memcpy(meta->pdd->PD_GUID, pde->PD_GUID, 24);
740 SET32(meta, pdd->PD_Reference, GET32D(meta, pde->PD_Reference));
741 SET8(meta, pdd->Forced_Ref_Flag, DDF_PDD_FORCED_REF);
742 SET8(meta, pdd->Forced_PD_GUID_Flag, DDF_PDD_FORCED_GUID);
744 /* Bad Block Management Log. */
745 if (GET32(meta, hdr->bbmlog_length) != 0) {
746 size = GET32(meta, hdr->bbmlog_length) * ss;
747 meta->bbm = malloc(size, M_MD_DDF, M_WAITOK);
748 memset(meta->bbm, 0xff, size);
749 SET32(meta, bbm->Signature, DDF_BBML_SIGNATURE);
750 SET32(meta, bbm->Entry_Count, 0);
751 SET32(meta, bbm->Spare_Block_Count, 0);
756 ddf_meta_copy(struct ddf_meta *dst, struct ddf_meta *src)
758 struct ddf_header *hdr;
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);
783 ddf_meta_update(struct ddf_meta *meta, struct ddf_meta *src)
785 struct ddf_pd_entry *pde, *spde;
788 for (i = 0; i < GET16(src, pdr->Populated_PDEs); i++) {
789 spde = &src->pdr->entry[i];
790 if (isff(spde->PD_GUID, 24))
792 j = ddf_meta_find_pd(meta, NULL,
793 GET32(src, pdr->entry[i].PD_Reference));
795 j = ddf_meta_find_pd(meta, NULL, 0xffffffff);
796 pde = &meta->pdr->entry[j];
797 memcpy(pde, spde, sizeof(*pde));
799 pde = &meta->pdr->entry[j];
800 SET16D(meta, pde->PD_State,
801 GET16D(meta, pde->PD_State) |
802 GET16D(src, pde->PD_State));
808 ddf_meta_free(struct ddf_meta *meta)
811 if (meta->hdr != NULL) {
812 free(meta->hdr, M_MD_DDF);
815 if (meta->cdr != NULL) {
816 free(meta->cdr, M_MD_DDF);
819 if (meta->pdr != NULL) {
820 free(meta->pdr, M_MD_DDF);
823 if (meta->vdr != NULL) {
824 free(meta->vdr, M_MD_DDF);
827 if (meta->cr != NULL) {
828 free(meta->cr, M_MD_DDF);
831 if (meta->pdd != NULL) {
832 free(meta->pdd, M_MD_DDF);
835 if (meta->bbm != NULL) {
836 free(meta->bbm, M_MD_DDF);
842 ddf_vol_meta_create(struct ddf_vol_meta *meta, struct ddf_meta *sample)
846 struct ddf_header *hdr;
850 meta->bigendian = sample->bigendian;
851 ss = meta->sectorsize = sample->sectorsize;
852 meta->hdr = malloc(ss, M_MD_DDF, M_WAITOK);
853 memcpy(meta->hdr, sample->hdr, ss);
854 meta->cdr = malloc(GET32(sample, hdr->cd_length) * ss, M_MD_DDF, M_WAITOK);
855 memcpy(meta->cdr, sample->cdr, GET32(sample, hdr->cd_length) * ss);
856 meta->vde = malloc(sizeof(struct ddf_vd_entry), M_MD_DDF, M_WAITOK);
857 memset(meta->vde, 0xff, sizeof(struct ddf_vd_entry));
859 clock_ts_to_ct(&ts, &ct);
860 snprintf(meta->vde->VD_GUID, 25, "FreeBSD%04d%02d%02d%08x%01x",
861 ct.year, ct.mon, ct.day,
862 arc4random(), arc4random() & 0xf);
863 size = GET16(sample, hdr->Configuration_Record_Length) * ss;
864 meta->vdc = malloc(size, M_MD_DDF, M_WAITOK);
865 memset(meta->vdc, 0xff, size);
866 SET32(meta, vdc->Signature, DDF_VDCR_SIGNATURE);
867 memcpy(meta->vdc->VD_GUID, meta->vde->VD_GUID, 24);
868 SET32(meta, vdc->Sequence_Number, 0);
872 ddf_vol_meta_update(struct ddf_vol_meta *dst, struct ddf_meta *src,
873 uint8_t *GUID, int started)
875 struct ddf_header *hdr;
876 struct ddf_vd_entry *vde;
877 struct ddf_vdc_record *vdc;
878 int vnew, bvnew, bvd, size;
882 vde = &src->vdr->entry[ddf_meta_find_vd(src, GUID)];
883 vdc = ddf_meta_find_vdc(src, GUID);
884 if (GET8D(src, vdc->Secondary_Element_Count) == 1)
887 bvd = GET8D(src, vdc->Secondary_Element_Seq);
888 size = GET16(src, hdr->Configuration_Record_Length) * src->sectorsize;
890 if (dst->vdc == NULL ||
891 (!started && ((int32_t)(GET32D(src, vdc->Sequence_Number) -
892 GET32(dst, vdc->Sequence_Number))) > 0))
897 if (dst->bvdc[bvd] == NULL ||
898 (!started && ((int32_t)(GET32D(src, vdc->Sequence_Number) -
899 GET32(dst, bvdc[bvd]->Sequence_Number))) > 0))
905 dst->bigendian = src->bigendian;
906 ss = dst->sectorsize = src->sectorsize;
907 if (dst->hdr != NULL)
908 free(dst->hdr, M_MD_DDF);
909 dst->hdr = malloc(ss, M_MD_DDF, M_WAITOK);
910 memcpy(dst->hdr, src->hdr, ss);
911 if (dst->cdr != NULL)
912 free(dst->cdr, M_MD_DDF);
913 dst->cdr = malloc(GET32(src, hdr->cd_length) * ss, M_MD_DDF, M_WAITOK);
914 memcpy(dst->cdr, src->cdr, GET32(src, hdr->cd_length) * ss);
915 if (dst->vde != NULL)
916 free(dst->vde, M_MD_DDF);
917 dst->vde = malloc(sizeof(struct ddf_vd_entry), M_MD_DDF, M_WAITOK);
918 memcpy(dst->vde, vde, sizeof(struct ddf_vd_entry));
919 if (dst->vdc != NULL)
920 free(dst->vdc, M_MD_DDF);
921 dst->vdc = malloc(size, M_MD_DDF, M_WAITOK);
922 memcpy(dst->vdc, vdc, size);
925 if (dst->bvdc[bvd] != NULL)
926 free(dst->bvdc[bvd], M_MD_DDF);
927 dst->bvdc[bvd] = malloc(size, M_MD_DDF, M_WAITOK);
928 memcpy(dst->bvdc[bvd], vdc, size);
933 ddf_vol_meta_free(struct ddf_vol_meta *meta)
937 if (meta->hdr != NULL) {
938 free(meta->hdr, M_MD_DDF);
941 if (meta->cdr != NULL) {
942 free(meta->cdr, M_MD_DDF);
945 if (meta->vde != NULL) {
946 free(meta->vde, M_MD_DDF);
949 if (meta->vdc != NULL) {
950 free(meta->vdc, M_MD_DDF);
953 for (i = 0; i < DDF_MAX_DISKS_HARD; i++) {
954 if (meta->bvdc[i] != NULL) {
955 free(meta->bvdc[i], M_MD_DDF);
956 meta->bvdc[i] = NULL;
962 ddf_meta_unused_range(struct ddf_meta *meta, off_t *off, off_t *size)
964 struct ddf_vdc_record *vdc;
965 off_t beg[32], end[32], beg1, end1;
967 int i, j, n, num, pos;
972 ref = GET32(meta, pdd->PD_Reference);
973 pos = ddf_meta_find_pd(meta, NULL, ref);
975 end[0] = GET64(meta, pdr->entry[pos].Configured_Size);
977 num = GETCRNUM(meta);
978 for (i = 0; i < num; i++) {
979 vdc = GETVDCPTR(meta, i);
980 if (GET32D(meta, vdc->Signature) != DDF_VDCR_SIGNATURE)
982 for (pos = 0; pos < GET16D(meta, vdc->Primary_Element_Count); pos++)
983 if (GET32D(meta, vdc->Physical_Disk_Sequence[pos]) == ref)
985 if (pos == GET16D(meta, vdc->Primary_Element_Count))
987 offp = (uint64_t *)&(vdc->Physical_Disk_Sequence[
988 GET16(meta, hdr->Max_Primary_Element_Entries)]);
989 beg1 = GET64P(meta, offp + pos);
990 end1 = beg1 + GET64D(meta, vdc->Block_Count);
991 for (j = 0; j < n; j++) {
992 if (beg[j] >= end1 || end[j] <= beg1 )
994 if (beg[j] < beg1 && end[j] > end1) {
999 } else if (beg[j] < beg1)
1005 for (j = 0; j < n; j++) {
1006 if (end[j] - beg[j] > *size) {
1008 *size = end[j] - beg[j];
1011 return ((*size > 0) ? 1 : 0);
1015 ddf_meta_get_name(struct ddf_meta *meta, int num, char *buf)
1020 b = meta->vdr->entry[num].VD_Name;
1021 for (i = 15; i >= 0; i--)
1024 memcpy(buf, b, i + 1);
1029 ddf_meta_put_name(struct ddf_vol_meta *meta, char *buf)
1033 len = min(strlen(buf), 16);
1034 memset(meta->vde->VD_Name, 0x20, 16);
1035 memcpy(meta->vde->VD_Name, buf, len);
1039 ddf_meta_read(struct g_consumer *cp, struct ddf_meta *meta)
1041 struct g_provider *pp;
1042 struct ddf_header *ahdr, *hdr;
1044 off_t plba, slba, lba;
1049 ddf_meta_free(meta);
1051 ss = meta->sectorsize = pp->sectorsize;
1052 /* Read anchor block. */
1053 abuf = g_read_data(cp, pp->mediasize - ss, ss, &error);
1055 G_RAID_DEBUG(1, "Cannot read metadata from %s (error=%d).",
1059 ahdr = (struct ddf_header *)abuf;
1061 /* Check if this is an DDF RAID struct */
1062 if (be32dec(&ahdr->Signature) == DDF_HEADER_SIGNATURE)
1063 meta->bigendian = 1;
1064 else if (le32dec(&ahdr->Signature) == DDF_HEADER_SIGNATURE)
1065 meta->bigendian = 0;
1067 G_RAID_DEBUG(1, "DDF signature check failed on %s", pp->name);
1071 if (ahdr->Header_Type != DDF_HEADER_ANCHOR) {
1072 G_RAID_DEBUG(1, "DDF header type check failed on %s", pp->name);
1077 plba = GET64(meta, hdr->Primary_Header_LBA);
1078 slba = GET64(meta, hdr->Secondary_Header_LBA);
1079 val = GET32(meta, hdr->CRC);
1080 SET32(meta, hdr->CRC, 0xffffffff);
1082 if (crc32(ahdr, ss) != val) {
1083 G_RAID_DEBUG(1, "DDF CRC mismatch on %s", pp->name);
1087 if ((plba + 6) * ss >= pp->mediasize) {
1088 G_RAID_DEBUG(1, "DDF primary header LBA is wrong on %s", pp->name);
1092 if (slba != -1 && (slba + 6) * ss >= pp->mediasize) {
1093 G_RAID_DEBUG(1, "DDF secondary header LBA is wrong on %s", pp->name);
1101 ddf_meta_free(meta);
1103 /* Read header block. */
1104 buf = g_read_data(cp, lba * ss, ss, &error);
1107 G_RAID_DEBUG(1, "DDF %s metadata read error on %s (error=%d).",
1108 (lba == plba) ? "primary" : "secondary", pp->name, error);
1109 if (lba == plba && slba != -1) {
1113 G_RAID_DEBUG(1, "DDF metadata read error on %s.", pp->name);
1116 meta->hdr = malloc(ss, M_MD_DDF, M_WAITOK);
1117 memcpy(meta->hdr, buf, ss);
1120 val = GET32(meta, hdr->CRC);
1121 SET32(meta, hdr->CRC, 0xffffffff);
1122 if (hdr->Signature != ahdr->Signature ||
1123 crc32(meta->hdr, ss) != val ||
1124 memcmp(hdr->DDF_Header_GUID, ahdr->DDF_Header_GUID, 24) ||
1125 GET64(meta, hdr->Primary_Header_LBA) != plba ||
1126 GET64(meta, hdr->Secondary_Header_LBA) != slba) {
1128 G_RAID_DEBUG(1, "DDF %s metadata check failed on %s",
1129 (lba == plba) ? "primary" : "secondary", pp->name);
1130 if (lba == plba && slba != -1) {
1134 G_RAID_DEBUG(1, "DDF metadata check failed on %s", pp->name);
1138 if ((lba == plba && hdr->Header_Type != DDF_HEADER_PRIMARY) ||
1139 (lba == slba && hdr->Header_Type != DDF_HEADER_SECONDARY))
1142 len = max(len, GET32(meta, hdr->cd_section) + GET32(meta, hdr->cd_length));
1143 len = max(len, GET32(meta, hdr->pdr_section) + GET32(meta, hdr->pdr_length));
1144 len = max(len, GET32(meta, hdr->vdr_section) + GET32(meta, hdr->vdr_length));
1145 len = max(len, GET32(meta, hdr->cr_section) + GET32(meta, hdr->cr_length));
1146 len = max(len, GET32(meta, hdr->pdd_section) + GET32(meta, hdr->pdd_length));
1147 if ((val = GET32(meta, hdr->bbmlog_section)) != 0xffffffff)
1148 len = max(len, val + GET32(meta, hdr->bbmlog_length));
1149 if ((val = GET32(meta, hdr->Diagnostic_Space)) != 0xffffffff)
1150 len = max(len, val + GET32(meta, hdr->Diagnostic_Space_Length));
1151 if ((val = GET32(meta, hdr->Vendor_Specific_Logs)) != 0xffffffff)
1152 len = max(len, val + GET32(meta, hdr->Vendor_Specific_Logs_Length));
1153 if ((plba + len) * ss >= pp->mediasize)
1155 if (slba != -1 && (slba + len) * ss >= pp->mediasize)
1157 /* Workaround for Adaptec implementation. */
1158 if (GET16(meta, hdr->Max_Primary_Element_Entries) == 0xffff) {
1159 SET16(meta, hdr->Max_Primary_Element_Entries,
1160 min(GET16(meta, hdr->Max_PD_Entries),
1161 (GET16(meta, hdr->Configuration_Record_Length) * ss - 512) / 12));
1164 if (GET32(meta, hdr->cd_length) * ss >= MAXPHYS ||
1165 GET32(meta, hdr->pdr_length) * ss >= MAXPHYS ||
1166 GET32(meta, hdr->vdr_length) * ss >= MAXPHYS ||
1167 GET32(meta, hdr->cr_length) * ss >= MAXPHYS ||
1168 GET32(meta, hdr->pdd_length) * ss >= MAXPHYS ||
1169 GET32(meta, hdr->bbmlog_length) * ss >= MAXPHYS) {
1170 G_RAID_DEBUG(1, "%s: Blocksize is too big.", pp->name);
1174 /* Read controller data. */
1175 buf = g_read_data(cp, (lba + GET32(meta, hdr->cd_section)) * ss,
1176 GET32(meta, hdr->cd_length) * ss, &error);
1179 meta->cdr = malloc(GET32(meta, hdr->cd_length) * ss, M_MD_DDF, M_WAITOK);
1180 memcpy(meta->cdr, buf, GET32(meta, hdr->cd_length) * ss);
1182 if (GET32(meta, cdr->Signature) != DDF_CONTROLLER_DATA_SIGNATURE)
1185 /* Read physical disk records. */
1186 buf = g_read_data(cp, (lba + GET32(meta, hdr->pdr_section)) * ss,
1187 GET32(meta, hdr->pdr_length) * ss, &error);
1190 meta->pdr = malloc(GET32(meta, hdr->pdr_length) * ss, M_MD_DDF, M_WAITOK);
1191 memcpy(meta->pdr, buf, GET32(meta, hdr->pdr_length) * ss);
1193 if (GET32(meta, pdr->Signature) != DDF_PDR_SIGNATURE)
1196 * Workaround for reading metadata corrupted due to graid bug.
1197 * XXX: Remove this before we have disks above 128PB. :)
1199 if (meta->bigendian) {
1200 for (i = 0; i < GET16(meta, pdr->Populated_PDEs); i++) {
1201 if (isff(meta->pdr->entry[i].PD_GUID, 24))
1203 if (GET32(meta, pdr->entry[i].PD_Reference) ==
1206 if (GET64(meta, pdr->entry[i].Configured_Size) >=
1208 SET16(meta, pdr->entry[i].PD_State,
1209 GET16(meta, pdr->entry[i].PD_State) &
1211 SET64(meta, pdr->entry[i].Configured_Size,
1212 GET64(meta, pdr->entry[i].Configured_Size) &
1213 ((1ULL << 48) - 1));
1218 /* Read virtual disk records. */
1219 buf = g_read_data(cp, (lba + GET32(meta, hdr->vdr_section)) * ss,
1220 GET32(meta, hdr->vdr_length) * ss, &error);
1223 meta->vdr = malloc(GET32(meta, hdr->vdr_length) * ss, M_MD_DDF, M_WAITOK);
1224 memcpy(meta->vdr, buf, GET32(meta, hdr->vdr_length) * ss);
1226 if (GET32(meta, vdr->Signature) != DDF_VD_RECORD_SIGNATURE)
1229 /* Read configuration records. */
1230 buf = g_read_data(cp, (lba + GET32(meta, hdr->cr_section)) * ss,
1231 GET32(meta, hdr->cr_length) * ss, &error);
1234 meta->cr = malloc(GET32(meta, hdr->cr_length) * ss, M_MD_DDF, M_WAITOK);
1235 memcpy(meta->cr, buf, GET32(meta, hdr->cr_length) * ss);
1238 /* Read physical disk data. */
1239 buf = g_read_data(cp, (lba + GET32(meta, hdr->pdd_section)) * ss,
1240 GET32(meta, hdr->pdd_length) * ss, &error);
1243 meta->pdd = malloc(GET32(meta, hdr->pdd_length) * ss, M_MD_DDF, M_WAITOK);
1244 memcpy(meta->pdd, buf, GET32(meta, hdr->pdd_length) * ss);
1246 if (GET32(meta, pdd->Signature) != DDF_PDD_SIGNATURE)
1248 i = ddf_meta_find_pd(meta, NULL, GET32(meta, pdd->PD_Reference));
1253 if (GET32(meta, hdr->bbmlog_section) != 0xffffffff &&
1254 GET32(meta, hdr->bbmlog_length) != 0) {
1255 buf = g_read_data(cp, (lba + GET32(meta, hdr->bbmlog_section)) * ss,
1256 GET32(meta, hdr->bbmlog_length) * ss, &error);
1259 meta->bbm = malloc(GET32(meta, hdr->bbmlog_length) * ss, M_MD_DDF, M_WAITOK);
1260 memcpy(meta->bbm, buf, GET32(meta, hdr->bbmlog_length) * ss);
1262 if (GET32(meta, bbm->Signature) != DDF_BBML_SIGNATURE)
1269 ddf_meta_free(meta);
1274 ddf_meta_write(struct g_consumer *cp, struct ddf_meta *meta)
1276 struct g_provider *pp;
1277 struct ddf_vdc_record *vdc;
1278 off_t alba, plba, slba, lba;
1283 ss = pp->sectorsize;
1284 lba = alba = pp->mediasize / ss - 1;
1285 plba = GET64(meta, hdr->Primary_Header_LBA);
1286 slba = GET64(meta, hdr->Secondary_Header_LBA);
1289 SET8(meta, hdr->Header_Type, (lba == alba) ? DDF_HEADER_ANCHOR :
1290 (lba == plba) ? DDF_HEADER_PRIMARY : DDF_HEADER_SECONDARY);
1291 SET32(meta, hdr->CRC, 0xffffffff);
1292 SET32(meta, hdr->CRC, crc32(meta->hdr, ss));
1293 error = g_write_data(cp, lba * ss, meta->hdr, ss);
1296 G_RAID_DEBUG(1, "Cannot write metadata to %s (error=%d).",
1306 size = GET32(meta, hdr->cd_length) * ss;
1307 SET32(meta, cdr->CRC, 0xffffffff);
1308 SET32(meta, cdr->CRC, crc32(meta->cdr, size));
1309 error = g_write_data(cp, (lba + GET32(meta, hdr->cd_section)) * ss,
1314 size = GET32(meta, hdr->pdr_length) * ss;
1315 SET32(meta, pdr->CRC, 0xffffffff);
1316 SET32(meta, pdr->CRC, crc32(meta->pdr, size));
1317 error = g_write_data(cp, (lba + GET32(meta, hdr->pdr_section)) * ss,
1322 size = GET32(meta, hdr->vdr_length) * ss;
1323 SET32(meta, vdr->CRC, 0xffffffff);
1324 SET32(meta, vdr->CRC, crc32(meta->vdr, size));
1325 error = g_write_data(cp, (lba + GET32(meta, hdr->vdr_section)) * ss,
1330 size = GET16(meta, hdr->Configuration_Record_Length) * ss;
1331 num = GETCRNUM(meta);
1332 for (i = 0; i < num; i++) {
1333 vdc = GETVDCPTR(meta, i);
1334 SET32D(meta, vdc->CRC, 0xffffffff);
1335 SET32D(meta, vdc->CRC, crc32(vdc, size));
1337 error = g_write_data(cp, (lba + GET32(meta, hdr->cr_section)) * ss,
1338 meta->cr, size * num);
1342 size = GET32(meta, hdr->pdd_length) * ss;
1343 SET32(meta, pdd->CRC, 0xffffffff);
1344 SET32(meta, pdd->CRC, crc32(meta->pdd, size));
1345 error = g_write_data(cp, (lba + GET32(meta, hdr->pdd_section)) * ss,
1350 if (GET32(meta, hdr->bbmlog_length) != 0) {
1351 size = GET32(meta, hdr->bbmlog_length) * ss;
1352 SET32(meta, bbm->CRC, 0xffffffff);
1353 SET32(meta, bbm->CRC, crc32(meta->bbm, size));
1354 error = g_write_data(cp,
1355 (lba + GET32(meta, hdr->bbmlog_section)) * ss,
1362 if (lba == plba && slba != -1) {
1371 ddf_meta_erase(struct g_consumer *cp)
1373 struct g_provider *pp;
1378 buf = malloc(pp->sectorsize, M_MD_DDF, M_WAITOK | M_ZERO);
1379 error = g_write_data(cp, pp->mediasize - pp->sectorsize,
1380 buf, pp->sectorsize);
1382 G_RAID_DEBUG(1, "Cannot erase metadata on %s (error=%d).",
1385 free(buf, M_MD_DDF);
1389 static struct g_raid_volume *
1390 g_raid_md_ddf_get_volume(struct g_raid_softc *sc, uint8_t *GUID)
1392 struct g_raid_volume *vol;
1393 struct g_raid_md_ddf_pervolume *pv;
1395 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
1396 pv = vol->v_md_data;
1397 if (memcmp(pv->pv_meta.vde->VD_GUID, GUID, 24) == 0)
1403 static struct g_raid_disk *
1404 g_raid_md_ddf_get_disk(struct g_raid_softc *sc, uint8_t *GUID, uint32_t id)
1406 struct g_raid_disk *disk;
1407 struct g_raid_md_ddf_perdisk *pd;
1408 struct ddf_meta *meta;
1410 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1411 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
1412 meta = &pd->pd_meta;
1414 if (memcmp(meta->pdd->PD_GUID, GUID, 24) == 0)
1417 if (GET32(meta, pdd->PD_Reference) == id)
1425 g_raid_md_ddf_purge_volumes(struct g_raid_softc *sc)
1427 struct g_raid_volume *vol, *tvol;
1428 struct g_raid_md_ddf_pervolume *pv;
1432 TAILQ_FOREACH_SAFE(vol, &sc->sc_volumes, v_next, tvol) {
1433 pv = vol->v_md_data;
1434 if (vol->v_stopping)
1436 for (i = 0; i < vol->v_disks_count; i++) {
1437 if (vol->v_subdisks[i].sd_state != G_RAID_SUBDISK_S_NONE)
1440 if (i >= vol->v_disks_count) {
1441 g_raid_destroy_volume(vol);
1449 g_raid_md_ddf_purge_disks(struct g_raid_softc *sc)
1452 struct g_raid_disk *disk, *tdisk;
1453 struct g_raid_volume *vol;
1454 struct g_raid_md_ddf_perdisk *pd;
1458 TAILQ_FOREACH_SAFE(disk, &sc->sc_disks, d_next, tdisk) {
1459 if (disk->d_state == G_RAID_DISK_S_SPARE)
1461 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
1463 /* Scan for deleted volumes. */
1464 for (i = 0; i < pd->pd_subdisks; ) {
1465 vol = g_raid_md_ddf_get_volume(sc,
1466 pd->pd_meta[i]->volume_id);
1467 if (vol != NULL && !vol->v_stopping) {
1471 free(pd->pd_meta[i], M_MD_DDF);
1472 for (j = i; j < pd->pd_subdisks - 1; j++)
1473 pd->pd_meta[j] = pd->pd_meta[j + 1];
1474 pd->pd_meta[DDF_MAX_SUBDISKS - 1] = NULL;
1479 /* If there is no metadata left - erase and delete disk. */
1480 if (pd->pd_subdisks == 0) {
1481 ddf_meta_erase(disk->d_consumer);
1482 g_raid_destroy_disk(disk);
1492 g_raid_md_ddf_supported(int level, int qual, int disks, int force)
1495 if (disks > DDF_MAX_DISKS_HARD)
1498 case G_RAID_VOLUME_RL_RAID0:
1499 if (qual != G_RAID_VOLUME_RLQ_NONE)
1503 if (!force && disks < 2)
1506 case G_RAID_VOLUME_RL_RAID1:
1509 if (qual == G_RAID_VOLUME_RLQ_R1SM) {
1510 if (!force && disks != 2)
1512 } else if (qual == G_RAID_VOLUME_RLQ_R1MM) {
1513 if (!force && disks != 3)
1518 case G_RAID_VOLUME_RL_RAID3:
1519 if (qual != G_RAID_VOLUME_RLQ_R3P0 &&
1520 qual != G_RAID_VOLUME_RLQ_R3PN)
1525 case G_RAID_VOLUME_RL_RAID4:
1526 if (qual != G_RAID_VOLUME_RLQ_R4P0 &&
1527 qual != G_RAID_VOLUME_RLQ_R4PN)
1532 case G_RAID_VOLUME_RL_RAID5:
1533 if (qual != G_RAID_VOLUME_RLQ_R5RA &&
1534 qual != G_RAID_VOLUME_RLQ_R5RS &&
1535 qual != G_RAID_VOLUME_RLQ_R5LA &&
1536 qual != G_RAID_VOLUME_RLQ_R5LS)
1541 case G_RAID_VOLUME_RL_RAID6:
1542 if (qual != G_RAID_VOLUME_RLQ_R6RA &&
1543 qual != G_RAID_VOLUME_RLQ_R6RS &&
1544 qual != G_RAID_VOLUME_RLQ_R6LA &&
1545 qual != G_RAID_VOLUME_RLQ_R6LS)
1550 case G_RAID_VOLUME_RL_RAIDMDF:
1551 if (qual != G_RAID_VOLUME_RLQ_RMDFRA &&
1552 qual != G_RAID_VOLUME_RLQ_RMDFRS &&
1553 qual != G_RAID_VOLUME_RLQ_RMDFLA &&
1554 qual != G_RAID_VOLUME_RLQ_RMDFLS)
1559 case G_RAID_VOLUME_RL_RAID1E:
1560 if (qual != G_RAID_VOLUME_RLQ_R1EA &&
1561 qual != G_RAID_VOLUME_RLQ_R1EO)
1566 case G_RAID_VOLUME_RL_SINGLE:
1567 if (qual != G_RAID_VOLUME_RLQ_NONE)
1572 case G_RAID_VOLUME_RL_CONCAT:
1573 if (qual != G_RAID_VOLUME_RLQ_NONE)
1578 case G_RAID_VOLUME_RL_RAID5E:
1579 if (qual != G_RAID_VOLUME_RLQ_R5ERA &&
1580 qual != G_RAID_VOLUME_RLQ_R5ERS &&
1581 qual != G_RAID_VOLUME_RLQ_R5ELA &&
1582 qual != G_RAID_VOLUME_RLQ_R5ELS)
1587 case G_RAID_VOLUME_RL_RAID5EE:
1588 if (qual != G_RAID_VOLUME_RLQ_R5EERA &&
1589 qual != G_RAID_VOLUME_RLQ_R5EERS &&
1590 qual != G_RAID_VOLUME_RLQ_R5EELA &&
1591 qual != G_RAID_VOLUME_RLQ_R5EELS)
1596 case G_RAID_VOLUME_RL_RAID5R:
1597 if (qual != G_RAID_VOLUME_RLQ_R5RRA &&
1598 qual != G_RAID_VOLUME_RLQ_R5RRS &&
1599 qual != G_RAID_VOLUME_RLQ_R5RLA &&
1600 qual != G_RAID_VOLUME_RLQ_R5RLS)
1612 g_raid_md_ddf_start_disk(struct g_raid_disk *disk, struct g_raid_volume *vol)
1614 struct g_raid_softc *sc;
1615 struct g_raid_subdisk *sd;
1616 struct g_raid_md_ddf_perdisk *pd;
1617 struct g_raid_md_ddf_pervolume *pv;
1618 struct g_raid_md_ddf_object *mdi;
1619 struct ddf_vol_meta *vmeta;
1620 struct ddf_meta *pdmeta, *gmeta;
1621 struct ddf_vdc_record *vdc1;
1622 struct ddf_sa_record *sa;
1623 off_t size, eoff = 0, esize = 0;
1625 int disk_pos, md_disk_bvd = -1, md_disk_pos = -1, md_pde_pos;
1626 int i, resurrection = 0;
1630 mdi = (struct g_raid_md_ddf_object *)sc->sc_md;
1631 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
1632 pdmeta = &pd->pd_meta;
1633 reference = GET32(&pd->pd_meta, pdd->PD_Reference);
1635 pv = vol->v_md_data;
1636 vmeta = &pv->pv_meta;
1637 gmeta = &mdi->mdio_meta;
1639 /* Find disk position in metadata by its reference. */
1640 disk_pos = ddf_meta_find_disk(vmeta, reference,
1641 &md_disk_bvd, &md_disk_pos);
1642 md_pde_pos = ddf_meta_find_pd(gmeta, NULL, reference);
1645 G_RAID_DEBUG1(1, sc,
1646 "Disk %s is not a present part of the volume %s",
1647 g_raid_get_diskname(disk), vol->v_name);
1649 /* Failed stale disk is useless for us. */
1650 if ((GET16(gmeta, pdr->entry[md_pde_pos].PD_State) & DDF_PDE_PFA) != 0) {
1651 g_raid_change_disk_state(disk, G_RAID_DISK_S_STALE_FAILED);
1655 /* If disk has some metadata for this volume - erase. */
1656 if ((vdc1 = ddf_meta_find_vdc(pdmeta, vmeta->vdc->VD_GUID)) != NULL)
1657 SET32D(pdmeta, vdc1->Signature, 0xffffffff);
1659 /* If we are in the start process, that's all for now. */
1660 if (!pv->pv_started)
1663 * If we have already started - try to get use of the disk.
1664 * Try to replace OFFLINE disks first, then FAILED.
1666 if (ddf_meta_count_vdc(&pd->pd_meta, NULL) >=
1667 GET16(&pd->pd_meta, hdr->Max_Partitions)) {
1668 G_RAID_DEBUG1(1, sc, "No free partitions on disk %s",
1669 g_raid_get_diskname(disk));
1672 ddf_meta_unused_range(&pd->pd_meta, &eoff, &esize);
1674 G_RAID_DEBUG1(1, sc, "No free space on disk %s",
1675 g_raid_get_diskname(disk));
1678 eoff *= pd->pd_meta.sectorsize;
1679 esize *= pd->pd_meta.sectorsize;
1681 for (i = 0; i < vol->v_disks_count; i++) {
1682 sd = &vol->v_subdisks[i];
1683 if (sd->sd_state != G_RAID_SUBDISK_S_NONE)
1685 if (sd->sd_state <= G_RAID_SUBDISK_S_FAILED &&
1687 vol->v_subdisks[i].sd_state < sd->sd_state))
1690 if (disk_pos >= 0 &&
1691 vol->v_raid_level != G_RAID_VOLUME_RL_CONCAT &&
1693 G_RAID_DEBUG1(1, sc, "Disk %s free space "
1694 "is too small (%ju < %ju)",
1695 g_raid_get_diskname(disk), esize, size);
1698 if (disk_pos >= 0) {
1699 if (vol->v_raid_level != G_RAID_VOLUME_RL_CONCAT)
1701 md_disk_bvd = disk_pos / GET16(vmeta, vdc->Primary_Element_Count); // XXX
1702 md_disk_pos = disk_pos % GET16(vmeta, vdc->Primary_Element_Count); // XXX
1705 if (disk->d_state == G_RAID_DISK_S_NONE)
1706 g_raid_change_disk_state(disk,
1707 G_RAID_DISK_S_STALE);
1712 * If spare is committable, delete spare record.
1713 * Othersize, mark it active and leave there.
1715 sa = ddf_meta_find_sa(&pd->pd_meta, 0);
1717 if ((GET8D(&pd->pd_meta, sa->Spare_Type) &
1718 DDF_SAR_TYPE_REVERTIBLE) == 0) {
1719 SET32D(&pd->pd_meta, sa->Signature, 0xffffffff);
1721 SET8D(&pd->pd_meta, sa->Spare_Type,
1722 GET8D(&pd->pd_meta, sa->Spare_Type) |
1723 DDF_SAR_TYPE_ACTIVE);
1727 G_RAID_DEBUG1(1, sc, "Disk %s takes pos %d in the volume %s",
1728 g_raid_get_diskname(disk), disk_pos, vol->v_name);
1732 sd = &vol->v_subdisks[disk_pos];
1734 if (resurrection && sd->sd_disk != NULL) {
1735 g_raid_change_disk_state(sd->sd_disk,
1736 G_RAID_DISK_S_STALE_FAILED);
1737 TAILQ_REMOVE(&sd->sd_disk->d_subdisks,
1740 vol->v_subdisks[disk_pos].sd_disk = disk;
1741 TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
1743 /* Welcome the new disk. */
1745 g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE);
1746 else if (GET16(gmeta, pdr->entry[md_pde_pos].PD_State) & DDF_PDE_PFA)
1747 g_raid_change_disk_state(disk, G_RAID_DISK_S_FAILED);
1749 g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE);
1752 sd->sd_offset = eoff;
1753 sd->sd_size = esize;
1754 } else if (pdmeta->cr != NULL &&
1755 (vdc1 = ddf_meta_find_vdc(pdmeta, vmeta->vdc->VD_GUID)) != NULL) {
1756 val2 = (uint64_t *)&(vdc1->Physical_Disk_Sequence[GET16(vmeta, hdr->Max_Primary_Element_Entries)]);
1757 sd->sd_offset = (off_t)GET64P(pdmeta, val2 + md_disk_pos) * 512;
1758 sd->sd_size = (off_t)GET64D(pdmeta, vdc1->Block_Count) * 512;
1762 /* Stale disk, almost same as new. */
1763 g_raid_change_subdisk_state(sd,
1764 G_RAID_SUBDISK_S_NEW);
1765 } else if (GET16(gmeta, pdr->entry[md_pde_pos].PD_State) & DDF_PDE_PFA) {
1767 g_raid_change_subdisk_state(sd,
1768 G_RAID_SUBDISK_S_FAILED);
1769 } else if ((GET16(gmeta, pdr->entry[md_pde_pos].PD_State) &
1770 (DDF_PDE_FAILED | DDF_PDE_REBUILD)) != 0) {
1771 /* Rebuilding disk. */
1772 g_raid_change_subdisk_state(sd,
1773 G_RAID_SUBDISK_S_REBUILD);
1774 sd->sd_rebuild_pos = 0;
1775 } else if ((GET8(vmeta, vde->VD_State) & DDF_VDE_DIRTY) != 0 ||
1776 (GET8(vmeta, vde->Init_State) & DDF_VDE_INIT_MASK) !=
1777 DDF_VDE_INIT_FULL) {
1778 /* Stale disk or dirty volume (unclean shutdown). */
1779 g_raid_change_subdisk_state(sd,
1780 G_RAID_SUBDISK_S_STALE);
1782 /* Up to date disk. */
1783 g_raid_change_subdisk_state(sd,
1784 G_RAID_SUBDISK_S_ACTIVE);
1786 g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW,
1787 G_RAID_EVENT_SUBDISK);
1789 return (resurrection);
1793 g_raid_md_ddf_refill(struct g_raid_softc *sc)
1795 struct g_raid_volume *vol;
1796 struct g_raid_subdisk *sd;
1797 struct g_raid_disk *disk;
1798 struct g_raid_md_object *md;
1799 struct g_raid_md_ddf_perdisk *pd;
1800 struct g_raid_md_ddf_pervolume *pv;
1801 int update, updated, i, bad;
1806 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
1807 pv = vol->v_md_data;
1808 if (!pv->pv_started || vol->v_stopping)
1811 /* Search for subdisk that needs replacement. */
1813 for (i = 0; i < vol->v_disks_count; i++) {
1814 sd = &vol->v_subdisks[i];
1815 if (sd->sd_state == G_RAID_SUBDISK_S_NONE ||
1816 sd->sd_state == G_RAID_SUBDISK_S_FAILED)
1822 G_RAID_DEBUG1(1, sc, "Volume %s is not complete, "
1823 "trying to refill.", vol->v_name);
1825 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1827 if (disk->d_state < G_RAID_DISK_S_SPARE)
1829 /* Skip already used by this volume. */
1830 for (i = 0; i < vol->v_disks_count; i++) {
1831 sd = &vol->v_subdisks[i];
1832 if (sd->sd_disk == disk)
1835 if (i < vol->v_disks_count)
1838 /* Try to use disk if it has empty extents. */
1839 pd = disk->d_md_data;
1840 if (ddf_meta_count_vdc(&pd->pd_meta, NULL) <
1841 GET16(&pd->pd_meta, hdr->Max_Partitions)) {
1842 update = g_raid_md_ddf_start_disk(disk, vol);
1847 g_raid_md_write_ddf(md, vol, NULL, disk);
1857 g_raid_md_ddf_start(struct g_raid_volume *vol)
1859 struct g_raid_softc *sc;
1860 struct g_raid_subdisk *sd;
1861 struct g_raid_disk *disk;
1862 struct g_raid_md_object *md;
1863 struct g_raid_md_ddf_perdisk *pd;
1864 struct g_raid_md_ddf_pervolume *pv;
1865 struct g_raid_md_ddf_object *mdi;
1866 struct ddf_vol_meta *vmeta;
1867 struct ddf_vdc_record *vdc;
1873 mdi = (struct g_raid_md_ddf_object *)md;
1874 pv = vol->v_md_data;
1875 vmeta = &pv->pv_meta;
1878 vol->v_raid_level = GET8(vmeta, vdc->Primary_RAID_Level);
1879 vol->v_raid_level_qualifier = GET8(vmeta, vdc->RLQ);
1880 if (GET8(vmeta, vdc->Secondary_Element_Count) > 1 &&
1881 vol->v_raid_level == G_RAID_VOLUME_RL_RAID1 &&
1882 GET8(vmeta, vdc->Secondary_RAID_Level) == 0)
1883 vol->v_raid_level = G_RAID_VOLUME_RL_RAID1E;
1884 vol->v_sectorsize = GET16(vmeta, vdc->Block_Size);
1885 if (vol->v_sectorsize == 0xffff)
1886 vol->v_sectorsize = vmeta->sectorsize;
1887 vol->v_strip_size = vol->v_sectorsize << GET8(vmeta, vdc->Stripe_Size);
1888 vol->v_disks_count = GET16(vmeta, vdc->Primary_Element_Count) *
1889 GET8(vmeta, vdc->Secondary_Element_Count);
1890 vol->v_mdf_pdisks = GET8(vmeta, vdc->MDF_Parity_Disks);
1891 vol->v_mdf_polynomial = GET16(vmeta, vdc->MDF_Parity_Generator_Polynomial);
1892 vol->v_mdf_method = GET8(vmeta, vdc->MDF_Constant_Generation_Method);
1893 if (GET8(vmeta, vdc->Rotate_Parity_count) > 31)
1894 vol->v_rotate_parity = 1;
1896 vol->v_rotate_parity = 1 << GET8(vmeta, vdc->Rotate_Parity_count);
1897 vol->v_mediasize = GET64(vmeta, vdc->VD_Size) * vol->v_sectorsize;
1898 for (i = 0, j = 0, bvd = 0; i < vol->v_disks_count; i++, j++) {
1899 if (j == GET16(vmeta, vdc->Primary_Element_Count)) {
1903 sd = &vol->v_subdisks[i];
1904 if (vmeta->bvdc[bvd] == NULL) {
1906 sd->sd_size = GET64(vmeta, vdc->Block_Count) *
1910 val2 = (uint64_t *)&(vmeta->bvdc[bvd]->Physical_Disk_Sequence[
1911 GET16(vmeta, hdr->Max_Primary_Element_Entries)]);
1912 sd->sd_offset = GET64P(vmeta, val2 + j) * vol->v_sectorsize;
1913 sd->sd_size = GET64(vmeta, bvdc[bvd]->Block_Count) *
1916 g_raid_start_volume(vol);
1918 /* Make all disks found till the moment take their places. */
1919 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1920 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
1921 if (ddf_meta_find_vdc(&pd->pd_meta, vmeta->vdc->VD_GUID) != NULL)
1922 g_raid_md_ddf_start_disk(disk, vol);
1926 mdi->mdio_starting--;
1927 callout_stop(&pv->pv_start_co);
1928 G_RAID_DEBUG1(0, sc, "Volume started.");
1929 g_raid_md_write_ddf(md, vol, NULL, NULL);
1931 /* Pickup any STALE/SPARE disks to refill array if needed. */
1932 g_raid_md_ddf_refill(sc);
1934 g_raid_event_send(vol, G_RAID_VOLUME_E_START, G_RAID_EVENT_VOLUME);
1938 g_raid_ddf_go(void *arg)
1940 struct g_raid_volume *vol;
1941 struct g_raid_softc *sc;
1942 struct g_raid_md_ddf_pervolume *pv;
1945 pv = vol->v_md_data;
1947 if (!pv->pv_started) {
1948 G_RAID_DEBUG1(0, sc, "Force volume start due to timeout.");
1949 g_raid_event_send(vol, G_RAID_VOLUME_E_STARTMD,
1950 G_RAID_EVENT_VOLUME);
1955 g_raid_md_ddf_new_disk(struct g_raid_disk *disk)
1957 struct g_raid_softc *sc;
1958 struct g_raid_md_object *md;
1959 struct g_raid_md_ddf_perdisk *pd;
1960 struct g_raid_md_ddf_pervolume *pv;
1961 struct g_raid_md_ddf_object *mdi;
1962 struct g_raid_volume *vol;
1963 struct ddf_meta *pdmeta;
1964 struct ddf_vol_meta *vmeta;
1965 struct ddf_vdc_record *vdc;
1966 struct ddf_vd_entry *vde;
1967 int i, j, k, num, have, need, cnt, spare;
1973 mdi = (struct g_raid_md_ddf_object *)md;
1974 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
1975 pdmeta = &pd->pd_meta;
1978 if (mdi->mdio_meta.hdr == NULL)
1979 ddf_meta_copy(&mdi->mdio_meta, pdmeta);
1981 ddf_meta_update(&mdi->mdio_meta, pdmeta);
1983 num = GETCRNUM(pdmeta);
1984 for (j = 0; j < num; j++) {
1985 vdc = GETVDCPTR(pdmeta, j);
1986 val = GET32D(pdmeta, vdc->Signature);
1988 if (val == DDF_SA_SIGNATURE && spare == -1)
1991 if (val != DDF_VDCR_SIGNATURE)
1994 k = ddf_meta_find_vd(pdmeta, vdc->VD_GUID);
1997 vde = &pdmeta->vdr->entry[k];
1999 /* Look for volume with matching ID. */
2000 vol = g_raid_md_ddf_get_volume(sc, vdc->VD_GUID);
2002 ddf_meta_get_name(pdmeta, k, buf);
2003 vol = g_raid_create_volume(sc, buf,
2004 GET16D(pdmeta, vde->VD_Number));
2005 pv = malloc(sizeof(*pv), M_MD_DDF, M_WAITOK | M_ZERO);
2006 vol->v_md_data = pv;
2007 callout_init(&pv->pv_start_co, 1);
2008 callout_reset(&pv->pv_start_co,
2009 g_raid_start_timeout * hz,
2010 g_raid_ddf_go, vol);
2011 mdi->mdio_starting++;
2013 pv = vol->v_md_data;
2015 /* If we haven't started yet - check metadata freshness. */
2016 vmeta = &pv->pv_meta;
2017 ddf_vol_meta_update(vmeta, pdmeta, vdc->VD_GUID, pv->pv_started);
2021 g_raid_change_disk_state(disk, G_RAID_DISK_S_SPARE);
2022 g_raid_md_ddf_refill(sc);
2025 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2026 pv = vol->v_md_data;
2027 vmeta = &pv->pv_meta;
2029 if (ddf_meta_find_vdc(pdmeta, vmeta->vdc->VD_GUID) == NULL)
2032 if (pv->pv_started) {
2033 if (g_raid_md_ddf_start_disk(disk, vol))
2034 g_raid_md_write_ddf(md, vol, NULL, NULL);
2038 /* If we collected all needed disks - start array. */
2041 for (k = 0; k < GET8(vmeta, vdc->Secondary_Element_Count); k++) {
2042 if (vmeta->bvdc[k] == NULL) {
2043 need += GET16(vmeta, vdc->Primary_Element_Count);
2046 cnt = GET16(vmeta, bvdc[k]->Primary_Element_Count);
2048 for (i = 0; i < cnt; i++) {
2049 val = GET32(vmeta, bvdc[k]->Physical_Disk_Sequence[i]);
2050 if (g_raid_md_ddf_get_disk(sc, NULL, val) != NULL)
2054 G_RAID_DEBUG1(1, sc, "Volume %s now has %d of %d disks",
2055 vol->v_name, have, need);
2057 g_raid_md_ddf_start(vol);
2062 g_raid_md_create_req_ddf(struct g_raid_md_object *md, struct g_class *mp,
2063 struct gctl_req *req, struct g_geom **gp)
2065 struct g_geom *geom;
2066 struct g_raid_softc *sc;
2067 struct g_raid_md_ddf_object *mdi, *mdi1;
2072 mdi = (struct g_raid_md_ddf_object *)md;
2073 fmtopt = gctl_get_asciiparam(req, "fmtopt");
2074 if (fmtopt == NULL || strcasecmp(fmtopt, "BE") == 0)
2076 else if (strcasecmp(fmtopt, "LE") == 0)
2079 gctl_error(req, "Incorrect fmtopt argument.");
2080 return (G_RAID_MD_TASTE_FAIL);
2083 /* Search for existing node. */
2084 LIST_FOREACH(geom, &mp->geom, geom) {
2088 if (sc->sc_stopping != 0)
2090 if (sc->sc_md->mdo_class != md->mdo_class)
2092 mdi1 = (struct g_raid_md_ddf_object *)sc->sc_md;
2093 if (mdi1->mdio_bigendian != be)
2099 return (G_RAID_MD_TASTE_EXISTING);
2102 /* Create new one if not found. */
2103 mdi->mdio_bigendian = be;
2104 snprintf(name, sizeof(name), "DDF%s", be ? "" : "-LE");
2105 sc = g_raid_create_node(mp, name, md);
2107 return (G_RAID_MD_TASTE_FAIL);
2110 return (G_RAID_MD_TASTE_NEW);
2114 g_raid_md_taste_ddf(struct g_raid_md_object *md, struct g_class *mp,
2115 struct g_consumer *cp, struct g_geom **gp)
2117 struct g_consumer *rcp;
2118 struct g_provider *pp;
2119 struct g_raid_softc *sc;
2120 struct g_raid_disk *disk;
2121 struct ddf_meta meta;
2122 struct g_raid_md_ddf_perdisk *pd;
2123 struct g_raid_md_ddf_object *mdi;
2124 struct g_geom *geom;
2125 int error, result, be;
2128 G_RAID_DEBUG(1, "Tasting DDF on %s", cp->provider->name);
2129 mdi = (struct g_raid_md_ddf_object *)md;
2132 /* Read metadata from device. */
2133 g_topology_unlock();
2134 bzero(&meta, sizeof(meta));
2135 error = ddf_meta_read(cp, &meta);
2138 return (G_RAID_MD_TASTE_FAIL);
2139 be = meta.bigendian;
2141 /* Metadata valid. Print it. */
2142 g_raid_md_ddf_print(&meta);
2144 /* Search for matching node. */
2146 LIST_FOREACH(geom, &mp->geom, geom) {
2150 if (sc->sc_stopping != 0)
2152 if (sc->sc_md->mdo_class != md->mdo_class)
2154 mdi = (struct g_raid_md_ddf_object *)sc->sc_md;
2155 if (mdi->mdio_bigendian != be)
2160 /* Found matching node. */
2162 G_RAID_DEBUG(1, "Found matching array %s", sc->sc_name);
2163 result = G_RAID_MD_TASTE_EXISTING;
2165 } else { /* Not found matching node -- create one. */
2166 result = G_RAID_MD_TASTE_NEW;
2167 mdi->mdio_bigendian = be;
2168 snprintf(name, sizeof(name), "DDF%s", be ? "" : "-LE");
2169 sc = g_raid_create_node(mp, name, md);
2174 /* There is no return after this point, so we close passed consumer. */
2175 g_access(cp, -1, 0, 0);
2177 rcp = g_new_consumer(geom);
2178 rcp->flags |= G_CF_DIRECT_RECEIVE;
2180 if (g_access(rcp, 1, 1, 1) != 0)
2183 g_topology_unlock();
2184 sx_xlock(&sc->sc_lock);
2186 pd = malloc(sizeof(*pd), M_MD_DDF, M_WAITOK | M_ZERO);
2188 disk = g_raid_create_disk(sc);
2189 disk->d_md_data = (void *)pd;
2190 disk->d_consumer = rcp;
2191 rcp->private = disk;
2193 g_raid_get_disk_info(disk);
2195 g_raid_md_ddf_new_disk(disk);
2197 sx_xunlock(&sc->sc_lock);
2204 g_raid_md_event_ddf(struct g_raid_md_object *md,
2205 struct g_raid_disk *disk, u_int event)
2207 struct g_raid_softc *sc;
2213 case G_RAID_DISK_E_DISCONNECTED:
2215 g_raid_change_disk_state(disk, G_RAID_DISK_S_NONE);
2216 g_raid_destroy_disk(disk);
2217 g_raid_md_ddf_purge_volumes(sc);
2219 /* Write updated metadata to all disks. */
2220 g_raid_md_write_ddf(md, NULL, NULL, NULL);
2222 /* Check if anything left. */
2223 if (g_raid_ndisks(sc, -1) == 0)
2224 g_raid_destroy_node(sc, 0);
2226 g_raid_md_ddf_refill(sc);
2233 g_raid_md_volume_event_ddf(struct g_raid_md_object *md,
2234 struct g_raid_volume *vol, u_int event)
2236 struct g_raid_md_ddf_pervolume *pv;
2238 pv = (struct g_raid_md_ddf_pervolume *)vol->v_md_data;
2240 case G_RAID_VOLUME_E_STARTMD:
2241 if (!pv->pv_started)
2242 g_raid_md_ddf_start(vol);
2249 g_raid_md_ctl_ddf(struct g_raid_md_object *md,
2250 struct gctl_req *req)
2252 struct g_raid_softc *sc;
2253 struct g_raid_volume *vol, *vol1;
2254 struct g_raid_subdisk *sd;
2255 struct g_raid_disk *disk, *disks[DDF_MAX_DISKS_HARD];
2256 struct g_raid_md_ddf_perdisk *pd;
2257 struct g_raid_md_ddf_pervolume *pv;
2258 struct g_raid_md_ddf_object *mdi;
2259 struct ddf_sa_record *sa;
2260 struct g_consumer *cp;
2261 struct g_provider *pp;
2263 const char *nodename, *verb, *volname, *levelname, *diskname;
2266 off_t size, sectorsize, strip, offs[DDF_MAX_DISKS_HARD], esize;
2267 intmax_t *sizearg, *striparg;
2268 int i, numdisks, len, level, qual;
2272 mdi = (struct g_raid_md_ddf_object *)md;
2273 verb = gctl_get_param(req, "verb", NULL);
2274 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
2277 if (strcmp(verb, "label") == 0) {
2280 gctl_error(req, "Invalid number of arguments.");
2283 volname = gctl_get_asciiparam(req, "arg1");
2284 if (volname == NULL) {
2285 gctl_error(req, "No volume name.");
2288 levelname = gctl_get_asciiparam(req, "arg2");
2289 if (levelname == NULL) {
2290 gctl_error(req, "No RAID level.");
2293 if (g_raid_volume_str2level(levelname, &level, &qual)) {
2294 gctl_error(req, "Unknown RAID level '%s'.", levelname);
2297 numdisks = *nargs - 3;
2298 force = gctl_get_paraml(req, "force", sizeof(*force));
2299 if (!g_raid_md_ddf_supported(level, qual, numdisks,
2300 force ? *force : 0)) {
2301 gctl_error(req, "Unsupported RAID level "
2302 "(0x%02x/0x%02x), or number of disks (%d).",
2303 level, qual, numdisks);
2307 /* Search for disks, connect them and probe. */
2310 bzero(disks, sizeof(disks));
2311 bzero(offs, sizeof(offs));
2312 for (i = 0; i < numdisks; i++) {
2313 snprintf(arg, sizeof(arg), "arg%d", i + 3);
2314 diskname = gctl_get_asciiparam(req, arg);
2315 if (diskname == NULL) {
2316 gctl_error(req, "No disk name (%s).", arg);
2320 if (strcmp(diskname, "NONE") == 0)
2323 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2324 if (disk->d_consumer != NULL &&
2325 disk->d_consumer->provider != NULL &&
2326 strcmp(disk->d_consumer->provider->name,
2331 if (disk->d_state != G_RAID_DISK_S_ACTIVE) {
2332 gctl_error(req, "Disk '%s' is in a "
2333 "wrong state (%s).", diskname,
2334 g_raid_disk_state2str(disk->d_state));
2338 pd = disk->d_md_data;
2339 if (ddf_meta_count_vdc(&pd->pd_meta, NULL) >=
2340 GET16(&pd->pd_meta, hdr->Max_Partitions)) {
2341 gctl_error(req, "No free partitions "
2347 pp = disk->d_consumer->provider;
2349 ddf_meta_unused_range(&pd->pd_meta,
2351 offs[i] *= pp->sectorsize;
2352 size = MIN(size, (off_t)esize * pp->sectorsize);
2353 sectorsize = MAX(sectorsize, pp->sectorsize);
2358 cp = g_raid_open_consumer(sc, diskname);
2360 gctl_error(req, "Can't open disk '%s'.",
2362 g_topology_unlock();
2367 pd = malloc(sizeof(*pd), M_MD_DDF, M_WAITOK | M_ZERO);
2368 disk = g_raid_create_disk(sc);
2369 disk->d_md_data = (void *)pd;
2370 disk->d_consumer = cp;
2373 ddf_meta_create(disk, &mdi->mdio_meta);
2374 if (mdi->mdio_meta.hdr == NULL)
2375 ddf_meta_copy(&mdi->mdio_meta, &pd->pd_meta);
2377 ddf_meta_update(&mdi->mdio_meta, &pd->pd_meta);
2378 g_topology_unlock();
2380 g_raid_get_disk_info(disk);
2382 /* Reserve some space for metadata. */
2383 size = MIN(size, GET64(&pd->pd_meta,
2384 pdr->entry[0].Configured_Size) * pp->sectorsize);
2385 sectorsize = MAX(sectorsize, pp->sectorsize);
2388 for (i = 0; i < numdisks; i++) {
2389 if (disks[i] != NULL &&
2390 disks[i]->d_state == G_RAID_DISK_S_NONE)
2391 g_raid_destroy_disk(disks[i]);
2396 if (sectorsize <= 0) {
2397 gctl_error(req, "Can't get sector size.");
2401 /* Handle size argument. */
2402 len = sizeof(*sizearg);
2403 sizearg = gctl_get_param(req, "size", &len);
2404 if (sizearg != NULL && len == sizeof(*sizearg) &&
2406 if (*sizearg > size) {
2407 gctl_error(req, "Size too big %lld > %lld.",
2408 (long long)*sizearg, (long long)size);
2414 /* Handle strip argument. */
2416 len = sizeof(*striparg);
2417 striparg = gctl_get_param(req, "strip", &len);
2418 if (striparg != NULL && len == sizeof(*striparg) &&
2420 if (*striparg < sectorsize) {
2421 gctl_error(req, "Strip size too small.");
2424 if (*striparg % sectorsize != 0) {
2425 gctl_error(req, "Incorrect strip size.");
2431 /* Round size down to strip or sector. */
2432 if (level == G_RAID_VOLUME_RL_RAID1 ||
2433 level == G_RAID_VOLUME_RL_RAID3 ||
2434 level == G_RAID_VOLUME_RL_SINGLE ||
2435 level == G_RAID_VOLUME_RL_CONCAT)
2436 size -= (size % sectorsize);
2437 else if (level == G_RAID_VOLUME_RL_RAID1E &&
2438 (numdisks & 1) != 0)
2439 size -= (size % (2 * strip));
2441 size -= (size % strip);
2443 gctl_error(req, "Size too small.");
2447 /* We have all we need, create things: volume, ... */
2448 pv = malloc(sizeof(*pv), M_MD_DDF, M_WAITOK | M_ZERO);
2449 ddf_vol_meta_create(&pv->pv_meta, &mdi->mdio_meta);
2451 vol = g_raid_create_volume(sc, volname, -1);
2452 vol->v_md_data = pv;
2453 vol->v_raid_level = level;
2454 vol->v_raid_level_qualifier = qual;
2455 vol->v_strip_size = strip;
2456 vol->v_disks_count = numdisks;
2457 if (level == G_RAID_VOLUME_RL_RAID0 ||
2458 level == G_RAID_VOLUME_RL_CONCAT ||
2459 level == G_RAID_VOLUME_RL_SINGLE)
2460 vol->v_mediasize = size * numdisks;
2461 else if (level == G_RAID_VOLUME_RL_RAID1)
2462 vol->v_mediasize = size;
2463 else if (level == G_RAID_VOLUME_RL_RAID3 ||
2464 level == G_RAID_VOLUME_RL_RAID4 ||
2465 level == G_RAID_VOLUME_RL_RAID5)
2466 vol->v_mediasize = size * (numdisks - 1);
2467 else if (level == G_RAID_VOLUME_RL_RAID5R) {
2468 vol->v_mediasize = size * (numdisks - 1);
2469 vol->v_rotate_parity = 1024;
2470 } else if (level == G_RAID_VOLUME_RL_RAID6 ||
2471 level == G_RAID_VOLUME_RL_RAID5E ||
2472 level == G_RAID_VOLUME_RL_RAID5EE)
2473 vol->v_mediasize = size * (numdisks - 2);
2474 else if (level == G_RAID_VOLUME_RL_RAIDMDF) {
2476 vol->v_mdf_pdisks = 2;
2478 vol->v_mdf_pdisks = 3;
2479 vol->v_mdf_polynomial = 0x11d;
2480 vol->v_mdf_method = 0x00;
2481 vol->v_mediasize = size * (numdisks - vol->v_mdf_pdisks);
2482 } else { /* RAID1E */
2483 vol->v_mediasize = ((size * numdisks) / strip / 2) *
2486 vol->v_sectorsize = sectorsize;
2487 g_raid_start_volume(vol);
2489 /* , and subdisks. */
2490 for (i = 0; i < numdisks; i++) {
2492 sd = &vol->v_subdisks[i];
2494 sd->sd_offset = offs[i];
2498 TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
2499 g_raid_change_disk_state(disk,
2500 G_RAID_DISK_S_ACTIVE);
2501 g_raid_change_subdisk_state(sd,
2502 G_RAID_SUBDISK_S_ACTIVE);
2503 g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW,
2504 G_RAID_EVENT_SUBDISK);
2507 /* Write metadata based on created entities. */
2508 G_RAID_DEBUG1(0, sc, "Array started.");
2509 g_raid_md_write_ddf(md, vol, NULL, NULL);
2511 /* Pickup any STALE/SPARE disks to refill array if needed. */
2512 g_raid_md_ddf_refill(sc);
2514 g_raid_event_send(vol, G_RAID_VOLUME_E_START,
2515 G_RAID_EVENT_VOLUME);
2518 if (strcmp(verb, "add") == 0) {
2520 gctl_error(req, "`add` command is not applicable, "
2521 "use `label` instead.");
2524 if (strcmp(verb, "delete") == 0) {
2526 nodename = gctl_get_asciiparam(req, "arg0");
2527 if (nodename != NULL && strcasecmp(sc->sc_name, nodename) != 0)
2530 /* Full node destruction. */
2531 if (*nargs == 1 && nodename != NULL) {
2532 /* Check if some volume is still open. */
2533 force = gctl_get_paraml(req, "force", sizeof(*force));
2534 if (force != NULL && *force == 0 &&
2535 g_raid_nopens(sc) != 0) {
2536 gctl_error(req, "Some volume is still open.");
2540 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2541 if (disk->d_consumer)
2542 ddf_meta_erase(disk->d_consumer);
2544 g_raid_destroy_node(sc, 0);
2548 /* Destroy specified volume. If it was last - all node. */
2550 gctl_error(req, "Invalid number of arguments.");
2553 volname = gctl_get_asciiparam(req,
2554 nodename != NULL ? "arg1" : "arg0");
2555 if (volname == NULL) {
2556 gctl_error(req, "No volume name.");
2560 /* Search for volume. */
2561 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2562 if (strcmp(vol->v_name, volname) == 0)
2564 pp = vol->v_provider;
2567 if (strcmp(pp->name, volname) == 0)
2569 if (strncmp(pp->name, "raid/", 5) == 0 &&
2570 strcmp(pp->name + 5, volname) == 0)
2574 i = strtol(volname, &tmp, 10);
2575 if (verb != volname && tmp[0] == 0) {
2576 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2577 if (vol->v_global_id == i)
2583 gctl_error(req, "Volume '%s' not found.", volname);
2587 /* Check if volume is still open. */
2588 force = gctl_get_paraml(req, "force", sizeof(*force));
2589 if (force != NULL && *force == 0 &&
2590 vol->v_provider_open != 0) {
2591 gctl_error(req, "Volume is still open.");
2595 /* Destroy volume and potentially node. */
2597 TAILQ_FOREACH(vol1, &sc->sc_volumes, v_next)
2600 g_raid_destroy_volume(vol);
2601 g_raid_md_ddf_purge_disks(sc);
2602 g_raid_md_write_ddf(md, NULL, NULL, NULL);
2604 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2605 if (disk->d_consumer)
2606 ddf_meta_erase(disk->d_consumer);
2608 g_raid_destroy_node(sc, 0);
2612 if (strcmp(verb, "remove") == 0 ||
2613 strcmp(verb, "fail") == 0) {
2615 gctl_error(req, "Invalid number of arguments.");
2618 for (i = 1; i < *nargs; i++) {
2619 snprintf(arg, sizeof(arg), "arg%d", i);
2620 diskname = gctl_get_asciiparam(req, arg);
2621 if (diskname == NULL) {
2622 gctl_error(req, "No disk name (%s).", arg);
2626 if (strncmp(diskname, "/dev/", 5) == 0)
2629 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2630 if (disk->d_consumer != NULL &&
2631 disk->d_consumer->provider != NULL &&
2632 strcmp(disk->d_consumer->provider->name,
2637 gctl_error(req, "Disk '%s' not found.",
2643 if (strcmp(verb, "fail") == 0) {
2644 g_raid_md_fail_disk_ddf(md, NULL, disk);
2648 /* Erase metadata on deleting disk and destroy it. */
2649 ddf_meta_erase(disk->d_consumer);
2650 g_raid_destroy_disk(disk);
2652 g_raid_md_ddf_purge_volumes(sc);
2654 /* Write updated metadata to remaining disks. */
2655 g_raid_md_write_ddf(md, NULL, NULL, NULL);
2657 /* Check if anything left. */
2658 if (g_raid_ndisks(sc, -1) == 0)
2659 g_raid_destroy_node(sc, 0);
2661 g_raid_md_ddf_refill(sc);
2664 if (strcmp(verb, "insert") == 0) {
2666 gctl_error(req, "Invalid number of arguments.");
2669 for (i = 1; i < *nargs; i++) {
2670 /* Get disk name. */
2671 snprintf(arg, sizeof(arg), "arg%d", i);
2672 diskname = gctl_get_asciiparam(req, arg);
2673 if (diskname == NULL) {
2674 gctl_error(req, "No disk name (%s).", arg);
2679 /* Try to find provider with specified name. */
2681 cp = g_raid_open_consumer(sc, diskname);
2683 gctl_error(req, "Can't open disk '%s'.",
2685 g_topology_unlock();
2690 g_topology_unlock();
2692 pd = malloc(sizeof(*pd), M_MD_DDF, M_WAITOK | M_ZERO);
2694 disk = g_raid_create_disk(sc);
2695 disk->d_consumer = cp;
2696 disk->d_md_data = (void *)pd;
2699 g_raid_get_disk_info(disk);
2701 /* Welcome the "new" disk. */
2702 g_raid_change_disk_state(disk, G_RAID_DISK_S_SPARE);
2703 ddf_meta_create(disk, &mdi->mdio_meta);
2704 sa = ddf_meta_find_sa(&pd->pd_meta, 1);
2706 SET32D(&pd->pd_meta, sa->Signature,
2708 SET8D(&pd->pd_meta, sa->Spare_Type, 0);
2709 SET16D(&pd->pd_meta, sa->Populated_SAEs, 0);
2710 SET16D(&pd->pd_meta, sa->MAX_SAE_Supported,
2711 (GET16(&pd->pd_meta, hdr->Configuration_Record_Length) *
2712 pd->pd_meta.sectorsize -
2713 sizeof(struct ddf_sa_record)) /
2714 sizeof(struct ddf_sa_entry));
2716 if (mdi->mdio_meta.hdr == NULL)
2717 ddf_meta_copy(&mdi->mdio_meta, &pd->pd_meta);
2719 ddf_meta_update(&mdi->mdio_meta, &pd->pd_meta);
2720 g_raid_md_write_ddf(md, NULL, NULL, NULL);
2721 g_raid_md_ddf_refill(sc);
2729 g_raid_md_write_ddf(struct g_raid_md_object *md, struct g_raid_volume *tvol,
2730 struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk)
2732 struct g_raid_softc *sc;
2733 struct g_raid_volume *vol;
2734 struct g_raid_subdisk *sd;
2735 struct g_raid_disk *disk;
2736 struct g_raid_md_ddf_perdisk *pd;
2737 struct g_raid_md_ddf_pervolume *pv;
2738 struct g_raid_md_ddf_object *mdi;
2739 struct ddf_meta *gmeta;
2740 struct ddf_vol_meta *vmeta;
2741 struct ddf_vdc_record *vdc;
2742 struct ddf_sa_record *sa;
2744 int i, j, pos, bvd, size;
2747 mdi = (struct g_raid_md_ddf_object *)md;
2748 gmeta = &mdi->mdio_meta;
2750 if (sc->sc_stopping == G_RAID_DESTROY_HARD)
2754 * Clear disk flags to let only really needed ones to be reset.
2755 * Do it only if there are no volumes in starting state now,
2756 * as they can update disk statuses yet and we may kill innocent.
2758 if (mdi->mdio_starting == 0) {
2759 for (i = 0; i < GET16(gmeta, pdr->Populated_PDEs); i++) {
2760 if (isff(gmeta->pdr->entry[i].PD_GUID, 24))
2762 SET16(gmeta, pdr->entry[i].PD_Type,
2763 GET16(gmeta, pdr->entry[i].PD_Type) &
2764 ~(DDF_PDE_PARTICIPATING |
2765 DDF_PDE_GLOBAL_SPARE | DDF_PDE_CONFIG_SPARE));
2766 if ((GET16(gmeta, pdr->entry[i].PD_State) &
2768 SET16(gmeta, pdr->entry[i].PD_State, 0);
2772 /* Generate/update new per-volume metadata. */
2773 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2774 pv = (struct g_raid_md_ddf_pervolume *)vol->v_md_data;
2775 if (vol->v_stopping || !pv->pv_started)
2777 vmeta = &pv->pv_meta;
2779 SET32(vmeta, vdc->Sequence_Number,
2780 GET32(vmeta, vdc->Sequence_Number) + 1);
2781 if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E &&
2782 vol->v_disks_count % 2 == 0)
2783 SET16(vmeta, vdc->Primary_Element_Count, 2);
2785 SET16(vmeta, vdc->Primary_Element_Count,
2786 vol->v_disks_count);
2787 SET8(vmeta, vdc->Stripe_Size,
2788 ffs(vol->v_strip_size / vol->v_sectorsize) - 1);
2789 if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E &&
2790 vol->v_disks_count % 2 == 0) {
2791 SET8(vmeta, vdc->Primary_RAID_Level,
2793 SET8(vmeta, vdc->RLQ, 0);
2794 SET8(vmeta, vdc->Secondary_Element_Count,
2795 vol->v_disks_count / 2);
2796 SET8(vmeta, vdc->Secondary_RAID_Level, 0);
2798 SET8(vmeta, vdc->Primary_RAID_Level,
2800 SET8(vmeta, vdc->RLQ,
2801 vol->v_raid_level_qualifier);
2802 SET8(vmeta, vdc->Secondary_Element_Count, 1);
2803 SET8(vmeta, vdc->Secondary_RAID_Level, 0);
2805 SET8(vmeta, vdc->Secondary_Element_Seq, 0);
2806 SET64(vmeta, vdc->Block_Count, 0);
2807 SET64(vmeta, vdc->VD_Size, vol->v_mediasize / vol->v_sectorsize);
2808 SET16(vmeta, vdc->Block_Size, vol->v_sectorsize);
2809 SET8(vmeta, vdc->Rotate_Parity_count,
2810 fls(vol->v_rotate_parity) - 1);
2811 SET8(vmeta, vdc->MDF_Parity_Disks, vol->v_mdf_pdisks);
2812 SET16(vmeta, vdc->MDF_Parity_Generator_Polynomial,
2813 vol->v_mdf_polynomial);
2814 SET8(vmeta, vdc->MDF_Constant_Generation_Method,
2817 SET16(vmeta, vde->VD_Number, vol->v_global_id);
2818 if (vol->v_state <= G_RAID_VOLUME_S_BROKEN)
2819 SET8(vmeta, vde->VD_State, DDF_VDE_FAILED);
2820 else if (vol->v_state <= G_RAID_VOLUME_S_DEGRADED)
2821 SET8(vmeta, vde->VD_State, DDF_VDE_DEGRADED);
2822 else if (vol->v_state <= G_RAID_VOLUME_S_SUBOPTIMAL)
2823 SET8(vmeta, vde->VD_State, DDF_VDE_PARTIAL);
2825 SET8(vmeta, vde->VD_State, DDF_VDE_OPTIMAL);
2827 g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_STALE) > 0 ||
2828 g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_RESYNC) > 0)
2829 SET8(vmeta, vde->VD_State,
2830 GET8(vmeta, vde->VD_State) | DDF_VDE_DIRTY);
2831 SET8(vmeta, vde->Init_State, DDF_VDE_INIT_FULL); // XXX
2832 ddf_meta_put_name(vmeta, vol->v_name);
2834 for (i = 0; i < vol->v_disks_count; i++) {
2835 sd = &vol->v_subdisks[i];
2836 bvd = i / GET16(vmeta, vdc->Primary_Element_Count);
2837 pos = i % GET16(vmeta, vdc->Primary_Element_Count);
2840 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
2841 if (vmeta->bvdc[bvd] == NULL) {
2843 hdr->Configuration_Record_Length) *
2845 vmeta->bvdc[bvd] = malloc(size,
2846 M_MD_DDF, M_WAITOK);
2847 memset(vmeta->bvdc[bvd], 0xff, size);
2849 memcpy(vmeta->bvdc[bvd], vmeta->vdc,
2850 sizeof(struct ddf_vdc_record));
2851 SET8(vmeta, bvdc[bvd]->Secondary_Element_Seq, bvd);
2852 SET64(vmeta, bvdc[bvd]->Block_Count,
2853 sd->sd_size / vol->v_sectorsize);
2854 SET32(vmeta, bvdc[bvd]->Physical_Disk_Sequence[pos],
2855 GET32(&pd->pd_meta, pdd->PD_Reference));
2856 val2 = (uint64_t *)&(vmeta->bvdc[bvd]->Physical_Disk_Sequence[
2857 GET16(vmeta, hdr->Max_Primary_Element_Entries)]);
2858 SET64P(vmeta, val2 + pos,
2859 sd->sd_offset / vol->v_sectorsize);
2861 if (vmeta->bvdc[bvd] == NULL)
2864 j = ddf_meta_find_pd(gmeta, NULL,
2865 GET32(vmeta, bvdc[bvd]->Physical_Disk_Sequence[pos]));
2868 SET16(gmeta, pdr->entry[j].PD_Type,
2869 GET16(gmeta, pdr->entry[j].PD_Type) |
2870 DDF_PDE_PARTICIPATING);
2871 if (sd->sd_state == G_RAID_SUBDISK_S_NONE)
2872 SET16(gmeta, pdr->entry[j].PD_State,
2873 GET16(gmeta, pdr->entry[j].PD_State) |
2874 (DDF_PDE_FAILED | DDF_PDE_MISSING));
2875 else if (sd->sd_state == G_RAID_SUBDISK_S_FAILED)
2876 SET16(gmeta, pdr->entry[j].PD_State,
2877 GET16(gmeta, pdr->entry[j].PD_State) |
2878 (DDF_PDE_FAILED | DDF_PDE_PFA));
2879 else if (sd->sd_state <= G_RAID_SUBDISK_S_REBUILD)
2880 SET16(gmeta, pdr->entry[j].PD_State,
2881 GET16(gmeta, pdr->entry[j].PD_State) |
2884 SET16(gmeta, pdr->entry[j].PD_State,
2885 GET16(gmeta, pdr->entry[j].PD_State) |
2890 /* Mark spare and failed disks as such. */
2891 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2892 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
2893 i = ddf_meta_find_pd(gmeta, NULL,
2894 GET32(&pd->pd_meta, pdd->PD_Reference));
2897 if (disk->d_state == G_RAID_DISK_S_FAILED) {
2898 SET16(gmeta, pdr->entry[i].PD_State,
2899 GET16(gmeta, pdr->entry[i].PD_State) |
2900 (DDF_PDE_FAILED | DDF_PDE_PFA));
2902 if (disk->d_state != G_RAID_DISK_S_SPARE)
2904 sa = ddf_meta_find_sa(&pd->pd_meta, 0);
2906 (GET8D(&pd->pd_meta, sa->Spare_Type) &
2907 DDF_SAR_TYPE_DEDICATED) == 0) {
2908 SET16(gmeta, pdr->entry[i].PD_Type,
2909 GET16(gmeta, pdr->entry[i].PD_Type) |
2910 DDF_PDE_GLOBAL_SPARE);
2912 SET16(gmeta, pdr->entry[i].PD_Type,
2913 GET16(gmeta, pdr->entry[i].PD_Type) |
2914 DDF_PDE_CONFIG_SPARE);
2916 SET16(gmeta, pdr->entry[i].PD_State,
2917 GET16(gmeta, pdr->entry[i].PD_State) |
2921 /* Remove disks without "participating" flag (unused). */
2922 for (i = 0, j = -1; i < GET16(gmeta, pdr->Populated_PDEs); i++) {
2923 if (isff(gmeta->pdr->entry[i].PD_GUID, 24))
2925 if ((GET16(gmeta, pdr->entry[i].PD_Type) &
2926 (DDF_PDE_PARTICIPATING |
2927 DDF_PDE_GLOBAL_SPARE | DDF_PDE_CONFIG_SPARE)) != 0 ||
2928 g_raid_md_ddf_get_disk(sc,
2929 NULL, GET32(gmeta, pdr->entry[i].PD_Reference)) != NULL)
2932 memset(&gmeta->pdr->entry[i], 0xff,
2933 sizeof(struct ddf_pd_entry));
2935 SET16(gmeta, pdr->Populated_PDEs, j + 1);
2937 /* Update per-disk metadata and write them. */
2938 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2939 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
2940 if (disk->d_state != G_RAID_DISK_S_ACTIVE &&
2941 disk->d_state != G_RAID_DISK_S_SPARE)
2944 memcpy(pd->pd_meta.pdr, gmeta->pdr,
2945 GET32(&pd->pd_meta, hdr->pdr_length) *
2946 pd->pd_meta.sectorsize);
2948 SET16(&pd->pd_meta, vdr->Populated_VDEs, 0);
2949 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2950 if (vol->v_stopping)
2952 pv = (struct g_raid_md_ddf_pervolume *)vol->v_md_data;
2953 i = ddf_meta_find_vd(&pd->pd_meta,
2954 pv->pv_meta.vde->VD_GUID);
2956 i = ddf_meta_find_vd(&pd->pd_meta, NULL);
2958 memcpy(&pd->pd_meta.vdr->entry[i],
2960 sizeof(struct ddf_vd_entry));
2963 if (mdi->mdio_starting == 0) {
2964 /* Remove all VDCs to restore needed later. */
2965 j = GETCRNUM(&pd->pd_meta);
2966 for (i = 0; i < j; i++) {
2967 vdc = GETVDCPTR(&pd->pd_meta, i);
2968 if (GET32D(&pd->pd_meta, vdc->Signature) !=
2971 SET32D(&pd->pd_meta, vdc->Signature, 0xffffffff);
2974 TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
2975 vol = sd->sd_volume;
2976 if (vol->v_stopping)
2978 pv = (struct g_raid_md_ddf_pervolume *)vol->v_md_data;
2979 vmeta = &pv->pv_meta;
2980 vdc = ddf_meta_find_vdc(&pd->pd_meta,
2981 vmeta->vde->VD_GUID);
2983 vdc = ddf_meta_find_vdc(&pd->pd_meta, NULL);
2985 bvd = sd->sd_pos / GET16(vmeta,
2986 vdc->Primary_Element_Count);
2987 memcpy(vdc, vmeta->bvdc[bvd],
2989 hdr->Configuration_Record_Length) *
2990 pd->pd_meta.sectorsize);
2993 G_RAID_DEBUG(1, "Writing DDF metadata to %s",
2994 g_raid_get_diskname(disk));
2995 g_raid_md_ddf_print(&pd->pd_meta);
2996 ddf_meta_write(disk->d_consumer, &pd->pd_meta);
3002 g_raid_md_fail_disk_ddf(struct g_raid_md_object *md,
3003 struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk)
3005 struct g_raid_softc *sc;
3006 struct g_raid_md_ddf_perdisk *pd;
3007 struct g_raid_subdisk *sd;
3011 pd = (struct g_raid_md_ddf_perdisk *)tdisk->d_md_data;
3013 /* We can't fail disk that is not a part of array now. */
3014 if (tdisk->d_state != G_RAID_DISK_S_ACTIVE)
3018 * Mark disk as failed in metadata and try to write that metadata
3019 * to the disk itself to prevent it's later resurrection as STALE.
3021 G_RAID_DEBUG(1, "Writing DDF metadata to %s",
3022 g_raid_get_diskname(tdisk));
3023 i = ddf_meta_find_pd(&pd->pd_meta, NULL, GET32(&pd->pd_meta, pdd->PD_Reference));
3024 SET16(&pd->pd_meta, pdr->entry[i].PD_State, DDF_PDE_FAILED | DDF_PDE_PFA);
3025 if (tdisk->d_consumer != NULL)
3026 ddf_meta_write(tdisk->d_consumer, &pd->pd_meta);
3028 /* Change states. */
3029 g_raid_change_disk_state(tdisk, G_RAID_DISK_S_FAILED);
3030 TAILQ_FOREACH(sd, &tdisk->d_subdisks, sd_next) {
3031 g_raid_change_subdisk_state(sd,
3032 G_RAID_SUBDISK_S_FAILED);
3033 g_raid_event_send(sd, G_RAID_SUBDISK_E_FAILED,
3034 G_RAID_EVENT_SUBDISK);
3037 /* Write updated metadata to remaining disks. */
3038 g_raid_md_write_ddf(md, NULL, NULL, tdisk);
3040 g_raid_md_ddf_refill(sc);
3045 g_raid_md_free_disk_ddf(struct g_raid_md_object *md,
3046 struct g_raid_disk *disk)
3048 struct g_raid_md_ddf_perdisk *pd;
3050 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
3051 ddf_meta_free(&pd->pd_meta);
3053 disk->d_md_data = NULL;
3058 g_raid_md_free_volume_ddf(struct g_raid_md_object *md,
3059 struct g_raid_volume *vol)
3061 struct g_raid_md_ddf_object *mdi;
3062 struct g_raid_md_ddf_pervolume *pv;
3064 mdi = (struct g_raid_md_ddf_object *)md;
3065 pv = (struct g_raid_md_ddf_pervolume *)vol->v_md_data;
3066 ddf_vol_meta_free(&pv->pv_meta);
3067 if (!pv->pv_started) {
3069 mdi->mdio_starting--;
3070 callout_stop(&pv->pv_start_co);
3073 vol->v_md_data = NULL;
3078 g_raid_md_free_ddf(struct g_raid_md_object *md)
3080 struct g_raid_md_ddf_object *mdi;
3082 mdi = (struct g_raid_md_ddf_object *)md;
3083 if (!mdi->mdio_started) {
3084 mdi->mdio_started = 0;
3085 callout_stop(&mdi->mdio_start_co);
3086 G_RAID_DEBUG1(1, md->mdo_softc,
3087 "root_mount_rel %p", mdi->mdio_rootmount);
3088 root_mount_rel(mdi->mdio_rootmount);
3089 mdi->mdio_rootmount = NULL;
3091 ddf_meta_free(&mdi->mdio_meta);
3095 G_RAID_MD_DECLARE(ddf, "DDF");