]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/geom/part/g_part_ebr.c
MFC r228061:
[FreeBSD/stable/8.git] / sys / geom / part / g_part_ebr.c
1 /*-
2  * Copyright (c) 2007-2009 Marcel Moolenaar
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "opt_geom.h"
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/bio.h>
34 #include <sys/diskmbr.h>
35 #include <sys/endian.h>
36 #include <sys/kernel.h>
37 #include <sys/kobj.h>
38 #include <sys/limits.h>
39 #include <sys/lock.h>
40 #include <sys/malloc.h>
41 #include <sys/mutex.h>
42 #include <sys/queue.h>
43 #include <sys/sbuf.h>
44 #include <sys/systm.h>
45 #include <geom/geom.h>
46 #include <geom/part/g_part.h>
47
48 #include "g_part_if.h"
49
50 #define EBRSIZE         512
51
52 struct g_part_ebr_table {
53         struct g_part_table     base;
54 #ifndef GEOM_PART_EBR_COMPAT
55         u_char          ebr[EBRSIZE];
56 #endif
57 };
58
59 struct g_part_ebr_entry {
60         struct g_part_entry     base;
61         struct dos_partition    ent;
62 };
63
64 static int g_part_ebr_add(struct g_part_table *, struct g_part_entry *,
65     struct g_part_parms *);
66 static int g_part_ebr_create(struct g_part_table *, struct g_part_parms *);
67 static int g_part_ebr_destroy(struct g_part_table *, struct g_part_parms *);
68 static void g_part_ebr_dumpconf(struct g_part_table *, struct g_part_entry *,
69     struct sbuf *, const char *);
70 static int g_part_ebr_dumpto(struct g_part_table *, struct g_part_entry *);
71 #if defined(GEOM_PART_EBR_COMPAT)
72 static void g_part_ebr_fullname(struct g_part_table *, struct g_part_entry *,
73     struct sbuf *, const char *);
74 #endif
75 static int g_part_ebr_modify(struct g_part_table *, struct g_part_entry *,  
76     struct g_part_parms *);
77 static const char *g_part_ebr_name(struct g_part_table *, struct g_part_entry *,
78     char *, size_t);
79 static int g_part_ebr_precheck(struct g_part_table *, enum g_part_ctl,
80     struct g_part_parms *);
81 static int g_part_ebr_probe(struct g_part_table *, struct g_consumer *);
82 static int g_part_ebr_read(struct g_part_table *, struct g_consumer *);
83 static int g_part_ebr_setunset(struct g_part_table *, struct g_part_entry *,
84     const char *, unsigned int);
85 static const char *g_part_ebr_type(struct g_part_table *, struct g_part_entry *,
86     char *, size_t);
87 static int g_part_ebr_write(struct g_part_table *, struct g_consumer *);
88
89 static kobj_method_t g_part_ebr_methods[] = {
90         KOBJMETHOD(g_part_add,          g_part_ebr_add),
91         KOBJMETHOD(g_part_create,       g_part_ebr_create),
92         KOBJMETHOD(g_part_destroy,      g_part_ebr_destroy),
93         KOBJMETHOD(g_part_dumpconf,     g_part_ebr_dumpconf),
94         KOBJMETHOD(g_part_dumpto,       g_part_ebr_dumpto),
95 #if defined(GEOM_PART_EBR_COMPAT)
96         KOBJMETHOD(g_part_fullname,     g_part_ebr_fullname),
97 #endif
98         KOBJMETHOD(g_part_modify,       g_part_ebr_modify),
99         KOBJMETHOD(g_part_name,         g_part_ebr_name),
100         KOBJMETHOD(g_part_precheck,     g_part_ebr_precheck),
101         KOBJMETHOD(g_part_probe,        g_part_ebr_probe),
102         KOBJMETHOD(g_part_read,         g_part_ebr_read),
103         KOBJMETHOD(g_part_setunset,     g_part_ebr_setunset),
104         KOBJMETHOD(g_part_type,         g_part_ebr_type),
105         KOBJMETHOD(g_part_write,        g_part_ebr_write),
106         { 0, 0 }
107 };
108
109 static struct g_part_scheme g_part_ebr_scheme = {
110         "EBR",
111         g_part_ebr_methods,
112         sizeof(struct g_part_ebr_table),
113         .gps_entrysz = sizeof(struct g_part_ebr_entry),
114         .gps_minent = 1,
115         .gps_maxent = INT_MAX,
116 };
117 G_PART_SCHEME_DECLARE(g_part_ebr);
118
119 static struct g_part_ebr_alias {
120         u_char          typ;
121         int             alias;
122 } ebr_alias_match[] = {
123         { DOSPTYP_386BSD,       G_PART_ALIAS_FREEBSD },
124         { DOSPTYP_NTFS,         G_PART_ALIAS_MS_NTFS },
125         { DOSPTYP_FAT32,        G_PART_ALIAS_MS_FAT32 },
126         { DOSPTYP_LINSWP,       G_PART_ALIAS_LINUX_SWAP },
127         { DOSPTYP_LINUX,        G_PART_ALIAS_LINUX_DATA },
128         { DOSPTYP_LINLVM,       G_PART_ALIAS_LINUX_LVM },
129         { DOSPTYP_LINRAID,      G_PART_ALIAS_LINUX_RAID },
130 };
131
132 static void ebr_set_chs(struct g_part_table *, uint32_t, u_char *, u_char *,
133     u_char *);
134
135 static void
136 ebr_entry_decode(const char *p, struct dos_partition *ent)
137 {
138         ent->dp_flag = p[0];
139         ent->dp_shd = p[1];
140         ent->dp_ssect = p[2];
141         ent->dp_scyl = p[3];
142         ent->dp_typ = p[4];
143         ent->dp_ehd = p[5];
144         ent->dp_esect = p[6];
145         ent->dp_ecyl = p[7];
146         ent->dp_start = le32dec(p + 8);
147         ent->dp_size = le32dec(p + 12);
148 }
149
150 static void
151 ebr_entry_link(struct g_part_table *table, uint32_t start, uint32_t end,
152    u_char *buf)
153 {
154
155         buf[0] = 0 /* dp_flag */;
156         ebr_set_chs(table, start, &buf[3] /* dp_scyl */, &buf[1] /* dp_shd */,
157             &buf[2] /* dp_ssect */);
158         buf[4] = 5 /* dp_typ */;
159         ebr_set_chs(table, end, &buf[7] /* dp_ecyl */, &buf[5] /* dp_ehd */,
160             &buf[6] /* dp_esect */);
161         le32enc(buf + 8, start);
162         le32enc(buf + 12, end - start + 1);
163 }
164
165 static int
166 ebr_parse_type(const char *type, u_char *dp_typ)
167 {
168         const char *alias;
169         char *endp;
170         long lt;
171         int i;
172
173         if (type[0] == '!') {
174                 lt = strtol(type + 1, &endp, 0);
175                 if (type[1] == '\0' || *endp != '\0' || lt <= 0 || lt >= 256)
176                         return (EINVAL);
177                 *dp_typ = (u_char)lt;
178                 return (0);
179         }
180         for (i = 0;
181             i < sizeof(ebr_alias_match) / sizeof(ebr_alias_match[0]); i++) {
182                 alias = g_part_alias_name(ebr_alias_match[i].alias);
183                 if (strcasecmp(type, alias) == 0) {
184                         *dp_typ = ebr_alias_match[i].typ;
185                         return (0);
186                 }
187         }
188         return (EINVAL);
189 }
190
191
192 static void
193 ebr_set_chs(struct g_part_table *table, uint32_t lba, u_char *cylp, u_char *hdp,
194     u_char *secp)
195 {
196         uint32_t cyl, hd, sec;
197
198         sec = lba % table->gpt_sectors + 1;
199         lba /= table->gpt_sectors;
200         hd = lba % table->gpt_heads;
201         lba /= table->gpt_heads;
202         cyl = lba;
203         if (cyl > 1023)
204                 sec = hd = cyl = ~0;
205
206         *cylp = cyl & 0xff;
207         *hdp = hd & 0xff;
208         *secp = (sec & 0x3f) | ((cyl >> 2) & 0xc0);
209 }
210
211 static int
212 g_part_ebr_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
213     struct g_part_parms *gpp)
214 {
215         struct g_geom *gp;
216         struct g_provider *pp;
217         struct g_part_ebr_entry *entry;
218         uint32_t start, size, sectors;
219
220         if (gpp->gpp_parms & G_PART_PARM_LABEL)
221                 return (EINVAL);
222
223         gp = basetable->gpt_gp;
224         pp = LIST_FIRST(&gp->consumer)->provider;
225         sectors = basetable->gpt_sectors;
226
227         entry = (struct g_part_ebr_entry *)baseentry;
228
229         start = gpp->gpp_start;
230         size = gpp->gpp_size;
231         if (size < 2 * sectors)
232                 return (EINVAL);
233         if (start % sectors) {
234                 size = size - sectors + (start % sectors);
235                 start = start - (start % sectors) + sectors;
236         }
237         if (size % sectors)
238                 size = size - (size % sectors);
239         if (size < 2 * sectors)
240                 return (EINVAL);
241
242         if (baseentry->gpe_deleted)
243                 bzero(&entry->ent, sizeof(entry->ent));
244
245         KASSERT(baseentry->gpe_start <= start, (__func__));
246         KASSERT(baseentry->gpe_end >= start + size - 1, (__func__));
247         baseentry->gpe_index = (start / sectors) + 1;
248         baseentry->gpe_offset = (off_t)(start + sectors) * pp->sectorsize;
249         baseentry->gpe_start = start;
250         baseentry->gpe_end = start + size - 1;
251         entry->ent.dp_start = sectors;
252         entry->ent.dp_size = size - sectors;
253         ebr_set_chs(basetable, entry->ent.dp_start, &entry->ent.dp_scyl,
254             &entry->ent.dp_shd, &entry->ent.dp_ssect);
255         ebr_set_chs(basetable, baseentry->gpe_end, &entry->ent.dp_ecyl,
256             &entry->ent.dp_ehd, &entry->ent.dp_esect);
257         return (ebr_parse_type(gpp->gpp_type, &entry->ent.dp_typ));
258 }
259
260 static int
261 g_part_ebr_create(struct g_part_table *basetable, struct g_part_parms *gpp)
262 {
263         char psn[8];
264         struct g_consumer *cp;
265         struct g_provider *pp;
266         uint32_t msize;
267         int error;
268
269         pp = gpp->gpp_provider;
270
271         if (pp->sectorsize < EBRSIZE)
272                 return (ENOSPC);
273         if (pp->sectorsize > 4096)
274                 return (ENXIO);
275
276         /* Check that we have a parent and that it's a MBR. */
277         if (basetable->gpt_depth == 0)
278                 return (ENXIO);
279         cp = LIST_FIRST(&pp->consumers);
280         error = g_getattr("PART::scheme", cp, &psn);
281         if (error)
282                 return (error);
283         if (strcmp(psn, "MBR"))
284                 return (ENXIO);
285
286         msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
287         msize -= msize % basetable->gpt_sectors;
288         basetable->gpt_first = 0;
289         basetable->gpt_last = msize - 1;
290         basetable->gpt_entries = msize / basetable->gpt_sectors;
291         return (0);
292 }
293
294 static int
295 g_part_ebr_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
296 {
297
298         /* Wipe the first sector to clear the partitioning. */
299         basetable->gpt_smhead |= 1;
300         return (0);
301 }
302
303 static void
304 g_part_ebr_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry, 
305     struct sbuf *sb, const char *indent)
306 {
307         struct g_part_ebr_entry *entry;
308  
309         entry = (struct g_part_ebr_entry *)baseentry;
310         if (indent == NULL) {
311                 /* conftxt: libdisk compatibility */
312                 sbuf_printf(sb, " xs MBREXT xt %u", entry->ent.dp_typ);
313         } else if (entry != NULL) {
314                 /* confxml: partition entry information */
315                 sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent,
316                     entry->ent.dp_typ);
317                 if (entry->ent.dp_flag & 0x80)
318                         sbuf_printf(sb, "%s<attrib>active</attrib>\n", indent);
319         } else {
320                 /* confxml: scheme information */
321         }
322 }
323
324 static int
325 g_part_ebr_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)  
326 {
327         struct g_part_ebr_entry *entry;
328
329         /* Allow dumping to a FreeBSD partition or Linux swap partition only. */
330         entry = (struct g_part_ebr_entry *)baseentry;
331         return ((entry->ent.dp_typ == DOSPTYP_386BSD ||
332             entry->ent.dp_typ == DOSPTYP_LINSWP) ? 1 : 0);
333 }
334
335 #if defined(GEOM_PART_EBR_COMPAT)
336 static void
337 g_part_ebr_fullname(struct g_part_table *table, struct g_part_entry *entry,
338     struct sbuf *sb, const char *pfx)
339 {
340         struct g_part_entry *iter;
341         u_int idx;
342
343         idx = 5;
344         LIST_FOREACH(iter, &table->gpt_entry, gpe_entry) {
345                 if (iter == entry)
346                         break;
347                 idx++;
348         }
349         sbuf_printf(sb, "%.*s%u", (int)strlen(pfx) - 1, pfx, idx);
350 }
351 #endif
352
353 static int
354 g_part_ebr_modify(struct g_part_table *basetable,
355     struct g_part_entry *baseentry, struct g_part_parms *gpp)
356 {
357         struct g_part_ebr_entry *entry;
358
359         if (gpp->gpp_parms & G_PART_PARM_LABEL)
360                 return (EINVAL);
361
362         entry = (struct g_part_ebr_entry *)baseentry;
363         if (gpp->gpp_parms & G_PART_PARM_TYPE)
364                 return (ebr_parse_type(gpp->gpp_type, &entry->ent.dp_typ));
365         return (0);
366 }
367
368 static const char *
369 g_part_ebr_name(struct g_part_table *table, struct g_part_entry *entry,
370     char *buf, size_t bufsz)
371 {
372
373         snprintf(buf, bufsz, "+%08u", entry->gpe_index);
374         return (buf);
375 }
376
377 static int
378 g_part_ebr_precheck(struct g_part_table *table, enum g_part_ctl req,
379     struct g_part_parms *gpp)
380 {
381 #if defined(GEOM_PART_EBR_COMPAT)
382         return (ECANCELED);
383 #else
384         /*
385          * The index is a function of the start of the partition.
386          * This is not something the user can override, nor is it
387          * something the common code will do right. We can set the
388          * index now so that we get what we need.
389          */
390         if (req == G_PART_CTL_ADD)
391                 gpp->gpp_index = (gpp->gpp_start / table->gpt_sectors) + 1;
392         return (0);
393 #endif
394 }
395
396 static int
397 g_part_ebr_probe(struct g_part_table *table, struct g_consumer *cp)
398 {
399         char psn[8];
400         struct g_provider *pp;
401         u_char *buf, *p;
402         int error, index, res;
403         uint16_t magic;
404
405         pp = cp->provider;
406
407         /* Sanity-check the provider. */
408         if (pp->sectorsize < EBRSIZE || pp->mediasize < pp->sectorsize)
409                 return (ENOSPC);
410         if (pp->sectorsize > 4096)
411                 return (ENXIO);
412
413         /* Check that we have a parent and that it's a MBR. */
414         if (table->gpt_depth == 0)
415                 return (ENXIO);
416         error = g_getattr("PART::scheme", cp, &psn);
417         if (error)
418                 return (error);
419         if (strcmp(psn, "MBR"))
420                 return (ENXIO);
421
422         /* Check that there's a EBR. */
423         buf = g_read_data(cp, 0L, pp->sectorsize, &error);
424         if (buf == NULL)
425                 return (error);
426
427         /* We goto out on mismatch. */
428         res = ENXIO;
429
430         magic = le16dec(buf + DOSMAGICOFFSET);
431         if (magic != DOSMAGIC)
432                 goto out;
433
434         for (index = 0; index < 2; index++) {
435                 p = buf + DOSPARTOFF + index * DOSPARTSIZE;
436                 if (p[0] != 0 && p[0] != 0x80)
437                         goto out;
438         }
439         res = G_PART_PROBE_PRI_NORM;
440
441  out:
442         g_free(buf);
443         return (res);
444 }
445
446 static int
447 g_part_ebr_read(struct g_part_table *basetable, struct g_consumer *cp)
448 {
449         struct dos_partition ent[2];
450         struct g_provider *pp;
451         struct g_part_entry *baseentry;
452         struct g_part_ebr_table *table;
453         struct g_part_ebr_entry *entry;
454         u_char *buf;
455         off_t ofs, msize;
456         u_int lba;
457         int error, index;
458
459         pp = cp->provider;
460         table = (struct g_part_ebr_table *)basetable;
461         msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
462
463         lba = 0;
464         while (1) {
465                 ofs = (off_t)lba * pp->sectorsize;
466                 buf = g_read_data(cp, ofs, pp->sectorsize, &error);
467                 if (buf == NULL)
468                         return (error);
469
470                 ebr_entry_decode(buf + DOSPARTOFF + 0 * DOSPARTSIZE, ent + 0);
471                 ebr_entry_decode(buf + DOSPARTOFF + 1 * DOSPARTSIZE, ent + 1);
472
473                 /* The 3rd & 4th entries should be zeroes. */
474                 if (le64dec(buf + DOSPARTOFF + 2 * DOSPARTSIZE) +
475                     le64dec(buf + DOSPARTOFF + 3 * DOSPARTSIZE) != 0) {
476                         basetable->gpt_corrupt = 1;
477                         printf("GEOM: %s: invalid entries in the EBR ignored.\n",
478                             pp->name);
479                 }
480 #ifndef GEOM_PART_EBR_COMPAT
481                 /* Save the first EBR, it can contain a boot code */
482                 if (lba == 0)
483                         bcopy(buf, table->ebr, sizeof(table->ebr));
484 #endif
485                 g_free(buf);
486
487                 if (ent[0].dp_typ == 0)
488                         break;
489
490                 if (ent[0].dp_typ == 5 && ent[1].dp_typ == 0) {
491                         lba = ent[0].dp_start;
492                         continue;
493                 }
494
495                 index = (lba / basetable->gpt_sectors) + 1;
496                 baseentry = (struct g_part_entry *)g_part_new_entry(basetable,
497                     index, lba, lba + ent[0].dp_start + ent[0].dp_size - 1);
498                 baseentry->gpe_offset = (off_t)(lba + ent[0].dp_start) *
499                     pp->sectorsize;
500                 entry = (struct g_part_ebr_entry *)baseentry;
501                 entry->ent = ent[0];
502
503                 if (ent[1].dp_typ == 0)
504                         break;
505
506                 lba = ent[1].dp_start;
507         }
508
509         basetable->gpt_entries = msize / basetable->gpt_sectors;
510         basetable->gpt_first = 0;
511         basetable->gpt_last = msize - (msize % basetable->gpt_sectors) - 1;
512         return (0);
513 }
514
515 static int
516 g_part_ebr_setunset(struct g_part_table *table, struct g_part_entry *baseentry,
517     const char *attrib, unsigned int set)
518 {
519         struct g_part_entry *iter;
520         struct g_part_ebr_entry *entry;
521         int changed;
522
523         if (strcasecmp(attrib, "active") != 0)
524                 return (EINVAL);
525
526         /* Only one entry can have the active attribute. */
527         LIST_FOREACH(iter, &table->gpt_entry, gpe_entry) {
528                 if (iter->gpe_deleted)
529                         continue;
530                 changed = 0;
531                 entry = (struct g_part_ebr_entry *)iter;
532                 if (iter == baseentry) {
533                         if (set && (entry->ent.dp_flag & 0x80) == 0) {
534                                 entry->ent.dp_flag |= 0x80;
535                                 changed = 1;
536                         } else if (!set && (entry->ent.dp_flag & 0x80)) {
537                                 entry->ent.dp_flag &= ~0x80;
538                                 changed = 1;
539                         }
540                 } else {
541                         if (set && (entry->ent.dp_flag & 0x80)) {
542                                 entry->ent.dp_flag &= ~0x80;
543                                 changed = 1;
544                         }
545                 }
546                 if (changed && !iter->gpe_created)
547                         iter->gpe_modified = 1;
548         }
549         return (0);
550 }
551
552 static const char *
553 g_part_ebr_type(struct g_part_table *basetable, struct g_part_entry *baseentry, 
554     char *buf, size_t bufsz)
555 {
556         struct g_part_ebr_entry *entry;
557         int i;
558
559         entry = (struct g_part_ebr_entry *)baseentry;
560         for (i = 0;
561             i < sizeof(ebr_alias_match) / sizeof(ebr_alias_match[0]); i++) {
562                 if (ebr_alias_match[i].typ == entry->ent.dp_typ)
563                         return (g_part_alias_name(ebr_alias_match[i].alias));
564         }
565         snprintf(buf, bufsz, "!%d", entry->ent.dp_typ);
566         return (buf);
567 }
568
569 static int
570 g_part_ebr_write(struct g_part_table *basetable, struct g_consumer *cp)
571 {
572 #ifndef GEOM_PART_EBR_COMPAT
573         struct g_part_ebr_table *table;
574 #endif
575         struct g_provider *pp;
576         struct g_part_entry *baseentry, *next;
577         struct g_part_ebr_entry *entry;
578         u_char *buf;
579         u_char *p;
580         int error;
581
582         pp = cp->provider;
583         buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
584 #ifndef GEOM_PART_EBR_COMPAT
585         table = (struct g_part_ebr_table *)basetable;
586         bcopy(table->ebr, buf, DOSPARTOFF);
587 #endif
588         le16enc(buf + DOSMAGICOFFSET, DOSMAGIC);
589
590         baseentry = LIST_FIRST(&basetable->gpt_entry);
591         while (baseentry != NULL && baseentry->gpe_deleted)
592                 baseentry = LIST_NEXT(baseentry, gpe_entry);
593
594         /* Wipe-out the first EBR when there are no slices. */
595         if (baseentry == NULL) {
596                 error = g_write_data(cp, 0, buf, pp->sectorsize);
597                 goto out;
598         }
599
600         /*
601          * If the first partition is not in LBA 0, we need to
602          * put a "link" EBR in LBA 0.
603          */
604         if (baseentry->gpe_start != 0) {
605                 ebr_entry_link(basetable, (uint32_t)baseentry->gpe_start,
606                     (uint32_t)baseentry->gpe_end, buf + DOSPARTOFF);
607                 error = g_write_data(cp, 0, buf, pp->sectorsize);
608                 if (error)
609                         goto out;
610         }
611
612         do {
613                 entry = (struct g_part_ebr_entry *)baseentry;
614
615                 p = buf + DOSPARTOFF;
616                 p[0] = entry->ent.dp_flag;
617                 p[1] = entry->ent.dp_shd;
618                 p[2] = entry->ent.dp_ssect;
619                 p[3] = entry->ent.dp_scyl;
620                 p[4] = entry->ent.dp_typ;
621                 p[5] = entry->ent.dp_ehd;
622                 p[6] = entry->ent.dp_esect;
623                 p[7] = entry->ent.dp_ecyl;
624                 le32enc(p + 8, entry->ent.dp_start);
625                 le32enc(p + 12, entry->ent.dp_size);
626  
627                 next = LIST_NEXT(baseentry, gpe_entry);
628                 while (next != NULL && next->gpe_deleted)
629                         next = LIST_NEXT(next, gpe_entry);
630
631                 p += DOSPARTSIZE;
632                 if (next != NULL)
633                         ebr_entry_link(basetable, (uint32_t)next->gpe_start,
634                             (uint32_t)next->gpe_end, p);
635                 else
636                         bzero(p, DOSPARTSIZE);
637
638                 error = g_write_data(cp, baseentry->gpe_start * pp->sectorsize,
639                     buf, pp->sectorsize);
640 #ifndef GEOM_PART_EBR_COMPAT
641                 if (baseentry->gpe_start == 0)
642                         bzero(buf, DOSPARTOFF);
643 #endif
644                 baseentry = next;
645         } while (!error && baseentry != NULL);
646
647  out:
648         g_free(buf);
649         return (error);
650 }