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