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);
77 * Vnode op for read using bio
80 nfs_bioread(struct vnode *vp, struct uio *uio, int ioflag)
82 struct nfsnode *np = VTONFS(vp);
84 struct buf *bp, *rabp;
87 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
97 if (uio->uio_rw != UIO_READ)
98 panic("nfs_read mode");
100 if (uio->uio_resid == 0)
102 if (uio->uio_offset < 0) /* XXX VDIR cookies can be negative */
106 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
107 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
108 (void)nfs_fsinfo(nmp, vp, td);
109 if (vp->v_type != VDIR &&
110 (uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
112 biosize = vp->v_mount->mnt_stat.f_iosize;
113 seqcount = (int)((off_t)(ioflag >> IO_SEQSHIFT) * biosize / BKVASIZE);
116 * For nfs, cache consistency can only be maintained approximately.
117 * Although RFC1094 does not specify the criteria, the following is
118 * believed to be compatible with the reference port.
120 * NFS: If local changes have been made and this is a
121 * directory, the directory must be invalidated and
122 * the attribute cache must be cleared.
124 * GETATTR is called to synchronize the file size.
126 * If remote changes are detected local data is flushed
127 * and the cache is invalidated.
129 * NOTE: In the normal case the attribute cache is not
130 * cleared which means GETATTR may use cached data and
131 * not immediately detect changes made on the server.
133 if ((np->n_flag & NLMODIFIED) && vp->v_type == VDIR) {
135 error = nfs_vinvalbuf(vp, V_SAVE, 1);
140 error = VOP_GETATTR(vp, &vattr);
145 * This can deadlock getpages/putpages for regular
146 * files. Only do it for directories.
148 if (np->n_flag & NRMODIFIED) {
149 if (vp->v_type == VDIR) {
151 error = nfs_vinvalbuf(vp, V_SAVE, 1);
154 np->n_flag &= ~NRMODIFIED;
159 * Loop until uio exhausted or we hit EOF
164 switch (vp->v_type) {
166 nfsstats.biocache_reads++;
167 lbn = uio->uio_offset / biosize;
168 boff = uio->uio_offset & (biosize - 1);
169 loffset = lbn * biosize;
172 * Start the read ahead(s), as required.
174 if (nmp->nm_readahead > 0 && nfs_asyncok(nmp)) {
175 for (nra = 0; nra < nmp->nm_readahead && nra < seqcount &&
176 (off_t)(lbn + 1 + nra) * biosize < np->n_size; nra++) {
177 rabn = lbn + 1 + nra;
178 raoffset = rabn * biosize;
179 if (findblk(vp, raoffset, FINDBLK_TEST) == NULL) {
180 rabp = nfs_getcacheblk(vp, raoffset, biosize, td);
183 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
184 rabp->b_cmd = BUF_CMD_READ;
185 vfs_busy_pages(vp, rabp);
186 nfs_asyncio(vp, &rabp->b_bio2);
195 * Obtain the buffer cache block. Figure out the buffer size
196 * when we are at EOF. If we are modifying the size of the
197 * buffer based on an EOF condition we need to hold
198 * nfs_rslock() through obtaining the buffer to prevent
199 * a potential writer-appender from messing with n_size.
200 * Otherwise we may accidently truncate the buffer and
203 * Note that bcount is *not* DEV_BSIZE aligned.
205 if (loffset + boff >= np->n_size) {
209 bp = nfs_getcacheblk(vp, loffset, biosize, td);
215 * If B_CACHE is not set, we must issue the read. If this
216 * fails, we return an error.
218 if ((bp->b_flags & B_CACHE) == 0) {
219 bp->b_cmd = BUF_CMD_READ;
220 bp->b_bio2.bio_done = nfsiodone_sync;
221 bp->b_bio2.bio_flags |= BIO_SYNC;
222 vfs_busy_pages(vp, bp);
223 error = nfs_doio(vp, &bp->b_bio2, td);
231 * on is the offset into the current bp. Figure out how many
232 * bytes we can copy out of the bp. Note that bcount is
233 * NOT DEV_BSIZE aligned.
235 * Then figure out how many bytes we can copy into the uio.
238 if (n > uio->uio_resid)
240 if (loffset + boff + n > np->n_size)
241 n = np->n_size - loffset - boff;
244 biosize = min(NFS_MAXPATHLEN, np->n_size);
245 nfsstats.biocache_readlinks++;
246 bp = nfs_getcacheblk(vp, (off_t)0, biosize, td);
249 if ((bp->b_flags & B_CACHE) == 0) {
250 bp->b_cmd = BUF_CMD_READ;
251 bp->b_bio2.bio_done = nfsiodone_sync;
252 bp->b_bio2.bio_flags |= BIO_SYNC;
253 vfs_busy_pages(vp, bp);
254 error = nfs_doio(vp, &bp->b_bio2, td);
256 bp->b_flags |= B_ERROR | B_INVAL;
261 n = szmin(uio->uio_resid, (size_t)bp->b_bcount - bp->b_resid);
265 nfsstats.biocache_readdirs++;
266 if (np->n_direofoffset &&
267 uio->uio_offset >= np->n_direofoffset
271 lbn = (uoff_t)uio->uio_offset / NFS_DIRBLKSIZ;
272 boff = uio->uio_offset & (NFS_DIRBLKSIZ - 1);
273 loffset = uio->uio_offset - boff;
274 bp = nfs_getcacheblk(vp, loffset, NFS_DIRBLKSIZ, td);
278 if ((bp->b_flags & B_CACHE) == 0) {
279 bp->b_cmd = BUF_CMD_READ;
280 bp->b_bio2.bio_done = nfsiodone_sync;
281 bp->b_bio2.bio_flags |= BIO_SYNC;
282 vfs_busy_pages(vp, bp);
283 error = nfs_doio(vp, &bp->b_bio2, td);
286 while (error == NFSERR_BAD_COOKIE) {
287 kprintf("got bad cookie vp %p bp %p\n", vp, bp);
289 error = nfs_vinvalbuf(vp, 0, 1);
291 * Yuck! The directory has been modified on the
292 * server. The only way to get the block is by
293 * reading from the beginning to get all the
296 * Leave the last bp intact unless there is an error.
297 * Loop back up to the while if the error is another
298 * NFSERR_BAD_COOKIE (double yuch!).
300 for (i = 0; i <= lbn && !error; i++) {
301 if (np->n_direofoffset
302 && (i * NFS_DIRBLKSIZ) >= np->n_direofoffset)
304 bp = nfs_getcacheblk(vp, (off_t)i * NFS_DIRBLKSIZ,
308 if ((bp->b_flags & B_CACHE) == 0) {
309 bp->b_cmd = BUF_CMD_READ;
310 bp->b_bio2.bio_done = nfsiodone_sync;
311 bp->b_bio2.bio_flags |= BIO_SYNC;
312 vfs_busy_pages(vp, bp);
313 error = nfs_doio(vp, &bp->b_bio2, td);
315 * no error + B_INVAL == directory EOF,
318 if (error == 0 && (bp->b_flags & B_INVAL))
322 * An error will throw away the block and the
323 * for loop will break out. If no error and this
324 * is not the block we want, we throw away the
325 * block and go for the next one via the for loop.
327 if (error || i < lbn)
332 * The above while is repeated if we hit another cookie
333 * error. If we hit an error and it wasn't a cookie error,
341 * If not eof and read aheads are enabled, start one.
342 * (You need the current block first, so that you have the
343 * directory offset cookie of the next block.)
345 if (nmp->nm_readahead > 0 && nfs_asyncok(nmp) &&
346 (bp->b_flags & B_INVAL) == 0 &&
347 (np->n_direofoffset == 0 ||
348 loffset + NFS_DIRBLKSIZ < np->n_direofoffset) &&
349 findblk(vp, loffset + NFS_DIRBLKSIZ, FINDBLK_TEST) == NULL
351 rabp = nfs_getcacheblk(vp, loffset + NFS_DIRBLKSIZ,
354 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
355 rabp->b_cmd = BUF_CMD_READ;
356 vfs_busy_pages(vp, rabp);
357 nfs_asyncio(vp, &rabp->b_bio2);
364 * Unlike VREG files, whos buffer size ( bp->b_bcount ) is
365 * chopped for the EOF condition, we cannot tell how large
366 * NFS directories are going to be until we hit EOF. So
367 * an NFS directory buffer is *not* chopped to its EOF. Now,
368 * it just so happens that b_resid will effectively chop it
369 * to EOF. *BUT* this information is lost if the buffer goes
370 * away and is reconstituted into a B_CACHE state ( due to
371 * being VMIO ) later. So we keep track of the directory eof
372 * in np->n_direofoffset and chop it off as an extra step
375 * NOTE: boff could already be beyond EOF.
377 if ((size_t)boff > NFS_DIRBLKSIZ - bp->b_resid) {
380 n = szmin(uio->uio_resid,
381 NFS_DIRBLKSIZ - bp->b_resid - (size_t)boff);
383 if (np->n_direofoffset &&
384 n > (size_t)(np->n_direofoffset - uio->uio_offset)) {
385 n = (size_t)(np->n_direofoffset - uio->uio_offset);
389 kprintf(" nfs_bioread: type %x unexpected\n",vp->v_type);
394 switch (vp->v_type) {
397 error = uiomovebp(bp, bp->b_data + boff, n, uio);
401 error = uiomovebp(bp, bp->b_data + boff, n, uio);
406 off_t old_off = uio->uio_offset;
408 struct nfs_dirent *dp;
411 * We are casting cpos to nfs_dirent, it must be
419 cpos = bp->b_data + boff;
420 epos = bp->b_data + boff + n;
421 while (cpos < epos && error == 0 && uio->uio_resid > 0) {
422 dp = (struct nfs_dirent *)cpos;
423 error = nfs_check_dirent(dp, (int)(epos - cpos));
426 if (vop_write_dirent(&error, uio, dp->nfs_ino,
427 dp->nfs_type, dp->nfs_namlen, dp->nfs_name)) {
430 cpos += dp->nfs_reclen;
434 uio->uio_offset = old_off + cpos -
440 kprintf(" nfs_bioread: type %x unexpected\n",vp->v_type);
444 } while (error == 0 && uio->uio_resid > 0 && n > 0);
449 * Userland can supply any 'seek' offset when reading a NFS directory.
450 * Validate the structure so we don't panic the kernel. Note that
451 * the element name is nul terminated and the nul is not included
456 nfs_check_dirent(struct nfs_dirent *dp, int maxlen)
458 int nfs_name_off = offsetof(struct nfs_dirent, nfs_name[0]);
460 if (nfs_name_off >= maxlen)
462 if (dp->nfs_reclen < nfs_name_off || dp->nfs_reclen > maxlen)
464 if (nfs_name_off + dp->nfs_namlen >= dp->nfs_reclen)
466 if (dp->nfs_reclen & 3)
472 * Vnode op for write using bio
474 * nfs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
475 * struct ucred *a_cred)
478 nfs_write(struct vop_write_args *ap)
480 struct uio *uio = ap->a_uio;
481 struct thread *td = uio->uio_td;
482 struct vnode *vp = ap->a_vp;
483 struct nfsnode *np = VTONFS(vp);
484 int ioflag = ap->a_ioflag;
487 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
497 if (uio->uio_rw != UIO_WRITE)
498 panic("nfs_write mode");
499 if (uio->uio_segflg == UIO_USERSPACE && uio->uio_td != curthread)
500 panic("nfs_write proc");
502 if (vp->v_type != VREG)
505 lwkt_gettoken(&nmp->nm_token);
507 if (np->n_flag & NWRITEERR) {
508 np->n_flag &= ~NWRITEERR;
509 lwkt_reltoken(&nmp->nm_token);
510 return (np->n_error);
512 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
513 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) {
514 (void)nfs_fsinfo(nmp, vp, td);
518 * Synchronously flush pending buffers if we are in synchronous
519 * mode or if we are appending.
521 if (ioflag & (IO_APPEND | IO_SYNC)) {
522 if (np->n_flag & NLMODIFIED) {
524 error = nfs_flush(vp, MNT_WAIT, td, 0);
525 /* error = nfs_vinvalbuf(vp, V_SAVE, 1); */
532 * If IO_APPEND then load uio_offset. We restart here if we cannot
533 * get the append lock.
536 if (ioflag & IO_APPEND) {
538 error = VOP_GETATTR(vp, &vattr);
541 uio->uio_offset = np->n_size;
544 if (uio->uio_offset < 0) {
548 if ((uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize) {
552 if (uio->uio_resid == 0) {
558 * We need to obtain the rslock if we intend to modify np->n_size
559 * in order to guarentee the append point with multiple contending
560 * writers, to guarentee that no other appenders modify n_size
561 * while we are trying to obtain a truncated buffer (i.e. to avoid
562 * accidently truncating data written by another appender due to
563 * the race), and to ensure that the buffer is populated prior to
564 * our extending of the file. We hold rslock through the entire
567 * Note that we do not synchronize the case where someone truncates
568 * the file while we are appending to it because attempting to lock
569 * this case may deadlock other parts of the system unexpectedly.
571 if ((ioflag & IO_APPEND) ||
572 uio->uio_offset + uio->uio_resid > np->n_size) {
573 switch(nfs_rslock(np)) {
589 * Maybe this should be above the vnode op call, but so long as
590 * file servers have no limits, i don't think it matters
592 if (td && td->td_proc && uio->uio_offset + uio->uio_resid >
593 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
594 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
601 biosize = vp->v_mount->mnt_stat.f_iosize;
604 nfsstats.biocache_writes++;
605 boff = uio->uio_offset & (biosize-1);
606 loffset = uio->uio_offset - boff;
607 bytes = (int)szmin((unsigned)(biosize - boff), uio->uio_resid);
610 * Handle direct append and file extension cases, calculate
611 * unaligned buffer size. When extending B_CACHE will be
612 * set if possible. See UIO_NOCOPY note below.
614 if (uio->uio_offset + bytes > np->n_size) {
615 np->n_flag |= NLMODIFIED;
616 trivial = (uio->uio_segflg != UIO_NOCOPY &&
617 uio->uio_offset <= np->n_size);
618 nfs_meta_setsize(vp, td, uio->uio_offset + bytes,
621 bp = nfs_getcacheblk(vp, loffset, biosize, td);
628 * Actual bytes in buffer which we care about
630 if (loffset + biosize < np->n_size)
633 bcount = (int)(np->n_size - loffset);
636 * Avoid a read by setting B_CACHE where the data we
637 * intend to write covers the entire buffer. Note
638 * that the buffer may have been set to B_CACHE by
639 * nfs_meta_setsize() above or otherwise inherited the
640 * flag, but if B_CACHE isn't set the buffer may be
641 * uninitialized and must be zero'd to accomodate
642 * future seek+write's.
644 * See the comments in kern/vfs_bio.c's getblk() for
647 * When doing a UIO_NOCOPY write the buffer is not
648 * overwritten and we cannot just set B_CACHE unconditionally
649 * for full-block writes.
651 if (boff == 0 && bytes == biosize &&
652 uio->uio_segflg != UIO_NOCOPY) {
653 bp->b_flags |= B_CACHE;
654 bp->b_flags &= ~(B_ERROR | B_INVAL);
658 * b_resid may be set due to file EOF if we extended out.
659 * The NFS bio code will zero the difference anyway so
660 * just acknowledged the fact and set b_resid to 0.
662 if ((bp->b_flags & B_CACHE) == 0) {
663 bp->b_cmd = BUF_CMD_READ;
664 bp->b_bio2.bio_done = nfsiodone_sync;
665 bp->b_bio2.bio_flags |= BIO_SYNC;
666 vfs_busy_pages(vp, bp);
667 error = nfs_doio(vp, &bp->b_bio2, td);
674 np->n_flag |= NLMODIFIED;
677 * If dirtyend exceeds file size, chop it down. This should
678 * not normally occur but there is an append race where it
679 * might occur XXX, so we log it.
681 * If the chopping creates a reverse-indexed or degenerate
682 * situation with dirtyoff/end, we 0 both of them.
684 if (bp->b_dirtyend > bcount) {
685 kprintf("NFS append race @%08llx:%d\n",
686 (long long)bp->b_bio2.bio_offset,
687 bp->b_dirtyend - bcount);
688 bp->b_dirtyend = bcount;
691 if (bp->b_dirtyoff >= bp->b_dirtyend)
692 bp->b_dirtyoff = bp->b_dirtyend = 0;
695 * If the new write will leave a contiguous dirty
696 * area, just update the b_dirtyoff and b_dirtyend,
697 * otherwise force a write rpc of the old dirty area.
699 * While it is possible to merge discontiguous writes due to
700 * our having a B_CACHE buffer ( and thus valid read data
701 * for the hole), we don't because it could lead to
702 * significant cache coherency problems with multiple clients,
703 * especially if locking is implemented later on.
705 * as an optimization we could theoretically maintain
706 * a linked list of discontinuous areas, but we would still
707 * have to commit them separately so there isn't much
708 * advantage to it except perhaps a bit of asynchronization.
710 if (bp->b_dirtyend > 0 &&
711 (boff > bp->b_dirtyend ||
712 (boff + bytes) < bp->b_dirtyoff)
714 if (bwrite(bp) == EINTR) {
721 error = uiomovebp(bp, bp->b_data + boff, bytes, uio);
724 * Since this block is being modified, it must be written
725 * again and not just committed. Since write clustering does
726 * not work for the stage 1 data write, only the stage 2
727 * commit rpc, we have to clear B_CLUSTEROK as well.
729 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
737 * Only update dirtyoff/dirtyend if not a degenerate
740 * The underlying VM pages have been marked valid by
741 * virtue of acquiring the bp. Because the entire buffer
742 * is marked dirty we do not have to worry about cleaning
743 * out the related dirty bits (and wouldn't really know
744 * how to deal with byte ranges anyway)
747 if (bp->b_dirtyend > 0) {
748 bp->b_dirtyoff = imin(boff, bp->b_dirtyoff);
749 bp->b_dirtyend = imax(boff + bytes,
752 bp->b_dirtyoff = boff;
753 bp->b_dirtyend = boff + bytes;
758 * If the lease is non-cachable or IO_SYNC do bwrite().
760 * IO_INVAL appears to be unused. The idea appears to be
761 * to turn off caching in this case. Very odd. XXX
763 * If nfs_async is set bawrite() will use an unstable write
764 * (build dirty bufs on the server), so we might as well
765 * push it out with bawrite(). If nfs_async is not set we
766 * use bdwrite() to cache dirty bufs on the client.
768 if (ioflag & IO_SYNC) {
769 if (ioflag & IO_INVAL)
770 bp->b_flags |= B_NOCACHE;
774 } else if (boff + bytes == biosize && nfs_async) {
779 } while (uio->uio_resid > 0 && bytes > 0);
785 lwkt_reltoken(&nmp->nm_token);
790 * Get an nfs cache block.
792 * Allocate a new one if the block isn't currently in the cache
793 * and return the block marked busy. If the calling process is
794 * interrupted by a signal for an interruptible mount point, return
797 * The caller must carefully deal with the possible B_INVAL state of
798 * the buffer. nfs_startio() clears B_INVAL (and nfs_asyncio() clears it
799 * indirectly), so synchronous reads can be issued without worrying about
800 * the B_INVAL state. We have to be a little more careful when dealing
801 * with writes (see comments in nfs_write()) when extending a file past
805 nfs_getcacheblk(struct vnode *vp, off_t loffset, int size, struct thread *td)
809 struct nfsmount *nmp;
814 if (nmp->nm_flag & NFSMNT_INT) {
815 bp = getblk(vp, loffset, size, GETBLK_PCATCH, 0);
817 if (nfs_sigintr(nmp, NULL, td))
819 bp = getblk(vp, loffset, size, 0, 2 * hz);
822 bp = getblk(vp, loffset, size, 0, 0);
826 * bio2, the 'device' layer. Since BIOs use 64 bit byte offsets
827 * now, no translation is necessary.
829 bp->b_bio2.bio_offset = loffset;
834 * Flush and invalidate all dirty buffers. If another process is already
835 * doing the flush, just wait for completion.
838 nfs_vinvalbuf(struct vnode *vp, int flags, int intrflg)
840 struct nfsnode *np = VTONFS(vp);
841 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
842 int error = 0, slpflag, slptimeo;
843 thread_t td = curthread;
845 if (vp->v_flag & VRECLAIMED)
848 if ((nmp->nm_flag & NFSMNT_INT) == 0)
858 * First wait for any other process doing a flush to complete.
860 while (np->n_flag & NFLUSHINPROG) {
861 np->n_flag |= NFLUSHWANT;
862 error = tsleep((caddr_t)&np->n_flag, 0, "nfsvinval", slptimeo);
863 if (error && intrflg && nfs_sigintr(nmp, NULL, td))
868 * Now, flush as required.
870 np->n_flag |= NFLUSHINPROG;
871 error = vinvalbuf(vp, flags, slpflag, 0);
873 if (intrflg && nfs_sigintr(nmp, NULL, td)) {
874 np->n_flag &= ~NFLUSHINPROG;
875 if (np->n_flag & NFLUSHWANT) {
876 np->n_flag &= ~NFLUSHWANT;
877 wakeup((caddr_t)&np->n_flag);
881 error = vinvalbuf(vp, flags, 0, slptimeo);
883 np->n_flag &= ~(NLMODIFIED | NFLUSHINPROG);
884 if (np->n_flag & NFLUSHWANT) {
885 np->n_flag &= ~NFLUSHWANT;
886 wakeup((caddr_t)&np->n_flag);
892 * Return true (non-zero) if the txthread and rxthread are operational
893 * and we do not already have too many not-yet-started BIO's built up.
896 nfs_asyncok(struct nfsmount *nmp)
898 return (nmp->nm_bioqlen < nfs_maxasyncbio &&
899 nmp->nm_bioqlen < nmp->nm_maxasync_scaled / NFS_ASYSCALE &&
900 nmp->nm_rxstate <= NFSSVC_PENDING &&
901 nmp->nm_txstate <= NFSSVC_PENDING);
905 * The read-ahead code calls this to queue a bio to the txthread.
907 * We don't touch the bio otherwise... that is, we do not even
908 * construct or send the initial rpc. The txthread will do it
911 * NOTE! nm_bioqlen is not decremented until the request completes,
912 * so it does not reflect the number of bio's on bioq.
915 nfs_asyncio(struct vnode *vp, struct bio *bio)
917 struct buf *bp = bio->bio_buf;
918 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
920 KKASSERT(vp->v_tag == VT_NFS);
924 * Shortcut swap cache (not done automatically because we are not
927 if (vn_cache_strategy(vp, bio))
930 bio->bio_driver_info = vp;
932 TAILQ_INSERT_TAIL(&nmp->nm_bioq, bio, bio_act);
933 atomic_add_int(&nmp->nm_bioqlen, 1);
935 nfssvc_iod_writer_wakeup(nmp);
939 * nfs_doio() - Execute a BIO operation synchronously. The BIO will be
940 * completed and its error returned. The caller is responsible
941 * for brelse()ing it. ONLY USE FOR BIO_SYNC IOs! Otherwise
942 * our error probe will be against an invalid pointer.
944 * nfs_startio()- Execute a BIO operation assynchronously.
946 * NOTE: nfs_asyncio() is used to initiate an asynchronous BIO operation,
947 * which basically just queues it to the txthread. nfs_startio()
948 * actually initiates the I/O AFTER it has gotten to the txthread.
950 * NOTE: td might be NULL.
952 * NOTE: Caller has already busied the I/O.
955 nfs_startio(struct vnode *vp, struct bio *bio, struct thread *td)
957 struct buf *bp = bio->bio_buf;
959 KKASSERT(vp->v_tag == VT_NFS);
962 * clear B_ERROR and B_INVAL state prior to initiating the I/O. We
963 * do this here so we do not have to do it in all the code that
966 bp->b_flags &= ~(B_ERROR | B_INVAL);
968 KASSERT(bp->b_cmd != BUF_CMD_DONE,
969 ("nfs_doio: bp %p already marked done!", bp));
971 if (bp->b_cmd == BUF_CMD_READ) {
972 switch (vp->v_type) {
974 nfsstats.read_bios++;
975 nfs_readrpc_bio(vp, bio);
980 nfsstats.readlink_bios++;
981 nfs_readlinkrpc_bio(vp, bio);
983 nfs_doio(vp, bio, td);
988 * NOTE: If nfs_readdirplusrpc_bio() is requested but
989 * not supported, it will chain to
990 * nfs_readdirrpc_bio().
993 nfsstats.readdir_bios++;
994 uiop->uio_offset = bio->bio_offset;
995 if (nmp->nm_flag & NFSMNT_RDIRPLUS)
996 nfs_readdirplusrpc_bio(vp, bio);
998 nfs_readdirrpc_bio(vp, bio);
1000 nfs_doio(vp, bio, td);
1004 kprintf("nfs_doio: type %x unexpected\n",vp->v_type);
1005 bp->b_flags |= B_ERROR;
1006 bp->b_error = EINVAL;
1012 * If we only need to commit, try to commit. If this fails
1013 * it will chain through to the write. Basically all the logic
1014 * in nfs_doio() is replicated.
1016 KKASSERT(bp->b_cmd == BUF_CMD_WRITE);
1017 if (bp->b_flags & B_NEEDCOMMIT)
1018 nfs_commitrpc_bio(vp, bio);
1020 nfs_writerpc_bio(vp, bio);
1025 nfs_doio(struct vnode *vp, struct bio *bio, struct thread *td)
1027 struct buf *bp = bio->bio_buf;
1030 struct nfsmount *nmp;
1032 int iomode, must_commit;
1039 * Shortcut swap cache (not done automatically because we are not
1042 * XXX The biowait is a hack until we can figure out how to stop a
1043 * biodone chain when a middle element is BIO_SYNC. BIO_SYNC is
1044 * set so the bp shouldn't get ripped out from under us. The only
1045 * use-cases are fully synchronous I/O cases.
1047 * XXX This is having problems, give up for now.
1049 if (vn_cache_strategy(vp, bio)) {
1050 error = biowait(&bio->bio_buf->b_bio1, "nfsrsw");
1055 KKASSERT(vp->v_tag == VT_NFS);
1057 nmp = VFSTONFS(vp->v_mount);
1059 uiop->uio_iov = &io;
1060 uiop->uio_iovcnt = 1;
1061 uiop->uio_segflg = UIO_SYSSPACE;
1065 * clear B_ERROR and B_INVAL state prior to initiating the I/O. We
1066 * do this here so we do not have to do it in all the code that
1069 bp->b_flags &= ~(B_ERROR | B_INVAL);
1071 KASSERT(bp->b_cmd != BUF_CMD_DONE,
1072 ("nfs_doio: bp %p already marked done!", bp));
1074 if (bp->b_cmd == BUF_CMD_READ) {
1075 io.iov_len = uiop->uio_resid = (size_t)bp->b_bcount;
1076 io.iov_base = bp->b_data;
1077 uiop->uio_rw = UIO_READ;
1079 switch (vp->v_type) {
1082 * When reading from a regular file zero-fill any residual.
1083 * Note that this residual has nothing to do with NFS short
1084 * reads, which nfs_readrpc_uio() will handle for us.
1086 * We have to do this because when we are write extending
1087 * a file the server may not have the same notion of
1088 * filesize as we do. Our BIOs should already be sized
1089 * (b_bcount) to account for the file EOF.
1091 nfsstats.read_bios++;
1092 uiop->uio_offset = bio->bio_offset;
1093 error = nfs_readrpc_uio(vp, uiop);
1094 if (error == 0 && uiop->uio_resid) {
1095 n = (size_t)bp->b_bcount - uiop->uio_resid;
1096 bzero(bp->b_data + n, bp->b_bcount - n);
1097 uiop->uio_resid = 0;
1099 if (td && td->td_proc && (vp->v_flag & VTEXT) &&
1100 np->n_mtime != np->n_vattr.va_mtime.tv_sec) {
1101 uprintf("Process killed due to text file modification\n");
1102 ksignal(td->td_proc, SIGKILL);
1106 uiop->uio_offset = 0;
1107 nfsstats.readlink_bios++;
1108 error = nfs_readlinkrpc_uio(vp, uiop);
1111 nfsstats.readdir_bios++;
1112 uiop->uio_offset = bio->bio_offset;
1113 if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
1114 error = nfs_readdirplusrpc_uio(vp, uiop);
1115 if (error == NFSERR_NOTSUPP)
1116 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
1118 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
1119 error = nfs_readdirrpc_uio(vp, uiop);
1121 * end-of-directory sets B_INVAL but does not generate an
1124 if (error == 0 && uiop->uio_resid == bp->b_bcount)
1125 bp->b_flags |= B_INVAL;
1128 kprintf("nfs_doio: type %x unexpected\n",vp->v_type);
1132 bp->b_flags |= B_ERROR;
1133 bp->b_error = error;
1135 bp->b_resid = uiop->uio_resid;
1138 * If we only need to commit, try to commit.
1140 * NOTE: The I/O has already been staged for the write and
1141 * its pages busied, so b_dirtyoff/end is valid.
1143 KKASSERT(bp->b_cmd == BUF_CMD_WRITE);
1144 if (bp->b_flags & B_NEEDCOMMIT) {
1148 off = bio->bio_offset + bp->b_dirtyoff;
1149 retv = nfs_commitrpc_uio(vp, off,
1150 bp->b_dirtyend - bp->b_dirtyoff,
1153 bp->b_dirtyoff = bp->b_dirtyend = 0;
1154 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1159 if (retv == NFSERR_STALEWRITEVERF) {
1160 nfs_clearcommit(vp->v_mount);
1165 * Setup for actual write
1167 if (bio->bio_offset + bp->b_dirtyend > np->n_size)
1168 bp->b_dirtyend = np->n_size - bio->bio_offset;
1170 if (bp->b_dirtyend > bp->b_dirtyoff) {
1171 io.iov_len = uiop->uio_resid = bp->b_dirtyend
1173 uiop->uio_offset = bio->bio_offset + bp->b_dirtyoff;
1174 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
1175 uiop->uio_rw = UIO_WRITE;
1176 nfsstats.write_bios++;
1178 if ((bp->b_flags & (B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == 0)
1179 iomode = NFSV3WRITE_UNSTABLE;
1181 iomode = NFSV3WRITE_FILESYNC;
1184 error = nfs_writerpc_uio(vp, uiop, &iomode, &must_commit);
1187 * We no longer try to use kern/vfs_bio's cluster code to
1188 * cluster commits, so B_CLUSTEROK is no longer set with
1189 * B_NEEDCOMMIT. The problem is that a vfs_busy_pages()
1190 * may have to clear B_NEEDCOMMIT if it finds underlying
1191 * pages have been redirtied through a memory mapping
1192 * and doing this on a clustered bp will probably cause
1193 * a panic, plus the flag in the underlying NFS bufs
1194 * making up the cluster bp will not be properly cleared.
1196 if (!error && iomode == NFSV3WRITE_UNSTABLE) {
1197 bp->b_flags |= B_NEEDCOMMIT;
1199 /* XXX do not enable commit clustering */
1200 if (bp->b_dirtyoff == 0
1201 && bp->b_dirtyend == bp->b_bcount)
1202 bp->b_flags |= B_CLUSTEROK;
1205 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1209 * For an interrupted write, the buffer is still valid
1210 * and the write hasn't been pushed to the server yet,
1211 * so we can't set B_ERROR and report the interruption
1212 * by setting B_EINTR. For the async case, B_EINTR
1213 * is not relevant, so the rpc attempt is essentially
1214 * a noop. For the case of a V3 write rpc not being
1215 * committed to stable storage, the block is still
1216 * dirty and requires either a commit rpc or another
1217 * write rpc with iomode == NFSV3WRITE_FILESYNC before
1218 * the block is reused. This is indicated by setting
1219 * the B_DELWRI and B_NEEDCOMMIT flags.
1221 * If the buffer is marked B_PAGING, it does not reside on
1222 * the vp's paging queues so we cannot call bdirty(). The
1223 * bp in this case is not an NFS cache block so we should
1227 || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
1229 bp->b_flags &= ~(B_INVAL|B_NOCACHE);
1230 if ((bp->b_flags & B_PAGING) == 0)
1233 bp->b_flags |= B_EINTR;
1237 bp->b_flags |= B_ERROR;
1238 bp->b_error = np->n_error = error;
1239 np->n_flag |= NWRITEERR;
1241 bp->b_dirtyoff = bp->b_dirtyend = 0;
1244 nfs_clearcommit(vp->v_mount);
1245 bp->b_resid = uiop->uio_resid;
1252 * I/O was run synchronously, biodone() it and calculate the
1256 KKASSERT(bp->b_cmd == BUF_CMD_DONE);
1257 if (bp->b_flags & B_EINTR)
1259 if (bp->b_flags & B_ERROR)
1260 return (bp->b_error ? bp->b_error : EIO);
1265 * Handle all truncation, write-extend, and ftruncate()-extend operations
1266 * on the NFS lcient side.
1268 * We use the new API in kern/vfs_vm.c to perform these operations in a
1269 * VM-friendly way. With this API VM pages are properly zerod and pages
1270 * still mapped into the buffer straddling EOF are not invalidated.
1273 nfs_meta_setsize(struct vnode *vp, struct thread *td, off_t nsize, int trivial)
1275 struct nfsnode *np = VTONFS(vp);
1277 int biosize = vp->v_mount->mnt_stat.f_iosize;
1283 if (nsize < osize) {
1284 error = nvtruncbuf(vp, nsize, biosize, -1, 0);
1286 error = nvextendbuf(vp, osize, nsize,
1287 biosize, biosize, -1, -1,
1294 * Synchronous completion for nfs_doio. Call bpdone() with elseit=FALSE.
1295 * Caller is responsible for brelse()'ing the bp.
1298 nfsiodone_sync(struct bio *bio)
1301 bpdone(bio->bio_buf, 0);
1305 * nfs read rpc - BIO version
1308 nfs_readrpc_bio(struct vnode *vp, struct bio *bio)
1310 struct buf *bp = bio->bio_buf;
1312 struct nfsmount *nmp;
1313 int error = 0, len, tsiz;
1314 struct nfsm_info *info;
1316 info = kmalloc(sizeof(*info), M_NFSREQ, M_WAITOK);
1318 info->v3 = NFS_ISV3(vp);
1320 nmp = VFSTONFS(vp->v_mount);
1321 tsiz = bp->b_bcount;
1322 KKASSERT(tsiz <= nmp->nm_rsize);
1323 if (bio->bio_offset + tsiz > nmp->nm_maxfilesize) {
1327 nfsstats.rpccnt[NFSPROC_READ]++;
1329 nfsm_reqhead(info, vp, NFSPROC_READ,
1330 NFSX_FH(info->v3) + NFSX_UNSIGNED * 3);
1331 ERROROUT(nfsm_fhtom(info, vp));
1332 tl = nfsm_build(info, NFSX_UNSIGNED * 3);
1334 txdr_hyper(bio->bio_offset, tl);
1335 *(tl + 2) = txdr_unsigned(len);
1337 *tl++ = txdr_unsigned(bio->bio_offset);
1338 *tl++ = txdr_unsigned(len);
1342 info->done = nfs_readrpc_bio_done;
1343 nfsm_request_bio(info, vp, NFSPROC_READ, NULL,
1344 nfs_vpcred(vp, ND_READ));
1347 kfree(info, M_NFSREQ);
1348 bp->b_error = error;
1349 bp->b_flags |= B_ERROR;
1354 nfs_readrpc_bio_done(nfsm_info_t info)
1356 struct nfsmount *nmp = VFSTONFS(info->vp->v_mount);
1357 struct bio *bio = info->bio;
1358 struct buf *bp = bio->bio_buf;
1365 KKASSERT(info->state == NFSM_STATE_DONE);
1367 lwkt_gettoken(&nmp->nm_token);
1370 ERROROUT(nfsm_postop_attr(info, info->vp, &attrflag,
1371 NFS_LATTR_NOSHRINK));
1372 NULLOUT(tl = nfsm_dissect(info, 2 * NFSX_UNSIGNED));
1373 eof = fxdr_unsigned(int, *(tl + 1));
1375 ERROROUT(nfsm_loadattr(info, info->vp, NULL));
1378 NEGATIVEOUT(retlen = nfsm_strsiz(info, nmp->nm_rsize));
1379 ERROROUT(nfsm_mtobio(info, bio, retlen));
1380 m_freem(info->mrep);
1384 * No error occured, if retlen is less then bcount and no EOF
1385 * and NFSv3 a zero-fill short read occured.
1387 * For NFSv2 a short-read indicates EOF.
1389 if (retlen < bp->b_bcount && info->v3 && eof == 0) {
1390 bzero(bp->b_data + retlen, bp->b_bcount - retlen);
1391 retlen = bp->b_bcount;
1395 * If we hit an EOF we still zero-fill, but return the expected
1396 * b_resid anyway. This should normally not occur since async
1397 * BIOs are not used for read-before-write case. Races against
1398 * the server can cause it though and we don't want to leave
1399 * garbage in the buffer.
1401 if (retlen < bp->b_bcount) {
1402 bzero(bp->b_data + retlen, bp->b_bcount - retlen);
1405 /* bp->b_resid = bp->b_bcount - retlen; */
1407 lwkt_reltoken(&nmp->nm_token);
1408 kfree(info, M_NFSREQ);
1410 bp->b_error = error;
1411 bp->b_flags |= B_ERROR;
1417 * nfs write call - BIO version
1419 * NOTE: Caller has already busied the I/O.
1422 nfs_writerpc_bio(struct vnode *vp, struct bio *bio)
1424 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1425 struct nfsnode *np = VTONFS(vp);
1426 struct buf *bp = bio->bio_buf;
1431 struct nfsm_info *info;
1435 * Setup for actual write. Just clean up the bio if there
1436 * is nothing to do. b_dirtyoff/end have already been staged
1437 * by the bp's pages getting busied.
1439 if (bio->bio_offset + bp->b_dirtyend > np->n_size)
1440 bp->b_dirtyend = np->n_size - bio->bio_offset;
1442 if (bp->b_dirtyend <= bp->b_dirtyoff) {
1447 len = bp->b_dirtyend - bp->b_dirtyoff;
1448 offset = bio->bio_offset + bp->b_dirtyoff;
1449 if (offset + len > nmp->nm_maxfilesize) {
1450 bp->b_flags |= B_ERROR;
1451 bp->b_error = EFBIG;
1456 nfsstats.write_bios++;
1458 info = kmalloc(sizeof(*info), M_NFSREQ, M_WAITOK);
1460 info->v3 = NFS_ISV3(vp);
1461 info->info_writerpc.must_commit = 0;
1462 if ((bp->b_flags & (B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == 0)
1463 iomode = NFSV3WRITE_UNSTABLE;
1465 iomode = NFSV3WRITE_FILESYNC;
1467 KKASSERT(len <= nmp->nm_wsize);
1469 nfsstats.rpccnt[NFSPROC_WRITE]++;
1470 nfsm_reqhead(info, vp, NFSPROC_WRITE,
1471 NFSX_FH(info->v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1472 ERROROUT(nfsm_fhtom(info, vp));
1474 tl = nfsm_build(info, 5 * NFSX_UNSIGNED);
1475 txdr_hyper(offset, tl);
1477 *tl++ = txdr_unsigned(len);
1478 *tl++ = txdr_unsigned(iomode);
1479 *tl = txdr_unsigned(len);
1483 tl = nfsm_build(info, 4 * NFSX_UNSIGNED);
1484 /* Set both "begin" and "current" to non-garbage. */
1485 x = txdr_unsigned((u_int32_t)offset);
1486 *tl++ = x; /* "begin offset" */
1487 *tl++ = x; /* "current offset" */
1488 x = txdr_unsigned(len);
1489 *tl++ = x; /* total to this offset */
1490 *tl = x; /* size of this write */
1492 ERROROUT(nfsm_biotom(info, bio, bp->b_dirtyoff, len));
1494 info->done = nfs_writerpc_bio_done;
1495 nfsm_request_bio(info, vp, NFSPROC_WRITE, NULL,
1496 nfs_vpcred(vp, ND_WRITE));
1499 kfree(info, M_NFSREQ);
1500 bp->b_error = error;
1501 bp->b_flags |= B_ERROR;
1506 nfs_writerpc_bio_done(nfsm_info_t info)
1508 struct nfsmount *nmp = VFSTONFS(info->vp->v_mount);
1509 struct nfsnode *np = VTONFS(info->vp);
1510 struct bio *bio = info->bio;
1511 struct buf *bp = bio->bio_buf;
1512 int wccflag = NFSV3_WCCRATTR;
1513 int iomode = NFSV3WRITE_FILESYNC;
1517 int len = bp->b_resid; /* b_resid was set to shortened length */
1520 lwkt_gettoken(&nmp->nm_token);
1524 * The write RPC returns a before and after mtime. The
1525 * nfsm_wcc_data() macro checks the before n_mtime
1526 * against the before time and stores the after time
1527 * in the nfsnode's cached vattr and n_mtime field.
1528 * The NRMODIFIED bit will be set if the before
1529 * time did not match the original mtime.
1531 wccflag = NFSV3_WCCCHK;
1532 ERROROUT(nfsm_wcc_data(info, info->vp, &wccflag));
1534 NULLOUT(tl = nfsm_dissect(info, 2 * NFSX_UNSIGNED + NFSX_V3WRITEVERF));
1535 rlen = fxdr_unsigned(int, *tl++);
1538 m_freem(info->mrep);
1541 } else if (rlen < len) {
1544 * XXX what do we do here?
1546 backup = len - rlen;
1547 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base - backup;
1548 uiop->uio_iov->iov_len += backup;
1549 uiop->uio_offset -= backup;
1550 uiop->uio_resid += backup;
1554 commit = fxdr_unsigned(int, *tl++);
1557 * Return the lowest committment level
1558 * obtained by any of the RPCs.
1560 if (iomode == NFSV3WRITE_FILESYNC)
1562 else if (iomode == NFSV3WRITE_DATASYNC &&
1563 commit == NFSV3WRITE_UNSTABLE)
1565 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1566 bcopy(tl, (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF);
1567 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1568 } else if (bcmp(tl, nmp->nm_verf, NFSX_V3WRITEVERF)) {
1569 info->info_writerpc.must_commit = 1;
1570 bcopy(tl, (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF);
1574 ERROROUT(nfsm_loadattr(info, info->vp, NULL));
1576 m_freem(info->mrep);
1580 if (info->vp->v_mount->mnt_flag & MNT_ASYNC)
1581 iomode = NFSV3WRITE_FILESYNC;
1585 * End of RPC. Now clean up the bp.
1587 * We no longer enable write clustering for commit operations,
1588 * See around line 1157 for a more detailed comment.
1590 if (!error && iomode == NFSV3WRITE_UNSTABLE) {
1591 bp->b_flags |= B_NEEDCOMMIT;
1593 /* XXX do not enable commit clustering */
1594 if (bp->b_dirtyoff == 0 && bp->b_dirtyend == bp->b_bcount)
1595 bp->b_flags |= B_CLUSTEROK;
1598 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1602 * For an interrupted write, the buffer is still valid
1603 * and the write hasn't been pushed to the server yet,
1604 * so we can't set B_ERROR and report the interruption
1605 * by setting B_EINTR. For the async case, B_EINTR
1606 * is not relevant, so the rpc attempt is essentially
1607 * a noop. For the case of a V3 write rpc not being
1608 * committed to stable storage, the block is still
1609 * dirty and requires either a commit rpc or another
1610 * write rpc with iomode == NFSV3WRITE_FILESYNC before
1611 * the block is reused. This is indicated by setting
1612 * the B_DELWRI and B_NEEDCOMMIT flags.
1614 * If the buffer is marked B_PAGING, it does not reside on
1615 * the vp's paging queues so we cannot call bdirty(). The
1616 * bp in this case is not an NFS cache block so we should
1619 if (error == EINTR || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
1621 bp->b_flags &= ~(B_INVAL|B_NOCACHE);
1622 if ((bp->b_flags & B_PAGING) == 0)
1625 bp->b_flags |= B_EINTR;
1629 bp->b_flags |= B_ERROR;
1630 bp->b_error = np->n_error = error;
1631 np->n_flag |= NWRITEERR;
1633 bp->b_dirtyoff = bp->b_dirtyend = 0;
1635 if (info->info_writerpc.must_commit)
1636 nfs_clearcommit(info->vp->v_mount);
1637 lwkt_reltoken(&nmp->nm_token);
1639 kfree(info, M_NFSREQ);
1641 bp->b_flags |= B_ERROR;
1642 bp->b_error = error;
1648 * Nfs Version 3 commit rpc - BIO version
1650 * This function issues the commit rpc and will chain to a write
1654 nfs_commitrpc_bio(struct vnode *vp, struct bio *bio)
1656 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1657 struct buf *bp = bio->bio_buf;
1658 struct nfsm_info *info;
1662 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) {
1663 bp->b_dirtyoff = bp->b_dirtyend = 0;
1664 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1670 info = kmalloc(sizeof(*info), M_NFSREQ, M_WAITOK);
1674 nfsstats.rpccnt[NFSPROC_COMMIT]++;
1675 nfsm_reqhead(info, vp, NFSPROC_COMMIT, NFSX_FH(1));
1676 ERROROUT(nfsm_fhtom(info, vp));
1677 tl = nfsm_build(info, 3 * NFSX_UNSIGNED);
1678 txdr_hyper(bio->bio_offset + bp->b_dirtyoff, tl);
1680 *tl = txdr_unsigned(bp->b_dirtyend - bp->b_dirtyoff);
1682 info->done = nfs_commitrpc_bio_done;
1683 nfsm_request_bio(info, vp, NFSPROC_COMMIT, NULL,
1684 nfs_vpcred(vp, ND_WRITE));
1688 * Chain to write RPC on (early) error
1690 kfree(info, M_NFSREQ);
1691 nfs_writerpc_bio(vp, bio);
1695 nfs_commitrpc_bio_done(nfsm_info_t info)
1697 struct nfsmount *nmp = VFSTONFS(info->vp->v_mount);
1698 struct bio *bio = info->bio;
1699 struct buf *bp = bio->bio_buf;
1701 int wccflag = NFSV3_WCCRATTR;
1704 lwkt_gettoken(&nmp->nm_token);
1706 ERROROUT(nfsm_wcc_data(info, info->vp, &wccflag));
1708 NULLOUT(tl = nfsm_dissect(info, NFSX_V3WRITEVERF));
1709 if (bcmp(nmp->nm_verf, tl, NFSX_V3WRITEVERF)) {
1710 bcopy(tl, nmp->nm_verf, NFSX_V3WRITEVERF);
1711 error = NFSERR_STALEWRITEVERF;
1714 m_freem(info->mrep);
1718 * On completion we must chain to a write bio if an
1723 bp->b_dirtyoff = bp->b_dirtyend = 0;
1724 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1728 nfs_writerpc_bio(info->vp, bio);
1730 kfree(info, M_NFSREQ);
1731 lwkt_reltoken(&nmp->nm_token);