FreeBSD/Linux Kernel Cross Reference
sys/geom/geom_io.c
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: src/sys/geom/geom_io.c,v 1.57.2.4 2005/01/30 03:02:10 rwatson Exp $");
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/bio.h>
44 #include <sys/ktr.h>
45
46 #include <sys/errno.h>
47 #include <geom/geom.h>
48 #include <geom/geom_int.h>
49 #include <sys/devicestat.h>
50
51 #include <vm/uma.h>
52
53 static struct g_bioq g_bio_run_down;
54 static struct g_bioq g_bio_run_up;
55 static struct g_bioq g_bio_run_task;
56
57 static u_int pace;
58 static uma_zone_t biozone;
59
60 #include <machine/atomic.h>
61
62 static void
63 g_bioq_lock(struct g_bioq *bq)
64 {
65
66 mtx_lock(&bq->bio_queue_lock);
67 }
68
69 static void
70 g_bioq_unlock(struct g_bioq *bq)
71 {
72
73 mtx_unlock(&bq->bio_queue_lock);
74 }
75
76 #if 0
77 static void
78 g_bioq_destroy(struct g_bioq *bq)
79 {
80
81 mtx_destroy(&bq->bio_queue_lock);
82 }
83 #endif
84
85 static void
86 g_bioq_init(struct g_bioq *bq)
87 {
88
89 TAILQ_INIT(&bq->bio_queue);
90 mtx_init(&bq->bio_queue_lock, "bio queue", NULL, MTX_DEF);
91 }
92
93 static struct bio *
94 g_bioq_first(struct g_bioq *bq)
95 {
96 struct bio *bp;
97
98 bp = TAILQ_FIRST(&bq->bio_queue);
99 if (bp != NULL) {
100 KASSERT((bp->bio_flags & BIO_ONQUEUE),
101 ("Bio not on queue bp=%p target %p", bp, bq));
102 bp->bio_flags &= ~BIO_ONQUEUE;
103 TAILQ_REMOVE(&bq->bio_queue, bp, bio_queue);
104 bq->bio_queue_length--;
105 }
106 return (bp);
107 }
108
109 struct bio *
110 g_new_bio(void)
111 {
112 struct bio *bp;
113
114 bp = uma_zalloc(biozone, M_NOWAIT | M_ZERO);
115 return (bp);
116 }
117
118 struct bio *
119 g_alloc_bio(void)
120 {
121 struct bio *bp;
122
123 bp = uma_zalloc(biozone, M_WAITOK | M_ZERO);
124 return (bp);
125 }
126
127 void
128 g_destroy_bio(struct bio *bp)
129 {
130
131 uma_zfree(biozone, bp);
132 }
133
134 struct bio *
135 g_clone_bio(struct bio *bp)
136 {
137 struct bio *bp2;
138
139 bp2 = uma_zalloc(biozone, M_NOWAIT | M_ZERO);
140 if (bp2 != NULL) {
141 bp2->bio_parent = bp;
142 bp2->bio_cmd = bp->bio_cmd;
143 bp2->bio_length = bp->bio_length;
144 bp2->bio_offset = bp->bio_offset;
145 bp2->bio_data = bp->bio_data;
146 bp2->bio_attribute = bp->bio_attribute;
147 bp->bio_children++;
148 }
149 return(bp2);
150 }
151
152 void
153 g_io_init()
154 {
155
156 g_bioq_init(&g_bio_run_down);
157 g_bioq_init(&g_bio_run_up);
158 g_bioq_init(&g_bio_run_task);
159 biozone = uma_zcreate("g_bio", sizeof (struct bio),
160 NULL, NULL,
161 NULL, NULL,
162 0, 0);
163 }
164
165 int
166 g_io_getattr(const char *attr, struct g_consumer *cp, int *len, void *ptr)
167 {
168 struct bio *bp;
169 int error;
170
171 g_trace(G_T_BIO, "bio_getattr(%s)", attr);
172 bp = g_alloc_bio();
173 bp->bio_cmd = BIO_GETATTR;
174 bp->bio_done = NULL;
175 bp->bio_attribute = attr;
176 bp->bio_length = *len;
177 bp->bio_data = ptr;
178 g_io_request(bp, cp);
179 error = biowait(bp, "ggetattr");
180 *len = bp->bio_completed;
181 g_destroy_bio(bp);
182 return (error);
183 }
184
185 static int
186 g_io_check(struct bio *bp)
187 {
188 struct g_consumer *cp;
189 struct g_provider *pp;
190
191 cp = bp->bio_from;
192 pp = bp->bio_to;
193
194 /* Fail if access counters dont allow the operation */
195 switch(bp->bio_cmd) {
196 case BIO_READ:
197 case BIO_GETATTR:
198 if (cp->acr == 0)
199 return (EPERM);
200 break;
201 case BIO_WRITE:
202 case BIO_DELETE:
203 if (cp->acw == 0)
204 return (EPERM);
205 break;
206 default:
207 return (EPERM);
208 }
209 /* if provider is marked for error, don't disturb. */
210 if (pp->error)
211 return (pp->error);
212
213 switch(bp->bio_cmd) {
214 case BIO_READ:
215 case BIO_WRITE:
216 case BIO_DELETE:
217 /* Zero sectorsize is a probably lack of media */
218 if (pp->sectorsize == 0)
219 return (ENXIO);
220 /* Reject I/O not on sector boundary */
221 if (bp->bio_offset % pp->sectorsize)
222 return (EINVAL);
223 /* Reject I/O not integral sector long */
224 if (bp->bio_length % pp->sectorsize)
225 return (EINVAL);
226 /* Reject requests before or past the end of media. */
227 if (bp->bio_offset < 0)
228 return (EIO);
229 if (bp->bio_offset > pp->mediasize)
230 return (EIO);
231 break;
232 default:
233 break;
234 }
235 return (0);
236 }
237
238 void
239 g_io_request(struct bio *bp, struct g_consumer *cp)
240 {
241 struct g_provider *pp;
242
243 KASSERT(cp != NULL, ("NULL cp in g_io_request"));
244 KASSERT(bp != NULL, ("NULL bp in g_io_request"));
245 KASSERT(bp->bio_data != NULL, ("NULL bp->data in g_io_request"));
246 pp = cp->provider;
247 KASSERT(pp != NULL, ("consumer not attached in g_io_request"));
248
249 #ifdef DIAGNOSTIC
250 if (bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_DELETE)) {
251 KASSERT(bp->bio_offset % cp->provider->sectorsize == 0,
252 ("wrong offset %jd for sectorsize %u",
253 bp->bio_offset, cp->provider->sectorsize));
254 KASSERT(bp->bio_length % cp->provider->sectorsize == 0,
255 ("wrong length %jd for sectorsize %u",
256 bp->bio_length, cp->provider->sectorsize));
257 }
258 #endif /* DIAGNOSTIC */
259
260 g_trace(G_T_BIO, "bio_request(%p) from %p(%s) to %p(%s) cmd %d",
261 bp, cp, cp->geom->name, pp, pp->name, bp->bio_cmd);
262
263 bp->bio_from = cp;
264 bp->bio_to = pp;
265 bp->bio_error = 0;
266 bp->bio_completed = 0;
267
268 KASSERT(!(bp->bio_flags & BIO_ONQUEUE),
269 ("Bio already on queue bp=%p", bp));
270 bp->bio_flags |= BIO_ONQUEUE;
271
272 binuptime(&bp->bio_t0);
273 if (g_collectstats & 4)
274 g_bioq_lock(&g_bio_run_down);
275 if (g_collectstats & 1)
276 devstat_start_transaction(pp->stat, &bp->bio_t0);
277 if (g_collectstats & 2)
278 devstat_start_transaction(cp->stat, &bp->bio_t0);
279
280 if (!(g_collectstats & 4))
281 g_bioq_lock(&g_bio_run_down);
282 pp->nstart++;
283 cp->nstart++;
284 TAILQ_INSERT_TAIL(&g_bio_run_down.bio_queue, bp, bio_queue);
285 g_bio_run_down.bio_queue_length++;
286 g_bioq_unlock(&g_bio_run_down);
287
288 /* Pass it on down. */
289 wakeup(&g_wait_down);
290 }
291
292 void
293 g_io_deliver(struct bio *bp, int error)
294 {
295 struct g_consumer *cp;
296 struct g_provider *pp;
297
298 KASSERT(bp != NULL, ("NULL bp in g_io_deliver"));
299 pp = bp->bio_to;
300 KASSERT(pp != NULL, ("NULL bio_to in g_io_deliver"));
301 cp = bp->bio_from;
302 if (cp == NULL) {
303 bp->bio_error = error;
304 bp->bio_done(bp);
305 return;
306 }
307 KASSERT(cp != NULL, ("NULL bio_from in g_io_deliver"));
308 KASSERT(cp->geom != NULL, ("NULL bio_from->geom in g_io_deliver"));
309 KASSERT(bp->bio_completed >= 0, ("bio_completed can't be less than 0"));
310 KASSERT(bp->bio_completed <= bp->bio_length,
311 ("bio_completed can't be greater than bio_length"));
312
313 g_trace(G_T_BIO,
314 "g_io_deliver(%p) from %p(%s) to %p(%s) cmd %d error %d off %jd len %jd",
315 bp, cp, cp->geom->name, pp, pp->name, bp->bio_cmd, error,
316 (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length);
317
318 KASSERT(!(bp->bio_flags & BIO_ONQUEUE),
319 ("Bio already on queue bp=%p", bp));
320
321 /*
322 * XXX: next two doesn't belong here
323 */
324 bp->bio_bcount = bp->bio_length;
325 bp->bio_resid = bp->bio_bcount - bp->bio_completed;
326
327 if (g_collectstats & 4)
328 g_bioq_lock(&g_bio_run_up);
329 if (g_collectstats & 1)
330 devstat_end_transaction_bio(pp->stat, bp);
331 if (g_collectstats & 2)
332 devstat_end_transaction_bio(cp->stat, bp);
333 if (!(g_collectstats & 4))
334 g_bioq_lock(&g_bio_run_up);
335 cp->nend++;
336 pp->nend++;
337 if (error != ENOMEM) {
338 bp->bio_error = error;
339 TAILQ_INSERT_TAIL(&g_bio_run_up.bio_queue, bp, bio_queue);
340 bp->bio_flags |= BIO_ONQUEUE;
341 g_bio_run_up.bio_queue_length++;
342 g_bioq_unlock(&g_bio_run_up);
343 wakeup(&g_wait_up);
344 return;
345 }
346 g_bioq_unlock(&g_bio_run_up);
347
348 if (bootverbose)
349 printf("ENOMEM %p on %p(%s)\n", bp, pp, pp->name);
350 bp->bio_children = 0;
351 bp->bio_inbed = 0;
352 g_io_request(bp, cp);
353 pace++;
354 return;
355 }
356
357 void
358 g_io_schedule_down(struct thread *tp __unused)
359 {
360 struct bio *bp;
361 off_t excess;
362 int error;
363 #ifdef WITNESS
364 struct mtx mymutex;
365
366 bzero(&mymutex, sizeof mymutex);
367 mtx_init(&mymutex, "g_xdown", NULL, MTX_DEF);
368 #endif
369
370 for(;;) {
371 g_bioq_lock(&g_bio_run_down);
372 bp = g_bioq_first(&g_bio_run_down);
373 if (bp == NULL) {
374 CTR0(KTR_GEOM, "g_down going to sleep");
375 msleep(&g_wait_down, &g_bio_run_down.bio_queue_lock,
376 PRIBIO | PDROP, "-", hz/10);
377 continue;
378 }
379 CTR0(KTR_GEOM, "g_down has work to do");
380 g_bioq_unlock(&g_bio_run_down);
381 if (pace > 0) {
382 CTR1(KTR_GEOM, "g_down pacing self (pace %d)", pace);
383 msleep(&error, NULL, PRIBIO, "g_down", hz/10);
384 pace--;
385 }
386 error = g_io_check(bp);
387 if (error) {
388 CTR3(KTR_GEOM, "g_down g_io_check on bp %p provider "
389 "%s returned %d", bp, bp->bio_to->name, error);
390 g_io_deliver(bp, error);
391 continue;
392 }
393 CTR2(KTR_GEOM, "g_down processing bp %p provider %s", bp,
394 bp->bio_to->name);
395 switch (bp->bio_cmd) {
396 case BIO_READ:
397 case BIO_WRITE:
398 case BIO_DELETE:
399 /* Truncate requests to the end of providers media. */
400 /*
401 * XXX: What if we truncate because of offset being
402 * bad, not length?
403 */
404 excess = bp->bio_offset + bp->bio_length;
405 if (excess > bp->bio_to->mediasize) {
406 excess -= bp->bio_to->mediasize;
407 bp->bio_length -= excess;
408 if (excess > 0)
409 CTR3(KTR_GEOM, "g_down truncated bio "
410 "%p provider %s by %d", bp,
411 bp->bio_to->name, excess);
412 }
413 /* Deliver zero length transfers right here. */
414 if (bp->bio_length == 0) {
415 g_io_deliver(bp, 0);
416 CTR2(KTR_GEOM, "g_down terminated 0-length "
417 "bp %p provider %s", bp, bp->bio_to->name);
418 continue;
419 }
420 break;
421 default:
422 break;
423 }
424 #ifdef WITNESS
425 mtx_lock(&mymutex);
426 #endif
427 CTR4(KTR_GEOM, "g_down starting bp %p provider %s off %ld "
428 "len %ld", bp, bp->bio_to->name, bp->bio_offset,
429 bp->bio_length);
430 bp->bio_to->geom->start(bp);
431 #ifdef WITNESS
432 mtx_unlock(&mymutex);
433 #endif
434 }
435 }
436
437 void
438 bio_taskqueue(struct bio *bp, bio_task_t *func, void *arg)
439 {
440 bp->bio_task = func;
441 bp->bio_task_arg = arg;
442 /*
443 * The taskqueue is actually just a second queue off the "up"
444 * queue, so we use the same lock.
445 */
446 g_bioq_lock(&g_bio_run_up);
447 KASSERT(!(bp->bio_flags & BIO_ONQUEUE),
448 ("Bio already on queue bp=%p target taskq", bp));
449 bp->bio_flags |= BIO_ONQUEUE;
450 TAILQ_INSERT_TAIL(&g_bio_run_task.bio_queue, bp, bio_queue);
451 g_bio_run_task.bio_queue_length++;
452 wakeup(&g_wait_up);
453 g_bioq_unlock(&g_bio_run_up);
454 }
455
456
457 void
458 g_io_schedule_up(struct thread *tp __unused)
459 {
460 struct bio *bp;
461 #ifdef WITNESS
462 struct mtx mymutex;
463
464 bzero(&mymutex, sizeof mymutex);
465 mtx_init(&mymutex, "g_xup", NULL, MTX_DEF);
466 #endif
467 for(;;) {
468 g_bioq_lock(&g_bio_run_up);
469 bp = g_bioq_first(&g_bio_run_task);
470 if (bp != NULL) {
471 g_bioq_unlock(&g_bio_run_up);
472 #ifdef WITNESS
473 mtx_lock(&mymutex);
474 #endif
475 CTR1(KTR_GEOM, "g_up processing task bp %p", bp);
476 bp->bio_task(bp->bio_task_arg);
477 #ifdef WITNESS
478 mtx_unlock(&mymutex);
479 #endif
480 continue;
481 }
482 bp = g_bioq_first(&g_bio_run_up);
483 if (bp != NULL) {
484 g_bioq_unlock(&g_bio_run_up);
485 #ifdef WITNESS
486 mtx_lock(&mymutex);
487 #endif
488 CTR4(KTR_GEOM, "g_up biodone bp %p provider %s off "
489 "%ld len %ld", bp, bp->bio_to->name,
490 bp->bio_offset, bp->bio_length);
491 biodone(bp);
492 #ifdef WITNESS
493 mtx_unlock(&mymutex);
494 #endif
495 continue;
496 }
497 CTR0(KTR_GEOM, "g_up going to sleep");
498 msleep(&g_wait_up, &g_bio_run_up.bio_queue_lock,
499 PRIBIO | PDROP, "-", hz/10);
500 }
501 }
502
503 void *
504 g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error)
505 {
506 struct bio *bp;
507 void *ptr;
508 int errorc;
509
510 KASSERT(length > 0 && length >= cp->provider->sectorsize &&
511 length <= MAXPHYS, ("g_read_data(): invalid length %jd",
512 (intmax_t)length));
513
514 bp = g_alloc_bio();
515 bp->bio_cmd = BIO_READ;
516 bp->bio_done = NULL;
517 bp->bio_offset = offset;
518 bp->bio_length = length;
519 ptr = g_malloc(length, M_WAITOK);
520 bp->bio_data = ptr;
521 g_io_request(bp, cp);
522 errorc = biowait(bp, "gread");
523 if (error != NULL)
524 *error = errorc;
525 g_destroy_bio(bp);
526 if (errorc) {
527 g_free(ptr);
528 ptr = NULL;
529 }
530 return (ptr);
531 }
532
533 int
534 g_write_data(struct g_consumer *cp, off_t offset, void *ptr, off_t length)
535 {
536 struct bio *bp;
537 int error;
538
539 KASSERT(length > 0 && length >= cp->provider->sectorsize &&
540 length <= MAXPHYS, ("g_write_data(): invalid length %jd",
541 (intmax_t)length));
542
543 bp = g_alloc_bio();
544 bp->bio_cmd = BIO_WRITE;
545 bp->bio_done = NULL;
546 bp->bio_offset = offset;
547 bp->bio_length = length;
548 bp->bio_data = ptr;
549 g_io_request(bp, cp);
550 error = biowait(bp, "gwrite");
551 g_destroy_bio(bp);
552 return (error);
553 }
554
555 void
556 g_print_bio(struct bio *bp)
557 {
558 const char *pname, *cmd = NULL;
559
560 if (bp->bio_to != NULL)
561 pname = bp->bio_to->name;
562 else
563 pname = "[unknown]";
564
565 switch (bp->bio_cmd) {
566 case BIO_GETATTR:
567 cmd = "GETATTR";
568 printf("%s[%s(attr=%s)]", pname, cmd, bp->bio_attribute);
569 return;
570 case BIO_READ:
571 cmd = "READ";
572 case BIO_WRITE:
573 if (cmd == NULL)
574 cmd = "WRITE";
575 case BIO_DELETE:
576 if (cmd == NULL)
577 cmd = "DELETE";
578 printf("%s[%s(offset=%jd, length=%jd)]", pname, cmd,
579 (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length);
580 return;
581 default:
582 cmd = "UNKNOWN";
583 printf("%s[%s()]", pname, cmd);
584 return;
585 }
586 /* NOTREACHED */
587 }
Cache object: d0c5e24f0fb388d1080efefd88f67491
|