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/11.2/sys/geom/geom_sunlabel.c 332640 2018-04-17 02:18:04Z kevans $");
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
56 FEATURE(geom_sunlabel, "GEOM Sun/Solaris partitioning support");
57
58 #define SUNLABEL_CLASS_NAME "SUN"
59
60 struct g_sunlabel_softc {
61 int sectorsize;
62 int nheads;
63 int nsects;
64 int nalt;
65 u_char labelsum[16];
66 };
67
68 static int g_sunlabel_once = 0;
69
70 static int
71 g_sunlabel_modify(struct g_geom *gp, struct g_sunlabel_softc *ms, u_char *sec0)
72 {
73 int i, error;
74 u_int u, v, csize;
75 struct sun_disklabel sl;
76 MD5_CTX md5sum;
77
78 error = sunlabel_dec(sec0, &sl);
79 if (error)
80 return (error);
81
82 csize = sl.sl_ntracks * sl.sl_nsectors;
83
84 for (i = 0; i < SUN_NPART; i++) {
85 v = sl.sl_part[i].sdkp_cyloffset;
86 u = sl.sl_part[i].sdkp_nsectors;
87 error = g_slice_config(gp, i, G_SLICE_CONFIG_CHECK,
88 ((off_t)v * csize) << 9ULL,
89 ((off_t)u) << 9ULL,
90 ms->sectorsize,
91 "%s%c", gp->name, 'a' + i);
92 if (error)
93 return (error);
94 }
95 for (i = 0; i < SUN_NPART; i++) {
96 v = sl.sl_part[i].sdkp_cyloffset;
97 u = sl.sl_part[i].sdkp_nsectors;
98 g_slice_config(gp, i, G_SLICE_CONFIG_SET,
99 ((off_t)v * csize) << 9ULL,
100 ((off_t)u) << 9ULL,
101 ms->sectorsize,
102 "%s%c", gp->name, 'a' + i);
103 }
104 ms->nalt = sl.sl_acylinders;
105 ms->nheads = sl.sl_ntracks;
106 ms->nsects = sl.sl_nsectors;
107
108 /*
109 * Calculate MD5 from the first sector and use it for avoiding
110 * recursive labels creation.
111 */
112 MD5Init(&md5sum);
113 MD5Update(&md5sum, sec0, ms->sectorsize);
114 MD5Final(ms->labelsum, &md5sum);
115
116 return (0);
117 }
118
119 static void
120 g_sunlabel_hotwrite(void *arg, int flag)
121 {
122 struct bio *bp;
123 struct g_geom *gp;
124 struct g_slicer *gsp;
125 struct g_slice *gsl;
126 struct g_sunlabel_softc *ms;
127 u_char *p;
128 int error;
129
130 KASSERT(flag != EV_CANCEL, ("g_sunlabel_hotwrite cancelled"));
131 bp = arg;
132 gp = bp->bio_to->geom;
133 gsp = gp->softc;
134 ms = gsp->softc;
135 gsl = &gsp->slices[bp->bio_to->index];
136 /*
137 * XXX: For all practical purposes, this whould be equvivalent to
138 * XXX: "p = (u_char *)bp->bio_data;" because the label is always
139 * XXX: in the first sector and we refuse sectors smaller than the
140 * XXX: label.
141 */
142 p = (u_char *)bp->bio_data - (bp->bio_offset + gsl->offset);
143
144 error = g_sunlabel_modify(gp, ms, p);
145 if (error) {
146 g_io_deliver(bp, EPERM);
147 return;
148 }
149 g_slice_finish_hot(bp);
150 }
151
152 static void
153 g_sunlabel_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
154 {
155 struct g_slicer *gsp;
156 struct g_sunlabel_softc *ms;
157
158 gsp = gp->softc;
159 ms = gsp->softc;
160 g_slice_dumpconf(sb, indent, gp, cp, pp);
161 if (indent == NULL) {
162 sbuf_printf(sb, " sc %u hd %u alt %u",
163 ms->nsects, ms->nheads, ms->nalt);
164 }
165 }
166
167 struct g_hh01 {
168 struct g_geom *gp;
169 struct g_sunlabel_softc *ms;
170 u_char *label;
171 int error;
172 };
173
174 static void
175 g_sunlabel_callconfig(void *arg, int flag)
176 {
177 struct g_hh01 *hp;
178
179 hp = arg;
180 hp->error = g_sunlabel_modify(hp->gp, hp->ms, hp->label);
181 if (!hp->error)
182 hp->error = g_write_data(LIST_FIRST(&hp->gp->consumer),
183 0, hp->label, SUN_SIZE);
184 }
185
186 /*
187 * NB! curthread is user process which GCTL'ed.
188 */
189 static void
190 g_sunlabel_config(struct gctl_req *req, struct g_class *mp, const char *verb)
191 {
192 u_char *label;
193 int error, i;
194 struct g_hh01 h0h0;
195 struct g_slicer *gsp;
196 struct g_geom *gp;
197 struct g_consumer *cp;
198
199 g_topology_assert();
200 gp = gctl_get_geom(req, mp, "geom");
201 if (gp == NULL)
202 return;
203 cp = LIST_FIRST(&gp->consumer);
204 gsp = gp->softc;
205 if (!strcmp(verb, "write label")) {
206 label = gctl_get_paraml(req, "label", SUN_SIZE);
207 if (label == NULL)
208 return;
209 h0h0.gp = gp;
210 h0h0.ms = gsp->softc;
211 h0h0.label = label;
212 h0h0.error = -1;
213 /* XXX: Does this reference register with our selfdestruct code ? */
214 error = g_access(cp, 1, 1, 1);
215 if (error) {
216 gctl_error(req, "could not access consumer");
217 return;
218 }
219 g_sunlabel_callconfig(&h0h0, 0);
220 g_access(cp, -1, -1, -1);
221 } else if (!strcmp(verb, "write bootcode")) {
222 label = gctl_get_paraml(req, "bootcode", SUN_BOOTSIZE);
223 if (label == NULL)
224 return;
225 /* XXX: Does this reference register with our selfdestruct code ? */
226 error = g_access(cp, 1, 1, 1);
227 if (error) {
228 gctl_error(req, "could not access consumer");
229 return;
230 }
231 for (i = 0; i < SUN_NPART; i++) {
232 if (gsp->slices[i].length <= SUN_BOOTSIZE)
233 continue;
234 g_write_data(cp,
235 gsp->slices[i].offset + SUN_SIZE, label + SUN_SIZE,
236 SUN_BOOTSIZE - SUN_SIZE);
237 }
238 g_access(cp, -1, -1, -1);
239 } else {
240 gctl_error(req, "Unknown verb parameter");
241 }
242 }
243
244 static int
245 g_sunlabel_start(struct bio *bp)
246 {
247 struct g_sunlabel_softc *mp;
248 struct g_slicer *gsp;
249
250 gsp = bp->bio_to->geom->softc;
251 mp = gsp->softc;
252 if (bp->bio_cmd == BIO_GETATTR) {
253 if (g_handleattr(bp, "SUN::labelsum", mp->labelsum,
254 sizeof(mp->labelsum)))
255 return (1);
256 }
257 return (0);
258 }
259
260 static struct g_geom *
261 g_sunlabel_taste(struct g_class *mp, struct g_provider *pp, int flags)
262 {
263 struct g_geom *gp;
264 struct g_consumer *cp;
265 struct g_sunlabel_softc *ms;
266 struct g_slicer *gsp;
267 u_char *buf, hash[16];
268 MD5_CTX md5sum;
269 int error;
270
271 g_trace(G_T_TOPOLOGY, "g_sunlabel_taste(%s,%s)", mp->name, pp->name);
272 g_topology_assert();
273 if (flags == G_TF_NORMAL &&
274 !strcmp(pp->geom->class->name, SUNLABEL_CLASS_NAME))
275 return (NULL);
276 gp = g_slice_new(mp, 8, pp, &cp, &ms, sizeof *ms, g_sunlabel_start);
277 if (gp == NULL)
278 return (NULL);
279 gsp = gp->softc;
280 do {
281 ms->sectorsize = cp->provider->sectorsize;
282 if (ms->sectorsize < 512)
283 break;
284 g_topology_unlock();
285 buf = g_read_data(cp, 0, ms->sectorsize, NULL);
286 g_topology_lock();
287 if (buf == NULL)
288 break;
289
290 /*
291 * Calculate MD5 from the first sector and use it for avoiding
292 * recursive labels creation.
293 */
294 MD5Init(&md5sum);
295 MD5Update(&md5sum, buf, ms->sectorsize);
296 MD5Final(ms->labelsum, &md5sum);
297
298 error = g_getattr("SUN::labelsum", cp, &hash);
299 if (!error && !bcmp(ms->labelsum, hash, sizeof(hash))) {
300 g_free(buf);
301 break;
302 }
303
304 g_sunlabel_modify(gp, ms, buf);
305 g_free(buf);
306
307 break;
308 } while (0);
309 g_access(cp, -1, 0, 0);
310 if (LIST_EMPTY(&gp->provider)) {
311 g_slice_spoiled(cp);
312 return (NULL);
313 }
314 g_slice_conf_hot(gp, 0, 0, SUN_SIZE,
315 G_SLICE_HOT_ALLOW, G_SLICE_HOT_DENY, G_SLICE_HOT_CALL);
316 gsp->hot = g_sunlabel_hotwrite;
317 if (!g_sunlabel_once) {
318 g_sunlabel_once = 1;
319 printf(
320 "WARNING: geom_sunlabel (geom %s) is deprecated, "
321 "use gpart instead.\n", gp->name);
322 }
323 return (gp);
324 }
325
326 static struct g_class g_sunlabel_class = {
327 .name = SUNLABEL_CLASS_NAME,
328 .version = G_VERSION,
329 .taste = g_sunlabel_taste,
330 .ctlreq = g_sunlabel_config,
331 .dumpconf = g_sunlabel_dumpconf,
332 };
333
334 DECLARE_GEOM_CLASS(g_sunlabel_class, g_sunlabel);
335 MODULE_VERSION(geom_sunlabel, 0);
Cache object: f3174f639fb33ef46130de3a8a574ed0
|