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