nfs_bio.c revision 1.63.2.11 1 /* $NetBSD: nfs_bio.c,v 1.63.2.11 2002/04/01 07:49:05 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.11 2002/04/01 07:49:05 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->l_proc)
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 ((uio->uio_offset + bytelen) & PAGE_MASK) == 0) {
626 win = ubc_alloc(&vp->v_uobj, uio->uio_offset, &bytelen,
627 UBC_WRITE | UBC_FAULTBUSY);
628 } else {
629 win = ubc_alloc(&vp->v_uobj, uio->uio_offset, &bytelen,
630 UBC_WRITE);
631 }
632 error = uiomove(win, bytelen, uio);
633 ubc_release(win, 0);
634 if (error) {
635 break;
636 }
637
638 /*
639 * update UVM's notion of the size now that we've
640 * copied the data into the vnode's pages.
641 */
642
643 if (vp->v_size < uio->uio_offset) {
644 uvm_vnp_setsize(vp, uio->uio_offset);
645 }
646
647 if ((oldoff & ~(nmp->nm_wsize - 1)) !=
648 (uio->uio_offset & ~(nmp->nm_wsize - 1))) {
649 simple_lock(&vp->v_interlock);
650 error = VOP_PUTPAGES(vp,
651 trunc_page(oldoff & ~(nmp->nm_wsize - 1)),
652 round_page((uio->uio_offset + nmp->nm_wsize - 1) &
653 ~(nmp->nm_wsize - 1)), PGO_CLEANIT);
654 }
655 } while (uio->uio_resid > 0);
656 if ((np->n_flag & NQNFSNONCACHE) || (ioflag & IO_SYNC)) {
657 simple_lock(&vp->v_interlock);
658 error = VOP_PUTPAGES(vp,
659 trunc_page(origoff & ~(nmp->nm_wsize - 1)),
660 round_page((uio->uio_offset + nmp->nm_wsize - 1) &
661 ~(nmp->nm_wsize - 1)),
662 PGO_CLEANIT | PGO_SYNCIO);
663 }
664 return error;
665 }
666
667 /*
668 * Get an nfs cache block.
669 * Allocate a new one if the block isn't currently in the cache
670 * and return the block marked busy. If the calling process is
671 * interrupted by a signal for an interruptible mount point, return
672 * NULL.
673 */
674 struct buf *
675 nfs_getcacheblk(vp, bn, size, p)
676 struct vnode *vp;
677 daddr_t bn;
678 int size;
679 struct proc *p;
680 {
681 struct buf *bp;
682 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
683
684 if (nmp->nm_flag & NFSMNT_INT) {
685 bp = getblk(vp, bn, size, PCATCH, 0);
686 while (bp == NULL) {
687 if (nfs_sigintr(nmp, NULL, p))
688 return (NULL);
689 bp = getblk(vp, bn, size, 0, 2 * hz);
690 }
691 } else
692 bp = getblk(vp, bn, size, 0, 0);
693 return (bp);
694 }
695
696 /*
697 * Flush and invalidate all dirty buffers. If another process is already
698 * doing the flush, just wait for completion.
699 */
700 int
701 nfs_vinvalbuf(vp, flags, cred, p, intrflg)
702 struct vnode *vp;
703 int flags;
704 struct ucred *cred;
705 struct proc *p;
706 int intrflg;
707 {
708 struct nfsnode *np = VTONFS(vp);
709 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
710 int error = 0, slpflag, slptimeo;
711
712 if ((nmp->nm_flag & NFSMNT_INT) == 0)
713 intrflg = 0;
714 if (intrflg) {
715 slpflag = PCATCH;
716 slptimeo = 2 * hz;
717 } else {
718 slpflag = 0;
719 slptimeo = 0;
720 }
721 /*
722 * First wait for any other process doing a flush to complete.
723 */
724 while (np->n_flag & NFLUSHINPROG) {
725 np->n_flag |= NFLUSHWANT;
726 error = tsleep((caddr_t)&np->n_flag, PRIBIO + 2, "nfsvinval",
727 slptimeo);
728 if (error && intrflg && nfs_sigintr(nmp, NULL, p))
729 return (EINTR);
730 }
731
732 /*
733 * Now, flush as required.
734 */
735 np->n_flag |= NFLUSHINPROG;
736 error = vinvalbuf(vp, flags, cred, p, slpflag, 0);
737 while (error) {
738 if (intrflg && nfs_sigintr(nmp, NULL, p)) {
739 np->n_flag &= ~NFLUSHINPROG;
740 if (np->n_flag & NFLUSHWANT) {
741 np->n_flag &= ~NFLUSHWANT;
742 wakeup((caddr_t)&np->n_flag);
743 }
744 return (EINTR);
745 }
746 error = vinvalbuf(vp, flags, cred, p, 0, slptimeo);
747 }
748 np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
749 if (np->n_flag & NFLUSHWANT) {
750 np->n_flag &= ~NFLUSHWANT;
751 wakeup((caddr_t)&np->n_flag);
752 }
753 return (0);
754 }
755
756 /*
757 * Initiate asynchronous I/O. Return an error if no nfsiods are available.
758 * This is mainly to avoid queueing async I/O requests when the nfsiods
759 * are all hung on a dead server.
760 */
761
762 int
763 nfs_asyncio(bp)
764 struct buf *bp;
765 {
766 int i;
767 struct nfsmount *nmp;
768 int gotiod, slpflag = 0, slptimeo = 0, error;
769
770 if (nfs_numasync == 0)
771 return (EIO);
772
773 nmp = VFSTONFS(bp->b_vp->v_mount);
774 again:
775 if (nmp->nm_flag & NFSMNT_INT)
776 slpflag = PCATCH;
777 gotiod = FALSE;
778
779 /*
780 * Find a free iod to process this request.
781 */
782
783 for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
784 if (nfs_iodwant[i]) {
785 /*
786 * Found one, so wake it up and tell it which
787 * mount to process.
788 */
789 nfs_iodwant[i] = NULL;
790 nfs_iodmount[i] = nmp;
791 nmp->nm_bufqiods++;
792 wakeup((caddr_t)&nfs_iodwant[i]);
793 gotiod = TRUE;
794 break;
795 }
796
797 /*
798 * If none are free, we may already have an iod working on this mount
799 * point. If so, it will process our request.
800 */
801
802 if (!gotiod && nmp->nm_bufqiods > 0)
803 gotiod = TRUE;
804
805 /*
806 * If we have an iod which can process the request, then queue
807 * the buffer.
808 */
809
810 if (gotiod) {
811
812 /*
813 * Ensure that the queue never grows too large.
814 */
815
816 while (nmp->nm_bufqlen >= 2*nfs_numasync) {
817 nmp->nm_bufqwant = TRUE;
818 error = tsleep(&nmp->nm_bufq, slpflag | PRIBIO,
819 "nfsaio", slptimeo);
820 if (error) {
821 if (nfs_sigintr(nmp, NULL, curproc->l_proc))
822 return (EINTR);
823 if (slpflag == PCATCH) {
824 slpflag = 0;
825 slptimeo = 2 * hz;
826 }
827 }
828
829 /*
830 * We might have lost our iod while sleeping,
831 * so check and loop if nescessary.
832 */
833
834 if (nmp->nm_bufqiods == 0)
835 goto again;
836 }
837 TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist);
838 nmp->nm_bufqlen++;
839 return (0);
840 }
841
842 /*
843 * All the iods are busy on other mounts, so return EIO to
844 * force the caller to process the i/o synchronously.
845 */
846
847 return (EIO);
848 }
849
850 /*
851 * Do an I/O operation to/from a cache block. This may be called
852 * synchronously or from an nfsiod.
853 */
854 int
855 nfs_doio(bp, p)
856 struct buf *bp;
857 struct proc *p;
858 {
859 struct uio *uiop;
860 struct vnode *vp;
861 struct nfsnode *np;
862 struct nfsmount *nmp;
863 int error = 0, diff, len, iomode, must_commit = 0;
864 int pushedrange;
865 struct uio uio;
866 struct iovec io;
867 off_t off, cnt;
868 struct uvm_object *uobj;
869 UVMHIST_FUNC("nfs_doio"); UVMHIST_CALLED(ubchist);
870
871 vp = bp->b_vp;
872 uobj = &vp->v_uobj;
873 np = VTONFS(vp);
874 nmp = VFSTONFS(vp->v_mount);
875 uiop = &uio;
876 uiop->uio_iov = &io;
877 uiop->uio_iovcnt = 1;
878 uiop->uio_segflg = UIO_SYSSPACE;
879 uiop->uio_procp = p;
880
881 /*
882 * Historically, paging was done with physio, but no more...
883 */
884 if (bp->b_flags & B_PHYS) {
885 /*
886 * ...though reading /dev/drum still gets us here.
887 */
888 io.iov_len = uiop->uio_resid = bp->b_bcount;
889 /* mapping was done by vmapbuf() */
890 io.iov_base = bp->b_data;
891 uiop->uio_offset = ((off_t)bp->b_blkno) << DEV_BSHIFT;
892 if (bp->b_flags & B_READ) {
893 uiop->uio_rw = UIO_READ;
894 nfsstats.read_physios++;
895 error = nfs_readrpc(vp, uiop);
896 } else {
897 iomode = NFSV3WRITE_DATASYNC;
898 uiop->uio_rw = UIO_WRITE;
899 nfsstats.write_physios++;
900 error = nfs_writerpc(vp, uiop, &iomode, &must_commit);
901 }
902 if (error) {
903 bp->b_flags |= B_ERROR;
904 bp->b_error = error;
905 }
906 } else if (bp->b_flags & B_READ) {
907 io.iov_len = uiop->uio_resid = bp->b_bcount;
908 io.iov_base = bp->b_data;
909 uiop->uio_rw = UIO_READ;
910 switch (vp->v_type) {
911 case VREG:
912 uiop->uio_offset = ((off_t)bp->b_blkno) << DEV_BSHIFT;
913 nfsstats.read_bios++;
914 error = nfs_readrpc(vp, uiop);
915 if (!error && uiop->uio_resid) {
916
917 /*
918 * If len > 0, there is a hole in the file and
919 * no writes after the hole have been pushed to
920 * the server yet.
921 * Just zero fill the rest of the valid area.
922 */
923
924 diff = bp->b_bcount - uiop->uio_resid;
925 len = np->n_size - ((((off_t)bp->b_blkno) << DEV_BSHIFT)
926 + diff);
927 if (len > 0) {
928 len = MIN(len, uiop->uio_resid);
929 memset((char *)bp->b_data + diff, 0, len);
930 }
931 }
932 if (p && (vp->v_flag & VTEXT) &&
933 (((nmp->nm_flag & NFSMNT_NQNFS) &&
934 NQNFS_CKINVALID(vp, np, ND_READ) &&
935 np->n_lrev != np->n_brev) ||
936 (!(nmp->nm_flag & NFSMNT_NQNFS) &&
937 np->n_mtime != np->n_vattr->va_mtime.tv_sec))) {
938 uprintf("Process killed due to "
939 "text file modification\n");
940 psignal(p, SIGKILL);
941 #if 0 /* XXX NJWLWP */
942 p->p_holdcnt++;
943 #endif
944 }
945 break;
946 case VLNK:
947 uiop->uio_offset = (off_t)0;
948 nfsstats.readlink_bios++;
949 error = nfs_readlinkrpc(vp, uiop, curproc->l_proc->p_ucred);
950 break;
951 case VDIR:
952 nfsstats.readdir_bios++;
953 uiop->uio_offset = bp->b_dcookie;
954 if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
955 error = nfs_readdirplusrpc(vp, uiop, curproc->l_proc->p_ucred);
956 if (error == NFSERR_NOTSUPP)
957 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
958 }
959 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
960 error = nfs_readdirrpc(vp, uiop, curproc->l_proc->p_ucred);
961 if (!error) {
962 bp->b_dcookie = uiop->uio_offset;
963 }
964 break;
965 default:
966 printf("nfs_doio: type %x unexpected\n",vp->v_type);
967 break;
968 }
969 if (error) {
970 bp->b_flags |= B_ERROR;
971 bp->b_error = error;
972 }
973 } else {
974 int i, npages = bp->b_bufsize >> PAGE_SHIFT;
975 struct vm_page *pgs[npages];
976 boolean_t needcommit = TRUE;
977
978 if ((bp->b_flags & B_ASYNC) != 0 && NFS_ISV3(vp)) {
979 iomode = NFSV3WRITE_UNSTABLE;
980 } else {
981 iomode = NFSV3WRITE_FILESYNC;
982 }
983
984 for (i = 0; i < npages; i++) {
985 pgs[i] = uvm_pageratop((vaddr_t)bp->b_data +
986 (i << PAGE_SHIFT));
987 if ((pgs[i]->flags & PG_NEEDCOMMIT) == 0) {
988 needcommit = FALSE;
989 }
990 }
991 if (!needcommit && iomode == NFSV3WRITE_UNSTABLE) {
992 for (i = 0; i < npages; i++) {
993 pgs[i]->flags |= PG_NEEDCOMMIT | PG_RDONLY;
994 pmap_page_protect(pgs[i], VM_PROT_READ);
995 }
996 }
997
998 uiop->uio_offset = (((off_t)bp->b_blkno) << DEV_BSHIFT);
999 off = uiop->uio_offset;
1000 cnt = bp->b_bcount;
1001
1002 /*
1003 * Send the data to the server if necessary,
1004 * otherwise just send a commit rpc.
1005 */
1006
1007 if (needcommit) {
1008
1009 /*
1010 * If the buffer is in the range that we already committed,
1011 * there's nothing to do.
1012 *
1013 * If it's in the range that we need to commit, push the
1014 * whole range at once, otherwise only push the buffer.
1015 * In both these cases, acquire the commit lock to avoid
1016 * other processes modifying the range.
1017 */
1018
1019 lockmgr(&np->n_commitlock, LK_EXCLUSIVE, NULL);
1020 if (!nfs_in_committed_range(vp, off, bp->b_bcount)) {
1021 if (nfs_in_tobecommitted_range(vp, off, bp->b_bcount)) {
1022 pushedrange = 1;
1023 off = np->n_pushlo;
1024 cnt = np->n_pushhi - np->n_pushlo;
1025 } else {
1026 pushedrange = 0;
1027 }
1028 error = nfs_commit(vp, off, cnt, curproc->l_proc);
1029 if (error == 0) {
1030 if (pushedrange) {
1031 nfs_merge_commit_ranges(vp);
1032 } else {
1033 nfs_add_committed_range(vp, off, cnt);
1034 }
1035 }
1036 }
1037 lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
1038 if (!error) {
1039 bp->b_resid = 0;
1040 simple_lock(&uobj->vmobjlock);
1041 for (i = 0; i < npages; i++) {
1042 pgs[i]->flags &= ~(PG_NEEDCOMMIT | PG_RDONLY);
1043 }
1044 simple_unlock(&uobj->vmobjlock);
1045 biodone(bp);
1046 return (0);
1047 } else if (error == NFSERR_STALEWRITEVERF) {
1048 nfs_clearcommit(bp->b_vp->v_mount);
1049 }
1050 }
1051 io.iov_base = bp->b_data;
1052 io.iov_len = uiop->uio_resid = bp->b_bcount;
1053 uiop->uio_rw = UIO_WRITE;
1054 nfsstats.write_bios++;
1055 error = nfs_writerpc(vp, uiop, &iomode, &must_commit);
1056 if (!error && iomode == NFSV3WRITE_UNSTABLE) {
1057 lockmgr(&np->n_commitlock, LK_EXCLUSIVE, NULL);
1058 nfs_add_tobecommitted_range(vp, off, cnt);
1059 simple_lock(&uobj->vmobjlock);
1060 for (i = 0; i < npages; i++) {
1061 pgs[i]->flags &= ~PG_CLEAN;
1062 }
1063 simple_unlock(&uobj->vmobjlock);
1064 if (np->n_pushhi - np->n_pushlo > nfs_commitsize) {
1065 off = np->n_pushlo;
1066 cnt = nfs_commitsize >> 1;
1067 error = nfs_commit(vp, off, cnt, curproc->l_proc);
1068 if (!error) {
1069 nfs_add_committed_range(vp, off, cnt);
1070 nfs_del_tobecommitted_range(vp, off, cnt);
1071 }
1072 }
1073 lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
1074 } else if (!error && needcommit) {
1075 lockmgr(&np->n_commitlock, LK_EXCLUSIVE, NULL);
1076 nfs_del_committed_range(vp, off, cnt);
1077 lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
1078 simple_lock(&uobj->vmobjlock);
1079 for (i = 0; i < npages; i++) {
1080 pgs[i]->flags &= ~(PG_NEEDCOMMIT | PG_RDONLY);
1081 }
1082 simple_unlock(&uobj->vmobjlock);
1083 }
1084 }
1085 bp->b_resid = uiop->uio_resid;
1086 if (must_commit || (error == NFSERR_STALEWRITEVERF)) {
1087 nfs_clearcommit(vp->v_mount);
1088 }
1089 biodone(bp);
1090 return (error);
1091 }
1092
1093 /*
1094 * Vnode op for VM getpages.
1095 */
1096
1097 int
1098 nfs_getpages(v)
1099 void *v;
1100 {
1101 struct vop_getpages_args /* {
1102 struct vnode *a_vp;
1103 voff_t a_offset;
1104 struct vm_page **a_m;
1105 int *a_count;
1106 int a_centeridx;
1107 vm_prot_t a_access_type;
1108 int a_advice;
1109 int a_flags;
1110 } */ *ap = v;
1111
1112 struct vnode *vp = ap->a_vp;
1113 struct uvm_object *uobj = &vp->v_uobj;
1114 struct nfsnode *np = VTONFS(vp);
1115 struct vm_page *pg, **pgs;
1116 off_t origoffset, len;
1117 int i, error, npages;
1118 boolean_t v3 = NFS_ISV3(vp);
1119 boolean_t write = (ap->a_access_type & VM_PROT_WRITE) != 0;
1120 boolean_t locked = (ap->a_flags & PGO_LOCKED) != 0;
1121
1122 /*
1123 * update the cached read creds for this node.
1124 */
1125
1126 if (np->n_rcred) {
1127 crfree(np->n_rcred);
1128 }
1129 np->n_rcred = curproc->l_proc->p_ucred;
1130 crhold(np->n_rcred);
1131
1132 /*
1133 * call the genfs code to get the pages.
1134 */
1135
1136 npages = *ap->a_count;
1137 error = genfs_getpages(v);
1138 if (error) {
1139 return error;
1140 }
1141
1142 /*
1143 * for read faults where the nfs node is not yet marked NMODIFIED,
1144 * set PG_RDONLY on the pages so that we come back here if someone
1145 * tries to modify later via the mapping that will be entered for
1146 * this fault.
1147 */
1148
1149 pgs = ap->a_m;
1150 if (!write && (np->n_flag & NMODIFIED) == 0 && pgs != NULL) {
1151 if (!locked) {
1152 simple_lock(&uobj->vmobjlock);
1153 }
1154 for (i = 0; i < npages; i++) {
1155 pg = pgs[i];
1156 if (pg == NULL || pg == PGO_DONTCARE) {
1157 continue;
1158 }
1159 pg->flags |= PG_RDONLY;
1160 }
1161 if (!locked) {
1162 simple_unlock(&uobj->vmobjlock);
1163 }
1164 }
1165 if (!write) {
1166 return error;
1167 }
1168
1169 /*
1170 * this is a write fault, update the commit info.
1171 */
1172
1173 origoffset = ap->a_offset;
1174 len = npages << PAGE_SHIFT;
1175
1176 np->n_flag |= NMODIFIED;
1177 if (v3) {
1178 lockmgr(&np->n_commitlock, LK_EXCLUSIVE, NULL);
1179 nfs_del_committed_range(vp, origoffset, len);
1180 nfs_del_tobecommitted_range(vp, origoffset, len);
1181 }
1182 if (!locked) {
1183 simple_lock(&uobj->vmobjlock);
1184 }
1185 for (i = 0; i < npages; i++) {
1186 pg = pgs[i];
1187 if (pg == NULL || pg == PGO_DONTCARE) {
1188 continue;
1189 }
1190 pg->flags &= ~(PG_NEEDCOMMIT | PG_RDONLY);
1191 }
1192 if (!locked) {
1193 simple_unlock(&uobj->vmobjlock);
1194 }
1195 if (v3) {
1196 lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
1197 }
1198 return 0;
1199 }
1200