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