2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2012 Alexander Motin <mav@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
32 #include <sys/gsb_crc32.h>
33 #include <sys/endian.h>
34 #include <sys/kernel.h>
36 #include <sys/limits.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
40 #include <sys/systm.h>
42 #include <sys/clock.h>
44 #include <geom/geom.h>
45 #include <geom/geom_dbg.h>
46 #include "geom/raid/g_raid.h"
47 #include "geom/raid/md_ddf.h"
48 #include "g_raid_md_if.h"
50 static MALLOC_DEFINE(M_MD_DDF, "md_ddf_data", "GEOM_RAID DDF metadata");
52 #define DDF_MAX_DISKS_HARD 128
54 #define DDF_MAX_DISKS 16
55 #define DDF_MAX_VDISKS 7
56 #define DDF_MAX_PARTITIONS 1
58 #define DECADE (3600*24*(365*10+2)) /* 10 years in seconds. */
63 struct ddf_header *hdr;
64 struct ddf_cd_record *cdr;
65 struct ddf_pd_record *pdr;
66 struct ddf_vd_record *vdr;
68 struct ddf_pdd_record *pdd;
69 struct ddf_bbm_log *bbm;
75 struct ddf_header *hdr;
76 struct ddf_cd_record *cdr;
77 struct ddf_vd_entry *vde;
78 struct ddf_vdc_record *vdc;
79 struct ddf_vdc_record *bvdc[DDF_MAX_DISKS_HARD];
82 struct g_raid_md_ddf_perdisk {
83 struct ddf_meta pd_meta;
86 struct g_raid_md_ddf_pervolume {
87 struct ddf_vol_meta pv_meta;
89 struct callout pv_start_co; /* STARTING state timer. */
92 struct g_raid_md_ddf_object {
93 struct g_raid_md_object mdio_base;
95 struct ddf_meta mdio_meta;
97 struct callout mdio_start_co; /* STARTING state timer. */
99 struct root_hold_token *mdio_rootmount; /* Root mount delay token. */
102 static g_raid_md_create_req_t g_raid_md_create_req_ddf;
103 static g_raid_md_taste_t g_raid_md_taste_ddf;
104 static g_raid_md_event_t g_raid_md_event_ddf;
105 static g_raid_md_volume_event_t g_raid_md_volume_event_ddf;
106 static g_raid_md_ctl_t g_raid_md_ctl_ddf;
107 static g_raid_md_write_t g_raid_md_write_ddf;
108 static g_raid_md_fail_disk_t g_raid_md_fail_disk_ddf;
109 static g_raid_md_free_disk_t g_raid_md_free_disk_ddf;
110 static g_raid_md_free_volume_t g_raid_md_free_volume_ddf;
111 static g_raid_md_free_t g_raid_md_free_ddf;
113 static kobj_method_t g_raid_md_ddf_methods[] = {
114 KOBJMETHOD(g_raid_md_create_req, g_raid_md_create_req_ddf),
115 KOBJMETHOD(g_raid_md_taste, g_raid_md_taste_ddf),
116 KOBJMETHOD(g_raid_md_event, g_raid_md_event_ddf),
117 KOBJMETHOD(g_raid_md_volume_event, g_raid_md_volume_event_ddf),
118 KOBJMETHOD(g_raid_md_ctl, g_raid_md_ctl_ddf),
119 KOBJMETHOD(g_raid_md_write, g_raid_md_write_ddf),
120 KOBJMETHOD(g_raid_md_fail_disk, g_raid_md_fail_disk_ddf),
121 KOBJMETHOD(g_raid_md_free_disk, g_raid_md_free_disk_ddf),
122 KOBJMETHOD(g_raid_md_free_volume, g_raid_md_free_volume_ddf),
123 KOBJMETHOD(g_raid_md_free, g_raid_md_free_ddf),
127 static struct g_raid_md_class g_raid_md_ddf_class = {
129 g_raid_md_ddf_methods,
130 sizeof(struct g_raid_md_ddf_object),
135 #define GET8(m, f) ((m)->f)
136 #define GET16(m, f) ((m)->bigendian ? be16dec(&(m)->f) : le16dec(&(m)->f))
137 #define GET32(m, f) ((m)->bigendian ? be32dec(&(m)->f) : le32dec(&(m)->f))
138 #define GET64(m, f) ((m)->bigendian ? be64dec(&(m)->f) : le64dec(&(m)->f))
139 #define GET8D(m, f) (f)
140 #define GET16D(m, f) ((m)->bigendian ? be16dec(&f) : le16dec(&f))
141 #define GET32D(m, f) ((m)->bigendian ? be32dec(&f) : le32dec(&f))
142 #define GET64D(m, f) ((m)->bigendian ? be64dec(&f) : le64dec(&f))
143 #define GET8P(m, f) (*(f))
144 #define GET16P(m, f) ((m)->bigendian ? be16dec(f) : le16dec(f))
145 #define GET32P(m, f) ((m)->bigendian ? be32dec(f) : le32dec(f))
146 #define GET64P(m, f) ((m)->bigendian ? be64dec(f) : le64dec(f))
148 #define SET8P(m, f, v) \
150 #define SET16P(m, f, v) \
152 if ((m)->bigendian) \
157 #define SET32P(m, f, v) \
159 if ((m)->bigendian) \
164 #define SET64P(m, f, v) \
166 if ((m)->bigendian) \
171 #define SET8(m, f, v) SET8P((m), &((m)->f), (v))
172 #define SET16(m, f, v) SET16P((m), &((m)->f), (v))
173 #define SET32(m, f, v) SET32P((m), &((m)->f), (v))
174 #define SET64(m, f, v) SET64P((m), &((m)->f), (v))
175 #define SET8D(m, f, v) SET8P((m), &(f), (v))
176 #define SET16D(m, f, v) SET16P((m), &(f), (v))
177 #define SET32D(m, f, v) SET32P((m), &(f), (v))
178 #define SET64D(m, f, v) SET64P((m), &(f), (v))
180 #define GETCRNUM(m) (GET32((m), hdr->cr_length) / \
181 GET16((m), hdr->Configuration_Record_Length))
183 #define GETVDCPTR(m, n) ((struct ddf_vdc_record *)((uint8_t *)(m)->cr + \
184 (n) * GET16((m), hdr->Configuration_Record_Length) * \
187 #define GETSAPTR(m, n) ((struct ddf_sa_record *)((uint8_t *)(m)->cr + \
188 (n) * GET16((m), hdr->Configuration_Record_Length) * \
192 isff(uint8_t *buf, int size)
196 for (i = 0; i < size; i++)
203 print_guid(uint8_t *buf)
208 for (i = 0; i < 24; i++) {
209 if (buf[i] != 0 && (buf[i] < ' ' || buf[i] > 127)) {
215 printf("'%.24s'", buf);
217 for (i = 0; i < 24; i++)
218 printf("%02x", buf[i]);
223 g_raid_md_ddf_print(struct ddf_meta *meta)
225 struct ddf_vdc_record *vdc;
226 struct ddf_vuc_record *vuc;
227 struct ddf_sa_record *sa;
230 int i, j, k, num, num2;
232 if (g_raid_debug < 1)
235 printf("********* DDF Metadata *********\n");
236 printf("**** Header ****\n");
237 printf("DDF_Header_GUID ");
238 print_guid(meta->hdr->DDF_Header_GUID);
240 printf("DDF_rev %8.8s\n", (char *)&meta->hdr->DDF_rev[0]);
241 printf("Sequence_Number 0x%08x\n", GET32(meta, hdr->Sequence_Number));
242 printf("TimeStamp 0x%08x\n", GET32(meta, hdr->TimeStamp));
243 printf("Open_Flag 0x%02x\n", GET16(meta, hdr->Open_Flag));
244 printf("Foreign_Flag 0x%02x\n", GET16(meta, hdr->Foreign_Flag));
245 printf("Diskgrouping 0x%02x\n", GET16(meta, hdr->Diskgrouping));
246 printf("Primary_Header_LBA %ju\n", GET64(meta, hdr->Primary_Header_LBA));
247 printf("Secondary_Header_LBA %ju\n", GET64(meta, hdr->Secondary_Header_LBA));
248 printf("WorkSpace_Length %u\n", GET32(meta, hdr->WorkSpace_Length));
249 printf("WorkSpace_LBA %ju\n", GET64(meta, hdr->WorkSpace_LBA));
250 printf("Max_PD_Entries %u\n", GET16(meta, hdr->Max_PD_Entries));
251 printf("Max_VD_Entries %u\n", GET16(meta, hdr->Max_VD_Entries));
252 printf("Max_Partitions %u\n", GET16(meta, hdr->Max_Partitions));
253 printf("Configuration_Record_Length %u\n", GET16(meta, hdr->Configuration_Record_Length));
254 printf("Max_Primary_Element_Entries %u\n", GET16(meta, hdr->Max_Primary_Element_Entries));
255 printf("Controller Data %u:%u\n", GET32(meta, hdr->cd_section), GET32(meta, hdr->cd_length));
256 printf("Physical Disk %u:%u\n", GET32(meta, hdr->pdr_section), GET32(meta, hdr->pdr_length));
257 printf("Virtual Disk %u:%u\n", GET32(meta, hdr->vdr_section), GET32(meta, hdr->vdr_length));
258 printf("Configuration Recs %u:%u\n", GET32(meta, hdr->cr_section), GET32(meta, hdr->cr_length));
259 printf("Physical Disk Recs %u:%u\n", GET32(meta, hdr->pdd_section), GET32(meta, hdr->pdd_length));
260 printf("BBM Log %u:%u\n", GET32(meta, hdr->bbmlog_section), GET32(meta, hdr->bbmlog_length));
261 printf("Diagnostic Space %u:%u\n", GET32(meta, hdr->Diagnostic_Space), GET32(meta, hdr->Diagnostic_Space_Length));
262 printf("Vendor_Specific_Logs %u:%u\n", GET32(meta, hdr->Vendor_Specific_Logs), GET32(meta, hdr->Vendor_Specific_Logs_Length));
263 printf("**** Controller Data ****\n");
264 printf("Controller_GUID ");
265 print_guid(meta->cdr->Controller_GUID);
267 printf("Controller_Type 0x%04x%04x 0x%04x%04x\n",
268 GET16(meta, cdr->Controller_Type.Vendor_ID),
269 GET16(meta, cdr->Controller_Type.Device_ID),
270 GET16(meta, cdr->Controller_Type.SubVendor_ID),
271 GET16(meta, cdr->Controller_Type.SubDevice_ID));
272 printf("Product_ID '%.16s'\n", (char *)&meta->cdr->Product_ID[0]);
273 printf("**** Physical Disk Records ****\n");
274 printf("Populated_PDEs %u\n", GET16(meta, pdr->Populated_PDEs));
275 printf("Max_PDE_Supported %u\n", GET16(meta, pdr->Max_PDE_Supported));
276 for (j = 0; j < GET16(meta, pdr->Populated_PDEs); j++) {
277 if (isff(meta->pdr->entry[j].PD_GUID, 24))
279 if (GET32(meta, pdr->entry[j].PD_Reference) == 0xffffffff)
282 print_guid(meta->pdr->entry[j].PD_GUID);
284 printf("PD_Reference 0x%08x\n",
285 GET32(meta, pdr->entry[j].PD_Reference));
286 printf("PD_Type 0x%04x\n",
287 GET16(meta, pdr->entry[j].PD_Type));
288 printf("PD_State 0x%04x\n",
289 GET16(meta, pdr->entry[j].PD_State));
290 printf("Configured_Size %ju\n",
291 GET64(meta, pdr->entry[j].Configured_Size));
292 printf("Block_Size %u\n",
293 GET16(meta, pdr->entry[j].Block_Size));
295 printf("**** Virtual Disk Records ****\n");
296 printf("Populated_VDEs %u\n", GET16(meta, vdr->Populated_VDEs));
297 printf("Max_VDE_Supported %u\n", GET16(meta, vdr->Max_VDE_Supported));
298 for (j = 0; j < GET16(meta, vdr->Populated_VDEs); j++) {
299 if (isff(meta->vdr->entry[j].VD_GUID, 24))
302 print_guid(meta->vdr->entry[j].VD_GUID);
304 printf("VD_Number 0x%04x\n",
305 GET16(meta, vdr->entry[j].VD_Number));
306 printf("VD_Type 0x%04x\n",
307 GET16(meta, vdr->entry[j].VD_Type));
308 printf("VD_State 0x%02x\n",
309 GET8(meta, vdr->entry[j].VD_State));
310 printf("Init_State 0x%02x\n",
311 GET8(meta, vdr->entry[j].Init_State));
312 printf("Drive_Failures_Remaining %u\n",
313 GET8(meta, vdr->entry[j].Drive_Failures_Remaining));
314 printf("VD_Name '%.16s'\n",
315 (char *)&meta->vdr->entry[j].VD_Name);
317 printf("**** Configuration Records ****\n");
318 num = GETCRNUM(meta);
319 for (j = 0; j < num; j++) {
320 vdc = GETVDCPTR(meta, j);
321 val = GET32D(meta, vdc->Signature);
323 case DDF_VDCR_SIGNATURE:
324 printf("** Virtual Disk Configuration **\n");
326 print_guid(vdc->VD_GUID);
328 printf("Timestamp 0x%08x\n",
329 GET32D(meta, vdc->Timestamp));
330 printf("Sequence_Number 0x%08x\n",
331 GET32D(meta, vdc->Sequence_Number));
332 printf("Primary_Element_Count %u\n",
333 GET16D(meta, vdc->Primary_Element_Count));
334 printf("Stripe_Size %u\n",
335 GET8D(meta, vdc->Stripe_Size));
336 printf("Primary_RAID_Level 0x%02x\n",
337 GET8D(meta, vdc->Primary_RAID_Level));
338 printf("RLQ 0x%02x\n",
339 GET8D(meta, vdc->RLQ));
340 printf("Secondary_Element_Count %u\n",
341 GET8D(meta, vdc->Secondary_Element_Count));
342 printf("Secondary_Element_Seq %u\n",
343 GET8D(meta, vdc->Secondary_Element_Seq));
344 printf("Secondary_RAID_Level 0x%02x\n",
345 GET8D(meta, vdc->Secondary_RAID_Level));
346 printf("Block_Count %ju\n",
347 GET64D(meta, vdc->Block_Count));
348 printf("VD_Size %ju\n",
349 GET64D(meta, vdc->VD_Size));
350 printf("Block_Size %u\n",
351 GET16D(meta, vdc->Block_Size));
352 printf("Rotate_Parity_count %u\n",
353 GET8D(meta, vdc->Rotate_Parity_count));
354 printf("Associated_Spare_Disks");
355 for (i = 0; i < 8; i++) {
356 if (GET32D(meta, vdc->Associated_Spares[i]) != 0xffffffff)
357 printf(" 0x%08x", GET32D(meta, vdc->Associated_Spares[i]));
360 printf("Cache_Flags %016jx\n",
361 GET64D(meta, vdc->Cache_Flags));
362 printf("BG_Rate %u\n",
363 GET8D(meta, vdc->BG_Rate));
364 printf("MDF_Parity_Disks %u\n",
365 GET8D(meta, vdc->MDF_Parity_Disks));
366 printf("MDF_Parity_Generator_Polynomial 0x%04x\n",
367 GET16D(meta, vdc->MDF_Parity_Generator_Polynomial));
368 printf("MDF_Constant_Generation_Method 0x%02x\n",
369 GET8D(meta, vdc->MDF_Constant_Generation_Method));
370 printf("Physical_Disks ");
371 num2 = GET16D(meta, vdc->Primary_Element_Count);
372 val2 = (uint64_t *)&(vdc->Physical_Disk_Sequence[GET16(meta, hdr->Max_Primary_Element_Entries)]);
373 for (i = 0; i < num2; i++)
374 printf(" 0x%08x @ %ju",
375 GET32D(meta, vdc->Physical_Disk_Sequence[i]),
376 GET64P(meta, val2 + i));
379 case DDF_VUCR_SIGNATURE:
380 printf("** Vendor Unique Configuration **\n");
381 vuc = (struct ddf_vuc_record *)vdc;
383 print_guid(vuc->VD_GUID);
386 case DDF_SA_SIGNATURE:
387 printf("** Spare Assignment Configuration **\n");
388 sa = (struct ddf_sa_record *)vdc;
389 printf("Timestamp 0x%08x\n",
390 GET32D(meta, sa->Timestamp));
391 printf("Spare_Type 0x%02x\n",
392 GET8D(meta, sa->Spare_Type));
393 printf("Populated_SAEs %u\n",
394 GET16D(meta, sa->Populated_SAEs));
395 printf("MAX_SAE_Supported %u\n",
396 GET16D(meta, sa->MAX_SAE_Supported));
397 for (i = 0; i < GET16D(meta, sa->Populated_SAEs); i++) {
398 if (isff(sa->entry[i].VD_GUID, 24))
401 for (k = 0; k < 24; k++)
402 printf("%02x", sa->entry[i].VD_GUID[k]);
404 printf("Secondary_Element %u\n",
405 GET16D(meta, sa->entry[i].Secondary_Element));
412 printf("Unknown configuration signature %08x\n", val);
416 printf("**** Physical Disk Data ****\n");
418 print_guid(meta->pdd->PD_GUID);
420 printf("PD_Reference 0x%08x\n",
421 GET32(meta, pdd->PD_Reference));
422 printf("Forced_Ref_Flag 0x%02x\n",
423 GET8(meta, pdd->Forced_Ref_Flag));
424 printf("Forced_PD_GUID_Flag 0x%02x\n",
425 GET8(meta, pdd->Forced_PD_GUID_Flag));
429 ddf_meta_find_pd(struct ddf_meta *meta, uint8_t *GUID, uint32_t PD_Reference)
433 for (i = 0; i < GET16(meta, pdr->Populated_PDEs); i++) {
435 if (memcmp(meta->pdr->entry[i].PD_GUID, GUID, 24) == 0)
437 } else if (PD_Reference != 0xffffffff) {
438 if (GET32(meta, pdr->entry[i].PD_Reference) == PD_Reference)
441 if (isff(meta->pdr->entry[i].PD_GUID, 24))
444 if (GUID == NULL && PD_Reference == 0xffffffff) {
445 if (i >= GET16(meta, pdr->Max_PDE_Supported))
447 SET16(meta, pdr->Populated_PDEs, i + 1);
454 ddf_meta_find_vd(struct ddf_meta *meta, uint8_t *GUID)
458 for (i = 0; i < GET16(meta, vdr->Populated_VDEs); i++) {
460 if (memcmp(meta->vdr->entry[i].VD_GUID, GUID, 24) == 0)
463 if (isff(meta->vdr->entry[i].VD_GUID, 24))
467 if (i >= GET16(meta, vdr->Max_VDE_Supported))
469 SET16(meta, vdr->Populated_VDEs, i + 1);
475 static struct ddf_vdc_record *
476 ddf_meta_find_vdc(struct ddf_meta *meta, uint8_t *GUID)
478 struct ddf_vdc_record *vdc;
481 num = GETCRNUM(meta);
482 for (i = 0; i < num; i++) {
483 vdc = GETVDCPTR(meta, i);
485 if (GET32D(meta, vdc->Signature) == DDF_VDCR_SIGNATURE &&
486 memcmp(vdc->VD_GUID, GUID, 24) == 0)
489 if (GET32D(meta, vdc->Signature) == 0xffffffff ||
490 GET32D(meta, vdc->Signature) == 0)
497 ddf_meta_count_vdc(struct ddf_meta *meta, uint8_t *GUID)
499 struct ddf_vdc_record *vdc;
503 num = GETCRNUM(meta);
504 for (i = 0; i < num; i++) {
505 vdc = GETVDCPTR(meta, i);
506 if (GET32D(meta, vdc->Signature) != DDF_VDCR_SIGNATURE)
508 if (GUID == NULL || memcmp(vdc->VD_GUID, GUID, 24) == 0)
515 ddf_meta_find_disk(struct ddf_vol_meta *vmeta, uint32_t PD_Reference,
516 int *bvdp, int *posp)
521 for (bvd = 0; bvd < GET8(vmeta, vdc->Secondary_Element_Count); bvd++) {
522 if (vmeta->bvdc[bvd] == NULL) {
523 i += GET16(vmeta, vdc->Primary_Element_Count); // XXX
526 for (pos = 0; pos < GET16(vmeta, bvdc[bvd]->Primary_Element_Count);
528 if (GET32(vmeta, bvdc[bvd]->Physical_Disk_Sequence[pos]) ==
541 static struct ddf_sa_record *
542 ddf_meta_find_sa(struct ddf_meta *meta, int create)
544 struct ddf_sa_record *sa;
547 num = GETCRNUM(meta);
548 for (i = 0; i < num; i++) {
549 sa = GETSAPTR(meta, i);
550 if (GET32D(meta, sa->Signature) == DDF_SA_SIGNATURE)
554 for (i = 0; i < num; i++) {
555 sa = GETSAPTR(meta, i);
556 if (GET32D(meta, sa->Signature) == 0xffffffff ||
557 GET32D(meta, sa->Signature) == 0)
565 ddf_meta_create(struct g_raid_disk *disk, struct ddf_meta *sample)
569 struct g_raid_md_ddf_perdisk *pd;
570 struct g_raid_md_ddf_object *mdi;
571 struct ddf_meta *meta;
572 struct ddf_pd_entry *pde;
576 char serial_buffer[DISK_IDENT_SIZE];
578 if (sample->hdr == NULL)
581 mdi = (struct g_raid_md_ddf_object *)disk->d_softc->sc_md;
582 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
584 ss = disk->d_consumer->provider->sectorsize;
585 anchorlba = disk->d_consumer->provider->mediasize / ss - 1;
587 meta->sectorsize = ss;
588 meta->bigendian = sample ? sample->bigendian : mdi->mdio_bigendian;
590 clock_ts_to_ct(&ts, &ct);
593 meta->hdr = malloc(ss, M_MD_DDF, M_WAITOK);
594 memset(meta->hdr, 0xff, ss);
596 memcpy(meta->hdr, sample->hdr, sizeof(struct ddf_header));
597 if (ss != sample->sectorsize) {
598 SET32(meta, hdr->WorkSpace_Length,
599 howmany(GET32(sample, hdr->WorkSpace_Length) *
600 sample->sectorsize, ss));
601 SET16(meta, hdr->Configuration_Record_Length,
602 howmany(GET16(sample,
603 hdr->Configuration_Record_Length) *
604 sample->sectorsize, ss));
605 SET32(meta, hdr->cd_length,
606 howmany(GET32(sample, hdr->cd_length) *
607 sample->sectorsize, ss));
608 SET32(meta, hdr->pdr_length,
609 howmany(GET32(sample, hdr->pdr_length) *
610 sample->sectorsize, ss));
611 SET32(meta, hdr->vdr_length,
612 howmany(GET32(sample, hdr->vdr_length) *
613 sample->sectorsize, ss));
614 SET32(meta, hdr->cr_length,
615 howmany(GET32(sample, hdr->cr_length) *
616 sample->sectorsize, ss));
617 SET32(meta, hdr->pdd_length,
618 howmany(GET32(sample, hdr->pdd_length) *
619 sample->sectorsize, ss));
620 SET32(meta, hdr->bbmlog_length,
621 howmany(GET32(sample, hdr->bbmlog_length) *
622 sample->sectorsize, ss));
623 SET32(meta, hdr->Diagnostic_Space,
624 howmany(GET32(sample, hdr->bbmlog_length) *
625 sample->sectorsize, ss));
626 SET32(meta, hdr->Vendor_Specific_Logs,
627 howmany(GET32(sample, hdr->bbmlog_length) *
628 sample->sectorsize, ss));
631 SET32(meta, hdr->Signature, DDF_HEADER_SIGNATURE);
632 snprintf(meta->hdr->DDF_Header_GUID, 25, "FreeBSD %08x%08x",
633 (u_int)(ts.tv_sec - DECADE), arc4random());
634 memcpy(meta->hdr->DDF_rev, "02.00.00", 8);
635 SET32(meta, hdr->TimeStamp, (ts.tv_sec - DECADE));
636 SET32(meta, hdr->WorkSpace_Length, 16 * 1024 * 1024 / ss);
637 SET16(meta, hdr->Max_PD_Entries, DDF_MAX_DISKS - 1);
638 SET16(meta, hdr->Max_VD_Entries, DDF_MAX_VDISKS);
639 SET16(meta, hdr->Max_Partitions, DDF_MAX_PARTITIONS);
640 SET16(meta, hdr->Max_Primary_Element_Entries, DDF_MAX_DISKS);
641 SET16(meta, hdr->Configuration_Record_Length,
642 howmany(sizeof(struct ddf_vdc_record) + (4 + 8) *
643 GET16(meta, hdr->Max_Primary_Element_Entries), ss));
644 SET32(meta, hdr->cd_length,
645 howmany(sizeof(struct ddf_cd_record), ss));
646 SET32(meta, hdr->pdr_length,
647 howmany(sizeof(struct ddf_pd_record) +
648 sizeof(struct ddf_pd_entry) * GET16(meta,
649 hdr->Max_PD_Entries), ss));
650 SET32(meta, hdr->vdr_length,
651 howmany(sizeof(struct ddf_vd_record) +
652 sizeof(struct ddf_vd_entry) *
653 GET16(meta, hdr->Max_VD_Entries), ss));
654 SET32(meta, hdr->cr_length,
655 GET16(meta, hdr->Configuration_Record_Length) *
656 (GET16(meta, hdr->Max_Partitions) + 1));
657 SET32(meta, hdr->pdd_length,
658 howmany(sizeof(struct ddf_pdd_record), ss));
659 SET32(meta, hdr->bbmlog_length, 0);
660 SET32(meta, hdr->Diagnostic_Space_Length, 0);
661 SET32(meta, hdr->Vendor_Specific_Logs_Length, 0);
664 SET32(meta, hdr->cd_section, pos);
665 pos += GET32(meta, hdr->cd_length);
666 SET32(meta, hdr->pdr_section, pos);
667 pos += GET32(meta, hdr->pdr_length);
668 SET32(meta, hdr->vdr_section, pos);
669 pos += GET32(meta, hdr->vdr_length);
670 SET32(meta, hdr->cr_section, pos);
671 pos += GET32(meta, hdr->cr_length);
672 SET32(meta, hdr->pdd_section, pos);
673 pos += GET32(meta, hdr->pdd_length);
674 SET32(meta, hdr->bbmlog_section,
675 GET32(meta, hdr->bbmlog_length) != 0 ? pos : 0xffffffff);
676 pos += GET32(meta, hdr->bbmlog_length);
677 SET32(meta, hdr->Diagnostic_Space,
678 GET32(meta, hdr->Diagnostic_Space_Length) != 0 ? pos : 0xffffffff);
679 pos += GET32(meta, hdr->Diagnostic_Space_Length);
680 SET32(meta, hdr->Vendor_Specific_Logs,
681 GET32(meta, hdr->Vendor_Specific_Logs_Length) != 0 ? pos : 0xffffffff);
682 pos += min(GET32(meta, hdr->Vendor_Specific_Logs_Length), 1);
683 SET64(meta, hdr->Primary_Header_LBA,
685 SET64(meta, hdr->Secondary_Header_LBA,
686 0xffffffffffffffffULL);
687 SET64(meta, hdr->WorkSpace_LBA,
688 anchorlba + 1 - 32 * 1024 * 1024 / ss);
690 /* Controller Data */
691 size = GET32(meta, hdr->cd_length) * ss;
692 meta->cdr = malloc(size, M_MD_DDF, M_WAITOK);
693 memset(meta->cdr, 0xff, size);
694 SET32(meta, cdr->Signature, DDF_CONTROLLER_DATA_SIGNATURE);
695 memcpy(meta->cdr->Controller_GUID, "FreeBSD GEOM RAID SERIAL", 24);
696 memcpy(meta->cdr->Product_ID, "FreeBSD GEOMRAID", 16);
698 /* Physical Drive Records. */
699 size = GET32(meta, hdr->pdr_length) * ss;
700 meta->pdr = malloc(size, M_MD_DDF, M_WAITOK);
701 memset(meta->pdr, 0xff, size);
702 SET32(meta, pdr->Signature, DDF_PDR_SIGNATURE);
703 SET16(meta, pdr->Populated_PDEs, 1);
704 SET16(meta, pdr->Max_PDE_Supported,
705 GET16(meta, hdr->Max_PD_Entries));
707 pde = &meta->pdr->entry[0];
708 len = sizeof(serial_buffer);
709 error = g_io_getattr("GEOM::ident", disk->d_consumer, &len, serial_buffer);
710 if (error == 0 && (len = strlen (serial_buffer)) >= 6 && len <= 20)
711 snprintf(pde->PD_GUID, 25, "DISK%20s", serial_buffer);
713 snprintf(pde->PD_GUID, 25, "DISK%04d%02d%02d%08x%04x",
714 ct.year, ct.mon, ct.day,
715 arc4random(), arc4random() & 0xffff);
716 SET32D(meta, pde->PD_Reference, arc4random());
717 SET16D(meta, pde->PD_Type, DDF_PDE_GUID_FORCE);
718 SET16D(meta, pde->PD_State, 0);
719 SET64D(meta, pde->Configured_Size,
720 anchorlba + 1 - 32 * 1024 * 1024 / ss);
721 SET16D(meta, pde->Block_Size, ss);
723 /* Virtual Drive Records. */
724 size = GET32(meta, hdr->vdr_length) * ss;
725 meta->vdr = malloc(size, M_MD_DDF, M_WAITOK);
726 memset(meta->vdr, 0xff, size);
727 SET32(meta, vdr->Signature, DDF_VD_RECORD_SIGNATURE);
728 SET32(meta, vdr->Populated_VDEs, 0);
729 SET16(meta, vdr->Max_VDE_Supported,
730 GET16(meta, hdr->Max_VD_Entries));
732 /* Configuration Records. */
733 size = GET32(meta, hdr->cr_length) * ss;
734 meta->cr = malloc(size, M_MD_DDF, M_WAITOK);
735 memset(meta->cr, 0xff, size);
737 /* Physical Disk Data. */
738 size = GET32(meta, hdr->pdd_length) * ss;
739 meta->pdd = malloc(size, M_MD_DDF, M_WAITOK);
740 memset(meta->pdd, 0xff, size);
741 SET32(meta, pdd->Signature, DDF_PDD_SIGNATURE);
742 memcpy(meta->pdd->PD_GUID, pde->PD_GUID, 24);
743 SET32(meta, pdd->PD_Reference, GET32D(meta, pde->PD_Reference));
744 SET8(meta, pdd->Forced_Ref_Flag, DDF_PDD_FORCED_REF);
745 SET8(meta, pdd->Forced_PD_GUID_Flag, DDF_PDD_FORCED_GUID);
747 /* Bad Block Management Log. */
748 if (GET32(meta, hdr->bbmlog_length) != 0) {
749 size = GET32(meta, hdr->bbmlog_length) * ss;
750 meta->bbm = malloc(size, M_MD_DDF, M_WAITOK);
751 memset(meta->bbm, 0xff, size);
752 SET32(meta, bbm->Signature, DDF_BBML_SIGNATURE);
753 SET32(meta, bbm->Entry_Count, 0);
754 SET32(meta, bbm->Spare_Block_Count, 0);
759 ddf_meta_copy(struct ddf_meta *dst, struct ddf_meta *src)
763 dst->bigendian = src->bigendian;
764 ss = dst->sectorsize = src->sectorsize;
765 dst->hdr = malloc(ss, M_MD_DDF, M_WAITOK);
766 memcpy(dst->hdr, src->hdr, ss);
767 dst->cdr = malloc(GET32(src, hdr->cd_length) * ss, M_MD_DDF, M_WAITOK);
768 memcpy(dst->cdr, src->cdr, GET32(src, hdr->cd_length) * ss);
769 dst->pdr = malloc(GET32(src, hdr->pdr_length) * ss, M_MD_DDF, M_WAITOK);
770 memcpy(dst->pdr, src->pdr, GET32(src, hdr->pdr_length) * ss);
771 dst->vdr = malloc(GET32(src, hdr->vdr_length) * ss, M_MD_DDF, M_WAITOK);
772 memcpy(dst->vdr, src->vdr, GET32(src, hdr->vdr_length) * ss);
773 dst->cr = malloc(GET32(src, hdr->cr_length) * ss, M_MD_DDF, M_WAITOK);
774 memcpy(dst->cr, src->cr, GET32(src, hdr->cr_length) * ss);
775 dst->pdd = malloc(GET32(src, hdr->pdd_length) * ss, M_MD_DDF, M_WAITOK);
776 memcpy(dst->pdd, src->pdd, GET32(src, hdr->pdd_length) * ss);
777 if (src->bbm != NULL) {
778 dst->bbm = malloc(GET32(src, hdr->bbmlog_length) * ss, M_MD_DDF, M_WAITOK);
779 memcpy(dst->bbm, src->bbm, GET32(src, hdr->bbmlog_length) * ss);
784 ddf_meta_update(struct ddf_meta *meta, struct ddf_meta *src)
786 struct ddf_pd_entry *pde, *spde;
789 for (i = 0; i < GET16(src, pdr->Populated_PDEs); i++) {
790 spde = &src->pdr->entry[i];
791 if (isff(spde->PD_GUID, 24))
793 j = ddf_meta_find_pd(meta, NULL,
794 GET32(src, pdr->entry[i].PD_Reference));
796 j = ddf_meta_find_pd(meta, NULL, 0xffffffff);
797 pde = &meta->pdr->entry[j];
798 memcpy(pde, spde, sizeof(*pde));
800 pde = &meta->pdr->entry[j];
801 SET16D(meta, pde->PD_State,
802 GET16D(meta, pde->PD_State) |
803 GET16D(src, pde->PD_State));
809 ddf_meta_free(struct ddf_meta *meta)
812 if (meta->hdr != NULL) {
813 free(meta->hdr, M_MD_DDF);
816 if (meta->cdr != NULL) {
817 free(meta->cdr, M_MD_DDF);
820 if (meta->pdr != NULL) {
821 free(meta->pdr, M_MD_DDF);
824 if (meta->vdr != NULL) {
825 free(meta->vdr, M_MD_DDF);
828 if (meta->cr != NULL) {
829 free(meta->cr, M_MD_DDF);
832 if (meta->pdd != NULL) {
833 free(meta->pdd, M_MD_DDF);
836 if (meta->bbm != NULL) {
837 free(meta->bbm, M_MD_DDF);
843 ddf_vol_meta_create(struct ddf_vol_meta *meta, struct ddf_meta *sample)
849 meta->bigendian = sample->bigendian;
850 ss = meta->sectorsize = sample->sectorsize;
851 meta->hdr = malloc(ss, M_MD_DDF, M_WAITOK);
852 memcpy(meta->hdr, sample->hdr, ss);
853 meta->cdr = malloc(GET32(sample, hdr->cd_length) * ss, M_MD_DDF, M_WAITOK);
854 memcpy(meta->cdr, sample->cdr, GET32(sample, hdr->cd_length) * ss);
855 meta->vde = malloc(sizeof(struct ddf_vd_entry), M_MD_DDF, M_WAITOK);
856 memset(meta->vde, 0xff, sizeof(struct ddf_vd_entry));
858 clock_ts_to_ct(&ts, &ct);
859 snprintf(meta->vde->VD_GUID, 25, "FreeBSD%04d%02d%02d%08x%01x",
860 ct.year, ct.mon, ct.day,
861 arc4random(), arc4random() & 0xf);
862 size = GET16(sample, hdr->Configuration_Record_Length) * ss;
863 meta->vdc = malloc(size, M_MD_DDF, M_WAITOK);
864 memset(meta->vdc, 0xff, size);
865 SET32(meta, vdc->Signature, DDF_VDCR_SIGNATURE);
866 memcpy(meta->vdc->VD_GUID, meta->vde->VD_GUID, 24);
867 SET32(meta, vdc->Sequence_Number, 0);
871 ddf_vol_meta_update(struct ddf_vol_meta *dst, struct ddf_meta *src,
872 uint8_t *GUID, int started)
874 struct ddf_vd_entry *vde;
875 struct ddf_vdc_record *vdc;
876 int vnew, bvnew, bvd, size;
879 vde = &src->vdr->entry[ddf_meta_find_vd(src, GUID)];
880 vdc = ddf_meta_find_vdc(src, GUID);
881 if (GET8D(src, vdc->Secondary_Element_Count) == 1)
884 bvd = GET8D(src, vdc->Secondary_Element_Seq);
885 size = GET16(src, hdr->Configuration_Record_Length) * src->sectorsize;
887 if (dst->vdc == NULL ||
888 (!started && ((int32_t)(GET32D(src, vdc->Sequence_Number) -
889 GET32(dst, vdc->Sequence_Number))) > 0))
894 if (dst->bvdc[bvd] == NULL ||
895 (!started && ((int32_t)(GET32D(src, vdc->Sequence_Number) -
896 GET32(dst, bvdc[bvd]->Sequence_Number))) > 0))
902 dst->bigendian = src->bigendian;
903 ss = dst->sectorsize = src->sectorsize;
904 if (dst->hdr != NULL)
905 free(dst->hdr, M_MD_DDF);
906 dst->hdr = malloc(ss, M_MD_DDF, M_WAITOK);
907 memcpy(dst->hdr, src->hdr, ss);
908 if (dst->cdr != NULL)
909 free(dst->cdr, M_MD_DDF);
910 dst->cdr = malloc(GET32(src, hdr->cd_length) * ss, M_MD_DDF, M_WAITOK);
911 memcpy(dst->cdr, src->cdr, GET32(src, hdr->cd_length) * ss);
912 if (dst->vde != NULL)
913 free(dst->vde, M_MD_DDF);
914 dst->vde = malloc(sizeof(struct ddf_vd_entry), M_MD_DDF, M_WAITOK);
915 memcpy(dst->vde, vde, sizeof(struct ddf_vd_entry));
916 if (dst->vdc != NULL)
917 free(dst->vdc, M_MD_DDF);
918 dst->vdc = malloc(size, M_MD_DDF, M_WAITOK);
919 memcpy(dst->vdc, vdc, size);
922 if (dst->bvdc[bvd] != NULL)
923 free(dst->bvdc[bvd], M_MD_DDF);
924 dst->bvdc[bvd] = malloc(size, M_MD_DDF, M_WAITOK);
925 memcpy(dst->bvdc[bvd], vdc, size);
930 ddf_vol_meta_free(struct ddf_vol_meta *meta)
934 if (meta->hdr != NULL) {
935 free(meta->hdr, M_MD_DDF);
938 if (meta->cdr != NULL) {
939 free(meta->cdr, M_MD_DDF);
942 if (meta->vde != NULL) {
943 free(meta->vde, M_MD_DDF);
946 if (meta->vdc != NULL) {
947 free(meta->vdc, M_MD_DDF);
950 for (i = 0; i < DDF_MAX_DISKS_HARD; i++) {
951 if (meta->bvdc[i] != NULL) {
952 free(meta->bvdc[i], M_MD_DDF);
953 meta->bvdc[i] = NULL;
959 ddf_meta_unused_range(struct ddf_meta *meta, off_t *off, off_t *size)
961 struct ddf_vdc_record *vdc;
962 off_t beg[32], end[32], beg1, end1;
964 int i, j, n, num, pos;
969 ref = GET32(meta, pdd->PD_Reference);
970 pos = ddf_meta_find_pd(meta, NULL, ref);
972 end[0] = GET64(meta, pdr->entry[pos].Configured_Size);
974 num = GETCRNUM(meta);
975 for (i = 0; i < num; i++) {
976 vdc = GETVDCPTR(meta, i);
977 if (GET32D(meta, vdc->Signature) != DDF_VDCR_SIGNATURE)
979 for (pos = 0; pos < GET16D(meta, vdc->Primary_Element_Count); pos++)
980 if (GET32D(meta, vdc->Physical_Disk_Sequence[pos]) == ref)
982 if (pos == GET16D(meta, vdc->Primary_Element_Count))
984 offp = (uint64_t *)&(vdc->Physical_Disk_Sequence[
985 GET16(meta, hdr->Max_Primary_Element_Entries)]);
986 beg1 = GET64P(meta, offp + pos);
987 end1 = beg1 + GET64D(meta, vdc->Block_Count);
988 for (j = 0; j < n; j++) {
989 if (beg[j] >= end1 || end[j] <= beg1 )
991 if (beg[j] < beg1 && end[j] > end1) {
996 } else if (beg[j] < beg1)
1002 for (j = 0; j < n; j++) {
1003 if (end[j] - beg[j] > *size) {
1005 *size = end[j] - beg[j];
1008 return ((*size > 0) ? 1 : 0);
1012 ddf_meta_get_name(struct ddf_meta *meta, int num, char *buf)
1017 b = meta->vdr->entry[num].VD_Name;
1018 for (i = 15; i >= 0; i--)
1021 memcpy(buf, b, i + 1);
1026 ddf_meta_put_name(struct ddf_vol_meta *meta, char *buf)
1030 len = min(strlen(buf), 16);
1031 memset(meta->vde->VD_Name, 0x20, 16);
1032 memcpy(meta->vde->VD_Name, buf, len);
1036 ddf_meta_read(struct g_consumer *cp, struct ddf_meta *meta)
1038 struct g_provider *pp;
1039 struct ddf_header *ahdr, *hdr;
1041 off_t plba, slba, lba;
1046 ddf_meta_free(meta);
1049 ss = meta->sectorsize = pp->sectorsize;
1050 if (ss < sizeof(*hdr))
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;
1431 TAILQ_FOREACH_SAFE(vol, &sc->sc_volumes, v_next, tvol) {
1432 if (vol->v_stopping)
1434 for (i = 0; i < vol->v_disks_count; i++) {
1435 if (vol->v_subdisks[i].sd_state != G_RAID_SUBDISK_S_NONE)
1438 if (i >= vol->v_disks_count) {
1439 g_raid_destroy_volume(vol);
1447 g_raid_md_ddf_purge_disks(struct g_raid_softc *sc)
1450 struct g_raid_disk *disk, *tdisk;
1451 struct g_raid_volume *vol;
1452 struct g_raid_md_ddf_perdisk *pd;
1456 TAILQ_FOREACH_SAFE(disk, &sc->sc_disks, d_next, tdisk) {
1457 if (disk->d_state == G_RAID_DISK_S_SPARE)
1459 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
1461 /* Scan for deleted volumes. */
1462 for (i = 0; i < pd->pd_subdisks; ) {
1463 vol = g_raid_md_ddf_get_volume(sc,
1464 pd->pd_meta[i]->volume_id);
1465 if (vol != NULL && !vol->v_stopping) {
1469 free(pd->pd_meta[i], M_MD_DDF);
1470 for (j = i; j < pd->pd_subdisks - 1; j++)
1471 pd->pd_meta[j] = pd->pd_meta[j + 1];
1472 pd->pd_meta[DDF_MAX_SUBDISKS - 1] = NULL;
1477 /* If there is no metadata left - erase and delete disk. */
1478 if (pd->pd_subdisks == 0) {
1479 ddf_meta_erase(disk->d_consumer);
1480 g_raid_destroy_disk(disk);
1490 g_raid_md_ddf_supported(int level, int qual, int disks, int force)
1493 if (disks > DDF_MAX_DISKS_HARD)
1496 case G_RAID_VOLUME_RL_RAID0:
1497 if (qual != G_RAID_VOLUME_RLQ_NONE)
1501 if (!force && disks < 2)
1504 case G_RAID_VOLUME_RL_RAID1:
1507 if (qual == G_RAID_VOLUME_RLQ_R1SM) {
1508 if (!force && disks != 2)
1510 } else if (qual == G_RAID_VOLUME_RLQ_R1MM) {
1511 if (!force && disks != 3)
1516 case G_RAID_VOLUME_RL_RAID3:
1517 if (qual != G_RAID_VOLUME_RLQ_R3P0 &&
1518 qual != G_RAID_VOLUME_RLQ_R3PN)
1523 case G_RAID_VOLUME_RL_RAID4:
1524 if (qual != G_RAID_VOLUME_RLQ_R4P0 &&
1525 qual != G_RAID_VOLUME_RLQ_R4PN)
1530 case G_RAID_VOLUME_RL_RAID5:
1531 if (qual != G_RAID_VOLUME_RLQ_R5RA &&
1532 qual != G_RAID_VOLUME_RLQ_R5RS &&
1533 qual != G_RAID_VOLUME_RLQ_R5LA &&
1534 qual != G_RAID_VOLUME_RLQ_R5LS)
1539 case G_RAID_VOLUME_RL_RAID6:
1540 if (qual != G_RAID_VOLUME_RLQ_R6RA &&
1541 qual != G_RAID_VOLUME_RLQ_R6RS &&
1542 qual != G_RAID_VOLUME_RLQ_R6LA &&
1543 qual != G_RAID_VOLUME_RLQ_R6LS)
1548 case G_RAID_VOLUME_RL_RAIDMDF:
1549 if (qual != G_RAID_VOLUME_RLQ_RMDFRA &&
1550 qual != G_RAID_VOLUME_RLQ_RMDFRS &&
1551 qual != G_RAID_VOLUME_RLQ_RMDFLA &&
1552 qual != G_RAID_VOLUME_RLQ_RMDFLS)
1557 case G_RAID_VOLUME_RL_RAID1E:
1558 if (qual != G_RAID_VOLUME_RLQ_R1EA &&
1559 qual != G_RAID_VOLUME_RLQ_R1EO)
1564 case G_RAID_VOLUME_RL_SINGLE:
1565 if (qual != G_RAID_VOLUME_RLQ_NONE)
1570 case G_RAID_VOLUME_RL_CONCAT:
1571 if (qual != G_RAID_VOLUME_RLQ_NONE)
1576 case G_RAID_VOLUME_RL_RAID5E:
1577 if (qual != G_RAID_VOLUME_RLQ_R5ERA &&
1578 qual != G_RAID_VOLUME_RLQ_R5ERS &&
1579 qual != G_RAID_VOLUME_RLQ_R5ELA &&
1580 qual != G_RAID_VOLUME_RLQ_R5ELS)
1585 case G_RAID_VOLUME_RL_RAID5EE:
1586 if (qual != G_RAID_VOLUME_RLQ_R5EERA &&
1587 qual != G_RAID_VOLUME_RLQ_R5EERS &&
1588 qual != G_RAID_VOLUME_RLQ_R5EELA &&
1589 qual != G_RAID_VOLUME_RLQ_R5EELS)
1594 case G_RAID_VOLUME_RL_RAID5R:
1595 if (qual != G_RAID_VOLUME_RLQ_R5RRA &&
1596 qual != G_RAID_VOLUME_RLQ_R5RRS &&
1597 qual != G_RAID_VOLUME_RLQ_R5RLA &&
1598 qual != G_RAID_VOLUME_RLQ_R5RLS)
1610 g_raid_md_ddf_start_disk(struct g_raid_disk *disk, struct g_raid_volume *vol)
1612 struct g_raid_softc *sc;
1613 struct g_raid_subdisk *sd;
1614 struct g_raid_md_ddf_perdisk *pd;
1615 struct g_raid_md_ddf_pervolume *pv;
1616 struct g_raid_md_ddf_object *mdi;
1617 struct ddf_vol_meta *vmeta;
1618 struct ddf_meta *pdmeta, *gmeta;
1619 struct ddf_vdc_record *vdc1;
1620 struct ddf_sa_record *sa;
1621 off_t size, eoff = 0, esize = 0;
1623 int disk_pos, md_disk_bvd = -1, md_disk_pos = -1, md_pde_pos;
1624 int i, resurrection = 0;
1628 mdi = (struct g_raid_md_ddf_object *)sc->sc_md;
1629 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
1630 pdmeta = &pd->pd_meta;
1631 reference = GET32(&pd->pd_meta, pdd->PD_Reference);
1633 pv = vol->v_md_data;
1634 vmeta = &pv->pv_meta;
1635 gmeta = &mdi->mdio_meta;
1637 /* Find disk position in metadata by its reference. */
1638 disk_pos = ddf_meta_find_disk(vmeta, reference,
1639 &md_disk_bvd, &md_disk_pos);
1640 md_pde_pos = ddf_meta_find_pd(gmeta, NULL, reference);
1643 G_RAID_DEBUG1(1, sc,
1644 "Disk %s is not a present part of the volume %s",
1645 g_raid_get_diskname(disk), vol->v_name);
1647 /* Failed stale disk is useless for us. */
1648 if ((GET16(gmeta, pdr->entry[md_pde_pos].PD_State) & DDF_PDE_PFA) != 0) {
1649 g_raid_change_disk_state(disk, G_RAID_DISK_S_STALE_FAILED);
1653 /* If disk has some metadata for this volume - erase. */
1654 if ((vdc1 = ddf_meta_find_vdc(pdmeta, vmeta->vdc->VD_GUID)) != NULL)
1655 SET32D(pdmeta, vdc1->Signature, 0xffffffff);
1657 /* If we are in the start process, that's all for now. */
1658 if (!pv->pv_started)
1661 * If we have already started - try to get use of the disk.
1662 * Try to replace OFFLINE disks first, then FAILED.
1664 if (ddf_meta_count_vdc(&pd->pd_meta, NULL) >=
1665 GET16(&pd->pd_meta, hdr->Max_Partitions)) {
1666 G_RAID_DEBUG1(1, sc, "No free partitions on disk %s",
1667 g_raid_get_diskname(disk));
1670 ddf_meta_unused_range(&pd->pd_meta, &eoff, &esize);
1672 G_RAID_DEBUG1(1, sc, "No free space on disk %s",
1673 g_raid_get_diskname(disk));
1676 eoff *= pd->pd_meta.sectorsize;
1677 esize *= pd->pd_meta.sectorsize;
1679 for (i = 0; i < vol->v_disks_count; i++) {
1680 sd = &vol->v_subdisks[i];
1681 if (sd->sd_state != G_RAID_SUBDISK_S_NONE)
1683 if (sd->sd_state <= G_RAID_SUBDISK_S_FAILED &&
1685 vol->v_subdisks[i].sd_state < sd->sd_state))
1688 if (disk_pos >= 0 &&
1689 vol->v_raid_level != G_RAID_VOLUME_RL_CONCAT &&
1691 G_RAID_DEBUG1(1, sc, "Disk %s free space "
1692 "is too small (%ju < %ju)",
1693 g_raid_get_diskname(disk), esize, size);
1696 if (disk_pos >= 0) {
1697 if (vol->v_raid_level != G_RAID_VOLUME_RL_CONCAT)
1699 md_disk_bvd = disk_pos / GET16(vmeta, vdc->Primary_Element_Count); // XXX
1700 md_disk_pos = disk_pos % GET16(vmeta, vdc->Primary_Element_Count); // XXX
1703 if (disk->d_state == G_RAID_DISK_S_NONE)
1704 g_raid_change_disk_state(disk,
1705 G_RAID_DISK_S_STALE);
1710 * If spare is committable, delete spare record.
1711 * Othersize, mark it active and leave there.
1713 sa = ddf_meta_find_sa(&pd->pd_meta, 0);
1715 if ((GET8D(&pd->pd_meta, sa->Spare_Type) &
1716 DDF_SAR_TYPE_REVERTIBLE) == 0) {
1717 SET32D(&pd->pd_meta, sa->Signature, 0xffffffff);
1719 SET8D(&pd->pd_meta, sa->Spare_Type,
1720 GET8D(&pd->pd_meta, sa->Spare_Type) |
1721 DDF_SAR_TYPE_ACTIVE);
1725 G_RAID_DEBUG1(1, sc, "Disk %s takes pos %d in the volume %s",
1726 g_raid_get_diskname(disk), disk_pos, vol->v_name);
1730 sd = &vol->v_subdisks[disk_pos];
1732 if (resurrection && sd->sd_disk != NULL) {
1733 g_raid_change_disk_state(sd->sd_disk,
1734 G_RAID_DISK_S_STALE_FAILED);
1735 TAILQ_REMOVE(&sd->sd_disk->d_subdisks,
1738 vol->v_subdisks[disk_pos].sd_disk = disk;
1739 TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
1741 /* Welcome the new disk. */
1743 g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE);
1744 else if (GET16(gmeta, pdr->entry[md_pde_pos].PD_State) & DDF_PDE_PFA)
1745 g_raid_change_disk_state(disk, G_RAID_DISK_S_FAILED);
1747 g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE);
1750 sd->sd_offset = eoff;
1751 sd->sd_size = esize;
1752 } else if (pdmeta->cr != NULL &&
1753 (vdc1 = ddf_meta_find_vdc(pdmeta, vmeta->vdc->VD_GUID)) != NULL) {
1754 val2 = (uint64_t *)&(vdc1->Physical_Disk_Sequence[GET16(vmeta, hdr->Max_Primary_Element_Entries)]);
1755 sd->sd_offset = (off_t)GET64P(pdmeta, val2 + md_disk_pos) * 512;
1756 sd->sd_size = (off_t)GET64D(pdmeta, vdc1->Block_Count) * 512;
1760 /* Stale disk, almost same as new. */
1761 g_raid_change_subdisk_state(sd,
1762 G_RAID_SUBDISK_S_NEW);
1763 } else if (GET16(gmeta, pdr->entry[md_pde_pos].PD_State) & DDF_PDE_PFA) {
1765 g_raid_change_subdisk_state(sd,
1766 G_RAID_SUBDISK_S_FAILED);
1767 } else if ((GET16(gmeta, pdr->entry[md_pde_pos].PD_State) &
1768 (DDF_PDE_FAILED | DDF_PDE_REBUILD)) != 0) {
1769 /* Rebuilding disk. */
1770 g_raid_change_subdisk_state(sd,
1771 G_RAID_SUBDISK_S_REBUILD);
1772 sd->sd_rebuild_pos = 0;
1773 } else if ((GET8(vmeta, vde->VD_State) & DDF_VDE_DIRTY) != 0 ||
1774 (GET8(vmeta, vde->Init_State) & DDF_VDE_INIT_MASK) !=
1775 DDF_VDE_INIT_FULL) {
1776 /* Stale disk or dirty volume (unclean shutdown). */
1777 g_raid_change_subdisk_state(sd,
1778 G_RAID_SUBDISK_S_STALE);
1780 /* Up to date disk. */
1781 g_raid_change_subdisk_state(sd,
1782 G_RAID_SUBDISK_S_ACTIVE);
1784 g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW,
1785 G_RAID_EVENT_SUBDISK);
1787 return (resurrection);
1791 g_raid_md_ddf_refill(struct g_raid_softc *sc)
1793 struct g_raid_volume *vol;
1794 struct g_raid_subdisk *sd;
1795 struct g_raid_disk *disk;
1796 struct g_raid_md_object *md;
1797 struct g_raid_md_ddf_perdisk *pd;
1798 struct g_raid_md_ddf_pervolume *pv;
1799 int update, updated, i, bad;
1804 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
1805 pv = vol->v_md_data;
1806 if (!pv->pv_started || vol->v_stopping)
1809 /* Search for subdisk that needs replacement. */
1811 for (i = 0; i < vol->v_disks_count; i++) {
1812 sd = &vol->v_subdisks[i];
1813 if (sd->sd_state == G_RAID_SUBDISK_S_NONE ||
1814 sd->sd_state == G_RAID_SUBDISK_S_FAILED)
1820 G_RAID_DEBUG1(1, sc, "Volume %s is not complete, "
1821 "trying to refill.", vol->v_name);
1823 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1825 if (disk->d_state < G_RAID_DISK_S_SPARE)
1827 /* Skip already used by this volume. */
1828 for (i = 0; i < vol->v_disks_count; i++) {
1829 sd = &vol->v_subdisks[i];
1830 if (sd->sd_disk == disk)
1833 if (i < vol->v_disks_count)
1836 /* Try to use disk if it has empty extents. */
1837 pd = disk->d_md_data;
1838 if (ddf_meta_count_vdc(&pd->pd_meta, NULL) <
1839 GET16(&pd->pd_meta, hdr->Max_Partitions)) {
1840 update = g_raid_md_ddf_start_disk(disk, vol);
1845 g_raid_md_write_ddf(md, vol, NULL, disk);
1855 g_raid_md_ddf_start(struct g_raid_volume *vol)
1857 struct g_raid_softc *sc;
1858 struct g_raid_subdisk *sd;
1859 struct g_raid_disk *disk;
1860 struct g_raid_md_object *md;
1861 struct g_raid_md_ddf_perdisk *pd;
1862 struct g_raid_md_ddf_pervolume *pv;
1863 struct g_raid_md_ddf_object *mdi;
1864 struct ddf_vol_meta *vmeta;
1870 mdi = (struct g_raid_md_ddf_object *)md;
1871 pv = vol->v_md_data;
1872 vmeta = &pv->pv_meta;
1874 vol->v_raid_level = GET8(vmeta, vdc->Primary_RAID_Level);
1875 vol->v_raid_level_qualifier = GET8(vmeta, vdc->RLQ);
1876 if (GET8(vmeta, vdc->Secondary_Element_Count) > 1 &&
1877 vol->v_raid_level == G_RAID_VOLUME_RL_RAID1 &&
1878 GET8(vmeta, vdc->Secondary_RAID_Level) == 0)
1879 vol->v_raid_level = G_RAID_VOLUME_RL_RAID1E;
1880 vol->v_sectorsize = GET16(vmeta, vdc->Block_Size);
1881 if (vol->v_sectorsize == 0xffff)
1882 vol->v_sectorsize = vmeta->sectorsize;
1883 vol->v_strip_size = vol->v_sectorsize << GET8(vmeta, vdc->Stripe_Size);
1884 vol->v_disks_count = GET16(vmeta, vdc->Primary_Element_Count) *
1885 GET8(vmeta, vdc->Secondary_Element_Count);
1886 vol->v_mdf_pdisks = GET8(vmeta, vdc->MDF_Parity_Disks);
1887 vol->v_mdf_polynomial = GET16(vmeta, vdc->MDF_Parity_Generator_Polynomial);
1888 vol->v_mdf_method = GET8(vmeta, vdc->MDF_Constant_Generation_Method);
1889 if (GET8(vmeta, vdc->Rotate_Parity_count) > 31)
1890 vol->v_rotate_parity = 1;
1892 vol->v_rotate_parity = 1 << GET8(vmeta, vdc->Rotate_Parity_count);
1893 vol->v_mediasize = GET64(vmeta, vdc->VD_Size) * vol->v_sectorsize;
1894 for (i = 0, j = 0, bvd = 0; i < vol->v_disks_count; i++, j++) {
1895 if (j == GET16(vmeta, vdc->Primary_Element_Count)) {
1899 sd = &vol->v_subdisks[i];
1900 if (vmeta->bvdc[bvd] == NULL) {
1902 sd->sd_size = GET64(vmeta, vdc->Block_Count) *
1906 val2 = (uint64_t *)&(vmeta->bvdc[bvd]->Physical_Disk_Sequence[
1907 GET16(vmeta, hdr->Max_Primary_Element_Entries)]);
1908 sd->sd_offset = GET64P(vmeta, val2 + j) * vol->v_sectorsize;
1909 sd->sd_size = GET64(vmeta, bvdc[bvd]->Block_Count) *
1912 g_raid_start_volume(vol);
1914 /* Make all disks found till the moment take their places. */
1915 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1916 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
1917 if (ddf_meta_find_vdc(&pd->pd_meta, vmeta->vdc->VD_GUID) != NULL)
1918 g_raid_md_ddf_start_disk(disk, vol);
1922 mdi->mdio_starting--;
1923 callout_stop(&pv->pv_start_co);
1924 G_RAID_DEBUG1(0, sc, "Volume started.");
1925 g_raid_md_write_ddf(md, vol, NULL, NULL);
1927 /* Pickup any STALE/SPARE disks to refill array if needed. */
1928 g_raid_md_ddf_refill(sc);
1930 g_raid_event_send(vol, G_RAID_VOLUME_E_START, G_RAID_EVENT_VOLUME);
1934 g_raid_ddf_go(void *arg)
1936 struct g_raid_volume *vol;
1937 struct g_raid_softc *sc;
1938 struct g_raid_md_ddf_pervolume *pv;
1941 pv = vol->v_md_data;
1943 if (!pv->pv_started) {
1944 G_RAID_DEBUG1(0, sc, "Force volume start due to timeout.");
1945 g_raid_event_send(vol, G_RAID_VOLUME_E_STARTMD,
1946 G_RAID_EVENT_VOLUME);
1951 g_raid_md_ddf_new_disk(struct g_raid_disk *disk)
1953 struct g_raid_softc *sc;
1954 struct g_raid_md_object *md;
1955 struct g_raid_md_ddf_perdisk *pd;
1956 struct g_raid_md_ddf_pervolume *pv;
1957 struct g_raid_md_ddf_object *mdi;
1958 struct g_raid_volume *vol;
1959 struct ddf_meta *pdmeta;
1960 struct ddf_vol_meta *vmeta;
1961 struct ddf_vdc_record *vdc;
1962 struct ddf_vd_entry *vde;
1963 int i, j, k, num, have, need, cnt, spare;
1969 mdi = (struct g_raid_md_ddf_object *)md;
1970 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
1971 pdmeta = &pd->pd_meta;
1974 if (mdi->mdio_meta.hdr == NULL)
1975 ddf_meta_copy(&mdi->mdio_meta, pdmeta);
1977 ddf_meta_update(&mdi->mdio_meta, pdmeta);
1979 num = GETCRNUM(pdmeta);
1980 for (j = 0; j < num; j++) {
1981 vdc = GETVDCPTR(pdmeta, j);
1982 val = GET32D(pdmeta, vdc->Signature);
1984 if (val == DDF_SA_SIGNATURE && spare == -1)
1987 if (val != DDF_VDCR_SIGNATURE)
1990 k = ddf_meta_find_vd(pdmeta, vdc->VD_GUID);
1993 vde = &pdmeta->vdr->entry[k];
1995 /* Look for volume with matching ID. */
1996 vol = g_raid_md_ddf_get_volume(sc, vdc->VD_GUID);
1998 ddf_meta_get_name(pdmeta, k, buf);
1999 vol = g_raid_create_volume(sc, buf,
2000 GET16D(pdmeta, vde->VD_Number));
2001 pv = malloc(sizeof(*pv), M_MD_DDF, M_WAITOK | M_ZERO);
2002 vol->v_md_data = pv;
2003 callout_init(&pv->pv_start_co, 1);
2004 callout_reset(&pv->pv_start_co,
2005 g_raid_start_timeout * hz,
2006 g_raid_ddf_go, vol);
2007 mdi->mdio_starting++;
2009 pv = vol->v_md_data;
2011 /* If we haven't started yet - check metadata freshness. */
2012 vmeta = &pv->pv_meta;
2013 ddf_vol_meta_update(vmeta, pdmeta, vdc->VD_GUID, pv->pv_started);
2017 g_raid_change_disk_state(disk, G_RAID_DISK_S_SPARE);
2018 g_raid_md_ddf_refill(sc);
2021 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2022 pv = vol->v_md_data;
2023 vmeta = &pv->pv_meta;
2025 if (ddf_meta_find_vdc(pdmeta, vmeta->vdc->VD_GUID) == NULL)
2028 if (pv->pv_started) {
2029 if (g_raid_md_ddf_start_disk(disk, vol))
2030 g_raid_md_write_ddf(md, vol, NULL, NULL);
2034 /* If we collected all needed disks - start array. */
2037 for (k = 0; k < GET8(vmeta, vdc->Secondary_Element_Count); k++) {
2038 if (vmeta->bvdc[k] == NULL) {
2039 need += GET16(vmeta, vdc->Primary_Element_Count);
2042 cnt = GET16(vmeta, bvdc[k]->Primary_Element_Count);
2044 for (i = 0; i < cnt; i++) {
2045 val = GET32(vmeta, bvdc[k]->Physical_Disk_Sequence[i]);
2046 if (g_raid_md_ddf_get_disk(sc, NULL, val) != NULL)
2050 G_RAID_DEBUG1(1, sc, "Volume %s now has %d of %d disks",
2051 vol->v_name, have, need);
2053 g_raid_md_ddf_start(vol);
2058 g_raid_md_create_req_ddf(struct g_raid_md_object *md, struct g_class *mp,
2059 struct gctl_req *req, struct g_geom **gp)
2061 struct g_geom *geom;
2062 struct g_raid_softc *sc;
2063 struct g_raid_md_ddf_object *mdi, *mdi1;
2068 mdi = (struct g_raid_md_ddf_object *)md;
2069 fmtopt = gctl_get_asciiparam(req, "fmtopt");
2070 if (fmtopt == NULL || strcasecmp(fmtopt, "BE") == 0)
2072 else if (strcasecmp(fmtopt, "LE") == 0)
2075 gctl_error(req, "Incorrect fmtopt argument.");
2076 return (G_RAID_MD_TASTE_FAIL);
2079 /* Search for existing node. */
2080 LIST_FOREACH(geom, &mp->geom, geom) {
2084 if (sc->sc_stopping != 0)
2086 if (sc->sc_md->mdo_class != md->mdo_class)
2088 mdi1 = (struct g_raid_md_ddf_object *)sc->sc_md;
2089 if (mdi1->mdio_bigendian != be)
2095 return (G_RAID_MD_TASTE_EXISTING);
2098 /* Create new one if not found. */
2099 mdi->mdio_bigendian = be;
2100 snprintf(name, sizeof(name), "DDF%s", be ? "" : "-LE");
2101 sc = g_raid_create_node(mp, name, md);
2103 return (G_RAID_MD_TASTE_FAIL);
2106 return (G_RAID_MD_TASTE_NEW);
2110 g_raid_md_taste_ddf(struct g_raid_md_object *md, struct g_class *mp,
2111 struct g_consumer *cp, struct g_geom **gp)
2113 struct g_consumer *rcp;
2114 struct g_provider *pp;
2115 struct g_raid_softc *sc;
2116 struct g_raid_disk *disk;
2117 struct ddf_meta meta;
2118 struct g_raid_md_ddf_perdisk *pd;
2119 struct g_raid_md_ddf_object *mdi;
2120 struct g_geom *geom;
2121 int error, result, be;
2124 G_RAID_DEBUG(1, "Tasting DDF on %s", cp->provider->name);
2125 mdi = (struct g_raid_md_ddf_object *)md;
2128 /* Read metadata from device. */
2129 g_topology_unlock();
2130 bzero(&meta, sizeof(meta));
2131 error = ddf_meta_read(cp, &meta);
2134 return (G_RAID_MD_TASTE_FAIL);
2135 be = meta.bigendian;
2137 /* Metadata valid. Print it. */
2138 g_raid_md_ddf_print(&meta);
2140 /* Search for matching node. */
2142 LIST_FOREACH(geom, &mp->geom, geom) {
2146 if (sc->sc_stopping != 0)
2148 if (sc->sc_md->mdo_class != md->mdo_class)
2150 mdi = (struct g_raid_md_ddf_object *)sc->sc_md;
2151 if (mdi->mdio_bigendian != be)
2156 /* Found matching node. */
2158 G_RAID_DEBUG(1, "Found matching array %s", sc->sc_name);
2159 result = G_RAID_MD_TASTE_EXISTING;
2161 } else { /* Not found matching node -- create one. */
2162 result = G_RAID_MD_TASTE_NEW;
2163 mdi->mdio_bigendian = be;
2164 snprintf(name, sizeof(name), "DDF%s", be ? "" : "-LE");
2165 sc = g_raid_create_node(mp, name, md);
2170 /* There is no return after this point, so we close passed consumer. */
2171 g_access(cp, -1, 0, 0);
2173 rcp = g_new_consumer(geom);
2174 rcp->flags |= G_CF_DIRECT_RECEIVE;
2176 if (g_access(rcp, 1, 1, 1) != 0)
2179 g_topology_unlock();
2180 sx_xlock(&sc->sc_lock);
2182 pd = malloc(sizeof(*pd), M_MD_DDF, M_WAITOK | M_ZERO);
2184 disk = g_raid_create_disk(sc);
2185 disk->d_md_data = (void *)pd;
2186 disk->d_consumer = rcp;
2187 rcp->private = disk;
2189 g_raid_get_disk_info(disk);
2191 g_raid_md_ddf_new_disk(disk);
2193 sx_xunlock(&sc->sc_lock);
2200 g_raid_md_event_ddf(struct g_raid_md_object *md,
2201 struct g_raid_disk *disk, u_int event)
2203 struct g_raid_softc *sc;
2209 case G_RAID_DISK_E_DISCONNECTED:
2211 g_raid_change_disk_state(disk, G_RAID_DISK_S_NONE);
2212 g_raid_destroy_disk(disk);
2213 g_raid_md_ddf_purge_volumes(sc);
2215 /* Write updated metadata to all disks. */
2216 g_raid_md_write_ddf(md, NULL, NULL, NULL);
2218 /* Check if anything left. */
2219 if (g_raid_ndisks(sc, -1) == 0)
2220 g_raid_destroy_node(sc, 0);
2222 g_raid_md_ddf_refill(sc);
2229 g_raid_md_volume_event_ddf(struct g_raid_md_object *md,
2230 struct g_raid_volume *vol, u_int event)
2232 struct g_raid_md_ddf_pervolume *pv;
2234 pv = (struct g_raid_md_ddf_pervolume *)vol->v_md_data;
2236 case G_RAID_VOLUME_E_STARTMD:
2237 if (!pv->pv_started)
2238 g_raid_md_ddf_start(vol);
2245 g_raid_md_ctl_ddf(struct g_raid_md_object *md,
2246 struct gctl_req *req)
2248 struct g_raid_softc *sc;
2249 struct g_raid_volume *vol, *vol1;
2250 struct g_raid_subdisk *sd;
2251 struct g_raid_disk *disk, *disks[DDF_MAX_DISKS_HARD];
2252 struct g_raid_md_ddf_perdisk *pd;
2253 struct g_raid_md_ddf_pervolume *pv;
2254 struct g_raid_md_ddf_object *mdi;
2255 struct ddf_sa_record *sa;
2256 struct g_consumer *cp;
2257 struct g_provider *pp;
2259 const char *nodename, *verb, *volname, *levelname, *diskname;
2262 off_t size, sectorsize, strip, offs[DDF_MAX_DISKS_HARD], esize;
2263 intmax_t *sizearg, *striparg;
2264 int i, numdisks, len, level, qual;
2268 mdi = (struct g_raid_md_ddf_object *)md;
2269 verb = gctl_get_param(req, "verb", NULL);
2270 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
2273 if (strcmp(verb, "label") == 0) {
2275 gctl_error(req, "Invalid number of arguments.");
2278 volname = gctl_get_asciiparam(req, "arg1");
2279 if (volname == NULL) {
2280 gctl_error(req, "No volume name.");
2283 levelname = gctl_get_asciiparam(req, "arg2");
2284 if (levelname == NULL) {
2285 gctl_error(req, "No RAID level.");
2288 if (g_raid_volume_str2level(levelname, &level, &qual)) {
2289 gctl_error(req, "Unknown RAID level '%s'.", levelname);
2292 numdisks = *nargs - 3;
2293 force = gctl_get_paraml(req, "force", sizeof(*force));
2294 if (!g_raid_md_ddf_supported(level, qual, numdisks,
2295 force ? *force : 0)) {
2296 gctl_error(req, "Unsupported RAID level "
2297 "(0x%02x/0x%02x), or number of disks (%d).",
2298 level, qual, numdisks);
2302 /* Search for disks, connect them and probe. */
2305 bzero(disks, sizeof(disks));
2306 bzero(offs, sizeof(offs));
2307 for (i = 0; i < numdisks; i++) {
2308 snprintf(arg, sizeof(arg), "arg%d", i + 3);
2309 diskname = gctl_get_asciiparam(req, arg);
2310 if (diskname == NULL) {
2311 gctl_error(req, "No disk name (%s).", arg);
2315 if (strcmp(diskname, "NONE") == 0)
2318 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2319 if (disk->d_consumer != NULL &&
2320 disk->d_consumer->provider != NULL &&
2321 strcmp(disk->d_consumer->provider->name,
2326 if (disk->d_state != G_RAID_DISK_S_ACTIVE) {
2327 gctl_error(req, "Disk '%s' is in a "
2328 "wrong state (%s).", diskname,
2329 g_raid_disk_state2str(disk->d_state));
2333 pd = disk->d_md_data;
2334 if (ddf_meta_count_vdc(&pd->pd_meta, NULL) >=
2335 GET16(&pd->pd_meta, hdr->Max_Partitions)) {
2336 gctl_error(req, "No free partitions "
2342 pp = disk->d_consumer->provider;
2344 ddf_meta_unused_range(&pd->pd_meta,
2346 offs[i] *= pp->sectorsize;
2347 size = MIN(size, (off_t)esize * pp->sectorsize);
2348 sectorsize = MAX(sectorsize, pp->sectorsize);
2353 cp = g_raid_open_consumer(sc, diskname);
2355 gctl_error(req, "Can't open disk '%s'.",
2357 g_topology_unlock();
2362 pd = malloc(sizeof(*pd), M_MD_DDF, M_WAITOK | M_ZERO);
2363 disk = g_raid_create_disk(sc);
2364 disk->d_md_data = (void *)pd;
2365 disk->d_consumer = cp;
2368 ddf_meta_create(disk, &mdi->mdio_meta);
2369 if (mdi->mdio_meta.hdr == NULL)
2370 ddf_meta_copy(&mdi->mdio_meta, &pd->pd_meta);
2372 ddf_meta_update(&mdi->mdio_meta, &pd->pd_meta);
2373 g_topology_unlock();
2375 g_raid_get_disk_info(disk);
2377 /* Reserve some space for metadata. */
2378 size = MIN(size, GET64(&pd->pd_meta,
2379 pdr->entry[0].Configured_Size) * pp->sectorsize);
2380 sectorsize = MAX(sectorsize, pp->sectorsize);
2383 for (i = 0; i < numdisks; i++) {
2384 if (disks[i] != NULL &&
2385 disks[i]->d_state == G_RAID_DISK_S_NONE)
2386 g_raid_destroy_disk(disks[i]);
2391 if (sectorsize <= 0) {
2392 gctl_error(req, "Can't get sector size.");
2396 /* Handle size argument. */
2397 len = sizeof(*sizearg);
2398 sizearg = gctl_get_param(req, "size", &len);
2399 if (sizearg != NULL && len == sizeof(*sizearg) &&
2401 if (*sizearg > size) {
2402 gctl_error(req, "Size too big %lld > %lld.",
2403 (long long)*sizearg, (long long)size);
2409 /* Handle strip argument. */
2411 len = sizeof(*striparg);
2412 striparg = gctl_get_param(req, "strip", &len);
2413 if (striparg != NULL && len == sizeof(*striparg) &&
2415 if (*striparg < sectorsize) {
2416 gctl_error(req, "Strip size too small.");
2419 if (*striparg % sectorsize != 0) {
2420 gctl_error(req, "Incorrect strip size.");
2426 /* Round size down to strip or sector. */
2427 if (level == G_RAID_VOLUME_RL_RAID1 ||
2428 level == G_RAID_VOLUME_RL_RAID3 ||
2429 level == G_RAID_VOLUME_RL_SINGLE ||
2430 level == G_RAID_VOLUME_RL_CONCAT)
2431 size -= (size % sectorsize);
2432 else if (level == G_RAID_VOLUME_RL_RAID1E &&
2433 (numdisks & 1) != 0)
2434 size -= (size % (2 * strip));
2436 size -= (size % strip);
2438 gctl_error(req, "Size too small.");
2442 /* We have all we need, create things: volume, ... */
2443 pv = malloc(sizeof(*pv), M_MD_DDF, M_WAITOK | M_ZERO);
2444 ddf_vol_meta_create(&pv->pv_meta, &mdi->mdio_meta);
2446 vol = g_raid_create_volume(sc, volname, -1);
2447 vol->v_md_data = pv;
2448 vol->v_raid_level = level;
2449 vol->v_raid_level_qualifier = qual;
2450 vol->v_strip_size = strip;
2451 vol->v_disks_count = numdisks;
2452 if (level == G_RAID_VOLUME_RL_RAID0 ||
2453 level == G_RAID_VOLUME_RL_CONCAT ||
2454 level == G_RAID_VOLUME_RL_SINGLE)
2455 vol->v_mediasize = size * numdisks;
2456 else if (level == G_RAID_VOLUME_RL_RAID1)
2457 vol->v_mediasize = size;
2458 else if (level == G_RAID_VOLUME_RL_RAID3 ||
2459 level == G_RAID_VOLUME_RL_RAID4 ||
2460 level == G_RAID_VOLUME_RL_RAID5)
2461 vol->v_mediasize = size * (numdisks - 1);
2462 else if (level == G_RAID_VOLUME_RL_RAID5R) {
2463 vol->v_mediasize = size * (numdisks - 1);
2464 vol->v_rotate_parity = 1024;
2465 } else if (level == G_RAID_VOLUME_RL_RAID6 ||
2466 level == G_RAID_VOLUME_RL_RAID5E ||
2467 level == G_RAID_VOLUME_RL_RAID5EE)
2468 vol->v_mediasize = size * (numdisks - 2);
2469 else if (level == G_RAID_VOLUME_RL_RAIDMDF) {
2471 vol->v_mdf_pdisks = 2;
2473 vol->v_mdf_pdisks = 3;
2474 vol->v_mdf_polynomial = 0x11d;
2475 vol->v_mdf_method = 0x00;
2476 vol->v_mediasize = size * (numdisks - vol->v_mdf_pdisks);
2477 } else { /* RAID1E */
2478 vol->v_mediasize = ((size * numdisks) / strip / 2) *
2481 vol->v_sectorsize = sectorsize;
2482 g_raid_start_volume(vol);
2484 /* , and subdisks. */
2485 for (i = 0; i < numdisks; i++) {
2487 sd = &vol->v_subdisks[i];
2489 sd->sd_offset = offs[i];
2493 TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
2494 g_raid_change_disk_state(disk,
2495 G_RAID_DISK_S_ACTIVE);
2496 g_raid_change_subdisk_state(sd,
2497 G_RAID_SUBDISK_S_ACTIVE);
2498 g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW,
2499 G_RAID_EVENT_SUBDISK);
2502 /* Write metadata based on created entities. */
2503 G_RAID_DEBUG1(0, sc, "Array started.");
2504 g_raid_md_write_ddf(md, vol, NULL, NULL);
2506 /* Pickup any STALE/SPARE disks to refill array if needed. */
2507 g_raid_md_ddf_refill(sc);
2509 g_raid_event_send(vol, G_RAID_VOLUME_E_START,
2510 G_RAID_EVENT_VOLUME);
2513 if (strcmp(verb, "add") == 0) {
2514 gctl_error(req, "`add` command is not applicable, "
2515 "use `label` instead.");
2518 if (strcmp(verb, "delete") == 0) {
2519 nodename = gctl_get_asciiparam(req, "arg0");
2520 if (nodename != NULL && strcasecmp(sc->sc_name, nodename) != 0)
2523 /* Full node destruction. */
2524 if (*nargs == 1 && nodename != NULL) {
2525 /* Check if some volume is still open. */
2526 force = gctl_get_paraml(req, "force", sizeof(*force));
2527 if (force != NULL && *force == 0 &&
2528 g_raid_nopens(sc) != 0) {
2529 gctl_error(req, "Some volume is still open.");
2533 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2534 if (disk->d_consumer)
2535 ddf_meta_erase(disk->d_consumer);
2537 g_raid_destroy_node(sc, 0);
2541 /* Destroy specified volume. If it was last - all node. */
2543 gctl_error(req, "Invalid number of arguments.");
2546 volname = gctl_get_asciiparam(req,
2547 nodename != NULL ? "arg1" : "arg0");
2548 if (volname == NULL) {
2549 gctl_error(req, "No volume name.");
2553 /* Search for volume. */
2554 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2555 if (strcmp(vol->v_name, volname) == 0)
2557 pp = vol->v_provider;
2560 if (strcmp(pp->name, volname) == 0)
2562 if (strncmp(pp->name, "raid/", 5) == 0 &&
2563 strcmp(pp->name + 5, volname) == 0)
2567 i = strtol(volname, &tmp, 10);
2568 if (verb != volname && tmp[0] == 0) {
2569 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2570 if (vol->v_global_id == i)
2576 gctl_error(req, "Volume '%s' not found.", volname);
2580 /* Check if volume is still open. */
2581 force = gctl_get_paraml(req, "force", sizeof(*force));
2582 if (force != NULL && *force == 0 &&
2583 vol->v_provider_open != 0) {
2584 gctl_error(req, "Volume is still open.");
2588 /* Destroy volume and potentially node. */
2590 TAILQ_FOREACH(vol1, &sc->sc_volumes, v_next)
2593 g_raid_destroy_volume(vol);
2594 g_raid_md_ddf_purge_disks(sc);
2595 g_raid_md_write_ddf(md, NULL, NULL, NULL);
2597 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2598 if (disk->d_consumer)
2599 ddf_meta_erase(disk->d_consumer);
2601 g_raid_destroy_node(sc, 0);
2605 if (strcmp(verb, "remove") == 0 ||
2606 strcmp(verb, "fail") == 0) {
2608 gctl_error(req, "Invalid number of arguments.");
2611 for (i = 1; i < *nargs; i++) {
2612 snprintf(arg, sizeof(arg), "arg%d", i);
2613 diskname = gctl_get_asciiparam(req, arg);
2614 if (diskname == NULL) {
2615 gctl_error(req, "No disk name (%s).", arg);
2619 if (strncmp(diskname, _PATH_DEV, 5) == 0)
2622 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2623 if (disk->d_consumer != NULL &&
2624 disk->d_consumer->provider != NULL &&
2625 strcmp(disk->d_consumer->provider->name,
2630 gctl_error(req, "Disk '%s' not found.",
2636 if (strcmp(verb, "fail") == 0) {
2637 g_raid_md_fail_disk_ddf(md, NULL, disk);
2641 /* Erase metadata on deleting disk and destroy it. */
2642 ddf_meta_erase(disk->d_consumer);
2643 g_raid_destroy_disk(disk);
2645 g_raid_md_ddf_purge_volumes(sc);
2647 /* Write updated metadata to remaining disks. */
2648 g_raid_md_write_ddf(md, NULL, NULL, NULL);
2650 /* Check if anything left. */
2651 if (g_raid_ndisks(sc, -1) == 0)
2652 g_raid_destroy_node(sc, 0);
2654 g_raid_md_ddf_refill(sc);
2657 if (strcmp(verb, "insert") == 0) {
2659 gctl_error(req, "Invalid number of arguments.");
2662 for (i = 1; i < *nargs; i++) {
2663 /* Get disk name. */
2664 snprintf(arg, sizeof(arg), "arg%d", i);
2665 diskname = gctl_get_asciiparam(req, arg);
2666 if (diskname == NULL) {
2667 gctl_error(req, "No disk name (%s).", arg);
2672 /* Try to find provider with specified name. */
2674 cp = g_raid_open_consumer(sc, diskname);
2676 gctl_error(req, "Can't open disk '%s'.",
2678 g_topology_unlock();
2683 g_topology_unlock();
2685 pd = malloc(sizeof(*pd), M_MD_DDF, M_WAITOK | M_ZERO);
2687 disk = g_raid_create_disk(sc);
2688 disk->d_consumer = cp;
2689 disk->d_md_data = (void *)pd;
2692 g_raid_get_disk_info(disk);
2694 /* Welcome the "new" disk. */
2695 g_raid_change_disk_state(disk, G_RAID_DISK_S_SPARE);
2696 ddf_meta_create(disk, &mdi->mdio_meta);
2697 sa = ddf_meta_find_sa(&pd->pd_meta, 1);
2699 SET32D(&pd->pd_meta, sa->Signature,
2701 SET8D(&pd->pd_meta, sa->Spare_Type, 0);
2702 SET16D(&pd->pd_meta, sa->Populated_SAEs, 0);
2703 SET16D(&pd->pd_meta, sa->MAX_SAE_Supported,
2704 (GET16(&pd->pd_meta, hdr->Configuration_Record_Length) *
2705 pd->pd_meta.sectorsize -
2706 sizeof(struct ddf_sa_record)) /
2707 sizeof(struct ddf_sa_entry));
2709 if (mdi->mdio_meta.hdr == NULL)
2710 ddf_meta_copy(&mdi->mdio_meta, &pd->pd_meta);
2712 ddf_meta_update(&mdi->mdio_meta, &pd->pd_meta);
2713 g_raid_md_write_ddf(md, NULL, NULL, NULL);
2714 g_raid_md_ddf_refill(sc);
2722 g_raid_md_write_ddf(struct g_raid_md_object *md, struct g_raid_volume *tvol,
2723 struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk)
2725 struct g_raid_softc *sc;
2726 struct g_raid_volume *vol;
2727 struct g_raid_subdisk *sd;
2728 struct g_raid_disk *disk;
2729 struct g_raid_md_ddf_perdisk *pd;
2730 struct g_raid_md_ddf_pervolume *pv;
2731 struct g_raid_md_ddf_object *mdi;
2732 struct ddf_meta *gmeta;
2733 struct ddf_vol_meta *vmeta;
2734 struct ddf_vdc_record *vdc;
2735 struct ddf_sa_record *sa;
2737 int i, j, pos, bvd, size;
2740 mdi = (struct g_raid_md_ddf_object *)md;
2741 gmeta = &mdi->mdio_meta;
2743 if (sc->sc_stopping == G_RAID_DESTROY_HARD)
2747 * Clear disk flags to let only really needed ones to be reset.
2748 * Do it only if there are no volumes in starting state now,
2749 * as they can update disk statuses yet and we may kill innocent.
2751 if (mdi->mdio_starting == 0) {
2752 for (i = 0; i < GET16(gmeta, pdr->Populated_PDEs); i++) {
2753 if (isff(gmeta->pdr->entry[i].PD_GUID, 24))
2755 SET16(gmeta, pdr->entry[i].PD_Type,
2756 GET16(gmeta, pdr->entry[i].PD_Type) &
2757 ~(DDF_PDE_PARTICIPATING |
2758 DDF_PDE_GLOBAL_SPARE | DDF_PDE_CONFIG_SPARE));
2759 if ((GET16(gmeta, pdr->entry[i].PD_State) &
2761 SET16(gmeta, pdr->entry[i].PD_State, 0);
2765 /* Generate/update new per-volume metadata. */
2766 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2767 pv = (struct g_raid_md_ddf_pervolume *)vol->v_md_data;
2768 if (vol->v_stopping || !pv->pv_started)
2770 vmeta = &pv->pv_meta;
2772 SET32(vmeta, vdc->Sequence_Number,
2773 GET32(vmeta, vdc->Sequence_Number) + 1);
2774 if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E &&
2775 vol->v_disks_count % 2 == 0)
2776 SET16(vmeta, vdc->Primary_Element_Count, 2);
2778 SET16(vmeta, vdc->Primary_Element_Count,
2779 vol->v_disks_count);
2780 SET8(vmeta, vdc->Stripe_Size,
2781 ffs(vol->v_strip_size / vol->v_sectorsize) - 1);
2782 if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E &&
2783 vol->v_disks_count % 2 == 0) {
2784 SET8(vmeta, vdc->Primary_RAID_Level,
2786 SET8(vmeta, vdc->RLQ, 0);
2787 SET8(vmeta, vdc->Secondary_Element_Count,
2788 vol->v_disks_count / 2);
2789 SET8(vmeta, vdc->Secondary_RAID_Level, 0);
2791 SET8(vmeta, vdc->Primary_RAID_Level,
2793 SET8(vmeta, vdc->RLQ,
2794 vol->v_raid_level_qualifier);
2795 SET8(vmeta, vdc->Secondary_Element_Count, 1);
2796 SET8(vmeta, vdc->Secondary_RAID_Level, 0);
2798 SET8(vmeta, vdc->Secondary_Element_Seq, 0);
2799 SET64(vmeta, vdc->Block_Count, 0);
2800 SET64(vmeta, vdc->VD_Size, vol->v_mediasize / vol->v_sectorsize);
2801 SET16(vmeta, vdc->Block_Size, vol->v_sectorsize);
2802 SET8(vmeta, vdc->Rotate_Parity_count,
2803 fls(vol->v_rotate_parity) - 1);
2804 SET8(vmeta, vdc->MDF_Parity_Disks, vol->v_mdf_pdisks);
2805 SET16(vmeta, vdc->MDF_Parity_Generator_Polynomial,
2806 vol->v_mdf_polynomial);
2807 SET8(vmeta, vdc->MDF_Constant_Generation_Method,
2810 SET16(vmeta, vde->VD_Number, vol->v_global_id);
2811 if (vol->v_state <= G_RAID_VOLUME_S_BROKEN)
2812 SET8(vmeta, vde->VD_State, DDF_VDE_FAILED);
2813 else if (vol->v_state <= G_RAID_VOLUME_S_DEGRADED)
2814 SET8(vmeta, vde->VD_State, DDF_VDE_DEGRADED);
2815 else if (vol->v_state <= G_RAID_VOLUME_S_SUBOPTIMAL)
2816 SET8(vmeta, vde->VD_State, DDF_VDE_PARTIAL);
2818 SET8(vmeta, vde->VD_State, DDF_VDE_OPTIMAL);
2820 g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_STALE) > 0 ||
2821 g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_RESYNC) > 0)
2822 SET8(vmeta, vde->VD_State,
2823 GET8(vmeta, vde->VD_State) | DDF_VDE_DIRTY);
2824 SET8(vmeta, vde->Init_State, DDF_VDE_INIT_FULL); // XXX
2825 ddf_meta_put_name(vmeta, vol->v_name);
2827 for (i = 0; i < vol->v_disks_count; i++) {
2828 sd = &vol->v_subdisks[i];
2829 bvd = i / GET16(vmeta, vdc->Primary_Element_Count);
2830 pos = i % GET16(vmeta, vdc->Primary_Element_Count);
2833 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
2834 if (vmeta->bvdc[bvd] == NULL) {
2836 hdr->Configuration_Record_Length) *
2838 vmeta->bvdc[bvd] = malloc(size,
2839 M_MD_DDF, M_WAITOK);
2840 memset(vmeta->bvdc[bvd], 0xff, size);
2842 memcpy(vmeta->bvdc[bvd], vmeta->vdc,
2843 sizeof(struct ddf_vdc_record));
2844 SET8(vmeta, bvdc[bvd]->Secondary_Element_Seq, bvd);
2845 SET64(vmeta, bvdc[bvd]->Block_Count,
2846 sd->sd_size / vol->v_sectorsize);
2847 SET32(vmeta, bvdc[bvd]->Physical_Disk_Sequence[pos],
2848 GET32(&pd->pd_meta, pdd->PD_Reference));
2849 val2 = (uint64_t *)&(vmeta->bvdc[bvd]->Physical_Disk_Sequence[
2850 GET16(vmeta, hdr->Max_Primary_Element_Entries)]);
2851 SET64P(vmeta, val2 + pos,
2852 sd->sd_offset / vol->v_sectorsize);
2854 if (vmeta->bvdc[bvd] == NULL)
2857 j = ddf_meta_find_pd(gmeta, NULL,
2858 GET32(vmeta, bvdc[bvd]->Physical_Disk_Sequence[pos]));
2861 SET16(gmeta, pdr->entry[j].PD_Type,
2862 GET16(gmeta, pdr->entry[j].PD_Type) |
2863 DDF_PDE_PARTICIPATING);
2864 if (sd->sd_state == G_RAID_SUBDISK_S_NONE)
2865 SET16(gmeta, pdr->entry[j].PD_State,
2866 GET16(gmeta, pdr->entry[j].PD_State) |
2867 (DDF_PDE_FAILED | DDF_PDE_MISSING));
2868 else if (sd->sd_state == G_RAID_SUBDISK_S_FAILED)
2869 SET16(gmeta, pdr->entry[j].PD_State,
2870 GET16(gmeta, pdr->entry[j].PD_State) |
2871 (DDF_PDE_FAILED | DDF_PDE_PFA));
2872 else if (sd->sd_state <= G_RAID_SUBDISK_S_REBUILD)
2873 SET16(gmeta, pdr->entry[j].PD_State,
2874 GET16(gmeta, pdr->entry[j].PD_State) |
2877 SET16(gmeta, pdr->entry[j].PD_State,
2878 GET16(gmeta, pdr->entry[j].PD_State) |
2883 /* Mark spare and failed disks as such. */
2884 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2885 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
2886 i = ddf_meta_find_pd(gmeta, NULL,
2887 GET32(&pd->pd_meta, pdd->PD_Reference));
2890 if (disk->d_state == G_RAID_DISK_S_FAILED) {
2891 SET16(gmeta, pdr->entry[i].PD_State,
2892 GET16(gmeta, pdr->entry[i].PD_State) |
2893 (DDF_PDE_FAILED | DDF_PDE_PFA));
2895 if (disk->d_state != G_RAID_DISK_S_SPARE)
2897 sa = ddf_meta_find_sa(&pd->pd_meta, 0);
2899 (GET8D(&pd->pd_meta, sa->Spare_Type) &
2900 DDF_SAR_TYPE_DEDICATED) == 0) {
2901 SET16(gmeta, pdr->entry[i].PD_Type,
2902 GET16(gmeta, pdr->entry[i].PD_Type) |
2903 DDF_PDE_GLOBAL_SPARE);
2905 SET16(gmeta, pdr->entry[i].PD_Type,
2906 GET16(gmeta, pdr->entry[i].PD_Type) |
2907 DDF_PDE_CONFIG_SPARE);
2909 SET16(gmeta, pdr->entry[i].PD_State,
2910 GET16(gmeta, pdr->entry[i].PD_State) |
2914 /* Remove disks without "participating" flag (unused). */
2915 for (i = 0, j = -1; i < GET16(gmeta, pdr->Populated_PDEs); i++) {
2916 if (isff(gmeta->pdr->entry[i].PD_GUID, 24))
2918 if ((GET16(gmeta, pdr->entry[i].PD_Type) &
2919 (DDF_PDE_PARTICIPATING |
2920 DDF_PDE_GLOBAL_SPARE | DDF_PDE_CONFIG_SPARE)) != 0 ||
2921 g_raid_md_ddf_get_disk(sc,
2922 NULL, GET32(gmeta, pdr->entry[i].PD_Reference)) != NULL)
2925 memset(&gmeta->pdr->entry[i], 0xff,
2926 sizeof(struct ddf_pd_entry));
2928 SET16(gmeta, pdr->Populated_PDEs, j + 1);
2930 /* Update per-disk metadata and write them. */
2931 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2932 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
2933 if (disk->d_state != G_RAID_DISK_S_ACTIVE &&
2934 disk->d_state != G_RAID_DISK_S_SPARE)
2937 memcpy(pd->pd_meta.pdr, gmeta->pdr,
2938 GET32(&pd->pd_meta, hdr->pdr_length) *
2939 pd->pd_meta.sectorsize);
2941 SET16(&pd->pd_meta, vdr->Populated_VDEs, 0);
2942 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2943 if (vol->v_stopping)
2945 pv = (struct g_raid_md_ddf_pervolume *)vol->v_md_data;
2946 i = ddf_meta_find_vd(&pd->pd_meta,
2947 pv->pv_meta.vde->VD_GUID);
2949 i = ddf_meta_find_vd(&pd->pd_meta, NULL);
2951 memcpy(&pd->pd_meta.vdr->entry[i],
2953 sizeof(struct ddf_vd_entry));
2956 if (mdi->mdio_starting == 0) {
2957 /* Remove all VDCs to restore needed later. */
2958 j = GETCRNUM(&pd->pd_meta);
2959 for (i = 0; i < j; i++) {
2960 vdc = GETVDCPTR(&pd->pd_meta, i);
2961 if (GET32D(&pd->pd_meta, vdc->Signature) !=
2964 SET32D(&pd->pd_meta, vdc->Signature, 0xffffffff);
2967 TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
2968 vol = sd->sd_volume;
2969 if (vol->v_stopping)
2971 pv = (struct g_raid_md_ddf_pervolume *)vol->v_md_data;
2972 vmeta = &pv->pv_meta;
2973 vdc = ddf_meta_find_vdc(&pd->pd_meta,
2974 vmeta->vde->VD_GUID);
2976 vdc = ddf_meta_find_vdc(&pd->pd_meta, NULL);
2978 bvd = sd->sd_pos / GET16(vmeta,
2979 vdc->Primary_Element_Count);
2980 memcpy(vdc, vmeta->bvdc[bvd],
2982 hdr->Configuration_Record_Length) *
2983 pd->pd_meta.sectorsize);
2986 G_RAID_DEBUG(1, "Writing DDF metadata to %s",
2987 g_raid_get_diskname(disk));
2988 g_raid_md_ddf_print(&pd->pd_meta);
2989 ddf_meta_write(disk->d_consumer, &pd->pd_meta);
2995 g_raid_md_fail_disk_ddf(struct g_raid_md_object *md,
2996 struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk)
2998 struct g_raid_softc *sc;
2999 struct g_raid_md_ddf_perdisk *pd;
3000 struct g_raid_subdisk *sd;
3004 pd = (struct g_raid_md_ddf_perdisk *)tdisk->d_md_data;
3006 /* We can't fail disk that is not a part of array now. */
3007 if (tdisk->d_state != G_RAID_DISK_S_ACTIVE)
3011 * Mark disk as failed in metadata and try to write that metadata
3012 * to the disk itself to prevent it's later resurrection as STALE.
3014 G_RAID_DEBUG(1, "Writing DDF metadata to %s",
3015 g_raid_get_diskname(tdisk));
3016 i = ddf_meta_find_pd(&pd->pd_meta, NULL, GET32(&pd->pd_meta, pdd->PD_Reference));
3017 SET16(&pd->pd_meta, pdr->entry[i].PD_State, DDF_PDE_FAILED | DDF_PDE_PFA);
3018 if (tdisk->d_consumer != NULL)
3019 ddf_meta_write(tdisk->d_consumer, &pd->pd_meta);
3021 /* Change states. */
3022 g_raid_change_disk_state(tdisk, G_RAID_DISK_S_FAILED);
3023 TAILQ_FOREACH(sd, &tdisk->d_subdisks, sd_next) {
3024 g_raid_change_subdisk_state(sd,
3025 G_RAID_SUBDISK_S_FAILED);
3026 g_raid_event_send(sd, G_RAID_SUBDISK_E_FAILED,
3027 G_RAID_EVENT_SUBDISK);
3030 /* Write updated metadata to remaining disks. */
3031 g_raid_md_write_ddf(md, NULL, NULL, tdisk);
3033 g_raid_md_ddf_refill(sc);
3038 g_raid_md_free_disk_ddf(struct g_raid_md_object *md,
3039 struct g_raid_disk *disk)
3041 struct g_raid_md_ddf_perdisk *pd;
3043 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
3044 ddf_meta_free(&pd->pd_meta);
3046 disk->d_md_data = NULL;
3051 g_raid_md_free_volume_ddf(struct g_raid_md_object *md,
3052 struct g_raid_volume *vol)
3054 struct g_raid_md_ddf_object *mdi;
3055 struct g_raid_md_ddf_pervolume *pv;
3057 mdi = (struct g_raid_md_ddf_object *)md;
3058 pv = (struct g_raid_md_ddf_pervolume *)vol->v_md_data;
3059 ddf_vol_meta_free(&pv->pv_meta);
3060 if (!pv->pv_started) {
3062 mdi->mdio_starting--;
3063 callout_stop(&pv->pv_start_co);
3066 vol->v_md_data = NULL;
3071 g_raid_md_free_ddf(struct g_raid_md_object *md)
3073 struct g_raid_md_ddf_object *mdi;
3075 mdi = (struct g_raid_md_ddf_object *)md;
3076 if (!mdi->mdio_started) {
3077 mdi->mdio_started = 0;
3078 callout_stop(&mdi->mdio_start_co);
3079 G_RAID_DEBUG1(1, md->mdo_softc,
3080 "root_mount_rel %p", mdi->mdio_rootmount);
3081 root_mount_rel(mdi->mdio_rootmount);
3082 mdi->mdio_rootmount = NULL;
3084 ddf_meta_free(&mdi->mdio_meta);
3088 G_RAID_MD_DECLARE(ddf, "DDF");