Home | History | Annotate | Line # | Download | only in nfs
nfs_vfsops.c revision 1.122
      1 /*	$NetBSD: nfs_vfsops.c,v 1.122 2003/02/26 06:31:20 matt 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.122 2003/02/26 06:31:20 matt 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 	if (sbp != &mp->mnt_stat) {
    209 		memcpy(sbp->f_mntonname, mp->mnt_stat.f_mntonname, MNAMELEN);
    210 		memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname, MNAMELEN);
    211 	}
    212 	strncpy(&sbp->f_fstypename[0], mp->mnt_op->vfs_name, MFSNAMELEN);
    213 	nfsm_reqdone;
    214 	crfree(cred);
    215 	return (error);
    216 }
    217 
    218 #ifndef NFS_V2_ONLY
    219 /*
    220  * nfs version 3 fsinfo rpc call
    221  */
    222 int
    223 nfs_fsinfo(nmp, vp, cred, p)
    224 	struct nfsmount *nmp;
    225 	struct vnode *vp;
    226 	struct ucred *cred;
    227 	struct proc *p;
    228 {
    229 	struct nfsv3_fsinfo *fsp;
    230 	caddr_t cp;
    231 	int32_t t1, t2;
    232 	u_int32_t *tl, pref, max;
    233 	caddr_t bpos, dpos, cp2;
    234 	int error = 0, retattr;
    235 	struct mbuf *mreq, *mrep, *md, *mb;
    236 	u_int64_t maxfsize;
    237 
    238 	nfsstats.rpccnt[NFSPROC_FSINFO]++;
    239 	nfsm_reqhead(vp, NFSPROC_FSINFO, NFSX_FH(1));
    240 	nfsm_fhtom(vp, 1);
    241 	nfsm_request(vp, NFSPROC_FSINFO, p, cred);
    242 	nfsm_postop_attr(vp, retattr, 0);
    243 	if (!error) {
    244 		nfsm_dissect(fsp, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
    245 		pref = fxdr_unsigned(u_int32_t, fsp->fs_wtpref);
    246 		if (pref < nmp->nm_wsize && pref >= NFS_FABLKSIZE)
    247 			nmp->nm_wsize = (pref + NFS_FABLKSIZE - 1) &
    248 				~(NFS_FABLKSIZE - 1);
    249 		max = fxdr_unsigned(u_int32_t, fsp->fs_wtmax);
    250 		if (max < nmp->nm_wsize && max > 0) {
    251 			nmp->nm_wsize = max & ~(NFS_FABLKSIZE - 1);
    252 			if (nmp->nm_wsize == 0)
    253 				nmp->nm_wsize = max;
    254 		}
    255 		pref = fxdr_unsigned(u_int32_t, fsp->fs_rtpref);
    256 		if (pref < nmp->nm_rsize && pref >= NFS_FABLKSIZE)
    257 			nmp->nm_rsize = (pref + NFS_FABLKSIZE - 1) &
    258 				~(NFS_FABLKSIZE - 1);
    259 		max = fxdr_unsigned(u_int32_t, fsp->fs_rtmax);
    260 		if (max < nmp->nm_rsize && max > 0) {
    261 			nmp->nm_rsize = max & ~(NFS_FABLKSIZE - 1);
    262 			if (nmp->nm_rsize == 0)
    263 				nmp->nm_rsize = max;
    264 		}
    265 		pref = fxdr_unsigned(u_int32_t, fsp->fs_dtpref);
    266 		if (pref < nmp->nm_readdirsize && pref >= NFS_DIRFRAGSIZ)
    267 			nmp->nm_readdirsize = (pref + NFS_DIRFRAGSIZ - 1) &
    268 				~(NFS_DIRFRAGSIZ - 1);
    269 		if (max < nmp->nm_readdirsize && max > 0) {
    270 			nmp->nm_readdirsize = max & ~(NFS_DIRFRAGSIZ - 1);
    271 			if (nmp->nm_readdirsize == 0)
    272 				nmp->nm_readdirsize = max;
    273 		}
    274 		/* XXX */
    275 		nmp->nm_maxfilesize = (u_int64_t)0x80000000 * DEV_BSIZE - 1;
    276 		maxfsize = fxdr_hyper(&fsp->fs_maxfilesize);
    277 		if (maxfsize > 0 && maxfsize < nmp->nm_maxfilesize)
    278 			nmp->nm_maxfilesize = maxfsize;
    279 		nmp->nm_iflag |= NFSMNT_GOTFSINFO;
    280 	}
    281 	nfsm_reqdone;
    282 	return (error);
    283 }
    284 #endif
    285 
    286 /*
    287  * Mount a remote root fs via. NFS.  It goes like this:
    288  * - Call nfs_boot_init() to fill in the nfs_diskless struct
    289  * - build the rootfs mount point and call mountnfs() to do the rest.
    290  */
    291 int
    292 nfs_mountroot()
    293 {
    294 	struct nfs_diskless *nd;
    295 	struct vattr attr;
    296 	struct mount *mp;
    297 	struct vnode *vp;
    298 	struct proc *procp;
    299 	long n;
    300 	int error;
    301 
    302 	procp = curproc; /* XXX */
    303 
    304 	if (root_device->dv_class != DV_IFNET)
    305 		return (ENODEV);
    306 
    307 	/*
    308 	 * XXX time must be non-zero when we init the interface or else
    309 	 * the arp code will wedge.  [Fixed now in if_ether.c]
    310 	 * However, the NFS attribute cache gives false "hits" when
    311 	 * time.tv_sec < NFS_ATTRTIMEO(np) so keep this in for now.
    312 	 */
    313 	if (time.tv_sec < NFS_MAXATTRTIMO)
    314 		time.tv_sec = NFS_MAXATTRTIMO;
    315 
    316 	/*
    317 	 * Call nfs_boot_init() to fill in the nfs_diskless struct.
    318 	 * Side effect:  Finds and configures a network interface.
    319 	 */
    320 	nd = malloc(sizeof(*nd), M_NFSMNT, M_WAITOK);
    321 	memset((caddr_t)nd, 0, sizeof(*nd));
    322 	error = nfs_boot_init(nd, procp);
    323 	if (error) {
    324 		free(nd, M_NFSMNT);
    325 		return (error);
    326 	}
    327 
    328 	/*
    329 	 * Create the root mount point.
    330 	 */
    331 	error = nfs_mount_diskless(&nd->nd_root, "/", &mp, &vp, procp);
    332 	if (error)
    333 		goto out;
    334 	printf("root on %s\n", nd->nd_root.ndm_host);
    335 
    336 	/*
    337 	 * Link it into the mount list.
    338 	 */
    339 	simple_lock(&mountlist_slock);
    340 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    341 	simple_unlock(&mountlist_slock);
    342 	rootvp = vp;
    343 	mp->mnt_vnodecovered = NULLVP;
    344 	vfs_unbusy(mp);
    345 
    346 	/* Get root attributes (for the time). */
    347 	error = VOP_GETATTR(vp, &attr, procp->p_ucred, procp);
    348 	if (error)
    349 		panic("nfs_mountroot: getattr for root");
    350 	n = attr.va_atime.tv_sec;
    351 #ifdef	DEBUG
    352 	printf("root time: 0x%lx\n", n);
    353 #endif
    354 	inittodr(n);
    355 
    356 out:
    357 	if (error)
    358 		nfs_boot_cleanup(nd, procp);
    359 	free(nd, M_NFSMNT);
    360 	return (error);
    361 }
    362 
    363 /*
    364  * Internal version of mount system call for diskless setup.
    365  * Separate function because we used to call it twice.
    366  * (once for root and once for swap)
    367  */
    368 static int
    369 nfs_mount_diskless(ndmntp, mntname, mpp, vpp, p)
    370 	struct nfs_dlmount *ndmntp;
    371 	const char *mntname;	/* mount point name */
    372 	struct mount **mpp;
    373 	struct vnode **vpp;
    374 	struct proc *p;
    375 {
    376 	struct mount *mp;
    377 	struct mbuf *m;
    378 	int error;
    379 
    380 	vfs_rootmountalloc(MOUNT_NFS, (char *)mntname, &mp);
    381 
    382 	mp->mnt_op = &nfs_vfsops;
    383 
    384 	/*
    385 	 * Historical practice expects NFS root file systems to
    386 	 * be initially mounted r/w.
    387 	 */
    388 	mp->mnt_flag &= ~MNT_RDONLY;
    389 
    390 	/* Get mbuf for server sockaddr. */
    391 	m = m_get(M_WAIT, MT_SONAME);
    392 	if (m == NULL)
    393 		panic("nfs_mountroot: mget soname for %s", mntname);
    394 	MCLAIM(m, &nfs_mowner);
    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 nfsmount *nmp = VFSTONFS(mp);
    566 	struct sockaddr *sa;
    567 	struct vnode *vp;
    568 	char *pth, *hst;
    569 	size_t len;
    570 	u_char *nfh;
    571 
    572 	error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args));
    573 	if (error)
    574 		return (error);
    575 
    576 	if (mp->mnt_flag & MNT_GETARGS) {
    577 
    578 		if (nmp == NULL)
    579 			return (EIO);
    580 		if (args.addr != NULL) {
    581 			sa = mtod(nmp->nm_nam, struct sockaddr *);
    582 			error = copyout(sa, args.addr, sa->sa_len);
    583 			if (error)
    584 				return (error);
    585 			args.addrlen = sa->sa_len;
    586 		} else
    587 			args.addrlen = 0;
    588 
    589 		args.version = NFS_ARGSVERSION;
    590 		args.sotype = nmp->nm_sotype;
    591 		args.proto = nmp->nm_soproto;
    592 		args.fh = NULL;
    593 		args.fhsize = 0;
    594 		args.flags = nmp->nm_flag;
    595 		args.wsize = nmp->nm_wsize;
    596 		args.rsize = nmp->nm_rsize;
    597 		args.readdirsize = nmp->nm_readdirsize;
    598 		args.timeo = nmp->nm_timeo;
    599 		args.retrans = nmp->nm_retry;
    600 		args.maxgrouplist = nmp->nm_numgrps;
    601 		args.readahead = nmp->nm_readahead;
    602 		args.leaseterm = nmp->nm_leaseterm;
    603 		args.deadthresh = nmp->nm_deadthresh;
    604 		args.hostname = NULL;
    605 		return (copyout(&args, data, sizeof(args)));
    606 	}
    607 
    608 	if (args.version != NFS_ARGSVERSION)
    609 		return (EPROGMISMATCH);
    610 #ifdef NFS_V2_ONLY
    611 	if (args.flags & NFSMNT_NQNFS)
    612 		return (EPROGUNAVAIL);
    613 	if (args.flags & NFSMNT_NFSV3)
    614 		return (EPROGMISMATCH);
    615 #endif
    616 	if (mp->mnt_flag & MNT_UPDATE) {
    617 		if (nmp == NULL)
    618 			return (EIO);
    619 		/*
    620 		 * When doing an update, we can't change from or to
    621 		 * v3 and/or nqnfs, or change cookie translation
    622 		 */
    623 		args.flags = (args.flags &
    624 		    ~(NFSMNT_NFSV3|NFSMNT_NQNFS|NFSMNT_XLATECOOKIE)) |
    625 		    (nmp->nm_flag &
    626 			(NFSMNT_NFSV3|NFSMNT_NQNFS|NFSMNT_XLATECOOKIE));
    627 		nfs_decode_args(nmp, &args);
    628 		return (0);
    629 	}
    630 	if (args.fhsize < 0 || args.fhsize > NFSX_V3FHMAX)
    631 		return (EINVAL);
    632 	MALLOC(nfh, u_char *, NFSX_V3FHMAX, M_TEMP, M_WAITOK);
    633 	error = copyin((caddr_t)args.fh, (caddr_t)nfh, args.fhsize);
    634 	if (error)
    635 		return (error);
    636 	MALLOC(pth, char *, MNAMELEN, M_TEMP, M_WAITOK);
    637 	error = copyinstr(path, pth, MNAMELEN - 1, &len);
    638 	if (error)
    639 		goto free_nfh;
    640 	memset(&pth[len], 0, MNAMELEN - len);
    641 	MALLOC(hst, char *, MNAMELEN, M_TEMP, M_WAITOK);
    642 	error = copyinstr(args.hostname, hst, MNAMELEN - 1, &len);
    643 	if (error)
    644 		goto free_pth;
    645 	memset(&hst[len], 0, MNAMELEN - len);
    646 	/* sockargs() call must be after above copyin() calls */
    647 	error = sockargs(&nam, (caddr_t)args.addr, args.addrlen, MT_SONAME);
    648 	if (error)
    649 		goto free_hst;
    650 	MCLAIM(nam, &nfs_mowner);
    651 	args.fh = nfh;
    652 	error = mountnfs(&args, mp, nam, pth, hst, &vp, p);
    653 
    654 free_hst:
    655 	FREE(hst, M_TEMP);
    656 free_pth:
    657 	FREE(pth, M_TEMP);
    658 free_nfh:
    659 	FREE(nfh, M_TEMP);
    660 
    661 	return (error);
    662 }
    663 
    664 /*
    665  * Common code for mount and mountroot
    666  */
    667 int
    668 mountnfs(argp, mp, nam, pth, hst, vpp, p)
    669 	struct nfs_args *argp;
    670 	struct mount *mp;
    671 	struct mbuf *nam;
    672 	const char *pth, *hst;
    673 	struct vnode **vpp;
    674 	struct proc *p;
    675 {
    676 	struct nfsmount *nmp;
    677 	struct nfsnode *np;
    678 	int error;
    679 	struct vattr *attrs;
    680 	struct ucred *cr;
    681 
    682 	/*
    683 	 * If the number of nfs iothreads to use has never
    684 	 * been set, create a reasonable number of them.
    685 	 */
    686 
    687 	if (nfs_niothreads < 0) {
    688 		nfs_niothreads = NFS_DEFAULT_NIOTHREADS;
    689 		nfs_getset_niothreads(TRUE);
    690 	}
    691 
    692 	if (mp->mnt_flag & MNT_UPDATE) {
    693 		nmp = VFSTONFS(mp);
    694 		/* update paths, file handles, etc, here	XXX */
    695 		m_freem(nam);
    696 		return (0);
    697 	} else {
    698 		MALLOC(nmp, struct nfsmount *, sizeof (struct nfsmount),
    699 		    M_NFSMNT, M_WAITOK);
    700 		memset((caddr_t)nmp, 0, sizeof (struct nfsmount));
    701 		mp->mnt_data = nmp;
    702 		TAILQ_INIT(&nmp->nm_uidlruhead);
    703 		TAILQ_INIT(&nmp->nm_bufq);
    704 	}
    705 	vfs_getnewfsid(mp);
    706 	nmp->nm_mountp = mp;
    707 
    708 #ifndef NFS_V2_ONLY
    709 	if (argp->flags & NFSMNT_NQNFS)
    710 		/*
    711 		 * We have to set mnt_maxsymlink to a non-zero value so
    712 		 * that COMPAT_43 routines will know that we are setting
    713 		 * the d_type field in directories (and can zero it for
    714 		 * unsuspecting binaries).
    715 		 */
    716 		mp->mnt_maxsymlinklen = 1;
    717 #endif
    718 
    719 #ifndef NFS_V2_ONLY
    720 	if ((argp->flags & NFSMNT_NFSV3) == 0)
    721 #endif
    722 		/*
    723 		 * V2 can only handle 32 bit filesizes. For v3, nfs_fsinfo
    724 		 * will fill this in.
    725 		 */
    726 		nmp->nm_maxfilesize = 0xffffffffLL;
    727 
    728 	nmp->nm_timeo = NFS_TIMEO;
    729 	nmp->nm_retry = NFS_RETRANS;
    730 	nmp->nm_wsize = NFS_WSIZE;
    731 	nmp->nm_rsize = NFS_RSIZE;
    732 	nmp->nm_readdirsize = NFS_READDIRSIZE;
    733 	nmp->nm_numgrps = NFS_MAXGRPS;
    734 	nmp->nm_readahead = NFS_DEFRAHEAD;
    735 	nmp->nm_leaseterm = NQ_DEFLEASE;
    736 	nmp->nm_deadthresh = NQ_DEADTHRESH;
    737 	CIRCLEQ_INIT(&nmp->nm_timerhead);
    738 	nmp->nm_inprog = NULLVP;
    739 #ifdef COMPAT_09
    740 	mp->mnt_stat.f_type = 2;
    741 #else
    742 	mp->mnt_stat.f_type = 0;
    743 #endif
    744 	strncpy(&mp->mnt_stat.f_fstypename[0], mp->mnt_op->vfs_name,
    745 	    MFSNAMELEN);
    746 	memcpy(mp->mnt_stat.f_mntfromname, hst, MNAMELEN);
    747 	memcpy(mp->mnt_stat.f_mntonname, pth, MNAMELEN);
    748 	nmp->nm_nam = nam;
    749 
    750 	/* Set up the sockets and per-host congestion */
    751 	nmp->nm_sotype = argp->sotype;
    752 	nmp->nm_soproto = argp->proto;
    753 
    754 	nfs_decode_args(nmp, argp);
    755 
    756 	mp->mnt_fs_bshift = ffs(MIN(nmp->nm_rsize, nmp->nm_wsize)) - 1;
    757 	mp->mnt_dev_bshift = DEV_BSHIFT;
    758 
    759 	/*
    760 	 * For Connection based sockets (TCP,...) defer the connect until
    761 	 * the first request, in case the server is not responding.
    762 	 */
    763 	if (nmp->nm_sotype == SOCK_DGRAM &&
    764 		(error = nfs_connect(nmp, (struct nfsreq *)0)))
    765 		goto bad;
    766 
    767 	/*
    768 	 * This is silly, but it has to be set so that vinifod() works.
    769 	 * We do not want to do an nfs_statfs() here since we can get
    770 	 * stuck on a dead server and we are holding a lock on the mount
    771 	 * point.
    772 	 */
    773 	mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA;
    774 	error = nfs_nget(mp, (nfsfh_t *)argp->fh, argp->fhsize, &np);
    775 	if (error)
    776 		goto bad;
    777 	*vpp = NFSTOV(np);
    778 	MALLOC(attrs, struct vattr *, sizeof(struct vattr), M_TEMP, M_WAITOK);
    779 	VOP_GETATTR(*vpp, attrs, p->p_ucred, p);
    780 	if ((nmp->nm_flag & NFSMNT_NFSV3) && ((*vpp)->v_type == VDIR)) {
    781 		cr = crget();
    782 		cr->cr_uid = attrs->va_uid;
    783 		cr->cr_gid = attrs->va_gid;
    784 		cr->cr_ngroups = 0;
    785 		nfs_cookieheuristic(*vpp, &nmp->nm_iflag, p, cr);
    786 		crfree(cr);
    787 	}
    788 	FREE(attrs, M_TEMP);
    789 
    790 	/*
    791 	 * A reference count is needed on the nfsnode representing the
    792 	 * remote root.  If this object is not persistent, then backward
    793 	 * traversals of the mount point (i.e. "..") will not work if
    794 	 * the nfsnode gets flushed out of the cache. Ufs does not have
    795 	 * this problem, because one can identify root inodes by their
    796 	 * number == ROOTINO (2). So, just unlock, but no rele.
    797 	 */
    798 
    799 	nmp->nm_vnode = *vpp;
    800 	VOP_UNLOCK(*vpp, 0);
    801 
    802 	return (0);
    803 bad:
    804 	nfs_disconnect(nmp);
    805 	free((caddr_t)nmp, M_NFSMNT);
    806 	m_freem(nam);
    807 	return (error);
    808 }
    809 
    810 /*
    811  * unmount system call
    812  */
    813 int
    814 nfs_unmount(mp, mntflags, p)
    815 	struct mount *mp;
    816 	int mntflags;
    817 	struct proc *p;
    818 {
    819 	struct nfsmount *nmp;
    820 	struct vnode *vp;
    821 	int error, flags = 0;
    822 
    823 	if (mntflags & MNT_FORCE)
    824 		flags |= FORCECLOSE;
    825 	nmp = VFSTONFS(mp);
    826 	/*
    827 	 * Goes something like this..
    828 	 * - Check for activity on the root vnode (other than ourselves).
    829 	 * - Call vflush() to clear out vnodes for this file system,
    830 	 *   except for the root vnode.
    831 	 * - Decrement reference on the vnode representing remote root.
    832 	 * - Close the socket
    833 	 * - Free up the data structures
    834 	 */
    835 	/*
    836 	 * We need to decrement the ref. count on the nfsnode representing
    837 	 * the remote root.  See comment in mountnfs().  The VFS unmount()
    838 	 * has done vput on this vnode, otherwise we would get deadlock!
    839 	 */
    840 	vp = nmp->nm_vnode;
    841 	error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
    842 	if (error != 0)
    843 		return error;
    844 
    845 	if ((mntflags & MNT_FORCE) == 0 && vp->v_usecount > 2) {
    846 		vput(vp);
    847 		return (EBUSY);
    848 	}
    849 
    850 	/*
    851 	 * Must handshake with nqnfs_clientd() if it is active.
    852 	 */
    853 	nmp->nm_iflag |= NFSMNT_DISMINPROG;
    854 	while (nmp->nm_inprog != NULLVP)
    855 		(void) tsleep((caddr_t)&lbolt, PSOCK, "nfsdism", 0);
    856 	error = vflush(mp, vp, flags);
    857 	if (error) {
    858 		vput(vp);
    859 		nmp->nm_iflag &= ~NFSMNT_DISMINPROG;
    860 		return (error);
    861 	}
    862 
    863 	/*
    864 	 * We are now committed to the unmount; mark the mount structure
    865 	 * as doomed so that any sleepers kicked awake by nfs_disconnect
    866 	 * will go away cleanly.
    867 	 */
    868 	nmp->nm_iflag |= NFSMNT_DISMNT;
    869 
    870 	/*
    871 	 * There are two reference counts to get rid of here
    872 	 * (see comment in mountnfs()).
    873 	 */
    874 	vrele(vp);
    875 	vput(vp);
    876 	vgone(vp);
    877 	nfs_disconnect(nmp);
    878 	m_freem(nmp->nm_nam);
    879 
    880 	/*
    881 	 * For NQNFS, let the server daemon free the nfsmount structure.
    882 	 */
    883 	if ((nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB)) == 0)
    884 		free((caddr_t)nmp, M_NFSMNT);
    885 	return (0);
    886 }
    887 
    888 /*
    889  * Return root of a filesystem
    890  */
    891 int
    892 nfs_root(mp, vpp)
    893 	struct mount *mp;
    894 	struct vnode **vpp;
    895 {
    896 	struct vnode *vp;
    897 	struct nfsmount *nmp;
    898 	int error;
    899 
    900 	nmp = VFSTONFS(mp);
    901 	vp = nmp->nm_vnode;
    902 	error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
    903 	if (error != 0)
    904 		return error;
    905 	if (vp->v_type == VNON)
    906 		vp->v_type = VDIR;
    907 	vp->v_flag = VROOT;
    908 	*vpp = vp;
    909 	return (0);
    910 }
    911 
    912 extern int syncprt;
    913 
    914 /*
    915  * Flush out the buffer cache
    916  */
    917 /* ARGSUSED */
    918 int
    919 nfs_sync(mp, waitfor, cred, p)
    920 	struct mount *mp;
    921 	int waitfor;
    922 	struct ucred *cred;
    923 	struct proc *p;
    924 {
    925 	struct vnode *vp;
    926 	int error, allerror = 0;
    927 
    928 	/*
    929 	 * Force stale buffer cache information to be flushed.
    930 	 */
    931 loop:
    932 	for (vp = mp->mnt_vnodelist.lh_first;
    933 	     vp != NULL;
    934 	     vp = vp->v_mntvnodes.le_next) {
    935 		/*
    936 		 * If the vnode that we are about to sync is no longer
    937 		 * associated with this mount point, start over.
    938 		 */
    939 		if (vp->v_mount != mp)
    940 			goto loop;
    941 		if (waitfor == MNT_LAZY || VOP_ISLOCKED(vp) ||
    942 		    (LIST_EMPTY(&vp->v_dirtyblkhd) &&
    943 		     vp->v_uobj.uo_npages == 0))
    944 			continue;
    945 		if (vget(vp, LK_EXCLUSIVE))
    946 			goto loop;
    947 		error = VOP_FSYNC(vp, cred,
    948 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p);
    949 		if (error)
    950 			allerror = error;
    951 		vput(vp);
    952 	}
    953 	return (allerror);
    954 }
    955 
    956 /*
    957  * NFS flat namespace lookup.
    958  * Currently unsupported.
    959  */
    960 /* ARGSUSED */
    961 int
    962 nfs_vget(mp, ino, vpp)
    963 	struct mount *mp;
    964 	ino_t ino;
    965 	struct vnode **vpp;
    966 {
    967 
    968 	return (EOPNOTSUPP);
    969 }
    970 
    971 /*
    972  * Do that sysctl thang...
    973  */
    974 int
    975 nfs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
    976 	int *name;
    977 	u_int namelen;
    978 	void *oldp;
    979 	size_t *oldlenp;
    980 	void *newp;
    981 	size_t newlen;
    982 	struct proc *p;
    983 {
    984 	int rv;
    985 
    986 	/*
    987 	 * All names at this level are terminal.
    988 	 */
    989 	if(namelen > 1)
    990 		return ENOTDIR;	/* overloaded */
    991 
    992 	switch(name[0]) {
    993 	case NFS_NFSSTATS:
    994 		if(!oldp) {
    995 			*oldlenp = sizeof nfsstats;
    996 			return 0;
    997 		}
    998 
    999 		if(*oldlenp < sizeof nfsstats) {
   1000 			*oldlenp = sizeof nfsstats;
   1001 			return ENOMEM;
   1002 		}
   1003 
   1004 		rv = copyout(&nfsstats, oldp, sizeof nfsstats);
   1005 		if(rv) return rv;
   1006 
   1007 		if(newp && newlen != sizeof nfsstats)
   1008 			return EINVAL;
   1009 
   1010 		if(newp) {
   1011 			return copyin(newp, &nfsstats, sizeof nfsstats);
   1012 		}
   1013 		return 0;
   1014 
   1015 	case NFS_IOTHREADS:
   1016 		nfs_getset_niothreads(0);
   1017 
   1018 		rv = (sysctl_int(oldp, oldlenp, newp, newlen,
   1019 		    &nfs_niothreads));
   1020 
   1021 		if (newp)
   1022 			nfs_getset_niothreads(1);
   1023 
   1024 		return rv;
   1025 
   1026 	default:
   1027 		return EOPNOTSUPP;
   1028 	}
   1029 }
   1030 
   1031 
   1032 /*
   1033  * At this point, this should never happen
   1034  */
   1035 /* ARGSUSED */
   1036 int
   1037 nfs_fhtovp(mp, fhp, vpp)
   1038 	struct mount *mp;
   1039 	struct fid *fhp;
   1040 	struct vnode **vpp;
   1041 {
   1042 
   1043 	return (EINVAL);
   1044 }
   1045 
   1046 /* ARGSUSED */
   1047 int
   1048 nfs_checkexp(mp, nam, exflagsp, credanonp)
   1049 	struct mount *mp;
   1050 	struct mbuf *nam;
   1051 	int *exflagsp;
   1052 	struct ucred **credanonp;
   1053 {
   1054 
   1055 	return (EINVAL);
   1056 }
   1057 
   1058 /*
   1059  * Vnode pointer to File handle, should never happen either
   1060  */
   1061 /* ARGSUSED */
   1062 int
   1063 nfs_vptofh(vp, fhp)
   1064 	struct vnode *vp;
   1065 	struct fid *fhp;
   1066 {
   1067 
   1068 	return (EINVAL);
   1069 }
   1070 
   1071 /*
   1072  * Vfs start routine, a no-op.
   1073  */
   1074 /* ARGSUSED */
   1075 int
   1076 nfs_start(mp, flags, p)
   1077 	struct mount *mp;
   1078 	int flags;
   1079 	struct proc *p;
   1080 {
   1081 
   1082 	return (0);
   1083 }
   1084 
   1085 /*
   1086  * Do operations associated with quotas, not supported
   1087  */
   1088 /* ARGSUSED */
   1089 int
   1090 nfs_quotactl(mp, cmd, uid, arg, p)
   1091 	struct mount *mp;
   1092 	int cmd;
   1093 	uid_t uid;
   1094 	caddr_t arg;
   1095 	struct proc *p;
   1096 {
   1097 
   1098 	return (EOPNOTSUPP);
   1099 }
   1100