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