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