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