xref: /freebsd/sys/geom/geom_disk.c (revision f9218d3d4fd34f082473b3a021c6d4d109fb47cf)
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$
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 #include <sys/lock.h>
54 #include <sys/mutex.h>
55 #include <geom/geom.h>
56 
57 static struct mtx g_disk_done_mtx;
58 
59 static g_access_t g_disk_access;
60 
61 struct g_class g_disk_class = {
62 	"DISK",
63 	NULL,
64 	NULL,
65 	G_CLASS_INITIALIZER
66 };
67 
68 static void
69 g_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 
77 DECLARE_GEOM_CLASS_INIT(g_disk_class, g_disk, g_disk_init);
78 
79 static void __inline
80 g_disk_lock_giant(struct disk *dp)
81 {
82 	if (dp->d_flags & DISKFLAG_NOGIANT)
83 		return;
84 	mtx_lock(&Giant);
85 }
86 
87 static void __inline
88 g_disk_unlock_giant(struct disk *dp)
89 {
90 	if (dp->d_flags & DISKFLAG_NOGIANT)
91 		return;
92 	mtx_unlock(&Giant);
93 }
94 
95 static int
96 g_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 
140 static void
141 g_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 
161 static void
162 g_disk_done(struct bio *bp)
163 {
164 
165 	/* See "notes" for why we need a mutex here */
166 	/* XXX: will witness accept a mix of Giant/unGiant drivers here ? */
167 	mtx_lock(&g_disk_done_mtx);
168 	bp->bio_completed = bp->bio_length - bp->bio_resid;
169 	g_std_done(bp);
170 	mtx_unlock(&g_disk_done_mtx);
171 }
172 
173 static void
174 g_disk_start(struct bio *bp)
175 {
176 	struct bio *bp2, *bp3;
177 	struct disk *dp;
178 	struct g_ioctl *gio;
179 	int error;
180 	off_t off;
181 
182 	dp = bp->bio_to->geom->softc;
183 	error = EJUSTRETURN;
184 	switch(bp->bio_cmd) {
185 	case BIO_DELETE:
186 		if (!(dp->d_flags & DISKFLAG_CANDELETE)) {
187 			error = 0;
188 			break;
189 		}
190 		/* fall-through */
191 	case BIO_READ:
192 	case BIO_WRITE:
193 		off = 0;
194 		bp3 = NULL;
195 		bp2 = g_clone_bio(bp);
196 		if (bp2 == NULL) {
197 			error = ENOMEM;
198 			break;
199 		}
200 		do {
201 			bp2->bio_offset += off;
202 			bp2->bio_length -= off;
203 			bp2->bio_data += off;
204 			if (bp2->bio_length > dp->d_maxsize) {
205 				/*
206 				 * XXX: If we have a stripesize we should really
207 				 * use it here.
208 				 */
209 				bp2->bio_length = dp->d_maxsize;
210 				off += dp->d_maxsize;
211 				/*
212 				 * To avoid a race, we need to grab the next bio
213 				 * before we schedule this one.  See "notes".
214 				 */
215 				bp3 = g_clone_bio(bp);
216 				if (bp3 == NULL)
217 					bp->bio_error = ENOMEM;
218 			}
219 			bp2->bio_done = g_disk_done;
220 			bp2->bio_blkno = bp2->bio_offset >> DEV_BSHIFT;
221 			bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize;
222 			bp2->bio_bcount = bp2->bio_length;
223 			bp2->bio_disk = dp;
224 			g_disk_lock_giant(dp);
225 			dp->d_strategy(bp2);
226 			g_disk_unlock_giant(dp);
227 			bp2 = bp3;
228 			bp3 = NULL;
229 		} while (bp2 != NULL);
230 		break;
231 	case BIO_GETATTR:
232 		if (g_handleattr_int(bp, "GEOM::fwsectors", dp->d_fwsectors))
233 			break;
234 		else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads))
235 			break;
236 		else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0))
237 			break;
238 		else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
239 			g_disk_kerneldump(bp, dp);
240 		else if ((dp->d_ioctl != NULL) &&
241 		    !strcmp(bp->bio_attribute, "GEOM::ioctl") &&
242 		    bp->bio_length == sizeof *gio) {
243 			gio = (struct g_ioctl *)bp->bio_data;
244 			gio->dev =  dp;
245 			gio->func = (d_ioctl_t *)(dp->d_ioctl);
246 			error = EDIRIOCTL;
247 		} else
248 			error = ENOIOCTL;
249 		break;
250 	case BIO_SETATTR:
251 		error = ENOIOCTL;
252 		break;
253 	default:
254 		error = EOPNOTSUPP;
255 		break;
256 	}
257 	if (error != EJUSTRETURN)
258 		g_io_deliver(bp, error);
259 	return;
260 }
261 
262 static void
263 g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
264 {
265 	struct disk *dp;
266 
267 	dp = gp->softc;
268 	if (indent == NULL) {
269 		sbuf_printf(sb, " hd %u", dp->d_fwheads);
270 		sbuf_printf(sb, " sc %u", dp->d_fwsectors);
271 		return;
272 	}
273 	if (pp != NULL) {
274 		sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n",
275 		    indent, dp->d_fwheads);
276 		sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n",
277 		    indent, dp->d_fwsectors);
278 	}
279 }
280 
281 static void
282 g_disk_create(void *arg)
283 {
284 	struct g_geom *gp;
285 	struct g_provider *pp;
286 	struct disk *dp;
287 
288 	g_topology_assert();
289 	dp = arg;
290 	gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit);
291 	gp->start = g_disk_start;
292 	gp->access = g_disk_access;
293 	gp->softc = dp;
294 	gp->dumpconf = g_disk_dumpconf;
295 	dp->d_geom = gp;
296 	pp = g_new_providerf(gp, "%s", gp->name);
297 	pp->mediasize = dp->d_mediasize;
298 	pp->sectorsize = dp->d_sectorsize;
299 	if (dp->d_flags & DISKFLAG_CANDELETE)
300 		pp->flags |= G_PF_CANDELETE;
301 	pp->stripeoffset = dp->d_stripeoffset;
302 	pp->stripesize = dp->d_stripesize;
303 	g_error_provider(pp, 0);
304 	if (bootverbose)
305 		printf("GEOM: new disk %s\n", gp->name);
306 }
307 
308 
309 
310 void
311 disk_create(int unit, struct disk *dp, int flags, void *unused __unused, void * unused2 __unused)
312 {
313 
314 	dp->d_unit = unit;
315 	dp->d_flags = flags;
316 	KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy"));
317 	KASSERT(dp->d_name != NULL, ("disk_create need d_name"));
318 	KASSERT(*dp->d_name != 0, ("disk_create need d_name"));
319 	KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long"));
320 	g_call_me(g_disk_create, dp);
321 }
322 
323 void
324 disk_destroy(struct disk *dp)
325 {
326 	struct g_geom *gp;
327 
328 	gp = dp->d_geom;
329 	gp->flags |= G_GEOM_WITHER;
330 	gp->softc = NULL;
331 	g_orphan_provider(LIST_FIRST(&gp->provider), ENXIO);
332 }
333 
334 static void
335 g_kern_disks(void *p)
336 {
337 	struct sbuf *sb;
338 	struct g_geom *gp;
339 	char *sp;
340 
341 	sb = p;
342 	sp = "";
343 	g_topology_assert();
344 	LIST_FOREACH(gp, &g_disk_class.geom, geom) {
345 		sbuf_printf(sb, "%s%s", sp, gp->name);
346 		sp = " ";
347 	}
348 	sbuf_finish(sb);
349 	wakeup(sb);
350 }
351 
352 static int
353 sysctl_disks(SYSCTL_HANDLER_ARGS)
354 {
355 	int error;
356 	struct sbuf *sb;
357 
358 	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
359 	sbuf_clear(sb);
360 	g_call_me(g_kern_disks, sb);
361 	do {
362 		tsleep(sb, PZERO, "kern.disks", hz);
363 	} while(!sbuf_done(sb));
364 	error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
365 	sbuf_delete(sb);
366 	return error;
367 }
368 
369 SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NOLOCK, 0, 0,
370     sysctl_disks, "A", "names of available disks");
371 
372