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