xref: /freebsd/sys/geom/part/g_part_bsd.c (revision 5b9c547c)
1 /*-
2  * Copyright (c) 2007 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 <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/bio.h>
32 #include <sys/disklabel.h>
33 #include <sys/endian.h>
34 #include <sys/kernel.h>
35 #include <sys/kobj.h>
36 #include <sys/limits.h>
37 #include <sys/lock.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
40 #include <sys/queue.h>
41 #include <sys/sbuf.h>
42 #include <sys/systm.h>
43 #include <sys/sysctl.h>
44 #include <geom/geom.h>
45 #include <geom/part/g_part.h>
46 
47 #include "g_part_if.h"
48 
49 #define	BOOT1_SIZE	512
50 #define	LABEL_SIZE	512
51 #define	BOOT2_OFF	(BOOT1_SIZE + LABEL_SIZE)
52 #define	BOOT2_SIZE	(BBSIZE - BOOT2_OFF)
53 
54 FEATURE(geom_part_bsd, "GEOM partitioning class for BSD disklabels");
55 
56 struct g_part_bsd_table {
57 	struct g_part_table	base;
58 	u_char			*bbarea;
59 	uint32_t		offset;
60 };
61 
62 struct g_part_bsd_entry {
63 	struct g_part_entry	base;
64 	struct partition	part;
65 };
66 
67 static int g_part_bsd_add(struct g_part_table *, struct g_part_entry *,
68     struct g_part_parms *);
69 static int g_part_bsd_bootcode(struct g_part_table *, struct g_part_parms *);
70 static int g_part_bsd_create(struct g_part_table *, struct g_part_parms *);
71 static int g_part_bsd_destroy(struct g_part_table *, struct g_part_parms *);
72 static void g_part_bsd_dumpconf(struct g_part_table *, struct g_part_entry *,
73     struct sbuf *, const char *);
74 static int g_part_bsd_dumpto(struct g_part_table *, struct g_part_entry *);
75 static int g_part_bsd_modify(struct g_part_table *, struct g_part_entry *,
76     struct g_part_parms *);
77 static const char *g_part_bsd_name(struct g_part_table *, struct g_part_entry *,
78     char *, size_t);
79 static int g_part_bsd_probe(struct g_part_table *, struct g_consumer *);
80 static int g_part_bsd_read(struct g_part_table *, struct g_consumer *);
81 static const char *g_part_bsd_type(struct g_part_table *, struct g_part_entry *,
82     char *, size_t);
83 static int g_part_bsd_write(struct g_part_table *, struct g_consumer *);
84 static int g_part_bsd_resize(struct g_part_table *, struct g_part_entry *,
85     struct g_part_parms *);
86 
87 static kobj_method_t g_part_bsd_methods[] = {
88 	KOBJMETHOD(g_part_add,		g_part_bsd_add),
89 	KOBJMETHOD(g_part_bootcode,	g_part_bsd_bootcode),
90 	KOBJMETHOD(g_part_create,	g_part_bsd_create),
91 	KOBJMETHOD(g_part_destroy,	g_part_bsd_destroy),
92 	KOBJMETHOD(g_part_dumpconf,	g_part_bsd_dumpconf),
93 	KOBJMETHOD(g_part_dumpto,	g_part_bsd_dumpto),
94 	KOBJMETHOD(g_part_modify,	g_part_bsd_modify),
95 	KOBJMETHOD(g_part_resize,	g_part_bsd_resize),
96 	KOBJMETHOD(g_part_name,		g_part_bsd_name),
97 	KOBJMETHOD(g_part_probe,	g_part_bsd_probe),
98 	KOBJMETHOD(g_part_read,		g_part_bsd_read),
99 	KOBJMETHOD(g_part_type,		g_part_bsd_type),
100 	KOBJMETHOD(g_part_write,	g_part_bsd_write),
101 	{ 0, 0 }
102 };
103 
104 static struct g_part_scheme g_part_bsd_scheme = {
105 	"BSD",
106 	g_part_bsd_methods,
107 	sizeof(struct g_part_bsd_table),
108 	.gps_entrysz = sizeof(struct g_part_bsd_entry),
109 	.gps_minent = 8,
110 	.gps_maxent = 20,	/* Only 22 entries fit in 512 byte sectors */
111 	.gps_bootcodesz = BBSIZE,
112 };
113 G_PART_SCHEME_DECLARE(g_part_bsd);
114 
115 static struct g_part_bsd_alias {
116 	uint8_t		type;
117 	int		alias;
118 } bsd_alias_match[] = {
119 	{ FS_BSDFFS,	G_PART_ALIAS_FREEBSD_UFS },
120 	{ FS_SWAP,	G_PART_ALIAS_FREEBSD_SWAP },
121 	{ FS_ZFS,	G_PART_ALIAS_FREEBSD_ZFS },
122 	{ FS_VINUM,	G_PART_ALIAS_FREEBSD_VINUM },
123 	{ FS_NANDFS,	G_PART_ALIAS_FREEBSD_NANDFS },
124 	{ FS_HAMMER,	G_PART_ALIAS_DFBSD_HAMMER },
125 	{ FS_HAMMER2,	G_PART_ALIAS_DFBSD_HAMMER2 },
126 };
127 
128 static int
129 bsd_parse_type(const char *type, uint8_t *fstype)
130 {
131 	const char *alias;
132 	char *endp;
133 	long lt;
134 	int i;
135 
136 	if (type[0] == '!') {
137 		lt = strtol(type + 1, &endp, 0);
138 		if (type[1] == '\0' || *endp != '\0' || lt <= 0 || lt >= 256)
139 			return (EINVAL);
140 		*fstype = (u_int)lt;
141 		return (0);
142 	}
143 	for (i = 0;
144 	    i < sizeof(bsd_alias_match) / sizeof(bsd_alias_match[0]); i++) {
145 		alias = g_part_alias_name(bsd_alias_match[i].alias);
146 		if (strcasecmp(type, alias) == 0) {
147 			*fstype = bsd_alias_match[i].type;
148 			return (0);
149 		}
150 	}
151 	return (EINVAL);
152 }
153 
154 static int
155 g_part_bsd_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
156     struct g_part_parms *gpp)
157 {
158 	struct g_part_bsd_entry *entry;
159 	struct g_part_bsd_table *table;
160 
161 	if (gpp->gpp_parms & G_PART_PARM_LABEL)
162 		return (EINVAL);
163 
164 	entry = (struct g_part_bsd_entry *)baseentry;
165 	table = (struct g_part_bsd_table *)basetable;
166 
167 	entry->part.p_size = gpp->gpp_size;
168 	entry->part.p_offset = gpp->gpp_start + table->offset;
169 	entry->part.p_fsize = 0;
170 	entry->part.p_frag = 0;
171 	entry->part.p_cpg = 0;
172 	return (bsd_parse_type(gpp->gpp_type, &entry->part.p_fstype));
173 }
174 
175 static int
176 g_part_bsd_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp)
177 {
178 	struct g_part_bsd_table *table;
179 	const u_char *codeptr;
180 
181 	if (gpp->gpp_codesize != BOOT1_SIZE && gpp->gpp_codesize != BBSIZE)
182 		return (ENODEV);
183 
184 	table = (struct g_part_bsd_table *)basetable;
185 	codeptr = gpp->gpp_codeptr;
186 	bcopy(codeptr, table->bbarea, BOOT1_SIZE);
187 	if (gpp->gpp_codesize == BBSIZE)
188 		bcopy(codeptr + BOOT2_OFF, table->bbarea + BOOT2_OFF,
189 		    BOOT2_SIZE);
190 	return (0);
191 }
192 
193 static int
194 g_part_bsd_create(struct g_part_table *basetable, struct g_part_parms *gpp)
195 {
196 	struct g_provider *pp;
197 	struct g_part_entry *baseentry;
198 	struct g_part_bsd_entry *entry;
199 	struct g_part_bsd_table *table;
200 	u_char *ptr;
201 	uint32_t msize, ncyls, secpercyl;
202 
203 	pp = gpp->gpp_provider;
204 
205 	if (pp->sectorsize < sizeof(struct disklabel))
206 		return (ENOSPC);
207 	if (BBSIZE % pp->sectorsize)
208 		return (ENOTBLK);
209 
210 	msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
211 	secpercyl = basetable->gpt_sectors * basetable->gpt_heads;
212 	ncyls = msize / secpercyl;
213 
214 	table = (struct g_part_bsd_table *)basetable;
215 	table->bbarea = g_malloc(BBSIZE, M_WAITOK | M_ZERO);
216 	ptr = table->bbarea + pp->sectorsize;
217 
218 	le32enc(ptr + 0, DISKMAGIC);			/* d_magic */
219 	le32enc(ptr + 40, pp->sectorsize);		/* d_secsize */
220 	le32enc(ptr + 44, basetable->gpt_sectors);	/* d_nsectors */
221 	le32enc(ptr + 48, basetable->gpt_heads);	/* d_ntracks */
222 	le32enc(ptr + 52, ncyls);			/* d_ncylinders */
223 	le32enc(ptr + 56, secpercyl);			/* d_secpercyl */
224 	le32enc(ptr + 60, msize);			/* d_secperunit */
225 	le16enc(ptr + 72, 3600);			/* d_rpm */
226 	le32enc(ptr + 132, DISKMAGIC);			/* d_magic2 */
227 	le16enc(ptr + 138, basetable->gpt_entries);	/* d_npartitions */
228 	le32enc(ptr + 140, BBSIZE);			/* d_bbsize */
229 
230 	basetable->gpt_first = 0;
231 	basetable->gpt_last = msize - 1;
232 	basetable->gpt_isleaf = 1;
233 
234 	baseentry = g_part_new_entry(basetable, RAW_PART + 1,
235 	    basetable->gpt_first, basetable->gpt_last);
236 	baseentry->gpe_internal = 1;
237 	entry = (struct g_part_bsd_entry *)baseentry;
238 	entry->part.p_size = basetable->gpt_last + 1;
239 	entry->part.p_offset = table->offset;
240 
241 	return (0);
242 }
243 
244 static int
245 g_part_bsd_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
246 {
247 	struct g_part_bsd_table *table;
248 
249 	table = (struct g_part_bsd_table *)basetable;
250 	if (table->bbarea != NULL)
251 		g_free(table->bbarea);
252 	table->bbarea = NULL;
253 
254 	/* Wipe the second sector to clear the partitioning. */
255 	basetable->gpt_smhead |= 2;
256 	return (0);
257 }
258 
259 static void
260 g_part_bsd_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
261     struct sbuf *sb, const char *indent)
262 {
263 	struct g_part_bsd_entry *entry;
264 
265 	entry = (struct g_part_bsd_entry *)baseentry;
266 	if (indent == NULL) {
267 		/* conftxt: libdisk compatibility */
268 		sbuf_printf(sb, " xs BSD xt %u", entry->part.p_fstype);
269 	} else if (entry != NULL) {
270 		/* confxml: partition entry information */
271 		sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent,
272 		    entry->part.p_fstype);
273 	} else {
274 		/* confxml: scheme information */
275 	}
276 }
277 
278 static int
279 g_part_bsd_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
280 {
281 	struct g_part_bsd_entry *entry;
282 
283 	/* Allow dumping to a swap partition or an unused partition. */
284 	entry = (struct g_part_bsd_entry *)baseentry;
285 	return ((entry->part.p_fstype == FS_UNUSED ||
286 	    entry->part.p_fstype == FS_SWAP) ? 1 : 0);
287 }
288 
289 static int
290 g_part_bsd_modify(struct g_part_table *basetable,
291     struct g_part_entry *baseentry, struct g_part_parms *gpp)
292 {
293 	struct g_part_bsd_entry *entry;
294 
295 	if (gpp->gpp_parms & G_PART_PARM_LABEL)
296 		return (EINVAL);
297 
298 	entry = (struct g_part_bsd_entry *)baseentry;
299 	if (gpp->gpp_parms & G_PART_PARM_TYPE)
300 		return (bsd_parse_type(gpp->gpp_type, &entry->part.p_fstype));
301 	return (0);
302 }
303 
304 static void
305 bsd_set_rawsize(struct g_part_table *basetable, struct g_provider *pp)
306 {
307 	struct g_part_bsd_table *table;
308 	struct g_part_bsd_entry *entry;
309 	struct g_part_entry *baseentry;
310 	uint32_t msize;
311 
312 	table = (struct g_part_bsd_table *)basetable;
313 	msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
314 	le32enc(table->bbarea + pp->sectorsize + 60, msize); /* d_secperunit */
315 	basetable->gpt_last = msize - 1;
316 	LIST_FOREACH(baseentry, &basetable->gpt_entry, gpe_entry) {
317 		if (baseentry->gpe_index != RAW_PART + 1)
318 			continue;
319 		baseentry->gpe_end = basetable->gpt_last;
320 		entry = (struct g_part_bsd_entry *)baseentry;
321 		entry->part.p_size = msize;
322 		return;
323 	}
324 }
325 
326 static int
327 g_part_bsd_resize(struct g_part_table *basetable,
328     struct g_part_entry *baseentry, struct g_part_parms *gpp)
329 {
330 	struct g_part_bsd_entry *entry;
331 	struct g_provider *pp;
332 
333 	if (baseentry == NULL) {
334 		pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
335 		bsd_set_rawsize(basetable, pp);
336 		return (0);
337 	}
338 	entry = (struct g_part_bsd_entry *)baseentry;
339 	baseentry->gpe_end = baseentry->gpe_start + gpp->gpp_size - 1;
340 	entry->part.p_size = gpp->gpp_size;
341 
342 	return (0);
343 }
344 
345 static const char *
346 g_part_bsd_name(struct g_part_table *table, struct g_part_entry *baseentry,
347     char *buf, size_t bufsz)
348 {
349 
350 	snprintf(buf, bufsz, "%c", 'a' + baseentry->gpe_index - 1);
351 	return (buf);
352 }
353 
354 static int
355 g_part_bsd_probe(struct g_part_table *table, struct g_consumer *cp)
356 {
357 	struct g_provider *pp;
358 	u_char *buf;
359 	uint32_t magic1, magic2;
360 	int error;
361 
362 	pp = cp->provider;
363 
364 	/* Sanity-check the provider. */
365 	if (pp->sectorsize < sizeof(struct disklabel) ||
366 	    pp->mediasize < BBSIZE)
367 		return (ENOSPC);
368 	if (BBSIZE % pp->sectorsize)
369 		return (ENOTBLK);
370 
371 	/* Check that there's a disklabel. */
372 	buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error);
373 	if (buf == NULL)
374 		return (error);
375 	magic1 = le32dec(buf + 0);
376 	magic2 = le32dec(buf + 132);
377 	g_free(buf);
378 	return ((magic1 == DISKMAGIC && magic2 == DISKMAGIC)
379 	    ? G_PART_PROBE_PRI_HIGH : ENXIO);
380 }
381 
382 static int
383 g_part_bsd_read(struct g_part_table *basetable, struct g_consumer *cp)
384 {
385 	struct g_provider *pp;
386 	struct g_part_bsd_table *table;
387 	struct g_part_entry *baseentry;
388 	struct g_part_bsd_entry *entry;
389 	struct partition part;
390 	u_char *buf, *p;
391 	off_t chs, msize;
392 	u_int sectors, heads;
393 	int error, index;
394 
395 	pp = cp->provider;
396 	table = (struct g_part_bsd_table *)basetable;
397 	msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
398 
399 	table->bbarea = g_read_data(cp, 0, BBSIZE, &error);
400 	if (table->bbarea == NULL)
401 		return (error);
402 
403 	buf = table->bbarea + pp->sectorsize;
404 
405 	if (le32dec(buf + 40) != pp->sectorsize)
406 		goto invalid_label;
407 	sectors = le32dec(buf + 44);
408 	if (sectors < 1 || sectors > 255)
409 		goto invalid_label;
410 	if (sectors != basetable->gpt_sectors && !basetable->gpt_fixgeom) {
411 		g_part_geometry_heads(msize, sectors, &chs, &heads);
412 		if (chs != 0) {
413 			basetable->gpt_sectors = sectors;
414 			basetable->gpt_heads = heads;
415 		}
416 	}
417 	heads = le32dec(buf + 48);
418 	if (heads < 1 || heads > 255)
419 		goto invalid_label;
420 	if (heads != basetable->gpt_heads && !basetable->gpt_fixgeom)
421 		basetable->gpt_heads = heads;
422 
423 	chs = le32dec(buf + 60);
424 	if (chs < 1)
425 		goto invalid_label;
426 	/* Fix-up a sysinstall bug. */
427 	if (chs > msize) {
428 		chs = msize;
429 		le32enc(buf + 60, msize);
430 	}
431 
432 	basetable->gpt_first = 0;
433 	basetable->gpt_last = msize - 1;
434 	basetable->gpt_isleaf = 1;
435 
436 	basetable->gpt_entries = le16dec(buf + 138);
437 	if (basetable->gpt_entries < g_part_bsd_scheme.gps_minent ||
438 	    basetable->gpt_entries > g_part_bsd_scheme.gps_maxent)
439 		goto invalid_label;
440 
441 	table->offset = le32dec(buf + 148 + RAW_PART * 16 + 4);
442 	for (index = basetable->gpt_entries - 1; index >= 0; index--) {
443 		p = buf + 148 + index * 16;
444 		part.p_size = le32dec(p + 0);
445 		part.p_offset = le32dec(p + 4);
446 		part.p_fsize = le32dec(p + 8);
447 		part.p_fstype = p[12];
448 		part.p_frag = p[13];
449 		part.p_cpg = le16dec(p + 14);
450 		if (part.p_size == 0)
451 			continue;
452 		if (part.p_offset < table->offset)
453 			continue;
454 		if (part.p_offset - table->offset > basetable->gpt_last)
455 			goto invalid_label;
456 		baseentry = g_part_new_entry(basetable, index + 1,
457 		    part.p_offset - table->offset,
458 		    part.p_offset - table->offset + part.p_size - 1);
459 		entry = (struct g_part_bsd_entry *)baseentry;
460 		entry->part = part;
461 		if (index == RAW_PART)
462 			baseentry->gpe_internal = 1;
463 	}
464 
465 	return (0);
466 
467  invalid_label:
468 	printf("GEOM: %s: invalid disklabel.\n", pp->name);
469 	g_free(table->bbarea);
470 	table->bbarea = NULL;
471 	return (EINVAL);
472 }
473 
474 static const char *
475 g_part_bsd_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
476     char *buf, size_t bufsz)
477 {
478 	struct g_part_bsd_entry *entry;
479 	int type;
480 
481 	entry = (struct g_part_bsd_entry *)baseentry;
482 	type = entry->part.p_fstype;
483 	if (type == FS_NANDFS)
484 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_NANDFS));
485 	if (type == FS_SWAP)
486 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP));
487 	if (type == FS_BSDFFS)
488 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS));
489 	if (type == FS_VINUM)
490 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM));
491 	if (type == FS_ZFS)
492 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS));
493 	snprintf(buf, bufsz, "!%d", type);
494 	return (buf);
495 }
496 
497 static int
498 g_part_bsd_write(struct g_part_table *basetable, struct g_consumer *cp)
499 {
500 	struct g_provider *pp;
501 	struct g_part_entry *baseentry;
502 	struct g_part_bsd_entry *entry;
503 	struct g_part_bsd_table *table;
504 	uint16_t sum;
505 	u_char *label, *p, *pe;
506 	int error, index;
507 
508 	pp = cp->provider;
509 	table = (struct g_part_bsd_table *)basetable;
510 	baseentry = LIST_FIRST(&basetable->gpt_entry);
511 	label = table->bbarea + pp->sectorsize;
512 	for (index = 1; index <= basetable->gpt_entries; index++) {
513 		p = label + 148 + (index - 1) * 16;
514 		entry = (baseentry != NULL && index == baseentry->gpe_index)
515 		    ? (struct g_part_bsd_entry *)baseentry : NULL;
516 		if (entry != NULL && !baseentry->gpe_deleted) {
517 			le32enc(p + 0, entry->part.p_size);
518 			le32enc(p + 4, entry->part.p_offset);
519 			le32enc(p + 8, entry->part.p_fsize);
520 			p[12] = entry->part.p_fstype;
521 			p[13] = entry->part.p_frag;
522 			le16enc(p + 14, entry->part.p_cpg);
523 		} else
524 			bzero(p, 16);
525 
526 		if (entry != NULL)
527 			baseentry = LIST_NEXT(baseentry, gpe_entry);
528 	}
529 
530 	/* Calculate checksum. */
531 	le16enc(label + 136, 0);
532 	pe = label + 148 + basetable->gpt_entries * 16;
533 	sum = 0;
534 	for (p = label; p < pe; p += 2)
535 		sum ^= le16dec(p);
536 	le16enc(label + 136, sum);
537 
538 	error = g_write_data(cp, 0, table->bbarea, BBSIZE);
539 	return (error);
540 }
541