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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)nfs_bio.c 8.9 (Berkeley) 3/30/95
33 * $FreeBSD: /repoman/r/ncvs/src/sys/nfsclient/nfs_bio.c,v 1.130 2004/04/14 23:23:55 peadar Exp $
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/resourcevar.h>
40 #include <sys/signalvar.h>
43 #include <sys/vnode.h>
44 #include <sys/mount.h>
45 #include <sys/kernel.h>
49 #include <vm/vm_extern.h>
50 #include <vm/vm_page.h>
51 #include <vm/vm_object.h>
52 #include <vm/vm_pager.h>
53 #include <vm/vnode_pager.h>
56 #include <sys/thread2.h>
57 #include <vm/vm_page2.h>
65 #include "nfsm_subs.h"
68 static struct buf *nfs_getcacheblk(struct vnode *vp, off_t loffset,
69 int size, struct thread *td);
70 static int nfs_check_dirent(struct nfs_dirent *dp, int maxlen);
71 static void nfsiodone_sync(struct bio *bio);
72 static void nfs_readrpc_bio_done(nfsm_info_t info);
73 static void nfs_writerpc_bio_done(nfsm_info_t info);
74 static void nfs_commitrpc_bio_done(nfsm_info_t info);
78 nfs_knote(struct vnode *vp, int flags)
81 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags);
85 * Vnode op for read using bio
88 nfs_bioread(struct vnode *vp, struct uio *uio, int ioflag)
90 struct nfsnode *np = VTONFS(vp);
92 struct buf *bp, *rabp;
95 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
105 if (uio->uio_rw != UIO_READ)
106 panic("nfs_read mode");
108 if (uio->uio_resid == 0)
110 if (uio->uio_offset < 0) /* XXX VDIR cookies can be negative */
114 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
115 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
116 (void)nfs_fsinfo(nmp, vp, td);
117 if (vp->v_type != VDIR &&
118 (uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
120 biosize = vp->v_mount->mnt_stat.f_iosize;
121 seqcount = (int)((off_t)(ioflag >> IO_SEQSHIFT) * biosize / MAXBSIZE);
124 * For nfs, cache consistency can only be maintained approximately.
125 * Although RFC1094 does not specify the criteria, the following is
126 * believed to be compatible with the reference port.
128 * NFS: If local changes have been made and this is a
129 * directory, the directory must be invalidated and
130 * the attribute cache must be cleared.
132 * GETATTR is called to synchronize the file size. To
133 * avoid a deadlock again the VM system, we cannot do
134 * this for UIO_NOCOPY reads.
136 * If remote changes are detected local data is flushed
137 * and the cache is invalidated.
139 * NOTE: In the normal case the attribute cache is not
140 * cleared which means GETATTR may use cached data and
141 * not immediately detect changes made on the server.
143 if ((np->n_flag & NLMODIFIED) && vp->v_type == VDIR) {
145 error = nfs_vinvalbuf(vp, V_SAVE, 1);
152 * Synchronize the file size when possible. We can't do this without
153 * risking a deadlock if this is NOCOPY read from a vm_fault->getpages
156 if (uio->uio_segflg != UIO_NOCOPY) {
157 error = VOP_GETATTR(vp, &vattr);
163 * This can deadlock getpages/putpages for regular
164 * files. Only do it for directories.
166 if (np->n_flag & NRMODIFIED) {
167 if (vp->v_type == VDIR) {
169 error = nfs_vinvalbuf(vp, V_SAVE, 1);
172 np->n_flag &= ~NRMODIFIED;
177 * Loop until uio exhausted or we hit EOF
182 switch (vp->v_type) {
184 nfsstats.biocache_reads++;
185 lbn = uio->uio_offset / biosize;
186 boff = uio->uio_offset & (biosize - 1);
187 loffset = lbn * biosize;
190 * Start the read ahead(s), as required.
192 if (nmp->nm_readahead > 0 && nfs_asyncok(nmp)) {
193 for (nra = 0; nra < nmp->nm_readahead && nra < seqcount &&
194 (off_t)(lbn + 1 + nra) * biosize < np->n_size; nra++) {
195 rabn = lbn + 1 + nra;
196 raoffset = rabn * biosize;
197 if (findblk(vp, raoffset, FINDBLK_TEST) == NULL) {
198 rabp = nfs_getcacheblk(vp, raoffset, biosize, td);
201 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
202 rabp->b_cmd = BUF_CMD_READ;
203 vfs_busy_pages(vp, rabp);
204 nfs_asyncio(vp, &rabp->b_bio2);
213 * Obtain the buffer cache block. Figure out the buffer size
214 * when we are at EOF. If we are modifying the size of the
215 * buffer based on an EOF condition we need to hold
216 * nfs_rslock() through obtaining the buffer to prevent
217 * a potential writer-appender from messing with n_size.
218 * Otherwise we may accidently truncate the buffer and
221 * Note that bcount is *not* DEV_BSIZE aligned.
223 if (loffset + boff >= np->n_size) {
227 bp = nfs_getcacheblk(vp, loffset, biosize, td);
233 * If B_CACHE is not set, we must issue the read. If this
234 * fails, we return an error.
236 if ((bp->b_flags & B_CACHE) == 0) {
237 bp->b_cmd = BUF_CMD_READ;
238 bp->b_bio2.bio_done = nfsiodone_sync;
239 bp->b_bio2.bio_flags |= BIO_SYNC;
240 vfs_busy_pages(vp, bp);
241 error = nfs_doio(vp, &bp->b_bio2, td);
249 * on is the offset into the current bp. Figure out how many
250 * bytes we can copy out of the bp. Note that bcount is
251 * NOT DEV_BSIZE aligned.
253 * Then figure out how many bytes we can copy into the uio.
256 if (n > uio->uio_resid)
258 if (loffset + boff + n > np->n_size)
259 n = np->n_size - loffset - boff;
262 biosize = min(NFS_MAXPATHLEN, np->n_size);
263 nfsstats.biocache_readlinks++;
264 bp = nfs_getcacheblk(vp, (off_t)0, biosize, td);
267 if ((bp->b_flags & B_CACHE) == 0) {
268 bp->b_cmd = BUF_CMD_READ;
269 bp->b_bio2.bio_done = nfsiodone_sync;
270 bp->b_bio2.bio_flags |= BIO_SYNC;
271 vfs_busy_pages(vp, bp);
272 error = nfs_doio(vp, &bp->b_bio2, td);
274 bp->b_flags |= B_ERROR | B_INVAL;
279 n = szmin(uio->uio_resid, (size_t)bp->b_bcount - bp->b_resid);
283 nfsstats.biocache_readdirs++;
284 if (np->n_direofoffset &&
285 uio->uio_offset >= np->n_direofoffset
289 lbn = (uoff_t)uio->uio_offset / NFS_DIRBLKSIZ;
290 boff = uio->uio_offset & (NFS_DIRBLKSIZ - 1);
291 loffset = uio->uio_offset - boff;
292 bp = nfs_getcacheblk(vp, loffset, NFS_DIRBLKSIZ, td);
296 if ((bp->b_flags & B_CACHE) == 0) {
297 bp->b_cmd = BUF_CMD_READ;
298 bp->b_bio2.bio_done = nfsiodone_sync;
299 bp->b_bio2.bio_flags |= BIO_SYNC;
300 vfs_busy_pages(vp, bp);
301 error = nfs_doio(vp, &bp->b_bio2, td);
304 while (error == NFSERR_BAD_COOKIE) {
305 kprintf("got bad cookie vp %p bp %p\n", vp, bp);
307 error = nfs_vinvalbuf(vp, 0, 1);
309 * Yuck! The directory has been modified on the
310 * server. The only way to get the block is by
311 * reading from the beginning to get all the
314 * Leave the last bp intact unless there is an error.
315 * Loop back up to the while if the error is another
316 * NFSERR_BAD_COOKIE (double yuch!).
318 for (i = 0; i <= lbn && !error; i++) {
319 if (np->n_direofoffset
320 && (i * NFS_DIRBLKSIZ) >= np->n_direofoffset)
322 bp = nfs_getcacheblk(vp, (off_t)i * NFS_DIRBLKSIZ,
326 if ((bp->b_flags & B_CACHE) == 0) {
327 bp->b_cmd = BUF_CMD_READ;
328 bp->b_bio2.bio_done = nfsiodone_sync;
329 bp->b_bio2.bio_flags |= BIO_SYNC;
330 vfs_busy_pages(vp, bp);
331 error = nfs_doio(vp, &bp->b_bio2, td);
333 * no error + B_INVAL == directory EOF,
336 if (error == 0 && (bp->b_flags & B_INVAL))
340 * An error will throw away the block and the
341 * for loop will break out. If no error and this
342 * is not the block we want, we throw away the
343 * block and go for the next one via the for loop.
345 if (error || i < lbn)
350 * The above while is repeated if we hit another cookie
351 * error. If we hit an error and it wasn't a cookie error,
359 * If not eof and read aheads are enabled, start one.
360 * (You need the current block first, so that you have the
361 * directory offset cookie of the next block.)
363 if (nmp->nm_readahead > 0 && nfs_asyncok(nmp) &&
364 (bp->b_flags & B_INVAL) == 0 &&
365 (np->n_direofoffset == 0 ||
366 loffset + NFS_DIRBLKSIZ < np->n_direofoffset) &&
367 findblk(vp, loffset + NFS_DIRBLKSIZ, FINDBLK_TEST) == NULL
369 rabp = nfs_getcacheblk(vp, loffset + NFS_DIRBLKSIZ,
372 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
373 rabp->b_cmd = BUF_CMD_READ;
374 vfs_busy_pages(vp, rabp);
375 nfs_asyncio(vp, &rabp->b_bio2);
382 * Unlike VREG files, whos buffer size ( bp->b_bcount ) is
383 * chopped for the EOF condition, we cannot tell how large
384 * NFS directories are going to be until we hit EOF. So
385 * an NFS directory buffer is *not* chopped to its EOF. Now,
386 * it just so happens that b_resid will effectively chop it
387 * to EOF. *BUT* this information is lost if the buffer goes
388 * away and is reconstituted into a B_CACHE state ( due to
389 * being VMIO ) later. So we keep track of the directory eof
390 * in np->n_direofoffset and chop it off as an extra step
393 * NOTE: boff could already be beyond EOF.
395 if ((size_t)boff > NFS_DIRBLKSIZ - bp->b_resid) {
398 n = szmin(uio->uio_resid,
399 NFS_DIRBLKSIZ - bp->b_resid - (size_t)boff);
401 if (np->n_direofoffset &&
402 n > (size_t)(np->n_direofoffset - uio->uio_offset)) {
403 n = (size_t)(np->n_direofoffset - uio->uio_offset);
407 kprintf(" nfs_bioread: type %x unexpected\n",vp->v_type);
412 switch (vp->v_type) {
415 error = uiomovebp(bp, bp->b_data + boff, n, uio);
419 error = uiomovebp(bp, bp->b_data + boff, n, uio);
424 off_t old_off = uio->uio_offset;
426 struct nfs_dirent *dp;
429 * We are casting cpos to nfs_dirent, it must be
437 cpos = bp->b_data + boff;
438 epos = bp->b_data + boff + n;
439 while (cpos < epos && error == 0 && uio->uio_resid > 0) {
440 dp = (struct nfs_dirent *)cpos;
441 error = nfs_check_dirent(dp, (int)(epos - cpos));
444 if (vop_write_dirent(&error, uio, dp->nfs_ino,
445 dp->nfs_type, dp->nfs_namlen, dp->nfs_name)) {
448 cpos += dp->nfs_reclen;
452 uio->uio_offset = old_off + cpos -
458 kprintf(" nfs_bioread: type %x unexpected\n",vp->v_type);
462 } while (error == 0 && uio->uio_resid > 0 && n > 0);
467 * Userland can supply any 'seek' offset when reading a NFS directory.
468 * Validate the structure so we don't panic the kernel. Note that
469 * the element name is nul terminated and the nul is not included
474 nfs_check_dirent(struct nfs_dirent *dp, int maxlen)
476 int nfs_name_off = offsetof(struct nfs_dirent, nfs_name[0]);
478 if (nfs_name_off >= maxlen)
480 if (dp->nfs_reclen < nfs_name_off || dp->nfs_reclen > maxlen)
482 if (nfs_name_off + dp->nfs_namlen >= dp->nfs_reclen)
484 if (dp->nfs_reclen & 3)
490 * Vnode op for write using bio
492 * nfs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
493 * struct ucred *a_cred)
496 nfs_write(struct vop_write_args *ap)
498 struct uio *uio = ap->a_uio;
499 struct thread *td = uio->uio_td;
500 struct vnode *vp = ap->a_vp;
501 struct nfsnode *np = VTONFS(vp);
502 int ioflag = ap->a_ioflag;
505 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
516 if (uio->uio_rw != UIO_WRITE)
517 panic("nfs_write mode");
518 if (uio->uio_segflg == UIO_USERSPACE && uio->uio_td != curthread)
519 panic("nfs_write proc");
521 if (vp->v_type != VREG)
524 lwkt_gettoken(&nmp->nm_token);
526 if (np->n_flag & NWRITEERR) {
527 np->n_flag &= ~NWRITEERR;
528 lwkt_reltoken(&nmp->nm_token);
529 return (np->n_error);
531 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
532 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) {
533 (void)nfs_fsinfo(nmp, vp, td);
537 * Synchronously flush pending buffers if we are in synchronous
538 * mode or if we are appending.
540 if (ioflag & (IO_APPEND | IO_SYNC)) {
541 if (np->n_flag & NLMODIFIED) {
543 error = nfs_flush(vp, MNT_WAIT, td, 0);
544 /* error = nfs_vinvalbuf(vp, V_SAVE, 1); */
551 * If IO_APPEND then load uio_offset. We restart here if we cannot
552 * get the append lock.
555 if (ioflag & IO_APPEND) {
557 error = VOP_GETATTR(vp, &vattr);
560 uio->uio_offset = np->n_size;
563 if (uio->uio_offset < 0) {
567 if ((uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize) {
571 if (uio->uio_resid == 0) {
577 * We need to obtain the rslock if we intend to modify np->n_size
578 * in order to guarentee the append point with multiple contending
579 * writers, to guarentee that no other appenders modify n_size
580 * while we are trying to obtain a truncated buffer (i.e. to avoid
581 * accidently truncating data written by another appender due to
582 * the race), and to ensure that the buffer is populated prior to
583 * our extending of the file. We hold rslock through the entire
586 * Note that we do not synchronize the case where someone truncates
587 * the file while we are appending to it because attempting to lock
588 * this case may deadlock other parts of the system unexpectedly.
590 if ((ioflag & IO_APPEND) ||
591 uio->uio_offset + uio->uio_resid > np->n_size) {
592 switch(nfs_rslock(np)) {
608 * Maybe this should be above the vnode op call, but so long as
609 * file servers have no limits, i don't think it matters
611 if (td && td->td_proc && uio->uio_offset + uio->uio_resid >
612 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
613 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
620 biosize = vp->v_mount->mnt_stat.f_iosize;
623 nfsstats.biocache_writes++;
624 boff = uio->uio_offset & (biosize-1);
625 loffset = uio->uio_offset - boff;
626 bytes = (int)szmin((unsigned)(biosize - boff), uio->uio_resid);
629 * Handle direct append and file extension cases, calculate
630 * unaligned buffer size. When extending B_CACHE will be
631 * set if possible. See UIO_NOCOPY note below.
633 if (uio->uio_offset + bytes > np->n_size) {
634 np->n_flag |= NLMODIFIED;
635 trivial = (uio->uio_segflg != UIO_NOCOPY &&
636 uio->uio_offset <= np->n_size);
637 nfs_meta_setsize(vp, td, uio->uio_offset + bytes,
639 kflags |= NOTE_EXTEND;
641 bp = nfs_getcacheblk(vp, loffset, biosize, td);
648 * Actual bytes in buffer which we care about
650 if (loffset + biosize < np->n_size)
653 bcount = (int)(np->n_size - loffset);
656 * Avoid a read by setting B_CACHE where the data we
657 * intend to write covers the entire buffer. Note
658 * that the buffer may have been set to B_CACHE by
659 * nfs_meta_setsize() above or otherwise inherited the
660 * flag, but if B_CACHE isn't set the buffer may be
661 * uninitialized and must be zero'd to accomodate
662 * future seek+write's.
664 * See the comments in kern/vfs_bio.c's getblk() for
667 * When doing a UIO_NOCOPY write the buffer is not
668 * overwritten and we cannot just set B_CACHE unconditionally
669 * for full-block writes.
671 if (boff == 0 && bytes == biosize &&
672 uio->uio_segflg != UIO_NOCOPY) {
673 bp->b_flags |= B_CACHE;
674 bp->b_flags &= ~(B_ERROR | B_INVAL);
678 * b_resid may be set due to file EOF if we extended out.
679 * The NFS bio code will zero the difference anyway so
680 * just acknowledged the fact and set b_resid to 0.
682 if ((bp->b_flags & B_CACHE) == 0) {
683 bp->b_cmd = BUF_CMD_READ;
684 bp->b_bio2.bio_done = nfsiodone_sync;
685 bp->b_bio2.bio_flags |= BIO_SYNC;
686 vfs_busy_pages(vp, bp);
687 error = nfs_doio(vp, &bp->b_bio2, td);
694 np->n_flag |= NLMODIFIED;
695 kflags |= NOTE_WRITE;
698 * If dirtyend exceeds file size, chop it down. This should
699 * not normally occur but there is an append race where it
700 * might occur XXX, so we log it.
702 * If the chopping creates a reverse-indexed or degenerate
703 * situation with dirtyoff/end, we 0 both of them.
705 if (bp->b_dirtyend > bcount) {
706 kprintf("NFS append race @%08llx:%d\n",
707 (long long)bp->b_bio2.bio_offset,
708 bp->b_dirtyend - bcount);
709 bp->b_dirtyend = bcount;
712 if (bp->b_dirtyoff >= bp->b_dirtyend)
713 bp->b_dirtyoff = bp->b_dirtyend = 0;
716 * If the new write will leave a contiguous dirty
717 * area, just update the b_dirtyoff and b_dirtyend,
718 * otherwise force a write rpc of the old dirty area.
720 * While it is possible to merge discontiguous writes due to
721 * our having a B_CACHE buffer ( and thus valid read data
722 * for the hole), we don't because it could lead to
723 * significant cache coherency problems with multiple clients,
724 * especially if locking is implemented later on.
726 * as an optimization we could theoretically maintain
727 * a linked list of discontinuous areas, but we would still
728 * have to commit them separately so there isn't much
729 * advantage to it except perhaps a bit of asynchronization.
731 if (bp->b_dirtyend > 0 &&
732 (boff > bp->b_dirtyend ||
733 (boff + bytes) < bp->b_dirtyoff)
735 if (bwrite(bp) == EINTR) {
742 error = uiomovebp(bp, bp->b_data + boff, bytes, uio);
745 * Since this block is being modified, it must be written
746 * again and not just committed. Since write clustering does
747 * not work for the stage 1 data write, only the stage 2
748 * commit rpc, we have to clear B_CLUSTEROK as well.
750 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
758 * Only update dirtyoff/dirtyend if not a degenerate
761 * The underlying VM pages have been marked valid by
762 * virtue of acquiring the bp. Because the entire buffer
763 * is marked dirty we do not have to worry about cleaning
764 * out the related dirty bits (and wouldn't really know
765 * how to deal with byte ranges anyway)
768 if (bp->b_dirtyend > 0) {
769 bp->b_dirtyoff = imin(boff, bp->b_dirtyoff);
770 bp->b_dirtyend = imax(boff + bytes,
773 bp->b_dirtyoff = boff;
774 bp->b_dirtyend = boff + bytes;
779 * If the lease is non-cachable or IO_SYNC do bwrite().
781 * IO_INVAL appears to be unused. The idea appears to be
782 * to turn off caching in this case. Very odd. XXX
784 * If nfs_async is set bawrite() will use an unstable write
785 * (build dirty bufs on the server), so we might as well
786 * push it out with bawrite(). If nfs_async is not set we
787 * use bdwrite() to cache dirty bufs on the client.
789 if (ioflag & IO_SYNC) {
790 if (ioflag & IO_INVAL)
791 bp->b_flags |= B_NOCACHE;
795 } else if (boff + bytes == biosize && nfs_async) {
800 } while (uio->uio_resid > 0 && bytes > 0);
806 nfs_knote(vp, kflags);
807 lwkt_reltoken(&nmp->nm_token);
812 * Get an nfs cache block.
814 * Allocate a new one if the block isn't currently in the cache
815 * and return the block marked busy. If the calling process is
816 * interrupted by a signal for an interruptible mount point, return
819 * The caller must carefully deal with the possible B_INVAL state of
820 * the buffer. nfs_startio() clears B_INVAL (and nfs_asyncio() clears it
821 * indirectly), so synchronous reads can be issued without worrying about
822 * the B_INVAL state. We have to be a little more careful when dealing
823 * with writes (see comments in nfs_write()) when extending a file past
827 nfs_getcacheblk(struct vnode *vp, off_t loffset, int size, struct thread *td)
831 struct nfsmount *nmp;
836 if (nmp->nm_flag & NFSMNT_INT) {
837 bp = getblk(vp, loffset, size, GETBLK_PCATCH, 0);
839 if (nfs_sigintr(nmp, NULL, td))
841 bp = getblk(vp, loffset, size, 0, 2 * hz);
844 bp = getblk(vp, loffset, size, 0, 0);
848 * bio2, the 'device' layer. Since BIOs use 64 bit byte offsets
849 * now, no translation is necessary.
851 bp->b_bio2.bio_offset = loffset;
856 * Flush and invalidate all dirty buffers. If another process is already
857 * doing the flush, just wait for completion.
860 nfs_vinvalbuf(struct vnode *vp, int flags, int intrflg)
862 struct nfsnode *np = VTONFS(vp);
863 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
864 int error = 0, slpflag, slptimeo;
865 thread_t td = curthread;
867 if (vp->v_flag & VRECLAIMED)
870 if ((nmp->nm_flag & NFSMNT_INT) == 0)
880 * First wait for any other process doing a flush to complete.
882 while (np->n_flag & NFLUSHINPROG) {
883 np->n_flag |= NFLUSHWANT;
884 error = tsleep((caddr_t)&np->n_flag, 0, "nfsvinval", slptimeo);
885 if (error && intrflg && nfs_sigintr(nmp, NULL, td))
890 * Now, flush as required.
892 np->n_flag |= NFLUSHINPROG;
893 error = vinvalbuf(vp, flags, slpflag, 0);
895 if (intrflg && nfs_sigintr(nmp, NULL, td)) {
896 np->n_flag &= ~NFLUSHINPROG;
897 if (np->n_flag & NFLUSHWANT) {
898 np->n_flag &= ~NFLUSHWANT;
899 wakeup((caddr_t)&np->n_flag);
903 error = vinvalbuf(vp, flags, 0, slptimeo);
905 np->n_flag &= ~(NLMODIFIED | NFLUSHINPROG);
906 if (np->n_flag & NFLUSHWANT) {
907 np->n_flag &= ~NFLUSHWANT;
908 wakeup((caddr_t)&np->n_flag);
914 * Return true (non-zero) if the txthread and rxthread are operational
915 * and we do not already have too many not-yet-started BIO's built up.
918 nfs_asyncok(struct nfsmount *nmp)
920 return (nmp->nm_bioqlen < nfs_maxasyncbio &&
921 nmp->nm_bioqlen < nmp->nm_maxasync_scaled / NFS_ASYSCALE &&
922 nmp->nm_rxstate <= NFSSVC_PENDING &&
923 nmp->nm_txstate <= NFSSVC_PENDING);
927 * The read-ahead code calls this to queue a bio to the txthread.
929 * We don't touch the bio otherwise... that is, we do not even
930 * construct or send the initial rpc. The txthread will do it
933 * NOTE! nm_bioqlen is not decremented until the request completes,
934 * so it does not reflect the number of bio's on bioq.
937 nfs_asyncio(struct vnode *vp, struct bio *bio)
939 struct buf *bp = bio->bio_buf;
940 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
942 KKASSERT(vp->v_tag == VT_NFS);
946 * Shortcut swap cache (not done automatically because we are not
949 if (vn_cache_strategy(vp, bio))
952 bio->bio_driver_info = vp;
954 TAILQ_INSERT_TAIL(&nmp->nm_bioq, bio, bio_act);
955 atomic_add_int(&nmp->nm_bioqlen, 1);
957 nfssvc_iod_writer_wakeup(nmp);
961 * nfs_doio() - Execute a BIO operation synchronously. The BIO will be
962 * completed and its error returned. The caller is responsible
963 * for brelse()ing it. ONLY USE FOR BIO_SYNC IOs! Otherwise
964 * our error probe will be against an invalid pointer.
966 * nfs_startio()- Execute a BIO operation assynchronously.
968 * NOTE: nfs_asyncio() is used to initiate an asynchronous BIO operation,
969 * which basically just queues it to the txthread. nfs_startio()
970 * actually initiates the I/O AFTER it has gotten to the txthread.
972 * NOTE: td might be NULL.
974 * NOTE: Caller has already busied the I/O.
977 nfs_startio(struct vnode *vp, struct bio *bio, struct thread *td)
979 struct buf *bp = bio->bio_buf;
981 KKASSERT(vp->v_tag == VT_NFS);
984 * clear B_ERROR and B_INVAL state prior to initiating the I/O. We
985 * do this here so we do not have to do it in all the code that
988 bp->b_flags &= ~(B_ERROR | B_INVAL);
990 KASSERT(bp->b_cmd != BUF_CMD_DONE,
991 ("nfs_doio: bp %p already marked done!", bp));
993 if (bp->b_cmd == BUF_CMD_READ) {
994 switch (vp->v_type) {
996 nfsstats.read_bios++;
997 nfs_readrpc_bio(vp, bio);
1001 bio->bio_offset = 0;
1002 nfsstats.readlink_bios++;
1003 nfs_readlinkrpc_bio(vp, bio);
1005 nfs_doio(vp, bio, td);
1010 * NOTE: If nfs_readdirplusrpc_bio() is requested but
1011 * not supported, it will chain to
1012 * nfs_readdirrpc_bio().
1015 nfsstats.readdir_bios++;
1016 uiop->uio_offset = bio->bio_offset;
1017 if (nmp->nm_flag & NFSMNT_RDIRPLUS)
1018 nfs_readdirplusrpc_bio(vp, bio);
1020 nfs_readdirrpc_bio(vp, bio);
1022 nfs_doio(vp, bio, td);
1026 kprintf("nfs_doio: type %x unexpected\n",vp->v_type);
1027 bp->b_flags |= B_ERROR;
1028 bp->b_error = EINVAL;
1034 * If we only need to commit, try to commit. If this fails
1035 * it will chain through to the write. Basically all the logic
1036 * in nfs_doio() is replicated.
1038 KKASSERT(bp->b_cmd == BUF_CMD_WRITE);
1039 if (bp->b_flags & B_NEEDCOMMIT)
1040 nfs_commitrpc_bio(vp, bio);
1042 nfs_writerpc_bio(vp, bio);
1047 nfs_doio(struct vnode *vp, struct bio *bio, struct thread *td)
1049 struct buf *bp = bio->bio_buf;
1052 struct nfsmount *nmp;
1054 int iomode, must_commit;
1061 * Shortcut swap cache (not done automatically because we are not
1064 * XXX The biowait is a hack until we can figure out how to stop a
1065 * biodone chain when a middle element is BIO_SYNC. BIO_SYNC is
1066 * set so the bp shouldn't get ripped out from under us. The only
1067 * use-cases are fully synchronous I/O cases.
1069 * XXX This is having problems, give up for now.
1071 if (vn_cache_strategy(vp, bio)) {
1072 error = biowait(&bio->bio_buf->b_bio1, "nfsrsw");
1077 KKASSERT(vp->v_tag == VT_NFS);
1079 nmp = VFSTONFS(vp->v_mount);
1081 uiop->uio_iov = &io;
1082 uiop->uio_iovcnt = 1;
1083 uiop->uio_segflg = UIO_SYSSPACE;
1087 * clear B_ERROR and B_INVAL state prior to initiating the I/O. We
1088 * do this here so we do not have to do it in all the code that
1091 bp->b_flags &= ~(B_ERROR | B_INVAL);
1093 KASSERT(bp->b_cmd != BUF_CMD_DONE,
1094 ("nfs_doio: bp %p already marked done!", bp));
1096 if (bp->b_cmd == BUF_CMD_READ) {
1097 io.iov_len = uiop->uio_resid = (size_t)bp->b_bcount;
1098 io.iov_base = bp->b_data;
1099 uiop->uio_rw = UIO_READ;
1101 switch (vp->v_type) {
1104 * When reading from a regular file zero-fill any residual.
1105 * Note that this residual has nothing to do with NFS short
1106 * reads, which nfs_readrpc_uio() will handle for us.
1108 * We have to do this because when we are write extending
1109 * a file the server may not have the same notion of
1110 * filesize as we do. Our BIOs should already be sized
1111 * (b_bcount) to account for the file EOF.
1113 nfsstats.read_bios++;
1114 uiop->uio_offset = bio->bio_offset;
1115 error = nfs_readrpc_uio(vp, uiop);
1116 if (error == 0 && uiop->uio_resid) {
1117 n = (size_t)bp->b_bcount - uiop->uio_resid;
1118 bzero(bp->b_data + n, bp->b_bcount - n);
1119 uiop->uio_resid = 0;
1121 if (td && td->td_proc && (vp->v_flag & VTEXT) &&
1122 np->n_mtime != np->n_vattr.va_mtime.tv_sec) {
1123 uprintf("Process killed due to text file modification\n");
1124 ksignal(td->td_proc, SIGKILL);
1128 uiop->uio_offset = 0;
1129 nfsstats.readlink_bios++;
1130 error = nfs_readlinkrpc_uio(vp, uiop);
1133 nfsstats.readdir_bios++;
1134 uiop->uio_offset = bio->bio_offset;
1135 if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
1136 error = nfs_readdirplusrpc_uio(vp, uiop);
1137 if (error == NFSERR_NOTSUPP)
1138 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
1140 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
1141 error = nfs_readdirrpc_uio(vp, uiop);
1143 * end-of-directory sets B_INVAL but does not generate an
1146 if (error == 0 && uiop->uio_resid == bp->b_bcount)
1147 bp->b_flags |= B_INVAL;
1150 kprintf("nfs_doio: type %x unexpected\n",vp->v_type);
1154 bp->b_flags |= B_ERROR;
1155 bp->b_error = error;
1157 bp->b_resid = uiop->uio_resid;
1160 * If we only need to commit, try to commit.
1162 * NOTE: The I/O has already been staged for the write and
1163 * its pages busied, so b_dirtyoff/end is valid.
1165 KKASSERT(bp->b_cmd == BUF_CMD_WRITE);
1166 if (bp->b_flags & B_NEEDCOMMIT) {
1170 off = bio->bio_offset + bp->b_dirtyoff;
1171 retv = nfs_commitrpc_uio(vp, off,
1172 bp->b_dirtyend - bp->b_dirtyoff,
1175 bp->b_dirtyoff = bp->b_dirtyend = 0;
1176 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1181 if (retv == NFSERR_STALEWRITEVERF) {
1182 nfs_clearcommit(vp->v_mount);
1187 * Setup for actual write
1189 if (bio->bio_offset + bp->b_dirtyend > np->n_size)
1190 bp->b_dirtyend = np->n_size - bio->bio_offset;
1192 if (bp->b_dirtyend > bp->b_dirtyoff) {
1193 io.iov_len = uiop->uio_resid = bp->b_dirtyend
1195 uiop->uio_offset = bio->bio_offset + bp->b_dirtyoff;
1196 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
1197 uiop->uio_rw = UIO_WRITE;
1198 nfsstats.write_bios++;
1200 if ((bp->b_flags & (B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == 0)
1201 iomode = NFSV3WRITE_UNSTABLE;
1203 iomode = NFSV3WRITE_FILESYNC;
1206 error = nfs_writerpc_uio(vp, uiop, &iomode, &must_commit);
1209 * We no longer try to use kern/vfs_bio's cluster code to
1210 * cluster commits, so B_CLUSTEROK is no longer set with
1211 * B_NEEDCOMMIT. The problem is that a vfs_busy_pages()
1212 * may have to clear B_NEEDCOMMIT if it finds underlying
1213 * pages have been redirtied through a memory mapping
1214 * and doing this on a clustered bp will probably cause
1215 * a panic, plus the flag in the underlying NFS bufs
1216 * making up the cluster bp will not be properly cleared.
1218 if (!error && iomode == NFSV3WRITE_UNSTABLE) {
1219 bp->b_flags |= B_NEEDCOMMIT;
1221 /* XXX do not enable commit clustering */
1222 if (bp->b_dirtyoff == 0
1223 && bp->b_dirtyend == bp->b_bcount)
1224 bp->b_flags |= B_CLUSTEROK;
1227 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1231 * For an interrupted write, the buffer is still valid
1232 * and the write hasn't been pushed to the server yet,
1233 * so we can't set B_ERROR and report the interruption
1234 * by setting B_EINTR. For the async case, B_EINTR
1235 * is not relevant, so the rpc attempt is essentially
1236 * a noop. For the case of a V3 write rpc not being
1237 * committed to stable storage, the block is still
1238 * dirty and requires either a commit rpc or another
1239 * write rpc with iomode == NFSV3WRITE_FILESYNC before
1240 * the block is reused. This is indicated by setting
1241 * the B_DELWRI and B_NEEDCOMMIT flags.
1243 * If the buffer is marked B_PAGING, it does not reside on
1244 * the vp's paging queues so we cannot call bdirty(). The
1245 * bp in this case is not an NFS cache block so we should
1249 || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
1251 bp->b_flags &= ~(B_INVAL|B_NOCACHE);
1252 if ((bp->b_flags & B_PAGING) == 0)
1255 bp->b_flags |= B_EINTR;
1259 bp->b_flags |= B_ERROR;
1260 bp->b_error = np->n_error = error;
1261 np->n_flag |= NWRITEERR;
1263 bp->b_dirtyoff = bp->b_dirtyend = 0;
1266 nfs_clearcommit(vp->v_mount);
1267 bp->b_resid = uiop->uio_resid;
1274 * I/O was run synchronously, biodone() it and calculate the
1278 KKASSERT(bp->b_cmd == BUF_CMD_DONE);
1279 if (bp->b_flags & B_EINTR)
1281 if (bp->b_flags & B_ERROR)
1282 return (bp->b_error ? bp->b_error : EIO);
1287 * Handle all truncation, write-extend, and ftruncate()-extend operations
1288 * on the NFS lcient side.
1290 * We use the new API in kern/vfs_vm.c to perform these operations in a
1291 * VM-friendly way. With this API VM pages are properly zerod and pages
1292 * still mapped into the buffer straddling EOF are not invalidated.
1295 nfs_meta_setsize(struct vnode *vp, struct thread *td, off_t nsize, int trivial)
1297 struct nfsnode *np = VTONFS(vp);
1299 int biosize = vp->v_mount->mnt_stat.f_iosize;
1305 if (nsize < osize) {
1306 error = nvtruncbuf(vp, nsize, biosize, -1, 0);
1308 error = nvextendbuf(vp, osize, nsize,
1309 biosize, biosize, -1, -1,
1316 * Synchronous completion for nfs_doio. Call bpdone() with elseit=FALSE.
1317 * Caller is responsible for brelse()'ing the bp.
1320 nfsiodone_sync(struct bio *bio)
1323 bpdone(bio->bio_buf, 0);
1327 * nfs read rpc - BIO version
1330 nfs_readrpc_bio(struct vnode *vp, struct bio *bio)
1332 struct buf *bp = bio->bio_buf;
1334 struct nfsmount *nmp;
1335 int error = 0, len, tsiz;
1336 struct nfsm_info *info;
1338 info = kmalloc(sizeof(*info), M_NFSREQ, M_WAITOK);
1340 info->v3 = NFS_ISV3(vp);
1342 nmp = VFSTONFS(vp->v_mount);
1343 tsiz = bp->b_bcount;
1344 KKASSERT(tsiz <= nmp->nm_rsize);
1345 if (bio->bio_offset + tsiz > nmp->nm_maxfilesize) {
1349 nfsstats.rpccnt[NFSPROC_READ]++;
1351 nfsm_reqhead(info, vp, NFSPROC_READ,
1352 NFSX_FH(info->v3) + NFSX_UNSIGNED * 3);
1353 ERROROUT(nfsm_fhtom(info, vp));
1354 tl = nfsm_build(info, NFSX_UNSIGNED * 3);
1356 txdr_hyper(bio->bio_offset, tl);
1357 *(tl + 2) = txdr_unsigned(len);
1359 *tl++ = txdr_unsigned(bio->bio_offset);
1360 *tl++ = txdr_unsigned(len);
1364 info->done = nfs_readrpc_bio_done;
1365 nfsm_request_bio(info, vp, NFSPROC_READ, NULL,
1366 nfs_vpcred(vp, ND_READ));
1369 kfree(info, M_NFSREQ);
1370 bp->b_error = error;
1371 bp->b_flags |= B_ERROR;
1376 nfs_readrpc_bio_done(nfsm_info_t info)
1378 struct nfsmount *nmp = VFSTONFS(info->vp->v_mount);
1379 struct bio *bio = info->bio;
1380 struct buf *bp = bio->bio_buf;
1387 KKASSERT(info->state == NFSM_STATE_DONE);
1389 lwkt_gettoken(&nmp->nm_token);
1391 ERROROUT(info->error);
1393 ERROROUT(nfsm_postop_attr(info, info->vp, &attrflag,
1394 NFS_LATTR_NOSHRINK));
1395 NULLOUT(tl = nfsm_dissect(info, 2 * NFSX_UNSIGNED));
1396 eof = fxdr_unsigned(int, *(tl + 1));
1398 ERROROUT(nfsm_loadattr(info, info->vp, NULL));
1401 NEGATIVEOUT(retlen = nfsm_strsiz(info, nmp->nm_rsize));
1402 ERROROUT(nfsm_mtobio(info, bio, retlen));
1403 m_freem(info->mrep);
1407 * No error occured, if retlen is less then bcount and no EOF
1408 * and NFSv3 a zero-fill short read occured.
1410 * For NFSv2 a short-read indicates EOF.
1412 if (retlen < bp->b_bcount && info->v3 && eof == 0) {
1413 bzero(bp->b_data + retlen, bp->b_bcount - retlen);
1414 retlen = bp->b_bcount;
1418 * If we hit an EOF we still zero-fill, but return the expected
1419 * b_resid anyway. This should normally not occur since async
1420 * BIOs are not used for read-before-write case. Races against
1421 * the server can cause it though and we don't want to leave
1422 * garbage in the buffer.
1424 if (retlen < bp->b_bcount) {
1425 bzero(bp->b_data + retlen, bp->b_bcount - retlen);
1428 /* bp->b_resid = bp->b_bcount - retlen; */
1430 lwkt_reltoken(&nmp->nm_token);
1431 kfree(info, M_NFSREQ);
1433 bp->b_error = error;
1434 bp->b_flags |= B_ERROR;
1440 * nfs write call - BIO version
1442 * NOTE: Caller has already busied the I/O.
1445 nfs_writerpc_bio(struct vnode *vp, struct bio *bio)
1447 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1448 struct nfsnode *np = VTONFS(vp);
1449 struct buf *bp = bio->bio_buf;
1454 struct nfsm_info *info;
1458 * Setup for actual write. Just clean up the bio if there
1459 * is nothing to do. b_dirtyoff/end have already been staged
1460 * by the bp's pages getting busied.
1462 if (bio->bio_offset + bp->b_dirtyend > np->n_size)
1463 bp->b_dirtyend = np->n_size - bio->bio_offset;
1465 if (bp->b_dirtyend <= bp->b_dirtyoff) {
1470 len = bp->b_dirtyend - bp->b_dirtyoff;
1471 offset = bio->bio_offset + bp->b_dirtyoff;
1472 if (offset + len > nmp->nm_maxfilesize) {
1473 bp->b_flags |= B_ERROR;
1474 bp->b_error = EFBIG;
1479 nfsstats.write_bios++;
1481 info = kmalloc(sizeof(*info), M_NFSREQ, M_WAITOK);
1483 info->v3 = NFS_ISV3(vp);
1484 info->info_writerpc.must_commit = 0;
1485 if ((bp->b_flags & (B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == 0)
1486 iomode = NFSV3WRITE_UNSTABLE;
1488 iomode = NFSV3WRITE_FILESYNC;
1490 KKASSERT(len <= nmp->nm_wsize);
1492 nfsstats.rpccnt[NFSPROC_WRITE]++;
1493 nfsm_reqhead(info, vp, NFSPROC_WRITE,
1494 NFSX_FH(info->v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1495 ERROROUT(nfsm_fhtom(info, vp));
1497 tl = nfsm_build(info, 5 * NFSX_UNSIGNED);
1498 txdr_hyper(offset, tl);
1500 *tl++ = txdr_unsigned(len);
1501 *tl++ = txdr_unsigned(iomode);
1502 *tl = txdr_unsigned(len);
1506 tl = nfsm_build(info, 4 * NFSX_UNSIGNED);
1507 /* Set both "begin" and "current" to non-garbage. */
1508 x = txdr_unsigned((u_int32_t)offset);
1509 *tl++ = x; /* "begin offset" */
1510 *tl++ = x; /* "current offset" */
1511 x = txdr_unsigned(len);
1512 *tl++ = x; /* total to this offset */
1513 *tl = x; /* size of this write */
1515 ERROROUT(nfsm_biotom(info, bio, bp->b_dirtyoff, len));
1517 info->done = nfs_writerpc_bio_done;
1518 nfsm_request_bio(info, vp, NFSPROC_WRITE, NULL,
1519 nfs_vpcred(vp, ND_WRITE));
1522 kfree(info, M_NFSREQ);
1523 bp->b_error = error;
1524 bp->b_flags |= B_ERROR;
1529 nfs_writerpc_bio_done(nfsm_info_t info)
1531 struct nfsmount *nmp = VFSTONFS(info->vp->v_mount);
1532 struct nfsnode *np = VTONFS(info->vp);
1533 struct bio *bio = info->bio;
1534 struct buf *bp = bio->bio_buf;
1535 int wccflag = NFSV3_WCCRATTR;
1536 int iomode = NFSV3WRITE_FILESYNC;
1540 int len = bp->b_resid; /* b_resid was set to shortened length */
1543 lwkt_gettoken(&nmp->nm_token);
1545 ERROROUT(info->error);
1548 * The write RPC returns a before and after mtime. The
1549 * nfsm_wcc_data() macro checks the before n_mtime
1550 * against the before time and stores the after time
1551 * in the nfsnode's cached vattr and n_mtime field.
1552 * The NRMODIFIED bit will be set if the before
1553 * time did not match the original mtime.
1555 wccflag = NFSV3_WCCCHK;
1556 ERROROUT(nfsm_wcc_data(info, info->vp, &wccflag));
1558 NULLOUT(tl = nfsm_dissect(info, 2 * NFSX_UNSIGNED + NFSX_V3WRITEVERF));
1559 rlen = fxdr_unsigned(int, *tl++);
1562 m_freem(info->mrep);
1565 } else if (rlen < len) {
1568 * XXX what do we do here?
1570 backup = len - rlen;
1571 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base - backup;
1572 uiop->uio_iov->iov_len += backup;
1573 uiop->uio_offset -= backup;
1574 uiop->uio_resid += backup;
1578 commit = fxdr_unsigned(int, *tl++);
1581 * Return the lowest committment level
1582 * obtained by any of the RPCs.
1584 if (iomode == NFSV3WRITE_FILESYNC)
1586 else if (iomode == NFSV3WRITE_DATASYNC &&
1587 commit == NFSV3WRITE_UNSTABLE)
1589 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1590 bcopy(tl, (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF);
1591 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1592 } else if (bcmp(tl, nmp->nm_verf, NFSX_V3WRITEVERF)) {
1593 info->info_writerpc.must_commit = 1;
1594 bcopy(tl, (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF);
1598 ERROROUT(nfsm_loadattr(info, info->vp, NULL));
1600 m_freem(info->mrep);
1604 if (info->vp->v_mount->mnt_flag & MNT_ASYNC)
1605 iomode = NFSV3WRITE_FILESYNC;
1609 * End of RPC. Now clean up the bp.
1611 * We no longer enable write clustering for commit operations,
1612 * See around line 1157 for a more detailed comment.
1614 if (!error && iomode == NFSV3WRITE_UNSTABLE) {
1615 bp->b_flags |= B_NEEDCOMMIT;
1617 /* XXX do not enable commit clustering */
1618 if (bp->b_dirtyoff == 0 && bp->b_dirtyend == bp->b_bcount)
1619 bp->b_flags |= B_CLUSTEROK;
1622 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1626 * For an interrupted write, the buffer is still valid
1627 * and the write hasn't been pushed to the server yet,
1628 * so we can't set B_ERROR and report the interruption
1629 * by setting B_EINTR. For the async case, B_EINTR
1630 * is not relevant, so the rpc attempt is essentially
1631 * a noop. For the case of a V3 write rpc not being
1632 * committed to stable storage, the block is still
1633 * dirty and requires either a commit rpc or another
1634 * write rpc with iomode == NFSV3WRITE_FILESYNC before
1635 * the block is reused. This is indicated by setting
1636 * the B_DELWRI and B_NEEDCOMMIT flags.
1638 * If the buffer is marked B_PAGING, it does not reside on
1639 * the vp's paging queues so we cannot call bdirty(). The
1640 * bp in this case is not an NFS cache block so we should
1643 if (error == EINTR || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
1645 bp->b_flags &= ~(B_INVAL|B_NOCACHE);
1646 if ((bp->b_flags & B_PAGING) == 0)
1649 bp->b_flags |= B_EINTR;
1653 bp->b_flags |= B_ERROR;
1654 bp->b_error = np->n_error = error;
1655 np->n_flag |= NWRITEERR;
1657 bp->b_dirtyoff = bp->b_dirtyend = 0;
1659 if (info->info_writerpc.must_commit)
1660 nfs_clearcommit(info->vp->v_mount);
1661 lwkt_reltoken(&nmp->nm_token);
1663 kfree(info, M_NFSREQ);
1665 bp->b_flags |= B_ERROR;
1666 bp->b_error = error;
1672 * Nfs Version 3 commit rpc - BIO version
1674 * This function issues the commit rpc and will chain to a write
1678 nfs_commitrpc_bio(struct vnode *vp, struct bio *bio)
1680 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1681 struct buf *bp = bio->bio_buf;
1682 struct nfsm_info *info;
1686 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) {
1687 bp->b_dirtyoff = bp->b_dirtyend = 0;
1688 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1694 info = kmalloc(sizeof(*info), M_NFSREQ, M_WAITOK);
1698 nfsstats.rpccnt[NFSPROC_COMMIT]++;
1699 nfsm_reqhead(info, vp, NFSPROC_COMMIT, NFSX_FH(1));
1700 ERROROUT(nfsm_fhtom(info, vp));
1701 tl = nfsm_build(info, 3 * NFSX_UNSIGNED);
1702 txdr_hyper(bio->bio_offset + bp->b_dirtyoff, tl);
1704 *tl = txdr_unsigned(bp->b_dirtyend - bp->b_dirtyoff);
1706 info->done = nfs_commitrpc_bio_done;
1707 nfsm_request_bio(info, vp, NFSPROC_COMMIT, NULL,
1708 nfs_vpcred(vp, ND_WRITE));
1712 * Chain to write RPC on (early) error
1714 kfree(info, M_NFSREQ);
1715 nfs_writerpc_bio(vp, bio);
1719 nfs_commitrpc_bio_done(nfsm_info_t info)
1721 struct nfsmount *nmp = VFSTONFS(info->vp->v_mount);
1722 struct bio *bio = info->bio;
1723 struct buf *bp = bio->bio_buf;
1725 int wccflag = NFSV3_WCCRATTR;
1728 lwkt_gettoken(&nmp->nm_token);
1730 ERROROUT(info->error);
1731 ERROROUT(nfsm_wcc_data(info, info->vp, &wccflag));
1733 NULLOUT(tl = nfsm_dissect(info, NFSX_V3WRITEVERF));
1734 if (bcmp(nmp->nm_verf, tl, NFSX_V3WRITEVERF)) {
1735 bcopy(tl, nmp->nm_verf, NFSX_V3WRITEVERF);
1736 error = NFSERR_STALEWRITEVERF;
1739 m_freem(info->mrep);
1743 * On completion we must chain to a write bio if an
1748 bp->b_dirtyoff = bp->b_dirtyend = 0;
1749 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1753 nfs_writerpc_bio(info->vp, bio);
1755 kfree(info, M_NFSREQ);
1756 lwkt_reltoken(&nmp->nm_token);