geom_io.c revision 121323
1184798Srafan/*- 2184798Srafan * Copyright (c) 2002 Poul-Henning Kamp 3184798Srafan * Copyright (c) 2002 Networks Associates Technology, Inc. 4184798Srafan * All rights reserved. 5184798Srafan * 6184798Srafan * This software was developed for the FreeBSD Project by Poul-Henning Kamp 7184798Srafan * and NAI Labs, the Security Research Division of Network Associates, Inc. 8184798Srafan * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 9184798Srafan * DARPA CHATS research program. 10184798Srafan * 11184798Srafan * Redistribution and use in source and binary forms, with or without 12184798Srafan * modification, are permitted provided that the following conditions 13184798Srafan * are met: 14184798Srafan * 1. Redistributions of source code must retain the above copyright 15184798Srafan * notice, this list of conditions and the following disclaimer. 16184798Srafan * 2. Redistributions in binary form must reproduce the above copyright 17184798Srafan * notice, this list of conditions and the following disclaimer in the 18184798Srafan * documentation and/or other materials provided with the distribution. 19184798Srafan * 3. The names of the authors may not be used to endorse or promote 20184798Srafan * products derived from this software without specific prior written 21184798Srafan * permission. 22184798Srafan * 23184798Srafan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 24184798Srafan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25184798Srafan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26184798Srafan * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 27184798Srafan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28184798Srafan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29184798Srafan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30184798Srafan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31184798Srafan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32184798Srafan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33184798Srafan * SUCH DAMAGE. 34184798Srafan */ 35184798Srafan 36184798Srafan#include <sys/cdefs.h> 37184798Srafan__FBSDID("$FreeBSD: head/sys/geom/geom_io.c 121323 2003-10-22 06:32:20Z phk $"); 38184798Srafan 39184798Srafan#include <sys/param.h> 40184798Srafan#include <sys/systm.h> 41184798Srafan#include <sys/kernel.h> 42184798Srafan#include <sys/malloc.h> 43184798Srafan#include <sys/bio.h> 44184798Srafan 45184798Srafan#include <sys/errno.h> 46184798Srafan#include <geom/geom.h> 47184798Srafan#include <geom/geom_int.h> 48184798Srafan#include <sys/devicestat.h> 49184798Srafan 50184798Srafan#include <vm/uma.h> 51184798Srafan 52184798Srafanstatic struct g_bioq g_bio_run_down; 53184798Srafanstatic struct g_bioq g_bio_run_up; 54184798Srafan 55184798Srafanstatic u_int pace; 56184798Srafanstatic uma_zone_t biozone; 57184798Srafan 58184798Srafan#include <machine/atomic.h> 59184798Srafan 60184798Srafanstatic void 61184798Srafang_bioq_lock(struct g_bioq *bq) 62184798Srafan{ 63184798Srafan 64184798Srafan mtx_lock(&bq->bio_queue_lock); 65184798Srafan} 66184798Srafan 67184798Srafanstatic void 68184798Srafang_bioq_unlock(struct g_bioq *bq) 69184798Srafan{ 70184798Srafan 71184798Srafan mtx_unlock(&bq->bio_queue_lock); 72184798Srafan} 73184798Srafan 74184798Srafan#if 0 75184798Srafanstatic void 76184798Srafang_bioq_destroy(struct g_bioq *bq) 77184798Srafan{ 78184798Srafan 79184798Srafan mtx_destroy(&bq->bio_queue_lock); 80184798Srafan} 81184798Srafan#endif 82184798Srafan 83184798Srafanstatic void 84184798Srafang_bioq_init(struct g_bioq *bq) 85184798Srafan{ 86184798Srafan 87184798Srafan TAILQ_INIT(&bq->bio_queue); 88184798Srafan mtx_init(&bq->bio_queue_lock, "bio queue", NULL, MTX_DEF); 89184798Srafan} 90184798Srafan 91184798Srafanstatic struct bio * 92184798Srafang_bioq_first(struct g_bioq *bq) 93184798Srafan{ 94184798Srafan struct bio *bp; 95184798Srafan 96184798Srafan bp = TAILQ_FIRST(&bq->bio_queue); 97184798Srafan if (bp != NULL) { 98184798Srafan TAILQ_REMOVE(&bq->bio_queue, bp, bio_queue); 99184798Srafan bq->bio_queue_length--; 100184798Srafan } 101184798Srafan return (bp); 102184798Srafan} 103184798Srafan 104184798Srafanstatic void 105184798Srafang_bioq_enqueue_tail(struct bio *bp, struct g_bioq *rq) 106184798Srafan{ 107184798Srafan 108184798Srafan g_bioq_lock(rq); 109184798Srafan TAILQ_INSERT_TAIL(&rq->bio_queue, bp, bio_queue); 110184798Srafan rq->bio_queue_length++; 111184798Srafan g_bioq_unlock(rq); 112184798Srafan} 113184798Srafan 114184798Srafanstruct bio * 115184798Srafang_new_bio(void) 116184798Srafan{ 117184798Srafan struct bio *bp; 118184798Srafan 119184798Srafan bp = uma_zalloc(biozone, M_NOWAIT | M_ZERO); 120184798Srafan return (bp); 121184798Srafan} 122184798Srafan 123184798Srafanvoid 124184798Srafang_destroy_bio(struct bio *bp) 125184798Srafan{ 126184798Srafan 127184798Srafan uma_zfree(biozone, bp); 128184798Srafan} 129184798Srafan 130184798Srafanstruct bio * 131184798Srafang_clone_bio(struct bio *bp) 132184798Srafan{ 133184798Srafan struct bio *bp2; 134184798Srafan 135184798Srafan bp2 = uma_zalloc(biozone, M_NOWAIT | M_ZERO); 136184798Srafan if (bp2 != NULL) { 137184798Srafan bp2->bio_parent = bp; 138184798Srafan bp2->bio_cmd = bp->bio_cmd; 139184798Srafan bp2->bio_length = bp->bio_length; 140184798Srafan bp2->bio_offset = bp->bio_offset; 141184798Srafan bp2->bio_data = bp->bio_data; 142184798Srafan bp2->bio_attribute = bp->bio_attribute; 143184798Srafan bp->bio_children++; 144184798Srafan } 145184798Srafan return(bp2); 146184798Srafan} 147184798Srafan 148184798Srafanvoid 149184798Srafang_io_init() 150184798Srafan{ 151184798Srafan 152184798Srafan g_bioq_init(&g_bio_run_down); 153184798Srafan g_bioq_init(&g_bio_run_up); 154184798Srafan biozone = uma_zcreate("g_bio", sizeof (struct bio), 155184798Srafan NULL, NULL, 156184798Srafan NULL, NULL, 157184798Srafan 0, 0); 158184798Srafan} 159184798Srafan 160184798Srafanint 161184798Srafang_io_getattr(const char *attr, struct g_consumer *cp, int *len, void *ptr) 162184798Srafan{ 163184798Srafan struct bio *bp; 164184798Srafan int error; 165184798Srafan 166184798Srafan g_trace(G_T_BIO, "bio_getattr(%s)", attr); 167184798Srafan bp = g_new_bio(); 168184798Srafan bp->bio_cmd = BIO_GETATTR; 169184798Srafan bp->bio_done = NULL; 170184798Srafan bp->bio_attribute = attr; 171184798Srafan bp->bio_length = *len; 172184798Srafan bp->bio_data = ptr; 173184798Srafan g_io_request(bp, cp); 174184798Srafan error = biowait(bp, "ggetattr"); 175184798Srafan *len = bp->bio_completed; 176184798Srafan g_destroy_bio(bp); 177184798Srafan return (error); 178184798Srafan} 179184798Srafan 180184798Srafanstatic int 181184798Srafang_io_check(struct bio *bp) 182184798Srafan{ 183184798Srafan struct g_consumer *cp; 184184798Srafan struct g_provider *pp; 185184798Srafan 186184798Srafan cp = bp->bio_from; 187184798Srafan pp = bp->bio_to; 188184798Srafan 189184798Srafan /* Fail if access counters dont allow the operation */ 190184798Srafan switch(bp->bio_cmd) { 191184798Srafan case BIO_READ: 192184798Srafan case BIO_GETATTR: 193184798Srafan if (cp->acr == 0) 194184798Srafan return (EPERM); 195184798Srafan break; 196184798Srafan case BIO_WRITE: 197184798Srafan case BIO_DELETE: 198184798Srafan if (cp->acw == 0) 199184798Srafan return (EPERM); 200184798Srafan break; 201184798Srafan default: 202184798Srafan return (EPERM); 203184798Srafan } 204184798Srafan /* if provider is marked for error, don't disturb. */ 205184798Srafan if (pp->error) 206 return (pp->error); 207 208 switch(bp->bio_cmd) { 209 case BIO_READ: 210 case BIO_WRITE: 211 case BIO_DELETE: 212 /* Zero sectorsize is a probably lack of media */ 213 if (pp->sectorsize == 0) 214 return (ENXIO); 215 /* Reject I/O not on sector boundary */ 216 if (bp->bio_offset % pp->sectorsize) 217 return (EINVAL); 218 /* Reject I/O not integral sector long */ 219 if (bp->bio_length % pp->sectorsize) 220 return (EINVAL); 221 /* Reject requests before or past the end of media. */ 222 if (bp->bio_offset < 0) 223 return (EIO); 224 if (bp->bio_offset > pp->mediasize) 225 return (EIO); 226 break; 227 default: 228 break; 229 } 230 return (0); 231} 232 233void 234g_io_request(struct bio *bp, struct g_consumer *cp) 235{ 236 struct g_provider *pp; 237 238 KASSERT(cp != NULL, ("NULL cp in g_io_request")); 239 KASSERT(bp != NULL, ("NULL bp in g_io_request")); 240 KASSERT(bp->bio_data != NULL, ("NULL bp->data in g_io_request")); 241 pp = cp->provider; 242 KASSERT(pp != NULL, ("consumer not attached in g_io_request")); 243 244 bp->bio_from = cp; 245 bp->bio_to = pp; 246 bp->bio_error = 0; 247 bp->bio_completed = 0; 248 249 if (g_collectstats) { 250 devstat_start_transaction_bio(cp->stat, bp); 251 devstat_start_transaction_bio(pp->stat, bp); 252 } 253 cp->nstart++; 254 pp->nstart++; 255 256 /* Pass it on down. */ 257 g_trace(G_T_BIO, "bio_request(%p) from %p(%s) to %p(%s) cmd %d", 258 bp, cp, cp->geom->name, pp, pp->name, bp->bio_cmd); 259 g_bioq_enqueue_tail(bp, &g_bio_run_down); 260 wakeup(&g_wait_down); 261} 262 263void 264g_io_deliver(struct bio *bp, int error) 265{ 266 struct g_consumer *cp; 267 struct g_provider *pp; 268 269 KASSERT(bp != NULL, ("NULL bp in g_io_deliver")); 270 pp = bp->bio_to; 271 KASSERT(pp != NULL, ("NULL bio_to in g_io_deliver")); 272 cp = bp->bio_from; 273 if (cp == NULL) { 274 bp->bio_error = error; 275 bp->bio_done(bp); 276 return; 277 } 278 KASSERT(cp != NULL, ("NULL bio_from in g_io_deliver")); 279 KASSERT(cp->geom != NULL, ("NULL bio_from->geom in g_io_deliver")); 280 281 g_trace(G_T_BIO, 282"g_io_deliver(%p) from %p(%s) to %p(%s) cmd %d error %d off %jd len %jd", 283 bp, cp, cp->geom->name, pp, pp->name, bp->bio_cmd, error, 284 (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length); 285 286 bp->bio_bcount = bp->bio_length; 287 if (g_collectstats) { 288 bp->bio_resid = bp->bio_bcount - bp->bio_completed; 289 devstat_end_transaction_bio(cp->stat, bp); 290 devstat_end_transaction_bio(pp->stat, bp); 291 } 292 cp->nend++; 293 pp->nend++; 294 295 if (error == ENOMEM) { 296 if (bootverbose) 297 printf("ENOMEM %p on %p(%s)\n", bp, pp, pp->name); 298 g_io_request(bp, cp); 299 pace++; 300 return; 301 } 302 bp->bio_error = error; 303 g_bioq_enqueue_tail(bp, &g_bio_run_up); 304 wakeup(&g_wait_up); 305} 306 307void 308g_io_schedule_down(struct thread *tp __unused) 309{ 310 struct bio *bp; 311 off_t excess; 312 int error; 313 struct mtx mymutex; 314 315 bzero(&mymutex, sizeof mymutex); 316 mtx_init(&mymutex, "g_xdown", MTX_DEF, 0); 317 318 for(;;) { 319 g_bioq_lock(&g_bio_run_down); 320 bp = g_bioq_first(&g_bio_run_down); 321 if (bp == NULL) { 322 msleep(&g_wait_down, &g_bio_run_down.bio_queue_lock, 323 PRIBIO | PDROP, "-", hz/10); 324 continue; 325 } 326 g_bioq_unlock(&g_bio_run_down); 327 if (pace > 0) { 328 msleep(&error, NULL, PRIBIO, "g_down", hz/10); 329 pace--; 330 } 331 error = g_io_check(bp); 332 if (error) { 333 g_io_deliver(bp, error); 334 continue; 335 } 336 switch (bp->bio_cmd) { 337 case BIO_READ: 338 case BIO_WRITE: 339 case BIO_DELETE: 340 /* Truncate requests to the end of providers media. */ 341 excess = bp->bio_offset + bp->bio_length; 342 if (excess > bp->bio_to->mediasize) { 343 excess -= bp->bio_to->mediasize; 344 bp->bio_length -= excess; 345 } 346 /* Deliver zero length transfers right here. */ 347 if (bp->bio_length == 0) { 348 g_io_deliver(bp, 0); 349 continue; 350 } 351 break; 352 default: 353 break; 354 } 355 mtx_lock(&mymutex); 356 bp->bio_to->geom->start(bp); 357 mtx_unlock(&mymutex); 358 } 359} 360 361void 362g_io_schedule_up(struct thread *tp __unused) 363{ 364 struct bio *bp; 365 struct mtx mymutex; 366 367 bzero(&mymutex, sizeof mymutex); 368 mtx_init(&mymutex, "g_xup", MTX_DEF, 0); 369 for(;;) { 370 g_bioq_lock(&g_bio_run_up); 371 bp = g_bioq_first(&g_bio_run_up); 372 if (bp != NULL) { 373 g_bioq_unlock(&g_bio_run_up); 374 mtx_lock(&mymutex); 375 biodone(bp); 376 mtx_unlock(&mymutex); 377 continue; 378 } 379 msleep(&g_wait_up, &g_bio_run_up.bio_queue_lock, 380 PRIBIO | PDROP, "-", hz/10); 381 } 382} 383 384void * 385g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error) 386{ 387 struct bio *bp; 388 void *ptr; 389 int errorc; 390 391 KASSERT(length >= 512 && length <= DFLTPHYS, 392 ("g_read_data(): invalid length %jd", (intmax_t)length)); 393 394 bp = g_new_bio(); 395 bp->bio_cmd = BIO_READ; 396 bp->bio_done = NULL; 397 bp->bio_offset = offset; 398 bp->bio_length = length; 399 ptr = g_malloc(length, M_WAITOK); 400 bp->bio_data = ptr; 401 g_io_request(bp, cp); 402 errorc = biowait(bp, "gread"); 403 if (error != NULL) 404 *error = errorc; 405 g_destroy_bio(bp); 406 if (errorc) { 407 g_free(ptr); 408 ptr = NULL; 409 } 410 return (ptr); 411} 412 413int 414g_write_data(struct g_consumer *cp, off_t offset, void *ptr, off_t length) 415{ 416 struct bio *bp; 417 int error; 418 419 KASSERT(length >= 512 && length <= DFLTPHYS, 420 ("g_write_data(): invalid length %jd", (intmax_t)length)); 421 422 bp = g_new_bio(); 423 bp->bio_cmd = BIO_WRITE; 424 bp->bio_done = NULL; 425 bp->bio_offset = offset; 426 bp->bio_length = length; 427 bp->bio_data = ptr; 428 g_io_request(bp, cp); 429 error = biowait(bp, "gwrite"); 430 g_destroy_bio(bp); 431 return (error); 432} 433