sys/kern/kern_physio.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | /* * Copyright (c) 1994 John S. Dyson * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice immediately at the beginning of the file, without modification, * this list of conditions, and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Absolutely no warranty of function or purpose is made by the author * John S. Dyson. * 4. Modifications may be freely made to this file if the above conditions * are met. */ #include <sys/param.h> #include <sys/systm.h> #include <sys/buf.h> #include <sys/conf.h> #include <sys/proc.h> #include <sys/uio.h> #include <sys/device.h> #include <vm/vm.h> #include <vm/vm_extern.h> static int physio(cdev_t dev, struct uio *uio, int ioflag) { int i; int error; int saflags; size_t iolen; size_t bcount; caddr_t ubase; struct buf *bp; if (uio->uio_segflg == UIO_USERSPACE) bp = getpbuf_mem(NULL); else bp = getpbuf_kva(NULL); saflags = bp->b_flags; error = 0; /* XXX: sanity check */ if (dev->si_iosize_max < PAGE_SIZE) { kprintf("WARNING: %s si_iosize_max=%d, using MAXPHYS.\n", devtoname(dev), dev->si_iosize_max); dev->si_iosize_max = MAXPHYS; } /* Must be a real uio */ KKASSERT(uio->uio_segflg != UIO_NOCOPY); for (i = 0; i < uio->uio_iovcnt; i++) { while (uio->uio_iov[i].iov_len) { if (uio->uio_rw == UIO_READ) bp->b_cmd = BUF_CMD_READ; else bp->b_cmd = BUF_CMD_WRITE; bp->b_flags = saflags; bcount = uio->uio_iov[i].iov_len; reinitbufbio(bp); /* clear translation cache */ bp->b_bio1.bio_offset = uio->uio_offset; bp->b_bio1.bio_done = biodone_sync; bp->b_bio1.bio_flags |= BIO_SYNC; /* * Setup for mapping the request into kernel memory. * * We can only write as much as fits in a pbuf, * which is MAXPHYS, and no larger then the device's * ability. * * If not using bounce pages the base address of the * user mapping into the pbuf may be offset, further * reducing how much will actually fit in the pbuf. */ if (bcount > dev->si_iosize_max) bcount = dev->si_iosize_max; ubase = uio->uio_iov[i].iov_base; iolen = ((vm_offset_t)ubase) & PAGE_MASK; if (bcount > bp->b_kvasize) bcount = bp->b_kvasize; /* * If we have to use a bounce buffer allocate kernel * memory and copyin/copyout. Otherwise map the * user buffer directly into kernel memory without * copying. */ if (uio->uio_segflg == UIO_USERSPACE) { bp->b_bcount = bcount; if (uio->uio_rw == UIO_WRITE) { error = copyin(ubase, bp->b_data, bcount); if (error) goto doerror; } } else { bp->b_data = uio->uio_iov[i].iov_base; bp->b_bcount = bcount; } dev_dstrategy(dev, &bp->b_bio1); biowait(&bp->b_bio1, "physstr"); iolen = bp->b_bcount - bp->b_resid; if (uio->uio_segflg == UIO_USERSPACE) { if (uio->uio_rw == UIO_READ && iolen) { error = copyout(bp->b_data, ubase, iolen); if (error) { bp->b_flags |= B_ERROR; bp->b_error = error; } } } if (iolen == 0 && !(bp->b_flags & B_ERROR)) goto doerror; /* EOF */ uio->uio_iov[i].iov_len -= iolen; uio->uio_iov[i].iov_base = (char *)uio->uio_iov[i].iov_base + iolen; uio->uio_resid -= iolen; uio->uio_offset += iolen; if (bp->b_flags & B_ERROR) { error = bp->b_error; goto doerror; } } } doerror: relpbuf(bp, NULL); return (error); } int physread(struct dev_read_args *ap) { return(physio(ap->a_head.a_dev, ap->a_uio, ap->a_ioflag)); } int physwrite(struct dev_write_args *ap) { return(physio(ap->a_head.a_dev, ap->a_uio, ap->a_ioflag)); } |