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