nfs_bio.c revision 51344
11541Srgrimes/* 21541Srgrimes * Copyright (c) 1989, 1993 31541Srgrimes * The Regents of the University of California. All rights reserved. 41541Srgrimes * 51541Srgrimes * This code is derived from software contributed to Berkeley by 61541Srgrimes * Rick Macklem at The University of Guelph. 71541Srgrimes * 81541Srgrimes * Redistribution and use in source and binary forms, with or without 91541Srgrimes * modification, are permitted provided that the following conditions 101541Srgrimes * are met: 111541Srgrimes * 1. Redistributions of source code must retain the above copyright 121541Srgrimes * notice, this list of conditions and the following disclaimer. 131541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141541Srgrimes * notice, this list of conditions and the following disclaimer in the 151541Srgrimes * documentation and/or other materials provided with the distribution. 161541Srgrimes * 3. All advertising materials mentioning features or use of this software 171541Srgrimes * must display the following acknowledgement: 181541Srgrimes * This product includes software developed by the University of 191541Srgrimes * California, Berkeley and its contributors. 201541Srgrimes * 4. Neither the name of the University nor the names of its contributors 211541Srgrimes * may be used to endorse or promote products derived from this software 221541Srgrimes * without specific prior written permission. 231541Srgrimes * 241541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 251541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 261541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 271541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 281541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 291541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 301541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 311541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 321541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 331541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 341541Srgrimes * SUCH DAMAGE. 351541Srgrimes * 3622521Sdyson * @(#)nfs_bio.c 8.9 (Berkeley) 3/30/95 3750477Speter * $FreeBSD: head/sys/nfsclient/nfs_bio.c 51344 1999-09-17 05:57:57Z dillon $ 381541Srgrimes */ 391541Srgrimes 4022521Sdyson 411541Srgrimes#include <sys/param.h> 421541Srgrimes#include <sys/systm.h> 431541Srgrimes#include <sys/resourcevar.h> 443305Sphk#include <sys/signalvar.h> 451541Srgrimes#include <sys/proc.h> 461541Srgrimes#include <sys/buf.h> 471541Srgrimes#include <sys/vnode.h> 481541Srgrimes#include <sys/mount.h> 491541Srgrimes#include <sys/kernel.h> 501541Srgrimes 511541Srgrimes#include <vm/vm.h> 5212662Sdg#include <vm/vm_extern.h> 5325930Sdfr#include <vm/vm_prot.h> 5425930Sdfr#include <vm/vm_page.h> 5525930Sdfr#include <vm/vm_object.h> 5625930Sdfr#include <vm/vm_pager.h> 5725930Sdfr#include <vm/vnode_pager.h> 581541Srgrimes 591541Srgrimes#include <nfs/rpcv2.h> 609336Sdfr#include <nfs/nfsproto.h> 611541Srgrimes#include <nfs/nfs.h> 621541Srgrimes#include <nfs/nfsmount.h> 631541Srgrimes#include <nfs/nqnfs.h> 649336Sdfr#include <nfs/nfsnode.h> 651541Srgrimes 6612911Sphkstatic struct buf *nfs_getcacheblk __P((struct vnode *vp, daddr_t bn, int size, 6712588Sbde struct proc *p)); 6812588Sbde 691541Srgrimesextern int nfs_numasync; 7042957Sdillonextern int nfs_pbuf_freecnt; 719336Sdfrextern struct nfsstats nfsstats; 721541Srgrimes 731541Srgrimes/* 7425930Sdfr * Vnode op for VM getpages. 7525930Sdfr */ 7625930Sdfrint 7725930Sdfrnfs_getpages(ap) 7836563Speter struct vop_getpages_args /* { 7936563Speter struct vnode *a_vp; 8036563Speter vm_page_t *a_m; 8136563Speter int a_count; 8236563Speter int a_reqpage; 8336563Speter vm_ooffset_t a_offset; 8436563Speter } */ *ap; 8525930Sdfr{ 8646349Salc int i, error, nextoff, size, toff, count, npages; 8732755Sdyson struct uio uio; 8832755Sdyson struct iovec iov; 8932755Sdyson vm_offset_t kva; 9034206Sdyson struct buf *bp; 9136563Speter struct vnode *vp; 9236563Speter struct proc *p; 9336563Speter struct ucred *cred; 9436563Speter struct nfsmount *nmp; 9536563Speter vm_page_t *pages; 9625930Sdfr 9736563Speter vp = ap->a_vp; 9836563Speter p = curproc; /* XXX */ 9936563Speter cred = curproc->p_ucred; /* XXX */ 10036563Speter nmp = VFSTONFS(vp->v_mount); 10136563Speter pages = ap->a_m; 10236563Speter count = ap->a_count; 10336563Speter 10436563Speter if (vp->v_object == NULL) { 10532286Sdyson printf("nfs_getpages: called with non-merged cache vnode??\n"); 10636563Speter return VM_PAGER_ERROR; 10725930Sdfr } 10825930Sdfr 10936563Speter if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 && 11036563Speter (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) 11136563Speter (void)nfs_fsinfo(nmp, vp, cred, p); 11246349Salc 11346349Salc npages = btoc(count); 11446349Salc 11534206Sdyson /* 11646349Salc * If the requested page is partially valid, just return it and 11746349Salc * allow the pager to zero-out the blanks. Partially valid pages 11846349Salc * can only occur at the file EOF. 11946349Salc */ 12046349Salc 12146349Salc { 12246349Salc vm_page_t m = pages[ap->a_reqpage]; 12346349Salc 12446349Salc if (m->valid != 0) { 12546349Salc /* handled by vm_fault now */ 12646349Salc /* vm_page_zero_invalid(m, TRUE); */ 12746349Salc for (i = 0; i < npages; ++i) { 12846349Salc if (i != ap->a_reqpage) 12946349Salc vnode_pager_freepage(pages[i]); 13046349Salc } 13146349Salc return(0); 13246349Salc } 13346349Salc } 13446349Salc 13546349Salc /* 13634206Sdyson * We use only the kva address for the buffer, but this is extremely 13734206Sdyson * convienient and fast. 13834206Sdyson */ 13942957Sdillon bp = getpbuf(&nfs_pbuf_freecnt); 14025930Sdfr 14134206Sdyson kva = (vm_offset_t) bp->b_data; 14236563Speter pmap_qenter(kva, pages, npages); 14334206Sdyson 14432755Sdyson iov.iov_base = (caddr_t) kva; 14536563Speter iov.iov_len = count; 14632755Sdyson uio.uio_iov = &iov; 14732755Sdyson uio.uio_iovcnt = 1; 14836563Speter uio.uio_offset = IDX_TO_OFF(pages[0]->pindex); 14936563Speter uio.uio_resid = count; 15032755Sdyson uio.uio_segflg = UIO_SYSSPACE; 15132755Sdyson uio.uio_rw = UIO_READ; 15236563Speter uio.uio_procp = p; 15325930Sdfr 15436563Speter error = nfs_readrpc(vp, &uio, cred); 15534206Sdyson pmap_qremove(kva, npages); 15632755Sdyson 15742957Sdillon relpbuf(bp, &nfs_pbuf_freecnt); 15834206Sdyson 15942957Sdillon if (error && (uio.uio_resid == count)) { 16042957Sdillon printf("nfs_getpages: error %d\n", error); 16142957Sdillon for (i = 0; i < npages; ++i) { 16242957Sdillon if (i != ap->a_reqpage) 16342957Sdillon vnode_pager_freepage(pages[i]); 16442957Sdillon } 16534206Sdyson return VM_PAGER_ERROR; 16642957Sdillon } 16734206Sdyson 16845347Sjulian /* 16945347Sjulian * Calculate the number of bytes read and validate only that number 17045347Sjulian * of bytes. Note that due to pending writes, size may be 0. This 17145347Sjulian * does not mean that the remaining data is invalid! 17245347Sjulian */ 17345347Sjulian 17436563Speter size = count - uio.uio_resid; 17534206Sdyson 17634206Sdyson for (i = 0, toff = 0; i < npages; i++, toff = nextoff) { 17734206Sdyson vm_page_t m; 17834206Sdyson nextoff = toff + PAGE_SIZE; 17936563Speter m = pages[i]; 18034206Sdyson 18134206Sdyson m->flags &= ~PG_ZERO; 18234206Sdyson 18334206Sdyson if (nextoff <= size) { 18445347Sjulian /* 18545347Sjulian * Read operation filled an entire page 18645347Sjulian */ 18734206Sdyson m->valid = VM_PAGE_BITS_ALL; 18849945Salc vm_page_undirty(m); 18945347Sjulian } else if (size > toff) { 19045347Sjulian /* 19146349Salc * Read operation filled a partial page. 19245347Sjulian */ 19346349Salc m->valid = 0; 19445347Sjulian vm_page_set_validclean(m, 0, size - toff); 19546349Salc /* handled by vm_fault now */ 19646349Salc /* vm_page_zero_invalid(m, TRUE); */ 19734206Sdyson } 19834206Sdyson 19925930Sdfr if (i != ap->a_reqpage) { 20034206Sdyson /* 20134206Sdyson * Whether or not to leave the page activated is up in 20234206Sdyson * the air, but we should put the page on a page queue 20334206Sdyson * somewhere (it already is in the object). Result: 20434206Sdyson * It appears that emperical results show that 20534206Sdyson * deactivating pages is best. 20634206Sdyson */ 20734206Sdyson 20834206Sdyson /* 20934206Sdyson * Just in case someone was asking for this page we 21034206Sdyson * now tell them that it is ok to use. 21134206Sdyson */ 21234206Sdyson if (!error) { 21334206Sdyson if (m->flags & PG_WANTED) 21434206Sdyson vm_page_activate(m); 21534206Sdyson else 21634206Sdyson vm_page_deactivate(m); 21738799Sdfr vm_page_wakeup(m); 21834206Sdyson } else { 21934206Sdyson vnode_pager_freepage(m); 22034206Sdyson } 22125930Sdfr } 22225930Sdfr } 22325930Sdfr return 0; 22425930Sdfr} 22525930Sdfr 22625930Sdfr/* 22734206Sdyson * Vnode op for VM putpages. 22834096Smsmith */ 22934096Smsmithint 23034096Smsmithnfs_putpages(ap) 23136563Speter struct vop_putpages_args /* { 23236563Speter struct vnode *a_vp; 23336563Speter vm_page_t *a_m; 23436563Speter int a_count; 23536563Speter int a_sync; 23636563Speter int *a_rtvals; 23736563Speter vm_ooffset_t a_offset; 23836563Speter } */ *ap; 23934096Smsmith{ 24034206Sdyson struct uio uio; 24134206Sdyson struct iovec iov; 24234206Sdyson vm_offset_t kva; 24334206Sdyson struct buf *bp; 24436563Speter int iomode, must_commit, i, error, npages, count; 24546349Salc off_t offset; 24634206Sdyson int *rtvals; 24736563Speter struct vnode *vp; 24836563Speter struct proc *p; 24936563Speter struct ucred *cred; 25036563Speter struct nfsmount *nmp; 25146349Salc struct nfsnode *np; 25236563Speter vm_page_t *pages; 25334206Sdyson 25436563Speter vp = ap->a_vp; 25546349Salc np = VTONFS(vp); 25636563Speter p = curproc; /* XXX */ 25736563Speter cred = curproc->p_ucred; /* XXX */ 25836563Speter nmp = VFSTONFS(vp->v_mount); 25936563Speter pages = ap->a_m; 26036563Speter count = ap->a_count; 26134206Sdyson rtvals = ap->a_rtvals; 26236563Speter npages = btoc(count); 26346349Salc offset = IDX_TO_OFF(pages[0]->pindex); 26434206Sdyson 26536563Speter if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 && 26636563Speter (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) 26736563Speter (void)nfs_fsinfo(nmp, vp, cred, p); 26834206Sdyson 26934206Sdyson for (i = 0; i < npages; i++) { 27034206Sdyson rtvals[i] = VM_PAGER_AGAIN; 27134206Sdyson } 27234206Sdyson 27334206Sdyson /* 27446349Salc * When putting pages, do not extend file past EOF. 27546349Salc */ 27646349Salc 27746349Salc if (offset + count > np->n_size) { 27846349Salc count = np->n_size - offset; 27946349Salc if (count < 0) 28046349Salc count = 0; 28146349Salc } 28246349Salc 28346349Salc /* 28434206Sdyson * We use only the kva address for the buffer, but this is extremely 28534206Sdyson * convienient and fast. 28634206Sdyson */ 28742957Sdillon bp = getpbuf(&nfs_pbuf_freecnt); 28834206Sdyson 28934206Sdyson kva = (vm_offset_t) bp->b_data; 29036563Speter pmap_qenter(kva, pages, npages); 29134206Sdyson 29234206Sdyson iov.iov_base = (caddr_t) kva; 29336563Speter iov.iov_len = count; 29434206Sdyson uio.uio_iov = &iov; 29534206Sdyson uio.uio_iovcnt = 1; 29646349Salc uio.uio_offset = offset; 29736563Speter uio.uio_resid = count; 29834206Sdyson uio.uio_segflg = UIO_SYSSPACE; 29934206Sdyson uio.uio_rw = UIO_WRITE; 30036563Speter uio.uio_procp = p; 30134206Sdyson 30234206Sdyson if ((ap->a_sync & VM_PAGER_PUT_SYNC) == 0) 30334206Sdyson iomode = NFSV3WRITE_UNSTABLE; 30434206Sdyson else 30534206Sdyson iomode = NFSV3WRITE_FILESYNC; 30634206Sdyson 30736563Speter error = nfs_writerpc(vp, &uio, cred, &iomode, &must_commit); 30834206Sdyson 30934206Sdyson pmap_qremove(kva, npages); 31042957Sdillon relpbuf(bp, &nfs_pbuf_freecnt); 31134206Sdyson 31234206Sdyson if (!error) { 31336563Speter int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE; 31434206Sdyson for (i = 0; i < nwritten; i++) { 31534206Sdyson rtvals[i] = VM_PAGER_OK; 31649945Salc vm_page_undirty(pages[i]); 31734206Sdyson } 31834206Sdyson if (must_commit) 31936563Speter nfs_clearcommit(vp->v_mount); 32034206Sdyson } 32136563Speter return rtvals[0]; 32234096Smsmith} 32334096Smsmith 32434096Smsmith/* 3251541Srgrimes * Vnode op for read using bio 3261541Srgrimes */ 3271549Srgrimesint 32846349Salcnfs_bioread(vp, uio, ioflag, cred) 3291541Srgrimes register struct vnode *vp; 3301541Srgrimes register struct uio *uio; 3311541Srgrimes int ioflag; 3321541Srgrimes struct ucred *cred; 3331541Srgrimes{ 3341541Srgrimes register struct nfsnode *np = VTONFS(vp); 33541791Sdt register int biosize, i; 3361549Srgrimes struct buf *bp = 0, *rabp; 3371541Srgrimes struct vattr vattr; 3381541Srgrimes struct proc *p; 3399336Sdfr struct nfsmount *nmp = VFSTONFS(vp->v_mount); 3405455Sdg daddr_t lbn, rabn; 34146349Salc int bcount; 34251344Sdillon int seqcount; 34346349Salc int nra, error = 0, n = 0, on = 0; 3441541Srgrimes 3451541Srgrimes#ifdef DIAGNOSTIC 3461541Srgrimes if (uio->uio_rw != UIO_READ) 3471541Srgrimes panic("nfs_read mode"); 3481541Srgrimes#endif 3491541Srgrimes if (uio->uio_resid == 0) 3501541Srgrimes return (0); 35136473Speter if (uio->uio_offset < 0) /* XXX VDIR cookies can be negative */ 3521541Srgrimes return (EINVAL); 3531541Srgrimes p = uio->uio_procp; 35451344Sdillon 35536176Speter if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 && 35636176Speter (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) 3579336Sdfr (void)nfs_fsinfo(nmp, vp, cred, p); 35836473Speter if (vp->v_type != VDIR && 35936473Speter (uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize) 36036473Speter return (EFBIG); 3619428Sdfr biosize = vp->v_mount->mnt_stat.f_iosize; 36251344Sdillon seqcount = (int)((off_t)(ioflag >> 16) * biosize / BKVASIZE); 3631541Srgrimes /* 3641541Srgrimes * For nfs, cache consistency can only be maintained approximately. 3651541Srgrimes * Although RFC1094 does not specify the criteria, the following is 3661541Srgrimes * believed to be compatible with the reference port. 3671541Srgrimes * For nqnfs, full cache consistency is maintained within the loop. 3681541Srgrimes * For nfs: 3691541Srgrimes * If the file's modify time on the server has changed since the 3701541Srgrimes * last read rpc or you have written to the file, 3711541Srgrimes * you may have lost data cache consistency with the 3721541Srgrimes * server, so flush all of the file's data out of the cache. 3731541Srgrimes * Then force a getattr rpc to ensure that you have up to date 3741541Srgrimes * attributes. 3751541Srgrimes * NB: This implies that cache data can be read when up to 3761541Srgrimes * NFS_ATTRTIMEO seconds out of date. If you find that you need current 3771541Srgrimes * attributes this could be forced by setting n_attrstamp to 0 before 3781541Srgrimes * the VOP_GETATTR() call. 3791541Srgrimes */ 38010219Sdfr if ((nmp->nm_flag & NFSMNT_NQNFS) == 0) { 3811541Srgrimes if (np->n_flag & NMODIFIED) { 3829336Sdfr if (vp->v_type != VREG) { 3839336Sdfr if (vp->v_type != VDIR) 3849336Sdfr panic("nfs: bioread, not dir"); 3859336Sdfr nfs_invaldir(vp); 3863305Sphk error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1); 3873305Sphk if (error) 3881541Srgrimes return (error); 3891541Srgrimes } 3901541Srgrimes np->n_attrstamp = 0; 3913305Sphk error = VOP_GETATTR(vp, &vattr, cred, p); 3923305Sphk if (error) 3931541Srgrimes return (error); 39418397Snate np->n_mtime = vattr.va_mtime.tv_sec; 3951541Srgrimes } else { 3963305Sphk error = VOP_GETATTR(vp, &vattr, cred, p); 3973305Sphk if (error) 3981541Srgrimes return (error); 39918397Snate if (np->n_mtime != vattr.va_mtime.tv_sec) { 4009336Sdfr if (vp->v_type == VDIR) 4019336Sdfr nfs_invaldir(vp); 4023305Sphk error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1); 4033305Sphk if (error) 4041541Srgrimes return (error); 40518397Snate np->n_mtime = vattr.va_mtime.tv_sec; 4061541Srgrimes } 4071541Srgrimes } 4081541Srgrimes } 4091541Srgrimes do { 4101541Srgrimes 4111541Srgrimes /* 4121541Srgrimes * Get a valid lease. If cached data is stale, flush it. 4131541Srgrimes */ 4141541Srgrimes if (nmp->nm_flag & NFSMNT_NQNFS) { 4159336Sdfr if (NQNFS_CKINVALID(vp, np, ND_READ)) { 4161541Srgrimes do { 4179336Sdfr error = nqnfs_getlease(vp, ND_READ, cred, p); 4181541Srgrimes } while (error == NQNFS_EXPIRED); 4191541Srgrimes if (error) 4201541Srgrimes return (error); 4211541Srgrimes if (np->n_lrev != np->n_brev || 4221541Srgrimes (np->n_flag & NQNFSNONCACHE) || 4231541Srgrimes ((np->n_flag & NMODIFIED) && vp->v_type == VDIR)) { 4249336Sdfr if (vp->v_type == VDIR) 4259336Sdfr nfs_invaldir(vp); 4263305Sphk error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1); 4273305Sphk if (error) 4281541Srgrimes return (error); 4291541Srgrimes np->n_brev = np->n_lrev; 4301541Srgrimes } 4311541Srgrimes } else if (vp->v_type == VDIR && (np->n_flag & NMODIFIED)) { 4329336Sdfr nfs_invaldir(vp); 4333305Sphk error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1); 4343305Sphk if (error) 4351541Srgrimes return (error); 4361541Srgrimes } 4371541Srgrimes } 4381541Srgrimes if (np->n_flag & NQNFSNONCACHE) { 4391541Srgrimes switch (vp->v_type) { 4401541Srgrimes case VREG: 4419336Sdfr return (nfs_readrpc(vp, uio, cred)); 4421541Srgrimes case VLNK: 4439336Sdfr return (nfs_readlinkrpc(vp, uio, cred)); 4441541Srgrimes case VDIR: 4451541Srgrimes break; 4463305Sphk default: 44722521Sdyson printf(" NQNFSNONCACHE: type %x unexpected\n", 4483305Sphk vp->v_type); 4491541Srgrimes }; 4501541Srgrimes } 4511541Srgrimes switch (vp->v_type) { 4521541Srgrimes case VREG: 4531541Srgrimes nfsstats.biocache_reads++; 4541541Srgrimes lbn = uio->uio_offset / biosize; 4559336Sdfr on = uio->uio_offset & (biosize - 1); 4561541Srgrimes 4571541Srgrimes /* 4581541Srgrimes * Start the read ahead(s), as required. 4591541Srgrimes */ 4609336Sdfr if (nfs_numasync > 0 && nmp->nm_readahead > 0) { 46151344Sdillon for (nra = 0; nra < nmp->nm_readahead && nra < seqcount && 46213612Smpp (off_t)(lbn + 1 + nra) * biosize < np->n_size; nra++) { 4635455Sdg rabn = lbn + 1 + nra; 4641541Srgrimes if (!incore(vp, rabn)) { 4651541Srgrimes rabp = nfs_getcacheblk(vp, rabn, biosize, p); 4661541Srgrimes if (!rabp) 4671541Srgrimes return (EINTR); 4688692Sdg if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) { 4691541Srgrimes rabp->b_flags |= (B_READ | B_ASYNC); 4705455Sdg vfs_busy_pages(rabp, 0); 47146580Sphk if (nfs_asyncio(rabp, cred, p)) { 4725455Sdg rabp->b_flags |= B_INVAL|B_ERROR; 4735455Sdg vfs_unbusy_pages(rabp); 4741541Srgrimes brelse(rabp); 4751541Srgrimes } 47622521Sdyson } else 4775471Sdg brelse(rabp); 4781541Srgrimes } 4791541Srgrimes } 4801541Srgrimes } 4811541Srgrimes 4821541Srgrimes /* 48346349Salc * Obtain the buffer cache block. Figure out the buffer size 48446349Salc * when we are at EOF. nfs_getcacheblk() will also force 48546349Salc * uncached delayed-writes to be flushed to the server. 48646349Salc * 48746349Salc * Note that bcount is *not* DEV_BSIZE aligned. 4881541Srgrimes */ 48946349Salc 49046349Salc bcount = biosize; 49146349Salc if ((off_t)lbn * biosize >= np->n_size) { 49246349Salc bcount = 0; 49346349Salc } else if ((off_t)(lbn + 1) * biosize > np->n_size) { 49446349Salc bcount = np->n_size - (off_t)lbn * biosize; 4958692Sdg } 49646349Salc 49746349Salc bp = nfs_getcacheblk(vp, lbn, bcount, p); 4987871Sdg if (!bp) 4997871Sdg return (EINTR); 50042957Sdillon 50125930Sdfr /* 50246349Salc * If B_CACHE is not set, we must issue the read. If this 50346349Salc * fails, we return an error. 50425930Sdfr */ 50546349Salc 5067871Sdg if ((bp->b_flags & B_CACHE) == 0) { 50732755Sdyson bp->b_flags |= B_READ; 50832755Sdyson vfs_busy_pages(bp, 0); 50932755Sdyson error = nfs_doio(bp, cred, p); 51032755Sdyson if (error) { 51132755Sdyson brelse(bp); 51232755Sdyson return (error); 51332755Sdyson } 5141541Srgrimes } 51546349Salc 51646349Salc /* 51746349Salc * on is the offset into the current bp. Figure out how many 51846349Salc * bytes we can copy out of the bp. Note that bcount is 51946349Salc * NOT DEV_BSIZE aligned. 52046349Salc * 52146349Salc * Then figure out how many bytes we can copy into the uio. 52246349Salc */ 52346349Salc 52446349Salc n = 0; 52546349Salc if (on < bcount) 52646349Salc n = min((unsigned)(bcount - on), uio->uio_resid); 5271541Srgrimes break; 5281541Srgrimes case VLNK: 5291541Srgrimes nfsstats.biocache_readlinks++; 5301541Srgrimes bp = nfs_getcacheblk(vp, (daddr_t)0, NFS_MAXPATHLEN, p); 5311541Srgrimes if (!bp) 5321541Srgrimes return (EINTR); 5337871Sdg if ((bp->b_flags & B_CACHE) == 0) { 53432755Sdyson bp->b_flags |= B_READ; 53532755Sdyson vfs_busy_pages(bp, 0); 53632755Sdyson error = nfs_doio(bp, cred, p); 53732755Sdyson if (error) { 53832755Sdyson bp->b_flags |= B_ERROR; 53932755Sdyson brelse(bp); 54032755Sdyson return (error); 54132755Sdyson } 5421541Srgrimes } 5431541Srgrimes n = min(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid); 5441541Srgrimes on = 0; 5451541Srgrimes break; 5461541Srgrimes case VDIR: 5471541Srgrimes nfsstats.biocache_readdirs++; 54824577Sdfr if (np->n_direofoffset 54924577Sdfr && uio->uio_offset >= np->n_direofoffset) { 55024577Sdfr return (0); 55124577Sdfr } 55236979Sbde lbn = (uoff_t)uio->uio_offset / NFS_DIRBLKSIZ; 5539336Sdfr on = uio->uio_offset & (NFS_DIRBLKSIZ - 1); 5545455Sdg bp = nfs_getcacheblk(vp, lbn, NFS_DIRBLKSIZ, p); 5551541Srgrimes if (!bp) 5569336Sdfr return (EINTR); 5577871Sdg if ((bp->b_flags & B_CACHE) == 0) { 5589336Sdfr bp->b_flags |= B_READ; 5599336Sdfr vfs_busy_pages(bp, 0); 5609336Sdfr error = nfs_doio(bp, cred, p); 56132912Stegge if (error) { 56232912Stegge brelse(bp); 56332912Stegge } 56432755Sdyson while (error == NFSERR_BAD_COOKIE) { 56546349Salc printf("got bad cookie vp %p bp %p\n", vp, bp); 56632755Sdyson nfs_invaldir(vp); 56732755Sdyson error = nfs_vinvalbuf(vp, 0, cred, p, 1); 56832755Sdyson /* 56932755Sdyson * Yuck! The directory has been modified on the 57032755Sdyson * server. The only way to get the block is by 57132755Sdyson * reading from the beginning to get all the 57232755Sdyson * offset cookies. 57346349Salc * 57446349Salc * Leave the last bp intact unless there is an error. 57546349Salc * Loop back up to the while if the error is another 57646349Salc * NFSERR_BAD_COOKIE (double yuch!). 57732755Sdyson */ 57832755Sdyson for (i = 0; i <= lbn && !error; i++) { 57932755Sdyson if (np->n_direofoffset 58032755Sdyson && (i * NFS_DIRBLKSIZ) >= np->n_direofoffset) 58124577Sdfr return (0); 58232755Sdyson bp = nfs_getcacheblk(vp, i, NFS_DIRBLKSIZ, p); 58332755Sdyson if (!bp) 58432755Sdyson return (EINTR); 58546349Salc if ((bp->b_flags & B_CACHE) == 0) { 58646349Salc bp->b_flags |= B_READ; 58746349Salc vfs_busy_pages(bp, 0); 58846349Salc error = nfs_doio(bp, cred, p); 58946349Salc /* 59046349Salc * no error + B_INVAL == directory EOF, 59146349Salc * use the block. 59246349Salc */ 59346349Salc if (error == 0 && (bp->b_flags & B_INVAL)) 59446349Salc break; 59546349Salc } 59646349Salc /* 59746349Salc * An error will throw away the block and the 59846349Salc * for loop will break out. If no error and this 59946349Salc * is not the block we want, we throw away the 60046349Salc * block and go for the next one via the for loop. 60146349Salc */ 60246349Salc if (error || i < lbn) 60332755Sdyson brelse(bp); 6041541Srgrimes } 60532912Stegge } 60646349Salc /* 60746349Salc * The above while is repeated if we hit another cookie 60846349Salc * error. If we hit an error and it wasn't a cookie error, 60946349Salc * we give up. 61046349Salc */ 61132912Stegge if (error) 6129336Sdfr return (error); 6131541Srgrimes } 6141541Srgrimes 6151541Srgrimes /* 6161541Srgrimes * If not eof and read aheads are enabled, start one. 6171541Srgrimes * (You need the current block first, so that you have the 6189336Sdfr * directory offset cookie of the next block.) 6191541Srgrimes */ 6201541Srgrimes if (nfs_numasync > 0 && nmp->nm_readahead > 0 && 62139782Smckusick (bp->b_flags & B_INVAL) == 0 && 6229336Sdfr (np->n_direofoffset == 0 || 6239336Sdfr (lbn + 1) * NFS_DIRBLKSIZ < np->n_direofoffset) && 6249336Sdfr !(np->n_flag & NQNFSNONCACHE) && 6259336Sdfr !incore(vp, lbn + 1)) { 6269336Sdfr rabp = nfs_getcacheblk(vp, lbn + 1, NFS_DIRBLKSIZ, p); 6271541Srgrimes if (rabp) { 6288692Sdg if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) { 6291541Srgrimes rabp->b_flags |= (B_READ | B_ASYNC); 6305455Sdg vfs_busy_pages(rabp, 0); 63146580Sphk if (nfs_asyncio(rabp, cred, p)) { 6326148Sdg rabp->b_flags |= B_INVAL|B_ERROR; 6335455Sdg vfs_unbusy_pages(rabp); 6341541Srgrimes brelse(rabp); 6351541Srgrimes } 6365471Sdg } else { 6375471Sdg brelse(rabp); 6381541Srgrimes } 6391541Srgrimes } 6401541Srgrimes } 64126469Sdfr /* 64246349Salc * Unlike VREG files, whos buffer size ( bp->b_bcount ) is 64346349Salc * chopped for the EOF condition, we cannot tell how large 64446349Salc * NFS directories are going to be until we hit EOF. So 64546349Salc * an NFS directory buffer is *not* chopped to its EOF. Now, 64646349Salc * it just so happens that b_resid will effectively chop it 64746349Salc * to EOF. *BUT* this information is lost if the buffer goes 64846349Salc * away and is reconstituted into a B_CACHE state ( due to 64946349Salc * being VMIO ) later. So we keep track of the directory eof 65046349Salc * in np->n_direofoffset and chop it off as an extra step 65146349Salc * right here. 65226469Sdfr */ 65326469Sdfr n = lmin(uio->uio_resid, NFS_DIRBLKSIZ - bp->b_resid - on); 65446349Salc if (np->n_direofoffset && n > np->n_direofoffset - uio->uio_offset) 65546349Salc n = np->n_direofoffset - uio->uio_offset; 6561541Srgrimes break; 6573305Sphk default: 6589336Sdfr printf(" nfs_bioread: type %x unexpected\n",vp->v_type); 6593305Sphk break; 6601541Srgrimes }; 6611541Srgrimes 6621541Srgrimes if (n > 0) { 66334206Sdyson error = uiomove(bp->b_data + on, (int)n, uio); 6641541Srgrimes } 6651541Srgrimes switch (vp->v_type) { 6661541Srgrimes case VREG: 6671541Srgrimes break; 6681541Srgrimes case VLNK: 6691541Srgrimes n = 0; 6701541Srgrimes break; 6711541Srgrimes case VDIR: 67246349Salc /* 67346349Salc * Invalidate buffer if caching is disabled, forcing a 67446349Salc * re-read from the remote later. 67546349Salc */ 6769336Sdfr if (np->n_flag & NQNFSNONCACHE) 6779336Sdfr bp->b_flags |= B_INVAL; 6781541Srgrimes break; 6793305Sphk default: 6809336Sdfr printf(" nfs_bioread: type %x unexpected\n",vp->v_type); 6813305Sphk } 68232755Sdyson brelse(bp); 6831541Srgrimes } while (error == 0 && uio->uio_resid > 0 && n > 0); 6841541Srgrimes return (error); 6851541Srgrimes} 6861541Srgrimes 6871541Srgrimes/* 6881541Srgrimes * Vnode op for write using bio 6891541Srgrimes */ 6901549Srgrimesint 6911541Srgrimesnfs_write(ap) 6921541Srgrimes struct vop_write_args /* { 6931541Srgrimes struct vnode *a_vp; 6941541Srgrimes struct uio *a_uio; 6951541Srgrimes int a_ioflag; 6961541Srgrimes struct ucred *a_cred; 6971541Srgrimes } */ *ap; 6981541Srgrimes{ 69946349Salc int biosize; 70046349Salc struct uio *uio = ap->a_uio; 7011541Srgrimes struct proc *p = uio->uio_procp; 70246349Salc struct vnode *vp = ap->a_vp; 7031541Srgrimes struct nfsnode *np = VTONFS(vp); 70446349Salc struct ucred *cred = ap->a_cred; 7051541Srgrimes int ioflag = ap->a_ioflag; 7061541Srgrimes struct buf *bp; 7071541Srgrimes struct vattr vattr; 7089336Sdfr struct nfsmount *nmp = VFSTONFS(vp->v_mount); 70911921Sphk daddr_t lbn; 71046349Salc int bcount; 7119336Sdfr int n, on, error = 0, iomode, must_commit; 7121541Srgrimes 7131541Srgrimes#ifdef DIAGNOSTIC 7141541Srgrimes if (uio->uio_rw != UIO_WRITE) 7151541Srgrimes panic("nfs_write mode"); 7161541Srgrimes if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc) 7171541Srgrimes panic("nfs_write proc"); 7181541Srgrimes#endif 7191541Srgrimes if (vp->v_type != VREG) 7201541Srgrimes return (EIO); 7211541Srgrimes if (np->n_flag & NWRITEERR) { 7221541Srgrimes np->n_flag &= ~NWRITEERR; 7231541Srgrimes return (np->n_error); 7241541Srgrimes } 72536176Speter if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 && 72636176Speter (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) 7279336Sdfr (void)nfs_fsinfo(nmp, vp, cred, p); 7281541Srgrimes if (ioflag & (IO_APPEND | IO_SYNC)) { 7291541Srgrimes if (np->n_flag & NMODIFIED) { 7301541Srgrimes np->n_attrstamp = 0; 7313305Sphk error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1); 7323305Sphk if (error) 7331541Srgrimes return (error); 7341541Srgrimes } 7351541Srgrimes if (ioflag & IO_APPEND) { 7361541Srgrimes np->n_attrstamp = 0; 7373305Sphk error = VOP_GETATTR(vp, &vattr, cred, p); 7383305Sphk if (error) 7391541Srgrimes return (error); 7401541Srgrimes uio->uio_offset = np->n_size; 7411541Srgrimes } 7421541Srgrimes } 7431541Srgrimes if (uio->uio_offset < 0) 7441541Srgrimes return (EINVAL); 74536473Speter if ((uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize) 74636473Speter return (EFBIG); 7471541Srgrimes if (uio->uio_resid == 0) 7481541Srgrimes return (0); 7491541Srgrimes /* 7501541Srgrimes * Maybe this should be above the vnode op call, but so long as 7511541Srgrimes * file servers have no limits, i don't think it matters 7521541Srgrimes */ 7531541Srgrimes if (p && uio->uio_offset + uio->uio_resid > 7541541Srgrimes p->p_rlimit[RLIMIT_FSIZE].rlim_cur) { 7551541Srgrimes psignal(p, SIGXFSZ); 7561541Srgrimes return (EFBIG); 7571541Srgrimes } 75846349Salc 7599428Sdfr biosize = vp->v_mount->mnt_stat.f_iosize; 76046349Salc 7611541Srgrimes do { 7621541Srgrimes /* 7631541Srgrimes * Check for a valid write lease. 7641541Srgrimes */ 7651541Srgrimes if ((nmp->nm_flag & NFSMNT_NQNFS) && 7669336Sdfr NQNFS_CKINVALID(vp, np, ND_WRITE)) { 7671541Srgrimes do { 7689336Sdfr error = nqnfs_getlease(vp, ND_WRITE, cred, p); 7691541Srgrimes } while (error == NQNFS_EXPIRED); 7701541Srgrimes if (error) 7711541Srgrimes return (error); 7721541Srgrimes if (np->n_lrev != np->n_brev || 7731541Srgrimes (np->n_flag & NQNFSNONCACHE)) { 7743305Sphk error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1); 7753305Sphk if (error) 7761541Srgrimes return (error); 7771541Srgrimes np->n_brev = np->n_lrev; 7781541Srgrimes } 7791541Srgrimes } 7809336Sdfr if ((np->n_flag & NQNFSNONCACHE) && uio->uio_iovcnt == 1) { 7819336Sdfr iomode = NFSV3WRITE_FILESYNC; 7829336Sdfr error = nfs_writerpc(vp, uio, cred, &iomode, &must_commit); 7839336Sdfr if (must_commit) 7849336Sdfr nfs_clearcommit(vp->v_mount); 7859336Sdfr return (error); 7869336Sdfr } 7871541Srgrimes nfsstats.biocache_writes++; 7881541Srgrimes lbn = uio->uio_offset / biosize; 7891541Srgrimes on = uio->uio_offset & (biosize-1); 7901541Srgrimes n = min((unsigned)(biosize - on), uio->uio_resid); 7911541Srgrimesagain: 79246349Salc /* 79346349Salc * Handle direct append and file extension cases, calculate 79446349Salc * unaligned buffer size. 79546349Salc */ 79646349Salc 79746349Salc if (uio->uio_offset == np->n_size && n) { 79846349Salc /* 79946349Salc * special append case. Obtain buffer prior to 80046349Salc * resizing it to maintain B_CACHE. 80146349Salc */ 80246349Salc long save; 80346349Salc 80446349Salc bcount = on; 80546349Salc bp = nfs_getcacheblk(vp, lbn, bcount, p); 80649659Sdt if (!bp) 80749659Sdt return (EINTR); 80846349Salc save = bp->b_flags & B_CACHE; 80946349Salc 8108692Sdg np->n_size = uio->uio_offset + n; 81125023Sdfr np->n_flag |= NMODIFIED; 81241026Speter vnode_pager_setsize(vp, np->n_size); 81346349Salc 81446349Salc bcount += n; 81546349Salc allocbuf(bp, bcount); 81646349Salc bp->b_flags |= save; 81746349Salc } else { 81846349Salc if (uio->uio_offset + n > np->n_size) { 81946349Salc np->n_size = uio->uio_offset + n; 82046349Salc np->n_flag |= NMODIFIED; 82146349Salc vnode_pager_setsize(vp, np->n_size); 82246349Salc } 82346349Salc bcount = biosize; 82446349Salc if ((off_t)(lbn + 1) * biosize > np->n_size) 82546349Salc bcount = np->n_size - (off_t)lbn * biosize; 82646349Salc bp = nfs_getcacheblk(vp, lbn, bcount, p); 82749659Sdt if (!bp) 82849659Sdt return (EINTR); 8298692Sdg } 83046349Salc 83146349Salc /* 83246349Salc * Issue a READ if B_CACHE is not set. In special-append 83346349Salc * mode, B_CACHE is based on the buffer prior to the write 83446349Salc * op and is typically set, avoiding the read. If a read 83546349Salc * is required in special append mode, the server will 83646349Salc * probably send us a short-read since we extended the file 83746349Salc * on our end, resulting in b_resid == 0 and, thusly, 83846349Salc * B_CACHE getting set. 83946349Salc * 84046349Salc * We can also avoid issuing the read if the write covers 84146349Salc * the entire buffer. We have to make sure the buffer state 84246349Salc * is reasonable in this case since we will not be initiating 84346349Salc * I/O. See the comments in kern/vfs_bio.c's getblk() for 84446349Salc * more information. 84546349Salc * 84646349Salc * B_CACHE may also be set due to the buffer being cached 84746349Salc * normally. 84846349Salc */ 84946349Salc 85046349Salc if (on == 0 && n == bcount) { 85146349Salc bp->b_flags |= B_CACHE; 85246349Salc bp->b_flags &= ~(B_ERROR | B_INVAL); 8538692Sdg } 85446349Salc 85546349Salc if ((bp->b_flags & B_CACHE) == 0) { 85646349Salc bp->b_flags |= B_READ; 85746349Salc vfs_busy_pages(bp, 0); 85846349Salc error = nfs_doio(bp, cred, p); 85946349Salc if (error) { 86046349Salc brelse(bp); 86146349Salc return (error); 86246349Salc } 86346349Salc } 8641541Srgrimes if (!bp) 8651541Srgrimes return (EINTR); 8661541Srgrimes if (bp->b_wcred == NOCRED) { 8671541Srgrimes crhold(cred); 8681541Srgrimes bp->b_wcred = cred; 8691541Srgrimes } 8701541Srgrimes np->n_flag |= NMODIFIED; 8718692Sdg 87245347Sjulian /* 87345347Sjulian * If dirtyend exceeds file size, chop it down. If this 87445347Sjulian * creates a reverse-indexed or degenerate situation with 87545347Sjulian * dirtyoff/end, 0 them. 87645347Sjulian */ 87745347Sjulian 87841791Sdt if ((off_t)bp->b_blkno * DEV_BSIZE + bp->b_dirtyend > np->n_size) 87941791Sdt bp->b_dirtyend = np->n_size - (off_t)bp->b_blkno * DEV_BSIZE; 88045347Sjulian if (bp->b_dirtyoff >= bp->b_dirtyend) 88145347Sjulian bp->b_dirtyoff = bp->b_dirtyend = 0; 88231617Sdyson 8831541Srgrimes /* 88431617Sdyson * If the new write will leave a contiguous dirty 88531617Sdyson * area, just update the b_dirtyoff and b_dirtyend, 88631617Sdyson * otherwise force a write rpc of the old dirty area. 88746349Salc * 88846349Salc * While it is possible to merge discontiguous writes due to 88946349Salc * our having a B_CACHE buffer ( and thus valid read data 89046349Salc * for the hole), we don't because it could lead to 89146349Salc * significant cache coherency problems with multiple clients, 89246349Salc * especially if locking is implemented later on. 89346349Salc * 89446349Salc * as an optimization we could theoretically maintain 89546349Salc * a linked list of discontinuous areas, but we would still 89646349Salc * have to commit them separately so there isn't much 89746349Salc * advantage to it except perhaps a bit of asynchronization. 89831617Sdyson */ 89942957Sdillon 90031617Sdyson if (bp->b_dirtyend > 0 && 90131617Sdyson (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) { 90247964Smckusick if (VOP_BWRITE(bp->b_vp, bp) == EINTR) 90331617Sdyson return (EINTR); 90431617Sdyson goto again; 90531617Sdyson } 90631617Sdyson 90731617Sdyson /* 9081541Srgrimes * Check for valid write lease and get one as required. 9091541Srgrimes * In case getblk() and/or bwrite() delayed us. 9101541Srgrimes */ 9111541Srgrimes if ((nmp->nm_flag & NFSMNT_NQNFS) && 9129336Sdfr NQNFS_CKINVALID(vp, np, ND_WRITE)) { 9131541Srgrimes do { 9149336Sdfr error = nqnfs_getlease(vp, ND_WRITE, cred, p); 9151541Srgrimes } while (error == NQNFS_EXPIRED); 9161541Srgrimes if (error) { 9171541Srgrimes brelse(bp); 9181541Srgrimes return (error); 9191541Srgrimes } 9201541Srgrimes if (np->n_lrev != np->n_brev || 9211541Srgrimes (np->n_flag & NQNFSNONCACHE)) { 9221541Srgrimes brelse(bp); 9233305Sphk error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1); 9243305Sphk if (error) 9251541Srgrimes return (error); 9261541Srgrimes np->n_brev = np->n_lrev; 9271541Srgrimes goto again; 9281541Srgrimes } 9291541Srgrimes } 93034206Sdyson 9313305Sphk error = uiomove((char *)bp->b_data + on, n, uio); 93239781Smckusick bp->b_flags &= ~B_NEEDCOMMIT; 9333305Sphk if (error) { 9341541Srgrimes bp->b_flags |= B_ERROR; 9351541Srgrimes brelse(bp); 9361541Srgrimes return (error); 9371541Srgrimes } 93834206Sdyson 93934206Sdyson /* 94045347Sjulian * Only update dirtyoff/dirtyend if not a degenerate 94145347Sjulian * condition. 94245347Sjulian */ 94345347Sjulian if (n) { 94445347Sjulian if (bp->b_dirtyend > 0) { 94545347Sjulian bp->b_dirtyoff = min(on, bp->b_dirtyoff); 94645347Sjulian bp->b_dirtyend = max((on + n), bp->b_dirtyend); 94745347Sjulian } else { 94845347Sjulian bp->b_dirtyoff = on; 94945347Sjulian bp->b_dirtyend = on + n; 95045347Sjulian } 95146349Salc vfs_bio_set_validclean(bp, on, n); 9521541Srgrimes } 95345347Sjulian 95444679Sjulian /* 95517186Sdfr * Since this block is being modified, it must be written 95617186Sdfr * again and not just committed. 95717186Sdfr */ 95817186Sdfr bp->b_flags &= ~B_NEEDCOMMIT; 95917186Sdfr 96017186Sdfr /* 9611541Srgrimes * If the lease is non-cachable or IO_SYNC do bwrite(). 96246349Salc * 96346349Salc * IO_INVAL appears to be unused. The idea appears to be 96446349Salc * to turn off caching in this case. Very odd. XXX 9651541Srgrimes */ 9661541Srgrimes if ((np->n_flag & NQNFSNONCACHE) || (ioflag & IO_SYNC)) { 96734206Sdyson if (ioflag & IO_INVAL) 96846349Salc bp->b_flags |= B_NOCACHE; 96947964Smckusick error = VOP_BWRITE(bp->b_vp, bp); 9703305Sphk if (error) 9711541Srgrimes return (error); 9729336Sdfr if (np->n_flag & NQNFSNONCACHE) { 9739336Sdfr error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1); 9749336Sdfr if (error) 9759336Sdfr return (error); 9769336Sdfr } 9771541Srgrimes } else if ((n + on) == biosize && 9781541Srgrimes (nmp->nm_flag & NFSMNT_NQNFS) == 0) { 9799336Sdfr bp->b_flags |= B_ASYNC; 98046580Sphk (void)nfs_writebp(bp, 0, 0); 98146349Salc } else { 9821541Srgrimes bdwrite(bp); 98346349Salc } 9841541Srgrimes } while (uio->uio_resid > 0 && n > 0); 9851541Srgrimes return (0); 9861541Srgrimes} 9871541Srgrimes 9881541Srgrimes/* 9891541Srgrimes * Get an nfs cache block. 9901541Srgrimes * Allocate a new one if the block isn't currently in the cache 9911541Srgrimes * and return the block marked busy. If the calling process is 9921541Srgrimes * interrupted by a signal for an interruptible mount point, return 9931541Srgrimes * NULL. 9941541Srgrimes */ 99512911Sphkstatic struct buf * 9961541Srgrimesnfs_getcacheblk(vp, bn, size, p) 9971541Srgrimes struct vnode *vp; 9981541Srgrimes daddr_t bn; 9991541Srgrimes int size; 10001541Srgrimes struct proc *p; 10011541Srgrimes{ 10021541Srgrimes register struct buf *bp; 100332755Sdyson struct mount *mp; 100432755Sdyson struct nfsmount *nmp; 10051541Srgrimes 100632755Sdyson mp = vp->v_mount; 100732755Sdyson nmp = VFSTONFS(mp); 100832755Sdyson 10091541Srgrimes if (nmp->nm_flag & NFSMNT_INT) { 10101541Srgrimes bp = getblk(vp, bn, size, PCATCH, 0); 10111541Srgrimes while (bp == (struct buf *)0) { 10121541Srgrimes if (nfs_sigintr(nmp, (struct nfsreq *)0, p)) 10131541Srgrimes return ((struct buf *)0); 10141541Srgrimes bp = getblk(vp, bn, size, 0, 2 * hz); 10151541Srgrimes } 101646349Salc } else { 10171541Srgrimes bp = getblk(vp, bn, size, 0, 0); 101846349Salc } 10195455Sdg 102041791Sdt if (vp->v_type == VREG) { 102132755Sdyson int biosize; 102246349Salc 102332755Sdyson biosize = mp->mnt_stat.f_iosize; 102441791Sdt bp->b_blkno = bn * (biosize / DEV_BSIZE); 102532755Sdyson } 10261541Srgrimes return (bp); 10271541Srgrimes} 10281541Srgrimes 10291541Srgrimes/* 10301541Srgrimes * Flush and invalidate all dirty buffers. If another process is already 10311541Srgrimes * doing the flush, just wait for completion. 10321541Srgrimes */ 10331549Srgrimesint 10341541Srgrimesnfs_vinvalbuf(vp, flags, cred, p, intrflg) 10351541Srgrimes struct vnode *vp; 10361541Srgrimes int flags; 10371541Srgrimes struct ucred *cred; 10381541Srgrimes struct proc *p; 10391541Srgrimes int intrflg; 10401541Srgrimes{ 10411541Srgrimes register struct nfsnode *np = VTONFS(vp); 10421541Srgrimes struct nfsmount *nmp = VFSTONFS(vp->v_mount); 10431541Srgrimes int error = 0, slpflag, slptimeo; 10441541Srgrimes 104532755Sdyson if (vp->v_flag & VXLOCK) { 104632755Sdyson return (0); 104732755Sdyson } 104832755Sdyson 10491541Srgrimes if ((nmp->nm_flag & NFSMNT_INT) == 0) 10501541Srgrimes intrflg = 0; 10511541Srgrimes if (intrflg) { 10521541Srgrimes slpflag = PCATCH; 10531541Srgrimes slptimeo = 2 * hz; 10541541Srgrimes } else { 10551541Srgrimes slpflag = 0; 10561541Srgrimes slptimeo = 0; 10571541Srgrimes } 10581541Srgrimes /* 10591541Srgrimes * First wait for any other process doing a flush to complete. 10601541Srgrimes */ 10611541Srgrimes while (np->n_flag & NFLUSHINPROG) { 10621541Srgrimes np->n_flag |= NFLUSHWANT; 10631541Srgrimes error = tsleep((caddr_t)&np->n_flag, PRIBIO + 2, "nfsvinval", 10641541Srgrimes slptimeo); 10651541Srgrimes if (error && intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, p)) 10661541Srgrimes return (EINTR); 10671541Srgrimes } 10681541Srgrimes 10691541Srgrimes /* 10701541Srgrimes * Now, flush as required. 10711541Srgrimes */ 10721541Srgrimes np->n_flag |= NFLUSHINPROG; 10731541Srgrimes error = vinvalbuf(vp, flags, cred, p, slpflag, 0); 10741541Srgrimes while (error) { 10751541Srgrimes if (intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, p)) { 10761541Srgrimes np->n_flag &= ~NFLUSHINPROG; 10771541Srgrimes if (np->n_flag & NFLUSHWANT) { 10781541Srgrimes np->n_flag &= ~NFLUSHWANT; 10791541Srgrimes wakeup((caddr_t)&np->n_flag); 10801541Srgrimes } 10811541Srgrimes return (EINTR); 10821541Srgrimes } 10831541Srgrimes error = vinvalbuf(vp, flags, cred, p, 0, slptimeo); 10841541Srgrimes } 10851541Srgrimes np->n_flag &= ~(NMODIFIED | NFLUSHINPROG); 10861541Srgrimes if (np->n_flag & NFLUSHWANT) { 10871541Srgrimes np->n_flag &= ~NFLUSHWANT; 10881541Srgrimes wakeup((caddr_t)&np->n_flag); 10891541Srgrimes } 10901541Srgrimes return (0); 10911541Srgrimes} 10921541Srgrimes 10931541Srgrimes/* 10941541Srgrimes * Initiate asynchronous I/O. Return an error if no nfsiods are available. 10951541Srgrimes * This is mainly to avoid queueing async I/O requests when the nfsiods 10961541Srgrimes * are all hung on a dead server. 109746349Salc * 109846349Salc * Note: nfs_asyncio() does not clear (B_ERROR|B_INVAL) but when the bp 109946349Salc * is eventually dequeued by the async daemon, nfs_doio() *will*. 11001541Srgrimes */ 11011549Srgrimesint 110246580Sphknfs_asyncio(bp, cred, procp) 11031541Srgrimes register struct buf *bp; 11041541Srgrimes struct ucred *cred; 110546580Sphk struct proc *procp; 11061541Srgrimes{ 110719449Sdfr struct nfsmount *nmp; 110819449Sdfr int i; 110919449Sdfr int gotiod; 111019449Sdfr int slpflag = 0; 111119449Sdfr int slptimeo = 0; 111219449Sdfr int error; 11131541Srgrimes 11141541Srgrimes if (nfs_numasync == 0) 11151541Srgrimes return (EIO); 111644679Sjulian 111719449Sdfr nmp = VFSTONFS(bp->b_vp->v_mount); 111819449Sdfragain: 111919449Sdfr if (nmp->nm_flag & NFSMNT_INT) 112019449Sdfr slpflag = PCATCH; 112119449Sdfr gotiod = FALSE; 112219449Sdfr 112319449Sdfr /* 112419449Sdfr * Find a free iod to process this request. 112519449Sdfr */ 11261541Srgrimes for (i = 0; i < NFS_MAXASYNCDAEMON; i++) 112719449Sdfr if (nfs_iodwant[i]) { 112819449Sdfr /* 112919449Sdfr * Found one, so wake it up and tell it which 113019449Sdfr * mount to process. 113119449Sdfr */ 113219449Sdfr NFS_DPF(ASYNCIO, 113319449Sdfr ("nfs_asyncio: waking iod %d for mount %p\n", 113419449Sdfr i, nmp)); 113519449Sdfr nfs_iodwant[i] = (struct proc *)0; 113619449Sdfr nfs_iodmount[i] = nmp; 113719449Sdfr nmp->nm_bufqiods++; 113819449Sdfr wakeup((caddr_t)&nfs_iodwant[i]); 113919449Sdfr gotiod = TRUE; 114025023Sdfr break; 114119449Sdfr } 114219449Sdfr 114319449Sdfr /* 114419449Sdfr * If none are free, we may already have an iod working on this mount 114519449Sdfr * point. If so, it will process our request. 114619449Sdfr */ 114719449Sdfr if (!gotiod) { 114819449Sdfr if (nmp->nm_bufqiods > 0) { 114919449Sdfr NFS_DPF(ASYNCIO, 115019449Sdfr ("nfs_asyncio: %d iods are already processing mount %p\n", 115119449Sdfr nmp->nm_bufqiods, nmp)); 115219449Sdfr gotiod = TRUE; 115319449Sdfr } 115419449Sdfr } 115519449Sdfr 115619449Sdfr /* 115719449Sdfr * If we have an iod which can process the request, then queue 115819449Sdfr * the buffer. 115919449Sdfr */ 116019449Sdfr if (gotiod) { 116119449Sdfr /* 116219449Sdfr * Ensure that the queue never grows too large. 116319449Sdfr */ 116419449Sdfr while (nmp->nm_bufqlen >= 2*nfs_numasync) { 116519449Sdfr NFS_DPF(ASYNCIO, 116619449Sdfr ("nfs_asyncio: waiting for mount %p queue to drain\n", nmp)); 116719449Sdfr nmp->nm_bufqwant = TRUE; 116819449Sdfr error = tsleep(&nmp->nm_bufq, slpflag | PRIBIO, 116919449Sdfr "nfsaio", slptimeo); 117019449Sdfr if (error) { 117146580Sphk if (nfs_sigintr(nmp, NULL, procp)) 117219449Sdfr return (EINTR); 117319449Sdfr if (slpflag == PCATCH) { 117419449Sdfr slpflag = 0; 117519449Sdfr slptimeo = 2 * hz; 117619449Sdfr } 117719449Sdfr } 117819449Sdfr /* 117919449Sdfr * We might have lost our iod while sleeping, 118019449Sdfr * so check and loop if nescessary. 118119449Sdfr */ 118219449Sdfr if (nmp->nm_bufqiods == 0) { 118319449Sdfr NFS_DPF(ASYNCIO, 118419449Sdfr ("nfs_asyncio: no iods after mount %p queue was drained, looping\n", nmp)); 118519449Sdfr goto again; 118619449Sdfr } 118719449Sdfr } 118819449Sdfr 11891541Srgrimes if (bp->b_flags & B_READ) { 11901541Srgrimes if (bp->b_rcred == NOCRED && cred != NOCRED) { 11911541Srgrimes crhold(cred); 11921541Srgrimes bp->b_rcred = cred; 11931541Srgrimes } 11941541Srgrimes } else { 11959336Sdfr bp->b_flags |= B_WRITEINPROG; 11961541Srgrimes if (bp->b_wcred == NOCRED && cred != NOCRED) { 11971541Srgrimes crhold(cred); 11981541Srgrimes bp->b_wcred = cred; 11991541Srgrimes } 12001541Srgrimes } 12018876Srgrimes 120248225Smckusick BUF_KERNPROC(bp); 120319449Sdfr TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist); 120419449Sdfr nmp->nm_bufqlen++; 12051541Srgrimes return (0); 120619449Sdfr } 12079336Sdfr 12089336Sdfr /* 120919449Sdfr * All the iods are busy on other mounts, so return EIO to 121019449Sdfr * force the caller to process the i/o synchronously. 12119336Sdfr */ 121219449Sdfr NFS_DPF(ASYNCIO, ("nfs_asyncio: no iods available, i/o is synchronous\n")); 121319449Sdfr return (EIO); 12141541Srgrimes} 12151541Srgrimes 12161541Srgrimes/* 12171541Srgrimes * Do an I/O operation to/from a cache block. This may be called 12181541Srgrimes * synchronously or from an nfsiod. 12191541Srgrimes */ 12201541Srgrimesint 12211541Srgrimesnfs_doio(bp, cr, p) 122244679Sjulian struct buf *bp; 12233305Sphk struct ucred *cr; 12241541Srgrimes struct proc *p; 12251541Srgrimes{ 122644679Sjulian struct uio *uiop; 122744679Sjulian struct vnode *vp; 12281541Srgrimes struct nfsnode *np; 12291541Srgrimes struct nfsmount *nmp; 123046349Salc int error = 0, iomode, must_commit = 0; 12311541Srgrimes struct uio uio; 12321541Srgrimes struct iovec io; 12331541Srgrimes 12341541Srgrimes vp = bp->b_vp; 12351541Srgrimes np = VTONFS(vp); 12361541Srgrimes nmp = VFSTONFS(vp->v_mount); 12371541Srgrimes uiop = &uio; 12381541Srgrimes uiop->uio_iov = &io; 12391541Srgrimes uiop->uio_iovcnt = 1; 12401541Srgrimes uiop->uio_segflg = UIO_SYSSPACE; 12411541Srgrimes uiop->uio_procp = p; 12421541Srgrimes 124346349Salc /* 124446349Salc * clear B_ERROR and B_INVAL state prior to initiating the I/O. We 124546349Salc * do this here so we do not have to do it in all the code that 124646349Salc * calls us. 124746349Salc */ 124846349Salc bp->b_flags &= ~(B_ERROR | B_INVAL); 124946349Salc 125044679Sjulian KASSERT(!(bp->b_flags & B_DONE), ("nfs_doio: bp %p already marked done", bp)); 125144679Sjulian 12521541Srgrimes /* 12531541Srgrimes * Historically, paging was done with physio, but no more. 12541541Srgrimes */ 12553664Sphk if (bp->b_flags & B_PHYS) { 12563664Sphk /* 12573664Sphk * ...though reading /dev/drum still gets us here. 12583664Sphk */ 12591541Srgrimes io.iov_len = uiop->uio_resid = bp->b_bcount; 12603664Sphk /* mapping was done by vmapbuf() */ 12611541Srgrimes io.iov_base = bp->b_data; 12629336Sdfr uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE; 12633664Sphk if (bp->b_flags & B_READ) { 12643664Sphk uiop->uio_rw = UIO_READ; 12653664Sphk nfsstats.read_physios++; 12663664Sphk error = nfs_readrpc(vp, uiop, cr); 12673664Sphk } else { 12689336Sdfr int com; 12699336Sdfr 12709336Sdfr iomode = NFSV3WRITE_DATASYNC; 12713664Sphk uiop->uio_rw = UIO_WRITE; 12723664Sphk nfsstats.write_physios++; 12739336Sdfr error = nfs_writerpc(vp, uiop, cr, &iomode, &com); 12743664Sphk } 12753664Sphk if (error) { 12763664Sphk bp->b_flags |= B_ERROR; 12773664Sphk bp->b_error = error; 12783664Sphk } 12793664Sphk } else if (bp->b_flags & B_READ) { 12803664Sphk io.iov_len = uiop->uio_resid = bp->b_bcount; 12813664Sphk io.iov_base = bp->b_data; 12821541Srgrimes uiop->uio_rw = UIO_READ; 12831541Srgrimes switch (vp->v_type) { 12841541Srgrimes case VREG: 12859336Sdfr uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE; 12861541Srgrimes nfsstats.read_bios++; 12871541Srgrimes error = nfs_readrpc(vp, uiop, cr); 12881541Srgrimes if (!error) { 12891541Srgrimes if (uiop->uio_resid) { 12901541Srgrimes /* 129146349Salc * If we had a short read with no error, we must have 129246349Salc * hit a file hole. We should zero-fill the remainder. 129346349Salc * This can also occur if the server hits the file EOF. 129446349Salc * 129546349Salc * Holes used to be able to occur due to pending 129646349Salc * writes, but that is not possible any longer. 12971541Srgrimes */ 129846349Salc int nread = bp->b_bcount - uiop->uio_resid; 129946349Salc int left = bp->b_bcount - nread; 130046349Salc 130146349Salc if (left > 0) 130246349Salc bzero((char *)bp->b_data + nread, left); 130346349Salc uiop->uio_resid = 0; 130446349Salc } 13051541Srgrimes } 13061541Srgrimes if (p && (vp->v_flag & VTEXT) && 13071541Srgrimes (((nmp->nm_flag & NFSMNT_NQNFS) && 13089336Sdfr NQNFS_CKINVALID(vp, np, ND_READ) && 13091541Srgrimes np->n_lrev != np->n_brev) || 13101541Srgrimes (!(nmp->nm_flag & NFSMNT_NQNFS) && 131118397Snate np->n_mtime != np->n_vattr.va_mtime.tv_sec))) { 13121541Srgrimes uprintf("Process killed due to text file modification\n"); 13131541Srgrimes psignal(p, SIGKILL); 131445361Speter PHOLD(p); 13151541Srgrimes } 13161541Srgrimes break; 13171541Srgrimes case VLNK: 13189336Sdfr uiop->uio_offset = (off_t)0; 13191541Srgrimes nfsstats.readlink_bios++; 13201541Srgrimes error = nfs_readlinkrpc(vp, uiop, cr); 13211541Srgrimes break; 13221541Srgrimes case VDIR: 13231541Srgrimes nfsstats.readdir_bios++; 13249336Sdfr uiop->uio_offset = ((u_quad_t)bp->b_lblkno) * NFS_DIRBLKSIZ; 13259336Sdfr if (nmp->nm_flag & NFSMNT_RDIRPLUS) { 13269336Sdfr error = nfs_readdirplusrpc(vp, uiop, cr); 13279336Sdfr if (error == NFSERR_NOTSUPP) 13289336Sdfr nmp->nm_flag &= ~NFSMNT_RDIRPLUS; 13299336Sdfr } 13309336Sdfr if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0) 13319336Sdfr error = nfs_readdirrpc(vp, uiop, cr); 133246349Salc /* 133346349Salc * end-of-directory sets B_INVAL but does not generate an 133446349Salc * error. 133546349Salc */ 133639782Smckusick if (error == 0 && uiop->uio_resid == bp->b_bcount) 133739782Smckusick bp->b_flags |= B_INVAL; 13381541Srgrimes break; 13393305Sphk default: 13403305Sphk printf("nfs_doio: type %x unexpected\n",vp->v_type); 13413305Sphk break; 13421541Srgrimes }; 13431541Srgrimes if (error) { 13441541Srgrimes bp->b_flags |= B_ERROR; 13451541Srgrimes bp->b_error = error; 13461541Srgrimes } 13471541Srgrimes } else { 134851344Sdillon /* 134951344Sdillon * If we only need to commit, try to commit 135051344Sdillon */ 135151344Sdillon if (bp->b_flags & B_NEEDCOMMIT) { 135251344Sdillon int retv; 135351344Sdillon off_t off; 135451344Sdillon 135551344Sdillon off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE + bp->b_dirtyoff; 135651344Sdillon bp->b_flags |= B_WRITEINPROG; 135751344Sdillon retv = nfs_commit( 135851344Sdillon bp->b_vp, off, bp->b_dirtyend-bp->b_dirtyoff, 135951344Sdillon bp->b_wcred, p); 136051344Sdillon bp->b_flags &= ~B_WRITEINPROG; 136151344Sdillon if (retv == 0) { 136251344Sdillon bp->b_dirtyoff = bp->b_dirtyend = 0; 136351344Sdillon bp->b_flags &= ~B_NEEDCOMMIT; 136451344Sdillon bp->b_resid = 0; 136551344Sdillon biodone(bp); 136651344Sdillon return (0); 136751344Sdillon } 136851344Sdillon if (retv == NFSERR_STALEWRITEVERF) { 136951344Sdillon nfs_clearcommit(bp->b_vp->v_mount); 137051344Sdillon } 137151344Sdillon } 137251344Sdillon 137351344Sdillon /* 137451344Sdillon * Setup for actual write 137551344Sdillon */ 137651344Sdillon 137741791Sdt if ((off_t)bp->b_blkno * DEV_BSIZE + bp->b_dirtyend > np->n_size) 137841791Sdt bp->b_dirtyend = np->n_size - (off_t)bp->b_blkno * DEV_BSIZE; 13798692Sdg 13808692Sdg if (bp->b_dirtyend > bp->b_dirtyoff) { 13818692Sdg io.iov_len = uiop->uio_resid = bp->b_dirtyend 13829336Sdfr - bp->b_dirtyoff; 138341791Sdt uiop->uio_offset = (off_t)bp->b_blkno * DEV_BSIZE 13849336Sdfr + bp->b_dirtyoff; 13858692Sdg io.iov_base = (char *)bp->b_data + bp->b_dirtyoff; 13868692Sdg uiop->uio_rw = UIO_WRITE; 13878692Sdg nfsstats.write_bios++; 138844679Sjulian 138925785Sdfr if ((bp->b_flags & (B_ASYNC | B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == B_ASYNC) 13909336Sdfr iomode = NFSV3WRITE_UNSTABLE; 13918692Sdg else 13929336Sdfr iomode = NFSV3WRITE_FILESYNC; 139344679Sjulian 13949336Sdfr bp->b_flags |= B_WRITEINPROG; 13959336Sdfr error = nfs_writerpc(vp, uiop, cr, &iomode, &must_commit); 139625003Sdfr if (!error && iomode == NFSV3WRITE_UNSTABLE) { 139725003Sdfr bp->b_flags |= B_NEEDCOMMIT; 139825003Sdfr if (bp->b_dirtyoff == 0 139946349Salc && bp->b_dirtyend == bp->b_bcount) 140025003Sdfr bp->b_flags |= B_CLUSTEROK; 140144679Sjulian } else { 14029336Sdfr bp->b_flags &= ~B_NEEDCOMMIT; 140344679Sjulian } 14049336Sdfr bp->b_flags &= ~B_WRITEINPROG; 14058692Sdg 14069336Sdfr /* 14079336Sdfr * For an interrupted write, the buffer is still valid 14089336Sdfr * and the write hasn't been pushed to the server yet, 14099336Sdfr * so we can't set B_ERROR and report the interruption 14109336Sdfr * by setting B_EINTR. For the B_ASYNC case, B_EINTR 14119336Sdfr * is not relevant, so the rpc attempt is essentially 14129336Sdfr * a noop. For the case of a V3 write rpc not being 14139336Sdfr * committed to stable storage, the block is still 14149336Sdfr * dirty and requires either a commit rpc or another 14159336Sdfr * write rpc with iomode == NFSV3WRITE_FILESYNC before 14169336Sdfr * the block is reused. This is indicated by setting 14179336Sdfr * the B_DELWRI and B_NEEDCOMMIT flags. 141842957Sdillon * 141942957Sdillon * If the buffer is marked B_PAGING, it does not reside on 142044679Sjulian * the vp's paging queues so we cannot call bdirty(). The 142144679Sjulian * bp in this case is not an NFS cache block so we should 142244679Sjulian * be safe. XXX 14239336Sdfr */ 14249336Sdfr if (error == EINTR 14259336Sdfr || (!error && (bp->b_flags & B_NEEDCOMMIT))) { 142634266Sjulian int s; 142734266Sjulian 142844679Sjulian s = splbio(); 14298692Sdg bp->b_flags &= ~(B_INVAL|B_NOCACHE); 143042957Sdillon if ((bp->b_flags & B_PAGING) == 0) { 143144679Sjulian bdirty(bp); 143244679Sjulian bp->b_flags &= ~B_DONE; 143342957Sdillon } 143447749Speter if (error && (bp->b_flags & B_ASYNC) == 0) 143532755Sdyson bp->b_flags |= B_EINTR; 143644679Sjulian splx(s); 14378692Sdg } else { 143844679Sjulian if (error) { 143944679Sjulian bp->b_flags |= B_ERROR; 144044679Sjulian bp->b_error = np->n_error = error; 144144679Sjulian np->n_flag |= NWRITEERR; 144244679Sjulian } 144344679Sjulian bp->b_dirtyoff = bp->b_dirtyend = 0; 14448692Sdg } 14451541Srgrimes } else { 14468692Sdg bp->b_resid = 0; 14478692Sdg biodone(bp); 14488692Sdg return (0); 14491541Srgrimes } 14501541Srgrimes } 14511541Srgrimes bp->b_resid = uiop->uio_resid; 14529336Sdfr if (must_commit) 145344679Sjulian nfs_clearcommit(vp->v_mount); 14541541Srgrimes biodone(bp); 14551541Srgrimes return (error); 14561541Srgrimes} 1457