Home | History | Annotate | Line # | Download | only in client
      1 /*	$NetBSD: nfs_clbio.c,v 1.7 2021/03/29 02:13:37 simonb Exp $	*/
      2 /*-
      3  * Copyright (c) 1989, 1993
      4  *	The Regents of the University of California.  All rights reserved.
      5  *
      6  * This code is derived from software contributed to Berkeley by
      7  * Rick Macklem at The University of Guelph.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  *
     33  *	@(#)nfs_bio.c	8.9 (Berkeley) 3/30/95
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 /* __FBSDID("FreeBSD: head/sys/fs/nfsclient/nfs_clbio.c 304026 2016-08-12 22:44:59Z rmacklem "); */
     38 __RCSID("$NetBSD: nfs_clbio.c,v 1.7 2021/03/29 02:13:37 simonb Exp $");
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/buf.h>
     43 #include <sys/kernel.h>
     44 #include <sys/mount.h>
     45 #include <sys/rwlock.h>
     46 #include <sys/vmmeter.h>
     47 #include <sys/vnode.h>
     48 
     49 #include <fs/nfs/common/nfsport.h>
     50 #include <fs/nfs/client/nfsmount.h>
     51 #include <fs/nfs/client/nfs.h>
     52 #include <fs/nfs/client/nfsnode.h>
     53 #include <fs/nfs/client/nfs_kdtrace.h>
     54 
     55 extern int newnfs_directio_allow_mmap;
     56 extern struct nfsstatsv1 nfsstatsv1;
     57 extern struct mtx ncl_iod_mutex;
     58 extern int ncl_numasync;
     59 extern enum nfsiod_state ncl_iodwant[NFS_MAXASYNCDAEMON];
     60 extern struct nfsmount *ncl_iodmount[NFS_MAXASYNCDAEMON];
     61 extern int newnfs_directio_enable;
     62 extern int nfs_keep_dirty_on_error;
     63 
     64 int ncl_pbuf_freecnt = -1;	/* start out unlimited */
     65 
     66 static struct buf *nfs_getcacheblk(struct vnode *vp, daddr_t bn, int size,
     67     struct thread *td);
     68 static int nfs_directio_write(struct vnode *vp, struct uio *uiop,
     69     struct ucred *cred, int ioflag);
     70 
     71 /*
     72  * Vnode op for VM getpages.
     73  */
     74 int
     75 ncl_getpages(struct vop_getpages_args *ap)
     76 {
     77 	int i, error, nextoff, size, toff, count, npages;
     78 	struct uio uio;
     79 	struct iovec iov;
     80 	vaddr_t kva;
     81 	struct buf *bp;
     82 	struct vnode *vp;
     83 	struct thread *td;
     84 	struct ucred *cred;
     85 	struct nfsmount *nmp;
     86 	vm_object_t object;
     87 	vm_page_t *pages;
     88 	struct nfsnode *np;
     89 
     90 	vp = ap->a_vp;
     91 	np = VTONFS(vp);
     92 	td = curthread;				/* XXX */
     93 	cred = curthread->td_ucred;		/* XXX */
     94 	nmp = VFSTONFS(vp->v_mount);
     95 	pages = ap->a_m;
     96 	npages = ap->a_count;
     97 
     98 	if ((object = vp->v_object) == NULL) {
     99 		printf("ncl_getpages: called with non-merged cache vnode\n");
    100 		return (VM_PAGER_ERROR);
    101 	}
    102 
    103 	if (newnfs_directio_enable && !newnfs_directio_allow_mmap) {
    104 		mtx_lock(&np->n_mtx);
    105 		if ((np->n_flag & NNONCACHE) && (vp->v_type == VREG)) {
    106 			mtx_unlock(&np->n_mtx);
    107 			printf("ncl_getpages: called on non-cacheable vnode\n");
    108 			return (VM_PAGER_ERROR);
    109 		} else
    110 			mtx_unlock(&np->n_mtx);
    111 	}
    112 
    113 	mtx_lock(&nmp->nm_mtx);
    114 	if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
    115 	    (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) {
    116 		mtx_unlock(&nmp->nm_mtx);
    117 		/* We'll never get here for v4, because we always have fsinfo */
    118 		(void)ncl_fsinfo(nmp, vp, cred, td);
    119 	} else
    120 		mtx_unlock(&nmp->nm_mtx);
    121 
    122 	/*
    123 	 * If the requested page is partially valid, just return it and
    124 	 * allow the pager to zero-out the blanks.  Partially valid pages
    125 	 * can only occur at the file EOF.
    126 	 *
    127 	 * XXXGL: is that true for NFS, where short read can occur???
    128 	 */
    129 	VM_OBJECT_WLOCK(object);
    130 	if (pages[npages - 1]->valid != 0 && --npages == 0)
    131 		goto out;
    132 	VM_OBJECT_WUNLOCK(object);
    133 
    134 	/*
    135 	 * We use only the kva address for the buffer, but this is extremely
    136 	 * convenient and fast.
    137 	 */
    138 	bp = getpbuf(&ncl_pbuf_freecnt);
    139 
    140 	kva = (vaddr_t) bp->b_data;
    141 	pmap_qenter(kva, pages, npages);
    142 	PCPU_INC(cnt.v_vnodein);
    143 	PCPU_ADD(cnt.v_vnodepgsin, npages);
    144 
    145 	count = npages << PAGE_SHIFT;
    146 	iov.iov_base = (caddr_t) kva;
    147 	iov.iov_len = count;
    148 	uio.uio_iov = &iov;
    149 	uio.uio_iovcnt = 1;
    150 	uio.uio_offset = IDX_TO_OFF(pages[0]->pindex);
    151 	uio.uio_resid = count;
    152 	uio.uio_segflg = UIO_SYSSPACE;
    153 	uio.uio_rw = UIO_READ;
    154 	uio.uio_td = td;
    155 
    156 	error = ncl_readrpc(vp, &uio, cred);
    157 	pmap_qremove(kva, npages);
    158 
    159 	relpbuf(bp, &ncl_pbuf_freecnt);
    160 
    161 	if (error && (uio.uio_resid == count)) {
    162 		printf("ncl_getpages: error %d\n", error);
    163 		return (VM_PAGER_ERROR);
    164 	}
    165 
    166 	/*
    167 	 * Calculate the number of bytes read and validate only that number
    168 	 * of bytes.  Note that due to pending writes, size may be 0.  This
    169 	 * does not mean that the remaining data is invalid!
    170 	 */
    171 
    172 	size = count - uio.uio_resid;
    173 	VM_OBJECT_WLOCK(object);
    174 	for (i = 0, toff = 0; i < npages; i++, toff = nextoff) {
    175 		vm_page_t m;
    176 		nextoff = toff + PAGE_SIZE;
    177 		m = pages[i];
    178 
    179 		if (nextoff <= size) {
    180 			/*
    181 			 * Read operation filled an entire page
    182 			 */
    183 			m->valid = VM_PAGE_BITS_ALL;
    184 			KASSERT(m->dirty == 0,
    185 			    ("nfs_getpages: page %p is dirty", m));
    186 		} else if (size > toff) {
    187 			/*
    188 			 * Read operation filled a partial page.
    189 			 */
    190 			m->valid = 0;
    191 			vm_page_set_valid_range(m, 0, size - toff);
    192 			KASSERT(m->dirty == 0,
    193 			    ("nfs_getpages: page %p is dirty", m));
    194 		} else {
    195 			/*
    196 			 * Read operation was short.  If no error
    197 			 * occurred we may have hit a zero-fill
    198 			 * section.  We leave valid set to 0, and page
    199 			 * is freed by vm_page_readahead_finish() if
    200 			 * its index is not equal to requested, or
    201 			 * page is zeroed and set valid by
    202 			 * vm_pager_get_pages() for requested page.
    203 			 */
    204 			;
    205 		}
    206 	}
    207 out:
    208 	VM_OBJECT_WUNLOCK(object);
    209 	if (ap->a_rbehind)
    210 		*ap->a_rbehind = 0;
    211 	if (ap->a_rahead)
    212 		*ap->a_rahead = 0;
    213 	return (VM_PAGER_OK);
    214 }
    215 
    216 /*
    217  * Vnode op for VM putpages.
    218  */
    219 int
    220 ncl_putpages(struct vop_putpages_args *ap)
    221 {
    222 	struct uio uio;
    223 	struct iovec iov;
    224 	vaddr_t kva;
    225 	struct buf *bp;
    226 	int iomode, must_commit, i, error, npages, count;
    227 	off_t offset;
    228 	int *rtvals;
    229 	struct vnode *vp;
    230 	struct thread *td;
    231 	struct ucred *cred;
    232 	struct nfsmount *nmp;
    233 	struct nfsnode *np;
    234 	vm_page_t *pages;
    235 
    236 	vp = ap->a_vp;
    237 	np = VTONFS(vp);
    238 	td = curthread;				/* XXX */
    239 	/* Set the cred to n_writecred for the write rpcs. */
    240 	if (np->n_writecred != NULL)
    241 		cred = crhold(np->n_writecred);
    242 	else
    243 		cred = crhold(curthread->td_ucred);	/* XXX */
    244 	nmp = VFSTONFS(vp->v_mount);
    245 	pages = ap->a_m;
    246 	count = ap->a_count;
    247 	rtvals = ap->a_rtvals;
    248 	npages = btoc(count);
    249 	offset = IDX_TO_OFF(pages[0]->pindex);
    250 
    251 	mtx_lock(&nmp->nm_mtx);
    252 	if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
    253 	    (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) {
    254 		mtx_unlock(&nmp->nm_mtx);
    255 		(void)ncl_fsinfo(nmp, vp, cred, td);
    256 	} else
    257 		mtx_unlock(&nmp->nm_mtx);
    258 
    259 	mtx_lock(&np->n_mtx);
    260 	if (newnfs_directio_enable && !newnfs_directio_allow_mmap &&
    261 	    (np->n_flag & NNONCACHE) && (vp->v_type == VREG)) {
    262 		mtx_unlock(&np->n_mtx);
    263 		printf("ncl_putpages: called on noncache-able vnode\n");
    264 		mtx_lock(&np->n_mtx);
    265 	}
    266 
    267 	for (i = 0; i < npages; i++)
    268 		rtvals[i] = VM_PAGER_ERROR;
    269 
    270 	/*
    271 	 * When putting pages, do not extend file past EOF.
    272 	 */
    273 	if (offset + count > np->n_size) {
    274 		count = np->n_size - offset;
    275 		if (count < 0)
    276 			count = 0;
    277 	}
    278 	mtx_unlock(&np->n_mtx);
    279 
    280 	/*
    281 	 * We use only the kva address for the buffer, but this is extremely
    282 	 * convenient and fast.
    283 	 */
    284 	bp = getpbuf(&ncl_pbuf_freecnt);
    285 
    286 	kva = (vaddr_t) bp->b_data;
    287 	pmap_qenter(kva, pages, npages);
    288 	PCPU_INC(cnt.v_vnodeout);
    289 	PCPU_ADD(cnt.v_vnodepgsout, count);
    290 
    291 	iov.iov_base = (caddr_t) kva;
    292 	iov.iov_len = count;
    293 	uio.uio_iov = &iov;
    294 	uio.uio_iovcnt = 1;
    295 	uio.uio_offset = offset;
    296 	uio.uio_resid = count;
    297 	uio.uio_segflg = UIO_SYSSPACE;
    298 	uio.uio_rw = UIO_WRITE;
    299 	uio.uio_td = td;
    300 
    301 	if ((ap->a_sync & VM_PAGER_PUT_SYNC) == 0)
    302 	    iomode = NFSWRITE_UNSTABLE;
    303 	else
    304 	    iomode = NFSWRITE_FILESYNC;
    305 
    306 	error = ncl_writerpc(vp, &uio, cred, &iomode, &must_commit, 0);
    307 	crfree(cred);
    308 
    309 	pmap_qremove(kva, npages);
    310 	relpbuf(bp, &ncl_pbuf_freecnt);
    311 
    312 	if (error == 0 || !nfs_keep_dirty_on_error) {
    313 		vnode_pager_undirty_pages(pages, rtvals, count - uio.uio_resid);
    314 		if (must_commit)
    315 			ncl_clearcommit(vp->v_mount);
    316 	}
    317 	return rtvals[0];
    318 }
    319 
    320 /*
    321  * For nfs, cache consistency can only be maintained approximately.
    322  * Although RFC1094 does not specify the criteria, the following is
    323  * believed to be compatible with the reference port.
    324  * For nfs:
    325  * If the file's modify time on the server has changed since the
    326  * last read rpc or you have written to the file,
    327  * you may have lost data cache consistency with the
    328  * server, so flush all of the file's data out of the cache.
    329  * Then force a getattr rpc to ensure that you have up to date
    330  * attributes.
    331  * NB: This implies that cache data can be read when up to
    332  * NFS_ATTRTIMEO seconds out of date. If you find that you need current
    333  * attributes this could be forced by setting n_attrstamp to 0 before
    334  * the VOP_GETATTR() call.
    335  */
    336 static inline int
    337 nfs_bioread_check_cons(struct vnode *vp, struct thread *td, struct ucred *cred)
    338 {
    339 	int error = 0;
    340 	struct vattr vattr;
    341 	struct nfsnode *np = VTONFS(vp);
    342 	int old_lock;
    343 
    344 	/*
    345 	 * Grab the exclusive lock before checking whether the cache is
    346 	 * consistent.
    347 	 * XXX - We can make this cheaper later (by acquiring cheaper locks).
    348 	 * But for now, this suffices.
    349 	 */
    350 	old_lock = ncl_upgrade_vnlock(vp);
    351 	if (vp->v_iflag & VI_DOOMED) {
    352 		ncl_downgrade_vnlock(vp, old_lock);
    353 		return (EBADF);
    354 	}
    355 
    356 	mtx_lock(&np->n_mtx);
    357 	if (np->n_flag & NMODIFIED) {
    358 		mtx_unlock(&np->n_mtx);
    359 		if (vp->v_type != VREG) {
    360 			if (vp->v_type != VDIR)
    361 				panic("nfs: bioread, not dir");
    362 			ncl_invaldir(vp);
    363 			error = ncl_vinvalbuf(vp, V_SAVE, td, 1);
    364 			if (error)
    365 				goto out;
    366 		}
    367 		np->n_attrstamp = 0;
    368 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
    369 		error = VOP_GETATTR(vp, &vattr, cred);
    370 		if (error)
    371 			goto out;
    372 		mtx_lock(&np->n_mtx);
    373 		np->n_mtime = vattr.va_mtime;
    374 		mtx_unlock(&np->n_mtx);
    375 	} else {
    376 		mtx_unlock(&np->n_mtx);
    377 		error = VOP_GETATTR(vp, &vattr, cred);
    378 		if (error)
    379 			return (error);
    380 		mtx_lock(&np->n_mtx);
    381 		if ((np->n_flag & NSIZECHANGED)
    382 		    || (NFS_TIMESPEC_COMPARE(&np->n_mtime, &vattr.va_mtime))) {
    383 			mtx_unlock(&np->n_mtx);
    384 			if (vp->v_type == VDIR)
    385 				ncl_invaldir(vp);
    386 			error = ncl_vinvalbuf(vp, V_SAVE, td, 1);
    387 			if (error)
    388 				goto out;
    389 			mtx_lock(&np->n_mtx);
    390 			np->n_mtime = vattr.va_mtime;
    391 			np->n_flag &= ~NSIZECHANGED;
    392 		}
    393 		mtx_unlock(&np->n_mtx);
    394 	}
    395 out:
    396 	ncl_downgrade_vnlock(vp, old_lock);
    397 	return error;
    398 }
    399 
    400 /*
    401  * Vnode op for read using bio
    402  */
    403 int
    404 ncl_bioread(struct vnode *vp, struct uio *uio, int ioflag, struct ucred *cred)
    405 {
    406 	struct nfsnode *np = VTONFS(vp);
    407 	int biosize, i;
    408 	struct buf *bp, *rabp;
    409 	struct thread *td;
    410 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
    411 	daddr_t lbn, rabn;
    412 	int bcount;
    413 	int seqcount;
    414 	int nra, error = 0, n = 0, on = 0;
    415 	off_t tmp_off;
    416 
    417 	KASSERT(uio->uio_rw == UIO_READ, ("ncl_read mode"));
    418 	if (uio->uio_resid == 0)
    419 		return (0);
    420 	if (uio->uio_offset < 0)	/* XXX VDIR cookies can be negative */
    421 		return (EINVAL);
    422 	td = uio->uio_td;
    423 
    424 	mtx_lock(&nmp->nm_mtx);
    425 	if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
    426 	    (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) {
    427 		mtx_unlock(&nmp->nm_mtx);
    428 		(void)ncl_fsinfo(nmp, vp, cred, td);
    429 		mtx_lock(&nmp->nm_mtx);
    430 	}
    431 	if (nmp->nm_rsize == 0 || nmp->nm_readdirsize == 0)
    432 		(void) newnfs_iosize(nmp);
    433 
    434 	tmp_off = uio->uio_offset + uio->uio_resid;
    435 	if (vp->v_type != VDIR &&
    436 	    (tmp_off > nmp->nm_maxfilesize || tmp_off < uio->uio_offset)) {
    437 		mtx_unlock(&nmp->nm_mtx);
    438 		return (EFBIG);
    439 	}
    440 	mtx_unlock(&nmp->nm_mtx);
    441 
    442 	if (newnfs_directio_enable && (ioflag & IO_DIRECT) && (vp->v_type == VREG))
    443 		/* No caching/ no readaheads. Just read data into the user buffer */
    444 		return ncl_readrpc(vp, uio, cred);
    445 
    446 	biosize = vp->v_bufobj.bo_bsize;
    447 	seqcount = (int)((off_t)(ioflag >> IO_SEQSHIFT) * biosize / BKVASIZE);
    448 
    449 	error = nfs_bioread_check_cons(vp, td, cred);
    450 	if (error)
    451 		return error;
    452 
    453 	do {
    454 	    u_quad_t nsize;
    455 
    456 	    mtx_lock(&np->n_mtx);
    457 	    nsize = np->n_size;
    458 	    mtx_unlock(&np->n_mtx);
    459 
    460 	    switch (vp->v_type) {
    461 	    case VREG:
    462 		NFSINCRGLOBAL(nfsstatsv1.biocache_reads);
    463 		lbn = uio->uio_offset / biosize;
    464 		on = uio->uio_offset - (lbn * biosize);
    465 
    466 		/*
    467 		 * Start the read ahead(s), as required.
    468 		 */
    469 		if (nmp->nm_readahead > 0) {
    470 		    for (nra = 0; nra < nmp->nm_readahead && nra < seqcount &&
    471 			(off_t)(lbn + 1 + nra) * biosize < nsize; nra++) {
    472 			rabn = lbn + 1 + nra;
    473 			if (incore(&vp->v_bufobj, rabn) == NULL) {
    474 			    rabp = nfs_getcacheblk(vp, rabn, biosize, td);
    475 			    if (!rabp) {
    476 				error = newnfs_sigintr(nmp, td);
    477 				return (error ? error : EINTR);
    478 			    }
    479 			    if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
    480 				rabp->b_flags |= B_ASYNC;
    481 				rabp->b_iocmd = BIO_READ;
    482 				vfs_busy_pages(rabp, 0);
    483 				if (ncl_asyncio(nmp, rabp, cred, td)) {
    484 				    rabp->b_flags |= B_INVAL;
    485 				    rabp->b_ioflags |= BIO_ERROR;
    486 				    vfs_unbusy_pages(rabp);
    487 				    brelse(rabp);
    488 				    break;
    489 				}
    490 			    } else {
    491 				brelse(rabp);
    492 			    }
    493 			}
    494 		    }
    495 		}
    496 
    497 		/* Note that bcount is *not* DEV_BSIZE aligned. */
    498 		bcount = biosize;
    499 		if ((off_t)lbn * biosize >= nsize) {
    500 			bcount = 0;
    501 		} else if ((off_t)(lbn + 1) * biosize > nsize) {
    502 			bcount = nsize - (off_t)lbn * biosize;
    503 		}
    504 		bp = nfs_getcacheblk(vp, lbn, bcount, td);
    505 
    506 		if (!bp) {
    507 			error = newnfs_sigintr(nmp, td);
    508 			return (error ? error : EINTR);
    509 		}
    510 
    511 		/*
    512 		 * If B_CACHE is not set, we must issue the read.  If this
    513 		 * fails, we return an error.
    514 		 */
    515 
    516 		if ((bp->b_flags & B_CACHE) == 0) {
    517 		    bp->b_iocmd = BIO_READ;
    518 		    vfs_busy_pages(bp, 0);
    519 		    error = ncl_doio(vp, bp, cred, td, 0);
    520 		    if (error) {
    521 			brelse(bp);
    522 			return (error);
    523 		    }
    524 		}
    525 
    526 		/*
    527 		 * on is the offset into the current bp.  Figure out how many
    528 		 * bytes we can copy out of the bp.  Note that bcount is
    529 		 * NOT DEV_BSIZE aligned.
    530 		 *
    531 		 * Then figure out how many bytes we can copy into the uio.
    532 		 */
    533 
    534 		n = 0;
    535 		if (on < bcount)
    536 			n = MIN((unsigned)(bcount - on), uio->uio_resid);
    537 		break;
    538 	    case VLNK:
    539 		NFSINCRGLOBAL(nfsstatsv1.biocache_readlinks);
    540 		bp = nfs_getcacheblk(vp, (daddr_t)0, NFS_MAXPATHLEN, td);
    541 		if (!bp) {
    542 			error = newnfs_sigintr(nmp, td);
    543 			return (error ? error : EINTR);
    544 		}
    545 		if ((bp->b_flags & B_CACHE) == 0) {
    546 		    bp->b_iocmd = BIO_READ;
    547 		    vfs_busy_pages(bp, 0);
    548 		    error = ncl_doio(vp, bp, cred, td, 0);
    549 		    if (error) {
    550 			bp->b_ioflags |= BIO_ERROR;
    551 			brelse(bp);
    552 			return (error);
    553 		    }
    554 		}
    555 		n = MIN(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid);
    556 		on = 0;
    557 		break;
    558 	    case VDIR:
    559 		NFSINCRGLOBAL(nfsstatsv1.biocache_readdirs);
    560 		if (np->n_direofoffset
    561 		    && uio->uio_offset >= np->n_direofoffset) {
    562 		    return (0);
    563 		}
    564 		lbn = (uoff_t)uio->uio_offset / NFS_DIRBLKSIZ;
    565 		on = uio->uio_offset & (NFS_DIRBLKSIZ - 1);
    566 		bp = nfs_getcacheblk(vp, lbn, NFS_DIRBLKSIZ, td);
    567 		if (!bp) {
    568 		    error = newnfs_sigintr(nmp, td);
    569 		    return (error ? error : EINTR);
    570 		}
    571 		if ((bp->b_flags & B_CACHE) == 0) {
    572 		    bp->b_iocmd = BIO_READ;
    573 		    vfs_busy_pages(bp, 0);
    574 		    error = ncl_doio(vp, bp, cred, td, 0);
    575 		    if (error) {
    576 			    brelse(bp);
    577 		    }
    578 		    while (error == NFSERR_BAD_COOKIE) {
    579 			ncl_invaldir(vp);
    580 			error = ncl_vinvalbuf(vp, 0, td, 1);
    581 			/*
    582 			 * Yuck! The directory has been modified on the
    583 			 * server. The only way to get the block is by
    584 			 * reading from the beginning to get all the
    585 			 * offset cookies.
    586 			 *
    587 			 * Leave the last bp intact unless there is an error.
    588 			 * Loop back up to the while if the error is another
    589 			 * NFSERR_BAD_COOKIE (double yuch!).
    590 			 */
    591 			for (i = 0; i <= lbn && !error; i++) {
    592 			    if (np->n_direofoffset
    593 				&& (i * NFS_DIRBLKSIZ) >= np->n_direofoffset)
    594 				    return (0);
    595 			    bp = nfs_getcacheblk(vp, i, NFS_DIRBLKSIZ, td);
    596 			    if (!bp) {
    597 				error = newnfs_sigintr(nmp, td);
    598 				return (error ? error : EINTR);
    599 			    }
    600 			    if ((bp->b_flags & B_CACHE) == 0) {
    601 				    bp->b_iocmd = BIO_READ;
    602 				    vfs_busy_pages(bp, 0);
    603 				    error = ncl_doio(vp, bp, cred, td, 0);
    604 				    /*
    605 				     * no error + B_INVAL == directory EOF,
    606 				     * use the block.
    607 				     */
    608 				    if (error == 0 && (bp->b_flags & B_INVAL))
    609 					    break;
    610 			    }
    611 			    /*
    612 			     * An error will throw away the block and the
    613 			     * for loop will break out.  If no error and this
    614 			     * is not the block we want, we throw away the
    615 			     * block and go for the next one via the for loop.
    616 			     */
    617 			    if (error || i < lbn)
    618 				    brelse(bp);
    619 			}
    620 		    }
    621 		    /*
    622 		     * The above while is repeated if we hit another cookie
    623 		     * error.  If we hit an error and it wasn't a cookie error,
    624 		     * we give up.
    625 		     */
    626 		    if (error)
    627 			    return (error);
    628 		}
    629 
    630 		/*
    631 		 * If not eof and read aheads are enabled, start one.
    632 		 * (You need the current block first, so that you have the
    633 		 *  directory offset cookie of the next block.)
    634 		 */
    635 		if (nmp->nm_readahead > 0 &&
    636 		    (bp->b_flags & B_INVAL) == 0 &&
    637 		    (np->n_direofoffset == 0 ||
    638 		    (lbn + 1) * NFS_DIRBLKSIZ < np->n_direofoffset) &&
    639 		    incore(&vp->v_bufobj, lbn + 1) == NULL) {
    640 			rabp = nfs_getcacheblk(vp, lbn + 1, NFS_DIRBLKSIZ, td);
    641 			if (rabp) {
    642 			    if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
    643 				rabp->b_flags |= B_ASYNC;
    644 				rabp->b_iocmd = BIO_READ;
    645 				vfs_busy_pages(rabp, 0);
    646 				if (ncl_asyncio(nmp, rabp, cred, td)) {
    647 				    rabp->b_flags |= B_INVAL;
    648 				    rabp->b_ioflags |= BIO_ERROR;
    649 				    vfs_unbusy_pages(rabp);
    650 				    brelse(rabp);
    651 				}
    652 			    } else {
    653 				brelse(rabp);
    654 			    }
    655 			}
    656 		}
    657 		/*
    658 		 * Unlike VREG files, whos buffer size ( bp->b_bcount ) is
    659 		 * chopped for the EOF condition, we cannot tell how large
    660 		 * NFS directories are going to be until we hit EOF.  So
    661 		 * an NFS directory buffer is *not* chopped to its EOF.  Now,
    662 		 * it just so happens that b_resid will effectively chop it
    663 		 * to EOF.  *BUT* this information is lost if the buffer goes
    664 		 * away and is reconstituted into a B_CACHE state ( due to
    665 		 * being VMIO ) later.  So we keep track of the directory eof
    666 		 * in np->n_direofoffset and chop it off as an extra step
    667 		 * right here.
    668 		 */
    669 		n = lmin(uio->uio_resid, NFS_DIRBLKSIZ - bp->b_resid - on);
    670 		if (np->n_direofoffset && n > np->n_direofoffset - uio->uio_offset)
    671 			n = np->n_direofoffset - uio->uio_offset;
    672 		break;
    673 	    default:
    674 		printf(" ncl_bioread: type %x unexpected\n", vp->v_type);
    675 		bp = NULL;
    676 		break;
    677 	    }
    678 
    679 	    if (n > 0) {
    680 		    error = vn_io_fault_uiomove(bp->b_data + on, (int)n, uio);
    681 	    }
    682 	    if (vp->v_type == VLNK)
    683 		n = 0;
    684 	    if (bp != NULL)
    685 		brelse(bp);
    686 	} while (error == 0 && uio->uio_resid > 0 && n > 0);
    687 	return (error);
    688 }
    689 
    690 /*
    691  * The NFS write path cannot handle iovecs with len > 1. So we need to
    692  * break up iovecs accordingly (restricting them to wsize).
    693  * For the SYNC case, we can do this with 1 copy (user buffer -> mbuf).
    694  * For the ASYNC case, 2 copies are needed. The first a copy from the
    695  * user buffer to a staging buffer and then a second copy from the staging
    696  * buffer to mbufs. This can be optimized by copying from the user buffer
    697  * directly into mbufs and passing the chain down, but that requires a
    698  * fair amount of re-working of the relevant codepaths (and can be done
    699  * later).
    700  */
    701 static int
    702 nfs_directio_write(vp, uiop, cred, ioflag)
    703 	struct vnode *vp;
    704 	struct uio *uiop;
    705 	struct ucred *cred;
    706 	int ioflag;
    707 {
    708 	int error;
    709 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
    710 	struct thread *td = uiop->uio_td;
    711 	int size;
    712 	int wsize;
    713 
    714 	mtx_lock(&nmp->nm_mtx);
    715 	wsize = nmp->nm_wsize;
    716 	mtx_unlock(&nmp->nm_mtx);
    717 	if (ioflag & IO_SYNC) {
    718 		int iomode, must_commit;
    719 		struct uio uio;
    720 		struct iovec iov;
    721 do_sync:
    722 		while (uiop->uio_resid > 0) {
    723 			size = MIN(uiop->uio_resid, wsize);
    724 			size = MIN(uiop->uio_iov->iov_len, size);
    725 			iov.iov_base = uiop->uio_iov->iov_base;
    726 			iov.iov_len = size;
    727 			uio.uio_iov = &iov;
    728 			uio.uio_iovcnt = 1;
    729 			uio.uio_offset = uiop->uio_offset;
    730 			uio.uio_resid = size;
    731 			uio.uio_segflg = UIO_USERSPACE;
    732 			uio.uio_rw = UIO_WRITE;
    733 			uio.uio_td = td;
    734 			iomode = NFSWRITE_FILESYNC;
    735 			error = ncl_writerpc(vp, &uio, cred, &iomode,
    736 			    &must_commit, 0);
    737 			KASSERT((must_commit == 0),
    738 				("ncl_directio_write: Did not commit write"));
    739 			if (error)
    740 				return (error);
    741 			uiop->uio_offset += size;
    742 			uiop->uio_resid -= size;
    743 			if (uiop->uio_iov->iov_len <= size) {
    744 				uiop->uio_iovcnt--;
    745 				uiop->uio_iov++;
    746 			} else {
    747 				uiop->uio_iov->iov_base =
    748 					(char *)uiop->uio_iov->iov_base + size;
    749 				uiop->uio_iov->iov_len -= size;
    750 			}
    751 		}
    752 	} else {
    753 		struct uio *t_uio;
    754 		struct iovec *t_iov;
    755 		struct buf *bp;
    756 
    757 		/*
    758 		 * Break up the write into blocksize chunks and hand these
    759 		 * over to nfsiod's for write back.
    760 		 * Unfortunately, this incurs a copy of the data. Since
    761 		 * the user could modify the buffer before the write is
    762 		 * initiated.
    763 		 *
    764 		 * The obvious optimization here is that one of the 2 copies
    765 		 * in the async write path can be eliminated by copying the
    766 		 * data here directly into mbufs and passing the mbuf chain
    767 		 * down. But that will require a fair amount of re-working
    768 		 * of the code and can be done if there's enough interest
    769 		 * in NFS directio access.
    770 		 */
    771 		while (uiop->uio_resid > 0) {
    772 			size = MIN(uiop->uio_resid, wsize);
    773 			size = MIN(uiop->uio_iov->iov_len, size);
    774 			bp = getpbuf(&ncl_pbuf_freecnt);
    775 			t_uio = malloc(sizeof(struct uio), M_NFSDIRECTIO, M_WAITOK);
    776 			t_iov = malloc(sizeof(struct iovec), M_NFSDIRECTIO, M_WAITOK);
    777 			t_iov->iov_base = malloc(size, M_NFSDIRECTIO, M_WAITOK);
    778 			t_iov->iov_len = size;
    779 			t_uio->uio_iov = t_iov;
    780 			t_uio->uio_iovcnt = 1;
    781 			t_uio->uio_offset = uiop->uio_offset;
    782 			t_uio->uio_resid = size;
    783 			t_uio->uio_segflg = UIO_SYSSPACE;
    784 			t_uio->uio_rw = UIO_WRITE;
    785 			t_uio->uio_td = td;
    786 			KASSERT(uiop->uio_segflg == UIO_USERSPACE ||
    787 			    uiop->uio_segflg == UIO_SYSSPACE,
    788 			    ("nfs_directio_write: Bad uio_segflg"));
    789 			if (uiop->uio_segflg == UIO_USERSPACE) {
    790 				error = copyin(uiop->uio_iov->iov_base,
    791 				    t_iov->iov_base, size);
    792 				if (error != 0)
    793 					goto err_free;
    794 			} else
    795 				/*
    796 				 * UIO_SYSSPACE may never happen, but handle
    797 				 * it just in case it does.
    798 				 */
    799 				bcopy(uiop->uio_iov->iov_base, t_iov->iov_base,
    800 				    size);
    801 			bp->b_flags |= B_DIRECT;
    802 			bp->b_iocmd = BIO_WRITE;
    803 			if (cred != NOCRED) {
    804 				crhold(cred);
    805 				bp->b_wcred = cred;
    806 			} else
    807 				bp->b_wcred = NOCRED;
    808 			bp->b_caller1 = (void *)t_uio;
    809 			bp->b_vp = vp;
    810 			error = ncl_asyncio(nmp, bp, NOCRED, td);
    811 err_free:
    812 			if (error) {
    813 				free(t_iov->iov_base, M_NFSDIRECTIO);
    814 				free(t_iov, M_NFSDIRECTIO);
    815 				free(t_uio, M_NFSDIRECTIO);
    816 				bp->b_vp = NULL;
    817 				relpbuf(bp, &ncl_pbuf_freecnt);
    818 				if (error == EINTR)
    819 					return (error);
    820 				goto do_sync;
    821 			}
    822 			uiop->uio_offset += size;
    823 			uiop->uio_resid -= size;
    824 			if (uiop->uio_iov->iov_len <= size) {
    825 				uiop->uio_iovcnt--;
    826 				uiop->uio_iov++;
    827 			} else {
    828 				uiop->uio_iov->iov_base =
    829 					(char *)uiop->uio_iov->iov_base + size;
    830 				uiop->uio_iov->iov_len -= size;
    831 			}
    832 		}
    833 	}
    834 	return (0);
    835 }
    836 
    837 /*
    838  * Vnode op for write using bio
    839  */
    840 int
    841 ncl_write(struct vop_write_args *ap)
    842 {
    843 	int biosize;
    844 	struct uio *uio = ap->a_uio;
    845 	struct thread *td = uio->uio_td;
    846 	struct vnode *vp = ap->a_vp;
    847 	struct nfsnode *np = VTONFS(vp);
    848 	struct ucred *cred = ap->a_cred;
    849 	int ioflag = ap->a_ioflag;
    850 	struct buf *bp;
    851 	struct vattr vattr;
    852 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
    853 	daddr_t lbn;
    854 	int bcount, noncontig_write, obcount;
    855 	int bp_cached, n, on, error = 0, error1, wouldcommit;
    856 	size_t orig_resid, local_resid;
    857 	off_t orig_size, tmp_off;
    858 
    859 	KASSERT(uio->uio_rw == UIO_WRITE, ("ncl_write mode"));
    860 	KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
    861 	    ("ncl_write proc"));
    862 	if (vp->v_type != VREG)
    863 		return (EIO);
    864 	mtx_lock(&np->n_mtx);
    865 	if (np->n_flag & NWRITEERR) {
    866 		np->n_flag &= ~NWRITEERR;
    867 		mtx_unlock(&np->n_mtx);
    868 		return (np->n_error);
    869 	} else
    870 		mtx_unlock(&np->n_mtx);
    871 	mtx_lock(&nmp->nm_mtx);
    872 	if ((nmp->nm_flag & NFSMNT_NFSV3) != 0 &&
    873 	    (nmp->nm_state & NFSSTA_GOTFSINFO) == 0) {
    874 		mtx_unlock(&nmp->nm_mtx);
    875 		(void)ncl_fsinfo(nmp, vp, cred, td);
    876 		mtx_lock(&nmp->nm_mtx);
    877 	}
    878 	if (nmp->nm_wsize == 0)
    879 		(void) newnfs_iosize(nmp);
    880 	mtx_unlock(&nmp->nm_mtx);
    881 
    882 	/*
    883 	 * Synchronously flush pending buffers if we are in synchronous
    884 	 * mode or if we are appending.
    885 	 */
    886 	if (ioflag & (IO_APPEND | IO_SYNC)) {
    887 		mtx_lock(&np->n_mtx);
    888 		if (np->n_flag & NMODIFIED) {
    889 			mtx_unlock(&np->n_mtx);
    890 #ifdef notyet /* Needs matching nonblock semantics elsewhere, too. */
    891 			/*
    892 			 * Require non-blocking, synchronous writes to
    893 			 * dirty files to inform the program it needs
    894 			 * to fsync(2) explicitly.
    895 			 */
    896 			if (ioflag & IO_NDELAY)
    897 				return (EAGAIN);
    898 #endif
    899 			np->n_attrstamp = 0;
    900 			KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
    901 			error = ncl_vinvalbuf(vp, V_SAVE, td, 1);
    902 			if (error)
    903 				return (error);
    904 		} else
    905 			mtx_unlock(&np->n_mtx);
    906 	}
    907 
    908 	orig_resid = uio->uio_resid;
    909 	mtx_lock(&np->n_mtx);
    910 	orig_size = np->n_size;
    911 	mtx_unlock(&np->n_mtx);
    912 
    913 	/*
    914 	 * If IO_APPEND then load uio_offset.  We restart here if we cannot
    915 	 * get the append lock.
    916 	 */
    917 	if (ioflag & IO_APPEND) {
    918 		np->n_attrstamp = 0;
    919 		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
    920 		error = VOP_GETATTR(vp, &vattr, cred);
    921 		if (error)
    922 			return (error);
    923 		mtx_lock(&np->n_mtx);
    924 		uio->uio_offset = np->n_size;
    925 		mtx_unlock(&np->n_mtx);
    926 	}
    927 
    928 	if (uio->uio_offset < 0)
    929 		return (EINVAL);
    930 	tmp_off = uio->uio_offset + uio->uio_resid;
    931 	if (tmp_off > nmp->nm_maxfilesize || tmp_off < uio->uio_offset)
    932 		return (EFBIG);
    933 	if (uio->uio_resid == 0)
    934 		return (0);
    935 
    936 	if (newnfs_directio_enable && (ioflag & IO_DIRECT) && vp->v_type == VREG)
    937 		return nfs_directio_write(vp, uio, cred, ioflag);
    938 
    939 	/*
    940 	 * Maybe this should be above the vnode op call, but so long as
    941 	 * file servers have no limits, i don't think it matters
    942 	 */
    943 	if (vn_rlimit_fsize(vp, uio, td))
    944 		return (EFBIG);
    945 
    946 	biosize = vp->v_bufobj.bo_bsize;
    947 	/*
    948 	 * Find all of this file's B_NEEDCOMMIT buffers.  If our writes
    949 	 * would exceed the local maximum per-file write commit size when
    950 	 * combined with those, we must decide whether to flush,
    951 	 * go synchronous, or return error.  We don't bother checking
    952 	 * IO_UNIT -- we just make all writes atomic anyway, as there's
    953 	 * no point optimizing for something that really won't ever happen.
    954 	 */
    955 	wouldcommit = 0;
    956 	if (!(ioflag & IO_SYNC)) {
    957 		int nflag;
    958 
    959 		mtx_lock(&np->n_mtx);
    960 		nflag = np->n_flag;
    961 		mtx_unlock(&np->n_mtx);
    962 		if (nflag & NMODIFIED) {
    963 			BO_LOCK(&vp->v_bufobj);
    964 			if (vp->v_bufobj.bo_dirty.bv_cnt != 0) {
    965 				TAILQ_FOREACH(bp, &vp->v_bufobj.bo_dirty.bv_hd,
    966 				    b_bobufs) {
    967 					if (bp->b_flags & B_NEEDCOMMIT)
    968 						wouldcommit += bp->b_bcount;
    969 				}
    970 			}
    971 			BO_UNLOCK(&vp->v_bufobj);
    972 		}
    973 	}
    974 
    975 	do {
    976 		if (!(ioflag & IO_SYNC)) {
    977 			wouldcommit += biosize;
    978 			if (wouldcommit > nmp->nm_wcommitsize) {
    979 				np->n_attrstamp = 0;
    980 				KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
    981 				error = ncl_vinvalbuf(vp, V_SAVE, td, 1);
    982 				if (error)
    983 					return (error);
    984 				wouldcommit = biosize;
    985 			}
    986 		}
    987 
    988 		NFSINCRGLOBAL(nfsstatsv1.biocache_writes);
    989 		lbn = uio->uio_offset / biosize;
    990 		on = uio->uio_offset - (lbn * biosize);
    991 		n = MIN((unsigned)(biosize - on), uio->uio_resid);
    992 again:
    993 		/*
    994 		 * Handle direct append and file extension cases, calculate
    995 		 * unaligned buffer size.
    996 		 */
    997 		mtx_lock(&np->n_mtx);
    998 		if ((np->n_flag & NHASBEENLOCKED) == 0 &&
    999 		    (nmp->nm_flag & NFSMNT_NONCONTIGWR) != 0)
   1000 			noncontig_write = 1;
   1001 		else
   1002 			noncontig_write = 0;
   1003 		if ((uio->uio_offset == np->n_size ||
   1004 		    (noncontig_write != 0 &&
   1005 		    lbn == (np->n_size / biosize) &&
   1006 		    uio->uio_offset + n > np->n_size)) && n) {
   1007 			mtx_unlock(&np->n_mtx);
   1008 			/*
   1009 			 * Get the buffer (in its pre-append state to maintain
   1010 			 * B_CACHE if it was previously set).  Resize the
   1011 			 * nfsnode after we have locked the buffer to prevent
   1012 			 * readers from reading garbage.
   1013 			 */
   1014 			obcount = np->n_size - (lbn * biosize);
   1015 			bp = nfs_getcacheblk(vp, lbn, obcount, td);
   1016 
   1017 			if (bp != NULL) {
   1018 				long save;
   1019 
   1020 				mtx_lock(&np->n_mtx);
   1021 				np->n_size = uio->uio_offset + n;
   1022 				np->n_flag |= NMODIFIED;
   1023 				vnode_pager_setsize(vp, np->n_size);
   1024 				mtx_unlock(&np->n_mtx);
   1025 
   1026 				save = bp->b_flags & B_CACHE;
   1027 				bcount = on + n;
   1028 				allocbuf(bp, bcount);
   1029 				bp->b_flags |= save;
   1030 				if (noncontig_write != 0 && on > obcount)
   1031 					vfs_bio_bzero_buf(bp, obcount, on -
   1032 					    obcount);
   1033 			}
   1034 		} else {
   1035 			/*
   1036 			 * Obtain the locked cache block first, and then
   1037 			 * adjust the file's size as appropriate.
   1038 			 */
   1039 			bcount = on + n;
   1040 			if ((off_t)lbn * biosize + bcount < np->n_size) {
   1041 				if ((off_t)(lbn + 1) * biosize < np->n_size)
   1042 					bcount = biosize;
   1043 				else
   1044 					bcount = np->n_size - (off_t)lbn * biosize;
   1045 			}
   1046 			mtx_unlock(&np->n_mtx);
   1047 			bp = nfs_getcacheblk(vp, lbn, bcount, td);
   1048 			mtx_lock(&np->n_mtx);
   1049 			if (uio->uio_offset + n > np->n_size) {
   1050 				np->n_size = uio->uio_offset + n;
   1051 				np->n_flag |= NMODIFIED;
   1052 				vnode_pager_setsize(vp, np->n_size);
   1053 			}
   1054 			mtx_unlock(&np->n_mtx);
   1055 		}
   1056 
   1057 		if (!bp) {
   1058 			error = newnfs_sigintr(nmp, td);
   1059 			if (!error)
   1060 				error = EINTR;
   1061 			break;
   1062 		}
   1063 
   1064 		/*
   1065 		 * Issue a READ if B_CACHE is not set.  In special-append
   1066 		 * mode, B_CACHE is based on the buffer prior to the write
   1067 		 * op and is typically set, avoiding the read.  If a read
   1068 		 * is required in special append mode, the server will
   1069 		 * probably send us a short-read since we extended the file
   1070 		 * on our end, resulting in b_resid == 0 and, thusly,
   1071 		 * B_CACHE getting set.
   1072 		 *
   1073 		 * We can also avoid issuing the read if the write covers
   1074 		 * the entire buffer.  We have to make sure the buffer state
   1075 		 * is reasonable in this case since we will not be initiating
   1076 		 * I/O.  See the comments in kern/vfs_bio.c's getblk() for
   1077 		 * more information.
   1078 		 *
   1079 		 * B_CACHE may also be set due to the buffer being cached
   1080 		 * normally.
   1081 		 */
   1082 
   1083 		bp_cached = 1;
   1084 		if (on == 0 && n == bcount) {
   1085 			if ((bp->b_flags & B_CACHE) == 0)
   1086 				bp_cached = 0;
   1087 			bp->b_flags |= B_CACHE;
   1088 			bp->b_flags &= ~B_INVAL;
   1089 			bp->b_ioflags &= ~BIO_ERROR;
   1090 		}
   1091 
   1092 		if ((bp->b_flags & B_CACHE) == 0) {
   1093 			bp->b_iocmd = BIO_READ;
   1094 			vfs_busy_pages(bp, 0);
   1095 			error = ncl_doio(vp, bp, cred, td, 0);
   1096 			if (error) {
   1097 				brelse(bp);
   1098 				break;
   1099 			}
   1100 		}
   1101 		if (bp->b_wcred == NOCRED)
   1102 			bp->b_wcred = crhold(cred);
   1103 		mtx_lock(&np->n_mtx);
   1104 		np->n_flag |= NMODIFIED;
   1105 		mtx_unlock(&np->n_mtx);
   1106 
   1107 		/*
   1108 		 * If dirtyend exceeds file size, chop it down.  This should
   1109 		 * not normally occur but there is an append race where it
   1110 		 * might occur XXX, so we log it.
   1111 		 *
   1112 		 * If the chopping creates a reverse-indexed or degenerate
   1113 		 * situation with dirtyoff/end, we 0 both of them.
   1114 		 */
   1115 
   1116 		if (bp->b_dirtyend > bcount) {
   1117 			printf("NFS append race @%lx:%d\n",
   1118 			    (long)bp->b_blkno * DEV_BSIZE,
   1119 			    bp->b_dirtyend - bcount);
   1120 			bp->b_dirtyend = bcount;
   1121 		}
   1122 
   1123 		if (bp->b_dirtyoff >= bp->b_dirtyend)
   1124 			bp->b_dirtyoff = bp->b_dirtyend = 0;
   1125 
   1126 		/*
   1127 		 * If the new write will leave a contiguous dirty
   1128 		 * area, just update the b_dirtyoff and b_dirtyend,
   1129 		 * otherwise force a write rpc of the old dirty area.
   1130 		 *
   1131 		 * If there has been a file lock applied to this file
   1132 		 * or vfs.nfs.old_noncontig_writing is set, do the following:
   1133 		 * While it is possible to merge discontiguous writes due to
   1134 		 * our having a B_CACHE buffer ( and thus valid read data
   1135 		 * for the hole), we don't because it could lead to
   1136 		 * significant cache coherency problems with multiple clients,
   1137 		 * especially if locking is implemented later on.
   1138 		 *
   1139 		 * If vfs.nfs.old_noncontig_writing is not set and there has
   1140 		 * not been file locking done on this file:
   1141 		 * Relax coherency a bit for the sake of performance and
   1142 		 * expand the current dirty region to contain the new
   1143 		 * write even if it means we mark some non-dirty data as
   1144 		 * dirty.
   1145 		 */
   1146 
   1147 		if (noncontig_write == 0 && bp->b_dirtyend > 0 &&
   1148 		    (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) {
   1149 			if (bwrite(bp) == EINTR) {
   1150 				error = EINTR;
   1151 				break;
   1152 			}
   1153 			goto again;
   1154 		}
   1155 
   1156 		local_resid = uio->uio_resid;
   1157 		error = vn_io_fault_uiomove((char *)bp->b_data + on, n, uio);
   1158 
   1159 		if (error != 0 && !bp_cached) {
   1160 			/*
   1161 			 * This block has no other content than what
   1162 			 * possibly was written by the faulty uiomove.
   1163 			 * Release it, forgetting the data pages, to
   1164 			 * prevent the leak of uninitialized data to
   1165 			 * usermode.
   1166 			 */
   1167 			bp->b_ioflags |= BIO_ERROR;
   1168 			brelse(bp);
   1169 			uio->uio_offset -= local_resid - uio->uio_resid;
   1170 			uio->uio_resid = local_resid;
   1171 			break;
   1172 		}
   1173 
   1174 		/*
   1175 		 * Since this block is being modified, it must be written
   1176 		 * again and not just committed.  Since write clustering does
   1177 		 * not work for the stage 1 data write, only the stage 2
   1178 		 * commit rpc, we have to clear B_CLUSTEROK as well.
   1179 		 */
   1180 		bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
   1181 
   1182 		/*
   1183 		 * Get the partial update on the progress made from
   1184 		 * uiomove, if an error occurred.
   1185 		 */
   1186 		if (error != 0)
   1187 			n = local_resid - uio->uio_resid;
   1188 
   1189 		/*
   1190 		 * Only update dirtyoff/dirtyend if not a degenerate
   1191 		 * condition.
   1192 		 */
   1193 		if (n > 0) {
   1194 			if (bp->b_dirtyend > 0) {
   1195 				bp->b_dirtyoff = uimin(on, bp->b_dirtyoff);
   1196 				bp->b_dirtyend = uimax((on + n), bp->b_dirtyend);
   1197 			} else {
   1198 				bp->b_dirtyoff = on;
   1199 				bp->b_dirtyend = on + n;
   1200 			}
   1201 			vfs_bio_set_valid(bp, on, n);
   1202 		}
   1203 
   1204 		/*
   1205 		 * If IO_SYNC do bwrite().
   1206 		 *
   1207 		 * IO_INVAL appears to be unused.  The idea appears to be
   1208 		 * to turn off caching in this case.  Very odd.  XXX
   1209 		 */
   1210 		if ((ioflag & IO_SYNC)) {
   1211 			if (ioflag & IO_INVAL)
   1212 				bp->b_flags |= B_NOCACHE;
   1213 			error1 = bwrite(bp);
   1214 			if (error1 != 0) {
   1215 				if (error == 0)
   1216 					error = error1;
   1217 				break;
   1218 			}
   1219 		} else if ((n + on) == biosize) {
   1220 			bp->b_flags |= B_ASYNC;
   1221 			(void) ncl_writebp(bp, 0, NULL);
   1222 		} else {
   1223 			bdwrite(bp);
   1224 		}
   1225 
   1226 		if (error != 0)
   1227 			break;
   1228 	} while (uio->uio_resid > 0 && n > 0);
   1229 
   1230 	if (error != 0) {
   1231 		if (ioflag & IO_UNIT) {
   1232 			VATTR_NULL(&vattr);
   1233 			vattr.va_size = orig_size;
   1234 			/* IO_SYNC is handled implicitly */
   1235 			(void)VOP_SETATTR(vp, &vattr, cred);
   1236 			uio->uio_offset -= orig_resid - uio->uio_resid;
   1237 			uio->uio_resid = orig_resid;
   1238 		}
   1239 	}
   1240 
   1241 	return (error);
   1242 }
   1243 
   1244 /*
   1245  * Get an nfs cache block.
   1246  *
   1247  * Allocate a new one if the block isn't currently in the cache
   1248  * and return the block marked busy. If the calling process is
   1249  * interrupted by a signal for an interruptible mount point, return
   1250  * NULL.
   1251  *
   1252  * The caller must carefully deal with the possible B_INVAL state of
   1253  * the buffer.  ncl_doio() clears B_INVAL (and ncl_asyncio() clears it
   1254  * indirectly), so synchronous reads can be issued without worrying about
   1255  * the B_INVAL state.  We have to be a little more careful when dealing
   1256  * with writes (see comments in nfs_write()) when extending a file past
   1257  * its EOF.
   1258  */
   1259 static struct buf *
   1260 nfs_getcacheblk(struct vnode *vp, daddr_t bn, int size, struct thread *td)
   1261 {
   1262 	struct buf *bp;
   1263 	struct mount *mp;
   1264 	struct nfsmount *nmp;
   1265 
   1266 	mp = vp->v_mount;
   1267 	nmp = VFSTONFS(mp);
   1268 
   1269 	if (nmp->nm_flag & NFSMNT_INT) {
   1270 		sigset_t oldset;
   1271 
   1272 		newnfs_set_sigmask(td, &oldset);
   1273 		bp = getblk(vp, bn, size, PCATCH, 0, 0);
   1274 		newnfs_restore_sigmask(td, &oldset);
   1275 		while (bp == NULL) {
   1276 			if (newnfs_sigintr(nmp, td))
   1277 				return (NULL);
   1278 			bp = getblk(vp, bn, size, 0, 2 * hz, 0);
   1279 		}
   1280 	} else {
   1281 		bp = getblk(vp, bn, size, 0, 0, 0);
   1282 	}
   1283 
   1284 	if (vp->v_type == VREG)
   1285 		bp->b_blkno = bn * (vp->v_bufobj.bo_bsize / DEV_BSIZE);
   1286 	return (bp);
   1287 }
   1288 
   1289 /*
   1290  * Flush and invalidate all dirty buffers. If another process is already
   1291  * doing the flush, just wait for completion.
   1292  */
   1293 int
   1294 ncl_vinvalbuf(struct vnode *vp, int flags, struct thread *td, int intrflg)
   1295 {
   1296 	struct nfsnode *np = VTONFS(vp);
   1297 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
   1298 	int error = 0, slpflag, slptimeo;
   1299 	int old_lock = 0;
   1300 
   1301 	ASSERT_VOP_LOCKED(vp, "ncl_vinvalbuf");
   1302 
   1303 	if ((nmp->nm_flag & NFSMNT_INT) == 0)
   1304 		intrflg = 0;
   1305 	if ((nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF))
   1306 		intrflg = 1;
   1307 	if (intrflg) {
   1308 		slpflag = PCATCH;
   1309 		slptimeo = 2 * hz;
   1310 	} else {
   1311 		slpflag = 0;
   1312 		slptimeo = 0;
   1313 	}
   1314 
   1315 	old_lock = ncl_upgrade_vnlock(vp);
   1316 	if (vp->v_iflag & VI_DOOMED) {
   1317 		/*
   1318 		 * Since vgonel() uses the generic vinvalbuf() to flush
   1319 		 * dirty buffers and it does not call this function, it
   1320 		 * is safe to just return OK when VI_DOOMED is set.
   1321 		 */
   1322 		ncl_downgrade_vnlock(vp, old_lock);
   1323 		return (0);
   1324 	}
   1325 
   1326 	/*
   1327 	 * Now, flush as required.
   1328 	 */
   1329 	if ((flags & V_SAVE) && (vp->v_bufobj.bo_object != NULL)) {
   1330 		VM_OBJECT_WLOCK(vp->v_bufobj.bo_object);
   1331 		vm_object_page_clean(vp->v_bufobj.bo_object, 0, 0, OBJPC_SYNC);
   1332 		VM_OBJECT_WUNLOCK(vp->v_bufobj.bo_object);
   1333 		/*
   1334 		 * If the page clean was interrupted, fail the invalidation.
   1335 		 * Not doing so, we run the risk of losing dirty pages in the
   1336 		 * vinvalbuf() call below.
   1337 		 */
   1338 		if (intrflg && (error = newnfs_sigintr(nmp, td)))
   1339 			goto out;
   1340 	}
   1341 
   1342 	error = vinvalbuf(vp, flags, slpflag, 0);
   1343 	while (error) {
   1344 		if (intrflg && (error = newnfs_sigintr(nmp, td)))
   1345 			goto out;
   1346 		error = vinvalbuf(vp, flags, 0, slptimeo);
   1347 	}
   1348 	if (NFSHASPNFS(nmp)) {
   1349 		nfscl_layoutcommit(vp, td);
   1350 		/*
   1351 		 * Invalidate the attribute cache, since writes to a DS
   1352 		 * won't update the size attribute.
   1353 		 */
   1354 		mtx_lock(&np->n_mtx);
   1355 		np->n_attrstamp = 0;
   1356 	} else
   1357 		mtx_lock(&np->n_mtx);
   1358 	if (np->n_directio_asyncwr == 0)
   1359 		np->n_flag &= ~NMODIFIED;
   1360 	mtx_unlock(&np->n_mtx);
   1361 out:
   1362 	ncl_downgrade_vnlock(vp, old_lock);
   1363 	return error;
   1364 }
   1365 
   1366 /*
   1367  * Initiate asynchronous I/O. Return an error if no nfsiods are available.
   1368  * This is mainly to avoid queueing async I/O requests when the nfsiods
   1369  * are all hung on a dead server.
   1370  *
   1371  * Note: ncl_asyncio() does not clear (BIO_ERROR|B_INVAL) but when the bp
   1372  * is eventually dequeued by the async daemon, ncl_doio() *will*.
   1373  */
   1374 int
   1375 ncl_asyncio(struct nfsmount *nmp, struct buf *bp, struct ucred *cred, struct thread *td)
   1376 {
   1377 	int iod;
   1378 	int gotiod;
   1379 	int slpflag = 0;
   1380 	int slptimeo = 0;
   1381 	int error, error2;
   1382 
   1383 	/*
   1384 	 * Commits are usually short and sweet so lets save some cpu and
   1385 	 * leave the async daemons for more important rpc's (such as reads
   1386 	 * and writes).
   1387 	 *
   1388 	 * Readdirplus RPCs do vget()s to acquire the vnodes for entries
   1389 	 * in the directory in order to update attributes. This can deadlock
   1390 	 * with another thread that is waiting for async I/O to be done by
   1391 	 * an nfsiod thread while holding a lock on one of these vnodes.
   1392 	 * To avoid this deadlock, don't allow the async nfsiod threads to
   1393 	 * perform Readdirplus RPCs.
   1394 	 */
   1395 	mtx_lock(&ncl_iod_mutex);
   1396 	if ((bp->b_iocmd == BIO_WRITE && (bp->b_flags & B_NEEDCOMMIT) &&
   1397 	     (nmp->nm_bufqiods > ncl_numasync / 2)) ||
   1398 	    (bp->b_vp->v_type == VDIR && (nmp->nm_flag & NFSMNT_RDIRPLUS))) {
   1399 		mtx_unlock(&ncl_iod_mutex);
   1400 		return(EIO);
   1401 	}
   1402 again:
   1403 	if (nmp->nm_flag & NFSMNT_INT)
   1404 		slpflag = PCATCH;
   1405 	gotiod = FALSE;
   1406 
   1407 	/*
   1408 	 * Find a free iod to process this request.
   1409 	 */
   1410 	for (iod = 0; iod < ncl_numasync; iod++)
   1411 		if (ncl_iodwant[iod] == NFSIOD_AVAILABLE) {
   1412 			gotiod = TRUE;
   1413 			break;
   1414 		}
   1415 
   1416 	/*
   1417 	 * Try to create one if none are free.
   1418 	 */
   1419 	if (!gotiod)
   1420 		ncl_nfsiodnew();
   1421 	else {
   1422 		/*
   1423 		 * Found one, so wake it up and tell it which
   1424 		 * mount to process.
   1425 		 */
   1426 		NFS_DPF(ASYNCIO, ("ncl_asyncio: waking iod %d for mount %p\n",
   1427 		    iod, nmp));
   1428 		ncl_iodwant[iod] = NFSIOD_NOT_AVAILABLE;
   1429 		ncl_iodmount[iod] = nmp;
   1430 		nmp->nm_bufqiods++;
   1431 		wakeup(&ncl_iodwant[iod]);
   1432 	}
   1433 
   1434 	/*
   1435 	 * If none are free, we may already have an iod working on this mount
   1436 	 * point.  If so, it will process our request.
   1437 	 */
   1438 	if (!gotiod) {
   1439 		if (nmp->nm_bufqiods > 0) {
   1440 			NFS_DPF(ASYNCIO,
   1441 				("ncl_asyncio: %d iods are already processing mount %p\n",
   1442 				 nmp->nm_bufqiods, nmp));
   1443 			gotiod = TRUE;
   1444 		}
   1445 	}
   1446 
   1447 	/*
   1448 	 * If we have an iod which can process the request, then queue
   1449 	 * the buffer.
   1450 	 */
   1451 	if (gotiod) {
   1452 		/*
   1453 		 * Ensure that the queue never grows too large.  We still want
   1454 		 * to asynchronize so we block rather than return EIO.
   1455 		 */
   1456 		while (nmp->nm_bufqlen >= 2*ncl_numasync) {
   1457 			NFS_DPF(ASYNCIO,
   1458 				("ncl_asyncio: waiting for mount %p queue to drain\n", nmp));
   1459 			nmp->nm_bufqwant = TRUE;
   1460 			error = newnfs_msleep(td, &nmp->nm_bufq,
   1461 			    &ncl_iod_mutex, slpflag | PRIBIO, "nfsaio",
   1462 			   slptimeo);
   1463 			if (error) {
   1464 				error2 = newnfs_sigintr(nmp, td);
   1465 				if (error2) {
   1466 					mtx_unlock(&ncl_iod_mutex);
   1467 					return (error2);
   1468 				}
   1469 				if (slpflag == PCATCH) {
   1470 					slpflag = 0;
   1471 					slptimeo = 2 * hz;
   1472 				}
   1473 			}
   1474 			/*
   1475 			 * We might have lost our iod while sleeping,
   1476 			 * so check and loop if necessary.
   1477 			 */
   1478 			goto again;
   1479 		}
   1480 
   1481 		/* We might have lost our nfsiod */
   1482 		if (nmp->nm_bufqiods == 0) {
   1483 			NFS_DPF(ASYNCIO,
   1484 				("ncl_asyncio: no iods after mount %p queue was drained, looping\n", nmp));
   1485 			goto again;
   1486 		}
   1487 
   1488 		if (bp->b_iocmd == BIO_READ) {
   1489 			if (bp->b_rcred == NOCRED && cred != NOCRED)
   1490 				bp->b_rcred = crhold(cred);
   1491 		} else {
   1492 			if (bp->b_wcred == NOCRED && cred != NOCRED)
   1493 				bp->b_wcred = crhold(cred);
   1494 		}
   1495 
   1496 		if (bp->b_flags & B_REMFREE)
   1497 			bremfreef(bp);
   1498 		BUF_KERNPROC(bp);
   1499 		TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist);
   1500 		nmp->nm_bufqlen++;
   1501 		if ((bp->b_flags & B_DIRECT) && bp->b_iocmd == BIO_WRITE) {
   1502 			mtx_lock(&(VTONFS(bp->b_vp))->n_mtx);
   1503 			VTONFS(bp->b_vp)->n_flag |= NMODIFIED;
   1504 			VTONFS(bp->b_vp)->n_directio_asyncwr++;
   1505 			mtx_unlock(&(VTONFS(bp->b_vp))->n_mtx);
   1506 		}
   1507 		mtx_unlock(&ncl_iod_mutex);
   1508 		return (0);
   1509 	}
   1510 
   1511 	mtx_unlock(&ncl_iod_mutex);
   1512 
   1513 	/*
   1514 	 * All the iods are busy on other mounts, so return EIO to
   1515 	 * force the caller to process the i/o synchronously.
   1516 	 */
   1517 	NFS_DPF(ASYNCIO, ("ncl_asyncio: no iods available, i/o is synchronous\n"));
   1518 	return (EIO);
   1519 }
   1520 
   1521 void
   1522 ncl_doio_directwrite(struct buf *bp)
   1523 {
   1524 	int iomode, must_commit;
   1525 	struct uio *uiop = (struct uio *)bp->b_caller1;
   1526 	char *iov_base = uiop->uio_iov->iov_base;
   1527 
   1528 	iomode = NFSWRITE_FILESYNC;
   1529 	uiop->uio_td = NULL; /* NULL since we're in nfsiod */
   1530 	ncl_writerpc(bp->b_vp, uiop, bp->b_wcred, &iomode, &must_commit, 0);
   1531 	KASSERT((must_commit == 0), ("ncl_doio_directwrite: Did not commit write"));
   1532 	free(iov_base, M_NFSDIRECTIO);
   1533 	free(uiop->uio_iov, M_NFSDIRECTIO);
   1534 	free(uiop, M_NFSDIRECTIO);
   1535 	if ((bp->b_flags & B_DIRECT) && bp->b_iocmd == BIO_WRITE) {
   1536 		struct nfsnode *np = VTONFS(bp->b_vp);
   1537 		mtx_lock(&np->n_mtx);
   1538 		if (NFSHASPNFS(VFSTONFS(vnode_mount(bp->b_vp)))) {
   1539 			/*
   1540 			 * Invalidate the attribute cache, since writes to a DS
   1541 			 * won't update the size attribute.
   1542 			 */
   1543 			np->n_attrstamp = 0;
   1544 		}
   1545 		np->n_directio_asyncwr--;
   1546 		if (np->n_directio_asyncwr == 0) {
   1547 			np->n_flag &= ~NMODIFIED;
   1548 			if ((np->n_flag & NFSYNCWAIT)) {
   1549 				np->n_flag &= ~NFSYNCWAIT;
   1550 				wakeup((caddr_t)&np->n_directio_asyncwr);
   1551 			}
   1552 		}
   1553 		mtx_unlock(&np->n_mtx);
   1554 	}
   1555 	bp->b_vp = NULL;
   1556 	relpbuf(bp, &ncl_pbuf_freecnt);
   1557 }
   1558 
   1559 /*
   1560  * Do an I/O operation to/from a cache block. This may be called
   1561  * synchronously or from an nfsiod.
   1562  */
   1563 int
   1564 ncl_doio(struct vnode *vp, struct buf *bp, struct ucred *cr, struct thread *td,
   1565     int called_from_strategy)
   1566 {
   1567 	struct uio *uiop;
   1568 	struct nfsnode *np;
   1569 	struct nfsmount *nmp;
   1570 	int error = 0, iomode, must_commit = 0;
   1571 	struct uio uio;
   1572 	struct iovec io;
   1573 	struct proc *p = td ? td->td_proc : NULL;
   1574 	uint8_t	iocmd;
   1575 
   1576 	np = VTONFS(vp);
   1577 	nmp = VFSTONFS(vp->v_mount);
   1578 	uiop = &uio;
   1579 	uiop->uio_iov = &io;
   1580 	uiop->uio_iovcnt = 1;
   1581 	uiop->uio_segflg = UIO_SYSSPACE;
   1582 	uiop->uio_td = td;
   1583 
   1584 	/*
   1585 	 * clear BIO_ERROR and B_INVAL state prior to initiating the I/O.  We
   1586 	 * do this here so we do not have to do it in all the code that
   1587 	 * calls us.
   1588 	 */
   1589 	bp->b_flags &= ~B_INVAL;
   1590 	bp->b_ioflags &= ~BIO_ERROR;
   1591 
   1592 	KASSERT(!(bp->b_flags & B_DONE), ("ncl_doio: bp %p already marked done", bp));
   1593 	iocmd = bp->b_iocmd;
   1594 	if (iocmd == BIO_READ) {
   1595 	    io.iov_len = uiop->uio_resid = bp->b_bcount;
   1596 	    io.iov_base = bp->b_data;
   1597 	    uiop->uio_rw = UIO_READ;
   1598 
   1599 	    switch (vp->v_type) {
   1600 	    case VREG:
   1601 		uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
   1602 		NFSINCRGLOBAL(nfsstatsv1.read_bios);
   1603 		error = ncl_readrpc(vp, uiop, cr);
   1604 
   1605 		if (!error) {
   1606 		    if (uiop->uio_resid) {
   1607 			/*
   1608 			 * If we had a short read with no error, we must have
   1609 			 * hit a file hole.  We should zero-fill the remainder.
   1610 			 * This can also occur if the server hits the file EOF.
   1611 			 *
   1612 			 * Holes used to be able to occur due to pending
   1613 			 * writes, but that is not possible any longer.
   1614 			 */
   1615 			int nread = bp->b_bcount - uiop->uio_resid;
   1616 			ssize_t left = uiop->uio_resid;
   1617 
   1618 			if (left > 0)
   1619 				bzero((char *)bp->b_data + nread, left);
   1620 			uiop->uio_resid = 0;
   1621 		    }
   1622 		}
   1623 		/* ASSERT_VOP_LOCKED(vp, "ncl_doio"); */
   1624 		if (p && (vp->v_vflag & VV_TEXT)) {
   1625 			mtx_lock(&np->n_mtx);
   1626 			if (NFS_TIMESPEC_COMPARE(&np->n_mtime, &np->n_vattr.na_mtime)) {
   1627 				mtx_unlock(&np->n_mtx);
   1628 				PROC_LOCK(p);
   1629 				killproc(p, "text file modification");
   1630 				PROC_UNLOCK(p);
   1631 			} else
   1632 				mtx_unlock(&np->n_mtx);
   1633 		}
   1634 		break;
   1635 	    case VLNK:
   1636 		uiop->uio_offset = (off_t)0;
   1637 		NFSINCRGLOBAL(nfsstatsv1.readlink_bios);
   1638 		error = ncl_readlinkrpc(vp, uiop, cr);
   1639 		break;
   1640 	    case VDIR:
   1641 		NFSINCRGLOBAL(nfsstatsv1.readdir_bios);
   1642 		uiop->uio_offset = ((u_quad_t)bp->b_lblkno) * NFS_DIRBLKSIZ;
   1643 		if ((nmp->nm_flag & NFSMNT_RDIRPLUS) != 0) {
   1644 			error = ncl_readdirplusrpc(vp, uiop, cr, td);
   1645 			if (error == NFSERR_NOTSUPP)
   1646 				nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
   1647 		}
   1648 		if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
   1649 			error = ncl_readdirrpc(vp, uiop, cr, td);
   1650 		/*
   1651 		 * end-of-directory sets B_INVAL but does not generate an
   1652 		 * error.
   1653 		 */
   1654 		if (error == 0 && uiop->uio_resid == bp->b_bcount)
   1655 			bp->b_flags |= B_INVAL;
   1656 		break;
   1657 	    default:
   1658 		printf("ncl_doio:  type %x unexpected\n", vp->v_type);
   1659 		break;
   1660 	    }
   1661 	    if (error) {
   1662 		bp->b_ioflags |= BIO_ERROR;
   1663 		bp->b_error = error;
   1664 	    }
   1665 	} else {
   1666 	    /*
   1667 	     * If we only need to commit, try to commit
   1668 	     */
   1669 	    if (bp->b_flags & B_NEEDCOMMIT) {
   1670 		    int retv;
   1671 		    off_t off;
   1672 
   1673 		    off = ((u_quad_t)bp->b_blkno) * DEV_BSIZE + bp->b_dirtyoff;
   1674 		    retv = ncl_commit(vp, off, bp->b_dirtyend-bp->b_dirtyoff,
   1675 			bp->b_wcred, td);
   1676 		    if (retv == 0) {
   1677 			    bp->b_dirtyoff = bp->b_dirtyend = 0;
   1678 			    bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
   1679 			    bp->b_resid = 0;
   1680 			    bufdone(bp);
   1681 			    return (0);
   1682 		    }
   1683 		    if (retv == NFSERR_STALEWRITEVERF) {
   1684 			    ncl_clearcommit(vp->v_mount);
   1685 		    }
   1686 	    }
   1687 
   1688 	    /*
   1689 	     * Setup for actual write
   1690 	     */
   1691 	    mtx_lock(&np->n_mtx);
   1692 	    if ((off_t)bp->b_blkno * DEV_BSIZE + bp->b_dirtyend > np->n_size)
   1693 		bp->b_dirtyend = np->n_size - (off_t)bp->b_blkno * DEV_BSIZE;
   1694 	    mtx_unlock(&np->n_mtx);
   1695 
   1696 	    if (bp->b_dirtyend > bp->b_dirtyoff) {
   1697 		io.iov_len = uiop->uio_resid = bp->b_dirtyend
   1698 		    - bp->b_dirtyoff;
   1699 		uiop->uio_offset = (off_t)bp->b_blkno * DEV_BSIZE
   1700 		    + bp->b_dirtyoff;
   1701 		io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
   1702 		uiop->uio_rw = UIO_WRITE;
   1703 		NFSINCRGLOBAL(nfsstatsv1.write_bios);
   1704 
   1705 		if ((bp->b_flags & (B_ASYNC | B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == B_ASYNC)
   1706 		    iomode = NFSWRITE_UNSTABLE;
   1707 		else
   1708 		    iomode = NFSWRITE_FILESYNC;
   1709 
   1710 		error = ncl_writerpc(vp, uiop, cr, &iomode, &must_commit,
   1711 		    called_from_strategy);
   1712 
   1713 		/*
   1714 		 * When setting B_NEEDCOMMIT also set B_CLUSTEROK to try
   1715 		 * to cluster the buffers needing commit.  This will allow
   1716 		 * the system to submit a single commit rpc for the whole
   1717 		 * cluster.  We can do this even if the buffer is not 100%
   1718 		 * dirty (relative to the NFS blocksize), so we optimize the
   1719 		 * append-to-file-case.
   1720 		 *
   1721 		 * (when clearing B_NEEDCOMMIT, B_CLUSTEROK must also be
   1722 		 * cleared because write clustering only works for commit
   1723 		 * rpc's, not for the data portion of the write).
   1724 		 */
   1725 
   1726 		if (!error && iomode == NFSWRITE_UNSTABLE) {
   1727 		    bp->b_flags |= B_NEEDCOMMIT;
   1728 		    if (bp->b_dirtyoff == 0
   1729 			&& bp->b_dirtyend == bp->b_bcount)
   1730 			bp->b_flags |= B_CLUSTEROK;
   1731 		} else {
   1732 		    bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
   1733 		}
   1734 
   1735 		/*
   1736 		 * For an interrupted write, the buffer is still valid
   1737 		 * and the write hasn't been pushed to the server yet,
   1738 		 * so we can't set BIO_ERROR and report the interruption
   1739 		 * by setting B_EINTR. For the B_ASYNC case, B_EINTR
   1740 		 * is not relevant, so the rpc attempt is essentially
   1741 		 * a noop.  For the case of a V3 write rpc not being
   1742 		 * committed to stable storage, the block is still
   1743 		 * dirty and requires either a commit rpc or another
   1744 		 * write rpc with iomode == NFSV3WRITE_FILESYNC before
   1745 		 * the block is reused. This is indicated by setting
   1746 		 * the B_DELWRI and B_NEEDCOMMIT flags.
   1747 		 *
   1748 		 * EIO is returned by ncl_writerpc() to indicate a recoverable
   1749 		 * write error and is handled as above, except that
   1750 		 * B_EINTR isn't set. One cause of this is a stale stateid
   1751 		 * error for the RPC that indicates recovery is required,
   1752 		 * when called with called_from_strategy != 0.
   1753 		 *
   1754 		 * If the buffer is marked B_PAGING, it does not reside on
   1755 		 * the vp's paging queues so we cannot call bdirty().  The
   1756 		 * bp in this case is not an NFS cache block so we should
   1757 		 * be safe. XXX
   1758 		 *
   1759 		 * The logic below breaks up errors into recoverable and
   1760 		 * unrecoverable. For the former, we clear B_INVAL|B_NOCACHE
   1761 		 * and keep the buffer around for potential write retries.
   1762 		 * For the latter (eg ESTALE), we toss the buffer away (B_INVAL)
   1763 		 * and save the error in the nfsnode. This is less than ideal
   1764 		 * but necessary. Keeping such buffers around could potentially
   1765 		 * cause buffer exhaustion eventually (they can never be written
   1766 		 * out, so will get constantly be re-dirtied). It also causes
   1767 		 * all sorts of vfs panics. For non-recoverable write errors,
   1768 		 * also invalidate the attrcache, so we'll be forced to go over
   1769 		 * the wire for this object, returning an error to user on next
   1770 		 * call (most of the time).
   1771 		 */
   1772 		if (error == EINTR || error == EIO || error == ETIMEDOUT
   1773 		    || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
   1774 			int s;
   1775 
   1776 			s = splbio();
   1777 			bp->b_flags &= ~(B_INVAL|B_NOCACHE);
   1778 			if ((bp->b_flags & B_PAGING) == 0) {
   1779 			    bdirty(bp);
   1780 			    bp->b_flags &= ~B_DONE;
   1781 			}
   1782 			if ((error == EINTR || error == ETIMEDOUT) &&
   1783 			    (bp->b_flags & B_ASYNC) == 0)
   1784 			    bp->b_flags |= B_EINTR;
   1785 			splx(s);
   1786 		} else {
   1787 		    if (error) {
   1788 			bp->b_ioflags |= BIO_ERROR;
   1789 			bp->b_flags |= B_INVAL;
   1790 			bp->b_error = np->n_error = error;
   1791 			mtx_lock(&np->n_mtx);
   1792 			np->n_flag |= NWRITEERR;
   1793 			np->n_attrstamp = 0;
   1794 			KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
   1795 			mtx_unlock(&np->n_mtx);
   1796 		    }
   1797 		    bp->b_dirtyoff = bp->b_dirtyend = 0;
   1798 		}
   1799 	    } else {
   1800 		bp->b_resid = 0;
   1801 		bufdone(bp);
   1802 		return (0);
   1803 	    }
   1804 	}
   1805 	bp->b_resid = uiop->uio_resid;
   1806 	if (must_commit)
   1807 	    ncl_clearcommit(vp->v_mount);
   1808 	bufdone(bp);
   1809 	return (error);
   1810 }
   1811 
   1812 /*
   1813  * Used to aid in handling ftruncate() operations on the NFS client side.
   1814  * Truncation creates a number of special problems for NFS.  We have to
   1815  * throw away VM pages and buffer cache buffers that are beyond EOF, and
   1816  * we have to properly handle VM pages or (potentially dirty) buffers
   1817  * that straddle the truncation point.
   1818  */
   1819 
   1820 int
   1821 ncl_meta_setsize(struct vnode *vp, struct ucred *cred, struct thread *td, u_quad_t nsize)
   1822 {
   1823 	struct nfsnode *np = VTONFS(vp);
   1824 	u_quad_t tsize;
   1825 	int biosize = vp->v_bufobj.bo_bsize;
   1826 	int error = 0;
   1827 
   1828 	mtx_lock(&np->n_mtx);
   1829 	tsize = np->n_size;
   1830 	np->n_size = nsize;
   1831 	mtx_unlock(&np->n_mtx);
   1832 
   1833 	if (nsize < tsize) {
   1834 		struct buf *bp;
   1835 		daddr_t lbn;
   1836 		int bufsize;
   1837 
   1838 		/*
   1839 		 * vtruncbuf() doesn't get the buffer overlapping the
   1840 		 * truncation point.  We may have a B_DELWRI and/or B_CACHE
   1841 		 * buffer that now needs to be truncated.
   1842 		 */
   1843 		error = vtruncbuf(vp, cred, nsize, biosize);
   1844 		lbn = nsize / biosize;
   1845 		bufsize = nsize - (lbn * biosize);
   1846 		bp = nfs_getcacheblk(vp, lbn, bufsize, td);
   1847 		if (!bp)
   1848 			return EINTR;
   1849 		if (bp->b_dirtyoff > bp->b_bcount)
   1850 			bp->b_dirtyoff = bp->b_bcount;
   1851 		if (bp->b_dirtyend > bp->b_bcount)
   1852 			bp->b_dirtyend = bp->b_bcount;
   1853 		bp->b_flags |= B_RELBUF;  /* don't leave garbage around */
   1854 		brelse(bp);
   1855 	} else {
   1856 		vnode_pager_setsize(vp, nsize);
   1857 	}
   1858 	return(error);
   1859 }
   1860 
   1861