nfs_bio.c revision 1.69 1 1.69 chs /* $NetBSD: nfs_bio.c,v 1.69 2001/09/15 20:36:39 chs Exp $ */
2 1.15 cgd
3 1.1 cgd /*
4 1.12 mycroft * Copyright (c) 1989, 1993
5 1.12 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Rick Macklem at The University of Guelph.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.1 cgd * 3. All advertising materials mentioning features or use of this software
19 1.1 cgd * must display the following acknowledgement:
20 1.1 cgd * This product includes software developed by the University of
21 1.1 cgd * California, Berkeley and its contributors.
22 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
23 1.1 cgd * may be used to endorse or promote products derived from this software
24 1.1 cgd * without specific prior written permission.
25 1.1 cgd *
26 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 cgd * SUCH DAMAGE.
37 1.1 cgd *
38 1.24 fvdl * @(#)nfs_bio.c 8.9 (Berkeley) 3/30/95
39 1.1 cgd */
40 1.1 cgd
41 1.51 bjh21 #include "opt_nfs.h"
42 1.54 chs #include "opt_ddb.h"
43 1.51 bjh21
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.54 chs #include <sys/malloc.h>
56 1.1 cgd
57 1.41 mrg #include <uvm/uvm_extern.h>
58 1.54 chs #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.48 augustss struct vnode *vp;
78 1.48 augustss struct uio *uio;
79 1.34 fvdl int ioflag, cflag;
80 1.1 cgd struct ucred *cred;
81 1.1 cgd {
82 1.48 augustss struct nfsnode *np = VTONFS(vp);
83 1.54 chs 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.54 chs 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.54 chs 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.51 bjh21 #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.51 bjh21 #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.54 chs
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.54 chs
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.54 chs
165 1.54 chs /*
166 1.54 chs * update the cached read creds for this node.
167 1.54 chs */
168 1.54 chs
169 1.54 chs if (np->n_rcred) {
170 1.54 chs crfree(np->n_rcred);
171 1.54 chs }
172 1.54 chs np->n_rcred = cred;
173 1.54 chs crhold(cred);
174 1.54 chs
175 1.1 cgd do {
176 1.53 bjh21 #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.53 bjh21 #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.54 chs 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.54 chs error = 0;
231 1.66 chs if (uio->uio_offset >= np->n_size) {
232 1.66 chs break;
233 1.66 chs }
234 1.54 chs while (uio->uio_resid > 0) {
235 1.54 chs void *win;
236 1.61 chs vsize_t bytelen = MIN(np->n_size - uio->uio_offset,
237 1.54 chs uio->uio_resid);
238 1.54 chs
239 1.54 chs if (bytelen == 0)
240 1.54 chs break;
241 1.69 chs win = ubc_alloc(&vp->v_uobj, uio->uio_offset,
242 1.54 chs &bytelen, UBC_READ);
243 1.54 chs error = uiomove(win, bytelen, uio);
244 1.54 chs ubc_release(win, 0);
245 1.54 chs if (error) {
246 1.54 chs break;
247 1.12 mycroft }
248 1.12 mycroft }
249 1.54 chs n = 0;
250 1.54 chs break;
251 1.12 mycroft
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.54 chs 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.63 chs 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.54 chs 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.65 chs 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.65 chs 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.65 chs 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.65 chs 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.54 chs 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.54 chs }
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.48 augustss struct uio *uio = ap->a_uio;
511 1.1 cgd struct proc *p = uio->uio_procp;
512 1.48 augustss struct vnode *vp = ap->a_vp;
513 1.12 mycroft struct nfsnode *np = VTONFS(vp);
514 1.48 augustss 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.69 chs void *win;
519 1.69 chs voff_t oldoff, origoff;
520 1.69 chs vsize_t bytelen;
521 1.54 chs int error = 0, iomode, must_commit;
522 1.1 cgd
523 1.1 cgd #ifdef DIAGNOSTIC
524 1.1 cgd if (uio->uio_rw != UIO_WRITE)
525 1.1 cgd panic("nfs_write mode");
526 1.1 cgd if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
527 1.1 cgd panic("nfs_write proc");
528 1.1 cgd #endif
529 1.1 cgd if (vp->v_type != VREG)
530 1.1 cgd return (EIO);
531 1.12 mycroft if (np->n_flag & NWRITEERR) {
532 1.12 mycroft np->n_flag &= ~NWRITEERR;
533 1.12 mycroft return (np->n_error);
534 1.12 mycroft }
535 1.51 bjh21 #ifndef NFS_V2_ONLY
536 1.34 fvdl if ((nmp->nm_flag & NFSMNT_NFSV3) &&
537 1.34 fvdl !(nmp->nm_iflag & NFSMNT_GOTFSINFO))
538 1.24 fvdl (void)nfs_fsinfo(nmp, vp, cred, p);
539 1.51 bjh21 #endif
540 1.1 cgd if (ioflag & (IO_APPEND | IO_SYNC)) {
541 1.1 cgd if (np->n_flag & NMODIFIED) {
542 1.12 mycroft np->n_attrstamp = 0;
543 1.23 christos error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
544 1.23 christos if (error)
545 1.12 mycroft return (error);
546 1.1 cgd }
547 1.1 cgd if (ioflag & IO_APPEND) {
548 1.1 cgd np->n_attrstamp = 0;
549 1.23 christos error = VOP_GETATTR(vp, &vattr, cred, p);
550 1.23 christos if (error)
551 1.1 cgd return (error);
552 1.1 cgd uio->uio_offset = np->n_size;
553 1.1 cgd }
554 1.1 cgd }
555 1.1 cgd if (uio->uio_offset < 0)
556 1.1 cgd return (EINVAL);
557 1.33 fvdl if ((uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
558 1.33 fvdl return (EFBIG);
559 1.1 cgd if (uio->uio_resid == 0)
560 1.1 cgd return (0);
561 1.1 cgd /*
562 1.1 cgd * Maybe this should be above the vnode op call, but so long as
563 1.1 cgd * file servers have no limits, i don't think it matters
564 1.1 cgd */
565 1.12 mycroft if (p && uio->uio_offset + uio->uio_resid >
566 1.1 cgd p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
567 1.1 cgd psignal(p, SIGXFSZ);
568 1.1 cgd return (EFBIG);
569 1.1 cgd }
570 1.54 chs
571 1.1 cgd /*
572 1.54 chs * update the cached write creds for this node.
573 1.1 cgd */
574 1.54 chs
575 1.54 chs if (np->n_wcred) {
576 1.54 chs crfree(np->n_wcred);
577 1.54 chs }
578 1.54 chs np->n_wcred = cred;
579 1.54 chs crhold(cred);
580 1.54 chs
581 1.54 chs if ((np->n_flag & NQNFSNONCACHE) && uio->uio_iovcnt == 1) {
582 1.54 chs iomode = NFSV3WRITE_FILESYNC;
583 1.54 chs error = nfs_writerpc(vp, uio, &iomode, &must_commit);
584 1.54 chs if (must_commit)
585 1.54 chs nfs_clearcommit(vp->v_mount);
586 1.54 chs return (error);
587 1.54 chs }
588 1.54 chs
589 1.69 chs origoff = uio->uio_offset;
590 1.1 cgd do {
591 1.69 chs oldoff = uio->uio_offset;
592 1.69 chs bytelen = uio->uio_resid;
593 1.12 mycroft
594 1.53 bjh21 #ifndef NFS_V2_ONLY
595 1.12 mycroft /*
596 1.12 mycroft * Check for a valid write lease.
597 1.12 mycroft */
598 1.12 mycroft if ((nmp->nm_flag & NFSMNT_NQNFS) &&
599 1.24 fvdl NQNFS_CKINVALID(vp, np, ND_WRITE)) {
600 1.12 mycroft do {
601 1.24 fvdl error = nqnfs_getlease(vp, ND_WRITE, cred, p);
602 1.12 mycroft } while (error == NQNFS_EXPIRED);
603 1.12 mycroft if (error)
604 1.12 mycroft return (error);
605 1.12 mycroft if (np->n_lrev != np->n_brev ||
606 1.12 mycroft (np->n_flag & NQNFSNONCACHE)) {
607 1.23 christos error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
608 1.23 christos if (error)
609 1.12 mycroft return (error);
610 1.12 mycroft np->n_brev = np->n_lrev;
611 1.12 mycroft }
612 1.12 mycroft }
613 1.53 bjh21 #endif
614 1.1 cgd nfsstats.biocache_writes++;
615 1.54 chs
616 1.12 mycroft np->n_flag |= NMODIFIED;
617 1.54 chs if (np->n_size < uio->uio_offset + bytelen) {
618 1.54 chs np->n_size = uio->uio_offset + bytelen;
619 1.12 mycroft }
620 1.69 chs if ((uio->uio_offset & PAGE_MASK) == 0 &&
621 1.69 chs ((uio->uio_offset + bytelen) & PAGE_MASK) == 0) {
622 1.69 chs win = ubc_alloc(&vp->v_uobj, uio->uio_offset, &bytelen,
623 1.69 chs UBC_WRITE | UBC_FAULTBUSY);
624 1.69 chs } else {
625 1.69 chs win = ubc_alloc(&vp->v_uobj, uio->uio_offset, &bytelen,
626 1.69 chs UBC_WRITE);
627 1.69 chs }
628 1.54 chs error = uiomove(win, bytelen, uio);
629 1.54 chs ubc_release(win, 0);
630 1.69 chs if (error) {
631 1.69 chs break;
632 1.69 chs }
633 1.69 chs
634 1.69 chs /*
635 1.69 chs * update UVM's notion of the size now that we've
636 1.69 chs * copied the data into the vnode's pages.
637 1.69 chs */
638 1.69 chs
639 1.69 chs if (vp->v_size < uio->uio_offset) {
640 1.69 chs uvm_vnp_setsize(vp, uio->uio_offset);
641 1.69 chs }
642 1.69 chs
643 1.69 chs if ((oldoff & ~(nmp->nm_wsize - 1)) !=
644 1.54 chs (uio->uio_offset & ~(nmp->nm_wsize - 1))) {
645 1.69 chs simple_lock(&vp->v_uobj.vmobjlock);
646 1.69 chs error = (vp->v_uobj.pgops->pgo_put)(&vp->v_uobj,
647 1.69 chs trunc_page(oldoff & ~(nmp->nm_wsize - 1)),
648 1.69 chs round_page((uio->uio_offset + nmp->nm_wsize - 1) &
649 1.69 chs ~(nmp->nm_wsize - 1)),
650 1.54 chs PGO_CLEANIT|PGO_WEAK);
651 1.52 fvdl }
652 1.54 chs } while (uio->uio_resid > 0);
653 1.69 chs if ((np->n_flag & NQNFSNONCACHE) || (ioflag & IO_SYNC)) {
654 1.69 chs simple_lock(&vp->v_uobj.vmobjlock);
655 1.69 chs error = (vp->v_uobj.pgops->pgo_put)(&vp->v_uobj,
656 1.69 chs trunc_page(origoff & ~(nmp->nm_wsize - 1)),
657 1.69 chs round_page((uio->uio_offset + nmp->nm_wsize - 1) &
658 1.69 chs ~(nmp->nm_wsize - 1)),
659 1.69 chs PGO_CLEANIT|PGO_SYNCIO);
660 1.69 chs }
661 1.54 chs return error;
662 1.12 mycroft }
663 1.12 mycroft
664 1.12 mycroft /*
665 1.12 mycroft * Get an nfs cache block.
666 1.12 mycroft * Allocate a new one if the block isn't currently in the cache
667 1.12 mycroft * and return the block marked busy. If the calling process is
668 1.12 mycroft * interrupted by a signal for an interruptible mount point, return
669 1.12 mycroft * NULL.
670 1.12 mycroft */
671 1.12 mycroft struct buf *
672 1.12 mycroft nfs_getcacheblk(vp, bn, size, p)
673 1.12 mycroft struct vnode *vp;
674 1.12 mycroft daddr_t bn;
675 1.12 mycroft int size;
676 1.12 mycroft struct proc *p;
677 1.12 mycroft {
678 1.48 augustss struct buf *bp;
679 1.12 mycroft struct nfsmount *nmp = VFSTONFS(vp->v_mount);
680 1.12 mycroft
681 1.12 mycroft if (nmp->nm_flag & NFSMNT_INT) {
682 1.12 mycroft bp = getblk(vp, bn, size, PCATCH, 0);
683 1.54 chs while (bp == NULL) {
684 1.54 chs if (nfs_sigintr(nmp, NULL, p))
685 1.54 chs return (NULL);
686 1.12 mycroft bp = getblk(vp, bn, size, 0, 2 * hz);
687 1.12 mycroft }
688 1.12 mycroft } else
689 1.12 mycroft bp = getblk(vp, bn, size, 0, 0);
690 1.12 mycroft return (bp);
691 1.12 mycroft }
692 1.12 mycroft
693 1.12 mycroft /*
694 1.12 mycroft * Flush and invalidate all dirty buffers. If another process is already
695 1.12 mycroft * doing the flush, just wait for completion.
696 1.12 mycroft */
697 1.23 christos int
698 1.12 mycroft nfs_vinvalbuf(vp, flags, cred, p, intrflg)
699 1.12 mycroft struct vnode *vp;
700 1.12 mycroft int flags;
701 1.12 mycroft struct ucred *cred;
702 1.12 mycroft struct proc *p;
703 1.12 mycroft int intrflg;
704 1.12 mycroft {
705 1.48 augustss struct nfsnode *np = VTONFS(vp);
706 1.12 mycroft struct nfsmount *nmp = VFSTONFS(vp->v_mount);
707 1.12 mycroft int error = 0, slpflag, slptimeo;
708 1.12 mycroft
709 1.12 mycroft if ((nmp->nm_flag & NFSMNT_INT) == 0)
710 1.12 mycroft intrflg = 0;
711 1.12 mycroft if (intrflg) {
712 1.12 mycroft slpflag = PCATCH;
713 1.12 mycroft slptimeo = 2 * hz;
714 1.12 mycroft } else {
715 1.12 mycroft slpflag = 0;
716 1.12 mycroft slptimeo = 0;
717 1.12 mycroft }
718 1.12 mycroft /*
719 1.12 mycroft * First wait for any other process doing a flush to complete.
720 1.12 mycroft */
721 1.12 mycroft while (np->n_flag & NFLUSHINPROG) {
722 1.12 mycroft np->n_flag |= NFLUSHWANT;
723 1.12 mycroft error = tsleep((caddr_t)&np->n_flag, PRIBIO + 2, "nfsvinval",
724 1.12 mycroft slptimeo);
725 1.54 chs if (error && intrflg && nfs_sigintr(nmp, NULL, p))
726 1.12 mycroft return (EINTR);
727 1.12 mycroft }
728 1.12 mycroft
729 1.12 mycroft /*
730 1.12 mycroft * Now, flush as required.
731 1.12 mycroft */
732 1.12 mycroft np->n_flag |= NFLUSHINPROG;
733 1.12 mycroft error = vinvalbuf(vp, flags, cred, p, slpflag, 0);
734 1.12 mycroft while (error) {
735 1.54 chs if (intrflg && nfs_sigintr(nmp, NULL, p)) {
736 1.12 mycroft np->n_flag &= ~NFLUSHINPROG;
737 1.12 mycroft if (np->n_flag & NFLUSHWANT) {
738 1.12 mycroft np->n_flag &= ~NFLUSHWANT;
739 1.12 mycroft wakeup((caddr_t)&np->n_flag);
740 1.12 mycroft }
741 1.12 mycroft return (EINTR);
742 1.12 mycroft }
743 1.12 mycroft error = vinvalbuf(vp, flags, cred, p, 0, slptimeo);
744 1.12 mycroft }
745 1.12 mycroft np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
746 1.12 mycroft if (np->n_flag & NFLUSHWANT) {
747 1.12 mycroft np->n_flag &= ~NFLUSHWANT;
748 1.12 mycroft wakeup((caddr_t)&np->n_flag);
749 1.12 mycroft }
750 1.12 mycroft return (0);
751 1.12 mycroft }
752 1.12 mycroft
753 1.12 mycroft /*
754 1.12 mycroft * Initiate asynchronous I/O. Return an error if no nfsiods are available.
755 1.12 mycroft * This is mainly to avoid queueing async I/O requests when the nfsiods
756 1.12 mycroft * are all hung on a dead server.
757 1.12 mycroft */
758 1.69 chs
759 1.23 christos int
760 1.54 chs nfs_asyncio(bp)
761 1.48 augustss struct buf *bp;
762 1.12 mycroft {
763 1.48 augustss int i;
764 1.48 augustss struct nfsmount *nmp;
765 1.30 thorpej int gotiod, slpflag = 0, slptimeo = 0, error;
766 1.12 mycroft
767 1.12 mycroft if (nfs_numasync == 0)
768 1.12 mycroft return (EIO);
769 1.30 thorpej
770 1.30 thorpej
771 1.30 thorpej nmp = VFSTONFS(bp->b_vp->v_mount);
772 1.30 thorpej again:
773 1.30 thorpej if (nmp->nm_flag & NFSMNT_INT)
774 1.30 thorpej slpflag = PCATCH;
775 1.30 thorpej gotiod = FALSE;
776 1.30 thorpej
777 1.30 thorpej /*
778 1.30 thorpej * Find a free iod to process this request.
779 1.30 thorpej */
780 1.30 thorpej
781 1.12 mycroft for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
782 1.30 thorpej if (nfs_iodwant[i]) {
783 1.30 thorpej /*
784 1.30 thorpej * Found one, so wake it up and tell it which
785 1.30 thorpej * mount to process.
786 1.30 thorpej */
787 1.54 chs nfs_iodwant[i] = NULL;
788 1.30 thorpej nfs_iodmount[i] = nmp;
789 1.30 thorpej nmp->nm_bufqiods++;
790 1.30 thorpej wakeup((caddr_t)&nfs_iodwant[i]);
791 1.30 thorpej gotiod = TRUE;
792 1.31 fvdl break;
793 1.30 thorpej }
794 1.30 thorpej /*
795 1.30 thorpej * If none are free, we may already have an iod working on this mount
796 1.30 thorpej * point. If so, it will process our request.
797 1.30 thorpej */
798 1.30 thorpej if (!gotiod && nmp->nm_bufqiods > 0)
799 1.30 thorpej gotiod = TRUE;
800 1.30 thorpej
801 1.30 thorpej /*
802 1.30 thorpej * If we have an iod which can process the request, then queue
803 1.30 thorpej * the buffer.
804 1.30 thorpej */
805 1.30 thorpej if (gotiod) {
806 1.30 thorpej /*
807 1.30 thorpej * Ensure that the queue never grows too large.
808 1.30 thorpej */
809 1.30 thorpej while (nmp->nm_bufqlen >= 2*nfs_numasync) {
810 1.30 thorpej nmp->nm_bufqwant = TRUE;
811 1.30 thorpej error = tsleep(&nmp->nm_bufq, slpflag | PRIBIO,
812 1.30 thorpej "nfsaio", slptimeo);
813 1.30 thorpej if (error) {
814 1.30 thorpej if (nfs_sigintr(nmp, NULL, bp->b_proc))
815 1.30 thorpej return (EINTR);
816 1.30 thorpej if (slpflag == PCATCH) {
817 1.30 thorpej slpflag = 0;
818 1.30 thorpej slptimeo = 2 * hz;
819 1.30 thorpej }
820 1.30 thorpej }
821 1.30 thorpej /*
822 1.30 thorpej * We might have lost our iod while sleeping,
823 1.30 thorpej * so check and loop if nescessary.
824 1.30 thorpej */
825 1.30 thorpej if (nmp->nm_bufqiods == 0)
826 1.30 thorpej goto again;
827 1.30 thorpej }
828 1.30 thorpej TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist);
829 1.30 thorpej nmp->nm_bufqlen++;
830 1.12 mycroft return (0);
831 1.12 mycroft }
832 1.24 fvdl
833 1.24 fvdl /*
834 1.30 thorpej * All the iods are busy on other mounts, so return EIO to
835 1.30 thorpej * force the caller to process the i/o synchronously.
836 1.24 fvdl */
837 1.30 thorpej return (EIO);
838 1.12 mycroft }
839 1.12 mycroft
840 1.12 mycroft /*
841 1.12 mycroft * Do an I/O operation to/from a cache block. This may be called
842 1.12 mycroft * synchronously or from an nfsiod.
843 1.12 mycroft */
844 1.12 mycroft int
845 1.54 chs nfs_doio(bp, p)
846 1.48 augustss struct buf *bp;
847 1.12 mycroft struct proc *p;
848 1.12 mycroft {
849 1.48 augustss struct uio *uiop;
850 1.48 augustss struct vnode *vp;
851 1.12 mycroft struct nfsnode *np;
852 1.12 mycroft struct nfsmount *nmp;
853 1.54 chs int error = 0, diff, len, iomode, must_commit = 0;
854 1.12 mycroft struct uio uio;
855 1.12 mycroft struct iovec io;
856 1.12 mycroft
857 1.12 mycroft vp = bp->b_vp;
858 1.12 mycroft np = VTONFS(vp);
859 1.12 mycroft nmp = VFSTONFS(vp->v_mount);
860 1.12 mycroft uiop = &uio;
861 1.12 mycroft uiop->uio_iov = &io;
862 1.12 mycroft uiop->uio_iovcnt = 1;
863 1.12 mycroft uiop->uio_segflg = UIO_SYSSPACE;
864 1.12 mycroft uiop->uio_procp = p;
865 1.12 mycroft
866 1.12 mycroft /*
867 1.14 pk * Historically, paging was done with physio, but no more...
868 1.12 mycroft */
869 1.14 pk if (bp->b_flags & B_PHYS) {
870 1.14 pk /*
871 1.14 pk * ...though reading /dev/drum still gets us here.
872 1.14 pk */
873 1.14 pk io.iov_len = uiop->uio_resid = bp->b_bcount;
874 1.14 pk /* mapping was done by vmapbuf() */
875 1.14 pk io.iov_base = bp->b_data;
876 1.54 chs uiop->uio_offset = ((off_t)bp->b_blkno) << DEV_BSHIFT;
877 1.14 pk if (bp->b_flags & B_READ) {
878 1.14 pk uiop->uio_rw = UIO_READ;
879 1.14 pk nfsstats.read_physios++;
880 1.54 chs error = nfs_readrpc(vp, uiop);
881 1.14 pk } else {
882 1.24 fvdl iomode = NFSV3WRITE_DATASYNC;
883 1.14 pk uiop->uio_rw = UIO_WRITE;
884 1.14 pk nfsstats.write_physios++;
885 1.54 chs error = nfs_writerpc(vp, uiop, &iomode, &must_commit);
886 1.14 pk }
887 1.14 pk if (error) {
888 1.14 pk bp->b_flags |= B_ERROR;
889 1.14 pk bp->b_error = error;
890 1.14 pk }
891 1.14 pk } else if (bp->b_flags & B_READ) {
892 1.12 mycroft io.iov_len = uiop->uio_resid = bp->b_bcount;
893 1.12 mycroft io.iov_base = bp->b_data;
894 1.12 mycroft uiop->uio_rw = UIO_READ;
895 1.12 mycroft switch (vp->v_type) {
896 1.12 mycroft case VREG:
897 1.54 chs uiop->uio_offset = ((off_t)bp->b_blkno) << DEV_BSHIFT;
898 1.12 mycroft nfsstats.read_bios++;
899 1.54 chs error = nfs_readrpc(vp, uiop);
900 1.54 chs if (!error && uiop->uio_resid) {
901 1.54 chs
902 1.12 mycroft /*
903 1.12 mycroft * If len > 0, there is a hole in the file and
904 1.12 mycroft * no writes after the hole have been pushed to
905 1.12 mycroft * the server yet.
906 1.12 mycroft * Just zero fill the rest of the valid area.
907 1.12 mycroft */
908 1.54 chs
909 1.12 mycroft diff = bp->b_bcount - uiop->uio_resid;
910 1.54 chs len = np->n_size - ((((off_t)bp->b_blkno) << DEV_BSHIFT)
911 1.12 mycroft + diff);
912 1.12 mycroft if (len > 0) {
913 1.63 chs len = MIN(len, uiop->uio_resid);
914 1.54 chs memset((char *)bp->b_data + diff, 0, len);
915 1.54 chs }
916 1.12 mycroft }
917 1.12 mycroft if (p && (vp->v_flag & VTEXT) &&
918 1.12 mycroft (((nmp->nm_flag & NFSMNT_NQNFS) &&
919 1.24 fvdl NQNFS_CKINVALID(vp, np, ND_READ) &&
920 1.12 mycroft np->n_lrev != np->n_brev) ||
921 1.12 mycroft (!(nmp->nm_flag & NFSMNT_NQNFS) &&
922 1.35 fvdl np->n_mtime != np->n_vattr->va_mtime.tv_sec))) {
923 1.54 chs uprintf("Process killed due to "
924 1.54 chs "text file modification\n");
925 1.12 mycroft psignal(p, SIGKILL);
926 1.13 mycroft p->p_holdcnt++;
927 1.12 mycroft }
928 1.12 mycroft break;
929 1.12 mycroft case VLNK:
930 1.24 fvdl uiop->uio_offset = (off_t)0;
931 1.12 mycroft nfsstats.readlink_bios++;
932 1.54 chs error = nfs_readlinkrpc(vp, uiop, curproc->p_ucred);
933 1.12 mycroft break;
934 1.12 mycroft case VDIR:
935 1.12 mycroft nfsstats.readdir_bios++;
936 1.34 fvdl uiop->uio_offset = bp->b_dcookie;
937 1.24 fvdl if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
938 1.54 chs error = nfs_readdirplusrpc(vp, uiop, curproc->p_ucred);
939 1.24 fvdl if (error == NFSERR_NOTSUPP)
940 1.24 fvdl nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
941 1.24 fvdl }
942 1.24 fvdl if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
943 1.54 chs error = nfs_readdirrpc(vp, uiop, curproc->p_ucred);
944 1.34 fvdl if (!error) {
945 1.34 fvdl bp->b_dcookie = uiop->uio_offset;
946 1.34 fvdl }
947 1.24 fvdl break;
948 1.24 fvdl default:
949 1.29 christos printf("nfs_doio: type %x unexpected\n",vp->v_type);
950 1.12 mycroft break;
951 1.54 chs }
952 1.12 mycroft if (error) {
953 1.12 mycroft bp->b_flags |= B_ERROR;
954 1.12 mycroft bp->b_error = error;
955 1.12 mycroft }
956 1.12 mycroft } else {
957 1.52 fvdl /*
958 1.52 fvdl * If B_NEEDCOMMIT is set, a commit rpc may do the trick. If not
959 1.52 fvdl * an actual write will have to be scheduled.
960 1.52 fvdl */
961 1.54 chs
962 1.54 chs io.iov_base = bp->b_data;
963 1.54 chs io.iov_len = uiop->uio_resid = bp->b_bcount;
964 1.54 chs uiop->uio_offset = (((off_t)bp->b_blkno) << DEV_BSHIFT);
965 1.54 chs uiop->uio_rw = UIO_WRITE;
966 1.54 chs nfsstats.write_bios++;
967 1.54 chs iomode = NFSV3WRITE_UNSTABLE;
968 1.54 chs error = nfs_writerpc(vp, uiop, &iomode, &must_commit);
969 1.54 chs }
970 1.54 chs bp->b_resid = uiop->uio_resid;
971 1.54 chs if (must_commit)
972 1.54 chs nfs_clearcommit(vp->v_mount);
973 1.54 chs biodone(bp);
974 1.54 chs return (error);
975 1.54 chs }
976 1.54 chs
977 1.54 chs /*
978 1.54 chs * Vnode op for VM getpages.
979 1.54 chs */
980 1.69 chs
981 1.54 chs int
982 1.54 chs nfs_getpages(v)
983 1.54 chs void *v;
984 1.54 chs {
985 1.54 chs struct vop_getpages_args /* {
986 1.54 chs struct vnode *a_vp;
987 1.54 chs voff_t a_offset;
988 1.67 chs struct vm_page **a_m;
989 1.54 chs int *a_count;
990 1.54 chs int a_centeridx;
991 1.54 chs vm_prot_t a_access_type;
992 1.54 chs int a_advice;
993 1.54 chs int a_flags;
994 1.54 chs } */ *ap = v;
995 1.54 chs
996 1.54 chs struct vnode *vp = ap->a_vp;
997 1.69 chs struct uvm_object *uobj = &vp->v_uobj;
998 1.54 chs struct nfsnode *np = VTONFS(vp);
999 1.69 chs struct vm_page *pg, **pgs;
1000 1.69 chs off_t origoffset;
1001 1.69 chs int i, error, npages;
1002 1.54 chs boolean_t v3 = NFS_ISV3(vp);
1003 1.54 chs boolean_t write = (ap->a_access_type & VM_PROT_WRITE) != 0;
1004 1.54 chs UVMHIST_FUNC("nfs_getpages"); UVMHIST_CALLED(ubchist);
1005 1.54 chs
1006 1.54 chs /*
1007 1.54 chs * update the cached read creds for this node.
1008 1.54 chs */
1009 1.54 chs
1010 1.54 chs if (np->n_rcred) {
1011 1.54 chs crfree(np->n_rcred);
1012 1.54 chs }
1013 1.54 chs np->n_rcred = curproc->p_ucred;
1014 1.54 chs crhold(np->n_rcred);
1015 1.54 chs
1016 1.54 chs /*
1017 1.69 chs * call the genfs code to get the pages.
1018 1.54 chs */
1019 1.54 chs
1020 1.69 chs npages = *ap->a_count;
1021 1.69 chs error = genfs_getpages(v);
1022 1.69 chs if (error || !write || !v3) {
1023 1.69 chs return error;
1024 1.54 chs }
1025 1.54 chs
1026 1.54 chs /*
1027 1.69 chs * this is a write fault, update the commit info.
1028 1.54 chs */
1029 1.54 chs
1030 1.69 chs origoffset = ap->a_offset;
1031 1.69 chs pgs = ap->a_m;
1032 1.54 chs
1033 1.69 chs lockmgr(&np->n_commitlock, LK_EXCLUSIVE, NULL);
1034 1.69 chs nfs_del_committed_range(vp, origoffset, npages);
1035 1.69 chs nfs_del_tobecommitted_range(vp, origoffset, npages);
1036 1.54 chs simple_lock(&uobj->vmobjlock);
1037 1.54 chs for (i = 0; i < npages; i++) {
1038 1.69 chs pg = pgs[i];
1039 1.69 chs if (pg == NULL || pg == PGO_DONTCARE) {
1040 1.54 chs continue;
1041 1.54 chs }
1042 1.69 chs pg->flags &= ~(PG_NEEDCOMMIT|PG_RDONLY);
1043 1.54 chs }
1044 1.54 chs simple_unlock(&uobj->vmobjlock);
1045 1.69 chs lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
1046 1.61 chs return 0;
1047 1.54 chs }
1048 1.54 chs
1049 1.54 chs int
1050 1.69 chs nfs_gop_write(struct vnode *vp, struct vm_page **pgs, int npages, int flags)
1051 1.54 chs {
1052 1.69 chs struct uvm_object *uobj = &vp->v_uobj;
1053 1.54 chs struct nfsnode *np = VTONFS(vp);
1054 1.69 chs off_t origoffset, commitoff;
1055 1.54 chs uint32_t commitbytes;
1056 1.69 chs int error, i;
1057 1.69 chs int bytes;
1058 1.54 chs boolean_t v3 = NFS_ISV3(vp);
1059 1.69 chs boolean_t weak = flags & PGO_WEAK;
1060 1.69 chs UVMHIST_FUNC("nfs_gop_write"); UVMHIST_CALLED(ubchist);
1061 1.54 chs
1062 1.69 chs /* XXX for now, skip the v3 stuff. */
1063 1.69 chs v3 = FALSE;
1064 1.54 chs
1065 1.54 chs /*
1066 1.69 chs * for NFSv2, just write normally.
1067 1.54 chs */
1068 1.52 fvdl
1069 1.69 chs if (!v3) {
1070 1.69 chs return genfs_gop_write(vp, pgs, npages, flags);
1071 1.54 chs }
1072 1.54 chs
1073 1.54 chs /*
1074 1.69 chs * for NFSv3, use delayed writes and the "commit" operation
1075 1.69 chs * to avoid sync writes.
1076 1.54 chs */
1077 1.54 chs
1078 1.69 chs origoffset = pgs[0]->offset;
1079 1.69 chs bytes = npages << PAGE_SHIFT;
1080 1.69 chs lockmgr(&np->n_commitlock, LK_EXCLUSIVE, NULL);
1081 1.69 chs if (nfs_in_committed_range(vp, origoffset, bytes)) {
1082 1.69 chs goto committed;
1083 1.69 chs }
1084 1.69 chs if (nfs_in_tobecommitted_range(vp, origoffset, bytes)) {
1085 1.69 chs if (weak) {
1086 1.69 chs lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
1087 1.69 chs return 0;
1088 1.69 chs } else {
1089 1.69 chs commitoff = np->n_pushlo;
1090 1.69 chs commitbytes = (uint32_t)(np->n_pushhi - np->n_pushlo);
1091 1.69 chs goto commit;
1092 1.54 chs }
1093 1.69 chs } else {
1094 1.69 chs commitoff = origoffset;
1095 1.69 chs commitbytes = npages << PAGE_SHIFT;
1096 1.69 chs }
1097 1.69 chs simple_lock(&uobj->vmobjlock);
1098 1.69 chs for (i = 0; i < npages; i++) {
1099 1.69 chs pgs[i]->flags |= PG_NEEDCOMMIT|PG_RDONLY;
1100 1.69 chs pgs[i]->flags &= ~PG_CLEAN;
1101 1.69 chs }
1102 1.69 chs simple_unlock(&uobj->vmobjlock);
1103 1.69 chs lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
1104 1.69 chs error = genfs_gop_write(vp, pgs, npages, flags);
1105 1.69 chs if (error) {
1106 1.54 chs return error;
1107 1.54 chs }
1108 1.54 chs lockmgr(&np->n_commitlock, LK_EXCLUSIVE, NULL);
1109 1.54 chs if (weak) {
1110 1.54 chs nfs_add_tobecommitted_range(vp, origoffset,
1111 1.69 chs npages << PAGE_SHIFT);
1112 1.54 chs } else {
1113 1.54 chs commit:
1114 1.54 chs error = nfs_commit(vp, commitoff, commitbytes, curproc);
1115 1.54 chs nfs_del_tobecommitted_range(vp, commitoff, commitbytes);
1116 1.54 chs committed:
1117 1.69 chs simple_lock(&uobj->vmobjlock);
1118 1.54 chs for (i = 0; i < npages; i++) {
1119 1.54 chs pgs[i]->flags &= ~(PG_NEEDCOMMIT|PG_RDONLY);
1120 1.1 cgd }
1121 1.69 chs simple_unlock(&uobj->vmobjlock);
1122 1.1 cgd }
1123 1.54 chs lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
1124 1.54 chs return error;
1125 1.1 cgd }
1126