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