1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2008 Marcel Moolenaar
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD$");
31
32#include <sys/param.h>
33#include <sys/bio.h>
34#include <sys/endian.h>
35#include <sys/kernel.h>
36#include <sys/kobj.h>
37#include <sys/limits.h>
38#include <sys/lock.h>
39#include <sys/malloc.h>
40#include <sys/mutex.h>
41#include <sys/queue.h>
42#include <sys/sbuf.h>
43#include <sys/systm.h>
44#include <sys/sysctl.h>
45#include <sys/vtoc.h>
46#include <geom/geom.h>
47#include <geom/geom_int.h>
48#include <geom/part/g_part.h>
49
50#include "g_part_if.h"
51
52FEATURE(geom_part_vtoc8, "GEOM partitioning class for SMI VTOC8 disk labels");
53
54struct g_part_vtoc8_table {
55	struct g_part_table	base;
56	struct vtoc8		vtoc;
57	uint32_t		secpercyl;
58};
59
60static int g_part_vtoc8_add(struct g_part_table *, struct g_part_entry *,
61    struct g_part_parms *);
62static int g_part_vtoc8_create(struct g_part_table *, struct g_part_parms *);
63static int g_part_vtoc8_destroy(struct g_part_table *, struct g_part_parms *);
64static void g_part_vtoc8_dumpconf(struct g_part_table *,
65    struct g_part_entry *, struct sbuf *, const char *);
66static int g_part_vtoc8_dumpto(struct g_part_table *, struct g_part_entry *);
67static int g_part_vtoc8_modify(struct g_part_table *, struct g_part_entry *,
68    struct g_part_parms *);
69static const char *g_part_vtoc8_name(struct g_part_table *,
70    struct g_part_entry *, char *, size_t);
71static int g_part_vtoc8_probe(struct g_part_table *, struct g_consumer *);
72static int g_part_vtoc8_read(struct g_part_table *, struct g_consumer *);
73static const char *g_part_vtoc8_type(struct g_part_table *,
74    struct g_part_entry *, char *, size_t);
75static int g_part_vtoc8_write(struct g_part_table *, struct g_consumer *);
76static int g_part_vtoc8_resize(struct g_part_table *, struct g_part_entry *,
77    struct g_part_parms *);
78
79static kobj_method_t g_part_vtoc8_methods[] = {
80	KOBJMETHOD(g_part_add,		g_part_vtoc8_add),
81	KOBJMETHOD(g_part_create,	g_part_vtoc8_create),
82	KOBJMETHOD(g_part_destroy,	g_part_vtoc8_destroy),
83	KOBJMETHOD(g_part_dumpconf,	g_part_vtoc8_dumpconf),
84	KOBJMETHOD(g_part_dumpto,	g_part_vtoc8_dumpto),
85	KOBJMETHOD(g_part_modify,	g_part_vtoc8_modify),
86	KOBJMETHOD(g_part_resize,	g_part_vtoc8_resize),
87	KOBJMETHOD(g_part_name,		g_part_vtoc8_name),
88	KOBJMETHOD(g_part_probe,	g_part_vtoc8_probe),
89	KOBJMETHOD(g_part_read,		g_part_vtoc8_read),
90	KOBJMETHOD(g_part_type,		g_part_vtoc8_type),
91	KOBJMETHOD(g_part_write,	g_part_vtoc8_write),
92	{ 0, 0 }
93};
94
95static struct g_part_scheme g_part_vtoc8_scheme = {
96	"VTOC8",
97	g_part_vtoc8_methods,
98	sizeof(struct g_part_vtoc8_table),
99	.gps_entrysz = sizeof(struct g_part_entry),
100	.gps_minent = VTOC8_NPARTS,
101	.gps_maxent = VTOC8_NPARTS,
102};
103G_PART_SCHEME_DECLARE(g_part_vtoc8);
104MODULE_VERSION(geom_part_vtoc8, 0);
105
106static int
107vtoc8_parse_type(const char *type, uint16_t *tag)
108{
109	const char *alias;
110	char *endp;
111	long lt;
112
113	if (type[0] == '!') {
114		lt = strtol(type + 1, &endp, 0);
115		if (type[1] == '\0' || *endp != '\0' || lt <= 0 ||
116		    lt >= 65536)
117			return (EINVAL);
118		*tag = (uint16_t)lt;
119		return (0);
120	}
121	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_NANDFS);
122	if (!strcasecmp(type, alias)) {
123		*tag = VTOC_TAG_FREEBSD_NANDFS;
124		return (0);
125	}
126	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP);
127	if (!strcasecmp(type, alias)) {
128		*tag = VTOC_TAG_FREEBSD_SWAP;
129		return (0);
130	}
131	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS);
132	if (!strcasecmp(type, alias)) {
133		*tag = VTOC_TAG_FREEBSD_UFS;
134		return (0);
135	}
136	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM);
137	if (!strcasecmp(type, alias)) {
138		*tag = VTOC_TAG_FREEBSD_VINUM;
139		return (0);
140	}
141	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS);
142	if (!strcasecmp(type, alias)) {
143		*tag = VTOC_TAG_FREEBSD_ZFS;
144		return (0);
145	}
146	return (EINVAL);
147}
148
149static int
150vtoc8_align(struct g_part_vtoc8_table *table, uint64_t *start, uint64_t *size)
151{
152
153	if (*size < table->secpercyl)
154		return (EINVAL);
155	if (start != NULL && (*start % table->secpercyl)) {
156		*size += (*start % table->secpercyl) - table->secpercyl;
157		*start -= (*start % table->secpercyl) - table->secpercyl;
158	}
159	if (*size % table->secpercyl)
160		*size -= (*size % table->secpercyl);
161	if (*size < table->secpercyl)
162		return (EINVAL);
163	return (0);
164}
165
166static int
167g_part_vtoc8_add(struct g_part_table *basetable, struct g_part_entry *entry,
168    struct g_part_parms *gpp)
169{
170	struct g_part_vtoc8_table *table;
171	int error, index;
172	uint64_t start, size;
173	uint16_t tag;
174
175	if (gpp->gpp_parms & G_PART_PARM_LABEL)
176		return (EINVAL);
177
178	error = vtoc8_parse_type(gpp->gpp_type, &tag);
179	if (error)
180		return (error);
181
182	table = (struct g_part_vtoc8_table *)basetable;
183	index = entry->gpe_index - 1;
184	start = gpp->gpp_start;
185	size = gpp->gpp_size;
186	if (vtoc8_align(table, &start, &size) != 0)
187		return (EINVAL);
188
189	KASSERT(entry->gpe_start <= start, (__func__));
190	KASSERT(entry->gpe_end >= start + size - 1, (__func__));
191	entry->gpe_start = start;
192	entry->gpe_end = start + size - 1;
193
194	be16enc(&table->vtoc.part[index].tag, tag);
195	be16enc(&table->vtoc.part[index].flag, 0);
196	be32enc(&table->vtoc.timestamp[index], 0);
197	be32enc(&table->vtoc.map[index].cyl, start / table->secpercyl);
198	be32enc(&table->vtoc.map[index].nblks, size);
199	return (0);
200}
201
202static int
203g_part_vtoc8_create(struct g_part_table *basetable, struct g_part_parms *gpp)
204{
205	struct g_provider *pp;
206	struct g_part_entry *entry;
207	struct g_part_vtoc8_table *table;
208	uint64_t msize;
209	uint32_t acyls, ncyls, pcyls;
210
211	pp = gpp->gpp_provider;
212
213	if (pp->sectorsize < sizeof(struct vtoc8))
214		return (ENOSPC);
215	if (pp->sectorsize > sizeof(struct vtoc8))
216		return (ENXIO);
217
218	table = (struct g_part_vtoc8_table *)basetable;
219
220	msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
221	table->secpercyl = basetable->gpt_sectors * basetable->gpt_heads;
222	pcyls = msize / table->secpercyl;
223	acyls = 2;
224	ncyls = pcyls - acyls;
225	msize = ncyls * table->secpercyl;
226
227	snprintf(table->vtoc.ascii, sizeof(table->vtoc.ascii),
228	    "FreeBSD%lldM cyl %u alt %u hd %u sec %u",
229	    (long long)(msize / 2048), ncyls, acyls, basetable->gpt_heads,
230	    basetable->gpt_sectors);
231	be32enc(&table->vtoc.version, VTOC_VERSION);
232	be16enc(&table->vtoc.nparts, VTOC8_NPARTS);
233	be32enc(&table->vtoc.sanity, VTOC_SANITY);
234	be16enc(&table->vtoc.rpm, 3600);
235	be16enc(&table->vtoc.physcyls, pcyls);
236	be16enc(&table->vtoc.ncyls, ncyls);
237	be16enc(&table->vtoc.altcyls, acyls);
238	be16enc(&table->vtoc.nheads, basetable->gpt_heads);
239	be16enc(&table->vtoc.nsecs, basetable->gpt_sectors);
240	be16enc(&table->vtoc.magic, VTOC_MAGIC);
241
242	basetable->gpt_first = 0;
243	basetable->gpt_last = msize - 1;
244	basetable->gpt_isleaf = 1;
245
246	entry = g_part_new_entry(basetable, VTOC_RAW_PART + 1,
247	    basetable->gpt_first, basetable->gpt_last);
248	entry->gpe_internal = 1;
249	be16enc(&table->vtoc.part[VTOC_RAW_PART].tag, VTOC_TAG_BACKUP);
250	be32enc(&table->vtoc.map[VTOC_RAW_PART].nblks, msize);
251	return (0);
252}
253
254static int
255g_part_vtoc8_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
256{
257
258	/* Wipe the first sector to clear the partitioning. */
259	basetable->gpt_smhead |= 1;
260	return (0);
261}
262
263static void
264g_part_vtoc8_dumpconf(struct g_part_table *basetable,
265    struct g_part_entry *entry, struct sbuf *sb, const char *indent)
266{
267	struct g_part_vtoc8_table *table;
268
269	table = (struct g_part_vtoc8_table *)basetable;
270	if (indent == NULL) {
271		/* conftxt: libdisk compatibility */
272		sbuf_printf(sb, " xs SUN sc %u hd %u alt %u",
273		    be16dec(&table->vtoc.nsecs), be16dec(&table->vtoc.nheads),
274		    be16dec(&table->vtoc.altcyls));
275	} else if (entry != NULL) {
276		/* confxml: partition entry information */
277		sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent,
278		    be16dec(&table->vtoc.part[entry->gpe_index - 1].tag));
279	} else {
280		/* confxml: scheme information */
281	}
282}
283
284static int
285g_part_vtoc8_dumpto(struct g_part_table *basetable,
286    struct g_part_entry *entry)
287{
288	struct g_part_vtoc8_table *table;
289	uint16_t tag;
290
291	/*
292	 * Allow dumping to a swap partition or a partition that
293	 * has no type.
294	 */
295	table = (struct g_part_vtoc8_table *)basetable;
296	tag = be16dec(&table->vtoc.part[entry->gpe_index - 1].tag);
297	return ((tag == 0 || tag == VTOC_TAG_FREEBSD_SWAP ||
298	    tag == VTOC_TAG_SWAP) ? 1 : 0);
299}
300
301static int
302g_part_vtoc8_modify(struct g_part_table *basetable,
303    struct g_part_entry *entry, struct g_part_parms *gpp)
304{
305	struct g_part_vtoc8_table *table;
306	int error;
307	uint16_t tag;
308
309	if (gpp->gpp_parms & G_PART_PARM_LABEL)
310		return (EINVAL);
311
312	table = (struct g_part_vtoc8_table *)basetable;
313	if (gpp->gpp_parms & G_PART_PARM_TYPE) {
314		error = vtoc8_parse_type(gpp->gpp_type, &tag);
315		if (error)
316			return(error);
317
318		be16enc(&table->vtoc.part[entry->gpe_index - 1].tag, tag);
319	}
320	return (0);
321}
322
323static int
324vtoc8_set_rawsize(struct g_part_table *basetable, struct g_provider *pp)
325{
326	struct g_part_vtoc8_table *table;
327	struct g_part_entry *baseentry;
328	off_t msize;
329	uint32_t acyls, ncyls, pcyls;
330
331	table = (struct g_part_vtoc8_table *)basetable;
332	msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
333	pcyls = msize / table->secpercyl;
334	if (pcyls > UINT16_MAX)
335		return (ERANGE);
336	acyls = be16dec(&table->vtoc.altcyls);
337	ncyls = pcyls - acyls;
338	msize = ncyls * table->secpercyl;
339	basetable->gpt_last = msize - 1;
340
341	bzero(table->vtoc.ascii, sizeof(table->vtoc.ascii));
342	snprintf(table->vtoc.ascii, sizeof(table->vtoc.ascii),
343	    "FreeBSD%lldM cyl %u alt %u hd %u sec %u",
344	    (long long)(msize / 2048), ncyls, acyls, basetable->gpt_heads,
345	    basetable->gpt_sectors);
346	be16enc(&table->vtoc.physcyls, pcyls);
347	be16enc(&table->vtoc.ncyls, ncyls);
348	be32enc(&table->vtoc.map[VTOC_RAW_PART].nblks, msize);
349	if (be32dec(&table->vtoc.sanity) == VTOC_SANITY)
350		be16enc(&table->vtoc.part[VTOC_RAW_PART].tag, VTOC_TAG_BACKUP);
351	LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) {
352		if (baseentry->gpe_index == VTOC_RAW_PART + 1) {
353			baseentry->gpe_end = basetable->gpt_last;
354			return (0);
355		}
356	}
357	return (ENXIO);
358}
359
360static int
361g_part_vtoc8_resize(struct g_part_table *basetable,
362    struct g_part_entry *entry, struct g_part_parms *gpp)
363{
364	struct g_part_vtoc8_table *table;
365	struct g_provider *pp;
366	uint64_t size;
367
368	if (entry == NULL) {
369		pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
370		return (vtoc8_set_rawsize(basetable, pp));
371	}
372	table = (struct g_part_vtoc8_table *)basetable;
373	size = gpp->gpp_size;
374	if (vtoc8_align(table, NULL, &size) != 0)
375		return (EINVAL);
376	/* XXX: prevent unexpected shrinking. */
377	pp = entry->gpe_pp;
378	if ((g_debugflags & G_F_FOOTSHOOTING) == 0 && size < gpp->gpp_size &&
379	    pp->mediasize / pp->sectorsize > size)
380		return (EBUSY);
381	entry->gpe_end = entry->gpe_start + size - 1;
382	be32enc(&table->vtoc.map[entry->gpe_index - 1].nblks, size);
383
384	return (0);
385}
386
387static const char *
388g_part_vtoc8_name(struct g_part_table *table, struct g_part_entry *baseentry,
389    char *buf, size_t bufsz)
390{
391
392	snprintf(buf, bufsz, "%c", 'a' + baseentry->gpe_index - 1);
393	return (buf);
394}
395
396static int
397g_part_vtoc8_probe(struct g_part_table *table, struct g_consumer *cp)
398{
399	struct g_provider *pp;
400	u_char *buf;
401	int error, ofs, res;
402	uint16_t cksum, magic;
403
404	pp = cp->provider;
405
406	/* Sanity-check the provider. */
407	if (pp->sectorsize != sizeof(struct vtoc8))
408		return (ENOSPC);
409
410	/* Check that there's a disklabel. */
411	buf = g_read_data(cp, 0, pp->sectorsize, &error);
412	if (buf == NULL)
413		return (error);
414
415	res = ENXIO;	/* Assume mismatch */
416
417	/* Check the magic */
418	magic = be16dec(buf + offsetof(struct vtoc8, magic));
419	if (magic != VTOC_MAGIC)
420		goto out;
421
422	/* Check the sum */
423	cksum = 0;
424	for (ofs = 0; ofs < sizeof(struct vtoc8); ofs += 2)
425		cksum ^= be16dec(buf + ofs);
426	if (cksum != 0)
427		goto out;
428
429	res = G_PART_PROBE_PRI_NORM;
430
431 out:
432	g_free(buf);
433	return (res);
434}
435
436static int
437g_part_vtoc8_read(struct g_part_table *basetable, struct g_consumer *cp)
438{
439	struct g_provider *pp;
440	struct g_part_vtoc8_table *table;
441	struct g_part_entry *entry;
442	u_char *buf;
443	off_t chs, msize;
444	uint64_t offset, size;
445	u_int cyls, heads, sectors;
446	int error, index, withtags;
447	uint16_t tag;
448
449	pp = cp->provider;
450	buf = g_read_data(cp, 0, pp->sectorsize, &error);
451	if (buf == NULL)
452		return (error);
453
454	table = (struct g_part_vtoc8_table *)basetable;
455	bcopy(buf, &table->vtoc, sizeof(table->vtoc));
456	g_free(buf);
457
458	msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
459	sectors = be16dec(&table->vtoc.nsecs);
460	if (sectors < 1)
461		goto invalid_label;
462	if (sectors != basetable->gpt_sectors && !basetable->gpt_fixgeom) {
463		g_part_geometry_heads(msize, sectors, &chs, &heads);
464		if (chs != 0) {
465			basetable->gpt_sectors = sectors;
466			basetable->gpt_heads = heads;
467		}
468	}
469
470	heads = be16dec(&table->vtoc.nheads);
471	if (heads < 1)
472		goto invalid_label;
473	if (heads != basetable->gpt_heads && !basetable->gpt_fixgeom)
474		basetable->gpt_heads = heads;
475	/*
476	 * Except for ATA disks > 32GB, Solaris uses the native geometry
477	 * as reported by the target for the labels while da(4) typically
478	 * uses a synthetic one so we don't complain too loudly if these
479	 * geometries don't match.
480	 */
481	if (bootverbose && (sectors != basetable->gpt_sectors ||
482	    heads != basetable->gpt_heads))
483		printf("GEOM: %s: geometry does not match VTOC8 label "
484		    "(label: %uh,%us GEOM: %uh,%us).\n", pp->name, heads,
485		    sectors, basetable->gpt_heads, basetable->gpt_sectors);
486
487	table->secpercyl = heads * sectors;
488	cyls = be16dec(&table->vtoc.ncyls);
489	chs = cyls * table->secpercyl;
490	if (chs < 1 || chs > msize)
491		goto invalid_label;
492
493	basetable->gpt_first = 0;
494	basetable->gpt_last = chs - 1;
495	basetable->gpt_isleaf = 1;
496
497	withtags = (be32dec(&table->vtoc.sanity) == VTOC_SANITY) ? 1 : 0;
498	if (!withtags) {
499		printf("GEOM: %s: adding VTOC8 information.\n", pp->name);
500		be32enc(&table->vtoc.version, VTOC_VERSION);
501		bzero(&table->vtoc.volume, VTOC_VOLUME_LEN);
502		be16enc(&table->vtoc.nparts, VTOC8_NPARTS);
503		bzero(&table->vtoc.part, sizeof(table->vtoc.part));
504		be32enc(&table->vtoc.sanity, VTOC_SANITY);
505	}
506
507	basetable->gpt_entries = be16dec(&table->vtoc.nparts);
508	if (basetable->gpt_entries < g_part_vtoc8_scheme.gps_minent ||
509	    basetable->gpt_entries > g_part_vtoc8_scheme.gps_maxent)
510		goto invalid_label;
511
512	for (index = basetable->gpt_entries - 1; index >= 0; index--) {
513		offset = be32dec(&table->vtoc.map[index].cyl) *
514		    table->secpercyl;
515		size = be32dec(&table->vtoc.map[index].nblks);
516		if (size == 0)
517			continue;
518		if (withtags)
519			tag = be16dec(&table->vtoc.part[index].tag);
520		else
521			tag = (index == VTOC_RAW_PART)
522			    ? VTOC_TAG_BACKUP
523			    : VTOC_TAG_UNASSIGNED;
524
525		if (index == VTOC_RAW_PART && tag != VTOC_TAG_BACKUP)
526			continue;
527		if (index != VTOC_RAW_PART && tag == VTOC_TAG_BACKUP)
528			continue;
529		entry = g_part_new_entry(basetable, index + 1, offset,
530		    offset + size - 1);
531		if (tag == VTOC_TAG_BACKUP)
532			entry->gpe_internal = 1;
533
534		if (!withtags)
535			be16enc(&table->vtoc.part[index].tag, tag);
536	}
537
538	return (0);
539
540 invalid_label:
541	printf("GEOM: %s: invalid VTOC8 label.\n", pp->name);
542	return (EINVAL);
543}
544
545static const char *
546g_part_vtoc8_type(struct g_part_table *basetable, struct g_part_entry *entry,
547    char *buf, size_t bufsz)
548{
549	struct g_part_vtoc8_table *table;
550	uint16_t tag;
551
552	table = (struct g_part_vtoc8_table *)basetable;
553	tag = be16dec(&table->vtoc.part[entry->gpe_index - 1].tag);
554	if (tag == VTOC_TAG_FREEBSD_NANDFS)
555		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_NANDFS));
556	if (tag == VTOC_TAG_FREEBSD_SWAP)
557		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP));
558	if (tag == VTOC_TAG_FREEBSD_UFS)
559		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS));
560	if (tag == VTOC_TAG_FREEBSD_VINUM)
561		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM));
562	if (tag == VTOC_TAG_FREEBSD_ZFS)
563		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS));
564	snprintf(buf, bufsz, "!%d", tag);
565	return (buf);
566}
567
568static int
569g_part_vtoc8_write(struct g_part_table *basetable, struct g_consumer *cp)
570{
571	struct g_provider *pp;
572	struct g_part_entry *entry;
573	struct g_part_vtoc8_table *table;
574	uint16_t sum;
575	u_char *p;
576	int error, index, match, offset;
577
578	pp = cp->provider;
579	table = (struct g_part_vtoc8_table *)basetable;
580	entry = LIST_FIRST(&basetable->gpt_entry);
581	for (index = 0; index < basetable->gpt_entries; index++) {
582		match = (entry != NULL && index == entry->gpe_index - 1)
583		    ? 1 : 0;
584		if (match) {
585			if (entry->gpe_deleted) {
586				be16enc(&table->vtoc.part[index].tag, 0);
587				be16enc(&table->vtoc.part[index].flag, 0);
588				be32enc(&table->vtoc.map[index].cyl, 0);
589				be32enc(&table->vtoc.map[index].nblks, 0);
590			}
591			entry = LIST_NEXT(entry, gpe_entry);
592		}
593	}
594
595	/* Calculate checksum. */
596	sum = 0;
597	p = (void *)&table->vtoc;
598	for (offset = 0; offset < sizeof(table->vtoc) - 2; offset += 2)
599		sum ^= be16dec(p + offset);
600	be16enc(&table->vtoc.cksum, sum);
601
602	error = g_write_data(cp, 0, p, pp->sectorsize);
603	return (error);
604}
605