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
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/resourcevar.h>
44 #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>
59 #include <nfs/rpcv2.h>
60 #include <nfs/nfsproto.h>
62 #include <nfs/nfsmount.h>
63 #include <nfs/nqnfs.h>
64 #include <nfs/nfsnode.h>
67 * Just call nfs_writebp() with the force argument set to 1.
69 * NOTE: B_DONE may or may not be set in a_bp on call.
72 nfs_bwrite(struct buf *bp)
74 return (nfs_writebp(bp, 1, curproc));
77 struct buf_ops buf_ops_nfs = {
83 static struct buf *nfs_getcacheblk __P((struct vnode *vp, daddr_t bn, int size,
86 extern int nfs_numasync;
87 extern int nfs_pbuf_freecnt;
88 extern struct nfsstats nfsstats;
91 * Vnode op for VM getpages.
95 struct vop_getpages_args /* {
100 vm_ooffset_t a_offset;
103 int i, error, nextoff, size, toff, count, npages;
111 struct nfsmount *nmp;
115 p = curproc; /* XXX */
116 cred = curproc->p_ucred; /* XXX */
117 nmp = VFSTONFS(vp->v_mount);
121 mtx_assert(&Giant, MA_OWNED);
122 if (vp->v_object == NULL) {
123 printf("nfs_getpages: called with non-merged cache vnode??\n");
124 return VM_PAGER_ERROR;
127 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
128 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) {
130 (void)nfs_fsinfo(nmp, vp, cred, p);
134 npages = btoc(count);
137 * If the requested page is partially valid, just return it and
138 * allow the pager to zero-out the blanks. Partially valid pages
139 * can only occur at the file EOF.
143 vm_page_t m = pages[ap->a_reqpage];
146 /* handled by vm_fault now */
147 /* vm_page_zero_invalid(m, TRUE); */
148 for (i = 0; i < npages; ++i) {
149 if (i != ap->a_reqpage)
150 vm_page_free(pages[i]);
157 * We use only the kva address for the buffer, but this is extremely
158 * convienient and fast.
160 bp = getpbuf(&nfs_pbuf_freecnt);
162 kva = (vm_offset_t) bp->b_data;
163 pmap_qenter(kva, pages, npages);
165 iov.iov_base = (caddr_t) kva;
169 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
170 uio.uio_resid = count;
171 uio.uio_segflg = UIO_SYSSPACE;
172 uio.uio_rw = UIO_READ;
176 error = nfs_readrpc(vp, &uio, cred);
178 pmap_qremove(kva, npages);
180 relpbuf(bp, &nfs_pbuf_freecnt);
182 if (error && (uio.uio_resid == count)) {
183 printf("nfs_getpages: error %d\n", error);
184 for (i = 0; i < npages; ++i) {
185 if (i != ap->a_reqpage)
186 vm_page_free(pages[i]);
188 return VM_PAGER_ERROR;
192 * Calculate the number of bytes read and validate only that number
193 * of bytes. Note that due to pending writes, size may be 0. This
194 * does not mean that the remaining data is invalid!
197 size = count - uio.uio_resid;
199 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
201 nextoff = toff + PAGE_SIZE;
204 m->flags &= ~PG_ZERO;
206 if (nextoff <= size) {
208 * Read operation filled an entire page
210 m->valid = VM_PAGE_BITS_ALL;
212 } else if (size > toff) {
214 * Read operation filled a partial page.
217 vm_page_set_validclean(m, 0, size - toff);
218 /* handled by vm_fault now */
219 /* vm_page_zero_invalid(m, TRUE); */
222 if (i != ap->a_reqpage) {
224 * Whether or not to leave the page activated is up in
225 * the air, but we should put the page on a page queue
226 * somewhere (it already is in the object). Result:
227 * It appears that emperical results show that
228 * deactivating pages is best.
232 * Just in case someone was asking for this page we
233 * now tell them that it is ok to use.
236 if (m->flags & PG_WANTED)
239 vm_page_deactivate(m);
250 * Vnode op for VM putpages.
254 struct vop_putpages_args /* {
260 vm_ooffset_t a_offset;
267 int iomode, must_commit, i, error, npages, count;
273 struct nfsmount *nmp;
279 p = curproc; /* XXX */
280 cred = curproc->p_ucred; /* XXX */
281 nmp = VFSTONFS(vp->v_mount);
284 rtvals = ap->a_rtvals;
285 npages = btoc(count);
286 offset = IDX_TO_OFF(pages[0]->pindex);
288 mtx_assert(&Giant, MA_OWNED);
289 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
290 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) {
292 (void)nfs_fsinfo(nmp, vp, cred, p);
296 for (i = 0; i < npages; i++) {
297 rtvals[i] = VM_PAGER_AGAIN;
301 * When putting pages, do not extend file past EOF.
304 if (offset + count > np->n_size) {
305 count = np->n_size - offset;
311 * We use only the kva address for the buffer, but this is extremely
312 * convienient and fast.
314 bp = getpbuf(&nfs_pbuf_freecnt);
316 kva = (vm_offset_t) bp->b_data;
317 pmap_qenter(kva, pages, npages);
319 iov.iov_base = (caddr_t) kva;
323 uio.uio_offset = offset;
324 uio.uio_resid = count;
325 uio.uio_segflg = UIO_SYSSPACE;
326 uio.uio_rw = UIO_WRITE;
329 if ((ap->a_sync & VM_PAGER_PUT_SYNC) == 0)
330 iomode = NFSV3WRITE_UNSTABLE;
332 iomode = NFSV3WRITE_FILESYNC;
335 error = nfs_writerpc(vp, &uio, cred, &iomode, &must_commit);
338 pmap_qremove(kva, npages);
339 relpbuf(bp, &nfs_pbuf_freecnt);
342 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
343 for (i = 0; i < nwritten; i++) {
344 rtvals[i] = VM_PAGER_OK;
345 vm_page_undirty(pages[i]);
349 nfs_clearcommit(vp->v_mount);
357 * Vnode op for read using bio
360 nfs_bioread(vp, uio, ioflag, cred)
361 register struct vnode *vp;
362 register struct uio *uio;
366 register struct nfsnode *np = VTONFS(vp);
367 register int biosize, i;
368 struct buf *bp = 0, *rabp;
371 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
375 int nra, error = 0, n = 0, on = 0;
378 if (uio->uio_rw != UIO_READ)
379 panic("nfs_read mode");
381 if (uio->uio_resid == 0)
383 if (uio->uio_offset < 0) /* XXX VDIR cookies can be negative */
387 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
388 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
389 (void)nfs_fsinfo(nmp, vp, cred, p);
390 if (vp->v_type != VDIR &&
391 (uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
393 biosize = vp->v_mount->mnt_stat.f_iosize;
394 seqcount = (int)((off_t)(ioflag >> 16) * biosize / BKVASIZE);
396 * For nfs, cache consistency can only be maintained approximately.
397 * Although RFC1094 does not specify the criteria, the following is
398 * believed to be compatible with the reference port.
399 * For nqnfs, full cache consistency is maintained within the loop.
401 * If the file's modify time on the server has changed since the
402 * last read rpc or you have written to the file,
403 * you may have lost data cache consistency with the
404 * server, so flush all of the file's data out of the cache.
405 * Then force a getattr rpc to ensure that you have up to date
407 * NB: This implies that cache data can be read when up to
408 * NFS_ATTRTIMEO seconds out of date. If you find that you need current
409 * attributes this could be forced by setting n_attrstamp to 0 before
410 * the VOP_GETATTR() call.
412 if ((nmp->nm_flag & NFSMNT_NQNFS) == 0) {
413 if (np->n_flag & NMODIFIED) {
414 if (vp->v_type != VREG) {
415 if (vp->v_type != VDIR)
416 panic("nfs: bioread, not dir");
418 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
423 error = VOP_GETATTR(vp, &vattr, cred, p);
426 np->n_mtime = vattr.va_mtime.tv_sec;
428 error = VOP_GETATTR(vp, &vattr, cred, p);
431 if (np->n_mtime != vattr.va_mtime.tv_sec) {
432 if (vp->v_type == VDIR)
434 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
437 np->n_mtime = vattr.va_mtime.tv_sec;
444 * Get a valid lease. If cached data is stale, flush it.
446 if (nmp->nm_flag & NFSMNT_NQNFS) {
447 if (NQNFS_CKINVALID(vp, np, ND_READ)) {
449 error = nqnfs_getlease(vp, ND_READ, cred, p);
450 } while (error == NQNFS_EXPIRED);
453 if (np->n_lrev != np->n_brev ||
454 (np->n_flag & NQNFSNONCACHE) ||
455 ((np->n_flag & NMODIFIED) && vp->v_type == VDIR)) {
456 if (vp->v_type == VDIR)
458 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
461 np->n_brev = np->n_lrev;
463 } else if (vp->v_type == VDIR && (np->n_flag & NMODIFIED)) {
465 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
470 if (np->n_flag & NQNFSNONCACHE) {
471 switch (vp->v_type) {
473 return (nfs_readrpc(vp, uio, cred));
475 return (nfs_readlinkrpc(vp, uio, cred));
479 printf(" NQNFSNONCACHE: type %x unexpected\n",
483 switch (vp->v_type) {
485 nfsstats.biocache_reads++;
486 lbn = uio->uio_offset / biosize;
487 on = uio->uio_offset & (biosize - 1);
490 * Start the read ahead(s), as required.
492 if (nfs_numasync > 0 && nmp->nm_readahead > 0) {
493 for (nra = 0; nra < nmp->nm_readahead && nra < seqcount &&
494 (off_t)(lbn + 1 + nra) * biosize < np->n_size; nra++) {
495 rabn = lbn + 1 + nra;
496 if (!incore(vp, rabn)) {
497 rabp = nfs_getcacheblk(vp, rabn, biosize, p);
500 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
501 rabp->b_flags |= B_ASYNC;
502 rabp->b_iocmd = BIO_READ;
503 vfs_busy_pages(rabp, 0);
504 if (nfs_asyncio(rabp, cred, p)) {
505 rabp->b_flags |= B_INVAL;
506 rabp->b_ioflags |= BIO_ERROR;
507 vfs_unbusy_pages(rabp);
519 * Obtain the buffer cache block. Figure out the buffer size
520 * when we are at EOF. If we are modifying the size of the
521 * buffer based on an EOF condition we need to hold
522 * nfs_rslock() through obtaining the buffer to prevent
523 * a potential writer-appender from messing with n_size.
524 * Otherwise we may accidently truncate the buffer and
527 * Note that bcount is *not* DEV_BSIZE aligned.
532 if ((off_t)lbn * biosize >= np->n_size) {
534 } else if ((off_t)(lbn + 1) * biosize > np->n_size) {
535 bcount = np->n_size - (off_t)lbn * biosize;
537 if (bcount != biosize) {
538 switch(nfs_rslock(np, p)) {
551 bp = nfs_getcacheblk(vp, lbn, bcount, p);
553 if (bcount != biosize)
559 * If B_CACHE is not set, we must issue the read. If this
560 * fails, we return an error.
563 if ((bp->b_flags & B_CACHE) == 0) {
564 bp->b_iocmd = BIO_READ;
565 vfs_busy_pages(bp, 0);
566 error = nfs_doio(bp, cred, p);
574 * on is the offset into the current bp. Figure out how many
575 * bytes we can copy out of the bp. Note that bcount is
576 * NOT DEV_BSIZE aligned.
578 * Then figure out how many bytes we can copy into the uio.
583 n = min((unsigned)(bcount - on), uio->uio_resid);
586 nfsstats.biocache_readlinks++;
587 bp = nfs_getcacheblk(vp, (daddr_t)0, NFS_MAXPATHLEN, p);
590 if ((bp->b_flags & B_CACHE) == 0) {
591 bp->b_iocmd = BIO_READ;
592 vfs_busy_pages(bp, 0);
593 error = nfs_doio(bp, cred, p);
595 bp->b_ioflags |= BIO_ERROR;
600 n = min(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid);
604 nfsstats.biocache_readdirs++;
605 if (np->n_direofoffset
606 && uio->uio_offset >= np->n_direofoffset) {
609 lbn = (uoff_t)uio->uio_offset / NFS_DIRBLKSIZ;
610 on = uio->uio_offset & (NFS_DIRBLKSIZ - 1);
611 bp = nfs_getcacheblk(vp, lbn, NFS_DIRBLKSIZ, p);
614 if ((bp->b_flags & B_CACHE) == 0) {
615 bp->b_iocmd = BIO_READ;
616 vfs_busy_pages(bp, 0);
617 error = nfs_doio(bp, cred, p);
621 while (error == NFSERR_BAD_COOKIE) {
622 printf("got bad cookie vp %p bp %p\n", vp, bp);
624 error = nfs_vinvalbuf(vp, 0, cred, p, 1);
626 * Yuck! The directory has been modified on the
627 * server. The only way to get the block is by
628 * reading from the beginning to get all the
631 * Leave the last bp intact unless there is an error.
632 * Loop back up to the while if the error is another
633 * NFSERR_BAD_COOKIE (double yuch!).
635 for (i = 0; i <= lbn && !error; i++) {
636 if (np->n_direofoffset
637 && (i * NFS_DIRBLKSIZ) >= np->n_direofoffset)
639 bp = nfs_getcacheblk(vp, i, NFS_DIRBLKSIZ, p);
642 if ((bp->b_flags & B_CACHE) == 0) {
643 bp->b_iocmd = BIO_READ;
644 vfs_busy_pages(bp, 0);
645 error = nfs_doio(bp, cred, p);
647 * no error + B_INVAL == directory EOF,
650 if (error == 0 && (bp->b_flags & B_INVAL))
654 * An error will throw away the block and the
655 * for loop will break out. If no error and this
656 * is not the block we want, we throw away the
657 * block and go for the next one via the for loop.
659 if (error || i < lbn)
664 * The above while is repeated if we hit another cookie
665 * error. If we hit an error and it wasn't a cookie error,
673 * If not eof and read aheads are enabled, start one.
674 * (You need the current block first, so that you have the
675 * directory offset cookie of the next block.)
677 if (nfs_numasync > 0 && nmp->nm_readahead > 0 &&
678 (bp->b_flags & B_INVAL) == 0 &&
679 (np->n_direofoffset == 0 ||
680 (lbn + 1) * NFS_DIRBLKSIZ < np->n_direofoffset) &&
681 !(np->n_flag & NQNFSNONCACHE) &&
682 !incore(vp, lbn + 1)) {
683 rabp = nfs_getcacheblk(vp, lbn + 1, NFS_DIRBLKSIZ, p);
685 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
686 rabp->b_flags |= B_ASYNC;
687 rabp->b_iocmd = BIO_READ;
688 vfs_busy_pages(rabp, 0);
689 if (nfs_asyncio(rabp, cred, p)) {
690 rabp->b_flags |= B_INVAL;
691 rabp->b_ioflags |= BIO_ERROR;
692 vfs_unbusy_pages(rabp);
701 * Unlike VREG files, whos buffer size ( bp->b_bcount ) is
702 * chopped for the EOF condition, we cannot tell how large
703 * NFS directories are going to be until we hit EOF. So
704 * an NFS directory buffer is *not* chopped to its EOF. Now,
705 * it just so happens that b_resid will effectively chop it
706 * to EOF. *BUT* this information is lost if the buffer goes
707 * away and is reconstituted into a B_CACHE state ( due to
708 * being VMIO ) later. So we keep track of the directory eof
709 * in np->n_direofoffset and chop it off as an extra step
712 n = lmin(uio->uio_resid, NFS_DIRBLKSIZ - bp->b_resid - on);
713 if (np->n_direofoffset && n > np->n_direofoffset - uio->uio_offset)
714 n = np->n_direofoffset - uio->uio_offset;
717 printf(" nfs_bioread: type %x unexpected\n",vp->v_type);
722 error = uiomove(bp->b_data + on, (int)n, uio);
724 switch (vp->v_type) {
732 * Invalidate buffer if caching is disabled, forcing a
733 * re-read from the remote later.
735 if (np->n_flag & NQNFSNONCACHE)
736 bp->b_flags |= B_INVAL;
739 printf(" nfs_bioread: type %x unexpected\n",vp->v_type);
742 } while (error == 0 && uio->uio_resid > 0 && n > 0);
747 * Vnode op for write using bio
751 struct vop_write_args /* {
755 struct ucred *a_cred;
759 struct uio *uio = ap->a_uio;
760 struct proc *p = uio->uio_procp;
761 struct vnode *vp = ap->a_vp;
762 struct nfsnode *np = VTONFS(vp);
763 struct ucred *cred = ap->a_cred;
764 int ioflag = ap->a_ioflag;
767 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
770 int n, on, error = 0, iomode, must_commit;
774 if (uio->uio_rw != UIO_WRITE)
775 panic("nfs_write mode");
776 if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
777 panic("nfs_write proc");
779 if (vp->v_type != VREG)
781 if (np->n_flag & NWRITEERR) {
782 np->n_flag &= ~NWRITEERR;
783 return (np->n_error);
785 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
786 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
787 (void)nfs_fsinfo(nmp, vp, cred, p);
790 * Synchronously flush pending buffers if we are in synchronous
791 * mode or if we are appending.
793 if (ioflag & (IO_APPEND | IO_SYNC)) {
794 if (np->n_flag & NMODIFIED) {
796 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
803 * If IO_APPEND then load uio_offset. We restart here if we cannot
804 * get the append lock.
807 if (ioflag & IO_APPEND) {
809 error = VOP_GETATTR(vp, &vattr, cred, p);
812 uio->uio_offset = np->n_size;
815 if (uio->uio_offset < 0)
817 if ((uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
819 if (uio->uio_resid == 0)
823 * We need to obtain the rslock if we intend to modify np->n_size
824 * in order to guarentee the append point with multiple contending
825 * writers, to guarentee that no other appenders modify n_size
826 * while we are trying to obtain a truncated buffer (i.e. to avoid
827 * accidently truncating data written by another appender due to
828 * the race), and to ensure that the buffer is populated prior to
829 * our extending of the file. We hold rslock through the entire
832 * Note that we do not synchronize the case where someone truncates
833 * the file while we are appending to it because attempting to lock
834 * this case may deadlock other parts of the system unexpectedly.
836 if ((ioflag & IO_APPEND) ||
837 uio->uio_offset + uio->uio_resid > np->n_size) {
838 switch(nfs_rslock(np, p)) {
853 * Maybe this should be above the vnode op call, but so long as
854 * file servers have no limits, i don't think it matters
856 if (p && uio->uio_offset + uio->uio_resid >
857 p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
866 biosize = vp->v_mount->mnt_stat.f_iosize;
870 * Check for a valid write lease.
872 if ((nmp->nm_flag & NFSMNT_NQNFS) &&
873 NQNFS_CKINVALID(vp, np, ND_WRITE)) {
875 error = nqnfs_getlease(vp, ND_WRITE, cred, p);
876 } while (error == NQNFS_EXPIRED);
879 if (np->n_lrev != np->n_brev ||
880 (np->n_flag & NQNFSNONCACHE)) {
881 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
884 np->n_brev = np->n_lrev;
887 if ((np->n_flag & NQNFSNONCACHE) && uio->uio_iovcnt == 1) {
888 iomode = NFSV3WRITE_FILESYNC;
889 error = nfs_writerpc(vp, uio, cred, &iomode, &must_commit);
891 nfs_clearcommit(vp->v_mount);
894 nfsstats.biocache_writes++;
895 lbn = uio->uio_offset / biosize;
896 on = uio->uio_offset & (biosize-1);
897 n = min((unsigned)(biosize - on), uio->uio_resid);
900 * Handle direct append and file extension cases, calculate
901 * unaligned buffer size.
904 if (uio->uio_offset == np->n_size && n) {
906 * Get the buffer (in its pre-append state to maintain
907 * B_CACHE if it was previously set). Resize the
908 * nfsnode after we have locked the buffer to prevent
909 * readers from reading garbage.
912 bp = nfs_getcacheblk(vp, lbn, bcount, p);
917 np->n_size = uio->uio_offset + n;
918 np->n_flag |= NMODIFIED;
919 vnode_pager_setsize(vp, np->n_size);
921 save = bp->b_flags & B_CACHE;
923 allocbuf(bp, bcount);
925 bp->b_magic = B_MAGIC_NFS;
926 bp->b_op = &buf_ops_nfs;
930 * Obtain the locked cache block first, and then
931 * adjust the file's size as appropriate.
934 if ((off_t)lbn * biosize + bcount < np->n_size) {
935 if ((off_t)(lbn + 1) * biosize < np->n_size)
938 bcount = np->n_size - (off_t)lbn * biosize;
941 bp = nfs_getcacheblk(vp, lbn, bcount, p);
943 if (uio->uio_offset + n > np->n_size) {
944 np->n_size = uio->uio_offset + n;
945 np->n_flag |= NMODIFIED;
946 vnode_pager_setsize(vp, np->n_size);
956 * Issue a READ if B_CACHE is not set. In special-append
957 * mode, B_CACHE is based on the buffer prior to the write
958 * op and is typically set, avoiding the read. If a read
959 * is required in special append mode, the server will
960 * probably send us a short-read since we extended the file
961 * on our end, resulting in b_resid == 0 and, thusly,
962 * B_CACHE getting set.
964 * We can also avoid issuing the read if the write covers
965 * the entire buffer. We have to make sure the buffer state
966 * is reasonable in this case since we will not be initiating
967 * I/O. See the comments in kern/vfs_bio.c's getblk() for
970 * B_CACHE may also be set due to the buffer being cached
974 if (on == 0 && n == bcount) {
975 bp->b_flags |= B_CACHE;
976 bp->b_flags &= ~B_INVAL;
977 bp->b_ioflags &= ~BIO_ERROR;
980 if ((bp->b_flags & B_CACHE) == 0) {
981 bp->b_iocmd = BIO_READ;
982 vfs_busy_pages(bp, 0);
983 error = nfs_doio(bp, cred, p);
993 if (bp->b_wcred == NOCRED) {
997 np->n_flag |= NMODIFIED;
1000 * If dirtyend exceeds file size, chop it down. This should
1001 * not normally occur but there is an append race where it
1002 * might occur XXX, so we log it.
1004 * If the chopping creates a reverse-indexed or degenerate
1005 * situation with dirtyoff/end, we 0 both of them.
1008 if (bp->b_dirtyend > bcount) {
1009 printf("NFS append race @%lx:%d\n",
1010 (long)bp->b_blkno * DEV_BSIZE,
1011 bp->b_dirtyend - bcount);
1012 bp->b_dirtyend = bcount;
1015 if (bp->b_dirtyoff >= bp->b_dirtyend)
1016 bp->b_dirtyoff = bp->b_dirtyend = 0;
1019 * If the new write will leave a contiguous dirty
1020 * area, just update the b_dirtyoff and b_dirtyend,
1021 * otherwise force a write rpc of the old dirty area.
1023 * While it is possible to merge discontiguous writes due to
1024 * our having a B_CACHE buffer ( and thus valid read data
1025 * for the hole), we don't because it could lead to
1026 * significant cache coherency problems with multiple clients,
1027 * especially if locking is implemented later on.
1029 * as an optimization we could theoretically maintain
1030 * a linked list of discontinuous areas, but we would still
1031 * have to commit them separately so there isn't much
1032 * advantage to it except perhaps a bit of asynchronization.
1035 if (bp->b_dirtyend > 0 &&
1036 (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) {
1037 if (BUF_WRITE(bp) == EINTR)
1043 * Check for valid write lease and get one as required.
1044 * In case getblk() and/or bwrite() delayed us.
1046 if ((nmp->nm_flag & NFSMNT_NQNFS) &&
1047 NQNFS_CKINVALID(vp, np, ND_WRITE)) {
1049 error = nqnfs_getlease(vp, ND_WRITE, cred, p);
1050 } while (error == NQNFS_EXPIRED);
1055 if (np->n_lrev != np->n_brev ||
1056 (np->n_flag & NQNFSNONCACHE)) {
1058 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
1061 np->n_brev = np->n_lrev;
1066 error = uiomove((char *)bp->b_data + on, n, uio);
1069 * Since this block is being modified, it must be written
1070 * again and not just committed. Since write clustering does
1071 * not work for the stage 1 data write, only the stage 2
1072 * commit rpc, we have to clear B_CLUSTEROK as well.
1074 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1077 bp->b_ioflags |= BIO_ERROR;
1083 * Only update dirtyoff/dirtyend if not a degenerate
1087 if (bp->b_dirtyend > 0) {
1088 bp->b_dirtyoff = min(on, bp->b_dirtyoff);
1089 bp->b_dirtyend = max((on + n), bp->b_dirtyend);
1091 bp->b_dirtyoff = on;
1092 bp->b_dirtyend = on + n;
1095 vfs_bio_set_validclean(bp, on, n);
1096 mtx_unlock(&vm_mtx);
1100 * If the lease is non-cachable or IO_SYNC do bwrite().
1102 * IO_INVAL appears to be unused. The idea appears to be
1103 * to turn off caching in this case. Very odd. XXX
1105 if ((np->n_flag & NQNFSNONCACHE) || (ioflag & IO_SYNC)) {
1106 if (ioflag & IO_INVAL)
1107 bp->b_flags |= B_NOCACHE;
1108 error = BUF_WRITE(bp);
1111 if (np->n_flag & NQNFSNONCACHE) {
1112 error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
1116 } else if ((n + on) == biosize &&
1117 (nmp->nm_flag & NFSMNT_NQNFS) == 0) {
1118 bp->b_flags |= B_ASYNC;
1119 (void)nfs_writebp(bp, 0, 0);
1123 } while (uio->uio_resid > 0 && n > 0);
1126 nfs_rsunlock(np, p);
1132 * Get an nfs cache block.
1134 * Allocate a new one if the block isn't currently in the cache
1135 * and return the block marked busy. If the calling process is
1136 * interrupted by a signal for an interruptible mount point, return
1139 * The caller must carefully deal with the possible B_INVAL state of
1140 * the buffer. nfs_doio() clears B_INVAL (and nfs_asyncio() clears it
1141 * indirectly), so synchronous reads can be issued without worrying about
1142 * the B_INVAL state. We have to be a little more careful when dealing
1143 * with writes (see comments in nfs_write()) when extending a file past
1147 nfs_getcacheblk(vp, bn, size, p)
1153 register struct buf *bp;
1155 struct nfsmount *nmp;
1160 if (nmp->nm_flag & NFSMNT_INT) {
1161 bp = getblk(vp, bn, size, PCATCH, 0);
1162 while (bp == (struct buf *)0) {
1163 if (nfs_sigintr(nmp, (struct nfsreq *)0, p))
1164 return ((struct buf *)0);
1165 bp = getblk(vp, bn, size, 0, 2 * hz);
1168 bp = getblk(vp, bn, size, 0, 0);
1171 if (vp->v_type == VREG) {
1174 biosize = mp->mnt_stat.f_iosize;
1175 bp->b_blkno = bn * (biosize / DEV_BSIZE);
1181 * Flush and invalidate all dirty buffers. If another process is already
1182 * doing the flush, just wait for completion.
1185 nfs_vinvalbuf(vp, flags, cred, p, intrflg)
1192 register struct nfsnode *np = VTONFS(vp);
1193 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1194 int error = 0, slpflag, slptimeo;
1196 if (vp->v_flag & VXLOCK) {
1200 if ((nmp->nm_flag & NFSMNT_INT) == 0)
1210 * First wait for any other process doing a flush to complete.
1212 while (np->n_flag & NFLUSHINPROG) {
1213 np->n_flag |= NFLUSHWANT;
1214 error = tsleep((caddr_t)&np->n_flag, PRIBIO + 2, "nfsvinval",
1216 if (error && intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, p))
1221 * Now, flush as required.
1223 np->n_flag |= NFLUSHINPROG;
1224 error = vinvalbuf(vp, flags, cred, p, slpflag, 0);
1226 if (intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, p)) {
1227 np->n_flag &= ~NFLUSHINPROG;
1228 if (np->n_flag & NFLUSHWANT) {
1229 np->n_flag &= ~NFLUSHWANT;
1230 wakeup((caddr_t)&np->n_flag);
1234 error = vinvalbuf(vp, flags, cred, p, 0, slptimeo);
1236 np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
1237 if (np->n_flag & NFLUSHWANT) {
1238 np->n_flag &= ~NFLUSHWANT;
1239 wakeup((caddr_t)&np->n_flag);
1245 * Initiate asynchronous I/O. Return an error if no nfsiods are available.
1246 * This is mainly to avoid queueing async I/O requests when the nfsiods
1247 * are all hung on a dead server.
1249 * Note: nfs_asyncio() does not clear (BIO_ERROR|B_INVAL) but when the bp
1250 * is eventually dequeued by the async daemon, nfs_doio() *will*.
1253 nfs_asyncio(bp, cred, procp)
1254 register struct buf *bp;
1258 struct nfsmount *nmp;
1266 * If no async daemons then return EIO to force caller to run the rpc
1269 if (nfs_numasync == 0)
1272 nmp = VFSTONFS(bp->b_vp->v_mount);
1275 * Commits are usually short and sweet so lets save some cpu and
1276 * leave the async daemons for more important rpc's (such as reads
1279 if (bp->b_iocmd == BIO_WRITE && (bp->b_flags & B_NEEDCOMMIT) &&
1280 (nmp->nm_bufqiods > nfs_numasync / 2)) {
1285 if (nmp->nm_flag & NFSMNT_INT)
1290 * Find a free iod to process this request.
1292 for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
1293 if (nfs_iodwant[i]) {
1295 * Found one, so wake it up and tell it which
1299 ("nfs_asyncio: waking iod %d for mount %p\n",
1301 nfs_iodwant[i] = (struct proc *)0;
1302 nfs_iodmount[i] = nmp;
1304 wakeup((caddr_t)&nfs_iodwant[i]);
1310 * If none are free, we may already have an iod working on this mount
1311 * point. If so, it will process our request.
1314 if (nmp->nm_bufqiods > 0) {
1316 ("nfs_asyncio: %d iods are already processing mount %p\n",
1317 nmp->nm_bufqiods, nmp));
1323 * If we have an iod which can process the request, then queue
1328 * Ensure that the queue never grows too large. We still want
1329 * to asynchronize so we block rather then return EIO.
1331 while (nmp->nm_bufqlen >= 2*nfs_numasync) {
1333 ("nfs_asyncio: waiting for mount %p queue to drain\n", nmp));
1334 nmp->nm_bufqwant = TRUE;
1335 error = tsleep(&nmp->nm_bufq, slpflag | PRIBIO,
1336 "nfsaio", slptimeo);
1338 if (nfs_sigintr(nmp, NULL, procp))
1340 if (slpflag == PCATCH) {
1346 * We might have lost our iod while sleeping,
1347 * so check and loop if nescessary.
1349 if (nmp->nm_bufqiods == 0) {
1351 ("nfs_asyncio: no iods after mount %p queue was drained, looping\n", nmp));
1356 if (bp->b_iocmd == BIO_READ) {
1357 if (bp->b_rcred == NOCRED && cred != NOCRED) {
1362 bp->b_flags |= B_WRITEINPROG;
1363 if (bp->b_wcred == NOCRED && cred != NOCRED) {
1370 TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist);
1376 * All the iods are busy on other mounts, so return EIO to
1377 * force the caller to process the i/o synchronously.
1379 NFS_DPF(ASYNCIO, ("nfs_asyncio: no iods available, i/o is synchronous\n"));
1384 * Do an I/O operation to/from a cache block. This may be called
1385 * synchronously or from an nfsiod.
1396 struct nfsmount *nmp;
1397 int error = 0, iomode, must_commit = 0;
1403 nmp = VFSTONFS(vp->v_mount);
1405 uiop->uio_iov = &io;
1406 uiop->uio_iovcnt = 1;
1407 uiop->uio_segflg = UIO_SYSSPACE;
1408 uiop->uio_procp = p;
1411 * clear BIO_ERROR and B_INVAL state prior to initiating the I/O. We
1412 * do this here so we do not have to do it in all the code that
1415 bp->b_flags &= ~B_INVAL;
1416 bp->b_ioflags &= ~BIO_ERROR;
1418 KASSERT(!(bp->b_flags & B_DONE), ("nfs_doio: bp %p already marked done", bp));
1421 * Historically, paging was done with physio, but no more.
1423 if (bp->b_flags & B_PHYS) {
1425 * ...though reading /dev/drum still gets us here.
1427 io.iov_len = uiop->uio_resid = bp->b_bcount;
1428 /* mapping was done by vmapbuf() */
1429 io.iov_base = bp->b_data;
1430 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
1431 if (bp->b_iocmd == BIO_READ) {
1432 uiop->uio_rw = UIO_READ;
1433 nfsstats.read_physios++;
1434 error = nfs_readrpc(vp, uiop, cr);
1438 iomode = NFSV3WRITE_DATASYNC;
1439 uiop->uio_rw = UIO_WRITE;
1440 nfsstats.write_physios++;
1441 error = nfs_writerpc(vp, uiop, cr, &iomode, &com);
1444 bp->b_ioflags |= BIO_ERROR;
1445 bp->b_error = error;
1447 } else if (bp->b_iocmd == BIO_READ) {
1448 io.iov_len = uiop->uio_resid = bp->b_bcount;
1449 io.iov_base = bp->b_data;
1450 uiop->uio_rw = UIO_READ;
1451 switch (vp->v_type) {
1453 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
1454 nfsstats.read_bios++;
1455 error = nfs_readrpc(vp, uiop, cr);
1457 if (uiop->uio_resid) {
1459 * If we had a short read with no error, we must have
1460 * hit a file hole. We should zero-fill the remainder.
1461 * This can also occur if the server hits the file EOF.
1463 * Holes used to be able to occur due to pending
1464 * writes, but that is not possible any longer.
1466 int nread = bp->b_bcount - uiop->uio_resid;
1467 int left = bp->b_bcount - nread;
1470 bzero((char *)bp->b_data + nread, left);
1471 uiop->uio_resid = 0;
1474 if (p && (vp->v_flag & VTEXT) &&
1475 (((nmp->nm_flag & NFSMNT_NQNFS) &&
1476 NQNFS_CKINVALID(vp, np, ND_READ) &&
1477 np->n_lrev != np->n_brev) ||
1478 (!(nmp->nm_flag & NFSMNT_NQNFS) &&
1479 np->n_mtime != np->n_vattr.va_mtime.tv_sec))) {
1480 uprintf("Process killed due to text file modification\n");
1482 psignal(p, SIGKILL);
1488 uiop->uio_offset = (off_t)0;
1489 nfsstats.readlink_bios++;
1490 error = nfs_readlinkrpc(vp, uiop, cr);
1493 nfsstats.readdir_bios++;
1494 uiop->uio_offset = ((u_quad_t)bp->b_lblkno) * NFS_DIRBLKSIZ;
1495 if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
1496 error = nfs_readdirplusrpc(vp, uiop, cr);
1497 if (error == NFSERR_NOTSUPP)
1498 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
1500 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
1501 error = nfs_readdirrpc(vp, uiop, cr);
1503 * end-of-directory sets B_INVAL but does not generate an
1506 if (error == 0 && uiop->uio_resid == bp->b_bcount)
1507 bp->b_flags |= B_INVAL;
1510 printf("nfs_doio: type %x unexpected\n",vp->v_type);
1514 bp->b_ioflags |= BIO_ERROR;
1515 bp->b_error = error;
1519 * If we only need to commit, try to commit
1521 if (bp->b_flags & B_NEEDCOMMIT) {
1525 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE + bp->b_dirtyoff;
1526 bp->b_flags |= B_WRITEINPROG;
1528 bp->b_vp, off, bp->b_dirtyend-bp->b_dirtyoff,
1530 bp->b_flags &= ~B_WRITEINPROG;
1532 bp->b_dirtyoff = bp->b_dirtyend = 0;
1533 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1538 if (retv == NFSERR_STALEWRITEVERF) {
1539 nfs_clearcommit(bp->b_vp->v_mount);
1544 * Setup for actual write
1547 if ((off_t)bp->b_blkno * DEV_BSIZE + bp->b_dirtyend > np->n_size)
1548 bp->b_dirtyend = np->n_size - (off_t)bp->b_blkno * DEV_BSIZE;
1550 if (bp->b_dirtyend > bp->b_dirtyoff) {
1551 io.iov_len = uiop->uio_resid = bp->b_dirtyend
1553 uiop->uio_offset = (off_t)bp->b_blkno * DEV_BSIZE
1555 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
1556 uiop->uio_rw = UIO_WRITE;
1557 nfsstats.write_bios++;
1559 if ((bp->b_flags & (B_ASYNC | B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == B_ASYNC)
1560 iomode = NFSV3WRITE_UNSTABLE;
1562 iomode = NFSV3WRITE_FILESYNC;
1564 bp->b_flags |= B_WRITEINPROG;
1565 error = nfs_writerpc(vp, uiop, cr, &iomode, &must_commit);
1568 * When setting B_NEEDCOMMIT also set B_CLUSTEROK to try
1569 * to cluster the buffers needing commit. This will allow
1570 * the system to submit a single commit rpc for the whole
1571 * cluster. We can do this even if the buffer is not 100%
1572 * dirty (relative to the NFS blocksize), so we optimize the
1573 * append-to-file-case.
1575 * (when clearing B_NEEDCOMMIT, B_CLUSTEROK must also be
1576 * cleared because write clustering only works for commit
1577 * rpc's, not for the data portion of the write).
1580 if (!error && iomode == NFSV3WRITE_UNSTABLE) {
1581 bp->b_flags |= B_NEEDCOMMIT;
1582 if (bp->b_dirtyoff == 0
1583 && bp->b_dirtyend == bp->b_bcount)
1584 bp->b_flags |= B_CLUSTEROK;
1586 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1588 bp->b_flags &= ~B_WRITEINPROG;
1591 * For an interrupted write, the buffer is still valid
1592 * and the write hasn't been pushed to the server yet,
1593 * so we can't set BIO_ERROR and report the interruption
1594 * by setting B_EINTR. For the B_ASYNC case, B_EINTR
1595 * is not relevant, so the rpc attempt is essentially
1596 * a noop. For the case of a V3 write rpc not being
1597 * committed to stable storage, the block is still
1598 * dirty and requires either a commit rpc or another
1599 * write rpc with iomode == NFSV3WRITE_FILESYNC before
1600 * the block is reused. This is indicated by setting
1601 * the B_DELWRI and B_NEEDCOMMIT flags.
1603 * If the buffer is marked B_PAGING, it does not reside on
1604 * the vp's paging queues so we cannot call bdirty(). The
1605 * bp in this case is not an NFS cache block so we should
1609 || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
1613 bp->b_flags &= ~(B_INVAL|B_NOCACHE);
1614 if ((bp->b_flags & B_PAGING) == 0) {
1616 bp->b_flags &= ~B_DONE;
1618 if (error && (bp->b_flags & B_ASYNC) == 0)
1619 bp->b_flags |= B_EINTR;
1623 bp->b_ioflags |= BIO_ERROR;
1624 bp->b_error = np->n_error = error;
1625 np->n_flag |= NWRITEERR;
1627 bp->b_dirtyoff = bp->b_dirtyend = 0;
1635 bp->b_resid = uiop->uio_resid;
1637 nfs_clearcommit(vp->v_mount);