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