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