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