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 VM getpages.
85 * nfs_getpages(struct vnode *a_vp, vm_page_t *a_m, int a_count,
86 * int a_reqpage, vm_ooffset_t a_offset)
89 nfs_getpages(struct vop_getpages_args *ap)
91 struct thread *td = curthread; /* XXX */
92 int i, error, nextoff, size, toff, count, npages;
103 nmp = VFSTONFS(vp->v_mount);
107 if (vp->v_object == NULL) {
108 kprintf("nfs_getpages: called with non-merged cache vnode??\n");
109 return VM_PAGER_ERROR;
112 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
113 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
114 (void)nfs_fsinfo(nmp, vp, td);
116 npages = btoc(count);
119 * NOTE that partially valid pages may occur in cases other
120 * then file EOF, such as when a file is partially written and
121 * ftruncate()-extended to a larger size. It is also possible
122 * for the valid bits to be set on garbage beyond the file EOF and
123 * clear in the area before EOF (e.g. m->valid == 0xfc), which can
124 * occur due to vtruncbuf() and the buffer cache's handling of
125 * pages which 'straddle' buffers or when b_bufsize is not a
126 * multiple of PAGE_SIZE.... the buffer cache cannot normally
127 * clear the extra bits. This kind of situation occurs when you
128 * make a small write() (m->valid == 0x03) and then mmap() and
129 * fault in the buffer(m->valid = 0xFF). When NFS flushes the
130 * buffer (vinvalbuf() m->valid = 0xFC) we are left with a mess.
132 * This is combined with the possibility that the pages are partially
133 * dirty or that there is a buffer backing the pages that is dirty
134 * (even if m->dirty is 0).
136 * To solve this problem several hacks have been made: (1) NFS
137 * guarentees that the IO block size is a multiple of PAGE_SIZE and
138 * (2) The buffer cache, when invalidating an NFS buffer, will
139 * disregard the buffer's fragmentory b_bufsize and invalidate
140 * the whole page rather then just the piece the buffer owns.
142 * This allows us to assume that a partially valid page found here
143 * is fully valid (vm_fault will zero'd out areas of the page not
146 m = pages[ap->a_reqpage];
148 for (i = 0; i < npages; ++i) {
149 if (i != ap->a_reqpage)
150 vnode_pager_freepage(pages[i]);
156 * Use an MSF_BUF as a medium to retrieve data from the pages.
158 msf_map_pagelist(&msf, pages, npages, 0);
160 kva = msf_buf_kva(msf);
166 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
167 uio.uio_resid = count;
168 uio.uio_segflg = UIO_SYSSPACE;
169 uio.uio_rw = UIO_READ;
172 error = nfs_readrpc_uio(vp, &uio);
175 if (error && ((int)uio.uio_resid == count)) {
176 kprintf("nfs_getpages: error %d\n", error);
177 for (i = 0; i < npages; ++i) {
178 if (i != ap->a_reqpage)
179 vnode_pager_freepage(pages[i]);
181 return VM_PAGER_ERROR;
185 * Calculate the number of bytes read and validate only that number
186 * of bytes. Note that due to pending writes, size may be 0. This
187 * does not mean that the remaining data is invalid!
190 size = count - (int)uio.uio_resid;
192 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
193 nextoff = toff + PAGE_SIZE;
196 m->flags &= ~PG_ZERO;
198 if (nextoff <= size) {
200 * Read operation filled an entire page
202 m->valid = VM_PAGE_BITS_ALL;
204 } else if (size > toff) {
206 * Read operation filled a partial page.
209 vm_page_set_valid(m, 0, size - toff);
210 vm_page_clear_dirty_end_nonincl(m, 0, size - toff);
211 /* handled by vm_fault now */
212 /* vm_page_zero_invalid(m, TRUE); */
215 * Read operation was short. If no error occured
216 * we may have hit a zero-fill section. We simply
217 * leave valid set to 0.
221 if (i != ap->a_reqpage) {
223 * Whether or not to leave the page activated is up in
224 * the air, but we should put the page on a page queue
225 * somewhere (it already is in the object). Result:
226 * It appears that emperical results show that
227 * deactivating pages is best.
231 * Just in case someone was asking for this page we
232 * now tell them that it is ok to use.
235 if (m->flags & PG_WANTED)
238 vm_page_deactivate(m);
241 vnode_pager_freepage(m);
249 * Vnode op for VM putpages.
251 * nfs_putpages(struct vnode *a_vp, vm_page_t *a_m, int a_count, int a_sync,
252 * int *a_rtvals, vm_ooffset_t a_offset)
255 nfs_putpages(struct vop_putpages_args *ap)
257 struct thread *td = curthread;
261 int iomode, must_commit, i, error, npages, count;
265 struct nfsmount *nmp;
272 nmp = VFSTONFS(vp->v_mount);
275 rtvals = ap->a_rtvals;
276 npages = btoc(count);
277 offset = IDX_TO_OFF(pages[0]->pindex);
279 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
280 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
281 (void)nfs_fsinfo(nmp, vp, td);
283 for (i = 0; i < npages; i++) {
284 rtvals[i] = VM_PAGER_AGAIN;
288 * When putting pages, do not extend file past EOF.
291 if (offset + count > np->n_size) {
292 count = np->n_size - offset;
298 * Use an MSF_BUF as a medium to retrieve data from the pages.
300 msf_map_pagelist(&msf, pages, npages, 0);
302 kva = msf_buf_kva(msf);
308 uio.uio_offset = offset;
309 uio.uio_resid = (size_t)count;
310 uio.uio_segflg = UIO_SYSSPACE;
311 uio.uio_rw = UIO_WRITE;
314 if ((ap->a_sync & VM_PAGER_PUT_SYNC) == 0)
315 iomode = NFSV3WRITE_UNSTABLE;
317 iomode = NFSV3WRITE_FILESYNC;
319 error = nfs_writerpc_uio(vp, &uio, &iomode, &must_commit);
324 int nwritten = round_page(count - (int)uio.uio_resid) / PAGE_SIZE;
325 for (i = 0; i < nwritten; i++) {
326 rtvals[i] = VM_PAGER_OK;
327 vm_page_undirty(pages[i]);
330 nfs_clearcommit(vp->v_mount);
336 * Vnode op for read using bio
339 nfs_bioread(struct vnode *vp, struct uio *uio, int ioflag)
341 struct nfsnode *np = VTONFS(vp);
343 struct buf *bp = 0, *rabp;
346 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
352 int nra, error = 0, n = 0, on = 0;
355 if (uio->uio_rw != UIO_READ)
356 panic("nfs_read mode");
358 if (uio->uio_resid == 0)
360 if (uio->uio_offset < 0) /* XXX VDIR cookies can be negative */
364 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
365 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
366 (void)nfs_fsinfo(nmp, vp, td);
367 if (vp->v_type != VDIR &&
368 (uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
370 biosize = vp->v_mount->mnt_stat.f_iosize;
371 seqcount = (int)((off_t)(ioflag >> IO_SEQSHIFT) * biosize / BKVASIZE);
374 * For nfs, cache consistency can only be maintained approximately.
375 * Although RFC1094 does not specify the criteria, the following is
376 * believed to be compatible with the reference port.
378 * NFS: If local changes have been made and this is a
379 * directory, the directory must be invalidated and
380 * the attribute cache must be cleared.
382 * GETATTR is called to synchronize the file size.
384 * If remote changes are detected local data is flushed
385 * and the cache is invalidated.
387 * NOTE: In the normal case the attribute cache is not
388 * cleared which means GETATTR may use cached data and
389 * not immediately detect changes made on the server.
391 if ((np->n_flag & NLMODIFIED) && vp->v_type == VDIR) {
393 error = nfs_vinvalbuf(vp, V_SAVE, 1);
398 error = VOP_GETATTR(vp, &vattr);
401 if (np->n_flag & NRMODIFIED) {
402 if (vp->v_type == VDIR)
404 error = nfs_vinvalbuf(vp, V_SAVE, 1);
407 np->n_flag &= ~NRMODIFIED;
410 if (np->n_flag & NDONTCACHE) {
411 switch (vp->v_type) {
413 return (nfs_readrpc_uio(vp, uio));
415 return (nfs_readlinkrpc_uio(vp, uio));
419 kprintf(" NDONTCACHE: type %x unexpected\n", vp->v_type);
423 switch (vp->v_type) {
425 nfsstats.biocache_reads++;
426 lbn = uio->uio_offset / biosize;
427 on = uio->uio_offset & (biosize - 1);
428 loffset = (off_t)lbn * biosize;
431 * Start the read ahead(s), as required.
433 if (nmp->nm_readahead > 0 && nfs_asyncok(nmp)) {
434 for (nra = 0; nra < nmp->nm_readahead && nra < seqcount &&
435 (off_t)(lbn + 1 + nra) * biosize < np->n_size; nra++) {
436 rabn = lbn + 1 + nra;
437 raoffset = (off_t)rabn * biosize;
438 if (findblk(vp, raoffset, FINDBLK_TEST) == NULL) {
439 rabp = nfs_getcacheblk(vp, raoffset, biosize, td);
442 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
443 rabp->b_cmd = BUF_CMD_READ;
444 vfs_busy_pages(vp, rabp);
445 nfs_asyncio(vp, &rabp->b_bio2);
454 * Obtain the buffer cache block. Figure out the buffer size
455 * when we are at EOF. If we are modifying the size of the
456 * buffer based on an EOF condition we need to hold
457 * nfs_rslock() through obtaining the buffer to prevent
458 * a potential writer-appender from messing with n_size.
459 * Otherwise we may accidently truncate the buffer and
462 * Note that bcount is *not* DEV_BSIZE aligned.
467 if (loffset >= np->n_size) {
469 } else if (loffset + biosize > np->n_size) {
470 bcount = np->n_size - loffset;
472 if (bcount != biosize) {
473 switch(nfs_rslock(np)) {
486 bp = nfs_getcacheblk(vp, loffset, bcount, td);
488 if (bcount != biosize)
494 * If B_CACHE is not set, we must issue the read. If this
495 * fails, we return an error.
497 if ((bp->b_flags & B_CACHE) == 0) {
498 bp->b_cmd = BUF_CMD_READ;
499 bp->b_bio2.bio_done = nfsiodone_sync;
500 bp->b_bio2.bio_flags |= BIO_SYNC;
501 vfs_busy_pages(vp, bp);
502 error = nfs_doio(vp, &bp->b_bio2, td);
510 * on is the offset into the current bp. Figure out how many
511 * bytes we can copy out of the bp. Note that bcount is
512 * NOT DEV_BSIZE aligned.
514 * Then figure out how many bytes we can copy into the uio.
518 n = (int)szmin((unsigned)(bcount - on), uio->uio_resid);
521 biosize = min(NFS_MAXPATHLEN, np->n_size);
522 nfsstats.biocache_readlinks++;
523 bp = nfs_getcacheblk(vp, (off_t)0, biosize, td);
526 if ((bp->b_flags & B_CACHE) == 0) {
527 bp->b_cmd = BUF_CMD_READ;
528 bp->b_bio2.bio_done = nfsiodone_sync;
529 bp->b_bio2.bio_flags |= BIO_SYNC;
530 vfs_busy_pages(vp, bp);
531 error = nfs_doio(vp, &bp->b_bio2, td);
533 bp->b_flags |= B_ERROR | B_INVAL;
538 n = (int)szmin(uio->uio_resid, bp->b_bcount - bp->b_resid);
542 nfsstats.biocache_readdirs++;
543 if (np->n_direofoffset
544 && uio->uio_offset >= np->n_direofoffset) {
547 lbn = (uoff_t)uio->uio_offset / NFS_DIRBLKSIZ;
548 on = uio->uio_offset & (NFS_DIRBLKSIZ - 1);
549 loffset = uio->uio_offset - on;
550 bp = nfs_getcacheblk(vp, loffset, NFS_DIRBLKSIZ, td);
554 if ((bp->b_flags & B_CACHE) == 0) {
555 bp->b_cmd = BUF_CMD_READ;
556 bp->b_bio2.bio_done = nfsiodone_sync;
557 bp->b_bio2.bio_flags |= BIO_SYNC;
558 vfs_busy_pages(vp, bp);
559 error = nfs_doio(vp, &bp->b_bio2, td);
562 while (error == NFSERR_BAD_COOKIE) {
563 kprintf("got bad cookie vp %p bp %p\n", vp, bp);
565 error = nfs_vinvalbuf(vp, 0, 1);
567 * Yuck! The directory has been modified on the
568 * server. The only way to get the block is by
569 * reading from the beginning to get all the
572 * Leave the last bp intact unless there is an error.
573 * Loop back up to the while if the error is another
574 * NFSERR_BAD_COOKIE (double yuch!).
576 for (i = 0; i <= lbn && !error; i++) {
577 if (np->n_direofoffset
578 && (i * NFS_DIRBLKSIZ) >= np->n_direofoffset)
580 bp = nfs_getcacheblk(vp, (off_t)i * NFS_DIRBLKSIZ,
584 if ((bp->b_flags & B_CACHE) == 0) {
585 bp->b_cmd = BUF_CMD_READ;
586 bp->b_bio2.bio_done = nfsiodone_sync;
587 bp->b_bio2.bio_flags |= BIO_SYNC;
588 vfs_busy_pages(vp, bp);
589 error = nfs_doio(vp, &bp->b_bio2, td);
591 * no error + B_INVAL == directory EOF,
594 if (error == 0 && (bp->b_flags & B_INVAL))
598 * An error will throw away the block and the
599 * for loop will break out. If no error and this
600 * is not the block we want, we throw away the
601 * block and go for the next one via the for loop.
603 if (error || i < lbn)
608 * The above while is repeated if we hit another cookie
609 * error. If we hit an error and it wasn't a cookie error,
617 * If not eof and read aheads are enabled, start one.
618 * (You need the current block first, so that you have the
619 * directory offset cookie of the next block.)
621 if (nmp->nm_readahead > 0 && nfs_asyncok(nmp) &&
622 (bp->b_flags & B_INVAL) == 0 &&
623 (np->n_direofoffset == 0 ||
624 loffset + NFS_DIRBLKSIZ < np->n_direofoffset) &&
625 (np->n_flag & NDONTCACHE) == 0 &&
626 findblk(vp, loffset + NFS_DIRBLKSIZ, FINDBLK_TEST) == NULL
628 rabp = nfs_getcacheblk(vp, loffset + NFS_DIRBLKSIZ,
631 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
632 rabp->b_cmd = BUF_CMD_READ;
633 vfs_busy_pages(vp, rabp);
634 nfs_asyncio(vp, &rabp->b_bio2);
641 * Unlike VREG files, whos buffer size ( bp->b_bcount ) is
642 * chopped for the EOF condition, we cannot tell how large
643 * NFS directories are going to be until we hit EOF. So
644 * an NFS directory buffer is *not* chopped to its EOF. Now,
645 * it just so happens that b_resid will effectively chop it
646 * to EOF. *BUT* this information is lost if the buffer goes
647 * away and is reconstituted into a B_CACHE state ( due to
648 * being VMIO ) later. So we keep track of the directory eof
649 * in np->n_direofoffset and chop it off as an extra step
652 n = (int)szmin(uio->uio_resid,
653 NFS_DIRBLKSIZ - bp->b_resid - on);
654 if (np->n_direofoffset && n > np->n_direofoffset - uio->uio_offset)
655 n = np->n_direofoffset - uio->uio_offset;
658 kprintf(" nfs_bioread: type %x unexpected\n",vp->v_type);
662 switch (vp->v_type) {
665 error = uiomove(bp->b_data + on, (int)n, uio);
669 error = uiomove(bp->b_data + on, (int)n, uio);
674 off_t old_off = uio->uio_offset;
676 struct nfs_dirent *dp;
679 * We are casting cpos to nfs_dirent, it must be
687 cpos = bp->b_data + on;
688 epos = bp->b_data + on + n;
689 while (cpos < epos && error == 0 && uio->uio_resid > 0) {
690 dp = (struct nfs_dirent *)cpos;
691 error = nfs_check_dirent(dp, (int)(epos - cpos));
694 if (vop_write_dirent(&error, uio, dp->nfs_ino,
695 dp->nfs_type, dp->nfs_namlen, dp->nfs_name)) {
698 cpos += dp->nfs_reclen;
702 uio->uio_offset = old_off + cpos - bp->b_data - on;
705 * Invalidate buffer if caching is disabled, forcing a
706 * re-read from the remote later.
708 if (np->n_flag & NDONTCACHE)
709 bp->b_flags |= B_INVAL;
712 kprintf(" nfs_bioread: type %x unexpected\n",vp->v_type);
715 } while (error == 0 && uio->uio_resid > 0 && n > 0);
720 * Userland can supply any 'seek' offset when reading a NFS directory.
721 * Validate the structure so we don't panic the kernel. Note that
722 * the element name is nul terminated and the nul is not included
727 nfs_check_dirent(struct nfs_dirent *dp, int maxlen)
729 int nfs_name_off = offsetof(struct nfs_dirent, nfs_name[0]);
731 if (nfs_name_off >= maxlen)
733 if (dp->nfs_reclen < nfs_name_off || dp->nfs_reclen > maxlen)
735 if (nfs_name_off + dp->nfs_namlen >= dp->nfs_reclen)
737 if (dp->nfs_reclen & 3)
743 * Vnode op for write using bio
745 * nfs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
746 * struct ucred *a_cred)
749 nfs_write(struct vop_write_args *ap)
751 struct uio *uio = ap->a_uio;
752 struct thread *td = uio->uio_td;
753 struct vnode *vp = ap->a_vp;
754 struct nfsnode *np = VTONFS(vp);
755 int ioflag = ap->a_ioflag;
758 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
761 int n, on, error = 0, iomode, must_commit;
767 if (uio->uio_rw != UIO_WRITE)
768 panic("nfs_write mode");
769 if (uio->uio_segflg == UIO_USERSPACE && uio->uio_td != curthread)
770 panic("nfs_write proc");
772 if (vp->v_type != VREG)
774 if (np->n_flag & NWRITEERR) {
775 np->n_flag &= ~NWRITEERR;
776 return (np->n_error);
778 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
779 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
780 (void)nfs_fsinfo(nmp, vp, td);
783 * Synchronously flush pending buffers if we are in synchronous
784 * mode or if we are appending.
786 if (ioflag & (IO_APPEND | IO_SYNC)) {
787 if (np->n_flag & NLMODIFIED) {
789 error = nfs_flush(vp, MNT_WAIT, td, 0);
790 /* error = nfs_vinvalbuf(vp, V_SAVE, 1); */
797 * If IO_APPEND then load uio_offset. We restart here if we cannot
798 * get the append lock.
801 if (ioflag & IO_APPEND) {
803 error = VOP_GETATTR(vp, &vattr);
806 uio->uio_offset = np->n_size;
809 if (uio->uio_offset < 0)
811 if ((uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
813 if (uio->uio_resid == 0)
817 * We need to obtain the rslock if we intend to modify np->n_size
818 * in order to guarentee the append point with multiple contending
819 * writers, to guarentee that no other appenders modify n_size
820 * while we are trying to obtain a truncated buffer (i.e. to avoid
821 * accidently truncating data written by another appender due to
822 * the race), and to ensure that the buffer is populated prior to
823 * our extending of the file. We hold rslock through the entire
826 * Note that we do not synchronize the case where someone truncates
827 * the file while we are appending to it because attempting to lock
828 * this case may deadlock other parts of the system unexpectedly.
830 if ((ioflag & IO_APPEND) ||
831 uio->uio_offset + uio->uio_resid > np->n_size) {
832 switch(nfs_rslock(np)) {
847 * Maybe this should be above the vnode op call, but so long as
848 * file servers have no limits, i don't think it matters
850 if (td->td_proc && uio->uio_offset + uio->uio_resid >
851 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
852 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
858 biosize = vp->v_mount->mnt_stat.f_iosize;
861 if ((np->n_flag & NDONTCACHE) && uio->uio_iovcnt == 1) {
862 iomode = NFSV3WRITE_FILESYNC;
863 error = nfs_writerpc_uio(vp, uio, &iomode, &must_commit);
865 nfs_clearcommit(vp->v_mount);
868 nfsstats.biocache_writes++;
869 lbn = uio->uio_offset / biosize;
870 on = uio->uio_offset & (biosize-1);
871 loffset = uio->uio_offset - on;
872 n = (int)szmin((unsigned)(biosize - on), uio->uio_resid);
875 * Handle direct append and file extension cases, calculate
876 * unaligned buffer size.
879 if (uio->uio_offset == np->n_size && n) {
881 * Get the buffer (in its pre-append state to maintain
882 * B_CACHE if it was previously set). Resize the
883 * nfsnode after we have locked the buffer to prevent
884 * readers from reading garbage.
887 bp = nfs_getcacheblk(vp, loffset, bcount, td);
892 np->n_size = uio->uio_offset + n;
893 np->n_flag |= NLMODIFIED;
894 vnode_pager_setsize(vp, np->n_size);
896 save = bp->b_flags & B_CACHE;
898 allocbuf(bp, bcount);
903 * Obtain the locked cache block first, and then
904 * adjust the file's size as appropriate.
907 if (loffset + bcount < np->n_size) {
908 if (loffset + biosize < np->n_size)
911 bcount = np->n_size - loffset;
913 bp = nfs_getcacheblk(vp, loffset, bcount, td);
914 if (uio->uio_offset + n > np->n_size) {
915 np->n_size = uio->uio_offset + n;
916 np->n_flag |= NLMODIFIED;
917 vnode_pager_setsize(vp, np->n_size);
927 * Avoid a read by setting B_CACHE where the data we
928 * intend to write covers the entire buffer. This also
929 * handles the normal append case as bcount will have
930 * byte resolution. The buffer state must also be adjusted.
932 * See the comments in kern/vfs_bio.c's getblk() for
935 * When doing a UIO_NOCOPY write the buffer is not
936 * overwritten and we cannot just set B_CACHE unconditionally
937 * for full-block writes.
939 if (on == 0 && n == bcount && uio->uio_segflg != UIO_NOCOPY) {
940 bp->b_flags |= B_CACHE;
941 bp->b_flags &= ~(B_ERROR | B_INVAL);
945 * b_resid may be set due to file EOF if we extended out.
946 * The NFS bio code will zero the difference anyway so
947 * just acknowledged the fact and set b_resid to 0.
949 if ((bp->b_flags & B_CACHE) == 0) {
950 bp->b_cmd = BUF_CMD_READ;
951 bp->b_bio2.bio_done = nfsiodone_sync;
952 bp->b_bio2.bio_flags |= BIO_SYNC;
953 vfs_busy_pages(vp, bp);
954 error = nfs_doio(vp, &bp->b_bio2, td);
965 np->n_flag |= NLMODIFIED;
968 * If dirtyend exceeds file size, chop it down. This should
969 * not normally occur but there is an append race where it
970 * might occur XXX, so we log it.
972 * If the chopping creates a reverse-indexed or degenerate
973 * situation with dirtyoff/end, we 0 both of them.
976 if (bp->b_dirtyend > bcount) {
977 kprintf("NFS append race @%08llx:%d\n",
978 (long long)bp->b_bio2.bio_offset,
979 bp->b_dirtyend - bcount);
980 bp->b_dirtyend = bcount;
983 if (bp->b_dirtyoff >= bp->b_dirtyend)
984 bp->b_dirtyoff = bp->b_dirtyend = 0;
987 * If the new write will leave a contiguous dirty
988 * area, just update the b_dirtyoff and b_dirtyend,
989 * otherwise force a write rpc of the old dirty area.
991 * While it is possible to merge discontiguous writes due to
992 * our having a B_CACHE buffer ( and thus valid read data
993 * for the hole), we don't because it could lead to
994 * significant cache coherency problems with multiple clients,
995 * especially if locking is implemented later on.
997 * as an optimization we could theoretically maintain
998 * a linked list of discontinuous areas, but we would still
999 * have to commit them separately so there isn't much
1000 * advantage to it except perhaps a bit of asynchronization.
1002 if (bp->b_dirtyend > 0 &&
1003 (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) {
1004 if (bwrite(bp) == EINTR) {
1011 error = uiomove((char *)bp->b_data + on, n, uio);
1014 * Since this block is being modified, it must be written
1015 * again and not just committed. Since write clustering does
1016 * not work for the stage 1 data write, only the stage 2
1017 * commit rpc, we have to clear B_CLUSTEROK as well.
1019 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1022 bp->b_flags |= B_ERROR;
1028 * Only update dirtyoff/dirtyend if not a degenerate
1031 * The underlying VM pages have been marked valid by
1032 * virtue of acquiring the bp. Because the entire buffer
1033 * is marked dirty we do not have to worry about cleaning
1034 * out the related dirty bits (and wouldn't really know
1035 * how to deal with byte ranges anyway)
1038 if (bp->b_dirtyend > 0) {
1039 bp->b_dirtyoff = min(on, bp->b_dirtyoff);
1040 bp->b_dirtyend = max((on + n), bp->b_dirtyend);
1042 bp->b_dirtyoff = on;
1043 bp->b_dirtyend = on + n;
1048 * If the lease is non-cachable or IO_SYNC do bwrite().
1050 * IO_INVAL appears to be unused. The idea appears to be
1051 * to turn off caching in this case. Very odd. XXX
1053 * If nfs_async is set bawrite() will use an unstable write
1054 * (build dirty bufs on the server), so we might as well
1055 * push it out with bawrite(). If nfs_async is not set we
1056 * use bdwrite() to cache dirty bufs on the client.
1058 if ((np->n_flag & NDONTCACHE) || (ioflag & IO_SYNC)) {
1059 if (ioflag & IO_INVAL)
1060 bp->b_flags |= B_NOCACHE;
1064 if (np->n_flag & NDONTCACHE) {
1065 error = nfs_vinvalbuf(vp, V_SAVE, 1);
1069 } else if ((n + on) == biosize && nfs_async) {
1074 } while (uio->uio_resid > 0 && n > 0);
1083 * Get an nfs cache block.
1085 * Allocate a new one if the block isn't currently in the cache
1086 * and return the block marked busy. If the calling process is
1087 * interrupted by a signal for an interruptible mount point, return
1090 * The caller must carefully deal with the possible B_INVAL state of
1091 * the buffer. nfs_startio() clears B_INVAL (and nfs_asyncio() clears it
1092 * indirectly), so synchronous reads can be issued without worrying about
1093 * the B_INVAL state. We have to be a little more careful when dealing
1094 * with writes (see comments in nfs_write()) when extending a file past
1098 nfs_getcacheblk(struct vnode *vp, off_t loffset, int size, struct thread *td)
1102 struct nfsmount *nmp;
1107 if (nmp->nm_flag & NFSMNT_INT) {
1108 bp = getblk(vp, loffset, size, GETBLK_PCATCH, 0);
1109 while (bp == NULL) {
1110 if (nfs_sigintr(nmp, NULL, td))
1112 bp = getblk(vp, loffset, size, 0, 2 * hz);
1115 bp = getblk(vp, loffset, size, 0, 0);
1119 * bio2, the 'device' layer. Since BIOs use 64 bit byte offsets
1120 * now, no translation is necessary.
1122 bp->b_bio2.bio_offset = loffset;
1127 * Flush and invalidate all dirty buffers. If another process is already
1128 * doing the flush, just wait for completion.
1131 nfs_vinvalbuf(struct vnode *vp, int flags, int intrflg)
1133 struct nfsnode *np = VTONFS(vp);
1134 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1135 int error = 0, slpflag, slptimeo;
1136 thread_t td = curthread;
1138 if (vp->v_flag & VRECLAIMED)
1141 if ((nmp->nm_flag & NFSMNT_INT) == 0)
1151 * First wait for any other process doing a flush to complete.
1153 while (np->n_flag & NFLUSHINPROG) {
1154 np->n_flag |= NFLUSHWANT;
1155 error = tsleep((caddr_t)&np->n_flag, 0, "nfsvinval", slptimeo);
1156 if (error && intrflg && nfs_sigintr(nmp, NULL, td))
1161 * Now, flush as required.
1163 np->n_flag |= NFLUSHINPROG;
1164 error = vinvalbuf(vp, flags, slpflag, 0);
1166 if (intrflg && nfs_sigintr(nmp, NULL, td)) {
1167 np->n_flag &= ~NFLUSHINPROG;
1168 if (np->n_flag & NFLUSHWANT) {
1169 np->n_flag &= ~NFLUSHWANT;
1170 wakeup((caddr_t)&np->n_flag);
1174 error = vinvalbuf(vp, flags, 0, slptimeo);
1176 np->n_flag &= ~(NLMODIFIED | NFLUSHINPROG);
1177 if (np->n_flag & NFLUSHWANT) {
1178 np->n_flag &= ~NFLUSHWANT;
1179 wakeup((caddr_t)&np->n_flag);
1185 * Return true (non-zero) if the txthread and rxthread are operational
1186 * and we do not already have too many not-yet-started BIO's built up.
1189 nfs_asyncok(struct nfsmount *nmp)
1191 return (nmp->nm_bioqlen < nfs_maxasyncbio &&
1192 nmp->nm_bioqlen < nmp->nm_maxasync_scaled / NFS_ASYSCALE &&
1193 nmp->nm_rxstate <= NFSSVC_PENDING &&
1194 nmp->nm_txstate <= NFSSVC_PENDING);
1198 * The read-ahead code calls this to queue a bio to the txthread.
1200 * We don't touch the bio otherwise... that is, we do not even
1201 * construct or send the initial rpc. The txthread will do it
1204 * NOTE! nm_bioqlen is not decremented until the request completes,
1205 * so it does not reflect the number of bio's on bioq.
1208 nfs_asyncio(struct vnode *vp, struct bio *bio)
1210 struct buf *bp = bio->bio_buf;
1211 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1213 KKASSERT(vp->v_tag == VT_NFS);
1215 bio->bio_driver_info = vp;
1217 TAILQ_INSERT_TAIL(&nmp->nm_bioq, bio, bio_act);
1218 atomic_add_int(&nmp->nm_bioqlen, 1);
1220 nfssvc_iod_writer_wakeup(nmp);
1224 * nfs_dio() - Execute a BIO operation synchronously. The BIO will be
1225 * completed and its error returned. The caller is responsible
1226 * for brelse()ing it. ONLY USE FOR BIO_SYNC IOs! Otherwise
1227 * our error probe will be against an invalid pointer.
1229 * nfs_startio()- Execute a BIO operation assynchronously.
1231 * NOTE: nfs_asyncio() is used to initiate an asynchronous BIO operation,
1232 * which basically just queues it to the txthread. nfs_startio()
1233 * actually initiates the I/O AFTER it has gotten to the txthread.
1235 * NOTE: td might be NULL.
1238 nfs_startio(struct vnode *vp, struct bio *bio, struct thread *td)
1240 struct buf *bp = bio->bio_buf;
1242 struct nfsmount *nmp;
1244 KKASSERT(vp->v_tag == VT_NFS);
1246 nmp = VFSTONFS(vp->v_mount);
1249 * clear B_ERROR and B_INVAL state prior to initiating the I/O. We
1250 * do this here so we do not have to do it in all the code that
1253 bp->b_flags &= ~(B_ERROR | B_INVAL);
1255 KASSERT(bp->b_cmd != BUF_CMD_DONE,
1256 ("nfs_doio: bp %p already marked done!", bp));
1258 if (bp->b_cmd == BUF_CMD_READ) {
1259 switch (vp->v_type) {
1261 nfsstats.read_bios++;
1262 nfs_readrpc_bio(vp, bio);
1266 bio->bio_offset = 0;
1267 nfsstats.readlink_bios++;
1268 nfs_readlinkrpc_bio(vp, bio);
1270 nfs_doio(vp, bio, td);
1275 * NOTE: If nfs_readdirplusrpc_bio() is requested but
1276 * not supported, it will chain to
1277 * nfs_readdirrpc_bio().
1280 nfsstats.readdir_bios++;
1281 uiop->uio_offset = bio->bio_offset;
1282 if (nmp->nm_flag & NFSMNT_RDIRPLUS)
1283 nfs_readdirplusrpc_bio(vp, bio);
1285 nfs_readdirrpc_bio(vp, bio);
1287 nfs_doio(vp, bio, td);
1291 kprintf("nfs_doio: type %x unexpected\n",vp->v_type);
1292 bp->b_flags |= B_ERROR;
1293 bp->b_error = EINVAL;
1299 * If we only need to commit, try to commit. If this fails
1300 * it will chain through to the write. Basically all the logic
1301 * in nfs_doio() is replicated.
1303 KKASSERT(bp->b_cmd == BUF_CMD_WRITE);
1304 if (bp->b_flags & B_NEEDCOMMIT)
1305 nfs_commitrpc_bio(vp, bio);
1307 nfs_writerpc_bio(vp, bio);
1312 nfs_doio(struct vnode *vp, struct bio *bio, struct thread *td)
1314 struct buf *bp = bio->bio_buf;
1317 struct nfsmount *nmp;
1319 int iomode, must_commit;
1324 KKASSERT(vp->v_tag == VT_NFS);
1326 nmp = VFSTONFS(vp->v_mount);
1328 uiop->uio_iov = &io;
1329 uiop->uio_iovcnt = 1;
1330 uiop->uio_segflg = UIO_SYSSPACE;
1334 * clear B_ERROR and B_INVAL state prior to initiating the I/O. We
1335 * do this here so we do not have to do it in all the code that
1338 bp->b_flags &= ~(B_ERROR | B_INVAL);
1340 KASSERT(bp->b_cmd != BUF_CMD_DONE,
1341 ("nfs_doio: bp %p already marked done!", bp));
1343 if (bp->b_cmd == BUF_CMD_READ) {
1344 io.iov_len = uiop->uio_resid = (size_t)bp->b_bcount;
1345 io.iov_base = bp->b_data;
1346 uiop->uio_rw = UIO_READ;
1348 switch (vp->v_type) {
1351 * When reading from a regular file zero-fill any residual.
1352 * Note that this residual has nothing to do with NFS short
1353 * reads, which nfs_readrpc_uio() will handle for us.
1355 * We have to do this because when we are write extending
1356 * a file the server may not have the same notion of
1357 * filesize as we do. Our BIOs should already be sized
1358 * (b_bcount) to account for the file EOF.
1360 nfsstats.read_bios++;
1361 uiop->uio_offset = bio->bio_offset;
1362 error = nfs_readrpc_uio(vp, uiop);
1363 if (error == 0 && uiop->uio_resid) {
1364 n = (size_t)bp->b_bcount - uiop->uio_resid;
1365 bzero(bp->b_data + n, bp->b_bcount - n);
1366 uiop->uio_resid = 0;
1368 if (td && td->td_proc && (vp->v_flag & VTEXT) &&
1369 np->n_mtime != np->n_vattr.va_mtime.tv_sec) {
1370 uprintf("Process killed due to text file modification\n");
1371 ksignal(td->td_proc, SIGKILL);
1375 uiop->uio_offset = 0;
1376 nfsstats.readlink_bios++;
1377 error = nfs_readlinkrpc_uio(vp, uiop);
1380 nfsstats.readdir_bios++;
1381 uiop->uio_offset = bio->bio_offset;
1382 if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
1383 error = nfs_readdirplusrpc_uio(vp, uiop);
1384 if (error == NFSERR_NOTSUPP)
1385 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
1387 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
1388 error = nfs_readdirrpc_uio(vp, uiop);
1390 * end-of-directory sets B_INVAL but does not generate an
1393 if (error == 0 && uiop->uio_resid == bp->b_bcount)
1394 bp->b_flags |= B_INVAL;
1397 kprintf("nfs_doio: type %x unexpected\n",vp->v_type);
1401 bp->b_flags |= B_ERROR;
1402 bp->b_error = error;
1404 bp->b_resid = uiop->uio_resid;
1407 * If we only need to commit, try to commit
1409 KKASSERT(bp->b_cmd == BUF_CMD_WRITE);
1410 if (bp->b_flags & B_NEEDCOMMIT) {
1414 off = bio->bio_offset + bp->b_dirtyoff;
1415 retv = nfs_commitrpc_uio(vp, off,
1416 bp->b_dirtyend - bp->b_dirtyoff,
1419 bp->b_dirtyoff = bp->b_dirtyend = 0;
1420 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1425 if (retv == NFSERR_STALEWRITEVERF) {
1426 nfs_clearcommit(vp->v_mount);
1431 * Setup for actual write
1433 if (bio->bio_offset + bp->b_dirtyend > np->n_size)
1434 bp->b_dirtyend = np->n_size - bio->bio_offset;
1436 if (bp->b_dirtyend > bp->b_dirtyoff) {
1437 io.iov_len = uiop->uio_resid = bp->b_dirtyend
1439 uiop->uio_offset = bio->bio_offset + bp->b_dirtyoff;
1440 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
1441 uiop->uio_rw = UIO_WRITE;
1442 nfsstats.write_bios++;
1444 if ((bp->b_flags & (B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == 0)
1445 iomode = NFSV3WRITE_UNSTABLE;
1447 iomode = NFSV3WRITE_FILESYNC;
1450 error = nfs_writerpc_uio(vp, uiop, &iomode, &must_commit);
1453 * When setting B_NEEDCOMMIT also set B_CLUSTEROK to try
1454 * to cluster the buffers needing commit. This will allow
1455 * the system to submit a single commit rpc for the whole
1456 * cluster. We can do this even if the buffer is not 100%
1457 * dirty (relative to the NFS blocksize), so we optimize the
1458 * append-to-file-case.
1460 * (when clearing B_NEEDCOMMIT, B_CLUSTEROK must also be
1461 * cleared because write clustering only works for commit
1462 * rpc's, not for the data portion of the write).
1465 if (!error && iomode == NFSV3WRITE_UNSTABLE) {
1466 bp->b_flags |= B_NEEDCOMMIT;
1467 if (bp->b_dirtyoff == 0
1468 && bp->b_dirtyend == bp->b_bcount)
1469 bp->b_flags |= B_CLUSTEROK;
1471 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1475 * For an interrupted write, the buffer is still valid
1476 * and the write hasn't been pushed to the server yet,
1477 * so we can't set B_ERROR and report the interruption
1478 * by setting B_EINTR. For the async case, B_EINTR
1479 * is not relevant, so the rpc attempt is essentially
1480 * a noop. For the case of a V3 write rpc not being
1481 * committed to stable storage, the block is still
1482 * dirty and requires either a commit rpc or another
1483 * write rpc with iomode == NFSV3WRITE_FILESYNC before
1484 * the block is reused. This is indicated by setting
1485 * the B_DELWRI and B_NEEDCOMMIT flags.
1487 * If the buffer is marked B_PAGING, it does not reside on
1488 * the vp's paging queues so we cannot call bdirty(). The
1489 * bp in this case is not an NFS cache block so we should
1493 || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
1495 bp->b_flags &= ~(B_INVAL|B_NOCACHE);
1496 if ((bp->b_flags & B_PAGING) == 0)
1499 bp->b_flags |= B_EINTR;
1503 bp->b_flags |= B_ERROR;
1504 bp->b_error = np->n_error = error;
1505 np->n_flag |= NWRITEERR;
1507 bp->b_dirtyoff = bp->b_dirtyend = 0;
1510 nfs_clearcommit(vp->v_mount);
1511 bp->b_resid = uiop->uio_resid;
1518 * I/O was run synchronously, biodone() it and calculate the
1522 KKASSERT(bp->b_cmd == BUF_CMD_DONE);
1523 if (bp->b_flags & B_EINTR)
1525 if (bp->b_flags & B_ERROR)
1526 return (bp->b_error ? bp->b_error : EIO);
1531 * Used to aid in handling ftruncate() operations on the NFS client side.
1532 * Truncation creates a number of special problems for NFS. We have to
1533 * throw away VM pages and buffer cache buffers that are beyond EOF, and
1534 * we have to properly handle VM pages or (potentially dirty) buffers
1535 * that straddle the truncation point.
1539 nfs_meta_setsize(struct vnode *vp, struct thread *td, u_quad_t nsize)
1541 struct nfsnode *np = VTONFS(vp);
1542 u_quad_t tsize = np->n_size;
1543 int biosize = vp->v_mount->mnt_stat.f_iosize;
1548 if (nsize < tsize) {
1554 * vtruncbuf() doesn't get the buffer overlapping the
1555 * truncation point. We may have a B_DELWRI and/or B_CACHE
1556 * buffer that now needs to be truncated.
1558 error = vtruncbuf(vp, nsize, biosize);
1559 bufsize = nsize & (biosize - 1);
1560 loffset = nsize - bufsize;
1561 bp = nfs_getcacheblk(vp, loffset, bufsize, td);
1562 if (bp->b_dirtyoff > bp->b_bcount)
1563 bp->b_dirtyoff = bp->b_bcount;
1564 if (bp->b_dirtyend > bp->b_bcount)
1565 bp->b_dirtyend = bp->b_bcount;
1566 bp->b_flags |= B_RELBUF; /* don't leave garbage around */
1569 vnode_pager_setsize(vp, nsize);
1575 * Synchronous completion for nfs_doio. Call bpdone() with elseit=FALSE.
1576 * Caller is responsible for brelse()'ing the bp.
1579 nfsiodone_sync(struct bio *bio)
1582 bpdone(bio->bio_buf, 0);
1586 * nfs read rpc - BIO version
1589 nfs_readrpc_bio(struct vnode *vp, struct bio *bio)
1591 struct buf *bp = bio->bio_buf;
1593 struct nfsmount *nmp;
1594 int error = 0, len, tsiz;
1595 struct nfsm_info *info;
1597 info = kmalloc(sizeof(*info), M_NFSREQ, M_WAITOK);
1599 info->v3 = NFS_ISV3(vp);
1601 nmp = VFSTONFS(vp->v_mount);
1602 tsiz = bp->b_bcount;
1603 KKASSERT(tsiz <= nmp->nm_rsize);
1604 if (bio->bio_offset + tsiz > nmp->nm_maxfilesize) {
1608 nfsstats.rpccnt[NFSPROC_READ]++;
1610 nfsm_reqhead(info, vp, NFSPROC_READ,
1611 NFSX_FH(info->v3) + NFSX_UNSIGNED * 3);
1612 ERROROUT(nfsm_fhtom(info, vp));
1613 tl = nfsm_build(info, NFSX_UNSIGNED * 3);
1615 txdr_hyper(bio->bio_offset, tl);
1616 *(tl + 2) = txdr_unsigned(len);
1618 *tl++ = txdr_unsigned(bio->bio_offset);
1619 *tl++ = txdr_unsigned(len);
1623 info->done = nfs_readrpc_bio_done;
1624 nfsm_request_bio(info, vp, NFSPROC_READ, NULL,
1625 nfs_vpcred(vp, ND_READ));
1628 kfree(info, M_NFSREQ);
1629 bp->b_error = error;
1630 bp->b_flags |= B_ERROR;
1635 nfs_readrpc_bio_done(nfsm_info_t info)
1637 struct nfsmount *nmp = VFSTONFS(info->vp->v_mount);
1638 struct bio *bio = info->bio;
1639 struct buf *bp = bio->bio_buf;
1646 KKASSERT(info->state == NFSM_STATE_DONE);
1649 ERROROUT(nfsm_postop_attr(info, info->vp, &attrflag,
1650 NFS_LATTR_NOSHRINK));
1651 NULLOUT(tl = nfsm_dissect(info, 2 * NFSX_UNSIGNED));
1652 eof = fxdr_unsigned(int, *(tl + 1));
1654 ERROROUT(nfsm_loadattr(info, info->vp, NULL));
1657 NEGATIVEOUT(retlen = nfsm_strsiz(info, nmp->nm_rsize));
1658 ERROROUT(nfsm_mtobio(info, bio, retlen));
1659 m_freem(info->mrep);
1663 * No error occured, if retlen is less then bcount and no EOF
1664 * and NFSv3 a zero-fill short read occured.
1666 * For NFSv2 a short-read indicates EOF.
1668 if (retlen < bp->b_bcount && info->v3 && eof == 0) {
1669 bzero(bp->b_data + retlen, bp->b_bcount - retlen);
1670 retlen = bp->b_bcount;
1674 * If we hit an EOF we still zero-fill, but return the expected
1675 * b_resid anyway. This should normally not occur since async
1676 * BIOs are not used for read-before-write case. Races against
1677 * the server can cause it though and we don't want to leave
1678 * garbage in the buffer.
1680 if (retlen < bp->b_bcount) {
1681 bzero(bp->b_data + retlen, bp->b_bcount - retlen);
1684 /* bp->b_resid = bp->b_bcount - retlen; */
1686 kfree(info, M_NFSREQ);
1688 bp->b_error = error;
1689 bp->b_flags |= B_ERROR;
1695 * nfs write call - BIO version
1698 nfs_writerpc_bio(struct vnode *vp, struct bio *bio)
1700 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1701 struct nfsnode *np = VTONFS(vp);
1702 struct buf *bp = bio->bio_buf;
1707 struct nfsm_info *info;
1711 * Setup for actual write. Just clean up the bio if there
1714 if (bio->bio_offset + bp->b_dirtyend > np->n_size)
1715 bp->b_dirtyend = np->n_size - bio->bio_offset;
1717 if (bp->b_dirtyend <= bp->b_dirtyoff) {
1722 len = bp->b_dirtyend - bp->b_dirtyoff;
1723 offset = bio->bio_offset + bp->b_dirtyoff;
1724 if (offset + len > nmp->nm_maxfilesize) {
1725 bp->b_flags |= B_ERROR;
1726 bp->b_error = EFBIG;
1731 nfsstats.write_bios++;
1733 info = kmalloc(sizeof(*info), M_NFSREQ, M_WAITOK);
1735 info->v3 = NFS_ISV3(vp);
1736 info->info_writerpc.must_commit = 0;
1737 if ((bp->b_flags & (B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == 0)
1738 iomode = NFSV3WRITE_UNSTABLE;
1740 iomode = NFSV3WRITE_FILESYNC;
1742 KKASSERT(len <= nmp->nm_wsize);
1744 nfsstats.rpccnt[NFSPROC_WRITE]++;
1745 nfsm_reqhead(info, vp, NFSPROC_WRITE,
1746 NFSX_FH(info->v3) + 5 * NFSX_UNSIGNED + nfsm_rndup(len));
1747 ERROROUT(nfsm_fhtom(info, vp));
1749 tl = nfsm_build(info, 5 * NFSX_UNSIGNED);
1750 txdr_hyper(offset, tl);
1752 *tl++ = txdr_unsigned(len);
1753 *tl++ = txdr_unsigned(iomode);
1754 *tl = txdr_unsigned(len);
1758 tl = nfsm_build(info, 4 * NFSX_UNSIGNED);
1759 /* Set both "begin" and "current" to non-garbage. */
1760 x = txdr_unsigned((u_int32_t)offset);
1761 *tl++ = x; /* "begin offset" */
1762 *tl++ = x; /* "current offset" */
1763 x = txdr_unsigned(len);
1764 *tl++ = x; /* total to this offset */
1765 *tl = x; /* size of this write */
1767 ERROROUT(nfsm_biotom(info, bio, bp->b_dirtyoff, len));
1769 info->done = nfs_writerpc_bio_done;
1770 nfsm_request_bio(info, vp, NFSPROC_WRITE, NULL,
1771 nfs_vpcred(vp, ND_WRITE));
1774 kfree(info, M_NFSREQ);
1775 bp->b_error = error;
1776 bp->b_flags |= B_ERROR;
1781 nfs_writerpc_bio_done(nfsm_info_t info)
1783 struct nfsmount *nmp = VFSTONFS(info->vp->v_mount);
1784 struct nfsnode *np = VTONFS(info->vp);
1785 struct bio *bio = info->bio;
1786 struct buf *bp = bio->bio_buf;
1787 int wccflag = NFSV3_WCCRATTR;
1788 int iomode = NFSV3WRITE_FILESYNC;
1792 int len = bp->b_resid; /* b_resid was set to shortened length */
1797 * The write RPC returns a before and after mtime. The
1798 * nfsm_wcc_data() macro checks the before n_mtime
1799 * against the before time and stores the after time
1800 * in the nfsnode's cached vattr and n_mtime field.
1801 * The NRMODIFIED bit will be set if the before
1802 * time did not match the original mtime.
1804 wccflag = NFSV3_WCCCHK;
1805 ERROROUT(nfsm_wcc_data(info, info->vp, &wccflag));
1807 NULLOUT(tl = nfsm_dissect(info, 2 * NFSX_UNSIGNED + NFSX_V3WRITEVERF));
1808 rlen = fxdr_unsigned(int, *tl++);
1811 m_freem(info->mrep);
1814 } else if (rlen < len) {
1817 * XXX what do we do here?
1819 backup = len - rlen;
1820 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base - backup;
1821 uiop->uio_iov->iov_len += backup;
1822 uiop->uio_offset -= backup;
1823 uiop->uio_resid += backup;
1827 commit = fxdr_unsigned(int, *tl++);
1830 * Return the lowest committment level
1831 * obtained by any of the RPCs.
1833 if (iomode == NFSV3WRITE_FILESYNC)
1835 else if (iomode == NFSV3WRITE_DATASYNC &&
1836 commit == NFSV3WRITE_UNSTABLE)
1838 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0){
1839 bcopy(tl, (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF);
1840 nmp->nm_state |= NFSSTA_HASWRITEVERF;
1841 } else if (bcmp(tl, nmp->nm_verf, NFSX_V3WRITEVERF)) {
1842 info->info_writerpc.must_commit = 1;
1843 bcopy(tl, (caddr_t)nmp->nm_verf, NFSX_V3WRITEVERF);
1847 ERROROUT(nfsm_loadattr(info, info->vp, NULL));
1849 m_freem(info->mrep);
1853 if (info->vp->v_mount->mnt_flag & MNT_ASYNC)
1854 iomode = NFSV3WRITE_FILESYNC;
1858 * End of RPC. Now clean up the bp.
1860 * When setting B_NEEDCOMMIT also set B_CLUSTEROK to try
1861 * to cluster the buffers needing commit. This will allow
1862 * the system to submit a single commit rpc for the whole
1863 * cluster. We can do this even if the buffer is not 100%
1864 * dirty (relative to the NFS blocksize), so we optimize the
1865 * append-to-file-case.
1867 * (when clearing B_NEEDCOMMIT, B_CLUSTEROK must also be
1868 * cleared because write clustering only works for commit
1869 * rpc's, not for the data portion of the write).
1871 if (!error && iomode == NFSV3WRITE_UNSTABLE) {
1872 bp->b_flags |= B_NEEDCOMMIT;
1873 if (bp->b_dirtyoff == 0 && bp->b_dirtyend == bp->b_bcount)
1874 bp->b_flags |= B_CLUSTEROK;
1876 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1880 * For an interrupted write, the buffer is still valid
1881 * and the write hasn't been pushed to the server yet,
1882 * so we can't set B_ERROR and report the interruption
1883 * by setting B_EINTR. For the async case, B_EINTR
1884 * is not relevant, so the rpc attempt is essentially
1885 * a noop. For the case of a V3 write rpc not being
1886 * committed to stable storage, the block is still
1887 * dirty and requires either a commit rpc or another
1888 * write rpc with iomode == NFSV3WRITE_FILESYNC before
1889 * the block is reused. This is indicated by setting
1890 * the B_DELWRI and B_NEEDCOMMIT flags.
1892 * If the buffer is marked B_PAGING, it does not reside on
1893 * the vp's paging queues so we cannot call bdirty(). The
1894 * bp in this case is not an NFS cache block so we should
1897 if (error == EINTR || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
1899 bp->b_flags &= ~(B_INVAL|B_NOCACHE);
1900 if ((bp->b_flags & B_PAGING) == 0)
1903 bp->b_flags |= B_EINTR;
1907 bp->b_flags |= B_ERROR;
1908 bp->b_error = np->n_error = error;
1909 np->n_flag |= NWRITEERR;
1911 bp->b_dirtyoff = bp->b_dirtyend = 0;
1913 if (info->info_writerpc.must_commit)
1914 nfs_clearcommit(info->vp->v_mount);
1915 kfree(info, M_NFSREQ);
1917 bp->b_flags |= B_ERROR;
1918 bp->b_error = error;
1924 * Nfs Version 3 commit rpc - BIO version
1926 * This function issues the commit rpc and will chain to a write
1930 nfs_commitrpc_bio(struct vnode *vp, struct bio *bio)
1932 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1933 struct buf *bp = bio->bio_buf;
1934 struct nfsm_info *info;
1938 if ((nmp->nm_state & NFSSTA_HASWRITEVERF) == 0) {
1939 bp->b_dirtyoff = bp->b_dirtyend = 0;
1940 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1946 info = kmalloc(sizeof(*info), M_NFSREQ, M_WAITOK);
1950 nfsstats.rpccnt[NFSPROC_COMMIT]++;
1951 nfsm_reqhead(info, vp, NFSPROC_COMMIT, NFSX_FH(1));
1952 ERROROUT(nfsm_fhtom(info, vp));
1953 tl = nfsm_build(info, 3 * NFSX_UNSIGNED);
1954 txdr_hyper(bio->bio_offset + bp->b_dirtyoff, tl);
1956 *tl = txdr_unsigned(bp->b_dirtyend - bp->b_dirtyoff);
1958 info->done = nfs_commitrpc_bio_done;
1959 nfsm_request_bio(info, vp, NFSPROC_COMMIT, NULL,
1960 nfs_vpcred(vp, ND_WRITE));
1964 * Chain to write RPC on (early) error
1966 kfree(info, M_NFSREQ);
1967 nfs_writerpc_bio(vp, bio);
1971 nfs_commitrpc_bio_done(nfsm_info_t info)
1973 struct nfsmount *nmp = VFSTONFS(info->vp->v_mount);
1974 struct bio *bio = info->bio;
1975 struct buf *bp = bio->bio_buf;
1977 int wccflag = NFSV3_WCCRATTR;
1980 ERROROUT(nfsm_wcc_data(info, info->vp, &wccflag));
1982 NULLOUT(tl = nfsm_dissect(info, NFSX_V3WRITEVERF));
1983 if (bcmp(nmp->nm_verf, tl, NFSX_V3WRITEVERF)) {
1984 bcopy(tl, nmp->nm_verf, NFSX_V3WRITEVERF);
1985 error = NFSERR_STALEWRITEVERF;
1988 m_freem(info->mrep);
1992 * On completion we must chain to a write bio if an
1996 kfree(info, M_NFSREQ);
1998 bp->b_dirtyoff = bp->b_dirtyend = 0;
1999 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
2003 nfs_writerpc_bio(info->vp, bio);