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 * $FreeBSD: head/sys/geom/geom_disk.c 104519 2002-10-05 16:35:33Z phk $
36 */
37
38#include "opt_geom.h"
39#ifndef NO_GEOM
40
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/kernel.h>
44#include <sys/sysctl.h>
45#include <sys/bio.h>
46#include <sys/conf.h>
47#include <sys/disk.h>
48#include <sys/diskslice.h>
49#include <sys/disklabel.h>
50#include <sys/malloc.h>
51#include <sys/sysctl.h>
52#include <sys/stdint.h>
53#include <machine/md_var.h>
54
55
56#include <sys/lock.h>
57#include <sys/mutex.h>
58#include <geom/geom.h>
59
60static g_access_t g_disk_access;
61
62struct g_class g_disk_class = {
63 "DISK",
64 NULL,
65 NULL,
66 G_CLASS_INITIALIZER
67};
68
69static int
70g_disk_access(struct g_provider *pp, int r, int w, int e)
71{
72 struct disk *dp;
73 dev_t dev;
74 int error;
75
76 g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)",
77 pp->name, r, w, e);
78 g_topology_assert();
79 r += pp->acr;
80 w += pp->acw;
81 e += pp->ace;
82 dp = pp->geom->softc;
83 dev = dp->d_dev;
84 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
85 mtx_lock(&Giant);
86 error = devsw(dev)->d_open(dev, 3, 0, NULL);
87 if (error != 0)
88 printf("Opened disk %s -> %d\n", pp->name, error);
89 mtx_unlock(&Giant);
90 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
91 mtx_lock(&Giant);
92 error = devsw(dev)->d_close(dev, 3, 0, NULL);
93 if (error != 0)
94 printf("Closed disk %s -> %d\n", pp->name, error);
95 mtx_unlock(&Giant);
96 } else {
97 error = 0;
98 }
99 pp->mediasize = dp->d_mediasize;
100 return (error);
101}
102
103static void
104g_disk_kerneldump(struct bio *bp, struct disk *dp)
105{
106 int error;
107 struct g_kerneldump *gkd;
108 struct dumperinfo di;
109 struct g_geom *gp;
110
111 gkd = (struct g_kerneldump*)bp->bio_data;
112 gp = bp->bio_to->geom;
113 g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)",
114 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length);
115 di.dumper = (dumper_t *)dp->d_devsw->d_dump;
116 di.priv = dp->d_dev;
117 di.blocksize = dp->d_sectorsize;
118 di.mediaoffset = gkd->offset;
119 di.mediasize = gkd->length;
120 error = set_dumper(&di);
121 g_io_deliver(bp, error);
122}
123
124static void
125g_disk_done(struct bio *bp)
126{
127
128 mtx_unlock(&Giant);
129 bp->bio_completed = bp->bio_length - bp->bio_resid;
130 g_std_done(bp);
131 mtx_lock(&Giant);
132}
133
134static void
135g_disk_ioctl(void *arg)
136{
137 struct bio *bp;
138 dev_t dev;
139 struct disk *dp;
140 struct g_ioctl *gio;
141 int error;
142
143 bp = arg;
144 dp = bp->bio_to->geom->softc;
145 dev = dp->d_dev;
146 gio = (struct g_ioctl *)bp->bio_data;
147 mtx_lock(&Giant);
148 error = devsw(dev)->d_ioctl(dev, gio->cmd,
149 gio->data, gio->fflag, gio->td);
150 mtx_unlock(&Giant);
151 g_io_deliver(bp, error);
152}
153
154static void
155g_disk_start(struct bio *bp)
156{
157 struct bio *bp2;
158 dev_t dev;
159 struct disk *dp;
160 struct g_ioctl *gio;
161 int error;
162
163 dp = bp->bio_to->geom->softc;
164 dev = dp->d_dev;
165 error = 0;
166 switch(bp->bio_cmd) {
167 case BIO_READ:
168 case BIO_WRITE:
169 bp2 = g_clone_bio(bp);
170 bp2->bio_done = g_disk_done;
171 bp2->bio_blkno = bp2->bio_offset >> DEV_BSHIFT;
172 bp2->bio_pblkno = bp2->bio_blkno;
173 bp2->bio_bcount = bp2->bio_length;
174 bp2->bio_dev = dev;
175 mtx_lock(&Giant);
176 devsw(dev)->d_strategy(bp2);
177 mtx_unlock(&Giant);
178 break;
179 case BIO_GETATTR:
180 if (g_handleattr_int(bp, "GEOM::sectorsize", dp->d_sectorsize))
181 break;
182 else if (g_handleattr_int(bp, "GEOM::fwsectors", dp->d_fwsectors))
183 break;
184 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads))
185 break;
186 else if (g_handleattr_off_t(bp, "GEOM::mediasize", dp->d_mediasize))
187 break;
188 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0))
189 break;
190 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
191 g_disk_kerneldump(bp, dp);
192 else if (!strcmp(bp->bio_attribute, "GEOM::ioctl") &&
193 bp->bio_length == sizeof *gio) {
194 g_call_me(g_disk_ioctl, bp);
195 return;
196 } else
197 error = ENOIOCTL;
198 break;
199 default:
200 error = EOPNOTSUPP;
201 break;
202 }
203 if (error) {
204 g_io_deliver(bp, error);
205 }
206 return;
207}
208
209dev_t
210disk_create(int unit, struct disk *dp, int flags, struct cdevsw *cdevsw, struct cdevsw *proto)
211{
212 static int once;
213 struct g_geom *gp;
214 struct g_provider *pp;
215 dev_t dev;
216
217 mtx_unlock(&Giant);
218 if (!once) {
219 g_add_class(&g_disk_class);
220 once++;
221 }
222 dev = g_malloc(sizeof *dev, M_WAITOK | M_ZERO);
223 dp->d_dev = dev;
224 dp->d_devsw = cdevsw;
225 dev->si_devsw = cdevsw;
226 dev->si_disk = dp;
227 dev->si_udev = dkmakeminor(unit, WHOLE_DISK_SLICE, RAW_PART);
228 g_topology_lock();
229 gp = g_new_geomf(&g_disk_class, "%s%d", cdevsw->d_name, unit);
230 strcpy(dev->si_name, gp->name);
231 gp->start = g_disk_start;
232 gp->access = g_disk_access;
233 gp->softc = dp;
234 dp->d_softc = gp;
235 pp = g_new_providerf(gp, "%s", gp->name);
236 g_error_provider(pp, 0);
237 g_topology_unlock();
238 mtx_lock(&Giant);
239 return (dev);
240}
241
242void disk_dev_synth(dev_t dev);
243
244void
245disk_dev_synth(dev_t dev)
246{
247
248 return;
249}
250
251void
252disk_destroy(dev_t dev)
253{
254 struct disk *dp;
255 struct g_geom *gp;
256
257 dp = dev->si_disk;
258 gp = dp->d_softc;
259 g_free(dev);
260 gp->flags |= G_GEOM_WITHER;
261 g_orphan_provider(LIST_FIRST(&gp->provider), ENXIO);
262}
263
264void
265disk_invalidate (struct disk *disk)
266{
267}
268
269static void
270g_kern_disks(void *p)
271{
272 struct sbuf *sb;
273 struct g_geom *gp;
274 char *sp;
275
276 sb = p;
277 sp = "";
278 g_topology_assert();
279 LIST_FOREACH(gp, &g_disk_class.geom, geom) {
280 sbuf_printf(sb, "%s%s", sp, gp->name);
281 sp = " ";
282 }
283 sbuf_finish(sb);
284 wakeup(sb);
285}
286
287static int
288sysctl_disks(SYSCTL_HANDLER_ARGS)
289{
290 int error;
291 struct sbuf *sb;
292
293 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
294 sbuf_clear(sb);
295 g_call_me(g_kern_disks, sb);
296 do {
297 tsleep(sb, PZERO, "kern.disks", hz);
298 } while(!sbuf_done(sb));
299 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb));
300 sbuf_delete(sb);
301 return error;
302}
303
304SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NOLOCK, 0, 0,
305 sysctl_disks, "A", "names of available disks");
306
307#endif
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 * $FreeBSD: head/sys/geom/geom_disk.c 104519 2002-10-05 16:35:33Z phk $
36 */
37
38#include "opt_geom.h"
39#ifndef NO_GEOM
40
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/kernel.h>
44#include <sys/sysctl.h>
45#include <sys/bio.h>
46#include <sys/conf.h>
47#include <sys/disk.h>
48#include <sys/diskslice.h>
49#include <sys/disklabel.h>
50#include <sys/malloc.h>
51#include <sys/sysctl.h>
52#include <sys/stdint.h>
53#include <machine/md_var.h>
54
55
56#include <sys/lock.h>
57#include <sys/mutex.h>
58#include <geom/geom.h>
59
60static g_access_t g_disk_access;
61
62struct g_class g_disk_class = {
63 "DISK",
64 NULL,
65 NULL,
66 G_CLASS_INITIALIZER
67};
68
69static int
70g_disk_access(struct g_provider *pp, int r, int w, int e)
71{
72 struct disk *dp;
73 dev_t dev;
74 int error;
75
76 g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)",
77 pp->name, r, w, e);
78 g_topology_assert();
79 r += pp->acr;
80 w += pp->acw;
81 e += pp->ace;
82 dp = pp->geom->softc;
83 dev = dp->d_dev;
84 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
85 mtx_lock(&Giant);
86 error = devsw(dev)->d_open(dev, 3, 0, NULL);
87 if (error != 0)
88 printf("Opened disk %s -> %d\n", pp->name, error);
89 mtx_unlock(&Giant);
90 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
91 mtx_lock(&Giant);
92 error = devsw(dev)->d_close(dev, 3, 0, NULL);
93 if (error != 0)
94 printf("Closed disk %s -> %d\n", pp->name, error);
95 mtx_unlock(&Giant);
96 } else {
97 error = 0;
98 }
99 pp->mediasize = dp->d_mediasize;
100 return (error);
101}
102
103static void
104g_disk_kerneldump(struct bio *bp, struct disk *dp)
105{
106 int error;
107 struct g_kerneldump *gkd;
108 struct dumperinfo di;
109 struct g_geom *gp;
110
111 gkd = (struct g_kerneldump*)bp->bio_data;
112 gp = bp->bio_to->geom;
113 g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)",
114 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length);
115 di.dumper = (dumper_t *)dp->d_devsw->d_dump;
116 di.priv = dp->d_dev;
117 di.blocksize = dp->d_sectorsize;
118 di.mediaoffset = gkd->offset;
119 di.mediasize = gkd->length;
120 error = set_dumper(&di);
121 g_io_deliver(bp, error);
122}
123
124static void
125g_disk_done(struct bio *bp)
126{
127
128 mtx_unlock(&Giant);
129 bp->bio_completed = bp->bio_length - bp->bio_resid;
130 g_std_done(bp);
131 mtx_lock(&Giant);
132}
133
134static void
135g_disk_ioctl(void *arg)
136{
137 struct bio *bp;
138 dev_t dev;
139 struct disk *dp;
140 struct g_ioctl *gio;
141 int error;
142
143 bp = arg;
144 dp = bp->bio_to->geom->softc;
145 dev = dp->d_dev;
146 gio = (struct g_ioctl *)bp->bio_data;
147 mtx_lock(&Giant);
148 error = devsw(dev)->d_ioctl(dev, gio->cmd,
149 gio->data, gio->fflag, gio->td);
150 mtx_unlock(&Giant);
151 g_io_deliver(bp, error);
152}
153
154static void
155g_disk_start(struct bio *bp)
156{
157 struct bio *bp2;
158 dev_t dev;
159 struct disk *dp;
160 struct g_ioctl *gio;
161 int error;
162
163 dp = bp->bio_to->geom->softc;
164 dev = dp->d_dev;
165 error = 0;
166 switch(bp->bio_cmd) {
167 case BIO_READ:
168 case BIO_WRITE:
169 bp2 = g_clone_bio(bp);
170 bp2->bio_done = g_disk_done;
171 bp2->bio_blkno = bp2->bio_offset >> DEV_BSHIFT;
172 bp2->bio_pblkno = bp2->bio_blkno;
173 bp2->bio_bcount = bp2->bio_length;
174 bp2->bio_dev = dev;
175 mtx_lock(&Giant);
176 devsw(dev)->d_strategy(bp2);
177 mtx_unlock(&Giant);
178 break;
179 case BIO_GETATTR:
180 if (g_handleattr_int(bp, "GEOM::sectorsize", dp->d_sectorsize))
181 break;
182 else if (g_handleattr_int(bp, "GEOM::fwsectors", dp->d_fwsectors))
183 break;
184 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads))
185 break;
186 else if (g_handleattr_off_t(bp, "GEOM::mediasize", dp->d_mediasize))
187 break;
188 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0))
189 break;
190 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
191 g_disk_kerneldump(bp, dp);
192 else if (!strcmp(bp->bio_attribute, "GEOM::ioctl") &&
193 bp->bio_length == sizeof *gio) {
194 g_call_me(g_disk_ioctl, bp);
195 return;
196 } else
197 error = ENOIOCTL;
198 break;
199 default:
200 error = EOPNOTSUPP;
201 break;
202 }
203 if (error) {
204 g_io_deliver(bp, error);
205 }
206 return;
207}
208
209dev_t
210disk_create(int unit, struct disk *dp, int flags, struct cdevsw *cdevsw, struct cdevsw *proto)
211{
212 static int once;
213 struct g_geom *gp;
214 struct g_provider *pp;
215 dev_t dev;
216
217 mtx_unlock(&Giant);
218 if (!once) {
219 g_add_class(&g_disk_class);
220 once++;
221 }
222 dev = g_malloc(sizeof *dev, M_WAITOK | M_ZERO);
223 dp->d_dev = dev;
224 dp->d_devsw = cdevsw;
225 dev->si_devsw = cdevsw;
226 dev->si_disk = dp;
227 dev->si_udev = dkmakeminor(unit, WHOLE_DISK_SLICE, RAW_PART);
228 g_topology_lock();
229 gp = g_new_geomf(&g_disk_class, "%s%d", cdevsw->d_name, unit);
230 strcpy(dev->si_name, gp->name);
231 gp->start = g_disk_start;
232 gp->access = g_disk_access;
233 gp->softc = dp;
234 dp->d_softc = gp;
235 pp = g_new_providerf(gp, "%s", gp->name);
236 g_error_provider(pp, 0);
237 g_topology_unlock();
238 mtx_lock(&Giant);
239 return (dev);
240}
241
242void disk_dev_synth(dev_t dev);
243
244void
245disk_dev_synth(dev_t dev)
246{
247
248 return;
249}
250
251void
252disk_destroy(dev_t dev)
253{
254 struct disk *dp;
255 struct g_geom *gp;
256
257 dp = dev->si_disk;
258 gp = dp->d_softc;
259 g_free(dev);
260 gp->flags |= G_GEOM_WITHER;
261 g_orphan_provider(LIST_FIRST(&gp->provider), ENXIO);
262}
263
264void
265disk_invalidate (struct disk *disk)
266{
267}
268
269static void
270g_kern_disks(void *p)
271{
272 struct sbuf *sb;
273 struct g_geom *gp;
274 char *sp;
275
276 sb = p;
277 sp = "";
278 g_topology_assert();
279 LIST_FOREACH(gp, &g_disk_class.geom, geom) {
280 sbuf_printf(sb, "%s%s", sp, gp->name);
281 sp = " ";
282 }
283 sbuf_finish(sb);
284 wakeup(sb);
285}
286
287static int
288sysctl_disks(SYSCTL_HANDLER_ARGS)
289{
290 int error;
291 struct sbuf *sb;
292
293 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
294 sbuf_clear(sb);
295 g_call_me(g_kern_disks, sb);
296 do {
297 tsleep(sb, PZERO, "kern.disks", hz);
298 } while(!sbuf_done(sb));
299 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb));
300 sbuf_delete(sb);
301 return error;
302}
303
304SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NOLOCK, 0, 0,
305 sysctl_disks, "A", "names of available disks");
306
307#endif