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