xref: /freebsd/sys/geom/geom_io.c (revision a3e8fd0b7f663db7eafff527d5c3ca3bcfa8a537)
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 
39 #include <sys/param.h>
40 #include <sys/stdint.h>
41 #ifndef _KERNEL
42 #include <stdio.h>
43 #include <string.h>
44 #include <stdlib.h>
45 #include <signal.h>
46 #include <err.h>
47 #include <sched.h>
48 #else
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.h>
52 #include <sys/bio.h>
53 #endif
54 
55 #include <sys/errno.h>
56 #include <geom/geom.h>
57 #include <geom/geom_int.h>
58 
59 static struct g_bioq g_bio_run_down;
60 static struct g_bioq g_bio_run_up;
61 static struct g_bioq g_bio_idle;
62 
63 #include <machine/atomic.h>
64 
65 static void
66 g_bioq_lock(struct g_bioq *bq)
67 {
68 
69 	mtx_lock(&bq->bio_queue_lock);
70 }
71 
72 static void
73 g_bioq_unlock(struct g_bioq *bq)
74 {
75 
76 	mtx_unlock(&bq->bio_queue_lock);
77 }
78 
79 #if 0
80 static void
81 g_bioq_destroy(struct g_bioq *bq)
82 {
83 
84 	mtx_destroy(&bq->bio_queue_lock);
85 }
86 #endif
87 
88 static void
89 g_bioq_init(struct g_bioq *bq)
90 {
91 
92 	TAILQ_INIT(&bq->bio_queue);
93 	mtx_init(&bq->bio_queue_lock, "bio queue", NULL, MTX_DEF);
94 }
95 
96 static struct bio *
97 g_bioq_first(struct g_bioq *bq)
98 {
99 	struct bio *bp;
100 
101 	g_bioq_lock(bq);
102 	bp = TAILQ_FIRST(&bq->bio_queue);
103 	if (bp != NULL) {
104 		TAILQ_REMOVE(&bq->bio_queue, bp, bio_queue);
105 		bq->bio_queue_length--;
106 	}
107 	g_bioq_unlock(bq);
108 	return (bp);
109 }
110 
111 static void
112 g_bioq_enqueue_tail(struct bio *bp, struct g_bioq *rq)
113 {
114 
115 	g_bioq_lock(rq);
116 	TAILQ_INSERT_TAIL(&rq->bio_queue, bp, bio_queue);
117 	rq->bio_queue_length++;
118 	g_bioq_unlock(rq);
119 }
120 
121 struct bio *
122 g_new_bio(void)
123 {
124 	struct bio *bp;
125 
126 	bp = g_bioq_first(&g_bio_idle);
127 	if (bp == NULL)
128 		bp = g_malloc(sizeof *bp, M_NOWAIT | M_ZERO);
129 	/* g_trace(G_T_BIO, "g_new_bio() = %p", bp); */
130 	return (bp);
131 }
132 
133 void
134 g_destroy_bio(struct bio *bp)
135 {
136 
137 	/* g_trace(G_T_BIO, "g_destroy_bio(%p)", bp); */
138 	bzero(bp, sizeof *bp);
139 	g_bioq_enqueue_tail(bp, &g_bio_idle);
140 }
141 
142 struct bio *
143 g_clone_bio(struct bio *bp)
144 {
145 	struct bio *bp2;
146 
147 	bp2 = g_new_bio();
148 	if (bp2 != NULL) {
149 		bp2->bio_linkage = bp;
150 		bp2->bio_cmd = bp->bio_cmd;
151 		bp2->bio_length = bp->bio_length;
152 		bp2->bio_offset = bp->bio_offset;
153 		bp2->bio_data = bp->bio_data;
154 		bp2->bio_attribute = bp->bio_attribute;
155 		bp->bio_children++;	/* XXX: atomic ? */
156 	}
157 	/* g_trace(G_T_BIO, "g_clone_bio(%p) = %p", bp, bp2); */
158 	return(bp2);
159 }
160 
161 void
162 g_io_init()
163 {
164 
165 	g_bioq_init(&g_bio_run_down);
166 	g_bioq_init(&g_bio_run_up);
167 	g_bioq_init(&g_bio_idle);
168 }
169 
170 int
171 g_io_setattr(const char *attr, struct g_consumer *cp, int len, void *ptr)
172 {
173 	struct bio *bp;
174 	int error;
175 
176 	g_trace(G_T_BIO, "bio_setattr(%s)", attr);
177 	bp = g_new_bio();
178 	bp->bio_cmd = BIO_SETATTR;
179 	bp->bio_done = NULL;
180 	bp->bio_attribute = attr;
181 	bp->bio_length = len;
182 	bp->bio_data = ptr;
183 	g_io_request(bp, cp);
184 	error = biowait(bp, "gsetattr");
185 	g_destroy_bio(bp);
186 	return (error);
187 }
188 
189 
190 int
191 g_io_getattr(const char *attr, struct g_consumer *cp, int *len, void *ptr)
192 {
193 	struct bio *bp;
194 	int error;
195 
196 	g_trace(G_T_BIO, "bio_getattr(%s)", attr);
197 	bp = g_new_bio();
198 	bp->bio_cmd = BIO_GETATTR;
199 	bp->bio_done = NULL;
200 	bp->bio_attribute = attr;
201 	bp->bio_length = *len;
202 	bp->bio_data = ptr;
203 	g_io_request(bp, cp);
204 	error = biowait(bp, "ggetattr");
205 	*len = bp->bio_completed;
206 	g_destroy_bio(bp);
207 	return (error);
208 }
209 
210 void
211 g_io_request(struct bio *bp, struct g_consumer *cp)
212 {
213 	int error;
214 	off_t excess;
215 
216 	KASSERT(cp != NULL, ("NULL cp in g_io_request"));
217 	KASSERT(bp != NULL, ("NULL bp in g_io_request"));
218 	KASSERT(bp->bio_data != NULL, ("NULL bp->data in g_io_request"));
219 	error = 0;
220 	bp->bio_from = cp;
221 	bp->bio_to = cp->provider;
222 	bp->bio_error = 0;
223 	bp->bio_completed = 0;
224 
225 	/* begin_stats(&bp->stats); */
226 
227 	atomic_add_int(&cp->biocount, 1);
228 	/* Fail on unattached consumers */
229 	if (bp->bio_to == NULL) {
230 		g_io_deliver(bp, ENXIO);
231 		return;
232 	}
233 	/* Fail if access doesn't allow operation */
234 	switch(bp->bio_cmd) {
235 	case BIO_READ:
236 	case BIO_GETATTR:
237 		if (cp->acr == 0) {
238 			g_io_deliver(bp, EPERM);
239 			return;
240 		}
241 		break;
242 	case BIO_WRITE:
243 	case BIO_DELETE:
244 		if (cp->acw == 0) {
245 			g_io_deliver(bp, EPERM);
246 			return;
247 		}
248 		break;
249 	case BIO_SETATTR:
250 		/* XXX: Should ideally check for (cp->ace == 0) */
251 		if ((cp->acw == 0)) {
252 #ifdef DIAGNOSTIC
253 			printf("setattr on %s mode (%d,%d,%d)\n",
254 				cp->provider->name,
255 				cp->acr, cp->acw, cp->ace);
256 #endif
257 			g_io_deliver(bp, EPERM);
258 			return;
259 		}
260 		break;
261 	default:
262 		g_io_deliver(bp, EPERM);
263 		return;
264 	}
265 	/* if provider is marked for error, don't disturb. */
266 	if (bp->bio_to->error) {
267 		g_io_deliver(bp, bp->bio_to->error);
268 		return;
269 	}
270 	switch(bp->bio_cmd) {
271 	case BIO_READ:
272 	case BIO_WRITE:
273 	case BIO_DELETE:
274 		/* Reject requests past the end of media. */
275 		if (bp->bio_offset > bp->bio_to->mediasize) {
276 			g_io_deliver(bp, EIO);
277 			return;
278 		}
279 		/* Truncate requests to the end of providers media. */
280 		excess = bp->bio_offset + bp->bio_length;
281 		if (excess > bp->bio_to->mediasize) {
282 			excess -= bp->bio_to->mediasize;
283 			bp->bio_length -= excess;
284 		}
285 		/* Deliver zero length transfers right here. */
286 		if (bp->bio_length == 0) {
287 			g_io_deliver(bp, 0);
288 			return;
289 		}
290 		break;
291 	default:
292 		break;
293 	}
294 	/* Pass it on down. */
295 	g_trace(G_T_BIO, "bio_request(%p) from %p(%s) to %p(%s) cmd %d",
296 	    bp, bp->bio_from, bp->bio_from->geom->name,
297 	    bp->bio_to, bp->bio_to->name, bp->bio_cmd);
298 	g_bioq_enqueue_tail(bp, &g_bio_run_down);
299 	wakeup(&g_wait_down);
300 }
301 
302 void
303 g_io_deliver(struct bio *bp, int error)
304 {
305 
306 	KASSERT(bp != NULL, ("NULL bp in g_io_deliver"));
307 	KASSERT(bp->bio_from != NULL, ("NULL bio_from in g_io_deliver"));
308 	KASSERT(bp->bio_from->geom != NULL, ("NULL bio_from->geom in g_io_deliver"));
309 	KASSERT(bp->bio_to != NULL, ("NULL bio_to in g_io_deliver"));
310 	g_trace(G_T_BIO,
311 	    "g_io_deliver(%p) from %p(%s) to %p(%s) cmd %d error %d off %jd len %jd",
312 	    bp, bp->bio_from, bp->bio_from->geom->name,
313 	    bp->bio_to, bp->bio_to->name, bp->bio_cmd, error,
314 	    (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length);
315 	/* finish_stats(&bp->stats); */
316 
317 	bp->bio_error = error;
318 
319 	g_bioq_enqueue_tail(bp, &g_bio_run_up);
320 
321 	wakeup(&g_wait_up);
322 }
323 
324 void
325 g_io_schedule_down(struct thread *tp __unused)
326 {
327 	struct bio *bp;
328 
329 	for(;;) {
330 		bp = g_bioq_first(&g_bio_run_down);
331 		if (bp == NULL)
332 			break;
333 		bp->bio_to->geom->start(bp);
334 	}
335 }
336 
337 void
338 g_io_schedule_up(struct thread *tp __unused)
339 {
340 	struct bio *bp;
341 	struct g_consumer *cp;
342 
343 	for(;;) {
344 		bp = g_bioq_first(&g_bio_run_up);
345 		if (bp == NULL)
346 			break;
347 
348 		cp = bp->bio_from;
349 
350 		atomic_add_int(&cp->biocount, -1);
351 		biodone(bp);
352 	}
353 }
354 
355 void *
356 g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error)
357 {
358 	struct bio *bp;
359 	void *ptr;
360 	int errorc;
361 
362 	bp = g_new_bio();
363 	bp->bio_cmd = BIO_READ;
364 	bp->bio_done = NULL;
365 	bp->bio_offset = offset;
366 	bp->bio_length = length;
367 	ptr = g_malloc(length, M_WAITOK);
368 	bp->bio_data = ptr;
369 	g_io_request(bp, cp);
370 	errorc = biowait(bp, "gread");
371 	if (error != NULL)
372 		*error = errorc;
373 	g_destroy_bio(bp);
374 	if (errorc) {
375 		g_free(ptr);
376 		ptr = NULL;
377 	}
378 	return (ptr);
379 }
380 
381 int
382 g_write_data(struct g_consumer *cp, off_t offset, void *ptr, off_t length)
383 {
384 	struct bio *bp;
385 	int error;
386 
387 	bp = g_new_bio();
388 	bp->bio_cmd = BIO_WRITE;
389 	bp->bio_done = NULL;
390 	bp->bio_offset = offset;
391 	bp->bio_length = length;
392 	bp->bio_data = ptr;
393 	g_io_request(bp, cp);
394 	error = biowait(bp, "gwrite");
395 	g_destroy_bio(bp);
396 	return (error);
397 }
398