nfs_bio.c revision 1.44.2.1 1 1.44.2.1 chs /* $NetBSD: nfs_bio.c,v 1.44.2.1 1998/11/09 06:06:33 chs Exp $ */
2 1.15 cgd
3 1.1 cgd /*
4 1.12 mycroft * Copyright (c) 1989, 1993
5 1.12 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Rick Macklem at The University of Guelph.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.1 cgd * 3. All advertising materials mentioning features or use of this software
19 1.1 cgd * must display the following acknowledgement:
20 1.1 cgd * This product includes software developed by the University of
21 1.1 cgd * California, Berkeley and its contributors.
22 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
23 1.1 cgd * may be used to endorse or promote products derived from this software
24 1.1 cgd * without specific prior written permission.
25 1.1 cgd *
26 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 cgd * SUCH DAMAGE.
37 1.1 cgd *
38 1.24 fvdl * @(#)nfs_bio.c 8.9 (Berkeley) 3/30/95
39 1.1 cgd */
40 1.1 cgd
41 1.42 mrg #include "opt_uvm.h"
42 1.24 fvdl
43 1.8 mycroft #include <sys/param.h>
44 1.8 mycroft #include <sys/systm.h>
45 1.12 mycroft #include <sys/resourcevar.h>
46 1.24 fvdl #include <sys/signalvar.h>
47 1.8 mycroft #include <sys/proc.h>
48 1.8 mycroft #include <sys/buf.h>
49 1.8 mycroft #include <sys/vnode.h>
50 1.8 mycroft #include <sys/trace.h>
51 1.8 mycroft #include <sys/mount.h>
52 1.12 mycroft #include <sys/kernel.h>
53 1.23 christos #include <sys/namei.h>
54 1.34 fvdl #include <sys/dirent.h>
55 1.12 mycroft
56 1.12 mycroft #include <vm/vm.h>
57 1.1 cgd
58 1.41 mrg #if defined(UVM)
59 1.44.2.1 chs #include <uvm/uvm.h>
60 1.44.2.1 chs #include <sys/malloc.h>
61 1.44.2.1 chs
62 1.44.2.1 chs #ifdef UBC
63 1.44.2.1 chs UVMHIST_DECL(ubchist);
64 1.44.2.1 chs #endif
65 1.41 mrg #endif
66 1.41 mrg
67 1.12 mycroft #include <nfs/rpcv2.h>
68 1.24 fvdl #include <nfs/nfsproto.h>
69 1.8 mycroft #include <nfs/nfs.h>
70 1.8 mycroft #include <nfs/nfsmount.h>
71 1.12 mycroft #include <nfs/nqnfs.h>
72 1.24 fvdl #include <nfs/nfsnode.h>
73 1.23 christos #include <nfs/nfs_var.h>
74 1.1 cgd
75 1.12 mycroft extern int nfs_numasync;
76 1.24 fvdl extern struct nfsstats nfsstats;
77 1.1 cgd
78 1.1 cgd /*
79 1.1 cgd * Vnode op for read using bio
80 1.1 cgd * Any similarity to readip() is purely coincidental
81 1.1 cgd */
82 1.23 christos int
83 1.34 fvdl nfs_bioread(vp, uio, ioflag, cred, cflag)
84 1.1 cgd register struct vnode *vp;
85 1.1 cgd register struct uio *uio;
86 1.34 fvdl int ioflag, cflag;
87 1.1 cgd struct ucred *cred;
88 1.1 cgd {
89 1.1 cgd register struct nfsnode *np = VTONFS(vp);
90 1.34 fvdl register int biosize, diff;
91 1.23 christos struct buf *bp = NULL, *rabp;
92 1.1 cgd struct vattr vattr;
93 1.12 mycroft struct proc *p;
94 1.24 fvdl struct nfsmount *nmp = VFSTONFS(vp->v_mount);
95 1.35 fvdl struct nfsdircache *ndp = NULL, *nndp = NULL;
96 1.12 mycroft daddr_t lbn, bn, rabn;
97 1.34 fvdl caddr_t baddr, ep, edp;
98 1.34 fvdl int got_buf = 0, nra, error = 0, n = 0, on = 0, not_readin, en, enn;
99 1.34 fvdl int enough = 0;
100 1.34 fvdl struct dirent *dp, *pdp;
101 1.43 fvdl off_t curoff = 0, offdiff;
102 1.1 cgd
103 1.1 cgd #ifdef DIAGNOSTIC
104 1.1 cgd if (uio->uio_rw != UIO_READ)
105 1.1 cgd panic("nfs_read mode");
106 1.1 cgd #endif
107 1.1 cgd if (uio->uio_resid == 0)
108 1.1 cgd return (0);
109 1.34 fvdl if (vp->v_type != VDIR && uio->uio_offset < 0)
110 1.1 cgd return (EINVAL);
111 1.24 fvdl p = uio->uio_procp;
112 1.34 fvdl if ((nmp->nm_flag & NFSMNT_NFSV3) &&
113 1.34 fvdl !(nmp->nm_iflag & NFSMNT_GOTFSINFO))
114 1.24 fvdl (void)nfs_fsinfo(nmp, vp, cred, p);
115 1.34 fvdl if (vp->v_type != VDIR &&
116 1.34 fvdl (uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
117 1.33 fvdl return (EFBIG);
118 1.12 mycroft biosize = nmp->nm_rsize;
119 1.1 cgd /*
120 1.12 mycroft * For nfs, cache consistency can only be maintained approximately.
121 1.12 mycroft * Although RFC1094 does not specify the criteria, the following is
122 1.12 mycroft * believed to be compatible with the reference port.
123 1.12 mycroft * For nqnfs, full cache consistency is maintained within the loop.
124 1.12 mycroft * For nfs:
125 1.1 cgd * If the file's modify time on the server has changed since the
126 1.1 cgd * last read rpc or you have written to the file,
127 1.1 cgd * you may have lost data cache consistency with the
128 1.1 cgd * server, so flush all of the file's data out of the cache.
129 1.1 cgd * Then force a getattr rpc to ensure that you have up to date
130 1.1 cgd * attributes.
131 1.1 cgd * NB: This implies that cache data can be read when up to
132 1.1 cgd * NFS_ATTRTIMEO seconds out of date. If you find that you need current
133 1.1 cgd * attributes this could be forced by setting n_attrstamp to 0 before
134 1.12 mycroft * the VOP_GETATTR() call.
135 1.1 cgd */
136 1.12 mycroft if ((nmp->nm_flag & NFSMNT_NQNFS) == 0 && vp->v_type != VLNK) {
137 1.1 cgd if (np->n_flag & NMODIFIED) {
138 1.24 fvdl if (vp->v_type != VREG) {
139 1.24 fvdl if (vp->v_type != VDIR)
140 1.24 fvdl panic("nfs: bioread, not dir");
141 1.35 fvdl nfs_invaldircache(vp, 0);
142 1.35 fvdl np->n_direofoffset = 0;
143 1.23 christos error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
144 1.23 christos if (error)
145 1.12 mycroft return (error);
146 1.12 mycroft }
147 1.1 cgd np->n_attrstamp = 0;
148 1.23 christos error = VOP_GETATTR(vp, &vattr, cred, p);
149 1.23 christos if (error)
150 1.1 cgd return (error);
151 1.22 jtc np->n_mtime = vattr.va_mtime.tv_sec;
152 1.1 cgd } else {
153 1.24 fvdl error = VOP_GETATTR(vp, &vattr, cred, p);
154 1.24 fvdl if (error)
155 1.1 cgd return (error);
156 1.22 jtc if (np->n_mtime != vattr.va_mtime.tv_sec) {
157 1.35 fvdl if (vp->v_type == VDIR) {
158 1.35 fvdl nfs_invaldircache(vp, 0);
159 1.35 fvdl np->n_direofoffset = 0;
160 1.35 fvdl }
161 1.23 christos error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
162 1.23 christos if (error)
163 1.12 mycroft return (error);
164 1.22 jtc np->n_mtime = vattr.va_mtime.tv_sec;
165 1.1 cgd }
166 1.1 cgd }
167 1.1 cgd }
168 1.1 cgd do {
169 1.12 mycroft
170 1.12 mycroft /*
171 1.12 mycroft * Get a valid lease. If cached data is stale, flush it.
172 1.12 mycroft */
173 1.12 mycroft if (nmp->nm_flag & NFSMNT_NQNFS) {
174 1.24 fvdl if (NQNFS_CKINVALID(vp, np, ND_READ)) {
175 1.12 mycroft do {
176 1.24 fvdl error = nqnfs_getlease(vp, ND_READ, cred, p);
177 1.12 mycroft } while (error == NQNFS_EXPIRED);
178 1.12 mycroft if (error)
179 1.12 mycroft return (error);
180 1.12 mycroft if (np->n_lrev != np->n_brev ||
181 1.12 mycroft (np->n_flag & NQNFSNONCACHE) ||
182 1.12 mycroft ((np->n_flag & NMODIFIED) && vp->v_type == VDIR)) {
183 1.35 fvdl if (vp->v_type == VDIR) {
184 1.35 fvdl nfs_invaldircache(vp, 0);
185 1.35 fvdl np->n_direofoffset = 0;
186 1.35 fvdl }
187 1.23 christos error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
188 1.23 christos if (error)
189 1.12 mycroft return (error);
190 1.12 mycroft np->n_brev = np->n_lrev;
191 1.12 mycroft }
192 1.12 mycroft } else if (vp->v_type == VDIR && (np->n_flag & NMODIFIED)) {
193 1.35 fvdl nfs_invaldircache(vp, 0);
194 1.23 christos error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
195 1.35 fvdl np->n_direofoffset = 0;
196 1.23 christos if (error)
197 1.12 mycroft return (error);
198 1.12 mycroft }
199 1.12 mycroft }
200 1.26 fvdl /*
201 1.26 fvdl * Don't cache symlinks.
202 1.26 fvdl */
203 1.26 fvdl if (np->n_flag & NQNFSNONCACHE
204 1.26 fvdl || ((vp->v_flag & VROOT) && vp->v_type == VLNK)) {
205 1.12 mycroft switch (vp->v_type) {
206 1.12 mycroft case VREG:
207 1.24 fvdl return (nfs_readrpc(vp, uio, cred));
208 1.12 mycroft case VLNK:
209 1.24 fvdl return (nfs_readlinkrpc(vp, uio, cred));
210 1.12 mycroft case VDIR:
211 1.23 christos break;
212 1.24 fvdl default:
213 1.29 christos printf(" NQNFSNONCACHE: type %x unexpected\n",
214 1.28 christos vp->v_type);
215 1.12 mycroft };
216 1.12 mycroft }
217 1.12 mycroft baddr = (caddr_t)0;
218 1.1 cgd switch (vp->v_type) {
219 1.1 cgd case VREG:
220 1.1 cgd nfsstats.biocache_reads++;
221 1.1 cgd lbn = uio->uio_offset / biosize;
222 1.24 fvdl on = uio->uio_offset & (biosize - 1);
223 1.12 mycroft bn = lbn * (biosize / DEV_BSIZE);
224 1.12 mycroft not_readin = 1;
225 1.12 mycroft
226 1.44.2.1 chs #ifdef UBC
227 1.44.2.1 chs offdiff = nra = rabn = diff = 0;
228 1.44.2.1 chs error = 0;
229 1.44.2.1 chs while (uio->uio_resid > 0) {
230 1.44.2.1 chs void *win;
231 1.44.2.1 chs int byteoff = uio->uio_offset & (MAXBSIZE - 1);
232 1.44.2.1 chs int bytelen = min(np->n_size - uio->uio_offset,
233 1.44.2.1 chs min(uio->uio_resid,
234 1.44.2.1 chs MAXBSIZE - byteoff));
235 1.44.2.1 chs
236 1.44.2.1 chs if (bytelen == 0)
237 1.44.2.1 chs break;
238 1.44.2.1 chs win = ubc_alloc(&vp->v_uvm.u_obj, uio->uio_offset,
239 1.44.2.1 chs bytelen, UBC_READ);
240 1.44.2.1 chs if (win == NULL)
241 1.44.2.1 chs panic("nfs_bioread: ubc_alloc -> NULL");
242 1.44.2.1 chs
243 1.44.2.1 chs error = uiomove(win, bytelen, uio);
244 1.44.2.1 chs ubc_release(win, 0);
245 1.44.2.1 chs if (error)
246 1.44.2.1 chs break;
247 1.44.2.1 chs }
248 1.44.2.1 chs #else
249 1.12 mycroft /*
250 1.12 mycroft * Start the read ahead(s), as required.
251 1.12 mycroft */
252 1.36 fvdl if (nfs_numasync > 0 && nmp->nm_readahead > 0 &&
253 1.36 fvdl lbn - 1 == vp->v_lastr) {
254 1.12 mycroft for (nra = 0; nra < nmp->nm_readahead &&
255 1.12 mycroft (lbn + 1 + nra) * biosize < np->n_size; nra++) {
256 1.12 mycroft rabn = (lbn + 1 + nra) * (biosize / DEV_BSIZE);
257 1.12 mycroft if (!incore(vp, rabn)) {
258 1.12 mycroft rabp = nfs_getcacheblk(vp, rabn, biosize, p);
259 1.12 mycroft if (!rabp)
260 1.12 mycroft return (EINTR);
261 1.12 mycroft if ((rabp->b_flags & (B_DELWRI | B_DONE)) == 0) {
262 1.12 mycroft rabp->b_flags |= (B_READ | B_ASYNC);
263 1.12 mycroft if (nfs_asyncio(rabp, cred)) {
264 1.12 mycroft rabp->b_flags |= B_INVAL;
265 1.12 mycroft brelse(rabp);
266 1.12 mycroft }
267 1.19 mycroft } else
268 1.19 mycroft brelse(rabp);
269 1.12 mycroft }
270 1.12 mycroft }
271 1.12 mycroft }
272 1.12 mycroft
273 1.12 mycroft /*
274 1.12 mycroft * If the block is in the cache and has the required data
275 1.12 mycroft * in a valid region, just copy it out.
276 1.12 mycroft * Otherwise, get the block and write back/read in,
277 1.12 mycroft * as required.
278 1.12 mycroft */
279 1.12 mycroft if ((bp = incore(vp, bn)) &&
280 1.12 mycroft (bp->b_flags & (B_BUSY | B_WRITEINPROG)) ==
281 1.12 mycroft (B_BUSY | B_WRITEINPROG))
282 1.12 mycroft got_buf = 0;
283 1.12 mycroft else {
284 1.12 mycroft again:
285 1.12 mycroft bp = nfs_getcacheblk(vp, bn, biosize, p);
286 1.12 mycroft if (!bp)
287 1.12 mycroft return (EINTR);
288 1.12 mycroft got_buf = 1;
289 1.12 mycroft if ((bp->b_flags & (B_DONE | B_DELWRI)) == 0) {
290 1.12 mycroft bp->b_flags |= B_READ;
291 1.12 mycroft not_readin = 0;
292 1.23 christos error = nfs_doio(bp, cred, p);
293 1.23 christos if (error) {
294 1.12 mycroft brelse(bp);
295 1.12 mycroft return (error);
296 1.12 mycroft }
297 1.12 mycroft }
298 1.12 mycroft }
299 1.11 cgd n = min((unsigned)(biosize - on), uio->uio_resid);
300 1.43 fvdl offdiff = np->n_size - uio->uio_offset;
301 1.43 fvdl if (offdiff < (off_t)n)
302 1.43 fvdl n = (int)offdiff;
303 1.12 mycroft if (not_readin && n > 0) {
304 1.12 mycroft if (on < bp->b_validoff || (on + n) > bp->b_validend) {
305 1.12 mycroft if (!got_buf) {
306 1.12 mycroft bp = nfs_getcacheblk(vp, bn, biosize, p);
307 1.12 mycroft if (!bp)
308 1.12 mycroft return (EINTR);
309 1.12 mycroft got_buf = 1;
310 1.12 mycroft }
311 1.24 fvdl bp->b_flags |= B_INVAFTERWRITE;
312 1.12 mycroft if (bp->b_dirtyend > 0) {
313 1.12 mycroft if ((bp->b_flags & B_DELWRI) == 0)
314 1.12 mycroft panic("nfsbioread");
315 1.12 mycroft if (VOP_BWRITE(bp) == EINTR)
316 1.12 mycroft return (EINTR);
317 1.12 mycroft } else
318 1.12 mycroft brelse(bp);
319 1.12 mycroft goto again;
320 1.12 mycroft }
321 1.12 mycroft }
322 1.1 cgd vp->v_lastr = lbn;
323 1.12 mycroft diff = (on >= bp->b_validend) ? 0 : (bp->b_validend - on);
324 1.12 mycroft if (diff < n)
325 1.12 mycroft n = diff;
326 1.44.2.1 chs #endif
327 1.1 cgd break;
328 1.1 cgd case VLNK:
329 1.1 cgd nfsstats.biocache_readlinks++;
330 1.12 mycroft bp = nfs_getcacheblk(vp, (daddr_t)0, NFS_MAXPATHLEN, p);
331 1.12 mycroft if (!bp)
332 1.12 mycroft return (EINTR);
333 1.12 mycroft if ((bp->b_flags & B_DONE) == 0) {
334 1.12 mycroft bp->b_flags |= B_READ;
335 1.24 fvdl error = nfs_doio(bp, cred, p);
336 1.24 fvdl if (error) {
337 1.12 mycroft brelse(bp);
338 1.12 mycroft return (error);
339 1.12 mycroft }
340 1.12 mycroft }
341 1.12 mycroft n = min(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid);
342 1.12 mycroft got_buf = 1;
343 1.1 cgd on = 0;
344 1.1 cgd break;
345 1.1 cgd case VDIR:
346 1.34 fvdl diragain:
347 1.34 fvdl nfsstats.biocache_readdirs++;
348 1.35 fvdl ndp = nfs_searchdircache(vp, uio->uio_offset,
349 1.35 fvdl (nmp->nm_flag & NFSMNT_XLATECOOKIE), 0);
350 1.35 fvdl if (!ndp) {
351 1.35 fvdl /*
352 1.35 fvdl * We've been handed a cookie that is not
353 1.35 fvdl * in the cache. If we're not translating
354 1.35 fvdl * 32 <-> 64, it may be a value that was
355 1.35 fvdl * flushed out of the cache because it grew
356 1.35 fvdl * too big. Let the server judge if it's
357 1.35 fvdl * valid or not. In the translation case,
358 1.35 fvdl * we have no way of validating this value,
359 1.35 fvdl * so punt.
360 1.35 fvdl */
361 1.35 fvdl if (nmp->nm_flag & NFSMNT_XLATECOOKIE)
362 1.35 fvdl return (EINVAL);
363 1.35 fvdl ndp = nfs_enterdircache(vp, uio->uio_offset,
364 1.35 fvdl uio->uio_offset, 0, 0);
365 1.35 fvdl }
366 1.35 fvdl
367 1.34 fvdl if (uio->uio_offset != 0 &&
368 1.35 fvdl ndp->dc_cookie == np->n_direofoffset) {
369 1.35 fvdl nfsstats.direofcache_hits++;
370 1.18 mycroft return (0);
371 1.35 fvdl }
372 1.35 fvdl
373 1.34 fvdl bp = nfs_getcacheblk(vp, ndp->dc_blkno, NFS_DIRBLKSIZ, p);
374 1.12 mycroft if (!bp)
375 1.24 fvdl return (EINTR);
376 1.12 mycroft if ((bp->b_flags & B_DONE) == 0) {
377 1.24 fvdl bp->b_flags |= B_READ;
378 1.35 fvdl bp->b_dcookie = ndp->dc_blkcookie;
379 1.24 fvdl error = nfs_doio(bp, cred, p);
380 1.24 fvdl if (error) {
381 1.34 fvdl /*
382 1.34 fvdl * Yuck! The directory has been modified on the
383 1.34 fvdl * server. Punt and let the userland code
384 1.34 fvdl * deal with it.
385 1.34 fvdl */
386 1.24 fvdl brelse(bp);
387 1.34 fvdl if (error == NFSERR_BAD_COOKIE) {
388 1.35 fvdl nfs_invaldircache(vp, 0);
389 1.34 fvdl nfs_vinvalbuf(vp, 0, cred, p, 1);
390 1.34 fvdl error = EINVAL;
391 1.12 mycroft }
392 1.34 fvdl return (error);
393 1.38 fvdl }
394 1.40 fvdl }
395 1.40 fvdl
396 1.40 fvdl /*
397 1.40 fvdl * Just return if we hit EOF right away with this
398 1.40 fvdl * block. Always check here, because direofoffset
399 1.40 fvdl * may have been set by an nfsiod since the last
400 1.40 fvdl * check.
401 1.40 fvdl */
402 1.40 fvdl if (np->n_direofoffset != 0 &&
403 1.39 fvdl ndp->dc_blkcookie == np->n_direofoffset) {
404 1.40 fvdl brelse(bp);
405 1.40 fvdl return (0);
406 1.12 mycroft }
407 1.12 mycroft
408 1.12 mycroft /*
409 1.34 fvdl * Find the entry we were looking for in the block.
410 1.34 fvdl */
411 1.34 fvdl
412 1.34 fvdl en = ndp->dc_entry;
413 1.34 fvdl
414 1.34 fvdl pdp = dp = (struct dirent *)bp->b_data;
415 1.34 fvdl edp = bp->b_data + bp->b_validend;
416 1.34 fvdl enn = 0;
417 1.34 fvdl while (enn < en && (caddr_t)dp < edp) {
418 1.34 fvdl pdp = dp;
419 1.34 fvdl dp = (struct dirent *)((caddr_t)dp + dp->d_reclen);
420 1.34 fvdl enn++;
421 1.34 fvdl }
422 1.34 fvdl
423 1.34 fvdl /*
424 1.34 fvdl * If the entry number was bigger than the number of
425 1.34 fvdl * entries in the block, or the cookie of the previous
426 1.34 fvdl * entry doesn't match, the directory cache is
427 1.34 fvdl * stale. Flush it and try again (i.e. go to
428 1.34 fvdl * the server).
429 1.34 fvdl */
430 1.34 fvdl if ((caddr_t)dp >= edp || (caddr_t)dp + dp->d_reclen > edp ||
431 1.35 fvdl (en > 0 && NFS_GETCOOKIE(pdp) != ndp->dc_cookie)) {
432 1.34 fvdl #ifdef DEBUG
433 1.37 thorpej printf("invalid cache: %p %p %p off %lx %lx\n",
434 1.37 thorpej pdp, dp, edp,
435 1.34 fvdl (unsigned long)uio->uio_offset,
436 1.34 fvdl (unsigned long)NFS_GETCOOKIE(pdp));
437 1.34 fvdl #endif
438 1.34 fvdl brelse(bp);
439 1.35 fvdl nfs_invaldircache(vp, 0);
440 1.34 fvdl nfs_vinvalbuf(vp, 0, cred, p, 0);
441 1.34 fvdl goto diragain;
442 1.34 fvdl }
443 1.34 fvdl
444 1.34 fvdl on = (caddr_t)dp - bp->b_data;
445 1.34 fvdl
446 1.34 fvdl /*
447 1.34 fvdl * Cache all entries that may be exported to the
448 1.34 fvdl * user, as they may be thrown back at us. The
449 1.34 fvdl * NFSBIO_CACHECOOKIES flag indicates that all
450 1.34 fvdl * entries are being 'exported', so cache them all.
451 1.34 fvdl */
452 1.34 fvdl
453 1.34 fvdl if (en == 0 && pdp == dp) {
454 1.34 fvdl dp = (struct dirent *)
455 1.34 fvdl ((caddr_t)dp + dp->d_reclen);
456 1.34 fvdl enn++;
457 1.34 fvdl }
458 1.34 fvdl
459 1.34 fvdl if (uio->uio_resid < (bp->b_validend - on)) {
460 1.34 fvdl n = uio->uio_resid;
461 1.34 fvdl enough = 1;
462 1.34 fvdl } else
463 1.34 fvdl n = bp->b_validend - on;
464 1.34 fvdl
465 1.34 fvdl ep = bp->b_data + on + n;
466 1.34 fvdl
467 1.34 fvdl /*
468 1.34 fvdl * Find last complete entry to copy, caching entries
469 1.34 fvdl * (if requested) as we go.
470 1.34 fvdl */
471 1.34 fvdl
472 1.34 fvdl while ((caddr_t)dp < ep && (caddr_t)dp + dp->d_reclen <= ep) {
473 1.35 fvdl if (cflag & NFSBIO_CACHECOOKIES) {
474 1.35 fvdl nndp = nfs_enterdircache(vp, NFS_GETCOOKIE(pdp),
475 1.35 fvdl ndp->dc_blkcookie, enn, bp->b_lblkno);
476 1.35 fvdl if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
477 1.35 fvdl NFS_STASHCOOKIE32(pdp,
478 1.35 fvdl nndp->dc_cookie32);
479 1.35 fvdl }
480 1.35 fvdl }
481 1.34 fvdl pdp = dp;
482 1.34 fvdl dp = (struct dirent *)((caddr_t)dp + dp->d_reclen);
483 1.34 fvdl enn++;
484 1.34 fvdl }
485 1.34 fvdl
486 1.34 fvdl /*
487 1.34 fvdl * If the last requested entry was not the last in the
488 1.34 fvdl * buffer (happens if NFS_DIRFRAGSIZ < NFS_DIRBLKSIZ),
489 1.34 fvdl * cache the cookie of the last requested one, and
490 1.34 fvdl * set of the offset to it.
491 1.34 fvdl */
492 1.34 fvdl
493 1.34 fvdl if ((on + n) < bp->b_validend) {
494 1.34 fvdl curoff = NFS_GETCOOKIE(pdp);
495 1.35 fvdl nndp = nfs_enterdircache(vp, curoff, ndp->dc_blkcookie,
496 1.35 fvdl enn, bp->b_lblkno);
497 1.35 fvdl if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
498 1.35 fvdl NFS_STASHCOOKIE32(pdp, nndp->dc_cookie32);
499 1.35 fvdl curoff = nndp->dc_cookie32;
500 1.35 fvdl }
501 1.34 fvdl } else
502 1.34 fvdl curoff = bp->b_dcookie;
503 1.34 fvdl
504 1.35 fvdl /*
505 1.35 fvdl * Always cache the entry for the next block,
506 1.35 fvdl * so that readaheads can use it.
507 1.35 fvdl */
508 1.35 fvdl nndp = nfs_enterdircache(vp, bp->b_dcookie, bp->b_dcookie, 0,0);
509 1.35 fvdl if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
510 1.35 fvdl if (curoff == bp->b_dcookie) {
511 1.35 fvdl NFS_STASHCOOKIE32(pdp, nndp->dc_cookie32);
512 1.35 fvdl curoff = nndp->dc_cookie32;
513 1.35 fvdl }
514 1.35 fvdl }
515 1.35 fvdl
516 1.34 fvdl n = ((caddr_t)pdp + pdp->d_reclen) - (bp->b_data + on);
517 1.34 fvdl
518 1.34 fvdl /*
519 1.12 mycroft * If not eof and read aheads are enabled, start one.
520 1.12 mycroft * (You need the current block first, so that you have the
521 1.24 fvdl * directory offset cookie of the next block.)
522 1.12 mycroft */
523 1.12 mycroft if (nfs_numasync > 0 && nmp->nm_readahead > 0 &&
524 1.34 fvdl np->n_direofoffset == 0 && !(np->n_flag & NQNFSNONCACHE)) {
525 1.35 fvdl rabp = nfs_getcacheblk(vp, nndp->dc_blkno,
526 1.34 fvdl NFS_DIRBLKSIZ, p);
527 1.12 mycroft if (rabp) {
528 1.12 mycroft if ((rabp->b_flags & (B_DONE | B_DELWRI)) == 0) {
529 1.35 fvdl rabp->b_dcookie = nndp->dc_cookie;
530 1.12 mycroft rabp->b_flags |= (B_READ | B_ASYNC);
531 1.12 mycroft if (nfs_asyncio(rabp, cred)) {
532 1.12 mycroft rabp->b_flags |= B_INVAL;
533 1.12 mycroft brelse(rabp);
534 1.12 mycroft }
535 1.19 mycroft } else
536 1.19 mycroft brelse(rabp);
537 1.12 mycroft }
538 1.12 mycroft }
539 1.12 mycroft got_buf = 1;
540 1.1 cgd break;
541 1.24 fvdl default:
542 1.29 christos printf(" nfsbioread: type %x unexpected\n",vp->v_type);
543 1.23 christos break;
544 1.44.2.1 chs }
545 1.12 mycroft
546 1.12 mycroft if (n > 0) {
547 1.12 mycroft if (!baddr)
548 1.12 mycroft baddr = bp->b_data;
549 1.12 mycroft error = uiomove(baddr + on, (int)n, uio);
550 1.1 cgd }
551 1.1 cgd switch (vp->v_type) {
552 1.24 fvdl case VREG:
553 1.24 fvdl break;
554 1.1 cgd case VLNK:
555 1.1 cgd n = 0;
556 1.1 cgd break;
557 1.1 cgd case VDIR:
558 1.24 fvdl if (np->n_flag & NQNFSNONCACHE)
559 1.24 fvdl bp->b_flags |= B_INVAL;
560 1.34 fvdl uio->uio_offset = curoff;
561 1.34 fvdl if (enough)
562 1.34 fvdl n = 0;
563 1.1 cgd break;
564 1.24 fvdl default:
565 1.29 christos printf(" nfsbioread: type %x unexpected\n",vp->v_type);
566 1.24 fvdl }
567 1.12 mycroft if (got_buf)
568 1.12 mycroft brelse(bp);
569 1.12 mycroft } while (error == 0 && uio->uio_resid > 0 && n > 0);
570 1.1 cgd return (error);
571 1.1 cgd }
572 1.1 cgd
573 1.1 cgd /*
574 1.1 cgd * Vnode op for write using bio
575 1.1 cgd */
576 1.23 christos int
577 1.23 christos nfs_write(v)
578 1.23 christos void *v;
579 1.23 christos {
580 1.12 mycroft struct vop_write_args /* {
581 1.24 fvdl struct vnode *a_vp;
582 1.12 mycroft struct uio *a_uio;
583 1.12 mycroft int a_ioflag;
584 1.12 mycroft struct ucred *a_cred;
585 1.23 christos } */ *ap = v;
586 1.12 mycroft register int biosize;
587 1.12 mycroft register struct uio *uio = ap->a_uio;
588 1.1 cgd struct proc *p = uio->uio_procp;
589 1.12 mycroft register struct vnode *vp = ap->a_vp;
590 1.12 mycroft struct nfsnode *np = VTONFS(vp);
591 1.12 mycroft register struct ucred *cred = ap->a_cred;
592 1.12 mycroft int ioflag = ap->a_ioflag;
593 1.1 cgd struct buf *bp;
594 1.1 cgd struct vattr vattr;
595 1.24 fvdl struct nfsmount *nmp = VFSTONFS(vp->v_mount);
596 1.1 cgd daddr_t lbn, bn;
597 1.24 fvdl int n, on, error = 0, iomode, must_commit;
598 1.1 cgd
599 1.1 cgd #ifdef DIAGNOSTIC
600 1.1 cgd if (uio->uio_rw != UIO_WRITE)
601 1.1 cgd panic("nfs_write mode");
602 1.1 cgd if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
603 1.1 cgd panic("nfs_write proc");
604 1.1 cgd #endif
605 1.1 cgd if (vp->v_type != VREG)
606 1.1 cgd return (EIO);
607 1.12 mycroft if (np->n_flag & NWRITEERR) {
608 1.12 mycroft np->n_flag &= ~NWRITEERR;
609 1.12 mycroft return (np->n_error);
610 1.12 mycroft }
611 1.34 fvdl if ((nmp->nm_flag & NFSMNT_NFSV3) &&
612 1.34 fvdl !(nmp->nm_iflag & NFSMNT_GOTFSINFO))
613 1.24 fvdl (void)nfs_fsinfo(nmp, vp, cred, p);
614 1.1 cgd if (ioflag & (IO_APPEND | IO_SYNC)) {
615 1.1 cgd if (np->n_flag & NMODIFIED) {
616 1.12 mycroft np->n_attrstamp = 0;
617 1.23 christos error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
618 1.23 christos if (error)
619 1.12 mycroft return (error);
620 1.1 cgd }
621 1.1 cgd if (ioflag & IO_APPEND) {
622 1.1 cgd np->n_attrstamp = 0;
623 1.23 christos error = VOP_GETATTR(vp, &vattr, cred, p);
624 1.23 christos if (error)
625 1.1 cgd return (error);
626 1.1 cgd uio->uio_offset = np->n_size;
627 1.1 cgd }
628 1.1 cgd }
629 1.1 cgd if (uio->uio_offset < 0)
630 1.1 cgd return (EINVAL);
631 1.33 fvdl if ((uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
632 1.33 fvdl return (EFBIG);
633 1.1 cgd if (uio->uio_resid == 0)
634 1.1 cgd return (0);
635 1.1 cgd /*
636 1.1 cgd * Maybe this should be above the vnode op call, but so long as
637 1.1 cgd * file servers have no limits, i don't think it matters
638 1.1 cgd */
639 1.12 mycroft if (p && uio->uio_offset + uio->uio_resid >
640 1.1 cgd p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
641 1.1 cgd psignal(p, SIGXFSZ);
642 1.1 cgd return (EFBIG);
643 1.1 cgd }
644 1.1 cgd /*
645 1.1 cgd * I use nm_rsize, not nm_wsize so that all buffer cache blocks
646 1.1 cgd * will be the same size within a filesystem. nfs_writerpc will
647 1.1 cgd * still use nm_wsize when sizing the rpc's.
648 1.1 cgd */
649 1.12 mycroft biosize = nmp->nm_rsize;
650 1.1 cgd do {
651 1.44.2.1 chs #ifdef UBC
652 1.44.2.1 chs void *win;
653 1.44.2.1 chs vaddr_t oldoff = uio->uio_offset;
654 1.44.2.1 chs int byteoff = uio->uio_offset & (MAXBSIZE - 1);
655 1.44.2.1 chs int bytelen = min(uio->uio_resid, MAXBSIZE - byteoff);
656 1.44.2.1 chs
657 1.44.2.1 chs /*
658 1.44.2.1 chs * XXX only do one page at a time for now.
659 1.44.2.1 chs * otherwise when we're extending the file,
660 1.44.2.1 chs * the flush for a given page will invalidate
661 1.44.2.1 chs * all the pages after that in the file.
662 1.44.2.1 chs * we should probably just defer looking at the attrs
663 1.44.2.1 chs * returned with write rpcs until the
664 1.44.2.1 chs * the entire write operation has finished.
665 1.44.2.1 chs */
666 1.44.2.1 chs byteoff = uio->uio_offset & (PAGE_SIZE - 1);
667 1.44.2.1 chs bytelen = min(uio->uio_resid, PAGE_SIZE - byteoff);
668 1.44.2.1 chs
669 1.44.2.1 chs /* XXX */
670 1.44.2.1 chs on = bn = lbn = 0;
671 1.44.2.1 chs bp = 0;
672 1.44.2.1 chs n = bytelen;
673 1.44.2.1 chs #endif
674 1.16 cgd
675 1.16 cgd /*
676 1.16 cgd * XXX make sure we aren't cached in the VM page cache
677 1.16 cgd */
678 1.41 mrg #if defined(UVM)
679 1.41 mrg (void)uvm_vnp_uncache(vp);
680 1.41 mrg #else
681 1.16 cgd (void)vnode_pager_uncache(vp);
682 1.41 mrg #endif
683 1.12 mycroft
684 1.12 mycroft /*
685 1.12 mycroft * Check for a valid write lease.
686 1.12 mycroft */
687 1.12 mycroft if ((nmp->nm_flag & NFSMNT_NQNFS) &&
688 1.24 fvdl NQNFS_CKINVALID(vp, np, ND_WRITE)) {
689 1.12 mycroft do {
690 1.24 fvdl error = nqnfs_getlease(vp, ND_WRITE, cred, p);
691 1.12 mycroft } while (error == NQNFS_EXPIRED);
692 1.12 mycroft if (error)
693 1.12 mycroft return (error);
694 1.12 mycroft if (np->n_lrev != np->n_brev ||
695 1.12 mycroft (np->n_flag & NQNFSNONCACHE)) {
696 1.23 christos error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
697 1.23 christos if (error)
698 1.12 mycroft return (error);
699 1.12 mycroft np->n_brev = np->n_lrev;
700 1.12 mycroft }
701 1.12 mycroft }
702 1.24 fvdl if ((np->n_flag & NQNFSNONCACHE) && uio->uio_iovcnt == 1) {
703 1.24 fvdl iomode = NFSV3WRITE_FILESYNC;
704 1.24 fvdl error = nfs_writerpc(vp, uio, cred, &iomode, &must_commit);
705 1.24 fvdl if (must_commit)
706 1.24 fvdl nfs_clearcommit(vp->v_mount);
707 1.24 fvdl return (error);
708 1.24 fvdl }
709 1.1 cgd nfsstats.biocache_writes++;
710 1.44.2.1 chs
711 1.44.2.1 chs #ifdef UBC
712 1.44.2.1 chs if (np->n_size < uio->uio_offset + n) {
713 1.44.2.1 chs np->n_size = uio->uio_offset + n;
714 1.44.2.1 chs uvm_vnp_setsize(vp, np->n_size);
715 1.44.2.1 chs }
716 1.44.2.1 chs
717 1.44.2.1 chs /* XXX check dirty region stuff */
718 1.44.2.1 chs /* XXX check for NQNFS lease */
719 1.44.2.1 chs
720 1.44.2.1 chs if (bytelen == 0) {
721 1.44.2.1 chs break;
722 1.44.2.1 chs }
723 1.44.2.1 chs win = ubc_alloc(&vp->v_uvm.u_obj, uio->uio_offset, bytelen,
724 1.44.2.1 chs UBC_WRITE);
725 1.44.2.1 chs if (win == NULL) {
726 1.44.2.1 chs panic("nfs_bioread: ubc_alloc -> NULL");
727 1.44.2.1 chs }
728 1.44.2.1 chs error = uiomove(win, bytelen, uio);
729 1.44.2.1 chs ubc_release(win, 0);
730 1.44.2.1 chs
731 1.44.2.1 chs /* XXX abstract this somehow */
732 1.44.2.1 chs simple_lock(&vp->v_uvm.u_obj.vmobjlock);
733 1.44.2.1 chs vp->v_uvm.u_obj.pgops->pgo_flush(&vp->v_uvm.u_obj,
734 1.44.2.1 chs trunc_page(oldoff),
735 1.44.2.1 chs oldoff + bytelen,
736 1.44.2.1 chs PGO_CLEANIT | PGO_SYNCIO);
737 1.44.2.1 chs #ifdef DEBUGxx
738 1.44.2.1 chs printf("nfs_write: flush vp %p start 0x%x end 0x%x newsize 0x%x\n",
739 1.44.2.1 chs vp, (int)trunc_page(oldoff), (int)(oldoff + bytelen),
740 1.44.2.1 chs (int)vp->v_uvm.u_size);
741 1.44.2.1 chs #endif
742 1.44.2.1 chs simple_unlock(&vp->v_uvm.u_obj.vmobjlock);
743 1.44.2.1 chs if (error) {
744 1.44.2.1 chs break;
745 1.44.2.1 chs }
746 1.44.2.1 chs
747 1.44.2.1 chs /* XXX set dirty region stuff */
748 1.44.2.1 chs /* XXX set page NEEDCOMMIT flag */
749 1.44.2.1 chs /* XXX handle IO_SYNC and NQNFSNONCACHE */
750 1.44.2.1 chs
751 1.44.2.1 chs #else
752 1.1 cgd lbn = uio->uio_offset / biosize;
753 1.1 cgd on = uio->uio_offset & (biosize-1);
754 1.11 cgd n = min((unsigned)(biosize - on), uio->uio_resid);
755 1.12 mycroft bn = lbn * (biosize / DEV_BSIZE);
756 1.1 cgd again:
757 1.12 mycroft bp = nfs_getcacheblk(vp, bn, biosize, p);
758 1.12 mycroft if (!bp)
759 1.12 mycroft return (EINTR);
760 1.12 mycroft if (bp->b_wcred == NOCRED) {
761 1.1 cgd crhold(cred);
762 1.1 cgd bp->b_wcred = cred;
763 1.1 cgd }
764 1.12 mycroft np->n_flag |= NMODIFIED;
765 1.12 mycroft if (uio->uio_offset + n > np->n_size) {
766 1.12 mycroft np->n_size = uio->uio_offset + n;
767 1.41 mrg #if defined(UVM)
768 1.41 mrg uvm_vnp_setsize(vp, np->n_size);
769 1.41 mrg #else
770 1.32 drochner vnode_pager_setsize(vp, np->n_size);
771 1.41 mrg #endif
772 1.12 mycroft }
773 1.12 mycroft
774 1.12 mycroft /*
775 1.12 mycroft * If the new write will leave a contiguous dirty
776 1.12 mycroft * area, just update the b_dirtyoff and b_dirtyend,
777 1.12 mycroft * otherwise force a write rpc of the old dirty area.
778 1.12 mycroft */
779 1.12 mycroft if (bp->b_dirtyend > 0 &&
780 1.12 mycroft (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) {
781 1.12 mycroft bp->b_proc = p;
782 1.12 mycroft if (VOP_BWRITE(bp) == EINTR)
783 1.12 mycroft return (EINTR);
784 1.12 mycroft goto again;
785 1.12 mycroft }
786 1.12 mycroft
787 1.12 mycroft /*
788 1.12 mycroft * Check for valid write lease and get one as required.
789 1.12 mycroft * In case getblk() and/or bwrite() delayed us.
790 1.12 mycroft */
791 1.12 mycroft if ((nmp->nm_flag & NFSMNT_NQNFS) &&
792 1.24 fvdl NQNFS_CKINVALID(vp, np, ND_WRITE)) {
793 1.12 mycroft do {
794 1.24 fvdl error = nqnfs_getlease(vp, ND_WRITE, cred, p);
795 1.12 mycroft } while (error == NQNFS_EXPIRED);
796 1.12 mycroft if (error) {
797 1.12 mycroft brelse(bp);
798 1.12 mycroft return (error);
799 1.12 mycroft }
800 1.12 mycroft if (np->n_lrev != np->n_brev ||
801 1.12 mycroft (np->n_flag & NQNFSNONCACHE)) {
802 1.12 mycroft brelse(bp);
803 1.23 christos error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
804 1.23 christos if (error)
805 1.1 cgd return (error);
806 1.12 mycroft np->n_brev = np->n_lrev;
807 1.1 cgd goto again;
808 1.1 cgd }
809 1.12 mycroft }
810 1.23 christos error = uiomove((char *)bp->b_data + on, n, uio);
811 1.23 christos if (error) {
812 1.12 mycroft bp->b_flags |= B_ERROR;
813 1.12 mycroft brelse(bp);
814 1.12 mycroft return (error);
815 1.12 mycroft }
816 1.12 mycroft if (bp->b_dirtyend > 0) {
817 1.12 mycroft bp->b_dirtyoff = min(on, bp->b_dirtyoff);
818 1.12 mycroft bp->b_dirtyend = max((on + n), bp->b_dirtyend);
819 1.1 cgd } else {
820 1.1 cgd bp->b_dirtyoff = on;
821 1.12 mycroft bp->b_dirtyend = on + n;
822 1.1 cgd }
823 1.12 mycroft if (bp->b_validend == 0 || bp->b_validend < bp->b_dirtyoff ||
824 1.12 mycroft bp->b_validoff > bp->b_dirtyend) {
825 1.12 mycroft bp->b_validoff = bp->b_dirtyoff;
826 1.12 mycroft bp->b_validend = bp->b_dirtyend;
827 1.12 mycroft } else {
828 1.12 mycroft bp->b_validoff = min(bp->b_validoff, bp->b_dirtyoff);
829 1.12 mycroft bp->b_validend = max(bp->b_validend, bp->b_dirtyend);
830 1.1 cgd }
831 1.25 fvdl
832 1.25 fvdl /*
833 1.25 fvdl * Since this block is being modified, it must be written
834 1.25 fvdl * again and not just committed.
835 1.25 fvdl */
836 1.25 fvdl bp->b_flags &= ~B_NEEDCOMMIT;
837 1.25 fvdl
838 1.12 mycroft /*
839 1.12 mycroft * If the lease is non-cachable or IO_SYNC do bwrite().
840 1.12 mycroft */
841 1.12 mycroft if ((np->n_flag & NQNFSNONCACHE) || (ioflag & IO_SYNC)) {
842 1.12 mycroft bp->b_proc = p;
843 1.24 fvdl error = VOP_BWRITE(bp);
844 1.24 fvdl if (error)
845 1.12 mycroft return (error);
846 1.24 fvdl if (np->n_flag & NQNFSNONCACHE) {
847 1.24 fvdl error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
848 1.24 fvdl if (error)
849 1.24 fvdl return (error);
850 1.24 fvdl }
851 1.12 mycroft } else if ((n + on) == biosize &&
852 1.12 mycroft (nmp->nm_flag & NFSMNT_NQNFS) == 0) {
853 1.1 cgd bp->b_proc = (struct proc *)0;
854 1.24 fvdl bp->b_flags |= B_ASYNC;
855 1.24 fvdl (void)nfs_writebp(bp, 0);
856 1.24 fvdl } else {
857 1.12 mycroft bdwrite(bp);
858 1.24 fvdl }
859 1.44.2.1 chs #endif
860 1.12 mycroft } while (uio->uio_resid > 0 && n > 0);
861 1.12 mycroft return (0);
862 1.12 mycroft }
863 1.12 mycroft
864 1.12 mycroft /*
865 1.12 mycroft * Get an nfs cache block.
866 1.12 mycroft * Allocate a new one if the block isn't currently in the cache
867 1.12 mycroft * and return the block marked busy. If the calling process is
868 1.12 mycroft * interrupted by a signal for an interruptible mount point, return
869 1.12 mycroft * NULL.
870 1.12 mycroft */
871 1.12 mycroft struct buf *
872 1.12 mycroft nfs_getcacheblk(vp, bn, size, p)
873 1.12 mycroft struct vnode *vp;
874 1.12 mycroft daddr_t bn;
875 1.12 mycroft int size;
876 1.12 mycroft struct proc *p;
877 1.12 mycroft {
878 1.12 mycroft register struct buf *bp;
879 1.12 mycroft struct nfsmount *nmp = VFSTONFS(vp->v_mount);
880 1.12 mycroft
881 1.12 mycroft if (nmp->nm_flag & NFSMNT_INT) {
882 1.12 mycroft bp = getblk(vp, bn, size, PCATCH, 0);
883 1.12 mycroft while (bp == (struct buf *)0) {
884 1.12 mycroft if (nfs_sigintr(nmp, (struct nfsreq *)0, p))
885 1.12 mycroft return ((struct buf *)0);
886 1.12 mycroft bp = getblk(vp, bn, size, 0, 2 * hz);
887 1.12 mycroft }
888 1.12 mycroft } else
889 1.12 mycroft bp = getblk(vp, bn, size, 0, 0);
890 1.12 mycroft return (bp);
891 1.12 mycroft }
892 1.12 mycroft
893 1.12 mycroft /*
894 1.12 mycroft * Flush and invalidate all dirty buffers. If another process is already
895 1.12 mycroft * doing the flush, just wait for completion.
896 1.12 mycroft */
897 1.23 christos int
898 1.12 mycroft nfs_vinvalbuf(vp, flags, cred, p, intrflg)
899 1.12 mycroft struct vnode *vp;
900 1.12 mycroft int flags;
901 1.12 mycroft struct ucred *cred;
902 1.12 mycroft struct proc *p;
903 1.12 mycroft int intrflg;
904 1.12 mycroft {
905 1.12 mycroft register struct nfsnode *np = VTONFS(vp);
906 1.12 mycroft struct nfsmount *nmp = VFSTONFS(vp->v_mount);
907 1.12 mycroft int error = 0, slpflag, slptimeo;
908 1.12 mycroft
909 1.12 mycroft if ((nmp->nm_flag & NFSMNT_INT) == 0)
910 1.12 mycroft intrflg = 0;
911 1.12 mycroft if (intrflg) {
912 1.12 mycroft slpflag = PCATCH;
913 1.12 mycroft slptimeo = 2 * hz;
914 1.12 mycroft } else {
915 1.12 mycroft slpflag = 0;
916 1.12 mycroft slptimeo = 0;
917 1.12 mycroft }
918 1.12 mycroft /*
919 1.12 mycroft * First wait for any other process doing a flush to complete.
920 1.12 mycroft */
921 1.12 mycroft while (np->n_flag & NFLUSHINPROG) {
922 1.12 mycroft np->n_flag |= NFLUSHWANT;
923 1.12 mycroft error = tsleep((caddr_t)&np->n_flag, PRIBIO + 2, "nfsvinval",
924 1.12 mycroft slptimeo);
925 1.12 mycroft if (error && intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, p))
926 1.12 mycroft return (EINTR);
927 1.12 mycroft }
928 1.12 mycroft
929 1.12 mycroft /*
930 1.12 mycroft * Now, flush as required.
931 1.12 mycroft */
932 1.12 mycroft np->n_flag |= NFLUSHINPROG;
933 1.12 mycroft error = vinvalbuf(vp, flags, cred, p, slpflag, 0);
934 1.12 mycroft while (error) {
935 1.12 mycroft if (intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, p)) {
936 1.12 mycroft np->n_flag &= ~NFLUSHINPROG;
937 1.12 mycroft if (np->n_flag & NFLUSHWANT) {
938 1.12 mycroft np->n_flag &= ~NFLUSHWANT;
939 1.12 mycroft wakeup((caddr_t)&np->n_flag);
940 1.12 mycroft }
941 1.12 mycroft return (EINTR);
942 1.12 mycroft }
943 1.12 mycroft error = vinvalbuf(vp, flags, cred, p, 0, slptimeo);
944 1.12 mycroft }
945 1.12 mycroft np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
946 1.12 mycroft if (np->n_flag & NFLUSHWANT) {
947 1.12 mycroft np->n_flag &= ~NFLUSHWANT;
948 1.12 mycroft wakeup((caddr_t)&np->n_flag);
949 1.12 mycroft }
950 1.12 mycroft return (0);
951 1.12 mycroft }
952 1.12 mycroft
953 1.12 mycroft /*
954 1.12 mycroft * Initiate asynchronous I/O. Return an error if no nfsiods are available.
955 1.12 mycroft * This is mainly to avoid queueing async I/O requests when the nfsiods
956 1.12 mycroft * are all hung on a dead server.
957 1.12 mycroft */
958 1.23 christos int
959 1.12 mycroft nfs_asyncio(bp, cred)
960 1.12 mycroft register struct buf *bp;
961 1.12 mycroft struct ucred *cred;
962 1.12 mycroft {
963 1.12 mycroft register int i;
964 1.30 thorpej register struct nfsmount *nmp;
965 1.30 thorpej int gotiod, slpflag = 0, slptimeo = 0, error;
966 1.12 mycroft
967 1.12 mycroft if (nfs_numasync == 0)
968 1.12 mycroft return (EIO);
969 1.30 thorpej
970 1.30 thorpej
971 1.30 thorpej nmp = VFSTONFS(bp->b_vp->v_mount);
972 1.30 thorpej again:
973 1.30 thorpej if (nmp->nm_flag & NFSMNT_INT)
974 1.30 thorpej slpflag = PCATCH;
975 1.30 thorpej gotiod = FALSE;
976 1.30 thorpej
977 1.30 thorpej /*
978 1.30 thorpej * Find a free iod to process this request.
979 1.30 thorpej */
980 1.30 thorpej
981 1.12 mycroft for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
982 1.30 thorpej if (nfs_iodwant[i]) {
983 1.30 thorpej /*
984 1.30 thorpej * Found one, so wake it up and tell it which
985 1.30 thorpej * mount to process.
986 1.30 thorpej */
987 1.30 thorpej nfs_iodwant[i] = (struct proc *)0;
988 1.30 thorpej nfs_iodmount[i] = nmp;
989 1.30 thorpej nmp->nm_bufqiods++;
990 1.30 thorpej wakeup((caddr_t)&nfs_iodwant[i]);
991 1.30 thorpej gotiod = TRUE;
992 1.31 fvdl break;
993 1.30 thorpej }
994 1.30 thorpej /*
995 1.30 thorpej * If none are free, we may already have an iod working on this mount
996 1.30 thorpej * point. If so, it will process our request.
997 1.30 thorpej */
998 1.30 thorpej if (!gotiod && nmp->nm_bufqiods > 0)
999 1.30 thorpej gotiod = TRUE;
1000 1.30 thorpej
1001 1.30 thorpej /*
1002 1.30 thorpej * If we have an iod which can process the request, then queue
1003 1.30 thorpej * the buffer.
1004 1.30 thorpej */
1005 1.30 thorpej if (gotiod) {
1006 1.30 thorpej /*
1007 1.30 thorpej * Ensure that the queue never grows too large.
1008 1.30 thorpej */
1009 1.30 thorpej while (nmp->nm_bufqlen >= 2*nfs_numasync) {
1010 1.30 thorpej nmp->nm_bufqwant = TRUE;
1011 1.30 thorpej error = tsleep(&nmp->nm_bufq, slpflag | PRIBIO,
1012 1.30 thorpej "nfsaio", slptimeo);
1013 1.30 thorpej if (error) {
1014 1.30 thorpej if (nfs_sigintr(nmp, NULL, bp->b_proc))
1015 1.30 thorpej return (EINTR);
1016 1.30 thorpej if (slpflag == PCATCH) {
1017 1.30 thorpej slpflag = 0;
1018 1.30 thorpej slptimeo = 2 * hz;
1019 1.30 thorpej }
1020 1.30 thorpej }
1021 1.30 thorpej /*
1022 1.30 thorpej * We might have lost our iod while sleeping,
1023 1.30 thorpej * so check and loop if nescessary.
1024 1.30 thorpej */
1025 1.30 thorpej if (nmp->nm_bufqiods == 0)
1026 1.30 thorpej goto again;
1027 1.30 thorpej }
1028 1.30 thorpej
1029 1.12 mycroft if (bp->b_flags & B_READ) {
1030 1.12 mycroft if (bp->b_rcred == NOCRED && cred != NOCRED) {
1031 1.12 mycroft crhold(cred);
1032 1.12 mycroft bp->b_rcred = cred;
1033 1.12 mycroft }
1034 1.1 cgd } else {
1035 1.24 fvdl bp->b_flags |= B_WRITEINPROG;
1036 1.12 mycroft if (bp->b_wcred == NOCRED && cred != NOCRED) {
1037 1.12 mycroft crhold(cred);
1038 1.12 mycroft bp->b_wcred = cred;
1039 1.12 mycroft }
1040 1.12 mycroft }
1041 1.12 mycroft
1042 1.30 thorpej TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist);
1043 1.30 thorpej nmp->nm_bufqlen++;
1044 1.12 mycroft return (0);
1045 1.12 mycroft }
1046 1.24 fvdl
1047 1.24 fvdl /*
1048 1.30 thorpej * All the iods are busy on other mounts, so return EIO to
1049 1.30 thorpej * force the caller to process the i/o synchronously.
1050 1.24 fvdl */
1051 1.30 thorpej return (EIO);
1052 1.12 mycroft }
1053 1.12 mycroft
1054 1.12 mycroft /*
1055 1.12 mycroft * Do an I/O operation to/from a cache block. This may be called
1056 1.12 mycroft * synchronously or from an nfsiod.
1057 1.12 mycroft */
1058 1.12 mycroft int
1059 1.12 mycroft nfs_doio(bp, cr, p)
1060 1.12 mycroft register struct buf *bp;
1061 1.23 christos struct ucred *cr;
1062 1.12 mycroft struct proc *p;
1063 1.12 mycroft {
1064 1.12 mycroft register struct uio *uiop;
1065 1.12 mycroft register struct vnode *vp;
1066 1.12 mycroft struct nfsnode *np;
1067 1.12 mycroft struct nfsmount *nmp;
1068 1.24 fvdl int error = 0, diff, len, iomode, must_commit = 0;
1069 1.12 mycroft struct uio uio;
1070 1.12 mycroft struct iovec io;
1071 1.12 mycroft
1072 1.12 mycroft vp = bp->b_vp;
1073 1.12 mycroft np = VTONFS(vp);
1074 1.12 mycroft nmp = VFSTONFS(vp->v_mount);
1075 1.12 mycroft uiop = &uio;
1076 1.12 mycroft uiop->uio_iov = &io;
1077 1.12 mycroft uiop->uio_iovcnt = 1;
1078 1.12 mycroft uiop->uio_segflg = UIO_SYSSPACE;
1079 1.12 mycroft uiop->uio_procp = p;
1080 1.12 mycroft
1081 1.12 mycroft /*
1082 1.14 pk * Historically, paging was done with physio, but no more...
1083 1.12 mycroft */
1084 1.14 pk if (bp->b_flags & B_PHYS) {
1085 1.14 pk /*
1086 1.14 pk * ...though reading /dev/drum still gets us here.
1087 1.14 pk */
1088 1.14 pk io.iov_len = uiop->uio_resid = bp->b_bcount;
1089 1.14 pk /* mapping was done by vmapbuf() */
1090 1.14 pk io.iov_base = bp->b_data;
1091 1.24 fvdl uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
1092 1.14 pk if (bp->b_flags & B_READ) {
1093 1.14 pk uiop->uio_rw = UIO_READ;
1094 1.14 pk nfsstats.read_physios++;
1095 1.14 pk error = nfs_readrpc(vp, uiop, cr);
1096 1.14 pk } else {
1097 1.24 fvdl iomode = NFSV3WRITE_DATASYNC;
1098 1.14 pk uiop->uio_rw = UIO_WRITE;
1099 1.14 pk nfsstats.write_physios++;
1100 1.24 fvdl error = nfs_writerpc(vp, uiop, cr, &iomode, &must_commit);
1101 1.14 pk }
1102 1.14 pk if (error) {
1103 1.14 pk bp->b_flags |= B_ERROR;
1104 1.14 pk bp->b_error = error;
1105 1.14 pk }
1106 1.14 pk } else if (bp->b_flags & B_READ) {
1107 1.12 mycroft io.iov_len = uiop->uio_resid = bp->b_bcount;
1108 1.12 mycroft io.iov_base = bp->b_data;
1109 1.12 mycroft uiop->uio_rw = UIO_READ;
1110 1.12 mycroft switch (vp->v_type) {
1111 1.12 mycroft case VREG:
1112 1.24 fvdl uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
1113 1.12 mycroft nfsstats.read_bios++;
1114 1.12 mycroft error = nfs_readrpc(vp, uiop, cr);
1115 1.12 mycroft if (!error) {
1116 1.12 mycroft bp->b_validoff = 0;
1117 1.12 mycroft if (uiop->uio_resid) {
1118 1.12 mycroft /*
1119 1.12 mycroft * If len > 0, there is a hole in the file and
1120 1.12 mycroft * no writes after the hole have been pushed to
1121 1.12 mycroft * the server yet.
1122 1.12 mycroft * Just zero fill the rest of the valid area.
1123 1.12 mycroft */
1124 1.12 mycroft diff = bp->b_bcount - uiop->uio_resid;
1125 1.24 fvdl len = np->n_size - (((u_quad_t)bp->b_blkno) * DEV_BSIZE
1126 1.12 mycroft + diff);
1127 1.12 mycroft if (len > 0) {
1128 1.12 mycroft len = min(len, uiop->uio_resid);
1129 1.44 perry memset((char *)bp->b_data + diff, 0, len);
1130 1.12 mycroft bp->b_validend = diff + len;
1131 1.12 mycroft } else
1132 1.12 mycroft bp->b_validend = diff;
1133 1.12 mycroft } else
1134 1.12 mycroft bp->b_validend = bp->b_bcount;
1135 1.12 mycroft }
1136 1.12 mycroft if (p && (vp->v_flag & VTEXT) &&
1137 1.12 mycroft (((nmp->nm_flag & NFSMNT_NQNFS) &&
1138 1.24 fvdl NQNFS_CKINVALID(vp, np, ND_READ) &&
1139 1.12 mycroft np->n_lrev != np->n_brev) ||
1140 1.12 mycroft (!(nmp->nm_flag & NFSMNT_NQNFS) &&
1141 1.35 fvdl np->n_mtime != np->n_vattr->va_mtime.tv_sec))) {
1142 1.12 mycroft uprintf("Process killed due to text file modification\n");
1143 1.12 mycroft psignal(p, SIGKILL);
1144 1.13 mycroft p->p_holdcnt++;
1145 1.12 mycroft }
1146 1.12 mycroft break;
1147 1.12 mycroft case VLNK:
1148 1.24 fvdl uiop->uio_offset = (off_t)0;
1149 1.12 mycroft nfsstats.readlink_bios++;
1150 1.12 mycroft error = nfs_readlinkrpc(vp, uiop, cr);
1151 1.12 mycroft break;
1152 1.12 mycroft case VDIR:
1153 1.12 mycroft nfsstats.readdir_bios++;
1154 1.34 fvdl uiop->uio_offset = bp->b_dcookie;
1155 1.24 fvdl if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
1156 1.24 fvdl error = nfs_readdirplusrpc(vp, uiop, cr);
1157 1.24 fvdl if (error == NFSERR_NOTSUPP)
1158 1.24 fvdl nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
1159 1.24 fvdl }
1160 1.24 fvdl if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
1161 1.24 fvdl error = nfs_readdirrpc(vp, uiop, cr);
1162 1.34 fvdl if (!error) {
1163 1.34 fvdl bp->b_dcookie = uiop->uio_offset;
1164 1.34 fvdl bp->b_validoff = 0;
1165 1.34 fvdl bp->b_validend = bp->b_bcount - uiop->uio_resid;
1166 1.34 fvdl }
1167 1.24 fvdl break;
1168 1.24 fvdl default:
1169 1.29 christos printf("nfs_doio: type %x unexpected\n",vp->v_type);
1170 1.12 mycroft break;
1171 1.12 mycroft };
1172 1.12 mycroft if (error) {
1173 1.12 mycroft bp->b_flags |= B_ERROR;
1174 1.12 mycroft bp->b_error = error;
1175 1.12 mycroft }
1176 1.12 mycroft } else {
1177 1.12 mycroft io.iov_len = uiop->uio_resid = bp->b_dirtyend
1178 1.12 mycroft - bp->b_dirtyoff;
1179 1.24 fvdl uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE
1180 1.12 mycroft + bp->b_dirtyoff;
1181 1.12 mycroft io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
1182 1.12 mycroft uiop->uio_rw = UIO_WRITE;
1183 1.12 mycroft nfsstats.write_bios++;
1184 1.24 fvdl if ((bp->b_flags & (B_ASYNC | B_NEEDCOMMIT | B_NOCACHE)) == B_ASYNC)
1185 1.24 fvdl iomode = NFSV3WRITE_UNSTABLE;
1186 1.24 fvdl else
1187 1.24 fvdl iomode = NFSV3WRITE_FILESYNC;
1188 1.24 fvdl bp->b_flags |= B_WRITEINPROG;
1189 1.24 fvdl #ifdef fvdl_debug
1190 1.29 christos printf("nfs_doio(%x): bp %x doff %d dend %d\n",
1191 1.24 fvdl vp, bp, bp->b_dirtyoff, bp->b_dirtyend);
1192 1.24 fvdl #endif
1193 1.24 fvdl error = nfs_writerpc(vp, uiop, cr, &iomode, &must_commit);
1194 1.24 fvdl if (!error && iomode == NFSV3WRITE_UNSTABLE)
1195 1.24 fvdl bp->b_flags |= B_NEEDCOMMIT;
1196 1.12 mycroft else
1197 1.24 fvdl bp->b_flags &= ~B_NEEDCOMMIT;
1198 1.24 fvdl bp->b_flags &= ~B_WRITEINPROG;
1199 1.12 mycroft
1200 1.12 mycroft /*
1201 1.12 mycroft * For an interrupted write, the buffer is still valid and the
1202 1.12 mycroft * write hasn't been pushed to the server yet, so we can't set
1203 1.12 mycroft * B_ERROR and report the interruption by setting B_EINTR. For
1204 1.12 mycroft * the B_ASYNC case, B_EINTR is not relevant, so the rpc attempt
1205 1.12 mycroft * is essentially a noop.
1206 1.24 fvdl * For the case of a V3 write rpc not being committed to stable
1207 1.24 fvdl * storage, the block is still dirty and requires either a commit
1208 1.24 fvdl * rpc or another write rpc with iomode == NFSV3WRITE_FILESYNC
1209 1.24 fvdl * before the block is reused. This is indicated by setting the
1210 1.24 fvdl * B_DELWRI and B_NEEDCOMMIT flags.
1211 1.12 mycroft */
1212 1.24 fvdl if (error == EINTR || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
1213 1.12 mycroft bp->b_flags |= B_DELWRI;
1214 1.12 mycroft
1215 1.12 mycroft /*
1216 1.12 mycroft * Since for the B_ASYNC case, nfs_bwrite() has reassigned the
1217 1.12 mycroft * buffer to the clean list, we have to reassign it back to the
1218 1.12 mycroft * dirty one. Ugh.
1219 1.12 mycroft */
1220 1.12 mycroft if (bp->b_flags & B_ASYNC)
1221 1.12 mycroft reassignbuf(bp, vp);
1222 1.27 fvdl else if (error)
1223 1.12 mycroft bp->b_flags |= B_EINTR;
1224 1.12 mycroft } else {
1225 1.12 mycroft if (error) {
1226 1.12 mycroft bp->b_flags |= B_ERROR;
1227 1.12 mycroft bp->b_error = np->n_error = error;
1228 1.12 mycroft np->n_flag |= NWRITEERR;
1229 1.1 cgd }
1230 1.12 mycroft bp->b_dirtyoff = bp->b_dirtyend = 0;
1231 1.12 mycroft }
1232 1.1 cgd }
1233 1.12 mycroft bp->b_resid = uiop->uio_resid;
1234 1.24 fvdl if (must_commit)
1235 1.24 fvdl nfs_clearcommit(vp->v_mount);
1236 1.12 mycroft biodone(bp);
1237 1.1 cgd return (error);
1238 1.1 cgd }
1239 1.44.2.1 chs
1240 1.44.2.1 chs #ifdef UBC
1241 1.44.2.1 chs /*
1242 1.44.2.1 chs * Vnode op for VM getpages.
1243 1.44.2.1 chs */
1244 1.44.2.1 chs int
1245 1.44.2.1 chs nfs_getpages(v)
1246 1.44.2.1 chs void *v;
1247 1.44.2.1 chs {
1248 1.44.2.1 chs struct vop_getpages_args /* {
1249 1.44.2.1 chs struct vnode *a_vp;
1250 1.44.2.1 chs vaddr_t a_offset;
1251 1.44.2.1 chs vm_page_t *a_m;
1252 1.44.2.1 chs int *a_count;
1253 1.44.2.1 chs int a_centeridx;
1254 1.44.2.1 chs vm_prot_t a_access_type;
1255 1.44.2.1 chs int a_advice;
1256 1.44.2.1 chs int a_flags;
1257 1.44.2.1 chs } */ *ap = v;
1258 1.44.2.1 chs int error;
1259 1.44.2.1 chs struct uio uio;
1260 1.44.2.1 chs struct iovec iov;
1261 1.44.2.1 chs vm_page_t m;
1262 1.44.2.1 chs vaddr_t kva;
1263 1.44.2.1 chs struct buf tmpbuf, *bp;
1264 1.44.2.1 chs struct vnode *vp = ap->a_vp;
1265 1.44.2.1 chs struct uvm_object *uobj = &vp->v_uvm.u_obj;
1266 1.44.2.1 chs UVMHIST_FUNC("nfs_getpages"); UVMHIST_CALLED(ubchist);
1267 1.44.2.1 chs UVMHIST_LOG(ubchist, "vp %p off 0x%x", vp, (int)ap->a_offset, 0,0);
1268 1.44.2.1 chs
1269 1.44.2.1 chs /* XXX don't deal with PGO_LOCKED yet */
1270 1.44.2.1 chs if (ap->a_flags & PGO_LOCKED) {
1271 1.44.2.1 chs *ap->a_count = 0;
1272 1.44.2.1 chs return 0;
1273 1.44.2.1 chs }
1274 1.44.2.1 chs
1275 1.44.2.1 chs #if 0
1276 1.44.2.1 chs printf("nfs_getpages vp %p off 0x%x\n", vp, (int)ap->a_offset);
1277 1.44.2.1 chs #endif
1278 1.44.2.1 chs
1279 1.44.2.1 chs /* vnode is VOP_LOCKed, uobj is locked */
1280 1.44.2.1 chs
1281 1.44.2.1 chs /* XXX for now, just do one page at a time */
1282 1.44.2.1 chs if (*ap->a_count != 1) {
1283 1.44.2.1 chs panic("nfs_getpages: one at a time, please\n");
1284 1.44.2.1 chs }
1285 1.44.2.1 chs
1286 1.44.2.1 chs #ifdef DIAGNOSTIC
1287 1.44.2.1 chs if (ap->a_centeridx < 0 || ap->a_centeridx > *ap->a_count) {
1288 1.44.2.1 chs panic("ffs_getpages: centeridx %d out of range",
1289 1.44.2.1 chs ap->a_centeridx);
1290 1.44.2.1 chs }
1291 1.44.2.1 chs #endif
1292 1.44.2.1 chs
1293 1.44.2.1 chs uvn_findpage(uobj, ap->a_offset, ap->a_m);
1294 1.44.2.1 chs m = ap->a_m[ap->a_centeridx];
1295 1.44.2.1 chs
1296 1.44.2.1 chs simple_unlock(&uobj->vmobjlock);
1297 1.44.2.1 chs
1298 1.44.2.1 chs if ((m->flags & PG_FAKE) == 0) {
1299 1.44.2.1 chs UVMHIST_LOG(ubchist, "page was valid",0,0,0,0);
1300 1.44.2.1 chs return 0;
1301 1.44.2.1 chs }
1302 1.44.2.1 chs
1303 1.44.2.1 chs /*
1304 1.44.2.1 chs * if the entire page is past the end of the file,
1305 1.44.2.1 chs * just zero it and return.
1306 1.44.2.1 chs */
1307 1.44.2.1 chs if (m->offset >= vp->v_uvm.u_size) {
1308 1.44.2.1 chs UVMHIST_LOG(ubchist, "off 0x%x past EOF 0x%x, zeroed page",
1309 1.44.2.1 chs m->offset, vp->v_uvm.u_size,0,0);
1310 1.44.2.1 chs printf("past EOF, clearing\n");
1311 1.44.2.1 chs pmap_zero_page(VM_PAGE_TO_PHYS(m));
1312 1.44.2.1 chs return 0;
1313 1.44.2.1 chs }
1314 1.44.2.1 chs
1315 1.44.2.1 chs /*
1316 1.44.2.1 chs * read at last part of the page.
1317 1.44.2.1 chs */
1318 1.44.2.1 chs
1319 1.44.2.1 chs kva = uvm_pagermapin(ap->a_m, *ap->a_count, NULL, M_WAITOK);
1320 1.44.2.1 chs if (kva == 0) {
1321 1.44.2.1 chs printf("uvm_pagermapin failed\n");
1322 1.44.2.1 chs return EAGAIN;
1323 1.44.2.1 chs }
1324 1.44.2.1 chs
1325 1.44.2.1 chs uio.uio_iov = &iov;
1326 1.44.2.1 chs iov.iov_len = 0;
1327 1.44.2.1 chs
1328 1.44.2.1 chs bp = &tmpbuf;
1329 1.44.2.1 chs bzero(bp, sizeof *bp);
1330 1.44.2.1 chs
1331 1.44.2.1 chs bp->b_bufsize = PAGE_SIZE;
1332 1.44.2.1 chs bp->b_bcount = min(PAGE_SIZE, vp->v_uvm.u_size - m->offset);
1333 1.44.2.1 chs bp->b_data = (void *)kva;
1334 1.44.2.1 chs bp->b_blkno = bp->b_lblkno = m->offset / DEV_BSIZE;
1335 1.44.2.1 chs bp->b_vp = vp;
1336 1.44.2.1 chs bp->b_rcred = 0;
1337 1.44.2.1 chs bp->b_flags = B_BUSY|B_READ;
1338 1.44.2.1 chs
1339 1.44.2.1 chs UVMHIST_LOG(ubchist, "reading page",0,0,0,0);
1340 1.44.2.1 chs error = nfs_doio(bp, curproc->p_ucred, curproc);
1341 1.44.2.1 chs
1342 1.44.2.1 chs uvm_pagermapout(kva, *ap->a_count);
1343 1.44.2.1 chs
1344 1.44.2.1 chs if (error) {
1345 1.44.2.1 chs simple_lock(&uobj->vmobjlock);
1346 1.44.2.1 chs if (m->flags & PG_WANTED)
1347 1.44.2.1 chs thread_wakeup(m); /* object lock still held */
1348 1.44.2.1 chs
1349 1.44.2.1 chs m->flags &= ~(PG_WANTED|PG_BUSY);
1350 1.44.2.1 chs UVM_PAGE_OWN(m, NULL);
1351 1.44.2.1 chs uvm_lock_pageq();
1352 1.44.2.1 chs uvm_pagefree(m);
1353 1.44.2.1 chs uvm_unlock_pageq();
1354 1.44.2.1 chs simple_unlock(&uobj->vmobjlock);
1355 1.44.2.1 chs } else {
1356 1.44.2.1 chs m->flags &= ~PG_FAKE;
1357 1.44.2.1 chs pmap_clear_modify(PMAP_PGARG(m));
1358 1.44.2.1 chs }
1359 1.44.2.1 chs
1360 1.44.2.1 chs return error;
1361 1.44.2.1 chs }
1362 1.44.2.1 chs
1363 1.44.2.1 chs /*
1364 1.44.2.1 chs * Vnode op for VM putpages.
1365 1.44.2.1 chs */
1366 1.44.2.1 chs int
1367 1.44.2.1 chs nfs_putpages(v)
1368 1.44.2.1 chs void *v;
1369 1.44.2.1 chs {
1370 1.44.2.1 chs struct vop_putpages_args /* {
1371 1.44.2.1 chs struct vnode *a_vp;
1372 1.44.2.1 chs vm_page_t *a_m;
1373 1.44.2.1 chs int a_count;
1374 1.44.2.1 chs int a_sync;
1375 1.44.2.1 chs int *a_rtvals;
1376 1.44.2.1 chs } */ *ap = v;
1377 1.44.2.1 chs
1378 1.44.2.1 chs struct vnode *vp = ap->a_vp;
1379 1.44.2.1 chs int mode, commit;
1380 1.44.2.1 chs int error;
1381 1.44.2.1 chs struct uio uio;
1382 1.44.2.1 chs struct iovec iov;
1383 1.44.2.1 chs vm_page_t m;
1384 1.44.2.1 chs vaddr_t kva;
1385 1.44.2.1 chs int iosize;
1386 1.44.2.1 chs
1387 1.44.2.1 chs /* XXX for now, just do one page at a time */
1388 1.44.2.1 chs if (ap->a_count != 1) {
1389 1.44.2.1 chs panic("nfs_putpages: one at a time, please\n");
1390 1.44.2.1 chs }
1391 1.44.2.1 chs
1392 1.44.2.1 chs m = ap->a_m[0];
1393 1.44.2.1 chs kva = uvm_pagermapin(ap->a_m, ap->a_count, NULL, M_WAITOK);
1394 1.44.2.1 chs if (kva == 0) {
1395 1.44.2.1 chs printf("uvm_pagermapin failed\n");
1396 1.44.2.1 chs return EAGAIN;
1397 1.44.2.1 chs }
1398 1.44.2.1 chs
1399 1.44.2.1 chs iosize = min(PAGE_SIZE, vp->v_uvm.u_size - m->offset);
1400 1.44.2.1 chs
1401 1.44.2.1 chs iov.iov_base = (caddr_t)kva;
1402 1.44.2.1 chs iov.iov_len = iosize;
1403 1.44.2.1 chs uio.uio_iov = &iov;
1404 1.44.2.1 chs uio.uio_iovcnt = 1;
1405 1.44.2.1 chs uio.uio_offset = m->offset;
1406 1.44.2.1 chs uio.uio_resid = iosize;
1407 1.44.2.1 chs uio.uio_segflg = UIO_SYSSPACE;
1408 1.44.2.1 chs uio.uio_rw = UIO_WRITE;
1409 1.44.2.1 chs uio.uio_procp = NULL;
1410 1.44.2.1 chs
1411 1.44.2.1 chs /* XXX */
1412 1.44.2.1 chs mode = NFSV3WRITE_FILESYNC;
1413 1.44.2.1 chs
1414 1.44.2.1 chs simple_unlock(&vp->v_uvm.u_obj.vmobjlock);
1415 1.44.2.1 chs error = nfs_writerpc(vp, &uio, curproc->p_ucred, &mode, &commit);
1416 1.44.2.1 chs
1417 1.44.2.1 chs uvm_pagermapout(kva, ap->a_count);
1418 1.44.2.1 chs
1419 1.44.2.1 chs return error;
1420 1.44.2.1 chs }
1421 1.44.2.1 chs #endif
1422