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: src/sys/nfs/nfs_bio.c,v 1.83.2.4 2002/12/29 18:19:53 dillon Exp $
38 * $DragonFly: src/sys/vfs/nfs/nfs_bio.c,v 1.13 2004/04/19 16:33:49 cpressey 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>
53 #include <vm/vm_extern.h>
54 #include <vm/vm_page.h>
55 #include <vm/vm_object.h>
56 #include <vm/vm_pager.h>
57 #include <vm/vnode_pager.h>
68 static struct buf *nfs_getcacheblk (struct vnode *vp, daddr_t bn, int size,
71 extern int nfs_numasync;
72 extern int nfs_pbuf_freecnt;
73 extern struct nfsstats nfsstats;
76 * Vnode op for VM getpages.
78 * nfs_getpages(struct vnode *a_vp, vm_page_t *a_m, int a_count,
79 * int a_reqpage, vm_ooffset_t a_offset)
82 nfs_getpages(struct vop_getpages_args *ap)
84 struct thread *td = curthread; /* XXX */
85 int i, error, nextoff, size, toff, count, npages;
95 nmp = VFSTONFS(vp->v_mount);
99 if (vp->v_object == NULL) {
100 printf("nfs_getpages: called with non-merged cache vnode??\n");
101 return VM_PAGER_ERROR;
104 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
105 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
106 (void)nfs_fsinfo(nmp, vp, td);
108 npages = btoc(count);
111 * If the requested page is partially valid, just return it and
112 * allow the pager to zero-out the blanks. Partially valid pages
113 * can only occur at the file EOF.
117 vm_page_t m = pages[ap->a_reqpage];
120 /* handled by vm_fault now */
121 /* vm_page_zero_invalid(m, TRUE); */
122 for (i = 0; i < npages; ++i) {
123 if (i != ap->a_reqpage)
124 vnode_pager_freepage(pages[i]);
131 * We use only the kva address for the buffer, but this is extremely
132 * convienient and fast.
134 bp = getpbuf(&nfs_pbuf_freecnt);
136 kva = (vm_offset_t) bp->b_data;
137 pmap_qenter(kva, pages, npages);
139 iov.iov_base = (caddr_t) kva;
143 uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
144 uio.uio_resid = count;
145 uio.uio_segflg = UIO_SYSSPACE;
146 uio.uio_rw = UIO_READ;
149 error = nfs_readrpc(vp, &uio);
150 pmap_qremove(kva, npages);
152 relpbuf(bp, &nfs_pbuf_freecnt);
154 if (error && (uio.uio_resid == count)) {
155 printf("nfs_getpages: error %d\n", error);
156 for (i = 0; i < npages; ++i) {
157 if (i != ap->a_reqpage)
158 vnode_pager_freepage(pages[i]);
160 return VM_PAGER_ERROR;
164 * Calculate the number of bytes read and validate only that number
165 * of bytes. Note that due to pending writes, size may be 0. This
166 * does not mean that the remaining data is invalid!
169 size = count - uio.uio_resid;
171 for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
173 nextoff = toff + PAGE_SIZE;
176 m->flags &= ~PG_ZERO;
178 if (nextoff <= size) {
180 * Read operation filled an entire page
182 m->valid = VM_PAGE_BITS_ALL;
184 } else if (size > toff) {
186 * Read operation filled a partial page.
189 vm_page_set_validclean(m, 0, size - toff);
190 /* handled by vm_fault now */
191 /* vm_page_zero_invalid(m, TRUE); */
194 * Read operation was short. If no error occured
195 * we may have hit a zero-fill section. We simply
196 * leave valid set to 0.
200 if (i != ap->a_reqpage) {
202 * Whether or not to leave the page activated is up in
203 * the air, but we should put the page on a page queue
204 * somewhere (it already is in the object). Result:
205 * It appears that emperical results show that
206 * deactivating pages is best.
210 * Just in case someone was asking for this page we
211 * now tell them that it is ok to use.
214 if (m->flags & PG_WANTED)
217 vm_page_deactivate(m);
220 vnode_pager_freepage(m);
228 * Vnode op for VM putpages.
230 * nfs_putpages(struct vnode *a_vp, vm_page_t *a_m, int a_count, int a_sync,
231 * int *a_rtvals, vm_ooffset_t a_offset)
234 nfs_putpages(struct vop_putpages_args *ap)
236 struct thread *td = curthread;
241 int iomode, must_commit, i, error, npages, count;
245 struct nfsmount *nmp;
251 nmp = VFSTONFS(vp->v_mount);
254 rtvals = ap->a_rtvals;
255 npages = btoc(count);
256 offset = IDX_TO_OFF(pages[0]->pindex);
258 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
259 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
260 (void)nfs_fsinfo(nmp, vp, td);
262 for (i = 0; i < npages; i++) {
263 rtvals[i] = VM_PAGER_AGAIN;
267 * When putting pages, do not extend file past EOF.
270 if (offset + count > np->n_size) {
271 count = np->n_size - offset;
277 * We use only the kva address for the buffer, but this is extremely
278 * convienient and fast.
280 bp = getpbuf(&nfs_pbuf_freecnt);
282 kva = (vm_offset_t) bp->b_data;
283 pmap_qenter(kva, pages, npages);
285 iov.iov_base = (caddr_t) kva;
289 uio.uio_offset = offset;
290 uio.uio_resid = count;
291 uio.uio_segflg = UIO_SYSSPACE;
292 uio.uio_rw = UIO_WRITE;
295 if ((ap->a_sync & VM_PAGER_PUT_SYNC) == 0)
296 iomode = NFSV3WRITE_UNSTABLE;
298 iomode = NFSV3WRITE_FILESYNC;
300 error = nfs_writerpc(vp, &uio, &iomode, &must_commit);
302 pmap_qremove(kva, npages);
303 relpbuf(bp, &nfs_pbuf_freecnt);
306 int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE;
307 for (i = 0; i < nwritten; i++) {
308 rtvals[i] = VM_PAGER_OK;
309 vm_page_undirty(pages[i]);
312 nfs_clearcommit(vp->v_mount);
318 * Vnode op for read using bio
321 nfs_bioread(struct vnode *vp, struct uio *uio, int ioflag)
323 struct nfsnode *np = VTONFS(vp);
325 struct buf *bp = 0, *rabp;
328 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
332 int nra, error = 0, n = 0, on = 0;
335 if (uio->uio_rw != UIO_READ)
336 panic("nfs_read mode");
338 if (uio->uio_resid == 0)
340 if (uio->uio_offset < 0) /* XXX VDIR cookies can be negative */
344 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
345 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
346 (void)nfs_fsinfo(nmp, vp, td);
347 if (vp->v_type != VDIR &&
348 (uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
350 biosize = vp->v_mount->mnt_stat.f_iosize;
351 seqcount = (int)((off_t)(ioflag >> IO_SEQSHIFT) * biosize / BKVASIZE);
353 * For nfs, cache consistency can only be maintained approximately.
354 * Although RFC1094 does not specify the criteria, the following is
355 * believed to be compatible with the reference port.
356 * For nqnfs, full cache consistency is maintained within the loop.
358 * If the file's modify time on the server has changed since the
359 * last read rpc or you have written to the file,
360 * you may have lost data cache consistency with the
361 * server, so flush all of the file's data out of the cache.
362 * Then force a getattr rpc to ensure that you have up to date
364 * NB: This implies that cache data can be read when up to
365 * NFS_ATTRTIMEO seconds out of date. If you find that you need current
366 * attributes this could be forced by setting n_attrstamp to 0 before
367 * the VOP_GETATTR() call.
369 if ((nmp->nm_flag & NFSMNT_NQNFS) == 0) {
370 if (np->n_flag & NMODIFIED) {
371 if (vp->v_type != VREG) {
372 if (vp->v_type != VDIR)
373 panic("nfs: bioread, not dir");
375 error = nfs_vinvalbuf(vp, V_SAVE, td, 1);
380 error = VOP_GETATTR(vp, &vattr, td);
383 np->n_mtime = vattr.va_mtime.tv_sec;
385 error = VOP_GETATTR(vp, &vattr, td);
388 if (np->n_mtime != vattr.va_mtime.tv_sec) {
389 if (vp->v_type == VDIR)
391 error = nfs_vinvalbuf(vp, V_SAVE, td, 1);
394 np->n_mtime = vattr.va_mtime.tv_sec;
401 * Get a valid lease. If cached data is stale, flush it.
403 if (nmp->nm_flag & NFSMNT_NQNFS) {
404 if (NQNFS_CKINVALID(vp, np, ND_READ)) {
406 error = nqnfs_getlease(vp, ND_READ, td);
407 } while (error == NQNFS_EXPIRED);
410 if (np->n_lrev != np->n_brev ||
411 (np->n_flag & NQNFSNONCACHE) ||
412 ((np->n_flag & NMODIFIED) && vp->v_type == VDIR)) {
413 if (vp->v_type == VDIR)
415 error = nfs_vinvalbuf(vp, V_SAVE, td, 1);
418 np->n_brev = np->n_lrev;
420 } else if (vp->v_type == VDIR && (np->n_flag & NMODIFIED)) {
422 error = nfs_vinvalbuf(vp, V_SAVE, td, 1);
427 if (np->n_flag & NQNFSNONCACHE) {
428 switch (vp->v_type) {
430 return (nfs_readrpc(vp, uio));
432 return (nfs_readlinkrpc(vp, uio));
436 printf(" NQNFSNONCACHE: type %x unexpected\n",
440 switch (vp->v_type) {
442 nfsstats.biocache_reads++;
443 lbn = uio->uio_offset / biosize;
444 on = uio->uio_offset & (biosize - 1);
447 * Start the read ahead(s), as required.
449 if (nfs_numasync > 0 && nmp->nm_readahead > 0) {
450 for (nra = 0; nra < nmp->nm_readahead && nra < seqcount &&
451 (off_t)(lbn + 1 + nra) * biosize < np->n_size; nra++) {
452 rabn = lbn + 1 + nra;
453 if (!incore(vp, rabn)) {
454 rabp = nfs_getcacheblk(vp, rabn, biosize, td);
457 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
458 rabp->b_flags |= (B_READ | B_ASYNC);
459 vfs_busy_pages(rabp, 0);
460 if (nfs_asyncio(rabp, td)) {
461 rabp->b_flags |= B_INVAL|B_ERROR;
462 vfs_unbusy_pages(rabp);
474 * Obtain the buffer cache block. Figure out the buffer size
475 * when we are at EOF. If we are modifying the size of the
476 * buffer based on an EOF condition we need to hold
477 * nfs_rslock() through obtaining the buffer to prevent
478 * a potential writer-appender from messing with n_size.
479 * Otherwise we may accidently truncate the buffer and
482 * Note that bcount is *not* DEV_BSIZE aligned.
487 if ((off_t)lbn * biosize >= np->n_size) {
489 } else if ((off_t)(lbn + 1) * biosize > np->n_size) {
490 bcount = np->n_size - (off_t)lbn * biosize;
492 if (bcount != biosize) {
493 switch(nfs_rslock(np, td)) {
506 bp = nfs_getcacheblk(vp, lbn, bcount, td);
508 if (bcount != biosize)
509 nfs_rsunlock(np, td);
514 * If B_CACHE is not set, we must issue the read. If this
515 * fails, we return an error.
518 if ((bp->b_flags & B_CACHE) == 0) {
519 bp->b_flags |= B_READ;
520 vfs_busy_pages(bp, 0);
521 error = nfs_doio(bp, td);
529 * on is the offset into the current bp. Figure out how many
530 * bytes we can copy out of the bp. Note that bcount is
531 * NOT DEV_BSIZE aligned.
533 * Then figure out how many bytes we can copy into the uio.
538 n = min((unsigned)(bcount - on), uio->uio_resid);
541 nfsstats.biocache_readlinks++;
542 bp = nfs_getcacheblk(vp, (daddr_t)0, NFS_MAXPATHLEN, td);
545 if ((bp->b_flags & B_CACHE) == 0) {
546 bp->b_flags |= B_READ;
547 vfs_busy_pages(bp, 0);
548 error = nfs_doio(bp, td);
550 bp->b_flags |= B_ERROR;
555 n = min(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid);
559 nfsstats.biocache_readdirs++;
560 if (np->n_direofoffset
561 && uio->uio_offset >= np->n_direofoffset) {
564 lbn = (uoff_t)uio->uio_offset / NFS_DIRBLKSIZ;
565 on = uio->uio_offset & (NFS_DIRBLKSIZ - 1);
566 bp = nfs_getcacheblk(vp, lbn, NFS_DIRBLKSIZ, td);
569 if ((bp->b_flags & B_CACHE) == 0) {
570 bp->b_flags |= B_READ;
571 vfs_busy_pages(bp, 0);
572 error = nfs_doio(bp, td);
576 while (error == NFSERR_BAD_COOKIE) {
577 printf("got bad cookie vp %p bp %p\n", vp, bp);
579 error = nfs_vinvalbuf(vp, 0, td, 1);
581 * Yuck! The directory has been modified on the
582 * server. The only way to get the block is by
583 * reading from the beginning to get all the
586 * Leave the last bp intact unless there is an error.
587 * Loop back up to the while if the error is another
588 * NFSERR_BAD_COOKIE (double yuch!).
590 for (i = 0; i <= lbn && !error; i++) {
591 if (np->n_direofoffset
592 && (i * NFS_DIRBLKSIZ) >= np->n_direofoffset)
594 bp = nfs_getcacheblk(vp, i, NFS_DIRBLKSIZ, td);
597 if ((bp->b_flags & B_CACHE) == 0) {
598 bp->b_flags |= B_READ;
599 vfs_busy_pages(bp, 0);
600 error = nfs_doio(bp, td);
602 * no error + B_INVAL == directory EOF,
605 if (error == 0 && (bp->b_flags & B_INVAL))
609 * An error will throw away the block and the
610 * for loop will break out. If no error and this
611 * is not the block we want, we throw away the
612 * block and go for the next one via the for loop.
614 if (error || i < lbn)
619 * The above while is repeated if we hit another cookie
620 * error. If we hit an error and it wasn't a cookie error,
628 * If not eof and read aheads are enabled, start one.
629 * (You need the current block first, so that you have the
630 * directory offset cookie of the next block.)
632 if (nfs_numasync > 0 && nmp->nm_readahead > 0 &&
633 (bp->b_flags & B_INVAL) == 0 &&
634 (np->n_direofoffset == 0 ||
635 (lbn + 1) * NFS_DIRBLKSIZ < np->n_direofoffset) &&
636 !(np->n_flag & NQNFSNONCACHE) &&
637 !incore(vp, lbn + 1)) {
638 rabp = nfs_getcacheblk(vp, lbn + 1, NFS_DIRBLKSIZ, td);
640 if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
641 rabp->b_flags |= (B_READ | B_ASYNC);
642 vfs_busy_pages(rabp, 0);
643 if (nfs_asyncio(rabp, td)) {
644 rabp->b_flags |= B_INVAL|B_ERROR;
645 vfs_unbusy_pages(rabp);
654 * Unlike VREG files, whos buffer size ( bp->b_bcount ) is
655 * chopped for the EOF condition, we cannot tell how large
656 * NFS directories are going to be until we hit EOF. So
657 * an NFS directory buffer is *not* chopped to its EOF. Now,
658 * it just so happens that b_resid will effectively chop it
659 * to EOF. *BUT* this information is lost if the buffer goes
660 * away and is reconstituted into a B_CACHE state ( due to
661 * being VMIO ) later. So we keep track of the directory eof
662 * in np->n_direofoffset and chop it off as an extra step
665 n = lmin(uio->uio_resid, NFS_DIRBLKSIZ - bp->b_resid - on);
666 if (np->n_direofoffset && n > np->n_direofoffset - uio->uio_offset)
667 n = np->n_direofoffset - uio->uio_offset;
670 printf(" nfs_bioread: type %x unexpected\n",vp->v_type);
675 error = uiomove(bp->b_data + on, (int)n, uio);
677 switch (vp->v_type) {
685 * Invalidate buffer if caching is disabled, forcing a
686 * re-read from the remote later.
688 if (np->n_flag & NQNFSNONCACHE)
689 bp->b_flags |= B_INVAL;
692 printf(" nfs_bioread: type %x unexpected\n",vp->v_type);
695 } while (error == 0 && uio->uio_resid > 0 && n > 0);
700 * Vnode op for write using bio
702 * nfs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
703 * struct ucred *a_cred)
706 nfs_write(struct vop_write_args *ap)
709 struct uio *uio = ap->a_uio;
710 struct thread *td = uio->uio_td;
711 struct vnode *vp = ap->a_vp;
712 struct nfsnode *np = VTONFS(vp);
713 int ioflag = ap->a_ioflag;
716 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
719 int n, on, error = 0, iomode, must_commit;
723 if (uio->uio_rw != UIO_WRITE)
724 panic("nfs_write mode");
725 if (uio->uio_segflg == UIO_USERSPACE && uio->uio_td != curthread)
726 panic("nfs_write proc");
728 if (vp->v_type != VREG)
730 if (np->n_flag & NWRITEERR) {
731 np->n_flag &= ~NWRITEERR;
732 return (np->n_error);
734 if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
735 (nmp->nm_state & NFSSTA_GOTFSINFO) == 0)
736 (void)nfs_fsinfo(nmp, vp, td);
739 * Synchronously flush pending buffers if we are in synchronous
740 * mode or if we are appending.
742 if (ioflag & (IO_APPEND | IO_SYNC)) {
743 if (np->n_flag & NMODIFIED) {
745 error = nfs_vinvalbuf(vp, V_SAVE, td, 1);
752 * If IO_APPEND then load uio_offset. We restart here if we cannot
753 * get the append lock.
756 if (ioflag & IO_APPEND) {
758 error = VOP_GETATTR(vp, &vattr, td);
761 uio->uio_offset = np->n_size;
764 if (uio->uio_offset < 0)
766 if ((uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
768 if (uio->uio_resid == 0)
772 * We need to obtain the rslock if we intend to modify np->n_size
773 * in order to guarentee the append point with multiple contending
774 * writers, to guarentee that no other appenders modify n_size
775 * while we are trying to obtain a truncated buffer (i.e. to avoid
776 * accidently truncating data written by another appender due to
777 * the race), and to ensure that the buffer is populated prior to
778 * our extending of the file. We hold rslock through the entire
781 * Note that we do not synchronize the case where someone truncates
782 * the file while we are appending to it because attempting to lock
783 * this case may deadlock other parts of the system unexpectedly.
785 if ((ioflag & IO_APPEND) ||
786 uio->uio_offset + uio->uio_resid > np->n_size) {
787 switch(nfs_rslock(np, td)) {
802 * Maybe this should be above the vnode op call, but so long as
803 * file servers have no limits, i don't think it matters
805 if (td->td_proc && uio->uio_offset + uio->uio_resid >
806 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
807 psignal(td->td_proc, SIGXFSZ);
809 nfs_rsunlock(np, td);
813 biosize = vp->v_mount->mnt_stat.f_iosize;
817 * Check for a valid write lease.
819 if ((nmp->nm_flag & NFSMNT_NQNFS) &&
820 NQNFS_CKINVALID(vp, np, ND_WRITE)) {
822 error = nqnfs_getlease(vp, ND_WRITE, td);
823 } while (error == NQNFS_EXPIRED);
826 if (np->n_lrev != np->n_brev ||
827 (np->n_flag & NQNFSNONCACHE)) {
828 error = nfs_vinvalbuf(vp, V_SAVE, td, 1);
831 np->n_brev = np->n_lrev;
834 if ((np->n_flag & NQNFSNONCACHE) && uio->uio_iovcnt == 1) {
835 iomode = NFSV3WRITE_FILESYNC;
836 error = nfs_writerpc(vp, uio, &iomode, &must_commit);
838 nfs_clearcommit(vp->v_mount);
841 nfsstats.biocache_writes++;
842 lbn = uio->uio_offset / biosize;
843 on = uio->uio_offset & (biosize-1);
844 n = min((unsigned)(biosize - on), uio->uio_resid);
847 * Handle direct append and file extension cases, calculate
848 * unaligned buffer size.
851 if (uio->uio_offset == np->n_size && n) {
853 * Get the buffer (in its pre-append state to maintain
854 * B_CACHE if it was previously set). Resize the
855 * nfsnode after we have locked the buffer to prevent
856 * readers from reading garbage.
859 bp = nfs_getcacheblk(vp, lbn, bcount, td);
864 np->n_size = uio->uio_offset + n;
865 np->n_flag |= NMODIFIED;
866 vnode_pager_setsize(vp, np->n_size);
868 save = bp->b_flags & B_CACHE;
870 allocbuf(bp, bcount);
875 * Obtain the locked cache block first, and then
876 * adjust the file's size as appropriate.
879 if ((off_t)lbn * biosize + bcount < np->n_size) {
880 if ((off_t)(lbn + 1) * biosize < np->n_size)
883 bcount = np->n_size - (off_t)lbn * biosize;
885 bp = nfs_getcacheblk(vp, lbn, bcount, td);
886 if (uio->uio_offset + n > np->n_size) {
887 np->n_size = uio->uio_offset + n;
888 np->n_flag |= NMODIFIED;
889 vnode_pager_setsize(vp, np->n_size);
899 * Issue a READ if B_CACHE is not set. In special-append
900 * mode, B_CACHE is based on the buffer prior to the write
901 * op and is typically set, avoiding the read. If a read
902 * is required in special append mode, the server will
903 * probably send us a short-read since we extended the file
904 * on our end, resulting in b_resid == 0 and, thusly,
905 * B_CACHE getting set.
907 * We can also avoid issuing the read if the write covers
908 * the entire buffer. We have to make sure the buffer state
909 * is reasonable in this case since we will not be initiating
910 * I/O. See the comments in kern/vfs_bio.c's getblk() for
913 * B_CACHE may also be set due to the buffer being cached
917 if (on == 0 && n == bcount) {
918 bp->b_flags |= B_CACHE;
919 bp->b_flags &= ~(B_ERROR | B_INVAL);
922 if ((bp->b_flags & B_CACHE) == 0) {
923 bp->b_flags |= B_READ;
924 vfs_busy_pages(bp, 0);
925 error = nfs_doio(bp, td);
935 np->n_flag |= NMODIFIED;
938 * If dirtyend exceeds file size, chop it down. This should
939 * not normally occur but there is an append race where it
940 * might occur XXX, so we log it.
942 * If the chopping creates a reverse-indexed or degenerate
943 * situation with dirtyoff/end, we 0 both of them.
946 if (bp->b_dirtyend > bcount) {
947 printf("NFS append race @%lx:%d\n",
948 (long)bp->b_blkno * DEV_BSIZE,
949 bp->b_dirtyend - bcount);
950 bp->b_dirtyend = bcount;
953 if (bp->b_dirtyoff >= bp->b_dirtyend)
954 bp->b_dirtyoff = bp->b_dirtyend = 0;
957 * If the new write will leave a contiguous dirty
958 * area, just update the b_dirtyoff and b_dirtyend,
959 * otherwise force a write rpc of the old dirty area.
961 * While it is possible to merge discontiguous writes due to
962 * our having a B_CACHE buffer ( and thus valid read data
963 * for the hole), we don't because it could lead to
964 * significant cache coherency problems with multiple clients,
965 * especially if locking is implemented later on.
967 * as an optimization we could theoretically maintain
968 * a linked list of discontinuous areas, but we would still
969 * have to commit them separately so there isn't much
970 * advantage to it except perhaps a bit of asynchronization.
973 if (bp->b_dirtyend > 0 &&
974 (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) {
975 if (VOP_BWRITE(bp->b_vp, bp) == EINTR) {
983 * Check for valid write lease and get one as required.
984 * In case getblk() and/or bwrite() delayed us.
986 if ((nmp->nm_flag & NFSMNT_NQNFS) &&
987 NQNFS_CKINVALID(vp, np, ND_WRITE)) {
989 error = nqnfs_getlease(vp, ND_WRITE, td);
990 } while (error == NQNFS_EXPIRED);
995 if (np->n_lrev != np->n_brev ||
996 (np->n_flag & NQNFSNONCACHE)) {
998 error = nfs_vinvalbuf(vp, V_SAVE, td, 1);
1001 np->n_brev = np->n_lrev;
1006 error = uiomove((char *)bp->b_data + on, n, uio);
1009 * Since this block is being modified, it must be written
1010 * again and not just committed. Since write clustering does
1011 * not work for the stage 1 data write, only the stage 2
1012 * commit rpc, we have to clear B_CLUSTEROK as well.
1014 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1017 bp->b_flags |= B_ERROR;
1023 * Only update dirtyoff/dirtyend if not a degenerate
1027 if (bp->b_dirtyend > 0) {
1028 bp->b_dirtyoff = min(on, bp->b_dirtyoff);
1029 bp->b_dirtyend = max((on + n), bp->b_dirtyend);
1031 bp->b_dirtyoff = on;
1032 bp->b_dirtyend = on + n;
1034 vfs_bio_set_validclean(bp, on, n);
1037 * If IO_NOWDRAIN then set B_NOWDRAIN (e.g. nfs-backed VN
1038 * filesystem). XXX also use for loopback NFS mounts.
1040 if (ioflag & IO_NOWDRAIN)
1041 bp->b_flags |= B_NOWDRAIN;
1044 * If the lease is non-cachable or IO_SYNC do bwrite().
1046 * IO_INVAL appears to be unused. The idea appears to be
1047 * to turn off caching in this case. Very odd. XXX
1049 if ((np->n_flag & NQNFSNONCACHE) || (ioflag & IO_SYNC)) {
1050 if (ioflag & IO_INVAL)
1051 bp->b_flags |= B_NOCACHE;
1052 error = VOP_BWRITE(bp->b_vp, bp);
1055 if (np->n_flag & NQNFSNONCACHE) {
1056 error = nfs_vinvalbuf(vp, V_SAVE, td, 1);
1060 } else if ((n + on) == biosize &&
1061 (nmp->nm_flag & NFSMNT_NQNFS) == 0) {
1062 bp->b_flags |= B_ASYNC;
1063 (void)nfs_writebp(bp, 0, 0);
1067 } while (uio->uio_resid > 0 && n > 0);
1070 nfs_rsunlock(np, td);
1076 * Get an nfs cache block.
1078 * Allocate a new one if the block isn't currently in the cache
1079 * and return the block marked busy. If the calling process is
1080 * interrupted by a signal for an interruptible mount point, return
1083 * The caller must carefully deal with the possible B_INVAL state of
1084 * the buffer. nfs_doio() clears B_INVAL (and nfs_asyncio() clears it
1085 * indirectly), so synchronous reads can be issued without worrying about
1086 * the B_INVAL state. We have to be a little more careful when dealing
1087 * with writes (see comments in nfs_write()) when extending a file past
1091 nfs_getcacheblk(struct vnode *vp, daddr_t bn, int size, struct thread *td)
1095 struct nfsmount *nmp;
1100 if (nmp->nm_flag & NFSMNT_INT) {
1101 bp = getblk(vp, bn, size, PCATCH, 0);
1102 while (bp == (struct buf *)0) {
1103 if (nfs_sigintr(nmp, (struct nfsreq *)0, td))
1104 return ((struct buf *)0);
1105 bp = getblk(vp, bn, size, 0, 2 * hz);
1108 bp = getblk(vp, bn, size, 0, 0);
1111 if (vp->v_type == VREG) {
1114 biosize = mp->mnt_stat.f_iosize;
1115 bp->b_blkno = bn * (biosize / DEV_BSIZE);
1121 * Flush and invalidate all dirty buffers. If another process is already
1122 * doing the flush, just wait for completion.
1125 nfs_vinvalbuf(struct vnode *vp, int flags,
1126 struct thread *td, int intrflg)
1128 struct nfsnode *np = VTONFS(vp);
1129 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
1130 int error = 0, slpflag, slptimeo;
1132 if (vp->v_flag & VXLOCK) {
1136 if ((nmp->nm_flag & NFSMNT_INT) == 0)
1146 * First wait for any other process doing a flush to complete.
1148 while (np->n_flag & NFLUSHINPROG) {
1149 np->n_flag |= NFLUSHWANT;
1150 error = tsleep((caddr_t)&np->n_flag, 0, "nfsvinval", slptimeo);
1151 if (error && intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, td))
1156 * Now, flush as required.
1158 np->n_flag |= NFLUSHINPROG;
1159 error = vinvalbuf(vp, flags, td, slpflag, 0);
1161 if (intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, td)) {
1162 np->n_flag &= ~NFLUSHINPROG;
1163 if (np->n_flag & NFLUSHWANT) {
1164 np->n_flag &= ~NFLUSHWANT;
1165 wakeup((caddr_t)&np->n_flag);
1169 error = vinvalbuf(vp, flags, td, 0, slptimeo);
1171 np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
1172 if (np->n_flag & NFLUSHWANT) {
1173 np->n_flag &= ~NFLUSHWANT;
1174 wakeup((caddr_t)&np->n_flag);
1180 * Initiate asynchronous I/O. Return an error if no nfsiods are available.
1181 * This is mainly to avoid queueing async I/O requests when the nfsiods
1182 * are all hung on a dead server.
1184 * Note: nfs_asyncio() does not clear (B_ERROR|B_INVAL) but when the bp
1185 * is eventually dequeued by the async daemon, nfs_doio() *will*.
1188 nfs_asyncio(struct buf *bp, struct thread *td)
1190 struct nfsmount *nmp;
1198 * If no async daemons then return EIO to force caller to run the rpc
1201 if (nfs_numasync == 0)
1204 nmp = VFSTONFS(bp->b_vp->v_mount);
1207 * Commits are usually short and sweet so lets save some cpu and
1208 * leave the async daemons for more important rpc's (such as reads
1211 if ((bp->b_flags & (B_READ|B_NEEDCOMMIT)) == B_NEEDCOMMIT &&
1212 (nmp->nm_bufqiods > nfs_numasync / 2)) {
1217 if (nmp->nm_flag & NFSMNT_INT)
1222 * Find a free iod to process this request.
1224 for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
1225 if (nfs_iodwant[i]) {
1227 * Found one, so wake it up and tell it which
1231 ("nfs_asyncio: waking iod %d for mount %p\n",
1233 nfs_iodwant[i] = NULL;
1234 nfs_iodmount[i] = nmp;
1236 wakeup((caddr_t)&nfs_iodwant[i]);
1242 * If none are free, we may already have an iod working on this mount
1243 * point. If so, it will process our request.
1246 if (nmp->nm_bufqiods > 0) {
1248 ("nfs_asyncio: %d iods are already processing mount %p\n",
1249 nmp->nm_bufqiods, nmp));
1255 * If we have an iod which can process the request, then queue
1260 * Ensure that the queue never grows too large. We still want
1261 * to asynchronize so we block rather then return EIO.
1263 while (nmp->nm_bufqlen >= 2*nfs_numasync) {
1265 ("nfs_asyncio: waiting for mount %p queue to drain\n", nmp));
1266 nmp->nm_bufqwant = TRUE;
1267 error = tsleep(&nmp->nm_bufq, slpflag,
1268 "nfsaio", slptimeo);
1270 if (nfs_sigintr(nmp, NULL, td))
1272 if (slpflag == PCATCH) {
1278 * We might have lost our iod while sleeping,
1279 * so check and loop if nescessary.
1281 if (nmp->nm_bufqiods == 0) {
1283 ("nfs_asyncio: no iods after mount %p queue was drained, looping\n", nmp));
1288 if ((bp->b_flags & B_READ) == 0)
1289 bp->b_flags |= B_WRITEINPROG;
1292 TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist);
1298 * All the iods are busy on other mounts, so return EIO to
1299 * force the caller to process the i/o synchronously.
1301 NFS_DPF(ASYNCIO, ("nfs_asyncio: no iods available, i/o is synchronous\n"));
1306 * Do an I/O operation to/from a cache block. This may be called
1307 * synchronously or from an nfsiod.
1309 * NOTE! TD MIGHT BE NULL
1312 nfs_doio(struct buf *bp, struct thread *td)
1317 struct nfsmount *nmp;
1318 int error = 0, iomode, must_commit = 0;
1324 nmp = VFSTONFS(vp->v_mount);
1326 uiop->uio_iov = &io;
1327 uiop->uio_iovcnt = 1;
1328 uiop->uio_segflg = UIO_SYSSPACE;
1332 * clear B_ERROR and B_INVAL state prior to initiating the I/O. We
1333 * do this here so we do not have to do it in all the code that
1336 bp->b_flags &= ~(B_ERROR | B_INVAL);
1338 KASSERT(!(bp->b_flags & B_DONE), ("nfs_doio: bp %p already marked done", bp));
1341 * Historically, paging was done with physio, but no more.
1343 if (bp->b_flags & B_PHYS) {
1345 * ...though reading /dev/drum still gets us here.
1347 io.iov_len = uiop->uio_resid = bp->b_bcount;
1348 /* mapping was done by vmapbuf() */
1349 io.iov_base = bp->b_data;
1350 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
1351 if (bp->b_flags & B_READ) {
1352 uiop->uio_rw = UIO_READ;
1353 nfsstats.read_physios++;
1354 error = nfs_readrpc(vp, uiop);
1358 iomode = NFSV3WRITE_DATASYNC;
1359 uiop->uio_rw = UIO_WRITE;
1360 nfsstats.write_physios++;
1361 error = nfs_writerpc(vp, uiop, &iomode, &com);
1364 bp->b_flags |= B_ERROR;
1365 bp->b_error = error;
1367 } else if (bp->b_flags & B_READ) {
1368 io.iov_len = uiop->uio_resid = bp->b_bcount;
1369 io.iov_base = bp->b_data;
1370 uiop->uio_rw = UIO_READ;
1372 switch (vp->v_type) {
1374 uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
1375 nfsstats.read_bios++;
1376 error = nfs_readrpc(vp, uiop);
1379 if (uiop->uio_resid) {
1381 * If we had a short read with no error, we must have
1382 * hit a file hole. We should zero-fill the remainder.
1383 * This can also occur if the server hits the file EOF.
1385 * Holes used to be able to occur due to pending
1386 * writes, but that is not possible any longer.
1388 int nread = bp->b_bcount - uiop->uio_resid;
1389 int left = uiop->uio_resid;
1392 bzero((char *)bp->b_data + nread, left);
1393 uiop->uio_resid = 0;
1396 if (td && td->td_proc && (vp->v_flag & VTEXT) &&
1397 (((nmp->nm_flag & NFSMNT_NQNFS) &&
1398 NQNFS_CKINVALID(vp, np, ND_READ) &&
1399 np->n_lrev != np->n_brev) ||
1400 (!(nmp->nm_flag & NFSMNT_NQNFS) &&
1401 np->n_mtime != np->n_vattr.va_mtime.tv_sec))) {
1402 uprintf("Process killed due to text file modification\n");
1403 psignal(td->td_proc, SIGKILL);
1408 uiop->uio_offset = (off_t)0;
1409 nfsstats.readlink_bios++;
1410 error = nfs_readlinkrpc(vp, uiop);
1413 nfsstats.readdir_bios++;
1414 uiop->uio_offset = ((u_quad_t)bp->b_lblkno) * NFS_DIRBLKSIZ;
1415 if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
1416 error = nfs_readdirplusrpc(vp, uiop);
1417 if (error == NFSERR_NOTSUPP)
1418 nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
1420 if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
1421 error = nfs_readdirrpc(vp, uiop);
1423 * end-of-directory sets B_INVAL but does not generate an
1426 if (error == 0 && uiop->uio_resid == bp->b_bcount)
1427 bp->b_flags |= B_INVAL;
1430 printf("nfs_doio: type %x unexpected\n",vp->v_type);
1434 bp->b_flags |= B_ERROR;
1435 bp->b_error = error;
1439 * If we only need to commit, try to commit
1441 if (bp->b_flags & B_NEEDCOMMIT) {
1445 off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE + bp->b_dirtyoff;
1446 bp->b_flags |= B_WRITEINPROG;
1447 retv = nfs_commit(bp->b_vp, off,
1448 bp->b_dirtyend - bp->b_dirtyoff, td);
1449 bp->b_flags &= ~B_WRITEINPROG;
1451 bp->b_dirtyoff = bp->b_dirtyend = 0;
1452 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1457 if (retv == NFSERR_STALEWRITEVERF) {
1458 nfs_clearcommit(bp->b_vp->v_mount);
1463 * Setup for actual write
1466 if ((off_t)bp->b_blkno * DEV_BSIZE + bp->b_dirtyend > np->n_size)
1467 bp->b_dirtyend = np->n_size - (off_t)bp->b_blkno * DEV_BSIZE;
1469 if (bp->b_dirtyend > bp->b_dirtyoff) {
1470 io.iov_len = uiop->uio_resid = bp->b_dirtyend
1472 uiop->uio_offset = (off_t)bp->b_blkno * DEV_BSIZE
1474 io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
1475 uiop->uio_rw = UIO_WRITE;
1476 nfsstats.write_bios++;
1478 if ((bp->b_flags & (B_ASYNC | B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == B_ASYNC)
1479 iomode = NFSV3WRITE_UNSTABLE;
1481 iomode = NFSV3WRITE_FILESYNC;
1483 bp->b_flags |= B_WRITEINPROG;
1484 error = nfs_writerpc(vp, uiop, &iomode, &must_commit);
1487 * When setting B_NEEDCOMMIT also set B_CLUSTEROK to try
1488 * to cluster the buffers needing commit. This will allow
1489 * the system to submit a single commit rpc for the whole
1490 * cluster. We can do this even if the buffer is not 100%
1491 * dirty (relative to the NFS blocksize), so we optimize the
1492 * append-to-file-case.
1494 * (when clearing B_NEEDCOMMIT, B_CLUSTEROK must also be
1495 * cleared because write clustering only works for commit
1496 * rpc's, not for the data portion of the write).
1499 if (!error && iomode == NFSV3WRITE_UNSTABLE) {
1500 bp->b_flags |= B_NEEDCOMMIT;
1501 if (bp->b_dirtyoff == 0
1502 && bp->b_dirtyend == bp->b_bcount)
1503 bp->b_flags |= B_CLUSTEROK;
1505 bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
1507 bp->b_flags &= ~B_WRITEINPROG;
1510 * For an interrupted write, the buffer is still valid
1511 * and the write hasn't been pushed to the server yet,
1512 * so we can't set B_ERROR and report the interruption
1513 * by setting B_EINTR. For the B_ASYNC case, B_EINTR
1514 * is not relevant, so the rpc attempt is essentially
1515 * a noop. For the case of a V3 write rpc not being
1516 * committed to stable storage, the block is still
1517 * dirty and requires either a commit rpc or another
1518 * write rpc with iomode == NFSV3WRITE_FILESYNC before
1519 * the block is reused. This is indicated by setting
1520 * the B_DELWRI and B_NEEDCOMMIT flags.
1522 * If the buffer is marked B_PAGING, it does not reside on
1523 * the vp's paging queues so we cannot call bdirty(). The
1524 * bp in this case is not an NFS cache block so we should
1528 || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
1532 bp->b_flags &= ~(B_INVAL|B_NOCACHE);
1533 if ((bp->b_flags & B_PAGING) == 0) {
1535 bp->b_flags &= ~B_DONE;
1537 if (error && (bp->b_flags & B_ASYNC) == 0)
1538 bp->b_flags |= B_EINTR;
1542 bp->b_flags |= B_ERROR;
1543 bp->b_error = np->n_error = error;
1544 np->n_flag |= NWRITEERR;
1546 bp->b_dirtyoff = bp->b_dirtyend = 0;
1554 bp->b_resid = uiop->uio_resid;
1556 nfs_clearcommit(vp->v_mount);
1562 * Used to aid in handling ftruncate() operations on the NFS client side.
1563 * Truncation creates a number of special problems for NFS. We have to
1564 * throw away VM pages and buffer cache buffers that are beyond EOF, and
1565 * we have to properly handle VM pages or (potentially dirty) buffers
1566 * that straddle the truncation point.
1570 nfs_meta_setsize(struct vnode *vp, struct thread *td, u_quad_t nsize)
1572 struct nfsnode *np = VTONFS(vp);
1573 u_quad_t tsize = np->n_size;
1574 int biosize = vp->v_mount->mnt_stat.f_iosize;
1579 if (np->n_size < tsize) {
1585 * vtruncbuf() doesn't get the buffer overlapping the
1586 * truncation point. We may have a B_DELWRI and/or B_CACHE
1587 * buffer that now needs to be truncated.
1589 error = vtruncbuf(vp, td, nsize, biosize);
1590 lbn = nsize / biosize;
1591 bufsize = nsize & (biosize - 1);
1592 bp = nfs_getcacheblk(vp, lbn, bufsize, td);
1593 if (bp->b_dirtyoff > bp->b_bcount)
1594 bp->b_dirtyoff = bp->b_bcount;
1595 if (bp->b_dirtyend > bp->b_bcount)
1596 bp->b_dirtyend = bp->b_bcount;
1597 bp->b_flags |= B_RELBUF; /* don't leave garbage around */
1600 vnode_pager_setsize(vp, nsize);