don't overflow doing long writes on ptc (from thomas@utah-cs)
[unix-history] / usr / src / sys / kern / kern_physio.c
CommitLineData
13849ba3 1/* kern_physio.c 6.2 83/09/09 */
961945a8
SL
2
3#include "../machine/pte.h"
663dbc72
BJ
4
5#include "../h/param.h"
6#include "../h/systm.h"
7#include "../h/dir.h"
8#include "../h/user.h"
9#include "../h/buf.h"
10#include "../h/conf.h"
11#include "../h/proc.h"
12#include "../h/seg.h"
663dbc72 13#include "../h/vm.h"
973ecc4f 14#include "../h/trace.h"
d668d9ba 15#include "../h/map.h"
d6d7360b 16#include "../h/uio.h"
663dbc72 17
663dbc72
BJ
18/*
19 * Swap IO headers -
20 * They contain the necessary information for the swap I/O.
21 * At any given time, a swap header can be in three
22 * different lists. When free it is in the free list,
23 * when allocated and the I/O queued, it is on the swap
24 * device list, and finally, if the operation was a dirty
25 * page push, when the I/O completes, it is inserted
26 * in a list of cleaned pages to be processed by the pageout daemon.
27 */
4c05b581 28struct buf *swbuf;
663dbc72 29
663dbc72
BJ
30/*
31 * swap I/O -
32 *
33 * If the flag indicates a dirty page push initiated
34 * by the pageout daemon, we map the page into the i th
35 * virtual page of process 2 (the daemon itself) where i is
36 * the index of the swap header that has been allocated.
37 * We simply initialize the header and queue the I/O but
38 * do not wait for completion. When the I/O completes,
39 * iodone() will link the header to a list of cleaned
40 * pages to be processed by the pageout daemon.
41 */
42swap(p, dblkno, addr, nbytes, rdflg, flag, dev, pfcent)
43 struct proc *p;
44 swblk_t dblkno;
45 caddr_t addr;
39d536e6 46 int nbytes, rdflg, flag;
663dbc72 47 dev_t dev;
39d536e6 48 u_int pfcent;
663dbc72
BJ
49{
50 register struct buf *bp;
e438ed8e 51 register u_int c;
663dbc72
BJ
52 int p2dp;
53 register struct pte *dpte, *vpte;
530d0032 54 int s;
d668d9ba 55 extern swdone();
663dbc72 56
530d0032 57 s = spl6();
663dbc72
BJ
58 while (bswlist.av_forw == NULL) {
59 bswlist.b_flags |= B_WANTED;
60 sleep((caddr_t)&bswlist, PSWP+1);
61 }
62 bp = bswlist.av_forw;
63 bswlist.av_forw = bp->av_forw;
530d0032 64 splx(s);
663dbc72
BJ
65
66 bp->b_flags = B_BUSY | B_PHYS | rdflg | flag;
67 if ((bp->b_flags & (B_DIRTY|B_PGIN)) == 0)
68 if (rdflg == B_READ)
69 sum.v_pswpin += btoc(nbytes);
70 else
71 sum.v_pswpout += btoc(nbytes);
72 bp->b_proc = p;
73 if (flag & B_DIRTY) {
74 p2dp = ((bp - swbuf) * CLSIZE) * KLMAX;
75 dpte = dptopte(&proc[2], p2dp);
76 vpte = vtopte(p, btop(addr));
77 for (c = 0; c < nbytes; c += NBPG) {
78 if (vpte->pg_pfnum == 0 || vpte->pg_fod)
79 panic("swap bad pte");
80 *dpte++ = *vpte++;
81 }
d668d9ba
SL
82 bp->b_un.b_addr = (caddr_t)ctob(dptov(&proc[2], p2dp));
83 bp->b_flags |= B_CALL;
84 bp->b_iodone = swdone;
85 bp->b_pfcent = pfcent;
663dbc72
BJ
86 } else
87 bp->b_un.b_addr = addr;
88 while (nbytes > 0) {
e438ed8e
BJ
89 bp->b_bcount = nbytes;
90 minphys(bp);
91 c = bp->b_bcount;
663dbc72
BJ
92 bp->b_blkno = dblkno;
93 bp->b_dev = dev;
53f9ca20
BJ
94#ifdef TRACE
95 trace(TR_SWAPIO, dev, bp->b_blkno);
96#endif
ca1f746a 97 physstrat(bp, bdevsw[major(dev)].d_strategy, PSWP);
663dbc72
BJ
98 if (flag & B_DIRTY) {
99 if (c < nbytes)
100 panic("big push");
663dbc72
BJ
101 return;
102 }
663dbc72
BJ
103 bp->b_un.b_addr += c;
104 bp->b_flags &= ~B_DONE;
105 if (bp->b_flags & B_ERROR) {
106 if ((flag & (B_UAREA|B_PAGET)) || rdflg == B_WRITE)
107 panic("hard IO err in swap");
108 swkill(p, (char *)0);
109 }
110 nbytes -= c;
919fe934 111 dblkno += btodb(c);
663dbc72 112 }
530d0032 113 s = spl6();
663dbc72
BJ
114 bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS|B_PAGET|B_UAREA|B_DIRTY);
115 bp->av_forw = bswlist.av_forw;
116 bswlist.av_forw = bp;
117 if (bswlist.b_flags & B_WANTED) {
118 bswlist.b_flags &= ~B_WANTED;
119 wakeup((caddr_t)&bswlist);
120 wakeup((caddr_t)&proc[2]);
121 }
530d0032 122 splx(s);
663dbc72
BJ
123}
124
d668d9ba
SL
125/*
126 * Put a buffer on the clean list after I/O is done.
127 * Called from biodone.
128 */
129swdone(bp)
130 register struct buf *bp;
131{
132 register int s;
133
134 if (bp->b_flags & B_ERROR)
135 panic("IO err in push");
136 s = spl6();
137 bp->av_forw = bclnlist;
138 cnt.v_pgout++;
139 cnt.v_pgpgout += bp->b_bcount / NBPG;
140 bclnlist = bp;
141 if (bswlist.b_flags & B_WANTED)
142 wakeup((caddr_t)&proc[2]);
143 splx(s);
144}
145
663dbc72
BJ
146/*
147 * If rout == 0 then killed on swap error, else
148 * rout is the name of the routine where we ran out of
149 * swap space.
150 */
151swkill(p, rout)
152 struct proc *p;
153 char *rout;
154{
444f631c 155 char *mesg;
663dbc72 156
444f631c 157 printf("pid %d: ", p->p_pid);
663dbc72 158 if (rout)
444f631c 159 printf(mesg = "killed due to no swap space\n");
663dbc72 160 else
444f631c
BJ
161 printf(mesg = "killed on swap error\n");
162 uprintf("sorry, pid %d was %s", p->p_pid, mesg);
663dbc72
BJ
163 /*
164 * To be sure no looping (e.g. in vmsched trying to
165 * swap out) mark process locked in core (as though
166 * done by user) after killing it so noone will try
167 * to swap it out.
168 */
a30d2e97 169 psignal(p, SIGKILL);
663dbc72
BJ
170 p->p_flag |= SULOCK;
171}
172
663dbc72
BJ
173/*
174 * Raw I/O. The arguments are
175 * The strategy routine for the device
176 * A buffer, which will always be a special buffer
177 * header owned exclusively by the device for this purpose
178 * The device number
179 * Read/write flag
180 * Essentially all the work is computing physical addresses and
181 * validating them.
182 * If the user has the proper access privilidges, the process is
183 * marked 'delayed unlock' and the pages involved in the I/O are
184 * faulted and locked. After the completion of the I/O, the above pages
185 * are unlocked.
186 */
d6d7360b
BJ
187physio(strat, bp, dev, rw, mincnt, uio)
188 int (*strat)();
189 register struct buf *bp;
190 dev_t dev;
191 int rw;
192 unsigned (*mincnt)();
193 struct uio *uio;
663dbc72 194{
406ddcbe 195 register struct iovec *iov = uio->uio_iov;
663dbc72
BJ
196 register int c;
197 char *a;
d6d7360b 198 int s, error = 0;
663dbc72 199
d6d7360b 200nextiov:
406ddcbe 201 if (uio->uio_iovcnt == 0)
d6d7360b 202 return (0);
406ddcbe 203 if (useracc(iov->iov_base,(u_int)iov->iov_len,rw==B_READ?B_WRITE:B_READ) == NULL)
d6d7360b 204 return (EFAULT);
530d0032 205 s = spl6();
663dbc72
BJ
206 while (bp->b_flags&B_BUSY) {
207 bp->b_flags |= B_WANTED;
208 sleep((caddr_t)bp, PRIBIO+1);
209 }
ef3b3d5a 210 splx(s);
663dbc72
BJ
211 bp->b_error = 0;
212 bp->b_proc = u.u_procp;
d6d7360b
BJ
213 bp->b_un.b_addr = iov->iov_base;
214 while (iov->iov_len > 0) {
663dbc72
BJ
215 bp->b_flags = B_BUSY | B_PHYS | rw;
216 bp->b_dev = dev;
919fe934 217 bp->b_blkno = btodb(uio->uio_offset);
d6d7360b 218 bp->b_bcount = iov->iov_len;
663dbc72
BJ
219 (*mincnt)(bp);
220 c = bp->b_bcount;
221 u.u_procp->p_flag |= SPHYSIO;
222 vslock(a = bp->b_un.b_addr, c);
e438ed8e 223 physstrat(bp, strat, PRIBIO);
81263dba 224 (void) spl6();
663dbc72
BJ
225 vsunlock(a, c, rw);
226 u.u_procp->p_flag &= ~SPHYSIO;
227 if (bp->b_flags&B_WANTED)
228 wakeup((caddr_t)bp);
530d0032 229 splx(s);
d6d7360b 230 c -= bp->b_resid;
663dbc72 231 bp->b_un.b_addr += c;
d6d7360b
BJ
232 iov->iov_len -= c;
233 uio->uio_resid -= c;
234 uio->uio_offset += c;
961945a8 235 /* temp kludge for tape drives */
35a494b8 236 if (bp->b_resid || (bp->b_flags&B_ERROR))
52a593fa 237 break;
663dbc72
BJ
238 }
239 bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS);
d6d7360b 240 error = geterror(bp);
961945a8
SL
241 /* temp kludge for tape drives */
242 if (bp->b_resid || error)
d6d7360b 243 return (error);
d6d7360b
BJ
244 uio->uio_iov++;
245 uio->uio_iovcnt--;
246 goto nextiov;
663dbc72
BJ
247}
248
35a494b8
SL
249#define MAXPHYS (63 * 1024)
250
663dbc72
BJ
251unsigned
252minphys(bp)
d6d7360b 253 struct buf *bp;
663dbc72
BJ
254{
255
35a494b8
SL
256 if (bp->b_bcount > MAXPHYS)
257 bp->b_bcount = MAXPHYS;
663dbc72 258}