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