nfs_bio.c revision 13612
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 * $Id: nfs_bio.c,v 1.21 1995/12/17 21:12:13 phk Exp $
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
50#include <vm/vm.h>
51#include <vm/vm_param.h>
52#include <vm/vm_extern.h>
53
54#include <nfs/rpcv2.h>
55#include <nfs/nfsproto.h>
56#include <nfs/nfs.h>
57#include <nfs/nfsmount.h>
58#include <nfs/nqnfs.h>
59#include <nfs/nfsnode.h>
60
61static struct buf *nfs_getcacheblk __P((struct vnode *vp, daddr_t bn, int size,
62					struct proc *p));
63
64extern struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON];
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.ts_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.ts_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.ts_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			not_readin = 0;
254			vfs_busy_pages(bp, 0);
255			error = nfs_doio(bp, cred, p);
256			if (error) {
257			    brelse(bp);
258			    return (error);
259			}
260		}
261		if (bufsize > on) {
262			n = min((unsigned)(bufsize - on), uio->uio_resid);
263		} else {
264			n = 0;
265		}
266		diff = np->n_size - uio->uio_offset;
267		if (diff < n)
268			n = diff;
269		if (not_readin && n > 0) {
270			if (on < bp->b_validoff || (on + n) > bp->b_validend) {
271				bp->b_flags |= B_NOCACHE;
272				if (bp->b_dirtyend > 0) {
273				    if ((bp->b_flags & B_DELWRI) == 0)
274					panic("nfsbioread");
275				    if (VOP_BWRITE(bp) == EINTR)
276					return (EINTR);
277				} else
278				    brelse(bp);
279				goto again;
280			}
281		}
282		vp->v_lastr = lbn;
283		diff = (on >= bp->b_validend) ? 0 : (bp->b_validend - on);
284		if (diff < n)
285			n = diff;
286		break;
287	    case VLNK:
288		nfsstats.biocache_readlinks++;
289		bp = nfs_getcacheblk(vp, (daddr_t)0, NFS_MAXPATHLEN, p);
290		if (!bp)
291			return (EINTR);
292		if ((bp->b_flags & B_CACHE) == 0) {
293			bp->b_flags |= B_READ;
294			vfs_busy_pages(bp, 0);
295			error = nfs_doio(bp, cred, p);
296			if (error) {
297				bp->b_flags |= B_ERROR;
298				brelse(bp);
299				return (error);
300			}
301		}
302		n = min(uio->uio_resid, NFS_MAXPATHLEN - bp->b_resid);
303		on = 0;
304		break;
305	    case VDIR:
306		nfsstats.biocache_readdirs++;
307		lbn = uio->uio_offset / NFS_DIRBLKSIZ;
308		on = uio->uio_offset & (NFS_DIRBLKSIZ - 1);
309		bp = nfs_getcacheblk(vp, lbn, NFS_DIRBLKSIZ, p);
310		if (!bp)
311		    return (EINTR);
312		if ((bp->b_flags & B_CACHE) == 0) {
313		    bp->b_flags |= B_READ;
314		    vfs_busy_pages(bp, 0);
315		    error = nfs_doio(bp, cred, p);
316		    if (error) {
317			brelse(bp);
318			while (error == NFSERR_BAD_COOKIE) {
319			    nfs_invaldir(vp);
320			    error = nfs_vinvalbuf(vp, 0, cred, p, 1);
321			    /*
322			     * Yuck! The directory has been modified on the
323			     * server. The only way to get the block is by
324			     * reading from the beginning to get all the
325			     * offset cookies.
326			     */
327			    for (i = 0; i <= lbn && !error; i++) {
328				bp = nfs_getcacheblk(vp, i, NFS_DIRBLKSIZ, p);
329				if (!bp)
330				    return (EINTR);
331				if ((bp->b_flags & B_DONE) == 0) {
332				    bp->b_flags |= B_READ;
333				    vfs_busy_pages(bp, 0);
334				    error = nfs_doio(bp, cred, p);
335				    if (error)
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		/*
475		 * XXX make sure we aren't cached in the VM page cache
476		 */
477		/*
478		 * Check for a valid write lease.
479		 */
480		if ((nmp->nm_flag & NFSMNT_NQNFS) &&
481		    NQNFS_CKINVALID(vp, np, ND_WRITE)) {
482			do {
483				error = nqnfs_getlease(vp, ND_WRITE, cred, p);
484			} while (error == NQNFS_EXPIRED);
485			if (error)
486				return (error);
487			if (np->n_lrev != np->n_brev ||
488			    (np->n_flag & NQNFSNONCACHE)) {
489				error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
490				if (error)
491					return (error);
492				np->n_brev = np->n_lrev;
493			}
494		}
495		if ((np->n_flag & NQNFSNONCACHE) && uio->uio_iovcnt == 1) {
496		    iomode = NFSV3WRITE_FILESYNC;
497		    error = nfs_writerpc(vp, uio, cred, &iomode, &must_commit);
498		    if (must_commit)
499			nfs_clearcommit(vp->v_mount);
500		    return (error);
501		}
502		nfsstats.biocache_writes++;
503		lbn = uio->uio_offset / biosize;
504		on = uio->uio_offset & (biosize-1);
505		n = min((unsigned)(biosize - on), uio->uio_resid);
506again:
507		if (uio->uio_offset + n > np->n_size) {
508			np->n_size = uio->uio_offset + n;
509			vnode_pager_setsize(vp, (u_long)np->n_size);
510		}
511		bufsize = biosize;
512		if ((lbn + 1) * biosize > np->n_size) {
513			bufsize = np->n_size - lbn * biosize;
514			bufsize = (bufsize + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
515		}
516		bp = nfs_getcacheblk(vp, lbn, bufsize, p);
517		if (!bp)
518			return (EINTR);
519		if (bp->b_wcred == NOCRED) {
520			crhold(cred);
521			bp->b_wcred = cred;
522		}
523		np->n_flag |= NMODIFIED;
524
525		if ((bp->b_blkno * DEV_BSIZE) + bp->b_dirtyend > np->n_size) {
526			bp->b_dirtyend = np->n_size - (bp->b_blkno * DEV_BSIZE);
527		}
528
529		/*
530		 * If the new write will leave a contiguous dirty
531		 * area, just update the b_dirtyoff and b_dirtyend,
532		 * otherwise force a write rpc of the old dirty area.
533		 */
534		if (bp->b_dirtyend > 0 &&
535		    (on > bp->b_dirtyend || (on + n) < bp->b_dirtyoff)) {
536			bp->b_proc = p;
537			if (VOP_BWRITE(bp) == EINTR)
538				return (EINTR);
539			goto again;
540		}
541
542		/*
543		 * Check for valid write lease and get one as required.
544		 * In case getblk() and/or bwrite() delayed us.
545		 */
546		if ((nmp->nm_flag & NFSMNT_NQNFS) &&
547		    NQNFS_CKINVALID(vp, np, ND_WRITE)) {
548			do {
549				error = nqnfs_getlease(vp, ND_WRITE, cred, p);
550			} while (error == NQNFS_EXPIRED);
551			if (error) {
552				brelse(bp);
553				return (error);
554			}
555			if (np->n_lrev != np->n_brev ||
556			    (np->n_flag & NQNFSNONCACHE)) {
557				brelse(bp);
558				error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
559				if (error)
560					return (error);
561				np->n_brev = np->n_lrev;
562				goto again;
563			}
564		}
565		error = uiomove((char *)bp->b_data + on, n, uio);
566		if (error) {
567			bp->b_flags |= B_ERROR;
568			brelse(bp);
569			return (error);
570		}
571		if (bp->b_dirtyend > 0) {
572			bp->b_dirtyoff = min(on, bp->b_dirtyoff);
573			bp->b_dirtyend = max((on + n), bp->b_dirtyend);
574		} else {
575			bp->b_dirtyoff = on;
576			bp->b_dirtyend = on + n;
577		}
578		if (bp->b_validend == 0 || bp->b_validend < bp->b_dirtyoff ||
579		    bp->b_validoff > bp->b_dirtyend) {
580			bp->b_validoff = bp->b_dirtyoff;
581			bp->b_validend = bp->b_dirtyend;
582		} else {
583			bp->b_validoff = min(bp->b_validoff, bp->b_dirtyoff);
584			bp->b_validend = max(bp->b_validend, bp->b_dirtyend);
585		}
586		/*
587		 * If the lease is non-cachable or IO_SYNC do bwrite().
588		 */
589		if ((np->n_flag & NQNFSNONCACHE) || (ioflag & IO_SYNC)) {
590			bp->b_proc = p;
591			error = VOP_BWRITE(bp);
592			if (error)
593				return (error);
594			if (np->n_flag & NQNFSNONCACHE) {
595				error = nfs_vinvalbuf(vp, V_SAVE, cred, p, 1);
596				if (error)
597					return (error);
598			}
599		} else if ((n + on) == biosize &&
600			(nmp->nm_flag & NFSMNT_NQNFS) == 0) {
601			bp->b_proc = (struct proc *)0;
602			bp->b_flags |= B_ASYNC;
603			(void)nfs_writebp(bp, 0);
604		} else
605			bdwrite(bp);
606	} while (uio->uio_resid > 0 && n > 0);
607	return (0);
608}
609
610/*
611 * Get an nfs cache block.
612 * Allocate a new one if the block isn't currently in the cache
613 * and return the block marked busy. If the calling process is
614 * interrupted by a signal for an interruptible mount point, return
615 * NULL.
616 */
617static struct buf *
618nfs_getcacheblk(vp, bn, size, p)
619	struct vnode *vp;
620	daddr_t bn;
621	int size;
622	struct proc *p;
623{
624	register struct buf *bp;
625	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
626	int biosize = vp->v_mount->mnt_stat.f_iosize;
627
628	if (nmp->nm_flag & NFSMNT_INT) {
629		bp = getblk(vp, bn, size, PCATCH, 0);
630		while (bp == (struct buf *)0) {
631			if (nfs_sigintr(nmp, (struct nfsreq *)0, p))
632				return ((struct buf *)0);
633			bp = getblk(vp, bn, size, 0, 2 * hz);
634		}
635	} else
636		bp = getblk(vp, bn, size, 0, 0);
637
638	if( vp->v_type == VREG)
639		bp->b_blkno = (bn * biosize) / DEV_BSIZE;
640
641	return (bp);
642}
643
644/*
645 * Flush and invalidate all dirty buffers. If another process is already
646 * doing the flush, just wait for completion.
647 */
648int
649nfs_vinvalbuf(vp, flags, cred, p, intrflg)
650	struct vnode *vp;
651	int flags;
652	struct ucred *cred;
653	struct proc *p;
654	int intrflg;
655{
656	register struct nfsnode *np = VTONFS(vp);
657	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
658	int error = 0, slpflag, slptimeo;
659
660	if ((nmp->nm_flag & NFSMNT_INT) == 0)
661		intrflg = 0;
662	if (intrflg) {
663		slpflag = PCATCH;
664		slptimeo = 2 * hz;
665	} else {
666		slpflag = 0;
667		slptimeo = 0;
668	}
669	/*
670	 * First wait for any other process doing a flush to complete.
671	 */
672	while (np->n_flag & NFLUSHINPROG) {
673		np->n_flag |= NFLUSHWANT;
674		error = tsleep((caddr_t)&np->n_flag, PRIBIO + 2, "nfsvinval",
675			slptimeo);
676		if (error && intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, p))
677			return (EINTR);
678	}
679
680	/*
681	 * Now, flush as required.
682	 */
683	np->n_flag |= NFLUSHINPROG;
684	error = vinvalbuf(vp, flags, cred, p, slpflag, 0);
685	while (error) {
686		if (intrflg && nfs_sigintr(nmp, (struct nfsreq *)0, p)) {
687			np->n_flag &= ~NFLUSHINPROG;
688			if (np->n_flag & NFLUSHWANT) {
689				np->n_flag &= ~NFLUSHWANT;
690				wakeup((caddr_t)&np->n_flag);
691			}
692			return (EINTR);
693		}
694		error = vinvalbuf(vp, flags, cred, p, 0, slptimeo);
695	}
696	np->n_flag &= ~(NMODIFIED | NFLUSHINPROG);
697	if (np->n_flag & NFLUSHWANT) {
698		np->n_flag &= ~NFLUSHWANT;
699		wakeup((caddr_t)&np->n_flag);
700	}
701	return (0);
702}
703
704/*
705 * Initiate asynchronous I/O. Return an error if no nfsiods are available.
706 * This is mainly to avoid queueing async I/O requests when the nfsiods
707 * are all hung on a dead server.
708 */
709int
710nfs_asyncio(bp, cred)
711	register struct buf *bp;
712	struct ucred *cred;
713{
714	register int i;
715
716	if (nfs_numasync == 0)
717		return (EIO);
718	for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
719	    if (nfs_iodwant[i]) {
720		if (bp->b_flags & B_READ) {
721			if (bp->b_rcred == NOCRED && cred != NOCRED) {
722				crhold(cred);
723				bp->b_rcred = cred;
724			}
725		} else {
726			bp->b_flags |= B_WRITEINPROG;
727			if (bp->b_wcred == NOCRED && cred != NOCRED) {
728				crhold(cred);
729				bp->b_wcred = cred;
730			}
731		}
732
733		TAILQ_INSERT_TAIL(&nfs_bufq, bp, b_freelist);
734		nfs_iodwant[i] = (struct proc *)0;
735		wakeup((caddr_t)&nfs_iodwant[i]);
736		return (0);
737	    }
738
739	/*
740	 * If it is a read or a write already marked B_WRITEINPROG or B_NOCACHE
741	 * return EIO so the process will call nfs_doio() and do it
742	 * synchronously.
743	 */
744	if (bp->b_flags & (B_READ | B_WRITEINPROG | B_NOCACHE))
745		return (EIO);
746
747	/*
748	 * Just turn the async write into a delayed write, instead of
749	 * doing in synchronously. Hopefully, at least one of the nfsiods
750	 * is currently doing a write for this file and will pick up the
751	 * delayed writes before going back to sleep.
752	 */
753	bp->b_flags |= B_DELWRI;
754	reassignbuf(bp, bp->b_vp);
755	biodone(bp);
756	return (0);
757}
758
759/*
760 * Do an I/O operation to/from a cache block. This may be called
761 * synchronously or from an nfsiod.
762 */
763int
764nfs_doio(bp, cr, p)
765	register struct buf *bp;
766	struct ucred *cr;
767	struct proc *p;
768{
769	register struct uio *uiop;
770	register struct vnode *vp;
771	struct nfsnode *np;
772	struct nfsmount *nmp;
773	int error = 0, diff, len, iomode, must_commit = 0;
774	struct uio uio;
775	struct iovec io;
776
777	vp = bp->b_vp;
778	np = VTONFS(vp);
779	nmp = VFSTONFS(vp->v_mount);
780	uiop = &uio;
781	uiop->uio_iov = &io;
782	uiop->uio_iovcnt = 1;
783	uiop->uio_segflg = UIO_SYSSPACE;
784	uiop->uio_procp = p;
785
786	/*
787	 * Historically, paging was done with physio, but no more.
788	 */
789	if (bp->b_flags & B_PHYS) {
790	    /*
791	     * ...though reading /dev/drum still gets us here.
792	     */
793	    io.iov_len = uiop->uio_resid = bp->b_bcount;
794	    /* mapping was done by vmapbuf() */
795	    io.iov_base = bp->b_data;
796	    uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
797	    if (bp->b_flags & B_READ) {
798		uiop->uio_rw = UIO_READ;
799		nfsstats.read_physios++;
800		error = nfs_readrpc(vp, uiop, cr);
801	    } else {
802		int com;
803
804		iomode = NFSV3WRITE_DATASYNC;
805		uiop->uio_rw = UIO_WRITE;
806		nfsstats.write_physios++;
807		error = nfs_writerpc(vp, uiop, cr, &iomode, &com);
808	    }
809	    if (error) {
810		bp->b_flags |= B_ERROR;
811		bp->b_error = error;
812	    }
813	} else if (bp->b_flags & B_READ) {
814	    io.iov_len = uiop->uio_resid = bp->b_bcount;
815	    io.iov_base = bp->b_data;
816	    uiop->uio_rw = UIO_READ;
817	    switch (vp->v_type) {
818	    case VREG:
819		uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE;
820		nfsstats.read_bios++;
821		error = nfs_readrpc(vp, uiop, cr);
822		if (!error) {
823		    bp->b_validoff = 0;
824		    if (uiop->uio_resid) {
825			/*
826			 * If len > 0, there is a hole in the file and
827			 * no writes after the hole have been pushed to
828			 * the server yet.
829			 * Just zero fill the rest of the valid area.
830			 */
831			diff = bp->b_bcount - uiop->uio_resid;
832			len = np->n_size - (((u_quad_t)bp->b_blkno) * DEV_BSIZE
833				+ diff);
834			if (len > 0) {
835			    len = min(len, uiop->uio_resid);
836			    bzero((char *)bp->b_data + diff, len);
837			    bp->b_validend = diff + len;
838			} else
839			    bp->b_validend = diff;
840		    } else
841			bp->b_validend = bp->b_bcount;
842		}
843		if (p && (vp->v_flag & VTEXT) &&
844			(((nmp->nm_flag & NFSMNT_NQNFS) &&
845			  NQNFS_CKINVALID(vp, np, ND_READ) &&
846			  np->n_lrev != np->n_brev) ||
847			 (!(nmp->nm_flag & NFSMNT_NQNFS) &&
848			  np->n_mtime != np->n_vattr.va_mtime.ts_sec))) {
849			uprintf("Process killed due to text file modification\n");
850			psignal(p, SIGKILL);
851#ifdef __NetBSD__
852			p->p_holdcnt++;
853#else
854			p->p_flag |= P_NOSWAP;
855#endif
856		}
857		break;
858	    case VLNK:
859		uiop->uio_offset = (off_t)0;
860		nfsstats.readlink_bios++;
861		error = nfs_readlinkrpc(vp, uiop, cr);
862		break;
863	    case VDIR:
864		nfsstats.readdir_bios++;
865		uiop->uio_offset = ((u_quad_t)bp->b_lblkno) * NFS_DIRBLKSIZ;
866		if (nmp->nm_flag & NFSMNT_RDIRPLUS) {
867			error = nfs_readdirplusrpc(vp, uiop, cr);
868			if (error == NFSERR_NOTSUPP)
869				nmp->nm_flag &= ~NFSMNT_RDIRPLUS;
870		}
871		if ((nmp->nm_flag & NFSMNT_RDIRPLUS) == 0)
872			error = nfs_readdirrpc(vp, uiop, cr);
873		break;
874	    default:
875		printf("nfs_doio:  type %x unexpected\n",vp->v_type);
876		break;
877	    };
878	    if (error) {
879		bp->b_flags |= B_ERROR;
880		bp->b_error = error;
881	    }
882	} else {
883	    if (((bp->b_blkno * DEV_BSIZE) + bp->b_dirtyend) > np->n_size)
884		bp->b_dirtyend = np->n_size - (bp->b_blkno * DEV_BSIZE);
885
886	    if (bp->b_dirtyend > bp->b_dirtyoff) {
887		io.iov_len = uiop->uio_resid = bp->b_dirtyend
888		    - bp->b_dirtyoff;
889		uiop->uio_offset = ((off_t)bp->b_blkno) * DEV_BSIZE
890		    + bp->b_dirtyoff;
891		io.iov_base = (char *)bp->b_data + bp->b_dirtyoff;
892		uiop->uio_rw = UIO_WRITE;
893		nfsstats.write_bios++;
894		if ((bp->b_flags & (B_ASYNC | B_NEEDCOMMIT | B_NOCACHE)) == B_ASYNC)
895		    iomode = NFSV3WRITE_UNSTABLE;
896		else
897		    iomode = NFSV3WRITE_FILESYNC;
898		bp->b_flags |= B_WRITEINPROG;
899		error = nfs_writerpc(vp, uiop, cr, &iomode, &must_commit);
900		if (!error && iomode == NFSV3WRITE_UNSTABLE)
901		    bp->b_flags |= B_NEEDCOMMIT;
902		else
903		    bp->b_flags &= ~B_NEEDCOMMIT;
904		bp->b_flags &= ~B_WRITEINPROG;
905
906		/*
907		 * For an interrupted write, the buffer is still valid
908		 * and the write hasn't been pushed to the server yet,
909		 * so we can't set B_ERROR and report the interruption
910		 * by setting B_EINTR. For the B_ASYNC case, B_EINTR
911		 * is not relevant, so the rpc attempt is essentially
912		 * a noop.  For the case of a V3 write rpc not being
913		 * committed to stable storage, the block is still
914		 * dirty and requires either a commit rpc or another
915		 * write rpc with iomode == NFSV3WRITE_FILESYNC before
916		 * the block is reused. This is indicated by setting
917		 * the B_DELWRI and B_NEEDCOMMIT flags.
918		 */
919    		if (error == EINTR
920		    || (!error && (bp->b_flags & B_NEEDCOMMIT))) {
921			bp->b_flags &= ~(B_INVAL|B_NOCACHE);
922			bp->b_flags |= B_DELWRI;
923
924		/*
925		 * Since for the B_ASYNC case, nfs_bwrite() has reassigned the
926		 * buffer to the clean list, we have to reassign it back to the
927		 * dirty one. Ugh.
928		 */
929			if (bp->b_flags & B_ASYNC)
930				reassignbuf(bp, vp);
931			else
932				bp->b_flags |= B_EINTR;
933	    	} else {
934			if (error) {
935				bp->b_flags |= B_ERROR;
936				bp->b_error = np->n_error = error;
937				np->n_flag |= NWRITEERR;
938			}
939			bp->b_dirtyoff = bp->b_dirtyend = 0;
940		}
941	    } else {
942		bp->b_resid = 0;
943		biodone(bp);
944		return (0);
945	    }
946	}
947	bp->b_resid = uiop->uio_resid;
948	if (must_commit)
949		nfs_clearcommit(vp->v_mount);
950	biodone(bp);
951	return (error);
952}
953