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)
760 struct ddf_header *hdr;
764 dst->bigendian = src->bigendian;
765 ss = dst->sectorsize = src->sectorsize;
766 dst->hdr = malloc(ss, M_MD_DDF, M_WAITOK);
767 memcpy(dst->hdr, src->hdr, ss);
768 dst->cdr = malloc(GET32(src, hdr->cd_length) * ss, M_MD_DDF, M_WAITOK);
769 memcpy(dst->cdr, src->cdr, GET32(src, hdr->cd_length) * ss);
770 dst->pdr = malloc(GET32(src, hdr->pdr_length) * ss, M_MD_DDF, M_WAITOK);
771 memcpy(dst->pdr, src->pdr, GET32(src, hdr->pdr_length) * ss);
772 dst->vdr = malloc(GET32(src, hdr->vdr_length) * ss, M_MD_DDF, M_WAITOK);
773 memcpy(dst->vdr, src->vdr, GET32(src, hdr->vdr_length) * ss);
774 dst->cr = malloc(GET32(src, hdr->cr_length) * ss, M_MD_DDF, M_WAITOK);
775 memcpy(dst->cr, src->cr, GET32(src, hdr->cr_length) * ss);
776 dst->pdd = malloc(GET32(src, hdr->pdd_length) * ss, M_MD_DDF, M_WAITOK);
777 memcpy(dst->pdd, src->pdd, GET32(src, hdr->pdd_length) * ss);
778 if (src->bbm != NULL) {
779 dst->bbm = malloc(GET32(src, hdr->bbmlog_length) * ss, M_MD_DDF, M_WAITOK);
780 memcpy(dst->bbm, src->bbm, GET32(src, hdr->bbmlog_length) * ss);
785 ddf_meta_update(struct ddf_meta *meta, struct ddf_meta *src)
787 struct ddf_pd_entry *pde, *spde;
790 for (i = 0; i < GET16(src, pdr->Populated_PDEs); i++) {
791 spde = &src->pdr->entry[i];
792 if (isff(spde->PD_GUID, 24))
794 j = ddf_meta_find_pd(meta, NULL,
795 GET32(src, pdr->entry[i].PD_Reference));
797 j = ddf_meta_find_pd(meta, NULL, 0xffffffff);
798 pde = &meta->pdr->entry[j];
799 memcpy(pde, spde, sizeof(*pde));
801 pde = &meta->pdr->entry[j];
802 SET16D(meta, pde->PD_State,
803 GET16D(meta, pde->PD_State) |
804 GET16D(src, pde->PD_State));
810 ddf_meta_free(struct ddf_meta *meta)
813 if (meta->hdr != NULL) {
814 free(meta->hdr, M_MD_DDF);
817 if (meta->cdr != NULL) {
818 free(meta->cdr, M_MD_DDF);
821 if (meta->pdr != NULL) {
822 free(meta->pdr, M_MD_DDF);
825 if (meta->vdr != NULL) {
826 free(meta->vdr, M_MD_DDF);
829 if (meta->cr != NULL) {
830 free(meta->cr, M_MD_DDF);
833 if (meta->pdd != NULL) {
834 free(meta->pdd, M_MD_DDF);
837 if (meta->bbm != NULL) {
838 free(meta->bbm, M_MD_DDF);
844 ddf_vol_meta_create(struct ddf_vol_meta *meta, struct ddf_meta *sample)
848 struct ddf_header *hdr;
852 meta->bigendian = sample->bigendian;
853 ss = meta->sectorsize = sample->sectorsize;
854 meta->hdr = malloc(ss, M_MD_DDF, M_WAITOK);
855 memcpy(meta->hdr, sample->hdr, ss);
856 meta->cdr = malloc(GET32(sample, hdr->cd_length) * ss, M_MD_DDF, M_WAITOK);
857 memcpy(meta->cdr, sample->cdr, GET32(sample, hdr->cd_length) * ss);
858 meta->vde = malloc(sizeof(struct ddf_vd_entry), M_MD_DDF, M_WAITOK);
859 memset(meta->vde, 0xff, sizeof(struct ddf_vd_entry));
861 clock_ts_to_ct(&ts, &ct);
862 snprintf(meta->vde->VD_GUID, 25, "FreeBSD%04d%02d%02d%08x%01x",
863 ct.year, ct.mon, ct.day,
864 arc4random(), arc4random() & 0xf);
865 size = GET16(sample, hdr->Configuration_Record_Length) * ss;
866 meta->vdc = malloc(size, M_MD_DDF, M_WAITOK);
867 memset(meta->vdc, 0xff, size);
868 SET32(meta, vdc->Signature, DDF_VDCR_SIGNATURE);
869 memcpy(meta->vdc->VD_GUID, meta->vde->VD_GUID, 24);
870 SET32(meta, vdc->Sequence_Number, 0);
874 ddf_vol_meta_update(struct ddf_vol_meta *dst, struct ddf_meta *src,
875 uint8_t *GUID, int started)
877 struct ddf_header *hdr;
878 struct ddf_vd_entry *vde;
879 struct ddf_vdc_record *vdc;
880 int vnew, bvnew, bvd, size;
884 vde = &src->vdr->entry[ddf_meta_find_vd(src, GUID)];
885 vdc = ddf_meta_find_vdc(src, GUID);
886 if (GET8D(src, vdc->Secondary_Element_Count) == 1)
889 bvd = GET8D(src, vdc->Secondary_Element_Seq);
890 size = GET16(src, hdr->Configuration_Record_Length) * src->sectorsize;
892 if (dst->vdc == NULL ||
893 (!started && ((int32_t)(GET32D(src, vdc->Sequence_Number) -
894 GET32(dst, vdc->Sequence_Number))) > 0))
899 if (dst->bvdc[bvd] == NULL ||
900 (!started && ((int32_t)(GET32D(src, vdc->Sequence_Number) -
901 GET32(dst, bvdc[bvd]->Sequence_Number))) > 0))
907 dst->bigendian = src->bigendian;
908 ss = dst->sectorsize = src->sectorsize;
909 if (dst->hdr != NULL)
910 free(dst->hdr, M_MD_DDF);
911 dst->hdr = malloc(ss, M_MD_DDF, M_WAITOK);
912 memcpy(dst->hdr, src->hdr, ss);
913 if (dst->cdr != NULL)
914 free(dst->cdr, M_MD_DDF);
915 dst->cdr = malloc(GET32(src, hdr->cd_length) * ss, M_MD_DDF, M_WAITOK);
916 memcpy(dst->cdr, src->cdr, GET32(src, hdr->cd_length) * ss);
917 if (dst->vde != NULL)
918 free(dst->vde, M_MD_DDF);
919 dst->vde = malloc(sizeof(struct ddf_vd_entry), M_MD_DDF, M_WAITOK);
920 memcpy(dst->vde, vde, sizeof(struct ddf_vd_entry));
921 if (dst->vdc != NULL)
922 free(dst->vdc, M_MD_DDF);
923 dst->vdc = malloc(size, M_MD_DDF, M_WAITOK);
924 memcpy(dst->vdc, vdc, size);
927 if (dst->bvdc[bvd] != NULL)
928 free(dst->bvdc[bvd], M_MD_DDF);
929 dst->bvdc[bvd] = malloc(size, M_MD_DDF, M_WAITOK);
930 memcpy(dst->bvdc[bvd], vdc, size);
935 ddf_vol_meta_free(struct ddf_vol_meta *meta)
939 if (meta->hdr != NULL) {
940 free(meta->hdr, M_MD_DDF);
943 if (meta->cdr != NULL) {
944 free(meta->cdr, M_MD_DDF);
947 if (meta->vde != NULL) {
948 free(meta->vde, M_MD_DDF);
951 if (meta->vdc != NULL) {
952 free(meta->vdc, M_MD_DDF);
955 for (i = 0; i < DDF_MAX_DISKS_HARD; i++) {
956 if (meta->bvdc[i] != NULL) {
957 free(meta->bvdc[i], M_MD_DDF);
958 meta->bvdc[i] = NULL;
964 ddf_meta_unused_range(struct ddf_meta *meta, off_t *off, off_t *size)
966 struct ddf_vdc_record *vdc;
967 off_t beg[32], end[32], beg1, end1;
969 int i, j, n, num, pos;
974 ref = GET32(meta, pdd->PD_Reference);
975 pos = ddf_meta_find_pd(meta, NULL, ref);
977 end[0] = GET64(meta, pdr->entry[pos].Configured_Size);
979 num = GETCRNUM(meta);
980 for (i = 0; i < num; i++) {
981 vdc = GETVDCPTR(meta, i);
982 if (GET32D(meta, vdc->Signature) != DDF_VDCR_SIGNATURE)
984 for (pos = 0; pos < GET16D(meta, vdc->Primary_Element_Count); pos++)
985 if (GET32D(meta, vdc->Physical_Disk_Sequence[pos]) == ref)
987 if (pos == GET16D(meta, vdc->Primary_Element_Count))
989 offp = (uint64_t *)&(vdc->Physical_Disk_Sequence[
990 GET16(meta, hdr->Max_Primary_Element_Entries)]);
991 beg1 = GET64P(meta, offp + pos);
992 end1 = beg1 + GET64D(meta, vdc->Block_Count);
993 for (j = 0; j < n; j++) {
994 if (beg[j] >= end1 || end[j] <= beg1 )
996 if (beg[j] < beg1 && end[j] > end1) {
1001 } else if (beg[j] < beg1)
1007 for (j = 0; j < n; j++) {
1008 if (end[j] - beg[j] > *size) {
1010 *size = end[j] - beg[j];
1013 return ((*size > 0) ? 1 : 0);
1017 ddf_meta_get_name(struct ddf_meta *meta, int num, char *buf)
1022 b = meta->vdr->entry[num].VD_Name;
1023 for (i = 15; i >= 0; i--)
1026 memcpy(buf, b, i + 1);
1031 ddf_meta_put_name(struct ddf_vol_meta *meta, char *buf)
1035 len = min(strlen(buf), 16);
1036 memset(meta->vde->VD_Name, 0x20, 16);
1037 memcpy(meta->vde->VD_Name, buf, len);
1041 ddf_meta_read(struct g_consumer *cp, struct ddf_meta *meta)
1043 struct g_provider *pp;
1044 struct ddf_header *ahdr, *hdr;
1046 off_t plba, slba, lba;
1051 ddf_meta_free(meta);
1053 ss = meta->sectorsize = pp->sectorsize;
1054 /* Read anchor block. */
1055 abuf = g_read_data(cp, pp->mediasize - ss, ss, &error);
1057 G_RAID_DEBUG(1, "Cannot read metadata from %s (error=%d).",
1061 ahdr = (struct ddf_header *)abuf;
1063 /* Check if this is an DDF RAID struct */
1064 if (be32dec(&ahdr->Signature) == DDF_HEADER_SIGNATURE)
1065 meta->bigendian = 1;
1066 else if (le32dec(&ahdr->Signature) == DDF_HEADER_SIGNATURE)
1067 meta->bigendian = 0;
1069 G_RAID_DEBUG(1, "DDF signature check failed on %s", pp->name);
1073 if (ahdr->Header_Type != DDF_HEADER_ANCHOR) {
1074 G_RAID_DEBUG(1, "DDF header type check failed on %s", pp->name);
1079 plba = GET64(meta, hdr->Primary_Header_LBA);
1080 slba = GET64(meta, hdr->Secondary_Header_LBA);
1081 val = GET32(meta, hdr->CRC);
1082 SET32(meta, hdr->CRC, 0xffffffff);
1084 if (crc32(ahdr, ss) != val) {
1085 G_RAID_DEBUG(1, "DDF CRC mismatch on %s", pp->name);
1089 if ((plba + 6) * ss >= pp->mediasize) {
1090 G_RAID_DEBUG(1, "DDF primary header LBA is wrong on %s", pp->name);
1094 if (slba != -1 && (slba + 6) * ss >= pp->mediasize) {
1095 G_RAID_DEBUG(1, "DDF secondary header LBA is wrong on %s", pp->name);
1103 ddf_meta_free(meta);
1105 /* Read header block. */
1106 buf = g_read_data(cp, lba * ss, ss, &error);
1109 G_RAID_DEBUG(1, "DDF %s metadata read error on %s (error=%d).",
1110 (lba == plba) ? "primary" : "secondary", pp->name, error);
1111 if (lba == plba && slba != -1) {
1115 G_RAID_DEBUG(1, "DDF metadata read error on %s.", pp->name);
1118 meta->hdr = malloc(ss, M_MD_DDF, M_WAITOK);
1119 memcpy(meta->hdr, buf, ss);
1122 val = GET32(meta, hdr->CRC);
1123 SET32(meta, hdr->CRC, 0xffffffff);
1124 if (hdr->Signature != ahdr->Signature ||
1125 crc32(meta->hdr, ss) != val ||
1126 memcmp(hdr->DDF_Header_GUID, ahdr->DDF_Header_GUID, 24) ||
1127 GET64(meta, hdr->Primary_Header_LBA) != plba ||
1128 GET64(meta, hdr->Secondary_Header_LBA) != slba) {
1130 G_RAID_DEBUG(1, "DDF %s metadata check failed on %s",
1131 (lba == plba) ? "primary" : "secondary", pp->name);
1132 if (lba == plba && slba != -1) {
1136 G_RAID_DEBUG(1, "DDF metadata check failed on %s", pp->name);
1140 if ((lba == plba && hdr->Header_Type != DDF_HEADER_PRIMARY) ||
1141 (lba == slba && hdr->Header_Type != DDF_HEADER_SECONDARY))
1144 len = max(len, GET32(meta, hdr->cd_section) + GET32(meta, hdr->cd_length));
1145 len = max(len, GET32(meta, hdr->pdr_section) + GET32(meta, hdr->pdr_length));
1146 len = max(len, GET32(meta, hdr->vdr_section) + GET32(meta, hdr->vdr_length));
1147 len = max(len, GET32(meta, hdr->cr_section) + GET32(meta, hdr->cr_length));
1148 len = max(len, GET32(meta, hdr->pdd_section) + GET32(meta, hdr->pdd_length));
1149 if ((val = GET32(meta, hdr->bbmlog_section)) != 0xffffffff)
1150 len = max(len, val + GET32(meta, hdr->bbmlog_length));
1151 if ((val = GET32(meta, hdr->Diagnostic_Space)) != 0xffffffff)
1152 len = max(len, val + GET32(meta, hdr->Diagnostic_Space_Length));
1153 if ((val = GET32(meta, hdr->Vendor_Specific_Logs)) != 0xffffffff)
1154 len = max(len, val + GET32(meta, hdr->Vendor_Specific_Logs_Length));
1155 if ((plba + len) * ss >= pp->mediasize)
1157 if (slba != -1 && (slba + len) * ss >= pp->mediasize)
1159 /* Workaround for Adaptec implementation. */
1160 if (GET16(meta, hdr->Max_Primary_Element_Entries) == 0xffff) {
1161 SET16(meta, hdr->Max_Primary_Element_Entries,
1162 min(GET16(meta, hdr->Max_PD_Entries),
1163 (GET16(meta, hdr->Configuration_Record_Length) * ss - 512) / 12));
1166 if (GET32(meta, hdr->cd_length) * ss >= MAXPHYS ||
1167 GET32(meta, hdr->pdr_length) * ss >= MAXPHYS ||
1168 GET32(meta, hdr->vdr_length) * ss >= MAXPHYS ||
1169 GET32(meta, hdr->cr_length) * ss >= MAXPHYS ||
1170 GET32(meta, hdr->pdd_length) * ss >= MAXPHYS ||
1171 GET32(meta, hdr->bbmlog_length) * ss >= MAXPHYS) {
1172 G_RAID_DEBUG(1, "%s: Blocksize is too big.", pp->name);
1176 /* Read controller data. */
1177 buf = g_read_data(cp, (lba + GET32(meta, hdr->cd_section)) * ss,
1178 GET32(meta, hdr->cd_length) * ss, &error);
1181 meta->cdr = malloc(GET32(meta, hdr->cd_length) * ss, M_MD_DDF, M_WAITOK);
1182 memcpy(meta->cdr, buf, GET32(meta, hdr->cd_length) * ss);
1184 if (GET32(meta, cdr->Signature) != DDF_CONTROLLER_DATA_SIGNATURE)
1187 /* Read physical disk records. */
1188 buf = g_read_data(cp, (lba + GET32(meta, hdr->pdr_section)) * ss,
1189 GET32(meta, hdr->pdr_length) * ss, &error);
1192 meta->pdr = malloc(GET32(meta, hdr->pdr_length) * ss, M_MD_DDF, M_WAITOK);
1193 memcpy(meta->pdr, buf, GET32(meta, hdr->pdr_length) * ss);
1195 if (GET32(meta, pdr->Signature) != DDF_PDR_SIGNATURE)
1198 * Workaround for reading metadata corrupted due to graid bug.
1199 * XXX: Remove this before we have disks above 128PB. :)
1201 if (meta->bigendian) {
1202 for (i = 0; i < GET16(meta, pdr->Populated_PDEs); i++) {
1203 if (isff(meta->pdr->entry[i].PD_GUID, 24))
1205 if (GET32(meta, pdr->entry[i].PD_Reference) ==
1208 if (GET64(meta, pdr->entry[i].Configured_Size) >=
1210 SET16(meta, pdr->entry[i].PD_State,
1211 GET16(meta, pdr->entry[i].PD_State) &
1213 SET64(meta, pdr->entry[i].Configured_Size,
1214 GET64(meta, pdr->entry[i].Configured_Size) &
1215 ((1ULL << 48) - 1));
1220 /* Read virtual disk records. */
1221 buf = g_read_data(cp, (lba + GET32(meta, hdr->vdr_section)) * ss,
1222 GET32(meta, hdr->vdr_length) * ss, &error);
1225 meta->vdr = malloc(GET32(meta, hdr->vdr_length) * ss, M_MD_DDF, M_WAITOK);
1226 memcpy(meta->vdr, buf, GET32(meta, hdr->vdr_length) * ss);
1228 if (GET32(meta, vdr->Signature) != DDF_VD_RECORD_SIGNATURE)
1231 /* Read configuration records. */
1232 buf = g_read_data(cp, (lba + GET32(meta, hdr->cr_section)) * ss,
1233 GET32(meta, hdr->cr_length) * ss, &error);
1236 meta->cr = malloc(GET32(meta, hdr->cr_length) * ss, M_MD_DDF, M_WAITOK);
1237 memcpy(meta->cr, buf, GET32(meta, hdr->cr_length) * ss);
1240 /* Read physical disk data. */
1241 buf = g_read_data(cp, (lba + GET32(meta, hdr->pdd_section)) * ss,
1242 GET32(meta, hdr->pdd_length) * ss, &error);
1245 meta->pdd = malloc(GET32(meta, hdr->pdd_length) * ss, M_MD_DDF, M_WAITOK);
1246 memcpy(meta->pdd, buf, GET32(meta, hdr->pdd_length) * ss);
1248 if (GET32(meta, pdd->Signature) != DDF_PDD_SIGNATURE)
1250 i = ddf_meta_find_pd(meta, NULL, GET32(meta, pdd->PD_Reference));
1255 if (GET32(meta, hdr->bbmlog_section) != 0xffffffff &&
1256 GET32(meta, hdr->bbmlog_length) != 0) {
1257 buf = g_read_data(cp, (lba + GET32(meta, hdr->bbmlog_section)) * ss,
1258 GET32(meta, hdr->bbmlog_length) * ss, &error);
1261 meta->bbm = malloc(GET32(meta, hdr->bbmlog_length) * ss, M_MD_DDF, M_WAITOK);
1262 memcpy(meta->bbm, buf, GET32(meta, hdr->bbmlog_length) * ss);
1264 if (GET32(meta, bbm->Signature) != DDF_BBML_SIGNATURE)
1271 ddf_meta_free(meta);
1276 ddf_meta_write(struct g_consumer *cp, struct ddf_meta *meta)
1278 struct g_provider *pp;
1279 struct ddf_vdc_record *vdc;
1280 off_t alba, plba, slba, lba;
1285 ss = pp->sectorsize;
1286 lba = alba = pp->mediasize / ss - 1;
1287 plba = GET64(meta, hdr->Primary_Header_LBA);
1288 slba = GET64(meta, hdr->Secondary_Header_LBA);
1291 SET8(meta, hdr->Header_Type, (lba == alba) ? DDF_HEADER_ANCHOR :
1292 (lba == plba) ? DDF_HEADER_PRIMARY : DDF_HEADER_SECONDARY);
1293 SET32(meta, hdr->CRC, 0xffffffff);
1294 SET32(meta, hdr->CRC, crc32(meta->hdr, ss));
1295 error = g_write_data(cp, lba * ss, meta->hdr, ss);
1298 G_RAID_DEBUG(1, "Cannot write metadata to %s (error=%d).",
1308 size = GET32(meta, hdr->cd_length) * ss;
1309 SET32(meta, cdr->CRC, 0xffffffff);
1310 SET32(meta, cdr->CRC, crc32(meta->cdr, size));
1311 error = g_write_data(cp, (lba + GET32(meta, hdr->cd_section)) * ss,
1316 size = GET32(meta, hdr->pdr_length) * ss;
1317 SET32(meta, pdr->CRC, 0xffffffff);
1318 SET32(meta, pdr->CRC, crc32(meta->pdr, size));
1319 error = g_write_data(cp, (lba + GET32(meta, hdr->pdr_section)) * ss,
1324 size = GET32(meta, hdr->vdr_length) * ss;
1325 SET32(meta, vdr->CRC, 0xffffffff);
1326 SET32(meta, vdr->CRC, crc32(meta->vdr, size));
1327 error = g_write_data(cp, (lba + GET32(meta, hdr->vdr_section)) * ss,
1332 size = GET16(meta, hdr->Configuration_Record_Length) * ss;
1333 num = GETCRNUM(meta);
1334 for (i = 0; i < num; i++) {
1335 vdc = GETVDCPTR(meta, i);
1336 SET32D(meta, vdc->CRC, 0xffffffff);
1337 SET32D(meta, vdc->CRC, crc32(vdc, size));
1339 error = g_write_data(cp, (lba + GET32(meta, hdr->cr_section)) * ss,
1340 meta->cr, size * num);
1344 size = GET32(meta, hdr->pdd_length) * ss;
1345 SET32(meta, pdd->CRC, 0xffffffff);
1346 SET32(meta, pdd->CRC, crc32(meta->pdd, size));
1347 error = g_write_data(cp, (lba + GET32(meta, hdr->pdd_section)) * ss,
1352 if (GET32(meta, hdr->bbmlog_length) != 0) {
1353 size = GET32(meta, hdr->bbmlog_length) * ss;
1354 SET32(meta, bbm->CRC, 0xffffffff);
1355 SET32(meta, bbm->CRC, crc32(meta->bbm, size));
1356 error = g_write_data(cp,
1357 (lba + GET32(meta, hdr->bbmlog_section)) * ss,
1364 if (lba == plba && slba != -1) {
1373 ddf_meta_erase(struct g_consumer *cp)
1375 struct g_provider *pp;
1380 buf = malloc(pp->sectorsize, M_MD_DDF, M_WAITOK | M_ZERO);
1381 error = g_write_data(cp, pp->mediasize - pp->sectorsize,
1382 buf, pp->sectorsize);
1384 G_RAID_DEBUG(1, "Cannot erase metadata on %s (error=%d).",
1387 free(buf, M_MD_DDF);
1391 static struct g_raid_volume *
1392 g_raid_md_ddf_get_volume(struct g_raid_softc *sc, uint8_t *GUID)
1394 struct g_raid_volume *vol;
1395 struct g_raid_md_ddf_pervolume *pv;
1397 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
1398 pv = vol->v_md_data;
1399 if (memcmp(pv->pv_meta.vde->VD_GUID, GUID, 24) == 0)
1405 static struct g_raid_disk *
1406 g_raid_md_ddf_get_disk(struct g_raid_softc *sc, uint8_t *GUID, uint32_t id)
1408 struct g_raid_disk *disk;
1409 struct g_raid_md_ddf_perdisk *pd;
1410 struct ddf_meta *meta;
1412 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1413 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
1414 meta = &pd->pd_meta;
1416 if (memcmp(meta->pdd->PD_GUID, GUID, 24) == 0)
1419 if (GET32(meta, pdd->PD_Reference) == id)
1427 g_raid_md_ddf_purge_volumes(struct g_raid_softc *sc)
1429 struct g_raid_volume *vol, *tvol;
1430 struct g_raid_md_ddf_pervolume *pv;
1434 TAILQ_FOREACH_SAFE(vol, &sc->sc_volumes, v_next, tvol) {
1435 pv = vol->v_md_data;
1436 if (vol->v_stopping)
1438 for (i = 0; i < vol->v_disks_count; i++) {
1439 if (vol->v_subdisks[i].sd_state != G_RAID_SUBDISK_S_NONE)
1442 if (i >= vol->v_disks_count) {
1443 g_raid_destroy_volume(vol);
1451 g_raid_md_ddf_purge_disks(struct g_raid_softc *sc)
1454 struct g_raid_disk *disk, *tdisk;
1455 struct g_raid_volume *vol;
1456 struct g_raid_md_ddf_perdisk *pd;
1460 TAILQ_FOREACH_SAFE(disk, &sc->sc_disks, d_next, tdisk) {
1461 if (disk->d_state == G_RAID_DISK_S_SPARE)
1463 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
1465 /* Scan for deleted volumes. */
1466 for (i = 0; i < pd->pd_subdisks; ) {
1467 vol = g_raid_md_ddf_get_volume(sc,
1468 pd->pd_meta[i]->volume_id);
1469 if (vol != NULL && !vol->v_stopping) {
1473 free(pd->pd_meta[i], M_MD_DDF);
1474 for (j = i; j < pd->pd_subdisks - 1; j++)
1475 pd->pd_meta[j] = pd->pd_meta[j + 1];
1476 pd->pd_meta[DDF_MAX_SUBDISKS - 1] = NULL;
1481 /* If there is no metadata left - erase and delete disk. */
1482 if (pd->pd_subdisks == 0) {
1483 ddf_meta_erase(disk->d_consumer);
1484 g_raid_destroy_disk(disk);
1494 g_raid_md_ddf_supported(int level, int qual, int disks, int force)
1497 if (disks > DDF_MAX_DISKS_HARD)
1500 case G_RAID_VOLUME_RL_RAID0:
1501 if (qual != G_RAID_VOLUME_RLQ_NONE)
1505 if (!force && disks < 2)
1508 case G_RAID_VOLUME_RL_RAID1:
1511 if (qual == G_RAID_VOLUME_RLQ_R1SM) {
1512 if (!force && disks != 2)
1514 } else if (qual == G_RAID_VOLUME_RLQ_R1MM) {
1515 if (!force && disks != 3)
1520 case G_RAID_VOLUME_RL_RAID3:
1521 if (qual != G_RAID_VOLUME_RLQ_R3P0 &&
1522 qual != G_RAID_VOLUME_RLQ_R3PN)
1527 case G_RAID_VOLUME_RL_RAID4:
1528 if (qual != G_RAID_VOLUME_RLQ_R4P0 &&
1529 qual != G_RAID_VOLUME_RLQ_R4PN)
1534 case G_RAID_VOLUME_RL_RAID5:
1535 if (qual != G_RAID_VOLUME_RLQ_R5RA &&
1536 qual != G_RAID_VOLUME_RLQ_R5RS &&
1537 qual != G_RAID_VOLUME_RLQ_R5LA &&
1538 qual != G_RAID_VOLUME_RLQ_R5LS)
1543 case G_RAID_VOLUME_RL_RAID6:
1544 if (qual != G_RAID_VOLUME_RLQ_R6RA &&
1545 qual != G_RAID_VOLUME_RLQ_R6RS &&
1546 qual != G_RAID_VOLUME_RLQ_R6LA &&
1547 qual != G_RAID_VOLUME_RLQ_R6LS)
1552 case G_RAID_VOLUME_RL_RAIDMDF:
1553 if (qual != G_RAID_VOLUME_RLQ_RMDFRA &&
1554 qual != G_RAID_VOLUME_RLQ_RMDFRS &&
1555 qual != G_RAID_VOLUME_RLQ_RMDFLA &&
1556 qual != G_RAID_VOLUME_RLQ_RMDFLS)
1561 case G_RAID_VOLUME_RL_RAID1E:
1562 if (qual != G_RAID_VOLUME_RLQ_R1EA &&
1563 qual != G_RAID_VOLUME_RLQ_R1EO)
1568 case G_RAID_VOLUME_RL_SINGLE:
1569 if (qual != G_RAID_VOLUME_RLQ_NONE)
1574 case G_RAID_VOLUME_RL_CONCAT:
1575 if (qual != G_RAID_VOLUME_RLQ_NONE)
1580 case G_RAID_VOLUME_RL_RAID5E:
1581 if (qual != G_RAID_VOLUME_RLQ_R5ERA &&
1582 qual != G_RAID_VOLUME_RLQ_R5ERS &&
1583 qual != G_RAID_VOLUME_RLQ_R5ELA &&
1584 qual != G_RAID_VOLUME_RLQ_R5ELS)
1589 case G_RAID_VOLUME_RL_RAID5EE:
1590 if (qual != G_RAID_VOLUME_RLQ_R5EERA &&
1591 qual != G_RAID_VOLUME_RLQ_R5EERS &&
1592 qual != G_RAID_VOLUME_RLQ_R5EELA &&
1593 qual != G_RAID_VOLUME_RLQ_R5EELS)
1598 case G_RAID_VOLUME_RL_RAID5R:
1599 if (qual != G_RAID_VOLUME_RLQ_R5RRA &&
1600 qual != G_RAID_VOLUME_RLQ_R5RRS &&
1601 qual != G_RAID_VOLUME_RLQ_R5RLA &&
1602 qual != G_RAID_VOLUME_RLQ_R5RLS)
1614 g_raid_md_ddf_start_disk(struct g_raid_disk *disk, struct g_raid_volume *vol)
1616 struct g_raid_softc *sc;
1617 struct g_raid_subdisk *sd;
1618 struct g_raid_md_ddf_perdisk *pd;
1619 struct g_raid_md_ddf_pervolume *pv;
1620 struct g_raid_md_ddf_object *mdi;
1621 struct ddf_vol_meta *vmeta;
1622 struct ddf_meta *pdmeta, *gmeta;
1623 struct ddf_vdc_record *vdc1;
1624 struct ddf_sa_record *sa;
1625 off_t size, eoff = 0, esize = 0;
1627 int disk_pos, md_disk_bvd = -1, md_disk_pos = -1, md_pde_pos;
1628 int i, resurrection = 0;
1632 mdi = (struct g_raid_md_ddf_object *)sc->sc_md;
1633 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
1634 pdmeta = &pd->pd_meta;
1635 reference = GET32(&pd->pd_meta, pdd->PD_Reference);
1637 pv = vol->v_md_data;
1638 vmeta = &pv->pv_meta;
1639 gmeta = &mdi->mdio_meta;
1641 /* Find disk position in metadata by its reference. */
1642 disk_pos = ddf_meta_find_disk(vmeta, reference,
1643 &md_disk_bvd, &md_disk_pos);
1644 md_pde_pos = ddf_meta_find_pd(gmeta, NULL, reference);
1647 G_RAID_DEBUG1(1, sc,
1648 "Disk %s is not a present part of the volume %s",
1649 g_raid_get_diskname(disk), vol->v_name);
1651 /* Failed stale disk is useless for us. */
1652 if ((GET16(gmeta, pdr->entry[md_pde_pos].PD_State) & DDF_PDE_PFA) != 0) {
1653 g_raid_change_disk_state(disk, G_RAID_DISK_S_STALE_FAILED);
1657 /* If disk has some metadata for this volume - erase. */
1658 if ((vdc1 = ddf_meta_find_vdc(pdmeta, vmeta->vdc->VD_GUID)) != NULL)
1659 SET32D(pdmeta, vdc1->Signature, 0xffffffff);
1661 /* If we are in the start process, that's all for now. */
1662 if (!pv->pv_started)
1665 * If we have already started - try to get use of the disk.
1666 * Try to replace OFFLINE disks first, then FAILED.
1668 if (ddf_meta_count_vdc(&pd->pd_meta, NULL) >=
1669 GET16(&pd->pd_meta, hdr->Max_Partitions)) {
1670 G_RAID_DEBUG1(1, sc, "No free partitions on disk %s",
1671 g_raid_get_diskname(disk));
1674 ddf_meta_unused_range(&pd->pd_meta, &eoff, &esize);
1676 G_RAID_DEBUG1(1, sc, "No free space on disk %s",
1677 g_raid_get_diskname(disk));
1680 eoff *= pd->pd_meta.sectorsize;
1681 esize *= pd->pd_meta.sectorsize;
1683 for (i = 0; i < vol->v_disks_count; i++) {
1684 sd = &vol->v_subdisks[i];
1685 if (sd->sd_state != G_RAID_SUBDISK_S_NONE)
1687 if (sd->sd_state <= G_RAID_SUBDISK_S_FAILED &&
1689 vol->v_subdisks[i].sd_state < sd->sd_state))
1692 if (disk_pos >= 0 &&
1693 vol->v_raid_level != G_RAID_VOLUME_RL_CONCAT &&
1695 G_RAID_DEBUG1(1, sc, "Disk %s free space "
1696 "is too small (%ju < %ju)",
1697 g_raid_get_diskname(disk), esize, size);
1700 if (disk_pos >= 0) {
1701 if (vol->v_raid_level != G_RAID_VOLUME_RL_CONCAT)
1703 md_disk_bvd = disk_pos / GET16(vmeta, vdc->Primary_Element_Count); // XXX
1704 md_disk_pos = disk_pos % GET16(vmeta, vdc->Primary_Element_Count); // XXX
1707 if (disk->d_state == G_RAID_DISK_S_NONE)
1708 g_raid_change_disk_state(disk,
1709 G_RAID_DISK_S_STALE);
1714 * If spare is committable, delete spare record.
1715 * Othersize, mark it active and leave there.
1717 sa = ddf_meta_find_sa(&pd->pd_meta, 0);
1719 if ((GET8D(&pd->pd_meta, sa->Spare_Type) &
1720 DDF_SAR_TYPE_REVERTIBLE) == 0) {
1721 SET32D(&pd->pd_meta, sa->Signature, 0xffffffff);
1723 SET8D(&pd->pd_meta, sa->Spare_Type,
1724 GET8D(&pd->pd_meta, sa->Spare_Type) |
1725 DDF_SAR_TYPE_ACTIVE);
1729 G_RAID_DEBUG1(1, sc, "Disk %s takes pos %d in the volume %s",
1730 g_raid_get_diskname(disk), disk_pos, vol->v_name);
1734 sd = &vol->v_subdisks[disk_pos];
1736 if (resurrection && sd->sd_disk != NULL) {
1737 g_raid_change_disk_state(sd->sd_disk,
1738 G_RAID_DISK_S_STALE_FAILED);
1739 TAILQ_REMOVE(&sd->sd_disk->d_subdisks,
1742 vol->v_subdisks[disk_pos].sd_disk = disk;
1743 TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
1745 /* Welcome the new disk. */
1747 g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE);
1748 else if (GET16(gmeta, pdr->entry[md_pde_pos].PD_State) & DDF_PDE_PFA)
1749 g_raid_change_disk_state(disk, G_RAID_DISK_S_FAILED);
1751 g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE);
1754 sd->sd_offset = eoff;
1755 sd->sd_size = esize;
1756 } else if (pdmeta->cr != NULL &&
1757 (vdc1 = ddf_meta_find_vdc(pdmeta, vmeta->vdc->VD_GUID)) != NULL) {
1758 val2 = (uint64_t *)&(vdc1->Physical_Disk_Sequence[GET16(vmeta, hdr->Max_Primary_Element_Entries)]);
1759 sd->sd_offset = (off_t)GET64P(pdmeta, val2 + md_disk_pos) * 512;
1760 sd->sd_size = (off_t)GET64D(pdmeta, vdc1->Block_Count) * 512;
1764 /* Stale disk, almost same as new. */
1765 g_raid_change_subdisk_state(sd,
1766 G_RAID_SUBDISK_S_NEW);
1767 } else if (GET16(gmeta, pdr->entry[md_pde_pos].PD_State) & DDF_PDE_PFA) {
1769 g_raid_change_subdisk_state(sd,
1770 G_RAID_SUBDISK_S_FAILED);
1771 } else if ((GET16(gmeta, pdr->entry[md_pde_pos].PD_State) &
1772 (DDF_PDE_FAILED | DDF_PDE_REBUILD)) != 0) {
1773 /* Rebuilding disk. */
1774 g_raid_change_subdisk_state(sd,
1775 G_RAID_SUBDISK_S_REBUILD);
1776 sd->sd_rebuild_pos = 0;
1777 } else if ((GET8(vmeta, vde->VD_State) & DDF_VDE_DIRTY) != 0 ||
1778 (GET8(vmeta, vde->Init_State) & DDF_VDE_INIT_MASK) !=
1779 DDF_VDE_INIT_FULL) {
1780 /* Stale disk or dirty volume (unclean shutdown). */
1781 g_raid_change_subdisk_state(sd,
1782 G_RAID_SUBDISK_S_STALE);
1784 /* Up to date disk. */
1785 g_raid_change_subdisk_state(sd,
1786 G_RAID_SUBDISK_S_ACTIVE);
1788 g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW,
1789 G_RAID_EVENT_SUBDISK);
1791 return (resurrection);
1795 g_raid_md_ddf_refill(struct g_raid_softc *sc)
1797 struct g_raid_volume *vol;
1798 struct g_raid_subdisk *sd;
1799 struct g_raid_disk *disk;
1800 struct g_raid_md_object *md;
1801 struct g_raid_md_ddf_perdisk *pd;
1802 struct g_raid_md_ddf_pervolume *pv;
1803 int update, updated, i, bad;
1808 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
1809 pv = vol->v_md_data;
1810 if (!pv->pv_started || vol->v_stopping)
1813 /* Search for subdisk that needs replacement. */
1815 for (i = 0; i < vol->v_disks_count; i++) {
1816 sd = &vol->v_subdisks[i];
1817 if (sd->sd_state == G_RAID_SUBDISK_S_NONE ||
1818 sd->sd_state == G_RAID_SUBDISK_S_FAILED)
1824 G_RAID_DEBUG1(1, sc, "Volume %s is not complete, "
1825 "trying to refill.", vol->v_name);
1827 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1829 if (disk->d_state < G_RAID_DISK_S_SPARE)
1831 /* Skip already used by this volume. */
1832 for (i = 0; i < vol->v_disks_count; i++) {
1833 sd = &vol->v_subdisks[i];
1834 if (sd->sd_disk == disk)
1837 if (i < vol->v_disks_count)
1840 /* Try to use disk if it has empty extents. */
1841 pd = disk->d_md_data;
1842 if (ddf_meta_count_vdc(&pd->pd_meta, NULL) <
1843 GET16(&pd->pd_meta, hdr->Max_Partitions)) {
1844 update = g_raid_md_ddf_start_disk(disk, vol);
1849 g_raid_md_write_ddf(md, vol, NULL, disk);
1859 g_raid_md_ddf_start(struct g_raid_volume *vol)
1861 struct g_raid_softc *sc;
1862 struct g_raid_subdisk *sd;
1863 struct g_raid_disk *disk;
1864 struct g_raid_md_object *md;
1865 struct g_raid_md_ddf_perdisk *pd;
1866 struct g_raid_md_ddf_pervolume *pv;
1867 struct g_raid_md_ddf_object *mdi;
1868 struct ddf_vol_meta *vmeta;
1869 struct ddf_vdc_record *vdc;
1875 mdi = (struct g_raid_md_ddf_object *)md;
1876 pv = vol->v_md_data;
1877 vmeta = &pv->pv_meta;
1880 vol->v_raid_level = GET8(vmeta, vdc->Primary_RAID_Level);
1881 vol->v_raid_level_qualifier = GET8(vmeta, vdc->RLQ);
1882 if (GET8(vmeta, vdc->Secondary_Element_Count) > 1 &&
1883 vol->v_raid_level == G_RAID_VOLUME_RL_RAID1 &&
1884 GET8(vmeta, vdc->Secondary_RAID_Level) == 0)
1885 vol->v_raid_level = G_RAID_VOLUME_RL_RAID1E;
1886 vol->v_sectorsize = GET16(vmeta, vdc->Block_Size);
1887 if (vol->v_sectorsize == 0xffff)
1888 vol->v_sectorsize = vmeta->sectorsize;
1889 vol->v_strip_size = vol->v_sectorsize << GET8(vmeta, vdc->Stripe_Size);
1890 vol->v_disks_count = GET16(vmeta, vdc->Primary_Element_Count) *
1891 GET8(vmeta, vdc->Secondary_Element_Count);
1892 vol->v_mdf_pdisks = GET8(vmeta, vdc->MDF_Parity_Disks);
1893 vol->v_mdf_polynomial = GET16(vmeta, vdc->MDF_Parity_Generator_Polynomial);
1894 vol->v_mdf_method = GET8(vmeta, vdc->MDF_Constant_Generation_Method);
1895 if (GET8(vmeta, vdc->Rotate_Parity_count) > 31)
1896 vol->v_rotate_parity = 1;
1898 vol->v_rotate_parity = 1 << GET8(vmeta, vdc->Rotate_Parity_count);
1899 vol->v_mediasize = GET64(vmeta, vdc->VD_Size) * vol->v_sectorsize;
1900 for (i = 0, j = 0, bvd = 0; i < vol->v_disks_count; i++, j++) {
1901 if (j == GET16(vmeta, vdc->Primary_Element_Count)) {
1905 sd = &vol->v_subdisks[i];
1906 if (vmeta->bvdc[bvd] == NULL) {
1908 sd->sd_size = GET64(vmeta, vdc->Block_Count) *
1912 val2 = (uint64_t *)&(vmeta->bvdc[bvd]->Physical_Disk_Sequence[
1913 GET16(vmeta, hdr->Max_Primary_Element_Entries)]);
1914 sd->sd_offset = GET64P(vmeta, val2 + j) * vol->v_sectorsize;
1915 sd->sd_size = GET64(vmeta, bvdc[bvd]->Block_Count) *
1918 g_raid_start_volume(vol);
1920 /* Make all disks found till the moment take their places. */
1921 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1922 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
1923 if (ddf_meta_find_vdc(&pd->pd_meta, vmeta->vdc->VD_GUID) != NULL)
1924 g_raid_md_ddf_start_disk(disk, vol);
1928 mdi->mdio_starting--;
1929 callout_stop(&pv->pv_start_co);
1930 G_RAID_DEBUG1(0, sc, "Volume started.");
1931 g_raid_md_write_ddf(md, vol, NULL, NULL);
1933 /* Pickup any STALE/SPARE disks to refill array if needed. */
1934 g_raid_md_ddf_refill(sc);
1936 g_raid_event_send(vol, G_RAID_VOLUME_E_START, G_RAID_EVENT_VOLUME);
1940 g_raid_ddf_go(void *arg)
1942 struct g_raid_volume *vol;
1943 struct g_raid_softc *sc;
1944 struct g_raid_md_ddf_pervolume *pv;
1947 pv = vol->v_md_data;
1949 if (!pv->pv_started) {
1950 G_RAID_DEBUG1(0, sc, "Force volume start due to timeout.");
1951 g_raid_event_send(vol, G_RAID_VOLUME_E_STARTMD,
1952 G_RAID_EVENT_VOLUME);
1957 g_raid_md_ddf_new_disk(struct g_raid_disk *disk)
1959 struct g_raid_softc *sc;
1960 struct g_raid_md_object *md;
1961 struct g_raid_md_ddf_perdisk *pd;
1962 struct g_raid_md_ddf_pervolume *pv;
1963 struct g_raid_md_ddf_object *mdi;
1964 struct g_raid_volume *vol;
1965 struct ddf_meta *pdmeta;
1966 struct ddf_vol_meta *vmeta;
1967 struct ddf_vdc_record *vdc;
1968 struct ddf_vd_entry *vde;
1969 int i, j, k, num, have, need, cnt, spare;
1975 mdi = (struct g_raid_md_ddf_object *)md;
1976 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
1977 pdmeta = &pd->pd_meta;
1980 if (mdi->mdio_meta.hdr == NULL)
1981 ddf_meta_copy(&mdi->mdio_meta, pdmeta);
1983 ddf_meta_update(&mdi->mdio_meta, pdmeta);
1985 num = GETCRNUM(pdmeta);
1986 for (j = 0; j < num; j++) {
1987 vdc = GETVDCPTR(pdmeta, j);
1988 val = GET32D(pdmeta, vdc->Signature);
1990 if (val == DDF_SA_SIGNATURE && spare == -1)
1993 if (val != DDF_VDCR_SIGNATURE)
1996 k = ddf_meta_find_vd(pdmeta, vdc->VD_GUID);
1999 vde = &pdmeta->vdr->entry[k];
2001 /* Look for volume with matching ID. */
2002 vol = g_raid_md_ddf_get_volume(sc, vdc->VD_GUID);
2004 ddf_meta_get_name(pdmeta, k, buf);
2005 vol = g_raid_create_volume(sc, buf,
2006 GET16D(pdmeta, vde->VD_Number));
2007 pv = malloc(sizeof(*pv), M_MD_DDF, M_WAITOK | M_ZERO);
2008 vol->v_md_data = pv;
2009 callout_init(&pv->pv_start_co, 1);
2010 callout_reset(&pv->pv_start_co,
2011 g_raid_start_timeout * hz,
2012 g_raid_ddf_go, vol);
2013 mdi->mdio_starting++;
2015 pv = vol->v_md_data;
2017 /* If we haven't started yet - check metadata freshness. */
2018 vmeta = &pv->pv_meta;
2019 ddf_vol_meta_update(vmeta, pdmeta, vdc->VD_GUID, pv->pv_started);
2023 g_raid_change_disk_state(disk, G_RAID_DISK_S_SPARE);
2024 g_raid_md_ddf_refill(sc);
2027 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2028 pv = vol->v_md_data;
2029 vmeta = &pv->pv_meta;
2031 if (ddf_meta_find_vdc(pdmeta, vmeta->vdc->VD_GUID) == NULL)
2034 if (pv->pv_started) {
2035 if (g_raid_md_ddf_start_disk(disk, vol))
2036 g_raid_md_write_ddf(md, vol, NULL, NULL);
2040 /* If we collected all needed disks - start array. */
2043 for (k = 0; k < GET8(vmeta, vdc->Secondary_Element_Count); k++) {
2044 if (vmeta->bvdc[k] == NULL) {
2045 need += GET16(vmeta, vdc->Primary_Element_Count);
2048 cnt = GET16(vmeta, bvdc[k]->Primary_Element_Count);
2050 for (i = 0; i < cnt; i++) {
2051 val = GET32(vmeta, bvdc[k]->Physical_Disk_Sequence[i]);
2052 if (g_raid_md_ddf_get_disk(sc, NULL, val) != NULL)
2056 G_RAID_DEBUG1(1, sc, "Volume %s now has %d of %d disks",
2057 vol->v_name, have, need);
2059 g_raid_md_ddf_start(vol);
2064 g_raid_md_create_req_ddf(struct g_raid_md_object *md, struct g_class *mp,
2065 struct gctl_req *req, struct g_geom **gp)
2067 struct g_geom *geom;
2068 struct g_raid_softc *sc;
2069 struct g_raid_md_ddf_object *mdi, *mdi1;
2074 mdi = (struct g_raid_md_ddf_object *)md;
2075 fmtopt = gctl_get_asciiparam(req, "fmtopt");
2076 if (fmtopt == NULL || strcasecmp(fmtopt, "BE") == 0)
2078 else if (strcasecmp(fmtopt, "LE") == 0)
2081 gctl_error(req, "Incorrect fmtopt argument.");
2082 return (G_RAID_MD_TASTE_FAIL);
2085 /* Search for existing node. */
2086 LIST_FOREACH(geom, &mp->geom, geom) {
2090 if (sc->sc_stopping != 0)
2092 if (sc->sc_md->mdo_class != md->mdo_class)
2094 mdi1 = (struct g_raid_md_ddf_object *)sc->sc_md;
2095 if (mdi1->mdio_bigendian != be)
2101 return (G_RAID_MD_TASTE_EXISTING);
2104 /* Create new one if not found. */
2105 mdi->mdio_bigendian = be;
2106 snprintf(name, sizeof(name), "DDF%s", be ? "" : "-LE");
2107 sc = g_raid_create_node(mp, name, md);
2109 return (G_RAID_MD_TASTE_FAIL);
2112 return (G_RAID_MD_TASTE_NEW);
2116 g_raid_md_taste_ddf(struct g_raid_md_object *md, struct g_class *mp,
2117 struct g_consumer *cp, struct g_geom **gp)
2119 struct g_consumer *rcp;
2120 struct g_provider *pp;
2121 struct g_raid_softc *sc;
2122 struct g_raid_disk *disk;
2123 struct ddf_meta meta;
2124 struct g_raid_md_ddf_perdisk *pd;
2125 struct g_raid_md_ddf_object *mdi;
2126 struct g_geom *geom;
2127 int error, result, be;
2130 G_RAID_DEBUG(1, "Tasting DDF on %s", cp->provider->name);
2131 mdi = (struct g_raid_md_ddf_object *)md;
2134 /* Read metadata from device. */
2135 g_topology_unlock();
2136 bzero(&meta, sizeof(meta));
2137 error = ddf_meta_read(cp, &meta);
2140 return (G_RAID_MD_TASTE_FAIL);
2141 be = meta.bigendian;
2143 /* Metadata valid. Print it. */
2144 g_raid_md_ddf_print(&meta);
2146 /* Search for matching node. */
2148 LIST_FOREACH(geom, &mp->geom, geom) {
2152 if (sc->sc_stopping != 0)
2154 if (sc->sc_md->mdo_class != md->mdo_class)
2156 mdi = (struct g_raid_md_ddf_object *)sc->sc_md;
2157 if (mdi->mdio_bigendian != be)
2162 /* Found matching node. */
2164 G_RAID_DEBUG(1, "Found matching array %s", sc->sc_name);
2165 result = G_RAID_MD_TASTE_EXISTING;
2167 } else { /* Not found matching node -- create one. */
2168 result = G_RAID_MD_TASTE_NEW;
2169 mdi->mdio_bigendian = be;
2170 snprintf(name, sizeof(name), "DDF%s", be ? "" : "-LE");
2171 sc = g_raid_create_node(mp, name, md);
2176 /* There is no return after this point, so we close passed consumer. */
2177 g_access(cp, -1, 0, 0);
2179 rcp = g_new_consumer(geom);
2180 rcp->flags |= G_CF_DIRECT_RECEIVE;
2182 if (g_access(rcp, 1, 1, 1) != 0)
2185 g_topology_unlock();
2186 sx_xlock(&sc->sc_lock);
2188 pd = malloc(sizeof(*pd), M_MD_DDF, M_WAITOK | M_ZERO);
2190 disk = g_raid_create_disk(sc);
2191 disk->d_md_data = (void *)pd;
2192 disk->d_consumer = rcp;
2193 rcp->private = disk;
2195 g_raid_get_disk_info(disk);
2197 g_raid_md_ddf_new_disk(disk);
2199 sx_xunlock(&sc->sc_lock);
2206 g_raid_md_event_ddf(struct g_raid_md_object *md,
2207 struct g_raid_disk *disk, u_int event)
2209 struct g_raid_softc *sc;
2215 case G_RAID_DISK_E_DISCONNECTED:
2217 g_raid_change_disk_state(disk, G_RAID_DISK_S_NONE);
2218 g_raid_destroy_disk(disk);
2219 g_raid_md_ddf_purge_volumes(sc);
2221 /* Write updated metadata to all disks. */
2222 g_raid_md_write_ddf(md, NULL, NULL, NULL);
2224 /* Check if anything left. */
2225 if (g_raid_ndisks(sc, -1) == 0)
2226 g_raid_destroy_node(sc, 0);
2228 g_raid_md_ddf_refill(sc);
2235 g_raid_md_volume_event_ddf(struct g_raid_md_object *md,
2236 struct g_raid_volume *vol, u_int event)
2238 struct g_raid_md_ddf_pervolume *pv;
2240 pv = (struct g_raid_md_ddf_pervolume *)vol->v_md_data;
2242 case G_RAID_VOLUME_E_STARTMD:
2243 if (!pv->pv_started)
2244 g_raid_md_ddf_start(vol);
2251 g_raid_md_ctl_ddf(struct g_raid_md_object *md,
2252 struct gctl_req *req)
2254 struct g_raid_softc *sc;
2255 struct g_raid_volume *vol, *vol1;
2256 struct g_raid_subdisk *sd;
2257 struct g_raid_disk *disk, *disks[DDF_MAX_DISKS_HARD];
2258 struct g_raid_md_ddf_perdisk *pd;
2259 struct g_raid_md_ddf_pervolume *pv;
2260 struct g_raid_md_ddf_object *mdi;
2261 struct ddf_sa_record *sa;
2262 struct g_consumer *cp;
2263 struct g_provider *pp;
2265 const char *nodename, *verb, *volname, *levelname, *diskname;
2268 off_t size, sectorsize, strip, offs[DDF_MAX_DISKS_HARD], esize;
2269 intmax_t *sizearg, *striparg;
2270 int i, numdisks, len, level, qual;
2274 mdi = (struct g_raid_md_ddf_object *)md;
2275 verb = gctl_get_param(req, "verb", NULL);
2276 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
2279 if (strcmp(verb, "label") == 0) {
2282 gctl_error(req, "Invalid number of arguments.");
2285 volname = gctl_get_asciiparam(req, "arg1");
2286 if (volname == NULL) {
2287 gctl_error(req, "No volume name.");
2290 levelname = gctl_get_asciiparam(req, "arg2");
2291 if (levelname == NULL) {
2292 gctl_error(req, "No RAID level.");
2295 if (g_raid_volume_str2level(levelname, &level, &qual)) {
2296 gctl_error(req, "Unknown RAID level '%s'.", levelname);
2299 numdisks = *nargs - 3;
2300 force = gctl_get_paraml(req, "force", sizeof(*force));
2301 if (!g_raid_md_ddf_supported(level, qual, numdisks,
2302 force ? *force : 0)) {
2303 gctl_error(req, "Unsupported RAID level "
2304 "(0x%02x/0x%02x), or number of disks (%d).",
2305 level, qual, numdisks);
2309 /* Search for disks, connect them and probe. */
2312 bzero(disks, sizeof(disks));
2313 bzero(offs, sizeof(offs));
2314 for (i = 0; i < numdisks; i++) {
2315 snprintf(arg, sizeof(arg), "arg%d", i + 3);
2316 diskname = gctl_get_asciiparam(req, arg);
2317 if (diskname == NULL) {
2318 gctl_error(req, "No disk name (%s).", arg);
2322 if (strcmp(diskname, "NONE") == 0)
2325 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2326 if (disk->d_consumer != NULL &&
2327 disk->d_consumer->provider != NULL &&
2328 strcmp(disk->d_consumer->provider->name,
2333 if (disk->d_state != G_RAID_DISK_S_ACTIVE) {
2334 gctl_error(req, "Disk '%s' is in a "
2335 "wrong state (%s).", diskname,
2336 g_raid_disk_state2str(disk->d_state));
2340 pd = disk->d_md_data;
2341 if (ddf_meta_count_vdc(&pd->pd_meta, NULL) >=
2342 GET16(&pd->pd_meta, hdr->Max_Partitions)) {
2343 gctl_error(req, "No free partitions "
2349 pp = disk->d_consumer->provider;
2351 ddf_meta_unused_range(&pd->pd_meta,
2353 offs[i] *= pp->sectorsize;
2354 size = MIN(size, (off_t)esize * pp->sectorsize);
2355 sectorsize = MAX(sectorsize, pp->sectorsize);
2360 cp = g_raid_open_consumer(sc, diskname);
2362 gctl_error(req, "Can't open disk '%s'.",
2364 g_topology_unlock();
2369 pd = malloc(sizeof(*pd), M_MD_DDF, M_WAITOK | M_ZERO);
2370 disk = g_raid_create_disk(sc);
2371 disk->d_md_data = (void *)pd;
2372 disk->d_consumer = cp;
2375 ddf_meta_create(disk, &mdi->mdio_meta);
2376 if (mdi->mdio_meta.hdr == NULL)
2377 ddf_meta_copy(&mdi->mdio_meta, &pd->pd_meta);
2379 ddf_meta_update(&mdi->mdio_meta, &pd->pd_meta);
2380 g_topology_unlock();
2382 g_raid_get_disk_info(disk);
2384 /* Reserve some space for metadata. */
2385 size = MIN(size, GET64(&pd->pd_meta,
2386 pdr->entry[0].Configured_Size) * pp->sectorsize);
2387 sectorsize = MAX(sectorsize, pp->sectorsize);
2390 for (i = 0; i < numdisks; i++) {
2391 if (disks[i] != NULL &&
2392 disks[i]->d_state == G_RAID_DISK_S_NONE)
2393 g_raid_destroy_disk(disks[i]);
2398 if (sectorsize <= 0) {
2399 gctl_error(req, "Can't get sector size.");
2403 /* Handle size argument. */
2404 len = sizeof(*sizearg);
2405 sizearg = gctl_get_param(req, "size", &len);
2406 if (sizearg != NULL && len == sizeof(*sizearg) &&
2408 if (*sizearg > size) {
2409 gctl_error(req, "Size too big %lld > %lld.",
2410 (long long)*sizearg, (long long)size);
2416 /* Handle strip argument. */
2418 len = sizeof(*striparg);
2419 striparg = gctl_get_param(req, "strip", &len);
2420 if (striparg != NULL && len == sizeof(*striparg) &&
2422 if (*striparg < sectorsize) {
2423 gctl_error(req, "Strip size too small.");
2426 if (*striparg % sectorsize != 0) {
2427 gctl_error(req, "Incorrect strip size.");
2433 /* Round size down to strip or sector. */
2434 if (level == G_RAID_VOLUME_RL_RAID1 ||
2435 level == G_RAID_VOLUME_RL_RAID3 ||
2436 level == G_RAID_VOLUME_RL_SINGLE ||
2437 level == G_RAID_VOLUME_RL_CONCAT)
2438 size -= (size % sectorsize);
2439 else if (level == G_RAID_VOLUME_RL_RAID1E &&
2440 (numdisks & 1) != 0)
2441 size -= (size % (2 * strip));
2443 size -= (size % strip);
2445 gctl_error(req, "Size too small.");
2449 /* We have all we need, create things: volume, ... */
2450 pv = malloc(sizeof(*pv), M_MD_DDF, M_WAITOK | M_ZERO);
2451 ddf_vol_meta_create(&pv->pv_meta, &mdi->mdio_meta);
2453 vol = g_raid_create_volume(sc, volname, -1);
2454 vol->v_md_data = pv;
2455 vol->v_raid_level = level;
2456 vol->v_raid_level_qualifier = qual;
2457 vol->v_strip_size = strip;
2458 vol->v_disks_count = numdisks;
2459 if (level == G_RAID_VOLUME_RL_RAID0 ||
2460 level == G_RAID_VOLUME_RL_CONCAT ||
2461 level == G_RAID_VOLUME_RL_SINGLE)
2462 vol->v_mediasize = size * numdisks;
2463 else if (level == G_RAID_VOLUME_RL_RAID1)
2464 vol->v_mediasize = size;
2465 else if (level == G_RAID_VOLUME_RL_RAID3 ||
2466 level == G_RAID_VOLUME_RL_RAID4 ||
2467 level == G_RAID_VOLUME_RL_RAID5)
2468 vol->v_mediasize = size * (numdisks - 1);
2469 else if (level == G_RAID_VOLUME_RL_RAID5R) {
2470 vol->v_mediasize = size * (numdisks - 1);
2471 vol->v_rotate_parity = 1024;
2472 } else if (level == G_RAID_VOLUME_RL_RAID6 ||
2473 level == G_RAID_VOLUME_RL_RAID5E ||
2474 level == G_RAID_VOLUME_RL_RAID5EE)
2475 vol->v_mediasize = size * (numdisks - 2);
2476 else if (level == G_RAID_VOLUME_RL_RAIDMDF) {
2478 vol->v_mdf_pdisks = 2;
2480 vol->v_mdf_pdisks = 3;
2481 vol->v_mdf_polynomial = 0x11d;
2482 vol->v_mdf_method = 0x00;
2483 vol->v_mediasize = size * (numdisks - vol->v_mdf_pdisks);
2484 } else { /* RAID1E */
2485 vol->v_mediasize = ((size * numdisks) / strip / 2) *
2488 vol->v_sectorsize = sectorsize;
2489 g_raid_start_volume(vol);
2491 /* , and subdisks. */
2492 for (i = 0; i < numdisks; i++) {
2494 sd = &vol->v_subdisks[i];
2496 sd->sd_offset = offs[i];
2500 TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
2501 g_raid_change_disk_state(disk,
2502 G_RAID_DISK_S_ACTIVE);
2503 g_raid_change_subdisk_state(sd,
2504 G_RAID_SUBDISK_S_ACTIVE);
2505 g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW,
2506 G_RAID_EVENT_SUBDISK);
2509 /* Write metadata based on created entities. */
2510 G_RAID_DEBUG1(0, sc, "Array started.");
2511 g_raid_md_write_ddf(md, vol, NULL, NULL);
2513 /* Pickup any STALE/SPARE disks to refill array if needed. */
2514 g_raid_md_ddf_refill(sc);
2516 g_raid_event_send(vol, G_RAID_VOLUME_E_START,
2517 G_RAID_EVENT_VOLUME);
2520 if (strcmp(verb, "add") == 0) {
2522 gctl_error(req, "`add` command is not applicable, "
2523 "use `label` instead.");
2526 if (strcmp(verb, "delete") == 0) {
2528 nodename = gctl_get_asciiparam(req, "arg0");
2529 if (nodename != NULL && strcasecmp(sc->sc_name, nodename) != 0)
2532 /* Full node destruction. */
2533 if (*nargs == 1 && nodename != NULL) {
2534 /* Check if some volume is still open. */
2535 force = gctl_get_paraml(req, "force", sizeof(*force));
2536 if (force != NULL && *force == 0 &&
2537 g_raid_nopens(sc) != 0) {
2538 gctl_error(req, "Some volume is still open.");
2542 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2543 if (disk->d_consumer)
2544 ddf_meta_erase(disk->d_consumer);
2546 g_raid_destroy_node(sc, 0);
2550 /* Destroy specified volume. If it was last - all node. */
2552 gctl_error(req, "Invalid number of arguments.");
2555 volname = gctl_get_asciiparam(req,
2556 nodename != NULL ? "arg1" : "arg0");
2557 if (volname == NULL) {
2558 gctl_error(req, "No volume name.");
2562 /* Search for volume. */
2563 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2564 if (strcmp(vol->v_name, volname) == 0)
2566 pp = vol->v_provider;
2569 if (strcmp(pp->name, volname) == 0)
2571 if (strncmp(pp->name, "raid/", 5) == 0 &&
2572 strcmp(pp->name + 5, volname) == 0)
2576 i = strtol(volname, &tmp, 10);
2577 if (verb != volname && tmp[0] == 0) {
2578 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2579 if (vol->v_global_id == i)
2585 gctl_error(req, "Volume '%s' not found.", volname);
2589 /* Check if volume is still open. */
2590 force = gctl_get_paraml(req, "force", sizeof(*force));
2591 if (force != NULL && *force == 0 &&
2592 vol->v_provider_open != 0) {
2593 gctl_error(req, "Volume is still open.");
2597 /* Destroy volume and potentially node. */
2599 TAILQ_FOREACH(vol1, &sc->sc_volumes, v_next)
2602 g_raid_destroy_volume(vol);
2603 g_raid_md_ddf_purge_disks(sc);
2604 g_raid_md_write_ddf(md, NULL, NULL, NULL);
2606 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2607 if (disk->d_consumer)
2608 ddf_meta_erase(disk->d_consumer);
2610 g_raid_destroy_node(sc, 0);
2614 if (strcmp(verb, "remove") == 0 ||
2615 strcmp(verb, "fail") == 0) {
2617 gctl_error(req, "Invalid number of arguments.");
2620 for (i = 1; i < *nargs; i++) {
2621 snprintf(arg, sizeof(arg), "arg%d", i);
2622 diskname = gctl_get_asciiparam(req, arg);
2623 if (diskname == NULL) {
2624 gctl_error(req, "No disk name (%s).", arg);
2628 if (strncmp(diskname, "/dev/", 5) == 0)
2631 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2632 if (disk->d_consumer != NULL &&
2633 disk->d_consumer->provider != NULL &&
2634 strcmp(disk->d_consumer->provider->name,
2639 gctl_error(req, "Disk '%s' not found.",
2645 if (strcmp(verb, "fail") == 0) {
2646 g_raid_md_fail_disk_ddf(md, NULL, disk);
2650 /* Erase metadata on deleting disk and destroy it. */
2651 ddf_meta_erase(disk->d_consumer);
2652 g_raid_destroy_disk(disk);
2654 g_raid_md_ddf_purge_volumes(sc);
2656 /* Write updated metadata to remaining disks. */
2657 g_raid_md_write_ddf(md, NULL, NULL, NULL);
2659 /* Check if anything left. */
2660 if (g_raid_ndisks(sc, -1) == 0)
2661 g_raid_destroy_node(sc, 0);
2663 g_raid_md_ddf_refill(sc);
2666 if (strcmp(verb, "insert") == 0) {
2668 gctl_error(req, "Invalid number of arguments.");
2671 for (i = 1; i < *nargs; i++) {
2672 /* Get disk name. */
2673 snprintf(arg, sizeof(arg), "arg%d", i);
2674 diskname = gctl_get_asciiparam(req, arg);
2675 if (diskname == NULL) {
2676 gctl_error(req, "No disk name (%s).", arg);
2681 /* Try to find provider with specified name. */
2683 cp = g_raid_open_consumer(sc, diskname);
2685 gctl_error(req, "Can't open disk '%s'.",
2687 g_topology_unlock();
2692 g_topology_unlock();
2694 pd = malloc(sizeof(*pd), M_MD_DDF, M_WAITOK | M_ZERO);
2696 disk = g_raid_create_disk(sc);
2697 disk->d_consumer = cp;
2698 disk->d_md_data = (void *)pd;
2701 g_raid_get_disk_info(disk);
2703 /* Welcome the "new" disk. */
2704 g_raid_change_disk_state(disk, G_RAID_DISK_S_SPARE);
2705 ddf_meta_create(disk, &mdi->mdio_meta);
2706 sa = ddf_meta_find_sa(&pd->pd_meta, 1);
2708 SET32D(&pd->pd_meta, sa->Signature,
2710 SET8D(&pd->pd_meta, sa->Spare_Type, 0);
2711 SET16D(&pd->pd_meta, sa->Populated_SAEs, 0);
2712 SET16D(&pd->pd_meta, sa->MAX_SAE_Supported,
2713 (GET16(&pd->pd_meta, hdr->Configuration_Record_Length) *
2714 pd->pd_meta.sectorsize -
2715 sizeof(struct ddf_sa_record)) /
2716 sizeof(struct ddf_sa_entry));
2718 if (mdi->mdio_meta.hdr == NULL)
2719 ddf_meta_copy(&mdi->mdio_meta, &pd->pd_meta);
2721 ddf_meta_update(&mdi->mdio_meta, &pd->pd_meta);
2722 g_raid_md_write_ddf(md, NULL, NULL, NULL);
2723 g_raid_md_ddf_refill(sc);
2731 g_raid_md_write_ddf(struct g_raid_md_object *md, struct g_raid_volume *tvol,
2732 struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk)
2734 struct g_raid_softc *sc;
2735 struct g_raid_volume *vol;
2736 struct g_raid_subdisk *sd;
2737 struct g_raid_disk *disk;
2738 struct g_raid_md_ddf_perdisk *pd;
2739 struct g_raid_md_ddf_pervolume *pv;
2740 struct g_raid_md_ddf_object *mdi;
2741 struct ddf_meta *gmeta;
2742 struct ddf_vol_meta *vmeta;
2743 struct ddf_vdc_record *vdc;
2744 struct ddf_sa_record *sa;
2746 int i, j, pos, bvd, size;
2749 mdi = (struct g_raid_md_ddf_object *)md;
2750 gmeta = &mdi->mdio_meta;
2752 if (sc->sc_stopping == G_RAID_DESTROY_HARD)
2756 * Clear disk flags to let only really needed ones to be reset.
2757 * Do it only if there are no volumes in starting state now,
2758 * as they can update disk statuses yet and we may kill innocent.
2760 if (mdi->mdio_starting == 0) {
2761 for (i = 0; i < GET16(gmeta, pdr->Populated_PDEs); i++) {
2762 if (isff(gmeta->pdr->entry[i].PD_GUID, 24))
2764 SET16(gmeta, pdr->entry[i].PD_Type,
2765 GET16(gmeta, pdr->entry[i].PD_Type) &
2766 ~(DDF_PDE_PARTICIPATING |
2767 DDF_PDE_GLOBAL_SPARE | DDF_PDE_CONFIG_SPARE));
2768 if ((GET16(gmeta, pdr->entry[i].PD_State) &
2770 SET16(gmeta, pdr->entry[i].PD_State, 0);
2774 /* Generate/update new per-volume metadata. */
2775 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2776 pv = (struct g_raid_md_ddf_pervolume *)vol->v_md_data;
2777 if (vol->v_stopping || !pv->pv_started)
2779 vmeta = &pv->pv_meta;
2781 SET32(vmeta, vdc->Sequence_Number,
2782 GET32(vmeta, vdc->Sequence_Number) + 1);
2783 if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E &&
2784 vol->v_disks_count % 2 == 0)
2785 SET16(vmeta, vdc->Primary_Element_Count, 2);
2787 SET16(vmeta, vdc->Primary_Element_Count,
2788 vol->v_disks_count);
2789 SET8(vmeta, vdc->Stripe_Size,
2790 ffs(vol->v_strip_size / vol->v_sectorsize) - 1);
2791 if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E &&
2792 vol->v_disks_count % 2 == 0) {
2793 SET8(vmeta, vdc->Primary_RAID_Level,
2795 SET8(vmeta, vdc->RLQ, 0);
2796 SET8(vmeta, vdc->Secondary_Element_Count,
2797 vol->v_disks_count / 2);
2798 SET8(vmeta, vdc->Secondary_RAID_Level, 0);
2800 SET8(vmeta, vdc->Primary_RAID_Level,
2802 SET8(vmeta, vdc->RLQ,
2803 vol->v_raid_level_qualifier);
2804 SET8(vmeta, vdc->Secondary_Element_Count, 1);
2805 SET8(vmeta, vdc->Secondary_RAID_Level, 0);
2807 SET8(vmeta, vdc->Secondary_Element_Seq, 0);
2808 SET64(vmeta, vdc->Block_Count, 0);
2809 SET64(vmeta, vdc->VD_Size, vol->v_mediasize / vol->v_sectorsize);
2810 SET16(vmeta, vdc->Block_Size, vol->v_sectorsize);
2811 SET8(vmeta, vdc->Rotate_Parity_count,
2812 fls(vol->v_rotate_parity) - 1);
2813 SET8(vmeta, vdc->MDF_Parity_Disks, vol->v_mdf_pdisks);
2814 SET16(vmeta, vdc->MDF_Parity_Generator_Polynomial,
2815 vol->v_mdf_polynomial);
2816 SET8(vmeta, vdc->MDF_Constant_Generation_Method,
2819 SET16(vmeta, vde->VD_Number, vol->v_global_id);
2820 if (vol->v_state <= G_RAID_VOLUME_S_BROKEN)
2821 SET8(vmeta, vde->VD_State, DDF_VDE_FAILED);
2822 else if (vol->v_state <= G_RAID_VOLUME_S_DEGRADED)
2823 SET8(vmeta, vde->VD_State, DDF_VDE_DEGRADED);
2824 else if (vol->v_state <= G_RAID_VOLUME_S_SUBOPTIMAL)
2825 SET8(vmeta, vde->VD_State, DDF_VDE_PARTIAL);
2827 SET8(vmeta, vde->VD_State, DDF_VDE_OPTIMAL);
2829 g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_STALE) > 0 ||
2830 g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_RESYNC) > 0)
2831 SET8(vmeta, vde->VD_State,
2832 GET8(vmeta, vde->VD_State) | DDF_VDE_DIRTY);
2833 SET8(vmeta, vde->Init_State, DDF_VDE_INIT_FULL); // XXX
2834 ddf_meta_put_name(vmeta, vol->v_name);
2836 for (i = 0; i < vol->v_disks_count; i++) {
2837 sd = &vol->v_subdisks[i];
2838 bvd = i / GET16(vmeta, vdc->Primary_Element_Count);
2839 pos = i % GET16(vmeta, vdc->Primary_Element_Count);
2842 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
2843 if (vmeta->bvdc[bvd] == NULL) {
2845 hdr->Configuration_Record_Length) *
2847 vmeta->bvdc[bvd] = malloc(size,
2848 M_MD_DDF, M_WAITOK);
2849 memset(vmeta->bvdc[bvd], 0xff, size);
2851 memcpy(vmeta->bvdc[bvd], vmeta->vdc,
2852 sizeof(struct ddf_vdc_record));
2853 SET8(vmeta, bvdc[bvd]->Secondary_Element_Seq, bvd);
2854 SET64(vmeta, bvdc[bvd]->Block_Count,
2855 sd->sd_size / vol->v_sectorsize);
2856 SET32(vmeta, bvdc[bvd]->Physical_Disk_Sequence[pos],
2857 GET32(&pd->pd_meta, pdd->PD_Reference));
2858 val2 = (uint64_t *)&(vmeta->bvdc[bvd]->Physical_Disk_Sequence[
2859 GET16(vmeta, hdr->Max_Primary_Element_Entries)]);
2860 SET64P(vmeta, val2 + pos,
2861 sd->sd_offset / vol->v_sectorsize);
2863 if (vmeta->bvdc[bvd] == NULL)
2866 j = ddf_meta_find_pd(gmeta, NULL,
2867 GET32(vmeta, bvdc[bvd]->Physical_Disk_Sequence[pos]));
2870 SET16(gmeta, pdr->entry[j].PD_Type,
2871 GET16(gmeta, pdr->entry[j].PD_Type) |
2872 DDF_PDE_PARTICIPATING);
2873 if (sd->sd_state == G_RAID_SUBDISK_S_NONE)
2874 SET16(gmeta, pdr->entry[j].PD_State,
2875 GET16(gmeta, pdr->entry[j].PD_State) |
2876 (DDF_PDE_FAILED | DDF_PDE_MISSING));
2877 else if (sd->sd_state == G_RAID_SUBDISK_S_FAILED)
2878 SET16(gmeta, pdr->entry[j].PD_State,
2879 GET16(gmeta, pdr->entry[j].PD_State) |
2880 (DDF_PDE_FAILED | DDF_PDE_PFA));
2881 else if (sd->sd_state <= G_RAID_SUBDISK_S_REBUILD)
2882 SET16(gmeta, pdr->entry[j].PD_State,
2883 GET16(gmeta, pdr->entry[j].PD_State) |
2886 SET16(gmeta, pdr->entry[j].PD_State,
2887 GET16(gmeta, pdr->entry[j].PD_State) |
2892 /* Mark spare and failed disks as such. */
2893 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2894 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
2895 i = ddf_meta_find_pd(gmeta, NULL,
2896 GET32(&pd->pd_meta, pdd->PD_Reference));
2899 if (disk->d_state == G_RAID_DISK_S_FAILED) {
2900 SET16(gmeta, pdr->entry[i].PD_State,
2901 GET16(gmeta, pdr->entry[i].PD_State) |
2902 (DDF_PDE_FAILED | DDF_PDE_PFA));
2904 if (disk->d_state != G_RAID_DISK_S_SPARE)
2906 sa = ddf_meta_find_sa(&pd->pd_meta, 0);
2908 (GET8D(&pd->pd_meta, sa->Spare_Type) &
2909 DDF_SAR_TYPE_DEDICATED) == 0) {
2910 SET16(gmeta, pdr->entry[i].PD_Type,
2911 GET16(gmeta, pdr->entry[i].PD_Type) |
2912 DDF_PDE_GLOBAL_SPARE);
2914 SET16(gmeta, pdr->entry[i].PD_Type,
2915 GET16(gmeta, pdr->entry[i].PD_Type) |
2916 DDF_PDE_CONFIG_SPARE);
2918 SET16(gmeta, pdr->entry[i].PD_State,
2919 GET16(gmeta, pdr->entry[i].PD_State) |
2923 /* Remove disks without "participating" flag (unused). */
2924 for (i = 0, j = -1; i < GET16(gmeta, pdr->Populated_PDEs); i++) {
2925 if (isff(gmeta->pdr->entry[i].PD_GUID, 24))
2927 if ((GET16(gmeta, pdr->entry[i].PD_Type) &
2928 (DDF_PDE_PARTICIPATING |
2929 DDF_PDE_GLOBAL_SPARE | DDF_PDE_CONFIG_SPARE)) != 0 ||
2930 g_raid_md_ddf_get_disk(sc,
2931 NULL, GET32(gmeta, pdr->entry[i].PD_Reference)) != NULL)
2934 memset(&gmeta->pdr->entry[i], 0xff,
2935 sizeof(struct ddf_pd_entry));
2937 SET16(gmeta, pdr->Populated_PDEs, j + 1);
2939 /* Update per-disk metadata and write them. */
2940 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2941 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
2942 if (disk->d_state != G_RAID_DISK_S_ACTIVE &&
2943 disk->d_state != G_RAID_DISK_S_SPARE)
2946 memcpy(pd->pd_meta.pdr, gmeta->pdr,
2947 GET32(&pd->pd_meta, hdr->pdr_length) *
2948 pd->pd_meta.sectorsize);
2950 SET16(&pd->pd_meta, vdr->Populated_VDEs, 0);
2951 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2952 if (vol->v_stopping)
2954 pv = (struct g_raid_md_ddf_pervolume *)vol->v_md_data;
2955 i = ddf_meta_find_vd(&pd->pd_meta,
2956 pv->pv_meta.vde->VD_GUID);
2958 i = ddf_meta_find_vd(&pd->pd_meta, NULL);
2960 memcpy(&pd->pd_meta.vdr->entry[i],
2962 sizeof(struct ddf_vd_entry));
2965 if (mdi->mdio_starting == 0) {
2966 /* Remove all VDCs to restore needed later. */
2967 j = GETCRNUM(&pd->pd_meta);
2968 for (i = 0; i < j; i++) {
2969 vdc = GETVDCPTR(&pd->pd_meta, i);
2970 if (GET32D(&pd->pd_meta, vdc->Signature) !=
2973 SET32D(&pd->pd_meta, vdc->Signature, 0xffffffff);
2976 TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
2977 vol = sd->sd_volume;
2978 if (vol->v_stopping)
2980 pv = (struct g_raid_md_ddf_pervolume *)vol->v_md_data;
2981 vmeta = &pv->pv_meta;
2982 vdc = ddf_meta_find_vdc(&pd->pd_meta,
2983 vmeta->vde->VD_GUID);
2985 vdc = ddf_meta_find_vdc(&pd->pd_meta, NULL);
2987 bvd = sd->sd_pos / GET16(vmeta,
2988 vdc->Primary_Element_Count);
2989 memcpy(vdc, vmeta->bvdc[bvd],
2991 hdr->Configuration_Record_Length) *
2992 pd->pd_meta.sectorsize);
2995 G_RAID_DEBUG(1, "Writing DDF metadata to %s",
2996 g_raid_get_diskname(disk));
2997 g_raid_md_ddf_print(&pd->pd_meta);
2998 ddf_meta_write(disk->d_consumer, &pd->pd_meta);
3004 g_raid_md_fail_disk_ddf(struct g_raid_md_object *md,
3005 struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk)
3007 struct g_raid_softc *sc;
3008 struct g_raid_md_ddf_perdisk *pd;
3009 struct g_raid_subdisk *sd;
3013 pd = (struct g_raid_md_ddf_perdisk *)tdisk->d_md_data;
3015 /* We can't fail disk that is not a part of array now. */
3016 if (tdisk->d_state != G_RAID_DISK_S_ACTIVE)
3020 * Mark disk as failed in metadata and try to write that metadata
3021 * to the disk itself to prevent it's later resurrection as STALE.
3023 G_RAID_DEBUG(1, "Writing DDF metadata to %s",
3024 g_raid_get_diskname(tdisk));
3025 i = ddf_meta_find_pd(&pd->pd_meta, NULL, GET32(&pd->pd_meta, pdd->PD_Reference));
3026 SET16(&pd->pd_meta, pdr->entry[i].PD_State, DDF_PDE_FAILED | DDF_PDE_PFA);
3027 if (tdisk->d_consumer != NULL)
3028 ddf_meta_write(tdisk->d_consumer, &pd->pd_meta);
3030 /* Change states. */
3031 g_raid_change_disk_state(tdisk, G_RAID_DISK_S_FAILED);
3032 TAILQ_FOREACH(sd, &tdisk->d_subdisks, sd_next) {
3033 g_raid_change_subdisk_state(sd,
3034 G_RAID_SUBDISK_S_FAILED);
3035 g_raid_event_send(sd, G_RAID_SUBDISK_E_FAILED,
3036 G_RAID_EVENT_SUBDISK);
3039 /* Write updated metadata to remaining disks. */
3040 g_raid_md_write_ddf(md, NULL, NULL, tdisk);
3042 g_raid_md_ddf_refill(sc);
3047 g_raid_md_free_disk_ddf(struct g_raid_md_object *md,
3048 struct g_raid_disk *disk)
3050 struct g_raid_md_ddf_perdisk *pd;
3052 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
3053 ddf_meta_free(&pd->pd_meta);
3055 disk->d_md_data = NULL;
3060 g_raid_md_free_volume_ddf(struct g_raid_md_object *md,
3061 struct g_raid_volume *vol)
3063 struct g_raid_md_ddf_object *mdi;
3064 struct g_raid_md_ddf_pervolume *pv;
3066 mdi = (struct g_raid_md_ddf_object *)md;
3067 pv = (struct g_raid_md_ddf_pervolume *)vol->v_md_data;
3068 ddf_vol_meta_free(&pv->pv_meta);
3069 if (!pv->pv_started) {
3071 mdi->mdio_starting--;
3072 callout_stop(&pv->pv_start_co);
3075 vol->v_md_data = NULL;
3080 g_raid_md_free_ddf(struct g_raid_md_object *md)
3082 struct g_raid_md_ddf_object *mdi;
3084 mdi = (struct g_raid_md_ddf_object *)md;
3085 if (!mdi->mdio_started) {
3086 mdi->mdio_started = 0;
3087 callout_stop(&mdi->mdio_start_co);
3088 G_RAID_DEBUG1(1, md->mdo_softc,
3089 "root_mount_rel %p", mdi->mdio_rootmount);
3090 root_mount_rel(mdi->mdio_rootmount);
3091 mdi->mdio_rootmount = NULL;
3093 ddf_meta_free(&mdi->mdio_meta);
3097 G_RAID_MD_DECLARE(ddf, "DDF");