Deleted Added
full compact
geom_sunlabel.c (125755) geom_sunlabel.c (133314)
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>
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: head/sys/geom/geom_sunlabel.c 125755 2004-02-12 22:42:11Z phk $");
37__FBSDID("$FreeBSD: head/sys/geom/geom_sunlabel.c 133314 2004-08-08 06:49:07Z phk $");
38
39#include <sys/param.h>
40#include <sys/endian.h>
41#include <sys/systm.h>
42#include <sys/kernel.h>
43#include <sys/conf.h>
44#include <sys/bio.h>
45#include <sys/malloc.h>
46#include <sys/lock.h>
47#include <sys/mutex.h>
48#include <sys/sun_disklabel.h>
49#include <geom/geom.h>
50#include <geom/geom_slice.h>
51#include <machine/endian.h>
52
53#define SUNLABEL_CLASS_NAME "SUN"
54
55struct g_sunlabel_softc {
56 int sectorsize;
57 int nheads;
58 int nsects;
59 int nalt;
60};
61
62static int
63g_sunlabel_modify(struct g_geom *gp, struct g_sunlabel_softc *ms, u_char *sec0)
64{
65 int i, error;
66 u_int u, v, csize;
67 struct sun_disklabel sl;
68
69 error = sunlabel_dec(sec0, &sl);
70 if (error)
71 return (error);
72
73 csize = sl.sl_ntracks * sl.sl_nsectors;
74
75 for (i = 0; i < SUN_NPART; i++) {
76 v = sl.sl_part[i].sdkp_cyloffset;
77 u = sl.sl_part[i].sdkp_nsectors;
78 error = g_slice_config(gp, i, G_SLICE_CONFIG_CHECK,
79 ((off_t)v * csize) << 9ULL,
80 ((off_t)u) << 9ULL,
81 ms->sectorsize,
82 "%s%c", gp->name, 'a' + i);
83 if (error)
84 return (error);
85 }
86 for (i = 0; i < SUN_NPART; i++) {
87 v = sl.sl_part[i].sdkp_cyloffset;
88 u = sl.sl_part[i].sdkp_nsectors;
89 g_slice_config(gp, i, G_SLICE_CONFIG_SET,
90 ((off_t)v * csize) << 9ULL,
91 ((off_t)u) << 9ULL,
92 ms->sectorsize,
93 "%s%c", gp->name, 'a' + i);
94 }
95 ms->nalt = sl.sl_acylinders;
96 ms->nheads = sl.sl_ntracks;
97 ms->nsects = sl.sl_nsectors;
98
99 return (0);
100}
101
102static void
103g_sunlabel_hotwrite(void *arg, int flag)
104{
105 struct bio *bp;
106 struct g_geom *gp;
107 struct g_slicer *gsp;
108 struct g_slice *gsl;
109 struct g_sunlabel_softc *ms;
110 u_char *p;
111 int error;
112
113 KASSERT(flag != EV_CANCEL, ("g_sunlabel_hotwrite cancelled"));
114 bp = arg;
115 gp = bp->bio_to->geom;
116 gsp = gp->softc;
117 ms = gsp->softc;
118 gsl = &gsp->slices[bp->bio_to->index];
119 /*
120 * XXX: For all practical purposes, this whould be equvivalent to
121 * XXX: "p = (u_char *)bp->bio_data;" because the label is always
122 * XXX: in the first sector and we refuse sectors smaller than the
123 * XXX: label.
124 */
125 p = (u_char *)bp->bio_data - (bp->bio_offset + gsl->offset);
126
127 error = g_sunlabel_modify(gp, ms, p);
128 if (error) {
129 g_io_deliver(bp, EPERM);
130 return;
131 }
132 g_slice_finish_hot(bp);
133}
134
135static void
136g_sunlabel_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
137{
138 struct g_slicer *gsp;
139 struct g_sunlabel_softc *ms;
140
141 gsp = gp->softc;
142 ms = gsp->softc;
143 g_slice_dumpconf(sb, indent, gp, cp, pp);
144 if (indent == NULL) {
145 sbuf_printf(sb, " sc %u hd %u alt %u",
146 ms->nsects, ms->nheads, ms->nalt);
147 }
148}
149
150struct g_hh01 {
151 struct g_geom *gp;
152 struct g_sunlabel_softc *ms;
153 u_char *label;
154 int error;
155};
156
157static void
158g_sunlabel_callconfig(void *arg, int flag)
159{
160 struct g_hh01 *hp;
161
162 hp = arg;
163 hp->error = g_sunlabel_modify(hp->gp, hp->ms, hp->label);
164 if (!hp->error)
165 hp->error = g_write_data(LIST_FIRST(&hp->gp->consumer),
166 0, hp->label, SUN_SIZE);
167}
168
169/*
170 * NB! curthread is user process which GCTL'ed.
171 */
172static void
173g_sunlabel_config(struct gctl_req *req, struct g_class *mp, const char *verb)
174{
175 u_char *label;
176 int error, i;
177 struct g_hh01 h0h0;
178 struct g_slicer *gsp;
179 struct g_geom *gp;
180 struct g_consumer *cp;
181
182 g_topology_assert();
183 gp = gctl_get_geom(req, mp, "geom");
184 if (gp == NULL)
185 return;
186 cp = LIST_FIRST(&gp->consumer);
187 gsp = gp->softc;
188 if (!strcmp(verb, "write label")) {
189 label = gctl_get_paraml(req, "label", SUN_SIZE);
190 if (label == NULL)
191 return;
192 h0h0.gp = gp;
193 h0h0.ms = gsp->softc;
194 h0h0.label = label;
195 h0h0.error = -1;
196 /* XXX: Does this reference register with our selfdestruct code ? */
197 error = g_access(cp, 1, 1, 1);
198 if (error) {
199 gctl_error(req, "could not access consumer");
200 return;
201 }
202 g_sunlabel_callconfig(&h0h0, 0);
203 g_access(cp, -1, -1, -1);
204 } else if (!strcmp(verb, "write bootcode")) {
205 label = gctl_get_paraml(req, "bootcode", SUN_BOOTSIZE);
206 if (label == NULL)
207 return;
208 /* XXX: Does this reference register with our selfdestruct code ? */
209 error = g_access(cp, 1, 1, 1);
210 if (error) {
211 gctl_error(req, "could not access consumer");
212 return;
213 }
214 for (i = 0; i < SUN_NPART; i++) {
215 if (gsp->slices[i].length <= SUN_BOOTSIZE)
216 continue;
217 g_write_data(cp,
218 gsp->slices[i].offset + SUN_SIZE, label + SUN_SIZE,
219 SUN_BOOTSIZE - SUN_SIZE);
220 }
221 g_access(cp, -1, -1, -1);
222 } else {
223 gctl_error(req, "Unknown verb parameter");
224 }
225}
226
227static struct g_geom *
228g_sunlabel_taste(struct g_class *mp, struct g_provider *pp, int flags)
229{
230 struct g_geom *gp;
231 struct g_consumer *cp;
232 int error;
233 u_char *buf;
234 struct g_sunlabel_softc *ms;
235 struct g_slicer *gsp;
236
237 g_trace(G_T_TOPOLOGY, "g_sunlabel_taste(%s,%s)", mp->name, pp->name);
238 g_topology_assert();
239 if (flags == G_TF_NORMAL &&
240 !strcmp(pp->geom->class->name, SUNLABEL_CLASS_NAME))
241 return (NULL);
242 gp = g_slice_new(mp, 8, pp, &cp, &ms, sizeof *ms, NULL);
243 if (gp == NULL)
244 return (NULL);
245 gsp = gp->softc;
38
39#include <sys/param.h>
40#include <sys/endian.h>
41#include <sys/systm.h>
42#include <sys/kernel.h>
43#include <sys/conf.h>
44#include <sys/bio.h>
45#include <sys/malloc.h>
46#include <sys/lock.h>
47#include <sys/mutex.h>
48#include <sys/sun_disklabel.h>
49#include <geom/geom.h>
50#include <geom/geom_slice.h>
51#include <machine/endian.h>
52
53#define SUNLABEL_CLASS_NAME "SUN"
54
55struct g_sunlabel_softc {
56 int sectorsize;
57 int nheads;
58 int nsects;
59 int nalt;
60};
61
62static int
63g_sunlabel_modify(struct g_geom *gp, struct g_sunlabel_softc *ms, u_char *sec0)
64{
65 int i, error;
66 u_int u, v, csize;
67 struct sun_disklabel sl;
68
69 error = sunlabel_dec(sec0, &sl);
70 if (error)
71 return (error);
72
73 csize = sl.sl_ntracks * sl.sl_nsectors;
74
75 for (i = 0; i < SUN_NPART; i++) {
76 v = sl.sl_part[i].sdkp_cyloffset;
77 u = sl.sl_part[i].sdkp_nsectors;
78 error = g_slice_config(gp, i, G_SLICE_CONFIG_CHECK,
79 ((off_t)v * csize) << 9ULL,
80 ((off_t)u) << 9ULL,
81 ms->sectorsize,
82 "%s%c", gp->name, 'a' + i);
83 if (error)
84 return (error);
85 }
86 for (i = 0; i < SUN_NPART; i++) {
87 v = sl.sl_part[i].sdkp_cyloffset;
88 u = sl.sl_part[i].sdkp_nsectors;
89 g_slice_config(gp, i, G_SLICE_CONFIG_SET,
90 ((off_t)v * csize) << 9ULL,
91 ((off_t)u) << 9ULL,
92 ms->sectorsize,
93 "%s%c", gp->name, 'a' + i);
94 }
95 ms->nalt = sl.sl_acylinders;
96 ms->nheads = sl.sl_ntracks;
97 ms->nsects = sl.sl_nsectors;
98
99 return (0);
100}
101
102static void
103g_sunlabel_hotwrite(void *arg, int flag)
104{
105 struct bio *bp;
106 struct g_geom *gp;
107 struct g_slicer *gsp;
108 struct g_slice *gsl;
109 struct g_sunlabel_softc *ms;
110 u_char *p;
111 int error;
112
113 KASSERT(flag != EV_CANCEL, ("g_sunlabel_hotwrite cancelled"));
114 bp = arg;
115 gp = bp->bio_to->geom;
116 gsp = gp->softc;
117 ms = gsp->softc;
118 gsl = &gsp->slices[bp->bio_to->index];
119 /*
120 * XXX: For all practical purposes, this whould be equvivalent to
121 * XXX: "p = (u_char *)bp->bio_data;" because the label is always
122 * XXX: in the first sector and we refuse sectors smaller than the
123 * XXX: label.
124 */
125 p = (u_char *)bp->bio_data - (bp->bio_offset + gsl->offset);
126
127 error = g_sunlabel_modify(gp, ms, p);
128 if (error) {
129 g_io_deliver(bp, EPERM);
130 return;
131 }
132 g_slice_finish_hot(bp);
133}
134
135static void
136g_sunlabel_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp)
137{
138 struct g_slicer *gsp;
139 struct g_sunlabel_softc *ms;
140
141 gsp = gp->softc;
142 ms = gsp->softc;
143 g_slice_dumpconf(sb, indent, gp, cp, pp);
144 if (indent == NULL) {
145 sbuf_printf(sb, " sc %u hd %u alt %u",
146 ms->nsects, ms->nheads, ms->nalt);
147 }
148}
149
150struct g_hh01 {
151 struct g_geom *gp;
152 struct g_sunlabel_softc *ms;
153 u_char *label;
154 int error;
155};
156
157static void
158g_sunlabel_callconfig(void *arg, int flag)
159{
160 struct g_hh01 *hp;
161
162 hp = arg;
163 hp->error = g_sunlabel_modify(hp->gp, hp->ms, hp->label);
164 if (!hp->error)
165 hp->error = g_write_data(LIST_FIRST(&hp->gp->consumer),
166 0, hp->label, SUN_SIZE);
167}
168
169/*
170 * NB! curthread is user process which GCTL'ed.
171 */
172static void
173g_sunlabel_config(struct gctl_req *req, struct g_class *mp, const char *verb)
174{
175 u_char *label;
176 int error, i;
177 struct g_hh01 h0h0;
178 struct g_slicer *gsp;
179 struct g_geom *gp;
180 struct g_consumer *cp;
181
182 g_topology_assert();
183 gp = gctl_get_geom(req, mp, "geom");
184 if (gp == NULL)
185 return;
186 cp = LIST_FIRST(&gp->consumer);
187 gsp = gp->softc;
188 if (!strcmp(verb, "write label")) {
189 label = gctl_get_paraml(req, "label", SUN_SIZE);
190 if (label == NULL)
191 return;
192 h0h0.gp = gp;
193 h0h0.ms = gsp->softc;
194 h0h0.label = label;
195 h0h0.error = -1;
196 /* XXX: Does this reference register with our selfdestruct code ? */
197 error = g_access(cp, 1, 1, 1);
198 if (error) {
199 gctl_error(req, "could not access consumer");
200 return;
201 }
202 g_sunlabel_callconfig(&h0h0, 0);
203 g_access(cp, -1, -1, -1);
204 } else if (!strcmp(verb, "write bootcode")) {
205 label = gctl_get_paraml(req, "bootcode", SUN_BOOTSIZE);
206 if (label == NULL)
207 return;
208 /* XXX: Does this reference register with our selfdestruct code ? */
209 error = g_access(cp, 1, 1, 1);
210 if (error) {
211 gctl_error(req, "could not access consumer");
212 return;
213 }
214 for (i = 0; i < SUN_NPART; i++) {
215 if (gsp->slices[i].length <= SUN_BOOTSIZE)
216 continue;
217 g_write_data(cp,
218 gsp->slices[i].offset + SUN_SIZE, label + SUN_SIZE,
219 SUN_BOOTSIZE - SUN_SIZE);
220 }
221 g_access(cp, -1, -1, -1);
222 } else {
223 gctl_error(req, "Unknown verb parameter");
224 }
225}
226
227static struct g_geom *
228g_sunlabel_taste(struct g_class *mp, struct g_provider *pp, int flags)
229{
230 struct g_geom *gp;
231 struct g_consumer *cp;
232 int error;
233 u_char *buf;
234 struct g_sunlabel_softc *ms;
235 struct g_slicer *gsp;
236
237 g_trace(G_T_TOPOLOGY, "g_sunlabel_taste(%s,%s)", mp->name, pp->name);
238 g_topology_assert();
239 if (flags == G_TF_NORMAL &&
240 !strcmp(pp->geom->class->name, SUNLABEL_CLASS_NAME))
241 return (NULL);
242 gp = g_slice_new(mp, 8, pp, &cp, &ms, sizeof *ms, NULL);
243 if (gp == NULL)
244 return (NULL);
245 gsp = gp->softc;
246 gp->dumpconf = g_sunlabel_dumpconf;
247 do {
248 if (gp->rank != 2 && flags == G_TF_NORMAL)
249 break;
250 ms->sectorsize = cp->provider->sectorsize;
251 if (ms->sectorsize < 512)
252 break;
253 g_topology_unlock();
254 buf = g_read_data(cp, 0, ms->sectorsize, &error);
255 g_topology_lock();
256 if (buf == NULL || error != 0)
257 break;
258
259 g_sunlabel_modify(gp, ms, buf);
260 g_free(buf);
261
262 break;
263 } while (0);
264 g_access(cp, -1, 0, 0);
265 if (LIST_EMPTY(&gp->provider)) {
266 g_slice_spoiled(cp);
267 return (NULL);
268 }
269 g_slice_conf_hot(gp, 0, 0, SUN_SIZE,
270 G_SLICE_HOT_ALLOW, G_SLICE_HOT_DENY, G_SLICE_HOT_CALL);
271 gsp->hot = g_sunlabel_hotwrite;
272 return (gp);
273}
274
275static struct g_class g_sunlabel_class = {
276 .name = SUNLABEL_CLASS_NAME,
277 .taste = g_sunlabel_taste,
278 .ctlreq = g_sunlabel_config,
246 do {
247 if (gp->rank != 2 && flags == G_TF_NORMAL)
248 break;
249 ms->sectorsize = cp->provider->sectorsize;
250 if (ms->sectorsize < 512)
251 break;
252 g_topology_unlock();
253 buf = g_read_data(cp, 0, ms->sectorsize, &error);
254 g_topology_lock();
255 if (buf == NULL || error != 0)
256 break;
257
258 g_sunlabel_modify(gp, ms, buf);
259 g_free(buf);
260
261 break;
262 } while (0);
263 g_access(cp, -1, 0, 0);
264 if (LIST_EMPTY(&gp->provider)) {
265 g_slice_spoiled(cp);
266 return (NULL);
267 }
268 g_slice_conf_hot(gp, 0, 0, SUN_SIZE,
269 G_SLICE_HOT_ALLOW, G_SLICE_HOT_DENY, G_SLICE_HOT_CALL);
270 gsp->hot = g_sunlabel_hotwrite;
271 return (gp);
272}
273
274static struct g_class g_sunlabel_class = {
275 .name = SUNLABEL_CLASS_NAME,
276 .taste = g_sunlabel_taste,
277 .ctlreq = g_sunlabel_config,
278 .dumpconf = g_sunlabel_dumpconf,
279};
280
281DECLARE_GEOM_CLASS(g_sunlabel_class, g_sunlabel);
279};
280
281DECLARE_GEOM_CLASS(g_sunlabel_class, g_sunlabel);