2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)nfs_bio.c 8.9 (Berkeley) 3/30/95
37 * $FreeBSD: /repoman/r/ncvs/src/sys/nfsclient/nfs_bio.c,v 1.130 2004/04/14 23:23:55 peadar Exp $
38 * $DragonFly: src/sys/vfs/nfs/nfs_bio.c,v 1.45 2008/07/18 00:09:39 dillon Exp $
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/resourcevar.h>
45 #include <sys/signalvar.h>
48 #include <sys/vnode.h>
49 #include <sys/mount.h>
50 #include <sys/kernel.h>
52 #include <sys/msfbuf.h>
55 #include <vm/vm_extern.h>
56 #include <vm/vm_page.h>
57 #include <vm/vm_object.h>
58 #include <vm/vm_pager.h>
59 #include <vm/vnode_pager.h>
62 #include <sys/thread2.h>
63 #include <vm/vm_page2.h>
71 #include "nfsm_subs.h"
74 static struct buf *nfs_getcacheblk(struct vnode *vp, off_t loffset,
75 int size, struct thread *td);
76 static int nfs_check_dirent(struct nfs_dirent *dp, int maxlen);
77 static void nfsiodone_sync(struct bio *bio);
78 static void nfs_readrpc_bio_done(nfsm_info_t info);
79 static void nfs_writerpc_bio_done(nfsm_info_t info);
80 static void nfs_commitrpc_bio_done(nfsm_info_t info);
83 * Vnode op for read using bio
86 nfs_bioread(struct vnode *vp, struct uio *uio, int ioflag)
88 struct nfsnode *np = VTONFS(vp);
90 struct buf *bp, *rabp;
93 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
103 if (uio->uio_rw != UIO_READ)
104 panic("nfs_read mode");
106 if (uio->uio_resid == 0)
108 if (uio->uio_offset < 0) /* XXX VDIR cookies can be negative */
112 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
113 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
114 (void)nfs_fsinfo(nmp, vp, td);
115 if (vp->v_type != VDIR &&
116 (uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
118 biosize = vp->v_mount->mnt_stat.f_iosize;
119 seqcount = (int)((off_t)(ioflag >> IO_SEQSHIFT) * biosize / BKVASIZE);
122 * For nfs, cache consistency can only be maintained approximately.
123 * Although RFC1094 does not specify the criteria, the following is
124 * believed to be compatible with the reference port.
126 * NFS: If local changes have been made and this is a
127 * directory, the directory must be invalidated and
128 * the attribute cache must be cleared.
130 * GETATTR is called to synchronize the file size.
132 * If remote changes are detected local data is flushed
133 * and the cache is invalidated.
135 * NOTE: In the normal case the attribute cache is not
136 * cleared which means GETATTR may use cached data and
137 * not immediately detect changes made on the server.
139 if ((np->n_flag & NLMODIFIED) && vp->v_type == VDIR) {
141 error = nfs_vinvalbuf(vp, V_SAVE, 1);
146 error = VOP_GETATTR(vp, &vattr);
151 * This can deadlock getpages/putpages for regular
152 * files. Only do it for directories.
154 if (np->n_flag & NRMODIFIED) {
155 if (vp->v_type == VDIR) {
157 error = nfs_vinvalbuf(vp, V_SAVE, 1);
160 np->n_flag &= ~NRMODIFIED;
165 * Loop until uio exhausted or we hit EOF
170 switch (vp->v_type) {
172 nfsstats.biocache_reads++;
173 lbn = uio->uio_offset / biosize;
174 boff = uio->uio_offset & (biosize - 1);
175 loffset = (off_t)lbn * biosize;
178 * Start the read ahead(s), as required.
180 if (nmp->nm_readahead > 0 && nfs_asyncok(nmp)) {
181 for (nra = 0; nra < nmp->nm_readahead && nra < seqcount &&
182 (off_t)(lbn + 1 + nra) * biosize < np->n_size; nra++) {
183 rabn = lbn + 1 + nra;
184 raoffset = (off_t)rabn * biosize;
185 if (findblk(vp, raoffset, FINDBLK_TEST) == NULL) {
186 rabp = nfs_getcacheblk(vp, raoffset, biosize, td);
189 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
190 rabp->b_cmd = BUF_CMD_READ;
191 vfs_busy_pages(vp, rabp);
192 nfs_asyncio(vp, &rabp->b_bio2);
201 * Obtain the buffer cache block. Figure out the buffer size
202 * when we are at EOF. If we are modifying the size of the
203 * buffer based on an EOF condition we need to hold
204 * nfs_rslock() through obtaining the buffer to prevent
205 * a potential writer-appender from messing with n_size.
206 * Otherwise we may accidently truncate the buffer and
209 * Note that bcount is *not* DEV_BSIZE aligned.
211 if (loffset + boff >= np->n_size) {
215 bp = nfs_getcacheblk(vp, loffset, biosize, td);
221 * If B_CACHE is not set, we must issue the read. If this
222 * fails, we return an error.
224 if ((bp->b_flags & B_CACHE) == 0) {
225 bp->b_cmd = BUF_CMD_READ;
226 bp->b_bio2.bio_done = nfsiodone_sync;
227 bp->b_bio2.bio_flags |= BIO_SYNC;
228 vfs_busy_pages(vp, bp);
229 error = nfs_doio(vp, &bp->b_bio2, td);
237 * on is the offset into the current bp. Figure out how many
238 * bytes we can copy out of the bp. Note that bcount is
239 * NOT DEV_BSIZE aligned.
241 * Then figure out how many bytes we can copy into the uio.
244 if (n > uio->uio_resid)
246 if (loffset + boff + n > np->n_size)
247 n = np->n_size - loffset - boff;
250 biosize = min(NFS_MAXPATHLEN, np->n_size);
251 nfsstats.biocache_readlinks++;
252 bp = nfs_getcacheblk(vp, (off_t)0, biosize, td);
255 if ((bp->b_flags & B_CACHE) == 0) {
256 bp->b_cmd = BUF_CMD_READ;
257 bp->b_bio2.bio_done = nfsiodone_sync;
258 bp->b_bio2.bio_flags |= BIO_SYNC;
259 vfs_busy_pages(vp, bp);
260 error = nfs_doio(vp, &bp->b_bio2, td);
262 bp->b_flags |= B_ERROR | B_INVAL;
267 n = szmin(uio->uio_resid, (size_t)bp->b_bcount - bp->b_resid);
271 nfsstats.biocache_readdirs++;
272 if (np->n_direofoffset &&
273 uio->uio_offset >= np->n_direofoffset
277 lbn = (uoff_t)uio->uio_offset / NFS_DIRBLKSIZ;
278 boff = uio->uio_offset & (NFS_DIRBLKSIZ - 1);
279 loffset = uio->uio_offset - boff;
280 bp = nfs_getcacheblk(vp, loffset, NFS_DIRBLKSIZ, td);
284 if ((bp->b_flags & B_CACHE) == 0) {
285 bp->b_cmd = BUF_CMD_READ;
286 bp->b_bio2.bio_done = nfsiodone_sync;
287 bp->b_bio2.bio_flags |= BIO_SYNC;
288 vfs_busy_pages(vp, bp);
289 error = nfs_doio(vp, &bp->b_bio2, td);
292 while (error == NFSERR_BAD_COOKIE) {
293 kprintf("got bad cookie vp %p bp %p\n", vp, bp);
295 error = nfs_vinvalbuf(vp, 0, 1);
297 * Yuck! The directory has been modified on the
298 * server. The only way to get the block is by
299 * reading from the beginning to get all the
302 * Leave the last bp intact unless there is an error.
303 * Loop back up to the while if the error is another
304 * NFSERR_BAD_COOKIE (double yuch!).
306 for (i = 0; i <= lbn && !error; i++) {
307 if (np->n_direofoffset
308 && (i * NFS_DIRBLKSIZ) >= np->n_direofoffset)
310 bp = nfs_getcacheblk(vp, (off_t)i * NFS_DIRBLKSIZ,
314 if ((bp->b_flags & B_CACHE) == 0) {
315 bp->b_cmd = BUF_CMD_READ;
316 bp->b_bio2.bio_done = nfsiodone_sync;
317 bp->b_bio2.bio_flags |= BIO_SYNC;
318 vfs_busy_pages(vp, bp);
319 error = nfs_doio(vp, &bp->b_bio2, td);
321 * no error + B_INVAL == directory EOF,
324 if (error == 0 && (bp->b_flags & B_INVAL))
328 * An error will throw away the block and the
329 * for loop will break out. If no error and this
330 * is not the block we want, we throw away the
331 * block and go for the next one via the for loop.
333 if (error || i < lbn)
338 * The above while is repeated if we hit another cookie
339 * error. If we hit an error and it wasn't a cookie error,
347 * If not eof and read aheads are enabled, start one.
348 * (You need the current block first, so that you have the
349 * directory offset cookie of the next block.)
351 if (nmp->nm_readahead > 0 && nfs_asyncok(nmp) &&
352 (bp->b_flags & B_INVAL) == 0 &&
353 (np->n_direofoffset == 0 ||
354 loffset + NFS_DIRBLKSIZ < np->n_direofoffset) &&
355 findblk(vp, loffset + NFS_DIRBLKSIZ, FINDBLK_TEST) == NULL
357 rabp = nfs_getcacheblk(vp, loffset + NFS_DIRBLKSIZ,
360 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
361 rabp->b_cmd = BUF_CMD_READ;
362 vfs_busy_pages(vp, rabp);
363 nfs_asyncio(vp, &rabp->b_bio2);
370 * Unlike VREG files, whos buffer size ( bp->b_bcount ) is
371 * chopped for the EOF condition, we cannot tell how large
372 * NFS directories are going to be until we hit EOF. So
373 * an NFS directory buffer is *not* chopped to its EOF. Now,
374 * it just so happens that b_resid will effectively chop it
375 * to EOF. *BUT* this information is lost if the buffer goes
376 * away and is reconstituted into a B_CACHE state ( due to
377 * being VMIO ) later. So we keep track of the directory eof
378 * in np->n_direofoffset and chop it off as an extra step
381 * NOTE: boff could already be beyond EOF.
383 if ((size_t)boff > NFS_DIRBLKSIZ - bp->b_resid) {
386 n = szmin(uio->uio_resid,
387 NFS_DIRBLKSIZ - bp->b_resid - (size_t)boff);
389 if (np->n_direofoffset &&
390 n > (size_t)(np->n_direofoffset - uio->uio_offset)) {
391 n = (size_t)(np->n_direofoffset - uio->uio_offset);
395 kprintf(" nfs_bioread: type %x unexpected\n",vp->v_type);
400 switch (vp->v_type) {
403 error = uiomove(bp->b_data + boff, n, uio);
407 error = uiomove(bp->b_data + boff, n, uio);
412 off_t old_off = uio->uio_offset;
414 struct nfs_dirent *dp;
417 * We are casting cpos to nfs_dirent, it must be
425 cpos = bp->b_data + boff;
426 epos = bp->b_data + boff + n;
427 while (cpos < epos && error == 0 && uio->uio_resid > 0) {
428 dp = (struct nfs_dirent *)cpos;
429 error = nfs_check_dirent(dp, (int)(epos - cpos));
432 if (vop_write_dirent(&error, uio, dp->nfs_ino,
433 dp->nfs_type, dp->nfs_namlen, dp->nfs_name)) {
436 cpos += dp->nfs_reclen;
440 uio->uio_offset = old_off + cpos -
446 kprintf(" nfs_bioread: type %x unexpected\n",vp->v_type);
450 } while (error == 0 && uio->uio_resid > 0 && n > 0);
455 * Userland can supply any 'seek' offset when reading a NFS directory.
456 * Validate the structure so we don't panic the kernel. Note that
457 * the element name is nul terminated and the nul is not included
462 nfs_check_dirent(struct nfs_dirent *dp, int maxlen)
464 int nfs_name_off = offsetof(struct nfs_dirent, nfs_name[0]);
466 if (nfs_name_off >= maxlen)
468 if (dp->nfs_reclen < nfs_name_off || dp->nfs_reclen > maxlen)
470 if (nfs_name_off + dp->nfs_namlen >= dp->nfs_reclen)
472 if (dp->nfs_reclen & 3)
478 * Vnode op for write using bio
480 * nfs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
481 * struct ucred *a_cred)
484 nfs_write(struct vop_write_args *ap)
486 struct uio *uio = ap->a_uio;
487 struct thread *td = uio->uio_td;
488 struct vnode *vp = ap->a_vp;
489 struct nfsnode *np = VTONFS(vp);
490 int ioflag = ap->a_ioflag;
493 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
503 if (uio->uio_rw != UIO_WRITE)
504 panic("nfs_write mode");
505 if (uio->uio_segflg == UIO_USERSPACE && uio->uio_td != curthread)
506 panic("nfs_write proc");
508 if (vp->v_type != VREG)
510 if (np->n_flag & NWRITEERR) {
511 np->n_flag &= ~NWRITEERR;
512 return (np->n_error);
514 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
515 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
516 (void)nfs_fsinfo(nmp, vp, td);
519 * Synchronously flush pending buffers if we are in synchronous
520 * mode or if we are appending.
522 if (ioflag & (IO_APPEND | IO_SYNC)) {
523 if (np->n_flag & NLMODIFIED) {
525 error = nfs_flush(vp, MNT_WAIT, td, 0);
526 /* error = nfs_vinvalbuf(vp, V_SAVE, 1); */
533 * If IO_APPEND then load uio_offset. We restart here if we cannot
534 * get the append lock.
537 if (ioflag & IO_APPEND) {
539 error = VOP_GETATTR(vp, &vattr);
542 uio->uio_offset = np->n_size;
545 if (uio->uio_offset < 0)
547 if ((uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
549 if (uio->uio_resid == 0)
553 * We need to obtain the rslock if we intend to modify np->n_size
554 * in order to guarentee the append point with multiple contending
555 * writers, to guarentee that no other appenders modify n_size
556 * while we are trying to obtain a truncated buffer (i.e. to avoid
557 * accidently truncating data written by another appender due to
558 * the race), and to ensure that the buffer is populated prior to
559 * our extending of the file. We hold rslock through the entire
562 * Note that we do not synchronize the case where someone truncates
563 * the file while we are appending to it because attempting to lock
564 * this case may deadlock other parts of the system unexpectedly.
566 if ((ioflag & IO_APPEND) ||
567 uio->uio_offset + uio->uio_resid > np->n_size) {
568 switch(nfs_rslock(np)) {
583 * Maybe this should be above the vnode op call, but so long as
584 * file servers have no limits, i don't think it matters
586 if (td && td->td_proc && uio->uio_offset + uio->uio_resid >
587 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
588 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
594 biosize = vp->v_mount->mnt_stat.f_iosize;
597 nfsstats.biocache_writes++;
598 boff = uio->uio_offset & (biosize-1);
599 loffset = uio->uio_offset - boff;
600 bytes = (int)szmin((unsigned)(biosize - boff), uio->uio_resid);
603 * Handle direct append and file extension cases, calculate
604 * unaligned buffer size. When extending B_CACHE will be
605 * set if possible. See UIO_NOCOPY note below.
607 if (uio->uio_offset + bytes > np->n_size) {
608 np->n_flag |= NLMODIFIED;
609 trivial = (uio->uio_segflg != UIO_NOCOPY &&
610 uio->uio_offset <= np->n_size);
611 nfs_meta_setsize(vp, td, uio->uio_offset + bytes,
614 bp = nfs_getcacheblk(vp, loffset, biosize, td);
621 * Actual bytes in buffer which we care about
623 if (loffset + biosize < np->n_size)
626 bcount = (int)(np->n_size - loffset);
629 * Avoid a read by setting B_CACHE where the data we
630 * intend to write covers the entire buffer. Note
631 * that the buffer may have been set to B_CACHE by
632 * nfs_meta_setsize() above or otherwise inherited the
633 * flag, but if B_CACHE isn't set the buffer may be
634 * uninitialized and must be zero'd to accomodate
635 * future seek+write's.
637 * See the comments in kern/vfs_bio.c's getblk() for
640 * When doing a UIO_NOCOPY write the buffer is not
641 * overwritten and we cannot just set B_CACHE unconditionally
642 * for full-block writes.
644 if (boff == 0 && bytes == biosize &&
645 uio->uio_segflg != UIO_NOCOPY) {
646 bp->b_flags |= B_CACHE;
647 bp->b_flags &= ~(B_ERROR | B_INVAL);
651 * b_resid may be set due to file EOF if we extended out.
652 * The NFS bio code will zero the difference anyway so
653 * just acknowledged the fact and set b_resid to 0.
655 if ((bp->b_flags & B_CACHE) == 0) {
656 bp->b_cmd = BUF_CMD_READ;
657 bp->b_bio2.bio_done = nfsiodone_sync;
658 bp->b_bio2.bio_flags |= BIO_SYNC;
659 vfs_busy_pages(vp, bp);
660 error = nfs_doio(vp, &bp->b_bio2, td);
667 np->n_flag |= NLMODIFIED;
670 * If dirtyend exceeds file size, chop it down. This should
671 * not normally occur but there is an append race where it
672 * might occur XXX, so we log it.
674 * If the chopping creates a reverse-indexed or degenerate
675 * situation with dirtyoff/end, we 0 both of them.
677 if (bp->b_dirtyend > bcount) {
678 kprintf("NFS append race @%08llx:%d\n",
679 (long long)bp->b_bio2.bio_offset,
680 bp->b_dirtyend - bcount);
681 bp->b_dirtyend = bcount;
684 if (bp->b_dirtyoff >= bp->b_dirtyend)
685 bp->b_dirtyoff = bp->b_dirtyend = 0;
688 * If the new write will leave a contiguous dirty
689 * area, just update the b_dirtyoff and b_dirtyend,
690 * otherwise force a write rpc of the old dirty area.
692 * While it is possible to merge discontiguous writes due to
693 * our having a B_CACHE buffer ( and thus valid read data
694 * for the hole), we don't because it could lead to
695 * significant cache coherency problems with multiple clients,
696 * especially if locking is implemented later on.
698 * as an optimization we could theoretically maintain
699 * a linked list of discontinuous areas, but we would still
700 * have to commit them separately so there isn't much
701 * advantage to it except perhaps a bit of asynchronization.
703 if (bp->b_dirtyend > 0 &&
704 (boff > bp->b_dirtyend ||
705 (boff + bytes) < bp->b_dirtyoff)
707 if (bwrite(bp) == EINTR) {
714 error = uiomove(bp->b_data + boff, bytes, uio);
717 * Since this block is being modified, it must be written
718 * again and not just committed. Since write clustering does
719 * not work for the stage 1 data write, only the stage 2
720 * commit rpc, we have to clear B_CLUSTEROK as well.
722 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
730 * Only update dirtyoff/dirtyend if not a degenerate
733 * The underlying VM pages have been marked valid by
734 * virtue of acquiring the bp. Because the entire buffer
735 * is marked dirty we do not have to worry about cleaning
736 * out the related dirty bits (and wouldn't really know
737 * how to deal with byte ranges anyway)
740 if (bp->b_dirtyend > 0) {
741 bp->b_dirtyoff = imin(boff, bp->b_dirtyoff);
742 bp->b_dirtyend = imax(boff + bytes,
745 bp->b_dirtyoff = boff;
746 bp->b_dirtyend = boff + bytes;
751 * If the lease is non-cachable or IO_SYNC do bwrite().
753 * IO_INVAL appears to be unused. The idea appears to be
754 * to turn off caching in this case. Very odd. XXX
756 * If nfs_async is set bawrite() will use an unstable write
757 * (build dirty bufs on the server), so we might as well
758 * push it out with bawrite(). If nfs_async is not set we
759 * use bdwrite() to cache dirty bufs on the client.
761 if (ioflag & IO_SYNC) {
762 if (ioflag & IO_INVAL)
763 bp->b_flags |= B_NOCACHE;
767 } else if (boff + bytes == biosize && nfs_async) {
772 } while (uio->uio_resid > 0 && bytes > 0);
781 * Get an nfs cache block.
783 * Allocate a new one if the block isn't currently in the cache
784 * and return the block marked busy. If the calling process is
785 * interrupted by a signal for an interruptible mount point, return
788 * The caller must carefully deal with the possible B_INVAL state of
789 * the buffer. nfs_startio() clears B_INVAL (and nfs_asyncio() clears it
790 * indirectly), so synchronous reads can be issued without worrying about
791 * the B_INVAL state. We have to be a little more careful when dealing
792 * with writes (see comments in nfs_write()) when extending a file past
796 nfs_getcacheblk(struct vnode *vp, off_t loffset, int size, struct thread *td)
800 struct nfsmount *nmp;
805 if (nmp->nm_flag & NFSMNT_INT) {
806 bp = getblk(vp, loffset, size, GETBLK_PCATCH, 0);
808 if (nfs_sigintr(nmp, NULL, td))
810 bp = getblk(vp, loffset, size, 0, 2 * hz);
813 bp = getblk(vp, loffset, size, 0, 0);
817 * bio2, the 'device' layer. Since BIOs use 64 bit byte offsets
818 * now, no translation is necessary.
820 bp->b_bio2.bio_offset = loffset;
825 * Flush and invalidate all dirty buffers. If another process is already
826 * doing the flush, just wait for completion.
829 nfs_vinvalbuf(struct vnode *vp, int flags, int intrflg)
831 struct nfsnode *np = VTONFS(vp);
832 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
833 int error = 0, slpflag, slptimeo;
834 thread_t td = curthread;
836 if (vp->v_flag & VRECLAIMED)
839 if ((nmp->nm_flag & NFSMNT_INT) == 0)
849 * First wait for any other process doing a flush to complete.
851 while (np->n_flag & NFLUSHINPROG) {
852 np->n_flag |= NFLUSHWANT;
853 error = tsleep((caddr_t)&np->n_flag, 0, "nfsvinval", slptimeo);
854 if (error && intrflg && nfs_sigintr(nmp, NULL, td))
859 * Now, flush as required.
861 np->n_flag |= NFLUSHINPROG;
862 error = vinvalbuf(vp, flags, slpflag, 0);
864 if (intrflg && nfs_sigintr(nmp, NULL, td)) {
865 np->n_flag &= ~NFLUSHINPROG;
866 if (np->n_flag & NFLUSHWANT) {
867 np->n_flag &= ~NFLUSHWANT;
868 wakeup((caddr_t)&np->n_flag);
872 error = vinvalbuf(vp, flags, 0, slptimeo);
874 np->n_flag &= ~(NLMODIFIED | NFLUSHINPROG);
875 if (np->n_flag & NFLUSHWANT) {
876 np->n_flag &= ~NFLUSHWANT;
877 wakeup((caddr_t)&np->n_flag);
883 * Return true (non-zero) if the txthread and rxthread are operational
884 * and we do not already have too many not-yet-started BIO's built up.
887 nfs_asyncok(struct nfsmount *nmp)
889 return (nmp->nm_bioqlen < nfs_maxasyncbio &&
890 nmp->nm_bioqlen < nmp->nm_maxasync_scaled / NFS_ASYSCALE &&
891 nmp->nm_rxstate <= NFSSVC_PENDING &&
892 nmp->nm_txstate <= NFSSVC_PENDING);
896 * The read-ahead code calls this to queue a bio to the txthread.
898 * We don't touch the bio otherwise... that is, we do not even
899 * construct or send the initial rpc. The txthread will do it
902 * NOTE! nm_bioqlen is not decremented until the request completes,
903 * so it does not reflect the number of bio's on bioq.
906 nfs_asyncio(struct vnode *vp, struct bio *bio)
908 struct buf *bp = bio->bio_buf;
909 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
911 KKASSERT(vp->v_tag == VT_NFS);
915 * Shortcut swap cache (not done automatically because we are not
918 if (vn_cache_strategy(vp, bio))
921 bio->bio_driver_info = vp;
923 TAILQ_INSERT_TAIL(&nmp->nm_bioq, bio, bio_act);
924 atomic_add_int(&nmp->nm_bioqlen, 1);
926 nfssvc_iod_writer_wakeup(nmp);
930 * nfs_dio() - Execute a BIO operation synchronously. The BIO will be
931 * completed and its error returned. The caller is responsible
932 * for brelse()ing it. ONLY USE FOR BIO_SYNC IOs! Otherwise
933 * our error probe will be against an invalid pointer.
935 * nfs_startio()- Execute a BIO operation assynchronously.
937 * NOTE: nfs_asyncio() is used to initiate an asynchronous BIO operation,
938 * which basically just queues it to the txthread. nfs_startio()
939 * actually initiates the I/O AFTER it has gotten to the txthread.
941 * NOTE: td might be NULL.
943 * NOTE: Caller has already busied the I/O.
946 nfs_startio(struct vnode *vp, struct bio *bio, struct thread *td)
948 struct buf *bp = bio->bio_buf;
950 struct nfsmount *nmp;
952 KKASSERT(vp->v_tag == VT_NFS);
954 nmp = VFSTONFS(vp->v_mount);
957 * clear B_ERROR and B_INVAL state prior to initiating the I/O. We
958 * do this here so we do not have to do it in all the code that
961 bp->b_flags &= ~(B_ERROR | B_INVAL);
963 KASSERT(bp->b_cmd != BUF_CMD_DONE,
964 ("nfs_doio: bp %p already marked done!", bp));
966 if (bp->b_cmd == BUF_CMD_READ) {
967 switch (vp->v_type) {
969 nfsstats.read_bios++;
970 nfs_readrpc_bio(vp, bio);
975 nfsstats.readlink_bios++;
976 nfs_readlinkrpc_bio(vp, bio);
978 nfs_doio(vp, bio, td);
983 * NOTE: If nfs_readdirplusrpc_bio() is requested but
984 * not supported, it will chain to
985 * nfs_readdirrpc_bio().
988 nfsstats.readdir_bios++;
989 uiop->uio_offset = bio->bio_offset;
990 if (nmp->nm_flag & NFSMNT_RDIRPLUS)
991 nfs_readdirplusrpc_bio(vp, bio);
993 nfs_readdirrpc_bio(vp, bio);
995 nfs_doio(vp, bio, td);
999 kprintf("nfs_doio: type %x unexpected\n",vp->v_type);
1000 bp->b_flags |= B_ERROR;
1001 bp->b_error = EINVAL;
1007 * If we only need to commit, try to commit. If this fails
1008 * it will chain through to the write. Basically all the logic
1009 * in nfs_doio() is replicated.
1011 KKASSERT(bp->b_cmd == BUF_CMD_WRITE);
1012 if (bp->b_flags & B_NEEDCOMMIT)
1013 nfs_commitrpc_bio(vp, bio);
1015 nfs_writerpc_bio(vp, bio);
1020 nfs_doio(struct vnode *vp, struct bio *bio, struct thread *td)
1022 struct buf *bp = bio->bio_buf;
1025 struct nfsmount *nmp;
1027 int iomode, must_commit;
1034 * Shortcut swap cache (not done automatically because we are not
1037 * XXX The biowait is a hack until we can figure out how to stop a
1038 * biodone chain when a middle element is BIO_SYNC. BIO_SYNC is
1039 * set so the bp shouldn't get ripped out from under us. The only
1040 * use-cases are fully synchronous I/O cases.
1042 * XXX This is having problems, give up for now.
1044 if (vn_cache_strategy(vp, bio)) {
1046 error = biowait(&bio->bio_buf->b_bio1, "nfsrsw");
1051 KKASSERT(vp->v_tag == VT_NFS);
1053 nmp = VFSTONFS(vp->v_mount);
1055 uiop->uio_iov = &io;
1056 uiop->uio_iovcnt = 1;
1057 uiop->uio_segflg = UIO_SYSSPACE;
1061 * clear B_ERROR and B_INVAL state prior to initiating the I/O. We
1062 * do this here so we do not have to do it in all the code that
1065 bp->b_flags &= ~(B_ERROR | B_INVAL);
1067 KASSERT(bp->b_cmd != BUF_CMD_DONE,
1068 ("nfs_doio: bp %p already marked done!", bp));
1070 if (bp->b_cmd == BUF_CMD_READ) {
1071 io.iov_len = uiop->uio_resid = (size_t)bp->b_bcount;
1072 io.iov_base = bp->b_data;
1073 uiop->uio_rw = UIO_READ;
1075 switch (vp->v_type) {
1078 * When reading from a regular file zero-fill any residual.
1079 * Note that this residual has nothing to do with NFS short
1080 * reads, which nfs_readrpc_uio() will handle for us.
1082 * We have to do this because when we are write extending
1083 * a file the server may not have the same notion of
1084 * filesize as we do. Our BIOs should already be sized
1085 * (b_bcount) to account for the file EOF.
1087 nfsstats.read_bios++;
1088 uiop->uio_offset = bio->bio_offset;
1089 error = nfs_readrpc_uio(vp, uiop);
1090 if (error == 0 && uiop->uio_resid) {
1091 n = (size_t)bp->b_bcount - uiop->uio_resid;
1092 bzero(bp->b_data + n, bp->b_bcount - n);
1093 uiop->uio_resid = 0;
1095 if (td && td->td_proc && (vp->v_flag & VTEXT) &&
1096 np->n_mtime != np->n_vattr.va_mtime.tv_sec) {
1097 uprintf("Process killed due to text file modification\n");
1098 ksignal(td->td_proc, SIGKILL);
1102 uiop->uio_offset = 0;
1103 nfsstats.readlink_bios++;
1104 error = nfs_readlinkrpc_uio(vp, uiop);
1107 nfsstats.readdir_bios++;
1108 uiop->uio_offset = bio->bio_offset;
1109 if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
1110 error = nfs_readdirplusrpc_uio(vp, uiop);
1111 if (error == NFSERR_NOTSUPP)
1112 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
1114 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
1115 error = nfs_readdirrpc_uio(vp, uiop);
1117 * end-of-directory sets B_INVAL but does not generate an
1120 if (error == 0 && uiop->uio_resid == bp->b_bcount)
1121 bp->b_flags |= B_INVAL;
1124 kprintf("nfs_doio: type %x unexpected\n",vp->v_type);
1128 bp->b_flags |= B_ERROR;
1129 bp->b_error = error;
1131 bp->b_resid = uiop->uio_resid;
1134 * If we only need to commit, try to commit.
1136 * NOTE: The I/O has already been staged for the write and
1137 * its pages busied, so b_dirtyoff/end is valid.
1139 KKASSERT(bp->b_cmd == BUF_CMD_WRITE);
1140 if (bp->b_flags & B_NEEDCOMMIT) {
1144 off = bio->bio_offset + bp->b_dirtyoff;
1145 retv = nfs_commitrpc_uio(vp, off,
1146 bp->b_dirtyend - bp->b_dirtyoff,
1149 bp->b_dirtyoff = bp->b_dirtyend = 0;
1150 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1155 if (retv == NFSERR_STALEWRITEVERF) {
1156 nfs_clearcommit(vp->v_mount);
1161 * Setup for actual write
1163 if (bio->bio_offset + bp->b_dirtyend > np->n_size)
1164 bp->b_dirtyend = np->n_size - bio->bio_offset;
1166 if (bp->b_dirtyend > bp->b_dirtyoff) {
1167 io.iov_len = uiop->uio_resid = bp->b_dirtyend
1169 uiop->uio_offset = bio->bio_offset + bp->b_dirtyoff;
1170 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
1171 uiop->uio_rw = UIO_WRITE;
1172 nfsstats.write_bios++;
1174 if ((bp->b_flags & (B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == 0)
1175 iomode = NFSV3WRITE_UNSTABLE;
1177 iomode = NFSV3WRITE_FILESYNC;
1180 error = nfs_writerpc_uio(vp, uiop, &iomode, &must_commit);
1183 * We no longer try to use kern/vfs_bio's cluster code to
1184 * cluster commits, so B_CLUSTEROK is no longer set with
1185 * B_NEEDCOMMIT. The problem is that a vfs_busy_pages()
1186 * may have to clear B_NEEDCOMMIT if it finds underlying
1187 * pages have been redirtied through a memory mapping
1188 * and doing this on a clustered bp will probably cause
1189 * a panic, plus the flag in the underlying NFS bufs
1190 * making up the cluster bp will not be properly cleared.
1192 if (!error && iomode == NFSV3WRITE_UNSTABLE) {
1193 bp->b_flags |= B_NEEDCOMMIT;
1195 /* XXX do not enable commit clustering */
1196 if (bp->b_dirtyoff == 0
1197 && bp->b_dirtyend == bp->b_bcount)
1198 bp->b_flags |= B_CLUSTEROK;
1201 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1205 * For an interrupted write, the buffer is still valid
1206 * and the write hasn't been pushed to the server yet,
1207 * so we can't set B_ERROR and report the interruption
1208 * by setting B_EINTR. For the async case, B_EINTR
1209 * is not relevant, so the rpc attempt is essentially
1210 * a noop. For the case of a V3 write rpc not being
1211 * committed to stable storage, the block is still
1212 * dirty and requires either a commit rpc or another
1213 * write rpc with iomode == NFSV3WRITE_FILESYNC before
1214 * the block is reused. This is indicated by setting
1215 * the B_DELWRI and B_NEEDCOMMIT flags.
1217 * If the buffer is marked B_PAGING, it does not reside on
1218 * the vp's paging queues so we cannot call bdirty(). The
1219 * bp in this case is not an NFS cache block so we should
1223 || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
1225 bp->b_flags &= ~(B_INVAL|B_NOCACHE);
1226 if ((bp->b_flags & B_PAGING) == 0)
1229 bp->b_flags |= B_EINTR;
1233 bp->b_flags |= B_ERROR;
1234 bp->b_error = np->n_error = error;
1235 np->n_flag |= NWRITEERR;
1237 bp->b_dirtyoff = bp->b_dirtyend = 0;
1240 nfs_clearcommit(vp->v_mount);
1241 bp->b_resid = uiop->uio_resid;
1248 * I/O was run synchronously, biodone() it and calculate the
1252 KKASSERT(bp->b_cmd == BUF_CMD_DONE);
1253 if (bp->b_flags & B_EINTR)
1255 if (bp->b_flags & B_ERROR)
1256 return (bp->b_error ? bp->b_error : EIO);
1261 * Handle all truncation, write-extend, and ftruncate()-extend operations
1262 * on the NFS lcient side.
1264 * We use the new API in kern/vfs_vm.c to perform these operations in a
1265 * VM-friendly way. With this API VM pages are properly zerod and pages
1266 * still mapped into the buffer straddling EOF are not invalidated.
1269 nfs_meta_setsize(struct vnode *vp, struct thread *td, off_t nsize, int trivial)
1271 struct nfsnode *np = VTONFS(vp);
1273 int biosize = vp->v_mount->mnt_stat.f_iosize;
1279 if (nsize < osize) {
1280 error = nvtruncbuf(vp, nsize, biosize, -1);
1282 error = nvextendbuf(vp, osize, nsize,
1283 biosize, biosize, -1, -1,
1290 * Synchronous completion for nfs_doio. Call bpdone() with elseit=FALSE.
1291 * Caller is responsible for brelse()'ing the bp.
1294 nfsiodone_sync(struct bio *bio)
1297 bpdone(bio->bio_buf, 0);
1301 * nfs read rpc - BIO version
1304 nfs_readrpc_bio(struct vnode *vp, struct bio *bio)
1306 struct buf *bp = bio->bio_buf;
1308 struct nfsmount *nmp;
1309 int error = 0, len, tsiz;
1310 struct nfsm_info *info;
1312 info = kmalloc(sizeof(*info), M_NFSREQ, M_WAITOK);
1314 info->v3 = NFS_ISV3(vp);
1316 nmp = VFSTONFS(vp->v_mount);
1317 tsiz = bp->b_bcount;
1318 KKASSERT(tsiz <= nmp->nm_rsize);
1319 if (bio->bio_offset + tsiz > nmp->nm_maxfilesize) {
1323 nfsstats.rpccnt[NFSPROC_READ]++;
1325 nfsm_reqhead(info, vp, NFSPROC_READ,
1326 NFSX_FH(info->v3) + NFSX_UNSIGNED * 3);
1327 ERROROUT(nfsm_fhtom(info, vp));
1328 tl = nfsm_build(info, NFSX_UNSIGNED * 3);
1330 txdr_hyper(bio->bio_offset, tl);
1331 *(tl + 2) = txdr_unsigned(len);
1333 *tl++ = txdr_unsigned(bio->bio_offset);
1334 *tl++ = txdr_unsigned(len);
1338 info->done = nfs_readrpc_bio_done;
1339 nfsm_request_bio(info, vp, NFSPROC_READ, NULL,
1340 nfs_vpcred(vp, ND_READ));
1343 kfree(info, M_NFSREQ);
1344 bp->b_error = error;
1345 bp->b_flags |= B_ERROR;
1350 nfs_readrpc_bio_done(nfsm_info_t info)
1352 struct nfsmount *nmp = VFSTONFS(info->vp->v_mount);
1353 struct bio *bio = info->bio;
1354 struct buf *bp = bio->bio_buf;
1361 KKASSERT(info->state == NFSM_STATE_DONE);
1364 ERROROUT(nfsm_postop_attr(info, info->vp, &attrflag,
1365 NFS_LATTR_NOSHRINK));
1366 NULLOUT(tl = nfsm_dissect(info, 2 * NFSX_UNSIGNED));
1367 eof = fxdr_unsigned(int, *(tl + 1));
1369 ERROROUT(nfsm_loadattr(info, info->vp, NULL));
1372 NEGATIVEOUT(retlen = nfsm_strsiz(info, nmp->nm_rsize));
1373 ERROROUT(nfsm_mtobio(info, bio, retlen));
1374 m_freem(info->mrep);
1378 * No error occured, if retlen is less then bcount and no EOF
1379 * and NFSv3 a zero-fill short read occured.
1381 * For NFSv2 a short-read indicates EOF.
1383 if (retlen < bp->b_bcount && info->v3 && eof == 0) {
1384 bzero(bp->b_data + retlen, bp->b_bcount - retlen);
1385 retlen = bp->b_bcount;
1389 * If we hit an EOF we still zero-fill, but return the expected
1390 * b_resid anyway. This should normally not occur since async
1391 * BIOs are not used for read-before-write case. Races against
1392 * the server can cause it though and we don't want to leave
1393 * garbage in the buffer.
1395 if (retlen < bp->b_bcount) {
1396 bzero(bp->b_data + retlen, bp->b_bcount - retlen);
1399 /* bp->b_resid = bp->b_bcount - retlen; */
1401 kfree(info, M_NFSREQ);
1403 bp->b_error = error;
1404 bp->b_flags |= B_ERROR;
1410 * nfs write call - BIO version
1412 * NOTE: Caller has already busied the I/O.
1415 nfs_writerpc_bio(struct vnode *vp, struct bio *bio)
1417 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1418 struct nfsnode *np = VTONFS(vp);
1419 struct buf *bp = bio->bio_buf;
1424 struct nfsm_info *info;
1428 * Setup for actual write. Just clean up the bio if there
1429 * is nothing to do. b_dirtyoff/end have already been staged
1430 * by the bp's pages getting busied.
1432 if (bio->bio_offset + bp->b_dirtyend > np->n_size)
1433 bp->b_dirtyend = np->n_size - bio->bio_offset;
1435 if (bp->b_dirtyend <= bp->b_dirtyoff) {
1440 len = bp->b_dirtyend - bp->b_dirtyoff;
1441 offset = bio->bio_offset + bp->b_dirtyoff;
1442 if (offset + len > nmp->nm_maxfilesize) {
1443 bp->b_flags |= B_ERROR;
1444 bp->b_error = EFBIG;
1449 nfsstats.write_bios++;
1451 info = kmalloc(sizeof(*info), M_NFSREQ, M_WAITOK);
1453 info->v3 = NFS_ISV3(vp);
1454 info->info_writerpc.must_commit = 0;
1455 if ((bp->b_flags & (B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == 0)
1456 iomode = NFSV3WRITE_UNSTABLE;
1458 iomode = NFSV3WRITE_FILESYNC;
1460 KKASSERT(len <= nmp->nm_wsize);
1462 nfsstats.rpccnt[NFSPROC_WRITE]++;
1463 nfsm_reqhead(info, vp, NFSPROC_WRITE,
1464 NFSX_FH(info->v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1465 ERROROUT(nfsm_fhtom(info, vp));
1467 tl = nfsm_build(info, 5 * NFSX_UNSIGNED);
1468 txdr_hyper(offset, tl);
1470 *tl++ = txdr_unsigned(len);
1471 *tl++ = txdr_unsigned(iomode);
1472 *tl = txdr_unsigned(len);
1476 tl = nfsm_build(info, 4 * NFSX_UNSIGNED);
1477 /* Set both "begin" and "current" to non-garbage. */
1478 x = txdr_unsigned((u_int32_t)offset);
1479 *tl++ = x; /* "begin offset" */
1480 *tl++ = x; /* "current offset" */
1481 x = txdr_unsigned(len);
1482 *tl++ = x; /* total to this offset */
1483 *tl = x; /* size of this write */
1485 ERROROUT(nfsm_biotom(info, bio, bp->b_dirtyoff, len));
1487 info->done = nfs_writerpc_bio_done;
1488 nfsm_request_bio(info, vp, NFSPROC_WRITE, NULL,
1489 nfs_vpcred(vp, ND_WRITE));
1492 kfree(info, M_NFSREQ);
1493 bp->b_error = error;
1494 bp->b_flags |= B_ERROR;
1499 nfs_writerpc_bio_done(nfsm_info_t info)
1501 struct nfsmount *nmp = VFSTONFS(info->vp->v_mount);
1502 struct nfsnode *np = VTONFS(info->vp);
1503 struct bio *bio = info->bio;
1504 struct buf *bp = bio->bio_buf;
1505 int wccflag = NFSV3_WCCRATTR;
1506 int iomode = NFSV3WRITE_FILESYNC;
1510 int len = bp->b_resid; /* b_resid was set to shortened length */
1515 * The write RPC returns a before and after mtime. The
1516 * nfsm_wcc_data() macro checks the before n_mtime
1517 * against the before time and stores the after time
1518 * in the nfsnode's cached vattr and n_mtime field.
1519 * The NRMODIFIED bit will be set if the before
1520 * time did not match the original mtime.
1522 wccflag = NFSV3_WCCCHK;
1523 ERROROUT(nfsm_wcc_data(info, info->vp, &wccflag));
1525 NULLOUT(tl = nfsm_dissect(info, 2 * NFSX_UNSIGNED + NFSX_V3WRITEVERF));
1526 rlen = fxdr_unsigned(int, *tl++);
1529 m_freem(info->mrep);
1532 } else if (rlen < len) {
1535 * XXX what do we do here?
1537 backup = len - rlen;
1538 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base - backup;
1539 uiop->uio_iov->iov_len += backup;
1540 uiop->uio_offset -= backup;
1541 uiop->uio_resid += backup;
1545 commit = fxdr_unsigned(int, *tl++);
1548 * Return the lowest committment level
1549 * obtained by any of the RPCs.
1551 if (iomode == NFSV3WRITE_FILESYNC)
1553 else if (iomode == NFSV3WRITE_DATASYNC &&
1554 commit == NFSV3WRITE_UNSTABLE)
1556 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1557 bcopy(tl, (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF);
1558 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1559 } else if (bcmp(tl, nmp->nm_verf, NFSX_V3WRITEVERF)) {
1560 info->info_writerpc.must_commit = 1;
1561 bcopy(tl, (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF);
1565 ERROROUT(nfsm_loadattr(info, info->vp, NULL));
1567 m_freem(info->mrep);
1571 if (info->vp->v_mount->mnt_flag & MNT_ASYNC)
1572 iomode = NFSV3WRITE_FILESYNC;
1576 * End of RPC. Now clean up the bp.
1578 * We no longer enable write clustering for commit operations,
1579 * See around line 1157 for a more detailed comment.
1581 if (!error && iomode == NFSV3WRITE_UNSTABLE) {
1582 bp->b_flags |= B_NEEDCOMMIT;
1584 /* XXX do not enable commit clustering */
1585 if (bp->b_dirtyoff == 0 && bp->b_dirtyend == bp->b_bcount)
1586 bp->b_flags |= B_CLUSTEROK;
1589 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1593 * For an interrupted write, the buffer is still valid
1594 * and the write hasn't been pushed to the server yet,
1595 * so we can't set B_ERROR and report the interruption
1596 * by setting B_EINTR. For the async case, B_EINTR
1597 * is not relevant, so the rpc attempt is essentially
1598 * a noop. For the case of a V3 write rpc not being
1599 * committed to stable storage, the block is still
1600 * dirty and requires either a commit rpc or another
1601 * write rpc with iomode == NFSV3WRITE_FILESYNC before
1602 * the block is reused. This is indicated by setting
1603 * the B_DELWRI and B_NEEDCOMMIT flags.
1605 * If the buffer is marked B_PAGING, it does not reside on
1606 * the vp's paging queues so we cannot call bdirty(). The
1607 * bp in this case is not an NFS cache block so we should
1610 if (error == EINTR || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
1612 bp->b_flags &= ~(B_INVAL|B_NOCACHE);
1613 if ((bp->b_flags & B_PAGING) == 0)
1616 bp->b_flags |= B_EINTR;
1620 bp->b_flags |= B_ERROR;
1621 bp->b_error = np->n_error = error;
1622 np->n_flag |= NWRITEERR;
1624 bp->b_dirtyoff = bp->b_dirtyend = 0;
1626 if (info->info_writerpc.must_commit)
1627 nfs_clearcommit(info->vp->v_mount);
1628 kfree(info, M_NFSREQ);
1630 bp->b_flags |= B_ERROR;
1631 bp->b_error = error;
1637 * Nfs Version 3 commit rpc - BIO version
1639 * This function issues the commit rpc and will chain to a write
1643 nfs_commitrpc_bio(struct vnode *vp, struct bio *bio)
1645 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1646 struct buf *bp = bio->bio_buf;
1647 struct nfsm_info *info;
1651 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) {
1652 bp->b_dirtyoff = bp->b_dirtyend = 0;
1653 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1659 info = kmalloc(sizeof(*info), M_NFSREQ, M_WAITOK);
1663 nfsstats.rpccnt[NFSPROC_COMMIT]++;
1664 nfsm_reqhead(info, vp, NFSPROC_COMMIT, NFSX_FH(1));
1665 ERROROUT(nfsm_fhtom(info, vp));
1666 tl = nfsm_build(info, 3 * NFSX_UNSIGNED);
1667 txdr_hyper(bio->bio_offset + bp->b_dirtyoff, tl);
1669 *tl = txdr_unsigned(bp->b_dirtyend - bp->b_dirtyoff);
1671 info->done = nfs_commitrpc_bio_done;
1672 nfsm_request_bio(info, vp, NFSPROC_COMMIT, NULL,
1673 nfs_vpcred(vp, ND_WRITE));
1677 * Chain to write RPC on (early) error
1679 kfree(info, M_NFSREQ);
1680 nfs_writerpc_bio(vp, bio);
1684 nfs_commitrpc_bio_done(nfsm_info_t info)
1686 struct nfsmount *nmp = VFSTONFS(info->vp->v_mount);
1687 struct bio *bio = info->bio;
1688 struct buf *bp = bio->bio_buf;
1690 int wccflag = NFSV3_WCCRATTR;
1693 ERROROUT(nfsm_wcc_data(info, info->vp, &wccflag));
1695 NULLOUT(tl = nfsm_dissect(info, NFSX_V3WRITEVERF));
1696 if (bcmp(nmp->nm_verf, tl, NFSX_V3WRITEVERF)) {
1697 bcopy(tl, nmp->nm_verf, NFSX_V3WRITEVERF);
1698 error = NFSERR_STALEWRITEVERF;
1701 m_freem(info->mrep);
1705 * On completion we must chain to a write bio if an
1709 kfree(info, M_NFSREQ);
1711 bp->b_dirtyoff = bp->b_dirtyend = 0;
1712 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1716 nfs_writerpc_bio(info->vp, bio);