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