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