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 110708 2003-02-11 14:12:06Z 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 if (bp2 == NULL) {
198 error = ENOMEM;
199 break;
200 }
201 bp2->bio_done = g_disk_done;
202 bp2->bio_blkno = bp2->bio_offset >> DEV_BSHIFT;
203 bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize;
204 bp2->bio_bcount = bp2->bio_length;
205 bp2->bio_dev = dev;
206 bp2->bio_disk = dp;
207 g_disk_lock_giant(dp);
208 dp->d_strategy(bp2);
209 g_disk_unlock_giant(dp);
210 break;
211 case BIO_GETATTR:
212 if (g_handleattr_int(bp, "GEOM::fwsectors", dp->d_fwsectors))
213 break;
214 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads))
215 break;
216 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0))
217 break;
218 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
219 g_disk_kerneldump(bp, dp);
220 else if ((dp->d_ioctl != NULL || dp->d_cioctl != NULL) &&
221 !strcmp(bp->bio_attribute, "GEOM::ioctl") &&
222 bp->bio_length == sizeof *gio) {
223 gio = (struct g_ioctl *)bp->bio_data;
224 if (dp->d_ioctl != NULL) {
225 gio->func = (d_ioctl_t *)(dp->d_ioctl);
226 gio->dev = dp;
227 } else {
228 gio->func = dp->d_cioctl;
229 gio->dev = dp->d_dev;
230 }
231 error = EDIRIOCTL;
232 } else
233 error = ENOIOCTL;
234 break;
235 case BIO_SETATTR:
236 if (!strcmp(bp->bio_attribute, "GEOM::ioctl") &&
237 bp->bio_length == sizeof *gio) {
238 gio = (struct g_ioctl *)bp->bio_data;
239 gio->func = devsw(dp->d_dev)->d_ioctl;
240 gio->dev = dp->d_dev;
241 error = EDIRIOCTL;
242 } else
243 error = ENOIOCTL;
244 break;
245 default:
246 error = EOPNOTSUPP;
247 break;
248 }
249 if (error != EJUSTRETURN)
250 g_io_deliver(bp, error);
251 return;
252}
253
254static void
255g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
256{
257 struct disk *dp;
258
259 dp = gp->softc;
260 if (indent == NULL) {
261 sbuf_printf(sb, " hd %u", dp->d_fwheads);
262 sbuf_printf(sb, " sc %u", dp->d_fwsectors);
263 return;
264 }
265 if (pp != NULL) {
266 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n",
267 indent, dp->d_fwheads);
268 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n",
269 indent, dp->d_fwsectors);
270 }
271}
272
273static void
274g_disk_create(void *arg)
275{
276 struct g_geom *gp;
277 struct g_provider *pp;
278 struct disk *dp;
279
280 g_topology_assert();
281 dp = arg;
282 gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit);
283 gp->start = g_disk_start;
284 gp->access = g_disk_access;
285 gp->softc = dp;
286 gp->dumpconf = g_disk_dumpconf;
287 dp->d_geom = gp;
288 pp = g_new_providerf(gp, "%s", gp->name);
289 pp->mediasize = dp->d_mediasize;
290 pp->sectorsize = dp->d_sectorsize;
291 if (dp->d_flags & DISKFLAG_CANDELETE)
292 pp->flags |= G_PF_CANDELETE;
293 g_error_provider(pp, 0);
294 if (bootverbose)
295 printf("GEOM: new disk %s\n", gp->name);
296}
297
298
299
300dev_t
301disk_create(int unit, struct disk *dp, int flags, struct cdevsw *cdevsw, void * unused __unused)
302{
303 dev_t dev;
304
305 dev = g_malloc(sizeof *dev, M_ZERO);
306 dp->d_unit = unit;
307 dp->d_dev = dev;
308 dp->d_flags = flags;
309 dp->d_devsw = cdevsw;
310 dev->si_devsw = cdevsw;
311 if (cdevsw != NULL) {
312 dp->d_copen = cdevsw->d_open;
313 dp->d_cclose = cdevsw->d_close;
314 dp->d_cioctl = cdevsw->d_ioctl;
315 dp->d_strategy = cdevsw->d_strategy;
316 dp->d_dump = (dumper_t *)cdevsw->d_dump;
317 dp->d_name = cdevsw->d_name;
318 }
319 KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy"));
320 KASSERT(dp->d_name != NULL, ("disk_create need d_name"));
321 KASSERT(*dp->d_name != 0, ("disk_create need d_name"));
322 KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long"));
323 dev->si_disk = dp;
324 dev->si_udev = 0x10002; /* XXX: Needed ? */
325 dev->si_name = dev->__si_namebuf;
326 sprintf(dev->si_name, "%s%d", dp->d_name, unit);
327 g_call_me(g_disk_create, dp);
328 return (dev);
329}
330
331void
332disk_destroy(dev_t dev)
333{
334 struct disk *dp;
335 struct g_geom *gp;
336
337 dp = dev->si_disk;
338 gp = dp->d_geom;
339 g_free(dev);
340 gp->flags |= G_GEOM_WITHER;
341 gp->softc = NULL;
342 g_orphan_provider(LIST_FIRST(&gp->provider), ENXIO);
343}
344
345static void
346g_kern_disks(void *p)
347{
348 struct sbuf *sb;
349 struct g_geom *gp;
350 char *sp;
351
352 sb = p;
353 sp = "";
354 g_topology_assert();
355 LIST_FOREACH(gp, &g_disk_class.geom, geom) {
356 sbuf_printf(sb, "%s%s", sp, gp->name);
357 sp = " ";
358 }
359 sbuf_finish(sb);
360 wakeup(sb);
361}
362
363static int
364sysctl_disks(SYSCTL_HANDLER_ARGS)
365{
366 int error;
367 struct sbuf *sb;
368
369 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
370 sbuf_clear(sb);
371 g_call_me(g_kern_disks, sb);
372 do {
373 tsleep(sb, PZERO, "kern.disks", hz);
374 } while(!sbuf_done(sb));
375 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
376 sbuf_delete(sb);
377 return error;
378}
379
380SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NOLOCK, 0, 0,
381 sysctl_disks, "A", "names of available disks");
382
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 110708 2003-02-11 14:12:06Z 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 if (bp2 == NULL) {
198 error = ENOMEM;
199 break;
200 }
201 bp2->bio_done = g_disk_done;
202 bp2->bio_blkno = bp2->bio_offset >> DEV_BSHIFT;
203 bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize;
204 bp2->bio_bcount = bp2->bio_length;
205 bp2->bio_dev = dev;
206 bp2->bio_disk = dp;
207 g_disk_lock_giant(dp);
208 dp->d_strategy(bp2);
209 g_disk_unlock_giant(dp);
210 break;
211 case BIO_GETATTR:
212 if (g_handleattr_int(bp, "GEOM::fwsectors", dp->d_fwsectors))
213 break;
214 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads))
215 break;
216 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0))
217 break;
218 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
219 g_disk_kerneldump(bp, dp);
220 else if ((dp->d_ioctl != NULL || dp->d_cioctl != NULL) &&
221 !strcmp(bp->bio_attribute, "GEOM::ioctl") &&
222 bp->bio_length == sizeof *gio) {
223 gio = (struct g_ioctl *)bp->bio_data;
224 if (dp->d_ioctl != NULL) {
225 gio->func = (d_ioctl_t *)(dp->d_ioctl);
226 gio->dev = dp;
227 } else {
228 gio->func = dp->d_cioctl;
229 gio->dev = dp->d_dev;
230 }
231 error = EDIRIOCTL;
232 } else
233 error = ENOIOCTL;
234 break;
235 case BIO_SETATTR:
236 if (!strcmp(bp->bio_attribute, "GEOM::ioctl") &&
237 bp->bio_length == sizeof *gio) {
238 gio = (struct g_ioctl *)bp->bio_data;
239 gio->func = devsw(dp->d_dev)->d_ioctl;
240 gio->dev = dp->d_dev;
241 error = EDIRIOCTL;
242 } else
243 error = ENOIOCTL;
244 break;
245 default:
246 error = EOPNOTSUPP;
247 break;
248 }
249 if (error != EJUSTRETURN)
250 g_io_deliver(bp, error);
251 return;
252}
253
254static void
255g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
256{
257 struct disk *dp;
258
259 dp = gp->softc;
260 if (indent == NULL) {
261 sbuf_printf(sb, " hd %u", dp->d_fwheads);
262 sbuf_printf(sb, " sc %u", dp->d_fwsectors);
263 return;
264 }
265 if (pp != NULL) {
266 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n",
267 indent, dp->d_fwheads);
268 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n",
269 indent, dp->d_fwsectors);
270 }
271}
272
273static void
274g_disk_create(void *arg)
275{
276 struct g_geom *gp;
277 struct g_provider *pp;
278 struct disk *dp;
279
280 g_topology_assert();
281 dp = arg;
282 gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit);
283 gp->start = g_disk_start;
284 gp->access = g_disk_access;
285 gp->softc = dp;
286 gp->dumpconf = g_disk_dumpconf;
287 dp->d_geom = gp;
288 pp = g_new_providerf(gp, "%s", gp->name);
289 pp->mediasize = dp->d_mediasize;
290 pp->sectorsize = dp->d_sectorsize;
291 if (dp->d_flags & DISKFLAG_CANDELETE)
292 pp->flags |= G_PF_CANDELETE;
293 g_error_provider(pp, 0);
294 if (bootverbose)
295 printf("GEOM: new disk %s\n", gp->name);
296}
297
298
299
300dev_t
301disk_create(int unit, struct disk *dp, int flags, struct cdevsw *cdevsw, void * unused __unused)
302{
303 dev_t dev;
304
305 dev = g_malloc(sizeof *dev, M_ZERO);
306 dp->d_unit = unit;
307 dp->d_dev = dev;
308 dp->d_flags = flags;
309 dp->d_devsw = cdevsw;
310 dev->si_devsw = cdevsw;
311 if (cdevsw != NULL) {
312 dp->d_copen = cdevsw->d_open;
313 dp->d_cclose = cdevsw->d_close;
314 dp->d_cioctl = cdevsw->d_ioctl;
315 dp->d_strategy = cdevsw->d_strategy;
316 dp->d_dump = (dumper_t *)cdevsw->d_dump;
317 dp->d_name = cdevsw->d_name;
318 }
319 KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy"));
320 KASSERT(dp->d_name != NULL, ("disk_create need d_name"));
321 KASSERT(*dp->d_name != 0, ("disk_create need d_name"));
322 KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long"));
323 dev->si_disk = dp;
324 dev->si_udev = 0x10002; /* XXX: Needed ? */
325 dev->si_name = dev->__si_namebuf;
326 sprintf(dev->si_name, "%s%d", dp->d_name, unit);
327 g_call_me(g_disk_create, dp);
328 return (dev);
329}
330
331void
332disk_destroy(dev_t dev)
333{
334 struct disk *dp;
335 struct g_geom *gp;
336
337 dp = dev->si_disk;
338 gp = dp->d_geom;
339 g_free(dev);
340 gp->flags |= G_GEOM_WITHER;
341 gp->softc = NULL;
342 g_orphan_provider(LIST_FIRST(&gp->provider), ENXIO);
343}
344
345static void
346g_kern_disks(void *p)
347{
348 struct sbuf *sb;
349 struct g_geom *gp;
350 char *sp;
351
352 sb = p;
353 sp = "";
354 g_topology_assert();
355 LIST_FOREACH(gp, &g_disk_class.geom, geom) {
356 sbuf_printf(sb, "%s%s", sp, gp->name);
357 sp = " ";
358 }
359 sbuf_finish(sb);
360 wakeup(sb);
361}
362
363static int
364sysctl_disks(SYSCTL_HANDLER_ARGS)
365{
366 int error;
367 struct sbuf *sb;
368
369 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
370 sbuf_clear(sb);
371 g_call_me(g_kern_disks, sb);
372 do {
373 tsleep(sb, PZERO, "kern.disks", hz);
374 } while(!sbuf_done(sb));
375 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
376 sbuf_delete(sb);
377 return error;
378}
379
380SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NOLOCK, 0, 0,
381 sysctl_disks, "A", "names of available disks");
382