nfs_bio.c revision 1.63.2.15 1 1.63.2.15 nathanw /* $NetBSD: nfs_bio.c,v 1.63.2.15 2002/07/12 01:40:35 nathanw 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.63.2.7 nathanw
41 1.63.2.7 nathanw #include <sys/cdefs.h>
42 1.63.2.15 nathanw __KERNEL_RCSID(0, "$NetBSD: nfs_bio.c,v 1.63.2.15 2002/07/12 01:40:35 nathanw Exp $");
43 1.1 cgd
44 1.51 bjh21 #include "opt_nfs.h"
45 1.54 chs #include "opt_ddb.h"
46 1.51 bjh21
47 1.8 mycroft #include <sys/param.h>
48 1.8 mycroft #include <sys/systm.h>
49 1.12 mycroft #include <sys/resourcevar.h>
50 1.24 fvdl #include <sys/signalvar.h>
51 1.8 mycroft #include <sys/proc.h>
52 1.8 mycroft #include <sys/buf.h>
53 1.8 mycroft #include <sys/vnode.h>
54 1.8 mycroft #include <sys/mount.h>
55 1.12 mycroft #include <sys/kernel.h>
56 1.23 christos #include <sys/namei.h>
57 1.34 fvdl #include <sys/dirent.h>
58 1.54 chs #include <sys/malloc.h>
59 1.1 cgd
60 1.41 mrg #include <uvm/uvm_extern.h>
61 1.54 chs #include <uvm/uvm.h>
62 1.41 mrg
63 1.12 mycroft #include <nfs/rpcv2.h>
64 1.24 fvdl #include <nfs/nfsproto.h>
65 1.8 mycroft #include <nfs/nfs.h>
66 1.8 mycroft #include <nfs/nfsmount.h>
67 1.12 mycroft #include <nfs/nqnfs.h>
68 1.24 fvdl #include <nfs/nfsnode.h>
69 1.23 christos #include <nfs/nfs_var.h>
70 1.1 cgd
71 1.12 mycroft extern int nfs_numasync;
72 1.63.2.9 nathanw extern int nfs_commitsize;
73 1.24 fvdl extern struct nfsstats nfsstats;
74 1.1 cgd
75 1.1 cgd /*
76 1.1 cgd * Vnode op for read using bio
77 1.1 cgd * Any similarity to readip() is purely coincidental
78 1.1 cgd */
79 1.23 christos int
80 1.34 fvdl nfs_bioread(vp, uio, ioflag, cred, cflag)
81 1.48 augustss struct vnode *vp;
82 1.48 augustss struct uio *uio;
83 1.34 fvdl int ioflag, cflag;
84 1.1 cgd struct ucred *cred;
85 1.1 cgd {
86 1.48 augustss struct nfsnode *np = VTONFS(vp);
87 1.23 christos struct buf *bp = NULL, *rabp;
88 1.1 cgd struct vattr vattr;
89 1.12 mycroft struct proc *p;
90 1.24 fvdl struct nfsmount *nmp = VFSTONFS(vp->v_mount);
91 1.35 fvdl struct nfsdircache *ndp = NULL, *nndp = NULL;
92 1.34 fvdl caddr_t baddr, ep, edp;
93 1.54 chs int got_buf = 0, error = 0, n = 0, on = 0, en, enn;
94 1.34 fvdl int enough = 0;
95 1.34 fvdl struct dirent *dp, *pdp;
96 1.54 chs off_t curoff = 0;
97 1.1 cgd
98 1.1 cgd #ifdef DIAGNOSTIC
99 1.1 cgd if (uio->uio_rw != UIO_READ)
100 1.1 cgd panic("nfs_read mode");
101 1.1 cgd #endif
102 1.1 cgd if (uio->uio_resid == 0)
103 1.1 cgd return (0);
104 1.34 fvdl if (vp->v_type != VDIR && uio->uio_offset < 0)
105 1.1 cgd return (EINVAL);
106 1.24 fvdl p = uio->uio_procp;
107 1.51 bjh21 #ifndef NFS_V2_ONLY
108 1.34 fvdl if ((nmp->nm_flag & NFSMNT_NFSV3) &&
109 1.34 fvdl !(nmp->nm_iflag & NFSMNT_GOTFSINFO))
110 1.24 fvdl (void)nfs_fsinfo(nmp, vp, cred, p);
111 1.51 bjh21 #endif
112 1.34 fvdl if (vp->v_type != VDIR &&
113 1.34 fvdl (uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
114 1.33 fvdl return (EFBIG);
115 1.54 chs
116 1.1 cgd /*
117 1.12 mycroft * For nfs, cache consistency can only be maintained approximately.
118 1.12 mycroft * Although RFC1094 does not specify the criteria, the following is
119 1.12 mycroft * believed to be compatible with the reference port.
120 1.12 mycroft * For nqnfs, full cache consistency is maintained within the loop.
121 1.12 mycroft * For nfs:
122 1.1 cgd * If the file's modify time on the server has changed since the
123 1.1 cgd * last read rpc or you have written to the file,
124 1.1 cgd * you may have lost data cache consistency with the
125 1.1 cgd * server, so flush all of the file's data out of the cache.
126 1.1 cgd * Then force a getattr rpc to ensure that you have up to date
127 1.1 cgd * attributes.
128 1.1 cgd * NB: This implies that cache data can be read when up to
129 1.1 cgd * NFS_ATTRTIMEO seconds out of date. If you find that you need current
130 1.1 cgd * attributes this could be forced by setting n_attrstamp to 0 before
131 1.12 mycroft * the VOP_GETATTR() call.
132 1.1 cgd */
133 1.54 chs
134 1.12 mycroft if ((nmp->nm_flag & NFSMNT_NQNFS) == 0 && vp->v_type != VLNK) {
135 1.1 cgd if (np->n_flag & NMODIFIED) {
136 1.24 fvdl if (vp->v_type != VREG) {
137 1.24 fvdl if (vp->v_type != VDIR)
138 1.24 fvdl panic("nfs: bioread, not dir");
139 1.35 fvdl nfs_invaldircache(vp, 0);
140 1.35 fvdl np->n_direofoffset = 0;
141 1.23 christos error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
142 1.23 christos if (error)
143 1.12 mycroft return (error);
144 1.12 mycroft }
145 1.1 cgd np->n_attrstamp = 0;
146 1.23 christos error = VOP_GETATTR(vp, &vattr, cred, p);
147 1.23 christos if (error)
148 1.1 cgd return (error);
149 1.22 jtc np->n_mtime = vattr.va_mtime.tv_sec;
150 1.1 cgd } else {
151 1.24 fvdl error = VOP_GETATTR(vp, &vattr, cred, p);
152 1.24 fvdl if (error)
153 1.1 cgd return (error);
154 1.22 jtc if (np->n_mtime != vattr.va_mtime.tv_sec) {
155 1.35 fvdl if (vp->v_type == VDIR) {
156 1.35 fvdl nfs_invaldircache(vp, 0);
157 1.35 fvdl np->n_direofoffset = 0;
158 1.35 fvdl }
159 1.23 christos error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
160 1.23 christos if (error)
161 1.12 mycroft return (error);
162 1.22 jtc np->n_mtime = vattr.va_mtime.tv_sec;
163 1.1 cgd }
164 1.1 cgd }
165 1.1 cgd }
166 1.54 chs
167 1.54 chs /*
168 1.54 chs * update the cached read creds for this node.
169 1.54 chs */
170 1.54 chs
171 1.54 chs if (np->n_rcred) {
172 1.54 chs crfree(np->n_rcred);
173 1.54 chs }
174 1.54 chs np->n_rcred = cred;
175 1.54 chs crhold(cred);
176 1.54 chs
177 1.1 cgd do {
178 1.53 bjh21 #ifndef NFS_V2_ONLY
179 1.12 mycroft /*
180 1.12 mycroft * Get a valid lease. If cached data is stale, flush it.
181 1.12 mycroft */
182 1.12 mycroft if (nmp->nm_flag & NFSMNT_NQNFS) {
183 1.24 fvdl if (NQNFS_CKINVALID(vp, np, ND_READ)) {
184 1.12 mycroft do {
185 1.24 fvdl error = nqnfs_getlease(vp, ND_READ, cred, p);
186 1.12 mycroft } while (error == NQNFS_EXPIRED);
187 1.12 mycroft if (error)
188 1.12 mycroft return (error);
189 1.12 mycroft if (np->n_lrev != np->n_brev ||
190 1.12 mycroft (np->n_flag & NQNFSNONCACHE) ||
191 1.12 mycroft ((np->n_flag & NMODIFIED) && vp->v_type == VDIR)) {
192 1.35 fvdl if (vp->v_type == VDIR) {
193 1.35 fvdl nfs_invaldircache(vp, 0);
194 1.35 fvdl np->n_direofoffset = 0;
195 1.35 fvdl }
196 1.23 christos error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
197 1.23 christos if (error)
198 1.12 mycroft return (error);
199 1.12 mycroft np->n_brev = np->n_lrev;
200 1.12 mycroft }
201 1.12 mycroft } else if (vp->v_type == VDIR && (np->n_flag & NMODIFIED)) {
202 1.35 fvdl nfs_invaldircache(vp, 0);
203 1.23 christos error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
204 1.35 fvdl np->n_direofoffset = 0;
205 1.23 christos if (error)
206 1.12 mycroft return (error);
207 1.12 mycroft }
208 1.12 mycroft }
209 1.53 bjh21 #endif
210 1.26 fvdl /*
211 1.26 fvdl * Don't cache symlinks.
212 1.26 fvdl */
213 1.26 fvdl if (np->n_flag & NQNFSNONCACHE
214 1.26 fvdl || ((vp->v_flag & VROOT) && vp->v_type == VLNK)) {
215 1.12 mycroft switch (vp->v_type) {
216 1.12 mycroft case VREG:
217 1.54 chs return (nfs_readrpc(vp, uio));
218 1.12 mycroft case VLNK:
219 1.24 fvdl return (nfs_readlinkrpc(vp, uio, cred));
220 1.12 mycroft case VDIR:
221 1.23 christos break;
222 1.24 fvdl default:
223 1.29 christos printf(" NQNFSNONCACHE: type %x unexpected\n",
224 1.28 christos vp->v_type);
225 1.12 mycroft };
226 1.12 mycroft }
227 1.12 mycroft baddr = (caddr_t)0;
228 1.1 cgd switch (vp->v_type) {
229 1.1 cgd case VREG:
230 1.1 cgd nfsstats.biocache_reads++;
231 1.12 mycroft
232 1.54 chs error = 0;
233 1.63.2.3 nathanw if (uio->uio_offset >= np->n_size) {
234 1.63.2.3 nathanw break;
235 1.63.2.3 nathanw }
236 1.54 chs while (uio->uio_resid > 0) {
237 1.54 chs void *win;
238 1.61 chs vsize_t bytelen = MIN(np->n_size - uio->uio_offset,
239 1.54 chs uio->uio_resid);
240 1.54 chs
241 1.54 chs if (bytelen == 0)
242 1.54 chs break;
243 1.63.2.5 nathanw win = ubc_alloc(&vp->v_uobj, uio->uio_offset,
244 1.54 chs &bytelen, UBC_READ);
245 1.54 chs error = uiomove(win, bytelen, uio);
246 1.54 chs ubc_release(win, 0);
247 1.54 chs if (error) {
248 1.54 chs break;
249 1.12 mycroft }
250 1.12 mycroft }
251 1.54 chs n = 0;
252 1.54 chs break;
253 1.12 mycroft
254 1.1 cgd case VLNK:
255 1.1 cgd nfsstats.biocache_readlinks++;
256 1.12 mycroft bp = nfs_getcacheblk(vp, (daddr_t)0, NFS_MAXPATHLEN, p);
257 1.12 mycroft if (!bp)
258 1.12 mycroft return (EINTR);
259 1.12 mycroft if ((bp->b_flags & B_DONE) == 0) {
260 1.12 mycroft bp->b_flags |= B_READ;
261 1.54 chs error = nfs_doio(bp, p);
262 1.24 fvdl if (error) {
263 1.12 mycroft brelse(bp);
264 1.12 mycroft return (error);
265 1.12 mycroft }
266 1.12 mycroft }
267 1.63 chs n = MIN(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid);
268 1.12 mycroft got_buf = 1;
269 1.1 cgd on = 0;
270 1.1 cgd break;
271 1.1 cgd case VDIR:
272 1.34 fvdl diragain:
273 1.34 fvdl nfsstats.biocache_readdirs++;
274 1.35 fvdl ndp = nfs_searchdircache(vp, uio->uio_offset,
275 1.35 fvdl (nmp->nm_flag & NFSMNT_XLATECOOKIE), 0);
276 1.35 fvdl if (!ndp) {
277 1.35 fvdl /*
278 1.35 fvdl * We've been handed a cookie that is not
279 1.35 fvdl * in the cache. If we're not translating
280 1.35 fvdl * 32 <-> 64, it may be a value that was
281 1.35 fvdl * flushed out of the cache because it grew
282 1.35 fvdl * too big. Let the server judge if it's
283 1.35 fvdl * valid or not. In the translation case,
284 1.35 fvdl * we have no way of validating this value,
285 1.35 fvdl * so punt.
286 1.35 fvdl */
287 1.35 fvdl if (nmp->nm_flag & NFSMNT_XLATECOOKIE)
288 1.35 fvdl return (EINVAL);
289 1.35 fvdl ndp = nfs_enterdircache(vp, uio->uio_offset,
290 1.35 fvdl uio->uio_offset, 0, 0);
291 1.35 fvdl }
292 1.35 fvdl
293 1.34 fvdl if (uio->uio_offset != 0 &&
294 1.35 fvdl ndp->dc_cookie == np->n_direofoffset) {
295 1.35 fvdl nfsstats.direofcache_hits++;
296 1.18 mycroft return (0);
297 1.35 fvdl }
298 1.35 fvdl
299 1.34 fvdl bp = nfs_getcacheblk(vp, ndp->dc_blkno, NFS_DIRBLKSIZ, p);
300 1.12 mycroft if (!bp)
301 1.24 fvdl return (EINTR);
302 1.12 mycroft if ((bp->b_flags & B_DONE) == 0) {
303 1.24 fvdl bp->b_flags |= B_READ;
304 1.35 fvdl bp->b_dcookie = ndp->dc_blkcookie;
305 1.54 chs error = nfs_doio(bp, p);
306 1.24 fvdl if (error) {
307 1.34 fvdl /*
308 1.34 fvdl * Yuck! The directory has been modified on the
309 1.34 fvdl * server. Punt and let the userland code
310 1.34 fvdl * deal with it.
311 1.34 fvdl */
312 1.24 fvdl brelse(bp);
313 1.34 fvdl if (error == NFSERR_BAD_COOKIE) {
314 1.35 fvdl nfs_invaldircache(vp, 0);
315 1.34 fvdl nfs_vinvalbuf(vp, 0, cred, p, 1);
316 1.34 fvdl error = EINVAL;
317 1.12 mycroft }
318 1.34 fvdl return (error);
319 1.38 fvdl }
320 1.40 fvdl }
321 1.40 fvdl
322 1.40 fvdl /*
323 1.40 fvdl * Just return if we hit EOF right away with this
324 1.40 fvdl * block. Always check here, because direofoffset
325 1.40 fvdl * may have been set by an nfsiod since the last
326 1.40 fvdl * check.
327 1.40 fvdl */
328 1.40 fvdl if (np->n_direofoffset != 0 &&
329 1.39 fvdl ndp->dc_blkcookie == np->n_direofoffset) {
330 1.40 fvdl brelse(bp);
331 1.40 fvdl return (0);
332 1.12 mycroft }
333 1.12 mycroft
334 1.12 mycroft /*
335 1.34 fvdl * Find the entry we were looking for in the block.
336 1.34 fvdl */
337 1.34 fvdl
338 1.34 fvdl en = ndp->dc_entry;
339 1.34 fvdl
340 1.34 fvdl pdp = dp = (struct dirent *)bp->b_data;
341 1.63.2.2 nathanw edp = bp->b_data + bp->b_bcount - bp->b_resid;
342 1.34 fvdl enn = 0;
343 1.34 fvdl while (enn < en && (caddr_t)dp < edp) {
344 1.34 fvdl pdp = dp;
345 1.34 fvdl dp = (struct dirent *)((caddr_t)dp + dp->d_reclen);
346 1.34 fvdl enn++;
347 1.34 fvdl }
348 1.34 fvdl
349 1.34 fvdl /*
350 1.34 fvdl * If the entry number was bigger than the number of
351 1.34 fvdl * entries in the block, or the cookie of the previous
352 1.34 fvdl * entry doesn't match, the directory cache is
353 1.34 fvdl * stale. Flush it and try again (i.e. go to
354 1.34 fvdl * the server).
355 1.34 fvdl */
356 1.34 fvdl if ((caddr_t)dp >= edp || (caddr_t)dp + dp->d_reclen > edp ||
357 1.35 fvdl (en > 0 && NFS_GETCOOKIE(pdp) != ndp->dc_cookie)) {
358 1.34 fvdl #ifdef DEBUG
359 1.37 thorpej printf("invalid cache: %p %p %p off %lx %lx\n",
360 1.37 thorpej pdp, dp, edp,
361 1.34 fvdl (unsigned long)uio->uio_offset,
362 1.34 fvdl (unsigned long)NFS_GETCOOKIE(pdp));
363 1.34 fvdl #endif
364 1.34 fvdl brelse(bp);
365 1.35 fvdl nfs_invaldircache(vp, 0);
366 1.34 fvdl nfs_vinvalbuf(vp, 0, cred, p, 0);
367 1.34 fvdl goto diragain;
368 1.34 fvdl }
369 1.34 fvdl
370 1.34 fvdl on = (caddr_t)dp - bp->b_data;
371 1.34 fvdl
372 1.34 fvdl /*
373 1.34 fvdl * Cache all entries that may be exported to the
374 1.34 fvdl * user, as they may be thrown back at us. The
375 1.34 fvdl * NFSBIO_CACHECOOKIES flag indicates that all
376 1.34 fvdl * entries are being 'exported', so cache them all.
377 1.34 fvdl */
378 1.34 fvdl
379 1.34 fvdl if (en == 0 && pdp == dp) {
380 1.34 fvdl dp = (struct dirent *)
381 1.34 fvdl ((caddr_t)dp + dp->d_reclen);
382 1.34 fvdl enn++;
383 1.34 fvdl }
384 1.34 fvdl
385 1.63.2.2 nathanw if (uio->uio_resid < (bp->b_bcount - bp->b_resid - on)) {
386 1.34 fvdl n = uio->uio_resid;
387 1.34 fvdl enough = 1;
388 1.34 fvdl } else
389 1.63.2.2 nathanw n = bp->b_bcount - bp->b_resid - on;
390 1.34 fvdl
391 1.34 fvdl ep = bp->b_data + on + n;
392 1.34 fvdl
393 1.34 fvdl /*
394 1.34 fvdl * Find last complete entry to copy, caching entries
395 1.34 fvdl * (if requested) as we go.
396 1.34 fvdl */
397 1.34 fvdl
398 1.34 fvdl while ((caddr_t)dp < ep && (caddr_t)dp + dp->d_reclen <= ep) {
399 1.35 fvdl if (cflag & NFSBIO_CACHECOOKIES) {
400 1.35 fvdl nndp = nfs_enterdircache(vp, NFS_GETCOOKIE(pdp),
401 1.35 fvdl ndp->dc_blkcookie, enn, bp->b_lblkno);
402 1.35 fvdl if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
403 1.35 fvdl NFS_STASHCOOKIE32(pdp,
404 1.35 fvdl nndp->dc_cookie32);
405 1.35 fvdl }
406 1.35 fvdl }
407 1.34 fvdl pdp = dp;
408 1.34 fvdl dp = (struct dirent *)((caddr_t)dp + dp->d_reclen);
409 1.34 fvdl enn++;
410 1.34 fvdl }
411 1.34 fvdl
412 1.34 fvdl /*
413 1.34 fvdl * If the last requested entry was not the last in the
414 1.34 fvdl * buffer (happens if NFS_DIRFRAGSIZ < NFS_DIRBLKSIZ),
415 1.34 fvdl * cache the cookie of the last requested one, and
416 1.34 fvdl * set of the offset to it.
417 1.34 fvdl */
418 1.34 fvdl
419 1.63.2.2 nathanw if ((on + n) < bp->b_bcount - bp->b_resid) {
420 1.34 fvdl curoff = NFS_GETCOOKIE(pdp);
421 1.35 fvdl nndp = nfs_enterdircache(vp, curoff, ndp->dc_blkcookie,
422 1.35 fvdl enn, bp->b_lblkno);
423 1.35 fvdl if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
424 1.35 fvdl NFS_STASHCOOKIE32(pdp, nndp->dc_cookie32);
425 1.35 fvdl curoff = nndp->dc_cookie32;
426 1.35 fvdl }
427 1.34 fvdl } else
428 1.34 fvdl curoff = bp->b_dcookie;
429 1.34 fvdl
430 1.35 fvdl /*
431 1.35 fvdl * Always cache the entry for the next block,
432 1.35 fvdl * so that readaheads can use it.
433 1.35 fvdl */
434 1.35 fvdl nndp = nfs_enterdircache(vp, bp->b_dcookie, bp->b_dcookie, 0,0);
435 1.35 fvdl if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
436 1.35 fvdl if (curoff == bp->b_dcookie) {
437 1.35 fvdl NFS_STASHCOOKIE32(pdp, nndp->dc_cookie32);
438 1.35 fvdl curoff = nndp->dc_cookie32;
439 1.35 fvdl }
440 1.35 fvdl }
441 1.35 fvdl
442 1.34 fvdl n = ((caddr_t)pdp + pdp->d_reclen) - (bp->b_data + on);
443 1.34 fvdl
444 1.34 fvdl /*
445 1.12 mycroft * If not eof and read aheads are enabled, start one.
446 1.12 mycroft * (You need the current block first, so that you have the
447 1.24 fvdl * directory offset cookie of the next block.)
448 1.12 mycroft */
449 1.12 mycroft if (nfs_numasync > 0 && nmp->nm_readahead > 0 &&
450 1.34 fvdl np->n_direofoffset == 0 && !(np->n_flag & NQNFSNONCACHE)) {
451 1.35 fvdl rabp = nfs_getcacheblk(vp, nndp->dc_blkno,
452 1.34 fvdl NFS_DIRBLKSIZ, p);
453 1.12 mycroft if (rabp) {
454 1.12 mycroft if ((rabp->b_flags & (B_DONE | B_DELWRI)) == 0) {
455 1.35 fvdl rabp->b_dcookie = nndp->dc_cookie;
456 1.12 mycroft rabp->b_flags |= (B_READ | B_ASYNC);
457 1.54 chs if (nfs_asyncio(rabp)) {
458 1.12 mycroft rabp->b_flags |= B_INVAL;
459 1.12 mycroft brelse(rabp);
460 1.12 mycroft }
461 1.19 mycroft } else
462 1.19 mycroft brelse(rabp);
463 1.12 mycroft }
464 1.12 mycroft }
465 1.12 mycroft got_buf = 1;
466 1.1 cgd break;
467 1.24 fvdl default:
468 1.29 christos printf(" nfsbioread: type %x unexpected\n",vp->v_type);
469 1.23 christos break;
470 1.54 chs }
471 1.12 mycroft
472 1.12 mycroft if (n > 0) {
473 1.12 mycroft if (!baddr)
474 1.12 mycroft baddr = bp->b_data;
475 1.12 mycroft error = uiomove(baddr + on, (int)n, uio);
476 1.1 cgd }
477 1.1 cgd switch (vp->v_type) {
478 1.24 fvdl case VREG:
479 1.24 fvdl break;
480 1.1 cgd case VLNK:
481 1.1 cgd n = 0;
482 1.1 cgd break;
483 1.1 cgd case VDIR:
484 1.24 fvdl if (np->n_flag & NQNFSNONCACHE)
485 1.24 fvdl bp->b_flags |= B_INVAL;
486 1.34 fvdl uio->uio_offset = curoff;
487 1.34 fvdl if (enough)
488 1.34 fvdl n = 0;
489 1.1 cgd break;
490 1.24 fvdl default:
491 1.29 christos printf(" nfsbioread: type %x unexpected\n",vp->v_type);
492 1.24 fvdl }
493 1.12 mycroft if (got_buf)
494 1.12 mycroft brelse(bp);
495 1.12 mycroft } while (error == 0 && uio->uio_resid > 0 && n > 0);
496 1.1 cgd return (error);
497 1.1 cgd }
498 1.1 cgd
499 1.1 cgd /*
500 1.1 cgd * Vnode op for write using bio
501 1.1 cgd */
502 1.23 christos int
503 1.23 christos nfs_write(v)
504 1.23 christos void *v;
505 1.23 christos {
506 1.12 mycroft struct vop_write_args /* {
507 1.24 fvdl struct vnode *a_vp;
508 1.12 mycroft struct uio *a_uio;
509 1.12 mycroft int a_ioflag;
510 1.12 mycroft struct ucred *a_cred;
511 1.23 christos } */ *ap = v;
512 1.48 augustss struct uio *uio = ap->a_uio;
513 1.1 cgd struct proc *p = uio->uio_procp;
514 1.48 augustss struct vnode *vp = ap->a_vp;
515 1.12 mycroft struct nfsnode *np = VTONFS(vp);
516 1.48 augustss struct ucred *cred = ap->a_cred;
517 1.12 mycroft int ioflag = ap->a_ioflag;
518 1.1 cgd struct vattr vattr;
519 1.24 fvdl struct nfsmount *nmp = VFSTONFS(vp->v_mount);
520 1.63.2.5 nathanw void *win;
521 1.63.2.5 nathanw voff_t oldoff, origoff;
522 1.63.2.5 nathanw vsize_t bytelen;
523 1.54 chs int error = 0, iomode, must_commit;
524 1.1 cgd
525 1.1 cgd #ifdef DIAGNOSTIC
526 1.1 cgd if (uio->uio_rw != UIO_WRITE)
527 1.1 cgd panic("nfs_write mode");
528 1.63.2.1 nathanw if (uio->uio_segflg == UIO_USERSPACE &&
529 1.63.2.14 nathanw uio->uio_procp != curproc)
530 1.1 cgd panic("nfs_write proc");
531 1.1 cgd #endif
532 1.1 cgd if (vp->v_type != VREG)
533 1.1 cgd return (EIO);
534 1.12 mycroft if (np->n_flag & NWRITEERR) {
535 1.12 mycroft np->n_flag &= ~NWRITEERR;
536 1.12 mycroft return (np->n_error);
537 1.12 mycroft }
538 1.51 bjh21 #ifndef NFS_V2_ONLY
539 1.34 fvdl if ((nmp->nm_flag & NFSMNT_NFSV3) &&
540 1.34 fvdl !(nmp->nm_iflag & NFSMNT_GOTFSINFO))
541 1.24 fvdl (void)nfs_fsinfo(nmp, vp, cred, p);
542 1.51 bjh21 #endif
543 1.1 cgd if (ioflag & (IO_APPEND | IO_SYNC)) {
544 1.1 cgd if (np->n_flag & NMODIFIED) {
545 1.12 mycroft np->n_attrstamp = 0;
546 1.23 christos error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
547 1.23 christos if (error)
548 1.12 mycroft return (error);
549 1.1 cgd }
550 1.1 cgd if (ioflag & IO_APPEND) {
551 1.1 cgd np->n_attrstamp = 0;
552 1.23 christos error = VOP_GETATTR(vp, &vattr, cred, p);
553 1.23 christos if (error)
554 1.1 cgd return (error);
555 1.1 cgd uio->uio_offset = np->n_size;
556 1.1 cgd }
557 1.1 cgd }
558 1.1 cgd if (uio->uio_offset < 0)
559 1.1 cgd return (EINVAL);
560 1.33 fvdl if ((uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
561 1.33 fvdl return (EFBIG);
562 1.1 cgd if (uio->uio_resid == 0)
563 1.1 cgd return (0);
564 1.1 cgd /*
565 1.1 cgd * Maybe this should be above the vnode op call, but so long as
566 1.1 cgd * file servers have no limits, i don't think it matters
567 1.1 cgd */
568 1.12 mycroft if (p && uio->uio_offset + uio->uio_resid >
569 1.1 cgd p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
570 1.1 cgd psignal(p, SIGXFSZ);
571 1.1 cgd return (EFBIG);
572 1.1 cgd }
573 1.54 chs
574 1.1 cgd /*
575 1.54 chs * update the cached write creds for this node.
576 1.1 cgd */
577 1.54 chs
578 1.54 chs if (np->n_wcred) {
579 1.54 chs crfree(np->n_wcred);
580 1.54 chs }
581 1.54 chs np->n_wcred = cred;
582 1.54 chs crhold(cred);
583 1.54 chs
584 1.54 chs if ((np->n_flag & NQNFSNONCACHE) && uio->uio_iovcnt == 1) {
585 1.54 chs iomode = NFSV3WRITE_FILESYNC;
586 1.54 chs error = nfs_writerpc(vp, uio, &iomode, &must_commit);
587 1.54 chs if (must_commit)
588 1.54 chs nfs_clearcommit(vp->v_mount);
589 1.54 chs return (error);
590 1.54 chs }
591 1.54 chs
592 1.63.2.5 nathanw origoff = uio->uio_offset;
593 1.1 cgd do {
594 1.63.2.5 nathanw oldoff = uio->uio_offset;
595 1.63.2.5 nathanw bytelen = uio->uio_resid;
596 1.12 mycroft
597 1.53 bjh21 #ifndef NFS_V2_ONLY
598 1.12 mycroft /*
599 1.12 mycroft * Check for a valid write lease.
600 1.12 mycroft */
601 1.12 mycroft if ((nmp->nm_flag & NFSMNT_NQNFS) &&
602 1.24 fvdl NQNFS_CKINVALID(vp, np, ND_WRITE)) {
603 1.12 mycroft do {
604 1.24 fvdl error = nqnfs_getlease(vp, ND_WRITE, cred, p);
605 1.12 mycroft } while (error == NQNFS_EXPIRED);
606 1.12 mycroft if (error)
607 1.12 mycroft return (error);
608 1.12 mycroft if (np->n_lrev != np->n_brev ||
609 1.12 mycroft (np->n_flag & NQNFSNONCACHE)) {
610 1.23 christos error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
611 1.23 christos if (error)
612 1.12 mycroft return (error);
613 1.12 mycroft np->n_brev = np->n_lrev;
614 1.12 mycroft }
615 1.12 mycroft }
616 1.53 bjh21 #endif
617 1.1 cgd nfsstats.biocache_writes++;
618 1.54 chs
619 1.12 mycroft np->n_flag |= NMODIFIED;
620 1.54 chs if (np->n_size < uio->uio_offset + bytelen) {
621 1.54 chs np->n_size = uio->uio_offset + bytelen;
622 1.12 mycroft }
623 1.63.2.5 nathanw if ((uio->uio_offset & PAGE_MASK) == 0 &&
624 1.63.2.12 nathanw (bytelen & PAGE_MASK) == 0 &&
625 1.63.2.12 nathanw uio->uio_offset >= vp->v_size) {
626 1.63.2.5 nathanw win = ubc_alloc(&vp->v_uobj, uio->uio_offset, &bytelen,
627 1.63.2.5 nathanw UBC_WRITE | UBC_FAULTBUSY);
628 1.63.2.5 nathanw } else {
629 1.63.2.5 nathanw win = ubc_alloc(&vp->v_uobj, uio->uio_offset, &bytelen,
630 1.63.2.5 nathanw UBC_WRITE);
631 1.63.2.5 nathanw }
632 1.54 chs error = uiomove(win, bytelen, uio);
633 1.54 chs ubc_release(win, 0);
634 1.58 chs if (error) {
635 1.54 chs break;
636 1.52 fvdl }
637 1.63.2.5 nathanw
638 1.63.2.5 nathanw /*
639 1.63.2.5 nathanw * update UVM's notion of the size now that we've
640 1.63.2.5 nathanw * copied the data into the vnode's pages.
641 1.63.2.5 nathanw */
642 1.63.2.5 nathanw
643 1.63.2.5 nathanw if (vp->v_size < uio->uio_offset) {
644 1.63.2.5 nathanw uvm_vnp_setsize(vp, uio->uio_offset);
645 1.63.2.5 nathanw }
646 1.63.2.5 nathanw
647 1.63.2.5 nathanw if ((oldoff & ~(nmp->nm_wsize - 1)) !=
648 1.63.2.5 nathanw (uio->uio_offset & ~(nmp->nm_wsize - 1))) {
649 1.63.2.8 nathanw simple_lock(&vp->v_interlock);
650 1.63.2.8 nathanw error = VOP_PUTPAGES(vp,
651 1.63.2.5 nathanw trunc_page(oldoff & ~(nmp->nm_wsize - 1)),
652 1.63.2.5 nathanw round_page((uio->uio_offset + nmp->nm_wsize - 1) &
653 1.63.2.11 nathanw ~(nmp->nm_wsize - 1)), PGO_CLEANIT);
654 1.63.2.5 nathanw }
655 1.54 chs } while (uio->uio_resid > 0);
656 1.63.2.5 nathanw if ((np->n_flag & NQNFSNONCACHE) || (ioflag & IO_SYNC)) {
657 1.63.2.8 nathanw simple_lock(&vp->v_interlock);
658 1.63.2.8 nathanw error = VOP_PUTPAGES(vp,
659 1.63.2.5 nathanw trunc_page(origoff & ~(nmp->nm_wsize - 1)),
660 1.63.2.5 nathanw round_page((uio->uio_offset + nmp->nm_wsize - 1) &
661 1.63.2.5 nathanw ~(nmp->nm_wsize - 1)),
662 1.63.2.8 nathanw PGO_CLEANIT | PGO_SYNCIO);
663 1.63.2.5 nathanw }
664 1.54 chs return error;
665 1.12 mycroft }
666 1.12 mycroft
667 1.12 mycroft /*
668 1.12 mycroft * Get an nfs cache block.
669 1.12 mycroft * Allocate a new one if the block isn't currently in the cache
670 1.12 mycroft * and return the block marked busy. If the calling process is
671 1.12 mycroft * interrupted by a signal for an interruptible mount point, return
672 1.12 mycroft * NULL.
673 1.12 mycroft */
674 1.12 mycroft struct buf *
675 1.12 mycroft nfs_getcacheblk(vp, bn, size, p)
676 1.12 mycroft struct vnode *vp;
677 1.12 mycroft daddr_t bn;
678 1.12 mycroft int size;
679 1.12 mycroft struct proc *p;
680 1.12 mycroft {
681 1.48 augustss struct buf *bp;
682 1.12 mycroft struct nfsmount *nmp = VFSTONFS(vp->v_mount);
683 1.12 mycroft
684 1.12 mycroft if (nmp->nm_flag & NFSMNT_INT) {
685 1.12 mycroft bp = getblk(vp, bn, size, PCATCH, 0);
686 1.54 chs while (bp == NULL) {
687 1.54 chs if (nfs_sigintr(nmp, NULL, p))
688 1.54 chs return (NULL);
689 1.12 mycroft bp = getblk(vp, bn, size, 0, 2 * hz);
690 1.12 mycroft }
691 1.12 mycroft } else
692 1.12 mycroft bp = getblk(vp, bn, size, 0, 0);
693 1.12 mycroft return (bp);
694 1.12 mycroft }
695 1.12 mycroft
696 1.12 mycroft /*
697 1.12 mycroft * Flush and invalidate all dirty buffers. If another process is already
698 1.12 mycroft * doing the flush, just wait for completion.
699 1.12 mycroft */
700 1.23 christos int
701 1.12 mycroft nfs_vinvalbuf(vp, flags, cred, p, intrflg)
702 1.12 mycroft struct vnode *vp;
703 1.12 mycroft int flags;
704 1.12 mycroft struct ucred *cred;
705 1.12 mycroft struct proc *p;
706 1.12 mycroft int intrflg;
707 1.12 mycroft {
708 1.48 augustss struct nfsnode *np = VTONFS(vp);
709 1.12 mycroft struct nfsmount *nmp = VFSTONFS(vp->v_mount);
710 1.12 mycroft int error = 0, slpflag, slptimeo;
711 1.12 mycroft
712 1.12 mycroft if ((nmp->nm_flag & NFSMNT_INT) == 0)
713 1.12 mycroft intrflg = 0;
714 1.12 mycroft if (intrflg) {
715 1.12 mycroft slpflag = PCATCH;
716 1.12 mycroft slptimeo = 2 * hz;
717 1.12 mycroft } else {
718 1.12 mycroft slpflag = 0;
719 1.12 mycroft slptimeo = 0;
720 1.12 mycroft }
721 1.12 mycroft /*
722 1.12 mycroft * First wait for any other process doing a flush to complete.
723 1.12 mycroft */
724 1.12 mycroft while (np->n_flag & NFLUSHINPROG) {
725 1.12 mycroft np->n_flag |= NFLUSHWANT;
726 1.12 mycroft error = tsleep((caddr_t)&np->n_flag, PRIBIO + 2, "nfsvinval",
727 1.12 mycroft slptimeo);
728 1.54 chs if (error && intrflg && nfs_sigintr(nmp, NULL, p))
729 1.12 mycroft return (EINTR);
730 1.12 mycroft }
731 1.12 mycroft
732 1.12 mycroft /*
733 1.12 mycroft * Now, flush as required.
734 1.12 mycroft */
735 1.12 mycroft np->n_flag |= NFLUSHINPROG;
736 1.12 mycroft error = vinvalbuf(vp, flags, cred, p, slpflag, 0);
737 1.12 mycroft while (error) {
738 1.54 chs if (intrflg && nfs_sigintr(nmp, NULL, p)) {
739 1.12 mycroft np->n_flag &= ~NFLUSHINPROG;
740 1.12 mycroft if (np->n_flag & NFLUSHWANT) {
741 1.12 mycroft np->n_flag &= ~NFLUSHWANT;
742 1.12 mycroft wakeup((caddr_t)&np->n_flag);
743 1.12 mycroft }
744 1.12 mycroft return (EINTR);
745 1.12 mycroft }
746 1.12 mycroft error = vinvalbuf(vp, flags, cred, p, 0, slptimeo);
747 1.12 mycroft }
748 1.12 mycroft np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
749 1.12 mycroft if (np->n_flag & NFLUSHWANT) {
750 1.12 mycroft np->n_flag &= ~NFLUSHWANT;
751 1.12 mycroft wakeup((caddr_t)&np->n_flag);
752 1.12 mycroft }
753 1.12 mycroft return (0);
754 1.12 mycroft }
755 1.12 mycroft
756 1.12 mycroft /*
757 1.12 mycroft * Initiate asynchronous I/O. Return an error if no nfsiods are available.
758 1.12 mycroft * This is mainly to avoid queueing async I/O requests when the nfsiods
759 1.12 mycroft * are all hung on a dead server.
760 1.12 mycroft */
761 1.63.2.5 nathanw
762 1.23 christos int
763 1.54 chs nfs_asyncio(bp)
764 1.48 augustss struct buf *bp;
765 1.12 mycroft {
766 1.48 augustss int i;
767 1.48 augustss struct nfsmount *nmp;
768 1.30 thorpej int gotiod, slpflag = 0, slptimeo = 0, error;
769 1.12 mycroft
770 1.12 mycroft if (nfs_numasync == 0)
771 1.12 mycroft return (EIO);
772 1.30 thorpej
773 1.30 thorpej nmp = VFSTONFS(bp->b_vp->v_mount);
774 1.30 thorpej again:
775 1.30 thorpej if (nmp->nm_flag & NFSMNT_INT)
776 1.30 thorpej slpflag = PCATCH;
777 1.30 thorpej gotiod = FALSE;
778 1.30 thorpej
779 1.30 thorpej /*
780 1.30 thorpej * Find a free iod to process this request.
781 1.30 thorpej */
782 1.30 thorpej
783 1.12 mycroft for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
784 1.30 thorpej if (nfs_iodwant[i]) {
785 1.30 thorpej /*
786 1.30 thorpej * Found one, so wake it up and tell it which
787 1.30 thorpej * mount to process.
788 1.30 thorpej */
789 1.54 chs nfs_iodwant[i] = NULL;
790 1.30 thorpej nfs_iodmount[i] = nmp;
791 1.30 thorpej nmp->nm_bufqiods++;
792 1.30 thorpej wakeup((caddr_t)&nfs_iodwant[i]);
793 1.30 thorpej gotiod = TRUE;
794 1.31 fvdl break;
795 1.30 thorpej }
796 1.63.2.9 nathanw
797 1.30 thorpej /*
798 1.30 thorpej * If none are free, we may already have an iod working on this mount
799 1.30 thorpej * point. If so, it will process our request.
800 1.30 thorpej */
801 1.63.2.9 nathanw
802 1.30 thorpej if (!gotiod && nmp->nm_bufqiods > 0)
803 1.30 thorpej gotiod = TRUE;
804 1.30 thorpej
805 1.30 thorpej /*
806 1.30 thorpej * If we have an iod which can process the request, then queue
807 1.30 thorpej * the buffer.
808 1.30 thorpej */
809 1.63.2.9 nathanw
810 1.30 thorpej if (gotiod) {
811 1.63.2.9 nathanw
812 1.30 thorpej /*
813 1.30 thorpej * Ensure that the queue never grows too large.
814 1.30 thorpej */
815 1.63.2.9 nathanw
816 1.30 thorpej while (nmp->nm_bufqlen >= 2*nfs_numasync) {
817 1.30 thorpej nmp->nm_bufqwant = TRUE;
818 1.30 thorpej error = tsleep(&nmp->nm_bufq, slpflag | PRIBIO,
819 1.30 thorpej "nfsaio", slptimeo);
820 1.30 thorpej if (error) {
821 1.63.2.14 nathanw if (nfs_sigintr(nmp, NULL, curproc))
822 1.30 thorpej return (EINTR);
823 1.30 thorpej if (slpflag == PCATCH) {
824 1.30 thorpej slpflag = 0;
825 1.30 thorpej slptimeo = 2 * hz;
826 1.30 thorpej }
827 1.30 thorpej }
828 1.63.2.9 nathanw
829 1.30 thorpej /*
830 1.30 thorpej * We might have lost our iod while sleeping,
831 1.30 thorpej * so check and loop if nescessary.
832 1.30 thorpej */
833 1.63.2.9 nathanw
834 1.30 thorpej if (nmp->nm_bufqiods == 0)
835 1.30 thorpej goto again;
836 1.30 thorpej }
837 1.30 thorpej TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist);
838 1.30 thorpej nmp->nm_bufqlen++;
839 1.12 mycroft return (0);
840 1.63.2.9 nathanw }
841 1.24 fvdl
842 1.24 fvdl /*
843 1.30 thorpej * All the iods are busy on other mounts, so return EIO to
844 1.30 thorpej * force the caller to process the i/o synchronously.
845 1.24 fvdl */
846 1.63.2.9 nathanw
847 1.30 thorpej return (EIO);
848 1.12 mycroft }
849 1.12 mycroft
850 1.12 mycroft /*
851 1.12 mycroft * Do an I/O operation to/from a cache block. This may be called
852 1.12 mycroft * synchronously or from an nfsiod.
853 1.12 mycroft */
854 1.12 mycroft int
855 1.54 chs nfs_doio(bp, p)
856 1.48 augustss struct buf *bp;
857 1.12 mycroft struct proc *p;
858 1.12 mycroft {
859 1.48 augustss struct uio *uiop;
860 1.48 augustss struct vnode *vp;
861 1.12 mycroft struct nfsnode *np;
862 1.12 mycroft struct nfsmount *nmp;
863 1.54 chs int error = 0, diff, len, iomode, must_commit = 0;
864 1.63.2.9 nathanw int pushedrange;
865 1.12 mycroft struct uio uio;
866 1.12 mycroft struct iovec io;
867 1.63.2.9 nathanw off_t off, cnt;
868 1.63.2.9 nathanw struct uvm_object *uobj;
869 1.63.2.9 nathanw UVMHIST_FUNC("nfs_doio"); UVMHIST_CALLED(ubchist);
870 1.12 mycroft
871 1.12 mycroft vp = bp->b_vp;
872 1.63.2.9 nathanw uobj = &vp->v_uobj;
873 1.12 mycroft np = VTONFS(vp);
874 1.12 mycroft nmp = VFSTONFS(vp->v_mount);
875 1.12 mycroft uiop = &uio;
876 1.12 mycroft uiop->uio_iov = &io;
877 1.12 mycroft uiop->uio_iovcnt = 1;
878 1.12 mycroft uiop->uio_segflg = UIO_SYSSPACE;
879 1.12 mycroft uiop->uio_procp = p;
880 1.12 mycroft
881 1.12 mycroft /*
882 1.14 pk * Historically, paging was done with physio, but no more...
883 1.12 mycroft */
884 1.14 pk if (bp->b_flags & B_PHYS) {
885 1.14 pk /*
886 1.14 pk * ...though reading /dev/drum still gets us here.
887 1.14 pk */
888 1.14 pk io.iov_len = uiop->uio_resid = bp->b_bcount;
889 1.14 pk /* mapping was done by vmapbuf() */
890 1.14 pk io.iov_base = bp->b_data;
891 1.54 chs uiop->uio_offset = ((off_t)bp->b_blkno) << DEV_BSHIFT;
892 1.14 pk if (bp->b_flags & B_READ) {
893 1.14 pk uiop->uio_rw = UIO_READ;
894 1.14 pk nfsstats.read_physios++;
895 1.54 chs error = nfs_readrpc(vp, uiop);
896 1.14 pk } else {
897 1.24 fvdl iomode = NFSV3WRITE_DATASYNC;
898 1.14 pk uiop->uio_rw = UIO_WRITE;
899 1.14 pk nfsstats.write_physios++;
900 1.54 chs error = nfs_writerpc(vp, uiop, &iomode, &must_commit);
901 1.14 pk }
902 1.14 pk if (error) {
903 1.14 pk bp->b_flags |= B_ERROR;
904 1.14 pk bp->b_error = error;
905 1.14 pk }
906 1.14 pk } else if (bp->b_flags & B_READ) {
907 1.12 mycroft io.iov_len = uiop->uio_resid = bp->b_bcount;
908 1.12 mycroft io.iov_base = bp->b_data;
909 1.12 mycroft uiop->uio_rw = UIO_READ;
910 1.12 mycroft switch (vp->v_type) {
911 1.12 mycroft case VREG:
912 1.54 chs uiop->uio_offset = ((off_t)bp->b_blkno) << DEV_BSHIFT;
913 1.12 mycroft nfsstats.read_bios++;
914 1.54 chs error = nfs_readrpc(vp, uiop);
915 1.54 chs if (!error && uiop->uio_resid) {
916 1.54 chs
917 1.12 mycroft /*
918 1.12 mycroft * If len > 0, there is a hole in the file and
919 1.12 mycroft * no writes after the hole have been pushed to
920 1.12 mycroft * the server yet.
921 1.12 mycroft * Just zero fill the rest of the valid area.
922 1.12 mycroft */
923 1.54 chs
924 1.12 mycroft diff = bp->b_bcount - uiop->uio_resid;
925 1.54 chs len = np->n_size - ((((off_t)bp->b_blkno) << DEV_BSHIFT)
926 1.12 mycroft + diff);
927 1.12 mycroft if (len > 0) {
928 1.63 chs len = MIN(len, uiop->uio_resid);
929 1.54 chs memset((char *)bp->b_data + diff, 0, len);
930 1.54 chs }
931 1.12 mycroft }
932 1.12 mycroft if (p && (vp->v_flag & VTEXT) &&
933 1.12 mycroft (((nmp->nm_flag & NFSMNT_NQNFS) &&
934 1.24 fvdl NQNFS_CKINVALID(vp, np, ND_READ) &&
935 1.12 mycroft np->n_lrev != np->n_brev) ||
936 1.12 mycroft (!(nmp->nm_flag & NFSMNT_NQNFS) &&
937 1.35 fvdl np->n_mtime != np->n_vattr->va_mtime.tv_sec))) {
938 1.54 chs uprintf("Process killed due to "
939 1.54 chs "text file modification\n");
940 1.12 mycroft psignal(p, SIGKILL);
941 1.63.2.1 nathanw #if 0 /* XXX NJWLWP */
942 1.13 mycroft p->p_holdcnt++;
943 1.63.2.1 nathanw #endif
944 1.12 mycroft }
945 1.12 mycroft break;
946 1.12 mycroft case VLNK:
947 1.24 fvdl uiop->uio_offset = (off_t)0;
948 1.12 mycroft nfsstats.readlink_bios++;
949 1.63.2.14 nathanw error = nfs_readlinkrpc(vp, uiop, curproc->p_ucred);
950 1.12 mycroft break;
951 1.12 mycroft case VDIR:
952 1.12 mycroft nfsstats.readdir_bios++;
953 1.34 fvdl uiop->uio_offset = bp->b_dcookie;
954 1.24 fvdl if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
955 1.63.2.14 nathanw error = nfs_readdirplusrpc(vp, uiop, curproc->p_ucred);
956 1.24 fvdl if (error == NFSERR_NOTSUPP)
957 1.24 fvdl nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
958 1.24 fvdl }
959 1.24 fvdl if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
960 1.63.2.14 nathanw error = nfs_readdirrpc(vp, uiop, curproc->p_ucred);
961 1.34 fvdl if (!error) {
962 1.34 fvdl bp->b_dcookie = uiop->uio_offset;
963 1.34 fvdl }
964 1.24 fvdl break;
965 1.24 fvdl default:
966 1.29 christos printf("nfs_doio: type %x unexpected\n",vp->v_type);
967 1.12 mycroft break;
968 1.54 chs }
969 1.12 mycroft if (error) {
970 1.12 mycroft bp->b_flags |= B_ERROR;
971 1.12 mycroft bp->b_error = error;
972 1.12 mycroft }
973 1.12 mycroft } else {
974 1.63.2.9 nathanw int i, npages = bp->b_bufsize >> PAGE_SHIFT;
975 1.63.2.9 nathanw struct vm_page *pgs[npages];
976 1.63.2.9 nathanw boolean_t needcommit = TRUE;
977 1.63.2.9 nathanw
978 1.63.2.11 nathanw if ((bp->b_flags & B_ASYNC) != 0 && NFS_ISV3(vp)) {
979 1.63.2.9 nathanw iomode = NFSV3WRITE_UNSTABLE;
980 1.63.2.9 nathanw } else {
981 1.63.2.9 nathanw iomode = NFSV3WRITE_FILESYNC;
982 1.63.2.9 nathanw }
983 1.63.2.9 nathanw
984 1.63.2.9 nathanw for (i = 0; i < npages; i++) {
985 1.63.2.9 nathanw pgs[i] = uvm_pageratop((vaddr_t)bp->b_data +
986 1.63.2.9 nathanw (i << PAGE_SHIFT));
987 1.63.2.9 nathanw if ((pgs[i]->flags & PG_NEEDCOMMIT) == 0) {
988 1.63.2.9 nathanw needcommit = FALSE;
989 1.63.2.9 nathanw }
990 1.63.2.9 nathanw }
991 1.63.2.9 nathanw if (!needcommit && iomode == NFSV3WRITE_UNSTABLE) {
992 1.63.2.9 nathanw for (i = 0; i < npages; i++) {
993 1.63.2.9 nathanw pgs[i]->flags |= PG_NEEDCOMMIT | PG_RDONLY;
994 1.63.2.9 nathanw pmap_page_protect(pgs[i], VM_PROT_READ);
995 1.63.2.9 nathanw }
996 1.63.2.9 nathanw }
997 1.63.2.9 nathanw
998 1.63.2.9 nathanw uiop->uio_offset = (((off_t)bp->b_blkno) << DEV_BSHIFT);
999 1.63.2.9 nathanw off = uiop->uio_offset;
1000 1.63.2.9 nathanw cnt = bp->b_bcount;
1001 1.63.2.9 nathanw
1002 1.52 fvdl /*
1003 1.63.2.9 nathanw * Send the data to the server if necessary,
1004 1.63.2.9 nathanw * otherwise just send a commit rpc.
1005 1.52 fvdl */
1006 1.54 chs
1007 1.63.2.9 nathanw if (needcommit) {
1008 1.63.2.9 nathanw
1009 1.63.2.9 nathanw /*
1010 1.63.2.9 nathanw * If the buffer is in the range that we already committed,
1011 1.63.2.9 nathanw * there's nothing to do.
1012 1.63.2.9 nathanw *
1013 1.63.2.9 nathanw * If it's in the range that we need to commit, push the
1014 1.63.2.9 nathanw * whole range at once, otherwise only push the buffer.
1015 1.63.2.9 nathanw * In both these cases, acquire the commit lock to avoid
1016 1.63.2.9 nathanw * other processes modifying the range.
1017 1.63.2.9 nathanw */
1018 1.63.2.9 nathanw
1019 1.63.2.9 nathanw lockmgr(&np->n_commitlock, LK_EXCLUSIVE, NULL);
1020 1.63.2.9 nathanw if (!nfs_in_committed_range(vp, off, bp->b_bcount)) {
1021 1.63.2.9 nathanw if (nfs_in_tobecommitted_range(vp, off, bp->b_bcount)) {
1022 1.63.2.9 nathanw pushedrange = 1;
1023 1.63.2.9 nathanw off = np->n_pushlo;
1024 1.63.2.9 nathanw cnt = np->n_pushhi - np->n_pushlo;
1025 1.63.2.9 nathanw } else {
1026 1.63.2.9 nathanw pushedrange = 0;
1027 1.63.2.9 nathanw }
1028 1.63.2.14 nathanw error = nfs_commit(vp, off, cnt, curproc);
1029 1.63.2.9 nathanw if (error == 0) {
1030 1.63.2.9 nathanw if (pushedrange) {
1031 1.63.2.9 nathanw nfs_merge_commit_ranges(vp);
1032 1.63.2.9 nathanw } else {
1033 1.63.2.9 nathanw nfs_add_committed_range(vp, off, cnt);
1034 1.63.2.9 nathanw }
1035 1.63.2.9 nathanw }
1036 1.63.2.9 nathanw }
1037 1.63.2.9 nathanw lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
1038 1.63.2.9 nathanw if (!error) {
1039 1.63.2.9 nathanw bp->b_resid = 0;
1040 1.63.2.9 nathanw simple_lock(&uobj->vmobjlock);
1041 1.63.2.9 nathanw for (i = 0; i < npages; i++) {
1042 1.63.2.9 nathanw pgs[i]->flags &= ~(PG_NEEDCOMMIT | PG_RDONLY);
1043 1.63.2.9 nathanw }
1044 1.63.2.9 nathanw simple_unlock(&uobj->vmobjlock);
1045 1.63.2.9 nathanw biodone(bp);
1046 1.63.2.9 nathanw return (0);
1047 1.63.2.9 nathanw } else if (error == NFSERR_STALEWRITEVERF) {
1048 1.63.2.9 nathanw nfs_clearcommit(bp->b_vp->v_mount);
1049 1.63.2.9 nathanw }
1050 1.63.2.9 nathanw }
1051 1.54 chs io.iov_base = bp->b_data;
1052 1.54 chs io.iov_len = uiop->uio_resid = bp->b_bcount;
1053 1.54 chs uiop->uio_rw = UIO_WRITE;
1054 1.54 chs nfsstats.write_bios++;
1055 1.54 chs error = nfs_writerpc(vp, uiop, &iomode, &must_commit);
1056 1.63.2.9 nathanw if (!error && iomode == NFSV3WRITE_UNSTABLE) {
1057 1.63.2.9 nathanw lockmgr(&np->n_commitlock, LK_EXCLUSIVE, NULL);
1058 1.63.2.9 nathanw nfs_add_tobecommitted_range(vp, off, cnt);
1059 1.63.2.9 nathanw simple_lock(&uobj->vmobjlock);
1060 1.63.2.9 nathanw for (i = 0; i < npages; i++) {
1061 1.63.2.9 nathanw pgs[i]->flags &= ~PG_CLEAN;
1062 1.63.2.9 nathanw }
1063 1.63.2.9 nathanw simple_unlock(&uobj->vmobjlock);
1064 1.63.2.9 nathanw if (np->n_pushhi - np->n_pushlo > nfs_commitsize) {
1065 1.63.2.9 nathanw off = np->n_pushlo;
1066 1.63.2.9 nathanw cnt = nfs_commitsize >> 1;
1067 1.63.2.14 nathanw error = nfs_commit(vp, off, cnt, curproc);
1068 1.63.2.9 nathanw if (!error) {
1069 1.63.2.9 nathanw nfs_add_committed_range(vp, off, cnt);
1070 1.63.2.9 nathanw nfs_del_tobecommitted_range(vp, off, cnt);
1071 1.63.2.9 nathanw }
1072 1.63.2.9 nathanw }
1073 1.63.2.9 nathanw lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
1074 1.63.2.9 nathanw } else if (!error && needcommit) {
1075 1.63.2.9 nathanw lockmgr(&np->n_commitlock, LK_EXCLUSIVE, NULL);
1076 1.63.2.9 nathanw nfs_del_committed_range(vp, off, cnt);
1077 1.63.2.9 nathanw lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
1078 1.63.2.9 nathanw simple_lock(&uobj->vmobjlock);
1079 1.63.2.9 nathanw for (i = 0; i < npages; i++) {
1080 1.63.2.9 nathanw pgs[i]->flags &= ~(PG_NEEDCOMMIT | PG_RDONLY);
1081 1.63.2.9 nathanw }
1082 1.63.2.9 nathanw simple_unlock(&uobj->vmobjlock);
1083 1.63.2.9 nathanw }
1084 1.54 chs }
1085 1.54 chs bp->b_resid = uiop->uio_resid;
1086 1.63.2.9 nathanw if (must_commit || (error == NFSERR_STALEWRITEVERF)) {
1087 1.54 chs nfs_clearcommit(vp->v_mount);
1088 1.63.2.9 nathanw }
1089 1.54 chs biodone(bp);
1090 1.54 chs return (error);
1091 1.54 chs }
1092 1.54 chs
1093 1.54 chs /*
1094 1.54 chs * Vnode op for VM getpages.
1095 1.54 chs */
1096 1.63.2.5 nathanw
1097 1.54 chs int
1098 1.54 chs nfs_getpages(v)
1099 1.54 chs void *v;
1100 1.54 chs {
1101 1.54 chs struct vop_getpages_args /* {
1102 1.54 chs struct vnode *a_vp;
1103 1.54 chs voff_t a_offset;
1104 1.63.2.3 nathanw struct vm_page **a_m;
1105 1.54 chs int *a_count;
1106 1.54 chs int a_centeridx;
1107 1.54 chs vm_prot_t a_access_type;
1108 1.54 chs int a_advice;
1109 1.54 chs int a_flags;
1110 1.54 chs } */ *ap = v;
1111 1.54 chs
1112 1.54 chs struct vnode *vp = ap->a_vp;
1113 1.63.2.5 nathanw struct uvm_object *uobj = &vp->v_uobj;
1114 1.54 chs struct nfsnode *np = VTONFS(vp);
1115 1.63.2.13 nathanw const int npages = *ap->a_count;
1116 1.63.2.13 nathanw struct vm_page *pg, **pgs, *opgs[npages];
1117 1.63.2.9 nathanw off_t origoffset, len;
1118 1.63.2.13 nathanw int i, error;
1119 1.54 chs boolean_t v3 = NFS_ISV3(vp);
1120 1.54 chs boolean_t write = (ap->a_access_type & VM_PROT_WRITE) != 0;
1121 1.63.2.8 nathanw boolean_t locked = (ap->a_flags & PGO_LOCKED) != 0;
1122 1.54 chs
1123 1.54 chs /*
1124 1.54 chs * update the cached read creds for this node.
1125 1.54 chs */
1126 1.54 chs
1127 1.54 chs if (np->n_rcred) {
1128 1.54 chs crfree(np->n_rcred);
1129 1.54 chs }
1130 1.63.2.14 nathanw np->n_rcred = curproc->p_ucred;
1131 1.54 chs crhold(np->n_rcred);
1132 1.54 chs
1133 1.54 chs /*
1134 1.63.2.13 nathanw * call the genfs code to get the pages. `pgs' may be NULL
1135 1.63.2.13 nathanw * when doing read-ahead.
1136 1.54 chs */
1137 1.54 chs
1138 1.63.2.13 nathanw pgs = ap->a_m;
1139 1.63.2.13 nathanw if (write && locked && v3) {
1140 1.63.2.13 nathanw KASSERT(pgs != NULL);
1141 1.63.2.13 nathanw #ifdef DEBUG
1142 1.63.2.13 nathanw
1143 1.63.2.13 nathanw /*
1144 1.63.2.13 nathanw * If PGO_LOCKED is set, real pages shouldn't exists
1145 1.63.2.13 nathanw * in the array.
1146 1.63.2.13 nathanw */
1147 1.63.2.13 nathanw
1148 1.63.2.13 nathanw for (i = 0; i < npages; i++)
1149 1.63.2.13 nathanw KDASSERT(pgs[i] == NULL || pgs[i] == PGO_DONTCARE);
1150 1.63.2.13 nathanw #endif
1151 1.63.2.13 nathanw memcpy(opgs, pgs, npages * sizeof(struct vm_pages *));
1152 1.63.2.13 nathanw }
1153 1.63.2.5 nathanw error = genfs_getpages(v);
1154 1.63.2.11 nathanw if (error) {
1155 1.63.2.13 nathanw return (error);
1156 1.54 chs }
1157 1.54 chs
1158 1.54 chs /*
1159 1.63.2.11 nathanw * for read faults where the nfs node is not yet marked NMODIFIED,
1160 1.63.2.11 nathanw * set PG_RDONLY on the pages so that we come back here if someone
1161 1.63.2.11 nathanw * tries to modify later via the mapping that will be entered for
1162 1.63.2.11 nathanw * this fault.
1163 1.54 chs */
1164 1.54 chs
1165 1.63.2.11 nathanw if (!write && (np->n_flag & NMODIFIED) == 0 && pgs != NULL) {
1166 1.63.2.11 nathanw if (!locked) {
1167 1.63.2.11 nathanw simple_lock(&uobj->vmobjlock);
1168 1.63.2.11 nathanw }
1169 1.63.2.11 nathanw for (i = 0; i < npages; i++) {
1170 1.63.2.11 nathanw pg = pgs[i];
1171 1.63.2.11 nathanw if (pg == NULL || pg == PGO_DONTCARE) {
1172 1.63.2.11 nathanw continue;
1173 1.63.2.11 nathanw }
1174 1.63.2.11 nathanw pg->flags |= PG_RDONLY;
1175 1.63.2.11 nathanw }
1176 1.63.2.11 nathanw if (!locked) {
1177 1.63.2.11 nathanw simple_unlock(&uobj->vmobjlock);
1178 1.63.2.11 nathanw }
1179 1.63.2.11 nathanw }
1180 1.63.2.11 nathanw if (!write) {
1181 1.63.2.13 nathanw return (0);
1182 1.63.2.11 nathanw }
1183 1.63.2.11 nathanw
1184 1.63.2.11 nathanw /*
1185 1.63.2.11 nathanw * this is a write fault, update the commit info.
1186 1.63.2.11 nathanw */
1187 1.63.2.11 nathanw
1188 1.63.2.9 nathanw origoffset = ap->a_offset;
1189 1.63.2.9 nathanw len = npages << PAGE_SHIFT;
1190 1.54 chs
1191 1.63.2.11 nathanw if (v3) {
1192 1.63.2.13 nathanw error = lockmgr(&np->n_commitlock,
1193 1.63.2.13 nathanw LK_EXCLUSIVE | (locked ? LK_NOWAIT : 0), NULL);
1194 1.63.2.13 nathanw if (error) {
1195 1.63.2.13 nathanw KASSERT(locked != 0);
1196 1.63.2.13 nathanw
1197 1.63.2.13 nathanw /*
1198 1.63.2.13 nathanw * Since PGO_LOCKED is set, we need to unbusy
1199 1.63.2.13 nathanw * all pages fetched by genfs_getpages() above,
1200 1.63.2.13 nathanw * tell the caller that there are no pages
1201 1.63.2.13 nathanw * available and put back original pgs array.
1202 1.63.2.13 nathanw */
1203 1.63.2.13 nathanw
1204 1.63.2.13 nathanw uvm_lock_pageq();
1205 1.63.2.13 nathanw uvm_page_unbusy(pgs, npages);
1206 1.63.2.13 nathanw uvm_unlock_pageq();
1207 1.63.2.13 nathanw *ap->a_count = 0;
1208 1.63.2.13 nathanw memcpy(pgs, opgs,
1209 1.63.2.13 nathanw npages * sizeof(struct vm_pages *));
1210 1.63.2.13 nathanw return (error);
1211 1.63.2.13 nathanw }
1212 1.63.2.11 nathanw nfs_del_committed_range(vp, origoffset, len);
1213 1.63.2.11 nathanw nfs_del_tobecommitted_range(vp, origoffset, len);
1214 1.63.2.11 nathanw }
1215 1.63.2.13 nathanw np->n_flag |= NMODIFIED;
1216 1.63.2.8 nathanw if (!locked) {
1217 1.63.2.8 nathanw simple_lock(&uobj->vmobjlock);
1218 1.63.2.8 nathanw }
1219 1.54 chs for (i = 0; i < npages; i++) {
1220 1.63.2.5 nathanw pg = pgs[i];
1221 1.63.2.5 nathanw if (pg == NULL || pg == PGO_DONTCARE) {
1222 1.54 chs continue;
1223 1.54 chs }
1224 1.63.2.9 nathanw pg->flags &= ~(PG_NEEDCOMMIT | PG_RDONLY);
1225 1.54 chs }
1226 1.63.2.8 nathanw if (!locked) {
1227 1.63.2.8 nathanw simple_unlock(&uobj->vmobjlock);
1228 1.63.2.8 nathanw }
1229 1.63.2.11 nathanw if (v3) {
1230 1.63.2.11 nathanw lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
1231 1.63.2.11 nathanw }
1232 1.63.2.13 nathanw return (0);
1233 1.1 cgd }
1234