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