nfs_bio.c revision 1.45.8.8 1 1.45.8.8 bouyer /* $NetBSD: nfs_bio.c,v 1.45.8.8 2001/04/21 17:47:01 bouyer 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.45.8.1 bouyer #include "opt_nfs.h"
42 1.45.8.2 bouyer #include "opt_ddb.h"
43 1.45.8.1 bouyer
44 1.8 mycroft #include <sys/param.h>
45 1.8 mycroft #include <sys/systm.h>
46 1.12 mycroft #include <sys/resourcevar.h>
47 1.24 fvdl #include <sys/signalvar.h>
48 1.8 mycroft #include <sys/proc.h>
49 1.8 mycroft #include <sys/buf.h>
50 1.8 mycroft #include <sys/vnode.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.45.8.2 bouyer #include <sys/malloc.h>
56 1.12 mycroft
57 1.41 mrg #include <uvm/uvm_extern.h>
58 1.45.8.2 bouyer #include <uvm/uvm.h>
59 1.41 mrg
60 1.12 mycroft #include <nfs/rpcv2.h>
61 1.24 fvdl #include <nfs/nfsproto.h>
62 1.8 mycroft #include <nfs/nfs.h>
63 1.8 mycroft #include <nfs/nfsmount.h>
64 1.12 mycroft #include <nfs/nqnfs.h>
65 1.24 fvdl #include <nfs/nfsnode.h>
66 1.23 christos #include <nfs/nfs_var.h>
67 1.1 cgd
68 1.12 mycroft extern int nfs_numasync;
69 1.24 fvdl extern struct nfsstats nfsstats;
70 1.1 cgd
71 1.1 cgd /*
72 1.1 cgd * Vnode op for read using bio
73 1.1 cgd * Any similarity to readip() is purely coincidental
74 1.1 cgd */
75 1.23 christos int
76 1.34 fvdl nfs_bioread(vp, uio, ioflag, cred, cflag)
77 1.45.8.1 bouyer struct vnode *vp;
78 1.45.8.1 bouyer struct uio *uio;
79 1.34 fvdl int ioflag, cflag;
80 1.1 cgd struct ucred *cred;
81 1.1 cgd {
82 1.45.8.1 bouyer struct nfsnode *np = VTONFS(vp);
83 1.45.8.2 bouyer int biosize;
84 1.23 christos struct buf *bp = NULL, *rabp;
85 1.1 cgd struct vattr vattr;
86 1.12 mycroft struct proc *p;
87 1.24 fvdl struct nfsmount *nmp = VFSTONFS(vp->v_mount);
88 1.35 fvdl struct nfsdircache *ndp = NULL, *nndp = NULL;
89 1.34 fvdl caddr_t baddr, ep, edp;
90 1.45.8.2 bouyer int got_buf = 0, error = 0, n = 0, on = 0, en, enn;
91 1.34 fvdl int enough = 0;
92 1.34 fvdl struct dirent *dp, *pdp;
93 1.45.8.2 bouyer off_t curoff = 0;
94 1.1 cgd
95 1.1 cgd #ifdef DIAGNOSTIC
96 1.1 cgd if (uio->uio_rw != UIO_READ)
97 1.1 cgd panic("nfs_read mode");
98 1.1 cgd #endif
99 1.1 cgd if (uio->uio_resid == 0)
100 1.1 cgd return (0);
101 1.34 fvdl if (vp->v_type != VDIR && uio->uio_offset < 0)
102 1.1 cgd return (EINVAL);
103 1.24 fvdl p = uio->uio_procp;
104 1.45.8.1 bouyer #ifndef NFS_V2_ONLY
105 1.34 fvdl if ((nmp->nm_flag & NFSMNT_NFSV3) &&
106 1.34 fvdl !(nmp->nm_iflag & NFSMNT_GOTFSINFO))
107 1.24 fvdl (void)nfs_fsinfo(nmp, vp, cred, p);
108 1.45.8.1 bouyer #endif
109 1.34 fvdl if (vp->v_type != VDIR &&
110 1.34 fvdl (uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
111 1.33 fvdl return (EFBIG);
112 1.12 mycroft biosize = nmp->nm_rsize;
113 1.45.8.2 bouyer
114 1.1 cgd /*
115 1.12 mycroft * For nfs, cache consistency can only be maintained approximately.
116 1.12 mycroft * Although RFC1094 does not specify the criteria, the following is
117 1.12 mycroft * believed to be compatible with the reference port.
118 1.12 mycroft * For nqnfs, full cache consistency is maintained within the loop.
119 1.12 mycroft * For nfs:
120 1.1 cgd * If the file's modify time on the server has changed since the
121 1.1 cgd * last read rpc or you have written to the file,
122 1.1 cgd * you may have lost data cache consistency with the
123 1.1 cgd * server, so flush all of the file's data out of the cache.
124 1.1 cgd * Then force a getattr rpc to ensure that you have up to date
125 1.1 cgd * attributes.
126 1.1 cgd * NB: This implies that cache data can be read when up to
127 1.1 cgd * NFS_ATTRTIMEO seconds out of date. If you find that you need current
128 1.1 cgd * attributes this could be forced by setting n_attrstamp to 0 before
129 1.12 mycroft * the VOP_GETATTR() call.
130 1.1 cgd */
131 1.45.8.2 bouyer
132 1.12 mycroft if ((nmp->nm_flag & NFSMNT_NQNFS) == 0 && vp->v_type != VLNK) {
133 1.1 cgd if (np->n_flag & NMODIFIED) {
134 1.24 fvdl if (vp->v_type != VREG) {
135 1.24 fvdl if (vp->v_type != VDIR)
136 1.24 fvdl panic("nfs: bioread, not dir");
137 1.35 fvdl nfs_invaldircache(vp, 0);
138 1.35 fvdl np->n_direofoffset = 0;
139 1.23 christos error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
140 1.23 christos if (error)
141 1.12 mycroft return (error);
142 1.12 mycroft }
143 1.1 cgd np->n_attrstamp = 0;
144 1.23 christos error = VOP_GETATTR(vp, &vattr, cred, p);
145 1.23 christos if (error)
146 1.1 cgd return (error);
147 1.22 jtc np->n_mtime = vattr.va_mtime.tv_sec;
148 1.1 cgd } else {
149 1.24 fvdl error = VOP_GETATTR(vp, &vattr, cred, p);
150 1.24 fvdl if (error)
151 1.1 cgd return (error);
152 1.22 jtc if (np->n_mtime != vattr.va_mtime.tv_sec) {
153 1.35 fvdl if (vp->v_type == VDIR) {
154 1.35 fvdl nfs_invaldircache(vp, 0);
155 1.35 fvdl np->n_direofoffset = 0;
156 1.35 fvdl }
157 1.23 christos error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
158 1.23 christos if (error)
159 1.12 mycroft return (error);
160 1.22 jtc np->n_mtime = vattr.va_mtime.tv_sec;
161 1.1 cgd }
162 1.1 cgd }
163 1.1 cgd }
164 1.12 mycroft
165 1.45.8.2 bouyer /*
166 1.45.8.2 bouyer * update the cached read creds for this node.
167 1.45.8.2 bouyer */
168 1.45.8.2 bouyer
169 1.45.8.2 bouyer if (np->n_rcred) {
170 1.45.8.2 bouyer crfree(np->n_rcred);
171 1.45.8.2 bouyer }
172 1.45.8.2 bouyer np->n_rcred = cred;
173 1.45.8.2 bouyer crhold(cred);
174 1.45.8.2 bouyer
175 1.45.8.2 bouyer do {
176 1.45.8.1 bouyer #ifndef NFS_V2_ONLY
177 1.12 mycroft /*
178 1.12 mycroft * Get a valid lease. If cached data is stale, flush it.
179 1.12 mycroft */
180 1.12 mycroft if (nmp->nm_flag & NFSMNT_NQNFS) {
181 1.24 fvdl if (NQNFS_CKINVALID(vp, np, ND_READ)) {
182 1.12 mycroft do {
183 1.24 fvdl error = nqnfs_getlease(vp, ND_READ, cred, p);
184 1.12 mycroft } while (error == NQNFS_EXPIRED);
185 1.12 mycroft if (error)
186 1.12 mycroft return (error);
187 1.12 mycroft if (np->n_lrev != np->n_brev ||
188 1.12 mycroft (np->n_flag & NQNFSNONCACHE) ||
189 1.12 mycroft ((np->n_flag & NMODIFIED) && vp->v_type == VDIR)) {
190 1.35 fvdl if (vp->v_type == VDIR) {
191 1.35 fvdl nfs_invaldircache(vp, 0);
192 1.35 fvdl np->n_direofoffset = 0;
193 1.35 fvdl }
194 1.23 christos error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
195 1.23 christos if (error)
196 1.12 mycroft return (error);
197 1.12 mycroft np->n_brev = np->n_lrev;
198 1.12 mycroft }
199 1.12 mycroft } else if (vp->v_type == VDIR && (np->n_flag & NMODIFIED)) {
200 1.35 fvdl nfs_invaldircache(vp, 0);
201 1.23 christos error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
202 1.35 fvdl np->n_direofoffset = 0;
203 1.23 christos if (error)
204 1.12 mycroft return (error);
205 1.12 mycroft }
206 1.12 mycroft }
207 1.45.8.1 bouyer #endif
208 1.26 fvdl /*
209 1.26 fvdl * Don't cache symlinks.
210 1.26 fvdl */
211 1.26 fvdl if (np->n_flag & NQNFSNONCACHE
212 1.26 fvdl || ((vp->v_flag & VROOT) && vp->v_type == VLNK)) {
213 1.12 mycroft switch (vp->v_type) {
214 1.12 mycroft case VREG:
215 1.45.8.2 bouyer return (nfs_readrpc(vp, uio));
216 1.12 mycroft case VLNK:
217 1.24 fvdl return (nfs_readlinkrpc(vp, uio, cred));
218 1.12 mycroft case VDIR:
219 1.23 christos break;
220 1.24 fvdl default:
221 1.29 christos printf(" NQNFSNONCACHE: type %x unexpected\n",
222 1.28 christos vp->v_type);
223 1.12 mycroft };
224 1.12 mycroft }
225 1.12 mycroft baddr = (caddr_t)0;
226 1.1 cgd switch (vp->v_type) {
227 1.1 cgd case VREG:
228 1.1 cgd nfsstats.biocache_reads++;
229 1.12 mycroft
230 1.45.8.2 bouyer error = 0;
231 1.45.8.8 bouyer if (uio->uio_offset >= np->n_size) {
232 1.45.8.8 bouyer break;
233 1.45.8.8 bouyer }
234 1.45.8.2 bouyer while (uio->uio_resid > 0) {
235 1.45.8.2 bouyer void *win;
236 1.45.8.6 bouyer vsize_t bytelen = MIN(np->n_size - uio->uio_offset,
237 1.45.8.2 bouyer uio->uio_resid);
238 1.45.8.2 bouyer
239 1.45.8.2 bouyer if (bytelen == 0)
240 1.45.8.2 bouyer break;
241 1.45.8.2 bouyer win = ubc_alloc(&vp->v_uvm.u_obj, uio->uio_offset,
242 1.45.8.2 bouyer &bytelen, UBC_READ);
243 1.45.8.2 bouyer error = uiomove(win, bytelen, uio);
244 1.45.8.2 bouyer ubc_release(win, 0);
245 1.45.8.2 bouyer if (error) {
246 1.45.8.2 bouyer break;
247 1.12 mycroft }
248 1.12 mycroft }
249 1.45.8.2 bouyer n = 0;
250 1.1 cgd break;
251 1.45.8.2 bouyer
252 1.1 cgd case VLNK:
253 1.1 cgd nfsstats.biocache_readlinks++;
254 1.12 mycroft bp = nfs_getcacheblk(vp, (daddr_t)0, NFS_MAXPATHLEN, p);
255 1.12 mycroft if (!bp)
256 1.12 mycroft return (EINTR);
257 1.12 mycroft if ((bp->b_flags & B_DONE) == 0) {
258 1.12 mycroft bp->b_flags |= B_READ;
259 1.45.8.2 bouyer error = nfs_doio(bp, p);
260 1.24 fvdl if (error) {
261 1.12 mycroft brelse(bp);
262 1.12 mycroft return (error);
263 1.12 mycroft }
264 1.12 mycroft }
265 1.45.8.7 bouyer n = MIN(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid);
266 1.12 mycroft got_buf = 1;
267 1.1 cgd on = 0;
268 1.1 cgd break;
269 1.1 cgd case VDIR:
270 1.34 fvdl diragain:
271 1.34 fvdl nfsstats.biocache_readdirs++;
272 1.35 fvdl ndp = nfs_searchdircache(vp, uio->uio_offset,
273 1.35 fvdl (nmp->nm_flag & NFSMNT_XLATECOOKIE), 0);
274 1.35 fvdl if (!ndp) {
275 1.35 fvdl /*
276 1.35 fvdl * We've been handed a cookie that is not
277 1.35 fvdl * in the cache. If we're not translating
278 1.35 fvdl * 32 <-> 64, it may be a value that was
279 1.35 fvdl * flushed out of the cache because it grew
280 1.35 fvdl * too big. Let the server judge if it's
281 1.35 fvdl * valid or not. In the translation case,
282 1.35 fvdl * we have no way of validating this value,
283 1.35 fvdl * so punt.
284 1.35 fvdl */
285 1.35 fvdl if (nmp->nm_flag & NFSMNT_XLATECOOKIE)
286 1.35 fvdl return (EINVAL);
287 1.35 fvdl ndp = nfs_enterdircache(vp, uio->uio_offset,
288 1.35 fvdl uio->uio_offset, 0, 0);
289 1.35 fvdl }
290 1.35 fvdl
291 1.34 fvdl if (uio->uio_offset != 0 &&
292 1.35 fvdl ndp->dc_cookie == np->n_direofoffset) {
293 1.35 fvdl nfsstats.direofcache_hits++;
294 1.18 mycroft return (0);
295 1.35 fvdl }
296 1.35 fvdl
297 1.34 fvdl bp = nfs_getcacheblk(vp, ndp->dc_blkno, NFS_DIRBLKSIZ, p);
298 1.12 mycroft if (!bp)
299 1.24 fvdl return (EINTR);
300 1.12 mycroft if ((bp->b_flags & B_DONE) == 0) {
301 1.24 fvdl bp->b_flags |= B_READ;
302 1.35 fvdl bp->b_dcookie = ndp->dc_blkcookie;
303 1.45.8.2 bouyer error = nfs_doio(bp, p);
304 1.24 fvdl if (error) {
305 1.34 fvdl /*
306 1.34 fvdl * Yuck! The directory has been modified on the
307 1.34 fvdl * server. Punt and let the userland code
308 1.34 fvdl * deal with it.
309 1.34 fvdl */
310 1.24 fvdl brelse(bp);
311 1.34 fvdl if (error == NFSERR_BAD_COOKIE) {
312 1.35 fvdl nfs_invaldircache(vp, 0);
313 1.34 fvdl nfs_vinvalbuf(vp, 0, cred, p, 1);
314 1.34 fvdl error = EINVAL;
315 1.12 mycroft }
316 1.34 fvdl return (error);
317 1.38 fvdl }
318 1.40 fvdl }
319 1.40 fvdl
320 1.40 fvdl /*
321 1.40 fvdl * Just return if we hit EOF right away with this
322 1.40 fvdl * block. Always check here, because direofoffset
323 1.40 fvdl * may have been set by an nfsiod since the last
324 1.40 fvdl * check.
325 1.40 fvdl */
326 1.40 fvdl if (np->n_direofoffset != 0 &&
327 1.39 fvdl ndp->dc_blkcookie == np->n_direofoffset) {
328 1.40 fvdl brelse(bp);
329 1.40 fvdl return (0);
330 1.12 mycroft }
331 1.12 mycroft
332 1.12 mycroft /*
333 1.34 fvdl * Find the entry we were looking for in the block.
334 1.34 fvdl */
335 1.34 fvdl
336 1.34 fvdl en = ndp->dc_entry;
337 1.34 fvdl
338 1.34 fvdl pdp = dp = (struct dirent *)bp->b_data;
339 1.45.8.8 bouyer edp = bp->b_data + bp->b_bcount - bp->b_resid;
340 1.34 fvdl enn = 0;
341 1.34 fvdl while (enn < en && (caddr_t)dp < edp) {
342 1.34 fvdl pdp = dp;
343 1.34 fvdl dp = (struct dirent *)((caddr_t)dp + dp->d_reclen);
344 1.34 fvdl enn++;
345 1.34 fvdl }
346 1.34 fvdl
347 1.34 fvdl /*
348 1.34 fvdl * If the entry number was bigger than the number of
349 1.34 fvdl * entries in the block, or the cookie of the previous
350 1.34 fvdl * entry doesn't match, the directory cache is
351 1.34 fvdl * stale. Flush it and try again (i.e. go to
352 1.34 fvdl * the server).
353 1.34 fvdl */
354 1.34 fvdl if ((caddr_t)dp >= edp || (caddr_t)dp + dp->d_reclen > edp ||
355 1.35 fvdl (en > 0 && NFS_GETCOOKIE(pdp) != ndp->dc_cookie)) {
356 1.34 fvdl #ifdef DEBUG
357 1.37 thorpej printf("invalid cache: %p %p %p off %lx %lx\n",
358 1.37 thorpej pdp, dp, edp,
359 1.34 fvdl (unsigned long)uio->uio_offset,
360 1.34 fvdl (unsigned long)NFS_GETCOOKIE(pdp));
361 1.34 fvdl #endif
362 1.34 fvdl brelse(bp);
363 1.35 fvdl nfs_invaldircache(vp, 0);
364 1.34 fvdl nfs_vinvalbuf(vp, 0, cred, p, 0);
365 1.34 fvdl goto diragain;
366 1.34 fvdl }
367 1.34 fvdl
368 1.34 fvdl on = (caddr_t)dp - bp->b_data;
369 1.34 fvdl
370 1.34 fvdl /*
371 1.34 fvdl * Cache all entries that may be exported to the
372 1.34 fvdl * user, as they may be thrown back at us. The
373 1.34 fvdl * NFSBIO_CACHECOOKIES flag indicates that all
374 1.34 fvdl * entries are being 'exported', so cache them all.
375 1.34 fvdl */
376 1.34 fvdl
377 1.34 fvdl if (en == 0 && pdp == dp) {
378 1.34 fvdl dp = (struct dirent *)
379 1.34 fvdl ((caddr_t)dp + dp->d_reclen);
380 1.34 fvdl enn++;
381 1.34 fvdl }
382 1.34 fvdl
383 1.45.8.8 bouyer if (uio->uio_resid < (bp->b_bcount - bp->b_resid - on)) {
384 1.34 fvdl n = uio->uio_resid;
385 1.34 fvdl enough = 1;
386 1.34 fvdl } else
387 1.45.8.8 bouyer n = bp->b_bcount - bp->b_resid - on;
388 1.34 fvdl
389 1.34 fvdl ep = bp->b_data + on + n;
390 1.34 fvdl
391 1.34 fvdl /*
392 1.34 fvdl * Find last complete entry to copy, caching entries
393 1.34 fvdl * (if requested) as we go.
394 1.34 fvdl */
395 1.34 fvdl
396 1.34 fvdl while ((caddr_t)dp < ep && (caddr_t)dp + dp->d_reclen <= ep) {
397 1.35 fvdl if (cflag & NFSBIO_CACHECOOKIES) {
398 1.35 fvdl nndp = nfs_enterdircache(vp, NFS_GETCOOKIE(pdp),
399 1.35 fvdl ndp->dc_blkcookie, enn, bp->b_lblkno);
400 1.35 fvdl if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
401 1.35 fvdl NFS_STASHCOOKIE32(pdp,
402 1.35 fvdl nndp->dc_cookie32);
403 1.35 fvdl }
404 1.35 fvdl }
405 1.34 fvdl pdp = dp;
406 1.34 fvdl dp = (struct dirent *)((caddr_t)dp + dp->d_reclen);
407 1.34 fvdl enn++;
408 1.34 fvdl }
409 1.34 fvdl
410 1.34 fvdl /*
411 1.34 fvdl * If the last requested entry was not the last in the
412 1.34 fvdl * buffer (happens if NFS_DIRFRAGSIZ < NFS_DIRBLKSIZ),
413 1.34 fvdl * cache the cookie of the last requested one, and
414 1.34 fvdl * set of the offset to it.
415 1.34 fvdl */
416 1.34 fvdl
417 1.45.8.8 bouyer if ((on + n) < bp->b_bcount - bp->b_resid) {
418 1.34 fvdl curoff = NFS_GETCOOKIE(pdp);
419 1.35 fvdl nndp = nfs_enterdircache(vp, curoff, ndp->dc_blkcookie,
420 1.35 fvdl enn, bp->b_lblkno);
421 1.35 fvdl if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
422 1.35 fvdl NFS_STASHCOOKIE32(pdp, nndp->dc_cookie32);
423 1.35 fvdl curoff = nndp->dc_cookie32;
424 1.35 fvdl }
425 1.34 fvdl } else
426 1.34 fvdl curoff = bp->b_dcookie;
427 1.34 fvdl
428 1.35 fvdl /*
429 1.35 fvdl * Always cache the entry for the next block,
430 1.35 fvdl * so that readaheads can use it.
431 1.35 fvdl */
432 1.35 fvdl nndp = nfs_enterdircache(vp, bp->b_dcookie, bp->b_dcookie, 0,0);
433 1.35 fvdl if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
434 1.35 fvdl if (curoff == bp->b_dcookie) {
435 1.35 fvdl NFS_STASHCOOKIE32(pdp, nndp->dc_cookie32);
436 1.35 fvdl curoff = nndp->dc_cookie32;
437 1.35 fvdl }
438 1.35 fvdl }
439 1.35 fvdl
440 1.34 fvdl n = ((caddr_t)pdp + pdp->d_reclen) - (bp->b_data + on);
441 1.34 fvdl
442 1.34 fvdl /*
443 1.12 mycroft * If not eof and read aheads are enabled, start one.
444 1.12 mycroft * (You need the current block first, so that you have the
445 1.24 fvdl * directory offset cookie of the next block.)
446 1.12 mycroft */
447 1.12 mycroft if (nfs_numasync > 0 && nmp->nm_readahead > 0 &&
448 1.34 fvdl np->n_direofoffset == 0 && !(np->n_flag & NQNFSNONCACHE)) {
449 1.35 fvdl rabp = nfs_getcacheblk(vp, nndp->dc_blkno,
450 1.34 fvdl NFS_DIRBLKSIZ, p);
451 1.12 mycroft if (rabp) {
452 1.12 mycroft if ((rabp->b_flags & (B_DONE | B_DELWRI)) == 0) {
453 1.35 fvdl rabp->b_dcookie = nndp->dc_cookie;
454 1.12 mycroft rabp->b_flags |= (B_READ | B_ASYNC);
455 1.45.8.2 bouyer if (nfs_asyncio(rabp)) {
456 1.12 mycroft rabp->b_flags |= B_INVAL;
457 1.12 mycroft brelse(rabp);
458 1.12 mycroft }
459 1.19 mycroft } else
460 1.19 mycroft brelse(rabp);
461 1.12 mycroft }
462 1.12 mycroft }
463 1.12 mycroft got_buf = 1;
464 1.1 cgd break;
465 1.24 fvdl default:
466 1.29 christos printf(" nfsbioread: type %x unexpected\n",vp->v_type);
467 1.23 christos break;
468 1.45.8.2 bouyer }
469 1.12 mycroft
470 1.12 mycroft if (n > 0) {
471 1.12 mycroft if (!baddr)
472 1.12 mycroft baddr = bp->b_data;
473 1.12 mycroft error = uiomove(baddr + on, (int)n, uio);
474 1.1 cgd }
475 1.1 cgd switch (vp->v_type) {
476 1.24 fvdl case VREG:
477 1.24 fvdl break;
478 1.1 cgd case VLNK:
479 1.1 cgd n = 0;
480 1.1 cgd break;
481 1.1 cgd case VDIR:
482 1.24 fvdl if (np->n_flag & NQNFSNONCACHE)
483 1.24 fvdl bp->b_flags |= B_INVAL;
484 1.34 fvdl uio->uio_offset = curoff;
485 1.34 fvdl if (enough)
486 1.34 fvdl n = 0;
487 1.1 cgd break;
488 1.24 fvdl default:
489 1.29 christos printf(" nfsbioread: type %x unexpected\n",vp->v_type);
490 1.24 fvdl }
491 1.12 mycroft if (got_buf)
492 1.12 mycroft brelse(bp);
493 1.12 mycroft } while (error == 0 && uio->uio_resid > 0 && n > 0);
494 1.1 cgd return (error);
495 1.1 cgd }
496 1.1 cgd
497 1.1 cgd /*
498 1.1 cgd * Vnode op for write using bio
499 1.1 cgd */
500 1.23 christos int
501 1.23 christos nfs_write(v)
502 1.23 christos void *v;
503 1.23 christos {
504 1.12 mycroft struct vop_write_args /* {
505 1.24 fvdl struct vnode *a_vp;
506 1.12 mycroft struct uio *a_uio;
507 1.12 mycroft int a_ioflag;
508 1.12 mycroft struct ucred *a_cred;
509 1.23 christos } */ *ap = v;
510 1.45.8.1 bouyer struct uio *uio = ap->a_uio;
511 1.1 cgd struct proc *p = uio->uio_procp;
512 1.45.8.1 bouyer struct vnode *vp = ap->a_vp;
513 1.12 mycroft struct nfsnode *np = VTONFS(vp);
514 1.45.8.1 bouyer struct ucred *cred = ap->a_cred;
515 1.12 mycroft int ioflag = ap->a_ioflag;
516 1.1 cgd struct vattr vattr;
517 1.24 fvdl struct nfsmount *nmp = VFSTONFS(vp->v_mount);
518 1.45.8.2 bouyer int error = 0, iomode, must_commit;
519 1.45.8.2 bouyer int rv;
520 1.1 cgd
521 1.1 cgd #ifdef DIAGNOSTIC
522 1.1 cgd if (uio->uio_rw != UIO_WRITE)
523 1.1 cgd panic("nfs_write mode");
524 1.1 cgd if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
525 1.1 cgd panic("nfs_write proc");
526 1.1 cgd #endif
527 1.1 cgd if (vp->v_type != VREG)
528 1.1 cgd return (EIO);
529 1.12 mycroft if (np->n_flag & NWRITEERR) {
530 1.12 mycroft np->n_flag &= ~NWRITEERR;
531 1.12 mycroft return (np->n_error);
532 1.12 mycroft }
533 1.45.8.1 bouyer #ifndef NFS_V2_ONLY
534 1.34 fvdl if ((nmp->nm_flag & NFSMNT_NFSV3) &&
535 1.34 fvdl !(nmp->nm_iflag & NFSMNT_GOTFSINFO))
536 1.24 fvdl (void)nfs_fsinfo(nmp, vp, cred, p);
537 1.45.8.1 bouyer #endif
538 1.1 cgd if (ioflag & (IO_APPEND | IO_SYNC)) {
539 1.1 cgd if (np->n_flag & NMODIFIED) {
540 1.12 mycroft np->n_attrstamp = 0;
541 1.23 christos error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
542 1.23 christos if (error)
543 1.12 mycroft return (error);
544 1.1 cgd }
545 1.1 cgd if (ioflag & IO_APPEND) {
546 1.1 cgd np->n_attrstamp = 0;
547 1.23 christos error = VOP_GETATTR(vp, &vattr, cred, p);
548 1.23 christos if (error)
549 1.1 cgd return (error);
550 1.1 cgd uio->uio_offset = np->n_size;
551 1.1 cgd }
552 1.1 cgd }
553 1.1 cgd if (uio->uio_offset < 0)
554 1.1 cgd return (EINVAL);
555 1.33 fvdl if ((uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
556 1.33 fvdl return (EFBIG);
557 1.1 cgd if (uio->uio_resid == 0)
558 1.1 cgd return (0);
559 1.1 cgd /*
560 1.1 cgd * Maybe this should be above the vnode op call, but so long as
561 1.1 cgd * file servers have no limits, i don't think it matters
562 1.1 cgd */
563 1.12 mycroft if (p && uio->uio_offset + uio->uio_resid >
564 1.1 cgd p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
565 1.1 cgd psignal(p, SIGXFSZ);
566 1.1 cgd return (EFBIG);
567 1.1 cgd }
568 1.45.8.2 bouyer
569 1.1 cgd /*
570 1.45.8.2 bouyer * update the cached write creds for this node.
571 1.1 cgd */
572 1.16 cgd
573 1.45.8.2 bouyer if (np->n_wcred) {
574 1.45.8.2 bouyer crfree(np->n_wcred);
575 1.45.8.2 bouyer }
576 1.45.8.2 bouyer np->n_wcred = cred;
577 1.45.8.2 bouyer crhold(cred);
578 1.45.8.2 bouyer
579 1.45.8.2 bouyer if ((np->n_flag & NQNFSNONCACHE) && uio->uio_iovcnt == 1) {
580 1.45.8.2 bouyer iomode = NFSV3WRITE_FILESYNC;
581 1.45.8.2 bouyer error = nfs_writerpc(vp, uio, &iomode, &must_commit);
582 1.45.8.2 bouyer if (must_commit)
583 1.45.8.2 bouyer nfs_clearcommit(vp->v_mount);
584 1.45.8.2 bouyer return (error);
585 1.45.8.2 bouyer }
586 1.45.8.2 bouyer
587 1.45.8.2 bouyer do {
588 1.45.8.2 bouyer void *win;
589 1.45.8.2 bouyer voff_t oldoff = uio->uio_offset;
590 1.45.8.2 bouyer vsize_t bytelen = uio->uio_resid;
591 1.12 mycroft
592 1.45.8.1 bouyer #ifndef NFS_V2_ONLY
593 1.12 mycroft /*
594 1.12 mycroft * Check for a valid write lease.
595 1.12 mycroft */
596 1.12 mycroft if ((nmp->nm_flag & NFSMNT_NQNFS) &&
597 1.24 fvdl NQNFS_CKINVALID(vp, np, ND_WRITE)) {
598 1.12 mycroft do {
599 1.24 fvdl error = nqnfs_getlease(vp, ND_WRITE, cred, p);
600 1.12 mycroft } while (error == NQNFS_EXPIRED);
601 1.12 mycroft if (error)
602 1.12 mycroft return (error);
603 1.12 mycroft if (np->n_lrev != np->n_brev ||
604 1.12 mycroft (np->n_flag & NQNFSNONCACHE)) {
605 1.23 christos error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
606 1.23 christos if (error)
607 1.12 mycroft return (error);
608 1.12 mycroft np->n_brev = np->n_lrev;
609 1.12 mycroft }
610 1.12 mycroft }
611 1.45.8.1 bouyer #endif
612 1.1 cgd nfsstats.biocache_writes++;
613 1.45.8.2 bouyer
614 1.12 mycroft np->n_flag |= NMODIFIED;
615 1.45.8.2 bouyer if (np->n_size < uio->uio_offset + bytelen) {
616 1.45.8.2 bouyer np->n_size = uio->uio_offset + bytelen;
617 1.41 mrg uvm_vnp_setsize(vp, np->n_size);
618 1.12 mycroft }
619 1.45.8.2 bouyer win = ubc_alloc(&vp->v_uvm.u_obj, uio->uio_offset, &bytelen,
620 1.45.8.2 bouyer UBC_WRITE);
621 1.45.8.2 bouyer error = uiomove(win, bytelen, uio);
622 1.45.8.2 bouyer ubc_release(win, 0);
623 1.45.8.2 bouyer rv = 1;
624 1.12 mycroft if ((np->n_flag & NQNFSNONCACHE) || (ioflag & IO_SYNC)) {
625 1.45.8.2 bouyer simple_lock(&vp->v_uvm.u_obj.vmobjlock);
626 1.45.8.2 bouyer rv = vp->v_uvm.u_obj.pgops->pgo_flush(
627 1.45.8.2 bouyer &vp->v_uvm.u_obj,
628 1.45.8.2 bouyer oldoff & ~(nmp->nm_wsize - 1),
629 1.45.8.2 bouyer uio->uio_offset & ~(nmp->nm_wsize - 1),
630 1.45.8.2 bouyer PGO_CLEANIT|PGO_SYNCIO);
631 1.45.8.2 bouyer simple_unlock(&vp->v_uvm.u_obj.vmobjlock);
632 1.45.8.2 bouyer } else if ((oldoff & ~(nmp->nm_wsize - 1)) !=
633 1.45.8.2 bouyer (uio->uio_offset & ~(nmp->nm_wsize - 1))) {
634 1.45.8.2 bouyer simple_lock(&vp->v_uvm.u_obj.vmobjlock);
635 1.45.8.2 bouyer rv = vp->v_uvm.u_obj.pgops->pgo_flush(
636 1.45.8.2 bouyer &vp->v_uvm.u_obj,
637 1.45.8.2 bouyer oldoff & ~(nmp->nm_wsize - 1),
638 1.45.8.2 bouyer uio->uio_offset & ~(nmp->nm_wsize - 1),
639 1.45.8.2 bouyer PGO_CLEANIT|PGO_WEAK);
640 1.45.8.2 bouyer simple_unlock(&vp->v_uvm.u_obj.vmobjlock);
641 1.24 fvdl }
642 1.45.8.2 bouyer if (!rv) {
643 1.45.8.2 bouyer error = EIO;
644 1.45.8.4 bouyer }
645 1.45.8.4 bouyer if (error) {
646 1.45.8.2 bouyer break;
647 1.45.8.2 bouyer }
648 1.45.8.2 bouyer } while (uio->uio_resid > 0);
649 1.45.8.2 bouyer return error;
650 1.12 mycroft }
651 1.12 mycroft
652 1.12 mycroft /*
653 1.12 mycroft * Get an nfs cache block.
654 1.12 mycroft * Allocate a new one if the block isn't currently in the cache
655 1.12 mycroft * and return the block marked busy. If the calling process is
656 1.12 mycroft * interrupted by a signal for an interruptible mount point, return
657 1.12 mycroft * NULL.
658 1.12 mycroft */
659 1.12 mycroft struct buf *
660 1.12 mycroft nfs_getcacheblk(vp, bn, size, p)
661 1.12 mycroft struct vnode *vp;
662 1.12 mycroft daddr_t bn;
663 1.12 mycroft int size;
664 1.12 mycroft struct proc *p;
665 1.12 mycroft {
666 1.45.8.1 bouyer struct buf *bp;
667 1.12 mycroft struct nfsmount *nmp = VFSTONFS(vp->v_mount);
668 1.12 mycroft
669 1.12 mycroft if (nmp->nm_flag & NFSMNT_INT) {
670 1.12 mycroft bp = getblk(vp, bn, size, PCATCH, 0);
671 1.45.8.2 bouyer while (bp == NULL) {
672 1.45.8.2 bouyer if (nfs_sigintr(nmp, NULL, p))
673 1.45.8.2 bouyer return (NULL);
674 1.12 mycroft bp = getblk(vp, bn, size, 0, 2 * hz);
675 1.12 mycroft }
676 1.12 mycroft } else
677 1.12 mycroft bp = getblk(vp, bn, size, 0, 0);
678 1.12 mycroft return (bp);
679 1.12 mycroft }
680 1.12 mycroft
681 1.12 mycroft /*
682 1.12 mycroft * Flush and invalidate all dirty buffers. If another process is already
683 1.12 mycroft * doing the flush, just wait for completion.
684 1.12 mycroft */
685 1.23 christos int
686 1.12 mycroft nfs_vinvalbuf(vp, flags, cred, p, intrflg)
687 1.12 mycroft struct vnode *vp;
688 1.12 mycroft int flags;
689 1.12 mycroft struct ucred *cred;
690 1.12 mycroft struct proc *p;
691 1.12 mycroft int intrflg;
692 1.12 mycroft {
693 1.45.8.1 bouyer struct nfsnode *np = VTONFS(vp);
694 1.12 mycroft struct nfsmount *nmp = VFSTONFS(vp->v_mount);
695 1.12 mycroft int error = 0, slpflag, slptimeo;
696 1.12 mycroft
697 1.12 mycroft if ((nmp->nm_flag & NFSMNT_INT) == 0)
698 1.12 mycroft intrflg = 0;
699 1.12 mycroft if (intrflg) {
700 1.12 mycroft slpflag = PCATCH;
701 1.12 mycroft slptimeo = 2 * hz;
702 1.12 mycroft } else {
703 1.12 mycroft slpflag = 0;
704 1.12 mycroft slptimeo = 0;
705 1.12 mycroft }
706 1.12 mycroft /*
707 1.12 mycroft * First wait for any other process doing a flush to complete.
708 1.12 mycroft */
709 1.12 mycroft while (np->n_flag & NFLUSHINPROG) {
710 1.12 mycroft np->n_flag |= NFLUSHWANT;
711 1.12 mycroft error = tsleep((caddr_t)&np->n_flag, PRIBIO + 2, "nfsvinval",
712 1.12 mycroft slptimeo);
713 1.45.8.2 bouyer if (error && intrflg && nfs_sigintr(nmp, NULL, p))
714 1.12 mycroft return (EINTR);
715 1.12 mycroft }
716 1.12 mycroft
717 1.12 mycroft /*
718 1.12 mycroft * Now, flush as required.
719 1.12 mycroft */
720 1.12 mycroft np->n_flag |= NFLUSHINPROG;
721 1.12 mycroft error = vinvalbuf(vp, flags, cred, p, slpflag, 0);
722 1.12 mycroft while (error) {
723 1.45.8.2 bouyer if (intrflg && nfs_sigintr(nmp, NULL, p)) {
724 1.12 mycroft np->n_flag &= ~NFLUSHINPROG;
725 1.12 mycroft if (np->n_flag & NFLUSHWANT) {
726 1.12 mycroft np->n_flag &= ~NFLUSHWANT;
727 1.12 mycroft wakeup((caddr_t)&np->n_flag);
728 1.12 mycroft }
729 1.12 mycroft return (EINTR);
730 1.12 mycroft }
731 1.12 mycroft error = vinvalbuf(vp, flags, cred, p, 0, slptimeo);
732 1.12 mycroft }
733 1.12 mycroft np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
734 1.12 mycroft if (np->n_flag & NFLUSHWANT) {
735 1.12 mycroft np->n_flag &= ~NFLUSHWANT;
736 1.12 mycroft wakeup((caddr_t)&np->n_flag);
737 1.12 mycroft }
738 1.12 mycroft return (0);
739 1.12 mycroft }
740 1.12 mycroft
741 1.12 mycroft /*
742 1.12 mycroft * Initiate asynchronous I/O. Return an error if no nfsiods are available.
743 1.12 mycroft * This is mainly to avoid queueing async I/O requests when the nfsiods
744 1.12 mycroft * are all hung on a dead server.
745 1.12 mycroft */
746 1.23 christos int
747 1.45.8.2 bouyer nfs_asyncio(bp)
748 1.45.8.1 bouyer struct buf *bp;
749 1.12 mycroft {
750 1.45.8.1 bouyer int i;
751 1.45.8.1 bouyer struct nfsmount *nmp;
752 1.30 thorpej int gotiod, slpflag = 0, slptimeo = 0, error;
753 1.12 mycroft
754 1.12 mycroft if (nfs_numasync == 0)
755 1.12 mycroft return (EIO);
756 1.30 thorpej
757 1.30 thorpej
758 1.30 thorpej nmp = VFSTONFS(bp->b_vp->v_mount);
759 1.30 thorpej again:
760 1.30 thorpej if (nmp->nm_flag & NFSMNT_INT)
761 1.30 thorpej slpflag = PCATCH;
762 1.30 thorpej gotiod = FALSE;
763 1.30 thorpej
764 1.30 thorpej /*
765 1.30 thorpej * Find a free iod to process this request.
766 1.30 thorpej */
767 1.30 thorpej
768 1.12 mycroft for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
769 1.30 thorpej if (nfs_iodwant[i]) {
770 1.30 thorpej /*
771 1.30 thorpej * Found one, so wake it up and tell it which
772 1.30 thorpej * mount to process.
773 1.30 thorpej */
774 1.45.8.2 bouyer nfs_iodwant[i] = NULL;
775 1.30 thorpej nfs_iodmount[i] = nmp;
776 1.30 thorpej nmp->nm_bufqiods++;
777 1.30 thorpej wakeup((caddr_t)&nfs_iodwant[i]);
778 1.30 thorpej gotiod = TRUE;
779 1.31 fvdl break;
780 1.30 thorpej }
781 1.30 thorpej /*
782 1.30 thorpej * If none are free, we may already have an iod working on this mount
783 1.30 thorpej * point. If so, it will process our request.
784 1.30 thorpej */
785 1.30 thorpej if (!gotiod && nmp->nm_bufqiods > 0)
786 1.30 thorpej gotiod = TRUE;
787 1.30 thorpej
788 1.30 thorpej /*
789 1.30 thorpej * If we have an iod which can process the request, then queue
790 1.30 thorpej * the buffer.
791 1.30 thorpej */
792 1.30 thorpej if (gotiod) {
793 1.30 thorpej /*
794 1.30 thorpej * Ensure that the queue never grows too large.
795 1.30 thorpej */
796 1.30 thorpej while (nmp->nm_bufqlen >= 2*nfs_numasync) {
797 1.30 thorpej nmp->nm_bufqwant = TRUE;
798 1.30 thorpej error = tsleep(&nmp->nm_bufq, slpflag | PRIBIO,
799 1.30 thorpej "nfsaio", slptimeo);
800 1.30 thorpej if (error) {
801 1.30 thorpej if (nfs_sigintr(nmp, NULL, bp->b_proc))
802 1.30 thorpej return (EINTR);
803 1.30 thorpej if (slpflag == PCATCH) {
804 1.30 thorpej slpflag = 0;
805 1.30 thorpej slptimeo = 2 * hz;
806 1.30 thorpej }
807 1.30 thorpej }
808 1.30 thorpej /*
809 1.30 thorpej * We might have lost our iod while sleeping,
810 1.30 thorpej * so check and loop if nescessary.
811 1.30 thorpej */
812 1.30 thorpej if (nmp->nm_bufqiods == 0)
813 1.30 thorpej goto again;
814 1.30 thorpej }
815 1.30 thorpej TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist);
816 1.30 thorpej nmp->nm_bufqlen++;
817 1.12 mycroft return (0);
818 1.12 mycroft }
819 1.24 fvdl
820 1.24 fvdl /*
821 1.30 thorpej * All the iods are busy on other mounts, so return EIO to
822 1.30 thorpej * force the caller to process the i/o synchronously.
823 1.24 fvdl */
824 1.30 thorpej return (EIO);
825 1.12 mycroft }
826 1.12 mycroft
827 1.12 mycroft /*
828 1.12 mycroft * Do an I/O operation to/from a cache block. This may be called
829 1.12 mycroft * synchronously or from an nfsiod.
830 1.12 mycroft */
831 1.12 mycroft int
832 1.45.8.2 bouyer nfs_doio(bp, p)
833 1.45.8.1 bouyer struct buf *bp;
834 1.12 mycroft struct proc *p;
835 1.12 mycroft {
836 1.45.8.1 bouyer struct uio *uiop;
837 1.45.8.1 bouyer struct vnode *vp;
838 1.12 mycroft struct nfsnode *np;
839 1.12 mycroft struct nfsmount *nmp;
840 1.45.8.2 bouyer int error = 0, diff, len, iomode, must_commit = 0;
841 1.12 mycroft struct uio uio;
842 1.12 mycroft struct iovec io;
843 1.12 mycroft
844 1.12 mycroft vp = bp->b_vp;
845 1.12 mycroft np = VTONFS(vp);
846 1.12 mycroft nmp = VFSTONFS(vp->v_mount);
847 1.12 mycroft uiop = &uio;
848 1.12 mycroft uiop->uio_iov = &io;
849 1.12 mycroft uiop->uio_iovcnt = 1;
850 1.12 mycroft uiop->uio_segflg = UIO_SYSSPACE;
851 1.12 mycroft uiop->uio_procp = p;
852 1.12 mycroft
853 1.12 mycroft /*
854 1.14 pk * Historically, paging was done with physio, but no more...
855 1.12 mycroft */
856 1.14 pk if (bp->b_flags & B_PHYS) {
857 1.14 pk /*
858 1.14 pk * ...though reading /dev/drum still gets us here.
859 1.14 pk */
860 1.14 pk io.iov_len = uiop->uio_resid = bp->b_bcount;
861 1.14 pk /* mapping was done by vmapbuf() */
862 1.14 pk io.iov_base = bp->b_data;
863 1.45.8.2 bouyer uiop->uio_offset = ((off_t)bp->b_blkno) << DEV_BSHIFT;
864 1.14 pk if (bp->b_flags & B_READ) {
865 1.14 pk uiop->uio_rw = UIO_READ;
866 1.14 pk nfsstats.read_physios++;
867 1.45.8.2 bouyer error = nfs_readrpc(vp, uiop);
868 1.14 pk } else {
869 1.24 fvdl iomode = NFSV3WRITE_DATASYNC;
870 1.14 pk uiop->uio_rw = UIO_WRITE;
871 1.14 pk nfsstats.write_physios++;
872 1.45.8.2 bouyer error = nfs_writerpc(vp, uiop, &iomode, &must_commit);
873 1.14 pk }
874 1.14 pk if (error) {
875 1.14 pk bp->b_flags |= B_ERROR;
876 1.14 pk bp->b_error = error;
877 1.14 pk }
878 1.14 pk } else if (bp->b_flags & B_READ) {
879 1.12 mycroft io.iov_len = uiop->uio_resid = bp->b_bcount;
880 1.12 mycroft io.iov_base = bp->b_data;
881 1.12 mycroft uiop->uio_rw = UIO_READ;
882 1.12 mycroft switch (vp->v_type) {
883 1.12 mycroft case VREG:
884 1.45.8.2 bouyer uiop->uio_offset = ((off_t)bp->b_blkno) << DEV_BSHIFT;
885 1.12 mycroft nfsstats.read_bios++;
886 1.45.8.2 bouyer error = nfs_readrpc(vp, uiop);
887 1.45.8.2 bouyer if (!error && uiop->uio_resid) {
888 1.45.8.2 bouyer
889 1.12 mycroft /*
890 1.12 mycroft * If len > 0, there is a hole in the file and
891 1.12 mycroft * no writes after the hole have been pushed to
892 1.12 mycroft * the server yet.
893 1.12 mycroft * Just zero fill the rest of the valid area.
894 1.12 mycroft */
895 1.45.8.2 bouyer
896 1.12 mycroft diff = bp->b_bcount - uiop->uio_resid;
897 1.45.8.2 bouyer len = np->n_size - ((((off_t)bp->b_blkno) << DEV_BSHIFT)
898 1.12 mycroft + diff);
899 1.12 mycroft if (len > 0) {
900 1.45.8.7 bouyer len = MIN(len, uiop->uio_resid);
901 1.45.8.2 bouyer memset((char *)bp->b_data + diff, 0, len);
902 1.45.8.2 bouyer }
903 1.12 mycroft }
904 1.12 mycroft if (p && (vp->v_flag & VTEXT) &&
905 1.12 mycroft (((nmp->nm_flag & NFSMNT_NQNFS) &&
906 1.24 fvdl NQNFS_CKINVALID(vp, np, ND_READ) &&
907 1.12 mycroft np->n_lrev != np->n_brev) ||
908 1.12 mycroft (!(nmp->nm_flag & NFSMNT_NQNFS) &&
909 1.35 fvdl np->n_mtime != np->n_vattr->va_mtime.tv_sec))) {
910 1.45.8.2 bouyer uprintf("Process killed due to "
911 1.45.8.2 bouyer "text file modification\n");
912 1.12 mycroft psignal(p, SIGKILL);
913 1.13 mycroft p->p_holdcnt++;
914 1.12 mycroft }
915 1.12 mycroft break;
916 1.12 mycroft case VLNK:
917 1.24 fvdl uiop->uio_offset = (off_t)0;
918 1.12 mycroft nfsstats.readlink_bios++;
919 1.45.8.2 bouyer error = nfs_readlinkrpc(vp, uiop, curproc->p_ucred);
920 1.12 mycroft break;
921 1.12 mycroft case VDIR:
922 1.12 mycroft nfsstats.readdir_bios++;
923 1.34 fvdl uiop->uio_offset = bp->b_dcookie;
924 1.24 fvdl if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
925 1.45.8.2 bouyer error = nfs_readdirplusrpc(vp, uiop, curproc->p_ucred);
926 1.24 fvdl if (error == NFSERR_NOTSUPP)
927 1.24 fvdl nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
928 1.24 fvdl }
929 1.24 fvdl if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
930 1.45.8.2 bouyer error = nfs_readdirrpc(vp, uiop, curproc->p_ucred);
931 1.34 fvdl if (!error) {
932 1.34 fvdl bp->b_dcookie = uiop->uio_offset;
933 1.34 fvdl }
934 1.24 fvdl break;
935 1.24 fvdl default:
936 1.29 christos printf("nfs_doio: type %x unexpected\n",vp->v_type);
937 1.12 mycroft break;
938 1.45.8.2 bouyer }
939 1.12 mycroft if (error) {
940 1.12 mycroft bp->b_flags |= B_ERROR;
941 1.12 mycroft bp->b_error = error;
942 1.12 mycroft }
943 1.12 mycroft } else {
944 1.45.8.1 bouyer /*
945 1.45.8.1 bouyer * If B_NEEDCOMMIT is set, a commit rpc may do the trick. If not
946 1.45.8.1 bouyer * an actual write will have to be scheduled.
947 1.45.8.1 bouyer */
948 1.45.8.2 bouyer
949 1.45.8.2 bouyer io.iov_base = bp->b_data;
950 1.45.8.2 bouyer io.iov_len = uiop->uio_resid = bp->b_bcount;
951 1.45.8.2 bouyer uiop->uio_offset = (((off_t)bp->b_blkno) << DEV_BSHIFT);
952 1.45.8.2 bouyer uiop->uio_rw = UIO_WRITE;
953 1.45.8.2 bouyer nfsstats.write_bios++;
954 1.45.8.2 bouyer iomode = NFSV3WRITE_UNSTABLE;
955 1.45.8.2 bouyer error = nfs_writerpc(vp, uiop, &iomode, &must_commit);
956 1.45.8.2 bouyer }
957 1.45.8.2 bouyer bp->b_resid = uiop->uio_resid;
958 1.45.8.2 bouyer if (must_commit)
959 1.45.8.2 bouyer nfs_clearcommit(vp->v_mount);
960 1.45.8.2 bouyer biodone(bp);
961 1.45.8.2 bouyer return (error);
962 1.45.8.2 bouyer }
963 1.45.8.2 bouyer
964 1.45.8.2 bouyer /*
965 1.45.8.2 bouyer * Vnode op for VM getpages.
966 1.45.8.2 bouyer */
967 1.45.8.2 bouyer int
968 1.45.8.2 bouyer nfs_getpages(v)
969 1.45.8.2 bouyer void *v;
970 1.45.8.2 bouyer {
971 1.45.8.2 bouyer struct vop_getpages_args /* {
972 1.45.8.2 bouyer struct vnode *a_vp;
973 1.45.8.2 bouyer voff_t a_offset;
974 1.45.8.2 bouyer vm_page_t *a_m;
975 1.45.8.2 bouyer int *a_count;
976 1.45.8.2 bouyer int a_centeridx;
977 1.45.8.2 bouyer vm_prot_t a_access_type;
978 1.45.8.2 bouyer int a_advice;
979 1.45.8.2 bouyer int a_flags;
980 1.45.8.2 bouyer } */ *ap = v;
981 1.45.8.2 bouyer
982 1.45.8.2 bouyer off_t eof, offset, origoffset, startoffset, endoffset;
983 1.45.8.2 bouyer int s, i, error, npages, orignpages, npgs, ridx, pidx, pcount;
984 1.45.8.2 bouyer vaddr_t kva;
985 1.45.8.2 bouyer struct buf *bp, *mbp;
986 1.45.8.2 bouyer struct vnode *vp = ap->a_vp;
987 1.45.8.2 bouyer struct nfsnode *np = VTONFS(vp);
988 1.45.8.2 bouyer struct uvm_object *uobj = &vp->v_uvm.u_obj;
989 1.45.8.2 bouyer struct nfsmount *nmp = VFSTONFS(vp->v_mount);
990 1.45.8.2 bouyer size_t bytes, iobytes, tailbytes, totalbytes, skipbytes;
991 1.45.8.2 bouyer int flags = ap->a_flags;
992 1.45.8.2 bouyer int bsize;
993 1.45.8.2 bouyer struct vm_page *pgs[16]; /* XXXUBC 16 */
994 1.45.8.2 bouyer boolean_t v3 = NFS_ISV3(vp);
995 1.45.8.2 bouyer boolean_t async = (flags & PGO_SYNCIO) == 0;
996 1.45.8.2 bouyer boolean_t write = (ap->a_access_type & VM_PROT_WRITE) != 0;
997 1.45.8.2 bouyer
998 1.45.8.2 bouyer UVMHIST_FUNC("nfs_getpages"); UVMHIST_CALLED(ubchist);
999 1.45.8.2 bouyer UVMHIST_LOG(ubchist, "vp %p off 0x%x count %d", vp, (int)ap->a_offset,
1000 1.45.8.2 bouyer *ap->a_count,0);
1001 1.45.8.2 bouyer
1002 1.45.8.2 bouyer #ifdef DIAGNOSTIC
1003 1.45.8.2 bouyer if (ap->a_centeridx < 0 || ap->a_centeridx >= *ap->a_count) {
1004 1.45.8.2 bouyer panic("nfs_getpages: centeridx %d out of range",
1005 1.45.8.2 bouyer ap->a_centeridx);
1006 1.45.8.2 bouyer }
1007 1.45.8.2 bouyer #endif
1008 1.45.8.2 bouyer
1009 1.45.8.2 bouyer error = 0;
1010 1.45.8.2 bouyer origoffset = ap->a_offset;
1011 1.45.8.2 bouyer eof = vp->v_uvm.u_size;
1012 1.45.8.2 bouyer if (origoffset >= eof) {
1013 1.45.8.2 bouyer if ((flags & PGO_LOCKED) == 0) {
1014 1.45.8.2 bouyer simple_unlock(&uobj->vmobjlock);
1015 1.45.8.2 bouyer }
1016 1.45.8.2 bouyer UVMHIST_LOG(ubchist, "off 0x%x past EOF 0x%x",
1017 1.45.8.2 bouyer (int)origoffset, (int)eof,0,0);
1018 1.45.8.2 bouyer return EINVAL;
1019 1.45.8.2 bouyer }
1020 1.45.8.2 bouyer
1021 1.45.8.2 bouyer if (flags & PGO_LOCKED) {
1022 1.45.8.2 bouyer uvn_findpages(uobj, origoffset, ap->a_count, ap->a_m,
1023 1.45.8.2 bouyer UFP_NOWAIT|UFP_NOALLOC);
1024 1.45.8.2 bouyer return 0;
1025 1.45.8.2 bouyer }
1026 1.45.8.2 bouyer
1027 1.45.8.2 bouyer /* vnode is VOP_LOCKed, uobj is locked */
1028 1.45.8.2 bouyer
1029 1.45.8.2 bouyer bsize = nmp->nm_rsize;
1030 1.45.8.6 bouyer orignpages = MIN(*ap->a_count,
1031 1.45.8.2 bouyer round_page(eof - origoffset) >> PAGE_SHIFT);
1032 1.45.8.2 bouyer npages = orignpages;
1033 1.45.8.2 bouyer startoffset = origoffset & ~(bsize - 1);
1034 1.45.8.2 bouyer endoffset = round_page((origoffset + (npages << PAGE_SHIFT)
1035 1.45.8.2 bouyer + bsize - 1) & ~(bsize - 1));
1036 1.45.8.6 bouyer endoffset = MIN(endoffset, round_page(eof));
1037 1.45.8.2 bouyer ridx = (origoffset - startoffset) >> PAGE_SHIFT;
1038 1.45.8.2 bouyer
1039 1.45.8.2 bouyer if (!async && !write) {
1040 1.45.8.6 bouyer int rapages = MAX(PAGE_SIZE, nmp->nm_rsize) >> PAGE_SHIFT;
1041 1.45.8.2 bouyer
1042 1.45.8.2 bouyer (void) VOP_GETPAGES(vp, endoffset, NULL, &rapages, 0,
1043 1.45.8.2 bouyer VM_PROT_READ, 0, 0);
1044 1.45.8.2 bouyer simple_lock(&uobj->vmobjlock);
1045 1.45.8.2 bouyer }
1046 1.45.8.2 bouyer
1047 1.45.8.2 bouyer UVMHIST_LOG(ubchist, "npages %d offset 0x%x", npages,
1048 1.45.8.2 bouyer (int)origoffset, 0,0);
1049 1.45.8.2 bouyer memset(pgs, 0, sizeof(pgs));
1050 1.45.8.2 bouyer uvn_findpages(uobj, origoffset, &npages, &pgs[ridx], UFP_ALL);
1051 1.45.8.2 bouyer
1052 1.45.8.2 bouyer if (flags & PGO_OVERWRITE) {
1053 1.45.8.2 bouyer UVMHIST_LOG(ubchist, "PGO_OVERWRITE",0,0,0,0);
1054 1.45.8.2 bouyer
1055 1.45.8.2 bouyer /* XXXUBC for now, zero the page if we allocated it */
1056 1.45.8.2 bouyer for (i = 0; i < npages; i++) {
1057 1.45.8.2 bouyer struct vm_page *pg = pgs[ridx + i];
1058 1.45.8.2 bouyer
1059 1.45.8.2 bouyer if (pg->flags & PG_FAKE) {
1060 1.45.8.2 bouyer uvm_pagezero(pg);
1061 1.45.8.2 bouyer pg->flags &= ~(PG_FAKE);
1062 1.45.8.2 bouyer }
1063 1.45.8.2 bouyer }
1064 1.45.8.6 bouyer npages += ridx;
1065 1.45.8.4 bouyer if (v3) {
1066 1.45.8.4 bouyer simple_unlock(&uobj->vmobjlock);
1067 1.45.8.4 bouyer goto uncommit;
1068 1.45.8.4 bouyer }
1069 1.45.8.2 bouyer goto out;
1070 1.45.8.2 bouyer }
1071 1.45.8.2 bouyer
1072 1.45.8.2 bouyer /*
1073 1.45.8.2 bouyer * if the pages are already resident, just return them.
1074 1.45.8.2 bouyer */
1075 1.45.8.2 bouyer
1076 1.45.8.2 bouyer for (i = 0; i < npages; i++) {
1077 1.45.8.2 bouyer struct vm_page *pg = pgs[ridx + i];
1078 1.45.8.2 bouyer
1079 1.45.8.2 bouyer if ((pg->flags & PG_FAKE) != 0 ||
1080 1.45.8.2 bouyer ((ap->a_access_type & VM_PROT_WRITE) &&
1081 1.45.8.2 bouyer (pg->flags & PG_RDONLY))) {
1082 1.45.8.2 bouyer break;
1083 1.45.8.2 bouyer }
1084 1.45.8.2 bouyer }
1085 1.45.8.2 bouyer if (i == npages) {
1086 1.45.8.2 bouyer UVMHIST_LOG(ubchist, "returning cached pages", 0,0,0,0);
1087 1.45.8.6 bouyer npages += ridx;
1088 1.45.8.2 bouyer goto out;
1089 1.45.8.2 bouyer }
1090 1.45.8.2 bouyer
1091 1.45.8.2 bouyer /*
1092 1.45.8.2 bouyer * the page wasn't resident and we're not overwriting,
1093 1.45.8.2 bouyer * so we're going to have to do some i/o.
1094 1.45.8.2 bouyer * find any additional pages needed to cover the expanded range.
1095 1.45.8.2 bouyer */
1096 1.45.8.2 bouyer
1097 1.45.8.2 bouyer if (startoffset != origoffset ||
1098 1.45.8.2 bouyer startoffset + (npages << PAGE_SHIFT) != endoffset) {
1099 1.45.8.4 bouyer
1100 1.45.8.4 bouyer /*
1101 1.45.8.4 bouyer * XXXUBC we need to avoid deadlocks caused by locking
1102 1.45.8.4 bouyer * additional pages at lower offsets than pages we
1103 1.45.8.4 bouyer * already have locked. for now, unlock them all and
1104 1.45.8.4 bouyer * start over.
1105 1.45.8.4 bouyer */
1106 1.45.8.4 bouyer
1107 1.45.8.4 bouyer for (i = 0; i < npages; i++) {
1108 1.45.8.4 bouyer struct vm_page *pg = pgs[ridx + i];
1109 1.45.8.4 bouyer
1110 1.45.8.4 bouyer if (pg->flags & PG_FAKE) {
1111 1.45.8.4 bouyer pg->flags |= PG_RELEASED;
1112 1.45.8.4 bouyer }
1113 1.45.8.4 bouyer }
1114 1.45.8.4 bouyer uvm_page_unbusy(&pgs[ridx], npages);
1115 1.45.8.4 bouyer memset(pgs, 0, sizeof(pgs));
1116 1.45.8.4 bouyer
1117 1.45.8.2 bouyer UVMHIST_LOG(ubchist, "reset npages start 0x%x end 0x%x",
1118 1.45.8.4 bouyer startoffset, endoffset, 0,0);
1119 1.45.8.2 bouyer npages = (endoffset - startoffset) >> PAGE_SHIFT;
1120 1.45.8.2 bouyer npgs = npages;
1121 1.45.8.2 bouyer uvn_findpages(uobj, startoffset, &npgs, pgs, UFP_ALL);
1122 1.45.8.2 bouyer }
1123 1.45.8.2 bouyer simple_unlock(&uobj->vmobjlock);
1124 1.45.8.2 bouyer
1125 1.45.8.2 bouyer /*
1126 1.45.8.2 bouyer * update the cached read creds for this node.
1127 1.45.8.2 bouyer */
1128 1.45.8.2 bouyer
1129 1.45.8.2 bouyer if (np->n_rcred) {
1130 1.45.8.2 bouyer crfree(np->n_rcred);
1131 1.45.8.2 bouyer }
1132 1.45.8.2 bouyer np->n_rcred = curproc->p_ucred;
1133 1.45.8.2 bouyer crhold(np->n_rcred);
1134 1.45.8.2 bouyer
1135 1.45.8.2 bouyer /*
1136 1.45.8.2 bouyer * read the desired page(s).
1137 1.45.8.2 bouyer */
1138 1.45.8.2 bouyer
1139 1.45.8.2 bouyer totalbytes = npages << PAGE_SHIFT;
1140 1.45.8.6 bouyer bytes = MIN(totalbytes, vp->v_uvm.u_size - startoffset);
1141 1.45.8.2 bouyer tailbytes = totalbytes - bytes;
1142 1.45.8.2 bouyer skipbytes = 0;
1143 1.45.8.2 bouyer
1144 1.45.8.2 bouyer kva = uvm_pagermapin(pgs, npages, UVMPAGER_MAPIN_WAITOK |
1145 1.45.8.2 bouyer UVMPAGER_MAPIN_READ);
1146 1.45.8.2 bouyer
1147 1.45.8.2 bouyer s = splbio();
1148 1.45.8.2 bouyer mbp = pool_get(&bufpool, PR_WAITOK);
1149 1.45.8.2 bouyer splx(s);
1150 1.45.8.2 bouyer mbp->b_bufsize = totalbytes;
1151 1.45.8.2 bouyer mbp->b_data = (void *)kva;
1152 1.45.8.2 bouyer mbp->b_resid = mbp->b_bcount = bytes;
1153 1.45.8.2 bouyer mbp->b_flags = B_BUSY|B_READ| (async ? B_CALL|B_ASYNC : 0);
1154 1.45.8.2 bouyer mbp->b_iodone = uvm_aio_biodone;
1155 1.45.8.2 bouyer mbp->b_vp = vp;
1156 1.45.8.6 bouyer mbp->b_proc = NULL; /* XXXUBC */
1157 1.45.8.2 bouyer LIST_INIT(&mbp->b_dep);
1158 1.45.8.2 bouyer
1159 1.45.8.2 bouyer /*
1160 1.45.8.2 bouyer * if EOF is in the middle of the last page, zero the part past EOF.
1161 1.45.8.2 bouyer */
1162 1.45.8.2 bouyer
1163 1.45.8.3 bouyer if (tailbytes > 0 && (pgs[bytes >> PAGE_SHIFT]->flags & PG_FAKE)) {
1164 1.45.8.2 bouyer memset((char *)kva + bytes, 0, tailbytes);
1165 1.45.8.2 bouyer }
1166 1.45.8.2 bouyer
1167 1.45.8.2 bouyer /*
1168 1.45.8.2 bouyer * now loop over the pages, reading as needed.
1169 1.45.8.2 bouyer */
1170 1.45.8.2 bouyer
1171 1.45.8.2 bouyer bp = NULL;
1172 1.45.8.2 bouyer for (offset = startoffset;
1173 1.45.8.2 bouyer bytes > 0;
1174 1.45.8.2 bouyer offset += iobytes, bytes -= iobytes) {
1175 1.45.8.2 bouyer
1176 1.45.8.1 bouyer /*
1177 1.45.8.2 bouyer * skip pages which don't need to be read.
1178 1.45.8.1 bouyer */
1179 1.45.8.2 bouyer
1180 1.45.8.2 bouyer pidx = (offset - startoffset) >> PAGE_SHIFT;
1181 1.45.8.2 bouyer UVMHIST_LOG(ubchist, "pidx %d offset 0x%x startoffset 0x%x",
1182 1.45.8.2 bouyer pidx, (int)offset, (int)startoffset,0);
1183 1.45.8.2 bouyer while ((pgs[pidx]->flags & PG_FAKE) == 0) {
1184 1.45.8.2 bouyer size_t b;
1185 1.45.8.2 bouyer
1186 1.45.8.4 bouyer KASSERT((offset & (PAGE_SIZE - 1)) == 0);
1187 1.45.8.6 bouyer b = MIN(PAGE_SIZE, bytes);
1188 1.45.8.2 bouyer offset += b;
1189 1.45.8.2 bouyer bytes -= b;
1190 1.45.8.2 bouyer skipbytes += b;
1191 1.45.8.2 bouyer pidx++;
1192 1.45.8.2 bouyer UVMHIST_LOG(ubchist, "skipping, new offset 0x%x",
1193 1.45.8.2 bouyer (int)offset, 0,0,0);
1194 1.45.8.2 bouyer if (bytes == 0) {
1195 1.45.8.2 bouyer goto loopdone;
1196 1.45.8.1 bouyer }
1197 1.45.8.1 bouyer }
1198 1.45.8.1 bouyer
1199 1.45.8.2 bouyer /*
1200 1.45.8.2 bouyer * see how many pages can be read with this i/o.
1201 1.45.8.2 bouyer * reduce the i/o size if necessary.
1202 1.45.8.2 bouyer */
1203 1.45.8.2 bouyer
1204 1.45.8.2 bouyer iobytes = bytes;
1205 1.45.8.2 bouyer if (offset + iobytes > round_page(offset)) {
1206 1.45.8.2 bouyer pcount = 1;
1207 1.45.8.2 bouyer while (pidx + pcount < npages &&
1208 1.45.8.2 bouyer pgs[pidx + pcount]->flags & PG_FAKE) {
1209 1.45.8.2 bouyer pcount++;
1210 1.45.8.2 bouyer }
1211 1.45.8.6 bouyer iobytes = MIN(iobytes, (pcount << PAGE_SHIFT) -
1212 1.45.8.2 bouyer (offset - trunc_page(offset)));
1213 1.45.8.2 bouyer }
1214 1.45.8.6 bouyer iobytes = MIN(iobytes, nmp->nm_rsize);
1215 1.45.8.2 bouyer
1216 1.45.8.2 bouyer /*
1217 1.45.8.2 bouyer * allocate a sub-buf for this piece of the i/o
1218 1.45.8.2 bouyer * (or just use mbp if there's only 1 piece),
1219 1.45.8.2 bouyer * and start it going.
1220 1.45.8.2 bouyer */
1221 1.45.8.2 bouyer
1222 1.45.8.2 bouyer if (offset == startoffset && iobytes == bytes) {
1223 1.45.8.2 bouyer bp = mbp;
1224 1.45.8.2 bouyer } else {
1225 1.45.8.2 bouyer s = splbio();
1226 1.45.8.2 bouyer bp = pool_get(&bufpool, PR_WAITOK);
1227 1.45.8.2 bouyer splx(s);
1228 1.45.8.2 bouyer bp->b_data = (char *)kva + offset - startoffset;
1229 1.45.8.2 bouyer bp->b_resid = bp->b_bcount = iobytes;
1230 1.45.8.2 bouyer bp->b_flags = B_BUSY|B_READ|B_CALL|B_ASYNC;
1231 1.45.8.2 bouyer bp->b_iodone = uvm_aio_biodone1;
1232 1.45.8.2 bouyer bp->b_vp = vp;
1233 1.45.8.6 bouyer bp->b_proc = NULL; /* XXXUBC */
1234 1.45.8.2 bouyer LIST_INIT(&bp->b_dep);
1235 1.45.8.2 bouyer }
1236 1.45.8.2 bouyer bp->b_private = mbp;
1237 1.45.8.2 bouyer bp->b_lblkno = bp->b_blkno = offset >> DEV_BSHIFT;
1238 1.45.8.2 bouyer
1239 1.45.8.2 bouyer UVMHIST_LOG(ubchist, "bp %p offset 0x%x bcount 0x%x blkno 0x%x",
1240 1.45.8.2 bouyer bp, offset, iobytes, bp->b_blkno);
1241 1.45.8.2 bouyer
1242 1.45.8.2 bouyer VOP_STRATEGY(bp);
1243 1.45.8.2 bouyer }
1244 1.45.8.2 bouyer
1245 1.45.8.2 bouyer loopdone:
1246 1.45.8.2 bouyer if (skipbytes) {
1247 1.45.8.2 bouyer s = splbio();
1248 1.45.8.2 bouyer mbp->b_resid -= skipbytes;
1249 1.45.8.2 bouyer if (mbp->b_resid == 0) {
1250 1.45.8.2 bouyer biodone(mbp);
1251 1.45.8.2 bouyer }
1252 1.45.8.2 bouyer splx(s);
1253 1.45.8.2 bouyer }
1254 1.45.8.2 bouyer if (async) {
1255 1.45.8.7 bouyer UVMHIST_LOG(ubchist, "returning 0 (async)",0,0,0,0);
1256 1.45.8.7 bouyer return 0;
1257 1.45.8.2 bouyer }
1258 1.45.8.2 bouyer if (bp != NULL) {
1259 1.45.8.2 bouyer error = biowait(mbp);
1260 1.45.8.2 bouyer }
1261 1.45.8.2 bouyer s = splbio();
1262 1.45.8.2 bouyer pool_put(&bufpool, mbp);
1263 1.45.8.2 bouyer splx(s);
1264 1.45.8.2 bouyer uvm_pagermapout(kva, npages);
1265 1.45.8.2 bouyer
1266 1.45.8.2 bouyer if (write && v3) {
1267 1.45.8.4 bouyer uncommit:
1268 1.45.8.1 bouyer lockmgr(&np->n_commitlock, LK_EXCLUSIVE, NULL);
1269 1.45.8.2 bouyer nfs_del_committed_range(vp, origoffset, npages);
1270 1.45.8.2 bouyer nfs_del_tobecommitted_range(vp, origoffset, npages);
1271 1.45.8.4 bouyer simple_lock(&uobj->vmobjlock);
1272 1.45.8.2 bouyer for (i = 0; i < npages; i++) {
1273 1.45.8.2 bouyer if (pgs[i] == NULL) {
1274 1.45.8.2 bouyer continue;
1275 1.45.8.2 bouyer }
1276 1.45.8.2 bouyer pgs[i]->flags &= ~(PG_NEEDCOMMIT|PG_RDONLY);
1277 1.45.8.2 bouyer }
1278 1.45.8.4 bouyer simple_unlock(&uobj->vmobjlock);
1279 1.45.8.1 bouyer lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
1280 1.45.8.2 bouyer }
1281 1.45.8.2 bouyer
1282 1.45.8.2 bouyer simple_lock(&uobj->vmobjlock);
1283 1.45.8.2 bouyer
1284 1.45.8.2 bouyer out:
1285 1.45.8.2 bouyer if (error) {
1286 1.45.8.6 bouyer uvm_lock_pageq();
1287 1.45.8.2 bouyer for (i = 0; i < npages; i++) {
1288 1.45.8.2 bouyer if (pgs[i] == NULL) {
1289 1.45.8.2 bouyer continue;
1290 1.45.8.2 bouyer }
1291 1.45.8.2 bouyer UVMHIST_LOG(ubchist, "examining pg %p flags 0x%x",
1292 1.45.8.2 bouyer pgs[i], pgs[i]->flags, 0,0);
1293 1.45.8.2 bouyer if (pgs[i]->flags & PG_WANTED) {
1294 1.45.8.2 bouyer wakeup(pgs[i]);
1295 1.45.8.2 bouyer }
1296 1.45.8.6 bouyer if (pgs[i]->flags & PG_RELEASED) {
1297 1.45.8.6 bouyer uvm_unlock_pageq();
1298 1.45.8.6 bouyer (uobj->pgops->pgo_releasepg)(pgs[i], NULL);
1299 1.45.8.6 bouyer uvm_lock_pageq();
1300 1.45.8.6 bouyer continue;
1301 1.45.8.6 bouyer }
1302 1.45.8.6 bouyer if (pgs[i]->flags & PG_FAKE) {
1303 1.45.8.6 bouyer uvm_pagefree(pgs[i]);
1304 1.45.8.7 bouyer continue;
1305 1.45.8.6 bouyer }
1306 1.45.8.7 bouyer uvm_pageactivate(pgs[i]);
1307 1.45.8.7 bouyer pgs[i]->flags &= ~(PG_WANTED|PG_BUSY);
1308 1.45.8.7 bouyer UVM_PAGE_OWN(pgs[i], NULL);
1309 1.45.8.2 bouyer }
1310 1.45.8.6 bouyer uvm_unlock_pageq();
1311 1.45.8.6 bouyer simple_unlock(&uobj->vmobjlock);
1312 1.45.8.6 bouyer UVMHIST_LOG(ubchist, "returning error %d", error,0,0,0);
1313 1.45.8.6 bouyer return error;
1314 1.45.8.2 bouyer }
1315 1.45.8.2 bouyer
1316 1.45.8.2 bouyer UVMHIST_LOG(ubchist, "ridx %d count %d", ridx, npages, 0,0);
1317 1.45.8.6 bouyer uvm_lock_pageq();
1318 1.45.8.2 bouyer for (i = 0; i < npages; i++) {
1319 1.45.8.2 bouyer if (pgs[i] == NULL) {
1320 1.45.8.2 bouyer continue;
1321 1.45.8.2 bouyer }
1322 1.45.8.2 bouyer UVMHIST_LOG(ubchist, "examining pg %p flags 0x%x",
1323 1.45.8.2 bouyer pgs[i], pgs[i]->flags, 0,0);
1324 1.45.8.2 bouyer if (pgs[i]->flags & PG_FAKE) {
1325 1.45.8.2 bouyer UVMHIST_LOG(ubchist, "unfaking pg %p offset 0x%x",
1326 1.45.8.2 bouyer pgs[i], (int)pgs[i]->offset,0,0);
1327 1.45.8.2 bouyer pgs[i]->flags &= ~(PG_FAKE);
1328 1.45.8.2 bouyer pmap_clear_modify(pgs[i]);
1329 1.45.8.2 bouyer pmap_clear_reference(pgs[i]);
1330 1.45.8.2 bouyer }
1331 1.45.8.2 bouyer if (i < ridx || i >= ridx + orignpages || async) {
1332 1.45.8.2 bouyer UVMHIST_LOG(ubchist, "unbusy pg %p offset 0x%x",
1333 1.45.8.2 bouyer pgs[i], (int)pgs[i]->offset,0,0);
1334 1.45.8.2 bouyer if (pgs[i]->flags & PG_WANTED) {
1335 1.45.8.2 bouyer wakeup(pgs[i]);
1336 1.45.8.2 bouyer }
1337 1.45.8.6 bouyer if (pgs[i]->flags & PG_RELEASED) {
1338 1.45.8.6 bouyer uvm_unlock_pageq();
1339 1.45.8.6 bouyer (uobj->pgops->pgo_releasepg)(pgs[i], NULL);
1340 1.45.8.6 bouyer uvm_lock_pageq();
1341 1.45.8.6 bouyer continue;
1342 1.45.8.2 bouyer }
1343 1.45.8.6 bouyer uvm_pageactivate(pgs[i]);
1344 1.45.8.2 bouyer pgs[i]->flags &= ~(PG_WANTED|PG_BUSY);
1345 1.45.8.2 bouyer UVM_PAGE_OWN(pgs[i], NULL);
1346 1.45.8.2 bouyer }
1347 1.45.8.2 bouyer }
1348 1.45.8.2 bouyer uvm_unlock_pageq();
1349 1.45.8.2 bouyer simple_unlock(&uobj->vmobjlock);
1350 1.45.8.2 bouyer if (ap->a_m != NULL) {
1351 1.45.8.2 bouyer memcpy(ap->a_m, &pgs[ridx],
1352 1.45.8.2 bouyer *ap->a_count * sizeof(struct vm_page *));
1353 1.45.8.2 bouyer }
1354 1.45.8.6 bouyer return 0;
1355 1.45.8.2 bouyer }
1356 1.45.8.2 bouyer
1357 1.45.8.2 bouyer /*
1358 1.45.8.2 bouyer * Vnode op for VM putpages.
1359 1.45.8.2 bouyer */
1360 1.45.8.2 bouyer int
1361 1.45.8.2 bouyer nfs_putpages(v)
1362 1.45.8.2 bouyer void *v;
1363 1.45.8.2 bouyer {
1364 1.45.8.2 bouyer struct vop_putpages_args /* {
1365 1.45.8.2 bouyer struct vnode *a_vp;
1366 1.45.8.2 bouyer struct vm_page **a_m;
1367 1.45.8.2 bouyer int a_count;
1368 1.45.8.2 bouyer int a_flags;
1369 1.45.8.2 bouyer int *a_rtvals;
1370 1.45.8.2 bouyer } */ *ap = v;
1371 1.45.8.2 bouyer
1372 1.45.8.2 bouyer struct vnode *vp = ap->a_vp;
1373 1.45.8.2 bouyer struct nfsnode *np = VTONFS(vp);
1374 1.45.8.2 bouyer struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1375 1.45.8.2 bouyer struct buf *bp, *mbp;
1376 1.45.8.2 bouyer struct vm_page **pgs = ap->a_m;
1377 1.45.8.2 bouyer int flags = ap->a_flags;
1378 1.45.8.2 bouyer int npages = ap->a_count;
1379 1.45.8.7 bouyer int s, error, i;
1380 1.45.8.2 bouyer size_t bytes, iobytes, skipbytes;
1381 1.45.8.2 bouyer vaddr_t kva;
1382 1.45.8.2 bouyer off_t offset, origoffset, commitoff;
1383 1.45.8.2 bouyer uint32_t commitbytes;
1384 1.45.8.2 bouyer boolean_t v3 = NFS_ISV3(vp);
1385 1.45.8.2 bouyer boolean_t async = (flags & PGO_SYNCIO) == 0;
1386 1.45.8.2 bouyer boolean_t weak = (flags & PGO_WEAK) && v3;
1387 1.45.8.2 bouyer UVMHIST_FUNC("nfs_putpages"); UVMHIST_CALLED(ubchist);
1388 1.45.8.2 bouyer
1389 1.45.8.2 bouyer UVMHIST_LOG(ubchist, "vp %p pgp %p count %d",
1390 1.45.8.2 bouyer vp, ap->a_m, ap->a_count,0);
1391 1.45.8.2 bouyer
1392 1.45.8.2 bouyer simple_unlock(&vp->v_uvm.u_obj.vmobjlock);
1393 1.45.8.2 bouyer
1394 1.45.8.7 bouyer error = 0;
1395 1.45.8.2 bouyer origoffset = pgs[0]->offset;
1396 1.45.8.6 bouyer bytes = MIN(ap->a_count << PAGE_SHIFT, vp->v_uvm.u_size - origoffset);
1397 1.45.8.2 bouyer skipbytes = 0;
1398 1.45.8.2 bouyer
1399 1.45.8.2 bouyer /*
1400 1.45.8.2 bouyer * if the range has been committed already, mark the pages thus.
1401 1.45.8.2 bouyer * if the range just needs to be committed, we're done
1402 1.45.8.2 bouyer * if it's a weak putpage, otherwise commit the range.
1403 1.45.8.2 bouyer */
1404 1.45.8.2 bouyer
1405 1.45.8.2 bouyer if (v3) {
1406 1.45.8.1 bouyer lockmgr(&np->n_commitlock, LK_EXCLUSIVE, NULL);
1407 1.45.8.2 bouyer if (nfs_in_committed_range(vp, origoffset, bytes)) {
1408 1.45.8.2 bouyer goto committed;
1409 1.45.8.2 bouyer }
1410 1.45.8.2 bouyer if (nfs_in_tobecommitted_range(vp, origoffset, bytes)) {
1411 1.45.8.2 bouyer if (weak) {
1412 1.45.8.2 bouyer lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
1413 1.45.8.2 bouyer return 0;
1414 1.45.8.2 bouyer } else {
1415 1.45.8.2 bouyer commitoff = np->n_pushlo;
1416 1.45.8.2 bouyer commitbytes = (uint32_t)(np->n_pushhi -
1417 1.45.8.2 bouyer np->n_pushlo);
1418 1.45.8.2 bouyer goto commit;
1419 1.45.8.2 bouyer }
1420 1.45.8.2 bouyer }
1421 1.45.8.1 bouyer lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
1422 1.45.8.2 bouyer }
1423 1.45.8.2 bouyer
1424 1.45.8.2 bouyer /*
1425 1.45.8.2 bouyer * otherwise write or commit all the pages.
1426 1.45.8.2 bouyer */
1427 1.45.8.2 bouyer
1428 1.45.8.2 bouyer kva = uvm_pagermapin(pgs, ap->a_count, UVMPAGER_MAPIN_WAITOK|
1429 1.45.8.2 bouyer UVMPAGER_MAPIN_WRITE);
1430 1.45.8.2 bouyer
1431 1.45.8.2 bouyer s = splbio();
1432 1.45.8.2 bouyer vp->v_numoutput += 2;
1433 1.45.8.2 bouyer mbp = pool_get(&bufpool, PR_WAITOK);
1434 1.45.8.2 bouyer UVMHIST_LOG(ubchist, "vp %p mbp %p num now %d bytes 0x%x",
1435 1.45.8.2 bouyer vp, mbp, vp->v_numoutput, bytes);
1436 1.45.8.2 bouyer splx(s);
1437 1.45.8.2 bouyer mbp->b_bufsize = npages << PAGE_SHIFT;
1438 1.45.8.2 bouyer mbp->b_data = (void *)kva;
1439 1.45.8.2 bouyer mbp->b_resid = mbp->b_bcount = bytes;
1440 1.45.8.2 bouyer mbp->b_flags = B_BUSY|B_WRITE|B_AGE |
1441 1.45.8.2 bouyer (async ? B_CALL|B_ASYNC : 0) |
1442 1.45.8.2 bouyer (curproc == uvm.pagedaemon_proc ? B_PDAEMON : 0);
1443 1.45.8.5 bouyer mbp->b_iodone = uvm_aio_biodone;
1444 1.45.8.2 bouyer mbp->b_vp = vp;
1445 1.45.8.6 bouyer mbp->b_proc = NULL; /* XXXUBC */
1446 1.45.8.2 bouyer LIST_INIT(&mbp->b_dep);
1447 1.45.8.2 bouyer
1448 1.45.8.2 bouyer for (offset = origoffset;
1449 1.45.8.2 bouyer bytes > 0;
1450 1.45.8.2 bouyer offset += iobytes, bytes -= iobytes) {
1451 1.45.8.6 bouyer iobytes = MIN(nmp->nm_wsize, bytes);
1452 1.12 mycroft
1453 1.12 mycroft /*
1454 1.45.8.2 bouyer * skip writing any pages which only need a commit.
1455 1.12 mycroft */
1456 1.45.8.2 bouyer
1457 1.45.8.2 bouyer if ((pgs[(offset - origoffset) >> PAGE_SHIFT]->flags &
1458 1.45.8.2 bouyer PG_NEEDCOMMIT) != 0) {
1459 1.45.8.4 bouyer KASSERT((offset & (PAGE_SIZE - 1)) == 0);
1460 1.45.8.6 bouyer iobytes = MIN(PAGE_SIZE, bytes);
1461 1.45.8.4 bouyer skipbytes += iobytes;
1462 1.45.8.2 bouyer continue;
1463 1.1 cgd }
1464 1.45.8.2 bouyer
1465 1.45.8.2 bouyer /* if it's really one i/o, don't make a second buf */
1466 1.45.8.2 bouyer if (offset == origoffset && iobytes == bytes) {
1467 1.45.8.2 bouyer bp = mbp;
1468 1.45.8.2 bouyer } else {
1469 1.45.8.2 bouyer s = splbio();
1470 1.45.8.2 bouyer vp->v_numoutput++;
1471 1.45.8.2 bouyer bp = pool_get(&bufpool, PR_WAITOK);
1472 1.45.8.2 bouyer UVMHIST_LOG(ubchist, "vp %p bp %p num now %d",
1473 1.45.8.2 bouyer vp, bp, vp->v_numoutput, 0);
1474 1.45.8.2 bouyer splx(s);
1475 1.45.8.2 bouyer bp->b_data = (char *)kva + (offset - origoffset);
1476 1.45.8.2 bouyer bp->b_resid = bp->b_bcount = iobytes;
1477 1.45.8.2 bouyer bp->b_flags = B_BUSY|B_WRITE|B_CALL|B_ASYNC;
1478 1.45.8.2 bouyer bp->b_iodone = uvm_aio_biodone1;
1479 1.45.8.2 bouyer bp->b_vp = vp;
1480 1.45.8.6 bouyer bp->b_proc = NULL; /* XXXUBC */
1481 1.45.8.2 bouyer LIST_INIT(&bp->b_dep);
1482 1.45.8.2 bouyer }
1483 1.45.8.2 bouyer bp->b_private = mbp;
1484 1.45.8.2 bouyer bp->b_lblkno = bp->b_blkno = (daddr_t)(offset >> DEV_BSHIFT);
1485 1.45.8.2 bouyer UVMHIST_LOG(ubchist, "bp %p numout %d",
1486 1.45.8.2 bouyer bp, vp->v_numoutput,0,0);
1487 1.45.8.2 bouyer VOP_STRATEGY(bp);
1488 1.1 cgd }
1489 1.45.8.2 bouyer if (skipbytes) {
1490 1.45.8.2 bouyer UVMHIST_LOG(ubchist, "skipbytes %d", bytes, 0,0,0);
1491 1.45.8.2 bouyer s = splbio();
1492 1.45.8.2 bouyer mbp->b_resid -= skipbytes;
1493 1.45.8.2 bouyer if (mbp->b_resid == 0) {
1494 1.45.8.2 bouyer biodone(mbp);
1495 1.45.8.2 bouyer }
1496 1.45.8.2 bouyer splx(s);
1497 1.45.8.2 bouyer }
1498 1.45.8.2 bouyer if (async) {
1499 1.45.8.7 bouyer return 0;
1500 1.45.8.7 bouyer }
1501 1.45.8.7 bouyer if (bp != NULL) {
1502 1.45.8.7 bouyer error = biowait(mbp);
1503 1.45.8.2 bouyer }
1504 1.45.8.2 bouyer
1505 1.45.8.2 bouyer s = splbio();
1506 1.45.8.2 bouyer vwakeup(mbp);
1507 1.45.8.2 bouyer pool_put(&bufpool, mbp);
1508 1.45.8.2 bouyer splx(s);
1509 1.45.8.2 bouyer
1510 1.45.8.2 bouyer uvm_pagermapout(kva, ap->a_count);
1511 1.45.8.2 bouyer if (error || !v3) {
1512 1.45.8.2 bouyer UVMHIST_LOG(ubchist, "returning error %d", error, 0,0,0);
1513 1.45.8.2 bouyer return error;
1514 1.45.8.2 bouyer }
1515 1.45.8.2 bouyer
1516 1.45.8.2 bouyer /*
1517 1.45.8.2 bouyer * for a weak put, mark the range as "to be committed"
1518 1.45.8.2 bouyer * and mark the pages read-only so that we will be notified
1519 1.45.8.2 bouyer * to remove the pages from the "to be committed" range
1520 1.45.8.2 bouyer * if they are made dirty again.
1521 1.45.8.2 bouyer * for a strong put, commit the pages and remove them from the
1522 1.45.8.2 bouyer * "to be committed" range. also, mark them as writable
1523 1.45.8.2 bouyer * and not cleanable with just a commit.
1524 1.45.8.2 bouyer */
1525 1.45.8.2 bouyer
1526 1.45.8.2 bouyer lockmgr(&np->n_commitlock, LK_EXCLUSIVE, NULL);
1527 1.45.8.2 bouyer if (weak) {
1528 1.45.8.2 bouyer nfs_add_tobecommitted_range(vp, origoffset,
1529 1.45.8.2 bouyer npages << PAGE_SHIFT);
1530 1.45.8.2 bouyer for (i = 0; i < npages; i++) {
1531 1.45.8.2 bouyer pgs[i]->flags |= PG_NEEDCOMMIT|PG_RDONLY;
1532 1.45.8.2 bouyer }
1533 1.45.8.2 bouyer } else {
1534 1.45.8.2 bouyer commitoff = origoffset;
1535 1.45.8.2 bouyer commitbytes = npages << PAGE_SHIFT;
1536 1.45.8.2 bouyer commit:
1537 1.45.8.2 bouyer error = nfs_commit(vp, commitoff, commitbytes, curproc);
1538 1.45.8.2 bouyer nfs_del_tobecommitted_range(vp, commitoff, commitbytes);
1539 1.45.8.2 bouyer committed:
1540 1.45.8.2 bouyer for (i = 0; i < npages; i++) {
1541 1.45.8.2 bouyer pgs[i]->flags &= ~(PG_NEEDCOMMIT|PG_RDONLY);
1542 1.45.8.2 bouyer }
1543 1.45.8.2 bouyer }
1544 1.45.8.2 bouyer lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
1545 1.45.8.2 bouyer return error;
1546 1.1 cgd }
1547