2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
34 #include <sys/endian.h>
35 #include <sys/kernel.h>
37 #include <sys/limits.h>
39 #include <sys/malloc.h>
40 #include <sys/mutex.h>
41 #include <sys/systm.h>
43 #include <sys/clock.h>
44 #include <geom/geom.h>
45 #include "geom/raid/g_raid.h"
46 #include "geom/raid/md_ddf.h"
47 #include "g_raid_md_if.h"
49 static MALLOC_DEFINE(M_MD_DDF, "md_ddf_data", "GEOM_RAID DDF metadata");
51 #define DDF_MAX_DISKS_HARD 128
53 #define DDF_MAX_DISKS 16
54 #define DDF_MAX_VDISKS 7
55 #define DDF_MAX_PARTITIONS 1
57 #define DECADE (3600*24*(365*10+2)) /* 10 years in seconds. */
62 struct ddf_header *hdr;
63 struct ddf_cd_record *cdr;
64 struct ddf_pd_record *pdr;
65 struct ddf_vd_record *vdr;
67 struct ddf_pdd_record *pdd;
68 struct ddf_bbm_log *bbm;
74 struct ddf_header *hdr;
75 struct ddf_cd_record *cdr;
76 struct ddf_vd_entry *vde;
77 struct ddf_vdc_record *vdc;
78 struct ddf_vdc_record *bvdc[DDF_MAX_DISKS_HARD];
81 struct g_raid_md_ddf_perdisk {
82 struct ddf_meta pd_meta;
85 struct g_raid_md_ddf_pervolume {
86 struct ddf_vol_meta pv_meta;
88 struct callout pv_start_co; /* STARTING state timer. */
91 struct g_raid_md_ddf_object {
92 struct g_raid_md_object mdio_base;
94 struct ddf_meta mdio_meta;
96 struct callout mdio_start_co; /* STARTING state timer. */
98 struct root_hold_token *mdio_rootmount; /* Root mount delay token. */
101 static g_raid_md_create_req_t g_raid_md_create_req_ddf;
102 static g_raid_md_taste_t g_raid_md_taste_ddf;
103 static g_raid_md_event_t g_raid_md_event_ddf;
104 static g_raid_md_volume_event_t g_raid_md_volume_event_ddf;
105 static g_raid_md_ctl_t g_raid_md_ctl_ddf;
106 static g_raid_md_write_t g_raid_md_write_ddf;
107 static g_raid_md_fail_disk_t g_raid_md_fail_disk_ddf;
108 static g_raid_md_free_disk_t g_raid_md_free_disk_ddf;
109 static g_raid_md_free_volume_t g_raid_md_free_volume_ddf;
110 static g_raid_md_free_t g_raid_md_free_ddf;
112 static kobj_method_t g_raid_md_ddf_methods[] = {
113 KOBJMETHOD(g_raid_md_create_req, g_raid_md_create_req_ddf),
114 KOBJMETHOD(g_raid_md_taste, g_raid_md_taste_ddf),
115 KOBJMETHOD(g_raid_md_event, g_raid_md_event_ddf),
116 KOBJMETHOD(g_raid_md_volume_event, g_raid_md_volume_event_ddf),
117 KOBJMETHOD(g_raid_md_ctl, g_raid_md_ctl_ddf),
118 KOBJMETHOD(g_raid_md_write, g_raid_md_write_ddf),
119 KOBJMETHOD(g_raid_md_fail_disk, g_raid_md_fail_disk_ddf),
120 KOBJMETHOD(g_raid_md_free_disk, g_raid_md_free_disk_ddf),
121 KOBJMETHOD(g_raid_md_free_volume, g_raid_md_free_volume_ddf),
122 KOBJMETHOD(g_raid_md_free, g_raid_md_free_ddf),
126 static struct g_raid_md_class g_raid_md_ddf_class = {
128 g_raid_md_ddf_methods,
129 sizeof(struct g_raid_md_ddf_object),
134 #define GET8(m, f) ((m)->f)
135 #define GET16(m, f) ((m)->bigendian ? be16dec(&(m)->f) : le16dec(&(m)->f))
136 #define GET32(m, f) ((m)->bigendian ? be32dec(&(m)->f) : le32dec(&(m)->f))
137 #define GET64(m, f) ((m)->bigendian ? be64dec(&(m)->f) : le64dec(&(m)->f))
138 #define GET8D(m, f) (f)
139 #define GET16D(m, f) ((m)->bigendian ? be16dec(&f) : le16dec(&f))
140 #define GET32D(m, f) ((m)->bigendian ? be32dec(&f) : le32dec(&f))
141 #define GET64D(m, f) ((m)->bigendian ? be64dec(&f) : le64dec(&f))
142 #define GET8P(m, f) (*(f))
143 #define GET16P(m, f) ((m)->bigendian ? be16dec(f) : le16dec(f))
144 #define GET32P(m, f) ((m)->bigendian ? be32dec(f) : le32dec(f))
145 #define GET64P(m, f) ((m)->bigendian ? be64dec(f) : le64dec(f))
147 #define SET8P(m, f, v) \
149 #define SET16P(m, f, v) \
151 if ((m)->bigendian) \
156 #define SET32P(m, f, v) \
158 if ((m)->bigendian) \
163 #define SET64P(m, f, v) \
165 if ((m)->bigendian) \
170 #define SET8(m, f, v) SET8P((m), &((m)->f), (v))
171 #define SET16(m, f, v) SET16P((m), &((m)->f), (v))
172 #define SET32(m, f, v) SET32P((m), &((m)->f), (v))
173 #define SET64(m, f, v) SET64P((m), &((m)->f), (v))
174 #define SET8D(m, f, v) SET8P((m), &(f), (v))
175 #define SET16D(m, f, v) SET16P((m), &(f), (v))
176 #define SET32D(m, f, v) SET32P((m), &(f), (v))
177 #define SET64D(m, f, v) SET64P((m), &(f), (v))
179 #define GETCRNUM(m) (GET32((m), hdr->cr_length) / \
180 GET16((m), hdr->Configuration_Record_Length))
182 #define GETVDCPTR(m, n) ((struct ddf_vdc_record *)((uint8_t *)(m)->cr + \
183 (n) * GET16((m), hdr->Configuration_Record_Length) * \
186 #define GETSAPTR(m, n) ((struct ddf_sa_record *)((uint8_t *)(m)->cr + \
187 (n) * GET16((m), hdr->Configuration_Record_Length) * \
191 isff(uint8_t *buf, int size)
195 for (i = 0; i < size; i++)
202 print_guid(uint8_t *buf)
207 for (i = 0; i < 24; i++) {
208 if (buf[i] != 0 && (buf[i] < ' ' || buf[i] > 127)) {
214 printf("'%.24s'", buf);
216 for (i = 0; i < 24; i++)
217 printf("%02x", buf[i]);
222 g_raid_md_ddf_print(struct ddf_meta *meta)
224 struct ddf_vdc_record *vdc;
225 struct ddf_vuc_record *vuc;
226 struct ddf_sa_record *sa;
229 int i, j, k, num, num2;
231 if (g_raid_debug < 1)
234 printf("********* DDF Metadata *********\n");
235 printf("**** Header ****\n");
236 printf("DDF_Header_GUID ");
237 print_guid(meta->hdr->DDF_Header_GUID);
239 printf("DDF_rev %8.8s\n", (char *)&meta->hdr->DDF_rev[0]);
240 printf("Sequence_Number 0x%08x\n", GET32(meta, hdr->Sequence_Number));
241 printf("TimeStamp 0x%08x\n", GET32(meta, hdr->TimeStamp));
242 printf("Open_Flag 0x%02x\n", GET16(meta, hdr->Open_Flag));
243 printf("Foreign_Flag 0x%02x\n", GET16(meta, hdr->Foreign_Flag));
244 printf("Diskgrouping 0x%02x\n", GET16(meta, hdr->Diskgrouping));
245 printf("Primary_Header_LBA %ju\n", GET64(meta, hdr->Primary_Header_LBA));
246 printf("Secondary_Header_LBA %ju\n", GET64(meta, hdr->Secondary_Header_LBA));
247 printf("WorkSpace_Length %u\n", GET32(meta, hdr->WorkSpace_Length));
248 printf("WorkSpace_LBA %ju\n", GET64(meta, hdr->WorkSpace_LBA));
249 printf("Max_PD_Entries %u\n", GET16(meta, hdr->Max_PD_Entries));
250 printf("Max_VD_Entries %u\n", GET16(meta, hdr->Max_VD_Entries));
251 printf("Max_Partitions %u\n", GET16(meta, hdr->Max_Partitions));
252 printf("Configuration_Record_Length %u\n", GET16(meta, hdr->Configuration_Record_Length));
253 printf("Max_Primary_Element_Entries %u\n", GET16(meta, hdr->Max_Primary_Element_Entries));
254 printf("Controller Data %u:%u\n", GET32(meta, hdr->cd_section), GET32(meta, hdr->cd_length));
255 printf("Physical Disk %u:%u\n", GET32(meta, hdr->pdr_section), GET32(meta, hdr->pdr_length));
256 printf("Virtual Disk %u:%u\n", GET32(meta, hdr->vdr_section), GET32(meta, hdr->vdr_length));
257 printf("Configuration Recs %u:%u\n", GET32(meta, hdr->cr_section), GET32(meta, hdr->cr_length));
258 printf("Physical Disk Recs %u:%u\n", GET32(meta, hdr->pdd_section), GET32(meta, hdr->pdd_length));
259 printf("BBM Log %u:%u\n", GET32(meta, hdr->bbmlog_section), GET32(meta, hdr->bbmlog_length));
260 printf("Diagnostic Space %u:%u\n", GET32(meta, hdr->Diagnostic_Space), GET32(meta, hdr->Diagnostic_Space_Length));
261 printf("Vendor_Specific_Logs %u:%u\n", GET32(meta, hdr->Vendor_Specific_Logs), GET32(meta, hdr->Vendor_Specific_Logs_Length));
262 printf("**** Controller Data ****\n");
263 printf("Controller_GUID ");
264 print_guid(meta->cdr->Controller_GUID);
266 printf("Controller_Type 0x%04x%04x 0x%04x%04x\n",
267 GET16(meta, cdr->Controller_Type.Vendor_ID),
268 GET16(meta, cdr->Controller_Type.Device_ID),
269 GET16(meta, cdr->Controller_Type.SubVendor_ID),
270 GET16(meta, cdr->Controller_Type.SubDevice_ID));
271 printf("Product_ID '%.16s'\n", (char *)&meta->cdr->Product_ID[0]);
272 printf("**** Physical Disk Records ****\n");
273 printf("Populated_PDEs %u\n", GET16(meta, pdr->Populated_PDEs));
274 printf("Max_PDE_Supported %u\n", GET16(meta, pdr->Max_PDE_Supported));
275 for (j = 0; j < GET16(meta, pdr->Populated_PDEs); j++) {
276 if (isff(meta->pdr->entry[j].PD_GUID, 24))
278 if (GET32(meta, pdr->entry[j].PD_Reference) == 0xffffffff)
281 print_guid(meta->pdr->entry[j].PD_GUID);
283 printf("PD_Reference 0x%08x\n",
284 GET32(meta, pdr->entry[j].PD_Reference));
285 printf("PD_Type 0x%04x\n",
286 GET16(meta, pdr->entry[j].PD_Type));
287 printf("PD_State 0x%04x\n",
288 GET16(meta, pdr->entry[j].PD_State));
289 printf("Configured_Size %ju\n",
290 GET64(meta, pdr->entry[j].Configured_Size));
291 printf("Block_Size %u\n",
292 GET16(meta, pdr->entry[j].Block_Size));
294 printf("**** Virtual Disk Records ****\n");
295 printf("Populated_VDEs %u\n", GET16(meta, vdr->Populated_VDEs));
296 printf("Max_VDE_Supported %u\n", GET16(meta, vdr->Max_VDE_Supported));
297 for (j = 0; j < GET16(meta, vdr->Populated_VDEs); j++) {
298 if (isff(meta->vdr->entry[j].VD_GUID, 24))
301 print_guid(meta->vdr->entry[j].VD_GUID);
303 printf("VD_Number 0x%04x\n",
304 GET16(meta, vdr->entry[j].VD_Number));
305 printf("VD_Type 0x%04x\n",
306 GET16(meta, vdr->entry[j].VD_Type));
307 printf("VD_State 0x%02x\n",
308 GET8(meta, vdr->entry[j].VD_State));
309 printf("Init_State 0x%02x\n",
310 GET8(meta, vdr->entry[j].Init_State));
311 printf("Drive_Failures_Remaining %u\n",
312 GET8(meta, vdr->entry[j].Drive_Failures_Remaining));
313 printf("VD_Name '%.16s'\n",
314 (char *)&meta->vdr->entry[j].VD_Name);
316 printf("**** Configuration Records ****\n");
317 num = GETCRNUM(meta);
318 for (j = 0; j < num; j++) {
319 vdc = GETVDCPTR(meta, j);
320 val = GET32D(meta, vdc->Signature);
322 case DDF_VDCR_SIGNATURE:
323 printf("** Virtual Disk Configuration **\n");
325 print_guid(vdc->VD_GUID);
327 printf("Timestamp 0x%08x\n",
328 GET32D(meta, vdc->Timestamp));
329 printf("Sequence_Number 0x%08x\n",
330 GET32D(meta, vdc->Sequence_Number));
331 printf("Primary_Element_Count %u\n",
332 GET16D(meta, vdc->Primary_Element_Count));
333 printf("Stripe_Size %u\n",
334 GET8D(meta, vdc->Stripe_Size));
335 printf("Primary_RAID_Level 0x%02x\n",
336 GET8D(meta, vdc->Primary_RAID_Level));
337 printf("RLQ 0x%02x\n",
338 GET8D(meta, vdc->RLQ));
339 printf("Secondary_Element_Count %u\n",
340 GET8D(meta, vdc->Secondary_Element_Count));
341 printf("Secondary_Element_Seq %u\n",
342 GET8D(meta, vdc->Secondary_Element_Seq));
343 printf("Secondary_RAID_Level 0x%02x\n",
344 GET8D(meta, vdc->Secondary_RAID_Level));
345 printf("Block_Count %ju\n",
346 GET64D(meta, vdc->Block_Count));
347 printf("VD_Size %ju\n",
348 GET64D(meta, vdc->VD_Size));
349 printf("Block_Size %u\n",
350 GET16D(meta, vdc->Block_Size));
351 printf("Rotate_Parity_count %u\n",
352 GET8D(meta, vdc->Rotate_Parity_count));
353 printf("Associated_Spare_Disks");
354 for (i = 0; i < 8; i++) {
355 if (GET32D(meta, vdc->Associated_Spares[i]) != 0xffffffff)
356 printf(" 0x%08x", GET32D(meta, vdc->Associated_Spares[i]));
359 printf("Cache_Flags %016jx\n",
360 GET64D(meta, vdc->Cache_Flags));
361 printf("BG_Rate %u\n",
362 GET8D(meta, vdc->BG_Rate));
363 printf("MDF_Parity_Disks %u\n",
364 GET8D(meta, vdc->MDF_Parity_Disks));
365 printf("MDF_Parity_Generator_Polynomial 0x%04x\n",
366 GET16D(meta, vdc->MDF_Parity_Generator_Polynomial));
367 printf("MDF_Constant_Generation_Method 0x%02x\n",
368 GET8D(meta, vdc->MDF_Constant_Generation_Method));
369 printf("Physical_Disks ");
370 num2 = GET16D(meta, vdc->Primary_Element_Count);
371 val2 = (uint64_t *)&(vdc->Physical_Disk_Sequence[GET16(meta, hdr->Max_Primary_Element_Entries)]);
372 for (i = 0; i < num2; i++)
373 printf(" 0x%08x @ %ju",
374 GET32D(meta, vdc->Physical_Disk_Sequence[i]),
375 GET64P(meta, val2 + i));
378 case DDF_VUCR_SIGNATURE:
379 printf("** Vendor Unique Configuration **\n");
380 vuc = (struct ddf_vuc_record *)vdc;
382 print_guid(vuc->VD_GUID);
385 case DDF_SA_SIGNATURE:
386 printf("** Spare Assignment Configuration **\n");
387 sa = (struct ddf_sa_record *)vdc;
388 printf("Timestamp 0x%08x\n",
389 GET32D(meta, sa->Timestamp));
390 printf("Spare_Type 0x%02x\n",
391 GET8D(meta, sa->Spare_Type));
392 printf("Populated_SAEs %u\n",
393 GET16D(meta, sa->Populated_SAEs));
394 printf("MAX_SAE_Supported %u\n",
395 GET16D(meta, sa->MAX_SAE_Supported));
396 for (i = 0; i < GET16D(meta, sa->Populated_SAEs); i++) {
397 if (isff(sa->entry[i].VD_GUID, 24))
400 for (k = 0; k < 24; k++)
401 printf("%02x", sa->entry[i].VD_GUID[k]);
403 printf("Secondary_Element %u\n",
404 GET16D(meta, sa->entry[i].Secondary_Element));
411 printf("Unknown configuration signature %08x\n", val);
415 printf("**** Physical Disk Data ****\n");
417 print_guid(meta->pdd->PD_GUID);
419 printf("PD_Reference 0x%08x\n",
420 GET32(meta, pdd->PD_Reference));
421 printf("Forced_Ref_Flag 0x%02x\n",
422 GET8(meta, pdd->Forced_Ref_Flag));
423 printf("Forced_PD_GUID_Flag 0x%02x\n",
424 GET8(meta, pdd->Forced_PD_GUID_Flag));
428 ddf_meta_find_pd(struct ddf_meta *meta, uint8_t *GUID, uint32_t PD_Reference)
432 for (i = 0; i < GET16(meta, pdr->Populated_PDEs); i++) {
434 if (memcmp(meta->pdr->entry[i].PD_GUID, GUID, 24) == 0)
436 } else if (PD_Reference != 0xffffffff) {
437 if (GET32(meta, pdr->entry[i].PD_Reference) == PD_Reference)
440 if (isff(meta->pdr->entry[i].PD_GUID, 24))
443 if (GUID == NULL && PD_Reference == 0xffffffff) {
444 if (i >= GET16(meta, pdr->Max_PDE_Supported))
446 SET16(meta, pdr->Populated_PDEs, i + 1);
453 ddf_meta_find_vd(struct ddf_meta *meta, uint8_t *GUID)
457 for (i = 0; i < GET16(meta, vdr->Populated_VDEs); i++) {
459 if (memcmp(meta->vdr->entry[i].VD_GUID, GUID, 24) == 0)
462 if (isff(meta->vdr->entry[i].VD_GUID, 24))
466 if (i >= GET16(meta, vdr->Max_VDE_Supported))
468 SET16(meta, vdr->Populated_VDEs, i + 1);
474 static struct ddf_vdc_record *
475 ddf_meta_find_vdc(struct ddf_meta *meta, uint8_t *GUID)
477 struct ddf_vdc_record *vdc;
480 num = GETCRNUM(meta);
481 for (i = 0; i < num; i++) {
482 vdc = GETVDCPTR(meta, i);
484 if (GET32D(meta, vdc->Signature) == DDF_VDCR_SIGNATURE &&
485 memcmp(vdc->VD_GUID, GUID, 24) == 0)
488 if (GET32D(meta, vdc->Signature) == 0xffffffff ||
489 GET32D(meta, vdc->Signature) == 0)
496 ddf_meta_count_vdc(struct ddf_meta *meta, uint8_t *GUID)
498 struct ddf_vdc_record *vdc;
502 num = GETCRNUM(meta);
503 for (i = 0; i < num; i++) {
504 vdc = GETVDCPTR(meta, i);
505 if (GET32D(meta, vdc->Signature) != DDF_VDCR_SIGNATURE)
507 if (GUID == NULL || memcmp(vdc->VD_GUID, GUID, 24) == 0)
514 ddf_meta_find_disk(struct ddf_vol_meta *vmeta, uint32_t PD_Reference,
515 int *bvdp, int *posp)
520 for (bvd = 0; bvd < GET8(vmeta, vdc->Secondary_Element_Count); bvd++) {
521 if (vmeta->bvdc[bvd] == NULL) {
522 i += GET16(vmeta, vdc->Primary_Element_Count); // XXX
525 for (pos = 0; pos < GET16(vmeta, bvdc[bvd]->Primary_Element_Count);
527 if (GET32(vmeta, bvdc[bvd]->Physical_Disk_Sequence[pos]) ==
540 static struct ddf_sa_record *
541 ddf_meta_find_sa(struct ddf_meta *meta, int create)
543 struct ddf_sa_record *sa;
546 num = GETCRNUM(meta);
547 for (i = 0; i < num; i++) {
548 sa = GETSAPTR(meta, i);
549 if (GET32D(meta, sa->Signature) == DDF_SA_SIGNATURE)
553 for (i = 0; i < num; i++) {
554 sa = GETSAPTR(meta, i);
555 if (GET32D(meta, sa->Signature) == 0xffffffff ||
556 GET32D(meta, sa->Signature) == 0)
564 ddf_meta_create(struct g_raid_disk *disk, struct ddf_meta *sample)
568 struct g_raid_md_ddf_perdisk *pd;
569 struct g_raid_md_ddf_object *mdi;
570 struct ddf_meta *meta;
571 struct ddf_pd_entry *pde;
575 char serial_buffer[24];
577 if (sample->hdr == NULL)
580 mdi = (struct g_raid_md_ddf_object *)disk->d_softc->sc_md;
581 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
583 ss = disk->d_consumer->provider->sectorsize;
584 anchorlba = disk->d_consumer->provider->mediasize / ss - 1;
586 meta->sectorsize = ss;
587 meta->bigendian = sample ? sample->bigendian : mdi->mdio_bigendian;
589 clock_ts_to_ct(&ts, &ct);
592 meta->hdr = malloc(ss, M_MD_DDF, M_WAITOK);
593 memset(meta->hdr, 0xff, ss);
595 memcpy(meta->hdr, sample->hdr, sizeof(struct ddf_header));
596 if (ss != sample->sectorsize) {
597 SET32(meta, hdr->WorkSpace_Length,
598 howmany(GET32(sample, hdr->WorkSpace_Length) *
599 sample->sectorsize, ss));
600 SET16(meta, hdr->Configuration_Record_Length,
601 howmany(GET16(sample,
602 hdr->Configuration_Record_Length) *
603 sample->sectorsize, ss));
604 SET32(meta, hdr->cd_length,
605 howmany(GET32(sample, hdr->cd_length) *
606 sample->sectorsize, ss));
607 SET32(meta, hdr->pdr_length,
608 howmany(GET32(sample, hdr->pdr_length) *
609 sample->sectorsize, ss));
610 SET32(meta, hdr->vdr_length,
611 howmany(GET32(sample, hdr->vdr_length) *
612 sample->sectorsize, ss));
613 SET32(meta, hdr->cr_length,
614 howmany(GET32(sample, hdr->cr_length) *
615 sample->sectorsize, ss));
616 SET32(meta, hdr->pdd_length,
617 howmany(GET32(sample, hdr->pdd_length) *
618 sample->sectorsize, ss));
619 SET32(meta, hdr->bbmlog_length,
620 howmany(GET32(sample, hdr->bbmlog_length) *
621 sample->sectorsize, ss));
622 SET32(meta, hdr->Diagnostic_Space,
623 howmany(GET32(sample, hdr->bbmlog_length) *
624 sample->sectorsize, ss));
625 SET32(meta, hdr->Vendor_Specific_Logs,
626 howmany(GET32(sample, hdr->bbmlog_length) *
627 sample->sectorsize, ss));
630 SET32(meta, hdr->Signature, DDF_HEADER_SIGNATURE);
631 snprintf(meta->hdr->DDF_Header_GUID, 25, "FreeBSD %08x%08x",
632 (u_int)(ts.tv_sec - DECADE), arc4random());
633 memcpy(meta->hdr->DDF_rev, "02.00.00", 8);
634 SET32(meta, hdr->TimeStamp, (ts.tv_sec - DECADE));
635 SET32(meta, hdr->WorkSpace_Length, 16 * 1024 * 1024 / ss);
636 SET16(meta, hdr->Max_PD_Entries, DDF_MAX_DISKS - 1);
637 SET16(meta, hdr->Max_VD_Entries, DDF_MAX_VDISKS);
638 SET16(meta, hdr->Max_Partitions, DDF_MAX_PARTITIONS);
639 SET16(meta, hdr->Max_Primary_Element_Entries, DDF_MAX_DISKS);
640 SET16(meta, hdr->Configuration_Record_Length,
641 howmany(sizeof(struct ddf_vdc_record) + (4 + 8) *
642 GET16(meta, hdr->Max_Primary_Element_Entries), ss));
643 SET32(meta, hdr->cd_length,
644 howmany(sizeof(struct ddf_cd_record), ss));
645 SET32(meta, hdr->pdr_length,
646 howmany(sizeof(struct ddf_pd_record) +
647 sizeof(struct ddf_pd_entry) * GET16(meta,
648 hdr->Max_PD_Entries), ss));
649 SET32(meta, hdr->vdr_length,
650 howmany(sizeof(struct ddf_vd_record) +
651 sizeof(struct ddf_vd_entry) *
652 GET16(meta, hdr->Max_VD_Entries), ss));
653 SET32(meta, hdr->cr_length,
654 GET16(meta, hdr->Configuration_Record_Length) *
655 (GET16(meta, hdr->Max_Partitions) + 1));
656 SET32(meta, hdr->pdd_length,
657 howmany(sizeof(struct ddf_pdd_record), ss));
658 SET32(meta, hdr->bbmlog_length, 0);
659 SET32(meta, hdr->Diagnostic_Space_Length, 0);
660 SET32(meta, hdr->Vendor_Specific_Logs_Length, 0);
663 SET32(meta, hdr->cd_section, pos);
664 pos += GET32(meta, hdr->cd_length);
665 SET32(meta, hdr->pdr_section, pos);
666 pos += GET32(meta, hdr->pdr_length);
667 SET32(meta, hdr->vdr_section, pos);
668 pos += GET32(meta, hdr->vdr_length);
669 SET32(meta, hdr->cr_section, pos);
670 pos += GET32(meta, hdr->cr_length);
671 SET32(meta, hdr->pdd_section, pos);
672 pos += GET32(meta, hdr->pdd_length);
673 SET32(meta, hdr->bbmlog_section,
674 GET32(meta, hdr->bbmlog_length) != 0 ? pos : 0xffffffff);
675 pos += GET32(meta, hdr->bbmlog_length);
676 SET32(meta, hdr->Diagnostic_Space,
677 GET32(meta, hdr->Diagnostic_Space_Length) != 0 ? pos : 0xffffffff);
678 pos += GET32(meta, hdr->Diagnostic_Space_Length);
679 SET32(meta, hdr->Vendor_Specific_Logs,
680 GET32(meta, hdr->Vendor_Specific_Logs_Length) != 0 ? pos : 0xffffffff);
681 pos += min(GET32(meta, hdr->Vendor_Specific_Logs_Length), 1);
682 SET64(meta, hdr->Primary_Header_LBA,
684 SET64(meta, hdr->Secondary_Header_LBA,
685 0xffffffffffffffffULL);
686 SET64(meta, hdr->WorkSpace_LBA,
687 anchorlba + 1 - 32 * 1024 * 1024 / ss);
689 /* Controller Data */
690 size = GET32(meta, hdr->cd_length) * ss;
691 meta->cdr = malloc(size, M_MD_DDF, M_WAITOK);
692 memset(meta->cdr, 0xff, size);
693 SET32(meta, cdr->Signature, DDF_CONTROLLER_DATA_SIGNATURE);
694 memcpy(meta->cdr->Controller_GUID, "FreeBSD GEOM RAID SERIAL", 24);
695 memcpy(meta->cdr->Product_ID, "FreeBSD GEOMRAID", 16);
697 /* Physical Drive Records. */
698 size = GET32(meta, hdr->pdr_length) * ss;
699 meta->pdr = malloc(size, M_MD_DDF, M_WAITOK);
700 memset(meta->pdr, 0xff, size);
701 SET32(meta, pdr->Signature, DDF_PDR_SIGNATURE);
702 SET16(meta, pdr->Populated_PDEs, 1);
703 SET16(meta, pdr->Max_PDE_Supported,
704 GET16(meta, hdr->Max_PD_Entries));
706 pde = &meta->pdr->entry[0];
707 len = sizeof(serial_buffer);
708 error = g_io_getattr("GEOM::ident", disk->d_consumer, &len, serial_buffer);
709 if (error == 0 && (len = strlen (serial_buffer)) >= 6 && len <= 20)
710 snprintf(pde->PD_GUID, 25, "DISK%20s", serial_buffer);
712 snprintf(pde->PD_GUID, 25, "DISK%04d%02d%02d%08x%04x",
713 ct.year, ct.mon, ct.day,
714 arc4random(), arc4random() & 0xffff);
715 SET32D(meta, pde->PD_Reference, arc4random());
716 SET16D(meta, pde->PD_Type, DDF_PDE_GUID_FORCE);
717 SET16D(meta, pde->PD_State, 0);
718 SET64D(meta, pde->Configured_Size,
719 anchorlba + 1 - 32 * 1024 * 1024 / ss);
720 SET16D(meta, pde->Block_Size, ss);
722 /* Virtual Drive Records. */
723 size = GET32(meta, hdr->vdr_length) * ss;
724 meta->vdr = malloc(size, M_MD_DDF, M_WAITOK);
725 memset(meta->vdr, 0xff, size);
726 SET32(meta, vdr->Signature, DDF_VD_RECORD_SIGNATURE);
727 SET32(meta, vdr->Populated_VDEs, 0);
728 SET16(meta, vdr->Max_VDE_Supported,
729 GET16(meta, hdr->Max_VD_Entries));
731 /* Configuration Records. */
732 size = GET32(meta, hdr->cr_length) * ss;
733 meta->cr = malloc(size, M_MD_DDF, M_WAITOK);
734 memset(meta->cr, 0xff, size);
736 /* Physical Disk Data. */
737 size = GET32(meta, hdr->pdd_length) * ss;
738 meta->pdd = malloc(size, M_MD_DDF, M_WAITOK);
739 memset(meta->pdd, 0xff, size);
740 SET32(meta, pdd->Signature, DDF_PDD_SIGNATURE);
741 memcpy(meta->pdd->PD_GUID, pde->PD_GUID, 24);
742 SET32(meta, pdd->PD_Reference, GET32D(meta, pde->PD_Reference));
743 SET8(meta, pdd->Forced_Ref_Flag, DDF_PDD_FORCED_REF);
744 SET8(meta, pdd->Forced_PD_GUID_Flag, DDF_PDD_FORCED_GUID);
746 /* Bad Block Management Log. */
747 if (GET32(meta, hdr->bbmlog_length) != 0) {
748 size = GET32(meta, hdr->bbmlog_length) * ss;
749 meta->bbm = malloc(size, M_MD_DDF, M_WAITOK);
750 memset(meta->bbm, 0xff, size);
751 SET32(meta, bbm->Signature, DDF_BBML_SIGNATURE);
752 SET32(meta, bbm->Entry_Count, 0);
753 SET32(meta, bbm->Spare_Block_Count, 0);
758 ddf_meta_copy(struct ddf_meta *dst, struct ddf_meta *src)
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)
848 meta->bigendian = sample->bigendian;
849 ss = meta->sectorsize = sample->sectorsize;
850 meta->hdr = malloc(ss, M_MD_DDF, M_WAITOK);
851 memcpy(meta->hdr, sample->hdr, ss);
852 meta->cdr = malloc(GET32(sample, hdr->cd_length) * ss, M_MD_DDF, M_WAITOK);
853 memcpy(meta->cdr, sample->cdr, GET32(sample, hdr->cd_length) * ss);
854 meta->vde = malloc(sizeof(struct ddf_vd_entry), M_MD_DDF, M_WAITOK);
855 memset(meta->vde, 0xff, sizeof(struct ddf_vd_entry));
857 clock_ts_to_ct(&ts, &ct);
858 snprintf(meta->vde->VD_GUID, 25, "FreeBSD%04d%02d%02d%08x%01x",
859 ct.year, ct.mon, ct.day,
860 arc4random(), arc4random() & 0xf);
861 size = GET16(sample, hdr->Configuration_Record_Length) * ss;
862 meta->vdc = malloc(size, M_MD_DDF, M_WAITOK);
863 memset(meta->vdc, 0xff, size);
864 SET32(meta, vdc->Signature, DDF_VDCR_SIGNATURE);
865 memcpy(meta->vdc->VD_GUID, meta->vde->VD_GUID, 24);
866 SET32(meta, vdc->Sequence_Number, 0);
870 ddf_vol_meta_update(struct ddf_vol_meta *dst, struct ddf_meta *src,
871 uint8_t *GUID, int started)
873 struct ddf_vd_entry *vde;
874 struct ddf_vdc_record *vdc;
875 int vnew, bvnew, bvd, size;
878 vde = &src->vdr->entry[ddf_meta_find_vd(src, GUID)];
879 vdc = ddf_meta_find_vdc(src, GUID);
880 if (GET8D(src, vdc->Secondary_Element_Count) == 1)
883 bvd = GET8D(src, vdc->Secondary_Element_Seq);
884 size = GET16(src, hdr->Configuration_Record_Length) * src->sectorsize;
886 if (dst->vdc == NULL ||
887 (!started && ((int32_t)(GET32D(src, vdc->Sequence_Number) -
888 GET32(dst, vdc->Sequence_Number))) > 0))
893 if (dst->bvdc[bvd] == NULL ||
894 (!started && ((int32_t)(GET32D(src, vdc->Sequence_Number) -
895 GET32(dst, bvdc[bvd]->Sequence_Number))) > 0))
901 dst->bigendian = src->bigendian;
902 ss = dst->sectorsize = src->sectorsize;
903 if (dst->hdr != NULL)
904 free(dst->hdr, M_MD_DDF);
905 dst->hdr = malloc(ss, M_MD_DDF, M_WAITOK);
906 memcpy(dst->hdr, src->hdr, ss);
907 if (dst->cdr != NULL)
908 free(dst->cdr, M_MD_DDF);
909 dst->cdr = malloc(GET32(src, hdr->cd_length) * ss, M_MD_DDF, M_WAITOK);
910 memcpy(dst->cdr, src->cdr, GET32(src, hdr->cd_length) * ss);
911 if (dst->vde != NULL)
912 free(dst->vde, M_MD_DDF);
913 dst->vde = malloc(sizeof(struct ddf_vd_entry), M_MD_DDF, M_WAITOK);
914 memcpy(dst->vde, vde, sizeof(struct ddf_vd_entry));
915 if (dst->vdc != NULL)
916 free(dst->vdc, M_MD_DDF);
917 dst->vdc = malloc(size, M_MD_DDF, M_WAITOK);
918 memcpy(dst->vdc, vdc, size);
921 if (dst->bvdc[bvd] != NULL)
922 free(dst->bvdc[bvd], M_MD_DDF);
923 dst->bvdc[bvd] = malloc(size, M_MD_DDF, M_WAITOK);
924 memcpy(dst->bvdc[bvd], vdc, size);
929 ddf_vol_meta_free(struct ddf_vol_meta *meta)
933 if (meta->hdr != NULL) {
934 free(meta->hdr, M_MD_DDF);
937 if (meta->cdr != NULL) {
938 free(meta->cdr, M_MD_DDF);
941 if (meta->vde != NULL) {
942 free(meta->vde, M_MD_DDF);
945 if (meta->vdc != NULL) {
946 free(meta->vdc, M_MD_DDF);
949 for (i = 0; i < DDF_MAX_DISKS_HARD; i++) {
950 if (meta->bvdc[i] != NULL) {
951 free(meta->bvdc[i], M_MD_DDF);
952 meta->bvdc[i] = NULL;
958 ddf_meta_unused_range(struct ddf_meta *meta, off_t *off, off_t *size)
960 struct ddf_vdc_record *vdc;
961 off_t beg[32], end[32], beg1, end1;
963 int i, j, n, num, pos;
968 ref = GET32(meta, pdd->PD_Reference);
969 pos = ddf_meta_find_pd(meta, NULL, ref);
971 end[0] = GET64(meta, pdr->entry[pos].Configured_Size);
973 num = GETCRNUM(meta);
974 for (i = 0; i < num; i++) {
975 vdc = GETVDCPTR(meta, i);
976 if (GET32D(meta, vdc->Signature) != DDF_VDCR_SIGNATURE)
978 for (pos = 0; pos < GET16D(meta, vdc->Primary_Element_Count); pos++)
979 if (GET32D(meta, vdc->Physical_Disk_Sequence[pos]) == ref)
981 if (pos == GET16D(meta, vdc->Primary_Element_Count))
983 offp = (uint64_t *)&(vdc->Physical_Disk_Sequence[
984 GET16(meta, hdr->Max_Primary_Element_Entries)]);
985 beg1 = GET64P(meta, offp + pos);
986 end1 = beg1 + GET64D(meta, vdc->Block_Count);
987 for (j = 0; j < n; j++) {
988 if (beg[j] >= end1 || end[j] <= beg1 )
990 if (beg[j] < beg1 && end[j] > end1) {
995 } else if (beg[j] < beg1)
1001 for (j = 0; j < n; j++) {
1002 if (end[j] - beg[j] > *size) {
1004 *size = end[j] - beg[j];
1007 return ((*size > 0) ? 1 : 0);
1011 ddf_meta_get_name(struct ddf_meta *meta, int num, char *buf)
1016 b = meta->vdr->entry[num].VD_Name;
1017 for (i = 15; i >= 0; i--)
1020 memcpy(buf, b, i + 1);
1025 ddf_meta_put_name(struct ddf_vol_meta *meta, char *buf)
1029 len = min(strlen(buf), 16);
1030 memset(meta->vde->VD_Name, 0x20, 16);
1031 memcpy(meta->vde->VD_Name, buf, len);
1035 ddf_meta_read(struct g_consumer *cp, struct ddf_meta *meta)
1037 struct g_provider *pp;
1038 struct ddf_header *ahdr, *hdr;
1040 off_t plba, slba, lba;
1045 ddf_meta_free(meta);
1047 ss = meta->sectorsize = pp->sectorsize;
1048 /* Read anchor block. */
1049 abuf = g_read_data(cp, pp->mediasize - ss, ss, &error);
1051 G_RAID_DEBUG(1, "Cannot read metadata from %s (error=%d).",
1055 ahdr = (struct ddf_header *)abuf;
1057 /* Check if this is an DDF RAID struct */
1058 if (be32dec(&ahdr->Signature) == DDF_HEADER_SIGNATURE)
1059 meta->bigendian = 1;
1060 else if (le32dec(&ahdr->Signature) == DDF_HEADER_SIGNATURE)
1061 meta->bigendian = 0;
1063 G_RAID_DEBUG(1, "DDF signature check failed on %s", pp->name);
1067 if (ahdr->Header_Type != DDF_HEADER_ANCHOR) {
1068 G_RAID_DEBUG(1, "DDF header type check failed on %s", pp->name);
1073 plba = GET64(meta, hdr->Primary_Header_LBA);
1074 slba = GET64(meta, hdr->Secondary_Header_LBA);
1075 val = GET32(meta, hdr->CRC);
1076 SET32(meta, hdr->CRC, 0xffffffff);
1078 if (crc32(ahdr, ss) != val) {
1079 G_RAID_DEBUG(1, "DDF CRC mismatch on %s", pp->name);
1083 if ((plba + 6) * ss >= pp->mediasize) {
1084 G_RAID_DEBUG(1, "DDF primary header LBA is wrong on %s", pp->name);
1088 if (slba != -1 && (slba + 6) * ss >= pp->mediasize) {
1089 G_RAID_DEBUG(1, "DDF secondary header LBA is wrong on %s", pp->name);
1097 ddf_meta_free(meta);
1099 /* Read header block. */
1100 buf = g_read_data(cp, lba * ss, ss, &error);
1103 G_RAID_DEBUG(1, "DDF %s metadata read error on %s (error=%d).",
1104 (lba == plba) ? "primary" : "secondary", pp->name, error);
1105 if (lba == plba && slba != -1) {
1109 G_RAID_DEBUG(1, "DDF metadata read error on %s.", pp->name);
1112 meta->hdr = malloc(ss, M_MD_DDF, M_WAITOK);
1113 memcpy(meta->hdr, buf, ss);
1116 val = GET32(meta, hdr->CRC);
1117 SET32(meta, hdr->CRC, 0xffffffff);
1118 if (hdr->Signature != ahdr->Signature ||
1119 crc32(meta->hdr, ss) != val ||
1120 memcmp(hdr->DDF_Header_GUID, ahdr->DDF_Header_GUID, 24) ||
1121 GET64(meta, hdr->Primary_Header_LBA) != plba ||
1122 GET64(meta, hdr->Secondary_Header_LBA) != slba) {
1124 G_RAID_DEBUG(1, "DDF %s metadata check failed on %s",
1125 (lba == plba) ? "primary" : "secondary", pp->name);
1126 if (lba == plba && slba != -1) {
1130 G_RAID_DEBUG(1, "DDF metadata check failed on %s", pp->name);
1134 if ((lba == plba && hdr->Header_Type != DDF_HEADER_PRIMARY) ||
1135 (lba == slba && hdr->Header_Type != DDF_HEADER_SECONDARY))
1138 len = max(len, GET32(meta, hdr->cd_section) + GET32(meta, hdr->cd_length));
1139 len = max(len, GET32(meta, hdr->pdr_section) + GET32(meta, hdr->pdr_length));
1140 len = max(len, GET32(meta, hdr->vdr_section) + GET32(meta, hdr->vdr_length));
1141 len = max(len, GET32(meta, hdr->cr_section) + GET32(meta, hdr->cr_length));
1142 len = max(len, GET32(meta, hdr->pdd_section) + GET32(meta, hdr->pdd_length));
1143 if ((val = GET32(meta, hdr->bbmlog_section)) != 0xffffffff)
1144 len = max(len, val + GET32(meta, hdr->bbmlog_length));
1145 if ((val = GET32(meta, hdr->Diagnostic_Space)) != 0xffffffff)
1146 len = max(len, val + GET32(meta, hdr->Diagnostic_Space_Length));
1147 if ((val = GET32(meta, hdr->Vendor_Specific_Logs)) != 0xffffffff)
1148 len = max(len, val + GET32(meta, hdr->Vendor_Specific_Logs_Length));
1149 if ((plba + len) * ss >= pp->mediasize)
1151 if (slba != -1 && (slba + len) * ss >= pp->mediasize)
1153 /* Workaround for Adaptec implementation. */
1154 if (GET16(meta, hdr->Max_Primary_Element_Entries) == 0xffff) {
1155 SET16(meta, hdr->Max_Primary_Element_Entries,
1156 min(GET16(meta, hdr->Max_PD_Entries),
1157 (GET16(meta, hdr->Configuration_Record_Length) * ss - 512) / 12));
1160 if (GET32(meta, hdr->cd_length) * ss >= MAXPHYS ||
1161 GET32(meta, hdr->pdr_length) * ss >= MAXPHYS ||
1162 GET32(meta, hdr->vdr_length) * ss >= MAXPHYS ||
1163 GET32(meta, hdr->cr_length) * ss >= MAXPHYS ||
1164 GET32(meta, hdr->pdd_length) * ss >= MAXPHYS ||
1165 GET32(meta, hdr->bbmlog_length) * ss >= MAXPHYS) {
1166 G_RAID_DEBUG(1, "%s: Blocksize is too big.", pp->name);
1170 /* Read controller data. */
1171 buf = g_read_data(cp, (lba + GET32(meta, hdr->cd_section)) * ss,
1172 GET32(meta, hdr->cd_length) * ss, &error);
1175 meta->cdr = malloc(GET32(meta, hdr->cd_length) * ss, M_MD_DDF, M_WAITOK);
1176 memcpy(meta->cdr, buf, GET32(meta, hdr->cd_length) * ss);
1178 if (GET32(meta, cdr->Signature) != DDF_CONTROLLER_DATA_SIGNATURE)
1181 /* Read physical disk records. */
1182 buf = g_read_data(cp, (lba + GET32(meta, hdr->pdr_section)) * ss,
1183 GET32(meta, hdr->pdr_length) * ss, &error);
1186 meta->pdr = malloc(GET32(meta, hdr->pdr_length) * ss, M_MD_DDF, M_WAITOK);
1187 memcpy(meta->pdr, buf, GET32(meta, hdr->pdr_length) * ss);
1189 if (GET32(meta, pdr->Signature) != DDF_PDR_SIGNATURE)
1192 * Workaround for reading metadata corrupted due to graid bug.
1193 * XXX: Remove this before we have disks above 128PB. :)
1195 if (meta->bigendian) {
1196 for (i = 0; i < GET16(meta, pdr->Populated_PDEs); i++) {
1197 if (isff(meta->pdr->entry[i].PD_GUID, 24))
1199 if (GET32(meta, pdr->entry[i].PD_Reference) ==
1202 if (GET64(meta, pdr->entry[i].Configured_Size) >=
1204 SET16(meta, pdr->entry[i].PD_State,
1205 GET16(meta, pdr->entry[i].PD_State) &
1207 SET64(meta, pdr->entry[i].Configured_Size,
1208 GET64(meta, pdr->entry[i].Configured_Size) &
1209 ((1ULL << 48) - 1));
1214 /* Read virtual disk records. */
1215 buf = g_read_data(cp, (lba + GET32(meta, hdr->vdr_section)) * ss,
1216 GET32(meta, hdr->vdr_length) * ss, &error);
1219 meta->vdr = malloc(GET32(meta, hdr->vdr_length) * ss, M_MD_DDF, M_WAITOK);
1220 memcpy(meta->vdr, buf, GET32(meta, hdr->vdr_length) * ss);
1222 if (GET32(meta, vdr->Signature) != DDF_VD_RECORD_SIGNATURE)
1225 /* Read configuration records. */
1226 buf = g_read_data(cp, (lba + GET32(meta, hdr->cr_section)) * ss,
1227 GET32(meta, hdr->cr_length) * ss, &error);
1230 meta->cr = malloc(GET32(meta, hdr->cr_length) * ss, M_MD_DDF, M_WAITOK);
1231 memcpy(meta->cr, buf, GET32(meta, hdr->cr_length) * ss);
1234 /* Read physical disk data. */
1235 buf = g_read_data(cp, (lba + GET32(meta, hdr->pdd_section)) * ss,
1236 GET32(meta, hdr->pdd_length) * ss, &error);
1239 meta->pdd = malloc(GET32(meta, hdr->pdd_length) * ss, M_MD_DDF, M_WAITOK);
1240 memcpy(meta->pdd, buf, GET32(meta, hdr->pdd_length) * ss);
1242 if (GET32(meta, pdd->Signature) != DDF_PDD_SIGNATURE)
1244 i = ddf_meta_find_pd(meta, NULL, GET32(meta, pdd->PD_Reference));
1249 if (GET32(meta, hdr->bbmlog_section) != 0xffffffff &&
1250 GET32(meta, hdr->bbmlog_length) != 0) {
1251 buf = g_read_data(cp, (lba + GET32(meta, hdr->bbmlog_section)) * ss,
1252 GET32(meta, hdr->bbmlog_length) * ss, &error);
1255 meta->bbm = malloc(GET32(meta, hdr->bbmlog_length) * ss, M_MD_DDF, M_WAITOK);
1256 memcpy(meta->bbm, buf, GET32(meta, hdr->bbmlog_length) * ss);
1258 if (GET32(meta, bbm->Signature) != DDF_BBML_SIGNATURE)
1265 ddf_meta_free(meta);
1270 ddf_meta_write(struct g_consumer *cp, struct ddf_meta *meta)
1272 struct g_provider *pp;
1273 struct ddf_vdc_record *vdc;
1274 off_t alba, plba, slba, lba;
1279 ss = pp->sectorsize;
1280 lba = alba = pp->mediasize / ss - 1;
1281 plba = GET64(meta, hdr->Primary_Header_LBA);
1282 slba = GET64(meta, hdr->Secondary_Header_LBA);
1285 SET8(meta, hdr->Header_Type, (lba == alba) ? DDF_HEADER_ANCHOR :
1286 (lba == plba) ? DDF_HEADER_PRIMARY : DDF_HEADER_SECONDARY);
1287 SET32(meta, hdr->CRC, 0xffffffff);
1288 SET32(meta, hdr->CRC, crc32(meta->hdr, ss));
1289 error = g_write_data(cp, lba * ss, meta->hdr, ss);
1292 G_RAID_DEBUG(1, "Cannot write metadata to %s (error=%d).",
1302 size = GET32(meta, hdr->cd_length) * ss;
1303 SET32(meta, cdr->CRC, 0xffffffff);
1304 SET32(meta, cdr->CRC, crc32(meta->cdr, size));
1305 error = g_write_data(cp, (lba + GET32(meta, hdr->cd_section)) * ss,
1310 size = GET32(meta, hdr->pdr_length) * ss;
1311 SET32(meta, pdr->CRC, 0xffffffff);
1312 SET32(meta, pdr->CRC, crc32(meta->pdr, size));
1313 error = g_write_data(cp, (lba + GET32(meta, hdr->pdr_section)) * ss,
1318 size = GET32(meta, hdr->vdr_length) * ss;
1319 SET32(meta, vdr->CRC, 0xffffffff);
1320 SET32(meta, vdr->CRC, crc32(meta->vdr, size));
1321 error = g_write_data(cp, (lba + GET32(meta, hdr->vdr_section)) * ss,
1326 size = GET16(meta, hdr->Configuration_Record_Length) * ss;
1327 num = GETCRNUM(meta);
1328 for (i = 0; i < num; i++) {
1329 vdc = GETVDCPTR(meta, i);
1330 SET32D(meta, vdc->CRC, 0xffffffff);
1331 SET32D(meta, vdc->CRC, crc32(vdc, size));
1333 error = g_write_data(cp, (lba + GET32(meta, hdr->cr_section)) * ss,
1334 meta->cr, size * num);
1338 size = GET32(meta, hdr->pdd_length) * ss;
1339 SET32(meta, pdd->CRC, 0xffffffff);
1340 SET32(meta, pdd->CRC, crc32(meta->pdd, size));
1341 error = g_write_data(cp, (lba + GET32(meta, hdr->pdd_section)) * ss,
1346 if (GET32(meta, hdr->bbmlog_length) != 0) {
1347 size = GET32(meta, hdr->bbmlog_length) * ss;
1348 SET32(meta, bbm->CRC, 0xffffffff);
1349 SET32(meta, bbm->CRC, crc32(meta->bbm, size));
1350 error = g_write_data(cp,
1351 (lba + GET32(meta, hdr->bbmlog_section)) * ss,
1358 if (lba == plba && slba != -1) {
1367 ddf_meta_erase(struct g_consumer *cp)
1369 struct g_provider *pp;
1374 buf = malloc(pp->sectorsize, M_MD_DDF, M_WAITOK | M_ZERO);
1375 error = g_write_data(cp, pp->mediasize - pp->sectorsize,
1376 buf, pp->sectorsize);
1378 G_RAID_DEBUG(1, "Cannot erase metadata on %s (error=%d).",
1381 free(buf, M_MD_DDF);
1385 static struct g_raid_volume *
1386 g_raid_md_ddf_get_volume(struct g_raid_softc *sc, uint8_t *GUID)
1388 struct g_raid_volume *vol;
1389 struct g_raid_md_ddf_pervolume *pv;
1391 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
1392 pv = vol->v_md_data;
1393 if (memcmp(pv->pv_meta.vde->VD_GUID, GUID, 24) == 0)
1399 static struct g_raid_disk *
1400 g_raid_md_ddf_get_disk(struct g_raid_softc *sc, uint8_t *GUID, uint32_t id)
1402 struct g_raid_disk *disk;
1403 struct g_raid_md_ddf_perdisk *pd;
1404 struct ddf_meta *meta;
1406 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1407 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
1408 meta = &pd->pd_meta;
1410 if (memcmp(meta->pdd->PD_GUID, GUID, 24) == 0)
1413 if (GET32(meta, pdd->PD_Reference) == id)
1421 g_raid_md_ddf_purge_volumes(struct g_raid_softc *sc)
1423 struct g_raid_volume *vol, *tvol;
1427 TAILQ_FOREACH_SAFE(vol, &sc->sc_volumes, v_next, tvol) {
1428 if (vol->v_stopping)
1430 for (i = 0; i < vol->v_disks_count; i++) {
1431 if (vol->v_subdisks[i].sd_state != G_RAID_SUBDISK_S_NONE)
1434 if (i >= vol->v_disks_count) {
1435 g_raid_destroy_volume(vol);
1443 g_raid_md_ddf_purge_disks(struct g_raid_softc *sc)
1446 struct g_raid_disk *disk, *tdisk;
1447 struct g_raid_volume *vol;
1448 struct g_raid_md_ddf_perdisk *pd;
1452 TAILQ_FOREACH_SAFE(disk, &sc->sc_disks, d_next, tdisk) {
1453 if (disk->d_state == G_RAID_DISK_S_SPARE)
1455 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
1457 /* Scan for deleted volumes. */
1458 for (i = 0; i < pd->pd_subdisks; ) {
1459 vol = g_raid_md_ddf_get_volume(sc,
1460 pd->pd_meta[i]->volume_id);
1461 if (vol != NULL && !vol->v_stopping) {
1465 free(pd->pd_meta[i], M_MD_DDF);
1466 for (j = i; j < pd->pd_subdisks - 1; j++)
1467 pd->pd_meta[j] = pd->pd_meta[j + 1];
1468 pd->pd_meta[DDF_MAX_SUBDISKS - 1] = NULL;
1473 /* If there is no metadata left - erase and delete disk. */
1474 if (pd->pd_subdisks == 0) {
1475 ddf_meta_erase(disk->d_consumer);
1476 g_raid_destroy_disk(disk);
1486 g_raid_md_ddf_supported(int level, int qual, int disks, int force)
1489 if (disks > DDF_MAX_DISKS_HARD)
1492 case G_RAID_VOLUME_RL_RAID0:
1493 if (qual != G_RAID_VOLUME_RLQ_NONE)
1497 if (!force && disks < 2)
1500 case G_RAID_VOLUME_RL_RAID1:
1503 if (qual == G_RAID_VOLUME_RLQ_R1SM) {
1504 if (!force && disks != 2)
1506 } else if (qual == G_RAID_VOLUME_RLQ_R1MM) {
1507 if (!force && disks != 3)
1512 case G_RAID_VOLUME_RL_RAID3:
1513 if (qual != G_RAID_VOLUME_RLQ_R3P0 &&
1514 qual != G_RAID_VOLUME_RLQ_R3PN)
1519 case G_RAID_VOLUME_RL_RAID4:
1520 if (qual != G_RAID_VOLUME_RLQ_R4P0 &&
1521 qual != G_RAID_VOLUME_RLQ_R4PN)
1526 case G_RAID_VOLUME_RL_RAID5:
1527 if (qual != G_RAID_VOLUME_RLQ_R5RA &&
1528 qual != G_RAID_VOLUME_RLQ_R5RS &&
1529 qual != G_RAID_VOLUME_RLQ_R5LA &&
1530 qual != G_RAID_VOLUME_RLQ_R5LS)
1535 case G_RAID_VOLUME_RL_RAID6:
1536 if (qual != G_RAID_VOLUME_RLQ_R6RA &&
1537 qual != G_RAID_VOLUME_RLQ_R6RS &&
1538 qual != G_RAID_VOLUME_RLQ_R6LA &&
1539 qual != G_RAID_VOLUME_RLQ_R6LS)
1544 case G_RAID_VOLUME_RL_RAIDMDF:
1545 if (qual != G_RAID_VOLUME_RLQ_RMDFRA &&
1546 qual != G_RAID_VOLUME_RLQ_RMDFRS &&
1547 qual != G_RAID_VOLUME_RLQ_RMDFLA &&
1548 qual != G_RAID_VOLUME_RLQ_RMDFLS)
1553 case G_RAID_VOLUME_RL_RAID1E:
1554 if (qual != G_RAID_VOLUME_RLQ_R1EA &&
1555 qual != G_RAID_VOLUME_RLQ_R1EO)
1560 case G_RAID_VOLUME_RL_SINGLE:
1561 if (qual != G_RAID_VOLUME_RLQ_NONE)
1566 case G_RAID_VOLUME_RL_CONCAT:
1567 if (qual != G_RAID_VOLUME_RLQ_NONE)
1572 case G_RAID_VOLUME_RL_RAID5E:
1573 if (qual != G_RAID_VOLUME_RLQ_R5ERA &&
1574 qual != G_RAID_VOLUME_RLQ_R5ERS &&
1575 qual != G_RAID_VOLUME_RLQ_R5ELA &&
1576 qual != G_RAID_VOLUME_RLQ_R5ELS)
1581 case G_RAID_VOLUME_RL_RAID5EE:
1582 if (qual != G_RAID_VOLUME_RLQ_R5EERA &&
1583 qual != G_RAID_VOLUME_RLQ_R5EERS &&
1584 qual != G_RAID_VOLUME_RLQ_R5EELA &&
1585 qual != G_RAID_VOLUME_RLQ_R5EELS)
1590 case G_RAID_VOLUME_RL_RAID5R:
1591 if (qual != G_RAID_VOLUME_RLQ_R5RRA &&
1592 qual != G_RAID_VOLUME_RLQ_R5RRS &&
1593 qual != G_RAID_VOLUME_RLQ_R5RLA &&
1594 qual != G_RAID_VOLUME_RLQ_R5RLS)
1606 g_raid_md_ddf_start_disk(struct g_raid_disk *disk, struct g_raid_volume *vol)
1608 struct g_raid_softc *sc;
1609 struct g_raid_subdisk *sd;
1610 struct g_raid_md_ddf_perdisk *pd;
1611 struct g_raid_md_ddf_pervolume *pv;
1612 struct g_raid_md_ddf_object *mdi;
1613 struct ddf_vol_meta *vmeta;
1614 struct ddf_meta *pdmeta, *gmeta;
1615 struct ddf_vdc_record *vdc1;
1616 struct ddf_sa_record *sa;
1617 off_t size, eoff = 0, esize = 0;
1619 int disk_pos, md_disk_bvd = -1, md_disk_pos = -1, md_pde_pos;
1620 int i, resurrection = 0;
1624 mdi = (struct g_raid_md_ddf_object *)sc->sc_md;
1625 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
1626 pdmeta = &pd->pd_meta;
1627 reference = GET32(&pd->pd_meta, pdd->PD_Reference);
1629 pv = vol->v_md_data;
1630 vmeta = &pv->pv_meta;
1631 gmeta = &mdi->mdio_meta;
1633 /* Find disk position in metadata by its reference. */
1634 disk_pos = ddf_meta_find_disk(vmeta, reference,
1635 &md_disk_bvd, &md_disk_pos);
1636 md_pde_pos = ddf_meta_find_pd(gmeta, NULL, reference);
1639 G_RAID_DEBUG1(1, sc,
1640 "Disk %s is not a present part of the volume %s",
1641 g_raid_get_diskname(disk), vol->v_name);
1643 /* Failed stale disk is useless for us. */
1644 if ((GET16(gmeta, pdr->entry[md_pde_pos].PD_State) & DDF_PDE_PFA) != 0) {
1645 g_raid_change_disk_state(disk, G_RAID_DISK_S_STALE_FAILED);
1649 /* If disk has some metadata for this volume - erase. */
1650 if ((vdc1 = ddf_meta_find_vdc(pdmeta, vmeta->vdc->VD_GUID)) != NULL)
1651 SET32D(pdmeta, vdc1->Signature, 0xffffffff);
1653 /* If we are in the start process, that's all for now. */
1654 if (!pv->pv_started)
1657 * If we have already started - try to get use of the disk.
1658 * Try to replace OFFLINE disks first, then FAILED.
1660 if (ddf_meta_count_vdc(&pd->pd_meta, NULL) >=
1661 GET16(&pd->pd_meta, hdr->Max_Partitions)) {
1662 G_RAID_DEBUG1(1, sc, "No free partitions on disk %s",
1663 g_raid_get_diskname(disk));
1666 ddf_meta_unused_range(&pd->pd_meta, &eoff, &esize);
1668 G_RAID_DEBUG1(1, sc, "No free space on disk %s",
1669 g_raid_get_diskname(disk));
1672 eoff *= pd->pd_meta.sectorsize;
1673 esize *= pd->pd_meta.sectorsize;
1675 for (i = 0; i < vol->v_disks_count; i++) {
1676 sd = &vol->v_subdisks[i];
1677 if (sd->sd_state != G_RAID_SUBDISK_S_NONE)
1679 if (sd->sd_state <= G_RAID_SUBDISK_S_FAILED &&
1681 vol->v_subdisks[i].sd_state < sd->sd_state))
1684 if (disk_pos >= 0 &&
1685 vol->v_raid_level != G_RAID_VOLUME_RL_CONCAT &&
1687 G_RAID_DEBUG1(1, sc, "Disk %s free space "
1688 "is too small (%ju < %ju)",
1689 g_raid_get_diskname(disk), esize, size);
1692 if (disk_pos >= 0) {
1693 if (vol->v_raid_level != G_RAID_VOLUME_RL_CONCAT)
1695 md_disk_bvd = disk_pos / GET16(vmeta, vdc->Primary_Element_Count); // XXX
1696 md_disk_pos = disk_pos % GET16(vmeta, vdc->Primary_Element_Count); // XXX
1699 if (disk->d_state == G_RAID_DISK_S_NONE)
1700 g_raid_change_disk_state(disk,
1701 G_RAID_DISK_S_STALE);
1706 * If spare is committable, delete spare record.
1707 * Othersize, mark it active and leave there.
1709 sa = ddf_meta_find_sa(&pd->pd_meta, 0);
1711 if ((GET8D(&pd->pd_meta, sa->Spare_Type) &
1712 DDF_SAR_TYPE_REVERTIBLE) == 0) {
1713 SET32D(&pd->pd_meta, sa->Signature, 0xffffffff);
1715 SET8D(&pd->pd_meta, sa->Spare_Type,
1716 GET8D(&pd->pd_meta, sa->Spare_Type) |
1717 DDF_SAR_TYPE_ACTIVE);
1721 G_RAID_DEBUG1(1, sc, "Disk %s takes pos %d in the volume %s",
1722 g_raid_get_diskname(disk), disk_pos, vol->v_name);
1726 sd = &vol->v_subdisks[disk_pos];
1728 if (resurrection && sd->sd_disk != NULL) {
1729 g_raid_change_disk_state(sd->sd_disk,
1730 G_RAID_DISK_S_STALE_FAILED);
1731 TAILQ_REMOVE(&sd->sd_disk->d_subdisks,
1734 vol->v_subdisks[disk_pos].sd_disk = disk;
1735 TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
1737 /* Welcome the new disk. */
1739 g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE);
1740 else if (GET16(gmeta, pdr->entry[md_pde_pos].PD_State) & DDF_PDE_PFA)
1741 g_raid_change_disk_state(disk, G_RAID_DISK_S_FAILED);
1743 g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE);
1746 sd->sd_offset = eoff;
1747 sd->sd_size = esize;
1748 } else if (pdmeta->cr != NULL &&
1749 (vdc1 = ddf_meta_find_vdc(pdmeta, vmeta->vdc->VD_GUID)) != NULL) {
1750 val2 = (uint64_t *)&(vdc1->Physical_Disk_Sequence[GET16(vmeta, hdr->Max_Primary_Element_Entries)]);
1751 sd->sd_offset = (off_t)GET64P(pdmeta, val2 + md_disk_pos) * 512;
1752 sd->sd_size = (off_t)GET64D(pdmeta, vdc1->Block_Count) * 512;
1756 /* Stale disk, almost same as new. */
1757 g_raid_change_subdisk_state(sd,
1758 G_RAID_SUBDISK_S_NEW);
1759 } else if (GET16(gmeta, pdr->entry[md_pde_pos].PD_State) & DDF_PDE_PFA) {
1761 g_raid_change_subdisk_state(sd,
1762 G_RAID_SUBDISK_S_FAILED);
1763 } else if ((GET16(gmeta, pdr->entry[md_pde_pos].PD_State) &
1764 (DDF_PDE_FAILED | DDF_PDE_REBUILD)) != 0) {
1765 /* Rebuilding disk. */
1766 g_raid_change_subdisk_state(sd,
1767 G_RAID_SUBDISK_S_REBUILD);
1768 sd->sd_rebuild_pos = 0;
1769 } else if ((GET8(vmeta, vde->VD_State) & DDF_VDE_DIRTY) != 0 ||
1770 (GET8(vmeta, vde->Init_State) & DDF_VDE_INIT_MASK) !=
1771 DDF_VDE_INIT_FULL) {
1772 /* Stale disk or dirty volume (unclean shutdown). */
1773 g_raid_change_subdisk_state(sd,
1774 G_RAID_SUBDISK_S_STALE);
1776 /* Up to date disk. */
1777 g_raid_change_subdisk_state(sd,
1778 G_RAID_SUBDISK_S_ACTIVE);
1780 g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW,
1781 G_RAID_EVENT_SUBDISK);
1783 return (resurrection);
1787 g_raid_md_ddf_refill(struct g_raid_softc *sc)
1789 struct g_raid_volume *vol;
1790 struct g_raid_subdisk *sd;
1791 struct g_raid_disk *disk;
1792 struct g_raid_md_object *md;
1793 struct g_raid_md_ddf_perdisk *pd;
1794 struct g_raid_md_ddf_pervolume *pv;
1795 int update, updated, i, bad;
1800 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
1801 pv = vol->v_md_data;
1802 if (!pv->pv_started || vol->v_stopping)
1805 /* Search for subdisk that needs replacement. */
1807 for (i = 0; i < vol->v_disks_count; i++) {
1808 sd = &vol->v_subdisks[i];
1809 if (sd->sd_state == G_RAID_SUBDISK_S_NONE ||
1810 sd->sd_state == G_RAID_SUBDISK_S_FAILED)
1816 G_RAID_DEBUG1(1, sc, "Volume %s is not complete, "
1817 "trying to refill.", vol->v_name);
1819 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1821 if (disk->d_state < G_RAID_DISK_S_SPARE)
1823 /* Skip already used by this volume. */
1824 for (i = 0; i < vol->v_disks_count; i++) {
1825 sd = &vol->v_subdisks[i];
1826 if (sd->sd_disk == disk)
1829 if (i < vol->v_disks_count)
1832 /* Try to use disk if it has empty extents. */
1833 pd = disk->d_md_data;
1834 if (ddf_meta_count_vdc(&pd->pd_meta, NULL) <
1835 GET16(&pd->pd_meta, hdr->Max_Partitions)) {
1836 update = g_raid_md_ddf_start_disk(disk, vol);
1841 g_raid_md_write_ddf(md, vol, NULL, disk);
1851 g_raid_md_ddf_start(struct g_raid_volume *vol)
1853 struct g_raid_softc *sc;
1854 struct g_raid_subdisk *sd;
1855 struct g_raid_disk *disk;
1856 struct g_raid_md_object *md;
1857 struct g_raid_md_ddf_perdisk *pd;
1858 struct g_raid_md_ddf_pervolume *pv;
1859 struct g_raid_md_ddf_object *mdi;
1860 struct ddf_vol_meta *vmeta;
1866 mdi = (struct g_raid_md_ddf_object *)md;
1867 pv = vol->v_md_data;
1868 vmeta = &pv->pv_meta;
1870 vol->v_raid_level = GET8(vmeta, vdc->Primary_RAID_Level);
1871 vol->v_raid_level_qualifier = GET8(vmeta, vdc->RLQ);
1872 if (GET8(vmeta, vdc->Secondary_Element_Count) > 1 &&
1873 vol->v_raid_level == G_RAID_VOLUME_RL_RAID1 &&
1874 GET8(vmeta, vdc->Secondary_RAID_Level) == 0)
1875 vol->v_raid_level = G_RAID_VOLUME_RL_RAID1E;
1876 vol->v_sectorsize = GET16(vmeta, vdc->Block_Size);
1877 if (vol->v_sectorsize == 0xffff)
1878 vol->v_sectorsize = vmeta->sectorsize;
1879 vol->v_strip_size = vol->v_sectorsize << GET8(vmeta, vdc->Stripe_Size);
1880 vol->v_disks_count = GET16(vmeta, vdc->Primary_Element_Count) *
1881 GET8(vmeta, vdc->Secondary_Element_Count);
1882 vol->v_mdf_pdisks = GET8(vmeta, vdc->MDF_Parity_Disks);
1883 vol->v_mdf_polynomial = GET16(vmeta, vdc->MDF_Parity_Generator_Polynomial);
1884 vol->v_mdf_method = GET8(vmeta, vdc->MDF_Constant_Generation_Method);
1885 if (GET8(vmeta, vdc->Rotate_Parity_count) > 31)
1886 vol->v_rotate_parity = 1;
1888 vol->v_rotate_parity = 1 << GET8(vmeta, vdc->Rotate_Parity_count);
1889 vol->v_mediasize = GET64(vmeta, vdc->VD_Size) * vol->v_sectorsize;
1890 for (i = 0, j = 0, bvd = 0; i < vol->v_disks_count; i++, j++) {
1891 if (j == GET16(vmeta, vdc->Primary_Element_Count)) {
1895 sd = &vol->v_subdisks[i];
1896 if (vmeta->bvdc[bvd] == NULL) {
1898 sd->sd_size = GET64(vmeta, vdc->Block_Count) *
1902 val2 = (uint64_t *)&(vmeta->bvdc[bvd]->Physical_Disk_Sequence[
1903 GET16(vmeta, hdr->Max_Primary_Element_Entries)]);
1904 sd->sd_offset = GET64P(vmeta, val2 + j) * vol->v_sectorsize;
1905 sd->sd_size = GET64(vmeta, bvdc[bvd]->Block_Count) *
1908 g_raid_start_volume(vol);
1910 /* Make all disks found till the moment take their places. */
1911 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1912 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
1913 if (ddf_meta_find_vdc(&pd->pd_meta, vmeta->vdc->VD_GUID) != NULL)
1914 g_raid_md_ddf_start_disk(disk, vol);
1918 mdi->mdio_starting--;
1919 callout_stop(&pv->pv_start_co);
1920 G_RAID_DEBUG1(0, sc, "Volume started.");
1921 g_raid_md_write_ddf(md, vol, NULL, NULL);
1923 /* Pickup any STALE/SPARE disks to refill array if needed. */
1924 g_raid_md_ddf_refill(sc);
1926 g_raid_event_send(vol, G_RAID_VOLUME_E_START, G_RAID_EVENT_VOLUME);
1930 g_raid_ddf_go(void *arg)
1932 struct g_raid_volume *vol;
1933 struct g_raid_softc *sc;
1934 struct g_raid_md_ddf_pervolume *pv;
1937 pv = vol->v_md_data;
1939 if (!pv->pv_started) {
1940 G_RAID_DEBUG1(0, sc, "Force volume start due to timeout.");
1941 g_raid_event_send(vol, G_RAID_VOLUME_E_STARTMD,
1942 G_RAID_EVENT_VOLUME);
1947 g_raid_md_ddf_new_disk(struct g_raid_disk *disk)
1949 struct g_raid_softc *sc;
1950 struct g_raid_md_object *md;
1951 struct g_raid_md_ddf_perdisk *pd;
1952 struct g_raid_md_ddf_pervolume *pv;
1953 struct g_raid_md_ddf_object *mdi;
1954 struct g_raid_volume *vol;
1955 struct ddf_meta *pdmeta;
1956 struct ddf_vol_meta *vmeta;
1957 struct ddf_vdc_record *vdc;
1958 struct ddf_vd_entry *vde;
1959 int i, j, k, num, have, need, cnt, spare;
1965 mdi = (struct g_raid_md_ddf_object *)md;
1966 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
1967 pdmeta = &pd->pd_meta;
1970 if (mdi->mdio_meta.hdr == NULL)
1971 ddf_meta_copy(&mdi->mdio_meta, pdmeta);
1973 ddf_meta_update(&mdi->mdio_meta, pdmeta);
1975 num = GETCRNUM(pdmeta);
1976 for (j = 0; j < num; j++) {
1977 vdc = GETVDCPTR(pdmeta, j);
1978 val = GET32D(pdmeta, vdc->Signature);
1980 if (val == DDF_SA_SIGNATURE && spare == -1)
1983 if (val != DDF_VDCR_SIGNATURE)
1986 k = ddf_meta_find_vd(pdmeta, vdc->VD_GUID);
1989 vde = &pdmeta->vdr->entry[k];
1991 /* Look for volume with matching ID. */
1992 vol = g_raid_md_ddf_get_volume(sc, vdc->VD_GUID);
1994 ddf_meta_get_name(pdmeta, k, buf);
1995 vol = g_raid_create_volume(sc, buf,
1996 GET16D(pdmeta, vde->VD_Number));
1997 pv = malloc(sizeof(*pv), M_MD_DDF, M_WAITOK | M_ZERO);
1998 vol->v_md_data = pv;
1999 callout_init(&pv->pv_start_co, 1);
2000 callout_reset(&pv->pv_start_co,
2001 g_raid_start_timeout * hz,
2002 g_raid_ddf_go, vol);
2003 mdi->mdio_starting++;
2005 pv = vol->v_md_data;
2007 /* If we haven't started yet - check metadata freshness. */
2008 vmeta = &pv->pv_meta;
2009 ddf_vol_meta_update(vmeta, pdmeta, vdc->VD_GUID, pv->pv_started);
2013 g_raid_change_disk_state(disk, G_RAID_DISK_S_SPARE);
2014 g_raid_md_ddf_refill(sc);
2017 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2018 pv = vol->v_md_data;
2019 vmeta = &pv->pv_meta;
2021 if (ddf_meta_find_vdc(pdmeta, vmeta->vdc->VD_GUID) == NULL)
2024 if (pv->pv_started) {
2025 if (g_raid_md_ddf_start_disk(disk, vol))
2026 g_raid_md_write_ddf(md, vol, NULL, NULL);
2030 /* If we collected all needed disks - start array. */
2033 for (k = 0; k < GET8(vmeta, vdc->Secondary_Element_Count); k++) {
2034 if (vmeta->bvdc[k] == NULL) {
2035 need += GET16(vmeta, vdc->Primary_Element_Count);
2038 cnt = GET16(vmeta, bvdc[k]->Primary_Element_Count);
2040 for (i = 0; i < cnt; i++) {
2041 val = GET32(vmeta, bvdc[k]->Physical_Disk_Sequence[i]);
2042 if (g_raid_md_ddf_get_disk(sc, NULL, val) != NULL)
2046 G_RAID_DEBUG1(1, sc, "Volume %s now has %d of %d disks",
2047 vol->v_name, have, need);
2049 g_raid_md_ddf_start(vol);
2054 g_raid_md_create_req_ddf(struct g_raid_md_object *md, struct g_class *mp,
2055 struct gctl_req *req, struct g_geom **gp)
2057 struct g_geom *geom;
2058 struct g_raid_softc *sc;
2059 struct g_raid_md_ddf_object *mdi, *mdi1;
2064 mdi = (struct g_raid_md_ddf_object *)md;
2065 fmtopt = gctl_get_asciiparam(req, "fmtopt");
2066 if (fmtopt == NULL || strcasecmp(fmtopt, "BE") == 0)
2068 else if (strcasecmp(fmtopt, "LE") == 0)
2071 gctl_error(req, "Incorrect fmtopt argument.");
2072 return (G_RAID_MD_TASTE_FAIL);
2075 /* Search for existing node. */
2076 LIST_FOREACH(geom, &mp->geom, geom) {
2080 if (sc->sc_stopping != 0)
2082 if (sc->sc_md->mdo_class != md->mdo_class)
2084 mdi1 = (struct g_raid_md_ddf_object *)sc->sc_md;
2085 if (mdi1->mdio_bigendian != be)
2091 return (G_RAID_MD_TASTE_EXISTING);
2094 /* Create new one if not found. */
2095 mdi->mdio_bigendian = be;
2096 snprintf(name, sizeof(name), "DDF%s", be ? "" : "-LE");
2097 sc = g_raid_create_node(mp, name, md);
2099 return (G_RAID_MD_TASTE_FAIL);
2102 return (G_RAID_MD_TASTE_NEW);
2106 g_raid_md_taste_ddf(struct g_raid_md_object *md, struct g_class *mp,
2107 struct g_consumer *cp, struct g_geom **gp)
2109 struct g_consumer *rcp;
2110 struct g_provider *pp;
2111 struct g_raid_softc *sc;
2112 struct g_raid_disk *disk;
2113 struct ddf_meta meta;
2114 struct g_raid_md_ddf_perdisk *pd;
2115 struct g_raid_md_ddf_object *mdi;
2116 struct g_geom *geom;
2117 int error, result, be;
2120 G_RAID_DEBUG(1, "Tasting DDF on %s", cp->provider->name);
2121 mdi = (struct g_raid_md_ddf_object *)md;
2124 /* Read metadata from device. */
2125 g_topology_unlock();
2126 bzero(&meta, sizeof(meta));
2127 error = ddf_meta_read(cp, &meta);
2130 return (G_RAID_MD_TASTE_FAIL);
2131 be = meta.bigendian;
2133 /* Metadata valid. Print it. */
2134 g_raid_md_ddf_print(&meta);
2136 /* Search for matching node. */
2138 LIST_FOREACH(geom, &mp->geom, geom) {
2142 if (sc->sc_stopping != 0)
2144 if (sc->sc_md->mdo_class != md->mdo_class)
2146 mdi = (struct g_raid_md_ddf_object *)sc->sc_md;
2147 if (mdi->mdio_bigendian != be)
2152 /* Found matching node. */
2154 G_RAID_DEBUG(1, "Found matching array %s", sc->sc_name);
2155 result = G_RAID_MD_TASTE_EXISTING;
2157 } else { /* Not found matching node -- create one. */
2158 result = G_RAID_MD_TASTE_NEW;
2159 mdi->mdio_bigendian = be;
2160 snprintf(name, sizeof(name), "DDF%s", be ? "" : "-LE");
2161 sc = g_raid_create_node(mp, name, md);
2166 /* There is no return after this point, so we close passed consumer. */
2167 g_access(cp, -1, 0, 0);
2169 rcp = g_new_consumer(geom);
2170 rcp->flags |= G_CF_DIRECT_RECEIVE;
2172 if (g_access(rcp, 1, 1, 1) != 0)
2175 g_topology_unlock();
2176 sx_xlock(&sc->sc_lock);
2178 pd = malloc(sizeof(*pd), M_MD_DDF, M_WAITOK | M_ZERO);
2180 disk = g_raid_create_disk(sc);
2181 disk->d_md_data = (void *)pd;
2182 disk->d_consumer = rcp;
2183 rcp->private = disk;
2185 g_raid_get_disk_info(disk);
2187 g_raid_md_ddf_new_disk(disk);
2189 sx_xunlock(&sc->sc_lock);
2196 g_raid_md_event_ddf(struct g_raid_md_object *md,
2197 struct g_raid_disk *disk, u_int event)
2199 struct g_raid_softc *sc;
2205 case G_RAID_DISK_E_DISCONNECTED:
2207 g_raid_change_disk_state(disk, G_RAID_DISK_S_NONE);
2208 g_raid_destroy_disk(disk);
2209 g_raid_md_ddf_purge_volumes(sc);
2211 /* Write updated metadata to all disks. */
2212 g_raid_md_write_ddf(md, NULL, NULL, NULL);
2214 /* Check if anything left. */
2215 if (g_raid_ndisks(sc, -1) == 0)
2216 g_raid_destroy_node(sc, 0);
2218 g_raid_md_ddf_refill(sc);
2225 g_raid_md_volume_event_ddf(struct g_raid_md_object *md,
2226 struct g_raid_volume *vol, u_int event)
2228 struct g_raid_md_ddf_pervolume *pv;
2230 pv = (struct g_raid_md_ddf_pervolume *)vol->v_md_data;
2232 case G_RAID_VOLUME_E_STARTMD:
2233 if (!pv->pv_started)
2234 g_raid_md_ddf_start(vol);
2241 g_raid_md_ctl_ddf(struct g_raid_md_object *md,
2242 struct gctl_req *req)
2244 struct g_raid_softc *sc;
2245 struct g_raid_volume *vol, *vol1;
2246 struct g_raid_subdisk *sd;
2247 struct g_raid_disk *disk, *disks[DDF_MAX_DISKS_HARD];
2248 struct g_raid_md_ddf_perdisk *pd;
2249 struct g_raid_md_ddf_pervolume *pv;
2250 struct g_raid_md_ddf_object *mdi;
2251 struct ddf_sa_record *sa;
2252 struct g_consumer *cp;
2253 struct g_provider *pp;
2255 const char *nodename, *verb, *volname, *levelname, *diskname;
2258 off_t size, sectorsize, strip, offs[DDF_MAX_DISKS_HARD], esize;
2259 intmax_t *sizearg, *striparg;
2260 int i, numdisks, len, level, qual;
2264 mdi = (struct g_raid_md_ddf_object *)md;
2265 verb = gctl_get_param(req, "verb", NULL);
2266 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
2269 if (strcmp(verb, "label") == 0) {
2272 gctl_error(req, "Invalid number of arguments.");
2275 volname = gctl_get_asciiparam(req, "arg1");
2276 if (volname == NULL) {
2277 gctl_error(req, "No volume name.");
2280 levelname = gctl_get_asciiparam(req, "arg2");
2281 if (levelname == NULL) {
2282 gctl_error(req, "No RAID level.");
2285 if (g_raid_volume_str2level(levelname, &level, &qual)) {
2286 gctl_error(req, "Unknown RAID level '%s'.", levelname);
2289 numdisks = *nargs - 3;
2290 force = gctl_get_paraml(req, "force", sizeof(*force));
2291 if (!g_raid_md_ddf_supported(level, qual, numdisks,
2292 force ? *force : 0)) {
2293 gctl_error(req, "Unsupported RAID level "
2294 "(0x%02x/0x%02x), or number of disks (%d).",
2295 level, qual, numdisks);
2299 /* Search for disks, connect them and probe. */
2302 bzero(disks, sizeof(disks));
2303 bzero(offs, sizeof(offs));
2304 for (i = 0; i < numdisks; i++) {
2305 snprintf(arg, sizeof(arg), "arg%d", i + 3);
2306 diskname = gctl_get_asciiparam(req, arg);
2307 if (diskname == NULL) {
2308 gctl_error(req, "No disk name (%s).", arg);
2312 if (strcmp(diskname, "NONE") == 0)
2315 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2316 if (disk->d_consumer != NULL &&
2317 disk->d_consumer->provider != NULL &&
2318 strcmp(disk->d_consumer->provider->name,
2323 if (disk->d_state != G_RAID_DISK_S_ACTIVE) {
2324 gctl_error(req, "Disk '%s' is in a "
2325 "wrong state (%s).", diskname,
2326 g_raid_disk_state2str(disk->d_state));
2330 pd = disk->d_md_data;
2331 if (ddf_meta_count_vdc(&pd->pd_meta, NULL) >=
2332 GET16(&pd->pd_meta, hdr->Max_Partitions)) {
2333 gctl_error(req, "No free partitions "
2339 pp = disk->d_consumer->provider;
2341 ddf_meta_unused_range(&pd->pd_meta,
2343 offs[i] *= pp->sectorsize;
2344 size = MIN(size, (off_t)esize * pp->sectorsize);
2345 sectorsize = MAX(sectorsize, pp->sectorsize);
2350 cp = g_raid_open_consumer(sc, diskname);
2352 gctl_error(req, "Can't open disk '%s'.",
2354 g_topology_unlock();
2359 pd = malloc(sizeof(*pd), M_MD_DDF, M_WAITOK | M_ZERO);
2360 disk = g_raid_create_disk(sc);
2361 disk->d_md_data = (void *)pd;
2362 disk->d_consumer = cp;
2365 ddf_meta_create(disk, &mdi->mdio_meta);
2366 if (mdi->mdio_meta.hdr == NULL)
2367 ddf_meta_copy(&mdi->mdio_meta, &pd->pd_meta);
2369 ddf_meta_update(&mdi->mdio_meta, &pd->pd_meta);
2370 g_topology_unlock();
2372 g_raid_get_disk_info(disk);
2374 /* Reserve some space for metadata. */
2375 size = MIN(size, GET64(&pd->pd_meta,
2376 pdr->entry[0].Configured_Size) * pp->sectorsize);
2377 sectorsize = MAX(sectorsize, pp->sectorsize);
2380 for (i = 0; i < numdisks; i++) {
2381 if (disks[i] != NULL &&
2382 disks[i]->d_state == G_RAID_DISK_S_NONE)
2383 g_raid_destroy_disk(disks[i]);
2388 if (sectorsize <= 0) {
2389 gctl_error(req, "Can't get sector size.");
2393 /* Handle size argument. */
2394 len = sizeof(*sizearg);
2395 sizearg = gctl_get_param(req, "size", &len);
2396 if (sizearg != NULL && len == sizeof(*sizearg) &&
2398 if (*sizearg > size) {
2399 gctl_error(req, "Size too big %lld > %lld.",
2400 (long long)*sizearg, (long long)size);
2406 /* Handle strip argument. */
2408 len = sizeof(*striparg);
2409 striparg = gctl_get_param(req, "strip", &len);
2410 if (striparg != NULL && len == sizeof(*striparg) &&
2412 if (*striparg < sectorsize) {
2413 gctl_error(req, "Strip size too small.");
2416 if (*striparg % sectorsize != 0) {
2417 gctl_error(req, "Incorrect strip size.");
2423 /* Round size down to strip or sector. */
2424 if (level == G_RAID_VOLUME_RL_RAID1 ||
2425 level == G_RAID_VOLUME_RL_RAID3 ||
2426 level == G_RAID_VOLUME_RL_SINGLE ||
2427 level == G_RAID_VOLUME_RL_CONCAT)
2428 size -= (size % sectorsize);
2429 else if (level == G_RAID_VOLUME_RL_RAID1E &&
2430 (numdisks & 1) != 0)
2431 size -= (size % (2 * strip));
2433 size -= (size % strip);
2435 gctl_error(req, "Size too small.");
2439 /* We have all we need, create things: volume, ... */
2440 pv = malloc(sizeof(*pv), M_MD_DDF, M_WAITOK | M_ZERO);
2441 ddf_vol_meta_create(&pv->pv_meta, &mdi->mdio_meta);
2443 vol = g_raid_create_volume(sc, volname, -1);
2444 vol->v_md_data = pv;
2445 vol->v_raid_level = level;
2446 vol->v_raid_level_qualifier = qual;
2447 vol->v_strip_size = strip;
2448 vol->v_disks_count = numdisks;
2449 if (level == G_RAID_VOLUME_RL_RAID0 ||
2450 level == G_RAID_VOLUME_RL_CONCAT ||
2451 level == G_RAID_VOLUME_RL_SINGLE)
2452 vol->v_mediasize = size * numdisks;
2453 else if (level == G_RAID_VOLUME_RL_RAID1)
2454 vol->v_mediasize = size;
2455 else if (level == G_RAID_VOLUME_RL_RAID3 ||
2456 level == G_RAID_VOLUME_RL_RAID4 ||
2457 level == G_RAID_VOLUME_RL_RAID5)
2458 vol->v_mediasize = size * (numdisks - 1);
2459 else if (level == G_RAID_VOLUME_RL_RAID5R) {
2460 vol->v_mediasize = size * (numdisks - 1);
2461 vol->v_rotate_parity = 1024;
2462 } else if (level == G_RAID_VOLUME_RL_RAID6 ||
2463 level == G_RAID_VOLUME_RL_RAID5E ||
2464 level == G_RAID_VOLUME_RL_RAID5EE)
2465 vol->v_mediasize = size * (numdisks - 2);
2466 else if (level == G_RAID_VOLUME_RL_RAIDMDF) {
2468 vol->v_mdf_pdisks = 2;
2470 vol->v_mdf_pdisks = 3;
2471 vol->v_mdf_polynomial = 0x11d;
2472 vol->v_mdf_method = 0x00;
2473 vol->v_mediasize = size * (numdisks - vol->v_mdf_pdisks);
2474 } else { /* RAID1E */
2475 vol->v_mediasize = ((size * numdisks) / strip / 2) *
2478 vol->v_sectorsize = sectorsize;
2479 g_raid_start_volume(vol);
2481 /* , and subdisks. */
2482 for (i = 0; i < numdisks; i++) {
2484 sd = &vol->v_subdisks[i];
2486 sd->sd_offset = offs[i];
2490 TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
2491 g_raid_change_disk_state(disk,
2492 G_RAID_DISK_S_ACTIVE);
2493 g_raid_change_subdisk_state(sd,
2494 G_RAID_SUBDISK_S_ACTIVE);
2495 g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW,
2496 G_RAID_EVENT_SUBDISK);
2499 /* Write metadata based on created entities. */
2500 G_RAID_DEBUG1(0, sc, "Array started.");
2501 g_raid_md_write_ddf(md, vol, NULL, NULL);
2503 /* Pickup any STALE/SPARE disks to refill array if needed. */
2504 g_raid_md_ddf_refill(sc);
2506 g_raid_event_send(vol, G_RAID_VOLUME_E_START,
2507 G_RAID_EVENT_VOLUME);
2510 if (strcmp(verb, "add") == 0) {
2512 gctl_error(req, "`add` command is not applicable, "
2513 "use `label` instead.");
2516 if (strcmp(verb, "delete") == 0) {
2518 nodename = gctl_get_asciiparam(req, "arg0");
2519 if (nodename != NULL && strcasecmp(sc->sc_name, nodename) != 0)
2522 /* Full node destruction. */
2523 if (*nargs == 1 && nodename != NULL) {
2524 /* Check if some volume is still open. */
2525 force = gctl_get_paraml(req, "force", sizeof(*force));
2526 if (force != NULL && *force == 0 &&
2527 g_raid_nopens(sc) != 0) {
2528 gctl_error(req, "Some volume is still open.");
2532 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2533 if (disk->d_consumer)
2534 ddf_meta_erase(disk->d_consumer);
2536 g_raid_destroy_node(sc, 0);
2540 /* Destroy specified volume. If it was last - all node. */
2542 gctl_error(req, "Invalid number of arguments.");
2545 volname = gctl_get_asciiparam(req,
2546 nodename != NULL ? "arg1" : "arg0");
2547 if (volname == NULL) {
2548 gctl_error(req, "No volume name.");
2552 /* Search for volume. */
2553 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2554 if (strcmp(vol->v_name, volname) == 0)
2556 pp = vol->v_provider;
2559 if (strcmp(pp->name, volname) == 0)
2561 if (strncmp(pp->name, "raid/", 5) == 0 &&
2562 strcmp(pp->name + 5, volname) == 0)
2566 i = strtol(volname, &tmp, 10);
2567 if (verb != volname && tmp[0] == 0) {
2568 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2569 if (vol->v_global_id == i)
2575 gctl_error(req, "Volume '%s' not found.", volname);
2579 /* Check if volume is still open. */
2580 force = gctl_get_paraml(req, "force", sizeof(*force));
2581 if (force != NULL && *force == 0 &&
2582 vol->v_provider_open != 0) {
2583 gctl_error(req, "Volume is still open.");
2587 /* Destroy volume and potentially node. */
2589 TAILQ_FOREACH(vol1, &sc->sc_volumes, v_next)
2592 g_raid_destroy_volume(vol);
2593 g_raid_md_ddf_purge_disks(sc);
2594 g_raid_md_write_ddf(md, NULL, NULL, NULL);
2596 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2597 if (disk->d_consumer)
2598 ddf_meta_erase(disk->d_consumer);
2600 g_raid_destroy_node(sc, 0);
2604 if (strcmp(verb, "remove") == 0 ||
2605 strcmp(verb, "fail") == 0) {
2607 gctl_error(req, "Invalid number of arguments.");
2610 for (i = 1; i < *nargs; i++) {
2611 snprintf(arg, sizeof(arg), "arg%d", i);
2612 diskname = gctl_get_asciiparam(req, arg);
2613 if (diskname == NULL) {
2614 gctl_error(req, "No disk name (%s).", arg);
2618 if (strncmp(diskname, "/dev/", 5) == 0)
2621 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2622 if (disk->d_consumer != NULL &&
2623 disk->d_consumer->provider != NULL &&
2624 strcmp(disk->d_consumer->provider->name,
2629 gctl_error(req, "Disk '%s' not found.",
2635 if (strcmp(verb, "fail") == 0) {
2636 g_raid_md_fail_disk_ddf(md, NULL, disk);
2640 /* Erase metadata on deleting disk and destroy it. */
2641 ddf_meta_erase(disk->d_consumer);
2642 g_raid_destroy_disk(disk);
2644 g_raid_md_ddf_purge_volumes(sc);
2646 /* Write updated metadata to remaining disks. */
2647 g_raid_md_write_ddf(md, NULL, NULL, NULL);
2649 /* Check if anything left. */
2650 if (g_raid_ndisks(sc, -1) == 0)
2651 g_raid_destroy_node(sc, 0);
2653 g_raid_md_ddf_refill(sc);
2656 if (strcmp(verb, "insert") == 0) {
2658 gctl_error(req, "Invalid number of arguments.");
2661 for (i = 1; i < *nargs; i++) {
2662 /* Get disk name. */
2663 snprintf(arg, sizeof(arg), "arg%d", i);
2664 diskname = gctl_get_asciiparam(req, arg);
2665 if (diskname == NULL) {
2666 gctl_error(req, "No disk name (%s).", arg);
2671 /* Try to find provider with specified name. */
2673 cp = g_raid_open_consumer(sc, diskname);
2675 gctl_error(req, "Can't open disk '%s'.",
2677 g_topology_unlock();
2682 g_topology_unlock();
2684 pd = malloc(sizeof(*pd), M_MD_DDF, M_WAITOK | M_ZERO);
2686 disk = g_raid_create_disk(sc);
2687 disk->d_consumer = cp;
2688 disk->d_md_data = (void *)pd;
2691 g_raid_get_disk_info(disk);
2693 /* Welcome the "new" disk. */
2694 g_raid_change_disk_state(disk, G_RAID_DISK_S_SPARE);
2695 ddf_meta_create(disk, &mdi->mdio_meta);
2696 sa = ddf_meta_find_sa(&pd->pd_meta, 1);
2698 SET32D(&pd->pd_meta, sa->Signature,
2700 SET8D(&pd->pd_meta, sa->Spare_Type, 0);
2701 SET16D(&pd->pd_meta, sa->Populated_SAEs, 0);
2702 SET16D(&pd->pd_meta, sa->MAX_SAE_Supported,
2703 (GET16(&pd->pd_meta, hdr->Configuration_Record_Length) *
2704 pd->pd_meta.sectorsize -
2705 sizeof(struct ddf_sa_record)) /
2706 sizeof(struct ddf_sa_entry));
2708 if (mdi->mdio_meta.hdr == NULL)
2709 ddf_meta_copy(&mdi->mdio_meta, &pd->pd_meta);
2711 ddf_meta_update(&mdi->mdio_meta, &pd->pd_meta);
2712 g_raid_md_write_ddf(md, NULL, NULL, NULL);
2713 g_raid_md_ddf_refill(sc);
2721 g_raid_md_write_ddf(struct g_raid_md_object *md, struct g_raid_volume *tvol,
2722 struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk)
2724 struct g_raid_softc *sc;
2725 struct g_raid_volume *vol;
2726 struct g_raid_subdisk *sd;
2727 struct g_raid_disk *disk;
2728 struct g_raid_md_ddf_perdisk *pd;
2729 struct g_raid_md_ddf_pervolume *pv;
2730 struct g_raid_md_ddf_object *mdi;
2731 struct ddf_meta *gmeta;
2732 struct ddf_vol_meta *vmeta;
2733 struct ddf_vdc_record *vdc;
2734 struct ddf_sa_record *sa;
2736 int i, j, pos, bvd, size;
2739 mdi = (struct g_raid_md_ddf_object *)md;
2740 gmeta = &mdi->mdio_meta;
2742 if (sc->sc_stopping == G_RAID_DESTROY_HARD)
2746 * Clear disk flags to let only really needed ones to be reset.
2747 * Do it only if there are no volumes in starting state now,
2748 * as they can update disk statuses yet and we may kill innocent.
2750 if (mdi->mdio_starting == 0) {
2751 for (i = 0; i < GET16(gmeta, pdr->Populated_PDEs); i++) {
2752 if (isff(gmeta->pdr->entry[i].PD_GUID, 24))
2754 SET16(gmeta, pdr->entry[i].PD_Type,
2755 GET16(gmeta, pdr->entry[i].PD_Type) &
2756 ~(DDF_PDE_PARTICIPATING |
2757 DDF_PDE_GLOBAL_SPARE | DDF_PDE_CONFIG_SPARE));
2758 if ((GET16(gmeta, pdr->entry[i].PD_State) &
2760 SET16(gmeta, pdr->entry[i].PD_State, 0);
2764 /* Generate/update new per-volume metadata. */
2765 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2766 pv = (struct g_raid_md_ddf_pervolume *)vol->v_md_data;
2767 if (vol->v_stopping || !pv->pv_started)
2769 vmeta = &pv->pv_meta;
2771 SET32(vmeta, vdc->Sequence_Number,
2772 GET32(vmeta, vdc->Sequence_Number) + 1);
2773 if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E &&
2774 vol->v_disks_count % 2 == 0)
2775 SET16(vmeta, vdc->Primary_Element_Count, 2);
2777 SET16(vmeta, vdc->Primary_Element_Count,
2778 vol->v_disks_count);
2779 SET8(vmeta, vdc->Stripe_Size,
2780 ffs(vol->v_strip_size / vol->v_sectorsize) - 1);
2781 if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E &&
2782 vol->v_disks_count % 2 == 0) {
2783 SET8(vmeta, vdc->Primary_RAID_Level,
2785 SET8(vmeta, vdc->RLQ, 0);
2786 SET8(vmeta, vdc->Secondary_Element_Count,
2787 vol->v_disks_count / 2);
2788 SET8(vmeta, vdc->Secondary_RAID_Level, 0);
2790 SET8(vmeta, vdc->Primary_RAID_Level,
2792 SET8(vmeta, vdc->RLQ,
2793 vol->v_raid_level_qualifier);
2794 SET8(vmeta, vdc->Secondary_Element_Count, 1);
2795 SET8(vmeta, vdc->Secondary_RAID_Level, 0);
2797 SET8(vmeta, vdc->Secondary_Element_Seq, 0);
2798 SET64(vmeta, vdc->Block_Count, 0);
2799 SET64(vmeta, vdc->VD_Size, vol->v_mediasize / vol->v_sectorsize);
2800 SET16(vmeta, vdc->Block_Size, vol->v_sectorsize);
2801 SET8(vmeta, vdc->Rotate_Parity_count,
2802 fls(vol->v_rotate_parity) - 1);
2803 SET8(vmeta, vdc->MDF_Parity_Disks, vol->v_mdf_pdisks);
2804 SET16(vmeta, vdc->MDF_Parity_Generator_Polynomial,
2805 vol->v_mdf_polynomial);
2806 SET8(vmeta, vdc->MDF_Constant_Generation_Method,
2809 SET16(vmeta, vde->VD_Number, vol->v_global_id);
2810 if (vol->v_state <= G_RAID_VOLUME_S_BROKEN)
2811 SET8(vmeta, vde->VD_State, DDF_VDE_FAILED);
2812 else if (vol->v_state <= G_RAID_VOLUME_S_DEGRADED)
2813 SET8(vmeta, vde->VD_State, DDF_VDE_DEGRADED);
2814 else if (vol->v_state <= G_RAID_VOLUME_S_SUBOPTIMAL)
2815 SET8(vmeta, vde->VD_State, DDF_VDE_PARTIAL);
2817 SET8(vmeta, vde->VD_State, DDF_VDE_OPTIMAL);
2819 g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_STALE) > 0 ||
2820 g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_RESYNC) > 0)
2821 SET8(vmeta, vde->VD_State,
2822 GET8(vmeta, vde->VD_State) | DDF_VDE_DIRTY);
2823 SET8(vmeta, vde->Init_State, DDF_VDE_INIT_FULL); // XXX
2824 ddf_meta_put_name(vmeta, vol->v_name);
2826 for (i = 0; i < vol->v_disks_count; i++) {
2827 sd = &vol->v_subdisks[i];
2828 bvd = i / GET16(vmeta, vdc->Primary_Element_Count);
2829 pos = i % GET16(vmeta, vdc->Primary_Element_Count);
2832 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
2833 if (vmeta->bvdc[bvd] == NULL) {
2835 hdr->Configuration_Record_Length) *
2837 vmeta->bvdc[bvd] = malloc(size,
2838 M_MD_DDF, M_WAITOK);
2839 memset(vmeta->bvdc[bvd], 0xff, size);
2841 memcpy(vmeta->bvdc[bvd], vmeta->vdc,
2842 sizeof(struct ddf_vdc_record));
2843 SET8(vmeta, bvdc[bvd]->Secondary_Element_Seq, bvd);
2844 SET64(vmeta, bvdc[bvd]->Block_Count,
2845 sd->sd_size / vol->v_sectorsize);
2846 SET32(vmeta, bvdc[bvd]->Physical_Disk_Sequence[pos],
2847 GET32(&pd->pd_meta, pdd->PD_Reference));
2848 val2 = (uint64_t *)&(vmeta->bvdc[bvd]->Physical_Disk_Sequence[
2849 GET16(vmeta, hdr->Max_Primary_Element_Entries)]);
2850 SET64P(vmeta, val2 + pos,
2851 sd->sd_offset / vol->v_sectorsize);
2853 if (vmeta->bvdc[bvd] == NULL)
2856 j = ddf_meta_find_pd(gmeta, NULL,
2857 GET32(vmeta, bvdc[bvd]->Physical_Disk_Sequence[pos]));
2860 SET16(gmeta, pdr->entry[j].PD_Type,
2861 GET16(gmeta, pdr->entry[j].PD_Type) |
2862 DDF_PDE_PARTICIPATING);
2863 if (sd->sd_state == G_RAID_SUBDISK_S_NONE)
2864 SET16(gmeta, pdr->entry[j].PD_State,
2865 GET16(gmeta, pdr->entry[j].PD_State) |
2866 (DDF_PDE_FAILED | DDF_PDE_MISSING));
2867 else if (sd->sd_state == G_RAID_SUBDISK_S_FAILED)
2868 SET16(gmeta, pdr->entry[j].PD_State,
2869 GET16(gmeta, pdr->entry[j].PD_State) |
2870 (DDF_PDE_FAILED | DDF_PDE_PFA));
2871 else if (sd->sd_state <= G_RAID_SUBDISK_S_REBUILD)
2872 SET16(gmeta, pdr->entry[j].PD_State,
2873 GET16(gmeta, pdr->entry[j].PD_State) |
2876 SET16(gmeta, pdr->entry[j].PD_State,
2877 GET16(gmeta, pdr->entry[j].PD_State) |
2882 /* Mark spare and failed disks as such. */
2883 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2884 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
2885 i = ddf_meta_find_pd(gmeta, NULL,
2886 GET32(&pd->pd_meta, pdd->PD_Reference));
2889 if (disk->d_state == G_RAID_DISK_S_FAILED) {
2890 SET16(gmeta, pdr->entry[i].PD_State,
2891 GET16(gmeta, pdr->entry[i].PD_State) |
2892 (DDF_PDE_FAILED | DDF_PDE_PFA));
2894 if (disk->d_state != G_RAID_DISK_S_SPARE)
2896 sa = ddf_meta_find_sa(&pd->pd_meta, 0);
2898 (GET8D(&pd->pd_meta, sa->Spare_Type) &
2899 DDF_SAR_TYPE_DEDICATED) == 0) {
2900 SET16(gmeta, pdr->entry[i].PD_Type,
2901 GET16(gmeta, pdr->entry[i].PD_Type) |
2902 DDF_PDE_GLOBAL_SPARE);
2904 SET16(gmeta, pdr->entry[i].PD_Type,
2905 GET16(gmeta, pdr->entry[i].PD_Type) |
2906 DDF_PDE_CONFIG_SPARE);
2908 SET16(gmeta, pdr->entry[i].PD_State,
2909 GET16(gmeta, pdr->entry[i].PD_State) |
2913 /* Remove disks without "participating" flag (unused). */
2914 for (i = 0, j = -1; i < GET16(gmeta, pdr->Populated_PDEs); i++) {
2915 if (isff(gmeta->pdr->entry[i].PD_GUID, 24))
2917 if ((GET16(gmeta, pdr->entry[i].PD_Type) &
2918 (DDF_PDE_PARTICIPATING |
2919 DDF_PDE_GLOBAL_SPARE | DDF_PDE_CONFIG_SPARE)) != 0 ||
2920 g_raid_md_ddf_get_disk(sc,
2921 NULL, GET32(gmeta, pdr->entry[i].PD_Reference)) != NULL)
2924 memset(&gmeta->pdr->entry[i], 0xff,
2925 sizeof(struct ddf_pd_entry));
2927 SET16(gmeta, pdr->Populated_PDEs, j + 1);
2929 /* Update per-disk metadata and write them. */
2930 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2931 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
2932 if (disk->d_state != G_RAID_DISK_S_ACTIVE &&
2933 disk->d_state != G_RAID_DISK_S_SPARE)
2936 memcpy(pd->pd_meta.pdr, gmeta->pdr,
2937 GET32(&pd->pd_meta, hdr->pdr_length) *
2938 pd->pd_meta.sectorsize);
2940 SET16(&pd->pd_meta, vdr->Populated_VDEs, 0);
2941 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2942 if (vol->v_stopping)
2944 pv = (struct g_raid_md_ddf_pervolume *)vol->v_md_data;
2945 i = ddf_meta_find_vd(&pd->pd_meta,
2946 pv->pv_meta.vde->VD_GUID);
2948 i = ddf_meta_find_vd(&pd->pd_meta, NULL);
2950 memcpy(&pd->pd_meta.vdr->entry[i],
2952 sizeof(struct ddf_vd_entry));
2955 if (mdi->mdio_starting == 0) {
2956 /* Remove all VDCs to restore needed later. */
2957 j = GETCRNUM(&pd->pd_meta);
2958 for (i = 0; i < j; i++) {
2959 vdc = GETVDCPTR(&pd->pd_meta, i);
2960 if (GET32D(&pd->pd_meta, vdc->Signature) !=
2963 SET32D(&pd->pd_meta, vdc->Signature, 0xffffffff);
2966 TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
2967 vol = sd->sd_volume;
2968 if (vol->v_stopping)
2970 pv = (struct g_raid_md_ddf_pervolume *)vol->v_md_data;
2971 vmeta = &pv->pv_meta;
2972 vdc = ddf_meta_find_vdc(&pd->pd_meta,
2973 vmeta->vde->VD_GUID);
2975 vdc = ddf_meta_find_vdc(&pd->pd_meta, NULL);
2977 bvd = sd->sd_pos / GET16(vmeta,
2978 vdc->Primary_Element_Count);
2979 memcpy(vdc, vmeta->bvdc[bvd],
2981 hdr->Configuration_Record_Length) *
2982 pd->pd_meta.sectorsize);
2985 G_RAID_DEBUG(1, "Writing DDF metadata to %s",
2986 g_raid_get_diskname(disk));
2987 g_raid_md_ddf_print(&pd->pd_meta);
2988 ddf_meta_write(disk->d_consumer, &pd->pd_meta);
2994 g_raid_md_fail_disk_ddf(struct g_raid_md_object *md,
2995 struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk)
2997 struct g_raid_softc *sc;
2998 struct g_raid_md_ddf_perdisk *pd;
2999 struct g_raid_subdisk *sd;
3003 pd = (struct g_raid_md_ddf_perdisk *)tdisk->d_md_data;
3005 /* We can't fail disk that is not a part of array now. */
3006 if (tdisk->d_state != G_RAID_DISK_S_ACTIVE)
3010 * Mark disk as failed in metadata and try to write that metadata
3011 * to the disk itself to prevent it's later resurrection as STALE.
3013 G_RAID_DEBUG(1, "Writing DDF metadata to %s",
3014 g_raid_get_diskname(tdisk));
3015 i = ddf_meta_find_pd(&pd->pd_meta, NULL, GET32(&pd->pd_meta, pdd->PD_Reference));
3016 SET16(&pd->pd_meta, pdr->entry[i].PD_State, DDF_PDE_FAILED | DDF_PDE_PFA);
3017 if (tdisk->d_consumer != NULL)
3018 ddf_meta_write(tdisk->d_consumer, &pd->pd_meta);
3020 /* Change states. */
3021 g_raid_change_disk_state(tdisk, G_RAID_DISK_S_FAILED);
3022 TAILQ_FOREACH(sd, &tdisk->d_subdisks, sd_next) {
3023 g_raid_change_subdisk_state(sd,
3024 G_RAID_SUBDISK_S_FAILED);
3025 g_raid_event_send(sd, G_RAID_SUBDISK_E_FAILED,
3026 G_RAID_EVENT_SUBDISK);
3029 /* Write updated metadata to remaining disks. */
3030 g_raid_md_write_ddf(md, NULL, NULL, tdisk);
3032 g_raid_md_ddf_refill(sc);
3037 g_raid_md_free_disk_ddf(struct g_raid_md_object *md,
3038 struct g_raid_disk *disk)
3040 struct g_raid_md_ddf_perdisk *pd;
3042 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
3043 ddf_meta_free(&pd->pd_meta);
3045 disk->d_md_data = NULL;
3050 g_raid_md_free_volume_ddf(struct g_raid_md_object *md,
3051 struct g_raid_volume *vol)
3053 struct g_raid_md_ddf_object *mdi;
3054 struct g_raid_md_ddf_pervolume *pv;
3056 mdi = (struct g_raid_md_ddf_object *)md;
3057 pv = (struct g_raid_md_ddf_pervolume *)vol->v_md_data;
3058 ddf_vol_meta_free(&pv->pv_meta);
3059 if (!pv->pv_started) {
3061 mdi->mdio_starting--;
3062 callout_stop(&pv->pv_start_co);
3065 vol->v_md_data = NULL;
3070 g_raid_md_free_ddf(struct g_raid_md_object *md)
3072 struct g_raid_md_ddf_object *mdi;
3074 mdi = (struct g_raid_md_ddf_object *)md;
3075 if (!mdi->mdio_started) {
3076 mdi->mdio_started = 0;
3077 callout_stop(&mdi->mdio_start_co);
3078 G_RAID_DEBUG1(1, md->mdo_softc,
3079 "root_mount_rel %p", mdi->mdio_rootmount);
3080 root_mount_rel(mdi->mdio_rootmount);
3081 mdi->mdio_rootmount = NULL;
3083 ddf_meta_free(&mdi->mdio_meta);
3087 G_RAID_MD_DECLARE(ddf, "DDF");