Home | History | Annotate | Line # | Download | only in nfs
nfs_vfsops.c revision 1.54
      1 /*	$NetBSD: nfs_vfsops.c,v 1.54 1996/12/03 00:22:48 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993, 1995
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Rick Macklem at The University of Guelph.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  *
     38  *	@(#)nfs_vfsops.c	8.12 (Berkeley) 5/20/95
     39  */
     40 
     41 #include <sys/param.h>
     42 #include <sys/conf.h>
     43 #include <sys/ioctl.h>
     44 #include <sys/signal.h>
     45 #include <sys/proc.h>
     46 #include <sys/namei.h>
     47 #include <sys/vnode.h>
     48 #include <sys/kernel.h>
     49 #include <sys/mount.h>
     50 #include <sys/buf.h>
     51 #include <sys/mbuf.h>
     52 #include <sys/socket.h>
     53 #include <sys/socketvar.h>
     54 #include <sys/systm.h>
     55 
     56 #include <net/if.h>
     57 #include <net/route.h>
     58 #include <netinet/in.h>
     59 
     60 #include <nfs/rpcv2.h>
     61 #include <nfs/nfsproto.h>
     62 #include <nfs/nfsnode.h>
     63 #include <nfs/nfs.h>
     64 #include <nfs/nfsmount.h>
     65 #include <nfs/xdr_subs.h>
     66 #include <nfs/nfsm_subs.h>
     67 #include <nfs/nfsdiskless.h>
     68 #include <nfs/nqnfs.h>
     69 #include <nfs/nfs_var.h>
     70 
     71 extern struct nfsstats nfsstats;
     72 extern int nfs_ticks;
     73 
     74 #ifdef notyet
     75 static int nfs_sysctl(int *, u_int, void *, size_t *, void *, size_t,
     76 		      struct proc *);
     77 #endif
     78 
     79 /*
     80  * nfs vfs operations.
     81  */
     82 struct vfsops nfs_vfsops = {
     83 	MOUNT_NFS,
     84 	nfs_mount,
     85 	nfs_start,
     86 	nfs_unmount,
     87 	nfs_root,
     88 	nfs_quotactl,
     89 	nfs_statfs,
     90 	nfs_sync,
     91 	nfs_vget,
     92 	nfs_fhtovp,
     93 	nfs_vptofh,
     94 	nfs_vfs_init,
     95 #ifdef notyet
     96 	nfs_sysctl
     97 #endif
     98 };
     99 
    100 extern u_int32_t nfs_procids[NFS_NPROCS];
    101 extern u_int32_t nfs_prog, nfs_vers;
    102 
    103 static struct mount *
    104 nfs_mount_diskless __P((struct nfs_dlmount *, char *, int, struct vnode **));
    105 
    106 #define TRUE	1
    107 #define	FALSE	0
    108 
    109 /*
    110  * nfs statfs call
    111  */
    112 int
    113 nfs_statfs(mp, sbp, p)
    114 	struct mount *mp;
    115 	register struct statfs *sbp;
    116 	struct proc *p;
    117 {
    118 	register struct vnode *vp;
    119 	register struct nfs_statfs *sfp;
    120 	register caddr_t cp;
    121 	register u_int32_t *tl;
    122 	register int32_t t1, t2;
    123 	caddr_t bpos, dpos, cp2;
    124 	struct nfsmount *nmp = VFSTONFS(mp);
    125 	int error = 0, v3 = (nmp->nm_flag & NFSMNT_NFSV3), retattr;
    126 	struct mbuf *mreq, *mrep = NULL, *md, *mb, *mb2;
    127 	struct ucred *cred;
    128 	struct nfsnode *np;
    129 	u_quad_t tquad;
    130 
    131 #ifndef nolint
    132 	sfp = (struct nfs_statfs *)0;
    133 #endif
    134 	error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
    135 	if (error)
    136 		return (error);
    137 	vp = NFSTOV(np);
    138 	cred = crget();
    139 	cred->cr_ngroups = 0;
    140 	if (v3 && (nmp->nm_flag & NFSMNT_GOTFSINFO) == 0)
    141 		(void)nfs_fsinfo(nmp, vp, cred, p);
    142 	nfsstats.rpccnt[NFSPROC_FSSTAT]++;
    143 	nfsm_reqhead(vp, NFSPROC_FSSTAT, NFSX_FH(v3));
    144 	nfsm_fhtom(vp, v3);
    145 	nfsm_request(vp, NFSPROC_FSSTAT, p, cred);
    146 	if (v3)
    147 		nfsm_postop_attr(vp, retattr);
    148 	if (error) {
    149 		if (mrep != NULL)
    150 			m_free(mrep);
    151 		goto nfsmout;
    152 	}
    153 	nfsm_dissect(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
    154 #ifdef COMPAT_09
    155 	sbp->f_type = 2;
    156 #else
    157 	sbp->f_type = 0;
    158 #endif
    159 	sbp->f_flags = nmp->nm_flag;
    160 	sbp->f_iosize = min(nmp->nm_rsize, nmp->nm_wsize);
    161 	if (v3) {
    162 		sbp->f_bsize = NFS_FABLKSIZE;
    163 		fxdr_hyper(&sfp->sf_tbytes, &tquad);
    164 		sbp->f_blocks = (long)(tquad / ((u_quad_t)NFS_FABLKSIZE));
    165 		fxdr_hyper(&sfp->sf_fbytes, &tquad);
    166 		sbp->f_bfree = (long)(tquad / ((u_quad_t)NFS_FABLKSIZE));
    167 		fxdr_hyper(&sfp->sf_abytes, &tquad);
    168 		sbp->f_bavail = (long)(tquad / ((u_quad_t)NFS_FABLKSIZE));
    169 		sbp->f_files = (fxdr_unsigned(int32_t,
    170 		    sfp->sf_tfiles.nfsuquad[1]) & 0x7fffffff);
    171 		sbp->f_ffree = (fxdr_unsigned(int32_t,
    172 		    sfp->sf_ffiles.nfsuquad[1]) & 0x7fffffff);
    173 	} else {
    174 		sbp->f_bsize = fxdr_unsigned(int32_t, sfp->sf_bsize);
    175 		sbp->f_blocks = fxdr_unsigned(int32_t, sfp->sf_blocks);
    176 		sbp->f_bfree = fxdr_unsigned(int32_t, sfp->sf_bfree);
    177 		sbp->f_bavail = fxdr_unsigned(int32_t, sfp->sf_bavail);
    178 		sbp->f_files = 0;
    179 		sbp->f_ffree = 0;
    180 	}
    181 	if (sbp != &mp->mnt_stat) {
    182 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
    183 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
    184 	}
    185 	strncpy(&sbp->f_fstypename[0], mp->mnt_op->vfs_name, MFSNAMELEN);
    186 	nfsm_reqdone;
    187 	vrele(vp);
    188 	crfree(cred);
    189 	return (error);
    190 }
    191 
    192 /*
    193  * nfs version 3 fsinfo rpc call
    194  */
    195 int
    196 nfs_fsinfo(nmp, vp, cred, p)
    197 	register struct nfsmount *nmp;
    198 	register struct vnode *vp;
    199 	struct ucred *cred;
    200 	struct proc *p;
    201 {
    202 	register struct nfsv3_fsinfo *fsp;
    203 	register caddr_t cp;
    204 	register int32_t t1, t2;
    205 	register u_int32_t *tl, pref, max;
    206 	caddr_t bpos, dpos, cp2;
    207 	int error = 0, retattr;
    208 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
    209 
    210 	nfsstats.rpccnt[NFSPROC_FSINFO]++;
    211 	nfsm_reqhead(vp, NFSPROC_FSINFO, NFSX_FH(1));
    212 	nfsm_fhtom(vp, 1);
    213 	nfsm_request(vp, NFSPROC_FSINFO, p, cred);
    214 	nfsm_postop_attr(vp, retattr);
    215 	if (!error) {
    216 		nfsm_dissect(fsp, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
    217 		pref = fxdr_unsigned(u_int32_t, fsp->fs_wtpref);
    218 		if (pref < nmp->nm_wsize)
    219 			nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) &
    220 				~(NFS_FABLKSIZE - 1);
    221 		max = fxdr_unsigned(u_int32_t, fsp->fs_wtmax);
    222 		if (max < nmp->nm_wsize) {
    223 			nmp->nm_wsize = max & ~(NFS_FABLKSIZE - 1);
    224 			if (nmp->nm_wsize == 0)
    225 				nmp->nm_wsize = max;
    226 		}
    227 		pref = fxdr_unsigned(u_int32_t, fsp->fs_rtpref);
    228 		if (pref < nmp->nm_rsize)
    229 			nmp->nm_rsize = (pref + NFS_FABLKSIZE - 1) &
    230 				~(NFS_FABLKSIZE - 1);
    231 		max = fxdr_unsigned(u_int32_t, fsp->fs_rtmax);
    232 		if (max < nmp->nm_rsize) {
    233 			nmp->nm_rsize = max & ~(NFS_FABLKSIZE - 1);
    234 			if (nmp->nm_rsize == 0)
    235 				nmp->nm_rsize = max;
    236 		}
    237 		pref = fxdr_unsigned(u_int32_t, fsp->fs_dtpref);
    238 		if (pref < nmp->nm_readdirsize)
    239 			nmp->nm_readdirsize = (pref + NFS_DIRBLKSIZ - 1) &
    240 				~(NFS_DIRBLKSIZ - 1);
    241 		if (max < nmp->nm_readdirsize) {
    242 			nmp->nm_readdirsize = max & ~(NFS_DIRBLKSIZ - 1);
    243 			if (nmp->nm_readdirsize == 0)
    244 				nmp->nm_readdirsize = max;
    245 		}
    246 		nmp->nm_flag |= NFSMNT_GOTFSINFO;
    247 	}
    248 	nfsm_reqdone;
    249 	return (error);
    250 }
    251 
    252 /*
    253  * Mount a remote root fs via. NFS.  It goes like this:
    254  * - Call nfs_boot_init() to fill in the nfs_diskless struct
    255  *   (using RARP, bootparam RPC, mountd RPC)
    256  * - hand craft the swap nfs vnode hanging off a fake mount point
    257  *	if swdevt[0].sw_dev == NODEV
    258  * - build the rootfs mount point and call mountnfs() to do the rest.
    259  */
    260 int
    261 nfs_mountroot()
    262 {
    263 	struct nfs_diskless nd;
    264 	struct vattr attr;
    265 	struct mount *mp;
    266 	struct vnode *vp;
    267 	struct proc *procp;
    268 	long n;
    269 	int error;
    270 
    271 	procp = curproc; /* XXX */
    272 
    273 	/*
    274 	 * XXX time must be non-zero when we init the interface or else
    275 	 * the arp code will wedge.  [Fixed now in if_ether.c]
    276 	 * However, the NFS attribute cache gives false "hits" when
    277 	 * time.tv_sec < NFS_ATTRTIMEO(np) so keep this in for now.
    278 	 */
    279 	if (time.tv_sec < NFS_MAXATTRTIMO)
    280 		time.tv_sec = NFS_MAXATTRTIMO;
    281 
    282 	/*
    283 	 * Call nfs_boot_init() to fill in the nfs_diskless struct.
    284 	 * Side effect:  Finds and configures a network interface.
    285 	 */
    286 	bzero((caddr_t) &nd, sizeof(nd));
    287 	nfs_boot_init(&nd, procp);
    288 
    289 	/*
    290 	 * Create the root mount point.
    291 	 */
    292 	nfs_boot_getfh(&nd.nd_boot, "root", &nd.nd_root);
    293 	mp = nfs_mount_diskless(&nd.nd_root, "/", 0, &vp);
    294 	printf("root on %s\n", nd.nd_root.ndm_host);
    295 
    296 	/*
    297 	 * Link it into the mount list.
    298 	 */
    299 #ifdef Lite2_integrated
    300 	simple_lock(&mountlist_slock);
    301 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    302 	simple_unlock(&mountlist_slock);
    303 	rootvp = vp;
    304 	vfs_unbusy(mp, procp);
    305 #else
    306 	if (vfs_lock(mp))
    307 		panic("nfs_mountroot: vfs_lock");
    308 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    309 	mp->mnt_vnodecovered = NULLVP;
    310 	vfs_unlock(mp);
    311 	rootvp = vp;
    312 #endif
    313 
    314 	/* Get root attributes (for the time). */
    315 	error = VOP_GETATTR(vp, &attr, procp->p_ucred, procp);
    316 	if (error) panic("nfs_mountroot: getattr for root");
    317 	n = attr.va_mtime.tv_sec;
    318 #ifdef	DEBUG
    319 	printf("root time: 0x%lx\n", n);
    320 #endif
    321 	inittodr(n);
    322 
    323 	/*
    324 	 * XXX splnet, so networks will receive...
    325 	 */
    326 	splnet();
    327 
    328 #ifdef notyet
    329 	/* Set up swap credentials. */
    330 	proc0.p_ucred->cr_uid = ntohl(nd.swap_ucred.cr_uid);
    331 	proc0.p_ucred->cr_gid = ntohl(nd.swap_ucred.cr_gid);
    332 	if ((proc0.p_ucred->cr_ngroups = ntohs(nd.swap_ucred.cr_ngroups)) >
    333 		NGROUPS)
    334 		proc0.p_ucred->cr_ngroups = NGROUPS;
    335 	for (i = 0; i < proc0.p_ucred->cr_ngroups; i++)
    336 	    proc0.p_ucred->cr_groups[i] = ntohl(nd.swap_ucred.cr_groups[i]);
    337 #endif
    338 
    339 	/*
    340 	 * "Mount" the swap device.
    341 	 *
    342 	 * On a "dataless" configuration (swap on disk) we will have:
    343 	 *	(swdevt[0].sw_dev != NODEV) identifying the swap device.
    344 	 */
    345 	if (bdevvp(swapdev, &swapdev_vp))
    346 		panic("nfs_mountroot: can't setup swap vp");
    347 	if (swdevt[0].sw_dev != NODEV) {
    348 		printf("swap on device 0x%x\n", swdevt[0].sw_dev);
    349 		return (0);
    350 	}
    351 
    352 	/*
    353 	 * If swapping to an nfs node:  (swdevt[0].sw_dev == NODEV)
    354 	 * Create a fake mount point just for the swap vnode so that the
    355 	 * swap file can be on a different server from the rootfs.
    356 	 */
    357 	nfs_boot_getfh(&nd.nd_boot, "swap", &nd.nd_swap);
    358 	mp = nfs_mount_diskless(&nd.nd_swap, "/swap", 0, &vp);
    359 #ifdef Lite2_integrated
    360 	vfs_unbusy(mp, procp);
    361 #endif
    362 	printf("swap on %s\n", nd.nd_swap.ndm_host);
    363 
    364 	/*
    365 	 * Since the swap file is not the root dir of a file system,
    366 	 * hack it to a regular file.
    367 	 */
    368 	vp->v_type = VREG;
    369 	vp->v_flag = 0;
    370 	swdevt[0].sw_vp = vp;
    371 
    372 	/*
    373 	 * Find out how large the swap file is.
    374 	 */
    375 	error = VOP_GETATTR(vp, &attr, procp->p_ucred, procp);
    376 	if (error)
    377 		panic("nfs_mountroot: getattr for swap");
    378 	n = (long) (attr.va_size >> DEV_BSHIFT);
    379 #ifdef	DEBUG
    380 	printf("swap size: 0x%lx (blocks)\n", n);
    381 #endif
    382 	swdevt[0].sw_nblks = n;
    383 
    384 	return (0);
    385 }
    386 
    387 /*
    388  * Internal version of mount system call for diskless setup.
    389  */
    390 static struct mount *
    391 nfs_mount_diskless(ndmntp, mntname, mntflag, vpp)
    392 	struct nfs_dlmount *ndmntp;
    393 	char *mntname;
    394 	int mntflag;
    395 	struct vnode **vpp;
    396 {
    397 	struct mount *mp;
    398 	struct mbuf *m;
    399 	int error;
    400 
    401 #ifdef Lite2_integrated
    402 	vfs_rootmountalloc("nfs", mntname, &mp);
    403 #else
    404 	/* Create the mount point. */
    405 	mp = (struct mount *)malloc((u_long)sizeof(struct mount),
    406 	    M_MOUNT, M_WAITOK);
    407 	bzero((char *)mp, (u_long)sizeof(struct mount));
    408 #endif
    409 	mp->mnt_op = &nfs_vfsops;
    410 	mp->mnt_flag = mntflag;
    411 
    412 	/* Get mbuf for server sockaddr. */
    413 	m = m_get(M_WAIT, MT_SONAME);
    414 	if (m == NULL)
    415 		panic("nfs_mountroot: mget soname for %s", mntname);
    416 	bcopy((caddr_t)ndmntp->ndm_args.addr, mtod(m, caddr_t),
    417 	      (m->m_len = ndmntp->ndm_args.addr->sa_len));
    418 
    419 	error = mountnfs(&ndmntp->ndm_args, mp, m, mntname,
    420 			 ndmntp->ndm_args.hostname, vpp);
    421 	if (error)
    422 		panic("nfs_mountroot: mount %s failed: %d", mntname, error);
    423 
    424 	return (mp);
    425 }
    426 
    427 void
    428 nfs_decode_args(nmp, argp)
    429 	struct nfsmount *nmp;
    430 	struct nfs_args *argp;
    431 {
    432 	int s;
    433 	int adjsock;
    434 	int maxio;
    435 
    436 	s = splsoftnet();
    437 
    438 	/* Re-bind if rsrvd port requested and wasn't on one */
    439 	adjsock = !(nmp->nm_flag & NFSMNT_RESVPORT)
    440 		  && (argp->flags & NFSMNT_RESVPORT);
    441 
    442 	/* Update flags atomically.  Don't change the lock bits. */
    443 	nmp->nm_flag =
    444 	    (argp->flags & ~NFSMNT_INTERNAL) | (nmp->nm_flag & NFSMNT_INTERNAL);
    445 	splx(s);
    446 
    447 	if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
    448 		nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
    449 		if (nmp->nm_timeo < NFS_MINTIMEO)
    450 			nmp->nm_timeo = NFS_MINTIMEO;
    451 		else if (nmp->nm_timeo > NFS_MAXTIMEO)
    452 			nmp->nm_timeo = NFS_MAXTIMEO;
    453 	}
    454 
    455 	if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
    456 		nmp->nm_retry = argp->retrans;
    457 		if (nmp->nm_retry > NFS_MAXREXMIT)
    458 			nmp->nm_retry = NFS_MAXREXMIT;
    459 	}
    460 
    461 	if (argp->flags & NFSMNT_NFSV3) {
    462 		if (argp->sotype == SOCK_DGRAM)
    463 			maxio = NFS_MAXDGRAMDATA;
    464 		else
    465 			maxio = NFS_MAXDATA;
    466 	} else
    467 		maxio = NFS_V2MAXDATA;
    468 
    469 	if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
    470 		int osize = nmp->nm_wsize;
    471 		nmp->nm_wsize = argp->wsize;
    472 		/* Round down to multiple of blocksize */
    473 		nmp->nm_wsize &= ~(NFS_FABLKSIZE - 1);
    474 		if (nmp->nm_wsize <= 0)
    475 			nmp->nm_wsize = NFS_FABLKSIZE;
    476 		adjsock |= (nmp->nm_wsize != osize);
    477 	}
    478 	if (nmp->nm_wsize > maxio)
    479 		nmp->nm_wsize = maxio;
    480 	if (nmp->nm_wsize > MAXBSIZE)
    481 		nmp->nm_wsize = MAXBSIZE;
    482 
    483 	if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
    484 		int osize = nmp->nm_rsize;
    485 		nmp->nm_rsize = argp->rsize;
    486 		/* Round down to multiple of blocksize */
    487 		nmp->nm_rsize &= ~(NFS_FABLKSIZE - 1);
    488 		if (nmp->nm_rsize <= 0)
    489 			nmp->nm_rsize = NFS_FABLKSIZE;
    490 		adjsock |= (nmp->nm_rsize != osize);
    491 	}
    492 	if (nmp->nm_rsize > maxio)
    493 		nmp->nm_rsize = maxio;
    494 	if (nmp->nm_rsize > MAXBSIZE)
    495 		nmp->nm_rsize = MAXBSIZE;
    496 
    497 	if ((argp->flags & NFSMNT_READDIRSIZE) && argp->readdirsize > 0) {
    498 		nmp->nm_readdirsize = argp->readdirsize;
    499 		/* Round down to multiple of blocksize */
    500 		nmp->nm_readdirsize &= ~(NFS_DIRBLKSIZ - 1);
    501 		if (nmp->nm_readdirsize < NFS_DIRBLKSIZ)
    502 			nmp->nm_readdirsize = NFS_DIRBLKSIZ;
    503 	} else if (argp->flags & NFSMNT_RSIZE)
    504 		nmp->nm_readdirsize = nmp->nm_rsize;
    505 
    506 	if (nmp->nm_readdirsize > maxio)
    507 		nmp->nm_readdirsize = maxio;
    508 
    509 	if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0 &&
    510 		argp->maxgrouplist <= NFS_MAXGRPS)
    511 		nmp->nm_numgrps = argp->maxgrouplist;
    512 	if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0 &&
    513 		argp->readahead <= NFS_MAXRAHEAD)
    514 		nmp->nm_readahead = argp->readahead;
    515 	if ((argp->flags & NFSMNT_LEASETERM) && argp->leaseterm >= 2 &&
    516 		argp->leaseterm <= NQ_MAXLEASE)
    517 		nmp->nm_leaseterm = argp->leaseterm;
    518 	if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 1 &&
    519 		argp->deadthresh <= NQ_NEVERDEAD)
    520 		nmp->nm_deadthresh = argp->deadthresh;
    521 
    522 	adjsock |= ((nmp->nm_sotype != argp->sotype) ||
    523 		    (nmp->nm_soproto != argp->proto));
    524 	nmp->nm_sotype = argp->sotype;
    525 	nmp->nm_soproto = argp->proto;
    526 
    527 	if (nmp->nm_so && adjsock) {
    528 		nfs_disconnect(nmp);
    529 		if (nmp->nm_sotype == SOCK_DGRAM)
    530 			while (nfs_connect(nmp, (struct nfsreq *)0)) {
    531 				printf("nfs_args: retrying connect\n");
    532 				(void) tsleep((caddr_t)&lbolt,
    533 					      PSOCK, "nfscon", 0);
    534 			}
    535 	}
    536 }
    537 
    538 /*
    539  * VFS Operations.
    540  *
    541  * mount system call
    542  * It seems a bit dumb to copyinstr() the host and path here and then
    543  * bcopy() them in mountnfs(), but I wanted to detect errors before
    544  * doing the sockargs() call because sockargs() allocates an mbuf and
    545  * an error after that means that I have to release the mbuf.
    546  */
    547 /* ARGSUSED */
    548 int
    549 nfs_mount(mp, path, data, ndp, p)
    550 	struct mount *mp;
    551 	char *path;
    552 	caddr_t data;
    553 	struct nameidata *ndp;
    554 	struct proc *p;
    555 {
    556 	int error;
    557 	struct nfs_args args;
    558 	struct mbuf *nam;
    559 	struct vnode *vp;
    560 	char pth[MNAMELEN], hst[MNAMELEN];
    561 	size_t len;
    562 	u_char nfh[NFSX_V3FHMAX];
    563 
    564 	error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args));
    565 	if (error)
    566 		return (error);
    567 	if (args.version != NFS_ARGSVERSION)
    568 		return (EPROGMISMATCH);
    569 	if (mp->mnt_flag & MNT_UPDATE) {
    570 		register struct nfsmount *nmp = VFSTONFS(mp);
    571 
    572 		if (nmp == NULL)
    573 			return (EIO);
    574 		/*
    575 		 * When doing an update, we can't change from or to
    576 		 * v3 and/or nqnfs.
    577 		 */
    578 		args.flags = (args.flags & ~(NFSMNT_NFSV3|NFSMNT_NQNFS)) |
    579 		    (nmp->nm_flag & (NFSMNT_NFSV3|NFSMNT_NQNFS));
    580 		nfs_decode_args(nmp, &args);
    581 		return (0);
    582 	}
    583 	error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize);
    584 	if (error)
    585 		return (error);
    586 	error = copyinstr(path, pth, MNAMELEN-1, &len);
    587 	if (error)
    588 		return (error);
    589 	bzero(&pth[len], MNAMELEN - len);
    590 	error = copyinstr(args.hostname, hst, MNAMELEN-1, &len);
    591 	if (error)
    592 		return (error);
    593 	bzero(&hst[len], MNAMELEN - len);
    594 	/* sockargs() call must be after above copyin() calls */
    595 	error = sockargs(&nam, (caddr_t)args.addr, args.addrlen, MT_SONAME);
    596 	if (error)
    597 		return (error);
    598 	args.fh = nfh;
    599 	error = mountnfs(&args, mp, nam, pth, hst, &vp);
    600 	return (error);
    601 }
    602 
    603 /*
    604  * Common code for mount and mountroot
    605  */
    606 int
    607 mountnfs(argp, mp, nam, pth, hst, vpp)
    608 	register struct nfs_args *argp;
    609 	register struct mount *mp;
    610 	struct mbuf *nam;
    611 	char *pth, *hst;
    612 	struct vnode **vpp;
    613 {
    614 	register struct nfsmount *nmp;
    615 	struct nfsnode *np;
    616 	int error;
    617 	struct vattr attrs;
    618 
    619 	if (mp->mnt_flag & MNT_UPDATE) {
    620 		nmp = VFSTONFS(mp);
    621 		/* update paths, file handles, etc, here	XXX */
    622 		m_freem(nam);
    623 		return (0);
    624 	} else {
    625 		MALLOC(nmp, struct nfsmount *, sizeof (struct nfsmount),
    626 		    M_NFSMNT, M_WAITOK);
    627 		bzero((caddr_t)nmp, sizeof (struct nfsmount));
    628 		mp->mnt_data = (qaddr_t)nmp;
    629 		TAILQ_INIT(&nmp->nm_uidlruhead);
    630 		TAILQ_INIT(&nmp->nm_bufq);
    631 	}
    632 #ifdef Lite2_integrated
    633 	vfs_getnewfsid(mp, makefstype(MOUNT_NFS));
    634 #else
    635 	getnewfsid(mp, makefstype(MOUNT_NFS));
    636 #endif
    637 	nmp->nm_mountp = mp;
    638 	if (argp->flags & NFSMNT_NQNFS)
    639 		/*
    640 		 * We have to set mnt_maxsymlink to a non-zero value so
    641 		 * that COMPAT_43 routines will know that we are setting
    642 		 * the d_type field in directories (and can zero it for
    643 		 * unsuspecting binaries).
    644 		 */
    645 		mp->mnt_maxsymlinklen = 1;
    646 	nmp->nm_timeo = NFS_TIMEO;
    647 	nmp->nm_retry = NFS_RETRANS;
    648 	nmp->nm_wsize = NFS_WSIZE;
    649 	nmp->nm_rsize = NFS_RSIZE;
    650 	nmp->nm_readdirsize = NFS_READDIRSIZE;
    651 	nmp->nm_numgrps = NFS_MAXGRPS;
    652 	nmp->nm_readahead = NFS_DEFRAHEAD;
    653 	nmp->nm_leaseterm = NQ_DEFLEASE;
    654 	nmp->nm_deadthresh = NQ_DEADTHRESH;
    655 	CIRCLEQ_INIT(&nmp->nm_timerhead);
    656 	nmp->nm_inprog = NULLVP;
    657 	nmp->nm_fhsize = argp->fhsize;
    658 	bcopy((caddr_t)argp->fh, (caddr_t)nmp->nm_fh, argp->fhsize);
    659 #ifdef COMPAT_09
    660 	mp->mnt_stat.f_type = 2;
    661 #else
    662 	mp->mnt_stat.f_type = 0;
    663 #endif
    664 	strncpy(&mp->mnt_stat.f_fstypename[0], mp->mnt_op->vfs_name, MFSNAMELEN);
    665 	bcopy(hst, mp->mnt_stat.f_mntfromname, MNAMELEN);
    666 	bcopy(pth, mp->mnt_stat.f_mntonname, MNAMELEN);
    667 	nmp->nm_nam = nam;
    668 
    669 	/* Set up the sockets and per-host congestion */
    670 	nmp->nm_sotype = argp->sotype;
    671 	nmp->nm_soproto = argp->proto;
    672 
    673 	nfs_decode_args(nmp, argp);
    674 
    675 	/*
    676 	 * For Connection based sockets (TCP,...) defer the connect until
    677 	 * the first request, in case the server is not responding.
    678 	 */
    679 	if (nmp->nm_sotype == SOCK_DGRAM &&
    680 		(error = nfs_connect(nmp, (struct nfsreq *)0)))
    681 		goto bad;
    682 
    683 	/*
    684 	 * This is silly, but it has to be set so that vinifod() works.
    685 	 * We do not want to do an nfs_statfs() here since we can get
    686 	 * stuck on a dead server and we are holding a lock on the mount
    687 	 * point.
    688 	 */
    689 	mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA;
    690 	/*
    691 	 * A reference count is needed on the nfsnode representing the
    692 	 * remote root.  If this object is not persistent, then backward
    693 	 * traversals of the mount point (i.e. "..") will not work if
    694 	 * the nfsnode gets flushed out of the cache. Ufs does not have
    695 	 * this problem, because one can identify root inodes by their
    696 	 * number == ROOTINO (2).
    697 	 */
    698 	error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
    699 	if (error)
    700 		goto bad;
    701 	*vpp = NFSTOV(np);
    702 	VOP_GETATTR(*vpp, &attrs, curproc->p_ucred, curproc); /* XXX */
    703 
    704 	return (0);
    705 bad:
    706 	nfs_disconnect(nmp);
    707 	free((caddr_t)nmp, M_NFSMNT);
    708 	m_freem(nam);
    709 	return (error);
    710 }
    711 
    712 /*
    713  * unmount system call
    714  */
    715 int
    716 nfs_unmount(mp, mntflags, p)
    717 	struct mount *mp;
    718 	int mntflags;
    719 	struct proc *p;
    720 {
    721 	register struct nfsmount *nmp;
    722 	struct nfsnode *np;
    723 	struct vnode *vp;
    724 	int error, flags = 0;
    725 
    726 	if (mntflags & MNT_FORCE)
    727 		flags |= FORCECLOSE;
    728 	nmp = VFSTONFS(mp);
    729 	/*
    730 	 * Goes something like this..
    731 	 * - Check for activity on the root vnode (other than ourselves).
    732 	 * - Call vflush() to clear out vnodes for this file system,
    733 	 *   except for the root vnode.
    734 	 * - Decrement reference on the vnode representing remote root.
    735 	 * - Close the socket
    736 	 * - Free up the data structures
    737 	 */
    738 	/*
    739 	 * We need to decrement the ref. count on the nfsnode representing
    740 	 * the remote root.  See comment in mountnfs().  The VFS unmount()
    741 	 * has done vput on this vnode, otherwise we would get deadlock!
    742 	 */
    743 	error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
    744 	if (error)
    745 		return(error);
    746 	vp = NFSTOV(np);
    747 	if ((mntflags & MNT_FORCE) == 0 && vp->v_usecount > 2) {
    748 		vput(vp);
    749 		return (EBUSY);
    750 	}
    751 
    752 	/*
    753 	 * Must handshake with nqnfs_clientd() if it is active.
    754 	 */
    755 	nmp->nm_flag |= NFSMNT_DISMINPROG;
    756 	while (nmp->nm_inprog != NULLVP)
    757 		(void) tsleep((caddr_t)&lbolt, PSOCK, "nfsdism", 0);
    758 	error = vflush(mp, vp, flags);
    759 	if (error) {
    760 		vput(vp);
    761 		nmp->nm_flag &= ~NFSMNT_DISMINPROG;
    762 		return (error);
    763 	}
    764 
    765 	/*
    766 	 * We are now committed to the unmount.
    767 	 * For NQNFS, let the server daemon free the nfsmount structure.
    768 	 */
    769 	if (nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB))
    770 		nmp->nm_flag |= NFSMNT_DISMNT;
    771 
    772 	/*
    773 	 * There are two reference counts to get rid of here.
    774 	 */
    775 	vrele(vp);
    776 	vrele(vp);
    777 	vgone(vp);
    778 	nfs_disconnect(nmp);
    779 	m_freem(nmp->nm_nam);
    780 
    781 	if ((nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB)) == 0)
    782 		free((caddr_t)nmp, M_NFSMNT);
    783 	return (0);
    784 }
    785 
    786 /*
    787  * Return root of a filesystem
    788  */
    789 int
    790 nfs_root(mp, vpp)
    791 	struct mount *mp;
    792 	struct vnode **vpp;
    793 {
    794 	register struct vnode *vp;
    795 	struct nfsmount *nmp;
    796 	struct nfsnode *np;
    797 	int error;
    798 
    799 	nmp = VFSTONFS(mp);
    800 	error = nfs_nget(mp, (nfsfh_t *)nmp->nm_fh, nmp->nm_fhsize, &np);
    801 	if (error)
    802 		return (error);
    803 	vp = NFSTOV(np);
    804 	if (vp->v_type == VNON)
    805 		vp->v_type = VDIR;
    806 	vp->v_flag = VROOT;
    807 	*vpp = vp;
    808 	return (0);
    809 }
    810 
    811 extern int syncprt;
    812 
    813 /*
    814  * Flush out the buffer cache
    815  */
    816 /* ARGSUSED */
    817 int
    818 nfs_sync(mp, waitfor, cred, p)
    819 	struct mount *mp;
    820 	int waitfor;
    821 	struct ucred *cred;
    822 	struct proc *p;
    823 {
    824 	register struct vnode *vp;
    825 	int error, allerror = 0;
    826 
    827 	/*
    828 	 * Force stale buffer cache information to be flushed.
    829 	 */
    830 loop:
    831 	for (vp = mp->mnt_vnodelist.lh_first;
    832 	     vp != NULL;
    833 	     vp = vp->v_mntvnodes.le_next) {
    834 		/*
    835 		 * If the vnode that we are about to sync is no longer
    836 		 * associated with this mount point, start over.
    837 		 */
    838 		if (vp->v_mount != mp)
    839 			goto loop;
    840 		if (VOP_ISLOCKED(vp) || vp->v_dirtyblkhd.lh_first == NULL)
    841 			continue;
    842 #ifdef Lite2_integrated
    843 		if (vget(vp, LK_EXCLUSIVE, p))
    844 #else
    845 		if (vget(vp, 1))
    846 #endif
    847 			goto loop;
    848 		error = VOP_FSYNC(vp, cred, waitfor, p);
    849 		if (error)
    850 			allerror = error;
    851 		vput(vp);
    852 	}
    853 	return (allerror);
    854 }
    855 
    856 /*
    857  * NFS flat namespace lookup.
    858  * Currently unsupported.
    859  */
    860 /* ARGSUSED */
    861 int
    862 nfs_vget(mp, ino, vpp)
    863 	struct mount *mp;
    864 	ino_t ino;
    865 	struct vnode **vpp;
    866 {
    867 
    868 	return (EOPNOTSUPP);
    869 }
    870 
    871 #ifdef notyet
    872 /*
    873  * Do that sysctl thang...
    874  */
    875 static int
    876 nfs_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
    877 	   size_t newlen, struct proc *p)
    878 {
    879 	int rv;
    880 
    881 	/*
    882 	 * All names at this level are terminal.
    883 	 */
    884 	if(namelen > 1)
    885 		return ENOTDIR;	/* overloaded */
    886 
    887 	switch(name[0]) {
    888 	case NFS_NFSSTATS:
    889 		if(!oldp) {
    890 			*oldlenp = sizeof nfsstats;
    891 			return 0;
    892 		}
    893 
    894 		if(*oldlenp < sizeof nfsstats) {
    895 			*oldlenp = sizeof nfsstats;
    896 			return ENOMEM;
    897 		}
    898 
    899 		rv = copyout(&nfsstats, oldp, sizeof nfsstats);
    900 		if(rv) return rv;
    901 
    902 		if(newp && newlen != sizeof nfsstats)
    903 			return EINVAL;
    904 
    905 		if(newp) {
    906 			return copyin(newp, &nfsstats, sizeof nfsstats);
    907 		}
    908 		return 0;
    909 
    910 	default:
    911 		return EOPNOTSUPP;
    912 	}
    913 }
    914 #endif
    915 
    916 
    917 /*
    918  * At this point, this should never happen
    919  */
    920 /* ARGSUSED */
    921 int
    922 nfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
    923 	register struct mount *mp;
    924 	struct fid *fhp;
    925 	struct mbuf *nam;
    926 	struct vnode **vpp;
    927 	int *exflagsp;
    928 	struct ucred **credanonp;
    929 {
    930 
    931 	return (EINVAL);
    932 }
    933 
    934 /*
    935  * Vnode pointer to File handle, should never happen either
    936  */
    937 /* ARGSUSED */
    938 int
    939 nfs_vptofh(vp, fhp)
    940 	struct vnode *vp;
    941 	struct fid *fhp;
    942 {
    943 
    944 	return (EINVAL);
    945 }
    946 
    947 /*
    948  * Vfs start routine, a no-op.
    949  */
    950 /* ARGSUSED */
    951 int
    952 nfs_start(mp, flags, p)
    953 	struct mount *mp;
    954 	int flags;
    955 	struct proc *p;
    956 {
    957 
    958 	return (0);
    959 }
    960 
    961 /*
    962  * Do operations associated with quotas, not supported
    963  */
    964 /* ARGSUSED */
    965 int
    966 nfs_quotactl(mp, cmd, uid, arg, p)
    967 	struct mount *mp;
    968 	int cmd;
    969 	uid_t uid;
    970 	caddr_t arg;
    971 	struct proc *p;
    972 {
    973 
    974 	return (EOPNOTSUPP);
    975 }
    976