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