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