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>
45 #include <geom/geom.h>
46 #include "geom/raid/g_raid.h"
47 #include "geom/raid/md_ddf.h"
48 #include "g_raid_md_if.h"
50 static MALLOC_DEFINE(M_MD_DDF, "md_ddf_data", "GEOM_RAID DDF metadata");
52 #define DDF_MAX_DISKS_HARD 128
54 #define DDF_MAX_DISKS 16
55 #define DDF_MAX_VDISKS 7
56 #define DDF_MAX_PARTITIONS 1
58 #define DECADE (3600*24*(365*10+2)) /* 10 years in seconds. */
63 struct ddf_header *hdr;
64 struct ddf_cd_record *cdr;
65 struct ddf_pd_record *pdr;
66 struct ddf_vd_record *vdr;
68 struct ddf_pdd_record *pdd;
69 struct ddf_bbm_log *bbm;
75 struct ddf_header *hdr;
76 struct ddf_cd_record *cdr;
77 struct ddf_vd_entry *vde;
78 struct ddf_vdc_record *vdc;
79 struct ddf_vdc_record *bvdc[DDF_MAX_DISKS_HARD];
82 struct g_raid_md_ddf_perdisk {
83 struct ddf_meta pd_meta;
86 struct g_raid_md_ddf_pervolume {
87 struct ddf_vol_meta pv_meta;
89 struct callout pv_start_co; /* STARTING state timer. */
92 struct g_raid_md_ddf_object {
93 struct g_raid_md_object mdio_base;
95 struct ddf_meta mdio_meta;
97 struct callout mdio_start_co; /* STARTING state timer. */
99 struct root_hold_token *mdio_rootmount; /* Root mount delay token. */
102 static g_raid_md_create_req_t g_raid_md_create_req_ddf;
103 static g_raid_md_taste_t g_raid_md_taste_ddf;
104 static g_raid_md_event_t g_raid_md_event_ddf;
105 static g_raid_md_volume_event_t g_raid_md_volume_event_ddf;
106 static g_raid_md_ctl_t g_raid_md_ctl_ddf;
107 static g_raid_md_write_t g_raid_md_write_ddf;
108 static g_raid_md_fail_disk_t g_raid_md_fail_disk_ddf;
109 static g_raid_md_free_disk_t g_raid_md_free_disk_ddf;
110 static g_raid_md_free_volume_t g_raid_md_free_volume_ddf;
111 static g_raid_md_free_t g_raid_md_free_ddf;
113 static kobj_method_t g_raid_md_ddf_methods[] = {
114 KOBJMETHOD(g_raid_md_create_req, g_raid_md_create_req_ddf),
115 KOBJMETHOD(g_raid_md_taste, g_raid_md_taste_ddf),
116 KOBJMETHOD(g_raid_md_event, g_raid_md_event_ddf),
117 KOBJMETHOD(g_raid_md_volume_event, g_raid_md_volume_event_ddf),
118 KOBJMETHOD(g_raid_md_ctl, g_raid_md_ctl_ddf),
119 KOBJMETHOD(g_raid_md_write, g_raid_md_write_ddf),
120 KOBJMETHOD(g_raid_md_fail_disk, g_raid_md_fail_disk_ddf),
121 KOBJMETHOD(g_raid_md_free_disk, g_raid_md_free_disk_ddf),
122 KOBJMETHOD(g_raid_md_free_volume, g_raid_md_free_volume_ddf),
123 KOBJMETHOD(g_raid_md_free, g_raid_md_free_ddf),
127 static struct g_raid_md_class g_raid_md_ddf_class = {
129 g_raid_md_ddf_methods,
130 sizeof(struct g_raid_md_ddf_object),
135 #define GET8(m, f) ((m)->f)
136 #define GET16(m, f) ((m)->bigendian ? be16dec(&(m)->f) : le16dec(&(m)->f))
137 #define GET32(m, f) ((m)->bigendian ? be32dec(&(m)->f) : le32dec(&(m)->f))
138 #define GET64(m, f) ((m)->bigendian ? be64dec(&(m)->f) : le64dec(&(m)->f))
139 #define GET8D(m, f) (f)
140 #define GET16D(m, f) ((m)->bigendian ? be16dec(&f) : le16dec(&f))
141 #define GET32D(m, f) ((m)->bigendian ? be32dec(&f) : le32dec(&f))
142 #define GET64D(m, f) ((m)->bigendian ? be64dec(&f) : le64dec(&f))
143 #define GET8P(m, f) (*(f))
144 #define GET16P(m, f) ((m)->bigendian ? be16dec(f) : le16dec(f))
145 #define GET32P(m, f) ((m)->bigendian ? be32dec(f) : le32dec(f))
146 #define GET64P(m, f) ((m)->bigendian ? be64dec(f) : le64dec(f))
148 #define SET8P(m, f, v) \
150 #define SET16P(m, f, v) \
152 if ((m)->bigendian) \
157 #define SET32P(m, f, v) \
159 if ((m)->bigendian) \
164 #define SET64P(m, f, v) \
166 if ((m)->bigendian) \
171 #define SET8(m, f, v) SET8P((m), &((m)->f), (v))
172 #define SET16(m, f, v) SET16P((m), &((m)->f), (v))
173 #define SET32(m, f, v) SET32P((m), &((m)->f), (v))
174 #define SET64(m, f, v) SET64P((m), &((m)->f), (v))
175 #define SET8D(m, f, v) SET8P((m), &(f), (v))
176 #define SET16D(m, f, v) SET16P((m), &(f), (v))
177 #define SET32D(m, f, v) SET32P((m), &(f), (v))
178 #define SET64D(m, f, v) SET64P((m), &(f), (v))
180 #define GETCRNUM(m) (GET32((m), hdr->cr_length) / \
181 GET16((m), hdr->Configuration_Record_Length))
183 #define GETVDCPTR(m, n) ((struct ddf_vdc_record *)((uint8_t *)(m)->cr + \
184 (n) * GET16((m), hdr->Configuration_Record_Length) * \
187 #define GETSAPTR(m, n) ((struct ddf_sa_record *)((uint8_t *)(m)->cr + \
188 (n) * GET16((m), hdr->Configuration_Record_Length) * \
192 isff(uint8_t *buf, int size)
196 for (i = 0; i < size; i++)
203 print_guid(uint8_t *buf)
208 for (i = 0; i < 24; i++) {
209 if (buf[i] != 0 && (buf[i] < ' ' || buf[i] > 127)) {
215 printf("'%.24s'", buf);
217 for (i = 0; i < 24; i++)
218 printf("%02x", buf[i]);
223 g_raid_md_ddf_print(struct ddf_meta *meta)
225 struct ddf_vdc_record *vdc;
226 struct ddf_vuc_record *vuc;
227 struct ddf_sa_record *sa;
230 int i, j, k, num, num2;
232 if (g_raid_debug < 1)
235 printf("********* DDF Metadata *********\n");
236 printf("**** Header ****\n");
237 printf("DDF_Header_GUID ");
238 print_guid(meta->hdr->DDF_Header_GUID);
240 printf("DDF_rev %8.8s\n", (char *)&meta->hdr->DDF_rev[0]);
241 printf("Sequence_Number 0x%08x\n", GET32(meta, hdr->Sequence_Number));
242 printf("TimeStamp 0x%08x\n", GET32(meta, hdr->TimeStamp));
243 printf("Open_Flag 0x%02x\n", GET16(meta, hdr->Open_Flag));
244 printf("Foreign_Flag 0x%02x\n", GET16(meta, hdr->Foreign_Flag));
245 printf("Diskgrouping 0x%02x\n", GET16(meta, hdr->Diskgrouping));
246 printf("Primary_Header_LBA %ju\n", GET64(meta, hdr->Primary_Header_LBA));
247 printf("Secondary_Header_LBA %ju\n", GET64(meta, hdr->Secondary_Header_LBA));
248 printf("WorkSpace_Length %u\n", GET32(meta, hdr->WorkSpace_Length));
249 printf("WorkSpace_LBA %ju\n", GET64(meta, hdr->WorkSpace_LBA));
250 printf("Max_PD_Entries %u\n", GET16(meta, hdr->Max_PD_Entries));
251 printf("Max_VD_Entries %u\n", GET16(meta, hdr->Max_VD_Entries));
252 printf("Max_Partitions %u\n", GET16(meta, hdr->Max_Partitions));
253 printf("Configuration_Record_Length %u\n", GET16(meta, hdr->Configuration_Record_Length));
254 printf("Max_Primary_Element_Entries %u\n", GET16(meta, hdr->Max_Primary_Element_Entries));
255 printf("Controller Data %u:%u\n", GET32(meta, hdr->cd_section), GET32(meta, hdr->cd_length));
256 printf("Physical Disk %u:%u\n", GET32(meta, hdr->pdr_section), GET32(meta, hdr->pdr_length));
257 printf("Virtual Disk %u:%u\n", GET32(meta, hdr->vdr_section), GET32(meta, hdr->vdr_length));
258 printf("Configuration Recs %u:%u\n", GET32(meta, hdr->cr_section), GET32(meta, hdr->cr_length));
259 printf("Physical Disk Recs %u:%u\n", GET32(meta, hdr->pdd_section), GET32(meta, hdr->pdd_length));
260 printf("BBM Log %u:%u\n", GET32(meta, hdr->bbmlog_section), GET32(meta, hdr->bbmlog_length));
261 printf("Diagnostic Space %u:%u\n", GET32(meta, hdr->Diagnostic_Space), GET32(meta, hdr->Diagnostic_Space_Length));
262 printf("Vendor_Specific_Logs %u:%u\n", GET32(meta, hdr->Vendor_Specific_Logs), GET32(meta, hdr->Vendor_Specific_Logs_Length));
263 printf("**** Controller Data ****\n");
264 printf("Controller_GUID ");
265 print_guid(meta->cdr->Controller_GUID);
267 printf("Controller_Type 0x%04x%04x 0x%04x%04x\n",
268 GET16(meta, cdr->Controller_Type.Vendor_ID),
269 GET16(meta, cdr->Controller_Type.Device_ID),
270 GET16(meta, cdr->Controller_Type.SubVendor_ID),
271 GET16(meta, cdr->Controller_Type.SubDevice_ID));
272 printf("Product_ID '%.16s'\n", (char *)&meta->cdr->Product_ID[0]);
273 printf("**** Physical Disk Records ****\n");
274 printf("Populated_PDEs %u\n", GET16(meta, pdr->Populated_PDEs));
275 printf("Max_PDE_Supported %u\n", GET16(meta, pdr->Max_PDE_Supported));
276 for (j = 0; j < GET16(meta, pdr->Populated_PDEs); j++) {
277 if (isff(meta->pdr->entry[j].PD_GUID, 24))
279 if (GET32(meta, pdr->entry[j].PD_Reference) == 0xffffffff)
282 print_guid(meta->pdr->entry[j].PD_GUID);
284 printf("PD_Reference 0x%08x\n",
285 GET32(meta, pdr->entry[j].PD_Reference));
286 printf("PD_Type 0x%04x\n",
287 GET16(meta, pdr->entry[j].PD_Type));
288 printf("PD_State 0x%04x\n",
289 GET16(meta, pdr->entry[j].PD_State));
290 printf("Configured_Size %ju\n",
291 GET64(meta, pdr->entry[j].Configured_Size));
292 printf("Block_Size %u\n",
293 GET16(meta, pdr->entry[j].Block_Size));
295 printf("**** Virtual Disk Records ****\n");
296 printf("Populated_VDEs %u\n", GET16(meta, vdr->Populated_VDEs));
297 printf("Max_VDE_Supported %u\n", GET16(meta, vdr->Max_VDE_Supported));
298 for (j = 0; j < GET16(meta, vdr->Populated_VDEs); j++) {
299 if (isff(meta->vdr->entry[j].VD_GUID, 24))
302 print_guid(meta->vdr->entry[j].VD_GUID);
304 printf("VD_Number 0x%04x\n",
305 GET16(meta, vdr->entry[j].VD_Number));
306 printf("VD_Type 0x%04x\n",
307 GET16(meta, vdr->entry[j].VD_Type));
308 printf("VD_State 0x%02x\n",
309 GET8(meta, vdr->entry[j].VD_State));
310 printf("Init_State 0x%02x\n",
311 GET8(meta, vdr->entry[j].Init_State));
312 printf("Drive_Failures_Remaining %u\n",
313 GET8(meta, vdr->entry[j].Drive_Failures_Remaining));
314 printf("VD_Name '%.16s'\n",
315 (char *)&meta->vdr->entry[j].VD_Name);
317 printf("**** Configuration Records ****\n");
318 num = GETCRNUM(meta);
319 for (j = 0; j < num; j++) {
320 vdc = GETVDCPTR(meta, j);
321 val = GET32D(meta, vdc->Signature);
323 case DDF_VDCR_SIGNATURE:
324 printf("** Virtual Disk Configuration **\n");
326 print_guid(vdc->VD_GUID);
328 printf("Timestamp 0x%08x\n",
329 GET32D(meta, vdc->Timestamp));
330 printf("Sequence_Number 0x%08x\n",
331 GET32D(meta, vdc->Sequence_Number));
332 printf("Primary_Element_Count %u\n",
333 GET16D(meta, vdc->Primary_Element_Count));
334 printf("Stripe_Size %u\n",
335 GET8D(meta, vdc->Stripe_Size));
336 printf("Primary_RAID_Level 0x%02x\n",
337 GET8D(meta, vdc->Primary_RAID_Level));
338 printf("RLQ 0x%02x\n",
339 GET8D(meta, vdc->RLQ));
340 printf("Secondary_Element_Count %u\n",
341 GET8D(meta, vdc->Secondary_Element_Count));
342 printf("Secondary_Element_Seq %u\n",
343 GET8D(meta, vdc->Secondary_Element_Seq));
344 printf("Secondary_RAID_Level 0x%02x\n",
345 GET8D(meta, vdc->Secondary_RAID_Level));
346 printf("Block_Count %ju\n",
347 GET64D(meta, vdc->Block_Count));
348 printf("VD_Size %ju\n",
349 GET64D(meta, vdc->VD_Size));
350 printf("Block_Size %u\n",
351 GET16D(meta, vdc->Block_Size));
352 printf("Rotate_Parity_count %u\n",
353 GET8D(meta, vdc->Rotate_Parity_count));
354 printf("Associated_Spare_Disks");
355 for (i = 0; i < 8; i++) {
356 if (GET32D(meta, vdc->Associated_Spares[i]) != 0xffffffff)
357 printf(" 0x%08x", GET32D(meta, vdc->Associated_Spares[i]));
360 printf("Cache_Flags %016jx\n",
361 GET64D(meta, vdc->Cache_Flags));
362 printf("BG_Rate %u\n",
363 GET8D(meta, vdc->BG_Rate));
364 printf("MDF_Parity_Disks %u\n",
365 GET8D(meta, vdc->MDF_Parity_Disks));
366 printf("MDF_Parity_Generator_Polynomial 0x%04x\n",
367 GET16D(meta, vdc->MDF_Parity_Generator_Polynomial));
368 printf("MDF_Constant_Generation_Method 0x%02x\n",
369 GET8D(meta, vdc->MDF_Constant_Generation_Method));
370 printf("Physical_Disks ");
371 num2 = GET16D(meta, vdc->Primary_Element_Count);
372 val2 = (uint64_t *)&(vdc->Physical_Disk_Sequence[GET16(meta, hdr->Max_Primary_Element_Entries)]);
373 for (i = 0; i < num2; i++)
374 printf(" 0x%08x @ %ju",
375 GET32D(meta, vdc->Physical_Disk_Sequence[i]),
376 GET64P(meta, val2 + i));
379 case DDF_VUCR_SIGNATURE:
380 printf("** Vendor Unique Configuration **\n");
381 vuc = (struct ddf_vuc_record *)vdc;
383 print_guid(vuc->VD_GUID);
386 case DDF_SA_SIGNATURE:
387 printf("** Spare Assignment Configuration **\n");
388 sa = (struct ddf_sa_record *)vdc;
389 printf("Timestamp 0x%08x\n",
390 GET32D(meta, sa->Timestamp));
391 printf("Spare_Type 0x%02x\n",
392 GET8D(meta, sa->Spare_Type));
393 printf("Populated_SAEs %u\n",
394 GET16D(meta, sa->Populated_SAEs));
395 printf("MAX_SAE_Supported %u\n",
396 GET16D(meta, sa->MAX_SAE_Supported));
397 for (i = 0; i < GET16D(meta, sa->Populated_SAEs); i++) {
398 if (isff(sa->entry[i].VD_GUID, 24))
401 for (k = 0; k < 24; k++)
402 printf("%02x", sa->entry[i].VD_GUID[k]);
404 printf("Secondary_Element %u\n",
405 GET16D(meta, sa->entry[i].Secondary_Element));
412 printf("Unknown configuration signature %08x\n", val);
416 printf("**** Physical Disk Data ****\n");
418 print_guid(meta->pdd->PD_GUID);
420 printf("PD_Reference 0x%08x\n",
421 GET32(meta, pdd->PD_Reference));
422 printf("Forced_Ref_Flag 0x%02x\n",
423 GET8(meta, pdd->Forced_Ref_Flag));
424 printf("Forced_PD_GUID_Flag 0x%02x\n",
425 GET8(meta, pdd->Forced_PD_GUID_Flag));
429 ddf_meta_find_pd(struct ddf_meta *meta, uint8_t *GUID, uint32_t PD_Reference)
433 for (i = 0; i < GET16(meta, pdr->Populated_PDEs); i++) {
435 if (memcmp(meta->pdr->entry[i].PD_GUID, GUID, 24) == 0)
437 } else if (PD_Reference != 0xffffffff) {
438 if (GET32(meta, pdr->entry[i].PD_Reference) == PD_Reference)
441 if (isff(meta->pdr->entry[i].PD_GUID, 24))
444 if (GUID == NULL && PD_Reference == 0xffffffff) {
445 if (i >= GET16(meta, pdr->Max_PDE_Supported))
447 SET16(meta, pdr->Populated_PDEs, i + 1);
454 ddf_meta_find_vd(struct ddf_meta *meta, uint8_t *GUID)
458 for (i = 0; i < GET16(meta, vdr->Populated_VDEs); i++) {
460 if (memcmp(meta->vdr->entry[i].VD_GUID, GUID, 24) == 0)
463 if (isff(meta->vdr->entry[i].VD_GUID, 24))
467 if (i >= GET16(meta, vdr->Max_VDE_Supported))
469 SET16(meta, vdr->Populated_VDEs, i + 1);
475 static struct ddf_vdc_record *
476 ddf_meta_find_vdc(struct ddf_meta *meta, uint8_t *GUID)
478 struct ddf_vdc_record *vdc;
481 num = GETCRNUM(meta);
482 for (i = 0; i < num; i++) {
483 vdc = GETVDCPTR(meta, i);
485 if (GET32D(meta, vdc->Signature) == DDF_VDCR_SIGNATURE &&
486 memcmp(vdc->VD_GUID, GUID, 24) == 0)
489 if (GET32D(meta, vdc->Signature) == 0xffffffff ||
490 GET32D(meta, vdc->Signature) == 0)
497 ddf_meta_count_vdc(struct ddf_meta *meta, uint8_t *GUID)
499 struct ddf_vdc_record *vdc;
503 num = GETCRNUM(meta);
504 for (i = 0; i < num; i++) {
505 vdc = GETVDCPTR(meta, i);
506 if (GET32D(meta, vdc->Signature) != DDF_VDCR_SIGNATURE)
508 if (GUID == NULL || memcmp(vdc->VD_GUID, GUID, 24) == 0)
515 ddf_meta_find_disk(struct ddf_vol_meta *vmeta, uint32_t PD_Reference,
516 int *bvdp, int *posp)
521 for (bvd = 0; bvd < GET8(vmeta, vdc->Secondary_Element_Count); bvd++) {
522 if (vmeta->bvdc[bvd] == NULL) {
523 i += GET16(vmeta, vdc->Primary_Element_Count); // XXX
526 for (pos = 0; pos < GET16(vmeta, bvdc[bvd]->Primary_Element_Count);
528 if (GET32(vmeta, bvdc[bvd]->Physical_Disk_Sequence[pos]) ==
541 static struct ddf_sa_record *
542 ddf_meta_find_sa(struct ddf_meta *meta, int create)
544 struct ddf_sa_record *sa;
547 num = GETCRNUM(meta);
548 for (i = 0; i < num; i++) {
549 sa = GETSAPTR(meta, i);
550 if (GET32D(meta, sa->Signature) == DDF_SA_SIGNATURE)
554 for (i = 0; i < num; i++) {
555 sa = GETSAPTR(meta, i);
556 if (GET32D(meta, sa->Signature) == 0xffffffff ||
557 GET32D(meta, sa->Signature) == 0)
565 ddf_meta_create(struct g_raid_disk *disk, struct ddf_meta *sample)
569 struct g_raid_md_ddf_perdisk *pd;
570 struct g_raid_md_ddf_object *mdi;
571 struct ddf_meta *meta;
572 struct ddf_pd_entry *pde;
576 char serial_buffer[DISK_IDENT_SIZE];
578 if (sample->hdr == NULL)
581 mdi = (struct g_raid_md_ddf_object *)disk->d_softc->sc_md;
582 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
584 ss = disk->d_consumer->provider->sectorsize;
585 anchorlba = disk->d_consumer->provider->mediasize / ss - 1;
587 meta->sectorsize = ss;
588 meta->bigendian = sample ? sample->bigendian : mdi->mdio_bigendian;
590 clock_ts_to_ct(&ts, &ct);
593 meta->hdr = malloc(ss, M_MD_DDF, M_WAITOK);
594 memset(meta->hdr, 0xff, ss);
596 memcpy(meta->hdr, sample->hdr, sizeof(struct ddf_header));
597 if (ss != sample->sectorsize) {
598 SET32(meta, hdr->WorkSpace_Length,
599 howmany(GET32(sample, hdr->WorkSpace_Length) *
600 sample->sectorsize, ss));
601 SET16(meta, hdr->Configuration_Record_Length,
602 howmany(GET16(sample,
603 hdr->Configuration_Record_Length) *
604 sample->sectorsize, ss));
605 SET32(meta, hdr->cd_length,
606 howmany(GET32(sample, hdr->cd_length) *
607 sample->sectorsize, ss));
608 SET32(meta, hdr->pdr_length,
609 howmany(GET32(sample, hdr->pdr_length) *
610 sample->sectorsize, ss));
611 SET32(meta, hdr->vdr_length,
612 howmany(GET32(sample, hdr->vdr_length) *
613 sample->sectorsize, ss));
614 SET32(meta, hdr->cr_length,
615 howmany(GET32(sample, hdr->cr_length) *
616 sample->sectorsize, ss));
617 SET32(meta, hdr->pdd_length,
618 howmany(GET32(sample, hdr->pdd_length) *
619 sample->sectorsize, ss));
620 SET32(meta, hdr->bbmlog_length,
621 howmany(GET32(sample, hdr->bbmlog_length) *
622 sample->sectorsize, ss));
623 SET32(meta, hdr->Diagnostic_Space,
624 howmany(GET32(sample, hdr->bbmlog_length) *
625 sample->sectorsize, ss));
626 SET32(meta, hdr->Vendor_Specific_Logs,
627 howmany(GET32(sample, hdr->bbmlog_length) *
628 sample->sectorsize, ss));
631 SET32(meta, hdr->Signature, DDF_HEADER_SIGNATURE);
632 snprintf(meta->hdr->DDF_Header_GUID, 25, "FreeBSD %08x%08x",
633 (u_int)(ts.tv_sec - DECADE), arc4random());
634 memcpy(meta->hdr->DDF_rev, "02.00.00", 8);
635 SET32(meta, hdr->TimeStamp, (ts.tv_sec - DECADE));
636 SET32(meta, hdr->WorkSpace_Length, 16 * 1024 * 1024 / ss);
637 SET16(meta, hdr->Max_PD_Entries, DDF_MAX_DISKS - 1);
638 SET16(meta, hdr->Max_VD_Entries, DDF_MAX_VDISKS);
639 SET16(meta, hdr->Max_Partitions, DDF_MAX_PARTITIONS);
640 SET16(meta, hdr->Max_Primary_Element_Entries, DDF_MAX_DISKS);
641 SET16(meta, hdr->Configuration_Record_Length,
642 howmany(sizeof(struct ddf_vdc_record) + (4 + 8) *
643 GET16(meta, hdr->Max_Primary_Element_Entries), ss));
644 SET32(meta, hdr->cd_length,
645 howmany(sizeof(struct ddf_cd_record), ss));
646 SET32(meta, hdr->pdr_length,
647 howmany(sizeof(struct ddf_pd_record) +
648 sizeof(struct ddf_pd_entry) * GET16(meta,
649 hdr->Max_PD_Entries), ss));
650 SET32(meta, hdr->vdr_length,
651 howmany(sizeof(struct ddf_vd_record) +
652 sizeof(struct ddf_vd_entry) *
653 GET16(meta, hdr->Max_VD_Entries), ss));
654 SET32(meta, hdr->cr_length,
655 GET16(meta, hdr->Configuration_Record_Length) *
656 (GET16(meta, hdr->Max_Partitions) + 1));
657 SET32(meta, hdr->pdd_length,
658 howmany(sizeof(struct ddf_pdd_record), ss));
659 SET32(meta, hdr->bbmlog_length, 0);
660 SET32(meta, hdr->Diagnostic_Space_Length, 0);
661 SET32(meta, hdr->Vendor_Specific_Logs_Length, 0);
664 SET32(meta, hdr->cd_section, pos);
665 pos += GET32(meta, hdr->cd_length);
666 SET32(meta, hdr->pdr_section, pos);
667 pos += GET32(meta, hdr->pdr_length);
668 SET32(meta, hdr->vdr_section, pos);
669 pos += GET32(meta, hdr->vdr_length);
670 SET32(meta, hdr->cr_section, pos);
671 pos += GET32(meta, hdr->cr_length);
672 SET32(meta, hdr->pdd_section, pos);
673 pos += GET32(meta, hdr->pdd_length);
674 SET32(meta, hdr->bbmlog_section,
675 GET32(meta, hdr->bbmlog_length) != 0 ? pos : 0xffffffff);
676 pos += GET32(meta, hdr->bbmlog_length);
677 SET32(meta, hdr->Diagnostic_Space,
678 GET32(meta, hdr->Diagnostic_Space_Length) != 0 ? pos : 0xffffffff);
679 pos += GET32(meta, hdr->Diagnostic_Space_Length);
680 SET32(meta, hdr->Vendor_Specific_Logs,
681 GET32(meta, hdr->Vendor_Specific_Logs_Length) != 0 ? pos : 0xffffffff);
682 pos += min(GET32(meta, hdr->Vendor_Specific_Logs_Length), 1);
683 SET64(meta, hdr->Primary_Header_LBA,
685 SET64(meta, hdr->Secondary_Header_LBA,
686 0xffffffffffffffffULL);
687 SET64(meta, hdr->WorkSpace_LBA,
688 anchorlba + 1 - 32 * 1024 * 1024 / ss);
690 /* Controller Data */
691 size = GET32(meta, hdr->cd_length) * ss;
692 meta->cdr = malloc(size, M_MD_DDF, M_WAITOK);
693 memset(meta->cdr, 0xff, size);
694 SET32(meta, cdr->Signature, DDF_CONTROLLER_DATA_SIGNATURE);
695 memcpy(meta->cdr->Controller_GUID, "FreeBSD GEOM RAID SERIAL", 24);
696 memcpy(meta->cdr->Product_ID, "FreeBSD GEOMRAID", 16);
698 /* Physical Drive Records. */
699 size = GET32(meta, hdr->pdr_length) * ss;
700 meta->pdr = malloc(size, M_MD_DDF, M_WAITOK);
701 memset(meta->pdr, 0xff, size);
702 SET32(meta, pdr->Signature, DDF_PDR_SIGNATURE);
703 SET16(meta, pdr->Populated_PDEs, 1);
704 SET16(meta, pdr->Max_PDE_Supported,
705 GET16(meta, hdr->Max_PD_Entries));
707 pde = &meta->pdr->entry[0];
708 len = sizeof(serial_buffer);
709 error = g_io_getattr("GEOM::ident", disk->d_consumer, &len, serial_buffer);
710 if (error == 0 && (len = strlen (serial_buffer)) >= 6 && len <= 20)
711 snprintf(pde->PD_GUID, 25, "DISK%20s", serial_buffer);
713 snprintf(pde->PD_GUID, 25, "DISK%04d%02d%02d%08x%04x",
714 ct.year, ct.mon, ct.day,
715 arc4random(), arc4random() & 0xffff);
716 SET32D(meta, pde->PD_Reference, arc4random());
717 SET16D(meta, pde->PD_Type, DDF_PDE_GUID_FORCE);
718 SET16D(meta, pde->PD_State, 0);
719 SET64D(meta, pde->Configured_Size,
720 anchorlba + 1 - 32 * 1024 * 1024 / ss);
721 SET16D(meta, pde->Block_Size, ss);
723 /* Virtual Drive Records. */
724 size = GET32(meta, hdr->vdr_length) * ss;
725 meta->vdr = malloc(size, M_MD_DDF, M_WAITOK);
726 memset(meta->vdr, 0xff, size);
727 SET32(meta, vdr->Signature, DDF_VD_RECORD_SIGNATURE);
728 SET32(meta, vdr->Populated_VDEs, 0);
729 SET16(meta, vdr->Max_VDE_Supported,
730 GET16(meta, hdr->Max_VD_Entries));
732 /* Configuration Records. */
733 size = GET32(meta, hdr->cr_length) * ss;
734 meta->cr = malloc(size, M_MD_DDF, M_WAITOK);
735 memset(meta->cr, 0xff, size);
737 /* Physical Disk Data. */
738 size = GET32(meta, hdr->pdd_length) * ss;
739 meta->pdd = malloc(size, M_MD_DDF, M_WAITOK);
740 memset(meta->pdd, 0xff, size);
741 SET32(meta, pdd->Signature, DDF_PDD_SIGNATURE);
742 memcpy(meta->pdd->PD_GUID, pde->PD_GUID, 24);
743 SET32(meta, pdd->PD_Reference, GET32D(meta, pde->PD_Reference));
744 SET8(meta, pdd->Forced_Ref_Flag, DDF_PDD_FORCED_REF);
745 SET8(meta, pdd->Forced_PD_GUID_Flag, DDF_PDD_FORCED_GUID);
747 /* Bad Block Management Log. */
748 if (GET32(meta, hdr->bbmlog_length) != 0) {
749 size = GET32(meta, hdr->bbmlog_length) * ss;
750 meta->bbm = malloc(size, M_MD_DDF, M_WAITOK);
751 memset(meta->bbm, 0xff, size);
752 SET32(meta, bbm->Signature, DDF_BBML_SIGNATURE);
753 SET32(meta, bbm->Entry_Count, 0);
754 SET32(meta, bbm->Spare_Block_Count, 0);
759 ddf_meta_copy(struct ddf_meta *dst, struct ddf_meta *src)
763 dst->bigendian = src->bigendian;
764 ss = dst->sectorsize = src->sectorsize;
765 dst->hdr = malloc(ss, M_MD_DDF, M_WAITOK);
766 memcpy(dst->hdr, src->hdr, ss);
767 dst->cdr = malloc(GET32(src, hdr->cd_length) * ss, M_MD_DDF, M_WAITOK);
768 memcpy(dst->cdr, src->cdr, GET32(src, hdr->cd_length) * ss);
769 dst->pdr = malloc(GET32(src, hdr->pdr_length) * ss, M_MD_DDF, M_WAITOK);
770 memcpy(dst->pdr, src->pdr, GET32(src, hdr->pdr_length) * ss);
771 dst->vdr = malloc(GET32(src, hdr->vdr_length) * ss, M_MD_DDF, M_WAITOK);
772 memcpy(dst->vdr, src->vdr, GET32(src, hdr->vdr_length) * ss);
773 dst->cr = malloc(GET32(src, hdr->cr_length) * ss, M_MD_DDF, M_WAITOK);
774 memcpy(dst->cr, src->cr, GET32(src, hdr->cr_length) * ss);
775 dst->pdd = malloc(GET32(src, hdr->pdd_length) * ss, M_MD_DDF, M_WAITOK);
776 memcpy(dst->pdd, src->pdd, GET32(src, hdr->pdd_length) * ss);
777 if (src->bbm != NULL) {
778 dst->bbm = malloc(GET32(src, hdr->bbmlog_length) * ss, M_MD_DDF, M_WAITOK);
779 memcpy(dst->bbm, src->bbm, GET32(src, hdr->bbmlog_length) * ss);
784 ddf_meta_update(struct ddf_meta *meta, struct ddf_meta *src)
786 struct ddf_pd_entry *pde, *spde;
789 for (i = 0; i < GET16(src, pdr->Populated_PDEs); i++) {
790 spde = &src->pdr->entry[i];
791 if (isff(spde->PD_GUID, 24))
793 j = ddf_meta_find_pd(meta, NULL,
794 GET32(src, pdr->entry[i].PD_Reference));
796 j = ddf_meta_find_pd(meta, NULL, 0xffffffff);
797 pde = &meta->pdr->entry[j];
798 memcpy(pde, spde, sizeof(*pde));
800 pde = &meta->pdr->entry[j];
801 SET16D(meta, pde->PD_State,
802 GET16D(meta, pde->PD_State) |
803 GET16D(src, pde->PD_State));
809 ddf_meta_free(struct ddf_meta *meta)
812 if (meta->hdr != NULL) {
813 free(meta->hdr, M_MD_DDF);
816 if (meta->cdr != NULL) {
817 free(meta->cdr, M_MD_DDF);
820 if (meta->pdr != NULL) {
821 free(meta->pdr, M_MD_DDF);
824 if (meta->vdr != NULL) {
825 free(meta->vdr, M_MD_DDF);
828 if (meta->cr != NULL) {
829 free(meta->cr, M_MD_DDF);
832 if (meta->pdd != NULL) {
833 free(meta->pdd, M_MD_DDF);
836 if (meta->bbm != NULL) {
837 free(meta->bbm, M_MD_DDF);
843 ddf_vol_meta_create(struct ddf_vol_meta *meta, struct ddf_meta *sample)
849 meta->bigendian = sample->bigendian;
850 ss = meta->sectorsize = sample->sectorsize;
851 meta->hdr = malloc(ss, M_MD_DDF, M_WAITOK);
852 memcpy(meta->hdr, sample->hdr, ss);
853 meta->cdr = malloc(GET32(sample, hdr->cd_length) * ss, M_MD_DDF, M_WAITOK);
854 memcpy(meta->cdr, sample->cdr, GET32(sample, hdr->cd_length) * ss);
855 meta->vde = malloc(sizeof(struct ddf_vd_entry), M_MD_DDF, M_WAITOK);
856 memset(meta->vde, 0xff, sizeof(struct ddf_vd_entry));
858 clock_ts_to_ct(&ts, &ct);
859 snprintf(meta->vde->VD_GUID, 25, "FreeBSD%04d%02d%02d%08x%01x",
860 ct.year, ct.mon, ct.day,
861 arc4random(), arc4random() & 0xf);
862 size = GET16(sample, hdr->Configuration_Record_Length) * ss;
863 meta->vdc = malloc(size, M_MD_DDF, M_WAITOK);
864 memset(meta->vdc, 0xff, size);
865 SET32(meta, vdc->Signature, DDF_VDCR_SIGNATURE);
866 memcpy(meta->vdc->VD_GUID, meta->vde->VD_GUID, 24);
867 SET32(meta, vdc->Sequence_Number, 0);
871 ddf_vol_meta_update(struct ddf_vol_meta *dst, struct ddf_meta *src,
872 uint8_t *GUID, int started)
874 struct ddf_vd_entry *vde;
875 struct ddf_vdc_record *vdc;
876 int vnew, bvnew, bvd, size;
879 vde = &src->vdr->entry[ddf_meta_find_vd(src, GUID)];
880 vdc = ddf_meta_find_vdc(src, GUID);
881 if (GET8D(src, vdc->Secondary_Element_Count) == 1)
884 bvd = GET8D(src, vdc->Secondary_Element_Seq);
885 size = GET16(src, hdr->Configuration_Record_Length) * src->sectorsize;
887 if (dst->vdc == NULL ||
888 (!started && ((int32_t)(GET32D(src, vdc->Sequence_Number) -
889 GET32(dst, vdc->Sequence_Number))) > 0))
894 if (dst->bvdc[bvd] == NULL ||
895 (!started && ((int32_t)(GET32D(src, vdc->Sequence_Number) -
896 GET32(dst, bvdc[bvd]->Sequence_Number))) > 0))
902 dst->bigendian = src->bigendian;
903 ss = dst->sectorsize = src->sectorsize;
904 if (dst->hdr != NULL)
905 free(dst->hdr, M_MD_DDF);
906 dst->hdr = malloc(ss, M_MD_DDF, M_WAITOK);
907 memcpy(dst->hdr, src->hdr, ss);
908 if (dst->cdr != NULL)
909 free(dst->cdr, M_MD_DDF);
910 dst->cdr = malloc(GET32(src, hdr->cd_length) * ss, M_MD_DDF, M_WAITOK);
911 memcpy(dst->cdr, src->cdr, GET32(src, hdr->cd_length) * ss);
912 if (dst->vde != NULL)
913 free(dst->vde, M_MD_DDF);
914 dst->vde = malloc(sizeof(struct ddf_vd_entry), M_MD_DDF, M_WAITOK);
915 memcpy(dst->vde, vde, sizeof(struct ddf_vd_entry));
916 if (dst->vdc != NULL)
917 free(dst->vdc, M_MD_DDF);
918 dst->vdc = malloc(size, M_MD_DDF, M_WAITOK);
919 memcpy(dst->vdc, vdc, size);
922 if (dst->bvdc[bvd] != NULL)
923 free(dst->bvdc[bvd], M_MD_DDF);
924 dst->bvdc[bvd] = malloc(size, M_MD_DDF, M_WAITOK);
925 memcpy(dst->bvdc[bvd], vdc, size);
930 ddf_vol_meta_free(struct ddf_vol_meta *meta)
934 if (meta->hdr != NULL) {
935 free(meta->hdr, M_MD_DDF);
938 if (meta->cdr != NULL) {
939 free(meta->cdr, M_MD_DDF);
942 if (meta->vde != NULL) {
943 free(meta->vde, M_MD_DDF);
946 if (meta->vdc != NULL) {
947 free(meta->vdc, M_MD_DDF);
950 for (i = 0; i < DDF_MAX_DISKS_HARD; i++) {
951 if (meta->bvdc[i] != NULL) {
952 free(meta->bvdc[i], M_MD_DDF);
953 meta->bvdc[i] = NULL;
959 ddf_meta_unused_range(struct ddf_meta *meta, off_t *off, off_t *size)
961 struct ddf_vdc_record *vdc;
962 off_t beg[32], end[32], beg1, end1;
964 int i, j, n, num, pos;
969 ref = GET32(meta, pdd->PD_Reference);
970 pos = ddf_meta_find_pd(meta, NULL, ref);
972 end[0] = GET64(meta, pdr->entry[pos].Configured_Size);
974 num = GETCRNUM(meta);
975 for (i = 0; i < num; i++) {
976 vdc = GETVDCPTR(meta, i);
977 if (GET32D(meta, vdc->Signature) != DDF_VDCR_SIGNATURE)
979 for (pos = 0; pos < GET16D(meta, vdc->Primary_Element_Count); pos++)
980 if (GET32D(meta, vdc->Physical_Disk_Sequence[pos]) == ref)
982 if (pos == GET16D(meta, vdc->Primary_Element_Count))
984 offp = (uint64_t *)&(vdc->Physical_Disk_Sequence[
985 GET16(meta, hdr->Max_Primary_Element_Entries)]);
986 beg1 = GET64P(meta, offp + pos);
987 end1 = beg1 + GET64D(meta, vdc->Block_Count);
988 for (j = 0; j < n; j++) {
989 if (beg[j] >= end1 || end[j] <= beg1 )
991 if (beg[j] < beg1 && end[j] > end1) {
996 } else if (beg[j] < beg1)
1002 for (j = 0; j < n; j++) {
1003 if (end[j] - beg[j] > *size) {
1005 *size = end[j] - beg[j];
1008 return ((*size > 0) ? 1 : 0);
1012 ddf_meta_get_name(struct ddf_meta *meta, int num, char *buf)
1017 b = meta->vdr->entry[num].VD_Name;
1018 for (i = 15; i >= 0; i--)
1021 memcpy(buf, b, i + 1);
1026 ddf_meta_put_name(struct ddf_vol_meta *meta, char *buf)
1030 len = min(strlen(buf), 16);
1031 memset(meta->vde->VD_Name, 0x20, 16);
1032 memcpy(meta->vde->VD_Name, buf, len);
1036 ddf_meta_read(struct g_consumer *cp, struct ddf_meta *meta)
1038 struct g_provider *pp;
1039 struct ddf_header *ahdr, *hdr;
1041 off_t plba, slba, lba;
1046 ddf_meta_free(meta);
1048 ss = meta->sectorsize = pp->sectorsize;
1049 /* Read anchor block. */
1050 abuf = g_read_data(cp, pp->mediasize - ss, ss, &error);
1052 G_RAID_DEBUG(1, "Cannot read metadata from %s (error=%d).",
1056 ahdr = (struct ddf_header *)abuf;
1058 /* Check if this is an DDF RAID struct */
1059 if (be32dec(&ahdr->Signature) == DDF_HEADER_SIGNATURE)
1060 meta->bigendian = 1;
1061 else if (le32dec(&ahdr->Signature) == DDF_HEADER_SIGNATURE)
1062 meta->bigendian = 0;
1064 G_RAID_DEBUG(1, "DDF signature check failed on %s", pp->name);
1068 if (ahdr->Header_Type != DDF_HEADER_ANCHOR) {
1069 G_RAID_DEBUG(1, "DDF header type check failed on %s", pp->name);
1074 plba = GET64(meta, hdr->Primary_Header_LBA);
1075 slba = GET64(meta, hdr->Secondary_Header_LBA);
1076 val = GET32(meta, hdr->CRC);
1077 SET32(meta, hdr->CRC, 0xffffffff);
1079 if (crc32(ahdr, ss) != val) {
1080 G_RAID_DEBUG(1, "DDF CRC mismatch on %s", pp->name);
1084 if ((plba + 6) * ss >= pp->mediasize) {
1085 G_RAID_DEBUG(1, "DDF primary header LBA is wrong on %s", pp->name);
1089 if (slba != -1 && (slba + 6) * ss >= pp->mediasize) {
1090 G_RAID_DEBUG(1, "DDF secondary header LBA is wrong on %s", pp->name);
1098 ddf_meta_free(meta);
1100 /* Read header block. */
1101 buf = g_read_data(cp, lba * ss, ss, &error);
1104 G_RAID_DEBUG(1, "DDF %s metadata read error on %s (error=%d).",
1105 (lba == plba) ? "primary" : "secondary", pp->name, error);
1106 if (lba == plba && slba != -1) {
1110 G_RAID_DEBUG(1, "DDF metadata read error on %s.", pp->name);
1113 meta->hdr = malloc(ss, M_MD_DDF, M_WAITOK);
1114 memcpy(meta->hdr, buf, ss);
1117 val = GET32(meta, hdr->CRC);
1118 SET32(meta, hdr->CRC, 0xffffffff);
1119 if (hdr->Signature != ahdr->Signature ||
1120 crc32(meta->hdr, ss) != val ||
1121 memcmp(hdr->DDF_Header_GUID, ahdr->DDF_Header_GUID, 24) ||
1122 GET64(meta, hdr->Primary_Header_LBA) != plba ||
1123 GET64(meta, hdr->Secondary_Header_LBA) != slba) {
1125 G_RAID_DEBUG(1, "DDF %s metadata check failed on %s",
1126 (lba == plba) ? "primary" : "secondary", pp->name);
1127 if (lba == plba && slba != -1) {
1131 G_RAID_DEBUG(1, "DDF metadata check failed on %s", pp->name);
1135 if ((lba == plba && hdr->Header_Type != DDF_HEADER_PRIMARY) ||
1136 (lba == slba && hdr->Header_Type != DDF_HEADER_SECONDARY))
1139 len = max(len, GET32(meta, hdr->cd_section) + GET32(meta, hdr->cd_length));
1140 len = max(len, GET32(meta, hdr->pdr_section) + GET32(meta, hdr->pdr_length));
1141 len = max(len, GET32(meta, hdr->vdr_section) + GET32(meta, hdr->vdr_length));
1142 len = max(len, GET32(meta, hdr->cr_section) + GET32(meta, hdr->cr_length));
1143 len = max(len, GET32(meta, hdr->pdd_section) + GET32(meta, hdr->pdd_length));
1144 if ((val = GET32(meta, hdr->bbmlog_section)) != 0xffffffff)
1145 len = max(len, val + GET32(meta, hdr->bbmlog_length));
1146 if ((val = GET32(meta, hdr->Diagnostic_Space)) != 0xffffffff)
1147 len = max(len, val + GET32(meta, hdr->Diagnostic_Space_Length));
1148 if ((val = GET32(meta, hdr->Vendor_Specific_Logs)) != 0xffffffff)
1149 len = max(len, val + GET32(meta, hdr->Vendor_Specific_Logs_Length));
1150 if ((plba + len) * ss >= pp->mediasize)
1152 if (slba != -1 && (slba + len) * ss >= pp->mediasize)
1154 /* Workaround for Adaptec implementation. */
1155 if (GET16(meta, hdr->Max_Primary_Element_Entries) == 0xffff) {
1156 SET16(meta, hdr->Max_Primary_Element_Entries,
1157 min(GET16(meta, hdr->Max_PD_Entries),
1158 (GET16(meta, hdr->Configuration_Record_Length) * ss - 512) / 12));
1161 if (GET32(meta, hdr->cd_length) * ss >= MAXPHYS ||
1162 GET32(meta, hdr->pdr_length) * ss >= MAXPHYS ||
1163 GET32(meta, hdr->vdr_length) * ss >= MAXPHYS ||
1164 GET32(meta, hdr->cr_length) * ss >= MAXPHYS ||
1165 GET32(meta, hdr->pdd_length) * ss >= MAXPHYS ||
1166 GET32(meta, hdr->bbmlog_length) * ss >= MAXPHYS) {
1167 G_RAID_DEBUG(1, "%s: Blocksize is too big.", pp->name);
1171 /* Read controller data. */
1172 buf = g_read_data(cp, (lba + GET32(meta, hdr->cd_section)) * ss,
1173 GET32(meta, hdr->cd_length) * ss, &error);
1176 meta->cdr = malloc(GET32(meta, hdr->cd_length) * ss, M_MD_DDF, M_WAITOK);
1177 memcpy(meta->cdr, buf, GET32(meta, hdr->cd_length) * ss);
1179 if (GET32(meta, cdr->Signature) != DDF_CONTROLLER_DATA_SIGNATURE)
1182 /* Read physical disk records. */
1183 buf = g_read_data(cp, (lba + GET32(meta, hdr->pdr_section)) * ss,
1184 GET32(meta, hdr->pdr_length) * ss, &error);
1187 meta->pdr = malloc(GET32(meta, hdr->pdr_length) * ss, M_MD_DDF, M_WAITOK);
1188 memcpy(meta->pdr, buf, GET32(meta, hdr->pdr_length) * ss);
1190 if (GET32(meta, pdr->Signature) != DDF_PDR_SIGNATURE)
1193 * Workaround for reading metadata corrupted due to graid bug.
1194 * XXX: Remove this before we have disks above 128PB. :)
1196 if (meta->bigendian) {
1197 for (i = 0; i < GET16(meta, pdr->Populated_PDEs); i++) {
1198 if (isff(meta->pdr->entry[i].PD_GUID, 24))
1200 if (GET32(meta, pdr->entry[i].PD_Reference) ==
1203 if (GET64(meta, pdr->entry[i].Configured_Size) >=
1205 SET16(meta, pdr->entry[i].PD_State,
1206 GET16(meta, pdr->entry[i].PD_State) &
1208 SET64(meta, pdr->entry[i].Configured_Size,
1209 GET64(meta, pdr->entry[i].Configured_Size) &
1210 ((1ULL << 48) - 1));
1215 /* Read virtual disk records. */
1216 buf = g_read_data(cp, (lba + GET32(meta, hdr->vdr_section)) * ss,
1217 GET32(meta, hdr->vdr_length) * ss, &error);
1220 meta->vdr = malloc(GET32(meta, hdr->vdr_length) * ss, M_MD_DDF, M_WAITOK);
1221 memcpy(meta->vdr, buf, GET32(meta, hdr->vdr_length) * ss);
1223 if (GET32(meta, vdr->Signature) != DDF_VD_RECORD_SIGNATURE)
1226 /* Read configuration records. */
1227 buf = g_read_data(cp, (lba + GET32(meta, hdr->cr_section)) * ss,
1228 GET32(meta, hdr->cr_length) * ss, &error);
1231 meta->cr = malloc(GET32(meta, hdr->cr_length) * ss, M_MD_DDF, M_WAITOK);
1232 memcpy(meta->cr, buf, GET32(meta, hdr->cr_length) * ss);
1235 /* Read physical disk data. */
1236 buf = g_read_data(cp, (lba + GET32(meta, hdr->pdd_section)) * ss,
1237 GET32(meta, hdr->pdd_length) * ss, &error);
1240 meta->pdd = malloc(GET32(meta, hdr->pdd_length) * ss, M_MD_DDF, M_WAITOK);
1241 memcpy(meta->pdd, buf, GET32(meta, hdr->pdd_length) * ss);
1243 if (GET32(meta, pdd->Signature) != DDF_PDD_SIGNATURE)
1245 i = ddf_meta_find_pd(meta, NULL, GET32(meta, pdd->PD_Reference));
1250 if (GET32(meta, hdr->bbmlog_section) != 0xffffffff &&
1251 GET32(meta, hdr->bbmlog_length) != 0) {
1252 buf = g_read_data(cp, (lba + GET32(meta, hdr->bbmlog_section)) * ss,
1253 GET32(meta, hdr->bbmlog_length) * ss, &error);
1256 meta->bbm = malloc(GET32(meta, hdr->bbmlog_length) * ss, M_MD_DDF, M_WAITOK);
1257 memcpy(meta->bbm, buf, GET32(meta, hdr->bbmlog_length) * ss);
1259 if (GET32(meta, bbm->Signature) != DDF_BBML_SIGNATURE)
1266 ddf_meta_free(meta);
1271 ddf_meta_write(struct g_consumer *cp, struct ddf_meta *meta)
1273 struct g_provider *pp;
1274 struct ddf_vdc_record *vdc;
1275 off_t alba, plba, slba, lba;
1280 ss = pp->sectorsize;
1281 lba = alba = pp->mediasize / ss - 1;
1282 plba = GET64(meta, hdr->Primary_Header_LBA);
1283 slba = GET64(meta, hdr->Secondary_Header_LBA);
1286 SET8(meta, hdr->Header_Type, (lba == alba) ? DDF_HEADER_ANCHOR :
1287 (lba == plba) ? DDF_HEADER_PRIMARY : DDF_HEADER_SECONDARY);
1288 SET32(meta, hdr->CRC, 0xffffffff);
1289 SET32(meta, hdr->CRC, crc32(meta->hdr, ss));
1290 error = g_write_data(cp, lba * ss, meta->hdr, ss);
1293 G_RAID_DEBUG(1, "Cannot write metadata to %s (error=%d).",
1303 size = GET32(meta, hdr->cd_length) * ss;
1304 SET32(meta, cdr->CRC, 0xffffffff);
1305 SET32(meta, cdr->CRC, crc32(meta->cdr, size));
1306 error = g_write_data(cp, (lba + GET32(meta, hdr->cd_section)) * ss,
1311 size = GET32(meta, hdr->pdr_length) * ss;
1312 SET32(meta, pdr->CRC, 0xffffffff);
1313 SET32(meta, pdr->CRC, crc32(meta->pdr, size));
1314 error = g_write_data(cp, (lba + GET32(meta, hdr->pdr_section)) * ss,
1319 size = GET32(meta, hdr->vdr_length) * ss;
1320 SET32(meta, vdr->CRC, 0xffffffff);
1321 SET32(meta, vdr->CRC, crc32(meta->vdr, size));
1322 error = g_write_data(cp, (lba + GET32(meta, hdr->vdr_section)) * ss,
1327 size = GET16(meta, hdr->Configuration_Record_Length) * ss;
1328 num = GETCRNUM(meta);
1329 for (i = 0; i < num; i++) {
1330 vdc = GETVDCPTR(meta, i);
1331 SET32D(meta, vdc->CRC, 0xffffffff);
1332 SET32D(meta, vdc->CRC, crc32(vdc, size));
1334 error = g_write_data(cp, (lba + GET32(meta, hdr->cr_section)) * ss,
1335 meta->cr, size * num);
1339 size = GET32(meta, hdr->pdd_length) * ss;
1340 SET32(meta, pdd->CRC, 0xffffffff);
1341 SET32(meta, pdd->CRC, crc32(meta->pdd, size));
1342 error = g_write_data(cp, (lba + GET32(meta, hdr->pdd_section)) * ss,
1347 if (GET32(meta, hdr->bbmlog_length) != 0) {
1348 size = GET32(meta, hdr->bbmlog_length) * ss;
1349 SET32(meta, bbm->CRC, 0xffffffff);
1350 SET32(meta, bbm->CRC, crc32(meta->bbm, size));
1351 error = g_write_data(cp,
1352 (lba + GET32(meta, hdr->bbmlog_section)) * ss,
1359 if (lba == plba && slba != -1) {
1368 ddf_meta_erase(struct g_consumer *cp)
1370 struct g_provider *pp;
1375 buf = malloc(pp->sectorsize, M_MD_DDF, M_WAITOK | M_ZERO);
1376 error = g_write_data(cp, pp->mediasize - pp->sectorsize,
1377 buf, pp->sectorsize);
1379 G_RAID_DEBUG(1, "Cannot erase metadata on %s (error=%d).",
1382 free(buf, M_MD_DDF);
1386 static struct g_raid_volume *
1387 g_raid_md_ddf_get_volume(struct g_raid_softc *sc, uint8_t *GUID)
1389 struct g_raid_volume *vol;
1390 struct g_raid_md_ddf_pervolume *pv;
1392 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
1393 pv = vol->v_md_data;
1394 if (memcmp(pv->pv_meta.vde->VD_GUID, GUID, 24) == 0)
1400 static struct g_raid_disk *
1401 g_raid_md_ddf_get_disk(struct g_raid_softc *sc, uint8_t *GUID, uint32_t id)
1403 struct g_raid_disk *disk;
1404 struct g_raid_md_ddf_perdisk *pd;
1405 struct ddf_meta *meta;
1407 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1408 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
1409 meta = &pd->pd_meta;
1411 if (memcmp(meta->pdd->PD_GUID, GUID, 24) == 0)
1414 if (GET32(meta, pdd->PD_Reference) == id)
1422 g_raid_md_ddf_purge_volumes(struct g_raid_softc *sc)
1424 struct g_raid_volume *vol, *tvol;
1428 TAILQ_FOREACH_SAFE(vol, &sc->sc_volumes, v_next, tvol) {
1429 if (vol->v_stopping)
1431 for (i = 0; i < vol->v_disks_count; i++) {
1432 if (vol->v_subdisks[i].sd_state != G_RAID_SUBDISK_S_NONE)
1435 if (i >= vol->v_disks_count) {
1436 g_raid_destroy_volume(vol);
1444 g_raid_md_ddf_purge_disks(struct g_raid_softc *sc)
1447 struct g_raid_disk *disk, *tdisk;
1448 struct g_raid_volume *vol;
1449 struct g_raid_md_ddf_perdisk *pd;
1453 TAILQ_FOREACH_SAFE(disk, &sc->sc_disks, d_next, tdisk) {
1454 if (disk->d_state == G_RAID_DISK_S_SPARE)
1456 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
1458 /* Scan for deleted volumes. */
1459 for (i = 0; i < pd->pd_subdisks; ) {
1460 vol = g_raid_md_ddf_get_volume(sc,
1461 pd->pd_meta[i]->volume_id);
1462 if (vol != NULL && !vol->v_stopping) {
1466 free(pd->pd_meta[i], M_MD_DDF);
1467 for (j = i; j < pd->pd_subdisks - 1; j++)
1468 pd->pd_meta[j] = pd->pd_meta[j + 1];
1469 pd->pd_meta[DDF_MAX_SUBDISKS - 1] = NULL;
1474 /* If there is no metadata left - erase and delete disk. */
1475 if (pd->pd_subdisks == 0) {
1476 ddf_meta_erase(disk->d_consumer);
1477 g_raid_destroy_disk(disk);
1487 g_raid_md_ddf_supported(int level, int qual, int disks, int force)
1490 if (disks > DDF_MAX_DISKS_HARD)
1493 case G_RAID_VOLUME_RL_RAID0:
1494 if (qual != G_RAID_VOLUME_RLQ_NONE)
1498 if (!force && disks < 2)
1501 case G_RAID_VOLUME_RL_RAID1:
1504 if (qual == G_RAID_VOLUME_RLQ_R1SM) {
1505 if (!force && disks != 2)
1507 } else if (qual == G_RAID_VOLUME_RLQ_R1MM) {
1508 if (!force && disks != 3)
1513 case G_RAID_VOLUME_RL_RAID3:
1514 if (qual != G_RAID_VOLUME_RLQ_R3P0 &&
1515 qual != G_RAID_VOLUME_RLQ_R3PN)
1520 case G_RAID_VOLUME_RL_RAID4:
1521 if (qual != G_RAID_VOLUME_RLQ_R4P0 &&
1522 qual != G_RAID_VOLUME_RLQ_R4PN)
1527 case G_RAID_VOLUME_RL_RAID5:
1528 if (qual != G_RAID_VOLUME_RLQ_R5RA &&
1529 qual != G_RAID_VOLUME_RLQ_R5RS &&
1530 qual != G_RAID_VOLUME_RLQ_R5LA &&
1531 qual != G_RAID_VOLUME_RLQ_R5LS)
1536 case G_RAID_VOLUME_RL_RAID6:
1537 if (qual != G_RAID_VOLUME_RLQ_R6RA &&
1538 qual != G_RAID_VOLUME_RLQ_R6RS &&
1539 qual != G_RAID_VOLUME_RLQ_R6LA &&
1540 qual != G_RAID_VOLUME_RLQ_R6LS)
1545 case G_RAID_VOLUME_RL_RAIDMDF:
1546 if (qual != G_RAID_VOLUME_RLQ_RMDFRA &&
1547 qual != G_RAID_VOLUME_RLQ_RMDFRS &&
1548 qual != G_RAID_VOLUME_RLQ_RMDFLA &&
1549 qual != G_RAID_VOLUME_RLQ_RMDFLS)
1554 case G_RAID_VOLUME_RL_RAID1E:
1555 if (qual != G_RAID_VOLUME_RLQ_R1EA &&
1556 qual != G_RAID_VOLUME_RLQ_R1EO)
1561 case G_RAID_VOLUME_RL_SINGLE:
1562 if (qual != G_RAID_VOLUME_RLQ_NONE)
1567 case G_RAID_VOLUME_RL_CONCAT:
1568 if (qual != G_RAID_VOLUME_RLQ_NONE)
1573 case G_RAID_VOLUME_RL_RAID5E:
1574 if (qual != G_RAID_VOLUME_RLQ_R5ERA &&
1575 qual != G_RAID_VOLUME_RLQ_R5ERS &&
1576 qual != G_RAID_VOLUME_RLQ_R5ELA &&
1577 qual != G_RAID_VOLUME_RLQ_R5ELS)
1582 case G_RAID_VOLUME_RL_RAID5EE:
1583 if (qual != G_RAID_VOLUME_RLQ_R5EERA &&
1584 qual != G_RAID_VOLUME_RLQ_R5EERS &&
1585 qual != G_RAID_VOLUME_RLQ_R5EELA &&
1586 qual != G_RAID_VOLUME_RLQ_R5EELS)
1591 case G_RAID_VOLUME_RL_RAID5R:
1592 if (qual != G_RAID_VOLUME_RLQ_R5RRA &&
1593 qual != G_RAID_VOLUME_RLQ_R5RRS &&
1594 qual != G_RAID_VOLUME_RLQ_R5RLA &&
1595 qual != G_RAID_VOLUME_RLQ_R5RLS)
1607 g_raid_md_ddf_start_disk(struct g_raid_disk *disk, struct g_raid_volume *vol)
1609 struct g_raid_softc *sc;
1610 struct g_raid_subdisk *sd;
1611 struct g_raid_md_ddf_perdisk *pd;
1612 struct g_raid_md_ddf_pervolume *pv;
1613 struct g_raid_md_ddf_object *mdi;
1614 struct ddf_vol_meta *vmeta;
1615 struct ddf_meta *pdmeta, *gmeta;
1616 struct ddf_vdc_record *vdc1;
1617 struct ddf_sa_record *sa;
1618 off_t size, eoff = 0, esize = 0;
1620 int disk_pos, md_disk_bvd = -1, md_disk_pos = -1, md_pde_pos;
1621 int i, resurrection = 0;
1625 mdi = (struct g_raid_md_ddf_object *)sc->sc_md;
1626 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
1627 pdmeta = &pd->pd_meta;
1628 reference = GET32(&pd->pd_meta, pdd->PD_Reference);
1630 pv = vol->v_md_data;
1631 vmeta = &pv->pv_meta;
1632 gmeta = &mdi->mdio_meta;
1634 /* Find disk position in metadata by its reference. */
1635 disk_pos = ddf_meta_find_disk(vmeta, reference,
1636 &md_disk_bvd, &md_disk_pos);
1637 md_pde_pos = ddf_meta_find_pd(gmeta, NULL, reference);
1640 G_RAID_DEBUG1(1, sc,
1641 "Disk %s is not a present part of the volume %s",
1642 g_raid_get_diskname(disk), vol->v_name);
1644 /* Failed stale disk is useless for us. */
1645 if ((GET16(gmeta, pdr->entry[md_pde_pos].PD_State) & DDF_PDE_PFA) != 0) {
1646 g_raid_change_disk_state(disk, G_RAID_DISK_S_STALE_FAILED);
1650 /* If disk has some metadata for this volume - erase. */
1651 if ((vdc1 = ddf_meta_find_vdc(pdmeta, vmeta->vdc->VD_GUID)) != NULL)
1652 SET32D(pdmeta, vdc1->Signature, 0xffffffff);
1654 /* If we are in the start process, that's all for now. */
1655 if (!pv->pv_started)
1658 * If we have already started - try to get use of the disk.
1659 * Try to replace OFFLINE disks first, then FAILED.
1661 if (ddf_meta_count_vdc(&pd->pd_meta, NULL) >=
1662 GET16(&pd->pd_meta, hdr->Max_Partitions)) {
1663 G_RAID_DEBUG1(1, sc, "No free partitions on disk %s",
1664 g_raid_get_diskname(disk));
1667 ddf_meta_unused_range(&pd->pd_meta, &eoff, &esize);
1669 G_RAID_DEBUG1(1, sc, "No free space on disk %s",
1670 g_raid_get_diskname(disk));
1673 eoff *= pd->pd_meta.sectorsize;
1674 esize *= pd->pd_meta.sectorsize;
1676 for (i = 0; i < vol->v_disks_count; i++) {
1677 sd = &vol->v_subdisks[i];
1678 if (sd->sd_state != G_RAID_SUBDISK_S_NONE)
1680 if (sd->sd_state <= G_RAID_SUBDISK_S_FAILED &&
1682 vol->v_subdisks[i].sd_state < sd->sd_state))
1685 if (disk_pos >= 0 &&
1686 vol->v_raid_level != G_RAID_VOLUME_RL_CONCAT &&
1688 G_RAID_DEBUG1(1, sc, "Disk %s free space "
1689 "is too small (%ju < %ju)",
1690 g_raid_get_diskname(disk), esize, size);
1693 if (disk_pos >= 0) {
1694 if (vol->v_raid_level != G_RAID_VOLUME_RL_CONCAT)
1696 md_disk_bvd = disk_pos / GET16(vmeta, vdc->Primary_Element_Count); // XXX
1697 md_disk_pos = disk_pos % GET16(vmeta, vdc->Primary_Element_Count); // XXX
1700 if (disk->d_state == G_RAID_DISK_S_NONE)
1701 g_raid_change_disk_state(disk,
1702 G_RAID_DISK_S_STALE);
1707 * If spare is committable, delete spare record.
1708 * Othersize, mark it active and leave there.
1710 sa = ddf_meta_find_sa(&pd->pd_meta, 0);
1712 if ((GET8D(&pd->pd_meta, sa->Spare_Type) &
1713 DDF_SAR_TYPE_REVERTIBLE) == 0) {
1714 SET32D(&pd->pd_meta, sa->Signature, 0xffffffff);
1716 SET8D(&pd->pd_meta, sa->Spare_Type,
1717 GET8D(&pd->pd_meta, sa->Spare_Type) |
1718 DDF_SAR_TYPE_ACTIVE);
1722 G_RAID_DEBUG1(1, sc, "Disk %s takes pos %d in the volume %s",
1723 g_raid_get_diskname(disk), disk_pos, vol->v_name);
1727 sd = &vol->v_subdisks[disk_pos];
1729 if (resurrection && sd->sd_disk != NULL) {
1730 g_raid_change_disk_state(sd->sd_disk,
1731 G_RAID_DISK_S_STALE_FAILED);
1732 TAILQ_REMOVE(&sd->sd_disk->d_subdisks,
1735 vol->v_subdisks[disk_pos].sd_disk = disk;
1736 TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
1738 /* Welcome the new disk. */
1740 g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE);
1741 else if (GET16(gmeta, pdr->entry[md_pde_pos].PD_State) & DDF_PDE_PFA)
1742 g_raid_change_disk_state(disk, G_RAID_DISK_S_FAILED);
1744 g_raid_change_disk_state(disk, G_RAID_DISK_S_ACTIVE);
1747 sd->sd_offset = eoff;
1748 sd->sd_size = esize;
1749 } else if (pdmeta->cr != NULL &&
1750 (vdc1 = ddf_meta_find_vdc(pdmeta, vmeta->vdc->VD_GUID)) != NULL) {
1751 val2 = (uint64_t *)&(vdc1->Physical_Disk_Sequence[GET16(vmeta, hdr->Max_Primary_Element_Entries)]);
1752 sd->sd_offset = (off_t)GET64P(pdmeta, val2 + md_disk_pos) * 512;
1753 sd->sd_size = (off_t)GET64D(pdmeta, vdc1->Block_Count) * 512;
1757 /* Stale disk, almost same as new. */
1758 g_raid_change_subdisk_state(sd,
1759 G_RAID_SUBDISK_S_NEW);
1760 } else if (GET16(gmeta, pdr->entry[md_pde_pos].PD_State) & DDF_PDE_PFA) {
1762 g_raid_change_subdisk_state(sd,
1763 G_RAID_SUBDISK_S_FAILED);
1764 } else if ((GET16(gmeta, pdr->entry[md_pde_pos].PD_State) &
1765 (DDF_PDE_FAILED | DDF_PDE_REBUILD)) != 0) {
1766 /* Rebuilding disk. */
1767 g_raid_change_subdisk_state(sd,
1768 G_RAID_SUBDISK_S_REBUILD);
1769 sd->sd_rebuild_pos = 0;
1770 } else if ((GET8(vmeta, vde->VD_State) & DDF_VDE_DIRTY) != 0 ||
1771 (GET8(vmeta, vde->Init_State) & DDF_VDE_INIT_MASK) !=
1772 DDF_VDE_INIT_FULL) {
1773 /* Stale disk or dirty volume (unclean shutdown). */
1774 g_raid_change_subdisk_state(sd,
1775 G_RAID_SUBDISK_S_STALE);
1777 /* Up to date disk. */
1778 g_raid_change_subdisk_state(sd,
1779 G_RAID_SUBDISK_S_ACTIVE);
1781 g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW,
1782 G_RAID_EVENT_SUBDISK);
1784 return (resurrection);
1788 g_raid_md_ddf_refill(struct g_raid_softc *sc)
1790 struct g_raid_volume *vol;
1791 struct g_raid_subdisk *sd;
1792 struct g_raid_disk *disk;
1793 struct g_raid_md_object *md;
1794 struct g_raid_md_ddf_perdisk *pd;
1795 struct g_raid_md_ddf_pervolume *pv;
1796 int update, updated, i, bad;
1801 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
1802 pv = vol->v_md_data;
1803 if (!pv->pv_started || vol->v_stopping)
1806 /* Search for subdisk that needs replacement. */
1808 for (i = 0; i < vol->v_disks_count; i++) {
1809 sd = &vol->v_subdisks[i];
1810 if (sd->sd_state == G_RAID_SUBDISK_S_NONE ||
1811 sd->sd_state == G_RAID_SUBDISK_S_FAILED)
1817 G_RAID_DEBUG1(1, sc, "Volume %s is not complete, "
1818 "trying to refill.", vol->v_name);
1820 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1822 if (disk->d_state < G_RAID_DISK_S_SPARE)
1824 /* Skip already used by this volume. */
1825 for (i = 0; i < vol->v_disks_count; i++) {
1826 sd = &vol->v_subdisks[i];
1827 if (sd->sd_disk == disk)
1830 if (i < vol->v_disks_count)
1833 /* Try to use disk if it has empty extents. */
1834 pd = disk->d_md_data;
1835 if (ddf_meta_count_vdc(&pd->pd_meta, NULL) <
1836 GET16(&pd->pd_meta, hdr->Max_Partitions)) {
1837 update = g_raid_md_ddf_start_disk(disk, vol);
1842 g_raid_md_write_ddf(md, vol, NULL, disk);
1852 g_raid_md_ddf_start(struct g_raid_volume *vol)
1854 struct g_raid_softc *sc;
1855 struct g_raid_subdisk *sd;
1856 struct g_raid_disk *disk;
1857 struct g_raid_md_object *md;
1858 struct g_raid_md_ddf_perdisk *pd;
1859 struct g_raid_md_ddf_pervolume *pv;
1860 struct g_raid_md_ddf_object *mdi;
1861 struct ddf_vol_meta *vmeta;
1867 mdi = (struct g_raid_md_ddf_object *)md;
1868 pv = vol->v_md_data;
1869 vmeta = &pv->pv_meta;
1871 vol->v_raid_level = GET8(vmeta, vdc->Primary_RAID_Level);
1872 vol->v_raid_level_qualifier = GET8(vmeta, vdc->RLQ);
1873 if (GET8(vmeta, vdc->Secondary_Element_Count) > 1 &&
1874 vol->v_raid_level == G_RAID_VOLUME_RL_RAID1 &&
1875 GET8(vmeta, vdc->Secondary_RAID_Level) == 0)
1876 vol->v_raid_level = G_RAID_VOLUME_RL_RAID1E;
1877 vol->v_sectorsize = GET16(vmeta, vdc->Block_Size);
1878 if (vol->v_sectorsize == 0xffff)
1879 vol->v_sectorsize = vmeta->sectorsize;
1880 vol->v_strip_size = vol->v_sectorsize << GET8(vmeta, vdc->Stripe_Size);
1881 vol->v_disks_count = GET16(vmeta, vdc->Primary_Element_Count) *
1882 GET8(vmeta, vdc->Secondary_Element_Count);
1883 vol->v_mdf_pdisks = GET8(vmeta, vdc->MDF_Parity_Disks);
1884 vol->v_mdf_polynomial = GET16(vmeta, vdc->MDF_Parity_Generator_Polynomial);
1885 vol->v_mdf_method = GET8(vmeta, vdc->MDF_Constant_Generation_Method);
1886 if (GET8(vmeta, vdc->Rotate_Parity_count) > 31)
1887 vol->v_rotate_parity = 1;
1889 vol->v_rotate_parity = 1 << GET8(vmeta, vdc->Rotate_Parity_count);
1890 vol->v_mediasize = GET64(vmeta, vdc->VD_Size) * vol->v_sectorsize;
1891 for (i = 0, j = 0, bvd = 0; i < vol->v_disks_count; i++, j++) {
1892 if (j == GET16(vmeta, vdc->Primary_Element_Count)) {
1896 sd = &vol->v_subdisks[i];
1897 if (vmeta->bvdc[bvd] == NULL) {
1899 sd->sd_size = GET64(vmeta, vdc->Block_Count) *
1903 val2 = (uint64_t *)&(vmeta->bvdc[bvd]->Physical_Disk_Sequence[
1904 GET16(vmeta, hdr->Max_Primary_Element_Entries)]);
1905 sd->sd_offset = GET64P(vmeta, val2 + j) * vol->v_sectorsize;
1906 sd->sd_size = GET64(vmeta, bvdc[bvd]->Block_Count) *
1909 g_raid_start_volume(vol);
1911 /* Make all disks found till the moment take their places. */
1912 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1913 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
1914 if (ddf_meta_find_vdc(&pd->pd_meta, vmeta->vdc->VD_GUID) != NULL)
1915 g_raid_md_ddf_start_disk(disk, vol);
1919 mdi->mdio_starting--;
1920 callout_stop(&pv->pv_start_co);
1921 G_RAID_DEBUG1(0, sc, "Volume started.");
1922 g_raid_md_write_ddf(md, vol, NULL, NULL);
1924 /* Pickup any STALE/SPARE disks to refill array if needed. */
1925 g_raid_md_ddf_refill(sc);
1927 g_raid_event_send(vol, G_RAID_VOLUME_E_START, G_RAID_EVENT_VOLUME);
1931 g_raid_ddf_go(void *arg)
1933 struct g_raid_volume *vol;
1934 struct g_raid_softc *sc;
1935 struct g_raid_md_ddf_pervolume *pv;
1938 pv = vol->v_md_data;
1940 if (!pv->pv_started) {
1941 G_RAID_DEBUG1(0, sc, "Force volume start due to timeout.");
1942 g_raid_event_send(vol, G_RAID_VOLUME_E_STARTMD,
1943 G_RAID_EVENT_VOLUME);
1948 g_raid_md_ddf_new_disk(struct g_raid_disk *disk)
1950 struct g_raid_softc *sc;
1951 struct g_raid_md_object *md;
1952 struct g_raid_md_ddf_perdisk *pd;
1953 struct g_raid_md_ddf_pervolume *pv;
1954 struct g_raid_md_ddf_object *mdi;
1955 struct g_raid_volume *vol;
1956 struct ddf_meta *pdmeta;
1957 struct ddf_vol_meta *vmeta;
1958 struct ddf_vdc_record *vdc;
1959 struct ddf_vd_entry *vde;
1960 int i, j, k, num, have, need, cnt, spare;
1966 mdi = (struct g_raid_md_ddf_object *)md;
1967 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
1968 pdmeta = &pd->pd_meta;
1971 if (mdi->mdio_meta.hdr == NULL)
1972 ddf_meta_copy(&mdi->mdio_meta, pdmeta);
1974 ddf_meta_update(&mdi->mdio_meta, pdmeta);
1976 num = GETCRNUM(pdmeta);
1977 for (j = 0; j < num; j++) {
1978 vdc = GETVDCPTR(pdmeta, j);
1979 val = GET32D(pdmeta, vdc->Signature);
1981 if (val == DDF_SA_SIGNATURE && spare == -1)
1984 if (val != DDF_VDCR_SIGNATURE)
1987 k = ddf_meta_find_vd(pdmeta, vdc->VD_GUID);
1990 vde = &pdmeta->vdr->entry[k];
1992 /* Look for volume with matching ID. */
1993 vol = g_raid_md_ddf_get_volume(sc, vdc->VD_GUID);
1995 ddf_meta_get_name(pdmeta, k, buf);
1996 vol = g_raid_create_volume(sc, buf,
1997 GET16D(pdmeta, vde->VD_Number));
1998 pv = malloc(sizeof(*pv), M_MD_DDF, M_WAITOK | M_ZERO);
1999 vol->v_md_data = pv;
2000 callout_init(&pv->pv_start_co, 1);
2001 callout_reset(&pv->pv_start_co,
2002 g_raid_start_timeout * hz,
2003 g_raid_ddf_go, vol);
2004 mdi->mdio_starting++;
2006 pv = vol->v_md_data;
2008 /* If we haven't started yet - check metadata freshness. */
2009 vmeta = &pv->pv_meta;
2010 ddf_vol_meta_update(vmeta, pdmeta, vdc->VD_GUID, pv->pv_started);
2014 g_raid_change_disk_state(disk, G_RAID_DISK_S_SPARE);
2015 g_raid_md_ddf_refill(sc);
2018 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2019 pv = vol->v_md_data;
2020 vmeta = &pv->pv_meta;
2022 if (ddf_meta_find_vdc(pdmeta, vmeta->vdc->VD_GUID) == NULL)
2025 if (pv->pv_started) {
2026 if (g_raid_md_ddf_start_disk(disk, vol))
2027 g_raid_md_write_ddf(md, vol, NULL, NULL);
2031 /* If we collected all needed disks - start array. */
2034 for (k = 0; k < GET8(vmeta, vdc->Secondary_Element_Count); k++) {
2035 if (vmeta->bvdc[k] == NULL) {
2036 need += GET16(vmeta, vdc->Primary_Element_Count);
2039 cnt = GET16(vmeta, bvdc[k]->Primary_Element_Count);
2041 for (i = 0; i < cnt; i++) {
2042 val = GET32(vmeta, bvdc[k]->Physical_Disk_Sequence[i]);
2043 if (g_raid_md_ddf_get_disk(sc, NULL, val) != NULL)
2047 G_RAID_DEBUG1(1, sc, "Volume %s now has %d of %d disks",
2048 vol->v_name, have, need);
2050 g_raid_md_ddf_start(vol);
2055 g_raid_md_create_req_ddf(struct g_raid_md_object *md, struct g_class *mp,
2056 struct gctl_req *req, struct g_geom **gp)
2058 struct g_geom *geom;
2059 struct g_raid_softc *sc;
2060 struct g_raid_md_ddf_object *mdi, *mdi1;
2065 mdi = (struct g_raid_md_ddf_object *)md;
2066 fmtopt = gctl_get_asciiparam(req, "fmtopt");
2067 if (fmtopt == NULL || strcasecmp(fmtopt, "BE") == 0)
2069 else if (strcasecmp(fmtopt, "LE") == 0)
2072 gctl_error(req, "Incorrect fmtopt argument.");
2073 return (G_RAID_MD_TASTE_FAIL);
2076 /* Search for existing node. */
2077 LIST_FOREACH(geom, &mp->geom, geom) {
2081 if (sc->sc_stopping != 0)
2083 if (sc->sc_md->mdo_class != md->mdo_class)
2085 mdi1 = (struct g_raid_md_ddf_object *)sc->sc_md;
2086 if (mdi1->mdio_bigendian != be)
2092 return (G_RAID_MD_TASTE_EXISTING);
2095 /* Create new one if not found. */
2096 mdi->mdio_bigendian = be;
2097 snprintf(name, sizeof(name), "DDF%s", be ? "" : "-LE");
2098 sc = g_raid_create_node(mp, name, md);
2100 return (G_RAID_MD_TASTE_FAIL);
2103 return (G_RAID_MD_TASTE_NEW);
2107 g_raid_md_taste_ddf(struct g_raid_md_object *md, struct g_class *mp,
2108 struct g_consumer *cp, struct g_geom **gp)
2110 struct g_consumer *rcp;
2111 struct g_provider *pp;
2112 struct g_raid_softc *sc;
2113 struct g_raid_disk *disk;
2114 struct ddf_meta meta;
2115 struct g_raid_md_ddf_perdisk *pd;
2116 struct g_raid_md_ddf_object *mdi;
2117 struct g_geom *geom;
2118 int error, result, be;
2121 G_RAID_DEBUG(1, "Tasting DDF on %s", cp->provider->name);
2122 mdi = (struct g_raid_md_ddf_object *)md;
2125 /* Read metadata from device. */
2126 g_topology_unlock();
2127 bzero(&meta, sizeof(meta));
2128 error = ddf_meta_read(cp, &meta);
2131 return (G_RAID_MD_TASTE_FAIL);
2132 be = meta.bigendian;
2134 /* Metadata valid. Print it. */
2135 g_raid_md_ddf_print(&meta);
2137 /* Search for matching node. */
2139 LIST_FOREACH(geom, &mp->geom, geom) {
2143 if (sc->sc_stopping != 0)
2145 if (sc->sc_md->mdo_class != md->mdo_class)
2147 mdi = (struct g_raid_md_ddf_object *)sc->sc_md;
2148 if (mdi->mdio_bigendian != be)
2153 /* Found matching node. */
2155 G_RAID_DEBUG(1, "Found matching array %s", sc->sc_name);
2156 result = G_RAID_MD_TASTE_EXISTING;
2158 } else { /* Not found matching node -- create one. */
2159 result = G_RAID_MD_TASTE_NEW;
2160 mdi->mdio_bigendian = be;
2161 snprintf(name, sizeof(name), "DDF%s", be ? "" : "-LE");
2162 sc = g_raid_create_node(mp, name, md);
2167 /* There is no return after this point, so we close passed consumer. */
2168 g_access(cp, -1, 0, 0);
2170 rcp = g_new_consumer(geom);
2171 rcp->flags |= G_CF_DIRECT_RECEIVE;
2173 if (g_access(rcp, 1, 1, 1) != 0)
2176 g_topology_unlock();
2177 sx_xlock(&sc->sc_lock);
2179 pd = malloc(sizeof(*pd), M_MD_DDF, M_WAITOK | M_ZERO);
2181 disk = g_raid_create_disk(sc);
2182 disk->d_md_data = (void *)pd;
2183 disk->d_consumer = rcp;
2184 rcp->private = disk;
2186 g_raid_get_disk_info(disk);
2188 g_raid_md_ddf_new_disk(disk);
2190 sx_xunlock(&sc->sc_lock);
2197 g_raid_md_event_ddf(struct g_raid_md_object *md,
2198 struct g_raid_disk *disk, u_int event)
2200 struct g_raid_softc *sc;
2206 case G_RAID_DISK_E_DISCONNECTED:
2208 g_raid_change_disk_state(disk, G_RAID_DISK_S_NONE);
2209 g_raid_destroy_disk(disk);
2210 g_raid_md_ddf_purge_volumes(sc);
2212 /* Write updated metadata to all disks. */
2213 g_raid_md_write_ddf(md, NULL, NULL, NULL);
2215 /* Check if anything left. */
2216 if (g_raid_ndisks(sc, -1) == 0)
2217 g_raid_destroy_node(sc, 0);
2219 g_raid_md_ddf_refill(sc);
2226 g_raid_md_volume_event_ddf(struct g_raid_md_object *md,
2227 struct g_raid_volume *vol, u_int event)
2229 struct g_raid_md_ddf_pervolume *pv;
2231 pv = (struct g_raid_md_ddf_pervolume *)vol->v_md_data;
2233 case G_RAID_VOLUME_E_STARTMD:
2234 if (!pv->pv_started)
2235 g_raid_md_ddf_start(vol);
2242 g_raid_md_ctl_ddf(struct g_raid_md_object *md,
2243 struct gctl_req *req)
2245 struct g_raid_softc *sc;
2246 struct g_raid_volume *vol, *vol1;
2247 struct g_raid_subdisk *sd;
2248 struct g_raid_disk *disk, *disks[DDF_MAX_DISKS_HARD];
2249 struct g_raid_md_ddf_perdisk *pd;
2250 struct g_raid_md_ddf_pervolume *pv;
2251 struct g_raid_md_ddf_object *mdi;
2252 struct ddf_sa_record *sa;
2253 struct g_consumer *cp;
2254 struct g_provider *pp;
2256 const char *nodename, *verb, *volname, *levelname, *diskname;
2259 off_t size, sectorsize, strip, offs[DDF_MAX_DISKS_HARD], esize;
2260 intmax_t *sizearg, *striparg;
2261 int i, numdisks, len, level, qual;
2265 mdi = (struct g_raid_md_ddf_object *)md;
2266 verb = gctl_get_param(req, "verb", NULL);
2267 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
2270 if (strcmp(verb, "label") == 0) {
2273 gctl_error(req, "Invalid number of arguments.");
2276 volname = gctl_get_asciiparam(req, "arg1");
2277 if (volname == NULL) {
2278 gctl_error(req, "No volume name.");
2281 levelname = gctl_get_asciiparam(req, "arg2");
2282 if (levelname == NULL) {
2283 gctl_error(req, "No RAID level.");
2286 if (g_raid_volume_str2level(levelname, &level, &qual)) {
2287 gctl_error(req, "Unknown RAID level '%s'.", levelname);
2290 numdisks = *nargs - 3;
2291 force = gctl_get_paraml(req, "force", sizeof(*force));
2292 if (!g_raid_md_ddf_supported(level, qual, numdisks,
2293 force ? *force : 0)) {
2294 gctl_error(req, "Unsupported RAID level "
2295 "(0x%02x/0x%02x), or number of disks (%d).",
2296 level, qual, numdisks);
2300 /* Search for disks, connect them and probe. */
2303 bzero(disks, sizeof(disks));
2304 bzero(offs, sizeof(offs));
2305 for (i = 0; i < numdisks; i++) {
2306 snprintf(arg, sizeof(arg), "arg%d", i + 3);
2307 diskname = gctl_get_asciiparam(req, arg);
2308 if (diskname == NULL) {
2309 gctl_error(req, "No disk name (%s).", arg);
2313 if (strcmp(diskname, "NONE") == 0)
2316 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2317 if (disk->d_consumer != NULL &&
2318 disk->d_consumer->provider != NULL &&
2319 strcmp(disk->d_consumer->provider->name,
2324 if (disk->d_state != G_RAID_DISK_S_ACTIVE) {
2325 gctl_error(req, "Disk '%s' is in a "
2326 "wrong state (%s).", diskname,
2327 g_raid_disk_state2str(disk->d_state));
2331 pd = disk->d_md_data;
2332 if (ddf_meta_count_vdc(&pd->pd_meta, NULL) >=
2333 GET16(&pd->pd_meta, hdr->Max_Partitions)) {
2334 gctl_error(req, "No free partitions "
2340 pp = disk->d_consumer->provider;
2342 ddf_meta_unused_range(&pd->pd_meta,
2344 offs[i] *= pp->sectorsize;
2345 size = MIN(size, (off_t)esize * pp->sectorsize);
2346 sectorsize = MAX(sectorsize, pp->sectorsize);
2351 cp = g_raid_open_consumer(sc, diskname);
2353 gctl_error(req, "Can't open disk '%s'.",
2355 g_topology_unlock();
2360 pd = malloc(sizeof(*pd), M_MD_DDF, M_WAITOK | M_ZERO);
2361 disk = g_raid_create_disk(sc);
2362 disk->d_md_data = (void *)pd;
2363 disk->d_consumer = cp;
2366 ddf_meta_create(disk, &mdi->mdio_meta);
2367 if (mdi->mdio_meta.hdr == NULL)
2368 ddf_meta_copy(&mdi->mdio_meta, &pd->pd_meta);
2370 ddf_meta_update(&mdi->mdio_meta, &pd->pd_meta);
2371 g_topology_unlock();
2373 g_raid_get_disk_info(disk);
2375 /* Reserve some space for metadata. */
2376 size = MIN(size, GET64(&pd->pd_meta,
2377 pdr->entry[0].Configured_Size) * pp->sectorsize);
2378 sectorsize = MAX(sectorsize, pp->sectorsize);
2381 for (i = 0; i < numdisks; i++) {
2382 if (disks[i] != NULL &&
2383 disks[i]->d_state == G_RAID_DISK_S_NONE)
2384 g_raid_destroy_disk(disks[i]);
2389 if (sectorsize <= 0) {
2390 gctl_error(req, "Can't get sector size.");
2394 /* Handle size argument. */
2395 len = sizeof(*sizearg);
2396 sizearg = gctl_get_param(req, "size", &len);
2397 if (sizearg != NULL && len == sizeof(*sizearg) &&
2399 if (*sizearg > size) {
2400 gctl_error(req, "Size too big %lld > %lld.",
2401 (long long)*sizearg, (long long)size);
2407 /* Handle strip argument. */
2409 len = sizeof(*striparg);
2410 striparg = gctl_get_param(req, "strip", &len);
2411 if (striparg != NULL && len == sizeof(*striparg) &&
2413 if (*striparg < sectorsize) {
2414 gctl_error(req, "Strip size too small.");
2417 if (*striparg % sectorsize != 0) {
2418 gctl_error(req, "Incorrect strip size.");
2424 /* Round size down to strip or sector. */
2425 if (level == G_RAID_VOLUME_RL_RAID1 ||
2426 level == G_RAID_VOLUME_RL_RAID3 ||
2427 level == G_RAID_VOLUME_RL_SINGLE ||
2428 level == G_RAID_VOLUME_RL_CONCAT)
2429 size -= (size % sectorsize);
2430 else if (level == G_RAID_VOLUME_RL_RAID1E &&
2431 (numdisks & 1) != 0)
2432 size -= (size % (2 * strip));
2434 size -= (size % strip);
2436 gctl_error(req, "Size too small.");
2440 /* We have all we need, create things: volume, ... */
2441 pv = malloc(sizeof(*pv), M_MD_DDF, M_WAITOK | M_ZERO);
2442 ddf_vol_meta_create(&pv->pv_meta, &mdi->mdio_meta);
2444 vol = g_raid_create_volume(sc, volname, -1);
2445 vol->v_md_data = pv;
2446 vol->v_raid_level = level;
2447 vol->v_raid_level_qualifier = qual;
2448 vol->v_strip_size = strip;
2449 vol->v_disks_count = numdisks;
2450 if (level == G_RAID_VOLUME_RL_RAID0 ||
2451 level == G_RAID_VOLUME_RL_CONCAT ||
2452 level == G_RAID_VOLUME_RL_SINGLE)
2453 vol->v_mediasize = size * numdisks;
2454 else if (level == G_RAID_VOLUME_RL_RAID1)
2455 vol->v_mediasize = size;
2456 else if (level == G_RAID_VOLUME_RL_RAID3 ||
2457 level == G_RAID_VOLUME_RL_RAID4 ||
2458 level == G_RAID_VOLUME_RL_RAID5)
2459 vol->v_mediasize = size * (numdisks - 1);
2460 else if (level == G_RAID_VOLUME_RL_RAID5R) {
2461 vol->v_mediasize = size * (numdisks - 1);
2462 vol->v_rotate_parity = 1024;
2463 } else if (level == G_RAID_VOLUME_RL_RAID6 ||
2464 level == G_RAID_VOLUME_RL_RAID5E ||
2465 level == G_RAID_VOLUME_RL_RAID5EE)
2466 vol->v_mediasize = size * (numdisks - 2);
2467 else if (level == G_RAID_VOLUME_RL_RAIDMDF) {
2469 vol->v_mdf_pdisks = 2;
2471 vol->v_mdf_pdisks = 3;
2472 vol->v_mdf_polynomial = 0x11d;
2473 vol->v_mdf_method = 0x00;
2474 vol->v_mediasize = size * (numdisks - vol->v_mdf_pdisks);
2475 } else { /* RAID1E */
2476 vol->v_mediasize = ((size * numdisks) / strip / 2) *
2479 vol->v_sectorsize = sectorsize;
2480 g_raid_start_volume(vol);
2482 /* , and subdisks. */
2483 for (i = 0; i < numdisks; i++) {
2485 sd = &vol->v_subdisks[i];
2487 sd->sd_offset = offs[i];
2491 TAILQ_INSERT_TAIL(&disk->d_subdisks, sd, sd_next);
2492 g_raid_change_disk_state(disk,
2493 G_RAID_DISK_S_ACTIVE);
2494 g_raid_change_subdisk_state(sd,
2495 G_RAID_SUBDISK_S_ACTIVE);
2496 g_raid_event_send(sd, G_RAID_SUBDISK_E_NEW,
2497 G_RAID_EVENT_SUBDISK);
2500 /* Write metadata based on created entities. */
2501 G_RAID_DEBUG1(0, sc, "Array started.");
2502 g_raid_md_write_ddf(md, vol, NULL, NULL);
2504 /* Pickup any STALE/SPARE disks to refill array if needed. */
2505 g_raid_md_ddf_refill(sc);
2507 g_raid_event_send(vol, G_RAID_VOLUME_E_START,
2508 G_RAID_EVENT_VOLUME);
2511 if (strcmp(verb, "add") == 0) {
2513 gctl_error(req, "`add` command is not applicable, "
2514 "use `label` instead.");
2517 if (strcmp(verb, "delete") == 0) {
2519 nodename = gctl_get_asciiparam(req, "arg0");
2520 if (nodename != NULL && strcasecmp(sc->sc_name, nodename) != 0)
2523 /* Full node destruction. */
2524 if (*nargs == 1 && nodename != NULL) {
2525 /* Check if some volume is still open. */
2526 force = gctl_get_paraml(req, "force", sizeof(*force));
2527 if (force != NULL && *force == 0 &&
2528 g_raid_nopens(sc) != 0) {
2529 gctl_error(req, "Some volume is still open.");
2533 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2534 if (disk->d_consumer)
2535 ddf_meta_erase(disk->d_consumer);
2537 g_raid_destroy_node(sc, 0);
2541 /* Destroy specified volume. If it was last - all node. */
2543 gctl_error(req, "Invalid number of arguments.");
2546 volname = gctl_get_asciiparam(req,
2547 nodename != NULL ? "arg1" : "arg0");
2548 if (volname == NULL) {
2549 gctl_error(req, "No volume name.");
2553 /* Search for volume. */
2554 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2555 if (strcmp(vol->v_name, volname) == 0)
2557 pp = vol->v_provider;
2560 if (strcmp(pp->name, volname) == 0)
2562 if (strncmp(pp->name, "raid/", 5) == 0 &&
2563 strcmp(pp->name + 5, volname) == 0)
2567 i = strtol(volname, &tmp, 10);
2568 if (verb != volname && tmp[0] == 0) {
2569 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2570 if (vol->v_global_id == i)
2576 gctl_error(req, "Volume '%s' not found.", volname);
2580 /* Check if volume is still open. */
2581 force = gctl_get_paraml(req, "force", sizeof(*force));
2582 if (force != NULL && *force == 0 &&
2583 vol->v_provider_open != 0) {
2584 gctl_error(req, "Volume is still open.");
2588 /* Destroy volume and potentially node. */
2590 TAILQ_FOREACH(vol1, &sc->sc_volumes, v_next)
2593 g_raid_destroy_volume(vol);
2594 g_raid_md_ddf_purge_disks(sc);
2595 g_raid_md_write_ddf(md, NULL, NULL, NULL);
2597 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2598 if (disk->d_consumer)
2599 ddf_meta_erase(disk->d_consumer);
2601 g_raid_destroy_node(sc, 0);
2605 if (strcmp(verb, "remove") == 0 ||
2606 strcmp(verb, "fail") == 0) {
2608 gctl_error(req, "Invalid number of arguments.");
2611 for (i = 1; i < *nargs; i++) {
2612 snprintf(arg, sizeof(arg), "arg%d", i);
2613 diskname = gctl_get_asciiparam(req, arg);
2614 if (diskname == NULL) {
2615 gctl_error(req, "No disk name (%s).", arg);
2619 if (strncmp(diskname, "/dev/", 5) == 0)
2622 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2623 if (disk->d_consumer != NULL &&
2624 disk->d_consumer->provider != NULL &&
2625 strcmp(disk->d_consumer->provider->name,
2630 gctl_error(req, "Disk '%s' not found.",
2636 if (strcmp(verb, "fail") == 0) {
2637 g_raid_md_fail_disk_ddf(md, NULL, disk);
2641 /* Erase metadata on deleting disk and destroy it. */
2642 ddf_meta_erase(disk->d_consumer);
2643 g_raid_destroy_disk(disk);
2645 g_raid_md_ddf_purge_volumes(sc);
2647 /* Write updated metadata to remaining disks. */
2648 g_raid_md_write_ddf(md, NULL, NULL, NULL);
2650 /* Check if anything left. */
2651 if (g_raid_ndisks(sc, -1) == 0)
2652 g_raid_destroy_node(sc, 0);
2654 g_raid_md_ddf_refill(sc);
2657 if (strcmp(verb, "insert") == 0) {
2659 gctl_error(req, "Invalid number of arguments.");
2662 for (i = 1; i < *nargs; i++) {
2663 /* Get disk name. */
2664 snprintf(arg, sizeof(arg), "arg%d", i);
2665 diskname = gctl_get_asciiparam(req, arg);
2666 if (diskname == NULL) {
2667 gctl_error(req, "No disk name (%s).", arg);
2672 /* Try to find provider with specified name. */
2674 cp = g_raid_open_consumer(sc, diskname);
2676 gctl_error(req, "Can't open disk '%s'.",
2678 g_topology_unlock();
2683 g_topology_unlock();
2685 pd = malloc(sizeof(*pd), M_MD_DDF, M_WAITOK | M_ZERO);
2687 disk = g_raid_create_disk(sc);
2688 disk->d_consumer = cp;
2689 disk->d_md_data = (void *)pd;
2692 g_raid_get_disk_info(disk);
2694 /* Welcome the "new" disk. */
2695 g_raid_change_disk_state(disk, G_RAID_DISK_S_SPARE);
2696 ddf_meta_create(disk, &mdi->mdio_meta);
2697 sa = ddf_meta_find_sa(&pd->pd_meta, 1);
2699 SET32D(&pd->pd_meta, sa->Signature,
2701 SET8D(&pd->pd_meta, sa->Spare_Type, 0);
2702 SET16D(&pd->pd_meta, sa->Populated_SAEs, 0);
2703 SET16D(&pd->pd_meta, sa->MAX_SAE_Supported,
2704 (GET16(&pd->pd_meta, hdr->Configuration_Record_Length) *
2705 pd->pd_meta.sectorsize -
2706 sizeof(struct ddf_sa_record)) /
2707 sizeof(struct ddf_sa_entry));
2709 if (mdi->mdio_meta.hdr == NULL)
2710 ddf_meta_copy(&mdi->mdio_meta, &pd->pd_meta);
2712 ddf_meta_update(&mdi->mdio_meta, &pd->pd_meta);
2713 g_raid_md_write_ddf(md, NULL, NULL, NULL);
2714 g_raid_md_ddf_refill(sc);
2722 g_raid_md_write_ddf(struct g_raid_md_object *md, struct g_raid_volume *tvol,
2723 struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk)
2725 struct g_raid_softc *sc;
2726 struct g_raid_volume *vol;
2727 struct g_raid_subdisk *sd;
2728 struct g_raid_disk *disk;
2729 struct g_raid_md_ddf_perdisk *pd;
2730 struct g_raid_md_ddf_pervolume *pv;
2731 struct g_raid_md_ddf_object *mdi;
2732 struct ddf_meta *gmeta;
2733 struct ddf_vol_meta *vmeta;
2734 struct ddf_vdc_record *vdc;
2735 struct ddf_sa_record *sa;
2737 int i, j, pos, bvd, size;
2740 mdi = (struct g_raid_md_ddf_object *)md;
2741 gmeta = &mdi->mdio_meta;
2743 if (sc->sc_stopping == G_RAID_DESTROY_HARD)
2747 * Clear disk flags to let only really needed ones to be reset.
2748 * Do it only if there are no volumes in starting state now,
2749 * as they can update disk statuses yet and we may kill innocent.
2751 if (mdi->mdio_starting == 0) {
2752 for (i = 0; i < GET16(gmeta, pdr->Populated_PDEs); i++) {
2753 if (isff(gmeta->pdr->entry[i].PD_GUID, 24))
2755 SET16(gmeta, pdr->entry[i].PD_Type,
2756 GET16(gmeta, pdr->entry[i].PD_Type) &
2757 ~(DDF_PDE_PARTICIPATING |
2758 DDF_PDE_GLOBAL_SPARE | DDF_PDE_CONFIG_SPARE));
2759 if ((GET16(gmeta, pdr->entry[i].PD_State) &
2761 SET16(gmeta, pdr->entry[i].PD_State, 0);
2765 /* Generate/update new per-volume metadata. */
2766 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2767 pv = (struct g_raid_md_ddf_pervolume *)vol->v_md_data;
2768 if (vol->v_stopping || !pv->pv_started)
2770 vmeta = &pv->pv_meta;
2772 SET32(vmeta, vdc->Sequence_Number,
2773 GET32(vmeta, vdc->Sequence_Number) + 1);
2774 if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E &&
2775 vol->v_disks_count % 2 == 0)
2776 SET16(vmeta, vdc->Primary_Element_Count, 2);
2778 SET16(vmeta, vdc->Primary_Element_Count,
2779 vol->v_disks_count);
2780 SET8(vmeta, vdc->Stripe_Size,
2781 ffs(vol->v_strip_size / vol->v_sectorsize) - 1);
2782 if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1E &&
2783 vol->v_disks_count % 2 == 0) {
2784 SET8(vmeta, vdc->Primary_RAID_Level,
2786 SET8(vmeta, vdc->RLQ, 0);
2787 SET8(vmeta, vdc->Secondary_Element_Count,
2788 vol->v_disks_count / 2);
2789 SET8(vmeta, vdc->Secondary_RAID_Level, 0);
2791 SET8(vmeta, vdc->Primary_RAID_Level,
2793 SET8(vmeta, vdc->RLQ,
2794 vol->v_raid_level_qualifier);
2795 SET8(vmeta, vdc->Secondary_Element_Count, 1);
2796 SET8(vmeta, vdc->Secondary_RAID_Level, 0);
2798 SET8(vmeta, vdc->Secondary_Element_Seq, 0);
2799 SET64(vmeta, vdc->Block_Count, 0);
2800 SET64(vmeta, vdc->VD_Size, vol->v_mediasize / vol->v_sectorsize);
2801 SET16(vmeta, vdc->Block_Size, vol->v_sectorsize);
2802 SET8(vmeta, vdc->Rotate_Parity_count,
2803 fls(vol->v_rotate_parity) - 1);
2804 SET8(vmeta, vdc->MDF_Parity_Disks, vol->v_mdf_pdisks);
2805 SET16(vmeta, vdc->MDF_Parity_Generator_Polynomial,
2806 vol->v_mdf_polynomial);
2807 SET8(vmeta, vdc->MDF_Constant_Generation_Method,
2810 SET16(vmeta, vde->VD_Number, vol->v_global_id);
2811 if (vol->v_state <= G_RAID_VOLUME_S_BROKEN)
2812 SET8(vmeta, vde->VD_State, DDF_VDE_FAILED);
2813 else if (vol->v_state <= G_RAID_VOLUME_S_DEGRADED)
2814 SET8(vmeta, vde->VD_State, DDF_VDE_DEGRADED);
2815 else if (vol->v_state <= G_RAID_VOLUME_S_SUBOPTIMAL)
2816 SET8(vmeta, vde->VD_State, DDF_VDE_PARTIAL);
2818 SET8(vmeta, vde->VD_State, DDF_VDE_OPTIMAL);
2820 g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_STALE) > 0 ||
2821 g_raid_nsubdisks(vol, G_RAID_SUBDISK_S_RESYNC) > 0)
2822 SET8(vmeta, vde->VD_State,
2823 GET8(vmeta, vde->VD_State) | DDF_VDE_DIRTY);
2824 SET8(vmeta, vde->Init_State, DDF_VDE_INIT_FULL); // XXX
2825 ddf_meta_put_name(vmeta, vol->v_name);
2827 for (i = 0; i < vol->v_disks_count; i++) {
2828 sd = &vol->v_subdisks[i];
2829 bvd = i / GET16(vmeta, vdc->Primary_Element_Count);
2830 pos = i % GET16(vmeta, vdc->Primary_Element_Count);
2833 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
2834 if (vmeta->bvdc[bvd] == NULL) {
2836 hdr->Configuration_Record_Length) *
2838 vmeta->bvdc[bvd] = malloc(size,
2839 M_MD_DDF, M_WAITOK);
2840 memset(vmeta->bvdc[bvd], 0xff, size);
2842 memcpy(vmeta->bvdc[bvd], vmeta->vdc,
2843 sizeof(struct ddf_vdc_record));
2844 SET8(vmeta, bvdc[bvd]->Secondary_Element_Seq, bvd);
2845 SET64(vmeta, bvdc[bvd]->Block_Count,
2846 sd->sd_size / vol->v_sectorsize);
2847 SET32(vmeta, bvdc[bvd]->Physical_Disk_Sequence[pos],
2848 GET32(&pd->pd_meta, pdd->PD_Reference));
2849 val2 = (uint64_t *)&(vmeta->bvdc[bvd]->Physical_Disk_Sequence[
2850 GET16(vmeta, hdr->Max_Primary_Element_Entries)]);
2851 SET64P(vmeta, val2 + pos,
2852 sd->sd_offset / vol->v_sectorsize);
2854 if (vmeta->bvdc[bvd] == NULL)
2857 j = ddf_meta_find_pd(gmeta, NULL,
2858 GET32(vmeta, bvdc[bvd]->Physical_Disk_Sequence[pos]));
2861 SET16(gmeta, pdr->entry[j].PD_Type,
2862 GET16(gmeta, pdr->entry[j].PD_Type) |
2863 DDF_PDE_PARTICIPATING);
2864 if (sd->sd_state == G_RAID_SUBDISK_S_NONE)
2865 SET16(gmeta, pdr->entry[j].PD_State,
2866 GET16(gmeta, pdr->entry[j].PD_State) |
2867 (DDF_PDE_FAILED | DDF_PDE_MISSING));
2868 else if (sd->sd_state == G_RAID_SUBDISK_S_FAILED)
2869 SET16(gmeta, pdr->entry[j].PD_State,
2870 GET16(gmeta, pdr->entry[j].PD_State) |
2871 (DDF_PDE_FAILED | DDF_PDE_PFA));
2872 else if (sd->sd_state <= G_RAID_SUBDISK_S_REBUILD)
2873 SET16(gmeta, pdr->entry[j].PD_State,
2874 GET16(gmeta, pdr->entry[j].PD_State) |
2877 SET16(gmeta, pdr->entry[j].PD_State,
2878 GET16(gmeta, pdr->entry[j].PD_State) |
2883 /* Mark spare and failed disks as such. */
2884 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2885 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
2886 i = ddf_meta_find_pd(gmeta, NULL,
2887 GET32(&pd->pd_meta, pdd->PD_Reference));
2890 if (disk->d_state == G_RAID_DISK_S_FAILED) {
2891 SET16(gmeta, pdr->entry[i].PD_State,
2892 GET16(gmeta, pdr->entry[i].PD_State) |
2893 (DDF_PDE_FAILED | DDF_PDE_PFA));
2895 if (disk->d_state != G_RAID_DISK_S_SPARE)
2897 sa = ddf_meta_find_sa(&pd->pd_meta, 0);
2899 (GET8D(&pd->pd_meta, sa->Spare_Type) &
2900 DDF_SAR_TYPE_DEDICATED) == 0) {
2901 SET16(gmeta, pdr->entry[i].PD_Type,
2902 GET16(gmeta, pdr->entry[i].PD_Type) |
2903 DDF_PDE_GLOBAL_SPARE);
2905 SET16(gmeta, pdr->entry[i].PD_Type,
2906 GET16(gmeta, pdr->entry[i].PD_Type) |
2907 DDF_PDE_CONFIG_SPARE);
2909 SET16(gmeta, pdr->entry[i].PD_State,
2910 GET16(gmeta, pdr->entry[i].PD_State) |
2914 /* Remove disks without "participating" flag (unused). */
2915 for (i = 0, j = -1; i < GET16(gmeta, pdr->Populated_PDEs); i++) {
2916 if (isff(gmeta->pdr->entry[i].PD_GUID, 24))
2918 if ((GET16(gmeta, pdr->entry[i].PD_Type) &
2919 (DDF_PDE_PARTICIPATING |
2920 DDF_PDE_GLOBAL_SPARE | DDF_PDE_CONFIG_SPARE)) != 0 ||
2921 g_raid_md_ddf_get_disk(sc,
2922 NULL, GET32(gmeta, pdr->entry[i].PD_Reference)) != NULL)
2925 memset(&gmeta->pdr->entry[i], 0xff,
2926 sizeof(struct ddf_pd_entry));
2928 SET16(gmeta, pdr->Populated_PDEs, j + 1);
2930 /* Update per-disk metadata and write them. */
2931 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
2932 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
2933 if (disk->d_state != G_RAID_DISK_S_ACTIVE &&
2934 disk->d_state != G_RAID_DISK_S_SPARE)
2937 memcpy(pd->pd_meta.pdr, gmeta->pdr,
2938 GET32(&pd->pd_meta, hdr->pdr_length) *
2939 pd->pd_meta.sectorsize);
2941 SET16(&pd->pd_meta, vdr->Populated_VDEs, 0);
2942 TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2943 if (vol->v_stopping)
2945 pv = (struct g_raid_md_ddf_pervolume *)vol->v_md_data;
2946 i = ddf_meta_find_vd(&pd->pd_meta,
2947 pv->pv_meta.vde->VD_GUID);
2949 i = ddf_meta_find_vd(&pd->pd_meta, NULL);
2951 memcpy(&pd->pd_meta.vdr->entry[i],
2953 sizeof(struct ddf_vd_entry));
2956 if (mdi->mdio_starting == 0) {
2957 /* Remove all VDCs to restore needed later. */
2958 j = GETCRNUM(&pd->pd_meta);
2959 for (i = 0; i < j; i++) {
2960 vdc = GETVDCPTR(&pd->pd_meta, i);
2961 if (GET32D(&pd->pd_meta, vdc->Signature) !=
2964 SET32D(&pd->pd_meta, vdc->Signature, 0xffffffff);
2967 TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
2968 vol = sd->sd_volume;
2969 if (vol->v_stopping)
2971 pv = (struct g_raid_md_ddf_pervolume *)vol->v_md_data;
2972 vmeta = &pv->pv_meta;
2973 vdc = ddf_meta_find_vdc(&pd->pd_meta,
2974 vmeta->vde->VD_GUID);
2976 vdc = ddf_meta_find_vdc(&pd->pd_meta, NULL);
2978 bvd = sd->sd_pos / GET16(vmeta,
2979 vdc->Primary_Element_Count);
2980 memcpy(vdc, vmeta->bvdc[bvd],
2982 hdr->Configuration_Record_Length) *
2983 pd->pd_meta.sectorsize);
2986 G_RAID_DEBUG(1, "Writing DDF metadata to %s",
2987 g_raid_get_diskname(disk));
2988 g_raid_md_ddf_print(&pd->pd_meta);
2989 ddf_meta_write(disk->d_consumer, &pd->pd_meta);
2995 g_raid_md_fail_disk_ddf(struct g_raid_md_object *md,
2996 struct g_raid_subdisk *tsd, struct g_raid_disk *tdisk)
2998 struct g_raid_softc *sc;
2999 struct g_raid_md_ddf_perdisk *pd;
3000 struct g_raid_subdisk *sd;
3004 pd = (struct g_raid_md_ddf_perdisk *)tdisk->d_md_data;
3006 /* We can't fail disk that is not a part of array now. */
3007 if (tdisk->d_state != G_RAID_DISK_S_ACTIVE)
3011 * Mark disk as failed in metadata and try to write that metadata
3012 * to the disk itself to prevent it's later resurrection as STALE.
3014 G_RAID_DEBUG(1, "Writing DDF metadata to %s",
3015 g_raid_get_diskname(tdisk));
3016 i = ddf_meta_find_pd(&pd->pd_meta, NULL, GET32(&pd->pd_meta, pdd->PD_Reference));
3017 SET16(&pd->pd_meta, pdr->entry[i].PD_State, DDF_PDE_FAILED | DDF_PDE_PFA);
3018 if (tdisk->d_consumer != NULL)
3019 ddf_meta_write(tdisk->d_consumer, &pd->pd_meta);
3021 /* Change states. */
3022 g_raid_change_disk_state(tdisk, G_RAID_DISK_S_FAILED);
3023 TAILQ_FOREACH(sd, &tdisk->d_subdisks, sd_next) {
3024 g_raid_change_subdisk_state(sd,
3025 G_RAID_SUBDISK_S_FAILED);
3026 g_raid_event_send(sd, G_RAID_SUBDISK_E_FAILED,
3027 G_RAID_EVENT_SUBDISK);
3030 /* Write updated metadata to remaining disks. */
3031 g_raid_md_write_ddf(md, NULL, NULL, tdisk);
3033 g_raid_md_ddf_refill(sc);
3038 g_raid_md_free_disk_ddf(struct g_raid_md_object *md,
3039 struct g_raid_disk *disk)
3041 struct g_raid_md_ddf_perdisk *pd;
3043 pd = (struct g_raid_md_ddf_perdisk *)disk->d_md_data;
3044 ddf_meta_free(&pd->pd_meta);
3046 disk->d_md_data = NULL;
3051 g_raid_md_free_volume_ddf(struct g_raid_md_object *md,
3052 struct g_raid_volume *vol)
3054 struct g_raid_md_ddf_object *mdi;
3055 struct g_raid_md_ddf_pervolume *pv;
3057 mdi = (struct g_raid_md_ddf_object *)md;
3058 pv = (struct g_raid_md_ddf_pervolume *)vol->v_md_data;
3059 ddf_vol_meta_free(&pv->pv_meta);
3060 if (!pv->pv_started) {
3062 mdi->mdio_starting--;
3063 callout_stop(&pv->pv_start_co);
3066 vol->v_md_data = NULL;
3071 g_raid_md_free_ddf(struct g_raid_md_object *md)
3073 struct g_raid_md_ddf_object *mdi;
3075 mdi = (struct g_raid_md_ddf_object *)md;
3076 if (!mdi->mdio_started) {
3077 mdi->mdio_started = 0;
3078 callout_stop(&mdi->mdio_start_co);
3079 G_RAID_DEBUG1(1, md->mdo_softc,
3080 "root_mount_rel %p", mdi->mdio_rootmount);
3081 root_mount_rel(mdi->mdio_rootmount);
3082 mdi->mdio_rootmount = NULL;
3084 ddf_meta_free(&mdi->mdio_meta);
3088 G_RAID_MD_DECLARE(ddf, "DDF");