nfs_bio.c revision 115041
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 371541Srgrimes */ 381541Srgrimes 3983651Speter#include <sys/cdefs.h> 4083654Speter__FBSDID("$FreeBSD: head/sys/nfsclient/nfs_bio.c 115041 2003-05-15 21:12:08Z rwatson $"); 4122521Sdyson 421541Srgrimes#include <sys/param.h> 431541Srgrimes#include <sys/systm.h> 4479247Sjhb#include <sys/bio.h> 4579247Sjhb#include <sys/buf.h> 4679247Sjhb#include <sys/kernel.h> 4779247Sjhb#include <sys/mount.h> 4879247Sjhb#include <sys/proc.h> 491541Srgrimes#include <sys/resourcevar.h> 503305Sphk#include <sys/signalvar.h> 5179247Sjhb#include <sys/vmmeter.h> 521541Srgrimes#include <sys/vnode.h> 531541Srgrimes 541541Srgrimes#include <vm/vm.h> 5512662Sdg#include <vm/vm_extern.h> 5625930Sdfr#include <vm/vm_page.h> 5725930Sdfr#include <vm/vm_object.h> 5825930Sdfr#include <vm/vm_pager.h> 5925930Sdfr#include <vm/vnode_pager.h> 601541Srgrimes 611541Srgrimes#include <nfs/rpcv2.h> 629336Sdfr#include <nfs/nfsproto.h> 6383651Speter#include <nfsclient/nfs.h> 6483651Speter#include <nfsclient/nfsmount.h> 6583651Speter#include <nfsclient/nfsnode.h> 661541Srgrimes 6775580Sphk/* 6875580Sphk * Just call nfs_writebp() with the force argument set to 1. 6975580Sphk * 7075580Sphk * NOTE: B_DONE may or may not be set in a_bp on call. 7175580Sphk */ 7275580Sphkstatic int 7375580Sphknfs_bwrite(struct buf *bp) 7475580Sphk{ 7583651Speter 7683366Sjulian return (nfs_writebp(bp, 1, curthread)); 7775580Sphk} 7875580Sphk 7975580Sphkstruct buf_ops buf_ops_nfs = { 8075580Sphk "buf_ops_nfs", 8175580Sphk nfs_bwrite 8275580Sphk}; 8375580Sphk 8483651Speterstatic struct buf *nfs_getcacheblk(struct vnode *vp, daddr_t bn, int size, 8583651Speter struct thread *td); 8675580Sphk 871541Srgrimes/* 8825930Sdfr * Vnode op for VM getpages. 8925930Sdfr */ 9025930Sdfrint 9183651Speternfs_getpages(struct vop_getpages_args *ap) 9225930Sdfr{ 9346349Salc int i, error, nextoff, size, toff, count, npages; 9432755Sdyson struct uio uio; 9532755Sdyson struct iovec iov; 9632755Sdyson vm_offset_t kva; 9734206Sdyson struct buf *bp; 9836563Speter struct vnode *vp; 9983366Sjulian struct thread *td; 10036563Speter struct ucred *cred; 10136563Speter struct nfsmount *nmp; 10236563Speter vm_page_t *pages; 10325930Sdfr 10479224Sdillon GIANT_REQUIRED; 10579224Sdillon 10636563Speter vp = ap->a_vp; 10783366Sjulian td = curthread; /* XXX */ 10891406Sjhb cred = curthread->td_ucred; /* XXX */ 10936563Speter nmp = VFSTONFS(vp->v_mount); 11036563Speter pages = ap->a_m; 11136563Speter count = ap->a_count; 11236563Speter 11336563Speter if (vp->v_object == NULL) { 11432286Sdyson printf("nfs_getpages: called with non-merged cache vnode??\n"); 11536563Speter return VM_PAGER_ERROR; 11625930Sdfr } 11725930Sdfr 11836563Speter if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 && 11976827Salfred (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) { 12083366Sjulian (void)nfs_fsinfo(nmp, vp, cred, td); 12176827Salfred } 12246349Salc 12346349Salc npages = btoc(count); 12446349Salc 12534206Sdyson /* 12646349Salc * If the requested page is partially valid, just return it and 12746349Salc * allow the pager to zero-out the blanks. Partially valid pages 12846349Salc * can only occur at the file EOF. 12946349Salc */ 13046349Salc 13146349Salc { 13246349Salc vm_page_t m = pages[ap->a_reqpage]; 13346349Salc 134100450Salc vm_page_lock_queues(); 13546349Salc if (m->valid != 0) { 13646349Salc /* handled by vm_fault now */ 13746349Salc /* vm_page_zero_invalid(m, TRUE); */ 13846349Salc for (i = 0; i < npages; ++i) { 13946349Salc if (i != ap->a_reqpage) 14075692Salfred vm_page_free(pages[i]); 14146349Salc } 142100450Salc vm_page_unlock_queues(); 14346349Salc return(0); 14446349Salc } 145100450Salc vm_page_unlock_queues(); 14646349Salc } 14746349Salc 14846349Salc /* 14934206Sdyson * We use only the kva address for the buffer, but this is extremely 15034206Sdyson * convienient and fast. 15134206Sdyson */ 15242957Sdillon bp = getpbuf(&nfs_pbuf_freecnt); 15325930Sdfr 15434206Sdyson kva = (vm_offset_t) bp->b_data; 15536563Speter pmap_qenter(kva, pages, npages); 15679247Sjhb cnt.v_vnodein++; 15779247Sjhb cnt.v_vnodepgsin += npages; 15834206Sdyson 15932755Sdyson iov.iov_base = (caddr_t) kva; 16036563Speter iov.iov_len = count; 16132755Sdyson uio.uio_iov = &iov; 16232755Sdyson uio.uio_iovcnt = 1; 16336563Speter uio.uio_offset = IDX_TO_OFF(pages[0]->pindex); 16436563Speter uio.uio_resid = count; 16532755Sdyson uio.uio_segflg = UIO_SYSSPACE; 16632755Sdyson uio.uio_rw = UIO_READ; 16783366Sjulian uio.uio_td = td; 16825930Sdfr 16936563Speter error = nfs_readrpc(vp, &uio, cred); 17034206Sdyson pmap_qremove(kva, npages); 17132755Sdyson 17242957Sdillon relpbuf(bp, &nfs_pbuf_freecnt); 17334206Sdyson 17442957Sdillon if (error && (uio.uio_resid == count)) { 17542957Sdillon printf("nfs_getpages: error %d\n", error); 176100450Salc vm_page_lock_queues(); 17742957Sdillon for (i = 0; i < npages; ++i) { 17842957Sdillon if (i != ap->a_reqpage) 17975692Salfred vm_page_free(pages[i]); 18042957Sdillon } 181100450Salc vm_page_unlock_queues(); 18234206Sdyson return VM_PAGER_ERROR; 18342957Sdillon } 18434206Sdyson 18545347Sjulian /* 18645347Sjulian * Calculate the number of bytes read and validate only that number 18745347Sjulian * of bytes. Note that due to pending writes, size may be 0. This 18845347Sjulian * does not mean that the remaining data is invalid! 18945347Sjulian */ 19045347Sjulian 19136563Speter size = count - uio.uio_resid; 192100450Salc vm_page_lock_queues(); 19334206Sdyson for (i = 0, toff = 0; i < npages; i++, toff = nextoff) { 19434206Sdyson vm_page_t m; 19534206Sdyson nextoff = toff + PAGE_SIZE; 19636563Speter m = pages[i]; 19734206Sdyson 19834206Sdyson m->flags &= ~PG_ZERO; 19934206Sdyson 20034206Sdyson if (nextoff <= size) { 20145347Sjulian /* 20245347Sjulian * Read operation filled an entire page 20345347Sjulian */ 20434206Sdyson m->valid = VM_PAGE_BITS_ALL; 20549945Salc vm_page_undirty(m); 20645347Sjulian } else if (size > toff) { 20745347Sjulian /* 20846349Salc * Read operation filled a partial page. 20945347Sjulian */ 21046349Salc m->valid = 0; 21145347Sjulian vm_page_set_validclean(m, 0, size - toff); 21246349Salc /* handled by vm_fault now */ 21346349Salc /* vm_page_zero_invalid(m, TRUE); */ 21487834Sdillon } else { 21587834Sdillon /* 21687834Sdillon * Read operation was short. If no error occured 21787834Sdillon * we may have hit a zero-fill section. We simply 21887834Sdillon * leave valid set to 0. 21987834Sdillon */ 22087834Sdillon ; 22134206Sdyson } 22225930Sdfr if (i != ap->a_reqpage) { 22334206Sdyson /* 22434206Sdyson * Whether or not to leave the page activated is up in 22534206Sdyson * the air, but we should put the page on a page queue 22634206Sdyson * somewhere (it already is in the object). Result: 22734206Sdyson * It appears that emperical results show that 22834206Sdyson * deactivating pages is best. 22934206Sdyson */ 23034206Sdyson 23134206Sdyson /* 23234206Sdyson * Just in case someone was asking for this page we 23334206Sdyson * now tell them that it is ok to use. 23434206Sdyson */ 23534206Sdyson if (!error) { 23634206Sdyson if (m->flags & PG_WANTED) 23734206Sdyson vm_page_activate(m); 23834206Sdyson else 23934206Sdyson vm_page_deactivate(m); 24038799Sdfr vm_page_wakeup(m); 24134206Sdyson } else { 24275692Salfred vm_page_free(m); 24334206Sdyson } 24425930Sdfr } 24525930Sdfr } 246100450Salc vm_page_unlock_queues(); 24725930Sdfr return 0; 24825930Sdfr} 24925930Sdfr 25025930Sdfr/* 25134206Sdyson * Vnode op for VM putpages. 25234096Smsmith */ 25334096Smsmithint 25483651Speternfs_putpages(struct vop_putpages_args *ap) 25534096Smsmith{ 25634206Sdyson struct uio uio; 25734206Sdyson struct iovec iov; 25834206Sdyson vm_offset_t kva; 25934206Sdyson struct buf *bp; 26036563Speter int iomode, must_commit, i, error, npages, count; 26146349Salc off_t offset; 26234206Sdyson int *rtvals; 26336563Speter struct vnode *vp; 26483366Sjulian struct thread *td; 26536563Speter struct ucred *cred; 26636563Speter struct nfsmount *nmp; 26746349Salc struct nfsnode *np; 26836563Speter vm_page_t *pages; 26934206Sdyson 27079224Sdillon GIANT_REQUIRED; 27179224Sdillon 27236563Speter vp = ap->a_vp; 27346349Salc np = VTONFS(vp); 27483366Sjulian td = curthread; /* XXX */ 27591406Sjhb cred = curthread->td_ucred; /* XXX */ 27636563Speter nmp = VFSTONFS(vp->v_mount); 27736563Speter pages = ap->a_m; 27836563Speter count = ap->a_count; 27934206Sdyson rtvals = ap->a_rtvals; 28036563Speter npages = btoc(count); 28146349Salc offset = IDX_TO_OFF(pages[0]->pindex); 28234206Sdyson 28336563Speter if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 && 28476827Salfred (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) { 28583366Sjulian (void)nfs_fsinfo(nmp, vp, cred, td); 28676827Salfred } 28734206Sdyson 28883651Speter for (i = 0; i < npages; i++) 28934206Sdyson rtvals[i] = VM_PAGER_AGAIN; 29034206Sdyson 29134206Sdyson /* 29246349Salc * When putting pages, do not extend file past EOF. 29346349Salc */ 29446349Salc 29546349Salc if (offset + count > np->n_size) { 29646349Salc count = np->n_size - offset; 29746349Salc if (count < 0) 29846349Salc count = 0; 29946349Salc } 30046349Salc 30146349Salc /* 30234206Sdyson * We use only the kva address for the buffer, but this is extremely 30334206Sdyson * convienient and fast. 30434206Sdyson */ 30542957Sdillon bp = getpbuf(&nfs_pbuf_freecnt); 30634206Sdyson 30734206Sdyson kva = (vm_offset_t) bp->b_data; 30836563Speter pmap_qenter(kva, pages, npages); 30979247Sjhb cnt.v_vnodeout++; 31079247Sjhb cnt.v_vnodepgsout += count; 31134206Sdyson 31234206Sdyson iov.iov_base = (caddr_t) kva; 31336563Speter iov.iov_len = count; 31434206Sdyson uio.uio_iov = &iov; 31534206Sdyson uio.uio_iovcnt = 1; 31646349Salc uio.uio_offset = offset; 31736563Speter uio.uio_resid = count; 31834206Sdyson uio.uio_segflg = UIO_SYSSPACE; 31934206Sdyson uio.uio_rw = UIO_WRITE; 32083366Sjulian uio.uio_td = td; 32134206Sdyson 32234206Sdyson if ((ap->a_sync & VM_PAGER_PUT_SYNC) == 0) 32334206Sdyson iomode = NFSV3WRITE_UNSTABLE; 32434206Sdyson else 32534206Sdyson iomode = NFSV3WRITE_FILESYNC; 32634206Sdyson 32736563Speter error = nfs_writerpc(vp, &uio, cred, &iomode, &must_commit); 32834206Sdyson 32934206Sdyson pmap_qremove(kva, npages); 33042957Sdillon relpbuf(bp, &nfs_pbuf_freecnt); 33134206Sdyson 33234206Sdyson if (!error) { 33336563Speter int nwritten = round_page(count - uio.uio_resid) / PAGE_SIZE; 33434206Sdyson for (i = 0; i < nwritten; i++) { 33534206Sdyson rtvals[i] = VM_PAGER_OK; 33649945Salc vm_page_undirty(pages[i]); 33734206Sdyson } 33876827Salfred if (must_commit) { 33936563Speter nfs_clearcommit(vp->v_mount); 34076827Salfred } 34134206Sdyson } 34236563Speter return rtvals[0]; 34334096Smsmith} 34434096Smsmith 34534096Smsmith/* 3461541Srgrimes * Vnode op for read using bio 3471541Srgrimes */ 3481549Srgrimesint 34983651Speternfs_bioread(struct vnode *vp, struct uio *uio, int ioflag, struct ucred *cred) 3501541Srgrimes{ 35183651Speter struct nfsnode *np = VTONFS(vp); 35283651Speter int biosize, i; 3531549Srgrimes struct buf *bp = 0, *rabp; 3541541Srgrimes struct vattr vattr; 35583366Sjulian struct thread *td; 3569336Sdfr struct nfsmount *nmp = VFSTONFS(vp->v_mount); 3575455Sdg daddr_t lbn, rabn; 35846349Salc int bcount; 35951344Sdillon int seqcount; 36046349Salc int nra, error = 0, n = 0, on = 0; 3611541Srgrimes 3621541Srgrimes#ifdef DIAGNOSTIC 3631541Srgrimes if (uio->uio_rw != UIO_READ) 3641541Srgrimes panic("nfs_read mode"); 3651541Srgrimes#endif 3661541Srgrimes if (uio->uio_resid == 0) 3671541Srgrimes return (0); 36836473Speter if (uio->uio_offset < 0) /* XXX VDIR cookies can be negative */ 3691541Srgrimes return (EINVAL); 37083366Sjulian td = uio->uio_td; 37151344Sdillon 37236176Speter if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 && 37336176Speter (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) 37483366Sjulian (void)nfs_fsinfo(nmp, vp, cred, td); 37536473Speter if (vp->v_type != VDIR && 37636473Speter (uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize) 37736473Speter return (EFBIG); 3789428Sdfr biosize = vp->v_mount->mnt_stat.f_iosize; 379108357Sdillon seqcount = (int)((off_t)(ioflag >> IO_SEQSHIFT) * biosize / BKVASIZE); 3801541Srgrimes /* 3811541Srgrimes * For nfs, cache consistency can only be maintained approximately. 3821541Srgrimes * Although RFC1094 does not specify the criteria, the following is 3831541Srgrimes * believed to be compatible with the reference port. 3841541Srgrimes * For nfs: 3851541Srgrimes * If the file's modify time on the server has changed since the 3861541Srgrimes * last read rpc or you have written to the file, 3871541Srgrimes * you may have lost data cache consistency with the 3881541Srgrimes * server, so flush all of the file's data out of the cache. 3891541Srgrimes * Then force a getattr rpc to ensure that you have up to date 3901541Srgrimes * attributes. 3911541Srgrimes * NB: This implies that cache data can be read when up to 3921541Srgrimes * NFS_ATTRTIMEO seconds out of date. If you find that you need current 3931541Srgrimes * attributes this could be forced by setting n_attrstamp to 0 before 3941541Srgrimes * the VOP_GETATTR() call. 3951541Srgrimes */ 39683651Speter if (np->n_flag & NMODIFIED) { 39783651Speter if (vp->v_type != VREG) { 39883651Speter if (vp->v_type != VDIR) 39983651Speter panic("nfs: bioread, not dir"); 40083651Speter nfs_invaldir(vp); 40183651Speter error = nfs_vinvalbuf(vp, V_SAVE, cred, td, 1); 4023305Sphk if (error) 4031541Srgrimes return (error); 4041541Srgrimes } 40583651Speter np->n_attrstamp = 0; 40683651Speter error = VOP_GETATTR(vp, &vattr, cred, td); 40783651Speter if (error) 4081541Srgrimes return (error); 40983651Speter np->n_mtime = vattr.va_mtime.tv_sec; 41083651Speter } else { 41183651Speter error = VOP_GETATTR(vp, &vattr, cred, td); 41283651Speter if (error) 41383651Speter return (error); 41483651Speter if (np->n_mtime != vattr.va_mtime.tv_sec) { 4159336Sdfr if (vp->v_type == VDIR) 41683651Speter nfs_invaldir(vp); 41783366Sjulian error = nfs_vinvalbuf(vp, V_SAVE, cred, td, 1); 4183305Sphk if (error) 41983651Speter return (error); 42083651Speter np->n_mtime = vattr.va_mtime.tv_sec; 4211541Srgrimes } 42283651Speter } 42383651Speter do { 4241541Srgrimes switch (vp->v_type) { 4251541Srgrimes case VREG: 4261541Srgrimes nfsstats.biocache_reads++; 4271541Srgrimes lbn = uio->uio_offset / biosize; 4289336Sdfr on = uio->uio_offset & (biosize - 1); 4291541Srgrimes 4301541Srgrimes /* 4311541Srgrimes * Start the read ahead(s), as required. 4321541Srgrimes */ 43389324Speter if (nmp->nm_readahead > 0) { 43451344Sdillon for (nra = 0; nra < nmp->nm_readahead && nra < seqcount && 43513612Smpp (off_t)(lbn + 1 + nra) * biosize < np->n_size; nra++) { 4365455Sdg rabn = lbn + 1 + nra; 43799737Sdillon if (incore(vp, rabn) == NULL) { 43883366Sjulian rabp = nfs_getcacheblk(vp, rabn, biosize, td); 4391541Srgrimes if (!rabp) 4401541Srgrimes return (EINTR); 4418692Sdg if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) { 44258345Sphk rabp->b_flags |= B_ASYNC; 44358345Sphk rabp->b_iocmd = BIO_READ; 4445455Sdg vfs_busy_pages(rabp, 0); 44583366Sjulian if (nfs_asyncio(rabp, cred, td)) { 44658934Sphk rabp->b_flags |= B_INVAL; 44758934Sphk rabp->b_ioflags |= BIO_ERROR; 4485455Sdg vfs_unbusy_pages(rabp); 4491541Srgrimes brelse(rabp); 45055431Sdillon break; 4511541Srgrimes } 45255431Sdillon } else { 4535471Sdg brelse(rabp); 45455431Sdillon } 4551541Srgrimes } 4561541Srgrimes } 4571541Srgrimes } 4581541Srgrimes 4591541Srgrimes /* 46046349Salc * Obtain the buffer cache block. Figure out the buffer size 46154605Sdillon * when we are at EOF. If we are modifying the size of the 46283651Speter * buffer based on an EOF condition we need to hold 46354605Sdillon * nfs_rslock() through obtaining the buffer to prevent 46454605Sdillon * a potential writer-appender from messing with n_size. 46554605Sdillon * Otherwise we may accidently truncate the buffer and 46654605Sdillon * lose dirty data. 46746349Salc * 46846349Salc * Note that bcount is *not* DEV_BSIZE aligned. 4691541Srgrimes */ 47046349Salc 47154605Sdillonagain: 47246349Salc bcount = biosize; 47346349Salc if ((off_t)lbn * biosize >= np->n_size) { 47446349Salc bcount = 0; 47546349Salc } else if ((off_t)(lbn + 1) * biosize > np->n_size) { 47646349Salc bcount = np->n_size - (off_t)lbn * biosize; 4778692Sdg } 47855431Sdillon if (bcount != biosize) { 47983366Sjulian switch(nfs_rslock(np, td)) { 48055431Sdillon case ENOLCK: 48155431Sdillon goto again; 48255431Sdillon /* not reached */ 48355431Sdillon case EINTR: 48455431Sdillon case ERESTART: 48555431Sdillon return(EINTR); 48655431Sdillon /* not reached */ 48755431Sdillon default: 48855431Sdillon break; 48955431Sdillon } 49055431Sdillon } 49146349Salc 49283366Sjulian bp = nfs_getcacheblk(vp, lbn, bcount, td); 49354605Sdillon 49454605Sdillon if (bcount != biosize) 49583366Sjulian nfs_rsunlock(np, td); 4967871Sdg if (!bp) 4977871Sdg return (EINTR); 49842957Sdillon 49925930Sdfr /* 50046349Salc * If B_CACHE is not set, we must issue the read. If this 50146349Salc * fails, we return an error. 50225930Sdfr */ 50346349Salc 5047871Sdg if ((bp->b_flags & B_CACHE) == 0) { 50558345Sphk bp->b_iocmd = BIO_READ; 50632755Sdyson vfs_busy_pages(bp, 0); 50783366Sjulian error = nfs_doio(bp, cred, td); 50832755Sdyson if (error) { 50932755Sdyson brelse(bp); 51032755Sdyson return (error); 51132755Sdyson } 5121541Srgrimes } 51346349Salc 51446349Salc /* 51546349Salc * on is the offset into the current bp. Figure out how many 51646349Salc * bytes we can copy out of the bp. Note that bcount is 51746349Salc * NOT DEV_BSIZE aligned. 51846349Salc * 51946349Salc * Then figure out how many bytes we can copy into the uio. 52046349Salc */ 52146349Salc 52246349Salc n = 0; 52346349Salc if (on < bcount) 52446349Salc n = min((unsigned)(bcount - on), uio->uio_resid); 5251541Srgrimes break; 5261541Srgrimes case VLNK: 5271541Srgrimes nfsstats.biocache_readlinks++; 52883366Sjulian bp = nfs_getcacheblk(vp, (daddr_t)0, NFS_MAXPATHLEN, td); 5291541Srgrimes if (!bp) 5301541Srgrimes return (EINTR); 5317871Sdg if ((bp->b_flags & B_CACHE) == 0) { 53258345Sphk bp->b_iocmd = BIO_READ; 53332755Sdyson vfs_busy_pages(bp, 0); 53483366Sjulian error = nfs_doio(bp, cred, td); 53532755Sdyson if (error) { 53658934Sphk bp->b_ioflags |= BIO_ERROR; 53732755Sdyson brelse(bp); 53832755Sdyson return (error); 53932755Sdyson } 5401541Srgrimes } 5411541Srgrimes n = min(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid); 5421541Srgrimes on = 0; 5431541Srgrimes break; 5441541Srgrimes case VDIR: 5451541Srgrimes nfsstats.biocache_readdirs++; 54624577Sdfr if (np->n_direofoffset 54724577Sdfr && uio->uio_offset >= np->n_direofoffset) { 54824577Sdfr return (0); 54924577Sdfr } 55036979Sbde lbn = (uoff_t)uio->uio_offset / NFS_DIRBLKSIZ; 5519336Sdfr on = uio->uio_offset & (NFS_DIRBLKSIZ - 1); 55283366Sjulian bp = nfs_getcacheblk(vp, lbn, NFS_DIRBLKSIZ, td); 5531541Srgrimes if (!bp) 5549336Sdfr return (EINTR); 5557871Sdg if ((bp->b_flags & B_CACHE) == 0) { 55658345Sphk bp->b_iocmd = BIO_READ; 5579336Sdfr vfs_busy_pages(bp, 0); 55883366Sjulian error = nfs_doio(bp, cred, td); 55932912Stegge if (error) { 56032912Stegge brelse(bp); 56132912Stegge } 56232755Sdyson while (error == NFSERR_BAD_COOKIE) { 56346349Salc printf("got bad cookie vp %p bp %p\n", vp, bp); 56432755Sdyson nfs_invaldir(vp); 56583366Sjulian error = nfs_vinvalbuf(vp, 0, cred, td, 1); 56632755Sdyson /* 56732755Sdyson * Yuck! The directory has been modified on the 56832755Sdyson * server. The only way to get the block is by 56932755Sdyson * reading from the beginning to get all the 57032755Sdyson * offset cookies. 57146349Salc * 57246349Salc * Leave the last bp intact unless there is an error. 57346349Salc * Loop back up to the while if the error is another 57446349Salc * NFSERR_BAD_COOKIE (double yuch!). 57532755Sdyson */ 57632755Sdyson for (i = 0; i <= lbn && !error; i++) { 57732755Sdyson if (np->n_direofoffset 57832755Sdyson && (i * NFS_DIRBLKSIZ) >= np->n_direofoffset) 57924577Sdfr return (0); 58083366Sjulian bp = nfs_getcacheblk(vp, i, NFS_DIRBLKSIZ, td); 58132755Sdyson if (!bp) 58232755Sdyson return (EINTR); 58346349Salc if ((bp->b_flags & B_CACHE) == 0) { 58458345Sphk bp->b_iocmd = BIO_READ; 58546349Salc vfs_busy_pages(bp, 0); 58683366Sjulian error = nfs_doio(bp, cred, td); 58746349Salc /* 58846349Salc * no error + B_INVAL == directory EOF, 58946349Salc * use the block. 59046349Salc */ 59146349Salc if (error == 0 && (bp->b_flags & B_INVAL)) 59246349Salc break; 59346349Salc } 59446349Salc /* 59546349Salc * An error will throw away the block and the 59646349Salc * for loop will break out. If no error and this 59746349Salc * is not the block we want, we throw away the 59846349Salc * block and go for the next one via the for loop. 59946349Salc */ 60046349Salc if (error || i < lbn) 60132755Sdyson brelse(bp); 6021541Srgrimes } 60332912Stegge } 60446349Salc /* 60546349Salc * The above while is repeated if we hit another cookie 60646349Salc * error. If we hit an error and it wasn't a cookie error, 60746349Salc * we give up. 60846349Salc */ 60932912Stegge if (error) 6109336Sdfr return (error); 6111541Srgrimes } 6121541Srgrimes 6131541Srgrimes /* 6141541Srgrimes * If not eof and read aheads are enabled, start one. 6151541Srgrimes * (You need the current block first, so that you have the 6169336Sdfr * directory offset cookie of the next block.) 6171541Srgrimes */ 61889324Speter if (nmp->nm_readahead > 0 && 61939782Smckusick (bp->b_flags & B_INVAL) == 0 && 6209336Sdfr (np->n_direofoffset == 0 || 6219336Sdfr (lbn + 1) * NFS_DIRBLKSIZ < np->n_direofoffset) && 62299737Sdillon incore(vp, lbn + 1) == NULL) { 62383366Sjulian rabp = nfs_getcacheblk(vp, lbn + 1, NFS_DIRBLKSIZ, td); 6241541Srgrimes if (rabp) { 6258692Sdg if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) { 62658345Sphk rabp->b_flags |= B_ASYNC; 62758345Sphk rabp->b_iocmd = BIO_READ; 6285455Sdg vfs_busy_pages(rabp, 0); 62983366Sjulian if (nfs_asyncio(rabp, cred, td)) { 63058934Sphk rabp->b_flags |= B_INVAL; 63158934Sphk rabp->b_ioflags |= BIO_ERROR; 6325455Sdg vfs_unbusy_pages(rabp); 6331541Srgrimes brelse(rabp); 6341541Srgrimes } 6355471Sdg } else { 6365471Sdg brelse(rabp); 6371541Srgrimes } 6381541Srgrimes } 6391541Srgrimes } 64026469Sdfr /* 64146349Salc * Unlike VREG files, whos buffer size ( bp->b_bcount ) is 64246349Salc * chopped for the EOF condition, we cannot tell how large 64346349Salc * NFS directories are going to be until we hit EOF. So 64446349Salc * an NFS directory buffer is *not* chopped to its EOF. Now, 64546349Salc * it just so happens that b_resid will effectively chop it 64646349Salc * to EOF. *BUT* this information is lost if the buffer goes 64746349Salc * away and is reconstituted into a B_CACHE state ( due to 64846349Salc * being VMIO ) later. So we keep track of the directory eof 64983651Speter * in np->n_direofoffset and chop it off as an extra step 65046349Salc * right here. 65126469Sdfr */ 65226469Sdfr n = lmin(uio->uio_resid, NFS_DIRBLKSIZ - bp->b_resid - on); 65346349Salc if (np->n_direofoffset && n > np->n_direofoffset - uio->uio_offset) 65446349Salc n = np->n_direofoffset - uio->uio_offset; 6551541Srgrimes break; 6563305Sphk default: 65783651Speter printf(" nfs_bioread: type %x unexpected\n", vp->v_type); 6583305Sphk break; 6591541Srgrimes }; 6601541Srgrimes 6611541Srgrimes if (n > 0) { 66234206Sdyson error = uiomove(bp->b_data + on, (int)n, uio); 6631541Srgrimes } 6641541Srgrimes switch (vp->v_type) { 6651541Srgrimes case VREG: 6661541Srgrimes break; 6671541Srgrimes case VLNK: 6681541Srgrimes n = 0; 6691541Srgrimes break; 6701541Srgrimes case VDIR: 6711541Srgrimes break; 6723305Sphk default: 67383651Speter printf(" nfs_bioread: type %x unexpected\n", vp->v_type); 6743305Sphk } 67532755Sdyson brelse(bp); 6761541Srgrimes } while (error == 0 && uio->uio_resid > 0 && n > 0); 6771541Srgrimes return (error); 6781541Srgrimes} 6791541Srgrimes 6801541Srgrimes/* 6811541Srgrimes * Vnode op for write using bio 6821541Srgrimes */ 6831549Srgrimesint 68483651Speternfs_write(struct vop_write_args *ap) 6851541Srgrimes{ 68646349Salc int biosize; 68746349Salc struct uio *uio = ap->a_uio; 68883366Sjulian struct thread *td = uio->uio_td; 68946349Salc struct vnode *vp = ap->a_vp; 6901541Srgrimes struct nfsnode *np = VTONFS(vp); 69146349Salc struct ucred *cred = ap->a_cred; 6921541Srgrimes int ioflag = ap->a_ioflag; 6931541Srgrimes struct buf *bp; 6941541Srgrimes struct vattr vattr; 6959336Sdfr struct nfsmount *nmp = VFSTONFS(vp->v_mount); 69611921Sphk daddr_t lbn; 69746349Salc int bcount; 69883651Speter int n, on, error = 0; 69954605Sdillon int haverslock = 0; 70083366Sjulian struct proc *p = td?td->td_proc:NULL; 7011541Srgrimes 70279224Sdillon GIANT_REQUIRED; 70379224Sdillon 7041541Srgrimes#ifdef DIAGNOSTIC 7051541Srgrimes if (uio->uio_rw != UIO_WRITE) 7061541Srgrimes panic("nfs_write mode"); 70783366Sjulian if (uio->uio_segflg == UIO_USERSPACE && uio->uio_td != curthread) 7081541Srgrimes panic("nfs_write proc"); 7091541Srgrimes#endif 7101541Srgrimes if (vp->v_type != VREG) 7111541Srgrimes return (EIO); 7121541Srgrimes if (np->n_flag & NWRITEERR) { 7131541Srgrimes np->n_flag &= ~NWRITEERR; 7141541Srgrimes return (np->n_error); 7151541Srgrimes } 71636176Speter if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 && 71736176Speter (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) 71883366Sjulian (void)nfs_fsinfo(nmp, vp, cred, td); 71954605Sdillon 72054605Sdillon /* 72154605Sdillon * Synchronously flush pending buffers if we are in synchronous 72254605Sdillon * mode or if we are appending. 72354605Sdillon */ 7241541Srgrimes if (ioflag & (IO_APPEND | IO_SYNC)) { 7251541Srgrimes if (np->n_flag & NMODIFIED) { 7261541Srgrimes np->n_attrstamp = 0; 72783366Sjulian error = nfs_vinvalbuf(vp, V_SAVE, cred, td, 1); 7283305Sphk if (error) 7291541Srgrimes return (error); 7301541Srgrimes } 7311541Srgrimes } 73254605Sdillon 73354605Sdillon /* 73454605Sdillon * If IO_APPEND then load uio_offset. We restart here if we cannot 73554605Sdillon * get the append lock. 73654605Sdillon */ 73754605Sdillonrestart: 73854605Sdillon if (ioflag & IO_APPEND) { 73954605Sdillon np->n_attrstamp = 0; 74083366Sjulian error = VOP_GETATTR(vp, &vattr, cred, td); 74154605Sdillon if (error) 74254605Sdillon return (error); 74354605Sdillon uio->uio_offset = np->n_size; 74454605Sdillon } 74554605Sdillon 7461541Srgrimes if (uio->uio_offset < 0) 7471541Srgrimes return (EINVAL); 74836473Speter if ((uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize) 74936473Speter return (EFBIG); 7501541Srgrimes if (uio->uio_resid == 0) 7511541Srgrimes return (0); 75254605Sdillon 7531541Srgrimes /* 75454605Sdillon * We need to obtain the rslock if we intend to modify np->n_size 75554605Sdillon * in order to guarentee the append point with multiple contending 75654605Sdillon * writers, to guarentee that no other appenders modify n_size 75754605Sdillon * while we are trying to obtain a truncated buffer (i.e. to avoid 75854605Sdillon * accidently truncating data written by another appender due to 75954605Sdillon * the race), and to ensure that the buffer is populated prior to 76054605Sdillon * our extending of the file. We hold rslock through the entire 76154605Sdillon * operation. 76254605Sdillon * 76354605Sdillon * Note that we do not synchronize the case where someone truncates 76454605Sdillon * the file while we are appending to it because attempting to lock 76554605Sdillon * this case may deadlock other parts of the system unexpectedly. 76654605Sdillon */ 76754605Sdillon if ((ioflag & IO_APPEND) || 76854605Sdillon uio->uio_offset + uio->uio_resid > np->n_size) { 76983366Sjulian switch(nfs_rslock(np, td)) { 77055431Sdillon case ENOLCK: 77154605Sdillon goto restart; 77255431Sdillon /* not reached */ 77355431Sdillon case EINTR: 77455431Sdillon case ERESTART: 77555431Sdillon return(EINTR); 77655431Sdillon /* not reached */ 77755431Sdillon default: 77855431Sdillon break; 77955431Sdillon } 78054605Sdillon haverslock = 1; 78154605Sdillon } 78254605Sdillon 78354605Sdillon /* 7841541Srgrimes * Maybe this should be above the vnode op call, but so long as 7851541Srgrimes * file servers have no limits, i don't think it matters 7861541Srgrimes */ 7871541Srgrimes if (p && uio->uio_offset + uio->uio_resid > 7881541Srgrimes p->p_rlimit[RLIMIT_FSIZE].rlim_cur) { 78973929Sjhb PROC_LOCK(p); 7901541Srgrimes psignal(p, SIGXFSZ); 79173929Sjhb PROC_UNLOCK(p); 79254605Sdillon if (haverslock) 79383366Sjulian nfs_rsunlock(np, td); 7941541Srgrimes return (EFBIG); 7951541Srgrimes } 79646349Salc 7979428Sdfr biosize = vp->v_mount->mnt_stat.f_iosize; 79846349Salc 7991541Srgrimes do { 8001541Srgrimes nfsstats.biocache_writes++; 8011541Srgrimes lbn = uio->uio_offset / biosize; 8021541Srgrimes on = uio->uio_offset & (biosize-1); 8031541Srgrimes n = min((unsigned)(biosize - on), uio->uio_resid); 8041541Srgrimesagain: 80546349Salc /* 80646349Salc * Handle direct append and file extension cases, calculate 80746349Salc * unaligned buffer size. 80846349Salc */ 80946349Salc 81046349Salc if (uio->uio_offset == np->n_size && n) { 81146349Salc /* 81254605Sdillon * Get the buffer (in its pre-append state to maintain 81354605Sdillon * B_CACHE if it was previously set). Resize the 81454605Sdillon * nfsnode after we have locked the buffer to prevent 81554605Sdillon * readers from reading garbage. 81646349Salc */ 81746349Salc bcount = on; 81883366Sjulian bp = nfs_getcacheblk(vp, lbn, bcount, td); 81946349Salc 82054605Sdillon if (bp != NULL) { 82154605Sdillon long save; 82246349Salc 82354605Sdillon np->n_size = uio->uio_offset + n; 82454605Sdillon np->n_flag |= NMODIFIED; 82554605Sdillon vnode_pager_setsize(vp, np->n_size); 82654605Sdillon 82754605Sdillon save = bp->b_flags & B_CACHE; 82854605Sdillon bcount += n; 82954605Sdillon allocbuf(bp, bcount); 83054605Sdillon bp->b_flags |= save; 83175580Sphk bp->b_magic = B_MAGIC_NFS; 83275580Sphk bp->b_op = &buf_ops_nfs; 83354605Sdillon } 83446349Salc } else { 83554605Sdillon /* 83683651Speter * Obtain the locked cache block first, and then 83754605Sdillon * adjust the file's size as appropriate. 83854605Sdillon */ 83954605Sdillon bcount = on + n; 84054605Sdillon if ((off_t)lbn * biosize + bcount < np->n_size) { 84154605Sdillon if ((off_t)(lbn + 1) * biosize < np->n_size) 84254605Sdillon bcount = biosize; 84354605Sdillon else 84454605Sdillon bcount = np->n_size - (off_t)lbn * biosize; 84554605Sdillon } 84683366Sjulian bp = nfs_getcacheblk(vp, lbn, bcount, td); 84746349Salc if (uio->uio_offset + n > np->n_size) { 84846349Salc np->n_size = uio->uio_offset + n; 84946349Salc np->n_flag |= NMODIFIED; 85046349Salc vnode_pager_setsize(vp, np->n_size); 85146349Salc } 8528692Sdg } 85346349Salc 85454605Sdillon if (!bp) { 85554605Sdillon error = EINTR; 85654605Sdillon break; 85754605Sdillon } 85854605Sdillon 85946349Salc /* 86046349Salc * Issue a READ if B_CACHE is not set. In special-append 86146349Salc * mode, B_CACHE is based on the buffer prior to the write 86246349Salc * op and is typically set, avoiding the read. If a read 86346349Salc * is required in special append mode, the server will 86446349Salc * probably send us a short-read since we extended the file 86583651Speter * on our end, resulting in b_resid == 0 and, thusly, 86646349Salc * B_CACHE getting set. 86746349Salc * 86846349Salc * We can also avoid issuing the read if the write covers 86946349Salc * the entire buffer. We have to make sure the buffer state 87046349Salc * is reasonable in this case since we will not be initiating 87146349Salc * I/O. See the comments in kern/vfs_bio.c's getblk() for 87246349Salc * more information. 87346349Salc * 87446349Salc * B_CACHE may also be set due to the buffer being cached 87546349Salc * normally. 87646349Salc */ 87746349Salc 87846349Salc if (on == 0 && n == bcount) { 87946349Salc bp->b_flags |= B_CACHE; 88058934Sphk bp->b_flags &= ~B_INVAL; 88158934Sphk bp->b_ioflags &= ~BIO_ERROR; 8828692Sdg } 88346349Salc 88446349Salc if ((bp->b_flags & B_CACHE) == 0) { 88558345Sphk bp->b_iocmd = BIO_READ; 88646349Salc vfs_busy_pages(bp, 0); 88783366Sjulian error = nfs_doio(bp, cred, td); 88846349Salc if (error) { 88946349Salc brelse(bp); 89054605Sdillon break; 89146349Salc } 89246349Salc } 89354605Sdillon if (!bp) { 89454605Sdillon error = EINTR; 89554605Sdillon break; 89654605Sdillon } 89784827Sjhb if (bp->b_wcred == NOCRED) 89884827Sjhb bp->b_wcred = crhold(cred); 8991541Srgrimes np->n_flag |= NMODIFIED; 9008692Sdg 90145347Sjulian /* 90254605Sdillon * If dirtyend exceeds file size, chop it down. This should 90354605Sdillon * not normally occur but there is an append race where it 90483651Speter * might occur XXX, so we log it. 90554605Sdillon * 90654605Sdillon * If the chopping creates a reverse-indexed or degenerate 90754605Sdillon * situation with dirtyoff/end, we 0 both of them. 90845347Sjulian */ 90945347Sjulian 91054605Sdillon if (bp->b_dirtyend > bcount) { 91183651Speter printf("NFS append race @%lx:%d\n", 91283651Speter (long)bp->b_blkno * DEV_BSIZE, 91354605Sdillon bp->b_dirtyend - bcount); 91454605Sdillon bp->b_dirtyend = bcount; 91554605Sdillon } 91654605Sdillon 91745347Sjulian if (bp->b_dirtyoff >= bp->b_dirtyend) 91845347Sjulian bp->b_dirtyoff = bp->b_dirtyend = 0; 91931617Sdyson 9201541Srgrimes /* 92131617Sdyson * If the new write will leave a contiguous dirty 92231617Sdyson * area, just update the b_dirtyoff and b_dirtyend, 92331617Sdyson * otherwise force a write rpc of the old dirty area. 92446349Salc * 92583651Speter * While it is possible to merge discontiguous writes due to 92646349Salc * our having a B_CACHE buffer ( and thus valid read data 92783651Speter * for the hole), we don't because it could lead to 92846349Salc * significant cache coherency problems with multiple clients, 92946349Salc * especially if locking is implemented later on. 93046349Salc * 93146349Salc * as an optimization we could theoretically maintain 93246349Salc * a linked list of discontinuous areas, but we would still 93346349Salc * have to commit them separately so there isn't much 93446349Salc * advantage to it except perhaps a bit of asynchronization. 93531617Sdyson */ 93642957Sdillon 93731617Sdyson if (bp->b_dirtyend > 0 && 93831617Sdyson (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) { 939100194Sdillon if (BUF_WRITE(bp) == EINTR) { 940100194Sdillon error = EINTR; 941100194Sdillon break; 942100194Sdillon } 94331617Sdyson goto again; 94431617Sdyson } 94531617Sdyson 9463305Sphk error = uiomove((char *)bp->b_data + on, n, uio); 94754480Sdillon 94854480Sdillon /* 94954480Sdillon * Since this block is being modified, it must be written 95054480Sdillon * again and not just committed. Since write clustering does 95154480Sdillon * not work for the stage 1 data write, only the stage 2 95254480Sdillon * commit rpc, we have to clear B_CLUSTEROK as well. 95354480Sdillon */ 95454480Sdillon bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK); 95554480Sdillon 9563305Sphk if (error) { 95758934Sphk bp->b_ioflags |= BIO_ERROR; 9581541Srgrimes brelse(bp); 95954605Sdillon break; 9601541Srgrimes } 96134206Sdyson 96234206Sdyson /* 96383651Speter * Only update dirtyoff/dirtyend if not a degenerate 96445347Sjulian * condition. 96545347Sjulian */ 96645347Sjulian if (n) { 96745347Sjulian if (bp->b_dirtyend > 0) { 96845347Sjulian bp->b_dirtyoff = min(on, bp->b_dirtyoff); 96945347Sjulian bp->b_dirtyend = max((on + n), bp->b_dirtyend); 97045347Sjulian } else { 97145347Sjulian bp->b_dirtyoff = on; 97245347Sjulian bp->b_dirtyend = on + n; 97345347Sjulian } 97446349Salc vfs_bio_set_validclean(bp, on, n); 9751541Srgrimes } 97686089Sdillon /* 97786089Sdillon * If IO_NOWDRAIN then set B_NOWDRAIN (nfs-backed MD 97886089Sdillon * filesystem) 97986089Sdillon */ 98086089Sdillon if (ioflag & IO_NOWDRAIN) 98186089Sdillon bp->b_flags |= B_NOWDRAIN; 98245347Sjulian 98344679Sjulian /* 98483651Speter * If IO_SYNC do bwrite(). 98546349Salc * 98646349Salc * IO_INVAL appears to be unused. The idea appears to be 98746349Salc * to turn off caching in this case. Very odd. XXX 9881541Srgrimes */ 98983651Speter if ((ioflag & IO_SYNC)) { 99034206Sdyson if (ioflag & IO_INVAL) 99146349Salc bp->b_flags |= B_NOCACHE; 99258349Sphk error = BUF_WRITE(bp); 9933305Sphk if (error) 99454605Sdillon break; 99583651Speter } else if ((n + on) == biosize) { 9969336Sdfr bp->b_flags |= B_ASYNC; 99746580Sphk (void)nfs_writebp(bp, 0, 0); 99846349Salc } else { 9991541Srgrimes bdwrite(bp); 100046349Salc } 10011541Srgrimes } while (uio->uio_resid > 0 && n > 0); 100254605Sdillon 100354605Sdillon if (haverslock) 100483366Sjulian nfs_rsunlock(np, td); 100554605Sdillon 100654605Sdillon return (error); 10071541Srgrimes} 10081541Srgrimes 10091541Srgrimes/* 10101541Srgrimes * Get an nfs cache block. 101154480Sdillon * 10121541Srgrimes * Allocate a new one if the block isn't currently in the cache 10131541Srgrimes * and return the block marked busy. If the calling process is 10141541Srgrimes * interrupted by a signal for an interruptible mount point, return 10151541Srgrimes * NULL. 101654480Sdillon * 101754480Sdillon * The caller must carefully deal with the possible B_INVAL state of 101854480Sdillon * the buffer. nfs_doio() clears B_INVAL (and nfs_asyncio() clears it 101954480Sdillon * indirectly), so synchronous reads can be issued without worrying about 102054480Sdillon * the B_INVAL state. We have to be a little more careful when dealing 102154480Sdillon * with writes (see comments in nfs_write()) when extending a file past 102254480Sdillon * its EOF. 10231541Srgrimes */ 102412911Sphkstatic struct buf * 102583651Speternfs_getcacheblk(struct vnode *vp, daddr_t bn, int size, struct thread *td) 10261541Srgrimes{ 102783651Speter struct buf *bp; 102832755Sdyson struct mount *mp; 102932755Sdyson struct nfsmount *nmp; 10301541Srgrimes 103132755Sdyson mp = vp->v_mount; 103232755Sdyson nmp = VFSTONFS(mp); 103332755Sdyson 10341541Srgrimes if (nmp->nm_flag & NFSMNT_INT) { 1035111856Sjeff bp = getblk(vp, bn, size, PCATCH, 0, 0); 103699797Sdillon while (bp == NULL) { 103799797Sdillon if (nfs_sigintr(nmp, NULL, td)) 103899797Sdillon return (NULL); 1039111856Sjeff bp = getblk(vp, bn, size, 0, 2 * hz, 0); 10401541Srgrimes } 104146349Salc } else { 1042111856Sjeff bp = getblk(vp, bn, size, 0, 0, 0); 104346349Salc } 10445455Sdg 104541791Sdt if (vp->v_type == VREG) { 104632755Sdyson int biosize; 104746349Salc 104832755Sdyson biosize = mp->mnt_stat.f_iosize; 104941791Sdt bp->b_blkno = bn * (biosize / DEV_BSIZE); 105032755Sdyson } 10511541Srgrimes return (bp); 10521541Srgrimes} 10531541Srgrimes 10541541Srgrimes/* 10551541Srgrimes * Flush and invalidate all dirty buffers. If another process is already 10561541Srgrimes * doing the flush, just wait for completion. 10571541Srgrimes */ 10581549Srgrimesint 105983651Speternfs_vinvalbuf(struct vnode *vp, int flags, struct ucred *cred, 106083651Speter struct thread *td, int intrflg) 10611541Srgrimes{ 106283651Speter struct nfsnode *np = VTONFS(vp); 10631541Srgrimes struct nfsmount *nmp = VFSTONFS(vp->v_mount); 10641541Srgrimes int error = 0, slpflag, slptimeo; 10651541Srgrimes 1066115041Srwatson ASSERT_VOP_LOCKED(vp, "nfs_vinvalbuf"); 1067115041Srwatson 1068101308Sjeff VI_LOCK(vp); 1069101308Sjeff if (vp->v_iflag & VI_XLOCK) { 1070101308Sjeff /* XXX Should we wait here? */ 1071101308Sjeff VI_UNLOCK(vp); 107232755Sdyson return (0); 107332755Sdyson } 1074101308Sjeff VI_UNLOCK(vp); 107532755Sdyson 10761541Srgrimes if ((nmp->nm_flag & NFSMNT_INT) == 0) 10771541Srgrimes intrflg = 0; 10781541Srgrimes if (intrflg) { 10791541Srgrimes slpflag = PCATCH; 10801541Srgrimes slptimeo = 2 * hz; 10811541Srgrimes } else { 10821541Srgrimes slpflag = 0; 10831541Srgrimes slptimeo = 0; 10841541Srgrimes } 10851541Srgrimes /* 10861541Srgrimes * First wait for any other process doing a flush to complete. 10871541Srgrimes */ 10881541Srgrimes while (np->n_flag & NFLUSHINPROG) { 10891541Srgrimes np->n_flag |= NFLUSHWANT; 1090111748Sdes error = tsleep(&np->n_flag, PRIBIO + 2, "nfsvinval", 10911541Srgrimes slptimeo); 109283651Speter if (error && intrflg && 109399797Sdillon nfs_sigintr(nmp, NULL, td)) 10941541Srgrimes return (EINTR); 10951541Srgrimes } 10961541Srgrimes 10971541Srgrimes /* 10981541Srgrimes * Now, flush as required. 10991541Srgrimes */ 11001541Srgrimes np->n_flag |= NFLUSHINPROG; 110183366Sjulian error = vinvalbuf(vp, flags, cred, td, slpflag, 0); 11021541Srgrimes while (error) { 110383651Speter if (intrflg && 110499797Sdillon nfs_sigintr(nmp, NULL, td)) { 11051541Srgrimes np->n_flag &= ~NFLUSHINPROG; 11061541Srgrimes if (np->n_flag & NFLUSHWANT) { 11071541Srgrimes np->n_flag &= ~NFLUSHWANT; 1108111748Sdes wakeup(&np->n_flag); 11091541Srgrimes } 11101541Srgrimes return (EINTR); 11111541Srgrimes } 111283366Sjulian error = vinvalbuf(vp, flags, cred, td, 0, slptimeo); 11131541Srgrimes } 11141541Srgrimes np->n_flag &= ~(NMODIFIED | NFLUSHINPROG); 11151541Srgrimes if (np->n_flag & NFLUSHWANT) { 11161541Srgrimes np->n_flag &= ~NFLUSHWANT; 1117111748Sdes wakeup(&np->n_flag); 11181541Srgrimes } 11191541Srgrimes return (0); 11201541Srgrimes} 11211541Srgrimes 11221541Srgrimes/* 11231541Srgrimes * Initiate asynchronous I/O. Return an error if no nfsiods are available. 11241541Srgrimes * This is mainly to avoid queueing async I/O requests when the nfsiods 11251541Srgrimes * are all hung on a dead server. 112646349Salc * 112758934Sphk * Note: nfs_asyncio() does not clear (BIO_ERROR|B_INVAL) but when the bp 112846349Salc * is eventually dequeued by the async daemon, nfs_doio() *will*. 11291541Srgrimes */ 11301549Srgrimesint 113183651Speternfs_asyncio(struct buf *bp, struct ucred *cred, struct thread *td) 11321541Srgrimes{ 113319449Sdfr struct nfsmount *nmp; 113489324Speter int iod; 113519449Sdfr int gotiod; 113619449Sdfr int slpflag = 0; 113719449Sdfr int slptimeo = 0; 113819449Sdfr int error; 11391541Srgrimes 114019449Sdfr nmp = VFSTONFS(bp->b_vp->v_mount); 114155431Sdillon 114255431Sdillon /* 114383651Speter * Commits are usually short and sweet so lets save some cpu and 114455431Sdillon * leave the async daemons for more important rpc's (such as reads 114555431Sdillon * and writes). 114655431Sdillon */ 114758345Sphk if (bp->b_iocmd == BIO_WRITE && (bp->b_flags & B_NEEDCOMMIT) && 114855431Sdillon (nmp->nm_bufqiods > nfs_numasync / 2)) { 114955431Sdillon return(EIO); 115055431Sdillon } 115155431Sdillon 115219449Sdfragain: 115319449Sdfr if (nmp->nm_flag & NFSMNT_INT) 115419449Sdfr slpflag = PCATCH; 115519449Sdfr gotiod = FALSE; 115619449Sdfr 115719449Sdfr /* 115819449Sdfr * Find a free iod to process this request. 115919449Sdfr */ 116089407Speter for (iod = 0; iod < nfs_numasync; iod++) 116189324Speter if (nfs_iodwant[iod]) { 116219449Sdfr gotiod = TRUE; 116325023Sdfr break; 116419449Sdfr } 116519449Sdfr 116619449Sdfr /* 116789324Speter * Try to create one if none are free. 116889324Speter */ 116989324Speter if (!gotiod) { 117089324Speter iod = nfs_nfsiodnew(); 117189324Speter if (iod != -1) 117289324Speter gotiod = TRUE; 117389324Speter } 117489324Speter 117589407Speter if (gotiod) { 117689407Speter /* 117789407Speter * Found one, so wake it up and tell it which 117889407Speter * mount to process. 117989407Speter */ 118089407Speter NFS_DPF(ASYNCIO, ("nfs_asyncio: waking iod %d for mount %p\n", 118189407Speter iod, nmp)); 118299797Sdillon nfs_iodwant[iod] = NULL; 118389407Speter nfs_iodmount[iod] = nmp; 118489407Speter nmp->nm_bufqiods++; 1185111748Sdes wakeup(&nfs_iodwant[iod]); 118689407Speter } 118789407Speter 118889324Speter /* 118919449Sdfr * If none are free, we may already have an iod working on this mount 119019449Sdfr * point. If so, it will process our request. 119119449Sdfr */ 119219449Sdfr if (!gotiod) { 119319449Sdfr if (nmp->nm_bufqiods > 0) { 119419449Sdfr NFS_DPF(ASYNCIO, 119519449Sdfr ("nfs_asyncio: %d iods are already processing mount %p\n", 119619449Sdfr nmp->nm_bufqiods, nmp)); 119719449Sdfr gotiod = TRUE; 119819449Sdfr } 119919449Sdfr } 120019449Sdfr 120119449Sdfr /* 120219449Sdfr * If we have an iod which can process the request, then queue 120319449Sdfr * the buffer. 120419449Sdfr */ 120519449Sdfr if (gotiod) { 120619449Sdfr /* 120755431Sdillon * Ensure that the queue never grows too large. We still want 120855431Sdillon * to asynchronize so we block rather then return EIO. 120919449Sdfr */ 121019449Sdfr while (nmp->nm_bufqlen >= 2*nfs_numasync) { 121119449Sdfr NFS_DPF(ASYNCIO, 121219449Sdfr ("nfs_asyncio: waiting for mount %p queue to drain\n", nmp)); 121319449Sdfr nmp->nm_bufqwant = TRUE; 121419449Sdfr error = tsleep(&nmp->nm_bufq, slpflag | PRIBIO, 121519449Sdfr "nfsaio", slptimeo); 121619449Sdfr if (error) { 121798988Sjhb if (nfs_sigintr(nmp, NULL, td)) 121819449Sdfr return (EINTR); 121919449Sdfr if (slpflag == PCATCH) { 122019449Sdfr slpflag = 0; 122119449Sdfr slptimeo = 2 * hz; 122219449Sdfr } 122319449Sdfr } 122419449Sdfr /* 122519449Sdfr * We might have lost our iod while sleeping, 122619449Sdfr * so check and loop if nescessary. 122719449Sdfr */ 122819449Sdfr if (nmp->nm_bufqiods == 0) { 122919449Sdfr NFS_DPF(ASYNCIO, 123019449Sdfr ("nfs_asyncio: no iods after mount %p queue was drained, looping\n", nmp)); 123119449Sdfr goto again; 123219449Sdfr } 123319449Sdfr } 123419449Sdfr 123558345Sphk if (bp->b_iocmd == BIO_READ) { 123684827Sjhb if (bp->b_rcred == NOCRED && cred != NOCRED) 123784827Sjhb bp->b_rcred = crhold(cred); 12381541Srgrimes } else { 12399336Sdfr bp->b_flags |= B_WRITEINPROG; 124084827Sjhb if (bp->b_wcred == NOCRED && cred != NOCRED) 124184827Sjhb bp->b_wcred = crhold(cred); 12421541Srgrimes } 12438876Srgrimes 124448225Smckusick BUF_KERNPROC(bp); 124519449Sdfr TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist); 124619449Sdfr nmp->nm_bufqlen++; 12471541Srgrimes return (0); 124819449Sdfr } 12499336Sdfr 12509336Sdfr /* 125119449Sdfr * All the iods are busy on other mounts, so return EIO to 125219449Sdfr * force the caller to process the i/o synchronously. 12539336Sdfr */ 125419449Sdfr NFS_DPF(ASYNCIO, ("nfs_asyncio: no iods available, i/o is synchronous\n")); 125519449Sdfr return (EIO); 12561541Srgrimes} 12571541Srgrimes 12581541Srgrimes/* 12591541Srgrimes * Do an I/O operation to/from a cache block. This may be called 12601541Srgrimes * synchronously or from an nfsiod. 12611541Srgrimes */ 12621541Srgrimesint 126383651Speternfs_doio(struct buf *bp, struct ucred *cr, struct thread *td) 12641541Srgrimes{ 126544679Sjulian struct uio *uiop; 126644679Sjulian struct vnode *vp; 12671541Srgrimes struct nfsnode *np; 12681541Srgrimes struct nfsmount *nmp; 126946349Salc int error = 0, iomode, must_commit = 0; 12701541Srgrimes struct uio uio; 12711541Srgrimes struct iovec io; 127283651Speter struct proc *p = td ? td->td_proc : NULL; 12731541Srgrimes 12741541Srgrimes vp = bp->b_vp; 12751541Srgrimes np = VTONFS(vp); 12761541Srgrimes nmp = VFSTONFS(vp->v_mount); 12771541Srgrimes uiop = &uio; 12781541Srgrimes uiop->uio_iov = &io; 12791541Srgrimes uiop->uio_iovcnt = 1; 12801541Srgrimes uiop->uio_segflg = UIO_SYSSPACE; 128183366Sjulian uiop->uio_td = td; 12821541Srgrimes 128346349Salc /* 128458934Sphk * clear BIO_ERROR and B_INVAL state prior to initiating the I/O. We 128546349Salc * do this here so we do not have to do it in all the code that 128646349Salc * calls us. 128746349Salc */ 128858934Sphk bp->b_flags &= ~B_INVAL; 128958934Sphk bp->b_ioflags &= ~BIO_ERROR; 129046349Salc 129144679Sjulian KASSERT(!(bp->b_flags & B_DONE), ("nfs_doio: bp %p already marked done", bp)); 129244679Sjulian 12931541Srgrimes /* 12941541Srgrimes * Historically, paging was done with physio, but no more. 12951541Srgrimes */ 12963664Sphk if (bp->b_flags & B_PHYS) { 12973664Sphk /* 12983664Sphk * ...though reading /dev/drum still gets us here. 12993664Sphk */ 13001541Srgrimes io.iov_len = uiop->uio_resid = bp->b_bcount; 13013664Sphk /* mapping was done by vmapbuf() */ 13021541Srgrimes io.iov_base = bp->b_data; 13039336Sdfr uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE; 130458345Sphk if (bp->b_iocmd == BIO_READ) { 13053664Sphk uiop->uio_rw = UIO_READ; 13063664Sphk nfsstats.read_physios++; 13073664Sphk error = nfs_readrpc(vp, uiop, cr); 13083664Sphk } else { 13099336Sdfr int com; 13109336Sdfr 13119336Sdfr iomode = NFSV3WRITE_DATASYNC; 13123664Sphk uiop->uio_rw = UIO_WRITE; 13133664Sphk nfsstats.write_physios++; 13149336Sdfr error = nfs_writerpc(vp, uiop, cr, &iomode, &com); 13153664Sphk } 13163664Sphk if (error) { 131758934Sphk bp->b_ioflags |= BIO_ERROR; 13183664Sphk bp->b_error = error; 13193664Sphk } 132058345Sphk } else if (bp->b_iocmd == BIO_READ) { 13213664Sphk io.iov_len = uiop->uio_resid = bp->b_bcount; 13223664Sphk io.iov_base = bp->b_data; 13231541Srgrimes uiop->uio_rw = UIO_READ; 132487834Sdillon 13251541Srgrimes switch (vp->v_type) { 13261541Srgrimes case VREG: 13279336Sdfr uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE; 13281541Srgrimes nfsstats.read_bios++; 13291541Srgrimes error = nfs_readrpc(vp, uiop, cr); 133087834Sdillon 13311541Srgrimes if (!error) { 13321541Srgrimes if (uiop->uio_resid) { 13331541Srgrimes /* 133446349Salc * If we had a short read with no error, we must have 133546349Salc * hit a file hole. We should zero-fill the remainder. 133646349Salc * This can also occur if the server hits the file EOF. 133746349Salc * 133883651Speter * Holes used to be able to occur due to pending 133946349Salc * writes, but that is not possible any longer. 13401541Srgrimes */ 134146349Salc int nread = bp->b_bcount - uiop->uio_resid; 134287834Sdillon int left = uiop->uio_resid; 134346349Salc 134446349Salc if (left > 0) 134546349Salc bzero((char *)bp->b_data + nread, left); 134646349Salc uiop->uio_resid = 0; 134746349Salc } 13481541Srgrimes } 1349115041Srwatson /* ASSERT_VOP_LOCKED(vp, "nfs_doio"); */ 1350101308Sjeff if (p && (vp->v_vflag & VV_TEXT) && 135183651Speter (np->n_mtime != np->n_vattr.va_mtime.tv_sec)) { 13521541Srgrimes uprintf("Process killed due to text file modification\n"); 135373929Sjhb PROC_LOCK(p); 13541541Srgrimes psignal(p, SIGKILL); 135573929Sjhb _PHOLD(p); 135673929Sjhb PROC_UNLOCK(p); 13571541Srgrimes } 13581541Srgrimes break; 13591541Srgrimes case VLNK: 13609336Sdfr uiop->uio_offset = (off_t)0; 13611541Srgrimes nfsstats.readlink_bios++; 13621541Srgrimes error = nfs_readlinkrpc(vp, uiop, cr); 13631541Srgrimes break; 13641541Srgrimes case VDIR: 13651541Srgrimes nfsstats.readdir_bios++; 13669336Sdfr uiop->uio_offset = ((u_quad_t)bp->b_lblkno) * NFS_DIRBLKSIZ; 13679336Sdfr if (nmp->nm_flag & NFSMNT_RDIRPLUS) { 13689336Sdfr error = nfs_readdirplusrpc(vp, uiop, cr); 13699336Sdfr if (error == NFSERR_NOTSUPP) 13709336Sdfr nmp->nm_flag &= ~NFSMNT_RDIRPLUS; 13719336Sdfr } 13729336Sdfr if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0) 13739336Sdfr error = nfs_readdirrpc(vp, uiop, cr); 137446349Salc /* 137546349Salc * end-of-directory sets B_INVAL but does not generate an 137646349Salc * error. 137746349Salc */ 137839782Smckusick if (error == 0 && uiop->uio_resid == bp->b_bcount) 137939782Smckusick bp->b_flags |= B_INVAL; 13801541Srgrimes break; 13813305Sphk default: 138283651Speter printf("nfs_doio: type %x unexpected\n", vp->v_type); 13833305Sphk break; 13841541Srgrimes }; 13851541Srgrimes if (error) { 138658934Sphk bp->b_ioflags |= BIO_ERROR; 13871541Srgrimes bp->b_error = error; 13881541Srgrimes } 13891541Srgrimes } else { 139083651Speter /* 139151344Sdillon * If we only need to commit, try to commit 139251344Sdillon */ 139351344Sdillon if (bp->b_flags & B_NEEDCOMMIT) { 139451344Sdillon int retv; 139551344Sdillon off_t off; 139651344Sdillon 139751344Sdillon off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE + bp->b_dirtyoff; 139851344Sdillon bp->b_flags |= B_WRITEINPROG; 139951344Sdillon retv = nfs_commit( 140051344Sdillon bp->b_vp, off, bp->b_dirtyend-bp->b_dirtyoff, 140183366Sjulian bp->b_wcred, td); 140251344Sdillon bp->b_flags &= ~B_WRITEINPROG; 140351344Sdillon if (retv == 0) { 140451344Sdillon bp->b_dirtyoff = bp->b_dirtyend = 0; 140554480Sdillon bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK); 140651344Sdillon bp->b_resid = 0; 140759249Sphk bufdone(bp); 140851344Sdillon return (0); 140951344Sdillon } 141051344Sdillon if (retv == NFSERR_STALEWRITEVERF) { 141151344Sdillon nfs_clearcommit(bp->b_vp->v_mount); 141251344Sdillon } 141351344Sdillon } 141451344Sdillon 141551344Sdillon /* 141651344Sdillon * Setup for actual write 141751344Sdillon */ 141851344Sdillon 141941791Sdt if ((off_t)bp->b_blkno * DEV_BSIZE + bp->b_dirtyend > np->n_size) 142041791Sdt bp->b_dirtyend = np->n_size - (off_t)bp->b_blkno * DEV_BSIZE; 14218692Sdg 14228692Sdg if (bp->b_dirtyend > bp->b_dirtyoff) { 14238692Sdg io.iov_len = uiop->uio_resid = bp->b_dirtyend 14249336Sdfr - bp->b_dirtyoff; 142541791Sdt uiop->uio_offset = (off_t)bp->b_blkno * DEV_BSIZE 14269336Sdfr + bp->b_dirtyoff; 14278692Sdg io.iov_base = (char *)bp->b_data + bp->b_dirtyoff; 14288692Sdg uiop->uio_rw = UIO_WRITE; 14298692Sdg nfsstats.write_bios++; 143044679Sjulian 143125785Sdfr if ((bp->b_flags & (B_ASYNC | B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == B_ASYNC) 14329336Sdfr iomode = NFSV3WRITE_UNSTABLE; 14338692Sdg else 14349336Sdfr iomode = NFSV3WRITE_FILESYNC; 143544679Sjulian 14369336Sdfr bp->b_flags |= B_WRITEINPROG; 14379336Sdfr error = nfs_writerpc(vp, uiop, cr, &iomode, &must_commit); 143851475Sdillon 143951475Sdillon /* 144051475Sdillon * When setting B_NEEDCOMMIT also set B_CLUSTEROK to try 144151475Sdillon * to cluster the buffers needing commit. This will allow 144251475Sdillon * the system to submit a single commit rpc for the whole 144383651Speter * cluster. We can do this even if the buffer is not 100% 144454480Sdillon * dirty (relative to the NFS blocksize), so we optimize the 144554480Sdillon * append-to-file-case. 144654480Sdillon * 144754480Sdillon * (when clearing B_NEEDCOMMIT, B_CLUSTEROK must also be 144854480Sdillon * cleared because write clustering only works for commit 144954480Sdillon * rpc's, not for the data portion of the write). 145051475Sdillon */ 145151475Sdillon 145225003Sdfr if (!error && iomode == NFSV3WRITE_UNSTABLE) { 145325003Sdfr bp->b_flags |= B_NEEDCOMMIT; 145425003Sdfr if (bp->b_dirtyoff == 0 145546349Salc && bp->b_dirtyend == bp->b_bcount) 145625003Sdfr bp->b_flags |= B_CLUSTEROK; 145744679Sjulian } else { 145854480Sdillon bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK); 145944679Sjulian } 14609336Sdfr bp->b_flags &= ~B_WRITEINPROG; 14618692Sdg 14629336Sdfr /* 14639336Sdfr * For an interrupted write, the buffer is still valid 14649336Sdfr * and the write hasn't been pushed to the server yet, 146558934Sphk * so we can't set BIO_ERROR and report the interruption 14669336Sdfr * by setting B_EINTR. For the B_ASYNC case, B_EINTR 14679336Sdfr * is not relevant, so the rpc attempt is essentially 14689336Sdfr * a noop. For the case of a V3 write rpc not being 14699336Sdfr * committed to stable storage, the block is still 14709336Sdfr * dirty and requires either a commit rpc or another 14719336Sdfr * write rpc with iomode == NFSV3WRITE_FILESYNC before 14729336Sdfr * the block is reused. This is indicated by setting 14739336Sdfr * the B_DELWRI and B_NEEDCOMMIT flags. 147442957Sdillon * 147542957Sdillon * If the buffer is marked B_PAGING, it does not reside on 147644679Sjulian * the vp's paging queues so we cannot call bdirty(). The 147744679Sjulian * bp in this case is not an NFS cache block so we should 147844679Sjulian * be safe. XXX 14799336Sdfr */ 14809336Sdfr if (error == EINTR 14819336Sdfr || (!error && (bp->b_flags & B_NEEDCOMMIT))) { 148234266Sjulian int s; 148334266Sjulian 148444679Sjulian s = splbio(); 14858692Sdg bp->b_flags &= ~(B_INVAL|B_NOCACHE); 148642957Sdillon if ((bp->b_flags & B_PAGING) == 0) { 148744679Sjulian bdirty(bp); 148844679Sjulian bp->b_flags &= ~B_DONE; 148942957Sdillon } 149047749Speter if (error && (bp->b_flags & B_ASYNC) == 0) 149132755Sdyson bp->b_flags |= B_EINTR; 149244679Sjulian splx(s); 14938692Sdg } else { 149444679Sjulian if (error) { 149558934Sphk bp->b_ioflags |= BIO_ERROR; 149644679Sjulian bp->b_error = np->n_error = error; 149744679Sjulian np->n_flag |= NWRITEERR; 149844679Sjulian } 149944679Sjulian bp->b_dirtyoff = bp->b_dirtyend = 0; 15008692Sdg } 15011541Srgrimes } else { 15028692Sdg bp->b_resid = 0; 150359249Sphk bufdone(bp); 15048692Sdg return (0); 15051541Srgrimes } 15061541Srgrimes } 15071541Srgrimes bp->b_resid = uiop->uio_resid; 15089336Sdfr if (must_commit) 150944679Sjulian nfs_clearcommit(vp->v_mount); 151059249Sphk bufdone(bp); 15111541Srgrimes return (error); 15121541Srgrimes} 151387834Sdillon 151487834Sdillon/* 151587834Sdillon * Used to aid in handling ftruncate() operations on the NFS client side. 151687834Sdillon * Truncation creates a number of special problems for NFS. We have to 151787834Sdillon * throw away VM pages and buffer cache buffers that are beyond EOF, and 151887834Sdillon * we have to properly handle VM pages or (potentially dirty) buffers 151987834Sdillon * that straddle the truncation point. 152087834Sdillon */ 152187834Sdillon 152287834Sdillonint 152387834Sdillonnfs_meta_setsize(struct vnode *vp, struct ucred *cred, struct thread *td, u_quad_t nsize) 152487834Sdillon{ 152587834Sdillon struct nfsnode *np = VTONFS(vp); 152687834Sdillon u_quad_t tsize = np->n_size; 152787834Sdillon int biosize = vp->v_mount->mnt_stat.f_iosize; 152887834Sdillon int error = 0; 152987834Sdillon 153087834Sdillon np->n_size = nsize; 153187834Sdillon 153287834Sdillon if (np->n_size < tsize) { 153387834Sdillon struct buf *bp; 153487834Sdillon daddr_t lbn; 153587834Sdillon int bufsize; 153687834Sdillon 153787834Sdillon /* 153887834Sdillon * vtruncbuf() doesn't get the buffer overlapping the 153987834Sdillon * truncation point. We may have a B_DELWRI and/or B_CACHE 154087834Sdillon * buffer that now needs to be truncated. 154187834Sdillon */ 154287834Sdillon error = vtruncbuf(vp, cred, td, nsize, biosize); 154387834Sdillon lbn = nsize / biosize; 154487834Sdillon bufsize = nsize & (biosize - 1); 154587834Sdillon bp = nfs_getcacheblk(vp, lbn, bufsize, td); 154687834Sdillon if (bp->b_dirtyoff > bp->b_bcount) 154787834Sdillon bp->b_dirtyoff = bp->b_bcount; 154887834Sdillon if (bp->b_dirtyend > bp->b_bcount) 154987834Sdillon bp->b_dirtyend = bp->b_bcount; 155087834Sdillon bp->b_flags |= B_RELBUF; /* don't leave garbage around */ 155187834Sdillon brelse(bp); 155287834Sdillon } else { 155387834Sdillon vnode_pager_setsize(vp, nsize); 155487834Sdillon } 155587834Sdillon return(error); 155687834Sdillon} 155787834Sdillon 1558