Home | History | Annotate | Line # | Download | only in kern
kern_physio.c revision 1.37.2.3
      1  1.37.2.2   bouyer /*	$NetBSD: kern_physio.c,v 1.37.2.3 2000/12/13 15:50:21 bouyer Exp $	*/
      2      1.20      cgd 
      3      1.20      cgd /*-
      4      1.20      cgd  * Copyright (c) 1994 Christopher G. Demetriou
      5      1.20      cgd  * Copyright (c) 1982, 1986, 1990, 1993
      6      1.20      cgd  *	The Regents of the University of California.  All rights reserved.
      7      1.20      cgd  * (c) UNIX System Laboratories, Inc.
      8      1.20      cgd  * All or some portions of this file are derived from material licensed
      9      1.20      cgd  * to the University of California by American Telephone and Telegraph
     10      1.20      cgd  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     11      1.20      cgd  * the permission of UNIX System Laboratories, Inc.
     12      1.20      cgd  *
     13      1.20      cgd  * Redistribution and use in source and binary forms, with or without
     14      1.20      cgd  * modification, are permitted provided that the following conditions
     15      1.20      cgd  * are met:
     16      1.20      cgd  * 1. Redistributions of source code must retain the above copyright
     17      1.20      cgd  *    notice, this list of conditions and the following disclaimer.
     18      1.20      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     19      1.20      cgd  *    notice, this list of conditions and the following disclaimer in the
     20      1.20      cgd  *    documentation and/or other materials provided with the distribution.
     21      1.20      cgd  * 3. All advertising materials mentioning features or use of this software
     22      1.20      cgd  *    must display the following acknowledgement:
     23      1.20      cgd  *	This product includes software developed by the University of
     24      1.20      cgd  *	California, Berkeley and its contributors.
     25      1.20      cgd  * 4. Neither the name of the University nor the names of its contributors
     26      1.20      cgd  *    may be used to endorse or promote products derived from this software
     27      1.20      cgd  *    without specific prior written permission.
     28      1.20      cgd  *
     29      1.20      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     30      1.20      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     31      1.20      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     32      1.20      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     33      1.20      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     34      1.20      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     35      1.20      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     36      1.20      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     37      1.20      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     38      1.20      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     39      1.20      cgd  * SUCH DAMAGE.
     40      1.20      cgd  *
     41      1.20      cgd  *	@(#)kern_physio.c	8.1 (Berkeley) 6/10/93
     42      1.20      cgd  */
     43      1.30      mrg 
     44      1.20      cgd #include <sys/param.h>
     45      1.20      cgd #include <sys/systm.h>
     46      1.20      cgd #include <sys/buf.h>
     47      1.29      mrg #include <sys/malloc.h>
     48      1.20      cgd #include <sys/proc.h>
     49      1.20      cgd 
     50      1.29      mrg #include <uvm/uvm_extern.h>
     51      1.29      mrg 
     52      1.20      cgd /*
     53      1.20      cgd  * The routines implemented in this file are described in:
     54      1.20      cgd  *	Leffler, et al.: The Design and Implementation of the 4.3BSD
     55      1.20      cgd  *	    UNIX Operating System (Addison Welley, 1989)
     56      1.20      cgd  * on pages 231-233.
     57      1.20      cgd  *
     58      1.20      cgd  * The routines "getphysbuf" and "putphysbuf" steal and return a swap
     59      1.20      cgd  * buffer.  Leffler, et al., says that swap buffers are used to do the
     60      1.20      cgd  * I/O, so raw I/O requests don't have to be single-threaded.
     61      1.20      cgd  */
     62      1.20      cgd 
     63      1.20      cgd struct buf *getphysbuf __P((void));
     64      1.20      cgd void putphysbuf __P((struct buf *bp));
     65      1.20      cgd 
     66      1.20      cgd /*
     67      1.20      cgd  * Do "physical I/O" on behalf of a user.  "Physical I/O" is I/O directly
     68      1.20      cgd  * from the raw device to user buffers, and bypasses the buffer cache.
     69      1.20      cgd  *
     70      1.20      cgd  * Comments in brackets are from Leffler, et al.'s pseudo-code implementation.
     71      1.20      cgd  */
     72      1.20      cgd int
     73      1.20      cgd physio(strategy, bp, dev, flags, minphys, uio)
     74      1.24  mycroft 	void (*strategy) __P((struct buf *));
     75      1.20      cgd 	struct buf *bp;
     76      1.20      cgd 	dev_t dev;
     77      1.20      cgd 	int flags;
     78      1.24  mycroft 	void (*minphys) __P((struct buf *));
     79      1.20      cgd 	struct uio *uio;
     80      1.20      cgd {
     81      1.20      cgd 	struct iovec *iovp;
     82      1.20      cgd 	struct proc *p = curproc;
     83  1.37.2.3   bouyer 	int error, done, i, nobuf, s;
     84  1.37.2.3   bouyer 	long todo;
     85      1.20      cgd 
     86      1.20      cgd 	error = 0;
     87  1.37.2.1   bouyer 	flags &= B_READ | B_WRITE | B_ORDERED;
     88      1.20      cgd 
     89      1.20      cgd 	/*
     90      1.20      cgd 	 * [check user read/write access to the data buffer]
     91      1.20      cgd 	 *
     92      1.20      cgd 	 * Check each iov one by one.  Note that we know if we're reading or
     93      1.20      cgd 	 * writing, so we ignore the uio's rw parameter.  Also note that if
     94      1.20      cgd 	 * we're doing a read, that's a *write* to user-space.
     95      1.20      cgd 	 */
     96  1.37.2.1   bouyer 	if (uio->uio_segflg == UIO_USERSPACE) {
     97  1.37.2.1   bouyer 		for (i = 0; i < uio->uio_iovcnt; i++) {
     98      1.35      mrg 			/* XXXCDC: map not locked, rethink */
     99  1.37.2.1   bouyer 			if (__predict_false(!uvm_useracc(uio->uio_iov[i].iov_base,
    100      1.29      mrg 				     uio->uio_iov[i].iov_len,
    101  1.37.2.1   bouyer 				     (flags == B_READ) ? B_WRITE : B_READ)))
    102      1.29      mrg 				return (EFAULT);
    103  1.37.2.1   bouyer 		}
    104  1.37.2.1   bouyer 	}
    105      1.20      cgd 
    106      1.20      cgd 	/* Make sure we have a buffer, creating one if necessary. */
    107      1.29      mrg 	if ((nobuf = (bp == NULL)) != 0) {
    108      1.29      mrg 
    109      1.20      cgd 		bp = getphysbuf();
    110      1.29      mrg 		/* bp was just malloc'd so can't already be busy */
    111      1.29      mrg 		bp->b_flags |= B_BUSY;
    112      1.29      mrg 
    113      1.29      mrg 	} else {
    114      1.29      mrg 
    115      1.29      mrg 		/* [raise the processor priority level to splbio;] */
    116      1.29      mrg 		s = splbio();
    117      1.20      cgd 
    118      1.29      mrg 		/* [while the buffer is marked busy] */
    119      1.29      mrg 		while (bp->b_flags & B_BUSY) {
    120      1.29      mrg 			/* [mark the buffer wanted] */
    121      1.29      mrg 			bp->b_flags |= B_WANTED;
    122      1.29      mrg 			/* [wait until the buffer is available] */
    123      1.29      mrg 			tsleep((caddr_t)bp, PRIBIO+1, "physbuf", 0);
    124      1.29      mrg 		}
    125      1.20      cgd 
    126      1.29      mrg 		/* Mark it busy, so nobody else will use it. */
    127      1.29      mrg 		bp->b_flags |= B_BUSY;
    128      1.20      cgd 
    129      1.29      mrg 		/* [lower the priority level] */
    130      1.29      mrg 		splx(s);
    131      1.20      cgd 
    132      1.29      mrg 	}
    133      1.20      cgd 
    134      1.20      cgd 	/* [set up the fixed part of the buffer for a transfer] */
    135      1.20      cgd 	bp->b_dev = dev;
    136      1.20      cgd 	bp->b_error = 0;
    137      1.20      cgd 	bp->b_proc = p;
    138  1.37.2.1   bouyer 	LIST_INIT(&bp->b_dep);
    139      1.20      cgd 
    140      1.20      cgd 	/*
    141      1.20      cgd 	 * [while there are data to transfer and no I/O error]
    142      1.20      cgd 	 * Note that I/O errors are handled with a 'goto' at the bottom
    143      1.20      cgd 	 * of the 'while' loop.
    144      1.20      cgd 	 */
    145      1.20      cgd 	for (i = 0; i < uio->uio_iovcnt; i++) {
    146      1.20      cgd 		iovp = &uio->uio_iov[i];
    147      1.20      cgd 		while (iovp->iov_len > 0) {
    148      1.20      cgd 			/*
    149      1.20      cgd 			 * [mark the buffer busy for physical I/O]
    150      1.20      cgd 			 * (i.e. set B_PHYS (because it's an I/O to user
    151      1.20      cgd 			 * memory, and B_RAW, because B_RAW is to be
    152      1.20      cgd 			 * "Set by physio for raw transfers.", in addition
    153      1.20      cgd 			 * to the "busy" and read/write flag.)
    154      1.20      cgd 			 */
    155      1.20      cgd 			bp->b_flags = B_BUSY | B_PHYS | B_RAW | flags;
    156      1.20      cgd 
    157      1.20      cgd 			/* [set up the buffer for a maximum-sized transfer] */
    158      1.20      cgd 			bp->b_blkno = btodb(uio->uio_offset);
    159      1.20      cgd 			bp->b_bcount = iovp->iov_len;
    160      1.20      cgd 			bp->b_data = iovp->iov_base;
    161      1.24  mycroft 
    162      1.20      cgd 			/*
    163      1.20      cgd 			 * [call minphys to bound the tranfer size]
    164      1.20      cgd 			 * and remember the amount of data to transfer,
    165      1.20      cgd 			 * for later comparison.
    166      1.20      cgd 			 */
    167      1.24  mycroft 			(*minphys)(bp);
    168      1.24  mycroft 			todo = bp->b_bcount;
    169      1.22      cgd #ifdef DIAGNOSTIC
    170  1.37.2.1   bouyer 			if (todo <= 0)
    171  1.37.2.3   bouyer 				panic("todo(%ld) <= 0; minphys broken", todo);
    172      1.22      cgd 			if (todo > MAXPHYS)
    173  1.37.2.3   bouyer 				panic("todo(%ld) > MAXPHYS; minphys broken",
    174  1.37.2.3   bouyer 				      todo);
    175      1.22      cgd #endif
    176      1.20      cgd 
    177      1.20      cgd 			/*
    178      1.20      cgd 			 * [lock the part of the user address space involved
    179      1.20      cgd 			 *    in the transfer]
    180      1.20      cgd 			 * Beware vmapbuf(); it clobbers b_data and
    181      1.20      cgd 			 * saves it in b_saveaddr.  However, vunmapbuf()
    182      1.20      cgd 			 * restores it.
    183      1.20      cgd 			 */
    184      1.28       pk 			PHOLD(p);
    185  1.37.2.1   bouyer 			if (__predict_false(uvm_vslock(p, bp->b_data, todo,
    186  1.37.2.1   bouyer 			    (flags & B_READ) ?
    187      1.37  thorpej 			    VM_PROT_READ | VM_PROT_WRITE : VM_PROT_READ)
    188  1.37.2.1   bouyer 			    != KERN_SUCCESS)) {
    189      1.37  thorpej 				bp->b_flags |= B_ERROR;
    190      1.37  thorpej 				bp->b_error = EFAULT;
    191      1.37  thorpej 				goto after_vsunlock;
    192      1.37  thorpej 			}
    193      1.20      cgd 			vmapbuf(bp, todo);
    194      1.20      cgd 
    195      1.20      cgd 			/* [call strategy to start the transfer] */
    196      1.20      cgd 			(*strategy)(bp);
    197      1.20      cgd 
    198      1.20      cgd 			/*
    199      1.20      cgd 			 * Note that the raise/wait/lower/get error
    200      1.20      cgd 			 * steps below would be done by biowait(), but
    201      1.20      cgd 			 * we want to unlock the address space before
    202      1.20      cgd 			 * we lower the priority.
    203      1.20      cgd 			 *
    204      1.20      cgd 			 * [raise the priority level to splbio]
    205      1.20      cgd 			 */
    206      1.20      cgd 			s = splbio();
    207      1.20      cgd 
    208      1.20      cgd 			/* [wait for the transfer to complete] */
    209      1.20      cgd 			while ((bp->b_flags & B_DONE) == 0)
    210      1.20      cgd 				tsleep((caddr_t) bp, PRIBIO + 1, "physio", 0);
    211      1.20      cgd 
    212      1.23  mycroft 			/* Mark it busy again, so nobody else will use it. */
    213      1.23  mycroft 			bp->b_flags |= B_BUSY;
    214      1.23  mycroft 
    215      1.23  mycroft 			/* [lower the priority level] */
    216      1.23  mycroft 			splx(s);
    217      1.23  mycroft 
    218      1.20      cgd 			/*
    219      1.20      cgd 			 * [unlock the part of the address space previously
    220      1.20      cgd 			 *    locked]
    221      1.20      cgd 			 */
    222      1.20      cgd 			vunmapbuf(bp, todo);
    223      1.31  thorpej 			uvm_vsunlock(p, bp->b_data, todo);
    224      1.37  thorpej  after_vsunlock:
    225      1.28       pk 			PRELE(p);
    226      1.20      cgd 
    227      1.20      cgd 			/* remember error value (save a splbio/splx pair) */
    228      1.20      cgd 			if (bp->b_flags & B_ERROR)
    229      1.20      cgd 				error = (bp->b_error ? bp->b_error : EIO);
    230      1.20      cgd 
    231      1.20      cgd 			/*
    232      1.20      cgd 			 * [deduct the transfer size from the total number
    233      1.20      cgd 			 *    of data to transfer]
    234      1.20      cgd 			 */
    235      1.20      cgd 			done = bp->b_bcount - bp->b_resid;
    236      1.25  mycroft #ifdef DIAGNOSTIC
    237  1.37.2.1   bouyer 			if (__predict_false(done < 0))
    238      1.25  mycroft 				panic("done < 0; strategy broken");
    239  1.37.2.1   bouyer 			if (__predict_false(done > todo))
    240      1.25  mycroft 				panic("done > todo; strategy broken");
    241      1.25  mycroft #endif
    242      1.20      cgd 			iovp->iov_len -= done;
    243      1.34   kleink 			iovp->iov_base = (caddr_t)iovp->iov_base + done;
    244      1.32   kleink 			uio->uio_offset += done;
    245      1.32   kleink 			uio->uio_resid -= done;
    246      1.20      cgd 
    247      1.20      cgd 			/*
    248      1.20      cgd 			 * Now, check for an error.
    249      1.20      cgd 			 * Also, handle weird end-of-disk semantics.
    250      1.20      cgd 			 */
    251      1.20      cgd 			if (error || done < todo)
    252      1.20      cgd 				goto done;
    253      1.20      cgd 		}
    254      1.20      cgd 	}
    255      1.20      cgd 
    256      1.20      cgd done:
    257      1.20      cgd 	/*
    258      1.20      cgd 	 * [clean up the state of the buffer]
    259      1.20      cgd 	 * Remember if somebody wants it, so we can wake them up below.
    260      1.20      cgd 	 * Also, if we had to steal it, give it back.
    261      1.20      cgd 	 */
    262      1.20      cgd 	s = splbio();
    263      1.20      cgd 	bp->b_flags &= ~(B_BUSY | B_PHYS | B_RAW);
    264      1.20      cgd 	if (nobuf)
    265      1.20      cgd 		putphysbuf(bp);
    266      1.20      cgd 	else {
    267      1.20      cgd 		/*
    268      1.20      cgd 		 * [if another process is waiting for the raw I/O buffer,
    269      1.20      cgd 		 *    wake up processes waiting to do physical I/O;
    270      1.20      cgd 		 */
    271      1.20      cgd 		if (bp->b_flags & B_WANTED) {
    272      1.20      cgd 			bp->b_flags &= ~B_WANTED;
    273      1.20      cgd 			wakeup(bp);
    274      1.20      cgd 		}
    275      1.20      cgd 	}
    276      1.20      cgd 	splx(s);
    277      1.20      cgd 
    278      1.20      cgd 	return (error);
    279      1.20      cgd }
    280      1.20      cgd 
    281      1.20      cgd /*
    282      1.29      mrg  * allocate a buffer structure for use in physical I/O.
    283      1.20      cgd  */
    284      1.20      cgd struct buf *
    285      1.20      cgd getphysbuf()
    286      1.20      cgd {
    287      1.20      cgd 	struct buf *bp;
    288  1.37.2.1   bouyer 	int s;
    289      1.20      cgd 
    290  1.37.2.1   bouyer 	s = splbio();
    291  1.37.2.1   bouyer 	bp = pool_get(&bufpool, PR_WAITOK);
    292  1.37.2.1   bouyer 	splx(s);
    293      1.33    perry 	memset(bp, 0, sizeof(*bp));
    294      1.29      mrg 
    295  1.37.2.2   bouyer 	/* XXXCDC: is the following line necessary? */
    296      1.29      mrg 	bp->b_vnbufs.le_next = NOLIST;
    297      1.29      mrg 
    298      1.29      mrg 	return(bp);
    299      1.20      cgd }
    300      1.20      cgd 
    301      1.20      cgd /*
    302      1.29      mrg  * get rid of a swap buffer structure which has been used in physical I/O.
    303      1.20      cgd  */
    304      1.20      cgd void
    305      1.20      cgd putphysbuf(bp)
    306      1.29      mrg         struct buf *bp;
    307      1.20      cgd {
    308  1.37.2.1   bouyer 	int s;
    309      1.20      cgd 
    310      1.29      mrg 	/* XXXCDC: is this necesary? */
    311      1.29      mrg 	if (bp->b_vp)
    312      1.29      mrg 		brelvp(bp);
    313      1.29      mrg 
    314  1.37.2.1   bouyer 	if (__predict_false(bp->b_flags & B_WANTED))
    315      1.29      mrg 		panic("putphysbuf: private buf B_WANTED");
    316  1.37.2.1   bouyer 	s = splbio();
    317  1.37.2.1   bouyer 	pool_put(&bufpool, bp);
    318  1.37.2.1   bouyer 	splx(s);
    319      1.20      cgd }
    320      1.20      cgd 
    321      1.20      cgd /*
    322      1.20      cgd  * Leffler, et al., says on p. 231:
    323      1.20      cgd  * "The minphys() routine is called by physio() to adjust the
    324      1.20      cgd  * size of each I/O transfer before the latter is passed to
    325      1.24  mycroft  * the strategy routine..."
    326      1.20      cgd  *
    327      1.20      cgd  * so, just adjust the buffer's count accounting to MAXPHYS here,
    328      1.20      cgd  * and return the new count;
    329      1.20      cgd  */
    330      1.24  mycroft void
    331      1.20      cgd minphys(bp)
    332      1.20      cgd 	struct buf *bp;
    333      1.20      cgd {
    334      1.20      cgd 
    335      1.24  mycroft 	if (bp->b_bcount > MAXPHYS)
    336      1.24  mycroft 		bp->b_bcount = MAXPHYS;
    337      1.20      cgd }
    338