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 110475 2003-02-06 21:50:39Z phk $
36 */
37
38#include "opt_geom.h"
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/kernel.h>
43#include <sys/sysctl.h>
44#include <sys/bio.h>
45#include <sys/conf.h>
46#include <sys/disk.h>
47#include <sys/fcntl.h>
48#include <sys/malloc.h>
49#include <sys/sysctl.h>
50#include <sys/stdint.h>
51#include <machine/md_var.h>
52
53
54#include <sys/lock.h>
55#include <sys/mutex.h>
56#include <geom/geom.h>
57
58static g_access_t g_disk_access;
59
60struct g_class g_disk_class = {
61 "DISK",
62 NULL,
63 NULL,
64 G_CLASS_INITIALIZER
65};
66
67DECLARE_GEOM_CLASS(g_disk_class, g_disk);
68
69static void __inline
70g_disk_lock_giant(struct disk *dp)
71{
72 if (dp->d_flags & DISKFLAG_NOGIANT)
73 return;
74 mtx_lock(&Giant);
75}
76
77static void __inline
78g_disk_unlock_giant(struct disk *dp)
79{
80 if (dp->d_flags & DISKFLAG_NOGIANT)
81 return;
82 mtx_unlock(&Giant);
83}
84
85static int
86g_disk_access(struct g_provider *pp, int r, int w, int e)
87{
88 struct disk *dp;
89 dev_t dev;
90 int error;
91
92 g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)",
93 pp->name, r, w, e);
94 g_topology_assert();
95 r += pp->acr;
96 w += pp->acw;
97 e += pp->ace;
98 dp = pp->geom->softc;
99 dev = dp->d_dev;
100 error = 0;
101 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
102 if (dp->d_open != NULL || dp->d_copen != NULL) {
103 g_disk_lock_giant(dp);
104 if (dp->d_open != NULL)
105 error = dp->d_open(dp);
106 else
107 error = dp->d_copen(dev, FREAD|FWRITE, 0, NULL);
108 if (error != 0)
109 printf("Opened disk %s -> %d\n",
110 pp->name, error);
111 g_disk_unlock_giant(dp);
112 }
113 pp->mediasize = dp->d_mediasize;
114 pp->sectorsize = dp->d_sectorsize;
115 dp->d_flags |= DISKFLAG_OPEN;
116 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
117 if (dp->d_close != NULL || dp->d_cclose != NULL) {
118 g_disk_lock_giant(dp);
119 if (dp->d_close != NULL)
120 error = dp->d_close(dp);
121 else
122 error = dp->d_cclose(dev, FREAD|FWRITE, 0, NULL);
123 if (error != 0)
124 printf("Closed disk %s -> %d\n",
125 pp->name, error);
126 g_disk_unlock_giant(dp);
127 }
128 dp->d_flags &= ~DISKFLAG_OPEN;
129 }
130 return (error);
131}
132
133static void
134g_disk_kerneldump(struct bio *bp, struct disk *dp)
135{
136 int error;
137 struct g_kerneldump *gkd;
138 struct dumperinfo di;
139 struct g_geom *gp;
140
141 gkd = (struct g_kerneldump*)bp->bio_data;
142 gp = bp->bio_to->geom;
143 g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)",
144 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length);
145 di.dumper = dp->d_dump;
146 di.priv = dp->d_dev;
147 di.blocksize = dp->d_sectorsize;
148 di.mediaoffset = gkd->offset;
149 di.mediasize = gkd->length;
150 error = set_dumper(&di);
151 g_io_deliver(bp, error);
152}
153
154static void
155g_disk_done(struct bio *bp)
156{
157#ifdef maybe_not
158 struct disk *dp;
159
160 dp = bp->bio_disk;
161 bp->bio_completed = bp->bio_length - bp->bio_resid;
162 if (!(dp->d_flags & DISKFLAG_NOGIANT)) {
163 DROP_GIANT();
164 g_std_done(bp);
165 PICKUP_GIANT();
166 } else {
167 g_std_done(bp);
168 }
169#else
170 bp->bio_completed = bp->bio_length - bp->bio_resid;
171 g_std_done(bp);
172#endif
173}
174
175static void
176g_disk_start(struct bio *bp)
177{
178 struct bio *bp2;
179 dev_t dev;
180 struct disk *dp;
181 struct g_ioctl *gio;
182 int error;
183
184 dp = bp->bio_to->geom->softc;
185 dev = dp->d_dev;
186 error = EJUSTRETURN;
187 switch(bp->bio_cmd) {
188 case BIO_DELETE:
189 if (!(dp->d_flags & DISKFLAG_CANDELETE)) {
190 error = 0;
191 break;
192 }
193 /* fall-through */
194 case BIO_READ:
195 case BIO_WRITE:
196 bp2 = g_clone_bio(bp);
197 bp2->bio_done = g_disk_done;
198 bp2->bio_blkno = bp2->bio_offset >> DEV_BSHIFT;
199 bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize;
200 bp2->bio_bcount = bp2->bio_length;
201 bp2->bio_dev = dev;
202 bp2->bio_disk = dp;
203 g_disk_lock_giant(dp);
204 dp->d_strategy(bp2);
205 g_disk_unlock_giant(dp);
206 break;
207 case BIO_GETATTR:
208 if (g_handleattr_int(bp, "GEOM::fwsectors", dp->d_fwsectors))
209 break;
210 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads))
211 break;
212 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0))
213 break;
214 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
215 g_disk_kerneldump(bp, dp);
216 else if ((dp->d_ioctl != NULL || dp->d_cioctl != NULL) &&
217 !strcmp(bp->bio_attribute, "GEOM::ioctl") &&
218 bp->bio_length == sizeof *gio) {
219 gio = (struct g_ioctl *)bp->bio_data;
220 if (dp->d_ioctl != NULL) {
221 gio->func = (d_ioctl_t *)(dp->d_ioctl);
222 gio->dev = dp;
223 } else {
224 gio->func = dp->d_cioctl;
225 gio->dev = dp->d_dev;
226 }
227 error = EDIRIOCTL;
228 } else
229 error = ENOIOCTL;
230 break;
231 case BIO_SETATTR:
232 if (!strcmp(bp->bio_attribute, "GEOM::ioctl") &&
233 bp->bio_length == sizeof *gio) {
234 gio = (struct g_ioctl *)bp->bio_data;
235 gio->func = devsw(dp->d_dev)->d_ioctl;
236 gio->dev = dp->d_dev;
237 error = EDIRIOCTL;
238 } else
239 error = ENOIOCTL;
240 break;
241 default:
242 error = EOPNOTSUPP;
243 break;
244 }
245 if (error != EJUSTRETURN)
246 g_io_deliver(bp, error);
247 return;
248}
249
250static void
251g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
252{
253 struct disk *dp;
254
255 dp = gp->softc;
256 if (indent == NULL) {
257 sbuf_printf(sb, " hd %u", dp->d_fwheads);
258 sbuf_printf(sb, " sc %u", dp->d_fwsectors);
259 return;
260 }
261 if (pp != NULL) {
262 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n",
263 indent, dp->d_fwheads);
264 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n",
265 indent, dp->d_fwsectors);
266 }
267}
268
269static void
270g_disk_create(void *arg)
271{
272 struct g_geom *gp;
273 struct g_provider *pp;
274 dev_t dev;
275
276 g_topology_assert();
277 dev = arg;
278 gp = g_new_geomf(&g_disk_class, dev->si_name);
279 gp->start = g_disk_start;
280 gp->access = g_disk_access;
281 gp->softc = dev->si_disk;
282 gp->dumpconf = g_disk_dumpconf;
283 dev->si_disk->d_geom = gp;
284 pp = g_new_providerf(gp, "%s", gp->name);
285 pp->mediasize = dev->si_disk->d_mediasize;
286 pp->sectorsize = dev->si_disk->d_sectorsize;
287 g_error_provider(pp, 0);
288 if (bootverbose)
289 printf("GEOM: new disk %s\n", gp->name);
290}
291
292
293
294dev_t
295disk_create(int unit, struct disk *dp, int flags, struct cdevsw *cdevsw, void * unused __unused)
296{
297 dev_t dev;
298
299 dev = g_malloc(sizeof *dev, M_ZERO);
300 dp->d_dev = dev;
301 dp->d_flags = flags;
302 dp->d_devsw = cdevsw;
303 dev->si_devsw = cdevsw;
304 if (cdevsw != NULL) {
305 dp->d_copen = cdevsw->d_open;
306 dp->d_cclose = cdevsw->d_close;
307 dp->d_cioctl = cdevsw->d_ioctl;
308 dp->d_strategy = cdevsw->d_strategy;
309 dp->d_dump = (dumper_t *)cdevsw->d_dump;
310 dp->d_name = cdevsw->d_name;
311 }
312 KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy"));
313 KASSERT(dp->d_name != NULL, ("disk_create need d_name"));
314 KASSERT(*dp->d_name != 0, ("disk_create need d_name"));
315 KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long"));
316 dev->si_disk = dp;
317 dev->si_udev = 0x10002; /* XXX: Needed ? */
318 dev->si_name = dev->__si_namebuf;
319 sprintf(dev->si_name, "%s%d", dp->d_name, unit);
320 g_call_me(g_disk_create, dev);
321 return (dev);
322}
323
324void
325disk_destroy(dev_t dev)
326{
327 struct disk *dp;
328 struct g_geom *gp;
329
330 dp = dev->si_disk;
331 gp = dp->d_geom;
332 g_free(dev);
333 gp->flags |= G_GEOM_WITHER;
334 gp->softc = NULL;
335 g_orphan_provider(LIST_FIRST(&gp->provider), ENXIO);
336}
337
338static void
339g_kern_disks(void *p)
340{
341 struct sbuf *sb;
342 struct g_geom *gp;
343 char *sp;
344
345 sb = p;
346 sp = "";
347 g_topology_assert();
348 LIST_FOREACH(gp, &g_disk_class.geom, geom) {
349 sbuf_printf(sb, "%s%s", sp, gp->name);
350 sp = " ";
351 }
352 sbuf_finish(sb);
353 wakeup(sb);
354}
355
356static int
357sysctl_disks(SYSCTL_HANDLER_ARGS)
358{
359 int error;
360 struct sbuf *sb;
361
362 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
363 sbuf_clear(sb);
364 g_call_me(g_kern_disks, sb);
365 do {
366 tsleep(sb, PZERO, "kern.disks", hz);
367 } while(!sbuf_done(sb));
368 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
369 sbuf_delete(sb);
370 return error;
371}
372
373SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NOLOCK, 0, 0,
374 sysctl_disks, "A", "names of available disks");
375
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 110475 2003-02-06 21:50:39Z phk $
36 */
37
38#include "opt_geom.h"
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/kernel.h>
43#include <sys/sysctl.h>
44#include <sys/bio.h>
45#include <sys/conf.h>
46#include <sys/disk.h>
47#include <sys/fcntl.h>
48#include <sys/malloc.h>
49#include <sys/sysctl.h>
50#include <sys/stdint.h>
51#include <machine/md_var.h>
52
53
54#include <sys/lock.h>
55#include <sys/mutex.h>
56#include <geom/geom.h>
57
58static g_access_t g_disk_access;
59
60struct g_class g_disk_class = {
61 "DISK",
62 NULL,
63 NULL,
64 G_CLASS_INITIALIZER
65};
66
67DECLARE_GEOM_CLASS(g_disk_class, g_disk);
68
69static void __inline
70g_disk_lock_giant(struct disk *dp)
71{
72 if (dp->d_flags & DISKFLAG_NOGIANT)
73 return;
74 mtx_lock(&Giant);
75}
76
77static void __inline
78g_disk_unlock_giant(struct disk *dp)
79{
80 if (dp->d_flags & DISKFLAG_NOGIANT)
81 return;
82 mtx_unlock(&Giant);
83}
84
85static int
86g_disk_access(struct g_provider *pp, int r, int w, int e)
87{
88 struct disk *dp;
89 dev_t dev;
90 int error;
91
92 g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)",
93 pp->name, r, w, e);
94 g_topology_assert();
95 r += pp->acr;
96 w += pp->acw;
97 e += pp->ace;
98 dp = pp->geom->softc;
99 dev = dp->d_dev;
100 error = 0;
101 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
102 if (dp->d_open != NULL || dp->d_copen != NULL) {
103 g_disk_lock_giant(dp);
104 if (dp->d_open != NULL)
105 error = dp->d_open(dp);
106 else
107 error = dp->d_copen(dev, FREAD|FWRITE, 0, NULL);
108 if (error != 0)
109 printf("Opened disk %s -> %d\n",
110 pp->name, error);
111 g_disk_unlock_giant(dp);
112 }
113 pp->mediasize = dp->d_mediasize;
114 pp->sectorsize = dp->d_sectorsize;
115 dp->d_flags |= DISKFLAG_OPEN;
116 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
117 if (dp->d_close != NULL || dp->d_cclose != NULL) {
118 g_disk_lock_giant(dp);
119 if (dp->d_close != NULL)
120 error = dp->d_close(dp);
121 else
122 error = dp->d_cclose(dev, FREAD|FWRITE, 0, NULL);
123 if (error != 0)
124 printf("Closed disk %s -> %d\n",
125 pp->name, error);
126 g_disk_unlock_giant(dp);
127 }
128 dp->d_flags &= ~DISKFLAG_OPEN;
129 }
130 return (error);
131}
132
133static void
134g_disk_kerneldump(struct bio *bp, struct disk *dp)
135{
136 int error;
137 struct g_kerneldump *gkd;
138 struct dumperinfo di;
139 struct g_geom *gp;
140
141 gkd = (struct g_kerneldump*)bp->bio_data;
142 gp = bp->bio_to->geom;
143 g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)",
144 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length);
145 di.dumper = dp->d_dump;
146 di.priv = dp->d_dev;
147 di.blocksize = dp->d_sectorsize;
148 di.mediaoffset = gkd->offset;
149 di.mediasize = gkd->length;
150 error = set_dumper(&di);
151 g_io_deliver(bp, error);
152}
153
154static void
155g_disk_done(struct bio *bp)
156{
157#ifdef maybe_not
158 struct disk *dp;
159
160 dp = bp->bio_disk;
161 bp->bio_completed = bp->bio_length - bp->bio_resid;
162 if (!(dp->d_flags & DISKFLAG_NOGIANT)) {
163 DROP_GIANT();
164 g_std_done(bp);
165 PICKUP_GIANT();
166 } else {
167 g_std_done(bp);
168 }
169#else
170 bp->bio_completed = bp->bio_length - bp->bio_resid;
171 g_std_done(bp);
172#endif
173}
174
175static void
176g_disk_start(struct bio *bp)
177{
178 struct bio *bp2;
179 dev_t dev;
180 struct disk *dp;
181 struct g_ioctl *gio;
182 int error;
183
184 dp = bp->bio_to->geom->softc;
185 dev = dp->d_dev;
186 error = EJUSTRETURN;
187 switch(bp->bio_cmd) {
188 case BIO_DELETE:
189 if (!(dp->d_flags & DISKFLAG_CANDELETE)) {
190 error = 0;
191 break;
192 }
193 /* fall-through */
194 case BIO_READ:
195 case BIO_WRITE:
196 bp2 = g_clone_bio(bp);
197 bp2->bio_done = g_disk_done;
198 bp2->bio_blkno = bp2->bio_offset >> DEV_BSHIFT;
199 bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize;
200 bp2->bio_bcount = bp2->bio_length;
201 bp2->bio_dev = dev;
202 bp2->bio_disk = dp;
203 g_disk_lock_giant(dp);
204 dp->d_strategy(bp2);
205 g_disk_unlock_giant(dp);
206 break;
207 case BIO_GETATTR:
208 if (g_handleattr_int(bp, "GEOM::fwsectors", dp->d_fwsectors))
209 break;
210 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads))
211 break;
212 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0))
213 break;
214 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
215 g_disk_kerneldump(bp, dp);
216 else if ((dp->d_ioctl != NULL || dp->d_cioctl != NULL) &&
217 !strcmp(bp->bio_attribute, "GEOM::ioctl") &&
218 bp->bio_length == sizeof *gio) {
219 gio = (struct g_ioctl *)bp->bio_data;
220 if (dp->d_ioctl != NULL) {
221 gio->func = (d_ioctl_t *)(dp->d_ioctl);
222 gio->dev = dp;
223 } else {
224 gio->func = dp->d_cioctl;
225 gio->dev = dp->d_dev;
226 }
227 error = EDIRIOCTL;
228 } else
229 error = ENOIOCTL;
230 break;
231 case BIO_SETATTR:
232 if (!strcmp(bp->bio_attribute, "GEOM::ioctl") &&
233 bp->bio_length == sizeof *gio) {
234 gio = (struct g_ioctl *)bp->bio_data;
235 gio->func = devsw(dp->d_dev)->d_ioctl;
236 gio->dev = dp->d_dev;
237 error = EDIRIOCTL;
238 } else
239 error = ENOIOCTL;
240 break;
241 default:
242 error = EOPNOTSUPP;
243 break;
244 }
245 if (error != EJUSTRETURN)
246 g_io_deliver(bp, error);
247 return;
248}
249
250static void
251g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
252{
253 struct disk *dp;
254
255 dp = gp->softc;
256 if (indent == NULL) {
257 sbuf_printf(sb, " hd %u", dp->d_fwheads);
258 sbuf_printf(sb, " sc %u", dp->d_fwsectors);
259 return;
260 }
261 if (pp != NULL) {
262 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n",
263 indent, dp->d_fwheads);
264 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n",
265 indent, dp->d_fwsectors);
266 }
267}
268
269static void
270g_disk_create(void *arg)
271{
272 struct g_geom *gp;
273 struct g_provider *pp;
274 dev_t dev;
275
276 g_topology_assert();
277 dev = arg;
278 gp = g_new_geomf(&g_disk_class, dev->si_name);
279 gp->start = g_disk_start;
280 gp->access = g_disk_access;
281 gp->softc = dev->si_disk;
282 gp->dumpconf = g_disk_dumpconf;
283 dev->si_disk->d_geom = gp;
284 pp = g_new_providerf(gp, "%s", gp->name);
285 pp->mediasize = dev->si_disk->d_mediasize;
286 pp->sectorsize = dev->si_disk->d_sectorsize;
287 g_error_provider(pp, 0);
288 if (bootverbose)
289 printf("GEOM: new disk %s\n", gp->name);
290}
291
292
293
294dev_t
295disk_create(int unit, struct disk *dp, int flags, struct cdevsw *cdevsw, void * unused __unused)
296{
297 dev_t dev;
298
299 dev = g_malloc(sizeof *dev, M_ZERO);
300 dp->d_dev = dev;
301 dp->d_flags = flags;
302 dp->d_devsw = cdevsw;
303 dev->si_devsw = cdevsw;
304 if (cdevsw != NULL) {
305 dp->d_copen = cdevsw->d_open;
306 dp->d_cclose = cdevsw->d_close;
307 dp->d_cioctl = cdevsw->d_ioctl;
308 dp->d_strategy = cdevsw->d_strategy;
309 dp->d_dump = (dumper_t *)cdevsw->d_dump;
310 dp->d_name = cdevsw->d_name;
311 }
312 KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy"));
313 KASSERT(dp->d_name != NULL, ("disk_create need d_name"));
314 KASSERT(*dp->d_name != 0, ("disk_create need d_name"));
315 KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long"));
316 dev->si_disk = dp;
317 dev->si_udev = 0x10002; /* XXX: Needed ? */
318 dev->si_name = dev->__si_namebuf;
319 sprintf(dev->si_name, "%s%d", dp->d_name, unit);
320 g_call_me(g_disk_create, dev);
321 return (dev);
322}
323
324void
325disk_destroy(dev_t dev)
326{
327 struct disk *dp;
328 struct g_geom *gp;
329
330 dp = dev->si_disk;
331 gp = dp->d_geom;
332 g_free(dev);
333 gp->flags |= G_GEOM_WITHER;
334 gp->softc = NULL;
335 g_orphan_provider(LIST_FIRST(&gp->provider), ENXIO);
336}
337
338static void
339g_kern_disks(void *p)
340{
341 struct sbuf *sb;
342 struct g_geom *gp;
343 char *sp;
344
345 sb = p;
346 sp = "";
347 g_topology_assert();
348 LIST_FOREACH(gp, &g_disk_class.geom, geom) {
349 sbuf_printf(sb, "%s%s", sp, gp->name);
350 sp = " ";
351 }
352 sbuf_finish(sb);
353 wakeup(sb);
354}
355
356static int
357sysctl_disks(SYSCTL_HANDLER_ARGS)
358{
359 int error;
360 struct sbuf *sb;
361
362 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
363 sbuf_clear(sb);
364 g_call_me(g_kern_disks, sb);
365 do {
366 tsleep(sb, PZERO, "kern.disks", hz);
367 } while(!sbuf_done(sb));
368 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
369 sbuf_delete(sb);
370 return error;
371}
372
373SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NOLOCK, 0, 0,
374 sysctl_disks, "A", "names of available disks");
375