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 $
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/resourcevar.h>
44 #include <sys/signalvar.h>
47 #include <sys/vnode.h>
48 #include <sys/mount.h>
49 #include <sys/kernel.h>
53 #include <vm/vm_extern.h>
54 #include <vm/vm_page.h>
55 #include <vm/vm_object.h>
56 #include <vm/vm_pager.h>
57 #include <vm/vnode_pager.h>
60 #include <sys/thread2.h>
61 #include <vm/vm_page2.h>
69 #include "nfsm_subs.h"
72 static struct buf *nfs_getcacheblk(struct vnode *vp, off_t loffset,
73 int size, struct thread *td);
74 static int nfs_check_dirent(struct nfs_dirent *dp, int maxlen);
75 static void nfsiodone_sync(struct bio *bio);
76 static void nfs_readrpc_bio_done(nfsm_info_t info);
77 static void nfs_writerpc_bio_done(nfsm_info_t info);
78 static void nfs_commitrpc_bio_done(nfsm_info_t info);
81 * Vnode op for read using bio
84 nfs_bioread(struct vnode *vp, struct uio *uio, int ioflag)
86 struct nfsnode *np = VTONFS(vp);
88 struct buf *bp, *rabp;
91 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
101 if (uio->uio_rw != UIO_READ)
102 panic("nfs_read mode");
104 if (uio->uio_resid == 0)
106 if (uio->uio_offset < 0) /* XXX VDIR cookies can be negative */
110 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
111 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
112 (void)nfs_fsinfo(nmp, vp, td);
113 if (vp->v_type != VDIR &&
114 (uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
116 biosize = vp->v_mount->mnt_stat.f_iosize;
117 seqcount = (int)((off_t)(ioflag >> IO_SEQSHIFT) * biosize / BKVASIZE);
120 * For nfs, cache consistency can only be maintained approximately.
121 * Although RFC1094 does not specify the criteria, the following is
122 * believed to be compatible with the reference port.
124 * NFS: If local changes have been made and this is a
125 * directory, the directory must be invalidated and
126 * the attribute cache must be cleared.
128 * GETATTR is called to synchronize the file size.
130 * If remote changes are detected local data is flushed
131 * and the cache is invalidated.
133 * NOTE: In the normal case the attribute cache is not
134 * cleared which means GETATTR may use cached data and
135 * not immediately detect changes made on the server.
137 if ((np->n_flag & NLMODIFIED) && vp->v_type == VDIR) {
139 error = nfs_vinvalbuf(vp, V_SAVE, 1);
144 error = VOP_GETATTR(vp, &vattr);
149 * This can deadlock getpages/putpages for regular
150 * files. Only do it for directories.
152 if (np->n_flag & NRMODIFIED) {
153 if (vp->v_type == VDIR) {
155 error = nfs_vinvalbuf(vp, V_SAVE, 1);
158 np->n_flag &= ~NRMODIFIED;
163 * Loop until uio exhausted or we hit EOF
168 switch (vp->v_type) {
170 nfsstats.biocache_reads++;
171 lbn = uio->uio_offset / biosize;
172 boff = uio->uio_offset & (biosize - 1);
173 loffset = (off_t)lbn * biosize;
176 * Start the read ahead(s), as required.
178 if (nmp->nm_readahead > 0 && nfs_asyncok(nmp)) {
179 for (nra = 0; nra < nmp->nm_readahead && nra < seqcount &&
180 (off_t)(lbn + 1 + nra) * biosize < np->n_size; nra++) {
181 rabn = lbn + 1 + nra;
182 raoffset = (off_t)rabn * biosize;
183 if (findblk(vp, raoffset, FINDBLK_TEST) == NULL) {
184 rabp = nfs_getcacheblk(vp, raoffset, biosize, td);
187 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
188 rabp->b_cmd = BUF_CMD_READ;
189 vfs_busy_pages(vp, rabp);
190 nfs_asyncio(vp, &rabp->b_bio2);
199 * Obtain the buffer cache block. Figure out the buffer size
200 * when we are at EOF. If we are modifying the size of the
201 * buffer based on an EOF condition we need to hold
202 * nfs_rslock() through obtaining the buffer to prevent
203 * a potential writer-appender from messing with n_size.
204 * Otherwise we may accidently truncate the buffer and
207 * Note that bcount is *not* DEV_BSIZE aligned.
209 if (loffset + boff >= np->n_size) {
213 bp = nfs_getcacheblk(vp, loffset, biosize, td);
219 * If B_CACHE is not set, we must issue the read. If this
220 * fails, we return an error.
222 if ((bp->b_flags & B_CACHE) == 0) {
223 bp->b_cmd = BUF_CMD_READ;
224 bp->b_bio2.bio_done = nfsiodone_sync;
225 bp->b_bio2.bio_flags |= BIO_SYNC;
226 vfs_busy_pages(vp, bp);
227 error = nfs_doio(vp, &bp->b_bio2, td);
235 * on is the offset into the current bp. Figure out how many
236 * bytes we can copy out of the bp. Note that bcount is
237 * NOT DEV_BSIZE aligned.
239 * Then figure out how many bytes we can copy into the uio.
242 if (n > uio->uio_resid)
244 if (loffset + boff + n > np->n_size)
245 n = np->n_size - loffset - boff;
248 biosize = min(NFS_MAXPATHLEN, np->n_size);
249 nfsstats.biocache_readlinks++;
250 bp = nfs_getcacheblk(vp, (off_t)0, biosize, td);
253 if ((bp->b_flags & B_CACHE) == 0) {
254 bp->b_cmd = BUF_CMD_READ;
255 bp->b_bio2.bio_done = nfsiodone_sync;
256 bp->b_bio2.bio_flags |= BIO_SYNC;
257 vfs_busy_pages(vp, bp);
258 error = nfs_doio(vp, &bp->b_bio2, td);
260 bp->b_flags |= B_ERROR | B_INVAL;
265 n = szmin(uio->uio_resid, (size_t)bp->b_bcount - bp->b_resid);
269 nfsstats.biocache_readdirs++;
270 if (np->n_direofoffset &&
271 uio->uio_offset >= np->n_direofoffset
275 lbn = (uoff_t)uio->uio_offset / NFS_DIRBLKSIZ;
276 boff = uio->uio_offset & (NFS_DIRBLKSIZ - 1);
277 loffset = uio->uio_offset - boff;
278 bp = nfs_getcacheblk(vp, loffset, NFS_DIRBLKSIZ, td);
282 if ((bp->b_flags & B_CACHE) == 0) {
283 bp->b_cmd = BUF_CMD_READ;
284 bp->b_bio2.bio_done = nfsiodone_sync;
285 bp->b_bio2.bio_flags |= BIO_SYNC;
286 vfs_busy_pages(vp, bp);
287 error = nfs_doio(vp, &bp->b_bio2, td);
290 while (error == NFSERR_BAD_COOKIE) {
291 kprintf("got bad cookie vp %p bp %p\n", vp, bp);
293 error = nfs_vinvalbuf(vp, 0, 1);
295 * Yuck! The directory has been modified on the
296 * server. The only way to get the block is by
297 * reading from the beginning to get all the
300 * Leave the last bp intact unless there is an error.
301 * Loop back up to the while if the error is another
302 * NFSERR_BAD_COOKIE (double yuch!).
304 for (i = 0; i <= lbn && !error; i++) {
305 if (np->n_direofoffset
306 && (i * NFS_DIRBLKSIZ) >= np->n_direofoffset)
308 bp = nfs_getcacheblk(vp, (off_t)i * NFS_DIRBLKSIZ,
312 if ((bp->b_flags & B_CACHE) == 0) {
313 bp->b_cmd = BUF_CMD_READ;
314 bp->b_bio2.bio_done = nfsiodone_sync;
315 bp->b_bio2.bio_flags |= BIO_SYNC;
316 vfs_busy_pages(vp, bp);
317 error = nfs_doio(vp, &bp->b_bio2, td);
319 * no error + B_INVAL == directory EOF,
322 if (error == 0 && (bp->b_flags & B_INVAL))
326 * An error will throw away the block and the
327 * for loop will break out. If no error and this
328 * is not the block we want, we throw away the
329 * block and go for the next one via the for loop.
331 if (error || i < lbn)
336 * The above while is repeated if we hit another cookie
337 * error. If we hit an error and it wasn't a cookie error,
345 * If not eof and read aheads are enabled, start one.
346 * (You need the current block first, so that you have the
347 * directory offset cookie of the next block.)
349 if (nmp->nm_readahead > 0 && nfs_asyncok(nmp) &&
350 (bp->b_flags & B_INVAL) == 0 &&
351 (np->n_direofoffset == 0 ||
352 loffset + NFS_DIRBLKSIZ < np->n_direofoffset) &&
353 findblk(vp, loffset + NFS_DIRBLKSIZ, FINDBLK_TEST) == NULL
355 rabp = nfs_getcacheblk(vp, loffset + NFS_DIRBLKSIZ,
358 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
359 rabp->b_cmd = BUF_CMD_READ;
360 vfs_busy_pages(vp, rabp);
361 nfs_asyncio(vp, &rabp->b_bio2);
368 * Unlike VREG files, whos buffer size ( bp->b_bcount ) is
369 * chopped for the EOF condition, we cannot tell how large
370 * NFS directories are going to be until we hit EOF. So
371 * an NFS directory buffer is *not* chopped to its EOF. Now,
372 * it just so happens that b_resid will effectively chop it
373 * to EOF. *BUT* this information is lost if the buffer goes
374 * away and is reconstituted into a B_CACHE state ( due to
375 * being VMIO ) later. So we keep track of the directory eof
376 * in np->n_direofoffset and chop it off as an extra step
379 * NOTE: boff could already be beyond EOF.
381 if ((size_t)boff > NFS_DIRBLKSIZ - bp->b_resid) {
384 n = szmin(uio->uio_resid,
385 NFS_DIRBLKSIZ - bp->b_resid - (size_t)boff);
387 if (np->n_direofoffset &&
388 n > (size_t)(np->n_direofoffset - uio->uio_offset)) {
389 n = (size_t)(np->n_direofoffset - uio->uio_offset);
393 kprintf(" nfs_bioread: type %x unexpected\n",vp->v_type);
398 switch (vp->v_type) {
401 error = uiomove(bp->b_data + boff, n, uio);
405 error = uiomove(bp->b_data + boff, n, uio);
410 off_t old_off = uio->uio_offset;
412 struct nfs_dirent *dp;
415 * We are casting cpos to nfs_dirent, it must be
423 cpos = bp->b_data + boff;
424 epos = bp->b_data + boff + n;
425 while (cpos < epos && error == 0 && uio->uio_resid > 0) {
426 dp = (struct nfs_dirent *)cpos;
427 error = nfs_check_dirent(dp, (int)(epos - cpos));
430 if (vop_write_dirent(&error, uio, dp->nfs_ino,
431 dp->nfs_type, dp->nfs_namlen, dp->nfs_name)) {
434 cpos += dp->nfs_reclen;
438 uio->uio_offset = old_off + cpos -
444 kprintf(" nfs_bioread: type %x unexpected\n",vp->v_type);
448 } while (error == 0 && uio->uio_resid > 0 && n > 0);
453 * Userland can supply any 'seek' offset when reading a NFS directory.
454 * Validate the structure so we don't panic the kernel. Note that
455 * the element name is nul terminated and the nul is not included
460 nfs_check_dirent(struct nfs_dirent *dp, int maxlen)
462 int nfs_name_off = offsetof(struct nfs_dirent, nfs_name[0]);
464 if (nfs_name_off >= maxlen)
466 if (dp->nfs_reclen < nfs_name_off || dp->nfs_reclen > maxlen)
468 if (nfs_name_off + dp->nfs_namlen >= dp->nfs_reclen)
470 if (dp->nfs_reclen & 3)
476 * Vnode op for write using bio
478 * nfs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
479 * struct ucred *a_cred)
482 nfs_write(struct vop_write_args *ap)
484 struct uio *uio = ap->a_uio;
485 struct thread *td = uio->uio_td;
486 struct vnode *vp = ap->a_vp;
487 struct nfsnode *np = VTONFS(vp);
488 int ioflag = ap->a_ioflag;
491 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
501 if (uio->uio_rw != UIO_WRITE)
502 panic("nfs_write mode");
503 if (uio->uio_segflg == UIO_USERSPACE && uio->uio_td != curthread)
504 panic("nfs_write proc");
506 if (vp->v_type != VREG)
509 lwkt_gettoken(&nmp->nm_token);
511 if (np->n_flag & NWRITEERR) {
512 np->n_flag &= ~NWRITEERR;
513 lwkt_reltoken(&nmp->nm_token);
514 return (np->n_error);
516 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
517 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) {
518 (void)nfs_fsinfo(nmp, vp, td);
522 * Synchronously flush pending buffers if we are in synchronous
523 * mode or if we are appending.
525 if (ioflag & (IO_APPEND | IO_SYNC)) {
526 if (np->n_flag & NLMODIFIED) {
528 error = nfs_flush(vp, MNT_WAIT, td, 0);
529 /* error = nfs_vinvalbuf(vp, V_SAVE, 1); */
536 * If IO_APPEND then load uio_offset. We restart here if we cannot
537 * get the append lock.
540 if (ioflag & IO_APPEND) {
542 error = VOP_GETATTR(vp, &vattr);
545 uio->uio_offset = np->n_size;
548 if (uio->uio_offset < 0) {
552 if ((uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize) {
556 if (uio->uio_resid == 0) {
562 * We need to obtain the rslock if we intend to modify np->n_size
563 * in order to guarentee the append point with multiple contending
564 * writers, to guarentee that no other appenders modify n_size
565 * while we are trying to obtain a truncated buffer (i.e. to avoid
566 * accidently truncating data written by another appender due to
567 * the race), and to ensure that the buffer is populated prior to
568 * our extending of the file. We hold rslock through the entire
571 * Note that we do not synchronize the case where someone truncates
572 * the file while we are appending to it because attempting to lock
573 * this case may deadlock other parts of the system unexpectedly.
575 if ((ioflag & IO_APPEND) ||
576 uio->uio_offset + uio->uio_resid > np->n_size) {
577 switch(nfs_rslock(np)) {
593 * Maybe this should be above the vnode op call, but so long as
594 * file servers have no limits, i don't think it matters
596 if (td && td->td_proc && uio->uio_offset + uio->uio_resid >
597 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
598 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
605 biosize = vp->v_mount->mnt_stat.f_iosize;
608 nfsstats.biocache_writes++;
609 boff = uio->uio_offset & (biosize-1);
610 loffset = uio->uio_offset - boff;
611 bytes = (int)szmin((unsigned)(biosize - boff), uio->uio_resid);
614 * Handle direct append and file extension cases, calculate
615 * unaligned buffer size. When extending B_CACHE will be
616 * set if possible. See UIO_NOCOPY note below.
618 if (uio->uio_offset + bytes > np->n_size) {
619 np->n_flag |= NLMODIFIED;
620 trivial = (uio->uio_segflg != UIO_NOCOPY &&
621 uio->uio_offset <= np->n_size);
622 nfs_meta_setsize(vp, td, uio->uio_offset + bytes,
625 bp = nfs_getcacheblk(vp, loffset, biosize, td);
632 * Actual bytes in buffer which we care about
634 if (loffset + biosize < np->n_size)
637 bcount = (int)(np->n_size - loffset);
640 * Avoid a read by setting B_CACHE where the data we
641 * intend to write covers the entire buffer. Note
642 * that the buffer may have been set to B_CACHE by
643 * nfs_meta_setsize() above or otherwise inherited the
644 * flag, but if B_CACHE isn't set the buffer may be
645 * uninitialized and must be zero'd to accomodate
646 * future seek+write's.
648 * See the comments in kern/vfs_bio.c's getblk() for
651 * When doing a UIO_NOCOPY write the buffer is not
652 * overwritten and we cannot just set B_CACHE unconditionally
653 * for full-block writes.
655 if (boff == 0 && bytes == biosize &&
656 uio->uio_segflg != UIO_NOCOPY) {
657 bp->b_flags |= B_CACHE;
658 bp->b_flags &= ~(B_ERROR | B_INVAL);
662 * b_resid may be set due to file EOF if we extended out.
663 * The NFS bio code will zero the difference anyway so
664 * just acknowledged the fact and set b_resid to 0.
666 if ((bp->b_flags & B_CACHE) == 0) {
667 bp->b_cmd = BUF_CMD_READ;
668 bp->b_bio2.bio_done = nfsiodone_sync;
669 bp->b_bio2.bio_flags |= BIO_SYNC;
670 vfs_busy_pages(vp, bp);
671 error = nfs_doio(vp, &bp->b_bio2, td);
678 np->n_flag |= NLMODIFIED;
681 * If dirtyend exceeds file size, chop it down. This should
682 * not normally occur but there is an append race where it
683 * might occur XXX, so we log it.
685 * If the chopping creates a reverse-indexed or degenerate
686 * situation with dirtyoff/end, we 0 both of them.
688 if (bp->b_dirtyend > bcount) {
689 kprintf("NFS append race @%08llx:%d\n",
690 (long long)bp->b_bio2.bio_offset,
691 bp->b_dirtyend - bcount);
692 bp->b_dirtyend = bcount;
695 if (bp->b_dirtyoff >= bp->b_dirtyend)
696 bp->b_dirtyoff = bp->b_dirtyend = 0;
699 * If the new write will leave a contiguous dirty
700 * area, just update the b_dirtyoff and b_dirtyend,
701 * otherwise force a write rpc of the old dirty area.
703 * While it is possible to merge discontiguous writes due to
704 * our having a B_CACHE buffer ( and thus valid read data
705 * for the hole), we don't because it could lead to
706 * significant cache coherency problems with multiple clients,
707 * especially if locking is implemented later on.
709 * as an optimization we could theoretically maintain
710 * a linked list of discontinuous areas, but we would still
711 * have to commit them separately so there isn't much
712 * advantage to it except perhaps a bit of asynchronization.
714 if (bp->b_dirtyend > 0 &&
715 (boff > bp->b_dirtyend ||
716 (boff + bytes) < bp->b_dirtyoff)
718 if (bwrite(bp) == EINTR) {
725 error = uiomove(bp->b_data + boff, bytes, uio);
728 * Since this block is being modified, it must be written
729 * again and not just committed. Since write clustering does
730 * not work for the stage 1 data write, only the stage 2
731 * commit rpc, we have to clear B_CLUSTEROK as well.
733 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
741 * Only update dirtyoff/dirtyend if not a degenerate
744 * The underlying VM pages have been marked valid by
745 * virtue of acquiring the bp. Because the entire buffer
746 * is marked dirty we do not have to worry about cleaning
747 * out the related dirty bits (and wouldn't really know
748 * how to deal with byte ranges anyway)
751 if (bp->b_dirtyend > 0) {
752 bp->b_dirtyoff = imin(boff, bp->b_dirtyoff);
753 bp->b_dirtyend = imax(boff + bytes,
756 bp->b_dirtyoff = boff;
757 bp->b_dirtyend = boff + bytes;
762 * If the lease is non-cachable or IO_SYNC do bwrite().
764 * IO_INVAL appears to be unused. The idea appears to be
765 * to turn off caching in this case. Very odd. XXX
767 * If nfs_async is set bawrite() will use an unstable write
768 * (build dirty bufs on the server), so we might as well
769 * push it out with bawrite(). If nfs_async is not set we
770 * use bdwrite() to cache dirty bufs on the client.
772 if (ioflag & IO_SYNC) {
773 if (ioflag & IO_INVAL)
774 bp->b_flags |= B_NOCACHE;
778 } else if (boff + bytes == biosize && nfs_async) {
783 } while (uio->uio_resid > 0 && bytes > 0);
789 lwkt_reltoken(&nmp->nm_token);
794 * Get an nfs cache block.
796 * Allocate a new one if the block isn't currently in the cache
797 * and return the block marked busy. If the calling process is
798 * interrupted by a signal for an interruptible mount point, return
801 * The caller must carefully deal with the possible B_INVAL state of
802 * the buffer. nfs_startio() clears B_INVAL (and nfs_asyncio() clears it
803 * indirectly), so synchronous reads can be issued without worrying about
804 * the B_INVAL state. We have to be a little more careful when dealing
805 * with writes (see comments in nfs_write()) when extending a file past
809 nfs_getcacheblk(struct vnode *vp, off_t loffset, int size, struct thread *td)
813 struct nfsmount *nmp;
818 if (nmp->nm_flag & NFSMNT_INT) {
819 bp = getblk(vp, loffset, size, GETBLK_PCATCH, 0);
821 if (nfs_sigintr(nmp, NULL, td))
823 bp = getblk(vp, loffset, size, 0, 2 * hz);
826 bp = getblk(vp, loffset, size, 0, 0);
830 * bio2, the 'device' layer. Since BIOs use 64 bit byte offsets
831 * now, no translation is necessary.
833 bp->b_bio2.bio_offset = loffset;
838 * Flush and invalidate all dirty buffers. If another process is already
839 * doing the flush, just wait for completion.
842 nfs_vinvalbuf(struct vnode *vp, int flags, int intrflg)
844 struct nfsnode *np = VTONFS(vp);
845 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
846 int error = 0, slpflag, slptimeo;
847 thread_t td = curthread;
849 if (vp->v_flag & VRECLAIMED)
852 if ((nmp->nm_flag & NFSMNT_INT) == 0)
862 * First wait for any other process doing a flush to complete.
864 while (np->n_flag & NFLUSHINPROG) {
865 np->n_flag |= NFLUSHWANT;
866 error = tsleep((caddr_t)&np->n_flag, 0, "nfsvinval", slptimeo);
867 if (error && intrflg && nfs_sigintr(nmp, NULL, td))
872 * Now, flush as required.
874 np->n_flag |= NFLUSHINPROG;
875 error = vinvalbuf(vp, flags, slpflag, 0);
877 if (intrflg && nfs_sigintr(nmp, NULL, td)) {
878 np->n_flag &= ~NFLUSHINPROG;
879 if (np->n_flag & NFLUSHWANT) {
880 np->n_flag &= ~NFLUSHWANT;
881 wakeup((caddr_t)&np->n_flag);
885 error = vinvalbuf(vp, flags, 0, slptimeo);
887 np->n_flag &= ~(NLMODIFIED | NFLUSHINPROG);
888 if (np->n_flag & NFLUSHWANT) {
889 np->n_flag &= ~NFLUSHWANT;
890 wakeup((caddr_t)&np->n_flag);
896 * Return true (non-zero) if the txthread and rxthread are operational
897 * and we do not already have too many not-yet-started BIO's built up.
900 nfs_asyncok(struct nfsmount *nmp)
902 return (nmp->nm_bioqlen < nfs_maxasyncbio &&
903 nmp->nm_bioqlen < nmp->nm_maxasync_scaled / NFS_ASYSCALE &&
904 nmp->nm_rxstate <= NFSSVC_PENDING &&
905 nmp->nm_txstate <= NFSSVC_PENDING);
909 * The read-ahead code calls this to queue a bio to the txthread.
911 * We don't touch the bio otherwise... that is, we do not even
912 * construct or send the initial rpc. The txthread will do it
915 * NOTE! nm_bioqlen is not decremented until the request completes,
916 * so it does not reflect the number of bio's on bioq.
919 nfs_asyncio(struct vnode *vp, struct bio *bio)
921 struct buf *bp = bio->bio_buf;
922 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
924 KKASSERT(vp->v_tag == VT_NFS);
928 * Shortcut swap cache (not done automatically because we are not
931 if (vn_cache_strategy(vp, bio))
934 bio->bio_driver_info = vp;
936 TAILQ_INSERT_TAIL(&nmp->nm_bioq, bio, bio_act);
937 atomic_add_int(&nmp->nm_bioqlen, 1);
939 nfssvc_iod_writer_wakeup(nmp);
943 * nfs_doio() - Execute a BIO operation synchronously. The BIO will be
944 * completed and its error returned. The caller is responsible
945 * for brelse()ing it. ONLY USE FOR BIO_SYNC IOs! Otherwise
946 * our error probe will be against an invalid pointer.
948 * nfs_startio()- Execute a BIO operation assynchronously.
950 * NOTE: nfs_asyncio() is used to initiate an asynchronous BIO operation,
951 * which basically just queues it to the txthread. nfs_startio()
952 * actually initiates the I/O AFTER it has gotten to the txthread.
954 * NOTE: td might be NULL.
956 * NOTE: Caller has already busied the I/O.
959 nfs_startio(struct vnode *vp, struct bio *bio, struct thread *td)
961 struct buf *bp = bio->bio_buf;
963 struct nfsmount *nmp;
965 KKASSERT(vp->v_tag == VT_NFS);
967 nmp = VFSTONFS(vp->v_mount);
970 * clear B_ERROR and B_INVAL state prior to initiating the I/O. We
971 * do this here so we do not have to do it in all the code that
974 bp->b_flags &= ~(B_ERROR | B_INVAL);
976 KASSERT(bp->b_cmd != BUF_CMD_DONE,
977 ("nfs_doio: bp %p already marked done!", bp));
979 if (bp->b_cmd == BUF_CMD_READ) {
980 switch (vp->v_type) {
982 nfsstats.read_bios++;
983 nfs_readrpc_bio(vp, bio);
988 nfsstats.readlink_bios++;
989 nfs_readlinkrpc_bio(vp, bio);
991 nfs_doio(vp, bio, td);
996 * NOTE: If nfs_readdirplusrpc_bio() is requested but
997 * not supported, it will chain to
998 * nfs_readdirrpc_bio().
1001 nfsstats.readdir_bios++;
1002 uiop->uio_offset = bio->bio_offset;
1003 if (nmp->nm_flag & NFSMNT_RDIRPLUS)
1004 nfs_readdirplusrpc_bio(vp, bio);
1006 nfs_readdirrpc_bio(vp, bio);
1008 nfs_doio(vp, bio, td);
1012 kprintf("nfs_doio: type %x unexpected\n",vp->v_type);
1013 bp->b_flags |= B_ERROR;
1014 bp->b_error = EINVAL;
1020 * If we only need to commit, try to commit. If this fails
1021 * it will chain through to the write. Basically all the logic
1022 * in nfs_doio() is replicated.
1024 KKASSERT(bp->b_cmd == BUF_CMD_WRITE);
1025 if (bp->b_flags & B_NEEDCOMMIT)
1026 nfs_commitrpc_bio(vp, bio);
1028 nfs_writerpc_bio(vp, bio);
1033 nfs_doio(struct vnode *vp, struct bio *bio, struct thread *td)
1035 struct buf *bp = bio->bio_buf;
1038 struct nfsmount *nmp;
1040 int iomode, must_commit;
1047 * Shortcut swap cache (not done automatically because we are not
1050 * XXX The biowait is a hack until we can figure out how to stop a
1051 * biodone chain when a middle element is BIO_SYNC. BIO_SYNC is
1052 * set so the bp shouldn't get ripped out from under us. The only
1053 * use-cases are fully synchronous I/O cases.
1055 * XXX This is having problems, give up for now.
1057 if (vn_cache_strategy(vp, bio)) {
1058 error = biowait(&bio->bio_buf->b_bio1, "nfsrsw");
1063 KKASSERT(vp->v_tag == VT_NFS);
1065 nmp = VFSTONFS(vp->v_mount);
1067 uiop->uio_iov = &io;
1068 uiop->uio_iovcnt = 1;
1069 uiop->uio_segflg = UIO_SYSSPACE;
1073 * clear B_ERROR and B_INVAL state prior to initiating the I/O. We
1074 * do this here so we do not have to do it in all the code that
1077 bp->b_flags &= ~(B_ERROR | B_INVAL);
1079 KASSERT(bp->b_cmd != BUF_CMD_DONE,
1080 ("nfs_doio: bp %p already marked done!", bp));
1082 if (bp->b_cmd == BUF_CMD_READ) {
1083 io.iov_len = uiop->uio_resid = (size_t)bp->b_bcount;
1084 io.iov_base = bp->b_data;
1085 uiop->uio_rw = UIO_READ;
1087 switch (vp->v_type) {
1090 * When reading from a regular file zero-fill any residual.
1091 * Note that this residual has nothing to do with NFS short
1092 * reads, which nfs_readrpc_uio() will handle for us.
1094 * We have to do this because when we are write extending
1095 * a file the server may not have the same notion of
1096 * filesize as we do. Our BIOs should already be sized
1097 * (b_bcount) to account for the file EOF.
1099 nfsstats.read_bios++;
1100 uiop->uio_offset = bio->bio_offset;
1101 error = nfs_readrpc_uio(vp, uiop);
1102 if (error == 0 && uiop->uio_resid) {
1103 n = (size_t)bp->b_bcount - uiop->uio_resid;
1104 bzero(bp->b_data + n, bp->b_bcount - n);
1105 uiop->uio_resid = 0;
1107 if (td && td->td_proc && (vp->v_flag & VTEXT) &&
1108 np->n_mtime != np->n_vattr.va_mtime.tv_sec) {
1109 uprintf("Process killed due to text file modification\n");
1110 ksignal(td->td_proc, SIGKILL);
1114 uiop->uio_offset = 0;
1115 nfsstats.readlink_bios++;
1116 error = nfs_readlinkrpc_uio(vp, uiop);
1119 nfsstats.readdir_bios++;
1120 uiop->uio_offset = bio->bio_offset;
1121 if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
1122 error = nfs_readdirplusrpc_uio(vp, uiop);
1123 if (error == NFSERR_NOTSUPP)
1124 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
1126 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
1127 error = nfs_readdirrpc_uio(vp, uiop);
1129 * end-of-directory sets B_INVAL but does not generate an
1132 if (error == 0 && uiop->uio_resid == bp->b_bcount)
1133 bp->b_flags |= B_INVAL;
1136 kprintf("nfs_doio: type %x unexpected\n",vp->v_type);
1140 bp->b_flags |= B_ERROR;
1141 bp->b_error = error;
1143 bp->b_resid = uiop->uio_resid;
1146 * If we only need to commit, try to commit.
1148 * NOTE: The I/O has already been staged for the write and
1149 * its pages busied, so b_dirtyoff/end is valid.
1151 KKASSERT(bp->b_cmd == BUF_CMD_WRITE);
1152 if (bp->b_flags & B_NEEDCOMMIT) {
1156 off = bio->bio_offset + bp->b_dirtyoff;
1157 retv = nfs_commitrpc_uio(vp, off,
1158 bp->b_dirtyend - bp->b_dirtyoff,
1161 bp->b_dirtyoff = bp->b_dirtyend = 0;
1162 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1167 if (retv == NFSERR_STALEWRITEVERF) {
1168 nfs_clearcommit(vp->v_mount);
1173 * Setup for actual write
1175 if (bio->bio_offset + bp->b_dirtyend > np->n_size)
1176 bp->b_dirtyend = np->n_size - bio->bio_offset;
1178 if (bp->b_dirtyend > bp->b_dirtyoff) {
1179 io.iov_len = uiop->uio_resid = bp->b_dirtyend
1181 uiop->uio_offset = bio->bio_offset + bp->b_dirtyoff;
1182 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
1183 uiop->uio_rw = UIO_WRITE;
1184 nfsstats.write_bios++;
1186 if ((bp->b_flags & (B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == 0)
1187 iomode = NFSV3WRITE_UNSTABLE;
1189 iomode = NFSV3WRITE_FILESYNC;
1192 error = nfs_writerpc_uio(vp, uiop, &iomode, &must_commit);
1195 * We no longer try to use kern/vfs_bio's cluster code to
1196 * cluster commits, so B_CLUSTEROK is no longer set with
1197 * B_NEEDCOMMIT. The problem is that a vfs_busy_pages()
1198 * may have to clear B_NEEDCOMMIT if it finds underlying
1199 * pages have been redirtied through a memory mapping
1200 * and doing this on a clustered bp will probably cause
1201 * a panic, plus the flag in the underlying NFS bufs
1202 * making up the cluster bp will not be properly cleared.
1204 if (!error && iomode == NFSV3WRITE_UNSTABLE) {
1205 bp->b_flags |= B_NEEDCOMMIT;
1207 /* XXX do not enable commit clustering */
1208 if (bp->b_dirtyoff == 0
1209 && bp->b_dirtyend == bp->b_bcount)
1210 bp->b_flags |= B_CLUSTEROK;
1213 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1217 * For an interrupted write, the buffer is still valid
1218 * and the write hasn't been pushed to the server yet,
1219 * so we can't set B_ERROR and report the interruption
1220 * by setting B_EINTR. For the async case, B_EINTR
1221 * is not relevant, so the rpc attempt is essentially
1222 * a noop. For the case of a V3 write rpc not being
1223 * committed to stable storage, the block is still
1224 * dirty and requires either a commit rpc or another
1225 * write rpc with iomode == NFSV3WRITE_FILESYNC before
1226 * the block is reused. This is indicated by setting
1227 * the B_DELWRI and B_NEEDCOMMIT flags.
1229 * If the buffer is marked B_PAGING, it does not reside on
1230 * the vp's paging queues so we cannot call bdirty(). The
1231 * bp in this case is not an NFS cache block so we should
1235 || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
1237 bp->b_flags &= ~(B_INVAL|B_NOCACHE);
1238 if ((bp->b_flags & B_PAGING) == 0)
1241 bp->b_flags |= B_EINTR;
1245 bp->b_flags |= B_ERROR;
1246 bp->b_error = np->n_error = error;
1247 np->n_flag |= NWRITEERR;
1249 bp->b_dirtyoff = bp->b_dirtyend = 0;
1252 nfs_clearcommit(vp->v_mount);
1253 bp->b_resid = uiop->uio_resid;
1260 * I/O was run synchronously, biodone() it and calculate the
1264 KKASSERT(bp->b_cmd == BUF_CMD_DONE);
1265 if (bp->b_flags & B_EINTR)
1267 if (bp->b_flags & B_ERROR)
1268 return (bp->b_error ? bp->b_error : EIO);
1273 * Handle all truncation, write-extend, and ftruncate()-extend operations
1274 * on the NFS lcient side.
1276 * We use the new API in kern/vfs_vm.c to perform these operations in a
1277 * VM-friendly way. With this API VM pages are properly zerod and pages
1278 * still mapped into the buffer straddling EOF are not invalidated.
1281 nfs_meta_setsize(struct vnode *vp, struct thread *td, off_t nsize, int trivial)
1283 struct nfsnode *np = VTONFS(vp);
1285 int biosize = vp->v_mount->mnt_stat.f_iosize;
1291 if (nsize < osize) {
1292 error = nvtruncbuf(vp, nsize, biosize, -1, 0);
1294 error = nvextendbuf(vp, osize, nsize,
1295 biosize, biosize, -1, -1,
1302 * Synchronous completion for nfs_doio. Call bpdone() with elseit=FALSE.
1303 * Caller is responsible for brelse()'ing the bp.
1306 nfsiodone_sync(struct bio *bio)
1309 bpdone(bio->bio_buf, 0);
1313 * nfs read rpc - BIO version
1316 nfs_readrpc_bio(struct vnode *vp, struct bio *bio)
1318 struct buf *bp = bio->bio_buf;
1320 struct nfsmount *nmp;
1321 int error = 0, len, tsiz;
1322 struct nfsm_info *info;
1324 info = kmalloc(sizeof(*info), M_NFSREQ, M_WAITOK);
1326 info->v3 = NFS_ISV3(vp);
1328 nmp = VFSTONFS(vp->v_mount);
1329 tsiz = bp->b_bcount;
1330 KKASSERT(tsiz <= nmp->nm_rsize);
1331 if (bio->bio_offset + tsiz > nmp->nm_maxfilesize) {
1335 nfsstats.rpccnt[NFSPROC_READ]++;
1337 nfsm_reqhead(info, vp, NFSPROC_READ,
1338 NFSX_FH(info->v3) + NFSX_UNSIGNED * 3);
1339 ERROROUT(nfsm_fhtom(info, vp));
1340 tl = nfsm_build(info, NFSX_UNSIGNED * 3);
1342 txdr_hyper(bio->bio_offset, tl);
1343 *(tl + 2) = txdr_unsigned(len);
1345 *tl++ = txdr_unsigned(bio->bio_offset);
1346 *tl++ = txdr_unsigned(len);
1350 info->done = nfs_readrpc_bio_done;
1351 nfsm_request_bio(info, vp, NFSPROC_READ, NULL,
1352 nfs_vpcred(vp, ND_READ));
1355 kfree(info, M_NFSREQ);
1356 bp->b_error = error;
1357 bp->b_flags |= B_ERROR;
1362 nfs_readrpc_bio_done(nfsm_info_t info)
1364 struct nfsmount *nmp = VFSTONFS(info->vp->v_mount);
1365 struct bio *bio = info->bio;
1366 struct buf *bp = bio->bio_buf;
1373 KKASSERT(info->state == NFSM_STATE_DONE);
1375 lwkt_gettoken(&nmp->nm_token);
1378 ERROROUT(nfsm_postop_attr(info, info->vp, &attrflag,
1379 NFS_LATTR_NOSHRINK));
1380 NULLOUT(tl = nfsm_dissect(info, 2 * NFSX_UNSIGNED));
1381 eof = fxdr_unsigned(int, *(tl + 1));
1383 ERROROUT(nfsm_loadattr(info, info->vp, NULL));
1386 NEGATIVEOUT(retlen = nfsm_strsiz(info, nmp->nm_rsize));
1387 ERROROUT(nfsm_mtobio(info, bio, retlen));
1388 m_freem(info->mrep);
1392 * No error occured, if retlen is less then bcount and no EOF
1393 * and NFSv3 a zero-fill short read occured.
1395 * For NFSv2 a short-read indicates EOF.
1397 if (retlen < bp->b_bcount && info->v3 && eof == 0) {
1398 bzero(bp->b_data + retlen, bp->b_bcount - retlen);
1399 retlen = bp->b_bcount;
1403 * If we hit an EOF we still zero-fill, but return the expected
1404 * b_resid anyway. This should normally not occur since async
1405 * BIOs are not used for read-before-write case. Races against
1406 * the server can cause it though and we don't want to leave
1407 * garbage in the buffer.
1409 if (retlen < bp->b_bcount) {
1410 bzero(bp->b_data + retlen, bp->b_bcount - retlen);
1413 /* bp->b_resid = bp->b_bcount - retlen; */
1415 lwkt_reltoken(&nmp->nm_token);
1416 kfree(info, M_NFSREQ);
1418 bp->b_error = error;
1419 bp->b_flags |= B_ERROR;
1425 * nfs write call - BIO version
1427 * NOTE: Caller has already busied the I/O.
1430 nfs_writerpc_bio(struct vnode *vp, struct bio *bio)
1432 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1433 struct nfsnode *np = VTONFS(vp);
1434 struct buf *bp = bio->bio_buf;
1439 struct nfsm_info *info;
1443 * Setup for actual write. Just clean up the bio if there
1444 * is nothing to do. b_dirtyoff/end have already been staged
1445 * by the bp's pages getting busied.
1447 if (bio->bio_offset + bp->b_dirtyend > np->n_size)
1448 bp->b_dirtyend = np->n_size - bio->bio_offset;
1450 if (bp->b_dirtyend <= bp->b_dirtyoff) {
1455 len = bp->b_dirtyend - bp->b_dirtyoff;
1456 offset = bio->bio_offset + bp->b_dirtyoff;
1457 if (offset + len > nmp->nm_maxfilesize) {
1458 bp->b_flags |= B_ERROR;
1459 bp->b_error = EFBIG;
1464 nfsstats.write_bios++;
1466 info = kmalloc(sizeof(*info), M_NFSREQ, M_WAITOK);
1468 info->v3 = NFS_ISV3(vp);
1469 info->info_writerpc.must_commit = 0;
1470 if ((bp->b_flags & (B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == 0)
1471 iomode = NFSV3WRITE_UNSTABLE;
1473 iomode = NFSV3WRITE_FILESYNC;
1475 KKASSERT(len <= nmp->nm_wsize);
1477 nfsstats.rpccnt[NFSPROC_WRITE]++;
1478 nfsm_reqhead(info, vp, NFSPROC_WRITE,
1479 NFSX_FH(info->v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1480 ERROROUT(nfsm_fhtom(info, vp));
1482 tl = nfsm_build(info, 5 * NFSX_UNSIGNED);
1483 txdr_hyper(offset, tl);
1485 *tl++ = txdr_unsigned(len);
1486 *tl++ = txdr_unsigned(iomode);
1487 *tl = txdr_unsigned(len);
1491 tl = nfsm_build(info, 4 * NFSX_UNSIGNED);
1492 /* Set both "begin" and "current" to non-garbage. */
1493 x = txdr_unsigned((u_int32_t)offset);
1494 *tl++ = x; /* "begin offset" */
1495 *tl++ = x; /* "current offset" */
1496 x = txdr_unsigned(len);
1497 *tl++ = x; /* total to this offset */
1498 *tl = x; /* size of this write */
1500 ERROROUT(nfsm_biotom(info, bio, bp->b_dirtyoff, len));
1502 info->done = nfs_writerpc_bio_done;
1503 nfsm_request_bio(info, vp, NFSPROC_WRITE, NULL,
1504 nfs_vpcred(vp, ND_WRITE));
1507 kfree(info, M_NFSREQ);
1508 bp->b_error = error;
1509 bp->b_flags |= B_ERROR;
1514 nfs_writerpc_bio_done(nfsm_info_t info)
1516 struct nfsmount *nmp = VFSTONFS(info->vp->v_mount);
1517 struct nfsnode *np = VTONFS(info->vp);
1518 struct bio *bio = info->bio;
1519 struct buf *bp = bio->bio_buf;
1520 int wccflag = NFSV3_WCCRATTR;
1521 int iomode = NFSV3WRITE_FILESYNC;
1525 int len = bp->b_resid; /* b_resid was set to shortened length */
1528 lwkt_gettoken(&nmp->nm_token);
1532 * The write RPC returns a before and after mtime. The
1533 * nfsm_wcc_data() macro checks the before n_mtime
1534 * against the before time and stores the after time
1535 * in the nfsnode's cached vattr and n_mtime field.
1536 * The NRMODIFIED bit will be set if the before
1537 * time did not match the original mtime.
1539 wccflag = NFSV3_WCCCHK;
1540 ERROROUT(nfsm_wcc_data(info, info->vp, &wccflag));
1542 NULLOUT(tl = nfsm_dissect(info, 2 * NFSX_UNSIGNED + NFSX_V3WRITEVERF));
1543 rlen = fxdr_unsigned(int, *tl++);
1546 m_freem(info->mrep);
1549 } else if (rlen < len) {
1552 * XXX what do we do here?
1554 backup = len - rlen;
1555 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base - backup;
1556 uiop->uio_iov->iov_len += backup;
1557 uiop->uio_offset -= backup;
1558 uiop->uio_resid += backup;
1562 commit = fxdr_unsigned(int, *tl++);
1565 * Return the lowest committment level
1566 * obtained by any of the RPCs.
1568 if (iomode == NFSV3WRITE_FILESYNC)
1570 else if (iomode == NFSV3WRITE_DATASYNC &&
1571 commit == NFSV3WRITE_UNSTABLE)
1573 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1574 bcopy(tl, (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF);
1575 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1576 } else if (bcmp(tl, nmp->nm_verf, NFSX_V3WRITEVERF)) {
1577 info->info_writerpc.must_commit = 1;
1578 bcopy(tl, (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF);
1582 ERROROUT(nfsm_loadattr(info, info->vp, NULL));
1584 m_freem(info->mrep);
1588 if (info->vp->v_mount->mnt_flag & MNT_ASYNC)
1589 iomode = NFSV3WRITE_FILESYNC;
1593 * End of RPC. Now clean up the bp.
1595 * We no longer enable write clustering for commit operations,
1596 * See around line 1157 for a more detailed comment.
1598 if (!error && iomode == NFSV3WRITE_UNSTABLE) {
1599 bp->b_flags |= B_NEEDCOMMIT;
1601 /* XXX do not enable commit clustering */
1602 if (bp->b_dirtyoff == 0 && bp->b_dirtyend == bp->b_bcount)
1603 bp->b_flags |= B_CLUSTEROK;
1606 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1610 * For an interrupted write, the buffer is still valid
1611 * and the write hasn't been pushed to the server yet,
1612 * so we can't set B_ERROR and report the interruption
1613 * by setting B_EINTR. For the async case, B_EINTR
1614 * is not relevant, so the rpc attempt is essentially
1615 * a noop. For the case of a V3 write rpc not being
1616 * committed to stable storage, the block is still
1617 * dirty and requires either a commit rpc or another
1618 * write rpc with iomode == NFSV3WRITE_FILESYNC before
1619 * the block is reused. This is indicated by setting
1620 * the B_DELWRI and B_NEEDCOMMIT flags.
1622 * If the buffer is marked B_PAGING, it does not reside on
1623 * the vp's paging queues so we cannot call bdirty(). The
1624 * bp in this case is not an NFS cache block so we should
1627 if (error == EINTR || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
1629 bp->b_flags &= ~(B_INVAL|B_NOCACHE);
1630 if ((bp->b_flags & B_PAGING) == 0)
1633 bp->b_flags |= B_EINTR;
1637 bp->b_flags |= B_ERROR;
1638 bp->b_error = np->n_error = error;
1639 np->n_flag |= NWRITEERR;
1641 bp->b_dirtyoff = bp->b_dirtyend = 0;
1643 if (info->info_writerpc.must_commit)
1644 nfs_clearcommit(info->vp->v_mount);
1645 lwkt_reltoken(&nmp->nm_token);
1647 kfree(info, M_NFSREQ);
1649 bp->b_flags |= B_ERROR;
1650 bp->b_error = error;
1656 * Nfs Version 3 commit rpc - BIO version
1658 * This function issues the commit rpc and will chain to a write
1662 nfs_commitrpc_bio(struct vnode *vp, struct bio *bio)
1664 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1665 struct buf *bp = bio->bio_buf;
1666 struct nfsm_info *info;
1670 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) {
1671 bp->b_dirtyoff = bp->b_dirtyend = 0;
1672 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1678 info = kmalloc(sizeof(*info), M_NFSREQ, M_WAITOK);
1682 nfsstats.rpccnt[NFSPROC_COMMIT]++;
1683 nfsm_reqhead(info, vp, NFSPROC_COMMIT, NFSX_FH(1));
1684 ERROROUT(nfsm_fhtom(info, vp));
1685 tl = nfsm_build(info, 3 * NFSX_UNSIGNED);
1686 txdr_hyper(bio->bio_offset + bp->b_dirtyoff, tl);
1688 *tl = txdr_unsigned(bp->b_dirtyend - bp->b_dirtyoff);
1690 info->done = nfs_commitrpc_bio_done;
1691 nfsm_request_bio(info, vp, NFSPROC_COMMIT, NULL,
1692 nfs_vpcred(vp, ND_WRITE));
1696 * Chain to write RPC on (early) error
1698 kfree(info, M_NFSREQ);
1699 nfs_writerpc_bio(vp, bio);
1703 nfs_commitrpc_bio_done(nfsm_info_t info)
1705 struct nfsmount *nmp = VFSTONFS(info->vp->v_mount);
1706 struct bio *bio = info->bio;
1707 struct buf *bp = bio->bio_buf;
1709 int wccflag = NFSV3_WCCRATTR;
1712 lwkt_gettoken(&nmp->nm_token);
1714 ERROROUT(nfsm_wcc_data(info, info->vp, &wccflag));
1716 NULLOUT(tl = nfsm_dissect(info, NFSX_V3WRITEVERF));
1717 if (bcmp(nmp->nm_verf, tl, NFSX_V3WRITEVERF)) {
1718 bcopy(tl, nmp->nm_verf, NFSX_V3WRITEVERF);
1719 error = NFSERR_STALEWRITEVERF;
1722 m_freem(info->mrep);
1726 * On completion we must chain to a write bio if an
1731 bp->b_dirtyoff = bp->b_dirtyend = 0;
1732 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1736 nfs_writerpc_bio(info->vp, bio);
1738 kfree(info, M_NFSREQ);
1739 lwkt_reltoken(&nmp->nm_token);