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