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