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