Home | History | Annotate | Line # | Download | only in nfs
nfs_bio.c revision 1.52
      1  1.52      fvdl /*	$NetBSD: nfs_bio.c,v 1.52 2000/09/19 22:11:47 fvdl Exp $	*/
      2  1.15       cgd 
      3   1.1       cgd /*
      4  1.12   mycroft  * Copyright (c) 1989, 1993
      5  1.12   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * This code is derived from software contributed to Berkeley by
      8   1.1       cgd  * Rick Macklem at The University of Guelph.
      9   1.1       cgd  *
     10   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11   1.1       cgd  * modification, are permitted provided that the following conditions
     12   1.1       cgd  * are met:
     13   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     19   1.1       cgd  *    must display the following acknowledgement:
     20   1.1       cgd  *	This product includes software developed by the University of
     21   1.1       cgd  *	California, Berkeley and its contributors.
     22   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     23   1.1       cgd  *    may be used to endorse or promote products derived from this software
     24   1.1       cgd  *    without specific prior written permission.
     25   1.1       cgd  *
     26   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36   1.1       cgd  * SUCH DAMAGE.
     37   1.1       cgd  *
     38  1.24      fvdl  *	@(#)nfs_bio.c	8.9 (Berkeley) 3/30/95
     39   1.1       cgd  */
     40   1.1       cgd 
     41  1.51     bjh21 #include "opt_nfs.h"
     42  1.51     bjh21 
     43   1.8   mycroft #include <sys/param.h>
     44   1.8   mycroft #include <sys/systm.h>
     45  1.12   mycroft #include <sys/resourcevar.h>
     46  1.24      fvdl #include <sys/signalvar.h>
     47   1.8   mycroft #include <sys/proc.h>
     48   1.8   mycroft #include <sys/buf.h>
     49   1.8   mycroft #include <sys/vnode.h>
     50   1.8   mycroft #include <sys/trace.h>
     51   1.8   mycroft #include <sys/mount.h>
     52  1.12   mycroft #include <sys/kernel.h>
     53  1.23  christos #include <sys/namei.h>
     54  1.34      fvdl #include <sys/dirent.h>
     55   1.1       cgd 
     56  1.41       mrg #include <uvm/uvm_extern.h>
     57  1.41       mrg 
     58  1.12   mycroft #include <nfs/rpcv2.h>
     59  1.24      fvdl #include <nfs/nfsproto.h>
     60   1.8   mycroft #include <nfs/nfs.h>
     61   1.8   mycroft #include <nfs/nfsmount.h>
     62  1.12   mycroft #include <nfs/nqnfs.h>
     63  1.24      fvdl #include <nfs/nfsnode.h>
     64  1.23  christos #include <nfs/nfs_var.h>
     65   1.1       cgd 
     66  1.12   mycroft extern int nfs_numasync;
     67  1.24      fvdl extern struct nfsstats nfsstats;
     68   1.1       cgd 
     69   1.1       cgd /*
     70   1.1       cgd  * Vnode op for read using bio
     71   1.1       cgd  * Any similarity to readip() is purely coincidental
     72   1.1       cgd  */
     73  1.23  christos int
     74  1.34      fvdl nfs_bioread(vp, uio, ioflag, cred, cflag)
     75  1.48  augustss 	struct vnode *vp;
     76  1.48  augustss 	struct uio *uio;
     77  1.34      fvdl 	int ioflag, cflag;
     78   1.1       cgd 	struct ucred *cred;
     79   1.1       cgd {
     80  1.48  augustss 	struct nfsnode *np = VTONFS(vp);
     81  1.48  augustss 	int biosize, diff;
     82  1.23  christos 	struct buf *bp = NULL, *rabp;
     83   1.1       cgd 	struct vattr vattr;
     84  1.12   mycroft 	struct proc *p;
     85  1.24      fvdl 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
     86  1.35      fvdl 	struct nfsdircache *ndp = NULL, *nndp = NULL;
     87  1.12   mycroft 	daddr_t lbn, bn, rabn;
     88  1.34      fvdl 	caddr_t baddr, ep, edp;
     89  1.34      fvdl 	int got_buf = 0, nra, error = 0, n = 0, on = 0, not_readin, en, enn;
     90  1.34      fvdl 	int enough = 0;
     91  1.34      fvdl 	struct dirent *dp, *pdp;
     92  1.43      fvdl 	off_t curoff = 0, offdiff;
     93   1.1       cgd 
     94   1.1       cgd #ifdef DIAGNOSTIC
     95   1.1       cgd 	if (uio->uio_rw != UIO_READ)
     96   1.1       cgd 		panic("nfs_read mode");
     97   1.1       cgd #endif
     98   1.1       cgd 	if (uio->uio_resid == 0)
     99   1.1       cgd 		return (0);
    100  1.34      fvdl 	if (vp->v_type != VDIR && uio->uio_offset < 0)
    101   1.1       cgd 		return (EINVAL);
    102  1.24      fvdl 	p = uio->uio_procp;
    103  1.51     bjh21 #ifndef NFS_V2_ONLY
    104  1.34      fvdl 	if ((nmp->nm_flag & NFSMNT_NFSV3) &&
    105  1.34      fvdl 	    !(nmp->nm_iflag & NFSMNT_GOTFSINFO))
    106  1.24      fvdl 		(void)nfs_fsinfo(nmp, vp, cred, p);
    107  1.51     bjh21 #endif
    108  1.34      fvdl 	if (vp->v_type != VDIR &&
    109  1.34      fvdl 	    (uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
    110  1.33      fvdl 		return (EFBIG);
    111  1.12   mycroft 	biosize = nmp->nm_rsize;
    112   1.1       cgd 	/*
    113  1.12   mycroft 	 * For nfs, cache consistency can only be maintained approximately.
    114  1.12   mycroft 	 * Although RFC1094 does not specify the criteria, the following is
    115  1.12   mycroft 	 * believed to be compatible with the reference port.
    116  1.12   mycroft 	 * For nqnfs, full cache consistency is maintained within the loop.
    117  1.12   mycroft 	 * For nfs:
    118   1.1       cgd 	 * If the file's modify time on the server has changed since the
    119   1.1       cgd 	 * last read rpc or you have written to the file,
    120   1.1       cgd 	 * you may have lost data cache consistency with the
    121   1.1       cgd 	 * server, so flush all of the file's data out of the cache.
    122   1.1       cgd 	 * Then force a getattr rpc to ensure that you have up to date
    123   1.1       cgd 	 * attributes.
    124   1.1       cgd 	 * NB: This implies that cache data can be read when up to
    125   1.1       cgd 	 * NFS_ATTRTIMEO seconds out of date. If you find that you need current
    126   1.1       cgd 	 * attributes this could be forced by setting n_attrstamp to 0 before
    127  1.12   mycroft 	 * the VOP_GETATTR() call.
    128   1.1       cgd 	 */
    129  1.12   mycroft 	if ((nmp->nm_flag & NFSMNT_NQNFS) == 0 && vp->v_type != VLNK) {
    130   1.1       cgd 		if (np->n_flag & NMODIFIED) {
    131  1.24      fvdl 			if (vp->v_type != VREG) {
    132  1.24      fvdl 				if (vp->v_type != VDIR)
    133  1.24      fvdl 					panic("nfs: bioread, not dir");
    134  1.35      fvdl 				nfs_invaldircache(vp, 0);
    135  1.35      fvdl 				np->n_direofoffset = 0;
    136  1.23  christos 				error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
    137  1.23  christos 				if (error)
    138  1.12   mycroft 					return (error);
    139  1.12   mycroft 			}
    140   1.1       cgd 			np->n_attrstamp = 0;
    141  1.23  christos 			error = VOP_GETATTR(vp, &vattr, cred, p);
    142  1.23  christos 			if (error)
    143   1.1       cgd 				return (error);
    144  1.22       jtc 			np->n_mtime = vattr.va_mtime.tv_sec;
    145   1.1       cgd 		} else {
    146  1.24      fvdl 			error = VOP_GETATTR(vp, &vattr, cred, p);
    147  1.24      fvdl 			if (error)
    148   1.1       cgd 				return (error);
    149  1.22       jtc 			if (np->n_mtime != vattr.va_mtime.tv_sec) {
    150  1.35      fvdl 				if (vp->v_type == VDIR) {
    151  1.35      fvdl 					nfs_invaldircache(vp, 0);
    152  1.35      fvdl 					np->n_direofoffset = 0;
    153  1.35      fvdl 				}
    154  1.23  christos 				error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
    155  1.23  christos 				if (error)
    156  1.12   mycroft 					return (error);
    157  1.22       jtc 				np->n_mtime = vattr.va_mtime.tv_sec;
    158   1.1       cgd 			}
    159   1.1       cgd 		}
    160   1.1       cgd 	}
    161   1.1       cgd 	do {
    162  1.12   mycroft 
    163  1.12   mycroft 	    /*
    164  1.12   mycroft 	     * Get a valid lease. If cached data is stale, flush it.
    165  1.12   mycroft 	     */
    166  1.12   mycroft 	    if (nmp->nm_flag & NFSMNT_NQNFS) {
    167  1.24      fvdl 		if (NQNFS_CKINVALID(vp, np, ND_READ)) {
    168  1.12   mycroft 		    do {
    169  1.24      fvdl 			error = nqnfs_getlease(vp, ND_READ, cred, p);
    170  1.12   mycroft 		    } while (error == NQNFS_EXPIRED);
    171  1.12   mycroft 		    if (error)
    172  1.12   mycroft 			return (error);
    173  1.12   mycroft 		    if (np->n_lrev != np->n_brev ||
    174  1.12   mycroft 			(np->n_flag & NQNFSNONCACHE) ||
    175  1.12   mycroft 			((np->n_flag & NMODIFIED) && vp->v_type == VDIR)) {
    176  1.35      fvdl 			if (vp->v_type == VDIR) {
    177  1.35      fvdl 				nfs_invaldircache(vp, 0);
    178  1.35      fvdl 				np->n_direofoffset = 0;
    179  1.35      fvdl 			}
    180  1.23  christos 			error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
    181  1.23  christos 			if (error)
    182  1.12   mycroft 			    return (error);
    183  1.12   mycroft 			np->n_brev = np->n_lrev;
    184  1.12   mycroft 		    }
    185  1.12   mycroft 		} else if (vp->v_type == VDIR && (np->n_flag & NMODIFIED)) {
    186  1.35      fvdl 		    nfs_invaldircache(vp, 0);
    187  1.23  christos 		    error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
    188  1.35      fvdl 		    np->n_direofoffset = 0;
    189  1.23  christos 		    if (error)
    190  1.12   mycroft 			return (error);
    191  1.12   mycroft 		}
    192  1.12   mycroft 	    }
    193  1.26      fvdl 	    /*
    194  1.26      fvdl 	     * Don't cache symlinks.
    195  1.26      fvdl 	     */
    196  1.26      fvdl 	    if (np->n_flag & NQNFSNONCACHE
    197  1.26      fvdl 		|| ((vp->v_flag & VROOT) && vp->v_type == VLNK)) {
    198  1.12   mycroft 		switch (vp->v_type) {
    199  1.12   mycroft 		case VREG:
    200  1.24      fvdl 			return (nfs_readrpc(vp, uio, cred));
    201  1.12   mycroft 		case VLNK:
    202  1.24      fvdl 			return (nfs_readlinkrpc(vp, uio, cred));
    203  1.12   mycroft 		case VDIR:
    204  1.23  christos 			break;
    205  1.24      fvdl 		default:
    206  1.29  christos 			printf(" NQNFSNONCACHE: type %x unexpected\n",
    207  1.28  christos 			    vp->v_type);
    208  1.12   mycroft 		};
    209  1.12   mycroft 	    }
    210  1.12   mycroft 	    baddr = (caddr_t)0;
    211   1.1       cgd 	    switch (vp->v_type) {
    212   1.1       cgd 	    case VREG:
    213   1.1       cgd 		nfsstats.biocache_reads++;
    214   1.1       cgd 		lbn = uio->uio_offset / biosize;
    215  1.24      fvdl 		on = uio->uio_offset & (biosize - 1);
    216  1.12   mycroft 		bn = lbn * (biosize / DEV_BSIZE);
    217  1.12   mycroft 		not_readin = 1;
    218  1.12   mycroft 
    219  1.12   mycroft 		/*
    220  1.12   mycroft 		 * Start the read ahead(s), as required.
    221  1.12   mycroft 		 */
    222  1.36      fvdl 		if (nfs_numasync > 0 && nmp->nm_readahead > 0 &&
    223  1.36      fvdl 		    lbn - 1 == vp->v_lastr) {
    224  1.12   mycroft 		    for (nra = 0; nra < nmp->nm_readahead &&
    225  1.12   mycroft 			(lbn + 1 + nra) * biosize < np->n_size; nra++) {
    226  1.12   mycroft 			rabn = (lbn + 1 + nra) * (biosize / DEV_BSIZE);
    227  1.12   mycroft 			if (!incore(vp, rabn)) {
    228  1.12   mycroft 			    rabp = nfs_getcacheblk(vp, rabn, biosize, p);
    229  1.12   mycroft 			    if (!rabp)
    230  1.12   mycroft 				return (EINTR);
    231  1.12   mycroft 			    if ((rabp->b_flags & (B_DELWRI | B_DONE)) == 0) {
    232  1.12   mycroft 				rabp->b_flags |= (B_READ | B_ASYNC);
    233  1.12   mycroft 				if (nfs_asyncio(rabp, cred)) {
    234  1.12   mycroft 				    rabp->b_flags |= B_INVAL;
    235  1.12   mycroft 				    brelse(rabp);
    236  1.12   mycroft 				}
    237  1.19   mycroft 			    } else
    238  1.19   mycroft 				brelse(rabp);
    239  1.12   mycroft 			}
    240  1.12   mycroft 		    }
    241  1.12   mycroft 		}
    242  1.12   mycroft 
    243  1.12   mycroft 		/*
    244  1.12   mycroft 		 * If the block is in the cache and has the required data
    245  1.12   mycroft 		 * in a valid region, just copy it out.
    246  1.12   mycroft 		 * Otherwise, get the block and write back/read in,
    247  1.12   mycroft 		 * as required.
    248  1.12   mycroft 		 */
    249  1.12   mycroft 		if ((bp = incore(vp, bn)) &&
    250  1.12   mycroft 		    (bp->b_flags & (B_BUSY | B_WRITEINPROG)) ==
    251  1.12   mycroft 		    (B_BUSY | B_WRITEINPROG))
    252  1.12   mycroft 			got_buf = 0;
    253  1.12   mycroft 		else {
    254  1.12   mycroft again:
    255  1.12   mycroft 			bp = nfs_getcacheblk(vp, bn, biosize, p);
    256  1.12   mycroft 			if (!bp)
    257  1.12   mycroft 				return (EINTR);
    258  1.12   mycroft 			got_buf = 1;
    259  1.12   mycroft 			if ((bp->b_flags & (B_DONE | B_DELWRI)) == 0) {
    260  1.12   mycroft 				bp->b_flags |= B_READ;
    261  1.12   mycroft 				not_readin = 0;
    262  1.23  christos 				error = nfs_doio(bp, cred, p);
    263  1.23  christos 				if (error) {
    264  1.12   mycroft 				    brelse(bp);
    265  1.12   mycroft 				    return (error);
    266  1.12   mycroft 				}
    267  1.12   mycroft 			}
    268  1.12   mycroft 		}
    269  1.11       cgd 		n = min((unsigned)(biosize - on), uio->uio_resid);
    270  1.43      fvdl 		offdiff = np->n_size - uio->uio_offset;
    271  1.43      fvdl 		if (offdiff < (off_t)n)
    272  1.43      fvdl 			n = (int)offdiff;
    273  1.12   mycroft 		if (not_readin && n > 0) {
    274  1.12   mycroft 			if (on < bp->b_validoff || (on + n) > bp->b_validend) {
    275  1.12   mycroft 				if (!got_buf) {
    276  1.12   mycroft 				    bp = nfs_getcacheblk(vp, bn, biosize, p);
    277  1.12   mycroft 				    if (!bp)
    278  1.12   mycroft 					return (EINTR);
    279  1.12   mycroft 				    got_buf = 1;
    280  1.12   mycroft 				}
    281  1.24      fvdl 				bp->b_flags |= B_INVAFTERWRITE;
    282  1.12   mycroft 				if (bp->b_dirtyend > 0) {
    283  1.12   mycroft 				    if ((bp->b_flags & B_DELWRI) == 0)
    284  1.12   mycroft 					panic("nfsbioread");
    285  1.12   mycroft 				    if (VOP_BWRITE(bp) == EINTR)
    286  1.12   mycroft 					return (EINTR);
    287  1.12   mycroft 				} else
    288  1.12   mycroft 				    brelse(bp);
    289  1.12   mycroft 				goto again;
    290  1.12   mycroft 			}
    291  1.12   mycroft 		}
    292   1.1       cgd 		vp->v_lastr = lbn;
    293  1.12   mycroft 		diff = (on >= bp->b_validend) ? 0 : (bp->b_validend - on);
    294  1.12   mycroft 		if (diff < n)
    295  1.12   mycroft 			n = diff;
    296   1.1       cgd 		break;
    297   1.1       cgd 	    case VLNK:
    298   1.1       cgd 		nfsstats.biocache_readlinks++;
    299  1.12   mycroft 		bp = nfs_getcacheblk(vp, (daddr_t)0, NFS_MAXPATHLEN, p);
    300  1.12   mycroft 		if (!bp)
    301  1.12   mycroft 			return (EINTR);
    302  1.12   mycroft 		if ((bp->b_flags & B_DONE) == 0) {
    303  1.12   mycroft 			bp->b_flags |= B_READ;
    304  1.24      fvdl 			error = nfs_doio(bp, cred, p);
    305  1.24      fvdl 			if (error) {
    306  1.12   mycroft 				brelse(bp);
    307  1.12   mycroft 				return (error);
    308  1.12   mycroft 			}
    309  1.12   mycroft 		}
    310  1.12   mycroft 		n = min(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid);
    311  1.12   mycroft 		got_buf = 1;
    312   1.1       cgd 		on = 0;
    313   1.1       cgd 		break;
    314   1.1       cgd 	    case VDIR:
    315  1.34      fvdl diragain:
    316  1.34      fvdl 		nfsstats.biocache_readdirs++;
    317  1.35      fvdl 		ndp = nfs_searchdircache(vp, uio->uio_offset,
    318  1.35      fvdl 			(nmp->nm_flag & NFSMNT_XLATECOOKIE), 0);
    319  1.35      fvdl 		if (!ndp) {
    320  1.35      fvdl 			/*
    321  1.35      fvdl 			 * We've been handed a cookie that is not
    322  1.35      fvdl 			 * in the cache. If we're not translating
    323  1.35      fvdl 			 * 32 <-> 64, it may be a value that was
    324  1.35      fvdl 			 * flushed out of the cache because it grew
    325  1.35      fvdl 			 * too big. Let the server judge if it's
    326  1.35      fvdl 			 * valid or not. In the translation case,
    327  1.35      fvdl 			 * we have no way of validating this value,
    328  1.35      fvdl 			 * so punt.
    329  1.35      fvdl 			 */
    330  1.35      fvdl 			if (nmp->nm_flag & NFSMNT_XLATECOOKIE)
    331  1.35      fvdl 				return (EINVAL);
    332  1.35      fvdl 			ndp = nfs_enterdircache(vp, uio->uio_offset,
    333  1.35      fvdl 				uio->uio_offset, 0, 0);
    334  1.35      fvdl 		}
    335  1.35      fvdl 
    336  1.34      fvdl 		if (uio->uio_offset != 0 &&
    337  1.35      fvdl 		    ndp->dc_cookie == np->n_direofoffset) {
    338  1.35      fvdl 			nfsstats.direofcache_hits++;
    339  1.18   mycroft 			return (0);
    340  1.35      fvdl 		}
    341  1.35      fvdl 
    342  1.34      fvdl 		bp = nfs_getcacheblk(vp, ndp->dc_blkno, NFS_DIRBLKSIZ, p);
    343  1.12   mycroft 		if (!bp)
    344  1.24      fvdl 		    return (EINTR);
    345  1.12   mycroft 		if ((bp->b_flags & B_DONE) == 0) {
    346  1.24      fvdl 		    bp->b_flags |= B_READ;
    347  1.35      fvdl 		    bp->b_dcookie = ndp->dc_blkcookie;
    348  1.24      fvdl 		    error = nfs_doio(bp, cred, p);
    349  1.24      fvdl 		    if (error) {
    350  1.34      fvdl 			/*
    351  1.34      fvdl 			 * Yuck! The directory has been modified on the
    352  1.34      fvdl 			 * server. Punt and let the userland code
    353  1.34      fvdl 			 * deal with it.
    354  1.34      fvdl 			 */
    355  1.24      fvdl 			brelse(bp);
    356  1.34      fvdl 			if (error == NFSERR_BAD_COOKIE) {
    357  1.35      fvdl 			    nfs_invaldircache(vp, 0);
    358  1.34      fvdl 			    nfs_vinvalbuf(vp, 0, cred, p, 1);
    359  1.34      fvdl 			    error = EINVAL;
    360  1.12   mycroft 			}
    361  1.34      fvdl 			return (error);
    362  1.38      fvdl 		    }
    363  1.40      fvdl 		}
    364  1.40      fvdl 
    365  1.40      fvdl 		/*
    366  1.40      fvdl 		 * Just return if we hit EOF right away with this
    367  1.40      fvdl 		 * block. Always check here, because direofoffset
    368  1.40      fvdl 		 * may have been set by an nfsiod since the last
    369  1.40      fvdl 		 * check.
    370  1.40      fvdl 		 */
    371  1.40      fvdl 		if (np->n_direofoffset != 0 &&
    372  1.39      fvdl 			ndp->dc_blkcookie == np->n_direofoffset) {
    373  1.40      fvdl 			brelse(bp);
    374  1.40      fvdl 			return (0);
    375  1.12   mycroft 		}
    376  1.12   mycroft 
    377  1.12   mycroft 		/*
    378  1.34      fvdl 		 * Find the entry we were looking for in the block.
    379  1.34      fvdl 		 */
    380  1.34      fvdl 
    381  1.34      fvdl 		en = ndp->dc_entry;
    382  1.34      fvdl 
    383  1.34      fvdl 		pdp = dp = (struct dirent *)bp->b_data;
    384  1.34      fvdl 		edp = bp->b_data + bp->b_validend;
    385  1.34      fvdl 		enn = 0;
    386  1.34      fvdl 		while (enn < en && (caddr_t)dp < edp) {
    387  1.34      fvdl 			pdp = dp;
    388  1.34      fvdl 			dp = (struct dirent *)((caddr_t)dp + dp->d_reclen);
    389  1.34      fvdl 			enn++;
    390  1.34      fvdl 		}
    391  1.34      fvdl 
    392  1.34      fvdl 		/*
    393  1.34      fvdl 		 * If the entry number was bigger than the number of
    394  1.34      fvdl 		 * entries in the block, or the cookie of the previous
    395  1.34      fvdl 		 * entry doesn't match, the directory cache is
    396  1.34      fvdl 		 * stale. Flush it and try again (i.e. go to
    397  1.34      fvdl 		 * the server).
    398  1.34      fvdl 		 */
    399  1.34      fvdl 		if ((caddr_t)dp >= edp || (caddr_t)dp + dp->d_reclen > edp ||
    400  1.35      fvdl 		    (en > 0 && NFS_GETCOOKIE(pdp) != ndp->dc_cookie)) {
    401  1.34      fvdl #ifdef DEBUG
    402  1.37   thorpej 		    	printf("invalid cache: %p %p %p off %lx %lx\n",
    403  1.37   thorpej 				pdp, dp, edp,
    404  1.34      fvdl 				(unsigned long)uio->uio_offset,
    405  1.34      fvdl 				(unsigned long)NFS_GETCOOKIE(pdp));
    406  1.34      fvdl #endif
    407  1.34      fvdl 			brelse(bp);
    408  1.35      fvdl 			nfs_invaldircache(vp, 0);
    409  1.34      fvdl 			nfs_vinvalbuf(vp, 0, cred, p, 0);
    410  1.34      fvdl 			goto diragain;
    411  1.34      fvdl 		}
    412  1.34      fvdl 
    413  1.34      fvdl 		on = (caddr_t)dp - bp->b_data;
    414  1.34      fvdl 
    415  1.34      fvdl 		/*
    416  1.34      fvdl 		 * Cache all entries that may be exported to the
    417  1.34      fvdl 		 * user, as they may be thrown back at us. The
    418  1.34      fvdl 		 * NFSBIO_CACHECOOKIES flag indicates that all
    419  1.34      fvdl 		 * entries are being 'exported', so cache them all.
    420  1.34      fvdl 		 */
    421  1.34      fvdl 
    422  1.34      fvdl 		if (en == 0 && pdp == dp) {
    423  1.34      fvdl 			dp = (struct dirent *)
    424  1.34      fvdl 			    ((caddr_t)dp + dp->d_reclen);
    425  1.34      fvdl 			enn++;
    426  1.34      fvdl 		}
    427  1.34      fvdl 
    428  1.34      fvdl 		if (uio->uio_resid < (bp->b_validend - on)) {
    429  1.34      fvdl 			n = uio->uio_resid;
    430  1.34      fvdl 			enough = 1;
    431  1.34      fvdl 		} else
    432  1.34      fvdl 			n = bp->b_validend - on;
    433  1.34      fvdl 
    434  1.34      fvdl 		ep = bp->b_data + on + n;
    435  1.34      fvdl 
    436  1.34      fvdl 		/*
    437  1.34      fvdl 		 * Find last complete entry to copy, caching entries
    438  1.34      fvdl 		 * (if requested) as we go.
    439  1.34      fvdl 		 */
    440  1.34      fvdl 
    441  1.34      fvdl 		while ((caddr_t)dp < ep && (caddr_t)dp + dp->d_reclen <= ep) {
    442  1.35      fvdl 			if (cflag & NFSBIO_CACHECOOKIES) {
    443  1.35      fvdl 				nndp = nfs_enterdircache(vp, NFS_GETCOOKIE(pdp),
    444  1.35      fvdl 				    ndp->dc_blkcookie, enn, bp->b_lblkno);
    445  1.35      fvdl 				if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
    446  1.35      fvdl 					NFS_STASHCOOKIE32(pdp,
    447  1.35      fvdl 					    nndp->dc_cookie32);
    448  1.35      fvdl 				}
    449  1.35      fvdl 			}
    450  1.34      fvdl 			pdp = dp;
    451  1.34      fvdl 			dp = (struct dirent *)((caddr_t)dp + dp->d_reclen);
    452  1.34      fvdl 			enn++;
    453  1.34      fvdl 		}
    454  1.34      fvdl 
    455  1.34      fvdl 		/*
    456  1.34      fvdl 		 * If the last requested entry was not the last in the
    457  1.34      fvdl 		 * buffer (happens if NFS_DIRFRAGSIZ < NFS_DIRBLKSIZ),
    458  1.34      fvdl 		 * cache the cookie of the last requested one, and
    459  1.34      fvdl 		 * set of the offset to it.
    460  1.34      fvdl 		 */
    461  1.34      fvdl 
    462  1.34      fvdl 		if ((on + n) < bp->b_validend) {
    463  1.34      fvdl 			curoff = NFS_GETCOOKIE(pdp);
    464  1.35      fvdl 			nndp = nfs_enterdircache(vp, curoff, ndp->dc_blkcookie,
    465  1.35      fvdl 			    enn, bp->b_lblkno);
    466  1.35      fvdl 			if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
    467  1.35      fvdl 				NFS_STASHCOOKIE32(pdp, nndp->dc_cookie32);
    468  1.35      fvdl 				curoff = nndp->dc_cookie32;
    469  1.35      fvdl 			}
    470  1.34      fvdl 		} else
    471  1.34      fvdl 			curoff = bp->b_dcookie;
    472  1.34      fvdl 
    473  1.35      fvdl 		/*
    474  1.35      fvdl 		 * Always cache the entry for the next block,
    475  1.35      fvdl 		 * so that readaheads can use it.
    476  1.35      fvdl 		 */
    477  1.35      fvdl 		nndp = nfs_enterdircache(vp, bp->b_dcookie, bp->b_dcookie, 0,0);
    478  1.35      fvdl 		if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
    479  1.35      fvdl 			if (curoff == bp->b_dcookie) {
    480  1.35      fvdl 				NFS_STASHCOOKIE32(pdp, nndp->dc_cookie32);
    481  1.35      fvdl 				curoff = nndp->dc_cookie32;
    482  1.35      fvdl 			}
    483  1.35      fvdl 		}
    484  1.35      fvdl 
    485  1.34      fvdl 		n = ((caddr_t)pdp + pdp->d_reclen) - (bp->b_data + on);
    486  1.34      fvdl 
    487  1.34      fvdl 		/*
    488  1.12   mycroft 		 * If not eof and read aheads are enabled, start one.
    489  1.12   mycroft 		 * (You need the current block first, so that you have the
    490  1.24      fvdl 		 *  directory offset cookie of the next block.)
    491  1.12   mycroft 		 */
    492  1.12   mycroft 		if (nfs_numasync > 0 && nmp->nm_readahead > 0 &&
    493  1.34      fvdl 		    np->n_direofoffset == 0 && !(np->n_flag & NQNFSNONCACHE)) {
    494  1.35      fvdl 			rabp = nfs_getcacheblk(vp, nndp->dc_blkno,
    495  1.34      fvdl 						NFS_DIRBLKSIZ, p);
    496  1.12   mycroft 			if (rabp) {
    497  1.12   mycroft 			    if ((rabp->b_flags & (B_DONE | B_DELWRI)) == 0) {
    498  1.35      fvdl 				rabp->b_dcookie = nndp->dc_cookie;
    499  1.12   mycroft 				rabp->b_flags |= (B_READ | B_ASYNC);
    500  1.12   mycroft 				if (nfs_asyncio(rabp, cred)) {
    501  1.12   mycroft 				    rabp->b_flags |= B_INVAL;
    502  1.12   mycroft 				    brelse(rabp);
    503  1.12   mycroft 				}
    504  1.19   mycroft 			    } else
    505  1.19   mycroft 				brelse(rabp);
    506  1.12   mycroft 			}
    507  1.12   mycroft 		}
    508  1.12   mycroft 		got_buf = 1;
    509   1.1       cgd 		break;
    510  1.24      fvdl 	    default:
    511  1.29  christos 		printf(" nfsbioread: type %x unexpected\n",vp->v_type);
    512  1.23  christos 		break;
    513   1.1       cgd 	    };
    514  1.12   mycroft 
    515  1.12   mycroft 	    if (n > 0) {
    516  1.12   mycroft 		if (!baddr)
    517  1.12   mycroft 			baddr = bp->b_data;
    518  1.12   mycroft 		error = uiomove(baddr + on, (int)n, uio);
    519   1.1       cgd 	    }
    520   1.1       cgd 	    switch (vp->v_type) {
    521  1.24      fvdl 	    case VREG:
    522  1.24      fvdl 		break;
    523   1.1       cgd 	    case VLNK:
    524   1.1       cgd 		n = 0;
    525   1.1       cgd 		break;
    526   1.1       cgd 	    case VDIR:
    527  1.24      fvdl 		if (np->n_flag & NQNFSNONCACHE)
    528  1.24      fvdl 			bp->b_flags |= B_INVAL;
    529  1.34      fvdl 		uio->uio_offset = curoff;
    530  1.34      fvdl 		if (enough)
    531  1.34      fvdl 			n = 0;
    532   1.1       cgd 		break;
    533  1.24      fvdl 	    default:
    534  1.29  christos 		printf(" nfsbioread: type %x unexpected\n",vp->v_type);
    535  1.24      fvdl 	    }
    536  1.12   mycroft 	    if (got_buf)
    537  1.12   mycroft 		brelse(bp);
    538  1.12   mycroft 	} while (error == 0 && uio->uio_resid > 0 && n > 0);
    539   1.1       cgd 	return (error);
    540   1.1       cgd }
    541   1.1       cgd 
    542   1.1       cgd /*
    543   1.1       cgd  * Vnode op for write using bio
    544   1.1       cgd  */
    545  1.23  christos int
    546  1.23  christos nfs_write(v)
    547  1.23  christos 	void *v;
    548  1.23  christos {
    549  1.12   mycroft 	struct vop_write_args /* {
    550  1.24      fvdl 		struct vnode *a_vp;
    551  1.12   mycroft 		struct uio *a_uio;
    552  1.12   mycroft 		int  a_ioflag;
    553  1.12   mycroft 		struct ucred *a_cred;
    554  1.23  christos 	} */ *ap = v;
    555  1.48  augustss 	int biosize;
    556  1.48  augustss 	struct uio *uio = ap->a_uio;
    557   1.1       cgd 	struct proc *p = uio->uio_procp;
    558  1.48  augustss 	struct vnode *vp = ap->a_vp;
    559  1.12   mycroft 	struct nfsnode *np = VTONFS(vp);
    560  1.48  augustss 	struct ucred *cred = ap->a_cred;
    561  1.12   mycroft 	int ioflag = ap->a_ioflag;
    562   1.1       cgd 	struct buf *bp;
    563   1.1       cgd 	struct vattr vattr;
    564  1.24      fvdl 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
    565   1.1       cgd 	daddr_t lbn, bn;
    566  1.24      fvdl 	int n, on, error = 0, iomode, must_commit;
    567   1.1       cgd 
    568   1.1       cgd #ifdef DIAGNOSTIC
    569   1.1       cgd 	if (uio->uio_rw != UIO_WRITE)
    570   1.1       cgd 		panic("nfs_write mode");
    571   1.1       cgd 	if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
    572   1.1       cgd 		panic("nfs_write proc");
    573   1.1       cgd #endif
    574   1.1       cgd 	if (vp->v_type != VREG)
    575   1.1       cgd 		return (EIO);
    576  1.12   mycroft 	if (np->n_flag & NWRITEERR) {
    577  1.12   mycroft 		np->n_flag &= ~NWRITEERR;
    578  1.12   mycroft 		return (np->n_error);
    579  1.12   mycroft 	}
    580  1.51     bjh21 #ifndef NFS_V2_ONLY
    581  1.34      fvdl 	if ((nmp->nm_flag & NFSMNT_NFSV3) &&
    582  1.34      fvdl 	    !(nmp->nm_iflag & NFSMNT_GOTFSINFO))
    583  1.24      fvdl 		(void)nfs_fsinfo(nmp, vp, cred, p);
    584  1.51     bjh21 #endif
    585   1.1       cgd 	if (ioflag & (IO_APPEND | IO_SYNC)) {
    586   1.1       cgd 		if (np->n_flag & NMODIFIED) {
    587  1.12   mycroft 			np->n_attrstamp = 0;
    588  1.23  christos 			error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
    589  1.23  christos 			if (error)
    590  1.12   mycroft 				return (error);
    591   1.1       cgd 		}
    592   1.1       cgd 		if (ioflag & IO_APPEND) {
    593   1.1       cgd 			np->n_attrstamp = 0;
    594  1.23  christos 			error = VOP_GETATTR(vp, &vattr, cred, p);
    595  1.23  christos 			if (error)
    596   1.1       cgd 				return (error);
    597   1.1       cgd 			uio->uio_offset = np->n_size;
    598   1.1       cgd 		}
    599   1.1       cgd 	}
    600   1.1       cgd 	if (uio->uio_offset < 0)
    601   1.1       cgd 		return (EINVAL);
    602  1.33      fvdl 	if ((uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
    603  1.33      fvdl 		return (EFBIG);
    604   1.1       cgd 	if (uio->uio_resid == 0)
    605   1.1       cgd 		return (0);
    606   1.1       cgd 	/*
    607   1.1       cgd 	 * Maybe this should be above the vnode op call, but so long as
    608   1.1       cgd 	 * file servers have no limits, i don't think it matters
    609   1.1       cgd 	 */
    610  1.12   mycroft 	if (p && uio->uio_offset + uio->uio_resid >
    611   1.1       cgd 	      p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
    612   1.1       cgd 		psignal(p, SIGXFSZ);
    613   1.1       cgd 		return (EFBIG);
    614   1.1       cgd 	}
    615   1.1       cgd 	/*
    616   1.1       cgd 	 * I use nm_rsize, not nm_wsize so that all buffer cache blocks
    617   1.1       cgd 	 * will be the same size within a filesystem. nfs_writerpc will
    618   1.1       cgd 	 * still use nm_wsize when sizing the rpc's.
    619   1.1       cgd 	 */
    620  1.12   mycroft 	biosize = nmp->nm_rsize;
    621   1.1       cgd 	do {
    622  1.16       cgd 
    623  1.16       cgd 		/*
    624  1.16       cgd 		 * XXX make sure we aren't cached in the VM page cache
    625  1.16       cgd 		 */
    626  1.41       mrg 		(void)uvm_vnp_uncache(vp);
    627  1.12   mycroft 
    628  1.12   mycroft 		/*
    629  1.12   mycroft 		 * Check for a valid write lease.
    630  1.12   mycroft 		 */
    631  1.12   mycroft 		if ((nmp->nm_flag & NFSMNT_NQNFS) &&
    632  1.24      fvdl 		    NQNFS_CKINVALID(vp, np, ND_WRITE)) {
    633  1.12   mycroft 			do {
    634  1.24      fvdl 				error = nqnfs_getlease(vp, ND_WRITE, cred, p);
    635  1.12   mycroft 			} while (error == NQNFS_EXPIRED);
    636  1.12   mycroft 			if (error)
    637  1.12   mycroft 				return (error);
    638  1.12   mycroft 			if (np->n_lrev != np->n_brev ||
    639  1.12   mycroft 			    (np->n_flag & NQNFSNONCACHE)) {
    640  1.23  christos 				error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
    641  1.23  christos 				if (error)
    642  1.12   mycroft 					return (error);
    643  1.12   mycroft 				np->n_brev = np->n_lrev;
    644  1.12   mycroft 			}
    645  1.12   mycroft 		}
    646  1.24      fvdl 		if ((np->n_flag & NQNFSNONCACHE) && uio->uio_iovcnt == 1) {
    647  1.24      fvdl 		    iomode = NFSV3WRITE_FILESYNC;
    648  1.24      fvdl 		    error = nfs_writerpc(vp, uio, cred, &iomode, &must_commit);
    649  1.24      fvdl 		    if (must_commit)
    650  1.24      fvdl 			nfs_clearcommit(vp->v_mount);
    651  1.24      fvdl 		    return (error);
    652  1.24      fvdl 		}
    653   1.1       cgd 		nfsstats.biocache_writes++;
    654   1.1       cgd 		lbn = uio->uio_offset / biosize;
    655   1.1       cgd 		on = uio->uio_offset & (biosize-1);
    656  1.11       cgd 		n = min((unsigned)(biosize - on), uio->uio_resid);
    657  1.12   mycroft 		bn = lbn * (biosize / DEV_BSIZE);
    658   1.1       cgd again:
    659  1.12   mycroft 		bp = nfs_getcacheblk(vp, bn, biosize, p);
    660  1.12   mycroft 		if (!bp)
    661  1.12   mycroft 			return (EINTR);
    662  1.12   mycroft 		if (bp->b_wcred == NOCRED) {
    663   1.1       cgd 			crhold(cred);
    664   1.1       cgd 			bp->b_wcred = cred;
    665   1.1       cgd 		}
    666  1.12   mycroft 		np->n_flag |= NMODIFIED;
    667  1.12   mycroft 		if (uio->uio_offset + n > np->n_size) {
    668  1.12   mycroft 			np->n_size = uio->uio_offset + n;
    669  1.41       mrg 			uvm_vnp_setsize(vp, np->n_size);
    670  1.12   mycroft 		}
    671  1.12   mycroft 
    672  1.12   mycroft 		/*
    673  1.12   mycroft 		 * If the new write will leave a contiguous dirty
    674  1.12   mycroft 		 * area, just update the b_dirtyoff and b_dirtyend,
    675  1.12   mycroft 		 * otherwise force a write rpc of the old dirty area.
    676  1.12   mycroft 		 */
    677  1.12   mycroft 		if (bp->b_dirtyend > 0 &&
    678  1.12   mycroft 		    (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) {
    679  1.12   mycroft 			bp->b_proc = p;
    680  1.12   mycroft 			if (VOP_BWRITE(bp) == EINTR)
    681  1.12   mycroft 				return (EINTR);
    682  1.12   mycroft 			goto again;
    683  1.12   mycroft 		}
    684  1.12   mycroft 
    685  1.12   mycroft 		/*
    686  1.12   mycroft 		 * Check for valid write lease and get one as required.
    687  1.12   mycroft 		 * In case getblk() and/or bwrite() delayed us.
    688  1.12   mycroft 		 */
    689  1.12   mycroft 		if ((nmp->nm_flag & NFSMNT_NQNFS) &&
    690  1.24      fvdl 		    NQNFS_CKINVALID(vp, np, ND_WRITE)) {
    691  1.12   mycroft 			do {
    692  1.24      fvdl 				error = nqnfs_getlease(vp, ND_WRITE, cred, p);
    693  1.12   mycroft 			} while (error == NQNFS_EXPIRED);
    694  1.12   mycroft 			if (error) {
    695  1.12   mycroft 				brelse(bp);
    696  1.12   mycroft 				return (error);
    697  1.12   mycroft 			}
    698  1.12   mycroft 			if (np->n_lrev != np->n_brev ||
    699  1.12   mycroft 			    (np->n_flag & NQNFSNONCACHE)) {
    700  1.12   mycroft 				brelse(bp);
    701  1.23  christos 				error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
    702  1.23  christos 				if (error)
    703   1.1       cgd 					return (error);
    704  1.12   mycroft 				np->n_brev = np->n_lrev;
    705   1.1       cgd 				goto again;
    706   1.1       cgd 			}
    707  1.12   mycroft 		}
    708  1.23  christos 		error = uiomove((char *)bp->b_data + on, n, uio);
    709  1.23  christos 		if (error) {
    710  1.12   mycroft 			bp->b_flags |= B_ERROR;
    711  1.12   mycroft 			brelse(bp);
    712  1.12   mycroft 			return (error);
    713  1.12   mycroft 		}
    714  1.12   mycroft 		if (bp->b_dirtyend > 0) {
    715  1.12   mycroft 			bp->b_dirtyoff = min(on, bp->b_dirtyoff);
    716  1.12   mycroft 			bp->b_dirtyend = max((on + n), bp->b_dirtyend);
    717   1.1       cgd 		} else {
    718   1.1       cgd 			bp->b_dirtyoff = on;
    719  1.12   mycroft 			bp->b_dirtyend = on + n;
    720   1.1       cgd 		}
    721  1.12   mycroft 		if (bp->b_validend == 0 || bp->b_validend < bp->b_dirtyoff ||
    722  1.12   mycroft 		    bp->b_validoff > bp->b_dirtyend) {
    723  1.12   mycroft 			bp->b_validoff = bp->b_dirtyoff;
    724  1.12   mycroft 			bp->b_validend = bp->b_dirtyend;
    725  1.12   mycroft 		} else {
    726  1.12   mycroft 			bp->b_validoff = min(bp->b_validoff, bp->b_dirtyoff);
    727  1.12   mycroft 			bp->b_validend = max(bp->b_validend, bp->b_dirtyend);
    728   1.1       cgd 		}
    729  1.25      fvdl 
    730  1.25      fvdl 		/*
    731  1.25      fvdl 		 * Since this block is being modified, it must be written
    732  1.25      fvdl 		 * again and not just committed.
    733  1.25      fvdl 		 */
    734  1.52      fvdl 		if (NFS_ISV3(vp)) {
    735  1.52      fvdl 			lockmgr(&np->n_commitlock, LK_EXCLUSIVE, NULL);
    736  1.52      fvdl 			if (bp->b_flags & B_NEEDCOMMIT) {
    737  1.52      fvdl 				bp->b_flags &= ~B_NEEDCOMMIT;
    738  1.52      fvdl 				nfs_del_tobecommitted_range(vp, bp);
    739  1.52      fvdl 			}
    740  1.52      fvdl 			nfs_del_committed_range(vp, bp);
    741  1.52      fvdl 			lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
    742  1.52      fvdl 		}
    743  1.25      fvdl 
    744  1.12   mycroft 		/*
    745  1.12   mycroft 		 * If the lease is non-cachable or IO_SYNC do bwrite().
    746  1.12   mycroft 		 */
    747  1.12   mycroft 		if ((np->n_flag & NQNFSNONCACHE) || (ioflag & IO_SYNC)) {
    748  1.12   mycroft 			bp->b_proc = p;
    749  1.24      fvdl 			error = VOP_BWRITE(bp);
    750  1.24      fvdl 			if (error)
    751  1.12   mycroft 				return (error);
    752  1.24      fvdl 			if (np->n_flag & NQNFSNONCACHE) {
    753  1.24      fvdl 				error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
    754  1.24      fvdl 				if (error)
    755  1.24      fvdl 					return (error);
    756  1.24      fvdl 			}
    757  1.12   mycroft 		} else if ((n + on) == biosize &&
    758  1.12   mycroft 			(nmp->nm_flag & NFSMNT_NQNFS) == 0) {
    759   1.1       cgd 			bp->b_proc = (struct proc *)0;
    760  1.52      fvdl 			bawrite(bp);
    761  1.24      fvdl 		} else {
    762  1.12   mycroft 			bdwrite(bp);
    763  1.24      fvdl 		}
    764  1.12   mycroft 	} while (uio->uio_resid > 0 && n > 0);
    765  1.12   mycroft 	return (0);
    766  1.12   mycroft }
    767  1.12   mycroft 
    768  1.12   mycroft /*
    769  1.12   mycroft  * Get an nfs cache block.
    770  1.12   mycroft  * Allocate a new one if the block isn't currently in the cache
    771  1.12   mycroft  * and return the block marked busy. If the calling process is
    772  1.12   mycroft  * interrupted by a signal for an interruptible mount point, return
    773  1.12   mycroft  * NULL.
    774  1.12   mycroft  */
    775  1.12   mycroft struct buf *
    776  1.12   mycroft nfs_getcacheblk(vp, bn, size, p)
    777  1.12   mycroft 	struct vnode *vp;
    778  1.12   mycroft 	daddr_t bn;
    779  1.12   mycroft 	int size;
    780  1.12   mycroft 	struct proc *p;
    781  1.12   mycroft {
    782  1.48  augustss 	struct buf *bp;
    783  1.12   mycroft 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
    784  1.12   mycroft 
    785  1.12   mycroft 	if (nmp->nm_flag & NFSMNT_INT) {
    786  1.12   mycroft 		bp = getblk(vp, bn, size, PCATCH, 0);
    787  1.12   mycroft 		while (bp == (struct buf *)0) {
    788  1.12   mycroft 			if (nfs_sigintr(nmp, (struct nfsreq *)0, p))
    789  1.12   mycroft 				return ((struct buf *)0);
    790  1.12   mycroft 			bp = getblk(vp, bn, size, 0, 2 * hz);
    791  1.12   mycroft 		}
    792  1.12   mycroft 	} else
    793  1.12   mycroft 		bp = getblk(vp, bn, size, 0, 0);
    794  1.12   mycroft 	return (bp);
    795  1.12   mycroft }
    796  1.12   mycroft 
    797  1.12   mycroft /*
    798  1.12   mycroft  * Flush and invalidate all dirty buffers. If another process is already
    799  1.12   mycroft  * doing the flush, just wait for completion.
    800  1.12   mycroft  */
    801  1.23  christos int
    802  1.12   mycroft nfs_vinvalbuf(vp, flags, cred, p, intrflg)
    803  1.12   mycroft 	struct vnode *vp;
    804  1.12   mycroft 	int flags;
    805  1.12   mycroft 	struct ucred *cred;
    806  1.12   mycroft 	struct proc *p;
    807  1.12   mycroft 	int intrflg;
    808  1.12   mycroft {
    809  1.48  augustss 	struct nfsnode *np = VTONFS(vp);
    810  1.12   mycroft 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
    811  1.12   mycroft 	int error = 0, slpflag, slptimeo;
    812  1.12   mycroft 
    813  1.12   mycroft 	if ((nmp->nm_flag & NFSMNT_INT) == 0)
    814  1.12   mycroft 		intrflg = 0;
    815  1.12   mycroft 	if (intrflg) {
    816  1.12   mycroft 		slpflag = PCATCH;
    817  1.12   mycroft 		slptimeo = 2 * hz;
    818  1.12   mycroft 	} else {
    819  1.12   mycroft 		slpflag = 0;
    820  1.12   mycroft 		slptimeo = 0;
    821  1.12   mycroft 	}
    822  1.12   mycroft 	/*
    823  1.12   mycroft 	 * First wait for any other process doing a flush to complete.
    824  1.12   mycroft 	 */
    825  1.12   mycroft 	while (np->n_flag & NFLUSHINPROG) {
    826  1.12   mycroft 		np->n_flag |= NFLUSHWANT;
    827  1.12   mycroft 		error = tsleep((caddr_t)&np->n_flag, PRIBIO + 2, "nfsvinval",
    828  1.12   mycroft 			slptimeo);
    829  1.12   mycroft 		if (error && intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, p))
    830  1.12   mycroft 			return (EINTR);
    831  1.12   mycroft 	}
    832  1.12   mycroft 
    833  1.12   mycroft 	/*
    834  1.12   mycroft 	 * Now, flush as required.
    835  1.12   mycroft 	 */
    836  1.12   mycroft 	np->n_flag |= NFLUSHINPROG;
    837  1.12   mycroft 	error = vinvalbuf(vp, flags, cred, p, slpflag, 0);
    838  1.12   mycroft 	while (error) {
    839  1.12   mycroft 		if (intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, p)) {
    840  1.12   mycroft 			np->n_flag &= ~NFLUSHINPROG;
    841  1.12   mycroft 			if (np->n_flag & NFLUSHWANT) {
    842  1.12   mycroft 				np->n_flag &= ~NFLUSHWANT;
    843  1.12   mycroft 				wakeup((caddr_t)&np->n_flag);
    844  1.12   mycroft 			}
    845  1.12   mycroft 			return (EINTR);
    846  1.12   mycroft 		}
    847  1.12   mycroft 		error = vinvalbuf(vp, flags, cred, p, 0, slptimeo);
    848  1.12   mycroft 	}
    849  1.12   mycroft 	np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
    850  1.12   mycroft 	if (np->n_flag & NFLUSHWANT) {
    851  1.12   mycroft 		np->n_flag &= ~NFLUSHWANT;
    852  1.12   mycroft 		wakeup((caddr_t)&np->n_flag);
    853  1.12   mycroft 	}
    854  1.12   mycroft 	return (0);
    855  1.12   mycroft }
    856  1.12   mycroft 
    857  1.12   mycroft /*
    858  1.12   mycroft  * Initiate asynchronous I/O. Return an error if no nfsiods are available.
    859  1.12   mycroft  * This is mainly to avoid queueing async I/O requests when the nfsiods
    860  1.12   mycroft  * are all hung on a dead server.
    861  1.12   mycroft  */
    862  1.23  christos int
    863  1.12   mycroft nfs_asyncio(bp, cred)
    864  1.48  augustss 	struct buf *bp;
    865  1.12   mycroft 	struct ucred *cred;
    866  1.12   mycroft {
    867  1.48  augustss 	int i;
    868  1.48  augustss 	struct nfsmount *nmp;
    869  1.30   thorpej 	int gotiod, slpflag = 0, slptimeo = 0, error;
    870  1.12   mycroft 
    871  1.12   mycroft 	if (nfs_numasync == 0)
    872  1.12   mycroft 		return (EIO);
    873  1.30   thorpej 
    874  1.30   thorpej 
    875  1.30   thorpej 	nmp = VFSTONFS(bp->b_vp->v_mount);
    876  1.30   thorpej again:
    877  1.30   thorpej 	if (nmp->nm_flag & NFSMNT_INT)
    878  1.30   thorpej 		slpflag = PCATCH;
    879  1.30   thorpej 	gotiod = FALSE;
    880  1.30   thorpej 
    881  1.30   thorpej 	/*
    882  1.30   thorpej 	 * Find a free iod to process this request.
    883  1.30   thorpej 	 */
    884  1.30   thorpej 
    885  1.12   mycroft 	for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
    886  1.30   thorpej 		if (nfs_iodwant[i]) {
    887  1.30   thorpej 			/*
    888  1.30   thorpej 			 * Found one, so wake it up and tell it which
    889  1.30   thorpej 			 * mount to process.
    890  1.30   thorpej 			 */
    891  1.30   thorpej 			nfs_iodwant[i] = (struct proc *)0;
    892  1.30   thorpej 			nfs_iodmount[i] = nmp;
    893  1.30   thorpej 			nmp->nm_bufqiods++;
    894  1.30   thorpej 			wakeup((caddr_t)&nfs_iodwant[i]);
    895  1.30   thorpej 			gotiod = TRUE;
    896  1.31      fvdl 			break;
    897  1.30   thorpej 		}
    898  1.30   thorpej 	/*
    899  1.30   thorpej 	 * If none are free, we may already have an iod working on this mount
    900  1.30   thorpej 	 * point.  If so, it will process our request.
    901  1.30   thorpej 	 */
    902  1.30   thorpej 	if (!gotiod && nmp->nm_bufqiods > 0)
    903  1.30   thorpej 		gotiod = TRUE;
    904  1.30   thorpej 
    905  1.30   thorpej 	/*
    906  1.30   thorpej 	 * If we have an iod which can process the request, then queue
    907  1.30   thorpej 	 * the buffer.
    908  1.30   thorpej 	 */
    909  1.30   thorpej 	if (gotiod) {
    910  1.30   thorpej 		/*
    911  1.30   thorpej 		 * Ensure that the queue never grows too large.
    912  1.30   thorpej 		 */
    913  1.30   thorpej 		while (nmp->nm_bufqlen >= 2*nfs_numasync) {
    914  1.30   thorpej 			nmp->nm_bufqwant = TRUE;
    915  1.30   thorpej 			error = tsleep(&nmp->nm_bufq, slpflag | PRIBIO,
    916  1.30   thorpej 				"nfsaio", slptimeo);
    917  1.30   thorpej 			if (error) {
    918  1.30   thorpej 				if (nfs_sigintr(nmp, NULL, bp->b_proc))
    919  1.30   thorpej 					return (EINTR);
    920  1.30   thorpej 				if (slpflag == PCATCH) {
    921  1.30   thorpej 					slpflag = 0;
    922  1.30   thorpej 					slptimeo = 2 * hz;
    923  1.30   thorpej 				}
    924  1.30   thorpej 			}
    925  1.30   thorpej 			/*
    926  1.30   thorpej 			 * We might have lost our iod while sleeping,
    927  1.30   thorpej 			 * so check and loop if nescessary.
    928  1.30   thorpej 			 */
    929  1.30   thorpej 			if (nmp->nm_bufqiods == 0)
    930  1.30   thorpej 				goto again;
    931  1.30   thorpej 		}
    932  1.30   thorpej 
    933  1.12   mycroft 		if (bp->b_flags & B_READ) {
    934  1.12   mycroft 			if (bp->b_rcred == NOCRED && cred != NOCRED) {
    935  1.12   mycroft 				crhold(cred);
    936  1.12   mycroft 				bp->b_rcred = cred;
    937  1.12   mycroft 			}
    938   1.1       cgd 		} else {
    939  1.24      fvdl 			bp->b_flags |= B_WRITEINPROG;
    940  1.12   mycroft 			if (bp->b_wcred == NOCRED && cred != NOCRED) {
    941  1.12   mycroft 				crhold(cred);
    942  1.12   mycroft 				bp->b_wcred = cred;
    943  1.12   mycroft 			}
    944  1.12   mycroft 		}
    945  1.47      fvdl 
    946  1.30   thorpej 		TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist);
    947  1.30   thorpej 		nmp->nm_bufqlen++;
    948  1.12   mycroft 		return (0);
    949  1.12   mycroft 	    }
    950  1.24      fvdl 
    951  1.24      fvdl 	/*
    952  1.30   thorpej 	 * All the iods are busy on other mounts, so return EIO to
    953  1.30   thorpej 	 * force the caller to process the i/o synchronously.
    954  1.24      fvdl 	 */
    955  1.30   thorpej 	return (EIO);
    956  1.12   mycroft }
    957  1.12   mycroft 
    958  1.12   mycroft /*
    959  1.12   mycroft  * Do an I/O operation to/from a cache block. This may be called
    960  1.12   mycroft  * synchronously or from an nfsiod.
    961  1.12   mycroft  */
    962  1.12   mycroft int
    963  1.12   mycroft nfs_doio(bp, cr, p)
    964  1.48  augustss 	struct buf *bp;
    965  1.23  christos 	struct ucred *cr;
    966  1.12   mycroft 	struct proc *p;
    967  1.12   mycroft {
    968  1.48  augustss 	struct uio *uiop;
    969  1.48  augustss 	struct vnode *vp;
    970  1.12   mycroft 	struct nfsnode *np;
    971  1.12   mycroft 	struct nfsmount *nmp;
    972  1.52      fvdl 	int error = 0, diff, len, iomode, must_commit = 0, s, retv = 0;
    973  1.52      fvdl 	int pushedrange;
    974  1.52      fvdl 	unsigned cnt;
    975  1.12   mycroft 	struct uio uio;
    976  1.12   mycroft 	struct iovec io;
    977  1.52      fvdl 	off_t off;
    978  1.12   mycroft 
    979  1.12   mycroft 	vp = bp->b_vp;
    980  1.12   mycroft 	np = VTONFS(vp);
    981  1.12   mycroft 	nmp = VFSTONFS(vp->v_mount);
    982  1.12   mycroft 	uiop = &uio;
    983  1.12   mycroft 	uiop->uio_iov = &io;
    984  1.12   mycroft 	uiop->uio_iovcnt = 1;
    985  1.12   mycroft 	uiop->uio_segflg = UIO_SYSSPACE;
    986  1.12   mycroft 	uiop->uio_procp = p;
    987  1.12   mycroft 
    988  1.12   mycroft 	/*
    989  1.14        pk 	 * Historically, paging was done with physio, but no more...
    990  1.12   mycroft 	 */
    991  1.14        pk 	if (bp->b_flags & B_PHYS) {
    992  1.14        pk 	    /*
    993  1.14        pk 	     * ...though reading /dev/drum still gets us here.
    994  1.14        pk 	     */
    995  1.14        pk 	    io.iov_len = uiop->uio_resid = bp->b_bcount;
    996  1.14        pk 	    /* mapping was done by vmapbuf() */
    997  1.14        pk 	    io.iov_base = bp->b_data;
    998  1.24      fvdl 	    uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
    999  1.14        pk 	    if (bp->b_flags & B_READ) {
   1000  1.14        pk 		uiop->uio_rw = UIO_READ;
   1001  1.14        pk 		nfsstats.read_physios++;
   1002  1.14        pk 		error = nfs_readrpc(vp, uiop, cr);
   1003  1.14        pk 	    } else {
   1004  1.24      fvdl 		iomode = NFSV3WRITE_DATASYNC;
   1005  1.14        pk 		uiop->uio_rw = UIO_WRITE;
   1006  1.14        pk 		nfsstats.write_physios++;
   1007  1.24      fvdl 		error = nfs_writerpc(vp, uiop, cr, &iomode, &must_commit);
   1008  1.14        pk 	    }
   1009  1.14        pk 	    if (error) {
   1010  1.14        pk 		bp->b_flags |= B_ERROR;
   1011  1.14        pk 		bp->b_error = error;
   1012  1.14        pk 	    }
   1013  1.14        pk 	} else if (bp->b_flags & B_READ) {
   1014  1.12   mycroft 	    io.iov_len = uiop->uio_resid = bp->b_bcount;
   1015  1.12   mycroft 	    io.iov_base = bp->b_data;
   1016  1.12   mycroft 	    uiop->uio_rw = UIO_READ;
   1017  1.12   mycroft 	    switch (vp->v_type) {
   1018  1.12   mycroft 	    case VREG:
   1019  1.24      fvdl 		uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
   1020  1.12   mycroft 		nfsstats.read_bios++;
   1021  1.12   mycroft 		error = nfs_readrpc(vp, uiop, cr);
   1022  1.12   mycroft 		if (!error) {
   1023  1.12   mycroft 		    bp->b_validoff = 0;
   1024  1.12   mycroft 		    if (uiop->uio_resid) {
   1025  1.12   mycroft 			/*
   1026  1.12   mycroft 			 * If len > 0, there is a hole in the file and
   1027  1.12   mycroft 			 * no writes after the hole have been pushed to
   1028  1.12   mycroft 			 * the server yet.
   1029  1.12   mycroft 			 * Just zero fill the rest of the valid area.
   1030  1.12   mycroft 			 */
   1031  1.12   mycroft 			diff = bp->b_bcount - uiop->uio_resid;
   1032  1.24      fvdl 			len = np->n_size - (((u_quad_t)bp->b_blkno) * DEV_BSIZE
   1033  1.12   mycroft 				+ diff);
   1034  1.12   mycroft 			if (len > 0) {
   1035  1.12   mycroft 			    len = min(len, uiop->uio_resid);
   1036  1.44     perry 			    memset((char *)bp->b_data + diff, 0, len);
   1037  1.12   mycroft 			    bp->b_validend = diff + len;
   1038  1.12   mycroft 			} else
   1039  1.12   mycroft 			    bp->b_validend = diff;
   1040  1.12   mycroft 		    } else
   1041  1.12   mycroft 			bp->b_validend = bp->b_bcount;
   1042  1.12   mycroft 		}
   1043  1.12   mycroft 		if (p && (vp->v_flag & VTEXT) &&
   1044  1.12   mycroft 			(((nmp->nm_flag & NFSMNT_NQNFS) &&
   1045  1.24      fvdl 			  NQNFS_CKINVALID(vp, np, ND_READ) &&
   1046  1.12   mycroft 			  np->n_lrev != np->n_brev) ||
   1047  1.12   mycroft 			 (!(nmp->nm_flag & NFSMNT_NQNFS) &&
   1048  1.35      fvdl 			  np->n_mtime != np->n_vattr->va_mtime.tv_sec))) {
   1049  1.12   mycroft 			uprintf("Process killed due to text file modification\n");
   1050  1.12   mycroft 			psignal(p, SIGKILL);
   1051  1.13   mycroft 			p->p_holdcnt++;
   1052  1.12   mycroft 		}
   1053  1.12   mycroft 		break;
   1054  1.12   mycroft 	    case VLNK:
   1055  1.24      fvdl 		uiop->uio_offset = (off_t)0;
   1056  1.12   mycroft 		nfsstats.readlink_bios++;
   1057  1.12   mycroft 		error = nfs_readlinkrpc(vp, uiop, cr);
   1058  1.12   mycroft 		break;
   1059  1.12   mycroft 	    case VDIR:
   1060  1.12   mycroft 		nfsstats.readdir_bios++;
   1061  1.34      fvdl 		uiop->uio_offset = bp->b_dcookie;
   1062  1.24      fvdl 		if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
   1063  1.24      fvdl 			error = nfs_readdirplusrpc(vp, uiop, cr);
   1064  1.24      fvdl 			if (error == NFSERR_NOTSUPP)
   1065  1.24      fvdl 				nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
   1066  1.24      fvdl 		}
   1067  1.24      fvdl 		if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
   1068  1.24      fvdl 			error = nfs_readdirrpc(vp, uiop, cr);
   1069  1.34      fvdl 		if (!error) {
   1070  1.34      fvdl 			bp->b_dcookie = uiop->uio_offset;
   1071  1.34      fvdl 			bp->b_validoff = 0;
   1072  1.34      fvdl 			bp->b_validend = bp->b_bcount - uiop->uio_resid;
   1073  1.34      fvdl 		}
   1074  1.24      fvdl 		break;
   1075  1.24      fvdl 	    default:
   1076  1.29  christos 		printf("nfs_doio:  type %x unexpected\n",vp->v_type);
   1077  1.12   mycroft 		break;
   1078  1.12   mycroft 	    };
   1079  1.12   mycroft 	    if (error) {
   1080  1.12   mycroft 		bp->b_flags |= B_ERROR;
   1081  1.12   mycroft 		bp->b_error = error;
   1082  1.12   mycroft 	    }
   1083  1.12   mycroft 	} else {
   1084  1.52      fvdl 	    /*
   1085  1.52      fvdl 	     * If B_NEEDCOMMIT is set, a commit rpc may do the trick. If not
   1086  1.52      fvdl 	     * an actual write will have to be scheduled.
   1087  1.52      fvdl 	     */
   1088  1.52      fvdl 	    if (bp->b_flags & B_NEEDCOMMIT) {
   1089  1.52      fvdl 		/*
   1090  1.52      fvdl 		 * If the buffer is in the range that we already committed,
   1091  1.52      fvdl 		 * there's nothing to do.
   1092  1.52      fvdl 		 *
   1093  1.52      fvdl 		 * If it's in the range that we need to commit, push the
   1094  1.52      fvdl 		 * whole range at once. Else only push the buffer. In
   1095  1.52      fvdl 		 * both these cases, acquire the commit lock to avoid
   1096  1.52      fvdl 		 * other processes modifying the range. Normally the
   1097  1.52      fvdl 		 * vnode lock should have handled this, but there are
   1098  1.52      fvdl 		 * no proper vnode locks for NFS yet (XXX).
   1099  1.52      fvdl 		 */
   1100  1.52      fvdl 		lockmgr(&np->n_commitlock, LK_EXCLUSIVE, NULL);
   1101  1.52      fvdl 		if (!(bp->b_flags & B_NEEDCOMMIT)) {
   1102  1.52      fvdl 			lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
   1103  1.52      fvdl 			goto dowrite;
   1104  1.52      fvdl 		}
   1105  1.52      fvdl 		if (!nfs_in_committed_range(vp, bp)) {
   1106  1.52      fvdl 			if (nfs_in_tobecommitted_range(vp, bp)) {
   1107  1.52      fvdl 				pushedrange = 1;
   1108  1.52      fvdl 				off = np->n_pushlo;
   1109  1.52      fvdl 				/* XXX will be too big if > 2G buffer cache */
   1110  1.52      fvdl 				cnt = np->n_pushhi - np->n_pushlo;
   1111  1.52      fvdl 			} else {
   1112  1.52      fvdl 				pushedrange = 0;
   1113  1.52      fvdl 				off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE;
   1114  1.52      fvdl 				cnt = bp->b_dirtyend;
   1115  1.52      fvdl 			}
   1116  1.52      fvdl 			bp->b_flags |= B_WRITEINPROG;
   1117  1.52      fvdl 			retv = nfs_commit(bp->b_vp, off, cnt,
   1118  1.52      fvdl 			    bp->b_wcred, bp->b_proc);
   1119  1.52      fvdl 
   1120  1.52      fvdl 			bp->b_flags &= ~B_WRITEINPROG;
   1121  1.52      fvdl 			if (retv == 0) {
   1122  1.52      fvdl 				if (pushedrange) {
   1123  1.52      fvdl 					nfs_merge_commit_ranges(vp);
   1124  1.52      fvdl 				}
   1125  1.52      fvdl 				else
   1126  1.52      fvdl 					nfs_add_committed_range(vp, bp);
   1127  1.52      fvdl 			}
   1128  1.52      fvdl 		}
   1129  1.52      fvdl 		lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
   1130  1.52      fvdl 		if (!retv) {
   1131  1.52      fvdl 			bp->b_resid = bp->b_dirtyoff = bp->b_dirtyend = 0;
   1132  1.52      fvdl 			bp->b_flags &= ~B_NEEDCOMMIT;
   1133  1.52      fvdl 			biodone(bp);
   1134  1.52      fvdl 			return (0);
   1135  1.52      fvdl 		} else if (retv == NFSERR_STALEWRITEVERF)
   1136  1.52      fvdl 			nfs_clearcommit(bp->b_vp->v_mount);
   1137  1.52      fvdl 	    }
   1138  1.52      fvdl dowrite:
   1139  1.12   mycroft 	    io.iov_len = uiop->uio_resid = bp->b_dirtyend
   1140  1.12   mycroft 		- bp->b_dirtyoff;
   1141  1.24      fvdl 	    uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE
   1142  1.12   mycroft 		+ bp->b_dirtyoff;
   1143  1.12   mycroft 	    io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
   1144  1.12   mycroft 	    uiop->uio_rw = UIO_WRITE;
   1145  1.12   mycroft 	    nfsstats.write_bios++;
   1146  1.24      fvdl 	    if ((bp->b_flags & (B_ASYNC | B_NEEDCOMMIT | B_NOCACHE)) == B_ASYNC)
   1147  1.24      fvdl 		iomode = NFSV3WRITE_UNSTABLE;
   1148  1.24      fvdl 	    else
   1149  1.24      fvdl 		iomode = NFSV3WRITE_FILESYNC;
   1150  1.52      fvdl 
   1151  1.24      fvdl 	    bp->b_flags |= B_WRITEINPROG;
   1152  1.24      fvdl 	    error = nfs_writerpc(vp, uiop, cr, &iomode, &must_commit);
   1153  1.47      fvdl 	    s = splbio();
   1154  1.52      fvdl 	    if (!error && iomode == NFSV3WRITE_UNSTABLE) {
   1155  1.24      fvdl 		bp->b_flags |= B_NEEDCOMMIT;
   1156  1.52      fvdl 		lockmgr(&np->n_commitlock, LK_EXCLUSIVE, NULL);
   1157  1.52      fvdl 		nfs_add_tobecommitted_range(vp, bp);
   1158  1.52      fvdl 		lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
   1159  1.52      fvdl 	    } else if (!error && bp->b_flags & B_NEEDCOMMIT) {
   1160  1.24      fvdl 		bp->b_flags &= ~B_NEEDCOMMIT;
   1161  1.52      fvdl 		lockmgr(&np->n_commitlock, LK_EXCLUSIVE, NULL);
   1162  1.52      fvdl 		nfs_del_committed_range(vp, bp);
   1163  1.52      fvdl 		lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
   1164  1.52      fvdl 	    }
   1165  1.52      fvdl 	    /* XXX the use of NOCACHE is a hack */
   1166  1.52      fvdl 	    bp->b_flags &= ~(B_WRITEINPROG|B_NOCACHE);
   1167  1.12   mycroft 
   1168  1.12   mycroft 	    /*
   1169  1.12   mycroft 	     * For an interrupted write, the buffer is still valid and the
   1170  1.12   mycroft 	     * write hasn't been pushed to the server yet, so we can't set
   1171  1.12   mycroft 	     * B_ERROR and report the interruption by setting B_EINTR. For
   1172  1.12   mycroft 	     * the B_ASYNC case, B_EINTR is not relevant, so the rpc attempt
   1173  1.12   mycroft 	     * is essentially a noop.
   1174  1.24      fvdl 	     * For the case of a V3 write rpc not being committed to stable
   1175  1.24      fvdl 	     * storage, the block is still dirty and requires either a commit
   1176  1.24      fvdl 	     * rpc or another write rpc with iomode == NFSV3WRITE_FILESYNC
   1177  1.24      fvdl 	     * before the block is reused. This is indicated by setting the
   1178  1.24      fvdl 	     * B_DELWRI and B_NEEDCOMMIT flags.
   1179  1.12   mycroft 	     */
   1180  1.24      fvdl 	    if (error == EINTR || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
   1181  1.12   mycroft 		bp->b_flags |= B_DELWRI;
   1182  1.12   mycroft 		/*
   1183  1.52      fvdl 		 * A B_ASYNC block still needs to be committed, so put
   1184  1.52      fvdl 		 * it back on the dirty list.
   1185  1.12   mycroft 		 */
   1186  1.52      fvdl 		if (bp->b_flags & B_ASYNC)
   1187  1.52      fvdl 			reassignbuf(bp, vp);
   1188  1.52      fvdl 		else if (error)
   1189  1.12   mycroft 		    bp->b_flags |= B_EINTR;
   1190  1.12   mycroft 	    } else {
   1191  1.12   mycroft 		if (error) {
   1192  1.12   mycroft 		    bp->b_flags |= B_ERROR;
   1193  1.12   mycroft 		    bp->b_error = np->n_error = error;
   1194  1.12   mycroft 		    np->n_flag |= NWRITEERR;
   1195   1.1       cgd 		}
   1196  1.12   mycroft 		bp->b_dirtyoff = bp->b_dirtyend = 0;
   1197  1.12   mycroft 	    }
   1198  1.47      fvdl 	    splx(s);
   1199   1.1       cgd 	}
   1200  1.12   mycroft 	bp->b_resid = uiop->uio_resid;
   1201  1.24      fvdl 	if (must_commit)
   1202  1.24      fvdl 		nfs_clearcommit(vp->v_mount);
   1203  1.12   mycroft 	biodone(bp);
   1204   1.1       cgd 	return (error);
   1205   1.1       cgd }
   1206