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