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