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_disk.c 120572 2003-09-29 07:44:23Z phk $");
38
39#include "opt_geom.h"
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/fcntl.h>
48#include <sys/malloc.h>
49#include <sys/sysctl.h>
50#include <sys/devicestat.h>
51#include <machine/md_var.h>
52
53#include <sys/lock.h>
54#include <sys/mutex.h>
55#include <geom/geom.h>
56#include <geom/geom_disk.h>
57#include <geom/geom_int.h>
58
59static struct mtx g_disk_done_mtx;
60
61static g_access_t g_disk_access;
62static g_init_t g_disk_init;
63static g_fini_t g_disk_fini;
64
65struct g_class g_disk_class = {
66 .name = "DISK",
67 .init = g_disk_init,
68 .fini = g_disk_fini,
69};
70
71static void
72g_disk_init(struct g_class *mp __unused)
73{
74
75 mtx_init(&g_disk_done_mtx, "g_disk_done", MTX_DEF, 0);
76}
77
78static void
79g_disk_fini(struct g_class *mp __unused)
80{
81
82 mtx_destroy(&g_disk_done_mtx);
83}
84
85DECLARE_GEOM_CLASS(g_disk_class, g_disk);
86
87static void __inline
88g_disk_lock_giant(struct disk *dp)
89{
90 if (dp->d_flags & DISKFLAG_NOGIANT)
91 return;
92 mtx_lock(&Giant);
93}
94
95static void __inline
96g_disk_unlock_giant(struct disk *dp)
97{
98 if (dp->d_flags & DISKFLAG_NOGIANT)
99 return;
100 mtx_unlock(&Giant);
101}
102
103static int
104g_disk_access(struct g_provider *pp, int r, int w, int e)
105{
106 struct disk *dp;
107 int error;
108
109 g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)",
110 pp->name, r, w, e);
111 g_topology_assert();
112 r += pp->acr;
113 w += pp->acw;
114 e += pp->ace;
115 dp = pp->geom->softc;
116 if (dp == NULL)
117 return (ENXIO);
118 error = 0;
119 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
120 if (dp->d_open != NULL) {
121 g_disk_lock_giant(dp);
122 error = dp->d_open(dp);
123 if (error != 0)
124 printf("Opened disk %s -> %d\n",
125 pp->name, error);
126 g_disk_unlock_giant(dp);
127 }
128 pp->mediasize = dp->d_mediasize;
129 pp->sectorsize = dp->d_sectorsize;
130 dp->d_flags |= DISKFLAG_OPEN;
131 if (dp->d_maxsize == 0) {
132 printf("WARNING: Disk drive %s%d has no d_maxsize\n",
133 dp->d_name, dp->d_unit);
134 dp->d_maxsize = DFLTPHYS;
135 }
136 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
137 if (dp->d_close != NULL) {
138 g_disk_lock_giant(dp);
139 error = dp->d_close(dp);
140 if (error != 0)
141 printf("Closed disk %s -> %d\n",
142 pp->name, error);
143 g_disk_unlock_giant(dp);
144 }
145 dp->d_flags &= ~DISKFLAG_OPEN;
146 }
147 return (error);
148}
149
150static void
151g_disk_kerneldump(struct bio *bp, struct disk *dp)
152{
153 int error;
154 struct g_kerneldump *gkd;
155 struct dumperinfo di;
156 struct g_geom *gp;
157
158 gkd = (struct g_kerneldump*)bp->bio_data;
159 gp = bp->bio_to->geom;
160 g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)",
161 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length);
162 if (dp->d_dump == NULL) {
163 g_io_deliver(bp, ENODEV);
164 return;
165 }
166 di.dumper = dp->d_dump;
167 di.priv = dp;
168 di.blocksize = dp->d_sectorsize;
169 di.mediaoffset = gkd->offset;
170 di.mediasize = gkd->length;
171 error = set_dumper(&di);
172 g_io_deliver(bp, error);
173}
174
175static void
176g_disk_done(struct bio *bp)
177{
178 struct bio *bp2;
179 struct disk *dp;
180
181 /* See "notes" for why we need a mutex here */
182 /* XXX: will witness accept a mix of Giant/unGiant drivers here ? */
183 mtx_lock(&g_disk_done_mtx);
184 bp->bio_completed = bp->bio_length - bp->bio_resid;
185
186 bp2 = bp->bio_parent;
187 dp = bp2->bio_to->geom->softc;
188 if (bp2->bio_error == 0)
189 bp2->bio_error = bp->bio_error;
190 bp2->bio_completed += bp->bio_completed;
191 g_destroy_bio(bp);
192 bp2->bio_inbed++;
193 if (bp2->bio_children == bp2->bio_inbed) {
194 bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed;
195 devstat_end_transaction_bio(dp->d_devstat, bp2);
196 g_io_deliver(bp2, bp2->bio_error);
197 }
198 mtx_unlock(&g_disk_done_mtx);
199}
200
201static int
202g_disk_ioctl(struct g_provider *pp, u_long cmd, void * data, struct thread *td)
203{
204 struct g_geom *gp;
205 struct disk *dp;
206 int error;
207
208 gp = pp->geom;
209 dp = gp->softc;
210
211 if (dp->d_ioctl == NULL)
212 return (ENOIOCTL);
213 g_disk_lock_giant(dp);
214 error = dp->d_ioctl(dp, cmd, data, 0, td);
215 g_disk_unlock_giant(dp);
216 return(error);
217}
218
219static void
220g_disk_start(struct bio *bp)
221{
222 struct bio *bp2, *bp3;
223 struct disk *dp;
224 int error;
225 off_t off;
226
227 dp = bp->bio_to->geom->softc;
228 if (dp == NULL)
229 g_io_deliver(bp, ENXIO);
230 error = EJUSTRETURN;
231 switch(bp->bio_cmd) {
232 case BIO_DELETE:
233 if (!(dp->d_flags & DISKFLAG_CANDELETE)) {
234 error = 0;
235 break;
236 }
237 /* fall-through */
238 case BIO_READ:
239 case BIO_WRITE:
240 off = 0;
241 bp3 = NULL;
242 bp2 = g_clone_bio(bp);
243 if (bp2 == NULL) {
244 error = ENOMEM;
245 break;
246 }
247 devstat_start_transaction_bio(dp->d_devstat, bp);
248 do {
249 bp2->bio_offset += off;
250 bp2->bio_length -= off;
251 bp2->bio_data += off;
252 if (bp2->bio_length > dp->d_maxsize) {
253 /*
254 * XXX: If we have a stripesize we should really
255 * use it here.
256 */
257 bp2->bio_length = dp->d_maxsize;
258 off += dp->d_maxsize;
259 /*
260 * To avoid a race, we need to grab the next bio
261 * before we schedule this one. See "notes".
262 */
263 bp3 = g_clone_bio(bp);
264 if (bp3 == NULL)
265 bp->bio_error = ENOMEM;
266 }
267 bp2->bio_done = g_disk_done;
268 bp2->bio_blkno = bp2->bio_offset >> DEV_BSHIFT;
269 bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize;
270 bp2->bio_bcount = bp2->bio_length;
271 bp2->bio_disk = dp;
272 g_disk_lock_giant(dp);
273 dp->d_strategy(bp2);
274 g_disk_unlock_giant(dp);
275 bp2 = bp3;
276 bp3 = NULL;
277 } while (bp2 != NULL);
278 break;
279 case BIO_GETATTR:
280 if (g_handleattr_int(bp, "GEOM::fwsectors", dp->d_fwsectors))
281 break;
282 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads))
283 break;
284 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0))
285 break;
286 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
287 g_disk_kerneldump(bp, dp);
288 else
289 error = ENOIOCTL;
290 break;
291 default:
292 error = EOPNOTSUPP;
293 break;
294 }
295 if (error != EJUSTRETURN)
296 g_io_deliver(bp, error);
297 return;
298}
299
300static void
301g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
302{
303 struct disk *dp;
304
305 dp = gp->softc;
306 if (dp == NULL)
307 return;
308 if (indent == NULL) {
309 sbuf_printf(sb, " hd %u", dp->d_fwheads);
310 sbuf_printf(sb, " sc %u", dp->d_fwsectors);
311 return;
312 }
313 if (pp != NULL) {
314 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n",
315 indent, dp->d_fwheads);
316 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n",
317 indent, dp->d_fwsectors);
318 }
319}
320
321static void
322g_disk_create(void *arg, int flag)
323{
324 struct g_geom *gp;
325 struct g_provider *pp;
326 struct disk *dp;
327
328 if (flag == EV_CANCEL)
329 return;
330 g_topology_assert();
331 dp = arg;
332 gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit);
333 gp->start = g_disk_start;
334 gp->access = g_disk_access;
335 gp->ioctl = g_disk_ioctl;
336 gp->softc = dp;
337 gp->dumpconf = g_disk_dumpconf;
338 pp = g_new_providerf(gp, "%s", gp->name);
339 pp->mediasize = dp->d_mediasize;
340 pp->sectorsize = dp->d_sectorsize;
341 if (dp->d_flags & DISKFLAG_CANDELETE)
342 pp->flags |= G_PF_CANDELETE;
343 pp->stripeoffset = dp->d_stripeoffset;
344 pp->stripesize = dp->d_stripesize;
345 if (bootverbose)
346 printf("GEOM: new disk %s\n", gp->name);
347 dp->d_geom = gp;
348 g_error_provider(pp, 0);
349}
350
351static void
352g_disk_destroy(void *ptr, int flag)
353{
354 struct g_geom *gp;
355
356 g_topology_assert();
357 gp = ptr;
358 gp->softc = NULL;
359 g_wither_geom(gp, ENXIO);
360}
361
362void
363disk_create(int unit, struct disk *dp, int flags, void *unused __unused, void * unused2 __unused)
364{
365
366 dp->d_unit = unit;
367 dp->d_flags = flags;
368 KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy"));
369 KASSERT(dp->d_name != NULL, ("disk_create need d_name"));
370 KASSERT(*dp->d_name != 0, ("disk_create need d_name"));
371 KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long"));
372 if (bootverbose || 1)
373 printf("GEOM: create disk %s%d dp=%p\n",
374 dp->d_name, dp->d_unit, dp);
375 if (dp->d_devstat == NULL)
376 dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit,
377 dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED,
378 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
379 dp->d_geom = NULL;
380 g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL);
381}
382
383void
384disk_destroy(struct disk *dp)
385{
386 struct g_geom *gp;
387
388 if (bootverbose || 1)
389 printf("GEOM: destroy disk %s%d dp=%p\n",
390 dp->d_name, dp->d_unit, dp);
391 g_cancel_event(dp);
392 gp = dp->d_geom;
393 if (gp == NULL)
394 return;
395 gp->softc = NULL;
396 devstat_remove_entry(dp->d_devstat);
397 g_waitfor_event(g_disk_destroy, gp, M_WAITOK, NULL, NULL);
398 dp->d_geom = NULL;
399}
400
401static void
402g_kern_disks(void *p, int flag __unused)
403{
404 struct sbuf *sb;
405 struct g_geom *gp;
406 char *sp;
407
408 sb = p;
409 sp = "";
410 g_topology_assert();
411 LIST_FOREACH(gp, &g_disk_class.geom, geom) {
412 sbuf_printf(sb, "%s%s", sp, gp->name);
413 sp = " ";
414 }
415 sbuf_finish(sb);
416}
417
418static int
419sysctl_disks(SYSCTL_HANDLER_ARGS)
420{
421 int error;
422 struct sbuf *sb;
423
424 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
425 sbuf_clear(sb);
426 g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL);
427 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
428 sbuf_delete(sb);
429 return error;
430}
431
432SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NOLOCK, 0, 0,
433 sysctl_disks, "A", "names of available disks");
434
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_disk.c 120572 2003-09-29 07:44:23Z phk $");
38
39#include "opt_geom.h"
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/fcntl.h>
48#include <sys/malloc.h>
49#include <sys/sysctl.h>
50#include <sys/devicestat.h>
51#include <machine/md_var.h>
52
53#include <sys/lock.h>
54#include <sys/mutex.h>
55#include <geom/geom.h>
56#include <geom/geom_disk.h>
57#include <geom/geom_int.h>
58
59static struct mtx g_disk_done_mtx;
60
61static g_access_t g_disk_access;
62static g_init_t g_disk_init;
63static g_fini_t g_disk_fini;
64
65struct g_class g_disk_class = {
66 .name = "DISK",
67 .init = g_disk_init,
68 .fini = g_disk_fini,
69};
70
71static void
72g_disk_init(struct g_class *mp __unused)
73{
74
75 mtx_init(&g_disk_done_mtx, "g_disk_done", MTX_DEF, 0);
76}
77
78static void
79g_disk_fini(struct g_class *mp __unused)
80{
81
82 mtx_destroy(&g_disk_done_mtx);
83}
84
85DECLARE_GEOM_CLASS(g_disk_class, g_disk);
86
87static void __inline
88g_disk_lock_giant(struct disk *dp)
89{
90 if (dp->d_flags & DISKFLAG_NOGIANT)
91 return;
92 mtx_lock(&Giant);
93}
94
95static void __inline
96g_disk_unlock_giant(struct disk *dp)
97{
98 if (dp->d_flags & DISKFLAG_NOGIANT)
99 return;
100 mtx_unlock(&Giant);
101}
102
103static int
104g_disk_access(struct g_provider *pp, int r, int w, int e)
105{
106 struct disk *dp;
107 int error;
108
109 g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)",
110 pp->name, r, w, e);
111 g_topology_assert();
112 r += pp->acr;
113 w += pp->acw;
114 e += pp->ace;
115 dp = pp->geom->softc;
116 if (dp == NULL)
117 return (ENXIO);
118 error = 0;
119 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
120 if (dp->d_open != NULL) {
121 g_disk_lock_giant(dp);
122 error = dp->d_open(dp);
123 if (error != 0)
124 printf("Opened disk %s -> %d\n",
125 pp->name, error);
126 g_disk_unlock_giant(dp);
127 }
128 pp->mediasize = dp->d_mediasize;
129 pp->sectorsize = dp->d_sectorsize;
130 dp->d_flags |= DISKFLAG_OPEN;
131 if (dp->d_maxsize == 0) {
132 printf("WARNING: Disk drive %s%d has no d_maxsize\n",
133 dp->d_name, dp->d_unit);
134 dp->d_maxsize = DFLTPHYS;
135 }
136 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
137 if (dp->d_close != NULL) {
138 g_disk_lock_giant(dp);
139 error = dp->d_close(dp);
140 if (error != 0)
141 printf("Closed disk %s -> %d\n",
142 pp->name, error);
143 g_disk_unlock_giant(dp);
144 }
145 dp->d_flags &= ~DISKFLAG_OPEN;
146 }
147 return (error);
148}
149
150static void
151g_disk_kerneldump(struct bio *bp, struct disk *dp)
152{
153 int error;
154 struct g_kerneldump *gkd;
155 struct dumperinfo di;
156 struct g_geom *gp;
157
158 gkd = (struct g_kerneldump*)bp->bio_data;
159 gp = bp->bio_to->geom;
160 g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)",
161 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length);
162 if (dp->d_dump == NULL) {
163 g_io_deliver(bp, ENODEV);
164 return;
165 }
166 di.dumper = dp->d_dump;
167 di.priv = dp;
168 di.blocksize = dp->d_sectorsize;
169 di.mediaoffset = gkd->offset;
170 di.mediasize = gkd->length;
171 error = set_dumper(&di);
172 g_io_deliver(bp, error);
173}
174
175static void
176g_disk_done(struct bio *bp)
177{
178 struct bio *bp2;
179 struct disk *dp;
180
181 /* See "notes" for why we need a mutex here */
182 /* XXX: will witness accept a mix of Giant/unGiant drivers here ? */
183 mtx_lock(&g_disk_done_mtx);
184 bp->bio_completed = bp->bio_length - bp->bio_resid;
185
186 bp2 = bp->bio_parent;
187 dp = bp2->bio_to->geom->softc;
188 if (bp2->bio_error == 0)
189 bp2->bio_error = bp->bio_error;
190 bp2->bio_completed += bp->bio_completed;
191 g_destroy_bio(bp);
192 bp2->bio_inbed++;
193 if (bp2->bio_children == bp2->bio_inbed) {
194 bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed;
195 devstat_end_transaction_bio(dp->d_devstat, bp2);
196 g_io_deliver(bp2, bp2->bio_error);
197 }
198 mtx_unlock(&g_disk_done_mtx);
199}
200
201static int
202g_disk_ioctl(struct g_provider *pp, u_long cmd, void * data, struct thread *td)
203{
204 struct g_geom *gp;
205 struct disk *dp;
206 int error;
207
208 gp = pp->geom;
209 dp = gp->softc;
210
211 if (dp->d_ioctl == NULL)
212 return (ENOIOCTL);
213 g_disk_lock_giant(dp);
214 error = dp->d_ioctl(dp, cmd, data, 0, td);
215 g_disk_unlock_giant(dp);
216 return(error);
217}
218
219static void
220g_disk_start(struct bio *bp)
221{
222 struct bio *bp2, *bp3;
223 struct disk *dp;
224 int error;
225 off_t off;
226
227 dp = bp->bio_to->geom->softc;
228 if (dp == NULL)
229 g_io_deliver(bp, ENXIO);
230 error = EJUSTRETURN;
231 switch(bp->bio_cmd) {
232 case BIO_DELETE:
233 if (!(dp->d_flags & DISKFLAG_CANDELETE)) {
234 error = 0;
235 break;
236 }
237 /* fall-through */
238 case BIO_READ:
239 case BIO_WRITE:
240 off = 0;
241 bp3 = NULL;
242 bp2 = g_clone_bio(bp);
243 if (bp2 == NULL) {
244 error = ENOMEM;
245 break;
246 }
247 devstat_start_transaction_bio(dp->d_devstat, bp);
248 do {
249 bp2->bio_offset += off;
250 bp2->bio_length -= off;
251 bp2->bio_data += off;
252 if (bp2->bio_length > dp->d_maxsize) {
253 /*
254 * XXX: If we have a stripesize we should really
255 * use it here.
256 */
257 bp2->bio_length = dp->d_maxsize;
258 off += dp->d_maxsize;
259 /*
260 * To avoid a race, we need to grab the next bio
261 * before we schedule this one. See "notes".
262 */
263 bp3 = g_clone_bio(bp);
264 if (bp3 == NULL)
265 bp->bio_error = ENOMEM;
266 }
267 bp2->bio_done = g_disk_done;
268 bp2->bio_blkno = bp2->bio_offset >> DEV_BSHIFT;
269 bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize;
270 bp2->bio_bcount = bp2->bio_length;
271 bp2->bio_disk = dp;
272 g_disk_lock_giant(dp);
273 dp->d_strategy(bp2);
274 g_disk_unlock_giant(dp);
275 bp2 = bp3;
276 bp3 = NULL;
277 } while (bp2 != NULL);
278 break;
279 case BIO_GETATTR:
280 if (g_handleattr_int(bp, "GEOM::fwsectors", dp->d_fwsectors))
281 break;
282 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads))
283 break;
284 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0))
285 break;
286 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
287 g_disk_kerneldump(bp, dp);
288 else
289 error = ENOIOCTL;
290 break;
291 default:
292 error = EOPNOTSUPP;
293 break;
294 }
295 if (error != EJUSTRETURN)
296 g_io_deliver(bp, error);
297 return;
298}
299
300static void
301g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
302{
303 struct disk *dp;
304
305 dp = gp->softc;
306 if (dp == NULL)
307 return;
308 if (indent == NULL) {
309 sbuf_printf(sb, " hd %u", dp->d_fwheads);
310 sbuf_printf(sb, " sc %u", dp->d_fwsectors);
311 return;
312 }
313 if (pp != NULL) {
314 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n",
315 indent, dp->d_fwheads);
316 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n",
317 indent, dp->d_fwsectors);
318 }
319}
320
321static void
322g_disk_create(void *arg, int flag)
323{
324 struct g_geom *gp;
325 struct g_provider *pp;
326 struct disk *dp;
327
328 if (flag == EV_CANCEL)
329 return;
330 g_topology_assert();
331 dp = arg;
332 gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit);
333 gp->start = g_disk_start;
334 gp->access = g_disk_access;
335 gp->ioctl = g_disk_ioctl;
336 gp->softc = dp;
337 gp->dumpconf = g_disk_dumpconf;
338 pp = g_new_providerf(gp, "%s", gp->name);
339 pp->mediasize = dp->d_mediasize;
340 pp->sectorsize = dp->d_sectorsize;
341 if (dp->d_flags & DISKFLAG_CANDELETE)
342 pp->flags |= G_PF_CANDELETE;
343 pp->stripeoffset = dp->d_stripeoffset;
344 pp->stripesize = dp->d_stripesize;
345 if (bootverbose)
346 printf("GEOM: new disk %s\n", gp->name);
347 dp->d_geom = gp;
348 g_error_provider(pp, 0);
349}
350
351static void
352g_disk_destroy(void *ptr, int flag)
353{
354 struct g_geom *gp;
355
356 g_topology_assert();
357 gp = ptr;
358 gp->softc = NULL;
359 g_wither_geom(gp, ENXIO);
360}
361
362void
363disk_create(int unit, struct disk *dp, int flags, void *unused __unused, void * unused2 __unused)
364{
365
366 dp->d_unit = unit;
367 dp->d_flags = flags;
368 KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy"));
369 KASSERT(dp->d_name != NULL, ("disk_create need d_name"));
370 KASSERT(*dp->d_name != 0, ("disk_create need d_name"));
371 KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long"));
372 if (bootverbose || 1)
373 printf("GEOM: create disk %s%d dp=%p\n",
374 dp->d_name, dp->d_unit, dp);
375 if (dp->d_devstat == NULL)
376 dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit,
377 dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED,
378 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
379 dp->d_geom = NULL;
380 g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL);
381}
382
383void
384disk_destroy(struct disk *dp)
385{
386 struct g_geom *gp;
387
388 if (bootverbose || 1)
389 printf("GEOM: destroy disk %s%d dp=%p\n",
390 dp->d_name, dp->d_unit, dp);
391 g_cancel_event(dp);
392 gp = dp->d_geom;
393 if (gp == NULL)
394 return;
395 gp->softc = NULL;
396 devstat_remove_entry(dp->d_devstat);
397 g_waitfor_event(g_disk_destroy, gp, M_WAITOK, NULL, NULL);
398 dp->d_geom = NULL;
399}
400
401static void
402g_kern_disks(void *p, int flag __unused)
403{
404 struct sbuf *sb;
405 struct g_geom *gp;
406 char *sp;
407
408 sb = p;
409 sp = "";
410 g_topology_assert();
411 LIST_FOREACH(gp, &g_disk_class.geom, geom) {
412 sbuf_printf(sb, "%s%s", sp, gp->name);
413 sp = " ";
414 }
415 sbuf_finish(sb);
416}
417
418static int
419sysctl_disks(SYSCTL_HANDLER_ARGS)
420{
421 int error;
422 struct sbuf *sb;
423
424 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
425 sbuf_clear(sb);
426 g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL);
427 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
428 sbuf_delete(sb);
429 return error;
430}
431
432SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NOLOCK, 0, 0,
433 sysctl_disks, "A", "names of available disks");
434