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