2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)nfs_bio.c 8.9 (Berkeley) 3/30/95
37 * $FreeBSD: src/sys/nfs/nfs_bio.c,v 1.83.2.4 2002/12/29 18:19:53 dillon Exp $
38 * $DragonFly: src/sys/vfs/nfs/nfs_bio.c,v 1.3 2003/06/19 01:55:07 dillon Exp $
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/resourcevar.h>
45 #include <sys/signalvar.h>
48 #include <sys/vnode.h>
49 #include <sys/mount.h>
50 #include <sys/kernel.h>
53 #include <vm/vm_extern.h>
54 #include <vm/vm_page.h>
55 #include <vm/vm_object.h>
56 #include <vm/vm_pager.h>
57 #include <vm/vnode_pager.h>
61 #include <nfs/rpcv2.h>
62 #include <nfs/nfsproto.h>
64 #include <nfs/nfsmount.h>
65 #include <nfs/nqnfs.h>
66 #include <nfs/nfsnode.h>
68 static struct buf *nfs_getcacheblk __P((struct vnode *vp, daddr_t bn, int size,
71 extern int nfs_numasync;
72 extern int nfs_pbuf_freecnt;
73 extern struct nfsstats nfsstats;
76 * Vnode op for VM getpages.
80 struct vop_getpages_args /* {
85 vm_ooffset_t a_offset;
88 int i, error, nextoff, size, toff, count, npages;
100 p = curproc; /* XXX */
101 cred = curproc->p_ucred; /* XXX */
102 nmp = VFSTONFS(vp->v_mount);
106 if (vp->v_object == NULL) {
107 printf("nfs_getpages: called with non-merged cache vnode??\n");
108 return VM_PAGER_ERROR;
111 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
112 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
113 (void)nfs_fsinfo(nmp, vp, cred, p);
115 npages = btoc(count);
118 * If the requested page is partially valid, just return it and
119 * allow the pager to zero-out the blanks. Partially valid pages
120 * can only occur at the file EOF.
124 vm_page_t m = pages[ap->a_reqpage];
127 /* handled by vm_fault now */
128 /* vm_page_zero_invalid(m, TRUE); */
129 for (i = 0; i < npages; ++i) {
130 if (i != ap->a_reqpage)
131 vnode_pager_freepage(pages[i]);
138 * We use only the kva address for the buffer, but this is extremely
139 * convienient and fast.
141 bp = getpbuf(&nfs_pbuf_freecnt);
143 kva = (vm_offset_t) bp->b_data;
144 pmap_qenter(kva, pages, npages);
146 iov.iov_base = (caddr_t) kva;
150 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
151 uio.uio_resid = count;
152 uio.uio_segflg = UIO_SYSSPACE;
153 uio.uio_rw = UIO_READ;
156 error = nfs_readrpc(vp, &uio, cred);
157 pmap_qremove(kva, npages);
159 relpbuf(bp, &nfs_pbuf_freecnt);
161 if (error && (uio.uio_resid == count)) {
162 printf("nfs_getpages: error %d\n", error);
163 for (i = 0; i < npages; ++i) {
164 if (i != ap->a_reqpage)
165 vnode_pager_freepage(pages[i]);
167 return VM_PAGER_ERROR;
171 * Calculate the number of bytes read and validate only that number
172 * of bytes. Note that due to pending writes, size may be 0. This
173 * does not mean that the remaining data is invalid!
176 size = count - uio.uio_resid;
178 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
180 nextoff = toff + PAGE_SIZE;
183 m->flags &= ~PG_ZERO;
185 if (nextoff <= size) {
187 * Read operation filled an entire page
189 m->valid = VM_PAGE_BITS_ALL;
191 } else if (size > toff) {
193 * Read operation filled a partial page.
196 vm_page_set_validclean(m, 0, size - toff);
197 /* handled by vm_fault now */
198 /* vm_page_zero_invalid(m, TRUE); */
201 * Read operation was short. If no error occured
202 * we may have hit a zero-fill section. We simply
203 * leave valid set to 0.
207 if (i != ap->a_reqpage) {
209 * Whether or not to leave the page activated is up in
210 * the air, but we should put the page on a page queue
211 * somewhere (it already is in the object). Result:
212 * It appears that emperical results show that
213 * deactivating pages is best.
217 * Just in case someone was asking for this page we
218 * now tell them that it is ok to use.
221 if (m->flags & PG_WANTED)
224 vm_page_deactivate(m);
227 vnode_pager_freepage(m);
235 * Vnode op for VM putpages.
239 struct vop_putpages_args /* {
245 vm_ooffset_t a_offset;
252 int iomode, must_commit, i, error, npages, count;
258 struct nfsmount *nmp;
264 p = curproc; /* XXX */
265 cred = curproc->p_ucred; /* XXX */
266 nmp = VFSTONFS(vp->v_mount);
269 rtvals = ap->a_rtvals;
270 npages = btoc(count);
271 offset = IDX_TO_OFF(pages[0]->pindex);
273 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
274 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
275 (void)nfs_fsinfo(nmp, vp, cred, p);
277 for (i = 0; i < npages; i++) {
278 rtvals[i] = VM_PAGER_AGAIN;
282 * When putting pages, do not extend file past EOF.
285 if (offset + count > np->n_size) {
286 count = np->n_size - offset;
292 * We use only the kva address for the buffer, but this is extremely
293 * convienient and fast.
295 bp = getpbuf(&nfs_pbuf_freecnt);
297 kva = (vm_offset_t) bp->b_data;
298 pmap_qenter(kva, pages, npages);
300 iov.iov_base = (caddr_t) kva;
304 uio.uio_offset = offset;
305 uio.uio_resid = count;
306 uio.uio_segflg = UIO_SYSSPACE;
307 uio.uio_rw = UIO_WRITE;
310 if ((ap->a_sync & VM_PAGER_PUT_SYNC) == 0)
311 iomode = NFSV3WRITE_UNSTABLE;
313 iomode = NFSV3WRITE_FILESYNC;
315 error = nfs_writerpc(vp, &uio, cred, &iomode, &must_commit);
317 pmap_qremove(kva, npages);
318 relpbuf(bp, &nfs_pbuf_freecnt);
321 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
322 for (i = 0; i < nwritten; i++) {
323 rtvals[i] = VM_PAGER_OK;
324 vm_page_undirty(pages[i]);
327 nfs_clearcommit(vp->v_mount);
333 * Vnode op for read using bio
336 nfs_bioread(vp, uio, ioflag, cred)
337 register struct vnode *vp;
338 register struct uio *uio;
342 register struct nfsnode *np = VTONFS(vp);
343 register int biosize, i;
344 struct buf *bp = 0, *rabp;
347 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
351 int nra, error = 0, n = 0, on = 0;
354 if (uio->uio_rw != UIO_READ)
355 panic("nfs_read mode");
357 if (uio->uio_resid == 0)
359 if (uio->uio_offset < 0) /* XXX VDIR cookies can be negative */
363 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
364 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
365 (void)nfs_fsinfo(nmp, vp, cred, p);
366 if (vp->v_type != VDIR &&
367 (uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
369 biosize = vp->v_mount->mnt_stat.f_iosize;
370 seqcount = (int)((off_t)(ioflag >> IO_SEQSHIFT) * biosize / BKVASIZE);
372 * For nfs, cache consistency can only be maintained approximately.
373 * Although RFC1094 does not specify the criteria, the following is
374 * believed to be compatible with the reference port.
375 * For nqnfs, full cache consistency is maintained within the loop.
377 * If the file's modify time on the server has changed since the
378 * last read rpc or you have written to the file,
379 * you may have lost data cache consistency with the
380 * server, so flush all of the file's data out of the cache.
381 * Then force a getattr rpc to ensure that you have up to date
383 * NB: This implies that cache data can be read when up to
384 * NFS_ATTRTIMEO seconds out of date. If you find that you need current
385 * attributes this could be forced by setting n_attrstamp to 0 before
386 * the VOP_GETATTR() call.
388 if ((nmp->nm_flag & NFSMNT_NQNFS) == 0) {
389 if (np->n_flag & NMODIFIED) {
390 if (vp->v_type != VREG) {
391 if (vp->v_type != VDIR)
392 panic("nfs: bioread, not dir");
394 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
399 error = VOP_GETATTR(vp, &vattr, cred, p);
402 np->n_mtime = vattr.va_mtime.tv_sec;
404 error = VOP_GETATTR(vp, &vattr, cred, p);
407 if (np->n_mtime != vattr.va_mtime.tv_sec) {
408 if (vp->v_type == VDIR)
410 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
413 np->n_mtime = vattr.va_mtime.tv_sec;
420 * Get a valid lease. If cached data is stale, flush it.
422 if (nmp->nm_flag & NFSMNT_NQNFS) {
423 if (NQNFS_CKINVALID(vp, np, ND_READ)) {
425 error = nqnfs_getlease(vp, ND_READ, cred, p);
426 } while (error == NQNFS_EXPIRED);
429 if (np->n_lrev != np->n_brev ||
430 (np->n_flag & NQNFSNONCACHE) ||
431 ((np->n_flag & NMODIFIED) && vp->v_type == VDIR)) {
432 if (vp->v_type == VDIR)
434 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
437 np->n_brev = np->n_lrev;
439 } else if (vp->v_type == VDIR && (np->n_flag & NMODIFIED)) {
441 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
446 if (np->n_flag & NQNFSNONCACHE) {
447 switch (vp->v_type) {
449 return (nfs_readrpc(vp, uio, cred));
451 return (nfs_readlinkrpc(vp, uio, cred));
455 printf(" NQNFSNONCACHE: type %x unexpected\n",
459 switch (vp->v_type) {
461 nfsstats.biocache_reads++;
462 lbn = uio->uio_offset / biosize;
463 on = uio->uio_offset & (biosize - 1);
466 * Start the read ahead(s), as required.
468 if (nfs_numasync > 0 && nmp->nm_readahead > 0) {
469 for (nra = 0; nra < nmp->nm_readahead && nra < seqcount &&
470 (off_t)(lbn + 1 + nra) * biosize < np->n_size; nra++) {
471 rabn = lbn + 1 + nra;
472 if (!incore(vp, rabn)) {
473 rabp = nfs_getcacheblk(vp, rabn, biosize, p);
476 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
477 rabp->b_flags |= (B_READ | B_ASYNC);
478 vfs_busy_pages(rabp, 0);
479 if (nfs_asyncio(rabp, cred, p)) {
480 rabp->b_flags |= B_INVAL|B_ERROR;
481 vfs_unbusy_pages(rabp);
493 * Obtain the buffer cache block. Figure out the buffer size
494 * when we are at EOF. If we are modifying the size of the
495 * buffer based on an EOF condition we need to hold
496 * nfs_rslock() through obtaining the buffer to prevent
497 * a potential writer-appender from messing with n_size.
498 * Otherwise we may accidently truncate the buffer and
501 * Note that bcount is *not* DEV_BSIZE aligned.
506 if ((off_t)lbn * biosize >= np->n_size) {
508 } else if ((off_t)(lbn + 1) * biosize > np->n_size) {
509 bcount = np->n_size - (off_t)lbn * biosize;
511 if (bcount != biosize) {
512 switch(nfs_rslock(np, p)) {
525 bp = nfs_getcacheblk(vp, lbn, bcount, p);
527 if (bcount != biosize)
533 * If B_CACHE is not set, we must issue the read. If this
534 * fails, we return an error.
537 if ((bp->b_flags & B_CACHE) == 0) {
538 bp->b_flags |= B_READ;
539 vfs_busy_pages(bp, 0);
540 error = nfs_doio(bp, cred, p);
548 * on is the offset into the current bp. Figure out how many
549 * bytes we can copy out of the bp. Note that bcount is
550 * NOT DEV_BSIZE aligned.
552 * Then figure out how many bytes we can copy into the uio.
557 n = min((unsigned)(bcount - on), uio->uio_resid);
560 nfsstats.biocache_readlinks++;
561 bp = nfs_getcacheblk(vp, (daddr_t)0, NFS_MAXPATHLEN, p);
564 if ((bp->b_flags & B_CACHE) == 0) {
565 bp->b_flags |= B_READ;
566 vfs_busy_pages(bp, 0);
567 error = nfs_doio(bp, cred, p);
569 bp->b_flags |= B_ERROR;
574 n = min(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid);
578 nfsstats.biocache_readdirs++;
579 if (np->n_direofoffset
580 && uio->uio_offset >= np->n_direofoffset) {
583 lbn = (uoff_t)uio->uio_offset / NFS_DIRBLKSIZ;
584 on = uio->uio_offset & (NFS_DIRBLKSIZ - 1);
585 bp = nfs_getcacheblk(vp, lbn, NFS_DIRBLKSIZ, p);
588 if ((bp->b_flags & B_CACHE) == 0) {
589 bp->b_flags |= B_READ;
590 vfs_busy_pages(bp, 0);
591 error = nfs_doio(bp, cred, p);
595 while (error == NFSERR_BAD_COOKIE) {
596 printf("got bad cookie vp %p bp %p\n", vp, bp);
598 error = nfs_vinvalbuf(vp, 0, cred, p, 1);
600 * Yuck! The directory has been modified on the
601 * server. The only way to get the block is by
602 * reading from the beginning to get all the
605 * Leave the last bp intact unless there is an error.
606 * Loop back up to the while if the error is another
607 * NFSERR_BAD_COOKIE (double yuch!).
609 for (i = 0; i <= lbn && !error; i++) {
610 if (np->n_direofoffset
611 && (i * NFS_DIRBLKSIZ) >= np->n_direofoffset)
613 bp = nfs_getcacheblk(vp, i, NFS_DIRBLKSIZ, p);
616 if ((bp->b_flags & B_CACHE) == 0) {
617 bp->b_flags |= B_READ;
618 vfs_busy_pages(bp, 0);
619 error = nfs_doio(bp, cred, p);
621 * no error + B_INVAL == directory EOF,
624 if (error == 0 && (bp->b_flags & B_INVAL))
628 * An error will throw away the block and the
629 * for loop will break out. If no error and this
630 * is not the block we want, we throw away the
631 * block and go for the next one via the for loop.
633 if (error || i < lbn)
638 * The above while is repeated if we hit another cookie
639 * error. If we hit an error and it wasn't a cookie error,
647 * If not eof and read aheads are enabled, start one.
648 * (You need the current block first, so that you have the
649 * directory offset cookie of the next block.)
651 if (nfs_numasync > 0 && nmp->nm_readahead > 0 &&
652 (bp->b_flags & B_INVAL) == 0 &&
653 (np->n_direofoffset == 0 ||
654 (lbn + 1) * NFS_DIRBLKSIZ < np->n_direofoffset) &&
655 !(np->n_flag & NQNFSNONCACHE) &&
656 !incore(vp, lbn + 1)) {
657 rabp = nfs_getcacheblk(vp, lbn + 1, NFS_DIRBLKSIZ, p);
659 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
660 rabp->b_flags |= (B_READ | B_ASYNC);
661 vfs_busy_pages(rabp, 0);
662 if (nfs_asyncio(rabp, cred, p)) {
663 rabp->b_flags |= B_INVAL|B_ERROR;
664 vfs_unbusy_pages(rabp);
673 * Unlike VREG files, whos buffer size ( bp->b_bcount ) is
674 * chopped for the EOF condition, we cannot tell how large
675 * NFS directories are going to be until we hit EOF. So
676 * an NFS directory buffer is *not* chopped to its EOF. Now,
677 * it just so happens that b_resid will effectively chop it
678 * to EOF. *BUT* this information is lost if the buffer goes
679 * away and is reconstituted into a B_CACHE state ( due to
680 * being VMIO ) later. So we keep track of the directory eof
681 * in np->n_direofoffset and chop it off as an extra step
684 n = lmin(uio->uio_resid, NFS_DIRBLKSIZ - bp->b_resid - on);
685 if (np->n_direofoffset && n > np->n_direofoffset - uio->uio_offset)
686 n = np->n_direofoffset - uio->uio_offset;
689 printf(" nfs_bioread: type %x unexpected\n",vp->v_type);
694 error = uiomove(bp->b_data + on, (int)n, uio);
696 switch (vp->v_type) {
704 * Invalidate buffer if caching is disabled, forcing a
705 * re-read from the remote later.
707 if (np->n_flag & NQNFSNONCACHE)
708 bp->b_flags |= B_INVAL;
711 printf(" nfs_bioread: type %x unexpected\n",vp->v_type);
714 } while (error == 0 && uio->uio_resid > 0 && n > 0);
719 * Vnode op for write using bio
723 struct vop_write_args /* {
727 struct ucred *a_cred;
731 struct uio *uio = ap->a_uio;
732 struct proc *p = uio->uio_procp;
733 struct vnode *vp = ap->a_vp;
734 struct nfsnode *np = VTONFS(vp);
735 struct ucred *cred = ap->a_cred;
736 int ioflag = ap->a_ioflag;
739 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
742 int n, on, error = 0, iomode, must_commit;
746 if (uio->uio_rw != UIO_WRITE)
747 panic("nfs_write mode");
748 if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
749 panic("nfs_write proc");
751 if (vp->v_type != VREG)
753 if (np->n_flag & NWRITEERR) {
754 np->n_flag &= ~NWRITEERR;
755 return (np->n_error);
757 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
758 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
759 (void)nfs_fsinfo(nmp, vp, cred, p);
762 * Synchronously flush pending buffers if we are in synchronous
763 * mode or if we are appending.
765 if (ioflag & (IO_APPEND | IO_SYNC)) {
766 if (np->n_flag & NMODIFIED) {
768 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
775 * If IO_APPEND then load uio_offset. We restart here if we cannot
776 * get the append lock.
779 if (ioflag & IO_APPEND) {
781 error = VOP_GETATTR(vp, &vattr, cred, p);
784 uio->uio_offset = np->n_size;
787 if (uio->uio_offset < 0)
789 if ((uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
791 if (uio->uio_resid == 0)
795 * We need to obtain the rslock if we intend to modify np->n_size
796 * in order to guarentee the append point with multiple contending
797 * writers, to guarentee that no other appenders modify n_size
798 * while we are trying to obtain a truncated buffer (i.e. to avoid
799 * accidently truncating data written by another appender due to
800 * the race), and to ensure that the buffer is populated prior to
801 * our extending of the file. We hold rslock through the entire
804 * Note that we do not synchronize the case where someone truncates
805 * the file while we are appending to it because attempting to lock
806 * this case may deadlock other parts of the system unexpectedly.
808 if ((ioflag & IO_APPEND) ||
809 uio->uio_offset + uio->uio_resid > np->n_size) {
810 switch(nfs_rslock(np, p)) {
825 * Maybe this should be above the vnode op call, but so long as
826 * file servers have no limits, i don't think it matters
828 if (p && uio->uio_offset + uio->uio_resid >
829 p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
836 biosize = vp->v_mount->mnt_stat.f_iosize;
840 * Check for a valid write lease.
842 if ((nmp->nm_flag & NFSMNT_NQNFS) &&
843 NQNFS_CKINVALID(vp, np, ND_WRITE)) {
845 error = nqnfs_getlease(vp, ND_WRITE, cred, p);
846 } while (error == NQNFS_EXPIRED);
849 if (np->n_lrev != np->n_brev ||
850 (np->n_flag & NQNFSNONCACHE)) {
851 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
854 np->n_brev = np->n_lrev;
857 if ((np->n_flag & NQNFSNONCACHE) && uio->uio_iovcnt == 1) {
858 iomode = NFSV3WRITE_FILESYNC;
859 error = nfs_writerpc(vp, uio, cred, &iomode, &must_commit);
861 nfs_clearcommit(vp->v_mount);
864 nfsstats.biocache_writes++;
865 lbn = uio->uio_offset / biosize;
866 on = uio->uio_offset & (biosize-1);
867 n = min((unsigned)(biosize - on), uio->uio_resid);
870 * Handle direct append and file extension cases, calculate
871 * unaligned buffer size.
874 if (uio->uio_offset == np->n_size && n) {
876 * Get the buffer (in its pre-append state to maintain
877 * B_CACHE if it was previously set). Resize the
878 * nfsnode after we have locked the buffer to prevent
879 * readers from reading garbage.
882 bp = nfs_getcacheblk(vp, lbn, bcount, p);
887 np->n_size = uio->uio_offset + n;
888 np->n_flag |= NMODIFIED;
889 vnode_pager_setsize(vp, np->n_size);
891 save = bp->b_flags & B_CACHE;
893 allocbuf(bp, bcount);
898 * Obtain the locked cache block first, and then
899 * adjust the file's size as appropriate.
902 if ((off_t)lbn * biosize + bcount < np->n_size) {
903 if ((off_t)(lbn + 1) * biosize < np->n_size)
906 bcount = np->n_size - (off_t)lbn * biosize;
908 bp = nfs_getcacheblk(vp, lbn, bcount, p);
909 if (uio->uio_offset + n > np->n_size) {
910 np->n_size = uio->uio_offset + n;
911 np->n_flag |= NMODIFIED;
912 vnode_pager_setsize(vp, np->n_size);
922 * Issue a READ if B_CACHE is not set. In special-append
923 * mode, B_CACHE is based on the buffer prior to the write
924 * op and is typically set, avoiding the read. If a read
925 * is required in special append mode, the server will
926 * probably send us a short-read since we extended the file
927 * on our end, resulting in b_resid == 0 and, thusly,
928 * B_CACHE getting set.
930 * We can also avoid issuing the read if the write covers
931 * the entire buffer. We have to make sure the buffer state
932 * is reasonable in this case since we will not be initiating
933 * I/O. See the comments in kern/vfs_bio.c's getblk() for
936 * B_CACHE may also be set due to the buffer being cached
940 if (on == 0 && n == bcount) {
941 bp->b_flags |= B_CACHE;
942 bp->b_flags &= ~(B_ERROR | B_INVAL);
945 if ((bp->b_flags & B_CACHE) == 0) {
946 bp->b_flags |= B_READ;
947 vfs_busy_pages(bp, 0);
948 error = nfs_doio(bp, cred, p);
958 if (bp->b_wcred == NOCRED) {
962 np->n_flag |= NMODIFIED;
965 * If dirtyend exceeds file size, chop it down. This should
966 * not normally occur but there is an append race where it
967 * might occur XXX, so we log it.
969 * If the chopping creates a reverse-indexed or degenerate
970 * situation with dirtyoff/end, we 0 both of them.
973 if (bp->b_dirtyend > bcount) {
974 printf("NFS append race @%lx:%d\n",
975 (long)bp->b_blkno * DEV_BSIZE,
976 bp->b_dirtyend - bcount);
977 bp->b_dirtyend = bcount;
980 if (bp->b_dirtyoff >= bp->b_dirtyend)
981 bp->b_dirtyoff = bp->b_dirtyend = 0;
984 * If the new write will leave a contiguous dirty
985 * area, just update the b_dirtyoff and b_dirtyend,
986 * otherwise force a write rpc of the old dirty area.
988 * While it is possible to merge discontiguous writes due to
989 * our having a B_CACHE buffer ( and thus valid read data
990 * for the hole), we don't because it could lead to
991 * significant cache coherency problems with multiple clients,
992 * especially if locking is implemented later on.
994 * as an optimization we could theoretically maintain
995 * a linked list of discontinuous areas, but we would still
996 * have to commit them separately so there isn't much
997 * advantage to it except perhaps a bit of asynchronization.
1000 if (bp->b_dirtyend > 0 &&
1001 (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) {
1002 if (VOP_BWRITE(bp->b_vp, bp) == EINTR) {
1010 * Check for valid write lease and get one as required.
1011 * In case getblk() and/or bwrite() delayed us.
1013 if ((nmp->nm_flag & NFSMNT_NQNFS) &&
1014 NQNFS_CKINVALID(vp, np, ND_WRITE)) {
1016 error = nqnfs_getlease(vp, ND_WRITE, cred, p);
1017 } while (error == NQNFS_EXPIRED);
1022 if (np->n_lrev != np->n_brev ||
1023 (np->n_flag & NQNFSNONCACHE)) {
1025 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
1028 np->n_brev = np->n_lrev;
1033 error = uiomove((char *)bp->b_data + on, n, uio);
1036 * Since this block is being modified, it must be written
1037 * again and not just committed. Since write clustering does
1038 * not work for the stage 1 data write, only the stage 2
1039 * commit rpc, we have to clear B_CLUSTEROK as well.
1041 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1044 bp->b_flags |= B_ERROR;
1050 * Only update dirtyoff/dirtyend if not a degenerate
1054 if (bp->b_dirtyend > 0) {
1055 bp->b_dirtyoff = min(on, bp->b_dirtyoff);
1056 bp->b_dirtyend = max((on + n), bp->b_dirtyend);
1058 bp->b_dirtyoff = on;
1059 bp->b_dirtyend = on + n;
1061 vfs_bio_set_validclean(bp, on, n);
1064 * If IO_NOWDRAIN then set B_NOWDRAIN (e.g. nfs-backed VN
1065 * filesystem). XXX also use for loopback NFS mounts.
1067 if (ioflag & IO_NOWDRAIN)
1068 bp->b_flags |= B_NOWDRAIN;
1071 * If the lease is non-cachable or IO_SYNC do bwrite().
1073 * IO_INVAL appears to be unused. The idea appears to be
1074 * to turn off caching in this case. Very odd. XXX
1076 if ((np->n_flag & NQNFSNONCACHE) || (ioflag & IO_SYNC)) {
1077 if (ioflag & IO_INVAL)
1078 bp->b_flags |= B_NOCACHE;
1079 error = VOP_BWRITE(bp->b_vp, bp);
1082 if (np->n_flag & NQNFSNONCACHE) {
1083 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
1087 } else if ((n + on) == biosize &&
1088 (nmp->nm_flag & NFSMNT_NQNFS) == 0) {
1089 bp->b_flags |= B_ASYNC;
1090 (void)nfs_writebp(bp, 0, 0);
1094 } while (uio->uio_resid > 0 && n > 0);
1097 nfs_rsunlock(np, p);
1103 * Get an nfs cache block.
1105 * Allocate a new one if the block isn't currently in the cache
1106 * and return the block marked busy. If the calling process is
1107 * interrupted by a signal for an interruptible mount point, return
1110 * The caller must carefully deal with the possible B_INVAL state of
1111 * the buffer. nfs_doio() clears B_INVAL (and nfs_asyncio() clears it
1112 * indirectly), so synchronous reads can be issued without worrying about
1113 * the B_INVAL state. We have to be a little more careful when dealing
1114 * with writes (see comments in nfs_write()) when extending a file past
1118 nfs_getcacheblk(vp, bn, size, p)
1124 register struct buf *bp;
1126 struct nfsmount *nmp;
1131 if (nmp->nm_flag & NFSMNT_INT) {
1132 bp = getblk(vp, bn, size, PCATCH, 0);
1133 while (bp == (struct buf *)0) {
1134 if (nfs_sigintr(nmp, (struct nfsreq *)0, p))
1135 return ((struct buf *)0);
1136 bp = getblk(vp, bn, size, 0, 2 * hz);
1139 bp = getblk(vp, bn, size, 0, 0);
1142 if (vp->v_type == VREG) {
1145 biosize = mp->mnt_stat.f_iosize;
1146 bp->b_blkno = bn * (biosize / DEV_BSIZE);
1152 * Flush and invalidate all dirty buffers. If another process is already
1153 * doing the flush, just wait for completion.
1156 nfs_vinvalbuf(vp, flags, cred, p, intrflg)
1163 register struct nfsnode *np = VTONFS(vp);
1164 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1165 int error = 0, slpflag, slptimeo;
1167 if (vp->v_flag & VXLOCK) {
1171 if ((nmp->nm_flag & NFSMNT_INT) == 0)
1181 * First wait for any other process doing a flush to complete.
1183 while (np->n_flag & NFLUSHINPROG) {
1184 np->n_flag |= NFLUSHWANT;
1185 error = tsleep((caddr_t)&np->n_flag, PRIBIO + 2, "nfsvinval",
1187 if (error && intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, p))
1192 * Now, flush as required.
1194 np->n_flag |= NFLUSHINPROG;
1195 error = vinvalbuf(vp, flags, cred, p, slpflag, 0);
1197 if (intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, p)) {
1198 np->n_flag &= ~NFLUSHINPROG;
1199 if (np->n_flag & NFLUSHWANT) {
1200 np->n_flag &= ~NFLUSHWANT;
1201 wakeup((caddr_t)&np->n_flag);
1205 error = vinvalbuf(vp, flags, cred, p, 0, slptimeo);
1207 np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
1208 if (np->n_flag & NFLUSHWANT) {
1209 np->n_flag &= ~NFLUSHWANT;
1210 wakeup((caddr_t)&np->n_flag);
1216 * Initiate asynchronous I/O. Return an error if no nfsiods are available.
1217 * This is mainly to avoid queueing async I/O requests when the nfsiods
1218 * are all hung on a dead server.
1220 * Note: nfs_asyncio() does not clear (B_ERROR|B_INVAL) but when the bp
1221 * is eventually dequeued by the async daemon, nfs_doio() *will*.
1224 nfs_asyncio(bp, cred, procp)
1225 register struct buf *bp;
1229 struct nfsmount *nmp;
1237 * If no async daemons then return EIO to force caller to run the rpc
1240 if (nfs_numasync == 0)
1243 nmp = VFSTONFS(bp->b_vp->v_mount);
1246 * Commits are usually short and sweet so lets save some cpu and
1247 * leave the async daemons for more important rpc's (such as reads
1250 if ((bp->b_flags & (B_READ|B_NEEDCOMMIT)) == B_NEEDCOMMIT &&
1251 (nmp->nm_bufqiods > nfs_numasync / 2)) {
1256 if (nmp->nm_flag & NFSMNT_INT)
1261 * Find a free iod to process this request.
1263 for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
1264 if (nfs_iodwant[i]) {
1266 * Found one, so wake it up and tell it which
1270 ("nfs_asyncio: waking iod %d for mount %p\n",
1272 nfs_iodwant[i] = (struct proc *)0;
1273 nfs_iodmount[i] = nmp;
1275 wakeup((caddr_t)&nfs_iodwant[i]);
1281 * If none are free, we may already have an iod working on this mount
1282 * point. If so, it will process our request.
1285 if (nmp->nm_bufqiods > 0) {
1287 ("nfs_asyncio: %d iods are already processing mount %p\n",
1288 nmp->nm_bufqiods, nmp));
1294 * If we have an iod which can process the request, then queue
1299 * Ensure that the queue never grows too large. We still want
1300 * to asynchronize so we block rather then return EIO.
1302 while (nmp->nm_bufqlen >= 2*nfs_numasync) {
1304 ("nfs_asyncio: waiting for mount %p queue to drain\n", nmp));
1305 nmp->nm_bufqwant = TRUE;
1306 error = tsleep(&nmp->nm_bufq, slpflag | PRIBIO,
1307 "nfsaio", slptimeo);
1309 if (nfs_sigintr(nmp, NULL, procp))
1311 if (slpflag == PCATCH) {
1317 * We might have lost our iod while sleeping,
1318 * so check and loop if nescessary.
1320 if (nmp->nm_bufqiods == 0) {
1322 ("nfs_asyncio: no iods after mount %p queue was drained, looping\n", nmp));
1327 if (bp->b_flags & B_READ) {
1328 if (bp->b_rcred == NOCRED && cred != NOCRED) {
1333 bp->b_flags |= B_WRITEINPROG;
1334 if (bp->b_wcred == NOCRED && cred != NOCRED) {
1341 TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist);
1347 * All the iods are busy on other mounts, so return EIO to
1348 * force the caller to process the i/o synchronously.
1350 NFS_DPF(ASYNCIO, ("nfs_asyncio: no iods available, i/o is synchronous\n"));
1355 * Do an I/O operation to/from a cache block. This may be called
1356 * synchronously or from an nfsiod.
1367 struct nfsmount *nmp;
1368 int error = 0, iomode, must_commit = 0;
1374 nmp = VFSTONFS(vp->v_mount);
1376 uiop->uio_iov = &io;
1377 uiop->uio_iovcnt = 1;
1378 uiop->uio_segflg = UIO_SYSSPACE;
1379 uiop->uio_procp = p;
1382 * clear B_ERROR and B_INVAL state prior to initiating the I/O. We
1383 * do this here so we do not have to do it in all the code that
1386 bp->b_flags &= ~(B_ERROR | B_INVAL);
1388 KASSERT(!(bp->b_flags & B_DONE), ("nfs_doio: bp %p already marked done", bp));
1391 * Historically, paging was done with physio, but no more.
1393 if (bp->b_flags & B_PHYS) {
1395 * ...though reading /dev/drum still gets us here.
1397 io.iov_len = uiop->uio_resid = bp->b_bcount;
1398 /* mapping was done by vmapbuf() */
1399 io.iov_base = bp->b_data;
1400 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
1401 if (bp->b_flags & B_READ) {
1402 uiop->uio_rw = UIO_READ;
1403 nfsstats.read_physios++;
1404 error = nfs_readrpc(vp, uiop, cr);
1408 iomode = NFSV3WRITE_DATASYNC;
1409 uiop->uio_rw = UIO_WRITE;
1410 nfsstats.write_physios++;
1411 error = nfs_writerpc(vp, uiop, cr, &iomode, &com);
1414 bp->b_flags |= B_ERROR;
1415 bp->b_error = error;
1417 } else if (bp->b_flags & B_READ) {
1418 io.iov_len = uiop->uio_resid = bp->b_bcount;
1419 io.iov_base = bp->b_data;
1420 uiop->uio_rw = UIO_READ;
1422 switch (vp->v_type) {
1424 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
1425 nfsstats.read_bios++;
1426 error = nfs_readrpc(vp, uiop, cr);
1429 if (uiop->uio_resid) {
1431 * If we had a short read with no error, we must have
1432 * hit a file hole. We should zero-fill the remainder.
1433 * This can also occur if the server hits the file EOF.
1435 * Holes used to be able to occur due to pending
1436 * writes, but that is not possible any longer.
1438 int nread = bp->b_bcount - uiop->uio_resid;
1439 int left = uiop->uio_resid;
1442 bzero((char *)bp->b_data + nread, left);
1443 uiop->uio_resid = 0;
1446 if (p && (vp->v_flag & VTEXT) &&
1447 (((nmp->nm_flag & NFSMNT_NQNFS) &&
1448 NQNFS_CKINVALID(vp, np, ND_READ) &&
1449 np->n_lrev != np->n_brev) ||
1450 (!(nmp->nm_flag & NFSMNT_NQNFS) &&
1451 np->n_mtime != np->n_vattr.va_mtime.tv_sec))) {
1452 uprintf("Process killed due to text file modification\n");
1453 psignal(p, SIGKILL);
1458 uiop->uio_offset = (off_t)0;
1459 nfsstats.readlink_bios++;
1460 error = nfs_readlinkrpc(vp, uiop, cr);
1463 nfsstats.readdir_bios++;
1464 uiop->uio_offset = ((u_quad_t)bp->b_lblkno) * NFS_DIRBLKSIZ;
1465 if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
1466 error = nfs_readdirplusrpc(vp, uiop, cr);
1467 if (error == NFSERR_NOTSUPP)
1468 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
1470 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
1471 error = nfs_readdirrpc(vp, uiop, cr);
1473 * end-of-directory sets B_INVAL but does not generate an
1476 if (error == 0 && uiop->uio_resid == bp->b_bcount)
1477 bp->b_flags |= B_INVAL;
1480 printf("nfs_doio: type %x unexpected\n",vp->v_type);
1484 bp->b_flags |= B_ERROR;
1485 bp->b_error = error;
1489 * If we only need to commit, try to commit
1491 if (bp->b_flags & B_NEEDCOMMIT) {
1495 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE + bp->b_dirtyoff;
1496 bp->b_flags |= B_WRITEINPROG;
1498 bp->b_vp, off, bp->b_dirtyend-bp->b_dirtyoff,
1500 bp->b_flags &= ~B_WRITEINPROG;
1502 bp->b_dirtyoff = bp->b_dirtyend = 0;
1503 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1508 if (retv == NFSERR_STALEWRITEVERF) {
1509 nfs_clearcommit(bp->b_vp->v_mount);
1514 * Setup for actual write
1517 if ((off_t)bp->b_blkno * DEV_BSIZE + bp->b_dirtyend > np->n_size)
1518 bp->b_dirtyend = np->n_size - (off_t)bp->b_blkno * DEV_BSIZE;
1520 if (bp->b_dirtyend > bp->b_dirtyoff) {
1521 io.iov_len = uiop->uio_resid = bp->b_dirtyend
1523 uiop->uio_offset = (off_t)bp->b_blkno * DEV_BSIZE
1525 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
1526 uiop->uio_rw = UIO_WRITE;
1527 nfsstats.write_bios++;
1529 if ((bp->b_flags & (B_ASYNC | B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == B_ASYNC)
1530 iomode = NFSV3WRITE_UNSTABLE;
1532 iomode = NFSV3WRITE_FILESYNC;
1534 bp->b_flags |= B_WRITEINPROG;
1535 error = nfs_writerpc(vp, uiop, cr, &iomode, &must_commit);
1538 * When setting B_NEEDCOMMIT also set B_CLUSTEROK to try
1539 * to cluster the buffers needing commit. This will allow
1540 * the system to submit a single commit rpc for the whole
1541 * cluster. We can do this even if the buffer is not 100%
1542 * dirty (relative to the NFS blocksize), so we optimize the
1543 * append-to-file-case.
1545 * (when clearing B_NEEDCOMMIT, B_CLUSTEROK must also be
1546 * cleared because write clustering only works for commit
1547 * rpc's, not for the data portion of the write).
1550 if (!error && iomode == NFSV3WRITE_UNSTABLE) {
1551 bp->b_flags |= B_NEEDCOMMIT;
1552 if (bp->b_dirtyoff == 0
1553 && bp->b_dirtyend == bp->b_bcount)
1554 bp->b_flags |= B_CLUSTEROK;
1556 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1558 bp->b_flags &= ~B_WRITEINPROG;
1561 * For an interrupted write, the buffer is still valid
1562 * and the write hasn't been pushed to the server yet,
1563 * so we can't set B_ERROR and report the interruption
1564 * by setting B_EINTR. For the B_ASYNC case, B_EINTR
1565 * is not relevant, so the rpc attempt is essentially
1566 * a noop. For the case of a V3 write rpc not being
1567 * committed to stable storage, the block is still
1568 * dirty and requires either a commit rpc or another
1569 * write rpc with iomode == NFSV3WRITE_FILESYNC before
1570 * the block is reused. This is indicated by setting
1571 * the B_DELWRI and B_NEEDCOMMIT flags.
1573 * If the buffer is marked B_PAGING, it does not reside on
1574 * the vp's paging queues so we cannot call bdirty(). The
1575 * bp in this case is not an NFS cache block so we should
1579 || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
1583 bp->b_flags &= ~(B_INVAL|B_NOCACHE);
1584 if ((bp->b_flags & B_PAGING) == 0) {
1586 bp->b_flags &= ~B_DONE;
1588 if (error && (bp->b_flags & B_ASYNC) == 0)
1589 bp->b_flags |= B_EINTR;
1593 bp->b_flags |= B_ERROR;
1594 bp->b_error = np->n_error = error;
1595 np->n_flag |= NWRITEERR;
1597 bp->b_dirtyoff = bp->b_dirtyend = 0;
1605 bp->b_resid = uiop->uio_resid;
1607 nfs_clearcommit(vp->v_mount);
1613 * Used to aid in handling ftruncate() operations on the NFS client side.
1614 * Truncation creates a number of special problems for NFS. We have to
1615 * throw away VM pages and buffer cache buffers that are beyond EOF, and
1616 * we have to properly handle VM pages or (potentially dirty) buffers
1617 * that straddle the truncation point.
1621 nfs_meta_setsize(struct vnode *vp, struct ucred *cred, struct proc *p, u_quad_t nsize)
1623 struct nfsnode *np = VTONFS(vp);
1624 u_quad_t tsize = np->n_size;
1625 int biosize = vp->v_mount->mnt_stat.f_iosize;
1630 if (np->n_size < tsize) {
1636 * vtruncbuf() doesn't get the buffer overlapping the
1637 * truncation point. We may have a B_DELWRI and/or B_CACHE
1638 * buffer that now needs to be truncated.
1640 error = vtruncbuf(vp, cred, p, nsize, biosize);
1641 lbn = nsize / biosize;
1642 bufsize = nsize & (biosize - 1);
1643 bp = nfs_getcacheblk(vp, lbn, bufsize, p);
1644 if (bp->b_dirtyoff > bp->b_bcount)
1645 bp->b_dirtyoff = bp->b_bcount;
1646 if (bp->b_dirtyend > bp->b_bcount)
1647 bp->b_dirtyend = bp->b_bcount;
1648 bp->b_flags |= B_RELBUF; /* don't leave garbage around */
1651 vnode_pager_setsize(vp, nsize);