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