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