xref: /csrg-svn/sys/kern/kern_physio.c (revision 48414)
123461Smckusick /*
245730Smckusick  * Copyright (c) 1982, 1986, 1990 Regents of the University of California.
3*48414Skarels  * All rights reserved.  The Berkeley software License Agreement
4*48414Skarels  * specifies the terms and conditions for redistribution.
523461Smckusick  *
6*48414Skarels  *	@(#)kern_physio.c	7.18 (Berkeley) 04/20/91
723461Smckusick  */
88Sbill 
917108Sbloom #include "param.h"
1017108Sbloom #include "systm.h"
1117108Sbloom #include "buf.h"
1217108Sbloom #include "conf.h"
1317108Sbloom #include "proc.h"
1417108Sbloom #include "seg.h"
1517108Sbloom #include "trace.h"
1617108Sbloom #include "map.h"
1737729Smckusick #include "vnode.h"
1840652Smckusick #include "specdev.h"
198Sbill 
20*48414Skarels #ifdef HPUXCOMPAT
21*48414Skarels #include "user.h"
22*48414Skarels #endif
23*48414Skarels 
24*48414Skarels static	struct buf *getswbuf();
25*48414Skarels static	freeswbuf();
26*48414Skarels 
2791Sbill /*
288Sbill  * Raw I/O. The arguments are
298Sbill  *	The strategy routine for the device
3034215Sbostic  *	A buffer, which will either be a special buffer header owned
3134215Sbostic  *	    exclusively by the device for this purpose, or NULL,
3234215Sbostic  *	    indicating that we should use a swap buffer
338Sbill  *	The device number
348Sbill  *	Read/write flag
358Sbill  * Essentially all the work is computing physical addresses and
368Sbill  * validating them.
378Sbill  * If the user has the proper access privilidges, the process is
388Sbill  * marked 'delayed unlock' and the pages involved in the I/O are
398Sbill  * faulted and locked. After the completion of the I/O, the above pages
408Sbill  * are unlocked.
418Sbill  */
427724Swnj physio(strat, bp, dev, rw, mincnt, uio)
437724Swnj 	int (*strat)();
447724Swnj 	register struct buf *bp;
457724Swnj 	dev_t dev;
467724Swnj 	int rw;
4734215Sbostic 	u_int (*mincnt)();
487724Swnj 	struct uio *uio;
498Sbill {
5017313Skarels 	register struct iovec *iov;
5138794Skarels 	register int requested, done;
5247540Skarels 	register struct proc *p = curproc;
538Sbill 	char *a;
5434215Sbostic 	int s, allocbuf = 0, error = 0;
558Sbill 
5634215Sbostic 	if (bp == NULL) {
5734215Sbostic 		allocbuf = 1;
5834215Sbostic 		bp = getswbuf(PRIBIO+1);
5934215Sbostic 	}
6034215Sbostic 	for (; uio->uio_iovcnt; uio->uio_iov++, uio->uio_iovcnt--) {
6130750Skarels 		iov = uio->uio_iov;
6234215Sbostic 		if (!useracc(iov->iov_base, (u_int)iov->iov_len,
6334215Sbostic 		    rw == B_READ ? B_WRITE : B_READ)) {
6434215Sbostic 			error = EFAULT;
6534215Sbostic 			break;
6630750Skarels 		}
6734215Sbostic 		if (!allocbuf) {	/* only if sharing caller's buffer */
6834215Sbostic 			s = splbio();
6934215Sbostic 			while (bp->b_flags&B_BUSY) {
7034215Sbostic 				bp->b_flags |= B_WANTED;
7134215Sbostic 				sleep((caddr_t)bp, PRIBIO+1);
7234215Sbostic 			}
7334215Sbostic 			splx(s);
7434215Sbostic 		}
7530750Skarels 		bp->b_error = 0;
7647540Skarels 		bp->b_proc = p;
7742001Smckusick #ifdef HPUXCOMPAT
7842001Smckusick 		if (ISHPMMADDR(iov->iov_base))
7942001Smckusick 			bp->b_un.b_addr = (caddr_t)HPMMBASEADDR(iov->iov_base);
8042001Smckusick 		else
8142001Smckusick #endif
8230750Skarels 		bp->b_un.b_addr = iov->iov_base;
8330750Skarels 		while (iov->iov_len > 0) {
8434215Sbostic 			bp->b_flags = B_BUSY | B_PHYS | B_RAW | rw;
8530750Skarels 			bp->b_dev = dev;
8630750Skarels 			bp->b_blkno = btodb(uio->uio_offset);
8730750Skarels 			bp->b_bcount = iov->iov_len;
8830750Skarels 			(*mincnt)(bp);
8938794Skarels 			requested = bp->b_bcount;
9047540Skarels 			p->p_flag |= SPHYSIO;
9138794Skarels 			vslock(a = bp->b_un.b_addr, requested);
9244773Swilliam #if defined(hp300) || defined(i386)
9342001Smckusick 			vmapbuf(bp);
9442001Smckusick #endif
9534215Sbostic 			(*strat)(bp);
9634215Sbostic 			s = splbio();
9734215Sbostic 			while ((bp->b_flags & B_DONE) == 0)
9834215Sbostic 				sleep((caddr_t)bp, PRIBIO);
9944773Swilliam #if defined(hp300) || defined(i386)
10042001Smckusick 			vunmapbuf(bp);
10142001Smckusick #endif
10238794Skarels 			vsunlock(a, requested, rw);
10347540Skarels 			p->p_flag &= ~SPHYSIO;
10434215Sbostic 			if (bp->b_flags&B_WANTED)	/* rare */
10530750Skarels 				wakeup((caddr_t)bp);
10630750Skarels 			splx(s);
10738794Skarels 			done = bp->b_bcount - bp->b_resid;
10838794Skarels 			bp->b_un.b_addr += done;
10938794Skarels 			iov->iov_len -= done;
11038794Skarels 			uio->uio_resid -= done;
11138794Skarels 			uio->uio_offset += done;
11238794Skarels 			/* temp kludge for disk drives */
11338794Skarels 			if (done < requested || bp->b_flags & B_ERROR)
11430750Skarels 				break;
11530750Skarels 		}
11634215Sbostic 		bp->b_flags &= ~(B_BUSY | B_WANTED | B_PHYS | B_RAW);
11737729Smckusick 		error = biowait(bp);
11838794Skarels 		/* temp kludge for disk drives */
11938794Skarels 		if (done < requested || bp->b_flags & B_ERROR)
12034215Sbostic 			break;
1218Sbill 	}
12242001Smckusick #if defined(hp300)
12342001Smckusick 	DCIU();
12442001Smckusick #endif
12534215Sbostic 	if (allocbuf)
12634215Sbostic 		freeswbuf(bp);
12734215Sbostic 	return (error);
1288Sbill }
1298Sbill 
13034215Sbostic u_int
1318Sbill minphys(bp)
1327724Swnj 	struct buf *bp;
1338Sbill {
13410400Ssam 	if (bp->b_bcount > MAXPHYS)
13510400Ssam 		bp->b_bcount = MAXPHYS;
1368Sbill }
13734215Sbostic 
13834215Sbostic static
13934215Sbostic struct buf *
14034215Sbostic getswbuf(prio)
14134215Sbostic 	int prio;
14234215Sbostic {
14334215Sbostic 	int s;
14434215Sbostic 	struct buf *bp;
14534215Sbostic 
14634215Sbostic 	s = splbio();
14734215Sbostic 	while (bswlist.av_forw == NULL) {
14834215Sbostic 		bswlist.b_flags |= B_WANTED;
14934215Sbostic 		sleep((caddr_t)&bswlist, prio);
15034215Sbostic 	}
15134215Sbostic 	bp = bswlist.av_forw;
15234215Sbostic 	bswlist.av_forw = bp->av_forw;
15334215Sbostic 	splx(s);
15434215Sbostic 	return (bp);
15534215Sbostic }
15634215Sbostic 
15734215Sbostic static
15834215Sbostic freeswbuf(bp)
15934215Sbostic 	struct buf *bp;
16034215Sbostic {
16134215Sbostic 	int s;
16234215Sbostic 
16334215Sbostic 	s = splbio();
16434215Sbostic 	bp->av_forw = bswlist.av_forw;
16534215Sbostic 	bswlist.av_forw = bp;
16639148Smckusick 	if (bp->b_vp)
16739148Smckusick 		brelvp(bp);
16834215Sbostic 	if (bswlist.b_flags & B_WANTED) {
16934215Sbostic 		bswlist.b_flags &= ~B_WANTED;
17034215Sbostic 		wakeup((caddr_t)&bswlist);
17147540Skarels 		wakeup((caddr_t)pageproc);
17234215Sbostic 	}
17334215Sbostic 	splx(s);
17434215Sbostic }
17534215Sbostic 
17634215Sbostic rawread(dev, uio)
17734215Sbostic 	dev_t dev;
17834215Sbostic 	struct uio *uio;
17934215Sbostic {
18034215Sbostic 	return (physio(cdevsw[major(dev)].d_strategy, (struct buf *)NULL,
18134215Sbostic 	    dev, B_READ, minphys, uio));
18234215Sbostic }
18334215Sbostic 
18434215Sbostic rawwrite(dev, uio)
18534215Sbostic 	dev_t dev;
18634215Sbostic 	struct uio *uio;
18734215Sbostic {
18834215Sbostic 	return (physio(cdevsw[major(dev)].d_strategy, (struct buf *)NULL,
18934215Sbostic 	    dev, B_WRITE, minphys, uio));
19034215Sbostic }
191