geom_io.c revision 114508
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: head/sys/geom/geom_io.c 114508 2003-05-02 06:36:14Z phk $ 36 */ 37 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 45#include <sys/errno.h> 46#include <geom/geom.h> 47#include <geom/geom_int.h> 48#include <sys/devicestat.h> 49 50static struct g_bioq g_bio_run_down; 51static struct g_bioq g_bio_run_up; 52static struct g_bioq g_bio_idle; 53 54static u_int pace; 55 56#include <machine/atomic.h> 57 58static void 59g_bioq_lock(struct g_bioq *bq) 60{ 61 62 mtx_lock(&bq->bio_queue_lock); 63} 64 65static void 66g_bioq_unlock(struct g_bioq *bq) 67{ 68 69 mtx_unlock(&bq->bio_queue_lock); 70} 71 72#if 0 73static void 74g_bioq_destroy(struct g_bioq *bq) 75{ 76 77 mtx_destroy(&bq->bio_queue_lock); 78} 79#endif 80 81static void 82g_bioq_init(struct g_bioq *bq) 83{ 84 85 TAILQ_INIT(&bq->bio_queue); 86 mtx_init(&bq->bio_queue_lock, "bio queue", NULL, MTX_DEF); 87} 88 89static struct bio * 90g_bioq_first(struct g_bioq *bq) 91{ 92 struct bio *bp; 93 94 bp = TAILQ_FIRST(&bq->bio_queue); 95 if (bp != NULL) { 96 TAILQ_REMOVE(&bq->bio_queue, bp, bio_queue); 97 bq->bio_queue_length--; 98 } 99 return (bp); 100} 101 102static void 103g_bioq_enqueue_tail(struct bio *bp, struct g_bioq *rq) 104{ 105 106 g_bioq_lock(rq); 107 TAILQ_INSERT_TAIL(&rq->bio_queue, bp, bio_queue); 108 rq->bio_queue_length++; 109 g_bioq_unlock(rq); 110} 111 112MALLOC_DEFINE(M_GEOMBIO, "GEOM bio", "Geom bio"); 113 114struct bio * 115g_new_bio(void) 116{ 117 struct bio *bp; 118 119 g_bioq_lock(&g_bio_idle); 120 bp = g_bioq_first(&g_bio_idle); 121 g_bioq_unlock(&g_bio_idle); 122 if (bp == NULL) 123 bp = malloc(sizeof *bp, M_GEOMBIO, M_NOWAIT | M_ZERO); 124 /* g_trace(G_T_BIO, "g_new_bio() = %p", bp); */ 125 return (bp); 126} 127 128void 129g_destroy_bio(struct bio *bp) 130{ 131 132#if 0 133 /* g_trace(G_T_BIO, "g_destroy_bio(%p)", bp); */ 134 bzero(bp, sizeof *bp); 135 g_bioq_enqueue_tail(bp, &g_bio_idle); 136#else 137 free(bp, M_GEOMBIO); 138#endif 139} 140 141struct bio * 142g_clone_bio(struct bio *bp) 143{ 144 struct bio *bp2; 145 146 bp2 = g_new_bio(); 147 if (bp2 != NULL) { 148 bp2->bio_parent = bp; 149 bp2->bio_cmd = bp->bio_cmd; 150 bp2->bio_length = bp->bio_length; 151 bp2->bio_offset = bp->bio_offset; 152 bp2->bio_data = bp->bio_data; 153 bp2->bio_attribute = bp->bio_attribute; 154 bp->bio_children++; 155 } 156 /* g_trace(G_T_BIO, "g_clone_bio(%p) = %p", bp, bp2); */ 157 return(bp2); 158} 159 160void 161g_io_init() 162{ 163 164 g_bioq_init(&g_bio_run_down); 165 g_bioq_init(&g_bio_run_up); 166 g_bioq_init(&g_bio_idle); 167} 168 169int 170g_io_getattr(const char *attr, struct g_consumer *cp, int *len, void *ptr) 171{ 172 struct bio *bp; 173 int error; 174 175 g_trace(G_T_BIO, "bio_getattr(%s)", attr); 176 bp = g_new_bio(); 177 bp->bio_cmd = BIO_GETATTR; 178 bp->bio_done = NULL; 179 bp->bio_attribute = attr; 180 bp->bio_length = *len; 181 bp->bio_data = ptr; 182 g_io_request(bp, cp); 183 error = biowait(bp, "ggetattr"); 184 *len = bp->bio_completed; 185 g_destroy_bio(bp); 186 return (error); 187} 188 189static int 190g_io_check(struct bio *bp) 191{ 192 struct g_consumer *cp; 193 struct g_provider *pp; 194 195 cp = bp->bio_from; 196 pp = bp->bio_to; 197 198 /* Fail if access counters dont allow the operation */ 199 switch(bp->bio_cmd) { 200 case BIO_READ: 201 case BIO_GETATTR: 202 if (cp->acr == 0) 203 return (EPERM); 204 break; 205 case BIO_WRITE: 206 case BIO_DELETE: 207 if (cp->acw == 0) 208 return (EPERM); 209 break; 210 default: 211 return (EPERM); 212 } 213 /* if provider is marked for error, don't disturb. */ 214 if (pp->error) 215 return (pp->error); 216 217 switch(bp->bio_cmd) { 218 case BIO_READ: 219 case BIO_WRITE: 220 case BIO_DELETE: 221 /* Reject I/O not on sector boundary */ 222 if (bp->bio_offset % pp->sectorsize) 223 return (EINVAL); 224 /* Reject I/O not integral sector long */ 225 if (bp->bio_length % pp->sectorsize) 226 return (EINVAL); 227 /* Reject requests past the end of media. */ 228 if (bp->bio_offset > pp->mediasize) 229 return (EIO); 230 break; 231 default: 232 break; 233 } 234 return (0); 235} 236 237void 238g_io_request(struct bio *bp, struct g_consumer *cp) 239{ 240 struct g_provider *pp; 241 242 pp = cp->provider; 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 KASSERT(pp != NULL, ("consumer not attached in g_io_request")); 247 248 bp->bio_from = cp; 249 bp->bio_to = pp; 250 bp->bio_error = 0; 251 bp->bio_completed = 0; 252 253 if (g_collectstats) { 254 devstat_start_transaction_bio(cp->stat, bp); 255 devstat_start_transaction_bio(pp->stat, bp); 256 } 257 cp->nstart++; 258 pp->nstart++; 259 260 /* Pass it on down. */ 261 g_trace(G_T_BIO, "bio_request(%p) from %p(%s) to %p(%s) cmd %d", 262 bp, cp, cp->geom->name, pp, pp->name, bp->bio_cmd); 263 g_bioq_enqueue_tail(bp, &g_bio_run_down); 264 wakeup(&g_wait_down); 265} 266 267void 268g_io_deliver(struct bio *bp, int error) 269{ 270 struct g_consumer *cp; 271 struct g_provider *pp; 272 273 cp = bp->bio_from; 274 pp = bp->bio_to; 275 KASSERT(bp != NULL, ("NULL bp in g_io_deliver")); 276 KASSERT(cp != NULL, ("NULL bio_from in g_io_deliver")); 277 KASSERT(cp->geom != NULL, ("NULL bio_from->geom in g_io_deliver")); 278 KASSERT(pp != NULL, ("NULL bio_to in g_io_deliver")); 279 280 g_trace(G_T_BIO, 281"g_io_deliver(%p) from %p(%s) to %p(%s) cmd %d error %d off %jd len %jd", 282 bp, cp, cp->geom->name, pp, pp->name, bp->bio_cmd, error, 283 (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length); 284 285 bp->bio_bcount = bp->bio_length; 286 if (g_collectstats) { 287 bp->bio_resid = bp->bio_bcount - bp->bio_completed; 288 devstat_end_transaction_bio(cp->stat, bp); 289 devstat_end_transaction_bio(pp->stat, bp); 290 } 291 cp->nend++; 292 pp->nend++; 293 294 if (error == ENOMEM) { 295 printf("ENOMEM %p on %p(%s)\n", bp, pp, pp->name); 296 g_io_request(bp, cp); 297 pace++; 298 return; 299 } 300 bp->bio_error = error; 301 g_bioq_enqueue_tail(bp, &g_bio_run_up); 302 wakeup(&g_wait_up); 303} 304 305void 306g_io_schedule_down(struct thread *tp __unused) 307{ 308 struct bio *bp; 309 off_t excess; 310 int error; 311 struct mtx mymutex; 312 313 bzero(&mymutex, sizeof mymutex); 314 mtx_init(&mymutex, "g_xdown", MTX_DEF, 0); 315 316 for(;;) { 317 g_bioq_lock(&g_bio_run_down); 318 bp = g_bioq_first(&g_bio_run_down); 319 if (bp == NULL) { 320 msleep(&g_wait_down, &g_bio_run_down.bio_queue_lock, 321 PRIBIO | PDROP, "g_down", hz/10); 322 continue; 323 } 324 g_bioq_unlock(&g_bio_run_down); 325 if (pace > 0) { 326 msleep(&error, NULL, PRIBIO, "g_down", hz/10); 327 pace--; 328 } 329 error = g_io_check(bp); 330 if (error) { 331 g_io_deliver(bp, error); 332 continue; 333 } 334 switch (bp->bio_cmd) { 335 case BIO_READ: 336 case BIO_WRITE: 337 case BIO_DELETE: 338 /* Truncate requests to the end of providers media. */ 339 excess = bp->bio_offset + bp->bio_length; 340 if (excess > bp->bio_to->mediasize) { 341 excess -= bp->bio_to->mediasize; 342 bp->bio_length -= excess; 343 } 344 /* Deliver zero length transfers right here. */ 345 if (bp->bio_length == 0) { 346 g_io_deliver(bp, 0); 347 continue; 348 } 349 break; 350 default: 351 break; 352 } 353 mtx_lock(&mymutex); 354 bp->bio_to->geom->start(bp); 355 mtx_unlock(&mymutex); 356 } 357} 358 359void 360g_io_schedule_up(struct thread *tp __unused) 361{ 362 struct bio *bp; 363 struct mtx mymutex; 364 365 bzero(&mymutex, sizeof mymutex); 366 mtx_init(&mymutex, "g_xup", MTX_DEF, 0); 367 for(;;) { 368 g_bioq_lock(&g_bio_run_up); 369 bp = g_bioq_first(&g_bio_run_up); 370 if (bp != NULL) { 371 g_bioq_unlock(&g_bio_run_up); 372 mtx_lock(&mymutex); 373 biodone(bp); 374 mtx_unlock(&mymutex); 375 continue; 376 } 377 msleep(&g_wait_up, &g_bio_run_up.bio_queue_lock, 378 PRIBIO | PDROP, "g_up", hz/10); 379 } 380} 381 382void * 383g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error) 384{ 385 struct bio *bp; 386 void *ptr; 387 int errorc; 388 389 bp = g_new_bio(); 390 bp->bio_cmd = BIO_READ; 391 bp->bio_done = NULL; 392 bp->bio_offset = offset; 393 bp->bio_length = length; 394 ptr = g_malloc(length, M_WAITOK); 395 bp->bio_data = ptr; 396 g_io_request(bp, cp); 397 errorc = biowait(bp, "gread"); 398 if (error != NULL) 399 *error = errorc; 400 g_destroy_bio(bp); 401 if (errorc) { 402 g_free(ptr); 403 ptr = NULL; 404 } 405 return (ptr); 406} 407 408int 409g_write_data(struct g_consumer *cp, off_t offset, void *ptr, off_t length) 410{ 411 struct bio *bp; 412 int error; 413 414 bp = g_new_bio(); 415 bp->bio_cmd = BIO_WRITE; 416 bp->bio_done = NULL; 417 bp->bio_offset = offset; 418 bp->bio_length = length; 419 bp->bio_data = ptr; 420 g_io_request(bp, cp); 421 error = biowait(bp, "gwrite"); 422 g_destroy_bio(bp); 423 return (error); 424} 425