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