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