nfs_bio.c revision 31617
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 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 *	@(#)nfs_bio.c	8.9 (Berkeley) 3/30/95
37 * $Id: nfs_bio.c,v 1.44 1997/09/10 19:52:25 phk Exp $
38 */
39
40
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/resourcevar.h>
44#include <sys/signalvar.h>
45#include <sys/proc.h>
46#include <sys/buf.h>
47#include <sys/vnode.h>
48#include <sys/mount.h>
49#include <sys/kernel.h>
50
51#include <vm/vm.h>
52#include <vm/vm_extern.h>
53#include <vm/vm_prot.h>
54#include <vm/vm_page.h>
55#include <vm/vm_object.h>
56#include <vm/vm_pager.h>
57#include <vm/vnode_pager.h>
58
59#include <nfs/rpcv2.h>
60#include <nfs/nfsproto.h>
61#include <nfs/nfs.h>
62#include <nfs/nfsmount.h>
63#include <nfs/nqnfs.h>
64#include <nfs/nfsnode.h>
65
66static struct buf *nfs_getcacheblk __P((struct vnode *vp, daddr_t bn, int size,
67					struct proc *p));
68
69extern int nfs_numasync;
70extern struct nfsstats nfsstats;
71
72/*
73 * Vnode op for VM getpages.
74 */
75int
76nfs_getpages(ap)
77	struct vop_getpages_args *ap;
78{
79	int i, bsize;
80	vm_object_t obj;
81	int pcount;
82	struct uio auio;
83	struct iovec aiov;
84	int error;
85	vm_page_t m;
86
87	if (!(ap->a_vp->v_flag & VVMIO)) {
88		printf("nfs_getpages: called with non-VMIO vnode??\n");
89		return EOPNOTSUPP;
90	}
91
92	pcount = round_page(ap->a_count) / PAGE_SIZE;
93
94	obj = ap->a_m[ap->a_reqpage]->object;
95	bsize = ap->a_vp->v_mount->mnt_stat.f_iosize;
96
97	for (i = 0; i < pcount; i++) {
98		if (i != ap->a_reqpage) {
99			vnode_pager_freepage(ap->a_m[i]);
100		}
101	}
102	m = ap->a_m[ap->a_reqpage];
103
104	m->busy++;
105	m->flags &= ~PG_BUSY;
106
107	auio.uio_iov = &aiov;
108	auio.uio_iovcnt = 1;
109	aiov.iov_base = 0;
110	aiov.iov_len = PAGE_SIZE;
111	auio.uio_resid = PAGE_SIZE;
112	auio.uio_offset = IDX_TO_OFF(m->pindex);
113	auio.uio_segflg = UIO_NOCOPY;
114	auio.uio_rw = UIO_READ;
115	auio.uio_procp = curproc;
116	error = nfs_bioread(ap->a_vp, &auio, IO_NODELOCKED, curproc->p_ucred, 1);
117
118	m->flags |= PG_BUSY;
119	m->busy--;
120
121	if (error && (auio.uio_resid == PAGE_SIZE))
122		return VM_PAGER_ERROR;
123	return 0;
124}
125
126/*
127 * Vnode op for read using bio
128 * Any similarity to readip() is purely coincidental
129 */
130int
131nfs_bioread(vp, uio, ioflag, cred, getpages)
132	register struct vnode *vp;
133	register struct uio *uio;
134	int ioflag;
135	struct ucred *cred;
136	int getpages;
137{
138	register struct nfsnode *np = VTONFS(vp);
139	register int biosize, diff, i;
140	struct buf *bp = 0, *rabp;
141	struct vattr vattr;
142	struct proc *p;
143	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
144	daddr_t lbn, rabn;
145	int bufsize;
146	int nra, error = 0, n = 0, on = 0, not_readin;
147
148#ifdef DIAGNOSTIC
149	if (uio->uio_rw != UIO_READ)
150		panic("nfs_read mode");
151#endif
152	if (uio->uio_resid == 0)
153		return (0);
154	if (uio->uio_offset < 0)
155		return (EINVAL);
156	p = uio->uio_procp;
157	if ((nmp->nm_flag & (NFSMNT_NFSV3 | NFSMNT_GOTFSINFO)) == NFSMNT_NFSV3)
158		(void)nfs_fsinfo(nmp, vp, cred, p);
159	biosize = vp->v_mount->mnt_stat.f_iosize;
160	/*
161	 * For nfs, cache consistency can only be maintained approximately.
162	 * Although RFC1094 does not specify the criteria, the following is
163	 * believed to be compatible with the reference port.
164	 * For nqnfs, full cache consistency is maintained within the loop.
165	 * For nfs:
166	 * If the file's modify time on the server has changed since the
167	 * last read rpc or you have written to the file,
168	 * you may have lost data cache consistency with the
169	 * server, so flush all of the file's data out of the cache.
170	 * Then force a getattr rpc to ensure that you have up to date
171	 * attributes.
172	 * NB: This implies that cache data can be read when up to
173	 * NFS_ATTRTIMEO seconds out of date. If you find that you need current
174	 * attributes this could be forced by setting n_attrstamp to 0 before
175	 * the VOP_GETATTR() call.
176	 */
177	if ((nmp->nm_flag & NFSMNT_NQNFS) == 0) {
178		if (np->n_flag & NMODIFIED) {
179			if (vp->v_type != VREG) {
180				if (vp->v_type != VDIR)
181					panic("nfs: bioread, not dir");
182				nfs_invaldir(vp);
183				error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
184				if (error)
185					return (error);
186			}
187			np->n_attrstamp = 0;
188			error = VOP_GETATTR(vp, &vattr, cred, p);
189			if (error)
190				return (error);
191			np->n_mtime = vattr.va_mtime.tv_sec;
192		} else {
193			error = VOP_GETATTR(vp, &vattr, cred, p);
194			if (error)
195				return (error);
196			if (np->n_mtime != vattr.va_mtime.tv_sec) {
197				if (vp->v_type == VDIR)
198					nfs_invaldir(vp);
199				error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
200				if (error)
201					return (error);
202				np->n_mtime = vattr.va_mtime.tv_sec;
203			}
204		}
205	}
206	do {
207
208	    /*
209	     * Get a valid lease. If cached data is stale, flush it.
210	     */
211	    if (nmp->nm_flag & NFSMNT_NQNFS) {
212		if (NQNFS_CKINVALID(vp, np, ND_READ)) {
213		    do {
214			error = nqnfs_getlease(vp, ND_READ, cred, p);
215		    } while (error == NQNFS_EXPIRED);
216		    if (error)
217			return (error);
218		    if (np->n_lrev != np->n_brev ||
219			(np->n_flag & NQNFSNONCACHE) ||
220			((np->n_flag & NMODIFIED) && vp->v_type == VDIR)) {
221			if (vp->v_type == VDIR)
222			    nfs_invaldir(vp);
223			error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
224			if (error)
225			    return (error);
226			np->n_brev = np->n_lrev;
227		    }
228		} else if (vp->v_type == VDIR && (np->n_flag & NMODIFIED)) {
229		    nfs_invaldir(vp);
230		    error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
231		    if (error)
232			return (error);
233		}
234	    }
235	    if (np->n_flag & NQNFSNONCACHE) {
236		switch (vp->v_type) {
237		case VREG:
238			return (nfs_readrpc(vp, uio, cred));
239		case VLNK:
240			return (nfs_readlinkrpc(vp, uio, cred));
241		case VDIR:
242			break;
243		default:
244			printf(" NQNFSNONCACHE: type %x unexpected\n",
245				vp->v_type);
246		};
247	    }
248	    switch (vp->v_type) {
249	    case VREG:
250		nfsstats.biocache_reads++;
251		lbn = uio->uio_offset / biosize;
252		on = uio->uio_offset & (biosize - 1);
253		not_readin = 1;
254
255		/*
256		 * Start the read ahead(s), as required.
257		 */
258		if (nfs_numasync > 0 && nmp->nm_readahead > 0) {
259		    for (nra = 0; nra < nmp->nm_readahead &&
260			(off_t)(lbn + 1 + nra) * biosize < np->n_size; nra++) {
261			rabn = lbn + 1 + nra;
262			if (!incore(vp, rabn)) {
263			    rabp = nfs_getcacheblk(vp, rabn, biosize, p);
264			    if (!rabp)
265				return (EINTR);
266			    if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
267				rabp->b_flags |= (B_READ | B_ASYNC);
268				vfs_busy_pages(rabp, 0);
269				if (nfs_asyncio(rabp, cred)) {
270				    rabp->b_flags |= B_INVAL|B_ERROR;
271				    vfs_unbusy_pages(rabp);
272				    brelse(rabp);
273				}
274			    } else
275				brelse(rabp);
276			}
277		    }
278		}
279
280		/*
281		 * If the block is in the cache and has the required data
282		 * in a valid region, just copy it out.
283		 * Otherwise, get the block and write back/read in,
284		 * as required.
285		 */
286again:
287		bufsize = biosize;
288		if ((off_t)(lbn + 1) * biosize > np->n_size &&
289		    (off_t)(lbn + 1) * biosize - np->n_size < biosize) {
290			bufsize = np->n_size - lbn * biosize;
291			bufsize = (bufsize + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
292		}
293		bp = nfs_getcacheblk(vp, lbn, bufsize, p);
294		if (!bp)
295			return (EINTR);
296		/*
297		 * If we are being called from nfs_getpages, we must
298		 * make sure the buffer is a vmio buffer.  The vp will
299		 * already be setup for vmio but there may be some old
300		 * non-vmio buffers attached to it.
301		 */
302		if (getpages && !(bp->b_flags & B_VMIO)) {
303#ifdef DIAGNOSTIC
304			printf("nfs_bioread: non vmio buf found, discarding\n");
305#endif
306			bp->b_flags |= B_NOCACHE;
307			bp->b_flags |= B_INVAFTERWRITE;
308			if (bp->b_dirtyend > 0) {
309				if ((bp->b_flags & B_DELWRI) == 0)
310					panic("nfsbioread");
311				if (VOP_BWRITE(bp) == EINTR)
312					return (EINTR);
313			} else
314				brelse(bp);
315			goto again;
316		}
317		if ((bp->b_flags & B_CACHE) == 0) {
318			bp->b_flags |= B_READ;
319			bp->b_flags &= ~(B_DONE | B_ERROR | B_INVAL);
320			not_readin = 0;
321			vfs_busy_pages(bp, 0);
322			error = nfs_doio(bp, cred, p);
323			if (error) {
324			    brelse(bp);
325			    return (error);
326			}
327		}
328		if (bufsize > on) {
329			n = min((unsigned)(bufsize - on), uio->uio_resid);
330		} else {
331			n = 0;
332		}
333		diff = np->n_size - uio->uio_offset;
334		if (diff < n)
335			n = diff;
336		if (not_readin && n > 0) {
337			if (on < bp->b_validoff || (on + n) > bp->b_validend) {
338				bp->b_flags |= B_NOCACHE;
339				bp->b_flags |= B_INVAFTERWRITE;
340				if (bp->b_dirtyend > 0) {
341				    if ((bp->b_flags & B_DELWRI) == 0)
342					panic("nfsbioread");
343				    if (VOP_BWRITE(bp) == EINTR)
344					return (EINTR);
345				} else
346				    brelse(bp);
347				goto again;
348			}
349		}
350		vp->v_lastr = lbn;
351		diff = (on >= bp->b_validend) ? 0 : (bp->b_validend - on);
352		if (diff < n)
353			n = diff;
354		break;
355	    case VLNK:
356		nfsstats.biocache_readlinks++;
357		bp = nfs_getcacheblk(vp, (daddr_t)0, NFS_MAXPATHLEN, p);
358		if (!bp)
359			return (EINTR);
360		if ((bp->b_flags & B_CACHE) == 0) {
361			bp->b_flags |= B_READ;
362			vfs_busy_pages(bp, 0);
363			error = nfs_doio(bp, cred, p);
364			if (error) {
365				bp->b_flags |= B_ERROR;
366				brelse(bp);
367				return (error);
368			}
369		}
370		n = min(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid);
371		on = 0;
372		break;
373	    case VDIR:
374		nfsstats.biocache_readdirs++;
375		if (np->n_direofoffset
376		    && uio->uio_offset >= np->n_direofoffset) {
377		    return (0);
378		}
379		lbn = uio->uio_offset / NFS_DIRBLKSIZ;
380		on = uio->uio_offset & (NFS_DIRBLKSIZ - 1);
381		bp = nfs_getcacheblk(vp, lbn, NFS_DIRBLKSIZ, p);
382		if (!bp)
383		    return (EINTR);
384		if ((bp->b_flags & B_CACHE) == 0) {
385		    bp->b_flags |= B_READ;
386		    vfs_busy_pages(bp, 0);
387		    error = nfs_doio(bp, cred, p);
388		    if (error) {
389		        vfs_unbusy_pages(bp);
390			brelse(bp);
391			while (error == NFSERR_BAD_COOKIE) {
392			    nfs_invaldir(vp);
393			    error = nfs_vinvalbuf(vp, 0, cred, p, 1);
394			    /*
395			     * Yuck! The directory has been modified on the
396			     * server. The only way to get the block is by
397			     * reading from the beginning to get all the
398			     * offset cookies.
399			     */
400			    for (i = 0; i <= lbn && !error; i++) {
401				if (np->n_direofoffset
402				    && (i * NFS_DIRBLKSIZ) >= np->n_direofoffset)
403				    return (0);
404				bp = nfs_getcacheblk(vp, i, NFS_DIRBLKSIZ, p);
405				if (!bp)
406				    return (EINTR);
407				if ((bp->b_flags & B_DONE) == 0) {
408				    bp->b_flags |= B_READ;
409				    vfs_busy_pages(bp, 0);
410				    error = nfs_doio(bp, cred, p);
411				    if (error) {
412					vfs_unbusy_pages(bp);
413					brelse(bp);
414				    } else if (i < lbn)
415					brelse(bp);
416				}
417			    }
418			}
419			if (error)
420			    return (error);
421		    }
422		}
423
424		/*
425		 * If not eof and read aheads are enabled, start one.
426		 * (You need the current block first, so that you have the
427		 *  directory offset cookie of the next block.)
428		 */
429		if (nfs_numasync > 0 && nmp->nm_readahead > 0 &&
430		    (np->n_direofoffset == 0 ||
431		    (lbn + 1) * NFS_DIRBLKSIZ < np->n_direofoffset) &&
432		    !(np->n_flag & NQNFSNONCACHE) &&
433		    !incore(vp, lbn + 1)) {
434			rabp = nfs_getcacheblk(vp, lbn + 1, NFS_DIRBLKSIZ, p);
435			if (rabp) {
436			    if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
437				rabp->b_flags |= (B_READ | B_ASYNC);
438				vfs_busy_pages(rabp, 0);
439				if (nfs_asyncio(rabp, cred)) {
440				    rabp->b_flags |= B_INVAL|B_ERROR;
441				    vfs_unbusy_pages(rabp);
442				    brelse(rabp);
443				}
444			    } else {
445				brelse(rabp);
446			    }
447			}
448		}
449		/*
450		 * Make sure we use a signed variant of min() since
451		 * the second term may be negative.
452		 */
453		n = lmin(uio->uio_resid, NFS_DIRBLKSIZ - bp->b_resid - on);
454		break;
455	    default:
456		printf(" nfs_bioread: type %x unexpected\n",vp->v_type);
457		break;
458	    };
459
460	    if (n > 0) {
461		error = uiomove(bp->b_data + on, (int)n, uio);
462	    }
463	    switch (vp->v_type) {
464	    case VREG:
465		break;
466	    case VLNK:
467		n = 0;
468		break;
469	    case VDIR:
470		if (np->n_flag & NQNFSNONCACHE)
471			bp->b_flags |= B_INVAL;
472		break;
473	    default:
474		printf(" nfs_bioread: type %x unexpected\n",vp->v_type);
475	    }
476 	    brelse(bp);
477	} while (error == 0 && uio->uio_resid > 0 && n > 0);
478	return (error);
479}
480
481/*
482 * Vnode op for write using bio
483 */
484int
485nfs_write(ap)
486	struct vop_write_args /* {
487		struct vnode *a_vp;
488		struct uio *a_uio;
489		int  a_ioflag;
490		struct ucred *a_cred;
491	} */ *ap;
492{
493	register int biosize;
494	register struct uio *uio = ap->a_uio;
495	struct proc *p = uio->uio_procp;
496	register struct vnode *vp = ap->a_vp;
497	struct nfsnode *np = VTONFS(vp);
498	register struct ucred *cred = ap->a_cred;
499	int ioflag = ap->a_ioflag;
500	struct buf *bp;
501	struct vattr vattr;
502	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
503	daddr_t lbn;
504	int bufsize;
505	int n, on, error = 0, iomode, must_commit;
506
507#ifdef DIAGNOSTIC
508	if (uio->uio_rw != UIO_WRITE)
509		panic("nfs_write mode");
510	if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
511		panic("nfs_write proc");
512#endif
513	if (vp->v_type != VREG)
514		return (EIO);
515	if (np->n_flag & NWRITEERR) {
516		np->n_flag &= ~NWRITEERR;
517		return (np->n_error);
518	}
519	if ((nmp->nm_flag & (NFSMNT_NFSV3 | NFSMNT_GOTFSINFO)) == NFSMNT_NFSV3)
520		(void)nfs_fsinfo(nmp, vp, cred, p);
521	if (ioflag & (IO_APPEND | IO_SYNC)) {
522		if (np->n_flag & NMODIFIED) {
523			np->n_attrstamp = 0;
524			error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
525			if (error)
526				return (error);
527		}
528		if (ioflag & IO_APPEND) {
529			np->n_attrstamp = 0;
530			error = VOP_GETATTR(vp, &vattr, cred, p);
531			if (error)
532				return (error);
533			uio->uio_offset = np->n_size;
534		}
535	}
536	if (uio->uio_offset < 0)
537		return (EINVAL);
538	if (uio->uio_resid == 0)
539		return (0);
540	/*
541	 * Maybe this should be above the vnode op call, but so long as
542	 * file servers have no limits, i don't think it matters
543	 */
544	if (p && uio->uio_offset + uio->uio_resid >
545	      p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
546		psignal(p, SIGXFSZ);
547		return (EFBIG);
548	}
549	/*
550	 * I use nm_rsize, not nm_wsize so that all buffer cache blocks
551	 * will be the same size within a filesystem. nfs_writerpc will
552	 * still use nm_wsize when sizing the rpc's.
553	 */
554	biosize = vp->v_mount->mnt_stat.f_iosize;
555	do {
556		/*
557		 * Check for a valid write lease.
558		 */
559		if ((nmp->nm_flag & NFSMNT_NQNFS) &&
560		    NQNFS_CKINVALID(vp, np, ND_WRITE)) {
561			do {
562				error = nqnfs_getlease(vp, ND_WRITE, cred, p);
563			} while (error == NQNFS_EXPIRED);
564			if (error)
565				return (error);
566			if (np->n_lrev != np->n_brev ||
567			    (np->n_flag & NQNFSNONCACHE)) {
568				error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
569				if (error)
570					return (error);
571				np->n_brev = np->n_lrev;
572			}
573		}
574		if ((np->n_flag & NQNFSNONCACHE) && uio->uio_iovcnt == 1) {
575		    iomode = NFSV3WRITE_FILESYNC;
576		    error = nfs_writerpc(vp, uio, cred, &iomode, &must_commit);
577		    if (must_commit)
578			nfs_clearcommit(vp->v_mount);
579		    return (error);
580		}
581		nfsstats.biocache_writes++;
582		lbn = uio->uio_offset / biosize;
583		on = uio->uio_offset & (biosize-1);
584		n = min((unsigned)(biosize - on), uio->uio_resid);
585again:
586		if (uio->uio_offset + n > np->n_size) {
587			np->n_size = uio->uio_offset + n;
588			np->n_flag |= NMODIFIED;
589			vnode_pager_setsize(vp, (u_long)np->n_size);
590		}
591		bufsize = biosize;
592		if ((lbn + 1) * biosize > np->n_size) {
593			bufsize = np->n_size - lbn * biosize;
594			bufsize = (bufsize + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
595		}
596		bp = nfs_getcacheblk(vp, lbn, bufsize, p);
597		if (!bp)
598			return (EINTR);
599		if (bp->b_wcred == NOCRED) {
600			crhold(cred);
601			bp->b_wcred = cred;
602		}
603		np->n_flag |= NMODIFIED;
604
605		if ((bp->b_blkno * DEV_BSIZE) + bp->b_dirtyend > np->n_size) {
606			bp->b_dirtyend = np->n_size - (bp->b_blkno * DEV_BSIZE);
607		}
608
609		/*
610		 * If the new write will leave a contiguous dirty
611		 * area, just update the b_dirtyoff and b_dirtyend,
612		 * otherwise force a write rpc of the old dirty area.
613		 */
614		if (bp->b_dirtyend > 0 &&
615		    (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) {
616			bp->b_proc = p;
617			if (VOP_BWRITE(bp) == EINTR)
618				return (EINTR);
619			goto again;
620		}
621
622		/*
623		 * Check for valid write lease and get one as required.
624		 * In case getblk() and/or bwrite() delayed us.
625		 */
626		if ((nmp->nm_flag & NFSMNT_NQNFS) &&
627		    NQNFS_CKINVALID(vp, np, ND_WRITE)) {
628			do {
629				error = nqnfs_getlease(vp, ND_WRITE, cred, p);
630			} while (error == NQNFS_EXPIRED);
631			if (error) {
632				brelse(bp);
633				return (error);
634			}
635			if (np->n_lrev != np->n_brev ||
636			    (np->n_flag & NQNFSNONCACHE)) {
637				brelse(bp);
638				error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
639				if (error)
640					return (error);
641				np->n_brev = np->n_lrev;
642				goto again;
643			}
644		}
645		error = uiomove((char *)bp->b_data + on, n, uio);
646		if (error) {
647			bp->b_flags |= B_ERROR;
648			brelse(bp);
649			return (error);
650		}
651		if (bp->b_dirtyend > 0) {
652			bp->b_dirtyoff = min(on, bp->b_dirtyoff);
653			bp->b_dirtyend = max((on + n), bp->b_dirtyend);
654		} else {
655			bp->b_dirtyoff = on;
656			bp->b_dirtyend = on + n;
657		}
658		if (bp->b_validend == 0 || bp->b_validend < bp->b_dirtyoff ||
659		    bp->b_validoff > bp->b_dirtyend) {
660			bp->b_validoff = bp->b_dirtyoff;
661			bp->b_validend = bp->b_dirtyend;
662		} else {
663			bp->b_validoff = min(bp->b_validoff, bp->b_dirtyoff);
664			bp->b_validend = max(bp->b_validend, bp->b_dirtyend);
665		}
666
667		/*
668		 * Since this block is being modified, it must be written
669		 * again and not just committed.
670		 */
671		bp->b_flags &= ~B_NEEDCOMMIT;
672
673		/*
674		 * If the lease is non-cachable or IO_SYNC do bwrite().
675		 */
676		if ((np->n_flag & NQNFSNONCACHE) || (ioflag & IO_SYNC)) {
677			bp->b_proc = p;
678			error = VOP_BWRITE(bp);
679			if (error)
680				return (error);
681			if (np->n_flag & NQNFSNONCACHE) {
682				error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
683				if (error)
684					return (error);
685			}
686		} else if ((n + on) == biosize &&
687			(nmp->nm_flag & NFSMNT_NQNFS) == 0) {
688			bp->b_proc = (struct proc *)0;
689			bp->b_flags |= B_ASYNC;
690			(void)nfs_writebp(bp, 0);
691		} else
692			bdwrite(bp);
693	} while (uio->uio_resid > 0 && n > 0);
694	return (0);
695}
696
697/*
698 * Get an nfs cache block.
699 * Allocate a new one if the block isn't currently in the cache
700 * and return the block marked busy. If the calling process is
701 * interrupted by a signal for an interruptible mount point, return
702 * NULL.
703 */
704static struct buf *
705nfs_getcacheblk(vp, bn, size, p)
706	struct vnode *vp;
707	daddr_t bn;
708	int size;
709	struct proc *p;
710{
711	register struct buf *bp;
712	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
713	int biosize = vp->v_mount->mnt_stat.f_iosize;
714
715	if (nmp->nm_flag & NFSMNT_INT) {
716		bp = getblk(vp, bn, size, PCATCH, 0);
717		while (bp == (struct buf *)0) {
718			if (nfs_sigintr(nmp, (struct nfsreq *)0, p))
719				return ((struct buf *)0);
720			bp = getblk(vp, bn, size, 0, 2 * hz);
721		}
722	} else
723		bp = getblk(vp, bn, size, 0, 0);
724
725	if( vp->v_type == VREG)
726		bp->b_blkno = (bn * biosize) / DEV_BSIZE;
727
728	return (bp);
729}
730
731/*
732 * Flush and invalidate all dirty buffers. If another process is already
733 * doing the flush, just wait for completion.
734 */
735int
736nfs_vinvalbuf(vp, flags, cred, p, intrflg)
737	struct vnode *vp;
738	int flags;
739	struct ucred *cred;
740	struct proc *p;
741	int intrflg;
742{
743	register struct nfsnode *np = VTONFS(vp);
744	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
745	int error = 0, slpflag, slptimeo;
746
747	if ((nmp->nm_flag & NFSMNT_INT) == 0)
748		intrflg = 0;
749	if (intrflg) {
750		slpflag = PCATCH;
751		slptimeo = 2 * hz;
752	} else {
753		slpflag = 0;
754		slptimeo = 0;
755	}
756	/*
757	 * First wait for any other process doing a flush to complete.
758	 */
759	while (np->n_flag & NFLUSHINPROG) {
760		np->n_flag |= NFLUSHWANT;
761		error = tsleep((caddr_t)&np->n_flag, PRIBIO + 2, "nfsvinval",
762			slptimeo);
763		if (error && intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, p))
764			return (EINTR);
765	}
766
767	/*
768	 * Now, flush as required.
769	 */
770	np->n_flag |= NFLUSHINPROG;
771	error = vinvalbuf(vp, flags, cred, p, slpflag, 0);
772	while (error) {
773		if (intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, p)) {
774			np->n_flag &= ~NFLUSHINPROG;
775			if (np->n_flag & NFLUSHWANT) {
776				np->n_flag &= ~NFLUSHWANT;
777				wakeup((caddr_t)&np->n_flag);
778			}
779			return (EINTR);
780		}
781		error = vinvalbuf(vp, flags, cred, p, 0, slptimeo);
782	}
783	np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
784	if (np->n_flag & NFLUSHWANT) {
785		np->n_flag &= ~NFLUSHWANT;
786		wakeup((caddr_t)&np->n_flag);
787	}
788	return (0);
789}
790
791/*
792 * Initiate asynchronous I/O. Return an error if no nfsiods are available.
793 * This is mainly to avoid queueing async I/O requests when the nfsiods
794 * are all hung on a dead server.
795 */
796int
797nfs_asyncio(bp, cred)
798	register struct buf *bp;
799	struct ucred *cred;
800{
801	struct nfsmount *nmp;
802	int i;
803	int gotiod;
804	int slpflag = 0;
805	int slptimeo = 0;
806	int error;
807
808	if (nfs_numasync == 0)
809		return (EIO);
810
811	nmp = VFSTONFS(bp->b_vp->v_mount);
812again:
813	if (nmp->nm_flag & NFSMNT_INT)
814		slpflag = PCATCH;
815	gotiod = FALSE;
816
817	/*
818	 * Find a free iod to process this request.
819	 */
820	for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
821		if (nfs_iodwant[i]) {
822			/*
823			 * Found one, so wake it up and tell it which
824			 * mount to process.
825			 */
826			NFS_DPF(ASYNCIO,
827				("nfs_asyncio: waking iod %d for mount %p\n",
828				 i, nmp));
829			nfs_iodwant[i] = (struct proc *)0;
830			nfs_iodmount[i] = nmp;
831			nmp->nm_bufqiods++;
832			wakeup((caddr_t)&nfs_iodwant[i]);
833			gotiod = TRUE;
834			break;
835		}
836
837	/*
838	 * If none are free, we may already have an iod working on this mount
839	 * point.  If so, it will process our request.
840	 */
841	if (!gotiod) {
842		if (nmp->nm_bufqiods > 0) {
843			NFS_DPF(ASYNCIO,
844				("nfs_asyncio: %d iods are already processing mount %p\n",
845				 nmp->nm_bufqiods, nmp));
846			gotiod = TRUE;
847		}
848	}
849
850	/*
851	 * If we have an iod which can process the request, then queue
852	 * the buffer.
853	 */
854	if (gotiod) {
855		/*
856		 * Ensure that the queue never grows too large.
857		 */
858		while (nmp->nm_bufqlen >= 2*nfs_numasync) {
859			NFS_DPF(ASYNCIO,
860				("nfs_asyncio: waiting for mount %p queue to drain\n", nmp));
861			nmp->nm_bufqwant = TRUE;
862			error = tsleep(&nmp->nm_bufq, slpflag | PRIBIO,
863				       "nfsaio", slptimeo);
864			if (error) {
865				if (nfs_sigintr(nmp, NULL, bp->b_proc))
866					return (EINTR);
867				if (slpflag == PCATCH) {
868					slpflag = 0;
869					slptimeo = 2 * hz;
870				}
871			}
872			/*
873			 * We might have lost our iod while sleeping,
874			 * so check and loop if nescessary.
875			 */
876			if (nmp->nm_bufqiods == 0) {
877				NFS_DPF(ASYNCIO,
878					("nfs_asyncio: no iods after mount %p queue was drained, looping\n", nmp));
879				goto again;
880			}
881		}
882
883		if (bp->b_flags & B_READ) {
884			if (bp->b_rcred == NOCRED && cred != NOCRED) {
885				crhold(cred);
886				bp->b_rcred = cred;
887			}
888		} else {
889			bp->b_flags |= B_WRITEINPROG;
890			if (bp->b_wcred == NOCRED && cred != NOCRED) {
891				crhold(cred);
892				bp->b_wcred = cred;
893			}
894		}
895
896		TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist);
897		nmp->nm_bufqlen++;
898		return (0);
899	}
900
901	/*
902	 * All the iods are busy on other mounts, so return EIO to
903	 * force the caller to process the i/o synchronously.
904	 */
905	NFS_DPF(ASYNCIO, ("nfs_asyncio: no iods available, i/o is synchronous\n"));
906	return (EIO);
907}
908
909/*
910 * Do an I/O operation to/from a cache block. This may be called
911 * synchronously or from an nfsiod.
912 */
913int
914nfs_doio(bp, cr, p)
915	register struct buf *bp;
916	struct ucred *cr;
917	struct proc *p;
918{
919	register struct uio *uiop;
920	register struct vnode *vp;
921	struct nfsnode *np;
922	struct nfsmount *nmp;
923	int error = 0, diff, len, iomode, must_commit = 0;
924	struct uio uio;
925	struct iovec io;
926
927	vp = bp->b_vp;
928	np = VTONFS(vp);
929	nmp = VFSTONFS(vp->v_mount);
930	uiop = &uio;
931	uiop->uio_iov = &io;
932	uiop->uio_iovcnt = 1;
933	uiop->uio_segflg = UIO_SYSSPACE;
934	uiop->uio_procp = p;
935
936	/*
937	 * Historically, paging was done with physio, but no more.
938	 */
939	if (bp->b_flags & B_PHYS) {
940	    /*
941	     * ...though reading /dev/drum still gets us here.
942	     */
943	    io.iov_len = uiop->uio_resid = bp->b_bcount;
944	    /* mapping was done by vmapbuf() */
945	    io.iov_base = bp->b_data;
946	    uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
947	    if (bp->b_flags & B_READ) {
948		uiop->uio_rw = UIO_READ;
949		nfsstats.read_physios++;
950		error = nfs_readrpc(vp, uiop, cr);
951	    } else {
952		int com;
953
954		iomode = NFSV3WRITE_DATASYNC;
955		uiop->uio_rw = UIO_WRITE;
956		nfsstats.write_physios++;
957		error = nfs_writerpc(vp, uiop, cr, &iomode, &com);
958	    }
959	    if (error) {
960		bp->b_flags |= B_ERROR;
961		bp->b_error = error;
962	    }
963	} else if (bp->b_flags & B_READ) {
964	    io.iov_len = uiop->uio_resid = bp->b_bcount;
965	    io.iov_base = bp->b_data;
966	    uiop->uio_rw = UIO_READ;
967	    switch (vp->v_type) {
968	    case VREG:
969		uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
970		nfsstats.read_bios++;
971		error = nfs_readrpc(vp, uiop, cr);
972		if (!error) {
973		    bp->b_validoff = 0;
974		    if (uiop->uio_resid) {
975			/*
976			 * If len > 0, there is a hole in the file and
977			 * no writes after the hole have been pushed to
978			 * the server yet.
979			 * Just zero fill the rest of the valid area.
980			 */
981			diff = bp->b_bcount - uiop->uio_resid;
982			len = np->n_size - (((u_quad_t)bp->b_blkno) * DEV_BSIZE
983				+ diff);
984			if (len > 0) {
985			    len = min(len, uiop->uio_resid);
986			    bzero((char *)bp->b_data + diff, len);
987			    bp->b_validend = diff + len;
988			} else
989			    bp->b_validend = diff;
990		    } else
991			bp->b_validend = bp->b_bcount;
992		}
993		if (p && (vp->v_flag & VTEXT) &&
994			(((nmp->nm_flag & NFSMNT_NQNFS) &&
995			  NQNFS_CKINVALID(vp, np, ND_READ) &&
996			  np->n_lrev != np->n_brev) ||
997			 (!(nmp->nm_flag & NFSMNT_NQNFS) &&
998			  np->n_mtime != np->n_vattr.va_mtime.tv_sec))) {
999			uprintf("Process killed due to text file modification\n");
1000			psignal(p, SIGKILL);
1001			p->p_flag |= P_NOSWAP;
1002		}
1003		break;
1004	    case VLNK:
1005		uiop->uio_offset = (off_t)0;
1006		nfsstats.readlink_bios++;
1007		error = nfs_readlinkrpc(vp, uiop, cr);
1008		break;
1009	    case VDIR:
1010		nfsstats.readdir_bios++;
1011		uiop->uio_offset = ((u_quad_t)bp->b_lblkno) * NFS_DIRBLKSIZ;
1012		if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
1013			error = nfs_readdirplusrpc(vp, uiop, cr);
1014			if (error == NFSERR_NOTSUPP)
1015				nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
1016		}
1017		if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
1018			error = nfs_readdirrpc(vp, uiop, cr);
1019		break;
1020	    default:
1021		printf("nfs_doio:  type %x unexpected\n",vp->v_type);
1022		break;
1023	    };
1024	    if (error) {
1025		bp->b_flags |= B_ERROR;
1026		bp->b_error = error;
1027	    }
1028	} else {
1029	    if (((bp->b_blkno * DEV_BSIZE) + bp->b_dirtyend) > np->n_size)
1030		bp->b_dirtyend = np->n_size - (bp->b_blkno * DEV_BSIZE);
1031
1032	    if (bp->b_dirtyend > bp->b_dirtyoff) {
1033		io.iov_len = uiop->uio_resid = bp->b_dirtyend
1034		    - bp->b_dirtyoff;
1035		uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE
1036		    + bp->b_dirtyoff;
1037		io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
1038		uiop->uio_rw = UIO_WRITE;
1039		nfsstats.write_bios++;
1040		if ((bp->b_flags & (B_ASYNC | B_NEEDCOMMIT | B_NOCACHE | B_CLUSTER)) == B_ASYNC)
1041		    iomode = NFSV3WRITE_UNSTABLE;
1042		else
1043		    iomode = NFSV3WRITE_FILESYNC;
1044		bp->b_flags |= B_WRITEINPROG;
1045		error = nfs_writerpc(vp, uiop, cr, &iomode, &must_commit);
1046		if (!error && iomode == NFSV3WRITE_UNSTABLE) {
1047		    bp->b_flags |= B_NEEDCOMMIT;
1048		    if (bp->b_dirtyoff == 0
1049			&& bp->b_dirtyend == bp->b_bufsize)
1050			bp->b_flags |= B_CLUSTEROK;
1051		} else
1052		    bp->b_flags &= ~B_NEEDCOMMIT;
1053		bp->b_flags &= ~B_WRITEINPROG;
1054
1055		/*
1056		 * For an interrupted write, the buffer is still valid
1057		 * and the write hasn't been pushed to the server yet,
1058		 * so we can't set B_ERROR and report the interruption
1059		 * by setting B_EINTR. For the B_ASYNC case, B_EINTR
1060		 * is not relevant, so the rpc attempt is essentially
1061		 * a noop.  For the case of a V3 write rpc not being
1062		 * committed to stable storage, the block is still
1063		 * dirty and requires either a commit rpc or another
1064		 * write rpc with iomode == NFSV3WRITE_FILESYNC before
1065		 * the block is reused. This is indicated by setting
1066		 * the B_DELWRI and B_NEEDCOMMIT flags.
1067		 */
1068    		if (error == EINTR
1069		    || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
1070			bp->b_flags &= ~(B_INVAL|B_NOCACHE);
1071			++numdirtybuffers;
1072			bp->b_flags |= B_DELWRI;
1073
1074		/*
1075		 * Since for the B_ASYNC case, nfs_bwrite() has reassigned the
1076		 * buffer to the clean list, we have to reassign it back to the
1077		 * dirty one. Ugh.
1078		 */
1079			if (bp->b_flags & B_ASYNC)
1080				reassignbuf(bp, vp);
1081			else
1082				bp->b_flags |= B_EINTR;
1083	    	} else {
1084			if (error) {
1085				bp->b_flags |= B_ERROR;
1086				bp->b_error = np->n_error = error;
1087				np->n_flag |= NWRITEERR;
1088			}
1089			bp->b_dirtyoff = bp->b_dirtyend = 0;
1090		}
1091	    } else {
1092		bp->b_resid = 0;
1093		biodone(bp);
1094		return (0);
1095	    }
1096	}
1097	bp->b_resid = uiop->uio_resid;
1098	if (must_commit)
1099		nfs_clearcommit(vp->v_mount);
1100	biodone(bp);
1101	return (error);
1102}
1103