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/vmmeter.h>
49 #include <sys/vnode.h>
52 #include <vm/vm_extern.h>
53 #include <vm/vm_page.h>
54 #include <vm/vm_object.h>
55 #include <vm/vm_pager.h>
56 #include <vm/vnode_pager.h>
58 #include <nfs/nfsproto.h>
59 #include <nfsclient/nfs.h>
60 #include <nfsclient/nfsmount.h>
61 #include <nfsclient/nfsnode.h>
62 #include <nfsclient/nfs_kdtrace.h>
64 static struct buf *nfs_getcacheblk(struct vnode *vp, daddr_t bn, int size,
66 static int nfs_directio_write(struct vnode *vp, struct uio *uiop,
67 struct ucred *cred, int ioflag);
69 extern int nfs_directio_enable;
70 extern int nfs_directio_allow_mmap;
73 * Vnode op for VM getpages.
76 nfs_getpages(struct vop_getpages_args *ap)
78 int i, error, nextoff, size, toff, count, npages;
93 td = curthread; /* XXX */
94 cred = curthread->td_ucred; /* XXX */
95 nmp = VFSTONFS(vp->v_mount);
99 if ((object = vp->v_object) == NULL) {
100 nfs_printf("nfs_getpages: called with non-merged cache vnode??\n");
101 return (VM_PAGER_ERROR);
104 if (nfs_directio_enable && !nfs_directio_allow_mmap) {
105 mtx_lock(&np->n_mtx);
106 if ((np->n_flag & NNONCACHE) && (vp->v_type == VREG)) {
107 mtx_unlock(&np->n_mtx);
108 nfs_printf("nfs_getpages: called on non-cacheable vnode??\n");
109 return (VM_PAGER_ERROR);
111 mtx_unlock(&np->n_mtx);
114 mtx_lock(&nmp->nm_mtx);
115 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
116 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) {
117 mtx_unlock(&nmp->nm_mtx);
118 /* We'll never get here for v4, because we always have fsinfo */
119 (void)nfs_fsinfo(nmp, vp, cred, td);
121 mtx_unlock(&nmp->nm_mtx);
123 npages = btoc(count);
126 * If the requested page is partially valid, just return it and
127 * allow the pager to zero-out the blanks. Partially valid pages
128 * can only occur at the file EOF.
130 VM_OBJECT_LOCK(object);
131 if (pages[ap->a_reqpage]->valid != 0) {
132 vm_page_lock_queues();
133 for (i = 0; i < npages; ++i) {
134 if (i != ap->a_reqpage)
135 vm_page_free(pages[i]);
137 vm_page_unlock_queues();
138 VM_OBJECT_UNLOCK(object);
141 VM_OBJECT_UNLOCK(object);
144 * We use only the kva address for the buffer, but this is extremely
145 * convienient and fast.
147 bp = getpbuf(&nfs_pbuf_freecnt);
149 kva = (vm_offset_t) bp->b_data;
150 pmap_qenter(kva, pages, npages);
151 PCPU_INC(cnt.v_vnodein);
152 PCPU_ADD(cnt.v_vnodepgsin, npages);
154 iov.iov_base = (caddr_t) kva;
158 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
159 uio.uio_resid = count;
160 uio.uio_segflg = UIO_SYSSPACE;
161 uio.uio_rw = UIO_READ;
164 error = (nmp->nm_rpcops->nr_readrpc)(vp, &uio, cred);
165 pmap_qremove(kva, npages);
167 relpbuf(bp, &nfs_pbuf_freecnt);
169 if (error && (uio.uio_resid == count)) {
170 nfs_printf("nfs_getpages: error %d\n", error);
171 VM_OBJECT_LOCK(object);
172 vm_page_lock_queues();
173 for (i = 0; i < npages; ++i) {
174 if (i != ap->a_reqpage)
175 vm_page_free(pages[i]);
177 vm_page_unlock_queues();
178 VM_OBJECT_UNLOCK(object);
179 return (VM_PAGER_ERROR);
183 * Calculate the number of bytes read and validate only that number
184 * of bytes. Note that due to pending writes, size may be 0. This
185 * does not mean that the remaining data is invalid!
188 size = count - uio.uio_resid;
189 VM_OBJECT_LOCK(object);
190 vm_page_lock_queues();
191 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
193 nextoff = toff + PAGE_SIZE;
196 if (nextoff <= size) {
198 * Read operation filled an entire page
200 m->valid = VM_PAGE_BITS_ALL;
201 KASSERT(m->dirty == 0,
202 ("nfs_getpages: page %p is dirty", m));
203 } else if (size > toff) {
205 * Read operation filled a partial page.
208 vm_page_set_valid(m, 0, size - toff);
209 KASSERT(m->dirty == 0,
210 ("nfs_getpages: page %p is dirty", m));
213 * Read operation was short. If no error occured
214 * we may have hit a zero-fill section. We simply
215 * leave valid set to 0.
219 if (i != ap->a_reqpage) {
221 * Whether or not to leave the page activated is up in
222 * the air, but we should put the page on a page queue
223 * somewhere (it already is in the object). Result:
224 * It appears that emperical results show that
225 * deactivating pages is best.
229 * Just in case someone was asking for this page we
230 * now tell them that it is ok to use.
233 if (m->oflags & VPO_WANTED)
236 vm_page_deactivate(m);
243 vm_page_unlock_queues();
244 VM_OBJECT_UNLOCK(object);
249 * Vnode op for VM putpages.
252 nfs_putpages(struct vop_putpages_args *ap)
258 int iomode, must_commit, i, error, npages, count;
264 struct nfsmount *nmp;
270 td = curthread; /* XXX */
271 cred = curthread->td_ucred; /* XXX */
272 nmp = VFSTONFS(vp->v_mount);
275 rtvals = ap->a_rtvals;
276 npages = btoc(count);
277 offset = IDX_TO_OFF(pages[0]->pindex);
279 mtx_lock(&nmp->nm_mtx);
280 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
281 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) {
282 mtx_unlock(&nmp->nm_mtx);
283 (void)nfs_fsinfo(nmp, vp, cred, td);
285 mtx_unlock(&nmp->nm_mtx);
287 mtx_lock(&np->n_mtx);
288 if (nfs_directio_enable && !nfs_directio_allow_mmap &&
289 (np->n_flag & NNONCACHE) && (vp->v_type == VREG)) {
290 mtx_unlock(&np->n_mtx);
291 nfs_printf("nfs_putpages: called on noncache-able vnode??\n");
292 mtx_lock(&np->n_mtx);
295 for (i = 0; i < npages; i++)
296 rtvals[i] = VM_PAGER_ERROR;
299 * When putting pages, do not extend file past EOF.
301 if (offset + count > np->n_size) {
302 count = np->n_size - offset;
306 mtx_unlock(&np->n_mtx);
309 * We use only the kva address for the buffer, but this is extremely
310 * convienient and fast.
312 bp = getpbuf(&nfs_pbuf_freecnt);
314 kva = (vm_offset_t) bp->b_data;
315 pmap_qenter(kva, pages, npages);
316 PCPU_INC(cnt.v_vnodeout);
317 PCPU_ADD(cnt.v_vnodepgsout, count);
319 iov.iov_base = (caddr_t) kva;
323 uio.uio_offset = offset;
324 uio.uio_resid = count;
325 uio.uio_segflg = UIO_SYSSPACE;
326 uio.uio_rw = UIO_WRITE;
329 if ((ap->a_sync & VM_PAGER_PUT_SYNC) == 0)
330 iomode = NFSV3WRITE_UNSTABLE;
332 iomode = NFSV3WRITE_FILESYNC;
334 error = (nmp->nm_rpcops->nr_writerpc)(vp, &uio, cred, &iomode, &must_commit);
336 pmap_qremove(kva, npages);
337 relpbuf(bp, &nfs_pbuf_freecnt);
340 vnode_pager_undirty_pages(pages, rtvals, count - uio.uio_resid);
342 nfs_clearcommit(vp->v_mount);
349 * For nfs, cache consistency can only be maintained approximately.
350 * Although RFC1094 does not specify the criteria, the following is
351 * believed to be compatible with the reference port.
353 * If the file's modify time on the server has changed since the
354 * last read rpc or you have written to the file,
355 * you may have lost data cache consistency with the
356 * server, so flush all of the file's data out of the cache.
357 * Then force a getattr rpc to ensure that you have up to date
359 * NB: This implies that cache data can be read when up to
360 * NFS_ATTRTIMEO seconds out of date. If you find that you need current
361 * attributes this could be forced by setting n_attrstamp to 0 before
362 * the VOP_GETATTR() call.
365 nfs_bioread_check_cons(struct vnode *vp, struct thread *td, struct ucred *cred)
369 struct nfsnode *np = VTONFS(vp);
371 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
374 * Grab the exclusive lock before checking whether the cache is
376 * XXX - We can make this cheaper later (by acquiring cheaper locks).
377 * But for now, this suffices.
379 old_lock = nfs_upgrade_vnlock(vp);
380 if (vp->v_iflag & VI_DOOMED) {
381 nfs_downgrade_vnlock(vp, old_lock);
385 mtx_lock(&np->n_mtx);
386 if (np->n_flag & NMODIFIED) {
387 mtx_unlock(&np->n_mtx);
388 if (vp->v_type != VREG) {
389 if (vp->v_type != VDIR)
390 panic("nfs: bioread, not dir");
391 (nmp->nm_rpcops->nr_invaldir)(vp);
392 error = nfs_vinvalbuf(vp, V_SAVE, td, 1);
397 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
398 error = VOP_GETATTR(vp, &vattr, cred);
401 mtx_lock(&np->n_mtx);
402 np->n_mtime = vattr.va_mtime;
403 mtx_unlock(&np->n_mtx);
405 mtx_unlock(&np->n_mtx);
406 error = VOP_GETATTR(vp, &vattr, cred);
409 mtx_lock(&np->n_mtx);
410 if ((np->n_flag & NSIZECHANGED)
411 || (NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime))) {
412 mtx_unlock(&np->n_mtx);
413 if (vp->v_type == VDIR)
414 (nmp->nm_rpcops->nr_invaldir)(vp);
415 error = nfs_vinvalbuf(vp, V_SAVE, td, 1);
418 mtx_lock(&np->n_mtx);
419 np->n_mtime = vattr.va_mtime;
420 np->n_flag &= ~NSIZECHANGED;
422 mtx_unlock(&np->n_mtx);
425 nfs_downgrade_vnlock(vp, old_lock);
430 * Vnode op for read using bio
433 nfs_bioread(struct vnode *vp, struct uio *uio, int ioflag, struct ucred *cred)
435 struct nfsnode *np = VTONFS(vp);
437 struct buf *bp, *rabp;
439 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
444 int nra, error = 0, n = 0, on = 0;
446 KASSERT(uio->uio_rw == UIO_READ, ("nfs_read mode"));
447 if (uio->uio_resid == 0)
449 if (uio->uio_offset < 0) /* XXX VDIR cookies can be negative */
453 mtx_lock(&nmp->nm_mtx);
454 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
455 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) {
456 mtx_unlock(&nmp->nm_mtx);
457 (void)nfs_fsinfo(nmp, vp, cred, td);
459 mtx_unlock(&nmp->nm_mtx);
461 end = uio->uio_offset + uio->uio_resid;
462 if (vp->v_type != VDIR &&
463 (end > nmp->nm_maxfilesize || end < uio->uio_offset))
466 if (nfs_directio_enable && (ioflag & IO_DIRECT) && (vp->v_type == VREG))
467 /* No caching/ no readaheads. Just read data into the user buffer */
468 return nfs_readrpc(vp, uio, cred);
470 biosize = vp->v_mount->mnt_stat.f_iosize;
471 seqcount = (int)((off_t)(ioflag >> IO_SEQSHIFT) * biosize / BKVASIZE);
473 error = nfs_bioread_check_cons(vp, td, cred);
480 mtx_lock(&np->n_mtx);
482 mtx_unlock(&np->n_mtx);
484 switch (vp->v_type) {
486 nfsstats.biocache_reads++;
487 lbn = uio->uio_offset / biosize;
488 on = uio->uio_offset & (biosize - 1);
491 * Start the read ahead(s), as required.
493 if (nmp->nm_readahead > 0) {
494 for (nra = 0; nra < nmp->nm_readahead && nra < seqcount &&
495 (off_t)(lbn + 1 + nra) * biosize < nsize; nra++) {
496 rabn = lbn + 1 + nra;
497 if (incore(&vp->v_bufobj, rabn) == NULL) {
498 rabp = nfs_getcacheblk(vp, rabn, biosize, td);
500 error = nfs_sigintr(nmp, td);
501 return (error ? error : EINTR);
503 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
504 rabp->b_flags |= B_ASYNC;
505 rabp->b_iocmd = BIO_READ;
506 vfs_busy_pages(rabp, 0);
507 if (nfs_asyncio(nmp, rabp, cred, td)) {
508 rabp->b_flags |= B_INVAL;
509 rabp->b_ioflags |= BIO_ERROR;
510 vfs_unbusy_pages(rabp);
521 /* Note that bcount is *not* DEV_BSIZE aligned. */
523 if ((off_t)lbn * biosize >= nsize) {
525 } else if ((off_t)(lbn + 1) * biosize > nsize) {
526 bcount = nsize - (off_t)lbn * biosize;
528 bp = nfs_getcacheblk(vp, lbn, bcount, td);
531 error = nfs_sigintr(nmp, td);
532 return (error ? error : EINTR);
536 * If B_CACHE is not set, we must issue the read. If this
537 * fails, we return an error.
540 if ((bp->b_flags & B_CACHE) == 0) {
541 bp->b_iocmd = BIO_READ;
542 vfs_busy_pages(bp, 0);
543 error = nfs_doio(vp, bp, cred, td);
551 * on is the offset into the current bp. Figure out how many
552 * bytes we can copy out of the bp. Note that bcount is
553 * NOT DEV_BSIZE aligned.
555 * Then figure out how many bytes we can copy into the uio.
560 n = min((unsigned)(bcount - on), uio->uio_resid);
563 nfsstats.biocache_readlinks++;
564 bp = nfs_getcacheblk(vp, (daddr_t)0, NFS_MAXPATHLEN, td);
566 error = nfs_sigintr(nmp, td);
567 return (error ? error : EINTR);
569 if ((bp->b_flags & B_CACHE) == 0) {
570 bp->b_iocmd = BIO_READ;
571 vfs_busy_pages(bp, 0);
572 error = nfs_doio(vp, bp, cred, td);
574 bp->b_ioflags |= BIO_ERROR;
579 n = min(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid);
583 nfsstats.biocache_readdirs++;
584 if (np->n_direofoffset
585 && uio->uio_offset >= np->n_direofoffset) {
588 lbn = (uoff_t)uio->uio_offset / NFS_DIRBLKSIZ;
589 on = uio->uio_offset & (NFS_DIRBLKSIZ - 1);
590 bp = nfs_getcacheblk(vp, lbn, NFS_DIRBLKSIZ, td);
592 error = nfs_sigintr(nmp, td);
593 return (error ? error : EINTR);
595 if ((bp->b_flags & B_CACHE) == 0) {
596 bp->b_iocmd = BIO_READ;
597 vfs_busy_pages(bp, 0);
598 error = nfs_doio(vp, bp, cred, td);
602 while (error == NFSERR_BAD_COOKIE) {
603 (nmp->nm_rpcops->nr_invaldir)(vp);
604 error = nfs_vinvalbuf(vp, 0, td, 1);
606 * Yuck! The directory has been modified on the
607 * server. The only way to get the block is by
608 * reading from the beginning to get all the
611 * Leave the last bp intact unless there is an error.
612 * Loop back up to the while if the error is another
613 * NFSERR_BAD_COOKIE (double yuch!).
615 for (i = 0; i <= lbn && !error; i++) {
616 if (np->n_direofoffset
617 && (i * NFS_DIRBLKSIZ) >= np->n_direofoffset)
619 bp = nfs_getcacheblk(vp, i, NFS_DIRBLKSIZ, td);
621 error = nfs_sigintr(nmp, td);
622 return (error ? error : EINTR);
624 if ((bp->b_flags & B_CACHE) == 0) {
625 bp->b_iocmd = BIO_READ;
626 vfs_busy_pages(bp, 0);
627 error = nfs_doio(vp, bp, cred, td);
629 * no error + B_INVAL == directory EOF,
632 if (error == 0 && (bp->b_flags & B_INVAL))
636 * An error will throw away the block and the
637 * for loop will break out. If no error and this
638 * is not the block we want, we throw away the
639 * block and go for the next one via the for loop.
641 if (error || i < lbn)
646 * The above while is repeated if we hit another cookie
647 * error. If we hit an error and it wasn't a cookie error,
655 * If not eof and read aheads are enabled, start one.
656 * (You need the current block first, so that you have the
657 * directory offset cookie of the next block.)
659 if (nmp->nm_readahead > 0 &&
660 (bp->b_flags & B_INVAL) == 0 &&
661 (np->n_direofoffset == 0 ||
662 (lbn + 1) * NFS_DIRBLKSIZ < np->n_direofoffset) &&
663 incore(&vp->v_bufobj, lbn + 1) == NULL) {
664 rabp = nfs_getcacheblk(vp, lbn + 1, NFS_DIRBLKSIZ, td);
666 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
667 rabp->b_flags |= B_ASYNC;
668 rabp->b_iocmd = BIO_READ;
669 vfs_busy_pages(rabp, 0);
670 if (nfs_asyncio(nmp, rabp, cred, td)) {
671 rabp->b_flags |= B_INVAL;
672 rabp->b_ioflags |= BIO_ERROR;
673 vfs_unbusy_pages(rabp);
682 * Unlike VREG files, whos buffer size ( bp->b_bcount ) is
683 * chopped for the EOF condition, we cannot tell how large
684 * NFS directories are going to be until we hit EOF. So
685 * an NFS directory buffer is *not* chopped to its EOF. Now,
686 * it just so happens that b_resid will effectively chop it
687 * to EOF. *BUT* this information is lost if the buffer goes
688 * away and is reconstituted into a B_CACHE state ( due to
689 * being VMIO ) later. So we keep track of the directory eof
690 * in np->n_direofoffset and chop it off as an extra step
693 n = lmin(uio->uio_resid, NFS_DIRBLKSIZ - bp->b_resid - on);
694 if (np->n_direofoffset && n > np->n_direofoffset - uio->uio_offset)
695 n = np->n_direofoffset - uio->uio_offset;
698 nfs_printf(" nfs_bioread: type %x unexpected\n", vp->v_type);
704 error = uiomove(bp->b_data + on, (int)n, uio);
706 if (vp->v_type == VLNK)
710 } while (error == 0 && uio->uio_resid > 0 && n > 0);
715 * The NFS write path cannot handle iovecs with len > 1. So we need to
716 * break up iovecs accordingly (restricting them to wsize).
717 * For the SYNC case, we can do this with 1 copy (user buffer -> mbuf).
718 * For the ASYNC case, 2 copies are needed. The first a copy from the
719 * user buffer to a staging buffer and then a second copy from the staging
720 * buffer to mbufs. This can be optimized by copying from the user buffer
721 * directly into mbufs and passing the chain down, but that requires a
722 * fair amount of re-working of the relevant codepaths (and can be done
726 nfs_directio_write(vp, uiop, cred, ioflag)
733 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
734 struct thread *td = uiop->uio_td;
738 mtx_lock(&nmp->nm_mtx);
739 wsize = nmp->nm_wsize;
740 mtx_unlock(&nmp->nm_mtx);
741 if (ioflag & IO_SYNC) {
742 int iomode, must_commit;
746 while (uiop->uio_resid > 0) {
747 size = min(uiop->uio_resid, wsize);
748 size = min(uiop->uio_iov->iov_len, size);
749 iov.iov_base = uiop->uio_iov->iov_base;
753 uio.uio_offset = uiop->uio_offset;
754 uio.uio_resid = size;
755 uio.uio_segflg = UIO_USERSPACE;
756 uio.uio_rw = UIO_WRITE;
758 iomode = NFSV3WRITE_FILESYNC;
759 error = (nmp->nm_rpcops->nr_writerpc)(vp, &uio, cred,
760 &iomode, &must_commit);
761 KASSERT((must_commit == 0),
762 ("nfs_directio_write: Did not commit write"));
765 uiop->uio_offset += size;
766 uiop->uio_resid -= size;
767 if (uiop->uio_iov->iov_len <= size) {
771 uiop->uio_iov->iov_base =
772 (char *)uiop->uio_iov->iov_base + size;
773 uiop->uio_iov->iov_len -= size;
782 * Break up the write into blocksize chunks and hand these
783 * over to nfsiod's for write back.
784 * Unfortunately, this incurs a copy of the data. Since
785 * the user could modify the buffer before the write is
788 * The obvious optimization here is that one of the 2 copies
789 * in the async write path can be eliminated by copying the
790 * data here directly into mbufs and passing the mbuf chain
791 * down. But that will require a fair amount of re-working
792 * of the code and can be done if there's enough interest
793 * in NFS directio access.
795 while (uiop->uio_resid > 0) {
796 size = min(uiop->uio_resid, wsize);
797 size = min(uiop->uio_iov->iov_len, size);
798 bp = getpbuf(&nfs_pbuf_freecnt);
799 t_uio = malloc(sizeof(struct uio), M_NFSDIRECTIO, M_WAITOK);
800 t_iov = malloc(sizeof(struct iovec), M_NFSDIRECTIO, M_WAITOK);
801 t_iov->iov_base = malloc(size, M_NFSDIRECTIO, M_WAITOK);
802 t_iov->iov_len = size;
803 t_uio->uio_iov = t_iov;
804 t_uio->uio_iovcnt = 1;
805 t_uio->uio_offset = uiop->uio_offset;
806 t_uio->uio_resid = size;
807 t_uio->uio_segflg = UIO_SYSSPACE;
808 t_uio->uio_rw = UIO_WRITE;
810 bcopy(uiop->uio_iov->iov_base, t_iov->iov_base, size);
811 bp->b_flags |= B_DIRECT;
812 bp->b_iocmd = BIO_WRITE;
813 if (cred != NOCRED) {
817 bp->b_wcred = NOCRED;
818 bp->b_caller1 = (void *)t_uio;
820 error = nfs_asyncio(nmp, bp, NOCRED, td);
822 free(t_iov->iov_base, M_NFSDIRECTIO);
823 free(t_iov, M_NFSDIRECTIO);
824 free(t_uio, M_NFSDIRECTIO);
826 relpbuf(bp, &nfs_pbuf_freecnt);
831 uiop->uio_offset += size;
832 uiop->uio_resid -= size;
833 if (uiop->uio_iov->iov_len <= size) {
837 uiop->uio_iov->iov_base =
838 (char *)uiop->uio_iov->iov_base + size;
839 uiop->uio_iov->iov_len -= size;
847 * Vnode op for write using bio
850 nfs_write(struct vop_write_args *ap)
853 struct uio *uio = ap->a_uio;
854 struct thread *td = uio->uio_td;
855 struct vnode *vp = ap->a_vp;
856 struct nfsnode *np = VTONFS(vp);
857 struct ucred *cred = ap->a_cred;
858 int ioflag = ap->a_ioflag;
861 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
865 int n, on, error = 0;
867 KASSERT(uio->uio_rw == UIO_WRITE, ("nfs_write mode"));
868 KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
870 if (vp->v_type != VREG)
872 mtx_lock(&np->n_mtx);
873 if (np->n_flag & NWRITEERR) {
874 np->n_flag &= ~NWRITEERR;
875 mtx_unlock(&np->n_mtx);
876 return (np->n_error);
878 mtx_unlock(&np->n_mtx);
879 mtx_lock(&nmp->nm_mtx);
880 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
881 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) {
882 mtx_unlock(&nmp->nm_mtx);
883 (void)nfs_fsinfo(nmp, vp, cred, td);
885 mtx_unlock(&nmp->nm_mtx);
888 * Synchronously flush pending buffers if we are in synchronous
889 * mode or if we are appending.
891 if (ioflag & (IO_APPEND | IO_SYNC)) {
892 mtx_lock(&np->n_mtx);
893 if (np->n_flag & NMODIFIED) {
894 mtx_unlock(&np->n_mtx);
895 #ifdef notyet /* Needs matching nonblock semantics elsewhere, too. */
897 * Require non-blocking, synchronous writes to
898 * dirty files to inform the program it needs
899 * to fsync(2) explicitly.
901 if (ioflag & IO_NDELAY)
906 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
907 error = nfs_vinvalbuf(vp, V_SAVE, td, 1);
911 mtx_unlock(&np->n_mtx);
915 * If IO_APPEND then load uio_offset. We restart here if we cannot
916 * get the append lock.
918 if (ioflag & IO_APPEND) {
920 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
921 error = VOP_GETATTR(vp, &vattr, cred);
924 mtx_lock(&np->n_mtx);
925 uio->uio_offset = np->n_size;
926 mtx_unlock(&np->n_mtx);
929 if (uio->uio_offset < 0)
931 end = uio->uio_offset + uio->uio_resid;
932 if (end > nmp->nm_maxfilesize || end < uio->uio_offset)
934 if (uio->uio_resid == 0)
937 if (nfs_directio_enable && (ioflag & IO_DIRECT) && vp->v_type == VREG)
938 return nfs_directio_write(vp, uio, cred, ioflag);
941 * Maybe this should be above the vnode op call, but so long as
942 * file servers have no limits, i don't think it matters
944 if (vn_rlimit_fsize(vp, uio, td))
947 biosize = vp->v_mount->mnt_stat.f_iosize;
949 * Find all of this file's B_NEEDCOMMIT buffers. If our writes
950 * would exceed the local maximum per-file write commit size when
951 * combined with those, we must decide whether to flush,
952 * go synchronous, or return error. We don't bother checking
953 * IO_UNIT -- we just make all writes atomic anyway, as there's
954 * no point optimizing for something that really won't ever happen.
956 if (!(ioflag & IO_SYNC)) {
959 mtx_lock(&np->n_mtx);
961 mtx_unlock(&np->n_mtx);
963 if (nmp->nm_wcommitsize < uio->uio_resid) {
965 * If this request could not possibly be completed
966 * without exceeding the maximum outstanding write
967 * commit size, see if we can convert it into a
968 * synchronous write operation.
970 if (ioflag & IO_NDELAY)
973 if (nflag & NMODIFIED)
975 } else if (nflag & NMODIFIED) {
977 BO_LOCK(&vp->v_bufobj);
978 if (vp->v_bufobj.bo_dirty.bv_cnt != 0) {
979 TAILQ_FOREACH(bp, &vp->v_bufobj.bo_dirty.bv_hd,
981 if (bp->b_flags & B_NEEDCOMMIT)
982 wouldcommit += bp->b_bcount;
985 BO_UNLOCK(&vp->v_bufobj);
987 * Since we're not operating synchronously and
988 * bypassing the buffer cache, we are in a commit
989 * and holding all of these buffers whether
990 * transmitted or not. If not limited, this
991 * will lead to the buffer cache deadlocking,
992 * as no one else can flush our uncommitted buffers.
994 wouldcommit += uio->uio_resid;
996 * If we would initially exceed the maximum
997 * outstanding write commit size, flush and restart.
999 if (wouldcommit > nmp->nm_wcommitsize)
1003 goto flush_and_restart;
1007 nfsstats.biocache_writes++;
1008 lbn = uio->uio_offset / biosize;
1009 on = uio->uio_offset & (biosize-1);
1010 n = min((unsigned)(biosize - on), uio->uio_resid);
1013 * Handle direct append and file extension cases, calculate
1014 * unaligned buffer size.
1016 mtx_lock(&np->n_mtx);
1017 if (uio->uio_offset == np->n_size && n) {
1018 mtx_unlock(&np->n_mtx);
1020 * Get the buffer (in its pre-append state to maintain
1021 * B_CACHE if it was previously set). Resize the
1022 * nfsnode after we have locked the buffer to prevent
1023 * readers from reading garbage.
1026 bp = nfs_getcacheblk(vp, lbn, bcount, td);
1031 mtx_lock(&np->n_mtx);
1032 np->n_size = uio->uio_offset + n;
1033 np->n_flag |= NMODIFIED;
1034 vnode_pager_setsize(vp, np->n_size);
1035 mtx_unlock(&np->n_mtx);
1037 save = bp->b_flags & B_CACHE;
1039 allocbuf(bp, bcount);
1040 bp->b_flags |= save;
1044 * Obtain the locked cache block first, and then
1045 * adjust the file's size as appropriate.
1048 if ((off_t)lbn * biosize + bcount < np->n_size) {
1049 if ((off_t)(lbn + 1) * biosize < np->n_size)
1052 bcount = np->n_size - (off_t)lbn * biosize;
1054 mtx_unlock(&np->n_mtx);
1055 bp = nfs_getcacheblk(vp, lbn, bcount, td);
1056 mtx_lock(&np->n_mtx);
1057 if (uio->uio_offset + n > np->n_size) {
1058 np->n_size = uio->uio_offset + n;
1059 np->n_flag |= NMODIFIED;
1060 vnode_pager_setsize(vp, np->n_size);
1062 mtx_unlock(&np->n_mtx);
1066 error = nfs_sigintr(nmp, td);
1073 * Issue a READ if B_CACHE is not set. In special-append
1074 * mode, B_CACHE is based on the buffer prior to the write
1075 * op and is typically set, avoiding the read. If a read
1076 * is required in special append mode, the server will
1077 * probably send us a short-read since we extended the file
1078 * on our end, resulting in b_resid == 0 and, thusly,
1079 * B_CACHE getting set.
1081 * We can also avoid issuing the read if the write covers
1082 * the entire buffer. We have to make sure the buffer state
1083 * is reasonable in this case since we will not be initiating
1084 * I/O. See the comments in kern/vfs_bio.c's getblk() for
1087 * B_CACHE may also be set due to the buffer being cached
1091 if (on == 0 && n == bcount) {
1092 bp->b_flags |= B_CACHE;
1093 bp->b_flags &= ~B_INVAL;
1094 bp->b_ioflags &= ~BIO_ERROR;
1097 if ((bp->b_flags & B_CACHE) == 0) {
1098 bp->b_iocmd = BIO_READ;
1099 vfs_busy_pages(bp, 0);
1100 error = nfs_doio(vp, bp, cred, td);
1106 if (bp->b_wcred == NOCRED)
1107 bp->b_wcred = crhold(cred);
1108 mtx_lock(&np->n_mtx);
1109 np->n_flag |= NMODIFIED;
1110 mtx_unlock(&np->n_mtx);
1113 * If dirtyend exceeds file size, chop it down. This should
1114 * not normally occur but there is an append race where it
1115 * might occur XXX, so we log it.
1117 * If the chopping creates a reverse-indexed or degenerate
1118 * situation with dirtyoff/end, we 0 both of them.
1121 if (bp->b_dirtyend > bcount) {
1122 nfs_printf("NFS append race @%lx:%d\n",
1123 (long)bp->b_blkno * DEV_BSIZE,
1124 bp->b_dirtyend - bcount);
1125 bp->b_dirtyend = bcount;
1128 if (bp->b_dirtyoff >= bp->b_dirtyend)
1129 bp->b_dirtyoff = bp->b_dirtyend = 0;
1132 * If the new write will leave a contiguous dirty
1133 * area, just update the b_dirtyoff and b_dirtyend,
1134 * otherwise force a write rpc of the old dirty area.
1136 * While it is possible to merge discontiguous writes due to
1137 * our having a B_CACHE buffer ( and thus valid read data
1138 * for the hole), we don't because it could lead to
1139 * significant cache coherency problems with multiple clients,
1140 * especially if locking is implemented later on.
1142 * as an optimization we could theoretically maintain
1143 * a linked list of discontinuous areas, but we would still
1144 * have to commit them separately so there isn't much
1145 * advantage to it except perhaps a bit of asynchronization.
1148 if (bp->b_dirtyend > 0 &&
1149 (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) {
1150 if (bwrite(bp) == EINTR) {
1157 error = uiomove((char *)bp->b_data + on, n, uio);
1160 * Since this block is being modified, it must be written
1161 * again and not just committed. Since write clustering does
1162 * not work for the stage 1 data write, only the stage 2
1163 * commit rpc, we have to clear B_CLUSTEROK as well.
1165 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1168 bp->b_ioflags |= BIO_ERROR;
1174 * Only update dirtyoff/dirtyend if not a degenerate
1178 if (bp->b_dirtyend > 0) {
1179 bp->b_dirtyoff = min(on, bp->b_dirtyoff);
1180 bp->b_dirtyend = max((on + n), bp->b_dirtyend);
1182 bp->b_dirtyoff = on;
1183 bp->b_dirtyend = on + n;
1185 vfs_bio_set_valid(bp, on, n);
1189 * If IO_SYNC do bwrite().
1191 * IO_INVAL appears to be unused. The idea appears to be
1192 * to turn off caching in this case. Very odd. XXX
1194 if ((ioflag & IO_SYNC)) {
1195 if (ioflag & IO_INVAL)
1196 bp->b_flags |= B_NOCACHE;
1200 } else if ((n + on) == biosize) {
1201 bp->b_flags |= B_ASYNC;
1202 (void) (nmp->nm_rpcops->nr_writebp)(bp, 0, NULL);
1206 } while (uio->uio_resid > 0 && n > 0);
1212 * Get an nfs cache block.
1214 * Allocate a new one if the block isn't currently in the cache
1215 * and return the block marked busy. If the calling process is
1216 * interrupted by a signal for an interruptible mount point, return
1219 * The caller must carefully deal with the possible B_INVAL state of
1220 * the buffer. nfs_doio() clears B_INVAL (and nfs_asyncio() clears it
1221 * indirectly), so synchronous reads can be issued without worrying about
1222 * the B_INVAL state. We have to be a little more careful when dealing
1223 * with writes (see comments in nfs_write()) when extending a file past
1227 nfs_getcacheblk(struct vnode *vp, daddr_t bn, int size, struct thread *td)
1231 struct nfsmount *nmp;
1236 if (nmp->nm_flag & NFSMNT_INT) {
1239 nfs_set_sigmask(td, &oldset);
1240 bp = getblk(vp, bn, size, NFS_PCATCH, 0, 0);
1241 nfs_restore_sigmask(td, &oldset);
1242 while (bp == NULL) {
1243 if (nfs_sigintr(nmp, td))
1245 bp = getblk(vp, bn, size, 0, 2 * hz, 0);
1248 bp = getblk(vp, bn, size, 0, 0, 0);
1251 if (vp->v_type == VREG) {
1254 biosize = mp->mnt_stat.f_iosize;
1255 bp->b_blkno = bn * (biosize / DEV_BSIZE);
1261 * Flush and invalidate all dirty buffers. If another process is already
1262 * doing the flush, just wait for completion.
1265 nfs_vinvalbuf(struct vnode *vp, int flags, struct thread *td, int intrflg)
1267 struct nfsnode *np = VTONFS(vp);
1268 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1269 int error = 0, slpflag, slptimeo;
1272 ASSERT_VOP_LOCKED(vp, "nfs_vinvalbuf");
1274 if ((nmp->nm_flag & NFSMNT_INT) == 0)
1277 slpflag = NFS_PCATCH;
1284 old_lock = nfs_upgrade_vnlock(vp);
1285 if (vp->v_iflag & VI_DOOMED) {
1287 * Since vgonel() uses the generic vinvalbuf() to flush
1288 * dirty buffers and it does not call this function, it
1289 * is safe to just return OK when VI_DOOMED is set.
1291 nfs_downgrade_vnlock(vp, old_lock);
1296 * Now, flush as required.
1298 if ((flags & V_SAVE) && (vp->v_bufobj.bo_object != NULL)) {
1299 VM_OBJECT_LOCK(vp->v_bufobj.bo_object);
1300 vm_object_page_clean(vp->v_bufobj.bo_object, 0, 0, OBJPC_SYNC);
1301 VM_OBJECT_UNLOCK(vp->v_bufobj.bo_object);
1303 * If the page clean was interrupted, fail the invalidation.
1304 * Not doing so, we run the risk of losing dirty pages in the
1305 * vinvalbuf() call below.
1307 if (intrflg && (error = nfs_sigintr(nmp, td)))
1311 error = vinvalbuf(vp, flags, slpflag, 0);
1313 if (intrflg && (error = nfs_sigintr(nmp, td)))
1315 error = vinvalbuf(vp, flags, 0, slptimeo);
1317 mtx_lock(&np->n_mtx);
1318 if (np->n_directio_asyncwr == 0)
1319 np->n_flag &= ~NMODIFIED;
1320 mtx_unlock(&np->n_mtx);
1322 nfs_downgrade_vnlock(vp, old_lock);
1327 * Initiate asynchronous I/O. Return an error if no nfsiods are available.
1328 * This is mainly to avoid queueing async I/O requests when the nfsiods
1329 * are all hung on a dead server.
1331 * Note: nfs_asyncio() does not clear (BIO_ERROR|B_INVAL) but when the bp
1332 * is eventually dequeued by the async daemon, nfs_doio() *will*.
1335 nfs_asyncio(struct nfsmount *nmp, struct buf *bp, struct ucred *cred, struct thread *td)
1344 * Commits are usually short and sweet so lets save some cpu and
1345 * leave the async daemons for more important rpc's (such as reads
1348 mtx_lock(&nfs_iod_mtx);
1349 if (bp->b_iocmd == BIO_WRITE && (bp->b_flags & B_NEEDCOMMIT) &&
1350 (nmp->nm_bufqiods > nfs_numasync / 2)) {
1351 mtx_unlock(&nfs_iod_mtx);
1355 if (nmp->nm_flag & NFSMNT_INT)
1356 slpflag = NFS_PCATCH;
1360 * Find a free iod to process this request.
1362 for (iod = 0; iod < nfs_numasync; iod++)
1363 if (nfs_iodwant[iod] == NFSIOD_AVAILABLE) {
1369 * Try to create one if none are free.
1375 * Found one, so wake it up and tell it which
1378 NFS_DPF(ASYNCIO, ("nfs_asyncio: waking iod %d for mount %p\n",
1380 nfs_iodwant[iod] = NFSIOD_NOT_AVAILABLE;
1381 nfs_iodmount[iod] = nmp;
1383 wakeup(&nfs_iodwant[iod]);
1387 * If none are free, we may already have an iod working on this mount
1388 * point. If so, it will process our request.
1391 if (nmp->nm_bufqiods > 0) {
1393 ("nfs_asyncio: %d iods are already processing mount %p\n",
1394 nmp->nm_bufqiods, nmp));
1400 * If we have an iod which can process the request, then queue
1405 * Ensure that the queue never grows too large. We still want
1406 * to asynchronize so we block rather then return EIO.
1408 while (nmp->nm_bufqlen >= 2 * nfs_numasync) {
1410 ("nfs_asyncio: waiting for mount %p queue to drain\n", nmp));
1411 nmp->nm_bufqwant = TRUE;
1412 error = nfs_msleep(td, &nmp->nm_bufq, &nfs_iod_mtx,
1414 "nfsaio", slptimeo);
1416 error2 = nfs_sigintr(nmp, td);
1418 mtx_unlock(&nfs_iod_mtx);
1421 if (slpflag == NFS_PCATCH) {
1427 * We might have lost our iod while sleeping,
1428 * so check and loop if nescessary.
1433 /* We might have lost our nfsiod */
1434 if (nmp->nm_bufqiods == 0) {
1436 ("nfs_asyncio: no iods after mount %p queue was drained, looping\n", nmp));
1440 if (bp->b_iocmd == BIO_READ) {
1441 if (bp->b_rcred == NOCRED && cred != NOCRED)
1442 bp->b_rcred = crhold(cred);
1444 if (bp->b_wcred == NOCRED && cred != NOCRED)
1445 bp->b_wcred = crhold(cred);
1448 if (bp->b_flags & B_REMFREE)
1451 TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist);
1453 if ((bp->b_flags & B_DIRECT) && bp->b_iocmd == BIO_WRITE) {
1454 mtx_lock(&(VTONFS(bp->b_vp))->n_mtx);
1455 VTONFS(bp->b_vp)->n_flag |= NMODIFIED;
1456 VTONFS(bp->b_vp)->n_directio_asyncwr++;
1457 mtx_unlock(&(VTONFS(bp->b_vp))->n_mtx);
1459 mtx_unlock(&nfs_iod_mtx);
1463 mtx_unlock(&nfs_iod_mtx);
1466 * All the iods are busy on other mounts, so return EIO to
1467 * force the caller to process the i/o synchronously.
1469 NFS_DPF(ASYNCIO, ("nfs_asyncio: no iods available, i/o is synchronous\n"));
1474 nfs_doio_directwrite(struct buf *bp)
1476 int iomode, must_commit;
1477 struct uio *uiop = (struct uio *)bp->b_caller1;
1478 char *iov_base = uiop->uio_iov->iov_base;
1479 struct nfsmount *nmp = VFSTONFS(bp->b_vp->v_mount);
1481 iomode = NFSV3WRITE_FILESYNC;
1482 uiop->uio_td = NULL; /* NULL since we're in nfsiod */
1483 (nmp->nm_rpcops->nr_writerpc)(bp->b_vp, uiop, bp->b_wcred, &iomode, &must_commit);
1484 KASSERT((must_commit == 0), ("nfs_doio_directwrite: Did not commit write"));
1485 free(iov_base, M_NFSDIRECTIO);
1486 free(uiop->uio_iov, M_NFSDIRECTIO);
1487 free(uiop, M_NFSDIRECTIO);
1488 if ((bp->b_flags & B_DIRECT) && bp->b_iocmd == BIO_WRITE) {
1489 struct nfsnode *np = VTONFS(bp->b_vp);
1490 mtx_lock(&np->n_mtx);
1491 np->n_directio_asyncwr--;
1492 if (np->n_directio_asyncwr == 0) {
1493 VTONFS(bp->b_vp)->n_flag &= ~NMODIFIED;
1494 if ((np->n_flag & NFSYNCWAIT)) {
1495 np->n_flag &= ~NFSYNCWAIT;
1496 wakeup((caddr_t)&np->n_directio_asyncwr);
1499 mtx_unlock(&np->n_mtx);
1502 relpbuf(bp, &nfs_pbuf_freecnt);
1506 * Do an I/O operation to/from a cache block. This may be called
1507 * synchronously or from an nfsiod.
1510 nfs_doio(struct vnode *vp, struct buf *bp, struct ucred *cr, struct thread *td)
1514 struct nfsmount *nmp;
1515 int error = 0, iomode, must_commit = 0;
1518 struct proc *p = td ? td->td_proc : NULL;
1522 nmp = VFSTONFS(vp->v_mount);
1524 uiop->uio_iov = &io;
1525 uiop->uio_iovcnt = 1;
1526 uiop->uio_segflg = UIO_SYSSPACE;
1530 * clear BIO_ERROR and B_INVAL state prior to initiating the I/O. We
1531 * do this here so we do not have to do it in all the code that
1534 bp->b_flags &= ~B_INVAL;
1535 bp->b_ioflags &= ~BIO_ERROR;
1537 KASSERT(!(bp->b_flags & B_DONE), ("nfs_doio: bp %p already marked done", bp));
1538 iocmd = bp->b_iocmd;
1539 if (iocmd == BIO_READ) {
1540 io.iov_len = uiop->uio_resid = bp->b_bcount;
1541 io.iov_base = bp->b_data;
1542 uiop->uio_rw = UIO_READ;
1544 switch (vp->v_type) {
1546 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
1547 nfsstats.read_bios++;
1548 error = (nmp->nm_rpcops->nr_readrpc)(vp, uiop, cr);
1551 if (uiop->uio_resid) {
1553 * If we had a short read with no error, we must have
1554 * hit a file hole. We should zero-fill the remainder.
1555 * This can also occur if the server hits the file EOF.
1557 * Holes used to be able to occur due to pending
1558 * writes, but that is not possible any longer.
1560 int nread = bp->b_bcount - uiop->uio_resid;
1561 int left = uiop->uio_resid;
1564 bzero((char *)bp->b_data + nread, left);
1565 uiop->uio_resid = 0;
1568 /* ASSERT_VOP_LOCKED(vp, "nfs_doio"); */
1569 if (p && (vp->v_vflag & VV_TEXT)) {
1570 mtx_lock(&np->n_mtx);
1571 if (NFS_TIMESPEC_COMPARE(&np->n_mtime, &np->n_vattr.va_mtime)) {
1572 mtx_unlock(&np->n_mtx);
1574 killproc(p, "text file modification");
1577 mtx_unlock(&np->n_mtx);
1581 uiop->uio_offset = (off_t)0;
1582 nfsstats.readlink_bios++;
1583 error = (nmp->nm_rpcops->nr_readlinkrpc)(vp, uiop, cr);
1586 nfsstats.readdir_bios++;
1587 uiop->uio_offset = ((u_quad_t)bp->b_lblkno) * NFS_DIRBLKSIZ;
1588 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) != 0) {
1589 error = nfs_readdirplusrpc(vp, uiop, cr);
1590 if (error == NFSERR_NOTSUPP)
1591 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
1593 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
1594 error = nfs_readdirrpc(vp, uiop, cr);
1596 * end-of-directory sets B_INVAL but does not generate an
1599 if (error == 0 && uiop->uio_resid == bp->b_bcount)
1600 bp->b_flags |= B_INVAL;
1603 nfs_printf("nfs_doio: type %x unexpected\n", vp->v_type);
1607 bp->b_ioflags |= BIO_ERROR;
1608 bp->b_error = error;
1612 * If we only need to commit, try to commit
1614 if (bp->b_flags & B_NEEDCOMMIT) {
1618 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE + bp->b_dirtyoff;
1619 retv = (nmp->nm_rpcops->nr_commit)(
1620 vp, off, bp->b_dirtyend-bp->b_dirtyoff,
1623 bp->b_dirtyoff = bp->b_dirtyend = 0;
1624 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1629 if (retv == NFSERR_STALEWRITEVERF) {
1630 nfs_clearcommit(vp->v_mount);
1635 * Setup for actual write
1637 mtx_lock(&np->n_mtx);
1638 if ((off_t)bp->b_blkno * DEV_BSIZE + bp->b_dirtyend > np->n_size)
1639 bp->b_dirtyend = np->n_size - (off_t)bp->b_blkno * DEV_BSIZE;
1640 mtx_unlock(&np->n_mtx);
1642 if (bp->b_dirtyend > bp->b_dirtyoff) {
1643 io.iov_len = uiop->uio_resid = bp->b_dirtyend
1645 uiop->uio_offset = (off_t)bp->b_blkno * DEV_BSIZE
1647 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
1648 uiop->uio_rw = UIO_WRITE;
1649 nfsstats.write_bios++;
1651 if ((bp->b_flags & (B_ASYNC | B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == B_ASYNC)
1652 iomode = NFSV3WRITE_UNSTABLE;
1654 iomode = NFSV3WRITE_FILESYNC;
1656 error = (nmp->nm_rpcops->nr_writerpc)(vp, uiop, cr, &iomode, &must_commit);
1659 * When setting B_NEEDCOMMIT also set B_CLUSTEROK to try
1660 * to cluster the buffers needing commit. This will allow
1661 * the system to submit a single commit rpc for the whole
1662 * cluster. We can do this even if the buffer is not 100%
1663 * dirty (relative to the NFS blocksize), so we optimize the
1664 * append-to-file-case.
1666 * (when clearing B_NEEDCOMMIT, B_CLUSTEROK must also be
1667 * cleared because write clustering only works for commit
1668 * rpc's, not for the data portion of the write).
1671 if (!error && iomode == NFSV3WRITE_UNSTABLE) {
1672 bp->b_flags |= B_NEEDCOMMIT;
1673 if (bp->b_dirtyoff == 0
1674 && bp->b_dirtyend == bp->b_bcount)
1675 bp->b_flags |= B_CLUSTEROK;
1677 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1681 * For an interrupted write, the buffer is still valid
1682 * and the write hasn't been pushed to the server yet,
1683 * so we can't set BIO_ERROR and report the interruption
1684 * by setting B_EINTR. For the B_ASYNC case, B_EINTR
1685 * is not relevant, so the rpc attempt is essentially
1686 * a noop. For the case of a V3 write rpc not being
1687 * committed to stable storage, the block is still
1688 * dirty and requires either a commit rpc or another
1689 * write rpc with iomode == NFSV3WRITE_FILESYNC before
1690 * the block is reused. This is indicated by setting
1691 * the B_DELWRI and B_NEEDCOMMIT flags.
1693 * If the buffer is marked B_PAGING, it does not reside on
1694 * the vp's paging queues so we cannot call bdirty(). The
1695 * bp in this case is not an NFS cache block so we should
1698 * The logic below breaks up errors into recoverable and
1699 * unrecoverable. For the former, we clear B_INVAL|B_NOCACHE
1700 * and keep the buffer around for potential write retries.
1701 * For the latter (eg ESTALE), we toss the buffer away (B_INVAL)
1702 * and save the error in the nfsnode. This is less than ideal
1703 * but necessary. Keeping such buffers around could potentially
1704 * cause buffer exhaustion eventually (they can never be written
1705 * out, so will get constantly be re-dirtied). It also causes
1706 * all sorts of vfs panics. For non-recoverable write errors,
1707 * also invalidate the attrcache, so we'll be forced to go over
1708 * the wire for this object, returning an error to user on next
1709 * call (most of the time).
1711 if (error == EINTR || error == EIO || error == ETIMEDOUT
1712 || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
1716 bp->b_flags &= ~(B_INVAL|B_NOCACHE);
1717 if ((bp->b_flags & B_PAGING) == 0) {
1719 bp->b_flags &= ~B_DONE;
1721 if (error && (bp->b_flags & B_ASYNC) == 0)
1722 bp->b_flags |= B_EINTR;
1726 bp->b_ioflags |= BIO_ERROR;
1727 bp->b_flags |= B_INVAL;
1728 bp->b_error = np->n_error = error;
1729 mtx_lock(&np->n_mtx);
1730 np->n_flag |= NWRITEERR;
1731 np->n_attrstamp = 0;
1732 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
1733 mtx_unlock(&np->n_mtx);
1735 bp->b_dirtyoff = bp->b_dirtyend = 0;
1743 bp->b_resid = uiop->uio_resid;
1745 nfs_clearcommit(vp->v_mount);
1751 * Used to aid in handling ftruncate() operations on the NFS client side.
1752 * Truncation creates a number of special problems for NFS. We have to
1753 * throw away VM pages and buffer cache buffers that are beyond EOF, and
1754 * we have to properly handle VM pages or (potentially dirty) buffers
1755 * that straddle the truncation point.
1759 nfs_meta_setsize(struct vnode *vp, struct ucred *cred, struct thread *td, u_quad_t nsize)
1761 struct nfsnode *np = VTONFS(vp);
1763 int biosize = vp->v_mount->mnt_stat.f_iosize;
1766 mtx_lock(&np->n_mtx);
1769 mtx_unlock(&np->n_mtx);
1771 if (nsize < tsize) {
1777 * vtruncbuf() doesn't get the buffer overlapping the
1778 * truncation point. We may have a B_DELWRI and/or B_CACHE
1779 * buffer that now needs to be truncated.
1781 error = vtruncbuf(vp, cred, td, nsize, biosize);
1782 lbn = nsize / biosize;
1783 bufsize = nsize & (biosize - 1);
1784 bp = nfs_getcacheblk(vp, lbn, bufsize, td);
1787 if (bp->b_dirtyoff > bp->b_bcount)
1788 bp->b_dirtyoff = bp->b_bcount;
1789 if (bp->b_dirtyend > bp->b_bcount)
1790 bp->b_dirtyend = bp->b_bcount;
1791 bp->b_flags |= B_RELBUF; /* don't leave garbage around */
1794 vnode_pager_setsize(vp, nsize);