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