xref: /freebsd/sys/geom/geom_disk.c (revision 729362425c09cf6b362366aabc6fb547eee8035a)
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/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>
57 
58 static struct mtx g_disk_done_mtx;
59 
60 static g_access_t g_disk_access;
61 
62 struct g_class g_disk_class = {
63 	.name = "DISK",
64 	G_CLASS_INITIALIZER
65 };
66 
67 static void
68 g_disk_init(void)
69 {
70 	mtx_unlock(&Giant);
71 	g_add_class(&g_disk_class);
72 	mtx_init(&g_disk_done_mtx, "g_disk_done", MTX_DEF, 0);
73 	mtx_lock(&Giant);
74 }
75 
76 DECLARE_GEOM_CLASS_INIT(g_disk_class, g_disk, g_disk_init);
77 
78 static void __inline
79 g_disk_lock_giant(struct disk *dp)
80 {
81 	if (dp->d_flags & DISKFLAG_NOGIANT)
82 		return;
83 	mtx_lock(&Giant);
84 }
85 
86 static void __inline
87 g_disk_unlock_giant(struct disk *dp)
88 {
89 	if (dp->d_flags & DISKFLAG_NOGIANT)
90 		return;
91 	mtx_unlock(&Giant);
92 }
93 
94 static int
95 g_disk_access(struct g_provider *pp, int r, int w, int e)
96 {
97 	struct disk *dp;
98 	int error;
99 
100 	g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)",
101 	    pp->name, r, w, e);
102 	g_topology_assert();
103 	r += pp->acr;
104 	w += pp->acw;
105 	e += pp->ace;
106 	dp = pp->geom->softc;
107 	error = 0;
108 	if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
109 		if (dp->d_open != NULL) {
110 			g_disk_lock_giant(dp);
111 			error = dp->d_open(dp);
112 			if (error != 0)
113 				printf("Opened disk %s -> %d\n",
114 				    pp->name, error);
115 			g_disk_unlock_giant(dp);
116 		}
117 		pp->mediasize = dp->d_mediasize;
118 		pp->sectorsize = dp->d_sectorsize;
119 		dp->d_flags |= DISKFLAG_OPEN;
120 		if (dp->d_maxsize == 0) {
121 			printf("WARNING: Disk drive %s%d has no d_maxsize\n",
122 			    dp->d_name, dp->d_unit);
123 			dp->d_maxsize = DFLTPHYS;
124 		}
125 	} else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
126 		if (dp->d_close != NULL) {
127 			g_disk_lock_giant(dp);
128 			error = dp->d_close(dp);
129 			if (error != 0)
130 				printf("Closed disk %s -> %d\n",
131 				    pp->name, error);
132 			g_disk_unlock_giant(dp);
133 		}
134 		dp->d_flags &= ~DISKFLAG_OPEN;
135 	}
136 	return (error);
137 }
138 
139 static void
140 g_disk_kerneldump(struct bio *bp, struct disk *dp)
141 {
142 	int error;
143 	struct g_kerneldump *gkd;
144 	struct dumperinfo di;
145 	struct g_geom *gp;
146 
147 	gkd = (struct g_kerneldump*)bp->bio_data;
148 	gp = bp->bio_to->geom;
149 	g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)",
150 		gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length);
151 	di.dumper = dp->d_dump;
152 	di.priv = dp;
153 	di.blocksize = dp->d_sectorsize;
154 	di.mediaoffset = gkd->offset;
155 	di.mediasize = gkd->length;
156 	error = set_dumper(&di);
157 	g_io_deliver(bp, error);
158 }
159 
160 static void
161 g_disk_done(struct bio *bp)
162 {
163 	struct bio *bp2;
164 	struct disk *dp;
165 
166 	/* See "notes" for why we need a mutex here */
167 	/* XXX: will witness accept a mix of Giant/unGiant drivers here ? */
168 	mtx_lock(&g_disk_done_mtx);
169 	bp->bio_completed = bp->bio_length - bp->bio_resid;
170 
171 	bp2 = bp->bio_parent;
172 	dp = bp2->bio_to->geom->softc;
173 	if (bp2->bio_error == 0)
174 		bp2->bio_error = bp->bio_error;
175 	bp2->bio_completed += bp->bio_completed;
176 	g_destroy_bio(bp);
177 	bp2->bio_inbed++;
178 	if (bp2->bio_children == bp2->bio_inbed) {
179 		bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed;
180 		devstat_end_transaction_bio(dp->d_devstat, bp2);
181 		g_io_deliver(bp2, bp2->bio_error);
182 	}
183 	mtx_unlock(&g_disk_done_mtx);
184 }
185 
186 static void
187 g_disk_start(struct bio *bp)
188 {
189 	struct bio *bp2, *bp3;
190 	struct disk *dp;
191 	struct g_ioctl *gio;
192 	int error;
193 	off_t off;
194 
195 	dp = bp->bio_to->geom->softc;
196 	error = EJUSTRETURN;
197 	switch(bp->bio_cmd) {
198 	case BIO_DELETE:
199 		if (!(dp->d_flags & DISKFLAG_CANDELETE)) {
200 			error = 0;
201 			break;
202 		}
203 		/* fall-through */
204 	case BIO_READ:
205 	case BIO_WRITE:
206 		off = 0;
207 		bp3 = NULL;
208 		bp2 = g_clone_bio(bp);
209 		if (bp2 == NULL) {
210 			error = ENOMEM;
211 			break;
212 		}
213 		devstat_start_transaction_bio(dp->d_devstat, bp);
214 		do {
215 			bp2->bio_offset += off;
216 			bp2->bio_length -= off;
217 			bp2->bio_data += off;
218 			if (bp2->bio_length > dp->d_maxsize) {
219 				/*
220 				 * XXX: If we have a stripesize we should really
221 				 * use it here.
222 				 */
223 				bp2->bio_length = dp->d_maxsize;
224 				off += dp->d_maxsize;
225 				/*
226 				 * To avoid a race, we need to grab the next bio
227 				 * before we schedule this one.  See "notes".
228 				 */
229 				bp3 = g_clone_bio(bp);
230 				if (bp3 == NULL)
231 					bp->bio_error = ENOMEM;
232 			}
233 			bp2->bio_done = g_disk_done;
234 			bp2->bio_blkno = bp2->bio_offset >> DEV_BSHIFT;
235 			bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize;
236 			bp2->bio_bcount = bp2->bio_length;
237 			bp2->bio_disk = dp;
238 			g_disk_lock_giant(dp);
239 			dp->d_strategy(bp2);
240 			g_disk_unlock_giant(dp);
241 			bp2 = bp3;
242 			bp3 = NULL;
243 		} while (bp2 != NULL);
244 		break;
245 	case BIO_GETATTR:
246 		if (g_handleattr_int(bp, "GEOM::fwsectors", dp->d_fwsectors))
247 			break;
248 		else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads))
249 			break;
250 		else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0))
251 			break;
252 		else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
253 			g_disk_kerneldump(bp, dp);
254 		else if ((g_debugflags & G_F_DISKIOCTL) &&
255 		    (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 
277 static void
278 g_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 
296 static void
297 g_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 
325 void
326 disk_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 
341 void
342 disk_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 
353 static void
354 g_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 
371 static int
372 sysctl_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 	error = g_call_me(g_kern_disks, sb);
380 	while (!error && !sbuf_done(sb)) {
381 		tsleep(sb, PZERO, "kern.disks", hz);
382 	}
383 	if (!error)
384 		error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
385 	sbuf_delete(sb);
386 	return error;
387 }
388 
389 SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NOLOCK, 0, 0,
390     sysctl_disks, "A", "names of available disks");
391 
392