nfs_bio.c revision 25003
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.34 1997/04/03 07:52:00 dfr 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#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		if (np->n_direofoffset
297		    && uio->uio_offset >= np->n_direofoffset) {
298		    return (0);
299		}
300		lbn = uio->uio_offset / NFS_DIRBLKSIZ;
301		on = uio->uio_offset & (NFS_DIRBLKSIZ - 1);
302		bp = nfs_getcacheblk(vp, lbn, NFS_DIRBLKSIZ, p);
303		if (!bp)
304		    return (EINTR);
305		if ((bp->b_flags & B_CACHE) == 0) {
306		    bp->b_flags |= B_READ;
307		    vfs_busy_pages(bp, 0);
308		    error = nfs_doio(bp, cred, p);
309		    if (error) {
310		        vfs_unbusy_pages(bp);
311			brelse(bp);
312			while (error == NFSERR_BAD_COOKIE) {
313			    nfs_invaldir(vp);
314			    error = nfs_vinvalbuf(vp, 0, cred, p, 1);
315			    /*
316			     * Yuck! The directory has been modified on the
317			     * server. The only way to get the block is by
318			     * reading from the beginning to get all the
319			     * offset cookies.
320			     */
321			    for (i = 0; i <= lbn && !error; i++) {
322				if (np->n_direofoffset
323				    && (i * NFS_DIRBLKSIZ) >= np->n_direofoffset)
324				    return (0);
325				bp = nfs_getcacheblk(vp, i, NFS_DIRBLKSIZ, p);
326				if (!bp)
327				    return (EINTR);
328				if ((bp->b_flags & B_DONE) == 0) {
329				    bp->b_flags |= B_READ;
330				    vfs_busy_pages(bp, 0);
331				    error = nfs_doio(bp, cred, p);
332				    if (error) {
333					vfs_unbusy_pages(bp);
334					brelse(bp);
335				    } else if (i < lbn)
336					brelse(bp);
337				}
338			    }
339			}
340			if (error)
341			    return (error);
342		    }
343		}
344
345		/*
346		 * If not eof and read aheads are enabled, start one.
347		 * (You need the current block first, so that you have the
348		 *  directory offset cookie of the next block.)
349		 */
350		if (nfs_numasync > 0 && nmp->nm_readahead > 0 &&
351		    (np->n_direofoffset == 0 ||
352		    (lbn + 1) * NFS_DIRBLKSIZ < np->n_direofoffset) &&
353		    !(np->n_flag & NQNFSNONCACHE) &&
354		    !incore(vp, lbn + 1)) {
355			rabp = nfs_getcacheblk(vp, lbn + 1, NFS_DIRBLKSIZ, p);
356			if (rabp) {
357			    if ((rabp->b_flags & (B_CACHE|B_DELWRI)) == 0) {
358				rabp->b_flags |= (B_READ | B_ASYNC);
359				vfs_busy_pages(rabp, 0);
360				if (nfs_asyncio(rabp, cred)) {
361				    rabp->b_flags |= B_INVAL|B_ERROR;
362				    vfs_unbusy_pages(rabp);
363				    brelse(rabp);
364				}
365			    } else {
366				brelse(rabp);
367			    }
368			}
369		}
370		n = min(uio->uio_resid, NFS_DIRBLKSIZ - bp->b_resid - on);
371		break;
372	    default:
373		printf(" nfs_bioread: type %x unexpected\n",vp->v_type);
374		break;
375	    };
376
377	    if (n > 0) {
378		error = uiomove(bp->b_data + on, (int)n, uio);
379	    }
380	    switch (vp->v_type) {
381	    case VREG:
382		break;
383	    case VLNK:
384		n = 0;
385		break;
386	    case VDIR:
387		if (np->n_flag & NQNFSNONCACHE)
388			bp->b_flags |= B_INVAL;
389		break;
390	    default:
391		printf(" nfs_bioread: type %x unexpected\n",vp->v_type);
392	    }
393 	    brelse(bp);
394	} while (error == 0 && uio->uio_resid > 0 && n > 0);
395	return (error);
396}
397
398/*
399 * Vnode op for write using bio
400 */
401int
402nfs_write(ap)
403	struct vop_write_args /* {
404		struct vnode *a_vp;
405		struct uio *a_uio;
406		int  a_ioflag;
407		struct ucred *a_cred;
408	} */ *ap;
409{
410	register int biosize;
411	register struct uio *uio = ap->a_uio;
412	struct proc *p = uio->uio_procp;
413	register struct vnode *vp = ap->a_vp;
414	struct nfsnode *np = VTONFS(vp);
415	register struct ucred *cred = ap->a_cred;
416	int ioflag = ap->a_ioflag;
417	struct buf *bp;
418	struct vattr vattr;
419	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
420	daddr_t lbn;
421	int bufsize;
422	int n, on, error = 0, iomode, must_commit;
423
424#ifdef DIAGNOSTIC
425	if (uio->uio_rw != UIO_WRITE)
426		panic("nfs_write mode");
427	if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
428		panic("nfs_write proc");
429#endif
430	if (vp->v_type != VREG)
431		return (EIO);
432	if (np->n_flag & NWRITEERR) {
433		np->n_flag &= ~NWRITEERR;
434		return (np->n_error);
435	}
436	if ((nmp->nm_flag & (NFSMNT_NFSV3 | NFSMNT_GOTFSINFO)) == NFSMNT_NFSV3)
437		(void)nfs_fsinfo(nmp, vp, cred, p);
438	if (ioflag & (IO_APPEND | IO_SYNC)) {
439		if (np->n_flag & NMODIFIED) {
440			np->n_attrstamp = 0;
441			error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
442			if (error)
443				return (error);
444		}
445		if (ioflag & IO_APPEND) {
446			np->n_attrstamp = 0;
447			error = VOP_GETATTR(vp, &vattr, cred, p);
448			if (error)
449				return (error);
450			uio->uio_offset = np->n_size;
451		}
452	}
453	if (uio->uio_offset < 0)
454		return (EINVAL);
455	if (uio->uio_resid == 0)
456		return (0);
457	/*
458	 * Maybe this should be above the vnode op call, but so long as
459	 * file servers have no limits, i don't think it matters
460	 */
461	if (p && uio->uio_offset + uio->uio_resid >
462	      p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
463		psignal(p, SIGXFSZ);
464		return (EFBIG);
465	}
466	/*
467	 * I use nm_rsize, not nm_wsize so that all buffer cache blocks
468	 * will be the same size within a filesystem. nfs_writerpc will
469	 * still use nm_wsize when sizing the rpc's.
470	 */
471	biosize = vp->v_mount->mnt_stat.f_iosize;
472	do {
473		/*
474		 * Check for a valid write lease.
475		 */
476		if ((nmp->nm_flag & NFSMNT_NQNFS) &&
477		    NQNFS_CKINVALID(vp, np, ND_WRITE)) {
478			do {
479				error = nqnfs_getlease(vp, ND_WRITE, cred, p);
480			} while (error == NQNFS_EXPIRED);
481			if (error)
482				return (error);
483			if (np->n_lrev != np->n_brev ||
484			    (np->n_flag & NQNFSNONCACHE)) {
485				error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
486				if (error)
487					return (error);
488				np->n_brev = np->n_lrev;
489			}
490		}
491		if ((np->n_flag & NQNFSNONCACHE) && uio->uio_iovcnt == 1) {
492		    iomode = NFSV3WRITE_FILESYNC;
493		    error = nfs_writerpc(vp, uio, cred, &iomode, &must_commit);
494		    if (must_commit)
495			nfs_clearcommit(vp->v_mount);
496		    return (error);
497		}
498		nfsstats.biocache_writes++;
499		lbn = uio->uio_offset / biosize;
500		on = uio->uio_offset & (biosize-1);
501		n = min((unsigned)(biosize - on), uio->uio_resid);
502again:
503		if (uio->uio_offset + n > np->n_size) {
504			np->n_size = uio->uio_offset + n;
505			vnode_pager_setsize(vp, (u_long)np->n_size);
506		}
507		bufsize = biosize;
508		if ((lbn + 1) * biosize > np->n_size) {
509			bufsize = np->n_size - lbn * biosize;
510			bufsize = (bufsize + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
511		}
512		bp = nfs_getcacheblk(vp, lbn, bufsize, p);
513		if (!bp)
514			return (EINTR);
515		if (bp->b_wcred == NOCRED) {
516			crhold(cred);
517			bp->b_wcred = cred;
518		}
519		np->n_flag |= NMODIFIED;
520
521		if ((bp->b_blkno * DEV_BSIZE) + bp->b_dirtyend > np->n_size) {
522			bp->b_dirtyend = np->n_size - (bp->b_blkno * DEV_BSIZE);
523		}
524
525		/*
526		 * If the new write will leave a contiguous dirty
527		 * area, just update the b_dirtyoff and b_dirtyend,
528		 * otherwise force a write rpc of the old dirty area.
529		 */
530		if (bp->b_dirtyend > 0 &&
531		    (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) {
532			bp->b_proc = p;
533			if (VOP_BWRITE(bp) == EINTR)
534				return (EINTR);
535			goto again;
536		}
537
538		/*
539		 * Check for valid write lease and get one as required.
540		 * In case getblk() and/or bwrite() delayed us.
541		 */
542		if ((nmp->nm_flag & NFSMNT_NQNFS) &&
543		    NQNFS_CKINVALID(vp, np, ND_WRITE)) {
544			do {
545				error = nqnfs_getlease(vp, ND_WRITE, cred, p);
546			} while (error == NQNFS_EXPIRED);
547			if (error) {
548				brelse(bp);
549				return (error);
550			}
551			if (np->n_lrev != np->n_brev ||
552			    (np->n_flag & NQNFSNONCACHE)) {
553				brelse(bp);
554				error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
555				if (error)
556					return (error);
557				np->n_brev = np->n_lrev;
558				goto again;
559			}
560		}
561		error = uiomove((char *)bp->b_data + on, n, uio);
562		if (error) {
563			bp->b_flags |= B_ERROR;
564			brelse(bp);
565			return (error);
566		}
567		if (bp->b_dirtyend > 0) {
568			bp->b_dirtyoff = min(on, bp->b_dirtyoff);
569			bp->b_dirtyend = max((on + n), bp->b_dirtyend);
570		} else {
571			bp->b_dirtyoff = on;
572			bp->b_dirtyend = on + n;
573		}
574		if (bp->b_validend == 0 || bp->b_validend < bp->b_dirtyoff ||
575		    bp->b_validoff > bp->b_dirtyend) {
576			bp->b_validoff = bp->b_dirtyoff;
577			bp->b_validend = bp->b_dirtyend;
578		} else {
579			bp->b_validoff = min(bp->b_validoff, bp->b_dirtyoff);
580			bp->b_validend = max(bp->b_validend, bp->b_dirtyend);
581		}
582
583		/*
584		 * Since this block is being modified, it must be written
585		 * again and not just committed.
586		 */
587		bp->b_flags &= ~B_NEEDCOMMIT;
588
589		/*
590		 * If the lease is non-cachable or IO_SYNC do bwrite().
591		 */
592		if ((np->n_flag & NQNFSNONCACHE) || (ioflag & IO_SYNC)) {
593			bp->b_proc = p;
594			error = VOP_BWRITE(bp);
595			if (error)
596				return (error);
597			if (np->n_flag & NQNFSNONCACHE) {
598				error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
599				if (error)
600					return (error);
601			}
602		} else if ((n + on) == biosize &&
603			(nmp->nm_flag & NFSMNT_NQNFS) == 0) {
604			bp->b_proc = (struct proc *)0;
605			bp->b_flags |= B_ASYNC;
606			(void)nfs_writebp(bp, 0);
607		} else
608			bdwrite(bp);
609	} while (uio->uio_resid > 0 && n > 0);
610	return (0);
611}
612
613/*
614 * Get an nfs cache block.
615 * Allocate a new one if the block isn't currently in the cache
616 * and return the block marked busy. If the calling process is
617 * interrupted by a signal for an interruptible mount point, return
618 * NULL.
619 */
620static struct buf *
621nfs_getcacheblk(vp, bn, size, p)
622	struct vnode *vp;
623	daddr_t bn;
624	int size;
625	struct proc *p;
626{
627	register struct buf *bp;
628	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
629	int biosize = vp->v_mount->mnt_stat.f_iosize;
630
631	if (nmp->nm_flag & NFSMNT_INT) {
632		bp = getblk(vp, bn, size, PCATCH, 0);
633		while (bp == (struct buf *)0) {
634			if (nfs_sigintr(nmp, (struct nfsreq *)0, p))
635				return ((struct buf *)0);
636			bp = getblk(vp, bn, size, 0, 2 * hz);
637		}
638	} else
639		bp = getblk(vp, bn, size, 0, 0);
640
641	if( vp->v_type == VREG)
642		bp->b_blkno = (bn * biosize) / DEV_BSIZE;
643
644	return (bp);
645}
646
647/*
648 * Flush and invalidate all dirty buffers. If another process is already
649 * doing the flush, just wait for completion.
650 */
651int
652nfs_vinvalbuf(vp, flags, cred, p, intrflg)
653	struct vnode *vp;
654	int flags;
655	struct ucred *cred;
656	struct proc *p;
657	int intrflg;
658{
659	register struct nfsnode *np = VTONFS(vp);
660	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
661	int error = 0, slpflag, slptimeo;
662
663	if ((nmp->nm_flag & NFSMNT_INT) == 0)
664		intrflg = 0;
665	if (intrflg) {
666		slpflag = PCATCH;
667		slptimeo = 2 * hz;
668	} else {
669		slpflag = 0;
670		slptimeo = 0;
671	}
672	/*
673	 * First wait for any other process doing a flush to complete.
674	 */
675	while (np->n_flag & NFLUSHINPROG) {
676		np->n_flag |= NFLUSHWANT;
677		error = tsleep((caddr_t)&np->n_flag, PRIBIO + 2, "nfsvinval",
678			slptimeo);
679		if (error && intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, p))
680			return (EINTR);
681	}
682
683	/*
684	 * Now, flush as required.
685	 */
686	np->n_flag |= NFLUSHINPROG;
687	error = vinvalbuf(vp, flags, cred, p, slpflag, 0);
688	while (error) {
689		if (intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, p)) {
690			np->n_flag &= ~NFLUSHINPROG;
691			if (np->n_flag & NFLUSHWANT) {
692				np->n_flag &= ~NFLUSHWANT;
693				wakeup((caddr_t)&np->n_flag);
694			}
695			return (EINTR);
696		}
697		error = vinvalbuf(vp, flags, cred, p, 0, slptimeo);
698	}
699	np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
700	if (np->n_flag & NFLUSHWANT) {
701		np->n_flag &= ~NFLUSHWANT;
702		wakeup((caddr_t)&np->n_flag);
703	}
704	return (0);
705}
706
707/*
708 * Initiate asynchronous I/O. Return an error if no nfsiods are available.
709 * This is mainly to avoid queueing async I/O requests when the nfsiods
710 * are all hung on a dead server.
711 */
712int
713nfs_asyncio(bp, cred)
714	register struct buf *bp;
715	struct ucred *cred;
716{
717	struct nfsmount *nmp;
718	int i;
719	int gotiod;
720	int slpflag = 0;
721	int slptimeo = 0;
722	int error;
723
724	if (nfs_numasync == 0)
725		return (EIO);
726
727	nmp = VFSTONFS(bp->b_vp->v_mount);
728again:
729	if (nmp->nm_flag & NFSMNT_INT)
730		slpflag = PCATCH;
731	gotiod = FALSE;
732
733	/*
734	 * Find a free iod to process this request.
735	 */
736	for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
737		if (nfs_iodwant[i]) {
738			/*
739			 * Found one, so wake it up and tell it which
740			 * mount to process.
741			 */
742			NFS_DPF(ASYNCIO,
743				("nfs_asyncio: waking iod %d for mount %p\n",
744				 i, nmp));
745			nfs_iodwant[i] = (struct proc *)0;
746			nfs_iodmount[i] = nmp;
747			nmp->nm_bufqiods++;
748			wakeup((caddr_t)&nfs_iodwant[i]);
749			gotiod = TRUE;
750		}
751
752	/*
753	 * If none are free, we may already have an iod working on this mount
754	 * point.  If so, it will process our request.
755	 */
756	if (!gotiod) {
757		if (nmp->nm_bufqiods > 0) {
758			NFS_DPF(ASYNCIO,
759				("nfs_asyncio: %d iods are already processing mount %p\n",
760				 nmp->nm_bufqiods, nmp));
761			gotiod = TRUE;
762		}
763	}
764
765	/*
766	 * If we have an iod which can process the request, then queue
767	 * the buffer.
768	 */
769	if (gotiod) {
770		/*
771		 * Ensure that the queue never grows too large.
772		 */
773		while (nmp->nm_bufqlen >= 2*nfs_numasync) {
774			NFS_DPF(ASYNCIO,
775				("nfs_asyncio: waiting for mount %p queue to drain\n", nmp));
776			nmp->nm_bufqwant = TRUE;
777			error = tsleep(&nmp->nm_bufq, slpflag | PRIBIO,
778				       "nfsaio", slptimeo);
779			if (error) {
780				if (nfs_sigintr(nmp, NULL, bp->b_proc))
781					return (EINTR);
782				if (slpflag == PCATCH) {
783					slpflag = 0;
784					slptimeo = 2 * hz;
785				}
786			}
787			/*
788			 * We might have lost our iod while sleeping,
789			 * so check and loop if nescessary.
790			 */
791			if (nmp->nm_bufqiods == 0) {
792				NFS_DPF(ASYNCIO,
793					("nfs_asyncio: no iods after mount %p queue was drained, looping\n", nmp));
794				goto again;
795			}
796		}
797
798		if (bp->b_flags & B_READ) {
799			if (bp->b_rcred == NOCRED && cred != NOCRED) {
800				crhold(cred);
801				bp->b_rcred = cred;
802			}
803		} else {
804			bp->b_flags |= B_WRITEINPROG;
805			if (bp->b_wcred == NOCRED && cred != NOCRED) {
806				crhold(cred);
807				bp->b_wcred = cred;
808			}
809		}
810
811		TAILQ_INSERT_TAIL(&nmp->nm_bufq, bp, b_freelist);
812		nmp->nm_bufqlen++;
813		return (0);
814	}
815
816	/*
817	 * All the iods are busy on other mounts, so return EIO to
818	 * force the caller to process the i/o synchronously.
819	 */
820	NFS_DPF(ASYNCIO, ("nfs_asyncio: no iods available, i/o is synchronous\n"));
821	return (EIO);
822}
823
824/*
825 * Do an I/O operation to/from a cache block. This may be called
826 * synchronously or from an nfsiod.
827 */
828int
829nfs_doio(bp, cr, p)
830	register struct buf *bp;
831	struct ucred *cr;
832	struct proc *p;
833{
834	register struct uio *uiop;
835	register struct vnode *vp;
836	struct nfsnode *np;
837	struct nfsmount *nmp;
838	int error = 0, diff, len, iomode, must_commit = 0;
839	struct uio uio;
840	struct iovec io;
841
842	vp = bp->b_vp;
843	np = VTONFS(vp);
844	nmp = VFSTONFS(vp->v_mount);
845	uiop = &uio;
846	uiop->uio_iov = &io;
847	uiop->uio_iovcnt = 1;
848	uiop->uio_segflg = UIO_SYSSPACE;
849	uiop->uio_procp = p;
850
851	/*
852	 * Historically, paging was done with physio, but no more.
853	 */
854	if (bp->b_flags & B_PHYS) {
855	    /*
856	     * ...though reading /dev/drum still gets us here.
857	     */
858	    io.iov_len = uiop->uio_resid = bp->b_bcount;
859	    /* mapping was done by vmapbuf() */
860	    io.iov_base = bp->b_data;
861	    uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
862	    if (bp->b_flags & B_READ) {
863		uiop->uio_rw = UIO_READ;
864		nfsstats.read_physios++;
865		error = nfs_readrpc(vp, uiop, cr);
866	    } else {
867		int com;
868
869		iomode = NFSV3WRITE_DATASYNC;
870		uiop->uio_rw = UIO_WRITE;
871		nfsstats.write_physios++;
872		error = nfs_writerpc(vp, uiop, cr, &iomode, &com);
873	    }
874	    if (error) {
875		bp->b_flags |= B_ERROR;
876		bp->b_error = error;
877	    }
878	} else if (bp->b_flags & B_READ) {
879	    io.iov_len = uiop->uio_resid = bp->b_bcount;
880	    io.iov_base = bp->b_data;
881	    uiop->uio_rw = UIO_READ;
882	    switch (vp->v_type) {
883	    case VREG:
884		uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
885		nfsstats.read_bios++;
886		error = nfs_readrpc(vp, uiop, cr);
887		if (!error) {
888		    bp->b_validoff = 0;
889		    if (uiop->uio_resid) {
890			/*
891			 * If len > 0, there is a hole in the file and
892			 * no writes after the hole have been pushed to
893			 * the server yet.
894			 * Just zero fill the rest of the valid area.
895			 */
896			diff = bp->b_bcount - uiop->uio_resid;
897			len = np->n_size - (((u_quad_t)bp->b_blkno) * DEV_BSIZE
898				+ diff);
899			if (len > 0) {
900			    len = min(len, uiop->uio_resid);
901			    bzero((char *)bp->b_data + diff, len);
902			    bp->b_validend = diff + len;
903			} else
904			    bp->b_validend = diff;
905		    } else
906			bp->b_validend = bp->b_bcount;
907		}
908		if (p && (vp->v_flag & VTEXT) &&
909			(((nmp->nm_flag & NFSMNT_NQNFS) &&
910			  NQNFS_CKINVALID(vp, np, ND_READ) &&
911			  np->n_lrev != np->n_brev) ||
912			 (!(nmp->nm_flag & NFSMNT_NQNFS) &&
913			  np->n_mtime != np->n_vattr.va_mtime.tv_sec))) {
914			uprintf("Process killed due to text file modification\n");
915			psignal(p, SIGKILL);
916#ifdef __NetBSD__
917			p->p_holdcnt++;
918#else
919			p->p_flag |= P_NOSWAP;
920#endif
921		}
922		break;
923	    case VLNK:
924		uiop->uio_offset = (off_t)0;
925		nfsstats.readlink_bios++;
926		error = nfs_readlinkrpc(vp, uiop, cr);
927		break;
928	    case VDIR:
929		nfsstats.readdir_bios++;
930		uiop->uio_offset = ((u_quad_t)bp->b_lblkno) * NFS_DIRBLKSIZ;
931		if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
932			error = nfs_readdirplusrpc(vp, uiop, cr);
933			if (error == NFSERR_NOTSUPP)
934				nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
935		}
936		if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
937			error = nfs_readdirrpc(vp, uiop, cr);
938		break;
939	    default:
940		printf("nfs_doio:  type %x unexpected\n",vp->v_type);
941		break;
942	    };
943	    if (error) {
944		bp->b_flags |= B_ERROR;
945		bp->b_error = error;
946	    }
947	} else {
948	    if (((bp->b_blkno * DEV_BSIZE) + bp->b_dirtyend) > np->n_size)
949		bp->b_dirtyend = np->n_size - (bp->b_blkno * DEV_BSIZE);
950
951	    if (bp->b_dirtyend > bp->b_dirtyoff) {
952		io.iov_len = uiop->uio_resid = bp->b_dirtyend
953		    - bp->b_dirtyoff;
954		uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE
955		    + bp->b_dirtyoff;
956		io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
957		uiop->uio_rw = UIO_WRITE;
958		nfsstats.write_bios++;
959		if ((bp->b_flags & (B_ASYNC | B_NEEDCOMMIT | B_NOCACHE)) == B_ASYNC)
960		    iomode = NFSV3WRITE_UNSTABLE;
961		else
962		    iomode = NFSV3WRITE_FILESYNC;
963		bp->b_flags |= B_WRITEINPROG;
964		error = nfs_writerpc(vp, uiop, cr, &iomode, &must_commit);
965		if (!error && iomode == NFSV3WRITE_UNSTABLE) {
966		    bp->b_flags |= B_NEEDCOMMIT;
967		    if (bp->b_dirtyoff == 0
968			&& bp->b_dirtyend == bp->b_bufsize)
969			bp->b_flags |= B_CLUSTEROK;
970		} else
971		    bp->b_flags &= ~B_NEEDCOMMIT;
972		bp->b_flags &= ~B_WRITEINPROG;
973
974		/*
975		 * For an interrupted write, the buffer is still valid
976		 * and the write hasn't been pushed to the server yet,
977		 * so we can't set B_ERROR and report the interruption
978		 * by setting B_EINTR. For the B_ASYNC case, B_EINTR
979		 * is not relevant, so the rpc attempt is essentially
980		 * a noop.  For the case of a V3 write rpc not being
981		 * committed to stable storage, the block is still
982		 * dirty and requires either a commit rpc or another
983		 * write rpc with iomode == NFSV3WRITE_FILESYNC before
984		 * the block is reused. This is indicated by setting
985		 * the B_DELWRI and B_NEEDCOMMIT flags.
986		 */
987    		if (error == EINTR
988		    || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
989			bp->b_flags &= ~(B_INVAL|B_NOCACHE);
990			bp->b_flags |= B_DELWRI;
991
992		/*
993		 * Since for the B_ASYNC case, nfs_bwrite() has reassigned the
994		 * buffer to the clean list, we have to reassign it back to the
995		 * dirty one. Ugh.
996		 */
997			if (bp->b_flags & B_ASYNC)
998				reassignbuf(bp, vp);
999			else
1000				bp->b_flags |= B_EINTR;
1001	    	} else {
1002			if (error) {
1003				bp->b_flags |= B_ERROR;
1004				bp->b_error = np->n_error = error;
1005				np->n_flag |= NWRITEERR;
1006			}
1007			bp->b_dirtyoff = bp->b_dirtyend = 0;
1008		}
1009	    } else {
1010		bp->b_resid = 0;
1011		biodone(bp);
1012		return (0);
1013	    }
1014	}
1015	bp->b_resid = uiop->uio_resid;
1016	if (must_commit)
1017		nfs_clearcommit(vp->v_mount);
1018	biodone(bp);
1019	return (error);
1020}
1021