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: /repoman/r/ncvs/src/sys/nfsclient/nfs_bio.c,v 1.130 2004/04/14 23:23:55 peadar Exp $
38 * $DragonFly: src/sys/vfs/nfs/nfs_bio.c,v 1.19 2005/03/04 00:44:48 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>
52 #include <sys/msfbuf.h>
55 #include <vm/vm_extern.h>
56 #include <vm/vm_page.h>
57 #include <vm/vm_object.h>
58 #include <vm/vm_pager.h>
59 #include <vm/vnode_pager.h>
68 static struct buf *nfs_getcacheblk (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.
78 * nfs_getpages(struct vnode *a_vp, vm_page_t *a_m, int a_count,
79 * int a_reqpage, vm_ooffset_t a_offset)
82 nfs_getpages(struct vop_getpages_args *ap)
84 struct thread *td = curthread; /* XXX */
85 int i, error, nextoff, size, toff, count, npages;
96 nmp = VFSTONFS(vp->v_mount);
100 if (vp->v_object == NULL) {
101 printf("nfs_getpages: called with non-merged cache vnode??\n");
102 return VM_PAGER_ERROR;
105 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
106 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
107 (void)nfs_fsinfo(nmp, vp, td);
109 npages = btoc(count);
112 * NOTE that partially valid pages may occur in cases other
113 * then file EOF, such as when a file is partially written and
114 * ftruncate()-extended to a larger size. It is also possible
115 * for the valid bits to be set on garbage beyond the file EOF and
116 * clear in the area before EOF (e.g. m->valid == 0xfc), which can
117 * occur due to vtruncbuf() and the buffer cache's handling of
118 * pages which 'straddle' buffers or when b_bufsize is not a
119 * multiple of PAGE_SIZE.... the buffer cache cannot normally
120 * clear the extra bits. This kind of situation occurs when you
121 * make a small write() (m->valid == 0x03) and then mmap() and
122 * fault in the buffer(m->valid = 0xFF). When NFS flushes the
123 * buffer (vinvalbuf() m->valid = 0xFC) we are left with a mess.
125 * This is combined with the possibility that the pages are partially
126 * dirty or that there is a buffer backing the pages that is dirty
127 * (even if m->dirty is 0).
129 * To solve this problem several hacks have been made: (1) NFS
130 * guarentees that the IO block size is a multiple of PAGE_SIZE and
131 * (2) The buffer cache, when invalidating an NFS buffer, will
132 * disregard the buffer's fragmentory b_bufsize and invalidate
133 * the whole page rather then just the piece the buffer owns.
135 * This allows us to assume that a partially valid page found here
136 * is fully valid (vm_fault will zero'd out areas of the page not
139 m = pages[ap->a_reqpage];
141 for (i = 0; i < npages; ++i) {
142 if (i != ap->a_reqpage)
143 vnode_pager_freepage(pages[i]);
149 * Use an MSF_BUF as a medium to retrieve data from the pages.
151 msf_map_pagelist(&msf, pages, npages, 0);
153 kva = msf_buf_kva(msf);
155 iov.iov_base = (caddr_t) kva;
159 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
160 uio.uio_resid = count;
161 uio.uio_segflg = UIO_SYSSPACE;
162 uio.uio_rw = UIO_READ;
165 error = nfs_readrpc(vp, &uio);
168 if (error && (uio.uio_resid == count)) {
169 printf("nfs_getpages: error %d\n", error);
170 for (i = 0; i < npages; ++i) {
171 if (i != ap->a_reqpage)
172 vnode_pager_freepage(pages[i]);
174 return VM_PAGER_ERROR;
178 * Calculate the number of bytes read and validate only that number
179 * of bytes. Note that due to pending writes, size may be 0. This
180 * does not mean that the remaining data is invalid!
183 size = count - uio.uio_resid;
185 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
186 nextoff = toff + PAGE_SIZE;
189 m->flags &= ~PG_ZERO;
191 if (nextoff <= size) {
193 * Read operation filled an entire page
195 m->valid = VM_PAGE_BITS_ALL;
197 } else if (size > toff) {
199 * Read operation filled a partial page.
202 vm_page_set_validclean(m, 0, size - toff);
203 /* handled by vm_fault now */
204 /* vm_page_zero_invalid(m, TRUE); */
207 * Read operation was short. If no error occured
208 * we may have hit a zero-fill section. We simply
209 * leave valid set to 0.
213 if (i != ap->a_reqpage) {
215 * Whether or not to leave the page activated is up in
216 * the air, but we should put the page on a page queue
217 * somewhere (it already is in the object). Result:
218 * It appears that emperical results show that
219 * deactivating pages is best.
223 * Just in case someone was asking for this page we
224 * now tell them that it is ok to use.
227 if (m->flags & PG_WANTED)
230 vm_page_deactivate(m);
233 vnode_pager_freepage(m);
241 * Vnode op for VM putpages.
243 * nfs_putpages(struct vnode *a_vp, vm_page_t *a_m, int a_count, int a_sync,
244 * int *a_rtvals, vm_ooffset_t a_offset)
247 nfs_putpages(struct vop_putpages_args *ap)
249 struct thread *td = curthread;
253 int iomode, must_commit, i, error, npages, count;
257 struct nfsmount *nmp;
264 nmp = VFSTONFS(vp->v_mount);
267 rtvals = ap->a_rtvals;
268 npages = btoc(count);
269 offset = IDX_TO_OFF(pages[0]->pindex);
271 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
272 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
273 (void)nfs_fsinfo(nmp, vp, td);
275 for (i = 0; i < npages; i++) {
276 rtvals[i] = VM_PAGER_AGAIN;
280 * When putting pages, do not extend file past EOF.
283 if (offset + count > np->n_size) {
284 count = np->n_size - offset;
290 * Use an MSF_BUF as a medium to retrieve data from the pages.
292 msf_map_pagelist(&msf, pages, npages, 0);
294 kva = msf_buf_kva(msf);
296 iov.iov_base = (caddr_t) kva;
300 uio.uio_offset = offset;
301 uio.uio_resid = count;
302 uio.uio_segflg = UIO_SYSSPACE;
303 uio.uio_rw = UIO_WRITE;
306 if ((ap->a_sync & VM_PAGER_PUT_SYNC) == 0)
307 iomode = NFSV3WRITE_UNSTABLE;
309 iomode = NFSV3WRITE_FILESYNC;
311 error = nfs_writerpc(vp, &uio, &iomode, &must_commit);
316 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
317 for (i = 0; i < nwritten; i++) {
318 rtvals[i] = VM_PAGER_OK;
319 vm_page_undirty(pages[i]);
322 nfs_clearcommit(vp->v_mount);
328 * Vnode op for read using bio
331 nfs_bioread(struct vnode *vp, struct uio *uio, int ioflag)
333 struct nfsnode *np = VTONFS(vp);
335 struct buf *bp = 0, *rabp;
338 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
342 int nra, error = 0, n = 0, on = 0;
345 if (uio->uio_rw != UIO_READ)
346 panic("nfs_read mode");
348 if (uio->uio_resid == 0)
350 if (uio->uio_offset < 0) /* XXX VDIR cookies can be negative */
354 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
355 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
356 (void)nfs_fsinfo(nmp, vp, td);
357 if (vp->v_type != VDIR &&
358 (uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
360 biosize = vp->v_mount->mnt_stat.f_iosize;
361 seqcount = (int)((off_t)(ioflag >> IO_SEQSHIFT) * biosize / BKVASIZE);
363 * For nfs, cache consistency can only be maintained approximately.
364 * Although RFC1094 does not specify the criteria, the following is
365 * believed to be compatible with the reference port.
366 * For nqnfs, full cache consistency is maintained within the loop.
368 * If the file's modify time on the server has changed since the
369 * last read rpc or you have written to the file,
370 * you may have lost data cache consistency with the
371 * server, so flush all of the file's data out of the cache.
372 * Then force a getattr rpc to ensure that you have up to date
374 * NB: This implies that cache data can be read when up to
375 * NFS_ATTRTIMEO seconds out of date. If you find that you need current
376 * attributes this could be forced by setting n_attrstamp to 0 before
377 * the VOP_GETATTR() call.
379 if ((nmp->nm_flag & NFSMNT_NQNFS) == 0) {
380 if (np->n_flag & NMODIFIED) {
381 if (vp->v_type != VREG) {
382 if (vp->v_type != VDIR)
383 panic("nfs: bioread, not dir");
385 error = nfs_vinvalbuf(vp, V_SAVE, td, 1);
390 error = VOP_GETATTR(vp, &vattr, td);
393 np->n_mtime = vattr.va_mtime.tv_sec;
395 error = VOP_GETATTR(vp, &vattr, td);
398 if ((np->n_flag & NSIZECHANGED)
399 || np->n_mtime != vattr.va_mtime.tv_sec) {
400 if (vp->v_type == VDIR)
402 error = nfs_vinvalbuf(vp, V_SAVE, td, 1);
405 np->n_mtime = vattr.va_mtime.tv_sec;
406 np->n_flag &= ~NSIZECHANGED;
413 * Get a valid lease. If cached data is stale, flush it.
415 if (nmp->nm_flag & NFSMNT_NQNFS) {
416 if (NQNFS_CKINVALID(vp, np, ND_READ)) {
418 error = nqnfs_getlease(vp, ND_READ, td);
419 } while (error == NQNFS_EXPIRED);
422 if (np->n_lrev != np->n_brev ||
423 (np->n_flag & NQNFSNONCACHE) ||
424 ((np->n_flag & NMODIFIED) && vp->v_type == VDIR)) {
425 if (vp->v_type == VDIR)
427 error = nfs_vinvalbuf(vp, V_SAVE, td, 1);
430 np->n_brev = np->n_lrev;
432 } else if (vp->v_type == VDIR && (np->n_flag & NMODIFIED)) {
434 error = nfs_vinvalbuf(vp, V_SAVE, td, 1);
439 if (np->n_flag & NQNFSNONCACHE) {
440 switch (vp->v_type) {
442 return (nfs_readrpc(vp, uio));
444 return (nfs_readlinkrpc(vp, uio));
448 printf(" NQNFSNONCACHE: type %x unexpected\n",
452 switch (vp->v_type) {
454 nfsstats.biocache_reads++;
455 lbn = uio->uio_offset / biosize;
456 on = uio->uio_offset & (biosize - 1);
459 * Start the read ahead(s), as required.
461 if (nfs_numasync > 0 && nmp->nm_readahead > 0) {
462 for (nra = 0; nra < nmp->nm_readahead && nra < seqcount &&
463 (off_t)(lbn + 1 + nra) * biosize < np->n_size; nra++) {
464 rabn = lbn + 1 + nra;
465 if (!incore(vp, rabn)) {
466 rabp = nfs_getcacheblk(vp, rabn, biosize, td);
469 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
470 rabp->b_flags |= (B_READ | B_ASYNC);
471 vfs_busy_pages(rabp, 0);
472 if (nfs_asyncio(rabp, td)) {
473 rabp->b_flags |= B_INVAL|B_ERROR;
474 vfs_unbusy_pages(rabp);
486 * Obtain the buffer cache block. Figure out the buffer size
487 * when we are at EOF. If we are modifying the size of the
488 * buffer based on an EOF condition we need to hold
489 * nfs_rslock() through obtaining the buffer to prevent
490 * a potential writer-appender from messing with n_size.
491 * Otherwise we may accidently truncate the buffer and
494 * Note that bcount is *not* DEV_BSIZE aligned.
499 if ((off_t)lbn * biosize >= np->n_size) {
501 } else if ((off_t)(lbn + 1) * biosize > np->n_size) {
502 bcount = np->n_size - (off_t)lbn * biosize;
504 if (bcount != biosize) {
505 switch(nfs_rslock(np, td)) {
518 bp = nfs_getcacheblk(vp, lbn, bcount, td);
520 if (bcount != biosize)
521 nfs_rsunlock(np, td);
526 * If B_CACHE is not set, we must issue the read. If this
527 * fails, we return an error.
530 if ((bp->b_flags & B_CACHE) == 0) {
531 bp->b_flags |= B_READ;
532 vfs_busy_pages(bp, 0);
533 error = nfs_doio(bp, td);
541 * on is the offset into the current bp. Figure out how many
542 * bytes we can copy out of the bp. Note that bcount is
543 * NOT DEV_BSIZE aligned.
545 * Then figure out how many bytes we can copy into the uio.
550 n = min((unsigned)(bcount - on), uio->uio_resid);
553 nfsstats.biocache_readlinks++;
554 bp = nfs_getcacheblk(vp, (daddr_t)0, NFS_MAXPATHLEN, td);
557 if ((bp->b_flags & B_CACHE) == 0) {
558 bp->b_flags |= B_READ;
559 vfs_busy_pages(bp, 0);
560 error = nfs_doio(bp, td);
562 bp->b_flags |= B_ERROR;
567 n = min(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid);
571 nfsstats.biocache_readdirs++;
572 if (np->n_direofoffset
573 && uio->uio_offset >= np->n_direofoffset) {
576 lbn = (uoff_t)uio->uio_offset / NFS_DIRBLKSIZ;
577 on = uio->uio_offset & (NFS_DIRBLKSIZ - 1);
578 bp = nfs_getcacheblk(vp, lbn, NFS_DIRBLKSIZ, td);
581 if ((bp->b_flags & B_CACHE) == 0) {
582 bp->b_flags |= B_READ;
583 vfs_busy_pages(bp, 0);
584 error = nfs_doio(bp, td);
588 while (error == NFSERR_BAD_COOKIE) {
589 printf("got bad cookie vp %p bp %p\n", vp, bp);
591 error = nfs_vinvalbuf(vp, 0, td, 1);
593 * Yuck! The directory has been modified on the
594 * server. The only way to get the block is by
595 * reading from the beginning to get all the
598 * Leave the last bp intact unless there is an error.
599 * Loop back up to the while if the error is another
600 * NFSERR_BAD_COOKIE (double yuch!).
602 for (i = 0; i <= lbn && !error; i++) {
603 if (np->n_direofoffset
604 && (i * NFS_DIRBLKSIZ) >= np->n_direofoffset)
606 bp = nfs_getcacheblk(vp, i, NFS_DIRBLKSIZ, td);
609 if ((bp->b_flags & B_CACHE) == 0) {
610 bp->b_flags |= B_READ;
611 vfs_busy_pages(bp, 0);
612 error = nfs_doio(bp, td);
614 * no error + B_INVAL == directory EOF,
617 if (error == 0 && (bp->b_flags & B_INVAL))
621 * An error will throw away the block and the
622 * for loop will break out. If no error and this
623 * is not the block we want, we throw away the
624 * block and go for the next one via the for loop.
626 if (error || i < lbn)
631 * The above while is repeated if we hit another cookie
632 * error. If we hit an error and it wasn't a cookie error,
640 * If not eof and read aheads are enabled, start one.
641 * (You need the current block first, so that you have the
642 * directory offset cookie of the next block.)
644 if (nfs_numasync > 0 && nmp->nm_readahead > 0 &&
645 (bp->b_flags & B_INVAL) == 0 &&
646 (np->n_direofoffset == 0 ||
647 (lbn + 1) * NFS_DIRBLKSIZ < np->n_direofoffset) &&
648 !(np->n_flag & NQNFSNONCACHE) &&
649 !incore(vp, lbn + 1)) {
650 rabp = nfs_getcacheblk(vp, lbn + 1, NFS_DIRBLKSIZ, td);
652 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
653 rabp->b_flags |= (B_READ | B_ASYNC);
654 vfs_busy_pages(rabp, 0);
655 if (nfs_asyncio(rabp, td)) {
656 rabp->b_flags |= B_INVAL|B_ERROR;
657 vfs_unbusy_pages(rabp);
666 * Unlike VREG files, whos buffer size ( bp->b_bcount ) is
667 * chopped for the EOF condition, we cannot tell how large
668 * NFS directories are going to be until we hit EOF. So
669 * an NFS directory buffer is *not* chopped to its EOF. Now,
670 * it just so happens that b_resid will effectively chop it
671 * to EOF. *BUT* this information is lost if the buffer goes
672 * away and is reconstituted into a B_CACHE state ( due to
673 * being VMIO ) later. So we keep track of the directory eof
674 * in np->n_direofoffset and chop it off as an extra step
677 n = lmin(uio->uio_resid, NFS_DIRBLKSIZ - bp->b_resid - on);
678 if (np->n_direofoffset && n > np->n_direofoffset - uio->uio_offset)
679 n = np->n_direofoffset - uio->uio_offset;
682 printf(" nfs_bioread: type %x unexpected\n",vp->v_type);
687 error = uiomove(bp->b_data + on, (int)n, uio);
689 switch (vp->v_type) {
697 * Invalidate buffer if caching is disabled, forcing a
698 * re-read from the remote later.
700 if (np->n_flag & NQNFSNONCACHE)
701 bp->b_flags |= B_INVAL;
704 printf(" nfs_bioread: type %x unexpected\n",vp->v_type);
707 } while (error == 0 && uio->uio_resid > 0 && n > 0);
712 * Vnode op for write using bio
714 * nfs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
715 * struct ucred *a_cred)
718 nfs_write(struct vop_write_args *ap)
721 struct uio *uio = ap->a_uio;
722 struct thread *td = uio->uio_td;
723 struct vnode *vp = ap->a_vp;
724 struct nfsnode *np = VTONFS(vp);
725 int ioflag = ap->a_ioflag;
728 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
731 int n, on, error = 0, iomode, must_commit;
735 if (uio->uio_rw != UIO_WRITE)
736 panic("nfs_write mode");
737 if (uio->uio_segflg == UIO_USERSPACE && uio->uio_td != curthread)
738 panic("nfs_write proc");
740 if (vp->v_type != VREG)
742 if (np->n_flag & NWRITEERR) {
743 np->n_flag &= ~NWRITEERR;
744 return (np->n_error);
746 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
747 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
748 (void)nfs_fsinfo(nmp, vp, td);
751 * Synchronously flush pending buffers if we are in synchronous
752 * mode or if we are appending.
754 if (ioflag & (IO_APPEND | IO_SYNC)) {
755 if (np->n_flag & NMODIFIED) {
757 error = nfs_vinvalbuf(vp, V_SAVE, td, 1);
764 * If IO_APPEND then load uio_offset. We restart here if we cannot
765 * get the append lock.
768 if (ioflag & IO_APPEND) {
770 error = VOP_GETATTR(vp, &vattr, td);
773 uio->uio_offset = np->n_size;
776 if (uio->uio_offset < 0)
778 if ((uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
780 if (uio->uio_resid == 0)
784 * We need to obtain the rslock if we intend to modify np->n_size
785 * in order to guarentee the append point with multiple contending
786 * writers, to guarentee that no other appenders modify n_size
787 * while we are trying to obtain a truncated buffer (i.e. to avoid
788 * accidently truncating data written by another appender due to
789 * the race), and to ensure that the buffer is populated prior to
790 * our extending of the file. We hold rslock through the entire
793 * Note that we do not synchronize the case where someone truncates
794 * the file while we are appending to it because attempting to lock
795 * this case may deadlock other parts of the system unexpectedly.
797 if ((ioflag & IO_APPEND) ||
798 uio->uio_offset + uio->uio_resid > np->n_size) {
799 switch(nfs_rslock(np, td)) {
814 * Maybe this should be above the vnode op call, but so long as
815 * file servers have no limits, i don't think it matters
817 if (td->td_proc && uio->uio_offset + uio->uio_resid >
818 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
819 psignal(td->td_proc, SIGXFSZ);
821 nfs_rsunlock(np, td);
825 biosize = vp->v_mount->mnt_stat.f_iosize;
829 * Check for a valid write lease.
831 if ((nmp->nm_flag & NFSMNT_NQNFS) &&
832 NQNFS_CKINVALID(vp, np, ND_WRITE)) {
834 error = nqnfs_getlease(vp, ND_WRITE, td);
835 } while (error == NQNFS_EXPIRED);
838 if (np->n_lrev != np->n_brev ||
839 (np->n_flag & NQNFSNONCACHE)) {
840 error = nfs_vinvalbuf(vp, V_SAVE, td, 1);
843 np->n_brev = np->n_lrev;
846 if ((np->n_flag & NQNFSNONCACHE) && uio->uio_iovcnt == 1) {
847 iomode = NFSV3WRITE_FILESYNC;
848 error = nfs_writerpc(vp, uio, &iomode, &must_commit);
850 nfs_clearcommit(vp->v_mount);
853 nfsstats.biocache_writes++;
854 lbn = uio->uio_offset / biosize;
855 on = uio->uio_offset & (biosize-1);
856 n = min((unsigned)(biosize - on), uio->uio_resid);
859 * Handle direct append and file extension cases, calculate
860 * unaligned buffer size.
863 if (uio->uio_offset == np->n_size && n) {
865 * Get the buffer (in its pre-append state to maintain
866 * B_CACHE if it was previously set). Resize the
867 * nfsnode after we have locked the buffer to prevent
868 * readers from reading garbage.
871 bp = nfs_getcacheblk(vp, lbn, bcount, td);
876 np->n_size = uio->uio_offset + n;
877 np->n_flag |= NMODIFIED;
878 vnode_pager_setsize(vp, np->n_size);
880 save = bp->b_flags & B_CACHE;
882 allocbuf(bp, bcount);
887 * Obtain the locked cache block first, and then
888 * adjust the file's size as appropriate.
891 if ((off_t)lbn * biosize + bcount < np->n_size) {
892 if ((off_t)(lbn + 1) * biosize < np->n_size)
895 bcount = np->n_size - (off_t)lbn * biosize;
897 bp = nfs_getcacheblk(vp, lbn, bcount, td);
898 if (uio->uio_offset + n > np->n_size) {
899 np->n_size = uio->uio_offset + n;
900 np->n_flag |= NMODIFIED;
901 vnode_pager_setsize(vp, np->n_size);
911 * Issue a READ if B_CACHE is not set. In special-append
912 * mode, B_CACHE is based on the buffer prior to the write
913 * op and is typically set, avoiding the read. If a read
914 * is required in special append mode, the server will
915 * probably send us a short-read since we extended the file
916 * on our end, resulting in b_resid == 0 and, thusly,
917 * B_CACHE getting set.
919 * We can also avoid issuing the read if the write covers
920 * the entire buffer. We have to make sure the buffer state
921 * is reasonable in this case since we will not be initiating
922 * I/O. See the comments in kern/vfs_bio.c's getblk() for
925 * B_CACHE may also be set due to the buffer being cached
929 if (on == 0 && n == bcount) {
930 bp->b_flags |= B_CACHE;
931 bp->b_flags &= ~(B_ERROR | B_INVAL);
934 if ((bp->b_flags & B_CACHE) == 0) {
935 bp->b_flags |= B_READ;
936 vfs_busy_pages(bp, 0);
937 error = nfs_doio(bp, td);
947 np->n_flag |= NMODIFIED;
950 * If dirtyend exceeds file size, chop it down. This should
951 * not normally occur but there is an append race where it
952 * might occur XXX, so we log it.
954 * If the chopping creates a reverse-indexed or degenerate
955 * situation with dirtyoff/end, we 0 both of them.
958 if (bp->b_dirtyend > bcount) {
959 printf("NFS append race @%lx:%d\n",
960 (long)bp->b_blkno * DEV_BSIZE,
961 bp->b_dirtyend - bcount);
962 bp->b_dirtyend = bcount;
965 if (bp->b_dirtyoff >= bp->b_dirtyend)
966 bp->b_dirtyoff = bp->b_dirtyend = 0;
969 * If the new write will leave a contiguous dirty
970 * area, just update the b_dirtyoff and b_dirtyend,
971 * otherwise force a write rpc of the old dirty area.
973 * While it is possible to merge discontiguous writes due to
974 * our having a B_CACHE buffer ( and thus valid read data
975 * for the hole), we don't because it could lead to
976 * significant cache coherency problems with multiple clients,
977 * especially if locking is implemented later on.
979 * as an optimization we could theoretically maintain
980 * a linked list of discontinuous areas, but we would still
981 * have to commit them separately so there isn't much
982 * advantage to it except perhaps a bit of asynchronization.
985 if (bp->b_dirtyend > 0 &&
986 (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) {
987 if (VOP_BWRITE(bp->b_vp, bp) == EINTR) {
995 * Check for valid write lease and get one as required.
996 * In case getblk() and/or bwrite() delayed us.
998 if ((nmp->nm_flag & NFSMNT_NQNFS) &&
999 NQNFS_CKINVALID(vp, np, ND_WRITE)) {
1001 error = nqnfs_getlease(vp, ND_WRITE, td);
1002 } while (error == NQNFS_EXPIRED);
1007 if (np->n_lrev != np->n_brev ||
1008 (np->n_flag & NQNFSNONCACHE)) {
1010 error = nfs_vinvalbuf(vp, V_SAVE, td, 1);
1013 np->n_brev = np->n_lrev;
1018 error = uiomove((char *)bp->b_data + on, n, uio);
1021 * Since this block is being modified, it must be written
1022 * again and not just committed. Since write clustering does
1023 * not work for the stage 1 data write, only the stage 2
1024 * commit rpc, we have to clear B_CLUSTEROK as well.
1026 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1029 bp->b_flags |= B_ERROR;
1035 * Only update dirtyoff/dirtyend if not a degenerate
1039 if (bp->b_dirtyend > 0) {
1040 bp->b_dirtyoff = min(on, bp->b_dirtyoff);
1041 bp->b_dirtyend = max((on + n), bp->b_dirtyend);
1043 bp->b_dirtyoff = on;
1044 bp->b_dirtyend = on + n;
1046 vfs_bio_set_validclean(bp, on, n);
1049 * If IO_NOWDRAIN then set B_NOWDRAIN (e.g. nfs-backed VN
1050 * filesystem). XXX also use for loopback NFS mounts.
1052 if (ioflag & IO_NOWDRAIN)
1053 bp->b_flags |= B_NOWDRAIN;
1056 * If the lease is non-cachable or IO_SYNC do bwrite().
1058 * IO_INVAL appears to be unused. The idea appears to be
1059 * to turn off caching in this case. Very odd. XXX
1061 if ((np->n_flag & NQNFSNONCACHE) || (ioflag & IO_SYNC)) {
1062 if (ioflag & IO_INVAL)
1063 bp->b_flags |= B_NOCACHE;
1064 error = VOP_BWRITE(bp->b_vp, bp);
1067 if (np->n_flag & NQNFSNONCACHE) {
1068 error = nfs_vinvalbuf(vp, V_SAVE, td, 1);
1072 } else if ((n + on) == biosize &&
1073 (nmp->nm_flag & NFSMNT_NQNFS) == 0) {
1074 bp->b_flags |= B_ASYNC;
1075 (void)nfs_writebp(bp, 0, 0);
1079 } while (uio->uio_resid > 0 && n > 0);
1082 nfs_rsunlock(np, td);
1088 * Get an nfs cache block.
1090 * Allocate a new one if the block isn't currently in the cache
1091 * and return the block marked busy. If the calling process is
1092 * interrupted by a signal for an interruptible mount point, return
1095 * The caller must carefully deal with the possible B_INVAL state of
1096 * the buffer. nfs_doio() clears B_INVAL (and nfs_asyncio() clears it
1097 * indirectly), so synchronous reads can be issued without worrying about
1098 * the B_INVAL state. We have to be a little more careful when dealing
1099 * with writes (see comments in nfs_write()) when extending a file past
1103 nfs_getcacheblk(struct vnode *vp, daddr_t bn, int size, struct thread *td)
1107 struct nfsmount *nmp;
1112 if (nmp->nm_flag & NFSMNT_INT) {
1113 bp = getblk(vp, bn, size, PCATCH, 0);
1114 while (bp == (struct buf *)0) {
1115 if (nfs_sigintr(nmp, (struct nfsreq *)0, td))
1116 return ((struct buf *)0);
1117 bp = getblk(vp, bn, size, 0, 2 * hz);
1120 bp = getblk(vp, bn, size, 0, 0);
1123 if (vp->v_type == VREG) {
1126 biosize = mp->mnt_stat.f_iosize;
1127 bp->b_blkno = bn * (biosize / DEV_BSIZE);
1133 * Flush and invalidate all dirty buffers. If another process is already
1134 * doing the flush, just wait for completion.
1137 nfs_vinvalbuf(struct vnode *vp, int flags,
1138 struct thread *td, int intrflg)
1140 struct nfsnode *np = VTONFS(vp);
1141 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1142 int error = 0, slpflag, slptimeo;
1144 if (vp->v_flag & VRECLAIMED)
1147 if ((nmp->nm_flag & NFSMNT_INT) == 0)
1157 * First wait for any other process doing a flush to complete.
1159 while (np->n_flag & NFLUSHINPROG) {
1160 np->n_flag |= NFLUSHWANT;
1161 error = tsleep((caddr_t)&np->n_flag, 0, "nfsvinval", slptimeo);
1162 if (error && intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, td))
1167 * Now, flush as required.
1169 np->n_flag |= NFLUSHINPROG;
1170 error = vinvalbuf(vp, flags, td, slpflag, 0);
1172 if (intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, td)) {
1173 np->n_flag &= ~NFLUSHINPROG;
1174 if (np->n_flag & NFLUSHWANT) {
1175 np->n_flag &= ~NFLUSHWANT;
1176 wakeup((caddr_t)&np->n_flag);
1180 error = vinvalbuf(vp, flags, td, 0, slptimeo);
1182 np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
1183 if (np->n_flag & NFLUSHWANT) {
1184 np->n_flag &= ~NFLUSHWANT;
1185 wakeup((caddr_t)&np->n_flag);
1191 * Initiate asynchronous I/O. Return an error if no nfsiods are available.
1192 * This is mainly to avoid queueing async I/O requests when the nfsiods
1193 * are all hung on a dead server.
1195 * Note: nfs_asyncio() does not clear (B_ERROR|B_INVAL) but when the bp
1196 * is eventually dequeued by the async daemon, nfs_doio() *will*.
1199 nfs_asyncio(struct buf *bp, struct thread *td)
1201 struct nfsmount *nmp;
1209 * If no async daemons then return EIO to force caller to run the rpc
1212 if (nfs_numasync == 0)
1215 nmp = VFSTONFS(bp->b_vp->v_mount);
1218 * Commits are usually short and sweet so lets save some cpu and
1219 * leave the async daemons for more important rpc's (such as reads
1222 if ((bp->b_flags & (B_READ|B_NEEDCOMMIT)) == B_NEEDCOMMIT &&
1223 (nmp->nm_bufqiods > nfs_numasync / 2)) {
1228 if (nmp->nm_flag & NFSMNT_INT)
1233 * Find a free iod to process this request.
1235 for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
1236 if (nfs_iodwant[i]) {
1238 * Found one, so wake it up and tell it which
1242 ("nfs_asyncio: waking iod %d for mount %p\n",
1244 nfs_iodwant[i] = NULL;
1245 nfs_iodmount[i] = nmp;
1247 wakeup((caddr_t)&nfs_iodwant[i]);
1253 * If none are free, we may already have an iod working on this mount
1254 * point. If so, it will process our request.
1257 if (nmp->nm_bufqiods > 0) {
1259 ("nfs_asyncio: %d iods are already processing mount %p\n",
1260 nmp->nm_bufqiods, nmp));
1266 * If we have an iod which can process the request, then queue
1271 * Ensure that the queue never grows too large. We still want
1272 * to asynchronize so we block rather then return EIO.
1274 while (nmp->nm_bufqlen >= 2*nfs_numasync) {
1276 ("nfs_asyncio: waiting for mount %p queue to drain\n", nmp));
1277 nmp->nm_bufqwant = TRUE;
1278 error = tsleep(&nmp->nm_bufq, slpflag,
1279 "nfsaio", slptimeo);
1281 if (nfs_sigintr(nmp, NULL, td))
1283 if (slpflag == PCATCH) {
1289 * We might have lost our iod while sleeping,
1290 * so check and loop if nescessary.
1292 if (nmp->nm_bufqiods == 0) {
1294 ("nfs_asyncio: no iods after mount %p queue was drained, looping\n", nmp));
1299 if ((bp->b_flags & B_READ) == 0)
1300 bp->b_flags |= B_WRITEINPROG;
1303 TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist);
1309 * All the iods are busy on other mounts, so return EIO to
1310 * force the caller to process the i/o synchronously.
1312 NFS_DPF(ASYNCIO, ("nfs_asyncio: no iods available, i/o is synchronous\n"));
1317 * Do an I/O operation to/from a cache block. This may be called
1318 * synchronously or from an nfsiod.
1320 * NOTE! TD MIGHT BE NULL
1323 nfs_doio(struct buf *bp, struct thread *td)
1328 struct nfsmount *nmp;
1329 int error = 0, iomode, must_commit = 0;
1335 nmp = VFSTONFS(vp->v_mount);
1337 uiop->uio_iov = &io;
1338 uiop->uio_iovcnt = 1;
1339 uiop->uio_segflg = UIO_SYSSPACE;
1343 * clear B_ERROR and B_INVAL state prior to initiating the I/O. We
1344 * do this here so we do not have to do it in all the code that
1347 bp->b_flags &= ~(B_ERROR | B_INVAL);
1349 KASSERT(!(bp->b_flags & B_DONE), ("nfs_doio: bp %p already marked done", bp));
1352 * Historically, paging was done with physio, but no more.
1354 if (bp->b_flags & B_PHYS) {
1356 * ...though reading /dev/drum still gets us here.
1358 io.iov_len = uiop->uio_resid = bp->b_bcount;
1359 /* mapping was done by vmapbuf() */
1360 io.iov_base = bp->b_data;
1361 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
1362 if (bp->b_flags & B_READ) {
1363 uiop->uio_rw = UIO_READ;
1364 nfsstats.read_physios++;
1365 error = nfs_readrpc(vp, uiop);
1369 iomode = NFSV3WRITE_DATASYNC;
1370 uiop->uio_rw = UIO_WRITE;
1371 nfsstats.write_physios++;
1372 error = nfs_writerpc(vp, uiop, &iomode, &com);
1375 bp->b_flags |= B_ERROR;
1376 bp->b_error = error;
1378 } else if (bp->b_flags & B_READ) {
1379 io.iov_len = uiop->uio_resid = bp->b_bcount;
1380 io.iov_base = bp->b_data;
1381 uiop->uio_rw = UIO_READ;
1383 switch (vp->v_type) {
1385 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
1386 nfsstats.read_bios++;
1387 error = nfs_readrpc(vp, uiop);
1390 if (uiop->uio_resid) {
1392 * If we had a short read with no error, we must have
1393 * hit a file hole. We should zero-fill the remainder.
1394 * This can also occur if the server hits the file EOF.
1396 * Holes used to be able to occur due to pending
1397 * writes, but that is not possible any longer.
1399 int nread = bp->b_bcount - uiop->uio_resid;
1400 int left = uiop->uio_resid;
1403 bzero((char *)bp->b_data + nread, left);
1404 uiop->uio_resid = 0;
1407 if (td && td->td_proc && (vp->v_flag & VTEXT) &&
1408 (((nmp->nm_flag & NFSMNT_NQNFS) &&
1409 NQNFS_CKINVALID(vp, np, ND_READ) &&
1410 np->n_lrev != np->n_brev) ||
1411 (!(nmp->nm_flag & NFSMNT_NQNFS) &&
1412 np->n_mtime != np->n_vattr.va_mtime.tv_sec))) {
1413 uprintf("Process killed due to text file modification\n");
1414 psignal(td->td_proc, SIGKILL);
1419 uiop->uio_offset = (off_t)0;
1420 nfsstats.readlink_bios++;
1421 error = nfs_readlinkrpc(vp, uiop);
1424 nfsstats.readdir_bios++;
1425 uiop->uio_offset = ((u_quad_t)bp->b_lblkno) * NFS_DIRBLKSIZ;
1426 if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
1427 error = nfs_readdirplusrpc(vp, uiop);
1428 if (error == NFSERR_NOTSUPP)
1429 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
1431 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
1432 error = nfs_readdirrpc(vp, uiop);
1434 * end-of-directory sets B_INVAL but does not generate an
1437 if (error == 0 && uiop->uio_resid == bp->b_bcount)
1438 bp->b_flags |= B_INVAL;
1441 printf("nfs_doio: type %x unexpected\n",vp->v_type);
1445 bp->b_flags |= B_ERROR;
1446 bp->b_error = error;
1450 * If we only need to commit, try to commit
1452 if (bp->b_flags & B_NEEDCOMMIT) {
1456 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE + bp->b_dirtyoff;
1457 bp->b_flags |= B_WRITEINPROG;
1458 retv = nfs_commit(bp->b_vp, off,
1459 bp->b_dirtyend - bp->b_dirtyoff, td);
1460 bp->b_flags &= ~B_WRITEINPROG;
1462 bp->b_dirtyoff = bp->b_dirtyend = 0;
1463 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1468 if (retv == NFSERR_STALEWRITEVERF) {
1469 nfs_clearcommit(bp->b_vp->v_mount);
1474 * Setup for actual write
1477 if ((off_t)bp->b_blkno * DEV_BSIZE + bp->b_dirtyend > np->n_size)
1478 bp->b_dirtyend = np->n_size - (off_t)bp->b_blkno * DEV_BSIZE;
1480 if (bp->b_dirtyend > bp->b_dirtyoff) {
1481 io.iov_len = uiop->uio_resid = bp->b_dirtyend
1483 uiop->uio_offset = (off_t)bp->b_blkno * DEV_BSIZE
1485 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
1486 uiop->uio_rw = UIO_WRITE;
1487 nfsstats.write_bios++;
1489 if ((bp->b_flags & (B_ASYNC | B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == B_ASYNC)
1490 iomode = NFSV3WRITE_UNSTABLE;
1492 iomode = NFSV3WRITE_FILESYNC;
1494 bp->b_flags |= B_WRITEINPROG;
1495 error = nfs_writerpc(vp, uiop, &iomode, &must_commit);
1498 * When setting B_NEEDCOMMIT also set B_CLUSTEROK to try
1499 * to cluster the buffers needing commit. This will allow
1500 * the system to submit a single commit rpc for the whole
1501 * cluster. We can do this even if the buffer is not 100%
1502 * dirty (relative to the NFS blocksize), so we optimize the
1503 * append-to-file-case.
1505 * (when clearing B_NEEDCOMMIT, B_CLUSTEROK must also be
1506 * cleared because write clustering only works for commit
1507 * rpc's, not for the data portion of the write).
1510 if (!error && iomode == NFSV3WRITE_UNSTABLE) {
1511 bp->b_flags |= B_NEEDCOMMIT;
1512 if (bp->b_dirtyoff == 0
1513 && bp->b_dirtyend == bp->b_bcount)
1514 bp->b_flags |= B_CLUSTEROK;
1516 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1518 bp->b_flags &= ~B_WRITEINPROG;
1521 * For an interrupted write, the buffer is still valid
1522 * and the write hasn't been pushed to the server yet,
1523 * so we can't set B_ERROR and report the interruption
1524 * by setting B_EINTR. For the B_ASYNC case, B_EINTR
1525 * is not relevant, so the rpc attempt is essentially
1526 * a noop. For the case of a V3 write rpc not being
1527 * committed to stable storage, the block is still
1528 * dirty and requires either a commit rpc or another
1529 * write rpc with iomode == NFSV3WRITE_FILESYNC before
1530 * the block is reused. This is indicated by setting
1531 * the B_DELWRI and B_NEEDCOMMIT flags.
1533 * If the buffer is marked B_PAGING, it does not reside on
1534 * the vp's paging queues so we cannot call bdirty(). The
1535 * bp in this case is not an NFS cache block so we should
1539 || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
1543 bp->b_flags &= ~(B_INVAL|B_NOCACHE);
1544 if ((bp->b_flags & B_PAGING) == 0) {
1546 bp->b_flags &= ~B_DONE;
1548 if (error && (bp->b_flags & B_ASYNC) == 0)
1549 bp->b_flags |= B_EINTR;
1553 bp->b_flags |= B_ERROR;
1554 bp->b_error = np->n_error = error;
1555 np->n_flag |= NWRITEERR;
1557 bp->b_dirtyoff = bp->b_dirtyend = 0;
1565 bp->b_resid = uiop->uio_resid;
1567 nfs_clearcommit(vp->v_mount);
1573 * Used to aid in handling ftruncate() operations on the NFS client side.
1574 * Truncation creates a number of special problems for NFS. We have to
1575 * throw away VM pages and buffer cache buffers that are beyond EOF, and
1576 * we have to properly handle VM pages or (potentially dirty) buffers
1577 * that straddle the truncation point.
1581 nfs_meta_setsize(struct vnode *vp, struct thread *td, u_quad_t nsize)
1583 struct nfsnode *np = VTONFS(vp);
1584 u_quad_t tsize = np->n_size;
1585 int biosize = vp->v_mount->mnt_stat.f_iosize;
1590 if (np->n_size < tsize) {
1596 * vtruncbuf() doesn't get the buffer overlapping the
1597 * truncation point. We may have a B_DELWRI and/or B_CACHE
1598 * buffer that now needs to be truncated.
1600 error = vtruncbuf(vp, td, nsize, biosize);
1601 lbn = nsize / biosize;
1602 bufsize = nsize & (biosize - 1);
1603 bp = nfs_getcacheblk(vp, lbn, bufsize, td);
1604 if (bp->b_dirtyoff > bp->b_bcount)
1605 bp->b_dirtyoff = bp->b_bcount;
1606 if (bp->b_dirtyend > bp->b_bcount)
1607 bp->b_dirtyend = bp->b_bcount;
1608 bp->b_flags |= B_RELBUF; /* don't leave garbage around */
1611 vnode_pager_setsize(vp, nsize);