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