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>
54 #include <vm/vm_extern.h>
55 #include <vm/vm_page.h>
56 #include <vm/vm_object.h>
57 #include <vm/vm_pager.h>
58 #include <vm/vnode_pager.h>
61 #include <sys/thread2.h>
62 #include <vm/vm_page2.h>
70 #include "nfsm_subs.h"
73 static struct buf *nfs_getcacheblk(struct vnode *vp, off_t loffset,
74 int size, struct thread *td);
75 static int nfs_check_dirent(struct nfs_dirent *dp, int maxlen);
76 static void nfsiodone_sync(struct bio *bio);
77 static void nfs_readrpc_bio_done(nfsm_info_t info);
78 static void nfs_writerpc_bio_done(nfsm_info_t info);
79 static void nfs_commitrpc_bio_done(nfsm_info_t info);
82 * Vnode op for read using bio
85 nfs_bioread(struct vnode *vp, struct uio *uio, int ioflag)
87 struct nfsnode *np = VTONFS(vp);
89 struct buf *bp, *rabp;
92 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
102 if (uio->uio_rw != UIO_READ)
103 panic("nfs_read mode");
105 if (uio->uio_resid == 0)
107 if (uio->uio_offset < 0) /* XXX VDIR cookies can be negative */
111 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
112 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
113 (void)nfs_fsinfo(nmp, vp, td);
114 if (vp->v_type != VDIR &&
115 (uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
117 biosize = vp->v_mount->mnt_stat.f_iosize;
118 seqcount = (int)((off_t)(ioflag >> IO_SEQSHIFT) * biosize / BKVASIZE);
121 * For nfs, cache consistency can only be maintained approximately.
122 * Although RFC1094 does not specify the criteria, the following is
123 * believed to be compatible with the reference port.
125 * NFS: If local changes have been made and this is a
126 * directory, the directory must be invalidated and
127 * the attribute cache must be cleared.
129 * GETATTR is called to synchronize the file size.
131 * If remote changes are detected local data is flushed
132 * and the cache is invalidated.
134 * NOTE: In the normal case the attribute cache is not
135 * cleared which means GETATTR may use cached data and
136 * not immediately detect changes made on the server.
138 if ((np->n_flag & NLMODIFIED) && vp->v_type == VDIR) {
140 error = nfs_vinvalbuf(vp, V_SAVE, 1);
145 error = VOP_GETATTR(vp, &vattr);
150 * This can deadlock getpages/putpages for regular
151 * files. Only do it for directories.
153 if (np->n_flag & NRMODIFIED) {
154 if (vp->v_type == VDIR) {
156 error = nfs_vinvalbuf(vp, V_SAVE, 1);
159 np->n_flag &= ~NRMODIFIED;
164 * Loop until uio exhausted or we hit EOF
169 switch (vp->v_type) {
171 nfsstats.biocache_reads++;
172 lbn = uio->uio_offset / biosize;
173 boff = uio->uio_offset & (biosize - 1);
174 loffset = (off_t)lbn * biosize;
177 * Start the read ahead(s), as required.
179 if (nmp->nm_readahead > 0 && nfs_asyncok(nmp)) {
180 for (nra = 0; nra < nmp->nm_readahead && nra < seqcount &&
181 (off_t)(lbn + 1 + nra) * biosize < np->n_size; nra++) {
182 rabn = lbn + 1 + nra;
183 raoffset = (off_t)rabn * biosize;
184 if (findblk(vp, raoffset, FINDBLK_TEST) == NULL) {
185 rabp = nfs_getcacheblk(vp, raoffset, biosize, td);
188 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
189 rabp->b_cmd = BUF_CMD_READ;
190 vfs_busy_pages(vp, rabp);
191 nfs_asyncio(vp, &rabp->b_bio2);
200 * Obtain the buffer cache block. Figure out the buffer size
201 * when we are at EOF. If we are modifying the size of the
202 * buffer based on an EOF condition we need to hold
203 * nfs_rslock() through obtaining the buffer to prevent
204 * a potential writer-appender from messing with n_size.
205 * Otherwise we may accidently truncate the buffer and
208 * Note that bcount is *not* DEV_BSIZE aligned.
210 if (loffset + boff >= np->n_size) {
214 bp = nfs_getcacheblk(vp, loffset, biosize, td);
220 * If B_CACHE is not set, we must issue the read. If this
221 * fails, we return an error.
223 if ((bp->b_flags & B_CACHE) == 0) {
224 bp->b_cmd = BUF_CMD_READ;
225 bp->b_bio2.bio_done = nfsiodone_sync;
226 bp->b_bio2.bio_flags |= BIO_SYNC;
227 vfs_busy_pages(vp, bp);
228 error = nfs_doio(vp, &bp->b_bio2, td);
236 * on is the offset into the current bp. Figure out how many
237 * bytes we can copy out of the bp. Note that bcount is
238 * NOT DEV_BSIZE aligned.
240 * Then figure out how many bytes we can copy into the uio.
243 if (n > uio->uio_resid)
245 if (loffset + boff + n > np->n_size)
246 n = np->n_size - loffset - boff;
249 biosize = min(NFS_MAXPATHLEN, np->n_size);
250 nfsstats.biocache_readlinks++;
251 bp = nfs_getcacheblk(vp, (off_t)0, biosize, td);
254 if ((bp->b_flags & B_CACHE) == 0) {
255 bp->b_cmd = BUF_CMD_READ;
256 bp->b_bio2.bio_done = nfsiodone_sync;
257 bp->b_bio2.bio_flags |= BIO_SYNC;
258 vfs_busy_pages(vp, bp);
259 error = nfs_doio(vp, &bp->b_bio2, td);
261 bp->b_flags |= B_ERROR | B_INVAL;
266 n = szmin(uio->uio_resid, (size_t)bp->b_bcount - bp->b_resid);
270 nfsstats.biocache_readdirs++;
271 if (np->n_direofoffset &&
272 uio->uio_offset >= np->n_direofoffset
276 lbn = (uoff_t)uio->uio_offset / NFS_DIRBLKSIZ;
277 boff = uio->uio_offset & (NFS_DIRBLKSIZ - 1);
278 loffset = uio->uio_offset - boff;
279 bp = nfs_getcacheblk(vp, loffset, NFS_DIRBLKSIZ, td);
283 if ((bp->b_flags & B_CACHE) == 0) {
284 bp->b_cmd = BUF_CMD_READ;
285 bp->b_bio2.bio_done = nfsiodone_sync;
286 bp->b_bio2.bio_flags |= BIO_SYNC;
287 vfs_busy_pages(vp, bp);
288 error = nfs_doio(vp, &bp->b_bio2, td);
291 while (error == NFSERR_BAD_COOKIE) {
292 kprintf("got bad cookie vp %p bp %p\n", vp, bp);
294 error = nfs_vinvalbuf(vp, 0, 1);
296 * Yuck! The directory has been modified on the
297 * server. The only way to get the block is by
298 * reading from the beginning to get all the
301 * Leave the last bp intact unless there is an error.
302 * Loop back up to the while if the error is another
303 * NFSERR_BAD_COOKIE (double yuch!).
305 for (i = 0; i <= lbn && !error; i++) {
306 if (np->n_direofoffset
307 && (i * NFS_DIRBLKSIZ) >= np->n_direofoffset)
309 bp = nfs_getcacheblk(vp, (off_t)i * NFS_DIRBLKSIZ,
313 if ((bp->b_flags & B_CACHE) == 0) {
314 bp->b_cmd = BUF_CMD_READ;
315 bp->b_bio2.bio_done = nfsiodone_sync;
316 bp->b_bio2.bio_flags |= BIO_SYNC;
317 vfs_busy_pages(vp, bp);
318 error = nfs_doio(vp, &bp->b_bio2, td);
320 * no error + B_INVAL == directory EOF,
323 if (error == 0 && (bp->b_flags & B_INVAL))
327 * An error will throw away the block and the
328 * for loop will break out. If no error and this
329 * is not the block we want, we throw away the
330 * block and go for the next one via the for loop.
332 if (error || i < lbn)
337 * The above while is repeated if we hit another cookie
338 * error. If we hit an error and it wasn't a cookie error,
346 * If not eof and read aheads are enabled, start one.
347 * (You need the current block first, so that you have the
348 * directory offset cookie of the next block.)
350 if (nmp->nm_readahead > 0 && nfs_asyncok(nmp) &&
351 (bp->b_flags & B_INVAL) == 0 &&
352 (np->n_direofoffset == 0 ||
353 loffset + NFS_DIRBLKSIZ < np->n_direofoffset) &&
354 findblk(vp, loffset + NFS_DIRBLKSIZ, FINDBLK_TEST) == NULL
356 rabp = nfs_getcacheblk(vp, loffset + NFS_DIRBLKSIZ,
359 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
360 rabp->b_cmd = BUF_CMD_READ;
361 vfs_busy_pages(vp, rabp);
362 nfs_asyncio(vp, &rabp->b_bio2);
369 * Unlike VREG files, whos buffer size ( bp->b_bcount ) is
370 * chopped for the EOF condition, we cannot tell how large
371 * NFS directories are going to be until we hit EOF. So
372 * an NFS directory buffer is *not* chopped to its EOF. Now,
373 * it just so happens that b_resid will effectively chop it
374 * to EOF. *BUT* this information is lost if the buffer goes
375 * away and is reconstituted into a B_CACHE state ( due to
376 * being VMIO ) later. So we keep track of the directory eof
377 * in np->n_direofoffset and chop it off as an extra step
380 * NOTE: boff could already be beyond EOF.
382 if ((size_t)boff > NFS_DIRBLKSIZ - bp->b_resid) {
385 n = szmin(uio->uio_resid,
386 NFS_DIRBLKSIZ - bp->b_resid - (size_t)boff);
388 if (np->n_direofoffset &&
389 n > (size_t)(np->n_direofoffset - uio->uio_offset)) {
390 n = (size_t)(np->n_direofoffset - uio->uio_offset);
394 kprintf(" nfs_bioread: type %x unexpected\n",vp->v_type);
399 switch (vp->v_type) {
402 error = uiomove(bp->b_data + boff, n, uio);
406 error = uiomove(bp->b_data + boff, n, uio);
411 off_t old_off = uio->uio_offset;
413 struct nfs_dirent *dp;
416 * We are casting cpos to nfs_dirent, it must be
424 cpos = bp->b_data + boff;
425 epos = bp->b_data + boff + n;
426 while (cpos < epos && error == 0 && uio->uio_resid > 0) {
427 dp = (struct nfs_dirent *)cpos;
428 error = nfs_check_dirent(dp, (int)(epos - cpos));
431 if (vop_write_dirent(&error, uio, dp->nfs_ino,
432 dp->nfs_type, dp->nfs_namlen, dp->nfs_name)) {
435 cpos += dp->nfs_reclen;
439 uio->uio_offset = old_off + cpos -
445 kprintf(" nfs_bioread: type %x unexpected\n",vp->v_type);
449 } while (error == 0 && uio->uio_resid > 0 && n > 0);
454 * Userland can supply any 'seek' offset when reading a NFS directory.
455 * Validate the structure so we don't panic the kernel. Note that
456 * the element name is nul terminated and the nul is not included
461 nfs_check_dirent(struct nfs_dirent *dp, int maxlen)
463 int nfs_name_off = offsetof(struct nfs_dirent, nfs_name[0]);
465 if (nfs_name_off >= maxlen)
467 if (dp->nfs_reclen < nfs_name_off || dp->nfs_reclen > maxlen)
469 if (nfs_name_off + dp->nfs_namlen >= dp->nfs_reclen)
471 if (dp->nfs_reclen & 3)
477 * Vnode op for write using bio
479 * nfs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
480 * struct ucred *a_cred)
483 nfs_write(struct vop_write_args *ap)
485 struct uio *uio = ap->a_uio;
486 struct thread *td = uio->uio_td;
487 struct vnode *vp = ap->a_vp;
488 struct nfsnode *np = VTONFS(vp);
489 int ioflag = ap->a_ioflag;
492 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
502 if (uio->uio_rw != UIO_WRITE)
503 panic("nfs_write mode");
504 if (uio->uio_segflg == UIO_USERSPACE && uio->uio_td != curthread)
505 panic("nfs_write proc");
507 if (vp->v_type != VREG)
509 if (np->n_flag & NWRITEERR) {
510 np->n_flag &= ~NWRITEERR;
511 return (np->n_error);
513 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
514 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
515 (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)
546 if ((uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
548 if (uio->uio_resid == 0)
552 * We need to obtain the rslock if we intend to modify np->n_size
553 * in order to guarentee the append point with multiple contending
554 * writers, to guarentee that no other appenders modify n_size
555 * while we are trying to obtain a truncated buffer (i.e. to avoid
556 * accidently truncating data written by another appender due to
557 * the race), and to ensure that the buffer is populated prior to
558 * our extending of the file. We hold rslock through the entire
561 * Note that we do not synchronize the case where someone truncates
562 * the file while we are appending to it because attempting to lock
563 * this case may deadlock other parts of the system unexpectedly.
565 if ((ioflag & IO_APPEND) ||
566 uio->uio_offset + uio->uio_resid > np->n_size) {
567 switch(nfs_rslock(np)) {
582 * Maybe this should be above the vnode op call, but so long as
583 * file servers have no limits, i don't think it matters
585 if (td && td->td_proc && uio->uio_offset + uio->uio_resid >
586 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
587 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
593 biosize = vp->v_mount->mnt_stat.f_iosize;
596 nfsstats.biocache_writes++;
597 boff = uio->uio_offset & (biosize-1);
598 loffset = uio->uio_offset - boff;
599 bytes = (int)szmin((unsigned)(biosize - boff), uio->uio_resid);
602 * Handle direct append and file extension cases, calculate
603 * unaligned buffer size. When extending B_CACHE will be
604 * set if possible. See UIO_NOCOPY note below.
606 if (uio->uio_offset + bytes > np->n_size) {
607 np->n_flag |= NLMODIFIED;
608 trivial = (uio->uio_segflg != UIO_NOCOPY &&
609 uio->uio_offset <= np->n_size);
610 nfs_meta_setsize(vp, td, uio->uio_offset + bytes,
613 bp = nfs_getcacheblk(vp, loffset, biosize, td);
620 * Actual bytes in buffer which we care about
622 if (loffset + biosize < np->n_size)
625 bcount = (int)(np->n_size - loffset);
628 * Avoid a read by setting B_CACHE where the data we
629 * intend to write covers the entire buffer. Note
630 * that the buffer may have been set to B_CACHE by
631 * nfs_meta_setsize() above or otherwise inherited the
632 * flag, but if B_CACHE isn't set the buffer may be
633 * uninitialized and must be zero'd to accomodate
634 * future seek+write's.
636 * See the comments in kern/vfs_bio.c's getblk() for
639 * When doing a UIO_NOCOPY write the buffer is not
640 * overwritten and we cannot just set B_CACHE unconditionally
641 * for full-block writes.
643 if (boff == 0 && bytes == biosize &&
644 uio->uio_segflg != UIO_NOCOPY) {
645 bp->b_flags |= B_CACHE;
646 bp->b_flags &= ~(B_ERROR | B_INVAL);
650 * b_resid may be set due to file EOF if we extended out.
651 * The NFS bio code will zero the difference anyway so
652 * just acknowledged the fact and set b_resid to 0.
654 if ((bp->b_flags & B_CACHE) == 0) {
655 bp->b_cmd = BUF_CMD_READ;
656 bp->b_bio2.bio_done = nfsiodone_sync;
657 bp->b_bio2.bio_flags |= BIO_SYNC;
658 vfs_busy_pages(vp, bp);
659 error = nfs_doio(vp, &bp->b_bio2, td);
666 np->n_flag |= NLMODIFIED;
669 * If dirtyend exceeds file size, chop it down. This should
670 * not normally occur but there is an append race where it
671 * might occur XXX, so we log it.
673 * If the chopping creates a reverse-indexed or degenerate
674 * situation with dirtyoff/end, we 0 both of them.
676 if (bp->b_dirtyend > bcount) {
677 kprintf("NFS append race @%08llx:%d\n",
678 (long long)bp->b_bio2.bio_offset,
679 bp->b_dirtyend - bcount);
680 bp->b_dirtyend = bcount;
683 if (bp->b_dirtyoff >= bp->b_dirtyend)
684 bp->b_dirtyoff = bp->b_dirtyend = 0;
687 * If the new write will leave a contiguous dirty
688 * area, just update the b_dirtyoff and b_dirtyend,
689 * otherwise force a write rpc of the old dirty area.
691 * While it is possible to merge discontiguous writes due to
692 * our having a B_CACHE buffer ( and thus valid read data
693 * for the hole), we don't because it could lead to
694 * significant cache coherency problems with multiple clients,
695 * especially if locking is implemented later on.
697 * as an optimization we could theoretically maintain
698 * a linked list of discontinuous areas, but we would still
699 * have to commit them separately so there isn't much
700 * advantage to it except perhaps a bit of asynchronization.
702 if (bp->b_dirtyend > 0 &&
703 (boff > bp->b_dirtyend ||
704 (boff + bytes) < bp->b_dirtyoff)
706 if (bwrite(bp) == EINTR) {
713 error = uiomove(bp->b_data + boff, bytes, uio);
716 * Since this block is being modified, it must be written
717 * again and not just committed. Since write clustering does
718 * not work for the stage 1 data write, only the stage 2
719 * commit rpc, we have to clear B_CLUSTEROK as well.
721 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
729 * Only update dirtyoff/dirtyend if not a degenerate
732 * The underlying VM pages have been marked valid by
733 * virtue of acquiring the bp. Because the entire buffer
734 * is marked dirty we do not have to worry about cleaning
735 * out the related dirty bits (and wouldn't really know
736 * how to deal with byte ranges anyway)
739 if (bp->b_dirtyend > 0) {
740 bp->b_dirtyoff = imin(boff, bp->b_dirtyoff);
741 bp->b_dirtyend = imax(boff + bytes,
744 bp->b_dirtyoff = boff;
745 bp->b_dirtyend = boff + bytes;
750 * If the lease is non-cachable or IO_SYNC do bwrite().
752 * IO_INVAL appears to be unused. The idea appears to be
753 * to turn off caching in this case. Very odd. XXX
755 * If nfs_async is set bawrite() will use an unstable write
756 * (build dirty bufs on the server), so we might as well
757 * push it out with bawrite(). If nfs_async is not set we
758 * use bdwrite() to cache dirty bufs on the client.
760 if (ioflag & IO_SYNC) {
761 if (ioflag & IO_INVAL)
762 bp->b_flags |= B_NOCACHE;
766 } else if (boff + bytes == biosize && nfs_async) {
771 } while (uio->uio_resid > 0 && bytes > 0);
780 * Get an nfs cache block.
782 * Allocate a new one if the block isn't currently in the cache
783 * and return the block marked busy. If the calling process is
784 * interrupted by a signal for an interruptible mount point, return
787 * The caller must carefully deal with the possible B_INVAL state of
788 * the buffer. nfs_startio() clears B_INVAL (and nfs_asyncio() clears it
789 * indirectly), so synchronous reads can be issued without worrying about
790 * the B_INVAL state. We have to be a little more careful when dealing
791 * with writes (see comments in nfs_write()) when extending a file past
795 nfs_getcacheblk(struct vnode *vp, off_t loffset, int size, struct thread *td)
799 struct nfsmount *nmp;
804 if (nmp->nm_flag & NFSMNT_INT) {
805 bp = getblk(vp, loffset, size, GETBLK_PCATCH, 0);
807 if (nfs_sigintr(nmp, NULL, td))
809 bp = getblk(vp, loffset, size, 0, 2 * hz);
812 bp = getblk(vp, loffset, size, 0, 0);
816 * bio2, the 'device' layer. Since BIOs use 64 bit byte offsets
817 * now, no translation is necessary.
819 bp->b_bio2.bio_offset = loffset;
824 * Flush and invalidate all dirty buffers. If another process is already
825 * doing the flush, just wait for completion.
828 nfs_vinvalbuf(struct vnode *vp, int flags, int intrflg)
830 struct nfsnode *np = VTONFS(vp);
831 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
832 int error = 0, slpflag, slptimeo;
833 thread_t td = curthread;
835 if (vp->v_flag & VRECLAIMED)
838 if ((nmp->nm_flag & NFSMNT_INT) == 0)
848 * First wait for any other process doing a flush to complete.
850 while (np->n_flag & NFLUSHINPROG) {
851 np->n_flag |= NFLUSHWANT;
852 error = tsleep((caddr_t)&np->n_flag, 0, "nfsvinval", slptimeo);
853 if (error && intrflg && nfs_sigintr(nmp, NULL, td))
858 * Now, flush as required.
860 np->n_flag |= NFLUSHINPROG;
861 error = vinvalbuf(vp, flags, slpflag, 0);
863 if (intrflg && nfs_sigintr(nmp, NULL, td)) {
864 np->n_flag &= ~NFLUSHINPROG;
865 if (np->n_flag & NFLUSHWANT) {
866 np->n_flag &= ~NFLUSHWANT;
867 wakeup((caddr_t)&np->n_flag);
871 error = vinvalbuf(vp, flags, 0, slptimeo);
873 np->n_flag &= ~(NLMODIFIED | NFLUSHINPROG);
874 if (np->n_flag & NFLUSHWANT) {
875 np->n_flag &= ~NFLUSHWANT;
876 wakeup((caddr_t)&np->n_flag);
882 * Return true (non-zero) if the txthread and rxthread are operational
883 * and we do not already have too many not-yet-started BIO's built up.
886 nfs_asyncok(struct nfsmount *nmp)
888 return (nmp->nm_bioqlen < nfs_maxasyncbio &&
889 nmp->nm_bioqlen < nmp->nm_maxasync_scaled / NFS_ASYSCALE &&
890 nmp->nm_rxstate <= NFSSVC_PENDING &&
891 nmp->nm_txstate <= NFSSVC_PENDING);
895 * The read-ahead code calls this to queue a bio to the txthread.
897 * We don't touch the bio otherwise... that is, we do not even
898 * construct or send the initial rpc. The txthread will do it
901 * NOTE! nm_bioqlen is not decremented until the request completes,
902 * so it does not reflect the number of bio's on bioq.
905 nfs_asyncio(struct vnode *vp, struct bio *bio)
907 struct buf *bp = bio->bio_buf;
908 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
910 KKASSERT(vp->v_tag == VT_NFS);
914 * Shortcut swap cache (not done automatically because we are not
917 if (vn_cache_strategy(vp, bio))
920 bio->bio_driver_info = vp;
922 TAILQ_INSERT_TAIL(&nmp->nm_bioq, bio, bio_act);
923 atomic_add_int(&nmp->nm_bioqlen, 1);
925 nfssvc_iod_writer_wakeup(nmp);
929 * nfs_dio() - Execute a BIO operation synchronously. The BIO will be
930 * completed and its error returned. The caller is responsible
931 * for brelse()ing it. ONLY USE FOR BIO_SYNC IOs! Otherwise
932 * our error probe will be against an invalid pointer.
934 * nfs_startio()- Execute a BIO operation assynchronously.
936 * NOTE: nfs_asyncio() is used to initiate an asynchronous BIO operation,
937 * which basically just queues it to the txthread. nfs_startio()
938 * actually initiates the I/O AFTER it has gotten to the txthread.
940 * NOTE: td might be NULL.
942 * NOTE: Caller has already busied the I/O.
945 nfs_startio(struct vnode *vp, struct bio *bio, struct thread *td)
947 struct buf *bp = bio->bio_buf;
949 struct nfsmount *nmp;
951 KKASSERT(vp->v_tag == VT_NFS);
953 nmp = VFSTONFS(vp->v_mount);
956 * clear B_ERROR and B_INVAL state prior to initiating the I/O. We
957 * do this here so we do not have to do it in all the code that
960 bp->b_flags &= ~(B_ERROR | B_INVAL);
962 KASSERT(bp->b_cmd != BUF_CMD_DONE,
963 ("nfs_doio: bp %p already marked done!", bp));
965 if (bp->b_cmd == BUF_CMD_READ) {
966 switch (vp->v_type) {
968 nfsstats.read_bios++;
969 nfs_readrpc_bio(vp, bio);
974 nfsstats.readlink_bios++;
975 nfs_readlinkrpc_bio(vp, bio);
977 nfs_doio(vp, bio, td);
982 * NOTE: If nfs_readdirplusrpc_bio() is requested but
983 * not supported, it will chain to
984 * nfs_readdirrpc_bio().
987 nfsstats.readdir_bios++;
988 uiop->uio_offset = bio->bio_offset;
989 if (nmp->nm_flag & NFSMNT_RDIRPLUS)
990 nfs_readdirplusrpc_bio(vp, bio);
992 nfs_readdirrpc_bio(vp, bio);
994 nfs_doio(vp, bio, td);
998 kprintf("nfs_doio: type %x unexpected\n",vp->v_type);
999 bp->b_flags |= B_ERROR;
1000 bp->b_error = EINVAL;
1006 * If we only need to commit, try to commit. If this fails
1007 * it will chain through to the write. Basically all the logic
1008 * in nfs_doio() is replicated.
1010 KKASSERT(bp->b_cmd == BUF_CMD_WRITE);
1011 if (bp->b_flags & B_NEEDCOMMIT)
1012 nfs_commitrpc_bio(vp, bio);
1014 nfs_writerpc_bio(vp, bio);
1019 nfs_doio(struct vnode *vp, struct bio *bio, struct thread *td)
1021 struct buf *bp = bio->bio_buf;
1024 struct nfsmount *nmp;
1026 int iomode, must_commit;
1033 * Shortcut swap cache (not done automatically because we are not
1036 * XXX The biowait is a hack until we can figure out how to stop a
1037 * biodone chain when a middle element is BIO_SYNC. BIO_SYNC is
1038 * set so the bp shouldn't get ripped out from under us. The only
1039 * use-cases are fully synchronous I/O cases.
1041 * XXX This is having problems, give up for now.
1043 if (vn_cache_strategy(vp, bio)) {
1045 error = biowait(&bio->bio_buf->b_bio1, "nfsrsw");
1050 KKASSERT(vp->v_tag == VT_NFS);
1052 nmp = VFSTONFS(vp->v_mount);
1054 uiop->uio_iov = &io;
1055 uiop->uio_iovcnt = 1;
1056 uiop->uio_segflg = UIO_SYSSPACE;
1060 * clear B_ERROR and B_INVAL state prior to initiating the I/O. We
1061 * do this here so we do not have to do it in all the code that
1064 bp->b_flags &= ~(B_ERROR | B_INVAL);
1066 KASSERT(bp->b_cmd != BUF_CMD_DONE,
1067 ("nfs_doio: bp %p already marked done!", bp));
1069 if (bp->b_cmd == BUF_CMD_READ) {
1070 io.iov_len = uiop->uio_resid = (size_t)bp->b_bcount;
1071 io.iov_base = bp->b_data;
1072 uiop->uio_rw = UIO_READ;
1074 switch (vp->v_type) {
1077 * When reading from a regular file zero-fill any residual.
1078 * Note that this residual has nothing to do with NFS short
1079 * reads, which nfs_readrpc_uio() will handle for us.
1081 * We have to do this because when we are write extending
1082 * a file the server may not have the same notion of
1083 * filesize as we do. Our BIOs should already be sized
1084 * (b_bcount) to account for the file EOF.
1086 nfsstats.read_bios++;
1087 uiop->uio_offset = bio->bio_offset;
1088 error = nfs_readrpc_uio(vp, uiop);
1089 if (error == 0 && uiop->uio_resid) {
1090 n = (size_t)bp->b_bcount - uiop->uio_resid;
1091 bzero(bp->b_data + n, bp->b_bcount - n);
1092 uiop->uio_resid = 0;
1094 if (td && td->td_proc && (vp->v_flag & VTEXT) &&
1095 np->n_mtime != np->n_vattr.va_mtime.tv_sec) {
1096 uprintf("Process killed due to text file modification\n");
1097 ksignal(td->td_proc, SIGKILL);
1101 uiop->uio_offset = 0;
1102 nfsstats.readlink_bios++;
1103 error = nfs_readlinkrpc_uio(vp, uiop);
1106 nfsstats.readdir_bios++;
1107 uiop->uio_offset = bio->bio_offset;
1108 if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
1109 error = nfs_readdirplusrpc_uio(vp, uiop);
1110 if (error == NFSERR_NOTSUPP)
1111 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
1113 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
1114 error = nfs_readdirrpc_uio(vp, uiop);
1116 * end-of-directory sets B_INVAL but does not generate an
1119 if (error == 0 && uiop->uio_resid == bp->b_bcount)
1120 bp->b_flags |= B_INVAL;
1123 kprintf("nfs_doio: type %x unexpected\n",vp->v_type);
1127 bp->b_flags |= B_ERROR;
1128 bp->b_error = error;
1130 bp->b_resid = uiop->uio_resid;
1133 * If we only need to commit, try to commit.
1135 * NOTE: The I/O has already been staged for the write and
1136 * its pages busied, so b_dirtyoff/end is valid.
1138 KKASSERT(bp->b_cmd == BUF_CMD_WRITE);
1139 if (bp->b_flags & B_NEEDCOMMIT) {
1143 off = bio->bio_offset + bp->b_dirtyoff;
1144 retv = nfs_commitrpc_uio(vp, off,
1145 bp->b_dirtyend - bp->b_dirtyoff,
1148 bp->b_dirtyoff = bp->b_dirtyend = 0;
1149 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1154 if (retv == NFSERR_STALEWRITEVERF) {
1155 nfs_clearcommit(vp->v_mount);
1160 * Setup for actual write
1162 if (bio->bio_offset + bp->b_dirtyend > np->n_size)
1163 bp->b_dirtyend = np->n_size - bio->bio_offset;
1165 if (bp->b_dirtyend > bp->b_dirtyoff) {
1166 io.iov_len = uiop->uio_resid = bp->b_dirtyend
1168 uiop->uio_offset = bio->bio_offset + bp->b_dirtyoff;
1169 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
1170 uiop->uio_rw = UIO_WRITE;
1171 nfsstats.write_bios++;
1173 if ((bp->b_flags & (B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == 0)
1174 iomode = NFSV3WRITE_UNSTABLE;
1176 iomode = NFSV3WRITE_FILESYNC;
1179 error = nfs_writerpc_uio(vp, uiop, &iomode, &must_commit);
1182 * We no longer try to use kern/vfs_bio's cluster code to
1183 * cluster commits, so B_CLUSTEROK is no longer set with
1184 * B_NEEDCOMMIT. The problem is that a vfs_busy_pages()
1185 * may have to clear B_NEEDCOMMIT if it finds underlying
1186 * pages have been redirtied through a memory mapping
1187 * and doing this on a clustered bp will probably cause
1188 * a panic, plus the flag in the underlying NFS bufs
1189 * making up the cluster bp will not be properly cleared.
1191 if (!error && iomode == NFSV3WRITE_UNSTABLE) {
1192 bp->b_flags |= B_NEEDCOMMIT;
1194 /* XXX do not enable commit clustering */
1195 if (bp->b_dirtyoff == 0
1196 && bp->b_dirtyend == bp->b_bcount)
1197 bp->b_flags |= B_CLUSTEROK;
1200 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1204 * For an interrupted write, the buffer is still valid
1205 * and the write hasn't been pushed to the server yet,
1206 * so we can't set B_ERROR and report the interruption
1207 * by setting B_EINTR. For the async case, B_EINTR
1208 * is not relevant, so the rpc attempt is essentially
1209 * a noop. For the case of a V3 write rpc not being
1210 * committed to stable storage, the block is still
1211 * dirty and requires either a commit rpc or another
1212 * write rpc with iomode == NFSV3WRITE_FILESYNC before
1213 * the block is reused. This is indicated by setting
1214 * the B_DELWRI and B_NEEDCOMMIT flags.
1216 * If the buffer is marked B_PAGING, it does not reside on
1217 * the vp's paging queues so we cannot call bdirty(). The
1218 * bp in this case is not an NFS cache block so we should
1222 || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
1224 bp->b_flags &= ~(B_INVAL|B_NOCACHE);
1225 if ((bp->b_flags & B_PAGING) == 0)
1228 bp->b_flags |= B_EINTR;
1232 bp->b_flags |= B_ERROR;
1233 bp->b_error = np->n_error = error;
1234 np->n_flag |= NWRITEERR;
1236 bp->b_dirtyoff = bp->b_dirtyend = 0;
1239 nfs_clearcommit(vp->v_mount);
1240 bp->b_resid = uiop->uio_resid;
1247 * I/O was run synchronously, biodone() it and calculate the
1251 KKASSERT(bp->b_cmd == BUF_CMD_DONE);
1252 if (bp->b_flags & B_EINTR)
1254 if (bp->b_flags & B_ERROR)
1255 return (bp->b_error ? bp->b_error : EIO);
1260 * Handle all truncation, write-extend, and ftruncate()-extend operations
1261 * on the NFS lcient side.
1263 * We use the new API in kern/vfs_vm.c to perform these operations in a
1264 * VM-friendly way. With this API VM pages are properly zerod and pages
1265 * still mapped into the buffer straddling EOF are not invalidated.
1268 nfs_meta_setsize(struct vnode *vp, struct thread *td, off_t nsize, int trivial)
1270 struct nfsnode *np = VTONFS(vp);
1272 int biosize = vp->v_mount->mnt_stat.f_iosize;
1278 if (nsize < osize) {
1279 error = nvtruncbuf(vp, nsize, biosize, -1);
1281 error = nvextendbuf(vp, osize, nsize,
1282 biosize, biosize, -1, -1,
1289 * Synchronous completion for nfs_doio. Call bpdone() with elseit=FALSE.
1290 * Caller is responsible for brelse()'ing the bp.
1293 nfsiodone_sync(struct bio *bio)
1296 bpdone(bio->bio_buf, 0);
1300 * nfs read rpc - BIO version
1303 nfs_readrpc_bio(struct vnode *vp, struct bio *bio)
1305 struct buf *bp = bio->bio_buf;
1307 struct nfsmount *nmp;
1308 int error = 0, len, tsiz;
1309 struct nfsm_info *info;
1311 info = kmalloc(sizeof(*info), M_NFSREQ, M_WAITOK);
1313 info->v3 = NFS_ISV3(vp);
1315 nmp = VFSTONFS(vp->v_mount);
1316 tsiz = bp->b_bcount;
1317 KKASSERT(tsiz <= nmp->nm_rsize);
1318 if (bio->bio_offset + tsiz > nmp->nm_maxfilesize) {
1322 nfsstats.rpccnt[NFSPROC_READ]++;
1324 nfsm_reqhead(info, vp, NFSPROC_READ,
1325 NFSX_FH(info->v3) + NFSX_UNSIGNED * 3);
1326 ERROROUT(nfsm_fhtom(info, vp));
1327 tl = nfsm_build(info, NFSX_UNSIGNED * 3);
1329 txdr_hyper(bio->bio_offset, tl);
1330 *(tl + 2) = txdr_unsigned(len);
1332 *tl++ = txdr_unsigned(bio->bio_offset);
1333 *tl++ = txdr_unsigned(len);
1337 info->done = nfs_readrpc_bio_done;
1338 nfsm_request_bio(info, vp, NFSPROC_READ, NULL,
1339 nfs_vpcred(vp, ND_READ));
1342 kfree(info, M_NFSREQ);
1343 bp->b_error = error;
1344 bp->b_flags |= B_ERROR;
1349 nfs_readrpc_bio_done(nfsm_info_t info)
1351 struct nfsmount *nmp = VFSTONFS(info->vp->v_mount);
1352 struct bio *bio = info->bio;
1353 struct buf *bp = bio->bio_buf;
1360 KKASSERT(info->state == NFSM_STATE_DONE);
1363 ERROROUT(nfsm_postop_attr(info, info->vp, &attrflag,
1364 NFS_LATTR_NOSHRINK));
1365 NULLOUT(tl = nfsm_dissect(info, 2 * NFSX_UNSIGNED));
1366 eof = fxdr_unsigned(int, *(tl + 1));
1368 ERROROUT(nfsm_loadattr(info, info->vp, NULL));
1371 NEGATIVEOUT(retlen = nfsm_strsiz(info, nmp->nm_rsize));
1372 ERROROUT(nfsm_mtobio(info, bio, retlen));
1373 m_freem(info->mrep);
1377 * No error occured, if retlen is less then bcount and no EOF
1378 * and NFSv3 a zero-fill short read occured.
1380 * For NFSv2 a short-read indicates EOF.
1382 if (retlen < bp->b_bcount && info->v3 && eof == 0) {
1383 bzero(bp->b_data + retlen, bp->b_bcount - retlen);
1384 retlen = bp->b_bcount;
1388 * If we hit an EOF we still zero-fill, but return the expected
1389 * b_resid anyway. This should normally not occur since async
1390 * BIOs are not used for read-before-write case. Races against
1391 * the server can cause it though and we don't want to leave
1392 * garbage in the buffer.
1394 if (retlen < bp->b_bcount) {
1395 bzero(bp->b_data + retlen, bp->b_bcount - retlen);
1398 /* bp->b_resid = bp->b_bcount - retlen; */
1400 kfree(info, M_NFSREQ);
1402 bp->b_error = error;
1403 bp->b_flags |= B_ERROR;
1409 * nfs write call - BIO version
1411 * NOTE: Caller has already busied the I/O.
1414 nfs_writerpc_bio(struct vnode *vp, struct bio *bio)
1416 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1417 struct nfsnode *np = VTONFS(vp);
1418 struct buf *bp = bio->bio_buf;
1423 struct nfsm_info *info;
1427 * Setup for actual write. Just clean up the bio if there
1428 * is nothing to do. b_dirtyoff/end have already been staged
1429 * by the bp's pages getting busied.
1431 if (bio->bio_offset + bp->b_dirtyend > np->n_size)
1432 bp->b_dirtyend = np->n_size - bio->bio_offset;
1434 if (bp->b_dirtyend <= bp->b_dirtyoff) {
1439 len = bp->b_dirtyend - bp->b_dirtyoff;
1440 offset = bio->bio_offset + bp->b_dirtyoff;
1441 if (offset + len > nmp->nm_maxfilesize) {
1442 bp->b_flags |= B_ERROR;
1443 bp->b_error = EFBIG;
1448 nfsstats.write_bios++;
1450 info = kmalloc(sizeof(*info), M_NFSREQ, M_WAITOK);
1452 info->v3 = NFS_ISV3(vp);
1453 info->info_writerpc.must_commit = 0;
1454 if ((bp->b_flags & (B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == 0)
1455 iomode = NFSV3WRITE_UNSTABLE;
1457 iomode = NFSV3WRITE_FILESYNC;
1459 KKASSERT(len <= nmp->nm_wsize);
1461 nfsstats.rpccnt[NFSPROC_WRITE]++;
1462 nfsm_reqhead(info, vp, NFSPROC_WRITE,
1463 NFSX_FH(info->v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1464 ERROROUT(nfsm_fhtom(info, vp));
1466 tl = nfsm_build(info, 5 * NFSX_UNSIGNED);
1467 txdr_hyper(offset, tl);
1469 *tl++ = txdr_unsigned(len);
1470 *tl++ = txdr_unsigned(iomode);
1471 *tl = txdr_unsigned(len);
1475 tl = nfsm_build(info, 4 * NFSX_UNSIGNED);
1476 /* Set both "begin" and "current" to non-garbage. */
1477 x = txdr_unsigned((u_int32_t)offset);
1478 *tl++ = x; /* "begin offset" */
1479 *tl++ = x; /* "current offset" */
1480 x = txdr_unsigned(len);
1481 *tl++ = x; /* total to this offset */
1482 *tl = x; /* size of this write */
1484 ERROROUT(nfsm_biotom(info, bio, bp->b_dirtyoff, len));
1486 info->done = nfs_writerpc_bio_done;
1487 nfsm_request_bio(info, vp, NFSPROC_WRITE, NULL,
1488 nfs_vpcred(vp, ND_WRITE));
1491 kfree(info, M_NFSREQ);
1492 bp->b_error = error;
1493 bp->b_flags |= B_ERROR;
1498 nfs_writerpc_bio_done(nfsm_info_t info)
1500 struct nfsmount *nmp = VFSTONFS(info->vp->v_mount);
1501 struct nfsnode *np = VTONFS(info->vp);
1502 struct bio *bio = info->bio;
1503 struct buf *bp = bio->bio_buf;
1504 int wccflag = NFSV3_WCCRATTR;
1505 int iomode = NFSV3WRITE_FILESYNC;
1509 int len = bp->b_resid; /* b_resid was set to shortened length */
1514 * The write RPC returns a before and after mtime. The
1515 * nfsm_wcc_data() macro checks the before n_mtime
1516 * against the before time and stores the after time
1517 * in the nfsnode's cached vattr and n_mtime field.
1518 * The NRMODIFIED bit will be set if the before
1519 * time did not match the original mtime.
1521 wccflag = NFSV3_WCCCHK;
1522 ERROROUT(nfsm_wcc_data(info, info->vp, &wccflag));
1524 NULLOUT(tl = nfsm_dissect(info, 2 * NFSX_UNSIGNED + NFSX_V3WRITEVERF));
1525 rlen = fxdr_unsigned(int, *tl++);
1528 m_freem(info->mrep);
1531 } else if (rlen < len) {
1534 * XXX what do we do here?
1536 backup = len - rlen;
1537 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base - backup;
1538 uiop->uio_iov->iov_len += backup;
1539 uiop->uio_offset -= backup;
1540 uiop->uio_resid += backup;
1544 commit = fxdr_unsigned(int, *tl++);
1547 * Return the lowest committment level
1548 * obtained by any of the RPCs.
1550 if (iomode == NFSV3WRITE_FILESYNC)
1552 else if (iomode == NFSV3WRITE_DATASYNC &&
1553 commit == NFSV3WRITE_UNSTABLE)
1555 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1556 bcopy(tl, (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF);
1557 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1558 } else if (bcmp(tl, nmp->nm_verf, NFSX_V3WRITEVERF)) {
1559 info->info_writerpc.must_commit = 1;
1560 bcopy(tl, (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF);
1564 ERROROUT(nfsm_loadattr(info, info->vp, NULL));
1566 m_freem(info->mrep);
1570 if (info->vp->v_mount->mnt_flag & MNT_ASYNC)
1571 iomode = NFSV3WRITE_FILESYNC;
1575 * End of RPC. Now clean up the bp.
1577 * We no longer enable write clustering for commit operations,
1578 * See around line 1157 for a more detailed comment.
1580 if (!error && iomode == NFSV3WRITE_UNSTABLE) {
1581 bp->b_flags |= B_NEEDCOMMIT;
1583 /* XXX do not enable commit clustering */
1584 if (bp->b_dirtyoff == 0 && bp->b_dirtyend == bp->b_bcount)
1585 bp->b_flags |= B_CLUSTEROK;
1588 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1592 * For an interrupted write, the buffer is still valid
1593 * and the write hasn't been pushed to the server yet,
1594 * so we can't set B_ERROR and report the interruption
1595 * by setting B_EINTR. For the async case, B_EINTR
1596 * is not relevant, so the rpc attempt is essentially
1597 * a noop. For the case of a V3 write rpc not being
1598 * committed to stable storage, the block is still
1599 * dirty and requires either a commit rpc or another
1600 * write rpc with iomode == NFSV3WRITE_FILESYNC before
1601 * the block is reused. This is indicated by setting
1602 * the B_DELWRI and B_NEEDCOMMIT flags.
1604 * If the buffer is marked B_PAGING, it does not reside on
1605 * the vp's paging queues so we cannot call bdirty(). The
1606 * bp in this case is not an NFS cache block so we should
1609 if (error == EINTR || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
1611 bp->b_flags &= ~(B_INVAL|B_NOCACHE);
1612 if ((bp->b_flags & B_PAGING) == 0)
1615 bp->b_flags |= B_EINTR;
1619 bp->b_flags |= B_ERROR;
1620 bp->b_error = np->n_error = error;
1621 np->n_flag |= NWRITEERR;
1623 bp->b_dirtyoff = bp->b_dirtyend = 0;
1625 if (info->info_writerpc.must_commit)
1626 nfs_clearcommit(info->vp->v_mount);
1627 kfree(info, M_NFSREQ);
1629 bp->b_flags |= B_ERROR;
1630 bp->b_error = error;
1636 * Nfs Version 3 commit rpc - BIO version
1638 * This function issues the commit rpc and will chain to a write
1642 nfs_commitrpc_bio(struct vnode *vp, struct bio *bio)
1644 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1645 struct buf *bp = bio->bio_buf;
1646 struct nfsm_info *info;
1650 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) {
1651 bp->b_dirtyoff = bp->b_dirtyend = 0;
1652 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1658 info = kmalloc(sizeof(*info), M_NFSREQ, M_WAITOK);
1662 nfsstats.rpccnt[NFSPROC_COMMIT]++;
1663 nfsm_reqhead(info, vp, NFSPROC_COMMIT, NFSX_FH(1));
1664 ERROROUT(nfsm_fhtom(info, vp));
1665 tl = nfsm_build(info, 3 * NFSX_UNSIGNED);
1666 txdr_hyper(bio->bio_offset + bp->b_dirtyoff, tl);
1668 *tl = txdr_unsigned(bp->b_dirtyend - bp->b_dirtyoff);
1670 info->done = nfs_commitrpc_bio_done;
1671 nfsm_request_bio(info, vp, NFSPROC_COMMIT, NULL,
1672 nfs_vpcred(vp, ND_WRITE));
1676 * Chain to write RPC on (early) error
1678 kfree(info, M_NFSREQ);
1679 nfs_writerpc_bio(vp, bio);
1683 nfs_commitrpc_bio_done(nfsm_info_t info)
1685 struct nfsmount *nmp = VFSTONFS(info->vp->v_mount);
1686 struct bio *bio = info->bio;
1687 struct buf *bp = bio->bio_buf;
1689 int wccflag = NFSV3_WCCRATTR;
1692 ERROROUT(nfsm_wcc_data(info, info->vp, &wccflag));
1694 NULLOUT(tl = nfsm_dissect(info, NFSX_V3WRITEVERF));
1695 if (bcmp(nmp->nm_verf, tl, NFSX_V3WRITEVERF)) {
1696 bcopy(tl, nmp->nm_verf, NFSX_V3WRITEVERF);
1697 error = NFSERR_STALEWRITEVERF;
1700 m_freem(info->mrep);
1704 * On completion we must chain to a write bio if an
1708 kfree(info, M_NFSREQ);
1710 bp->b_dirtyoff = bp->b_dirtyend = 0;
1711 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1715 nfs_writerpc_bio(info->vp, bio);