xref: /freebsd/sys/geom/part/g_part_bsd.c (revision 9336e0699bda8a301cd2bfa37106b6ec5e32012e)
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 <geom/geom.h>
44 #include <geom/part/g_part.h>
45 
46 #include "g_part_if.h"
47 
48 struct g_part_bsd_table {
49 	struct g_part_table	base;
50 	u_char			*label;
51 	uint32_t		offset;
52 };
53 
54 struct g_part_bsd_entry {
55 	struct g_part_entry	base;
56 	struct partition	part;
57 };
58 
59 static int g_part_bsd_add(struct g_part_table *, struct g_part_entry *,
60     struct g_part_parms *);
61 static int g_part_bsd_create(struct g_part_table *, struct g_part_parms *);
62 static int g_part_bsd_destroy(struct g_part_table *, struct g_part_parms *);
63 static int g_part_bsd_dumpto(struct g_part_table *, struct g_part_entry *);
64 static int g_part_bsd_modify(struct g_part_table *, struct g_part_entry *,
65     struct g_part_parms *);
66 static char *g_part_bsd_name(struct g_part_table *, struct g_part_entry *,
67     char *, size_t);
68 static int g_part_bsd_probe(struct g_part_table *, struct g_consumer *);
69 static int g_part_bsd_read(struct g_part_table *, struct g_consumer *);
70 static const char *g_part_bsd_type(struct g_part_table *, struct g_part_entry *,
71     char *, size_t);
72 static int g_part_bsd_write(struct g_part_table *, struct g_consumer *);
73 
74 static kobj_method_t g_part_bsd_methods[] = {
75 	KOBJMETHOD(g_part_add,		g_part_bsd_add),
76 	KOBJMETHOD(g_part_create,	g_part_bsd_create),
77 	KOBJMETHOD(g_part_destroy,	g_part_bsd_destroy),
78 	KOBJMETHOD(g_part_dumpto,	g_part_bsd_dumpto),
79 	KOBJMETHOD(g_part_modify,	g_part_bsd_modify),
80 	KOBJMETHOD(g_part_name,		g_part_bsd_name),
81 	KOBJMETHOD(g_part_probe,	g_part_bsd_probe),
82 	KOBJMETHOD(g_part_read,		g_part_bsd_read),
83 	KOBJMETHOD(g_part_type,		g_part_bsd_type),
84 	KOBJMETHOD(g_part_write,	g_part_bsd_write),
85 	{ 0, 0 }
86 };
87 
88 static struct g_part_scheme g_part_bsd_scheme = {
89 	"BSD",
90 	g_part_bsd_methods,
91 	sizeof(struct g_part_bsd_table),
92 	.gps_entrysz = sizeof(struct g_part_bsd_entry),
93 	.gps_minent = 8,
94 	.gps_maxent = 20,
95 };
96 G_PART_SCHEME_DECLARE(g_part_bsd_scheme);
97 
98 static int
99 bsd_parse_type(const char *type, uint8_t *fstype)
100 {
101 	const char *alias;
102 	char *endp;
103 	long lt;
104 
105 	if (type[0] == '!') {
106 		lt = strtol(type + 1, &endp, 0);
107 		if (type[1] == '\0' || *endp != '\0' || lt <= 0 || lt >= 256)
108 			return (EINVAL);
109 		*fstype = (u_int)lt;
110 		return (0);
111 	}
112 	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP);
113 	if (!strcasecmp(type, alias)) {
114 		*fstype = FS_SWAP;
115 		return (0);
116 	}
117 	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS);
118 	if (!strcasecmp(type, alias)) {
119 		*fstype = FS_BSDFFS;
120 		return (0);
121 	}
122 	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM);
123 	if (!strcasecmp(type, alias)) {
124 		*fstype = FS_VINUM;
125 		return (0);
126 	}
127 	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS);
128 	if (!strcasecmp(type, alias)) {
129 		*fstype = FS_ZFS;
130 		return (0);
131 	}
132 	return (EINVAL);
133 }
134 
135 static int
136 g_part_bsd_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
137     struct g_part_parms *gpp)
138 {
139 	struct g_part_bsd_entry *entry;
140 	struct g_part_bsd_table *table;
141 
142 	if (gpp->gpp_parms & G_PART_PARM_LABEL)
143 		return (EINVAL);
144 
145 	entry = (struct g_part_bsd_entry *)baseentry;
146 	table = (struct g_part_bsd_table *)basetable;
147 
148 	entry->part.p_size = gpp->gpp_size;
149 	entry->part.p_offset = gpp->gpp_start + table->offset;
150 	entry->part.p_fsize = 0;
151 	entry->part.p_frag = 0;
152 	entry->part.p_cpg = 0;
153 	return (bsd_parse_type(gpp->gpp_type, &entry->part.p_fstype));
154 }
155 
156 static int
157 g_part_bsd_create(struct g_part_table *basetable, struct g_part_parms *gpp)
158 {
159 	struct g_consumer *cp;
160 	struct g_provider *pp;
161 	struct g_part_entry *baseentry;
162 	struct g_part_bsd_entry *entry;
163 	struct g_part_bsd_table *table;
164 	u_char *ptr;
165 	uint64_t msize;
166 	uint32_t ncyls, secpercyl;
167 
168 	pp = gpp->gpp_provider;
169 	cp = LIST_FIRST(&pp->consumers);
170 
171 	if (pp->sectorsize < sizeof(struct disklabel))
172 		return (ENOSPC);
173 
174 	msize = pp->mediasize / pp->sectorsize;
175 	secpercyl = basetable->gpt_sectors * basetable->gpt_heads;
176 	ncyls = msize / secpercyl;
177 
178 	table = (struct g_part_bsd_table *)basetable;
179 	ptr = table->label = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
180 
181 	le32enc(ptr + 0, DISKMAGIC);			/* d_magic */
182 	le32enc(ptr + 40, pp->sectorsize);		/* d_secsize */
183 	le32enc(ptr + 44, basetable->gpt_sectors);	/* d_nsectors */
184 	le32enc(ptr + 48, basetable->gpt_heads);	/* d_ntracks */
185 	le32enc(ptr + 52, ncyls);			/* d_ncylinders */
186 	le32enc(ptr + 56, secpercyl);			/* d_secpercyl */
187 	le32enc(ptr + 60, msize);			/* d_secperunit */
188 	le16enc(ptr + 72, 3600);			/* d_rpm */
189 	le32enc(ptr + 132, DISKMAGIC);			/* d_magic2 */
190 	le16enc(ptr + 138, basetable->gpt_entries);	/* d_npartitions */
191 	le32enc(ptr + 140, BBSIZE);			/* d_bbsize */
192 
193 	basetable->gpt_first = 0;
194 	basetable->gpt_last = msize - 1;
195 	basetable->gpt_isleaf = 1;
196 
197 	baseentry = g_part_new_entry(basetable, RAW_PART + 1,
198 	    basetable->gpt_first, basetable->gpt_last);
199 	baseentry->gpe_internal = 1;
200 	entry = (struct g_part_bsd_entry *)baseentry;
201 	entry->part.p_size = basetable->gpt_last + 1;
202 	entry->part.p_offset = table->offset;
203 
204 	return (0);
205 }
206 
207 static int
208 g_part_bsd_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
209 {
210 
211 	/* Wipe the second sector to clear the partitioning. */
212 	basetable->gpt_smhead |= 2;
213 	return (0);
214 }
215 
216 static int
217 g_part_bsd_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
218 {
219 	struct g_part_bsd_entry *entry;
220 
221 	/* Allow dumping to a swap partition only. */
222 	entry = (struct g_part_bsd_entry *)baseentry;
223 	return ((entry->part.p_fstype == FS_SWAP) ? 1 : 0);
224 }
225 
226 static int
227 g_part_bsd_modify(struct g_part_table *basetable,
228     struct g_part_entry *baseentry, struct g_part_parms *gpp)
229 {
230 	struct g_part_bsd_entry *entry;
231 
232 	if (gpp->gpp_parms & G_PART_PARM_LABEL)
233 		return (EINVAL);
234 
235 	entry = (struct g_part_bsd_entry *)baseentry;
236 	if (gpp->gpp_parms & G_PART_PARM_TYPE)
237 		return (bsd_parse_type(gpp->gpp_type, &entry->part.p_fstype));
238 	return (0);
239 }
240 
241 static char *
242 g_part_bsd_name(struct g_part_table *table, struct g_part_entry *baseentry,
243     char *buf, size_t bufsz)
244 {
245 
246 	snprintf(buf, bufsz, "%c", 'a' + baseentry->gpe_index - 1);
247 	return (buf);
248 }
249 
250 static int
251 g_part_bsd_probe(struct g_part_table *table, struct g_consumer *cp)
252 {
253 	struct g_provider *pp;
254 	u_char *buf;
255 	uint32_t magic1, magic2;
256 	int error;
257 
258 	pp = cp->provider;
259 
260 	/* Sanity-check the provider. */
261 	if (pp->sectorsize < sizeof(struct disklabel) ||
262 	    pp->mediasize < BBSIZE)
263 		return (ENOSPC);
264 
265 	/* Check that there's a disklabel. */
266 	buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error);
267 	if (buf == NULL)
268 		return (error);
269 	magic1 = le32dec(buf + 0);
270 	magic2 = le32dec(buf + 132);
271 	g_free(buf);
272 	return ((magic1 == DISKMAGIC && magic2 == DISKMAGIC)
273 	    ? G_PART_PROBE_PRI_NORM : ENXIO);
274 }
275 
276 static int
277 g_part_bsd_read(struct g_part_table *basetable, struct g_consumer *cp)
278 {
279 	struct g_provider *pp;
280 	struct g_part_bsd_table *table;
281 	struct g_part_entry *baseentry;
282 	struct g_part_bsd_entry *entry;
283 	struct partition part;
284 	u_char *buf, *p;
285 	off_t chs, msize;
286 	u_int sectors, heads;
287 	int error, index;
288 
289 	pp = cp->provider;
290 	table = (struct g_part_bsd_table *)basetable;
291 	msize = pp->mediasize / pp->sectorsize;
292 
293 	buf = g_read_data(cp, pp->sectorsize, pp->sectorsize, &error);
294 	if (buf == NULL)
295 		return (error);
296 
297 	table->label = buf;
298 
299 	if (le32dec(buf + 40) != pp->sectorsize)
300 		goto invalid_label;
301 	sectors = le32dec(buf + 44);
302 	if (sectors < 1 || sectors > 63)
303 		goto invalid_label;
304 	if (sectors != basetable->gpt_sectors && !basetable->gpt_fixgeom) {
305 		g_part_geometry_heads(msize, sectors, &chs, &heads);
306 		if (chs != 0) {
307 			basetable->gpt_sectors = sectors;
308 			basetable->gpt_heads = heads;
309 		}
310 	}
311 	heads = le32dec(buf + 48);
312 	if (heads < 1 || heads > 255)
313 		goto invalid_label;
314 	if (heads != basetable->gpt_heads && !basetable->gpt_fixgeom)
315 		basetable->gpt_heads = heads;
316 	if (sectors != basetable->gpt_sectors ||
317 	    heads != basetable->gpt_heads)
318 		printf("GEOM: %s: geometry does not match label.\n", pp->name);
319 
320 	chs = le32dec(buf + 60);
321 	if (chs < 1 || chs > msize)
322 		goto invalid_label;
323 	if (chs != msize)
324 		printf("GEOM: %s: media size does not match label.\n",
325 		    pp->name);
326 
327 	basetable->gpt_first = 0;
328 	basetable->gpt_last = msize - 1;
329 	basetable->gpt_isleaf = 1;
330 
331 	basetable->gpt_entries = le16dec(buf + 138);
332 	if (basetable->gpt_entries < g_part_bsd_scheme.gps_minent ||
333 	    basetable->gpt_entries > g_part_bsd_scheme.gps_maxent)
334 		goto invalid_label;
335 
336 	table->offset = le32dec(buf + 148 + RAW_PART * 16 + 4);
337 	for (index = basetable->gpt_entries - 1; index >= 0; index--) {
338 		p = buf + 148 + index * 16;
339 		part.p_size = le32dec(p + 0);
340 		part.p_offset = le32dec(p + 4);
341 		part.p_fsize = le32dec(p + 8);
342 		part.p_fstype = p[12];
343 		part.p_frag = p[13];
344 		part.p_cpg = le16dec(p + 14);
345 		if (part.p_size == 0)
346 			continue;
347 		if (part.p_fstype == FS_UNUSED && index != RAW_PART)
348 			continue;
349 		if (part.p_offset < table->offset)
350 			continue;
351 		baseentry = g_part_new_entry(basetable, index + 1,
352 		    part.p_offset - table->offset,
353 		    part.p_offset - table->offset + part.p_size - 1);
354 		entry = (struct g_part_bsd_entry *)baseentry;
355 		entry->part = part;
356 		if (part.p_fstype == FS_UNUSED)
357 			baseentry->gpe_internal = 1;
358 	}
359 
360 	return (0);
361 
362  invalid_label:
363 	printf("GEOM: %s: invalid disklabel.\n", pp->name);
364 	g_free(table->label);
365 	return (EINVAL);
366 }
367 
368 static const char *
369 g_part_bsd_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
370     char *buf, size_t bufsz)
371 {
372 	struct g_part_bsd_entry *entry;
373 	int type;
374 
375 	entry = (struct g_part_bsd_entry *)baseentry;
376 	type = entry->part.p_fstype;
377 	if (type == FS_SWAP)
378 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP));
379 	if (type == FS_BSDFFS)
380 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS));
381 	if (type == FS_VINUM)
382 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM));
383 	if (type == FS_ZFS)
384 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS));
385 	snprintf(buf, bufsz, "!%d", type);
386 	return (buf);
387 }
388 
389 static int
390 g_part_bsd_write(struct g_part_table *basetable, struct g_consumer *cp)
391 {
392 	struct g_provider *pp;
393 	struct g_part_entry *baseentry;
394 	struct g_part_bsd_entry *entry;
395 	struct g_part_bsd_table *table;
396 	uint16_t sum;
397 	u_char *p, *pe;
398 	int error, index;
399 
400 	pp = cp->provider;
401 	table = (struct g_part_bsd_table *)basetable;
402 	baseentry = LIST_FIRST(&basetable->gpt_entry);
403 	for (index = 1; index <= basetable->gpt_entries; index++) {
404 		p = table->label + 148 + (index - 1) * 16;
405 		entry = (baseentry != NULL && index == baseentry->gpe_index)
406 		    ? (struct g_part_bsd_entry *)baseentry : NULL;
407 		if (entry != NULL && !baseentry->gpe_deleted) {
408 			le32enc(p + 0, entry->part.p_size);
409 			le32enc(p + 4, entry->part.p_offset);
410 			le32enc(p + 8, entry->part.p_fsize);
411 			p[12] = entry->part.p_fstype;
412 			p[13] = entry->part.p_frag;
413 			le16enc(p + 14, entry->part.p_cpg);
414 		} else
415 			bzero(p, 16);
416 
417 		if (entry != NULL)
418 			baseentry = LIST_NEXT(baseentry, gpe_entry);
419 	}
420 
421 	/* Calculate checksum. */
422 	le16enc(table->label + 136, 0);
423 	pe = table->label + 148 + basetable->gpt_entries * 16;
424 	sum = 0;
425 	for (p = table->label; p < pe; p += 2)
426 		sum ^= le16dec(p);
427 	le16enc(table->label + 136, sum);
428 
429 	error = g_write_data(cp, pp->sectorsize, table->label, pp->sectorsize);
430 	return (error);
431 }
432