1/*-
2 * Copyright (c) 2002 Poul-Henning Kamp
3 * Copyright (c) 2002 Networks Associates Technology, Inc.
4 * All rights reserved.
5 *
6 * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7 * and NAI Labs, the Security Research Division of Network Associates, Inc.
8 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9 * DARPA CHATS research program.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. The names of the authors may not be used to endorse or promote
20 *    products derived from this software without specific prior written
21 *    permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include <sys/cdefs.h>
37__FBSDID("$FreeBSD: releng/10.3/sys/geom/geom_sunlabel.c 223921 2011-07-11 05:22:31Z ae $");
38
39#include <sys/param.h>
40#include <sys/endian.h>
41#include <sys/systm.h>
42#include <sys/sysctl.h>
43#include <sys/kernel.h>
44#include <sys/conf.h>
45#include <sys/bio.h>
46#include <sys/malloc.h>
47#include <sys/lock.h>
48#include <sys/mutex.h>
49#include <sys/md5.h>
50#include <sys/sbuf.h>
51#include <sys/sun_disklabel.h>
52#include <geom/geom.h>
53#include <geom/geom_slice.h>
54#include <machine/endian.h>
55
56FEATURE(geom_sunlabel, "GEOM Sun/Solaris partitioning support");
57
58#define SUNLABEL_CLASS_NAME "SUN"
59
60struct g_sunlabel_softc {
61	int sectorsize;
62	int nheads;
63	int nsects;
64	int nalt;
65	u_char labelsum[16];
66};
67
68static int
69g_sunlabel_modify(struct g_geom *gp, struct g_sunlabel_softc *ms, u_char *sec0)
70{
71	int i, error;
72	u_int u, v, csize;
73	struct sun_disklabel sl;
74	MD5_CTX md5sum;
75
76	error = sunlabel_dec(sec0, &sl);
77	if (error)
78		return (error);
79
80	csize = sl.sl_ntracks * sl.sl_nsectors;
81
82	for (i = 0; i < SUN_NPART; i++) {
83		v = sl.sl_part[i].sdkp_cyloffset;
84		u = sl.sl_part[i].sdkp_nsectors;
85		error = g_slice_config(gp, i, G_SLICE_CONFIG_CHECK,
86		    ((off_t)v * csize) << 9ULL,
87		    ((off_t)u) << 9ULL,
88		    ms->sectorsize,
89		    "%s%c", gp->name, 'a' + i);
90		if (error)
91			return (error);
92	}
93	for (i = 0; i < SUN_NPART; i++) {
94		v = sl.sl_part[i].sdkp_cyloffset;
95		u = sl.sl_part[i].sdkp_nsectors;
96		g_slice_config(gp, i, G_SLICE_CONFIG_SET,
97		    ((off_t)v * csize) << 9ULL,
98		    ((off_t)u) << 9ULL,
99		    ms->sectorsize,
100		    "%s%c", gp->name, 'a' + i);
101	}
102	ms->nalt = sl.sl_acylinders;
103	ms->nheads = sl.sl_ntracks;
104	ms->nsects = sl.sl_nsectors;
105
106	/*
107	 * Calculate MD5 from the first sector and use it for avoiding
108	 * recursive labels creation.
109	 */
110	MD5Init(&md5sum);
111	MD5Update(&md5sum, sec0, ms->sectorsize);
112	MD5Final(ms->labelsum, &md5sum);
113
114	return (0);
115}
116
117static void
118g_sunlabel_hotwrite(void *arg, int flag)
119{
120	struct bio *bp;
121	struct g_geom *gp;
122	struct g_slicer *gsp;
123	struct g_slice *gsl;
124	struct g_sunlabel_softc *ms;
125	u_char *p;
126	int error;
127
128	KASSERT(flag != EV_CANCEL, ("g_sunlabel_hotwrite cancelled"));
129	bp = arg;
130	gp = bp->bio_to->geom;
131	gsp = gp->softc;
132	ms = gsp->softc;
133	gsl = &gsp->slices[bp->bio_to->index];
134	/*
135	 * XXX: For all practical purposes, this whould be equvivalent to
136	 * XXX: "p = (u_char *)bp->bio_data;" because the label is always
137	 * XXX: in the first sector and we refuse sectors smaller than the
138	 * XXX: label.
139	 */
140	p = (u_char *)bp->bio_data - (bp->bio_offset + gsl->offset);
141
142	error = g_sunlabel_modify(gp, ms, p);
143	if (error) {
144		g_io_deliver(bp, EPERM);
145		return;
146	}
147	g_slice_finish_hot(bp);
148}
149
150static void
151g_sunlabel_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
152{
153	struct g_slicer *gsp;
154	struct g_sunlabel_softc *ms;
155
156	gsp = gp->softc;
157	ms = gsp->softc;
158	g_slice_dumpconf(sb, indent, gp, cp, pp);
159	if (indent == NULL) {
160		sbuf_printf(sb, " sc %u hd %u alt %u",
161		    ms->nsects, ms->nheads, ms->nalt);
162	}
163}
164
165struct g_hh01 {
166	struct g_geom *gp;
167	struct g_sunlabel_softc *ms;
168	u_char *label;
169	int error;
170};
171
172static void
173g_sunlabel_callconfig(void *arg, int flag)
174{
175	struct g_hh01 *hp;
176
177	hp = arg;
178	hp->error = g_sunlabel_modify(hp->gp, hp->ms, hp->label);
179	if (!hp->error)
180		hp->error = g_write_data(LIST_FIRST(&hp->gp->consumer),
181		    0, hp->label, SUN_SIZE);
182}
183
184/*
185 * NB! curthread is user process which GCTL'ed.
186 */
187static void
188g_sunlabel_config(struct gctl_req *req, struct g_class *mp, const char *verb)
189{
190	u_char *label;
191	int error, i;
192	struct g_hh01 h0h0;
193	struct g_slicer *gsp;
194	struct g_geom *gp;
195	struct g_consumer *cp;
196
197	g_topology_assert();
198	gp = gctl_get_geom(req, mp, "geom");
199	if (gp == NULL)
200		return;
201	cp = LIST_FIRST(&gp->consumer);
202	gsp = gp->softc;
203	if (!strcmp(verb, "write label")) {
204		label = gctl_get_paraml(req, "label", SUN_SIZE);
205		if (label == NULL)
206			return;
207		h0h0.gp = gp;
208		h0h0.ms = gsp->softc;
209		h0h0.label = label;
210		h0h0.error = -1;
211		/* XXX: Does this reference register with our selfdestruct code ? */
212		error = g_access(cp, 1, 1, 1);
213		if (error) {
214			gctl_error(req, "could not access consumer");
215			return;
216		}
217		g_sunlabel_callconfig(&h0h0, 0);
218		g_access(cp, -1, -1, -1);
219	} else if (!strcmp(verb, "write bootcode")) {
220		label = gctl_get_paraml(req, "bootcode", SUN_BOOTSIZE);
221		if (label == NULL)
222			return;
223		/* XXX: Does this reference register with our selfdestruct code ? */
224		error = g_access(cp, 1, 1, 1);
225		if (error) {
226			gctl_error(req, "could not access consumer");
227			return;
228		}
229		for (i = 0; i < SUN_NPART; i++) {
230			if (gsp->slices[i].length <= SUN_BOOTSIZE)
231				continue;
232			g_write_data(cp,
233			    gsp->slices[i].offset + SUN_SIZE, label + SUN_SIZE,
234			    SUN_BOOTSIZE - SUN_SIZE);
235		}
236		g_access(cp, -1, -1, -1);
237	} else {
238		gctl_error(req, "Unknown verb parameter");
239	}
240}
241
242static int
243g_sunlabel_start(struct bio *bp)
244{
245	struct g_sunlabel_softc *mp;
246	struct g_slicer *gsp;
247
248	gsp = bp->bio_to->geom->softc;
249	mp = gsp->softc;
250	if (bp->bio_cmd == BIO_GETATTR) {
251		if (g_handleattr(bp, "SUN::labelsum", mp->labelsum,
252		    sizeof(mp->labelsum)))
253			return (1);
254	}
255	return (0);
256}
257
258static struct g_geom *
259g_sunlabel_taste(struct g_class *mp, struct g_provider *pp, int flags)
260{
261	struct g_geom *gp;
262	struct g_consumer *cp;
263	struct g_sunlabel_softc *ms;
264	struct g_slicer *gsp;
265	u_char *buf, hash[16];
266	MD5_CTX md5sum;
267	int error;
268
269	g_trace(G_T_TOPOLOGY, "g_sunlabel_taste(%s,%s)", mp->name, pp->name);
270	g_topology_assert();
271	if (flags == G_TF_NORMAL &&
272	    !strcmp(pp->geom->class->name, SUNLABEL_CLASS_NAME))
273		return (NULL);
274	gp = g_slice_new(mp, 8, pp, &cp, &ms, sizeof *ms, g_sunlabel_start);
275	if (gp == NULL)
276		return (NULL);
277	gsp = gp->softc;
278	do {
279		ms->sectorsize = cp->provider->sectorsize;
280		if (ms->sectorsize < 512)
281			break;
282		g_topology_unlock();
283		buf = g_read_data(cp, 0, ms->sectorsize, NULL);
284		g_topology_lock();
285		if (buf == NULL)
286			break;
287
288		/*
289		 * Calculate MD5 from the first sector and use it for avoiding
290		 * recursive labels creation.
291		 */
292		MD5Init(&md5sum);
293		MD5Update(&md5sum, buf, ms->sectorsize);
294		MD5Final(ms->labelsum, &md5sum);
295
296		error = g_getattr("SUN::labelsum", cp, &hash);
297		if (!error && !bcmp(ms->labelsum, hash, sizeof(hash))) {
298			g_free(buf);
299			break;
300		}
301
302		g_sunlabel_modify(gp, ms, buf);
303		g_free(buf);
304
305		break;
306	} while (0);
307	g_access(cp, -1, 0, 0);
308	if (LIST_EMPTY(&gp->provider)) {
309		g_slice_spoiled(cp);
310		return (NULL);
311	}
312	g_slice_conf_hot(gp, 0, 0, SUN_SIZE,
313	    G_SLICE_HOT_ALLOW, G_SLICE_HOT_DENY, G_SLICE_HOT_CALL);
314	gsp->hot = g_sunlabel_hotwrite;
315	return (gp);
316}
317
318static struct g_class g_sunlabel_class = {
319	.name = SUNLABEL_CLASS_NAME,
320	.version = G_VERSION,
321	.taste = g_sunlabel_taste,
322	.ctlreq = g_sunlabel_config,
323	.dumpconf = g_sunlabel_dumpconf,
324};
325
326DECLARE_GEOM_CLASS(g_sunlabel_class, g_sunlabel);
327