Home | History | Annotate | Line # | Download | only in nfs
nfs_bio.c revision 1.63.2.1
      1 /*	$NetBSD: nfs_bio.c,v 1.63.2.1 2001/03/05 22:49:58 nathanw 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 #include "opt_nfs.h"
     42 #include "opt_ddb.h"
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/resourcevar.h>
     47 #include <sys/signalvar.h>
     48 #include <sys/lwp.h>
     49 #include <sys/proc.h>
     50 #include <sys/buf.h>
     51 #include <sys/vnode.h>
     52 #include <sys/mount.h>
     53 #include <sys/kernel.h>
     54 #include <sys/namei.h>
     55 #include <sys/dirent.h>
     56 #include <sys/malloc.h>
     57 
     58 #include <uvm/uvm_extern.h>
     59 #include <uvm/uvm.h>
     60 
     61 #include <nfs/rpcv2.h>
     62 #include <nfs/nfsproto.h>
     63 #include <nfs/nfs.h>
     64 #include <nfs/nfsmount.h>
     65 #include <nfs/nqnfs.h>
     66 #include <nfs/nfsnode.h>
     67 #include <nfs/nfs_var.h>
     68 
     69 extern int nfs_numasync;
     70 extern struct nfsstats nfsstats;
     71 
     72 /*
     73  * Vnode op for read using bio
     74  * Any similarity to readip() is purely coincidental
     75  */
     76 int
     77 nfs_bioread(vp, uio, ioflag, cred, cflag)
     78 	struct vnode *vp;
     79 	struct uio *uio;
     80 	int ioflag, cflag;
     81 	struct ucred *cred;
     82 {
     83 	struct nfsnode *np = VTONFS(vp);
     84 	int biosize;
     85 	struct buf *bp = NULL, *rabp;
     86 	struct vattr vattr;
     87 	struct proc *p;
     88 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
     89 	struct nfsdircache *ndp = NULL, *nndp = NULL;
     90 	caddr_t baddr, ep, edp;
     91 	int got_buf = 0, error = 0, n = 0, on = 0, en, enn;
     92 	int enough = 0;
     93 	struct dirent *dp, *pdp;
     94 	off_t curoff = 0;
     95 
     96 #ifdef DIAGNOSTIC
     97 	if (uio->uio_rw != UIO_READ)
     98 		panic("nfs_read mode");
     99 #endif
    100 	if (uio->uio_resid == 0)
    101 		return (0);
    102 	if (vp->v_type != VDIR && uio->uio_offset < 0)
    103 		return (EINVAL);
    104 	p = uio->uio_procp;
    105 #ifndef NFS_V2_ONLY
    106 	if ((nmp->nm_flag & NFSMNT_NFSV3) &&
    107 	    !(nmp->nm_iflag & NFSMNT_GOTFSINFO))
    108 		(void)nfs_fsinfo(nmp, vp, cred, p);
    109 #endif
    110 	if (vp->v_type != VDIR &&
    111 	    (uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
    112 		return (EFBIG);
    113 	biosize = nmp->nm_rsize;
    114 
    115 	/*
    116 	 * For nfs, cache consistency can only be maintained approximately.
    117 	 * Although RFC1094 does not specify the criteria, the following is
    118 	 * believed to be compatible with the reference port.
    119 	 * For nqnfs, full cache consistency is maintained within the loop.
    120 	 * For nfs:
    121 	 * If the file's modify time on the server has changed since the
    122 	 * last read rpc or you have written to the file,
    123 	 * you may have lost data cache consistency with the
    124 	 * server, so flush all of the file's data out of the cache.
    125 	 * Then force a getattr rpc to ensure that you have up to date
    126 	 * attributes.
    127 	 * NB: This implies that cache data can be read when up to
    128 	 * NFS_ATTRTIMEO seconds out of date. If you find that you need current
    129 	 * attributes this could be forced by setting n_attrstamp to 0 before
    130 	 * the VOP_GETATTR() call.
    131 	 */
    132 
    133 	if ((nmp->nm_flag & NFSMNT_NQNFS) == 0 && vp->v_type != VLNK) {
    134 		if (np->n_flag & NMODIFIED) {
    135 			if (vp->v_type != VREG) {
    136 				if (vp->v_type != VDIR)
    137 					panic("nfs: bioread, not dir");
    138 				nfs_invaldircache(vp, 0);
    139 				np->n_direofoffset = 0;
    140 				error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
    141 				if (error)
    142 					return (error);
    143 			}
    144 			np->n_attrstamp = 0;
    145 			error = VOP_GETATTR(vp, &vattr, cred, p);
    146 			if (error)
    147 				return (error);
    148 			np->n_mtime = vattr.va_mtime.tv_sec;
    149 		} else {
    150 			error = VOP_GETATTR(vp, &vattr, cred, p);
    151 			if (error)
    152 				return (error);
    153 			if (np->n_mtime != vattr.va_mtime.tv_sec) {
    154 				if (vp->v_type == VDIR) {
    155 					nfs_invaldircache(vp, 0);
    156 					np->n_direofoffset = 0;
    157 				}
    158 				error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
    159 				if (error)
    160 					return (error);
    161 				np->n_mtime = vattr.va_mtime.tv_sec;
    162 			}
    163 		}
    164 	}
    165 
    166 	/*
    167 	 * update the cached read creds for this node.
    168 	 */
    169 
    170 	if (np->n_rcred) {
    171 		crfree(np->n_rcred);
    172 	}
    173 	np->n_rcred = cred;
    174 	crhold(cred);
    175 
    176 	do {
    177 #ifndef NFS_V2_ONLY
    178 	    /*
    179 	     * Get a valid lease. If cached data is stale, flush it.
    180 	     */
    181 	    if (nmp->nm_flag & NFSMNT_NQNFS) {
    182 		if (NQNFS_CKINVALID(vp, np, ND_READ)) {
    183 		    do {
    184 			error = nqnfs_getlease(vp, ND_READ, cred, p);
    185 		    } while (error == NQNFS_EXPIRED);
    186 		    if (error)
    187 			return (error);
    188 		    if (np->n_lrev != np->n_brev ||
    189 			(np->n_flag & NQNFSNONCACHE) ||
    190 			((np->n_flag & NMODIFIED) && vp->v_type == VDIR)) {
    191 			if (vp->v_type == VDIR) {
    192 				nfs_invaldircache(vp, 0);
    193 				np->n_direofoffset = 0;
    194 			}
    195 			error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
    196 			if (error)
    197 			    return (error);
    198 			np->n_brev = np->n_lrev;
    199 		    }
    200 		} else if (vp->v_type == VDIR && (np->n_flag & NMODIFIED)) {
    201 		    nfs_invaldircache(vp, 0);
    202 		    error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
    203 		    np->n_direofoffset = 0;
    204 		    if (error)
    205 			return (error);
    206 		}
    207 	    }
    208 #endif
    209 	    /*
    210 	     * Don't cache symlinks.
    211 	     */
    212 	    if (np->n_flag & NQNFSNONCACHE
    213 		|| ((vp->v_flag & VROOT) && vp->v_type == VLNK)) {
    214 		switch (vp->v_type) {
    215 		case VREG:
    216 			return (nfs_readrpc(vp, uio));
    217 		case VLNK:
    218 			return (nfs_readlinkrpc(vp, uio, cred));
    219 		case VDIR:
    220 			break;
    221 		default:
    222 			printf(" NQNFSNONCACHE: type %x unexpected\n",
    223 			    vp->v_type);
    224 		};
    225 	    }
    226 	    baddr = (caddr_t)0;
    227 	    switch (vp->v_type) {
    228 	    case VREG:
    229 		nfsstats.biocache_reads++;
    230 
    231 		error = 0;
    232 		while (uio->uio_resid > 0) {
    233 			void *win;
    234 			vsize_t bytelen = MIN(np->n_size - uio->uio_offset,
    235 					      uio->uio_resid);
    236 
    237 			if (bytelen == 0)
    238 				break;
    239 			win = ubc_alloc(&vp->v_uvm.u_obj, uio->uio_offset,
    240 					&bytelen, UBC_READ);
    241 			error = uiomove(win, bytelen, uio);
    242 			ubc_release(win, 0);
    243 			if (error) {
    244 				break;
    245 			}
    246 		}
    247 		n = 0;
    248 		break;
    249 
    250 	    case VLNK:
    251 		nfsstats.biocache_readlinks++;
    252 		bp = nfs_getcacheblk(vp, (daddr_t)0, NFS_MAXPATHLEN, p);
    253 		if (!bp)
    254 			return (EINTR);
    255 		if ((bp->b_flags & B_DONE) == 0) {
    256 			bp->b_flags |= B_READ;
    257 			error = nfs_doio(bp, p);
    258 			if (error) {
    259 				brelse(bp);
    260 				return (error);
    261 			}
    262 		}
    263 		n = MIN(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid);
    264 		got_buf = 1;
    265 		on = 0;
    266 		break;
    267 	    case VDIR:
    268 diragain:
    269 		nfsstats.biocache_readdirs++;
    270 		ndp = nfs_searchdircache(vp, uio->uio_offset,
    271 			(nmp->nm_flag & NFSMNT_XLATECOOKIE), 0);
    272 		if (!ndp) {
    273 			/*
    274 			 * We've been handed a cookie that is not
    275 			 * in the cache. If we're not translating
    276 			 * 32 <-> 64, it may be a value that was
    277 			 * flushed out of the cache because it grew
    278 			 * too big. Let the server judge if it's
    279 			 * valid or not. In the translation case,
    280 			 * we have no way of validating this value,
    281 			 * so punt.
    282 			 */
    283 			if (nmp->nm_flag & NFSMNT_XLATECOOKIE)
    284 				return (EINVAL);
    285 			ndp = nfs_enterdircache(vp, uio->uio_offset,
    286 				uio->uio_offset, 0, 0);
    287 		}
    288 
    289 		if (uio->uio_offset != 0 &&
    290 		    ndp->dc_cookie == np->n_direofoffset) {
    291 			nfsstats.direofcache_hits++;
    292 			return (0);
    293 		}
    294 
    295 		bp = nfs_getcacheblk(vp, ndp->dc_blkno, NFS_DIRBLKSIZ, p);
    296 		if (!bp)
    297 		    return (EINTR);
    298 		if ((bp->b_flags & B_DONE) == 0) {
    299 		    bp->b_flags |= B_READ;
    300 		    bp->b_dcookie = ndp->dc_blkcookie;
    301 		    error = nfs_doio(bp, p);
    302 		    if (error) {
    303 			/*
    304 			 * Yuck! The directory has been modified on the
    305 			 * server. Punt and let the userland code
    306 			 * deal with it.
    307 			 */
    308 			brelse(bp);
    309 			if (error == NFSERR_BAD_COOKIE) {
    310 			    nfs_invaldircache(vp, 0);
    311 			    nfs_vinvalbuf(vp, 0, cred, p, 1);
    312 			    error = EINVAL;
    313 			}
    314 			return (error);
    315 		    }
    316 		}
    317 
    318 		/*
    319 		 * Just return if we hit EOF right away with this
    320 		 * block. Always check here, because direofoffset
    321 		 * may have been set by an nfsiod since the last
    322 		 * check.
    323 		 */
    324 		if (np->n_direofoffset != 0 &&
    325 			ndp->dc_blkcookie == np->n_direofoffset) {
    326 			brelse(bp);
    327 			return (0);
    328 		}
    329 
    330 		/*
    331 		 * Find the entry we were looking for in the block.
    332 		 */
    333 
    334 		en = ndp->dc_entry;
    335 
    336 		pdp = dp = (struct dirent *)bp->b_data;
    337 		edp = bp->b_data + bp->b_bcount;
    338 		enn = 0;
    339 		while (enn < en && (caddr_t)dp < edp) {
    340 			pdp = dp;
    341 			dp = (struct dirent *)((caddr_t)dp + dp->d_reclen);
    342 			enn++;
    343 		}
    344 
    345 		/*
    346 		 * If the entry number was bigger than the number of
    347 		 * entries in the block, or the cookie of the previous
    348 		 * entry doesn't match, the directory cache is
    349 		 * stale. Flush it and try again (i.e. go to
    350 		 * the server).
    351 		 */
    352 		if ((caddr_t)dp >= edp || (caddr_t)dp + dp->d_reclen > edp ||
    353 		    (en > 0 && NFS_GETCOOKIE(pdp) != ndp->dc_cookie)) {
    354 #ifdef DEBUG
    355 		    	printf("invalid cache: %p %p %p off %lx %lx\n",
    356 				pdp, dp, edp,
    357 				(unsigned long)uio->uio_offset,
    358 				(unsigned long)NFS_GETCOOKIE(pdp));
    359 #endif
    360 			brelse(bp);
    361 			nfs_invaldircache(vp, 0);
    362 			nfs_vinvalbuf(vp, 0, cred, p, 0);
    363 			goto diragain;
    364 		}
    365 
    366 		on = (caddr_t)dp - bp->b_data;
    367 
    368 		/*
    369 		 * Cache all entries that may be exported to the
    370 		 * user, as they may be thrown back at us. The
    371 		 * NFSBIO_CACHECOOKIES flag indicates that all
    372 		 * entries are being 'exported', so cache them all.
    373 		 */
    374 
    375 		if (en == 0 && pdp == dp) {
    376 			dp = (struct dirent *)
    377 			    ((caddr_t)dp + dp->d_reclen);
    378 			enn++;
    379 		}
    380 
    381 		if (uio->uio_resid < (bp->b_bcount - on)) {
    382 			n = uio->uio_resid;
    383 			enough = 1;
    384 		} else
    385 			n = bp->b_bcount - on;
    386 
    387 		ep = bp->b_data + on + n;
    388 
    389 		/*
    390 		 * Find last complete entry to copy, caching entries
    391 		 * (if requested) as we go.
    392 		 */
    393 
    394 		while ((caddr_t)dp < ep && (caddr_t)dp + dp->d_reclen <= ep) {
    395 			if (cflag & NFSBIO_CACHECOOKIES) {
    396 				nndp = nfs_enterdircache(vp, NFS_GETCOOKIE(pdp),
    397 				    ndp->dc_blkcookie, enn, bp->b_lblkno);
    398 				if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
    399 					NFS_STASHCOOKIE32(pdp,
    400 					    nndp->dc_cookie32);
    401 				}
    402 			}
    403 			pdp = dp;
    404 			dp = (struct dirent *)((caddr_t)dp + dp->d_reclen);
    405 			enn++;
    406 		}
    407 
    408 		/*
    409 		 * If the last requested entry was not the last in the
    410 		 * buffer (happens if NFS_DIRFRAGSIZ < NFS_DIRBLKSIZ),
    411 		 * cache the cookie of the last requested one, and
    412 		 * set of the offset to it.
    413 		 */
    414 
    415 		if ((on + n) < bp->b_bcount) {
    416 			curoff = NFS_GETCOOKIE(pdp);
    417 			nndp = nfs_enterdircache(vp, curoff, ndp->dc_blkcookie,
    418 			    enn, bp->b_lblkno);
    419 			if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
    420 				NFS_STASHCOOKIE32(pdp, nndp->dc_cookie32);
    421 				curoff = nndp->dc_cookie32;
    422 			}
    423 		} else
    424 			curoff = bp->b_dcookie;
    425 
    426 		/*
    427 		 * Always cache the entry for the next block,
    428 		 * so that readaheads can use it.
    429 		 */
    430 		nndp = nfs_enterdircache(vp, bp->b_dcookie, bp->b_dcookie, 0,0);
    431 		if (nmp->nm_flag & NFSMNT_XLATECOOKIE) {
    432 			if (curoff == bp->b_dcookie) {
    433 				NFS_STASHCOOKIE32(pdp, nndp->dc_cookie32);
    434 				curoff = nndp->dc_cookie32;
    435 			}
    436 		}
    437 
    438 		n = ((caddr_t)pdp + pdp->d_reclen) - (bp->b_data + on);
    439 
    440 		/*
    441 		 * If not eof and read aheads are enabled, start one.
    442 		 * (You need the current block first, so that you have the
    443 		 *  directory offset cookie of the next block.)
    444 		 */
    445 		if (nfs_numasync > 0 && nmp->nm_readahead > 0 &&
    446 		    np->n_direofoffset == 0 && !(np->n_flag & NQNFSNONCACHE)) {
    447 			rabp = nfs_getcacheblk(vp, nndp->dc_blkno,
    448 						NFS_DIRBLKSIZ, p);
    449 			if (rabp) {
    450 			    if ((rabp->b_flags & (B_DONE | B_DELWRI)) == 0) {
    451 				rabp->b_dcookie = nndp->dc_cookie;
    452 				rabp->b_flags |= (B_READ | B_ASYNC);
    453 				if (nfs_asyncio(rabp)) {
    454 				    rabp->b_flags |= B_INVAL;
    455 				    brelse(rabp);
    456 				}
    457 			    } else
    458 				brelse(rabp);
    459 			}
    460 		}
    461 		got_buf = 1;
    462 		break;
    463 	    default:
    464 		printf(" nfsbioread: type %x unexpected\n",vp->v_type);
    465 		break;
    466 	    }
    467 
    468 	    if (n > 0) {
    469 		if (!baddr)
    470 			baddr = bp->b_data;
    471 		error = uiomove(baddr + on, (int)n, uio);
    472 	    }
    473 	    switch (vp->v_type) {
    474 	    case VREG:
    475 		break;
    476 	    case VLNK:
    477 		n = 0;
    478 		break;
    479 	    case VDIR:
    480 		if (np->n_flag & NQNFSNONCACHE)
    481 			bp->b_flags |= B_INVAL;
    482 		uio->uio_offset = curoff;
    483 		if (enough)
    484 			n = 0;
    485 		break;
    486 	    default:
    487 		printf(" nfsbioread: type %x unexpected\n",vp->v_type);
    488 	    }
    489 	    if (got_buf)
    490 		brelse(bp);
    491 	} while (error == 0 && uio->uio_resid > 0 && n > 0);
    492 	return (error);
    493 }
    494 
    495 /*
    496  * Vnode op for write using bio
    497  */
    498 int
    499 nfs_write(v)
    500 	void *v;
    501 {
    502 	struct vop_write_args /* {
    503 		struct vnode *a_vp;
    504 		struct uio *a_uio;
    505 		int  a_ioflag;
    506 		struct ucred *a_cred;
    507 	} */ *ap = v;
    508 	struct uio *uio = ap->a_uio;
    509 	struct proc *p = uio->uio_procp;
    510 	struct vnode *vp = ap->a_vp;
    511 	struct nfsnode *np = VTONFS(vp);
    512 	struct ucred *cred = ap->a_cred;
    513 	int ioflag = ap->a_ioflag;
    514 	struct vattr vattr;
    515 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
    516 	int error = 0, iomode, must_commit;
    517 	int rv;
    518 
    519 #ifdef DIAGNOSTIC
    520 	if (uio->uio_rw != UIO_WRITE)
    521 		panic("nfs_write mode");
    522 	if (uio->uio_segflg == UIO_USERSPACE &&
    523 	    uio->uio_procp != curproc->l_proc)
    524 		panic("nfs_write proc");
    525 #endif
    526 	if (vp->v_type != VREG)
    527 		return (EIO);
    528 	if (np->n_flag & NWRITEERR) {
    529 		np->n_flag &= ~NWRITEERR;
    530 		return (np->n_error);
    531 	}
    532 #ifndef NFS_V2_ONLY
    533 	if ((nmp->nm_flag & NFSMNT_NFSV3) &&
    534 	    !(nmp->nm_iflag & NFSMNT_GOTFSINFO))
    535 		(void)nfs_fsinfo(nmp, vp, cred, p);
    536 #endif
    537 	if (ioflag & (IO_APPEND | IO_SYNC)) {
    538 		if (np->n_flag & NMODIFIED) {
    539 			np->n_attrstamp = 0;
    540 			error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
    541 			if (error)
    542 				return (error);
    543 		}
    544 		if (ioflag & IO_APPEND) {
    545 			np->n_attrstamp = 0;
    546 			error = VOP_GETATTR(vp, &vattr, cred, p);
    547 			if (error)
    548 				return (error);
    549 			uio->uio_offset = np->n_size;
    550 		}
    551 	}
    552 	if (uio->uio_offset < 0)
    553 		return (EINVAL);
    554 	if ((uio->uio_offset + uio->uio_resid) > nmp->nm_maxfilesize)
    555 		return (EFBIG);
    556 	if (uio->uio_resid == 0)
    557 		return (0);
    558 	/*
    559 	 * Maybe this should be above the vnode op call, but so long as
    560 	 * file servers have no limits, i don't think it matters
    561 	 */
    562 	if (p && uio->uio_offset + uio->uio_resid >
    563 	      p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
    564 		psignal(p, SIGXFSZ);
    565 		return (EFBIG);
    566 	}
    567 
    568 	/*
    569 	 * update the cached write creds for this node.
    570 	 */
    571 
    572 	if (np->n_wcred) {
    573 		crfree(np->n_wcred);
    574 	}
    575 	np->n_wcred = cred;
    576 	crhold(cred);
    577 
    578 	if ((np->n_flag & NQNFSNONCACHE) && uio->uio_iovcnt == 1) {
    579 		iomode = NFSV3WRITE_FILESYNC;
    580 		error = nfs_writerpc(vp, uio, &iomode, &must_commit);
    581 		if (must_commit)
    582 			nfs_clearcommit(vp->v_mount);
    583 		return (error);
    584 	}
    585 
    586 	do {
    587 		void *win;
    588 		voff_t oldoff = uio->uio_offset;
    589 		vsize_t bytelen = uio->uio_resid;
    590 
    591 #ifndef NFS_V2_ONLY
    592 		/*
    593 		 * Check for a valid write lease.
    594 		 */
    595 		if ((nmp->nm_flag & NFSMNT_NQNFS) &&
    596 		    NQNFS_CKINVALID(vp, np, ND_WRITE)) {
    597 			do {
    598 				error = nqnfs_getlease(vp, ND_WRITE, cred, p);
    599 			} while (error == NQNFS_EXPIRED);
    600 			if (error)
    601 				return (error);
    602 			if (np->n_lrev != np->n_brev ||
    603 			    (np->n_flag & NQNFSNONCACHE)) {
    604 				error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
    605 				if (error)
    606 					return (error);
    607 				np->n_brev = np->n_lrev;
    608 			}
    609 		}
    610 #endif
    611 		nfsstats.biocache_writes++;
    612 
    613 		np->n_flag |= NMODIFIED;
    614 		if (np->n_size < uio->uio_offset + bytelen) {
    615 			np->n_size = uio->uio_offset + bytelen;
    616 			uvm_vnp_setsize(vp, np->n_size);
    617 		}
    618 		win = ubc_alloc(&vp->v_uvm.u_obj, uio->uio_offset, &bytelen,
    619 				UBC_WRITE);
    620 		error = uiomove(win, bytelen, uio);
    621 		ubc_release(win, 0);
    622 		rv = 1;
    623 		if ((np->n_flag & NQNFSNONCACHE) || (ioflag & IO_SYNC)) {
    624 			simple_lock(&vp->v_uvm.u_obj.vmobjlock);
    625 			rv = vp->v_uvm.u_obj.pgops->pgo_flush(
    626 			    &vp->v_uvm.u_obj,
    627 			    oldoff & ~(nmp->nm_wsize - 1),
    628 			    uio->uio_offset & ~(nmp->nm_wsize - 1),
    629 			    PGO_CLEANIT|PGO_SYNCIO);
    630 			simple_unlock(&vp->v_uvm.u_obj.vmobjlock);
    631 		} else if ((oldoff & ~(nmp->nm_wsize - 1)) !=
    632 		    (uio->uio_offset & ~(nmp->nm_wsize - 1))) {
    633 			simple_lock(&vp->v_uvm.u_obj.vmobjlock);
    634 			rv = vp->v_uvm.u_obj.pgops->pgo_flush(
    635 			    &vp->v_uvm.u_obj,
    636 			    oldoff & ~(nmp->nm_wsize - 1),
    637 			    uio->uio_offset & ~(nmp->nm_wsize - 1),
    638 			    PGO_CLEANIT|PGO_WEAK);
    639 			simple_unlock(&vp->v_uvm.u_obj.vmobjlock);
    640 		}
    641 		if (!rv) {
    642 			error = EIO;
    643 		}
    644 		if (error) {
    645 			break;
    646 		}
    647 	} while (uio->uio_resid > 0);
    648 	return error;
    649 }
    650 
    651 /*
    652  * Get an nfs cache block.
    653  * Allocate a new one if the block isn't currently in the cache
    654  * and return the block marked busy. If the calling process is
    655  * interrupted by a signal for an interruptible mount point, return
    656  * NULL.
    657  */
    658 struct buf *
    659 nfs_getcacheblk(vp, bn, size, p)
    660 	struct vnode *vp;
    661 	daddr_t bn;
    662 	int size;
    663 	struct proc *p;
    664 {
    665 	struct buf *bp;
    666 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
    667 
    668 	if (nmp->nm_flag & NFSMNT_INT) {
    669 		bp = getblk(vp, bn, size, PCATCH, 0);
    670 		while (bp == NULL) {
    671 			if (nfs_sigintr(nmp, NULL, p))
    672 				return (NULL);
    673 			bp = getblk(vp, bn, size, 0, 2 * hz);
    674 		}
    675 	} else
    676 		bp = getblk(vp, bn, size, 0, 0);
    677 	return (bp);
    678 }
    679 
    680 /*
    681  * Flush and invalidate all dirty buffers. If another process is already
    682  * doing the flush, just wait for completion.
    683  */
    684 int
    685 nfs_vinvalbuf(vp, flags, cred, p, intrflg)
    686 	struct vnode *vp;
    687 	int flags;
    688 	struct ucred *cred;
    689 	struct proc *p;
    690 	int intrflg;
    691 {
    692 	struct nfsnode *np = VTONFS(vp);
    693 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
    694 	int error = 0, slpflag, slptimeo;
    695 
    696 	if ((nmp->nm_flag & NFSMNT_INT) == 0)
    697 		intrflg = 0;
    698 	if (intrflg) {
    699 		slpflag = PCATCH;
    700 		slptimeo = 2 * hz;
    701 	} else {
    702 		slpflag = 0;
    703 		slptimeo = 0;
    704 	}
    705 	/*
    706 	 * First wait for any other process doing a flush to complete.
    707 	 */
    708 	while (np->n_flag & NFLUSHINPROG) {
    709 		np->n_flag |= NFLUSHWANT;
    710 		error = tsleep((caddr_t)&np->n_flag, PRIBIO + 2, "nfsvinval",
    711 			slptimeo);
    712 		if (error && intrflg && nfs_sigintr(nmp, NULL, p))
    713 			return (EINTR);
    714 	}
    715 
    716 	/*
    717 	 * Now, flush as required.
    718 	 */
    719 	np->n_flag |= NFLUSHINPROG;
    720 	error = vinvalbuf(vp, flags, cred, p, slpflag, 0);
    721 	while (error) {
    722 		if (intrflg && nfs_sigintr(nmp, NULL, p)) {
    723 			np->n_flag &= ~NFLUSHINPROG;
    724 			if (np->n_flag & NFLUSHWANT) {
    725 				np->n_flag &= ~NFLUSHWANT;
    726 				wakeup((caddr_t)&np->n_flag);
    727 			}
    728 			return (EINTR);
    729 		}
    730 		error = vinvalbuf(vp, flags, cred, p, 0, slptimeo);
    731 	}
    732 	np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
    733 	if (np->n_flag & NFLUSHWANT) {
    734 		np->n_flag &= ~NFLUSHWANT;
    735 		wakeup((caddr_t)&np->n_flag);
    736 	}
    737 	return (0);
    738 }
    739 
    740 /*
    741  * Initiate asynchronous I/O. Return an error if no nfsiods are available.
    742  * This is mainly to avoid queueing async I/O requests when the nfsiods
    743  * are all hung on a dead server.
    744  */
    745 int
    746 nfs_asyncio(bp)
    747 	struct buf *bp;
    748 {
    749 	int i;
    750 	struct nfsmount *nmp;
    751 	int gotiod, slpflag = 0, slptimeo = 0, error;
    752 
    753 	if (nfs_numasync == 0)
    754 		return (EIO);
    755 
    756 
    757 	nmp = VFSTONFS(bp->b_vp->v_mount);
    758 again:
    759 	if (nmp->nm_flag & NFSMNT_INT)
    760 		slpflag = PCATCH;
    761 	gotiod = FALSE;
    762 
    763 	/*
    764 	 * Find a free iod to process this request.
    765 	 */
    766 
    767 	for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
    768 		if (nfs_iodwant[i]) {
    769 			/*
    770 			 * Found one, so wake it up and tell it which
    771 			 * mount to process.
    772 			 */
    773 			nfs_iodwant[i] = NULL;
    774 			nfs_iodmount[i] = nmp;
    775 			nmp->nm_bufqiods++;
    776 			wakeup((caddr_t)&nfs_iodwant[i]);
    777 			gotiod = TRUE;
    778 			break;
    779 		}
    780 	/*
    781 	 * If none are free, we may already have an iod working on this mount
    782 	 * point.  If so, it will process our request.
    783 	 */
    784 	if (!gotiod && nmp->nm_bufqiods > 0)
    785 		gotiod = TRUE;
    786 
    787 	/*
    788 	 * If we have an iod which can process the request, then queue
    789 	 * the buffer.
    790 	 */
    791 	if (gotiod) {
    792 		/*
    793 		 * Ensure that the queue never grows too large.
    794 		 */
    795 		while (nmp->nm_bufqlen >= 2*nfs_numasync) {
    796 			nmp->nm_bufqwant = TRUE;
    797 			error = tsleep(&nmp->nm_bufq, slpflag | PRIBIO,
    798 				"nfsaio", slptimeo);
    799 			if (error) {
    800 				if (nfs_sigintr(nmp, NULL, bp->b_proc))
    801 					return (EINTR);
    802 				if (slpflag == PCATCH) {
    803 					slpflag = 0;
    804 					slptimeo = 2 * hz;
    805 				}
    806 			}
    807 			/*
    808 			 * We might have lost our iod while sleeping,
    809 			 * so check and loop if nescessary.
    810 			 */
    811 			if (nmp->nm_bufqiods == 0)
    812 				goto again;
    813 		}
    814 		TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist);
    815 		nmp->nm_bufqlen++;
    816 		return (0);
    817 	    }
    818 
    819 	/*
    820 	 * All the iods are busy on other mounts, so return EIO to
    821 	 * force the caller to process the i/o synchronously.
    822 	 */
    823 	return (EIO);
    824 }
    825 
    826 /*
    827  * Do an I/O operation to/from a cache block. This may be called
    828  * synchronously or from an nfsiod.
    829  */
    830 int
    831 nfs_doio(bp, p)
    832 	struct buf *bp;
    833 	struct proc *p;
    834 {
    835 	struct uio *uiop;
    836 	struct vnode *vp;
    837 	struct nfsnode *np;
    838 	struct nfsmount *nmp;
    839 	int error = 0, diff, len, iomode, must_commit = 0;
    840 	struct uio uio;
    841 	struct iovec io;
    842 
    843 	vp = bp->b_vp;
    844 	np = VTONFS(vp);
    845 	nmp = VFSTONFS(vp->v_mount);
    846 	uiop = &uio;
    847 	uiop->uio_iov = &io;
    848 	uiop->uio_iovcnt = 1;
    849 	uiop->uio_segflg = UIO_SYSSPACE;
    850 	uiop->uio_procp = p;
    851 
    852 	/*
    853 	 * Historically, paging was done with physio, but no more...
    854 	 */
    855 	if (bp->b_flags & B_PHYS) {
    856 	    /*
    857 	     * ...though reading /dev/drum still gets us here.
    858 	     */
    859 	    io.iov_len = uiop->uio_resid = bp->b_bcount;
    860 	    /* mapping was done by vmapbuf() */
    861 	    io.iov_base = bp->b_data;
    862 	    uiop->uio_offset = ((off_t)bp->b_blkno) << DEV_BSHIFT;
    863 	    if (bp->b_flags & B_READ) {
    864 		uiop->uio_rw = UIO_READ;
    865 		nfsstats.read_physios++;
    866 		error = nfs_readrpc(vp, uiop);
    867 	    } else {
    868 		iomode = NFSV3WRITE_DATASYNC;
    869 		uiop->uio_rw = UIO_WRITE;
    870 		nfsstats.write_physios++;
    871 		error = nfs_writerpc(vp, uiop, &iomode, &must_commit);
    872 	    }
    873 	    if (error) {
    874 		bp->b_flags |= B_ERROR;
    875 		bp->b_error = error;
    876 	    }
    877 	} else if (bp->b_flags & B_READ) {
    878 	    io.iov_len = uiop->uio_resid = bp->b_bcount;
    879 	    io.iov_base = bp->b_data;
    880 	    uiop->uio_rw = UIO_READ;
    881 	    switch (vp->v_type) {
    882 	    case VREG:
    883 		uiop->uio_offset = ((off_t)bp->b_blkno) << DEV_BSHIFT;
    884 		nfsstats.read_bios++;
    885 		error = nfs_readrpc(vp, uiop);
    886 		if (!error && uiop->uio_resid) {
    887 
    888 			/*
    889 			 * If len > 0, there is a hole in the file and
    890 			 * no writes after the hole have been pushed to
    891 			 * the server yet.
    892 			 * Just zero fill the rest of the valid area.
    893 			 */
    894 
    895 			diff = bp->b_bcount - uiop->uio_resid;
    896 			len = np->n_size - ((((off_t)bp->b_blkno) << DEV_BSHIFT)
    897 				+ diff);
    898 			if (len > 0) {
    899 				len = MIN(len, uiop->uio_resid);
    900 				memset((char *)bp->b_data + diff, 0, len);
    901 			}
    902 		}
    903 		if (p && (vp->v_flag & VTEXT) &&
    904 			(((nmp->nm_flag & NFSMNT_NQNFS) &&
    905 			  NQNFS_CKINVALID(vp, np, ND_READ) &&
    906 			  np->n_lrev != np->n_brev) ||
    907 			 (!(nmp->nm_flag & NFSMNT_NQNFS) &&
    908 			  np->n_mtime != np->n_vattr->va_mtime.tv_sec))) {
    909 			uprintf("Process killed due to "
    910 				"text file modification\n");
    911 			psignal(p, SIGKILL);
    912 #if 0 /* XXX NJWLWP */
    913 			p->p_holdcnt++;
    914 #endif
    915 		}
    916 		break;
    917 	    case VLNK:
    918 		uiop->uio_offset = (off_t)0;
    919 		nfsstats.readlink_bios++;
    920 		error = nfs_readlinkrpc(vp, uiop, curproc->l_proc->p_ucred);
    921 		break;
    922 	    case VDIR:
    923 		nfsstats.readdir_bios++;
    924 		uiop->uio_offset = bp->b_dcookie;
    925 		if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
    926 			error = nfs_readdirplusrpc(vp, uiop, curproc->l_proc->p_ucred);
    927 			if (error == NFSERR_NOTSUPP)
    928 				nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
    929 		}
    930 		if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
    931 			error = nfs_readdirrpc(vp, uiop, curproc->l_proc->p_ucred);
    932 		if (!error) {
    933 			bp->b_dcookie = uiop->uio_offset;
    934 		}
    935 		break;
    936 	    default:
    937 		printf("nfs_doio:  type %x unexpected\n",vp->v_type);
    938 		break;
    939 	    }
    940 	    if (error) {
    941 		bp->b_flags |= B_ERROR;
    942 		bp->b_error = error;
    943 	    }
    944 	} else {
    945 	    /*
    946 	     * If B_NEEDCOMMIT is set, a commit rpc may do the trick. If not
    947 	     * an actual write will have to be scheduled.
    948 	     */
    949 
    950 	    io.iov_base = bp->b_data;
    951 	    io.iov_len = uiop->uio_resid = bp->b_bcount;
    952 	    uiop->uio_offset = (((off_t)bp->b_blkno) << DEV_BSHIFT);
    953 	    uiop->uio_rw = UIO_WRITE;
    954 	    nfsstats.write_bios++;
    955 	    iomode = NFSV3WRITE_UNSTABLE;
    956 	    error = nfs_writerpc(vp, uiop, &iomode, &must_commit);
    957 	}
    958 	bp->b_resid = uiop->uio_resid;
    959 	if (must_commit)
    960 		nfs_clearcommit(vp->v_mount);
    961 	biodone(bp);
    962 	return (error);
    963 }
    964 
    965 /*
    966  * Vnode op for VM getpages.
    967  */
    968 int
    969 nfs_getpages(v)
    970 	void *v;
    971 {
    972 	struct vop_getpages_args /* {
    973 		struct vnode *a_vp;
    974 		voff_t a_offset;
    975 		vm_page_t *a_m;
    976 		int *a_count;
    977 		int a_centeridx;
    978 		vm_prot_t a_access_type;
    979 		int a_advice;
    980 		int a_flags;
    981 	} */ *ap = v;
    982 
    983 	off_t eof, offset, origoffset, startoffset, endoffset;
    984 	int s, i, error, npages, orignpages, npgs, ridx, pidx, pcount;
    985 	vaddr_t kva;
    986 	struct buf *bp, *mbp;
    987 	struct vnode *vp = ap->a_vp;
    988 	struct nfsnode *np = VTONFS(vp);
    989 	struct uvm_object *uobj = &vp->v_uvm.u_obj;
    990 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
    991 	size_t bytes, iobytes, tailbytes, totalbytes, skipbytes;
    992 	int flags = ap->a_flags;
    993 	int bsize;
    994 	struct vm_page *pgs[16];			/* XXXUBC 16 */
    995 	boolean_t v3 = NFS_ISV3(vp);
    996 	boolean_t async = (flags & PGO_SYNCIO) == 0;
    997 	boolean_t write = (ap->a_access_type & VM_PROT_WRITE) != 0;
    998 
    999 	UVMHIST_FUNC("nfs_getpages"); UVMHIST_CALLED(ubchist);
   1000 	UVMHIST_LOG(ubchist, "vp %p off 0x%x count %d", vp, (int)ap->a_offset,
   1001 		    *ap->a_count,0);
   1002 
   1003 #ifdef DIAGNOSTIC
   1004 	if (ap->a_centeridx < 0 || ap->a_centeridx >= *ap->a_count) {
   1005 		panic("nfs_getpages: centeridx %d out of range",
   1006 		      ap->a_centeridx);
   1007 	}
   1008 #endif
   1009 
   1010 	error = 0;
   1011 	origoffset = ap->a_offset;
   1012 	eof = vp->v_uvm.u_size;
   1013 	if (origoffset >= eof) {
   1014 		if ((flags & PGO_LOCKED) == 0) {
   1015 			simple_unlock(&uobj->vmobjlock);
   1016 		}
   1017 		UVMHIST_LOG(ubchist, "off 0x%x past EOF 0x%x",
   1018 			    (int)origoffset, (int)eof,0,0);
   1019 		return EINVAL;
   1020 	}
   1021 
   1022 	if (flags & PGO_LOCKED) {
   1023 		uvn_findpages(uobj, origoffset, ap->a_count, ap->a_m,
   1024 			      UFP_NOWAIT|UFP_NOALLOC);
   1025 		return 0;
   1026 	}
   1027 
   1028 	/* vnode is VOP_LOCKed, uobj is locked */
   1029 
   1030 	bsize = nmp->nm_rsize;
   1031 	orignpages = MIN(*ap->a_count,
   1032 			 round_page(eof - origoffset) >> PAGE_SHIFT);
   1033 	npages = orignpages;
   1034 	startoffset = origoffset & ~(bsize - 1);
   1035 	endoffset = round_page((origoffset + (npages << PAGE_SHIFT)
   1036 				+ bsize - 1) & ~(bsize - 1));
   1037 	endoffset = MIN(endoffset, round_page(eof));
   1038 	ridx = (origoffset - startoffset) >> PAGE_SHIFT;
   1039 
   1040 	if (!async && !write) {
   1041 		int rapages = MAX(PAGE_SIZE, nmp->nm_rsize) >> PAGE_SHIFT;
   1042 
   1043 		(void) VOP_GETPAGES(vp, endoffset, NULL, &rapages, 0,
   1044 				    VM_PROT_READ, 0, 0);
   1045 		simple_lock(&uobj->vmobjlock);
   1046 	}
   1047 
   1048 	UVMHIST_LOG(ubchist, "npages %d offset 0x%x", npages,
   1049 		    (int)origoffset, 0,0);
   1050 	memset(pgs, 0, sizeof(pgs));
   1051 	uvn_findpages(uobj, origoffset, &npages, &pgs[ridx], UFP_ALL);
   1052 
   1053 	if (flags & PGO_OVERWRITE) {
   1054 		UVMHIST_LOG(ubchist, "PGO_OVERWRITE",0,0,0,0);
   1055 
   1056 		/* XXXUBC for now, zero the page if we allocated it */
   1057 		for (i = 0; i < npages; i++) {
   1058 			struct vm_page *pg = pgs[ridx + i];
   1059 
   1060 			if (pg->flags & PG_FAKE) {
   1061 				uvm_pagezero(pg);
   1062 				pg->flags &= ~(PG_FAKE);
   1063 			}
   1064 		}
   1065 		npages += ridx;
   1066 		if (v3) {
   1067 			simple_unlock(&uobj->vmobjlock);
   1068 			goto uncommit;
   1069 		}
   1070 		goto out;
   1071 	}
   1072 
   1073 	/*
   1074 	 * if the pages are already resident, just return them.
   1075 	 */
   1076 
   1077 	for (i = 0; i < npages; i++) {
   1078 		struct vm_page *pg = pgs[ridx + i];
   1079 
   1080 		if ((pg->flags & PG_FAKE) != 0 ||
   1081 		    ((ap->a_access_type & VM_PROT_WRITE) &&
   1082 		      (pg->flags & PG_RDONLY))) {
   1083 			break;
   1084 		}
   1085 	}
   1086 	if (i == npages) {
   1087 		UVMHIST_LOG(ubchist, "returning cached pages", 0,0,0,0);
   1088 		npages += ridx;
   1089 		goto out;
   1090 	}
   1091 
   1092 	/*
   1093 	 * the page wasn't resident and we're not overwriting,
   1094 	 * so we're going to have to do some i/o.
   1095 	 * find any additional pages needed to cover the expanded range.
   1096 	 */
   1097 
   1098 	if (startoffset != origoffset ||
   1099 	    startoffset + (npages << PAGE_SHIFT) != endoffset) {
   1100 
   1101 		/*
   1102 		 * XXXUBC we need to avoid deadlocks caused by locking
   1103 		 * additional pages at lower offsets than pages we
   1104 		 * already have locked.  for now, unlock them all and
   1105 		 * start over.
   1106 		 */
   1107 
   1108 		for (i = 0; i < npages; i++) {
   1109 			struct vm_page *pg = pgs[ridx + i];
   1110 
   1111 			if (pg->flags & PG_FAKE) {
   1112 				pg->flags |= PG_RELEASED;
   1113 			}
   1114 		}
   1115 		uvm_page_unbusy(&pgs[ridx], npages);
   1116 		memset(pgs, 0, sizeof(pgs));
   1117 
   1118 		UVMHIST_LOG(ubchist, "reset npages start 0x%x end 0x%x",
   1119 			    startoffset, endoffset, 0,0);
   1120 		npages = (endoffset - startoffset) >> PAGE_SHIFT;
   1121 		npgs = npages;
   1122 		uvn_findpages(uobj, startoffset, &npgs, pgs, UFP_ALL);
   1123 	}
   1124 	simple_unlock(&uobj->vmobjlock);
   1125 
   1126 	/*
   1127 	 * update the cached read creds for this node.
   1128 	 */
   1129 
   1130 	if (np->n_rcred) {
   1131 		crfree(np->n_rcred);
   1132 	}
   1133 	np->n_rcred = curproc->l_proc->p_ucred;
   1134 	crhold(np->n_rcred);
   1135 
   1136 	/*
   1137 	 * read the desired page(s).
   1138 	 */
   1139 
   1140 	totalbytes = npages << PAGE_SHIFT;
   1141 	bytes = MIN(totalbytes, vp->v_uvm.u_size - startoffset);
   1142 	tailbytes = totalbytes - bytes;
   1143 	skipbytes = 0;
   1144 
   1145 	kva = uvm_pagermapin(pgs, npages, UVMPAGER_MAPIN_WAITOK |
   1146 			     UVMPAGER_MAPIN_READ);
   1147 
   1148 	s = splbio();
   1149 	mbp = pool_get(&bufpool, PR_WAITOK);
   1150 	splx(s);
   1151 	mbp->b_bufsize = totalbytes;
   1152 	mbp->b_data = (void *)kva;
   1153 	mbp->b_resid = mbp->b_bcount = bytes;
   1154 	mbp->b_flags = B_BUSY|B_READ| (async ? B_CALL|B_ASYNC : 0);
   1155 	mbp->b_iodone = uvm_aio_biodone;
   1156 	mbp->b_vp = vp;
   1157 	mbp->b_proc = NULL;		/* XXXUBC */
   1158 	LIST_INIT(&mbp->b_dep);
   1159 
   1160 	/*
   1161 	 * if EOF is in the middle of the last page, zero the part past EOF.
   1162 	 */
   1163 
   1164 	if (tailbytes > 0 && (pgs[bytes >> PAGE_SHIFT]->flags & PG_FAKE)) {
   1165 		memset((char *)kva + bytes, 0, tailbytes);
   1166 	}
   1167 
   1168 	/*
   1169 	 * now loop over the pages, reading as needed.
   1170 	 */
   1171 
   1172 	bp = NULL;
   1173 	for (offset = startoffset;
   1174 	     bytes > 0;
   1175 	     offset += iobytes, bytes -= iobytes) {
   1176 
   1177 		/*
   1178 		 * skip pages which don't need to be read.
   1179 		 */
   1180 
   1181 		pidx = (offset - startoffset) >> PAGE_SHIFT;
   1182 		UVMHIST_LOG(ubchist, "pidx %d offset 0x%x startoffset 0x%x",
   1183 			    pidx, (int)offset, (int)startoffset,0);
   1184 		while ((pgs[pidx]->flags & PG_FAKE) == 0) {
   1185 			size_t b;
   1186 
   1187 			KASSERT((offset & (PAGE_SIZE - 1)) == 0);
   1188 			b = MIN(PAGE_SIZE, bytes);
   1189 			offset += b;
   1190 			bytes -= b;
   1191 			skipbytes += b;
   1192 			pidx++;
   1193 			UVMHIST_LOG(ubchist, "skipping, new offset 0x%x",
   1194 				    (int)offset, 0,0,0);
   1195 			if (bytes == 0) {
   1196 				goto loopdone;
   1197 			}
   1198 		}
   1199 
   1200 		/*
   1201 		 * see how many pages can be read with this i/o.
   1202 		 * reduce the i/o size if necessary.
   1203 		 */
   1204 
   1205 		iobytes = bytes;
   1206 		if (offset + iobytes > round_page(offset)) {
   1207 			pcount = 1;
   1208 			while (pidx + pcount < npages &&
   1209 			       pgs[pidx + pcount]->flags & PG_FAKE) {
   1210 				pcount++;
   1211 			}
   1212 			iobytes = MIN(iobytes, (pcount << PAGE_SHIFT) -
   1213 				      (offset - trunc_page(offset)));
   1214 		}
   1215 		iobytes = MIN(iobytes, nmp->nm_rsize);
   1216 
   1217 		/*
   1218 		 * allocate a sub-buf for this piece of the i/o
   1219 		 * (or just use mbp if there's only 1 piece),
   1220 		 * and start it going.
   1221 		 */
   1222 
   1223 		if (offset == startoffset && iobytes == bytes) {
   1224 			bp = mbp;
   1225 		} else {
   1226 			s = splbio();
   1227 			bp = pool_get(&bufpool, PR_WAITOK);
   1228 			splx(s);
   1229 			bp->b_data = (char *)kva + offset - startoffset;
   1230 			bp->b_resid = bp->b_bcount = iobytes;
   1231 			bp->b_flags = B_BUSY|B_READ|B_CALL|B_ASYNC;
   1232 			bp->b_iodone = uvm_aio_biodone1;
   1233 			bp->b_vp = vp;
   1234 			bp->b_proc = NULL;	/* XXXUBC */
   1235 			LIST_INIT(&bp->b_dep);
   1236 		}
   1237 		bp->b_private = mbp;
   1238 		bp->b_lblkno = bp->b_blkno = offset >> DEV_BSHIFT;
   1239 
   1240 		UVMHIST_LOG(ubchist, "bp %p offset 0x%x bcount 0x%x blkno 0x%x",
   1241 			    bp, offset, iobytes, bp->b_blkno);
   1242 
   1243 		VOP_STRATEGY(bp);
   1244 	}
   1245 
   1246 loopdone:
   1247 	if (skipbytes) {
   1248 		s = splbio();
   1249 		mbp->b_resid -= skipbytes;
   1250 		if (mbp->b_resid == 0) {
   1251 			biodone(mbp);
   1252 		}
   1253 		splx(s);
   1254 	}
   1255 	if (async) {
   1256 		UVMHIST_LOG(ubchist, "returning PEND",0,0,0,0);
   1257 		return EINPROGRESS;
   1258 	}
   1259 	if (bp != NULL) {
   1260 		error = biowait(mbp);
   1261 	}
   1262 	s = splbio();
   1263 	pool_put(&bufpool, mbp);
   1264 	splx(s);
   1265 	uvm_pagermapout(kva, npages);
   1266 
   1267 	if (write && v3) {
   1268 uncommit:
   1269 		lockmgr(&np->n_commitlock, LK_EXCLUSIVE, NULL);
   1270 		nfs_del_committed_range(vp, origoffset, npages);
   1271 		nfs_del_tobecommitted_range(vp, origoffset, npages);
   1272 		simple_lock(&uobj->vmobjlock);
   1273 		for (i = 0; i < npages; i++) {
   1274 			if (pgs[i] == NULL) {
   1275 				continue;
   1276 			}
   1277 			pgs[i]->flags &= ~(PG_NEEDCOMMIT|PG_RDONLY);
   1278 		}
   1279 		simple_unlock(&uobj->vmobjlock);
   1280 		lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
   1281 	}
   1282 
   1283 	simple_lock(&uobj->vmobjlock);
   1284 
   1285 out:
   1286 	if (error) {
   1287 		uvm_lock_pageq();
   1288 		for (i = 0; i < npages; i++) {
   1289 			if (pgs[i] == NULL) {
   1290 				continue;
   1291 			}
   1292 			UVMHIST_LOG(ubchist, "examining pg %p flags 0x%x",
   1293 				    pgs[i], pgs[i]->flags, 0,0);
   1294 			if (pgs[i]->flags & PG_WANTED) {
   1295 				wakeup(pgs[i]);
   1296 			}
   1297 			if (pgs[i]->flags & PG_RELEASED) {
   1298 				uvm_unlock_pageq();
   1299 				(uobj->pgops->pgo_releasepg)(pgs[i], NULL);
   1300 				uvm_lock_pageq();
   1301 				continue;
   1302 			}
   1303 			if (pgs[i]->flags & PG_FAKE) {
   1304 				uvm_pagefree(pgs[i]);
   1305 				continue;
   1306 			}
   1307 			uvm_pageactivate(pgs[i]);
   1308 			pgs[i]->flags &= ~(PG_WANTED|PG_BUSY);
   1309 			UVM_PAGE_OWN(pgs[i], NULL);
   1310 		}
   1311 		uvm_unlock_pageq();
   1312 		simple_unlock(&uobj->vmobjlock);
   1313 		UVMHIST_LOG(ubchist, "returning error %d", error,0,0,0);
   1314 		return error;
   1315 	}
   1316 
   1317 	UVMHIST_LOG(ubchist, "ridx %d count %d", ridx, npages, 0,0);
   1318 	uvm_lock_pageq();
   1319 	for (i = 0; i < npages; i++) {
   1320 		if (pgs[i] == NULL) {
   1321 			continue;
   1322 		}
   1323 		UVMHIST_LOG(ubchist, "examining pg %p flags 0x%x",
   1324 			    pgs[i], pgs[i]->flags, 0,0);
   1325 		if (pgs[i]->flags & PG_FAKE) {
   1326 			UVMHIST_LOG(ubchist, "unfaking pg %p offset 0x%x",
   1327 				    pgs[i], (int)pgs[i]->offset,0,0);
   1328 			pgs[i]->flags &= ~(PG_FAKE);
   1329 			pmap_clear_modify(pgs[i]);
   1330 			pmap_clear_reference(pgs[i]);
   1331 		}
   1332 		if (i < ridx || i >= ridx + orignpages || async) {
   1333 			UVMHIST_LOG(ubchist, "unbusy pg %p offset 0x%x",
   1334 				    pgs[i], (int)pgs[i]->offset,0,0);
   1335 			if (pgs[i]->flags & PG_WANTED) {
   1336 				wakeup(pgs[i]);
   1337 			}
   1338 			if (pgs[i]->flags & PG_RELEASED) {
   1339 				uvm_unlock_pageq();
   1340 				(uobj->pgops->pgo_releasepg)(pgs[i], NULL);
   1341 				uvm_lock_pageq();
   1342 				continue;
   1343 			}
   1344 			uvm_pageactivate(pgs[i]);
   1345 			pgs[i]->flags &= ~(PG_WANTED|PG_BUSY);
   1346 			UVM_PAGE_OWN(pgs[i], NULL);
   1347 		}
   1348 	}
   1349 	uvm_unlock_pageq();
   1350 	simple_unlock(&uobj->vmobjlock);
   1351 	if (ap->a_m != NULL) {
   1352 		memcpy(ap->a_m, &pgs[ridx],
   1353 		       *ap->a_count * sizeof(struct vm_page *));
   1354 	}
   1355 	return 0;
   1356 }
   1357 
   1358 /*
   1359  * Vnode op for VM putpages.
   1360  */
   1361 int
   1362 nfs_putpages(v)
   1363 	void *v;
   1364 {
   1365 	struct vop_putpages_args /* {
   1366 		struct vnode *a_vp;
   1367 		struct vm_page **a_m;
   1368 		int a_count;
   1369 		int a_flags;
   1370 		int *a_rtvals;
   1371 	} */ *ap = v;
   1372 
   1373 	struct vnode *vp = ap->a_vp;
   1374 	struct nfsnode *np = VTONFS(vp);
   1375 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   1376 	struct buf *bp, *mbp;
   1377 	struct vm_page **pgs = ap->a_m;
   1378 	int flags = ap->a_flags;
   1379 	int npages = ap->a_count;
   1380 	int s, error, i;
   1381 	size_t bytes, iobytes, skipbytes;
   1382 	vaddr_t kva;
   1383 	off_t offset, origoffset, commitoff;
   1384 	uint32_t commitbytes;
   1385 	boolean_t v3 = NFS_ISV3(vp);
   1386 	boolean_t async = (flags & PGO_SYNCIO) == 0;
   1387 	boolean_t weak = (flags & PGO_WEAK) && v3;
   1388 	UVMHIST_FUNC("nfs_putpages"); UVMHIST_CALLED(ubchist);
   1389 
   1390 	UVMHIST_LOG(ubchist, "vp %p pgp %p count %d",
   1391 		    vp, ap->a_m, ap->a_count,0);
   1392 
   1393 	simple_unlock(&vp->v_uvm.u_obj.vmobjlock);
   1394 
   1395 	error = 0;
   1396 	origoffset = pgs[0]->offset;
   1397 	bytes = MIN(ap->a_count << PAGE_SHIFT, vp->v_uvm.u_size - origoffset);
   1398 	skipbytes = 0;
   1399 
   1400 	/*
   1401 	 * if the range has been committed already, mark the pages thus.
   1402 	 * if the range just needs to be committed, we're done
   1403 	 * if it's a weak putpage, otherwise commit the range.
   1404 	 */
   1405 
   1406 	if (v3) {
   1407 		lockmgr(&np->n_commitlock, LK_EXCLUSIVE, NULL);
   1408 		if (nfs_in_committed_range(vp, origoffset, bytes)) {
   1409 			goto committed;
   1410 		}
   1411 		if (nfs_in_tobecommitted_range(vp, origoffset, bytes)) {
   1412 			if (weak) {
   1413 				lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
   1414 				return 0;
   1415 			} else {
   1416 				commitoff = np->n_pushlo;
   1417 				commitbytes = (uint32_t)(np->n_pushhi -
   1418 							 np->n_pushlo);
   1419 				goto commit;
   1420 			}
   1421 		}
   1422 		lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
   1423 	}
   1424 
   1425 	/*
   1426 	 * otherwise write or commit all the pages.
   1427 	 */
   1428 
   1429 	kva = uvm_pagermapin(pgs, ap->a_count, UVMPAGER_MAPIN_WAITOK|
   1430 			     UVMPAGER_MAPIN_WRITE);
   1431 
   1432 	s = splbio();
   1433 	vp->v_numoutput += 2;
   1434 	mbp = pool_get(&bufpool, PR_WAITOK);
   1435 	UVMHIST_LOG(ubchist, "vp %p mbp %p num now %d bytes 0x%x",
   1436 		    vp, mbp, vp->v_numoutput, bytes);
   1437 	splx(s);
   1438 	mbp->b_bufsize = npages << PAGE_SHIFT;
   1439 	mbp->b_data = (void *)kva;
   1440 	mbp->b_resid = mbp->b_bcount = bytes;
   1441 	mbp->b_flags = B_BUSY|B_WRITE|B_AGE |
   1442 		(async ? B_CALL|B_ASYNC : 0) |
   1443 		(curproc == uvm.pagedaemon_proc ? B_PDAEMON : 0);
   1444 	mbp->b_iodone = uvm_aio_biodone;
   1445 	mbp->b_vp = vp;
   1446 	mbp->b_proc = NULL;		/* XXXUBC */
   1447 	LIST_INIT(&mbp->b_dep);
   1448 
   1449 	for (offset = origoffset;
   1450 	     bytes > 0;
   1451 	     offset += iobytes, bytes -= iobytes) {
   1452 		iobytes = MIN(nmp->nm_wsize, bytes);
   1453 
   1454 		/*
   1455 		 * skip writing any pages which only need a commit.
   1456 		 */
   1457 
   1458 		if ((pgs[(offset - origoffset) >> PAGE_SHIFT]->flags &
   1459 		     PG_NEEDCOMMIT) != 0) {
   1460 			KASSERT((offset & (PAGE_SIZE - 1)) == 0);
   1461 			iobytes = MIN(PAGE_SIZE, bytes);
   1462 			skipbytes += iobytes;
   1463 			continue;
   1464 		}
   1465 
   1466 		/* if it's really one i/o, don't make a second buf */
   1467 		if (offset == origoffset && iobytes == bytes) {
   1468 			bp = mbp;
   1469 		} else {
   1470 			s = splbio();
   1471 			vp->v_numoutput++;
   1472 			bp = pool_get(&bufpool, PR_WAITOK);
   1473 			UVMHIST_LOG(ubchist, "vp %p bp %p num now %d",
   1474 				    vp, bp, vp->v_numoutput, 0);
   1475 			splx(s);
   1476 			bp->b_data = (char *)kva + (offset - origoffset);
   1477 			bp->b_resid = bp->b_bcount = iobytes;
   1478 			bp->b_flags = B_BUSY|B_WRITE|B_CALL|B_ASYNC;
   1479 			bp->b_iodone = uvm_aio_biodone1;
   1480 			bp->b_vp = vp;
   1481 			bp->b_proc = NULL;	/* XXXUBC */
   1482 			LIST_INIT(&bp->b_dep);
   1483 		}
   1484 		bp->b_private = mbp;
   1485 		bp->b_lblkno = bp->b_blkno = (daddr_t)(offset >> DEV_BSHIFT);
   1486 		UVMHIST_LOG(ubchist, "bp %p numout %d",
   1487 			    bp, vp->v_numoutput,0,0);
   1488 		VOP_STRATEGY(bp);
   1489 	}
   1490 	if (skipbytes) {
   1491 		UVMHIST_LOG(ubchist, "skipbytes %d", bytes, 0,0,0);
   1492 		s = splbio();
   1493 		mbp->b_resid -= skipbytes;
   1494 		if (mbp->b_resid == 0) {
   1495 			biodone(mbp);
   1496 		}
   1497 		splx(s);
   1498 	}
   1499 	if (async) {
   1500 		return EINPROGRESS;
   1501 	}
   1502 	if (bp != NULL) {
   1503 		error = biowait(mbp);
   1504 	}
   1505 
   1506 	s = splbio();
   1507 	vwakeup(mbp);
   1508 	pool_put(&bufpool, mbp);
   1509 	splx(s);
   1510 
   1511 	uvm_pagermapout(kva, ap->a_count);
   1512 	if (error || !v3) {
   1513 		UVMHIST_LOG(ubchist, "returning error %d", error, 0,0,0);
   1514 		return error;
   1515 	}
   1516 
   1517 	/*
   1518 	 * for a weak put, mark the range as "to be committed"
   1519 	 * and mark the pages read-only so that we will be notified
   1520 	 * to remove the pages from the "to be committed" range
   1521 	 * if they are made dirty again.
   1522 	 * for a strong put, commit the pages and remove them from the
   1523 	 * "to be committed" range.  also, mark them as writable
   1524 	 * and not cleanable with just a commit.
   1525 	 */
   1526 
   1527 	lockmgr(&np->n_commitlock, LK_EXCLUSIVE, NULL);
   1528 	if (weak) {
   1529 		nfs_add_tobecommitted_range(vp, origoffset,
   1530 					    npages << PAGE_SHIFT);
   1531 		for (i = 0; i < npages; i++) {
   1532 			pgs[i]->flags |= PG_NEEDCOMMIT|PG_RDONLY;
   1533 		}
   1534 	} else {
   1535 		commitoff = origoffset;
   1536 		commitbytes = npages << PAGE_SHIFT;
   1537 commit:
   1538 		error = nfs_commit(vp, commitoff, commitbytes, curproc->l_proc);
   1539 		nfs_del_tobecommitted_range(vp, commitoff, commitbytes);
   1540 committed:
   1541 		for (i = 0; i < npages; i++) {
   1542 			pgs[i]->flags &= ~(PG_NEEDCOMMIT|PG_RDONLY);
   1543 		}
   1544 	}
   1545 	lockmgr(&np->n_commitlock, LK_RELEASE, NULL);
   1546 	return error;
   1547 }
   1548