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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)nfs_bio.c 8.9 (Berkeley) 3/30/95
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include "opt_kdtrace.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/mount.h>
48 #include <sys/rwlock.h>
49 #include <sys/vmmeter.h>
50 #include <sys/vnode.h>
53 #include <vm/vm_param.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>
60 #include <nfs/nfsproto.h>
61 #include <nfsclient/nfs.h>
62 #include <nfsclient/nfsmount.h>
63 #include <nfsclient/nfsnode.h>
64 #include <nfs/nfs_kdtrace.h>
66 static struct buf *nfs_getcacheblk(struct vnode *vp, daddr_t bn, int size,
68 static int nfs_directio_write(struct vnode *vp, struct uio *uiop,
69 struct ucred *cred, int ioflag);
71 extern int nfs_directio_enable;
72 extern int nfs_directio_allow_mmap;
75 * Vnode op for VM getpages.
78 nfs_getpages(struct vop_getpages_args *ap)
80 int i, error, nextoff, size, toff, count, npages;
95 td = curthread; /* XXX */
96 cred = curthread->td_ucred; /* XXX */
97 nmp = VFSTONFS(vp->v_mount);
101 if ((object = vp->v_object) == NULL) {
102 nfs_printf("nfs_getpages: called with non-merged cache vnode??\n");
103 return (VM_PAGER_ERROR);
106 if (nfs_directio_enable && !nfs_directio_allow_mmap) {
107 mtx_lock(&np->n_mtx);
108 if ((np->n_flag & NNONCACHE) && (vp->v_type == VREG)) {
109 mtx_unlock(&np->n_mtx);
110 nfs_printf("nfs_getpages: called on non-cacheable vnode??\n");
111 return (VM_PAGER_ERROR);
113 mtx_unlock(&np->n_mtx);
116 mtx_lock(&nmp->nm_mtx);
117 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
118 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) {
119 mtx_unlock(&nmp->nm_mtx);
120 /* We'll never get here for v4, because we always have fsinfo */
121 (void)nfs_fsinfo(nmp, vp, cred, td);
123 mtx_unlock(&nmp->nm_mtx);
125 npages = btoc(count);
128 * If the requested page is partially valid, just return it and
129 * allow the pager to zero-out the blanks. Partially valid pages
130 * can only occur at the file EOF.
132 VM_OBJECT_WLOCK(object);
133 if (pages[ap->a_reqpage]->valid != 0) {
134 for (i = 0; i < npages; ++i) {
135 if (i != ap->a_reqpage) {
136 vm_page_lock(pages[i]);
137 vm_page_free(pages[i]);
138 vm_page_unlock(pages[i]);
141 VM_OBJECT_WUNLOCK(object);
144 VM_OBJECT_WUNLOCK(object);
147 * We use only the kva address for the buffer, but this is extremely
148 * convienient and fast.
150 bp = getpbuf(&nfs_pbuf_freecnt);
152 kva = (vm_offset_t) bp->b_data;
153 pmap_qenter(kva, pages, npages);
154 PCPU_INC(cnt.v_vnodein);
155 PCPU_ADD(cnt.v_vnodepgsin, npages);
157 iov.iov_base = (caddr_t) kva;
161 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
162 uio.uio_resid = count;
163 uio.uio_segflg = UIO_SYSSPACE;
164 uio.uio_rw = UIO_READ;
167 error = (nmp->nm_rpcops->nr_readrpc)(vp, &uio, cred);
168 pmap_qremove(kva, npages);
170 relpbuf(bp, &nfs_pbuf_freecnt);
172 if (error && (uio.uio_resid == count)) {
173 nfs_printf("nfs_getpages: error %d\n", error);
174 VM_OBJECT_WLOCK(object);
175 for (i = 0; i < npages; ++i) {
176 if (i != ap->a_reqpage) {
177 vm_page_lock(pages[i]);
178 vm_page_free(pages[i]);
179 vm_page_unlock(pages[i]);
182 VM_OBJECT_WUNLOCK(object);
183 return (VM_PAGER_ERROR);
187 * Calculate the number of bytes read and validate only that number
188 * of bytes. Note that due to pending writes, size may be 0. This
189 * does not mean that the remaining data is invalid!
192 size = count - uio.uio_resid;
193 VM_OBJECT_WLOCK(object);
194 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
196 nextoff = toff + PAGE_SIZE;
199 if (nextoff <= size) {
201 * Read operation filled an entire page
203 m->valid = VM_PAGE_BITS_ALL;
204 KASSERT(m->dirty == 0,
205 ("nfs_getpages: page %p is dirty", m));
206 } else if (size > toff) {
208 * Read operation filled a partial page.
211 vm_page_set_valid_range(m, 0, size - toff);
212 KASSERT(m->dirty == 0,
213 ("nfs_getpages: page %p is dirty", m));
216 * Read operation was short. If no error
217 * occured we may have hit a zero-fill
218 * section. We leave valid set to 0, and page
219 * is freed by vm_page_readahead_finish() if
220 * its index is not equal to requested, or
221 * page is zeroed and set valid by
222 * vm_pager_get_pages() for requested page.
226 if (i != ap->a_reqpage)
227 vm_page_readahead_finish(m);
229 VM_OBJECT_WUNLOCK(object);
234 * Vnode op for VM putpages.
237 nfs_putpages(struct vop_putpages_args *ap)
243 int iomode, must_commit, i, error, npages, count;
249 struct nfsmount *nmp;
255 td = curthread; /* XXX */
256 /* Set the cred to n_writecred for the write rpcs. */
257 if (np->n_writecred != NULL)
258 cred = crhold(np->n_writecred);
260 cred = crhold(curthread->td_ucred); /* XXX */
261 nmp = VFSTONFS(vp->v_mount);
264 rtvals = ap->a_rtvals;
265 npages = btoc(count);
266 offset = IDX_TO_OFF(pages[0]->pindex);
268 mtx_lock(&nmp->nm_mtx);
269 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
270 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) {
271 mtx_unlock(&nmp->nm_mtx);
272 (void)nfs_fsinfo(nmp, vp, cred, td);
274 mtx_unlock(&nmp->nm_mtx);
276 mtx_lock(&np->n_mtx);
277 if (nfs_directio_enable && !nfs_directio_allow_mmap &&
278 (np->n_flag & NNONCACHE) && (vp->v_type == VREG)) {
279 mtx_unlock(&np->n_mtx);
280 nfs_printf("nfs_putpages: called on noncache-able vnode??\n");
281 mtx_lock(&np->n_mtx);
284 for (i = 0; i < npages; i++)
285 rtvals[i] = VM_PAGER_ERROR;
288 * When putting pages, do not extend file past EOF.
290 if (offset + count > np->n_size) {
291 count = np->n_size - offset;
295 mtx_unlock(&np->n_mtx);
298 * We use only the kva address for the buffer, but this is extremely
299 * convienient and fast.
301 bp = getpbuf(&nfs_pbuf_freecnt);
303 kva = (vm_offset_t) bp->b_data;
304 pmap_qenter(kva, pages, npages);
305 PCPU_INC(cnt.v_vnodeout);
306 PCPU_ADD(cnt.v_vnodepgsout, count);
308 iov.iov_base = (caddr_t) kva;
312 uio.uio_offset = offset;
313 uio.uio_resid = count;
314 uio.uio_segflg = UIO_SYSSPACE;
315 uio.uio_rw = UIO_WRITE;
318 if ((ap->a_sync & VM_PAGER_PUT_SYNC) == 0)
319 iomode = NFSV3WRITE_UNSTABLE;
321 iomode = NFSV3WRITE_FILESYNC;
323 error = (nmp->nm_rpcops->nr_writerpc)(vp, &uio, cred, &iomode, &must_commit);
326 pmap_qremove(kva, npages);
327 relpbuf(bp, &nfs_pbuf_freecnt);
330 vnode_pager_undirty_pages(pages, rtvals, count - uio.uio_resid);
332 nfs_clearcommit(vp->v_mount);
339 * For nfs, cache consistency can only be maintained approximately.
340 * Although RFC1094 does not specify the criteria, the following is
341 * believed to be compatible with the reference port.
343 * If the file's modify time on the server has changed since the
344 * last read rpc or you have written to the file,
345 * you may have lost data cache consistency with the
346 * server, so flush all of the file's data out of the cache.
347 * Then force a getattr rpc to ensure that you have up to date
349 * NB: This implies that cache data can be read when up to
350 * NFS_ATTRTIMEO seconds out of date. If you find that you need current
351 * attributes this could be forced by setting n_attrstamp to 0 before
352 * the VOP_GETATTR() call.
355 nfs_bioread_check_cons(struct vnode *vp, struct thread *td, struct ucred *cred)
359 struct nfsnode *np = VTONFS(vp);
361 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
364 * Grab the exclusive lock before checking whether the cache is
366 * XXX - We can make this cheaper later (by acquiring cheaper locks).
367 * But for now, this suffices.
369 old_lock = nfs_upgrade_vnlock(vp);
370 if (vp->v_iflag & VI_DOOMED) {
371 nfs_downgrade_vnlock(vp, old_lock);
375 mtx_lock(&np->n_mtx);
376 if (np->n_flag & NMODIFIED) {
377 mtx_unlock(&np->n_mtx);
378 if (vp->v_type != VREG) {
379 if (vp->v_type != VDIR)
380 panic("nfs: bioread, not dir");
381 (nmp->nm_rpcops->nr_invaldir)(vp);
382 error = nfs_vinvalbuf(vp, V_SAVE, td, 1);
387 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
388 error = VOP_GETATTR(vp, &vattr, cred);
391 mtx_lock(&np->n_mtx);
392 np->n_mtime = vattr.va_mtime;
393 mtx_unlock(&np->n_mtx);
395 mtx_unlock(&np->n_mtx);
396 error = VOP_GETATTR(vp, &vattr, cred);
399 mtx_lock(&np->n_mtx);
400 if ((np->n_flag & NSIZECHANGED)
401 || (NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime))) {
402 mtx_unlock(&np->n_mtx);
403 if (vp->v_type == VDIR)
404 (nmp->nm_rpcops->nr_invaldir)(vp);
405 error = nfs_vinvalbuf(vp, V_SAVE, td, 1);
408 mtx_lock(&np->n_mtx);
409 np->n_mtime = vattr.va_mtime;
410 np->n_flag &= ~NSIZECHANGED;
412 mtx_unlock(&np->n_mtx);
415 nfs_downgrade_vnlock(vp, old_lock);
420 * Vnode op for read using bio
423 nfs_bioread(struct vnode *vp, struct uio *uio, int ioflag, struct ucred *cred)
425 struct nfsnode *np = VTONFS(vp);
427 struct buf *bp, *rabp;
429 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
434 int nra, error = 0, n = 0, on = 0;
436 KASSERT(uio->uio_rw == UIO_READ, ("nfs_read mode"));
437 if (uio->uio_resid == 0)
439 if (uio->uio_offset < 0) /* XXX VDIR cookies can be negative */
443 mtx_lock(&nmp->nm_mtx);
444 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
445 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) {
446 mtx_unlock(&nmp->nm_mtx);
447 (void)nfs_fsinfo(nmp, vp, cred, td);
449 mtx_unlock(&nmp->nm_mtx);
451 end = uio->uio_offset + uio->uio_resid;
452 if (vp->v_type != VDIR &&
453 (end > nmp->nm_maxfilesize || end < uio->uio_offset))
456 if (nfs_directio_enable && (ioflag & IO_DIRECT) && (vp->v_type == VREG))
457 /* No caching/ no readaheads. Just read data into the user buffer */
458 return nfs_readrpc(vp, uio, cred);
460 biosize = vp->v_bufobj.bo_bsize;
461 seqcount = (int)((off_t)(ioflag >> IO_SEQSHIFT) * biosize / BKVASIZE);
463 error = nfs_bioread_check_cons(vp, td, cred);
470 mtx_lock(&np->n_mtx);
472 mtx_unlock(&np->n_mtx);
474 switch (vp->v_type) {
476 nfsstats.biocache_reads++;
477 lbn = uio->uio_offset / biosize;
478 on = uio->uio_offset - (lbn * biosize);
481 * Start the read ahead(s), as required.
483 if (nmp->nm_readahead > 0) {
484 for (nra = 0; nra < nmp->nm_readahead && nra < seqcount &&
485 (off_t)(lbn + 1 + nra) * biosize < nsize; nra++) {
486 rabn = lbn + 1 + nra;
487 if (incore(&vp->v_bufobj, rabn) == NULL) {
488 rabp = nfs_getcacheblk(vp, rabn, biosize, td);
490 error = nfs_sigintr(nmp, td);
491 return (error ? error : EINTR);
493 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
494 rabp->b_flags |= B_ASYNC;
495 rabp->b_iocmd = BIO_READ;
496 vfs_busy_pages(rabp, 0);
497 if (nfs_asyncio(nmp, rabp, cred, td)) {
498 rabp->b_flags |= B_INVAL;
499 rabp->b_ioflags |= BIO_ERROR;
500 vfs_unbusy_pages(rabp);
511 /* Note that bcount is *not* DEV_BSIZE aligned. */
513 if ((off_t)lbn * biosize >= nsize) {
515 } else if ((off_t)(lbn + 1) * biosize > nsize) {
516 bcount = nsize - (off_t)lbn * biosize;
518 bp = nfs_getcacheblk(vp, lbn, bcount, td);
521 error = nfs_sigintr(nmp, td);
522 return (error ? error : EINTR);
526 * If B_CACHE is not set, we must issue the read. If this
527 * fails, we return an error.
530 if ((bp->b_flags & B_CACHE) == 0) {
531 bp->b_iocmd = BIO_READ;
532 vfs_busy_pages(bp, 0);
533 error = nfs_doio(vp, bp, cred, td);
541 * on is the offset into the current bp. Figure out how many
542 * bytes we can copy out of the bp. Note that bcount is
543 * NOT DEV_BSIZE aligned.
545 * Then figure out how many bytes we can copy into the uio.
550 n = MIN((unsigned)(bcount - on), uio->uio_resid);
553 nfsstats.biocache_readlinks++;
554 bp = nfs_getcacheblk(vp, (daddr_t)0, NFS_MAXPATHLEN, td);
556 error = nfs_sigintr(nmp, td);
557 return (error ? error : EINTR);
559 if ((bp->b_flags & B_CACHE) == 0) {
560 bp->b_iocmd = BIO_READ;
561 vfs_busy_pages(bp, 0);
562 error = nfs_doio(vp, bp, cred, td);
564 bp->b_ioflags |= BIO_ERROR;
569 n = MIN(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid);
573 nfsstats.biocache_readdirs++;
574 if (np->n_direofoffset
575 && uio->uio_offset >= np->n_direofoffset) {
578 lbn = (uoff_t)uio->uio_offset / NFS_DIRBLKSIZ;
579 on = uio->uio_offset & (NFS_DIRBLKSIZ - 1);
580 bp = nfs_getcacheblk(vp, lbn, NFS_DIRBLKSIZ, td);
582 error = nfs_sigintr(nmp, td);
583 return (error ? error : EINTR);
585 if ((bp->b_flags & B_CACHE) == 0) {
586 bp->b_iocmd = BIO_READ;
587 vfs_busy_pages(bp, 0);
588 error = nfs_doio(vp, bp, cred, td);
592 while (error == NFSERR_BAD_COOKIE) {
593 (nmp->nm_rpcops->nr_invaldir)(vp);
594 error = nfs_vinvalbuf(vp, 0, td, 1);
596 * Yuck! The directory has been modified on the
597 * server. The only way to get the block is by
598 * reading from the beginning to get all the
601 * Leave the last bp intact unless there is an error.
602 * Loop back up to the while if the error is another
603 * NFSERR_BAD_COOKIE (double yuch!).
605 for (i = 0; i <= lbn && !error; i++) {
606 if (np->n_direofoffset
607 && (i * NFS_DIRBLKSIZ) >= np->n_direofoffset)
609 bp = nfs_getcacheblk(vp, i, NFS_DIRBLKSIZ, td);
611 error = nfs_sigintr(nmp, td);
612 return (error ? error : EINTR);
614 if ((bp->b_flags & B_CACHE) == 0) {
615 bp->b_iocmd = BIO_READ;
616 vfs_busy_pages(bp, 0);
617 error = nfs_doio(vp, bp, cred, td);
619 * no error + B_INVAL == directory EOF,
622 if (error == 0 && (bp->b_flags & B_INVAL))
626 * An error will throw away the block and the
627 * for loop will break out. If no error and this
628 * is not the block we want, we throw away the
629 * block and go for the next one via the for loop.
631 if (error || i < lbn)
636 * The above while is repeated if we hit another cookie
637 * error. If we hit an error and it wasn't a cookie error,
645 * If not eof and read aheads are enabled, start one.
646 * (You need the current block first, so that you have the
647 * directory offset cookie of the next block.)
649 if (nmp->nm_readahead > 0 &&
650 (bp->b_flags & B_INVAL) == 0 &&
651 (np->n_direofoffset == 0 ||
652 (lbn + 1) * NFS_DIRBLKSIZ < np->n_direofoffset) &&
653 incore(&vp->v_bufobj, lbn + 1) == NULL) {
654 rabp = nfs_getcacheblk(vp, lbn + 1, NFS_DIRBLKSIZ, td);
656 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
657 rabp->b_flags |= B_ASYNC;
658 rabp->b_iocmd = BIO_READ;
659 vfs_busy_pages(rabp, 0);
660 if (nfs_asyncio(nmp, rabp, cred, td)) {
661 rabp->b_flags |= B_INVAL;
662 rabp->b_ioflags |= BIO_ERROR;
663 vfs_unbusy_pages(rabp);
672 * Unlike VREG files, whos buffer size ( bp->b_bcount ) is
673 * chopped for the EOF condition, we cannot tell how large
674 * NFS directories are going to be until we hit EOF. So
675 * an NFS directory buffer is *not* chopped to its EOF. Now,
676 * it just so happens that b_resid will effectively chop it
677 * to EOF. *BUT* this information is lost if the buffer goes
678 * away and is reconstituted into a B_CACHE state ( due to
679 * being VMIO ) later. So we keep track of the directory eof
680 * in np->n_direofoffset and chop it off as an extra step
683 n = lmin(uio->uio_resid, NFS_DIRBLKSIZ - bp->b_resid - on);
684 if (np->n_direofoffset && n > np->n_direofoffset - uio->uio_offset)
685 n = np->n_direofoffset - uio->uio_offset;
688 nfs_printf(" nfs_bioread: type %x unexpected\n", vp->v_type);
694 error = uiomove(bp->b_data + on, (int)n, uio);
696 if (vp->v_type == VLNK)
700 } while (error == 0 && uio->uio_resid > 0 && n > 0);
705 * The NFS write path cannot handle iovecs with len > 1. So we need to
706 * break up iovecs accordingly (restricting them to wsize).
707 * For the SYNC case, we can do this with 1 copy (user buffer -> mbuf).
708 * For the ASYNC case, 2 copies are needed. The first a copy from the
709 * user buffer to a staging buffer and then a second copy from the staging
710 * buffer to mbufs. This can be optimized by copying from the user buffer
711 * directly into mbufs and passing the chain down, but that requires a
712 * fair amount of re-working of the relevant codepaths (and can be done
716 nfs_directio_write(vp, uiop, cred, ioflag)
723 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
724 struct thread *td = uiop->uio_td;
728 mtx_lock(&nmp->nm_mtx);
729 wsize = nmp->nm_wsize;
730 mtx_unlock(&nmp->nm_mtx);
731 if (ioflag & IO_SYNC) {
732 int iomode, must_commit;
736 while (uiop->uio_resid > 0) {
737 size = MIN(uiop->uio_resid, wsize);
738 size = MIN(uiop->uio_iov->iov_len, size);
739 iov.iov_base = uiop->uio_iov->iov_base;
743 uio.uio_offset = uiop->uio_offset;
744 uio.uio_resid = size;
745 uio.uio_segflg = UIO_USERSPACE;
746 uio.uio_rw = UIO_WRITE;
748 iomode = NFSV3WRITE_FILESYNC;
749 error = (nmp->nm_rpcops->nr_writerpc)(vp, &uio, cred,
750 &iomode, &must_commit);
751 KASSERT((must_commit == 0),
752 ("nfs_directio_write: Did not commit write"));
755 uiop->uio_offset += size;
756 uiop->uio_resid -= size;
757 if (uiop->uio_iov->iov_len <= size) {
761 uiop->uio_iov->iov_base =
762 (char *)uiop->uio_iov->iov_base + size;
763 uiop->uio_iov->iov_len -= size;
772 * Break up the write into blocksize chunks and hand these
773 * over to nfsiod's for write back.
774 * Unfortunately, this incurs a copy of the data. Since
775 * the user could modify the buffer before the write is
778 * The obvious optimization here is that one of the 2 copies
779 * in the async write path can be eliminated by copying the
780 * data here directly into mbufs and passing the mbuf chain
781 * down. But that will require a fair amount of re-working
782 * of the code and can be done if there's enough interest
783 * in NFS directio access.
785 while (uiop->uio_resid > 0) {
786 size = MIN(uiop->uio_resid, wsize);
787 size = MIN(uiop->uio_iov->iov_len, size);
788 bp = getpbuf(&nfs_pbuf_freecnt);
789 t_uio = malloc(sizeof(struct uio), M_NFSDIRECTIO, M_WAITOK);
790 t_iov = malloc(sizeof(struct iovec), M_NFSDIRECTIO, M_WAITOK);
791 t_iov->iov_base = malloc(size, M_NFSDIRECTIO, M_WAITOK);
792 t_iov->iov_len = size;
793 t_uio->uio_iov = t_iov;
794 t_uio->uio_iovcnt = 1;
795 t_uio->uio_offset = uiop->uio_offset;
796 t_uio->uio_resid = size;
797 t_uio->uio_segflg = UIO_SYSSPACE;
798 t_uio->uio_rw = UIO_WRITE;
800 KASSERT(uiop->uio_segflg == UIO_USERSPACE ||
801 uiop->uio_segflg == UIO_SYSSPACE,
802 ("nfs_directio_write: Bad uio_segflg"));
803 if (uiop->uio_segflg == UIO_USERSPACE) {
804 error = copyin(uiop->uio_iov->iov_base,
805 t_iov->iov_base, size);
810 * UIO_SYSSPACE may never happen, but handle
811 * it just in case it does.
813 bcopy(uiop->uio_iov->iov_base, t_iov->iov_base,
815 bp->b_flags |= B_DIRECT;
816 bp->b_iocmd = BIO_WRITE;
817 if (cred != NOCRED) {
821 bp->b_wcred = NOCRED;
822 bp->b_caller1 = (void *)t_uio;
824 error = nfs_asyncio(nmp, bp, NOCRED, td);
827 free(t_iov->iov_base, M_NFSDIRECTIO);
828 free(t_iov, M_NFSDIRECTIO);
829 free(t_uio, M_NFSDIRECTIO);
831 relpbuf(bp, &nfs_pbuf_freecnt);
836 uiop->uio_offset += size;
837 uiop->uio_resid -= size;
838 if (uiop->uio_iov->iov_len <= size) {
842 uiop->uio_iov->iov_base =
843 (char *)uiop->uio_iov->iov_base + size;
844 uiop->uio_iov->iov_len -= size;
852 * Vnode op for write using bio
855 nfs_write(struct vop_write_args *ap)
858 struct uio *uio = ap->a_uio;
859 struct thread *td = uio->uio_td;
860 struct vnode *vp = ap->a_vp;
861 struct nfsnode *np = VTONFS(vp);
862 struct ucred *cred = ap->a_cred;
863 int ioflag = ap->a_ioflag;
866 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
870 int n, on, error = 0;
872 KASSERT(uio->uio_rw == UIO_WRITE, ("nfs_write mode"));
873 KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
875 if (vp->v_type != VREG)
877 mtx_lock(&np->n_mtx);
878 if (np->n_flag & NWRITEERR) {
879 np->n_flag &= ~NWRITEERR;
880 mtx_unlock(&np->n_mtx);
881 return (np->n_error);
883 mtx_unlock(&np->n_mtx);
884 mtx_lock(&nmp->nm_mtx);
885 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
886 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) {
887 mtx_unlock(&nmp->nm_mtx);
888 (void)nfs_fsinfo(nmp, vp, cred, td);
890 mtx_unlock(&nmp->nm_mtx);
893 * Synchronously flush pending buffers if we are in synchronous
894 * mode or if we are appending.
896 if (ioflag & (IO_APPEND | IO_SYNC)) {
897 mtx_lock(&np->n_mtx);
898 if (np->n_flag & NMODIFIED) {
899 mtx_unlock(&np->n_mtx);
900 #ifdef notyet /* Needs matching nonblock semantics elsewhere, too. */
902 * Require non-blocking, synchronous writes to
903 * dirty files to inform the program it needs
904 * to fsync(2) explicitly.
906 if (ioflag & IO_NDELAY)
911 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
912 error = nfs_vinvalbuf(vp, V_SAVE, td, 1);
916 mtx_unlock(&np->n_mtx);
920 * If IO_APPEND then load uio_offset. We restart here if we cannot
921 * get the append lock.
923 if (ioflag & IO_APPEND) {
925 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
926 error = VOP_GETATTR(vp, &vattr, cred);
929 mtx_lock(&np->n_mtx);
930 uio->uio_offset = np->n_size;
931 mtx_unlock(&np->n_mtx);
934 if (uio->uio_offset < 0)
936 end = uio->uio_offset + uio->uio_resid;
937 if (end > nmp->nm_maxfilesize || end < uio->uio_offset)
939 if (uio->uio_resid == 0)
942 if (nfs_directio_enable && (ioflag & IO_DIRECT) && vp->v_type == VREG)
943 return nfs_directio_write(vp, uio, cred, ioflag);
946 * Maybe this should be above the vnode op call, but so long as
947 * file servers have no limits, i don't think it matters
949 if (vn_rlimit_fsize(vp, uio, td))
952 biosize = vp->v_bufobj.bo_bsize;
954 * Find all of this file's B_NEEDCOMMIT buffers. If our writes
955 * would exceed the local maximum per-file write commit size when
956 * combined with those, we must decide whether to flush,
957 * go synchronous, or return error. We don't bother checking
958 * IO_UNIT -- we just make all writes atomic anyway, as there's
959 * no point optimizing for something that really won't ever happen.
961 if (!(ioflag & IO_SYNC)) {
964 mtx_lock(&np->n_mtx);
966 mtx_unlock(&np->n_mtx);
968 if (nmp->nm_wcommitsize < uio->uio_resid) {
970 * If this request could not possibly be completed
971 * without exceeding the maximum outstanding write
972 * commit size, see if we can convert it into a
973 * synchronous write operation.
975 if (ioflag & IO_NDELAY)
978 if (nflag & NMODIFIED)
980 } else if (nflag & NMODIFIED) {
982 BO_LOCK(&vp->v_bufobj);
983 if (vp->v_bufobj.bo_dirty.bv_cnt != 0) {
984 TAILQ_FOREACH(bp, &vp->v_bufobj.bo_dirty.bv_hd,
986 if (bp->b_flags & B_NEEDCOMMIT)
987 wouldcommit += bp->b_bcount;
990 BO_UNLOCK(&vp->v_bufobj);
992 * Since we're not operating synchronously and
993 * bypassing the buffer cache, we are in a commit
994 * and holding all of these buffers whether
995 * transmitted or not. If not limited, this
996 * will lead to the buffer cache deadlocking,
997 * as no one else can flush our uncommitted buffers.
999 wouldcommit += uio->uio_resid;
1001 * If we would initially exceed the maximum
1002 * outstanding write commit size, flush and restart.
1004 if (wouldcommit > nmp->nm_wcommitsize)
1008 goto flush_and_restart;
1012 nfsstats.biocache_writes++;
1013 lbn = uio->uio_offset / biosize;
1014 on = uio->uio_offset - (lbn * biosize);
1015 n = MIN((unsigned)(biosize - on), uio->uio_resid);
1018 * Handle direct append and file extension cases, calculate
1019 * unaligned buffer size.
1021 mtx_lock(&np->n_mtx);
1022 if (uio->uio_offset == np->n_size && n) {
1023 mtx_unlock(&np->n_mtx);
1025 * Get the buffer (in its pre-append state to maintain
1026 * B_CACHE if it was previously set). Resize the
1027 * nfsnode after we have locked the buffer to prevent
1028 * readers from reading garbage.
1031 bp = nfs_getcacheblk(vp, lbn, bcount, td);
1036 mtx_lock(&np->n_mtx);
1037 np->n_size = uio->uio_offset + n;
1038 np->n_flag |= NMODIFIED;
1039 vnode_pager_setsize(vp, np->n_size);
1040 mtx_unlock(&np->n_mtx);
1042 save = bp->b_flags & B_CACHE;
1044 allocbuf(bp, bcount);
1045 bp->b_flags |= save;
1049 * Obtain the locked cache block first, and then
1050 * adjust the file's size as appropriate.
1053 if ((off_t)lbn * biosize + bcount < np->n_size) {
1054 if ((off_t)(lbn + 1) * biosize < np->n_size)
1057 bcount = np->n_size - (off_t)lbn * biosize;
1059 mtx_unlock(&np->n_mtx);
1060 bp = nfs_getcacheblk(vp, lbn, bcount, td);
1061 mtx_lock(&np->n_mtx);
1062 if (uio->uio_offset + n > np->n_size) {
1063 np->n_size = uio->uio_offset + n;
1064 np->n_flag |= NMODIFIED;
1065 vnode_pager_setsize(vp, np->n_size);
1067 mtx_unlock(&np->n_mtx);
1071 error = nfs_sigintr(nmp, td);
1078 * Issue a READ if B_CACHE is not set. In special-append
1079 * mode, B_CACHE is based on the buffer prior to the write
1080 * op and is typically set, avoiding the read. If a read
1081 * is required in special append mode, the server will
1082 * probably send us a short-read since we extended the file
1083 * on our end, resulting in b_resid == 0 and, thusly,
1084 * B_CACHE getting set.
1086 * We can also avoid issuing the read if the write covers
1087 * the entire buffer. We have to make sure the buffer state
1088 * is reasonable in this case since we will not be initiating
1089 * I/O. See the comments in kern/vfs_bio.c's getblk() for
1092 * B_CACHE may also be set due to the buffer being cached
1096 if (on == 0 && n == bcount) {
1097 bp->b_flags |= B_CACHE;
1098 bp->b_flags &= ~B_INVAL;
1099 bp->b_ioflags &= ~BIO_ERROR;
1102 if ((bp->b_flags & B_CACHE) == 0) {
1103 bp->b_iocmd = BIO_READ;
1104 vfs_busy_pages(bp, 0);
1105 error = nfs_doio(vp, bp, cred, td);
1111 if (bp->b_wcred == NOCRED)
1112 bp->b_wcred = crhold(cred);
1113 mtx_lock(&np->n_mtx);
1114 np->n_flag |= NMODIFIED;
1115 mtx_unlock(&np->n_mtx);
1118 * If dirtyend exceeds file size, chop it down. This should
1119 * not normally occur but there is an append race where it
1120 * might occur XXX, so we log it.
1122 * If the chopping creates a reverse-indexed or degenerate
1123 * situation with dirtyoff/end, we 0 both of them.
1126 if (bp->b_dirtyend > bcount) {
1127 nfs_printf("NFS append race @%lx:%d\n",
1128 (long)bp->b_blkno * DEV_BSIZE,
1129 bp->b_dirtyend - bcount);
1130 bp->b_dirtyend = bcount;
1133 if (bp->b_dirtyoff >= bp->b_dirtyend)
1134 bp->b_dirtyoff = bp->b_dirtyend = 0;
1137 * If the new write will leave a contiguous dirty
1138 * area, just update the b_dirtyoff and b_dirtyend,
1139 * otherwise force a write rpc of the old dirty area.
1141 * While it is possible to merge discontiguous writes due to
1142 * our having a B_CACHE buffer ( and thus valid read data
1143 * for the hole), we don't because it could lead to
1144 * significant cache coherency problems with multiple clients,
1145 * especially if locking is implemented later on.
1147 * as an optimization we could theoretically maintain
1148 * a linked list of discontinuous areas, but we would still
1149 * have to commit them separately so there isn't much
1150 * advantage to it except perhaps a bit of asynchronization.
1153 if (bp->b_dirtyend > 0 &&
1154 (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) {
1155 if (bwrite(bp) == EINTR) {
1162 error = uiomove((char *)bp->b_data + on, n, uio);
1165 * Since this block is being modified, it must be written
1166 * again and not just committed. Since write clustering does
1167 * not work for the stage 1 data write, only the stage 2
1168 * commit rpc, we have to clear B_CLUSTEROK as well.
1170 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1173 bp->b_ioflags |= BIO_ERROR;
1179 * Only update dirtyoff/dirtyend if not a degenerate
1183 if (bp->b_dirtyend > 0) {
1184 bp->b_dirtyoff = min(on, bp->b_dirtyoff);
1185 bp->b_dirtyend = max((on + n), bp->b_dirtyend);
1187 bp->b_dirtyoff = on;
1188 bp->b_dirtyend = on + n;
1190 vfs_bio_set_valid(bp, on, n);
1194 * If IO_SYNC do bwrite().
1196 * IO_INVAL appears to be unused. The idea appears to be
1197 * to turn off caching in this case. Very odd. XXX
1199 if ((ioflag & IO_SYNC)) {
1200 if (ioflag & IO_INVAL)
1201 bp->b_flags |= B_NOCACHE;
1205 } else if ((n + on) == biosize) {
1206 bp->b_flags |= B_ASYNC;
1207 (void) (nmp->nm_rpcops->nr_writebp)(bp, 0, NULL);
1211 } while (uio->uio_resid > 0 && n > 0);
1217 * Get an nfs cache block.
1219 * Allocate a new one if the block isn't currently in the cache
1220 * and return the block marked busy. If the calling process is
1221 * interrupted by a signal for an interruptible mount point, return
1224 * The caller must carefully deal with the possible B_INVAL state of
1225 * the buffer. nfs_doio() clears B_INVAL (and nfs_asyncio() clears it
1226 * indirectly), so synchronous reads can be issued without worrying about
1227 * the B_INVAL state. We have to be a little more careful when dealing
1228 * with writes (see comments in nfs_write()) when extending a file past
1232 nfs_getcacheblk(struct vnode *vp, daddr_t bn, int size, struct thread *td)
1236 struct nfsmount *nmp;
1241 if (nmp->nm_flag & NFSMNT_INT) {
1244 nfs_set_sigmask(td, &oldset);
1245 bp = getblk(vp, bn, size, PCATCH, 0, 0);
1246 nfs_restore_sigmask(td, &oldset);
1247 while (bp == NULL) {
1248 if (nfs_sigintr(nmp, td))
1250 bp = getblk(vp, bn, size, 0, 2 * hz, 0);
1253 bp = getblk(vp, bn, size, 0, 0, 0);
1256 if (vp->v_type == VREG)
1257 bp->b_blkno = bn * (vp->v_bufobj.bo_bsize / DEV_BSIZE);
1262 * Flush and invalidate all dirty buffers. If another process is already
1263 * doing the flush, just wait for completion.
1266 nfs_vinvalbuf(struct vnode *vp, int flags, struct thread *td, int intrflg)
1268 struct nfsnode *np = VTONFS(vp);
1269 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1270 int error = 0, slpflag, slptimeo;
1273 ASSERT_VOP_LOCKED(vp, "nfs_vinvalbuf");
1275 if ((nmp->nm_flag & NFSMNT_INT) == 0)
1285 old_lock = nfs_upgrade_vnlock(vp);
1286 if (vp->v_iflag & VI_DOOMED) {
1288 * Since vgonel() uses the generic vinvalbuf() to flush
1289 * dirty buffers and it does not call this function, it
1290 * is safe to just return OK when VI_DOOMED is set.
1292 nfs_downgrade_vnlock(vp, old_lock);
1297 * Now, flush as required.
1299 if ((flags & V_SAVE) && (vp->v_bufobj.bo_object != NULL)) {
1300 VM_OBJECT_WLOCK(vp->v_bufobj.bo_object);
1301 vm_object_page_clean(vp->v_bufobj.bo_object, 0, 0, OBJPC_SYNC);
1302 VM_OBJECT_WUNLOCK(vp->v_bufobj.bo_object);
1304 * If the page clean was interrupted, fail the invalidation.
1305 * Not doing so, we run the risk of losing dirty pages in the
1306 * vinvalbuf() call below.
1308 if (intrflg && (error = nfs_sigintr(nmp, td)))
1312 error = vinvalbuf(vp, flags, slpflag, 0);
1314 if (intrflg && (error = nfs_sigintr(nmp, td)))
1316 error = vinvalbuf(vp, flags, 0, slptimeo);
1318 mtx_lock(&np->n_mtx);
1319 if (np->n_directio_asyncwr == 0)
1320 np->n_flag &= ~NMODIFIED;
1321 mtx_unlock(&np->n_mtx);
1323 nfs_downgrade_vnlock(vp, old_lock);
1328 * Initiate asynchronous I/O. Return an error if no nfsiods are available.
1329 * This is mainly to avoid queueing async I/O requests when the nfsiods
1330 * are all hung on a dead server.
1332 * Note: nfs_asyncio() does not clear (BIO_ERROR|B_INVAL) but when the bp
1333 * is eventually dequeued by the async daemon, nfs_doio() *will*.
1336 nfs_asyncio(struct nfsmount *nmp, struct buf *bp, struct ucred *cred, struct thread *td)
1345 * Commits are usually short and sweet so lets save some cpu and
1346 * leave the async daemons for more important rpc's (such as reads
1349 * Readdirplus RPCs do vget()s to acquire the vnodes for entries
1350 * in the directory in order to update attributes. This can deadlock
1351 * with another thread that is waiting for async I/O to be done by
1352 * an nfsiod thread while holding a lock on one of these vnodes.
1353 * To avoid this deadlock, don't allow the async nfsiod threads to
1354 * perform Readdirplus RPCs.
1356 mtx_lock(&nfs_iod_mtx);
1357 if ((bp->b_iocmd == BIO_WRITE && (bp->b_flags & B_NEEDCOMMIT) &&
1358 (nmp->nm_bufqiods > nfs_numasync / 2)) ||
1359 (bp->b_vp->v_type == VDIR && (nmp->nm_flag & NFSMNT_RDIRPLUS))) {
1360 mtx_unlock(&nfs_iod_mtx);
1364 if (nmp->nm_flag & NFSMNT_INT)
1369 * Find a free iod to process this request.
1371 for (iod = 0; iod < nfs_numasync; iod++)
1372 if (nfs_iodwant[iod] == NFSIOD_AVAILABLE) {
1378 * Try to create one if none are free.
1384 * Found one, so wake it up and tell it which
1387 NFS_DPF(ASYNCIO, ("nfs_asyncio: waking iod %d for mount %p\n",
1389 nfs_iodwant[iod] = NFSIOD_NOT_AVAILABLE;
1390 nfs_iodmount[iod] = nmp;
1392 wakeup(&nfs_iodwant[iod]);
1396 * If none are free, we may already have an iod working on this mount
1397 * point. If so, it will process our request.
1400 if (nmp->nm_bufqiods > 0) {
1402 ("nfs_asyncio: %d iods are already processing mount %p\n",
1403 nmp->nm_bufqiods, nmp));
1409 * If we have an iod which can process the request, then queue
1414 * Ensure that the queue never grows too large. We still want
1415 * to asynchronize so we block rather then return EIO.
1417 while (nmp->nm_bufqlen >= 2 * nfs_numasync) {
1419 ("nfs_asyncio: waiting for mount %p queue to drain\n", nmp));
1420 nmp->nm_bufqwant = TRUE;
1421 error = nfs_msleep(td, &nmp->nm_bufq, &nfs_iod_mtx,
1423 "nfsaio", slptimeo);
1425 error2 = nfs_sigintr(nmp, td);
1427 mtx_unlock(&nfs_iod_mtx);
1430 if (slpflag == PCATCH) {
1436 * We might have lost our iod while sleeping,
1437 * so check and loop if nescessary.
1442 /* We might have lost our nfsiod */
1443 if (nmp->nm_bufqiods == 0) {
1445 ("nfs_asyncio: no iods after mount %p queue was drained, looping\n", nmp));
1449 if (bp->b_iocmd == BIO_READ) {
1450 if (bp->b_rcred == NOCRED && cred != NOCRED)
1451 bp->b_rcred = crhold(cred);
1453 if (bp->b_wcred == NOCRED && cred != NOCRED)
1454 bp->b_wcred = crhold(cred);
1457 if (bp->b_flags & B_REMFREE)
1460 TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist);
1462 if ((bp->b_flags & B_DIRECT) && bp->b_iocmd == BIO_WRITE) {
1463 mtx_lock(&(VTONFS(bp->b_vp))->n_mtx);
1464 VTONFS(bp->b_vp)->n_flag |= NMODIFIED;
1465 VTONFS(bp->b_vp)->n_directio_asyncwr++;
1466 mtx_unlock(&(VTONFS(bp->b_vp))->n_mtx);
1468 mtx_unlock(&nfs_iod_mtx);
1472 mtx_unlock(&nfs_iod_mtx);
1475 * All the iods are busy on other mounts, so return EIO to
1476 * force the caller to process the i/o synchronously.
1478 NFS_DPF(ASYNCIO, ("nfs_asyncio: no iods available, i/o is synchronous\n"));
1483 nfs_doio_directwrite(struct buf *bp)
1485 int iomode, must_commit;
1486 struct uio *uiop = (struct uio *)bp->b_caller1;
1487 char *iov_base = uiop->uio_iov->iov_base;
1488 struct nfsmount *nmp = VFSTONFS(bp->b_vp->v_mount);
1490 iomode = NFSV3WRITE_FILESYNC;
1491 uiop->uio_td = NULL; /* NULL since we're in nfsiod */
1492 (nmp->nm_rpcops->nr_writerpc)(bp->b_vp, uiop, bp->b_wcred, &iomode, &must_commit);
1493 KASSERT((must_commit == 0), ("nfs_doio_directwrite: Did not commit write"));
1494 free(iov_base, M_NFSDIRECTIO);
1495 free(uiop->uio_iov, M_NFSDIRECTIO);
1496 free(uiop, M_NFSDIRECTIO);
1497 if ((bp->b_flags & B_DIRECT) && bp->b_iocmd == BIO_WRITE) {
1498 struct nfsnode *np = VTONFS(bp->b_vp);
1499 mtx_lock(&np->n_mtx);
1500 np->n_directio_asyncwr--;
1501 if (np->n_directio_asyncwr == 0) {
1502 VTONFS(bp->b_vp)->n_flag &= ~NMODIFIED;
1503 if ((np->n_flag & NFSYNCWAIT)) {
1504 np->n_flag &= ~NFSYNCWAIT;
1505 wakeup((caddr_t)&np->n_directio_asyncwr);
1508 mtx_unlock(&np->n_mtx);
1511 relpbuf(bp, &nfs_pbuf_freecnt);
1515 * Do an I/O operation to/from a cache block. This may be called
1516 * synchronously or from an nfsiod.
1519 nfs_doio(struct vnode *vp, struct buf *bp, struct ucred *cr, struct thread *td)
1523 struct nfsmount *nmp;
1524 int error = 0, iomode, must_commit = 0;
1527 struct proc *p = td ? td->td_proc : NULL;
1531 nmp = VFSTONFS(vp->v_mount);
1533 uiop->uio_iov = &io;
1534 uiop->uio_iovcnt = 1;
1535 uiop->uio_segflg = UIO_SYSSPACE;
1539 * clear BIO_ERROR and B_INVAL state prior to initiating the I/O. We
1540 * do this here so we do not have to do it in all the code that
1543 bp->b_flags &= ~B_INVAL;
1544 bp->b_ioflags &= ~BIO_ERROR;
1546 KASSERT(!(bp->b_flags & B_DONE), ("nfs_doio: bp %p already marked done", bp));
1547 iocmd = bp->b_iocmd;
1548 if (iocmd == BIO_READ) {
1549 io.iov_len = uiop->uio_resid = bp->b_bcount;
1550 io.iov_base = bp->b_data;
1551 uiop->uio_rw = UIO_READ;
1553 switch (vp->v_type) {
1555 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
1556 nfsstats.read_bios++;
1557 error = (nmp->nm_rpcops->nr_readrpc)(vp, uiop, cr);
1560 if (uiop->uio_resid) {
1562 * If we had a short read with no error, we must have
1563 * hit a file hole. We should zero-fill the remainder.
1564 * This can also occur if the server hits the file EOF.
1566 * Holes used to be able to occur due to pending
1567 * writes, but that is not possible any longer.
1569 int nread = bp->b_bcount - uiop->uio_resid;
1570 int left = uiop->uio_resid;
1573 bzero((char *)bp->b_data + nread, left);
1574 uiop->uio_resid = 0;
1577 /* ASSERT_VOP_LOCKED(vp, "nfs_doio"); */
1578 if (p && (vp->v_vflag & VV_TEXT)) {
1579 mtx_lock(&np->n_mtx);
1580 if (NFS_TIMESPEC_COMPARE(&np->n_mtime, &np->n_vattr.va_mtime)) {
1581 mtx_unlock(&np->n_mtx);
1583 killproc(p, "text file modification");
1586 mtx_unlock(&np->n_mtx);
1590 uiop->uio_offset = (off_t)0;
1591 nfsstats.readlink_bios++;
1592 error = (nmp->nm_rpcops->nr_readlinkrpc)(vp, uiop, cr);
1595 nfsstats.readdir_bios++;
1596 uiop->uio_offset = ((u_quad_t)bp->b_lblkno) * NFS_DIRBLKSIZ;
1597 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) != 0) {
1598 error = nfs_readdirplusrpc(vp, uiop, cr);
1599 if (error == NFSERR_NOTSUPP)
1600 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
1602 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
1603 error = nfs_readdirrpc(vp, uiop, cr);
1605 * end-of-directory sets B_INVAL but does not generate an
1608 if (error == 0 && uiop->uio_resid == bp->b_bcount)
1609 bp->b_flags |= B_INVAL;
1612 nfs_printf("nfs_doio: type %x unexpected\n", vp->v_type);
1616 bp->b_ioflags |= BIO_ERROR;
1617 bp->b_error = error;
1621 * If we only need to commit, try to commit
1623 if (bp->b_flags & B_NEEDCOMMIT) {
1627 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE + bp->b_dirtyoff;
1628 retv = (nmp->nm_rpcops->nr_commit)(
1629 vp, off, bp->b_dirtyend-bp->b_dirtyoff,
1632 bp->b_dirtyoff = bp->b_dirtyend = 0;
1633 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1638 if (retv == NFSERR_STALEWRITEVERF) {
1639 nfs_clearcommit(vp->v_mount);
1644 * Setup for actual write
1646 mtx_lock(&np->n_mtx);
1647 if ((off_t)bp->b_blkno * DEV_BSIZE + bp->b_dirtyend > np->n_size)
1648 bp->b_dirtyend = np->n_size - (off_t)bp->b_blkno * DEV_BSIZE;
1649 mtx_unlock(&np->n_mtx);
1651 if (bp->b_dirtyend > bp->b_dirtyoff) {
1652 io.iov_len = uiop->uio_resid = bp->b_dirtyend
1654 uiop->uio_offset = (off_t)bp->b_blkno * DEV_BSIZE
1656 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
1657 uiop->uio_rw = UIO_WRITE;
1658 nfsstats.write_bios++;
1660 if ((bp->b_flags & (B_ASYNC | B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == B_ASYNC)
1661 iomode = NFSV3WRITE_UNSTABLE;
1663 iomode = NFSV3WRITE_FILESYNC;
1665 error = (nmp->nm_rpcops->nr_writerpc)(vp, uiop, cr, &iomode, &must_commit);
1668 * When setting B_NEEDCOMMIT also set B_CLUSTEROK to try
1669 * to cluster the buffers needing commit. This will allow
1670 * the system to submit a single commit rpc for the whole
1671 * cluster. We can do this even if the buffer is not 100%
1672 * dirty (relative to the NFS blocksize), so we optimize the
1673 * append-to-file-case.
1675 * (when clearing B_NEEDCOMMIT, B_CLUSTEROK must also be
1676 * cleared because write clustering only works for commit
1677 * rpc's, not for the data portion of the write).
1680 if (!error && iomode == NFSV3WRITE_UNSTABLE) {
1681 bp->b_flags |= B_NEEDCOMMIT;
1682 if (bp->b_dirtyoff == 0
1683 && bp->b_dirtyend == bp->b_bcount)
1684 bp->b_flags |= B_CLUSTEROK;
1686 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1690 * For an interrupted write, the buffer is still valid
1691 * and the write hasn't been pushed to the server yet,
1692 * so we can't set BIO_ERROR and report the interruption
1693 * by setting B_EINTR. For the B_ASYNC case, B_EINTR
1694 * is not relevant, so the rpc attempt is essentially
1695 * a noop. For the case of a V3 write rpc not being
1696 * committed to stable storage, the block is still
1697 * dirty and requires either a commit rpc or another
1698 * write rpc with iomode == NFSV3WRITE_FILESYNC before
1699 * the block is reused. This is indicated by setting
1700 * the B_DELWRI and B_NEEDCOMMIT flags.
1702 * If the buffer is marked B_PAGING, it does not reside on
1703 * the vp's paging queues so we cannot call bdirty(). The
1704 * bp in this case is not an NFS cache block so we should
1707 * The logic below breaks up errors into recoverable and
1708 * unrecoverable. For the former, we clear B_INVAL|B_NOCACHE
1709 * and keep the buffer around for potential write retries.
1710 * For the latter (eg ESTALE), we toss the buffer away (B_INVAL)
1711 * and save the error in the nfsnode. This is less than ideal
1712 * but necessary. Keeping such buffers around could potentially
1713 * cause buffer exhaustion eventually (they can never be written
1714 * out, so will get constantly be re-dirtied). It also causes
1715 * all sorts of vfs panics. For non-recoverable write errors,
1716 * also invalidate the attrcache, so we'll be forced to go over
1717 * the wire for this object, returning an error to user on next
1718 * call (most of the time).
1720 if (error == EINTR || error == EIO || error == ETIMEDOUT
1721 || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
1725 bp->b_flags &= ~(B_INVAL|B_NOCACHE);
1726 if ((bp->b_flags & B_PAGING) == 0) {
1728 bp->b_flags &= ~B_DONE;
1730 if (error && (bp->b_flags & B_ASYNC) == 0)
1731 bp->b_flags |= B_EINTR;
1735 bp->b_ioflags |= BIO_ERROR;
1736 bp->b_flags |= B_INVAL;
1737 bp->b_error = np->n_error = error;
1738 mtx_lock(&np->n_mtx);
1739 np->n_flag |= NWRITEERR;
1740 np->n_attrstamp = 0;
1741 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
1742 mtx_unlock(&np->n_mtx);
1744 bp->b_dirtyoff = bp->b_dirtyend = 0;
1752 bp->b_resid = uiop->uio_resid;
1754 nfs_clearcommit(vp->v_mount);
1760 * Used to aid in handling ftruncate() operations on the NFS client side.
1761 * Truncation creates a number of special problems for NFS. We have to
1762 * throw away VM pages and buffer cache buffers that are beyond EOF, and
1763 * we have to properly handle VM pages or (potentially dirty) buffers
1764 * that straddle the truncation point.
1768 nfs_meta_setsize(struct vnode *vp, struct ucred *cred, struct thread *td, u_quad_t nsize)
1770 struct nfsnode *np = VTONFS(vp);
1772 int biosize = vp->v_bufobj.bo_bsize;
1775 mtx_lock(&np->n_mtx);
1778 mtx_unlock(&np->n_mtx);
1780 if (nsize < tsize) {
1786 * vtruncbuf() doesn't get the buffer overlapping the
1787 * truncation point. We may have a B_DELWRI and/or B_CACHE
1788 * buffer that now needs to be truncated.
1790 error = vtruncbuf(vp, cred, nsize, biosize);
1791 lbn = nsize / biosize;
1792 bufsize = nsize - (lbn * biosize);
1793 bp = nfs_getcacheblk(vp, lbn, bufsize, td);
1796 if (bp->b_dirtyoff > bp->b_bcount)
1797 bp->b_dirtyoff = bp->b_bcount;
1798 if (bp->b_dirtyend > bp->b_bcount)
1799 bp->b_dirtyend = bp->b_bcount;
1800 bp->b_flags |= B_RELBUF; /* don't leave garbage around */
1803 vnode_pager_setsize(vp, nsize);